- 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
- 57 lines of Go from the credited upstream file 1342E.go.
- The implementation keeps its working state in language-native values and containers.
- 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)7 89func CF1342E(in io.Reader, out io.Writer) {10 const mod = 99824435311 const mx int = 2e512 F := [mx + 1]int64{1}13 for i := 1; i <= mx; i++ {14 F[i] = F[i-1] * int64(i) % mod15 }16 pow := func(x int64, n int) (res int64) {17 res = 118 for ; n > 0; n >>= 1 {19 if n&1 == 1 {20 res = res * x % mod21 }22 x = x * x % mod23 }24 return res25 }26 invF := [...]int64{mx: pow(F[mx], mod-2)}27 for i := mx; i > 0; i-- {28 invF[i-1] = invF[i] * int64(i) % mod29 }30 C := func(n, k int) int64 { return F[n] * invF[k] % mod * invF[n-k] % mod }31 32 var n int33 var kk, ans int6434 Fscan(in, &n, &kk)35 if kk >= int64(n) {36 Fprint(out, 0)37 return38 }39 k := int(kk)40 41 c := n - k42 for i := c; i >= 0; i-- {43 if i&1 == c&1 {44 ans = (ans + pow(int64(i), n)*C(c, i)) % mod45 } else {46 ans = (ans + mod - pow(int64(i), n)*C(c, i)%mod) % mod47 }48 }49 ans = ans * C(n, c) % mod50 if k > 0 {51 ans = ans * 2 % mod52 }53 Fprint(out, ans)54}55 5657