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 C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)


#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------
ll V;
const ll MAX_V = 400100;
vector<int> G[MAX_V];
ll match[MAX_V];
bool used[MAX_V];
void add_edge(ll u, ll v){
    G[u].push_back(v);
    G[v].push_back(u);
}
bool dfs(ll v){
    used[v] = true;
    ll i;
    FOR(i,G[v].size()){
        ll u = G[v][i], w = match[u];
        if(w<0 || !used[w] && dfs(w)){
            match[v] = u;
            match[u] = V;
            return true;
        }
    }
    return false;
}
ll bimatching(){
    ll res = 0;
    memset(match, -1, sizeof(match));
    ll v;
    FOR(v,V){
        memset(used, 0, sizeof(used));
        if(dfs(v)){
            res++;
        }
    }
    return res;
}
void solve() {
    ll i,j,k,l,r,x,y; string s;
    bool exit = false;
    ll N;
    cin>>N;
    V=N*2;
    vector<ll> A(N);
    FOR(i,N){
        cin>>A[i];
    }
    sort(ALL(A));
    vector<ll> pow_vec;
    x=1;
    while(x<=2*1000000000){
        pow_vec.push_back(x);
        //cout<<x<<endl;
        x *= 2;
    }
    


    FOR(j, N){
        y=A[j];
        FORR(x, pow_vec){
            ll obj = x - y;
            if(obj<0 || obj>1000000000){
                continue;
            }
            auto beg = A.begin();
            ll ub = ll(upper_bound(ALL(A),obj) - beg);
            ll lb = ll(lower_bound(ALL(A),obj) - beg);
            if(lb >= N) {
                continue;
            }
            if(ub - lb>0){
                for(i=lb; i<ub ;i++){
                    if(i==j) continue;
                    add_edge(i,j+N);
                    //cout<<A[i]<<"+"<<A[j]<<endl;
                }
                //cout<<"x:"<<x<<" y:"<<y<<" num:"<<ll(ub-lb)<<endl;
            }
        }
    }

    cout<<bimatching() / 2<<endl;
    //cout<<ret/2<<endl;
    /*cin>>N;
    vector<ll> V((N+1)*(N+1));
    FOR(x,N+1) {
            FOR(y,N+1) {
                    ll ret=1;
                    FOR(i,x) ret*=2;
                    FOR(i,y) ret*=5;
                    V.push_back(ret);
            }
    }
    sort(ALL(V));
    FORR(v,V) cout<<v<<endl;*/
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}