#include<iostream> #include<vector> #include<set> #include<map> #include<algorithm> using namespace std; #define ll long long #define ull long long #define pb push_back #define FOR(i, a, b) for(int i =(a); i <=(b); ++i) #define REP(i, n) for(int i = 0;i <(n); ++i) const int M = 250000; ull T2[250001][20]; ull T1[1000][200]; ull f(int n, int k){ if(n<0 || k<0)return 0; if(n>=1000) return T2[n][k]; else return T1[n][k]; } int F[250000]; vector<int>sol; bool ok2 = false; int g1; int S = 0; void znajdz(ull N,ull M,ull K){ // cout<<"N: "<<N<<" M: "<<M<<" K: "<<K<<" I: "<<(N-1)*(N)/2<<"\n"; int i =0; if((N-2)*(N-1)/2 <= K) i = K-(N-1)*(N-2)/2; while(i<N){ ull K2 = min((N-2)*(N-1)/2 - (K-i),K-i); if(N-1>=1000){ if ( K2 < 10 && K2>=0 && T2[N-1][K2] < M && T2[N-1][K2]>0 ){ M -= T2[N-1][K2]; i++; continue; } }else{ if ( K2 < 191 && K2>=0 && T1[N-1][K2] <M && T1[N-1][K2]>0 ){ M -= T1[N-1][K2]; i++; continue; } } // cout<<F[i]<< " "; sol.pb(F[i+S]); if( i > (N+S)/2 || i > 10){ for(int j = i+S; j < N+S; j++) F[j]=F[j+1]; }else{ for(int k = S+i; k >S; k--){ F[k]=F[k-1]; } ++S; } znajdz(N-1,M,K-i); break; i++; } } const ull max_m = 1000000000000000000ll; void init(){ T1[0][0] = 1; T1[1][0] = 1; T1[2][0] = 1; T1[2][1] = 1; int y = 0; int x = 0; for(int i = 3;i<M;i++){ if(i >= 1000 ){ //cout<<"I:" << i<<" - "; for(int j=0;j<=min((ull)i*(ull)(i-1)/(ull)2,(ull)200);j++){ T2[i][j]=f(i,j-1)+f(i-1,j)-f(i-1,j-i); // cout<<T2[i][j]<<" "; if(T2[i][j]==0){ int fg;cin>>fg; } // cout<<T2[i][j]<<" "; if(T2[i][j]>max_m){ T2[i][j] = max_m; y = max(y,j); break; }else{ y = max(y,j); } } }else{ for(int j=0;j<=min(i*(i-1)/2,500);j++){ T1[i][j]=f(i,j-1)+f(i-1,j)-f(i-1,j-i); if(T1[i][j]>max_m){ T1[i][j] = max_m; x = max(x,j); break; }else{ x = max(x,j); } } } // cout<<"\n"; // int fg;cin>>fg; } // cout<<"y: "<<y<<"\n"; // cout<<"x: "<<x<<"\n"; } int main(){ ios_base::sync_with_stdio(0); init(); //cout<<"init end"; ull N,N2,K,M; cin>>N; cin>>M; //N = 39384; //M = 522394763518044228ull; if(N%4>1){ cout<<"NIE"; return 0; } else{ REP(i,250000)F[i]=i+1; K=N*(N-1)/4; znajdz(N,M,K); if(sol.size()!=N){ cout<<"NIE"; }else{ cout<<"TAK\n"; REP(i,N)cout<<sol[i]<<" "; } } 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 | #include<iostream> #include<vector> #include<set> #include<map> #include<algorithm> using namespace std; #define ll long long #define ull long long #define pb push_back #define FOR(i, a, b) for(int i =(a); i <=(b); ++i) #define REP(i, n) for(int i = 0;i <(n); ++i) const int M = 250000; ull T2[250001][20]; ull T1[1000][200]; ull f(int n, int k){ if(n<0 || k<0)return 0; if(n>=1000) return T2[n][k]; else return T1[n][k]; } int F[250000]; vector<int>sol; bool ok2 = false; int g1; int S = 0; void znajdz(ull N,ull M,ull K){ // cout<<"N: "<<N<<" M: "<<M<<" K: "<<K<<" I: "<<(N-1)*(N)/2<<"\n"; int i =0; if((N-2)*(N-1)/2 <= K) i = K-(N-1)*(N-2)/2; while(i<N){ ull K2 = min((N-2)*(N-1)/2 - (K-i),K-i); if(N-1>=1000){ if ( K2 < 10 && K2>=0 && T2[N-1][K2] < M && T2[N-1][K2]>0 ){ M -= T2[N-1][K2]; i++; continue; } }else{ if ( K2 < 191 && K2>=0 && T1[N-1][K2] <M && T1[N-1][K2]>0 ){ M -= T1[N-1][K2]; i++; continue; } } // cout<<F[i]<< " "; sol.pb(F[i+S]); if( i > (N+S)/2 || i > 10){ for(int j = i+S; j < N+S; j++) F[j]=F[j+1]; }else{ for(int k = S+i; k >S; k--){ F[k]=F[k-1]; } ++S; } znajdz(N-1,M,K-i); break; i++; } } const ull max_m = 1000000000000000000ll; void init(){ T1[0][0] = 1; T1[1][0] = 1; T1[2][0] = 1; T1[2][1] = 1; int y = 0; int x = 0; for(int i = 3;i<M;i++){ if(i >= 1000 ){ //cout<<"I:" << i<<" - "; for(int j=0;j<=min((ull)i*(ull)(i-1)/(ull)2,(ull)200);j++){ T2[i][j]=f(i,j-1)+f(i-1,j)-f(i-1,j-i); // cout<<T2[i][j]<<" "; if(T2[i][j]==0){ int fg;cin>>fg; } // cout<<T2[i][j]<<" "; if(T2[i][j]>max_m){ T2[i][j] = max_m; y = max(y,j); break; }else{ y = max(y,j); } } }else{ for(int j=0;j<=min(i*(i-1)/2,500);j++){ T1[i][j]=f(i,j-1)+f(i-1,j)-f(i-1,j-i); if(T1[i][j]>max_m){ T1[i][j] = max_m; x = max(x,j); break; }else{ x = max(x,j); } } } // cout<<"\n"; // int fg;cin>>fg; } // cout<<"y: "<<y<<"\n"; // cout<<"x: "<<x<<"\n"; } int main(){ ios_base::sync_with_stdio(0); init(); //cout<<"init end"; ull N,N2,K,M; cin>>N; cin>>M; //N = 39384; //M = 522394763518044228ull; if(N%4>1){ cout<<"NIE"; return 0; } else{ REP(i,250000)F[i]=i+1; K=N*(N-1)/4; znajdz(N,M,K); if(sol.size()!=N){ cout<<"NIE"; }else{ cout<<"TAK\n"; REP(i,N)cout<<sol[i]<<" "; } } return 0; } |