hdu 3723 Delta Wave

http://acm.hdu.edu.cn/showproblem.php?pid=3723

需要用到大整数 终于用java AC了

此题为经典 catalan 问题的变形

如果问题中的路径只能向上和向下的话就变成了 catalan 了,既然向上和向下的个数相等

枚举向上和向下的个数 每次枚举和剩下的向前状态个数进行组合 即可

代码:

 

import java.util.*;

import java.math.BigInteger;

import java.math.BigDecimal;



;



public class Main {



	public static void main(String[] args) {

		int N = 5005;

		BigInteger MOD = BigInteger.ONE;

		BigInteger k = BigInteger.TEN;

		for (int i = 0; i < 100; ++i) {

			MOD = MOD.multiply(k);

		}

		BigInteger[] catalan = new BigInteger[N];

		catalan[0] = BigInteger.ONE;

		for (int i = 1; i < N; ++i) {

			catalan[i] = catalan[i - 1].multiply(BigInteger.valueOf(4 * i - 2));

			catalan[i] = catalan[i].divide(BigInteger.valueOf(i + 1));

		}

		BigInteger[] C = new BigInteger[N * 2];

		Scanner in = new Scanner(System.in);

		int n;

		while (in.hasNext()) {

			n = in.nextInt();

			BigInteger ans = BigInteger.ZERO;

			C[0] = BigInteger.ONE;

			for (int i = 1; i <= n; ++i) {

				C[i] = C[i - 1].multiply(BigInteger.valueOf(n - i + 1));

				C[i] = C[i].divide(BigInteger.valueOf(i));

			}

			for (int i = 0; i <= n / 2; ++i) {

				BigInteger tmp = C[2 * i];

				tmp = tmp.multiply(catalan[i]);

				ans = ans.add(tmp);

			}

			ans = ans.mod(MOD);

			System.out.println(ans);

		}

	}



}

 

你可能感兴趣的:(HDU)