- Choose the invariant that makes a window valid or useful.
- Advance the right boundary and add the new element.
- Move the left boundary only as needed while maintaining the invariant and updating the answer.
Code notes
- 75 lines of Go from the credited upstream file 498E.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Confirm that neither pointer moves backwards; if so, the scan is usually linear apart from the window’s data-structure operations.
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 89type matrix98 [][]int10 11func newMatrix98(n, m int) matrix98 {12 a := make(matrix98, n)13 for i := range a {14 a[i] = make([]int, m)15 }16 return a17}18 19func (a matrix98) mul(b matrix98) matrix98 {20 c := newMatrix98(len(a), len(b[0]))21 for i, row := range a {22 for k, x := range row {23 if x == 0 {24 continue25 }26 for j, y := range b[k] {27 c[i][j] = (c[i][j] + x*y) % 1_000_000_00728 }29 }30 }31 return c32}33 34func (a matrix98) powMul(n int, f0 matrix98) matrix98 {35 res := f036 for ; n > 0; n /= 2 {37 if n%2 > 0 {38 res = a.mul(res)39 }40 a = a.mul(a)41 }42 return res43}44 45func cf498E(in io.Reader, out io.Writer) {46 var w int47 f := matrix98{{1}}48 for h := 1; h <= 7; h++ {49 m := newMatrix98(1<<h, 1<<h)50 for right := range m {51 for left := range m[right] {52 f0, f1 := 0, 153 for i := range h {54 s := f0 + f155 if left&right>>i&1 > 0 {56 f1 = f057 } else {58 f1 = s59 }60 f0 = s61 }62 m[right][left] = f163 }64 }65 66 Fscan(in, &w)67 68 f = append(newMatrix98(len(f), 1), f...)69 f = m.powMul(w, f)70 }71 Fprint(out, f[1<<7-1][0])72}73 7475