首页 > 其他 > 详细

POJ 3090 Visible Lattice Points

时间:2014-08-16 21:11:56      阅读:317      评论:0      收藏:0      [点我收藏+]

Description

A lattice point (xy) 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 (xy) 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 (xy) with 0 ≤ xy ≤ 5 with lines from the origin to the visible points.

bubuko.com,布布扣

Write a program which, given a value for the size, N, computes the number of visible points (xy) with 0 ≤ xy ≤ 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

4
2
4
5
231

Sample Output

1 2 5
2 4 13
3 5 21
4 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,布布扣,bubuko.com

POJ 3090 Visible Lattice Points

原文:http://blog.csdn.net/u012313382/article/details/38616011

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!