#include <bits/stdc++.h> using namespace std; #define ll long long vector<int> solveFast(int n, long long kk) { int64_t k = kk; int64_t needed = 1LL * n * (n - 1) / 2; if(needed % 2) { return vector<int> (); } needed /= 2; vector<vector<int64_t>> dp(n + 1); dp[1].push_back(1); dp[0].push_back(1); for(int i = 1; i <= 100; ++i) { dp[0].push_back(0); dp[1].push_back(0); } for(int i = 2; i <= n; ++i) { for(int j = 0;true; ++j) { int64_t res = 0; for(int here = 0; here <= min(i - 1, j); ++here) { if(j - here + 1 > (int) dp[i - 1].size()) { res = -1; break; } else { res += dp[i - 1][j - here]; if(res > 1e18) { res = -1; break; } } } if(res == -1) { break; } else { dp[i].push_back(res); } } } auto getCount = [&] (int elements, int64_t inversions) -> int64_t { int64_t maxx = 1LL * elements * (elements - 1) / 2; if(inversions > maxx) { return 0; } inversions = min(inversions, maxx - inversions); if(inversions >= (int64_t) dp[elements].size()) { return -1; } return dp[elements][inversions]; }; vector<int> aib(n + 1, 0); vector<int> ans; auto insert = [&] (int x) { while(x <= n) { aib[x] += 1; x += (x & (-x)); } }; auto query = [&] (int x) { int ans = 0; while(x > 0) { ans += aib[x]; x -= (x & (-x)); } return ans; }; auto getIndex = [&] (int x) { int ans = 0; for(int lf = 1, rt = n; lf <= rt;) { int mid = (lf + rt) / 2; if(mid - query(mid - 1) <= x) { ans = mid; lf = mid + 1; } else { rt = mid - 1; } } return ans; }; for(int i = 1; i <= n; ++i) { int start = 1; int64_t diff = needed - 1LL * (n - i) * (n - i - 1) / 2; start = max((int64_t) 1, diff + 1); if(start > n) { return vector<int> (); } int64_t past = 0, prev = 0; int who = -1; while(true and start <= n) { int64_t current = getCount(n - i, needed - (start - 1)); if(current == -1) { who = getIndex(start); break; } else { past += current; if(past >= k) { who = getIndex(start); break; } prev = past; } start += 1; } if(who == -1) { return vector<int> (); } k -= prev; needed -= (start - 1); ans.push_back(who); insert(who); } return ans; } int main() { int n; cin >> n; long long k; cin >> k; auto sol = solveFast(n, k); if(sol.empty()) { cout << "NIE\n"; } else { cout << "TAK\n"; for(auto val : sol) { cout << val << " "; } cout << "\n"; } }
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 | #include <bits/stdc++.h> using namespace std; #define ll long long vector<int> solveFast(int n, long long kk) { int64_t k = kk; int64_t needed = 1LL * n * (n - 1) / 2; if(needed % 2) { return vector<int> (); } needed /= 2; vector<vector<int64_t>> dp(n + 1); dp[1].push_back(1); dp[0].push_back(1); for(int i = 1; i <= 100; ++i) { dp[0].push_back(0); dp[1].push_back(0); } for(int i = 2; i <= n; ++i) { for(int j = 0;true; ++j) { int64_t res = 0; for(int here = 0; here <= min(i - 1, j); ++here) { if(j - here + 1 > (int) dp[i - 1].size()) { res = -1; break; } else { res += dp[i - 1][j - here]; if(res > 1e18) { res = -1; break; } } } if(res == -1) { break; } else { dp[i].push_back(res); } } } auto getCount = [&] (int elements, int64_t inversions) -> int64_t { int64_t maxx = 1LL * elements * (elements - 1) / 2; if(inversions > maxx) { return 0; } inversions = min(inversions, maxx - inversions); if(inversions >= (int64_t) dp[elements].size()) { return -1; } return dp[elements][inversions]; }; vector<int> aib(n + 1, 0); vector<int> ans; auto insert = [&] (int x) { while(x <= n) { aib[x] += 1; x += (x & (-x)); } }; auto query = [&] (int x) { int ans = 0; while(x > 0) { ans += aib[x]; x -= (x & (-x)); } return ans; }; auto getIndex = [&] (int x) { int ans = 0; for(int lf = 1, rt = n; lf <= rt;) { int mid = (lf + rt) / 2; if(mid - query(mid - 1) <= x) { ans = mid; lf = mid + 1; } else { rt = mid - 1; } } return ans; }; for(int i = 1; i <= n; ++i) { int start = 1; int64_t diff = needed - 1LL * (n - i) * (n - i - 1) / 2; start = max((int64_t) 1, diff + 1); if(start > n) { return vector<int> (); } int64_t past = 0, prev = 0; int who = -1; while(true and start <= n) { int64_t current = getCount(n - i, needed - (start - 1)); if(current == -1) { who = getIndex(start); break; } else { past += current; if(past >= k) { who = getIndex(start); break; } prev = past; } start += 1; } if(who == -1) { return vector<int> (); } k -= prev; needed -= (start - 1); ans.push_back(who); insert(who); } return ans; } int main() { int n; cin >> n; long long k; cin >> k; auto sol = solveFast(n, k); if(sol.empty()) { cout << "NIE\n"; } else { cout << "TAK\n"; for(auto val : sol) { cout << val << " "; } cout << "\n"; } } |