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:
The greatest common divisor of any adjacent values in the sequence is equal to 1.
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 thetotal number of distinct sequences possible. Since the answer may be very large, return it modulo10^9 + 7.
Two sequences are considered distinct if at least one element is different.
Input: n =4Output: 184Explanation: 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 return184.
Input: n =2Output: 22Explanation: 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 return22.
We need to count sequences of dice rolls of length n, where adjacent rolls are coprime and no value repeats within 2 positions. This is a classic DP with state: last two rolls, current position, and value constraints.
#include<vector>#include<numeric>usingnamespace std;
constint MOD =1e9+7;
intgcd(int a, int b) { return b ? gcd(b, a % b) : a; }
classSolution {
public:int distinctSequences(int n) {
vector<vector<vector<int>>> dp(n+1, vector<vector<int>>(7, vector<int>(7, -1)));
function<int(int,int,int)> dfs = [&](int pos, int last, int second_last) {
if (pos == n) return1;
int&res = dp[pos][last][second_last];
if (res !=-1) return res;
res =0;
for (int d =1; d <=6; ++d) {
if (d == last || d == second_last) continue;
if (last && gcd(d, last) !=1) continue;
res = (res + dfs(pos+1, d, last)) % MOD;
}
return res;
};
returndfs(0, 0, 0);
}
};
import java.util.*;
classSolution {
int MOD = (int)1e9+7;
int[][][] dp;
intgcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
publicintdistinctSequences(int n) {
dp =newint[n+1][7][7];
for (int[][] arr : dp) for (int[] a : arr) Arrays.fill(a, -1);
return dfs(0, 0, 0, n);
}
intdfs(int pos, int last, int second_last, int n) {
if (pos == n) return 1;
if (dp[pos][last][second_last]!=-1) return dp[pos][last][second_last];
int res = 0;
for (int d = 1; d <= 6; ++d) {
if (d == last || d == second_last) continue;
if (last != 0 && gcd(d, last) != 1) continue;
res = (res + dfs(pos+1, d, last, n)) % MOD;
}
return dp[pos][last][second_last]= res;
}
}
classSolution {
val MOD = 1_000_000_007
lateinitvar dp: Array<Array<IntArray>>
fungcd(a: Int, b: Int): Int = if (b ==0) a else gcd(b, a % b)
fundistinctSequences(n: Int): Int {
dp = Array(n+1) { Array(7) { IntArray(7) { -1 } } }
fundfs(pos: Int, last: Int, secondLast: Int): Int {
if (pos == n) return1if (dp[pos][last][secondLast] != -1) return dp[pos][last][secondLast]
var res = 0for (d in1..6) {
if (d == last || d == secondLast) continueif (last !=0&& gcd(d, last) !=1) continue res = (res + dfs(pos+1, d, last)) % MOD
}
dp[pos][last][secondLast] = res
return res
}
return dfs(0, 0, 0)
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from functools import lru_cache
from math import gcd
MOD =10**9+7classSolution:
defdistinctSequences(self, n: int) -> int:
@lru_cache(None)
defdfs(pos, last, second_last):
if pos == n: return1 res =0for d in range(1, 7):
if d == last or d == second_last: continueif last and gcd(d, last) !=1: continue res = (res + dfs(pos+1, d, last)) % MOD
return res
return dfs(0, 0, 0)
use std::collections::HashMap;
fngcd(a: i32, b: i32) -> i32 { if b ==0 { a } else { gcd(b, a % b) } }
impl Solution {
pubfndistinct_sequences(n: i32) -> i32 {
fndfs(pos: i32, last: i32, second_last: i32, n: i32, memo: &mut HashMap<(i32,i32,i32),i32>) -> i32 {
if pos == n { return1; }
iflet Some(&v) = memo.get(&(pos, last, second_last)) { return v; }
letmut res =0;
for d in1..=6 {
if d == last || d == second_last { continue; }
if last !=0&& gcd(d, last) !=1 { continue; }
res = (res + dfs(pos+1, d, last, n, memo)) %1_000_000_007;
}
memo.insert((pos, last, second_last), res);
res
}
letmut memo = HashMap::new();
dfs(0, 0, 0, n, &mut memo)
}
}