#include <bits/stdc++.h> #include <unordered_map> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; using namespace std; #define PB push_back #define MP make_pair #define LL long long #define int LL #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define FORD(i,a,b) for(int i = (a); i >= (b); i--) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define R(i,n) REP(i,n) #define VI vector<int> #define PII pair<int,int> #define LD long double #define FI first #define SE second #define st FI #define nd SE #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define M_PI 3.14159265358979323846 template<class C> void mini(C& _a4, C _b4) { _a4 = min(_a4, _b4); } template<class C> void maxi(C& _a4, C _b4) { _a4 = max(_a4, _b4); } template<class TH> void _dbg(const char *sdbg, TH h) { cerr << sdbg << '=' << h << endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while (*sdbg != ',')cerr << *sdbg++; cerr << '=' << h << ','; _dbg(sdbg + 1, a...); } template<class T> ostream& operator<<(ostream& os, vector<T> V) { os << "["; for (auto& vv : V) os << vv << ","; os << "]"; return os; } template<class T> ostream& operator<<(ostream& os, set<T> V) { os << "["; for (auto& vv : V) os << vv << ","; os << "]"; return os; } template<class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) { return os << "(" << P.st << "," << P.nd << ")"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #endif const int maxn = 2.5e5; const int maxk = 1e18; const int mx = LLONG_MAX / 2; vector<VI> inv(maxn + 1); int getInvCount(int n, int k) { if (k > n * (n - 1) / 2) { return 0; } mini(k, (n * (n - 1) / 2 - k)); if (SZ(inv[n]) <= k) { return mx + 1; } else { return inv[n][k]; } } int getInvBrut(int n, int k) { VI dupa(n); REP(i, n) { dupa[i] = i; } int res = 0; do { int acc = 0; REP(i, n) { FOR(j, i + 1, n - 1) { if (dupa[i] > dupa[j]) { acc++; } } } res += acc == k; } while (next_permutation(ALL(dupa))); return res; } int32_t main(int32_t argc, char ** argv) { ios_base::sync_with_stdio(0); inv[1].push_back(1); int total = 0; FOR(i, 2, maxn) { int acc = 1; inv[i].push_back(acc); int mid = i * (i - 1) / 4; FOR(j, 1, mid) { if (j - i >= 0) { acc -= getInvCount(i - 1, j - i); } acc += getInvCount(i - 1, j); if (acc > maxk) { break; } inv[i].push_back(acc); total++; } } int n, k; cin >> n >> k; int inwersje = n * (n - 1) / 4; if(n * (n - 1) % 4 != 0){ cout<<"NIE\n"; return 0; } ordered_set<int> secik; REP(i, n){ secik.insert(i + 1); } VI res; REP(i, n) { int elementy = SZ(secik); int mInwersje = (elementy - 1) * (elementy - 2) / 2; int diff = max(0LL, inwersje - mInwersje); FOR(j, diff, elementy - 1){ int dupsko = getInvCount(elementy - 1, inwersje - j); if(dupsko >= k){ auto it = secik.find_by_order(j); res.PB(*it); secik.erase(it); inwersje -= j; goto nxt; } else{ k -= dupsko; } } break; nxt:; } //cerr<<res<<"\n"; if(SZ(res) != n || k != 1 || inwersje != 0){ cout<<"NIE\n"; } else{ cout<<"TAK\n"; for(int a: res){ cout<<a<<" "; } cout<<"\n"; } }
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 | #include <bits/stdc++.h> #include <unordered_map> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; using namespace std; #define PB push_back #define MP make_pair #define LL long long #define int LL #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define FORD(i,a,b) for(int i = (a); i >= (b); i--) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define R(i,n) REP(i,n) #define VI vector<int> #define PII pair<int,int> #define LD long double #define FI first #define SE second #define st FI #define nd SE #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define M_PI 3.14159265358979323846 template<class C> void mini(C& _a4, C _b4) { _a4 = min(_a4, _b4); } template<class C> void maxi(C& _a4, C _b4) { _a4 = max(_a4, _b4); } template<class TH> void _dbg(const char *sdbg, TH h) { cerr << sdbg << '=' << h << endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while (*sdbg != ',')cerr << *sdbg++; cerr << '=' << h << ','; _dbg(sdbg + 1, a...); } template<class T> ostream& operator<<(ostream& os, vector<T> V) { os << "["; for (auto& vv : V) os << vv << ","; os << "]"; return os; } template<class T> ostream& operator<<(ostream& os, set<T> V) { os << "["; for (auto& vv : V) os << vv << ","; os << "]"; return os; } template<class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) { return os << "(" << P.st << "," << P.nd << ")"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #endif const int maxn = 2.5e5; const int maxk = 1e18; const int mx = LLONG_MAX / 2; vector<VI> inv(maxn + 1); int getInvCount(int n, int k) { if (k > n * (n - 1) / 2) { return 0; } mini(k, (n * (n - 1) / 2 - k)); if (SZ(inv[n]) <= k) { return mx + 1; } else { return inv[n][k]; } } int getInvBrut(int n, int k) { VI dupa(n); REP(i, n) { dupa[i] = i; } int res = 0; do { int acc = 0; REP(i, n) { FOR(j, i + 1, n - 1) { if (dupa[i] > dupa[j]) { acc++; } } } res += acc == k; } while (next_permutation(ALL(dupa))); return res; } int32_t main(int32_t argc, char ** argv) { ios_base::sync_with_stdio(0); inv[1].push_back(1); int total = 0; FOR(i, 2, maxn) { int acc = 1; inv[i].push_back(acc); int mid = i * (i - 1) / 4; FOR(j, 1, mid) { if (j - i >= 0) { acc -= getInvCount(i - 1, j - i); } acc += getInvCount(i - 1, j); if (acc > maxk) { break; } inv[i].push_back(acc); total++; } } int n, k; cin >> n >> k; int inwersje = n * (n - 1) / 4; if(n * (n - 1) % 4 != 0){ cout<<"NIE\n"; return 0; } ordered_set<int> secik; REP(i, n){ secik.insert(i + 1); } VI res; REP(i, n) { int elementy = SZ(secik); int mInwersje = (elementy - 1) * (elementy - 2) / 2; int diff = max(0LL, inwersje - mInwersje); FOR(j, diff, elementy - 1){ int dupsko = getInvCount(elementy - 1, inwersje - j); if(dupsko >= k){ auto it = secik.find_by_order(j); res.PB(*it); secik.erase(it); inwersje -= j; goto nxt; } else{ k -= dupsko; } } break; nxt:; } //cerr<<res<<"\n"; if(SZ(res) != n || k != 1 || inwersje != 0){ cout<<"NIE\n"; } else{ cout<<"TAK\n"; for(int a: res){ cout<<a<<" "; } cout<<"\n"; } } |