POJ3090(SummerTrainingDay04-M 欧拉函数)

Visible Lattice Points

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7450   Accepted: 4536

Description

A lattice point (xy) 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 (xy) does not pass through any other lattice point. For example, the point (4, 2) is not visible since the line from the origin passes through (2, 1). The figure below shows the points (xy) with 0 ≤ xy ≤ 5 with lines from the origin to the visible points.

Write a program which, given a value for the size, N, computes the number of visible points (xy) with 0 ≤ xy ≤ 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

 1 //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 }
原文地址:https://www.cnblogs.com/Penn000/p/7287410.html