- 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
- 65 lines of Go from the credited upstream file 2111F.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 cf2111F(in io.Reader, _w io.Writer) {11 out := bufio.NewWriter(_w)12 defer out.Flush()13 var T, p, s int14 for Fscan(in, &T); T > 0; T-- {15 Fscan(in, &p, &s)16 r := p / s17 if r > 4 {18 Fprintln(out, -1)19 } else if r == 4 || r == 3 {20 if p%s > 0 {21 Fprintln(out, -1)22 } else {23 k := 5 - r24 Fprintln(out, k)25 for x := range k {26 Fprintln(out, x, 0)27 }28 }29 } else if r == 2 && p%s > 0 {30 if s*2%(p-s*2) > 0 {31 Fprintln(out, -1)32 } else {33 k := s * 2 / (p - s*2)34 Fprintln(out, k)35 for x := range k {36 Fprintln(out, x, 0)37 }38 }39 } else {40 const sz = 50 * 441 k := (sz*2-1)/p + 142 base := p*k - 143 s *= k * 244 Fprintln(out, s)45 for x := range sz {46 Fprintln(out, x, 0)47 }48 for y := 1; y <= base-sz; y++ {49 Fprintln(out, 0, y)50 }51 need := s - base52 for y := 1; y <= need/(sz-1); y++ {53 for x := 1; x < sz; x++ {54 Fprintln(out, x, y)55 }56 }57 for x := 1; x <= need%(sz-1); x++ {58 Fprintln(out, x, need/(sz-1)+1)59 }60 }61 }62}63 6465