hdu 2067 小兔的棋盘(卡特兰数)

来源:互联网 发布:知乎miya的米牙 编辑:程序博客网 时间:2024/05/12 17:30

小兔的棋盘

小兔的叔叔从外面旅游回来给她带来了一个礼物,小兔有所失望。不过没过几天发现了棋盘的好玩之处。从起点(0,0)走到终点(n,n)的最短路径数是C(2n,n),现在小兔又想如果不穿越对角线(但可接触对角线上的格点),这样的路径数有多少?小兔想了很长时间都没想出来,现在想请你帮助小兔解决这个问题,对于你来说应该不难吧!
Input
每次输入一个数n(1<=n<=35),当n等于-1时结束输入。
Output
对于每个输入数据输出路径数,具体格式看Sample。
Sample Input

1312-1

Sample Output

1 1 22 3 103 12 416024


#include <iostream>#include <cstring>#include <cstdio>using namespace std;#define N 35#define ll long longll c[N];void Catalan(){    c[0] = c[1] = 1;    for(int i = 2 ; i <= N ; i ++ ){        c[i] = 0;        for(int j = 0 ; j < i ; j ++ ){            c[i] += c[j] * c[i-j-1];            ///h(n)= h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)*h(0) (n>=2)        }//cout<<c[i]<<endl;    }}int main(){    freopen( "in.txt", "r", stdin );    Catalan();    int n, cas = 1;    while( scanf( "%d", &n ) != EOF ){        if( n == -1 ) break;        cout<<cas++<<" "<<n<<" "<<2*c[n]<<endl;    }    return 0;}