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
#include <algorithm>
#include <cstdio>
#include <ctime>

#define PER_BRUT_ENABLED

using namespace std;

static const int N = 250000;

static int a[N];

static long long permutations(const int n)
{
    static const int MAX = 21;
    static long long perms[MAX + 2] =
    {
        0, 0, 0, 0, 6, 22, 0, 0, 3836, 29228, 0, 0, 25598186, 296643390, 0, 0, 738680521142, 11501573822788, 0, 0, 62119523114983224,
        1214967840930909302, 4611686018427387904
    };

    return perms[min(n*(!(n % 4) || !((n - 1) % 4)), MAX + 1)];
}

static bool inv(const int a[], const int n, const long long p)
{
    long long r = p;

    for(int i = 0; i < n - 1 && r >= 0; i++)
    {
        for(int j = i + 1; j < n && r >= 0; j++)
        {
            r -= (a[j] < a[i]);
        }
    }

    return r == 0;
}

static void generateNext(int a[], const int n, const long long p)
{
    do
    {
        int i = n - 2;

        while(a[i] > a[i + 1])
        {
            i--;
        }

        int j = n - 1;

        while(a[i] > a[j])
        {
            j--;
        }

        swap(a[i], a[j]);

        int r = n - 1;
        int s = i + 1;

        while(r > s)
        {
            swap(a[r], a[s]);
            r--;
            s++;
        }
    }
    while(!inv(a, n, p));
}

static void generate(int a[], const int n, const int k)
{
    for(int i = 0; i < n; i++)
    {
        a[i] = i + 1;
    }

    const long long p = n*(n - 1LL)/4;

    for(int i = 0; i < k; i++)
    {
        generateNext(a, n, p);
    }
}

static void print(const int a[], const int n)
{
    for(int i = 0; i < n; i++)
    {
        printf("%d ", a[i]);
    }

    puts("");
}

#ifdef PER_BRUT_ENABLED
int main()
{
    int n, k;

    while(scanf("%d%d", &n, &k) > 0)
    {
        const long long m = permutations(n);

        if (k > m)
        {
            puts("NIE");
        }
        else
        {
            generate(a, n, k);
            puts("TAK");
            print(a, n);
        }
    }

    return 0;
}
#endif