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

官方题解:

1002 Harry And Magic Box
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个元素的所有组合的个数。
  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdlib>
  4 #include<cstdio>
  5 #include<algorithm>
  6 #include<cmath>
  7 #include<queue>
  8 #include<map>
  9 #include<set>
 10 #include<string>
 11
 12 #define N 55
 13 #define M 10
 14 #define mod 1000000007
 15 //#define p 10000007
 16 #define mod2 1000000000
 17 #define ll long long
 18 #define LL long long
 19 #define eps 1e-9
 20 #define maxi(a,b) (a)>(b)? (a) : (b)
 21 #define mini(a,b) (a)<(b)? (a) : (b)
 22
 23 using namespace std;
 24
 25 ll n,m;
 26 ll dp[N][N];
 27 ll ans;
 28 ll c[N][N];
 29 ll sum[N][N];
 30
 31 void ini1()
 32 {
 33     memset(c,0,sizeof(c));
 34     memset(sum,0,sizeof(sum));
 35     int i,j;
 36     for(i=0;i<=N-5;i++){
 37         c[i][0]=c[i][i]=1;
 38     }
 39     for(i=2;i<=N-5;i++){
 40         for(j=1;j<i;j++){
 41             c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod;
 42         }
 43     }
 44     //for(i=1;i<=M-5;i++){
 45     //    for(j=0;j<=i;j++){
 46     //        printf(" i=%d j=%d c=%I64d\n",i,j,c[i][j]);
 47     //    }
 48     //}
 49     for(i=1;i<=N-5;i++){
 50         sum[i][0]=1;
 51         for(j=1;j<=i;j++){
 52             sum[i][j]=(sum[i][j-1]+c[i][j])%mod;
 53         }
 54     }
 55 }
 56
 57 void ini()
 58 {
 59     memset(dp,0,sizeof(dp));
 60     ans=0;
 61     dp[0][0]=1;
 62 }
 63
 64 void solve()
 65 {
 66     int i,j,k,o;
 67     i=1;
 68     for(j=1;j<=m;j++){
 69         dp[i][j]=c[m][j];
 70     }
 71     for(i=2;i<=n;i++){
 72         for(j=1;j<=m;j++){
 73             dp[i][j]=( dp[i-1][j]*(sum[j][j]-1) )%mod;
 74             for(k=1;k<j;k++){
 75                 o=j-k;
 76                 dp[i][j]=(dp[i][j]+( ( dp[i-1][k]*(sum[k][k]) ) %mod ) * (c[m-k][o]) )%mod;
 77             }
 78         }
 79     }
 80 }
 81
 82 void out()
 83 {
 84     //int i,j;
 85
 86     //for(i=1;i<=n;i++){
 87     //    for(j=1;j<=m;j++){
 88      //       printf(" i=%d j=%d dp=%I64d\n",i,j,dp[i][j]);
 89      //  }
 90    // }
 91     ans=(dp[n][m])%mod;
 92     printf("%I64d\n",ans);
 93 }
 94
 95 int main()
 96 {
 97     ini1();
 98    // freopen("data.in","r",stdin);
 99     //freopen("data.out","w",stdout);
100     //scanf("%d",&T);
101     //for(int ccnt=1;ccnt<=T;ccnt++)
102    // while(T--)
103     while(scanf("%I64d%I64d",&n,&m)!=EOF)
104     {
105         ini();
106         solve();
107         out();
108     }
109
110     return 0;
111 }
时间: 2025-01-14 05:22:19

BestCoder Round #25 1002 Harry And Magic Box [dp]的相关文章

Manacher BestCoder Round #49 ($) 1002 Three Palindromes

题目传送门 1 /* 2 Manacher:该算法能求最长回文串,思路时依据回文半径p数组找到第一个和第三个会文串,然后暴力枚举判断是否存在中间的回文串 3 另外,在原字符串没啥用时可以直接覆盖,省去一个数组空间,位运算 >>1 比 /2 速度快,用了程序跑快200ms左右,位运算大法好 4 */ 5 /************************************************ 6 Author :Running_Time 7 Created Time :2015-8-1

贪心/二分查找 BestCoder Round #43 1002 pog loves szh II

题目传送门 1 /* 2 贪心/二分查找:首先对ai%=p,然后sort,这样的话就有序能使用二分查找.贪心的思想是每次找到一个aj使得和为p-1(如果有的话) 3 当然有可能两个数和超过p,那么an的值最优,每次还要和an比较 4 注意:不能选取两个相同的数 5 反思:比赛时想到了%p和sort,lower_bound,但是还是没有想到这个贪心方法保证得出最大值,还是题目做的少啊:( 6 */ 7 #include <cstdio> 8 #include <algorithm>

矩阵快速幂---BestCoder Round#8 1002

当要求递推数列的第n项且n很大时,怎么快速求得第n项呢?可以用矩阵快速幂来加速计算.我们可以用矩阵来表示数列递推公式比如fibonacci数列 可以表示为 [f(n)   f(n-1)] = [f(n-1)    f(n-2)] [ 1 1 ]     [ 1 0 ] 设A = [ 1 1 ]  [ 1 0 ] [f(n)   f(n-1)] = [f(n-2)   f(n-3)]*A*A[f(n)   f(n-1)] = [f(2)   f(1)]*A^(n-2)矩阵满足结合律,所以先计算A^

二分图判定+点染色 BestCoder Round #48 ($) 1002 wyh2000 and pupil

题目传送门 1 /* 2 二分图判定+点染色:因为有很多联通块,要对所有点二分图匹配,若不能,存在点是无法分配的,no 3 每一次二分图匹配时,将点多的集合加大最后第一个集合去 4 注意:n <= 1,no,两个集合都至少有一人:ans == n,扔一个给另一个集合 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cstring> 9 #include <cmath> 10 #in

暴力+降复杂度 BestCoder Round #39 1002 Mutiple

题目传送门 1 /* 2 设一个b[]来保存每一个a[]的质因数的id,从后往前每一次更新质因数的id, 3 若没有,默认加0,nlogn复杂度: 4 我用暴力竟然水过去了:) 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <cstring> 9 #include <string> 10 #include <algorithm> 11 using namespace std;

BestCoder Round #1 1002 项目管理 (HDU 4858)

项目管理 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 738    Accepted Submission(s): 260 Problem Description 我们建造了一个大项目!这个项目有n个节点,用很多边连接起来,并且这个项目是连通的!两个节点间可能有多条边,不过一条边的两端必然是不同的节点.每个节点都有一个能量值. 现在我

hdu 5154 Harry and Magical Computer(BestCoder Round #25)

Harry and Magical Computer                                                       Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 472    Accepted Submission(s): 222 Problem Description In reward

BestCoder Round #4 1002

这题真是丧心病狂,引来今天的hack狂潮~ Miaomiao's Geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 10    Accepted Submission(s): 3 Problem Description There are N point on X-axis . Miaomiao would like t

HDU BestCoder Round #1 1002 项目管理

项目管理 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 0    Accepted Submission(s): 0 Problem Description 我们建造了一个大项目!这个项目有n个节点,用很多边连接起来,并且这个项目是连通的! 两个节点间可能有多条边,不过一条边的两端必然是不同的节点. 每个节点都有一个能量值. 现在我们