interesting interval xor value time limit (normal/java): ms/6000 MS running memory limit: 65536 KByte
Total submissions: 354 tested by: 35
Contest description
Generate a sequence according to the following rules when x > 0 o'clock a[x] = a[x-1] xor x, when x = = 0 o'clock, i.e. a[0] = 0, of course, the title is not so simple, there are n sets of queries, for each group of inquiry l,r, the output sequence A[l] XOR a[l+1] XOR a[l+2 ]...xor A[r-1] XOR A[r], which is the XOR value of the sequence under the [L,r] interval.
Input
The first line is a number n, which indicates the n set of queries (n <= 105), and the next n rows, and line I gives the L,r (1<=l<=r<=109) asked by group I.
Output
Outputs n rows, one number per line, which is the XOR value of the sequence under the [L,r] Interval
Sample input
4
1 5
2 8
3 10
10 100
Sample output
7
9
0
111 Test Instructions: The Chinese question does not explain
Train of thought: straight to see this problem no idea, thought is different or may have the law, so play the table to find the law.
Sure enough, AI is every 4 occurrences of a 0, prefix and is every 8 occurrences of a 0
So the 4 and 8 are the two loops, so just handle it.
For L~r This interval we can simplify, the result is XO (r)-xo (L-1), using the 0~l-1 this interval or two times into 0 properties
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <
Cmath> using namespace std;
void init () {int n=0,sum=0;
for (int i=1; i<=100; i++) {printf ("%d%d\n", n,sum);
N^=i;
Sum^=n;
}} long long xo (int x)///a0xora1...xorax {long long n=0,sum=0;
if (x<=7) for (int i=1; i<=x; i++) {n^=i;
Sum^=n;
} else {int m=x;
m-=7;
int pos=m%8;
if (x<4) for (int i=1; i<=x; i++) n^=i;
m-=3;
int pos1=n%4;
for (int i=x-pos1+1; i<=x; i++) n^=i;
for (int i=x-pos+1; i<=x; i++) {n^=i;
Sum^=n;
}} return sum;
} int main () {int t,l,r;
Init ();
scanf ("%d", &t);
while (t--) {scanf ("%d%d", &l,&r); printf ("%lld\n", XO (r) ^xo (L-1));
} return 0;
}