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;
}