- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 95 lines of Go from the credited upstream file 93D.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1package main2 3import (4 . "fmt"5 "io"6)7 89const mod93 = 1_000_000_00710 11type matrix93 [][]int12 13func newMatrix93(n, m int) matrix93 {14 a := make(matrix93, n)15 for i := range a {16 a[i] = make([]int, m)17 }18 return a19}20 21func (a matrix93) mul(b matrix93) matrix93 {22 c := newMatrix93(len(a), len(b[0]))23 for i, row := range a {24 for k, x := range row {25 if x == 0 {26 continue27 }28 for j, y := range b[k] {29 c[i][j] = (c[i][j] + x*y) % mod9330 }31 }32 }33 return c34}35 36func (a matrix93) powMul(n int, f0 matrix93) matrix93 {37 res := f038 for ; n > 0; n /= 2 {39 if n%2 > 0 {40 res = a.mul(res)41 }42 a = a.mul(a)43 }44 return res45}46 47func f93(n int) int {48 if n == 0 {49 return 050 }51 if n == 1 {52 return 453 }54 55 m := newMatrix93(17, 17)56 f2 := newMatrix93(17, 1)57 f2[16][0] = 4 58 59 for i := range 4 {60 for j := range 4 {61 if j == i || i+j == 3 {62 continue63 }64 for k := range 4 {65 if k != j && j+k != 3 && (j != 0 || i+k != 3) {66 m[i*4+j][j*4+k] = 167 m[16][j*4+k]++ 68 }69 }70 f2[i*4+j][0] = 171 f2[16][0]++ 72 }73 }74 m[16][16] = 175 76 return m.powMul(n-2, f2)[16][0]77}78 79808182func solve93(n int) int {83 const inv2 = (mod93 + 1) / 284 return (f93(n) + f93((n+1)/2)) * inv285}86 87func cf93D(in io.Reader, out io.Writer) {88 var l, r int89 Fscan(in, &l, &r)90 ans := solve93(r) - solve93(l-1)91 Fprintln(out, (ans%mod93+mod93)%mod93)92}93 9495