Csu_1410: Integer Conversion

Source: Internet
Author: User
Description we can add 1 to an integer a or multiply it by 2 to convert it to another integer B. Given two integers x and y, it takes at least a few steps to convert X to Y .. The first line of input contains an integer T (1 ≤ T ≤ 500), indicating a total of T groups of test data. Each group of test data occupies one row, which contains two integers x, y (1 ≤ x ≤ y ≤ 1018 ). For each group of test data, the output must take at least several steps to convert X to Y. Sample Input
31 13 102 11
Sample output
034
Hint

For example 2, 3 is converted to-> 4-> 5-> 10 in three steps.
For example 3, you can convert 2 to> 4-> 5-> 10-> 11 in four steps.

Source

The Eighth Program Design Competition for Central South University Students

// Inverse consideration from Y to X. Use long # include <cstdio> long a, B; void solve () {long ans = 0; while (A <B) {If (A> B/2) {ans + = B-a, B =; // A> B/2 so that a becomes B, it can only be one by one plus 1} else {If (B % 2) ans + = 2, B/= 2; // if the remainder of B % 2 is not equal to 0, a must first add 1, multiply by 2 else ans ++, B/= 2 ;}} printf ("% LLD \ n", ANS) ;}int main () {int T, I; scanf ("% d", & T); While (t --) {scanf ("% LLD", & A, & B); solve ();} return 0 ;}


 

Csu_1410: Integer Conversion

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.