hdu 5155(递推)
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
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.
For each test case,there are two integers n and m indicating the size of the box. 0≤n,m≤50.
2 2
2 3
7
25
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.
如果 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(递推)的更多相关文章
- HDOJ(HDU).2044-2049 递推专题
HDOJ(HDU).2044-2049 递推专题 点我挑战题目 HDU.2044 题意分析 先考虑递推关系:从1到第n个格子的时候由多少种走法? 如图,当n为下方格子的时候,由于只能向右走,所以有2中 ...
- HDU 2842 (递推+矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...
- "红色病毒"问题 HDU 2065 递推+找循环节
题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=2065 递推类题目, 可以考虑用数学方法来做, 但是明显也可以有递推思维来理解. 递推的话基本就是状态 ...
- Children’s Queue HDU 1297 递推+大数
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1297 题目大意: 有n个同学, 站成一排, 要求 女生最少是两个站在一起, 问有多少种排列方式. 题 ...
- hdu 2044-2050 递推专题
总结一下做递推题的经验,一般都开成long long (别看项数少,随便就超了) 一般从第 i 项开始推其与前面项的关系(动态规划也是这样),而不是从第i 项推其与后面的项的关系. hdu2044:h ...
- ZOJ 3182 HDU 2842递推
ZOJ 3182 Nine Interlinks 题目大意:把一些带标号的环套到棍子上,标号为1的可以所以操作,标号i的根子在棍子上时,只有它标号比它小的换都不在棍子上,才能把标号为i+1的环,放在棍 ...
- hdu 2604 递推 矩阵快速幂
HDU 2604 Queuing (递推+矩阵快速幂) 这位作者讲的不错,可以看看他的 #include <cstdio> #include <iostream> #inclu ...
- hdu 4055 递推
转自:http://blog.csdn.net/shiqi_614/article/details/7983298 题意:由数字1到n组成的所有排列中,问满足题目所给的n-1个字符的排列有多少个,如果 ...
- HDU 3123-GCC(递推)
GCC Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Subm ...
随机推荐
- WPF的线程模型
原文:WPF的线程模型 WPF的线程模型 周银辉 谈到多线程,很多人对其可能都不太有好感,觉得麻烦与易出错.所以我们不排除有这样的情况:假设我对“多线程”.“异步”这些字眼潜意识 ...
- 1079: [SCOI2008]着色方案
链接 思路 首先是dp,如果直接用每个种颜色的剩余个数做状态的话,复杂度为5^15. 由于c<=5,所以用剩余数量的颜色的种类数做状态:f[a][b][c][d][e][last]表示剩余数量为 ...
- android的dmtracedump工具生成trace文件图片 'dot' 不是内部或外部命令,也不是可运行的程序 或批处理文件。
http://jingyan.baidu.com/article/c910274bfa6c1fcd361d2df7.html http://www.cnblogs.com/albert1017/p/3 ...
- ElasticSearch学习笔记(四)-- 分布式
1. 分布式介绍及cerebro cerebro插件 点击release下载 解压运行 访问9000端口,连接es的9200端口 2. 构建集群 新增一个节点 3. 副本与分片 再加入一个节点 4. ...
- linux 检测进程是否存在
1. 直接遍历/proc目录 int find_pid_by_name( char* pidname, pid_t *pidlist) { #define READ_BUF_SIZE 256 DIR ...
- WIN8、WIN7访问Windows Server 2003服务器的数据库速度很慢、远程速度很慢的解决方法
原因是微软在WIN7开始上加入了网络速度限制.在控制台执行以下命令即可解决: netsh interface tcp set global autotuninglevel=disabled
- Restful API实战
简介:随着移动互联网的发展,客户端层出不穷,app,web,微信端等等,而后端业务逻辑基于是一致的,如何做到业务逻辑“一次编写,随时接入”?答案是通过远程调用API,而目前比较火的方案是“Restfu ...
- HTML--留
1.html图像 <p> 这是个图像<img src=“\路径\” alt=“图像不显示不出来时代替图片” width=“1” height=“1” > </p ...
- CandyCrush 糖果传奇
1.unity自带触发事件 unity的每一个Collider对象都有类似OnMouseDown.OnMouseOver等事件.此事件是存在于MonoBehaviour脚本里的,而MonoBehavi ...
- 斯坦福大学CS231n简要笔记和课后作业
笔记目录: 1. CS231n--图像分类(KNN实现) 2. 待更新... 3. 4.