#include <cstdio> #include <algorithm> #include <vector> #include <iterator> #include <set> #define lld long long #define INF 1000000000000000002ll using namespace std; vector <lld> ppNI[250001]; lld normSum(lld n) { if (n > INF) return INF; return n; } lld getPpNI(lld i, lld j) { if (j < 0) return 0; if (j > i * (i - 1) / 2) return 0; if (j >= ppNI[i].size()) { j = i * (i - 1) / 2 - j; } if (j < ppNI[i].size()) { return ppNI[i][j]; } return *ppNI[i].rbegin(); } lld n; lld k; vector <int> result; lld persum = 0; #define TREE 262144 int tree[2 * TREE]; void initTree(int n) { for (int i = TREE + 1; i <= TREE + n; i++) { tree[i] = 1; } for (int i = TREE - 1; i > 0; i--) { tree[i] = tree[2 * i] + tree[2 * i + 1]; } } int popNth(int n) { int w = 1, which = n; while (w < TREE) { if (tree[2 * w] >= which) { w = 2 * w; } else { which -= tree[2 * w]; w = 2 * w + 1; } } which = w - TREE; while (w > 0) { tree[w]--; w = w / 2; } return which; } int main() { scanf("%lld%lld", &n, &k); if (n % 4 == 2 || n % 4 == 3) { printf("NIE\n"); return 0; } ppNI[0] = {1, 0}; ppNI[1] = {1, 0}; for (int i = 2; i <= n; i++) { int j; lld sum = 0; for (j = 0; sum < INF; j++) { sum += getPpNI(i - 1, j); sum -= getPpNI(i - 1, j - i); ppNI[i].push_back(normSum(sum)); if (sum == 0) { break; } } } lld invLeft = (n - 1) * n / 4; if (getPpNI(n, invLeft) < k) { printf("NIE\n"); return 0; } printf("TAK\n"); lld x = invLeft, y = n - 1; for (int i = 1; i <= n; i++) { lld which = x; if (x > y * (y - 1) / 2) x = y * (y - 1) / 2; lld ppni = getPpNI(y, x); while (ppni < k) { //printf("%lld %lld %lld %lld\n", ppni, k, x, y); k -= ppni; ppni = getPpNI(y, --x); } which = which - x; persum += which; //printf("%lld %lld %lld %lld %d\n", ppni, k, x, y, which); result.push_back(which); y--; } initTree(n); for (auto e : result) { printf("%d ", popNth(e + 1)); } printf("\n"); //printf("\n%lld %lld\n", persum, invLeft); 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 | #include <cstdio> #include <algorithm> #include <vector> #include <iterator> #include <set> #define lld long long #define INF 1000000000000000002ll using namespace std; vector <lld> ppNI[250001]; lld normSum(lld n) { if (n > INF) return INF; return n; } lld getPpNI(lld i, lld j) { if (j < 0) return 0; if (j > i * (i - 1) / 2) return 0; if (j >= ppNI[i].size()) { j = i * (i - 1) / 2 - j; } if (j < ppNI[i].size()) { return ppNI[i][j]; } return *ppNI[i].rbegin(); } lld n; lld k; vector <int> result; lld persum = 0; #define TREE 262144 int tree[2 * TREE]; void initTree(int n) { for (int i = TREE + 1; i <= TREE + n; i++) { tree[i] = 1; } for (int i = TREE - 1; i > 0; i--) { tree[i] = tree[2 * i] + tree[2 * i + 1]; } } int popNth(int n) { int w = 1, which = n; while (w < TREE) { if (tree[2 * w] >= which) { w = 2 * w; } else { which -= tree[2 * w]; w = 2 * w + 1; } } which = w - TREE; while (w > 0) { tree[w]--; w = w / 2; } return which; } int main() { scanf("%lld%lld", &n, &k); if (n % 4 == 2 || n % 4 == 3) { printf("NIE\n"); return 0; } ppNI[0] = {1, 0}; ppNI[1] = {1, 0}; for (int i = 2; i <= n; i++) { int j; lld sum = 0; for (j = 0; sum < INF; j++) { sum += getPpNI(i - 1, j); sum -= getPpNI(i - 1, j - i); ppNI[i].push_back(normSum(sum)); if (sum == 0) { break; } } } lld invLeft = (n - 1) * n / 4; if (getPpNI(n, invLeft) < k) { printf("NIE\n"); return 0; } printf("TAK\n"); lld x = invLeft, y = n - 1; for (int i = 1; i <= n; i++) { lld which = x; if (x > y * (y - 1) / 2) x = y * (y - 1) / 2; lld ppni = getPpNI(y, x); while (ppni < k) { //printf("%lld %lld %lld %lld\n", ppni, k, x, y); k -= ppni; ppni = getPpNI(y, --x); } which = which - x; persum += which; //printf("%lld %lld %lld %lld %d\n", ppni, k, x, y, which); result.push_back(which); y--; } initTree(n); for (auto e : result) { printf("%d ", popNth(e + 1)); } printf("\n"); //printf("\n%lld %lld\n", persum, invLeft); return 0; } |