Description
A Lattice Point (X,Y) In the first quadrant (XAndYAre integers greater than or equal to 0), other than the origin, is visible from the origin if the line from (0, 0) (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 integerC(1 ≤C≤ 1000) which is the number of datasets that follow.
Each dataset consists of a single line of input containing a single integerN(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
Train of Thought: in fact, the question has already provided you with a train of thought, that is, the comparison slope. As long as it is a point where the slope does not appear, we can add these points. We find that each more number means an outer layer, we only need to add the points on the outer layer, and the points on the outer layer are symmetric!
So the AC code:
# Include <stdio. h> # include <string. h ># include <algorithm> using namespace STD; int A [1005] [1005]; // use the array label to represent the X and Y coordinates, use the array value to indicate whether the slope has exceeded int B [1005]; // number of points in the record int gcd (int A, int B) {if (a <B) swap (a, B); If (B = 0) return a; return gcd (B, A % B);} int main () {int I, n, J, t, CNT = 1; memset (A, 0, sizeof (a); memset (B, 0, sizeof (B); B [1] = 3; B [2] = 5; A [1] [1] = A [1] [2] = A [2] [2] = A [2] [1] = 1; for (I = 3; I <= 1005; I ++) {int sum = 0; For (j = 1; j <I; j ++ ) {Int c = gcd (I, j); If (! A [I/c] [J/C]) // check whether the slope has occurred. Divide their coordinate points by their maximum common appointment number {sum ++; A [I] [J] = 1 ;}} B [I] = B [I-1] + sum * 2;} scanf ("% d", & T ); while (t --) {scanf ("% d", & N); printf ("% d \ n", CNT ++, n, B [N]);} return 0 ;}