Editorial for COCI '15 Contest 7 #3 Ozljeda


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Firstly, let's notice that the first k+1 elements of the xorbonacci sequence are cyclically repeated throughout the entire sequence. In other words, it holds X_A = X_{A+k+1} for every A. The proof of this statement is left as an exercise to the reader.

Now we can recall some properties of the xor operation (labeled with \oplus). More precisely, we are interested in the following properties:

\displaystyle \begin{align*}
A \oplus 0 &= A \\
A \oplus A &= 0 \\
A \oplus B &= B \oplus A
\end{align*}

Using these properties, we deduce that it holds:

\displaystyle X_L \oplus X_{L+1} \oplus \dots \oplus X_R = (X_1 \oplus X_2 \oplus \dots \oplus X_R) \oplus (X_1 \oplus X_2 \oplus \dots \oplus X_{L-1})

Using the sequence's cyclicality from the first paragraph and the aforementioned properties, it is clear that the xor of the first N elements of the xorbonacci sequence is equal to the xor of the first N' elements of the xorbonacci sequence where 0 \le N' \le 2(K+1). Therefore, it is enough to preprocess the xors of the first 2(K+1) elements and, using them, answer all given queries in \mathcal O(1).

Unveiling the explicit relation between N and N' is left to you as an exercise.


Comments

There are no comments at the moment.