add new solutions

This commit is contained in:
kanna5 2025-12-31 15:14:22 +09:00
parent 71189b61cf
commit 9c2c959a9b
Signed by: kkyy
GPG key ID: 06332F3965E9B0CF
10 changed files with 349 additions and 8 deletions

View file

@ -0,0 +1,62 @@
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