卡塔兰数

这几天做hdu的ACM steps,遇到了好几个卡塔兰数

卡塔兰数是组合数学中一个常在各种计数问题中出现的数列。以比利时的数学家欧仁·查理·卡塔兰 (1814–1894)命名。

卡塔兰数的一般项公式为

卡特兰数前几项为 :1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012……

怎么来的我也不知道,好几个题大神们推出是卡塔兰数,然后我就安公式用java的大数求的……我用的公式是h(n)= h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)h(0) (n>=2)

下面附上几道题,以后慢慢研究……

Train Problem II

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 477 Accepted Submission(s): 291
 
Problem 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.


##代码

import java.util.Scanner;
import java.math.BigInteger;
public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner cin = new Scanner(System.in);
        BigInteger[]  a = new BigInteger[110];
        BigInteger b;
        int n,i,j;
        a[0]= new BigInteger("1");
        a[1]= new BigInteger("1");
        for(i=2;i<=100;i++){
            b=new BigInteger("0");
            for(j=0;j<i;j++){
                b=b.add(a[j].multiply(a[i-j-1]));
            }
            a[i]=b;
            //System.out.println(a[10]);
        }
            
        while(cin.hasNext()){
            n = cin.nextInt();
            System.out.println(a[n]);
        }
        cin.close();
    }

}

第二个题

看的网上的解题报告,说是卡塔兰数*A(N,N)的全排列,即不算顺序……表示不懂……不会推……

Count the Trees

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 489 Accepted Submission(s): 355
 
Problem Description
Another common social inability is known as ACM (Abnormally Compulsive Meditation). This psychological disorder is somewhat common among programmers. It can be described as the temporary (although frequent) loss of the faculty of speech when the whole power of the brain is applied to something extremely interesting or challenging.  
Juan is a very gifted programmer, and has a severe case of ACM (he even participated in an ACM world championship a few months ago). Lately, his loved ones are worried about him, because he has found a new exciting problem to exercise his intellectual powers, and he has been speechless for several weeks now. The problem is the determination of the number of different labeled binary trees that can be built using exactly n different elements.  

For example, given one element A, just one binary tree can be formed (using A as the root of the tree). With two elements, A and B, four different binary trees can be created, as shown in the figure.  

If you are able to provide a solution for this problem, Juan will be able to talk again, and his friends and family will be forever grateful.  

 
Input
The input will consist of several input cases, one per line. Each input case will be specified by the number n ( 1 ≤ n ≤ 100 ) of different elements that must be used to form the trees. A number 0 will mark the end of input and is not to be processed.  
 
Output
For each input case print the number of binary trees that can be built using the n elements, followed by a newline character.  
 
Sample Input
1
2
10
25
0
 
Sample Output
1
4
60949324800
75414671852339208296275849248768000000
 

##


第三个题

这个题是我看着那个样例,感觉是卡塔兰数,然后就那么写的,然后就过了……

How Many Trees?

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 679 Accepted Submission(s): 372
 
Problem Description
A binary search tree is a binary tree with root k such that any node v reachable from its left has label (v) <label (k) and any node w reachable from its right has label (w) > label (k). It is a search structure which can find a node with label x in O(n log n) average time, where n is the size of the tree (number of vertices).  

Given a number n, can you tell how many different binary search trees may be constructed with a set of numbers of size n such that each element of the set will be associated to the label of exactly one node in a binary search tree?  
 
Input
The input will contain a number 1 <= i <= 100 per line representing the number of elements of the set.
 
Output
You have to print a line in the output for each entry with the answer to the previous question.
 
Sample Input
1
2
3
 
Sample Output
1
2
5
 



第四个题

据说这个题也是……我木有发现……用dp干出来的……组合数学,坑爹啊……

Buy the Ticket

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1022 Accepted Submission(s): 475
 
Problem Description
The \\\\\\\"Harry Potter and the Goblet of Fire\\\\\\\" will be on show in the next few days. As a crazy fan of Harry Potter, you will go to the cinema and have the first sight, won’t you?

Suppose the cinema only has one ticket-office and the price for per-ticket is 50 dollars. The queue for buying the tickets is consisted of m + n persons (m persons each only has the 50-dollar bill and n persons each only has the 100-dollar bill).

Now the problem for you is to calculate the number of different ways of the queue that the buying process won\\\\\\\'t be stopped from the first person till the last person.  
Note: initially the ticket-office has no money.  

The buying process will be stopped on the occasion that the ticket-office has no 50-dollar bill but the first person of the queue only has the 100-dollar bill.
 
Input
The input file contains several test cases. Each test case is made up of two integer numbers: m and n. It is terminated by m = n = 0. Otherwise, m, n <=100.
 
Output

            For each test case, first print the test number (counting from 1) in one line, then output the number of different ways in another line.
 
Sample Input
3 0
3 1
3 3
0 0
 
Sample Output
Test #1:
6
Test #2:
18
Test #3:
180
 

##代码

代码好乱,反正就是求了所有的分支;dp[i][j]=dp[i-1][j]*(n-i+1)+dp[i][j-1]*(m-j+1);边界处理下完了……卡塔兰数解法木有看呢……

import java.util.Scanner;
import java.math.BigDecimal;
import java.math.BigInteger;
public class Main {
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
	Scanner cin = new Scanner(System.in);
	int n,i,m,k,j,t=0;
	BigInteger[][] dp;
	BigInteger ww,iw,jw,nw,mw;
	while(true){
		n = cin.nextInt();
		m = cin.nextInt();
		if(n==0&&m==0)
			break;
		t++;
		if(n<m){
			System.out.println("Test #"+t+":");
			System.out.println(0);
			continue;
		}
		dp = new BigInteger[n+1][m+1];
		for(i=0;i<=n;i++)
			for(j=0;j<=m;j++)
			dp[i][j]=new BigInteger("0");
		dp[0][0]=new BigInteger("1");
		ww = new BigInteger("1");
		for(i=0;i<n;i++)
			ww=ww.add(new BigInteger("1"));
		nw = ww;
		mw = new BigInteger("1");
		for(j=1;j<m;j++)
			mw = mw.add(new BigInteger("1"));
		//System.out.println(ww+" "+mw);
		for(i=1;i<=n;i++)
			dp[i][0]=dp[i-1][0].multiply(ww=ww.subtract(new BigInteger("1")));
		//for(i=0;i<=n;i++)
		//System.out.print(dp[i][0]+" ");
		//System.out.println();
		iw = new BigInteger("0");
		for(i=1;i<=n;i++){
			iw=iw.add(new BigInteger("1"));
			jw = new BigInteger("-1");
			for(j=1;j<=m;j++){
				jw=jw.add(new BigInteger("1"));
				if(i-1>=j)
				dp[i][j]=dp[i-1][j].multiply(nw.subtract(iw));//(n-i+1);
				//System.out.println(n-i+1);
				//System.out.println(nw.subtract(iw));
				if(j>0){
					ww=dp[i][j-1].multiply(mw.subtract(jw));
					dp[i][j]=dp[i][j].add(ww);
				}
				//dp[i][j]=dp[i][j].add(dp[i][j-1].multiply(mw.subtract(jw)));//*(m-j+1);
				//System.out.print(dp[i][j]+" ");
			}
			//System.out.println();
		}
		System.out.println("Test #"+t+":");
		System.out.println(dp[n][m]);
	}
	cin.close();
	}

}

#########################################################################################################

这组合数学要是会的话感觉好牛X,什么都不用想有公式往里面一代数, so easy, 的就A了……不会压力好大啊……



你可能感兴趣的:(卡塔兰数)