XOR Circle

XOR Circle

题目描述:
Snuke has N hats. The i-th hat has an integer ai written on it.
There are N camels standing in a circle. Snuke will put one of his hats on each of these camels.
If there exists a way to distribute the hats to the camels such that the following condition is satisfied for every camel, print Yes; otherwise, print No.
The bitwise XOR of the numbers written on the hats on both adjacent camels is equal to the number on the hat on itself.
What is XOR?
Constraints
All values in input are integers.
3≤N≤105
0≤ai≤109
输入 :
Input is given from Standard Input in the following format:
N
a1 a2 … aN
输出 :
Print the answer.
样例输入 :
3
1 2 3
样例输出 :
Yes
提示 :
If we put the hats with 1, 2, and 3 in this order, clockwise, the condition will be satisfied for every camel, so the answer is Yes.
题目大意:一个序列,绕成环形,任取两个数A和B,其中间有一个C,使C为A,B的异或。
分析:ABC三个数量要一样,或者1个为0,另外两个相等。

#include<bits/stdc++.h>
using namespace std;
#define N 100005
#define ll long long
#define MOD 1000000007
#define INF 0x3f3f3f3f
int n;
ll a[N],num[10];
int cnt[10],tot;
int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
        tot=0;
        cnt[1]=cnt[2]=cnt[3]=0;
        num[1]=num[2]=num[3]=0;
        bool f=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
            if(a[i]!=0) f=1;
        }
        if(f==0)
        {
            puts("Yes");
            continue;
        }
        sort(a+1,a+n+1);
        a[0]=INF;
        for(int i=1;i<=n;i++)
        {
            if(a[i]!=a[i-1])
                cnt[++tot]++,num[tot]=a[i];
            else cnt[tot]++;
            if(tot>3)
            {
                puts("No");
                f=0;
                break;
            }
        }
        if(f==0) continue;
        if(n%3==0)
        {
            if(tot==3&&(num[1]^num[2]^num[3])==0&&cnt[1]==n/3&&cnt[2]==n/3&&cnt[3]==n/3)
            {
                puts("Yes");
                continue;
            }
            if(tot==2&&num[1]==0&&cnt[1]==n/3&&cnt[2]==n/3*2)
            {
                puts("Yes");
                continue;
            }
        }
        puts("No");
    }
    return 0;
}
发布了66 篇原创文章 · 获赞 15 · 访问量 6578

猜你喜欢

转载自blog.csdn.net/soul_mingling/article/details/103040503
xor