source
stringclasses
4 values
task_type
stringclasses
1 value
in_source_id
stringlengths
0
138
problem
stringlengths
219
13.2k
gold_standard_solution
stringlengths
0
413k
problem_id
stringlengths
5
10
metadata
dict
verification_info
dict
taco
verifiable_code
https://www.codechef.com/IC32016/problems/HBB
Solve the following coding problem using the programming language python: Humpy, the little elephant, has his birthday coming up. He invited all his cousins but doesn’t know how many of them are really coming as some of them are having exams coming up. He will only get to know how many of them are coming on the day of his birthday. He ordered sugarcane for his party, of length L. Humpy’s mom decided that she will be dividing the sugarcane among Humty and his friends in a way such that they get the sugarcane in ratio of their ages. Your task is to determine whether it is possible to serve sugarcane to everyone as integral multiples of their ages. -----INPUT----- First line of input contains an integer N, denoting the number of test cases. Then N test cases follow. The first line of each test case contains three integers K, L and E. K denoting the number of friends coming; L denoting the length of the sugarcane and E denoting the age of the little elephant. Next line has K space separated integers denoting the age of friends who came to the party. -----OUTPUT----- For each test case, output “YES” (without quotes) if everyone gets their part as integral multiples of their ages; otherwise output “NO”(without quotes). -----CONSTRAINTS----- - 1 <= T<=30 - 1 <= K<=1000 - 1 <= L<=1000000 - 1 <= E<=100000 - 1 <= Age of Cousins<=100000 -----Example----- Input: 2 4 10 2 2 2 3 1 4 12 3 6 5 7 3 Output: YES NO The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from sys import stdin, stdout n = int(stdin.readline()) while n: n -= 1 (k, l, e) = map(int, stdin.readline().strip().split(' ')) a = map(int, stdin.readline().strip().split(' ')) x = float(l) / float(e + sum(a)) if x - int(x): stdout.write('NO\n') else: stdout.write('YES\n') ```
vfc_145136
{ "difficulty": "unknown_difficulty", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://www.codechef.com/IC32016/problems/HBB", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\n4 10 2\n2 2 3 1\n4 12 3\n6 5 7 3\n", "output": "YES\nNO\n", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://www.codechef.com/problems/RRTREE
Solve the following coding problem using the programming language python: You are given a tree that is built in a following way: initially there is single vertex 1. All the other vertices are added one by one, from vertex 2 to vertex N, by connecting it to one of those that have been added before. You are to find the diameter of the tree after adding each vertex. Let the distance between vertex u and v be the minimal number of edges you have to pass to get from u to v, then diameter is the maximum distance between any two pairs (u,v) that have already been added to the tree. ------ Input ------ The input consists of several Test cases. The file line of input contains an integer T, the number of test cases. T test cases follow. The first line of each test case contains an integer N, then N-1 lines follow: ith line contains an integer P_{i}, which means that vertex i+1 is connected to the vertex P_{i}. ------ Output ------ For each test case, output N-1 lines - the diameter after adding vertices 2, 3,..,N ------ Constraints ------ 1 ≤ T ≤ 15 1 ≤ N ≤ 10^{5} P_{i} ≤ i Sum of N over all test cases in a file does not exceed 2 * 10^{5} ------ Example ------ Input: 2 3 1 1 5 1 2 3 3 Output: 1 2 1 2 3 3 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def update(M, level, u, v): level[u] = level[v] + 1 M[u][0] = v for j in range(1, 18): if M[u][j - 1]: M[u][j] = M[M[u][j - 1]][j - 1] def LCA(M, level, u, v): if u == v: return u if level[u] < level[v]: (u, v) = (v, u) for i in range(17, -1, -1): if M[u][i] and level[M[u][i]] >= level[v]: u = M[u][i] if u == v: return u for i in range(17, -1, -1): if M[u][i] and M[u][i] != M[v][i]: u = M[u][i] v = M[v][i] return M[u][0] def distance(par, level, a, b): return level[a] + level[b] - 2 * level[LCA(par, level, a, b)] def solve(): for _ in range(int(input())): N = int(input()) M = [[0 for _ in range(18)] for _ in range(N + 5)] level = [0] * N (u, v) = (0, 0) results = [] diameter = 0 for i in range(1, N): p = int(input()) update(M, level, i, p - 1) cur = distance(M, level, i, u) if cur > diameter: diameter = cur v = i else: cur = distance(M, level, i, v) if cur > diameter: diameter = cur u = i results.append(diameter) print('\n'.join((str(k) for k in results))) solve() ```
vfc_145144
{ "difficulty": "very_hard", "memory_limit": "50000 bytes", "memory_limit_bytes": null, "problem_url": "https://www.codechef.com/problems/RRTREE", "time_limit": "2 seconds" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\n3\n1\n1\n5\n1\n2\n3\n3", "output": "1\n2\n1\n2\n3\n3", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://codeforces.com/problemset/problem/641/D
Solve the following coding problem using the programming language python: Little Artyom decided to study probability theory. He found a book with a lot of nice exercises and now wants you to help him with one of them. Consider two dices. When thrown each dice shows some integer from 1 to n inclusive. For each dice the probability of each outcome is given (of course, their sum is 1), and different dices may have different probability distributions. We throw both dices simultaneously and then calculate values max(a, b) and min(a, b), where a is equal to the outcome of the first dice, while b is equal to the outcome of the second dice. You don't know the probability distributions for particular values on each dice, but you know the probability distributions for max(a, b) and min(a, b). That is, for each x from 1 to n you know the probability that max(a, b) would be equal to x and the probability that min(a, b) would be equal to x. Find any valid probability distribution for values on the dices. It's guaranteed that the input data is consistent, that is, at least one solution exists. -----Input----- First line contains the integer n (1 ≤ n ≤ 100 000) — the number of different values for both dices. Second line contains an array consisting of n real values with up to 8 digits after the decimal point  — probability distribution for max(a, b), the i-th of these values equals to the probability that max(a, b) = i. It's guaranteed that the sum of these values for one dice is 1. The third line contains the description of the distribution min(a, b) in the same format. -----Output----- Output two descriptions of the probability distribution for a on the first line and for b on the second line. The answer will be considered correct if each value of max(a, b) and min(a, b) probability distribution values does not differ by more than 10^{ - 6} from ones given in input. Also, probabilities should be non-negative and their sums should differ from 1 by no more than 10^{ - 6}. -----Examples----- Input 2 0.25 0.75 0.75 0.25 Output 0.5 0.5 0.5 0.5 Input 3 0.125 0.25 0.625 0.625 0.25 0.125 Output 0.25 0.25 0.5 0.5 0.25 0.25 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def tle(): k = 0 while k >= 0: k += 1 def quad(a, b, c): disc = b ** 2 - 4 * a * c if disc < 0: disc = 0 disc = disc ** 0.5 return ((-b + disc) / 2 / a, (-b - disc) / 2 / a) x = int(input()) y = list(map(float, input().strip().split(' '))) z = list(map(float, input().strip().split(' '))) py = [0, y[0]] for i in range(1, x): py.append(py[-1] + y[i]) z.reverse() pz = [0, z[0]] for i in range(1, x): pz.append(pz[-1] + z[i]) pz.reverse() k = [] for i in range(0, x + 1): k.append(py[i] + 1 - pz[i]) l = [0] for i in range(x): l.append(k[i + 1] - k[i]) s1 = 0 s2 = 0 avals = [] bvals = [] for i in range(1, x + 1): (a, b) = quad(-1, s1 + l[i] - s2, (s1 + l[i]) * s2 - py[i]) if b < 0 or l[i] - b < 0: (a, b) = (b, a) if a < 0 and b < 0: a = 0 b = 0 bvals.append(b) avals.append(l[i] - b) s1 += avals[-1] s2 += bvals[-1] for i in range(len(avals)): if abs(avals[i]) <= 10 ** (-10): avals[i] = 0 if abs(bvals[i]) <= 10 ** (-10): bvals[i] = 0 print(' '.join([str(i) for i in avals])) print(' '.join([str(i) for i in bvals])) ```
vfc_145148
{ "difficulty": "very_hard", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/641/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\n0.25 0.75\n0.75 0.25\n", "output": "0.5 0.5 \n0.5 0.5 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n0.125 0.25 0.625\n0.625 0.25 0.125\n", "output": "0.25 0.25 0.5 \n0.5 0.25 0.25 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n0.01 0.01 0.01 0.01 0.01 0.1 0.2 0.2 0.4 0.05\n1.0 0 0 0 0 0 0 0 0 0\n", "output": "0.010000000000000009 0.010000000000000009 0.010000000000000009 0.009999999999999953 0.010000000000000009 0.10000000000000003 0.2 0.1999999999999999 0.39999999999999825 0.05000000000000182 \n1.0 0.0 0.0 0.0 0.0 -1.1102230246251565E-16 1.1102230246251565E-16 0.0 1.9984014443252818E-15 -1.9984014443252818E-15 \n", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://codeforces.com/problemset/problem/1374/E1
Solve the following coding problem using the programming language python: Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ — the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ — the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ — the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python (n, k) = map(int, input().split()) a = [] b = [] both = [] for _ in range(n): (x, y, z) = map(int, input().split()) if y == 1 and z == 1: both.append(x) elif y == 1: a.append(x) elif z == 1: b.append(x) a.sort() b.sort() for i in range(min(len(a), len(b))): both.append(a[i] + b[i]) both.sort() if len(both) < k: print(-1) else: print(sum(both[:k])) ```
vfc_145152
{ "difficulty": "medium_hard", "memory_limit": "256 megabytes", "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1374/E1", "time_limit": "2 seconds" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "output": "18\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "output": "8\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 1\n3 0 1\n3 1 0\n3 0 0\n", "output": "6\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 1\n7 1 1\n2 1 1\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "output": "2\n", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://www.codechef.com/problems/MISREP
Solve the following coding problem using the programming language python: You are given an array A consisting of N integers. In one operation, you can: Choose any two indices i and j (i \neq j); Subtract min(A_{i} , A_{j}) from both A_{i} and A_{j}. Note that min(A_{i} , A_{j}) denotes the minimum of A_{i} and A_{j}. Determine whether you can make all the elements of the array equal to zero in less than or equal to N operations. If it is possible to do so, output the required operations as well. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N, denoting number of elements in A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case, if it possible to make all the elements of the array equal to zero in less than or equal to N operations, print K+1 lines: - In the first line, print K (1≤ K ≤ N), the required operations. - In each of the next K lines, print two space-separated integers i and j, denoting the operation. In case it is not possible to make all elements equal to 0, print a single line containing -1. ------ Constraints ------ $1 ≤ T ≤ 50$ $2 ≤ N ≤ 300$ $1 ≤ A_{i} ≤ 300$ - The sum of $N$ over all test cases does not exceed $600$. ----- Sample Input 1 ------ 3 2 1 1 3 1 3 1 4 1 3 1 3 ----- Sample Output 1 ------ 1 1 2 -1 2 1 3 2 4 ----- explanation 1 ------ Test case $1$: We can pick $i = 1$ and $j = 2$ and subtract $min(A_{1}, A_{2}) = 1$ from both $A_{1}$ and $A_{2}$. Thus, the final array is $[0 , 0]$. Test case $2$: It can be proven that there is no way to make all elements of the array equal to zero. Test case $3$: We can perform $2$ operations in the following way: - Pick $i = 1$ and $j = 3$ and subtract $min(A_{1}, A_{3}) = 1$ from both $A_{1}$ and $A_{3}$. Thus, the obtained array is $[0, 3, 0, 3]$. - Pick $i = 2$ and $j = 4$ and subtract $min(A_{2}, A_{4}) = 3$ from both $A_{2}$ and $A_{4}$. Thus, the obtained array is $[0, 0, 0, 0]$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python for _ in range(int(input())): n = int(input()) a = [int(x) for x in input().split()] assert len(a) == n assert all((x > 0 for x in a)) sm = sum(a) if sm % 2 != 0: print(-1) continue ps_sm = {0: []} for (i, x) in enumerate(a): ps_sm_new = ps_sm.copy() for (v, indices) in ps_sm.items(): if v + x not in ps_sm_new: ps_sm_new[v + x] = indices + [i] ps_sm = ps_sm_new if sm // 2 not in ps_sm: print(-1) continue indices_1 = ps_sm[sm // 2] indices_2 = [i for i in range(len(a)) if i not in indices_1] vals_1 = [[a[i], i] for i in indices_1] vals_2 = [[a[i], i] for i in indices_2] ops = [] while len(vals_1) != 0: ops.append([vals_1[0][1], vals_2[0][1]]) min_v = min(vals_1[0][0], vals_2[0][0]) vals_1[0][0] -= min_v vals_2[0][0] -= min_v if vals_1[0][0] == 0: del vals_1[0] if vals_2[0][0] == 0: del vals_2[0] assert len(vals_2) == 0 print(len(ops)) for (x, y) in ops: print(x + 1, y + 1) ```
vfc_145156
{ "difficulty": "very_hard", "memory_limit": "50000 bytes", "memory_limit_bytes": null, "problem_url": "https://www.codechef.com/problems/MISREP", "time_limit": "1 seconds" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n2\n1 1 \n3 \n1 3 1\n4\n1 3 1 3\n", "output": "1\n1 2\n-1\n2\n1 3\n2 4", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://www.codechef.com/AARA2018/problems/ARMBH4
Solve the following coding problem using the programming language python: Ted$Ted$ loves prime numbers. One day he is playing a game called legendary$legendary$ with his girlfriend Robin$Robin$. Ted$Ted$ writes a number N$N$ on a table and the number is in the form of : N = P1A1 * P2A2 * ……….. * PnAn Ted$Ted$ asks Robin$Robin$ to find the sum of all the numbers which are less than or equal to N$N$ and also contains all the primes whose minimum power in the number is given by an array B$B$. As Robin$Robin$ is bad with maths she asks your help to answer this question. -----Input:----- - The first line of the input contains a single integer T$T$ denoting the number of test cases. The description of T$T$ test cases follows. - The first line of each test case contains a single integer n$n$, represents a number of distinct prime numbers. - The second line of each test case contains n$n$ space separated distinct prime numbers which represents an array P$P$. - The third line of each test case contains n$n$ space separated integers which represents an array A$A$. - The fourth line of each test case contains n$n$ space separated integers which represents an array B$B$ -----Output:----- For each test case, output the Answer Modulo 109 + 7 in a single line. -----Constraints----- - 1≤T≤3$1 \leq T \leq 3$ - 1≤n≤105$1 \leq n \leq 10^5$ - 2≤Pi≤106$2 \leq P_{i} \leq 10^6$ - 1≤Ai≤109$1 \leq A_{i} \leq 10^9$ - 0≤Bi≤Ai$0 \leq B_{i} \leq Ai$ -----Sample Input:----- 1 3 2 3 5 2 1 2 1 1 1 -----Sample Output:----- 540 -----EXPLANATION:----- 22 * 31 * 52= 300 which is N over here. The four numbers less than or equal to 300 are 30, 60, 150 and 300. 30 = 21 * 31 * 51 , 60 = 22 * 31 * 51, 150 = 21 * 31 * 52 and 300 = 22 * 31 * 52. In the 4 numbers, the minimum powers for 2, 3 and 5 are 1, 1 and 1 or more than them in every case. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python d = 10 ** 9 + 7 t = int(input()) while t: t -= 1 n = int(input()) p = list(map(int, input().strip().split())) a = list(map(int, input().strip().split())) b = list(map(int, input().strip().split())) ans = 1 for i in range(n): c = a[i] - b[i] + 1 tmp = pow(p[i], b[i], d) * ((pow(p[i], c, d) - 1 + d) % d) * pow(p[i] - 1, d - 2, d) % d ans *= tmp ans = ans % d print(ans) ```
vfc_145160
{ "difficulty": "unknown_difficulty", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://www.codechef.com/AARA2018/problems/ARMBH4", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1\n3\n2 3 5\n2 1 2\n1 1 1\n", "output": "540\n", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://codeforces.com/problemset/problem/828/B
Solve the following coding problem using the programming language python: Polycarp has a checkered sheet of paper of size n × m. Polycarp painted some of cells with black, the others remained white. Inspired by Malevich's "Black Square", Polycarp wants to paint minimum possible number of white cells with black so that all black cells form a square. You are to determine the minimum possible number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. The square's side should have positive length. -----Input----- The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the sizes of the sheet. The next n lines contain m letters 'B' or 'W' each — the description of initial cells' colors. If a letter is 'B', then the corresponding cell is painted black, otherwise it is painted white. -----Output----- Print the minimum number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. If it is impossible, print -1. -----Examples----- Input 5 4 WWWW WWWB WWWB WWBB WWWW Output 5 Input 1 2 BB Output -1 Input 3 3 WWW WWW WWW Output 1 -----Note----- In the first example it is needed to paint 5 cells — (2, 2), (2, 3), (3, 2), (3, 3) and (4, 2). Then there will be a square with side equal to three, and the upper left corner in (2, 2). In the second example all the cells are painted black and form a rectangle, so it's impossible to get a square. In the third example all cells are colored white, so it's sufficient to color any cell black. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def min_squares(canvas, length, width): def find_top(): for i in range(length): for j in range(width): if canvas[i][j] == 'B': return i return -1 def find_left(): for j in range(width): for i in range(length): if canvas[i][j] == 'B': return j def find_bottom(): for i in reversed(range(length)): for j in range(width): if canvas[i][j] == 'B': return i def find_right(): for j in reversed(range(width)): for i in range(length): if canvas[i][j] == 'B': return j t = find_top() if t == -1: return 1 l = find_left() b = find_bottom() r = find_right() painted = 0 for i in range(t, b + 1): for j in range(l, r + 1): if canvas[i][j] == 'W': painted += 1 while b - t < r - l: if t > 0: t -= 1 painted += r - l + 1 elif b < length - 1: b += 1 painted += r - l + 1 else: return -1 while r - l < b - t: if l > 0: l -= 1 painted += b - t + 1 elif r < width - 1: r += 1 painted += b - t + 1 else: return -1 return painted (l, w) = map(int, input().split()) canvas = [] for _ in range(l): canvas.append(input()) print(min_squares(canvas, l, w)) ```
vfc_145168
{ "difficulty": "easy", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/828/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5 4\nWWWW\nWWWB\nWWWB\nWWBB\nWWWW\n", "output": "5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 2\nBB\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 3\nWWW\nWWW\nWWW\n", "output": "1\n", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://practice.geeksforgeeks.org/problems/7645a18a9015b17f754b7a7e1c7d70825dde6acb/1
Solve the following coding problem using the programming language python: There is a one-dimensional garden of length N. In each position of the N length garden, a sprinkler has been installed. Given an array a[]such that a[i] describes the coverage limit of the i^{th} sprinkler. A sprinkler can cover the range from the position max(i - a[i], 1) to min(i + a[i], N). In beginning, all the sprinklers are switched off. The task is to find the minimum number of sprinklers needed to be activated such that the whole N-length garden can be covered by water. Note: Array is 1-based indexed. Example 1: Input: a[] = {1, 2, 1} Output: 1 Explanation: For position 1: a[1] = 1, range = 1 to 2 For position 2: a[2] = 2, range = 1 to 3 For position 3: a[3] = 1, range = 2 to 3 Therefore, the fountain at position a[2] covers the whole garden. Therefore, the required output is 1. Example 2: Input: a[] = {2, 1, 1, 2, 1} Output: 2 Explanation: For position 1: a[1] = 2, range = 1 to 3 For position 2: a[2] = 1, range = 1 to 3 For position 3: a[3] = 1, range = 2 to 4 For position 3: a[4] = 2, range = 2 to 5 For position 3: a[5] = 1, range = 4 to 5 Therefore, the fountain at position a[1] and a[4] covers the whole garden. Therefore, the required output is 2. Also possible answer is a[2] and a[4]. Your Task: Your task is to complete the function minSprinkler() which takes an integer array a and an integer N as the input parameters and returns an integer denoting the minimum number of sprinkler needed to be activated such that the whole N-length garden can be covered by water. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 2*10^{5} 1 <= arr[i] <= 10^{9} Write your solution by modifying this code: ```python #User function Template for python3 class Solution(): def minSprinkler(self, arr, N): #your code goes here ``` The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python class Solution: def minSprinkler(self, arr, N): coverages = [-1] * N for (i, a) in enumerate(arr): min_coverage = max(0, i - a) max_coverage = min(i + a, N - 1) coverages[min_coverage] = max(coverages[min_coverage], max_coverage) needed_sprinklers_count = 0 current_coverage = -1 next_max_coverage = -1 for (i, max_cov) in enumerate(coverages): next_max_coverage = max(next_max_coverage, max_cov) if i > current_coverage: needed_sprinklers_count += 1 current_coverage = next_max_coverage return needed_sprinklers_count ```
vfc_145172
{ "difficulty": "medium_hard", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://practice.geeksforgeeks.org/problems/7645a18a9015b17f754b7a7e1c7d70825dde6acb/1", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "a[] = {1, 2, 1}", "output": "1", "type": "stdin_stdout" }, { "fn_name": null, "input": "a[] = {2, 1, 1, 2, 1}", "output": "2", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://practice.geeksforgeeks.org/problems/juggler-sequence3930/1
Solve the following coding problem using the programming language python: Juggler Sequence is a series of integers in which the first term starts with a positive integer number a and the remaining terms are generated from the immediate previous term using the below recurrence relation: Given a number N, find the Juggler Sequence for this number as the first term of the sequence. Example 1: Input: N = 9 Output: 9 27 140 11 36 6 2 1 Explaination: We start with 9 and use above formula to get next terms. Example 2: Input: N = 6 Output: 6 2 1 Explaination: 6^{1/2} = 2. 2^{1/2} = 1. Your Task: You do not need to read input or print anything. Your Task is to complete the function jugglerSequence() which takes N as the input parameter and returns a list of integers denoting the generated sequence. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 100 Write your solution by modifying this code: ```python #User function Template for python3 class Solution: def jugglerSequence(self, N): # code here ``` The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import math class Solution: def jugglerSequence(self, N): if N == 1: return [1] seq = [N] if N % 2 == 0: seq.extend(self.jugglerSequence(int(N ** 0.5))) else: seq.extend(self.jugglerSequence(int(N ** 1.5))) return seq ```
vfc_145173
{ "difficulty": "easy", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://practice.geeksforgeeks.org/problems/juggler-sequence3930/1", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "N = 9", "output": "9 27 140 11 36 6 2 1", "type": "stdin_stdout" }, { "fn_name": null, "input": "N = 6", "output": " 6 2 1", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://www.codechef.com/problems/DISHOWN
Solve the following coding problem using the programming language python: Read problems statements in Mandarin Chinese and Russian. This summer, there is a worldwide competition being held in Chef Town and some of the best chefs of the world are participating. The rules of this competition are quite simple. Each participant needs to bring his or her best dish. The judges will initially assign a score to each of the dishes. Now, several rounds will follow. In each round, any two chefs will be called up on the stage. Each of the chefs can then choose any one dish to battle against the other chef and the one having the dish with the higher score will win this round. The winner of the round will also obtain all the dishes of the loser who will then be eliminated. In case both the dishes have equal scores, this round will be considered as a tie and nothing else will happen. Note that initially each chef will have only one dish and all the chefs play the rounds optimally. Your task is to simulate and answer some queries related to this. You will be given N dishes numbered from 1 to N with the i^{th} dish belonging to the i^{th} chef initially. You will also be given an array S where S[i] denotes the score given by the judges to the i^{th} dish before starting the rounds. You will have to answer Q queries, each of which can be of the following types : 1. 0 x y : This denotes that the chef containing dish number x competes with the chef containing dish number y currently in this round. If a single chef is the owner of both the dishes, print "Invalid query!" (without quotes), otherwise execute and store the result of this round as described by the rules above. 2. 1 x : You need to output the index of the chef containing dish x at this point. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of chefs in the contest. The next line contains N space separated integers where the i^{th} integer represents S[i]. The next line contains an integer Q denoting the number of queries. Q lines follow where each line can be of the format 0 x y or 1 x as described in the problem statement. ------ Output ------ For each test, print in each line the answer for the queries as described in the problem statement . ------ Constraints ------ $ 1 ≤ T ≤ 25 $ $ 1 ≤ N ≤ 10000(10^{4}) $ $ 0 ≤ S[i] ≤ 1000000(10^{6}) $$ 1 ≤ Q ≤ 10000(10^{4}) $$ 1 ≤ x, y ≤ N ----- Sample Input 1 ------ 1 2 1 2 2 0 1 2 1 1 ----- Sample Output 1 ------ 2 ----- explanation 1 ------ There are two chefs with scores of dishes 1 and 2 respectively. After the first query, chef 2 acquires dish 1 since S[2] > S[1] . Hence, the answer for the second query, i.e owner of the first dish is chef 2. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def find(child): new_parent = child while new_parent != parents[new_parent]: new_parent = parents[new_parent] while child != parents[child]: temp = parents[child] parents[child] = new_parent child = temp return new_parent def union(root1, root2): (real_root1, real_root2) = (find(root1), find(root2)) if ratings[real_root1] > ratings[real_root2]: parents[real_root2] = real_root1 elif ratings[real_root1] < ratings[real_root2]: parents[real_root1] = real_root2 for _ in range(int(input().strip())): n = int(input().strip()) parents = list(range(n)) ratings = list(map(int, input().strip().split())) queries = int(input()) for query in range(queries): inp = list(map(int, input().strip().split())) if inp[0] == 0: (root1, root2) = (inp[1] - 1, inp[2] - 1) if find(root1) == find(root2): print('Invalid query!') else: union(root1, root2) else: print(find(inp[1] - 1) + 1) ```
vfc_145174
{ "difficulty": "medium_hard", "memory_limit": "50000 bytes", "memory_limit_bytes": null, "problem_url": "https://www.codechef.com/problems/DISHOWN", "time_limit": "0.5 seconds" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1\n2\n1 2\n2\n0 1 2\n1 1", "output": "2", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n2\n1 3\n2\n0 1 2\n1 1", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n2\n1 1\n2\n0 1 2\n1 1", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n2\n1 3\n2\n0 1 1\n1 1", "output": "Invalid query!\n1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n2\n0 1\n2\n0 2 2\n1 0", "output": "Invalid query!\n2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n4\n0 1\n2\n0 2 2\n1 0", "output": "Invalid query!\n4\n", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://codeforces.com/problemset/problem/507/C
Solve the following coding problem using the programming language python: Amr bought a new video game "Guess Your Way Out!". The goal of the game is to find an exit from the maze that looks like a perfect binary tree of height h. The player is initially standing at the root of the tree and the exit from the tree is located at some leaf node. Let's index all the leaf nodes from the left to the right from 1 to 2^{h}. The exit is located at some node n where 1 ≤ n ≤ 2^{h}, the player doesn't know where the exit is so he has to guess his way out! Amr follows simple algorithm to choose the path. Let's consider infinite command string "LRLRLRLRL..." (consisting of alternating characters 'L' and 'R'). Amr sequentially executes the characters of the string using following rules: Character 'L' means "go to the left child of the current node"; Character 'R' means "go to the right child of the current node"; If the destination node is already visited, Amr skips current command, otherwise he moves to the destination node; If Amr skipped two consecutive commands, he goes back to the parent of the current node before executing next command; If he reached a leaf node that is not the exit, he returns to the parent of the current node; If he reaches an exit, the game is finished. Now Amr wonders, if he follows this algorithm, how many nodes he is going to visit before reaching the exit? -----Input----- Input consists of two integers h, n (1 ≤ h ≤ 50, 1 ≤ n ≤ 2^{h}). -----Output----- Output a single integer representing the number of nodes (excluding the exit node) Amr is going to visit before reaching the exit by following this algorithm. -----Examples----- Input 1 2 Output 2 Input 2 3 Output 5 Input 3 6 Output 10 Input 10 1024 Output 2046 -----Note----- A perfect binary tree of height h is a binary tree consisting of h + 1 levels. Level 0 consists of a single node called root, level h consists of 2^{h} nodes called leaves. Each node that is not a leaf has exactly two children, left and right one. Following picture illustrates the sample test number 3. Nodes are labeled according to the order of visit. [Image] The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys import math MAXNUM = math.inf MINNUM = -1 * math.inf ASCIILOWER = 97 ASCIIUPPER = 65 def getInt(): return int(sys.stdin.readline().rstrip()) def getInts(): return map(int, sys.stdin.readline().rstrip().split(' ')) def getString(): return sys.stdin.readline().rstrip() def printOutput(ans): sys.stdout.write(str(ans) + '\n') def solve(h, n): heightNodes = [1 for _ in range(51)] for height in range(1, 51): heightNodes[height] = 2 ** height + heightNodes[height - 1] curPos = 2 ** (h - 1) depth = h add = 2 ** (h - 1) direction = -1 total = 0 while True: if depth == 0: break if curPos >= n and direction == -1 or (curPos < n and direction == 1): total += 1 depth -= 1 add //= 2 curPos += direction * add else: total += heightNodes[depth - 1] direction *= -1 return total def readinput(): (h, n) = getInts() printOutput(solve(h, n)) readinput() ```
vfc_145178
{ "difficulty": "medium_hard", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/507/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1 2\n", "output": "2", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 3\n", "output": "5", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 6\n", "output": "10", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 1024\n", "output": "2046", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 577\n", "output": "1345", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://codeforces.com/problemset/problem/1176/F
Solve the following coding problem using the programming language python: You are playing a computer card game called Splay the Sire. Currently you are struggling to defeat the final boss of the game. The boss battle consists of $n$ turns. During each turn, you will get several cards. Each card has two parameters: its cost $c_i$ and damage $d_i$. You may play some of your cards during each turn in some sequence (you choose the cards and the exact order they are played), as long as the total cost of the cards you play during the turn does not exceed $3$. After playing some (possibly zero) cards, you end your turn, and all cards you didn't play are discarded. Note that you can use each card at most once. Your character has also found an artifact that boosts the damage of some of your actions: every $10$-th card you play deals double damage. What is the maximum possible damage you can deal during $n$ turns? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of turns. Then $n$ blocks of input follow, the $i$-th block representing the cards you get during the $i$-th turn. Each block begins with a line containing one integer $k_i$ ($1 \le k_i \le 2 \cdot 10^5$) — the number of cards you get during $i$-th turn. Then $k_i$ lines follow, each containing two integers $c_j$ and $d_j$ ($1 \le c_j \le 3$, $1 \le d_j \le 10^9$) — the parameters of the corresponding card. It is guaranteed that $\sum \limits_{i = 1}^{n} k_i \le 2 \cdot 10^5$. -----Output----- Print one integer — the maximum damage you may deal. -----Example----- Input 5 3 1 6 1 7 1 5 2 1 4 1 3 3 1 10 3 5 2 3 3 1 15 2 4 1 10 1 1 100 Output 263 -----Note----- In the example test the best course of action is as follows: During the first turn, play all three cards in any order and deal $18$ damage. During the second turn, play both cards and deal $7$ damage. During the third turn, play the first and the third card and deal $13$ damage. During the fourth turn, play the first and the third card and deal $25$ damage. During the fifth turn, play the only card, which will deal double damage ($200$). The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from sys import stdin, stdout, exit n = int(input()) inf = 10 ** 18 dp = [[-inf] * 10 for i in range(n + 1)] dp[0][0] = 0 for i in range(n): k = int(stdin.readline()) cards = [] for j in range(k): (c, d) = map(int, stdin.readline().split()) cards.append((c, d)) cards.sort(reverse=True) cards_by_cost = [[] for i in range(3)] for (c, d) in cards: cards_by_cost[c - 1].append(d) for j in range(3): cards_by_cost[j] = cards_by_cost[j][:3] for prev_played in range(10): val = dp[i][prev_played] dp[i + 1][prev_played] = max(dp[i + 1][prev_played], val) for num_played in range(len(cards_by_cost[0])): pld = num_played + prev_played + 1 if pld >= 10: dp[i + 1][pld % 10] = max(dp[i + 1][pld % 10], val + sum(cards_by_cost[0][:num_played + 1]) + cards_by_cost[0][0]) else: dp[i + 1][pld] = max(dp[i + 1][pld], val + sum(cards_by_cost[0][:num_played + 1])) if len(cards_by_cost[1]) > 0 and len(cards_by_cost[0]) > 0: pld = 2 + prev_played c0 = cards_by_cost[0][0] c1 = cards_by_cost[1][0] if pld >= 10: dp[i + 1][pld % 10] = max(dp[i + 1][pld % 10], val + c0 + c1 + max(c0, c1)) else: dp[i + 1][pld] = max(dp[i + 1][pld], val + c0 + c1) if len(cards_by_cost[1]) > 0: pld = 1 + prev_played if pld >= 10: dp[i + 1][pld % 10] = max(dp[i + 1][pld % 10], val + 2 * cards_by_cost[1][0]) else: dp[i + 1][pld] = max(dp[i + 1][pld], val + cards_by_cost[1][0]) if len(cards_by_cost[2]) > 0: pld = 1 + prev_played if pld >= 10: dp[i + 1][pld % 10] = max(dp[i + 1][pld % 10], val + 2 * cards_by_cost[2][0]) else: dp[i + 1][pld] = max(dp[i + 1][pld], val + cards_by_cost[2][0]) ans = max((dp[n][i] for i in range(10))) stdout.write(str(ans) + '\n') ```
vfc_145182
{ "difficulty": "hard", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1176/F", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n3\n1 6\n1 7\n1 5\n2\n1 4\n1 3\n3\n1 10\n3 5\n2 3\n3\n1 15\n2 4\n1 10\n1\n1 100\n", "output": "263\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n3\n1 1\n1 1\n1 1\n3\n1 1\n1 1\n1 1\n3\n1 1\n1 1\n1 1\n3\n1 1\n1 1\n1 1\n3\n1 100\n1 1\n1 1\n", "output": "211\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n4\n1 1\n1 1\n2 2\n3 4\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n3\n1 1\n1 1\n1 1\n3\n1 1\n1 1\n1 1\n3\n1 1\n1 1\n1 1\n3\n1 1\n1 1\n1 1\n3\n1 100\n1 1\n1 1\n", "output": "211\n", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://www.codechef.com/PTRN2021/problems/ITGUY52
Solve the following coding problem using the programming language python: The chef is trying to solve some pattern problems, Chef wants your help to code it. Chef has one number K to form a new pattern. Help the chef to code this pattern problem. -----Input:----- - First-line will contain $T$, the number of test cases. Then the test cases follow. - Each test case contains a single line of input, one integer $K$. -----Output:----- For each test case, output as the pattern. -----Constraints----- - $1 \leq T \leq 100$ - $1 \leq K \leq 100$ -----Sample Input:----- 4 1 2 3 4 -----Sample Output:----- 1 1 10 11 100 1 10 11 100 101 110 111 1000 1001 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 10000 -----EXPLANATION:----- No need, else pattern can be decode easily. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python t = int(input()) for _ in range(t): k = int(input()) count = 1 for _ in range(k): output = [] for index in range(1, k + 1): output.append(bin(count).replace('0b', '')) count += 1 print(*output) ```
vfc_145186
{ "difficulty": "unknown_difficulty", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://www.codechef.com/PTRN2021/problems/ITGUY52", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n1\n2\n3\n4\n", "output": "1\n1 10\n11 100\n1 10 11\n100 101 110\n111 1000 1001\n1 10 11 100\n101 110 111 1000\n1001 1010 1011 1100\n1101 1110 1111 10000\n", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://codeforces.com/problemset/problem/401/B
Solve the following coding problem using the programming language python: Sereja is a coder and he likes to take part in Codesorfes rounds. However, Uzhland doesn't have good internet connection, so Sereja sometimes skips rounds. Codesorfes has rounds of two types: Div1 (for advanced coders) and Div2 (for beginner coders). Two rounds, Div1 and Div2, can go simultaneously, (Div1 round cannot be held without Div2) in all other cases the rounds don't overlap in time. Each round has a unique identifier — a positive integer. The rounds are sequentially (without gaps) numbered with identifiers by the starting time of the round. The identifiers of rounds that are run simultaneously are different by one, also the identifier of the Div1 round is always greater. Sereja is a beginner coder, so he can take part only in rounds of Div2 type. At the moment he is taking part in a Div2 round, its identifier equals to x. Sereja remembers very well that he has taken part in exactly k rounds before this round. Also, he remembers all identifiers of the rounds he has taken part in and all identifiers of the rounds that went simultaneously with them. Sereja doesn't remember anything about the rounds he missed. Sereja is wondering: what minimum and what maximum number of Div2 rounds could he have missed? Help him find these two numbers. -----Input----- The first line contains two integers: x (1 ≤ x ≤ 4000) — the round Sereja is taking part in today, and k (0 ≤ k < 4000) — the number of rounds he took part in. Next k lines contain the descriptions of the rounds that Sereja took part in before. If Sereja took part in one of two simultaneous rounds, the corresponding line looks like: "1 num_2 num_1" (where num_2 is the identifier of this Div2 round, num_1 is the identifier of the Div1 round). It is guaranteed that num_1 - num_2 = 1. If Sereja took part in a usual Div2 round, then the corresponding line looks like: "2 num" (where num is the identifier of this Div2 round). It is guaranteed that the identifiers of all given rounds are less than x. -----Output----- Print in a single line two integers — the minimum and the maximum number of rounds that Sereja could have missed. -----Examples----- Input 3 2 2 1 2 2 Output 0 0 Input 9 3 1 2 3 2 8 1 4 5 Output 2 3 Input 10 0 Output 5 9 -----Note----- In the second sample we have unused identifiers of rounds 1, 6, 7. The minimum number of rounds Sereja could have missed equals to 2. In this case, the round with the identifier 1 will be a usual Div2 round and the round with identifier 6 will be synchronous with the Div1 round. The maximum number of rounds equals 3. In this case all unused identifiers belong to usual Div2 rounds. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python (X, n) = list(map(int, input().split())) Taken = [True] * (X + 1) for i in range(n): x = list(map(int, input().split())) if x[0] == 1: Taken[x[1]] = False Taken[x[2]] = False else: Taken[x[1]] = False cnt = 0 minn = 0 maxx = 0 ans = 0 for i in range(1, X): if Taken[i]: cnt += 1 maxx += 1 else: ans += cnt // 2 if cnt % 2 != 0: ans += 1 cnt = 0 ans += cnt // 2 if cnt % 2 != 0: ans += 1 print(ans, maxx) ```
vfc_145196
{ "difficulty": "easy", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/401/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 2\n2 1\n2 2\n", "output": "0 0", "type": "stdin_stdout" }, { "fn_name": null, "input": "9 3\n1 2 3\n2 8\n1 4 5\n", "output": "2 3", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 0\n", "output": "5 9", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 2\n1 1 2\n1 8 9\n", "output": "3 5", "type": "stdin_stdout" }, { "fn_name": null, "input": "9 3\n1 4 5\n1 1 2\n1 6 7\n", "output": "2 2", "type": "stdin_stdout" }, { "fn_name": null, "input": "7 2\n2 3\n1 5 6\n", "output": "2 3", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://www.codechef.com/problems/CHEFAPAR
Solve the following coding problem using the programming language python: Chef lives in a big apartment in Chefland. The apartment charges maintenance fees that he is supposed to pay monthly on time. But Chef is a lazy person and sometimes misses the deadlines. The apartment charges 1000 Rs per month as maintenance fees. Also, they also charge a one-time fine of 100 Rs for each of the late payments. It does not matter how late the payment is done, the fine is fixed to be Rs.100. Chef has been living in the apartment for N months. Now, he wants to switch the apartment, so he has to pay the entire dues to the apartment. The deadline for the N-th month is also over. From his bank statement, he finds the information whether he paid apartment rent for a particular month for not. You are given this information by an array A of size N, where Ai (can be either 0 or 1) specifies whether he has paid the 1000Rs in the i-th month or not. Assume that Chef paid the fees in the i-th month, then this fees will be considered for the earliest month for which Chef has not yet paid the fees. For example, assume Chef did not pay any money in first month and 1000Rs in the second month. Then this rent of 1000Rs will be considered for 1st month. But this counts as late payment for the first month's fees, and hence he will have to pay Rs. 100 for that. And since the payment he made in the second month is not accounted for the second month, but rather for the first month, he will incur a fine of Rs.100 even for the second month. He has not paid any of the fines so far. Can you please help in finding Chef total due (all the fines, plus all the unpaid maintenance fees) that he has to pay to apartment? -----Input----- First line of the input contains an integer T denoting number of test cases. The description of T test cases follows. The first line of each test cases contains an integer N denoting the number of months for which you have to calculate the total amount of money that Chef has to pay at the end of the n month to clear all of his dues. Next line of each test case contains N space separated integers (each of them can be either 0 or 1) denoting whether Chef paid the 1000Rs fees in the corresponding month or not. If the corresponding integer is 1, it means that Chef paid the maintenance fees for that month, whereas 0 will mean that Chef did not pay the maintenance fees that month. -----Output----- For each test case, output a single integer denoting the total amount including fine that Chef has to pay. -----Constraints-----Constraints - 1 ≤ T ≤ 25 - 0 ≤ Ai ≤ 1 -----Subtasks----- Subtask #1 (30 points) - 1 ≤ N ≤ 103 Subtask #2 (70 points) - 1 ≤ N ≤ 105 -----Example----- Input 4 2 1 1 2 0 0 3 0 1 0 2 0 1 Output 0 2200 2300 1200 -----Explanation----- Example case 1. Chef paid maintenance fees for both the months. So, he does not have any dues left. Example case 2. Chef did not pay the maintenance fees for any of the months. For the first month, he has to pay 1000Rs, and 100Rs fine as a late payment. For second month payments, he just have to pay 1100Rs. So, in total he has a due of 1100 + 1100 = 2200 Rs. Example case 3. In the first month, Chef did not pay his maintenance fees. He paid the maintenance of first in the second month. So, he paid a fine of 100Rs for first month. For second and third month, Chef did not pay the maintenance fees. So, he has to pay 1000Rs + 100Rs (of fine) for second month and only 1000Rs + 100Rs (of fine) for third month. In total, Chef has got to pay 100 + 1100 + 1100 = 2300 Rs. Example case 4. In the first month, Chef did not pay his maintenance fees. He paid the maintenance of first in the second month. So, he paid a fine of 100Rs for first month. For second month, Chef did not pay the maintenance fees. So, he has to pay 1000Rs + 100Rs (of fine) for second month. In total, Chef has got to pay 100 + 1100 = 1200 Rs. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python for i in range(int(input())): n = int(input()) p = list(map(int, input().split())) amount = p.count(0) * 1000 if p.count(0) != 0: q = p.index(0) print(100 * (n - q) + amount) else: print(amount) ```
vfc_145200
{ "difficulty": "easy", "memory_limit": "50000 bytes", "memory_limit_bytes": null, "problem_url": "https://www.codechef.com/problems/CHEFAPAR", "time_limit": "2 seconds" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n2\n1 1\n2\n0 0\n3\n0 1 0\n2\n0 1\n\n\n", "output": "0\n2200\n2300\n1200\n", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://codeforces.com/problemset/problem/1243/B1
Solve the following coding problem using the programming language python: This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems. After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first. Ujan has two distinct strings $s$ and $t$ of length $n$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $i$ and $j$ ($1 \le i,j \le n$, the values $i$ and $j$ can be equal or different), and swaps the characters $s_i$ and $t_j$. Can he succeed? Note that he has to perform this operation exactly once. He has to perform this operation. -----Input----- The first line contains a single integer $k$ ($1 \leq k \leq 10$), the number of test cases. For each of the test cases, the first line contains a single integer $n$ ($2 \leq n \leq 10^4$), the length of the strings $s$ and $t$. Each of the next two lines contains the strings $s$ and $t$, each having length exactly $n$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. -----Output----- For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). -----Example----- Input 4 5 souse houhe 3 cat dog 2 aa az 3 abc bca Output Yes No No No -----Note----- In the first test case, Ujan can swap characters $s_1$ and $t_4$, obtaining the word "house". In the second test case, it is not possible to make the strings equal using exactly one swap of $s_i$ and $t_j$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python t = int(input()) for x in range(t): n = int(input()) s = input() t = input() i = c = 0 a = b = '' while c < 4 and i < n: if s[i] != t[i]: c += 1 a += s[i] b += t[i] i += 1 if c == 2 and len(set(a)) == len(set(b)) == 1: print('Yes') else: print('No') ```
vfc_145204
{ "difficulty": "easy", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1243/B1", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca\n", "output": "Yes\nNo\nNo\nNo\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n11\nartiovnldnp\nartiovsldsp\n2\naa\nzz\n2\naa\nxy\n2\nab\nba\n2\nza\nzz\n3\nabc\nbca\n16\naajjhdsjfdsfkadf\naajjhjsjfdsfkajf\n2\nix\nii\n2\noo\nqo\n2\npp\npa\n", "output": "Yes\nYes\nNo\nNo\nNo\nNo\nYes\nNo\nNo\nNo\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n2\nab\ncd\n", "output": "No\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n4\naacd\nbbdc\n", "output": "No\n", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://codeforces.com/problemset/problem/1326/C
Solve the following coding problem using the programming language python: You are given a permutation $p_1, p_2, \ldots, p_n$ of integers from $1$ to $n$ and an integer $k$, such that $1 \leq k \leq n$. A permutation means that every number from $1$ to $n$ is contained in $p$ exactly once. Let's consider all partitions of this permutation into $k$ disjoint segments. Formally, a partition is a set of segments $\{[l_1, r_1], [l_2, r_2], \ldots, [l_k, r_k]\}$, such that: $1 \leq l_i \leq r_i \leq n$ for all $1 \leq i \leq k$; For all $1 \leq j \leq n$ there exists exactly one segment $[l_i, r_i]$, such that $l_i \leq j \leq r_i$. Two partitions are different if there exists a segment that lies in one partition but not the other. Let's calculate the partition value, defined as $\sum\limits_{i=1}^{k} {\max\limits_{l_i \leq j \leq r_i} {p_j}}$, for all possible partitions of the permutation into $k$ disjoint segments. Find the maximum possible partition value over all such partitions, and the number of partitions with this value. As the second value can be very large, you should find its remainder when divided by $998\,244\,353$. -----Input----- The first line contains two integers, $n$ and $k$ ($1 \leq k \leq n \leq 200\,000$) — the size of the given permutation and the number of segments in a partition. The second line contains $n$ different integers $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq n$) — the given permutation. -----Output----- Print two integers — the maximum possible partition value over all partitions of the permutation into $k$ disjoint segments and the number of such partitions for which the partition value is equal to the maximum possible value, modulo $998\,244\,353$. Please note that you should only find the second value modulo $998\,244\,353$. -----Examples----- Input 3 2 2 1 3 Output 5 2 Input 5 5 2 1 5 3 4 Output 15 1 Input 7 3 2 7 3 1 5 4 6 Output 18 6 -----Note----- In the first test, for $k = 2$, there exists only two valid partitions: $\{[1, 1], [2, 3]\}$ and $\{[1, 2], [3, 3]\}$. For each partition, the partition value is equal to $2 + 3 = 5$. So, the maximum possible value is $5$ and the number of partitions is $2$. In the third test, for $k = 3$, the partitions with the maximum possible partition value are $\{[1, 2], [3, 5], [6, 7]\}$, $\{[1, 3], [4, 5], [6, 7]\}$, $\{[1, 4], [5, 5], [6, 7]\}$, $\{[1, 2], [3, 6], [7, 7]\}$, $\{[1, 3], [4, 6], [7, 7]\}$, $\{[1, 4], [5, 6], [7, 7]\}$. For all of them, the partition value is equal to $7 + 5 + 6 = 18$. The partition $\{[1, 2], [3, 4], [5, 7]\}$, however, has the partition value $7 + 3 + 6 = 16$. This is not the maximum possible value, so we don't count it. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python (n, k) = [int(el) for el in input().split()] per = [int(el) for el in input().split()] sp = sorted(per, reverse=True) sp.append(0) mod = 998244353 result = 1 prev = 0 for (i, p) in enumerate(per, 1): if p > sp[k]: if not prev: prev = i else: result *= i - prev result %= mod prev = i print(sum(sp[:k]), result) ```
vfc_145208
{ "difficulty": "easy", "memory_limit": "256 megabytes", "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1326/C", "time_limit": "1 second" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 2\n2 1 3\n", "output": "5 2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 5\n2 1 5 3 4\n", "output": "15 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7 3\n2 7 3 1 5 4 6\n", "output": "18 6\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1\n1\n", "output": "1 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 1\n1 2\n", "output": "2 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 2\n2 1\n", "output": "3 1\n", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://www.hackerrank.com/challenges/points-on-rectangle/problem
Solve the following coding problem using the programming language python: You are given $\textit{q}$ queries where each query consists of a set of $n$ points on a two-dimensional plane (i.e., $(x,y)$). For each set of points, print YES on a new line if all the points fall on the edges (i.e., sides and/or corners) of a non-degenerate rectangle which is axis parallel; otherwise, print NO instead. Input Format The first line contains a single positive integer, $\textit{q}$, denoting the number of queries. The subsequent lines describe each query in the following format: The first line contains a single positive integer, $n$, denoting the number of points in the query. Each line $\boldsymbol{i}$ of the $n$ subsequent lines contains two space-separated integers describing the respective values of $x_i$ and $y_i$ for the point at coordinate $(x_i,y_i)$. Constraints $1\leq q\leq10$ $1\leq n\leq10$ $-10^4\leq x,y\leq10^4$ Output Format For each query, print YES on a new line if all $n$ points lie on the edges of some non-degenerate rectangle which is axis parallel; otherwise, print NO instead. Sample Input 2 3 0 0 0 1 1 0 4 0 0 0 2 2 0 1 1 Sample Output YES NO Explanation We perform the following $q=2$ queries: In the first query, all points lie on the edges of a non-degenerate rectangle with corners at $(0,0)$, $(0,1)$, $(1,0)$, and $(1,1)$. Thus, we print YES on a new line. In the second query, points $(0,0)$, $(0,2)$, and $(2,0)$ could all lie along the edge of some non-degenerate rectangle, but point $(1,1)$ would have to fall inside that rectangle. Thus, we print NO on a new line. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def main(): for i in range(int(input())): p = [list(map(int, input().split())) for j in range(int(input()))] (px, py) = zip(*p) x0 = min(px) x1 = max(px) y0 = min(py) y1 = max(py) result = all((x in (x0, x1) or y in (y0, y1) for (x, y) in p)) print('YES' if result else 'NO') main() ```
vfc_145212
{ "difficulty": "easy", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://www.hackerrank.com/challenges/points-on-rectangle/problem", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\n3\n0 0\n0 1\n1 0\n4\n0 0\n0 2\n2 0\n1 1\n", "output": "YES\nNO\n", "type": "stdin_stdout" } ] }
taco
verifiable_code
Solve the following coding problem using the programming language python: Singh is getting bored at work. He has a lot of ideas (N) to work on (as side projects) but is unable to choose one. He chooses a random number (R) and lines up his ideas in a circle. He starts counting from first and removes the R^th idea in the circle until only one remains. Help him choose an idea to work on. Input Format: no_of_ideas random_number Output Format: idea_number Constraints: 1 ≤ N ≤ 20 1 ≤ R ≤ 10000000 SAMPLE INPUT 5 3 SAMPLE OUTPUT 4 Explanation List: {1,2,3,4,5}. Start counting from 1, 1 2 3 -> 3 removed 4 5 1 -> 1 removed 2 4 5 -> 5 removed 2 4 2 -> 2 removed Remaining number: 4 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python N, R = [int(i) for i in input().split()] r = 0 for i in range(1, N+1): r = (r+R)%i print(r+1) ```
vfc_145220
{ "difficulty": "unknown_difficulty", "memory_limit": null, "memory_limit_bytes": null, "problem_url": null, "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "23 9000001", "output": "6", "type": "stdin_stdout" }, { "fn_name": null, "input": "23 9000000", "output": "4", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://codeforces.com/problemset/problem/358/A
Solve the following coding problem using the programming language python: Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x_1, 0) and (x_2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). [Image] Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^3). The second line contains n distinct integers x_1, x_2, ..., x_{n} ( - 10^6 ≤ x_{i} ≤ 10^6) — the i-th point has coordinates (x_{i}, 0). The points are not necessarily sorted by their x coordinate. -----Output----- In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). -----Examples----- Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no -----Note----- The first test from the statement is on the picture to the left, the second test is on the picture to the right. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python length = int(input()) sequence = list(map(int, input().split())) if length <= 3: print('no') else: for i in range(length - 1): (w, x) = sorted([sequence[i], sequence[i + 1]]) for j in range(length - 1): (y, z) = sorted([sequence[j], sequence[j + 1]]) if w < y < x < z or y < w < z < x: print('yes') exit() print('no') ```
vfc_145224
{ "difficulty": "medium", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/358/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n0 10 5 15\n", "output": "yes\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n0 15 5 10\n", "output": "no\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n0 1000 2000 3000 1500\n", "output": "yes\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n-724093 710736 -383722 -359011 439613\n", "output": "no\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "50\n384672 661179 -775591 -989608 611120 442691 601796 502406 384323 -315945 -934146 873993 -156910 -94123 -930137 208544 816236 466922 473696 463604 794454 -872433 -149791 -858684 -467655 -555239 623978 -217138 -408658 493342 -733576 -350871 711210 884148 -426172 519986 -356885 527171 661680 977247 141654 906254 -961045 -759474 -48634 891473 -606365 -513781 -966166 27696\n", "output": "yes\n", "type": "stdin_stdout" } ] }
taco
verifiable_code
Solve the following coding problem using the programming language python: Rachel, being an awesome android programmer, just finished an App that will let us draw a triangle by selecting three points on the touch-plane.Now She asks her friend Bruce to draw a Right-Angled Triangle (we'll call it RAT) by selecting 3 integral points on the plane. A RAT is a triangle with Non-Zero area and a right-angle. But Bruce, being in a hurry as always, makes a mistake on drawing the points. Now Rachel's app can fix Bruce's mistake if only one coordinate of any one point can be adjusted within a distance of 1 units. i.e., if the drawn triangle can be made a RAT by adjusting any one coordinate within 1 unit of distance. Your task is simple, given 6 coordinates (each Integer pair representing a point), you have to tell can Rachel's app correct Bruce's mistake automatically? Input: First line containing T, the number of test cases. Then T lines follow with 6 space separated coordinates. Output: print "YES" in a newline if it's possible. otherwise print "NO" in a newline. Constraints: 1 ≤ T ≤ 100 -1000 ≤ xi,yi ≤ 1000 SAMPLE INPUT 3 -1 0 2 0 0 1 2 3 4 5 6 6 0 0 2 0 10 10 SAMPLE OUTPUT YES NO NO Explanation Case 1: For (-1, 0), (2, 0) and (0, 1), shifting (-1, 0) to (0, 0) makes it a RAT. Case 2 and Case 3: No any possible shift results into a RAT. Note that any point can be moved either in x- or in y- direction at a time by 1 unit. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def dist(x1,y1,x2,y2): return (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2) def is_RAT(a): ab = dist(a[0],a[1],a[2],a[3]) bc = dist(a[4],a[5],a[2],a[3]) ca = dist(a[0],a[1],a[4],a[5]) if ab == bc+ca or ca == bc+ab or bc == ab+ca: return True return False def samepoint(a): if a[0]==a[2] and a[1] == a[3]: return True if a[0]==a[4] and a[1] == a[5]: return True if a[4]==a[2] and a[5] == a[3]: return True return False t=eval(input()) while t: t-=1 a=[int(x) for x in input().split()] d = [0,-1,+1] found = False for diff in d: for i in range(0,6): a[i]=a[i]+diff if is_RAT(a) and not samepoint(a): found = True break a[i]=a[i]-diff if found: break if found: print("YES") else: print("NO") ```
vfc_145232
{ "difficulty": "unknown_difficulty", "memory_limit": null, "memory_limit_bytes": null, "problem_url": null, "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "20\n0 0 1 0 4 1\n0 0 1 0 100 1\n60 4 90 -53 32 -12\n52 -34 -37 -63 23 54\n39 22 95 25 42 -33\n-10 -11 62 6 -12 -3\n22 -15 -24 77 -69 -60\n99 85 90 87 64 -20\n-50 -37 -93 -6 -80 -80\n4 -13 4 -49 -24 -13\n0 -3 -3 -10 4 -7\n-45 -87 -34 -79 -60 -62\n-67 49 89 -76 -37 87\n22 32 -33 -30 -18 68\n36 1 -17 -54 -19 55\n55 44 15 14 23 83\n-19 0 -89 -54 25 -57\n69 -45 1 11 56 -63\n72 68 56 72 33 -88\n59 86 74 -49 77 88", "output": "YES\nNO\nYES\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nYES\nYES\nNO\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nNO\nNO\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nNO\nNO\nNO\nNO\nYES\nNO\nNO\nNO\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://practice.geeksforgeeks.org/problems/implementation-of-priority-queue-using-binary-heap/1
Solve the following coding problem using the programming language python: Given a binary heap implementation of Priority Queue. Extract the maximum element from the queue i.e. remove it from the Queue and return it's value. Example 1: Input: 4 2 8 16 24 2 6 5 Output: 24 Priority Queue after extracting maximum: 16 8 6 5 2 2 4 Example 2: Input: 64 12 8 48 5 Output: 64 Priority Queue after extracting maximum: 48 12 8 5 Your Task: Complete the function extractMax() which extracts the maximum element, i.e. remove it from the Queue and return it's value. Expected Time Complexity: O(logN) Expected Space Complexity: O(N) Constraints: 1<=N<=500 Write your solution by modifying this code: ```python #User function Template for python3 # 1. parent(i): Function to return the parent node of node i # 2. leftChild(i): Function to return index of the left child of node i # 3. rightChild(i): Function to return index of the right child of node i # 4. shiftUp(int i): Function to shift up the node in order to maintain the # heap property # 5. shiftDown(int i): Function to shift down the node in order to maintain the # heap property. # int s=-1, current index value of the array H[]. class Solution: def extractMax(self): # Code here ``` The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python class Solution: def extractMax(self): global s ans = H[0] H[0] = H[s] s -= 1 shiftDown(0) return ans ```
vfc_145236
{ "difficulty": "medium", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://practice.geeksforgeeks.org/problems/implementation-of-priority-queue-using-binary-heap/1", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4 2 8 16 24 2 6 5", "output": "24", "type": "stdin_stdout" }, { "fn_name": null, "input": "64 12 8 48 5", "output": "64", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://codeforces.com/problemset/problem/1204/E
Solve the following coding problem using the programming language python: Natasha's favourite numbers are $n$ and $1$, and Sasha's favourite numbers are $m$ and $-1$. One day Natasha and Sasha met and wrote down every possible array of length $n+m$ such that some $n$ of its elements are equal to $1$ and another $m$ elements are equal to $-1$. For each such array they counted its maximal prefix sum, probably an empty one which is equal to $0$ (in another words, if every nonempty prefix sum is less to zero, then it is considered equal to zero). Formally, denote as $f(a)$ the maximal prefix sum of an array $a_{1, \ldots ,l}$ of length $l \geq 0$. Then: $$f(a) = \max (0, \smash{\displaystyle\max_{1 \leq i \leq l}} \sum_{j=1}^{i} a_j )$$ Now they want to count the sum of maximal prefix sums for each such an array and they are asking you to help. As this sum can be very large, output it modulo $998\: 244\: 853$. -----Input----- The only line contains two integers $n$ and $m$ ($0 \le n,m \le 2\,000$). -----Output----- Output the answer to the problem modulo $998\: 244\: 853$. -----Examples----- Input 0 2 Output 0 Input 2 0 Output 2 Input 2 2 Output 5 Input 2000 2000 Output 674532367 -----Note----- In the first example the only possible array is [-1,-1], its maximal prefix sum is equal to $0$. In the second example the only possible array is [1,1], its maximal prefix sum is equal to $2$. There are $6$ possible arrays in the third example: [1,1,-1,-1], f([1,1,-1,-1]) = 2 [1,-1,1,-1], f([1,-1,1,-1]) = 1 [1,-1,-1,1], f([1,-1,-1,1]) = 1 [-1,1,1,-1], f([-1,1,1,-1]) = 1 [-1,1,-1,1], f([-1,1,-1,1]) = 0 [-1,-1,1,1], f([-1,-1,1,1]) = 0 So the answer for the third example is $2+1+1+1+0+0 = 5$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python P = 998244853 N = 4000 (f, fi) = ([0] * (N + 1), [0] * (N + 1)) f[0] = 1 for i in range(N): f[i + 1] = f[i] * (i + 1) % P fi[-1] = pow(f[-1], P - 2, P) for i in reversed(range(N)): fi[i] = fi[i + 1] * (i + 1) % P def C(n, r): c = 1 while n or r: (a, b) = (n % P, r % P) if a < b: return 0 c = c * f[a] % P * fi[b] % P * fi[a - b] % P n //= P r //= P return c (n, m) = map(int, input().split()) k = max(n - m - 1, 0) print((k * C(n + m, m) + sum((C(n + m, m + i) for i in range(k + 1, n))) + 1) % P if n else 0) ```
vfc_145237
{ "difficulty": "hard", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1204/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "0 2\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 0\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 2\n", "output": "5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2000 2000\n", "output": "674532367\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "0 0\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "11 2\n", "output": "716\n", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://practice.geeksforgeeks.org/problems/find-difference-between-sum-of-diagonals1554/1
Solve the following coding problem using the programming language python: Given a matrix Grid[][] of size NxN. Calculate the absolute difference between the sums of its diagonals. Example 1: Input: N=3 Grid=[[1,2,3],[4,5,6],[7,8,9]] Output: 0 Explanation: Sum of primary diagonal = 1+5+9 = 15. Sum of secondary diagonal = 3+5+7 = 15. Difference = |15 - 15| = 0. Example 2: Input: N=3 Grid=[[1,1,1],[1,1,1],[1,1,1]] Output: 0 Explanation: Sum of primary diagonal = 1+1+1=3. Sum of secondary diagonal = 1+1+1=3. Difference = |3-3| = 0. Your Task: You don't need to read input or print anything.Your task is to complete the function diagonalSumDifference() which takes an integer N and a 2D array Grid as input parameters and returns the absolutes difference between the sums of its diagonals. Expected Time Complexity:O(N) Expected Auxillary Space:O(1) Constraints: 1<=N<=1000 -1000<=Grid[i][j]<=1000, for 0<=i,j Write your solution by modifying this code: ```python #User function Template for python3 class Solution: def diagonalSumDifference(self,N,Grid): #code here ``` The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python class Solution: def diagonalSumDifference(self, N, Grid): (Grid1, Grid2) = (0, 0) for i in range(N): for j in range(N): if i == j: Grid1 += Grid[i][j] if i + j == N - 1: Grid2 += Grid[i][j] return abs(Grid1 - Grid2) ```
vfc_145241
{ "difficulty": "easy", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://practice.geeksforgeeks.org/problems/find-difference-between-sum-of-diagonals1554/1", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "N=3\nGrid=[[1,2,3],[4,5,6],[7,8,9]]", "output": "0", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://practice.geeksforgeeks.org/problems/leaders-in-an-array-1587115620/1
Solve the following coding problem using the programming language python: Given an array A of positive integers. Your task is to find the leaders in the array. An element of array is leader if it is greater than or equal to all the elements to its right side. The rightmost element is always a leader. Example 1: Input: n = 6 A[] = {16,17,4,3,5,2} Output: 17 5 2 Explanation: The first leader is 17 as it is greater than all the elements to its right. Similarly, the next leader is 5. The right most element is always a leader so it is also included. Example 2: Input: n = 5 A[] = {1,2,3,4,0} Output: 4 0 Your Task: You don't need to read input or print anything. The task is to complete the function leader() which takes array A and n as input parameters and returns an array of leaders in order of their appearance. Expected Time Complexity: O(n) Expected Auxiliary Space: O(n) Constraints: 1 <= n <= 10^{7} 0 <= A_{i} <= 10^{7} Write your solution by modifying this code: ```python class Solution: #Back-end complete function Template for Python 3 #Function to find the leaders in the array. def leaders(self, A, N): #Code here ``` The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python class Solution: def leaders(self, A, N): leaders = [] max_right = A[N - 1] leaders.append(max_right) for i in range(N - 2, -1, -1): if A[i] >= max_right: leaders.append(A[i]) max_right = A[i] leaders.reverse() return leaders ```
vfc_145242
{ "difficulty": "easy", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://practice.geeksforgeeks.org/problems/leaders-in-an-array-1587115620/1", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "n = 6\r\nA[] = {16,17,4,3,5,2}", "output": "17 5 2", "type": "stdin_stdout" }, { "fn_name": null, "input": "n = 5\r\nA[] = {1,2,3,4,0}", "output": "4 0", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://codeforces.com/problemset/problem/1012/C
Solve the following coding problem using the programming language python: Welcome to Innopolis city. Throughout the whole year, Innopolis citizens suffer from everlasting city construction. From the window in your room, you see the sequence of n hills, where i-th of them has height a_{i}. The Innopolis administration wants to build some houses on the hills. However, for the sake of city appearance, a house can be only built on the hill, which is strictly higher than neighbouring hills (if they are present). For example, if the sequence of heights is 5, 4, 6, 2, then houses could be built on hills with heights 5 and 6 only. The Innopolis administration has an excavator, that can decrease the height of an arbitrary hill by one in one hour. The excavator can only work on one hill at a time. It is allowed to decrease hills up to zero height, or even to negative values. Increasing height of any hill is impossible. The city administration wants to build k houses, so there must be at least k hills that satisfy the condition above. What is the minimum time required to adjust the hills to achieve the administration's plan? However, the exact value of k is not yet determined, so could you please calculate answers for all k in range $1 \leq k \leq \lceil \frac{n}{2} \rceil$? Here $\lceil \frac{n}{2} \rceil$ denotes n divided by two, rounded up. -----Input----- The first line of input contains the only integer n (1 ≤ n ≤ 5000)—the number of the hills in the sequence. Second line contains n integers a_{i} (1 ≤ a_{i} ≤ 100 000)—the heights of the hills in the sequence. -----Output----- Print exactly $\lceil \frac{n}{2} \rceil$ numbers separated by spaces. The i-th printed number should be equal to the minimum number of hours required to level hills so it becomes possible to build i houses. -----Examples----- Input 5 1 1 1 1 1 Output 1 2 2 Input 3 1 2 3 Output 0 2 Input 5 1 2 3 2 2 Output 0 1 3 -----Note----- In the first example, to get at least one hill suitable for construction, one can decrease the second hill by one in one hour, then the sequence of heights becomes 1, 0, 1, 1, 1 and the first hill becomes suitable for construction. In the first example, to get at least two or at least three suitable hills, one can decrease the second and the fourth hills, then the sequence of heights becomes 1, 0, 1, 0, 1, and hills 1, 3, 5 become suitable for construction. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from sys import stdin from math import ceil n = int(stdin.readline().strip()) s = tuple([0] + list(map(int, stdin.readline().strip().split())) + [0]) lim = ceil(n / 2) + 1 dp = [[2000000002 for i in range(n + 1)] for j in range(lim)] vis = [[False for i in range(n + 1)] for j in range(lim)] for i in range(n + 1): dp[0][i] = 0 ans = [0 for i in range(lim - 1)] for i in range(1, lim): for j in range(1, n + 1): x = 0 y = s[j - 1] if vis[i - 1][j - 2]: y = min(y, s[j - 2] - 1) if y >= s[j]: x = y - s[j] + 1 if s[j + 1] >= s[j]: x += s[j + 1] - s[j] + 1 if j == 1: if dp[i - 1][0] + x <= dp[i][j - 1]: vis[i][j] = True dp[i][j] = dp[i - 1][0] + x else: dp[i][j] = dp[i][j - 1] elif dp[i - 1][j - 2] + x <= dp[i][j - 1]: vis[i][j] = True dp[i][j] = dp[i - 1][j - 2] + x else: dp[i][j] = dp[i][j - 1] ans[i - 1] = dp[i][-1] print(*ans) ```
vfc_145244
{ "difficulty": "hard", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1012/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n1 1 1 1 1\n", "output": "1 2 2 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n1 2 3\n", "output": "0 2 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n1 2 3 2 2\n", "output": "0 1 3 \n", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://practice.geeksforgeeks.org/problems/addition-of-two-numbers0812/1
Solve the following coding problem using the programming language python: Given two numbers A and B. Your task is to return the sum of A and B. Example 1: Input: A = 1, B = 2 Output: 3 Explanation: Addition of 1 and 2 is 3. Example 2: Input: A = 10, B = 20 Output: 30 Explanation: Addition os 10 and 20 is 30. Your Task: You don't need to read input or print anything. Your task is to complete the function addition() which takes two numbers A and B and returns the sum of A and B. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints: 1 <= A, B <= 10^{18} Write your solution by modifying this code: ```python #User function Template for python3 class Solution: def addition (ob,A,B): # code here ``` The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python class Solution: def addition(ob, A, B): return A + B ```
vfc_145248
{ "difficulty": "easy", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://practice.geeksforgeeks.org/problems/addition-of-two-numbers0812/1", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "A = 1, B = 2", "output": "3", "type": "stdin_stdout" }, { "fn_name": null, "input": "A = 10, B = 20", "output": "30", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://www.hackerrank.com/challenges/twins/problem
Solve the following coding problem using the programming language python: Lia is fascinated by anything she considers to be a twin. She calls a pairs of positive integers, $\boldsymbol{i}$ and $j$, twins if: They are both prime. A prime number is an integer greater than $\mbox{1}$ that has no positive divisors other than $\mbox{1}$ and itself. Their absolute difference is exactly equal to $2$ (i.e., $|j-i|=2$). Given an inclusive interval of integers from $n$ to $m$, can you help Lia find the number of pairs of twins there are in the interval (i.e., $[n,m]$)? Note that pairs $(i,j)$ and $(j,i)$ are considered to be the same pair. Input Format Two space-separated integers describing the respective values of $n$ and $m$. Constraints $1\leq n\leq m\leq10^9$ $m-n\leq10^6$ Output Format Print a single integer denoting the number of pairs of twins. Sample Input 0 3 13 Sample Output 0 3 Explanation 0 There are three pairs of twins: $(3,5),(5,7)$, and $(11,13)$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python limit = 1000000 (beg, end) = map(int, input().strip().split(' ')) is_prime = [False] * (limit + 1) prime = [] for x in range(1, int(limit ** 0.5) + 1): for y in range(1, int(limit ** 0.5) + 1): n = 4 * x ** 2 + y ** 2 if n <= limit and (n % 12 == 1 or n % 12 == 5): is_prime[n] = not is_prime[n] n = 3 * x ** 2 + y ** 2 if n <= limit and n % 12 == 7: is_prime[n] = not is_prime[n] n = 3 * x ** 2 - y ** 2 if x > y and n <= limit and (n % 12 == 11): is_prime[n] = not is_prime[n] for n in range(5, int(limit ** 0.5)): if is_prime[n]: for k in range(n ** 2, limit + 1, n ** 2): is_prime[k] = False is_prime[2] = is_prime[3] = True if end <= limit: for i in range(beg, end + 1): if is_prime[i]: prime.append(i) else: d = end - beg ans = [True] * (d + 2) i = 2 while i * i <= end: if is_prime[i]: k = beg // i k = k * i if k < beg: k += i if k == i: k += i while k <= end: ans[k - beg] = False k += i i += 1 for i in range(d + 1): if ans[i]: prime.append(beg + i) count = 0 l = len(prime) for i in range(l - 1): if abs(prime[i] - prime[i + 1]) == 2: count += 1 print(count) ```
vfc_145249
{ "difficulty": "medium", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://www.hackerrank.com/challenges/twins/problem", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 13\n", "output": "3\n", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://practice.geeksforgeeks.org/problems/remove-bst-keys-outside-given-range/1
Solve the following coding problem using the programming language python: Given a Binary Search Tree (BST) and a range [min, max], remove all keys which are outside the given range. The modified tree should also be BST. Example 1: Input: Range = [-10, 13] Output: -8 6 7 13 Explanation: Nodes with values -13, 14 and 15 are outside the given range and hence are removed from the BST. This is the resultant BST and it's inorder traversal is -8 6 7 13. Example 2: Input: Range = [2, 6] 14 / \ 4 16 / \ / 2 8 15 / \ / \ -8 3 7 10 Output: 2 3 4 Explanation: After removing nodes outside the range, the resultant BST looks like: 4 / 2 \ 3 The inorder traversal of this tree is: 2 3 4 Your task: You don't have to read input or print anything. Your task is to complete the function removekeys() which takes the root node of the BST and the range as input and returns the root of the modified BST after removing the nodes outside the given range. Expected Time Complexity: O(number of nodes) Expected Auxiliary Space: O(h) Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ K ≤ 10^{5} Write your solution by modifying this code: ```python #User function Template for python3 ''' class Node: def __init__(self, val): self.right = None self.data = val self.left = None ''' class Solution: def removekeys(self, root, l, r): #code here ``` The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python class Solution: def removekeys(self, root, l, r): if root is None: return root root.left = self.removekeys(root.left, l, r) root.right = self.removekeys(root.right, l, r) if root.data > r: return root.left if root.data < l: return root.right return root ```
vfc_145253
{ "difficulty": "medium", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://practice.geeksforgeeks.org/problems/remove-bst-keys-outside-given-range/1", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "Range = [-10, 13]", "output": "-8 6 7 13", "type": "stdin_stdout" }, { "fn_name": null, "input": "Range = [2, 6]\r\n 14\r\n / \\\r\n 4 16\r\n / \\ /\r\n 2 8 15\r\n / \\ / \\\r\n -8 3 7 10", "output": "2 3 4", "type": "stdin_stdout" } ] }
taco
verifiable_code
Solve the following coding problem using the programming language python: You are given two string S and T. Find the maximal length of some prefix of the string S which occurs in strings T as subsequence. Input The first line contains string S. The second line contains string T. Both strings consist of lowecase Latin letters. Output Output one integer - answer to the question. Constraints 1 ≤ length of S, T ≤ 10^6 SAMPLE INPUT digger biggerdiagram SAMPLE OUTPUT 3 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from collections import deque, defaultdict, Counter from math import factorial from fractions import gcd from sys import stdin, exit from itertools import * import heapq import string import re def prase(T): if T == 1: return list(map(int, testcase.next().strip().split())) return testcase.next().strip().split() def get(): return testcase.next().strip() testcase = stdin # code starts here s = get() t = get() count = 0 n = 0 for i in s: for j in range(n, len(t)): if t[j] == i: count += 1 n = j+1 break else: break print(count) ```
vfc_145259
{ "difficulty": "easy", "memory_limit": null, "memory_limit_bytes": null, "problem_url": null, "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "informatika\ninformatikainformatikainformatika\n", "output": "3", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://codeforces.com/problemset/problem/451/D
Solve the following coding problem using the programming language python: We call a string good, if after merging all the consecutive equal characters, the resulting string is palindrome. For example, "aabba" is good, because after the merging step it will become "aba". Given a string, you have to find two values: the number of good substrings of even length; the number of good substrings of odd length. -----Input----- The first line of the input contains a single string of length n (1 ≤ n ≤ 10^5). Each character of the string will be either 'a' or 'b'. -----Output----- Print two space-separated integers: the number of good substrings of even length and the number of good substrings of odd length. -----Examples----- Input bb Output 1 2 Input baab Output 2 4 Input babb Output 2 5 Input babaa Output 2 7 -----Note----- In example 1, there are three good substrings ("b", "b", and "bb"). One of them has even length and two of them have odd length. In example 2, there are six good substrings (i.e. "b", "a", "a", "b", "aa", "baab"). Two of them have even length and four of them have odd length. In example 3, there are seven good substrings (i.e. "b", "a", "b", "b", "bb", "bab", "babb"). Two of them have even length and five of them have odd length. Definitions A substring s[l, r] (1 ≤ l ≤ r ≤ n) of string s = s_1s_2... s_{n} is string s_{l}s_{l} + 1... s_{r}. A string s = s_1s_2... s_{n} is a palindrome if it is equal to string s_{n}s_{n} - 1... s_1. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def R(): return map(int, input().split()) def I(): return int(input()) def S(): return str(input()) def L(): return list(R()) from collections import Counter import math import sys from itertools import permutations import bisect mod = 10 ** 9 + 7 s = S() l = len(s) A = [0] * 2 B = [0] * 2 for i in range(2): A[i] = [0] * (l + 3) B[i] = [0] * (l + 3) for i in range(l): for j in range(2): if i % 2 != j: A[j][i] = A[j][i - 1] B[j][i] = B[j][i - 1] else: A[j][i] = A[j][i - 2] + (s[i] == 'a') B[j][i] = B[j][i - 2] + (s[i] == 'b') ans = [0] * 2 for i in range(l): if s[i] == 'a': ans[0] += A[1 - i % 2][i] ans[1] += A[i % 2][i] else: ans[0] += B[1 - i % 2][i] ans[1] += B[i % 2][i] print(ans[0], ans[1]) ```
vfc_145263
{ "difficulty": "hard", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/451/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "bb\n", "output": "1 2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "baab\n", "output": "2 4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "babb\n", "output": "2 5\n", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://codeforces.com/problemset/problem/707/C
Solve the following coding problem using the programming language python: Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding to triple. Such triples are called Pythagorean triples. For example, triples (3, 4, 5), (5, 12, 13) and (6, 8, 10) are Pythagorean triples. Here Katya wondered if she can specify the length of some side of right triangle and find any Pythagorean triple corresponding to such length? Note that the side which length is specified can be a cathetus as well as hypotenuse. Katya had no problems with completing this task. Will you do the same? -----Input----- The only line of the input contains single integer n (1 ≤ n ≤ 10^9) — the length of some side of a right triangle. -----Output----- Print two integers m and k (1 ≤ m, k ≤ 10^18), such that n, m and k form a Pythagorean triple, in the only line. In case if there is no any Pythagorean triple containing integer n, print - 1 in the only line. If there are many answers, print any of them. -----Examples----- Input 3 Output 4 5 Input 6 Output 8 10 Input 1 Output -1 Input 17 Output 144 145 Input 67 Output 2244 2245 -----Note-----[Image] Illustration for the first sample. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys, os, io def rs(): return sys.stdin.readline().rstrip() def ri(): return int(sys.stdin.readline()) def ria(): return list(map(int, sys.stdin.readline().split())) def ws(s): sys.stdout.write(s + '\n') def wi(n): sys.stdout.write(str(n) + '\n') def wia(a): sys.stdout.write(' '.join([str(x) for x in a]) + '\n') import math, datetime, functools, itertools, operator, bisect, fractions, statistics from collections import deque, defaultdict, OrderedDict, Counter from fractions import Fraction from decimal import Decimal from sys import stdout from heapq import heappush, heappop, heapify, _heapify_max, _heappop_max, nsmallest, nlargest sys.setrecursionlimit(111111) INF = 99999999999999999999999999999999 def main(): mod = 1000000007 starttime = datetime.datetime.now() if os.path.exists('input.txt'): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') tc = 1 for _ in range(tc): x = ri() if x < 3: print(-1) elif x % 2: print((x * x - 1) // 2, (x * x + 1) // 2) else: print((x // 2) ** 2 - 1, (x // 2) ** 2 + 1) endtime = datetime.datetime.now() time = (endtime - starttime).total_seconds() * 1000 if os.path.exists('input.txt'): print('Time:', time, 'ms') class FastReader(io.IOBase): newlines = 0 def __init__(self, fd, chunk_size=1024 * 8): self._fd = fd self._chunk_size = chunk_size self.buffer = io.BytesIO() def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size)) if not b: break ptr = self.buffer.tell() (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)) self.newlines = 0 return self.buffer.read() def readline(self, size=-1): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size)) self.newlines = b.count(b'\n') + (not b) ptr = self.buffer.tell() (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)) self.newlines -= 1 return self.buffer.readline() class FastWriter(io.IOBase): def __init__(self, fd): self._fd = fd self.buffer = io.BytesIO() self.write = self.buffer.write def flush(self): os.write(self._fd, self.buffer.getvalue()) (self.buffer.truncate(0), self.buffer.seek(0)) class FastStdin(io.IOBase): def __init__(self, fd=0): self.buffer = FastReader(fd) self.read = lambda : self.buffer.read().decode('ascii') self.readline = lambda : self.buffer.readline().decode('ascii') class FastStdout(io.IOBase): def __init__(self, fd=1): self.buffer = FastWriter(fd) self.write = lambda s: self.buffer.write(s.encode('ascii')) self.flush = self.buffer.flush sys.stdin = FastStdin() sys.stdout = FastStdout() main() ```
vfc_145267
{ "difficulty": "medium", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/707/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n", "output": "4 5", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n", "output": "8 10", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n", "output": "-1", "type": "stdin_stdout" }, { "fn_name": null, "input": "17\n", "output": "144 145", "type": "stdin_stdout" }, { "fn_name": null, "input": "67\n", "output": "2244 2245", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n", "output": "24 26", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://codeforces.com/problemset/problem/344/B
Solve the following coding problem using the programming language python: Mad scientist Mike is busy carrying out experiments in chemistry. Today he will attempt to join three atoms into one molecule. A molecule consists of atoms, with some pairs of atoms connected by atomic bonds. Each atom has a valence number — the number of bonds the atom must form with other atoms. An atom can form one or multiple bonds with any other atom, but it cannot form a bond with itself. The number of bonds of an atom in the molecule must be equal to its valence number. [Image] Mike knows valence numbers of the three atoms. Find a molecule that can be built from these atoms according to the stated rules, or determine that it is impossible. -----Input----- The single line of the input contains three space-separated integers a, b and c (1 ≤ a, b, c ≤ 10^6) — the valence numbers of the given atoms. -----Output----- If such a molecule can be built, print three space-separated integers — the number of bonds between the 1-st and the 2-nd, the 2-nd and the 3-rd, the 3-rd and the 1-st atoms, correspondingly. If there are multiple solutions, output any of them. If there is no solution, print "Impossible" (without the quotes). -----Examples----- Input 1 1 2 Output 0 1 1 Input 3 4 5 Output 1 3 2 Input 4 1 1 Output Impossible -----Note----- The first sample corresponds to the first figure. There are no bonds between atoms 1 and 2 in this case. The second sample corresponds to the second figure. There is one or more bonds between each pair of atoms. The third sample corresponds to the third figure. There is no solution, because an atom cannot form bonds with itself. The configuration in the fourth figure is impossible as each atom must have at least one atomic bond. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python a = list(map(int, input().split())) if sum(a) % 2 == 1 or max(a) > sum(a) - max(a): print('Impossible') elif len(set(a)) == 1: print(a[0] // 2, a[0] // 2, a[0] // 2) elif a.count(min(a)) == 2: pos = a.index(max(a)) ans = [0] * 3 ans[pos] = ans[pos - 1] = max(a) // 2 ans[pos - 2] = (min(a) * 2 - max(a)) // 2 print(' '.join((str(ans[i]) for i in range(3)))) elif min(a) == a[0]: print(a[1] - sum(a) // 2 + a[0], sum(a) // 2 - a[0], a[2] - sum(a) // 2 + a[0]) elif min(a) == a[1]: print(a[0] - sum(a) // 2 + a[1], a[2] - sum(a) // 2 + a[1], sum(a) // 2 - a[1]) elif min(a) == a[2]: print(sum(a) // 2 - a[2], a[1] - sum(a) // 2 + a[2], a[0] - sum(a) // 2 + a[2]) ```
vfc_145271
{ "difficulty": "easy", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/344/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1 1 2\n", "output": "0 1 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 4 5\n", "output": "1 3 2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 1 1\n", "output": "Impossible\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1 1\n", "output": "Impossible\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1000000 1000000 1000000\n", "output": "500000 500000 500000\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 11 8\n", "output": "3 8 0\n", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://www.codechef.com/ENAU2020/problems/ECAUG202
Solve the following coding problem using the programming language python: One day, Delta, the dog, got very angry. He has $N$ items with different values, and he decided to destroy a few of them. However, Delta loves his hooman as well. So he only destroyed those items whose Least Significant Bit in binary representation is 0. Can you help Delta to find the total damage he did so he could make an equally sorry face? -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - First line of Each test case a single integer $N$. - Next line contains $N$ integers denoting values of items. -----Output:----- For each testcase, output in a single line the total damage caused by Delta. -----Constraints----- - $1 \leq T \leq 10^3$ - $1 \leq N \leq 10^3$ - $1 \leq value \leq 10^3$ -----Sample Input:----- 1 5 1 2 3 4 5 -----Sample Output:----- 6 -----EXPLANATION:----- Total Damage: $2 + 4 = 6$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python for test in range(int(input())): n = int(input()) ar = list(map(int, input().split())) count = 0 for item in ar: if bin(item)[-1] == '0': count += item print(count) ```
vfc_145275
{ "difficulty": "unknown_difficulty", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://www.codechef.com/ENAU2020/problems/ECAUG202", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1\n5\n1 2 3 4 5\n", "output": "6\n", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://www.codechef.com/problems/CLASSES
Solve the following coding problem using the programming language python: Chef's college is starting next week. There are $S$ subjects in total, and he needs to choose $K$ of them to attend each day, to fulfill the required number of credits to pass the semester. There are $N + 1$ buildings. His hostel is in building number $0$. Subject $i$ is taught in building $A_{i}$. After each subject, there is a break, during which he goes back to his hostel. There are $M$ bidirectional paths of length $1$ which connects building $u$ to building $v$. Find the minimum possible total distance Chef needs to travel each day if he chooses his subjects wisely. ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contain $M + 2$ lines of input. First line will contain $4$ space separated integers $N$, $M$, $S$, $K$, number of buildings other than hostel building, number of edges, total number of subjects taught, number of subjects required to pass the semester. Next $M$ lines have $2$ space separated integers $u$, $v$ representing the path connecting buildings $u$ and $v$. Next line has $S$ space separated integers $A_{1}, A_{2}, \ldots A_{S}$ representing the building in which $i^{th}$ subject is taught. ------ Output: ------ For each testcase, output in a single line answer to the problem. ------ Constraints ------ $1 ≤ T ≤ 3$ $1 ≤ N, S ≤ 10^{5}$ $1 ≤ M ≤ 2*10^{5}$ $1 ≤ K ≤ S$ $1 ≤ A_{i} ≤ N$ $0 ≤ u, v ≤ N$ Its guaranteed that the graph is connected and has no self loops. ----- Sample Input 1 ------ 3 2 3 2 2 0 1 1 2 2 0 1 2 2 2 2 2 0 1 1 2 1 2 6 7 5 3 0 1 0 2 0 4 1 3 1 4 2 5 2 6 1 2 3 5 6 ----- Sample Output 1 ------ 4 6 8 ----- explanation 1 ------ TestCase 1: First Chef attends the subject in the building $2$ and he travels $1$ units to go there and $1$ units to come back during the break to the hostel. Second subject he attends in the building $1$ and he travels $1$ unit to go there and $1$ unit to come back during the break to the hostel. In total the total minimum distance to be travelled is $2 + 2 = 4$ units. TestCase 2: First Chef attends the subject in the building $2$ and he travels $2$ units to go there and $2$ units to come back during the break to the hostel. Second subject he attends in the building $1$ and he travels $1$ unit to go there and $1$ unit to come back during the break to the hostel. In total the total minimum distance to be travelled is $4 + 2 = 6$ units. TestCase 3: First Chef attends the subject in the building $3$ and he travels $2$ units to go there and $2$ units to come back during the break to the hostel. Second subject he attends in the building $1$ and he travels $1$ unit to go there and $1$ unit to come back during the break to the hostel. Final subject he attends in the building $2$ and he travels $1$ unit to go there and $1$ unit to come back during the break to the hostel. In total the total minimum distance to be travelled is $4 + 2 + 2 = 8$ units. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from collections import deque def BFS(bldg, s): queue = deque() queue.append(s) cost = [-1 for i in range(n + 1)] cost[s] = 0 while queue: s = queue.popleft() for i in bldg[s]: if cost[i] == -1: queue.append(i) cost[i] = cost[s] + 1 return cost for _ in range(int(input())): (n, m, s, k) = map(int, input().split()) buildings = [[] for i in range(n + 1)] for i in range(m): (u, v) = map(int, input().split()) buildings[u] += [v] buildings[v] += [u] sub = list(map(int, input().split())) costa = BFS(buildings, 0) for i in range(s): sub[i] = costa[sub[i]] sub.sort() print(2 * sum(sub[:k])) ```
vfc_145279
{ "difficulty": "very_hard", "memory_limit": "50000 bytes", "memory_limit_bytes": null, "problem_url": "https://www.codechef.com/problems/CLASSES", "time_limit": "1 seconds" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n2 3 2 2\n0 1\n1 2\n2 0\n1 2\n2 2 2 2\n0 1\n1 2\n1 2\n6 7 5 3\n0 1\n0 2\n0 4\n1 3\n1 4\n2 5\n2 6\n1 2 3 5 6", "output": "4\n6\n8", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://practice.geeksforgeeks.org/problems/maximum-number-of-partitions-that-can-be-sorted-individually-to-make-sorted2926/1
Solve the following coding problem using the programming language python: Given an array arr[ ] of size n such that elements of arr[ ] in range [0, 1, ..n-1]. Our task is to divide the array into the maximum number of partitions that can be sorted individually, then concatenated to make the whole array sorted. Example 1: ​Input : arr[ ] = {2, 1, 0, 3} Output : 2 Explanation: If divide arr[] into two partitions {2, 1, 0} and {3}, sort then and concatenate then, we get the whole array sorted. ​Example 2: Input : arr[ ] = {2, 1, 0, 3, 4, 5} Output : 4 Your Task: This is a function problem. The input is already taken care of by the driver code. You only need to complete the function maxPartitions() that takes an array (arr), sizeOfArray (n), and return the maximum number of partitions. The driver code takes care of the printing. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ T ≤ 100 1 ≤ N ≤ 10^{5} Write your solution by modifying this code: ```python #User function Template for python3 def maxPartitions (arr, n) : #Complete the function ``` Your solution should implemented in the function "maxPartitions". The inputs will be passed to it and it should return the correct solution. Now solve the problem and return the code.
```python def maxPartitions(arr, n): a = arr[0] c = 0 for i in range(n): if a < arr[i]: a = arr[i] if a == i: c += 1 return c ```
vfc_145283
{ "difficulty": "easy", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://practice.geeksforgeeks.org/problems/maximum-number-of-partitions-that-can-be-sorted-individually-to-make-sorted2926/1", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": "maxPartitions", "input": "arr[ ] = {2, 1, 0, 3}", "output": "2", "type": "function_call" }, { "fn_name": "maxPartitions", "input": "arr[ ] = {2, 1, 0, 3, 4, 5}", "output": "4", "type": "function_call" } ] }
taco
verifiable_code
https://codeforces.com/problemset/problem/634/A
Solve the following coding problem using the programming language python: A remote island chain contains n islands, labeled 1 through n. Bidirectional bridges connect the islands to form a simple cycle — a bridge connects islands 1 and 2, islands 2 and 3, and so on, and additionally a bridge connects islands n and 1. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal. The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: First, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal. Determine if it is possible for the islanders to arrange the statues in the desired order. -----Input----- The first line contains a single integer n (2 ≤ n ≤ 200 000) — the total number of islands. The second line contains n space-separated integers a_{i} (0 ≤ a_{i} ≤ n - 1) — the statue currently placed on the i-th island. If a_{i} = 0, then the island has no statue. It is guaranteed that the a_{i} are distinct. The third line contains n space-separated integers b_{i} (0 ≤ b_{i} ≤ n - 1) — the desired statues of the ith island. Once again, b_{i} = 0 indicates the island desires no statue. It is guaranteed that the b_{i} are distinct. -----Output----- Print "YES" (without quotes) if the rearrangement can be done in the existing network, and "NO" otherwise. -----Examples----- Input 3 1 0 2 2 0 1 Output YES Input 2 1 0 0 1 Output YES Input 4 1 2 3 0 0 3 2 1 Output NO -----Note----- In the first sample, the islanders can first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3. In the second sample, the islanders can simply move statue 1 from island 1 to island 2. In the third sample, no sequence of movements results in the desired position. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python input() l1 = [int(x) for x in input().split() if x != '0'] l2 = [int(x) for x in input().split() if x != '0'] m = len(l1) x = l1.index(l2[0]) b = True for i in range(len(l1)): b &= l2[i] == l1[(x + i) % m] print('YES' if b else 'NO') ```
vfc_145284
{ "difficulty": "easy", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/634/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n1 0 2\n2 0 1\n", "output": "YES\n", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://www.hackerrank.com/challenges/python-string-formatting/problem
Solve the following coding problem using the programming language python: Given an integer, $n$, print the following values for each integer $\boldsymbol{i}$ from $\mbox{1}$ to $n$: Decimal Octal Hexadecimal (capitalized) Binary Function Description Complete the print_formatted function in the editor below. print_formatted has the following parameters: int number: the maximum value to print Prints The four values must be printed on a single line in the order specified above for each $\boldsymbol{i}$ from $\mbox{1}$ to $number$. Each value should be space-padded to match the width of the binary value of $number$ and the values should be separated by a single space. Input Format A single integer denoting $n$. Constraints $1\leq n\leq99$ Sample Input 17 Sample Output 1 1 1 1 2 2 2 10 3 3 3 11 4 4 4 100 5 5 5 101 6 6 6 110 7 7 7 111 8 10 8 1000 9 11 9 1001 10 12 A 1010 11 13 B 1011 12 14 C 1100 13 15 D 1101 14 16 E 1110 15 17 F 1111 16 20 10 10000 17 21 11 10001 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python N = int(input()) width = len(str(bin(N))) - 2 for n in range(1, N + 1): for base in 'doXb': print('{0:{width}{base}}'.format(n, base=base, width=width), end=' ') print() ```
vfc_145288
{ "difficulty": "easy", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://www.hackerrank.com/challenges/python-string-formatting/problem", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "17\n", "output": " 1 1 1 1\n 2 2 2 10\n 3 3 3 11\n 4 4 4 100\n 5 5 5 101\n 6 6 6 110\n 7 7 7 111\n 8 10 8 1000\n 9 11 9 1001\n 10 12 A 1010\n 11 13 B 1011\n 12 14 C 1100\n 13 15 D 1101\n 14 16 E 1110\n 15 17 F 1111\n 16 20 10 10000\n 17 21 11 10001\n", "type": "stdin_stdout" } ] }
taco
verifiable_code
https://codeforces.com/problemset/problem/163/A
Solve the following coding problem using the programming language python: One day Polycarpus got hold of two non-empty strings s and t, consisting of lowercase Latin letters. Polycarpus is quite good with strings, so he immediately wondered, how many different pairs of "x y" are there, such that x is a substring of string s, y is a subsequence of string t, and the content of x and y is the same. Two pairs are considered different, if they contain different substrings of string s or different subsequences of string t. Read the whole statement to understand the definition of different substrings and subsequences. The length of string s is the number of characters in it. If we denote the length of the string s as |s|, we can write the string as s = s1s2... s|s|. A substring of s is a non-empty string x = s[a... b] = sasa + 1... sb (1 ≤ a ≤ b ≤ |s|). For example, "code" and "force" are substrings or "codeforces", while "coders" is not. Two substrings s[a... b] and s[c... d] are considered to be different if a ≠ c or b ≠ d. For example, if s="codeforces", s[2...2] and s[6...6] are different, though their content is the same. A subsequence of s is a non-empty string y = s[p1p2... p|y|] = sp1sp2... sp|y| (1 ≤ p1 < p2 < ... < p|y| ≤ |s|). For example, "coders" is a subsequence of "codeforces". Two subsequences u = s[p1p2... p|u|] and v = s[q1q2... q|v|] are considered different if the sequences p and q are different. Input The input consists of two lines. The first of them contains s (1 ≤ |s| ≤ 5000), and the second one contains t (1 ≤ |t| ≤ 5000). Both strings consist of lowercase Latin letters. Output Print a single number — the number of different pairs "x y" such that x is a substring of string s, y is a subsequence of string t, and the content of x and y is the same. As the answer can be rather large, print it modulo 1000000007 (109 + 7). Examples Input aa aa Output 5 Input codeforces forceofcode Output 60 Note Let's write down all pairs "x y" that form the answer in the first sample: "s[1...1] t[1]", "s[2...2] t[1]", "s[1...1] t[2]","s[2...2] t[2]", "s[1...2] t[1 2]". The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from sys import stdin s = [ord(i) - 97 for i in stdin.readline().strip()] s1 = [ord(i) - 97 for i in stdin.readline().strip()] n = len(s) m = len(s1) mod = 1000000007 dp = [[0 for i in range(n)] for j in range(26)] for i in range(m): arr = [0 for j in range(n)] for j in range(n): if s1[i] == s[j]: arr[j] = 1 if j > 0: arr[j] = (arr[j] + dp[s[j - 1]][j - 1]) % mod for j in range(n): dp[s1[i]][j] = (arr[j] + dp[s1[i]][j]) % mod x = 0 for i in dp: for j in i: x = (x + j) % mod print(x) ```
vfc_145293
{ "difficulty": "medium_hard", "memory_limit": "256.0 megabytes", "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/163/A", "time_limit": "2.0 seconds" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "bbabb\nbababbbbab\n", "output": "222\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "ab\nbbbba\n", "output": "5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "xzzxxxzxzzzxzzzxxzzxzzxzxzxxzxxzxxzxzzxxzxxzxxxzxzxzxxzzxxxxzxzzzxxxzxzxxxzzxxzxxzxxzzxxzxxzxzxzzzxzzzzxzxxzzxzxxzxxzzxzxzx\nzzx\n", "output": "291\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "a\nb\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "zxzxzxzxzxzxzx\nd\n", "output": "0\n", "type": "stdin_stdout" } ] }