牛客小白月赛5 A 无关(relationship)

本文介绍了一种算法,用于计算在给定区间[L,R]内,与特定集合A中所有元素(均为素数)无关的正整数的数量。通过使用容斥原理,文章提供了一个有效的方法来解决这个问题,并给出了两种实现方案:一种是基于位运算的遍历,另一种是深度优先搜索(DFS)的方法。

链接:https://ac.nowcoder.com/acm/contest/135/A
来源:牛客网

题目描述
若一个集合A内所有的元素都不是正整数N的因数,则称N与集合A无关。

给出一个含有k个元素的集合A={a1,a2,a3,…,ak},求区间[L,R]内与A无关的正整数的个数。
保证A内的元素都是素数。
输入描述:

输入数据共两行:
第一行三个正整数L,R,k,意义如“题目描述”。
第二行k个正整数,描述集合A,保证k个正整数两两不相同。

输出描述:

输出数据共一行:
第一行一个正整数表示区间[L,R]内与集合A无关的正整数的个数

示例1

输入

1 10 4
2 3 5 7

输出

1

示例2

输入

2 10 4
2 3 5 7

输出

0

说明

对于30%的数据:1<=L<=R<=10^6
对于100%的数据:1<=L<=R<=10^18,1<=k<=20,2<=ai<=100

这题可以用容斥获得与N有关的 个数
这里要注意集合A的乘积会爆 long 需要特判一下
接下来给出AC代码

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.StreamTokenizer;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
 
public class Main {
    @SuppressWarnings("deprecation")
    static StreamTokenizer st = new StreamTokenizer(new BufferedInputStream(System.in));
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long l = sc.nextLong();
        long r = sc.nextLong();
        int t = sc.nextInt();
        int rc[] = new int[t];
        for (int i = 0; i < t; i++) {
            rc[i] = sc.nextInt();
        }
        l--;
        long cont1 = 0;
        for (int i = 1; i < (1 << t); i++) {
            int cont = 0;
            long te = 1;
            for (int j = 0; j < t; j++) {
                if (((i) & (1 << j)) != 0) {
                    cont++;
                    te *= rc[j];
                    if(te>r) {
                        break;
                    }
                }
            }
            if ((cont & 1) == 1) {
                cont1 -= l / te;
                cont1 += r / te;
            } else {
                cont1 += l / te;
                cont1 -= r / te;
            }
 
        }
        System.out.println(r-l-cont1);
    }
}

另外还有一种dfs容斥的方法

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.StreamTokenizer;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;

public class Main {
	@SuppressWarnings("deprecation")
	static StreamTokenizer st = new StreamTokenizer(new BufferedInputStream(System.in));
	static long L, R, ans;
	static int num, k, A[] = new int[30];

	static void dfs(int p, long pro, int n) {
		if (p > k && n != 0) {
			if ((n & 1) != 0)
				ans += R / pro - (L - 1) / pro;
			else
				ans += (L - 1) / pro - R / pro;
		}
		if (p <= k) {
			dfs(p + 1, pro, n);
			if (R / pro >= A[p])
				dfs(p + 1, pro * A[p], n + 1);
		}
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		L = sc.nextLong();
		R = sc.nextLong();
		k = sc.nextInt();
		for (int i = 1; i <= k; i++) {
			A[i] = sc.nextInt();
		}
		dfs(1, 1, 0);
		System.out.println(R - L + 1 - ans);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值