codeforces-702B-Powers of Two
2016-07-30 20:34
941 查看
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).
Input
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).
Output
Print the number of pairs of indexes i, j (i < j) that ai + aj is a power of 2.
Examples
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
Note
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.
这道题在做教育场的时候并没有想到怎么做,一直T14,后来做完之后才听别人说的,大概就是数据范围内2的幂,只有30个,然后二分去找它减一减就好了
Input
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).
Output
Print the number of pairs of indexes i, j (i < j) that ai + aj is a power of 2.
Examples
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
Note
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.
这道题在做教育场的时候并没有想到怎么做,一直T14,后来做完之后才听别人说的,大概就是数据范围内2的幂,只有30个,然后二分去找它减一减就好了
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; typedef long long ll; const int maxn = 1e5 + 10; int n; ll a[maxn]; ll p[40]; int main() { while(scanf("%d",&n)!=EOF) { for(int i = p[0] = 1; i <= 32; i++) p[i] = p[i-1] *2; ll ans = 0; for(int i = 0; i < n; i++) scanf("%I64d", &a[i]); sort(a, a + n); for(int i = 0; i < n; i++) for(int j = 1; j <= 31; j++){ ll tmp = p[j] - a[i]; if(tmp > 0) ans += upper_bound(a + i + 1, a + n, tmp) - lower_bound(a + i + 1, a + n, tmp); } printf("%I64d\n", ans); } return 0; }
相关文章推荐
- 【CodeForces】702B - Powers of Two(二分)
- CodeForces 702B: Powers of Two(遍历、穷举)
- 【Codeforces】-702B-Powers of Two(二分)
- Codeforces 702B - Powers of Two
- codeforces 305 C. Ivan and Powers of Two
- CodeForces 305C--Ivan and Powers of Two (STL:set)
- codeforces 305 C. Ivan and Powers of Two
- Codeforces 702 B. Powers of Two(二分)
- codeforces-Powers of Two(map的应用)
- coderforces 702B - Powers of Two(二分)
- CodeForces 305C Ivan and Powers of Two【set】
- Educational Codeforces Round 15, problem: (B) Powers of Two
- CF_305_C. Ivan and Powers of Two
- Codeforces-239A-Two Bags of Potatoes
- (CodeForces - 600D)Area of Two Circles' Intersection
- Ivan and Powers of Two
- Educational Codeforces Round 15_B. Powers of Two
- Ivan and Powers of Two
- Educational Codeforces Round 15 B. Powers of Two (数学)
- Educational Codeforces Round 15 B. Powers of Two(暴力)