fork download
  1. let solve() =
  2. let permutation k ls =
  3. let rec p a b = function
  4. | _, [] | 0, _ -> a::b
  5. | k, ls -> List.fold (fun x y -> p (y::a) x (k - 1, List.filter ((<>) y) ls)) b ls
  6. p [] [] (k, ls)
  7. let rec gcd = function x, 0 -> x | x, y -> gcd (y, x % y)
  8. let add x xs =
  9. seq { for i in 0 .. List.length xs ->
  10. [Seq.take i xs; Seq.singleton x; Seq.skip i xs] |> Seq.concat |> Seq.toList }
  11. let toNumbers x = add x >> Seq.map (List.reduce (fun x y -> x * 10 + y))
  12. let toDigits = Seq.unfold (function 0 -> None | x -> Some(x % 10, x / 10)) >> Seq.sort >> Seq.toList
  13. let digits = set [1 .. 9]
  14. [for d in 2 .. 9 do
  15. for n in 1 .. d - 1 do
  16. if gcd (d, n) = 1 then yield! [for k in 1 .. 7 -> (n, d, k)]]
  17. |> Seq.collect (fun (n, d, k) ->
  18. digits |> Set.remove n |> Set.remove d |> Set.toList |> permutation k
  19. |> Seq.collect (fun ps ->
  20. let ds = d::ps |> List.sort
  21. ps |> toNumbers n |> Seq.choose (fun nf -> if nf % n = 0 then Some(nf, nf / n * d) else None)
  22. |> Seq.filter (fun (nf, df) -> let g = gcd (df, nf) in nf / g = n && df / g = d && toDigits df = ds)))
  23.  
Success #stdin #stdout 0.04s 11184KB
stdin
Standard input is empty
stdout
Standard output is empty