HDU3949 异或线性基

原文链接:https://blog.csdn.net/WuBaizhe/java/article/details/77885002

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;

const int A = 1e4 + 10;
ll a[A],b[110],c[A];
int n,zero,tot;

void init(){
    zero = tot = 0;
    memset(b,0,sizeof(b));
    for(int i=1 ;i<=n ;i++) for(int j=62 ;j>=0 ;j--){
        if((a[i]>>j)&1){
            if(b[j]) a[i] ^= b[j];
            else{
                b[j] = a[i];tot++;
                for(int k=j-1 ;k>=0 ;k--) if(b[k] && ((b[j]>>k)&1)) b[j] ^= b[k];
                for(int k=j+1 ;k<=62;k++) if(((b[k]>>j)&1))         b[k] ^= b[j];
                break;
            }
        }
    }
    zero = (tot<n);tot = 0;
    for(int i=0 ;i<=62 ;i++) if(b[i]) c[tot++] = b[i];
}

ll solve(ll k){
    if(zero) k--;
    if(k >= (1LL<<tot)) return -1;
    ll ans = 0;
    for(int i=0 ;i<=62 ;i++) if((k>>i)&1) ans ^= c[i];
    return ans;
}

int main(){
    int T,_=1;scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(int i=1 ;i<=n ;i++) scanf("%I64d",&a[i]);
        init();
        int q;scanf("%d",&q);
        printf("Case #%d:\n",_++);
        while(q--){
            ll k;scanf("%I64d",&k);
            printf("%I64d\n",solve(k));
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/cutemush/p/12761779.html