- 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
- 74 lines of Go from the credited upstream file 1257F.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 Sol1257F(reader io.Reader, writer io.Writer) {12 in := bufio.NewReader(reader)13 out := bufio.NewWriter(writer)14 defer out.Flush()15 16 var n int17 Fscan(in, &n)18 op := [30][100]int8{}19 baseOnes := [100]int8{}20 for j := 0; j < n; j++ {21 var v uint22 Fscan(in, &v)23 for i := range op {24 if v>>uint(i)&1 == 0 {25 op[i][j] = 126 } else {27 op[i][j] = -128 }29 }30 baseOnes[j] = int8(bits.OnesCount(v))31 }32 33 for ones := int8(0); ones <= 30; ones++ {34 mp := map[[100]int8]uint{}35 for i := uint(0); i < 1<<15; i++ {36 sum := [100]int8{}37 for j := uint(0); j < 15; j++ {38 if i>>j&1 == 1 {39 for k, v := range op[j] {40 sum[k] += v41 }42 }43 }44 mp[sum] = i45 }46 curOnes := [100]int8{}47 for i := 0; i < n; i++ {48 curOnes[i] = ones - baseOnes[i]49 }50 for i := uint(0); i < 1<<15; i++ {51 curOnesCopy := [100]int8{}52 for j, v := range curOnes {53 curOnesCopy[j] = v54 }55 for j := uint(0); j < 15; j++ {56 if i>>j&1 == 1 {57 for k, v := range op[j+15] {58 curOnesCopy[k] -= v59 }60 }61 }62 if v, ok := mp[curOnesCopy]; ok {63 Fprint(out, i<<15|v)64 return65 }66 }67 }68 Fprint(out, -1)69}70 71727374