fork download
  1. let sumnum n =
  2. let rec repeat n a ret =
  3. if n = 0 then ret else repeat (n - 1) a (a :: ret) in
  4. let first n = repeat ((n - 1) / 9) 9 [(n - 1) mod 9 + 1; 0] in
  5. let first2 n = repeat ((n - 1) / 9) 9 [(n - 1) mod 9 + 1] in
  6. let rec next l digit sum = match l with
  7. [f; 0] -> if sum = 0 then f - 1 :: (repeat digit 0 [1; 0]) else List.rev_append (List.rev (first2 (sum - 1))) ([f + 1; 0])
  8. | f :: s :: r ->
  9. if sum + f > 0 && s < 9 then List.rev_append (List.rev (first2 (sum + f - 1))) (s + 1 :: r)
  10. else next (s :: r) (digit + 1) (sum + f)
  11. | _ -> [] in
  12. let rec format l ret = match l with
  13. f :: s :: r -> format (s :: r) (f :: ret)
  14. | _ -> ret in
  15. let rec ans f n ret =
  16. if n = 0 then ret else (fun x -> ans x (n - 1) ((format f []) :: ret)) (next f 0 0) in
  17. List.rev (ans (first n) 5 [])
  18.  
  19. let string_of_list l =
  20. let rec f l ret = match l with
  21. [] -> ret
  22. | [a] -> ret ^ a
  23. | a :: b -> f b (ret ^ a ^ ",") in
  24. (f l "[") ^ "]"
  25.  
  26. let () = Scanf.scanf "%d" (fun x -> print_string (string_of_list (List.map (List.fold_left (fun x y -> x ^ (string_of_int y)) "") (sumnum x))))
Success #stdin #stdout 0s 16176KB
stdin
12
stdout
[39,48,57,66,75]