ZOJ 3233(45/600)

本文介绍了一个有趣的数学问题:在给定区间内找到符合特定条件的幸运数数量,并计算主人公获得幸福的概率。通过构建两个基本序列,利用算法解决这一问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值