【链接】 我是链接,点我呀:)

【题意】

让你求出l~r当中二进制表示1的个数最多的数x

【题解】

最多有64位
我们可以从l开始一直增大到r
怎么增大?
找到l的二进制表示当中0所在的位置
假设i这一位的0经过加法变成了1
那么我们再从低位到高位依次枚举那一位为1就好
然后把这个二进制转换成十进制
看看它是不是比ri来的小
如果是,则尝试更新最小值
a[i]=(int)一个long类型的数字%2
a[i]是Int
会出错..

另解

从低位到高位依次让每个0bit都变成1bit

看看新变成的数字是否小于等于ri就好.

如果小于等于,那么就继续尝试让更高位的0变成1

因为是从低位到高位,因此每次增加1个0的代价肯定都是最小的

        cin >>l>>r;
for(long long i=1;(l|i)<=r;i<<=1)l|=i;
cout <<l<<endl;

【代码】

import java.io.*;
import java.util.*; public class Main { static InputReader in;
static PrintWriter out; public static void main(String[] args) throws IOException{
//InputStream ins = new FileInputStream("E:\\rush.txt");
InputStream ins = System.in;
in = new InputReader(ins);
out = new PrintWriter(System.out);
//code start from here
new Task().solve(in, out);
out.close();
} static int N = 50000;
static class Task{
int n,len;
long li,ri;
long Bits[]; public void solve(InputReader in,PrintWriter out) {
Bits = new long[100];
n = in.nextInt();
for (int ii = 1;ii <= n;ii++) {
li = in.nextLong();ri = in.nextLong();
long ans = li;int ans1 = 0;
for (int j = 63;j >= 1;j--){
Bits[j] = li%2;
li = li/2;
ans1 += Bits[j];
} //out.println("ans="+ans);
//out.println("ans1="+ans);
long temp = ans;
int temp1 = ans1;
long now = 1;
for (int j = 63;j >= 1;j--) {
temp = temp - now*Bits[j];
temp1-=Bits[j];
if(Bits[j]==0) {
temp1 = temp1+1;
temp = temp + now;
//out.println("temp="+temp+" now = "+now);
if (temp<=ri) {
if (temp1>ans1) {
ans1 = temp1;
ans = temp;
}else if (temp1==ans1 && temp<ans) {
ans = temp;
}
}
long temp2 = 1,cur = 0;
for (int k = 1;k <= (63-j);k++) {
cur = cur + temp2;
if (temp + cur<=ri) {
if (temp1 + k > ans1) {
ans1 = temp1 + k;
ans = temp + cur;
}else if (temp1+k==ans1 && temp+cur<ans) {
ans = temp + cur;
}
}else break;
temp2 = temp2*2;
}
temp = temp - now;
temp1 = temp1-1;
}
now = now * 2;
}
out.println(ans);
//out.println(ans1);
} }
} static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer; public InputReader(InputStream ins) {
br = new BufferedReader(new InputStreamReader(ins));
tokenizer = null;
} public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
} public int nextInt() {
return Integer.parseInt(next());
} public long nextLong() {
return Long.parseLong(next());
}
}
}

【Codeforces 484A】Bits的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  3. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  4. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  5. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  6. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  7. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  8. 【Codeforces 670C】 Cinema

    [题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...

  9. 【codeforces 515D】Drazil and Tiles

    [题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...

随机推荐

  1. MySQL - MyCat 实现读写分离

    前言 MyCat是一个彻底开源的,面向企业应用开发的大数据库集群,支持事务.ACID.可以替代MySQL的加强版数据库.其功能有可以视为MySQL集群的企业级数据库,用来替代昂贵的Oracle集群.融 ...

  2. CAS和Oauth2的区别

    CAS是单点登陆(SSO) Oauth2是对某种资源进行授权访问

  3. P2597 [ZJOI2012]灾难(倍增LCA+拓扑排序)

    传送门 据大佬说这玩意儿好像叫灾难树还是灭绝树? 我们先按建图,设点$u$的食物有$x[1]...x[k]$,即在图中这些点都有一条指向它的边 以样例来说,对于人,羊和牛都有一条指向它的边,然而不管是 ...

  4. 【React Native】React Native项目设计与知识点分享

    闲暇之余,写了一个React Native的demo,可以作为大家的入门学习参考. GitHub:https://github.com/xujianfu/ElmApp.git GitHub:https ...

  5. java自学-方法

    上节介绍了流程控制语句,一个复杂的业务逻辑会由很多java代码组成,包含许多功能.比如说购物业务,就包含选商品.下单.支付等功能,如果这些功能的代码写到一起,就会显得很臃肿,可读性非常不好.java提 ...

  6. 读懂mysql慢查询日志

    我们来看一下如何去读懂这些慢查询日志.在跟踪慢查询日志之前,首先你得保证最少发生过一次慢查询.如果你没有可以自己制造一个:root@server# mysql -e 'SELECT SLEEP(8); ...

  7. Spring boot 分环境部署

    一.如果配置文件为:application.properties时 1.application.properties用于填些公共文件 以下为不同环境的配置文件需要单独配置 application-de ...

  8. BST二叉查找树转双向链表DoubleLinke

    问题:在不创建任何新的节点的情况下,实现将一颗BST变成有序的双向链表. 分析: 在结构上,如图的一颗BST,每个节点都有left right指针分别指指向左右儿子.结构上和双向链表节点是完全相同的. ...

  9. 用纯函数式思维在Java8下写的一段奇葩程序

    首先说一下什么是纯函数式.在我的理解,"纯函数式"用一句话就可以描述:Anything is value.--我的理解不一定准确,但我就是这么理解的. 就是所有的东西都是值--没有 ...

  10. 新开的坑-python学习笔记(1)——连接符与转义

    1/print输出格式虽然知道怎么用却还要打破砂锅的问问题... 问题答案是 需要读很多基础文档教程 --------例如 "+"  . "," 作为连接符的作 ...