Find an array of numbers by their XOR
You need to find an array of numbers (no more than 10^5 elements, each of which is greater than 0, but less than 2^50). In this case, the XOR of all the numbers in this array is X. I want to see either the code (in python) or the theory for creating such an algorithm
1 answers
For the XOR operation, the following statements are true: let X=A xor B
, then A=X xor B
and B=X xor A
. the same way X=X xor 0
, X xor X=0
. Due to this behavior, to find an array of length N, we just need to generate N-1 random elements, calculate their xor (let it be "A", A=M[1] xor M[2] ... xor M[N-1]
). If A=X
then the last element would have to be made 0, but it is forbidden, so we get A=A xor M[N-1]
(remove the influence of the N-1 element), change M[N-1] to any other number, and get A=A xor M[N-1]
again. Calculate the last element of the array M[N]=A xor X
.