牛客寒假6-G.区间或和

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

题意:

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

思路:

求a-b的差的每一个二进制位

自己也看不懂。。。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;


int main()
{
    LL a,b;
    while(cin >> a >> b)
    {
        LL res = a;
        LL x = 1;
        while (a + x <= b)
        {
            res |= (x + a);
            x <<= 1;
        }
        cout << (res|b) << endl;
    }

    return 0;
}

  

posted @ 2019-02-05 17:13  YDDDD  阅读(87)  评论(0编辑  收藏  举报