cf C. Bits

http://codeforces.com/contest/485/problem/C

利用位运算来解决这个题,从L开始,从每一位按位或,知道到达r位置,ans=ans|(1<<k);就可以为每一位增加一个1.

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define LL __int64
 5 using namespace std;
 6 
 7 int n;
 8 LL l,r;
 9 
10 int main()
11 {
12     scanf("%d",&n);
13     while(n--)
14     {
15         scanf("%I64d%I64d",&l,&r);
16         LL ans=l;
17         for(int k=0; (ans|((LL)1<<(LL)k))<=r; k++)
18         {
19             ans=ans|((LL)1<<(LL)k);
20         }
21         printf("%I64d
",ans);
22     }
23     return 0;
24 }
View Code
原文地址:https://www.cnblogs.com/fanminghui/p/4079206.html