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
#include <iostream>
#include <list>
#include <assert.h>
#include <vector>

using namespace std;

typedef long long int ll;
typedef unsigned long long int ull;

int n;
ll k;

const int MAXN = 300000;
//const int SEC = MAXN*(MAXN-1)/2 + 4;

const ull MAXK = 1e18;

int REV;

const int tree_size = 262144;
int tree[tree_size*2];

void insert(int index, int val) {
    index += tree_size;
    tree[index] = val;
    
    for(int i = index/2; i > 0; i /= 2) {
        tree[i] = tree[i*2] + tree[i*2+1];
    }
}

int query(int a, int b) {
    a += tree_size;
    b += tree_size;
    
    int ret = tree[a];
    if(a!=b) ret += tree[b];
    
    while (a / 2 != b / 2) {
        if(a%2 == 0) {
            ret += tree[a+1];
        }
        
        if(b%2== 1) {
            ret += tree[b-1];
        }
        
        a /= 2;
        b /= 2;
    }
    
    return ret;
}


void read() {
    cin >> n >> k;
    REV = (n*(n-1))/2;
}

//ull dp_sum[MAXN+1][SEC+1];

vector<vector<ull> > dp;

ull query_num(int i, int j) {
    if(j < dp[i].size()) return dp[i][j];
    ull size = (i*(i-1))/2 + 1;
    if(j >= size) return 0;
    ull inf_start = dp[i].size();
    ull inf_end = inf_start + (size - dp[i].size()*2) - 1;
    if(j >= inf_start && j <= inf_end) return MAXK;
    assert(size-1-j >= 0);
    return dp[i][size-1-j];
}

void count_dp() {
    dp.resize(n+1);
    dp[0].push_back(1);
    dp[1].push_back(1);
    
    
    for(int i = 2; i <= n; ++i) {
        dp[i].push_back(1);
        int len = i*(i-1)/2 + 1;
        
        for(int j = 1; j < len; ++j) {
            ull my_val = dp[i][j-1] + query_num(i-1, j);
            int idx = j-i;
            if(idx >= 0) {
                my_val -=  query_num(i-1, idx);
            }
            if(my_val > MAXK) break;
            dp[i].push_back(my_val);
        }
        
        
    }
}

list<int> nums_left;
list<int>::iterator it;

int answer[MAXN+1];



void DFS_ans(int i, ll left, long long int sum_rev) {
    if(i == n) return;
    int len = n-i;

    for(it = nums_left.begin(); it != nums_left.end(); ++it) {
        int bigger = query(0, (*it)-1);
        if((REV/2 - sum_rev - bigger) < 0 || left < 0) {
            return;
        }
        ll num = query_num(len-1, REV/2 - sum_rev - bigger);
        if(num >= left) {
            answer[i] = *it;
            insert(*it, 0);
            nums_left.erase(it);
            DFS_ans(i+1, left, sum_rev + bigger);
            return;
        }
        
        left -= num;
    }
}

bool count_ans() {
    for(int i = 1; i <= 250000; ++i) {
        insert(i, 1);
    }
    for(int i = 1; i <= n; ++i) {
        nums_left.push_back(i);
    }
    
    DFS_ans(0, k, 0);
    
    for(int i = 0; i < n; ++i) {
        if(answer[i] == 0) return false;
    }
    return true;
    
}

void write_tab() {
    /*for(int i = 0; i <= 230; ++i) {
     for(int j = 21; j <= 21; ++j) {
     cout << dp[j][i] << " ";
     }
     cout << endl;
     }*/
    int index = 0;
    for(auto len: dp) {
        cout << index << ": ";
        for(ull val : len) {
            cout << val << " ";
        }
        cout << endl;
        index++;
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    
    
    read();
    if(REV%2 == 1) {
        cout << "NIE" << endl;
        return 0;
    }
    
    
    count_dp();


    
    bool can =count_ans();
    if(can) {
        cout << "TAK" << endl;
        for(int i = 0; i < n; ++i) {
            cout << answer[i] << " ";
        }
        cout << endl;
    } else {
        cout << "NIE" << endl;
    }
    
    return 0;
}