Visible lattice pointstime limit: 1000 msmemory limit: 65536 ktotal submissions: 5653 accepted: 3331 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) 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 (x, y) with 0 ≤ x, y ≤ 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 (x, y) with 0 ≤ x, y ≤ 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 2006 visible lattice points
There is a two-dimensional coordinate system, which is only a bit of discrete integer coordinates.
Now we are standing around at N points. Ask how many points can be seen.
If you see (), then () after )... It is blocked.
No more.
When 1*1 is considered, there are three points ).
() And () about () Symmetry
When we look at 2*2 again, there is a point)
() And () about () Symmetry
() And () about () Symmetry
There are two more points than 1*1. And they are all about () symmetry, while () is blocked ().
So we only consider the lower triangle. The result * 2 + 1 is the final answer.
Because the points with the same slope are covered by the first point and cannot be seen, we only consider the number of slope types to get the result.
When 1*1, the slope is 0.
2*2, the slope is 0, 1/2
When 3*3, the slope is 0, 1, 2, 1, 3, 2, and 3.
4*4, the slope is 0, 1/2 (2/4), 1/3, 2/3, 1/4, 3/4;
5*5, the slope is 2/4/2 (1/3), 2/3, 1/4, 3/4, 1/5, 2/5, 3/5, 4/5
When 6*6, the slope is 0, 1/2 (2/4, 3/6), 1/3 (2/6), 2/3 (4/6), 1/4, 3/4, 1/5, 2/5, 3/5, 4/5, 1/6, 5/6
It can be seen that, in fact, it is to find the true score of the denominator less than or equal to n.
This is just the Euler's function. here we can use both the ordinary Euler's function and quick search for the Euler's function.
Reference blog: http://blog.csdn.net/zhang20072844/article/details/8108727
# Include <stdio. h> int prime [1010], Phi [1001]; bool unprime [1010]; void Euler () // quickly evaluate the Euler function {int I, j, k = 0; for (I = 2; I <= 1000; I ++) {If (! Unprime [I]) {Prime [k ++] = I; Phi [I] = I-1;} For (j = 0; j <K & I * prime [J] <= 1000; j ++) {unprime [prime [J] * I] = true; if (I % prime [J]! = 0) Phi [prime [J] * I] = Phi [I] * (Prime [J]-1 ); else {Phi [prime [J] * I] = Phi [I] * prime [J]; break ;}}} int main () {int C, N; euler (); Phi [1] = 1; scanf ("% d", & C); int Kase = 1; while (c --) {scanf ("% d ", & N); int sum = 0; For (INT I = 1; I <= N; I ++) sum + = Phi [I]; printf ("% d \ n", Kase ++, N, 2 * sum + 1);} return 0 ;}
# Include <stdio. h> # include <math. h> int Euler (int n) // returns the Euler's {int I, ret = N; for (I = 2; I <= SQRT (1.0 * n ); I ++) {If (N % I = 0) {ret = ret-RET/I;} while (N % I = 0) N/= I ;} if (n> 1) ret = ret-RET/N; return ret;} int main () {int C, N; scanf ("% d", & C ); int Kase = 1; while (c --) {scanf ("% d", & N); int sum = 0; For (INT I = 1; I <= N; I ++) sum + = Euler (I); printf ("% d \ n", Kase ++, N, 2 * sum + 1 );}}
Poj3090_visible lattice points [Euler's function]