- 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
- 153 lines of Go from the credited upstream file 1895F.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)8 910const mod95 = 1_000_000_00711 12func pow95(x, n int) int {13 res := 114 for ; n > 0; n /= 2 {15 if n%2 > 0 {16 res = res * x % mod9517 }18 x = x * x % mod9519 }20 return res21}22 23func berlekampMassey95(a []int) (coef []int) {24 var preC []int25 preI, preD := -1, 026 for i, v := range a {27 d := v28 for j, c := range coef {29 d = (d - c*a[i-1-j]) % mod9530 }31 if d == 0 {32 continue33 }34 35 if preI < 0 {36 coef = make([]int, i+1)37 preI, preD = i, d38 continue39 }40 41 bias := i - preI42 oldLen := len(coef)43 newLen := bias + len(preC)44 var tmp []int45 if newLen > oldLen {46 tmp = slices.Clone(coef)47 coef = slices.Grow(coef, newLen-oldLen)[:newLen]48 }49 50 delta := d * pow95(preD, mod95-2) % mod9551 coef[bias-1] = (coef[bias-1] + delta) % mod9552 for j, c := range preC {53 coef[bias+j] = (coef[bias+j] - delta*c) % mod9554 }55 56 if newLen > oldLen {57 preC = tmp58 preI, preD = i, d59 }60 }61 62 return63}64 65func kitamasa95(coef, a []int, n int) (ans int) {66 defer func() { ans = (ans%mod95 + mod95) % mod95 }()67 if n < len(a) {68 return a[n]69 }70 71 k := len(coef)72 if k == 0 {73 return74 }75 if k == 1 {76 return a[0] * pow95(coef[0], n)77 }78 79 compose := func(a, b []int) []int {80 c := make([]int, k)81 for _, v := range a {82 for j, w := range b {83 c[j] = (c[j] + v*w) % mod9584 }85 bk1 := b[k-1]86 for j := k - 1; j > 0; j-- {87 b[j] = (b[j-1] + bk1*coef[j]) % mod9588 }89 b[0] = bk1 * coef[0] % mod9590 }91 return c92 }93 94 resC := make([]int, k)95 resC[0] = 196 c := make([]int, k)97 c[1] = 198 for ; n > 0; n /= 2 {99 if n%2 > 0 {100 resC = compose(c, resC)101 }102 c = compose(c, slices.Clone(c))103 }104 105 for i, c := range resC {106 ans = (ans + c*a[i]) % mod95107 }108 return109}110 111func cf1895F(in io.Reader, out io.Writer) {112 var T, n, x, k int113 for Fscan(in, &T); T > 0; T-- {114 Fscan(in, &n, &x, &k)115 if n == 1 {116 Fprintln(out, k)117 continue118 }119 120 ans := (x + k) * pow95(k*2+1, n-1)121 if x == 0 {122 Fprintln(out, ans%mod95)123 continue124 }125 126 f := make([]int, x)127 for i := range f {128 f[i] = 1129 }130 sum := make([]int, x+1)131 132 a := make([]int, x*2)133 for i := range a {134 for j, v := range f {135 sum[j+1] = sum[j] + v136 }137 for j := range f {138 f[j] = (sum[min(j+k+1, x)] - sum[max(j-k, 0)]) % mod95139 a[i] += f[j]140 }141 a[i] %= mod95142 }143 144 coef := berlekampMassey95(a)145 slices.Reverse(coef)146 ans -= kitamasa95(coef, a, n-2)147 148 Fprintln(out, (ans%mod95+mod95)%mod95)149 }150}151 152153