前言
修改 3 处笔误,曾经过审过,求给过。
先读题
给出四个非负整数 。你需要找到任意一个整数 满足 且 。其中, 表示 中所有整数的二进制按位异或和。
如有多解,求出任何一个均可。如果无解,请指出。
前置知识
表示异或。
思路
题目要求求区间异或和。
和区间和先求前缀和一样,先求成前缀异或和。
比如, 的区间异或和可以先求出 ,,……, 的前缀异或和,再用 减 。
但是本题的数据范围很大,如果每个都算出来,要达到 ,会超时。
我们用这道题的思路,先暴力计算出前面部分,找规律。
我们发现:
于是我们记 ,问题变成求 。
分类讨论即就能过。
代码
#include <bits/stdc++.h>
using namespace std;
int n,l,r,k,x,sum;
int main(){;
cin>>n;
for(int i=1;i<=n;i++){
cin>>l>>r>>k>>x;
sum=l-(l%4)+1;
k--;
if(k%4==1)k=1;
else if(k%4==2)k++;
else if(k%4==3)k=0;
x^=k;
if(x%4==0&&l<=x&&x<=r){
cout<<x<<endl;
continue;
}
if((x-1)%4==2&&l<=x-1&&x-1<=r){
cout<<x-1<<endl;
continue;
}
if(x==1){
if(sum<l)sum+=4;
if(sum<=r){
cout<<sum<<endl;
continue;
}
else{
cout<<-1<<endl;
continue;
}
}
else if(x==0){
sum=l-(l%4)+3;
if(sum<=r){
cout<<sum<<endl;
continue;
}
}
cout<<-1<<endl;
}
return 0;
}