Codeforces Round #717 (Div. 2) B. AGAGA XOOORRR(位运算 + 思维)

传送门

题意

给出一个长度为n的数组a,选两个相邻的数将他们换为这两个数的异或^(数组长度减1),问可不可以经过适量的操作数,使得数组(保证数组有两个或者以上数目的元素)各个元素相等。

思路

相等的条件:

如果最终状态有偶数个相等的数,那么^值为0,直接输出YES即可;
如果最终状态有奇数个相等的数,那么^值为ans;

我们先将这n个数^一遍,如果值为0,直接输出YES;(两个数相等,^值为0,所以直接输出YES;)

如果不为0,我们开始枚举,从第一个数开始进行^处理,如果中间等于ans后,我们更新变量为0,继续枚举,如果到最后一个数,变量不为0,或者变量等于ans的次数小于2,我们输出NO;

代码

#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <string>
#include <algorithm>
#include <queue>
#include <utility>
#include <stack>
#include <map>
#include <cmath>
#define mes memset 
#define mec memcpy
 
using namespace std;
 
typedef long long ll;
typedef pair<int,int>PII;
 
const int N = 2010;
const int null = 0x3f3f3f3f;
const ll mod = 1e9 + 7;

int T;
int n;

int main()
{
	cin >> T;
	
	while(T --)
	{
		cin >> n;
		
		int a[N];
		int u = 0;
		for(int i = 1;i <= n;i ++) cin >> a[i],u ^= a[i];
		
		if(u == 0)
		{
			puts("YES");
			continue;
		}
		
		int ans = u,cnt = 0;
	//	cout << ans << endl;
		u = 0;
		for(int i = 1;i <= n;i ++)
		{	
			u ^= a[i];
		//	cout << u << endl;
			if(u == ans) u = 0,cnt ++;
		}
		
		if(u != 0 || cnt < 2) puts("NO");
		else puts("YES");
		
		
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半碗无糖蓝莓冻

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值