- 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
- 94 lines of Go from the credited upstream file 1780D.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 "math/bits"7 "os"8)9 1011type interaction80 interface {12 readInitData() initData8013 query(request80) response8014 printAnswer(answer80)15}16 17type stdIO80 struct {18 in *bufio.Reader19 out *bufio.Writer20}21 22type (23 initData80 struct{ n int }24 request80 struct{ q int }25 response80 struct{ res int }26 answer80 struct{ ans int }27)28 29func (io stdIO80) readInitData() initData80 {30 in := io.in31 32 var n int33 Fscan(in, &n)34 return initData80{n}35}36 37func (io stdIO80) query(q request80) (resp response80) {38 in, out := io.in, io.out39 40 Fprintln(out, "-", q.q)41 out.Flush()42 43 Fscan(in, &resp.res)44 45 if resp.res < 0 {46 panic(-1)47 }48 return49}50 51func (io stdIO80) printAnswer(a answer80) {52 out := io.out53 54 Fprintln(out, "!", a.ans)55 56 out.Flush()57}58 59func doInteraction80(it interaction80) {60 dt := it.readInitData()61 init1 := dt.n62 63 q := func(q int) int {64 return it.query(request80{q}).res65 }66 67 var ans int68 defer func() { it.printAnswer(answer80{ans}) }()69 70 x := 171 pre := init172 for i := 0; i < init1; i++ {73 res := q(x)74 if res >= pre {75 x = 1 << (res - pre + bits.Len(uint(x)))76 }77 ans |= x78 pre = res79 }80}81 82func run80() {83 in := bufio.NewReader(os.Stdin)84 out := bufio.NewWriter(os.Stdout)85 86 T := 187 Fscan(in, &T)88 for ; T > 0; T-- {89 doInteraction80(stdIO80{in, out})90 }91}92 9394