Skip to content

Latest commit

 

History

History

2318

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

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

Companies: ServiceNow

Related Topics:
Dynamic Programming, Memoization

Similar Questions:

Solution 1. DP

Let dp[i][a][b] be the number of distinct sequences with i rolls and last two numbers being a and b.

dp[i][a][b] = SUM( dp[i-1][x][a] | x != a and x != b and gcd(x,a) == 1 )
dp[2][a][b] = 1 if a != b and gcd(a, b) == 1

The answer is `SUM( dp[n][a][b] | a != b and gcd(a,b) == 1 )

// OJ: https://leetcode.com/problems/number-of-distinct-roll-sequences
// Author: github.com/lzl124631x
// Time: O(N * D^3) where D is the number of sides on the dice. D=6 in this problem.
// Space: O(N * D^2)
class Solution {
public:
    int distinctSequences(int n) {
        if (n == 1) return 6;
        int mod = 1e9 + 7, dp[10001][7][7] = {}, ans = 0;
        for (int i = 2; i <= n; ++i) {
            for (int a = 1; a <= 6; ++a) {
                for (int b = 1; b <= 6; ++b) {
                    if (a == b || gcd(a, b) != 1) continue;
                    if (i == 2) dp[i][a][b] = 1;
                    else {
                        for (int x = 1; x <= 6; ++x) {
                            if (x != a && x != b && gcd(x, a) == 1) dp[i][a][b] = (dp[i][a][b] + dp[i - 1][x][a]) % mod;
                        }
                    }
                    if (i == n) ans = (ans + dp[i][a][b]) % mod;
                }
            }
        }
        return ans;
    }
};

Solution 2. DP with Space Optimization

// OJ: https://leetcode.com/problems/number-of-distinct-roll-sequences
// Author: github.com/lzl124631x
// Time: O(N * D^3)
// Space: O(1)
class Solution {
public:
    int distinctSequences(int n) {
        if (n == 1) return 6;
        int mod = 1e9 + 7, dp[7][7] = {}, ans = 0;
        for (int i = 2; i <= n; ++i) {
            int next[7][7] = {};
            for (int a = 1; a <= 6; ++a) {
                for (int b = 1; b <= 6; ++b) {
                    if (a == b || gcd(a, b) != 1) continue;
                    if (i == 2) next[a][b] = 1;
                    else {
                        for (int x = 1; x <= 6; ++x) {
                            if (x != a && x != b && gcd(x, a) == 1) next[a][b] = (next[a][b] + dp[x][a]) % mod;
                        }
                    }
                    if (i == n) ans = (ans + next[a][b]) % mod;
                }
            }
            swap(dp, next);
        }
        return ans;
    }
};