[ACM] HDU 1400 Mondriaan's Dream (State compression, 2-width, 1-rectangle full)

Source: Internet
Author: User
Tags integer numbers

Mondriaan's dream

Time Limit: 20000/10000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 783 accepted submission (s): 506


Problem description

 

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 file 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 into rical tilings multiple times.


 

Sample Input

 

1 21 31 42 22 32 42 114 110 0


 

Sample output

 

10123514451205


 

Source

 

Universities of Ulm Local contest 2000

 

Solution:

How many methods can be used to hold a project. Because the length, width, and maximum size are 11, the status can be compressed.

Start from the first line.

DP [I] [J] is defined as the number of methods in which the status of row I is J.

The small rectangle is represented in the 01 state. The small rectangle is composed of two squares. For the small rectangle that is placed horizontally, the left and right squares are represented by 11. For the vertical small rectangle, the upper and lower squares are represented by 01 respectively.

The status S2 of row I is related to the status S1.

S1 and S2 meet two conditions:

1. S1 | the binary number obtained by S2 is 1 for each bit, because 0 | 1 must be 1 for vertical columns, 11 for horizontal columns, and 11 for phase or also.

2. The number of consecutive numbers obtained from S1 and S2 is an even number. Note that 0 is also an even number. You can view the image and observe it.

 

Errors in this question:

1.

It is incorrect to obtain the I-th or 1 of a number x binary, and use if (X & (1 <I) = 1, set the binary number of X to 0 except the I-th bit. the I-th bit is 0 or 1 through & 1, but the obtained number is not necessarily 1, is a multiple of 2, such as 0010 or 0100.

2.

Determine whether each bit of a number x binary is equal to 1. If there is M bit, use if (x = 1 <m)-1) directly ), without the judgment of each digit, the former is more efficient.

Code:

 

#include <iostream>#include <stdlib.h>#include <stdio.h>#include <vector>#include <algorithm>#include <string.h>using namespace std;#define ll long longll dp[12][1<<12];//dp[i][j]表示第i行状态为j有多少种方法int n,m;bool ok(int s1,int s2){    int temp=s1|s2;//两个状态或运算每一位都必须是1    if(temp!=(1<<m)-1)        return false;    int cnt=0;    temp=s1&s2;//两个状态且运算,必须连续的1都是偶数个    for(int i=0;i<m;i++)    {        if((temp&(1<<i)))//第i位是1            cnt++;        else        {            if(cnt&1)                return false;        }    }    if(cnt&1)        return false;    return true;}void solve(){    memset(dp,0,sizeof(dp));    int maxd=1<<m;    for(int i=0;i<maxd;i++)//铺第一行        if(ok(maxd-1,i))            dp[1][i]++;    for(int i=2;i<=n;i++)//铺第i行    {        for(int j=0;j<maxd;j++)        {            for(int k=0;k<maxd;k++)                if(ok(j,k))                    dp[i][j]+=dp[i-1][k];        }    }    ll ans=0;    ans+=dp[n][maxd-1];//最后一行肯定都是1    printf("%I64d\n",ans);}int main(){    while(scanf("%d%d",&n,&m)!=EOF)    {        if(!n||!m)            break;        if(n*m&1)//小方块的个数为奇数个,肯定不能铺满        {            printf("0\n");            continue;        }        if(n<m)            n=n^m,m=n^m,n=n^m;        solve();    }    return 0;}


 

 

[ACM] HDU 1400 Mondriaan's Dream (State compression, 2-width, 1-rectangle full)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.