fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <boost/multiprecision/cpp_int.hpp>
  4.  
  5. using namespace std;
  6. using BigInt = boost::multiprecision::cpp_int;
  7.  
  8. string nthPartition(int m, BigInt n)
  9. {
  10. vector<vector<BigInt>> A(m, vector<BigInt>(m, 0));
  11. fill_n(A[0].begin(), min(m, 5), 1);
  12. for (int i = 1; i < m; i++) {
  13. int j = 0;
  14. while (++j < m) A[i][j] = A[i][j - 1] + A[i - 1][j - 1];
  15. while (--j > 5) A[i][j] -= A[i][j - 5];
  16. }
  17.  
  18. int L;
  19. for (L = 1; L <= m; L++) {
  20. if (n <= A[L - 1][m - 1]) break;
  21. if (L < m) {
  22. n -= A[L - 1][m - 1];
  23. } else {
  24. return "0";
  25. }
  26. }
  27.  
  28. string a(L, '0');
  29. for (int i = 0; i < L; i++) {
  30. int j;
  31. for (j = 1; j <= 5; j++) {
  32. if (i < L - 1) {
  33. if (n <= A[L - i - 2][m - j - 1]) break;
  34. n -= A[L - i - 2][m - j - 1];
  35. } else {
  36. if (j == m) break;
  37. }
  38. }
  39.  
  40. a[i] += j;
  41. m -= j;
  42. }
  43.  
  44. return a;
  45. }
  46.  
  47. int main(void)
  48. {
  49. int m = 2000;
  50. BigInt n = (BigInt)1 << 1024;
  51. cout << "(" << m << ", " << n << ")\n ↓\n" << nthPartition(m, n) << endl;
  52. return 0;
  53. }
Success #stdin #stdout 0.5s 377708KB
stdin
Standard input is empty
stdout
(2000, 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216)
 ↓
133455533351454245243544545545533544533353455351551354434255345155535155553543255433542555343535545355541554555135355524435524344435532524545543525545533451134455552254455333442334354534523153425445155555435153554255552353344553554445354545514355554555554435333353123555345255242334345515554533345534425455553521553555455455545441241132525455435442255542322515454522545115515532453523344525423555455451555554153554555544451254441554323215525455545135444445535554555545425522544345243455524545444534534551543444144