Codeforces Round #276 (Div. 2)
C. Bitstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output
Let's denote as the number of bits set ("1 'bits) in the binary representation of the non-negative integerX.
You are given multiple queries consisting of pairs of integersLAndR. For each query, findX, Such thatL? ¡U?X? ¡U?R, And is maximum possible. If there are multiple such numbers find the smallest of them.
Input
The first line contains integerNPlease specify the number of queries (1? ¡U?N? ¡U? 10000 ). <symbol · blank "http://www.bkjia.com/kf/ware/vc/" target = "_ blank" class = "keylink"> vcD4KPHA + export + bjwvZW0 + export + aTwvZW0 + LD88ZW0 + cjwvZW0 + export/odw/export/odw /PGVtPnI8L2VtPjxlbT5pPC9lbT4/odw/MTAxOCkuPC9wPgoKCgpPdXRwdXQKPHA + fill = "brush: java; "> 31 22 41 10Output
137
Note
The binary representations of numbers from 1 to 10 are listed below:
110? =? 12
210? =? 102
310? =? 112
410? =? 1002
510? =? 1012
610? =? 1102
710? =? 1112
810? =? 10002
910? =? 10012
1010? =? 10102
~Áá Ò % *************************************************************** £°
#include
#include #include
#include
using namespace std;__int64 bit[65];int main() { bit[0]=1; for(int i = 1; i <= 63; i++) { bit[i] = 2*bit[i-1]; } int n; scanf("%d",&n); while(n--) { __int64 l,r; scanf("%I64d%I64d",&l,&r); int j; __int64 sum = 0; for(j = 0; sum <=r; j++) { sum += bit[j]; } j--; while(sum > r) { sum -= bit[j]; if(sum < l) sum += bit[j]; j--; } printf("%I64d\n",sum); } return 0;}