Lucky Number

Time Limit: 5000ms
Memory 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 [lowhigh]?

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), lowhigh (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

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 = ;
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的更多相关文章

  1. ZOJ 3233 Lucky Number --容斥原理

    这题被出题人给活活坑了,题目居然理解错了..哎,不想多说. 题意:给两组数,A组为幸运基数,B组为不幸运的基数,问在[low,high]区间内有多少个数:至少被A组中一个数整除,并且不被B中任意一个数 ...

  2. 枚举 + 进制转换 --- hdu 4937 Lucky Number

    Lucky Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)To ...

  3. SCU3502 The Almost Lucky Number

    Description A lucky number is a number whose decimal representation contains only the digits \(4\) a ...

  4. HDOJ 4937 Lucky Number

    当进制转换后所剩下的为数较少时(2位.3位),相应的base都比較大.能够用数学的方法计算出来. 预处理掉转换后位数为3位后,base就小于n的3次方了,能够暴力计算. . .. Lucky Numb ...

  5. 题目1380:lucky number

    转载请注明文本链接 http://blog.csdn.net/yangnanhai93/article/details/40441709 题目链接地址:http://ac.jobdu.com/prob ...

  6. HDU 3346 Lucky Number

    水题 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> us ...

  7. 九度oj 题目1380:lucky number

    题目描述: 每个人有自己的lucky number,小A也一样.不过他的lucky number定义不一样.他认为一个序列中某些数出现的次数为n的话,都是他的lucky number.但是,现在这个序 ...

  8. 『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 ...

  9. B - Nearly Lucky Number

    Problem description Petya loves lucky numbers. We all know that lucky numbers are the positive integ ...

随机推荐

  1. 网络安全中机器学习大合集 Awesome

    网络安全中机器学习大合集 from:https://github.com/jivoi/awesome-ml-for-cybersecurity/blob/master/README_ch.md#-da ...

  2. 在PL/SQL使用游标获取数据及动态SQL

    1.游标概念: 当在PL/SQL块中执行DML(增删改)时,Oracle会为其分配上下文区(Context Area),游标是指向上下文区的指针 2.  游标分类: A.  隐式游标 a.  在PL/ ...

  3. openstack 杂记 备忘002

  4. IDEA Spark Streaming Kafka数据源-Consumer

    import org.apache.spark.SparkConf import org.apache.spark.streaming.kafka.KafkaUtils import org.apac ...

  5. eclipse下整合springboot和mybatis

    1.新建maven项目 先新建一个maven项目,勾选上creat a simple project,填写groupid,artifactid 2.建立项目结构 3.添加依赖 <parent&g ...

  6. 【学习笔记】线段树—扫描线补充 (IC_QQQ)

    [学习笔记]线段树-扫描线补充 (IC_QQQ) (感谢 \(IC\)_\(QQQ\) 大佬授以本内容的著作权.此人超然于世外,仅有 \(Luogu\) 账号 尚可膜拜) [学习笔记]线段树详解(全) ...

  7. 解决:xxx is not in the sudoers file.This incident will be reported.的解决方法

    Linux中普通用户用sudo执行命令时报”xxx is not in the sudoers file.This incident will be reported”错误,解决方法就是在/etc/s ...

  8. ACM_3n+1问题(克拉兹问题+线段树区间查询最大值)

    3n+1问题 Time Limit: 2000/1000ms (Java/Others) Problem Description: 考虑如下的序列生成算法:从整数n开始,如果n是偶数,把它除以2:如果 ...

  9. [转]linux之patch命令

    转自:http://blog.chinaunix.net/uid-9525959-id-2001542.html patch [选项] [原始文件 [补丁文件]] [功能] 给文件1应用补丁文件变成另 ...

  10. Jquery音频播放插件下载地址(有Html、JS、CSS、音频)

    有详细的html文件.全部JS代码文件.Css样式文件.测试音频资料 音频播放插件下载链接(百度云): http://pan.baidu.com/s/1pKC904F 提取码评论留邮箱发送,谢谢!