首页 > 代码库 > POJ 3090

POJ 3090

Visible Lattice Points
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5356 Accepted: 3136

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

4245231

Sample Output

1 2 52 4 133 5 214 231 32549

 

先画个图发现那些可见得点关于y=x对称

下三角中

并且除去x=0

每一行的可见点x,y互质

个数为euler_phi(x);

 1 #include <iostream> 2 #include <cmath> 3 using namespace std; 4  5 int euler_phi(int n) 6 { 7     int m=(int)sqrt(n+0.5); 8     int ans=n; 9     for(int i=2;i<=m;i++)10         if(n%i==0)11         {12             ans=ans/i*(i-1);13             while(n%i==0)n/=i;14         }15     if(n>1)ans=ans/n*(n-1);16     return ans;17 }18 19 int main()20 {21     int C;22     cin>>C;23     for(int c =1;c<=C;c++)24     {25         int N;26         cin>>N;27         int ans = 0;28        for(int i =2;i<=N;i++)29        {30            ans+=euler_phi(i);31        }32         ans = ans*2 +3;33         cout<<c<<" "<<N<<" "<<ans<<endl;34 35     }36 }