Mondriaan's Dream
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 11424 | Accepted: 6637 |
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!

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

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
题意:给个n*m的方格,放1*2的木板,求有多少种放满的情况。
题解:先用二进制求出满足一行的状态。相邻1必须是偶数,1表示放了木板。至于是横着还是竖着得根据下一行联合判断,想想都觉得很神奇的说。dp递推的条件是当前与上一行|满足都为1,并且&后仍然满足相邻1是偶数。注意dp数组开成LL类型。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
using namespace std;
#define LL __int64
LL dp[1<<11][12];
bool sta[1<<11];
int t,n,m,i,j;
bool pd(int x)
{
int k=0;
while (x)
{
if (x & 1) k++;
else
{
if (k & 1) return 0;
else k=0;
}
x>>=1;
}
if (k & 1) return 0;
else return 1;
}
int check(int x,int y)
{
if ((x | y)!=t-1) return 0;
return sta[x & y];
}
int main()
{
memset(sta,0,sizeof(sta));
t=1<<11;
for (i=0;i<t;i++)
if (pd(i))
sta[i]=1;
while (~scanf("%d%d",&n,&m) && (n+m))
{
memset(dp,0,sizeof(dp));
t=1<<m;
for (i=0;i<t;i++)
if (sta[i])
dp[i][0]=1;
for (int l=1;l<n;l++)
{
for (i=0;i<t;i++)
for (j=0;j<t;j++)
{
if (!check(i,j)) continue;
dp[i][l]+=dp[j][l-1];
}
}
cout<<dp[t-1][n-1]<<endl;
}
return 0;
}