hdu 5155 Harry And Magic Box(递推)
2016-07-15 18:48
357 查看
Harry And Magic Box
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 625 Accepted Submission(s): 294
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
HintThere 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.
题意:有一个n*m的盒子,每行每列至少有一个1,问一共有多少种可能
思路:参考此人博客。
点击打开链接
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define mod 1000000007
#define N 55
long long dp
;
long long c
;
void init()
{
for(int i=1;i<N;i++)
{
c[i][0]=c[i][i]=1;
for(int j=1;j<i;j++)
{
c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod;
}
}
}
long long pow_mod(long long a,long long n)
{
long long ans=1;
while(n)
{
if(n&1) ans=ans*a%mod;
a=a*a%mod;
n>>=1;
}
return ans;
}
int main()
{
int n,m;
init();
while(~scanf("%d %d",&n,&m))
{
memset(dp,0,sizeof(dp));
for(int i=1;i<=m;i++)
dp[1][i]=1;
for(int i=1;i<=n;i++)
dp[i][1]=1;
for(int i=2;i<=n;i++)
{
for(int j=2;j<=m;j++)
{
dp[i][j]=dp[i][j-1]*(pow_mod(2,i)-1)%mod;
for(int k=1;k<=i;k++)
{
dp[i][j]=(dp[i][j]+dp[i-k][j-1]*c[i][k]%mod*pow_mod(2,i-k)%mod)%mod;
}
}
}
printf("%lld\n",dp
[m]);
}
return 0;
}
相关文章推荐
- IOS老工程pod适配ReactNative坑点
- 字典树(需要删除操作)——DNA Prefix ( LOJ 1224 )
- OpenCV 简介
- 【慢速学数据结构】查找树篇
- 僵尸进程和孤儿进程
- 浅析Context
- input 选择多个文件上传
- 【慢速学数据结构】散列篇
- NLog的配置和使用(一)
- 零散知识学习
- [输入输出 字符串] cf 697B Barnicle
- Android surfaceview详解
- javax.servlet.Filter
- Activity的创建过程分析
- excel 导出
- 基于Word文档的试卷单题拆分工具
- 在线任意进制转换工具 - aTool在线工具
- 一个简单的生产者与消费者的多线程例子(一):简单的框架
- Spring学习总结(14)——Spring10种常见异常解决方法
- Spring学习总结(14)——Spring10种常见异常解决方法