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 ;
}

最新文章

  1. Linux下安装Redis
  2. oracle数据库如何创建表空间,临时表空间
  3. 我的J2EE学习历程
  4. jquery submit()不执行
  5. objective c,copy, mutableCopy区别
  6. 在excel批量更改单元格类型后的批量刷新显示
  7. history对象
  8. 利用phantomjs模拟QQ自动登录
  9. (poj)3159 Candies
  10. 【Xamarin挖墙脚系列:常用的Mac 命令】
  11. swift 定位 根据定位到的经纬度转换城市名
  12. 【MAC】Mac下部分常用的小工具
  13. php 添加环境变量
  14. 数据库别名AS区别
  15. python flask 小项目
  16. ie6下a标签click事件无法触发加载iframe
  17. at com.mysql.jdbc.SQLError.createSQLException
  18. delphi webbrowser 常用方法示例
  19. Opencv 直方图比较
  20. 日历类Calendar

热门文章

  1. WPF里ItemsControl的分组实现
  2. W/System.err: at android.view.ViewConfiguration.get(ViewConfiguration.java:369)
  3. Android快速发布项目到jcenter详解
  4. ElasticSearch学习笔记(三)-- 查询
  5. java中多态的概念
  6. 每天一个Linux命令(8):chmod命令
  7. Python学习2,小程序
  8. Linux认知之旅【02 装个软件玩玩】!
  9. 【转载】Unity3D研究院transform.parent = parent坐标就乱了
  10. neutron floating ip 限速