首页 > 代码库 > P8 Visible Lattice Points
P8 Visible Lattice Points
P8 Visible Lattice Points
Time Limit:1000ms, Memory Limit:65536KB
Description
A lattice point (x, y) in the first quadrant (x and y are 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.
图片链接: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 is 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 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
3
2
4
231
Sample Output
1 2 5
2 4 13
3 231 32549
分析:
主要求法:是用两个互质因数的求解,我个人认为有求最大公约数的方法进行求解!!!
#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