Codeforces Round #717 (Div. 2) B. AGAGA XOOORRR

B. AGAGA XOOORRR
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
inputCopy
2
3
0 2 2
4
2 3 1 10
outputCopy
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.

yike同志教的。
思路关键:
暴力
就是n2暴力

把每个区间的异或和都算一遍
sum[l][r]表示l到r的异或和

其实本来我是这样想的
只是觉得要看2段3段4段…超时了又要写个树,麻烦

其实要熟悉,用到异或重要性质:
A ^ A=0,A ^ 0=A
可得到
A^ A ^A=A
所以要么分2段要么分3段
多了可以化少,如5段就是三段(1 ^2 ^3)4 5

两段三段,都是SUM相等

WA多次:
判断失误
三段,不是SUM左边两个异或等于右边两个,或加上特判两边
是SUM本身相等,要说的话异或都是0才对

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<iostream>
//#include<algorithm>
//#include<string>
//#include<sstream>
//#include<vector>
//#include<map>
//#include<set>
//#include<ctype.h>
//#include<stack>
//#include<queue>
#ifdef LOCAL
FILE*FP=freopen("text.in","r",stdin);
//FILE*fp=freopen("text.out","w",stdout);
#endif
using namespace std;
#define ll long long
#define ld long double
#define pii pair<int,int>
#define piii pair<int,pii>
#define pll pair<ll,ll>
#define plll pair<ll,pll> 
#define pdd pair<double,double>
#define pdi pair<double,int>
#define pid pair<int,double>
#define vi vector <int> 
#define vii vector <vi> 
#define st first
#define nd second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define _forplus(i,a,b) for( register int i=(a); i<=(b); i++)
#define forplus(i,a,b) for( register int i=(a); i<(b); i++)
#define _forsub(i,a,b) for( register int i=(a); i>=(b); i--)
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define pi (acos(-1))
#define EPS 0.00000001
#define MOD 1000000007
#define fastio 	std::ios::sync_with_stdio(false);std::cin.tie(0);
#define N 2005
int a[N],sum[N][N];//原始数据,从l到r的异或和 
int main(){
	fastio
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		_forplus(i,1,n){
			cin>>a[i];
		}//输入 
		_forplus(i,1,n){
			sum[i][i]=a[i];
			_forplus(j,i+1,n){
				sum[i][j]=sum[i][j-1]^a[j];
			}
		}//sum 
		//能否分成2个or3个区间 
		int flag=0;//默认不能找到一个 ,即NO
		_forplus(i,1,n-1){
			if(sum[1][i]==sum[i+1][n]){
				flag=1;
				break;
			}
			_forplus(j,i+1,n-1){
				//if(sum[1][i]==(sum[1][i]^sum[i+1][j]^sum[j+1][n])){//不一定 
				//if((sum[1][i]^sum[i+1][j])==(sum[i+1][j]^sum[j+1][n])&&sum[1][i]==sum[j+1][n]){
				if(sum[1][i]==sum[i+1][j]&&sum[1][i]==sum[j+1][n]){
					flag=1;
					break;
				}
			}
			if(flag)break;
		}
		cout<<(flag?"YES":"NO")<<endl; //
	}
	return 0;
}
/*10
37 26 62 60 54 29 24 7 8 8 
5
20 39 26 43 43 
*/
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值