POJ3090(SummerTrainingDay04-M 歐拉函數)
Visible Lattice Points
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7450 | Accepted: 4536 |
Description
A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), other than the origin, is visible from the origin if the line from (0, 0) to (x, y
Write a program which, given a value for the size, N
Input
The first line of input contains a single integer C (1 ≤ C ≤ 1000) which is the number of datasets that follow.
Each dataset consists of a single line of input containing a single integer N (1 ≤ N ≤ 1000), which is the size.
Output
For each dataset, there is to be one line of output consisting of: the dataset number starting at 1, a single space, the size, a single space and the number of visible points for that size.
Sample Input
4 2 4 5 231
Sample Output
1 2 5 2 4 13 3 5 21 4 231 32549
Source
Greater New York 20061 //2017-08-04 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 7 using namespace std; 8 9 const int N = 1010; 10 int phi[N],prime[N],tot,ans; 11 bool book[N]; 12 13 void getphi() 14 { 15 int i,j; 16 phi[1]=1; 17 for(i=2;i<=N;i++)//相當於分解質因式的逆過程 18 { 19 if(!book[i]) 20 { 21 prime[++tot]=i;//篩素數的時候首先會判斷i是否是素數。 22 phi[i]=i-1;//當 i 是素數時 phi[i]=i-1 23 } 24 for(j=1;j<=tot;j++) 25 { 26 if(i*prime[j]>N) break; 27 book[i*prime[j]]=1;//確定i*prime[j]不是素數 28 if(i%prime[j]==0)//接著我們會看prime[j]是否是i的約數 29 { 30 phi[i*prime[j]]=phi[i]*prime[j];break; 31 } 32 else phi[i*prime[j]]=phi[i]*(prime[j]-1);//其實這裏prime[j]-1就是phi[prime[j]],利用了歐拉函數的積性 33 } 34 } 35 } 36 37 int solve(int n){ 38 int ans = 0; 39 for(int i = 2; i <= n; i++){ 40 ans += phi[i]; 41 } 42 return ans*2+3; 43 } 44 45 int main() 46 { 47 int T, n, kase = 0; 48 getphi(); 49 scanf("%d", &T); 50 while(T--){ 51 scanf("%d", &n); 52 cout<<++kase<<" "<<n<<" "<<solve(n)<<endl; 53 } 54 55 return 0; 56 }
POJ3090(SummerTrainingDay04-M 歐拉函數)