Codeforces 484A Bits(贪心)

2015-01-27 10:08:51 · 作者: · 浏览: 10

题目链接:Codeforces 484A Bits

题目大意:给定区间l,r,找到一个数x,保证x在区间上,并且要求x的bitcount尽量大的前提下数值尽量小。

解题思路:默认x为全1的二进制数,每次从最高为判断,看最高位的1变为0后大于r,就将该为变成0;落在区间上则即

为要照的答案;小于l则表示该为不能为0.

#include 
   
     #include 
    
      #include 
     
       #include 
      
        using namespace std; typedef long long ll; ll l, r; int main () { int cas; scanf("%d", &cas); while (cas--) { scanf("%lld%lld", &l, &r); ll ans = (1LL<<62)-1; for (int i = 61; i >= 0; i--) { if (r >= ans && ans >= l) break; if ((ans^(1LL<