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 "bits/stdc++.h"

using namespace std;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

using ll = long long;
using ld = long double;

constexpr ll Pot(ll a, int p) {
  return (p == 0) ? 1 : a * Pot(a, p - 1);
}

constexpr ll C2(ll n) {
  return (n * (n - 1)) / 2;
}

constexpr int nax = 250 * 1000 + 105;
constexpr ll infty = Pot(10ll, 18) + 105;
constexpr int mod = 1000 * 1000 * 1000 + 7;

ll length[nax];
vector<ll> prefix[nax];

ll CntPerm(int n, ll k) {
  assert(0 <= n and n < nax);
  if (k < 0) return 0;
  if (k > length[n]) return 0;
  const int ps = (int) prefix[n].size();
  if (k < ps) return prefix[n][k];
  if (length[n] - k < ps) return prefix[n][length[n] - k];
  return infty;
}

void Preprocessing() {
  length[0] = 0;
  prefix[0].push_back(1ll);
  length[1] = 0;
  prefix[1].push_back(1ll);
  for (int n = 2; n < nax; n++) {
    length[n] = C2(n);
    ll sum = 0ll;
    const int ps = (int) prefix[n - 1].size();
    for (int i = 0; i + 1 < ps + n; i++) {
      const int subtract = i - n;
      if (subtract >= 0) {
        sum -= prefix[n - 1][subtract];
      }
      if (i < ps) {
        sum += prefix[n - 1][i];
      }
      if (sum >= infty) {
        sum = infty;
      }
      prefix[n].push_back(sum);
      if (sum >= infty) {
        break;
      }
    }
  }
}

int cnt[nax * 4];

void Init_(int w, int p, int k) {
  cnt[w] = k - p + 1;
  if (p < k) {
    Init_(w * 2, p, (p + k) / 2);
    Init_(w * 2 + 1, (p + k) / 2 + 1, k);
  }
}

int FetchKth_(int w, int p, int k, int x) {
  assert(0 <= x and x < cnt[w]);
  cnt[w]--;
  if (p == k) {
    assert(cnt[w] == 0);
    return p;
  }
  if (x < cnt[w * 2]) {
    return FetchKth_(w * 2, p, (p + k) / 2, x);
  } else {
    return FetchKth_(w * 2 + 1, (p + k) / 2 + 1, k, x - cnt[w * 2]);
  }
}

void Init(int n) {
  Init_(1, 1, n);
}

int FetchKth(int k, int n) {
  return FetchKth_(1, 1, n, k);
}

vector<int> Algos(int n, ll inv, ll k) {
  assert(k >= 0);
  if (CntPerm(n, inv) <= k) {
    throw "Nie ma wystarczająco dużo permutacji.";
  }
  Init(n);
  vector<int> result;
  for (int i = 1; i <= n; i++) {
    debug() << imie(i);
    // inv - pos <= length[n - i]
    // pos >= inv - length[n - i]
    // inv - length[n - i] <= pos
    ll min_pos = max(0ll, inv - length[n - i]);
    bool found = false;
    while (min_pos <= n - i) {
      const ll ile = CntPerm(n - i, inv - min_pos);
      if (ile > k) {
        result.push_back(FetchKth(min_pos, n));
        found = true;
        inv -= min_pos;
        break;
      } else {
        k -= ile;
        min_pos++;
      }
    }
    assert(found);
  }
  assert(k == 0);
  return result;
}

int main() {
  Preprocessing();
  try {
    int n;
    ll k;
    scanf("%d%lld", &n, &k);
    const ll all_inv = C2(n);
    if (all_inv % 2 == 1) {
      throw "C(n, 2) nieparzyste - nie istnieje żadna stabilna permutacja.";
    }
    vector<int> result = Algos(n, all_inv / 2, k - 1);
    assert((int) result.size() == n);
    printf("TAK\n");
    for (int i = 0; i < n; i++) {
      if (i) printf(" ");
      printf("%d", result[i]);
    }
    printf("\n");
  } catch (const char* error) {
    debug() << imie(error);
    printf("NIE\n");
  }
  return 0;
}