Here is a range of non-negative integers. Calculate the maximum number of 1 and the minimum number in the binary.
Problem-solving ideas: messing around, finding the first unmatched digits of two numbers, and assigning all the following bits to 1 (if the right endpoint itself is 1, it starts from this ),
Solution code:
1 // Author: darkdream 2 // Created Time: thursday, November 06, 2014, 10 seconds, 3 4 # include <vector> 5 # include <list> 6 # include <map> 7 # include <set> 8 # include <deque> 9 # include <stack> 10 # include <bitset> 11 # include <algorithm> 12 # include <functional> 13 # include <numeric> 14 # include <utility> 15 # include <sstream> 16 # include <iostream> 17 # include <iomanip> 18 # include <cstdio> 19 # include <cmath> 20 # include <cstdlib> 21 # in Clude <cstring> 22 # include <ctime> 23 # define LL long long24 25 using namespace std; 26 int main () {27 int n; 28 LL l, r; 29 scanf ("% d", & n); 30 for (int I = 1; I <= n; I ++) 31 {32 LL ans = 0; 33 scanf ("% I64d % I64d", & l, & r); 34 for (int I = 62; I> = 0; I --) 35 {36 if (r & (1ll <I ))! = 0) 37 {38 if (l & (1ll <I ))! = 0) 39 {40 ans + = (1ll <I); 41 r & = (~ (1ll <I); 42 l & = (~ (1ll <I); 43} else {44 if (1ll <(I + 1)-1 <= r) 45 {46 ans + = (1ll <(I + 1)-1; 47} 48 else ans + = (1ll <I)-1; 49 break; 50} 51} 52 53} 54 printf ("% I64d \ n", ans); 55} 56 return 0; 57}
View Code
Codeforces 484 (#276 Div 1) A Bits messing around