Visible Lattice Points
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 5779 |
|
Accepted: 3409 |
Description
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
Source
let's start with the definition of lattice: the point at which coordinates x, y are integers are the lattice points. then we give a conclusion: two points P1 (x1,y1), P2 (x2,y2) on the line (excluding P1,P2) the number of lattice points are: |x1-x2| and |y1-y2| Greatest common divisor-1. (See Challenge Program Design Competition 2nd edition)the problem is to point to (0,0) the line, that is, in the N*n (the first quadrant) how many points (x, Y) to the origin of the line without lattice, then is gcd (x, y) = 1, in fact, how many pairs of x, Y. then the problem is obvious, set PHI[J] to indicate the number of 1~j and J. In the N*n lattice, by symmetry, we only look at the origin and (n,n) of the line below the triangular region (composed of (0,0), (n,0) and (n,n), required when x=2, there are several lattice points to meet the test instructions, when x=3, there are several satisfied; x=4, x=5,,,,x= N when there are several satisfying, then sum phi[2]+phi[3]+phi[4]+......+phi[n] is the required answer (x=1 special treatment). Don't forget we only deal with the next triangle, and the upper triangle, from the symmetry, directly multiplied by 2 is good.
phi[] Prime sieve methodPhi[1]+...+phip[n] Bray series
(when X=1 is three satisfied, (1,0), (1, 1,), (0,1) satisfied, special treatment will be good)
#include <stdio.h> #include <string.h>const int maxn=1000+10;int phi[maxn];void phi_table () {int I,j;memset (Phi,0,sizeof (PHI));p hi[1]=1;for (i=2;i<=maxn;i++) if (!phi[i]) for (j=i;j<=maxn;j+=i) {if (!phi[j]) phi[j]=j; phi[j]=phi[j]/i* (i-1);}} int s[maxn];//Bray series void Cal () {int i;s[1]=0;s[2]=phi[2];for (i=3;i<=maxn;i++) s[i]=s[i-1]+phi[i];} int main () {int i,t,kcase=1,n;phi_table (), Cal (), scanf ("%d", &t), while (t--) {scanf ("%d", &n);p rintf ("%d%d%d\n ", kcase++,n,s[n]*2+3);} return 0;}
POJ 3090 Visible Lattice Points Bray Series