算法笔记--线性基求交模板
https://blog.csdn.net/demon_rieman/article/details/88830846
模板1:
struct LinearBase {
LL v[32];
inline void clr() {memset(v, 0, sizeof(v));}
inline void ins(LL a) {
for (int i = 31; i >= 0; --i) {
if(a&(1LL<<i)) {
if(!v[i]) {
v[i] = a;
break;
}
a ^= v[i];
}
}
}
inline bool can_ex(LL a) {
for (int i = 31; i >= 0; --i) if((a^v[i]) < a) a ^= v[i];
return a == 0;
}
//交
inline friend LinearBase operator * (const LinearBase &a, const LinearBase &b) {
LinearBase all, c, d;
all.clr(), c.clr(), d.clr();
for (int i = 31; i >= 0; --i) {
all.v[i] = a.v[i];
d.v[i] = 1LL<<i;
}
for (int i = 31; i >= 0; --i) {
if(b.v[i]) {
LL v = b.v[i], k = 0;
bool can = true;
for (int j = 31; j >= 0; --j) {
if(v&(1LL<<j)) {
if(all.v[j]) {
v ^= all.v[j];
k ^= d.v[j];
}
else {
can = false;
all.v[j] = v;
d.v[j] = k;
break;
}
}
}
if(can) {
LL v = 0;
for (int j = 31; j >= 0; --j) {
if(k&(1LL<<j)) {
v ^= a.v[j];
}
}
c.ins(v);
}
}
}
return c;
}
};
模板2:
struct LinearBase {
LL v[32];
inline void clr() {memset(v, 0, sizeof(v));}
inline void ins(LL a) {
for (int i = 31; i >= 0; --i) {
if(a&(1LL<<i)) {
if(!v[i]) {
v[i] = a;
break;
}
a ^= v[i];
}
}
}
inline bool can_ex(LL a) {
for (int i = 31; i >= 0; --i) if((a^v[i]) < a) a ^= v[i];
return a == 0;
}
//交
inline friend LinearBase operator * (const LinearBase &a, const LinearBase &b) {
LinearBase all, c, d;
all.clr(), c.clr(), d.clr();
for (int i = 31; i >= 0; --i) all.v[i] = a.v[i];
for (int i = 31; i >= 0; --i) {
if(b.v[i]) {
LL v = b.v[i], k = 1LL<<i;
bool can = true;
for (int j = 31; j >= 0; --j) {
if(v&(1LL<<j)) {
if(all.v[j]) {
v ^= all.v[j];
k ^= d.v[j];
}
else {
can = false;
all.v[j] = v;
d.v[j] = k;
break;
}
}
}
if(can) {
LL v = 0;
for (int j = 31; j >= 0; --j) {
if(k&(1LL<<j)) {
v ^= b.v[j];
}
}
c.ins(v);
}
}
}
return c;
}
};