传送门

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. JS与JQ 获取页面元素值的方法和差异对比

    获取浏览器高度和宽度 document.documentElement.clientWidth ==> 浏览器可见区域宽度 document.documentElement.clientHeig ...

  2. 题解 P5082 【成绩】

    随机跳题跳到了这一题,一看是个红题,本蒟蒻就 艰难地思考起来 高兴地写起来 这题实在不能用数组,用了数组就RE 一开始就卡在这上面了 说实话,这道题真的 很难 不算很难,只要照着公式往上面套就行了 废 ...

  3. ios 团购信息客户端demo(一)

    团购信息客户端,主要整合了ASIHTTPREQUEST,KISSXML,AQGridView,MBProgressHUD这几个主要流行的IOS开发库,我们先来看一下效果图 首先我们新建一个IOS工程, ...

  4. DOM事件总结

    1.DOM事件: DOM0: element.onclick=function(){} DOM2: element.addEventListener(‘click’,function(){}) add ...

  5. Redis string类型常用操作

      Redis 有 string.list.set.zset.hash数据类型.string类型是最基础的,其他类型都是在string类型上去建立的,所以了解熟悉string类型的常用操作对于学习re ...

  6. free指令的说明

    CentOS 6.x系统中的freefree [-b|-k|-m|-g|-h] [-l] [-o] [-t] [-s delay] [-c count] [-V] -b #-k,-m,-g 以单位by ...

  7. django第六天(模板相关,过滤器和标记)

    django第6天 DTL简介 django template language django模板语言 语法: filter{{}} tag{% %} 简单变量的使用 视图函数可以通过两种方式将变量船 ...

  8. java复习之基础环境

    环境基本介绍: JDK(Java Development Kit) 是 Java 语言的软件开发工具包(SDK).在JDK的安装目录下有一个jre目录,里面有两个文件夹bin和lib,在这里可以认为b ...

  9. LeetCode(90) Subsets II

    题目 Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  10. shell脚本举例

    1.有时在写一些以循环方式运行的监控脚本,设置时间间隔是必不可少的,下面是一个Shell进度条的脚本演示在脚本中生成延时. #!/bin/bash b='' for ((i=0;$i<=100; ...