An important conclusion about XOR

one question

Given two non-negative integer numbers a, b (a≤b), find a xor (a + 1) xor (a + 2) xor ... xor b

Required complexity is O (1)

analysis

If you go directly from the b a, it is certainly  TLE 's

So, we consider a conclusion:

For arbitrary integer x, x xor x = 0 be apparent

So, we can simplify this:

 

   a xor (a+1) xor (a+2) xor ... xor b

= (1 xor 2 xor 3 xor ... xor b) xor (1 xor 2 xor 3 xor ... xor (a-1))

Therefore, the discussion only 1 xor 2 xor ... k values on the line (k is any positive integer)

Note that if a = 0, then for special treatment

Otherwise, the answer will be a negative number XOR

Guess you like

Origin www.cnblogs.com/zengpeichen/p/10943113.html
XOR