#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef pair < int, int > PII; typedef pair < LL, LL > PLL; typedef pair < LD, LD > PDD; #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() #define MP make_pair template < typename _T > inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template < typename _T, typename... args > void _DBG(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); } #ifdef LOCAL #define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__) #else #define DBG(...) (__VA_ARGS__) #define cerr if(0) cout #endif // ********************** CODE ********************** // const LL INF = 1e18 + 7; vector < int > ans; LL dp[107][5007]; LL dp2[250007][37]; LL solve(LL n, LL inv) { if(n * (n - 1) / 2 < inv || inv < 0) return 0; inv = min(inv, n * (n - 1) / 2 - inv); if(inv == 0) return 1; if(n > 100 && inv > 30) return INF; if(n > 1e4 && inv > 6) return INF; if(n > 100) { return dp2[n][inv]; } return dp[n][inv]; } void Go(LL n, LL k, LL inv) { bool ok = 1; while(ok) { ok = 0; LL val = 0; int p = 1; if(n > 100 && solve(n - 1, inv - (n - 20) + 1) < sqrt(k)) p = n - 20; for(int i = p; i <= n; i++) { if(inv - i + 1 < 0) break; LL tmp = solve(n - 1, inv - i + 1); val += tmp; if(k - val <= 0) { ans.push_back(i); ok = 1, k -= val - tmp, inv -= i - 1; break; } } n--; } } const int N = 25e4 + 7; const int MAX = 1 << 18; LL n, k; int tree[2 * MAX + 7]; bool vis[N]; void update(int e) { e += MAX; while(e) tree[e]--, e >>= 1; } int query(int kk, int node = 1, int l = 0, int r = MAX - 1) { if(l == r) return l; if(tree[2 * node] >= kk) return query(kk, 2 * node, l, (l + r) / 2); return query(kk - tree[2 * node], 2 * node + 1, (l + r) / 2 + 1, r); } int main() { scanf("%lld%lld", &n, &k); if(n == 1 && k == 1) { puts("TAK"); puts("1"); return 0; } if(n % 4 != 0 && n % 4 != 1) { puts("NIE"); return 0; } for(int i = 1; i <= 100; i++) { dp[i][0] = 1; for(int j = 1; j <= i * (i - 1) / 2; j++) { for(int j2 = 1; j2 <= min(i, j + 1); j2++) { dp[i][j] = min(dp[i][j] + dp[i - 1][j + 1 - j2], INF); } } } if(n > 100) { for(int i = 101; i <= n; i++) { dp2[i][0] = 1; for(int j = 1; j <= 30; j++) { if(i > 1e4 && j > 6) break; for(int j2 = 1; j2 <= min(i, j + 1); j2++) { if(i - 1 <= 100) dp2[i][j] = min(dp2[i][j] + dp[i - 1][j + 1 - j2], INF); dp2[i][j] = min(dp2[i][j] + dp2[i - 1][j + 1 - j2], INF); } } } } Go(n, k, n * (n - 1) / 4); if(ans.empty()) { puts("NIE"); return 0; } for(int i = 1; i <= n; i++) tree[i + MAX] = 1; for(int i = MAX - 1; i; i--) tree[i] = tree[2 * i] + tree[2 * i + 1]; puts("TAK"); for(int i = 0; i < sz(ans); i++) { int x = ans[i]; int id = query(x); printf("%d ", id); update(id); vis[id] = 1; } for(int i = n; i; i--) { if(!vis[i]) printf("%d ", i); } puts(""); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef pair < int, int > PII; typedef pair < LL, LL > PLL; typedef pair < LD, LD > PDD; #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() #define MP make_pair template < typename _T > inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template < typename _T, typename... args > void _DBG(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); } #ifdef LOCAL #define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__) #else #define DBG(...) (__VA_ARGS__) #define cerr if(0) cout #endif // ********************** CODE ********************** // const LL INF = 1e18 + 7; vector < int > ans; LL dp[107][5007]; LL dp2[250007][37]; LL solve(LL n, LL inv) { if(n * (n - 1) / 2 < inv || inv < 0) return 0; inv = min(inv, n * (n - 1) / 2 - inv); if(inv == 0) return 1; if(n > 100 && inv > 30) return INF; if(n > 1e4 && inv > 6) return INF; if(n > 100) { return dp2[n][inv]; } return dp[n][inv]; } void Go(LL n, LL k, LL inv) { bool ok = 1; while(ok) { ok = 0; LL val = 0; int p = 1; if(n > 100 && solve(n - 1, inv - (n - 20) + 1) < sqrt(k)) p = n - 20; for(int i = p; i <= n; i++) { if(inv - i + 1 < 0) break; LL tmp = solve(n - 1, inv - i + 1); val += tmp; if(k - val <= 0) { ans.push_back(i); ok = 1, k -= val - tmp, inv -= i - 1; break; } } n--; } } const int N = 25e4 + 7; const int MAX = 1 << 18; LL n, k; int tree[2 * MAX + 7]; bool vis[N]; void update(int e) { e += MAX; while(e) tree[e]--, e >>= 1; } int query(int kk, int node = 1, int l = 0, int r = MAX - 1) { if(l == r) return l; if(tree[2 * node] >= kk) return query(kk, 2 * node, l, (l + r) / 2); return query(kk - tree[2 * node], 2 * node + 1, (l + r) / 2 + 1, r); } int main() { scanf("%lld%lld", &n, &k); if(n == 1 && k == 1) { puts("TAK"); puts("1"); return 0; } if(n % 4 != 0 && n % 4 != 1) { puts("NIE"); return 0; } for(int i = 1; i <= 100; i++) { dp[i][0] = 1; for(int j = 1; j <= i * (i - 1) / 2; j++) { for(int j2 = 1; j2 <= min(i, j + 1); j2++) { dp[i][j] = min(dp[i][j] + dp[i - 1][j + 1 - j2], INF); } } } if(n > 100) { for(int i = 101; i <= n; i++) { dp2[i][0] = 1; for(int j = 1; j <= 30; j++) { if(i > 1e4 && j > 6) break; for(int j2 = 1; j2 <= min(i, j + 1); j2++) { if(i - 1 <= 100) dp2[i][j] = min(dp2[i][j] + dp[i - 1][j + 1 - j2], INF); dp2[i][j] = min(dp2[i][j] + dp2[i - 1][j + 1 - j2], INF); } } } } Go(n, k, n * (n - 1) / 4); if(ans.empty()) { puts("NIE"); return 0; } for(int i = 1; i <= n; i++) tree[i + MAX] = 1; for(int i = MAX - 1; i; i--) tree[i] = tree[2 * i] + tree[2 * i + 1]; puts("TAK"); for(int i = 0; i < sz(ans); i++) { int x = ans[i]; int id = query(x); printf("%d ", id); update(id); vis[id] = 1; } for(int i = n; i; i--) { if(!vis[i]) printf("%d ", i); } puts(""); return 0; } |