- 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
- 66 lines of Go from the credited upstream file 855E.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 "strconv"8)9 1011func CF855E(_r io.Reader, _w io.Writer) {12 in := bufio.NewReader(_r)13 out := bufio.NewWriter(_w)14 defer out.Flush()15 16 var q, b int17 var l, r int6418 var s string19 dp := [11][60][]int64{}20 for i := range dp {21 for j := range dp[i] {22 dp[i][j] = make([]int64, 1<<i)23 for k := range dp[i][j] {24 dp[i][j][k] = -125 }26 }27 }28 var f func(int, int, bool, bool) int6429 f = func(p, mask int, limitUp, hasD bool) (res int64) {30 if p < 0 {31 if mask == 0 && hasD {32 return 133 }34 return35 }36 if !limitUp && hasD {37 dv := &dp[b][p][mask]38 if *dv >= 0 {39 return *dv40 }41 defer func() { *dv = res }()42 }43 up := byte(b - 1)44 if limitUp {45 up = s[len(s)-1-p] & 1546 }47 for ch := byte(0); ch <= up; ch++ {48 tmp := mask49 if hasD || ch > 0 {50 tmp ^= 1 << ch51 }52 res += f(p-1, tmp, limitUp && ch == up, hasD || ch > 0)53 }54 return55 }56 for Fscan(in, &q); q > 0; q-- {57 Fscan(in, &b, &l, &r)58 s = strconv.FormatInt(r, b)59 ans := f(len(s)-1, 0, true, false)60 s = strconv.FormatInt(l-1, b)61 Fprintln(out, ans-f(len(s)-1, 0, true, false))62 }63}64 6566