问题:

Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his 'toilet series' (where he had to use his toilet paper to draw on, for all of his paper was filled with squares and rectangles), he dreamt of filling a large rectangle with small rectangles of width 2 and height 1 in varying ways. 

Expert as he was in this material, he saw at a glance that he'll need a computer to calculate the number of ways to fill the large rectangle whose dimensions were integer values, as well. Help him, so that his dream won't turn into a nightmare!

Input

The input contains several test cases. Each test case is made up of two integer numbers: the height h and the width w of the large rectangle. Input is terminated by h=w=0. Otherwise, 1<=h,w<=11.

Output

For each test case, output the number of different ways the given rectangle can be filled with small rectangles of size 2 times 1. Assume the given large rectangle is oriented, i.e. count symmetrical tilings multiple times.

Sample Input

  1. 1 2
  2. 1 3
  3. 1 4
  4. 2 2
  5. 2 3
  6. 2 4
  7. 2 11
  8. 4 11
  9. 0 0

Sample Output

  1. 1
  2. 0
  3. 1
  4. 2
  5. 3
  6. 5
  7. 144
  8. 51205
  • 到底是这么想到用01和11的,是不是有一类这样处理的题型呢?
  • 这种题一般都有很多优化,包括很多预处理;或者运算方式的优化,比如尽量用位运算;或者有多个check时优先check哪一个。
  • 这里实现了两种情况避免了暴力check(据说还有很多优化,但是代码肯定没我现在这么短了,嘻嘻哒):

α,上(x)下(y)不能同时为0,则一定是x|y==(1<<m)-1。这样就不需要一位一位check

β,上(x)下(y)同为1的连续个数需要为偶,即z=x&y中1必须成连续偶数次出现(表示竖着放的部分)。我们预先处理各种z,避免了每次都check。

  • 这里的写法应该是归类为插头DP还是轮廓线DP还是其他?不清楚,我已经混了,或者本来就是交叉没有明确界限的。还可以用轮廓DP的思想来解决,过几天再试一试。
  1. #include<cstdio>
  2. #include<cstdlib>
  3. #include<iostream>
  4. #include<cstring>
  5. #include<cmath>
  6. using namespace std;
  7. long long dp[<<][];int n,m;
  8. bool ok[<<];
  9. void getok()
  10. {
  11. for(int i=;i<(<<);i++){
  12. ok[i]=true;
  13. for(int j=;j<;j++)
  14. if(i&<<j){
  15. int cnt=;
  16. while(i&(<<(j+))) cnt++,j++;
  17. if(cnt&){ok[i]=false ;break;}
  18. }
  19. }
  20. }
  21. bool check(int x,int y)
  22. {
  23. if((x|y)!=((<<m)-)) return false; //上下不能同时为0
  24. return ok[x&y];//上下同时为1要满足连续时为偶。
  25. }
  26. int main()
  27. {
  28. int i,j,k;
  29. getok();
  30. while(~scanf("%d%d",&n,&m)){
  31. if(n==&&m==) return ;
  32. memset(dp,,sizeof(dp));
  33. dp[(<<m)-][]=;
  34. for(i=;i<=n;i++)
  35. for(k=;k<(<<m);k++)
  36. for(j=;j<(<<m);j++)
  37. if(check(k,j)) dp[j][i]+=dp[k][i-];
  38. printf("%lld\n",dp[(<<m)-][n]);
  39. }
  40. return ;
  41. }

POJ2411Mondriaan's Dream(DP+状态压缩 or 插头DP)的更多相关文章

  1. POJ3254Corn Fields (状态压缩or插头DP)

    Description Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; ...

  2. HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)

    题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率,每包至多一张卡片,可能没有卡片.求需要买多少包才能拿到所以的N张卡片,求次数的期望. 析:期望DP,是很容易看出来的,然后由 ...

  3. HDU 1074 Doing Homework (dp+状态压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:学生要完成各科作业, 给出各科老师给出交作业的期限和学生完成该科所需时间, 如果逾期一 ...

  4. hdu_4352_XHXJ's LIS(数位DP+状态压缩)

    题目连接:hdu_4352_XHXJ's LIS 题意:这题花大篇篇幅来介绍电子科大的一个传奇学姐,最后几句话才是题意,这题意思就是给你一个LL范围内的区间,问你在这个区间内最长递增子序列长度恰为K的 ...

  5. hdu 4352 数位dp + 状态压缩

    XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. 【bzoj1076】[SCOI2008]奖励关 期望dp+状态压缩dp

    题目描述 你正在玩你最喜欢的电子游戏,并且刚刚进入一个奖励关.在这个奖励关里,系统将依次随机抛出k次宝物,每次你都可以选择吃或者不吃(必须在抛出下一个宝物之前做出选择,且现在决定不吃的宝物以后也不能再 ...

  7. hdu4336 Card Collector(概率DP,状态压缩)

    In your childhood, do you crazy for collecting the beautiful cards in the snacks? They said that, fo ...

  8. dp状态压缩

    dp状态压缩 动态规划本来就很抽象,状态的设定和状态的转移都不好把握,而状态压缩的动态规划解决的就是那种状态很多,不容易用一般的方法表示的动态规划问题,这个就更加的难于把握了.难点在于以下几个方面:状 ...

  9. 洛谷 1052 dp 状态压缩

    洛谷1052 dp 状态压缩 传送门 (https://www.luogu.org/problem/show?pid=1052#sub) 做完这道题之后,感觉涨了好多见识,以前做的好多状压题目都是将一 ...

随机推荐

  1. bacula 备份恢复

    一.数据恢复: 在bacula服务器执行: /opt/bacula/etc/ bconsole #进入交互窗口 *restore #输入restore恢复命令 Automatically select ...

  2. sql两张表关联更新字段

    记录,推荐使用第三种,个人觉得比较好 --第一种写法,之前一直用,没有在意 UPDATE sr_t_TCodeUserReciveCfg SET fUserID=a.fUserID,fImportFl ...

  3. Java系列之EJB 理解

    EJB = Enterprise Java Bean,它和JavaBean有本质的区别,最好不要将他们混淆起来,就像不要将Java和 Javascript混淆起来一样.EJB有3中类型:Session ...

  4. 【leetcode刷题笔记】4Sum

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  5. 教你在windows10环境下如何安装minepy并成功运行!

    在学习使用sklearn做单机特征工程这篇文章时,发现在计算互信息时from minepy import MINE代码运行出错ModuleNotFoundError: No module named ...

  6. Java Hibernate 5.3.x

    SchemeExport Hibernate根据实体类和实体类映射文件自动生成表结构. 示例代码: <?xml version='1.0' encoding='utf-8'?> <! ...

  7. 【转载】poj 1276 Cash Machine 【凑钱数的问题】【枚举思路 或者 多重背包解决】

    转载地址:http://m.blog.csdn.net/blog/u010489766/9229011 题目链接:http://poj.org/problem?id=1276 题意:机器里面共有n种面 ...

  8. 重定向【TLCL】

    >                 重定向标准输出 > ls-output.txt       清空或者创建一个新文件夹 >>                         ...

  9. 【FINAL】NOI

    我就是复习一下..根本就不是什么题解...谁也看不懂的... NOI2007 社交网络         最短路 货币兑换         斜率优化动态规划 项链工厂         线段树 生成树计数 ...

  10. 回溯法之n皇后问题

    package main import ( "fmt" "math" ) //判断第k行的某一列放置是否合法 func check(col []int, k i ...