- 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
- 76 lines of Go from the credited upstream file 1970E3.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 mod70 = 1_000_000_00710 11type matrix70 [][]int12 13func newMatrix70(n, m int) matrix70 {14 a := make(matrix70, n)15 for i := range a {16 a[i] = make([]int, m)17 }18 return a19}20 21func (a matrix70) mul(b matrix70) matrix70 {22 c := newMatrix70(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) % mod7030 }31 }32 }33 return c34}35 36func (a matrix70) powMul(n int, f0 matrix70) matrix70 {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 cf1970E3(in io.Reader, out io.Writer) {48 var k, n, l, ss, sl int49 Fscan(in, &k, &n)50 a := make([]int, k)51 for i := range a {52 Fscan(in, &a[i])53 ss += a[i]54 }55 56 m := newMatrix70(2, 2)57 f1 := newMatrix70(2, 1)58 f1[0][0] = a[0]59 for i, s := range a {60 Fscan(in, &l)61 if i == 0 {62 f1[1][0] = l63 }64 sl += l65 m[0][0] = (m[0][0] + s*(s+l)) % mod7066 m[0][1] = (m[0][1] + s*s) % mod7067 m[1][0] = (m[1][0] + l*(s+l)) % mod7068 m[1][1] = (m[1][1] + l*s) % mod7069 }70 71 fn := m.powMul(n-1, f1)72 Fprint(out, (fn[0][0]*(ss+sl)+fn[1][0]*ss)%mod70)73}74 7576