- 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
- 96 lines of Go from the credited upstream file 2150D.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 . "fmt"5 "io"6 "slices"7)8 910const mod50 = 99824435311 12func pow50(x, n int) (res int) {13 res = 114 for ; n > 0; n /= 2 {15 if n%2 > 0 {16 res = res * x % mod5017 }18 x = x * x % mod5019 }20 return21}22 23type comb50 struct{ _f, _invF []int }24 25func newComb50(mx int) *comb50 {26 c := &comb50{[]int{1}, []int{1}}27 c._grow(mx)28 return c29}30 31func (c *comb50) _grow(mx int) {32 n := len(c._f)33 c._f = slices.Grow(c._f, mx+1)[:mx+1]34 for i := n; i <= mx; i++ {35 c._f[i] = c._f[i-1] * i % mod5036 }37 c._invF = slices.Grow(c._invF, mx+1)[:mx+1]38 c._invF[mx] = pow50(c._f[mx], mod50-2)39 for i := mx; i > n; i-- {40 c._invF[i-1] = c._invF[i] * i % mod5041 }42}43 44func (c *comb50) f(n int) int {45 if n >= len(c._f) {46 c._grow(n * 2)47 }48 return c._f[n]49}50 51func (c *comb50) invF(n int) int {52 if n >= len(c._f) {53 c._grow(n * 2)54 }55 return c._invF[n]56}57 58func (c *comb50) c(n, k int) int {59 if k < 0 || k > n {60 return 061 }62 return c.f(n) * c.invF(k) % mod50 * c.invF(n-k) % mod5063}64 65func cf2150D(in io.Reader, out io.Writer) {66 var cm = newComb50(0)67 var T, n int68 for Fscan(in, &T); T > 0; T-- {69 Fscan(in, &n)70 a := make([]int, n+1)71 s := make([]int, n+1)72 for i := 1; i <= n; i++ {73 Fscan(in, &a[i])74 a[i] = (a[i] + a[i-1]) % mod5075 s[i] = (s[i-1] + a[i]) % mod5076 }77 78 ans := a[n] * n % mod5079 for x := range 2 {80 for y := range 2 {81 for l := 2; l <= n-x-y; l++ {82 if (l+n-x-y)&1 > 0 {83 continue84 }85 f := cm.c((l+n-x-y-2)>>1, l-1)86 g := f * (n - x - y) % mod50 * cm.invF(l) % mod50 * cm.f(l-1) % mod5087 ans = (ans + g*(s[n]-s[l-1]-s[n-l]) + f*(x*a[n-l+1]+y*(a[n]-a[l-1]))) % mod5088 }89 }90 }91 Fprintln(out, (ans+mod50)%mod50)92 }93}94 9596