#include <bits/stdc++.h> #include <unistd.h> #define ll long long #define ull unsigned long long #define inf 1000000007 #define inf16 0x3f3f3f3f #define INF 1000000000000000007LL #define VI vector<int> #define VPII vector<pair<int, int> > #define VLL vector<ll> #define PII pair<int, int> #define PLL pair<ll, ll> #define st first #define nd second #define pb push_back #define mp make_pair #define eb emplace_back #define endl '\n' #define ALL(c) (c).begin(), (c).end() using namespace std; #include<bits/stdc++.h> #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>; const int X = 107; const int N = 250007; int n; ll k; ll dp1[X][X*X]; //[>=X][>=15] daje INF ll dp2[N][15]; VI ans; ordered_set<int> S; inline ll get(int n, ll k) { k = min(k, (ll)n*(n-1)/2-k); if(k<0 || k>(ll)n*(n-1)/2) return 0; if(n<X) return dp1[n][k]; else { if(k<15) return dp2[n][k]; else return INF; } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> n >> k; --k; if(n%4 && (n-1)%4) { cout << "NIE\n"; return 0; } dp1[0][0] = 1; for(int i = 0; i+1 < X; ++i) { for(int j = 0; j <= i*(i-1)/2; ++j) { for(int k = 0; k <= i; ++k) { dp1[i+1][j+k] += dp1[i][j]; dp1[i+1][j+k] = min(dp1[i+1][j+k], INF); } } } dp2[0][0] = 1; for(int i = 0; i < n; ++i) { for(int j = 0; j < 15; ++j) { for(int k = 0; k <= i && j+k < 15; ++k) { dp2[i+1][j+k] += dp2[i][j]; dp2[i+1][j+k] = min(dp2[i+1][j+k], INF); } } } ll chce = (ll)n*(n-1)/4; for(int i = 1; i <= n; ++i) { S.insert(i); } for(int i = 1; i <= n; ++i) { chce -= S.size()-1; int skip = max(0ll, -chce); chce += skip; for(int j = skip; j < S.size(); ++j) { if(k>=get(n-i, chce)) { k -= get(n-i, chce); ++chce; } else { ans.pb(*S.find_by_order(j)); S.erase(ans.back()); break; } } } if(k>0) { cout << "NIE\n"; return 0; } while(!S.empty()) { ans.pb(*prev(S.end())); S.erase(prev(S.end())); } cout << "TAK\n"; for(auto it:ans) cout << it << " "; cout << endl; }
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 | #include <bits/stdc++.h> #include <unistd.h> #define ll long long #define ull unsigned long long #define inf 1000000007 #define inf16 0x3f3f3f3f #define INF 1000000000000000007LL #define VI vector<int> #define VPII vector<pair<int, int> > #define VLL vector<ll> #define PII pair<int, int> #define PLL pair<ll, ll> #define st first #define nd second #define pb push_back #define mp make_pair #define eb emplace_back #define endl '\n' #define ALL(c) (c).begin(), (c).end() using namespace std; #include<bits/stdc++.h> #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>; const int X = 107; const int N = 250007; int n; ll k; ll dp1[X][X*X]; //[>=X][>=15] daje INF ll dp2[N][15]; VI ans; ordered_set<int> S; inline ll get(int n, ll k) { k = min(k, (ll)n*(n-1)/2-k); if(k<0 || k>(ll)n*(n-1)/2) return 0; if(n<X) return dp1[n][k]; else { if(k<15) return dp2[n][k]; else return INF; } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> n >> k; --k; if(n%4 && (n-1)%4) { cout << "NIE\n"; return 0; } dp1[0][0] = 1; for(int i = 0; i+1 < X; ++i) { for(int j = 0; j <= i*(i-1)/2; ++j) { for(int k = 0; k <= i; ++k) { dp1[i+1][j+k] += dp1[i][j]; dp1[i+1][j+k] = min(dp1[i+1][j+k], INF); } } } dp2[0][0] = 1; for(int i = 0; i < n; ++i) { for(int j = 0; j < 15; ++j) { for(int k = 0; k <= i && j+k < 15; ++k) { dp2[i+1][j+k] += dp2[i][j]; dp2[i+1][j+k] = min(dp2[i+1][j+k], INF); } } } ll chce = (ll)n*(n-1)/4; for(int i = 1; i <= n; ++i) { S.insert(i); } for(int i = 1; i <= n; ++i) { chce -= S.size()-1; int skip = max(0ll, -chce); chce += skip; for(int j = skip; j < S.size(); ++j) { if(k>=get(n-i, chce)) { k -= get(n-i, chce); ++chce; } else { ans.pb(*S.find_by_order(j)); S.erase(ans.back()); break; } } } if(k>0) { cout << "NIE\n"; return 0; } while(!S.empty()) { ans.pb(*prev(S.end())); S.erase(prev(S.end())); } cout << "TAK\n"; for(auto it:ans) cout << it << " "; cout << endl; } |