返回介绍

solution / 2300-2399 / 2318.Number of Distinct Roll Sequences / README_EN

发布于 2024-06-17 01:03:07 字数 6369 浏览 0 评论 0 收藏 0

2318. Number of Distinct Roll Sequences

中文文档

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 109 + 7.

Two sequences are considered distinct if at least one element is different.

 

Example 1:

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:

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.

 

Constraints:

  • 1 <= n <= 104

Solutions

Solution 1

class Solution:
  def distinctSequences(self, n: int) -> int:
    if n == 1:
      return 6
    mod = 10**9 + 7
    dp = [[[0] * 6 for _ in range(6)] for _ in range(n + 1)]
    for i in range(6):
      for j in range(6):
        if gcd(i + 1, j + 1) == 1 and i != j:
          dp[2][i][j] = 1
    for k in range(3, n + 1):
      for i in range(6):
        for j in range(6):
          if gcd(i + 1, j + 1) == 1 and i != j:
            for h in range(6):
              if gcd(h + 1, i + 1) == 1 and h != i and h != j:
                dp[k][i][j] += dp[k - 1][h][i]
    ans = 0
    for i in range(6):
      for j in range(6):
        ans += dp[-1][i][j]
    return ans % mod
class Solution {
  public int distinctSequences(int n) {
    if (n == 1) {
      return 6;
    }
    int mod = (int) 1e9 + 7;
    int[][][] dp = new int[n + 1][6][6];
    for (int i = 0; i < 6; ++i) {
      for (int j = 0; j < 6; ++j) {
        if (gcd(i + 1, j + 1) == 1 && i != j) {
          dp[2][i][j] = 1;
        }
      }
    }
    for (int k = 3; k <= n; ++k) {
      for (int i = 0; i < 6; ++i) {
        for (int j = 0; j < 6; ++j) {
          if (gcd(i + 1, j + 1) == 1 && i != j) {
            for (int h = 0; h < 6; ++h) {
              if (gcd(h + 1, i + 1) == 1 && h != i && h != j) {
                dp[k][i][j] = (dp[k][i][j] + dp[k - 1][h][i]) % mod;
              }
            }
          }
        }
      }
    }
    int ans = 0;
    for (int i = 0; i < 6; ++i) {
      for (int j = 0; j < 6; ++j) {
        ans = (ans + dp[n][i][j]) % mod;
      }
    }
    return ans;
  }

  private int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
  }
}
class Solution {
public:
  int distinctSequences(int n) {
    if (n == 1) return 6;
    int mod = 1e9 + 7;
    vector<vector<vector<int>>> dp(n + 1, vector<vector<int>>(6, vector<int>(6)));
    for (int i = 0; i < 6; ++i)
      for (int j = 0; j < 6; ++j)
        if (gcd(i + 1, j + 1) == 1 && i != j)
          dp[2][i][j] = 1;
    for (int k = 3; k <= n; ++k)
      for (int i = 0; i < 6; ++i)
        for (int j = 0; j < 6; ++j)
          if (gcd(i + 1, j + 1) == 1 && i != j)
            for (int h = 0; h < 6; ++h)
              if (gcd(h + 1, i + 1) == 1 && h != i && h != j)
                dp[k][i][j] = (dp[k][i][j] + dp[k - 1][h][i]) % mod;
    int ans = 0;
    for (int i = 0; i < 6; ++i)
      for (int j = 0; j < 6; ++j)
        ans = (ans + dp[n][i][j]) % mod;
    return ans;
  }

  int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
  }
};
func distinctSequences(n int) int {
  if n == 1 {
    return 6
  }
  dp := make([][][]int, n+1)
  for k := range dp {
    dp[k] = make([][]int, 6)
    for i := range dp[k] {
      dp[k][i] = make([]int, 6)
    }
  }
  for i := 0; i < 6; i++ {
    for j := 0; j < 6; j++ {
      if gcd(i+1, j+1) == 1 && i != j {
        dp[2][i][j] = 1
      }
    }
  }
  mod := int(1e9) + 7
  for k := 3; k <= n; k++ {
    for i := 0; i < 6; i++ {
      for j := 0; j < 6; j++ {
        if gcd(i+1, j+1) == 1 && i != j {
          for h := 0; h < 6; h++ {
            if gcd(h+1, i+1) == 1 && h != i && h != j {
              dp[k][i][j] = (dp[k][i][j] + dp[k-1][h][i]) % mod
            }
          }
        }
      }
    }
  }
  ans := 0
  for i := 0; i < 6; i++ {
    for j := 0; j < 6; j++ {
      ans = (ans + dp[n][i][j]) % mod
    }
  }
  return ans
}

func gcd(a, b int) int {
  if b == 0 {
    return a
  }
  return gcd(b, a%b)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文