Scanf.scanf "%d" (fun n ->
    let module M = Map.Make (struct type t = int let compare = compare end) in
    let add key v map = M.add key (v + try M.find key map with _ -> 0) map in
    let dec key map =
        let v = M.find key map in
        if v = 1 then M.remove key map else M.add key (v - 1) map
    in
    let rec loop i map =
        if i = n then map else
            let map = Scanf.scanf " %d" (fun a -> add a 1 map) in
            loop (i + 1) map
    in
    let map = loop 0 M.empty in
    let inikey k =
        let rec loop i =
            if i > k then i else loop (i + i)
        in
        loop 1
    in
    let rec loop map acc =
        if M.is_empty map then acc else
            let (k, v) = M.max_binding map in
            let map = dec k map in
            let ik = inikey k in
            if M.mem (ik - k) map then (
                let map = dec (ik - k) map in
                loop map (acc + 1)
            ) else loop map acc
    in
    loop map 0 |> Printf.printf "%d\n"
)