题目传送门
题目大意
定义为,其中表示异或,给出多组数据,每组数据给出 ,求一个数,满足,若没有则输出-1。
题目做法
思路
思路一
最简单的思路是,在中枚举n。那么我们很容易有以下代码
#include <bits/stdc++.h>
using namespace std;
int main()
{
int T,l,r,k,x;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d",&l,&r,&k,&x);
bool b=1;
for(int i=k,s=0;i<=r;++i)
{
s^=i;
if(i>=l&&s==x)
{
printf("%d\n",i);
b=0;
break;
}
}
if(b) printf("-1\n");
}
}思路二
记为,那么 即解
这样我们就计算得出了
再观察一下,的前128项:
1 3 0 4 1 7 0 8 1 11 0 12 1 15 0 16 1 19 0 20 1 23 0 24 1 27 0 28 1 31 0 32 1 35 0 36 1 39 0 40 1 43 0 44 1 47 0 48 1 51 0 52 1 55 0 56 1 59 0 60 1 63 0 64 1 67 0 68 1 71 0 72 1 75 0 76 1 79 0 80 1 83 0 84 1 87 0 88 1 91 0 92 1 95 0 96 1 99 0 100 1 103 0 104 1 107 0 108 1 111 0 112 1 115 0 116 1 119 0 120 1 123 0 124 1 127 0 128归纳可知:
时,;
时,;
时,;
时,;
对计算出来的进行逆运算,将在中判断对应,就大功告成了!
错误代码(小细节写崩了,希望各位指正)
不过至少没有TLE了
