open Big_int_Z module BISet = Set.Make( struct let compare = compare_big_int type t = big_int end ) let ( +! ) = add_big_int let ( %! ) = mod_big_int let () = let arr = Array.make 2 zero_big_int in Scanf.scanf "%s %s\n" (fun n d -> arr.(0) <- big_int_of_string n; arr.(1) <- big_int_of_string d); let n, d = arr.(0), arr.(1) in let rec count_pyon pos hist count = let next_pos, next_hist = (pos +! d) %! n, BISet.add pos hist in if BISet.mem next_pos next_hist then string_of_big_int count else let next_count = count +! unit_big_int in count_pyon next_pos next_hist next_count in count_pyon zero_big_int BISet.empty zero_big_int |> Printf.printf "%s\n"