#include <cstdio> #include <cstdlib> #include <cstring> #include <cassert> using namespace std; #define MAXN 300000 #define INF 1000000000000000100ULL int N; long long K; long long T; unsigned long long *mahonian[MAXN]; int mahonian_len[MAXN]; bool mahonian_cut[MAXN]; inline unsigned long long get_mahonian(long long n, long long k) { long long maxk = n*(n-1)/2; if (k < 0 || k > maxk) return 0; if (k < mahonian_len[n]) return mahonian[n][k]; if (k > maxk - mahonian_len[n]) return mahonian[n][maxk-k]; return INF; } void compute_mahonian() { int i, j, k; unsigned long long current_row[1000]; mahonian[0] = new unsigned long long[1]; mahonian[0][0] = 0; for(i = 1; i <= N; i++) { current_row[0] = 1; for (j = 1; ; j++) { current_row[j] = 0; for (k = 0; k <= j; k++) { if (k > i-1) break; current_row[j] += get_mahonian(i-1, j-k); if (current_row[j] >= INF) break; } if (current_row[j] >= INF) { j--; mahonian_cut[i] = true; break; } if (current_row[j] == 0) break; } mahonian_len[i] = j+1; mahonian[i] = new unsigned long long[mahonian_len[i]]; memcpy(mahonian[i], current_row, mahonian_len[i]*sizeof(unsigned long long)); } } #define LEFT(i) (2*i+1) #define RIGHT(i) (2*i+2) #define PARENT(i) ((i-1)/2) int count_tree[2*MAXN]; int start; void init_count_tree() { int i; start = 1; while (start < N) start *= 2; start--; for (i = start; i < start + N; i++) count_tree[i] = 1; for (i = start-1; i >= 0; i--) { count_tree[i] = count_tree[LEFT(i)] + count_tree[RIGHT(i)]; } } int tree_get_nth(int n) { int node = 0; while(node < start) { if (n <= count_tree[LEFT(node)]) { node = LEFT(node); } else { n -= count_tree[LEFT(node)]; node = RIGHT(node); } } return node - start + 1; } void tree_remove(int n) { int node = start + n - 1; while (true) { assert(count_tree[node] > 0); count_tree[node]--; if (node == 0) break; node = PARENT(node); } } int main() { int i; scanf("%d%lld", &N, &K); T = (long long) N*(N-1)/2; if (T % 2 == 1) { printf("NIE\n"); return 0; } T /= 2; compute_mahonian(); /* for (int n = 0; n < N; n++) { printf("%d -> %d %d | ", n, mahonian_len[n], mahonian_cut[n]); for (int k = 0; k < mahonian_len[n]; k++) printf("%llu ", mahonian[n][k]); printf("\n"); } */ if (K > get_mahonian(N, T)) { printf("NIE\n"); return 0; } init_count_tree(); printf("TAK\n"); for (i = 1; i <= N; i++) { long long nth; //printf("{%lld %lld %lld}\n", T, (long long)(N-i+1)*(N-i)/2, K); if (T == (long long)(N-i+1)*(N-i)/2) { nth = N-i+1; } else { nth = 1; /*while (T - (nth - 1) > (long long)(N-i)*(N-i-1)/2) { assert(get_mahonian(N-i, T - (nth - 1)) == 0); nth++; }*/ //("<{%d %lld}> ", nth, T + 1 - (long long)(N-i)*(N-i-1)/2); nth = T + 1 - (long long)(N-i)*(N-i-1)/2; if (nth < 1) { nth = 1; } else if (nth >= N-i+1) { nth = N-i+1; } else { for (; nth <= N-i+1; nth++) { //printf("{%d} ", nth); unsigned long long m = get_mahonian(N-i, T - (nth - 1)); //printf("[%lld %lld] {%d %d}", N-i, T - (nth - 1), mahonian_len[N-i], mahonian_cut[N-i]); if (m >= K) break; //printf("(%lld %lld %lld) ", K, T, m); K -= m; } } } T -= (nth - 1); int l = tree_get_nth(nth); //printf("[%d] {%lld} %d \n", nth, T, l); printf("%d ", l); tree_remove(l); } 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 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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | #include <cstdio> #include <cstdlib> #include <cstring> #include <cassert> using namespace std; #define MAXN 300000 #define INF 1000000000000000100ULL int N; long long K; long long T; unsigned long long *mahonian[MAXN]; int mahonian_len[MAXN]; bool mahonian_cut[MAXN]; inline unsigned long long get_mahonian(long long n, long long k) { long long maxk = n*(n-1)/2; if (k < 0 || k > maxk) return 0; if (k < mahonian_len[n]) return mahonian[n][k]; if (k > maxk - mahonian_len[n]) return mahonian[n][maxk-k]; return INF; } void compute_mahonian() { int i, j, k; unsigned long long current_row[1000]; mahonian[0] = new unsigned long long[1]; mahonian[0][0] = 0; for(i = 1; i <= N; i++) { current_row[0] = 1; for (j = 1; ; j++) { current_row[j] = 0; for (k = 0; k <= j; k++) { if (k > i-1) break; current_row[j] += get_mahonian(i-1, j-k); if (current_row[j] >= INF) break; } if (current_row[j] >= INF) { j--; mahonian_cut[i] = true; break; } if (current_row[j] == 0) break; } mahonian_len[i] = j+1; mahonian[i] = new unsigned long long[mahonian_len[i]]; memcpy(mahonian[i], current_row, mahonian_len[i]*sizeof(unsigned long long)); } } #define LEFT(i) (2*i+1) #define RIGHT(i) (2*i+2) #define PARENT(i) ((i-1)/2) int count_tree[2*MAXN]; int start; void init_count_tree() { int i; start = 1; while (start < N) start *= 2; start--; for (i = start; i < start + N; i++) count_tree[i] = 1; for (i = start-1; i >= 0; i--) { count_tree[i] = count_tree[LEFT(i)] + count_tree[RIGHT(i)]; } } int tree_get_nth(int n) { int node = 0; while(node < start) { if (n <= count_tree[LEFT(node)]) { node = LEFT(node); } else { n -= count_tree[LEFT(node)]; node = RIGHT(node); } } return node - start + 1; } void tree_remove(int n) { int node = start + n - 1; while (true) { assert(count_tree[node] > 0); count_tree[node]--; if (node == 0) break; node = PARENT(node); } } int main() { int i; scanf("%d%lld", &N, &K); T = (long long) N*(N-1)/2; if (T % 2 == 1) { printf("NIE\n"); return 0; } T /= 2; compute_mahonian(); /* for (int n = 0; n < N; n++) { printf("%d -> %d %d | ", n, mahonian_len[n], mahonian_cut[n]); for (int k = 0; k < mahonian_len[n]; k++) printf("%llu ", mahonian[n][k]); printf("\n"); } */ if (K > get_mahonian(N, T)) { printf("NIE\n"); return 0; } init_count_tree(); printf("TAK\n"); for (i = 1; i <= N; i++) { long long nth; //printf("{%lld %lld %lld}\n", T, (long long)(N-i+1)*(N-i)/2, K); if (T == (long long)(N-i+1)*(N-i)/2) { nth = N-i+1; } else { nth = 1; /*while (T - (nth - 1) > (long long)(N-i)*(N-i-1)/2) { assert(get_mahonian(N-i, T - (nth - 1)) == 0); nth++; }*/ //("<{%d %lld}> ", nth, T + 1 - (long long)(N-i)*(N-i-1)/2); nth = T + 1 - (long long)(N-i)*(N-i-1)/2; if (nth < 1) { nth = 1; } else if (nth >= N-i+1) { nth = N-i+1; } else { for (; nth <= N-i+1; nth++) { //printf("{%d} ", nth); unsigned long long m = get_mahonian(N-i, T - (nth - 1)); //printf("[%lld %lld] {%d %d}", N-i, T - (nth - 1), mahonian_len[N-i], mahonian_cut[N-i]); if (m >= K) break; //printf("(%lld %lld %lld) ", K, T, m); K -= m; } } } T -= (nth - 1); int l = tree_get_nth(nth); //printf("[%d] {%lld} %d \n", nth, T, l); printf("%d ", l); tree_remove(l); } printf("\n"); return 0; } |