题意
根据莫比乌斯反演的常见套路,我们可以很容易地化到这一步:
由于这样的枚举最后复杂度是
,肯定会TLE,我们需要考虑如何去优化
然后后面的
部分是可以预处理前缀和的,所以整个的复杂度就降到了
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
#define re register
#define gc getchar()
#define ll long long
inline int read()
{
re int x(0); re char ch(gc);
while(ch>'9'||ch<'0') ch=gc;
while(ch>='0'&&ch<='9') x=(x*10)+(ch^48),ch=gc;
return x;
}
const int N=1e7+10;
int mu[N],pri[N],sum[N],vis[N],cnt,f[N];
void get_mu(int n) //线性筛求mu函数
{
mu[1]=1;
n-=5;
for(int i=2;i<=n;++i)
{
if(!vis[i])
mu[i]=-1,pri[++cnt]=i,f[i]=1;
for(int j=1;j<=cnt&&i*pri[j]<=n;++j)
{
vis[i*pri[j]]=1;
if(i%pri[j]==0)
{
f[i*pri[j]]=mu[i];
mu[i*pri[j]]=0;
break;
}
else
f[i*pri[j]]=-f[i]+mu[i],mu[i*pri[j]]=-mu[i];
}
f[i]+=f[i-1];
}
}
void work()
{
int a=read(),b=read();
int n=min(a,b);
ll ans=0;
for(int l=1,r;l<=n;l=r+1) //数论分块优化
{
r=min(a/(a/l),b/(b/l));
ans+=1LL*(f[r]-f[l-1])*(a/l)*(b/l);
}
cout<<ans<<endl;
}
int main()
{
int T=read();
get_mu(N);
while(T--) work();
return 0;
}
一年OI一年WA,不开 long long 见祖宗