uva10820 send a table (nlogn求1-n欧拉函数值模版
//重点就是求1-n的欧拉函数啦,重点是nlogn求法的版
//大概过程类似于筛选法求素数
1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4 #include<algorithm> 5 #include<cstring> 6 #include<cstdlib> 7 #include<queue> 8 #include<vector> 9 #include<map> 10 #include<stack> 11 #include<string> 12 13 using namespace std; 14 15 int n; 16 int phi[50001]; 17 int f[50001]; 18 19 int main(){ 20 memset(phi,0,sizeof(phi)); 21 memset(f,0,sizeof(f)); 22 phi[1]=1; 23 for (int i=2;i<=50000;i++){ 24 if (phi[i]==0){ 25 for (int j=i;j<=50000;j+=i){ 26 if (phi[j]==0) phi[j]=j; 27 phi[j]=phi[j]/i*(i-1); 28 } 29 } 30 } 31 for (int i=1;i<=50000;i++) f[i]=f[i-1]+phi[i]; 32 while (scanf("%d",&n)==1){ 33 if (n==0) return 0; 34 printf("%d\n",f[n]*2-1); 35 } 36 return 0; 37 } 38 /* 39 2 40 5 41 0 42 */