- 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
- 78 lines of Go from the credited upstream file 1762E.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 mod62 = 99824435311 12func pow62(x, n int) (res int) {13 res = 114 for ; n > 0; n /= 2 {15 if n%2 > 0 {16 res = res * x % mod6217 }18 x = x * x % mod6219 }20 return21}22 23type comb62 struct{ _f, _invF []int }24 25func newComb62(mx int) *comb62 {26 c := &comb62{[]int{1}, []int{1}}27 c._grow(mx)28 return c29}30 31func (c *comb62) _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 % mod6236 }37 c._invF = slices.Grow(c._invF, mx+1)[:mx+1]38 c._invF[mx] = pow62(c._f[mx], mod62-2)39 for i := mx; i > n; i-- {40 c._invF[i-1] = c._invF[i] * i % mod6241 }42}43 44func (c *comb62) 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 *comb62) 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 *comb62) c(n, k int) int {59 if k < 0 || k > n {60 return 061 }62 return c.f(n) * c.invF(k) % mod62 * c.invF(n-k) % mod6263}64 65func cf1762E(in io.Reader, out io.Writer) {66 cm := newComb62(0)67 var n int68 Fscan(in, &n)69 ans := 070 for i := 1; i < n; i++ {71 res := pow62(i, i-1) * pow62(n-i, n-i-1) % mod62 * cm.c(n-2, i-1) % mod6272 ans += (1 - i%2*2) * res73 }74 Fprint(out, (ans%mod62+mod62)%mod62)75}76 7778