题面
题解
题意:给定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\n", qpow(2, ans));
}
int main()
{
// freopen("in.in", "r", stdin);
work();
// fclose(stdin);
return 0;
}
本文介绍了一种利用线性基解决01串异或组合问题的算法。核心在于通过线性基找到给定01串集合的独立基底,进而计算不同01串异或组合的总数。文章提供了完整的代码实现,包括输入处理、线性基构建及最终方案数的计算。
403

被折叠的 条评论
为什么被折叠?



