-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCountingTilings.java
More file actions
53 lines (52 loc) · 1.72 KB
/
CountingTilings.java
File metadata and controls
53 lines (52 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.io.*;
import java.util.*;
class CountingTilings {
static long[][] dp = new long[1000+1][1<<11];
static long mod = 1000000007;
public static void main (String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s[] = br.readLine().split(" ");
int n = Integer.parseInt(s[0]);
int m = Integer.parseInt(s[1]);
for(long arr[] : dp) {
Arrays.fill(arr, -1l);
}
long ans = solve(1, 0, n, m);
System.out.println(ans);
}
public static long solve(int col, int mask, int n, int m) {
if(col == m+1) {
if(mask == 0) {
return 1l;
}
return 0l;
}
if(dp[col][mask] != -1) {
return dp[col][mask];
}
long ans = 0l;
ArrayList<Integer> nextMasks = new ArrayList<>();
generateNewMask(mask, 1, 0, nextMasks, n, m);
for(int nextMask : nextMasks) {
ans = (ans + solve(col+1, nextMask, n, m)) % mod;
}
return dp[col][mask] = ans;
}
public static void generateNewMask(int currentMask, int i, int nextMask, ArrayList<Integer> nextMasks, int n, int m) {
if(i == n+1) {
nextMasks.add(nextMask);
return;
}
if((currentMask & (1 << i)) != 0) {
generateNewMask(currentMask, i+1, nextMask, nextMasks, n, m); //when this ith place is occupied.
}
if(i != n) {
if((currentMask & (1 << i)) == 0 && (currentMask & (1 << i+1)) == 0)
generateNewMask(currentMask, i+2, nextMask, nextMasks, n, m);
}
if((currentMask & (1 << i)) == 0) {
generateNewMask(currentMask, i+1, nextMask + (1 << i), nextMasks, n, m);
}
}
}