|
49 | 49 |
|
50 | 50 | <!-- 这里可写通用的实现逻辑 -->
|
51 | 51 |
|
| 52 | +**方法一:记忆化搜索** |
| 53 | + |
| 54 | +我们可以设计一个函数 $dfs(i, j, x)$ 表示从第 $i$ 次掷骰子开始,当前掷出的点数为 $j$,且连续掷出 $j$ 的次数为 $x$ 的方案数。其中 $j$ 的取值范围为 $[1, 6]$,而 $x$ 的取值范围为 $[1, rollMax[j - 1]]$。那么答案就是 $dfs(0, 0, 0)$。 |
| 55 | + |
| 56 | +函数 $dfs(i, j, x)$ 的计算过程如下: |
| 57 | + |
| 58 | +- 如果 $i \ge n$,说明已经掷完了 $n$ 次骰子,返回 $1$。 |
| 59 | +- 否则,我们枚举下一次掷出的点数 $k$,如果 $k \ne j$,那么我们可以直接掷出 $k$,此时连续掷出 $j$ 的次数 $x$ 就会被重置为 $1$,因此方案数为 $dfs(i + 1, k, 1)$。如果 $k = j$,那么我们需要判断 $x$ 是否小于 $rollMax[j - 1]$,如果小于,那么我们可以继续掷出 $j$,此时连续掷出 $j$ 的次数 $x$ 就会加 $1$,因此方案数为 $dfs(i + 1, j, x + 1)$。最后将所有方案数相加,即为 $dfs(i, j, x)$ 的值。注意答案可能很大,因此需要对 $10^9 + 7$ 取模。 |
| 60 | + |
| 61 | +过程中,我们可以使用记忆化搜索避免重复计算。 |
| 62 | + |
| 63 | +时间复杂度 $O(n \times k^2 \times M)$,空间复杂度 $O(n \times k \times M)$。其中 $k$ 为点数的取值范围,而 $M$ 为连续掷出某个点数的最大次数。 |
| 64 | + |
| 65 | +**方法二:动态规划** |
| 66 | + |
| 67 | +我们可以将方法一中的记忆化搜索改为动态规划。 |
| 68 | + |
| 69 | +定义 $f[i][j][x]$ 表示投掷前 $i$ 次骰子,且第 $i$ 次投掷的点数为 $j$,且连续投掷点数 $j$ 的次数为 $x$ 的方案数。初始时 $f[1][j][1] = 1$,其中 $1 \leq j \leq 6$。答案即是: |
| 70 | + |
| 71 | +$$ |
| 72 | +\sum_{j=1}^6 \sum_{x=1}^{rollMax[j-1]} f[n][j][x] |
| 73 | +$$ |
| 74 | + |
| 75 | +我们枚举上一次投掷的点数为 $j$,且连续投掷点数 $j$ 的次数为 $x$,那么当前投掷的点数可以为 $1, 2, \cdots, 6$,如果当前投掷的点数为 $k$,那么有如下两种情况: |
| 76 | + |
| 77 | +- 如果 $k \neq j$,那么我们可以直接投掷出 $k$,此时连续投掷点数 $j$ 的次数 $x$ 就会被重置为 $1$,因此方案数 $f[i][k][1]$ 就会增加 $f[i-1][j][x]$。 |
| 78 | +- 如果 $k = j$,那么我们需要判断 $x+1$ 是否小于等于 $rollMax[j-1]$,如果小于等于,那么我们可以继续投掷出 $j$,此时连续投掷点数 $j$ 的次数 $x$ 就会加 $1$,因此方案数 $f[i][j][x+1]$ 就会增加 $f[i-1][j][x]$。 |
| 79 | + |
| 80 | +最终的答案即为所有 $f[n][j][x]$ 的和。 |
| 81 | + |
| 82 | +时间复杂度 $O(n \times k^2 \times M)$,空间复杂度 $O(n \times k \times M)$。其中 $k$ 为点数的取值范围,而 $M$ 为连续掷出某个点数的最大次数。 |
| 83 | + |
52 | 84 | <!-- tabs:start -->
|
53 | 85 |
|
54 | 86 | ### **Python3**
|
55 | 87 |
|
56 | 88 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
57 | 89 |
|
58 | 90 | ```python
|
| 91 | +class Solution: |
| 92 | + def dieSimulator(self, n: int, rollMax: List[int]) -> int: |
| 93 | + @cache |
| 94 | + def dfs(i, j, x): |
| 95 | + if i >= n: |
| 96 | + return 1 |
| 97 | + ans = 0 |
| 98 | + for k in range(1, 7): |
| 99 | + if k != j: |
| 100 | + ans += dfs(i + 1, k, 1) |
| 101 | + elif x < rollMax[j - 1]: |
| 102 | + ans += dfs(i + 1, j, x + 1) |
| 103 | + return ans % (10 ** 9 + 7) |
| 104 | + |
| 105 | + return dfs(0, 0, 0) |
| 106 | +``` |
59 | 107 |
|
| 108 | +```python |
| 109 | +class Solution: |
| 110 | + def dieSimulator(self, n: int, rollMax: List[int]) -> int: |
| 111 | + f = [[[0] * 16 for _ in range(7)] for _ in range(n + 1)] |
| 112 | + for j in range(1, 7): |
| 113 | + f[1][j][1] = 1 |
| 114 | + for i in range(2, n + 1): |
| 115 | + for j in range(1, 7): |
| 116 | + for x in range(1, rollMax[j - 1] + 1): |
| 117 | + for k in range(1, 7): |
| 118 | + if k != j: |
| 119 | + f[i][k][1] += f[i - 1][j][x] |
| 120 | + elif x + 1 <= rollMax[j - 1]: |
| 121 | + f[i][j][x + 1] += f[i - 1][j][x] |
| 122 | + mod = 10**9 + 7 |
| 123 | + ans = 0 |
| 124 | + for j in range(1, 7): |
| 125 | + for x in range(1, rollMax[j - 1] + 1): |
| 126 | + ans = (ans + f[n][j][x]) % mod |
| 127 | + return ans |
60 | 128 | ```
|
61 | 129 |
|
62 | 130 | ### **Java**
|
63 | 131 |
|
64 | 132 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
65 | 133 |
|
66 | 134 | ```java
|
| 135 | +class Solution { |
| 136 | + private Integer[][][] f; |
| 137 | + private int[] rollMax; |
| 138 | + |
| 139 | + public int dieSimulator(int n, int[] rollMax) { |
| 140 | + f = new Integer[n][7][16]; |
| 141 | + this.rollMax = rollMax; |
| 142 | + return dfs(0, 0, 0); |
| 143 | + } |
| 144 | + |
| 145 | + private int dfs(int i, int j, int x) { |
| 146 | + if (i >= f.length) { |
| 147 | + return 1; |
| 148 | + } |
| 149 | + if (f[i][j][x] != null) { |
| 150 | + return f[i][j][x]; |
| 151 | + } |
| 152 | + long ans = 0; |
| 153 | + for (int k = 1; k <= 6; ++k) { |
| 154 | + if (k != j) { |
| 155 | + ans += dfs(i + 1, k, 1); |
| 156 | + } else if (x < rollMax[j - 1]) { |
| 157 | + ans += dfs(i + 1, j, x + 1); |
| 158 | + } |
| 159 | + } |
| 160 | + ans %= 1000000007; |
| 161 | + return f[i][j][x] = (int) ans; |
| 162 | + } |
| 163 | +} |
| 164 | +``` |
| 165 | + |
| 166 | +```java |
| 167 | +class Solution { |
| 168 | + public int dieSimulator(int n, int[] rollMax) { |
| 169 | + int[][][] f = new int[n + 1][7][16]; |
| 170 | + for (int j = 1; j <= 6; ++j) { |
| 171 | + f[1][j][1] = 1; |
| 172 | + } |
| 173 | + final int mod = (int) 1e9 + 7; |
| 174 | + for (int i = 2; i <= n; ++i) { |
| 175 | + for (int j = 1; j <= 6; ++j) { |
| 176 | + for (int x = 1; x <= rollMax[j - 1]; ++x) { |
| 177 | + for (int k = 1; k <= 6; ++k) { |
| 178 | + if (k != j) { |
| 179 | + f[i][k][1] = (f[i][k][1] + f[i - 1][j][x]) % mod; |
| 180 | + } else if (x + 1 <= rollMax[j - 1]) { |
| 181 | + f[i][j][x + 1] = (f[i][j][x + 1] + f[i - 1][j][x]) % mod; |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + int ans = 0; |
| 188 | + for (int j = 1; j <= 6; ++j) { |
| 189 | + for (int x = 1; x <= rollMax[j - 1]; ++x) { |
| 190 | + ans = (ans + f[n][j][x]) % mod; |
| 191 | + } |
| 192 | + } |
| 193 | + return ans; |
| 194 | + } |
| 195 | +} |
| 196 | +``` |
| 197 | + |
| 198 | +### **C++** |
| 199 | + |
| 200 | +```cpp |
| 201 | +class Solution { |
| 202 | +public: |
| 203 | + int dieSimulator(int n, vector<int>& rollMax) { |
| 204 | + int f[n][7][16]; |
| 205 | + memset(f, 0, sizeof f); |
| 206 | + const int mod = 1e9 + 7; |
| 207 | + function<int(int, int, int)> dfs = [&](int i, int j, int x) -> int { |
| 208 | + if (i >= n) { |
| 209 | + return 1; |
| 210 | + } |
| 211 | + if (f[i][j][x]) { |
| 212 | + return f[i][j][x]; |
| 213 | + } |
| 214 | + long ans = 0; |
| 215 | + for (int k = 1; k <= 6; ++k) { |
| 216 | + if (k != j) { |
| 217 | + ans += dfs(i + 1, k, 1); |
| 218 | + } else if (x < rollMax[j - 1]) { |
| 219 | + ans += dfs(i + 1, j, x + 1); |
| 220 | + } |
| 221 | + } |
| 222 | + ans %= mod; |
| 223 | + return f[i][j][x] = ans; |
| 224 | + }; |
| 225 | + return dfs(0, 0, 0); |
| 226 | + } |
| 227 | +}; |
| 228 | +``` |
| 229 | +
|
| 230 | +```cpp |
| 231 | +class Solution { |
| 232 | +public: |
| 233 | + int dieSimulator(int n, vector<int>& rollMax) { |
| 234 | + int f[n + 1][7][16]; |
| 235 | + memset(f, 0, sizeof f); |
| 236 | + for (int j = 1; j <= 6; ++j) { |
| 237 | + f[1][j][1] = 1; |
| 238 | + } |
| 239 | + const int mod = 1e9 + 7; |
| 240 | + for (int i = 2; i <= n; ++i) { |
| 241 | + for (int j = 1; j <= 6; ++j) { |
| 242 | + for (int x = 1; x <= rollMax[j - 1]; ++x) { |
| 243 | + for (int k = 1; k <= 6; ++k) { |
| 244 | + if (k != j) { |
| 245 | + f[i][k][1] = (f[i][k][1] + f[i - 1][j][x]) % mod; |
| 246 | + } else if (x + 1 <= rollMax[j - 1]) { |
| 247 | + f[i][j][x + 1] = (f[i][j][x + 1] + f[i - 1][j][x]) % mod; |
| 248 | + } |
| 249 | + } |
| 250 | + } |
| 251 | + } |
| 252 | + } |
| 253 | + int ans = 0; |
| 254 | + for (int j = 1; j <= 6; ++j) { |
| 255 | + for (int x = 1; x <= rollMax[j - 1]; ++x) { |
| 256 | + ans = (ans + f[n][j][x]) % mod; |
| 257 | + } |
| 258 | + } |
| 259 | + return ans; |
| 260 | + } |
| 261 | +}; |
| 262 | +``` |
| 263 | + |
| 264 | +### **Go** |
| 265 | + |
| 266 | +```go |
| 267 | +func dieSimulator(n int, rollMax []int) int { |
| 268 | + f := make([][7][16]int, n) |
| 269 | + const mod = 1e9 + 7 |
| 270 | + var dfs func(i, j, x int) int |
| 271 | + dfs = func(i, j, x int) int { |
| 272 | + if i >= n { |
| 273 | + return 1 |
| 274 | + } |
| 275 | + if f[i][j][x] != 0 { |
| 276 | + return f[i][j][x] |
| 277 | + } |
| 278 | + ans := 0 |
| 279 | + for k := 1; k <= 6; k++ { |
| 280 | + if k != j { |
| 281 | + ans += dfs(i+1, k, 1) |
| 282 | + } else if x < rollMax[j-1] { |
| 283 | + ans += dfs(i+1, j, x+1) |
| 284 | + } |
| 285 | + } |
| 286 | + f[i][j][x] = ans % mod |
| 287 | + return f[i][j][x] |
| 288 | + } |
| 289 | + return dfs(0, 0, 0) |
| 290 | +} |
| 291 | +``` |
67 | 292 |
|
| 293 | +```go |
| 294 | +func dieSimulator(n int, rollMax []int) (ans int) { |
| 295 | + f := make([][7][16]int, n+1) |
| 296 | + for j := 1; j <= 6; j++ { |
| 297 | + f[1][j][1] = 1 |
| 298 | + } |
| 299 | + const mod = 1e9 + 7 |
| 300 | + for i := 2; i <= n; i++ { |
| 301 | + for j := 1; j <= 6; j++ { |
| 302 | + for x := 1; x <= rollMax[j-1]; x++ { |
| 303 | + for k := 1; k <= 6; k++ { |
| 304 | + if k != j { |
| 305 | + f[i][k][1] = (f[i][k][1] + f[i-1][j][x]) % mod |
| 306 | + } else if x+1 <= rollMax[j-1] { |
| 307 | + f[i][j][x+1] = (f[i][j][x+1] + f[i-1][j][x]) % mod |
| 308 | + } |
| 309 | + } |
| 310 | + } |
| 311 | + } |
| 312 | + } |
| 313 | + for j := 1; j <= 6; j++ { |
| 314 | + for x := 1; x <= rollMax[j-1]; x++ { |
| 315 | + ans = (ans + f[n][j][x]) % mod |
| 316 | + } |
| 317 | + } |
| 318 | + return |
| 319 | +} |
68 | 320 | ```
|
69 | 321 |
|
70 | 322 | ### **...**
|
|
0 commit comments