- 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
- 70 lines of Go from the credited upstream file 570E.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 "bufio"5 . "fmt"6 "io"7)8 910func CF570E(_r io.Reader, out io.Writer) {11 in := bufio.NewReader(_r)12 const mod = 1_000_000_00713 min := func(a, b int) int {14 if a > b {15 return b16 }17 return a18 }19 max := func(a, b int) int {20 if b > a {21 return b22 }23 return a24 }25 26 var n, m int27 Fscan(in, &n, &m)28 a := make([]string, n)29 for i := range a {30 Fscan(in, &a[i])31 }32 if a[0][0] != a[n-1][m-1] {33 Fprint(out, 0)34 return35 }36 37 f := make([][]int, n+1)38 for i := range f {39 f[i] = make([]int, n+2)40 }41 f[1][n] = 142 for i := 1; i < (n+m)/2; i++ {43 for r1 := min(n, i+1); r1 > 0 && r1 >= i+2-m; r1-- {44 c1 := i + 2 - r145 for r2 := max(1, max(n-i, r1-2)); r2 <= n && r2 < n+m-i; r2++ {46 c2 := n + m - i - r247 if a[r1-1][c1-1] == a[r2-1][c2-1] {48 f[r1][r2] = (((f[r1][r2]+f[r1][r2+1])%mod+f[r1-1][r2+1])%mod + f[r1-1][r2]) % mod49 } else {50 f[r1][r2] = 051 }52 }53 }54 }55 56 ans := int64(0)57 if (n+m)%2 > 0 {58 for i := 1; i <= n; i++ {59 ans += int64(f[i][i] + f[i][i+1])60 }61 } else {62 for i := 1; i <= n; i++ {63 ans += int64(f[i][i])64 }65 }66 Fprint(out, ans%mod)67}68 6970