- 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
- 65 lines of Go from the credited upstream file 1117D.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 "slices"7)8 910const mod17 = 1_000_000_00711 12func kitamasa17(coef, a []int, n int) (ans int) {13 defer func() { ans = (ans%mod17 + mod17) % mod17 }()14 if n < len(a) {15 return a[n]16 }17 18 k := len(coef)19 compose := func(a, b []int) []int {20 c := make([]int, k)21 for _, v := range a {22 for j, w := range b {23 c[j] = (c[j] + v*w) % mod1724 }25 bk1 := b[k-1]26 for j := k - 1; j > 0; j-- {27 b[j] = (b[j-1] + bk1*coef[j]) % mod1728 }29 b[0] = bk1 * coef[0] % mod1730 }31 return c32 }33 34 resC := make([]int, k)35 resC[0] = 136 c := make([]int, k)37 c[1] = 138 for ; n > 0; n /= 2 {39 if n%2 > 0 {40 resC = compose(c, resC)41 }42 c = compose(c, slices.Clone(c))43 }44 45 for i, c := range resC {46 ans = (ans + c*a[i]) % mod1747 }48 return49}50 51func cf1117D(in io.Reader, out io.Writer) {52 var n, m int53 Fscan(in, &n, &m)54 a := make([]int, m)55 for i := range a {56 a[i] = 157 }58 coef := make([]int, m)59 coef[0] = 160 coef[m-1] = 161 Fprint(out, kitamasa17(coef, a, n))62}63 6465