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

using namespace std;

bool dont_use[20] = {false};
int result[20];

int stage = 0;
long long k, counter, found;

bool isValid(int n) {
    int left, right;
    left = right = 0;
    for ( int i = 1; i <= n; i++ ) {
        for ( int j = i+1; j <= n; j++ )
            if ( result[i] > result[j] )
                left ++;
    }
    for ( int i = n; i > 0; i-- )
        for ( int j = i-1; j > 0; j-- )
            if (result[i] > result[j])
                right++;
    return left == right;
}


void generateAllPermutation(int n) {
    if ( found )
        return;
    bool last = true;
    stage++;
    int i;
    for ( i = 1; i <= n; i++ ) {
        if ( dont_use[i] == true )
            continue;
        last = false;
        dont_use[i] = true;
        result[stage] = i;
        generateAllPermutation(n);
        dont_use[i] = false;
    }
    if ( last && isValid(n) ) {
        counter++;
        if ( counter == k) {
            cout << "TAK" << endl;
            for ( int j = 1; j <= n; j++ )
                cout << result[j] << " ";
            cout << endl;
            found = true;
        }
    }
    stage--;
}


int main(int argc, const char * argv[]) {
    int n;
    cin >> n >> k;
    counter = 0;
    found = false;
    if ( n < 20 )
        generateAllPermutation(n);
    if ( !found )
        cout << "NIE" << endl;
    return 0;
}