您的位置:首页 > 其它

HDU1134——Game of Connections (大数乘法,除法)+ (卡特兰数列)

2015-04-15 21:14 549 查看

Game of Connections

Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 14 Accepted Submission(s) : 10
[align=left]Problem Description[/align]
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?

[align=left]Input[/align]
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.

[align=left]Output[/align]
For each n, print in a single line the number of ways to connect the 2n numbers into pairs.

[align=left]Sample Input[/align]

2
3
-1


[align=left]Sample Output[/align]

2
5


[align=left]Source[/align]
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
[t])  t--;
for(;t>=0;t--)
{
printf("%d",map
[t]);
}
printf("\n");
}
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: