#include <iostream> #include <vector> #include <list> #define pb push_back using namespace std; typedef long long int ll; const ll inf = (int)1e18 + 5; int main() { ios_base::sync_with_stdio(0); ll n, k; cin >> n >> k; if (n % 4 && (n-1) % 4) { cout << "NIE\n"; return 0; } ll inv = n * (n-1) / 4; vector<vector<ll> > a(n); list<int> l; for (int i = 0; i < n; i++) { l.pb(i+1); } a[1].pb(1); a[2].pb(1); a[2].pb(1); a[2].pb(0); for (int i = 3; i < n; i++) { a[i].pb(1); ll i_inv = (ll)i * (i-1) / 2; for (int j = 1; j <= min(inv, i_inv); j++) { if (j < i) { a[i].pb(a[i][j-1] + a[i-1][j]); } else if (j < a[i-1].size()) { a[i].pb(a[i][j-1] + a[i-1][j] - a[i-1][j-i]); } else { a[i].pb(a[i][j-1] - a[i-1][j-i]); } } } vector<int> out; ll before = 0; ll left = inv; for (int lvl = n; lvl > 1; lvl--) { int it_c = 0; for (auto it = l.begin(); it != l.end(); it++) { ll nx = left - it_c; if (a[lvl-1].size() <= nx) { it_c++; continue; } if (before + a[lvl-1][nx] < k) { before += a[lvl-1][nx]; it_c++; continue; } left -= it_c; out.pb(*it); l.erase(it); break; } } if (out.size() == n-1) out.pb(l.front()); if (out.size() == n) { cout << "TAK" << endl; for (int i = 0; i < out.size(); i++) { cout << out[i] << ' '; } cout << endl; } else { cout << "NIE" << 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 | #include <iostream> #include <vector> #include <list> #define pb push_back using namespace std; typedef long long int ll; const ll inf = (int)1e18 + 5; int main() { ios_base::sync_with_stdio(0); ll n, k; cin >> n >> k; if (n % 4 && (n-1) % 4) { cout << "NIE\n"; return 0; } ll inv = n * (n-1) / 4; vector<vector<ll> > a(n); list<int> l; for (int i = 0; i < n; i++) { l.pb(i+1); } a[1].pb(1); a[2].pb(1); a[2].pb(1); a[2].pb(0); for (int i = 3; i < n; i++) { a[i].pb(1); ll i_inv = (ll)i * (i-1) / 2; for (int j = 1; j <= min(inv, i_inv); j++) { if (j < i) { a[i].pb(a[i][j-1] + a[i-1][j]); } else if (j < a[i-1].size()) { a[i].pb(a[i][j-1] + a[i-1][j] - a[i-1][j-i]); } else { a[i].pb(a[i][j-1] - a[i-1][j-i]); } } } vector<int> out; ll before = 0; ll left = inv; for (int lvl = n; lvl > 1; lvl--) { int it_c = 0; for (auto it = l.begin(); it != l.end(); it++) { ll nx = left - it_c; if (a[lvl-1].size() <= nx) { it_c++; continue; } if (before + a[lvl-1][nx] < k) { before += a[lvl-1][nx]; it_c++; continue; } left -= it_c; out.pb(*it); l.erase(it); break; } } if (out.size() == n-1) out.pb(l.front()); if (out.size() == n) { cout << "TAK" << endl; for (int i = 0; i < out.size(); i++) { cout << out[i] << ' '; } cout << endl; } else { cout << "NIE" << endl; } return 0; } |