#include <iostream> #define MAXLONG 9223372036854775807 #define MAXNA 500 #define MAXIA 500 #define MAXNB 250000 #define MAXIB 10 using namespace std; long long ta[MAXNA][MAXIA]; long long tb[MAXNB][MAXIB]; long long perm(long long n, long long i) { long long a = (n * (n - 1)) / 2; if (a - i < i) i = a - i; if (i < 0) return 0; else { if (n < MAXNA) { if (i < MAXIA) return ta[n][i]; else return MAXLONG; } else { if (i < MAXIB) return tb[n][i]; else return MAXLONG; } } } void init_t() { ta[0][0] = 1; ta[1][0] = 1; for (int n = 2; n < MAXNA; n++) { ta[n][0] = 1; for (int i = 1; i < MAXIA; i++) { ta[n][i] = ta[n][i - 1] + ta[n - 1][i]; if (ta[n][i] < 0) ta[n][i] = MAXLONG; else if (i >= n) { ta[n][i] -= ta[n - 1][i - n]; if (ta[n][i] < 0) ta[n][i] = MAXLONG; } } } tb[0][0] = 1; tb[1][0] = 1; for (int n = 2; n < MAXNB; n++) { tb[n][0] = 1; for (int i = 1; i < MAXIB; i++) { tb[n][i] = tb[n][i - 1] + tb[n - 1][i]; if (tb[n][i] < 0) tb[n][i] = MAXLONG; else { if (i >= n) tb[n][i] -= tb[n - 1][i - n]; if (tb[n][i] < 0) tb[n][i] = MAXLONG; } } } } int set[524287]; void set_add(int n) { n += 262144; while (n != 0) { set[n]++; n /= 2; } } void set_remove(int n) { n += 262144; while (n != 0) { set[n]--; n /= 2; } } int set_nth(int n) { int i = 1; while (i < 262144) { i *= 2; if (n > set[i]) { n -= set[i]; i++; } } return i - 262144; } int p[250001]; int main() { init_t(); long long n, k, in, m, a; cin>>n>>k; in = (n * (n - 1) / 4); if (n % 4 == 2 || n % 4 == 3 || perm(n, in) < k) cout<<"NIE\n"; else { cout<<"TAK\n"; for (int i = 1; i <= n; i++) set_add(i); for (int i = 1; i <= n; i++) { a = 1; m = n - i; if (in > (m * (m - 1)) / 2) { a += in - (m * (m - 1)) / 2; in = (m * (m - 1)) / 2; } while (k > perm(m, in)) { k -= perm(m, in); in--; a++; } p[i] = set_nth(a); set_remove(p[i]); cout<<p[i]<<" "; } cout<<"\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 153 154 155 156 157 158 159 | #include <iostream> #define MAXLONG 9223372036854775807 #define MAXNA 500 #define MAXIA 500 #define MAXNB 250000 #define MAXIB 10 using namespace std; long long ta[MAXNA][MAXIA]; long long tb[MAXNB][MAXIB]; long long perm(long long n, long long i) { long long a = (n * (n - 1)) / 2; if (a - i < i) i = a - i; if (i < 0) return 0; else { if (n < MAXNA) { if (i < MAXIA) return ta[n][i]; else return MAXLONG; } else { if (i < MAXIB) return tb[n][i]; else return MAXLONG; } } } void init_t() { ta[0][0] = 1; ta[1][0] = 1; for (int n = 2; n < MAXNA; n++) { ta[n][0] = 1; for (int i = 1; i < MAXIA; i++) { ta[n][i] = ta[n][i - 1] + ta[n - 1][i]; if (ta[n][i] < 0) ta[n][i] = MAXLONG; else if (i >= n) { ta[n][i] -= ta[n - 1][i - n]; if (ta[n][i] < 0) ta[n][i] = MAXLONG; } } } tb[0][0] = 1; tb[1][0] = 1; for (int n = 2; n < MAXNB; n++) { tb[n][0] = 1; for (int i = 1; i < MAXIB; i++) { tb[n][i] = tb[n][i - 1] + tb[n - 1][i]; if (tb[n][i] < 0) tb[n][i] = MAXLONG; else { if (i >= n) tb[n][i] -= tb[n - 1][i - n]; if (tb[n][i] < 0) tb[n][i] = MAXLONG; } } } } int set[524287]; void set_add(int n) { n += 262144; while (n != 0) { set[n]++; n /= 2; } } void set_remove(int n) { n += 262144; while (n != 0) { set[n]--; n /= 2; } } int set_nth(int n) { int i = 1; while (i < 262144) { i *= 2; if (n > set[i]) { n -= set[i]; i++; } } return i - 262144; } int p[250001]; int main() { init_t(); long long n, k, in, m, a; cin>>n>>k; in = (n * (n - 1) / 4); if (n % 4 == 2 || n % 4 == 3 || perm(n, in) < k) cout<<"NIE\n"; else { cout<<"TAK\n"; for (int i = 1; i <= n; i++) set_add(i); for (int i = 1; i <= n; i++) { a = 1; m = n - i; if (in > (m * (m - 1)) / 2) { a += in - (m * (m - 1)) / 2; in = (m * (m - 1)) / 2; } while (k > perm(m, in)) { k -= perm(m, in); in--; a++; } p[i] = set_nth(a); set_remove(p[i]); cout<<p[i]<<" "; } cout<<"\n"; } } |