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
#include <iostream>
#include <vector>
#include <limits>

using namespace std;

typedef unsigned long long l;
typedef vector<l> v_l;
typedef vector<v_l> vv_l;


class C{
    private:
    vv_l perm;
    vv_l cum_perm;
    v_l max_perm_data;
    public:
    C(){
        l s, t1, t2, t3;
        max_perm_data.push_back(1);
        for(int i=1;i<22;i++){
            max_perm_data.push_back(max_perm_data[i-1] * i);
        }

        for(int n=0;n<3;n++){
            v_l temp;
            perm.push_back(temp);
            perm[n].push_back(1);
        }
        for(int n=3;n<250001;n++){
            v_l temp;
            perm.push_back(temp);
            for(int i=0;i<max_inv(n);i++){
                t1 = T(n-1, i);
                t2 = T(n, i-1);
                t3 = T(n-1, i-n);
                s = t1 + t2;
                if (s < t1 || s < t1){
                   break;
                }
                s -= t3;
                perm[n].push_back(s);
            }
        }
        for(int n=0;n<2;n++){
            v_l temp;
            cum_perm.push_back(temp);
            cum_perm[n].push_back(T(n, 0));
        }
        for(int n=2;n<250001;n++){
            v_l temp;
            cum_perm.push_back(temp);
            cum_perm[n].push_back(T(n, 0));
            for(int i=1;i<perm[n].size()+1;i++){
                t1 = max_perm(n, i-1);
                t2 = T(n, i);
                s = t1 + t2;
                if (s < t1 || s < t2){
                    break;
                }
                cum_perm[n].push_back(s);
            }
        }
    }
    l T(l n, l k){
        if (n > 250000) return 0;
        l M = max_inv(n);
        if (k > M) return 0;
        if (k > M/2) k = M - k;
        if (k >= perm[n].size()) return numeric_limits<l>::max();
        return perm[n][k];
    }

    l max_inv(l n){
        return (n * (n - 1)) / 2;
    }
    l max_perm(l n, l k){
        if (n > 250000) return 0;
        if (k > numeric_limits<l>::max()/2) return 0;
        l M = max_inv(n);
        if (k > M) return max_perm(n);
        if (k >= perm[n].size() && k <= M - perm[n].size()){
            return numeric_limits<l>::max();
        }
        l s, t1, t2;
        if (k >= cum_perm[n].size()){
            return numeric_limits<l>::max();
        }
        return cum_perm[n][k];
        // cum_perm[n][k]
        // t1 = T(n, k);
        // if (t1 == numeric_limits<l>::max()) return t1;
        // t2 = max_perm(n, k-1);
        // s = t1 + t2;
        // if (s < t1 || s < t2) return numeric_limits<l>::max();
        return s;
    }
    l max_perm(l n){
        if (n < 22) {
            return max_perm_data[n];
        }
        else{
            return numeric_limits<l>::max();
        }
    }

    l max_stable_perm(l n){
        if (n % 4 != 0 && n % 4 != 1) return 0;
        return T(n, max_inv(n) / 2);
    }
};

int main(){
    C c;
    l n, k;
    // IN:
    cin>>n>>k;
    // OUT: "NIE"
    l M = c.max_stable_perm(n);
    if (k > M){
        cout<<"NIE"<<endl;
        return 0;
    }
    // OUT: "TAK"
    cout<<"TAK"<<endl;
    l min_arg=0;
    l inversions=c.max_inv(n)/2;
    // PRINT FIRST min_arg ORDERED ELEMENTS
    for(min_arg=0;min_arg<n;min_arg++){
        if (c.T(n-min_arg-1, inversions) >= k) cout<<min_arg+1<<" ";
        else break;
    }
    v_l temp;
    // non trivial last part of sequence
    for(int i=min_arg;i<n;i++) temp.push_back(i+1);
    // ALG:
    for(int i=min_arg;i<n;i++){
        l j, left, now, j_min, j_max;
        j_min = 0;
        j_max = n-i;
        for(;;){
            j=(j_min+j_max)/2;
            now = c.T(n-i-1, inversions -  j);
			left = c.max_perm(n-i-1, inversions) - c.max_perm(n-i-1, inversions - j);
			if (left >= k){
				// move left
                j_max = j;
			}
            if (left + now >= k){
                k -= left;
                inversions -= j;
                break;
            }
			else{
				// move right
                j_min = j+1;
			}
        }
        cout<<temp[j];
        if(i != n-1)cout<<" ";
        temp.erase(temp.begin() + j);
    }
    cout<<endl;
}