#include <cstdio> #include <list> #include <numeric> typedef unsigned long long ullong; const int MAX_N = 250009; int a[MAX_N]; const int MAX_BIG_PRE = 20; ullong dp[100][100*102]; ullong dp2[MAX_N][MAX_BIG_PRE]; const ullong INF = 4*1000000000ULL*1000000000ULL; void precalc() { dp[0][0] = 1; for (int i = 1; i < 100; ++i) { dp[i][0] = 1; for (int j = 1; j <= i*(i-1)/4; ++j) { if (dp[i-1][j] == INF || dp[i][j-1] == INF) { for (; j <= i*(i-1)/4; ++j) { dp[i][j] = INF; } } else { dp[i][j] = dp[i-1][j]; if (i <= j) { dp[i][j] -= dp[i-1][j-i]; } dp[i][j] += dp[i][j-1]; if (dp[i][j] > INF) { dp[i][j] = INF; } } } for (int j = i*(i-1)/4+1; j <= i*(i-1)/2; ++j) { dp[i][j] = dp[i][i*(i-1)/2-j]; } } for (int i = 0; i < MAX_BIG_PRE; ++i) { dp2[99][i] = dp[99][i]; } for (int i = 100; i < MAX_N; ++i) { dp2[i][0] = 1; for (int j = 1; j < MAX_BIG_PRE; ++j) { dp2[i][j] = dp2[i-1][j]+dp2[i][j-1]; if (dp2[i][j] > INF) { dp2[i][j] = INF; } } } } ullong T(int n, ullong k) { if (k > ((ullong)n)*(n-1)/2) { return 0; } if (k > ((ullong)n)*(n-1)/4) { return T(n, ((ullong)n)*(n-1)/2-k); } if (n >= 100) { if (k > MAX_BIG_PRE) { return INF; } return dp2[n][k]; } return dp[n][k]; } int tree[MAX_N*4]; int left(int n) { return n*2+1; } int right(int n) { return n*2+2; } void init_tree(int num, int node = 0, int cap = 0) { if (node == 0) { cap = 1; while (cap < num) { cap *= 2; } } if (cap == 1) { tree[node] = -1; } else { if (num > cap/2) { tree[node] = cap/2; init_tree(cap/2, left(node), cap/2); init_tree(num-cap/2, right(node), cap/2); } else { tree[node] = num; init_tree(num, left(node), cap/2); init_tree(0, right(node), cap/2); } } } int take_nth(int n) { int elem = 0; int node = 0; while (tree[node] >= 0) { elem *= 2; if (n >= tree[node]) { ++elem; n -= tree[node]; node = right(node); } else { --tree[node]; node = left(node); } } return elem+1; } int main() { int n; ullong k; scanf("%d%lld", &n, &k); if (n%4 > 1) { puts("NIE"); return 0; } precalc(); ullong invs = 1ULL*n*(n-1)/4; if (T(n, invs) != INF && T(n, invs) < k) { puts("NIE"); return 0; } for (int i = 0; i < n-1; ++i) { if (invs > ((ullong)n-i-1)*(n-i-2)/2) { a[i] = invs-((ullong)n-i-1)*(n-i-2)/2; invs = ((ullong)n-i-1)*(n-i-2)/2; } while (T(n-i-1, invs) != INF && T(n-i-1, invs) < k) { k -= T(n-i-1, invs); ++a[i]; --invs; } } puts("TAK"); init_tree(n); printf("%d", take_nth(a[0])); for (int i = 1; i < n; ++i) { printf(" %d", take_nth(a[i])); } printf("\n"); }
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 | #include <cstdio> #include <list> #include <numeric> typedef unsigned long long ullong; const int MAX_N = 250009; int a[MAX_N]; const int MAX_BIG_PRE = 20; ullong dp[100][100*102]; ullong dp2[MAX_N][MAX_BIG_PRE]; const ullong INF = 4*1000000000ULL*1000000000ULL; void precalc() { dp[0][0] = 1; for (int i = 1; i < 100; ++i) { dp[i][0] = 1; for (int j = 1; j <= i*(i-1)/4; ++j) { if (dp[i-1][j] == INF || dp[i][j-1] == INF) { for (; j <= i*(i-1)/4; ++j) { dp[i][j] = INF; } } else { dp[i][j] = dp[i-1][j]; if (i <= j) { dp[i][j] -= dp[i-1][j-i]; } dp[i][j] += dp[i][j-1]; if (dp[i][j] > INF) { dp[i][j] = INF; } } } for (int j = i*(i-1)/4+1; j <= i*(i-1)/2; ++j) { dp[i][j] = dp[i][i*(i-1)/2-j]; } } for (int i = 0; i < MAX_BIG_PRE; ++i) { dp2[99][i] = dp[99][i]; } for (int i = 100; i < MAX_N; ++i) { dp2[i][0] = 1; for (int j = 1; j < MAX_BIG_PRE; ++j) { dp2[i][j] = dp2[i-1][j]+dp2[i][j-1]; if (dp2[i][j] > INF) { dp2[i][j] = INF; } } } } ullong T(int n, ullong k) { if (k > ((ullong)n)*(n-1)/2) { return 0; } if (k > ((ullong)n)*(n-1)/4) { return T(n, ((ullong)n)*(n-1)/2-k); } if (n >= 100) { if (k > MAX_BIG_PRE) { return INF; } return dp2[n][k]; } return dp[n][k]; } int tree[MAX_N*4]; int left(int n) { return n*2+1; } int right(int n) { return n*2+2; } void init_tree(int num, int node = 0, int cap = 0) { if (node == 0) { cap = 1; while (cap < num) { cap *= 2; } } if (cap == 1) { tree[node] = -1; } else { if (num > cap/2) { tree[node] = cap/2; init_tree(cap/2, left(node), cap/2); init_tree(num-cap/2, right(node), cap/2); } else { tree[node] = num; init_tree(num, left(node), cap/2); init_tree(0, right(node), cap/2); } } } int take_nth(int n) { int elem = 0; int node = 0; while (tree[node] >= 0) { elem *= 2; if (n >= tree[node]) { ++elem; n -= tree[node]; node = right(node); } else { --tree[node]; node = left(node); } } return elem+1; } int main() { int n; ullong k; scanf("%d%lld", &n, &k); if (n%4 > 1) { puts("NIE"); return 0; } precalc(); ullong invs = 1ULL*n*(n-1)/4; if (T(n, invs) != INF && T(n, invs) < k) { puts("NIE"); return 0; } for (int i = 0; i < n-1; ++i) { if (invs > ((ullong)n-i-1)*(n-i-2)/2) { a[i] = invs-((ullong)n-i-1)*(n-i-2)/2; invs = ((ullong)n-i-1)*(n-i-2)/2; } while (T(n-i-1, invs) != INF && T(n-i-1, invs) < k) { k -= T(n-i-1, invs); ++a[i]; --invs; } } puts("TAK"); init_tree(n); printf("%d", take_nth(a[0])); for (int i = 1; i < n; ++i) { printf(" %d", take_nth(a[i])); } printf("\n"); } |