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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <algorithm>
#include <cstdio>
#include <map>
#include <set>
#include <vector>

using namespace std;

using ULL = unsigned long long;

constexpr int MAXN = 250005;
constexpr ULL INF = 1000000000000000005ULL;

std::map<ULL, ULL> T[MAXN + 5];

int tree[2*MAXN];

// x > 0
// Add x to the tree.
void AddToTree(int x) {
  while (x < 2*MAXN) {
    ++tree[x];
    x += (x & -x);
  }
}

// x > 0
// How many y's smaller than or equal to x are in the tree already?
int ReadSmallerThan(int x) {
  int res = 0;
  while (x > 0) {
    res += tree[x];
    x -= (x & -x);
  }
  return res;
}

void PrintResult(const std::vector<int>& v) {
  for (auto x : v) {
    int l = 1, u = v.size(), r = -1;
    while (l <= u) {
      int m = (l + u) / 2;
      if (m - ReadSmallerThan(m) > x) {
        r = m;
        u = m - 1;
      } else {
        l = m + 1;
      }
    }
    printf("%d ", r);
    AddToTree(r);
  }
  printf("\n");
}

ULL Add(const ULL a, const ULL b) {
  if (a + b >= INF) { return INF;
  }
  return a + b;
}

void PreCompute() {
  T[0][0] = 1;
  T[1][0] = 1;
  ULL inf_boundary = INF;
  for (int i = 1; i < MAXN; ++i) {
    T[i].erase(T[i].lower_bound(inf_boundary), T[i].end());
    for (const auto& entry : T[i]) {
      const auto inversions = entry.first;
      const auto number = entry.second;
      for (int j = 0; j < i + 1 && inversions + j <= inf_boundary; ++j) {
        T[i + 1][inversions + j] = Add(T[i + 1][inversions + j], number);
        if (T[i + 1][inversions + j] == INF) {
          inf_boundary = std::min(inf_boundary, inversions + j);
          break;
        }
      }
    }
  }
}

ULL Inversions(int n, ULL k) {
  ULL possible = ((ULL) n * (ULL)(n - 1)) / 2;
  if (k > possible || k < 0) {
    return 0;
  }
  if (2 * k >= possible) {
    k = possible - k;
  }
  if (T[n].find(k) == T[n].end()) {
    return INF;
  }
  return T[n][k];
}

void Calc() {
  int n;
  ULL k;
  scanf("%d%llu", &n, &k);
  if (n % 4 == 2 || n % 4 == 3) {
    printf("NIE\n");
    return;
  }
  ULL inversions =
      ((ULL) n * (ULL) (n - 1)) / 4;
  std::vector<int> result;
  for (int i = 0; i < n; ++i) {
    int j;
    const auto sensible_inversions =
        (n - i - 1 > 0) ? ((ULL) (n - i - 1) * ((ULL) (n - i - 2)) / 2) : 0;
    if (sensible_inversions >= inversions) {
      j = 0;
    } else {
      if (inversions - sensible_inversions < (ULL) (n - i)) {
        j = inversions - sensible_inversions;
      } else {
        j = n - i;
      }
    }
    for (; j < n - i; ++j) {
      if (inversions < (ULL) j) {
        printf("NIE\n");
        return;
      }
      const auto value = Inversions(n - i - 1, inversions - j);
      if (value >= k) {
        result.push_back(j);
        inversions -= j;
        break;
      } else {
        k -= value;
      }
    }
    if (result.size() < i + 1) {
      printf("NIE\n");
      return;
    }
  }

  if (inversions == 0) {
    printf("TAK\n");
    PrintResult(result);
  } else {
    printf("NIE\n");
  }
}

int main() {
  PreCompute();
  int s = 0;
  for (const auto& t : T) {
    s += t.size();
  }
  Calc();
  return 0;
}