这题大概就是提取一下d,然后就跟前面的题目差不多了。
#include<bits/stdc++.h> #define N 10000005 using namespace std; typedef long long ll; int maxn,qx[100010],qy[100010],f[N],prime[N],cnt,mu[N],vis[N]; ll sum[N]; void calcmu(){ cnt=0;memset(vis,1,sizeof(vis));mu[1]=1; for(int i=2;i<=maxn;i++){ if(vis[i]){prime[++cnt]=i;mu[i]=-1;} for(int j=1;j<=cnt;j++){ int t=prime[j]*i;if(t>maxn)break; vis[t]=0; if(i%prime[j]==0){mu[t]=0;break;} mu[t]=-mu[i]; } } for(int i=2;i<=maxn;i++)if(mu[i])sum[i]-=mu[i]; for(int i=maxn;i>=2;i--)if(sum[i]) for(ll j=1LL*i*i;j<=maxn;j*=i)sum[j]=sum[i]; for(int i=2;i<=maxn;i++)sum[i]+=sum[i-1]; } inline int read(){ int f=1,x=0;char ch; do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9'); do{x=x*10+ch-'0';ch=getchar();}while(ch>='0'&&ch<='9'); return f*x; } int main(){ int T=read(); for(int i=1;i<=T;i++){ qx[i]=read();qy[i]=read(); maxn=max(maxn,max(qx[i],qy[i])); } calcmu(); for(int aaa=1;aaa<=T;aaa++){ int n=qx[aaa],m=qy[aaa]; if(n>m)swap(n,m);ll ans=0; for(int i=1,j=1;i<=n;i=j+1){ j=min(n/(n/i),m/(m/i)); ans+=(ll)(n/i)*(ll)(m/i)*(sum[j]-sum[i-1]); } printf("%lld ",ans); } }