ZOJ 3233

3 篇文章 0 订阅
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的情况下,满足条件B的数有多少个。(从区间l到h中的所有的数)

条件A:至少能被NBLN中任意一个数整除。

条件B:至少不能被NBUN中任意一个数整除。

注意:条件B是在条件A产生的情况下才有的。

所以条件A是一个大的圆,条件B是包含在A里面的,直接干正面并不好,所以我们可以求条件B的对立面。就是能被NBUN中的所有的数整除。

于是对立面条件就转为成了:

条件A:至少能被NBLN中任意一个数整除。

条件B:能被NBUN中的所有的数整除。(既相当于能整除NBUN里面的数的最小公倍数)

所以我们直接求出条件A有多少个数满足,然后减掉对立面的条件。就得到了所有的数。

显然要用到容斥。

1:枚举NBLN的所有因子求满足条件A的所有数。:

2:然后求NBUN中的最小公倍数,然后枚举NBLN的因子再和NBUN中的最小公倍数求一个最小公倍数,得到既满足条件A,又满足条件B的对立面的所有的因子。

然后用1减去2就是答案

上代码

#include<stdio.h>
#define MAXN 1000000000000000000LL
long long ans,l,h,p,n;
long long a[18],b[505];
long long gcd(long long a,long long b)
{
    if (b==0) return a;
    else
        return gcd(b,a%b);
}
long long rongzhi(long long g,long long k,long long t)
{
    long long gg;
    long long s;
    if (t==n+1)
    {
        if (k!=0)
        {
            s=gcd(p,g);
            if (p/s>MAXN/g) s=h+1;
            else
                s=p/s*g;
            if (k%2==0)
                ans-=h/g-h/(s)-((l-1)/g-(l-1)/(s));
            else
                ans+=h/g-h/(s)-((l-1)/g-(l-1)/(s));
        }
        return 0;
    }
    gg=gcd(g,a[t]);
    if (g/gg>MAXN/a[t]) gg=h+1;
    else
        gg=g/gg*a[t];
    rongzhi(gg,k+1,t+1);
    rongzhi(g,k,t+1);
}
int main()
{
    long long m,i,j,g,k;
    while(scanf("%lld%lld%lld%lld",&n,&m,&l,&h)!=EOF)
    {
        if ((n==0)&&(m==0)&&(l==0)&&(h==0))
            break;
        for(i=1; i<=n; i++)
            scanf("%lld",&a[i]);
        for(i=1; i<=m; i++)
            scanf("%lld",&b[i]);
        p=b[1];
        for(i=2; i<=m; i++)
        {
            k=gcd(p,b[i]);
            if (p/k>MAXN/b[i]) p=h+1;
            else
                p=p/k*b[i];
        }
        ans=0;
        rongzhi(1,0,1);
        printf("%lld\n",ans);
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值