Codeforces Round #496 (Div. 3) 解题报告 C. Summarize to the Power of Two 暴力

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);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值