#include <bits/stdc++.h> using namespace std; #define fru(j,n) for(int j=0; j<(n); ++j) #define tr(it,v) for(auto it=(v).begin(); it!=(v).end(); ++it) #define x first #define y second #define pb push_back #define ALL(G) (G).begin(),(G).end() #define DEBUG #ifdef DEBUG #define DEB printf #else #define DEB(...) #endif typedef double D; typedef long long LL; typedef pair<int,int> pii; typedef vector<int> vi; const int MAXN = 250*1000+5, milion = 1000*1000, N=25, NN=42; const LL INF=1LL*milion*milion*milion+5; LL dp[MAXN][N], dp2[NN][NN*NN]; void prep(){ dp[1][0]=1; for(int n=2;n<MAXN;++n) for(int k=0;k<=min(N-1LL,1LL*n*(n-1)/2);++k){ LL s=0; fru(i,min(n,k+1)) s=min(INF,s+dp[n-1][k-i]); dp[n][k]=s; } dp2[1][0]=1; for(int n=2;n<NN;++n) for(int k=0;k<=n*(n-1)/2;++k){ LL s=0; fru(i,min(n,k+1)) s=min(INF,s+dp2[n-1][k-i]); dp2[n][k]=s; } } int n; int T[MAXN]; LL k; int total; int A[MAXN]; int mag(int x){ return x-(x&(x-1));} void add(int x,int val){ while(x<MAXN){ A[x]+=val; x+=mag(x); } } int licz(int x){ int ret=0; while(x) { ret+=A[x]; x-=mag(x); } return ret; } void zakoncz(){ puts("TAK"); for(int i=1;i<=n;++i) add(i,1); fru(i,n){ int pp=0,kk=n+1; while(pp+1<kk){ int m=(pp+kk)/2; if(licz(m)<=T[i]) pp=m; else kk=m; } printf("%d ",kk); add(kk,-1); } exit(0); } void go(int poz, LL inv){ if(inv<0) return; if(poz==n){ if(inv) return; ++total; k--; if(k==0) { zakoncz(); } return; } int left=n-poz; LL HALF=1LL*left*(left-1)/2; if(inv>HALF) return; LL tu=INF; if(left<NN) tu=dp2[left][inv]; else if(inv<N || HALF-inv<N) tu=dp[left][min(inv,HALF-inv)]; if(tu<k){ k-=tu; return; } int pp=0,kk=left; while(pp+1<kk){ int m=(pp+kk)/2; if(inv-m<=1LL*(left-1)*(left-2)/2){ kk=m; } else pp=m; } kk=min(1LL*left,inv+1); for(int i=pp;i<kk;++i){ T[poz]=i; go(poz+1,inv-i); } } int main(){ prep(); scanf("%d%lld",&n,&k); if(1LL*n*(n-1)%4){ puts("NIE"); return 0; } go(0,1LL*n*(n-1)/4); puts("NIE"); 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 | #include <bits/stdc++.h> using namespace std; #define fru(j,n) for(int j=0; j<(n); ++j) #define tr(it,v) for(auto it=(v).begin(); it!=(v).end(); ++it) #define x first #define y second #define pb push_back #define ALL(G) (G).begin(),(G).end() #define DEBUG #ifdef DEBUG #define DEB printf #else #define DEB(...) #endif typedef double D; typedef long long LL; typedef pair<int,int> pii; typedef vector<int> vi; const int MAXN = 250*1000+5, milion = 1000*1000, N=25, NN=42; const LL INF=1LL*milion*milion*milion+5; LL dp[MAXN][N], dp2[NN][NN*NN]; void prep(){ dp[1][0]=1; for(int n=2;n<MAXN;++n) for(int k=0;k<=min(N-1LL,1LL*n*(n-1)/2);++k){ LL s=0; fru(i,min(n,k+1)) s=min(INF,s+dp[n-1][k-i]); dp[n][k]=s; } dp2[1][0]=1; for(int n=2;n<NN;++n) for(int k=0;k<=n*(n-1)/2;++k){ LL s=0; fru(i,min(n,k+1)) s=min(INF,s+dp2[n-1][k-i]); dp2[n][k]=s; } } int n; int T[MAXN]; LL k; int total; int A[MAXN]; int mag(int x){ return x-(x&(x-1));} void add(int x,int val){ while(x<MAXN){ A[x]+=val; x+=mag(x); } } int licz(int x){ int ret=0; while(x) { ret+=A[x]; x-=mag(x); } return ret; } void zakoncz(){ puts("TAK"); for(int i=1;i<=n;++i) add(i,1); fru(i,n){ int pp=0,kk=n+1; while(pp+1<kk){ int m=(pp+kk)/2; if(licz(m)<=T[i]) pp=m; else kk=m; } printf("%d ",kk); add(kk,-1); } exit(0); } void go(int poz, LL inv){ if(inv<0) return; if(poz==n){ if(inv) return; ++total; k--; if(k==0) { zakoncz(); } return; } int left=n-poz; LL HALF=1LL*left*(left-1)/2; if(inv>HALF) return; LL tu=INF; if(left<NN) tu=dp2[left][inv]; else if(inv<N || HALF-inv<N) tu=dp[left][min(inv,HALF-inv)]; if(tu<k){ k-=tu; return; } int pp=0,kk=left; while(pp+1<kk){ int m=(pp+kk)/2; if(inv-m<=1LL*(left-1)*(left-2)/2){ kk=m; } else pp=m; } kk=min(1LL*left,inv+1); for(int i=pp;i<kk;++i){ T[poz]=i; go(poz+1,inv-i); } } int main(){ prep(); scanf("%d%lld",&n,&k); if(1LL*n*(n-1)%4){ puts("NIE"); return 0; } go(0,1LL*n*(n-1)/4); puts("NIE"); return 0; } |