- 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 1237F.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)7 8func cf1237F(in io.Reader, out io.Writer) {9 const mod = 99824435310 const mx = 360011 pow := func(x, n int) int {12 res := 113 for ; n > 0; n /= 2 {14 if n%2 > 0 {15 res = res * x % mod16 }17 x = x * x % mod18 }19 return res20 }21 F := [mx + 1]int{1}22 for i := 1; i <= mx; i++ {23 F[i] = F[i-1] * i % mod24 }25 invF := [...]int{mx: pow(F[mx], mod-2)}26 for i := mx; i > 0; i-- {27 invF[i-1] = invF[i] * i % mod28 }29 perm := func(n, k int) int {30 return F[n] * invF[n-k] % mod31 }32 33 var n, m, k, r1, c1, r2, c2, ans int34 Fscan(in, &n, &m, &k)35 banR := make([]bool, n)36 banC := make([]bool, m)37 for ; k > 0; k-- {38 Fscan(in, &r1, &c1, &r2, &c2)39 banR[r1-1] = true40 banR[r2-1] = true41 banC[c1-1] = true42 banC[c2-1] = true43 }44 45 calc := func(ban []bool) ([]int, int) {46 n := len(ban)47 f := make([][]int, n+1)48 for i := range f {49 f[i] = make([]int, n/2+1)50 f[i][0] = 151 }52 for i := 1; i < n; i++ {53 for j := 1; j <= (i+1)/2; j++ {54 f[i+1][j] = f[i][j]55 if !ban[i] && !ban[i-1] {56 f[i+1][j] = (f[i+1][j] + f[i-1][j-1]) % mod57 }58 }59 }60 empty := 061 for _, b := range ban {62 if !b {63 empty++64 }65 }66 return f[n], empty67 }68 69 f, emptyR := calc(banR)70 g, emptyC := calc(banC)71 for i, v := range f { 72 for j, w := range g { 73 if j > emptyR-i*2 || i > emptyC-j*2 {74 break75 }76 ans = (ans + v*w%mod*perm(emptyR-i*2, j)%mod*perm(emptyC-j*2, i)) % mod77 }78 }79 Fprint(out, ans)80}81 8283