- 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
- 85 lines of Go from the credited upstream file 1930E.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 "slices"8)9 1011const mod30 = 99824435312 13func pow30(x, n int) (res int) {14 res = 115 for ; n > 0; n /= 2 {16 if n%2 > 0 {17 res = res * x % mod3018 }19 x = x * x % mod3020 }21 return22}23 24type comb30 struct{ _f, _invF []int }25 26func newComb30(mx int) *comb30 {27 c := &comb30{[]int{1}, []int{1}}28 c._grow(mx)29 return c30}31 32func (c *comb30) _grow(mx int) {33 n := len(c._f)34 c._f = slices.Grow(c._f, mx+1)[:mx+1]35 for i := n; i <= mx; i++ {36 c._f[i] = c._f[i-1] * i % mod3037 }38 c._invF = slices.Grow(c._invF, mx+1)[:mx+1]39 c._invF[mx] = pow30(c._f[mx], mod30-2)40 for i := mx; i > n; i-- {41 c._invF[i-1] = c._invF[i] * i % mod3042 }43}44 45func (c *comb30) f(n int) int {46 if n >= len(c._f) {47 c._grow(n * 2)48 }49 return c._f[n]50}51 52func (c *comb30) invF(n int) int {53 if n >= len(c._f) {54 c._grow(n * 2)55 }56 return c._invF[n]57}58 59func (c *comb30) c(n, k int) int {60 if k < 0 || k > n {61 return 062 }63 return c.f(n) * c.invF(k) % mod30 * c.invF(n-k) % mod3064}65 66func cf1930E(in io.Reader, _w io.Writer) {67 out := bufio.NewWriter(_w)68 defer out.Flush()69 cm := newComb30(0)70 var T, n int71 for Fscan(in, &T); T > 0; T-- {72 Fscan(in, &n)73 for k := 1; k*2 < n; k++ {74 ans := 175 for m := 1; m*k*2 < n; m++ {76 ans += cm.c(n, m*k*2) - cm.c(n-(m-1)*k*2-1, k*2-1)77 }78 Fprint(out, (ans%mod30+mod30)%mod30, " ")79 }80 Fprintln(out)81 }82}83 8485