日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

hdu1032 Train Problem II (卡特兰数)

發布時間:2025/7/14 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hdu1032 Train Problem II (卡特兰数) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題意:

給你一個數n,表示有n輛火車,編號從1到n,入站,問你有多少種出站的可能。??? (題于文末)

?

知識點:

??????????????????????????????????????????????????????????????????????????????????????? ps:百度百科的卡特蘭數講的不錯,注意看其參考的博客。

卡特蘭數(Catalan):前幾項為 : 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845, 35357670…

??? 令h(0)=1,h(1)=1,catalan數滿足遞推式:

???? h(n)= h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)h(0) (n>=2)

??? 另類遞推式:

???

??? 遞推關系的解為:

???

??? 遞推關系的另類解為:

??

?? 對于在2n位的2進制中,有n個0,其余為1,且1的累計數>=0的累計數,二進制數有種

?? 對于在n位的2進制中,有m個0,其余為1的catalan數為:

??? 理解:catalan數的理解

??? 應用:

??? 1.出棧次序: 一個棧(無窮大)的進棧序列為1,2,3,…,n,有多少個不同的出棧序列?????????? h(n)種。

??? 2.給定節點組成二叉樹:給定n個節點,能構成多少種不同的二叉樹???? h(n)種。

??? 3.括號化:矩陣連乘,依據乘法結合律,不改變其順序,只用括號表示成對的乘積,有幾種括號化的方案?? h(n-1)種。

??? 4.凸多邊形三角劃分:在一個凸n邊形中,通過若干條互不相交的對角線,有多少種方法把這個多邊形劃分成若干個三角形????????? h(n-2)種。

/**/???

?

題解:

此題為應用1,運用公式?? ?

catalan數計算一般都涉及大數運算,java寫起來方便。

?

import java.util.Scanner; import java.math.BigInteger; import java.io.*;public class Main{public static void main(String[] args){int n;Scanner sc=new Scanner(System.in);while(sc.hasNext()){n=sc.nextInt();BigInteger ans=BigInteger.valueOf(1);for(int i=1;i<=n;i++){ans=ans.multiply(BigInteger.valueOf(4*i-2));ans=ans.divide(BigInteger.valueOf(i+1));}System.out.println(ans);}} }

?

?

?

?

?

Train Problem II

Time Limit:1000MS???? Memory Limit:32768KB???? 64bit IO Format:%I64d & %I64u

Submit Status

Description

As we all know the Train Problem I, the boss of the Ignatius Train Station want to know if all the trains come in strict-increasing order, how many orders that all the trains can get out of the railway.

Input

The input contains several test cases. Each test cases consists of a number N(1<=N<=100). The input is terminated by the end of file.

Output

For each test case, you should output how many ways that all the trains can get out of the railway.

Sample Input

1 2 3 10

Sample Output

1 2 5 16796

Hint

The result will be very large, so you may not process it by 32-bit integers.

轉載于:https://www.cnblogs.com/shentr/p/5349707.html

總結

以上是生活随笔為你收集整理的hdu1032 Train Problem II (卡特兰数)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。