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

using namespace std;

void fenwick_inc(vector<int>& tree, size_t index) {
	while (index < tree.size()) {
		++tree[index];
		index += index & (-index);
	}
}

int fenwick_query(const vector<int>& tree, size_t index) {
	int sum{};
	while (index > 0) {
		sum += tree[index];
		index -= index & (-index);
	}
	return sum;
}

int invcnt(const vector<int>& v) {
  vector<int> tree(v.size() + 1, 0);

  int inv = 0;
  for (size_t i = v.size() - 1; i < v.size(); --i) {
    inv += fenwick_query(tree, v[i] - 1);
    fenwick_inc(tree, v[i]);
  }

  return inv;
}

uint64_t factor(uint64_t i) {
  if (i == 0) return 1;
  return i * factor(i - 1);
}

int main() {
  ios::sync_with_stdio(false);

  uint64_t n, k;
  cin >> n >> k;

  vector<int> perm(n);
  for (uint64_t i = 0; i < perm.size(); ++i)
    perm[i] = i + 1;

  uint64_t stable{};
  for (uint64_t i = 0; i < factor(n); ++i) {
    auto rperm = vector<int>(perm.rbegin(), perm.rend());
    if (invcnt(perm) == invcnt(rperm)) {
      ++stable;
    }

    if (stable == k) {
      cout << "TAK\n";
      for (auto x : perm) {
        cout << x << ' ';
      }
      cout << '\n';
      return 0;
    }

    next_permutation(perm.begin(), perm.end());
  }

  cout << "NIE\n";

  return 0;
}