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?
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.
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数 关于这个数 我也是第一次接触 先看这道题
卡塔兰数的一般项公式为 



八个点的时候有这些可以连的方法 点围成的不是矩形二十圆形 题目要求范围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的情况:


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的情况:

最后更新:2017-04-02 15:14:57