持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第12天,点击查看活动详情
Problem - M - Codeforces
Walk_alone has a sequence a of length n. He can do the following operations for arbitrary times (possibly zero):
Select an index x and an integer y (1≤x≤n,0≤y<2^30 ); For all index i such that i≠x, change ai to ai⊕y, where ⊕ denotes bitwise XOR. Note that Walk_alone can select different y in different operations.
Now he wants to know if he can change all numbers in the sequence to 0.
Input
The first line contains a integers n (1≤n≤10^5 ), the length of sequence a.
The next line contains n integers a1,a2,…,an (0≤ai<2^30 ), indicating the elements in a.
Output
Print "YES" (without quotes) if Walk_alone can change all numbers in the sequence to 0, otherwise print "NO".
Examples
input
5
5 4 2 1 2
output
YES
问题解析
这题是说,给你一个数组,每次可以选一个数y和一个下标i,把除这个下标i外的元素都用y进行一次异或运算。问有没有可能把这个数组变成一个全是0的情况。
这是一个结论题,先说结论:当数组长度是偶数时,必是YES;如果是奇数就看数组所有数异或后是不是0,如果是就是YES,反之是NO。接下来来推结论:
我们可以先通过n次操作把数组全变成相同的一个数,我们下标从i=0取到i=n-1,y就选arr[i]。为什么这样可以使得数组变成相同的一个数?因为这样就相当于使得数组中的每一个数,将除了自己以外的元素都进行了异或运算。这样得到的一个数就是arr[0]^arr[1]^arr[2]……^arr[n-1]。这样每个数都会是相同的,而且这个数就是这个式子得到的结果。
异或运算有两个性质:异或自己就相当于把自己变成0;异或同一个数两次就相当于没异或。
我们再取下标i=0到i=n-1进行操作,y就取arr[0]^arr[1]^arr[2]……^arr[n-1],即现在数组都相同的那个数,这样会使得数组每个元素进行了n-1次运算,当n-1是偶数时,相当于没异或,数组的值不变;当n-1是奇数时,相当于每个元素都异或了一下自身,变成0.
所以当n-1是奇数时,我们必然能把数组变成全是0;而n-1是偶数时,数组的值不会改变,除非arr[0]^arr[1]^arr[2]……^arr[n-1]等于0,这样在一开始把数组变成相同的时候数组就全是0了,其余情况都不能把数组变成0.
AC代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<math.h>
#include<set>
#include<numeric>
#include<string>
#include<string.h>
#include<iterator>
#include<map>
#include<unordered_map>
#include<stack>
#include<list>
#include<queue>
#include<iomanip>
#define endl '\n'
#define int ll
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PII;
const int N = 550;
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin >> n;
if (n % 2 == 0)
{
cout << "YES";
return 0;
}
int x, res = 0;
for (int i = 0; i < n; i++)
{
cin >> x;
res ^= x;
}
if (res == 0)cout << "YES";
else cout << "NO";
return 0;
}