Educational Codeforces Round 15 B. Powers of Two(暴力)

题目链接: 传送门

Powers of Two

time limit per test:3 second     memory limit per test:256 megabytes

Description

You are given n integers a1, a2, ..., an. Find the number of pairs of indexes i, j (i < j) that ai + aj is a power of 2 (i. e. some integer x exists so that ai + aj = 2^x).

Input

The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 10^9).

Output

Print the number of pairs of indexes i, j (i < j) that ai + aj is a power of 2.

Sample Input

4
7 3 2 1

3
1 1 1

Sample Output

2

3

解题思路

注意到231次方就能达到109级别,所以枚举的时候枚举2的幂次方而不是枚举N,枚举N果断超时。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
typedef __int64 LL;
int main()
{
	int N;
	while (~scanf("%d",&N))
	{
		LL tmp,res = 0;
		map<LL,LL>mp;
		for (int i = 0;i < N;i++)
		{
			scanf("%I64d",&tmp);
			for (int j = 1;j < 32;j++)
			{
				if (mp.find((1<<j)-tmp) != mp.end())
				{
					res += mp[(1<<j)-tmp];
				}
			}
			mp[tmp]++;
		}
		printf("%I64d\n",res);
	}
	return 0;
}
posted @ 2016-07-30 09:22  zxzhang  阅读(238)  评论(0)    收藏  举报