Problem:
Score : 600 points

Problem Statement

Takahashi has N balls with positive integers written on them. The integer written on the i-th ball is A_i.
He would like to form some number of pairs such that the sum of the integers written on each pair of balls is a power of 2.
Note that a ball cannot belong to multiple pairs.
Find the maximum possible number of pairs that can be formed.

Here, a positive integer is said to be a power of 2 when it can be written as 2^t using some non-negative integer t.

Constraints

1 \leq N \leq 2\times 10^5

1 \leq A_i \leq 10^9

A_i is an integer.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible number of pairs such that the sum of the integers written on each pair of balls is a power of 2.

Sample Input 1

3
1 2 3

Sample Output 1

1

We can form one pair whose sum of the written numbers is 4 by pairing the first and third balls.
Note that we cannot pair the second ball with itself.

Sample Input 2

5
3 11 14 5 13

Sample Output 2

2

Solution in Rust (1.15.1) (judge verdict: Wrong Answer)

#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), "={:?} "),*), $($a),*);
    }
}

#[allow(unused_imports)]
use std::cmp::{min, max};
use std::collections::HashMap;

fn main() {
    input!{
      n: usize,
      aa: [usize; n],
    }

    let mut pp: Vec<HashMap<usize, Vec<usize>>> = vec![];
    for i in 1..32 {
        let mut map: HashMap<usize, Vec<usize>> = HashMap::new();
        let p = 2usize.pow(i);
        for j in 0..n {
            let a = aa[j];
            if a >= p {
                continue;
            }
            let r = a%p;
            let e = map.entry(r).or_insert(vec![]);
            e.push(j);
        }
        pp.push(map);
    }

    let mut pairs = vec![(0, 0); n];
    {
        for i in 0..n {
            pairs[i].0 = i;
    }
    }

    for i in 0..n {
        let a = aa[i];
        for j in 1..32 {
            let p = 2usize.pow(j as u32);
            if a >= p {
                continue;
            }
            let r = p - a%p;
            let map = &pp[j-1];
            if let Some(vs) = map.get(&r) {
                pairs[i].1 += vs.len();
                if r == a%p {
                    pairs[i].1 -= 1;
                }
            }
        }
    }

    // for i in 0..31 {
    //     debug!(pp[i]);
    // }

    let mut used = vec![false; n];
    pairs.sort_by_key(|v| v.1);

    let mut ans = 0;
    for i in 0..n {
        let pair = &pairs[i];
        if pair.1 == 0 {
            continue;
        }
        let v1 = pair.0;
        if used[v1] {
            continue;
        }

        let a = aa[v1];
        for j in 1..32 {
            let p = 2usize.pow(j as u32);
            if a >= p {
                continue;
            }
            let r = p - a%p;
            let mut map = &mut pp[j-1];
            if map.contains_key(&r) {
                let e = map.entry(r).or_insert(vec![]);
                while e.len() > 0 {
                    if let Some(v2) = e.pop() {
                        if used[v2] {
                            continue;
                        }
                        if v2 == v1 {
                            continue;
                        }

                        used[v1] = true;
                        used[v2] = true;
                        ans += 1;
                    }
                }
            }
        }
    }
    println!("{}", ans);
}