diff --git a/leetcode/2301-2400/2318.Number-of-Distinct-Roll-Sequences/README.md b/leetcode/2301-2400/2318.Number-of-Distinct-Roll-Sequences/README.md index 1f1428c10..bdd848471 100755 --- a/leetcode/2301-2400/2318.Number-of-Distinct-Roll-Sequences/README.md +++ b/leetcode/2301-2400/2318.Number-of-Distinct-Roll-Sequences/README.md @@ -1,32 +1,39 @@ # [2318.Number of Distinct Roll Sequences][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +You are given an integer `n`. You roll a fair 6-sided dice `n` times. Determine the total number of **distinct** sequences of rolls possible such that the following conditions are satisfied: + +1. The **greatest common divisor** of any **adjacent** values in the sequence is equal to `1`. +2. There is **at least** a gap of 2 rolls between **equal** valued rolls. More formally, if the value of the ith roll is **equal** to the value of the jth roll, then `abs(i - j) > 2`. + +Return the **total number** of distinct sequences possible. Since the answer may be very large, return it **modulo** `10^9 + 7`. + +Two sequences are considered distinct if at least one element is different. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: n = 4 +Output: 184 +Explanation: Some of the possible sequences are (1, 2, 3, 4), (6, 1, 2, 3), (1, 2, 3, 1), etc. +Some invalid sequences are (1, 2, 1, 3), (1, 2, 3, 6). +(1, 2, 1, 3) is invalid since the first and third roll have an equal value and abs(1 - 3) = 2 (i and j are 1-indexed). +(1, 2, 3, 6) is invalid since the greatest common divisor of 3 and 6 = 3. +There are a total of 184 distinct sequences possible, so we return 184. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Number of Distinct Roll Sequences -```go ``` - +Input: n = 2 +Output: 22 +Explanation: Some of the possible sequences are (1, 2), (2, 1), (3, 2). +Some invalid sequences are (3, 6), (2, 4) since the greatest common divisor is not equal to 1. +There are a total of 22 distinct sequences possible, so we return 22. +``` ## 结语 如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] - [title]: https://leetcode.com/problems/number-of-distinct-roll-sequences/ [me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2301-2400/2318.Number-of-Distinct-Roll-Sequences/Solution.go b/leetcode/2301-2400/2318.Number-of-Distinct-Roll-Sequences/Solution.go index d115ccf5e..03b6acffd 100644 --- a/leetcode/2301-2400/2318.Number-of-Distinct-Roll-Sequences/Solution.go +++ b/leetcode/2301-2400/2318.Number-of-Distinct-Roll-Sequences/Solution.go @@ -1,5 +1,59 @@ package Solution -func Solution(x bool) bool { - return x +const mod2318 = 1000000007 + +func Solution(n int) int { + cache := make([]map[int]map[[7]int]int, n+1) + for i := 0; i <= n; i++ { + cache[i] = map[int]map[[7]int]int{} + for j := 1; j < 7; j++ { + cache[i][j] = map[[7]int]int{} + } + } + ref := map[int][]int{ + 1: {1, 2, 3, 4, 5, 6}, + 2: {1, 3, 5}, + 3: {1, 2, 4, 5}, + 4: {1, 3, 5}, + 5: {1, 2, 3, 4, 6}, + 6: {1, 5}, + } + + var dfs func(int, int, [7]int) int + dfs = func(nn, selected int, used [7]int) int { + if nn <= 1 { + return nn + } + m := cache[nn] + if v, ok := m[selected]; ok { + if v1, ok1 := v[used]; ok1 { + return v1 + } + } + ans := 0 + for _, next := range ref[selected] { + x := used + if used[next] == 0 { + used[next] = 2 + for j := 1; j < 7; j++ { + if j == next || used[j] == 0 { + continue + } + used[j]-- + } + ans = (ans + dfs(nn-1, next, used)) % mod2318 + } + used = x + } + cache[nn][selected][used] = ans + return ans + } + r := 0 + for i := 1; i < 7; i++ { + // 1:2 + used := [7]int{} + used[i] = 2 + r = (r + dfs(n, i, used)) % mod2318 + } + return r } diff --git a/leetcode/2301-2400/2318.Number-of-Distinct-Roll-Sequences/Solution_test.go b/leetcode/2301-2400/2318.Number-of-Distinct-Roll-Sequences/Solution_test.go index 14ff50eb4..675a23c05 100644 --- a/leetcode/2301-2400/2318.Number-of-Distinct-Roll-Sequences/Solution_test.go +++ b/leetcode/2301-2400/2318.Number-of-Distinct-Roll-Sequences/Solution_test.go @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 4, 184}, + {"TestCase2", 2, 22}, + {"TestCase3", 999, 209513669}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }