CodeForcesGym 100212E Long Dominoes

Long Dominoes

Time Limit: 1000ms

Memory Limit: 65536KB

This problem will be judged on CodeForcesGym. Original ID: 100212E
64-bit integer IO format: %I64d      Java class name: (Any)

Find the number of ways to tile an m*n rectangle with long dominoes -- 3*1 rectangles.

Each domino must be completely within the rectangle, dominoes must not overlap (of course, they may touch each other), each point of the rectangle must be covered.

Input

The input contains several cases. Each case stands two integers m and n (1 <= m <= 9, 1 <= n <= 30) in a single line. The input ends up with a case of m = n = 0.

Output

Output the number of ways to tile an m*n rectangle with long dominoes.

Sample Input

3 3
3 10
0 0

Sample Output

2
28

Source

Andrew Stankevich‘s Contest #4

Author

Andrew Stankevich

解题:状压dp

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const int maxn = 1<<18;
 5 LL dp[2][maxn];
 6 vector<int>g[maxn];
 7 bool tab[10][10];
 8 int stx[maxn],tot;
 9 void dfs(int row,int st,int n) {
10     if(row == n) {
11         int tst = 0;
12         for(int i = n-1; i >= 0; --i) {
13             tst <<= 2;
14             tst |= tab[i][1]|(tab[i][2]<<1);
15         }
16         g[tst].push_back(st);
17         stx[tot++] = tst;
18         stx[tot++] = st;
19         return;
20     }
21     if(!tab[row][0]) {
22         if(!tab[row][1] && !tab[row][2]) {
23             tab[row][0] = tab[row][1] = tab[row][2] = true;
24             dfs(row + 1,st,n);
25             tab[row][0] = tab[row][1] = tab[row][2] = false;
26         }
27         if(row + 3 > n || tab[row + 1][0] || tab[row + 2][0]) return;
28         tab[row + 2][0] = tab[row + 1][0] = tab[row][0] = true;
29         dfs(row + 3,st,n);
30         tab[row + 2][0] = tab[row + 1][0] = tab[row][0] = false;
31     } else dfs(row + 1,st,n);
32 }
33 void init(int st,int n) {
34     memset(tab,false,sizeof tab);
35     for(int i = 0,xst = st; i < n; ++i,xst >>= 2) {
36         int row = xst&3;
37         tab[i][0] = row&1;
38         tab[i][1] = (row>>1)&1;
39         if(row == 2) return;
40     }
41     dfs(0,st,n);
42 }
43 int main() {
44     freopen("dominoes.in","r",stdin);
45     freopen("dominoes.out","w",stdout);
46     int m,n;
47     scanf("%d%d",&m,&n);
48     for(int i = 0; i < (1<<(m + m)); ++i) init(i,m);
49     sort(stx,stx + tot);
50     tot = unique(stx,stx + tot) - stx;
51     int cur = dp[0][0] = 1;
52     for(int i = 1; i <= n; ++i) {
53         for(int j = 0; j < tot; ++j) {
54             for(int k = g[stx[j]].size()-1; k >= 0; --k)
55                 dp[cur][stx[j]] += dp[cur^1][g[stx[j]][k]];
56         }
57         cur ^= 1;
58         memset(dp[cur],0,sizeof dp[cur]);
59     }
60     printf("%I64d\n",dp[cur^1][0]);
61     return 0;
62 }

时间: 2024-11-10 07:29:54

CodeForcesGym 100212E Long Dominoes的相关文章

CodeForcesGym 100753E Change of Scenery

Change of Scenery Time Limit: 10000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. Original ID: 100753E64-bit integer IO format: %I64d      Java class name: (Any) 解题:最短路 1 #include <bits/stdc++.h> 2 using namespace std; 3 typ

CodeForcesGym 100753F Divisions

Divisions Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. Original ID: 100753F64-bit integer IO format: %I64d      Java class name: (Any) 解题:大数质因子分解 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef

CodeForcesGym 100753B Bounty Hunter II

Bounty Hunter II Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. Original ID: 100753B64-bit integer IO format: %I64d      Java class name: (Any) 解题:最小路径覆盖 1 #include <bits/stdc++.h> 2 using namespace std; 3 co

CodeForcesGym 100502E Opening Ceremony

Opening Ceremony Time Limit: 5000ms Memory Limit: 524288KB This problem will be judged on CodeForcesGym. Original ID: 100502E64-bit integer IO format: %I64d      Java class name: (Any) For the grand opening of the algorithmic games in NlogNsglow, a r

CodeForcesGym 100517B Bubble Sort

Bubble Sort Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. Original ID: 100517B64-bit integer IO format: %I64d      Java class name: (Any) 解题:我们发现假设当前位选择1,那么发现比1大的并且没有使用的有b个,那么当前为选1,后面就还有$2^{b-1}$种方式,所以贪心的选,只要

CodeForcesGym 100753A A Journey to Greece

A Journey to Greece Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. Original ID: 100753A64-bit integer IO format: %I64d      Java class name: (Any) 解题:状压dp 1 #include <bits/stdc++.h> 2 using namespace std; 3 t

CodeForcesGym 100502D Dice Game

Dice Game Time Limit: 1000ms Memory Limit: 524288KB This problem will be judged on CodeForcesGym. Original ID: 100502D64-bit integer IO format: %I64d      Java class name: (Any) Gunnar and Emma play a lot of board games at home, so they own many dice

codeforces 342D Xenia and Dominoes(状压dp+容斥)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud D. Xenia and Dominoes Xenia likes puzzles very much. She is especially fond of the puzzles that consist of domino pieces. Look at the picture that shows one of such puzzles. A puzzle is a 3 ×

CodeForcesGym 100502G Outing

Outing Time Limit: 1000ms Memory Limit: 524288KB This problem will be judged on CodeForcesGym. Original ID: 100502G64-bit integer IO format: %I64d      Java class name: (Any) Organising a group trip for the elderly can be a daunting task... Not least