poj3090 Visible Lattice Points [欧拉函数]

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

一个点(x,y)能被射中的充分必要条件是x,y互质,是不是很像欧拉函数?
经过容斥可以得到,若用sum表示1...n的phi值,ans=sum*2+2-1=sum*2+1
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 using namespace std;
 5 #define dbg(x) cout<<#x<<" = "<<x<<endl
 6 
 7 const int maxn=1005;
 8 
 9 bool check[maxn];
10 int prime[maxn],phi[maxn],n,dat[maxn],phisum[maxn];
11 int ans=0,cnt=0,T;
12 
13 void getphi(){
14     phi[1]=1;
15     for(int i=2;i<=n;i++){
16         if(!check[i]){
17             prime[++cnt]=i;
18             phi[i]=i-1;
19         }
20         for(int j=1;j<=cnt&&prime[j]*i<=n;j++){
21             check[i*prime[j]]=1;
22             if(i%prime[j])  phi[i*prime[j]]=phi[i]*(prime[j]-1);
23             else{
24                 phi[i*prime[j]]=phi[i]*prime[j];
25                 break;
26             }
27         }
28     }
29     for(int i=1;i<=n;i++)  phisum[i]=phisum[i-1]+phi[i];
30 }
31 
32 int main(){
33     scanf("%d",&T);
34     for(int i=1;i<=T;i++){
35         scanf("%d",&dat[i]);
36         n=max(dat[i],n);
37     }
38     getphi();
39     for(int i=1;i<=T;i++){
40         printf("%d %d %d\n",i,dat[i],(phisum[dat[i]]<<1)+1);
41     }
42     return 0;
43 }

 

posted @ 2017-08-01 23:58  ZYBGMZL  阅读(153)  评论(0编辑  收藏  举报