first, the topic
A Lattice Point (x, y) in the first quadrant (x and y is integers greater than or equ Al to 0), other than the origin, was visible from the origin if the line from (0, 0) to (x, y) does not Pass through any and 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, yx, y ≤ N.
Input
The first line of input contains a single integer C (1≤ C ≤1000) which is the number of datasets T Hat 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 are to being one line of output consisting of:the dataset number starting at 1, a single space, the S Ize, a single space and the number of the visible points for the size.
Sample Input
4245231
Sample Output
1 2 52 4 133 5 214 231 32549
Second, test instructions analysis
This problem test instructions compare understood, given a number N, represents the two-dimensional plane in the first quadrant of a square edge length, you can get (n+1) * (n+1) a whole point. Then ask in addition to the origin of the (n+1) ^2-1 points, how many points connected to the origin point, two points between the lines of the line is the point of the wood.
Supplement Nutrition: Read the Challenge Program design contest students must know, given a right triangle two side side length A, B, then GCD (A, b) +1 represents the number of the hour on this edge. Remove two endpoints so GCD (A, b) -1=0 is not the question to be satisfied, that is gcd (A, b) = 1.
Conversion: This problem can be converted to the Euler function value of a number n by the above knowledge. Then we analyze,n=1 when the total of 4 points, remove the origin has 3 points satisfied, the result is f[1] = 3. N=2 when the total of 9 points,n=1 When the point of satisfaction at this time is also bound to meet. It is important to note that, because it is in the two-dimensional plane, the outermost side of the outer edge has φ (2) points to satisfy, then the other side also has φ (2) points to meet, then is f[1] + 2*φ (2). After the same principle, you get a recursive
F[n] = f[n-1] + 2*φ (N), where f[1] = 3
Then the linear sieve method is used to calculate the value of Euler function, and then recursion with another array.
third, the code
#include <iostream> #include <cstring>using namespace std;const int maxn = 1e3+5;int PRIME[MAXN], PHI[MAXN], n Prime;long long ans[maxn];void Euler () {memset (phi, 0, sizeof (PHI)); PHI[1] = 1; Nprime = 0; for (int i = 2; i < MAXN; i++) {if (! Phi[i]) {phi[i] = i-1; prime[nprime++] = i; } for (int j = 0; J < nprime && I*prime[j] < MAXN; J + +) {if (I%prime[j]) {Phi[i*prime[j]] = phi[i]* (Prime[j]-1); } else {Phi[i*prime[j]] = phi[i]*prime[j]; Break }}}}void Solve () {Euler (); ANS[1] = 3; for (int i = 2; i < MAXN; i++) {ans[i] = ans[i-1] + phi[i]*2; }}int Main () {int T, N; Cin >> T; Solve (); for (int i = 1; I <= T; i++) {cin >> N; cout << i << ' << N << ' << ans[n] <&Lt Endl } return 0;}
poj_3090 Visible Lattice Points "Euler function + recursion"