P8 Visible Lattice Points

本文介绍了一种计算第一象限内可见格点数量的算法。对于大小为N的区域,通过判断各点坐标是否互质来确定可见性,并统计可见点总数。提供了一个具体的实现代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
    }

 

转载于:https://www.cnblogs.com/yaobolove/p/4155170.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值