- 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
- 95 lines of Go from the credited upstream file 611D.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 "index/suffixarray"7 "io"8 "math/bits"9 "reflect"10 "unsafe"11)12 1314func CF611D(in io.Reader, out io.Writer) {15 const mod int = 1e9 + 716 min := func(a, b int) int {17 if a < b {18 return a19 }20 return b21 }22 23 var n, ans int24 var s []byte25 Fscan(bufio.NewReader(in), &n, &s)26 sa := *(*[]int32)(unsafe.Pointer(reflect.ValueOf(suffixarray.New(s)).Elem().FieldByName("sa").Field(0).UnsafeAddr()))27 rank := make([]int, n)28 for i := range rank {29 rank[sa[i]] = i30 }31 height := make([]int, n)32 h := 033 for i, rk := range rank {34 if h > 0 {35 h--36 }37 if rk > 0 {38 for j := int(sa[rk-1]); i+h < n && j+h < n && s[i+h] == s[j+h]; h++ {39 }40 }41 height[rk] = h42 }43 const mx = 1344 st := make([][mx]int, n)45 for i, v := range height {46 st[i][0] = v47 }48 for j := 1; 1<<j <= n; j++ {49 for i := 0; i+1<<j <= n; i++ {50 st[i][j] = min(st[i][j-1], st[i+1<<(j-1)][j-1])51 }52 }53 _q := func(l, r int) int { k := bits.Len(uint(r-l)) - 1; return min(st[l][k], st[r-1<<k][k]) }54 lcp := func(i, j int) int {55 if i == j {56 return n - i57 }58 ri, rj := rank[i], rank[j]59 if ri > rj {60 ri, rj = rj, ri61 }62 return _q(ri+1, rj+1)63 }64 65 dp := make([][]int, n)66 for i := range dp {67 dp[i] = make([]int, n)68 }69 for j := 0; j < n; j++ {70 dp[0][j] = 171 }72 for i := 1; i < n; i++ {73 if s[i] == '0' {74 continue75 }76 for j, k, sum := i, i-1, 0; j < n; j++ {77 dp[i][j] = sum78 if k < 0 {79 continue80 }81 if s[k] > '0' && rank[k] < rank[i] && lcp(k, i) < i-k {82 dp[i][j] = (dp[i][j] + dp[k][i-1]) % mod83 }84 sum = (sum + dp[k][i-1]) % mod85 k--86 }87 }88 for _, d := range dp {89 ans = (ans + d[n-1]) % mod90 }91 Fprint(out, ans)92}93 9495