Game of Connections
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 14 Accepted Submission(s) : 10
Problem Description
This is a small but ancient game. You are supposed to write down the numbers 1, 2, 3, ... , 2n - 1, 2n consecutively in clockwise order on the ground to form a circle, and then, to draw some straight line segments to connect them
into number pairs. Every number must be connected to exactly one another. And, no two segments are allowed to intersect.
It's still a simple game, isn't it? But after you've written down the 2n numbers, can you tell me in how many different ways can you connect the numbers into pairs? Life is harder, right?
It's still a simple game, isn't it? But after you've written down the 2n numbers, can you tell me in how many different ways can you connect the numbers into pairs? Life is harder, right?
Input
Each line of the input file will be a single positive number n, except the last line, which is a number -1. You may assume that 1 <= n <= 100.
Output
For each n, print in a single line the number of ways to connect the 2n numbers into pairs.
Sample Input
2 3 -1
Sample Output
2 5
Source
Asia 2004, Shanghai (Mainland China), Preliminary
对于这道题,半天推导不出来,什么东西。唯有百度解决问题;发现了卡特兰数列,
竟然是大数模拟乘法,及除法。看了看书,又将大数乘法,除法搞定。。。
#include<stdio.h>
#include<string.h>
int map[110][110];
int main()
{
int i,j;
int len=1;
int temp=0;
int r=0;
int n;
map[1][0]=1;
//从第二项开始模拟乘法
for(i=2;i<=100;i++)
{
for(j=0;j<len;j++)
{
map[i][j]=map[i-1][j]*(4*i-2);//从低位依次相乘
}
//对结果进行处理
r=0;
for(j=0;j<len;j++)
{
temp=map[i][j]+r;//是否超过10;
map[i][j]=temp%10;
r=temp/10;
}
//遇见高位超出限制的话
for(;r;)
{
map[i][len]=r%10;
r/=10;
len++;
}
//同时进行模拟相除
//len--;
r=0;
for(j=len-1;j>=0;j--)
{
temp=map[i][j]+r*10;//去高位进行相除
map[i][j]=temp/(i+1);
r=temp%(i+1); //有保留的话,下次调用
}
//别忘记去掉高位的0
while(!map[i][len-1]) len--;
}
while(scanf("%d",&n)&&n!=-1)
{
int t=100;
while(!map[n][t]) t--;
for(;t>=0;t--)
{
printf("%d",map[n][t]);
}
printf("\n");
}
return 0;
}