#include <iostream> #include<sstream> #include<fstream> #include<algorithm> #include <vector> #include <stdexcept> #include <cstring> #include <set> #define ll long long using namespace std; vector<vector<ll> > D; ll oo = (ll)1e18 + 1; ll getVal(ll n, ll inv) { if(inv ==0)return 1; if(n<=1)return 0; ll all = n*(n-1)/2; if(inv>all)return 0; if(inv > all - inv)inv = all -inv; if(inv >= D[n].size())return oo; return D[n][inv]; } void fill_D(int n){ D.push_back(vector<ll>()); D.push_back(vector<ll>()); for(ll nn=2;nn<n;nn++){ vector<ll> R; R.push_back(1); for(ll inv =1;inv<=nn*(nn-1)/2;inv++){ ll r =0; for(ll s = max(1LL,nn-inv);s<=nn;s++){ ll p = nn-s; r += getVal(nn-1,(inv-p)); if(r>=oo){ r=oo; break; } } if(r==oo)break; else R.push_back(r); } D.push_back(R); // if(nn%1000==0)cout <<nn<<": "<< R.size()<<endl; } } int main() { std::ios::sync_with_stdio(false); istream& my_in = cin; /* for(int n=1;n<50;n++) { int nn = n*(n-1); if(nn%4==0)cout << count(n,nn/4)<<" "; else cout <<"0 "; } cout << endl;*/ /* for(int i=1;i<100;i++)for(int j=0;j<200;j++){ ll res = count(i,j); if(res != D[i][j]) { cout << "error "<<i<<" "<<j<<endl; } if(D[i][j]==0)cout <<"0"; else if(D[i][j]==oo)cout <<"."; else cout <<"x"; if(j==199)cout << endl; }*/ // cout << count(10000,5)<<endl; // cout <<getVal(10000,5)<<endl; ll n; ll k; cin >> n >>k; //n = 5;k=1; fill_D(n+1); if (n*(n-1)%4 !=0 ){ cout <<"NIE\n"; return 0; } ll all = getVal(n, n*(n-1)/4); if(k>all) { cout <<"NIE\n"; return 0; } set<int> S; for(int i=1;i<=n;i++)S.insert(i); cout <<"TAK\n"; ll need_inv = n*(n-1)/4; for(int i=0;i<n;i++) { auto it = S.begin(); ll skipped =0; ll current_pairs = (n-i-1)*(n-i-2)/2; ll to_skip = max(0LL, need_inv - current_pairs); if(to_skip < S.size() - to_skip){ std::advance(it, to_skip); } else { it = S.end(); std::advance(it, to_skip - S.size()); } for(int el =to_skip;el<n;el++){ ll av = getVal(n-i-1, need_inv - el); if(av +skipped >=k) { cout << *it << " "; S.erase(it); k -=skipped; need_inv -=el; break; } else { skipped +=av; } it++; } } cout <<endl; 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 | #include <iostream> #include<sstream> #include<fstream> #include<algorithm> #include <vector> #include <stdexcept> #include <cstring> #include <set> #define ll long long using namespace std; vector<vector<ll> > D; ll oo = (ll)1e18 + 1; ll getVal(ll n, ll inv) { if(inv ==0)return 1; if(n<=1)return 0; ll all = n*(n-1)/2; if(inv>all)return 0; if(inv > all - inv)inv = all -inv; if(inv >= D[n].size())return oo; return D[n][inv]; } void fill_D(int n){ D.push_back(vector<ll>()); D.push_back(vector<ll>()); for(ll nn=2;nn<n;nn++){ vector<ll> R; R.push_back(1); for(ll inv =1;inv<=nn*(nn-1)/2;inv++){ ll r =0; for(ll s = max(1LL,nn-inv);s<=nn;s++){ ll p = nn-s; r += getVal(nn-1,(inv-p)); if(r>=oo){ r=oo; break; } } if(r==oo)break; else R.push_back(r); } D.push_back(R); // if(nn%1000==0)cout <<nn<<": "<< R.size()<<endl; } } int main() { std::ios::sync_with_stdio(false); istream& my_in = cin; /* for(int n=1;n<50;n++) { int nn = n*(n-1); if(nn%4==0)cout << count(n,nn/4)<<" "; else cout <<"0 "; } cout << endl;*/ /* for(int i=1;i<100;i++)for(int j=0;j<200;j++){ ll res = count(i,j); if(res != D[i][j]) { cout << "error "<<i<<" "<<j<<endl; } if(D[i][j]==0)cout <<"0"; else if(D[i][j]==oo)cout <<"."; else cout <<"x"; if(j==199)cout << endl; }*/ // cout << count(10000,5)<<endl; // cout <<getVal(10000,5)<<endl; ll n; ll k; cin >> n >>k; //n = 5;k=1; fill_D(n+1); if (n*(n-1)%4 !=0 ){ cout <<"NIE\n"; return 0; } ll all = getVal(n, n*(n-1)/4); if(k>all) { cout <<"NIE\n"; return 0; } set<int> S; for(int i=1;i<=n;i++)S.insert(i); cout <<"TAK\n"; ll need_inv = n*(n-1)/4; for(int i=0;i<n;i++) { auto it = S.begin(); ll skipped =0; ll current_pairs = (n-i-1)*(n-i-2)/2; ll to_skip = max(0LL, need_inv - current_pairs); if(to_skip < S.size() - to_skip){ std::advance(it, to_skip); } else { it = S.end(); std::advance(it, to_skip - S.size()); } for(int el =to_skip;el<n;el++){ ll av = getVal(n-i-1, need_inv - el); if(av +skipped >=k) { cout << *it << " "; S.erase(it); k -=skipped; need_inv -=el; break; } else { skipped +=av; } it++; } } cout <<endl; return 0; } |