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
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
1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 using namespace std; 5 typedef long long LL; 6 const int maxn = 1e6 + 10; 7 const int INF = 0x7fffffff; 8 const int mod = 100000000; 9 int n, m, tot; 10 LL dp[15][1 << 15]; 11 int init(int x) { 12 for (int i = 0 ; i < tot ;) { 13 if (x & (1 << i)) { 14 if (i == m - 1) return 0; 15 if (x & (1 << (i + 1))) i += 2; 16 else return 0; 17 } else i++; 18 } 19 return 1; 20 } 21 int check(int x, int y) { 22 for (int i = 0 ; i < m ;) { 23 if (x & (1 << i)) { 24 if (y & (1 << i)) { 25 if (i == m - 1 || !(x & 1 << (i + 1)) || !(y & 1 << (i + 1))) return 0; 26 else i += 2; 27 } else i++; 28 } else { 29 if (y & (1 << i)) i++; 30 else return 0; 31 } 32 } 33 return 1; 34 } 35 int main() { 36 while(scanf("%d%d", &n, &m) != EOF) { 37 if (n == 0 && m == 0 ) break; 38 memset(dp, 0, sizeof(dp)); 39 if (n & 1 && m & 1) { 40 printf("0 "); 41 continue; 42 } 43 if (n < m) swap(n, m); 44 tot = 1 << m; 45 for (int i = 0 ; i < tot ; i++) 46 if (init(i)) dp[1][i] = 1; 47 for (int i = 2 ; i <= n ; i++) { 48 for (int j = 0 ; j < tot ; j++) { 49 for (int k = 0 ; k < tot ; k++) { 50 if (check(j, k)) dp[i][j] += dp[i - 1][k]; 51 } 52 } 53 } 54 printf("%lld ", dp[n][tot - 1]); 55 } 56 return 0; 57 }