ZOJ 3233 Lucky Number 容斥原理
2017-07-31 20:40
363 查看
Lucky NumberTime Limit: 5 Seconds Memory Limit: 32768 KB
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
Sample Output
Hint
The lucky numbers in the sample are 72, 74, 76, 78, 81.
[b]题意:[/b]
给你两个数组,和一个范围[L,R],找出其中 能至少被A数组中一个数整除 和 不能被B数组中至少一个数整除 的数的个数
[b]题解:[/b]
将第二个条件转化为,全被B数组中的数整除
定义第一个条件为P1,第二个条件为P2。
则要求的是P1&&P2。由于P2条件比较奇怪,至少不能被一个数整除,所以我们考虑其反命题,能被第二组中的所有数整除,则是能被最小公倍数整除。
那么P1&&P2=P1-P1&&(~P2),这样便可以解决了。
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.
[b]题意:[/b]
给你两个数组,和一个范围[L,R],找出其中 能至少被A数组中一个数整除 和 不能被B数组中至少一个数整除 的数的个数
[b]题解:[/b]
将第二个条件转化为,全被B数组中的数整除
定义第一个条件为P1,第二个条件为P2。
则要求的是P1&&P2。由于P2条件比较奇怪,至少不能被一个数整除,所以我们考虑其反命题,能被第二组中的所有数整除,则是能被最小公倍数整除。
那么P1&&P2=P1-P1&&(~P2),这样便可以解决了。
#include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define ls i<<1 #define rs ls | 1 #define mid ((ll+rr)>>1) #define pii pair<int,int> #define MP make_pair typedef long long LL; const long long INF = 1e18+1LL; const double pi = acos(-1.0); const int N = 1e4+10, M = 1e3+20,inf = 2e9; const LL mod = 1e9+7LL; LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; } LL a ,b ,l,r; LL solve(LL x,int n,int m) { if(x <= 0) return 0; LL ret = 0; for(int i = 1; i < (1<<n); ++i) { int num = 0; LL tmp = 1; int ok = 1; for(int j = 0; j < n; ++j) { if((1<<j)&i) { if(INF / a[j+1] < tmp/gcd(tmp,a[j+1])) {ok = 0;break;} num++,tmp = tmp/gcd(tmp,a[j+1])*a[j+1]; } } if(!ok) continue; if(num & 1) { ret += x/tmp; } else ret -= x/tmp; } LL tmp1 = 1; for(int i = 1; i <= m; ++i) { if(INF / b[i] < tmp1/gcd(tmp1,b[i])) { tmp1 = -1; break; } tmp1 = tmp1/gcd(tmp1,b[i])*b[i]; } for(int i = 1; i < (1<<n); ++i) { int num = 0; LL tmp = tmp1; if(tmp == -1) break; int ok = 1; for(int j = 0; j < n; ++j) { if((1<<j)&i) { if(INF / a[j+1] < tmp/gcd(tmp,a[j+1])) {ok = 0;break;} num++,tmp = tmp/gcd(tmp,a[j+1])*a[j+1]; } } if(!ok) continue; if(num & 1) { ret -= x/tmp; } else ret += x/tmp; } return ret; } int n,m; int main() { while(scanf("%d%d%lld%lld",&n,&m,&l,&r)!=EOF) { if(!n&&!m&&!l&&!r) return 0; for(int i = 1; i <= n; ++i) scanf("%lld",&a[i]); for(int i = 1; i <= m; ++i) scanf("%lld",&b[i]); printf("%lld\n",solve(r,n,m) - solve(l-1,n,m)); } return 0; }
相关文章推荐
- 【ZOJ】3233【容斥原理】【Lucky Number】
- ZOJ 3233 Lucky Number --容斥原理
- ZOJ 3233 Lucky Number(容斥原理)
- ZOJ 3233 Lucky Number(数论,容斥原理)
- zoj 3233 Lucky Number(容斥原理)
- ZOJ 3233 Lucky Number
- ZOJ 3233 Lucky Number 容斥
- zoj 3233 容斥原理 + 双条件
- ZJU 3233 - Lucky Number (容斥原理)
- 【ZOJ】3233 Lucky Number
- [容斥原理] zoj 2836 Number Puzzle
- zoj 2836 容斥原理
- ACM学习历程—ZOJ 3868 GCD Expectation(莫比乌斯 || 容斥原理)
- [容斥原理] zoj 3556 How Many Sets I
- zoj 3547(容斥原理)
- ZOJ 1442 Dinner Is Ready 容斥原理 + java大数
- zoj 3556 How Many Sets I 解题报告 <容斥原理>
- ZOJ-3687 The Review Plan I(容斥原理,禁位排列,dfs)
- zoj 3868 GCD Expectation(容斥原理)
- zoj 3547 - The Boss on Mars(容斥原理)