http://codeforces.com/contest/1005/problem/C
解题思路:
1.先弄出一个2的N次方的数组
2.hashmap记录数字是否出现,以及是否重复(这里我令重复的hash值都为2)
3.遍历原数组,每个数字与2的N次方数组作差,用hashmap查看差值是否存在:
- 差值小于0就不需要判断(原数组不存在负数)
- 存在且差值和数组值不相等的,表示该数组值能找到一个数使得它等于2的M次方
- 存在但差值和数组值相等的时候,判断hash值是否为2(即是否有原数组重复元素),有重复值表示满足条件
- 否则表示该值不能满足条件
import java.util.HashMap;
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
int[] good = new int[120000 + 10];
long[] po = new long[35];
po[0] = 1;
for(int i = 1;i <= 32;i++) {
po[i] = po[i-1] * 2;
}
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
HashMap<Integer,Integer> m = new HashMap<>();
for(int i = 0;i < n;i++) {
good[i] = sc.nextInt();
if (m.containsKey(good[i])) {
m.remove(good[i]);
m.put(good[i],2);
} else m.put(good[i],1);
}
int cnt = 0,j;
for(int i = 0;i < n;i++) {
for(j = 0;j <= 30;j++) {
int x = (int) (po[j] - good[i]);
if (x <= 0)
continue;
if (x == good[i] && m.get(good[i]) == 2)
break;
if (x != good[i] && m.get(x) != null)
break;
}
if(j == 31)
cnt++;
}
System.out.println(cnt);
}
}