分析:筛素数,然后枚举,莫比乌斯反演,然后关键就是分块加速(分块加速在上一篇文章)
#include<cstdio> #include<cstring> #include<queue> #include<cstdlib> #include<algorithm> #include<vector> #include<cmath> using namespace std; typedef long long LL; const int N=1e7+5; const int INF=0x3f3f3f3f; bool vis[N]; int prime[N],mu[N],cnt; void getmu() { mu[1] = 1; for(int i=2; i<=N-5; i++) { if(!vis[i]) { prime[++cnt] = i; mu[i] = -1; } for(int j=1; j<=cnt&&i*prime[j]<=N-5; j++) { vis[i*prime[j]] = 1; if(i%prime[j]) mu[i*prime[j]] = -mu[i]; else { mu[i*prime[j]] = 0; break; } } } } int main(){ getmu(); for(int i=1;i<=N-5;++i)mu[i]+=mu[i-1]; int n; scanf("%d",&n); LL ans=0; for(int i=1;i<=cnt&&prime[i]<=n;++i){ int l=n/prime[i]; for(int k=1,j;k<=l;k=j+1){ j=l/(l/k); ans+=1ll*(mu[j]-mu[k-1])*(l/k)*(l/k); } } printf("%lld\n",ans); return 0; }
时间: 2024-10-13 17:34:22