标签:
Catalan数
中文:卡特兰数
原理:
令h(1)=1,h(0)=1,catalan数满足递归式:
h(n)= h(1)*h(n-1) + h(2)*h(n-2) + ... + h(n-1)h(1) (其中n>=2)
另类递归式:
h(n)=((4*n-2)/(n+1))*h(n-1);
该递推关系的解为:
h(n+1)=C(2n,n)/(n+1) (n=1,2,3,...)
Catalan数的组合公式为 Cn=C(2n,n) / (n+1);
没什么好说的 作为模板就行了
1 #include <iostream> 2 #include <algorithm> 3 #include <queue> 4 #include <cstring> 5 #include <cstdio> 6 #include <vector> 7 #include <string> 8 #include <iterator> 9 #include <cmath> 10 #include <deque> 11 #include <stack> 12 #include <cctype> 13 using namespace std; 14 15 const int N = 150; 16 const int INF = 0xfffffff; 17 18 typedef long long ll; 19 typedef long double ld; 20 21 #define INFL 0x7fffffffffffffffLL 22 #define met(a, b) memset(a, b, sizeof(a)) 23 #define rep(c, a, b) for (int c = a; c < b; c++) 24 #define nre(c, a, b) for (int c = a; c > b; c--) 25 26 //h(n) = ( (4*n-2) / (n+1) ) * h(n-1); 27 28 int ans[N][N]; 29 30 void init (); 31 32 int main () 33 { 34 int n; 35 init(); 36 while (cin >> n) 37 { 38 int len = N - 1; 39 while (ans[n][len] == 0) len--; 40 while (len>=0) cout << ans[n][len--]; 41 cout << endl; 42 } 43 return 0; 44 } 45 46 void init () 47 { 48 ans[1][0] = 1; 49 for (int i=2; i<101; i++) 50 { 51 int t = 0; 52 for (int j=0; j<N; j++) 53 { 54 int x = ans[i-1][j] * (4 * i - 2) + t; 55 ans[i][j] = x % 10, t = x / 10; 56 } 57 t = 0; 58 for (int j=N-1; j>=0; j--) 59 { 60 int x = ans[i][j] + t * 10; 61 ans[i][j] = x / (i + 1), t = x % (i + 1); 62 } 63 } 64 }
标签:
原文地址:http://www.cnblogs.com/darkdomain/p/4381598.html