Train Problem II

这题是一个Catalan数,利用公式h(n)=h(n-1)*(4*n-2)/(n+1),其实还可以用其他卡特兰公式,但这个用起来更简单,但是涉及大数问题,用C写比较麻烦,本人用了最近师哥讲的JAVA写的.

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.

        
 

JAVA代码:

import java.util.*;
import java.math.*;
public class Main {
	public static void main(String args[]) {
		Scanner cin = new Scanner(System.in);
		BigInteger a,b,c,d,n,s;
		BigInteger[] dp;
		dp=new BigInteger[105];
		dp[1]=new BigInteger("1");
		a=new BigInteger("4");
		b=new BigInteger("2");
		c=new BigInteger("1");
		int i,m;
		for(i=2;i<=100;i++)
		{
			d=BigInteger.valueOf(i);
			n=d.multiply(a).subtract(b);
			s=d.add(c);
			dp[i]=dp[i-1].multiply(n).divide(s);
		}
		while(cin.hasNext())
		{
		    m=cin.nextInt();
		    System.out.println(dp[m]);
		}
	}
}

你可能感兴趣的:(卡特兰数,dp)