zoukankan      html  css  js  c++  java
  • 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:
    ",_++);
            while(q--){
                ll k;scanf("%I64d",&k);
                printf("%I64d
    ",solve(k));
            }
        }
        return 0;
    }
  • 相关阅读:
    poj3264
    codevs4373 窗口==poj2823 Sliding Window
    BZOJ 3831
    1107 等价表达式
    codevs4600 [NOI2015]程序自动分析==洛谷P1955 程序自动分析
    BZOJ 1050
    1294 全排列[多种]
    BZOJ 2456
    BZOJ 3725
    BZOJ 3043
  • 原文地址:https://www.cnblogs.com/cutemush/p/12761779.html
Copyright © 2011-2022 走看看