codeforces C. Bits(数学题+或运算)

题意:给定一个区间,求区间中的一个数,这个数表示成二进制的时候,数字1的个数最多!
如果有多个这样的数字,输出最小的那个!

思路:对左区间的这个数lx的二进制 从右往左将0变成1,直到lx的值大于右区间的值rx!

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>

using namespace std;

int main(){
    long long a, b;
    int n;
    cin>>n;
    while(n--){
        cin>>a>>b;
        for(long long i=1; (a|i) <= b; i<<=1)
            a |= i;
        cout<<a<<endl;
    }
    return 0;
}
时间: 2024-08-20 06:53:44

codeforces C. Bits(数学题+或运算)的相关文章

【LeetCode从零单排】No 191.Number of 1 Bits(考察位运算)

题目 Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight). For example, the 32-bit integer '11' has binary representation 00000000000000000000000000001011, so the function should r

[LeetCode]--190. Reverse Bits(不是很懂的位运算)

补充知识,Java的位运算(bitwise operators) Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000

CodeForces 233B Non-square Equation

链接: http://codeforces.com/problemset/problem/233/B 题目: B. Non-square Equation time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Let's consider equation: x2+s(x)·x-n=0, where x,n are positive

UVa 10718 Bit Mask:贪心&amp;amp;位运算

10718 - Bit Mask Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1659 In bit-wise expression, mask is a common term. You can get a certain bit-pattern using mask. For exa

Codeforces Round #100 / 140A:简单几何

http://codeforces.com/contest/140/problem/A 过大圆圆心作小圆切线即可发现规律,详见代码. 注意判相等一定要用fabs!!! 完整代码: /*30ms,0KB*/ #include<bits/stdc++.h> using namespace std; int main() { int n, R, r; double a; scanf("%d%d%d", &n, &R, &r); if (r > R |

编程-leetcode上的一题,Reverse Bits?

问题描述 leetcode上的一题,Reverse Bits? 1C 题目如下:Reverse bits of a given 32 bits unsigned integer.For example given input 43261596 (represented in binary as 00000010100101000001111010011100) return 964176192 (represented in binary as00111001011110000010100101

PostgreSQL Oracle 兼容性之 - BIT_TO_NUM , BITAND , 比特运算 , 比特与整型互相转换

背景 比特类型转换为整型,整型转换为比特类型,以及整型的比特运算. 在数据分析时被经常使用,例如对多个用0和1表示的标签字段叠加,使用一个整型表示. 又或者将数字表述的标签信息转换为比特位,以获取分散的标签信息. 在Oracle中可以使用bit_to_num将多个0,1转换为number,使用bitand对两个number进行比特与运算得到另一个number. bit_to_num(exp1, exp2, ...., expn) BIN_TO_NUM converts a bit vector

[LeetCode] Number of 1 Bits &amp;amp; Reverse Integer - 整数问题系列

目录:1.Number of 1 Bits  - 计算二进制1的个数 [与运算] 2.Contains Duplicate - 是否存在重复数字 [遍历]3.Reverse Integer - 翻转整数 [int边界问题]4.Excel Sheet Column Number - Excel字符串转整数 [简单]5.Power of Two & Happy Number - 计算各个位数字 [%10 /10] 一.Number of 1 Bits  题目概述:Write a function t

[LeetCode] Counting Bits

Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Example: For num = 5 you should return [0,1,1,2,1,2]. Follow up: It is very