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

/* ***********************************************
Author        :CKboss
Created Time  :2015年02月15日 星期日 22时41分33秒
File Name     :HDOJ5155.cpp
************************************************ */

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

using namespace std;

typedef long long int LL;

const LL mod=1000000007LL; 

LL dp[100][100];
LL C[100][100];
LL two[100];

void init()
{
	for(int i=1;i<=50;i++)
	{
		dp[1][i]=dp[i][1]=1LL;
		two[i]=(1LL<<i)%mod;
	}

	for(int i=1;i<=50;i++)
		C[i][0]=C[i][i]=1LL;
	for(int i=2;i<=50;i++)
	{
		for(int j=1;j<i;j++)
		{
			C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;
		}
	}

	for(int i=1;i<=50;i++)
	{
		for(int j=1;j<=50;j++)
		{
			if(dp[i][j]==0)
			{
				LL temp=dp[i][j-1]*(two[i]+mod-1)%mod;
				for(int k=1;k<i;k++)
				{
					temp=(temp+((C[i][k]*dp[i-k][j-1])%mod*two[i-k])%mod)%mod;
				}
				dp[i][j]=dp[j][i]=temp;
			}
		}
	}
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

	int a,b;
	init();
	while(scanf("%d%d",&a,&b)!=EOF)
	{
		cout<<dp[a][b]<<endl;
	}

    return 0;
}
时间: 2024-10-10 20:56:29

HDOJ 5155 Harry And Magic Box DP的相关文章

[HDOJ 5155] Harry And Magic Box

题目链接:HDOJ - 5155 题目大意 有一个 n * m 的棋盘,已知每行每列都至少有一个棋子,求可能有多少种不同的棋子分布情况.答案对一个大素数取模. 题目分析 算法1: 使用容斥原理与递推. 首先,一个 n * m 的棋盘不考虑任何限制时,可能的分布情况为 2^(n*m) ,除去没有棋子的情况,为 2^(n*m) - 1 . 然后,因为所有的 n 行,m 列都有棋子,所以枚举 ii (1 <= ii <= i) , jj (1 <= jj <= j) .对于枚举的一组 (

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

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.

【HDOJ】5155 Harry And Magic Box

DP.dp[i][j]可以表示i行j列满足要求的组合个数,考虑dp[i-1][k]满足条件,那么第i行的那k列可以为任意排列(2^k),其余的j-k列必须全为1,因此dp[i][j] += dp[i-1][k]*(2^k)*C(j, k). 1 /* 5155 */ 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 6 #define MAXN 51 7 8 const __int64 MOD

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

问题描述 有一天,哈利得到了一个神奇的盒子.这个盒子由n*m个格子组成,有一些格子里会有闪闪发光的宝石.但是盒子的顶部和底部都被神奇的魔法封印着,所以哈利没办法从顶部和底部看到盒子的内部.然而,盒子的四边都是透明的,哈利可以从盒子的四边看到里面的情况.哈利发现,这个神奇的盒子,从左边看过去,每一行都闪烁着光芒,从前面看过去,每一列也都闪烁着光芒.哈利想知道,盒子里的宝石有多少种分布情况.答案有可能很大,所以输出答案对1000000007取模. 输入描述 多组输入数据 每组数据一行,输入两个数n

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

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