// Krzysztof Małysa #include <bits/stdc++.h> using namespace std; #define FOR(i,a,n) for (auto i = (a), i ## __ = (n); i <= i ## __; ++i) #define FORD(i,a,n) for (auto i = (a), i ## __ = (n); i >= i ## __; --i) #define REP(i,n) FOR(i, 0, n - 1) #define ALL(x) x.begin(), x.end() #define SZ(x) (int(x.size())) #define EB emplace_back #define ST first #define ND second #define tpv typedef vector< typedef long long LL; typedef pair<int, int> PII; tpv int> VI; tpv VI> VVI; tpv PII> VPII; tpv LL> VLL; constexpr char nl = '\n'; #define endl nl #define ris return *this #define tem template<class T tem, class B> inline void mini(T&& a, B&& b) { if (b < a) a = b; } tem, class B> inline void maxi(T&& a, B&& b) { if (b > a) a = b; } int ceil2(int x) { return x < 2 ? 1 : 1 << (sizeof(x) * 8 - __builtin_clz(x - 1)); } tem> struct Dump { T a, b; }; tem> auto dump(T&& x) -> Dump<decltype(x.begin())> { return {ALL(x)}; } struct Debug { ~Debug() { cerr << endl; } tem> auto operator<<(T x) -> decltype(cerr << x, *this) { cerr << boolalpha << x; return *this; } tem> auto operator<<(T x) -> decltype(x.begin(), *this) { auto a = x.begin(), b = x.end(); *this << "{"; for (; a != b;) *this << *a++, *this << (a == b ? "" : " "); return *this << "}"; } tem, class B> Debug& operator<<(pair<T, B> p) { ris << "(" << p.ST << ", " << p.ND << ")"; } tem> Debug& operator<<(Dump<T> d) { *this << "{\n"; for (T a = d.a, c = a; a != d.b; ++a) *this << " " << distance(c, a) << ": " << *a << '\n'; ris << "}"; } }; struct Foo {tem>Foo& operator<<(T) {ris;}}; #ifdef DEBUG # define deb Debug() #else # define deb Foo() #endif #define imie(x...) #x ": " << (x) << " " #define LOG(x...) deb << imie(x) #define DOG(x...) deb << #x ": " << dump(x) << " " class ITree { int size; VI t; // t[i] - how many zeros are in the subtree of i public: explicit ITree(int n) : size(ceil2(n)), t(size << 1, 1) { FORD (i, size - 1, 1) t[i] = t[i << 1] + t[i << 1 | 1]; } void set1(int x) { x += size; if (t[x]) { t[x] = 0; while (x >>= 1) --t[x]; } } // Finds k-th 0, assuming that first is 0-th int findkth0(int k) { return findkth0(k, 1, size); } int findkth0(int k, int i, int sz) { LOG(VI{k, i, sz}); if (sz == 1) return i - size; int sz2 = sz >> 1; i <<= 1; if (t[i] <= k) return findkth0(k - t[i], i + 1, sz2); else return findkth0(k, i, sz2); } }; const LL INF = 1000000000000000001LL; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; LL k; cin >> n >> k; // Calculate dp - dp[i][j] = # of permutations of length i having j inversions vector<vector<LL>> dpp(2); auto dp = [&](int i, LL j, LL sz) { if (j > sz) return 0LL; j = min(j, sz - j); if (j >= SZ(dpp[i])) return INF; else return dpp[i][j]; }; dpp[0] = {}; dpp[1] = {1}; FOR (i, 2, n) { dpp.EB(VLL{1}); FOR (j, 1LL, i * LL(i - 1) >> 2) { LL dppij = dpp[i][j - 1]; LL sz = LL(i - 2) * (i - 1) >> 1; dppij += dp(i - 1, j, sz); if (j >= i) dppij -= dp(i - 1, j - i, sz); if (dppij >= INF) break; dpp[i].EB(dppij); } } DOG(dpp); // Find the result LL j = LL(n - 1) * n >> 2; if (LL(n - 1) * n & 3 || dp(n, j, LL(n - 1) * n >> 1) < k) return puts("NIE"), 0; cout << "TAK\n"; --k; ITree tree(n); LL sum = 0; FORD (i, n, 2) { LL p = min(LL(i - 2) * (i - 1) >> 1, j); LL sz = LL(i - 2) * (i - 1) >> 1; for (;;) { LL x = dp(i - 1, p, sz); // cerr << imie(x) << nl; if (x > k) break; k -= x; --p; ++sum; } // LOG(VLL{i, p, j, k}); LOG(j - p); int x = tree.findkth0(j - p); LOG(x); tree.set1(x); cout << x + 1 << ' '; j = p; } cout << tree.findkth0(0) + 1 << nl; // cerr << sum << nl; 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 | // Krzysztof Małysa #include <bits/stdc++.h> using namespace std; #define FOR(i,a,n) for (auto i = (a), i ## __ = (n); i <= i ## __; ++i) #define FORD(i,a,n) for (auto i = (a), i ## __ = (n); i >= i ## __; --i) #define REP(i,n) FOR(i, 0, n - 1) #define ALL(x) x.begin(), x.end() #define SZ(x) (int(x.size())) #define EB emplace_back #define ST first #define ND second #define tpv typedef vector< typedef long long LL; typedef pair<int, int> PII; tpv int> VI; tpv VI> VVI; tpv PII> VPII; tpv LL> VLL; constexpr char nl = '\n'; #define endl nl #define ris return *this #define tem template<class T tem, class B> inline void mini(T&& a, B&& b) { if (b < a) a = b; } tem, class B> inline void maxi(T&& a, B&& b) { if (b > a) a = b; } int ceil2(int x) { return x < 2 ? 1 : 1 << (sizeof(x) * 8 - __builtin_clz(x - 1)); } tem> struct Dump { T a, b; }; tem> auto dump(T&& x) -> Dump<decltype(x.begin())> { return {ALL(x)}; } struct Debug { ~Debug() { cerr << endl; } tem> auto operator<<(T x) -> decltype(cerr << x, *this) { cerr << boolalpha << x; return *this; } tem> auto operator<<(T x) -> decltype(x.begin(), *this) { auto a = x.begin(), b = x.end(); *this << "{"; for (; a != b;) *this << *a++, *this << (a == b ? "" : " "); return *this << "}"; } tem, class B> Debug& operator<<(pair<T, B> p) { ris << "(" << p.ST << ", " << p.ND << ")"; } tem> Debug& operator<<(Dump<T> d) { *this << "{\n"; for (T a = d.a, c = a; a != d.b; ++a) *this << " " << distance(c, a) << ": " << *a << '\n'; ris << "}"; } }; struct Foo {tem>Foo& operator<<(T) {ris;}}; #ifdef DEBUG # define deb Debug() #else # define deb Foo() #endif #define imie(x...) #x ": " << (x) << " " #define LOG(x...) deb << imie(x) #define DOG(x...) deb << #x ": " << dump(x) << " " class ITree { int size; VI t; // t[i] - how many zeros are in the subtree of i public: explicit ITree(int n) : size(ceil2(n)), t(size << 1, 1) { FORD (i, size - 1, 1) t[i] = t[i << 1] + t[i << 1 | 1]; } void set1(int x) { x += size; if (t[x]) { t[x] = 0; while (x >>= 1) --t[x]; } } // Finds k-th 0, assuming that first is 0-th int findkth0(int k) { return findkth0(k, 1, size); } int findkth0(int k, int i, int sz) { LOG(VI{k, i, sz}); if (sz == 1) return i - size; int sz2 = sz >> 1; i <<= 1; if (t[i] <= k) return findkth0(k - t[i], i + 1, sz2); else return findkth0(k, i, sz2); } }; const LL INF = 1000000000000000001LL; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; LL k; cin >> n >> k; // Calculate dp - dp[i][j] = # of permutations of length i having j inversions vector<vector<LL>> dpp(2); auto dp = [&](int i, LL j, LL sz) { if (j > sz) return 0LL; j = min(j, sz - j); if (j >= SZ(dpp[i])) return INF; else return dpp[i][j]; }; dpp[0] = {}; dpp[1] = {1}; FOR (i, 2, n) { dpp.EB(VLL{1}); FOR (j, 1LL, i * LL(i - 1) >> 2) { LL dppij = dpp[i][j - 1]; LL sz = LL(i - 2) * (i - 1) >> 1; dppij += dp(i - 1, j, sz); if (j >= i) dppij -= dp(i - 1, j - i, sz); if (dppij >= INF) break; dpp[i].EB(dppij); } } DOG(dpp); // Find the result LL j = LL(n - 1) * n >> 2; if (LL(n - 1) * n & 3 || dp(n, j, LL(n - 1) * n >> 1) < k) return puts("NIE"), 0; cout << "TAK\n"; --k; ITree tree(n); LL sum = 0; FORD (i, n, 2) { LL p = min(LL(i - 2) * (i - 1) >> 1, j); LL sz = LL(i - 2) * (i - 1) >> 1; for (;;) { LL x = dp(i - 1, p, sz); // cerr << imie(x) << nl; if (x > k) break; k -= x; --p; ++sum; } // LOG(VLL{i, p, j, k}); LOG(j - p); int x = tree.findkth0(j - p); LOG(x); tree.set1(x); cout << x + 1 << ' '; j = p; } cout << tree.findkth0(0) + 1 << nl; // cerr << sum << nl; return 0; } |