#include<bits/stdc++.h> #define PII pair<int,int> #define f first #define s second #define VI vector<int> #define LL long long #define MP make_pair #define LD long double #define PB push_back #define ALL(V) V.begin(),V.end() #define abs(x) max((x),-(x)) #define PDD pair<LD,LD> #define VPII vector< PII > #define siz(V) ((int)V.size()) #define FOR(x, b, e) for(int x=b;x<=(e);x++) #define FORD(x, b, e) for(int x=b;x>=(e);x--) #define REP(x, n) for(int x=0;x<(int)(n);x++) #define mini(a,b) a=min(a,b) #define maxi(a,b) a=max(a,b) using namespace std; const int MXN = 250100; vector<LL> dp[MXN];//dp[n][k] = ile permutacji liczb 1...n mających k inwersji vector<bool> memo[MXN]; LL inf = 1.1e18; LL go(LL n, LL k) { if(k < 0)return 0; if(n == 0 && k == 0)return 1; if(n == 0)return 0; LL po2 = (LL)n * (n-1) / 2; if(k > po2)return 0; k = min(k, po2-k); if(k >= (int)memo[n].size())return inf; if(memo[n][k])return dp[n][k]; memo[n][k] = 1; FOR(i, 1, n) { int new_inv = i-1; if(k - new_inv < 0)break; // po break mało operacji (353551) dp[n][k] += go(n-1, k - new_inv); if(dp[n][k] >= inf) { dp[n][k] = inf; return inf; } } return dp[n][k]; } VI get(LL n, LL inv, LL kth) { VI V; FOR(_, 1, n) { LL maxinv = (n-_) * (n-_ - 1) / 2; FOR(i, max(1LL, inv-maxinv+1), n) // działa szybko bo max { LL ways = go(n-_, inv-(i-1)); if(ways < kth) { kth -= ways; } else { V.PB(i); inv -= (i-1); break; } } } return V; } #include<ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; VI repair(VI V) { ordered_set<int> S; FOR(i, 1, (int)V.size()) S.insert(i); VI V2; REP(i, V.size()) { int x = *S.find_by_order(V[i]-1); S.erase(x); V2.PB(x); } return V2; } int main() { LL n, k; cin>>n>>k; REP(i, 1000) { dp[i].resize(100); memo[i].resize(100); } FOR(i, 1000, n) { dp[i].resize(10); memo[i].resize(10); } LL pol = n * (n-1) / 4; if((n * (n-1)/2) % 2 != 0 || go(n, pol) < k) { puts("NIE"); return 0; } VI V = get(n, pol, k); V = repair(V); puts("TAK"); for(auto i : V) printf("%d ", i); puts(""); }
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 | #include<bits/stdc++.h> #define PII pair<int,int> #define f first #define s second #define VI vector<int> #define LL long long #define MP make_pair #define LD long double #define PB push_back #define ALL(V) V.begin(),V.end() #define abs(x) max((x),-(x)) #define PDD pair<LD,LD> #define VPII vector< PII > #define siz(V) ((int)V.size()) #define FOR(x, b, e) for(int x=b;x<=(e);x++) #define FORD(x, b, e) for(int x=b;x>=(e);x--) #define REP(x, n) for(int x=0;x<(int)(n);x++) #define mini(a,b) a=min(a,b) #define maxi(a,b) a=max(a,b) using namespace std; const int MXN = 250100; vector<LL> dp[MXN];//dp[n][k] = ile permutacji liczb 1...n mających k inwersji vector<bool> memo[MXN]; LL inf = 1.1e18; LL go(LL n, LL k) { if(k < 0)return 0; if(n == 0 && k == 0)return 1; if(n == 0)return 0; LL po2 = (LL)n * (n-1) / 2; if(k > po2)return 0; k = min(k, po2-k); if(k >= (int)memo[n].size())return inf; if(memo[n][k])return dp[n][k]; memo[n][k] = 1; FOR(i, 1, n) { int new_inv = i-1; if(k - new_inv < 0)break; // po break mało operacji (353551) dp[n][k] += go(n-1, k - new_inv); if(dp[n][k] >= inf) { dp[n][k] = inf; return inf; } } return dp[n][k]; } VI get(LL n, LL inv, LL kth) { VI V; FOR(_, 1, n) { LL maxinv = (n-_) * (n-_ - 1) / 2; FOR(i, max(1LL, inv-maxinv+1), n) // działa szybko bo max { LL ways = go(n-_, inv-(i-1)); if(ways < kth) { kth -= ways; } else { V.PB(i); inv -= (i-1); break; } } } return V; } #include<ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; VI repair(VI V) { ordered_set<int> S; FOR(i, 1, (int)V.size()) S.insert(i); VI V2; REP(i, V.size()) { int x = *S.find_by_order(V[i]-1); S.erase(x); V2.PB(x); } return V2; } int main() { LL n, k; cin>>n>>k; REP(i, 1000) { dp[i].resize(100); memo[i].resize(100); } FOR(i, 1000, n) { dp[i].resize(10); memo[i].resize(10); } LL pol = n * (n-1) / 4; if((n * (n-1)/2) % 2 != 0 || go(n, pol) < k) { puts("NIE"); return 0; } VI V = get(n, pol, k); V = repair(V); puts("TAK"); for(auto i : V) printf("%d ", i); puts(""); } |