用一个xor表示多个xor
Problem statement:
问题陈述:
You are given an array A[] of size N. Now, we call the value of an array the bit-wise XOR of all elements it contains. For example, the value of the array [1,2, 3] = 1^2^3. Now, your task is to find the bit-wise XOR of the values of all sub-arrays of array A.
给您一个大小为N的数组A [] 。 现在,我们将数组的值称为它包含的所有元素的按位XOR。 例如, 数组[1,2,3]的值= 1 ^ 2 ^ 3 。 现在,您的任务是找到数组A的所有子数组的值的按位XOR。
Example:
例:
Input array:
1, 2, 3, 4
Subarrays and their XOR values
Subarray of length 1:
1 //value of the subarray: 1
2 //value of the subarray: 2
3 //value of the subarray: 3
4 //value of the subarray: 4
Subarray of length 2:
1, 2 //value of this subarray:1^2
2, 3 //value of this subarray:2^3
3, 4 //value of this subarray:3^4
Subarray of length 3
1, 2, 3 //value of this subarray:1^2^3
2, 3, 4 // value of this subarray:2^3^4
Subarray of length 4
1, 2, 3, 4 //value of this subarray:1^2^3^4
So
Bitwise XOR of all the values are
1^2^3^4^(1^2)^(2^3)^(3^4)^(1^2^3)^(2^3^4)^(1^2^3^4)
=0
Solution:
解:
One naïve approach can be of course to compute values ( XORing elements) for all subarrays. This is going to be real tedious as we need to find out all subarrays.
一种简单的方法当然是为所有子数组计算值(XORing元素)。 这将非常繁琐,因为我们需要找出所有子数组。
Effective approach can be the XOR nature. We know XORing an element twice results 0.
有效的方法可以是XOR性质。 我们知道对元素进行XOR两次会得到0。
i^i=0
i ^ i = 0
Let's revise our example:
让我们修改示例:
For input array:
1, 2, 3, 4
Sub arrays are:
{1}
{2}
{3}
{4}
{1, 2}
{2, 3}
{3, 4}
{1, 2, 3}
{2, 3, 4}
{1, 2, 3, 4}
This is all possible sub arrays
A prompt observation leads to the fact that every element
actually occurred even number of times while XORing all values
1^2^3^4^(1^2)^(2^3)^(3^4)^(1^2^3)^(2^3^4)^(1^2^3^4)
1 has appeared 4 times
2 has appeared 6 times
3 has appeared 6 times
4 has appeared 4 times
So the final result is actually 0
Now take any even length array
You will find all elements appearing even no of times
if you expand the XORing sequence like the previous one.
So,
For even length array the output will be always 0,
no matter what the elements are.
Now what about odd lengths.
Let's quickly revise an example
The array be:
1, 2, 3, 4, 5
So,
Subarrays are:
{1}
{2}
{3}
{4}
{1, 2}
{2, 3}
{3, 4}
{4, 5}
{1, 2, 3}
{2, 3, 4}
{3, 4, 5}
{1, 2, 3, 4}
{2, 3, 4, 5}
{1, 2, 3, 4, 5}
So the expanded result sequence would be:
1^2^3^4^5^(1^2)^(2^3)^(3^4)^(4^5)^(1^2^3) ^
(2^3^4) ^ (3^4^5) ^ (1^2^3^4) ^(2^3^4^5) ^ (1^2^3 ^4^5)
So, 1 appears 5 times //resulting 1
2 appears 8 times //resulting 0
3 appears 9 times //resulting 3
4 appears 8 times //resulting 0
5 appears 5 times //resulting 5
Output will be : 1^3^5
Thus for an odd length array it's found that:
因此,对于奇数长度的数组,发现:
Result will be XORing of all even indexed elements (0-based indexing)
结果将是对所有偶数索引元素进行异或(基于0的索引)
You can check the argue by doing few more examples your own.
您可以通过自己做一些其他示例来检查争论。
So the entire problem actually reduced to a simple concept-
因此,整个问题实际上简化为一个简单的概念-
Even length array: output 0
偶数长度数组:输出0
Odd length array: XORing all even-indexed elements
奇数长度数组:对所有偶数索引元素进行异或
No need to compute all the sub arrays at all!!
根本不需要计算所有子数组!!
C++ implementation:
C ++实现:
#include <bits/stdc++.h>
using namespace std;
int gameOfXor(vector<int> a,int n){
if(a.size()%2==0) //for even length array
return 0;
int sum=a[0];
// for odd length array
for(int it=2;it<a.size();it=it+2)
sum=sum^a[it];
return sum;
}
int main(){
int n,item;
cout<<"Enter number of elements\n";
scanf("%d",&n);
vector<int> a;
cout<<"Enter array elements\n";
for(int j=0;j<n;j++){
scanf("%d",&item);
a.push_back(item);
}
cout<<"Output is:\n";
cout<<gameOfXor(a,n)<<endl;
return 0;
}
Output
输出量
First run:
Enter number of elements
4
Enter array elements
1 2 3 4
Output is:
0
Second run:
Enter number of elements
5
Enter array elements
1 2 3 4 5
Output is:
7
用一个xor表示多个xor