#include <iostream> #include <cstdio> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <algorithm> #include <cstring> #include <ctime> #include <cassert> using namespace std; #define pb push_back #define mp make_pair #define pii pair<int,int> #define fi first #define se second #define vi vector<int> #define vpii vector<pii> #define SZ(x) ((int)(x.size())) #define DBG cerr << "debug here" << endl; #define DBGV(vari) cerr << #vari<< " = "<< (vari) <<endl; using ull = long long; const ull INF = 2e18; const ull N = 250000; const ull MAXK = 260; ull npo2(ull n) { return n*(n-1)/2; } vector<ull> b[N+10]; //ull b[N+10][MAXK+10]; ull getKnownB(int n, int k) { if (k < 0 or n < 0) { return 0; } if (n <= 1) { return (k == 0 ? 1 : 0); } if (k > npo2(n)) return 0; if (k >= b[n].size()) { if (b[n].back() == INF) return INF; return 0; } return b[n][k]; } void precompute_b() { //b[0][0] = 1; //b[1][0] = 1; b[0].pb(1); b[1].pb(1); int sz = 0; for (int n = 2; n <= N; ++n) { for (int k = 0; k <= min(npo2(n), MAXK); ++k) { ull v = getKnownB(n, k-1) + getKnownB(n-1, k) - getKnownB(n-1, k-n); if (v > INF) { b[n].pb(INF); break; } b[n].pb(v); } sz += b[n].size(); } } struct Node { int left; int sum; int id; Node() { } Node(int left_, int sum_, int id_) : left(left_), sum(sum_), id(id_) {} }; namespace SegmentTree2 { const int M = 1<<18; Node t[2*M]; Node merge(const Node& a, const Node& b) { Node node; node.left = a.sum; node.sum = a.sum + b.sum; return node; } void init(int n) { for (int x = 2*M-1; x >= 1; --x) { if (x >= M) { if (1 <= x-M and x-M <= n) { t[x] = Node(1, 1, x-M); } else { t[x] = Node(0, 0, 0); } } else { t[x] = merge(t[2*x], t[2*x+1]); } } } void remove(int x) { x += M; t[x].left = 0; t[x].sum = 0; while (x > 1) { x /= 2; t[x] = merge(t[2*x], t[2*x+1]); } } int query(int k) { int x = 1; while (x < M) { if (t[x].left >= k) { x = 2*x; } else { k -= t[2*x].sum; x = 2*x+1; } } return t[x].id; } }; int main() { precompute_b(); /* for (int n = 0; n <= 5; ++n) { cout << n << ": "; for (int j = 0; j <= 10; ++j) { cout << b[n][j] << " "; } cout << endl; } */ ios_base::sync_with_stdio(0); ull n, idx; cin >> n >> idx; SegmentTree2::init(n); ull nc2 = npo2(n); if (nc2 % 2 != 0) { cout << "NIE" << endl; return 0; } else { bool found = false; vector<int> res; ull k = nc2/2; while (n >= 0 and k >= 0) { //cerr << "loop " << n << " " << k << " " << idx << endl; if (n == 0 and k == 0) { found = true; break; } ull acc = 0; bool flag = 0; for (ull rnk = max(1LL, n-k); rnk <= min(n, npo2(n-1)+n-k); ++rnk) { ull nextK = k-n+rnk; ull nc2 = npo2(n-1); ull tmpK = nextK; if (tmpK > nc2/2) { tmpK = nc2-tmpK; } ull tmp = getKnownB(n-1, min(tmpK, MAXK)); //cout << rnk << " " << acc << " " << tmp << " " << acc+tmp << endl; if (acc + tmp >= idx) { //cout << "good" << endl; int val = SegmentTree2::query(rnk); //cout << "! " << rnk << " " << val << endl; res.pb(val); SegmentTree2::remove(val); n--; k = nextK; idx = idx-acc; //gen(n-1, nextK, idx-acc); flag = 1; break; } acc += tmp; } if (flag) continue; else break; } if (!found) { cout << "NIE" << endl; return 0; } else { cout << "TAK" << endl; for (auto e : res) { cout << e << " "; } 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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | #include <iostream> #include <cstdio> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <algorithm> #include <cstring> #include <ctime> #include <cassert> using namespace std; #define pb push_back #define mp make_pair #define pii pair<int,int> #define fi first #define se second #define vi vector<int> #define vpii vector<pii> #define SZ(x) ((int)(x.size())) #define DBG cerr << "debug here" << endl; #define DBGV(vari) cerr << #vari<< " = "<< (vari) <<endl; using ull = long long; const ull INF = 2e18; const ull N = 250000; const ull MAXK = 260; ull npo2(ull n) { return n*(n-1)/2; } vector<ull> b[N+10]; //ull b[N+10][MAXK+10]; ull getKnownB(int n, int k) { if (k < 0 or n < 0) { return 0; } if (n <= 1) { return (k == 0 ? 1 : 0); } if (k > npo2(n)) return 0; if (k >= b[n].size()) { if (b[n].back() == INF) return INF; return 0; } return b[n][k]; } void precompute_b() { //b[0][0] = 1; //b[1][0] = 1; b[0].pb(1); b[1].pb(1); int sz = 0; for (int n = 2; n <= N; ++n) { for (int k = 0; k <= min(npo2(n), MAXK); ++k) { ull v = getKnownB(n, k-1) + getKnownB(n-1, k) - getKnownB(n-1, k-n); if (v > INF) { b[n].pb(INF); break; } b[n].pb(v); } sz += b[n].size(); } } struct Node { int left; int sum; int id; Node() { } Node(int left_, int sum_, int id_) : left(left_), sum(sum_), id(id_) {} }; namespace SegmentTree2 { const int M = 1<<18; Node t[2*M]; Node merge(const Node& a, const Node& b) { Node node; node.left = a.sum; node.sum = a.sum + b.sum; return node; } void init(int n) { for (int x = 2*M-1; x >= 1; --x) { if (x >= M) { if (1 <= x-M and x-M <= n) { t[x] = Node(1, 1, x-M); } else { t[x] = Node(0, 0, 0); } } else { t[x] = merge(t[2*x], t[2*x+1]); } } } void remove(int x) { x += M; t[x].left = 0; t[x].sum = 0; while (x > 1) { x /= 2; t[x] = merge(t[2*x], t[2*x+1]); } } int query(int k) { int x = 1; while (x < M) { if (t[x].left >= k) { x = 2*x; } else { k -= t[2*x].sum; x = 2*x+1; } } return t[x].id; } }; int main() { precompute_b(); /* for (int n = 0; n <= 5; ++n) { cout << n << ": "; for (int j = 0; j <= 10; ++j) { cout << b[n][j] << " "; } cout << endl; } */ ios_base::sync_with_stdio(0); ull n, idx; cin >> n >> idx; SegmentTree2::init(n); ull nc2 = npo2(n); if (nc2 % 2 != 0) { cout << "NIE" << endl; return 0; } else { bool found = false; vector<int> res; ull k = nc2/2; while (n >= 0 and k >= 0) { //cerr << "loop " << n << " " << k << " " << idx << endl; if (n == 0 and k == 0) { found = true; break; } ull acc = 0; bool flag = 0; for (ull rnk = max(1LL, n-k); rnk <= min(n, npo2(n-1)+n-k); ++rnk) { ull nextK = k-n+rnk; ull nc2 = npo2(n-1); ull tmpK = nextK; if (tmpK > nc2/2) { tmpK = nc2-tmpK; } ull tmp = getKnownB(n-1, min(tmpK, MAXK)); //cout << rnk << " " << acc << " " << tmp << " " << acc+tmp << endl; if (acc + tmp >= idx) { //cout << "good" << endl; int val = SegmentTree2::query(rnk); //cout << "! " << rnk << " " << val << endl; res.pb(val); SegmentTree2::remove(val); n--; k = nextK; idx = idx-acc; //gen(n-1, nextK, idx-acc); flag = 1; break; } acc += tmp; } if (flag) continue; else break; } if (!found) { cout << "NIE" << endl; return 0; } else { cout << "TAK" << endl; for (auto e : res) { cout << e << " "; } cout << endl; } } return 0; } |