首页 > 代码库 > hdu 5155(DP)

hdu 5155(DP)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5155

Harry And Magic Box

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 273    Accepted Submission(s): 137


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.0n,m50技术分享.
 

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


题意:在一个n*m的纸盒上放置宝石,求保证每行每列至少放一个的方法数;

官方的解答:

dp题,我们一行一行的考虑。dp[i][j],表示前i行,都满足了每一行至少有一个宝石的条件,而只有j列满足了有宝石的条件的情况有多少种。枚举第i+1行放的宝石数k,这k个当中有t个是放在没有宝石的列上的,那么我们可以得到转移方程:
dp[i+1][j+t]+=dp[i][j]*c[m-j][t]*c[j][k-t],其中c[x][y],意为在x个不同元素中无序地选出y个元素的所有组合的个数。
代码如下:
/*
最好用 long long 
*/
#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <algorithm>
const int mod=1000000007;
using namespace std;
typedef long long ll;
int n,m;
ll C[110][110];
ll dp[110][110];

void init()//预处理组合数
{
    memset(C,0,sizeof(C));
    C[0][0]=1;
    for(int i=1;i<=55;i++)
    {
      C[i][0]=1;
      for(int j=1;j<i;j++)
      {
          C[i][j]=C[i-1][j-1]+C[i-1][j];
          if(C[i][j]>=mod)C[i][j]%=mod;
      }
      C[i][i]=1;
    }
}

void DP()
{
  for(int i=1;i<=m;i++)dp[1][i]=C[m][i];
  for(int i=2;i<=n;i++)
    for(int k=1;k<=m;k++)
      for(int z=0;z<=k;z++)
        for(int j=1;j<=m;j++)
        {
          if( j+z>m || (k-z>j) )continue;
          ll t=((dp[i-1][j]%mod*C[j][k-z]%mod)%mod*C[m-j][z]%mod)%mod;
          dp[i][j+z]+=t;
          dp[i][j+z]%=mod;
        }

  printf("%I64d\n",dp[n][m]);
}

int main()
{
  init();
  while(scanf("%d%d",&n,&m)!=EOF)
  {

     memset(dp,0,sizeof(dp));
     DP();
  }
  return 0;
}

hdu 5155(DP)