P8 Visible Lattice Points
Time limit:1000ms, Memory Limit:65536KB
Description
A lattice point (x, y) in the first quadrant (x and y being 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.
Image Link: http://blog.sina.com.cn/s/blog_4a7304560101ajjf.html
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 are 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 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
3
2
4
231
Sample Output
1 2 5
2 4 13
3 231 32549
Analysis:
The main method of seeking: is to use two mutual factorization solution, I personally think there is a way to find greatest common divisor solution!!!
#include <iostream> #include <math.h>using namespace Std;int main () { int n,m,k=0; int x, y; int count; int prime (int,int);cin>>m; while (m--) { cin>>n; count=2; k++; if (n==1) cout<<k<< "" <<n<< "" << "3"; else if (n>1) {for (x=1;x<=n;x++) {for (y=1;y<=n;y++) { if (prime (x, y) ==1) count++; } } cout<<k<< "" << n<< "" <<count; cout<<endl; } } return 0;} int prime (int u,int v) {int t,r;r=1;if (v>u) { t=u;u=v;v=t;} while ((R=U%V)!=0) { u=v; v=r; } return v; }
P8 Visible Lattice Points