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.
arr = list(map(int, input().rstrip().split())) n = arr[0] m = arr[1] ans = 0 while m > n: if m % 2 == 0: m //= 2 else: m += 1 ans += 1 print(ans + n - m)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER 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.
3 n, m = list(map(int, input().split())) mem = [2**30] * 4 * max(n, m) mem[n] = 0 q = [n] while q: c = q.pop(0) if 2 * c < len(mem) and mem[2 * c] > mem[c] + 1: q.append(2 * c) mem[2 * c] = mem[c] + 1 if c > 1 and mem[c - 1] > mem[c] + 1: q.append(c - 1) mem[c - 1] = mem[c] + 1 print(mem[m])
EXPR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST BIN_OP NUMBER NUMBER NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR 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.
nums = str(input()).split() fr = int(nums[0]) t = int(nums[1]) def diff(a, b): if b <= a: return a - b elif b % 2 == 0: return diff(a, b / 2) + 1 else: return diff(a, (b + 1) / 2) + 2 print(int(diff(fr, t)))
ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR VAR RETURN BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER 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.
n, m = map(int, input().split()) g = [] for _ in range(1, 20001): g.append([]) for i in range(1, 20000): g[i].append(i - 1) for i in range(1, 10001): g[i - 1].append(i * 2 - 1) used = [None] * 20000 used[n - 1] = 0 queue = [n - 1] def bfs(): while queue != []: for v in g[queue[0]]: if used[v] == None: queue.append(v) used[v] = used[queue[0]] + 1 del queue[0] bfs() print(used[m - 1])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NONE NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER FUNC_DEF WHILE VAR LIST FOR VAR VAR VAR NUMBER IF VAR VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR 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.
inputArray = input().split(" ") n = int(inputArray[0]) m = int(inputArray[1]) count = 0 if n == m: print(int(0)) elif m < n: print(int(n - m)) else: while m > n: if m % 2 == 0: m = m / 2 else: m = m + 1 count += 1 count += n - m print(int(count))
ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP 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 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.
p = input() p = p.split() n = int(p[0]) m = int(p[1]) c = 0 if m > n: a = 0 b = n c = m while c > b: b = b * 2 a += 1 while b > c: b -= 1 a += 1 d = 0 e = n f = m while f > e: if f % 2 != 0: f = f + 1 d += 1 f = f // 2 d += 1 while f < e: e -= 1 d += 1 c = min(a, d) if m < n: while m < n: n -= 1 c += 1 print(c)
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 ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR 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 VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR WHILE 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.
def solve(a, b): big = float("inf") small = float("-inf") if a == b: return 0 elif a > b: steps = a - b return steps else: ab, bb = a, b steps = 0 while a < b: last = a a *= 2 steps += 1 steps += a - b nsteps = 0 while ab < bb: last = bb nsteps += 1 rem = 0 if bb % 2 != 0: nsteps += 1 rem += 1 bb //= 2 bb += rem nsteps += ab - bb return min(steps, nsteps) def main(): d = input() d = [int(i) for i in d.split()] a = d[0] b = d[1] ans = solve(a, b) print(ans) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR 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.
n, m = map(int, input().split()) r = [0] * 10**5 r[n] = 1 t = [n] i = 0 while t[i] != m: a = t[i] if a * 2 < 10**5 and r[a * 2] == 0: r[a * 2] = r[a] + 1 t.append(a * 2) if a and r[a - 1] == 0: r[a - 1] = r[a] + 1 t.append(a - 1) i += 1 print(r[m] - 1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER BIN_OP NUMBER 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 IF VAR 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 EXPR FUNC_CALL VAR BIN_OP 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.
import sys rs = [int(i) for i in input().split()] m = rs[0] n = rs[1] if m > n: print(m - n) sys.exit(0) if m == n: print(0) sys.exit(0) q = 0 while m < n: if n % 2 == 0: n = n // 2 else: n += 1 q += 1 if n >= m: print(q + (n - m)) else: print(q + (m - n))
IMPORT ASSIGN VAR FUNC_CALL VAR 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 EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL 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 VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP 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()) ans = [(-1) for i in range(10**5)] q = [(0) for i in range(10**5)] le = rg = 0 q[le] = n ans[n] = 0 def add(x, t): global le, rg, q, ans if ans[x] != -1: return ans[x] = ans[t] + 1 rg += 1 q[rg] = x while le <= rg: top = q[le] le += 1 if top < m: add(top * 2, top) if top > 0: add(top - 1, top) print(ans[m])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_DEF IF VAR VAR NUMBER RETURN ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER 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) if b < a: print(a - b) else: x = a y = b i = 0 while x < y: x = 2 * x i = i + 1 i = i + x - y j = 0 while b > a: if b % 2 != 0: b = b + 1 j = j + 1 b = b / 2 j = j + 1 j = j + a - b m = min(i, j) print(int(m))
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR 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 ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL 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.
import sys def main(n, m): count = 0 if m < n: return n - m while m > n: if m % 2 == 0: m /= 2 count += 1 else: m += 1 count += 1 return count + n - m lines = sys.stdin.readlines() nums = lines[0].strip().split(" ") n = int(nums[0]) m = int(nums[1]) print(int(main(n, m)))
IMPORT FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR RETURN BIN_OP VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR NUMBER STRING 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.
n, m = input().split() n = int(n) m = int(m) if n >= m: print(n - m) else: count = 0 while n < m: count += 1 n *= 2 num = n - m num = bin(num)[2:] count2 = 0 for i in range(count): if num[-1] == "1": count2 += 1 num = num[:-1] if num == "": break if num == "": print(count + count2) else: print(count + count2 + int("0b" + num, 2))
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR STRING IF VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP STRING 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 while n != m: if n >= m: t += n - m break elif n < m and m % 2 == 0: m /= 2 t += 1 else: m += 1 t += 1 print(int(t))
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER 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.
from sys import stdin def solution(n, m): if n == m: print(0) return if n > m: print(n - m) return counter = 0 while n != m: if m > n and m % 2 == 0: m /= 2 else: m += 1 counter += 1 print(counter) for line in stdin: testcases = list(map(int, line.split())) solution(testcases[0], testcases[1])
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 NUMBER WHILE VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR 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 isPair(x): return not x % 2 start, target = map(int, input().split()) ans = 0 while start != target: ans += 1 if start < target and isPair(target): target /= 2 else: target += 1 print(ans)
FUNC_DEF RETURN BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR FUNC_CALL 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.
numbers = input().split() numbers[0] = int(numbers[0]) numbers[1] = int(numbers[1]) def NumPress(numbers): if numbers[0] >= numbers[1]: return numbers[0] - numbers[1] length = 0 visited = [] OldQueue = [numbers[0]] NewQueue = [] while 1 == 1: length = length + 1 add = True for i in range(len(OldQueue)): if OldQueue[i] - 1 == numbers[1]: return length if OldQueue[i] - 1 > 0: if OldQueue[i] - 1 not in visited: NewQueue.append(OldQueue[i] - 1) visited.append(OldQueue[i] - 1) if OldQueue[i] * 2 == numbers[1]: return length if OldQueue[i] < numbers[1]: NewQueue.append(OldQueue[i] * 2) visited.append(OldQueue[i] * 2) OldQueue = NewQueue NewQueue = [] print(NumPress(numbers))
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST WHILE NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR IF BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST 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), b = map(int, input().split()), 0 def m_is_even(m): return m % 2 == 0 if n == m: print(b) else: while m > n: b += 1 if m_is_even(m) and m > n: m -= m // 2 continue m += 1 while n > m: b += 1 m += 1 print(b)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER FUNC_DEF RETURN BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE 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.
n, m = map(int, input().split()) answer = 0 while True: red = m / 2 blue = m + 1 answer += 1 if red == n or blue == n: break elif not red == int(red): m = blue else: tryred = abs(1 - red / n) tryblue = abs(1 - blue / n) if tryred < tryblue: m = red elif blue < n: m = blue else: m = red print(answer)
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 IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR 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.
def fun(n, m): 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 return moves n, t = map(int, str(input()).split(" ")) print(fun(n, t))
FUNC_DEF 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 RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR 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 = map(int, input().split()) lis = [[m, 0]] length = 1 dp = [(0) for i in range(100000)] while True: if length == 0 or length == 200: break li = lis.pop() length -= 1 a = li[0] b = li[1] if dp[a] == 1: continue dp[a] = 1 if a == n: print(b) break if a // 2 == a / 2: l = [a // 2, b + 1] lis.insert(0, l) length += 1 l = [a + 1, b + 1] lis.insert(0, l) length += 1
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER WHILE NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER 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.
n, m = map(int, input().split()) if n >= m: print(n - m) else: c = 0 d = m while True: if d <= n: break if d % 2 == 0: d = d // 2 c = c + 1 elif d % 2 != 0: d = d + 1 d = d // 2 c = c + 2 c = c + (n - d) print(c)
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 ASSIGN VAR VAR WHILE NUMBER IF VAR VAR IF 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 NUMBER ASSIGN VAR BIN_OP 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 bfs(n, m): parent = [-1] * (10 * m) q = list() q.append(n) parent[n] = n while len(q) != 0: child = q.pop() if child == m: break if child > 2 * m: continue if child * 2 < len(parent) and parent[child * 2] == -1: q.append(child * 2) parent[child * 2] = child if child - 1 >= 0 and parent[child - 1] == -1: parent[child - 1] = child q.append(child - 1) leaf = m cnt = 0 print(parent) while parent[leaf] != n: cnt += 1 leaf = parent[leaf] return cnt + 1 def main(): n, m = [int(t) for t in input().split()] if n >= m: print(n - m) return cnt = 0 while m != n: if m > n and m % 2 == 0: m /= 2 elif m < n or m > n and m % 2 != 0: m += 1 m = int(m) cnt += 1 print(cnt) main()
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR VAR IF VAR BIN_OP NUMBER VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR 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.
x, y = map(int, input().split()) if x > y: print(x - y) else: c = 0 while y > x: if y % 2 == 0: y = y // 2 c += 1 else: y = y + 1 c += 1 print(c + (x - y))
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 ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP 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.
def bfs(start, goal): moves = 0 q = [start] seen = set() while q: for _ in range(len(q)): num = q.pop(0) if num < 1 or num > 10**4: continue if num == goal: return moves seen.add(num) if num * 2 not in seen: q.append(num * 2) if num - 1 not in seen: q.append(num - 1) moves += 1 return moves test = input() s, g = test.split(" ") print(bfs(int(s), int(g)))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR BIN_OP NUMBER NUMBER IF VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR 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.
class Solution: def __init__(self): self.n, self.m = map(int, input().split()) self.cnt = 0 def solution(self): if self.n >= self.m: print(self.n - self.m) else: while self.m > self.n: if self.m % 2 == 0: self.m //= 2 else: self.m += 1 self.cnt += 1 if self.m == self.n: print(self.cnt) else: print(self.cnt + self.n - self.m) def main(): ob = Solution() ob.solution() main()
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL 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 Main(n, m): cnt = 0 lft = n rgt = m while True: if lft == rgt: break if lft < rgt: if rgt % 2 == 1: rgt += 1 rgt //= 2 cnt += 2 else: rgt //= 2 cnt += 1 elif lft > rgt: cnt += lft - rgt rgt += lft - rgt print(cnt) n, m = map(int, input().split()) Main(n, m)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE NUMBER IF VAR VAR IF VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL 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()) v = set() s = [n] g = {} while s: x = s.pop() if x not in v: v.add(x) if x <= 1: s.append(2 * x) g[x] = [2 * x] elif x > m: s.append(x - 1) g[x] = [x - 1] else: s.append(x - 1) s.append(2 * x) g[x] = [x - 1, 2 * x] v = [False] * 50000 d = [0] * 50000 q = [n] v[n] = True while q: s = q.pop(0) for u in g[s]: if v[u]: continue v[u] = True if d[u] == 0: d[u] = d[s] + 1 else: d[u] = min(d[u], d[s] + 1) q.append(u) print(d[m])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR ASSIGN VAR DICT WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR LIST BIN_OP NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR LIST BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR LIST BIN_OP VAR NUMBER BIN_OP NUMBER VAR 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 NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR 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 def calculate(n, m): if n >= m: return n - m elif m % 2 == 0: return calculate(n, m // 2) + 1 else: return calculate(n, m + 1) + 1 line = list(map(int, sys.stdin.readline().split())) n = line[0] m = line[1] print(calculate(n, m))
IMPORT FUNC_DEF IF VAR VAR RETURN BIN_OP VAR VAR IF BIN_OP VAR NUMBER 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER 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 bfs(graph, begin): d = [None] * len(graph) d[begin] = 0 vertices = [begin] cur_d = 1 while len(vertices) > 0: s_ver = [] for v in vertices: for u in graph[v]: if d[u] is None: d[u] = cur_d s_ver.append(u) vertices = s_ver cur_d += 1 return d n, m = list(map(int, input().split())) graph = [[] for _ in range(max(n, m) * 2 + 1)] for i in range(1, max(n, m) * 2 + 1): if i - 1 > 0: graph[i].append(i - 1) if i * 2 <= max(n, m) * 2: graph[i].append(i * 2) print(bfs(graph, n)[m])
FUNC_DEF ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR IF VAR VAR NONE ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER 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.
n, m = map(int, input().split()) done = {n} frontier = {n} i = 0 while True: i += 1 new_f = set() for e in frontier: for v in (e * 2, e - 1): if v < 0 or v in done or v > 20000: continue if v == m: print(i) exit() done.add(v) new_f.add(v) frontier = set() for e in new_f: frontier.add(e)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FOR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR 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 get_min_operations(n, m): if m == n: return 0 if m < n: return n - m cnt = 0 while m >= n: if m == n: return cnt if m % 2 != 0: cnt += 1 m += 1 else: if m / 2 < n: return cnt + 1 + n - int(m / 2) cnt += 1 m /= 2 return cnt n, m = map(int, input().split()) print(get_min_operations(n, m))
FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR RETURN VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER 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 = [int(i) for i in input().split()] c = 0 while True: if m == n: print(c) break if m % 2 == 1 or m < n: c += 1 m += 1 else: m = m // 2 c += 1
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP 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.
n, ex = list(map(int, input().split())) count = 0 if ex < n: print(n - ex) else: ex, n = n, ex while True: if n < ex or n == ex: count += ex - n print(count) break elif n % 2 == 0: n //= 2 count += 1 elif n % 2 != 0: n += 1 count += 1
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR WHILE NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER 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.
inp = input().split() num1 = int(inp[0]) num2 = int(inp[1]) def least_presses(start, goal): curr = list() curr.append(start) visited = list() visited.append(start) steps = 0 while True: next = list() for num in curr: if num * 2 not in visited and num <= -(-goal // 2): if num * 2 == goal: return steps + 1 visited.append(num * 2) next.append(num * 2) if num - 1 not in visited and num > 1: if num - 1 == goal: return steps + 1 visited.append(num - 1) next.append(num - 1) curr = next steps += 1 print(least_presses(num1, num2))
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER 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 main() -> object: n, m = [int(i) for i in input().split()] count = 0 while n < m: if m % 2 == 0: m >>= 1 else: m += 1 count += 1 count += n - m return count print(main())
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR 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 VAR BIN_OP VAR VAR RETURN VAR VAR 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 = [int(x) for x in input().split(" ")] d = 0 def main(n, m): global d if m == n: print(d) exit() if n >= m: d += n - m print(d) exit() elif m % 2 == 0: d += 1 m //= 2 else: d += 1 m += 1 return main(n, m) main(n, m)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR 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 two_degree(n, m): deg = 0 res = n while res < m: res *= 2 deg += 1 print(res) return deg, res - m my_str = input() n, m = int(my_str.split()[0]), int(my_str.split()[1]) if m < n: print(n - m) else: res = n oper = 0 while res != m: if m % 2 == 1 and m > res: m += 1 oper += 1 elif m > res: m = int(m / 2) oper += 1 elif m < res: oper += res - m m = res print(oper)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR 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.
l = list(map(int, input().split())) n = l[0] m = l[1] if m < n: print(n - m) else: r = 0 while m != n: if m < n: r += n - m break if m % 2 == 0: r += 1 m = int(m / 2) else: r += 1 m += 1 print(r)
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 WHILE VAR VAR IF VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL 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.
def get_num_of_steps(start_num, target_num): upper_bound = start_num while upper_bound < target_num: upper_bound *= 2 visited = [False] * (upper_bound + 1) visited[start_num] = True queue = [(start_num, 0)] min_steps = None while len(queue) != 0: head, steps = queue[0] queue = queue[1:] if head < target_num: if 2 * head <= upper_bound and not visited[2 * head]: visited[2 * head] = True queue.append((2 * head, steps + 1)) if head >= 1 and not visited[head - 1]: visited[head - 1] = True queue.append((head - 1, steps + 1)) elif head > target_num: num_steps = steps + head - target_num if min_steps is None or num_steps < min_steps: min_steps = num_steps elif min_steps is None or steps < min_steps: min_steps = steps return min_steps def solve(): line = input() start_num, target_num = [int(num) for num in line.split()] num_of_steps = get_num_of_steps(start_num, target_num) print(num_of_steps) solve()
FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NONE WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR IF BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NONE VAR VAR ASSIGN VAR VAR IF VAR NONE VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR 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.
def main(): n, m = map(int, input().split()) res, delta = 0, 1 while n < m: res += 1 n *= 2 delta *= 2 while n > m: while n - delta >= m: res += 1 n -= delta delta //= 2 print(res) def __starting_point(): main() __starting_point()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR WHILE BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL 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.
from sys import * inp = lambda: stdin.readline() def main(): n, m = map(int, inp().split()) ans = 0 if n < m: while m > n: if m % 2 == 0: m //= 2 ans += 1 else: m += 1 ans += 1 ans += n - m else: ans += n - m print(ans) main()
ASSIGN VAR FUNC_CALL VAR FUNC_DEF 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 VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP 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.
def solution(n, m): operations = 0 while n != m: operations += 1 m += 1 if m < n or m % 2 == 1 else -m // 2 return operations n, m = map(int, input().split()) print(solution(n, m))
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP 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.
def fun(n, m): if n >= m: return n - m Q = [n] mySet = set() cnt = 1 while True: new_Q = [] for num in Q: x = 2 * num y = num - 1 if x == m or y == m: return cnt if not x in mySet and x < 20000 and x > 0: mySet.add(x) new_Q.append(x) if not y in mySet and y < 20000 and y > 0: mySet.add(y) new_Q.append(y) Q = new_Q cnt += 1 n, m = [int(c) for c in input().split()] print(fun(n, m))
FUNC_DEF IF VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR RETURN VAR IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR 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.
n, m = map(int, input().split()) count = 0 while m > n: if m % 2 == 0: m = m // 2 else: m += 1 count += 1 while m < n: m += 1 count += 1 print(count)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER WHILE 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.
input = input().split() start = int(input[0]) goal = int(input[1]) if goal == start: print(0) list = [] list.append(start) found = False i = 0 done = [False] * 20000 iter = 0 while not found: end = len(list) if goal in list[i:end]: print(iter) found = True for j in range(i, end): if list[j] * 2 < len(done): if done[list[j] * 2] == False: list.append(list[j] * 2) done[list[j] * 2] = True if list[j] != 1: if done[list[j] - 1] == False: list.append(list[j] - 1) done[list[j] - 1] = True iter += 1 i = end quit()
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN 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 = list(map(lambda x: int(x), input().split())) graph = {} for i in range(2, 10**4 + 1): graph[i] = [] graph[1] = [2] distances = {} max_value = 10**4 for i in range(2, max_value + 1): graph[i] = [i - 1] if i * 2 <= max_value: graph[i].append(i * 2) path_length = 0 q = [n] distances[n] = 0 used_to = [False] * (10**4 + 1) used_to[n] = True while len(q) != 0: current_node = q.pop(0) if current_node == m: print(distances[m]) exit(0) for node in graph[current_node]: if used_to[node] == False: q.append(node) distances[node] = distances[current_node] + 1 used_to[node] = True
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR LIST ASSIGN VAR NUMBER LIST NUMBER ASSIGN VAR DICT ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR LIST BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN 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(u, m): N = 10**5 vis = [False] * N d = [10**10] * N d[u] = 0 q = [] q.append(u) while q: v = q.pop(0) if v == m: return d[v] if v < 0: continue if vis[v] == False: vis[v] = True q.append(v - 1) d[v - 1] = d[v] + 1 if v < m: q.append(2 * v) d[2 * v] = d[v] + 1 return -1 n, m = list(map(int, input().split())) if n > m: print(n - m) else: k = bfs(n, m) print(k)
FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR RETURN VAR VAR IF VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR VAR NUMBER RETURN 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 ASSIGN VAR 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.
def botoes(n, m): op = 0 while True: if m == n: return op if m < n: return op + int(n - m) if m % 2 == 0: m /= 2 op += 1 else: m += 1 op += 1 NM = [int(i) for i in input().split()] n = NM[0] m = NM[1] print(botoes(n, m))
FUNC_DEF ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR RETURN VAR IF VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER 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 tb(x, y): presses = 0 if x > y: print(x - y) return 0 while y != x: if y < x or y % 2 == 1: y += 1 presses += 1 else: y = y / 2 presses += 1 print(presses) return 0 values = input().split() n = int(values[0]) m = int(values[1]) tb(n, m)
FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN NUMBER WHILE VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN 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.
start, end = input().split() start = int(start) end = int(end) counter = 0 if end < start: print(int(min(start - 1, start - end))) else: end_new = end while end_new > start: if end_new % 2 != 0: end_new = end_new + 1 counter += 1 else: end_new = end_new / 2 counter += 1 if end_new < start: counter += min(start - 1, start - end_new) - 1 if end_new != start: start -= 1 counter += 1 print(int(counter))
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR 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 IF VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR 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().split()) q = [n] visit = {} dist = {n: 0} while len(q) != 0: nod = q.pop(0) if nod == m: print(dist[m]) break if nod - 1 not in visit and nod - 1 > 0: visit[nod - 1] = 1 dist[nod - 1] = dist[nod] + 1 q.append(nod - 1) if nod * 2 not in visit and nod < m: visit[nod * 2] = 1 dist[nod * 2] = dist[nod] + 1 q.append(nod * 2)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR ASSIGN VAR DICT ASSIGN VAR DICT VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN 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 BIN_OP VAR NUMBER VAR VAR VAR ASSIGN 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
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 b, e = list(map(int, input().split())) if b >= e: print(b - e) sys.exit() count = 0 while b is not e: if e > b: if e % 2 is 0: e = e // 2 else: e = e + 1 else: count = count + b - e break count = count + 1 print(count)
IMPORT 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 ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR 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 VAR VAR 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 ri = lambda: map(int, input().split(" ")) n, m = ri() seen = set() q = [(n, 0)] seen.add(n) while q: x, t = q[0] q.pop(0) if x == m: import sys print(t) sys.exit(0) if x > 1 and x - 1 not in seen: seen.add(x - 1) q.append((x - 1, t + 1)) if x < m and 2 * x not in seen: seen.add(2 * x) q.append((2 * x, t + 1))
IMPORT ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR IMPORT EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP 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 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 = input().split() n, m = int(n), int(m) x = m Sum = 0 if n >= m: print(n - m) else: v = 0 for i in range(0, 15): m = m / 2 v = v + 1 if m <= n: break if m == n: print(v) elif m == int(m): a = n - m print(int(a + v)) else: m = x a = 0 for j in range(0, 15): m = m / 2 if m == int(m): if m <= n: break continue else: m = int(m) + 1 a = a + 1 if m <= n: break if m == n: Sum = j + a + 1 else: b = n - int(m) + 1 Sum = j + a + b print(Sum)
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR 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.
n, m = [int(x) for x in input().split()] nb_steps = 0 copy_m = m double = {1} while copy_m != 1: copy_m = int((copy_m + 1) / 2) double.add(copy_m) while n != m: nb_steps += 1 if n in double: n = 2 * n else: n -= 1 print(nb_steps)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP NUMBER 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 = [int(i) for i in input().split(" ")] times_ = 0 minus_ = 0 while n < m: if m % 2 == 0: m = int(m / 2) else: m = m + 1 times_ += 1 print(times_ + (n - m))
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP 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.
def bfs(n, m): queue = [n] level = {} level[n] = 0 if n > m: return n - m while queue: s = queue.pop(0) for a in [s * 2, s - 1]: if level.get(a) == None and a > 0 and a < 2 * m: level[a] = level[s] + 1 queue.append(a) return level.get(m) n, m = map(int, input().split()) print(bfs(n, m))
FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER IF VAR VAR RETURN BIN_OP VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NONE VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR 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()) h = 1 q = [n] k = 2 * 10**4 + 1 popa = [(0) for i in range(2 * 10**4 + 1)] if n > m: print(n - m) else: visited = [] while q: n = [] for i in q: if i * 2 not in visited and i < 2 * m: if 2 * i == m: print(h) exit() n.append(i * 2) visited.append(2 * i) if i - 1 not in visited and i > 1: visited.append(i - 1) if i - 1 == m: print(h) exit() n.append(i - 1) visited.append(2 * i) q = n h += 1
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP NUMBER VAR IF BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN 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.
n, m = map(int, input().split()) if n > m: print(n - m) else: q = [n] i = 0 steps = {n: 0} run = True while i < len(q) and run: v = q[i] i += 1 nxt = [] if v > 1: nxt.append(v - 1) if v < m: nxt.append(v * 2) for u in nxt: if u > 0 and u not in steps: q.append(u) steps[u] = steps[v] + 1 if u == m: run = False break print(steps[m])
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 LIST VAR ASSIGN VAR NUMBER ASSIGN VAR DICT VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR 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 VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN 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 class Linear_Graph: def __init__(self, lis): self._vertices = lis self._connections = dict() def construct_links(self): for vert in self._vertices: if vert not in self._connections: self._connections[vert] = [] if vert - 1 in self._vertices: self._connections[vert].append(vert - 1) if vert * 2 in self._vertices: self._connections[vert].append(vert * 2) def print(self): print(self._connections) def shortest_path(self, start, end, discovered): level = [start] discovered[start] = None depth = 0 while len(level) > 0: next_level = [] for vert in level: for adjacent in self._connections[vert]: if adjacent == end: depth += 1 print(depth) sys.exit() if adjacent not in discovered: next_level.append(adjacent) discovered[adjacent] = vert level = next_level depth += 1 start, end = map(int, input().split()) if start >= end: print(start - end) sys.exit() a = Linear_Graph(list(range(1, end * 2 + 10))) a.construct_links() print(a.shortest_path(start, end, dict()))
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF FOR VAR VAR IF VAR VAR ASSIGN VAR VAR LIST IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR VAR NONE ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR 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 = list(map(int, input().split())) def bouton(n, m): if n == m: return 0 if n > m: return n - m if n <= 0 and m > 0: return n if m % 2 == 1: return 1 + bouton(n, m + 1) else: return 1 + bouton(n, m / 2) print(int(bouton(n, m)))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER RETURN 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 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.
x = input() n, m = x.strip().split(" ") n, m = int(n), int(m) LIM = 10**4 + 100 st = [None] * (LIM + 1) st[n] = 0 delam = [n] while len(delam) > 0: nov = set() for x in delam: for k in (2 * x, x - 1): if 0 <= k < LIM: if st[k] is None: nov.add(k) st[k] = st[x] + 1 elif st[x] + 1 < st[k]: nov.add(k) st[k] = st[x] + 1 delam = nov print(st[m])
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FOR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER IF NUMBER VAR VAR IF VAR VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR 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 extend(l): global n, door for i in l[:]: if done[i] == 0: if i < n and i > 0: if i * 2 not in l: l.append(i * 2) if i - 1 not in l: l.append(i - 1) elif i > 0: door = min(step + i - n, door) l.remove(i) done[i] = 1 return l m, n = map(int, input().split()) s = [m] step = 0 door = 10000 done = [0] * max(m + 1, 2 * n + 1) while n not in s: s = extend(s) step += 1 if step > door: break print(min(door, step))
FUNC_DEF FOR VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR 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.
a, b = [int(x) for x in input().split()] if a > b: print(a - b) else: tot = 0 while a < b: a *= 2 tot += 1 greatestPower = 2**tot remainder = (a - b) % greatestPower tot += (a - b) // greatestPower for i in bin(remainder)[2:]: if i == "1": tot += 1 print(tot)
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 NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR STRING 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.
ans = [-1] * 20004 def go(cur): ans[cur] = 0 q = [[cur, 0]] while q: u, d = q.pop(0) if u - 1 >= 0: if ans[u - 1] == -1: ans[u - 1] = d + 1 q.append([u - 1, d + 1]) if u * 2 <= 20000: if ans[2 * u] == -1: ans[2 * u] = d + 1 q.append([2 * u, d + 1]) n, m = map(int, input().split()) go(n) print(ans[m])
ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR LIST LIST VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR 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.
n, m = map(int, input().split()) d = [] a = [] count = 0 a.append(n) if m < n: print(n - m) else: while 1: b = [] count = count + 1 for i in a: if i not in d: d.append(i) if i <= m: if 2 * i - m <= 100: b.append(2 * i) if i - 1 > 0: b.append(i - 1) if m in b: break a = b print(count)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR WHILE NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR IF BIN_OP BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR 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.
currentsteps = 9999999999999 def step(current, goal, steps, stepes): global currentsteps if stepes > 500: return 99999999999999 if steps > currentsteps: return steps valores = [] for n in current: if n >= goal: val = steps + abs(n - goal) if val < currentsteps: currentsteps = val continue elif n >= goal / 2: val = abs(n - int(goal / 2)) + 1 if val < currentsteps: currentsteps = val continue if n < goal: valor1 = n * 2 if valor1 == goal: return steps + 1 if valor1 not in valores: valores.append(valor1) if n >= goal / 2: valor2 = n - 1 if valor2 == goal: return steps + 1 if valor2 >= 0 and valor2 not in valores: valores.append(valor2) if len(valores) == 0: return 9999999999 else: return step(valores, goal, steps + 1, stepes + 1) line = input() numbers = line.split(" ") n = int(numbers[0]) m = int(numbers[1]) goal = m divnumber = 0 impares = [] while n < goal / 2 and goal % 2 == 0 or n < int(goal / 2 + 1) and goal % 2 == 1: divnumber += 1 if goal % 2 == 0: goal = int(goal / 2) impares.append(False) else: goal = int(goal / 2 + 1) impares.append(True) launch = n min = step([launch], goal, 0, 0) if currentsteps <= min: min = currentsteps for i in range(0, divnumber): launch = goal if impares[-1 - i] == True: goal -= 1 goal *= 2 goal += 1 else: goal *= 2 currentsteps = 99999999999989 newmin = step([launch], goal, 0, 0) if currentsteps <= newmin: newmin = currentsteps min += newmin print(min)
ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR RETURN BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR RETURN BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER 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 VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR IF VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN 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.
def count_minor_ops(a, b): ops = 0 while True: if a == b: break elif b < a: ops += a - b break elif b % 2 == 0: b = b // 2 else: b += 1 ops += 1 return ops a, b = list(map(int, input().split())) print(count_minor_ops(a, b))
FUNC_DEF ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR IF VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL 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.
numbers = input().split() numbers = [int(x) for x in numbers] steps = 0 while numbers[0] != numbers[1]: if numbers[0] == numbers[1]: print(steps) break if numbers[1] % 2 == 1: numbers[1] += 1 steps += 1 while numbers[0] < numbers[1] and numbers: if numbers[1] % 2 == 1: numbers[1] += 1 steps += 1 numbers[1] /= 2 steps += 1 while numbers[0] > numbers[1]: numbers[0] -= 1 steps += 1 print(steps)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER WHILE VAR 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.
def fun(x, y, c): if x == y: print(c) elif y > x: if y % 2 == 0: c += 1 fun(x, y / 2, c) else: c += 1 fun(x, y + 1, c) else: print(int(x - y + c)) n, m = input().split() n = int(n) m = int(m) fun(n, m, 0)
FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR 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.
a, b = map(int, input().split()) z = 0 while a < b: if b % 2 == True: z += 1 b += 1 else: b = b // 2 z += 1 print(str(int(abs(z + a - b))))
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 ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR 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.
a, b = [int(x) for x in input().split()] count = 0 big = b small = a while big > small: count += 1 if big % 2 == 0: big /= 2 else: big += 1 ans = count + (small - big) print(int(ans))
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP 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 = map(int, input().split()) if n > m: print(n - m) exit(0) c = 0 while n < m: if m % 2 == 0: m = m / 2 else: m += 1 c += 1 print(int(c + n - m))
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 NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER 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.
def main(): n, m = input().split() n, m = int(n), int(m) a = 0 while m > n: if m % 2: m += 1 else: m //= 2 a += 1 print(a + n - m) main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP 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 = [int(i) for i in input().split(" ")] def tow_buttons(m: int) -> int: return ( 0 if n == m else ( n - m if n > m else tow_buttons(m // 2) + 1 if not m % 2 else tow_buttons((m + 1) // 2) + 2 ) ) print(tow_buttons(m))
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF VAR RETURN VAR VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER 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 def twoButton(): import sys data = sys.stdin.read() data = data.split(" ") n = int(data[0]) m = int(data[1]) if n == m: return 0 elif m < n: return n - m else: count = 0 while n < m: if m % 2 == 0: m = m / 2 count += 1 else: m += 1 count += 1 while n > m: m += 1 count += 1 return count print(twoButton())
IMPORT FUNC_DEF IMPORT 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 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 ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER RETURN VAR 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.
import sys input_list = input().split() beg = int(input_list[0]) end = int(input_list[1]) steps = 0 while end > beg: if end % 2 == 1: end = end + 1 else: end = end / 2 steps = steps + 1 diff = abs(beg - end) steps = steps + diff print(int(steps))
IMPORT 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 BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL 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.
def solve(n, m): count = 0 while m != n: count += 1 if m > n and m % 2 == 0: m /= 2 else: m += 1 print(count) x = tuple(map(int, input().rstrip().split())) solve(x[0], x[1])
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR 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 getPushes(n, m): pushes = 0 while m != n: if m < n: m += 1 elif m % 2 == 0: m /= 2 else: m += 1 pushes += 1 return pushes s = input().split() n = int(s[0]) m = int(s[1]) if m <= n: print(n - m) else: print(int(getPushes(n, m)))
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR 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.
n, m = map(int, input().split()) if n >= m: print(n - m), exit() v = [-1] * (2 * m) v[m] = 0 q = [m] while q: cur = q.pop(0) d = v[cur] if cur < 2 * m - 1 and v[cur + 1] == -1: v[cur + 1] = d + 1 q.append(cur + 1) if cur % 2 == 0 and v[cur // 2] == -1: v[cur // 2] = d + 1 q.append(cur // 2) print(v[n])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP 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.
def main(): a = list(map(int, input().split(" "))) n, m = a[0], a[1] if n >= m: print(n - m) else: d = [1000000000000.0] * (2 * m) d[n] = 0 Q = [n] while Q: e = Q[0] del Q[0] if e < m and d[2 * e] >= 1000000000000.0: Q.append(2 * e) d[2 * e] = d[e] + 1 if e - 1 > 0 and d[e - 1] >= 1000000000000.0: Q.append(e - 1) d[e - 1] = d[e] + 1 print(d[m]) while True: try: main() except: break
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR WHILE 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.
n, m = (int(x) for x in input().split()) q = [(n, 0)] visited = {n} while q: now = q[0] del q[0] if now[0] == m: print(now[1]) break else: if now[0] * 2 <= 2 * m and not now[0] * 2 in visited: q.append((now[0] * 2, now[1] + 1)) visited.add(now[0] * 2) if now[0] - 1 > 0 and not now[0] - 1 in visited: q.append((now[0] - 1, now[1] + 1)) visited.add(now[0] - 1)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR 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 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.
m, n = [int(i) for i in input().split()] def d(begin, end): if begin == end: return 0 if begin == end + 1: return 1 if end % 2 == 0 and end // 2 == begin: return 1 if begin > end: return begin - end if end % 2 == 0: return d(begin, end // 2) + 1 return d(begin, end + 1) + 1 print(d(m, n))
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR RETURN NUMBER IF VAR VAR RETURN BIN_OP VAR VAR IF BIN_OP VAR NUMBER 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 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()) INF = 100000000 dp = [INF] * max(n + 2, m + 2) for i in range(n, 0, -1): dp[i] = n - i for i in range(1, m): if 2 * i <= m + 1: dp[2 * i] = min(dp[2 * i], dp[i] + 1) dp[2 * i - 1] = min(dp[2 * i - 1], dp[i] + 2) print(dp[m])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Dima worked all day and wrote down on a long paper strip his favorite number $n$ consisting of $l$ digits. Unfortunately, the strip turned out to be so long that it didn't fit in the Dima's bookshelf. To solve the issue, Dima decided to split the strip into two non-empty parts so that each of them contains a positive integer without leading zeros. After that he will compute the sum of the two integers and write it down on a new strip. Dima wants the resulting integer to be as small as possible, because it increases the chances that the sum will fit it in the bookshelf. Help Dima decide what is the minimum sum he can obtain. -----Input----- The first line contains a single integer $l$ ($2 \le l \le 100\,000$) — the length of the Dima's favorite number. The second line contains the positive integer $n$ initially written on the strip: the Dima's favorite number. The integer $n$ consists of exactly $l$ digits and it does not contain leading zeros. Dima guarantees, that there is at least one valid way to split the strip. -----Output----- Print a single integer — the smallest number Dima can obtain. -----Examples----- Input 7 1234567 Output 1801 Input 3 101 Output 11 -----Note----- In the first example Dima can split the number $1234567$ into integers $1234$ and $567$. Their sum is $1801$. In the second example Dima can split the number $101$ into integers $10$ and $1$. Their sum is $11$. Note that it is impossible to split the strip into "1" and "01" since the numbers can't start with zeros.
n = int(input()) s = input() idx = (n + 1) // 2 - 1 if n % 2 == 1: x = idx + 1 ans = int(s) while x < n and s[x] == "0": x += 1 if x < n: t1 = int(s[0:x]) t2 = int(s[x:n]) ans = min(t1 + t2, ans) x = idx while x >= 0 and s[x] == "0": x -= 1 if x > 0: t1 = int(s[0:x]) t2 = int(s[x:n]) ans = min(t1 + t2, ans) else: x = idx + 1 ans = int(s) while x < n and s[x] == "0": x += 1 if x < n: t1 = int(s[0:x]) t2 = int(s[x:n]) ans = min(t1 + t2, ans) x = idx while x >= 0 and s[x] == "0": x -= 1 if x > 0: t1 = int(s[0:x]) t2 = int(s[x:n]) ans = min(t1 + t2, ans) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Dima worked all day and wrote down on a long paper strip his favorite number $n$ consisting of $l$ digits. Unfortunately, the strip turned out to be so long that it didn't fit in the Dima's bookshelf. To solve the issue, Dima decided to split the strip into two non-empty parts so that each of them contains a positive integer without leading zeros. After that he will compute the sum of the two integers and write it down on a new strip. Dima wants the resulting integer to be as small as possible, because it increases the chances that the sum will fit it in the bookshelf. Help Dima decide what is the minimum sum he can obtain. -----Input----- The first line contains a single integer $l$ ($2 \le l \le 100\,000$) — the length of the Dima's favorite number. The second line contains the positive integer $n$ initially written on the strip: the Dima's favorite number. The integer $n$ consists of exactly $l$ digits and it does not contain leading zeros. Dima guarantees, that there is at least one valid way to split the strip. -----Output----- Print a single integer — the smallest number Dima can obtain. -----Examples----- Input 7 1234567 Output 1801 Input 3 101 Output 11 -----Note----- In the first example Dima can split the number $1234567$ into integers $1234$ and $567$. Their sum is $1801$. In the second example Dima can split the number $101$ into integers $10$ and $1$. Their sum is $11$. Note that it is impossible to split the strip into "1" and "01" since the numbers can't start with zeros.
n = int(input()) m = input() x, y, z = -1, -1, -1 for i in range(n // 2 - 1, -1, -1): if m[i] != "0": x = i break for i in range(n - n // 2, n): if m[i] != "0": y = i break if n % 2 == 1 and m[n // 2] != "0": z = n // 2 ans = int(m) for xx in [x, y, z]: if xx == -1: continue try: ans = min(ans, int(m[:xx]) + int(m[xx:])) except: pass print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR LIST VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Dima worked all day and wrote down on a long paper strip his favorite number $n$ consisting of $l$ digits. Unfortunately, the strip turned out to be so long that it didn't fit in the Dima's bookshelf. To solve the issue, Dima decided to split the strip into two non-empty parts so that each of them contains a positive integer without leading zeros. After that he will compute the sum of the two integers and write it down on a new strip. Dima wants the resulting integer to be as small as possible, because it increases the chances that the sum will fit it in the bookshelf. Help Dima decide what is the minimum sum he can obtain. -----Input----- The first line contains a single integer $l$ ($2 \le l \le 100\,000$) — the length of the Dima's favorite number. The second line contains the positive integer $n$ initially written on the strip: the Dima's favorite number. The integer $n$ consists of exactly $l$ digits and it does not contain leading zeros. Dima guarantees, that there is at least one valid way to split the strip. -----Output----- Print a single integer — the smallest number Dima can obtain. -----Examples----- Input 7 1234567 Output 1801 Input 3 101 Output 11 -----Note----- In the first example Dima can split the number $1234567$ into integers $1234$ and $567$. Their sum is $1801$. In the second example Dima can split the number $101$ into integers $10$ and $1$. Their sum is $11$. Note that it is impossible to split the strip into "1" and "01" since the numbers can't start with zeros.
l = int(input()) s = input() x = l // 2 y = (l + 1) // 2 while x > -1 and s[x] == "0": x -= 1 while y < l and s[y] == "0": y += 1 a = 0 b = 0 for i in range(x): a = 10 * a + int(s[i]) for i in range(x, l): b = 10 * b + int(s[i]) ans = a + b a = 0 b = 0 if y != l: for i in range(y): a = 10 * a + int(s[i]) for i in range(y, l): b = 10 * b + int(s[i]) print(min(ans, a + b)) else: print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER VAR VAR STRING VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Dima worked all day and wrote down on a long paper strip his favorite number $n$ consisting of $l$ digits. Unfortunately, the strip turned out to be so long that it didn't fit in the Dima's bookshelf. To solve the issue, Dima decided to split the strip into two non-empty parts so that each of them contains a positive integer without leading zeros. After that he will compute the sum of the two integers and write it down on a new strip. Dima wants the resulting integer to be as small as possible, because it increases the chances that the sum will fit it in the bookshelf. Help Dima decide what is the minimum sum he can obtain. -----Input----- The first line contains a single integer $l$ ($2 \le l \le 100\,000$) — the length of the Dima's favorite number. The second line contains the positive integer $n$ initially written on the strip: the Dima's favorite number. The integer $n$ consists of exactly $l$ digits and it does not contain leading zeros. Dima guarantees, that there is at least one valid way to split the strip. -----Output----- Print a single integer — the smallest number Dima can obtain. -----Examples----- Input 7 1234567 Output 1801 Input 3 101 Output 11 -----Note----- In the first example Dima can split the number $1234567$ into integers $1234$ and $567$. Their sum is $1801$. In the second example Dima can split the number $101$ into integers $10$ and $1$. Their sum is $11$. Note that it is impossible to split the strip into "1" and "01" since the numbers can't start with zeros.
a = int(input()) b = input() ans = int(b) mid1 = a // 2 mid2 = a // 2 + 1 while mid1 >= 1: if b[mid1] != "0": ans = min(ans, int(b[0:mid1]) + int(b[mid1:])) break mid1 = mid1 - 1 while mid2 < a: if b[mid2] != "0": ans = min(ans, int(b[0:mid2]) + int(b[mid2:])) break mid2 = mid2 + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Dima worked all day and wrote down on a long paper strip his favorite number $n$ consisting of $l$ digits. Unfortunately, the strip turned out to be so long that it didn't fit in the Dima's bookshelf. To solve the issue, Dima decided to split the strip into two non-empty parts so that each of them contains a positive integer without leading zeros. After that he will compute the sum of the two integers and write it down on a new strip. Dima wants the resulting integer to be as small as possible, because it increases the chances that the sum will fit it in the bookshelf. Help Dima decide what is the minimum sum he can obtain. -----Input----- The first line contains a single integer $l$ ($2 \le l \le 100\,000$) — the length of the Dima's favorite number. The second line contains the positive integer $n$ initially written on the strip: the Dima's favorite number. The integer $n$ consists of exactly $l$ digits and it does not contain leading zeros. Dima guarantees, that there is at least one valid way to split the strip. -----Output----- Print a single integer — the smallest number Dima can obtain. -----Examples----- Input 7 1234567 Output 1801 Input 3 101 Output 11 -----Note----- In the first example Dima can split the number $1234567$ into integers $1234$ and $567$. Their sum is $1801$. In the second example Dima can split the number $101$ into integers $10$ and $1$. Their sum is $11$. Note that it is impossible to split the strip into "1" and "01" since the numbers can't start with zeros.
n = int(input()) s = input().strip() mid = n // 2 ans = int(s) if s[mid] != "0": num = int(s[:mid]) + int(s[mid:]) ans = min(ans, num) for i in range(mid + 1, n): if s[i] != "0": num = int(s[:i]) + int(s[i:]) ans = min(ans, num) break for i in range(mid - 1, 0, -1): if s[i] != "0": num = int(s[:i]) + int(s[i:]) ans = min(ans, num) break print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Dima worked all day and wrote down on a long paper strip his favorite number $n$ consisting of $l$ digits. Unfortunately, the strip turned out to be so long that it didn't fit in the Dima's bookshelf. To solve the issue, Dima decided to split the strip into two non-empty parts so that each of them contains a positive integer without leading zeros. After that he will compute the sum of the two integers and write it down on a new strip. Dima wants the resulting integer to be as small as possible, because it increases the chances that the sum will fit it in the bookshelf. Help Dima decide what is the minimum sum he can obtain. -----Input----- The first line contains a single integer $l$ ($2 \le l \le 100\,000$) — the length of the Dima's favorite number. The second line contains the positive integer $n$ initially written on the strip: the Dima's favorite number. The integer $n$ consists of exactly $l$ digits and it does not contain leading zeros. Dima guarantees, that there is at least one valid way to split the strip. -----Output----- Print a single integer — the smallest number Dima can obtain. -----Examples----- Input 7 1234567 Output 1801 Input 3 101 Output 11 -----Note----- In the first example Dima can split the number $1234567$ into integers $1234$ and $567$. Their sum is $1801$. In the second example Dima can split the number $101$ into integers $10$ and $1$. Their sum is $11$. Note that it is impossible to split the strip into "1" and "01" since the numbers can't start with zeros.
n = int(input()) s = input() mid1 = n // 2 if n % 2 == 0: mid1 -= 1 mid2 = mid1 else: mid2 = mid1 mid1 -= 1 while mid1 >= 0 and s[mid1 + 1] == "0": mid1 -= 1 while mid2 + 1 < n and s[mid2 + 1] == "0": mid2 += 1 ans = -1 if 0 <= mid1 < n - 1: a = s[: mid1 + 1] b = s[mid1 + 1 :] if ans == -1: ans = int(a) + int(b) if 0 <= mid2 < n - 1: c = s[: mid2 + 1] d = s[mid2 + 1 :] if ans == -1: ans = int(c) + int(d) elif ans > int(c) + int(d): ans = int(c) + int(d) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER STRING VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR NUMBER IF NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Dima worked all day and wrote down on a long paper strip his favorite number $n$ consisting of $l$ digits. Unfortunately, the strip turned out to be so long that it didn't fit in the Dima's bookshelf. To solve the issue, Dima decided to split the strip into two non-empty parts so that each of them contains a positive integer without leading zeros. After that he will compute the sum of the two integers and write it down on a new strip. Dima wants the resulting integer to be as small as possible, because it increases the chances that the sum will fit it in the bookshelf. Help Dima decide what is the minimum sum he can obtain. -----Input----- The first line contains a single integer $l$ ($2 \le l \le 100\,000$) — the length of the Dima's favorite number. The second line contains the positive integer $n$ initially written on the strip: the Dima's favorite number. The integer $n$ consists of exactly $l$ digits and it does not contain leading zeros. Dima guarantees, that there is at least one valid way to split the strip. -----Output----- Print a single integer — the smallest number Dima can obtain. -----Examples----- Input 7 1234567 Output 1801 Input 3 101 Output 11 -----Note----- In the first example Dima can split the number $1234567$ into integers $1234$ and $567$. Their sum is $1801$. In the second example Dima can split the number $101$ into integers $10$ and $1$. Their sum is $11$. Note that it is impossible to split the strip into "1" and "01" since the numbers can't start with zeros.
import sys def f(s, k): return int(s[:k]) + int(s[k:]) def solve(s, k): if s[k] != "0": return f(s, k) ans = [] i = k while s[i] == "0": i -= 1 if i != 0: ans.append(f(s, i)) j = k while j < len(s) and s[j] == "0": j += 1 if j != len(s): ans.append(f(s, j)) assert ans return min(ans) n = sys.stdin.readline() s = sys.stdin.readline().rstrip() n = len(s) print(min(solve(s, n // 2), solve(s, (n + 1) // 2)))
IMPORT FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR STRING RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR STRING VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER
Dima worked all day and wrote down on a long paper strip his favorite number $n$ consisting of $l$ digits. Unfortunately, the strip turned out to be so long that it didn't fit in the Dima's bookshelf. To solve the issue, Dima decided to split the strip into two non-empty parts so that each of them contains a positive integer without leading zeros. After that he will compute the sum of the two integers and write it down on a new strip. Dima wants the resulting integer to be as small as possible, because it increases the chances that the sum will fit it in the bookshelf. Help Dima decide what is the minimum sum he can obtain. -----Input----- The first line contains a single integer $l$ ($2 \le l \le 100\,000$) — the length of the Dima's favorite number. The second line contains the positive integer $n$ initially written on the strip: the Dima's favorite number. The integer $n$ consists of exactly $l$ digits and it does not contain leading zeros. Dima guarantees, that there is at least one valid way to split the strip. -----Output----- Print a single integer — the smallest number Dima can obtain. -----Examples----- Input 7 1234567 Output 1801 Input 3 101 Output 11 -----Note----- In the first example Dima can split the number $1234567$ into integers $1234$ and $567$. Their sum is $1801$. In the second example Dima can split the number $101$ into integers $10$ and $1$. Their sum is $11$. Note that it is impossible to split the strip into "1" and "01" since the numbers can't start with zeros.
nn = int(input()) n = input() d = list(n) if nn % 2 == 0: p = nn // 2 if n[p] == "0": q = p - 1 while n[p] == "0" and n[q] == "0": p = p + 1 q = q - 1 if n[p] == "0": print(int(n[:q]) + int(n[q:])) else: print(int(n[:p]) + int(n[p:])) else: print(int(n[:p]) + int(n[p:])) else: p = nn // 2 if n[p] == "0": q = p while n[p] == "0" and n[q] == "0": p = p + 1 q = q - 1 if n[p] == "0": print(int(n[:q]) + int(n[q:])) else: print(int(n[:p]) + int(n[p:])) elif n[p] > n[0]: print(int(n[: p + 1]) + int(n[p + 1 :])) elif n[p] == n[0]: if int(n[: p + 1]) > int(n[p:]): print(int(n[:p]) + int(n[p:])) else: print(int(n[: p + 1]) + int(n[p + 1 :])) else: print(int(n[:p]) + int(n[p:]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR STRING VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR WHILE VAR VAR STRING VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR
Dima worked all day and wrote down on a long paper strip his favorite number $n$ consisting of $l$ digits. Unfortunately, the strip turned out to be so long that it didn't fit in the Dima's bookshelf. To solve the issue, Dima decided to split the strip into two non-empty parts so that each of them contains a positive integer without leading zeros. After that he will compute the sum of the two integers and write it down on a new strip. Dima wants the resulting integer to be as small as possible, because it increases the chances that the sum will fit it in the bookshelf. Help Dima decide what is the minimum sum he can obtain. -----Input----- The first line contains a single integer $l$ ($2 \le l \le 100\,000$) — the length of the Dima's favorite number. The second line contains the positive integer $n$ initially written on the strip: the Dima's favorite number. The integer $n$ consists of exactly $l$ digits and it does not contain leading zeros. Dima guarantees, that there is at least one valid way to split the strip. -----Output----- Print a single integer — the smallest number Dima can obtain. -----Examples----- Input 7 1234567 Output 1801 Input 3 101 Output 11 -----Note----- In the first example Dima can split the number $1234567$ into integers $1234$ and $567$. Their sum is $1801$. In the second example Dima can split the number $101$ into integers $10$ and $1$. Their sum is $11$. Note that it is impossible to split the strip into "1" and "01" since the numbers can't start with zeros.
def getnumber(A, a): return int(int(A[a + 1 :]) + int(A[: a + 1])) n = int(input()) A = input() k = n // 2 - 1 if n % 2 == 0: a = k if A[k + 1] == "0": while A[k + 1] == "0" and k < n - 2: k += 1 while A[a + 1] == "0" and a >= 0: a -= 1 if A[k + 1] != "0" and a != -1: print(min(getnumber(A, k), getnumber(A, a))) elif A[k + 1] != "0" and a == 0: print(getnumber(A, k)) elif A[k + 1] == "0" and a != -1: print(getnumber(A, a)) else: print(A) else: print(getnumber(A, k)) else: k = k + 1 if A[k + 1] != "0" and A[k] != "0": print(min(getnumber(A, k), getnumber(A, k - 1))) elif A[k + 1] != "0": print(getnumber(A, k)) elif A[k] != "0": print(getnumber(A, k - 1)) else: a = k - 1 while A[k + 1] == "0" and k < n - 2: k += 1 while A[a + 1] == "0" and a >= 0: a -= 1 if A[k + 1] != "0" and a != -1: print(min(getnumber(A, k), getnumber(A, a))) elif A[k + 1] != "0" and a == -1: print(getnumber(A, k)) elif A[k + 1] == "0" and a != -1: print(getnumber(A, a)) else: print(A)
FUNC_DEF RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER STRING WHILE VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Dima worked all day and wrote down on a long paper strip his favorite number $n$ consisting of $l$ digits. Unfortunately, the strip turned out to be so long that it didn't fit in the Dima's bookshelf. To solve the issue, Dima decided to split the strip into two non-empty parts so that each of them contains a positive integer without leading zeros. After that he will compute the sum of the two integers and write it down on a new strip. Dima wants the resulting integer to be as small as possible, because it increases the chances that the sum will fit it in the bookshelf. Help Dima decide what is the minimum sum he can obtain. -----Input----- The first line contains a single integer $l$ ($2 \le l \le 100\,000$) — the length of the Dima's favorite number. The second line contains the positive integer $n$ initially written on the strip: the Dima's favorite number. The integer $n$ consists of exactly $l$ digits and it does not contain leading zeros. Dima guarantees, that there is at least one valid way to split the strip. -----Output----- Print a single integer — the smallest number Dima can obtain. -----Examples----- Input 7 1234567 Output 1801 Input 3 101 Output 11 -----Note----- In the first example Dima can split the number $1234567$ into integers $1234$ and $567$. Their sum is $1801$. In the second example Dima can split the number $101$ into integers $10$ and $1$. Their sum is $11$. Note that it is impossible to split the strip into "1" and "01" since the numbers can't start with zeros.
n = int(input()) v = input() c = n // 2 while c > 0 and v[c] == "0": c -= 1 d = n // 2 + 1 while d < n - 1 and v[d] == "0": d += 1 a = int(v[:d]) + int(v[d:]) if v[d] == "0": a = 10**100100 if c == 0: c = d b = int(v[:c]) + int(v[c:]) print(min(a, b))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Dima worked all day and wrote down on a long paper strip his favorite number $n$ consisting of $l$ digits. Unfortunately, the strip turned out to be so long that it didn't fit in the Dima's bookshelf. To solve the issue, Dima decided to split the strip into two non-empty parts so that each of them contains a positive integer without leading zeros. After that he will compute the sum of the two integers and write it down on a new strip. Dima wants the resulting integer to be as small as possible, because it increases the chances that the sum will fit it in the bookshelf. Help Dima decide what is the minimum sum he can obtain. -----Input----- The first line contains a single integer $l$ ($2 \le l \le 100\,000$) — the length of the Dima's favorite number. The second line contains the positive integer $n$ initially written on the strip: the Dima's favorite number. The integer $n$ consists of exactly $l$ digits and it does not contain leading zeros. Dima guarantees, that there is at least one valid way to split the strip. -----Output----- Print a single integer — the smallest number Dima can obtain. -----Examples----- Input 7 1234567 Output 1801 Input 3 101 Output 11 -----Note----- In the first example Dima can split the number $1234567$ into integers $1234$ and $567$. Their sum is $1801$. In the second example Dima can split the number $101$ into integers $10$ and $1$. Their sum is $11$. Note that it is impossible to split the strip into "1" and "01" since the numbers can't start with zeros.
n = int(input()) s = input() if n % 2: for i in range(int(n / 2 + 1)): a = None b = None ind1 = int(n / 2 - i) ind2 = int(n / 2 + 1 + i) if s[ind1] != "0" and s[ind2] == "0": print(int(s[0:ind1]) + int(s[ind1:])) break elif s[ind2] != "0" and s[ind1] == "0": print(int(s[0:ind2]) + int(s[ind2:])) break elif s[ind1] != "0" and s[ind2] != "0": x = min(int(s[0:ind1]) + int(s[ind1:]), int(s[0:ind2]) + int(s[ind2:])) print(x) break pass else: for i in range(int(n / 2 + 1)): a = None b = None ind1 = int(n / 2 - i) ind2 = int(n / 2 + i) if s[ind1] != "0" and s[ind2] == "0": print(int(s[0:ind1]) + int(s[ind1:])) break elif s[ind2] != "0" and s[ind1] == "0": print(int(s[0:ind2]) + int(s[ind2:])) break elif s[ind1] != "0" and s[ind2] != "0": x = min(int(s[0:ind1]) + int(s[ind1:]), int(s[0:ind2]) + int(s[ind2:])) print(x) break pass
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR IF VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Dima worked all day and wrote down on a long paper strip his favorite number $n$ consisting of $l$ digits. Unfortunately, the strip turned out to be so long that it didn't fit in the Dima's bookshelf. To solve the issue, Dima decided to split the strip into two non-empty parts so that each of them contains a positive integer without leading zeros. After that he will compute the sum of the two integers and write it down on a new strip. Dima wants the resulting integer to be as small as possible, because it increases the chances that the sum will fit it in the bookshelf. Help Dima decide what is the minimum sum he can obtain. -----Input----- The first line contains a single integer $l$ ($2 \le l \le 100\,000$) — the length of the Dima's favorite number. The second line contains the positive integer $n$ initially written on the strip: the Dima's favorite number. The integer $n$ consists of exactly $l$ digits and it does not contain leading zeros. Dima guarantees, that there is at least one valid way to split the strip. -----Output----- Print a single integer — the smallest number Dima can obtain. -----Examples----- Input 7 1234567 Output 1801 Input 3 101 Output 11 -----Note----- In the first example Dima can split the number $1234567$ into integers $1234$ and $567$. Their sum is $1801$. In the second example Dima can split the number $101$ into integers $10$ and $1$. Their sum is $11$. Note that it is impossible to split the strip into "1" and "01" since the numbers can't start with zeros.
n = int(input()) s = input() ans = int(s) for k, i in ((1, n // 2), (-1, (n + 1) // 2)): while i < n and "1" > s[i]: i += k if 0 < i < n: ans = min(ans, int(s[:i]) + int(s[i:])) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR VAR STRING VAR VAR VAR VAR IF NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Dima worked all day and wrote down on a long paper strip his favorite number $n$ consisting of $l$ digits. Unfortunately, the strip turned out to be so long that it didn't fit in the Dima's bookshelf. To solve the issue, Dima decided to split the strip into two non-empty parts so that each of them contains a positive integer without leading zeros. After that he will compute the sum of the two integers and write it down on a new strip. Dima wants the resulting integer to be as small as possible, because it increases the chances that the sum will fit it in the bookshelf. Help Dima decide what is the minimum sum he can obtain. -----Input----- The first line contains a single integer $l$ ($2 \le l \le 100\,000$) — the length of the Dima's favorite number. The second line contains the positive integer $n$ initially written on the strip: the Dima's favorite number. The integer $n$ consists of exactly $l$ digits and it does not contain leading zeros. Dima guarantees, that there is at least one valid way to split the strip. -----Output----- Print a single integer — the smallest number Dima can obtain. -----Examples----- Input 7 1234567 Output 1801 Input 3 101 Output 11 -----Note----- In the first example Dima can split the number $1234567$ into integers $1234$ and $567$. Their sum is $1801$. In the second example Dima can split the number $101$ into integers $10$ and $1$. Their sum is $11$. Note that it is impossible to split the strip into "1" and "01" since the numbers can't start with zeros.
l = int(input()) n = input() if l % 2 == 0: res1, res2 = n[: l // 2], n[l // 2 :] i, j = l // 2 - 1, 0 else: a, b = n[: l // 2 + 1], n[l // 2 :] if int(a) < int(b): res1, res2 = a, n[l // 2 + 1 :] i, j = l // 2, 0 else: res1, res2 = n[: l // 2], b i, j = l // 2 - 1, 0 x1, y1, x2, y2 = res1, res2, res1, res2 for u in range(l // 2): if res1[i] == "0": x1 = x1[:-1] y1 = "0" + y1 i -= 1 if res2[j] == "0": x2 += "0" y2 = y2[1:] j += 1 if y1[0] == "0": a = x1[i] x1, y1 = x1[:-1], a + y1 if x1 == "": print(int(x2) + int(y2)) elif y2 == "": print(int(x1) + int(y1)) else: print(min(int(x2) + int(y2), int(x1) + int(y1)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP STRING VAR VAR NUMBER IF VAR VAR STRING VAR STRING ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR IF VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Dima worked all day and wrote down on a long paper strip his favorite number $n$ consisting of $l$ digits. Unfortunately, the strip turned out to be so long that it didn't fit in the Dima's bookshelf. To solve the issue, Dima decided to split the strip into two non-empty parts so that each of them contains a positive integer without leading zeros. After that he will compute the sum of the two integers and write it down on a new strip. Dima wants the resulting integer to be as small as possible, because it increases the chances that the sum will fit it in the bookshelf. Help Dima decide what is the minimum sum he can obtain. -----Input----- The first line contains a single integer $l$ ($2 \le l \le 100\,000$) — the length of the Dima's favorite number. The second line contains the positive integer $n$ initially written on the strip: the Dima's favorite number. The integer $n$ consists of exactly $l$ digits and it does not contain leading zeros. Dima guarantees, that there is at least one valid way to split the strip. -----Output----- Print a single integer — the smallest number Dima can obtain. -----Examples----- Input 7 1234567 Output 1801 Input 3 101 Output 11 -----Note----- In the first example Dima can split the number $1234567$ into integers $1234$ and $567$. Their sum is $1801$. In the second example Dima can split the number $101$ into integers $10$ and $1$. Their sum is $11$. Note that it is impossible to split the strip into "1" and "01" since the numbers can't start with zeros.
length = int(input()) n = input() if length == 2: Bestans = int(n[0]) + int(n[1]) print(Bestans) else: bestAns = int(n) p = length // 2 for i in range(p, -1, -1): if n[i + 1] == "0": continue bestAns = min(bestAns, int(n[0 : i + 1]) + int(n[i + 1 :])) break p = length // 2 for i in range(p, length): if n[i] == "0": continue bestAns = min(bestAns, int(n[:i]) + int(n[i:])) break print(bestAns)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Dima worked all day and wrote down on a long paper strip his favorite number $n$ consisting of $l$ digits. Unfortunately, the strip turned out to be so long that it didn't fit in the Dima's bookshelf. To solve the issue, Dima decided to split the strip into two non-empty parts so that each of them contains a positive integer without leading zeros. After that he will compute the sum of the two integers and write it down on a new strip. Dima wants the resulting integer to be as small as possible, because it increases the chances that the sum will fit it in the bookshelf. Help Dima decide what is the minimum sum he can obtain. -----Input----- The first line contains a single integer $l$ ($2 \le l \le 100\,000$) — the length of the Dima's favorite number. The second line contains the positive integer $n$ initially written on the strip: the Dima's favorite number. The integer $n$ consists of exactly $l$ digits and it does not contain leading zeros. Dima guarantees, that there is at least one valid way to split the strip. -----Output----- Print a single integer — the smallest number Dima can obtain. -----Examples----- Input 7 1234567 Output 1801 Input 3 101 Output 11 -----Note----- In the first example Dima can split the number $1234567$ into integers $1234$ and $567$. Their sum is $1801$. In the second example Dima can split the number $101$ into integers $10$ and $1$. Their sum is $11$. Note that it is impossible to split the strip into "1" and "01" since the numbers can't start with zeros.
n = int(input()) s = input() p = [i for i in range(n) if s[i] != "0"][1:] p = sorted(p, key=lambda x: abs(x - n / 2)) minn = int(s) for i in range(min(4, len(p))): minn = min(minn, int(s[: p[i]]) + int(s[p[i] :])) print(minn)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR