求(ans=sumlimits_{i=1}^nsumlimits_{i=1}^m[gcd(i,j)=prime])
设函数(f(d)=sumlimits_{i=1}^nsumlimits_{i=1}^m[gcd(i,j)=d])
设函数$F(d)=sumlimits_{i=1}^nsumlimits_{i=1}^m[d|gcd(i,j)]=leftlfloor frac{n}{d} ight floor leftlfloor frac{m}{d} ight floor $
莫比乌斯反演:
(F(d)=sumlimits_{d|k}f(k))
( herefore f(d)=sumlimits_{d|k}mu(leftlfloor frac{k}{n} ight floor)F(d))
推式子了
(ans=sumlimits_{pin prime}sumlimits_{i=1}^nsumlimits_{j=1}^m[gcd(i,j)=p])
将(f(d)=sumlimits_{i=1}^nsumlimits_{i=1}^m[gcd(i,j)=d])带进去
(ans=sumlimits_{pin prime}f(p))
将(f(d)=sumlimits_{d|k}mu(leftlfloor frac{k}{n} ight floor)F(d))带进去
(ans=sumlimits_{pin prime}sumlimits_{p|k}mu(leftlfloor frac{k}{n} ight floor)F(p))
不觉得(p|k)看着不舒服吗,也不好处理,我们换一种方式枚举(leftlfloor frac{k}{n} ight floor)
(ans=sumlimits_{p in prime} sumlimits_{d=1}^{ min( dfrac{n}{p},frac{m}{p} ) } mu(d) F(dp))
(~~~~~=sumlimits_{p in prime} sumlimits_{d=1}^{ min( frac{n}{p},frac{m}{p} ) } mu(d) leftlfloor frac{n}{dp} ight floor leftlfloor frac{m}{dp} ight floor)
这样做也不好分块,前面的(p)有点难处理,我们应该把素数想办法和莫比乌斯函数联系起来,然后丢到欧拉筛处理
设(T)为(dp)放到前面去
(ans=sumlimits_{T=1}^{min(n,m)} sumlimits_{t|T,t in prime} mu(leftlfloor frac{T}{t} ight floor) leftlfloor frac{n}{T} ight floor leftlfloor frac{m}{T} ight floor)
还是不好分块吧,(mu)单独就好处理了,于是我们变成这样
$ans=sumlimits_{T=1}^{min(n,m)} leftlfloor frac{n}{T} ight floor leftlfloor frac{m}{T} ight floor sumlimits_{t|T,t in prime} mu(leftlfloor frac{T}{t} ight floor) $
后面的(sumlimits_{t|T,t in prime} mu(leftlfloor frac{T}{t} ight floor))预处理出来,然后对前面的(leftlfloor frac{n}{T} ight floor leftlfloor frac{m}{T} ight floor)进行分块就好了
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=1e7+10;
inline int Read(){
LL x=0,f=1; char c=getchar();
while(c<'0'||c>'9'){
if(c=='-') f=-1; c=getchar();
}
while(c>='0'&&c<='9'){
x=(x<<3)+(x<<1)+c-'0',c=getchar();
}
return x*f;
}
int T;
int mu[maxn],prime[maxn];
LL g[maxn],sum[maxn];
bool visit[maxn];
inline void F_phi(LL n){
mu[1]=1;
int tot=0;
for(int i=2;i<=n;++i){
if(!visit[i]){
prime[++tot]=i;
mu[i]=-1;
}
for(int j=1;j<=tot&&i*prime[j]<=n;++j){
visit[i*prime[j]]=true;
if(i%prime[j]==0)
break;
else
mu[i*prime[j]]=-mu[i];
}
}
for(int j=1;j<=tot;++j)
for(LL i=1;i*prime[j]<=n;++i)
g[i*prime[j]]+=(LL)mu[i];
for(int i=1;i<=n;++i)
sum[i]=sum[i-1]+g[i];
}
int main(){
T=Read();
F_phi(maxn);
while(T--){
int n=Read(),m=Read();
if(n>m)
swap(n,m);
LL ans(0);
for(int l=1,r;l<=n;l=r+1){
r=min(n/(n/l),m/(m/l));
ans+=(LL)(n/l)*(m/l)*(sum[r]-sum[l-1]);
}
printf("%lld
",ans);
}
}