let sumnum n =
let rec repeat n a ret =
if n = 0 then ret else repeat (n - 1) a (a :: ret) in
let first n = repeat ((n - 1) / 9) 9 [(n - 1) mod 9 + 1; 0] in
let first2 n = repeat ((n - 1) / 9) 9 [(n - 1) mod 9 + 1] in
let rec next l digit sum = match l with
[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]) | f :: s :: r ->
if sum
+ f
> 0 && s
< 9 then List.rev_append
(List.rev
(first2
(sum
+ f
- 1))) (s
+ 1 :: r
) else next (s :: r) (digit + 1) (sum + f)
| _ -> [] in
let rec format l ret
= match l
with f
:: s
:: r
-> format (s
:: r
) (f
:: ret
) | _ -> ret in
let rec ans f n ret =
if n
= 0 then ret
else (fun x
-> ans x
(n
- 1) ((format f
[]) :: ret
)) (next f
0 0) in List.rev
(ans
(first n
) 5 [])
let string_of_list l =
let rec f l ret = match l with
[] -> ret
| [a] -> ret ^ a
| a :: b -> f b (ret ^ a ^ ",") in
(f l "[") ^ "]"
bGV0IHN1bW51bSBuID0gCglsZXQgcmVjIHJlcGVhdCBuIGEgcmV0ID0gCgkJaWYgbiA9IDAgdGhlbiByZXQgZWxzZSByZXBlYXQgKG4gLSAxKSBhIChhIDo6IHJldCkgaW4KCWxldCBmaXJzdCBuID0gcmVwZWF0ICgobiAtIDEpIC8gOSkgOSBbKG4gLSAxKSBtb2QgOSArIDE7IDBdIGluCglsZXQgZmlyc3QyIG4gPSByZXBlYXQgKChuIC0gMSkgLyA5KSA5IFsobiAtIDEpIG1vZCA5ICsgMV0gaW4KCWxldCByZWMgbmV4dCBsIGRpZ2l0IHN1bSA9IG1hdGNoIGwgd2l0aAoJCVtmOyAwXSAtPiBpZiBzdW0gPSAwIHRoZW4gZiAtIDEgOjogKHJlcGVhdCBkaWdpdCAwIFsxOyAwXSkgZWxzZSBMaXN0LnJldl9hcHBlbmQgKExpc3QucmV2IChmaXJzdDIgKHN1bSAtID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