问题:

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 2
1 3
1 4
2 2
2 3
2 4
2 11
4 11
0 0

Sample Output

1
0
1
2
3
5
144
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的思想来解决,过几天再试一试。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
long long dp[<<][];int n,m;
bool ok[<<];
void getok()
{
for(int i=;i<(<<);i++){
ok[i]=true;
for(int j=;j<;j++)
if(i&<<j){
int cnt=;
while(i&(<<(j+))) cnt++,j++;
if(cnt&){ok[i]=false ;break;}
}
}
}
bool check(int x,int y)
{
if((x|y)!=((<<m)-)) return false; //上下不能同时为0
return ok[x&y];//上下同时为1要满足连续时为偶。
}
int main()
{
int i,j,k;
getok();
while(~scanf("%d%d",&n,&m)){
if(n==&&m==) return ;
memset(dp,,sizeof(dp));
dp[(<<m)-][]=;
for(i=;i<=n;i++)
for(k=;k<(<<m);k++)
for(j=;j<(<<m);j++)
if(check(k,j)) dp[j][i]+=dp[k][i-];
printf("%lld\n",dp[(<<m)-][n]);
}
return ;
}

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. Ubuntu输入su命令提示认证失败的解决办法

    Ubuntu安装后,root用户默认是被锁定了的,不允许登录,也不允许执行"su命令到root".对于桌面用户而言,这样可以提高安全性.但对于服务器可以设置成允许"su命 ...

  2. requirejs源码分析: config中shim

    shim处理的源码: //Merge shim                 if (cfg.shim) {                     eachProp(cfg.shim, funct ...

  3. jQuery:自学笔记(4)——事件与事件对象

    jQuery:自学笔记(4)——事件与事件对象 jQuery中的事件 什么是事件 所谓事件,就是被对象识别的操作,即操作对象队环境变化的感知和反应,例如单击按钮或者敲击键盘上的按键. 所谓事件流,是指 ...

  4. RabbitMQ队列/Redis缓存

    一.RabbitMQ队列 RabbitMQ安装(Centos7安装):1.安装依赖:yum install socat (不安装会报错)2.下载rpm包:wget http://www.rabbitm ...

  5. C# 学习黑马.Net视频教程,大文件拷贝

    设计器代码: namespace 大文件拷贝 { partial class Form1 { /// <summary> /// 必需的设计器变量. /// </summary> ...

  6. $Android制作和使用Nine-Patch图片

    Nine-Patch图片是一种经过特殊处理的png图片,能够指定图片的哪些区域可以被拉伸而哪些区域不可以. (一)普通图片被拉伸时的缺陷 有如下xml文件,其中子LinearLayout的背景图片设置 ...

  7. 【Tech】POI标签分类

    寒假老板给的任务,让我重现这个实验http://www.liuhaihua.cn/archives/15565.html.自己就随便试了下,用的都是比较经典(lao)的算法和知识,记录一下. 一.从网 ...

  8. 【转载】如何简单地理解Python中的if __name__ == '__main__'

    原帖:https://blog.csdn.net/yjk13703623757/article/details/77918633 通俗的理解__name__ == '__main__':假如你叫小明. ...

  9. JSP的动态Include的静态Include

    1. 静态导入示例 先总结: 1:静态include是把被引入的文件拼接到本页面中,再做为一个整体来编译,返回结果给客户端. 动态include是分别编译本页面和被引入的页面,再把结果合成一个html ...

  10. streambase log(log4j和logback)

    需要注意的是:当streambase servce 由window service 方式启动时,logback日志机制就不起作用了需要做下配置处理 https://support.tibco.com/ ...