#include <vector> #include <iostream> #include <algorithm> #include <set> #include <random> #include <cstdlib> #include <ctime> #include <limits> #include <unordered_map> #include <utility> using namespace std; struct pair_hash { template <class T1, class T2> std::size_t operator () (const std::pair<T1,T2> &p) const { auto h1 = std::hash<T1>{}(p.first); auto h2 = std::hash<T2>{}(p.second); // Mainly for demonstration purposes, i.e. works but is overly simple // In the real world, use sth. like boost.hash_combine return h1 ^ h2; } }; unordered_map<pair<int, long long>, long long, pair_hash> seen; long long countperm(long long n, long long left, long long limit) { if(left < 0){ return 0; } if(left == 0){ return 1; } if(n < 0){ return 0; } if(n == 0 and left != 0){ return 0; } if(left == 1){ return n - 1; } if(left > (n * (n - 1))/ 2ll){ return 0; } auto it = seen.find({n, left}); if(it != seen.end()){ return it->second; } long long ret = 0; for(int i = n - 1; i >= 0; i--){ long long cur = countperm(n - 1, left - i, limit - ret); if(cur > limit){ return limit + 1; } if(numeric_limits<long long>::max() - ret <= cur){ return (long long)(1e18) + 1; } ret += cur; } seen[{n, left}] = ret; return ret; } void findperm(long long n, long long l){ vector<int> ret; int j = n % 2; for(; j < n; j += 2){ long long left = (n * (n - 1)) / 4; long long cur = countperm(j, left, l); if( cur > l){ break; } } long long left = (n * (n - 1)) / 4; l --; long long total = 0; int sofar = 0; for(int i = 0; i < j; i++){ int z = 0; for(; z < j - i; z ++){ long long cur = countperm(j - i - 1, left - sofar - z, l - total + 1); if(total + cur > l) { break; } else { total += cur; } } sofar += z; ret.push_back(z); } reverse(ret.begin(), ret.end()); vector<int> retur; for(auto a : ret){ for(auto & b : retur){ if(b > a){ b++; } } retur.push_back(a + 1); } reverse(retur.begin(), retur.end()); //print result for(int i = 0; i < (n - j); i ++){ cout << i + 1 << " "; } for(auto a : retur){ cout << a + (n - j)<< " " ; } cout << endl; } int main(){ std::srand ( unsigned ( std::time(0) ) ); vector<int> all; int n; long long l; cin >> n; cin >> l; if((((n - 2) * (n - 1)) / 2) % 2 != (n % 2)){ if(n >= 24 or l <= countperm(n, ((n) * (n - 1))/ 4, 1000000000000000001ll)){ cout << "TAK" << endl; findperm(n, l); } else { cout << "NIE" << endl; } } else { cout << "NIE" << endl; } }
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 | #include <vector> #include <iostream> #include <algorithm> #include <set> #include <random> #include <cstdlib> #include <ctime> #include <limits> #include <unordered_map> #include <utility> using namespace std; struct pair_hash { template <class T1, class T2> std::size_t operator () (const std::pair<T1,T2> &p) const { auto h1 = std::hash<T1>{}(p.first); auto h2 = std::hash<T2>{}(p.second); // Mainly for demonstration purposes, i.e. works but is overly simple // In the real world, use sth. like boost.hash_combine return h1 ^ h2; } }; unordered_map<pair<int, long long>, long long, pair_hash> seen; long long countperm(long long n, long long left, long long limit) { if(left < 0){ return 0; } if(left == 0){ return 1; } if(n < 0){ return 0; } if(n == 0 and left != 0){ return 0; } if(left == 1){ return n - 1; } if(left > (n * (n - 1))/ 2ll){ return 0; } auto it = seen.find({n, left}); if(it != seen.end()){ return it->second; } long long ret = 0; for(int i = n - 1; i >= 0; i--){ long long cur = countperm(n - 1, left - i, limit - ret); if(cur > limit){ return limit + 1; } if(numeric_limits<long long>::max() - ret <= cur){ return (long long)(1e18) + 1; } ret += cur; } seen[{n, left}] = ret; return ret; } void findperm(long long n, long long l){ vector<int> ret; int j = n % 2; for(; j < n; j += 2){ long long left = (n * (n - 1)) / 4; long long cur = countperm(j, left, l); if( cur > l){ break; } } long long left = (n * (n - 1)) / 4; l --; long long total = 0; int sofar = 0; for(int i = 0; i < j; i++){ int z = 0; for(; z < j - i; z ++){ long long cur = countperm(j - i - 1, left - sofar - z, l - total + 1); if(total + cur > l) { break; } else { total += cur; } } sofar += z; ret.push_back(z); } reverse(ret.begin(), ret.end()); vector<int> retur; for(auto a : ret){ for(auto & b : retur){ if(b > a){ b++; } } retur.push_back(a + 1); } reverse(retur.begin(), retur.end()); //print result for(int i = 0; i < (n - j); i ++){ cout << i + 1 << " "; } for(auto a : retur){ cout << a + (n - j)<< " " ; } cout << endl; } int main(){ std::srand ( unsigned ( std::time(0) ) ); vector<int> all; int n; long long l; cin >> n; cin >> l; if((((n - 2) * (n - 1)) / 2) % 2 != (n % 2)){ if(n >= 24 or l <= countperm(n, ((n) * (n - 1))/ 4, 1000000000000000001ll)){ cout << "TAK" << endl; findperm(n, l); } else { cout << "NIE" << endl; } } else { cout << "NIE" << endl; } } |