B. AGAGA XOOORRR

题:
Baby Ehab is known for his love for a certain operation. He has an array a of length n, and he decided to keep doing the following operation on it:

he picks 2 adjacent elements; he then removes them and places a single integer in their place: their bitwise XOR. Note that the length of the array decreases by one.
Now he asks you if he can make all elements of the array equal. Since babies like to make your life harder, he requires that you leave at least 2 elements remaining.

Input
The first line contains an integer t (1≤t≤15) — the number of test cases you need to solve.

The first line of each test case contains an integers n (2≤n≤2000) — the number of elements in the array a.

The second line contains n space-separated integers a1, a2, …, an (0≤ai<230) — the elements of the array a.

Output
If Baby Ehab can make all elements equal while leaving at least 2 elements standing, print “YES”. Otherwise, print “NO”.

Example
input
2
3
0 2 2
4
2 3 1 10
output
YES
NO

Note
In the first sample, he can remove the first 2 elements, 0 and 2, and replace them by 0⊕2=2. The array will be [2,2], so all the elements are equal.

In the second sample, there’s no way to make all the elements equal.

题意:
给出一个n个元素的数组,任意相邻之间的元素可以按位异或,每次操作后长度减一。
求这个数组是否可以在操作后得到各元素相等并且数组里至少有两个元素的情况。

思路:
首先,考虑两种情况,长度为2或3的数组各元素相等,长度为4的数组各元素相等可以看作长度为2的数组各元素相等,后面以此类推,因此只要考虑这两种就好了。
遍历求前缀异或和。
如果该前缀异或和为0,以此为界,显然,前面的区间必然存在操作数次异或和后相等的两个数。
如果为n时的前缀异或和为0,显然,这是长度为2的数组各元素都相等的情况。
如果为n时的前缀异或和不为0:
零异或任何数都等于该数,所以后面数的异或跟前面区间的值无关,如果后面整个区间的异或和可以在前面区间的前缀异或和中找到,那么以该值为界,前面区间可以分为两个小区间,这两个小区间的异或和必然是两个相等的数。
因此有长度为3的数组各元素都相等的情况。

#include<iostream>
using namespace std ;
 
int main()
{
	int t ;
	cin >> t ;
	while(t--)
	{
		int n ;
		cin >> n ;
		long long a[2005] ;
		for( int i = 1 ; i <= n ; ++i )
			cin >> a[i] ;
		int m = n ;
		long long s ;
		int flag = 0 ;
		for( int i = 1 ; i <= n ; ++i )
		{
			a[i] ^= a[i-1] ;
			if( a[i] == 0 && !flag )
			{
				flag = i ;
			}
		}
		int kj = 0 ;
		for( int i = 1 ; i <= flag ; ++i )
		{
			if( a[i]== a[n] )
				kj = 1 ;
		}
		if( !kj )
			cout << "NO" << endl ;
		else
			cout << "YES" << endl ;
	}
	return 0 ;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值