Harry And Magic Box
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 196 Accepted Submission(s): 97
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
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.
/*
HDU 5155 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表示组合数
C[m-j][k-j]表示新增的那些原来没棋子现在有棋子的列k-j列分别可以放到上一行没放的地方m-j个地方,这样放会增加至少有一个棋子的列
C[j][t-(k-j)]表示剩下的在原来那j个有棋子的列去放,这样放不会增加至少有一个棋子的列。
*/
#include<iostream>
#include<stdio.h>
using namespace std;
#define MOD 1000000007
#define N 55
#define LL __int64
LL c[N][N],dp[N][N];
void init()
{
for(int i=0;i<=51;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;
}
}
int main()
{
int m,n,i,j,k,t;
init();
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(dp,0,sizeof(dp));
dp[0][0]=1;
for(i=1;i<=n;i++)//第i行
{
for(k=1;k<=m;k++)//这一行有多少个亮
{
for(j=0;j<=k;j++) //上一行有多少个亮
{
for(t=max(1,k-j);t<=k;t++)//这一行放多少个,至少放一个
{
dp[i][k] = (dp[i][k]+dp[i-1][j]*c[m-j][k-j]%MOD*c[j][t-(k-j)]%MOD)%MOD;
}
}
}
}
printf("%I64d\n",dp[n][m]%MOD);
}
return 0;
}