Advertisement
Guest User

Untitled

a guest
Aug 27th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.26 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define forn(i, n) for (int i = 0; i < (int)n; i++)
  6. #define ford(i, n) for (int i = (int)n - 1; i >= 0; i--)
  7. #define forab(i, a, b) for (int i = (int)a; i < (int)b; i++)
  8. #define pb push_back
  9. #define mp make_pair
  10. #define all(x) (x).begin(),(x).end()
  11. #define X first
  12. #define Y second
  13. #define fst first
  14. #define snd second
  15. #define db(x) cout << #x << " = " << x << endl;
  16. #define db2(x,y) cout << #x << " = " << x << "; " << #y << " = " << y << endl;
  17.  
  18. #define sz(v) ((int)(v).size())
  19.  
  20. typedef vector<int> vi;
  21. typedef pair<int, int> pii;
  22. typedef long long ll;
  23. typedef long double ld;
  24.  
  25. const int INF = (int)1E9;
  26. const ld eps = 1e-9;
  27. const ld pi = acos(-1.0);
  28. const int MAXN = 100500;
  29.  
  30. #define FNAME "a"
  31.  
  32. const int base = (int)1e9;
  33. const int dg_base = 9;
  34.  
  35.  
  36. struct bigint
  37. {              
  38.     vi val;
  39.  
  40.     bigint ()
  41.     {
  42.          val = {0};
  43.     }
  44.  
  45.     bigint (int x)
  46.     {
  47.         val = {x};
  48.     }
  49.  
  50.     bigint (const vi &v)
  51.     {
  52.         val = v;
  53.     }
  54.  
  55.     int len()
  56.     {
  57.         return sz(val);
  58.     }
  59. };
  60.  
  61. bigint operator + (const bigint &l, const bigint &r)
  62.     {
  63.     //  cerr << "+" << endl;
  64.         const vi &vl = l.val, &vr = r.val;
  65.         vi ans(max(sz(vl), sz(vr)));
  66.         for (int i = 0; i < sz(vl); i++)
  67.             ans[i] += vl[i];
  68.         for (int i = 0; i < sz(vr); i++)
  69.             ans[i] += vr[i];
  70.  
  71.         for (int i = 0; i < sz(ans); i++)
  72.         {
  73.             if (ans[i] >= base)
  74.             {
  75.                 if (i + 1 == sz(ans))
  76.                     ans.pb(0);
  77.                 ans[i] -= base;
  78.                 ans[i + 1]++;
  79.             }
  80.         }  
  81.  
  82.         return bigint(ans);
  83.     }
  84.  
  85.     bool operator < (const bigint &l, const bigint &r)
  86.     {
  87.     //  cerr <<"<" << endl;
  88.         const vi &vl = l.val, &vr = r.val;
  89.         if (sz(vl) != sz(vr))
  90.             return sz(vl) < sz(vr);
  91.         for (int i = sz(vl) - 1; i >= 0; i--)
  92.         {
  93.             if (vl[i] != vr[i])
  94.                 return vl[i] < vr[i];
  95.         }
  96.         return false;
  97.     }
  98.  
  99.     bigint operator * (const bigint &l, const bigint &r)
  100.     {
  101.     //  cerr << "*" << endl;
  102.         const vi &vl = l.val, &vr = r.val;
  103.         vector<ll> ans(sz(vl) + sz(vr));
  104.         for (int i = 0; i < sz(vl); i++)
  105.         for (int j = 0; j < sz(vr); j++)
  106.         {
  107.             ans[i + j] += (ll)vl[i] * vr[j];
  108.             if (ans[i + j] >= (ll)base * base)
  109.             {
  110.                 ans[i + j] -= (ll)base * base;
  111.                 ans[i + j + 1]++;
  112.             }
  113.         }                        
  114.  
  115.         for (int i = 0; i < sz(ans); i++)
  116.         if (ans[i] >= base)
  117.         {
  118.             ans[i + 1] += ans[i] / base;
  119.             ans[i] %= base;
  120.         }
  121.  
  122.         while (ans.back() == 0 && sz(ans) >= 2)
  123.             ans.pop_back();
  124.        
  125.         vi res(sz(ans));
  126.         copy(all(ans), res.begin());
  127.         return bigint(res);
  128.     }
  129.  
  130.     bigint operator - (const bigint &l, const bigint  &r)
  131.      {
  132.         //cerr << "-" << endl;
  133.         const vi &vl = l.val, &vr = r.val;
  134.         assert(!(vl < vr));
  135.                
  136.         vi ans = vl;
  137.         for (int i = 0; i < sz(vr); i++)
  138.             ans[i] -= vr[i];
  139.  
  140.         for (int i = 0; i < sz(ans); i++)
  141.         if (ans[i] < 0)
  142.         {
  143.             assert(i + 1 < sz(ans));    
  144.             ans[i + 1]--;
  145.             ans[i] += base;
  146.             assert(ans[i] >= 0);
  147.         }
  148.  
  149.         while (sz(ans) >= 2 && ans.back() == 0)
  150.             ans.pop_back();
  151.         return bigint(ans);
  152.     }
  153.  
  154.  
  155. //typedef long long bigint;
  156.  
  157. const int N = 510;
  158. bigint dp[N][N], ch[N][N];
  159.  
  160. void precalc()
  161. {
  162.     dp[0][0] = 1;
  163.     for (int i = 1; i < N; i++)
  164.     {
  165.         dp[0][i] = dp[0][i - 1] * i;  
  166.     }
  167.  
  168.     for (int x = 1; x < N; x++)
  169.     for (int y = 0; y < N; y++)
  170.     {
  171.         dp[x][y] = dp[x - 1][y] * y;
  172.         if (x >= 2 && y + 1 < N)
  173.             dp[x][y] = dp[x][y] + dp[x - 2][y + 1] * (x - 1);
  174.    
  175. //      cerr << dp[x][y] << ' ';
  176. //      if (y + 1 == N)
  177. //          cerr << endl;
  178.     }
  179.  
  180.     for (int i = 0; i < N; i++)
  181.     {
  182.         ch[i][0] = 1;
  183.         ch[i][i] = 1;
  184.         for (int j = 1; j < i; j++)
  185.         {
  186.             ch[i][j] = ch[i - 1][j - 1] + ch[i - 1][j];
  187.         }
  188.     }
  189. }
  190.  
  191. void read (bigint &d)
  192. {
  193.     string s;
  194.     cin >> s;
  195.     bigint ans(0);
  196.     for (char ch : s)
  197.     {
  198.         ans = ans * bigint(10);
  199.         ans = ans + bigint(ch - '0');
  200.     }
  201.     d = ans;
  202. }
  203.  
  204. void solve (int n)
  205. {
  206.     int k;
  207.     cin >> k;
  208.  
  209.     bigint d;
  210.     read(d);
  211. //  cerr << "Read" << endl;
  212.     d = d - bigint(1);
  213. //  cerr << "herre" << endl;
  214.  
  215.     vi who(n, -1);
  216.     vector<char> used(n, 0);
  217.     int ck = k;
  218.  
  219.     int x = n, y = 0;
  220.  
  221.     for (int i = 0; i < n; i++)
  222.     {              
  223.         //assert(x >= ck);
  224.         map<tuple<int, int, int>, bigint> what;
  225.                                                        
  226.         for (int j = 0; j < n; j++)
  227.         if (!used[j])
  228.         {
  229.           //  cout << "i=" << i << " j=" << j << endl;
  230.            
  231.             int nx = x, ny = y, nk = ck;
  232.             if (i == j)
  233.             {
  234.                 nk--;
  235.                 nx--;
  236.             }
  237.             else if (j < i)
  238.             {
  239.                 ny--;
  240.                 if (!used[i])
  241.                     ny++, nx--;
  242.             }
  243.             else if (j > i)
  244.             {
  245.                 nx--;
  246.                 if (!used[i])
  247.                     nx--, ny++;
  248.             }
  249.         //  cout << "nx=" << nx << ' ' << " ny=" << ny << endl;
  250.            
  251.             if (!what.count(make_tuple(nx, ny, nk)))
  252.             {
  253.                 bigint nres = 0;
  254.                 if (nx >= nk)
  255.                  nres = ch[nx][nk] * dp[nx - nk][ny];
  256.                 what[make_tuple(nx, ny, nk)] = nres;
  257.             }
  258.  
  259.             const bigint &res = what[make_tuple(nx, ny, nk)];
  260.         //  cout << res << endl;
  261.             if (!(d < res))
  262.             {
  263.                 d = d - res;
  264.             }
  265.             else
  266.             {
  267.               //  cout << "OK! " << i << " " << j << endl;
  268.                 who[i] = j;
  269.                 used[j] = 1;
  270.                 x = nx;
  271.                 y = ny;
  272.                 ck = nk;
  273.                 break;
  274.             }
  275.         }
  276.     }  
  277.  
  278.     for (int i = 0; i < n; i++)
  279.         cout << who[i] + 1 << " \n"[i == n - 1];
  280. }
  281.  
  282. int main() {
  283.     int t;
  284.     cin >> t;
  285.  
  286.     precalc();
  287. //    cerr << "sdF" << endl;
  288.     for (int i = 0; i < t; i++)
  289.     {
  290.         int n;
  291.         cin >> n;
  292.         solve(n);  
  293.     }
  294.    
  295.     return 0;
  296. }
  297.  
  298. /*
  299.  
  300. 0 1 4 18 96 600 4320 35280 322560 3265920 36288000 439084800 5748019200 80951270400 1220496076800 19615115520000 334764638208000 6046686277632000 115242726703104000 2311256907767808000
  301. 1 3 14 78 504 3720 30960 287280 2943360 33022080 402796800 5308934400 75203251200 1139544806400 18394619443200 315149522688000 5711921639424000 109196040425472000 2196014181064704000 7020393100169248768
  302. 2 11 64 426 3216 27240 256320 2656080 30078720 369774720 4906137600 69894316800 1064341555200 17255074636800 296754903244800 5396772116736000 103484118786048000 2086818140639232000 7257280927281184768 4260260387248865280
  303. 9 53 362 2790 24024 229080 2399760 27422640 339696000 4536362880 64988179200 994447238400 16190733081600 279499828608000 5100017213491200 98087346669312000 1983334021853184000 5170462786641952768 4118283401892659200 7157971062890233856
  304. 44 309 2428 21234 205056 2170680 25022880 312273360 4196666880 60451816320 929459059200 15196285843200 263309095526400 4820517384883200 92987329455820800 1885246675183872000 3187128764788768768 6246526639780626432 -1063577585484431360 6874241678947581952
  305. 265 2119 18806 183822 1965624 22852200 287250480 3884393520 56255149440 869007242880 14266826784000 248112809683200 4557208289356800 88166812070937600 1792259345728051200 1301882089604896768 3059397874991857664 -2344838630232915968 -1801285297978146816 1483383384037195776
  306. 1854 16687 165016 1781802 20886576 264398280 3597143040 52370755920 812752093440 13397819541120 233845982899200 4309095479673600 83609603781580800 1704092533657113600 -490377256123154432 1757515785386960896 -5804194529994276864 9096509994262495232 8822314710078849024 -8709203850712383488
  307. 14833 148329 1616786 19104774 243511704 3332744760 48773612880 760381337520 12585067447680 220448163358080 4075249496774400 79300508301907200 1620482929875532800 -2194469789780268032 2247893041510115328 -7561710315381237760 1399481550718468096 -5542279773061021696 3164651806293688320 545823499850678272
  308. 133496 1468457 17487988 224406930 3089233056 45440868120 711607724640 11824686110160 207863095910400 3854801333416320 75225258805132800 1541182421573625600 -3814952719655800832 4442362831290383360 8637140716818198528 6161485692713183232 2930064397047693312 -5193494387697025024 5736845781296873472 -8076097576546664448
  309. 1334961 16019531 206918942 2864826126 42351635064 666166856520 11113078385520 196038409800240 3646938237505920 71370457471716480 1465957162768492800 -5356135141229426432 8257315550946184192 4194777885527815168 -2475655024105015296 -5662845095093348352 -2999487604786102272 -4467306113077575680 -2504828880257482752 -5871901364710211584
  310. 14684570 190899411 2657907184 39486808938 623815221456 10446911529000 184925331414720 3450899827705680 67723519234210560 1394586705296776320 -6822092303997919232 -4833293381533940992 -4062537665418369024 -6670432909632830464 8508942515952066560 -301800234794637312 -7693025185000128512 -129002035640500224 3279312905865527296 -885661487236710400
  311. 176214841 2467007773 36828901754 584328412518 9823096307544 174478419885720 3265974496290960 64272619406504880 1326863186062565760 -8216679009294695552 1988798922463978240 770755716115571968 -2607895244214461440 -3267368648124654592 1493666956173445120 -627879027147581440 -6208073540469424128 7147335858698289152 -5563282706232836096 1619175816212054016
  312. 2290792932 34361893981 547499510764 9238767895026 164655323578176 3091496076405240 61006644910213920 1262590566656060880 8903201878352290304 -8241266141950877824 -1218043206348406272 -3378650960330033408 -659473403910193152 4291285544675634176 -1157009504816967680 8945976815042045952 -8643480706649030656 -5164232195128721408 -86562116774264832 -6129147639390076928
  313. 32071101049 513137616783 8691268384262 155416555683150 2926840752827064 57915148833808680 1201583921745846960 7640611311696229424 1302276053406383488 7023222935602471552 -2160607753981627136 2719177556419840256 4950758948585827328 1417406216199824384 -5913816346646554624 -1855536021600479232 -8486836995887628288 5906074165170831360 1044423435110383616 -5773340706154151936
  314. 481066515734 8178130767479 146725287298888 2771424197143914 54988308080981616 1143668772912038280 6439027389950382464 -6338335258289845936 5720946882196088064 -9183830689584098688 4879785310401467392 2231581392165987072 8806640566231496704 2228147743160169472 5556758410117775360 -1267817627417204736 -5174457855199084544 6957670804516667392 6778531175364034560 986991025328422912
  315. 7697064251745 138547156531409 2624698909845026 52216883883837702 1088680464831056664 5295358617038344184 5669381425469323216 -6387461933223617616 3541966501929364864 -4383128073723985536 -2648203918235480320 6575059174065509632 -2186428478191535104 -4401092317487461376 -5378654803486988288 1254133240103917568 5799786794377117696 4819546687472238592 -1480037583178760192 306085407530483712
  316. 130850092279664 2486151753313617 49592184973992676 1036463580947218962 4206678152207287520 374022808430979032 6389900715016610784 -8517315638556569136 -7925094575653350400 1734924155488505216 9223263092300989952 -8129028910090066176 -9033521921445261312 -5199553712871695360 -3352528918945353728 -8639094860497934336 1205136771495493632 5921351755757092864 7597923981746634752 5815622743079190528
  317. 2355301661033953 47106033220679059 986871395973226286 3170214571260068558 -3832655343776308488 6015877906585631752 3539527720136371696 592221062903218736 -8786725342567696000 7488338936812484736 1094452071318495488 2538606375720036608 1246608282465537024 6989366736775123968 -7168395709758801920 5903440744361089024 8980761662408196096 1715364638993448960 -5607868990218764288 -183632323752689664
  318. 44750731559645106 939765362752547227 2183343175286842272 -7002869915036377046 -8598210823347611376 -2476350186449260056 -2947306657233152960 9067797668238636880 -2171679794329370880 -6393886865493989248 -6250791152469374976 -5551515937127439616 -4845691147265865728 -6377241110358858752 2393169606347677696 -436391389922088960 -7977898829774913536 -96866129057906688 3739567551487672320 -3489014151301103616
  319.  
  320.  
  321. 1 2 3 4
  322. 1 2 3 4
  323. 2 0 0 0
  324. 2 0 0 0
  325. 3 1 0 0
  326. 3 1 0 0
  327. 4 1 2 0
  328. 4 1 2 0
  329.  
  330. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement