题意:给一个2*n的矩形块,求把它分成k个连通块的方法数。(有公共边即视为联通)

思路:由于宽度只有2,于是很容易设计状态使问题满足阶段性以及无后效性。具体来说,令dp[i][j][0]和dp[i][j][1]表示把前i行分成j个连通块最后两个格子分别属于和不属于同一个连通块的方法数,于是有下面的状态转移方程:

dp[i][j][0]=dp[i-1][j-1][0..1]+dp[i-1][j][0]+dp[i-1][j][1]*2

dp[i][j][1]=dp[i-1][j-2][0..1]+dp[i-1][j][1]+dp[i-1][j-1][0..1]*2

  1. #pragma comment(linker, "/STACK:10240000,10240000")
  2.  
  3. #include <iostream>
  4. #include <cstdio>
  5. #include <algorithm>
  6. #include <cstdlib>
  7. #include <cstring>
  8. #include <map>
  9. #include <queue>
  10. #include <deque>
  11. #include <cmath>
  12. #include <vector>
  13. #include <ctime>
  14. #include <cctype>
  15. #include <set>
  16. #include <bitset>
  17. #include <functional>
  18. #include <numeric>
  19. #include <stdexcept>
  20. #include <utility>
  21.  
  22. using namespace std;
  23.  
  24. #define mem0(a) memset(a, 0, sizeof(a))
  25. #define mem_1(a) memset(a, -1, sizeof(a))
  26. #define lson l, m, rt << 1
  27. #define rson m + 1, r, rt << 1 | 1
  28. #define rep_up0(a, b) for (int a = 0; a < (b); a++)
  29. #define rep_up1(a, b) for (int a = 1; a <= (b); a++)
  30. #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
  31. #define rep_down1(a, b) for (int a = b; a > 0; a--)
  32. #define all(a) (a).begin(), (a).end()
  33. #define lowbit(x) ((x) & (-(x)))
  34. #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
  35. #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
  36. #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
  37. #define pchr(a) putchar(a)
  38. #define pstr(a) printf("%s", a)
  39. #define sstr(a) scanf("%s", a)
  40. #define sint(a) scanf("%d", &a)
  41. #define sint2(a, b) scanf("%d%d", &a, &b)
  42. #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
  43. #define pint(a) printf("%d\n", a)
  44. #define test_print1(a) cout << "var1 = " << a << endl
  45. #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
  46. #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
  47. #define mp(a, b) make_pair(a, b)
  48. #define pb(a) push_back(a)
  49.  
  50. typedef unsigned int uint;
  51. typedef long long LL;
  52. typedef pair<int, int> pii;
  53. typedef vector<int> vi;
  54.  
  55. const int dx[] = {, , -, , , , -, -};
  56. const int dy[] = {-, , , , , -, , - };
  57. const int maxn = 1e3 + ;
  58. const int md = ;
  59. const int inf = 1e9 + ;
  60. const LL inf_L = 1e18 + ;
  61. const double pi = acos(-1.0);
  62. const double eps = 1e-;
  63.  
  64. template<class T>T gcd(T a, T b){return b==?a:gcd(b,a%b);}
  65. template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
  66. template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
  67. template<class T>T condition(bool f, T a, T b){return f?a:b;}
  68. template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
  69. int make_id(int x, int y, int n) { return x * n + y; }
  70.  
  71. int dp[][][];
  72.  
  73. void init() {
  74. dp[][][] = ;
  75. dp[][][] = ;
  76. for (int i = ; i <= ; i ++) {
  77. rep_up1(j, * i) {
  78. dp[i][j][] = dp[i - ][j][] + dp[i - ][j][] * + dp[i - ][j - ][] + dp[i - ][j - ][];
  79. dp[i][j][] = dp[i - ][j][] + dp[i - ][j - ][] * + dp[i - ][j - ][] * ;
  80. if (j > ) dp[i][j][] += dp[i - ][j - ][] + dp[i - ][j - ][];
  81. dp[i][j][] %= md;
  82. dp[i][j][] %= md;
  83. }
  84. }
  85. }
  86.  
  87. int main() {
  88. //freopen("in.txt", "r", stdin);
  89. init();
  90. int T;
  91. cin >> T;
  92. rep_up0(cas, T) {
  93. int n, k;
  94. cin >> n >> k;
  95. cout << (dp[n][k][] + dp[n][k][]) % md << endl;
  96. }
  97. return ;
  98. }

[hdu4301]DP的更多相关文章

  1. BZOJ 1911: [Apio2010]特别行动队 [斜率优化DP]

    1911: [Apio2010]特别行动队 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 4142  Solved: 1964[Submit][Statu ...

  2. 2013 Asia Changsha Regional Contest---Josephina and RPG(DP)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4800 Problem Description A role-playing game (RPG and ...

  3. AEAI DP V3.7.0 发布,开源综合应用开发平台

    1  升级说明 AEAI DP 3.7版本是AEAI DP一个里程碑版本,基于JDK1.7开发,在本版本中新增支持Rest服务开发机制(默认支持WebService服务开发机制),且支持WS服务.RS ...

  4. AEAI DP V3.6.0 升级说明,开源综合应用开发平台

    AEAI DP综合应用开发平台是一款扩展开发工具,专门用于开发MIS类的Java Web应用,本次发版的AEAI DP_v3.6.0版本为AEAI DP _v3.5.0版本的升级版本,该产品现已开源并 ...

  5. BZOJ 1597: [Usaco2008 Mar]土地购买 [斜率优化DP]

    1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4026  Solved: 1473[Submit] ...

  6. [斜率优化DP]【学习笔记】【更新中】

    参考资料: 1.元旦集训的课件已经很好了 http://files.cnblogs.com/files/candy99/dp.pdf 2.http://www.cnblogs.com/MashiroS ...

  7. BZOJ 1010: [HNOI2008]玩具装箱toy [DP 斜率优化]

    1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 9812  Solved: 3978[Submit][St ...

  8. px、dp和sp,这些单位有什么区别?

    DP 这个是最常用但也最难理解的尺寸单位.它与“像素密度”密切相关,所以 首先我们解释一下什么是像素密度.假设有一部手机,屏幕的物理尺寸为1.5英寸x2英寸,屏幕分辨率为240x320,则我们可以计算 ...

  9. android px转换为dip/dp

    /** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public int dipTopx(Context context, float dpValue) { final floa ...

随机推荐

  1. Python - 关于带参数的装饰器的理解

    [原创]转载请注明作者Johnthegreat和本文链接 关于装饰器的理解,特别像<盗梦空间>中的进入梦境和从梦境出来的过程,一层一层的深入梦境,然后又一层一层的返回,被带入梦境的是被装饰 ...

  2. <cstring>中常用的两个函数memset()和memcpy()

    <cstring>是c++对c中的<string.h>进行了重写,这两个头文件中的函数用法是一样的,所以在用的时候包含哪个头文件都行.下面介绍一下 <cstring> ...

  3. php静态变量的销毁

    什么都不说,先上代码: public function _childrenids($data,$cate_id,$clear=false) { static $arr = array(); if ($ ...

  4. $_FILES上传错误类型

    $_FILES['file']['error']其值为 0,没有错误发生,文件上传成功. 其值为 1,上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值. 其值 ...

  5. Cannot find libcrypto in Ubuntu

    https://stackoverflow.com/questions/13811889/cannot-find-libcrypto-in-ubuntu sudo apt-get install li ...

  6. java中Runnable和Callable的区别

    文章目录 运行机制 返回值的不同 Exception处理 java中Runnable和Callable的区别 在java的多线程开发中Runnable一直以来都是多线程的核心,而Callable是ja ...

  7. C6 C7的开机启动流程

    C6开机启动流程 1.内核引导,加电自检(通电后检查内核):检查bios的配置,检测硬件 装好系统之后才会进行以下内容 MBR 引导 (3.2.1...) GRUB菜单 (选择不同的系统)(按e,进入 ...

  8. HTML JavaScript 基础(上)

    一.初识JavaScript JavaScript 和 Java什么关系? 半毛线关系都没有,只是名字有点重合而已. JavaScript 和python.C#.Java.Ruby一样,都是一门独立的 ...

  9. php beast windows编译教程

    git clone https://github.com/Microsoft/php-sdk-binary-tools.git c:\php-sdk cd c:\php-sdk git checkou ...

  10. SQL 文件导入数据库

    1.首先通过 xshell 连接数据库服务器,执行命令 mysql -u root -p 命令,按照提示输入密码,连接上数据库 2.在连接终端上执行命令 create database JD_Mode ...