Problem solution · Go

Codeforces 617E — XOR and Favorite Number

Codeforces 617E — XOR and Favorite Number: a Go solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to EndlessCheng Codeforces Go.

Technique
Direct simulation
Source
EndlessCheng Codeforces Go
Length
87 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For Codeforces 617E — XOR and Favorite Number, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 87 lines of Go from the credited upstream file 617E.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.

Source

Code and credit

This code comes from EndlessCheng Codeforces Go by Σndless (EndlessCheng) and is used under the MIT licence.

Full codeCodeforces 617E — XOR and Favorite Number · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io"	"math"	"sort") // github.com/EndlessCheng/codeforces-gofunc Sol617E(reader io.Reader, writer io.Writer) {	in := bufio.NewReader(reader)	out := bufio.NewWriter(writer)	defer out.Flush() 	var n, q, k, v int	Fscan(in, &n, &q, &k)	sum := make([]int, n+1)	for i := 0; i < n; i++ {		Fscan(in, &v)		sum[i+1] = sum[i] ^ v	} 	type query struct {		blockIdx  int		l, r, idx int	}	qs := make([]query, q)	blockSize := int(math.Round(math.Sqrt(float64(n))))	for i := range qs {		var l, r int		Fscan(in, &l, &r)		qs[i] = query{l / blockSize, l, r + 1, i}	}	sort.Slice(qs, func(i, j int) bool {		qi, qj := qs[i], qs[j]		if qi.blockIdx != qj.blockIdx {			return qi.blockIdx < qj.blockIdx		}		if qi.blockIdx&1 == 0 {			return qi.r < qj.r		}		return qi.r > qj.r	}) 	l, r, _ans := 1, 1, int64(0)	cnt := make([]int, 1<<20)	cnt[0] = 1	update := func(idx, delta int) {		s := sum[idx]		tar := k ^ s		if delta == 1 {			_ans += int64(cnt[tar])			cnt[s]++		} else {			cnt[s]--			_ans -= int64(cnt[tar])		}	}	ans := make([]int64, q)	for _, q := range qs {		for ; r < q.r; r++ {			update(r, 1)		}		for ; l < q.l; l++ {			update(l-1, -1)		}		for l > q.l {			l--			update(l-1, 1)		}		for r > q.r {			r--			update(r, -1)		}		ans[q.idx] = _ans	}	for _, v := range ans {		Fprintln(out, v)	}} //func main() {//	Sol617E(os.Stdin, os.Stdout)//} 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗