fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5. // FUCK BITWISE
  6.  
  7. vector<int> pro(int n) {
  8. int x = log2(n);
  9. int y = (1 << x);
  10. vector<int> ans;
  11. ans.push_back(y);
  12. ans.push_back(y - 1);
  13. ans.push_back(y - 2);
  14. ans.push_back(3);
  15. ans.push_back(1);
  16. ans.push_back(2);
  17. for (int i = 4; i < y - 2; ++i) ans.push_back(i);
  18. for (int i = y + 1; i <= n; ++i) ans.push_back(i);
  19. reverse(ans.begin(), ans.end());
  20. return ans;
  21. }
  22.  
  23. void solve() {
  24. int n; cin >> n;
  25. if (n % 2 == 0) {
  26. vector<int> ans = pro(n - 1);
  27. ans.push_back(n);
  28. cout << n << '\n';
  29. for (auto it : ans) cout << it << ' ';
  30. cout << '\n';
  31. } else {
  32. vector<int> ans = pro(n);
  33. int k = 1 << ((int)log2(n) + 1);
  34. k--;
  35. cout << k << '\n';
  36. for (auto it : ans) cout << it << ' ';
  37. cout << '\n';
  38. }
  39. }
  40.  
  41. int main() {
  42. ios_base::sync_with_stdio(false);
  43. cin.tie(0);
  44. int t; cin >> t;
  45. while (t--) solve();
  46. }
  47.  
Success #stdin #stdout 0.01s 5276KB
stdin
6
5
6
7
8
9
10
stdout
7
5 2 1 3 2 3 4 
6
5 2 1 3 2 3 4 6 
7
7 6 5 2 1 3 2 3 4 
8
7 6 5 2 1 3 2 3 4 8 
15
9 5 4 2 1 3 6 7 8 
10
9 5 4 2 1 3 6 7 8 10