问题:

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. JAVA使用DES加密解密

    在使用DES加密解密的时候,遇到了一些问题,廖记一下.如有哪位大神亲临留言指点,不胜感激. 先上代码: public DESUtil() { } //密码,长度要是8的倍数 注意此处为简单密码 简单应 ...

  2. Python 1 数据类型的操作

    一.数字(Number) 1.数学函数: 函数 返回值 ( 描述 ) abs(x) 返回数字的绝对值,如abs(-10) 返回 10 ceil(x) 返回数字的上入整数,如math.ceil(4.1) ...

  3. nginx负载均衡详情

    负载均衡是我们大流量网站要做的一个东西,下面我来给大家介绍在Nginx服务器上进行负载均衡配置方法,希望对有需要的同学有所帮助哦. 负载均衡 先来简单了解一下什么是负载均衡,单从字面上的意思来理解就可 ...

  4. pdoModel封装

    <?php /** * Created by PhpStorm. * User: Administrator * Date: 2017/7/24 * Time: 14:03 */ /** * 数 ...

  5. javascript重置(base层)(。。。。不完整)

    1.nextSibling浏览器兼容问题 <ul> <li id="item1"></li> <li id="item2&quo ...

  6. 5.4WEB服务器、应用程序服务器、HTTP服务器区别

    WEB服务器.应用程序服务器.HTTP服务器有何区别?IIS.Apache.Tomcat.Weblogic.WebSphere都各属于哪种服务器,这些问题困惑了很久,今天终于梳理清楚了:   Web服 ...

  7. Linux下解压分包文件zip(zip/z01/z02)

    分包压缩的zip文件不能被7z解压,且这种格式是Windows才能创建出来,在Linux下不会以这种方式去压包.下面是在Linux下处理这种文件的做法: 方法一: cat xx.z01 xx.zip ...

  8. UVA 796 连通图求桥

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=86270#problem/C#include<iostream> #inclu ...

  9. Oracle 数据库 INTERVAL DAY TO SECOND类型的使用

    INTERVAL DAY TO SECOND类型可以用来存储单位为天和秒的时间间隔.下面这条语句创建一个名为promotions的表,用来存储促销信息.promotions表包含了一个INTERVAL ...

  10. Luogu-3966 [TJOI2013]单词

    这道题应该是后缀数组的套路题啊,把单词连接起来,中间用没有出现过且互不相同的字符来分隔开,求一下\(height\)数组. 对于一个单词来说,设单词长\(len\),所在的后缀为\(i\),如果某后缀 ...