hdu5491 The Next
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 740 Accepted Submission(s): 294
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.
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.
the test case number starting from 1. Y is
the next WYH number.
11 2 4
22 3 3
15 2 5
Case #2: 25
Case #3: 17
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的更多相关文章
- hdu-5491 The Next(贪心)
题目链接: The Next Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu5491 The Next 模拟
Let LL denote the number of 1s in integer DD’s binary representation. Given two integers S1S1 and S2 ...
随机推荐
- GC算法介绍及工作原理和优缺点
一.GC定义与作用 GC就是垃圾回收机制的简写 GC可以找到内存中的垃圾,并释放和回收空间,GC里的垃圾是什么 如下图所示: GC算法是什么:GC是一种机制,垃圾回收器完成具体的工作 工作的内容就是查 ...
- 关于SET/GET PARAMETER ID的注意事项
通常这两个语法配合 PARAMETER, select-options中的参数 memory id来使用. 如,选择屏幕定义 PARAMETER p1 TYPE c LENGTH 10 MEMORY ...
- 24v转3.3v稳压芯片,高效率DC-DC变换器3A输出电流
PW6206系列是一个高精度,高输入电压低静态电流,高速,低功耗降线性稳压器具有高纹波抑制.输入电压高达40V,负载电流为在VOUT=5V和VIN=7V时高达300mA.该设备采用BCD工艺制造.PW ...
- Kubernetes集群管理工具kubectl命令技巧大全
一. kubectl概述 Kubectl是用于控制Kubernetes集群的命令行工具,通过kubectl能够对集群本身进行管理,并能够在集群上进行容器化应用的安装部署. kubectl命令的语法如下 ...
- 制作 Ubuntu 16.04 离线apt源
1.下载离线安装包 ubuntu下安装包都会下载到/var/cache/apt/archives下,首先清空该目录 sudo apt-get clean 下载需要安装包 sudo apt-get in ...
- Samba共享工具安装
Samba 是一种在局域网上共享文件的一种通信协议,它为局域网内的不同计算机之间提供文件的共享服务. (1)下载并安装 Samba 工具. 确定 Ubuntu 已连接到互联网, 执行如下命令下载 Sa ...
- Spring Security,没有看起来那么复杂(附源码)
权限管理是每个项目必备的功能,只是各自要求的复杂程度不同,简单的项目可能一个 Filter 或 Interceptor 就解决了,复杂一点的就可能会引入安全框架,如 Shiro, Spring Sec ...
- Vue基础之Vue的模板语法
Vue基础之Vue的模板语法 数据绑定 01 数据绑定最常见的形式就是使用插值表达式(两个大括号!)[也就是小胡子语法!mustache] <body> <!-- Vue.js的应用 ...
- 设置一个两边固定中间自适应的css
1.两边浮动,中间自动宽度 给左右两个盒子设置左右浮动,中间的盒子不设置宽度,左右两边边距为左右盒子的宽度,中间盒子的位置必须写在右盒子下面,不然会把右盒子挤下去 如: <div class ...
- udp 连接
在今天的内容里,我对 UDP 套接字调用 connect 方法进行了深入的分析.之所以对 UDP 使用 connect,绑定本地地址和端口,是为了让我们的程序可以快速获取异步错误信息的通知,同时也可以 ...