# 【洛谷4450】双亲数（莫比乌斯反演大水题）

### 莫比乌斯反演

$\sum_{i=1}^{\lfloor\frac nd\rfloor}\sum_{j=1}^{\lfloor\frac md\rfloor}[gcd(i,j)==1]$

$\sum_{i=1}^{\lfloor\frac nd\rfloor}\sum_{j=1}^{\lfloor\frac md\rfloor}\sum_{p|i,p|j}\mu(p)$

$\sum_{p=1}^{min(\lfloor\frac nd\rfloor,\lfloor\frac md\rfloor)}\mu(p)\lfloor\frac n{dp}\rfloor\lfloor\frac m{dp}\rfloor$

$\sum_{p=1}^{min(n,m)}\mu(p)\lfloor\frac n{p}\rfloor\lfloor\frac m{p}\rfloor$

### 代码

#include<bits/stdc++.h>
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
#define Reg register
#define RI Reg int
#define Con const
#define CI Con int&
#define I inline
#define W while
#define N 1000000
#define LL long long
#define min(x,y) ((x)<(y)?(x):(y))
using namespace std;
int n,m,d;
class LinearSieve//线性筛筛μ
{
private:
int Pt,P[N+5],mu[N+5];
public:
I int operator [] (CI x) {return mu[x];}
I LinearSieve()
{
RI i,j;for(mu[1]=1,i=2;i<=N;++i)
for(!P[i]&&(mu[P[++Pt]=i]=-1),j=1;j<=Pt&&i*P[j]<=N;++j)
if(P[i*P[j]]=1,i%P[j]) mu[i*P[j]]=-mu[i];else break;
for(i=2;i<=N;++i) mu[i]+=mu[i-1];//统计前缀和
}
}Mu;
int main()
{
RI l,r,lim;LL t=0;scanf("%d%d%d",&n,&m,&d),n/=d,m/=d;//一开始直接各除以d
for(lim=min(n,m),l=1;l<=lim;l=r+1)//除法分块
r=min(n/(n/l),m/(m/l)),t+=1LL*(Mu[r]-Mu[l-1])*(n/l)*(m/l);
return printf("%lld",t),0;//输出答案
}

posted @ 2019-12-22 10:38  TheLostWeak  阅读(...)  评论(...编辑  收藏