Ant's problem (I) time limit: 1000 MS | memory limit: 65535 kb difficulty: 2
-
Description
-
Little ant shoes recently fell in love with bit operations, and he felt that bit operations were amazing. However, he encountered a problem recently:
Given a range [a, B], we can look for two numbers x and y in the range to make X exclusive or y the largest. Come on, help him!
-
Input
-
There are multiple groups of test data (ended with EOF ).
Enter two numbers A and B for each group of data (0 <= A <B <2 ^ 63)
-
Output
-
Outputs An exclusive or maximum value between A and B.
-
Sample Input
-
1 28 9
-
Sample output
-
31
-
Ideas:
Multiple Data tests show that each output result is 2 ^ n-1, and the relationship between N and A and B is required. This topic guides us to use bitwise operations,
Take the number of two groups, for example, 4 or 8. The calculated range is 7 ^ 8 = 15 = (1111) 2 = 2 ^ 4-1;
Search for a group of 2, 6 at will, and calculate the range of 2 ^ 5 = 7 = (111) 2 = 2 ^ 3-1;
-
Through the above analysis, we can know that the maximum value is 2 ^ n-1, and N is the binary number of The number of right boundary.
-
Therefore, you can think of the following code:
-
# Include <stdio. h> int main () {long a, B, X; // use the long type while (~ Scanf ("% LLD", & A, & B) {x = a ^ B; // find the number of digits int CNT = 0; while (x) ++ CNT, x> = 1; // calculate the number of digits of printf ("% LLD \ n", (1ll) <CNT)-1 ); // obtain 2 ^ CNT-1;} return 0 ;}
-
Nyoj 744 ant financial problems (1)