# BZOJ3994: [SDOI2015]约数个数和

## 题目分析

$\sum_{i=1}^{N}\sum_{j=1}^{m}d(ij)$

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

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

$F(n)=\sum_{n\mid d}f(d)=\lfloor \frac Nd \rfloor \lfloor \frac Md \rfloor$

$f(n)=\sum_{n\mid d}\mu(\frac dn)F(d)$

$d(i,j)$代回原来的式子并化简：

\begin{align} Ans&=\sum_{i=1}^{N}\sum_{j=1}^{M}d(ij) \\ &=\sum_{i=1}^{N}\sum_{j=1}^{M}\sum_{x\mid i}\sum_{y\mid j}[gcd(x,y)=1]\ 根据\mu的性质把它代进去\\ &=\sum_{i=1}^{N}\sum_{j=1}^{M}\sum_{x\mid i}\sum_{y\mid j}\sum_{d\mid gcd(x,y)}\mu(d)\ 然后更换枚举约数为枚举d \\ &=\sum_{i=1}^{N}\sum_{j=1}^{M}\sum_{x\mid i}\sum_{y\mid j}\sum_{d=1}^{min(N,M)}\mu(d)\times [d\mid gcd(x,y)]\\ &=\sum_{d=1}^{min(N,M)}\mu(d)\sum_{i=1}^{N}\sum_{j=1}^{M}\sum_{x\mid i}\sum_{y\mid j}[d\mid gcd(x,y)]\\ &=\sum_{d=1}^{min(N,M)}\mu(d)\sum_{x=1}^{N}\sum_{y=1}^{M}[d\mid gcd(x,y)]\lfloor \frac {N}{x} \rfloor \lfloor \frac{M}{y}\rfloor \\ &=\sum_{d=1}^{min(N,M)}\mu(d)\sum_{x=1}^{\lfloor\frac{N}{d}\rfloor}\sum_{y=1}^{\lfloor\frac{M}{y}\rfloor}\lfloor\frac{N}{dx}\rfloor\lfloor\frac{M}{dy}\rfloor\\ &=\sum_{d=1}^{min(N,M)}\mu(d)(\sum_{x=1}^{\lfloor\frac{n}{d}\rfloor}\lfloor\frac{n}{dx}\rfloor)(\sum_{y=1}^{\lfloor\frac{m}{d}\rfloor}\lfloor\frac{m}{dy}\rfloor) \end{align}

## 是代码呢

#include <bits/stdc++.h>
using namespace std;
const int MAXN=1e5+7;
#define ll long long
ll sum[MAXN],g[MAXN];
int mu[MAXN],prime[MAXN];
bool vis[MAXN];
int n,m;
inline void get_mu(int N)
{
mu[1]=1;
for(int i=2;i<=N;i++){
if(!vis[i]){
mu[i]=-1;
prime[++prime[0]]=i;
}
for(int j=1;j<=prime[0];j++){
if(prime[j]*i>N) break;
vis[prime[j]*i]=1;
if(i%prime[j]==0) break;
else mu[i*prime[j]]=-mu[i];
}
}
for(int i=1;i<=N;i++) sum[i]=sum[i-1]+mu[i];
for(int i=1;i<=N;i++){
ll ans=0;
for(int l=1,r;l<=i;l=r+1){
r=(i/(i/l));
ans+=1ll*(r-l+1)*(i/l);
}
g[i]=ans;
}
}
{
int x=0,c=1;
char ch=' ';
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
while(ch=='-')c*=-1,ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*c;
}
int main()
{
get_mu(50000);
while(T--){