hdu 5155 Harry And Magic Box BC#25比赛1002
来源:互联网 发布:娶马来西亚老婆知乎 编辑:程序博客网 时间:2024/05/18 11:27
Harry And Magic Box
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 237 Accepted Submission(s): 120
Problem DescriptionOne 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.
InputThere 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 .
OutputFor each test case, just output one line that contains an integer indicating the answer.
Sample Input1 12 22 3
Sample Output1725HintThere are 7 possible arrangements for the second test case.They are:1111111011011011011101101001Assume that a grids is '1' when it contains a jewel otherwise not.
11。。虽然我在比赛时想到dp求解,我知道了初值dp[i][1]=dp[1][i]=1;但是我是没有找到状态转移方程。
网上看到这样一种解答,dp[i][j]从dp[i-k][j-1]转移而来。然后枚举前j-1列中k行为0的情况,并计数就好。#include<iostream>#include<cstdio>#include<cstdlib>#include<vector>#include<algorithm>#include<stack>#include<queue>#include<set>#include<map>#include<string>#include<cstring>#include<cmath>#include<fstream>typedef long long ll;#define cl(a,n) memset(a,n,sizeof a )using namespace std;/**************************** By:七柳先森丶 Date: 1.1 2015*****************************/const int mx=55;//感觉一道比较难的DP题。#define mod 1000000007ll c[mx][mx],dp[mx][mx],t[mx]; void work(){//求组合数。 for(int i=0; i<=50 ; 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;}t[0]=1;for(int i=1;i<=50;i++){t[i]=(2*t[i-1])% mod;}for(int i=1;i<=50;i++) dp[i][1]=dp[1][i]=1;//dp[i][j]是由dp[i-k][j-1]转移来的,是一种计数方式,只要不重复即可。 for(int i=2;i<=50;i++){for(int j=2;j<=50;j++){dp[i][j]=(dp[i][j-1]*(t[i]-1))%mod;for(int k=1;k<i;k++)dp[i][j]=(dp[i][j]+c[i][k]*t[i-k] % mod * dp[i-k][j-1] % mod)%mod; }} }int main(){int n,m;work();while(scanf("%d%d",&n,&m)==2){printf("%d\n",(int)dp[n][m]);} return 0;}
Harry And Magic Box
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 237 Accepted Submission(s): 120
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 .
For each test case,there are two integers n and m indicating the size of the box.
Output
For each test case, just output one line that contains an integer indicating the answer.
Sample Input
1 12 22 3
Sample Output
1725HintThere are 7 possible arrangements for the second test case.They are:1111111011011011011101101001Assume that a grids is '1' when it contains a jewel otherwise not.
#include<iostream>#include<cstdio>#include<cstdlib>#include<vector>#include<algorithm>#include<stack>#include<queue>#include<set>#include<map>#include<string>#include<cstring>#include<cmath>#include<fstream>typedef long long ll;#define cl(a,n) memset(a,n,sizeof a )using namespace std;/**************************** By:七柳先森丶 Date: 1.1 2015*****************************/const int mx=55;//感觉一道比较难的DP题。#define mod 1000000007ll c[mx][mx],dp[mx][mx],t[mx]; void work(){//求组合数。 for(int i=0; i<=50 ; 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;}t[0]=1;for(int i=1;i<=50;i++){t[i]=(2*t[i-1])% mod;}for(int i=1;i<=50;i++) dp[i][1]=dp[1][i]=1;//dp[i][j]是由dp[i-k][j-1]转移来的,是一种计数方式,只要不重复即可。 for(int i=2;i<=50;i++){for(int j=2;j<=50;j++){dp[i][j]=(dp[i][j-1]*(t[i]-1))%mod;for(int k=1;k<i;k++)dp[i][j]=(dp[i][j]+c[i][k]*t[i-k] % mod * dp[i-k][j-1] % mod)%mod; }} }int main(){int n,m;work();while(scanf("%d%d",&n,&m)==2){printf("%d\n",(int)dp[n][m]);} return 0;}
0 0
- hdu 5155 Harry And Magic Box BC#25比赛1002
- 【CUGBACM15级BC第25场 B】hdu 5155 Harry And Magic Box
- hdu 5155 Harry And Magic Box(DP)
- hdu 5155 Harry And Magic Box
- HDU - 5155 Harry And Magic Box
- HDU 5155 Harry And Magic Box
- HDU 5155 Harry And Magic Box --DP
- Hdu 5155 Harry And Magic Box
- 【HDU】5155 Harry And Magic Box 【容斥原理】
- HDU 5155 Harry And Magic Box DP题
- HDU 5155 Harry And Magic Box(组合+容斥)
- hdu 5155 Harry And Magic Box(递推)
- HDU-5155 Harry And Magic Box(DP,组合数)
- Harry And Magic Box
- HDOJ 5155 Harry And Magic Box DP
- [BestCoder Round #25 1002]Harry And Magic Box 解题报告
- hdu5155---Harry And Magic Box
- HDU5155 Harry And Magic Box
- Paxos算法之旅(四)zookeeper代码解析
- CoreOS实践指南(二):架设CoreOS集群
- mongodb和java的简单链接
- Windows下Jekyll配置过程详解
- Curator源码解析(二)初始化和启动分析
- hdu 5155 Harry And Magic Box BC#25比赛1002
- 5类内部排序----交换排序之冒泡排序
- Example of Using Google Maps Service and GPS in Android
- linux如何安装Jdk
- JAVA Garbage Collection
- Sql Server视图
- Android笔记----Android应用资源
- 不说再见-to全体学习数据结构同学
- APUE 学习笔记(第三章)