EllysXors_求L-R中所有数异或最后的值

Problem Statement

 XOR problems became very popular in TopCoder recently. (If you do not know the bitwise operation XOR, see the Notes section for an explanation.) That's why Elly decided to invent one of her own. Fortunately for you, the one she came up with is quite simple. You are given two long longs L and R. She wants you to find the XOR of all numbers between L and R, inclusive.

Definition

 
Class:EllysXors
Method:getXor
Parameters:long long, long long
Returns:long long
Method signature:long long getXor(long long L, long long R)
(be sure your method is public)
 
 

Notes

-XOR (exclusive or) is a binary operation, performed on two numbers in binary notation. First, the shorter number is prepended with leading zeroes until both numbers have the same number of digits (in binary). Then, the result is calculated as follows: for each bit where the numbers differ the result has 1 in its binary representation. It has 0 in all other positions.
-For example 42 XOR 7 is performed as follows. First, the numbers are converted to binary: 42 is 101010 and 7 is 111. Then the shorter number is prepended with leading zeros until both numbers have the same number of digits. This means 7 becomes 000111. Then 101010 XOR 000111 = 101101 (the result has ones only in the positions where the two numbers differ). Then the result can be converted back to decimal notation. In this case 101101 = 45, so 42 XOR 7 = 45.
-One of the ways to calculate the XOR of more than two numbers A1, A2, ..., An is "A1 XOR (A2 XOR (... XOR An))..))". Since the function is commutative and associative, you can also express it as "A1 XOR A2 XOR ... XOR An" and group the operands in any way you like.
-It can be proved that the answer is always less than 2*R for the given constraints.

Constraints

-L and R will be between 1 and 4,000,000,000, inclusive.
-L will be less than or equal to R.

Examples

0) 
 
3
10
Returns: 8
((((((3 XOR 4) XOR 5) XOR 6) XOR 7) XOR 8) XOR 9) XOR 10 =
        (((((7 XOR 5) XOR 6) XOR 7) XOR 8) XOR 9) XOR 10 = 
                ((((2 XOR 6) XOR 7) XOR 8) XOR 9) XOR 10 = 
                        (((4 XOR 7) XOR 8) XOR 9) XOR 10 = 
                                ((3 XOR 8) XOR 9) XOR 10 =
                                       (11 XOR 9) XOR 10 =
                                                2 XOR 10 = 8.
1) 
 
5
5
Returns: 5
The XOR of a single number is the number itself.
2) 
 
13
42
Returns: 39
A bit larger example.
3) 
 
666
1337
Returns: 0
The answer can be zero.
4) 
 
1234567
89101112
Returns: 89998783

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.     


//手动写了几个数发现个规律,当n为偶数时n^(n+1)=1,所以L-R之间的所有数,每次都 可以将连续的两个数异或后得到1,而1^1=0,1^0=1,所以先可以处理中间成对的数,最后的得到1的个数t/2%2便是所要的值,最后如果两边还有多余的数再异或,这样最终要处理的最多只有三个数。

class EllysXors
{
	public:
    long long getXor(long long L, long long R)
    {
		if(L==R)
        	return L;
       	else
       	{
       		long long u,ans;
			if(L%2)
			{
				
				ans=L;
				u=L+1;	
				if(R%2)
					ans^=((R-u+1)/2)%2;
				else
				{
					ans^=((R-u)/2)%2;
					ans^=R;
				}
				return ans;
			}   
			else
			{
				ans=0;
				if(R%2)
					ans^=((R-L+1)/2)%2;
				else
				{
					ans^=((R-L)/2)%2;
					ans^=R;
				}
				return ans;
			}
		}
    }
};

// Powered by FileEdit
// Powered by TZTester 1.01 [25-Feb-2003]
// Powered by CodeProcessor

//怕出错,讨论两边边界的时候很细所以if/else很多, 后来得到他人推荐的神公式:return a*(a&1)^b*!(b&1)^!!(((a^b)+1)&2); 一句话搞定。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值