HDU 5491 The Next
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≤).
Each test case consists of three integers D, S1, and S2, as described above. It is guaranteed that ≤D< 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 . Y is the next WYH number. Sample Input Sample Output Case #:
Case #:
Case #: Source ACM/ICPC Asia Regional Hefei Online
这道题在比赛的时候我就是用的这个方法,但是一直超时。后来重新写了一遍,用了G++过了。
思路:
从小到大找必定超,我看限制条件一的个数是0~32差不多,这么个循环是不会超时的。我就从i->s1~s2循环,里面找一的个数为i的。
这里因为要i个1,然后又要比L大那么把第i个1前移一位。
下面这里分两个情况:
以需要3个1为例
①11011->11100
②10111->11001如果进位了,就在末尾补1.
还有一种情况就是找不到第i个1:就从后往前变0为1,直到满足i个1.
然后去每种1个数的最小值。
证明正确性:
试想要i个1那么不够的话还要比他大是不是往后补就行,因为1放在越后面越小。
要是够,因为要比他大,这里可知1的个数比需要的多。我们就要删掉1,那么删哪些呢?到第i个1为止,你后面在怎么变①后面1变少,数值变小,不行②1变多,我们现在要删掉1,不行;
所以后面的都不行,那么第i个1要进位了。然后进位完,1不够了,最小就往最后面补。
#include<stdio.h>
int main()
{
int T,s1,s2;
__int64 L,p;
int cas=; scanf("%d",&T);
while(T--)
{
scanf("%I64d%d%d",&L,&s1,&s2);
p=(__int64)<<; if(L==&&s1==&&s2==){
printf("Case #%d: 0\n",++cas);
continue;
}
int flag=;
__int64 t;
for(int i=s1;i<=s2;i++)
{
int s=,f=;
t=(__int64);
for(int j=;j>=;j--)
{
if(L&((__int64)<<j))s++;
if(s==i)
{
int z,k;
for(k=j;k<=;k++)
{if(L&((__int64)<<k));
else {
t|=(__int64)<<k;
break;
}
} z=;
for(int kk=k+;kk<=;kk++)
{
if(L&((__int64)<<kk))t|=(__int64)<<kk,z++;
}
z=i-z;
for(k=;k<=&&z>;k++)t|=(__int64)<<k,z--;
p=p>t?t:p; f=;
break;
}
}
if(!f)
{
s=i-s;
t=L;
for(int k=;k<=&&s>;k++){
if(L&((__int64)<<k));
else t|=(__int64)<<k,s--;
}
p=p>t?t:p; } }
printf("Case #%d: %I64d\n",++cas,p);
}
}
HDU 5491 The Next的更多相关文章
- hdu 5491 The Next (位运算)
http://acm.hdu.edu.cn/showproblem.php?pid=5491 题目大意:给定一个数D,它的二进制数中1的个数为L,求比D大的数的最小值x且x的二进制数中1的个数num满 ...
- 【贪心】【模拟】HDU 5491 The Next (2015 ACM/ICPC Asia Regional Hefei Online)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5491 题目大意: 一个数D(0<=D<231),求比D大的第一个满足:二进制下1个个数在 ...
- hdu 5491(2015合肥网赛)The Next
题目;http://acm.hdu.edu.cn/showproblem.php?pid=5491 题意就是,T组测试数据.然后L,S1,S2.L的二进制中有x个1,x满足 S1<=x< ...
- HDU 5491 The Next(位运算)
题意:已知D(0<=D<2^31).s1.s2,其中L为D转化为二进制数时1的个数,题目保证s1<=L<=s2,求一个数,满足以下条件: 1.比D大 2.转化为二进制时1的个数 ...
- hdu 5491 The Next(暴力枚举)
Problem Description Let L denote the number of 1s in integer D’s binary representation. Given two in ...
- hdu 5491(位运算)
题意:给你n,a,b. 希望得到比n大,二进制1的个数在 a ,b之间的最小的数 思路:①满足条件,输出 ②num < a 从右找到是0的最小位,变成1 ③num > b从右到左找是1的最 ...
- HDU - 5491 The Next 2015 ACM/ICPC Asia Regional Hefei Online
从D+1开始,对于一个数x,区间[x,x+lowbit(x))内的数字的二进制位上1的数量整体来说是单调不减的,因此可快速得出1在这个区间的取值范围. 每次判断一下有没有和[s1,s2]有没有交集,一 ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
随机推荐
- 为 Joomla 而生的 Kunena 论坛安装手册
查看并下载最新版本Kunena论坛,或直接下载Kunena1.7.2.如果你打算在Joomla网站中安装一个论坛,可以考虑一下Kunena论坛组件,一个为Joomla而生的免费的开源论坛组件. 安装: ...
- [转] rtp h264注意点(FU-A分包方式说明)
总括: 一帧视频数据可以编码成多个H264的NALU, 每个NALU的开头为00 00 00 01: 一个RTP包可以传送 部分.一个或多个 NALU,看NALU的大小而定. 之前写过一篇文章,分析了 ...
- Yii之权限管理扩展 srbac
最近在研究 Yii 的权限控制功能,尽管Yii 自身提供了一个简单的权限管理,但是很多时候,我们还是需要对其做一点扩展. 在这里,我向大家推荐一个不错的扩展:SRBAC. 在Yii的官方网站的exte ...
- 【杭州(含嘉兴,绍兴,金华,湖州,义乌)】Uber优步司机奖励政策(2月1日~2月7日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 【ImageMagick】ImageMagick命令行工具
[关于ImageMagick] [命令行工具] [源码安装] [二进位发布版本] [资源配置文件] [相关下载] ImageMagick命令行工具 [ convert | identify | mog ...
- java的主函数中各个词的作用
主函数 public static void main(String[] args){} public: main主方法是由jvm(虚拟机)来调用,jvm实际也是一程序,为了保证jvm能在任何情况下调 ...
- 浙大PTA - - File Transfer
题目链接:https://pta.patest.cn/pta/test/1342/exam/4/question/21732 #include "iostream" #includ ...
- array_column php 函数
今天想从二维数组中取某个列成一维数组 结果发现 array_column需要php5.5支持才行 然后自己写了一个仿造版本的array_column 两种思路 1.直接遍历 取值给追加数组 返回 2. ...
- CSS 列表 你知道吗
CSS 列表属性允许你放置.改变列表项标志,或者将图像作为列表项标志.CSS 列表从某种意义上讲,不是描述性的文本的任何内容都可以认为是列表.人口普查.太阳系.家谱.参观菜单,甚至你的所有朋友都可以表 ...
- Robotium---环境搭建及入门示例
Robotium是一款基于控件的Android自动化测试框架 环境搭建(window): 安装JDK以及集成Android Sdk的eclipise. 入门: 1,下载Robotium Solo 5. ...