- 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
- 71 lines of Go from the credited upstream file 431D.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 . "fmt"5 "io"6 "strconv"7)8 910func CF431D(in io.Reader, out io.Writer) {11 search := func(l, r int64, f func(int64) bool) int64 {12 for l < r {13 m := (l + r) >> 114 if f(m) {15 r = m16 } else {17 l = m + 118 }19 }20 return l21 }22 23 var tar int6424 var k int25 Fscan(in, &tar, &k)26 calc := func(s string) int64 {27 const lowerC, upperC byte = '0', '1'28 n := len(s)29 dp := make([][]int64, n)30 for i := range dp {31 dp[i] = make([]int64, k+1)32 for j := range dp[i] {33 dp[i][j] = -134 }35 }36 var f func(p, c1 int, limitUp bool) int6437 f = func(p, c1 int, limitUp bool) (res int64) {38 if c1 > k {39 return40 }41 if p == n {42 if c1 == k {43 return 144 }45 return46 }47 if !limitUp {48 dv := &dp[p][c1]49 if *dv >= 0 {50 return *dv51 }52 defer func() { *dv = res }()53 }54 up := upperC55 if limitUp {56 up = s[p]57 }58 for d := lowerC; d <= up; d++ {59 res += f(p+1, c1+int(d&1), limitUp && d == up)60 }61 return62 }63 return f(0, 0, true)64 }65 Fprint(out, search(1, 1e18, func(n int64) bool {66 return calc(strconv.FormatInt(n*2, 2))-calc(strconv.FormatInt(n, 2)) >= tar67 }))68}69 7071