【瞎搞】 Codeforces Round 276 DIV 2 C.Bits

求L-R 区间内的X的二进制中1 最多的个数

当前 L 为 popcount(L) 中最小的

每次从低到高 在 L为0 的位置上添上1  保证了值的最小


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <cmath>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <map>
#define cler(arr, val)    memset(arr, val, sizeof(arr))
typedef long long  LL;
const int MAXN = 100000+6;
const int MAXM = 140000;
const int INF = 0x3f3f3f3f;
const int mod = 1000000007;
int num(LL x)
{
    int sum=0;
    while(x)
    {
        LL c=x%2;
        x/=2;
        if(c) sum++;
    }
    return sum;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
#endif
    LL n,l,r;
    cin>>n;
    while(n--)
    {
        cin>>l>>r;
        for(int i=60;i>=0;i--)
        {
            LL c=l;
            int cnt=i-num(l);
            for(int j=0;cnt&&c<=r;j++)
            {
                if((c&((LL)1<<j))==0)
                    c|=((LL)1<<j),cnt--;
            }
            if(cnt==0&&c<=r)
            {
                cout<<c<<endl;
                break;
            }
        }
    }
    return 0;

}


posted @ 2014-11-08 21:43  kewowlo  阅读(143)  评论(0编辑  收藏  举报