#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
vector<int> V;
const int MAXN = 250*1000+5;
int Tree[MAXN];
long long n;
unsigned long long liczba[22] = {0, 1, 1, 2, 6, 22, 101, 573, 3836, 29228, 250749, 2409581,
25598186, 296643390, 3727542188, 50626553988, 738680521142, 11501573822788,
190418421447330, 3344822488498265, 62119523114983224, 1214967840930909302};
void chTree(int node, int v)
{
while (node <= n)
{
Tree[node] += v;
node += (node&(-node));
}
}
int read(int node)
{
int res = 0;
while (node)
{
res += Tree[node];
node -= (node&(-node));
}
return res;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long k;
cin >> n >> k;
if (n <= 21)
{
if (k > liczba[n])
{
cout << "NIE\n";
return 0;
}
}
long long INV = (n*n-n)/4;
if ((n*n-n)%4)
{
cout << "NIE\n";
return 0;
}
cout << "TAK\n";
for (int i = 1; i <= n; i++) V.push_back(i);
int ctr = 0;
do {
int tmp = 0;
for (int v : V)
{
tmp += read(n) - read(v);
chTree(v, 1);
}
if (tmp == INV) ctr++;
if (ctr == k)
{
for (int v : V) cout << v << " ";
return 0;
}
for (int v : V) chTree(v, -1);
} while(next_permutation(V.begin(), V.end()));
}
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 | #include <iostream> #include <vector> #include <algorithm> #include <cmath> using namespace std; vector<int> V; const int MAXN = 250*1000+5; int Tree[MAXN]; long long n; unsigned long long liczba[22] = {0, 1, 1, 2, 6, 22, 101, 573, 3836, 29228, 250749, 2409581, 25598186, 296643390, 3727542188, 50626553988, 738680521142, 11501573822788, 190418421447330, 3344822488498265, 62119523114983224, 1214967840930909302}; void chTree(int node, int v) { while (node <= n) { Tree[node] += v; node += (node&(-node)); } } int read(int node) { int res = 0; while (node) { res += Tree[node]; node -= (node&(-node)); } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long k; cin >> n >> k; if (n <= 21) { if (k > liczba[n]) { cout << "NIE\n"; return 0; } } long long INV = (n*n-n)/4; if ((n*n-n)%4) { cout << "NIE\n"; return 0; } cout << "TAK\n"; for (int i = 1; i <= n; i++) V.push_back(i); int ctr = 0; do { int tmp = 0; for (int v : V) { tmp += read(n) - read(v); chTree(v, 1); } if (tmp == INV) ctr++; if (ctr == k) { for (int v : V) cout << v << " "; return 0; } for (int v : V) chTree(v, -1); } while(next_permutation(V.begin(), V.end())); } |
English