#include <iostream> #include <vector> using namespace std; namespace { template<typename T> constexpr T square(T x) { return x*x; } template <typename T> constexpr T ipow(T base, int power) { return power == 0 ? 1 : (square(ipow(base, power / 2)) * (power % 2 == 0 ? 1 : base)); } constexpr long long choose2(int n) { return static_cast<long long>(n) * (n - 1) / 2; } struct Counter { long long inf; vector<vector<long long>> data; Counter(int n, long long inf_): inf{inf_} { data.reserve(n + 1); data.push_back({1}); for (int i = 1; i <= n; ++i) { data.emplace_back(); auto m = choose2(i); long long tmp = 0; for (int j = 0; j <= m; ++j) { tmp -= operator()(i - 1, j - i); tmp += operator()(i - 1, j); if (tmp >= inf) break; data.back().emplace_back(tmp); } } } long long operator()(int n, long long k) { auto m = choose2(n); if (k < 0 || k > m) return 0; if (k > m - k) k = m - k; if (k >= static_cast<long long>(data[n].size())) return inf; return data[n][k]; } }; struct Finder { int n; vector<int> count; static int round_up(int n) { int p = 1; while (p < n) p *= 2; return p; } Finder(int n_): n{round_up(n_ + 1)}, count(2 * n) { for (int i = 1; i <= n_; ++i) { count[n + i] = 1; } for (int i = n - 1; i > 0; --i) { count[i] = count[2 * i] + count[2 * i + 1]; } } int find_and_erase(int p) { int i = 1; while (i < n) { if (p < count[2 * i]) { i *= 2; } else { p -= count[2 * i]; i *= 2; ++i; } } int res = i - n; count[i] = 0; while (i > 1) { i /= 2; count[i] = count[2 * i] + count[2 * i + 1]; } return res; } }; vector<int> solve(int n, long long k) { long long m = static_cast<long long>(n) * (n - 1) / 2; if (m % 2 == 1) return {}; m /= 2; Counter count{n, k}; if (k > count(n, m)) return {}; vector<int> inv; for (int i = n - 1; i >= 0; --i) { int j0 = 0; if (m > choose2(i)) { j0 = m - choose2(i); m -= j0; } for (int j = j0; j <= i; ++j) { auto cnt = count(i, m); if (k <= cnt) { inv.emplace_back(j); break; } k -= cnt; --m; } } Finder left(n); vector<int> p; p.reserve(n); for (int x: inv) { p.emplace_back(left.find_and_erase(x)); } return p; } } int main() { iostream::sync_with_stdio(false); cin.tie(nullptr); int n; long long k; cin >> n >> k; auto res = solve(n, k); if (res.empty()) { cout << "NIE" << endl; } else { cout << "TAK\n"; bool first = true; for (auto x: res) { if (!first) cout << ' '; first = false; cout << x; } } cout << endl; 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 | #include <iostream> #include <vector> using namespace std; namespace { template<typename T> constexpr T square(T x) { return x*x; } template <typename T> constexpr T ipow(T base, int power) { return power == 0 ? 1 : (square(ipow(base, power / 2)) * (power % 2 == 0 ? 1 : base)); } constexpr long long choose2(int n) { return static_cast<long long>(n) * (n - 1) / 2; } struct Counter { long long inf; vector<vector<long long>> data; Counter(int n, long long inf_): inf{inf_} { data.reserve(n + 1); data.push_back({1}); for (int i = 1; i <= n; ++i) { data.emplace_back(); auto m = choose2(i); long long tmp = 0; for (int j = 0; j <= m; ++j) { tmp -= operator()(i - 1, j - i); tmp += operator()(i - 1, j); if (tmp >= inf) break; data.back().emplace_back(tmp); } } } long long operator()(int n, long long k) { auto m = choose2(n); if (k < 0 || k > m) return 0; if (k > m - k) k = m - k; if (k >= static_cast<long long>(data[n].size())) return inf; return data[n][k]; } }; struct Finder { int n; vector<int> count; static int round_up(int n) { int p = 1; while (p < n) p *= 2; return p; } Finder(int n_): n{round_up(n_ + 1)}, count(2 * n) { for (int i = 1; i <= n_; ++i) { count[n + i] = 1; } for (int i = n - 1; i > 0; --i) { count[i] = count[2 * i] + count[2 * i + 1]; } } int find_and_erase(int p) { int i = 1; while (i < n) { if (p < count[2 * i]) { i *= 2; } else { p -= count[2 * i]; i *= 2; ++i; } } int res = i - n; count[i] = 0; while (i > 1) { i /= 2; count[i] = count[2 * i] + count[2 * i + 1]; } return res; } }; vector<int> solve(int n, long long k) { long long m = static_cast<long long>(n) * (n - 1) / 2; if (m % 2 == 1) return {}; m /= 2; Counter count{n, k}; if (k > count(n, m)) return {}; vector<int> inv; for (int i = n - 1; i >= 0; --i) { int j0 = 0; if (m > choose2(i)) { j0 = m - choose2(i); m -= j0; } for (int j = j0; j <= i; ++j) { auto cnt = count(i, m); if (k <= cnt) { inv.emplace_back(j); break; } k -= cnt; --m; } } Finder left(n); vector<int> p; p.reserve(n); for (int x: inv) { p.emplace_back(left.find_and_erase(x)); } return p; } } int main() { iostream::sync_with_stdio(false); cin.tie(nullptr); int n; long long k; cin >> n >> k; auto res = solve(n, k); if (res.empty()) { cout << "NIE" << endl; } else { cout << "TAK\n"; bool first = true; for (auto x: res) { if (!first) cout << ' '; first = false; cout << x; } } cout << endl; return 0; } |