ZOJ 3233 Lucky Number 容斥

给你a数组和b数组 求x到y之间有多少个数至少被a中一个数整除并且至少不被b中一个数整除

容斥第一问很简单 第二问可以考虑反面

设满足被a中至少一个数整除的数有sum1个

在被a中至少一个数整除的前提下 被b中所有数整除的数有sum2

答案就是sum1-sum2

在dfs的时候溢出了 借鉴了某大牛的方法

#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int maxn = 510;
LL a[maxn], b[maxn];
LL n, m, X, Y, l;
LL gcd(LL a, LL b)
{
	return b ? gcd(b, a%b) : a;
}
LL lcm(LL a, LL b)
{
    LL g;
    g = gcd(a, b);
    if (a / g > Y / b)
        return Y + 1;
    return a / g * b;
}
void dfs(int i, LL x, int k, LL n, LL& ans1, LL& ans2)
{
	if(i == -1)
	{
		if(!k)
			return;
		LL y = lcm(x, l);
		if(k&1)
		{
			ans1 += n/x;
			ans2 += n/y;
		}
		else
		{
			ans1 -= n/x;
			ans2 -= n/y;
		}
		return;
	}
	dfs(i-1, x, k, n, ans1, ans2);
	dfs(i-1, lcm(x, a[i]), k+1, n, ans1, ans2);
}
LL cal(LL x)
{
	LL ans1 = 0;
	LL ans2 = 0;
	dfs(n-1, 1, 0, x, ans1, ans2);
	return ans1-ans2;
}
int main()
{

	while(scanf("%lld %lld %lld %lld", &n, &m, &X, &Y) != EOF)
	{
		if(!n && !m && !X && !Y)
			break;
		for(int i = 0; i < n; i++)
			scanf("%lld", &a[i]);
		for(int i = 0; i < m; i++)
			scanf("%lld", &b[i]);
		l = 1;
		for(int i = 0; i < m; i++)
			l = lcm(b[i], l);
		a[n] = l;

		LL ans = cal(Y)-cal(X-1);
		printf("%lld\n", ans);
	}
	return 0;
}
时间: 2024-12-19 15:01:05

ZOJ 3233 Lucky Number 容斥的相关文章

ZOJ 3233 Lucky Number

Lucky Number Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Original ID: 323364-bit integer IO format: %lld      Java class name: Main Watashi loves M mm very much. One day, M mm gives Watashi a chance to choose a number

ZOJ 3233 Lucky Number --容斥原理

这题被出题人给活活坑了,题目居然理解错了..哎,不想多说. 题意:给两组数,A组为幸运基数,B组为不幸运的基数,问在[low,high]区间内有多少个数:至少被A组中一个数整除,并且不被B中任意一个数整除.|A|<=15. 分析:看到A长度这么小,以及求区间内满足条件的个数问题,容易想到容斥原理,因为不被B中任意一个数整除,所以将B数组所有数取一个最小公倍数LCM,那么就变成了幸运数字都不会被这个LCM整除. 然后枚举子集,实现要将A中元素去除相互整除的情况,比如A = [2,4],这时因为被至

XTU 1242 Yada Number 容斥

Yada Number Problem Description: Every positive integer can be expressed by multiplication of prime integers. Duoxida says an integer is a yada number if the total amount of 2,3,5,7,11,13 in its prime factors is even. For instance, 18=2 * 3 * 3 is no

Lucky HDU - 5213 (莫队,容斥)

WLD is always very lucky.His secret is a lucky number . is a fixed odd number. Now he meets a stranger with numbers:.The stranger asks him questions.Each question is like this:Given two ranges and ,you can choose two numbers and to make .The you can

zoj 3688 The Review Plan II 禁位排列 棋盘多项式 容斥

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4971 题意:共有N天和N个章节,每天完成一个章节,规定第i个章节不可以在第i天或者i+1天完成(第N个章节则是第N天和第1天不能),求分配能完成所有章节的方案数. 思路: 主要还是根据棋盘多项式的公式来求解: 但是这题和ZOJ3687不同,数据量N最大有100000,因此不能爆搜,需要推一下公式. 按照题意,先求禁位组成的棋盘的棋盘多项式,再用容斥.禁位组成的棋盘如

双元素非递增(容斥)--Number Of Permutations Educational Codeforces Round 71 (Rated for Div. 2)

题意:https://codeforc.es/contest/1207/problem/D n个元素,每个元素有a.b两个属性,问你n个元素的a序列和b序列有多少种排序方法使他们不同时非递减(不同时good). 思路: 真难则反+容斥,反向考虑,ans1=如果a序列非递减则有a中各个数字出现次数的阶乘的乘积个,ans2=b序列也是一样. ans3=然后还要减去a序列和b序列都是good的方案数,就是元素相同的出现次数阶乘的乘积(注意,如果不存在双good就不算ans3). ANS就是:全排列 -

HDU 5768Lucky7(多校第四场)容斥+中国剩余定理(扩展欧几里德求逆元的)+快速乘法

地址:http://acm.hdu.edu.cn/showproblem.php?pid=5768 Lucky7 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 754    Accepted Submission(s): 279 Problem Description When ?? was born, seven crows flew

POJ2103 Jackpot(容斥+高精度)

Jackpot Time Limit: 20000MS   Memory Limit: 64000K Total Submissions: 1044   Accepted: 216 Case Time Limit: 2000MS Description The Great Dodgers company has recently developed a brand-new playing machine. You put a coin into the machine and pull the

HDU5768Lucky7(中国剩余定理+容斥定理)(区间个数统计)

When ?? was born, seven crows flew in and stopped beside him. In its childhood, ?? had been unfortunately fall into the sea. While it was dying, seven dolphins arched its body and sent it back to the shore. It is said that ?? used to surrounded by 7