Summarize to the Power of Two
For example, the following sequences are good:
For example, the following sequences are not good:
Input
The first line contains the integer n (1≤n≤120000) — the length of the given sequence.
The second line contains the sequence of integers a1,a2,…,an (1≤ai≤109).
题意:给定一个数组,问数组中每一个元素 a[i] , 是否存在另一个元素 a[j], 使 a[i] + a[j] 是 2 的次幂, i != j
思路:贪心,先打出足够的2的次幂,然后对于每一个数判断 power - a[j] 是否存在就可以了。 由于数很大,用map就可以了。本ZZ煞笔的写了一发离散化,还好过了。。。
AC代码:
#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define INT(t) int t; scanf("%d",&t)
#define LLI(t) LL t; scanf("%I64d",&t)
using namespace std;
const int maxn = 120010;
int vis[maxn];
LL power2[32];
int a[maxn];
int b[maxn];
int main()
{
power2[0] = 1;
rep(i,1,32)
power2[i] = power2[i - 1] * 2;
int n;
while(~scanf("%d",&n)){
memset(vis,0,sizeof(vis));
rep(i,0,n){ scanf("%d",&a[i]); b[i] = a[i]; }
sort(b,b + n);
sort(a,a + n);
int f = unique(b,b + n) - b;
rep(i,0,n){
int x = lower_bound(b,b + f,a[i]) - b;
//debug(x);
++ vis[x];
}
int ans = 0;
rep(i,0,n){
int x = lower_bound(b,b + f,a[i]) - b;
int y;
int flag = 0;
rep(j,0,32){
y = lower_bound(b,b + f,power2[j] - a[i]) - b;
if(b[x] + b[y] == power2[j]){
if(x != y || (x == y && vis[x] > 1)){
flag = 1;
break;
}
}
}
if(!flag){ ++ ans; }
}
printf("%d\n",ans);
}
return 0;
}