zoukankan      html  css  js  c++  java
  • 数学—线性基

    //模板
    const int maxbit = 63;		//maxbit不能太大
    
    struct L_B{
    	ll lba[maxbit], p[maxbit];
    	int cnt;
    	L_B(){
    		memset(lba, 0, sizeof(lba));
    		memset(p, 0, sizeof(p));
    		cnt = 0;
    	}
    	
    /*
    	bool Insert(ll val){		//插入
    		for(int i = maxbit - 1; i >= 0; -- i){
    			if(val & (1ll << i)){
    				if(!lba[i]){
    					lba[i] = val;
    					break;
    				}
    				val ^= lba[i];
    			}
    		}
    		return val > 0;
    	}
    */
    	void Insert(ll val){		//插入
    		for(int i = maxbit - 1; i >= 0; -- i)
    			if(val & (1ll << i)){
    				if(!lba[i]){
    					lba[i] = val;
    					break;
    				}
    				val ^= lba[i];
    			}
    	}
    	
    /*	不常用
    	ll Size(){
    		ll num = 0;
    		for(int i = maxbit; i >= 0; -- i){
    			if(lba[i]) num ++;
    		}
    		return num;
    	}
    */
    
    	ll query_max(){		//查找最大值
    		ll res = 0;
    		for(int i = maxbit - 1; i >= 0; -- i){
    		    ll che = res ^ lba[i];
    			if(che > res) res^=lba[i];
    		}
    		return res;
    	}
    
    	ll query_min(){		//查找最小值
    		for(int i = 0; i <= maxbit; ++ i)
    			if(lba[i])
    				return lba[i];
    		return 0;
    	}
    
    	void rebuild(){		//重构
    		for(int i = maxbit - 1; i >= 0; -- i){
    			for(int j = i - 1; j >= 0; -- j)
    				if(lba[i] & (1ll << j)) lba[i] ^= lba[j];
    		}
    		for(int i = 0; i <= maxbit - 1; ++ i){
    			if(lba[i]) p[cnt ++] = lba[i];
    		}
    	}
    
    	ll query_kth(ll k){		//求第k个
    		int res = 0;
    		if(k >= (1ll << cnt)) return -1;
    		for(int i = maxbit - 1; i >= 0; -- i){
    			if(k & (1ll << i)) res ^= p[i];
    		}
    		return res;
    	}
    
    	bool check(ll x){		//判断x在不在这个线性基中
    		for(int i= maxbit - 1; i >= 0 ; -- i)
    			if(x & (1ll << i)) x ^= lba[i];
    		return x==0;
    	}
    
    };
    
    
    L_B Merge(const L_B &n1, const L_B &n2){	//合并
    	L_B res = n1;
    	for(int i = maxbit - 1; i >= 0; -- i)
    		if(n2.lba[i]) res.Insert(n1.lba[i]);
    	return res;
    }
    
    
    
  • 相关阅读:
    670. Maximum Swap
    653. Two Sum IV
    639. Decode Ways II
    636. Exclusive Time of Functions
    621. Task Scheduler
    572. Subtree of Another Tree
    554. Brick Wall
    543. Diameter of Binary Tree
    535. Encode and Decode TinyURL
    博客园自定义背景图片
  • 原文地址:https://www.cnblogs.com/A-sc/p/12725582.html
Copyright © 2011-2022 走看看