题目大意:给你两个数l,r(l<r),求一个数是大于等于l且小于等于r的数中二进制数的1的个数最多,如果1的个数相同则取最小的那个(翻译渣,请见谅!)

思路:把左区间L化为二进制,再把左区间的二进制的从最小位开始,每位变为1,因为这是在当前1的个数中最小的且大于L的。条件是小于等于右区间R。

代码:

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <stdio.h>
#include <cstring>
#include<algorithm>
using namespace std;
__int64 bits[];
int main()
{
int t;
__int64 l,r;
scanf("%d",&t);
while(t--)
{
__int64 ans;
memset(bits,,sizeof(bits));
scanf("%I64d%I64d",&l,&r);
ans=l;
int i=;
while(l>)
{
bits[i++]=(l%);
l=l/;
}
int len=i;
for(i=;; i++)
{
bits[i]=;
__int64 temp=;
for(int j=; j<max(len,i+); j++)
{
temp+=(bits[j]<<j);
}
if(temp<=r)
ans=temp;
else
break;
}
printf("%I64d\n",ans);
}
return ;
}

一开始把bits数组定义为int了运行错误,好像是temp+=(bits[j]<<j)这一步位运算爆int 了orz~~

C. Bits (Codeforces Round #276 (Div. 2) )的更多相关文章

  1. codeforces 484a//Bits// Codeforces Round #276(Div. 1)

    题意:给出区间[ll,rr],求中间一个数二进制表示时一的个数最多. 写出ll和rr的二进制,设出现第一个不同的位置为pos(从高位到低位),找的数为x,那么为了使x在[ll,rr]内,前pos-1个 ...

  2. Codeforces Round #276 (Div. 1) A. Bits 二进制 贪心

    A. Bits Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/484/problem/A Des ...

  3. CF&&CC百套计划4 Codeforces Round #276 (Div. 1) A. Bits

    http://codeforces.com/contest/484/problem/A 题意: 询问[a,b]中二进制位1最多且最小的数 贪心,假设开始每一位都是1 从高位i开始枚举, 如果当前数&g ...

  4. Codeforces Round #276 (Div. 1) A. Bits 贪心

    A. Bits   Let's denote as  the number of bits set ('1' bits) in the binary representation of the non ...

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

    这道题直接去构造答案即可. 对于l的二进制表示,从右到左一位一位的使其变为1,当不能再变了(再变l就大于r了)时,答案就是l. 这种方法既可以保证答案大于等于l且小于等于r,也可以保证二进制表示时的1 ...

  6. Codeforces Round #276 (Div. 2) 解题报告

    题目地址:http://codeforces.com/contest/485 A题.Factory 模拟.判断是否出现循环,如果出现,肯定不可能. 代码: #include<cstdio> ...

  7. Codeforces Round #276 (Div. 1) E. Sign on Fence (二分答案 主席树 区间合并)

    链接:http://codeforces.com/contest/484/problem/E 题意: 给你n个数的,每个数代表高度: 再给出m个询问,每次询问[l,r]区间内连续w个数的最大的最小值: ...

  8. Codeforces Round #276 (Div. 1) E. Sign on Fence 二分+主席树

    E. Sign on Fence   Bizon the Champion has recently finished painting his wood fence. The fence consi ...

  9. Codeforces Round #276 (Div. 2)

    A. Factory 题意:给出a,m,第一天的总量为a,需要生产为a%m,第二天的总量为a+a%m,需要生产(a+a%m)%m 计算到哪一天a%m==0为止 自己做的时候,把i开到1000来循环就过 ...

随机推荐

  1. OGR SQL (GEOM)

    The OGRDataSource supports executing commands against a datasource via the OGRDataSource::ExecuteSQL ...

  2. 修改、设置root密码

    参考文档: http://zhidao.baidu.com/link?url=OaUTAj6FrMGDjbPZHWv3NNDOaIl3HNqZz_3lF_Zpi8oZpLkBfnHfPlpgE1EvN ...

  3. paip.输入法编程---输入法ATIaN历史记录 c823

    paip.输入法编程---输入法ATIaN历史记录 c823 作者Attilax ,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csd ...

  4. android Listview,gridview局部刷新,部分刷新

    众所周知Listview和Gridview的刷新界面的方式是调用adapter.notifyDataSetChanged()进行界面刷新. 但是此方法有其弊端,他是将界面中的数据全部刷新一遍,不论数据 ...

  5. WinForm----DataGridview---连接数据库,以及双击一条数据,显示信息到Label控件,也可以是TextBox控件。

    最终效果: 代码: using System; using System.Collections.Generic; using System.ComponentModel; using System. ...

  6. iOS-响应上下左右滑动手势

    -(void)viewDidLoad{ UISwipeGestureRecognizer *recognizer; recognizer = [[UISwipeGestureRecognizer al ...

  7. 官方发布的新版本Qt已经不支持XP了,要自己从源代码编译

    官方发布的新版本Qt已经不支持XP了,要自己从源代码编译,编译选项里有个-target项,用来提供XP支持的,编译时加上-target xp就可以了 http://www.qtcn.org/bbs/r ...

  8. TPersistent的三个用途(读写DFM文件,Assign,RTTI),最主要还是第三个用途

    不是什么类对象都需要RTTI,如果把它放在TObject,除了增加可执行文件的大小以及运行内存空间以外,没什么好处.

  9. 使用 jackson序列格式化日期

    [1]自定义时间,序列化类 [java] view plaincopy package com.fsti.bm.utils; import java.io.IOException; import ja ...

  10. 关于Linux路由表的route命令(转)

    查看 Linux 内核路由表 使用下面的 route 命令可以查看 Linux 内核路由表. # route Destination  Gateway      Genmask          Fl ...