第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(济南)(热身赛) B-Four Xor

题目

There is a sequence (A_{1...n}), you need to answer whether there are 4 integers x,y,z,wx,y,z,w satisfying (1leq x<y<z<wleq n) and (A_xoplus A_yoplus A_zoplus A_w=0)

The input guarantees that (forall i eq j, A_i eq A_j)
Note: (xoplus y) means the exclusive or of x and y ((x~xor~y))

在这里插入图片描述
在这里插入图片描述

题意

给出n个不同的数,如果这n个数有四个异或起来等于0,就是YES,如果一组都没有就是NO。

思路

考虑 a ^ b = c , 每次把两两异或后的值记录为1,如果有一个数记录了2次,即有两组数异或起来相等,由于这n个数都是不同的数,所以不可能存在a^b=c && a^d=c 的情况,所以必当存在四个数满足条件。
考虑时间,(a_i<=1e5) 所以就算全部都异或一遍,最坏情况才会操作(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
";
} 

原文地址:https://www.cnblogs.com/liangyj/p/14195198.html