# 【BZOJ3994】[SDOI2015] 约数个数和（莫比乌斯反演）

### 一个重要的性质

$d(i·j)=\sum_{x|i}\sum_{y|j}[gcd(x,y)==1]$

### 一些定义

$f(d)=\sum_{i=1}^N\sum_{j=1}^M[gcd(i,j)==d]$

$F(n)=\sum_{n|d}f(d)$

$f(n)=\sum_{n|d}\mu(\lfloor\frac dn\rfloor)F(d)$

### 公式化简

$answer=\sum_{i=1}^N\sum_{j=1}^Md(i·j)$

$answer=\sum_{i=1}^N\sum_{j=1}^M\sum_{x|i}\sum_{y|j}[gcd(x,y)==1]$

$answer=\sum_{i=1}^N\sum_{j=1}^M\sum_{x|i}\sum_{y|j}\sum_{d|gcd(x,y)}\mu(d)$

$answer=\sum_{i=1}^N\sum_{j=1}^M\sum_{x|i}\sum_{y|j}\sum_{d=1}^{min(n,m)}\mu(d)*[d|gcd(x,y)]$

$answer=\sum_{d=1}^{min(n,m)}\mu(d)\sum_{i=1}^N\sum_{j=1}^M\sum_{x|i}\sum_{y|j}[d|gcd(x,y)]$

$answer=\sum_{d=1}^{min(n,m)}\mu(d)\sum_{x=1}^N\sum_{y=1}^M[d|gcd(x,y)]\lfloor\frac Nx\rfloor\lfloor\frac My\rfloor$

$answer=\sum_{d=1}^{min(n,m)}\mu(d)\sum_{x=1}^{\lfloor\frac Nd\rfloor}\sum_{y=1}^{\lfloor\frac Md\rfloor}\lfloor\frac N{d·x}\rfloor\lfloor\frac M{d·y}\rfloor$

$answer=(\sum_{d=1}^{min(n,m)}\mu(d))(\sum_{x=1}^{\lfloor\frac Nd\rfloor}\lfloor\frac N{d·x}\rfloor)(\sum_{y=1}^{\lfloor\frac Md\rfloor}\lfloor\frac M{d·y}\rfloor)$

### 代码

#include<bits/stdc++.h>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define uint unsigned int
#define LL long long
#define ull unsigned long long
#define swap(x,y) (x^=y,y^=x,x^=y)
#define abs(x) ((x)<0?-(x):(x))
#define INF 1e9
#define Inc(x,y) ((x+=(y))>=MOD&&(x-=MOD))
#define ten(x) (((x)<<3)+((x)<<1))
#define N 50000
using namespace std;
int n,m;
class FIO
{
private:
#define Fsize 100000
#define pc(ch) (FoutSize<Fsize?Fout[FoutSize++]=ch:(fwrite(Fout,1,FoutSize,stdout),Fout[(FoutSize=0)++]=ch))
int f,FoutSize,OutputTop;char ch,Fin[Fsize],*FinNow,*FinEnd,Fout[Fsize],OutputStack[Fsize];
public:
FIO() {FinNow=FinEnd=Fin;}
inline void read(int &x) {x=0,f=1;while(!isdigit(ch=tc())) f=ch^'-'?1:-1;while(x=ten(x)+(ch&15),isdigit(ch=tc()));x*=f;}
inline void read_string(string &x) {x="";while(isspace(ch=tc()));while(x+=ch,!isspace(ch=tc())) if(!~ch) return;}
inline void write(LL x) {if(!x) return (void)pc('0');if(x<0) pc('-'),x=-x;while(x) OutputStack[++OutputTop]=x%10+48,x/=10;while(OutputTop) pc(OutputStack[OutputTop]),--OutputTop;}
inline void write_char(char x) {pc(x);}
inline void write_string(string x) {register int i,len=x.length();for(i=0;i<len;++i) pc(x[i]);}
inline void end() {fwrite(Fout,1,FoutSize,stdout);}
}F;
class Class_Mobius//莫比乌斯反演
{
private:
int Prime_cnt,mu[N+5],Prime[N+5];bool IsNotPrime[N+5];
public:
int sum[N+5];LL g[N+5];
Class_Mobius()//预处理
{
register int i,j,l,r;
for(mu[1]=1,i=2;i<=N;++i)//求出莫比乌斯函数
{
if(!IsNotPrime[i]) Prime[++Prime_cnt]=i,mu[i]=-1;
for(j=1;j<=Prime_cnt&&i*Prime[j]<=N;++j)
if(IsNotPrime[i*Prime[j]]=true,i%Prime[j]) mu[i*Prime[j]]=-mu[i];else break;
}
for(i=1;i<=N;++i) sum[i]=sum[i-1]+mu[i];//求出前缀和
for(i=1;i<=N;++i) for(l=1;l<=i;l=r+1) r=i/(i/l),g[i]+=1LL*(r-l+1)*(i/l);//第一次除法分块
}
}Mobius;
int main()
{