Skip to content

Instantly share code, notes, and snippets.

@kunishi
Created July 2, 2012 03:53
Show Gist options
  • Save kunishi/3030952 to your computer and use it in GitHub Desktop.
Save kunishi/3030952 to your computer and use it in GitHub Desktop.
ACM International Collegiate Programming Contest, Asia Regional (Tokyo), 2004, Problem C
open List;;
open Int64;;
let binary_list_of_int n =
let rec binary_list_of_int n bit =
if bit = 1 then [n]
else (rem n (of_int 2)) :: binary_list_of_int (div n (of_int 2)) (bit-1)
in
binary_list_of_int n 32;;
let get_key l =
let n9 = binary_list_of_int (hd (rev l)) in
let binl = map binary_list_of_int (tl (rev l)) in
let one_nums l = fold_right add (map hd l) zero
in
let rec get_key_1 binl n n9 =
match n9 with
[] -> []
| x :: xs ->
let m = one_nums binl in
let binl_xs = map tl binl in
if rem (add m n) (of_int 2) = x
then
zero :: get_key_1 binl_xs (div (add m n) (of_int 2)) xs
else
one :: get_key_1 binl_xs
(div (add (sub (of_int 8) m) n) (of_int 2)) xs
in
of_string ("0b" ^
(fold_right (^)
(rev (map to_string (get_key_1 binl zero n9))) ""));;
let get_text filename =
let input = open_in filename in
let s = ref (int_of_string (input_line input)) in
try
while !s > 0 do
let rec get_next_n_list input l =
match length l with
9 -> l
| n ->
get_next_n_list input
(append l
(Str.split (Str.regexp_string " ") (input_line input)))
in
Printf.printf "%Lx\n"
(get_key
(map
(fun s -> of_string ("0x" ^ s))
(get_next_n_list input [])));
s := !s - 1;
done
with
End_of_file -> ();;
Arg.parse [] (fun s -> get_text (s)) ("");;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment