- 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
- 56 lines of Go from the credited upstream file 294C.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 "sort"8)9 1011func cf294C(_r io.Reader, out io.Writer) {12 in := bufio.NewReader(_r)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 const mx int = 100025 F := make([]int, mx+1)26 F[0] = 127 for i := 1; i <= mx; i++ {28 F[i] = F[i-1] * i % mod29 }30 invF := make([]int, mx+1)31 invF[mx] = pow(F[mx], mod-2)32 for i := mx; i > 0; i-- {33 invF[i-1] = invF[i] * i % mod34 }35 36 var n, m int37 Fscan(in, &n, &m)38 a := make([]int, m)39 for i := range a {40 Fscan(in, &a[i])41 }42 sort.Ints(a)43 ans := F[n-m] * invF[a[0]-1] % mod * invF[n-a[m-1]] % mod44 e := 045 for i := 1; i < m; i++ {46 d := a[i] - a[i-1] - 147 if d > 0 {48 ans = ans * invF[d] % mod49 e += d - 150 }51 }52 Fprint(out, ans*pow(2, e)%mod)53}54 5556