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
#include <vector>
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <cstring>

using namespace std;

const int N = 250000;
long long infty = 1000000000000000001ll;
vector<long long> InwNo[N+1];
bool broken[N+1];
//bool is[N+1];
const int bug=46342;
vector<int> result;

const int BASE = 1 << 18;
int tree[2 * BASE];

void init(int n)
{
    memset(tree, 0, sizeof(tree));
    for(int i=1; i<=n; i++)
        tree[BASE+i] = 1;
    for(int i = BASE - 1; i > 0; i--)
        tree[i] = tree[2*i] + tree[2*i+1];
}

int find(int j)
{
    int i = 1;
    while(i < BASE) {
        i *= 2;
        if (tree[i] < j) {
            j -= tree[i];
            i++;
        }
    }
    return i - BASE;
}

void remove(int i)
{
    i += BASE;
    while(i>0) {
        tree[i]--;
        i /= 2;
    }
}

/*
int find(int j) {
    int i=0;
    int l=0;
    while(i < j) {
        ++l;
        if(is[l]) ++i;
    }
    return l;
}
*/

int main()
{
    InwNo[0].push_back(1);
    InwNo[1].push_back(1);
    fill(broken,broken+(N+1),false);

    int n; long long k;
    cin >> n >> k;

    for (int i = 2; i < n; ++i) {
        long long sum = 0;
        long long K=min(((long long)n)*(n-1)/4, ((long long) i)*(i-1)/2);
        for (long long j = 0; j <= K; ++j) {
            if(InwNo[i-1].size() > j)  sum += InwNo[i-1][j];
            else
                if(broken[i-1]) { broken[i]=true;  break; }
            if (j >= i && InwNo[i-1].size() > j-i) { sum -= InwNo[i-1][j-i];  }
            if (sum >= infty) { broken[i]=true;  break;  }
            InwNo[i].push_back(sum);
        }
    }
    init(n);

    //for(int i=20; i<22; ++i) {
    //    for(int j=0; j<InwNo[i].size(); ++j) cout << InwNo[i][j] << " ";
    //    cout << "size: " << InwNo[i].size() << endl;
    //}


    long long inw=((long long) n)*(n-1)/2;
    if(inw % 2) {
        cout << "NIE" << endl;
        return 0;
    }
    inw /=2;
    //cout << "szukam " << inw << " inwersji" << endl;

    //fill(is,is+(N+1),true);
    for(int i=n; i > 0; --i) { // po pozycjach
        long long totalinw=((long long) (i-1))*(i-2)/2;
        // inw-inwj <= totalinw+1 <=> j-1 >= inw-totalinw-1 <=> j >= int-totalinw
        for(long long j=max(1LL, inw-totalinw); j<=i; ++j) { // co na i-tej pozycji
            //cout << "runda " << i <<  ", rozważam " << j << "-ty el : ";
            long long ile;
            long long inwj=j-1;
            //cout << inw << ", " << inwj << ", " << k << endl;
            if( inw < inwj ) {
                cout << "NIE" << endl;
                return 0;
            }
            long long deltainw=inw-inwj;
            if (deltainw > totalinw) ile=0;
            else {
                if(totalinw-deltainw < deltainw) deltainw=totalinw-deltainw;
                if( InwNo[i-1].size() > deltainw )  ile=InwNo[i-1][deltainw];
                else {
                    if( deltainw > totalinw ) ile=0;
                    else ile=infty;
                }
            }
            //out << "j=" << j << " k=" << k << " ile=" << ile << " deltainw=" << deltainw << endl;
            if( k > ile) { k-=ile; //cout << "przeskakuje " << ile << endl;
                if(j==n) { cout << "NIE" << endl; return 0; }
                }
            else {
                int l=find(j);
                result.push_back(l);
                remove(l);
                //is[l]=false;
                inw-=inwj;
                break;
            }
        }

    }
    printf("TAK\n");
    for(int i=0; i<n; ++i) printf("%d ",result[i]);
    printf("\n");
    return 0;
}