莫比乌斯反演模板--Gym 101982B
Posted shuizhidao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了莫比乌斯反演模板--Gym 101982B相关的知识,希望对你有一定的参考价值。
题意
给你a,b,c,d四个数,求[a,b]与[c,d],互质的数的对数
代码
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <sstream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <iomanip>
#include <stack>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int N = 1e7 + 100;
const int MOD = 1e9 + 9;
#define lson l, m, rt << 14
#define rson m + 1, r, rt << 1 | 1
#define F(i, l, r) for(int i = l;i <= (r);++i)
#define RF(i, l, r) for(int i = l;i >= (r);--i)
int prime[N], vis[N], cnt, mu[N], sum[N];
void Isprime(int n)
{
mu[1] = 1;
for(int i = 2;i <= n;++i)
{
if(!vis[i]) {prime[cnt++] = i;mu[i] = -1;}
for(int j = 0;j < cnt && i * prime[j] <= n;++j)
{
vis[i * prime[j]] = 1;
if(i % prime[j] == 0)
break;
mu[i * prime[j]] = -mu[i];
}
}
for(int i = 1;i <= n;++i)
sum[i] = sum[i - 1] + mu[i];
}
LL a, b, c, d;
LL solve(LL n, LL m)
{
if(n > m) swap(n, m);
LL ans = 0;
for(int i = 1, last;i <= n;i = last + 1)
{
last = min(n / (n / i), m / (m / i));
ans += (sum[last] - sum[i - 1]) * (n / i) * (m / i);
}
return ans;
}
int main()
{
Isprime(1e7+5);
cin >> a >> b >> c >> d;
cout << solve(b, d) - solve(a - 1, d) - solve(b, c - 1) + solve(a - 1, c - 1);
return 0;
}
以上是关于莫比乌斯反演模板--Gym 101982B的主要内容,如果未能解决你的问题,请参考以下文章