Approach
Depth-first search
For Codeforces 1073E — Segment Sum, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.
- Define the state carried into one recursive or stack frame.
- Mark or choose the current state before exploring children.
- Combine child results or undo the choice when the branch finishes.
Code notes
- 70 lines of Go from the credited upstream file 1073E.go.
- The implementation visibly relies on sequence storage, cached states.
- No explicit loop blocks detected, together with recursive traversal.
Complexity
Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.
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 "math"7 "math/bits"8)9 1011func cf1073E(in io.Reader, out io.Writer) {12 const mod = 99824435313 var lowS, highS string14 var k int15 Fscan(in, &lowS, &highS, &k)16 n := len(highS)17 diffLH := n - len(lowS)18 type pair struct{ cnt, sum int }19 memo := make([][1 << 10]pair, n)20 for i := range memo {21 for j := range memo[i] {22 memo[i][j].cnt = -123 }24 }25 26 var dfs func(int, int, bool, bool) pair27 dfs = func(i, mask int, limitLow, limitHigh bool) (res pair) {28 if i == n {29 return pair{1, 0}30 }31 if !limitLow && !limitHigh {32 dv := &memo[i][mask]33 if dv.cnt >= 0 {34 return *dv35 }36 defer func() { *dv = res }()37 }38 39 lo := 040 if limitLow && i >= diffLH {41 lo = int(lowS[i-diffLH] - '0')42 }43 hi := 944 if limitHigh {45 hi = int(highS[i] - '0')46 }47 48 d := lo49 if limitLow && i < diffLH {50 res = dfs(i+1, 0, true, false)51 d = 152 }53 54 for ; d <= hi; d++ {55 newMask := mask | 1<<d56 if bits.OnesCount(uint(newMask)) > k {57 continue58 }59 sub := dfs(i+1, newMask, limitLow && d == lo, limitHigh && d == hi)60 res.cnt = (res.cnt + sub.cnt) % mod61 v := d * int(math.Pow10(n-1-i)) % mod62 res.sum = (res.sum + sub.sum + v*sub.cnt) % mod63 }64 return65 }66 Fprint(out, dfs(0, 0, true, true).sum)67}68 6970