#include <iostream> #include <vector> #include <algorithm> #include <fstream> #include <numeric> using namespace std; typedef unsigned long long ull; template<typename Iter> ull calculate_inverse(Iter begin, Iter end) { ull result = 0; for (; begin != end; ++begin) { for (auto cpy = begin + 1; cpy != end; ++cpy) { if (*cpy < *begin) { ++result; } } } return result; } void solve_brut(ull n, ull k) { vector<int> v(n); std::iota(v.begin(), v.end(), 1); ull count = 0; const ull inversion_count = (n*n - n) / 4; do { if (calculate_inverse(v.begin(), v.end()) == (n*n - n) / 4) { ++count; if(count == k) { cout << "TAK" << endl; for(int i = 0; i < n; ++i) { cout << v[i] << ' '; } cout << endl; return; } } } while (std::next_permutation(v.begin(), v.end())); } ull fact(ull n) { ull ret = 1; for (ull i = 1; i <= n; ++i) ret *= i; return ret; } int main() { ios::sync_with_stdio(false); ull n, k; cin >> n >> k; if ((n*n - n) % 4 != 0) { cout << "NIE"; } else if (n <= 20 && fact(n) < k) { cout << "NIE"; } else { solve_brut(n, k); } 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 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 | #include <iostream> #include <vector> #include <algorithm> #include <fstream> #include <numeric> using namespace std; typedef unsigned long long ull; template<typename Iter> ull calculate_inverse(Iter begin, Iter end) { ull result = 0; for (; begin != end; ++begin) { for (auto cpy = begin + 1; cpy != end; ++cpy) { if (*cpy < *begin) { ++result; } } } return result; } void solve_brut(ull n, ull k) { vector<int> v(n); std::iota(v.begin(), v.end(), 1); ull count = 0; const ull inversion_count = (n*n - n) / 4; do { if (calculate_inverse(v.begin(), v.end()) == (n*n - n) / 4) { ++count; if(count == k) { cout << "TAK" << endl; for(int i = 0; i < n; ++i) { cout << v[i] << ' '; } cout << endl; return; } } } while (std::next_permutation(v.begin(), v.end())); } ull fact(ull n) { ull ret = 1; for (ull i = 1; i <= n; ++i) ret *= i; return ret; } int main() { ios::sync_with_stdio(false); ull n, k; cin >> n >> k; if ((n*n - n) % 4 != 0) { cout << "NIE"; } else if (n <= 20 && fact(n) < k) { cout << "NIE"; } else { solve_brut(n, k); } return 0; } |