Codeforces Round #276 (Div. 2) C

来源:互联网 发布:软件创新之路 编辑:程序博客网 时间:2024/05/22 06:56

C. Bits


        题意:在一个区间[l,r]内找出二进制下"1"最多的数,如果有多解,输出最小的数。

        思路:贪心。从l开始,从低位到高位,逐位或1,如果或结果不比r大,就或上去。

#include<cstdio>#include<iostream>#include<algorithm>using namespace std;#define ll long longint main(){int t;cin>>t;while(t--){ll l,r;cin>>l>>r;ll cur=l;ll tmp=1;while(tmp<r){if((cur|tmp)<=r)cur|=tmp;tmp<<=1;}cout<<cur<<endl;}return 0;}


0 0