- 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 359D.go.
- The implementation visibly relies on sequence storage.
- 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 "sort"9)10 1112func CF359D(_r io.Reader, _w io.Writer) {13 in := bufio.NewReader(_r)14 out := bufio.NewWriter(_w)15 defer out.Flush()16 type pair struct{ mi, g int }17 gcd := func(a, b int) int {18 for a != 0 {19 a, b = b%a, a20 }21 return b22 }23 min := func(a, b int) int {24 if a < b {25 return a26 }27 return b28 }29 30 var n, v int31 Fscan(in, &n)32 st := make([][19]pair, n)33 for i := range st {34 Fscan(in, &v)35 st[i][0] = pair{v, v}36 }37 for j := 1; 1<<j <= n; j++ {38 for i := 0; i+1<<j <= n; i++ {39 p, q := st[i][j-1], st[i+1<<(j-1)][j-1]40 st[i][j] = pair{min(p.mi, q.mi), gcd(p.g, q.g)}41 }42 }43 query := func(l, r int) bool {44 k := bits.Len(uint(r-l)) - 145 p, q := st[l][k], st[r-1<<k][k]46 return min(p.mi, q.mi) == gcd(p.g, q.g)47 }48 49 ans := sort.Search(n+1, func(sz int) bool {50 for r := sz; r <= n; r++ {51 if query(r-sz, r) {52 return false53 }54 }55 return true56 }) - 157 ls := []int{}58 for r := ans; r <= n; r++ {59 if query(r-ans, r) {60 ls = append(ls, r-ans+1)61 }62 }63 Fprintln(out, len(ls), ans-1)64 for _, l := range ls {65 Fprint(out, l, " ")66 }67}68 6970