Beingamanforever

Global Round 27 C

Oct 28th, 2024
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.16 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
  4. #define NeedForSpeed                  \
  5.     ios_base::sync_with_stdio(false); \
  6.     cin.tie(NULL);                    \
  7.     cout.tie(NULL);
  8. #define int long long
  9. #define all(x) (x).begin(), (x).end()
  10. typedef vector<int> vi;
  11. typedef vector<bool> vb;
  12. typedef vector<vi> vvi;
  13. typedef vector<pair<int, int>> vpi;
  14. #define f first
  15. #define s second
  16. #define endl "\n"
  17. const int mod = 1000000007;
  18. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  19. void solve()
  20. {
  21.     // for n -> odd, we are bounded as last is &, so we keep (n-1, n) at the last as (n-1 & n) = n-1
  22.     // but we can go more than (n-1), by turing on the last bit of (n-1) (which is even) so we use (3,1)
  23.     // for n -> even, try to chase 2^k -1
  24.     int n;
  25.     cin >> n;
  26.     if (n % 2 == 1)
  27.     {
  28.         cout << n << endl;
  29.         // odd case
  30.         for (int i = 1; i <= n - 2; i++)
  31.         {
  32.             if (i != 1 && i != 3)
  33.             {
  34.                 cout << i << " ";
  35.             }
  36.         }
  37.         cout << 3 << " " << 1 << " " << (n - 1) << " " << n << endl;
  38.     }
  39.     else
  40.     {
  41.         // if it is a power of 2, then we can keep the last as n and n-1 before it to reach 2^k-1
  42.         int lg = 0;
  43.         while ((1 << lg) <= n)
  44.         {
  45.             lg++;
  46.         }
  47.         lg--;
  48.         cout << ((1 << (lg + 1)) - 1) << endl;
  49.         int m = 0;
  50.         vi a(n + 1);
  51.         for (int i = 1; i <= n; i++)
  52.         {
  53.             a[i] = i;
  54.             m |= i;
  55.         }
  56.         int c = 1;
  57.         int r = log2(m);
  58.         for (int i = 0; i < r; i++)
  59.         {
  60.             c *= 2;
  61.         }
  62.         // not very intutive
  63.         swap(a[n], a[c]);
  64.         swap(a[c - 1], a[n - 1]);
  65.         swap(a[c - 2], a[n - 2]);
  66.         swap(a[1], a[n - 4]);
  67.         swap(a[n - 3], a[3]);
  68.         for (int i = 1; i <= n; i++)
  69.         {
  70.             cout << a[i] << " ";
  71.         }
  72.         cout << endl;
  73.     }
  74.     return;
  75. }
  76.  
  77. signed main()
  78. {
  79.     NeedForSpeed;
  80.     int t = 1;
  81.     cin >> t;
  82.     while (t--)
  83.     {
  84.         solve();
  85.     }
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment