poj2478 Farey Sequence 欧拉函数
题意:欧拉函数前n项和.
思路:欧拉函数
欧拉函数:
φ函数的值 通式:φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn) ,其中p1, p2……pn为x的所有质因数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 using namespace std; 3 const int N = 1e6+5; 4 long long phi[N]; 5 6 void Init() 7 { 8 int i, j; 9 for (i=1; i<N; i++) 10 phi[i] = i; 11 for (i=2; i<N; i+=2) 12 phi[i] /= 2; //质因子有2的数字 phi[i] = phi[i] * ( 1- 1/2) 13 14 for (i=3; i<N; i+=2) 15 if (phi[i] == i) // 16 for (j=i; j<N; j+=i) //质因子有 i 的数字 17 phi[j] = phi[j] / i * (i - 1); 18 //for (i=1; i<20; i++) 19 // cout<<i<<" "<<phi[i]<<endl; 20 for (i=3; i<N; i++) 21 phi[i] += phi[i-1]; 22 23 } 24 25 int main() 26 { 27 int n; 28 Init(); 29 while (cin>>n && n) 30 cout<<phi[n]<<endl; 31 return 0; 32 }