HDU - 5491 The Next 2015 ACM/ICPC Asia Regional Hefei Online
2015-09-27 18:12
429 查看
从D+1开始,对于一个数x,区间[x,x+lowbit(x))内的数字的二进制位上1的数量整体来说是单调不减的,因此可快速得出1在这个区间的取值范围。
每次判断一下有没有和[s1,s2]有没有交集,一旦发现解就贪心最小的一个。
复杂度是O(T*log(ans-D))
每次判断一下有没有和[s1,s2]有没有交集,一旦发现解就贪心最小的一个。
复杂度是O(T*log(ans-D))
#include<bits/stdc++.h> using namespace std; inline int read() { char c; while(c=getchar(),c<'0'||c>'9'); int re = c-'0'; while(c=getchar(),c>='0'&&c<='9') re = re*10+c-'0'; return re; } #define lowbit(x) (x&-x) int bc(long long x) { int re = 0; while(x){ re += x&1; x>>=1; } return re; } //bitset<64> bs; #define cer(x) bs = x; cout<<bs<<endl; //#define LOCAL int main() { #ifdef LOCAL freopen("in.txt","r",stdin); #endif int T = read(); for(int ks = 1; ks <= T; ks++){ int D = read(), s1 = read(), s2 = read(); long long cur = D+1LL,lb = lowbit(cur); int ct = bc(cur); while(ct < s1 || ct > s2){ int ex = bc(lb-1); if(s1 > ct+ex || s2 < ct) { cur += lb; lb = lowbit(cur); ct = bc(cur); }else { int ad = max(ct,s1)-ct; cur += (1<<ad)-1; break; } } printf("Case #%d: %I64d\n",ks,cur); } return 0; }
相关文章推荐
- JavaScript高级程序设计之面向对象的程序设计之继承之原型链 第6.3.1讲笔记
- HDU 5486 Difference of Clustering 图论
- JSP的基础语法
- HTML5新增的元素和废除的元素
- HTML初步学习2
- 深入javascript-继承机制
- Javascript--函数技巧
- jQuery 关于时间的相互转化
- jQuery 匹配正整数
- CSS命名规范
- [LeetCode][JavaScript]House Robber II
- 前端工程师所不知道的<meta>标签用法
- css3 新增属性
- HTML5的语法的改变
- 浏览器兼容、CSS3 选择器
- [LeetCode][JavaScript]House Robber
- JavaScript中的值类型和引用类型
- string 和strngbuffer间的相互转换
- 为前端转身——HTML
- JavaScript高级程序设计之面向对象的程序设计之创建对象之稳妥构造函数模式 第6.2.7讲笔记