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
#include <stdio.h>
#include <stdlib.h>

long long int perm_counter = 0;
int found = 0;

void permute(int arr[], int index, int k, int size){
     int i, j, m, l, t;
    
	if(index >= size - 1){
        int greater_counter = 0;
        int less_counter = 0;
       
        for(i = 0; i <= size - 2; i++){
            for(j = i+1; j <= size - 1; j++){
                if(arr[i] > arr[j]) greater_counter++;
                else less_counter++;
            }
        }
        
        if(greater_counter == less_counter) perm_counter++;
        
        if(perm_counter == k){
            printf("TAK\n");
             for(m = 0; m < size; m++){
                printf("%d",arr[m]);
                if (m < size - 1) printf(" ");
            }
            found = 1;
        }
        
        return;
    }

    for(l = index; l < size; l++){
        t = arr[index];
        arr[index] = arr[l];
        arr[l] = t;

        if(!found) permute(arr, index+1, k, size);

        t = arr[index];
        arr[index] = arr[l];
        arr[l] = t;
    }
}
    
int main()
{
    int n, c, i;
    long long int k;
	scanf("%d", &n);
	scanf("%d", &k);
	int perm_tab[n];
	for(i = 0; i < n; i++){
		perm_tab[i] = i+1;
	}
    
	permute(perm_tab, 0, k, n);
	
	if(!found) printf("NIE\n");
	
    return 0;
}