Visible lattice points
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:5636 |
|
Accepted:3317 |
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
Source
Greater New York 2006
Mean:
In the first quadrant, enter N, and then count the number of viewpoints in the range of (0 <= x <= N, 0 <= Y <= N.
The so-called viewpoint can be reached (x1, Y1) starting from (0, 0) without any point of intersection in the middle.
Analyze:
Through analysis, we will find that as long as X and Y are mutually qualitative, (x, y) is the viewpoint. We only need to obtain [0, 0] ~ If the number of vertices (x, y) in [x, y] that meet the requirements of X and Y, the problem can be solved. The Euler's function is used to solve the number of numbers less than N and the mutual quality of N.
Time Complexity:O (N)
Source code:
// Memory time // 1347 K 0 Ms //: snarl_jsb // 2014-09-12-22.35 # include <algorithm> # include <cstdio> # include <cstring> # include <cstdlib> # include <iostream> # include <vector> # include <queue >#include <stack> # include <map> # include <string> # include <climits> # include <cmath> # define n 1000010 # define ll long longusing namespace STD; int gcd (int A, int B) {return B? Gcd (B, A % B): A;} inline int lcm (int A, int B) {return a/gcd (a, B) * B ;} int Eular (int n) /// evaluate 1 .. n-1: the number of the numbers of mutex-N {int ret = 1, I; for (I = 2; I * I <= N; I ++) if (N % I = 0) {n/= I, RET * = I-1; while (N % I = 0) N/= I, RET * = I ;} if (n> 1) RET * = n-1; return ret;} int main () {// freopen ("C: \ Users \ Asus \ Desktop \ cin. CPP "," r ", stdin); // freopen (" C: \ Users \ Asus \ Desktop \ cout. CPP "," W ", stdout); int T, CAS = 1; CIN> T; while (t --) {int N; CIN> N; ll ans = 0; For (INT I = 1; I <= N; I ++) {ans + = Eular (I );} printf ("% d \ n", CAS ++, N, ANS * 2 + 1);} return 0 ;}
Number Theory-Using Euler's function --- poj 3090: visible lattice points