卡特兰数
定义设h[i]为卡特兰数的第i项,则h[i]=sum(j=0 to j=i-1)h[j]*h[i-j-1]
公式
网上许多博客的公式是错的,我在这里整理一些正确的公式
1.递推式1(定义式):f(n)=sigma(f[i]*f[n-i-1])(0<=i<=n-1)
2.递推式2:f(n+1)=f(n)*(4n+2)/(n+2);
f[n]=f(n-1)*(4n-2)/(n+1)
注:递推式中f(0)=1;
3.通项公式1:f(n)=C(2n,n)/(n+1);
4.通项公式2:f(n)=C(2n,n)-C(2n,n+1);
证明 博客推荐:(部分内容来自此博客)
https://www.cnblogs.com/zyt1253679098/p/9190217.html
在上文提到的出栈序列的问题情景中,如果有n个元素,在平面直角坐标系中用x坐标表示入栈数,y坐标表示出栈数,则坐标(a,b)表示目前已经进行了a次入栈和b次出栈,则再进行一次入栈就是走到(a+1,b),再进行一次出栈就是走到(a,b+1)。并且,由于入栈数一定小于等于出栈数,所以路径不能跨越直线y=x
因此,题目相当于求从(0,0)走到(n,n)且不跨越直线y=x的方案数
方案数=总方案数-不合法方案数;
首先,如果不考虑不能跨越直线y=x的要求,相当于从2n次操作中选n次进行入栈,相当于从2n个位置选n个位置作为入栈时间,则方案数为C(2n,n),这是总方案数。
然后,考虑对于一种不合法的方案,一定在若干次操作后有一次出栈数比入栈数多一次,这个点在直线y=x+1上。那么把第一次碰到该直线以后的部分关于该直线对称,则最终到达的点是(n−1,n+1) 。
显然,任何非法方案都可以通过此方式变成一条从(0,0)到(n−1,n+1)的路径,任何一条从(0,0)到(n−1,n+1)的路径都可以对应一种非法方案,相当于形成了方案与路径的双射。(证明:能走到(n-1,n+1)->经过直线y=x+1->不合法方案;合法方案-->不会有点在y=x+1上-->无论沿路径上哪一点对称,对称后的路径都存在断层-->这不是一条路径)。于是,我们证明了不合法方案与从(0,0)到(n−1,n+1)的无限制路径的一一对应关系。而从(0,0)到(n−1,n+1)的无限制路径有C(2n,n-1)种,所以不合法方案一共有C(2n,n-1)种。而任何合法方案由于不接触直线y=x+1,无论从哪个点对称都不是一条连续的路径。由于合法方案数就是Catalan[n],所以:
Catalan[n]=C(2n,n)−C(2n,n-1)
(卡特兰数其他公式的数学证明详见推荐的博客,~~打符号太麻烦了~~)
应用
1、一个栈(无穷大)的进栈序列为1,2,3,…,n,有多少个不同的出栈序列?
2、n个节点构成的二叉树,共有多少种情形?
3、求一个凸多边形区域划分成三角形区域的方法数?
4、在圆上选择2n个点,将这些点成对链接起来使得所得到的n条线段不相交,一共有多少种方法?(下图供参考)
5、n* n的方格地图中,从一个角到另外一个角,不跨越对角线的路径数为h(n)
6、n层的阶梯切割为n个矩形的切法数也是。
卡特兰数的前一百位
//以下数据是从1开始的 //写递推程序时一定要先写h[0]=1; string catalan[]={ "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", "18367353072152", "69533550916004", "263747951750360", "1002242216651368", "3814986502092304", "14544636039226909", "55534064877048198", "212336130412243110", "812944042149730764", "3116285494907301262", "11959798385860453492", "45950804324621742364", "176733862787006701400", "680425371729975800390", "2622127042276492108820", "10113918591637898134020", "39044429911904443959240", "150853479205085351660700", "583300119592996693088040", "2257117854077248073253720", "8740328711533173390046320", "33868773757191046886429490", "131327898242169365477991900", "509552245179617138054608572", "1978261657756160653623774456", "7684785670514316385230816156", "29869166945772625950142417512", "116157871455782434250553845880", "451959718027953471447609509424", "1759414616608818870992479875972", "6852456927844873497549658464312", "26700952856774851904245220912664", "104088460289122304033498318812080", "405944995127576985730643443367112", "1583850964596120042686772779038896", "6182127958584855650487080847216336", "24139737743045626825711458546273312", "94295850558771979787935384946380125", "368479169875816659479009042713546950", "1440418573150919668872489894243865350", "5632681584560312734993915705849145100", "22033725021956517463358552614056949950", "86218923998960285726185640663701108500", "337485502510215975556783793455058624700", "1321422108420282270489942177190229544600", "5175569924646105559418940193995065716350", "20276890389709399862928998568254641025700", "79463489365077377841208237632349268884500", "311496878311103321137536291518809134027240", "1221395654430378811828760722007962130791020", "4790408930363303911328386208394864461024520", "18793142726809884575211361279087545193250040", "73745243611532458459690151854647329239335600", "289450081175264899454283846029490767264392230", "1136359577947336271931632877004667456667613940", "4462290049988320482463241297506133183499654740", "17526585015616776834735140517915655636396234280", "68854441132780194707888052034668647142985206100", "270557451039395118028642463289168566420671280440", "1063353702922273835973036658043476458723103404520", "4180080073556524734514695828170907458428751314320", "16435314834665426797069144960762886143367590394940", "64633260585762914370496637486146181462681535261000", "254224158304000796523953440778841647086547372026600", "1000134600800354781929399250536541864362461089950800", "3935312233584004685417853572763349509774031680023800", "15487357822491889407128326963778343232013931127835600", "60960876535340415751462563580829648891969728907438000", "239993345518077005168915776623476723006280827488229600", "944973797977428207852605870454939596837230758234904050", "3721443204405954385563870541379246659709506697378694300", "14657929356129575437016877846657032761712954950899755100", "57743358069601357782187700608042856334020731624756611000", "227508830794229349661819540395688853956041682601541047340", "896519947090131496687170070074100632420837521538745909320" };