This version of mine goes through a time limit (but still awfully slow ~ 14 seconds):
open System open System.IO // need to change standard buffer, not to add an additional one let stream = new StreamReader(Console.OpenStandardInput(4096)) let stdin = Seq.unfold (fun s -> if s = null then None else Some (s,stream.ReadLine())) <| stream.ReadLine() let inline s2i (s : string) = Array.fold (fun ad -> a*10u + (uint32 d - uint32 '0') ) 0u <| s.ToCharArray() let calc = let fl = Seq.head stdin let [|_;ks|] = fl.Split(' ') let k = uint32 ks Seq.fold (fun as -> if (s2i s) % k = 0u then a+1 else a) 0 <| Seq.skip 1 stdin printf "%A" calc
Despite the fact that the bottleneck of this version is actually converted to string -> uint32 (the standard output of uint32 from a string is even slower), the reading itself takes about 2 seconds (versus 6 seconds of total time) on my input example (file ~ 100 M) - still not a great result. When s2i rewritten in an imperative style, the total execution time can be reduced to 10 seconds by spoj:
let inline s2i (s : string) = let mutable a = 0u for i in 0..s.Length-1 do a <- a*10u + uint32 (s.Chars(i)) - uint32 '0' a
source share