#include <bits/stdc++.h> using namespace std; typedef long long int ll; const ll MAX = 250000; ll tab[MAX]; ll n, k; ll getInvCount() { set<ll> S; S.insert(tab[0]); ll invcount = 0; set<ll>::iterator it; for (ll i = 1; i < n; i++) { S.insert(tab[i]); it = S.upper_bound(tab[i]); invcount += distance(it, S.end()); } return invcount; } int main() { ios_base::sync_with_stdio(false); cin >> n >> k; if(n % 4 == 2 || n % 4 == 3) { cout << "NIE" << endl; return 0; } for(ll i = 0; i < n; i++) { tab[i] = i + 1; } do { if(getInvCount() == n * (n - 1) / 4) { if(--k == 0) { cout << "TAK" << endl; for(int i = 0; i < n; i++) { cout << tab[i] << " "; } cout << endl; return 0; } } } while (std::next_permutation(tab, tab + n)); cout << "NIE" << endl; return 0; }
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 | #include <bits/stdc++.h> using namespace std; typedef long long int ll; const ll MAX = 250000; ll tab[MAX]; ll n, k; ll getInvCount() { set<ll> S; S.insert(tab[0]); ll invcount = 0; set<ll>::iterator it; for (ll i = 1; i < n; i++) { S.insert(tab[i]); it = S.upper_bound(tab[i]); invcount += distance(it, S.end()); } return invcount; } int main() { ios_base::sync_with_stdio(false); cin >> n >> k; if(n % 4 == 2 || n % 4 == 3) { cout << "NIE" << endl; return 0; } for(ll i = 0; i < n; i++) { tab[i] = i + 1; } do { if(getInvCount() == n * (n - 1) / 4) { if(--k == 0) { cout << "TAK" << endl; for(int i = 0; i < n; i++) { cout << tab[i] << " "; } cout << endl; return 0; } } } while (std::next_permutation(tab, tab + n)); cout << "NIE" << endl; return 0; } |