Let LL denote the number of 1s in integer DD’s binary representation. Given two integers S1S1 and S2S2, we call DD a WYH number if S1≤L≤S2S1≤L≤S2. 
With a given DD, we would like to find the next WYH number YY, which is JUST larger than DD. In other words, YY is the smallest WYH number among the numbers larger than DD. Please write a program to solve this problem. 

InputThe first line of input contains a number TT indicating the number of test cases (T≤300000T≤300000). 
Each test case consists of three integers DD, S1S1, and S2S2, as described above. It is guaranteed that 0≤D<2310≤D<231 and DD is a WYH number. 
OutputFor each test case, output a single line consisting of “Case #X: Y”. XX is the test case number starting from 1. YY is the next WYH number.Sample Input

3
11 2 4
22 3 3
15 2 5

Sample Output

Case #1: 12
Case #2: 25
Case #3: 17

题目需要求的是比d大的且转化为二进制后1的个数在s1和s2之间的最小的数

开始想的是从d开始判断yi的个数分比s1小,在s1、s2之间(这里考虑的特别复杂),比s2大三种情况考虑,结果写了一大堆判断最后完美wa

后来在网上看别人的代码,是先让d+1,因为最后得到的数是比d大的,然后也是三种情况考虑,但是如果在s1、s2之间就可以直接输出,接着是比s1小则遇到0直接变成1,比s2大则遇到1变成0然后再一次进位
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<cmath>
#include<queue>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
int a[50], j;
ll sum() {ll ans = 0, t = 1;for( int i = 0; i <= 33; i ++ ) {ans = ans + a[i] * t;t *= 2;}return ans;
}
int main() {std::ios::sync_with_stdio(false);int T, cnt = 0;cin >> T;while( T -- ) {cnt ++;ll n, num = 0, x, y;j = 0;cin >> n >> x >> y;n ++;memset( a, 0, sizeof(a) );while( n ) {if( n % 2 == 1 ) {a[j++] = 1;num ++;} else {a[j++] = 0;}n /= 2;}while( 1 ) {if( num >= x && num <= y ) {cout << "Case #" << cnt << ": " << sum() << endl;break;}if( num < x ) {for( int i = 0; ; i ++ ) {if( a[i] == 0 ) {a[i] = 1;num ++;break;}}} else {int i = 0;while( a[i] == 0 ) {i ++;}a[i] ++;while( a[i] == 2 ) {a[i] = 0;num --;a[i+1] ++;i ++;}num ++;}}}return 0;
}

转载于:https://www.cnblogs.com/l609929321/p/9013054.html

hdu5491 The Next 模拟相关推荐

  1. springboot实现SSE服务端主动向客户端推送数据,java服务端向客户端推送数据,kotlin模拟客户端向服务端推送数据

    SSE服务端推送 服务器向浏览器推送信息,除了 WebSocket,还有一种方法:Server-Sent Events(以下简称 SSE).本文介绍它的用法. 在很多业务场景中,会涉及到服务端向客户端 ...

  2. curl模拟post请求

    另外可尝试 postman工具 或者用request 直接请求 CURL 发送POST请求curl -header "Content-Type: application/json" ...

  3. flask_模拟请求post,get

    #coding:utf-8 import requestsres = requests.post(url="http://192.168.135.105:8888/",data={ ...

  4. 模拟内存计算如何解决边缘人工智能推理的功耗挑战

    模拟内存计算如何解决边缘人工智能推理的功耗挑战 How analog in-memory computing can solve power challenges of edge AI inferen ...

  5. 为放大器模拟输入模块提供可靠的输入过电压保护

    为放大器模拟输入模块提供可靠的输入过电压保护 Signal Chain Basics #159: Provide robust input overvoltage protection for amp ...

  6. 模拟Servlet本质

    JavaWeb系列教程,持续更新 JavaWeb-Servlet 模拟Servlet本质 使用IDEA开发Servlet程序 Servlet对象的生命周期 适配器(GenericServlet)改造S ...

  7. 2021年大数据Flink(四十):​​​​​​​Flink模拟双十一实时大屏统计

    目录 Flink模拟双十一实时大屏统计 需求 数据 编码步骤: 1.env 2.source 3.transformation 4.使用上面聚合的结果,实现业务需求: 5.execute 参考代码 实 ...

  8. Python:模拟登录、点击和执行 JavaScript 语句案例

    案例一:网站模拟登录 # douban.pyfrom selenium import webdriver from selenium.webdriver.common.keys import Keys ...

  9. 杨老师课堂_Java核心技术下之控制台模拟文件管理器案例

    背景需求介绍: 编写一个模拟文件管理器的程序,实现控制台对文件和文件夹的管理操作. 要求在此程序中: 当用户输入指令 1 时,代表"指定关键字检索文件",此时需要用户输入检索的目录 ...

最新文章

  1. andorid 启动模式面试题
  2. Nature综述:菌根共生的独特性和共性
  3. ISME:Micrarchaeota和Parvarchaeota古菌门的代谢多样性
  4. 量子领域、人工智能都是佼佼者,中国科技实力详解
  5. bzoj 3223: Tyvj 1729 文艺平衡树
  6. 系列教程丨用 Docker 探索开源软件 —— PostgreSQL(一)
  7. pythonqueue函数_如何将函数和参数放入python队列?
  8. js修改mysql数据库数据_Node.js操作mysql数据库增删改查
  9. sphinx 入门_Sphinx搜索引擎入门
  10. 图解MongoChef的安装步骤
  11. iOS App 签名的原理 App 重签名(二)
  12. UmiJS基础UmiJS+Dva
  13. c语言 获取硬盘序列号,获取硬盘序列号的C++代码
  14. 如何配置Gitlab的双因子验证(Two-Factor Authentication)
  15. 平安科技软件+金山WPS测试面试题
  16. MNE预处理脑电数据
  17. 电脑商城-02-注册
  18. 安卓程序打包到安卓手机上运行Android程序
  19. 通俗易懂,unity和c#是什么关系
  20. 对于业务中库存超卖测试

热门文章

  1. android webview开启html5支持
  2. Java开源搜索引擎
  3. cron计划任务使用
  4. 大数据教程(13.6)sqoop使用教程
  5. java8学习:新的日期使用
  6. 理解MVC—从实例出发:基于MVC模式的简易算术计算器
  7. 浅谈Http模块,Express和Koa实现http服务
  8. 一些C实现的数学函数实现(估算)
  9. vlan简介,access、trunk、hybrid的区别
  10. Android 7.0 WifiMonitor工作流程分析