fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. ios::sync_with_stdio(false);
  6. cin.tie(nullptr);
  7.  
  8. int t;
  9. cin >> t;
  10. while (t--) {
  11. int n;
  12. cin >> n;
  13. int N = 1 << n;
  14.  
  15. vector<int> a(N);
  16. iota(a.begin(), a.end(), 0);
  17.  
  18. sort(a.begin(), a.end(), [&](int x, int y) {
  19. int px = __builtin_popcount(x);
  20. int py = __builtin_popcount(y);
  21. if (px != py) return px > py;
  22. return x < y;
  23. });
  24.  
  25. for (int i = 0; i < N; i++) {
  26. cout << a[i] << (i + 1 < N ? ' ' : '\n');
  27. }
  28. }
  29. return 0;
  30. }
  31.  
Success #stdin #stdout 0.01s 5292KB
stdin
2
1
2
stdout
1 0
3 1 2 0