- 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
- 78 lines of Go from the credited upstream file 691E.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 "math/bits"7)8 910const mod91 = 1_000_000_00711 12type matrix91 [][]int13 14func newMatrix91(n, m int) matrix91 {15 a := make(matrix91, n)16 for i := range a {17 a[i] = make([]int, m)18 }19 return a20}21 22func (a matrix91) mul(b matrix91) matrix91 {23 c := newMatrix91(len(a), len(b[0]))24 for i, row := range a {25 for k, x := range row {26 if x == 0 {27 continue28 }29 for j, y := range b[k] {30 c[i][j] = (c[i][j] + x*y) % mod9131 }32 }33 }34 return c35}36 37func (a matrix91) powMul(n int, f0 matrix91) matrix91 {38 res := f039 for ; n > 0; n /= 2 {40 if n%2 > 0 {41 res = a.mul(res)42 }43 a = a.mul(a)44 }45 return res46}47 48func cf691E(in io.Reader, out io.Writer) {49 var n, k, ans int50 Fscan(in, &n, &k)51 a := make([]int, n)52 for i := range a {53 Fscan(in, &a[i])54 }55 m := newMatrix91(n, n)56 for i, v := range a {57 for j, w := range a[:i+1] {58 if bits.OnesCount(uint(v^w))%3 == 0 {59 m[i][j] = 160 m[j][i] = 161 }62 }63 }64 65 f0 := newMatrix91(n, 1)66 for i := range f0 {67 f0[i][0] = 168 }69 70 fk := m.powMul(k-1, f0)71 for _, row := range fk {72 ans += row[0]73 }74 Fprint(out, ans%mod91)75}76 7778