Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 740    Accepted Submission(s): 294


Problem Description
Let L denote
the number of 1s in integer D’s
binary representation. Given two integers S1 and S2,
we call D a
WYH number if S1≤L≤S2.

With a given D,
we would like to find the next WYH number Y,
which is JUST larger than D.
In other words, Y is
the smallest WYH number among the numbers larger than D.
Please write a program to solve this problem.
 

Input
The first line of input contains a number T indicating
the number of test cases (T≤300000).

Each test case consists of three integers D, S1,
and S2,
as described above. It is guaranteed that 0≤D<231 and D is
a WYH number.
 

Output
For each test case, output a single line consisting of “Case #X: Y”. X is
the test case number starting from 1. Y 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
 

Source

2015 ACM/ICPC Asia Regional Hefei Online

这题自己没有想到,比赛时是队友做的,后来知道了做法。先把D加1变成m,然后判断m是否满足条件,如果满足就直接输出m,如果不满足,那么有两种情况,第一种是二进制后的1的个数小于s1,那么我们只要从右往左找第一个0,使得其变为1(即加上2^i),如果二进制后的1的个数大于s1,那么我们只要从右往左找第一个1,然后加上(2^i),使其变为0,然后继续下去。

这种方法可行的原因是每次都是使改变最少,所以得到的值一定是成立中最小的。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define ll long long
#define inf 0x7fffffff
int main()
{
ll n,m,i,j,T,t1,t2,wei,num1=0,num;
int a[40];
scanf("%lld",&T);
while(T--)
{
scanf("%lld%lld%lld",&n,&t1,&t2);
n++;
while(1){
m=n;
wei=0;num=0;
while(m){
a[++wei]=m%2;
if(m%2==1)num++;
m/=2;
}
if(num<t1){
for(i=1;i<=wei;i++){
if(a[i]==0){
j=i;break;
}
}
n+=(1<<(j-1));
}
else if(num>t2){
for(i=1;i<=wei;i++){
if(a[i]==1){
j=i;break;
}
}
n+=(1<<(j-1) ); }
else break;
}
num1++;
printf("Case #%lld: %lld\n",num1,n); }
return 0;
}

hdu5491 The Next的更多相关文章

  1. hdu-5491 The Next(贪心)

    题目链接: The Next Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. hdu5491 The Next 模拟

    Let LL denote the number of 1s in integer DD’s binary representation. Given two integers S1S1 and S2 ...

随机推荐

  1. 通过写n本书的积累,我似乎找到了写好技术文章的方法(回复送我写的python股票电子书)

    我写的书不算少,写的博文就更多了,但大多数书的销量也就一般,而我写的技术文章里,虽然也有点击过万的,但不少点击量也就只有三位数. 通过不断反思,也通过对比了一些畅销书和顶流文章,我似乎找到了一些原因, ...

  2. tensorflow安装使用过程错误及解决方法

    tensorflow2.x 使用过程中常见错误(持续更新) 安装配置,使用tensorflow训练模型,转换为tflite模型,并部署与移动端过程中,虽然不难,但是也常出现一些莫名其妙的问题,下面简单 ...

  3. If you see someone without smile

    If you see someone without smile, give them one of yours. 难怪我每次和不认识的人说话都放肆大笑.

  4. sa-token 之权限验证

    权限验证 核心思想 所谓权限验证,验证的核心就是当前账号是否拥有一个权限码 有:就让你通过.没有:那么禁止访问 再往底了说,就是每个账号都会拥有一个权限码集合,我来验证这个集合中是否包括我需要检测的那 ...

  5. 单片机—Arduino UNO-R3—学习笔记002

    led控制 本篇主要介绍Arduino数字引脚及相关函数,通过数字I/O输出控制板载LED灯亮灭状态(数字引脚13). 数字信号是以0.1表示的电平不连续变化的信号,也就是以二进制的形式表示的信号. ...

  6. 5.2 Spring5源码--Spring AOP源码分析二

    目标: 1. 什么是AOP, 什么是AspectJ 2. 什么是Spring AOP 3. Spring AOP注解版实现原理 4. Spring AOP切面原理解析 一. 认识AOP及其使用 详见博 ...

  7. service自动发现,yaml文件管理内外部端口访问

    service服务发现 [root@k8s-master ~]# vim busybox-5d4f595646-dzjv4.yaml apiVersion: v1 kind: Pod metadata ...

  8. scrapy框架的中间件

    中间件的使用 作用:拦截所有的请求和响应 拦截请求:process_request拦截正常的请求,process_exception拦截异常的请求 篡改请求的头信息 def process_reque ...

  9. 小白也能看懂的ACID与隔离级别

    前言 现如今JAVA开发工程师的数量越来越多,但大多数工程师平时做的工作都是简单的CRUD,当你一直处于这种舒适的环境中不追求进步的时候,如果哪一天你突然想要改变环境,换个工作,去与面试官当面聊技术的 ...

  10. 记一次Nginx反向代理500的排查记录

    今天公司项目遇到一个奇怪的问题,记录一下. 注: 数据已经过脱敏处理,未暴露公司具体的IP等数据. TLDR; 项目简单介绍 用 Vue + ElementUI 实现的后台项目(以下简称:a-proj ...