首页 > 其他 > 详细

HDU3949 异或线性基

时间:2020-04-23 17:42:40      阅读:56      评论:0      收藏:0      [点我收藏+]

原文链接: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;
}

 

HDU3949 异或线性基

原文:https://www.cnblogs.com/cutemush/p/12761779.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!