- 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
- 68 lines of Go from the credited upstream file 1039C.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 CF1039C(_r io.Reader, out io.Writer) {11 in := bufio.NewReader(_r)12 const mod int64 = 1e9 + 713 var n, m, k, v, w int14 Fscan(in, &n, &m, &k)15 a := make([]int64, n)16 for i := range a {17 Fscan(in, &a[i])18 }19 type edge struct{ v, w int }20 mp := map[int64][]edge{}21 for ; m > 0; m-- {22 Fscan(in, &v, &w)23 v--24 w--25 s := a[v] ^ a[w]26 mp[s] = append(mp[s], edge{v, w})27 }28 p2 := make([]int64, n+1)29 p2[0] = 130 for i := 1; i <= n; i++ {31 p2[i] = p2[i-1] * 2 % mod32 }33 34 ans := (int64(1)<<k - int64(len(mp))) % mod * p2[n] % mod35 vis := make([]int64, n+1)36 for s, es := range mp {37 vs := map[int]struct{}{}38 g := map[int][]int{}39 for _, e := range es {40 v, w := e.v, e.w41 vs[v] = struct{}{}42 vs[w] = struct{}{}43 g[v] = append(g[v], w)44 g[w] = append(g[w], v)45 }46 cc := n - len(vs)47 var f func(int)48 f = func(v int) {49 vis[v] = s50 for _, w := range g[v] {51 if vis[w] != s {52 f(w)53 }54 }55 }56 for v := range vs {57 if vis[v] != s {58 cc++59 f(v)60 }61 }62 ans += p2[cc]63 }64 Fprint(out, ans%mod)65}66 6768