An array, , is defined as follows:
- A0=0
- Ax=Ax-1^x for x>0 , where ^ is the symbol for XOR
Print the answer to each question.
Input Format
The first line contains (the number of questions).
The subsequent lines each contain two space separated integers, and , respectively. Line contains and .
Constraints
Subtasks
For score:
The subsequent lines each contain two space separated integers, and , respectively. Line contains and .
Constraints
Subtasks
For score:
Output Format
On a new line for each test case , print the exclusive-or of 's elements in the inclusive range between indices and .
Sample Input
3
2 4
2 8
5 9
Sample Output
7
9
15
Explanation
The beginning of our array looks like this:
A=[0,1,3,0,4,1,7,0,8,1,11,0...]Test Case 0: 3^0^4=7
Test Case 1:3^0^4^1^7^0^8=9
Test Case 2:1^7^0^8^1=15
Observation:
for every 4 of A, is repeated by this pattern: 0+n*4,1,3+n*4,0
and their XOR is always 2 no matter what n is.
So the calculation is ^ of numbers before a segment start point, ^ with 2 if there are odd number of segment of 4(or do nothing with even number of segment of 4, because their ^ result is 0, and XOR with 0 is still itself), then ^ with numbers in last incomplete segment.
With 0+n*4,1,3+n*4,0, I can come up a formula for every single number easily. that is getAi function. That function is further used to calculate left and right part of numbers which are in partial segments. so maximum 6 getAi need to be invoked no matter how many numbers between the two given numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | package bitwise; import java.util.Scanner; public class XOR { public static long getAi(long i){ long firstFour[] =new long[]{0,1,3,0};//rest will be 0+n*4,1,3+n*4,0. each 4 of these int position = (int)(i%4);//i is 0 based long times = i/4; if(position==0 ||position==2){ return (long)(firstFour[position]+ times*4); } else if(position==1){ return 1; } else return 0; } public static void main(String[] args) { // TODO Auto-generated method stub // for (long i=0;i<12;i++) // System.out.println(getAi(i)); Scanner in = new Scanner(System.in); int Q = in.nextInt(); for(int a0 = 0; a0 < Q; a0++){ long L = in.nextLong(); long R = in.nextLong(); long result = 0; long l=L; while(l<=R && l%4!=3){ result=result^getAi(l); l++; } // while(l<=R && (l+1)%4!=0){ // result=result^getAi(l); // l++; // } //the forth is always 0, so not need to calculate with it long numSectionsBetweenThem = (R-l)/4; int reminder=(int)((R-l)%4); if(reminder==0&& numSectionsBetweenThem%2==0){ //do not need to do anything }else if(reminder==0 &&numSectionsBetweenThem%2==1){ result=result^2;//one segment's xor is 2 } else if(reminder>0){ if(numSectionsBetweenThem%2==1){ result = result^2; } long r=R; while(l<=r&& (r+1)%4!=0){ result = result^getAi(r); r--; } //result = result^getAi(r); } System.out.println(result); } } } |