线性基模板(线性基)

stO ppl Orz

蒟蒻写了个模板,通过了对拍检验。

该模板资磁ppl的博客中提到的所有操作,除了getmin和getmax的意义不同。

蒟蒻的getmin和getmax是求线性基内能与x异或出的最小/大值。那么ppl的getmax等价于这里的getmax(0)。

可兼容bitset(不能使用getmin,getmax和getkth),可兼容二进制高精度模板

#include<bits/stdc++.h>
using namespace std;
namespace FlashHu{
#define I inline
#define RG register
#define R RG T
#define UI unsigned int
	template<typename T,const UI L>
	struct LB{
		T a[L];
		LB(){
			memset(a,0,sizeof(a));
		}
		I void operator+=(R x){
			for(RG UI i=L-1;x!=0;--i)
				if(((T)1<<i&x)!=0){
					if(a[i]==0){a[i]=x;return;}
					x^=a[i];
				}
		}
		I void operator+=(RG LB&x){
			for(RG UI i=0;i<L;++i)
				*this+=x.a[i];
		}
		I bool exist(R x){
			if(x==0)return 0;
			for(RG UI i=L-1;x!=0;--i)
				if(((T)1<<i&x)!=0){
					if(a[i]==0)return 0;
					x^=a[i];
				}
			return 1;
		}
		I T getmin(R x){
			for(RG UI i=L-1;~i;--i)
				if(x>(x^a[i]))x^=a[i];
			return x;
		}
		I T getmax(R x){
			for(RG UI i=L-1;~i;--i)
				if(x<(x^a[i]))x^=a[i];
			return x;
		}
		I T getkth(R k){
			R x=1;
			RG UI i,j;
			static T b[L];
			for(i=0;i<L;++i,x<<=1)
				for(j=i+1;j<L;++j)
					if((a[j]&x)!=0)a[j]^=a[i];
			for(i=j=0;i<L;++i)
				if(a[i]!=0)b[j++]=a[i];
			if((T)1<<j<=k)return 0;
			x=0;
			for(i=0;i<L;++i)
				if(((T)1<<i&k)!=0)x^=b[i];
			return x;
		}
	};
#undef I
#undef RG
#undef R
#undef UI
}
posted @ 2018-09-16 16:21  Flash_Hu  阅读(902)  评论(5编辑  收藏  举报