description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You have $n$ sticks of the given lengths. Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks. Let $S$ be the area of the rectangle and $P$ be the perimeter of the rectangle. The chosen rectangle should have the value $\frac{P^2}{S}$ minimal possible. The value is taken without any rounding. If there are multiple answers, print any of them. Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($T \ge 1$) β€” the number of lists of sticks in the testcase. Then $2T$ lines follow β€” lines $(2i - 1)$ and $2i$ of them describe the $i$-th list. The first line of the pair contains a single integer $n$ ($4 \le n \le 10^6$) β€” the number of sticks in the $i$-th list. The second line of the pair contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_j \le 10^4$) β€” lengths of the sticks in the $i$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $T$ lists doesn't exceed $10^6$ in each testcase. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th list of the input. That is the lengths of the four sticks you choose from the $i$-th list, so that they form a rectangle and the value $\frac{P^2}{S}$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. -----Example----- Input 3 4 7 2 2 7 8 2 8 1 4 8 2 1 5 5 5 5 5 5 5 Output 2 7 7 2 2 2 1 1 5 5 5 5 -----Note----- There is only one way to choose four sticks in the first list, they form a rectangle with sides $2$ and $7$, its area is $2 \cdot 7 = 14$, perimeter is $2(2 + 7) = 18$. $\frac{18^2}{14} \approx 23.143$. The second list contains subsets of four sticks that can form rectangles with sides $(1, 2)$, $(2, 8)$ and $(1, 8)$. Their values are $\frac{6^2}{2} = 18$, $\frac{20^2}{16} = 25$ and $\frac{18^2}{8} = 40.5$, respectively. The minimal one of them is the rectangle $(1, 2)$. You can choose any four of the $5$ given sticks from the third list, they will form a square with side $5$, which is still a rectangle with sides $(5, 5)$.
import sys f = sys.stdin out = sys.stdout def fun(l, b): return (2 * (l + b)) ** 2 / (l * b) t = int(f.readline().rstrip("\r\n")) for _ in range(t): n = int(f.readline().rstrip("\r\n")) a = list(map(int, f.readline().rstrip("\r\n").split())) d = {} arr = [] for i in a: if i in d: d[i] += 1 else: d[i] = 1 arr.append(i) arr.sort() mi = float("inf") tmp = -1 l1, b1 = -1, -1 for i in range(len(arr)): if tmp == -1: if d[arr[i]] >= 4: tmp = arr[i] pmi = mi mi = min(mi, fun(arr[i], arr[i])) if pmi != mi: l1, b1 = arr[i], arr[i] elif d[arr[i]] >= 2: tmp = arr[i] elif d[arr[i]] >= 4: tmp = arr[i] pmi = mi mi = min(mi, fun(arr[i], arr[i])) if pmi != mi: l1, b1 = arr[i], arr[i] elif d[arr[i]] >= 2: pmi = mi mi = min(mi, fun(tmp, arr[i])) if pmi != mi: l1, b1 = tmp, arr[i] tmp = arr[i] out.write(str(l1) + " " + str(l1) + " " + str(b1) + " " + str(b1) + "\n")
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING
You have $n$ sticks of the given lengths. Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks. Let $S$ be the area of the rectangle and $P$ be the perimeter of the rectangle. The chosen rectangle should have the value $\frac{P^2}{S}$ minimal possible. The value is taken without any rounding. If there are multiple answers, print any of them. Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($T \ge 1$) β€” the number of lists of sticks in the testcase. Then $2T$ lines follow β€” lines $(2i - 1)$ and $2i$ of them describe the $i$-th list. The first line of the pair contains a single integer $n$ ($4 \le n \le 10^6$) β€” the number of sticks in the $i$-th list. The second line of the pair contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_j \le 10^4$) β€” lengths of the sticks in the $i$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $T$ lists doesn't exceed $10^6$ in each testcase. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th list of the input. That is the lengths of the four sticks you choose from the $i$-th list, so that they form a rectangle and the value $\frac{P^2}{S}$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. -----Example----- Input 3 4 7 2 2 7 8 2 8 1 4 8 2 1 5 5 5 5 5 5 5 Output 2 7 7 2 2 2 1 1 5 5 5 5 -----Note----- There is only one way to choose four sticks in the first list, they form a rectangle with sides $2$ and $7$, its area is $2 \cdot 7 = 14$, perimeter is $2(2 + 7) = 18$. $\frac{18^2}{14} \approx 23.143$. The second list contains subsets of four sticks that can form rectangles with sides $(1, 2)$, $(2, 8)$ and $(1, 8)$. Their values are $\frac{6^2}{2} = 18$, $\frac{20^2}{16} = 25$ and $\frac{18^2}{8} = 40.5$, respectively. The minimal one of them is the rectangle $(1, 2)$. You can choose any four of the $5$ given sticks from the third list, they will form a square with side $5$, which is still a rectangle with sides $(5, 5)$.
super_ans = [] for i in range(int(input())): n = input() if n == "4": super_ans.append(input()) continue elif n == "5": a = sorted(map(int, input().split())) if a[0] == a[1] and a[2] == a[3]: super_ans.append(" ".join([str(a[0]), str(a[0]), str(a[2]), str(a[2])])) elif a[0] == a[1] and a[3] == a[4]: super_ans.append(" ".join([str(a[0]), str(a[0]), str(a[3]), str(a[3])])) else: super_ans.append(" ".join([str(a[-1]), str(a[-1]), str(a[-3]), str(a[-3])])) continue d = 10**9 ans = [] t = tc = x = y = 0 for ai in sorted(map(int, input().split())): if t == ai: tc += 1 if tc == 2: x = y y = ai if x and y / x < d: d = y / x ans = [x, y] elif tc == 4: ans = [t, t] break else: t = ai tc = 1 super_ans.append(" ".join(map(str, ans * 2))) print("\n".join(super_ans))
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING LIST FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING LIST FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING LIST FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST VAR VAR IF VAR NUMBER ASSIGN VAR LIST VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You have $n$ sticks of the given lengths. Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks. Let $S$ be the area of the rectangle and $P$ be the perimeter of the rectangle. The chosen rectangle should have the value $\frac{P^2}{S}$ minimal possible. The value is taken without any rounding. If there are multiple answers, print any of them. Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($T \ge 1$) β€” the number of lists of sticks in the testcase. Then $2T$ lines follow β€” lines $(2i - 1)$ and $2i$ of them describe the $i$-th list. The first line of the pair contains a single integer $n$ ($4 \le n \le 10^6$) β€” the number of sticks in the $i$-th list. The second line of the pair contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_j \le 10^4$) β€” lengths of the sticks in the $i$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $T$ lists doesn't exceed $10^6$ in each testcase. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th list of the input. That is the lengths of the four sticks you choose from the $i$-th list, so that they form a rectangle and the value $\frac{P^2}{S}$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. -----Example----- Input 3 4 7 2 2 7 8 2 8 1 4 8 2 1 5 5 5 5 5 5 5 Output 2 7 7 2 2 2 1 1 5 5 5 5 -----Note----- There is only one way to choose four sticks in the first list, they form a rectangle with sides $2$ and $7$, its area is $2 \cdot 7 = 14$, perimeter is $2(2 + 7) = 18$. $\frac{18^2}{14} \approx 23.143$. The second list contains subsets of four sticks that can form rectangles with sides $(1, 2)$, $(2, 8)$ and $(1, 8)$. Their values are $\frac{6^2}{2} = 18$, $\frac{20^2}{16} = 25$ and $\frac{18^2}{8} = 40.5$, respectively. The minimal one of them is the rectangle $(1, 2)$. You can choose any four of the $5$ given sticks from the third list, they will form a square with side $5$, which is still a rectangle with sides $(5, 5)$.
import sys read = sys.stdin.buffer.read input = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines t = int(input()) for case in range(t): n = int(input()) a = list(map(int, input().split())) a.sort() tmp = 10**4 ans = 10**9 i = 0 rec = 0, 0 break_bit = 0 while i < n - 1: cnt = 2 if a[i] == a[i + 1]: if ans > a[i] / tmp + tmp / a[i]: rec = tmp, a[i] ans = a[i] / tmp + tmp / a[i] while i + 2 < n and a[i + 1] == a[i + 2]: cnt += 1 i += 1 if cnt == 4: rec = a[i], a[i] break_bit = 1 break tmp = a[i] if break_bit == 1: break i += 1 print(rec[0], rec[0], rec[1], rec[1])
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR WHILE BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER
You have $n$ sticks of the given lengths. Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks. Let $S$ be the area of the rectangle and $P$ be the perimeter of the rectangle. The chosen rectangle should have the value $\frac{P^2}{S}$ minimal possible. The value is taken without any rounding. If there are multiple answers, print any of them. Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($T \ge 1$) β€” the number of lists of sticks in the testcase. Then $2T$ lines follow β€” lines $(2i - 1)$ and $2i$ of them describe the $i$-th list. The first line of the pair contains a single integer $n$ ($4 \le n \le 10^6$) β€” the number of sticks in the $i$-th list. The second line of the pair contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_j \le 10^4$) β€” lengths of the sticks in the $i$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $T$ lists doesn't exceed $10^6$ in each testcase. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th list of the input. That is the lengths of the four sticks you choose from the $i$-th list, so that they form a rectangle and the value $\frac{P^2}{S}$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. -----Example----- Input 3 4 7 2 2 7 8 2 8 1 4 8 2 1 5 5 5 5 5 5 5 Output 2 7 7 2 2 2 1 1 5 5 5 5 -----Note----- There is only one way to choose four sticks in the first list, they form a rectangle with sides $2$ and $7$, its area is $2 \cdot 7 = 14$, perimeter is $2(2 + 7) = 18$. $\frac{18^2}{14} \approx 23.143$. The second list contains subsets of four sticks that can form rectangles with sides $(1, 2)$, $(2, 8)$ and $(1, 8)$. Their values are $\frac{6^2}{2} = 18$, $\frac{20^2}{16} = 25$ and $\frac{18^2}{8} = 40.5$, respectively. The minimal one of them is the rectangle $(1, 2)$. You can choose any four of the $5$ given sticks from the third list, they will form a square with side $5$, which is still a rectangle with sides $(5, 5)$.
import sys input = sys.stdin.readline T = int(input()) for i in range(T): n = int(input()) d = {} lens = [] nums = list(map(int, input().strip().split())) for num in nums: d[num] = d.get(num, 0) + 1 if d[num] == 2: lens.append(num) d[num] = 0 lens = sorted(lens) a, b = lens[0], lens[1] mmin = float("inf") for i in range(1, len(lens)): if lens[i] / lens[i - 1] < mmin: mmin = lens[i] / lens[i - 1] a = lens[i] b = lens[i - 1] print(a, a, b, b)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR
You have $n$ sticks of the given lengths. Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks. Let $S$ be the area of the rectangle and $P$ be the perimeter of the rectangle. The chosen rectangle should have the value $\frac{P^2}{S}$ minimal possible. The value is taken without any rounding. If there are multiple answers, print any of them. Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($T \ge 1$) β€” the number of lists of sticks in the testcase. Then $2T$ lines follow β€” lines $(2i - 1)$ and $2i$ of them describe the $i$-th list. The first line of the pair contains a single integer $n$ ($4 \le n \le 10^6$) β€” the number of sticks in the $i$-th list. The second line of the pair contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_j \le 10^4$) β€” lengths of the sticks in the $i$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $T$ lists doesn't exceed $10^6$ in each testcase. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th list of the input. That is the lengths of the four sticks you choose from the $i$-th list, so that they form a rectangle and the value $\frac{P^2}{S}$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. -----Example----- Input 3 4 7 2 2 7 8 2 8 1 4 8 2 1 5 5 5 5 5 5 5 Output 2 7 7 2 2 2 1 1 5 5 5 5 -----Note----- There is only one way to choose four sticks in the first list, they form a rectangle with sides $2$ and $7$, its area is $2 \cdot 7 = 14$, perimeter is $2(2 + 7) = 18$. $\frac{18^2}{14} \approx 23.143$. The second list contains subsets of four sticks that can form rectangles with sides $(1, 2)$, $(2, 8)$ and $(1, 8)$. Their values are $\frac{6^2}{2} = 18$, $\frac{20^2}{16} = 25$ and $\frac{18^2}{8} = 40.5$, respectively. The minimal one of them is the rectangle $(1, 2)$. You can choose any four of the $5$ given sticks from the third list, they will form a square with side $5$, which is still a rectangle with sides $(5, 5)$.
from sys import stdin, stdout def read(): return stdin.readline().rstrip("\n") def read_array(sep=None, maxsplit=-1): return read().split(sep, maxsplit) def read_int(): return int(read()) def read_int_array(sep=None, maxsplit=-1): return [int(a) for a in read_array(sep, maxsplit)] def write(*args, **kwargs): sep = kwargs.get("sep", " ") end = kwargs.get("end", "\n") stdout.write(sep.join(str(a) for a in args) + end) def write_array(array, **kwargs): sep = kwargs.get("sep", " ") end = kwargs.get("end", "\n") stdout.write(sep.join(str(a) for a in array) + end) def f(a, b): return a / b + b / a n = read_int() results = [] for _ in range(n): read_int() arr = sorted(read_int_array(sep=" ")) i = 0 len_arr_minus_one = len(arr) - 1 prev_el, next_el = None, None min_sticks_el = None min_sticks = None while i < len_arr_minus_one: if arr[i] == arr[i + 1]: prev_el, next_el = next_el, arr[i] i += 2 if prev_el and next_el: if not min_sticks_el: min_sticks_el = prev_el, next_el min_sticks = f(prev_el, next_el) continue current_value = f(prev_el, next_el) if min_sticks > current_value: min_sticks = current_value min_sticks_el = prev_el, next_el else: i += 1 results.append("{0} {0} {1} {1}".format(min_sticks_el[0], min_sticks_el[1])) print("\n".join(results))
FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF NONE NUMBER RETURN FUNC_CALL FUNC_CALL VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF NONE NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NONE NONE ASSIGN VAR NONE ASSIGN VAR NONE WHILE VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You have $n$ sticks of the given lengths. Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks. Let $S$ be the area of the rectangle and $P$ be the perimeter of the rectangle. The chosen rectangle should have the value $\frac{P^2}{S}$ minimal possible. The value is taken without any rounding. If there are multiple answers, print any of them. Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($T \ge 1$) β€” the number of lists of sticks in the testcase. Then $2T$ lines follow β€” lines $(2i - 1)$ and $2i$ of them describe the $i$-th list. The first line of the pair contains a single integer $n$ ($4 \le n \le 10^6$) β€” the number of sticks in the $i$-th list. The second line of the pair contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_j \le 10^4$) β€” lengths of the sticks in the $i$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $T$ lists doesn't exceed $10^6$ in each testcase. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th list of the input. That is the lengths of the four sticks you choose from the $i$-th list, so that they form a rectangle and the value $\frac{P^2}{S}$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. -----Example----- Input 3 4 7 2 2 7 8 2 8 1 4 8 2 1 5 5 5 5 5 5 5 Output 2 7 7 2 2 2 1 1 5 5 5 5 -----Note----- There is only one way to choose four sticks in the first list, they form a rectangle with sides $2$ and $7$, its area is $2 \cdot 7 = 14$, perimeter is $2(2 + 7) = 18$. $\frac{18^2}{14} \approx 23.143$. The second list contains subsets of four sticks that can form rectangles with sides $(1, 2)$, $(2, 8)$ and $(1, 8)$. Their values are $\frac{6^2}{2} = 18$, $\frac{20^2}{16} = 25$ and $\frac{18^2}{8} = 40.5$, respectively. The minimal one of them is the rectangle $(1, 2)$. You can choose any four of the $5$ given sticks from the third list, they will form a square with side $5$, which is still a rectangle with sides $(5, 5)$.
import sys read = lambda: sys.stdin.readline().strip() prcs = lambda x, y: (x + y) ** 2 / (x * y) n = int(read()) for i in range(0, n): t = int(read()) a = list(map(int, read().split())) a.sort() dub = [] ind = 0 while ind < t - 1: if a[ind] == a[ind + 1]: dub.append(a[ind]) ind += 1 ind += 1 mn = prcs(dub[0], dub[1]) index = 0 for j in range(0, len(dub) - 1): if mn > prcs(dub[j], dub[j + 1]): mn = prcs(dub[j], dub[j + 1]) index = j print(dub[index], dub[index], dub[index + 1], dub[index + 1])
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER
You have $n$ sticks of the given lengths. Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks. Let $S$ be the area of the rectangle and $P$ be the perimeter of the rectangle. The chosen rectangle should have the value $\frac{P^2}{S}$ minimal possible. The value is taken without any rounding. If there are multiple answers, print any of them. Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($T \ge 1$) β€” the number of lists of sticks in the testcase. Then $2T$ lines follow β€” lines $(2i - 1)$ and $2i$ of them describe the $i$-th list. The first line of the pair contains a single integer $n$ ($4 \le n \le 10^6$) β€” the number of sticks in the $i$-th list. The second line of the pair contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_j \le 10^4$) β€” lengths of the sticks in the $i$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $T$ lists doesn't exceed $10^6$ in each testcase. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th list of the input. That is the lengths of the four sticks you choose from the $i$-th list, so that they form a rectangle and the value $\frac{P^2}{S}$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. -----Example----- Input 3 4 7 2 2 7 8 2 8 1 4 8 2 1 5 5 5 5 5 5 5 Output 2 7 7 2 2 2 1 1 5 5 5 5 -----Note----- There is only one way to choose four sticks in the first list, they form a rectangle with sides $2$ and $7$, its area is $2 \cdot 7 = 14$, perimeter is $2(2 + 7) = 18$. $\frac{18^2}{14} \approx 23.143$. The second list contains subsets of four sticks that can form rectangles with sides $(1, 2)$, $(2, 8)$ and $(1, 8)$. Their values are $\frac{6^2}{2} = 18$, $\frac{20^2}{16} = 25$ and $\frac{18^2}{8} = 40.5$, respectively. The minimal one of them is the rectangle $(1, 2)$. You can choose any four of the $5$ given sticks from the third list, they will form a square with side $5$, which is still a rectangle with sides $(5, 5)$.
from sys import stdin, stdout T = int(stdin.readline()) ans = [] for cc in range(0, T): n = int(stdin.readline()) a = [int(x) for x in stdin.readline().split()] a.sort() a.append(1061109567) res = [0, 0, 1, 1] pre = 0 cnt = 0 for i in a: if i == pre: cnt += 1 else: if cnt >= 4: res = [pre, pre, pre, pre] break cnt = 1 pre = i if res[0] == 0: pre = 0 val = 0 for i in a: if i == pre and i != val: if val * res[-1] > res[0] * i: res = [val, val, i, i] val = i pre = i ans.append(" ".join([str(x) for x in res])) stdout.write("\n".join(ans) + "\n")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR LIST VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING VAR STRING
You have $n$ sticks of the given lengths. Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks. Let $S$ be the area of the rectangle and $P$ be the perimeter of the rectangle. The chosen rectangle should have the value $\frac{P^2}{S}$ minimal possible. The value is taken without any rounding. If there are multiple answers, print any of them. Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($T \ge 1$) β€” the number of lists of sticks in the testcase. Then $2T$ lines follow β€” lines $(2i - 1)$ and $2i$ of them describe the $i$-th list. The first line of the pair contains a single integer $n$ ($4 \le n \le 10^6$) β€” the number of sticks in the $i$-th list. The second line of the pair contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_j \le 10^4$) β€” lengths of the sticks in the $i$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $T$ lists doesn't exceed $10^6$ in each testcase. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th list of the input. That is the lengths of the four sticks you choose from the $i$-th list, so that they form a rectangle and the value $\frac{P^2}{S}$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. -----Example----- Input 3 4 7 2 2 7 8 2 8 1 4 8 2 1 5 5 5 5 5 5 5 Output 2 7 7 2 2 2 1 1 5 5 5 5 -----Note----- There is only one way to choose four sticks in the first list, they form a rectangle with sides $2$ and $7$, its area is $2 \cdot 7 = 14$, perimeter is $2(2 + 7) = 18$. $\frac{18^2}{14} \approx 23.143$. The second list contains subsets of four sticks that can form rectangles with sides $(1, 2)$, $(2, 8)$ and $(1, 8)$. Their values are $\frac{6^2}{2} = 18$, $\frac{20^2}{16} = 25$ and $\frac{18^2}{8} = 40.5$, respectively. The minimal one of them is the rectangle $(1, 2)$. You can choose any four of the $5$ given sticks from the third list, they will form a square with side $5$, which is still a rectangle with sides $(5, 5)$.
queries = int(input()) output = [] for i in range(queries): numCount = input() if numCount == "4": output.append(input()) else: if numCount == "5": l = sorted(map(int, input().split())) x, y = l[1], l[3] else: lastNum = b = x = y = 0 d = 10**12 for num in sorted(map(int, input().split())): if num == lastNum: count += 1 if count == 2: a = b b = num if a and b / a < d: d = b / a x = a y = b elif count == 4: x = y = num break else: lastNum = num count = 1 output.append(str(x) + " " + str(x) + " " + str(y) + " " + str(y)) print("\n".join(output))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You have $n$ sticks of the given lengths. Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks. Let $S$ be the area of the rectangle and $P$ be the perimeter of the rectangle. The chosen rectangle should have the value $\frac{P^2}{S}$ minimal possible. The value is taken without any rounding. If there are multiple answers, print any of them. Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($T \ge 1$) β€” the number of lists of sticks in the testcase. Then $2T$ lines follow β€” lines $(2i - 1)$ and $2i$ of them describe the $i$-th list. The first line of the pair contains a single integer $n$ ($4 \le n \le 10^6$) β€” the number of sticks in the $i$-th list. The second line of the pair contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_j \le 10^4$) β€” lengths of the sticks in the $i$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $T$ lists doesn't exceed $10^6$ in each testcase. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th list of the input. That is the lengths of the four sticks you choose from the $i$-th list, so that they form a rectangle and the value $\frac{P^2}{S}$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. -----Example----- Input 3 4 7 2 2 7 8 2 8 1 4 8 2 1 5 5 5 5 5 5 5 Output 2 7 7 2 2 2 1 1 5 5 5 5 -----Note----- There is only one way to choose four sticks in the first list, they form a rectangle with sides $2$ and $7$, its area is $2 \cdot 7 = 14$, perimeter is $2(2 + 7) = 18$. $\frac{18^2}{14} \approx 23.143$. The second list contains subsets of four sticks that can form rectangles with sides $(1, 2)$, $(2, 8)$ and $(1, 8)$. Their values are $\frac{6^2}{2} = 18$, $\frac{20^2}{16} = 25$ and $\frac{18^2}{8} = 40.5$, respectively. The minimal one of them is the rectangle $(1, 2)$. You can choose any four of the $5$ given sticks from the third list, they will form a square with side $5$, which is still a rectangle with sides $(5, 5)$.
import sys T = int(sys.stdin.readline().rstrip()) for i in range(T): n = int(sys.stdin.readline().rstrip()) a = list(map(int, sys.stdin.readline().rstrip().split())) rec = [] a = sorted(a) k = 0 while k < n - 1: if a[k] == a[k + 1]: rec.append(a[k]) k += 1 k += 1 ans1, ans2 = 1, 1e18 for j in range(1, len(rec)): if ((ans1 + ans2) * 2) ** 2 / (ans1 * ans2) > ( (rec[j] + rec[j - 1]) * 2 ) ** 2 / (rec[j] * rec[j - 1]): ans1, ans2 = rec[j], rec[j - 1] print(ans1, ans2, ans1, ans2)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR
You have $n$ sticks of the given lengths. Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks. Let $S$ be the area of the rectangle and $P$ be the perimeter of the rectangle. The chosen rectangle should have the value $\frac{P^2}{S}$ minimal possible. The value is taken without any rounding. If there are multiple answers, print any of them. Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($T \ge 1$) β€” the number of lists of sticks in the testcase. Then $2T$ lines follow β€” lines $(2i - 1)$ and $2i$ of them describe the $i$-th list. The first line of the pair contains a single integer $n$ ($4 \le n \le 10^6$) β€” the number of sticks in the $i$-th list. The second line of the pair contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_j \le 10^4$) β€” lengths of the sticks in the $i$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $T$ lists doesn't exceed $10^6$ in each testcase. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th list of the input. That is the lengths of the four sticks you choose from the $i$-th list, so that they form a rectangle and the value $\frac{P^2}{S}$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. -----Example----- Input 3 4 7 2 2 7 8 2 8 1 4 8 2 1 5 5 5 5 5 5 5 Output 2 7 7 2 2 2 1 1 5 5 5 5 -----Note----- There is only one way to choose four sticks in the first list, they form a rectangle with sides $2$ and $7$, its area is $2 \cdot 7 = 14$, perimeter is $2(2 + 7) = 18$. $\frac{18^2}{14} \approx 23.143$. The second list contains subsets of four sticks that can form rectangles with sides $(1, 2)$, $(2, 8)$ and $(1, 8)$. Their values are $\frac{6^2}{2} = 18$, $\frac{20^2}{16} = 25$ and $\frac{18^2}{8} = 40.5$, respectively. The minimal one of them is the rectangle $(1, 2)$. You can choose any four of the $5$ given sticks from the third list, they will form a square with side $5$, which is still a rectangle with sides $(5, 5)$.
import sys input = sys.stdin.readline t = int(input()) for i in range(t): n = int(input()) arr = list(map(int, input().split())) freq = {} for item in arr: if item in freq: freq[item] += 1 else: freq[item] = 1 sticks = [] for item in arr: if freq[item] >= 2: sticks.append(item) freq[item] -= 2 sticks.sort() ans = float("inf") lens = len(sticks) pair = [-1, -1, -1, -1] for idx, i in enumerate(sticks): if idx == 0: res = (2 * (i + sticks[idx + 1])) ** 2 / (i * sticks[idx + 1]) if res < ans: pair[0] = i pair[1] = sticks[idx + 1] pair[2] = i pair[3] = sticks[idx + 1] ans = res elif idx == lens - 1: res = (2 * (i + sticks[idx - 1])) ** 2 / (i * sticks[idx - 1]) if res < ans: pair[0] = i pair[1] = sticks[idx - 1] pair[2] = i pair[3] = sticks[idx - 1] ans = res else: res = (2 * (i + sticks[idx + 1])) ** 2 / (i * sticks[idx + 1]) if res < ans: pair[0] = i pair[1] = sticks[idx + 1] pair[2] = i pair[3] = sticks[idx + 1] ans = res res = (2 * (i + sticks[idx - 1])) ** 2 / (i * sticks[idx - 1]) if res < ans: pair[0] = i pair[1] = sticks[idx - 1] pair[2] = i pair[3] = sticks[idx - 1] ans = res print(*pair)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You have $n$ sticks of the given lengths. Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks. Let $S$ be the area of the rectangle and $P$ be the perimeter of the rectangle. The chosen rectangle should have the value $\frac{P^2}{S}$ minimal possible. The value is taken without any rounding. If there are multiple answers, print any of them. Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($T \ge 1$) β€” the number of lists of sticks in the testcase. Then $2T$ lines follow β€” lines $(2i - 1)$ and $2i$ of them describe the $i$-th list. The first line of the pair contains a single integer $n$ ($4 \le n \le 10^6$) β€” the number of sticks in the $i$-th list. The second line of the pair contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_j \le 10^4$) β€” lengths of the sticks in the $i$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $T$ lists doesn't exceed $10^6$ in each testcase. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th list of the input. That is the lengths of the four sticks you choose from the $i$-th list, so that they form a rectangle and the value $\frac{P^2}{S}$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. -----Example----- Input 3 4 7 2 2 7 8 2 8 1 4 8 2 1 5 5 5 5 5 5 5 Output 2 7 7 2 2 2 1 1 5 5 5 5 -----Note----- There is only one way to choose four sticks in the first list, they form a rectangle with sides $2$ and $7$, its area is $2 \cdot 7 = 14$, perimeter is $2(2 + 7) = 18$. $\frac{18^2}{14} \approx 23.143$. The second list contains subsets of four sticks that can form rectangles with sides $(1, 2)$, $(2, 8)$ and $(1, 8)$. Their values are $\frac{6^2}{2} = 18$, $\frac{20^2}{16} = 25$ and $\frac{18^2}{8} = 40.5$, respectively. The minimal one of them is the rectangle $(1, 2)$. You can choose any four of the $5$ given sticks from the third list, they will form a square with side $5$, which is still a rectangle with sides $(5, 5)$.
import sys rd = lambda: sys.stdin.readline().rstrip() t = int(rd()) for _ in range(t): n = int(rd()) a = list(map(int, rd().split())) b = [] res_a, res_b = 1, 1e18 a = sorted(a) i = 0 while i < n - 1: if a[i] == a[i + 1]: b.append(a[i]) i += 1 i += 1 p2s = lambda x, y: (x + y) ** 2 / (x * y) for i in range(len(b) - 1): if p2s(res_a, res_b) > p2s(b[i], b[i + 1]): res_a, res_b = b[i], b[i + 1] print(res_a, res_a, res_b, res_b)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR
You have $n$ sticks of the given lengths. Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks. Let $S$ be the area of the rectangle and $P$ be the perimeter of the rectangle. The chosen rectangle should have the value $\frac{P^2}{S}$ minimal possible. The value is taken without any rounding. If there are multiple answers, print any of them. Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($T \ge 1$) β€” the number of lists of sticks in the testcase. Then $2T$ lines follow β€” lines $(2i - 1)$ and $2i$ of them describe the $i$-th list. The first line of the pair contains a single integer $n$ ($4 \le n \le 10^6$) β€” the number of sticks in the $i$-th list. The second line of the pair contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_j \le 10^4$) β€” lengths of the sticks in the $i$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $T$ lists doesn't exceed $10^6$ in each testcase. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th list of the input. That is the lengths of the four sticks you choose from the $i$-th list, so that they form a rectangle and the value $\frac{P^2}{S}$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. -----Example----- Input 3 4 7 2 2 7 8 2 8 1 4 8 2 1 5 5 5 5 5 5 5 Output 2 7 7 2 2 2 1 1 5 5 5 5 -----Note----- There is only one way to choose four sticks in the first list, they form a rectangle with sides $2$ and $7$, its area is $2 \cdot 7 = 14$, perimeter is $2(2 + 7) = 18$. $\frac{18^2}{14} \approx 23.143$. The second list contains subsets of four sticks that can form rectangles with sides $(1, 2)$, $(2, 8)$ and $(1, 8)$. Their values are $\frac{6^2}{2} = 18$, $\frac{20^2}{16} = 25$ and $\frac{18^2}{8} = 40.5$, respectively. The minimal one of them is the rectangle $(1, 2)$. You can choose any four of the $5$ given sticks from the third list, they will form a square with side $5$, which is still a rectangle with sides $(5, 5)$.
from sys import stdin, stdout def read(): return stdin.readline().rstrip("\n") def f(a, b): return a / b + b / a results = [] for _ in range(int(read())): read() arr = sorted(map(int, read().split())) i = 0 len_arr_minus_one = len(arr) - 1 prev_el, next_el = None, None min_sticks_el = None min_sticks = None while i < len_arr_minus_one: if arr[i] == arr[i + 1]: prev_el, next_el = next_el, arr[i] i += 2 if prev_el and next_el: if not min_sticks_el: min_sticks_el = prev_el, next_el min_sticks = f(prev_el, next_el) continue current_value = f(prev_el, next_el) if min_sticks > current_value: min_sticks = current_value min_sticks_el = prev_el, next_el else: i += 1 results.append("{0} {0} {1} {1}".format(min_sticks_el[0], min_sticks_el[1])) stdout.write("\n".join(results) + "\n")
FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NONE NONE ASSIGN VAR NONE ASSIGN VAR NONE WHILE VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING VAR STRING
You have $n$ sticks of the given lengths. Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks. Let $S$ be the area of the rectangle and $P$ be the perimeter of the rectangle. The chosen rectangle should have the value $\frac{P^2}{S}$ minimal possible. The value is taken without any rounding. If there are multiple answers, print any of them. Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($T \ge 1$) β€” the number of lists of sticks in the testcase. Then $2T$ lines follow β€” lines $(2i - 1)$ and $2i$ of them describe the $i$-th list. The first line of the pair contains a single integer $n$ ($4 \le n \le 10^6$) β€” the number of sticks in the $i$-th list. The second line of the pair contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_j \le 10^4$) β€” lengths of the sticks in the $i$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $T$ lists doesn't exceed $10^6$ in each testcase. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th list of the input. That is the lengths of the four sticks you choose from the $i$-th list, so that they form a rectangle and the value $\frac{P^2}{S}$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. -----Example----- Input 3 4 7 2 2 7 8 2 8 1 4 8 2 1 5 5 5 5 5 5 5 Output 2 7 7 2 2 2 1 1 5 5 5 5 -----Note----- There is only one way to choose four sticks in the first list, they form a rectangle with sides $2$ and $7$, its area is $2 \cdot 7 = 14$, perimeter is $2(2 + 7) = 18$. $\frac{18^2}{14} \approx 23.143$. The second list contains subsets of four sticks that can form rectangles with sides $(1, 2)$, $(2, 8)$ and $(1, 8)$. Their values are $\frac{6^2}{2} = 18$, $\frac{20^2}{16} = 25$ and $\frac{18^2}{8} = 40.5$, respectively. The minimal one of them is the rectangle $(1, 2)$. You can choose any four of the $5$ given sticks from the third list, they will form a square with side $5$, which is still a rectangle with sides $(5, 5)$.
from sys import stdin def main(): input() l = stdin.read().splitlines()[1::2] for i, s in enumerate(l): cnt, x, n = [], 0.0, 0 for y in sorted(map(float, s.split())): if x == y: n += 1 else: if n: cnt.append((x, n)) x, n = y, 0 if n: cnt.append((y, n)) x = t = 0.0 for y, n in cnt: if n > 2: u = v = y break else: z = x / y if t < z: t, u, v = z, x, y x = y l[i] = "%d %d %d %d" % (u, u, v, v) print("\n".join(l)) main()
FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR IF VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP STRING VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
You have $n$ sticks of the given lengths. Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks. Let $S$ be the area of the rectangle and $P$ be the perimeter of the rectangle. The chosen rectangle should have the value $\frac{P^2}{S}$ minimal possible. The value is taken without any rounding. If there are multiple answers, print any of them. Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($T \ge 1$) β€” the number of lists of sticks in the testcase. Then $2T$ lines follow β€” lines $(2i - 1)$ and $2i$ of them describe the $i$-th list. The first line of the pair contains a single integer $n$ ($4 \le n \le 10^6$) β€” the number of sticks in the $i$-th list. The second line of the pair contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_j \le 10^4$) β€” lengths of the sticks in the $i$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $T$ lists doesn't exceed $10^6$ in each testcase. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th list of the input. That is the lengths of the four sticks you choose from the $i$-th list, so that they form a rectangle and the value $\frac{P^2}{S}$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. -----Example----- Input 3 4 7 2 2 7 8 2 8 1 4 8 2 1 5 5 5 5 5 5 5 Output 2 7 7 2 2 2 1 1 5 5 5 5 -----Note----- There is only one way to choose four sticks in the first list, they form a rectangle with sides $2$ and $7$, its area is $2 \cdot 7 = 14$, perimeter is $2(2 + 7) = 18$. $\frac{18^2}{14} \approx 23.143$. The second list contains subsets of four sticks that can form rectangles with sides $(1, 2)$, $(2, 8)$ and $(1, 8)$. Their values are $\frac{6^2}{2} = 18$, $\frac{20^2}{16} = 25$ and $\frac{18^2}{8} = 40.5$, respectively. The minimal one of them is the rectangle $(1, 2)$. You can choose any four of the $5$ given sticks from the third list, they will form a square with side $5$, which is still a rectangle with sides $(5, 5)$.
import sys input = sys.stdin.readline t = int(input()) for i in range(t): n = int(input()) b = list(map(int, input().split())) d = dict() c = [] for j in range(n): if b[j] in d.keys(): d[b[j]] += 1 if d[b[j]] % 2 == 0: c.append(b[j]) else: d[b[j]] = 1 c.sort() j = 0 m = 10000000000 while j < len(c) - 1: e = c[j + 1] / c[j] + c[j] / c[j + 1] if e < m: m = e p = c[j] q = c[j + 1] j += 1 print(p, p, q, q)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR
You have $n$ sticks of the given lengths. Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks. Let $S$ be the area of the rectangle and $P$ be the perimeter of the rectangle. The chosen rectangle should have the value $\frac{P^2}{S}$ minimal possible. The value is taken without any rounding. If there are multiple answers, print any of them. Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($T \ge 1$) β€” the number of lists of sticks in the testcase. Then $2T$ lines follow β€” lines $(2i - 1)$ and $2i$ of them describe the $i$-th list. The first line of the pair contains a single integer $n$ ($4 \le n \le 10^6$) β€” the number of sticks in the $i$-th list. The second line of the pair contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_j \le 10^4$) β€” lengths of the sticks in the $i$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $T$ lists doesn't exceed $10^6$ in each testcase. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th list of the input. That is the lengths of the four sticks you choose from the $i$-th list, so that they form a rectangle and the value $\frac{P^2}{S}$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. -----Example----- Input 3 4 7 2 2 7 8 2 8 1 4 8 2 1 5 5 5 5 5 5 5 Output 2 7 7 2 2 2 1 1 5 5 5 5 -----Note----- There is only one way to choose four sticks in the first list, they form a rectangle with sides $2$ and $7$, its area is $2 \cdot 7 = 14$, perimeter is $2(2 + 7) = 18$. $\frac{18^2}{14} \approx 23.143$. The second list contains subsets of four sticks that can form rectangles with sides $(1, 2)$, $(2, 8)$ and $(1, 8)$. Their values are $\frac{6^2}{2} = 18$, $\frac{20^2}{16} = 25$ and $\frac{18^2}{8} = 40.5$, respectively. The minimal one of them is the rectangle $(1, 2)$. You can choose any four of the $5$ given sticks from the third list, they will form a square with side $5$, which is still a rectangle with sides $(5, 5)$.
import sys t = int(sys.stdin.readline()) arrz = [] for l in range(t): n = int(sys.stdin.readline()) arr = list(map(int, sys.stdin.readline().split())) arr.sort() flag = 0 arr2 = [] dict1 = {} for i in range(n): try: dict1[arr[i]] += 1 if dict1[arr[i]] == 2: arr2.append(arr[i]) dict1[arr[i]] = 0 except: KeyError dict1[arr[i]] = 1 fans = arr2[1] / arr2[0] index = 0 for i in range(len(arr2) - 1): if arr2[i + 1] / arr2[i] < fans: fans = arr2[i + 1] / arr2[i] index = i arrz.append( "%d %d %d %d" % (arr2[index], arr2[index], arr2[index + 1], arr2[index + 1]) ) print("\n".join(arrz))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You have $n$ sticks of the given lengths. Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks. Let $S$ be the area of the rectangle and $P$ be the perimeter of the rectangle. The chosen rectangle should have the value $\frac{P^2}{S}$ minimal possible. The value is taken without any rounding. If there are multiple answers, print any of them. Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($T \ge 1$) β€” the number of lists of sticks in the testcase. Then $2T$ lines follow β€” lines $(2i - 1)$ and $2i$ of them describe the $i$-th list. The first line of the pair contains a single integer $n$ ($4 \le n \le 10^6$) β€” the number of sticks in the $i$-th list. The second line of the pair contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_j \le 10^4$) β€” lengths of the sticks in the $i$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $T$ lists doesn't exceed $10^6$ in each testcase. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th list of the input. That is the lengths of the four sticks you choose from the $i$-th list, so that they form a rectangle and the value $\frac{P^2}{S}$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. -----Example----- Input 3 4 7 2 2 7 8 2 8 1 4 8 2 1 5 5 5 5 5 5 5 Output 2 7 7 2 2 2 1 1 5 5 5 5 -----Note----- There is only one way to choose four sticks in the first list, they form a rectangle with sides $2$ and $7$, its area is $2 \cdot 7 = 14$, perimeter is $2(2 + 7) = 18$. $\frac{18^2}{14} \approx 23.143$. The second list contains subsets of four sticks that can form rectangles with sides $(1, 2)$, $(2, 8)$ and $(1, 8)$. Their values are $\frac{6^2}{2} = 18$, $\frac{20^2}{16} = 25$ and $\frac{18^2}{8} = 40.5$, respectively. The minimal one of them is the rectangle $(1, 2)$. You can choose any four of the $5$ given sticks from the third list, they will form a square with side $5$, which is still a rectangle with sides $(5, 5)$.
import sys input = sys.stdin.readline out = sys.stdout t = int(input()) for i in range(t): n = int(input()) a = list(map(int, input().split())) if len(set(a)) == 1: print(a[0], a[0], a[0], a[0]) else: a.sort() g1 = False d = {} mx = 10001 for i in a: if i not in d.keys(): d[i] = 1 else: d[i] += 1 if d[i] == 4: g1 = True if i < mx: mx = i if g1: out.write(str(mx) + " " + str(mx) + " " + str(mx) + " " + str(mx) + "\n") else: res = [] for k in d.keys(): if d[k] >= 2: res.append(k) m = len(res) minj = 0 for j in range(m - 1): if res[j] * res[j + 1] * (res[minj] ** 2 + res[minj + 1] ** 2) > res[ minj ] * res[minj + 1] * (res[j] ** 2 + res[j + 1] ** 2): minj = j out.write( str(res[minj]) + " " + str(res[minj]) + " " + str(res[minj + 1]) + " " + str(res[minj + 1]) + "\n" )
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR STRING FUNC_CALL VAR VAR VAR STRING FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING
You have $n$ sticks of the given lengths. Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks. Let $S$ be the area of the rectangle and $P$ be the perimeter of the rectangle. The chosen rectangle should have the value $\frac{P^2}{S}$ minimal possible. The value is taken without any rounding. If there are multiple answers, print any of them. Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($T \ge 1$) β€” the number of lists of sticks in the testcase. Then $2T$ lines follow β€” lines $(2i - 1)$ and $2i$ of them describe the $i$-th list. The first line of the pair contains a single integer $n$ ($4 \le n \le 10^6$) β€” the number of sticks in the $i$-th list. The second line of the pair contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_j \le 10^4$) β€” lengths of the sticks in the $i$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $T$ lists doesn't exceed $10^6$ in each testcase. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th list of the input. That is the lengths of the four sticks you choose from the $i$-th list, so that they form a rectangle and the value $\frac{P^2}{S}$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. -----Example----- Input 3 4 7 2 2 7 8 2 8 1 4 8 2 1 5 5 5 5 5 5 5 Output 2 7 7 2 2 2 1 1 5 5 5 5 -----Note----- There is only one way to choose four sticks in the first list, they form a rectangle with sides $2$ and $7$, its area is $2 \cdot 7 = 14$, perimeter is $2(2 + 7) = 18$. $\frac{18^2}{14} \approx 23.143$. The second list contains subsets of four sticks that can form rectangles with sides $(1, 2)$, $(2, 8)$ and $(1, 8)$. Their values are $\frac{6^2}{2} = 18$, $\frac{20^2}{16} = 25$ and $\frac{18^2}{8} = 40.5$, respectively. The minimal one of them is the rectangle $(1, 2)$. You can choose any four of the $5$ given sticks from the third list, they will form a square with side $5$, which is still a rectangle with sides $(5, 5)$.
ans = [] def main(): t = int(input()) for l in range(t): n = int(input()) if n == 4: ans.append(input()) else: b = y = num = x = 0 d = 10**10 for i in sorted(map(int, input().split())): if num == i: c += 1 if c == 2: a = b b = i if a != 0 and a / b + b / a < d: d = a / b + b / a x = a y = b elif c == 4: x = y = i break else: num = i c = 1 ans.append(str(x) + " " + str(y) + " " + str(x) + " " + str(y)) return ans print("\n".join(main()))
ASSIGN VAR LIST FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
for t in range(int(input())): n, k = map(int, input().split()) no = n // 2 if no <= k: print(n * (n - 1) // 2) elif n == 1: print(0) else: ans = n * (n - 1) // 2 r = n - 2 * k se = r * (r - 1) // 2 ans = ans - se print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
def ans(n, k): if k >= n // 2: return n * (n - 1) // 2 else: not_swapped = n - 2 * k return n * (n - 1) // 2 - not_swapped * (not_swapped - 1) // 2 test_cases = int(input()) while test_cases != 0: d = list(map(int, input().split())) print(ans(d[0], d[1])) test_cases -= 1
FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
for t in range(int(input())): n, k = map(int, input().split()) mid = n // 2 if n == 1: print(0) elif mid < k: print(n * (n - 1) // 2) else: p = n - 2 * k t = n * (n - 1) // 2 - p * (p - 1) // 2 print(t)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
n = int(input()) for i in range(n): N, K = map(int, input().split(" ")) if K >= N // 2: print(N * (N - 1) // 2) else: print(K * N - K * (K + 1) // 2 + (N - K * 2) * K + (K - 1) * K // 2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
def sum_upto_n(n): return n * (n + 1) // 2 for _ in range(int(input())): n, k = map(int, input().strip().split()) k = min(k, n // 2) q1 = sum_upto_n(n - 1) - sum_upto_n(n - k - 1) q2 = (n - 2 * k) * k q3 = sum_upto_n(k - 1) print(q1 + q2 + q3)
FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
T = int(input()) for i in range(0, T): temp = input().split() N = int(temp[0]) K = int(temp[1]) var2 = 0 if K >= int(N // 2): var2 = (N - 1) * N // 2 else: var2 = -2 * K**2 + 2 * (K * N) - K print(int(var2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
T = int(input()) for x in range(T): N, K = map(int, input().split()) if K >= N // 2: print(int(N * (N - 1) // 2)) else: print(K * (2 * (N - K) - 1))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR NUMBER
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
for _ in range(int(input())): n, b = map(int, input().split()) if n // 2 <= b: s = (n - 1) * n // 2 print(s) else: s = ( (n - 1 - b * 2) * b + b * (b + 1) // 2 + (n - 1) * n // 2 - (n - b - 1) * (n - b) // 2 ) print(s)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
t = input() for c in range(0, int(t)): length, moves = map(int, input().split()) ans = length * (length - 1) // 2 if moves < length // 2: noswap = length - 2 * moves nopairs = noswap * (noswap - 1) // 2 ans = ans - nopairs print(ans)
ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
for _ in range(int(input())): n, k = map(int, input().split()) m = n // 2 if n == 1: print(0) elif m <= k: a = n * (n - 1) // 2 print(a) else: b = n * (n - 1) // 2 c = n - 2 * k c = c * (c - 1) // 2 print(b - c)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
for i in range(int(input())): a, b = map(int, input().split()) x = a * (a - 1) // 2 if b > a // 2: print(x) else: c = a - b * 2 y = c * (c - 1) // 2 print(x - y)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
for _ in range(int(input())): n, k = map(int, input().split()) if k >= n // 2: print(n * (n - 1) // 2) else: val = k * (k - 1) // 2 val += (n - 2 * k) * k req = n - k - 1 temp = n * (n - 1) // 2 - req * (req + 1) // 2 val += temp print(val)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
for _ in range(int(input())): x, y = map(int, input().split()) f = x * (x - 1) // 2 h = 0 if y < x / 2: h = x - 2 * y f = f - h * (h - 1) // 2 print(f)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
T = int(input()) for t in range(T): temp = input().split() N = int(temp[0]) K = int(temp[1]) if N == 1: print(0) elif K >= N // 2: print((N - 1) * N // 2) else: s = -2 * K * K + 2 * N * K - K print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
T = int(input()) for t in range(T): s = input().split() n = int(s[0]) k = int(s[1]) if n == 1: print(0) elif n == 2 and k % 2 == 0: print(0) elif n == 2 and k % 2 != 0: print(1) elif k <= n // 2: ans = n * k - k + (n - 2 * k) * k print(ans) else: k = n // 2 ans = n * k - k + (n - 2 * k) * k print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
for tc in range(int(input())): n, k = map(int, input().split()) if n == 1: print(0) elif n // 2 <= k: print(n * (n - 1) // 2) else: tot = 0 x = n - k tot += n * (n - 1) // 2 - x * (x - 1) // 2 con = n - 2 * k tot += k * con tot += k * (k - 1) // 2 print(tot)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
t = int(input()) for _ in range(0, t): n, k = map(int, input().split()) if n == 1: print(0) continue if n < 2 * k: k = n // 2 print((2 * (n - k) - 1) * k)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR VAR NUMBER VAR
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
def sigma(n): s = n * (n + 1) // 2 return s T = int(input()) for tc in range(T): s = 0 n, k = map(int, input().split(" ")) if k >= n // 2: print(sigma(n - 1)) else: s += sigma(n - 1) - sigma(n - k - 1) s += sigma(k - 1) s += (n - 2 * k) * k print(s)
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
for _ in range(int(input())): n, k = map(int, input().split()) k = min(k, n // 2) ans = (n - 1) * k + (n - 2 * k) * k print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
t = int(input()) for _ in range(t): n, k = list(map(int, input().split())) if n == 1: print(0) elif k >= n // 2: print(n * (n - 1) // 2) else: print((2 * n - 2 * k - 1) * k)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR NUMBER VAR
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
for i in range(int(input())): n, k = map(int, input().split()) sum1 = 0 if k < n // 2: j = n - k sum1 += n * (n - 1) // 2 - j * (j - 1) // 2 sum1 += (n - k - k) * k sum1 += k * (k - 1) // 2 else: sum1 = n * (n - 1) // 2 print(sum1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
tc = int(input()) for i in range(tc): arr = input().split(" ") n = int(arr[0]) k = int(arr[1]) if 2 * k >= n: print((n - 1) * n // 2) else: print(2 * n * k - 2 * k * k - k)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
for i in range(int(input())): n, k = map(int, input().split()) k = min(k, n // 2) print( n * (n - 1) // 2 - (n - k) * (n - k - 1) // 2 + (k - 1) * k // 2 + (n - 2 * k) * k )
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
def func(n): return n * (n + 1) // 2 for _ in range(int(input())): ans = 0 n, k = list(map(int, input().split())) m = n // 2 if k >= m: print(func(n - 1)) else: ans = func(n) - func(n - k) - k me = n - 2 * k ans += me * k ans += func(k - 1) print(ans)
FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
for i in range(int(input())): n, k = map(int, input().split()) if k >= n // 2: print(n * (n - 1) // 2) else: print(k * (2 * n - 3 - (k - 1) * 2))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
for _ in range(int(input())): sum1 = 0 n, k = map(int, input().split()) p = n t = n - k if k >= n // 2: print(n * (n - 1) // 2) else: sum1 += p * (p - 1) // 2 - t * (t - 1) // 2 sum1 += k * (p - 2 * k) + k * (k - 1) // 2 print(sum1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
T = int(input()) for i in range(T): N, K = map(int, input().split()) if N == 1: print(0) else: p = N - min(K * 2, N - 1) - 1 x = 0 x = N - 1 print(x * (x + 1) // 2 - p * (p + 1) // 2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
T = int(input()) while T: T -= 1 n, k = list(map(int, input().split())) if n == 1 or k == 0: print(0) elif k >= n // 2: print(n * (n - 1) // 2) else: j = k * (k * 2 - 1) l = n - k * 2 print(j + l * k * 2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
def solve(): n, k = map(int, input().split()) if k > n // 2: k = n // 2 print(k * (2 * n - 2 * k - 1)) for _ in range(int(input())): solve()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) xn = n // 2 if k >= xn: print(n * (n - 1) // 2) else: a = n * (n - 1) // 2 x = n - 2 * k - 1 b = x * (x + 1) // 2 print(a - b)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
def summ(n): return n * (n + 1) // 2 for _ in range(int(input())): n, k = map(int, input().split()) step = min(n // 2, k) if n % 2 == 0: rem = (n // 2 - step) * 2 - 1 else: rem = (n // 2 - step) * 2 ang = summ(n - 1) - summ(rem) print(ang)
FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
numberofcases = int(input()) def sum(n): return n * (n + 1) / 2 for case in range(numberofcases): inputs = input().split(" ") pieces = int(inputs[0]) swaps = int(inputs[1]) swapp = min(swaps, pieces // 2) if pieces >= 2: print(swapp * (pieces + pieces - 2 * swapp + 2) - 3 * swapp) else: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR NUMBER
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
def getSummation(n): return (n - 1) * (n - 1 + 1) // 2 def getSummation1(n, k): ans = n - 1 itr = ans - 1 for _ in range(k - 1): ans += itr itr -= 1 return ans t = int(input()) while t: n, k = map(int, input().split()) if k == 0: print(0) t -= 1 continue if k >= n // 2: print(getSummation(n)) else: print(getSummation(n) - getSummation(n - k) + (n - k * 2) * k + getSummation(k)) t -= 1
FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
for x in range(int(input())): n, k = map(int, input().split()) if k > n // 2: print(int(n * (n - 1) // 2)) else: a = n * (n - 1) // 2 - (n - k - 1) * (n - k) // 2 print(a + (n - 2 * k) * k + k * (k - 1) // 2)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER
Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≀ i < j ≀ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{9}$ $1 ≀ K ≀ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
for i in range(int(input())): a, b = map(int, input().split()) c = a // 2 s = 0 if b >= c: c = a - 1 s = c * (c + 1) // 2 else: s = -2 * b**2 + 2 * (b * a) - b print(s)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Everything got unclear to us in a far away constellation Tau Ceti. Specifically, the Taucetians choose names to their children in a very peculiar manner. Two young parents abac and bbad think what name to give to their first-born child. They decided that the name will be the permutation of letters of string s. To keep up with the neighbours, they decided to call the baby so that the name was lexicographically strictly larger than the neighbour's son's name t. On the other hand, they suspect that a name tax will be introduced shortly. According to it, the Taucetians with lexicographically larger names will pay larger taxes. That's the reason abac and bbad want to call the newborn so that the name was lexicographically strictly larger than name t and lexicographically minimum at that. The lexicographical order of strings is the order we are all used to, the "dictionary" order. Such comparison is used in all modern programming languages to compare strings. Formally, a string p of length n is lexicographically less than string q of length m, if one of the two statements is correct: * n < m, and p is the beginning (prefix) of string q (for example, "aba" is less than string "abaa"), * p1 = q1, p2 = q2, ..., pk - 1 = qk - 1, pk < qk for some k (1 ≀ k ≀ min(n, m)), here characters in strings are numbered starting from 1. Write a program that, given string s and the heighbours' child's name t determines the string that is the result of permutation of letters in s. The string should be lexicographically strictly more than t and also, lexicographically minimum. Input The first line contains a non-empty string s (1 ≀ |s| ≀ 5000), where |s| is its length. The second line contains a non-empty string t (1 ≀ |t| ≀ 5000), where |t| is its length. Both strings consist of lowercase Latin letters. Output Print the sought name or -1 if it doesn't exist. Examples Input aad aac Output aad Input abad bob Output daab Input abc defg Output -1 Input czaaab abcdef Output abczaa Note In the first sample the given string s is the sought one, consequently, we do not need to change the letter order there.
def findmin(lcopy, toexceed): toex = ord(toexceed) - 97 for each in lcopy[toex + 1 :]: if each > 0: return True return False def arrange(lcopy, toexceed=None): if toexceed is None: ans = "" for i in range(26): ans += chr(i + 97) * lcopy[i] return ans ans = "" for i in range(ord(toexceed) - 97 + 1, 26): if lcopy[i] > 0: ans += chr(i + 97) lcopy[i] -= 1 break return ans + arrange(lcopy) def operation(s1, s2): first_count = [0] * 26 for letter in s1: first_count[ord(letter) - 97] += 1 common = 0 lcopy = list(first_count) for i in range(len(s2)): letter = s2[i] num = ord(letter) - 97 if lcopy[num] > 0: lcopy[num] -= 1 common += 1 else: break found = False ans = "" for cval in range(common, -1, -1): if cval >= len(s1): lcopy[ord(s2[cval - 1]) - 97] += 1 continue elif cval == len(s2): found = True ans = s2[:cval] + arrange(lcopy) break elif findmin(lcopy, s2[cval]): found = True ans = s2[:cval] + arrange(lcopy, s2[cval]) break else: lcopy[ord(s2[cval - 1]) - 97] += 1 if not found: return -1 else: return ans s1 = input() s2 = input() print(operation(s1, s2))
FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF NONE IF VAR NONE ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR RETURN NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Everything got unclear to us in a far away constellation Tau Ceti. Specifically, the Taucetians choose names to their children in a very peculiar manner. Two young parents abac and bbad think what name to give to their first-born child. They decided that the name will be the permutation of letters of string s. To keep up with the neighbours, they decided to call the baby so that the name was lexicographically strictly larger than the neighbour's son's name t. On the other hand, they suspect that a name tax will be introduced shortly. According to it, the Taucetians with lexicographically larger names will pay larger taxes. That's the reason abac and bbad want to call the newborn so that the name was lexicographically strictly larger than name t and lexicographically minimum at that. The lexicographical order of strings is the order we are all used to, the "dictionary" order. Such comparison is used in all modern programming languages to compare strings. Formally, a string p of length n is lexicographically less than string q of length m, if one of the two statements is correct: * n < m, and p is the beginning (prefix) of string q (for example, "aba" is less than string "abaa"), * p1 = q1, p2 = q2, ..., pk - 1 = qk - 1, pk < qk for some k (1 ≀ k ≀ min(n, m)), here characters in strings are numbered starting from 1. Write a program that, given string s and the heighbours' child's name t determines the string that is the result of permutation of letters in s. The string should be lexicographically strictly more than t and also, lexicographically minimum. Input The first line contains a non-empty string s (1 ≀ |s| ≀ 5000), where |s| is its length. The second line contains a non-empty string t (1 ≀ |t| ≀ 5000), where |t| is its length. Both strings consist of lowercase Latin letters. Output Print the sought name or -1 if it doesn't exist. Examples Input aad aac Output aad Input abad bob Output daab Input abc defg Output -1 Input czaaab abcdef Output abczaa Note In the first sample the given string s is the sought one, consequently, we do not need to change the letter order there.
from sys import stdin, stdout words = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", ] s = stdin.readline().strip() f = stdin.readline().strip() ans = -1 d = {} for v in s: if v in d: d[v] += 1 else: d[v] = 1 for i in range(min(len(s), len(f))): if f[i] in d and d[f[i]]: d[f[i]] -= 1 else: break for v in words: if v in d and d[v] and (i == len(f) - 1 or v > f[i + 1]): ans = i break if ans == -1 and max(list(s)) > f[0]: s = sorted(list(s)) first = "" for i in range(len(s)): if s[i] > f[0]: first += s[i] s[i] = "_" break first += "".join(sorted(list(s))[1:]) elif ans == -1: first = "-1" else: s = sorted(list(s)) d = {} first = "" for i in range(ans + 1): first += f[i] if f[i] in d: d[f[i]] += 1 else: d[f[i]] = 1 for i in range(len(s)): if s[i] in d and d[s[i]]: d[s[i]] -= 1 s[i] = "_" for i in range(len(s)): if s[i] != "_" and (ans + 1 == len(f) or s[i] > f[ans + 1]): first += s[i] s[i] = "_" break for i in range(len(s)): if s[i] != "_": first += s[i] stdout.write(first)
ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR STRING VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING 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 = input().split() a = int(n) b = int(m) count = 0 while b > a: if b % 2 == 1: count = count + 1 b = b + 1 else: b = b // 2 count = count + 1 count = count + (a - b) print(count)
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER 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 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 minimum_push(n, m): if n >= m: return n - m if m % 2 == 0: return 1 + minimum_push(n, m // 2) else: return 1 + minimum_push(n, m + 1) n, m = map(int, input().split()) print(minimum_push(n, m))
FUNC_DEF IF VAR VAR RETURN BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER 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.
start, end = [int(x) for x in input().split()] times = 0 while True: if start == end: break elif start > end: times += start - end break elif end % 2 == 0: end = int(end / 2) times += 1 else: end += 1 times += 1 print(times)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR IF VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n. Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result? -----Input----- The first and the only line of the input contains two distinct integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- Input 4 6 Output 2 Input 10 1 Output 9 -----Note----- In the first example you need to push the blue button once, and then push the red button once. In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.
from sys import stdin, stdout a, b = map(int, stdin.readline().split()) if a > b: print(a - b) elif a < b: c = 0 while a < b: if b % 2 == 0: b = b // 2 c += 1 else: c += 1 b += 1 b = b // 2 c += 1 c += a - b print(c) else: print("0")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF 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 ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
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()) c = 0 while True: if b % 2 == 0 and b > a: c += 1 b = b // 2 else: c += 1 b += 1 if b == a: break print(c)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n. Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result? -----Input----- The first and the only line of the input contains two distinct integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- Input 4 6 Output 2 Input 10 1 Output 9 -----Note----- In the first example you need to push the blue button once, and then push the red button once. In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.
n, m = map(int, input().split()) stepnums = [-1] * 20000 stepnums[n] = 0 queue = [] queue.append(n) while len(queue) > 0: current = queue.pop(0) if current == m: print(stepnums[current]) break if current * 2 < 2 * 10**4 and stepnums[current * 2] == -1: queue.append(current * 2) stepnums[current * 2] = stepnums[current] + 1 if current - 1 >= 0 and stepnums[current - 1] == -1: queue.append(current - 1) stepnums[current - 1] = stepnums[current] + 1
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR 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 BIN_OP NUMBER BIN_OP 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 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
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 Cal_Num(a, b): if b % 2 != 0: aim = b + 1 steps = 1 else: aim = b steps = 0 avg = aim // 2 if a >= avg: steps += a - avg else: steps += Cal_Num(a, avg) return steps + 1 n, m = map(int, input().split()) if n >= m: print(n - m) exit() print(Cal_Num(n, m))
FUNC_DEF IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR 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.
from sys import stdin max_val = int(10000000000000.0) min_val = int(-10000000000000.0) def read_int(): return int(stdin.readline()) def read_ints(): return [int(x) for x in stdin.readline().split()] def read_str(): return input() def read_strs(): return [x for x in stdin.readline().split()] has, need = read_ints() ops = 0 while has != need: ops += 1 if need < has or need % 2 == 1: need += 1 else: need //= 2 print(ops)
ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR 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
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()] if n >= m: ans = n - m else: count = 0 while m != n: if m % 2 != 0 and m > n: m += 1 m /= 2 count += 1 elif m > n and m % 2 == 0: m /= 2 else: m += 1 count += 1 ans = count print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n. Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result? -----Input----- The first and the only line of the input contains two distinct integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- Input 4 6 Output 2 Input 10 1 Output 9 -----Note----- In the first example you need to push the blue button once, and then push the red button once. In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.
n, m = map(int, input().split()) maxn = 30000.0 + 1 lay = [(0) for _ in range(int(30000.0 + 1))] vis = [(0) for _ in range(int(30000.0 + 1))] def bfs(now): cnt, head, tail = 0, 0, 0 que = [(0) for i in range(int(100000.0))] que[tail], tail = now, tail + 1 while tail > head: fa, head = que[head], head + 1 if vis[fa] == 1: continue vis[fa] = 1 if fa == m: return lay[fa] if fa > 0 and fa < 30000.0: if fa - 1 > 0: que[tail], tail, lay[fa - 1] = fa - 1, tail + 1, lay[fa] + 1 if fa * 2 < 30000.0: que[tail], tail, lay[fa * 2] = fa * 2, tail + 1, lay[fa] + 1 return -1 print(bfs(n))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR RETURN VAR VAR IF VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER RETURN 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 = [int(x) for x in input().split()] count = 0 while n != m: if n > m: n -= 1 count += 1 elif m > n: if m % 2 == 0: m = m // 2 count += 1 elif m % 2 == 1: m = m + 1 count += 1 print(count)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n. Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result? -----Input----- The first and the only line of the input contains two distinct integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- Input 4 6 Output 2 Input 10 1 Output 9 -----Note----- In the first example you need to push the blue button once, and then push the red button once. In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.
def twoBotton(n, m): q = [(n, 0)] visited = set() while q: cur, count = q.pop(0) if cur == m: return count if cur * 2 < m * 2 and cur * 2 not in visited: visited.add(cur * 2) q.append((cur * 2, count + 1)) if cur - 1 >= 0 and cur - 1 not in visited: visited.add(cur - 1) q.append((cur - 1, count + 1)) return -1 x, y = input().split() print(twoBotton(int(x), int(y)))
FUNC_DEF ASSIGN VAR LIST VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR VAR RETURN VAR IF BIN_OP VAR NUMBER BIN_OP 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 BIN_OP VAR NUMBER 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 RETURN NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR 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.
n, m = map(int, input().split()) q = [n] b = 1 r = 0 i = 0 used = [False] * (2 * 10**4 + 10) used[n] = True while i < len(q): if i == b: b = len(q) r += 1 if q[i] == m: print(r) break if q[i] > 1: if not used[q[i] - 1]: q.append(q[i] - 1) used[q[i] - 1] = True if q[i] < m: if not used[q[i] * 2]: q.append(q[i] * 2) used[q[i] * 2] = True i += 1
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER 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, m = map(int, input().split()) print( min( i + ((n << i) - m >> i) + bin(((n << i) - m) % (1 << i)).count("1") for i in range(50) if n << i >= m ) )
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP NUMBER VAR STRING VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR
Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n. Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result? -----Input----- The first and the only line of the input contains two distinct integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- Input 4 6 Output 2 Input 10 1 Output 9 -----Note----- In the first example you need to push the blue button once, and then push the red button once. In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.
n, m = map(int, input().strip().split(" ")) if m <= n: print(n - m) else: cont = 1 pas = 0 while m > n: n *= 2 pas += 1 cont *= 2 tot = n - m while tot > 0: pas += tot // cont tot = tot % cont cont /= 2 print(int(pas))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n. Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result? -----Input----- The first and the only line of the input contains two distinct integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- Input 4 6 Output 2 Input 10 1 Output 9 -----Note----- In the first example you need to push the blue button once, and then push the red button once. In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.
a, b = map(int, input().split()) if a >= b: print(a - b) else: op = 0 while b != a: if b < a: op += a - b break if b % 2 != 0: b += 1 else: b = b // 2 op += 1 print(op)
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 VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n. Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result? -----Input----- The first and the only line of the input contains two distinct integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- Input 4 6 Output 2 Input 10 1 Output 9 -----Note----- In the first example you need to push the blue button once, and then push the red button once. In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.
x = list(input().split()) n = int(x[0]) m = int(x[1]) total = 0 z = m while True: while n > z: n -= 1 total += 1 red = 0 while m % 2 == 0 and m > n: m = m / 2 red += 1 total += 1 azul2 = 0 while m % 2 == 1 and m > n: m += 1 azul2 += 1 total += 1 azul = 0 while m < n: m += 1 azul += 1 total += 1 if n == m: break print(total)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n. Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result? -----Input----- The first and the only line of the input contains two distinct integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- Input 4 6 Output 2 Input 10 1 Output 9 -----Note----- In the first example you need to push the blue button once, and then push the red button once. In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.
import sys input = sys.stdin.readline def solve(n, m): if n >= m: return n - m graph = {} for i in range(1, 2 * m + 1): graph[i] = set() for i in graph: if i - 1 in graph: graph[i].add(i - 1) if i * 2 in graph: graph[i].add(i * 2) ans = 0 queue, next_queue, visited = [n], [], set() while True: for i in queue: visited.add(i) if i == m: return ans for j in graph[i]: if j not in visited: next_queue.append(j) queue, next_queue = next_queue, [] ans += 1 n, m = map(int, input().split(" ")) ans = solve(n, m) print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FOR VAR VAR 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 ASSIGN VAR NUMBER ASSIGN VAR VAR VAR LIST VAR LIST FUNC_CALL VAR WHILE NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR LIST VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING 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 process_node(arr, visited, o_visited, rev): curr = arr.pop(0) if curr[0] in visited: return visited[curr[0]] = curr[1] if curr[0] in o_visited: print(o_visited[curr[0]] + visited[curr[0]]) exit() if not rev: arr.append((curr[0] * 2, curr[1] + 1)) arr.append((curr[0] - 1, curr[1] + 1)) else: if curr[0] % 2 == 0: arr.append((curr[0] / 2, curr[1] + 1)) arr.append((curr[0] + 1, curr[1] + 1)) n, m = map(int, input().split()) arr, arr2 = [], [] arr.append((n, 0)) arr2.append((m, 0)) visited = {} visited2 = {} while True: process_node(arr, visited, visited2, False) process_node(arr2, visited2, visited, True)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR RETURN ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT WHILE NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER
Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n. Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result? -----Input----- The first and the only line of the input contains two distinct integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- Input 4 6 Output 2 Input 10 1 Output 9 -----Note----- In the first example you need to push the blue button once, and then push the red button once. In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.
input = input().split(" ") n = int(input[0]) m = int(input[1]) result = 0 while n < m: if m % 2 == 0: m //= 2 else: m += 1 result += 1 if n > m: result += n - m print(result)
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 WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n. Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result? -----Input----- The first and the only line of the input contains two distinct integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- Input 4 6 Output 2 Input 10 1 Output 9 -----Note----- In the first example you need to push the blue button once, and then push the red button once. In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.
s = input() s = s.split() n = int(s[0]) m = int(s[1]) graph = [[] for i in range(0, 20001)] count = 1 for i in graph[1:]: if count <= 10000: i.append(count * 2) if count > 1: i.append(count - 1) count += 1 visited = [(False) for i in range(0, 40004)] stk = [n] dist = 0 while True: dist += 1 stk_temp = [] for i in stk: for j in graph[i]: if j == m: print(dist) quit() elif visited[j] == False: stk_temp.append(j) visited[j] = True stk = stk_temp
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 LIST VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST VAR ASSIGN VAR NUMBER WHILE NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR
Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n. Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result? -----Input----- The first and the only line of the input contains two distinct integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- Input 4 6 Output 2 Input 10 1 Output 9 -----Note----- In the first example you need to push the blue button once, and then push the red button once. In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.
n, m = map(int, input().split()) res = 0 if n >= m: res = n - m elif n < m: while m > n: if m % 2 == 0: m = m // 2 res += 1 elif m % 2 == 1: m = m + 1 res += 1 if m < n: res = res + (n - m) break print(res)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR 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.
n, m = [int(next) for next in input().split()] if m <= n: print(n - m) else: answer = 0 while n < m: if m % 2 == 1: m += 1 answer += 1 m //= 2 answer += 1 if n == m: print(answer) else: print(answer + n - m)
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 IF BIN_OP VAR NUMBER NUMBER VAR 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
Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n. Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result? -----Input----- The first and the only line of the input contains two distinct integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- Input 4 6 Output 2 Input 10 1 Output 9 -----Note----- In the first example you need to push the blue button once, and then push the red button once. In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.
n, m = map(int, input().split()) k = 0 while not m == n: if m < n: m = m + 1 k += 1 continue if m % 2 == 0: m = m // 2 else: m = m + 1 k += 1 print(k)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n. Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result? -----Input----- The first and the only line of the input contains two distinct integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- Input 4 6 Output 2 Input 10 1 Output 9 -----Note----- In the first example you need to push the blue button once, and then push the red button once. In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.
n, m = map(int, input().split()) if n >= m: print(n - m) exit(0) q = [(n, 0)] d = {} while q: x, y = q.pop(0) if x in d: continue d[x] = True if x == m: print(y) break if 2 * x <= 2 * m: q.append((2 * x, y + 1)) if x - 1 > 0: q.append((x - 1, y + 1))
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 LIST VAR NUMBER ASSIGN VAR DICT WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n. Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result? -----Input----- The first and the only line of the input contains two distinct integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- Input 4 6 Output 2 Input 10 1 Output 9 -----Note----- In the first example you need to push the blue button once, and then push the red button once. In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.
n, m = map(int, input().split(" ")) b = 1 r = 2 i = 0 if n >= m: while n > m: n -= b i += 1 elif n < m: while n < m: if m % 2 == 0: m //= 2 else: m += 1 i += 1 while n > m: n -= b i += 1 print(i)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n. Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result? -----Input----- The first and the only line of the input contains two distinct integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- Input 4 6 Output 2 Input 10 1 Output 9 -----Note----- In the first example you need to push the blue button once, and then push the red button once. In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.
n, m = map(int, input().split()) upar = [n] niche = [] steps = 0 br = 0 dict = {n} if n < m: while True: if br == 1: break for i in upar: if i > 0 and i < m * 2: if i - 1 not in dict: niche.append(i - 1) dict.add(i - 1) if niche[-1] == m: br = 1 break if i * 2 not in dict: niche.append(i * 2) dict.add(i * 2) if niche[-1] == m: br = 1 break upar = [] steps += 1 if br == 1: break for i in niche: if i > 0 and i < m * 2: if i - 1 not in dict: upar.append(i - 1) dict.add(i - 1) if upar[-1] == m: br = 1 break if i * 2 not in dict: upar.append(i * 2) dict.add(i * 2) if upar[-1] == m: br = 1 break niche = [] steps += 1 else: steps = n - m print(steps)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR VAR WHILE NUMBER IF VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR NUMBER IF VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n. Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result? -----Input----- The first and the only line of the input contains two distinct integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- Input 4 6 Output 2 Input 10 1 Output 9 -----Note----- In the first example you need to push the blue button once, and then push the red button once. In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.
from sys import stdin input = stdin.readline().rstrip() n, m = int(input.split()[0]), int(input.split()[1]) q = [n] visited = {} count = {n: 0} while q: num = q.pop() if num == m: print(count[num]) break if num - 1 > 0 and not visited.get(num - 1): q.insert(0, num - 1) visited[num - 1] = True count[num - 1] = count[num] + 1 if num < m and not visited.get(num * 2): q.insert(0, num * 2) visited[num * 2] = True count[num * 2] = count[num] + 1
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR DICT ASSIGN VAR DICT VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER 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.
n, m = map(int, input().split()) if n > m: print(n - m) exit() r = 0 while m != n: if m % 2 or m < n: r += 1 m += 1 else: r += 1 m //= 2 print(r)
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 NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n. Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result? -----Input----- The first and the only line of the input contains two distinct integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- Input 4 6 Output 2 Input 10 1 Output 9 -----Note----- In the first example you need to push the blue button once, and then push the red button once. In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.
a, b = map(int, input().split()) if b > a: ans = 0 while True: if b % 2 == 0: b /= 2 ans += 1 else: b += 1 ans += 1 if b == a: print(ans) break elif b < a: print(int(ans + a - b)) break else: print(a - b)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n. Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result? -----Input----- The first and the only line of the input contains two distinct integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- Input 4 6 Output 2 Input 10 1 Output 9 -----Note----- In the first example you need to push the blue button once, and then push the red button once. In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.
a = input().split() n = int(a[0]) m = int(a[1]) k = 0 if m < n: k = n - m else: while m != n: if m < n: m += 1 k += 1 elif m % 2 == 0: m /= 2 k += 1 else: m += 1 k += 1 print(k)
ASSIGN VAR FUNC_CALL 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 BIN_OP VAR VAR WHILE VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n. Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result? -----Input----- The first and the only line of the input contains two distinct integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- Input 4 6 Output 2 Input 10 1 Output 9 -----Note----- In the first example you need to push the blue button once, and then push the red button once. In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.
def two_buttons(n, m): d = {n: 0} keys = {n} while True: new_keys = set() for k in keys: if 2 * k <= 10000 and 2 * k not in d: d[2 * k] = d[k] + 1 new_keys.add(2 * k) if k >= 1 and k - 1 not in d: d[k - 1] = d[k] + 1 new_keys.add(k - 1) if m in d: return d[m] keys = new_keys def main(): n, m = map(int, input().split()) ans = two_buttons(n, m) print(ans) main()
FUNC_DEF ASSIGN VAR DICT VAR NUMBER ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL 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 findMin(n, m): sum = 0 if n >= m: return n - m while n < m: if m % 2 != 0: m += 1 else: m //= 2 sum += 1 return sum + (n - m) n, m = [int(x) for x in input().split()] print(findMin(n, m))
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 RETURN BIN_OP VAR BIN_OP VAR VAR 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()) d = {} d[n] = 0 unvisited = [n] finished = False while m not in d and unvisited: n = unvisited[0] del unvisited[0] if n > 1 and n - 1 not in d: d[n - 1] = d[n] + 1 unvisited += [n - 1] if n < m and n * 2 not in d: d[n * 2] = d[n] + 1 unvisited += [n * 2] print(d[m])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR LIST BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR LIST 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.
import sys n, m = map(int, sys.stdin.readline().split()) counter = 0 if n > m: counter = n - m else: while m - n != 0: if m % 2 == 0 and m > n: m /= 2 counter += 1 else: if n >= m: counter += n - m break m = (m + 1) / 2 counter += 2 print(int(counter))
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER 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.
p = input().split() a = int(p[0]) b = int(p[1]) if b <= a: print(a - b) else: i = 0 while a != b: while b % 2 == 0 and b > a: b = b // 2 i = i + 1 if b != a: b = b + 1 i = i + 1 print(i)
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 ASSIGN VAR NUMBER WHILE VAR VAR WHILE BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n. Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result? -----Input----- The first and the only line of the input contains two distinct integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- Input 4 6 Output 2 Input 10 1 Output 9 -----Note----- In the first example you need to push the blue button once, and then push the red button once. In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.
n, m = map(int, input().split()) cont = 0 while n < m: if m & 1: m += 1 else: m = m // 2 cont += 1 resultado = cont + (n - m) print(resultado)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER 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.
n, m = [int(x) for x in input().split()] if n >= m: print(n - m) else: count = 0 while n != m: if n < m: if m % 2 == 0: m = m / 2 else: m = m + 1 count += 1 else: count += n - m n = m print("{0:.0f}".format(count))
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 IF 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 ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING 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.
lists = [] list_count = [1] visited = set() count = 0 def find_short(end): global count, lists, list_count, visited while len(lists) != 0: current = lists[0] current_count = list_count[0] lists = lists[1:] list_count = list_count[1:] if current == end: if current_count == 0: count = count - 1 return count if current in visited: if current_count == 1: list_count[0] = 1 continue if current < 0: continue visited.add(current) if current < end: if current_count != 0: list_count.append(1) list_count.append(0) else: list_count.append(0) list_count.append(0) lists.append(current * 2) lists.append(current - 1) else: if current_count != 0: list_count.append(1) else: list_count.append(0) lists.append(current - 1) count += current_count return -1 def main(): input_val = input() dicti = input_val.split(" ") start = int(dicti[0]) end = int(dicti[1]) lists.append(start) result = find_short(end) print(result) return result main()
ASSIGN VAR LIST ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR IF VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER FUNC_DEF 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 EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN 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 stdin n, m = str(stdin.readline()).replace("\n", "").split(" ") n = int(n) m = int(m) i = 0 if n >= m: print(n - m) else: while m != n: if m % 2 == 0 and n < m: m = m / 2 i = i + 1 elif m % 2 == 1 and n < m: m = m + 1 m = m / 2 i = i + 2 elif n > m: m = m + 1 i = i + 1 print(i)
ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR STRING STRING STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n. Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result? -----Input----- The first and the only line of the input contains two distinct integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- Input 4 6 Output 2 Input 10 1 Output 9 -----Note----- In the first example you need to push the blue button once, and then push the red button once. In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.
inp = [int(i) for i in str(input()).split(" ")] start = inp[0] ans = inp[1] def BFS(start, ans): if start == ans: print(0) else: SAPairs = [(0, start)] explored = {} while SAPairs: path = SAPairs.pop(0) successors = [] if path[-1] > 0: successors.append(path[-1] - 1) if path[-1] < ans: successors.append(path[-1] * 2) counter = path[0] + 1 for newState in successors: if newState not in explored: explored[newState] = 1 if newState == ans: return counter SAPairs.append((counter, newState)) print(BFS(start, ans))
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER VAR ASSIGN VAR DICT WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n. Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result? -----Input----- The first and the only line of the input contains two distinct integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- Input 4 6 Output 2 Input 10 1 Output 9 -----Note----- In the first example you need to push the blue button once, and then push the red button once. In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.
from sys import stdin, stdout def get_value(steps, key, value): if key in steps: numbers = steps[key] numbers.append(value) return numbers else: return [value] def is_valid(new_number, calculated_numbers): max_number = 10000 if ( new_number >= 0 and new_number <= max_number and not new_number in calculated_numbers ): return True return False def solve(n, m): step_count = 0 calculated_numbers = {} steps = {} steps[0] = [n] max_number = 10000 while step_count in steps: numbers = steps[step_count] for number in numbers: new_number01 = number - 1 new_number02 = number * 2 if new_number01 == m or new_number02 == m: return step_count + 1 if is_valid(new_number01, calculated_numbers): value = get_value(steps, step_count + 1, new_number01) steps[step_count + 1] = value if is_valid(new_number02, calculated_numbers): value = get_value(steps, step_count + 1, new_number02) steps[step_count + 1] = value calculated_numbers[number] = True step_count += 1 input_data = [int(x) for x in stdin.readline().rstrip("\r\n").split()] n = input_data[0] m = input_data[1] stdout.write(str(solve(n, m)))
FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR RETURN LIST VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER LIST VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN 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.
start, end = map(int, input().split()) def compute(start, end): if start >= end: return start - end elif end % 2 != 0: return 1 + compute(start, end + 1) else: return 1 + compute(start, end // 2) print(compute(start, end))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER 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.
class Graph: adj_list = {} num_vertices = 0 visited = set() def __init__(self, start, end, num_vertices): self.start = start self.end = end self.adj_list = {} self.num_vertices = num_vertices self.visited = set() self.path = [None] * (num_vertices * 4 + 1) def generate_edges(self): self.adj_list[1] = [2] for i in range(2, self.num_vertices * 2 + 1): self.adj_list[i] = [i - 1, 2 * i] for i in range(self.num_vertices * 2 + 1, self.num_vertices * 4 + 1): self.adj_list[i] = [i - 1] for i in range(1, self.num_vertices * 4 + 1): self.path[i] = None print(self.bfs()) def bfs(self): queue = [self.start] self.visited.add(self.start) self.path[self.start] = 0 while queue: v = queue.pop(0) vlna = self.path[v] for node in self.adj_list[v]: if node not in self.visited: if self.path[node] is None or self.path[node] > vlna: self.path[node] = vlna + 1 self.visited.add(node) queue.append(node) if node == self.end: return self.path[node] list_n_m = input().split() list_n_m = [int(i) for i in list_n_m] n = int(list_n_m[0]) m = int(list_n_m[1]) g = Graph(n, m, max(list_n_m)) g.generate_edges()
CLASS_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR LIST BIN_OP VAR NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR LIST BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NONE EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR VAR IF VAR VAR IF VAR VAR NONE VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n. Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result? -----Input----- The first and the only line of the input contains two distinct integers n and m (1 ≀ n, m ≀ 10^4), separated by a space . -----Output----- Print a single number β€” the minimum number of times one needs to push the button required to get the number m out of number n. -----Examples----- Input 4 6 Output 2 Input 10 1 Output 9 -----Note----- In the first example you need to push the blue button once, and then push the red button once. In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.
import sys input = sys.stdin.readline def inp(): return int(input()) def inlt(): return list(map(int, input().split())) def insr(): s = input() return list(s[: len(s) - 1]) def invr(): return map(int, input().split()) numbers = list(invr()) n = numbers[0] m = numbers[1] if m < n: print(n - m) else: count = 0 while m > n: if m % 2 != 0: m = m + 1 count = count + 1 else: count = count + 1 m = m / 2 print(int(count + (n - m)))
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR 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 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 EXPR FUNC_CALL VAR 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 rec(n, m): if m == n: return 0 elif (m + 1) // 2 < n: return n - m // 2 + 1 elif (m + 1) // 2 >= n: if m % 2 == 0: return 1 + rec(n, m // 2) else: return 1 + 1 + rec(n, (m + 1) // 2) def main(): s = input().split() n = int(s[0]) m = int(s[1]) if n > m: print(n - m) elif n <= m: print(rec(n, m)) main()
FUNC_DEF IF VAR VAR RETURN NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF 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 IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR