#include <stdio.h> #include <stdlib.h> #define MAXN 256000 int fx(long long n, long long k) { long long s = 0; long long i; if (n==1) return k==0 ? 1 : 0; for (i=0;i<=k && i<n;i++) s += fx(n-1, k-i); return s; } int get_ix(int *av, int j) { int s = 0, i; for (i=0;i<MAXN;i++) { if (av[i]!=-1) s++; if (s-1==j) return i; } } int main() { long long n, k, i, mm; int av[MAXN]; scanf("%lld%lld", &n, &k); mm = n*(n-1)/2; if (mm%2!=0) { printf("NIE\n"); return 0; } mm /= 2; if (k>fx(n, mm)) { printf("NIE\n"); return 0; } for (i=0;i<n;i++) av[i] = i; printf("TAK\n"); for (i=0;i<n-1;i++) { long long s = 0, s1 = 0; int j = 0; int ix; //printf("(%d,%d,%d)\n", n-i, k, mm); while (s<k) { s1 = s; s += fx(n-i-1, mm-j); j++; } j--; ix = get_ix(av, j); printf("%d ", ix+1); av[ix] = -1; mm -= j; k -= s1; } for (i=0;i<n;i++) if (av[i]!=-1) printf("%d", i+1); 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 | #include <stdio.h> #include <stdlib.h> #define MAXN 256000 int fx(long long n, long long k) { long long s = 0; long long i; if (n==1) return k==0 ? 1 : 0; for (i=0;i<=k && i<n;i++) s += fx(n-1, k-i); return s; } int get_ix(int *av, int j) { int s = 0, i; for (i=0;i<MAXN;i++) { if (av[i]!=-1) s++; if (s-1==j) return i; } } int main() { long long n, k, i, mm; int av[MAXN]; scanf("%lld%lld", &n, &k); mm = n*(n-1)/2; if (mm%2!=0) { printf("NIE\n"); return 0; } mm /= 2; if (k>fx(n, mm)) { printf("NIE\n"); return 0; } for (i=0;i<n;i++) av[i] = i; printf("TAK\n"); for (i=0;i<n-1;i++) { long long s = 0, s1 = 0; int j = 0; int ix; //printf("(%d,%d,%d)\n", n-i, k, mm); while (s<k) { s1 = s; s += fx(n-i-1, mm-j); j++; } j--; ix = get_ix(av, j); printf("%d ", ix+1); av[ix] = -1; mm -= j; k -= s1; } for (i=0;i<n;i++) if (av[i]!=-1) printf("%d", i+1); printf("\n"); return 0; } |