- 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
- 54 lines of Go from the credited upstream file 1622D.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 "bufio"5 . "fmt"6 "io"7)8 910func CF1622D(in io.Reader, out io.Writer) {11 const mod, mx = 998244353, 500012 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, n int64) (res int64) {17 res = 118 for ; n > 0; n >>= 1 {19 if n&1 > 0 {20 res = res * x % mod21 }22 x = x * x % mod23 }24 return25 }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, k int33 var s string34 Fscan(bufio.NewReader(in), &n, &k, &s)35 pos := []int{}36 for i, b := range s {37 if b == '1' {38 pos = append(pos, i)39 }40 }41 if k == 0 || len(pos) < k {42 Fprint(out, 1)43 return44 }45 pos = append(pos, n)46 ans := C(pos[k], k)47 for i, p := range pos[:len(pos)-k-1] {48 ans += C(pos[i+k+1]-p-1, k) - C(pos[i+k]-p-1, k-1)49 }50 Fprint(out, (ans%mod+mod)%mod)51}52 5354