blob: d8bd02bd60c932439d05e6ad023e2c7d50bb53e7 (
plain)
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
|
// 790. Domino and Tromino Tiling
#include "leetcode.h"
/*
* you have two types of tiles: a '2 x 1' domino shape and a tromino shape. you
* may rotate these shapes. given an integer 'n', return the number of ways to
* tile a '2 x n' board. since the answer may be very large, return it modulo
* 10^9+7. in a tiling, every square must be tiled by a tile. two tilings are
* different if and only if there are two 4-directionally adjacent cells on the
* board such that exactly one of the tilings has both squares occupied by a
* tile.
*/
int numTilings(int n) {
static int mod = 1e9 + 7;
if (n <= 2)
return n;
int x = 1, y = 2, z = 5, m;
for (int i = 4; i <= n; ++i) {
m = (2LL * z + x) % mod;
x ^= y ^= x ^= y;
y ^= z ^= y ^= z;
z ^= m ^= z ^= m;
}
return z;
}
int main() {
printf("%d\n", numTilings(3)); // expect: 5
printf("%d\n", numTilings(1)); // expect: 1
}
|