JOJ2026:Divide the Birthday Cake
2011-11-16 10:50
791 查看
On L.X.'s birthday, I bought her a big Birthday Cake, and she felt very happy.
"So how do you divide the cake?" I asked.
"I do not know. I do not want to simply divide it into several sectors. Hmn, Let me see. The cake is a little strange. Its shape looks like a polygon. En, I want to divide it into small pieces of triangles. I mean, if there are N sides, there should be N-2
small triangles. We can use N-3 diagonal(s) which do not intersect inside of the polygon to divide the cake into N-2 triangles." She said.
"Yeah, of course we can. It is an easy job." I took out a pencil and drew a dividing method on the paper.
"Yes. But I want to know how many different methods to divide the cake?" She smiled.
"Ah, it is not difficult. I will tell you the answer later!" said.
"Ok, but if you do not get the answer at last, you cannot eat the cake!"
"Ok…"
Then I began to calculate the number and she began to cut the cake. But I didn't get the answer, for there were 16 vertexes. Oh, I was hungry! You must help me!!!
Here are the examples of "VALID" and "INVALID" dividing method.
题目大意:多边形分割成三角形的方案数。
思路:典型的卡特兰数,按照卡特兰数公式求解一下就行了。卡特兰数详情见这里。
代码:
"So how do you divide the cake?" I asked.
"I do not know. I do not want to simply divide it into several sectors. Hmn, Let me see. The cake is a little strange. Its shape looks like a polygon. En, I want to divide it into small pieces of triangles. I mean, if there are N sides, there should be N-2
small triangles. We can use N-3 diagonal(s) which do not intersect inside of the polygon to divide the cake into N-2 triangles." She said.
"Yeah, of course we can. It is an easy job." I took out a pencil and drew a dividing method on the paper.
"Yes. But I want to know how many different methods to divide the cake?" She smiled.
"Ah, it is not difficult. I will tell you the answer later!" said.
"Ok, but if you do not get the answer at last, you cannot eat the cake!"
"Ok…"
Then I began to calculate the number and she began to cut the cake. But I didn't get the answer, for there were 16 vertexes. Oh, I was hungry! You must help me!!!
Here are the examples of "VALID" and "INVALID" dividing method.
Input
There are several lines in the input file, each line contains a number N stands for the number the vertex of the polygon. 5<=N<=31. Your program should proceed to the end of the file.Output
Print the number of dividing method one per line.Sample Input
5 6
Sample Output
5 14
题目大意:多边形分割成三角形的方案数。
思路:典型的卡特兰数,按照卡特兰数公式求解一下就行了。卡特兰数详情见这里。
代码:
#include <cstdio> using namespace std; int main() { int num; while (scanf("%d", &num) != EOF) { int n, k; double res = 1.0; num--; n = 2 * num - 2; k = num - 1; for (; k >= 1; --k) { res *= 1.0 * n / k; n--; } res /= 1.0 * num; printf("%.lf\n", res); } }
相关文章推荐
- JOJ 2412 Divide the finger
- [概率]Cut the Cake
- codeforces #334 D. Babaei and Birthday Cake (线段树或者dp+二分)
- HDU5783 - Divide the Sequence(2016 Multi-University Training Contest 5 - C)
- 杭电2134-Cuts the cake
- Hust oj 1197 Cuts the cake(水题)
- UVA 10256 The Great Divide
- Divide the Sequence
- hackerrank初级篇之Birthday Cake Candles
- 2016多校5 hdu 5783 Divide the Sequence 贪心
- Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake
- ZS and The Birthday Paradox
- UVa 10256 - The Great Divide 判断凸包相交
- How to Capture the Integer-Divide-By-Zero Error in C++(提前定义信号)
- HDU 5783 Divide the Sequence 【贪心】
- HDOJ 5783 (2016多校联合训练 Training Contest 5) Divide the Sequence
- HDU-2134 Cuts the cake 水题 数学题
- NOJ [1306] Divide The Apples
- JOJ 1026 The staircases
- Codeforces 629D.( Babaei and Birthday Cake)