Mondriaan's Dream
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 19226 | Accepted: 10946 |
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!
![](https://i-blog.csdnimg.cn/blog_migrate/91cd2a849209448a71d50fa577e5d5ed.jpeg)
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
![](https://i-blog.csdnimg.cn/blog_migrate/9275391b5c2fd4e3cb556e4ab10140b5.jpeg)
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
Source
参考算法竞赛进阶指南作者给的代码
/**
* @Author: H S-J
* @DateTime: 2018-04-18 18:07:09
* @Description:
*/
// #include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define MST(a, b) memset(a, b, sizeof(a));
#define CLR(a) MST(a, 0);
#define DBG(x) cout << '#' << " = " << (x) << endl;
#define ALL(x) x.begin(), x.end()
#define INS(x) inserter(x, x.begin())
#define lson(x) (x) << 1
#define rson(x) (x) << 1|1
#define pb push_back
#define rep(x, y, z) for (int x = y; x < z; ++x)
#define dwn(x, y, z) for (int x = y; x >= z; --x)
#define ech(x, y, z) for (auto x = z.begin(), y = z.end(); x != y; ++x)
#define lowbit(x) (x & (-x))
#define opr operator
#define _st first
#define _nd second
typedef long long LL;
typedef stringstream strstm;
const double PI = 3.1415926535898;
const double E = 2.718281828459;
LL dp[13][1 << 11];
bool in_s[1 << 11];
int n, m;
void init() {
int end = 1 << m;
rep(i, 0, end) {
bool odd = 0, zero = 0;
rep(j, 0, m) {
if(i >> j & 1) odd |= zero, zero = 0;
else zero ^= 1;
}
odd |= zero;
in_s[i] = odd ? 0 : 1;
}
}
LL solve() {
int end = 1 << m;
dp[0][0] = 1;
rep(i, 1, n+1) rep(j, 0, end) {
dp[i][j] = 0;
rep(k, 0, end) if((j&k)==0 && in_s[j|k]) {
dp[i][j] += dp[i-1][k];
}
}
return dp[n][0];
}
int main(int argc, char const *argv[]) {
ios::sync_with_stdio(0);cin.tie(0);
while(cin >> n >> m) {
if(n == 0 && m == 0) break;
init();
cout << solve() << endl;
}
return 0;
}