-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution2.cpp
31 lines (31 loc) · 1011 Bytes
/
Solution2.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
class Solution {
public:
int dieSimulator(int n, vector<int>& rollMax) {
int f[n + 1][7][16];
memset(f, 0, sizeof f);
for (int j = 1; j <= 6; ++j) {
f[1][j][1] = 1;
}
const int mod = 1e9 + 7;
for (int i = 2; i <= n; ++i) {
for (int j = 1; j <= 6; ++j) {
for (int x = 1; x <= rollMax[j - 1]; ++x) {
for (int k = 1; k <= 6; ++k) {
if (k != j) {
f[i][k][1] = (f[i][k][1] + f[i - 1][j][x]) % mod;
} else if (x + 1 <= rollMax[j - 1]) {
f[i][j][x + 1] = (f[i][j][x + 1] + f[i - 1][j][x]) % mod;
}
}
}
}
}
int ans = 0;
for (int j = 1; j <= 6; ++j) {
for (int x = 1; x <= rollMax[j - 1]; ++x) {
ans = (ans + f[n][j][x]) % mod;
}
}
return ans;
}
};