// Artur Kraska, II UWr #include <algorithm> #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <vector> #include <cmath> #include <list> #include <set> #include <map> #define forr(i, n) for(int i=0; i<n; i++) #define FOREACH(iter, coll) for(typeof(coll.begin()) iter = coll.begin(); iter != coll.end(); ++iter) #define FOREACHR(iter, coll) for(typeof(coll.rbegin()) iter = coll.rbegin(); iter != coll.rend(); ++iter) #define lbound(P,R,PRED) ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;}) #define testy() int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests) #define CLEAR(tab) memset(tab, 0, sizeof(tab)) #define CONTAIN(el, coll) (coll.find(el) != coll.end()) #define FOR(i, a, b) for(int i=a; i<=b; i++) #define FORD(i, a, b) for(int i=a; i>=b; i--) #define MP make_pair #define PB push_back #define deb(X) X; #define M 1000000007 #define INF 1000000000000000007ll using namespace std; //int tab[107], res[107][10007]; //long long il[107][10007]; long long n, k; vector <vector <long long> > v; int d[1000007], S = 1<<18; /* void dzialaj(int poz) { if(poz == n) { int il = 0; forr(i, n) FOR(j, i+1, n-1) if(tab[i] > tab[j]) il++; res[n][il]++; } else { FOR(i, poz, n-1) { swap(tab[i], tab[poz]); dzialaj(poz+1); swap(tab[i], tab[poz]); } } } */ long long ile(long long n, long long k) { if(k > n*(n-1)/2) return 0; if(k < v[n].size()) return v[n][k]; if(n*(n-1)/2 - k < v[n].size()) return v[n][n*(n-1)/2 - k]; return INF; } int znajdz_i_wez(int poz) { // cout << "szuka " << poz << endl; int nr = 1; while(nr < S) { d[nr]--; if(d[nr*2] < poz) { poz -= d[nr*2]; nr = nr*2+1; } else nr *= 2; } d[nr]--; // cout << "znalazl " << nr-S+1 << endl; return nr-S+1; } int main() { /* FOR(x, 1, 10) { n = x; forr(i, n) tab[i] = i+1; dzialaj(0); FOR(i, 0, n*(n-1)/4) cout << "res[" << n << "][" << i << "] = " << res[n][i] << endl; }*/ vector <long long> v0; v0.PB(1); v.PB(v0); //il[0][0] = 1; FOR(x, 1, 250003) { vector <long long> prev = v.back(), now; //il[x][0] = il[x][x*(x-1)/2] = 1; now.PB(1); FOR(i, 1, max(10, x*(x-1)/2)) { //il[x][i] = il[x][i-1] + il[x-1][i] - (i >= x ? il[x-1][i-x] : 0); now.PB(now[i-1] + (prev.size() > i ? prev[i] : 0) - (prev.size() > i-x && i-x >= 0 ? prev[i-x] : 0)); if(now.back() >= INF) { now[i] = INF; break; } //il[x][x*(x-1)/2-i] = il[x][i]; } v.PB(now); } FOR(i, 0, -250000) { cout << i << ":"; forr(j, v[i].size()) { cout << " " << v[i][j]; } cout << endl; } scanf("%lld %lld", &n, &k); long long inw = n*(n-1)/4; if(n*(n-1)/2 %2 != 0 || k > ile(n, inw)) { printf("NIE\n"); return 0; } printf("TAK\n"); k--; //list <long long> l; //FOR(i, 1, n) // l.PB(i); FOR(i, 0, n) d[i+S] = 1; FORD(i, S-1, 1) d[i] = d[i*2] + d[i*2+1]; while(n > 0) { int nr = 0; //cout << "przed " << n << endl; if(inw > (n-1)*(n-2)/2) { int x = inw - (n-1)*(n-2)/2; inw -= x; nr += x; } while(ile(n-1, inw) <= k) { nr++; k -= ile(n-1, inw); inw--; } //cout << "po" << endl; int elem = znajdz_i_wez(nr+1); /*list <long long>::iterator it = l.begin(); while(nr > 0) { it++; nr--; }*/ printf("%d ", elem); //l.erase(it); n--; } 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 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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | // Artur Kraska, II UWr #include <algorithm> #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <vector> #include <cmath> #include <list> #include <set> #include <map> #define forr(i, n) for(int i=0; i<n; i++) #define FOREACH(iter, coll) for(typeof(coll.begin()) iter = coll.begin(); iter != coll.end(); ++iter) #define FOREACHR(iter, coll) for(typeof(coll.rbegin()) iter = coll.rbegin(); iter != coll.rend(); ++iter) #define lbound(P,R,PRED) ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;}) #define testy() int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests) #define CLEAR(tab) memset(tab, 0, sizeof(tab)) #define CONTAIN(el, coll) (coll.find(el) != coll.end()) #define FOR(i, a, b) for(int i=a; i<=b; i++) #define FORD(i, a, b) for(int i=a; i>=b; i--) #define MP make_pair #define PB push_back #define deb(X) X; #define M 1000000007 #define INF 1000000000000000007ll using namespace std; //int tab[107], res[107][10007]; //long long il[107][10007]; long long n, k; vector <vector <long long> > v; int d[1000007], S = 1<<18; /* void dzialaj(int poz) { if(poz == n) { int il = 0; forr(i, n) FOR(j, i+1, n-1) if(tab[i] > tab[j]) il++; res[n][il]++; } else { FOR(i, poz, n-1) { swap(tab[i], tab[poz]); dzialaj(poz+1); swap(tab[i], tab[poz]); } } } */ long long ile(long long n, long long k) { if(k > n*(n-1)/2) return 0; if(k < v[n].size()) return v[n][k]; if(n*(n-1)/2 - k < v[n].size()) return v[n][n*(n-1)/2 - k]; return INF; } int znajdz_i_wez(int poz) { // cout << "szuka " << poz << endl; int nr = 1; while(nr < S) { d[nr]--; if(d[nr*2] < poz) { poz -= d[nr*2]; nr = nr*2+1; } else nr *= 2; } d[nr]--; // cout << "znalazl " << nr-S+1 << endl; return nr-S+1; } int main() { /* FOR(x, 1, 10) { n = x; forr(i, n) tab[i] = i+1; dzialaj(0); FOR(i, 0, n*(n-1)/4) cout << "res[" << n << "][" << i << "] = " << res[n][i] << endl; }*/ vector <long long> v0; v0.PB(1); v.PB(v0); //il[0][0] = 1; FOR(x, 1, 250003) { vector <long long> prev = v.back(), now; //il[x][0] = il[x][x*(x-1)/2] = 1; now.PB(1); FOR(i, 1, max(10, x*(x-1)/2)) { //il[x][i] = il[x][i-1] + il[x-1][i] - (i >= x ? il[x-1][i-x] : 0); now.PB(now[i-1] + (prev.size() > i ? prev[i] : 0) - (prev.size() > i-x && i-x >= 0 ? prev[i-x] : 0)); if(now.back() >= INF) { now[i] = INF; break; } //il[x][x*(x-1)/2-i] = il[x][i]; } v.PB(now); } FOR(i, 0, -250000) { cout << i << ":"; forr(j, v[i].size()) { cout << " " << v[i][j]; } cout << endl; } scanf("%lld %lld", &n, &k); long long inw = n*(n-1)/4; if(n*(n-1)/2 %2 != 0 || k > ile(n, inw)) { printf("NIE\n"); return 0; } printf("TAK\n"); k--; //list <long long> l; //FOR(i, 1, n) // l.PB(i); FOR(i, 0, n) d[i+S] = 1; FORD(i, S-1, 1) d[i] = d[i*2] + d[i*2+1]; while(n > 0) { int nr = 0; //cout << "przed " << n << endl; if(inw > (n-1)*(n-2)/2) { int x = inw - (n-1)*(n-2)/2; inw -= x; nr += x; } while(ile(n-1, inw) <= k) { nr++; k -= ile(n-1, inw); inw--; } //cout << "po" << endl; int elem = znajdz_i_wez(nr+1); /*list <long long>::iterator it = l.begin(); while(nr > 0) { it++; nr--; }*/ printf("%d ", elem); //l.erase(it); n--; } printf("\n"); return 0; } |