poj2084Catalan数

Game of Connections
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 8859   Accepted: 4365

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?

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

 

C1=1,C2=2,C3=5,C4=14,C5=42,

C6=132,C7=429,C8=1430,C9=4862,C10=16796,

C11=58786,C12=208012,C13=742900,C14=2674440,C15=9694845

Catalan数的组合公式为 Cn=C(2n,n) / (n+1);

此数的递归公式为 h(n ) = h(n-1)*(4*n-2) / (n+1)

对于大数来说,使用万进制
https://www.cnblogs.com/jackge/archive/2013/05/19/3086519.html
 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 #include <string.h>
 5 #include <cstring>
 6 #include <math.h>
 7 #include <stdio.h>
 8 #include <cstdio>
 9 
10 #define mem(a) memset(a,0,sizeof(a))
11 #define maxn 100
12 #define BASE 10000
13 #define ll long long
14 
15 using namespace std;
16 int a[105][maxn];
17 void multiply(int a[],int Max,int b){  //模拟大数乘法
18     int i,array=0;
19     for(i=Max-1;i>=0;i--){
20         array+=b*a[i]; 
21         a[i]=array%BASE;  //取四位
22         array/=BASE;  //进位
23     }
24 }
25 void divide (int a[],int Max,int b){
26     int i,div=0;
27     for(i=0;i<Max;i++){
28         div=div*BASE+a[i];
29         a[i]=div/b;
30         div%=b;
31     }
32 }
33 int main(){
34    int i,n;
35     memset(a[1],0,sizeof(a[1]));
36     for(i=2;a[1][maxn-1]=1,i<101;i++){ //高坐标存放大数低位
37         memcpy(a[i],a[i-1],sizeof(a[1])); //h(n)=h(n-1)
38         multiply(a[i],maxn,4*i-2);  //h(n)*4*i-2
39         divide(a[i],maxn,i+1);  //(h(n)*4*i-2)/(n+1)
40     }
41     while(cin>>n,n!=-1){
42         for(i=0;i<maxn&&a[n][i]==0;i++);//去掉数组前为0的数字。
43         cout<<a[n][i++];  //输出第一个非0数
44         for(;i<maxn;i++){
45             printf("%04d",a[n][i]);  //输出后面的数,并每位都保持4位长度
46         }
47         cout<<endl;
48     }
49     return 0;
50 }

 

 
 
 
posted @ 2018-03-09 14:29  浅忆~  阅读(192)  评论(0编辑  收藏  举报