把化掉之後枚舉可以得到
由於有一個的限制
可以把拆成
調和級數預處理
每次調和級數計算即可
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob)?EOF:*ib++;
}
inline int read(){
char ch=gc();
int res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
inline ll readll(){
char ch=gc();
ll res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
inline int readstring(char *s){
int top=0;char ch=gc();
while(isspace(ch))ch=gc();
while(!isspace(ch)&&ch!=EOF)s[++top]=ch,ch=gc();
return top;
}
template<typename tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<typename tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int N=1000005;
int mu[N],vis[N],pr[N],f[N],tot;
inline void init_sieve(cs int n=N-5){
mu[1]=1;
for(int i=2;i<=n;i++){
if(!vis[i])pr[++tot]=i,mu[i]=-1;
for(int j=1,p;j<=tot&&i*pr[j]<=n;j++){
p=i*pr[j],vis[p]=1;
if(i%pr[j]==0)break;
mu[p]=-mu[i];
}
}
for(int i=1;i<=n;i++)
for(int j=1;i*j<=n;j++)
f[i*j]+=mu[i]*mu[j];
}
int n,m;
int main(){
#ifdef Stargazer
freopen("lx.in","r",stdin);
#endif
init_sieve();
int T=read();
while(T--){
n=read(),m=read();
ll res=0;
if(n>m)swap(n,m);
for(int T=1,v1,v2,i;T<=n;T++){
v1=0,v2=0,i=T;
for(;i<=n;i+=T)v1+=mu[i],v2+=mu[i];
for(;i<=m;i+=T)v2+=mu[i];
res+=f[T]*v1*v2;
}
cout<<res<<'\n';
}
}