- 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
- 65 lines of Go from the credited upstream file 840C.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 89func cf840C(in io.Reader, out io.Writer) {10 const M = 1_000_000_00711 const mx = 30112 C := [mx][mx]int{}13 for i := range C {14 C[i][0] = 115 for j := 1; j <= i; j++ {16 C[i][j] = (C[i-1][j-1] + C[i-1][j]) % M17 }18 }19 20 var n, v, s int21 Fscan(in, &n)22 cnt := map[int]int{}23 perm := 124 for range n {25 Fscan(in, &v)26 for j := 2; j*j <= v; j++ {27 for v%(j*j) == 0 {28 v /= j * j29 }30 }31 cnt[v]++32 perm = perm * cnt[v] % M33 }34 35 f := make([][]int, n+1)36 for i := range f {37 f[i] = make([]int, n+1)38 }39 f[0][0] = perm40 41 i := 042 for _, c := range cnt {43 44 for bad, dp := range f[i][:s+1] {45 if dp == 0 {46 continue47 }48 for b := 1; b <= c; b++ {49 for j := range min(bad, b) + 1 {50 51 52 53 54 f[i+1][bad-j+c-b] = (f[i+1][bad-j+c-b] + dp*C[c-1][b-1]%M*C[bad][j]%M*C[s+1-bad][b-j]) % M55 }56 }57 }58 i++59 s += c60 }61 Fprint(out, f[i][0])62}63 6465