luogu_2158 [SDOI2008]儀仗隊
阿新 • • 發佈:2017-10-07
[1] aik blank smi cst target cpp 歐拉 middle
若n為質數則
//歐拉函數:
歐拉函數是積性函數——若m,n互質 特殊性質:當n為奇數時,若n為質數則
#include <cstdio> #include <iostream> using namespace std; const int N=40010; int n,p[N],nop[N],cnt,phi[N],ans; int main(){ scanf("%d",&n); if(n==0){puts("0"); return 0;} nop[1]=1; for(int i=2;i<=n;i++){ if(!nop[i]){p[++cnt]=i; phi[i]=i-1;} for(int j=1;j<=cnt && i*p[j]<=n;j++){ nop[i*p[j]]=1; if(i%p[j]==0){phi[i*p[j]]=phi[i]*p[j]; break;} else phi[i*p[j]]=phi[i]*(p[j]-1); } } for(int i=2;i<=n-1;i++)ans+=phi[i]; printf("%d\n",ans*2+3); return 0; }
luogu_2158 [SDOI2008]儀仗隊