forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
number-of-distinct-roll-sequences.cpp
47 lines (46 loc) · 1.56 KB
/
number-of-distinct-roll-sequences.cpp
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
// Time: O(6^3 * n)
// Space: O(6^2)
// dp
class Solution {
public:
int distinctSequences(int n) {
static const int MOD = 1e9 + 7;
if (n == 1) {
return 6;
}
vector<vector<int>> dp(6, vector<int>(6));
for (int i = 0; i < 6; ++i) {
for (int j = 0; j < 6; ++j) {
if (i != j && gcd(i + 1, j + 1) == 1) {
dp[i][j] = 1;
}
}
}
for (int _ = 0; _ < n - 2; ++_) {
vector<vector<int>> new_dp(6, vector<int>(6));
for (int i = 0; i < 6; ++i) {
for (int j = 0; j < 6; ++j) {
if (!dp[i][j]) {
continue;
}
for (int k = 0; k < 6; ++k) {
if (!dp[j][k]) {
continue;
}
if (k != i) {
new_dp[i][j] = (new_dp[i][j] + dp[j][k]) % MOD;
}
}
}
}
dp = move(new_dp);
}
return accumulate(cbegin(dp), cend(dp), 0,
[&](int total, const auto& x) {
return (total + accumulate(cbegin(x), cend(x), 0,
[&](int total, int x) {
return (total + x) % MOD;
})) % MOD;
});
}
};