POJ 3090 Visible Lattice Points 布雷级数
2015-03-20 23:10
260 查看
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
先说一下格点的定义:在坐标x,y都是整数的点就是格点。
然后给出一个结论:两个点P1(x1,y1),P2(x2,y2)的连线上(不含P1,P2)格点的个数是:|x1-x2|和|y1-y2|的最大公约数-1.(见《挑战程序设计竞赛 第2版》)
这个题是都是点到(0,0)的连线,也就是求在N*N(第一象限)内有多少个点(x,y)到原点的连线上没有格点,那么就是gcd(x,y)=1,其实求多少对x,y互素。
那么问题就很明显了,设phi[j]表示1~j中与j互素的数的个数。 在N*N的点阵中,由对称性,我们只看原点和(N,N)的连线的下面的三角区域(由(0,0),(N,0)和(N,N)组成),要求当x=2时,有几个满足题意的格点;当x=3时,有几个满足;x=4时,,x=5,,,,x=N时有几个满足,然后求和phi[2]+phi[3]+phi[4]+……+phi
就是要求的答案(x=1时特殊处理一下)。
别忘了我们只处理下三角,还有上三角,由对称性,直接乘以2就好了。
phi[] 素数筛法
phi[1]+...+phip
布雷级数
(当x=1时有三个满足,(1,0),(1,1,),(0,1)满足,特殊处理一下就好了)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5779 | Accepted: 3409 |
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
先说一下格点的定义:在坐标x,y都是整数的点就是格点。
然后给出一个结论:两个点P1(x1,y1),P2(x2,y2)的连线上(不含P1,P2)格点的个数是:|x1-x2|和|y1-y2|的最大公约数-1.(见《挑战程序设计竞赛 第2版》)
这个题是都是点到(0,0)的连线,也就是求在N*N(第一象限)内有多少个点(x,y)到原点的连线上没有格点,那么就是gcd(x,y)=1,其实求多少对x,y互素。
那么问题就很明显了,设phi[j]表示1~j中与j互素的数的个数。 在N*N的点阵中,由对称性,我们只看原点和(N,N)的连线的下面的三角区域(由(0,0),(N,0)和(N,N)组成),要求当x=2时,有几个满足题意的格点;当x=3时,有几个满足;x=4时,,x=5,,,,x=N时有几个满足,然后求和phi[2]+phi[3]+phi[4]+……+phi
就是要求的答案(x=1时特殊处理一下)。
别忘了我们只处理下三角,还有上三角,由对称性,直接乘以2就好了。
phi[] 素数筛法
phi[1]+...+phip
布雷级数
(当x=1时有三个满足,(1,0),(1,1,),(0,1)满足,特殊处理一下就好了)
#include <stdio.h> #include <string.h> const int maxn=1000+10; int phi[maxn]; void phi_table(){ int i,j; memset(phi,0,sizeof(phi)); phi[1]=1; for(i=2;i<=maxn;i++) if(!phi[i]) for(j=i;j<=maxn;j+=i){ if(!phi[j]) phi[j]=j; phi[j]=phi[j]/i*(i-1); } } int s[maxn]; //布雷级数 void cal(){ int i; s[1]=0; s[2]=phi[2]; for(i=3;i<=maxn;i++) s[i]=s[i-1]+phi[i]; } int main() { int i,T,kcase=1,n; phi_table(); cal(); scanf("%d",&T); while(T--){ scanf("%d",&n); printf("%d %d %d\n",kcase++,n,s *2+3); } return 0; }
相关文章推荐
- POJ 3090 Visible Lattice Points 法雷级数
- POJ 3090 (欧拉函数) Visible Lattice Points
- POJ 3090-Visible Lattice Points(筛选法求欧拉函数)
- poj 3090 Visible Lattice Points
- POJ 3090 Visible Lattice Points (欧拉函数)
- POJ 3090 Visible Lattice Points 欧拉函数
- POJ 3090 Visible Lattice Points 法雷级数
- 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(欧拉函数)
- POJ 3090 Visible Lattice Points 欧拉函数
- poj3090 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