线性筛
https://www.luogu.org/problemnew/show/P1403
#include<bits/stdc++.h> #define fi first #define se second #define INF 0x3f3f3f3f #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define pqueue priority_queue #define NEW(a,b) memset(a,b,sizeof(a)) #define lowbit(x) ((x)&(-x)) const double pi=4.0*atan(1.0); const int maxn=1e6+8; typedef long long LL; typedef unsigned long long ULL; const LL mod=1e9+7; const ULL base=1e7+7; const double e=exp(1); using namespace std; int prime[maxn]; bool isnotprime[maxn]={1,1}; int d[maxn]; int f[maxn]; int tot=0; void get_prime(int n){ for(int i=2;i<=n;i++){ if(!isnotprime[i]){ prime[tot++]=i; d[i]=i; } for(int j=0;j<tot;j++){ if(i*prime[j]>n) break; isnotprime[i*prime[j]]=1; d[i*prime[j]]=d[i]; if(i%prime[j]==0) break; } } } int main(){ fio; int n; cin>>n; f[1]=1; LL sum=1; get_prime(n); for(int i=2;i<=n;i++){ int t=2; int j=i/d[i]; int now=d[i]; while(j%d[i]==0){ j/=d[i]; now*=d[i]; t++; } //cout<<i<<' '<<now<<endl; if(now!=i) d[i]=now; else{ d[i]=0; f[i]=t; } } for(int i=2;i<=n;i++){ if(!isnotprime[i]){ f[i]=2; } else if(d[i]){ f[i]=f[d[i]]*f[i/d[i]]; } sum+=f[i]; //cout<<i<<' '<<f[i]<<' '<<d[i]<<endl; } cout<<sum<<endl; }