首页 > 代码库 > Harry And Magic Box

Harry And Magic Box

import java.util.Scanner;

//http://acm.hdu.edu.cn/showproblem.php?pid=5155
public class Main {

	public static int[][] c;
	public static int[] bi;
	public static int len = 55;
	public static int mod = 1000000007;

	static {
		c = new int[len][len];
		bi = new int[len];
		for (int i = 0; i < len; i++) {
			c[i][0] = 1;
			for (int j = 1; j <= i; j++) {
				c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod;
			}
		}
		bi[0] = 1;
		for (int i = 1; i < len; i++) {
			bi[i] = (int) (bi[i - 1] * (long) 2) % mod;
		}
	}

	public static long solve(int n, int m) {
		long[][] dp = new long[len][len];
		dp[0][0] = 1;
		//dp[i][j] i层时有j列布1,在m-j外布k列1
		for (int i = 1; i <= n; i++) {
			for (int j = 0; j <= m; j++) {
				for (int k = 0; k + j <= m; k++) {
					//每行肯定有一个1
					if (j + k == 0)
						continue;
					long tmp = (dp[i - 1][j] * c[m - j][k]) % mod;
					if (k != 0)
						tmp = tmp * bi[j] % mod;
					else
						//当k==1时有2^j次减去不放的情况
						tmp = tmp * (bi[j] - 1) % mod;
					dp[i][j + k] = (dp[i][j + k] + tmp) % mod;
				}
			}
		}
		return dp[n][m];
	}

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);

		while (in.hasNextInt()) {
			int n = in.nextInt();
			int m = in.nextInt();
			System.out.println(solve(n, m));
		}
	}
}

Harry And Magic Box