forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
number-of-distinct-roll-sequences.py
39 lines (35 loc) · 1.04 KB
/
number-of-distinct-roll-sequences.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Time: O(6^3 * n)
# Space: O(6^2)
import collections
# dp
class Solution(object):
def distinctSequences(self, n):
"""
:type n: int
:rtype: int
"""
def gcd(a, b):
while b:
a, b = b, a%b
return a
if n == 1:
return 6
MOD = 10**9 + 7
dp = [[0]*6 for _ in xrange(6)]
for i in xrange(6):
for j in xrange(6):
if i != j and gcd(i+1, j+1) == 1:
dp[i][j] = 1
for _ in xrange(n-2):
new_dp = [[0]*6 for _ in xrange(6)]
for i in xrange(6):
for j in xrange(6):
if not dp[i][j]:
continue
for k in xrange(6):
if not dp[j][k]:
continue
if k != i:
new_dp[i][j] = (new_dp[i][j]+dp[j][k]) % MOD
dp = new_dp
return sum(dp[i][j] for i in xrange(6) for j in xrange(6)) % MOD