P3327 [SDOI2015] 约数个数和 的代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=5*1e4;
const int maxn=N;
int T,n,m;
int ans;
bool vis[maxn+5];
int pr[maxn+5],prt;
int mu[maxn+5];
int sm[maxn+5];
int h[maxn+5];
inline void Init(){
for(int i=1;i<=N;i++){
for(int j=1,k;j<=i;j=k+1){
k=i/(i/j);
h[i]+=(i/j)*(k-j+1);
}
}
mu[1]=1;
for(int i=2;i<=N;i++){
if(!vis[i]) pr[++prt]=i,mu[i]=-1;
for(int j=1;j<=prt&&i*pr[j]<=N;j++){
vis[i*pr[j]]=1;
if(i%pr[j]==0){
mu[i*pr[j]]=0;
break;
}
mu[i*pr[j]]=-mu[i];
}
}
for(int i=1;i<=N;i++) sm[i]=sm[i-1]+mu[i];
}
signed main(){
Init();
scanf("%lld",&T);
while(T--){
scanf("%lld%lld",&n,&m);
if(n>m) swap(n,m);
ans=0;
for(int i=1,j;i<=n;i=j+1){
j=min(n/(n/i),m/(m/i));
ans+=(sm[j]-sm[i-1])*h[n/i]*h[m/i];
}
printf("%lld\n",ans);
}
return 0;
}