Nyoj 744 ant financial problems (1)

Source: Internet
Author: User
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.