P2158 [SDOI2008]仪仗队 线性筛(欧拉函数和素数表)
上三角行恰好是[1,n-1]的欧拉函数
http://www.luogu.org/problem/show?pid=2158#sub
1 //#pragma comment(linker, "/STACK:167772160") 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <iostream> 6 #include <queue> 7 #include <stack> 8 #include <cmath> 9 #include <set> 10 #include <utility> 11 #include <algorithm> 12 #include <vector> 13 #include <map> 14 // #include<malloc.h> 15 using namespace std; 16 #define clc(a,b) memset(a,b,sizeof(a)) 17 #define LL long long 18 void fre() { 19 freopen("in.txt","r",stdin); 20 } 21 const int inf = 0x3f3f3f3f; 22 #define eps 1e-8 23 // const double pi = acos(-1); 24 const LL mod = 1e9+7; 25 inline int r(){ 26 int x=0,f=1;char ch=getchar(); 27 while(ch>'9'||ch<'0'){if(ch=='-') f=-1;ch=getchar();} 28 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 29 return x*f; 30 } 31 const int maxn=1e7; 32 const int N=40000; 33 bool vis[maxn]; 34 int phi[maxn]; 35 int prime[maxn]; 36 int tot; 37 void p_pri(){ 38 // clc(vis,0); 39 phi[1]=1; 40 tot=0; 41 int i,j; 42 for(i=2;i<=N;i++){ 43 if(!vis[i]){ 44 prime[tot++]=i; 45 phi[i]=i-1; 46 } 47 for(j=0;j<tot;j++){ 48 if(i*prime[j]>N) break; 49 vis[i*prime[j]]=true; 50 if(i%prime[j]==0){ 51 phi[i*prime[j]]=phi[i]*prime[j]; 52 break; 53 } 54 else{ 55 phi[i*prime[j]]=phi[i]*(prime[j]-1); 56 } 57 } 58 } 59 } 60 61 int main(){ 62 int n; 63 LL ans; 64 ans=0; 65 scanf("%d",&n); 66 p_pri(); 67 for(int i=1;i<n;i++){ 68 ans+=phi[i]; 69 } 70 cout<<ans*2+1<<endl; 71 return 0; 72 }