Poj 2084
Game ofConnections
TimeLimit1000ms
Description
This is asmall 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 numberpairs.
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 2nnumbers, can you tell me in how many different ways can you connect the numbersinto pairs? Life is harder, right?
Input
Each lineof 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 eachn, print in a single line the number of ways to connect the 2n numbers intopairs.
Sample Input
2
3
-1
Sample Output
2
5
卡塔兰数
问题分析:好吧,纯粹的卡特兰数,加大数乘法,并不是很懂……
#include<stdio.h>
#include<string.h>
int ans[102][100];
void list()
{
ans[0][0] = ans[1][0] = 1;
int i, j;
for(i = 2; i < 102; i ++)
{
int c = 0;
for(j = 0; j < 100; j ++)
{
ans[i][j] = ans[i-1][j]*(4*i-2)+c;
c = ans[i][j]/10;
ans[i][j] %= 10;
}
int z = 0;
for(j = 99; j >= 0; j --)
{
z= z*10+ans[i][j];
ans[i][j] = z/(i+1);
z %= (i+1);
}
}
}
int main(){
int t;
list();
while(~scanf("%d", &t))
{
int i = 99;
if (t == -1)
break;
while(ans[t][i] == 0)
i --;
for(;i >= 0; i--)
printf("%d", ans[t][i]);
printf("\n");
}
return 0;
}
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
int a[104][200];
void init();
int strlen_new(int r[]);
int main(){
int n;
init();
scanf("%d",&n);
while(n!=-1){
for(int i = strlen_new(a[n]);i>=0;i--){
printf("%d",a[n][i]);
}
printf("\n");
scanf("%d",&n);
}
return 0;
}
int strlen_new(int r[]){
for(int i=150;i>=0;i--){
if(r[i]){
return i;
}
}
}
void strcpy_new(int a[],int b[]){
for(int i=0;i<=150;i++){
a[i] = b[i];
}
}
void init(){
memset(a,0,sizeof(a));
a[0][0] = 1;
a[1][0] = 1;
for(int i=2;i<=101;i++){
int temp = (2*(2*(i-1)+1));
int e = strlen_new(a[i-1]);
for(int j=0;j<=e;j++){ //mul
a[i][j] += a[i-1][j]*temp;
int t = a[i][j];
int k=j+1;
t/=10;
while(t){
a[i][k] += t%10;
k++;
t/=10;
}
a[i][j]= a[i][j]%10;
}
int c[200]; //chu
memset(c,0,sizeof(c));
int t = i+1;
int chu = 0;
int ee = strlen_new(a[i]);
for(int k=ee;k>=0;k--){
chu= a[i][k]+chu*10;
if(chu/t>0){
c[k] = chu/t;
chu%=t;
}
}
strcpy_new(a[i],c);
}
}
本文介绍了一个经典的组合数学问题——连接游戏,通过计算卡塔兰数来确定连接方式的数量。文章提供了两种实现方法,包括大数乘法处理,以解决超出基本整数类型范围的问题。
313

被折叠的 条评论
为什么被折叠?



