POJ2084 catalan数

Game of Connections
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 6323 Accepted: 3258

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

Source



题解:这道题运用组合数学中的catalan数 关于这个数 我也是第一次接触 先看这道题
卡塔兰数的一般项公式为 C_n = \frac{1}{n+1}{2n \choose n} = \frac{(2n)!}{(n+1)!n!}
八个点的时候有这些可以连的方法 点围成的不是矩形二十圆形 题目要求范围100 显然第100项超过了范围
所以要用高精度 
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;

#define MAX 100
#define BASE 10000

void multiply(int a[],int Max,int b)
{
    int array=0;
    for(int i=Max-1; i>=0; i--)
    {
        array+=b*a[i];
        a[i]=array%BASE;
        array/=BASE;
    }
}
void divide(int a[],int Max,int b)
{
    int div=0;
    for(int i=0; i<Max; i++)
    {
        div=div*BASE+a[i];
        a[i]=div/b;
        div%=b;
    }
}
int main()
{
    int n,i,a[101][MAX];
    memset(a[1],0,MAX*sizeof(int));
    for(i=2,a[1][MAX-1]=1; i<101; i++)
    {
        memcpy(a[i],a[i-1],MAX*sizeof(int));
        multiply(a[i],MAX,4*i-2);
        divide(a[i],MAX,i+1);
    }
    while(cin>>n)
    {
        if(n==-1)
            break;
        for(i=0; i<MAX&&a[n][i]==0; i++);
        cout<<a[n][i++];
        for(; i<MAX; i++)
            printf("%04d",a[n][i]);
        cout<<endl;
    }
    return 0;
}

再来看看catalan数的应用 我在维基 百度的百科和博客上看到的总结一下
 
Cn表示所有在n × n格点中不越过对角线的单调路径的个数。一个单调路径从格点左下角出发,在格点右上角结束,每一步均为向上或向右。计算这种路径的个数等价于计算Dyck word的个数: X代表“向右”,Y代表“向上”。下图为n = 4的情况:
Catalan number 4x4 grid example.svg
Cn表示通过连结顶点而将n + 2边的 凸多边形 分成 三角形 的方法个数。下图中为 n  = 4的情况:
Catalan-Hexagons-example.svg
Cn表示对{1, ..., n}依序进出 置换 个数。一个置换 w 是依序进出栈的当 S ( w ) = (1, ...,  n ), 其中 S ( w )递归定义如下:令 w  =  unv ,其中 n w 的最大元素, u v 为更短的数列;再令 S ( w ) =  S ( u ) S ( v ) n ,其中 S 为所有含一个元素的数列的单位元。
Cn表示用n个长方形填充一个高度为n的阶梯状图形的方法个数。下图为 n = 4的情况:
Catalan stairsteps 4.svg
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值