Sicily 11598 XOR

题目描述


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;
}












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值