description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
t = int(input()) while t: t -= 1 n, k = [int(x) for x in input().split()] l = [int(x) for x in input().split()] if k <= n: delta = k * (k - 1) // 2 tmps = sum(l[:k]) s = tmps for i in range(k, n): tmps += l[i] tmps -= l[i - k] s = tmps if tmps > s else s else: s = sum(l) delta = n * (2 * k - n - 1) // 2 print(s + delta)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
for _ in range(int(input())): n, k = map(int, input().split(" ")) l = list(map(int, input().split(" "))) if n == k: print(sum(l) + n * (n - 1) // 2) elif n > k: m = sum(l[:k]) c = m for i in range(k, n): c += l[i] - l[i - k] m = max(c, m) print(m + k * (k - 1) // 2) else: sm = 0 sm = sum(l) + n * (n - 1) // 2 sm = sm + (k - n) * len(l) print(sm)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
def ar(n): return n * (n - 1) // 2 for _ in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) if k > n: ans = sum(a) ans += ar(n) + k - n + (k - n) * (n - 1) else: b = [0] s = 0 for i in a: s += i b.append(s) s = 0 for i in range(k, n + 1): s = max(s, b[i] - b[i - k]) ans = s ans += ar(k) 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 VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
tests = int(input()) for i in range(tests): n, steps = map(int, input().split()) florest = [int(n) for n in input().split()] if steps <= n: sum_ = 0 for j in range(steps): sum_ += florest[j] result = sum_ for j in range(steps, n, 1): sum_ += florest[j] sum_ -= florest[j - steps] result = max(result, sum_) result += steps * (steps - 1) / 2 print(int(result)) else: result = n * (steps - n) + n * (n - 1) / 2 for j in range(n): result += florest[j] print(int(result))
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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
t = int(input()) for i in range(0, t): n, k = [int(x) for x in input().split(" ")] a = [int(x) for x in input().split(" ")] if k > n: print(sum(a) + n * k - n * (n + 1) // 2) else: l = 0 r = k - 1 m = sum(a[:k]) s = m for r in range(k, n): s = s - a[r - k] + a[r] m = max(s, m) print(m + k * (k - 1) // 2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
tc = int(input()) for t in range(tc): n, k = map(int, input().split()) arr = list(map(int, input().split())) if k >= n: cun = sum(arr) r = n - k sm = (k * (k - 1) - r * (r + 1)) // 2 print(cun + sm) else: cn = sum(arr[:k]) mx = cn for j in range(k, n): cn = cn + arr[j] - arr[j - k] if cn > mx: mx = cn sm = k * (k - 1) // 2 print(mx + sm)
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
x = int(input()) def cal(n, m, arr): if m >= n: t = sum(arr) + n * (m - 1) - (n - 1) * n // 2 return t t = sum(arr[:m]) k = t for i in range(n - m): k -= arr[i] k += arr[i + m] t = max(t, k) return t + m * (m - 1) - m * (m - 1) // 2 for jj in range(x): n, m = [int(i) for i in input().split(" ")] arr = [int(i) for i in input().split(" ")] print(cal(n, m, arr))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
def solve(a, n, k): if k > n: return sum(a) + n * k - n * (n + 1) // 2 ksum = k * (k - 1) // 2 ps = [0] * (n + 1) for i in range(n): ps[i + 1] = ps[i] + a[i] nsum = 0 for i in range(n - k + 1): nsum = max(nsum, ps[i + k] - ps[i]) return nsum + ksum t = int(input()) for _ in range(t): n, k = map(int, input().split()) a = list(map(int, input().split())) print(solve(a, n, k))
FUNC_DEF IF VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR 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 LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR RETURN BIN_OP VAR VAR 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
t = int(input()) while t > 0: n, k = map(int, input().split()) arr = list(map(int, input().split())) sumArr = [0] res = 0 for i in range(n): sumArr.append(sumArr[i] + arr[i]) if k <= n: for i in range(n - k + 1): res = max(res, sumArr[i + k] - sumArr[i]) res += k * (k - 1) / 2 else: res = sumArr[n] + n * (2 * k - n - 1) / 2 t -= 1 print(int(res))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
def solve(n, k, array): ans = 0 if k <= n: ans += int(k * (k - 1) / 2) temp = 0 for i in range(k): temp += array[i] maxTemp = temp for i in range(n - k): temp -= array[i] temp += array[i + k] maxTemp = max(temp, maxTemp) ans += maxTemp else: ans += sum(array) temp = n * k temp -= int(n * (n + 1) / 2) ans += temp return ans t = int(input()) for _ in range(t): n, k = input().split(" ") n = int(n) k = int(k) array = [int(i) for i in input().split(" ")] print(solve(n, k, array))
FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
from itertools import accumulate for _ in range(int(input())): n, k = list(map(int, input().split())) a = list(map(int, input().split())) if n == 1: print(a[0] + k - 1) elif n == 2: if k == 1: print(max(a)) else: print(sum(a) + k * n - n * (n + 1) // 2) elif k == n: print(sum(a) + n * (n - 1) // 2) elif k < n: temp1 = k % n preSum = list(accumulate(a, initial=0)) maxn = 0 for i in range(n - temp1 + 1): maxn = max(maxn, preSum[i + temp1] - preSum[i]) print(maxn + temp1 * (temp1 - 1) // 2) else: print(sum(a) + k * n - n * (n + 1) // 2)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
for __ in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) if k >= n: print(sum(a) + (k - 1) * n - (n - 1) * n // 2) continue s = sum(a[:k]) big = s for i in range(k, n): s += a[i] - a[i - k] big = max(big, s) print(big + 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
for time in range(int(input())): n, x = map(int, input().split()) nums = [int(a) for a in input().split()] if n > x: s = 0 for j in range(x): s += nums[j] maxi = s for j in range(x, n): s += nums[j] - nums[j - x] maxi = max(s, maxi) print(maxi + (x * x - x) // 2) else: s = sum(j for j in nums) print(s + n * (x - 1) - n * (n - 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 ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) a = list(map(int, input().split())) if k < n: m = sum(a[:k]) c = m for i in range(k, n): c += a[i] - a[i - k] m = max(m, c) print(m + k * (k - 1) // 2) else: print(sum(a) + (k - n) * n + n * (n - 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
T = int(input()) for testcase in range(T): n, k = list(map(int, input().split())) a = list(map(int, input().split())) s = sum(a) if k >= n: print(s + k * n - n * (n + 1) // 2) else: ps = [0] pssum = 0 for x in a: pssum += x ps.append(pssum) bestsum = 0 for i in range(n - k + 1): if ps[i + k] - ps[i] > bestsum: bestsum = ps[i + k] - ps[i] print(bestsum + (k - 1) * k // 2)
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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
t = int(input()) for _ in range(t): n, k = [int(x) for x in input().strip().split()] a = [int(x) for x in input().strip().split()] a = [0] + a for i in range(1, len(a)): a[i] += a[i - 1] if k > n: print(a[n] + (k - 1 + k - n) * n // 2) else: ans = 0 for i in range(0, 2 * n): if i + k <= n: ans = max(ans, a[i + k] - a[i]) print(ans + k * (k - 1) // 2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
t = int(input()) for _ in range(t): n, k = list(map(int, input().split())) vals = list(map(int, input().split())) maxSum = 0 if k < n: workingSum = sum(vals[:k]) maxSum = workingSum for i in range(k, n): workingSum += vals[i] workingSum -= vals[i - k] maxSum = max(maxSum, workingSum) timeAdded = k * (k - 1) / 2 else: maxSum = sum(vals) maxSum += (k - n) * n maxSum += (n - 1) * n / 2 timeAdded = 0 print(int(maxSum + timeAdded))
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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
runs = int(input()) def solveCaseB(map, x, k): return map[x] + (k - 1 + k - x) * x // 2 def solveCaseA(map, x, k): max = 0 for i in range(x + 1): if i >= k: curr = map[i] - map[i - k] if curr > max: max = curr return max + (1 + k - 1) * (k - 1) // 2 for i in range(runs): [x, k] = [int(i) for i in input().split()] mapa = [0] + [int(i) for i in input().split()] for i in range(1, x + 1): mapa[i] += mapa[i - 1] if k > x: print(solveCaseB(mapa, x, k)) else: print(solveCaseA(mapa, x, k))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
t = int(input()) for i in range(t): n, k = [int(x) for x in input().split()] nums = [int(x) for x in input().split()] if k >= n: print(int(sum(nums) + n * (k - 1) - (n - 1) * n / 2)) else: cur_max = sum(nums[0:k]) current = cur_max for j in range(n - k): current = current + nums[k + j] - nums[j] if current > cur_max: cur_max = current print(int(cur_max + (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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
for _ in range(int(input())): n, k = map(int, input().split()) arr = list(map(int, input().split())) temp = 0 if k <= n: curr_first = arr[0] curr_sum = sum(arr[:k]) res = curr_sum for nn in arr[k:]: curr_sum = curr_sum - curr_first + nn res = max(res, curr_sum) temp += 1 curr_first = arr[temp] print(res + k * (k - 1) // 2) else: res = sum(arr) + n * (n - 1) // 2 + n * (k - n) print(res)
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
def resi(): n, k = map(int, input().split()) a = list(map(int, input().split())) s = sum(a) if k >= n: print(n * (k - n) + s + (n - 1) * n // 2) return tmp = 0 for i in range(k): tmp += a[i] maxx = tmp for i in range(k, n): tmp += a[i] tmp -= a[i - k] if maxx < tmp: maxx = tmp print(maxx + k * (k - 1) // 2) return for _ in range(int(input())): resi()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER RETURN ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 150\,000$) — the number of pairs. Each of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \le a_i, b_i \le 2 \cdot 10^9$). -----Output----- Print a single integer — the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $-1$. -----Examples----- Input 3 17 18 15 24 12 15 Output 6 Input 2 10 16 7 17 Output -1 Input 5 90 108 45 105 75 40 165 175 33 30 Output 5 -----Note----- In the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $1$ satisfying the conditions. In the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output.
n = int(input()) a = [0] * n b = [0] * n for i in range(n): a[i], b[i] = map(int, input().split()) d = [] for x in (a[0], b[0]): for i in range(2, x + 1): if i * i > x: break if x % i == 0: d.append(i) while x % i == 0: x //= i if x != 1: d.append(x) for x in set(d): ok = True for i in range(1, n): if a[i] % x != 0 and b[i] % x != 0: ok = False break if ok: print(x) exit(0) print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 150\,000$) — the number of pairs. Each of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \le a_i, b_i \le 2 \cdot 10^9$). -----Output----- Print a single integer — the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $-1$. -----Examples----- Input 3 17 18 15 24 12 15 Output 6 Input 2 10 16 7 17 Output -1 Input 5 90 108 45 105 75 40 165 175 33 30 Output 5 -----Note----- In the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $1$ satisfying the conditions. In the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output.
import sys input = sys.stdin.readline def check(f): for Ai, Bi in AB: if Ai % f != 0 and Bi % f != 0: return False return True def factorize(n): factors = [] for i in range(2, int(n**0.5) + 1): cnt = 0 while n % i == 0: n //= i cnt += 1 if cnt > 0: factors.append((i, cnt)) if n > 1: factors.append((n, 1)) return factors n = int(input()) AB = [tuple(map(int, input().split())) for _ in range(n)] cands = factorize(AB[0][0]) + factorize(AB[0][1]) for f, _ in cands: if check(f): print(f) exit() print(-1)
IMPORT ASSIGN VAR VAR FUNC_DEF FOR VAR VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 150\,000$) — the number of pairs. Each of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \le a_i, b_i \le 2 \cdot 10^9$). -----Output----- Print a single integer — the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $-1$. -----Examples----- Input 3 17 18 15 24 12 15 Output 6 Input 2 10 16 7 17 Output -1 Input 5 90 108 45 105 75 40 165 175 33 30 Output 5 -----Note----- In the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $1$ satisfying the conditions. In the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output.
n = int(input()) ar = [] a, b = map(int, input().split()) num = a**0.5 + 2 i = 2 while i < num: if a % i == 0: ar += [i] while a % i == 0: a //= i i += 1 num = b**0.5 + 2 i = 2 while i < num: if b % i == 0: ar += [i] while b % i == 0: b //= i i += 1 if a != 1: ar += [a] if b != 1: ar += [b] for i in range(1, n): a, b = map(int, input().split()) ar2 = [] for j in ar: if a % j == 0 or b % j == 0: ar2 += [j] ar = ar2 if len(ar) == 0: print(-1) else: print(ar[0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR NUMBER VAR LIST VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR NUMBER VAR LIST VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR LIST VAR IF VAR NUMBER VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR LIST VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 150\,000$) — the number of pairs. Each of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \le a_i, b_i \le 2 \cdot 10^9$). -----Output----- Print a single integer — the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $-1$. -----Examples----- Input 3 17 18 15 24 12 15 Output 6 Input 2 10 16 7 17 Output -1 Input 5 90 108 45 105 75 40 165 175 33 30 Output 5 -----Note----- In the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $1$ satisfying the conditions. In the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output.
def prime_divs(n): res = set() d = 2 while d * d <= n: if n % d == 0: while n % d == 0: n = n // d res.add(d) else: d = d + 1 if n > 1: res.add(n) return res n = int(input()) ls = [] for i in range(n): a, b = list(map(int, input().split())) ls.append([a, b]) set1 = prime_divs(ls[0][0]) set1 = set1.union(prime_divs(ls[0][1])) for i in range(1, n): to_remove = set() for d in set1: if not (ls[i][0] % d == 0 or ls[i][1] % d == 0): to_remove.add(d) set1 = set1.difference(to_remove) if len(set1) == 0: print(-1) else: print(list(set1)[0])
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 150\,000$) — the number of pairs. Each of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \le a_i, b_i \le 2 \cdot 10^9$). -----Output----- Print a single integer — the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $-1$. -----Examples----- Input 3 17 18 15 24 12 15 Output 6 Input 2 10 16 7 17 Output -1 Input 5 90 108 45 105 75 40 165 175 33 30 Output 5 -----Note----- In the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $1$ satisfying the conditions. In the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output.
n, m = int(input()), 10**5 a = [list(map(int, input().split())) for i in range(n)] p, v = [(0) for i in range(m)], [(0) for i in range(m)] for i in range(2, m): if not v[i]: p[0] += 1 p[p[0]] = i for j in range(1, p[0] + 1): if i * p[j] >= m: break v[i * p[j]] = 1 if i % p[j] == 0: break def ok(x): global a for i in a: if i[0] % x > 0 and i[1] % x > 0: return False return True def judge(x): global p for i in p[1 : p[0] + 1]: if x % i == 0: if ok(i): print(i) return True while x % i == 0: x //= i if x != 1: if ok(x): print(x) return True return False if judge(a[0][0]): exit(0) if judge(a[0][1]): exit(0) print(-1)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER FUNC_DEF FOR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 150\,000$) — the number of pairs. Each of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \le a_i, b_i \le 2 \cdot 10^9$). -----Output----- Print a single integer — the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $-1$. -----Examples----- Input 3 17 18 15 24 12 15 Output 6 Input 2 10 16 7 17 Output -1 Input 5 90 108 45 105 75 40 165 175 33 30 Output 5 -----Note----- In the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $1$ satisfying the conditions. In the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output.
def prime_factors(n): i = 2 factors = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(i) if n > 1: factors.append(n) return factors n = int(input()) Ar = [[None, None] for _ in range(n)] for i in range(n): Ar[i][0], Ar[i][1] = map(int, input().split()) A = prime_factors(Ar[0][0]) for x in prime_factors(Ar[0][1]): A.append(x) A = list(set(A)) cnt = 0 for j in A: for i in range(n): if Ar[i][0] % j == 0 or Ar[i][1] % j == 0: cnt += 1 if cnt != i + 1: cnt = 0 break if cnt == n: print(j) break if cnt == 0: print(-1)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NONE NONE VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 150\,000$) — the number of pairs. Each of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \le a_i, b_i \le 2 \cdot 10^9$). -----Output----- Print a single integer — the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $-1$. -----Examples----- Input 3 17 18 15 24 12 15 Output 6 Input 2 10 16 7 17 Output -1 Input 5 90 108 45 105 75 40 165 175 33 30 Output 5 -----Note----- In the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $1$ satisfying the conditions. In the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output.
def gcd(x, y): while y: x, y = y, x % y return x n = int(input()) al, bl = list(), list() last = 0 for i in range(n): a, b = map(int, input().split()) al.append(a) bl.append(b) last = gcd(last, al[i] * bl[i]) for i in range(n): if gcd(last, al[i]) > 1: last = gcd(last, al[i]) else: last = gcd(last, bl[i]) if last == 1: print(-1) else: print(last)
FUNC_DEF WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 150\,000$) — the number of pairs. Each of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \le a_i, b_i \le 2 \cdot 10^9$). -----Output----- Print a single integer — the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $-1$. -----Examples----- Input 3 17 18 15 24 12 15 Output 6 Input 2 10 16 7 17 Output -1 Input 5 90 108 45 105 75 40 165 175 33 30 Output 5 -----Note----- In the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $1$ satisfying the conditions. In the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output.
def gcd(a, b): if b == 0: return a else: return gcd(b, a % b) n = int(input()) sum = 0 l, r = 0, 0 while n > 0: l, r = input().split() l = int(l) r = int(r) t = gcd(l, r) t = int(l * r) // t sum = gcd(sum, t) n -= 1 if sum == 1: print(-1) elif gcd(l, sum) == 1: i = 2 sum = gcd(r, sum) while sum >= i * i: if sum % i == 0: sum = i break i += 1 print(int(sum)) else: i = 2 sum = gcd(l, sum) while sum >= i * i: if sum % i == 0: sum = i break i += 1 print(int(sum))
FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 150\,000$) — the number of pairs. Each of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \le a_i, b_i \le 2 \cdot 10^9$). -----Output----- Print a single integer — the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $-1$. -----Examples----- Input 3 17 18 15 24 12 15 Output 6 Input 2 10 16 7 17 Output -1 Input 5 90 108 45 105 75 40 165 175 33 30 Output 5 -----Note----- In the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $1$ satisfying the conditions. In the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output.
primes = set() ar, br = [0] * 150001, [0] * 150001 def gen(num): i = 2 while i * i <= num: if num % i == 0: while num % i == 0: num //= i primes.add(i) i += 1 if num > 1: primes.add(num) n = int(input()) for i in range(1, n + 1): ar[i], br[i] = map(int, input().split()) gen(ar[1]) gen(br[1]) for p in primes: ok = 1 for i in range(2, n + 1): if ar[i] % p != 0 and br[i] % p != 0: ok = 0 break if ok: print(p) exit(0) print(-1)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER NUMBER BIN_OP LIST NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 150\,000$) — the number of pairs. Each of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \le a_i, b_i \le 2 \cdot 10^9$). -----Output----- Print a single integer — the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $-1$. -----Examples----- Input 3 17 18 15 24 12 15 Output 6 Input 2 10 16 7 17 Output -1 Input 5 90 108 45 105 75 40 165 175 33 30 Output 5 -----Note----- In the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $1$ satisfying the conditions. In the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output.
import sys input = sys.stdin.readline R = lambda: map(int, input().split()) I = lambda: int(input()) S = lambda: input().rstrip("\n") L = lambda: list(R()) class WCD: def __init__(self): self.n = I() self.ab = [tuple(R()) for _ in range(self.n)] def ok(self, x): for u, v in self.ab: if u % x and v % x: return False return True def factorize(self, num): ret = [] for i in range(2, int(num**0.5) + 1): if num % i == 0: ret.append(i) while num % i == 0: num //= i if num > 1: ret.append(num) return ret def solve(self): factors = self.factorize(self.ab[0][0]) + self.factorize(self.ab[0][1]) for x in factors: if self.ok(x): print(x) exit() print(-1) exit() o = WCD() o.solve()
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF FOR VAR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 150\,000$) — the number of pairs. Each of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \le a_i, b_i \le 2 \cdot 10^9$). -----Output----- Print a single integer — the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $-1$. -----Examples----- Input 3 17 18 15 24 12 15 Output 6 Input 2 10 16 7 17 Output -1 Input 5 90 108 45 105 75 40 165 175 33 30 Output 5 -----Note----- In the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $1$ satisfying the conditions. In the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output.
def primes(n): d = 2 while d * d <= n: while n % d == 0: primfac.add(d) n //= d d += 1 if n > 1: primfac.add(n) return primfac n = int(input()) pairs = [] for i in range(n): pairs.append([int(i) for i in input().split()]) primfac = set() primes(pairs[0][0]) primes(pairs[0][1]) for i in range(n - 1): to_delete = [] for j in primfac: if pairs[i + 1][0] % j != 0 and pairs[i + 1][1] % j != 0: to_delete.append(j) for j in to_delete: primfac.remove(j) li = list(primfac) if len(li) == 0: print(-1) else: print(li[-1])
FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR WHILE BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 150\,000$) — the number of pairs. Each of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \le a_i, b_i \le 2 \cdot 10^9$). -----Output----- Print a single integer — the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $-1$. -----Examples----- Input 3 17 18 15 24 12 15 Output 6 Input 2 10 16 7 17 Output -1 Input 5 90 108 45 105 75 40 165 175 33 30 Output 5 -----Note----- In the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $1$ satisfying the conditions. In the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output.
n = int(input()) a, b = [0] * n, [0] * n g = 0 def gcd(a, b): return a if b == 0 else gcd(b, a % b) for i in range(n): a[i], b[i] = map(int, input().split()) g = gcd(g, a[i] * b[i]) for i in range(n): if gcd(g, a[i]) > 1: g = gcd(g, a[i]) else: g = gcd(g, b[i]) print(g if g > 1 else -1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FUNC_DEF RETURN VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 150\,000$) — the number of pairs. Each of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \le a_i, b_i \le 2 \cdot 10^9$). -----Output----- Print a single integer — the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $-1$. -----Examples----- Input 3 17 18 15 24 12 15 Output 6 Input 2 10 16 7 17 Output -1 Input 5 90 108 45 105 75 40 165 175 33 30 Output 5 -----Note----- In the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $1$ satisfying the conditions. In the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output.
def gcd(a, b): if b == 0: return a return gcd(b, a % b) n = int(input()) a = [] b = [] for i in range(n): A, B = map(int, input().split()) a.append(A) b.append(B) ans = -1 x = 0 for i in range(n): x = gcd(x, a[i] * b[i]) if x != 1: for i in range(n): if gcd(x, a[i]) > 1: x = gcd(x, a[i]) ans = gcd(x, a[i]) else: x = gcd(x, b[i]) ans = gcd(x, b[i]) print(ans)
FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 150\,000$) — the number of pairs. Each of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \le a_i, b_i \le 2 \cdot 10^9$). -----Output----- Print a single integer — the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $-1$. -----Examples----- Input 3 17 18 15 24 12 15 Output 6 Input 2 10 16 7 17 Output -1 Input 5 90 108 45 105 75 40 165 175 33 30 Output 5 -----Note----- In the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $1$ satisfying the conditions. In the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output.
import sys def trial_division(n): a = [] while n % 2 == 0: a.append(2) n = n // 2 f = 3 while f * f <= n: if n % f == 0: a.append(f) n = n // f else: f += 2 if n != 1: a.append(n) return a n = int(sys.stdin.readline()) a = [] for i in range(n): b, c = map(int, sys.stdin.readline().split()) a.append((b, c)) ans = 0 if n == 1: ans = a[0][0] else: arr = trial_division(a[0][0]) arr.extend(trial_division(a[0][1])) divs = set(arr) for i in range(1, n): to_remove = set() for d in divs: if a[i][0] % d != 0 and a[i][1] % d != 0: to_remove.add(d) for r in to_remove: divs.remove(r) if len(divs) > 0: ans = divs.pop() else: ans = -1 print(ans)
IMPORT FUNC_DEF ASSIGN VAR LIST WHILE BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 150\,000$) — the number of pairs. Each of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \le a_i, b_i \le 2 \cdot 10^9$). -----Output----- Print a single integer — the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $-1$. -----Examples----- Input 3 17 18 15 24 12 15 Output 6 Input 2 10 16 7 17 Output -1 Input 5 90 108 45 105 75 40 165 175 33 30 Output 5 -----Note----- In the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $1$ satisfying the conditions. In the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output.
def countdiv(n): count = set() i = 2 nn = n while n > 1: if n % i == 0: count.add(i) n //= i else: i += 1 if i > nn**0.5 + 1: count.add(n) break return count def countt(n, s): ss = set() for i in s: if n % i == 0: ss.add(i) return ss n = int(input()) a = list(map(int, input().split())) s = countdiv(a[0]).union(countdiv(a[1])) for itr in range(1, n): a = list(map(int, input().split())) s = countt(a[0] * a[1], s) if len(s) == 0: break if len(s) == 0: print(-1) else: print(list(s)[0])
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN 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 FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 150\,000$) — the number of pairs. Each of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \le a_i, b_i \le 2 \cdot 10^9$). -----Output----- Print a single integer — the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $-1$. -----Examples----- Input 3 17 18 15 24 12 15 Output 6 Input 2 10 16 7 17 Output -1 Input 5 90 108 45 105 75 40 165 175 33 30 Output 5 -----Note----- In the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $1$ satisfying the conditions. In the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output.
def gcd(a, b): if b == 0: return a else: return gcd(b, a % b) test = int(input()) list = [tuple(map(int, input().split())) for i in range(test)] ans = 0 for a, b in list: ans = gcd(a * b, ans) for a, b in list: if gcd(ans, b) != 1: ans = gcd(ans, b) else: ans = gcd(ans, a) if ans == 1: print(-1) else: print(ans)
FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 150\,000$) — the number of pairs. Each of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \le a_i, b_i \le 2 \cdot 10^9$). -----Output----- Print a single integer — the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $-1$. -----Examples----- Input 3 17 18 15 24 12 15 Output 6 Input 2 10 16 7 17 Output -1 Input 5 90 108 45 105 75 40 165 175 33 30 Output 5 -----Note----- In the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $1$ satisfying the conditions. In the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output.
import sys def prime_factors(a): factors = set() i = 2 orig = a while i <= int(orig**0.5) + 1: while a % i == 0: a = a // i factors.add(i) i += 1 if a > 1: factors.add(a) return factors N = int(sys.stdin.readline()) a, b = [int(x) for x in sys.stdin.readline().split()] divisors = prime_factors(a).union(prime_factors(b)) divisors = [int(x) for x in divisors] for i in range(N - 1): checker = [] c, d = [int(x) for x in sys.stdin.readline().split()] if divisors: for i in divisors: if c * d % i == 0: checker.append(i) divisors = checker print(divisors[0] if divisors != [] else "-1")
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR FOR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR LIST VAR NUMBER STRING
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 150\,000$) — the number of pairs. Each of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \le a_i, b_i \le 2 \cdot 10^9$). -----Output----- Print a single integer — the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $-1$. -----Examples----- Input 3 17 18 15 24 12 15 Output 6 Input 2 10 16 7 17 Output -1 Input 5 90 108 45 105 75 40 165 175 33 30 Output 5 -----Note----- In the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $1$ satisfying the conditions. In the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output.
n = int(input()) x, y = map(int, input().split()) primes = list() arr = [] for i in [x, y]: k = 2 while k * k <= i: if i % k == 0: while i % k == 0: i //= k primes.append(k) k += 1 if i > 1: primes.append(i) for _ in range(n - 1): arr.append(list(map(int, input().split()))) for p in primes: flag = True for x, y in arr: if x % p != 0 and y % p != 0: flag = False break if flag: print(p) break else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR LIST VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 150\,000$) — the number of pairs. Each of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \le a_i, b_i \le 2 \cdot 10^9$). -----Output----- Print a single integer — the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $-1$. -----Examples----- Input 3 17 18 15 24 12 15 Output 6 Input 2 10 16 7 17 Output -1 Input 5 90 108 45 105 75 40 165 175 33 30 Output 5 -----Note----- In the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $1$ satisfying the conditions. In the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output.
import sys input = sys.stdin.buffer.readline def is_prime(x): i = 2 global s p = x while i * i <= x: flag = 0 while p % i == 0: p //= i flag = 1 if flag == 1: s.add(i) i += 1 if p > 1: s.add(p) n = int(input()) s = set() lst = [] for i in range(n): a, b = map(int, input().split()) lst.append([a, b]) is_prime(lst[0][0]) is_prime(lst[0][1]) r = set() for i in range(1, n): l = lst[i][0] rr = lst[i][1] r = set() for j in s: if l % j == 0: r.add(j) for j in s: if rr % j == 0: r.add(j) s = r if len(s) == 0: print(-1) else: for i in s: print(i) break
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 150\,000$) — the number of pairs. Each of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \le a_i, b_i \le 2 \cdot 10^9$). -----Output----- Print a single integer — the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $-1$. -----Examples----- Input 3 17 18 15 24 12 15 Output 6 Input 2 10 16 7 17 Output -1 Input 5 90 108 45 105 75 40 165 175 33 30 Output 5 -----Note----- In the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $1$ satisfying the conditions. In the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output.
def f(n): d = 2 A = [] while d * d <= n: if n % d == 0: A.append(d) n = n // d else: d += 1 if n != 1: A.append(n) return A n = int(input()) a, b = map(int, input().split()) A = set(f(a)) B = set(f(b)) ans = A.union(B) ans = list(ans) for i in range(1, n): a, b = map(int, input().split()) upd = ans.copy() for j in ans: if a % j != 0 and b % j != 0: upd.remove(j) ans = upd.copy() if len(ans) == 0: print(-1) else: print(ans[0])
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 150\,000$) — the number of pairs. Each of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \le a_i, b_i \le 2 \cdot 10^9$). -----Output----- Print a single integer — the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $-1$. -----Examples----- Input 3 17 18 15 24 12 15 Output 6 Input 2 10 16 7 17 Output -1 Input 5 90 108 45 105 75 40 165 175 33 30 Output 5 -----Note----- In the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $1$ satisfying the conditions. In the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output.
def prime_factors(n): i = 2 factors = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(i) if n > 1: factors.append(n) return factors nums = [list(map(int, input().split())) for _ in range(int(input()))] prods = [(x * y) for x, y in nums] divisors = [] divisors += prime_factors(nums[0][0]) divisors += prime_factors(nums[0][1]) prods = list(dict.fromkeys(prods)) numWorks = 0 i = -1 while numWorks < len(prods) and i < len(divisors) - 1: i += 1 numWorks = 0 for j in range(0, len(prods)): if prods[j] % divisors[i] == 0: numWorks += 1 else: break if numWorks == len(prods): print(divisors[i]) else: print(-1)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 150\,000$) — the number of pairs. Each of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \le a_i, b_i \le 2 \cdot 10^9$). -----Output----- Print a single integer — the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $-1$. -----Examples----- Input 3 17 18 15 24 12 15 Output 6 Input 2 10 16 7 17 Output -1 Input 5 90 108 45 105 75 40 165 175 33 30 Output 5 -----Note----- In the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $1$ satisfying the conditions. In the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output.
def prime_factorize(n): primes_factors = {} while n % 2 == 0: n >>= 1 primes_factors[2] = primes_factors.get(2, 0) + 1 for i in range(3, int(n ** (1 / 2)) + 1, 2): while n % i == 0: n //= i primes_factors[i] = primes_factors.get(i, 0) + 1 if n > 1: primes_factors[n] = 1 return [*primes_factors.keys()] n = int(input()) a = set([]) for _ in range(n): i, j = map(int, input().split()) if _ == 0: a = set(prime_factorize(i) + prime_factorize(j)) continue for k in [*a]: if i % k and j % k: a.remove(k) print([*a][0] if len(a) else -1)
FUNC_DEF ASSIGN VAR DICT WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER RETURN LIST FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR LIST VAR IF BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR LIST VAR NUMBER NUMBER
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 150\,000$) — the number of pairs. Each of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \le a_i, b_i \le 2 \cdot 10^9$). -----Output----- Print a single integer — the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $-1$. -----Examples----- Input 3 17 18 15 24 12 15 Output 6 Input 2 10 16 7 17 Output -1 Input 5 90 108 45 105 75 40 165 175 33 30 Output 5 -----Note----- In the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $1$ satisfying the conditions. In the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output.
import sys input = sys.stdin.readline def f(a): s = [] n = a for i in range(2, int(n**0.5) + 1): if a % i == 0: while a % i == 0: a //= i s.append(i) i += 1 if a > 1: s.append(a) return s def check(elem): for item in a: if item[0] % elem != 0 and item[1] % elem != 0: return False return True n = int(input()) a = [] for i in range(n): x, y = map(int, input().split()) a.append((x, y)) s = f(a[0][0]) + f(a[0][1]) for elem in s: if check(elem): print(elem) exit() print(-1)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF FOR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 150\,000$) — the number of pairs. Each of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \le a_i, b_i \le 2 \cdot 10^9$). -----Output----- Print a single integer — the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $-1$. -----Examples----- Input 3 17 18 15 24 12 15 Output 6 Input 2 10 16 7 17 Output -1 Input 5 90 108 45 105 75 40 165 175 33 30 Output 5 -----Note----- In the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $1$ satisfying the conditions. In the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output.
n = int(input()) l1 = [] l2 = [] for i in range(n): a, b = map(int, input().split()) l1.append(a) l2.append(b) L = [] j = 2 a = l1[0] b = l2[0] while j * j <= a: if a % j == 0: while a % j == 0: a = a // j L.append(j) j = j + 1 if a > 1: L.append(a) j = 2 while j * j <= b: if b % j == 0: while b % j == 0: b = b // j L.append(j) j = j + 1 if b > 1: L.append(b) L = list(set(L)) g = 0 for j in L: f = 0 for k in range(1, n): if l1[k] % j != 0 and l2[k] % j != 0: f = 1 break if f == 0: g = 1 print(j) break if g == 0: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 150\,000$) — the number of pairs. Each of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \le a_i, b_i \le 2 \cdot 10^9$). -----Output----- Print a single integer — the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $-1$. -----Examples----- Input 3 17 18 15 24 12 15 Output 6 Input 2 10 16 7 17 Output -1 Input 5 90 108 45 105 75 40 165 175 33 30 Output 5 -----Note----- In the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $1$ satisfying the conditions. In the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output.
import sys input = sys.stdin.readline def primes(n): fact = [] if n % 2 == 0: fact.extend([2]) while n % 2 == 0: n //= 2 for i in range(3, int(n**0.5) + 1, 2): if n % i == 0: fact.append(i) while n % i == 0: n //= i if n != 1: fact.append(n) return fact def prog(): n = int(input()) a = [list(map(int, input().split())) for i in range(n)] valid_primes = set(primes(a[0][0]) + primes(a[0][1])) for i in range(1, n): to_remove = [] for p in valid_primes: if a[i][0] % p != 0 and a[i][1] % p != 0: to_remove.append(p) for p in to_remove: valid_primes.remove(p) if valid_primes: print(list(valid_primes)[0]) else: print(-1) prog()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR LIST IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 150\,000$) — the number of pairs. Each of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \le a_i, b_i \le 2 \cdot 10^9$). -----Output----- Print a single integer — the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $-1$. -----Examples----- Input 3 17 18 15 24 12 15 Output 6 Input 2 10 16 7 17 Output -1 Input 5 90 108 45 105 75 40 165 175 33 30 Output 5 -----Note----- In the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $1$ satisfying the conditions. In the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output.
import sys input = sys.stdin.readline def inpl(): return list(map(int, input().split())) def gcd(x, y): if x % y == 0: return y else: x, y = y, x % y return gcd(x, y) N = int(input()) nums = [] g = 0 for i in range(N): a, b = inpl() nums.append([a, b]) g = gcd(g, a * b) for i in range(N): ga, gb = gcd(g, nums[i][0]), gcd(g, nums[i][1]) g = max(ga, gb) if g == 1: g = -1 print(g)
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 150\,000$) — the number of pairs. Each of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \le a_i, b_i \le 2 \cdot 10^9$). -----Output----- Print a single integer — the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $-1$. -----Examples----- Input 3 17 18 15 24 12 15 Output 6 Input 2 10 16 7 17 Output -1 Input 5 90 108 45 105 75 40 165 175 33 30 Output 5 -----Note----- In the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $1$ satisfying the conditions. In the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output.
def prime(a): i = 2 if a <= 1: return 0 if a == 2 or a == 3: return 1 while i * i <= a: if a % i == 0: return 0 i += 1 return 1 def faq(a): l = [] l1 = [] i = 1 while i * i <= a: if a % i == 0: l.append(i) l.append(a // i) i += 1 for i in range(0, len(l)): if prime(l[i]) == 1: l1.append(l[i]) return l1 n = int(input()) a, b = map(int, input().split()) c = a * b l = faq(a) l.extend(faq(b)) l = list(set(l)) for i in range(1, n): a, b = map(int, input().split()) if a * b != c: j = 0 while j < len(l): c = a * b if c % l[j] != 0: l.remove(l[j]) if len(l) == 0: print(-1) else: j += 1 if len(l) != 0: print(l[0])
FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 150\,000$) — the number of pairs. Each of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \le a_i, b_i \le 2 \cdot 10^9$). -----Output----- Print a single integer — the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $-1$. -----Examples----- Input 3 17 18 15 24 12 15 Output 6 Input 2 10 16 7 17 Output -1 Input 5 90 108 45 105 75 40 165 175 33 30 Output 5 -----Note----- In the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $1$ satisfying the conditions. In the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output.
q = int(input()) a, b = map(int, input().split()) s = set() for x in [a, b]: if x % 2 == 0: s.add(2) while x % 2 == 0: x //= 2 i = 3 while i * i <= x: if x % i == 0: s.add(i) while x % i == 0: x //= i i += 2 if x != 1: s.add(x) for k in range(q - 1): u, v = map(int, input().split()) s0 = set() for z in s: if u % z == 0 or v % z == 0: s0.add(z) s = s0 if len(s) == 0: print(-1) else: print(min(s))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR LIST VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 150\,000$) — the number of pairs. Each of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \le a_i, b_i \le 2 \cdot 10^9$). -----Output----- Print a single integer — the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $-1$. -----Examples----- Input 3 17 18 15 24 12 15 Output 6 Input 2 10 16 7 17 Output -1 Input 5 90 108 45 105 75 40 165 175 33 30 Output 5 -----Note----- In the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $1$ satisfying the conditions. In the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output.
def divisors(a, b): Divisors = [] i = 2 while i * i <= a: if a % i == 0: Divisors.append(i) while a % i == 0: a /= i i += 1 i = 2 while i * i <= b: if b % i == 0: if i not in Divisors: Divisors.append(i) while b % i == 0: b /= i i += 1 if a != 1: Divisors.append(int(a)) if b != 1: Divisors.append(int(b)) return Divisors def main(): n = int(input()) A = list() for i in range(0, n): b = list(map(int, input().split())) A += b Prime = divisors(A[0], A[1]) ans = -1 for i in range(0, len(Prime)): Ok = 1 for j in range(1, n): if A[j * 2] % Prime[i] != 0 and A[2 * j + 1] % Prime[i] != 0: Ok = 0 break if Ok != 0: ans = Prime[i] break print(ans) main()
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 150\,000$) — the number of pairs. Each of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \le a_i, b_i \le 2 \cdot 10^9$). -----Output----- Print a single integer — the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $-1$. -----Examples----- Input 3 17 18 15 24 12 15 Output 6 Input 2 10 16 7 17 Output -1 Input 5 90 108 45 105 75 40 165 175 33 30 Output 5 -----Note----- In the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $1$ satisfying the conditions. In the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output.
def findPF(n, pf): if n <= 1: return i = 2 while i * i <= n: if n % i == 0: pf.add(i) while n > 1 and n % i == 0: n = n // i i += 1 if n > 1: pf.add(n) n = int(input()) a, b = map(int, input().split()) pf = set() findPF(a, pf) findPF(b, pf) for i in range(n - 1): a, b = map(int, input().split()) for s in list(pf): if a % s != 0 and b % s != 0: pf.remove(s) if len(pf) >= 1: print(pf.pop()) else: print(-1)
FUNC_DEF IF VAR NUMBER RETURN ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 150\,000$) — the number of pairs. Each of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \le a_i, b_i \le 2 \cdot 10^9$). -----Output----- Print a single integer — the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $-1$. -----Examples----- Input 3 17 18 15 24 12 15 Output 6 Input 2 10 16 7 17 Output -1 Input 5 90 108 45 105 75 40 165 175 33 30 Output 5 -----Note----- In the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $1$ satisfying the conditions. In the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output.
def fact(n): i = 2 arr = set() while i * i <= n: if n % i == 0: arr.add(i) while n % i == 0: n //= i i += 1 if n > 1: arr.add(n) return list(arr) arr = [] for i in range(int(input())): a, b = map(int, input().split()) arr.append([a, b]) a, b = arr[0] prime_dvsrs = list(set(fact(a) + fact(b))) for i in prime_dvsrs: flag = 0 for j in range(1, len(arr)): x, y = arr[j] if x % i != 0 and y % i != 0: flag = 1 break if flag == 0: print(i) break else: print(-1)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
from sys import exit n, m = map(int, input().split()) a, b = 2 * n, 3 * m for i in range(6, 10**10, 6): if a >= i and b >= i: if a <= b: a += 2 else: b += 3 else: break print(max(a, b))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
n, m = map(int, input().split()) i = 0 while i // 2 < n or i // 3 < m or i // 2 + i // 3 - i // 6 < n + m: i += 1 print(i)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
n, m = tuple(map(int, input().split())) a = 2 * n b = 3 * m c = n // 3 d = m // 2 while c > 0 and d > 0: if a > b: b += 3 c -= 1 d -= 1 if b % 6 == 0: d += 1 else: a += 2 c -= 1 d -= 1 if a % 6 == 0: c += 1 print(str(max(a, b)))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
n, m = input().split() n = int(n) m = int(m) res_n = n * 2 res_m = m * 3 tmp_height = 6 while tmp_height <= min(res_n, res_m): tmpresn = res_n + 2 tmpresm = res_m + 3 if max(tmpresn, res_m) > max(res_n, tmpresm): res_m = tmpresm else: res_n = tmpresn tmp_height += 6 print(max(res_m, res_n))
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
read_ints = lambda: map(int, input().split()) def solve(m, n): h2, h3 = 2 * m, 3 * n left = min(h2, h3) // 6 while left > 0: if h2 + 2 < h3 + 3: h2 += 2 if h2 % 3 != 0 or h2 > h3: left -= 1 else: h3 += 3 if h3 % 2 != 0 or h3 > h2: left -= 1 return max(h2, h3) m, n = read_ints() print(solve(m, n))
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
n, m = list(map(int, input().split())) def test(k): return k // 2 >= n and k // 3 >= m and k // 2 + k // 3 - k // 6 >= n + m lo = 0 hi = 6 * (n + m) while hi - lo > 1: mid = (hi + lo) // 2 if test(mid): hi = mid else: lo = mid print(hi)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
import sys n, m = list(map(int, input().split())) i = k2 = k3 = k6 = 0 while True: i += 1 if i % 6 == 0: k6 += 1 else: if i % 3 == 0: k3 += 1 if i % 2 == 0: k2 += 1 k2 = min(n, k2) k3 = min(m, k3) need = n - k2 + m - k3 if need <= k6: print(i) return
IMPORT ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER WHILE NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
import sys def main(): n, m = [int(f) for f in sys.stdin.readline().split()] h = max(2 * n, 3 * m) n_common = h // 6 n3 = h // 3 - n_common n2 = h // 2 - n_common if n3 + n2 + n_common >= m + n: res = h else: while n3 + n2 + n_common < m + n: h += 1 if h % 2 != 0 and h % 3 != 0: continue n_common = h // 6 n3 = h // 3 - n_common n2 = h // 2 - n_common res = h print(res) main()
IMPORT FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR WHILE BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
a, b = list(map(int, input().split(" "))) lo = 1 hi = 1000000000 while lo < hi: mid = (lo + hi) // 2 x = mid // 2 y = mid // 3 z = mid // 6 x -= z y -= z t = 0 if x <= a: t += a - x if y <= b: t += b - y if t > z: lo = mid + 1 else: hi = mid print(lo)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
n, m = map(int, input().split()) l = 0 r = 10**9 while r - l > 1: mid = (r + l) // 2 c2 = mid // 2 c3 = mid // 3 c6 = mid // 6 cnt2 = c2 - c6 cnt3 = c3 - c6 if cnt2 < n: c6 -= n - cnt2 if cnt3 < m: c6 -= m - cnt3 if c6 >= 0: r = mid else: l = mid print(r)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR 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 VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
n, m = map(int, input().split()) ans = max(2 * n, 3 * m, (3 * (n + m) + 1) // 2) - 1 if ans // 2 < n or ans // 3 < m or ans // 2 + ans // 3 - ans // 6 < n + m: ans += 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
n, m = map(int, input().split()) max_m = m * 3 busy_by_m = int(m * 3 / 6) max_n = (n + busy_by_m) * 2 * bool(n) nd = 0 md = 0 maxv = 0 if max_n > max_m: nd = -2 md = +3 max_n_prev = max_n max_m_prev = max_m for i in range(busy_by_m): max_n_prev = max_n max_m_prev = max_m max_n += nd max_m += md if max_m % 2 == 0: max_m += md if max(max_n_prev, max_m_prev) < max(max_n, max_m): break maxv = min(max(max_n_prev, max_m_prev), max(max_n, max_m)) else: maxv = max_m print(maxv)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
def condn(x, n, m): return not (x // 2 >= n and x // 3 >= m and x // 2 + x // 3 - x // 6 >= n + m) n, m = map(int, input().split()) x = 1 while condn(x, n, m): x += 1 print(x)
FUNC_DEF RETURN BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
n, m = map(int, input().split()) x = [(6 * (i + 1)) for i in range(m + n)] mt, mth = 2 * n, 3 * m for i in x: t = 2 * max(0, 1 + (mt - i) // 2) th = 3 * max(0, 1 + (mth - i) // 2) if t == 0 or th == 0: break if th < t: mth += 3 else: mt += 2 print(max(mth, mt))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR FOR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR NUMBER BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR NUMBER BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
s = input().split() n, m = s n = int(n) m = int(m) n *= 2 m *= 3 i = 1 while True: if n <= m: if i * 6 <= n: n += 2 else: break elif i * 6 <= m: m += 3 else: break i += 1 print(max(n, m))
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
n, m = map(int, input().split()) start = 0 end = 10**10 while end - start > 1: mid = (end + start) // 2 two = mid // 2 - mid // 6 three = mid // 3 - mid // 6 six = mid // 6 nn = n mm = m nn -= two mm -= three nn = max(nn, 0) mm = max(mm, 0) if six >= nn + mm: end = mid else: start = mid print(end)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
def check_2(n, m, x): two_mul = x - x // 3 three_mul = 2 * x // 3 - x // 3 six_mul = x // 3 return (n - two_mul if n >= two_mul else 0) + ( m - three_mul if m >= three_mul else 0 ) <= six_mul def check_3(n, m, x): three_mul = x - x // 2 two_mul = 3 * x // 2 - x // 2 six_mul = x // 2 return (n - two_mul if n >= two_mul else 0) + ( m - three_mul if m >= three_mul else 0 ) <= six_mul def main(): n, m = [int(x) for x in input().split()] func_list = [(check_2, 2), (check_3, 3)] ans = 1061109567 for func, mul in func_list: l = 0 r = 10000000 while l <= r: mid = l + r >> 1 if func(n, m, mid): ans = min(ans, mul * mid) r = mid - 1 else: l = mid + 1 print(ans) main()
FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
import time n, m = (int(i) for i in input().split()) start = time.time() h2 = 2 * n h3 = 3 * m k = 6 while k <= min(h2, h3): if h2 + 2 < h3 + 3: h2 += 2 else: h3 += 3 k += 6 print(max(h2, h3)) finish = time.time()
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
from itertools import count n, m = input().split() n, m = int(n), int(m) for x in count(0): if x // 2 >= n and x // 3 >= m and x // 2 + x // 3 - x // 6 >= n + m: print(x) break
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
n, m = map(int, input().split()) num = max(n * 2, m * 3) while True: if num % 2 == 0 or num % 3 == 0: x = num // 2 y = num // 3 z = num // 6 if x >= n and y >= m and x + y - z >= m + n: print(num) exit() num += 1
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
def check(n2, n3, k): q2 = k // 2 q3 = k // 3 q6 = k // 6 q2 -= q6 q3 -= q6 lo = n2 - q2 hi = q3 + q6 - n3 return lo <= hi and lo <= q6 and 0 <= hi def go(n2, n3): lo = -1 hi = int(60000000.0) while lo + 1 < hi: k = (lo + hi) // 2 if check(n2, n3, k): hi = k else: lo = k return hi n2, n3 = map(int, input().split()) print(go(n2, n3))
FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR VAR VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
import sys n, m = sys.stdin.readline().split() n = int(n) m = int(m) if n == 0: print(m * 3) exit(0) elif m == 0: print(n * 2) exit(0) twos = [] cur2 = 2 for i in range(n): twos.append(cur2) cur2 += 2 if cur2 % 6 == 0: cur2 += 2 threes = [] cur3 = 3 for i in range(m): threes.append(cur3) cur3 += 6 ans = 0 ans = max(twos[-1], threes[-1]) for ts in range(n): twolim = max(twos[-(ts + 1)], ts * 6) first_six = (ts + 1) * 6 beg = 0 end = m while end - beg > 1: mid = (end + beg) // 2 if threes[mid] < first_six: beg = mid else: end = mid if end == m: threelim = threes[-1] else: threelim = threes[beg] + (m - beg - 1) * 3 oans = ans ans = min([ans, max([twolim, threelim])]) print(ans)
IMPORT ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
def __starting_point(): d, t = list(map(int, input().split(" "))) result = 0 i2 = d * 2 i3 = t * 3 cm = int(max(i2, i3) / 6) while cm > 0: if i2 + 2 < i3 + 3: i2 += 2 elif i2 + 2 > i3 + 3: i3 += 3 else: i2 += 2 cm += 1 cm -= 1 print(max(i2, i3)) __starting_point()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
n, m = map(int, input().split()) left = 0 right = 10000000 while right - left != 1: mid = (left + right) // 2 all_places = mid // 2 + mid // 3 - mid // 6 if not (2 * n <= mid and 3 * m <= mid and n + m <= all_places): left = mid else: right = mid print(right)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
t2, t3 = list(map(int, input().split(" "))) left = 2 * t2 - 10 right = 3 * (t2 + t3) + 10 while right - left != 1: guess = int((left + right) / 2) work2 = int(guess / 2) - int(guess / 6) work3 = int(guess / 3) - int(guess / 6) work6 = int(guess / 6) if max(0, t2 - work2) + max(0, t3 - work3) <= work6: right = guess else: left = guess print(right)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
n, m = list(map(int, input().split())) answer = 0 while True: if n >= 3 and m >= 2: if n / m > 3 / 2: n -= 3 m -= 1 else: n -= 2 m -= 2 answer += 6 elif m == 1: answer += max(3, 2 * n) break elif m == 0: answer += 2 * n break else: answer += 3 * m break print(answer)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR IF VAR NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
n, m = list(map(int, input().split())) maxn = 0 for i in range(n): maxn += 2 if maxn % 3 == 0: maxn += 2 maxm = m * 6 - 3 now = 6 while now < max(maxm, maxn): if maxm >= maxn: maxm -= 3 if maxm % 2 == 0 and maxm - 3 > now: maxm -= 3 else: maxn -= 2 if maxn % 3 == 0 and maxn - 2 > now: maxn -= 2 now += 6 print(max(maxm, maxn))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
def right(m, n, a): b = True n2 = a // 2 - a // 6 n3 = a // 3 - a // 6 n6 = a // 6 if n2 + n6 < n or n3 + n6 < m or n2 + n3 + n6 < n + m: b = False return b n, m = list(map(int, input().split())) ans = n + m while not right(m, n, ans): ans += 1 print(ans)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
n, m = map(int, input().split()) low = 1 high = 10**9 def p(x): t1 = x // 2 t2 = x // 3 if t1 >= n and t2 >= m: t3 = x // 6 return t1 + t2 - t3 >= m + n return False while low < high: mid = low + (high - low) // 2 if p(mid): high = mid else: low = mid + 1 print(low)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR RETURN NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
n, m = map(int, input().split()) nbcom = min(n * 2, 3 * m) // 6 res1 = 2 * n res2 = 3 * m while nbcom: if res1 + 2 <= res2 + 3: res1 += 2 if res1 % 6 != 0 or res1 > res2: nbcom -= 1 else: res2 += 3 if res2 % 6 != 0 or res2 > res1: nbcom -= 1 print(max(res1, res2))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR WHILE VAR IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
n, m = [int(x) for x in input().split()] h1 = lambda k: 6 * ((k - 1) // 2) + 2 * ((k - 1) % 2 + 1) if k > 0 else 0 h2 = lambda k: 3 + (k - 1) * 6 if k > 0 else 0 h3 = lambda l: 6 * k newx = lambda k: k - 2 if k % 6 == 4 else k - 4 newy = lambda k: k - 6 x, y, z = h1(n), h2(m), 0 while max(x, y) > z + 6: z += 6 if x > y: x = newx(x) else: y = newy(y) print(max(x, y, z))
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
def main(): n2, n3 = map(int, input().split()) nmax = 3 * max(n2, n3) * 2 c2 = c3 = c6 = 0 for n in range(1, nmax): if n % 6 == 0: c6 += 1 elif c3 < n3 and n % 3 == 0: c3 += 1 elif c2 < n2 and n % 2 == 0: c2 += 1 if n2 - c2 + n3 - c3 - c6 == 0: print(n) break main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
nm = input().split(" ") n = int(nm[0]) m = int(nm[1]) if n == 0: print(3 * m) elif m == 0: print(2 * n) else: new = max(3 * m, 2 * n) i = 0 while i < new + 1: if i % 6 == 0 and i != 0: if i // 3 <= m and i // 2 <= n and 2 * (n + 1) - new < 3 * (m + 1) - new: n += 1 elif i // 3 <= m and i // 2 <= n and 2 * (n + 1) - new >= 3 * (m + 1) - new: m += 1 new = max(3 * m, 2 * n) i += 1 print(max(3 * m, 2 * n))
ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
def check(x): return ( max(n - (x - 2) // 6 - (x - 4) // 6 - 2, 0) + max(m - (x - 3) // 6 - 1, 0) <= x // 6 ) n, m = list(map(int, input().split())) left, right = 0, 10**10 while left + 1 < right: middle = left + right >> 1 if check(middle): right = middle else: left = middle print(right)
FUNC_DEF RETURN BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP NUMBER NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
n, m = map(int, input().split()) a, b, i = 2 * n, 3 * m, 6 while i <= min(a, b): if b < a: b += 3 else: a += 2 i += 6 print(max(a, b))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
n, m = map(int, input().split()) k = n + m a = [6, 3][m % 2] + 6 * ((m - 1) // 2) b = [6, 2, 4][n % 3] + 6 * ((n - 1) // 3) c = [6, 2, 3, 4][k % 4] + 6 * ((k - 1) // 4) print(a if n < m else b if n > 3 * m else c)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. -----Input----- The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. -----Output----- Print a single integer, denoting the minimum possible height of the tallest tower. -----Examples----- Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 -----Note----- In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
def check(n, m, ans): div2 = ans // 2 div3 = ans // 3 div6 = ans // 6 k = 0 if div2 < n or div3 < m: return False if div2 - div6 < n: k = n - (div2 - div6) return div3 - k >= m n, m = list(map(int, input().split())) l = -1 r = max(n * 6, m * 6) + 1 while l < r - 1: mid = (l + r) // 2 if check(n, m, mid): r = mid else: l = mid print(r)
FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) — the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) — the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) — the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys read = sys.stdin.buffer.read input = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines t = int(input()) for i in range(t): n = int(input()) a = [] b = [] for j in range(n): p, q = map(int, input().split()) a.append(p) b.append(q) s = 0 m = 1000000000000000000000 for j in range(n): s += max(0, a[j] - b[j - 1]) m = min(a[j], b[j - 1], m) print(s + m)
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 LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) — the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) — the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) — the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
from sys import stdin input = stdin.buffer.readline for _ in range(int(input())): n = int(input()) a, b = map(int, input().split()) l = a z = 0 su = 0 t = b q = float("-infinity") for i in range(1, n): a, b = map(int, input().split()) f = max(0, a - t) q = max(q, f - a) t = b su += f z = max(0, l - t) su += z q = max(q, z - l) print(su - q)
ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) — the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) — the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) — the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline def main(): t = int(input()) for _ in range(t): n = int(input()) hp, dmg = [], [] for _ in range(n): a, b = map(int, input().split()) hp.append(a) dmg.append(b) for i in range(n): dmg[i - 1] = min(hp[i], dmg[i - 1]) print(sum(hp) - sum(dmg) + min(dmg)) main()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) — the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) — the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) — the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline for _ in range(int(input())): n = int(input()) a = [(0) for i in range(n)] b = [(0) for i in range(n)] res = 0 for i in range(n): a[i], b[i] = map(int, input().split()) first = 10**15 for i in range(1, n): res += max(0, a[i] - b[i - 1]) first = min(first, min(a[i], b[i - 1])) res += max(0, a[0] - b[n - 1]) first = min(first, min(a[0], b[n - 1])) print(res + first)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) — the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) — the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) — the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline for _ in range(int(input())): n = int(input()) arr = [] ans = 0 m = 10**20 for i in range(n): arr.append(list(map(int, input().split()))) if i > 0: k = max(0, arr[i][0] - arr[i - 1][1]) ans += k m = min(m, arr[i][0] - k) k = max(0, arr[0][0] - arr[n - 1][1]) ans += k m = min(m, arr[0][0] - k) ans += m print(ans)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) — the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) — the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) — the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.read class GetInt: def __init__(self): self.nums = list(map(int, input().split())) self.n_idx = -1 def get(self): self.n_idx += 1 return self.nums[self.n_idx] inp = GetInt() T = inp.get() fans = [] for _ in range(T): ans = 0 A = [] B = [] n = inp.get() for i in range(n): a, b = inp.get(), inp.get() A.append(a) B.append(b) for i in range(n): B[i] = min(B[i], A[(i + 1) % n]) for i in range(n): ans += A[i] - B[(i - 1 + n) % n] fans.append(str(ans + min(B))) print("\n".join(fans))
IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) — the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) — the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) — the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys def solve(): input = sys.stdin.buffer.readline t = int(input()) while t > 0: n = int(input()) a = [] b = [] cnt = 0 mn = float("inf") for i in range(n): x, y = map(int, input().split()) a.append(x) b.append(y) for i in range(n): extra = max(0, a[i] - b[i - 1]) mn = min(mn, a[i] - extra) cnt += extra cnt += mn sys.stdout.write(str(cnt) + "\n") t -= 1 solve()
IMPORT FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) — the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) — the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) — the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline testCases = int(input()) for test in range(testCases): n = int(input()) a = [] b = [] for i in range(n): aa, bb = map(int, input().split()) a.append(aa) b.append(bb) ab = [0] * n for i in range(n): ab[i] = max(0, a[i] - b[i - 1]) s = sum(ab) res = s - ab[0] + a[0] for i in range(1, n): if res > s - ab[i] + a[i]: res = s - ab[i] + a[i] print(res)
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 LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) — the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) — the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) — the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline def I(): return list(map(int, input().split())) def sieve(n): a = [1] * n for i in range(2, n): if a[i]: for j in range(i * i, n, i): a[j] = 0 return a for __ in range(int(input())): n = int(input()) s = 0 a = [0] * n b = [0] * n extraForStart = 10000000000000 for i in range(n): a[i], b[i] = I() for i in range(n): s += max(0, a[i] - b[i - 1]) extraForStart = min(extraForStart, a[i] - max(0, a[i] - b[i - 1])) print(s + extraForStart)
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) — the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) — the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) — the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
from sys import stdin t = int(stdin.buffer.readline()) for _ in range(t): n = int(stdin.buffer.readline()) a = list() b = list() for _ in range(n): x, y = map(int, stdin.buffer.readline().split()) a.append(x) b.append(y) Sum = 0 res = float("inf") for i in range(n): d = a[i] - b[(i - 1 + n) % n] if a[i] >= b[(i - 1 + n) % n] else 0 Sum += d res = min(res, a[i] - d) print(Sum + res)
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 ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) — the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) — the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) — the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline t = int(input()) for _ in range(t): n = int(input()) a = [] b = [] for __ in range(n): x, y = map(int, input().split()) a.append(x) b.append(y) suma = 0 for i in range(n): suma += max(a[i] - b[(i - 1) % n], 0) mini = a[0] + suma - max(a[0] - b[-1 % n], 0) for i in range(n): mini = min(mini, a[i] + suma - max(a[i] - b[(i - 1) % n], 0)) print(mini)
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 LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR