#include <iostream> #include <vector> #include <algorithm> using namespace std; std::vector<int> gen(int n) { std::vector<int> v; for(int i = 1; i <= n; ++i) { v.push_back(i); } return v; } void print_vec(const std::vector<int> &v) { for(auto it = v.begin(); it != v.end(); ++it) { cout << *it << " "; } cout << endl; } template<class iterator_type> int count_inv(iterator_type b, iterator_type e) { int cnt = 0; for(auto it1 = b; it1 != e; ++it1) { for(auto it2 = it1 + 1; it2 != e; ++it2) { if(*it2 < *it1) { cnt++; } } } return cnt; } long long factorial(long long n) { long long r = 1; for (long long i = 2; i <= n; ++i) { r *= i; } return r; } bool is_stable(const std::vector<int> &v) { int cnt1 = count_inv(v.begin(), v.end()); int cnt2 = count_inv(v.rbegin(), v.rend()); int n = v.size(); // cout << ((cnt1 == cnt2) ? "+|" : "-|") << ((n * (n-1))/2) << "|" << cnt1 << " " << cnt2 << "|"; // print_vec(v); // if(cnt1 == cnt2) print_vec(v); return cnt1 == cnt2; } int main() { ios_base::sync_with_stdio(false); int n; long long k; cin >> n >> k; auto v = gen(n); vector<std::vector<int> > v2; if(is_stable(v)) { v2.push_back(v); } while(next_permutation(v.begin(), v.end())) { if(is_stable(v)) { v2.push_back(v); } } if(v2.size() >= k) { cout << "TAK" << endl; print_vec(v2[k - 1]); } else { 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 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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; std::vector<int> gen(int n) { std::vector<int> v; for(int i = 1; i <= n; ++i) { v.push_back(i); } return v; } void print_vec(const std::vector<int> &v) { for(auto it = v.begin(); it != v.end(); ++it) { cout << *it << " "; } cout << endl; } template<class iterator_type> int count_inv(iterator_type b, iterator_type e) { int cnt = 0; for(auto it1 = b; it1 != e; ++it1) { for(auto it2 = it1 + 1; it2 != e; ++it2) { if(*it2 < *it1) { cnt++; } } } return cnt; } long long factorial(long long n) { long long r = 1; for (long long i = 2; i <= n; ++i) { r *= i; } return r; } bool is_stable(const std::vector<int> &v) { int cnt1 = count_inv(v.begin(), v.end()); int cnt2 = count_inv(v.rbegin(), v.rend()); int n = v.size(); // cout << ((cnt1 == cnt2) ? "+|" : "-|") << ((n * (n-1))/2) << "|" << cnt1 << " " << cnt2 << "|"; // print_vec(v); // if(cnt1 == cnt2) print_vec(v); return cnt1 == cnt2; } int main() { ios_base::sync_with_stdio(false); int n; long long k; cin >> n >> k; auto v = gen(n); vector<std::vector<int> > v2; if(is_stable(v)) { v2.push_back(v); } while(next_permutation(v.begin(), v.end())) { if(is_stable(v)) { v2.push_back(v); } } if(v2.size() >= k) { cout << "TAK" << endl; print_vec(v2[k - 1]); } else { cout << "NIE" << endl; } return 0; } |