- Define precisely what one DP state represents.
- Establish the base cases before transitions are evaluated.
- Process states in dependency order and combine only already-known values.
Code notes
- 69 lines of Go from the credited upstream file 2B.go.
- The implementation visibly relies on sequence storage, cached states.
- No explicit loop blocks detected.
Complexity
Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.
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 "strings"8)9 1011func CF2B(_r io.Reader, out io.Writer) {12 in := bufio.NewReader(_r)13 var n, v int14 Fscan(in, &n)15 zx := -116 dp := make([][][2]int, n)17 for i := range dp {18 dp[i] = make([][2]int, n)19 for j := 0; j < n; j++ {20 Fscan(in, &v)21 if v == 0 {22 zx = i23 }24 for k, d := range []int{2, 5} {25 if i == 0 && j == 0 {26 } else if i == 0 {27 dp[i][j][k] = dp[i][j-1][k]28 } else if j == 0 {29 dp[i][j][k] = dp[i-1][j][k]30 } else {31 dp[i][j][k] = min(dp[i-1][j][k], dp[i][j-1][k])32 }33 if v > 0 {34 for ; v%d == 0; v /= d {35 dp[i][j][k]++36 }37 }38 }39 }40 }41 42 d := dp[n-1][n-1]43 k := 044 if d[1] < d[0] {45 k = 146 }47 if d[k] > 1 && zx != -1 {48 Fprint(out, "1\n", strings.Repeat("D", zx), strings.Repeat("R", n-1), strings.Repeat("D", n-1-zx))49 return50 }51 Fprintln(out, d[k])52 var f func(x, y int)53 f = func(x, y int) {54 if x == 0 && y == 0 {55 return56 }57 if x == 0 || y > 0 && dp[x][y-1][k] < dp[x-1][y][k] {58 f(x, y-1)59 Fprint(out, "R")60 } else {61 f(x-1, y)62 Fprint(out, "D")63 }64 }65 f(n-1, n-1)66}67 6869