- 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 1025D.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)8 910func CF1025D(_r io.Reader, out io.Writer) {11 in := bufio.NewReader(_r)12 gcd := func(a, b int) int {13 for a != 0 {14 a, b = b%a, a15 }16 return b17 }18 19 var n int20 Fscan(in, &n)21 a := make([]int, n)22 ok := make([][]bool, n)23 for i := range a {24 Fscan(in, &a[i])25 ok[i] = make([]bool, n)26 }27 for i, v := range a {28 for j, w := range a[:i] {29 if gcd(v, w) > 1 {30 ok[i][j] = true31 ok[j][i] = true32 }33 }34 }35 36 dp := make([][][2]int8, n)37 for i := range dp {38 dp[i] = make([][2]int8, n)39 for j := range dp[i] {40 dp[i][j] = [2]int8{-1, -1}41 }42 }43 var f func(int, int, int) int844 f = func(l, r, side int) (res int8) {45 if l > r {46 return 147 }48 dv := &dp[l][r][side]49 if *dv != -1 {50 return *dv51 }52 defer func() { *dv = res }()53 for i := l; i <= r; i++ {54 if side == 0 && (l == 0 || ok[l-1][i]) && f(l, i-1, 1) > 0 && f(i+1, r, 0) > 0 ||55 side == 1 && (r == n-1 || ok[r+1][i]) && f(l, i-1, 1) > 0 && f(i+1, r, 0) > 0 {56 return 157 }58 }59 return60 }61 if f(0, n-1, 0) > 0 {62 Fprint(out, "Yes")63 } else {64 Fprint(out, "No")65 }66}67 6869