卡特兰数的运用
( C(m+n, n) - C(m+n, m+1) ) * m! * n! 化简即 (m+n)! * (m-n+1) / (m+1)
#include<iostream> #include<cstdio> #include<cstring> using namespace std; long long s[205][5002]; long long tem[205][5002]; long long temp[205][5002]; void f() { int i , j; memset(s,0,sizeof(s)); s[1][0]=1;s[2][0]=2;s[3][0]=6; for(i=4;i<205;i++) { for(j=0;j<5002;j++) { s[i][j]=s[i-1][j]*i; } for(j=0;j<5002;j++) { if(s[i][j]>=10) { s[i][j+1]+=(s[i][j]/10); s[i][j]%=10; } } } } int main() { f(); int i, j, d, t1, t2, count = 1; int m,n; while(cin>>m>>n) { if(m==0 && n==0) break; if(n>m) { cout<<"Test #"<<count<<":"<<endl; cout<<0<<endl; count++; continue; } t1 = (m-n+1); t2 = m+1; memset(tem,0,sizeof(tem)); for(i=0;i<5002;i++) { tem[m+n][i] += s[m+n][i]*t1; tem[m+n][i+1] += tem[m+n][i]/10; tem[m+n][i] %= 10; } memset(temp,0,sizeof(temp)); d = 0; for(i=5001;i>=0;i--) { d=d*10+tem[m+n][i]; temp[m+n][i]=d/t2; d%=t2; } cout<<"Test #"<<count<<":"<<endl; count++; i = 5001; while(i--) { if(temp[m+n][i]!=0) break; } for(;i>=0;i--) { cout<<temp[m+n][i]; } cout<<endl; } return 0; }