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: Runtime Error)

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define mod 998244353LL
#define eps 1e-13
// #define PI 3.141592653589793238L
#define INF 1000000011
#define INFLL 1000000000000000011LL
#define vi vector<int>
#define vll vector<long long>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pil pair<int, long long>
#define pli pair<long long, int>
#define mp make_pair
#define F first
#define S second
#define pb push_back
#define fo(i,a,n) for(i = (a); i < (n); i++)
#define gtl(x) getline(cin, (x))
#define flsh fflush(stdout)
#define sws ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define gcd __gcd
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)((a).size())
#define io_file freopen("D:/Coding Problems/Contest/input_file.in", "r", stdin); freopen("D:/Coding Problems/Contest/output_file.out", "w", stdout)

ll modx(ll Base, ll exponent)
{
	ll ans = 1;
	if(Base == 1)
		return Base;
	while(exponent)
	{
		if(exponent & 1)
			ans = (ans * Base)%mod;
		Base = (Base * Base)%mod;
		exponent = exponent >> 1;
	}
	return ans;
}

ll inmodx(ll num)
{
	return (modx(num, mod-2LL));
}

bool cmp(pair < pii, int > a, pair < pii, int > b)//true for a before b
{
	if(a.F.F/300 < b.F.F/300)
		return 1;
	if(a.F.F/300 > b.F.F/300)
		return 0;
	if(a.F.S < b.F.S)
		return 1;
	if(a.F.S > b.F.S)
		return 0;
	if(a.F <= b.F)
		return 1;
	return 0;
}

const int N = (2e5) + 9;
const int M = (N<<2) + 9;
const int LOGN = ((int)log2(N)) + 3;
const int LOGM = ((int)log2(M)) + 3;
const int BUCK = 2*550;
const int SQRT = BUCK+9;

const int alpha = 32;

ll a[N];
int cnt[N], ps_cnt[N];
map < ll, int > mapp;
vll arr;
vi v[N];
ll p2[N];
int deg[N];
set < int > temp;
queue < int > q;
bool mark[N];

void perform(int n, int &ans)
{
	int i, val, z;
	fo(i,1,n+1)
	{
		deg[i] = sz(v[i]);
		mark[i] = 0;
	}
	while(!q.empty())
		q.pop();

	z = deg[1];
	fo(i,1,n+1)
		z = min(z, deg[i]);
	z = max(z,1);

	fo(i,1,n+1)
		if(deg[i] == z)
		{
			mark[i] = 1;
			q.push(i);
		}

	while(!q.empty())
	{
		z = q.front();
		q.pop();
		for(auto x : v[z])
		{
			val = min(cnt[z],cnt[x]);
			ans += val;
			cnt[z] -= val;
			cnt[x] -= val;
			if(!mark[x])
			{
				mark[x] = 1;
				q.push(x);
			}
		}
	}
	return;
}

void solve()
{
	int n, i, j, ans = 0;
	p2[0] = 1;
	fo(i,1,alpha+1)
		p2[i] = p2[i-1]+p2[i-1];
	cin >> n;
	fo(i,1,n+1)
	{
		cin >> a[i];
		temp.insert(a[i]);
	}
	arr.pb(0);
	for(auto x : temp)
		arr.pb(x);
	fo(i,1,sz(arr))
		mapp[arr[i]] = i;
	fo(i,1,n+1)
		cnt[mapp[a[i]]]++;

	n = sz(arr)-1;
	fo(i,1,n+1)
		fo(j,0,alpha+1)
			if(mapp[p2[j]-arr[i]] && p2[j]-arr[i] != arr[i])
				v[i].pb(mapp[p2[j]-arr[i]]);
	fo(i,1,n+1)
		if((arr[i]&(arr[i]-1LL)) == 0LL)
		{
			ps_cnt[i] = cnt[i];
			cnt[i] = 0;
		}
	assert(false);
	perform(n,ans);
	fo(i,1,n+1)
		if(ps_cnt[i] != 0)
			cnt[i] = ps_cnt[i];
	perform(n,ans);
	fo(i,1,n+1)
		if(cnt[i] > 0 && (arr[i]&(arr[i]-1LL)) == 0LL)
			ans += (cnt[i]/2);
	cout << ans << '\n';
	return;
}

int main()
{
	sws;
	clock_t clk;
	clk = clock();
	// io_file;
	// srand (time(NULL));

	//Code here
	int t = 1, cs;
	cout << fixed << setprecision(9);
	// cin >> t;
	fo(cs,1,t+1)
	{
		// cout << "Case #" << cs << ": ";
		solve();
	}
	// Code ends here

	clk = clock() - clk;
	cerr << fixed << setprecision(6) << "Time: " << ((double)clk)/CLOCKS_PER_SEC << "\n";
	return 0;
}