Description

 

Problem A: XOR

Given two integers S and F, what is the XOR (exclusive-or) of all numbers between S and F (inclusive)?

Input

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 ≤ SF ≤ 1 000 000 000).

Output

For each test case, output the (decimal) value of the XOR of all numbers between S and F, inclusive.

Sample Input

5
3 10
5 5
13 42
666 1337
1234567 89101112

Sample Output

8
5
39
0
89998783

从1到n的连续异或和sum:

n%4==1   sum=1;

n%4==2   sum=n+1;

n%4==3   sum=0;

n%4==0   sum=n;

有了规律瞬间变水题

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    long long a,b;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld%lld",&a,&b);
        long long tmp;
        if(b%4==1) tmp=1;
        else if(b%4==2) tmp=b+1;
        else if(b%4==3) tmp=0;
        else if(b%4==0) tmp=b;
        a--;
        if(a%4==1) tmp^=1;
        else if(a%4==2) tmp^=a+1;
        else if(a%4==3) tmp^=0;
        else if(a%4==0) tmp^=a;
        cout<<tmp<<'\n';
    }
    return 0;
}