Catalan数

Catalan数又称为卡塔兰数,是组合数学中常出现的一种数列,由数学家欧仁·查理·卡塔兰命名。

令h1=1,h2=1.

hn=h1*hn-1+h2*hn-2+.......+hn-1h1(n>=3).

另类递推式:

hn=hn-1*(4*(n-1)-2)/((n-1)+1).

由该式又可以得到:

hn+1=C(2*n,n)/(n+1) (n=1,2,3.....)其中C(2*n,n)表示2*n个物品中选出n个物品的组合数。

hn+1=C(2*n,n)-C(2*n,n+1) (n=1,2,3.....)。

常见的应用Catalan数的例子:

1.出栈序列,一个栈(无穷大)的进栈序列为:1,2,3......,n,那么有hn+1个不同的出栈序列。

证明:设f(n)表示大小为n的栈的出栈序列的个数,f(0)=f(1)=1。

设第一个出栈的序列是k,那么现在栈北分为[1,k-1]和[k+1,n],由乘法定理可以得到f(n)=f((k-1)-1+1)*f(n-(k+1)+1)。

又因为k的取值是在[1,n],那么f(n)=f(0)*f(n-1)+f(1)*f(n-2)+....+f(n-1)*f(0)。

对照Catalan数得到f(n)=hn+1

2.凸多边形的三角划分

将凸多边形通过若干条互不相交的线段划分成多个三角形,问有多少种方案?

设f(n)表示凸n边形的方案,那么有f(n)=hn-1

3.给定节点组成二叉树

例题:POJ 2084

Game of Connections
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 9559   Accepted: 4680

Description

This is a small but ancient game. You are supposed to write down the numbers 1, 2, 3, . . . , 2n - 1, 2n consecutively in clockwise order on the ground to form a circle, and then, to draw some straight line segments to connect them into number pairs. Every number must be connected to exactly one another.
And, no two segments are allowed to intersect.
It's still a simple game, isn't it? But after you've written down the 2n numbers, can you tell me in how many different ways can you connect the numbers into pairs? Life is harder, right?

Input

Each line of the input file will be a single positive number n, except the last line, which is a number -1.
You may assume that 1 <= n <= 100.

Output

For each n, print in a single line the number of ways to connect the 2n numbers into pairs.

Sample Input

2
3
-1

Sample Output

2
5

AC code:

#include<cstdio>
using namespace std;
struct BIGNUM{
    short s[200],l;
}c[120];
BIGNUM operator*(BIGNUM a,int b)
{
    for(int i=0;i<a.l;i++)    a.s[i]*=b;
    for(int i=0;i<a.l;i++)
    {
        a.s[i+1]+=a.s[i]/10;
        a.s[i]%=10;
    }
    while(a.s[a.l]!=0)
    {
        a.s[a.l+1]+=a.s[a.l]/10;
        a.s[a.l]%=10;
        a.l++;
    }
    return a;
}
BIGNUM operator/(BIGNUM a,int b)
{
    for(int i=a.l-1;i>0;i--)
    {
        a.s[i-1]+=(a.s[i]%b)*10;
        a.s[i]/=b;
    }
    a.s[0]/=b;
    while(a.s[a.l-1]==0)    a.l--;
    return a;
}
void print(BIGNUM a)
{
    for(int i=a.l-1;i>=0;i--)
    {
        printf("%d",a.s[i]);
    }
}
int main()
{
    freopen("input.txt","r",stdin);
    int n;
    c[0].l=1;
    c[0].s[0]=1;
    for(int i=0;i<=101;i++)
    {
        c[i+1]=c[i]*(4*i+2)/(i+2);
    }
    while(~scanf("%d",&n)&&n!=-1)
    {
        print(c[n]);
        printf("\n");
    }
    return 0;
}
View Code

 

posted on 2019-08-26 11:07  Caution_X  阅读(200)  评论(0编辑  收藏  举报

导航