可得应当优先寻找最大的2^n-1这个数

如果l的位数不等于r的位数,那么这个数 2^n-1 就是最优解(每一位全为1)

如果l和r的位数相同,先看r是否符合 2^n-1,符合直接返回,不符合的话拆除最高位继续寻找

例如 l=10 r=14

即1010~1110 B

l和r位数相同且r不全为1

则可以拆除最高位1后

寻找10~110B中位数最大的最小数

位数不同,直接找到11B返回

则最终答案为1000B+11B=8+3=11

#include<stdio.h>
typedef long long ll;
ll fd(ll a,ll b)
{
ll i;
for(i=;i<=b+;i*=);
i/=;//找出小于等于i的最大的2的幂次
if(a<i)
return i-;
else
return fd(a-i,b-i)+i;//拆出最高位的1,然后寻找其余低位上的最大符合题意的值
}
int main(){
int T,t;
ll a,b;
scanf("%d",&T);
for(t=;t<T;t++)
{
scanf("%lld%lld",&a,&b);
printf("%lld\n",fd(a,b));
} return ;
}

ZJNU 1699 - Bits的更多相关文章

  1. HDU3047 Zjnu Stadium 【带权并查集】

    HDU3047 Zjnu Stadium Problem Description In 12th Zhejiang College Students Games 2007, there was a n ...

  2. bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队 分块

    1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec  Memory Limit: 64 MB Description 每天,农夫 John ...

  3. [LeetCode] Number of 1 Bits 位1的个数

    Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...

  4. [LeetCode] Reverse Bits 翻转位

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  5. 【leetcode】Number of 1 Bits

    题目描述: Write a function that takes an unsigned integer and returns the number of '1' bits it has (als ...

  6. Leetcode-190 Reverse Bits

    #190. Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 432615 ...

  7. CodeForces 485C Bits[贪心 二进制]

    C. Bits time limit per test1 second memory limit per test256 megabytes inputstandard input outputsta ...

  8. uva12545 Bits Equalizer

    uva12545 Bits Equalizer You are given two non-empty strings S and T of equal lengths. S contains the ...

  9. LeetCode Counting Bits

    原题链接在这里:https://leetcode.com/problems/counting-bits/ 题目: Given a non negative integer number num. Fo ...

随机推荐

  1. ACM-牛喝水

    题目描述:牛喝水  The cows have a line of 20 water bowls from which they drink. The bowls can be either righ ...

  2. 【LeetCode】最长连续序列

    [问题]给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [, , , , , ] 输出: 解释: 最长连续序列是 [, , , ].它的长度为 ...

  3. jQuery搜索框输入实时进行查询

    在手机上,我们期望在搜索框中输入数据,能够实时更新查询出来的内容,不需要按回车. 实现方式为: $(".search").bind("input propertychan ...

  4. Java学习笔记(一) 面向对象---封装

    面向对象---封装 封装是面向对象思想的三大特征之一. 理解: 隐藏对象的属性和实现细节,仅对外提供公共访问方式. 优点: 将变化隔离 便于使用 提升代码复用性 提高安全性 封装原则: 将不需要对外提 ...

  5. 安装yii2 框架遇到的问题

    1按要求安装好yii2时,访问yii2欢迎页面时,始终提示 CAssetManager.basePath “/assets” is invalid. Please make sure the dire ...

  6. CMD命令实现数字雨

    首先,我们在桌面上先创建一个“.bat”格式的文件,文件名为“数字雨.bat”,创建好了之后右键点击编辑. 在其中输入: @echo off title 数字雨 color 0a setlocal E ...

  7. oo第四单元及课程总结

    一.第四单元作业总结 第四单元有两次作业,第十三次作业是实现一个UML类图解析器,可以通过输入一些查询指令来查询一些类图的信息.程序的主干部分已经提供,我们的任务就是实现给出的接口,过程并不繁琐.第十 ...

  8. SQL字符替换函数translater, replace

    translate() 函数原型是:translate(string, from, to) SELECT TRANSLATE('12345', '134', 'ax') 得到:a2x5 这个函数会把f ...

  9. 浅谈无字母数字构造webshell

    0x00 问题 <?php include 'flag.php'; if(isset($_GET['code'])){ $code = $_GET['code']; if(strlen($cod ...

  10. POJ 1273:Drainage Ditches 网络流模板题

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63339   Accepted: 2443 ...