【bzoj2818】: Gcd 数论-欧拉函数

【bzoj2818】: Gcd

考虑素数p<=n

gcd(xp,yp)=p 当 gcd(x,y)=1 xp,yp<=n满足条件

p对答案的贡献:

预处理前缀和就好了

 1 /* http://www.cnblogs.com/karl07/ */
 2 #include <cstdlib>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <cmath>
 6 #include <algorithm>
 7 using namespace std;
 8 
 9 #define ll long long
10 const int N=1e7+5;
11 ll phi[N],prime[N];
12 int cnt=0,n;
13 ll ans;
14 
15 void PHI(int n){
16     phi[1]=1;
17     for (int i=2;i<=n;i++){
18         if (!phi[i]){
19             prime[++cnt]=i;
20             for (int j=i;j<=n;j+=i){
21                 if (phi[j]==0) phi[j]=j;
22                 phi[j]=phi[j]/i*(i-1);
23             }
24         }
25     }
26 }
27 
28 int main(){
29     scanf("%d",&n);
30     PHI(n);
31     for (int i=1;i<=n;i++) phi[i]+=phi[i-1];
32     for (int i=1;i<=cnt;i++){
33         ans+=phi[n/prime[i]]*2-1;
34     }
35     printf("%lld\n",ans);
36     return 0;
37 }
View Code

 

posted @ 2017-03-19 17:33  karl07  阅读(196)  评论(0编辑  收藏  举报