#include <bits/stdc++.h> using namespace std; using ull = unsigned long long; const ull inf = 1000000000000000001ll; vector<int> solve(int n, ull k) { if (n == 1) { if (k == 0) { return {0}; } return {}; } auto c = (ull)n * (n-1) / 2; if (c % 2) { return {}; } c /= 2; vector<vector<ull>> cnt(n+1, {1}); auto f = [&cnt](int n_, ull k_) { auto c_ = (ull)n_ * (n_-1) / 2; if (k_ > c_) { return 0ull; } k_ = min(k_, c_ - k_); if (cnt[n_].size() > k_) { return cnt[n_][k_]; } return inf; }; for (int i=2; i <= n; i++) { for (int j=1; j <= (ull)i*(i-1)/2; j++) { ull s = 0; for (int t=0; s < inf && t < i && j-t >= 0; t++) { s += f(i-1, j-t); } s = min(s, inf); if (s == inf) { break; } cnt[i].push_back(s); } } if (f(n, c) <= k) { return {}; } vector<int> res(n); for (int i = 0; i < n; i++) { int idx = 0; auto nn = n-i-1; auto cnn = (ull)nn*(nn-1)/2; if (c > cnn) { idx = c - cnn; } for (; idx < n-i; idx++) { if (f(n-i-1, c - idx) > k) { res[i] = idx; break; } k -= f(n-i-1, c - idx); } c -= idx; } return res; } struct tree { tree(int n) { for (size_ = 1; size_ < n; size_ *= 2); v_.resize(2*size_, 1); for (int i = size_ - 1; i > 0; i--) { v_[i] = v_[2*i] + v_[2*i+1]; } } int get(int k) { return _get(1, k); } private: int size_; vector<int> v_; int _get(int v, int k) { v_[v]--; if (v >= size_) { assert(k == 0); return v - size_; } if (v_[2*v] > k) { return _get(2*v, k); } return _get(2*v+1, k - v_[2*v]); } }; int main() { int n; ull k; scanf("%d %llu", &n, &k); tree t(n); auto res = solve(n, k-1); if (res.empty()) { printf("NIE\n"); } else { printf("TAK\n"); for (auto i : res) { printf("%d ", t.get(i) + 1); } printf("\n"); } 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 | #include <bits/stdc++.h> using namespace std; using ull = unsigned long long; const ull inf = 1000000000000000001ll; vector<int> solve(int n, ull k) { if (n == 1) { if (k == 0) { return {0}; } return {}; } auto c = (ull)n * (n-1) / 2; if (c % 2) { return {}; } c /= 2; vector<vector<ull>> cnt(n+1, {1}); auto f = [&cnt](int n_, ull k_) { auto c_ = (ull)n_ * (n_-1) / 2; if (k_ > c_) { return 0ull; } k_ = min(k_, c_ - k_); if (cnt[n_].size() > k_) { return cnt[n_][k_]; } return inf; }; for (int i=2; i <= n; i++) { for (int j=1; j <= (ull)i*(i-1)/2; j++) { ull s = 0; for (int t=0; s < inf && t < i && j-t >= 0; t++) { s += f(i-1, j-t); } s = min(s, inf); if (s == inf) { break; } cnt[i].push_back(s); } } if (f(n, c) <= k) { return {}; } vector<int> res(n); for (int i = 0; i < n; i++) { int idx = 0; auto nn = n-i-1; auto cnn = (ull)nn*(nn-1)/2; if (c > cnn) { idx = c - cnn; } for (; idx < n-i; idx++) { if (f(n-i-1, c - idx) > k) { res[i] = idx; break; } k -= f(n-i-1, c - idx); } c -= idx; } return res; } struct tree { tree(int n) { for (size_ = 1; size_ < n; size_ *= 2); v_.resize(2*size_, 1); for (int i = size_ - 1; i > 0; i--) { v_[i] = v_[2*i] + v_[2*i+1]; } } int get(int k) { return _get(1, k); } private: int size_; vector<int> v_; int _get(int v, int k) { v_[v]--; if (v >= size_) { assert(k == 0); return v - size_; } if (v_[2*v] > k) { return _get(2*v, k); } return _get(2*v+1, k - v_[2*v]); } }; int main() { int n; ull k; scanf("%d %llu", &n, &k); tree t(n); auto res = solve(n, k-1); if (res.empty()) { printf("NIE\n"); } else { printf("TAK\n"); for (auto i : res) { printf("%d ", t.get(i) + 1); } printf("\n"); } return 0; } |