Let f(A,B) be the exclusive OR of A,A+1,…,B. Find f(A,B).
What is exclusive OR?
Constraints
All values in input are integers.
0≤A≤B≤10^12
输入
Input is given from Standard Input in the following format:
A B
输出
Compute f(A,B) and print it.
输入样例
2 4
输出样例
5
模拟题
先模拟1~n的异或和
--------二进制--------异或和
1-0001
2-0010-0011-3
3-0011-0000-0
4-0100-0100-4
5-0101-0001-1
6-0110-0111-7
7-0111-0000-0
8-1000-1000-8
9-1001-0001-1
可以看出 1~n的异或和规律为
n%4=0 异或和为n
n%4=1 异或和为1
n%4=2 异或和为n+1
n%4=3 异或和为0
但是模拟x~n(x<=n)时不符合以上规律
/推导ing…/
最后发现
a=3 b=4时
1~2(a-1) 0011
1~4(b)0100
f(3,4)=7
f(f(1,2)^f(1,4))=7
其他情况也适用(a>0)
a=0时 让a不变就行
公式为f(a,b)=f(1,a-1)^f(1,b)
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long int a,b;
scanf("%lld%lld",&a,&b);
if (a)
a=a-1;
if (a%4==1)
{
a=1;
}
else if (a%4==2)
{
a=a+1;
}
else if (a%4==3)
{
a=0;
}
if (b%4==1)
{
b=1;
}
else if (b%4==2)
{
b=b+1;
}
else if (b%4==3)
{
b=0;
}
printf("%lld",a^b);
return 0;
}