- 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
- 83 lines of Go from the credited upstream file 285E.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 mod85 = 1_000_000_00711 12func pow85(x, n int) (res int) {13 res = 114 for ; n > 0; n /= 2 {15 if n%2 > 0 {16 res = res * x % mod8517 }18 x = x * x % mod8519 }20 return21}22 23type comb85 struct{ _f, _invF []int }24 25func newComb85(mx int) *comb85 {26 c := &comb85{[]int{1}, []int{1}}27 c._grow(mx)28 return c29}30 31func (c *comb85) _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 % mod8536 }37 c._invF = slices.Grow(c._invF, mx+1)[:mx+1]38 c._invF[mx] = pow85(c._f[mx], mod85-2)39 for i := mx; i > n; i-- {40 c._invF[i-1] = c._invF[i] * i % mod8541 }42}43 44func (c *comb85) 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 *comb85) 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 *comb85) c(n, k int) int {59 return c.f(n) * c.invF(k) % mod85 * c.invF(n-k) % mod8560}61 62func cf285E(in io.Reader, out io.Writer) {63 cm := newComb85(0)64 var n, k, ans int65 Fscan(in, &n, &k)66 f := make([]int, n+1)67 for i := range n/2 + 1 {68 for j := range n/2 + 1 {69 f[i+j] = (f[i+j] + cm.c(n-i, i)*cm.c(n-j, j)) % mod8570 }71 }72 for i := range n + 1 {73 f[i] = f[i] * cm.f(n-i) % mod8574 }75 for i := k; i <= n; i++ {76 sign := 1 - (i-k)%2*277 ans = (ans + sign*f[i]*cm.c(i, k)) % mod8578 }79 Fprint(out, (ans+mod85)%mod85)80}81 8283