#include<bits/stdc++.h> usingnamespace std; #ifdef LOCAL #define debug(...) fprintf(stdout, ##__VA_ARGS__) #else #define debug(...) void(0) #endif //#define int long long int ptr = 0; char buf[1 << 23]; inlinevoidwriteChar(char c){ if (ptr == 1 << 23) fwrite(buf, 1, ptr, stdout), ptr = 0; buf[ptr++] = c; } inlinevoidwriteInt(int x){ int t = 0; char s[10]; while (x) s[t++] = (char)('0' + x % 10), x /= 10; while (t) writeChar(s[--t]); } inlinevoidflushOut(){ if (ptr) fwrite(buf, 1, ptr, stdout), ptr = 0; } inlineintread(){int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+ (x<<3)+(ch^48);ch=getchar();}return x*f;} #define Z(x) (x)*(x) #define pb push_back #define fi first #define se second //#define M //#define mo #define N 35 vector<int> solveP(int n); vector<int> solveQ(int n); int n, m, i, j, k, T; vector<int>P[N], Q[N]; int G[N], path[N];
vector<int> Hami(int n, int os, int L){ int i, j; for(i = 0; i < n; ++i) G[i] = 0; for(i = 0; i < n; ++i) for(j = i + 1; j < n; ++j) if(L % (i + j + 2 - os) == 0) { G[i] |= (1 << j); G[j] |= (1 << i); // debug("Cun(%d %d)\n", i, j); } vector<int>ans; path[0] = 0; // debug("--------\n"); function<bool(int, int)> dfs = [&](int u, int mask) -> bool { // debug("%d %d\n", u, mask); if(mask == (1 << n) - 1) { // debug("step 1\n"); if(u == 1) { // debug("step 2!\n"); for(i = 0; i < n; ++i) ans.pb(path[i] + 1); returntrue; } returnfalse; } int avail = G[u] & ~mask; vector<pair<int, int> > cand; for(int v = 0; v < n; ++v) if((avail >> v) & 1) { int cnt = __builtin_popcount(G[v] & ~mask); cand.pb({cnt, v}); } sort(cand.begin(), cand.end()); for(auto t : cand) { path[__builtin_popcount(mask)] = t.se; if(dfs(t.se, mask | (1 << t.se))) returntrue; } returnfalse; }; bool x = dfs(0, 1); // printf("For %d %d\n", n, os); // cout << x << endl; return ans; }
voidinit(){ int p[N]; for(n = 1; n <= 13; ++n) { for(i = 1; i <= n; ++i) p[i] = i; do { int L = 1; for(i = 1; i <= n; ++i) { int x = p[i] + p[i % n + 1]; L = L / __gcd(L, x) * x; if(L > 20 * n) break; } if(i > n) break; }while(next_permutation(p + 1, p + n + 1)); for(i = 1; i <= n; ++i) P[n].pb(p[i]); // for(auto t : P[n]) debug("%d ", t); debug("\n"); } for(n = 14; n <= 27; ++n) { int lmit = (n <= 25 ? 168 : 336); P[n] = Hami(n, 0, lmit); Q[n] = Hami(n, 1, lmit); } }
vector<int> solveP(int n){ debug("Construct P %d\n", n); //system("pause"); if(n <= 27) return P[n]; int n1 = (n + 1) / 2, n2 = n / 2; auto a1 = solveQ(n1); auto a2 = solveP(n2); vector<int>ans; reverse(a2.begin(), a2.end()); for(auto t : a1) ans.pb(t * 2 - 1); for(auto t : a2) ans.pb(t * 2); return ans; }
vector<int> solveQ(int n){ debug("Construct Q %d\n", n); if(n <= 27) return Q[n]; vector<int>ans; if(n % 2 == 0) { auto a = solveQ(n / 2); int i = 0; for(auto t : a) ++i, ans.pb(2 * t - i % 2); reverse(a.begin(), a.end()); for(auto t : a) ++i, ans.pb(2 * t - i % 2); return ans; } else { ans.pb(1); auto a = solveP(n / 2); int i = 0; for(auto t : a) ++i, ans.pb(2 * t + i % 2); reverse(a.begin(), a.end()); for(auto t : a) ++i, ans.pb(2 * t + i % 2); return ans; } }
signedmain(){ #ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif // srand(time(NULL)); // T = read(); // while(T--) { // // } init(); T = read(); while(T--) { n = read(); if(n < 28) { for(i = 0; i < n; ++i) writeInt(P[n][i]), writeChar(' '); } else { auto ans = solveP(n); for(auto t : ans) writeInt(t), writeChar(' '); } writeChar('\n'); //flushOut(); } flushOut(); return0; }