CF1516B AGAGA XOOORRR 题解

· · 题解

假设我们已经操作完了整个序列,由于异或操作的性质,x \oplus x = 00 \oplus x = x

所以,如果整个序列的异或和是 0,那么最后一定是有偶数个段,一定可以操作成所有元素相等。

如果异或和不是 0 而是 x,只需要判断一下有多少段的异或和为 x 即可。如果有大于 1 个段就可以完成操作。

#include <bits/stdc++.h>
using namespace std;

namespace fastIO
{
    template<typename T> inline void read(T &t)
    {
        T x = 0;
        int f = 0;
        char ch = getchar();
        while (!isdigit(ch)) f ^= !(ch ^ 45), ch = getchar();
        while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
        t = f ? -x : x;
    }
    template<typename T, typename ...Args> inline void read(T &x, Args&... args)
    {
        read(x), read(args...);
    }
    template<typename T> void write(T t)
    {
        if (t < 0) putchar('-'), t = -t;
        if (t >= 10) write(t / 10);
        putchar(t % 10 + '0');
    }
    template<typename T, typename ...Args> void write(T t, Args... args)
    {
        write(t), putchar(' '), write(args...);
    }
    template<typename T> void writeln(T t)
    {
        write(t), putchar('\n');
    }
};
using namespace fastIO;

int main()
{
    int tt;
    read(tt);
    while (tt -- )
    {
        int n, x = 0;
        read(n);
        vector<int> a(n);
        for (auto &i: a) read(i), x ^= i;
        if (x == 0) {puts("YES"); continue;}
        int cnt = 0, cur = 0;
        for (int i = 0; i < n; i ++ )
        {
            cur ^= a[i];
            if (cur == x) {cur = 0; cnt ++ ;}
        }
        puts(cnt >= 2 ? "YES" : "NO");
    }
    return 0;
}