CF1516B AGAGA XOOORRR

文章讲述了BabyEhab对数组进行异或操作的问题,他试图通过删除相邻元素并替换为它们的异或值,使得数组所有元素相等,同时至少保留2个元素。讨论了如何判断在给定条件下是否可行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AGAGA XOOORRR

题目描述

Baby Ehab is known for his love for a certain operation. He has an array a a a of length n n 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 2 2 elements remaining.

输入格式

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

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

The second line contains $ n $ space-separated integers a 1 a_1 a1 , a 2 a_2 a2 , … \ldots , a n a_{n} an ( 0 ≤ a i < 2 30 0 \le a_i < 2^{30} 0ai<230 ) — the elements of the array a a a .

输出格式

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

样例 #1

样例输入 #1

2
3
0 2 2
4
2 3 1 10

样例输出 #1

YES
NO

提示

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

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

题面翻译

给定一个 n n n 长度的数组 a a a ,可以进行操作:

将数组相邻的两个元素删除,并在原来的位置插入这两个元素的异或和。

求在最多删除 n − 2 n-2 n2 个元素(即至少剩余 2 2 2 个元素)的情况下能否使数组所有元素相等。

思路:

容易想到最后留下的数要么 2 2 2 个,要么 3 3 3 个。这很好证明。

如果有 n n n 个数相等,那么我一定可以拉出来两个数,异或一下,变成 0 0 0,然后随便搞到一个数身上。由于最后不可以只留下一个数,所以留下的数要么 2 2 2 个,要么 3 3 3 个。

2 2 2 个数的情况很好判断,只需要判断所有数异或起来是否等于 0 0 0

3 3 3 个数也很好做,注意到 N N N 的范围,我们可以直接枚举三个区间,利用好维护好的前缀和搞一下即可。

代码:

#include <bits/stdc++.h>

#define LL long long
using namespace std;
const int N = 2e3 + 10 , INF = 0x3f3f3f3f;
inline LL read(){
	   LL x = 0 , f = 1 ; char ch = getchar();
	   while (ch < '0'||ch > '9'){if (ch == '-') f = -1 ; ch = getchar();}
	   while (ch >= '0'&&ch <= '9'){x = x * 10 + ch - 48 ; ch = getchar();}
	   return x * f;
}
void write(LL x){
    if(x < 0)
        putchar('-'),x = -x;
    if(x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}
LL a[N]; 
LL f[N] , b[N];
signed main(){
	int T = read();
	while(T --){
		int n = read() , g; bool flag = 0;
		for(int i = 1;i <= n;i ++)
			cin >> a[i];
		b[n + 1] = 0; f[0] = 0;
		for(int i = 1;i <= n;i ++) f[i] = f[i - 1] ^ a[i];
		for(int i = n;i >= 1;i --) b[i] = b[i + 1] ^ a[i];
		for(int i = 2;i <= n - 1;i ++){
			g = 0;
			for(int j = i ;j < n;j ++){
				g ^= a[j];
				if(g == f[i - 1] && g == b[j + 1]) {
					flag = 1;
					break ;
				}
			}
		}
		if(!f[n]) flag = 1;
		puts( flag ? "YES" : "NO");
	}
	      
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值