本文介绍了有人可以解释以下xor属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在一个论坛中,有人提到给定的 n
个数字数组:
I a forum it is mentioned that given array of n
numbers:
arr[0........n-1]
以下条件持有, ^
是 xor
运算符`
The following conditon holds, ^
is the xor
operator `
f(l,r) = f(0,r) ^ f(0,l-1)
其中 f(l,r)= arr [l] ^ arr [l + 1] ^ ........ arr [r]
我检查了上面的数组数量以及 l
和<$ c $的不同值c> r 和是,的确如此。但是我不明白怎么办?
I checked the above taking number of arrays and different values of l
and r
and YES, it is true. But I don't understand how?
有人可以解释一下这背后的逻辑吗?
Can somebody please explain the logic behind this?
推荐答案
使用XOR的最简单属性
Use the simplest property of XOR
f(0,r) ^ f(0,l-1) = f(l,r)
=> (f(0,r) ^ f(0,l-1)) ^ f(0,l-1) = f(0,l-1) ^ f(l,r)
=> f(0,r) = f(l,r) ^ f(0,l-1) [Since XOR is associative f(0,l-1) ^ f(0,l-1) = 0 and x ^ 0 = x]
=> f(0,r) = (arr[0]^...arr[l-1])^(arr[l]^...^arr[r])
是 f(0,r)
的定义。
这篇关于有人可以解释以下xor属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!