zoukankan      html  css  js  c++  java
  • [HDU5969] 最大的位或

    题目类型:位运算

    传送门:>Here<

    题意:给出(l)(r),求最大的(x|y),其中(x,y)([l,r])范围内

    解题思路

    首先让我想到了前面那题(Bits),然而并不是1越多越好,而是越前面越好(于是就(WA)了……)

    其实很简单。分类讨论:

    如果左右边界转为二进制后长度不等,那么左边界一定能够做到全为1,且长度为右边界-1.再或以下就又变长了一格了。于是答案很明显是(2^{len(r)}-1)

    如果长度相等,我们发现他们肯定有公共前缀。而从高到低第一个不同的位上,左边界的那一位一定是0,右边界一定是1。因此只需要把这之后的全部变为1就可以了。

    反思

    概念不要混淆,二进制里数字大并不是1越多越好,而是越前越好

    依然是分类讨论,分类讨论太重要了!

    Code

    /*By DennyQi 2018*/
    #include <cstdio>
    #include <queue>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    typedef long long ll;
    #define int ll
    const int MAXN = 10010;
    const int MAXM = 20010;
    const int INF = 1061109567;
    inline int Max(const int a, const int b){ return (a > b) ? a : b; }
    inline int Min(const int a, const int b){ return (a < b) ? a : b; }
    inline int read(){
        int x = 0; int w = 1; register char c = getchar();
        for(; c ^ '-' && (c < '0' || c > '9'); c = getchar());
        if(c == '-') w = -1, c = getchar();
        for(; c >= '0' && c <= '9'; c = getchar()) x = (x<<3) + (x<<1) + c - '0'; return x * w;
    }
    int T,mx,p,t,l,r;
    int b1[100],b2[100],len1,len2;
    inline int solve(int l, int r){
    	len1 = len2 = 0;
    	memset(b1, 0, sizeof b1);
    	memset(b2, 0, sizeof b2);
    	int x = l,res=0;
    	while(x > 0){
    		b1[++len1] = x % 2;
    		x /= 2;
    	}
    	x = r;
    	while(x > 0){
    		b2[++len2] = x % 2;
    		x /= 2;
    	}
    	if(len1 != len2){
    		return (1ll<<len2)-1;
    	}
    	for(int i = len2; i; --i){
    		if(b1[i] == b2[i]){
    			res += b1[i] * (1ll<<(i-1));
    		}
    		else{
    			res += (1ll<<i)-1;
    			return res;
    		}
    	}
    	return l;
    }
    signed main(){
    	T = read();
    	while(T--){
    		l = read(), r = read();
    		printf("%lld
    ", solve(l,r));
    	}
    	return 0;
    }
    
  • 相关阅读:
    读书笔记-NIO的工作方式
    高精度运算-阶乘累积求和
    面试题-Java设计模式举例
    Web请求过程总结
    python 之栈的实现
    python之数据结构链表实现方式
    python 之分发包
    python之smtplib发邮件
    装饰器习题-接受参数的装饰器
    python之装饰器
  • 原文地址:https://www.cnblogs.com/qixingzhi/p/9744745.html
Copyright © 2011-2022 走看看