POJ 3090 ZOJ 2777 UVALive 3571 Visible Lattice Points(用递推比用欧拉函数更好)
2016-08-19 10:07
519 查看
题目:
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
这个题目当然可以用欧拉函数来做,不过我更喜欢这个递推的方法。
代码:
代码很简洁而且非常高效,16ms AC
这个递推式是什么原理呢?
首先解释一下,这个r不是答案,r+2才是答案,也就是说,
我没有计算x=0或者y=0的情况,也就是没有计算(0,1)和(1,0)这2个点。
然后怎么算r呢?
枚举gcd(x,y)
gcd(x,y)=1的情况有r
种,gcd(x,y)=2的情况有r[n/2]种
gcd(x,y)=3的情况有r[n/3]种......
所有的加起来,一共是n*n,这就得到了递推式。
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
4 2 4 5 231
Sample Output
1 2 5 2 4 13 3 5 21 4 231 32549
这个题目当然可以用欧拉函数来做,不过我更喜欢这个递推的方法。
代码:
#include<iostream> #include<stdio.h> using namespace std; int r[1001]; int main() { r[1] = 1; for (int i = 2; i <= 1000; i++) { r[i] = i*i; for (int j = 2; j <= i; j++)r[i] -= r[i / j]; } int t, n; cin >> t; for (int cas = 1; cas <= t; cas++) { cin >> n; cout << cas << " " << n << " " << r[n] + 2 << endl; } return 0; }
代码很简洁而且非常高效,16ms AC
这个递推式是什么原理呢?
首先解释一下,这个r不是答案,r+2才是答案,也就是说,
我没有计算x=0或者y=0的情况,也就是没有计算(0,1)和(1,0)这2个点。
然后怎么算r呢?
枚举gcd(x,y)
gcd(x,y)=1的情况有r
种,gcd(x,y)=2的情况有r[n/2]种
gcd(x,y)=3的情况有r[n/3]种......
所有的加起来,一共是n*n,这就得到了递推式。
相关文章推荐
- uvalive 3401 poj 2741 Colored Cubes
- POJ 2478 Farey Sequence(法雷级数+欧拉函数递推)
- uvalive 3882(递推)
- uva live 4123 Glenbow Museum 数学递推
- 曼哈顿最小距离生成树(poj 3241&& UVALive 3662)
- uva 11426 线性欧拉函数筛选+递推
- POJ 3279 Fliptile(二进制+暴 力) UVALive 3953
- UVALIVE 2675 Peals (POJ 1260 HDU 1300)
- UVaLive ( LA ) 3516 - Exploring Pyramids (DP 递推)
- POJ 2407 Relatives && UVA 10299 Relatives(欧拉函数)
- Poj 3150/UVA 1386/UVALive 3704 Cellular Automaton 循环矩阵
- UVa Live Archive 3635 & POJ 3122 - Pie
- UVa Live Archive 3971 & UVa 12124 & POJ 3497 - Assemble
- uva 10820 poj 2407 poj 1284 poj 2478 poj 3090 (欧拉函数入门)
- Human Gene Functions - POJ 1080 UVaLive 2344 dp
- poj 3809 UVaLive 4643 Twenty Questions
- UVA 1394 POJ 3517 And Then There Was One (双向循环链表和递推)
- 【转】UVALive 5964 LCM Extreme --欧拉函数
- poj 2478 Farey Sequence(递推求欧拉函数)
- UVALive 4727 Jump(约瑟夫环,递推)