位运算的一些案例
2014-11-13 14:41
381 查看
C.Bits
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Let's denote as
the
number of bits set ('1' bits) in the binary representation of the non-negative integer x.
You are given multiple queries consisting of pairs of integers l and r.
For each query, find the x, such that l ≤ x ≤ r,
and
is
maximum possible. If there are multiple such numbers find the smallest of them.
Input
The first line contains integer n — the number of queries (1 ≤ n ≤ 10000).
Each of the following n lines contain two integers li, ri —
the arguments for the corresponding query (0 ≤ li ≤ ri ≤ 1018).
Output
For each query print the answer in a separate line.
Sample test(s)
input
output
题目大意是找出二进制的l到r之间,含有1最多的数
由于n可能取到10000,且l和r都会取到10的18次方,所以不能一个一个的搜索
而且还要注重效率,这个时候用位运算就再巧妙不过了
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Let's denote as
the
number of bits set ('1' bits) in the binary representation of the non-negative integer x.
You are given multiple queries consisting of pairs of integers l and r.
For each query, find the x, such that l ≤ x ≤ r,
and
is
maximum possible. If there are multiple such numbers find the smallest of them.
Input
The first line contains integer n — the number of queries (1 ≤ n ≤ 10000).
Each of the following n lines contain two integers li, ri —
the arguments for the corresponding query (0 ≤ li ≤ ri ≤ 1018).
Output
For each query print the answer in a separate line.
Sample test(s)
input
3 1 2 2 4 1 10
output
1 3 7
题目大意是找出二进制的l到r之间,含有1最多的数
由于n可能取到10000,且l和r都会取到10的18次方,所以不能一个一个的搜索
而且还要注重效率,这个时候用位运算就再巧妙不过了
#include<cstdio> #include<cmath> #include<cstring> #include<cstdlib> #include<vector> #include<iostream> #include<algorithm> #include<windows.h> using namespace std; #define T cout<<"***"<<endl; #define set0(arr) memset(arr,0,sizeof(arr)); #define fr(i,r) for(i=0;i<r;i++) #define fsr(i,s,r) for(i=s;i<r;i++) #define ll __int64 #define pf printf #define sf scanf int main() { int n,i,k; cin>>n; fr(k,n){ ll l,r; sf("%I64d%I64d",&l,&r); ll x=l; fr(i,64){ if((x|(ll)1<<i)<=r) x|=((ll)1<<i); else{ pf("%I64d\n",x); break; } } } }/**/
相关文章推荐
- C语言浮点数运算,讲述原理并总结一些案例
- 《死亡晚餐派对·真实医学探案故事集》:一些相对罕见的病原体、化学物品、药品致病的案例和这些案例得到明确诊断、找到具体原因的过程。三星推荐
- 【一些事晚报】经典案例:如何把胸罩卖给男人?
- 关于Hive中的复杂数据类型Array,Map,Structs的一些使用案例
- 用C++实现简单地多项式运算。(因为是菜鸟,会有一些漏洞)
- linux运算统计案例
- 点石互动--robin之操作一个英文SEO案例的一些体会
- 对java基础数据类型在运算过程中的自动转换的一些分析
- 2014年3月2日 关于Matlab 运算过程中的一些错误问题
- Response及一些案例
- 一些学习到的矩阵运算的模板(T_T)
- Math的一些常用的数学运算(包括取整、保留几位小数等)
- 图像处理 估计退化函数之运动模糊和矩阵matlab运算的一些实验情况
- ASP.NET Eval 求值运算的一些用法
- js一些案例的使用
- 一些运算
- python[三]:python入门及一些案例推荐
- 浏览器在css样式 的一些小差别案例1(菜鸟版 简单易懂)
- 字节流和一些字节流案例
- GUI_用户登录注册加入四则运算案例