Visible Lattice Points - POJ 3090 欧拉公式

本文介绍了一种利用欧拉公式解决可见格点问题的方法。给定一个正方形网格的大小N,通过算法计算出从原点出发能够直接“看见”的格点数量。文章详细解释了算法原理,并提供了一个具体的实现案例。

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

Visible Lattice Points
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5239 Accepted: 3049

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.

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

这道题是欧拉公式,给你t组数据,每组数据里有有1个n,建立一个(n+1)*(n+1)的点图,问你从左下角的的点可以连到多少个点。(每条连线不能在途中包含其他点)

看的时候先把从左下到右上的对角线去掉,剩下的两个部分是相同的。拿该图来说,把从左下到右上的对角线去掉后下面的那个三角形是一个5*5的三角形,再用欧拉公式算1-5的和(把1的值看做1),1→1,2→1,3→2,4→2,5→4,可以在图中试着找到对应的点。如第五列第三行对应的1/4,第五列第五行对应的3/4,第四列第四行对应的1/3,第四列第五行对应的2/3。最后结果为(1-n的欧拉和)*2+1(对角线还还有一个)

输出结果为 第t组数据  给你的n数  结果

#include<cstdio>
using namespace std;

int p[1005];     //p[j]表示小于j的正整数与j互斥的数有多少个,比如j=4,那么1/4,3/4共两个
int s[1005];     //s[j]表示从p[2]加到p[j]一共有多少个数
int main()
{ int i,j,t=1,n,k;
  p[1]=1;        //设p[1]为1
  for(i=2;i<=1000;i++)      //欧拉公式
  { if(!p[i])               //如果这个数是素数
    { for(j=i;j<=1000;j+=i) //所有能被这个数整除的数
      { if(!p[j])           //没有涉及过的数先初始化一下p[j]的个数
         p[j]=j;
        p[j]=p[j]/i*(i-1);  //都除以i再乘以(i-1)
      }
    }
  }
  for(i=1;i<=1000;i++)      //算从p[1]加到p[i]一共有多少个数
   s[i]=s[i-1]+p[i];
  scanf("%d",&n);
  while(n--)
  { scanf("%d",&k);
    printf("%d %d %d\n",t++,k,s[k]*2+1);
  }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值