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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <cstdio>
#include <algorithm>
#include <set>
#include <vector>
using namespace std;
#define LL long long
#define PB push_back
#define MAX_N 250020
LL OVER_MAX;

LL N, K;

vector<LL> inversions[MAX_N];
vector<int> score;
set<int> available_values;

int T[2000000];
int S;
void add(int x) {
  x += S;
  while (x) {
    T[x]++;
    x /= 2;
  }
}
int sum(int x, int y) {
  x += S; y += S;
  int W = T[x];
  if (x != y) W += T[y];
  while (x / 2 != y / 2) {
    if (x % 2 == 0) W += T[x + 1];
    if (y % 2 == 1) W += T[y - 1];
    x /= 2;
    y /= 2;
  }
  return W;
}

void count_inversions();
LL inv(int n, LL k);
void init();
void input();
bool solve();

int main() {
  init();
  input();
  if (solve()) {
    printf("TAK\n");
    for (int i = 0; i < score.size(); ++i) {
      printf("%d ", score[i]);
    }
    printf("\n");
  } else {
    printf("NIE\n");
  }
}

int count_skipped(int x) {
  return x - 1 - sum(1, x);
}

int binary(int a, int b, int M) {
  if (a == b) return a;
  int c = (a + b) / 2;
  int skipped = count_skipped(c);

  if (skipped >= M) {
    return binary(a, c, M);
  }
  return binary(c + 1, b, M);
}

bool solve() {
  LL N_MAX_INVS = N * (N - 1) / 2;
  LL INV_COUNT = N_MAX_INVS / 2;

  if (N_MAX_INVS - INV_COUNT != INV_COUNT) {
    return false;
  }
  if (inv(N, INV_COUNT) < K) {
    return false;
  }

  for (int i = 1; i <= N; ++i) {
    available_values.insert(i);
  }

  LL inv_in_score = 0;
  LL values_left;
  int start_idx;
  LL lower_skipped;
  LL invs_needed;
  LL max_perms;
  LL to_skip;
  score.clear();
  while (!available_values.empty()) {
    values_left = available_values.size() - 1;
    max_perms = values_left * (values_left - 1) / 2;
    to_skip = INV_COUNT - inv_in_score - max_perms;
    if (to_skip > 0)
      start_idx = binary(1, N, to_skip);
    else start_idx = 0;

    auto it=available_values.lower_bound(start_idx);
    int val = *it;
    lower_skipped = count_skipped(val);

    for (; it != available_values.end(); it++) {
      invs_needed = INV_COUNT - (inv_in_score + lower_skipped);
      LL invs = inv(values_left, invs_needed);
      if (invs < K) {
        K -= invs;
      } else {
        val = *it;
        score.PB(val);
        add(val);
        inv_in_score += lower_skipped;
        available_values.erase(it);
        break;
      }
      ++lower_skipped;
    }
  }

  return true;
}

void init() {
  OVER_MAX = 1000 * 1000 * 1000;
  OVER_MAX *= OVER_MAX;
  OVER_MAX++;
  count_inversions();
}

void input() {
  scanf("%lld%lld", &N, &K);
  S = 1;
  while (S <= (N+1)) S *= 2;
}

void count_inversions() {
  inversions[1].PB(1);
  for (LL i = 2; i <= MAX_N; ++i) {
    for (LL j = 0; j <= i * (i - 1) / 2; ++j) {
      LL val = 0;
      for (LL l = 0; l < i && j - l >= 0; l++) {
        val += inv(i - 1, j - l);
        if (val >= OVER_MAX) {
          val = OVER_MAX;
          break;
        }
      }
      inversions[i].PB(val);
      if (val >= OVER_MAX) {
        break;
      }
    }
  }
}

LL inv(int n, LL k) {
  if (k == 0) {
    return 1;
  }
  LL max_perms = ((LL)n)*  (n - 1) / 2;
  if (k > max_perms) {
    return 0;
  }
  if (k > max_perms / 2) {
    return inv(n, max_perms - k);
  }
  if (k >= inversions[n].size()) {
    return OVER_MAX;
  }
  return inversions[n][k];
}