#include <bits/stdc++.h> using namespace std; struct seg_tree{ int size; int* arr; int* right; int* left; seg_tree(int n){ int x = 1; while(x < n) x *= 2; this->size = x * 2 + 1; this->arr = new int[size]; this->left = new int[size]; this->right = new int[size]; for(int i = 0; i < size; i++) this->arr[i] = 1; for(int i = this->size/2; i < this->size; i++){ this->left[i] = this->right[i] = i - this->size/2; } for(int i = this->size/2 - 1; i >= 1; i--){ this->arr[i] = this->arr[i*2] + this->arr[2*i + 1]; this->left[i] = this->left[2*i]; this->right[i] = this->right[2*i + 1]; } } ~seg_tree(){ delete this->left; delete this->right; delete this->arr; } void set_zero(int pos){ this->arr[pos + this->size/2] = 0; int i = (this->size/2 + pos)/2; while(i){ this->arr[i] = this->arr[i*2] + this->arr[2*i + 1]; i /= 2; } } int get_kth(int k, int ind){ if(ind >= this->size/2) return (ind - this->size/2); if(this->arr[ind * 2] >= k) return get_kth(k, ind * 2); else return get_kth(k - this->arr[ind * 2], ind * 2 + 1); } }; const long long MAX = 1000000000000000001; vector<long long> dp[250001]; vector<int> solution; void prepare(){ dp[0].push_back(1); dp[1].push_back(1); for(long long i = 2; i <= 250000; i++){ for(long long k = 0; k <= 171; k++){ if(k==0){ dp[i].push_back(1); continue; } if(i * (i-1)/2 < k){ break; } long long result = 0; for(long long j = min((long long)k, (long long)dp[i-1].size() - 1); j >= max((long long)0, (long long)(k - (i-1))); j--){ result += dp[i-1][j]; if(result >= MAX){ result = MAX; break; } } dp[i].push_back(result); if(result == MAX) break; } } } void find(seg_tree& a, long long n, int ind, long long amount, long long k){ while(1){ // printf("%lld %d %lld %lld\n", n, ind, amount, k); if(ind == n - 1){ printf("TAK\n"); for(int i = 0; i < n - 1; i++) printf("%d ", solution[i]); printf("%d", a.get_kth(1, 1) + 1); printf("\n"); return; } long long length = n - ind; int it; for(long long j = max((long long)0,amount - (length-1) * (length-2)/2); j < length; j++){ long long act = amount - j; it = ind + j; if(act > (length-1) * (length-2)/4){ act = (length-1) * (length-2)/2 - act; } if(dp[length-1][min(act, (long long)dp[length-1].size() - 1)] >= k){ int r = a.get_kth(j+1, 1); solution.push_back(r + 1); a.set_zero(r); ind++; amount = amount - j; break; } k -= dp[length-1][min(act, (long long)dp[length-1].size() - 1)]; it++; } } } void solve(seg_tree& a, long long n, long long k){ if(dp[n].size() - 1 < (n * (n-1))/4){ if(dp[n][dp[n].size()-1] < k){ printf("NIE\n"); return; } } else{ if(dp[n][(n * (n-1))/4] < k){ printf("NIE\n"); return; } } find(a, n, 0, n*(n-1)/4, k); } int main(){ prepare(); int n; long long k; scanf("%d %lld", &n, &k); seg_tree a(n+1); if(n == 1){ if(k == 1){ printf("1\n"); } else{ printf("NIE\n"); } } else{ if(n * (n-1) % 4 != 0){ printf("NIE\n"); }else{ solve(a, n, k); } } }
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 146 147 148 | #include <bits/stdc++.h> using namespace std; struct seg_tree{ int size; int* arr; int* right; int* left; seg_tree(int n){ int x = 1; while(x < n) x *= 2; this->size = x * 2 + 1; this->arr = new int[size]; this->left = new int[size]; this->right = new int[size]; for(int i = 0; i < size; i++) this->arr[i] = 1; for(int i = this->size/2; i < this->size; i++){ this->left[i] = this->right[i] = i - this->size/2; } for(int i = this->size/2 - 1; i >= 1; i--){ this->arr[i] = this->arr[i*2] + this->arr[2*i + 1]; this->left[i] = this->left[2*i]; this->right[i] = this->right[2*i + 1]; } } ~seg_tree(){ delete this->left; delete this->right; delete this->arr; } void set_zero(int pos){ this->arr[pos + this->size/2] = 0; int i = (this->size/2 + pos)/2; while(i){ this->arr[i] = this->arr[i*2] + this->arr[2*i + 1]; i /= 2; } } int get_kth(int k, int ind){ if(ind >= this->size/2) return (ind - this->size/2); if(this->arr[ind * 2] >= k) return get_kth(k, ind * 2); else return get_kth(k - this->arr[ind * 2], ind * 2 + 1); } }; const long long MAX = 1000000000000000001; vector<long long> dp[250001]; vector<int> solution; void prepare(){ dp[0].push_back(1); dp[1].push_back(1); for(long long i = 2; i <= 250000; i++){ for(long long k = 0; k <= 171; k++){ if(k==0){ dp[i].push_back(1); continue; } if(i * (i-1)/2 < k){ break; } long long result = 0; for(long long j = min((long long)k, (long long)dp[i-1].size() - 1); j >= max((long long)0, (long long)(k - (i-1))); j--){ result += dp[i-1][j]; if(result >= MAX){ result = MAX; break; } } dp[i].push_back(result); if(result == MAX) break; } } } void find(seg_tree& a, long long n, int ind, long long amount, long long k){ while(1){ // printf("%lld %d %lld %lld\n", n, ind, amount, k); if(ind == n - 1){ printf("TAK\n"); for(int i = 0; i < n - 1; i++) printf("%d ", solution[i]); printf("%d", a.get_kth(1, 1) + 1); printf("\n"); return; } long long length = n - ind; int it; for(long long j = max((long long)0,amount - (length-1) * (length-2)/2); j < length; j++){ long long act = amount - j; it = ind + j; if(act > (length-1) * (length-2)/4){ act = (length-1) * (length-2)/2 - act; } if(dp[length-1][min(act, (long long)dp[length-1].size() - 1)] >= k){ int r = a.get_kth(j+1, 1); solution.push_back(r + 1); a.set_zero(r); ind++; amount = amount - j; break; } k -= dp[length-1][min(act, (long long)dp[length-1].size() - 1)]; it++; } } } void solve(seg_tree& a, long long n, long long k){ if(dp[n].size() - 1 < (n * (n-1))/4){ if(dp[n][dp[n].size()-1] < k){ printf("NIE\n"); return; } } else{ if(dp[n][(n * (n-1))/4] < k){ printf("NIE\n"); return; } } find(a, n, 0, n*(n-1)/4, k); } int main(){ prepare(); int n; long long k; scanf("%d %lld", &n, &k); seg_tree a(n+1); if(n == 1){ if(k == 1){ printf("1\n"); } else{ printf("NIE\n"); } } else{ if(n * (n-1) % 4 != 0){ printf("NIE\n"); }else{ solve(a, n, k); } } } |