• 有关异或问题的专题


    分析:
    不难发现首先我们找到二进制最大的为1的那个数maxx 我们得到的答案起码最高位是1

    然后如果想要答案最大 就想尽可能使得后面为0的均为1 发现 maxx-1 就是后面位均为1

    这样两者异或一定是最大的

    但是可能[L,R]没有将两者包括 也就是说区间最高位都是1 那这样怎么办?

    这时候发现无论我们怎么选两个数 得到的答案最高位一定是0 如果这样的话可以就不用考虑最高位就行 依次递归就好

    #include<cstdio>
    #include<iostream>
    #define LL long long
    using namespace std;
    LL bitcnt(LL x){  //返回x的二进制位数 
    	LL cnt=0;
    	while(x)cnt++,x>>=1;
    	return cnt;
    }
    LL solve(LL l,LL r){
    	LL a=bitcnt(l),b=bitcnt(r);
    	if(a!=b) return (1LL<<b)-1;  //位数相同 
    	LL ans= solve(l&((1LL<<(a-1))-1),r&(((1LL<<(b-1)))-1)); 
    		//位数不同,抹去最高位 
    	return ans;
    }
    int main(){
    	LL a,b;
    	cin>>a>>b;
    	cout<<solve(a,b); 
    }
    

    这个题数据开到非常大都可以

    首先我们可以打表找规律 发现除去第一个数 剩下的就是每四个一轮回 并且这四个一轮回的第1和第3个数是确定的

    这样就好办了

    点击查看代码
    #include<bits/stdc++.h>
    using namespace std;
    #define lowbit(x) x&(-x)
    #define ll long long
    int main(){
    	int n,start;
    	cin>>n>>start;
    	if(n==1){
    		cout<<start<<endl;
    		return 0;
    	}
    	n--;
    	int a1=start^(start+2);
    	int a2=start^(start+2)^(start+4)^(start+6);
    	int t=n%4;
    	if(t==3)cout<<a2<<endl;
    	else if(t==1)cout<<a1<<endl;
    	else if(t==2)cout<<(a1^(n*2+start))<<endl;
    	else if(t==0)cout<<(a2^(n*2+start))<<endl;
         return 0;
    }
    
    
  • 相关阅读:
    Python File readline() 方法
    Python File read() 方法
    Python File next() 方法
    Python File isatty() 方法
    POJ 3281 Dining(最大流板子)
    poj 3436 ACM Computer Factory 最大流+记录路径
    HDU2732 Leapin' Lizards 最大流
    线段覆盖、区间选点、区间覆盖贪心讲解
    顺序表完成教师职称管理系统
    c++派生类中构造函数和析构函数执行顺序、判断对象类型、抽象类、虚函数
  • 原文地址:https://www.cnblogs.com/wzxbeliever/p/16266927.html
Copyright © 2020-2023  润新知