ZOJ 3233(45/600)
2017-07-17 16:08
281 查看
Watashi loves M mm very much. One day, M mm gives Watashi a chance to choose a number between low and high, and if the choosen number is lucky, M mm will marry him.
M mm has 2 sequences, the first one is BLN (Basic Lucky Numbers), and the second one is BUN (Basic Unlucky Numbers). She says that a number is lucky if it’s divisible by at least one number from BLN and not divisible by at least one number from BUN.
Obviously, Watashi doesn’t know the numbers in these 2 sequences, and he asks M mm that how many lucky number are there in [low, high]?
Please help M mm calculate it, meanwhile, tell Watashi what is the probability that M mm marries him.
Input
The first line of each test case contains the numbers NBLN (1 <= NBLN <= 15), NBUN (1 <= NBUN <= 500), low, high (1 <= low <= high <= 1018).
The second and third line contain NBLN and NBUN integers, respectively. Each integer in sequences BLN and BUN is from interval [1, 32767].
The last test case is followed by four zero.
The input will contain no more than 50 test cases.
Output
For each test case output one number, the number of lucky number between low and high.
Sample Input
2 1 70 81
2 3
5
0 0 0 0
Sample Output
5
Hint
The lucky numbers in the sample are 72, 74, 76, 78, 81.
这个题是稍微有点变形了
所有的结果都要去掉减第二个数组lcm的数量
所以每次容斥的时候我都带着
M mm has 2 sequences, the first one is BLN (Basic Lucky Numbers), and the second one is BUN (Basic Unlucky Numbers). She says that a number is lucky if it’s divisible by at least one number from BLN and not divisible by at least one number from BUN.
Obviously, Watashi doesn’t know the numbers in these 2 sequences, and he asks M mm that how many lucky number are there in [low, high]?
Please help M mm calculate it, meanwhile, tell Watashi what is the probability that M mm marries him.
Input
The first line of each test case contains the numbers NBLN (1 <= NBLN <= 15), NBUN (1 <= NBUN <= 500), low, high (1 <= low <= high <= 1018).
The second and third line contain NBLN and NBUN integers, respectively. Each integer in sequences BLN and BUN is from interval [1, 32767].
The last test case is followed by four zero.
The input will contain no more than 50 test cases.
Output
For each test case output one number, the number of lucky number between low and high.
Sample Input
2 1 70 81
2 3
5
0 0 0 0
Sample Output
5
Hint
The lucky numbers in the sample are 72, 74, 76, 78, 81.
这个题是稍微有点变形了
所有的结果都要去掉减第二个数组lcm的数量
所以每次容斥的时候我都带着
#include<bits/stdc++.h> using namespace std; typedef unsigned long long l; l tu[50]; l n,m,z,y,zs; l gcd(l q,l w) { if(!w)return q; return gcd(w,q%w); } l lcm(l q,l w) { l we=w/gcd(q,w); return q*we; } l dfs(l mule,l cs,l lc,l tt) { if(mule>n)return 0; l daan=0; for(int a=mule;a<=n;a++) { daan+=cs/lcm(tu[a],lc)-cs/lcm(lcm(tu[a],lc),tt)-dfs(a+1,cs,lcm(tu[a],lc),tt); } return daan; } l jju(l w,l tt) { l daan=0; for(int a=1;a<=n;a++) { daan+=w/tu[a]-w/lcm(tu[a],tt)-dfs(a+1,w,tu[a],tt); } return daan; } int main() { while(cin>>n>>m>>z>>y) { if(n==0&&m==0&&z==0&&y==0)break; for(l a=1;a<=n;a++)cin>>tu[a]; l tt=1; for(l a=1;a<=m;a++) { cin>>zs; tt=lcm(tt,zs); } cout<<jju(y,tt)-jju(z-1,tt)<<endl; } }
相关文章推荐
- 【ZOJ】3233 Lucky Number
- zoj 3233 Lucky Number(容斥原理)
- ZOJ 3233 Lucky Number(数论,容斥原理)
- ZOJ 3233
- ZOJ 3233 Lucky Number --容斥原理
- ZOJ 3233 Lucky Number 容斥
- ZOJ 3233 Lucky Number(容斥原理)
- zoj 3233 容斥原理 + 双条件
- ZOJ 3233 Lucky Number 容斥原理
- ZOJ 3233 Lucky Number
- 剑指Offer 面试题45:圆圈中最后剩下的数字(约瑟夫环问题,ZOJ 1088:System Overload类似)题解
- 【ZOJ】3233【容斥原理】【Lucky Number】
- [ZOJ 1008]Gnome Tetravex (dfs搜索 + 小优化)
- poj 3233(矩阵快速幂+二分)
- zoj 2545
- ZOJ 3725 Painting Storages 解题报告
- ZOJ 1081 Within(点是否在多边形内)| 计算几何
- POJ 3132 & ZOJ 2822 Sum of Different Primes(dp)
- ZOJ 3935 2016
- ZOJ - 2974 Just Pour the Water