- 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
- 80 lines of Go from the credited upstream file 1152F2.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 mod52 = 1_000_000_00711 12type matrix52 [][]int13 14func newMatrix52(n, m int) matrix52 {15 a := make(matrix52, n)16 for i := range a {17 a[i] = make([]int, m)18 }19 return a20}21 22func (a matrix52) mul(b matrix52) matrix52 {23 c := newMatrix52(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) % mod5231 }32 }33 }34 return c35}36 37func (a matrix52) powMul(n int, f0 matrix52) matrix52 {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 cf1152F2(in io.Reader, out io.Writer) {49 var n, k, d, ans int50 Fscan(in, &n, &k, &d)51 f := func(i, j int) int { return i*(k+1) + j }52 53 sz := (k + 1) << d54 m := newMatrix52(sz, sz)55 for s := range 1 << d {56 t := s << 1 & (1<<d - 1)57 c := bits.OnesCount(uint(s))58 for j := c; j <= k; j++ {59 60 m[f(t, j)][f(s, j)] = 161 if j < k {62 63 64 m[f(t^1, j+1)][f(s, j)] = c + 165 }66 }67 }68 69 f0 := newMatrix52(sz, 1)70 f0[0][0] = 171 72 fn := m.powMul(n, f0)73 for s := range 1 << d {74 ans += fn[f(s, k)][0]75 }76 Fprint(out, ans%mod52)77}78 7980