2的幂次的预处理、判断数组内是否有两个数相加的和为2的幂次

题意:给你一个数列,然后让你删掉某个数,让每一个数都可以与另外一个数相加,之和等于2的^d次方,问最少删掉的个数

解题思路:

先预处理出2的次幂,然后暴力枚举出每个数与每个2的次幂之差,去判断在map中是否有这样的差存在(注意相同的两个数,比如4 ,4   这个时候就可以特判一下,出现次数),若不存在那么就需要删除掉这个数。

解题思路:

1.先弄出一个2的N次方的数组

2.hashmap记录数字是否出现,以及是否重复

3.遍历原数组,每个数字与2的N次方数组作差,用hashmap查看差值是否存在:

  • 差值小于0就不需要判断(原数组不存在负数)
  • 存在且差值和数组值不相等的,表示该数组值能找到一个数使得它等于2的M次方
  • 存在但差值和数组值相等的时候,判断hash值是否>=2(即是否有原数组重复元素),有重复值表示满足条件
  • 否则表示该值不能满足条件

AC代码:

import javax.print.DocFlavor;
import javax.swing.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.nio.file.attribute.AclEntryFlag;
import java.security.AlgorithmConstraints;
import java.sql.Struct;
import java.text.CollationElementIterator;
import java.text.DateFormatSymbols;
import java.util.*;
import java.util.stream.Collectors;


public class Main
{
    static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static int N = (int)12e4 + 10;
    static math_myself math_me = new math_myself();
    static int a[] = new int[N];
    static long d[] = new long[32];
    static Map<Integer, Integer> map = new HashMap<>(); // 存每个数操作次数

    // 2次幂的预处理
    static void init_2_pow()
    {
        d[0] = 1;
        for(int i = 1 ; i <= 31 ; i ++)  d[i] = d[i - 1]*2;
    }

    public static void main(String[] args ) throws IOException
    {
        init_2_pow();
        int cnt = 0;

        int n = rd.nextInt();
        for (int i = 0; i < n; i++)
        {
            a[i] = rd.nextInt();
            if (map.get(a[i]) == null) map.put(a[i], 1);
            else map.put(a[i], map.getOrDefault(a[i], 0) + 1);
        }

        int j;
        // 枚举每一个a[i]
        for (int i = 0; i < n; i++)
        {
            // 枚举每一个指数
            for (j = 0; j <= 31; j++)
            {
                 int k = (int) (d[j] - a[i]); // 2次幂与a[i]的差值
                 if(k <= 0)  continue; // 2次幂太小,不足以构成两个数的和
                 if(k == a[i] && map.get(k) >= 2)  break; // 两个数是一样的,比满足两数之和是2次幂
                 if(k != a[i] && map.get(k) != null)  break; // 每个数不一样,a[j]也存在于哈希表中,即原数组
            }
            if(j == 32)  cnt ++;
        }

        pw.println(cnt);
        pw.flush();
    }

}

class rd
{
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tokenizer = new StringTokenizer("");

    static String nextLine() throws IOException   { return reader.readLine(); }

    static String next() throws IOException
    {
        while (!tokenizer.hasMoreTokens())  tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException  { return Integer.parseInt(next()); }

    static double nextDouble() throws IOException { return Double.parseDouble(next()); }

    static long nextLong() throws IOException  { return Long.parseLong(next());}

    static BigInteger nextBigInteger() throws IOException
    {
        BigInteger d = new BigInteger(rd.nextLine());
        return d;
    }
}

class PII
{
    int x,y;
    public PII(int x ,int y)
    {
        this.x = x;
        this.y = y;
    }
}

class math_myself
{
    int gcd(int a,int b)
    {
        if(b == 0)  return a;
        else return gcd(b,a % b);
    }

    int lcm(int a,int b)
    {
        return a * b / gcd(a, b);
    }

    // 求n的所有约数
    List get_factor(int n)
    {
        List<Long> a = new ArrayList<>();
        for(long i = 1; i <= Math.sqrt(n) ; i ++)
        {
            if(n % i == 0)
            {
                a.add(i);
                if(i != n / i)  a.add(n / i);  // // 避免一下的情况:x = 16时,i = 4 ,x / i = 4的情况,这样会加入两种情况  ^-^复杂度能减少多少是多少
            }
        }

        // 相同因子去重,这个方法,完美
        a = a.stream().distinct().collect(Collectors.toList());

        // 对因子排序(升序)
        Collections.sort(a);

        return a;
    }

    // 判断是否是质数
    boolean check_isPrime(int n)
    {
        if(n < 2) return false;
        for(int i = 2 ; i <= n / i; i ++)  if (n % i == 0) return false;
        return true;
    }
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

21RGHLY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值