[ACM] hdu 2067 小兔的棋盘(卡特兰数Catalan)

小兔的棋盘

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5814    Accepted Submission(s): 3186


Problem Description
小兔的叔叔从外面旅游回来给她带来了一个礼物,小兔高兴地跑回自己的房间,拆开一看是一个棋盘,小兔有所失望。不过没过几天发现了棋盘的好玩之处。从起点(0,0)走到终点(n,n)的最短路径数是C(2n,n),现在小兔又想如果不穿越对角线(但可接触对角线上的格点),这样的路径数有多少?小兔想了很长时间都没想出来,现在想请你帮助小兔解决这个问题,对于你来说应该不难吧!
 


 

Input
每次输入一个数n(1<=n<=35),当n等于-1时结束输入。
 


 

Output
对于每个输入数据输出路径数,具体格式看Sample。
 


 

Sample Input
1 3 12 -1
 


 

Sample Output
1 1 2 2 3 10 3 12 416024
 


 

Author
Rabbit
 


 

Source

解题思路:

卡特兰数:

1 通项公式:h(n)=C(n,2n)/(n+1)=(2n)!/((n!)*(n+1)!)

2递推公式:h(n)=((4*n-2)/(n+1))*h(n-1); h(n)=h(0)*h(n-1)+h(1)*h(n-2)+...+h(n-1)*h(0).

3前几项为:h(0)=1,h(1)=1,h(2)=2,h(3)=5,h(4)=14,h(5)=42,......

代码采用第二个公式。本题结果为2*C[n]

参考资料:

http://blog.163.com/lz_666888/blog/static/1147857262009914112922803/

http://www.cnblogs.com/buptLizer/archive/2011/10/23/2222027.html

http://www.cppblog.com/MiYu/archive/2010/08/07/122573.html

代码:

#include <iostream>
#include <string.h>
using namespace std;

const int N=36;
long long c[N];

void Catalan()
{
    memset(c,0,sizeof(c));
    c[0]=c[1]=1;
    for(int i=2;i<=35;i++)
        for(int j=0;j<i;j++)
    {
        c[i]+=c[j]*c[i-j-1];
    }
}



int main()
{
    Catalan();
    int n;
    int t=1;
    while(cin>>n&&n!=-1)
    {
        cout<<t++<<" "<<n<<" "<<2*c[n]<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/sr1993/p/3697929.html