[反演] LOJ #509. 「LibreOJ NOI Round #1」动态几何问题

μ2(n)=d2|nμ(d)

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;

ll n,m;

const int maxn=122474490;
const int N=maxn+5;

int prime[7000000],num;
int mu[N],mu2[N];

const int P=10000007;

inline void Pre(int n){
mu[1]=1; int *vst=mu2;
for (int i=2;i<=n;i++){
if (!vst[i]) prime[++num]=i,mu[i]=-1; ll t;
for (int j=1;j<=num && (t=(ll)i*prime[j])<=n;j++){
vst[t]=1;
if (i%prime[j]==0){
mu[t]=0; break;
}else
mu[t]=-mu[i];
}
}
for (int i=1;i<=n;i++) mu2[i]=((bool)mu[i])+mu2[i-1],mu[i]=mu[i-1]+mu[i];
}

inline ll S(ll n){
if (n<=maxn) return mu2[n];
int x=sqrt(n); ll ret=0;
for (int i=1,j;i<=x;i=j+1){
ll t=n/i/i; j=sqrt(n/t);
ret+=(mu[j]-mu[i-1])*t;
}
return ret;
}

int main(){
Pre(maxn);
scanf("%lld%lld",&n,&m);
if (n>m) swap(n,m); ll ans=0;
ll last=0,cur;
for (ll i=1,j;i<=n;i=j+1){
ll t1=sqrt(n/i),t2=sqrt(m/i);
j=min(n/(t1*t1),m/(t2*t2));
cur=S(j);
ans+=(cur-last)*t1*t2;
last=cur;
}
printf("%lld\n",ans);
return 0;
}