ZOJ 3233 Lucky Number
2015-09-18 21:17
471 查看
Lucky Number
Time Limit: 5000msMemory Limit: 32768KB
This problem will be judged on ZJU. Original ID: 3233
64-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 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.
Source
ZOJ Monthly, August 2009Author
OUYANG, Jialin解题:容斥求给出的区间$[low\dots high]$中有多少个数,在A序列至少有一个是他的因子,在B序列里至少有一个不是他的因子。
我们可以先求出在A序列中至少有一个是他的因子的数的个数,减去至少有一个是他的因子的数的个数,且B里面的数都是他的因子的数的个数
判q < 0 是判断是否溢出
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int maxn = 1010; LL a[maxn],b[maxn],low,high,n,m; LL LCM(LL a,LL b){ return a/__gcd(a,b)*b; } int main(){ ios::sync_with_stdio(false); cin.tie(0); while(cin>>n>>m>>low>>high,n||m||low||high){ for(int i = 0; i < n; ++i) cin>>a[i]; LL q = 1; for(int j = 0; j < m; ++j){ cin>>b[j]; q = LCM(q,b[j]); } LL ret = 0; for(int i = 1; i < (1<<n); ++i){ LL p = 1; int cnt = 0; for(int j = 0; j < n; ++j){ if((i>>j)&1){ cnt++; p = LCM(p,a[j]); } } if(q < 0){ if(cnt&1) ret += high/p - (low-1)/p; else ret -= high/p - (low - 1)/p; continue; } LL s = LCM(p,q); if(cnt&1) ret += high/p - (low - 1)/p - (high/s - (low-1)/s); else ret -= high/p - (low - 1)/p - (high/s - (low-1)/s); } printf("%lld\n",ret); } return 0; }
View Code
相关文章推荐
- JS用户表单
- 数字图像处理:基本算法-卷积和相关
- TestArrayList
- Http的Cache机制
- usaco Factorials
- MyEclipse 快捷键方法
- TCP/IP协议原理与应用笔记05:TCP/IP协议下的网关
- sql server2008 事务日志满的解决办法
- php输出字体颜色时遇到的小问题
- Map和HashMap
- Ubuntu常用命令
- markdown使用书写
- LeetCode(80)Remove Duplicates from Sorted Array II
- Android图形图像之自定义控件属性(demo:刮刮乐与打码以及图片的缓存)
- General Problem Solving Techniques [Examples]~A
- LeetCode(80)Remove Duplicates from Sorted Array II
- 内存分页机制总结
- hdu5446 Unknown Treasure(Lucas+中国剩余定理)
- HDU 5418 Victor and World (状态压缩dp)
- 数据结构应用实例#栈&单链表#简易计算器