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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*********************************
*****     Common v2.0.8      *****
*********************************/
#include <bits/stdc++.h>

void test();

namespace common {

    template<typename T>
    T gcd(T const &a, T const &b) { return b == 0 ? a : gcd(b, a%b); }


    namespace io {
        template<typename T>
        inline std::ostream& operator<< (std::ostream& out, const std::vector<T>& data) {
            if(data.size() > 0)
            {
                out << data[0];

                for( auto it = data.begin()+1; it != data.end(); it++ )
                    out << ' ' << *it;
            }
            return out;
        }

        template<typename T>
        inline std::istream& operator>> (std::istream& in, std::vector<T>& data) {
            for( auto &v : data )
                in >> v;
            return in;
        }

        template<typename A, typename B>
        inline std::ostream& operator<< (std::ostream& out, const std::pair<A, B>& data) {
            return out << data.first << ' ' << data.second;
        }

        template<typename A, typename B>
        inline std::istream& operator>> (std::istream& in, std::pair<A, B>& data) {
            return in >> data.first >> data.second;
        }
    }

    template<typename T>
    class vector_from_one : public std::vector<T> {
        public:
            using std::vector<T>::vector;
            T& operator[] (size_t n) { return std::vector<T>::operator[](n-1); }
            const T& operator[] (size_t n) const { return std::vector<T>::operator[](n-1); }
            T& at (size_t n) { return std::vector<T>::at(n-1); }
            const T& at (size_t n) const { return std::vector<T>::at(n-1); }
    };


    namespace functional {    
        template<typename Type, typename C> struct GetComparator { using Comparator = C; };
        template<typename Type> struct GetComparator<Type, void> { using Comparator = std::less<Type>; };

        template<typename Object, typename Type, Type Object::* default_field, typename Comparator = void>
        struct CompareField
        {
            const Type Object::* field;

            CompareField() : field(default_field) {}
            CompareField(Type Object::* field) : field(field) {}
            constexpr bool operator()(Object const &lhs, Object const &rhs) const
            {
                return typename GetComparator<Type, Comparator>::Comparator()(lhs.*field, rhs.*field);
            }
        };

        template<typename Comparator = void, typename Object, typename Type>
        const CompareField<Object, Type, nullptr, Comparator> compare_field(Type Object::* field) { return {field}; }

        template<typename iterator_type>
        class Iterable
        {
            typedef iterator_type iterator;
            const iterator begin_iterator;
            const iterator end_iterator;
        public:
            Iterable(iterator begin, iterator end) : begin_iterator(begin), end_iterator(end) {}
            
            iterator begin() { return begin_iterator; }
            iterator end() { return end_iterator; }
        };

        template<typename iterator>
        Iterable<iterator> iterable(iterator begin, iterator end){ return Iterable<iterator>(begin, end); }

        template<typename Collection>
        auto reversed(Collection &collection) -> Iterable<decltype(collection.rbegin())> { return iterable(collection.rbegin(), collection.rend()); }
    }

    
    namespace operators {
        struct base_operator {};
        template <typename LHS, typename OP> struct operator_proxy { LHS lhs; OP op; };

        template <typename LHS, typename OP, typename SFINAE = typename std::enable_if<std::is_base_of<base_operator, typename std::remove_reference<OP>::type>::value>::type>
        operator_proxy<LHS, OP> operator<(LHS &&lhs, OP &&op) { return {lhs, op}; }

        template <typename LHS, typename OP, typename RHS>
        auto operator>(operator_proxy<LHS, OP> proxy, RHS &&rhs) -> decltype(proxy.op(proxy.lhs, rhs)) { proxy.op(proxy.lhs, rhs); }

        struct : public base_operator { template <typename T> void operator()(T &x, T y) const { x = std::max(x, y); } } const set_if_greater;
        struct : public base_operator { template <typename T> void operator()(T &x, T y) const { x = std::min(x, y); } } const set_if_less;
    }
    

    namespace main {
        int _default(int const, char const * const[])
        {
            std::cout << "Undefined common::main app!" << std::endl;
            return 1;
        }

        int (*_app)(int const, char const * const[]);

        class one
        {
            static int main(int const argc, char const * const argv[])
            {
                std::ios_base::sync_with_stdio(false);
                std::cin.tie(NULL);
                test();
                return 0;
            }
        public:
            one() { _app = main; }
        };

        class many
        {
            static int main(int const argc, char const * const argv[])
            {
                std::ios_base::sync_with_stdio(false);
                std::cin.tie(NULL);
                int T;
                std::cin >> T;
                while( T --> 0 )
                    test();
                return 0;
            }
        public:
            many() { _app = main; }
        };

        class all
        {
            static int main(int const argc, char const * const argv[])
            {
                std::ios_base::sync_with_stdio(false);
                std::cin.tie(NULL);
                std::cin.exceptions(std::ifstream::eofbit);
                try {
                    while(std::cin)
                        test();
                }
                catch (std::ifstream::failure&) {}
                return 0;
            }
        public:
            all() { _app = main; }
        };
    }
}

int main(int const argc, char const * const argv[])
{
    return common::main::_app(argc, argv);
}

using namespace std;
using namespace common;
using namespace common::io;
using namespace common::functional;
using namespace common::operators;

// end of #include <common.hpp>
//====================================================

main::one _;

const long long INF = 1000000000000000001ll;

template<typename T>
class SparseMagicArray
{
public:
    struct Row { int index; int size; long long capacity; };

    vector<T> data;
    vector<Row> _rows;
public:
    void next_row(long long capacity) {
        _rows.push_back({(int)data.size(), 0, capacity});
    }

    void push(T value) {
        data.push_back(value);
        _rows.back().size++;
    }

    T operator() (int n, long long k) const {
        if (n < 0 or k < 0) return 0;

        auto &row = _rows[n];
        if (k >= row.capacity) return 0;

        k <set_if_less> abs(row.capacity - 1 - k);

        if (k >= row.size) return INF;

        return data[row.index + k];
    }

    int rows() const {
        return _rows.size();
    }

    int cols(int n) const {
        return _rows[n].size;
    }

    long long capacity(int n) const {
        return _rows[n].capacity;
    }
};

class Tree
{
    int size;
    vector<int> counts;

    int& data(int idx) { return counts.at(idx + size - 1); }
public:
    Tree(int _size)
    {
        int total_size = 1;
        for (size = 1; size < _size; )
        {
            size *= 2;
            total_size += size;
        }

        counts.resize(total_size, 0);

        for (int i = 0; i < size; ++i)
        {
            data(i) = i < _size ? 1 : 0;
        }

        for (int i = size - 2; i >= 0; --i)
        {
            counts.at(i) = counts.at(2 * i + 1) + counts.at(2 * i + 2);
        }
    }

    void print() const
    {
        for (int i = 0; i < (int) counts.size(); ++i)
        {
            if (i > 0 && (i & (i + 1)) == 0)
                cout << endl;

            cout << counts[i] << ' ';
        }
        cout << endl << endl;
    }

    void erase(int value) {
        int index = value + size - 1;
        counts[index] = 0;

        while (index > 0) {
            index = (index - 1) / 2;
            counts.at(index) = counts.at(2 * index + 1) + counts.at(2 * index + 2);
        }
    }

    int nth_element(int n)
    {
        int index = 0;
        const int first_valid = size - 1;

        if (counts[0] <= n)
            return -1;

        while (index < first_valid)
        {
            index = 2 * index + 1;
            if (counts[index] <= n)
            {
                n -= counts[index];
                ++index;
            }
        }

        return index-first_valid;
    }
};

void test() {
    int n;
    long long k;
    cin >> n >> k;

    if (n bitand 0x2) 
    {
        cout << "NIE" << endl;
        return;
    }

    long long required_inv = (1LL * n * (n-1) / 2) / 2;
 
    SparseMagicArray<long long> array;
    array.next_row(1);
    array.push(1);

    long long row_length = 1;
    for (int i = 1; i <= n; ++i)
    {
        row_length += (i-1);
        array.next_row(row_length);

        for (int j = 0;; ++j)
        {
            long long next = array(i, j-1) + array(i-1, j) - array(i-1, j-i);

            if (next <= 0)
                break;

            if (next > k)
            {
                break;
            }

            array.push(next);
        }
    }

    if (array(n, required_inv) < k)
    {
        cout << "NIE" << endl;
        return;
    }

    cout << "TAK\n";

    Tree tree(n);

    for (int pos = 0; pos < n; ++pos)
    {
        int next_index = 0;

        long long zeros_to_skip = (required_inv-next_index) - array.capacity(n-pos-1);

        if ( zeros_to_skip > 0 )
        {
            next_index += zeros_to_skip;
        }

        while (true) 
        {
            if (next_index > n) return;
            long long current_perms = array(n-pos-1, required_inv-next_index);
            if (k <= current_perms)
            {
                break;
            }

            k -= current_perms;
            next_index++;
        }

        required_inv -= next_index;

        int answer = tree.nth_element(next_index);
        cout << answer+1 << ' ';
        tree.erase(answer); 
    }
    cout << endl;
}