- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 70 lines of Go from the credited upstream file 296B.go.
- The implementation keeps its working state in language-native values and containers.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
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 cf296B(in io.Reader, out io.Writer) {11 const mod = 1_000_000_00712 var n int13 var s, t string14 var le, gr bool15 Fscan(bufio.NewReader(in), &n, &s, &t)16 all, same := 1, 117 for i := range s {18 v, w := s[i], t[i]19 if v == '?' || w == '?' {20 if v == '?' {21 all = all * 10 % mod22 }23 if w == '?' {24 all = all * 10 % mod25 }26 if v == '?' && w == '?' {27 same = same * 10 % mod28 }29 } else if v < w {30 le = true31 } else if v > w {32 gr = true33 }34 }35 if le && gr {36 Fprint(out, all)37 return38 }39 40 res1, res2 := 1, 141 for i, v := range s {42 w := t[i]43 if v == '?' {44 if w == '?' {45 res1 = res1 * 55 % mod46 res2 = res2 * 55 % mod47 } else {48 res1 = res1 * int(w-'0'+1) % mod49 res2 = res2 * int('9'-w+1) % mod50 }51 } else if w == '?' {52 res1 = res1 * int('9'-v+1) % mod53 res2 = res2 * int(v-'0'+1) % mod54 }55 }56 57 if !gr {58 all -= res159 }60 if !le {61 all -= res262 }63 if !le && !gr {64 all += same65 }66 Fprint(out, (all%mod+mod)%mod)67}68 6970