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

unsigned int factorial(unsigned int n)
{
	unsigned int ret = 1;
	for (unsigned int i = 1; i <= n; ++i)
		ret *= i;
	return ret;
}

template<class BidirectionalIterator>
int inversionCount(BidirectionalIterator begin, BidirectionalIterator end)
{
	int count = 0;

	for (auto first = begin; first != end; ++first)
	{
		for (auto second = first + 1; second != end; ++second)
		{
			if (begin != second && *first > *second)
			{
				count++;
			}
		}
	}

	return count;
}

bool isStable(const std::vector<int>& v)
{
	return inversionCount(v.begin(), v.end()) == inversionCount(v.rbegin(), v.rend());
}

void process()
{
	int size, order;
	std::cin >> size >> order;

	std::vector<int> permutation;
	permutation.resize(size);

	for (int i = 0; i < size; i++)
	{
		permutation[i] = i + 1;
	}

	auto startingPermutation = permutation;
	int count = 0;

	for (;;)
	{
		if (isStable(permutation))
		{
			count++;
			if (order == count)
			{
				std::cout << "TAK" << std::endl;

				for (auto element : permutation)
				{
					std::cout << element << " ";
				}

				return;
			}
		}

		std::next_permutation(permutation.begin(), permutation.end());

		if (startingPermutation == permutation)
		{
			break;
		}
	}

	std::cout << "NIE" << std::endl;
}

int main()
{
	process();
    return 0;
}