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 = 2x).
The first line contains the single positive integer n (1 ≤ n ≤ 105) — the number of integers.
The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Print the number of pairs of indexes i, j (i < j) that ai + aj is a power of 2.
4
7 3 2 1
2
3
1 1 1
3
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer.
题目链接:http://codeforces.com/contest/702/problem/B
2^32是大于1e9的,n^2的复杂度,内层用2的幂次单独判断,用map处理每个数出现的次数。最后+1,开始时候的判断只会判断已经加入map中的数,所以不会出现重复的情况。
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <map> 5 using namespace std; 6 map<long long, long long>m; 7 int main() 8 { 9 int n; 10 cin >> n; 11 long long t; 12 long long ans=0; 13 for(int i=0;i<n;i++) 14 { 15 cin >> t; 16 for(int j = 0; j < 32; j++) 17 { 18 ans+=m[(2<<j)-t]; 19 } 20 m[t]++; 21 } 22 cout << ans; 23 return 0; 24 }