ZOJ 3233 Lucky Number(容斥原理)
来源:互联网 发布:红外线控制开关软件 编辑:程序博客网 时间:2024/05/21 14:10
Watashi loves M mm very much. One day, M mm gives Watashi a chance to choose a number betweenlow 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 andhigh.
Sample Input
2 1 70 812 350 0 0 0
Sample Output
5
Hint
The lucky numbers in the sample are 72, 74, 76, 78, 81.
题意:询问[low,high]中,A集合存在一个数是它的约数,且B集合至少存在一个数不是它的约数的个数。
题解:容斥原理。假设B集合中的所有数都是约数,则满足答案的[low,high]之间的数一定可以整除B中所有数的lcm->bm
这样我们只需要在用容斥原理求[low,high]满足A集合条件的x的时候,减去[low,high]能整除lcm(x,bm)的个数。
求满足[low,high]中A存在存在一个数是他的约数,把每个数当做一个因子,再容斥。
#include <iostream>#include <string.h>#include <algorithm>#include <stdio.h>#include <math.h>#include <bitset>#include <vector>using namespace std;typedef long long ll;const int N = 16;const ll inf=1e18+10;ll l,r;int n,m;int a[N],b[N];ll bm;vector<int>vec;ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b);}ll lcm(ll a,ll b) { if(a==-1||b==-1) return -1; ll gd=gcd(a,b); if(inf/b<a/gd) return -1; return a/gd*b;}void init(int x) { vec.clear(); for(int i=2; i*i<=x; i++) { if(x%i==0) { vec.push_back(i); while(x%i==0)x/=i; } } if(x>1)vec.push_back(x);}void solve() { ll sum=0; for(int i=1; i<(1<<n); i++) { ll lc=1; int cnt=0; for(int j=0; j<n; j++) { if(i&(1<<j)) { lc=lcm(lc,a[j]); cnt++; } } if(lc==-1)lc=inf; ll tmp=lcm(bm,lc); if(tmp==-1) tmp=inf; ll num=(r/lc-r/tmp)-(l/lc-l/tmp); if(cnt&1)sum+=num; else sum-=num; } printf("%lld\n",sum);}int main() { //freopen("test.in","r",stdin); while(~scanf("%d%d%lld%lld",&n,&m,&l,&r)) { if(n==0&&m==0&&l==0&&r==0)break; l--; for(int i=0; i<n; i++)scanf("%d",&a[i]); bm=1; for(int i=0; i<m; i++) { ll x; scanf("%lld",&x); bm=lcm(bm,x); } solve(); } return 0;}
- 【ZOJ】3233【容斥原理】【Lucky Number】
- zoj 3233 Lucky Number(容斥原理)
- ZOJ 3233 Lucky Number(容斥原理)
- ZOJ 3233 Lucky Number(数论,容斥原理)
- ZOJ 3233 Lucky Number 容斥
- ZJU 3233 - Lucky Number (容斥原理)
- zoj 2836 Number Puzzle(容斥原理)
- ZOJ 2836 Number Puzzle(容斥原理啊)
- ZOJ 2836 Number Puzzle (容斥原理)
- zoj 2836 Number Puzzle(容斥原理)
- [容斥原理] zoj 2836 Number Puzzle
- ZOJ 2836 Number Puzzle(容斥原理)
- ZOJ 2836Number Puzzle(容斥原理)
- HDOJ 5213 Lucky(分块+莫队+容斥原理)
- HDU 5213 Lucky(容斥原理+莫队算法)
- Lucky Number ZOJ
- [HDU 5213]Lucky:莫队+容斥原理
- zoj 3233 容斥原理 + 双条件
- Java动态代理
- sqlserver2008完全卸载 转载自百度经验
- c++ new和delete产生的debug error
- Android 项目中执行java的main方法
- MATLAB的输入与输出语句
- ZOJ 3233 Lucky Number(容斥原理)
- Task not serializable:java.io.NotSerializableExceptionon
- Android 编程下 Touch 事件的分发和消费机制
- 文章标题
- python xlwt写excel格式控制
- IT架构的SOA整合
- ubuntu14.04 lts server安装配置备忘
- ArrayList的学习小结
- C++ map的基本操作和使用