[TJOI2008]彩灯 线性基

题面

题面

题解

题意:给定n个01串,求互相异或能凑出多少不同的01串。
线性基的基础应用。
对于线性基中的01串,如果我们取其中一些凑成一个新的01串,有一个重要的性质:任意2个不同方案凑出的01串也不相同。
因此我们只需要求出给定01串的线性基大小,然后求出有多少搭配方案即可,方案数即为(2^{tot} - 1)

#include<bits/stdc++.h>
using namespace std;
#define R register int
#define AC 55
#define LL long long
#define p 2008

int n, m, ans;
LL f[AC], maxn;
char s[AC];

inline int qpow(int x, int have)
{
    int rnt = 1;
    while(have)
    {
        if(have & 1) rnt = rnt * x % p;
        x = x * x % p, have >>= 1;
    }
    return rnt;
}

void work()
{
    scanf("%d%d", &n, &m), maxn = 1LL << 50;
    for(R i = 1; i <= m; i ++)
    {
        scanf("%s", s + 1);
        LL x = 0;
        for(R j = 1; j <= n; j ++) x = (x << 1) + (s[j] == 'O' ? 1 : 0);
        maxn = 1LL << 50;
        for(R j = 50; ~j; j --, maxn >>= 1)
        {
            if(!(x & maxn)) continue;
            if(!f[j]) {f[j] = x, ++ ans; break;}
            else x ^= f[j];
        }
    }
    printf("%d
", qpow(2, ans));
}

int main()
{
//	freopen("in.in", "r", stdin);
    work();
//	fclose(stdin);
    return 0;
}
原文地址:https://www.cnblogs.com/ww3113306/p/10354311.html