线性基 P3857 [TJOI2008]彩灯

线性基 P3857 [TJOI2008]彩灯

这个题目没写出来。。。

好傻啊,看了题解才会的。。。

因为这个n只有50,是在 $long ,, long $ 的范围之内的,做线性基的题目应该要对这些数字比较敏感,你可以把 (O) 当作 1,把 (X) 当作0,所以这个字符串就转化成一些数字了,要不同有多少个,其实就是求数字有多少个。

这个就和线性基有关系了,线性基内的元素都是线性无关,也就是任意几个组成在一起异或得到的值一定是唯一的一个数。

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define inf64 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
long long d[70],tot;
void add(long long x) {
    for (int i = 60; i >= 0; i--) {
        if (x & (1ll << i)) {
            if (d[i]) {
                x ^= d[i];
            } else {
                d[i] = x;
                break;//²åÈë³É¹¦¾ÍÍ˳ö
            }
        }
    }
    tot = 0;
    for(int i=60;i>=0;i--) if(d[i]) tot++;
}
char s[110][110];
int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        scanf("%s",s[i]+1);
        ll now = 0;
        for(int j=1;j<=n;j++){
            int x = 0;
            if(s[i][j]=='O') x = 1;
            //因为O表示控制,开始又都是不开的情况,所以O要表示1
            now = now*2+x;
        }
        add(now);
    }
//    printf("tot=%d
",tot);
    ll ans = 1ll<<tot;
    printf("%lld
",ans%2008);
    return 0;
}
/*
3 2
XX
XO
OX
*/

原文地址:https://www.cnblogs.com/EchoZQN/p/13399452.html