zoukankan      html  css  js  c++  java
  • #线性基#LOJ 114 k大异或和

    题目


    分析

    建出线性基后,但是要求最小所以要重建线性基让大的尽量小,
    然后第k小就是拼凑


    代码

    #include <cstdio>
    #include <cctype>
    #include <cstring>
    #define rr register
    using namespace std;
    typedef long long lll;
    const int N=51; int n;
    inline lll iut(){
    	rr lll ans=0; rr char c=getchar();
    	while (!isdigit(c)) c=getchar();
    	while (isdigit(c)) ans=(ans<<3)+(ans<<1)+(c^48),c=getchar();
    	return ans;
    }
    inline void print(lll ans){
    	if (ans<0) putchar('-'),ans=-ans;
    	if (ans>9) print(ans/10);
    	putchar(ans%10+48); 
    }
    struct Vector_Space{
    	lll re[N],p[N],tot;
    	inline void BUILD(){
    		memset(re,0,sizeof(re)),
    		memset(p,0,sizeof(p)),tot=0;
    	}
    	inline void Insert(lll x){
    		for (rr int i=N-1;~i;--i)
    		if ((x>>i)&1){
    			if (!re[i]) {re[i]=x; return;}
    			x^=re[i];
    		}
    	}
    	inline void Rebuild(){
    		for (rr int i=1;i<N;++i){
    			for (rr int j=i-1;~j;--j)
    			if ((re[i]^re[j])<re[i])
    			    re[i]^=re[j];
    		}
    		for (rr int i=0;i<N;++i)
    		    if (re[i]) p[tot++]=re[i];
    	}
    	inline lll query(lll x){
    		for (rr int i=tot-1;~i;--i)
    		    if ((x^p[i])>x) x^=p[i];
    		return x;
    	}
    	inline lll Kth(lll kth){
    		if (tot^n) --kth;
    		if (kth>=(1ll<<tot)) return -1;
    		rr lll ans=0;
    		for (rr int i=0;i<tot;++i)
    		    if ((kth>>i)&1) ans^=p[i];
    		return ans; 
    	}
    }H;
    signed main(){
    	n=iut(),H.BUILD();
    	for (rr int i=1;i<=n;++i)
    	    H.Insert(iut());
    	H.Rebuild();
    	for (rr int Q=iut();Q;--Q)
    		print(H.Kth(iut())),putchar(10);
    	return 0;
    } 
    
  • 相关阅读:
    多层感知机的简洁实现
    汇编语言总结(1)
    回顾下TCP/IP协议
    五笔输入法的前世今生
    大O算法
    算法基础概念
    TCP和UDP的区别
    数据结构基本概念和术语
    gdb调试方法
    GD32F20x系列使用问题总结
  • 原文地址:https://www.cnblogs.com/Spare-No-Effort/p/13930157.html
Copyright © 2011-2022 走看看