Contest 1789 - the second stage of 2019 I want to strengthen the 12th session of personal training competition

Keywords: Java

 

Title Description

Given a sequence a1..an of length n, how many positive integers within m that cannot be divided by any ai in a1..an?

 

input

Two numbers in the first row n,m
Number of n next line, a1..an

 

 

output

There is a total number, that is, how many positive integers within m that cannot be divided by any ai in a1..an.

 

sample input

Copy sample data

3 2015
4 5 6

sample output

1075

 

Tips

For 30% data, 1 ≤ m ≤ 100000
For the other 30%, n=3
For 100% data, 1 ≤ n ≤ 20, 1 ≤ m ≤ 1018, 1 ≤ ai ≤ 109

Problem solving: This is a simple tolerance and exclusion, but when two numbers take the least common multiple, it may exceed long long, so I used java. When I used java for the first time, I had a question. It was cool.

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

 

Posted by wiccan8888 on Sat, 02 Nov 2019 02:10:32 -0700