Nowcoder 练习赛26 D xor序列 ( 线性基 )

Posted rubbishes

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nowcoder 练习赛26 D xor序列 ( 线性基 )相关的知识,希望对你有一定的参考价值。

题目链接

题意 : 中文题、点链接

 

分析 :

对于给定的 X 和 Y 假设存在一个 Z

使得 X (xor) Z = Y

做一个变形

X (xor) Z (xor) Y = 0

X (xor) Y = Z

也就是原集合中

是否存在一个异或组合结果为 X (xor) Y

线性基模板一套、AC

 

技术分享图片
#include<bits/stdc++.h>
#define LL long long
#define ULL unsigned long long

#define scl(i) scanf("%lld", &i)
#define scll(i, j) scanf("%lld %lld", &i, &j)
#define sclll(i, j, k) scanf("%lld %lld %lld", &i, &j, &k)
#define scllll(i, j, k, l) scanf("%lld %lld %lld %lld", &i, &j, &k, &l)

#define scs(i) scanf("%s", i)
#define sci(i) scanf("%d", &i)
#define scd(i) scanf("%lf", &i)
#define scIl(i) scanf("%I64d", &i)
#define scii(i, j) scanf("%d %d", &i, &j)
#define scdd(i, j) scanf("%lf %lf", &i, &j)
#define scIll(i, j) scanf("%I64d %I64d", &i, &j)
#define sciii(i, j, k) scanf("%d %d %d", &i, &j, &k)
#define scddd(i, j, k) scanf("%lf %lf %lf", &i, &j, &k)
#define scIlll(i, j, k) scanf("%I64d %I64d %I64d", &i, &j, &k)
#define sciiii(i, j, k, l) scanf("%d %d %d %d", &i, &j, &k, &l)
#define scdddd(i, j, k, l) scanf("%lf %lf %lf %lf", &i, &j, &k, &l)
#define scIllll(i, j, k, l) scanf("%I64d %I64d %I64d %I64d", &i, &j, &k, &l)

#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define lowbit(i) (i & (-i))
#define mem(i, j) memset(i, j, sizeof(i))

#define fir first
#define sec second
#define VI vector<int>
#define ins(i) insert(i)
#define pb(i) push_back(i)
#define pii pair<int, int>
#define VL vector<long long>
#define mk(i, j) make_pair(i, j)
#define all(i) i.begin(), i.end()
#define pll pair<long long, long long>

#define _TIME 0
#define _INPUT 0
#define _OUTPUT 0
clock_t START, END;
void __stTIME();
void __enTIME();
void __IOPUT();
using namespace std;
struct L_B {
    LL d[65], p[65];
    int cnt;

    void init() {
        mem(d, 0);
        mem(p, 0);
        cnt = 0;
    }

    bool Insert(LL val) {
        for (int i = 32 ; i >= 0 ; i --) {
            if (val & (1LL << i)) {
                if (!d[i]) {
                    d[i]=val;
                    break;
                }
                val^=d[i];
            }
        }
        return val > 0;
    }

    bool judge(LL val) {
        for (int i = 32 ; i >= 0 ; i --) {
            if (val & (1LL << i)) {
                if (!d[i]) {
                    //d[i]=val;
                    break;
                }
                val^=d[i];
            }
        }
        return val > 0;
    }
}LB;

int main(void){__stTIME();__IOPUT();

    int n;
    sci(n);

    LB.init();
    for(int i=0; i<n; i++){
        LL tmp; scl(tmp);
        LB.Insert(tmp);
    }

    int Q;
    sci(Q);
    while(Q--){
        LL x, y;
        scll(x, y);
        if(!LB.judge(x^y)) puts("YES");
        else puts("NO");
    }


__enTIME();return 0;}


void __stTIME()
{
    #if _TIME
        START = clock();
    #endif
}

void __enTIME()
{
    #if _TIME
        END = clock();
        cerr<<"execute time = "<<(double)(END-START)/CLOCKS_PER_SEC<<endl;
    #endif
}

void __IOPUT()
{
    #if _INPUT
        freopen("in.txt", "r", stdin);
    #endif
    #if _OUTPUT
        freopen("out.txt", "w", stdout);
    #endif
}
View Code

 

以上是关于Nowcoder 练习赛26 D xor序列 ( 线性基 )的主要内容,如果未能解决你的问题,请参考以下文章

51nod.26区间xor(位运算)

2019 Nowcoder Multi-University Training Contest 1 H-XOR

牛客练习赛56 D.小翔和泰拉瑞亚 线段树

[codeforces Mail.Ru Cup 2018 Round 1 D][ xor 操作]

cf 1174 D Ehab and the Expected XOR Problem

newcoder 练习赛17 B 好位置 dp 最长公共子序列