Harry And Magic Box

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

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.

 
Source
 
题意:一个n*m的矩阵,里面有一些珠宝,保证从行看过去每一行至少都有一个,从列看过去每列至少会有一个,问总共有多少珠宝摆放的可能方式??
题解:巧妙地递推.
假设dp[i][j]是前 i 行 前 j 列满足条件的个数
如果 dp[i][j-1] 已经满足了前 i 行,j-1 列都满足条件,那么第j行可以从 (1,n)个随意摆放
dp[i][j] = dp[i][j-1]*(C(i,1)+C(i,2)...+C(i,i)) = dp[i][j-1]*(2^i-1)
如果 dp[i][j-1] 中有k 行没有放东西,那么
dp[i][j] = dp[i-k][j-1]*C(i,k)*(C(i-k,0)+C(i-k,1)+C(i-k,2)...+C(i-k,i-k))=dp[i-k][j-1]*C(i,k)*2^(i-k)
/**
假设dp[i][j]是前 i 行 前 j 列满足条件的个数
如果 dp[i][j-1] 已经满足了前 i 行,j-1 列都满足条件,那么第j行可以从 (1,n)个随意摆放
dp[i][j] = dp[i][j-1]*(C(i,1)+C(i,2)...+C(i,i)) = dp[i][j-1]*(2^i-1)
如果 dp[i][j-1] 中有k 行没有放东西,那么
dp[i][j] = dp[i-k][j-1]*C(i,k)*(C(i-k,0)+C(i-k,1)+C(i-k,2)...+C(i-k,i-k))=dp[i-k][j-1]*C(i,k)*2^(i-k)
*/
#include<stdio.h>
#include<iostream>
#include<string.h>
#include <stdlib.h>
#include<math.h>
#include<algorithm>
#include <queue>
using namespace std;
typedef long long LL;
const LL mod = ;
LL c[][];
LL dp[][];
void init(){
for(int i=;i<;i++){
c[i][]=c[i][i]=;
for(int j=;j<i;j++){
c[i][j] = (c[i-][j-]+c[i-][j])%mod;
}
}
}
LL pow_mod(LL a,LL n){
LL ans = ;
while(n){
if(n&) ans = ans*a%mod;
a = a*a%mod;
n>>=;
}
return ans;
}
int main()
{
init();
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++){
dp[i][] = ;
}
for(int i=;i<=m;i++){
dp[][i] = ;
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
dp[i][j] = dp[i][j-]*(pow_mod(,i)-)%mod;
for(int k=;k<i;k++){
dp[i][j] = (dp[i][j] + dp[i-k][j-]*c[i][k]%mod*(pow_mod(,i-k))%mod)%mod;
}
}
}
printf("%lld\n",dp[n][m]);
}
return ;
}

hdu 5155(递推)的更多相关文章

  1. HDOJ(HDU).2044-2049 递推专题

    HDOJ(HDU).2044-2049 递推专题 点我挑战题目 HDU.2044 题意分析 先考虑递推关系:从1到第n个格子的时候由多少种走法? 如图,当n为下方格子的时候,由于只能向右走,所以有2中 ...

  2. HDU 2842 (递推+矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...

  3. "红色病毒"问题 HDU 2065 递推+找循环节

    题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=2065 递推类题目, 可以考虑用数学方法来做, 但是明显也可以有递推思维来理解. 递推的话基本就是状态 ...

  4. Children’s Queue HDU 1297 递推+大数

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1297 题目大意: 有n个同学, 站成一排, 要求 女生最少是两个站在一起, 问有多少种排列方式. 题 ...

  5. hdu 2044-2050 递推专题

    总结一下做递推题的经验,一般都开成long long (别看项数少,随便就超了) 一般从第 i 项开始推其与前面项的关系(动态规划也是这样),而不是从第i 项推其与后面的项的关系. hdu2044:h ...

  6. ZOJ 3182 HDU 2842递推

    ZOJ 3182 Nine Interlinks 题目大意:把一些带标号的环套到棍子上,标号为1的可以所以操作,标号i的根子在棍子上时,只有它标号比它小的换都不在棍子上,才能把标号为i+1的环,放在棍 ...

  7. hdu 2604 递推 矩阵快速幂

    HDU 2604 Queuing (递推+矩阵快速幂) 这位作者讲的不错,可以看看他的 #include <cstdio> #include <iostream> #inclu ...

  8. hdu 4055 递推

    转自:http://blog.csdn.net/shiqi_614/article/details/7983298 题意:由数字1到n组成的所有排列中,问满足题目所给的n-1个字符的排列有多少个,如果 ...

  9. HDU 3123-GCC(递推)

    GCC Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Subm ...

随机推荐

  1. WPF开发实例——仿QQ登录界面

    原文:WPF开发实例--仿QQ登录界面 版权声明:本文为博主原创文章,如需转载请标明转载地址 http://blog.csdn.net/u013981858 https://blog.csdn.net ...

  2. MyBatis---集合查询(一对多)

    这里的集合查询即一对多的数据联合查询.如一个用户多次登录的信息查询 要实现这样的联合查询需要在用户实体类中添加登录实体类的一个集合属性字段,表中不存在该字段. <resultMap id=&qu ...

  3. 关于 package.json 和 package-lock.json 文件说明

    package.json 在 Node.js 中,模块是一个库或框架,也是一个 Node.js 项目.Node.js 项目遵循模块化的架构,当我们创建了一个 Node.js 项目,意味着创建了一个模块 ...

  4. [转]全图形PPT设计指南

    三.什么时候使用 全图形PPT并不适用于所有时候,一般来说,我们在以下场合可以考虑使用:陈述一个故事.名人简介.产品介绍.读书笔记.心灵鸡汤.生活情趣等. 四.如何制作全图形PPT 全图形PPT的制作 ...

  5. Web 开发的未来:React、Falcor 和 ES6

    Web 开发的未来:React.Falcor 和 ES6 Widen是一家数字资产管理解决方案提供商.目前,其技术栈还非常传统,包括服务器端的Java.浏览器端的AngularJS.提供REST AP ...

  6. 什么是App加壳,以及App加壳的利与弊

    非著名程序员涩郎 非著名程序员,字耿左直右,号涩郎,爱搞机,爱编程,是爬行在移动互联网中的一名码匠!个人微信号:loonggg,微博:涩郎,专注于移动互联网的开发和研究,本号致力于分享IT技术和程序猿 ...

  7. Python3.7在win10下安装PyAudio库以及实现音频的录制与播放

    Python3.7 无法安装pyaudio 度娘的结果基本都是这个,pip install pyaudio.....然而十有八九你的电脑不买账,会报错. 报错信息: running install r ...

  8. Python全栈工程师(字符串/序列)

    ParisGabriel     Python 入门基础       字符串:str用来记录文本信息字符串的表示方式:在非注释中凡是用引号括起来的部分都是字符串‘’ 单引号“” 双引号''' ''' ...

  9. Visual C++ 图像处理类库CxImage源代码

    说明:VC++ 图像处理类库CxImage源代码,CxImage是一个可以用于MFC 的C++类,可以打开,保存,显示,转换各种格式的图像文件,比如BMP, JPEG, GIF, PNG, TIFF, ...

  10. HDU 4763 Theme Section ( KMP next函数应用 )

    设串为str, 串长为len. 对整个串求一遍next函数,从串结尾开始顺着next函数往前找<=len/3的最长串,假设串长为ans,由于next的性质,所以找到的串肯定满足E……E这种形式, ...