POJ 3090 Visible Lattice Points(欧拉函数)

来源:互联网 发布:哪个软件加油便宜 编辑:程序博客网 时间:2024/06/08 18:04
                                                                                                                      Visible Lattice Points
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 5390 Accepted: 3157

Description

A lattice point (x, y) in the first quadrant (x andy 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, yN.

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

Source

Greater New York 2006

 

 

/*由图我们可以看出,只有横坐标和纵坐标互素的时候才可以看见该点,
如果横纵坐标不互素,那么他们肯定可以约分,化为更简单的形式,
约分后横纵坐标的数值都会变小,这两个变小的坐标必然在前面出现过。
由此问题转化成了求欧拉函数值累加的问题。
沿着k=1斜率线,将第一象限分成两份,
我们要求的结果就是2倍的(2,3。。n)的欧拉函数值,
再加上1的欧拉函数值,再加上2(其中一个是斜率为0,一个是无穷大的上下方向)*/
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=1000+10;
int phi[maxn];//用来存储欧拉函数值
int n;//表示点横坐标和纵坐标在0-n范围内
int main()
{
 void phi_table(int);
 int t,i,kase=0;
 phi_table(maxn-1);/*注意这里的参数不要写成maxn,因为maxn为数组的大小,最大下标为maxn-1,所以所能求的
                     最大数为maxn-1的欧拉函数*/
 cin>>t;
 while(t--)
 {
  cin>>n;
  int ans=0;
  for(i=1;i<=n;i++)
  {ans+=phi[i];}
  ans=2*ans+1;
  kase++;
  cout<<kase<<" "<<n<<" "<<ans<<endl;
 }
 return 0;
}


void phi_table(int n)
{
 int i,j;
    for(i=2;i<=n;i++)phi[i]=0;
    phi[1]=1;
    for(i=2;i<=n;i++)
        if(!phi[i])
            for(j=i;j<=n;j+=i)
            {
                if(!phi[j])phi[j]=j;
                phi[j]=phi[j]/i*(i-1);
            }
}

 

0 0