HDU5491 The Next
来源:互联网 发布:hec ras 软件下载 编辑:程序博客网 时间:2024/05/18 16:55
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5491
题意:一个数对应的二进制的1的个数在[ s1 , s2 ]内,则称为WHY number。
给定一个数字d为WHY number,求比d大的最小的WHY number(即下一个)
思路:先计算出d的二进制中含有1的个数,如果小于上限d2且d是偶数,则答案为d+1。
否则加上lowbit(d),由于加上后1的个数只减不增,故不会出现num1>d2的情况。
若此时符合条件,即为答案。否则补上不足的1(s1-num1),即2^(s1-num1)-1
code:
#include<cstdio>using namespace std;typedef long long ll;ll lowbit(ll x){ return x&-x;}ll num1(ll x){ ll res=0; while(x) { if(x&1) res++; x/=2; } return res;}int main(){ int T,cas=1; scanf("%d",&T); while(T--) { ll x,s1,s2; scanf("%lld%lld%lld",&x,&s1,&s2); if((x&1)==0&&s2>num1(x)) x++; else { x+=lowbit(x); ll t=num1(x); if(t<s1) x+=(1<<(s1-t))-1; } printf("Case #%d: %lld\n",cas++,x); } return 0;}
0 0
- hdu5491 The Next
- HDU5491 The Next【二进制】
- HDU5491 The Next
- HDU5491-The Next
- HDU5491 The Next 位模拟
- hdu5491 The Next(合肥网赛)
- HDU5491 The Next 字符串+位模拟+思维
- 2015 ACM/ICPC合肥网赛&HDU5491 The Next
- hdu5491
- Testing, the Next Generation
- The next step
- The next revolution -- 转帖
- the next station?
- The next :jsp
- the next:项目
- The Next:预答辩
- The next permutation
- The Next Permutation
- 移动联通基站定位API以及电信基站定位API
- 17.5 添加额外的文件
- 移动设备开发的那些meta
- 笔记 style.xml风格记录
- 多个pdf怎么转换成word文档最简单
- HDU5491 The Next
- js样式
- lintcode longest-increasing-continuous-subsequence 最长上升连续子序列
- 新博客
- jQuery中获取点击事件的元素及元素的id属性
- UnderstandingDSOLinkChange
- 让你的APP轻松加上扫描二维码功能
- PyGobject(十二)布局容器之Button篇——Gtk.MenuButton
- sql 执行顺序