Number of Distinct Roll Sequences - Problem

Imagine you're a game designer creating a special dice game with magical rules! You have a fair 6-sided die and need to roll it n times to create a sequence. However, this isn't just any ordinary sequence - it must follow two mystical constraints:

๐ŸŽฏ Rule 1: Adjacent Harmony
Any two consecutive rolls must be coprime (their greatest common divisor equals 1). For example, rolling a 2 followed by a 3 is valid, but rolling a 2 followed by a 4 is not (since gcd(2,4) = 2).

๐ŸŽฏ Rule 2: Separation Distance
If you roll the same number twice, there must be at least 2 other rolls between them. So if you roll a 3 at position i, you cannot roll another 3 at positions i+1, i+2, or i+3.

Your task is to determine how many distinct valid sequences are possible. Since this number can be astronomically large, return the result modulo 109 + 7.

Two sequences are considered distinct if they differ in at least one position.

Input & Output

example_1.py โ€” Basic Case
$ Input: n = 4
โ€บ Output: 184
๐Ÿ’ก Note: For n=4, we need sequences of 4 dice rolls. Example valid sequence: [1,3,2,5]. This satisfies both constraints: adjacent elements are coprime (gcd(1,3)=1, gcd(3,2)=1, gcd(2,5)=1) and no repeated values appear within 2 positions of each other.
example_2.py โ€” Edge Case Small
$ Input: n = 2
โ€บ Output: 22
๐Ÿ’ก Note: For n=2, we need pairs of dice rolls that are coprime. Valid pairs: (1,2), (1,3), (1,4), (1,5), (1,6), (2,1), (2,3), (2,5), (3,1), (3,2), (3,4), (3,5), (4,1), (4,3), (4,5), (5,1), (5,2), (5,3), (5,4), (5,6), (6,1), (6,5). Total: 22 pairs.
example_3.py โ€” Single Roll
$ Input: n = 1
โ€บ Output: 6
๐Ÿ’ก Note: For n=1, any single dice roll is valid since there are no adjacent elements to check and no repetition constraints. We can roll any of the 6 values: 1, 2, 3, 4, 5, or 6.

Constraints

  • 1 โ‰ค n โ‰ค 104
  • Dice values are always between 1 and 6 inclusive
  • Result must be returned modulo 109 + 7

Visualization

Tap to expand
๐ŸŽฒ Building Valid Dice SequencesStep 1: First Roll (Any Value)3Position 1Step 2: Check Coprime Constraint32โœ“ gcd(3,2)=136โœ— gcd(3,6)=3Step 3: Check Separation Constraint1321โœ— Gap < 31325โœ“ All gaps โ‰ฅ 3DP State ManagementState: (position, prev1, prev2)Example: (3, dice=2, prev_dice=3)Valid Next Movesโ€ข Must be coprime with prev1 (dice=2)โ€ข Cannot equal prev1 (2) or prev2 (3)โ€ข Valid options: {1, 5}Memoization Keymemo[(3,2,3)] = count_of_valid_sequencesTime Complexity: O(n ร— 6ยฒ) vs O(6โฟ)๐ŸŽฏ Key Insight:Only the last TWO dice values determine valid next moves, making this perfect for DP optimization!
Understanding the Visualization
1
Start with First Roll
Any of the 6 dice values (1-6) can be the first roll
2
Check Adjacent Harmony
Next roll must be coprime with the previous roll (gcd = 1)
3
Enforce Separation Rule
Cannot repeat the same value within 2 positions
4
Use DP to Build Efficiently
Memoize (position, prev1, prev2) states to avoid recalculation
Key Takeaway
๐ŸŽฏ Key Insight: By tracking only the last two dice values, we can efficiently determine all valid next moves while satisfying both the coprime and separation constraints, transforming an exponential brute force solution into an elegant polynomial-time dynamic programming approach.
Asked in
Google 12 Amazon 8 Meta 6 Microsoft 4
25.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen