[ACM] hdu 2067 小兔的棋盤(卡特蘭數Catalan)
阿新 • • 發佈:2019-02-19
小兔的棋盤
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5814 Accepted Submission(s): 3186
解題思路:
卡特蘭數:
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]
參考資料:
程式碼:
#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; }