- 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
- 63 lines of Go from the credited upstream file 1548B.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 CF1548B(_r io.Reader, _w io.Writer) {13 in := bufio.NewReader(_r)14 out := bufio.NewWriter(_w)15 defer out.Flush()16 abs := func(x int64) int64 {17 if x < 0 {18 return -x19 }20 return x21 }22 gcd := func(a, b int64) int64 {23 for a != 0 {24 a, b = b%a, a25 }26 return b27 }28 max := func(a, b int) int {29 if a > b {30 return a31 }32 return b33 }34 35 var T, n int36 var pre, v int6437 for Fscan(in, &T); T > 0; T-- {38 Fscan(in, &n, &pre)39 n--40 st := make([][18]int64, n)41 for i := range st {42 Fscan(in, &v)43 st[i][0] = abs(v - pre)44 pre = v45 }46 for j := 1; 1<<j <= n; j++ {47 for i := 0; i+1<<j <= n; i++ {48 st[i][j] = gcd(st[i][j-1], st[i+1<<(j-1)][j-1])49 }50 }51 52 ans := 153 for r := 1; r <= n; r++ {54 55 l := sort.Search(r, func(l int) bool { k := bits.Len(uint(r-l)) - 1; return gcd(st[l][k], st[r-1<<k][k]) > 1 })56 ans = max(ans, r-l+1)57 }58 Fprintln(out, ans)59 }60}61 6263