- 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
- 63 lines of Go from the credited upstream file 1178F1.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 CF1178F1(_r io.Reader, out io.Writer) {11 in := bufio.NewReader(_r)12 const mod = 99824435313 14 var n int15 Fscan(in, &n, &n)16 a := make([]int, n)17 for i := range a {18 Fscan(in, &a[i])19 }20 pMin := make([][]int, n)21 for i := range pMin {22 pMin[i] = make([]int, n)23 pMin[i][i] = i24 for j, p := i+1, i; j < n; j++ {25 if a[j] < a[p] {26 p = j27 }28 pMin[i][j] = p29 }30 }31 32 dp := make([][]int64, n)33 for i := range dp {34 dp[i] = make([]int64, n)35 for j := range dp[i] {36 dp[i][j] = -137 }38 }39 var f func(int, int) int6440 f = func(l, r int) (res int64) {41 if l >= r {42 return 143 }44 dv := &dp[l][r]45 if *dv != -1 {46 return *dv47 }48 defer func() { *dv = res }()49 p := pMin[l][r]50 var sl, sr int6451 for i := l; i <= p; i++ {52 sl = (sl + f(l, i-1)*f(i, p-1)) % mod53 }54 for i := p; i <= r; i++ {55 sr = (sr + f(p+1, i)*f(i+1, r)) % mod56 }57 return sl * sr % mod58 }59 Fprint(out, f(0, n-1))60}61 6263