P3327/bzoj3994 [SDOI2015]约数个数和(莫比乌斯反演)

时间:2022-10-13 17:21:56

P3327 [SDOI2015]约数个数和

神犇题解(转)

无话可补

#include<iostream>
#include<cstdio>
#include<cstring>
#define re register
using namespace std;
template<typename T>T max(T &a,T &b){return a>b?a:b;}
template<typename T>T min(T &a,T &b){return a<b?a:b;}
#define N 50001
int t,n,m,pct,pri[N],mu[N],sum[N];
long long g[N],ans;
bool v[N];
int main(){
mu[]=;
for(re int i=;i<N;++i){
if(!v[i]) pri[++pct]=i,mu[i]=-;
for(re int j=;j<=pct;++j){
re int tmp=i*pri[j];
if(tmp>=N) break;
v[tmp]=;
if(i%pri[j]) mu[tmp]=-mu[i];
else break;
}//线性筛
}re int u;
for(u=;u+<N;u+=){
sum[u]=sum[u-]+mu[u];
sum[u+]=sum[u]+mu[u+];
sum[u+]=sum[u+]+mu[u+];
sum[u+]=sum[u+]+mu[u+];
}//循环展开:微小加速
for(;u<N;++u) sum[u]=sum[u-]+mu[u];
for(re int i=;i<N;++i){
ans=;
for(re int l=,r;l<=i;l=r+){
r=i/(i/l);
ans+=1ll*(r-l+)*(i/l);
}g[i]=ans;
} scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
if(n>m) swap(n,m);
ans=;
for(re int l=,r;l<=n;l=r+){
r=min(n/(n/l),m/(m/l));
ans+=1ll*(sum[r]-sum[l-])*g[n/l]*g[m/l];
}printf("%lld\n",ans);
}return ;
}