HDU3723-Delta Wave(Catalan數+組合計數)

Delta Wave

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 741    Accepted Submission(s): 243


Problem Description
A delta wave is a high amplitude brain wave in humans with a frequency of 1 – 4 hertz which can be recorded with an electroencephalogram (EEG) and is usually associated with slow-wave sleep (SWS).
-- from Wikipedia

The researchers have discovered a new kind of species called "otaku", whose brain waves are rather strange. The delta wave of an otaku's brain can be approximated by a polygonal line in the 2D coordinate system. The line is a route from point (0, 0) to (N, 0), and it is allowed to move only to the right (up, down or straight) at every step. And during the whole moving, it is not allowed to dip below the y = 0 axis.

For example, there are the 9 kinds of delta waves for N = 4:





Given N, you are requested to find out how many kinds of different delta waves of otaku.
 

Input
There are no more than 20 test cases. There is only one line for each case, containing an integer N (2 < N <= 10000)

 

Output
Output one line for each test case. For the answer may be quite huge, you need only output the answer module 10100.
 

Sample Input
3 4
 

Sample Output
4 9
 
題意:可以歸納爲:前n個數(0,1,-1)對於每個k <=n都有前綴和大於等於0,且前n項和爲0.
思路:先不管0,前n個數相加,總和爲0的方案數,很容易看出這是Catalan數,至於0,只要計算一下組合數就行了。然後與Catalan數相結合推出遞推公式就行。
import java.util.*;
import java.math.*;

public class Main {

	private static Scanner in;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		BigInteger MOD = BigInteger.ONE;
		for(int i = 1; i <= 100; i++){
			MOD = MOD.multiply(BigInteger.valueOf(10));
		}
		
		in = new Scanner(System.in);
		while(in.hasNextInt()){
			int n = in.nextInt();
			BigInteger ret = BigInteger.ONE,now = BigInteger.ONE;
			for(int i = 1; i+i <= n; i++){
				now = now.multiply(BigInteger.valueOf(n-2*i+1)).multiply(BigInteger.valueOf(n-2*i+2)).divide(BigInteger.valueOf(i*i+i));
				ret = ret.add(now);
			}
			ret = ret.mod(MOD);
			System.out.println(ret);
		}
	}
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章