首页 > 其他 > 详细

HDU 5147 Harry And Magic Box dp+组合数

时间:2015-01-04 17:15:32      阅读:300      评论:0      收藏:0      [点我收藏+]

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

官方题解:
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个元素的所有组合的个数。
//187MS	1200K
#include<stdio.h>
#include<algorithm>
#include<string.h>
#define ll long long
#define mod 1000000007
using namespace std;
ll dp[57][57],c[57][57];
void calc()//求组合数
{
    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 ()
{
    calc();
    int n,m;
    while (scanf ("%d%d",&n,&m)!=EOF)
    {
        memset(dp,0,sizeof(dp));
        dp[0][0]=1;
        for(int i=1;i<=n;i++)//第i行
            for(int k=1;k<=m;k++)//第k列
                for(int j=0;j<=k;j++)//上一行亮多少个
                    for(int 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;
        printf("%d\n",dp[n][m]%mod);
    }
    return 0;
}


HDU 5147 Harry And Magic Box dp+组合数

原文:http://blog.csdn.net/crescent__moon/article/details/42393209

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!