description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, m = list(map(int, input().split())) cnt = 0 l = n while l < m: cnt += 1 l *= 2 dif = l - m val = 2**cnt while dif > 0: cnt += dif // val dif %= val val //= 2 print(cnt)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER VAR WHILE VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
N, M = map(int, input().split()) queue = [N] d = [(-1) for i in range(100010)] d[N] = 0 while queue: p = queue.pop(0) if p < 0: continue if d[p - 1] == -1: if p - 1 == M: print(d[p] + 1) exit() d[p - 1] = d[p] + 1 queue.append(p - 1) if p * 2 > max(N, M) * 2: continue if d[p * 2] == -1: if p * 2 == M: print(d[p] + 1) exit() d[p * 2] = d[p] + 1 queue.append(p * 2)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
def eh_par(x): return numero_m % 2 == 0 numero_n, numero_m = map(int, input().split()) aux = 0 while numero_m > numero_n: if eh_par(numero_m): numero_m = numero_m // 2 else: numero_m += 1 aux += 1 diferenca = numero_n - numero_m min_apertos = aux + diferenca print(min_apertos)
FUNC_DEF RETURN BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
target, value = map(int, input().split()) count = 0 while True: if value == target: break if value < target: value += 1 elif value % 2 == 1: value += 1 else: value = value // 2 count += 1 print(count)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
import sys sys.setrecursionlimit(100000) def _(lista, m, counter=1, visitado={}): temporal = [] for i in lista: l = i - 1 r = i * 2 if r < 2 * m: if r not in visitado: temporal += [r] visitado[r] = 0 if l > 0: if l not in visitado: temporal += [l] visitado[l] = 0 lista = temporal if m in visitado: return counter else: return _(lista, m, counter + 1) n, m = map(int, input().split()) if m < n: print(n - m) else: print(_([n], m))
IMPORT EXPR FUNC_CALL VAR NUMBER FUNC_DEF NUMBER DICT ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP NUMBER VAR IF VAR VAR VAR LIST VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER IF VAR VAR VAR LIST VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
rin = lambda: map(int, input().split()) def solve(n, m): if m < n: return n - m counter = 0 while True: next_m = m // 2 if m % 2 == 0 else (m + 1) // 2 if next_m <= n: return min(n - next_m + 1 + m % 2 + counter, counter + (2 * n - m) + 1) else: counter += 2 if m % 2 else 1 m = m // 2 if m % 2 == 0 else (m + 1) // 2 m, n = rin() print(solve(m, n))
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
a, b = map(int, input().split()) s = 0 while b > a: if b % 2 == 0: b //= 2 else: b += 1 s += 1 print(a - b + s)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, m = [int(x) for x in input().split()] flag = True c = 0 while flag: if m < n: print(n - m + c) break elif m % 2 == 0: m //= 2 c += 1 else: m += 1 c += 1 if m == n: flag = False print(c)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, m = input().strip().split() n = int(n) m = int(m) time = 0 if n > m: time += n - m else: while m - n != 0: if divmod(m, 2)[1] == 0 and m > n: m = int(m / 2) time += 1 elif n >= m: time += n - m break else: m = int((m + 1) / 2) time += 2 print(time)
ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR WHILE BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, m = map(int, input().split()) k = max(n, m) a = [-1] * (k * 3) b = [0] * (k * 4) d = [0] * (k * 4) l = 1 b[0] = n i = 0 a[n] = 0 while i < l: cur = b[i] j = cur * 2 if j < m * 2: if a[j] == -1: b[l] = j d[l] = d[i] + 1 a[j] = d[l] l += 1 j = cur - 1 if j >= 0: if a[j] == -1: b[l] = j d[l] = d[i] + 1 a[j] = d[l] l += 1 i += 1 print(a[m])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
def bfs(d, s, e): que = [] que.append(s) dist = {s: 0} flag = 0 while que: f = que[0] que.pop(0) for i in d[f]: if i not in dist: que.append(i) dist[i] = dist[f] + 1 if i == e: flag = 1 break if flag == 1: break return dist[e] n, m = map(int, input().split()) d = {} v = [i for i in range(1, 2 * max(n, m) + 1)] for i in v: if i == 1: d[i] = [2 * i] elif 2 * i > 2 * m: d[i] = [i - 1] else: d[i] = [i - 1, 2 * i] print(bfs(d, n, m))
FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR LIST BIN_OP NUMBER VAR IF BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR VAR LIST BIN_OP VAR NUMBER ASSIGN VAR VAR LIST BIN_OP VAR NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
x = input() x = x.split(" ") y = int(x[1]) x = int(x[0]) save_set = set() second_set = set() dict_set = set() save_set.add(x) count = 0 while True: if y in save_set: print(count) break for num in save_set: if num - 1 not in dict_set: second_set.add(num - 1) dict_set.add(num - 1) if num < y and num * 2 not in dict_set: second_set.add(num * 2) dict_set.add(num * 2) count += 1 save_set = set(second_set) second_set = set()
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, aim = map(int, input().split()) step = 0 while 1: if aim < n: step += n - aim print(step) break elif aim > n: if aim % 2 == 0: aim = aim // 2 step += 1 else: aim += 1 step += 1 else: print(step) break
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
f = lambda: input() n, m = list(map(int, f().split())) dp = [99999] * 10001 dp[n] = 0 def click(): for i in range(0, n + 1): dp[i] = n - i if i * 2 <= 10000: if dp[i * 2] > dp[i] + 1: dp[i * 2] = dp[i] + 1 for i in range(n + 1, 10001): if dp[i] == 99999: d = 1 while i + d <= 10000 and dp[i + d] == 99999: d += 1 if i + d <= 10000: dp[i] = d + dp[i + d] if i * 2 <= 10000 and dp[i * 2] > dp[i] + 1: dp[i * 2] = dp[i] + 1 click() print(dp[m])
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
def bfs(): n, m = map(int, input().split()) count = 0 queue = [[n, count]] seen = [] if n >= m: return n - m else: while queue: k = queue.pop() xx = k[0] t = k[1] seen.append(xx) if xx == m: return t else: if xx * 2 == m or xx - 1 == m: return t + 1 if xx * 2 not in seen and xx * 2 < m + 11: queue = [[xx * 2, t + 1]] + queue if xx - 1 not in seen and xx - 1 > 0: queue = [[xx - 1, t + 1]] + queue return 0 print(bfs())
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST LIST VAR VAR ASSIGN VAR LIST IF VAR VAR RETURN BIN_OP VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, m = map(int, input().split()) if n < m: x = m c = 0 while x != n: if x % 2 == 1: x += 1 c += 1 if x == n: break if x > n: x = x / 2 else: x += 1 c += 1 print(c) elif n > m: print(n - m) else: print(0)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
def buttons(n, m): moves = 0 while n != m: if m % 2 != 0 or m < n: m += 1 else: m = m / 2 moves += 1 return moves n, m = map(int, input().split()) print(buttons(n, m))
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, m = map(int, input().split()) result = 0 while n < m: result += 1 + m % 2 m = (m + m % 2) / 2 result += n - m print(int(result))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
import sys n, m = map(int, input().split()) a = set([n]) i = 0 while True: i += 1 b = set() for cur in a: b1 = cur * 2 b2 = cur - 1 if b1 == m or b2 == m: print(i) sys.exit() if cur < m: b.add(b1) if b2 > 0: b.add(b2) a = b
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST VAR ASSIGN VAR NUMBER WHILE NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, m = [int(i) for i in input().strip().split(" ")] targets = list() targets.append(m) while targets[-1] > n: next_target = targets[-1] if next_target % 2 == 1: targets.append(next_target + 1) else: targets.append(next_target / 2) targets.reverse() print(n - int(targets[0]) + len(targets) - 1)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
def main(): entrada = list(map(int, input().split())) n = entrada[0] m = entrada[1] if n == m: print(0) elif n >= m: print(n - m) else: contador = 0 while m > n: if m % 2 == 0: m = m / 2 contador += 1 else: m = m + 1 contador += 1 while n != m: m = m + 1 contador += 1 print(contador) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
ori, fin = input().split(" ") ori = int(ori) fin = int(fin) myset = set() myset.add(ori) ret = -1 finished = False visited = set() while not finished and myset: ret += 1 newset = set() for num in myset: if num == fin: finished = True if num < fin and 2 * num not in visited: visited.add(2 * num) newset.add(2 * num) if num > 1 and num - 1 not in visited: visited.add(num - 1) newset.add(num - 1) myset = newset print(ret)
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, m = map(int, input().split()) a = 0 def soma(n, m, a): while n < m: if m & 1: m += 1 else: m = m // 2 a += 1 return a + (n - m) a = soma(n, m, a) if n < m: if a == 0: print(n - m) else: print(a) else: print(n - m)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF WHILE VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, m = map(int, input().split()) t = 0 if m > n: while n < m: if m % 2 == 1: m = m + 1 elif m % 2 == 0: m = m / 2 t = t + 1 if m < n: while n != m: m = m + 1 t = t + 1 print(t)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
queue = [] first, second = list(map(int, input().split(" "))) queue.append([first, 0]) pos_mem = [False] * 100005 while queue: e = queue.pop(0) if pos_mem[e[0]] or e[0] > 10000 or e[0] < 1: continue else: pos_mem[e[0]] = True if e[0] - 1 == second: print(e[1] + 1) break else: queue.append([e[0] - 1, e[1] + 1]) if e[0] * 2 == second: print(e[1] + 1) break else: queue.append([e[0] * 2, e[1] + 1])
ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR LIST VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, m = map(int, input().split()) res = 0 while n != m: res += 1 if n < m and m % 2 == 0: half = m / 2 m -= half else: m += 1 print(res)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
a, b = map(int, input().split(" ")) for i in range(15): if a * 2**i >= b: x = 2**i y = a * 2**i - b break tot = 0 while x != 1: if y % 2 == 0: y //= 2 x //= 2 tot += 1 elif y % 2 == 1: y -= 1 y //= 2 x //= 2 tot += 2 print(tot + y)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, m = [int(x) for x in input().split()] moves = 0 if n > m: print(abs(n - m)) exit() while n != m: if m % 2 == 0 and m > n: m /= 2 moves += 1 else: m += 1 moves += 1 print(moves)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, t = map(int, input().split(" ")) if t <= n: print(abs(t - n)) else: count = 0 if t % 2 != 0: count += 1 t += 1 while t > n: t = t / 2 count += 1 if t % 2 != 0 and t != n: count += 1 t += 1 count += n - t print(int(count))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, m = input().split() n = int(n) m = int(m) bfs = [] tmp = [m] bfs.append(tmp) res = -1 i = 0 while True: tmp = [] maior = 0 for j in bfs[i]: if j == n: res = i break if j > maior: maior = j if j % 2 == 0: tmp.append(j / 2) else: tmp.append(j + 1) if res != -1: break elif maior < n: res = i + n - maior break bfs.append(tmp) i += 1 print(int(res))
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
import sys input = lambda: sys.stdin.readline() int_arr = lambda: list(map(int, input().split())) str_arr = lambda: list(map(str, input().split())) get_str = lambda: map(str, input().split()) get_int = lambda: map(int, input().split()) get_flo = lambda: map(float, input().split()) mod = 1000000007 def solve(n, m): if n == m: print(0) else: if n > m: print(n - m) return c = 0 while m > n: if m % 2 != 0: m += 1 else: m //= 2 c += 1 c += n - m print(c) n, m = get_int() solve(n, m)
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, m = map(int, input().split()) a = 0 while n != m: if m > n: if m % 2 == 0: m /= 2 a += 1 else: m += 1 a += 1 elif n > m: m += 1 a += 1 print(abs(a))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, m = map(int, input().strip().split(" ")) Step = 0 while n != m: if m < n or m % 2 != 0: m += 1 elif m > n: m = int(m / 2) Step += 1 print(Step)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
def get_step(n, m): step = 0 while m > n: if m % 2 == 0: m //= 2 else: m += 1 step += 1 if m == n: return step else: return step + n - m n, m = input().split(" ") n = int(n) m = int(m) print(get_step(n, m))
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR RETURN VAR RETURN BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
q = input().split() x = int(q[0]) p = int(q[1]) count = 0 while x < p: if p % 2 == 0: p = int(p / 2) count += 1 else: p = p + 1 p = int(p / 2) count += 2 if p == x: count = count + 0 else: count = count + x - p print(count)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
def calculate(x, y): if x == y: return 0 elif x > y: return abs(y - x) elif y % 2 == 0: return 1 + calculate(x, y // 2) else: return 1 + calculate(x, y + 1) def main(): show, target = map(int, input().split()) print(calculate(show, target)) main()
FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
inp = input() chars = inp.split() n = int(chars[0]) m = int(chars[1]) ans = 0 while n < m: if m % 2 == 1: m = m + 1 else: m = m / 2 ans = ans + 1 print(int(ans + n - m))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
import sys sys.setrecursionlimit(100000000) s = input() s = s.split() n = int(s[0]) m = int(s[1]) c = 0 def solve(n, m, c): if m < n: return n - m + c if m == n: return c elif m % 2 == 0: return solve(n, int(m / 2), c + 1) else: return solve(n, m + 1, c + 1) print(solve(n, m, 0))
IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR IF VAR VAR RETURN VAR IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
def bfs(n, m): stack = [n] counter = 0 if n == m: print(0) else: while stack: counter += 1 n_i = stack.pop(0) if m in apuntadores[n_i]: print(contadores[n_i]) break else: for a in apuntadores[n_i]: if not visitados[a]: contadores[a] = contadores[n_i] + 1 stack.append(a) visitados[a] = True values = list(map(int, input().split())) n = values[0] m = values[1] apuntadores = {} contadores = {} for i in range(0, 10001): contadores.update({i: 1}) if i <= 5000: apuntadores.update({i: (i - 1, 2 * i)}) else: apuntadores.update({i: (i - 1, 0)}) visitados = [False] * apuntadores.__len__() visitados[0] = True bfs(n, m)
FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR DICT VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR DICT VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR DICT VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
def solve(n, m): if n >= m: return n - m elif m % 2 == 0: if m / 2 < n: return n - m // 2 + 1 elif m / 2 > n: return 1 + solve(n, m // 2) else: return 1 else: return 1 + solve(n, m + 1) a, b = map(int, input().split()) print(solve(a, b))
FUNC_DEF IF VAR VAR RETURN BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR RETURN BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, m = map(int, input().split()) t = 0 if n > m: print(n - m) else: while n < m: if m % 2 == 0: m = m / 2 t += 1 else: m = (m + 1) / 2 t += 2 print(int(abs(n - m) + t))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
start, end = list(map(int, input().split())) if start > end: print(start - end) else: count = 0 while start != end: if not end % 2 and end > start: end = end // 2 else: end += 1 count += 1 print(count)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
import sys range = sys.stdin.readline() range_list = range.split() range1 = int(range_list[0]) range2 = int(range_list[1]) count = 0 if range2 > range1: while range2 > range1: if range2 % 2 == 0: range2 /= 2 else: range2 += 1 count += 1 count += range1 - range2 else: count = range1 - range2 print(int(count))
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, m = [int(x) for x in input().strip().split()] if m <= n: print(n - m) else: end = m + 1 if m % 2 else m dp = [(n - i if i <= n else 0) for i in range(end + 1)] if n % 2: dp[n + 1] = dp[int((n + 1) / 2)] + 1 start = n + 3 else: start = n + 2 for i in range(start, end + 1, 2): dp[i] = dp[int(i / 2)] + 1 dp[i - 1] = dp[i] + 1 print(dp[m])
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, m = list(map(int, input().strip().split())) c = m - n for i in range(1000): if m <= n: break if m % 2 != 0: m = m + 1 continue else: m = m / 2 continue print([int(i + n - m), -c][c <= 0])
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
x, y = map(int, input().split()) q = [] q.append([x, 0]) vis = set() while q: t = q.pop(0) if t[0] == y: print(t[1]) break if t[0] * 2 == y or t[0] - 1 == y: print(t[1] + 1) break if t[0] * 2 not in vis and t[0] * 2 <= y * 4: q.append([t[0] * 2, t[1] + 1]) vis.add(t[0] * 2) if t[0] - 1 >= 0 and t[0] - 1 not in vis: q.append([t[0] - 1, t[1] + 1]) vis.add(t[0] - 1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, m = map(int, input().split()) vis = [-1] * 20000 q = [0] * 20000 tail = 1 q[0] = n vis[n] = 0 on = 0 while on < tail: cur = q[on] on += 1 if cur + cur < 20000 and vis[cur + cur] == -1: vis[cur + cur] = 1 + vis[cur] q[tail] = cur + cur tail += 1 if cur - 1 > 0 and vis[cur - 1] == -1: vis[cur - 1] = 1 + vis[cur] q[tail] = cur - 1 tail += 1 print(vis[m])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
import sys sys.setrecursionlimit(100000) n, m = map(int, input().split()) def find(stack, m, count=1, visited={}): tmp = [] for e in stack: l = e * 2 r = e - 1 if l < 2 * m: if l not in visited: tmp += [l] visited[l] = 0 if r > 0: if r not in visited: tmp += [r] visited[r] = 0 stack = tmp if m in visited: return count else: return find(stack, m, count + 1) if m < n: print(n - m) else: print(find([n], m))
IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF NUMBER DICT ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP NUMBER VAR IF VAR VAR VAR LIST VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER IF VAR VAR VAR LIST VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
source, target = [int(x) for x in input().split(" ")] rev_buf = target steps = 0 while rev_buf != source: if rev_buf < source: rev_buf += 1 elif rev_buf % 2 == 0: rev_buf /= 2 else: rev_buf += 1 steps += 1 print(steps)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
from sys import stdin n, m = map(int, stdin.readline().rstrip().split(" ")) c = 0 while m != n: c += 1 if m % 2 == 1 or m < n: m += 1 elif m > n and not m % 2: m = m // 2 print(c)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
a, b = list(map(int, input().split())) if a >= b: print(a - b) else: p = 0 while True: if b % 2 == 0: p += 1 b = b // 2 else: p += 2 b = b // 2 + 1 if a >= b: p += a - b break print(p)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
import sys n, m = map(int, sys.stdin.readline().split()) s = set() s.add(n) s_total = set() ans = 0 while s: ans += 1 s_total |= s s = {(2 * i) for i in s} | {(i - 1) for i in s if i > 1} s = s - s_total s1 = {i for i in s if i <= m} s2 = s - s1 s = s1 if s2: s.add(min(s2)) if s: if m in s: print(ans) break else: print(-1) break
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
a, b = map(int, input().split()) if b <= a: print(a - b) else: k = int(0) while b != a: if b > a and b % 2 == 0: b = b / 2 else: b = b + 1 k = k + 1 print(k)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER WHILE VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
import sys def search(n, m, res): if n == m: return res if n > m: return res + (n - m) if m % 2 == 0: return search(n, m // 2, res + 1) return search(n, m + 1, res + 1) def main(): f = sys.stdin n, m = map(int, f.readline().rstrip().split()) print(search(n, m, 0)) main()
IMPORT FUNC_DEF IF VAR VAR RETURN VAR IF VAR VAR RETURN BIN_OP VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
R = lambda: map(int, input().split()) n, m = R() if n >= m: print(n - m) exit() cnt = 0 while n != m: if n > m: m += 1 elif not m % 2: m //= 2 else: m += 1 cnt += 1 print(cnt)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
q = list(map(int, input().split())) n, m = q[0], q[1] if n > m: print(n - m) else: L = [n] q = 0 a = m while n < a: if a % 2 == 0: a = a // 2 else: a = a + 1 q += 1 print(n - a + q)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
nz, mz = input().split() n = int(nz) m = int(mz) c = 0 while m > n: if m % 2 == 0: m = m // 2 else: m += 1 c += 1 c += n - m print(c)
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, mi = map(int, input().split()) ras = 0 for i in range(100000): if n == mi: break if n > mi: ras = ras + 1 mi = mi + 1 continue elif mi % 2: mi = mi + 1 ras = ras + 1 continue elif mi % 2 == 0: mi = mi // 2 ras += 1 continue print(ras)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
arr = list(map(int, input().split())) n = arr[0] m = arr[1] if m <= n: print(n - m) else: c = 0 t = n while not (t <= m and m <= 2 * t): t *= 2 c += 1 l = [0] * (2 * t + 1) for i in range(1, n): l[i] = n - i h = n stk = [n] c = 0 while h <= t: h *= 2 stk.append(h) c += 1 l[h] = c f = len(stk) i = 0 while i != f - 1: st = stk[i] end = stk[i + 1] for j in range(end - 1, st, -1): if j % 2 == 1: l[j] = l[j + 1] + 1 else: l[j] = min(l[j // 2] + 1, l[j + 1] + 1) i += 1 print(l[m])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, m = str(input()).split() n = int(n) m = int(m) num = 0 while n != m: if n > m: num += n - m n = m else: if m % 2 == 1: m += 1 num += 1 m //= 2 num += 1 print(num)
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, m = list(map(lambda x: int(x), input().split())) if n > m: print(n - m) elif n == m: print(0) elif m // n == 2: print(1) else: count = 0 while m > n: if m % 2 == 0: m = m // 2 else: m = m + 1 count += 1 if m == n: print(count) else: count += n - m print(count)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, p = map(int, input().split()) def dp(n, p, steps=0): if n >= p: return steps + n - p steps += 1 x = p // 2 if p % 2: steps += 2 * (p // 2 + 1) - p x += 1 return dp(n, x, steps) print(dp(n, p))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF NUMBER IF VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
takeInput = input() N, M = [int(x) for x in takeInput.split(" ")] short = 0 def func(n, m): global short if n == m: return short if n > m: short = short + n - m return short short += 1 if m % 2 == 0: return func(n, m / 2) else: return func(n, m + 1) print(int(func(N, M)))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
def fun(n, m): ans = 0 ans = n * (m // n) print(n, m, "---") if m % 2 == 0: ans += n - m // 2 + 1 else: ans += n - m // 2 + 2 return ans def bfs(n, m): temp = [(n, 0)] ans = 0 visited = set() count = 0 while temp: node, count = temp.pop(0) if node in visited or node <= 0 or node > 10000: continue visited.add(node) if node == m: return count if node - 1 not in visited: temp.append((node - 1, count + 1)) if node * 2 not in visited: temp.append((node * 2, count + 1)) n, m = list(map(int, input().split())) if m < n: print(n - m) else: print(bfs(n, m))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, k = map(int, input().split()) ans = 0 while k != n: while k < n: ans += 1 k += 1 while k > n: if k % 2 == 0: k //= 2 ans += 1 else: ans += 1 k += 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
nums = [int(x) for x in input().split(" ")] n = nums[0] m = nums[1] if n > m: print(n - m) else: count = 0 while m != n: if m > n: if m % 2 == 0: m /= 2 else: m += 1 else: m += 1 count += 1 print(count)
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
def min_count(a, b): if a == b: return 0 if a > b: return a - b count = 0 while b > a: if b % 2 == 1: b += 1 count += 1 b /= 2 count += 1 count += a - b return count def main(): a, b = map(int, input().split()) print(int(min_count(a, b))) main()
FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, m = map(int, input().strip().split(" ")) if n > m: print(n - m) else: verge = {} path = [-1] * 2 * m pick = [] for i in range(1, 2 * m + 1): temp = [] if i > 1: temp.append(i - 1) if i <= m: temp.append(2 * i) verge[i] = temp path[n] = 0 pick.append(n) while path[m] == -1: cur = pick.pop(0) for j in verge[cur]: if path[j] == -1: path[j] = path[cur] + 1 pick.append(j) print(path[m])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP BIN_OP LIST NUMBER NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
import sys numbers = sys.stdin.readline().split(" ") numbers = list(map(int, numbers)) p = numbers[0] q = numbers[1] count2 = 0 count1 = 0 while q > p: if q % 2 != 0: q = (q + 1) / 2 count2 = count2 + 1 count1 = count1 + 1 elif q % 2 == 0: q = q / 2 count2 = count2 + 1 diff = p - q ans = count2 + count1 + diff print(int(ans))
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, m = map(int, input().split()) if n >= m: print(n - m) else: k = 0 while m > n: k += 1 + m % 2 m = round((m + m % 2) / 2) k += n - m print(k)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, m = map(int, input().split()) cnt = 0 while True: if n == m: print(int(cnt)) break if m < n: print(int(cnt + n - m)) break elif m % 2 == 0: m = m / 2 cnt += 1 else: m += 1 cnt += 1
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
def buttons(m, n, Steps): if m == n: return Steps elif m % 2 == 0 and m > n: return buttons(m // 2, n, Steps + 1) elif m % 2 == 1 and m > n: return buttons((m + 1) // 2, n, Steps + 2) else: return Steps + (n - m) [n, m] = input().split(" ") print(buttons(int(m), int(n), 0))
FUNC_DEF IF VAR VAR RETURN VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR BIN_OP VAR VAR ASSIGN LIST VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
def nrstDouble(n, m): i = 0 while n < m: n *= 2 i += 1 return i n, m = map(int, input().split()) ans = 0 while 1: if m <= n: ans += n - m break i = nrstDouble(n, m) val = (n * 2**i - m) // 2**i if val == 0: n *= 2 ans += 1 else: n -= val ans += val print(ans)
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, m = map(int, input().split()) if n >= m: print(n - m) else: count = 0 while m > n: if m % 2 == 0: m //= 2 count += 1 else: count += 1 m += 1 if m == n: print(count) else: c1 = count + (n - m) while m != n: if m % 2 == 0: m //= 2 count += 1 else: count += 1 m += 1 if count > c1: break print(min(c1, count))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, m = map(int, input().split(" ")) class Fila(list): def __init__(self): super(Fila, self).__init__() self.max_N = 10010 self.queue = [] self.memo = [(False) for _ in range(self.max_N)] def is_empty(self): return len(self.queue) == 0 def push(self, element): if element[0] < 1 or element[0] >= self.max_N: return if self.memo[element[0]]: return self.memo[element[0]] = True self.queue.append(element) def pop(self): if not self.is_empty(): top = self.queue[0] self.queue = self.queue[1:] if len(self.queue) > 1 else [] return top raise Error("The queue is empty!") fila = Fila() fila.push((n, 0)) while not fila.is_empty(): top, step = fila.pop() if top == m: print(step) break fila.push((top - 1, step + 1)) fila.push((top * 2, step + 1))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING CLASS_DEF VAR FUNC_DEF EXPR FUNC_CALL FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR NUMBER NUMBER VAR NUMBER VAR RETURN IF VAR VAR NUMBER RETURN ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF IF FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER LIST RETURN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER WHILE FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, m = [int(x) for x in input().split()] t = 0 def wh(m): global t if m % 2 == 0: m = m / 2 t = t + 1 else: m = (m + 1) / 2 t = t + 2 if m > n: wh(m) else: t = t + n - m print(int(t)) if n >= m: print(n - m) else: wh(m)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
def main(): n, m = [int(x) for x in input().split()] used = [(0) for x in range(1, 100001)] q = [n] count = 1 if m == n: print(0) return elif m < n: print(n - m) return while True: newQ = [] for x in q: if x - 1 == m or 2 * x == m: print(count) return if used[x - 1] == 0 and x - 1 > 0: newQ.append(x - 1) used[x - 1] = 1 if 2 * x < 2 * m and used[2 * x] == 0: newQ.append(2 * x) used[2 * x] = 1 q = newQ count += 1 main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN WHILE NUMBER ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
def two_buttons(start, end): cache = get_cache(start) temp = start while end not in cache: cache[temp * 2] = 1 + cache[temp] for i in range(temp * 2 - 1, temp, -1): if i % 2 == 0: cache[i] = 1 + min(cache[i // 2], cache[i + 1]) else: cache[i] = 1 + cache[i + 1] temp *= 2 return cache[end] def get_cache(start): cache = {i: (start - i) for i in range(1, start)} cache[start] = 0 return cache def get_points_to_mark(end): points_to_mark = [end + 1] if end % 2 == 0: points_to_mark.append(end // 2) return points_to_mark def points_marked(points_to_mark, cache): for point in points_to_mark: if point not in cache: return False return True n, m = [int(e) for e in input().split()] print(two_buttons(n, m))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF FOR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
def findit(n, m): if n == m: print(0) return if n > m: print(n - m) return a = [-1] * (2 * m) q = [n] a[n] = 0 while True: nextone = q.pop(0) if nextone == m: print(a[nextone]) return howmany = a[n] subone = nextone - 1 multtwo = nextone * 2 if subone > 0 and a[subone] == -1: q.append(subone) a[subone] = a[nextone] + 1 if multtwo < 2 * m: q.append(multtwo) a[multtwo] = a[nextone] + 1 listone = input().split() n = int(listone[0]) m = int(listone[1]) findit(n, m)
FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR LIST VAR ASSIGN VAR VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
x, y = list(map(int, input().split())) a = 0 while x != y: a = a + 1 if y < x or y & 1: y = y + 1 else: y = y // 2 print(a)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
def Solve(n, m): moves = 0 stack = [(n, 0)] count = 0 vals = set() while stack: curr = stack.pop(0) k = curr[0] moves = curr[1] if k not in vals: vals.add(k) if k == m: optimal = moves if 2 * k - m <= n: stack.append((k * 2, moves + 1)) if k - 1 >= 0: stack.append((k - 1, moves + 1)) return optimal def main(): n, m = map(int, input().split()) print(Solve(n, m)) main()
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
def min_Push(n, m): if m > n: if m % 2 == 0: return 1 + min_Push(n, m / 2) else: return 1 + min_Push(n, m + 1) else: return n - m nums = input().split() n = int(nums[0]) m = int(nums[1]) print(int(min_Push(n, m)))
FUNC_DEF IF VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
def count_presses(n, m): if n == m: return 0 if n > m: return n - m if m % 2: return count_presses(n, m + 1) + 1 return count_presses(n, m // 2) + 1 print(count_presses(*map(int, input().split())))
FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN BIN_OP VAR VAR IF BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
start, end = map(int, input().split()) step_list = [0] * (10000 * 3) queue = [start] for i in queue: if i == end: break else: if i > 1: if step_list[i - 1] == 0: step_list[i - 1] = step_list[i] + 1 queue.append(i - 1) if i < 10000: if step_list[i * 2] == 0: step_list[i * 2] = step_list[i] + 1 queue.append(i * 2) print(step_list[end])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR LIST VAR FOR VAR VAR IF VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, m = map(int, input().split()) dp = [None] * 10030 todo = [m] cur = 0 while dp[n] is None: newtodo = [] for x in todo: if x < 10030 and dp[x] is None: dp[x] = cur newtodo.append(x + 1) if x % 2 == 0: newtodo.append(x // 2) cur += 1 todo = newtodo print(dp[n])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE NUMBER ASSIGN VAR LIST VAR ASSIGN VAR NUMBER WHILE VAR VAR NONE ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR VAR NONE ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
def solve(): n, m = map(int, input().split()) count = 0 while n != m: count += 1 if m < n: m += 1 elif m % 2 == 0: m /= 2 else: m += 1 print(count) solve()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
def pops(): global head head = head + 1 return q[head - 1] head = 0 n, m = map(int, input().split()) q = [n] d = {n: 0} while q: n = q.pop(0) j = n * 2 k = n - 1 if d.get(j, -1) == -1 and j > -1 and j <= 20001: d[j] = d[n] + 1 q.append(j) if d.get(k, -1) == -1 and k > -1 and k <= 20001: d[k] = d[n] + 1 q.append(k) print(d[m])
FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR ASSIGN VAR DICT VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
a, b = input().split() a = int(a) b = int(b) count = 0 def get_min(a, b): global count if b >= a: if b == a: return count if b % 2 == 0: count += 1 get_min(a, b / 2) else: count += 2 get_min(a, (b + 1) / 2) else: count += int(a - b) get_min(b, b) get_min(a, b) print(count)
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR IF VAR VAR RETURN VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
class Node: def __init__(self, value, level): self.value = value self.level = level def run(): nums = [eval(n) for n in input().split()] start = Node(nums[0], 0) end = nums[1] count = 0 queue = [start] visited = {} while len(queue) != 0: node = queue.pop(0) if node.value == end: count = node.level break if node.value <= 0 or node.value in visited: continue visited[node.value] = True if node.value < end: double = Node(node.value * 2, node.level + 1) queue.append(double) minus = Node(node.value - 1, node.level + 1) queue.append(minus) print(count) run()
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR DICT WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
import sys a, b = map(int, input().split()) depth = 0 while True: red = b / 2 blue = b + 1 depth += 1 if red == a or blue == a: print(depth) exit() elif not red == int(red): b = blue elif blue < a: b = blue else: b = red
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
def count(x, y, res): if x >= y: return x - y + res elif y % 2: return count(x, (y + 1) // 2, res + 2) else: return count(x, y // 2, res + 1) s = input().split() x, y = s print(count(int(x), int(y), 0))
FUNC_DEF IF VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
def findShortest(n, m): count = 0 if n > m: count += n - m n = m while n < m: count += 1 if m % 2 == 0: m //= 2 else: m += 1 return count + n - m n, m = (int(x) for x in input().split(" ")) print(findShortest(n, m))
FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, m = list(map(int, input().split())) d = [-1] * 30000 d[n] = 0 head = 0 tail = 1 q = [] q.append(n) while head < tail: v = q[head] head += 1 if v > 1 and d[v - 1] == -1: d[v - 1] = d[v] + 1 q.append(v - 1) tail += 1 if v <= m and d[2 * v] == -1: d[2 * v] = d[v] + 1 q.append(2 * v) tail += 1 print(d[m])
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
a = list(map(int, input().split())) if a[1] <= a[0]: print(a[0] - a[1]) else: count = 0 while True: if a[1] <= a[0]: count += a[0] - a[1] break elif a[1] % 2 == 0: a[1] //= 2 count += 1 else: a[1] += 1 count += 1 print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
start, goal = [int(x) for x in input().split()] if start >= goal: print(start - goal) else: temp = goal steps = 0 while temp != start: if temp > start: if temp % 2 == 0: temp = temp // 2 else: temp += 1 else: temp += 1 steps += 1 print(steps)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
n, m = map(int, input().split()) t, q = n, 0 while t < m: t *= 2 q += 1 q += t - m stack = [[n, 0]] mn = q visited = [False] * 20000 while stack: cur = stack.pop(0) if cur[1] < q: if cur[0] * 2 == m or cur[0] - 1 == m: if cur[1] + 1 < mn: mn = cur[1] + 1 continue if cur[0] - 1 > 0 and not visited[cur[0] - 1]: visited[cur[0] - 1] = True stack.append([cur[0] - 1, cur[1] + 1]) if cur[0] * 2 < 20000 and not visited[cur[0] * 2]: visited[cur[0] * 2] = True if cur[0] < m: stack.append([cur[0] * 2, cur[1] + 1]) elif cur[1] + cur[0] - m < mn: mn = cur[1] + cur[0] - m print(mn)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR LIST LIST VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
from sys import stdin def get_int_list(): from sys import stdin return [int(x) for x in stdin.readline().replace("\n", "").split(" ")] n, m = get_int_list() count = 0 while n != m: if m > n: if m % 2 == 0: m = m / 2 count += 1 else: m += 1 count += 1 elif n > m: m += 1 count += 1 print(count)
FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING STRING STRING ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
g = {} for i in range(-100, 20001): g[i] = [] for i in range(1, 10001): if 2 * i <= 10000: g[i] = [i - 1, 2 * i] else: g[i] = [i - 1] a, b = list(map(int, input().split(" "))) visited = [False] * 20005 dist = [100000] * 20005 x = [a] dist[a] = 0 while x: a = x.pop() if not visited[a]: visited[a] = True x = g[a] + x for i in g[a]: dist[i] = min(dist[i], dist[a] + 1) print(dist[b])
ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR LIST BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR LIST BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST VAR ASSIGN VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
s = input().split() a = int(s[1]) b = int(s[0]) otv = 0 while a != b: otv += 1 if a % 2 == 0 and a > b: a = a / 2 elif a % 2 == 1 and a > b: a = a + 1 else: a = a + 1 print(otv)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
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 number n. Bob wants to get number m on 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 integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- 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.
import sys def read(): return sys.stdin.readline().strip() def printf(a, sep=" ", end="\n"): sys.stdout.write(sep.join(map(str, a)) + end) def readf(): return [int(i) for i in read().split()] def main(): n, m = readf() if n > m: printf([n - m]) else: k = m // n + 1 dp = [10**4 + 1] * (2 * (n * k) + 1) for i in range(n + 1): dp[i] = n - i dp[i * 2] = min(dp[i * 2], dp[i] + 1) for i in range(n + 1, n * k + 1): if i % 2 == 0: dp[i] = min(dp[i], dp[i // 2] + 1) else: dp[i] = min(dp[i], dp[i + 1] + 1) dp[i * 2] = min(dp[i * 2], dp[i] + 1) printf([dp[m]]) main()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF STRING STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP BIN_OP NUMBER NUMBER NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR