【欧拉函数】BZOJ2818-GCD

怎么漏了这一道……本来想要水一水,结果忘记了φ[1]=1,果然要滚一遍前面的知识……

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 const int MAXN=10000000+50;
 7 typedef long long ll;
 8 int n;
 9 int prime[MAXN],pnum=0;
10 int phi[MAXN];
11 ll sum[MAXN];
12 
13 void get_phi(int maxn)
14 {
15     memset(phi,0,sizeof(phi));
16     memset(sum,0,sizeof(sum));
17     phi[1]=sum[1]=1;
18     for (int i=2;i<=maxn;i++)
19     {
20         if (phi[i]==0)
21         {
22             prime[++pnum]=i;
23             phi[i]=i-1;
24         }
25         for (int j=1;j<=pnum;j++)
26         {
27             if (i*prime[j]>maxn) break;
28             if (i%prime[j]==0) phi[i*prime[j]]=phi[i]*prime[j];
29                 else phi[i*prime[j]]=phi[i]*(prime[j]-1);
30         }
31         sum[i]=sum[i-1]+phi[i];
32     }
33 }
34 
35 int main()
36 {
37     scanf("%d",&n);
38     get_phi(n);
39     ll ans=0;
40     for (int i=1;i<=pnum;i++)
41     {
42         ans+=sum[n/prime[i]]*2-1;
43     }
44     printf("%lld",ans);
45     return 0;    
46 } 

 

posted @ 2016-07-11 22:31  iiyiyi  阅读(176)  评论(0编辑  收藏  举报