xor序列【线性基_判断是否可以异或得到】

题目链接


(1)、得到线性基

(2)、从二进制高位开始异或,最后如果得到0,说明可以得到该数

【从低位的话会有不确定性,比如10011和10是线性基的两个数,我们判断10001是不是可以异或得到?从低位开始显然就是不对的。】

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
inline int read()
{
    int x = 0, f = 1; char c = getchar();
    while(c < '0' || c > '9') { if(c == '-') f = -f; c = getchar(); }
    while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
    return x * f;
}

const int maxN = 55;
const int maxBit = 31;

int n, a, p[maxBit];
int cnt;

void add(int x)
{
    for(int i = maxBit - 1; i >= 0; -- i )
    {
        if(x >> i & 1)
        {
            if(!p[i]) { p[i] = x; break; }
            x ^= p[i];
        }
    }
}

int main()
{
    n = read();
    for(int i = 0; i < n; ++ i )
        a = read(), add(a);
    for(int i = 0; i < maxBit; ++ i )
        if(p[i]) ++cnt;
    int q; q = read();
    while(q -- )
    {
        int x, y; x = read(); y = read();
        int now = x ^ y;
        if(now == 0)
        {
            if(cnt == n) printf("NO\n");
            else printf("YES\n");
        }
        else
        {
            bool flag = false;
            for(int i = maxBit - 1; i >= 0; -- i )
            {
                if(now >> i & 1)
                    now ^= p[i];
                if(!now)
                {
                    flag = true;
                    break;
                }
            }
            if(flag) printf("YES\n");
            else printf("NO\n");
        }
    }
    return 0;
}
发布了273 篇原创文章 · 获赞 76 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_44049850/article/details/104652124
今日推荐