- 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
- 56 lines of Go from the credited upstream file 1225E.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 CF1225E(_r io.Reader, out io.Writer) {11 in := bufio.NewReader(_r)12 const mod int64 = 1e9 + 713 14 var n, m int15 Fscan(in, &n, &m)16 if n == 1 && m == 1 {17 Fprint(out, 1)18 return19 }20 a := make([]string, n)21 for i := range a {22 Fscan(in, &a[i])23 }24 25 r := make([][]int, n+1)26 c := make([][]int, n+1)27 dpr := make([][]int64, n+1) 28 dpc := make([][]int64, n+1)29 for i := range r {30 r[i] = make([]int, m+1)31 c[i] = make([]int, m+1)32 dpr[i] = make([]int64, m+1)33 dpc[i] = make([]int64, m+1)34 }35 for i := n - 1; i >= 0; i-- {36 for j := m - 1; j >= 0; j-- {37 r[i][j] = r[i][j+1]38 c[i][j] = c[i+1][j]39 if a[i][j] == 'R' {40 r[i][j]++41 c[i][j]++42 }43 if i == n-1 && j == m-1 {44 dpc[i][j] = 145 dpr[i][j] = 146 } else {47 dpc[i][j] = (dpc[i+1][j] + dpr[i][j+1] - dpr[i][m-r[i][j+1]]) % mod48 dpr[i][j] = (dpr[i][j+1] + dpc[i+1][j] - dpc[n-c[i+1][j]][j]) % mod49 }50 }51 }52 Fprint(out, ((dpc[0][0]-dpc[1][0]+dpr[0][0]-dpr[0][1])%mod+mod)%mod)53}54 5556