- 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
- 99 lines of Go from the credited upstream file 1028H.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)9 1011 1213func cf1028H(in io.Reader, _w io.Writer) {14 out := bufio.NewWriter(_w)15 defer out.Flush()16 const mx = 503210817 lpf := [mx]int{}18 for i := 2; i < mx; i++ {19 if lpf[i] == 0 {20 for j := i; j < mx; j += i {21 if lpf[j] == 0 {22 lpf[j] = i23 }24 }25 }26 }27 28 var n, q int29 Fscan(in, &n, &q)30 a := make([]int, n)31 for i := range a {32 Fscan(in, &a[i])33 }34 type query struct{ l, i int }35 qs := make([][]query, n)36 for i := range q {37 var l, r int38 Fscan(in, &l, &r)39 qs[r-1] = append(qs[r-1], query{l, i})40 }41 42 ans := make([]int, q)43 const mxW = 744 opToI := make([]int, mxW*2+1)45 maxI := [mx][mxW + 1]int{}46 ps := []int{}47 mul := [1 << mxW]int{1}48 for i, v := range a {49 ps = ps[:0]50 for v > 1 {51 p := lpf[v]52 e := 153 for v /= p; v%p == 0; v /= p {54 e ^= 155 }56 if e > 0 {57 ps = append(ps, p)58 }59 }60 w := len(ps)61 62 for w2 := range mxW + 1 {63 op := w + w264 opToI[op] = max(opToI[op], maxI[1][w2])65 }66 maxI[1][w] = i + 167 68 for j, p := range ps {69 b := 1 << j70 for k, m := range mul[:b] {71 m *= p72 mul[b|k] = m73 74 common := bits.OnesCount8(uint8(b | k))75 for w2 := common; w2 <= mxW; w2++ {76 op := w + w2 - common*277 opToI[op] = max(opToI[op], maxI[m][w2])78 }79 maxI[m][w] = i + 180 }81 }82 83 for _, p := range qs[i] {84 for op, j := range opToI {85 if j >= p.l {86 ans[p.i] = op87 break88 }89 }90 }91 }92 93 for _, v := range ans {94 Fprintln(out, v)95 }96}97 9899