zoukankan      html  css  js  c++  java
  • 牛客寒假算法基础集训营6 区间或和(思维)

    区间或和

    链接:https://ac.nowcoder.com/acm/contest/332/G

    题目描述

    求a|(a+1)|(a+2)|...|(b-1)|b。

    其中|表示[按位或](https://baike.baidu.com/item/%E6%8C%89%E4%BD%8D%E6%88%96)。

    输入描述:

    多组输入,每行两个数表示a和b

    输出描述:

    对于每组输入,输出一个数a|(a+1)|(a+2)|...|(b-1)|b。
    示例1

    输入

    99 109
    68 77
    55 66
    34 43
    1111234 1114321

    输出

    111
    79
    127
    47
    1179647

    备注:

    输入不超过10000行,0a,b10^18

    题解:

    如果 a=b ,那么答案 =a ;否则 ab 考虑a和b的二进制表示从高到低第一个不同的位i,必定b的第i位=1,a的第i位=0。那么可以断定,对于答案的二进制表示,


    (1) 比第i位更高的那些位一定跟a相同。(2) 第i位及比第i位更低的那些位一定为1。
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 typedef long long ll;
     7 int main()
     8 {
     9     ll a,b;
    10     while(~scanf("%lld%lld",&a,&b))
    11     {
    12         ll c=b-a;
    13         ll ans=a|b;
    14         ll cnt=1;
    15         while(c)
    16         {
    17             ans|=cnt;
    18             c>>=1;
    19             cnt<<=1;
    20         }
    21         printf("%lld
    ",ans);
    22     }
    23 }
     
  • 相关阅读:
    hadoop wordcount
    hadoop map reduce 实例wordcount的使用
    玉髓
    数据类型检测的四种方式
    天猫前端招聘要求
    正则示例1
    字面量和实例创建的区别
    正则表达式
    面试题1
    this关键字
  • 原文地址:https://www.cnblogs.com/1013star/p/10372404.html
Copyright © 2011-2022 走看看