fork download
  1. let f m n =
  2. let append x y = Big_int.(add_int_big_int y (mult_int_big_int 10 x)) in
  3. let zero = Big_int.zero_big_int in
  4. let one = Big_int.unit_big_int in
  5. let add = Big_int.add_big_int in
  6. let le = Big_int.le_big_int in
  7. let lt = Big_int.lt_big_int in
  8. let cc = Array.make_matrix (m + 1) (m + 1) zero in
  9. let rec count_permutations s w =
  10. if s < 1 || w < 1 || s < w || w * 5 < s then
  11. zero
  12. else if s <= 5 && w = 1 then
  13. one
  14. else if lt zero cc.(s).(w) then
  15. cc.(s).(w)
  16. else
  17. let f = fun c d -> add c (count_permutations (s - d) (w - 1)) in
  18. let () = cc.(s).(w) <- List.fold_left f zero [1; 2; 3; 4; 5] in cc.(s).(w)
  19. in
  20. let rec aux acc b s w =
  21. let rec loop b d =
  22. if 5 < d then
  23. zero
  24. else
  25. let c = count_permutations (s - d) (w - 1) in
  26. let bc = add b c in
  27. if le n bc then
  28. aux (append acc d) b (s - d) (w - 1)
  29. else
  30. loop bc (d + 1)
  31. in
  32. if s < 1 || w < 1 then
  33. acc
  34. else if w = 1 then
  35. append acc s
  36. else
  37. loop b 1
  38. in
  39. let rec loop b w =
  40. if m < w then
  41. zero
  42. else
  43. let c = count_permutations m w in
  44. let bc = add b c in
  45. if le n bc then
  46. aux zero b m w
  47. else
  48. loop bc (w + 1)
  49. in
  50. if m < 1 || lt n one then
  51. zero
  52. else
  53. loop zero 1
  54. (*
  55. let g m n =
  56.   Printf.printf "(%d,%s) → %s\n" m n Big_int.(string_of_big_int (f m (big_int_of_string n)))
  57. let () =
  58.   g (-1) "1";
  59.   g 1 "-1";
  60.   g (-1) "-1";
  61.   g 2 "1";
  62.   g 2 "2";
  63.   g 2 "3";
  64.   g 20 "1";
  65.   g 20 "2";
  66.   g 20 "3";
  67.   g 20 "400096";
  68.   g 20 "400097";
  69.   g 32 "1";
  70.   g 32 "2";
  71.   g 32 "3";
  72.   g 32 "1000";
  73.   g 32 "1000000";
  74.   g 32 "1000000000";
  75.   g 32 "1333610936";
  76.   g 32 "1333610937"
  77. *)
  78. let g m n =
  79. Printf.printf "(%d,%s) → %s\n" m (Big_int.string_of_big_int n) Big_int.(string_of_big_int (f m n))
  80. let () =
  81. g 2000 Big_int.(shift_left_big_int unit_big_int 1024)
  82.  
Success #stdin #stdout 0.2s 62760KB
stdin
Standard input is empty
stdout
(2000,179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216) → 133455533351454245243544545545533544533353455351551354434255345155535155553543255433542555343535545355541554555135355524435524344435532524545543525545533451134455552254455333442334354534523153425445155555435153554255552353344553554445354545514355554555554435333353123555345255242334345515554533345534425455553521553555455455545441241132525455435442255542322515454522545115515532453523344525423555455451555554153554555544451254441554323215525455545135444445535554555545425522544345243455524545444534534551543444144