#include <iostream> #include <cmath> #include <cstring> #include <cstdio> #include <algorithm> #include <vector> #include <set> using namespace std; const int N=260000; const long long inf=1.5e18; int n; long long K; vector<long long>dp[N],pre[N]; long long get(long long k1,int n){ if (k1>=dp[n].size()) k1=1ll*n*(n-1)/2-k1; if (k1<0) return 0; if (k1>=dp[n].size()) return inf; return dp[n][k1]; } long long getrange(long long l,long long r,long long n){ long long tot=1ll*n*(n-1)/2; l=max(l,0ll); r=min(r,tot); //cout<<"fa "<<l<<" "<<r<<" "<<n<<endl; if (l>r) return 0; long long ans=0; for (long long i=l;i<=r;i++){ ans=min(inf,get(i,n)+ans); if (ans>=inf) return ans; } return ans; } int ans[N]; int A[N]; void add(int k1,int k2){ for (;k1<=n;k1+=k1&(-k1)) A[k1]+=k2; } int get(int K){ int now=0; //cout<<"fa "<<K<<endl; for (int i=20;i>=0;i--) if (now+(1<<i)<=n&&A[now+(1<<i)]<K){ now+=(1<<i); K-=A[now]; } return now+1; } int main(){ scanf("%d%lld",&n,&K); if (1ll*n*(n-1)%4){ printf("NIE\n"); return 0; } dp[1].push_back(1); for (int i=2;i<=n;i++){ long long lim=1ll*i*(i-1)/4; for (int j=0;j<=lim;j++){ long long now=getrange(j-(i-1),j,i-1); //cout<<i<<" "<<j<<" "<<now<<endl; //cout<<"asd "<<i<<" "<<j<<" "<<now<<endl; if (now>=inf) break; dp[i].push_back(now); } } long long cur=1ll*n*(n-1)/4; if (getrange(cur,cur,n)<K){ printf("NIE\n"); return 0; } for (int i=1;i<=n;i++) add(i,1); for (int where=1;where<=n;where++){ long long tot=1ll*(n-where)*(n-where-1)/2; int fir=max(1ll,cur-tot+1); //cout<<"fas "<<fir<<" "<<cur<<" "<<tot<<endl; for (;;fir++){ long long ne=cur-(fir-1),noww=get(ne,n-where); if (noww>=K){ cur=ne; break; } K-=noww; } ans[where]=get(fir); add(ans[where],-1); } printf("TAK\n"); for (int i=1;i<=n;i++) printf("%d ",ans[i]); printf("\n"); 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 | #include <iostream> #include <cmath> #include <cstring> #include <cstdio> #include <algorithm> #include <vector> #include <set> using namespace std; const int N=260000; const long long inf=1.5e18; int n; long long K; vector<long long>dp[N],pre[N]; long long get(long long k1,int n){ if (k1>=dp[n].size()) k1=1ll*n*(n-1)/2-k1; if (k1<0) return 0; if (k1>=dp[n].size()) return inf; return dp[n][k1]; } long long getrange(long long l,long long r,long long n){ long long tot=1ll*n*(n-1)/2; l=max(l,0ll); r=min(r,tot); //cout<<"fa "<<l<<" "<<r<<" "<<n<<endl; if (l>r) return 0; long long ans=0; for (long long i=l;i<=r;i++){ ans=min(inf,get(i,n)+ans); if (ans>=inf) return ans; } return ans; } int ans[N]; int A[N]; void add(int k1,int k2){ for (;k1<=n;k1+=k1&(-k1)) A[k1]+=k2; } int get(int K){ int now=0; //cout<<"fa "<<K<<endl; for (int i=20;i>=0;i--) if (now+(1<<i)<=n&&A[now+(1<<i)]<K){ now+=(1<<i); K-=A[now]; } return now+1; } int main(){ scanf("%d%lld",&n,&K); if (1ll*n*(n-1)%4){ printf("NIE\n"); return 0; } dp[1].push_back(1); for (int i=2;i<=n;i++){ long long lim=1ll*i*(i-1)/4; for (int j=0;j<=lim;j++){ long long now=getrange(j-(i-1),j,i-1); //cout<<i<<" "<<j<<" "<<now<<endl; //cout<<"asd "<<i<<" "<<j<<" "<<now<<endl; if (now>=inf) break; dp[i].push_back(now); } } long long cur=1ll*n*(n-1)/4; if (getrange(cur,cur,n)<K){ printf("NIE\n"); return 0; } for (int i=1;i<=n;i++) add(i,1); for (int where=1;where<=n;where++){ long long tot=1ll*(n-where)*(n-where-1)/2; int fir=max(1ll,cur-tot+1); //cout<<"fas "<<fir<<" "<<cur<<" "<<tot<<endl; for (;;fir++){ long long ne=cur-(fir-1),noww=get(ne,n-where); if (noww>=K){ cur=ne; break; } K-=noww; } ans[where]=get(fir); add(ans[where],-1); } printf("TAK\n"); for (int i=1;i<=n;i++) printf("%d ",ans[i]); printf("\n"); return 0; } |