#include <iostream> #include <map> #include <set> #include <vector> #include <functional> #include <iterator> #include <deque> #define D(x) #define D2(x) using namespace std; typedef long long I; I n,k; #define MAXI 100 #define MAX2 251100 I v[MAXI][MAX2]; I o(int i,int p) { if (i < 0 || p<0) { return 0; } return v[i][p]; } I correct(I i, I p) { I o = p*(p-1)/2+1; if(i>o/2) return o-i-1; return i; } void ov() { v[0][0]=1; v[0][1]=1; for(int p=2;p<=n;p++) { for(int i=0;i<MAXI;i++) { v[i][p] = o(i-1,p) + o(i,p-1) - o(i-p,p-1); if(v[i][p] > k+1){ v[i][p] = k+1; } } for(int i=0;i<MAXI;i++) { int c = correct(i,p); v[i][p] = o(c,p); } } } I oblicz(I i, I p) { I ii = correct(i, p); if(ii>=MAXI) return k+1; return o(ii,p); } int main() { deque<int> r; vector<int> r2; cin >> n >> k; ov(); for(int e=1;e<=n;e++) r.push_back(e); I ii = (n-1)*(n)/2; D(cout << "n: " << n << " k:" << k << "\n"); D(cout << "ii: " << ii << "\n"); #define D3(x) if((ii&1) == 0) { ii/=2; while(!r.empty() && ii >= 0) { I p = r.size(); bool ok = false; for(auto it = r.begin(); it != r.end() && ii >= 0; it++) { if(ii>(p-1)*(p-2)/2+1) { I dist = ii - ((p-1)*(p-2)/2+1); ii -= dist; advance(it, dist); if(it==r.end()) break; } I kk = oblicz(ii, p-1); D3(cout << "r:"<< *it << " ii2:" << ii << " p-1:" << p-1 << " kk:" << kk << " k:" << k<<"\n"); if (k <= kk) { D(cout << "r2:" << *it << "\n"); r2.push_back(*it); r.erase(it); ok = true; break; } k-=kk; ii--; } if(!ok) break; } } if(r2.size() == n) { cout << "TAK\n"; for(auto x: r2) cout << x << " "; } else { cout << "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 | #include <iostream> #include <map> #include <set> #include <vector> #include <functional> #include <iterator> #include <deque> #define D(x) #define D2(x) using namespace std; typedef long long I; I n,k; #define MAXI 100 #define MAX2 251100 I v[MAXI][MAX2]; I o(int i,int p) { if (i < 0 || p<0) { return 0; } return v[i][p]; } I correct(I i, I p) { I o = p*(p-1)/2+1; if(i>o/2) return o-i-1; return i; } void ov() { v[0][0]=1; v[0][1]=1; for(int p=2;p<=n;p++) { for(int i=0;i<MAXI;i++) { v[i][p] = o(i-1,p) + o(i,p-1) - o(i-p,p-1); if(v[i][p] > k+1){ v[i][p] = k+1; } } for(int i=0;i<MAXI;i++) { int c = correct(i,p); v[i][p] = o(c,p); } } } I oblicz(I i, I p) { I ii = correct(i, p); if(ii>=MAXI) return k+1; return o(ii,p); } int main() { deque<int> r; vector<int> r2; cin >> n >> k; ov(); for(int e=1;e<=n;e++) r.push_back(e); I ii = (n-1)*(n)/2; D(cout << "n: " << n << " k:" << k << "\n"); D(cout << "ii: " << ii << "\n"); #define D3(x) if((ii&1) == 0) { ii/=2; while(!r.empty() && ii >= 0) { I p = r.size(); bool ok = false; for(auto it = r.begin(); it != r.end() && ii >= 0; it++) { if(ii>(p-1)*(p-2)/2+1) { I dist = ii - ((p-1)*(p-2)/2+1); ii -= dist; advance(it, dist); if(it==r.end()) break; } I kk = oblicz(ii, p-1); D3(cout << "r:"<< *it << " ii2:" << ii << " p-1:" << p-1 << " kk:" << kk << " k:" << k<<"\n"); if (k <= kk) { D(cout << "r2:" << *it << "\n"); r2.push_back(*it); r.erase(it); ok = true; break; } k-=kk; ii--; } if(!ok) break; } } if(r2.size() == n) { cout << "TAK\n"; for(auto x: r2) cout << x << " "; } else { cout << "NIE\n"; } return 0; } |