同YY的GCD。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define maxn 10000000 using namespace std; long long t,n,m,prime[maxn/10],miu[maxn+50],f[maxn+50],top=0; bool vis[maxn+50]; void get_table() { miu[1]=1;f[1]=0; for (long long i=2;i<=maxn;i++) { if (!vis[i]) { vis[i]=true; prime[++top]=i;miu[i]=-1;f[i]=1; } for (long long j=1;j<=top && i*prime[j]<=maxn;j++) { vis[i*prime[j]]=true; if (!(i%prime[j])) { miu[i*prime[j]]=0;f[i*prime[j]]=miu[i]; break; } else {miu[i*prime[j]]=-miu[i];f[i*prime[j]]=miu[i]-f[i];} } } for (long long i=1;i<=maxn;i++) f[i]+=f[i-1]; } void work() { scanf("%lld",&n); long long l=1,r,ans=0; while (l<=n) { long long r=n/(n/l); ans+=(n/l)*(n/l)*(f[r]-f[l-1]); l=r+1; } printf("%lld\n",ans); } int main() { get_table(); work(); return 0; }