题目描述
Given two integers S and F, what is the XOR (exclusive-or) of all numbers between S and F (inclusive)?
输入
The first line of input is the integer T, which is the number of test cases (1 ≤ T ≤ 1000). T lines follow, with each line containing two integers S and F (1 ≤ S ≤ F ≤ 1 000 000 000).
输出
For each test case, output the (decimal) value of the XOR of all numbers between S and F, inclusive.
样例输入
5
3 10
5 5
13 42
666 1337
1234567 89101112
样例输出
8
5
39
0
89998783
/*
题目大意:求从a到b之间所有数异或的结果
数据范围10^9,暴力循环肯定t,所以要想方法
因为 a^...^b=(1^...a-1)^(1^...^b)
所以 只需要求得 (1^...a-1) 和 (1^...^b) 即可
通过打表可以发现规律,规律如下。
f(1)=1 f(5)=1 ... f(4k+1)=1
f(2)=3 f(6)=7 ... f(4k+2)=4k+3
f(3)=0 f(7)=0 ... f(4k+3)=0
f(4)=4 f(8)=8 ... f(4k+4)=4k+4
*/
#include<stdio.h>
int main(){
int t,a,b;
scanf("%d",&t);
int s=0;
for(int i=1;i<1000;i++){
s=s^i;
printf("%d=%d\n",i,s);
}
while(t--){
scanf("%d%d",&a,&b);
a--;
int c,d;
if(a%4==0) c=a;
else if(a%4==1) c=1;
else if(a%4==2) c=a+1;
else if(a%4==3) c=0;
if(b%4==0) d=b;
else if(b%4==1) d=1;
else if(b%4==2) d=b+1;
else if(b%4==3) d=0;
printf("%d\n",c^d);
}
return 0;
}