zoukankan      html  css  js  c++  java
  • 算法笔记--线性基求交模板

    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;
        }
    };
    
  • 相关阅读:
    前端:js
    HTML和CSS总结
    前端二:CSS
    前端一:走进HTML
    SQLALchemy(连表)、paramiko
    上下文管理、线程池、redis订阅和发布
    P4234 最小差值生成树
    P2387 [NOI2014]魔法森林
    P3721 [AH2017/HNOI2017]单旋
    P4271 [USACO18FEB]New Barns
  • 原文地址:https://www.cnblogs.com/widsom/p/11261288.html
Copyright © 2011-2022 走看看