Catalan Numbers 卡特兰数
卡特蘭數源于組合數學,遞推式為?H【1】 = 1;H【n】 = H【n-1】*(4*n-2)/(n+1){n>=2};
卡塔蘭數的漸近增長為
下面給出幾個求卡特蘭數的公式,用h(n)表示卡特蘭數的第n項,其中??h(0)=1,h(1)=1
公式一:h(n)= h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)*h(0) (n>=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, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790, 477638700, 1767263190, 6564120420, 24466267020, 91482563640, 343059613650, 1289904147324, 4861946401452, ...主要應用:
1.括號化問題
矩陣鏈乘:,根據乘法結合律,不改變順序,只用括號表示對的乘積,有多少種括號化的方案?h(n)種
2.出棧次序問題:
一個棧(無窮大)的進棧次序為1,2,3,4,5,6.........n,有多少個出棧次序?
HDU1023
類似:
(1)有2n個人排成一行進入劇場,入場費5元,其中只有n個人有一張5元,另外n個人只有10元,劇院無其他鈔票,問有多少種方法使得只有10元的人買票(售票處只有5元找零)?
將持5元者到達視作將5元入棧,持10元者到達視作棧中某5元出棧
HDU4165
(2)在圓上選擇2n個點,將這些點成對連接起來,使得所得到的n條線段不相交的方法數?h(n)種
POJ2084
3.將多邊形劃分為三角形問題,將一個凸多邊形區域分成三角形區域的方法數?
類似:
(1)一位律師在他住所以北n個街區和以東n個街區處工作,每天他走2n個街區上班,如果不穿越(但可以碰到)從家到辦公室的對角線,那么有多少條可能的道路?ans=2*h(n)
HDU2067
4.給n個節點,能構成多少種形狀不同的二叉樹?
先去掉一個點作為頂點,左邊依次可以取0至N-1個,右邊是N-1到0個,兩兩配對相乘,就是
h(0)*h(n-1)+h(2)*h(n-2)+......+h(n-1)*h(0)=h(n)
import java.util.*; import java.math.*; public class Main {public static void main(String[] args) {BigInteger[] arr = new BigInteger[107];arr[1]=BigInteger.ONE;for(int i=2;i<=100;i++) {arr[i] = arr[i-1].multiply(BigInteger.valueOf(4*i-2)).divide(BigInteger.valueOf(i+1));}Scanner cin=new Scanner(System.in);while(cin.hasNext()) {int n=cin.nextInt();if(n==-1)break;System.out.println(arr[n]);}cin.close();} }HDU3723
A delta wave is a high amplitude brain wave in humans with a frequency of 1 – 4 hertz which can be recorded with an electroencephalogram (EEG) and is usually associated with slow-wave sleep (SWS).?
-- from Wikipedia?
The researchers have discovered a new kind of species called "otaku", whose brain waves are rather strange. The delta wave of an otaku's brain can be approximated by a polygonal line in the 2D coordinate system. The line is a route from point (0, 0) to (N, 0), and it is allowed to move only to the right (up, down or straight) at every step. And during the whole moving, it is not allowed to dip below the y = 0 axis.?
For example, there are the 9 kinds of delta waves for N = 4:?
Given N, you are requested to find out how many kinds of different delta waves of otaku.
Input
There are no more than 20 test cases. There is only one line for each case, containing an integer N (2 < N <= 10000)?
Output
Output one line for each test case. For the answer may be quite huge, you need only output the answer module 10?100.
Sample Input
3 4Sample Output
4 9題意:
在第一象限,從(0,0)到(n,0)點,每次y值的差值最大為1,要么加1(向上走),要么減1(向下走),要么不變(水平),問有多少種走法?
分析:
起點和終點在一條直線上,所以向上 i 次,就要向下 i 次,且(0<=i<n/2,?i+i <=n),而上下的次序不固定,顯然就是 i 個元素進棧出棧的次序,方案數為??? (公式三);
從n個元素中選出2*i個向上走或者向下走,n-2*i 個元素水平走,方案數為??;
向上0次,向下0次,水平直線走,方案數為??;
向上 i 次,向下 i 次,方案數??
向上i+1次,向下i+1次,方案數?
遞推式:
?
import java.math.*; import java.util.*;public class Main{public static void main(String[] args) {Scanner cin=new Scanner(System.in);while(cin.hasNext()) {BigInteger ans=BigInteger.ONE;BigInteger tmp=BigInteger.ONE;int n=cin.nextInt();for(int i=0;i<=n/2;i++) {tmp=tmp.multiply(BigInteger.valueOf(n-2*i)).multiply(BigInteger.valueOf(n-2*i-1)).divide(BigInteger.valueOf(i+2)).divide(BigInteger.valueOf(i+1));ans=ans.add(tmp);}ans=ans.mod(BigInteger.TEN.pow(100));//mod 10^100System.out.println(ans);}cin.close();} }?
總結
以上是生活随笔為你收集整理的Catalan Numbers 卡特兰数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU4135 HDU2841 HDU1
- 下一篇: HDU2049 组合数学 错排公式