Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
- #define NeedForSpeed \
- ios_base::sync_with_stdio(false); \
- cin.tie(NULL); \
- cout.tie(NULL);
- #define int long long
- #define all(x) (x).begin(), (x).end()
- typedef vector<int> vi;
- typedef vector<bool> vb;
- typedef vector<vi> vvi;
- typedef vector<pair<int, int>> vpi;
- #define f first
- #define s second
- #define endl "\n"
- const int mod = 1000000007;
- int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
- void solve()
- {
- // for n -> odd, we are bounded as last is &, so we keep (n-1, n) at the last as (n-1 & n) = n-1
- // 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)
- // for n -> even, try to chase 2^k -1
- int n;
- cin >> n;
- if (n % 2 == 1)
- {
- cout << n << endl;
- // odd case
- for (int i = 1; i <= n - 2; i++)
- {
- if (i != 1 && i != 3)
- {
- cout << i << " ";
- }
- }
- cout << 3 << " " << 1 << " " << (n - 1) << " " << n << endl;
- }
- else
- {
- // 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
- int lg = 0;
- while ((1 << lg) <= n)
- {
- lg++;
- }
- lg--;
- cout << ((1 << (lg + 1)) - 1) << endl;
- int m = 0;
- vi a(n + 1);
- for (int i = 1; i <= n; i++)
- {
- a[i] = i;
- m |= i;
- }
- int c = 1;
- int r = log2(m);
- for (int i = 0; i < r; i++)
- {
- c *= 2;
- }
- // not very intutive
- swap(a[n], a[c]);
- swap(a[c - 1], a[n - 1]);
- swap(a[c - 2], a[n - 2]);
- swap(a[1], a[n - 4]);
- swap(a[n - 3], a[3]);
- for (int i = 1; i <= n; i++)
- {
- cout << a[i] << " ";
- }
- cout << endl;
- }
- return;
- }
- signed main()
- {
- NeedForSpeed;
- int t = 1;
- cin >> t;
- while (t--)
- {
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment