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

题目

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 1x<y<z<wn and A x ⊕ A y ⊕ A z ⊕ A w = 0 A_x\oplus A_y\oplus A_z\oplus A_w=0 AxAyAzAw=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 xy 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";
} 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值