- 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 1516E.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 cf1516E(in io.Reader, _w io.Writer) {11 out := bufio.NewWriter(_w)12 defer out.Flush()13 const mod = 1_000_000_00714 pow := func(x, n int) int {15 res := 116 for ; n > 0; n /= 2 {17 if n%2 > 0 {18 res = res * x % mod19 }20 x = x * x % mod21 }22 return res23 }24 25 var n, k int26 Fscan(in, &n, &k)27 b := make([]int, 2*k+1)28 b[0] = 129 for i := 1; i <= 2*k; i++ {30 b[i] = b[i-1] * (n - i + 1) % mod * pow(i, mod-2) % mod31 }32 33 s := make([][]int, 2*k-1)34 for i := range s {35 s[i] = make([]int, k)36 }37 s[0][0] = 138 for i := 1; i < 2*k-1; i++ {39 for j := 0; j <= i/2; j++ {40 s[i][j] = (i + 1) * s[i-1][j] % mod41 if j > 0 {42 s[i][j] = (s[i][j] + (i+1)*s[i-2][j-1]) % mod43 }44 }45 }46 47 d := make([]int, k+1)48 d[0] = 149 if k >= 1 {50 d[1] = n * (n - 1) % mod * pow(2, mod-2) % mod51 }52 for i := 2; i <= k; i++ {53 d[i] = d[i-2]54 for t := 1; t <= i; t++ {55 d[i] = (d[i] + s[i+t-2][t-1]*b[i+t]) % mod56 }57 }58 59 for i := 1; i <= k; i++ {60 Fprint(out, d[i], " ")61 }62}63 6465