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. Flutter 布局类组件:层叠布局(Stack和Positioned)

    前言 层叠布局,即子组件可以根据距父容器四个角的位置来确定自身的位置.绝对定位运行子组件堆叠起来,即按照代码中声明的顺序. Flutter中使用Stack和Positioned这两个组件来配合实现绝对 ...

  2. SpringBoot整合Shiro完成认证

    三.SpringBoot整合Shiro思路 首先从客户端发来的所有请求都经过Shiro过滤器,如果用户没有认证的都打回去进行认证,认证成功的,再判断是否具有访问某类资源(公有资源,私有资源)的权限,如 ...

  3. 如何实现微信小程序动画?添加到我的小程序动画实现详细讲解,轻松学会动画开发!附壁纸小程序源码下载链接

    为了让用户能尽可能多地使用小程序,也算是沉淀用户,现在很多小程序中,都有引导用户"添加到我的小程序"的操作提示,而且大多都是有动画效果.在高清壁纸推荐小程序首页,用户每次进入,都会 ...

  4. me21n增强BADI:ME_PROCESS_PO_CUST之process_account

    当实施ME_PROCESS_PO_CUST这个badi来增强ME21N的时候,用了到方法process_account,既对ME21N的行项目的科目分配做增强.主要用到如下类: IF_PURCHASE ...

  5. 18V转5V,18V转3.3V,18V转3V稳压芯片,0.01A-3A输出

    18V转5V,18V转3.3V,18V转3V, 18V转5V稳压芯片,18V转3.3V稳压芯片,18V转3V稳压芯片, 18V常降压转成5V电压,3.3V电压和3V电压给其他芯片或设备供电,适用于这个 ...

  6. ModelForm的基本用法:

    一.ModelForm的基本用法示例: from django import forms from app01 import models class BookModelForm(forms.Mode ...

  7. 自翻------Office 2013 RT 使用说明

    Office Home and Student 2013 RT Preview的更新 介绍 Microsoft已发布Microsoft Office Home and Student 2013 RT ...

  8. Qt QMenuBar和QMenu以及QAction巧妙的使用方法

    这里简单介绍QMenuBar和QMenu以及QAction是什么,其详细功能本文不做介绍,如果还不了解的朋友可以查阅Qt的帮助手册或浏览其它相关博客.如下图,软件中蓝色条框是QMenuBar用来承载Q ...

  9. Git:.gitignore和.gitkeep文件的使用 让空文件夹被跟踪

    Git:.gitignore和.gitkeep文件的使用 Git:.gitignore和.gitkeep文件的使用 https://majing.io/posts/10000001781172 .gi ...

  10. functools.singledispatchmethod(Python 3.8) | 码农网 https://www.codercto.com/a/83245.html

    functools.singledispatchmethod(Python 3.8) | 码农网 https://www.codercto.com/a/83245.html