求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_JUDGEfreopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);
#endifLL 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;}

转载于:https://www.cnblogs.com/kewowlo/p/4088293.html

【瞎搞】 Codeforces Round 276 DIV 2 C.Bits相关推荐

  1. CFCC百套计划4 Codeforces Round #276 (Div. 1) E. Sign on Fence

    http://codeforces.com/contest/484/problem/E 题意: 给出n个数,查询最大的在区间[l,r]内,长为w的子区间的最小值 第i棵线段树表示>=i的数 维护 ...

  2. Codeforces Round #276 (Div. 2)

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

  3. Codeforces Round #635 (Div. 2) D. Xenia and Colorful Gems 暴力 + 二分

    传送门 文章目录 题意: 思路: 题意: 给你三个数组a,b,ca,b,ca,b,c,让你从每个数组中选择一个数x,y,zx,y,zx,y,z,使得(x−y)2+(x−z)2+(y−z)2(x-y)^ ...

  4. Codeforces Round #701 (Div. 2) A ~ F ,6题全,超高质量良心题解【每日亿题】2021/2/13

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 A - Add and Divide B - Replace and Keep Sorted C ...

  5. Codeforces Round FF(Div. 2)

    layout: post title: Codeforces Round FF(Div. 2) author: "luowentaoaa" catalog: true tags: ...

  6. Codeforces Round #246 (Div. 2)

    主题链接:Codeforces Round #246 (Div. 2) A:直接找满足的人数.然后整除3就是答案 B:开一个vis数组记录每一个衣服的主场和客场出现次数,然后输出的时候主场数量加上反复 ...

  7. Codeforces Round #653 (Div. 3)(A, B, C, D, E1详解)

    Codeforces Round #653 (Div. 3) Required Remainder Thinking(binary search) 既然是找最大值问题,我又懒得去推式子,于是我直接就上 ...

  8. Codeforces Round #648 (Div. 2)(A, B, C, D)

    Codeforces Round #648 (Div. 2) 或许更好的阅读体验 A:Matrix Game 思路 题意可以说是非常简单的,我们选定的格子的行列都不能存在1,可以发现我们可以放的格子一 ...

  9. CodeCraft-21 and Codeforces Round #711 (Div. 2)

    CodeCraft-21 and Codeforces Round #711 (Div. 2) 题号 题目 考点 A GCD Sum 签到,模拟 B Box Fitting 贪心 C Planar R ...

最新文章

  1. 如何修改安装包程序的产品描述和版权信息
  2. 学习下如何统计【0-9】在任意给定数中出现的次数
  3. vs 启动调用的目标发生异常_如何解决不可测、异常场景的问题?
  4. 科研汪的日常--“键皇”,静电容的又一座高峰(REALFORCE RFU联名版开箱)
  5. [HNOI2007] 分裂游戏
  6. 【重难点】【Java集合 01】HashMap 和 ConcurrentHashMap
  7. spring mvc 中自定义404页面在IE中无法显示favicon.ico问题的解决方法。
  8. [转]Hibernate不能自动建表解决办法及Hibernate不同数据库的连接及SQL方言
  9. 关于opacity、visibility、display属性的一道CSS面试题
  10. boost升压斩波电路 分析
  11. photoshop 插件_Photoshop的线性光
  12. Mac端视频字幕压制:HandBrake/Premute/松果压制
  13. 计算机课程设计答辩评语,课程设计评语模板.doc
  14. 临床数据库挖掘系列3-手把手教你使用R语言对seer数据库清洗
  15. 家里宽带网络连接第二台路由器实验一
  16. Android 手机红外遥控器实现
  17. 武汉地铁站点最短路径搜索的实现(一)——Dijkstra算法(资料收集)
  18. JavaScript自用
  19. octave java_Octave快速入门
  20. 企业邮箱如何代理?企业邮箱哪家好用?

热门文章

  1. 分区partition是否只保存一部分数据?
  2. spring boot的hello world小实验
  3. 为什么是hbase而不是mongodb
  4. Saleor ran into an unexpected problem
  5. GPU代码修改成TPU代码
  6. SVM入门(八)松弛变量(转)
  7. 吴恩达深度学习一:神经网络
  8. 2.2 矩阵基本运算
  9. mysql :The user specified ... does not exist 报错
  10. React解决长列表方案(react-virtualized)