Coprime Integers—— 莫比乌斯反演

Given intervals [a,b] and [c,d], count the number of pairs of coprime integers (x,y) such that a ≤ x ≤ b and c ≤ y ≤ d. Two numbers are coprime if their greatest common divisor is 1. A divisor of a number is a positive integer that evenly divides that number.
Input
The input consists of a single line containing space-separated integers a, b, c, and d (1 ≤ a ≤ b ≤ 107, 1 ≤ c ≤ d ≤ 107).
Output
Print the result as a single integer on one line.
Sample Input and Output
1 5 1 5 19
12 12 1 12 4
1 100 1 100 6087

题意:

问你区间a,b和c,d有多少对数互质。

题解:

用懵逼钨丝反演就好了,用一下容斥,先求出1到b和1到d的对数,再减去1到a和1到d的对数,再减去1到c和1到b的对数,最后加上1到a和1到c的对数。

#include<bits/stdc++.h>
using namespace std;
#define eps 1e-6
#define ll long long
int n,m;
const int maxn=1e7+5;
int nprime[maxn],prime[maxn],cnt;
ll mu[maxn];
void init()
{
    cnt=0;
    mu[1]=1;
    for(int i=2;i<maxn;i++)
    {
        if(!nprime[i])
        {
            prime[++cnt]=i;
            mu[i]=-1;
        }
        for(int j=1;j<=cnt&&prime[j]*i<maxn;j++)
        {
            nprime[prime[j]*i]=1;
            if(i%prime[j]==0)
            {
                mu[i*prime[j]]=0;
                break;
            }
            mu[i*prime[j]]=-mu[i];
        }
    }
}
int main()
{
    init();
    ll a,b,c,d;
    scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
    a--,c--;
    ll ans=0;
    for(ll i=1;i<=b;i++)
        ans+=mu[i]*(b/i)*(d/i);
    for(ll i=1;i<=a;i++)
        ans-=(a/i)*mu[i]*(d/i);
    for(ll i=1;i<=c;i++)
        ans-=(c/i)*mu[i]*(b/i);
    for(ll i=1;i<=a;i++)
        ans+=(a/i)*(c/i)*mu[i];
    printf("%lld\n",ans);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值