# 【刷题】BZOJ 3994 [SDOI2015]约数个数和

## Output

T行，每行一个整数，表示你所求的答案。

2
7 4
5 6

110
121

1<=N, M<=50000
1<=T<=50000

## Solution

$\sum_{i=1}^N\sum_{j=1}^Md(ij)=\sum_{i=1}^N\sum_{j=1}^M\sum_{k|i}\sum_{l|j}[gcd(k,l)=1]\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \$

$=\sum_{i=1}^N\sum_{j=1}^M\sum_{k|i}\sum_{l|j}\sum_{d|gcd(k,l)}\mu(d)\ \ \ \ (\sum_{d|n}\mu(d)=[n=1])$

$=\sum_{i=1}^N\sum_{j=1}^M\sum_{d=1}^{min(N,M)}\mu(d)\sum_{k|i}\sum_{l|j}[d|gcd(k,l)]\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \$

$=\sum_{d=1}^{min(N,M)}\mu(d)\sum_{i=1}^N\sum_{j=1}^M\sum_{k|i}\sum_{l|j}[d|gcd(k,l)]\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \$

$=\sum_{d=1}^{min(N,M)}\mu(d)\sum_{k=1}^N\sum_{l=1}^M[d|gcd(k,l)]\lfloor \frac{N}{k} \rfloor \lfloor \frac{M}{l} \rfloor\ \ \ \ \ \ \ \ \ \ \$

$=\sum_{d=1}^{min(N,M)}\mu(d)\sum_{dk=1}^N\sum_{dl=1}^M[d|gcd(dk,dl)]\lfloor \frac{N}{dk} \rfloor \lfloor \frac{M}{dl} \rfloor\ \ \ \$

$=\sum_{d=1}^{min(N,M)}\mu(d)\sum_{k=1}^{\lfloor \frac{N}{d} \rfloor}\sum_{l=1}^{\lfloor \frac{M}{d} \rfloor}\lfloor \frac{N}{dk} \rfloor \lfloor \frac{M}{dl} \rfloor\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \$

$=\sum_{d=1}^{min(N,M)}\mu(d)(\sum_{k=1}^{\lfloor \frac{N}{d} \rfloor}\lfloor \frac{N}{dk} \rfloor)(\sum_{l=1}^{\lfloor \frac{M}{d} \rfloor} \lfloor \frac{M}{dl} \rfloor)\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \$

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

#include<bits/stdc++.h>
#define ll long long
const int MAXN=50000+10;
int T,cnt,prime[MAXN],mu[MAXN],s[MAXN],f[MAXN];
bool vis[MAXN];
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char c='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(c!='\0')putchar(c);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline void init()
{
memset(vis,1,sizeof(vis));
vis[0]=vis[1]=0;
mu[1]=1;
for(register int i=2;i<MAXN;++i)
{
if(vis[i])
{
prime[++cnt]=i;
mu[i]=-1;
}
for(register int j=1;j<=cnt&&i*prime[j]<MAXN;++j)
{
vis[i*prime[j]]=0;
if(i%prime[j])mu[i*prime[j]]=-mu[i];
else break;
}
}
for(register int i=1;i<MAXN;++i)s[i]=s[i-1]+mu[i];
for(register int k=1;k<MAXN;++k)
for(register int i=1;;)
{
if(i>k)break;
int j=k/(k/i);
f[k]+=(k/i)*(j-i+1);
i=j+1;
}
}
inline ll solve(int N,int M)
{
ll res=0;
for(register int i=1;;)
{
if(i>min(N,M))break;
int j=min(N/(N/i),M/(M/i));
res+=(ll)f[N/i]*(ll)f[M/i]*(ll)(s[j]-s[i-1]);
i=j+1;
}
return res;
}
int main()
{