传送门

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个元素的所有组合的个数。
 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<string> #define N 55
#define M 10
#define mod 1000000007
//#define p 10000007
#define mod2 1000000000
#define ll long long
#define LL long long
#define eps 1e-9
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; ll n,m;
ll dp[N][N];
ll ans;
ll c[N][N];
ll sum[N][N]; void ini1()
{
memset(c,,sizeof(c));
memset(sum,,sizeof(sum));
int i,j;
for(i=;i<=N-;i++){
c[i][]=c[i][i]=;
}
for(i=;i<=N-;i++){
for(j=;j<i;j++){
c[i][j]=(c[i-][j-]+c[i-][j])%mod;
}
}
//for(i=1;i<=M-5;i++){
// for(j=0;j<=i;j++){
// printf(" i=%d j=%d c=%I64d\n",i,j,c[i][j]);
// }
//}
for(i=;i<=N-;i++){
sum[i][]=;
for(j=;j<=i;j++){
sum[i][j]=(sum[i][j-]+c[i][j])%mod;
}
}
} void ini()
{
memset(dp,,sizeof(dp));
ans=;
dp[][]=;
} void solve()
{
int i,j,k,o;
i=;
for(j=;j<=m;j++){
dp[i][j]=c[m][j];
}
for(i=;i<=n;i++){
for(j=;j<=m;j++){
dp[i][j]=( dp[i-][j]*(sum[j][j]-) )%mod;
for(k=;k<j;k++){
o=j-k;
dp[i][j]=(dp[i][j]+( ( dp[i-][k]*(sum[k][k]) ) %mod ) * (c[m-k][o]) )%mod;
}
}
}
} void out()
{
//int i,j; //for(i=1;i<=n;i++){
// for(j=1;j<=m;j++){
// printf(" i=%d j=%d dp=%I64d\n",i,j,dp[i][j]);
// }
// }
ans=(dp[n][m])%mod;
printf("%I64d\n",ans);
} int main()
{
ini1();
// freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
//scanf("%d",&T);
//for(int ccnt=1;ccnt<=T;ccnt++)
// while(T--)
while(scanf("%I64d%I64d",&n,&m)!=EOF)
{
ini();
solve();
out();
} return ;
}

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

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

    题目传送门 /* 设一个b[]来保存每一个a[]的质因数的id,从后往前每一次更新质因数的id, 若没有,默认加0,nlogn复杂度: 我用暴力竟然水过去了:) */ #include <cst ...

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

    当要求递推数列的第n项且n很大时,怎么快速求得第n项呢?可以用矩阵快速幂来加速计算.我们可以用矩阵来表示数列递推公式比如fibonacci数列 可以表示为 [f(n)   f(n-1)] = [f(n ...

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

    题目传送门 /* 贪心/二分查找:首先对ai%=p,然后sort,这样的话就有序能使用二分查找.贪心的思想是每次找到一个aj使得和为p-1(如果有的话) 当然有可能两个数和超过p,那么an的值最优,每 ...

  4. Manacher BestCoder Round #49 ($) 1002 Three Palindromes

    题目传送门 /* Manacher:该算法能求最长回文串,思路时依据回文半径p数组找到第一个和第三个会文串,然后暴力枚举判断是否存在中间的回文串 另外,在原字符串没啥用时可以直接覆盖,省去一个数组空间 ...

  5. 二分图判定+点染色/并查集 BestCoder Round #48 ($) 1002 wyh2000 and pupil

    题目传送门 /* 二分图判定+点染色:因为有很多联通块,要对所有点二分图匹配,若不能,存在点是无法分配的,no 每一次二分图匹配时,将点多的集合加大最后第一个集合去 注意:n <= 1,no,两 ...

  6. hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]

    传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131 ...

  7. BestCoder Round #56 1002 Clarke and problem 1003 Clarke and puzzle (dp,二维bit或线段树)

    今天第二次做BC,不习惯hdu的oj,CE过2次... 1002 Clarke and problem 和Codeforces Round #319 (Div. 2) B Modulo Sum思路差不 ...

  8. BestCoder Round #92 1002 Count the Sheep —— 枚举+技巧

    题目链接:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=748&pid=1002 题解: 做题的时候只是想到 ...

  9. BestCoder Round #80 1002

    HDU 5666 Segment 题意:给你条斜率为-1,常数项为q(q为质数)的直线,连接原点与直线上整数格点,问你在有多少个格点在形成的无数个三角形内,而不在线段上,结果对P取模. 思路:best ...

随机推荐

  1. Javascript 日期格式化

    Javascript 日期格式化 需求: 给出:日期 .格式,根据日期格式进行输出. Date.prototype.Format = function (fmt) { //author: meizz ...

  2. 兼容IE6\7\8浏览器的html5标签的几个方案

    html5大行其道的时代已经到来.如果你还在等待浏览器兼容,说明你已经与web脱节几条街了.当然,这得益于移动客户端的蓬勃发展.如果还在纠结于,是否应该掌握html5和css3技术时,请狠狠的抽自己几 ...

  3. Asp.Net Core 入门(十)—— 模型绑定和验证

    模型绑定时将Http请求中的数据映射到控制器操作方法上对应的参数,操作方法中的参数可以是简单类型,如整形,字符串等,也可以是复杂类型,如Product,Order等. Asp.Net Core MVC ...

  4. python_108_格式化字符串format函数

    #通过关键字映射 print('I am {name},age {age}'.format(name='qiqi齐',age=18))#I am qiqi齐,age 18 dictory={'name ...

  5. node mocha mochawesome报安装不成功

    1.进行cnpm安装: npm install cnpm -g --registry=https://registry.npm.taobao.org 2.查看cnpm版本 cnpm -v 3.安装mo ...

  6. CPP-基础:cout

    C++编程语言互换流中的标准输出流,需要iostream.h支持.读为 "c out". 使用范例 //用户输入的数字由cin保存于变量a中,并通过cout输出. #include ...

  7. 浏览器window产生的缓存九种解决办法

    浏览器缓存(Browser Caching)是浏览器端保存数据用于快速读取或避免重复资源请求的优化机制,有效的缓存使用可以避免重复的网络请求和浏览器快速地读取本地数据,整体上加速网页展示给用户.浏览器 ...

  8. 牛客网NOIP赛前集训营-提高组(第三场)A 管道维修

    https://www.nowcoder.com/acm/contest/174/A 这个的话  一个位置被清理的时间就是它到空白格子/边界的最短路对吧qww 然后求期望的话 假设它在第i步被清理掉的 ...

  9. 【dp 贪心】bzoj4391: [Usaco2015 dec]High Card Low Card

    巧妙的贪心 Description Bessie the cow is a huge fan of card games, which is quite surprising, given her l ...

  10. MySql的基操勿六

    2018/12/6 星期四 19:34:07 authot by dabaine 数据库注释; -- 这就是注释 /*.....*/ 这也是注释 创建库; create databse [if not ...