Contest1789 - 2019年第二阶段我要变强个人训练赛第十二场 问题 H: 计数JS 容斥 JAVA大数

 

题目描述

给定一个长度为n的序列a1..an,求m以内的不能被a1..an中任意一个ai整除的正整数有多少个?

 

输入

第一行两个数n,m
接下来一行n个数,a1..an

 

 

输出

共一个数,即m以内的不能被a1..an中任意一个ai整除的正整数有多少个。

 

样例输入

复制样例数据

3 2015
4 5 6

样例输出

1075

 

提示

对于 30% 的数据,1≤m≤100000
对于另外 30% 的数据,n=3
对于 100% 的数据,1≤n≤20, 1≤m≤1018, 1≤ ai≤109

题解:这就是一个简单的容斥,但两个数取最小公倍数时可能会超long long所以用了下JAVA,第一次用java比赛时过题,爽

import java.math.BigInteger;
import java.text.DateFormatSymbols;
import java.util.Scanner;
 
 
 
public class Main{
    public static BigInteger m;
    public static int n;
    public static BigInteger O = new BigInteger("0");
    public static BigInteger [] a = new BigInteger[22];
    public static BigInteger ans;
    public  static void main(String[] args) {
     
        Scanner cin = new Scanner(System.in);
        n = cin.nextInt();
        m = cin.nextBigInteger();
        for(int i = 1; i <= n; i++)
            a[i] = cin.nextBigInteger();
        ans = m;
        dfs(1, BigInteger.valueOf(0), 0);
         
        System.out.println(ans);
    }
    public static void dfs(int pos, BigInteger res, int sum) {
        //System.out.println();
        if(res.compareTo(m) == 1) return;
        if(pos == n + 1 ) {
            if(sum == 0) return;
            if(sum % 2 == 0) ans=ans.add(m.divide(res));
            else ans=ans.subtract(m.divide(res));
        //  System.out.println(m.divide(res) + " " + res+" "+ans);
            return;
        }
        dfs(pos + 1, res, sum);
        BigInteger temp = new BigInteger("0");
        BigInteger t = new BigInteger("0");
        if(res.compareTo(O) == 0) temp = a[pos];
        else {
            t = res.gcd(a[pos]);
            temp =  res.multiply(a[pos]);
            temp = temp.divide(t);
        //  System.out.println(sum + " " + temp+" "+a[pos]+" "+res);
        }
         
        dfs(pos + 1, temp, sum + 1);
    }
     
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值