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.
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);
}
}