- 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
- 90 lines of Go from the credited upstream file 145C.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 "strconv"8 "strings"9)10 1112const mod45 = 1_000_000_00713 14type comb45 struct{ _f, _invF []int }15 16func (c *comb45) _grow(mx int) {17 pow := func(x, n int) int {18 res := 119 for ; n > 0; n /= 2 {20 if n%2 > 0 {21 res = res * x % mod4522 }23 x = x * x % mod4524 }25 return res26 }27 n := len(c._f)28 c._f = slices.Grow(c._f, mx+1)[:mx+1]29 for i := n; i <= mx; i++ {30 c._f[i] = c._f[i-1] * i % mod4531 }32 c._invF = slices.Grow(c._invF, mx+1)[:mx+1]33 c._invF[mx] = pow(c._f[mx], mod45-2)34 for i := mx; i > n; i-- {35 c._invF[i-1] = c._invF[i] * i % mod4536 }37}38 39func (c *comb45) f(n int) int {40 if n >= len(c._f) {41 c._grow(n * 2)42 }43 return c._f[n]44}45 46func (c *comb45) invF(n int) int {47 if n >= len(c._f) {48 c._grow(n * 2)49 }50 return c._invF[n]51}52 53func (c *comb45) c(n, k int) int {54 if k < 0 || k > n {55 return 056 }57 return c.f(n) * c.invF(k) % mod45 * c.invF(n-k) % mod4558}59 60func cf145C(in io.Reader, out io.Writer) {61 var n, k, tot, ans int62 var s string63 Fscan(in, &n, &k)64 cnt := map[int]int{}65 for range n {66 Fscan(in, &s)67 if strings.Count(s, "4")+strings.Count(s, "7") == len(s) {68 v, _ := strconv.Atoi(s)69 cnt[v]++70 tot++71 }72 }73 74 f := make([]int, min(tot, k)+1)75 f[0] = 176 for _, c := range cnt {77 for j := len(f) - 1; j > 0; j-- {78 f[j] = (f[j] + f[j-1]*c) % mod4579 }80 }81 82 cm := &comb45{[]int{1}, []int{1}}83 for i, v := range f {84 ans = (ans + v*cm.c(n-tot, k-i)) % mod4585 }86 Fprint(out, ans)87}88 8990