您的位置:首页 > 其它

ZOJ 3233(45/600)

2017-07-17 16:08 281 查看
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.

这个题是稍微有点变形了

所有的结果都要去掉减第二个数组lcm的数量

所以每次容斥的时候我都带着

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long l;
l tu[50];
l n,m,z,y,zs;
l gcd(l q,l w)
{
if(!w)return q;
return gcd(w,q%w);
}
l lcm(l q,l w)
{
l we=w/gcd(q,w);
return q*we;
}
l dfs(l mule,l cs,l lc,l tt)
{
if(mule>n)return 0;
l daan=0;
for(int a=mule;a<=n;a++)
{
daan+=cs/lcm(tu[a],lc)-cs/lcm(lcm(tu[a],lc),tt)-dfs(a+1,cs,lcm(tu[a],lc),tt);
}
return daan;
}
l jju(l w,l tt)
{
l daan=0;
for(int a=1;a<=n;a++)
{
daan+=w/tu[a]-w/lcm(tu[a],tt)-dfs(a+1,w,tu[a],tt);
}
return daan;
}
int main()
{
while(cin>>n>>m>>z>>y)
{
if(n==0&&m==0&&z==0&&y==0)break;
for(l a=1;a<=n;a++)cin>>tu[a];
l tt=1;
for(l a=1;a<=m;a++)
{
cin>>zs;
tt=lcm(tt,zs);
}
cout<<jju(y,tt)-jju(z-1,tt)<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: