CF 295div2 B dp (memory-based search), 295div2dp

Source: Internet
Author: User

CF 295div2 B dp (memory-based search), 295div2dp

Http://codeforces.com/contest/520/problem/ B

B. Two Buttonstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

Vasya has found a strange device. on the front panel of a device there are: a red button, a blue button and a display showing some positive integer. after clicking the red button, device multiplies the displayed number by two. after clicking the blue button, device subtracts one from the number on the display. if at some point the number stops being positive, the device breaks down. the display can show arbitrarily large numbers. initially, the display shows numberN.

Bob wants to get numberMOn the display. What minimum number of clicks he has to make in order to achieve this result?

Input

The first and the only line of the input contains two distinct integersNAndM(1 digit ≤ DigitN, Bytes,MLimit ≤ limit 104), separated by a space.

Output

Print a single number-the minimum number of times one needs to push the button required to get the numberMOut of numberN.

Sample test (s) input
4 6
Output
2
Input
10 1
Output
9
Note

In the first example you need to push the blue button once, and then push the red button once.

In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.


/** CF 295div2 B dp (memory-based search) Question: Given a given n, each operation can be reduced by 1, or multiplied by 2, and you can ask at least a few operations to solve m: if n> = m, the answer is n-m; otherwise dp [x] = min (dp [x-1], dp [2 * x]), use the Memory search to write */# include <stdio. h> # include <iostream> # include <algorithm> # include <string. h> using namespace std; int n, m, dp [20005], vis [20005]; int dfs (int x) {if (x <= 0) return 9999999; if (x> = m) {dp [x] = x-m; // printf ("% d, % d \ n", x, dp [x]); return dp [x];} if (vis [x]) {// printf ("% d, % d \ n", x, dp [x]); return Dp [x];} vis [x] = 1; dp [x] = min (dfs (x-1), dfs (2 * x) + 1; /// printf ("% d, % d \ n", x, dp [x]); return dp [x];} int main () {while (~ Scanf ("% d", & n, & m) {memset (dp, 0x3f3f3f, sizeof (dp); memset (vis, 0, sizeof (vis); printf ("% d \ n", dfs (n);} return 0 ;}


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.