#include <cstdio> #include <numeric> #include <memory> #include <vector> #define INF 1000000000000000001LL using namespace std; inline long long get_count(const vector<vector<long long>>& _count, int n, long long inv_count) { const vector<long long>& __count = _count[n]; if (inv_count < __count.size()) { return __count[inv_count]; } long long all = n * (long long) (n-1) / 2; if (all < inv_count) { return 0; } if (all - inv_count < __count.size()) { return __count[all - inv_count]; } return INF; } long long count_count = 0; vector<vector<long long>> count(int n) { vector<vector<long long>> result; result.push_back(vector<long long>(1, 1)); for (int i = 1; i <= n; i++) { long long all = i * (long long) (i-1) / 2; const vector<long long>& prev = result[i - 1]; vector<long long> row; long long last = 0; for (int k = 0; k <= all; k++) { last += (k < prev.size() ? prev[k] : 0) - ((k - i) >= 0 ? prev[k - i] : 0); if (last >= INF) { break; } row.push_back(last); count_count++; } result.push_back(std::move(row)); } return result; } vector<vector<long long>> pref_sum(const vector<vector<long long>>& _count) { vector<vector<long long>> result; for (const vector<long long>& row : _count) { vector<long long> ps_row; long long ps = 0; for (long long cell : row) { ps += cell; if (ps >= INF) { break; } ps_row.push_back(ps); } result.push_back(std::move(ps_row)); } return result; } long long get_pref_sum(const vector<vector<long long>>& _pref_sum, int n, long long inv_count){ const vector<long long>& __ps = _pref_sum[n]; return (inv_count < __ps.size()) ? __ps[inv_count] : 0; } pair<int, long long> find_min_bound(int k, const vector<vector<long long>>& _count, int n, long long inv_count) { //printf("find_min_bound %d %d %lld\n", k, n, inv_count); long long prevps = 0; long long newps = get_count(_count, n, inv_count); int step = 0; while (newps < k) { step++; long long val = get_count(_count, n, inv_count - step); prevps = newps; newps += val; } return make_pair(step, prevps); } vector<int> trans(vector<int> a) { for (int i = a.size() - 1; i >=0 ;--i) { for (int j = i + 1; j < a.size(); ++j) { if (a[j] >= a[i]) { a[j]++; } } } return a; } vector<int>* get_kth_stable_inverse(int n, long long k) { long long all = n * (long long) (n-1) / 2; if (all % 2) { return NULL; } //printf("count\n"); vector<vector<long long>> _count = count(n); //vector<vector<long long>> _pref_sum = pref_sum(_count); /*printf("_________\n"); for (const vector<long long>& vec : _count) { for (long long v : vec) { printf("%lld ", v); } printf("\n"); } printf("_________\n");*/ long long m = all / 2; if (k > get_count(_count, n, m)) { return NULL; } vector<int> step_result; long long inv_count = m; //printf("for\n"); for (int i = 0; i < n - 1; i++) { pair<int, long long> step_prevps = find_min_bound(k, _count, n - 1 - i, inv_count); //printf("find_min_bound %d %lld\n", step_prevps.first, step_prevps.second); step_result.push_back(step_prevps.first); k -= step_prevps.second; inv_count -= step_prevps.first; } step_result.push_back(0); //printf("return\n"); return new vector<int>(trans(step_result)); } int main() { int n; long long k; scanf("%d%lld", &n, &k); vector<int>* solution = get_kth_stable_inverse(n, k); //printf("%lld\n", INF); if (solution) { printf("TAK\n"); for (int el : *solution) { printf("%d ", el + 1); } printf("\n"); } else { printf("NIE\n"); } 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 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 145 146 147 148 149 150 151 152 153 | #include <cstdio> #include <numeric> #include <memory> #include <vector> #define INF 1000000000000000001LL using namespace std; inline long long get_count(const vector<vector<long long>>& _count, int n, long long inv_count) { const vector<long long>& __count = _count[n]; if (inv_count < __count.size()) { return __count[inv_count]; } long long all = n * (long long) (n-1) / 2; if (all < inv_count) { return 0; } if (all - inv_count < __count.size()) { return __count[all - inv_count]; } return INF; } long long count_count = 0; vector<vector<long long>> count(int n) { vector<vector<long long>> result; result.push_back(vector<long long>(1, 1)); for (int i = 1; i <= n; i++) { long long all = i * (long long) (i-1) / 2; const vector<long long>& prev = result[i - 1]; vector<long long> row; long long last = 0; for (int k = 0; k <= all; k++) { last += (k < prev.size() ? prev[k] : 0) - ((k - i) >= 0 ? prev[k - i] : 0); if (last >= INF) { break; } row.push_back(last); count_count++; } result.push_back(std::move(row)); } return result; } vector<vector<long long>> pref_sum(const vector<vector<long long>>& _count) { vector<vector<long long>> result; for (const vector<long long>& row : _count) { vector<long long> ps_row; long long ps = 0; for (long long cell : row) { ps += cell; if (ps >= INF) { break; } ps_row.push_back(ps); } result.push_back(std::move(ps_row)); } return result; } long long get_pref_sum(const vector<vector<long long>>& _pref_sum, int n, long long inv_count){ const vector<long long>& __ps = _pref_sum[n]; return (inv_count < __ps.size()) ? __ps[inv_count] : 0; } pair<int, long long> find_min_bound(int k, const vector<vector<long long>>& _count, int n, long long inv_count) { //printf("find_min_bound %d %d %lld\n", k, n, inv_count); long long prevps = 0; long long newps = get_count(_count, n, inv_count); int step = 0; while (newps < k) { step++; long long val = get_count(_count, n, inv_count - step); prevps = newps; newps += val; } return make_pair(step, prevps); } vector<int> trans(vector<int> a) { for (int i = a.size() - 1; i >=0 ;--i) { for (int j = i + 1; j < a.size(); ++j) { if (a[j] >= a[i]) { a[j]++; } } } return a; } vector<int>* get_kth_stable_inverse(int n, long long k) { long long all = n * (long long) (n-1) / 2; if (all % 2) { return NULL; } //printf("count\n"); vector<vector<long long>> _count = count(n); //vector<vector<long long>> _pref_sum = pref_sum(_count); /*printf("_________\n"); for (const vector<long long>& vec : _count) { for (long long v : vec) { printf("%lld ", v); } printf("\n"); } printf("_________\n");*/ long long m = all / 2; if (k > get_count(_count, n, m)) { return NULL; } vector<int> step_result; long long inv_count = m; //printf("for\n"); for (int i = 0; i < n - 1; i++) { pair<int, long long> step_prevps = find_min_bound(k, _count, n - 1 - i, inv_count); //printf("find_min_bound %d %lld\n", step_prevps.first, step_prevps.second); step_result.push_back(step_prevps.first); k -= step_prevps.second; inv_count -= step_prevps.first; } step_result.push_back(0); //printf("return\n"); return new vector<int>(trans(step_result)); } int main() { int n; long long k; scanf("%d%lld", &n, &k); vector<int>* solution = get_kth_stable_inverse(n, k); //printf("%lld\n", INF); if (solution) { printf("TAK\n"); for (int el : *solution) { printf("%d ", el + 1); } printf("\n"); } else { printf("NIE\n"); } return 0; } |