仪仗队

题目链接: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],phi[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             phi[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                 phi[i*prime[j]]=phi[i]*prime[j];
28                 break;
29             }
30             phi[i*prime[j]]=phi[i]*(prime[j]-1);
31         }
32     }
33     for(int i=2;i<=n-1;i++)
34     sum+=phi[i];
35     cout<<sum*2+3<<endl;
36     return 0; 
37 }

 

posted @ 2019-07-23 19:48  LyingFlat666  阅读(211)  评论(0编辑  收藏  举报