poj 2411
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 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
状压dp,设dp[i][j]表示第i行的j状态下的方案总数,j是一个用十进制表示的01串。
用第i-1行的状态k转移,当且仅当j & k =1且j | k在二进制表示下必须每段连续的0有偶数个,这个先预处理出。转移方程: dp[i][j]+=dp[i-1][k]
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 11;
typedef long long ll;
int n,m;
ll dp[N+5][1<<N+5];
bool yes[1<<N+5];
int main(){
while(~scanf("%d%d",&n,&m)){
if(n==0 && m==0) break;
memset(yes,false,sizeof(yes));
for(register int i=0;i<1<<m;i++){ //预处理。
bool cnt=0,has_odd=0;
for(register int j=0;j<m;j++){
if(i>>j&1) has_odd|=cnt,cnt=0;
else cnt^=1;
}
if((cnt|has_odd)==1) yes[i]=0;
else yes[i]=1;
// cout<<i<<" "<<yes[i]<<endl;
}
dp[0][0]=1;
for(register int i=1;i<=n;i++)
for(register int j=0;j<1<<m;j++){
dp[i][j]=0;
for(register int k=0;k<1<<m;k++){
if((j&k)==0 && yes[j|k])
dp[i][j]+=dp[i-1][k];
}
}
printf("%I64d\n",dp[n][0]);
}
}