#include <iostream> #include <cstdio> #include <vector> #include <stack> using namespace std; const long long M=1e18, N=250005, D=550000, pods=262143; int n, d[D]; long long k, ilo[N]; vector<long long> v[N]; void add(int x) { x+=pods; while(x!=0) { d[x]++; x/=2; } } int suma(int x, int y) { x+=pods; y+=pods; int ans=d[x]; if(x!=y) ans+=d[y]; while(x/2!=y/2) { if(x%2==0) ans+=d[x+1]; if(y%2==1) ans+=d[y-1]; x/=2; y/=2; } return ans; } int BIN(int p, int k, int w) { if(p>=k) return p; int mid=(p+k)/2; int an=mid-suma(1,mid); if(an==w) an+=suma(mid,mid); if(an>=w) return BIN(p,mid,w); return BIN(mid+1,k,w); } int main() { v[1].push_back(1); v[2].push_back(1); v[2].push_back(1); v[3].push_back(1); v[3].push_back(2); v[3].push_back(2); v[3].push_back(1); ilo[1]=1; ilo[2]=2; ilo[3]=4; int il=4; scanf("%d%lld", &n, &k); for(int i=4;i<=n;i++) { ilo[i]=ilo[i-1]+il-1; long long sum=0; for(int j=0;j<v[i-1].size();j++) { sum+=v[i-1][j]; if(j>=il) sum-=v[i-1][j-il]; if(sum>M) { v[i].push_back(M+1); break; } v[i].push_back(sum); } if(sum<=M) for(int j=v[i-1].size()-il;j<v[i-1].size()-1;j++) { sum-=v[i-1][j]; if(sum>M) { v[i].push_back(M+1); break; } v[i].push_back(sum); } il++; } long long sum=0; for(int i=1;i<=n;i++) sum+=i-1; if(sum%2==1) printf("NIE"); else { sum/=2; if(sum<v[n].size() && v[n][sum]<k) printf("NIE"); else { printf("TAK\n"); int nn=n; n--; while(n!=0) { long long wsk=0; long long pref=0; if(sum-wsk>ilo[n]-1) wsk=sum-ilo[n]+1; if(sum-wsk<v[n].size()) { while(k>pref+v[n][sum-wsk]) { pref+=v[n][sum-wsk]; wsk++; } } else { long long dd=ilo[n]-sum+wsk-1; if(dd<(long long)v[n].size()) { int wsk2=dd; while(k>pref+v[n][wsk2]) { pref+=v[n][wsk2]; wsk2++; } wsk+=wsk2; } } int d=BIN(1,nn+1,wsk+1); add(d); printf("%d ", d); k-=pref; sum-=wsk; n--; } printf("%d", BIN(1,nn+1,1)); } } 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 | #include <iostream> #include <cstdio> #include <vector> #include <stack> using namespace std; const long long M=1e18, N=250005, D=550000, pods=262143; int n, d[D]; long long k, ilo[N]; vector<long long> v[N]; void add(int x) { x+=pods; while(x!=0) { d[x]++; x/=2; } } int suma(int x, int y) { x+=pods; y+=pods; int ans=d[x]; if(x!=y) ans+=d[y]; while(x/2!=y/2) { if(x%2==0) ans+=d[x+1]; if(y%2==1) ans+=d[y-1]; x/=2; y/=2; } return ans; } int BIN(int p, int k, int w) { if(p>=k) return p; int mid=(p+k)/2; int an=mid-suma(1,mid); if(an==w) an+=suma(mid,mid); if(an>=w) return BIN(p,mid,w); return BIN(mid+1,k,w); } int main() { v[1].push_back(1); v[2].push_back(1); v[2].push_back(1); v[3].push_back(1); v[3].push_back(2); v[3].push_back(2); v[3].push_back(1); ilo[1]=1; ilo[2]=2; ilo[3]=4; int il=4; scanf("%d%lld", &n, &k); for(int i=4;i<=n;i++) { ilo[i]=ilo[i-1]+il-1; long long sum=0; for(int j=0;j<v[i-1].size();j++) { sum+=v[i-1][j]; if(j>=il) sum-=v[i-1][j-il]; if(sum>M) { v[i].push_back(M+1); break; } v[i].push_back(sum); } if(sum<=M) for(int j=v[i-1].size()-il;j<v[i-1].size()-1;j++) { sum-=v[i-1][j]; if(sum>M) { v[i].push_back(M+1); break; } v[i].push_back(sum); } il++; } long long sum=0; for(int i=1;i<=n;i++) sum+=i-1; if(sum%2==1) printf("NIE"); else { sum/=2; if(sum<v[n].size() && v[n][sum]<k) printf("NIE"); else { printf("TAK\n"); int nn=n; n--; while(n!=0) { long long wsk=0; long long pref=0; if(sum-wsk>ilo[n]-1) wsk=sum-ilo[n]+1; if(sum-wsk<v[n].size()) { while(k>pref+v[n][sum-wsk]) { pref+=v[n][sum-wsk]; wsk++; } } else { long long dd=ilo[n]-sum+wsk-1; if(dd<(long long)v[n].size()) { int wsk2=dd; while(k>pref+v[n][wsk2]) { pref+=v[n][wsk2]; wsk2++; } wsk+=wsk2; } } int d=BIN(1,nn+1,wsk+1); add(d); printf("%d ", d); k-=pref; sum-=wsk; n--; } printf("%d", BIN(1,nn+1,1)); } } return 0; } |