仪仗队
作者:互联网
题目链接:https://www.luogu.org/problemnew/show/P2158
欧拉函数模板题
WARNING:考虑n=1的情况,因为n=1时看不到任何人,此时输出的值为0
代码:
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 using namespace std; 5 const int N=40005; 6 int n,b[N],prime[N],faila[N],cnt=0,sum; 7 int main() 8 { 9 cin>>n; 10 if(n==1) 11 { 12 cout<<"0"<<endl; 13 return 0; 14 } 15 for(int i=2;i<=n-1;i++) 16 { 17 if(!b[i]) 18 { 19 prime[++cnt]=i; 20 faila[i]=i-1; 21 } 22 for(int j=1;j<=cnt&&i*prime[j]<=n-1;j++) 23 { 24 b[i*prime[j]]=1; 25 if(i%prime[j]==0) 26 { 27 faila[i*prime[j]]=faila[i]*prime[j]; 28 break; 29 } 30 faila[i*prime[j]]=faila[i]*(prime[j]-1); 31 } 32 } 33 for(int i=2;i<=n-1;i++) 34 sum+=faila[i]; 35 cout<<sum*2+3<<endl; 36 return 0; 37 }
标签:www,cout,show,int,仪仗队,P2158,include 来源: https://www.cnblogs.com/cptbtptpbcptbtptp/p/11234008.html