您的位置:首页 > 其它

POJ 3090 Visible Lattice Points

2014-08-27 10:50 330 查看
Visible Lattice Points

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5574 Accepted: 3282
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

Source

Greater New York 2006

题目大意:

        给你一个数n,让你求从(0 , 0) 到(i , j) (i<=n , j<=n)的线段上只有两个整数点的线段的条数。

思路:

        欧拉公式,求在n以内和n互质的个数。

       根据欧拉公式。φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn),

        其中p1, p2……pn为x的所有质因数,x是不为0的整数。

        φ(1)=1(唯一和1互质的数就是1本身)。

        (注意:每种质因数只一个。比如12=2*2*3

         欧拉公式那么φ(12)=12*(1-1/2)*(1-1/3)=4)

代码:

 
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
double sbreak(int m)//分解质因子
{
int i,k;
double sum;
int a[1005];
double t;
sum=m;
memset(a,0,sizeof(a));
k=0;
for(i=2;i*i<=m;i++)
{
if(m%i==0)
{
a[k++]=i;
}
while(m%i==0)
{
m/=i;
}
}
if(m!=1)
{
a[k++]=m;
}
for(i=0;i<k;i++)
{
t=1.0/a[i];
sum=sum*(1-t);
/*根据欧拉公式。φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn),
其中p1, p2……pn为x的所有质因数,x是不为0的整数。
φ(1)=1(唯一和1互质的数就是1本身)。
(注意:每种质因数只一个。比如12=2*2*3
欧拉公式那么φ(12)=12*(1-1/2)*(1-1/3)=4)*/
}
return sum;
}
int main()
{
int i,j,n,m,count;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&m);
count=0;
for(j=2;j<=m;j++)
{
// t=sbreak(j);
//printf("%d\n",t);
count+=sbreak(j);//求出来的count的值,只是在x=y这条直线的一侧的,并且不包含x=y上的点,和在想x,y轴上的点
}
printf("%d %d %d\n",i,m,2*count+3);//2*count表示两边的相加,3是x=y轴上的(1,1)点,还有x轴上的(1,0)点,y轴上的(0,1)点
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM poj 3090 欧拉公式