#include <bits/stdc++.h> #define REP(i, n) for(int i=0; i<(n); i++) using namespace std; typedef long long ll; const int MN = 250000; const ll MAX = 1000LL*1000LL*1000LL*1000LL*1000LL*1000LL; const ll INF = MAX + 1; vector<ll> t[MN + 1]; inline ll T1(ll n, ll k) { if(k < 0) return 0; if(k >= (ll) t[n].size()) { if(t[n].back() > MAX) return INF; else return 0; } return t[n][k]; } ll T(ll n, ll k) { return min(T1(n, k), T1(n, n*(n - 1)/2 - k)); } void gen() { t[1].push_back(1); for(ll n = 2; n <= MN; n++) { t[n].push_back(1); bool cont = true; for(ll k = 1; k <= n*(n - 1)/2 && cont; k++) { t[n].push_back( t[n].back() + T(n - 1, k) - T(n - 1, k - n) ); if(t[n].back() > MAX) { t[n].back() = INF; cont = false; } } } } struct BinTree { int base = (1<<16); vector<int> v; BinTree() { v.resize(2*base, 1); for(int x = base-1; x > 0; x--) v[x] = v[2*x] + v[2*x + 1]; } void remove(int pos) { pos += base - 1; for(; pos > 0; pos /= 2) v[pos]--; } int get(int k) { int w = 1; while(w < base) { if(v[2*w] >= k) w = 2*w; else { k -= v[2*w]; w = 2*w + 1; } } return w - base + 1; } }; void wypisz(vector<int>& v) { BinTree tr; for(int x: v) { x = tr.get(x); tr.remove(x); cout << x << ' '; } cout << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); gen(); /* for(int n = 1; n <= 32; n++) { cout << "n = " << n << ": "; for(ll x: t[n]) cout << x << ' '; cout << endl; } */ ll n, k; cin >> n >> k; if(n % 4 != 0) { cout << "NIE\n"; return 0; } ll l_inwersji = n * (n - 1) / 4; vector<int> res(n); for(ll i = 0; i < n; i++) { ll len = n - i; if(len == 1) { if(k > 1) { cout << "NIE\n"; return 0; } res[i] = 1; break; } ll pli = (len - 1)*(len - 2)/2; ll nowa_l_inw = min(l_inwersji, pli); ll l = l_inwersji - nowa_l_inw + 1; bool cont = true; while(l <= len && cont) { ll nowa_l_inw = l_inwersji - l + 1; if(T(len - 1, nowa_l_inw) >= k) { res[i] = l; l_inwersji = nowa_l_inw; cont = false; } else k -= T(len - 1, nowa_l_inw); l++; } if(cont) { cout << "NIE\n"; return 0; } } cout << "TAK\n"; wypisz(res); }
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 | #include <bits/stdc++.h> #define REP(i, n) for(int i=0; i<(n); i++) using namespace std; typedef long long ll; const int MN = 250000; const ll MAX = 1000LL*1000LL*1000LL*1000LL*1000LL*1000LL; const ll INF = MAX + 1; vector<ll> t[MN + 1]; inline ll T1(ll n, ll k) { if(k < 0) return 0; if(k >= (ll) t[n].size()) { if(t[n].back() > MAX) return INF; else return 0; } return t[n][k]; } ll T(ll n, ll k) { return min(T1(n, k), T1(n, n*(n - 1)/2 - k)); } void gen() { t[1].push_back(1); for(ll n = 2; n <= MN; n++) { t[n].push_back(1); bool cont = true; for(ll k = 1; k <= n*(n - 1)/2 && cont; k++) { t[n].push_back( t[n].back() + T(n - 1, k) - T(n - 1, k - n) ); if(t[n].back() > MAX) { t[n].back() = INF; cont = false; } } } } struct BinTree { int base = (1<<16); vector<int> v; BinTree() { v.resize(2*base, 1); for(int x = base-1; x > 0; x--) v[x] = v[2*x] + v[2*x + 1]; } void remove(int pos) { pos += base - 1; for(; pos > 0; pos /= 2) v[pos]--; } int get(int k) { int w = 1; while(w < base) { if(v[2*w] >= k) w = 2*w; else { k -= v[2*w]; w = 2*w + 1; } } return w - base + 1; } }; void wypisz(vector<int>& v) { BinTree tr; for(int x: v) { x = tr.get(x); tr.remove(x); cout << x << ' '; } cout << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); gen(); /* for(int n = 1; n <= 32; n++) { cout << "n = " << n << ": "; for(ll x: t[n]) cout << x << ' '; cout << endl; } */ ll n, k; cin >> n >> k; if(n % 4 != 0) { cout << "NIE\n"; return 0; } ll l_inwersji = n * (n - 1) / 4; vector<int> res(n); for(ll i = 0; i < n; i++) { ll len = n - i; if(len == 1) { if(k > 1) { cout << "NIE\n"; return 0; } res[i] = 1; break; } ll pli = (len - 1)*(len - 2)/2; ll nowa_l_inw = min(l_inwersji, pli); ll l = l_inwersji - nowa_l_inw + 1; bool cont = true; while(l <= len && cont) { ll nowa_l_inw = l_inwersji - l + 1; if(T(len - 1, nowa_l_inw) >= k) { res[i] = l; l_inwersji = nowa_l_inw; cont = false; } else k -= T(len - 1, nowa_l_inw); l++; } if(cont) { cout << "NIE\n"; return 0; } } cout << "TAK\n"; wypisz(res); } |