#include <bits/stdc++.h> using namespace std; #define INF 1010000000 #define EPS 1E-6 #define MP make_pair #define ST first #define ND second #define REP(i, n) for(int i = 0; i < (n); ++i) #define REPD(i, n) for(int i = (n) - 1; i >= 0; --i) #define FOR(i, a, n) for(int i = (a); i <= (n); ++i) #define FORD(i, a, n) for(int i = (a); i >= (n); --i) #define DD(x, args...) { vector<string> _v = _split(#args, ','); _err(x, _v.begin(), args); } #define D(args...) DD(", ", args) #define DE(args...) DD("\n", args) #define D2(a, args...) { cerr << a << ": "; D(args); } #define DD2(x, a, args...) { cerr << a << ": "; DD(x, args); } #define E cerr << endl; #define OUT(...) ostream &operator<<(ostream &ost, const __VA_ARGS__ &_cnt) { return _out(ost, ALL(_cnt)); } #define SZ(x) ((int)(x).size()) #define PB push_back #define EB emplace_back #define ALL(x) x.begin(), x.end() #define endl '\n' typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef pair<double, double> PDD; template<class c1> ostream &_out(ostream &ost, c1 a, c1 b); template<class T1, class T2> ostream &operator<<(ostream &ost, const pair<T1, T2> &_cnt); template<class T1> OUT(vector<T1>); template<class T1> OUT(deque<T1>); template<class T1> OUT(list<T1>); template<class T1, class T2> OUT(set<T1, T2>); template<class T1, class T2> OUT(multiset<T1, T2>); template<class T1, class T2, class T3> OUT(map<T1, T2, T3>); template<class T1, class T2, class T3> OUT(multimap<T1, T2, T3>); template<class T1, size_t T2> OUT(array<T1, T2>); template<class T1, class T2> ostream &operator<<(ostream &ost, const pair<T1, T2> &_cnt) { return ost << '(' << _cnt.ST << ", " << _cnt.ND << ')'; } template<class T1> ostream &_out(ostream &ost, T1 a, T1 b) { ost << '{'; if(a != b) { ost << *a; while(++a != b) ost << ", " << *a; } return ost << '}'; } vector<string> _split(const string &s, char c) { int br = 0; vector<string> v(1); REP(i, SZ(s)) { if(s[i] == '[' || s[i] == '(' || s[i] == '{'/* || s[i] == '<'*/) br++; else if(s[i] == ']' || s[i] == ')' || s[i] == '}'/* || s[i] == '>'*/) br--; if(s[i] == c && br == 0) v.PB(""); else v.back().PB(s[i]); } return v; } template<class T1> void _err(string del, vector<string>::iterator it, T1 a) { bool wb = (*it)[0] == ' ', we = (*it).back() == ' '; cerr << it -> substr(wb, SZ(*it) - wb - we) << " = " << a << endl; (void)del; } template<class T1, class... Args> void _err(string del, vector<string>::iterator it, T1 a, Args... args) { bool wb = (*it)[0] == ' ', we = (*it).back() == ' '; cerr << it -> substr(wb, SZ(*it) - wb - we) << " = " << a << del; _err(del, ++it, args...); } ///////////////////////////////////////////////////////////////////// #define DUZO 1000000000000000010 vector<vector<LL>> dp; vector<vector<LL>> line; void count(int n, int k, vector<vector<LL>> &l) { l.resize(n + 1, vector<LL>(k + 1)); REP(i, n + 1) { REP(j, k + 1) { if(i == 0) l[i][j] = 0; else if(j == 0) l[i][j] = 1; else { for(int ii = 0; ii < i && j - ii >= 0; ii++) { l[i][j] += l[i - 1][j - ii]; l[i][j] = min(l[i][j], (LL)DUZO); } } } } } void prep(int n) { count(300, 300, dp); count(n, 10, line); } inline LL ile(int n, LL k) { if(n < 300 && k < 300) return dp[n][k]; else if(n >= 300 && k < 10) return line[n][k]; return DUZO; // wydaje sie ze tyle starczy } int N = 1 << 18; vector<int> T(1 << 19); void init(int n) { FOR(i, N + 1, N + n) T[i] = 1; FORD(i, N - 1, 1) T[i] = T[i << 1] + T[(i << 1) + 1]; } void change(int p, int c) { p += N; T[p] = c; while(p != 1) { p >>= 1; T[p] = T[p << 1] + T[(p << 1) + 1]; } } int findKth(int ile, int a = 0, int b = N - 1, int id = 1) { if(a == b) return a; if(T[(id << 1) + 1] >= ile) return findKth(ile, (a + b + 1) >> 1, b, (id << 1) + 1); return findKth(ile - T[(id << 1) + 1], a, (a + b) >> 1, id << 1); } int main(int argc, char** argv) { (void) argc; (void) argv; ios::sync_with_stdio(0); cin.tie(0); int n; LL k; cin >> n >> k; if((LL)n*(n-1) % 4 != 0) { cout << "NIE" << endl; return 0; } init(n); prep(n); set<int> l; REP(i, n) l.insert(i + 1); LL up = (LL)n * (n - 1) / 4; int cur_n = n; vector<int> res; REP(i, n) { cur_n--; if(cur_n == 0) { if(k == 1) { res.EB(*l.begin()); break; } else { cout << "NIE" << endl; return 0; } } LL bigger, sum = 0; set<int>::iterator it; if(T[1] <= up + 1) { it = l.begin(); bigger = SZ(l); } else { int val = findKth(up + 1); it = l.find(val); bigger = up + 1; } for(; it != l.end(); it++) { int cur = *it; bigger--; if(up < bigger) // teoretycznie nie powinno zajsc continue; LL cur_up = up - bigger; LL tm = ile(cur_n, cur_up); if(tm + sum >= k) { res.EB(cur); l.erase(it); change(cur, 0); k -= sum; up = cur_up; goto nex; } else sum += tm; } cout << "NIE" << endl; return 0; nex:; } cout << "TAK" << endl; REP(i, n) cout << res[i] << ' '; cout << endl; return 0; } /* 21 1000000000000000000 20 62119523114983224 20 62119523114983225 20 1000000000000000000 250000 1000000000000000000 */
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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | #include <bits/stdc++.h> using namespace std; #define INF 1010000000 #define EPS 1E-6 #define MP make_pair #define ST first #define ND second #define REP(i, n) for(int i = 0; i < (n); ++i) #define REPD(i, n) for(int i = (n) - 1; i >= 0; --i) #define FOR(i, a, n) for(int i = (a); i <= (n); ++i) #define FORD(i, a, n) for(int i = (a); i >= (n); --i) #define DD(x, args...) { vector<string> _v = _split(#args, ','); _err(x, _v.begin(), args); } #define D(args...) DD(", ", args) #define DE(args...) DD("\n", args) #define D2(a, args...) { cerr << a << ": "; D(args); } #define DD2(x, a, args...) { cerr << a << ": "; DD(x, args); } #define E cerr << endl; #define OUT(...) ostream &operator<<(ostream &ost, const __VA_ARGS__ &_cnt) { return _out(ost, ALL(_cnt)); } #define SZ(x) ((int)(x).size()) #define PB push_back #define EB emplace_back #define ALL(x) x.begin(), x.end() #define endl '\n' typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef pair<double, double> PDD; template<class c1> ostream &_out(ostream &ost, c1 a, c1 b); template<class T1, class T2> ostream &operator<<(ostream &ost, const pair<T1, T2> &_cnt); template<class T1> OUT(vector<T1>); template<class T1> OUT(deque<T1>); template<class T1> OUT(list<T1>); template<class T1, class T2> OUT(set<T1, T2>); template<class T1, class T2> OUT(multiset<T1, T2>); template<class T1, class T2, class T3> OUT(map<T1, T2, T3>); template<class T1, class T2, class T3> OUT(multimap<T1, T2, T3>); template<class T1, size_t T2> OUT(array<T1, T2>); template<class T1, class T2> ostream &operator<<(ostream &ost, const pair<T1, T2> &_cnt) { return ost << '(' << _cnt.ST << ", " << _cnt.ND << ')'; } template<class T1> ostream &_out(ostream &ost, T1 a, T1 b) { ost << '{'; if(a != b) { ost << *a; while(++a != b) ost << ", " << *a; } return ost << '}'; } vector<string> _split(const string &s, char c) { int br = 0; vector<string> v(1); REP(i, SZ(s)) { if(s[i] == '[' || s[i] == '(' || s[i] == '{'/* || s[i] == '<'*/) br++; else if(s[i] == ']' || s[i] == ')' || s[i] == '}'/* || s[i] == '>'*/) br--; if(s[i] == c && br == 0) v.PB(""); else v.back().PB(s[i]); } return v; } template<class T1> void _err(string del, vector<string>::iterator it, T1 a) { bool wb = (*it)[0] == ' ', we = (*it).back() == ' '; cerr << it -> substr(wb, SZ(*it) - wb - we) << " = " << a << endl; (void)del; } template<class T1, class... Args> void _err(string del, vector<string>::iterator it, T1 a, Args... args) { bool wb = (*it)[0] == ' ', we = (*it).back() == ' '; cerr << it -> substr(wb, SZ(*it) - wb - we) << " = " << a << del; _err(del, ++it, args...); } ///////////////////////////////////////////////////////////////////// #define DUZO 1000000000000000010 vector<vector<LL>> dp; vector<vector<LL>> line; void count(int n, int k, vector<vector<LL>> &l) { l.resize(n + 1, vector<LL>(k + 1)); REP(i, n + 1) { REP(j, k + 1) { if(i == 0) l[i][j] = 0; else if(j == 0) l[i][j] = 1; else { for(int ii = 0; ii < i && j - ii >= 0; ii++) { l[i][j] += l[i - 1][j - ii]; l[i][j] = min(l[i][j], (LL)DUZO); } } } } } void prep(int n) { count(300, 300, dp); count(n, 10, line); } inline LL ile(int n, LL k) { if(n < 300 && k < 300) return dp[n][k]; else if(n >= 300 && k < 10) return line[n][k]; return DUZO; // wydaje sie ze tyle starczy } int N = 1 << 18; vector<int> T(1 << 19); void init(int n) { FOR(i, N + 1, N + n) T[i] = 1; FORD(i, N - 1, 1) T[i] = T[i << 1] + T[(i << 1) + 1]; } void change(int p, int c) { p += N; T[p] = c; while(p != 1) { p >>= 1; T[p] = T[p << 1] + T[(p << 1) + 1]; } } int findKth(int ile, int a = 0, int b = N - 1, int id = 1) { if(a == b) return a; if(T[(id << 1) + 1] >= ile) return findKth(ile, (a + b + 1) >> 1, b, (id << 1) + 1); return findKth(ile - T[(id << 1) + 1], a, (a + b) >> 1, id << 1); } int main(int argc, char** argv) { (void) argc; (void) argv; ios::sync_with_stdio(0); cin.tie(0); int n; LL k; cin >> n >> k; if((LL)n*(n-1) % 4 != 0) { cout << "NIE" << endl; return 0; } init(n); prep(n); set<int> l; REP(i, n) l.insert(i + 1); LL up = (LL)n * (n - 1) / 4; int cur_n = n; vector<int> res; REP(i, n) { cur_n--; if(cur_n == 0) { if(k == 1) { res.EB(*l.begin()); break; } else { cout << "NIE" << endl; return 0; } } LL bigger, sum = 0; set<int>::iterator it; if(T[1] <= up + 1) { it = l.begin(); bigger = SZ(l); } else { int val = findKth(up + 1); it = l.find(val); bigger = up + 1; } for(; it != l.end(); it++) { int cur = *it; bigger--; if(up < bigger) // teoretycznie nie powinno zajsc continue; LL cur_up = up - bigger; LL tm = ile(cur_n, cur_up); if(tm + sum >= k) { res.EB(cur); l.erase(it); change(cur, 0); k -= sum; up = cur_up; goto nex; } else sum += tm; } cout << "NIE" << endl; return 0; nex:; } cout << "TAK" << endl; REP(i, n) cout << res[i] << ' '; cout << endl; return 0; } /* 21 1000000000000000000 20 62119523114983224 20 62119523114983225 20 1000000000000000000 250000 1000000000000000000 */ |