知识点 - 线性基

知识点 - 线性基

解决问题类型:

很多情况下,只要有关异或运算和求最值,就可以用到线性基。线性基有很多很好的性质,比如说如果有很多个数,我们可以构出这些数的线性基,那么这个线性基可以通过互相xor,能够构出原来的数可以相互xor构出的所有的数。所以可以大大减少判断的时间和次数。同时线性基的任何一个非空子集都不会使得其xor和为0,证明也很简单,反证法就可以说明。这个性质在很多题目中可以保证算法合法性,

论文:2014 浅谈线性相关

博客

概念与操作

1、线性基:

若干数的线性基是一组数 a 1 , a 2 , . . . a n a_1,a_2,...a_n a1,a2,...an其中 a x a_x ax的最高位的1在第x位。

通过线性基中元素 x o r xor xor出的数的值域与原来的数 x o r xor xor出数的值域相同。

2、线性基的构造法:

对每一个数 p p p从高位到低位扫,扫到第 x x x位为1时,若 a x a_x ax不存在,则 a x = p ax=p ax=p并结束此数的扫描,否则令 p = p   x o r   a x p=p\ xor\ a_x p=p xor ax

for(int i=1;i<=n;i++) {    
        for(int j=62;j>=0;j--) {
             if(!(a[i]>>j)) continue;//对线性基的这一位没有贡献           
               if(!p[j]) { p[j]=a[i]; break; }//选入线性基中                   
               a[i]^=p[j];
             }
       }

3、查询:

用线性基求这组数 x o r xor xor出的最大值:从高往低扫 a x a_x ax,若异或上 a x a_x ax使答案变大,则异或。

for(int i=62;i>=0;i--) if((ans^p[i])>ans) ans=ans^p[i];//从线性基中得到最大值

4、判断:

用线性基求一个数能否被 x o r xor xor出:从高到低,对该数每个是1的位置x,将这个数异或上 a x a_x ax(注意异或后这个数为1的位置和原数就不一样了),若最终变为0,则可被异或出。当然需要特判0(在构造过程中看是否有p变为0即可)。例子:(11111,10001)的线性基是 a 5 = 11111 , a 4 = 01110 a5=11111,a4=01110 a5=11111a4=01110,要判断11111能否被xor出, 11111   x o r   a 5 = 0 11111\ xor\ a5=0 11111 xor a5=0,则这个数后来就没有是1的位置了,最终得到结果为0,说明11111能被xor出。

复杂度:

例题

[WC2011]最大XOR和路径

codeforces724G div1

[Codeforces Round #228 Div.1 DFox and Perfect Sets

代码

//T2
//It is made by ljh2000
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <complex>
using namespace std;
typedef long long LL;
typedef long double LB;
typedef complex<double> C;
const double pi = acos(-1);
const int mod = 1000000007;
const int MAXN = 200011;
const int MAXM = 400011;
int n,m,ecnt,first[MAXN],to[MAXM],next[MAXM],scnt,dui[MAXN],cir_cnt,r;
LL dis[MAXN],w[MAXM],p[70]/*!!!不要开大了...*/,cir[MAXM],ans,fp[MAXM<<1],cnt[2];//数组不要开小了!!!
bool vis[MAXN],in[MAXN];
inline void link(int x,int y,LL z){ next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y; w[ecnt]=z; }
inline LL fast_pow(LL x,LL y){ LL r=1; while(y>0) { if(y&1) r*=x,r%=mod; x*=x; x%=mod; y>>=1; } return r; }
inline int getint(){
    int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
    if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
}
 
inline LL getlong(){
    LL w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
    if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
}
 
inline void dfs(int x,LL dd,int fa){
    if(dis[x]==-1) dis[x]=dd;
    else {//找环
        cir[++cir_cnt]=dd^dis[x];
        return ;
    }
    vis[x]=1; dis[x]=dd; dui[++scnt]=x;
    for(int i=first[x];i;i=next[i]) {
        int v=to[i]; if(v==fa) continue;
        dfs(v,dd^w[i],x);
    }
}
 
inline void build(){//构线性基
    memset(p,0,sizeof(p)); r=0;
    for(int i=1;i<=cir_cnt;i++) {
        for(int j=62;j>=0;j--) {
            if(!(cir[i]>>j)) continue;
            if(!p[j]) { p[j]=cir[i]; break; }
            cir[i]^=p[j];
        }
    }
    for(int j=0;j<=62;j++) if(p[j]!=0) r++;//计算线性基有效的向量个数
}
 
inline LL calc(){
    build(); LL tot=0,now; bool flag;
    for(int i=0;i<=62;i++) {//按位算贡献
        cnt[0]=cnt[1]=0; flag=false;//是否存在某个向量的这一位为1
        for(int j=0;j<=62;j++) if((p[j]>>i)&1) { flag=true; break; }
        for(int j=1;j<=scnt;j++) cnt[(dis[ dui[j] ]>>i)&1]++;//统计每个dis的这一位是0还是1
 
        now=cnt[0]*(cnt[0]-1)/2+cnt[1]*(cnt[1]-1)/2;//组合的方式记得考虑/2!!!
        now%=mod;
        if(flag) {
            if(r>=1) now*=fp[r-1],now%=mod;
            now*=fp[i],now%=mod;
            tot+=now; tot%=mod;
        }
 
        now=cnt[0]*cnt[1]; now%=mod;
        if(flag) { if(r>=1) now*=fp[r-1],now%=mod; }
        else now*=fp[r],now%=mod;
        now*=fp[i],now%=mod;
        tot+=now; tot%=mod;
    }
    return tot;
}
 
inline void work(){
    n=getint(); m=getint(); int x,y,lim=max(n,m)*2; LL z;
    for(int i=1;i<=m;i++) { x=getint(); y=getint(); z=getlong(); link(x,y,z); link(y,x,z); }
    fp[0]=1; for(int i=1;i<=lim;i++) fp[i]=fp[i-1]*2,fp[i]%=mod;//预处理2的整数幂
    memset(dis,-1,sizeof(dis));
    for(int i=1;i<=n;i++) {
        if(vis[i]) continue;
        scnt=0; cir_cnt=0;
        dfs(i,0,0);
        ans+=calc();
        ans%=mod;
    }
    printf("%I64d",ans);
}
 
int main()
{
    work();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Best KeyBoard

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值