#include <bits/stdc++.h> using namespace std; #define mp make_pair #define pb push_back #define eb emplace_back #define e1 first #define e2 second #define OUT(x) {cout << x; exit(0); } #define scanf(...) scanf(__VA_ARGS__)?:0 #define boost ios_base::sync_with_stdio(0); cin.tie(0) #define FOR(i, a, b) for(int i=(a); i<=(b); ++i) typedef long long int ll; typedef unsigned long long ull; typedef unsigned int ui; typedef long double ld; typedef pair <int, int> PII; typedef pair <int, PII> PIP; typedef pair <PII, int> PPI; typedef pair <ll, ll> PLL; typedef pair <PII, PII> PP; const int mod = 1e9+7; const int inf = 1e9+9; const int MOD = 1e9+696969; const ll INF = (ll)1e18 + 3; const int maxn = 250100; int ktore[maxn], tab[maxn]; ll n, k; const int R = (1 << 18); struct segmentTree { ll dr[2 * R + 5]; segmentTree() { fill(dr, dr+2*R+3, 0); } inline void insert(int x, ll val) { assert(val >= 0); x += R; while (x) { dr[x] += val; dr[x] = min(dr[x], INF); x >>= 1; } } inline void czysc(int x) { x += R; dr[x] = 0; x >>= 1; while (x) { dr[x] = dr[x << 1 | 0] + dr[x << 1 | 1]; dr[x] = min(dr[x], INF); x >>= 1; } } inline ll query(int x, int y) { x += R; y += R; if (x > y) return 0; if (x == y) return dr[x]; ll ret = dr[x] + dr[y]; ret = min(ret, INF); while (x + 1 < y) { if (!(x & 1)) ret += dr[x + 1]; if (y & 1) ret += dr[y-1]; ret = min(ret, INF); x >>= 1; y >>= 1; } return ret; } int search(int k) { assert(dr[1] >= k); int x = 1; while (x < R) { if (dr[2 * x] >= k) x *= 2; else k -= dr[2 * x], x = 2 * x + 1; } return x - R; } }; segmentTree depek; vector <ll> dp[maxn]; void countDP(int range) { dp[0].pb(1); depek.insert(0, 1); for (ll i=1; i<=range; ++i) { for (ll j = 0; j <= i * (i - 1)/2; ++j) { int X = 0, Y = (int)dp[i-1].size() - 1; X = max(X, (int)(j - i + 1)); Y = min(Y, (int)j); ll value = depek.query(X, Y); dp[i].pb(value); if (value >= INF) break; } for (int j=0; j<(int)dp[i-1].size(); ++j) depek.czysc(j); for (int j=0; j<(int)dp[i].size(); ++j) depek.insert(j, dp[i][j]); } } inline ll getDP(ll n, ll k) { ll inve = n * (n - 1) / 2; k = min(k, inve - k); //cout << "Getting: " << n << ' ' << k << endl; if (k < 0) return 0; if (k >= (int)dp[n].size()) return INF; return dp[n][k]; } int main() { boost; cin >> n >> k; ll m = n * (n - 1) / 2; if (m % 2 == 1) OUT("NIE"); m /= 2; segmentTree elementy = segmentTree(); FOR(i, 1, n) elementy.insert(i, 1); countDP(250000); if (getDP(n, m) < k) OUT("NIE"); FOR(i, 1, n) { int skip = 0; int pocz = 0, kon = min(m, n - i); while (pocz < kon) { int sr = (pocz + kon)/2; if (getDP(n - i, m - sr) == 0) pocz = ++sr; else kon = sr; } skip = pocz; while (getDP(n - i, m - skip) < k) { k -= getDP(n - i, m - skip); ++skip; } ktore[i] = elementy.search(1 + skip); elementy.czysc(ktore[i]); m -= skip; } cout << "TAK\n"; FOR(i, 1, n) cout << ktore[i] << ' '; }
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 | #include <bits/stdc++.h> using namespace std; #define mp make_pair #define pb push_back #define eb emplace_back #define e1 first #define e2 second #define OUT(x) {cout << x; exit(0); } #define scanf(...) scanf(__VA_ARGS__)?:0 #define boost ios_base::sync_with_stdio(0); cin.tie(0) #define FOR(i, a, b) for(int i=(a); i<=(b); ++i) typedef long long int ll; typedef unsigned long long ull; typedef unsigned int ui; typedef long double ld; typedef pair <int, int> PII; typedef pair <int, PII> PIP; typedef pair <PII, int> PPI; typedef pair <ll, ll> PLL; typedef pair <PII, PII> PP; const int mod = 1e9+7; const int inf = 1e9+9; const int MOD = 1e9+696969; const ll INF = (ll)1e18 + 3; const int maxn = 250100; int ktore[maxn], tab[maxn]; ll n, k; const int R = (1 << 18); struct segmentTree { ll dr[2 * R + 5]; segmentTree() { fill(dr, dr+2*R+3, 0); } inline void insert(int x, ll val) { assert(val >= 0); x += R; while (x) { dr[x] += val; dr[x] = min(dr[x], INF); x >>= 1; } } inline void czysc(int x) { x += R; dr[x] = 0; x >>= 1; while (x) { dr[x] = dr[x << 1 | 0] + dr[x << 1 | 1]; dr[x] = min(dr[x], INF); x >>= 1; } } inline ll query(int x, int y) { x += R; y += R; if (x > y) return 0; if (x == y) return dr[x]; ll ret = dr[x] + dr[y]; ret = min(ret, INF); while (x + 1 < y) { if (!(x & 1)) ret += dr[x + 1]; if (y & 1) ret += dr[y-1]; ret = min(ret, INF); x >>= 1; y >>= 1; } return ret; } int search(int k) { assert(dr[1] >= k); int x = 1; while (x < R) { if (dr[2 * x] >= k) x *= 2; else k -= dr[2 * x], x = 2 * x + 1; } return x - R; } }; segmentTree depek; vector <ll> dp[maxn]; void countDP(int range) { dp[0].pb(1); depek.insert(0, 1); for (ll i=1; i<=range; ++i) { for (ll j = 0; j <= i * (i - 1)/2; ++j) { int X = 0, Y = (int)dp[i-1].size() - 1; X = max(X, (int)(j - i + 1)); Y = min(Y, (int)j); ll value = depek.query(X, Y); dp[i].pb(value); if (value >= INF) break; } for (int j=0; j<(int)dp[i-1].size(); ++j) depek.czysc(j); for (int j=0; j<(int)dp[i].size(); ++j) depek.insert(j, dp[i][j]); } } inline ll getDP(ll n, ll k) { ll inve = n * (n - 1) / 2; k = min(k, inve - k); //cout << "Getting: " << n << ' ' << k << endl; if (k < 0) return 0; if (k >= (int)dp[n].size()) return INF; return dp[n][k]; } int main() { boost; cin >> n >> k; ll m = n * (n - 1) / 2; if (m % 2 == 1) OUT("NIE"); m /= 2; segmentTree elementy = segmentTree(); FOR(i, 1, n) elementy.insert(i, 1); countDP(250000); if (getDP(n, m) < k) OUT("NIE"); FOR(i, 1, n) { int skip = 0; int pocz = 0, kon = min(m, n - i); while (pocz < kon) { int sr = (pocz + kon)/2; if (getDP(n - i, m - sr) == 0) pocz = ++sr; else kon = sr; } skip = pocz; while (getDP(n - i, m - skip) < k) { k -= getDP(n - i, m - skip); ++skip; } ktore[i] = elementy.search(1 + skip); elementy.czysc(ktore[i]); m -= skip; } cout << "TAK\n"; FOR(i, 1, n) cout << ktore[i] << ' '; } |