牛客108D

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sxy201658506207/article/details/84667412

小a有n个数,他提出了一个很有意思的问题:他想知道对于任意的x, y,能否将x与这n个数中的任意多个数异或任意多次后变为y
第一行为一个整数n,表示元素个数
第二行一行包含n个整数,分别代表序列中的元素
第三行为一个整数Q,表示询问次数
接下来Q行,每行两个数x,y,含义如题所示
输出Q行,若x可以变换为y,输出“YES”,否则输出“NO”
5
1 2 3 4 5
3
6 7 
2 1
3 8

YES
YES
NO

对于(6,7)来说,6可以先和3异或,再和2异或 对于(2,1)来说,2可以和3异或 对于(3,8)来说,3不论如何都不能变换为8
对于100%的数据,n,Q<=105 保证所有运算均在int范围内

性质:x^y=z  =>x^z=y
 

#include<bits/stdc++.h>
#include <iostream>
#include <cmath>
#include <cstdio>
#include <stdlib.h>
#include <ctime>
using namespace std;
typedef long long ll;
int a[65];
void build(int A)
{
    for(int x=31; x>=0; --x)
    {
        if(A>>x&1)
        {
            if(a[x]==0)
            {
                a[x]=A;
                break;
            }
            A^=a[x];
        }
    }
}
bool query(int A)
{
    for(int i=31; i>=0; --i)
    {
        if((A>>i&1)&&a[i])
            A^=a[i];
    }
    return A==0;
}
int main()
{
    int n;
    int A;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0; i<n; ++i)
        {
            scanf("%d",&A);
            build(A);
        }
        int t,x,y;
        cin>>t;
        while(t--)
        {
            scanf("%d%d",&x,&y);
            if(query(x^y))
                cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sxy201658506207/article/details/84667412
今日推荐