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));
		}
	}
}

时间: 2024-11-04 23:01:30

Harry And Magic Box的相关文章

hdu--5155Harry And Magic Box(组合数+容斥原理)

Harry And Magic Box Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Appoint description:  System Crawler  (2015-01-07) Description One day, Harry got a magical box. The box is made of n*m grids. There are sp

微软线上笔试-2015-4-3(1,2题) Magic Box &amp;&amp; Professor Q&#39;s Software

写在前面: http://blog.csdn.net/michael_kong_nju/article/details/44872519 关于4.3号的微软线上挑战赛,感觉自己还是刷题刷少了,表现在几个方面:1. 编程经验不足.2. 算法的使用不灵活.所以下面还是要加强OJ的训练, 把Leetcode上的题多做做.后面又把4道题仔细的编写调试了一下,希望和我情况类似的同学也能加紧代码的训练. 1. 第一题的原题是: The circus clown Sunny has a magic box.

hdu5155---Harry And Magic Box

Harry And Magic Box Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 131    Accepted Submission(s): 64 Problem Description One day, Harry got a magical box. The box is made of n*m grids. There a

[hihocoder] Magic Box

题目1 : Magic Box 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 The circus clown Sunny has a magic box. When the circus is performing, Sunny puts some balls into the box one by one. The balls are in three colors: red(R), yellow(Y) and blue(B). Let Cr, Cy, Cb

HDOJ 5155 Harry And Magic Box DP

dp[i][j] 表示 长宽为i,j的矩形的可能的总数 dp[i][j+1] 可由 dp[i][j] 推过来,枚举dp[i][j]所保留的行数(1...i)即可 Harry And Magic Box Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 441    Accepted Submission(s): 209 Problem D

BestCoder Round #25 1002 Harry And Magic Box [dp]

传送门 Harry And Magic Box Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 165    Accepted Submission(s): 64 Problem Description One day, Harry got a magical box. The box is made of n*m grids. Ther

HDU 5147 Harry And Magic Box dp+组合数

点击打开链接 Harry And Magic Box Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 197    Accepted Submission(s): 97 Problem Description One day, Harry got a magical box. The box is made of n*m grids.

HDU 5155 Harry And Magic Box(组合数学+容斥)

Problem Description One day, Harry got a magical box. The box is made of n*m grids. There are sparking jewel in some grids. But the top and bottom of the box is locked by amazing magic, so Harry can't see the inside from the top or bottom. However, f

HDU 5155 Harry And Magic Box --DP

题意:nxm的棋盘,要求每行每列至少放一个棋子的方法数. 解法:首先可以明确是DP,这种行和列的DP很多时候都要一行一行的推过去,即至少枚举此行和前一行. dp[i][j]表示前 i 行有 j 列都有了棋子,且每行也有棋子. 这题做法: 从第1行到第n行,枚举这一行有k列已至少有一个,再枚举前一行有j列至少有一个,然后枚举这一行新放多少个棋子t,至少一个(因为每行至少一个) 那么有 dp[i][k] += dp[i-1][j]*C[m-j][k-j]*C[j][t-(k-j)], C表示组合数