码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #276 (Div. 2)C. Bits(构造法)

时间:2014-11-06 23:30:09      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   os   sp   for   div   on   

这道题直接去构造答案即可。

对于l的二进制表示,从右到左一位一位的使其变为1,当不能再变了(再变l就大于r了)时,答案就是l。

这种方法既可以保证答案大于等于l且小于等于r,也可以保证二进制表示时的1最多。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<cctype>
#include<sstream>
using namespace std;
#define pii pair<int,int>
#define LL long long int
const int eps=1e-8;
const int INF=1000000000;
const int maxn=10000+5;
int n;
LL l,r,p;

int main()
{
    //freopen("in9.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    scanf("%d",&n);
    while(n--)
    {
        scanf("%I64d%I64d",&l,&r);
        p=1;
        while(1)
        {
            if((l|p)>r)
            {
                printf("%I64d\n",l);
                break;
            }
            else
            {
                l|=p;
                p*=2;
            }
        }
    }
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}

 

Codeforces Round #276 (Div. 2)C. Bits(构造法)

标签:style   blog   io   color   os   sp   for   div   on   

原文地址:http://www.cnblogs.com/zywscq/p/4080089.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!