HDU3949:XOR 高斯消元求线性基+二进制拆分

Orz hzwer

#include<iostream>
#include<cstdio>
#include<cstring>
#define ll long long 
using namespace std;
int now,zero;
ll a[10005];
int T,n,m;
inline ll read()
{
    ll a=0,f=1; char c=getchar();
    while (c<'0'||c>'9') {if (c=='-') f=-1; c=getchar();}
    while (c>='0'&&c<='9') {a=a*10+c-'0'; c=getchar();}
    return a*f;
}
inline void Gauss()
{
    int y; now=1; zero=0;
    for (int k=60;~k;k--)
    {
        for (y=now;y<=n;y++)
            if ((1ll<<k)&a[y]) break;
        if (y>n) continue;
        if (now!=y) swap(a[now],a[y]);
        for (int i=1;i<=n;i++)
            if (i!=now&&((1ll<<k)&a[i])) a[i]^=a[now];
        now++;
    }
    now--;
    if (now!=n) zero=1;
}
ll query(ll x)
{
    ll ans=0;
    x-=zero;
    if (!x) return 0;
    if (x>=(1ll<<now)) return -1;
    for (int i=1;i<=now;i++)
        if (x&(1ll<<(now-i))) ans^=a[i];
    return ans;
}
int main()
{
    T=read();
    for (int t=1;t<=T;t++)
    {
        n=read();
        for (int i=1;i<=n;i++) a[i]=read();
        Gauss();
        m=read();
        printf("Case #%d:\n",t);
        while (m--)
        {
            int x=read();
            printf("%lld\n",query(x));
        }
    }
    return 0;
}

你可能感兴趣的:(HDU3949:XOR 高斯消元求线性基+二进制拆分)