POJ 3090 Visible Lattice Points
2014-08-27 10:50
330 查看
Visible Lattice Points
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.
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
Sample Output
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)
代码:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5574 | Accepted: 3282 |
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.
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
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; }
相关文章推荐
- poj 2478——Farey Sequence & poj 3090——Visible Lattice Points
- Poj3090 Visible Lattice Points 欧拉函数求和
- POJ 3090 Visible Lattice Points 欧拉函数
- POJ 3090 Visible Lattice Points(莫比乌斯反演)
- 法雷级数(poj 3090 Visible Lattice Points)
- POJ 3090 Visible Lattice Points(Farey数列&欧拉函数求和)
- POJ 3090-Visible Lattice Points(筛选法求欧拉函数)
- POJ 3090 Visible Lattice Points (ZOJ 2777)
- POJ 3090 Visible Lattice Points[]
- POJ 3090 Visible Lattice Points | 其实是欧拉函数
- poj 3090 Visible Lattice Points 欧拉函数打表求和
- POJ 3090 Visible Lattice Points 布雷级数
- poj3090 Visible Lattice Points(莫比乌斯反演)
- Visible Lattice Points POJ - 3090
- POJ 3090 Visible Lattice Points (ZOJ 2777)
- POJ 3090 Visible Lattice Points (欧拉函数)
- POJ 3090 Visible Lattice Points 笔记
- (Relax 数论1.9)POJ 3090 Visible Lattice Points(欧拉函数的应用:计算前n项欧拉数之和)
- poj 3090:Visible Lattice Points
- poj 3090 Visible Lattice Points