^m
路漫漫

Mondriaan's Dream

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

Source

题意:n*m方格用1*2填满,问方案数

题目链接

第一次状压dp按格来推

这题代码写得特别诡异反正过了就行懒得改了

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<cstring>
 6 #include<vector>
 7 
 8 using namespace std;
 9 
10 const int MAXN = 15;
11 int n, m;
12 long long f[MAXN][MAXN][1 << MAXN];
13 long long an[MAXN][MAXN];
14 
15 template <typename tn> void read (tn & a) {
16     tn x = 0, f = 1;
17     char c = getchar();
18     while (c < '0' || c > '9'){ if (c == '-') f = -1; c = getchar(); }
19     while (c >= '0' && c <= '9'){ x = x * 10 + c - '0'; c = getchar(); }
20     a = f == 1 ? x : -x;
21 }
22 
23 long long dfs (int x, int y, int b) {
24     if (x == m) return (b == 0);
25     if (f[x][y][b]>=0) return f[x][y][b];
26     int x_, y_;
27     if (y == n - 1) {
28         y_ = 0;
29         x_ = x + 1;
30     } 
31     else {
32         x_ = x;
33         y_ = y + 1;
34     }
35     int M = 1 << y;
36     long long ans = dfs(x_, y_, b ^ M);
37     if (b & M) {
38         ans = dfs(x_, y_, b ^ M); 
39     } else {
40         ans = dfs(x_, y_, b ^ M);
41         int M_ = M << 1;
42         if (y + 1 == y_ && ((b & M_) == 0)) {
43            ans += dfs(x_, y_, b ^ M_);
44         }
45     }
46     return f[x][y][b] = ans;
47 }
48 
49 
50 void solve (int n, int m) {
51     memset(f, -1, sizeof(f));
52     an[n][m] = dfs(0, 0, 0);
53 }
54 
55 int main() {
56     read(n);
57     read(m);
58     while (n != 0 && m != 0) {
59         if (n < m) {
60             int t = n;
61             n = m;
62             m = t;
63         }
64         if (n % 2 != 0 && m % 2 != 0) {
65             printf("0\n");
66         }
67         else {
68             solve(n, m);
69             printf("%d\n", an[n][m]);
70         }
71         read(n);
72         read(m);
73     }
74     return 0;
75 }
View Code

 

posted on 2018-04-17 07:49  ^m  阅读(165)  评论(0编辑  收藏  举报