#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef tree< int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update > stat_tree; struct MahonianContainer { vector< vector<int64_t> > data; vector<int64_t> sizes; int n; int64_t const BIG; MahonianContainer(int _n, int64_t _k): n(_n), BIG(_k + 1) { data.push_back(vector<int64_t>()); sizes.push_back(0); data.push_back(vector<int64_t>(1, 1)); sizes.push_back(1); for(int i = 2; i <= n; i++) { int64_t sz = i * int64_t(i - 1) / 2 + 1; data.push_back(vector<int64_t>()); sizes.push_back(sz); int psz = data[i-1].size(); int64_t v = 0; for(int64_t j = 0; j < sz; j++) { v += (j < psz ? data[i - 1][j] : 0); v -= (j - i >= 0 ? (j - i < psz ? data[i - 1][j - i] : 0) : 0); data[i].push_back(v); if(data[i].back() > BIG) { data[i].back() = BIG; break; } } } } int64_t operator()(int i, int64_t j) { j = min<int64_t>(j, sizes[i] - 1 - j); if(j < 0) return 0; if(j >= int(data[i].size())) return BIG; return data[i][j]; } void gen_perm_code(int64_t k, int64_t m, vector<int> &code) { for(int i = n; i > 1; i--) { int p = 1; if(m - p + 1 >= sizes[i - 1]) { p = m - sizes[i - 1] + 2; } for(int64_t z; k > (z = (*this)(i - 1, m - p + 1));) { k -= z; p++; } code.push_back(p - 1); m -= p - 1; } code.push_back(0); } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; int64_t k; cin >> n >> k; if((n & 2) != 0) { cout << "NIE\n"; return 0; } MahonianContainer mc(n, k + 1); int64_t m = (n * int64_t(n - 1)) >> 2; if(mc(n, m) < k) { cout << "NIE\n"; return 0; } cout << "TAK\n"; vector<int> code; mc.gen_perm_code(k, m, code); int i0 = 1, i1 = n; int ci = 0; while(ci < int(code.size()) and code[ci] == 0) { cout << i0 << ' '; i0++; ci++; } stat_tree available; for(int i = i0; i <= i1; i++) { available.insert(i); } for(; ci < int(code.size()); ci++) { stat_tree::iterator it = available.find_by_order(code[ci]); int v = *it; cout << v << ' '; available.erase(it); } cout << '\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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef tree< int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update > stat_tree; struct MahonianContainer { vector< vector<int64_t> > data; vector<int64_t> sizes; int n; int64_t const BIG; MahonianContainer(int _n, int64_t _k): n(_n), BIG(_k + 1) { data.push_back(vector<int64_t>()); sizes.push_back(0); data.push_back(vector<int64_t>(1, 1)); sizes.push_back(1); for(int i = 2; i <= n; i++) { int64_t sz = i * int64_t(i - 1) / 2 + 1; data.push_back(vector<int64_t>()); sizes.push_back(sz); int psz = data[i-1].size(); int64_t v = 0; for(int64_t j = 0; j < sz; j++) { v += (j < psz ? data[i - 1][j] : 0); v -= (j - i >= 0 ? (j - i < psz ? data[i - 1][j - i] : 0) : 0); data[i].push_back(v); if(data[i].back() > BIG) { data[i].back() = BIG; break; } } } } int64_t operator()(int i, int64_t j) { j = min<int64_t>(j, sizes[i] - 1 - j); if(j < 0) return 0; if(j >= int(data[i].size())) return BIG; return data[i][j]; } void gen_perm_code(int64_t k, int64_t m, vector<int> &code) { for(int i = n; i > 1; i--) { int p = 1; if(m - p + 1 >= sizes[i - 1]) { p = m - sizes[i - 1] + 2; } for(int64_t z; k > (z = (*this)(i - 1, m - p + 1));) { k -= z; p++; } code.push_back(p - 1); m -= p - 1; } code.push_back(0); } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; int64_t k; cin >> n >> k; if((n & 2) != 0) { cout << "NIE\n"; return 0; } MahonianContainer mc(n, k + 1); int64_t m = (n * int64_t(n - 1)) >> 2; if(mc(n, m) < k) { cout << "NIE\n"; return 0; } cout << "TAK\n"; vector<int> code; mc.gen_perm_code(k, m, code); int i0 = 1, i1 = n; int ci = 0; while(ci < int(code.size()) and code[ci] == 0) { cout << i0 << ' '; i0++; ci++; } stat_tree available; for(int i = i0; i <= i1; i++) { available.insert(i); } for(; ci < int(code.size()); ci++) { stat_tree::iterator it = available.find_by_order(code[ci]); int v = *it; cout << v << ' '; available.erase(it); } cout << '\n'; return 0; } |