码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3090

时间:2014-07-23 16:32:21      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   

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.

bubuko.com,布布扣

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

 

先画个图发现那些可见得点关于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 }

POJ 3090,布布扣,bubuko.com

POJ 3090

标签:des   style   blog   http   color   os   

原文地址:http://www.cnblogs.com/Run-dream/p/3863247.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!