POJ 3090 Visible Lattice Points
来源:互联网 发布:java简单投票系统 编辑:程序博客网 时间:2024/04/29 06:22
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.
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
4245231
Sample Output
1 2 52 4 133 5 214 231 32549
思路:实际上题目在说的时候已经为你提供了思路,那就是比较斜率了。只要是斜率没有出现的点,那么我们就可以加上这些点,我们发现,每多一个数,就意味着要多一个外层,我们只需要加上这些外层上的点就可以了,而且一个外层上的点是对称的!
所以AC代码:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int a[1005][1005]; //用数组的标号代表x,y坐标,用数组的值表示斜率是否出现过int b[1005]; //记录有多少个点int gcd(int a,int b){ if(a<b) swap(a,b); if(b==0) return a; return gcd(b,a%b);}int main(){ int i,n,j,t,cnt=1; memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); b[1]=3; b[2]=5; a[1][1]=a[1][2]=a[2][2]=a[2][1]=1; for(i=3;i<=1005;i++) { int sum=0; for(j=1;j<i;j++) { int c=gcd(i,j); if(!a[i/c][j/c])//确认其斜率是否出现过,把他们的坐标点除以它们的最大公约数 { sum++; a[i][j]=1; } } b[i]=b[i-1]+sum*2; } scanf("%d",&t); while(t--) { scanf("%d",&n); printf("%d %d %d\n",cnt++,n,b[n]); } return 0;}
- POJ 3090 Visible Lattice Points
- poj 3090 Visible Lattice Points
- poj 3090 Visible Lattice Points
- POJ 3090 Visible Lattice Points
- POJ 3090 Visible Lattice Points
- POJ 3090 Visible Lattice Points
- POJ - 3090 Visible Lattice Points
- POJ 3090 Visible Lattice Points
- poj Visible Lattice Points(3090)
- POJ 3090 Visible Lattice Points
- POJ 3090 Visible Lattice Points
- POJ 3090 Visible Lattice Points
- POJ-3090 Visible Lattice Points
- POJ 3090 Visible Lattice Points
- poj 3090:Visible Lattice Points
- POJ 3090 Visible Lattice Points[]
- POJ 3090 Visible Lattice Points
- poj 3090 Visible Lattice Points
- 处理程序“WebServiceHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”
- Kalevich Strikes Back
- Hibernate学习_008_update()等方法总结
- Basics of an IDL file
- centos 6.4 QT5 的安装,找不到GLIBCXX_3.4.15的解决办法
- POJ 3090 Visible Lattice Points
- Linux 用户及用户组的相关操作
- Seajs 基础----使用SeaJS实现模块化JavaScript开发
- 组合查询——继承与多态的小练习
- UVA654- Ratio(暴力枚举)
- ios硬件特性(通讯录,邮件,短信,重力感应)
- C++ 孙鑫视屏学习01
- 圆形进度条定制
- poj Visible Lattice Points(3090)