fork download
  1. #include <boost/multiprecision/cpp_int.hpp>
  2. #include <iostream>
  3. #include <vector>
  4. namespace mp = boost::multiprecision;
  5. class f845 {
  6. private:
  7. std::vector<std::vector<mp::cpp_int> > cc;
  8. mp::cpp_int count_permutations(int s, int w) {
  9. if (s < 1 || w < 1) {
  10. return 0;
  11. } else if (s <= 5 && w == 1) {
  12. return 1;
  13. } else if (0 <= cc[s][w]) {
  14. return cc[s][w];
  15. } else {
  16. mp::cpp_int c = 0;
  17. for (int d = 1; d <= 5; d++)
  18. c += count_permutations(s - d, w - 1);
  19. return cc[s][w] = c;
  20. }
  21. }
  22. std::vector<int> aux(mp::cpp_int n, std::vector<int> acc, mp::cpp_int b, int s, int w) {
  23. if (s < 1 || w < 1) {
  24. return acc;
  25. } else if (w == 1) {
  26. acc.push_back(s);
  27. return acc;
  28. } else {
  29. for (int d = 1; d <= 5; d++) {
  30. mp::cpp_int c = count_permutations(s - d, w - 1);
  31. if (n <= b + c)
  32. return acc.push_back(d), aux(n, acc, b, s - d, w - 1);
  33. b += c;
  34. }
  35. return {{}};
  36. }
  37. }
  38. public:
  39. f845(int m) : cc(m + 1, std::vector<mp::cpp_int>(m + 1, -1)) {}
  40. mp::cpp_int operator()(int m, mp::cpp_int n) {
  41. mp::cpp_int b = 0;
  42. for (int w = 1; w <= m; w++) {
  43. mp::cpp_int c = count_permutations(m, w);
  44. if (n <= b + c) {
  45. mp::cpp_int acc = 0;
  46. for (int d : aux(n, std::vector<int>(), b, m, w))
  47. acc = acc * 10 + d;
  48. return acc;
  49. }
  50. b += c;
  51. }
  52. return 0;
  53. }
  54. };
  55. int main() {
  56. int m = 2000;
  57. mp::cpp_int n = (mp::cpp_int)1 << 1024;
  58. auto f = f845(m);
  59. std::cout << "(" << m << ", " << n << ")\n ↓\n" << f(m, n) << std::endl;
  60. return 0;
  61. }
  62.  
Success #stdin #stdout 0.3s 147908KB
stdin
Standard input is empty
stdout
(2000, 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216)
 ↓
133455533351454245243544545545533544533353455351551354434255345155535155553543255433542555343535545355541554555135355524435524344435532524545543525545533451134455552254455333442334354534523153425445155555435153554255552353344553554445354545514355554555554435333353123555345255242334345515554533345534425455553521553555455455545441241132525455435442255542322515454522545115515532453523344525423555455451555554153554555544451254441554323215525455545135444445535554555545425522544345243455524545444534534551543444144