// n ^ 3 #include <bits/stdc++.h> using namespace std; #define LL long long const int mxn = 3e5; const LL MOD = 1e9 + 7LL; const LL MX = 1e18; int n; LL k; map < pair <int, int>, LL> dp; LL f(int n, int k) { if(n == 0) return 0; if(k == 0) return 1; if(dp.find({n, k}) != dp.end()) return dp[{n, k}]; LL val = 0; for(int i = 0; i < n && k - i >= 0; i++) { val = val + f(n - 1, k - i); if(val > MX) val = MX + 1LL; } dp[{n, k}] = val; return dp[{n, k}]; } vector <int> wynik; bool usu[mxn]; int mty_element(int m) { int cnt = 0; for(int i = 1; i <= n; i++) { if(usu[i] == false) cnt++; if(cnt == m) { return i; } } return -1; } int main() { scanf("%d%lld", &n, &k); if(n % 4 == 2 || n % 4 == 3) { printf("NIE\n"); return 0; } int ile_inw; if(n % 4 == 0) ile_inw = n * (n / 4) - (n / 4); else ile_inw = n * (n / 4); if(f(n, ile_inw) < k) { printf("NIE\n"); return 0; } for(int i = 1; i <= n; i++) { LL sum = 0; for(int j = 1; j <= n - i + 1; j++) { LL dodaj = f(n - i, ile_inw); if(sum + dodaj >= k) { k -= sum; int id = mty_element(j); usu[id] = true; wynik.push_back(id); break; } ile_inw--; sum += dodaj; } } wynik.push_back(mty_element(1)); printf("TAK\n"); for(auto a : wynik) printf("%d ", a); printf("\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 | // n ^ 3 #include <bits/stdc++.h> using namespace std; #define LL long long const int mxn = 3e5; const LL MOD = 1e9 + 7LL; const LL MX = 1e18; int n; LL k; map < pair <int, int>, LL> dp; LL f(int n, int k) { if(n == 0) return 0; if(k == 0) return 1; if(dp.find({n, k}) != dp.end()) return dp[{n, k}]; LL val = 0; for(int i = 0; i < n && k - i >= 0; i++) { val = val + f(n - 1, k - i); if(val > MX) val = MX + 1LL; } dp[{n, k}] = val; return dp[{n, k}]; } vector <int> wynik; bool usu[mxn]; int mty_element(int m) { int cnt = 0; for(int i = 1; i <= n; i++) { if(usu[i] == false) cnt++; if(cnt == m) { return i; } } return -1; } int main() { scanf("%d%lld", &n, &k); if(n % 4 == 2 || n % 4 == 3) { printf("NIE\n"); return 0; } int ile_inw; if(n % 4 == 0) ile_inw = n * (n / 4) - (n / 4); else ile_inw = n * (n / 4); if(f(n, ile_inw) < k) { printf("NIE\n"); return 0; } for(int i = 1; i <= n; i++) { LL sum = 0; for(int j = 1; j <= n - i + 1; j++) { LL dodaj = f(n - i, ile_inw); if(sum + dodaj >= k) { k -= sum; int id = mty_element(j); usu[id] = true; wynik.push_back(id); break; } ile_inw--; sum += dodaj; } } wynik.push_back(mty_element(1)); printf("TAK\n"); for(auto a : wynik) printf("%d ", a); printf("\n"); return 0; } |