Approach
Depth-first search
For Codeforces 1616H — Keep XOR Low, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.
- Define the state carried into one recursive or stack frame.
- Mark or choose the current state before exploring children.
- Combine child results or undo the choice when the branch finishes.
Code notes
- 83 lines of Go from the credited upstream file 1616H.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected, together with recursive traversal.
Complexity
Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.
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)7 89func cf1616H(in io.Reader, out io.Writer) {10 type node struct {11 son [2]*node12 sz int13 }14 size := func(o *node) int {15 if o == nil {16 return 017 }18 return o.sz19 }20 root := &node{}21 22 var n, x, v int23 Fscan(in, &n, &x)24 for range n {25 Fscan(in, &v)26 o := root27 for i := 29; i >= 0; i-- {28 b := v >> i & 129 if o.son[b] == nil {30 o.son[b] = &node{}31 }32 o = o.son[b]33 o.sz++34 }35 }36 37 const mod = 99824435338 pow2 := make([]int, n+1)39 pow2[0] = 140 for i := 1; i <= n; i++ {41 pow2[i] = pow2[i-1] * 2 % mod42 }43 44 var dfs func(*node, *node, int) int45 dfs = func(p, q *node, d int) int {46 if p == nil || q == nil {47 return pow2[size(p)+size(q)]48 }49 if d < 0 {50 if p == q {51 return pow2[p.sz]52 }53 return pow2[p.sz+q.sz]54 }55 56 bit := x >> d & 157 58 59 res1 := dfs(p.son[0], q.son[bit], d-1)60 res2 := dfs(p.son[1], q.son[bit^1], d-1)61 62 if bit > 0 {63 if p == q {64 return res165 }66 return res1 * res2 % mod67 }68 69 if p == q {70 return (res1 + res2 - 1) % mod71 }72 73 74 75 return (res1 + res2 - 1 +76 (pow2[size(p.son[0])]-1)*(pow2[size(p.son[1])]-1) +77 (pow2[size(q.son[0])]-1)*(pow2[size(q.son[1])]-1)) % mod78 }79 Fprint(out, (dfs(root, root, 29)+mod*2-1)%mod)80}81 8283