传送门

Harry And Magic Box

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

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.

官方题解:

  1. 1002 Harry And Magic Box
  2. dp题,我们一行一行的考虑。dp[i][j],表示前i行,都满足了每一行至少有一个宝石的条件,而只有j列满足了有宝石的条件的情况有多少种。枚举第i+1行放的宝石数k,这k个当中有t个是放在没有宝石的列上的,那么我们可以得到转移方程:
  3. dp[i+1][j+t]+=dp[i][j]*c[m-j][t]*c[j][k-t],其中c[x][y],意为在x个不同元素中无序地选出y个元素的所有组合的个数。
  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdlib>
  4. #include<cstdio>
  5. #include<algorithm>
  6. #include<cmath>
  7. #include<queue>
  8. #include<map>
  9. #include<set>
  10. #include<string>
  11.  
  12. #define N 55
  13. #define M 10
  14. #define mod 1000000007
  15. //#define p 10000007
  16. #define mod2 1000000000
  17. #define ll long long
  18. #define LL long long
  19. #define eps 1e-9
  20. #define maxi(a,b) (a)>(b)? (a) : (b)
  21. #define mini(a,b) (a)<(b)? (a) : (b)
  22.  
  23. using namespace std;
  24.  
  25. ll n,m;
  26. ll dp[N][N];
  27. ll ans;
  28. ll c[N][N];
  29. ll sum[N][N];
  30.  
  31. void ini1()
  32. {
  33. memset(c,,sizeof(c));
  34. memset(sum,,sizeof(sum));
  35. int i,j;
  36. for(i=;i<=N-;i++){
  37. c[i][]=c[i][i]=;
  38. }
  39. for(i=;i<=N-;i++){
  40. for(j=;j<i;j++){
  41. c[i][j]=(c[i-][j-]+c[i-][j])%mod;
  42. }
  43. }
  44. //for(i=1;i<=M-5;i++){
  45. // for(j=0;j<=i;j++){
  46. // printf(" i=%d j=%d c=%I64d\n",i,j,c[i][j]);
  47. // }
  48. //}
  49. for(i=;i<=N-;i++){
  50. sum[i][]=;
  51. for(j=;j<=i;j++){
  52. sum[i][j]=(sum[i][j-]+c[i][j])%mod;
  53. }
  54. }
  55. }
  56.  
  57. void ini()
  58. {
  59. memset(dp,,sizeof(dp));
  60. ans=;
  61. dp[][]=;
  62. }
  63.  
  64. void solve()
  65. {
  66. int i,j,k,o;
  67. i=;
  68. for(j=;j<=m;j++){
  69. dp[i][j]=c[m][j];
  70. }
  71. for(i=;i<=n;i++){
  72. for(j=;j<=m;j++){
  73. dp[i][j]=( dp[i-][j]*(sum[j][j]-) )%mod;
  74. for(k=;k<j;k++){
  75. o=j-k;
  76. dp[i][j]=(dp[i][j]+( ( dp[i-][k]*(sum[k][k]) ) %mod ) * (c[m-k][o]) )%mod;
  77. }
  78. }
  79. }
  80. }
  81.  
  82. void out()
  83. {
  84. //int i,j;
  85.  
  86. //for(i=1;i<=n;i++){
  87. // for(j=1;j<=m;j++){
  88. // printf(" i=%d j=%d dp=%I64d\n",i,j,dp[i][j]);
  89. // }
  90. // }
  91. ans=(dp[n][m])%mod;
  92. printf("%I64d\n",ans);
  93. }
  94.  
  95. int main()
  96. {
  97. ini1();
  98. // freopen("data.in","r",stdin);
  99. //freopen("data.out","w",stdout);
  100. //scanf("%d",&T);
  101. //for(int ccnt=1;ccnt<=T;ccnt++)
  102. // while(T--)
  103. while(scanf("%I64d%I64d",&n,&m)!=EOF)
  104. {
  105. ini();
  106. solve();
  107. out();
  108. }
  109.  
  110. return ;
  111. }

BestCoder Round #25 1002 Harry And Magic Box [dp]的更多相关文章

  1. 暴力+降复杂度 BestCoder Round #39 1002 Mutiple

    题目传送门 /* 设一个b[]来保存每一个a[]的质因数的id,从后往前每一次更新质因数的id, 若没有,默认加0,nlogn复杂度: 我用暴力竟然水过去了:) */ #include <cst ...

  2. 矩阵快速幂---BestCoder Round#8 1002

    当要求递推数列的第n项且n很大时,怎么快速求得第n项呢?可以用矩阵快速幂来加速计算.我们可以用矩阵来表示数列递推公式比如fibonacci数列 可以表示为 [f(n)   f(n-1)] = [f(n ...

  3. 贪心/二分查找 BestCoder Round #43 1002 pog loves szh II

    题目传送门 /* 贪心/二分查找:首先对ai%=p,然后sort,这样的话就有序能使用二分查找.贪心的思想是每次找到一个aj使得和为p-1(如果有的话) 当然有可能两个数和超过p,那么an的值最优,每 ...

  4. Manacher BestCoder Round #49 ($) 1002 Three Palindromes

    题目传送门 /* Manacher:该算法能求最长回文串,思路时依据回文半径p数组找到第一个和第三个会文串,然后暴力枚举判断是否存在中间的回文串 另外,在原字符串没啥用时可以直接覆盖,省去一个数组空间 ...

  5. 二分图判定+点染色/并查集 BestCoder Round #48 ($) 1002 wyh2000 and pupil

    题目传送门 /* 二分图判定+点染色:因为有很多联通块,要对所有点二分图匹配,若不能,存在点是无法分配的,no 每一次二分图匹配时,将点多的集合加大最后第一个集合去 注意:n <= 1,no,两 ...

  6. hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]

    传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131 ...

  7. BestCoder Round #56 1002 Clarke and problem 1003 Clarke and puzzle (dp,二维bit或线段树)

    今天第二次做BC,不习惯hdu的oj,CE过2次... 1002 Clarke and problem 和Codeforces Round #319 (Div. 2) B Modulo Sum思路差不 ...

  8. BestCoder Round #92 1002 Count the Sheep —— 枚举+技巧

    题目链接:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=748&pid=1002 题解: 做题的时候只是想到 ...

  9. BestCoder Round #80 1002

    HDU 5666 Segment 题意:给你条斜率为-1,常数项为q(q为质数)的直线,连接原点与直线上整数格点,问你在有多少个格点在形成的无数个三角形内,而不在线段上,结果对P取模. 思路:best ...

随机推荐

  1. Objective-C相关Category的收集(更新)

    Categories是给你得不到源码的classes增加功能的一种方法.这个页面收集一些相关的Category,并且持续更新,你可以订阅关注.作者是Fille ?str?m,是@ IMGNRY的联合创 ...

  2. IIS应用程序池"启用32位"导致服务不可用的503错误

    原来运行正常的站点,突然不正常了,出现503错误.查看操作系统的日志查看器显示: 由于配置问题,无法加载模块 DLL“C:\Program Files (x86)\IIS\Asp.Net Core M ...

  3. webuploader项目中多图片上传实例

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  4. html制作简单框架网页 实现自己的音乐驿站 操作步骤及源文件下载 (播放功能限mp3文件)

    使用HTML语言来设计制作 Hyper Text Markup Language 超文本标记语言 这门语言的特点就是标记,就是把所有的命令单词用<>标记起来,就可以发挥作用 还有一个特点, ...

  5. 三段式fsm

    1.状态转移的always中CS,同步ouput的always中NS. 2.3段fsm vs 2段fsm:output逻辑是组合逻辑和同步时序逻辑(消除里不稳的和毛刺). 3.3段fsm vs 1段f ...

  6. Python包,json&pickle,time&datetime,random模块

    补充内容: 解决模块循环导入的两种方法:(不得已而为之,表示程序结构不够严谨) 将导入模块语句放在文件最下方 保证语句导入之前函数内代码能够被执行 将导入语句放进函数体内 使其不影响整个函数的运行 包 ...

  7. Python 基本数据类型 (一) - 整数

    帮助文档网址: https://docs.python.org/3.7/tutorial/introduction.html 待补充

  8. HDU 4003 Find Metal Mineral

    这个题是POJ1849的加强版. 先说一个很重要的结论,下面两种方法都是从这个结论出发的. 一个人从起点遍历一颗树,如果最终要回到起点,走过的最小权值就是整棵树的权值的2倍. 而且K个人的情况也是如此 ...

  9. wei UI使用

    1.前言 通过前面系列文章的学习与讲解,相信大家已经对微信的开发有了一个全新的认识.后端基本能够基于盛派的第三方sdk搞定大部分事宜,剩下的就是前端了.关于手机端的浏览器的兼容性问题相信一直是开发者们 ...

  10. 本机机器ssh docker容器

    https://blog.csdn.net/u010324465/article/details/77184506 1.在docker中安装openssh-server 2.sudo /etc/ini ...