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
#include <bits/stdc++.h>
using namespace std;

#define FOR(i,a,n) for (LL i = (a), i##__ = (n); i <= i##__; ++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define FORD(i,a,n) for (LL i = (a), i##__ = (n); i >= i##__; --i)
#define REPD(i,n) FORD(i,(n)-1,0)
#define ALL(x) x.begin(), x.end()
#define ALLR(x) x.rbegin(), x.rend()
#define EB emplace_back
#define ST first
#define ND second
#define OS ostream
#define OO(A) template<class... T> OS& operator<<(OS& os, const A<T...>& x) { return __o(os, ALL(x)); }
#define OD(...) OS& operator<<(OS &os, const __VA_ARGS__ &x)
#define SZ(x) ((int)x.size())
#define RS resize
#define V vector
#define nl '\n'

typedef long long LL;
typedef pair<int, int> PII;
typedef V<int> VI;
typedef V<VI> VVI;
typedef V<PII> VPII;
typedef V<VPII> VVPII;
typedef V<bool> VB;
typedef V<VB> VVB;

template<class I> OS& __o(OS&, I, I);
template<class T, size_t N> OD(array<T, N>) { return __o(os, ALL(x)); }
OO(vector) OO(deque) OO(set) OO(multiset) OO(map) OO(multimap)
template<class A, class B> OD(pair<A, B>) {
    return os << "(" << x.ST << ", " << x.ND << ")";
}
template<class I> OS& __o(OS& os, I a, I b) {
    os << "{ ";
    for (; a != b;)
        os << *a++, os << " ";
    return os << "}";
}
template<class I> OS& __d(OS& os, I a, I b) {
    os << "{\n";
    for (I c = a; a != b; ++a)
        os << "  " << distance(c, a) << ": " << *a << endl;
    return os << "}";
}
template<class... T> void __e(T&&... a) {
    int t[] = {(cerr << forward<T>(a), 0)...}; (void)t;
    cerr << endl;
}

template<class A, class B> inline void mini(A& a, B&& b) { if (b < a) a = b; }
template<class A, class B> inline void maxi(A& a, B&& b) { if (b > a) a = b; }

inline int pow2(int n) { return sizeof(int) * 8 - __builtin_clz(n); }

#ifdef DEBUG
# define D(...) __VA_ARGS__
#else
# define D(...)
#endif

#define LOG(x) D(cerr << #x ": " << x << "  ")
#define LOGN(x) D(LOG(x) << endl)
#define DUMP(x) D(cerr << #x ": ", __d(cerr, ALL(x)) << endl)
#define E(...) D(__e(__VA_ARGS__))

//end of templates

V< V<LL> > dp;
constexpr LL max_k = 1000000000000000001;
LL f(LL n) {
    return n*(n-1)/2;
}
LL dp_size(int n) {
    LL x = f(n) + 1;
    if(x % 2 == 0)
        --x;
    return x >> 1;
}
LL get_dp(int n, LL k) {
    if(f(n) < k)
        return 0;
    LL sz = dp_size(n);
    if(k > sz) {
        k = 2*sz-k;
        if(f(n) % 2)
            ++k;
    }
    if(SZ(dp[n]) <= k)
        return max_k;
    else
        return dp[n][k];
}

struct Tree {
    int sz;
    VI t;
    Tree(int n) : sz(1 << pow2(n)), t(sz << 1, 1) {
        t[sz] = 0;
        REPD(v, sz)
            t[v] = t[v<<1] + t[v<<1|1];
    }
    void set(int v) {
        v += sz;
        while(v) {
            --t[v];
            v >>= 1;
        }
    }
    int get(int x) {
        int v = 1;
        while(sz > v) {
            if(t[v<<1] < x)
                x -= t[v<<1], v = v<<1|1;
            else
                v <<= 1;
        }
        return v - sz;
    }
};

int main() 
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    LL n_in, k_in;
    cin >> n_in >> k_in;

    if(n_in % 4 > 1)
        return cout << "NIE", 0;

    dp.RS(n_in + 1);
    dp[0].EB(0);
    FOR(n, 1, n_in)
        dp[n].EB(1);
    FOR(n, 1, n_in-1) {
        LOGN(n);
        LL sum = 1;
        FOR(k, 1, dp_size(n+1)) {
            sum += get_dp(n, k);
            if(k > n)
                sum -= get_dp(n, k-n-1);
            if(sum < max_k)
                dp[n+1].EB(sum);
            else
                break;
        }
    }
    DUMP(dp);

    LL inw = f(n_in)/2;
    if(get_dp(n_in, inw) < k_in)
        return cout << "NIE\n", 0;
    cout << "TAK\n";

    Tree t(n_in+1);
    REPD(i, n_in) {
        if(!inw) {
            int v = t.get(1);
            cout << v << " ";
            t.set(v);
        } else
            FOR(act, LL(max(1ll, inw-f(i)+1)), n_in) {
                LL searched = inw - act + 1;
                auto g = get_dp(i, searched);
                if(g < k_in)
                    k_in -= g;
                else {
                    inw = searched;
                    int v = t.get(act);
                    cout << v << " ";
                    t.set(v);
                    break;
                }
            }
    }
    cout << nl;
}