[code forces 959F] Mahmoud and Ehab and yet another xor task (linear basis)

Title:
Given a sequence a, each query gives ll,xx, and the number of alternative or sum XX schemes in the number of ll before the query
Explanation:
Let the linearity of set VV be beta
For all numbers that can be represented by a linear basis, the number of occurrences is 2|V | | beta | 2|V | | beta|
Since different linear bases will not exceed log(amax)log(amax) when they are added continuously from the beginning to the end, the online query of O(log)O(log) can be achieved only by pre-processing the linear bases of all prefixes.

//by sdfzchy
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int inf=(1<<30);
inline LL in()
{
    char ch=getchar();
    LL f=1,tmp=0;
    while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') {tmp=(tmp<<1)+(tmp<<3)+(ch-'0');ch=getchar();}
    return tmp*f;
}

const int mod=1e9+7;
const int N=100010;
int n,m;
LL a[N],mi[N];

struct LB
{
    LL a[61],cnt;
    bool ins(LL x)
    {
        for(int i=60;i>=0;i--) if(x&(1ll<<i))
            {
                if(!a[i]) {a[i]=x;cnt++;break;}
                x^=a[i];
            }
        return x>0;
    }
}Xor[100];

LL query(const LB &c,LL x,int p)
{
    for(int i=60;i>=0;i--)if(x&(1ll<<i))

        {
            if(!c.a[i]) return 0;
            x^=c.a[i];
        }
    if(x>0) return 0;
    return mi[p-c.cnt];
}

int tot,bel[N];

int main()
{
    n=in();m=in();
    mi[0]=1;
    for(int i=1;i<N;i++) mi[i]=mi[i-1]*2%mod;
    for(int i=1;i<=n;i++)
    {
        LL x=in();
        LB cur=Xor[tot];
        if(cur.ins(x)) Xor[++tot]=cur;
        bel[i]=tot;
    }
    for(int i=1;i<=m;i++)
    {
        LL l=in(),x=in();
        printf("%I64d\n",query(Xor[bel[l]],x,l));
    }
    return 0;
}

Posted by jasonok6 on Wed, 06 Feb 2019 18:54:17 -0800