soj 11598. XOR

Constraints

Time Limit: 1 secs, Memory Limit: 256 MB

Description

Given two integers S and F, what is the XOR (exclusive-or) of all numbers between Sand 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 ≤ S ≤ F ≤ 1 000 000 000).

Output

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

Sample Input

5
3 10
5 5
13 42
666 1337
1234567 89101112

Sample Output

8
5
39
0
89998783

Problem Source

2014年每周一赛第八场

题目意思很简单,就是要求出从s到f之间所有数的按位异或的值,因为中间的数可能很多,所以我们不可能暴力。就需要换一种其他的思路。
异或有一个性质,就是对某一位来说,假如说这些数中的这一位有奇数个1,那么异或起来这以位就是1,有偶数个1,异或起来就是0,这很好理解。我们就可以利用这个性质来做这道题目。
对于一个数n来说,计算出来从0到n的第i位上有多少个1,那么我们就可以求得从s到f之间的数有多少个1。现在问题就转变成了求0到n的第i位上有多少个1。先考虑一个特殊的情况,当n的二进制形式为全1时,即n等于2^k-1,第i位为1的个数就是(n+1)/2,因为第i位不是0,就是1,当然占总个数的一半。得到这个结论就离答案更近了,由于我们只关心第i位是0还是1,那么高于第i位的我们都不关心,我们截取第1位一直到第i位获得它的十进制值,这里的截取相当于对2^i取模,这样我们就把0到n这n+1个数分成了2^i类,即模2^i为0,1,2,... ,2^i-1的,而只有模2^i的余数大于或等于2^(i-1)的数第i位才是1,我们先计算从0到n有多少个完整的模2^i余数为0到2^i-1的,这个数就是n/(2^i),再乘以2^(i-1),就是1的个数的一部分,另一部分是模2^i余数是0到m,m小于2^i-1,如果m大于等于2^(i-1),那么这部分第i位为1的个数为m-2^(i-1)+1,否则,就是0。这样就求出来了第i位上1的个数,即:n/(2^i)*(2^(i-1)),如果n%(2^i)大于等于2^(i-1),那么再加上n%(2^i)-2^(i-1)+1,将这个最后的数记为cnt,如果cnt为奇数则结果的第i位为1,否则为0。
注意到异或后的结果不可能超过右边界,所以只需要看右边界有多少位,就能确定需要计算多少位,复杂度为log级别的。
我当时写的时候出了好多错误,其中一个是括号扩错了,找了好半天才找到,真坑,看来以后还是要细心。
代码如下:
/*************************************************************************
	> File Name: a.cpp
	> Author: gwq
	> Mail: gwq5210@qq.com 
	> Created Time: 2014年10月21日 星期二 19时18分10秒
 ************************************************************************/

#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>

#define INF (INT_MAX / 10)
#define SQR(x) ((x) * (x))
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repf(i, a, b) for (int i = (a); i <= (b); ++i)
#define repd(i, a, b) for (int i = (a); i >= (b); --i)
#define clr(arr, val) memset(arr, val, sizeof(arr))
#define pb push_back
#define sz(a) ((int)(a).size())
#define middle(x, y) ((x + y) >> 1)

using namespace std;
typedef set<int> si;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef long long ll;

const double esp = 1e-5;

//获取从0到n的第cnt+1位上的1的个数。
int getcnt(int n, int cnt)
{
	int ret = n / (1 << (cnt + 1)) * (1 << cnt);
	//括号括错了,坑了好半天
	if (n % (1 << (cnt + 1)) >= (1 << cnt)) {
		ret += n % (1 << (cnt + 1)) - (1 << cnt) + 1;
	}
	return ret;
}

int main(int argc, char *argv[])
{
	int t;

	scanf("%d", &t);
	while (t--) {
		int l, r;
		scanf("%d%d", &l, &r);
		int cnt = 0;
		int ans = 0;
		while ((r >> cnt) != 0) {
			int tmp = (getcnt(r, cnt) - getcnt(l - 1, cnt)) % 2;
			ans |= (tmp << cnt);
			++cnt;
		}

		printf("%d\n", ans);
	}

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值