# ●BZOJ 3994 [SDOI2015]约数个数和

http://www.lydsy.com/JudgeOnline/problem.php?id=3994

（先定义这样一个符号[x]，如果x为true，则[x]=1，否则[x]=0）

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

$ANS=\sum_{n=1}^{N}\sum_{m=1}^{M}d(nm)$

$\quad\quad=\sum_{n=1}^{N}\sum_{m=1}^{M}\sum_{i|n}\sum_{j|m}[gcd(i,j)==1]$

$\quad\quad=\sum_{i=1}^{N}\sum_{j=1}^{M}[gcd(i,j)==1]\lfloor \frac{N}{i} \rfloor \lfloor \frac{M}{j} \rfloor$

$w(x)=\sum_{d|x}\mu(d)$ 若x==1则w(x)=1，否则w(x)=0

$ANS=\sum_{i=1}^{N}\sum_{j=1}^{M}\lfloor \frac{N}{i} \rfloor \lfloor \frac{M}{j} \rfloor\sum_{d|gcd(i,j)}\mu(d)$

$\quad\quad=\sum_{d=1}^{min(n,m)}\mu(d)\sum_{i=1}^{N/d}\lfloor \frac{N}{id}\rfloor\sum_{j=1}^{M/d}\lfloor \frac{M}{jd}\rfloor$

$ANS=\sum_{d=1}^{min(n,m)}\mu(d)f(\lfloor \frac{N}{d} \rfloor)f(\lfloor \frac{M}{d} \rfloor)$

#include<cstdio>
#include<cstring>
#include<iostream>
#define ll long long
#define MAXN 50050
using namespace std;
ll f[MAXN];
int mu[MAXN];
void Sieve(){
static bool np[MAXN];
static int prime[MAXN],pnt;
f[1]=mu[1]=1;
for(int i=2,tmp,d;i<=50000;i++){
if(!np[i]) prime[++pnt]=i,mu[i]=-1,f[i]=2;
for(int j=1;j<=pnt&&i<=50000/prime[j];j++){
np[i*prime[j]]=1; tmp=i; d=1;
while(tmp%prime[j]==0) tmp/=prime[j],d++;
f[i*prime[j]]=f[tmp]*(d+1);
if(i%prime[j]) mu[i*prime[j]]=-mu[i];
else break;
}
}
for(int i=2;i<=50000;i++)
f[i]+=f[i-1],mu[i]+=mu[i-1];
}
int main(){
Sieve(); ll ans;
int Case,n,m,mini;
scanf("%d",&Case);
while(Case--){
scanf("%d%d",&n,&m);
mini=min(n,m); ans=0;
for(int d=1,last;d<=mini;d=last+1){
last=min(n/(n/d),m/(m/d));
ans+=(mu[last]-mu[d-1])*f[n/d]*f[m/d];
}
printf("%lld\n",ans);
}
return 0;
}


Do not go gentle into that good night.
Rage, rage against the dying of the light.
————Dylan Thomas
posted @ 2018-01-16 20:04  *ZJ  阅读(186)  评论(0编辑  收藏  举报