Timus 1531. Zones on a plane
2008-07-06 19:07
375 查看
Timus 1531. Zones on a plane 要求计算满足给定条件的简单多边形的个数。
Memory Limit: 64 MB
Consider zones zi on a plane which consist of triangles. Zone z1
consists of two right-angled isosceles triangles, forming a square. Zone
zn + 1 is produced from zone zn in the following way. For each
triangle from the previous zone, construct two isosceles right-angled
triangles on each of its two legs as a hypotenuse. Then, remove every
triangle that is a part of a zone with lower number. The remaining triangles
constitute the zone zn + 1.
Given an integer number n, find how many simple polygons constitute
the zone zn.
of the input.
consists of.
Problem Author: Dmitry Gozman
Problem Source: Dmitry Gozman Contest 1, Petrozavodsk training camp, January 2007
解答如下:
1 using System;
2
3 namespace Skyiv.Ben.Timus
4 {
5 // http://acm.timus.ru/problem.aspx?space=1&num=1531
6 sealed class T1531
7 {
8 static void Main()
9 {
Console.WriteLine(Zones(int.Parse(Console.ReadLine())));
}
static BigInteger Zones(int n)
{
if (n == 1) return 1;
if (n == 2) return 4;
BigInteger z = 4, c = 2;
for (int i = 3; i <= n; i++, z += c) if (i % 2 != 0) c *= 2;
return z;
}
}
}
注意,这个程序使用了 BigInteger 类,请到我的另一篇随笔 Timus 1013. K-based numbers. Version 3 中查看其源代码。
这道题目是说,在平原上有若干三角形组成的区域 Zi。Z1 包含两个等腰直角三角形,组成一个正方形。Zn+1 由 Zn 按以下方法生成:以 Zn 中的三角形的直角边作为新的等腰直角三角形的斜边,然后再移去 Zn 中的三角形,剩下的三角形就组成了 Zn+1。现在要求计算 Zn 中包含多少个简单多边形。
为了找出其中的规律,我们继续画图,一直画到 n = 8,如右图所示。然后在右图中数出 Zn 来,如下:
1, 4, 8, 12, 20, 28, 44, 60
我们发现,该数列后项减前项为:
3, 4, 4, 8, 8, 16, 16
除了第一个数 3 以外,其余各数依次为:
22, 22, 23, 23, 24, 24
这样,我们就得到以下递推公式 ( [x] 表示对 x 进行下取整 ):
Z1 = 1, Z2 = 4, Zn = Zn-1 + 2[(n+1)/2] (n > 2)
有了递推公式,写出相应的程序就非常容易了。由于 Z2000 ≈ 4.286 x 10301,所以程序中使用 BigInteger 类进行计算。
1531. Zones on a plane
Time Limit: 1.0 secondMemory Limit: 64 MB
Consider zones zi on a plane which consist of triangles. Zone z1
consists of two right-angled isosceles triangles, forming a square. Zone
zn + 1 is produced from zone zn in the following way. For each
triangle from the previous zone, construct two isosceles right-angled
triangles on each of its two legs as a hypotenuse. Then, remove every
triangle that is a part of a zone with lower number. The remaining triangles
constitute the zone zn + 1.
Given an integer number n, find how many simple polygons constitute
the zone zn.
Input
There is a single integer n (1 ≤ n ≤ 2000) on the first lineof the input.
Output
Output a single number — the number of simple polygons zone znconsists of.
Samples
input | output |
---|---|
1 | 1 |
2 | 4 |
3 | 8 |
4 | 12 |
Problem Source: Dmitry Gozman Contest 1, Petrozavodsk training camp, January 2007
解答如下:
1 using System;
2
3 namespace Skyiv.Ben.Timus
4 {
5 // http://acm.timus.ru/problem.aspx?space=1&num=1531
6 sealed class T1531
7 {
8 static void Main()
9 {
Console.WriteLine(Zones(int.Parse(Console.ReadLine())));
}
static BigInteger Zones(int n)
{
if (n == 1) return 1;
if (n == 2) return 4;
BigInteger z = 4, c = 2;
for (int i = 3; i <= n; i++, z += c) if (i % 2 != 0) c *= 2;
return z;
}
}
}
注意,这个程序使用了 BigInteger 类,请到我的另一篇随笔 Timus 1013. K-based numbers. Version 3 中查看其源代码。
这道题目是说,在平原上有若干三角形组成的区域 Zi。Z1 包含两个等腰直角三角形,组成一个正方形。Zn+1 由 Zn 按以下方法生成:以 Zn 中的三角形的直角边作为新的等腰直角三角形的斜边,然后再移去 Zn 中的三角形,剩下的三角形就组成了 Zn+1。现在要求计算 Zn 中包含多少个简单多边形。
为了找出其中的规律,我们继续画图,一直画到 n = 8,如右图所示。然后在右图中数出 Zn 来,如下:
1, 4, 8, 12, 20, 28, 44, 60
我们发现,该数列后项减前项为:
3, 4, 4, 8, 8, 16, 16
除了第一个数 3 以外,其余各数依次为:
22, 22, 23, 23, 24, 24
这样,我们就得到以下递推公式 ( [x] 表示对 x 进行下取整 ):
Z1 = 1, Z2 = 4, Zn = Zn-1 + 2[(n+1)/2] (n > 2)
有了递推公式,写出相应的程序就非常容易了。由于 Z2000 ≈ 4.286 x 10301,所以程序中使用 BigInteger 类进行计算。
相关文章推荐
- Leaving on a jet plane
- Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
- codeforces #319 div 2 E C. Points on Plane (分块)
- [LeetCode]Maximum number of points on a straight line in 2d plane
- Codeforces Round #319 (Div. 2)E Points on Plane
- Codefoces 277 E. Binary Tree on Plane
- 题目描述 Given n points on a 2D plane, find the maximum number of points that lie on the same straight
- ural 1207. Median on the Plane
- Ural_1207. Median on the Plane(计算几何)
- Codeforces Round #170 (Div. 1) E.Binary Tree on Plane
- 【April Fools Day Contest 2014G】【愚人节脑洞】On a plane 输出纵坐标平均值
- Codeforces Round #319 (Div. 1) C. Points on Plane
- ural 1207. Median on the Plane(极角排序)
- 【CodeForces】576 C. Points on Plane
- Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
- Codeforces Round #319 (Div. 2) E. Points on Plane(分块+排序)
- matrix67 on a plane
- Leetcode--->Given n points on a 2D plane, find the maximum number of points that lie on the same str
- Nearest Points on a Plane
- POJ 3316 Snakes on a Plane 可能会