Catalan Numbers 卡特兰数
卡特蘭數(shù)源于組合數(shù)學(xué),遞推式為?H【1】 = 1;H【n】 = H【n-1】*(4*n-2)/(n+1){n>=2};
卡塔蘭數(shù)的漸近增長為
下面給出幾個求卡特蘭數(shù)的公式,用h(n)表示卡特蘭數(shù)的第n項(xiàng),其中??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,...)?
其前幾項(xiàng)為 :
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, ...主要應(yīng)用:
1.括號化問題
矩陣鏈乘:,根據(jù)乘法結(jié)合律,不改變順序,只用括號表示對的乘積,有多少種括號化的方案?h(n)種
2.出棧次序問題:
一個棧(無窮大)的進(jìn)棧次序?yàn)?,2,3,4,5,6.........n,有多少個出棧次序?
HDU1023
類似:
(1)有2n個人排成一行進(jìn)入劇場,入場費(fèi)5元,其中只有n個人有一張5元,另外n個人只有10元,劇院無其他鈔票,問有多少種方法使得只有10元的人買票(售票處只有5元找零)?
將持5元者到達(dá)視作將5元入棧,持10元者到達(dá)視作棧中某5元出棧
HDU4165
(2)在圓上選擇2n個點(diǎn),將這些點(diǎn)成對連接起來,使得所得到的n條線段不相交的方法數(shù)?h(n)種
POJ2084
3.將多邊形劃分為三角形問題,將一個凸多邊形區(qū)域分成三角形區(qū)域的方法數(shù)?
類似:
(1)一位律師在他住所以北n個街區(qū)和以東n個街區(qū)處工作,每天他走2n個街區(qū)上班,如果不穿越(但可以碰到)從家到辦公室的對角線,那么有多少條可能的道路?ans=2*h(n)
HDU2067
4.給n個節(jié)點(diǎn),能構(gòu)成多少種形狀不同的二叉樹?
先去掉一個點(diǎn)作為頂點(diǎ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)點(diǎn),每次y值的差值最大為1,要么加1(向上走),要么減1(向下走),要么不變(水平),問有多少種走法?
分析:
起點(diǎn)和終點(diǎn)在一條直線上,所以向上 i 次,就要向下 i 次,且(0<=i<n/2,?i+i <=n),而上下的次序不固定,顯然就是 i 個元素進(jìn)棧出棧的次序,方案數(shù)為??? (公式三);
從n個元素中選出2*i個向上走或者向下走,n-2*i 個元素水平走,方案數(shù)為??;
向上0次,向下0次,水平直線走,方案數(shù)為??;
向上 i 次,向下 i 次,方案數(shù)??
向上i+1次,向下i+1次,方案數(shù)?
遞推式:
?
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();} }?
總結(jié)
以上是生活随笔為你收集整理的Catalan Numbers 卡特兰数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU4135 HDU2841 HDU1
- 下一篇: HDU2049 组合数学 错排公式