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