Lucky number__zju3233 解题报告

Lucky Number


Time 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

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.

 

解题思路:

  命题A:至少能被BLN中的一个数整除

  命题B:至少不能被BUN中的一个数整除

  所以A&&B=A-A&&(~B);

题目就是求[low,high]中符合A&&B的数的个数,用容斥原理求出符合A的个数并减去符合A&&(~B)的个数;

求出BUN中所有数的最小公倍数p,

递归从BLN中取出k个数,求他们的最小公倍数g,

求出p和g的最小公倍数s;

当k为奇数:ans+=high/g-high/s-((low-1)/g-(low-1)/s);

当k为偶数:ans-=high/g-high/s-((low-1)/g-(low-1)/s);(容斥原理)

ans就是最后结果;

 

时间复杂度:2^15

浙大oj 840ms

空间复杂度 180kB

代码:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值