fork download
  1. let (<<) f g x = f (g x)
  2. let tally xs =
  3. let h = Hashtbl.create 32 in
  4. let increment key = match Hashtbl.find_opt h key with
  5. None -> Hashtbl.add h key 1
  6. | Some count -> Hashtbl.replace h key (count + 1)
  7. in
  8. let () = List.iter increment xs in h
  9. let factrial =
  10. let rec aux acc = function
  11. 0 -> acc
  12. | n -> aux (acc * n) (n - 1)
  13. in aux 1
  14. let n_permutations xs =
  15. factrial (List.length xs) / Hashtbl.fold (fun k v acc -> acc * factrial v) (tally xs) 1
  16. let count f =
  17. List.fold_left (fun acc x -> acc + if f x then 1 else 0) 0
  18. let f =
  19. let rec aux acc = function
  20. [] -> acc
  21. | x :: xs as ys -> aux (acc + count ((>) x) xs * n_permutations ys / List.length ys) xs
  22. in aux 1
  23. let g = print_newline << print_int << f
  24. let () =
  25. g [1;2;3;4;5;6;7;8;9];
  26. g [1;2;3;4;5;6;7;9;8];
  27. g [1;2;3;4;5;6;8;7;9];
  28. g [4;1;6;5;8;9;7;3;2];
  29. g [6;8;4;7;5;3;2;1;9];
  30. g [9;8;7;6;5;4;3;2;1];
  31. g [1;1;1;2;2;2;3;3;3;4;4;4];
  32. g [1;1;1;2;2;2;3;3;4;3;4;4];
  33. g [1;1;1;2;2;2;3;3;4;4;3;4];
  34. g [2;2;2;3;3;1;4;3;4;1;1;4];
  35. g [3;2;4;4;2;4;3;3;1;1;1;2];
  36. g [4;4;4;3;3;3;2;2;2;1;1;1];
  37. g [3;1;4;1;5;9];
  38.  
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
1
2
3
123456
234567
362880
1
2
3
123456
234567
369600
127