- 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
- 56 lines of Go from the credited upstream file 2075D.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 "math/bits"8)9 1011func cf2075D(in io.Reader, _w io.Writer) {12 out := bufio.NewWriter(_w)13 defer out.Flush()14 const u = 5815 var f, f2 [u][u]int16 for i := range f {17 for j := range f[i] {18 f[i][j] = 8e1819 f2[i][j] = 8e1820 }21 }22 f[0][0] = 023 f2[0][0] = 024 for i := 1; i < u; i++ {25 for j := u - 1; j >= 0; j-- {26 for k := u - 1; k >= 0; k-- {27 28 f[j][k] = min(f[j][k], f[j][max(k-i, 0)]+1<<i, f[max(j-i, 0)][k]+1<<i)29 30 if k >= i {31 f2[j][k] = min(f2[j][k], f2[j][k-i]+1<<i)32 }33 if j >= i {34 f2[j][k] = min(f2[j][k], f2[j-i][k]+1<<i)35 }36 }37 }38 }39 40 var T, x, y int41 for Fscan(in, &T); T > 0; T-- {42 Fscan(in, &x, &y)43 if x > y {44 x, y = y, x45 }46 n, m := bits.Len(uint(x)), bits.Len(uint(y))47 ans := f[n][m]48 for a := bits.Len(uint(x ^ y>>(m-n))); a < n; a++ {49 ans = min(ans, f2[a][a+m-n])50 }51 Fprintln(out, ans)52 }53}54 5556