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
#include <iostream>
#include <vector>
#include <list>
#include <stdint.h>
#include <assert.h>

using namespace std;

const uint64_t max_k = 1000000000000000000ULL;

static vector<vector<uint64_t>> I;

static uint64_t count(uint32_t n, uint64_t k);

void precompute(uint32_t n)
{
	if (I.empty()) {
		I.push_back({});
		I.push_back({});
		I.push_back({});
		I.push_back({1,2});
	}
	for (uint32_t i = I.size(); i <= n; i++) {
		uint64_t K = i*(i-1)/4 + 1;
		I.push_back({});
		vector<uint64_t> &new_row = I[i];
		uint64_t sum = 0;
		uint32_t j;
		for (j = 0; j < K; j++) {
			sum += count(i-1, j);
			if (sum > max_k) break;
			if (j >= i) sum -= count(i-1, j-i);
			new_row.push_back(sum);
		}
	}
}

uint64_t count(uint32_t n, uint64_t k)
{
	if (k==0) return 1;
	if (k==1) return n == 0 ? 0 : n - 1;
	uint64_t x = uint64_t(n)*(n-1)/2;
	if (k > x) return 0;
	if (k > x/2) k = x - k;
	assert(k <= x/2);
	precompute(n);
	auto &R = I[n];
	return k < R.size() ? R[k] : max_k + 1;
}

vector<int> kth_stable_n_permutation(uint32_t n, uint64_t k)
{
	vector<int> R;
	uint64_t pairs = uint64_t(n)*(n-1)/2;
	if (pairs % 2 != 0) return {};
	uint64_t i = pairs/2;
	vector<int> A;

	bool remove = false;
	uint32_t remove_from = 0, remove_to = 0;

	for (uint32_t j=1; j<=n; j++) A.push_back(j);
	uint32_t N = n;

	while (N) {
		assert(A.size() == N + (remove ? remove_to - remove_from + 1 : 0));
		uint32_t j_end = N;
		if (i + 1 < j_end) j_end = i + 1;
		uint64_t x = uint64_t(N-1)*(N-2)/2;
		uint32_t j = 0;
		if (i > x) j = i - x;
		for (; j < j_end; j++) {
			uint64_t dk = count(N-1, i-j);
			if (dk >= k) {
				if (!remove) {
					R.push_back(A[j]);
					remove_from = remove_to = j;
					remove = true;
				} else {
					uint32_t J = j;
					if (j >= remove_from) J += remove_to - remove_from + 1;
					assert(J < A.size());
					R.push_back(A[J]);
					if (J == remove_from - 1) {
						remove_from = J;
					} else if (J == remove_to + 1) {
						remove_to = J;
					} else {
						A.erase(A.begin() + remove_from, A.begin() + remove_to + 1);
						remove_from = remove_to = j;
					}
				}
				N--;
				i -= j;
				break;
			}
			k -= dk;
		}
		if (j == j_end) return {};
	}
	assert(R.size() == n);
	return R;
}

int main()
{
	uint32_t n;
	uint64_t k;
	cin >> n >> k;
	assert(1 <= n);
	assert(n <= 250000);
      	assert(1 <= k);
      	assert(k <= max_k);

	vector<int> P = kth_stable_n_permutation(n, k);
	if (P.empty()) {
		cout << "NIE" << endl;
	} else {
		cout << "TAK" << endl;
		for (size_t i = 0; i < P.size()-1; i++) {
			cout << P[i] << " ";
		}
		cout << P.back() << endl;
	}
	return 0;
}