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 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, four
sides of the box are transparent, so Harry can see the inside from the four sides. Seeing from the left of the box, Harry finds each row is shining(it means each row has at least one jewel). And seeing from the front of the box, each column is shining(it means
each column has at least one jewel). Harry wants to know how many kinds of jewel’s distribution are there in the box.And the answer may be too large, you should output the answer mod 1000000007.

Input

There are several test cases.

For each test case,there are two integers n and m indicating the size of the box.
0≤n,m≤50.

Output

For each test case, just output one line that contains an integer indicating the answer.

Sample Input

1 1
2 2
2 3

Sample Output

1
7
25

Hint

There are 7 possible arrangements for the second test case.
They are:
11
11

11
10

11
01

10
11

01
11

01
10

10
01

Assume that a grids is ‘1‘ when it contains a jewel otherwise not.

 

Source

BestCoder Round #25

Recommend

heyang   |   We have carefully selected several similar problems for you:  5157 5156 5153 5152 5151

一开始想复杂了,想了个三维dp,dp[i][j][k]表示放了k个棋子,现在有i行j列是亮的

然后悲剧了半天还是没调出来

dp[i][j] 表示在前i行,每一行都亮,而一共有j列是亮的

考虑第i行放k个,亮t个

dp[i][j + t] = dp[i - 1][j] * C[m - j][t] * C[j][k - t]

/*************************************************************************
    > File Name: hdu5155.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年01月03日 星期六 21时52分09秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int mod = 1000000007;
long long dp[55][55];
long long C[55][55];

void Combine()
{
	memset (C, 0, sizeof(C));
	C[0][0] = 1;
	for (int i = 1; i <= 50; ++i)
	{
		C[i][0] = 1;
		for (int j = 1; j <= i; ++j)
		{
			if (i == j)
			{
				C[i][j] = 1;
			}
			else if (j == 1)
			{
				C[i][j] = i;
			}
			else
			{
				C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
				C[i][j] %= mod;
			}
//			printf("%d\n", C[i][j]);
		}
	}
}

int main()
{
	Combine();
	int n, m;
	while (~scanf("%d%d", &n, &m))
	{
		memset (dp, 0, sizeof(dp));
		dp[0][0] = 1;
		for (int i = 1; i <= n; ++i)
		{
			for (int j = 0; j <= m; ++j) //前i - 1行亮的列数
			{
				for (int k = 1; k <= m; ++k)//这一行放了k个
				{
					for (int t = 0; j + t <= m; ++t)//这一行亮t个
					{
						if (k - t < 0)
						{
							continue;
						}
						long long tmp = dp[i - 1][j] * C[m - j][t];
						tmp %= mod;
						tmp *= C[j][k - t];
						tmp %= mod;
						dp[i][j + t] += tmp;
						dp[i][j + t] %= mod;
					}
				}
			}
		}
		printf("%lld\n", dp[n][m]);
	}
	return 0;
}
时间: 2024-08-06 19:44:30

hdu5155---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.

[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表示组合数

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 i