- 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
- 53 lines of Go from the credited upstream file 407C.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 cf407C(in io.Reader, _w io.Writer) {11 const mod = 1_000_000_00712 out := bufio.NewWriter(_w)13 defer out.Flush()14 var n, m, l, r, k int15 Fscan(in, &n, &m)16 a := make([]int, n)17 for i := range a {18 Fscan(in, &a[i])19 }20 21 const mx = 10022 c := make([][mx + 1]int, n+mx+1)23 for i := range c {24 c[i][0] = 125 for j := 1; j <= min(i, mx); j++ {26 c[i][j] = (c[i-1][j] + c[i-1][j-1]) % mod27 }28 }29 30 f := [mx + 2][]int{}31 for i := range f {32 f[i] = make([]int, n+2)33 }34 for range m {35 Fscan(in, &l, &r, &k)36 f[k][l]++37 for j := range k + 1 {38 f[j][r+1] = (f[j][r+1] - c[r-l+k-j][k-j]) % mod39 }40 }41 for i := mx; i >= 0; i-- {42 for j := 1; j <= n; j++ {43 f[i][j] = (f[i][j] + f[i][j-1] + f[i+1][j]) % mod44 }45 }46 47 for i, v := range a {48 Fprint(out, (f[0][i+1]+v+mod)%mod, " ")49 }50}51 5253