62 lines
1.1 KiB
Go
62 lines
1.1 KiB
Go
package q1411
|
|
|
|
type Color uint8
|
|
|
|
const MODULO int = 1e9 + 7
|
|
|
|
func numOfWays(n int) int {
|
|
patterns := make([][3]Color, 0, 12)
|
|
for a := range 3 {
|
|
for b := range 3 {
|
|
for c := range 3 {
|
|
if a == b || b == c {
|
|
continue
|
|
}
|
|
patterns = append(patterns, [3]Color{Color(a), Color(b), Color(c)})
|
|
}
|
|
}
|
|
}
|
|
|
|
// Find compatible patterns
|
|
compatiblePtns := make([][]int, len(patterns))
|
|
for i := range len(patterns) - 1 {
|
|
for j := i + 1; j < len(patterns); j++ {
|
|
ok := true
|
|
for k := range 3 {
|
|
if patterns[i][k] == patterns[j][k] {
|
|
ok = false
|
|
break
|
|
}
|
|
}
|
|
if ok {
|
|
compatiblePtns[i] = append(compatiblePtns[i], j)
|
|
compatiblePtns[j] = append(compatiblePtns[j], i)
|
|
}
|
|
}
|
|
}
|
|
|
|
counts := make([]int, len(patterns))
|
|
for i := range counts {
|
|
counts[i] = 1
|
|
}
|
|
|
|
countsNext := make([]int, len(patterns))
|
|
for range n - 1 {
|
|
for i := range len(patterns) {
|
|
countsNext[i] = 0
|
|
for _, j := range compatiblePtns[i] {
|
|
countsNext[i] += counts[j]
|
|
}
|
|
countsNext[i] %= MODULO
|
|
}
|
|
counts, countsNext = countsNext, counts
|
|
}
|
|
|
|
sum := 0
|
|
for _, c := range counts {
|
|
sum += c
|
|
}
|
|
return sum % MODULO
|
|
}
|
|
|
|
var _ = numOfWays
|