欧拉函数-bzoj2818-简单推导
This article is made by Jason-Cow.
Welcome to reprint.
But please post the writer's address.
http://www.cnblogs.com/JasonCow/
/*
ans=
sigma(for each prime<=n)
{
{
simga(for i=1 to n/now prime) phi[i]
}*2 -1
}
*/
1 #include <cstdio>
2 int GI(){
3 int x=0,c=getchar(),f=0;
4 while(c<'0'||c>'9'){if(c=='-')f=1;c=getchar();}
5 while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();
6 return f?-x:x;
7 }
8 const int maxn=(int)1e7+10;
9 int p[maxn],cnt;bool flag[maxn];long long ans,phi[maxn];
10 void init(int n){
11 phi[1]=1;
12 for(int i=2;i<=n;i++){
13 if(!flag[i])p[++cnt]=i,phi[i]=i-1;
14 for(int j=1;j<=cnt && i*p[j]<=n;j++){
15 flag[i*p[j]]=1;
16 if(i%p[j]==0){phi[i*p[j]]=phi[i]*p[j];break;}
17 phi[i*p[j]]=phi[i]*(p[j]-1);
18 }
19 }
20 for(int i=2;i<=n;i++)phi[i]+=phi[i-1];
21 }
22 int main(){
23 int n=GI();
24 init(n);
25 for(int i=1;i<=cnt;i++)ans+=phi[n/p[i]]*2-1;
26 printf("%lld\n",ans);
27 return 0;
28 }
~~Jason_liu O(∩_∩)O