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

你可能感兴趣的:(数学,莫比乌斯)