ZOJ 3233 Lucky Number
Lucky Number
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
Author
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = ;
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();
while(cin>>n>>m>>low>>high,n||m||low||high){
for(int i = ; i < n; ++i) cin>>a[i];
LL q = ;
for(int j = ; j < m; ++j){
cin>>b[j];
q = LCM(q,b[j]);
}
LL ret = ;
for(int i = ; i < (<<n); ++i){
LL p = ;
int cnt = ;
for(int j = ; j < n; ++j){
if((i>>j)&){
cnt++;
p = LCM(p,a[j]);
}
}
if(q < ){
if(cnt&) ret += high/p - (low-)/p;
else ret -= high/p - (low - )/p;
continue;
}
LL s = LCM(p,q);
if(cnt&) ret += high/p - (low - )/p - (high/s - (low-)/s);
else ret -= high/p - (low - )/p - (high/s - (low-)/s);
}
printf("%lld\n",ret);
}
return ;
}
ZOJ 3233 Lucky Number的更多相关文章
- ZOJ 3233 Lucky Number --容斥原理
这题被出题人给活活坑了,题目居然理解错了..哎,不想多说. 题意:给两组数,A组为幸运基数,B组为不幸运的基数,问在[low,high]区间内有多少个数:至少被A组中一个数整除,并且不被B中任意一个数 ...
- 枚举 + 进制转换 --- hdu 4937 Lucky Number
Lucky Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)To ...
- SCU3502 The Almost Lucky Number
Description A lucky number is a number whose decimal representation contains only the digits \(4\) a ...
- HDOJ 4937 Lucky Number
当进制转换后所剩下的为数较少时(2位.3位),相应的base都比較大.能够用数学的方法计算出来. 预处理掉转换后位数为3位后,base就小于n的3次方了,能够暴力计算. . .. Lucky Numb ...
- 题目1380:lucky number
转载请注明文本链接 http://blog.csdn.net/yangnanhai93/article/details/40441709 题目链接地址:http://ac.jobdu.com/prob ...
- HDU 3346 Lucky Number
水题 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> us ...
- 九度oj 题目1380:lucky number
题目描述: 每个人有自己的lucky number,小A也一样.不过他的lucky number定义不一样.他认为一个序列中某些数出现的次数为n的话,都是他的lucky number.但是,现在这个序 ...
- 『NYIST』第九届河南省ACM竞赛队伍选拔赛[正式赛二]- Nearly Lucky Number(Codeforces Beta Round #84 (Div. 2 Only)A. Nearly)
A. Nearly Lucky Number time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- B - Nearly Lucky Number
Problem description Petya loves lucky numbers. We all know that lucky numbers are the positive integ ...
随机推荐
- C# 数据库访问
C# 数据库访问 分类: C#学习笔记2011-07-05 11:26 515人阅读 评论(0) 收藏 举报 数据库c#datasettextboxcommandexception 目录(?)[+ ...
- hdoj--1205--吃糖果(规律)
吃糖果 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Sub ...
- Coursera Algorithms Programming Assignment 4: 8 Puzzle (100分)
题目原文:http://coursera.cs.princeton.edu/algs4/assignments/8puzzle.html 题目要求:设计一个程序解决8 puzzle问题以及该问题的推广 ...
- js和php中几种生成验证码的方式
之前做过取随机数和生成验证码的练习,都是通过取随机数作为数组下标,然后从数组中取值的方式(js): /*验证码*/ function sj_yzm(){ //存一个包括数字和字母的数组 var zon ...
- MySQL关于视图的创建
-- 视图就是一条select 语句 执行后返回结果集,是一种虚拟表,是一个逻辑表 -- 方便操作,减少复杂的SQL语句,增加可读性,更加安全一些 create view demo_view as s ...
- 属性字符串(NSAttributedString)的简单应用
属性字符串NSAttributedString 可以对字符串附加格式信息,由于对于对不同文本片段使用不同的格式,属性字符串类特别合适. IOS 6中对样式文本有了大改善,大部分主要的UIKit控件都允 ...
- Windows下apache+tomcat负载均衡
Windows下apache+tomcat负载均衡 网上已经有很多的资料,但是很多都比较零碎,需要整合一起才能搭建出理想的负载均衡,正好前段时间搭建了windows与linux下的负载均衡,在此记录, ...
- 使用Jupter Notebook实现简单的神经网络
参考:http://python.jobbole.com/82208/ 注:1)# %matplotlib inline 注解可以使Jupyter中显示图片 2)注意包的导入方式 一.使用的Pytho ...
- Resources.getResourceAsReader 报空指针
1.maven项目中 可能未编译 导致找不到, 解决方法:mvn clean install -DskipTests -X 编译一下项目 2.可能在 Resources.getResourceAs ...
- JS——client
clientTop.clientLeft: clientTop:盒子的上boder clientLeft:盒子的左border clientWidth与clientHeight 1.在有DTD情况下: ...