1. 程式人生 > >卡特蘭數(JAVA)

卡特蘭數(JAVA)

Game of Connections

Description

This is a small but ancient game. You are supposed to write down the numbers 1, 2, 3, ... , 2n - 1, 2n consecutively in clockwise order on the ground to form a circle, and then, to draw some straight line segments to connect them into number pairs. Every number must be connected to exactly one another. And, no two segments are allowed to intersect.

It's still a simple game, isn't it? But after you've written down the 2n numbers, can you tell me in how many different ways can you connect the numbers into pairs? Life is harder, right?

Input

Each line of the input file will be a single positive number n, except the last line, which is a number -1. You may assume that 1 <= n <= 100.

Output

For each n, print in a single line the number of ways to connect the 2n numbers into pairs.

Sample Input

2 
3 
-1

Sample Output

2 
5
import java.util.*;
import java.math.*;

public class Main {
	
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		BigInteger[]h=new BigInteger[101];
		h[1]=new BigInteger("1");
		for(int i=2;i<=100;i++){
			h[i]=h[i-1].multiply(BigInteger.valueOf(4*i-2)).divide(BigInteger.valueOf(i+1));
			
		}
		while(cin.hasNext()) {
			int n=cin.nextInt();
			if(n==-1)
				break;
			System.out.println(h[n]);
		}
	}
}

- How Many Trees?

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
import java.util.*;
import java.math.*;

public class Main {
	
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		BigInteger[]h=new BigInteger[101];
		h[1]=new BigInteger("1");
		for(int i=2;i<=100;i++){
			h[i]=h[i-1].multiply(BigInteger.valueOf(4*i-2)).divide(BigInteger.valueOf(i+1));
			
		}
		while(cin.hasNext()) {
			int n=cin.nextInt();
			if(n==-1)
				break;
			System.out.println(h[n]);
		}
	}
}

m人有50元,n人有100元,收營員要拿收到的50找給100,如果沒有50就停止,求符合條件序列的個數。

m < n 時 不存在合法方案 
當 m >= n 時 
合法排列有定有 m!×n!×C(m,m+n)種排法 
而有 m!×n!×C(m+1,m+n)          非法排法 (從有1個50的前邊至少多1個100) 
所以有合法排法 m!×n!×(C(m,m+n)−C(m+1,m+n))= (m+n)!×(m−n+1)/(m+1)

Sample Input

3 0

3 1

3 3

0 0
Sample Output

Test #1: 6

Test #2: 18

Test #3: 180
import java.util.*;
import java.math.*;

public class Main {
	
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int cnt=1;
		while(cin.hasNext()) {
			int a=cin.nextInt();
			int b=cin.nextInt();
			if(a==0&&b==0)
				break;
			System.out.println("Test #"+(cnt++)+":");
			BigDecimal c;
			if(a>=b)
			{
				c=BigDecimal.ONE;
				for(int i=1; i<=(a+b); i++)
                    c=c.multiply(new BigDecimal(i));
                c=c.multiply(new BigDecimal(a-b+1));
                c=c.divide(new BigDecimal(a+1));
                System.out.println(c);
			}
			else
				System.out.println("0");
		}
	}
}