题目
There is a sequence A 1... n A_{1...n} A1...n, you need to answer whether there are 4 integers x,y,z,wx,y,z,w satisfying 1 ≤ x < y < z < w ≤ n 1\leq x<y<z<w\leq n 1≤x<y<z<w≤n and A x ⊕ A y ⊕ A z ⊕ A w = 0 A_x\oplus A_y\oplus A_z\oplus A_w=0 Ax⊕Ay⊕Az⊕Aw=0
The input guarantees that
∀
i
≠
j
,
A
i
≠
A
j
\forall i\neq j, A_i\neq A_j
∀i=j,Ai=Aj
Note:
x
⊕
y
x\oplus y
x⊕y means the exclusive or of x and y
(
x
x
o
r
y
)
(x~xor~y)
(x xor y)
题意
给出n个不同的数,如果这n个数有四个异或起来等于0,就是YES,如果一组都没有就是NO。
思路
考虑 a ^ b = c , 每次把两两异或后的值记录为1,如果有一个数记录了2次,即有两组数异或起来相等,由于这n个数都是不同的数,所以不可能存在a^b=c && a^d=c 的情况,所以必当存在四个数满足条件。
考虑时间,
a
i
<
=
1
e
5
a_i<=1e5
ai<=1e5 所以就算全部都异或一遍,最坏情况才会操作
2
e
5
2e5
2e5次,不会超时。
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000000+100;
int a[maxn] , bk[maxn];
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
if(bk[a[i] ^ a[j]]) return cout<<"Yes",0;
bk[a[i] ^ a[j]] = 1;
}
}
cout<<"No\n";
}