卡特兰数

         卡特兰数又称卡塔兰数,英文名Catalan number,是组合数学中一个常出现在各种计数问题中出现的数列。由以比利时的数学家欧仁·查理·卡塔兰 (1814–1894)命名,其前几项为 : 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790, 477638700, 1767263190, 6564120420, 24466267020, 91482563640, 343059613650, 1289904147324, 4861946401452, ...

原理:

 

  
令h(0)=1,h(1)=1,catalan数满足递推式[1] :
h(n)= h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)h(0) (n>=2)
例如:h(2)=h(0)*h(1)+h(1)*h(0)=1*1+1*1=2
h(3)=h(0)*h(2)+h(1)*h(1)+h(2)*h(0)=1*2+1*1+2*1=5
另类递推式[2] :
h(n)=h(n-1)*(4*n-2)/(n+1);
递推关系的解为:
h(n)=C(2n,n)/(n+1) (n=0,1,2,...)
递推关系的另类解为:
h(n)=c(2n,n)-c(2n,n+1)(n=0,1,2,...)
 
 
应用:
 
 
代码:
 1 #include <algorithm>
 2 #include <iostream>
 3 #include <sstream>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <cstdio>
 7 #include <string>
 8 #include <bitset>
 9 #include <vector>
10 #include <queue>
11 #include <stack>
12 #include <cmath>
13 #include <list>
14 #include <set>
15 using namespace std;
16 /***************************************/
17 #define ll long long
18 #define int64 __int64
19 /***************************************/
20 const int INF = 0x7f7f7f7f;
21 const ll LINF = (1LL<<60);
22 const double eps = 1e-8;
23 const double PIE=acos(-1.0);
24 const int d1x[]= {0,-1,0,1};
25 const int d1y[]= {-1,0,1,0};
26 const int d2x[]= {0,-1,0,1};
27 const int d2y[]= {1,0,-1,0};
28 const int fx[]= {-1,-1,-1,0,0,1,1,1};
29 const int fy[]= {-1,0,1,-1,1,-1,0,1};
30 inline int min_32(int (a),int (b))
31 {
32     return (a)<(b)?(a):(b);
33 }
34 inline int max_32(int (a),int (b))
35 {
36     return (a)>(b)?(a):(b);
37 }
38 inline long long min_64(long long (a),long long (b))
39 {
40     return (a)<(b)?(a):(b);
41 }
42 inline long long max_64(long long (a),long long (b))
43 {
44     return (a)>(b)?(a):(b);
45 }
46 /***************************************/
47 void openfile()
48 {
49     freopen("data.in","rb",stdin);
50     freopen("data.out","wb",stdout);
51 }
52 /**********************华丽丽的分割线,以上为模板部分*****************/
53 
54 
55 //卡特兰数  其前几项为 : 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440,
56 //  9694845, 35357670, 129644790, 477638700, 1767263190
57 
58 
59 int a[22];
60 
61 int main()
62 {
63 
64     int n;
65     a[0]=1;
66     int i,j;
67     scanf("%d",&n);
68     for(i=1;i<=n;i++)
69     {
70         a[i]=(a[i-1]*(4*i-2))/(i+1); //公式
71     }
72     printf("%d
",a[n]);
73     return 0;
74 }
View Code
原文地址:https://www.cnblogs.com/ZhaoPengkinghold/p/4415432.html