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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Karol Kosinski
#include <cstdio>
#include <utility>
#include <algorithm>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define FOD(i,b,a) for(int i=(b)-1;i>=(a);--i)
//#define DEBUG(x...) printf(x)
#define DEBUG(x...)
//#define DEBUG2(x...) printf(x)
#define DEBUG2(x...)
using namespace std;

typedef unsigned long long LG;
typedef pair<LG, LG> DLG;
constexpr LG MOD = 1000000000000000ull;
constexpr int MX_N = 114;
constexpr int MX_INV = (MX_N - 2) * (MX_N - 1) / 2 + 1;
constexpr int MX_SIZE = 250001;
constexpr int MX_EXP = 15;
constexpr int T_SIZE = 262145;
constexpr LG INFTY = 1000000000000000003ull;
DLG P[MX_N][MX_INV];
DLG Q[MX_SIZE][MX_EXP];
int indPerm = 0;
int Perm[MX_SIZE];
int levelT;
int T[20][T_SIZE];
int n;
LG k;

inline DLG toDLG(LG a)
{
    return make_pair(a / MOD, a % MOD);
}

DLG operator+(const DLG& a, const DLG& b)
{
    DLG res = a;
    res.second += b.second;
    if (res.second >= MOD)
    {
        res.second -= MOD;
        res.first += 1;
    }
    res.first += b.first;
    return res;
}

DLG operator-(const DLG& a, const DLG& b)
{
    DLG res = a;
    if (res.second < b.second)
    {
        res.first -= 1;
        res.second += MOD;
    }
    res.second -= b.second;
    res.first -= b.first;
    return res;
}

inline DLG val(int n, int inv)
{
    if (inv < 0) return make_pair(0, 0);
    return P[n][inv];
}

inline DLG valq(int n, int inv)
{
    if (inv < 0) return make_pair(0, 0);
    return Q[n][inv];
}

void preproc()
{
    const auto INF = toDLG(INFTY);
    P[0][0] = toDLG(1);
    P[1][0] = toDLG(1);
    P[2][0] = toDLG(1);
    P[2][1] = toDLG(1);
    FOR(n,3,MX_N)
    {
        FOR(inv,0,MX_INV)
        {
            P[n][inv] = min(val(n, inv - 1) + val(n - 1, inv) - val(n - 1, inv - n), INF);
            DEBUG2("%llu%15llu   ", P[n][inv].first, P[n][inv].second);
        }
        DEBUG2("\n");
        DEBUG2("%llu%15llu   \n", P[n][n*(n-1)/4].first, P[n][n*(n-1)/4].second);
        DEBUG2("%llu%15llu   \n", val(n-1, n*(n-1)/4-n+1).first, val(n-1, n*(n-1)/4-n+1).second);
    }
    DEBUG("%u\n", sizeof(P));
    
    Q[0][0] = toDLG(1);
    Q[1][0] = toDLG(1);
    Q[2][0] = toDLG(1);
    Q[2][1] = toDLG(1);
    FOR(n,3,MX_SIZE)
    {
        FOR(inv,0,MX_EXP)
        {
            Q[n][inv] = min(valq(n, inv - 1) + valq(n - 1, inv) - valq(n - 1, inv - n), INF);
            DEBUG2("%llu%15llu   ", Q[n][inv].first, Q[n][inv].second);
        }
        DEBUG2("\n");
    }
    DEBUG("%u\n", sizeof(Q));
}

inline int left(int ind)
{
    return 2 * ind;
}

inline int right(int ind)
{
    return 2 * ind + 1;
}

void initT()
{
    int aux = n;
    FOR(i,0,aux) T[0][i] = 1;
    while (aux > 1)
    {
        ++levelT;
        aux = (aux + 1) / 2;
        FOR(i,0,aux)
        {
            T[levelT][i] = T[levelT - 1][left(i)] + T[levelT - 1][right(i)];
            DEBUG2("%7d", T[levelT][i]);
        }
        DEBUG2("\n");
    }
    DEBUG2("%d\n", levelT);
    DEBUG("%d\n", sizeof(T));
}

int selectLowest(int ind)
{
    ++ind;
    int i = levelT;
    int pos = 0;
    while (i > 0)
    {
        DEBUG("==== i(%d)   pos(%d)   ind(%d)   val(%d)\n", i, pos, ind, T[i][pos]);
        T[i--][pos] -= 1;
        if (ind <= T[i][left(pos)])
        {
            pos = left(pos);
        }
        else
        {
            ind -= T[i][left(pos)];
            pos = right(pos);
        }
    }
    DEBUG("==== i(%d)   pos(%d)   ind(%d)   val(%d)\n", i, pos, ind, T[i][pos]);
    T[i][pos] -= 1;
    return pos + 1;
}

int main()
{
    preproc();
    scanf("%d%llu", &n, &k);
    if (n % 4 > 1)
    {
        printf("NIE\n");
        return 0;
    }
    initT();
    if (n < MX_N)
    {
        auto kk = toDLG(k);
        int ind = n*(n-1)/4;
        DEBUG("** ind: %d   \n", ind);
        DEBUG("** kk: %llu%15llu   \n", kk.first, kk.second);
        if (P[n][ind] < kk)
        {
            printf("NIE\n");
            return 0;
        }
        FOD(nn,n,0)
        {
            ind -= nn;
            DEBUG("** ind: %d   \n", ind);
            DEBUG("** kk: %llu%15llu   \n", kk.first, kk.second);
            int start = ind;
            ind = max(ind, 0);
            DLG sum = make_pair(0, 0);
            DLG next = val(nn, ind);
            DEBUG("---- next: %llu%15llu   \n", next.first, next.second);
            while (kk > next)
            {
                sum = next;
                next = next + val(nn, ++ind);
                DEBUG("---- ind: %d   \n", ind);
                DEBUG("---- next: %llu%15llu   \n", next.first, next.second);
            }
            kk = kk - sum;
            Perm[indPerm++] = selectLowest(ind - start);
        }
        printf("TAK\n");
        FOR(i,0,indPerm) printf("%d ", Perm[i]);
        printf("\n");
    }
    else
    {
        long long bigInd = (long long)(n)*(long long)(n-1)/4;
        DEBUG("** bigInd: %lld\n", bigInd);
        int aux = 0;
        int ind = 0;
        FOD(nn,n,MX_N-1)
        {
            if (bigInd - nn < MX_EXP)
            {
                DEBUG("!!!!!!! nn: %d\n", nn);
                aux = nn;
                ind = int(bigInd);
                break;
            }
            bigInd -= nn;
            DEBUG("** bigInd: %lld\n", bigInd);
            Perm[indPerm++] = selectLowest(0);
        }
        auto kk = toDLG(k);
        FOD(nn,aux+1,0)
        {
            ind -= nn;
            DEBUG("** ind: %d   \n", ind);
            DEBUG("** kk: %llu%15llu   \n", kk.first, kk.second);
            int start = ind;
            ind = max(ind, 0);
            DLG sum = make_pair(0, 0);
            DLG next = valq(nn, ind);
            DEBUG("---- next: %llu%15llu   \n", next.first, next.second);
            while (kk > next)
            {
                sum = next;
                next = next + valq(nn, ++ind);
                DEBUG("---- ind: %d   \n", ind);
                DEBUG("---- next: %llu%15llu   \n", next.first, next.second);
            }
            kk = kk - sum;
            Perm[indPerm++] = selectLowest(ind - start);
        }
        printf("TAK\n");
        FOR(i,0,indPerm) printf("%d ", Perm[i]);
        printf("\n");
    }
    return 0;
}