题意(Bzoj上是权限题,所以就去Luogu罗)
接下来的p表示质数p
∑p∑na=1∑mb=1[gcd(a,b)=p] ∑ p ∑ a = 1 n ∑ b = 1 m [ g c d ( a , b ) = p ]
∑p∑⌊np⌋a=1∑⌊mp⌋b=1[gcd(a,b)=1] ∑ p ∑ a = 1 ⌊ n p ⌋ ∑ b = 1 ⌊ m p ⌋ [ g c d ( a , b ) = 1 ]
∑p∑⌊np⌋a=1∑⌊mp⌋b=1∑d|gcd(a,b)μ(d) ∑ p ∑ a = 1 ⌊ n p ⌋ ∑ b = 1 ⌊ m p ⌋ ∑ d | g c d ( a , b ) μ ( d )
∑p∑⌊np⌋d=1μ(d)⌊npd⌋⌊mpd⌋ ∑ p ∑ d = 1 ⌊ n p ⌋ μ ( d ) ⌊ n p d ⌋ ⌊ m p d ⌋
将pd设为T
∑nT=1∑p|Tμ(Tp)⌊nT⌋⌊mT⌋ ∑ T = 1 n ∑ p | T μ ( T p ) ⌊ n T ⌋ ⌊ m T ⌋
∑nT=1f(T)⌊nT⌋⌊mT⌋ ∑ T = 1 n f ( T ) ⌊ n T ⌋ ⌊ m T ⌋
其实暴力求f(T)是O(质数个数*调和级数*n) ≈ ≈ O(n)
考虑线性筛f
①: f(∏pkii)=∑p|∏pkiiμ(∏pkiip)=k∗(−1)k−1 f ( ∏ p i k i ) = ∑ p | ∏ p i k i μ ( ∏ p i k i p ) = k ∗ ( − 1 ) k − 1
②: f(pq)=f(p∏pkii)=(k+1)∗(−1)k=(−1)k−k∗(−1)k−1=μ(q)−f(q) f ( p q ) = f ( p ∏ p i k i ) = ( k + 1 ) ∗ ( − 1 ) k = ( − 1 ) k − k ∗ ( − 1 ) k − 1 = μ ( q ) − f ( q )
③: f(pkq)=∑a|pkqμ(pkqa) f ( p k q ) = ∑ a | p k q μ ( p k q a ) ,只有当a=p,k=2时 μ(pkqa) μ ( p k q a ) 才有值,为 μ(pq) μ ( p q )
#include<cmath>
#include<cstdio>
#include<algorithm>
#define fp(i,a,b) for(int i=a,I=b;i<=I;++i)
#define file(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
using namespace std;
const int N=1e7+5;
typedef int arr[N];
int n=1e7;arr g,is,mu,pr;long long ans;
int main(){
#ifndef ONLINE_JUDGE
file("s");
#endif
mu[1]=1;
fp(i,2,n){
if(!is[i])pr[++pr[0]]=i,mu[i]=-1,g[i]=1;
for(int j=1,x;j<=pr[0]&&(x=i*pr[j])<=n;j++){
is[x]=1;
if(i%pr[j])mu[x]=-mu[i],g[x]=mu[i]-g[i];
else{mu[x]=0,g[x]=mu[i];break;}
}
}
fp(i,1,n)g[i]+=g[i-1];
int T,a,b,i,j,s,t;scanf("%d",&T);
while(T--){
scanf("%d%d",&a,&b);if(a>b)swap(a,b);
i=1,j=sqrt(a);ans=0;
for(;i<=j;++i)ans+=1ll*(a/i)*(b/i)*(g[i]-g[i-1]);
for(t=g[i-1];i<=a;i=j+1,t=s){
j=min(a/(a/i),b/(b/i));s=g[j];
ans+=1ll*(a/i)*(b/i)*(s-t);
}
printf("%lld\n",ans);
}
return 0;
}