BZOJ
Luogu

# sol

$d(ij)=\sum_{u|i}\sum_{v|j}[\gcd(u,v)==1]$

（证明晚上再补。。。）

$ans=\sum_{i=1}^{n}\sum_{j=1}^{m}\sum_{u|i}\sum_{v|j}[\gcd(u,v)==1]$

$ans=\sum_{u=1}^{n}\sum_{v=1}^{m}[\gcd(u,v)==1]\lfloor \frac nu \rfloor\lfloor \frac mv \rfloor$

$f(d)=\sum_{u=1}^{n}\sum_{v=1}^{m}[\gcd(u,v)==d]\lfloor \frac nu \rfloor\lfloor \frac mv \rfloor$

$F(d)=\sum_{u=1}^{n}\sum_{v=1}^{m}[d|\gcd(u,v)]\lfloor \frac nu \rfloor\lfloor \frac mv \rfloor$

$F(d)$的表达式中显然$u$$v$都是$d$的倍数，所以我们可以令$u=id,v=jd$然后

$F(d)=\sum_{i=1}^{n/d}\sum_{j=1}^{m/d}\lfloor \frac {n}{id} \rfloor\lfloor \frac {m}{jd} \rfloor=\sum_{i=1}^{n/d}\lfloor \frac {n/d}{i} \rfloor * \sum_{j=1}^{m/d}\lfloor \frac {m/d}{j} \rfloor$

$sum(n)=\sum_{i=1}^{n} \frac ni$表示1~n中每个数的约数个数和

$ans=f(1)=\sum_{d=1}^{n}\mu(d)F(d)=\sum_{d=1}^{n}\mu(d)sum(\lfloor \frac {n}{d} \rfloor)sum(\lfloor \frac {m}{d} \rfloor)$

$O(n)$处理出$\mu(d)$的前缀和然后直接数论分块一波

## code

#include<cstdio>
#include<algorithm>
using namespace std;
#define ll long long
const int N = 50000;
int gi()
{
int x=0,w=1;char ch=getchar();
while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
if (ch=='-') w=0,ch=getchar();
while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return w?x:-x;
}
int mu[N+5],pri[N+5],tot,zhi[N+5];
ll s[N+5],f[N+5];
void Mobius()
{
zhi[1]=mu[1]=1;
for (int i=2;i<=N;i++)
{
if (!zhi[i]) pri[++tot]=i,mu[i]=-1;
for (int j=1;j<=tot&&i*pri[j]<=N;j++)
{
zhi[i*pri[j]]=1;
if (i%pri[j]) mu[i*pri[j]]=-mu[i];
else {mu[i*pri[j]]=0;break;}
}
}
for (int i=1;i<=N;i++)
s[i]=s[i-1]+mu[i];
}
int Divide(int x)
{
int p[10]={0},k[10]={0},t=0;
for (int i=2;i*i<=x;i++)
if (x%i==0)
{
p[++t]=i;
while (x%i==0) k[t]++,x/=i;
}
if (x>1) p[++t]=x,k[t]=1;
int res=1;
for (int i=1;i<=t;i++)
res*=k[i]+1;
return res;
}
int main()
{
Mobius();
for (int i=1;i<=N;i++)
f[i]=f[i-1]+Divide(i);
int T=gi();
while (T--)
{
int n=gi(),m=gi();
if (n>m) swap(n,m);
int i=1;ll ans=0;
while (i<=n)
{
int j=min(n/(n/i),m/(m/i));
ans+=(s[j]-s[i-1])*f[n/i]*f[m/i];
i=j+1;
}
printf("%lld\n",ans);
}
return 0;
}

posted @ 2018-01-09 14:46  租酥雨  阅读(184)  评论(1编辑  收藏  举报