Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define forn(i, n) for (int i = 0; i < (int)n; i++)
- #define ford(i, n) for (int i = (int)n - 1; i >= 0; i--)
- #define forab(i, a, b) for (int i = (int)a; i < (int)b; i++)
- #define pb push_back
- #define mp make_pair
- #define all(x) (x).begin(),(x).end()
- #define X first
- #define Y second
- #define fst first
- #define snd second
- #define db(x) cout << #x << " = " << x << endl;
- #define db2(x,y) cout << #x << " = " << x << "; " << #y << " = " << y << endl;
- #define sz(v) ((int)(v).size())
- typedef vector<int> vi;
- typedef pair<int, int> pii;
- typedef long long ll;
- typedef long double ld;
- const int INF = (int)1E9;
- const ld eps = 1e-9;
- const ld pi = acos(-1.0);
- const int MAXN = 100500;
- #define FNAME "a"
- const int base = (int)1e9;
- const int dg_base = 9;
- struct bigint
- {
- vi val;
- bigint ()
- {
- val = {0};
- }
- bigint (int x)
- {
- val = {x};
- }
- bigint (const vi &v)
- {
- val = v;
- }
- int len()
- {
- return sz(val);
- }
- };
- bigint operator + (const bigint &l, const bigint &r)
- {
- // cerr << "+" << endl;
- const vi &vl = l.val, &vr = r.val;
- vi ans(max(sz(vl), sz(vr)));
- for (int i = 0; i < sz(vl); i++)
- ans[i] += vl[i];
- for (int i = 0; i < sz(vr); i++)
- ans[i] += vr[i];
- for (int i = 0; i < sz(ans); i++)
- {
- if (ans[i] >= base)
- {
- if (i + 1 == sz(ans))
- ans.pb(0);
- ans[i] -= base;
- ans[i + 1]++;
- }
- }
- return bigint(ans);
- }
- bool operator < (const bigint &l, const bigint &r)
- {
- // cerr <<"<" << endl;
- const vi &vl = l.val, &vr = r.val;
- if (sz(vl) != sz(vr))
- return sz(vl) < sz(vr);
- for (int i = sz(vl) - 1; i >= 0; i--)
- {
- if (vl[i] != vr[i])
- return vl[i] < vr[i];
- }
- return false;
- }
- bigint operator * (const bigint &l, const bigint &r)
- {
- // cerr << "*" << endl;
- const vi &vl = l.val, &vr = r.val;
- vector<ll> ans(sz(vl) + sz(vr));
- for (int i = 0; i < sz(vl); i++)
- for (int j = 0; j < sz(vr); j++)
- {
- ans[i + j] += (ll)vl[i] * vr[j];
- if (ans[i + j] >= (ll)base * base)
- {
- ans[i + j] -= (ll)base * base;
- ans[i + j + 1]++;
- }
- }
- for (int i = 0; i < sz(ans); i++)
- if (ans[i] >= base)
- {
- ans[i + 1] += ans[i] / base;
- ans[i] %= base;
- }
- while (ans.back() == 0 && sz(ans) >= 2)
- ans.pop_back();
- vi res(sz(ans));
- copy(all(ans), res.begin());
- return bigint(res);
- }
- bigint operator - (const bigint &l, const bigint &r)
- {
- //cerr << "-" << endl;
- const vi &vl = l.val, &vr = r.val;
- assert(!(vl < vr));
- vi ans = vl;
- for (int i = 0; i < sz(vr); i++)
- ans[i] -= vr[i];
- for (int i = 0; i < sz(ans); i++)
- if (ans[i] < 0)
- {
- assert(i + 1 < sz(ans));
- ans[i + 1]--;
- ans[i] += base;
- assert(ans[i] >= 0);
- }
- while (sz(ans) >= 2 && ans.back() == 0)
- ans.pop_back();
- return bigint(ans);
- }
- //typedef long long bigint;
- const int N = 510;
- bigint dp[N][N], ch[N][N];
- void precalc()
- {
- dp[0][0] = 1;
- for (int i = 1; i < N; i++)
- {
- dp[0][i] = dp[0][i - 1] * i;
- }
- for (int x = 1; x < N; x++)
- for (int y = 0; y < N; y++)
- {
- dp[x][y] = dp[x - 1][y] * y;
- if (x >= 2 && y + 1 < N)
- dp[x][y] = dp[x][y] + dp[x - 2][y + 1] * (x - 1);
- // cerr << dp[x][y] << ' ';
- // if (y + 1 == N)
- // cerr << endl;
- }
- for (int i = 0; i < N; i++)
- {
- ch[i][0] = 1;
- ch[i][i] = 1;
- for (int j = 1; j < i; j++)
- {
- ch[i][j] = ch[i - 1][j - 1] + ch[i - 1][j];
- }
- }
- }
- void read (bigint &d)
- {
- string s;
- cin >> s;
- bigint ans(0);
- for (char ch : s)
- {
- ans = ans * bigint(10);
- ans = ans + bigint(ch - '0');
- }
- d = ans;
- }
- void solve (int n)
- {
- int k;
- cin >> k;
- bigint d;
- read(d);
- // cerr << "Read" << endl;
- d = d - bigint(1);
- // cerr << "herre" << endl;
- vi who(n, -1);
- vector<char> used(n, 0);
- int ck = k;
- int x = n, y = 0;
- for (int i = 0; i < n; i++)
- {
- //assert(x >= ck);
- map<tuple<int, int, int>, bigint> what;
- for (int j = 0; j < n; j++)
- if (!used[j])
- {
- // cout << "i=" << i << " j=" << j << endl;
- int nx = x, ny = y, nk = ck;
- if (i == j)
- {
- nk--;
- nx--;
- }
- else if (j < i)
- {
- ny--;
- if (!used[i])
- ny++, nx--;
- }
- else if (j > i)
- {
- nx--;
- if (!used[i])
- nx--, ny++;
- }
- // cout << "nx=" << nx << ' ' << " ny=" << ny << endl;
- if (!what.count(make_tuple(nx, ny, nk)))
- {
- bigint nres = 0;
- if (nx >= nk)
- nres = ch[nx][nk] * dp[nx - nk][ny];
- what[make_tuple(nx, ny, nk)] = nres;
- }
- const bigint &res = what[make_tuple(nx, ny, nk)];
- // cout << res << endl;
- if (!(d < res))
- {
- d = d - res;
- }
- else
- {
- // cout << "OK! " << i << " " << j << endl;
- who[i] = j;
- used[j] = 1;
- x = nx;
- y = ny;
- ck = nk;
- break;
- }
- }
- }
- for (int i = 0; i < n; i++)
- cout << who[i] + 1 << " \n"[i == n - 1];
- }
- int main() {
- int t;
- cin >> t;
- precalc();
- // cerr << "sdF" << endl;
- for (int i = 0; i < t; i++)
- {
- int n;
- cin >> n;
- solve(n);
- }
- return 0;
- }
- /*
- 0 1 4 18 96 600 4320 35280 322560 3265920 36288000 439084800 5748019200 80951270400 1220496076800 19615115520000 334764638208000 6046686277632000 115242726703104000 2311256907767808000
- 1 3 14 78 504 3720 30960 287280 2943360 33022080 402796800 5308934400 75203251200 1139544806400 18394619443200 315149522688000 5711921639424000 109196040425472000 2196014181064704000 7020393100169248768
- 2 11 64 426 3216 27240 256320 2656080 30078720 369774720 4906137600 69894316800 1064341555200 17255074636800 296754903244800 5396772116736000 103484118786048000 2086818140639232000 7257280927281184768 4260260387248865280
- 9 53 362 2790 24024 229080 2399760 27422640 339696000 4536362880 64988179200 994447238400 16190733081600 279499828608000 5100017213491200 98087346669312000 1983334021853184000 5170462786641952768 4118283401892659200 7157971062890233856
- 44 309 2428 21234 205056 2170680 25022880 312273360 4196666880 60451816320 929459059200 15196285843200 263309095526400 4820517384883200 92987329455820800 1885246675183872000 3187128764788768768 6246526639780626432 -1063577585484431360 6874241678947581952
- 265 2119 18806 183822 1965624 22852200 287250480 3884393520 56255149440 869007242880 14266826784000 248112809683200 4557208289356800 88166812070937600 1792259345728051200 1301882089604896768 3059397874991857664 -2344838630232915968 -1801285297978146816 1483383384037195776
- 1854 16687 165016 1781802 20886576 264398280 3597143040 52370755920 812752093440 13397819541120 233845982899200 4309095479673600 83609603781580800 1704092533657113600 -490377256123154432 1757515785386960896 -5804194529994276864 9096509994262495232 8822314710078849024 -8709203850712383488
- 14833 148329 1616786 19104774 243511704 3332744760 48773612880 760381337520 12585067447680 220448163358080 4075249496774400 79300508301907200 1620482929875532800 -2194469789780268032 2247893041510115328 -7561710315381237760 1399481550718468096 -5542279773061021696 3164651806293688320 545823499850678272
- 133496 1468457 17487988 224406930 3089233056 45440868120 711607724640 11824686110160 207863095910400 3854801333416320 75225258805132800 1541182421573625600 -3814952719655800832 4442362831290383360 8637140716818198528 6161485692713183232 2930064397047693312 -5193494387697025024 5736845781296873472 -8076097576546664448
- 1334961 16019531 206918942 2864826126 42351635064 666166856520 11113078385520 196038409800240 3646938237505920 71370457471716480 1465957162768492800 -5356135141229426432 8257315550946184192 4194777885527815168 -2475655024105015296 -5662845095093348352 -2999487604786102272 -4467306113077575680 -2504828880257482752 -5871901364710211584
- 14684570 190899411 2657907184 39486808938 623815221456 10446911529000 184925331414720 3450899827705680 67723519234210560 1394586705296776320 -6822092303997919232 -4833293381533940992 -4062537665418369024 -6670432909632830464 8508942515952066560 -301800234794637312 -7693025185000128512 -129002035640500224 3279312905865527296 -885661487236710400
- 176214841 2467007773 36828901754 584328412518 9823096307544 174478419885720 3265974496290960 64272619406504880 1326863186062565760 -8216679009294695552 1988798922463978240 770755716115571968 -2607895244214461440 -3267368648124654592 1493666956173445120 -627879027147581440 -6208073540469424128 7147335858698289152 -5563282706232836096 1619175816212054016
- 2290792932 34361893981 547499510764 9238767895026 164655323578176 3091496076405240 61006644910213920 1262590566656060880 8903201878352290304 -8241266141950877824 -1218043206348406272 -3378650960330033408 -659473403910193152 4291285544675634176 -1157009504816967680 8945976815042045952 -8643480706649030656 -5164232195128721408 -86562116774264832 -6129147639390076928
- 32071101049 513137616783 8691268384262 155416555683150 2926840752827064 57915148833808680 1201583921745846960 7640611311696229424 1302276053406383488 7023222935602471552 -2160607753981627136 2719177556419840256 4950758948585827328 1417406216199824384 -5913816346646554624 -1855536021600479232 -8486836995887628288 5906074165170831360 1044423435110383616 -5773340706154151936
- 481066515734 8178130767479 146725287298888 2771424197143914 54988308080981616 1143668772912038280 6439027389950382464 -6338335258289845936 5720946882196088064 -9183830689584098688 4879785310401467392 2231581392165987072 8806640566231496704 2228147743160169472 5556758410117775360 -1267817627417204736 -5174457855199084544 6957670804516667392 6778531175364034560 986991025328422912
- 7697064251745 138547156531409 2624698909845026 52216883883837702 1088680464831056664 5295358617038344184 5669381425469323216 -6387461933223617616 3541966501929364864 -4383128073723985536 -2648203918235480320 6575059174065509632 -2186428478191535104 -4401092317487461376 -5378654803486988288 1254133240103917568 5799786794377117696 4819546687472238592 -1480037583178760192 306085407530483712
- 130850092279664 2486151753313617 49592184973992676 1036463580947218962 4206678152207287520 374022808430979032 6389900715016610784 -8517315638556569136 -7925094575653350400 1734924155488505216 9223263092300989952 -8129028910090066176 -9033521921445261312 -5199553712871695360 -3352528918945353728 -8639094860497934336 1205136771495493632 5921351755757092864 7597923981746634752 5815622743079190528
- 2355301661033953 47106033220679059 986871395973226286 3170214571260068558 -3832655343776308488 6015877906585631752 3539527720136371696 592221062903218736 -8786725342567696000 7488338936812484736 1094452071318495488 2538606375720036608 1246608282465537024 6989366736775123968 -7168395709758801920 5903440744361089024 8980761662408196096 1715364638993448960 -5607868990218764288 -183632323752689664
- 44750731559645106 939765362752547227 2183343175286842272 -7002869915036377046 -8598210823347611376 -2476350186449260056 -2947306657233152960 9067797668238636880 -2171679794329370880 -6393886865493989248 -6250791152469374976 -5551515937127439616 -4845691147265865728 -6377241110358858752 2393169606347677696 -436391389922088960 -7977898829774913536 -96866129057906688 3739567551487672320 -3489014151301103616
- 1 2 3 4
- 1 2 3 4
- 2 0 0 0
- 2 0 0 0
- 3 1 0 0
- 3 1 0 0
- 4 1 2 0
- 4 1 2 0
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement