#include <stdio.h> #define DEF #define #define RI _ri(); #define RL _rl(); #define RS(x) scanf(" %s",x) #define WI(x) printf("%d\n",x) #define WL(x) printf("%lld\n",x) #define WS(x) puts(x) #define RAI(x,n) FOR(_i,n) x[_i] = RI #define RAL(x,n) FOR(_i,n) x[_i] = RL #define FOR(x,y) for(int x = 0; x < y; x++) static inline int _ri() { int r, i = scanf(" %d",&r); (void)i; return r; } static inline long long _rl() { long long r, i = scanf(" %lld",&r); (void)i; return r; } long long x = 1214967840930909302LL; long long t[21] = { 1, 1, 0, 0, 6, 22, 0, 0, 3836, 29228, 0, 0, 25598186LL, 296643390LL, 0, 0, 738680521142LL, 11501573822788LL, 0, 0, 62119523114983224LL }; long long zz[1000000]; int zl[300000], za[300000], zp; int p[300000], *pp = p; long long f(int k, long long n) { long long m = (long long) k * (k - 1) / 2; if (n > m / 2) n = m-n; if (n < 0) return 0; if (!n) return 1; n--; if (n >= zl[k]) return -1; return zz[za[k] + n]; } #include <string.h> int main() { int k = RI; long long n = RL; if ((k % 4) > 1 || (k < 21 && n > t[k])) { puts("NIE"); return 0; } puts("TAK"); n--; for (int i = 2; i < k; i++) { long long m = (long long) i * (i - 1) / 2; za[i] = zp; for (int j = 1; j <= m; j++) { long long xx = f(i-1,j) + f(i,j-1); if (f(i-1,j) < 0 || f(i,j-1) < 0 || xx > 1000000000000000000LL) break; zz[za[i] + zl[i]++] = xx - f(i-1,j-i); zp++; } } for (int i = 0; i < 2000; i++) { for (int j = 0; j < 100; j++) { // printf("%17lld ", f(i,j)); } // puts(""); } long long m = (long long) k * (k - 1) / 4; int i; for (i = 0; i < k; i++) p[i] = i+1; while (k) { i = 0; long long mk = (long long)(k - 1) * (k - 2) / 2; if (m - i > mk) i = m - mk; for (; i < k && f(k-1,m-i) >= 0 && n >= f(k-1,m-i); i++) { // printf("%d(%d %d) ",f(k-1,m-i),k-1,m-i); n -= f(k-1,m-i); } // puts(""); // printf("[%d] %d rest = %lld\n", p[v], i, n); printf("%d%c", pp[i], k > 1 ? ' ':'\n'); k--; m -= i; if (!i) pp++; else for (; i < k; i++) { pp[i] = pp[i+1]; } // for (i = 0; i < 4; i++) printf("%d ", p[i]); puts(""); } 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 | #include <stdio.h> #define DEF #define #define RI _ri(); #define RL _rl(); #define RS(x) scanf(" %s",x) #define WI(x) printf("%d\n",x) #define WL(x) printf("%lld\n",x) #define WS(x) puts(x) #define RAI(x,n) FOR(_i,n) x[_i] = RI #define RAL(x,n) FOR(_i,n) x[_i] = RL #define FOR(x,y) for(int x = 0; x < y; x++) static inline int _ri() { int r, i = scanf(" %d",&r); (void)i; return r; } static inline long long _rl() { long long r, i = scanf(" %lld",&r); (void)i; return r; } long long x = 1214967840930909302LL; long long t[21] = { 1, 1, 0, 0, 6, 22, 0, 0, 3836, 29228, 0, 0, 25598186LL, 296643390LL, 0, 0, 738680521142LL, 11501573822788LL, 0, 0, 62119523114983224LL }; long long zz[1000000]; int zl[300000], za[300000], zp; int p[300000], *pp = p; long long f(int k, long long n) { long long m = (long long) k * (k - 1) / 2; if (n > m / 2) n = m-n; if (n < 0) return 0; if (!n) return 1; n--; if (n >= zl[k]) return -1; return zz[za[k] + n]; } #include <string.h> int main() { int k = RI; long long n = RL; if ((k % 4) > 1 || (k < 21 && n > t[k])) { puts("NIE"); return 0; } puts("TAK"); n--; for (int i = 2; i < k; i++) { long long m = (long long) i * (i - 1) / 2; za[i] = zp; for (int j = 1; j <= m; j++) { long long xx = f(i-1,j) + f(i,j-1); if (f(i-1,j) < 0 || f(i,j-1) < 0 || xx > 1000000000000000000LL) break; zz[za[i] + zl[i]++] = xx - f(i-1,j-i); zp++; } } for (int i = 0; i < 2000; i++) { for (int j = 0; j < 100; j++) { // printf("%17lld ", f(i,j)); } // puts(""); } long long m = (long long) k * (k - 1) / 4; int i; for (i = 0; i < k; i++) p[i] = i+1; while (k) { i = 0; long long mk = (long long)(k - 1) * (k - 2) / 2; if (m - i > mk) i = m - mk; for (; i < k && f(k-1,m-i) >= 0 && n >= f(k-1,m-i); i++) { // printf("%d(%d %d) ",f(k-1,m-i),k-1,m-i); n -= f(k-1,m-i); } // puts(""); // printf("[%d] %d rest = %lld\n", p[v], i, n); printf("%d%c", pp[i], k > 1 ? ' ':'\n'); k--; m -= i; if (!i) pp++; else for (; i < k; i++) { pp[i] = pp[i+1]; } // for (i = 0; i < 4; i++) printf("%d ", p[i]); puts(""); } return 0; } |