Xor and Sum (bit operation)

 

Title Description

Given an array of size N A, the i-th element Ai.

I asked how many of the sub-interval [LR], to meet the range of values ​​and XOR equal intervals and values, namely:

     Al xor Al + 1 xor ... xor Ar = Al + Al + 1 + ... + Ar (l + 1 represents a subscript)
      a xor b, and a and b is the binary bitwise xor decimal number to obtain a new representation c
xor 5 and 12 is calculated as follows:

510=01012

(12)10=(1100)2

01012xor11002=(1001)2

(1001)2=(9)10

Entry

The first line of a given integer N.
The second row with a given N integers, is the i-th Ai.
^. 5 × 10 ≦ n ≦ 2
0≤A_i≤2 ^ 30

Export

The number of sub-intervals to meet the conditions of LR output.

Sample input

10
0 0 740 361 473 0 0 826 479 974


Sample Output

18

 

 

answer:

xor operation can be considered as not carry binary addition, is adding itself to carry adder.

You can simply come to such a property: For a range, if XOR and addition answers the same, then the answer is definitely still the same narrow range; if XOR and addition answer is not the same, then the answer is certainly expand the range is not the same .

So we can enumerate the right point range, to find the smallest of the left point, this interval is equal to or different intervals and then at this point the number of legitimate range interval is the interval of the right length (inside the left point reduction are legitimate ).

This also may be pretreated prefix and the prefix and XOR, maintaining it with the double pointer.

 

 

 

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<stdio.h>
 4 #include<string.h>
 5 #include<string>
 6 #include<queue>
 7 #include<stdlib.h>
 8 #include<math.h>
 9 #define per(i,a,b) for(int i=a;i<=b;++i)
10 #define rep(i,a,b) for(int i=a;i>=b;--i)
11 #define inf 0xf3f3f3f
12 #define ll long long int 
13 using namespace std;
14 int p[200005];
15 int s[200005];
16 int z[200005];
17 int main()
18 {
19     int m,a;
20     cin>>m;
21     z[0]=0;s[0]=0;
22     per(i,1,m) 
23     {
24         cin>>a;
25         s[i]=s[i-1]+a;
26         z[i]=z[i-1]^a;
27     }
28     ll l=0,sum=0;
29     per(i,1,m)
30     {
31         while((z[i]^z[l])!=(s[i]-s[l])) l++;
32         sum+=i-l;
33     }
34     cout<<sum;
35     return 0;
36 }

 

 

 

 

 

 

 

-

Guess you like

Origin www.cnblogs.com/jiamian/p/12446120.html