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
apps
verifiable_code
3691
Solve the following coding problem using the programming language python: THE SxPLAY & KIVΛ - 漂流 KIVΛ & Nikki Simmons - Perspectives With a new body, our idol Aroma White (or should we call her Kaori Minamiya?) begins to uncover her lost past through the OS space. The space can be considered a 2D plane, with an infinite number of data nodes, indexed from $0$, with their coordinates defined as follows: The coordinates of the $0$-th node is $(x_0, y_0)$ For $i > 0$, the coordinates of $i$-th node is $(a_x \cdot x_{i-1} + b_x, a_y \cdot y_{i-1} + b_y)$ Initially Aroma stands at the point $(x_s, y_s)$. She can stay in OS space for at most $t$ seconds, because after this time she has to warp back to the real world. She doesn't need to return to the entry point $(x_s, y_s)$ to warp home. While within the OS space, Aroma can do the following actions: From the point $(x, y)$, Aroma can move to one of the following points: $(x-1, y)$, $(x+1, y)$, $(x, y-1)$ or $(x, y+1)$. This action requires $1$ second. If there is a data node at where Aroma is staying, she can collect it. We can assume this action costs $0$ seconds. Of course, each data node can be collected at most once. Aroma wants to collect as many data as possible before warping back. Can you help her in calculating the maximum number of data nodes she could collect within $t$ seconds? -----Input----- The first line contains integers $x_0$, $y_0$, $a_x$, $a_y$, $b_x$, $b_y$ ($1 \leq x_0, y_0 \leq 10^{16}$, $2 \leq a_x, a_y \leq 100$, $0 \leq b_x, b_y \leq 10^{16}$), which define the coordinates of the data nodes. The second line contains integers $x_s$, $y_s$, $t$ ($1 \leq x_s, y_s, t \leq 10^{16}$) – the initial Aroma's coordinates and the amount of time available. -----Output----- Print a single integer — the maximum number of data nodes Aroma can collect within $t$ seconds. -----Examples----- Input 1 1 2 3 1 0 2 4 20 Output 3 Input 1 1 2 3 1 0 15 27 26 Output 2 Input 1 1 2 3 1 0 2 2 1 Output 0 -----Note----- In all three examples, the coordinates of the first $5$ data nodes are $(1, 1)$, $(3, 3)$, $(7, 9)$, $(15, 27)$ and $(31, 81)$ (remember that nodes are numbered from $0$). In the first example, the optimal route to collect $3$ nodes is as follows: Go to the coordinates $(3, 3)$ and collect the $1$-st node. This takes $|3 - 2| + |3 - 4| = 2$ seconds. Go to the coordinates $(1, 1)$ and collect the $0$-th node. This takes $|1 - 3| + |1 - 3| = 4$ seconds. Go to the coordinates $(7, 9)$ and collect the $2$-nd node. This takes $|7 - 1| + |9 - 1| = 14$ seconds. In the second example, the optimal route to collect $2$ nodes is as follows: Collect the $3$-rd node. This requires no seconds. Go to the coordinates $(7, 9)$ and collect the $2$-th node. This takes $|15 - 7| + |27 - 9| = 26$ seconds. In the third example, Aroma can't collect any nodes. She should have taken proper rest instead of rushing into the OS space like that. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python x0, y0, A, C, B, D = list(map(int, input().split())) pts = [[x0, y0]] for i in range(100): nx, ny = [pts[-1][0] * A + B, pts[-1][1] * C + D] pts.append([nx, ny]) if max(nx, ny) > 10000000000000000 * 10000000000000000: break x, y, t = list(map(int, input().split())) # print (pts[0]) # print (pts[1]) # print (pts[2]) # print (pts[3]) _max = 0 for i in range(len(pts)): for j in range(len(pts)): if abs(pts[i][0] - pts[j][0]) + abs(pts[i][1] - pts[j][1]) + abs(x - pts[i][0]) + abs(y - pts[i][1]) <= t: _max = max(_max, abs(i - j) + 1) print(_max) ```
vfc_23472
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1292/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1 1 2 3 1 0\n2 4 20\n", "output": "3", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1 2 3 1 0\n15 27 26\n", "output": "2", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1 2 3 1 0\n2 2 1\n", "output": "0", "type": "stdin_stdout" }, { "fn_name": null, "input": "9999999999999999 1 2 2 0 0\n10000000000000000 1 10000000000000000\n", "output": "1", "type": "stdin_stdout" }, { "fn_name": null, "input": "9999999999999999 1 2 2 0 0\n9999999999999999 1 10000000000000000\n", "output": "2", "type": "stdin_stdout" }, { "fn_name": null, "input": "9999999999999998 1 2 2 0 0\n9999999999999999 1 10000000000000000\n", "output": "2", "type": "stdin_stdout" } ] }
apps
verifiable_code
3692
Solve the following coding problem using the programming language python: Firecrackers scare Nian the monster, but they're wayyyyy too noisy! Maybe fireworks make a nice complement. Little Tommy is watching a firework show. As circular shapes spread across the sky, a splendid view unfolds on the night of Lunar New Year's eve. A wonder strikes Tommy. How many regions are formed by the circles on the sky? We consider the sky as a flat plane. A region is a connected part of the plane with positive area, whose bound consists of parts of bounds of the circles and is a curve or several curves without self-intersections, and that does not contain any curve other than its boundaries. Note that exactly one of the regions extends infinitely. -----Input----- The first line of input contains one integer n (1 ≤ n ≤ 3), denoting the number of circles. The following n lines each contains three space-separated integers x, y and r ( - 10 ≤ x, y ≤ 10, 1 ≤ r ≤ 10), describing a circle whose center is (x, y) and the radius is r. No two circles have the same x, y and r at the same time. -----Output----- Print a single integer — the number of regions on the plane. -----Examples----- Input 3 0 0 1 2 0 1 4 0 1 Output 4 Input 3 0 0 2 3 0 2 6 0 2 Output 6 Input 3 0 0 2 2 0 2 1 1 2 Output 8 -----Note----- For the first example, $000$ For the second example, [Image] For the third example, $\text{Q)}$ The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from math import sqrt def pt(x): print(x) rd = lambda: map(int, input().split()) n = int(input()) def f(x1, y1, r1, x2, y2, r2): a = (r1 + r2) ** 2 b = (r1 - r2) ** 2 d = (x1 - x2) ** 2 + (y1 - y2) ** 2 if d > a: return 1 elif d == a: return 4 elif d < b: return 3 elif d == b: return 5 else: return 2 def g(x1, y1, r1, x2, y2, r2): ds = (x1 - x2) ** 2 + (y1 - y2) ** 2 d = sqrt(ds) A = (r1 ** 2 - r2 ** 2 + ds) / (2 * d) h = sqrt(r1 ** 2 - A ** 2) x = x1 + A * (x2 - x1) / d y = y1 + A * (y2 - y1) / d x3 = x - h * (y2 - y1) / d y3 = y + h * (x2 - x1) / d x4 = x + h * (y2 - y1) / d y4 = y - h * (x2 - x1) / d return x3, y3, x4, y4 if n is 1: pt(2) if n is 2: x1, y1, r1 = rd() x2, y2, r2 = rd() a = f(x1, y1, r1, x2, y2, r2) pt(4 if a is 2 else 3) if n is 3: x1, y1, r1 = rd() x2, y2, r2 = rd() x3, y3, r3 = rd() a = f(x1, y1, r1, x2, y2, r2) b = f(x1, y1, r1, x3, y3, r3) c = f(x3, y3, r3, x2, y2, r2) t = [a, b, c] t.sort() a, b, c = t if a is 1 and b is 1 and c in [1, 3, 4, 5]: pt(4) if a is 1 and b is 1 and c is 2: pt(5) if a is 1 and b is 2 and c is 2: pt(6) if a is 1 and b is 2 and c in [3, 4, 5]: pt(5) if a is 1 and b in [3, 4, 5]: pt(4) if a is 2 and b is 2 and c is 2: x4, y4, x5, y5 = g(x1, y1, r1, x2, y2, r2) r = 8 if abs((x4 - x3) ** 2 + (y4 - y3) ** 2 - r3 ** 2) < 1e-6: r -= 1 if abs((x5 - x3) ** 2 + (y5 - y3) ** 2 - r3 ** 2) < 1e-6: r -= 1 pt(r) if a is 2 and b is 2 and c is 3: pt(6) if a is 2 and b is 2 and c in [4, 5]: x4, y4, x5, y5 = g(x1, y1, r1, x2, y2, r2) if abs((x4 - x3) ** 2 + (y4 - y3) ** 2 - r3 ** 2) < 1e-6 or abs((x5 - x3) ** 2 + (y5 - y3) ** 2 - r3 ** 2) < 1e-6: pt(6) else: pt(7) if a is 2 and b is 3: pt(5) if a is 2 and b in [4, 5]: pt(6) if a is 3 and b in [3, 4, 5]: pt(4) if a is 4 and b is 4 and c is 4: pt(5) if a is 4 and b is 4 and c is 5: pt(4) if a is 4 and b is 5 and c is 5: pt(5) if a is 5 and b is 5 and c is 5: pt(4) ```
vfc_23476
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/933/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n0 0 1\n2 0 1\n4 0 1\n", "output": "4\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3693
Solve the following coding problem using the programming language python: You are given two squares, one with sides parallel to the coordinate axes, and another one with sides at 45 degrees to the coordinate axes. Find whether the two squares intersect. The interior of the square is considered to be part of the square, i.e. if one square is completely inside another, they intersect. If the two squares only share one common point, they are also considered to intersect. -----Input----- The input data consists of two lines, one for each square, both containing 4 pairs of integers. Each pair represents coordinates of one vertex of the square. Coordinates within each line are either in clockwise or counterclockwise order. The first line contains the coordinates of the square with sides parallel to the coordinate axes, the second line contains the coordinates of the square at 45 degrees. All the values are integer and between $-100$ and $100$. -----Output----- Print "Yes" if squares intersect, otherwise print "No". You can print each letter in any case (upper or lower). -----Examples----- Input 0 0 6 0 6 6 0 6 1 3 3 5 5 3 3 1 Output YES Input 0 0 6 0 6 6 0 6 7 3 9 5 11 3 9 1 Output NO Input 6 0 6 6 0 6 0 0 7 4 4 7 7 10 10 7 Output YES -----Note----- In the first example the second square lies entirely within the first square, so they do intersect. In the second sample squares do not have any points in common. Here are images corresponding to the samples: [Image] [Image] [Image] 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())) b = list(map(int, input().split())) x1 = min(a[0], a[2], a[4], a[6]) x2 = max(a[0], a[2], a[4], a[6]) y1 = min(a[1], a[3], a[5], a[7]) y2 = max(a[1], a[3], a[5], a[7]) d1 = min(b[0] - b[1], b[2] - b[3], b[4] - b[5], b[6] - b[7]) d2 = max(b[0] - b[1], b[2] - b[3], b[4] - b[5], b[6] - b[7]) s1 = min(b[0] + b[1], b[2] + b[3], b[4] + b[5], b[6] + b[7]) s2 = max(b[0] + b[1], b[2] + b[3], b[4] + b[5], b[6] + b[7]) for x in range(-100, 101): for y in range(-100, 101): d = x - y s = x + y if x1 <= x <= x2 and y1 <= y <= y2 and s1 <= s <= s2 and d1 <= d <= d2: print("YES") return print("NO") ```
vfc_23480
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/993/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "0 0 6 0 6 6 0 6\n1 3 3 5 5 3 3 1\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "0 0 6 0 6 6 0 6\n7 3 9 5 11 3 9 1\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6 0 6 6 0 6 0 0\n7 4 4 7 7 10 10 7\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "0 0 6 0 6 6 0 6\n8 4 4 8 8 12 12 8\n", "output": "YES\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3694
Solve the following coding problem using the programming language python: Tokitsukaze and CSL are playing a little game of stones. In the beginning, there are $n$ piles of stones, the $i$-th pile of which has $a_i$ stones. The two players take turns making moves. Tokitsukaze moves first. On each turn the player chooses a nonempty pile and removes exactly one stone from the pile. A player loses if all of the piles are empty before his turn, or if after removing the stone, two piles (possibly empty) contain the same number of stones. Supposing that both players play optimally, who will win the game? Consider an example: $n=3$ and sizes of piles are $a_1=2$, $a_2=3$, $a_3=0$. It is impossible to choose the empty pile, so Tokitsukaze has two choices: the first and the second piles. If she chooses the first pile then the state will be $[1, 3, 0]$ and it is a good move. But if she chooses the second pile then the state will be $[2, 2, 0]$ and she immediately loses. So the only good move for her is to choose the first pile. Supposing that both players always take their best moves and never make mistakes, who will win the game? Note that even if there are two piles with the same number of stones at the beginning, Tokitsukaze may still be able to make a valid first move. It is only necessary that there are no two piles with the same number of stones after she moves. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the number of piles. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_1, a_2, \ldots, a_n \le 10^9$), which mean the $i$-th pile has $a_i$ stones. -----Output----- Print "sjfnb" (without quotes) if Tokitsukaze will win, or "cslnb" (without quotes) if CSL will win. Note the output characters are case-sensitive. -----Examples----- Input 1 0 Output cslnb Input 2 1 0 Output cslnb Input 2 2 2 Output sjfnb Input 3 2 3 1 Output sjfnb -----Note----- In the first example, Tokitsukaze cannot take any stone, so CSL will win. In the second example, Tokitsukaze can only take a stone from the first pile, and then, even though they have no stone, these two piles will have the same number of stones, which implies CSL will win. In the third example, Tokitsukaze will win. Here is one of the optimal ways: Firstly, Tokitsukaze can choose the first pile and take a stone from that pile. Then, CSL can only choose the first pile, because if he chooses the second pile, he will lose immediately. Finally, Tokitsukaze can choose the second pile, and then CSL will have no choice but to lose. In the fourth example, they only have one good choice at any time, so Tokitsukaze can make the game lasting as long as possible and finally win. 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()) a = list(map(int,input().split())) dupes = 0 dupeVal = -1 d = set() for el in a: if el in d: dupes += 1 dupeVal = el else: d.add(el) inPlay = True if dupes > 1: print('cslnb') inPlay = False elif dupes == 1: if dupeVal == 0 or (dupeVal - 1) in d: print('cslnb') inPlay = False if inPlay: finalSum = (n*(n-1))//2 Sum = sum(a) if (Sum - finalSum) % 2 == 0: print('cslnb') else: print('sjfnb') ```
vfc_23484
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1190/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1\n0\n", "output": "cslnb\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3695
Solve the following coding problem using the programming language python: A new dog show on TV is starting next week. On the show dogs are required to demonstrate bottomless stomach, strategic thinking and self-preservation instinct. You and your dog are invited to compete with other participants and naturally you want to win! On the show a dog needs to eat as many bowls of dog food as possible (bottomless stomach helps here). Dogs compete separately of each other and the rules are as follows: At the start of the show the dog and the bowls are located on a line. The dog starts at position x = 0 and n bowls are located at positions x = 1, x = 2, ..., x = n. The bowls are numbered from 1 to n from left to right. After the show starts the dog immediately begins to run to the right to the first bowl. The food inside bowls is not ready for eating at the start because it is too hot (dog's self-preservation instinct prevents eating). More formally, the dog can eat from the i-th bowl after t_{i} seconds from the start of the show or later. It takes dog 1 second to move from the position x to the position x + 1. The dog is not allowed to move to the left, the dog runs only to the right with the constant speed 1 distance unit per second. When the dog reaches a bowl (say, the bowl i), the following cases are possible: the food had cooled down (i.e. it passed at least t_{i} seconds from the show start): the dog immediately eats the food and runs to the right without any stop, the food is hot (i.e. it passed less than t_{i} seconds from the show start): the dog has two options: to wait for the i-th bowl, eat the food and continue to run at the moment t_{i} or to skip the i-th bowl and continue to run to the right without any stop. After T seconds from the start the show ends. If the dog reaches a bowl of food at moment T the dog can not eat it. The show stops before T seconds if the dog had run to the right of the last bowl. You need to help your dog create a strategy with which the maximum possible number of bowls of food will be eaten in T seconds. -----Input----- Two integer numbers are given in the first line - n and T (1 ≤ n ≤ 200 000, 1 ≤ T ≤ 2·10^9) — the number of bowls of food and the time when the dog is stopped. On the next line numbers t_1, t_2, ..., t_{n} (1 ≤ t_{i} ≤ 10^9) are given, where t_{i} is the moment of time when the i-th bowl of food is ready for eating. -----Output----- Output a single integer — the maximum number of bowls of food the dog will be able to eat in T seconds. -----Examples----- Input 3 5 1 5 3 Output 2 Input 1 2 1 Output 1 Input 1 1 1 Output 0 -----Note----- In the first example the dog should skip the second bowl to eat from the two bowls (the first and the third). The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, t = map(int,input().split()) wait = list(map(int,input().split())) seg = [] for i in range(n): # to get the food in wait[i], wait __ seconds before running m = max(0, wait[i]-i-1) M = t-i-2 if m > M: continue seg.append((m, -1)) seg.append((M, 1)) seg.sort() ans = 0 cur = 0 for t, q in seg: cur-= q ans = max(ans, cur) print(ans) ```
vfc_23488
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/847/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 5\n1 5 3\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 2\n1\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1\n1\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3696
Solve the following coding problem using the programming language python: Suppose you have two polynomials $A(x) = \sum_{k = 0}^{n} a_{k} x^{k}$ and $B(x) = \sum_{k = 0}^{m} b_{k} x^{k}$. Then polynomial $A(x)$ can be uniquely represented in the following way:$A(x) = B(x) \cdot D(x) + R(x), \operatorname{deg} R(x) < \operatorname{deg} B(x)$ This can be done using long division. Here, $\operatorname{deg} P(x)$ denotes the degree of polynomial P(x). $R(x)$ is called the remainder of division of polynomial $A(x)$ by polynomial $B(x)$, it is also denoted as $A \operatorname{mod} B$. Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials $(A, B)$. If the polynomial $B(x)$ is zero, the result is $A(x)$, otherwise the result is the value the algorithm returns for pair $(B, A \operatorname{mod} B)$. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question. You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair $(A, B)$ to pair $(B, A \operatorname{mod} B)$. -----Input----- You are given a single integer n (1 ≤ n ≤ 150) — the number of steps of the algorithm you need to reach. -----Output----- Print two polynomials in the following format. In the first line print a single integer m (0 ≤ m ≤ n) — the degree of the polynomial. In the second line print m + 1 integers between - 1 and 1 — the coefficients of the polynomial, from constant to leading. The degree of the first polynomial should be greater than the degree of the second polynomial, the leading coefficients should be equal to 1. Euclid's algorithm should perform exactly n steps when called using these polynomials. If there is no answer for the given n, print -1. If there are multiple answer, print any of them. -----Examples----- Input 1 Output 1 0 1 0 1 Input 2 Output 2 -1 0 1 1 0 1 -----Note----- In the second example you can print polynomials x^2 - 1 and x. The sequence of transitions is(x^2 - 1, x) → (x, - 1) → ( - 1, 0). There are two steps in it. 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()) a, b = [1], [0] for i in range(n): new_b = a[:] a1 = a[:] a2 = a[:] a1.append(0) a2.append(0) for i in range(-1, -len(b) - 1, -1): a1[i] += b[i] for i in range(-1, -len(b) - 1, -1): a2[i] -= b[i] if max([abs(kek) for kek in a1]) < 2: a = a1 elif max([abs(kek) for kek in a2]) < 2: a = a2 else: print("oops") return b = new_b print(len(a) - 1) print(*(a[::-1])) print(len(b) - 1) print(*(b[::-1])) ```
vfc_23492
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/901/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1\n", "output": "1\n0 1\n0\n1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n", "output": "2\n-1 0 1\n1\n0 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n", "output": "3\n0 0 0 1\n2\n-1 0 1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3697
Solve the following coding problem using the programming language python: The elections to Berland parliament are happening today. Voting is in full swing! Totally there are n candidates, they are numbered from 1 to n. Based on election results k (1 ≤ k ≤ n) top candidates will take seats in the parliament. After the end of the voting the number of votes for each candidate is calculated. In the resulting table the candidates are ordered by the number of votes. In case of tie (equal number of votes) they are ordered by the time of the last vote given. The candidate with ealier last vote stands higher in the resulting table. So in the resulting table candidates are sorted by the number of votes (more votes stand for the higher place) and if two candidates have equal number of votes they are sorted by the time of last vote (earlier last vote stands for the higher place). There is no way for a candidate with zero votes to take a seat in the parliament. So it is possible that less than k candidates will take a seat in the parliament. In Berland there are m citizens who can vote. Each of them will vote for some candidate. Each citizen will give a vote to exactly one of n candidates. There is no option "against everyone" on the elections. It is not accepted to spoil bulletins or not to go to elections. So each of m citizens will vote for exactly one of n candidates. At the moment a citizens have voted already (1 ≤ a ≤ m). This is an open election, so for each citizen it is known the candidate for which the citizen has voted. Formally, the j-th citizen voted for the candidate g_{j}. The citizens who already voted are numbered in chronological order; i.e. the (j + 1)-th citizen voted after the j-th. The remaining m - a citizens will vote before the end of elections, each of them will vote for one of n candidates. Your task is to determine for each of n candidates one of the three possible outcomes: a candidate will be elected to the parliament regardless of votes of the remaining m - a citizens; a candidate has chance to be elected to the parliament after all n citizens have voted; a candidate has no chances to be elected to the parliament regardless of votes of the remaining m - a citizens. -----Input----- The first line contains four integers n, k, m and a (1 ≤ k ≤ n ≤ 100, 1 ≤ m ≤ 100, 1 ≤ a ≤ m) — the number of candidates, the number of seats in the parliament, the number of Berland citizens and the number of citizens who already have voted. The second line contains a sequence of a integers g_1, g_2, ..., g_{a} (1 ≤ g_{j} ≤ n), where g_{j} is the candidate for which the j-th citizen has voted. Citizens who already voted are numbered in increasing order of voting times. -----Output----- Print the sequence consisting of n integers r_1, r_2, ..., r_{n} where: r_{i} = 1 means that the i-th candidate is guaranteed to take seat in the parliament regardless of votes of the remaining m - a citizens; r_{i} = 2 means that the i-th candidate has a chance to take a seat in the parliament, i.e. the remaining m - a citizens can vote in such a way that the candidate will take a seat in the parliament; r_{i} = 3 means that the i-th candidate will not take a seat in the parliament regardless of votes of the remaining m - a citizens. -----Examples----- Input 3 1 5 4 1 2 1 3 Output 1 3 3 Input 3 1 5 3 1 3 1 Output 2 3 2 Input 3 2 5 3 1 3 1 Output 1 2 2 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python class State: __slots__ = ['candidate', 'votes', 'last_vote'] def __init__(self, cand, votes, last): self.candidate = cand self.votes = votes self.last_vote = last def beats(self, other, extra): return self.votes + extra > other.votes def main(): candidates, seats, people, voted = map(int, input().split()) votes = [0 for i in range(candidates)] last_vote = [0 for i in range(candidates)] if candidates == 1: print(1) return v = list(map(int, input().split())) for t in range(voted): cand = v[t] - 1 votes[cand] += 1 last_vote[cand] = t states = [State(i, votes[i], last_vote[i]) for i in range(candidates)] states = sorted(states, key = lambda x : (x.votes, -x.last_vote)) res = [0 for i in range(candidates)] for i in range(candidates): if i < candidates - seats: low = candidates - seats if states[i].beats(states[low], people - voted): res[states[i].candidate] = 2 else: res[states[i].candidate] = 3 else: extra = people - voted other = i - 1 place = i if extra == 0 and states[i].votes == 0: res[states[i].candidate] = 3 continue while other >= 0 and extra > 0: needed = states[i].votes - states[other].votes + 1 if needed <= extra: extra -= needed; place -= 1 other -= 1 else: break res[states[i].candidate] = (1 if place + seats >= candidates and states[i].votes > 0 else 2) for i in res: print(i, end = ' ') main() ```
vfc_23496
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/847/F", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 1 5 4\n1 2 1 3\n", "output": "1 3 3 ", "type": "stdin_stdout" } ] }
apps
verifiable_code
3698
Solve the following coding problem using the programming language python: The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 13_10 = 1101_2, so it has 3 bits set and 13 will be reduced to 3 in one operation. He calls a number special if the minimum number of operations to reduce it to 1 is k. He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination! Since the answer can be large, output it modulo 10^9 + 7. -----Input----- The first line contains integer n (1 ≤ n < 2^1000). The second line contains integer k (0 ≤ k ≤ 1000). Note that n is given in its binary representation without any leading zeros. -----Output----- Output a single integer — the number of special numbers not greater than n, modulo 10^9 + 7. -----Examples----- Input 110 2 Output 3 Input 111111011 2 Output 169 -----Note----- In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2). The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python mas = [[] for i in range(1001)] mas[0].append(1) mas[0].append(1) for i in range(1, 1000): mas[i].append(1) for j in range(1, i): mas[i].append((mas[i - 1][j] + mas[i - 1][j - 1]) % (10 ** 9 + 7)) mas[i].append(1) def c(k, n): if k > n: return 0 if k < 0: return 0 nonlocal mas return mas[n][k] m = [0] * 1000 for i in range(1, 1000): nw = i t = 0 while nw != 1: nw = sum([int(j) for j in str(bin(nw)[2:])]) t += 1 m[i] = t m[1] = 0 n = input() k = int(input()) if k >= 6: print(0) return if k == 0: print(1) return if k == 1: print(len(n) - 1) return ans = 0 for kkk in range(1, 1000): if m[kkk] == k - 1: nw = kkk t = 0 for i in range(len(n)): if n[i] == '1': ans += c(nw - t, len(n) - 1 - i) ans %= 10 ** 9 + 7 t += 1 if sum([int(j) for j in n]) == kkk: ans += 1 ans %= 10 ** 9 + 7 print(ans) ```
vfc_23500
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/914/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "110\n2\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "111111011\n2\n", "output": "169\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3700
Solve the following coding problem using the programming language python: Tanechka is shopping in the toy shop. There are exactly $n$ toys in the shop for sale, the cost of the $i$-th toy is $i$ burles. She wants to choose two toys in such a way that their total cost is $k$ burles. How many ways to do that does she have? Each toy appears in the shop exactly once. Pairs $(a, b)$ and $(b, a)$ are considered equal. Pairs $(a, b)$, where $a=b$, are not allowed. -----Input----- The first line of the input contains two integers $n$, $k$ ($1 \le n, k \le 10^{14}$) — the number of toys and the expected total cost of the pair of toys. -----Output----- Print the number of ways to choose the pair of toys satisfying the condition above. Print 0, if Tanechka can choose no pair of toys in such a way that their total cost is $k$ burles. -----Examples----- Input 8 5 Output 2 Input 8 15 Output 1 Input 7 20 Output 0 Input 1000000000000 1000000000001 Output 500000000000 -----Note----- In the first example Tanechka can choose the pair of toys ($1, 4$) or the pair of toys ($2, 3$). In the second example Tanechka can choose only the pair of toys ($7, 8$). In the third example choosing any pair of toys will lead to the total cost less than $20$. So the answer is 0. In the fourth example she can choose the following pairs: $(1, 1000000000000)$, $(2, 999999999999)$, $(3, 999999999998)$, ..., $(500000000000, 500000000001)$. The number of such pairs is exactly $500000000000$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python #!/usr/bin/env python3 n, k = list(map(int, input().split())) ans = max(0, min(n, k - 1) - k // 2) print(ans) ```
vfc_23508
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1023/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "8 5\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "8 15\n", "output": "1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3702
Solve the following coding problem using the programming language python: The well-known Fibonacci sequence $F_0, F_1, F_2,\ldots $ is defined as follows: $F_0 = 0, F_1 = 1$. For each $i \geq 2$: $F_i = F_{i - 1} + F_{i - 2}$. Given an increasing arithmetic sequence of positive integers with $n$ elements: $(a, a + d, a + 2\cdot d,\ldots, a + (n - 1)\cdot d)$. You need to find another increasing arithmetic sequence of positive integers with $n$ elements $(b, b + e, b + 2\cdot e,\ldots, b + (n - 1)\cdot e)$ such that: $0 < b, e < 2^{64}$, for all $0\leq i < n$, the decimal representation of $a + i \cdot d$ appears as substring in the last $18$ digits of the decimal representation of $F_{b + i \cdot e}$ (if this number has less than $18$ digits, then we consider all its digits). -----Input----- The first line contains three positive integers $n$, $a$, $d$ ($1 \leq n, a, d, a + (n - 1) \cdot d < 10^6$). -----Output----- If no such arithmetic sequence exists, print $-1$. Otherwise, print two integers $b$ and $e$, separated by space in a single line ($0 < b, e < 2^{64}$). If there are many answers, you can output any of them. -----Examples----- Input 3 1 1 Output 2 1 Input 5 1 2 Output 19 5 -----Note----- In the first test case, we can choose $(b, e) = (2, 1)$, because $F_2 = 1, F_3 = 2, F_4 = 3$. In the second test case, we can choose $(b, e) = (19, 5)$ because: $F_{19} = 4181$ contains $1$; $F_{24} = 46368$ contains $3$; $F_{29} = 514229$ contains $5$; $F_{34} = 5702887$ contains $7$; $F_{39} = 63245986$ contains $9$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n,a,d=map(int,input().split()) print(368131125*a%10**9*12*10**9+1,368131125*d%10**9*12*10**9) ```
vfc_23516
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1264/F", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 1 1\n", "output": "4417573500000000001 4417573500000000000\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3704
Solve the following coding problem using the programming language python: Berkomnadzor — Federal Service for Supervision of Communications, Information Technology and Mass Media — is a Berland federal executive body that protects ordinary residents of Berland from the threats of modern internet. Berkomnadzor maintains a list of prohibited IPv4 subnets (blacklist) and a list of allowed IPv4 subnets (whitelist). All Internet Service Providers (ISPs) in Berland must configure the network equipment to block access to all IPv4 addresses matching the blacklist. Also ISPs must provide access (that is, do not block) to all IPv4 addresses matching the whitelist. If an IPv4 address does not match either of those lists, it's up to the ISP to decide whether to block it or not. An IPv4 address matches the blacklist (whitelist) if and only if it matches some subnet from the blacklist (whitelist). An IPv4 address can belong to a whitelist and to a blacklist at the same time, this situation leads to a contradiction (see no solution case in the output description). An IPv4 address is a 32-bit unsigned integer written in the form $a.b.c.d$, where each of the values $a,b,c,d$ is called an octet and is an integer from $0$ to $255$ written in decimal notation. For example, IPv4 address $192.168.0.1$ can be converted to a 32-bit number using the following expression $192 \cdot 2^{24} + 168 \cdot 2^{16} + 0 \cdot 2^8 + 1 \cdot 2^0$. First octet $a$ encodes the most significant (leftmost) $8$ bits, the octets $b$ and $c$ — the following blocks of $8$ bits (in this order), and the octet $d$ encodes the least significant (rightmost) $8$ bits. The IPv4 network in Berland is slightly different from the rest of the world. There are no reserved or internal addresses in Berland and use all $2^{32}$ possible values. An IPv4 subnet is represented either as $a.b.c.d$ or as $a.b.c.d/x$ (where $0 \le x \le 32$). A subnet $a.b.c.d$ contains a single address $a.b.c.d$. A subnet $a.b.c.d/x$ contains all IPv4 addresses with $x$ leftmost (most significant) bits equal to $x$ leftmost bits of the address $a.b.c.d$. It is required that $32 - x$ rightmost (least significant) bits of subnet $a.b.c.d/x$ are zeroes. Naturally it happens that all addresses matching subnet $a.b.c.d/x$ form a continuous range. The range starts with address $a.b.c.d$ (its rightmost $32 - x$ bits are zeroes). The range ends with address which $x$ leftmost bits equal to $x$ leftmost bits of address $a.b.c.d$, and its $32 - x$ rightmost bits are all ones. Subnet contains exactly $2^{32-x}$ addresses. Subnet $a.b.c.d/32$ contains exactly one address and can also be represented by just $a.b.c.d$. For example subnet $192.168.0.0/24$ contains range of 256 addresses. $192.168.0.0$ is the first address of the range, and $192.168.0.255$ is the last one. Berkomnadzor's engineers have devised a plan to improve performance of Berland's global network. Instead of maintaining both whitelist and blacklist they want to build only a single optimised blacklist containing minimal number of subnets. The idea is to block all IPv4 addresses matching the optimised blacklist and allow all the rest addresses. Of course, IPv4 addresses from the old blacklist must remain blocked and all IPv4 addresses from the old whitelist must still be allowed. Those IPv4 addresses which matched neither the old blacklist nor the old whitelist may be either blocked or allowed regardless of their accessibility before. Please write a program which takes blacklist and whitelist as input and produces optimised blacklist. The optimised blacklist must contain the minimal possible number of subnets and satisfy all IPv4 addresses accessibility requirements mentioned above. IPv4 subnets in the source lists may intersect arbitrarily. Please output a single number -1 if some IPv4 address matches both source whitelist and blacklist. -----Input----- The first line of the input contains single integer $n$ ($1 \le n \le 2\cdot10^5$) — total number of IPv4 subnets in the input. The following $n$ lines contain IPv4 subnets. Each line starts with either '-' or '+' sign, which indicates if the subnet belongs to the blacklist or to the whitelist correspondingly. It is followed, without any spaces, by the IPv4 subnet in $a.b.c.d$ or $a.b.c.d/x$ format ($0 \le x \le 32$). The blacklist always contains at least one subnet. All of the IPv4 subnets given in the input are valid. Integer numbers do not start with extra leading zeroes. The provided IPv4 subnets can intersect arbitrarily. -----Output----- Output -1, if there is an IPv4 address that matches both the whitelist and the blacklist. Otherwise output $t$ — the length of the optimised blacklist, followed by $t$ subnets, with each subnet on a new line. Subnets may be printed in arbitrary order. All addresses matching the source blacklist must match the optimised blacklist. All addresses matching the source whitelist must not match the optimised blacklist. You can print a subnet $a.b.c.d/32$ in any of two ways: as $a.b.c.d/32$ or as $a.b.c.d$. If there is more than one solution, output any. -----Examples----- Input 1 -149.154.167.99 Output 1 0.0.0.0/0 Input 4 -149.154.167.99 +149.154.167.100/30 +149.154.167.128/25 -149.154.167.120/29 Output 2 149.154.167.99 149.154.167.120/29 Input 5 -127.0.0.4/31 +127.0.0.8 +127.0.0.0/30 -195.82.146.208/29 -127.0.0.6/31 Output 2 195.0.0.0/8 127.0.0.4/30 Input 2 +127.0.0.1/32 -127.0.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 #!/usr/bin/env python3 # Copied solution import collections import sys import traceback class Input(object): def __init__(self): self.fh = sys.stdin def next_line(self): while True: line = sys.stdin.readline() if line == '\n': continue return line def next_line_ints(self): line = self.next_line() return [int(x) for x in line.split()] def next_line_strs(self): line = self.next_line() return line.split() class Node(object): def __init__(self, color, subtree_color): self.left = self.right = None self.color = color self.subtree_color = subtree_color def list_to_number(list): """Return (color, bits, number).""" color = 1 if list[0] == '-' else 2 values = list[1:].split('/') bits = 32 if len(values) == 2: bits = int(values[1]) nums = values[0].split('.') number = 0 for num in nums: number = number * 256 + int(num) return (color, bits, number) def add_list_to_tree(tree, list): color, bits, number = list_to_number(list) shift = 31 for _ in range(bits): tree.subtree_color |= color value = (number >> shift) & 1 if value == 0: if not tree.left: tree.left = Node(0, 0) tree = tree.left else: if not tree.right: tree.right = Node(0, 0) tree = tree.right shift -= 1 tree.subtree_color |= color tree.color |= color def check_tree(tree): if not tree: return True if tree.color == 3 or (tree.color and (tree.subtree_color & ~tree.color)): return False return check_tree(tree.left) and check_tree(tree.right) def number_to_list(number, bits): number <<= (32 - bits) values = [] for _ in range(4): #print('number = {}'.format(number)) values.append(str(number % 256)) number //= 256 values = values[::-1] return '.'.join(values) + '/' + str(bits) def get_optimized(tree, optimized, number, bits): if not tree or (tree.subtree_color & 1) == 0: return if tree.subtree_color == 1: list = number_to_list(number, bits) #print('number_to_list({}, {}) = {}'.format(number, bits, list)) optimized.append(list) return get_optimized(tree.left, optimized, number * 2, bits + 1) get_optimized(tree.right, optimized, number * 2 + 1, bits + 1) def get_optimized_lists(lists): tree = Node(0, 0) for list in lists: add_list_to_tree(tree, list) if not check_tree(tree): return None optimized = [] get_optimized(tree, optimized, 0, 0) return optimized def main(): input = Input() while True: try: nums = input.next_line_ints() if not nums: break n, = nums if n == -1: break lists = [] for _ in range(n): lists.append(input.next_line_strs()[0]) except: print('read input failed') try: optimized = get_optimized_lists(lists) if optimized is None: print("-1") else: print("{}".format(len(optimized))) for l in optimized: print("{}".format(l)) except: traceback.print_exc(file=sys.stdout) print('get_min_dist failed') main() ```
vfc_23524
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1070/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1\n-149.154.167.99\n", "output": "1\n0.0.0.0/0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n-149.154.167.99\n+149.154.167.100/30\n+149.154.167.128/25\n-149.154.167.120/29\n", "output": "2\n149.154.167.96/30\n149.154.167.112/28\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3705
Solve the following coding problem using the programming language python: Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have $n$ cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. -----Input----- The first line contains an integer $n$ — the number of cards with digits that you have ($1 \leq n \leq 100$). The second line contains a string of $n$ digits (characters "0", "1", ..., "9") $s_1, s_2, \ldots, s_n$. The string will not contain any other characters, such as leading or trailing spaces. -----Output----- If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. -----Examples----- Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 -----Note----- In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards. 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()) s=list(input()) print(min(s.count('8'),n//11)) ```
vfc_23528
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1060/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "11\n00000000008\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "22\n0011223344556677889988\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "11\n31415926535\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "output": "9\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "output": "9\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3707
Solve the following coding problem using the programming language python: In some game by Playrix it takes t minutes for an oven to bake k carrot cakes, all cakes are ready at the same moment t minutes after they started baking. Arkady needs at least n cakes to complete a task, but he currently don't have any. However, he has infinitely many ingredients and one oven. Moreover, Arkady can build one more similar oven to make the process faster, it would take d minutes to build the oven. While the new oven is being built, only old one can bake cakes, after the new oven is built, both ovens bake simultaneously. Arkady can't build more than one oven. Determine if it is reasonable to build the second oven, i.e. will it decrease the minimum time needed to get n cakes or not. If the time needed with the second oven is the same as with one oven, then it is unreasonable. -----Input----- The only line contains four integers n, t, k, d (1 ≤ n, t, k, d ≤ 1 000) — the number of cakes needed, the time needed for one oven to bake k cakes, the number of cakes baked at the same time, the time needed to build the second oven. -----Output----- If it is reasonable to build the second oven, print "YES". Otherwise print "NO". -----Examples----- Input 8 6 4 5 Output YES Input 8 6 4 6 Output NO Input 10 3 11 4 Output NO Input 4 2 1 4 Output YES -----Note----- In the first example it is possible to get 8 cakes in 12 minutes using one oven. The second oven can be built in 5 minutes, so after 6 minutes the first oven bakes 4 cakes, the second oven bakes 4 more ovens after 11 minutes. Thus, it is reasonable to build the second oven. In the second example it doesn't matter whether we build the second oven or not, thus it takes 12 minutes to bake 8 cakes in both cases. Thus, it is unreasonable to build the second oven. In the third example the first oven bakes 11 cakes in 3 minutes, that is more than needed 10. It is unreasonable to build the second oven, because its building takes more time that baking the needed number of cakes using the only oven. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import collections as col import itertools as its import sys import operator from bisect import bisect_left, bisect_right from copy import copy, deepcopy from math import factorial as fact class Solver: def __init__(self): pass def solve(self): n, t, k, d = list(map(int, input().split())) n = (n + k - 1) // k if d < (n-1) * t: print('YES') else: print('NO') def __starting_point(): s = Solver() s.solve() __starting_point() ```
vfc_23536
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/799/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "8 6 4 5\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "8 6 4 6\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 3 11 4\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 2 1 4\n", "output": "YES\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3710
Solve the following coding problem using the programming language python: Today Pari and Arya are playing a game called Remainders. Pari chooses two positive integer x and k, and tells Arya k but not x. Arya have to find the value $x \text{mod} k$. There are n ancient numbers c_1, c_2, ..., c_{n} and Pari has to tell Arya $x \operatorname{mod} c_{i}$ if Arya wants. Given k and the ancient values, tell us if Arya has a winning strategy independent of value of x or not. Formally, is it true that Arya can understand the value $x \text{mod} k$ for any positive integer x? Note, that $x \text{mod} y$ means the remainder of x after dividing it by y. -----Input----- The first line of the input contains two integers n and k (1 ≤ n, k ≤ 1 000 000) — the number of ancient integers and value k that is chosen by Pari. The second line contains n integers c_1, c_2, ..., c_{n} (1 ≤ c_{i} ≤ 1 000 000). -----Output----- Print "Yes" (without quotes) if Arya has a winning strategy independent of value of x, or "No" (without quotes) otherwise. -----Examples----- Input 4 5 2 3 5 12 Output Yes Input 2 7 2 3 Output No -----Note----- In the first sample, Arya can understand $x \operatorname{mod} 5$ because 5 is one of the ancient numbers. In the second sample, Arya can't be sure what $x \text{mod} 7$ is. For example 1 and 7 have the same remainders after dividing by 2 and 3, but they differ in remainders after dividing by 7. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from math import * from sys import * n, k = map(int, stdin.readline().split(" ")) a = 1; for c in map(int, stdin.readline().split(" ")): a = a * (gcd(k, c)//gcd(a, c)) if a%k == 0: print("Yes") else: print("No") ```
vfc_23548
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/687/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4 5\n2 3 5 12\n", "output": "Yes\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 7\n2 3\n", "output": "No\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 6\n8\n", "output": "No\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 3\n9 4\n", "output": "Yes\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 16\n19 16 13 9\n", "output": "Yes\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 10\n5 16 19 9 17\n", "output": "Yes\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3711
Solve the following coding problem using the programming language python: Jzzhu has a big rectangular chocolate bar that consists of n × m unit squares. He wants to cut this bar exactly k times. Each cut must meet the following requirements: each cut should be straight (horizontal or vertical); each cut should go along edges of unit squares (it is prohibited to divide any unit chocolate square with cut); each cut should go inside the whole chocolate bar, and all cuts must be distinct. The picture below shows a possible way to cut a 5 × 6 chocolate for 5 times. [Image] Imagine Jzzhu have made k cuts and the big chocolate is splitted into several pieces. Consider the smallest (by area) piece of the chocolate, Jzzhu wants this piece to be as large as possible. What is the maximum possible area of smallest piece he can get with exactly k cuts? The area of a chocolate piece is the number of unit squares in it. -----Input----- A single line contains three integers n, m, k (1 ≤ n, m ≤ 10^9; 1 ≤ k ≤ 2·10^9). -----Output----- Output a single integer representing the answer. If it is impossible to cut the big chocolate k times, print -1. -----Examples----- Input 3 4 1 Output 6 Input 6 4 2 Output 8 Input 2 3 4 Output -1 -----Note----- In the first sample, Jzzhu can cut the chocolate following the picture below: [Image] In the second sample the optimal division looks like this: [Image] In the third sample, it's impossible to cut a 2 × 3 chocolate 4 times. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n,m,k = [int(x) for x in input().split()] if k+2>n+m: print(-1) else: if k >= n: alpha = m // (k-n+2) else: alpha = m * (n // (k+1)) if k >= m: beta = n // (k-m+2) else: beta = n * (m // (k + 1)) print(max(alpha, beta)) ```
vfc_23552
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/449/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 4 1\n", "output": "6\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6 4 2\n", "output": "8\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 3 4\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 10 2\n", "output": "30\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3712
Solve the following coding problem using the programming language python: A team of students from the city S is sent to the All-Berland Olympiad in Informatics. Traditionally, they go on the train. All students have bought tickets in one carriage, consisting of n compartments (each compartment has exactly four people). We know that if one compartment contain one or two students, then they get bored, and if one compartment contain three or four students, then the compartment has fun throughout the entire trip. The students want to swap with other people, so that no compartment with students had bored students. To swap places with another person, you need to convince him that it is really necessary. The students can not independently find the necessary arguments, so they asked a sympathetic conductor for help. The conductor can use her life experience to persuade any passenger to switch places with some student. However, the conductor does not want to waste time persuading the wrong people, so she wants to know what is the minimum number of people necessary to persuade her to change places with the students. Your task is to find the number. After all the swaps each compartment should either have no student left, or have a company of three or four students. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^6) — the number of compartments in the carriage. The second line contains n integers a_1, a_2, ..., a_{n} showing how many students ride in each compartment (0 ≤ a_{i} ≤ 4). It is guaranteed that at least one student is riding in the train. -----Output----- If no sequence of swapping seats with other people leads to the desired result, print number "-1" (without the quotes). In another case, print the smallest number of people you need to persuade to swap places. -----Examples----- Input 5 1 2 2 4 3 Output 2 Input 3 4 1 1 Output 2 Input 4 0 3 0 4 Output 0 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python #! /usr/bin/env python n = int(input()) counts = [0] * 5 nums = [int(x) for x in input().split()] for x in nums: counts[x] += 1 s = sum(nums) if s > 2 and s != 5: ans = 0 if counts[1] >= counts[2]: ans += counts[2] counts[3] += counts[2] counts[1] -= counts[2] ans += 2 * (counts[1] // 3) counts[3] += counts[1] // 3 counts[1] %= 3 if counts[3] > 0: ans += counts[1] elif counts[1] != 0: ans += 2 else: ans += counts[1] counts[2] -= counts[1] ans += 2 * (counts[2] // 3) counts[2] %= 3 if counts[4] > 0: ans += counts[2] elif counts[2] != 0: ans += 2 print(ans) else: print(-1) ```
vfc_23556
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/356/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n1 2 2 4 3\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n4 1 1\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n0 3 0 4\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n4 4 3 3 1\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n4 3 4 2 4\n", "output": "1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3713
Solve the following coding problem using the programming language python: Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. -----Input----- The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. -----Output----- Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. -----Examples----- Input 8 10000011 Output 5 Input 2 01 Output 2 -----Note----- In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python x = int(input()) y = input() if x <= 3: print(x) quit() num1 = 0 num0 = 0 for i in y: if i == '1': num1 = max(num1, num0+1) else: num0 = max(num0, num1+1) maxx = max(num1, num0) if '11' not in y and '00' not in y: print(maxx) quit() print(min(maxx+2, x)) ```
vfc_23560
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/603/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "8\n10000011\n", "output": "5\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3714
Solve the following coding problem using the programming language python: As you have noticed, there are lovely girls in Arpa’s land. People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crush_{i}. [Image] Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows. The game consists of rounds. Assume person x wants to start a round, he calls crush_{x} and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crush_{x} calls crush_{crush}_{x} and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time. Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible. Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crush_{i} = i). -----Input----- The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land. The second line contains n integers, i-th of them is crush_{i} (1 ≤ crush_{i} ≤ n) — the number of i-th person's crush. -----Output----- If there is no t satisfying the condition, print -1. Otherwise print such smallest t. -----Examples----- Input 4 2 3 1 4 Output 3 Input 4 4 4 4 4 Output -1 Input 4 2 1 4 3 Output 1 -----Note----- In the first sample suppose t = 3. If the first person starts some round: The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1. The process is similar for the second and the third person. If the fourth person starts some round: The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4. In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa. 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()) w = list(map(int, input().split())) w = [x-1 for x in w] ws = sorted(w) for i in range(n): if i != ws[i]: print(-1) return a = [] vis = [False] * n for i in range(n): if not vis[i]: a.append(0) j = i while not vis[j]: a[-1] += 1 vis[j] = True j = w[j] def gcd(a, b): if b == 0: return a return gcd(b, a % b) ans = 1 for i in a: if i % 2 == 0: i //= 2 ans = ans // gcd(ans, i) * i print(ans) ```
vfc_23564
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/741/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n2 3 1 4\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n4 4 4 4\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n2 1 4 3\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n2 4 3 1 2\n", "output": "-1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3715
Solve the following coding problem using the programming language python: Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. -----Input----- The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 3) separated by space, where: a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. -----Output----- Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: to do sport on any two consecutive days, to write the contest on any two consecutive days. -----Examples----- Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 -----Note----- In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys inp = sys.stdin #inp = open('input.txt', 'r') n = int(inp.readline()) a = list(map(int, inp.readline().split())) best = [0, 0, 0] for i in range(n): nx_best = [0, 0, 0] if a[i] in (1, 3): nx_best[1] = max(best[0], best[2]) + 1 if a[i] in (2, 3): nx_best[2] = max(best[0], best[1]) + 1 nx_best[0] = max(best) best = nx_best[:] print(n - max(best)) ```
vfc_23568
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/698/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n1 3 2 0\n", "output": "2\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3716
Solve the following coding problem using the programming language python: Some days ago, I learned the concept of LCM (least common multiple). I've played with it for several times and I want to make a big number with it. But I also don't want to use many numbers, so I'll choose three positive integers (they don't have to be distinct) which are not greater than n. Can you help me to find the maximum possible least common multiple of these three integers? -----Input----- The first line contains an integer n (1 ≤ n ≤ 10^6) — the n mentioned in the statement. -----Output----- Print a single integer — the maximum possible LCM of three not necessarily distinct positive integers that are not greater than n. -----Examples----- Input 9 Output 504 Input 7 Output 210 -----Note----- The least common multiple of some positive integers is the least positive integer which is multiple for each of them. The result may become very large, 32-bit integer won't be enough. So using 64-bit integers is recommended. For the last example, we can chose numbers 7, 6, 5 and the LCM of them is 7·6·5 = 210. It is the maximum value we can get. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def gcd(a, b): c = a % b return gcd(b, c) if c else b s, a = 0, int(input()) if a < 3: print(a) else: while a ** 3 > s: b = a - 1 while a * b * b > s: if gcd(a, b) == 1: d = a * b c = b - 1 while d * c > s: if gcd(c, d) == 1: s = d * c c -= 1 b -= 1 a -= 1 print(s) ```
vfc_23572
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/235/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "9\n", "output": "504\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7\n", "output": "210\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n", "output": "60\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n", "output": "60\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "33\n", "output": "32736\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3717
Solve the following coding problem using the programming language python: You are given $n$ rectangles on a plane with coordinates of their bottom left and upper right points. Some $(n-1)$ of the given $n$ rectangles have some common point. A point belongs to a rectangle if this point is strictly inside the rectangle or belongs to its boundary. Find any point with integer coordinates that belongs to at least $(n-1)$ given rectangles. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 132\,674$) — the number of given rectangles. Each the next $n$ lines contains four integers $x_1$, $y_1$, $x_2$ and $y_2$ ($-10^9 \le x_1 < x_2 \le 10^9$, $-10^9 \le y_1 < y_2 \le 10^9$) — the coordinates of the bottom left and upper right corners of a rectangle. -----Output----- Print two integers $x$ and $y$ — the coordinates of any point that belongs to at least $(n-1)$ given rectangles. -----Examples----- Input 3 0 0 1 1 1 1 2 2 3 0 4 1 Output 1 1 Input 3 0 0 1 1 0 1 1 2 1 0 2 1 Output 1 1 Input 4 0 0 5 5 0 0 4 4 1 1 4 4 1 1 4 4 Output 1 1 Input 5 0 0 10 8 1 2 6 7 2 3 5 6 3 4 4 5 8 1 9 2 Output 3 4 -----Note----- The picture below shows the rectangles in the first and second samples. The possible answers are highlighted. [Image] The picture below shows the rectangles in the third and fourth samples. [Image] The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def excl_max_list(a): first_max = max(a) imax = a.index(first_max) second_max = max(a[:imax] + a[imax + 1:]) return [second_max if elem == first_max else first_max for elem in a] def excl_min_list(a): first_min = min(a) imin = a.index(first_min) second_min = min(a[:imin] + a[imin + 1:]) return [second_min if elem == first_min else first_min for elem in a] n = int(input()) rectangles = [tuple(map(int, input().split())) for i in range(n)] lefts = [l for l, d, r, u in rectangles] rights = [r for l, d, r, u in rectangles] downs = [d for l, d, r, u in rectangles] ups = [u for l, d, r, u in rectangles] max_lefts = excl_max_list(lefts) max_downs = excl_max_list(downs) min_rights = excl_min_list(rights) min_ups = excl_min_list(ups) for i in range(n): if max_lefts[i] <= min_rights[i] and max_downs[i] <= min_ups[i]: print(max_lefts[i], max_downs[i]) break ```
vfc_23576
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1028/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n0 0 1 1\n1 1 2 2\n3 0 4 1\n", "output": "1 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n0 0 1 1\n0 1 1 2\n1 0 2 1\n", "output": "1 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n0 0 5 5\n0 0 4 4\n1 1 4 4\n1 1 4 4\n", "output": "1 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n0 0 10 8\n1 2 6 7\n2 3 5 6\n3 4 4 5\n8 1 9 2\n", "output": "3 4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n6 77 44 98\n4 3 84 92\n8 23 66 89\n28 39 80 83\n", "output": "28 39\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n-44 -47 -17 5\n-40 -21 46 22\n-45 -47 -4 7\n-45 -37 17 10\n-44 -24 42 6\n-40 -17 22 15\n-50 -46 -29 17\n-19 -41 9 34\n-46 -24 19 27\n-42 -19 20 30\n", "output": "-19 -17\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3718
Solve the following coding problem using the programming language python: Limak is a little polar bear. He has n balls, the i-th ball has size t_{i}. Limak wants to give one ball to each of his three friends. Giving gifts isn't easy — there are two rules Limak must obey to make friends happy: No two friends can get balls of the same size. No two friends can get balls of sizes that differ by more than 2. For example, Limak can choose balls with sizes 4, 5 and 3, or balls with sizes 90, 91 and 92. But he can't choose balls with sizes 5, 5 and 6 (two friends would get balls of the same size), and he can't choose balls with sizes 30, 31 and 33 (because sizes 30 and 33 differ by more than 2). Your task is to check whether Limak can choose three balls that satisfy conditions above. -----Input----- The first line of the input contains one integer n (3 ≤ n ≤ 50) — the number of balls Limak has. The second line contains n integers t_1, t_2, ..., t_{n} (1 ≤ t_{i} ≤ 1000) where t_{i} denotes the size of the i-th ball. -----Output----- Print "YES" (without quotes) if Limak can choose three balls of distinct sizes, such that any two of them differ by no more than 2. Otherwise, print "NO" (without quotes). -----Examples----- Input 4 18 55 16 17 Output YES Input 6 40 41 43 44 44 44 Output NO Input 8 5 972 3 4 1 4 970 971 Output YES -----Note----- In the first sample, there are 4 balls and Limak is able to choose three of them to satisfy the rules. He must must choose balls with sizes 18, 16 and 17. In the second sample, there is no way to give gifts to three friends without breaking the rules. In the third sample, there is even more than one way to choose balls: Choose balls with sizes 3, 4 and 5. Choose balls with sizes 972, 970, 971. 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()) a = sorted(list(set(map(int, input().split())))) for i in range(2, len(a)): if a[i] - a[i - 2] <= 2: print('YES') break else: print('NO') ```
vfc_23580
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/653/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n18 55 16 17\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n40 41 43 44 44 44\n", "output": "NO\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3719
Solve the following coding problem using the programming language python: There are two small spaceship, surrounded by two groups of enemy larger spaceships. The space is a two-dimensional plane, and one group of the enemy spaceships is positioned in such a way that they all have integer $y$-coordinates, and their $x$-coordinate is equal to $-100$, while the second group is positioned in such a way that they all have integer $y$-coordinates, and their $x$-coordinate is equal to $100$. Each spaceship in both groups will simultaneously shoot two laser shots (infinite ray that destroys any spaceship it touches), one towards each of the small spaceships, all at the same time. The small spaceships will be able to avoid all the laser shots, and now want to position themselves at some locations with $x=0$ (with not necessarily integer $y$-coordinates), such that the rays shot at them would destroy as many of the enemy spaceships as possible. Find the largest numbers of spaceships that can be destroyed this way, assuming that the enemy spaceships can't avoid laser shots. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 60$), the number of enemy spaceships with $x = -100$ and the number of enemy spaceships with $x = 100$, respectively. The second line contains $n$ integers $y_{1,1}, y_{1,2}, \ldots, y_{1,n}$ ($|y_{1,i}| \le 10\,000$) — the $y$-coordinates of the spaceships in the first group. The third line contains $m$ integers $y_{2,1}, y_{2,2}, \ldots, y_{2,m}$ ($|y_{2,i}| \le 10\,000$) — the $y$-coordinates of the spaceships in the second group. The $y$ coordinates are not guaranteed to be unique, even within a group. -----Output----- Print a single integer – the largest number of enemy spaceships that can be destroyed. -----Examples----- Input 3 9 1 2 3 1 2 3 7 8 9 11 12 13 Output 9 Input 5 5 1 2 3 4 5 1 2 3 4 5 Output 10 -----Note----- In the first example the first spaceship can be positioned at $(0, 2)$, and the second – at $(0, 7)$. This way all the enemy spaceships in the first group and $6$ out of $9$ spaceships in the second group will be destroyed. In the second example the first spaceship can be positioned at $(0, 3)$, and the second can be positioned anywhere, it will be sufficient to destroy all the enemy spaceships. 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 Counter MV = 400020 a = [0] * MV for i in range(MV): a[i] = set() n ,m = list(map(int , input().split())) first = list(map(int , input().split())) second = list(map(int , input().split())) for fid, f in enumerate(first): for sid, s in enumerate(second): a[f+s].add(fid + MV) a[f+s].add(sid) a.sort(key = lambda x: -len(x)) b = [len(k) for k in a] # for k in range(MV): # if b[k]>0: # print(k, b[k], a[k]) best_res = b[0] for pos in range(MV): for pos2 in range(MV): if b[pos] + b [pos2] <= best_res: break cur = len(a[pos].union(a[pos2])) if cur > best_res : best_res = cur print(best_res) ```
vfc_23584
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/993/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 9\n1 2 3\n1 2 3 7 8 9 11 12 13\n", "output": "9\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 5\n1 2 3 4 5\n1 2 3 4 5\n", "output": "10\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "50 50\n744 333 562 657 680 467 357 376 759 311 371 327 369 172 286 577 446 922 16 69 350 92 627 852 878 733 148 857 663 969 131 250 563 665 67 169 178 625 975 457 414 434 146 602 235 86 240 756 161 675\n222 371 393 634 76 268 348 294 227 429 835 534 756 67 174 704 685 462 829 561 249 148 868 512 118 232 33 450 445 420 397 129 122 74 426 441 989 892 662 727 492 702 352 818 399 968 894 297 342 405\n", "output": "29\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "60 60\n842 229 415 973 606 880 422 808 121 317 41 358 725 32 395 286 819 550 410 516 81 599 623 275 568 102 778 234 385 445 194 89 105 643 220 165 872 858 420 653 843 465 696 723 594 8 127 273 289 345 260 553 231 940 912 687 205 272 14 706\n855 361 529 341 602 225 922 807 775 149 212 789 547 766 813 624 236 583 207 586 516 21 621 839 259 774 419 286 537 284 685 944 223 189 358 232 495 688 877 920 400 105 968 919 543 700 538 466 739 33 729 292 891 797 707 174 799 427 321 953\n", "output": "40\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 5\n1\n1 2 3 4 5\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 1\n1 2 3 4 5\n1\n", "output": "3\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3720
Solve the following coding problem using the programming language python: Vasya and Petya wrote down all integers from 1 to n to play the "powers" game (n can be quite large; however, Vasya and Petya are not confused by this fact). Players choose numbers in turn (Vasya chooses first). If some number x is chosen at the current turn, it is forbidden to choose x or all of its other positive integer powers (that is, x^2, x^3, ...) at the next turns. For instance, if the number 9 is chosen at the first turn, one cannot choose 9 or 81 later, while it is still allowed to choose 3 or 27. The one who cannot make a move loses. Who wins if both Vasya and Petya play optimally? -----Input----- Input contains single integer n (1 ≤ n ≤ 10^9). -----Output----- Print the name of the winner — "Vasya" or "Petya" (without quotes). -----Examples----- Input 1 Output Vasya Input 2 Output Petya Input 8 Output Petya -----Note----- In the first sample Vasya will choose 1 and win immediately. In the second sample no matter which number Vasya chooses during his first turn, Petya can choose the remaining number and win. 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 import math, collections mod = 10**9+7 def isPower(n): if (n <= 1): return True for x in range(2, (int)(math.sqrt(n)) + 1): p = x while (p <= n): p = p * x if (p == n): return True return False n = int(input()) arr = [0,1,2,1,4,3,2,1,5,6,2,1,8,7,5,9,8,7,3,4,7,4,2,1,10,9,3,6,11,12] ans = arr[int(math.log(n, 2))] s = int(math.log(n, 2)) for i in range(3, int(n**0.5)+1): if not isPower(i): ans^=arr[int(math.log(n, i))] s+=int(math.log(n, i)) ans^=((n-s)%2) print("Vasya" if ans else "Petya") ```
vfc_23588
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/317/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1\n", "output": "Vasya\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n", "output": "Petya\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3721
Solve the following coding problem using the programming language python: Innopolis University scientists continue to investigate the periodic table. There are n·m known elements and they form a periodic table: a rectangle with n rows and m columns. Each element can be described by its coordinates (r, c) (1 ≤ r ≤ n, 1 ≤ c ≤ m) in the table. Recently scientists discovered that for every four different elements in this table that form a rectangle with sides parallel to the sides of the table, if they have samples of three of the four elements, they can produce a sample of the fourth element using nuclear fusion. So if we have elements in positions (r_1, c_1), (r_1, c_2), (r_2, c_1), where r_1 ≠ r_2 and c_1 ≠ c_2, then we can produce element (r_2, c_2). [Image] Samples used in fusion are not wasted and can be used again in future fusions. Newly crafted elements also can be used in future fusions. Innopolis University scientists already have samples of q elements. They want to obtain samples of all n·m elements. To achieve that, they will purchase some samples from other laboratories and then produce all remaining elements using an arbitrary number of nuclear fusions in some order. Help them to find the minimal number of elements they need to purchase. -----Input----- The first line contains three integers n, m, q (1 ≤ n, m ≤ 200 000; 0 ≤ q ≤ min(n·m, 200 000)), the chemical table dimensions and the number of elements scientists already have. The following q lines contain two integers r_{i}, c_{i} (1 ≤ r_{i} ≤ n, 1 ≤ c_{i} ≤ m), each describes an element that scientists already have. All elements in the input are different. -----Output----- Print the minimal number of elements to be purchased. -----Examples----- Input 2 2 3 1 2 2 2 2 1 Output 0 Input 1 5 3 1 3 1 1 1 5 Output 2 Input 4 3 6 1 2 1 3 2 2 2 3 3 1 3 3 Output 1 -----Note----- For each example you have a picture which illustrates it. The first picture for each example describes the initial set of element samples available. Black crosses represent elements available in the lab initially. The second picture describes how remaining samples can be obtained. Red dashed circles denote elements that should be purchased from other labs (the optimal solution should minimize the number of red circles). Blue dashed circles are elements that can be produced with nuclear fusion. They are numbered in order in which they can be produced. Test 1 We can use nuclear fusion and get the element from three other samples, so we don't need to purchase anything. [Image] Test 2 We cannot use any nuclear fusion at all as there is only one row, so we have to purchase all missing elements. [Image] Test 3 There are several possible solutions. One of them is illustrated below. Note that after purchasing one element marked as red it's still not possible to immidiately produce the middle element in the bottom row (marked as 4). So we produce the element in the left-top corner first (marked as 1), and then use it in future fusions. [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 n,m,q=map(int,input().split()) p=[-1]*(n+m) r=[0]*(n+m) def par(i): if p[i]==-1: return i p[i]=par(p[i]) return p[i] def merge(a,b): a,b=par(a),par(b) if a==b: return 0 if r[a]<r[b]:p[a]=b elif r[b]<r[a]:p[b]=a else:p[a]=b;r[b]+=1 return 1 v=n+m for l in sys.stdin.read().strip().split('\n') if q else []: a,b=map(int,l.split()) v-=merge(a-1,b-1+n) print(v-1) ```
vfc_23592
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1012/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2 2 3\n1 2\n2 2\n2 1\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3723
Solve the following coding problem using the programming language python: Bash has set out on a journey to become the greatest Pokemon master. To get his first Pokemon, he went to Professor Zulu's Lab. Since Bash is Professor Zulu's favourite student, Zulu allows him to take as many Pokemon from his lab as he pleases. But Zulu warns him that a group of k > 1 Pokemon with strengths {s_1, s_2, s_3, ..., s_{k}} tend to fight among each other if gcd(s_1, s_2, s_3, ..., s_{k}) = 1 (see notes for gcd definition). Bash, being smart, does not want his Pokemon to fight among each other. However, he also wants to maximize the number of Pokemon he takes from the lab. Can you help Bash find out the maximum number of Pokemon he can take? Note: A Pokemon cannot fight with itself. -----Input----- The input consists of two lines. The first line contains an integer n (1 ≤ n ≤ 10^5), the number of Pokemon in the lab. The next line contains n space separated integers, where the i-th of them denotes s_{i} (1 ≤ s_{i} ≤ 10^5), the strength of the i-th Pokemon. -----Output----- Print single integer — the maximum number of Pokemons Bash can take. -----Examples----- Input 3 2 3 4 Output 2 Input 5 2 3 4 6 7 Output 3 -----Note----- gcd (greatest common divisor) of positive integers set {a_1, a_2, ..., a_{n}} is the maximum positive integer that divides all the integers {a_1, a_2, ..., a_{n}}. In the first sample, we can take Pokemons with strengths {2, 4} since gcd(2, 4) = 2. In the second sample, we can take Pokemons with strengths {2, 4, 6}, and there is no larger group with gcd ≠ 1. 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 Counter n = int(input()) a = [int(_) for _ in input().split()] f = Counter(a) N = 10 ** 5 + 10 p = [0 for i in range(N)] ans = Counter() for i in range(2, N): if p[i]: continue for j in range(i, N, i): p[j] = 1 ans[i] += f[j] print(max(1, ans.most_common(1)[0][1])) ```
vfc_23600
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/757/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n2 3 4\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n2 3 4 6 7\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n5 6 4\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "8\n41 74 4 27 85 39 100 36\n", "output": "4\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3725
Solve the following coding problem using the programming language python: Mike has a frog and a flower. His frog is named Xaniar and his flower is named Abol. Initially(at time 0), height of Xaniar is h_1 and height of Abol is h_2. Each second, Mike waters Abol and Xaniar. [Image] So, if height of Xaniar is h_1 and height of Abol is h_2, after one second height of Xaniar will become $(x_{1} h_{1} + y_{1}) \operatorname{mod} m$ and height of Abol will become $(x_{2} h_{2} + y_{2}) \operatorname{mod} m$ where x_1, y_1, x_2 and y_2 are some integer numbers and $a \operatorname{mod} b$ denotes the remainder of a modulo b. Mike is a competitive programmer fan. He wants to know the minimum time it takes until height of Xania is a_1 and height of Abol is a_2. Mike has asked you for your help. Calculate the minimum time or say it will never happen. -----Input----- The first line of input contains integer m (2 ≤ m ≤ 10^6). The second line of input contains integers h_1 and a_1 (0 ≤ h_1, a_1 < m). The third line of input contains integers x_1 and y_1 (0 ≤ x_1, y_1 < m). The fourth line of input contains integers h_2 and a_2 (0 ≤ h_2, a_2 < m). The fifth line of input contains integers x_2 and y_2 (0 ≤ x_2, y_2 < m). It is guaranteed that h_1 ≠ a_1 and h_2 ≠ a_2. -----Output----- Print the minimum number of seconds until Xaniar reaches height a_1 and Abol reaches height a_2 or print -1 otherwise. -----Examples----- Input 5 4 2 1 1 0 1 2 3 Output 3 Input 1023 1 2 1 0 1 2 1 1 Output -1 -----Note----- In the first sample, heights sequences are following: Xaniar: $4 \rightarrow 0 \rightarrow 1 \rightarrow 2$ Abol: $0 \rightarrow 3 \rightarrow 4 \rightarrow 1$ The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import fractions def read_data(): m = int(input()) h1, a1 = map(int, input().split()) x1, y1 = map(int, input().split()) h2, a2 = map(int, input().split()) x2, y2 = map(int, input().split()) return m, h1, a1, x1, y1, h2, a2, x2, y2 def solve(m, h1, a1, x1, y1, h2, a2, x2, y2): t = 0 h1s = [-1] * m h2s = [-1] * m h1s[h1] = 0 h2s[h2] = 0 t1 = -1 t2 = -1 while h1 != a1 or h2 != a2: t += 1 h1 = (x1 * h1 + y1) % m h2 = (x2 * h2 + y2) % m if h1s[h1] >= 0 and t1 == -1: t1 = h1s[h1] s1 = t - t1 if t2 >= 0: break else: h1s[h1] = t if h2s[h2] >= 0 and t2 == -1: t2 = h2s[h2] s2 = t - t2 if t1 >= 0: break else: h2s[h2] = t if h1 == a1 and h2 == a2: return t return retrieve(a1, a2, t1, s1, t2, s2, h1s, h2s) def retrieve(a1, a2, t1, s1, t2, s2, h1s, h2s): u1 = h1s[a1] u2 = h2s[a2] if u1 == -1 or u2 == -1: return -1 if u1 < t1: if guess(h2s, u1, t2, s2, a2): return u1 else: return -1 if u2 < t2: if guess(h1s, u2, t1, s1, a1): return u2 else: return -1 return find_time(u1, s1, u2, s2) def guess(hs, u, t, s, a): if u <= t: return hs[a] == u tt = t + (u - t) % s return hs[a] == tt def find_time(u1, s1, u2, s2): g = fractions.gcd(s1, s2) if abs(u1 - u2) % g: return -1 k1, k2 = extended_euclid(s1, s2, u2-u1, g) b = s2 // g return (k1 % b) * s1 + u1 def egcd(a, b): x, lastx = 0, 1 y, lasty = 1, 0 while b: q = a // b a, b = b, a % b x, lastx = lastx - q * x, x y, lasty = lasty - q * y, y return lastx, lasty def extended_euclid(a, b, c, g): x, y = egcd(a, b) return (c // g) * x, (x // g) * y param = read_data() print(solve(*param)) ```
vfc_23608
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/547/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n4 2\n1 1\n0 1\n2 3\n", "output": "3\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3727
Solve the following coding problem using the programming language python: An integer sequence is called beautiful if the difference between any two consecutive numbers is equal to $1$. More formally, a sequence $s_1, s_2, \ldots, s_{n}$ is beautiful if $|s_i - s_{i+1}| = 1$ for all $1 \leq i \leq n - 1$. Trans has $a$ numbers $0$, $b$ numbers $1$, $c$ numbers $2$ and $d$ numbers $3$. He wants to construct a beautiful sequence using all of these $a + b + c + d$ numbers. However, it turns out to be a non-trivial task, and Trans was not able to do it. Could you please help Trans? -----Input----- The only input line contains four non-negative integers $a$, $b$, $c$ and $d$ ($0 < a+b+c+d \leq 10^5$). -----Output----- If it is impossible to construct a beautiful sequence satisfying the above constraints, print "NO" (without quotes) in one line. Otherwise, print "YES" (without quotes) in the first line. Then in the second line print $a + b + c + d$ integers, separated by spaces — a beautiful sequence. There should be $a$ numbers equal to $0$, $b$ numbers equal to $1$, $c$ numbers equal to $2$ and $d$ numbers equal to $3$. If there are multiple answers, you can print any of them. -----Examples----- Input 2 2 2 1 Output YES 0 1 0 1 2 3 2 Input 1 2 3 4 Output NO Input 2 2 2 3 Output NO -----Note----- In the first test, it is easy to see, that the sequence is beautiful because the difference between any two consecutive numbers is equal to $1$. Also, there are exactly two numbers, equal to $0$, $1$, $2$ and exactly one number, equal to $3$. It can be proved, that it is impossible to construct beautiful sequences in the second and third tests. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def solve(a, e): if not a[e]: return False, [] a = list(a[::]) ans = [e] a[e] -= 1 for i in range(sum(a)): if ans[-1] - 1 >= 0 and a[ans[-1] - 1] > 0: v = ans[-1] - 1 ans.append(v) a[v] -= 1 elif ans[-1] + 1 <= 3 and a[ans[-1] + 1] > 0: v = ans[-1] + 1 ans.append(v) a[v] -= 1 else: return False, [] return True, ans def main(): a = list(map(int, input().split())) for i in range(4): r, b = solve(a, i) if r: print('YES') print(*b) return print('NO') main() ```
vfc_23616
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1264/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2 2 2 1\n", "output": "YES\n0 1 0 1 2 3 2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 2 3 4\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 2 2 3\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3436 3436 46563 46564\n", "output": "NO\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3728
Solve the following coding problem using the programming language python: You are given a table consisting of n rows and m columns. Numbers in each row form a permutation of integers from 1 to m. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. -----Input----- The first line of the input contains two integers n and m (1 ≤ n, m ≤ 20) — the number of rows and the number of columns in the given table. Each of next n lines contains m integers — elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m. -----Output----- If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). -----Examples----- Input 2 4 1 3 2 4 1 3 4 2 Output YES Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Output NO Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Output YES -----Note----- In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1 2 3 4 1 4 3 2 In the second row, swap the second and the fourth elements. Now the table is 1 2 3 4 1 2 3 4 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys n, m = list(map(int,input().split())) g = [list(map(int,input().split())) for _ in range(n)] for c1 in range(m): for c2 in range(c1, m): ok = True for row in g: row[c1], row[c2] = row[c2], row[c1] cnt = 0 for i in range(m): if row[i] != i + 1: cnt += 1 if cnt > 2: break row[c1], row[c2] = row[c2], row[c1] if cnt > 2: ok = False break if ok: print('YES') return print('NO') ```
vfc_23620
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/724/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2 4\n1 3 2 4\n1 3 4 2\n", "output": "YES\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3729
Solve the following coding problem using the programming language python: Tarly has two different type of items, food boxes and wine barrels. There are f food boxes and w wine barrels. Tarly stores them in various stacks and each stack can consist of either food boxes or wine barrels but not both. The stacks are placed in a line such that no two stacks of food boxes are together and no two stacks of wine barrels are together. The height of a stack is defined as the number of items in the stack. Two stacks are considered different if either their heights are different or one of them contains food and other contains wine. Jon Snow doesn't like an arrangement if any stack of wine barrels has height less than or equal to h. What is the probability that Jon Snow will like the arrangement if all arrangement are equiprobably? Two arrangement of stacks are considered different if exists such i, that i-th stack of one arrangement is different from the i-th stack of the other arrangement. -----Input----- The first line of input contains three integers f, w, h (0 ≤ f, w, h ≤ 10^5) — number of food boxes, number of wine barrels and h is as described above. It is guaranteed that he has at least one food box or at least one wine barrel. -----Output----- Output the probability that Jon Snow will like the arrangement. The probability is of the form [Image], then you need to output a single integer p·q^{ - 1} mod (10^9 + 7). -----Examples----- Input 1 1 1 Output 0 Input 1 2 1 Output 666666672 -----Note----- In the first example f = 1, w = 1 and h = 1, there are only two possible arrangement of stacks and Jon Snow doesn't like any of them. In the second example f = 1, w = 2 and h = 1, there are three arrangements. Jon Snow likes the (1) and (3) arrangement. So the probabilty is $\frac{2}{3}$. [Image] The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def build_fac(): nonlocal mod fac = [1] * int(3e5 + 1) for i in range(1, int(3e5)): fac[i] = i*fac[i-1] % mod return fac def inv(x): nonlocal mod return pow(x, mod-2, mod) def ncr(n, r): nonlocal fac if n < 0 or n < r: return 0 return fac[n]*inv(fac[r])*inv(fac[n-r]) % mod def cf(f, w, h): nonlocal mod if w == 0: return 1 rs = 0 for k in range(1, min(w//(h+1),f+1)+1): rs += ncr(f+1, k) * ncr(w-k*h-1, k-1) % mod rs %= mod return rs f, w, h = map(int,input().split(' ')) mod = int(1e9 + 7) fac = build_fac() cnt = cf(f, w, h) rs = cnt*inv(ncr(f+w, w)) % mod print(rs) ```
vfc_23624
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/768/F", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1 1 1\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 2 1\n", "output": "666666672\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6 5 7\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "12 12 24\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "20 12 32\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "21 32 10\n", "output": "683999264\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3730
Solve the following coding problem using the programming language python: DZY has a sequence a, consisting of n integers. We'll call a sequence a_{i}, a_{i} + 1, ..., a_{j} (1 ≤ i ≤ j ≤ n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment. Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing. You only need to output the length of the subsegment you find. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5). The next line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9). -----Output----- In a single line print the answer to the problem — the maximum length of the required subsegment. -----Examples----- Input 6 7 2 3 1 5 6 Output 5 -----Note----- You can choose subsegment a_2, a_3, a_4, a_5, a_6 and change its 3rd element (that is a_4) to 4. 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()) vec=list(map(int,input().split())) al=[] otrez=[] for x in vec: if len(al)==0: al.append(x) elif(x<=al[-1]): otrez.append(al) al=[x] else: al.append(x) otrez.append(al) maxx=0 if(len(otrez)!=1): for i in range(0,len(otrez)-1): if(len(otrez[i])==1)or(len(otrez[i+1])==1): maxot=len(otrez[i])+len(otrez[i+1]) elif (otrez[i+1][0]-otrez[i][-2])>1 or (otrez[i+1][1]-otrez[i][-1])>1: maxot=len(otrez[i])+len(otrez[i+1]) else: maxot=max([len(otrez[i]),len(otrez[i+1])])+1 if(maxot>maxx): maxx=maxot print(maxx) else: print(len(otrez[0])) ```
vfc_23628
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/446/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6\n7 2 3 1 5 6\n", "output": "5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n424238336 649760493 681692778 714636916 719885387 804289384 846930887 957747794 596516650 189641422\n", "output": "9\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "50\n804289384 846930887 681692778 714636916 957747794 424238336 719885387 649760493 596516650 189641422 25202363 350490028 783368691 102520060 44897764 967513927 365180541 540383427 304089173 303455737 35005212 521595369 294702568 726956430 336465783 861021531 59961394 89018457 101513930 125898168 131176230 145174068 233665124 278722863 315634023 369133070 468703136 628175012 635723059 653377374 656478043 801979803 859484422 914544920 608413785 756898538 734575199 973594325 149798316 38664371\n", "output": "19\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n1\n", "output": "1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3731
Solve the following coding problem using the programming language python: Sometimes Mister B has free evenings when he doesn't know what to do. Fortunately, Mister B found a new game, where the player can play against aliens. All characters in this game are lowercase English letters. There are two players: Mister B and his competitor. Initially the players have a string s consisting of the first a English letters in alphabetical order (for example, if a = 5, then s equals to "abcde"). The players take turns appending letters to string s. Mister B moves first. Mister B must append exactly b letters on each his move. He can arbitrary choose these letters. His opponent adds exactly a letters on each move. Mister B quickly understood that his opponent was just a computer that used a simple algorithm. The computer on each turn considers the suffix of string s of length a and generates a string t of length a such that all letters in the string t are distinct and don't appear in the considered suffix. From multiple variants of t lexicographically minimal is chosen (if a = 4 and the suffix is "bfdd", the computer chooses string t equal to "aceg"). After that the chosen string t is appended to the end of s. Mister B soon found the game boring and came up with the following question: what can be the minimum possible number of different letters in string s on the segment between positions l and r, inclusive. Letters of string s are numerated starting from 1. -----Input----- First and only line contains four space-separated integers: a, b, l and r (1 ≤ a, b ≤ 12, 1 ≤ l ≤ r ≤ 10^9) — the numbers of letters each player appends and the bounds of the segment. -----Output----- Print one integer — the minimum possible number of different letters in the segment from position l to position r, inclusive, in string s. -----Examples----- Input 1 1 1 8 Output 2 Input 4 2 2 6 Output 3 Input 3 7 4 6 Output 1 -----Note----- In the first sample test one of optimal strategies generate string s = "abababab...", that's why answer is 2. In the second sample test string s = "abcdbcaefg..." can be obtained, chosen segment will look like "bcdbc", that's why answer is 3. In the third sample test string s = "abczzzacad..." can be obtained, chosen, segment will look like "zzz", that's why answer is 1. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python a,b,l,r=list(map(int, input().split())) length=int(l/(a+b)) if a==3 and b==1 and l==4 and r==10: print(4) return l-=length*(a+b) r-=length*(a+b) if r>=4*a+4*b: r=4*a+4*b if b>=a: _A=[] for i in range(a): _A.append(i+1) for i in range(b): _A.append(a) for i in range(a): _A.append(i+1) _A[2*a+b-1]+=1 for i in range(b): _A.append(_A[2*a+b-1]) for i in range(2*a+2*b): _A.append(_A[i]) _B=[] for i in range(25): _B.append(0) cnt=0 for i in range(r-l+1): if _B[_A[l+i-1]]==0: cnt+=1 _B[_A[l+i-1]]=1 else: _A=[] for i in range(a): _A.append(i+1) for i in range(b): _A.append(a) for i in range(a): if i+1<=b: _A.append(i+1) else: _A.append(a+i-b+2) for i in range(b): _A.append(_A[2*a+b-1]) for i in range(2*a+2*b): _A.append(_A[i]) _B=[] for i in range(25): _B.append(0) cnt=0 for i in range(r-l+1): if _B[_A[l+i-1]]==0: cnt+=1 _B[_A[l+i-1]]=1 # print(_A) print(cnt) ```
vfc_23632
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/819/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1 1 1 8\n", "output": "2", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 2 2 6\n", "output": "3", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 7 4 6\n", "output": "1", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 5 1 1\n", "output": "1", "type": "stdin_stdout" } ] }
apps
verifiable_code
3732
Solve the following coding problem using the programming language python: Let us call a pair of integer numbers m-perfect, if at least one number in the pair is greater than or equal to m. Thus, the pairs (3, 3) and (0, 2) are 2-perfect while the pair (-1, 1) is not. Two integers x, y are written on the blackboard. It is allowed to erase one of them and replace it with the sum of the numbers, (x + y). What is the minimum number of such operations one has to perform in order to make the given pair of integers m-perfect? -----Input----- Single line of the input contains three integers x, y and m ( - 10^18 ≤ x, y, m ≤ 10^18). Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preffered to use the cin, cout streams or the %I64d specifier. -----Output----- Print the minimum number of operations or "-1" (without quotes), if it is impossible to transform the given pair to the m-perfect one. -----Examples----- Input 1 2 5 Output 2 Input -1 4 15 Output 4 Input 0 -1 5 Output -1 -----Note----- In the first sample the following sequence of operations is suitable: (1, 2) $\rightarrow$ (3, 2) $\rightarrow$ (5, 2). In the second sample: (-1, 4) $\rightarrow$ (3, 4) $\rightarrow$ (7, 4) $\rightarrow$ (11, 4) $\rightarrow$ (15, 4). Finally, in the third sample x, y cannot be made positive, hence there is no proper sequence of operations. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python k = 0 x, y, m = map(int, input().split()) if (y < x): x, y = y, x if (y >= m): print(0) else: if (x <= 0) and (y <= 0): print(-1) else: if (x <= 0) and (y > 0): if (abs(x) % y > 0): k += abs(x) // y + 1 else: k += abs(x) // y x = x + y * k a = 0 b = 1 c = 0 while (c < 5000000000000000000): if (a * x + b * y >= m): print(k) break c = a + b a = b b = c k += 1 if (c >= 5000000000000000000): print(-1) ```
vfc_23636
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/317/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1 2 5\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "-1 4 15\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "0 -1 5\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "0 1 8\n", "output": "5\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3733
Solve the following coding problem using the programming language python: One common way of digitalizing sound is to record sound intensity at particular time moments. For each time moment intensity is recorded as a non-negative integer. Thus we can represent a sound file as an array of $n$ non-negative integers. If there are exactly $K$ distinct values in the array, then we need $k = \lceil \log_{2} K \rceil$ bits to store each value. It then takes $nk$ bits to store the whole file. To reduce the memory consumption we need to apply some compression. One common way is to reduce the number of possible intensity values. We choose two integers $l \le r$, and after that all intensity values are changed in the following way: if the intensity value is within the range $[l;r]$, we don't change it. If it is less than $l$, we change it to $l$; if it is greater than $r$, we change it to $r$. You can see that we lose some low and some high intensities. Your task is to apply this compression in such a way that the file fits onto a disk of size $I$ bytes, and the number of changed elements in the array is minimal possible. We remind you that $1$ byte contains $8$ bits. $k = \lceil log_{2} K \rceil$ is the smallest integer such that $K \le 2^{k}$. In particular, if $K = 1$, then $k = 0$. -----Input----- The first line contains two integers $n$ and $I$ ($1 \le n \le 4 \cdot 10^{5}$, $1 \le I \le 10^{8}$) — the length of the array and the size of the disk in bytes, respectively. The next line contains $n$ integers $a_{i}$ ($0 \le a_{i} \le 10^{9}$) — the array denoting the sound file. -----Output----- Print a single integer — the minimal possible number of changed elements. -----Examples----- Input 6 1 2 1 2 3 4 3 Output 2 Input 6 2 2 1 2 3 4 3 Output 0 Input 6 1 1 1 2 2 3 3 Output 2 -----Note----- In the first example we can choose $l=2, r=3$. The array becomes 2 2 2 3 3 3, the number of distinct elements is $K=2$, and the sound file fits onto the disk. Only two values are changed. In the second example the disk is larger, so the initial file fits it and no changes are required. In the third example we have to change both 1s or both 3s. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python N, I = list(map(int, input().split())) A = sorted([int(a) for a in input().split()]) B = [] j = 0 for i in range(N): if i == 0 or A[i] == A[i-1]: B.append(j) else: j += 1 B.append(j) def calc(k): K = 1<<k i = 0 j = 0 ma = 0 while j < N: if B[j]-B[i] <= K - 1: ma = max(ma, j-i+1) j += 1 else: i += 1 return N-ma ans = 10**100 for i in range(31): if i * N <= 8 * I: ans = min(ans, calc(i)) print(ans) ```
vfc_23640
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1198/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6 1\n2 1 2 3 4 3\n", "output": "2\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3734
Solve the following coding problem using the programming language python: You are given names of two days of the week. Please, determine whether it is possible that during some non-leap year the first day of some month was equal to the first day of the week you are given, while the first day of the next month was equal to the second day of the week you are given. Both months should belong to one year. In this problem, we consider the Gregorian calendar to be used. The number of months in this calendar is equal to 12. The number of days in months during any non-leap year is: 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31. Names of the days of the week are given with lowercase English letters: "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday". -----Input----- The input consists of two lines, each of them containing the name of exactly one day of the week. It's guaranteed that each string in the input is from the set "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday". -----Output----- Print "YES" (without quotes) if such situation is possible during some non-leap year. Otherwise, print "NO" (without quotes). -----Examples----- Input monday tuesday Output NO Input sunday sunday Output YES Input saturday tuesday Output YES -----Note----- In the second sample, one can consider February 1 and March 1 of year 2015. Both these days were Sundays. In the third sample, one can consider July 1 and August 1 of year 2017. First of these two days is Saturday, while the second one is Tuesday. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python a, b = input(), input() d = {'monday': 1, 'tuesday': 2, 'wednesday': 3, 'thursday': 4, 'friday': 5, 'saturday': 6, 'sunday': 7} x, y = d[a], d[b] flag = (y - x) % 7 in {0, 2, 3} print('YES' if flag else 'NO') ```
vfc_23644
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/724/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "monday\ntuesday\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "sunday\nsunday\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "saturday\ntuesday\n", "output": "YES\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3735
Solve the following coding problem using the programming language python: You are given a positive integer $n$. Let $S(x)$ be sum of digits in base 10 representation of $x$, for example, $S(123) = 1 + 2 + 3 = 6$, $S(0) = 0$. Your task is to find two integers $a, b$, such that $0 \leq a, b \leq n$, $a + b = n$ and $S(a) + S(b)$ is the largest possible among all such pairs. -----Input----- The only line of input contains an integer $n$ $(1 \leq n \leq 10^{12})$. -----Output----- Print largest $S(a) + S(b)$ among all pairs of integers $a, b$, such that $0 \leq a, b \leq n$ and $a + b = n$. -----Examples----- Input 35 Output 17 Input 10000000000 Output 91 -----Note----- In the first example, you can choose, for example, $a = 17$ and $b = 18$, so that $S(17) + S(18) = 1 + 7 + 1 + 8 = 17$. It can be shown that it is impossible to get a larger answer. In the second test example, you can choose, for example, $a = 5000000001$ and $b = 4999999999$, with $S(5000000001) + S(4999999999) = 91$. It can be shown that it is impossible to get a larger answer. 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()) a = int(str(n)[0]) l = len(str(n)) - 1 val1 = a * 10 ** l - 1 val2 = n - val1 def sd(x): return sum(int(d) for d in str(x)) print(sd(val1) + sd(val2)) ```
vfc_23648
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1060/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "35\n", "output": "17\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10000000000\n", "output": "91\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4394826\n", "output": "90\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "96\n", "output": "24\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "9999991\n", "output": "109\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3736
Solve the following coding problem using the programming language python: Recently, a start up by two students of a state university of city F gained incredible popularity. Now it's time to start a new company. But what do we call it? The market analysts came up with a very smart plan: the name of the company should be identical to its reflection in a mirror! In other words, if we write out the name of the company on a piece of paper in a line (horizontally, from left to right) with large English letters, then put this piece of paper in front of the mirror, then the reflection of the name in the mirror should perfectly match the line written on the piece of paper. There are many suggestions for the company name, so coming up to the mirror with a piece of paper for each name wouldn't be sensible. The founders of the company decided to automatize this process. They asked you to write a program that can, given a word, determine whether the word is a 'mirror' word or not. -----Input----- The first line contains a non-empty name that needs to be checked. The name contains at most 10^5 large English letters. The name will be written with the next sans serif font: $\text{ABCDEFGHI JKLMNOPQRSTUVWXYZ}$ -----Output----- Print 'YES' (without the quotes), if the given name matches its mirror reflection. Otherwise, print 'NO' (without the quotes). -----Examples----- Input AHA Output YES Input Z Output NO Input XO Output NO The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python s=input() M={"A","H","I","M","O","T","U","V","W","X","Y"} ans="YES" n=len(s) for i in range(len(s)//2): x=s[i] y=s[n-i-1] if(x!=y or x not in M): ans="NO" break if(n%2==1): if(s[n//2] not in M): ans="NO" print(ans) ```
vfc_23652
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/420/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "AHA\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "Z\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "XO\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "AAA\n", "output": "YES\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3737
Solve the following coding problem using the programming language python: "Night gathers, and now my watch begins. It shall not end until my death. I shall take no wife, hold no lands, father no children. I shall wear no crowns and win no glory. I shall live and die at my post. I am the sword in the darkness. I am the watcher on the walls. I am the shield that guards the realms of men. I pledge my life and honor to the Night's Watch, for this night and all the nights to come." — The Night's Watch oath. With that begins the watch of Jon Snow. He is assigned the task to support the stewards. This time he has n stewards with him whom he has to provide support. Each steward has his own strength. Jon Snow likes to support a steward only if there exists at least one steward who has strength strictly less than him and at least one steward who has strength strictly greater than him. Can you find how many stewards will Jon support? -----Input----- First line consists of a single integer n (1 ≤ n ≤ 10^5) — the number of stewards with Jon Snow. Second line consists of n space separated integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9) representing the values assigned to the stewards. -----Output----- Output a single integer representing the number of stewards which Jon will feed. -----Examples----- Input 2 1 5 Output 0 Input 3 1 2 5 Output 1 -----Note----- In the first sample, Jon Snow cannot support steward with strength 1 because there is no steward with strength less than 1 and he cannot support steward with strength 5 because there is no steward with strength greater than 5. In the second sample, Jon Snow can support steward with strength 2 because there are stewards with strength less than 2 and greater than 2. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python x = int(input()) l = list(map(int, input().split(' '))) if min(l) == max(l): print(0) else: print(x-l.count(min(l))-l.count(max(l))) ```
vfc_23656
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/768/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\n1 5\n", "output": "0", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n1 2 5\n", "output": "1", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n1 2 3 4\n", "output": "2", "type": "stdin_stdout" }, { "fn_name": null, "input": "8\n7 8 9 4 5 6 1 2\n", "output": "6", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n1\n", "output": "0", "type": "stdin_stdout" } ] }
apps
verifiable_code
3741
Solve the following coding problem using the programming language python: You are given $n$ integer numbers $a_1, a_2, \dots, a_n$. Consider graph on $n$ nodes, in which nodes $i$, $j$ ($i\neq j$) are connected if and only if, $a_i$ AND $a_j\neq 0$, where AND denotes the bitwise AND operation. Find the length of the shortest cycle in this graph or determine that it doesn't have cycles at all. -----Input----- The first line contains one integer $n$ $(1 \le n \le 10^5)$ — number of numbers. The second line contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^{18}$). -----Output----- If the graph doesn't have any cycles, output $-1$. Else output the length of the shortest cycle. -----Examples----- Input 4 3 6 28 9 Output 4 Input 5 5 12 9 16 48 Output 3 Input 4 1 2 4 8 Output -1 -----Note----- In the first example, the shortest cycle is $(9, 3, 6, 28)$. In the second example, the shortest cycle is $(5, 12, 9)$. The graph has no cycles in the third example. 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 bits = [0] * 64 n = int(input()) data = list([x for x in map(int, input().split()) if x != 0]) n = len(data) if n == 0: print(-1) return for v in data: i = 0 while v != 0: bits[i] += v & 1 i += 1 v >>= 1 for i in range(64): if bits[i] > 2: print(3) return graph = [[] for _ in range(n)] for u in range(n): for v in range(u): if (data[u] & data[v]) != 0 and u != v: graph[v].append(u) graph[u].append(v) def bfs(start): group = [-1] * n depth = [0] + [-1] * (n - 1) for j in range(len(graph[start])): to = graph[start][j] group[to] = j depth[to] = 1 bfsQ = deque(graph[start]) minlen = 999999999 while len(bfsQ) > 0: u = bfsQ[0] bfsQ.popleft() for v in graph[u]: if v == start: if depth[u] < 2: continue return depth[u] + 1 if group[v] == -1: group[v] = group[u] depth[v] = depth[u] + 1 bfsQ.append(v) elif group[v] != group[u]: newlen = depth[u] + depth[v] + 1 if newlen < minlen: minlen = newlen return minlen answer = min(list(map(bfs, list(range(n))))) print(answer if answer <= n else -1) ```
vfc_23671
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1205/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n3 6 28 9\n", "output": "4", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n5 12 9 16 48\n", "output": "3", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n1 2 4 8\n", "output": "-1", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n1000000000000000000\n", "output": "-1", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n70369817919488 281474976710657 70368744177665 281476050452480\n", "output": "4", "type": "stdin_stdout" } ] }
apps
verifiable_code
3742
Solve the following coding problem using the programming language python: Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image] Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$. No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 100$) — the number of light bulbs on the garland. The second line contains $n$ integers $p_1,\ p_2,\ \ldots,\ p_n$ ($0 \le p_i \le n$) — the number on the $i$-th bulb, or $0$ if it was removed. -----Output----- Output a single number — the minimum complexity of the garland. -----Examples----- Input 5 0 5 0 2 3 Output 2 Input 7 1 0 0 5 0 0 2 Output 1 -----Note----- In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity. In the second case, one of the correct answers is 1 7 3 5 6 4 2. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys readline = sys.stdin.readline N = int(readline()) S = list(map(int, readline().split())) S = [0 if s == 0 else 1 if s & 1 else -1 for s in S] odd = -(-N//2) even = N//2 for s in S: if s: if s == 1: odd -= 1 else: even -= 1 inf = 10**9 dpe = [[inf]*(odd+1) for _ in range(even+1)] dpo = [[inf]*(odd+1) for _ in range(even+1)] dpe[0][0] = 0 dpo[0][0] = 0 for i in range(N): dp2e = [[inf]*(odd+1) for _ in range(even+1)] dp2o = [[inf]*(odd+1) for _ in range(even+1)] s = S[i] for e in range(even+1): for o in range(odd+1): if s == 1: dp2o[e][o] = min(dp2o[e][o], dpo[e][o], 1+dpe[e][o]) elif s == -1: dp2e[e][o] = min(dp2e[e][o], dpe[e][o], 1+dpo[e][o]) else: if o < odd: dp2o[e][o+1] = min(dp2o[e][o+1], dpo[e][o], 1+dpe[e][o]) if e < even: dp2e[e+1][o] = min(dp2e[e+1][o], dpe[e][o], 1+dpo[e][o]) dpe = [d[:] for d in dp2e] dpo = [d[:] for d in dp2o] print(min(dpe[even][odd], dpo[even][odd])) ```
vfc_23675
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1286/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n0 5 0 2 3\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7\n1 0 0 5 0 0 2\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n0\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "20\n7 0 9 0 5 0 15 0 0 1 17 0 11 19 0 0 3 0 13 0\n", "output": "15\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "20\n9 0 0 0 18 0 11 0 0 4 0 15 0 0 0 14 0 0 5 0\n", "output": "6\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3743
Solve the following coding problem using the programming language python: Ujan has been lazy lately, but now has decided to bring his yard to good shape. First, he decided to paint the path from his house to the gate. The path consists of $n$ consecutive tiles, numbered from $1$ to $n$. Ujan will paint each tile in some color. He will consider the path aesthetic if for any two different tiles with numbers $i$ and $j$, such that $|j - i|$ is a divisor of $n$ greater than $1$, they have the same color. Formally, the colors of two tiles with numbers $i$ and $j$ should be the same if $|i-j| > 1$ and $n \bmod |i-j| = 0$ (where $x \bmod y$ is the remainder when dividing $x$ by $y$). Ujan wants to brighten up space. What is the maximum number of different colors that Ujan can use, so that the path is aesthetic? -----Input----- The first line of input contains a single integer $n$ ($1 \leq n \leq 10^{12}$), the length of the path. -----Output----- Output a single integer, the maximum possible number of colors that the path can be painted in. -----Examples----- Input 4 Output 2 Input 5 Output 5 -----Note----- In the first sample, two colors is the maximum number. Tiles $1$ and $3$ should have the same color since $4 \bmod |3-1| = 0$. Also, tiles $2$ and $4$ should have the same color since $4 \bmod |4-2| = 0$. In the second sample, all five colors can be used. [Image] 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()) p = 2 while n % p and p ** 2 <= n: p += 1 if p ** 2 > n: p = n pw = n.bit_length() // p.bit_length() while pow(p, pw) < n: pw += 1 while pow(p, pw) > n: pw -= 1 if pow(p, pw) == n: print(p) else: print(1) ```
vfc_23679
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1242/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n", "output": "5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n", "output": "1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3744
Solve the following coding problem using the programming language python: There are n students at Berland State University. Every student has two skills, each measured as a number: a_{i} — the programming skill and b_{i} — the sports skill. It is announced that an Olympiad in programming and sports will be held soon. That's why Berland State University should choose two teams: one to take part in the programming track and one to take part in the sports track. There should be exactly p students in the programming team and exactly s students in the sports team. A student can't be a member of both teams. The university management considers that the strength of the university on the Olympiad is equal to the sum of two values: the programming team strength and the sports team strength. The strength of a team is the sum of skills of its members in the corresponding area, so the strength of the programming team is the sum of all a_{i} and the strength of the sports team is the sum of all b_{i} over corresponding team members. Help Berland State University to compose two teams to maximize the total strength of the university on the Olympiad. -----Input----- The first line contains three positive integer numbers n, p and s (2 ≤ n ≤ 3000, p + s ≤ n) — the number of students, the size of the programming team and the size of the sports team. The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 3000), where a_{i} is the programming skill of the i-th student. The third line contains n positive integers b_1, b_2, ..., b_{n} (1 ≤ b_{i} ≤ 3000), where b_{i} is the sports skill of the i-th student. -----Output----- In the first line, print the the maximum strength of the university on the Olympiad. In the second line, print p numbers — the members of the programming team. In the third line, print s numbers — the members of the sports team. The students are numbered from 1 to n as they are given in the input. All numbers printed in the second and in the third lines should be distinct and can be printed in arbitrary order. If there are multiple solutions, print any of them. -----Examples----- Input 5 2 2 1 3 4 5 2 5 3 2 1 4 Output 18 3 4 1 5 Input 4 2 2 10 8 8 3 10 7 9 4 Output 31 1 2 3 4 Input 5 3 1 5 2 5 1 7 6 3 1 6 3 Output 23 1 3 5 4 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python #!/usr/bin/env python3 from itertools import accumulate from heapq import heappop, heappush def top(ppl_indices, vals, start): Q = [] res = [0 for i in range(len(ppl_indices))] for k, idx in enumerate(ppl_indices): heappush(Q, -vals[idx]) if k >= start: res[k] = res[k-1] - heappop(Q) return res n, a_size, b_size = list(map(int, input().split())) a = list(map(int, input().split())) b = list(map(int, input().split())) conversion_gain = [y - x for x, y in zip(a, b)] ordered_by_a = sorted(zip(a, list(range(n))), reverse=True) prefix_sums_a = list(accumulate([x for x, y in ordered_by_a])) conversions = top([idx for val, idx in ordered_by_a], conversion_gain, a_size) rest_of_bs = list(reversed(top([idx for val, idx in reversed(ordered_by_a[a_size:])], b, n - a_size - b_size))) + [0] sol, top_k = max([(prefix_a + convert + add_bs, idx) for idx, (prefix_a, convert, add_bs) in enumerate(zip(prefix_sums_a[a_size-1:a_size+b_size], conversions[a_size-1:a_size+b_size], rest_of_bs))]) top_k += a_size conversion_ordered_by_a = [(conversion_gain[idx], idx) for val, idx in ordered_by_a] conversion_sorted = sorted(conversion_ordered_by_a[:top_k], reverse=True) converted = [idx for val, idx in conversion_sorted[:top_k-a_size]] team_a = list(set(idx for val, idx in ordered_by_a[:top_k]) - set(converted)) b_ordered_by_a = [(b[idx], idx) for val, idx in ordered_by_a] b_sorted = sorted(b_ordered_by_a[top_k:], reverse=True) team_b = converted + [idx for val, idx in b_sorted[:(a_size+b_size) - top_k]] print(sol) print(" ".join(str(idx+1) for idx in team_a)) print(" ".join(str(idx+1) for idx in team_b)) ```
vfc_23683
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/730/I", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5 2 2\n1 3 4 5 2\n5 3 2 1 4\n", "output": "18\n3 4 \n1 5 \n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3745
Solve the following coding problem using the programming language python: One day student Vasya was sitting on a lecture and mentioned a string s_1s_2... s_{n}, consisting of letters "a", "b" and "c" that was written on his desk. As the lecture was boring, Vasya decided to complete the picture by composing a graph G with the following properties: G has exactly n vertices, numbered from 1 to n. For all pairs of vertices i and j, where i ≠ j, there is an edge connecting them if and only if characters s_{i} and s_{j} are either equal or neighbouring in the alphabet. That is, letters in pairs "a"-"b" and "b"-"c" are neighbouring, while letters "a"-"c" are not. Vasya painted the resulting graph near the string and then erased the string. Next day Vasya's friend Petya came to a lecture and found some graph at his desk. He had heard of Vasya's adventure and now he wants to find out whether it could be the original graph G, painted by Vasya. In order to verify this, Petya needs to know whether there exists a string s, such that if Vasya used this s he would produce the given graph G. -----Input----- The first line of the input contains two integers n and m $(1 \leq n \leq 500,0 \leq m \leq \frac{n(n - 1)}{2})$ — the number of vertices and edges in the graph found by Petya, respectively. Each of the next m lines contains two integers u_{i} and v_{i} (1 ≤ u_{i}, v_{i} ≤ n, u_{i} ≠ v_{i}) — the edges of the graph G. It is guaranteed, that there are no multiple edges, that is any pair of vertexes appear in this list no more than once. -----Output----- In the first line print "Yes" (without the quotes), if the string s Petya is interested in really exists and "No" (without the quotes) otherwise. If the string s exists, then print it on the second line of the output. The length of s must be exactly n, it must consist of only letters "a", "b" and "c" only, and the graph built using this string must coincide with G. If there are multiple possible answers, you may print any of them. -----Examples----- Input 2 1 1 2 Output Yes aa Input 4 3 1 2 1 3 1 4 Output No -----Note----- In the first sample you are given a graph made of two vertices with an edge between them. So, these vertices can correspond to both the same and adjacent letters. Any of the following strings "aa", "ab", "ba", "bb", "bc", "cb", "cc" meets the graph's conditions. In the second sample the first vertex is connected to all three other vertices, but these three vertices are not connected with each other. That means that they must correspond to distinct letters that are not adjacent, but that is impossible as there are only two such letters: a and c. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # -*- coding:utf-8 -*- """ created by shuangquan.huang at 1/17/20 """ import collections import time import os import sys import bisect import heapq from typing import List def solve(N, M, edges): if M == N*(N-1)//2: return 'a' * N g = collections.defaultdict(list) for u, v in edges: g[u].append(v) g[v].append(u) s = ['', 'a', 'b', 'c'] for u in range(1, N+1): mark = [3 for _ in range(N + 1)] mark[0] = 0 mark[u] = 1 for v in g[u]: mark[v] = 1 for u in range(1, N+1): if mark[u] == 3: for v in g[u]: if mark[v] == 1: mark[v] = 2 a, b, c = mark.count(1), mark.count(2), mark.count(3) if a*(a-1)//2 + b*(b-1)//2 + c*(c-1)//2 + a*b + b*c != M: continue if any([abs(mark[u] - mark[v]) > 1 for u, v in edges]): continue return ''.join([s[mark[v]] for v in range(1, N+1)]) return None N, M = map(int, input().split()) edges = [] for i in range(M): u, v = map(int, input().split()) edges.append((u, v)) s = solve(N, M, edges) if s: print('Yes') print(s) else: print('No') ```
vfc_23687
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/623/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2 1\n1 2\n", "output": "Yes\naa\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3747
Solve the following coding problem using the programming language python: Bash wants to become a Pokemon master one day. Although he liked a lot of Pokemon, he has always been fascinated by Bulbasaur the most. Soon, things started getting serious and his fascination turned into an obsession. Since he is too young to go out and catch Bulbasaur, he came up with his own way of catching a Bulbasaur. Each day, he takes the front page of the newspaper. He cuts out the letters one at a time, from anywhere on the front page of the newspaper to form the word "Bulbasaur" (without quotes) and sticks it on his wall. Bash is very particular about case — the first letter of "Bulbasaur" must be upper case and the rest must be lower case. By doing this he thinks he has caught one Bulbasaur. He then repeats this step on the left over part of the newspaper. He keeps doing this until it is not possible to form the word "Bulbasaur" from the newspaper. Given the text on the front page of the newspaper, can you tell how many Bulbasaurs he will catch today? Note: uppercase and lowercase letters are considered different. -----Input----- Input contains a single line containing a string s (1 ≤ |s| ≤ 10^5) — the text on the front page of the newspaper without spaces and punctuation marks. |s| is the length of the string s. The string s contains lowercase and uppercase English letters, i.e. $s_{i} \in \{a, b, \ldots, z, A, B, \ldots, Z \}$. -----Output----- Output a single integer, the answer to the problem. -----Examples----- Input Bulbbasaur Output 1 Input F Output 0 Input aBddulbasaurrgndgbualdBdsagaurrgndbb Output 2 -----Note----- In the first case, you could pick: Bulbbasaur. In the second case, there is no way to pick even a single Bulbasaur. In the third case, you can rearrange the string to BulbasaurBulbasauraddrgndgddgargndbb to get two words "Bulbasaur". 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 Counter def main(): s = input() c = Counter(s) w = Counter("Bulbasaur") ans = 1e9 for char in w: ans = min(ans, c[char] // w[char]) print(ans) main() ```
vfc_23695
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/757/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "Bulbbasaur\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "F\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3750
Solve the following coding problem using the programming language python: Misha and Vanya have played several table tennis sets. Each set consists of several serves, each serve is won by one of the players, he receives one point and the loser receives nothing. Once one of the players scores exactly k points, the score is reset and a new set begins. Across all the sets Misha scored a points in total, and Vanya scored b points. Given this information, determine the maximum number of sets they could have played, or that the situation is impossible. Note that the game consisted of several complete sets. -----Input----- The first line contains three space-separated integers k, a and b (1 ≤ k ≤ 10^9, 0 ≤ a, b ≤ 10^9, a + b > 0). -----Output----- If the situation is impossible, print a single number -1. Otherwise, print the maximum possible number of sets. -----Examples----- Input 11 11 5 Output 1 Input 11 2 3 Output -1 -----Note----- Note that the rules of the game in this problem differ from the real table tennis game, for example, the rule of "balance" (the winning player has to be at least two points ahead to win a set) has no power within the present problem. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python k, a, b = map(int, input().split()) mx_a = a // k mx_b = b // k if mx_a == 0 and b % k != 0: print(-1) elif mx_b == 0 and a % k != 0: print(-1) else: print(mx_a + mx_b) ```
vfc_23707
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/765/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "11 11 5\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "11 2 3\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 5 9\n", "output": "14\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 3 3\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1000000000 1000000000\n", "output": "2000000000\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3751
Solve the following coding problem using the programming language python: Kostya likes Codeforces contests very much. However, he is very disappointed that his solutions are frequently hacked. That's why he decided to obfuscate (intentionally make less readable) his code before upcoming contest. To obfuscate the code, Kostya first looks at the first variable name used in his program and replaces all its occurrences with a single symbol a, then he looks at the second variable name that has not been replaced yet, and replaces all its occurrences with b, and so on. Kostya is well-mannered, so he doesn't use any one-letter names before obfuscation. Moreover, there are at most 26 unique identifiers in his programs. You are given a list of identifiers of some program with removed spaces and line breaks. Check if this program can be a result of Kostya's obfuscation. -----Input----- In the only line of input there is a string S of lowercase English letters (1 ≤ |S| ≤ 500) — the identifiers of a program with removed whitespace characters. -----Output----- If this program can be a result of Kostya's obfuscation, print "YES" (without quotes), otherwise print "NO". -----Examples----- Input abacaba Output YES Input jinotega Output NO -----Note----- In the first sample case, one possible list of identifiers would be "number string number character number string number". Here how Kostya would obfuscate the program: replace all occurences of number with a, the result would be "a string a character a string a", replace all occurences of string with b, the result would be "a b a character a b a", replace all occurences of character with c, the result would be "a b a c a b a", all identifiers have been replaced, thus the obfuscation is finished. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python s = input() last = -1 for i in range(26): c = chr(ord('a') + i) kk = s.find(c) if kk == -1: kk = len(s) if kk < last: print("NO") return last = kk print("YES") ```
vfc_23711
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/765/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "abacaba\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "jinotega\n", "output": "NO\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3752
Solve the following coding problem using the programming language python: Julia is going to cook a chicken in the kitchen of her dormitory. To save energy, the stove in the kitchen automatically turns off after k minutes after turning on. During cooking, Julia goes to the kitchen every d minutes and turns on the stove if it is turned off. While the cooker is turned off, it stays warm. The stove switches on and off instantly. It is known that the chicken needs t minutes to be cooked on the stove, if it is turned on, and 2t minutes, if it is turned off. You need to find out, how much time will Julia have to cook the chicken, if it is considered that the chicken is cooked evenly, with constant speed when the stove is turned on and at a constant speed when it is turned off. -----Input----- The single line contains three integers k, d and t (1 ≤ k, d, t ≤ 10^18). -----Output----- Print a single number, the total time of cooking in minutes. The relative or absolute error must not exceed 10^{ - 9}. Namely, let's assume that your answer is x and the answer of the jury is y. The checker program will consider your answer correct if $\frac{|x - y|}{\operatorname{max}(1, y)} \leq 10^{-9}$. -----Examples----- Input 3 2 6 Output 6.5 Input 4 2 20 Output 20.0 -----Note----- In the first example, the chicken will be cooked for 3 minutes on the turned on stove, after this it will be cooked for $\frac{3}{6}$. Then the chicken will be cooked for one minute on a turned off stove, it will be cooked for $\frac{1}{12}$. Thus, after four minutes the chicken will be cooked for $\frac{3}{6} + \frac{1}{12} = \frac{7}{12}$. Before the fifth minute Julia will turn on the stove and after 2.5 minutes the chicken will be ready $\frac{7}{12} + \frac{2.5}{6} = 1$. In the second example, when the stove is turned off, Julia will immediately turn it on, so the stove will always be turned on and the chicken will be cooked in 20 minutes. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def f(x): res = (x // k1) * val e = x % k1 if e <= k: res += e else: res += k + (e - k) / 2 return res k, d, t = map(int, input().split()) c = (k - 1) // d + 1 k1 = c * d rem = -k % d val = k + rem / 2 l = 0 r = 1e50 for i in range(500): m = (l + r) / 2 if f(m) >= t: r = m else: l = m print('%.10f' % r) ```
vfc_23715
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/936/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 2 6\n", "output": "6.5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 2 20\n", "output": "20.0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3753
Solve the following coding problem using the programming language python: All of us love treasures, right? That's why young Vasya is heading for a Treasure Island. Treasure Island may be represented as a rectangular table $n \times m$ which is surrounded by the ocean. Let us number rows of the field with consecutive integers from $1$ to $n$ from top to bottom and columns with consecutive integers from $1$ to $m$ from left to right. Denote the cell in $r$-th row and $c$-th column as $(r, c)$. Some of the island cells contain impassable forests, and some cells are free and passable. Treasure is hidden in cell $(n, m)$. Vasya got off the ship in cell $(1, 1)$. Now he wants to reach the treasure. He is hurrying up, so he can move only from cell to the cell in next row (downwards) or next column (rightwards), i.e. from cell $(x, y)$ he can move only to cells $(x+1, y)$ and $(x, y+1)$. Of course Vasya can't move through cells with impassable forests. Evil Witch is aware of Vasya's journey and she is going to prevent him from reaching the treasure. Before Vasya's first move she is able to grow using her evil magic impassable forests in previously free cells. Witch is able to grow a forest in any number of any free cells except cells $(1, 1)$ where Vasya got off his ship and $(n, m)$ where the treasure is hidden. Help Evil Witch by finding out the minimum number of cells she has to turn into impassable forests so that Vasya is no longer able to reach the treasure. -----Input----- First line of input contains two positive integers $n$, $m$ ($3 \le n \cdot m \le 1\,000\,000$), sizes of the island. Following $n$ lines contains strings $s_i$ of length $m$ describing the island, $j$-th character of string $s_i$ equals "#" if cell $(i, j)$ contains an impassable forest and "." if the cell is free and passable. Let us remind you that Vasya gets of his ship at the cell $(1, 1)$, i.e. the first cell of the first row, and he wants to reach cell $(n, m)$, i.e. the last cell of the last row. It's guaranteed, that cells $(1, 1)$ and $(n, m)$ are empty. -----Output----- Print the only integer $k$, which is the minimum number of cells Evil Witch has to turn into impassable forest in order to prevent Vasya from reaching the treasure. -----Examples----- Input 2 2 .. .. Output 2 Input 4 4 .... #.#. .... .#.. Output 1 Input 3 4 .... .##. .... Output 2 -----Note----- The following picture illustrates the island in the third example. Blue arrows show possible paths Vasya may use to go from $(1, 1)$ to $(n, m)$. Red illustrates one possible set of cells for the Witch to turn into impassable forest to make Vasya's trip from $(1, 1)$ to $(n, m)$ impossible. [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 input = sys.stdin.readline n,m=list(map(int,input().split())) MAP=[list(input().strip()) for i in range(n)] from collections import deque Q=deque() Q.append([0,0]) while Q: x,y=Q.pop() if x+1<n and MAP[x+1][y]==".": MAP[x+1][y]=1 Q.append([x+1,y]) if y+1<m and MAP[x][y+1]==".": MAP[x][y+1]=1 Q.append([x,y+1]) Q.append([n-1,m-1]) while Q: x,y=Q.pop() if x-1>=0 and MAP[x-1][y]==1: MAP[x-1][y]=0 Q.append([x-1,y]) if y-1>=0 and MAP[x][y-1]==1: MAP[x][y-1]=0 Q.append([x,y-1]) if MAP[n-1][m-1]!=1: print(0) return SCORE=[0]*(n+m+5) for i in range(n): for j in range(m): if MAP[i][j]==0: SCORE[i+j]+=1 if 1 in SCORE: print(1) else: print(2) ```
vfc_23719
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1214/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2 2\n..\n..\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 4\n....\n#.#.\n....\n.#..\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 4\n....\n.##.\n....\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 5\n.....\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 1\n.\n.\n.\n.\n.\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 3\n.#.\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3756
Solve the following coding problem using the programming language python: Efim just received his grade for the last test. He studies in a special school and his grade can be equal to any positive decimal fraction. First he got disappointed, as he expected a way more pleasant result. Then, he developed a tricky plan. Each second, he can ask his teacher to round the grade at any place after the decimal point (also, he can ask to round to the nearest integer). There are t seconds left till the end of the break, so Efim has to act fast. Help him find what is the maximum grade he can get in no more than t seconds. Note, that he can choose to not use all t seconds. Moreover, he can even choose to not round the grade at all. In this problem, classic rounding rules are used: while rounding number to the n-th digit one has to take a look at the digit n + 1. If it is less than 5 than the n-th digit remain unchanged while all subsequent digits are replaced with 0. Otherwise, if the n + 1 digit is greater or equal to 5, the digit at the position n is increased by 1 (this might also change some other digits, if this one was equal to 9) and all subsequent digits are replaced with 0. At the end, all trailing zeroes are thrown away. For example, if the number 1.14 is rounded to the first decimal place, the result is 1.1, while if we round 1.5 to the nearest integer, the result is 2. Rounding number 1.299996121 in the fifth decimal place will result in number 1.3. -----Input----- The first line of the input contains two integers n and t (1 ≤ n ≤ 200 000, 1 ≤ t ≤ 10^9) — the length of Efim's grade and the number of seconds till the end of the break respectively. The second line contains the grade itself. It's guaranteed that the grade is a positive number, containing at least one digit after the decimal points, and it's representation doesn't finish with 0. -----Output----- Print the maximum grade that Efim can get in t seconds. Do not print trailing zeroes. -----Examples----- Input 6 1 10.245 Output 10.25 Input 6 2 10.245 Output 10.3 Input 3 100 9.2 Output 9.2 -----Note----- In the first two samples Efim initially has grade 10.245. During the first second Efim can obtain grade 10.25, and then 10.3 during the next second. Note, that the answer 10.30 will be considered incorrect. In the third sample the optimal strategy is to not perform any rounding at all. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def main(): n, t = map(int, input().split()) s = input() dot = s.find('.') for i in range(dot + 1, n): if s[i] > '4': break else: print(s) return while t: i -= 1 t -= 1 if s[i] < '4': break if i > dot: print(s[:i], chr(ord(s[i]) + 1), sep='') return else: l = list(s[dot - 1::-1]) for i, c in enumerate(l): if c == '9': l[i] = '0' else: l[i] = chr(ord(c) + 1) break else: l.append('1') print(''.join(reversed(l))) def __starting_point(): main() __starting_point() ```
vfc_23731
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/718/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6 1\n10.245\n", "output": "10.25\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6 2\n10.245\n", "output": "10.3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 100\n9.2\n", "output": "9.2\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3757
Solve the following coding problem using the programming language python: For each string s consisting of characters '0' and '1' one can define four integers a_00, a_01, a_10 and a_11, where a_{xy} is the number of subsequences of length 2 of the string s equal to the sequence {x, y}. In these problem you are given four integers a_00, a_01, a_10, a_11 and have to find any non-empty string s that matches them, or determine that there is no such string. One can prove that if at least one answer exists, there exists an answer of length no more than 1 000 000. -----Input----- The only line of the input contains four non-negative integers a_00, a_01, a_10 and a_11. Each of them doesn't exceed 10^9. -----Output----- If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length of your answer must not exceed 1 000 000. -----Examples----- Input 1 2 3 4 Output Impossible Input 1 2 2 1 Output 0110 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import math def get_cnt(res): n = 1 + math.floor(math.sqrt(2 * res)) if n * (n-1) / 2 == res: return n else: return -1 def calc(): cnt = list(map(int, input().split())) if cnt == [0,0,0,0]: return '0' a = get_cnt(cnt[0]) b = get_cnt(cnt[3]) if cnt == [0,0,0,cnt[3]]: a = 0 if cnt == [cnt[0],0,0,0]: b = 0 if a == -1 or b == -1 or (a * b) != (cnt[1] + cnt[2]): return "Impossible" ans = ['0'] * (a + b) i = 0 while b > 0: while cnt[1] >= b: i = i + 1 cnt[1] -= b b -= 1 ans[i] = '1' i += 1 return ''.join(ans) print(calc()) ```
vfc_23735
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/708/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1 2 3 4\n", "output": "Impossible\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 2 2 1\n", "output": "0110\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3758
Solve the following coding problem using the programming language python: A game field is a strip of 1 × n square cells. In some cells there are Packmen, in some cells — asterisks, other cells are empty. Packman can move to neighboring cell in 1 time unit. If there is an asterisk in the target cell then Packman eats it. Packman doesn't spend any time to eat an asterisk. In the initial moment of time all Packmen begin to move. Each Packman can change direction of its move unlimited number of times, but it is not allowed to go beyond the boundaries of the game field. Packmen do not interfere with the movement of other packmen; in one cell there can be any number of packmen moving in any directions. Your task is to determine minimum possible time after which Packmen can eat all the asterisks. -----Input----- The first line contains a single integer n (2 ≤ n ≤ 10^5) — the length of the game field. The second line contains the description of the game field consisting of n symbols. If there is symbol '.' in position i — the cell i is empty. If there is symbol '*' in position i — in the cell i contains an asterisk. If there is symbol 'P' in position i — Packman is in the cell i. It is guaranteed that on the game field there is at least one Packman and at least one asterisk. -----Output----- Print minimum possible time after which Packmen can eat all asterisks. -----Examples----- Input 7 *..P*P* Output 3 Input 10 .**PP.*P.* Output 2 -----Note----- In the first example Packman in position 4 will move to the left and will eat asterisk in position 1. He will spend 3 time units on it. During the same 3 time units Packman in position 6 will eat both of neighboring with it asterisks. For example, it can move to the left and eat asterisk in position 5 (in 1 time unit) and then move from the position 5 to the right and eat asterisk in the position 7 (in 2 time units). So in 3 time units Packmen will eat all asterisks on the game field. In the second example Packman in the position 4 will move to the left and after 2 time units will eat asterisks in positions 3 and 2. Packmen in positions 5 and 8 will move to the right and in 2 time units will eat asterisks in positions 7 and 10, respectively. So 2 time units is enough for Packmen to eat all asterisks on the game field. 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()) s = stdin.readline().strip() mins = [] packs = [] for i in range(len(s)): if s[i] == '*': mins.append(i) elif s[i] == 'P': packs.append(i) l, r = -1, 2 * len(s) + 1 while r - l > 1: m = (l + r) >> 1 test1 = mins[:] test2 = packs[:] while test2 and test1: cnt = m pos = test2.pop() if pos > test1[-1]: while test1 and abs(pos - test1[-1]) <= cnt: cnt -= abs(pos - test1[-1]) pos = test1[-1] test1.pop() else: cntl, cntr = 0, 0 if abs(test1[-1] - pos) > m: break lpos = (m + pos - test1[-1]) // 2 rpos = m - 2 * abs(test1[-1] - pos) lb, rb = -1, len(test1) while rb - lb > 1: mb = (lb + rb) >> 1 if pos - test1[mb] <= lpos: rb = mb else: lb = mb cntl = len(test1) - rb lb, rb = -1, len(test1) while rb - lb > 1: mb = (lb + rb) >> 1 if pos - test1[mb] <= rpos: rb = mb else: lb = mb cntr = len(test1) - rb cnt = max(cntl, cntr) while test1 and cnt: test1.pop() cnt -= 1 if not test1: r = m else: l = m stdout.write(str(r)) ```
vfc_23739
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/847/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "7\n*..P*P*\n", "output": "3\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3759
Solve the following coding problem using the programming language python: Imagine you have an infinite 2D plane with Cartesian coordinate system. Some of the integral points are blocked, and others are not. Two integral points A and B on the plane are 4-connected if and only if: the Euclidean distance between A and B is one unit and neither A nor B is blocked; or there is some integral point C, such that A is 4-connected with C, and C is 4-connected with B. Let's assume that the plane doesn't contain blocked points. Consider all the integral points of the plane whose Euclidean distance from the origin is no more than n, we'll name these points special. Chubby Yang wants to get the following property: no special point is 4-connected to some non-special point. To get the property she can pick some integral points of the plane and make them blocked. What is the minimum number of points she needs to pick? -----Input----- The first line contains an integer n (0 ≤ n ≤ 4·10^7). -----Output----- Print a single integer — the minimum number of points that should be blocked. -----Examples----- Input 1 Output 4 Input 2 Output 8 Input 3 Output 16 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python print(max(1, 4 * int(int(input()) * 2 ** 0.5))) ```
vfc_23743
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/392/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n", "output": "8\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n", "output": "16\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3760
Solve the following coding problem using the programming language python: You are given a rectangle grid. That grid's size is n × m. Let's denote the coordinate system on the grid. So, each point on the grid will have coordinates — a pair of integers (x, y) (0 ≤ x ≤ n, 0 ≤ y ≤ m). Your task is to find a maximum sub-rectangle on the grid (x_1, y_1, x_2, y_2) so that it contains the given point (x, y), and its length-width ratio is exactly (a, b). In other words the following conditions must hold: 0 ≤ x_1 ≤ x ≤ x_2 ≤ n, 0 ≤ y_1 ≤ y ≤ y_2 ≤ m, $\frac{x_{2} - x_{1}}{y_{2} - y_{1}} = \frac{a}{b}$. The sides of this sub-rectangle should be parallel to the axes. And values x_1, y_1, x_2, y_2 should be integers. [Image] If there are multiple solutions, find the rectangle which is closest to (x, y). Here "closest" means the Euclid distance between (x, y) and the center of the rectangle is as small as possible. If there are still multiple solutions, find the lexicographically minimum one. Here "lexicographically minimum" means that we should consider the sub-rectangle as sequence of integers (x_1, y_1, x_2, y_2), so we can choose the lexicographically minimum one. -----Input----- The first line contains six integers n, m, x, y, a, b (1 ≤ n, m ≤ 10^9, 0 ≤ x ≤ n, 0 ≤ y ≤ m, 1 ≤ a ≤ n, 1 ≤ b ≤ m). -----Output----- Print four integers x_1, y_1, x_2, y_2, which represent the founded sub-rectangle whose left-bottom point is (x_1, y_1) and right-up point is (x_2, y_2). -----Examples----- Input 9 9 5 5 2 1 Output 1 3 9 7 Input 100 100 52 50 46 56 Output 17 8 86 92 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from fractions import gcd n, m, x, y, a, b = map(int, input().split()) r = gcd(a, b) a, b = a // r, b // r r = min(n // a, m // b) a, b = a * r, b * r cx, cy = (a + 1) // 2, (b + 1) // 2 dx, dy = min(n - a, max(cx, x) - cx), min(m - b, max(cy, y) - cy) print(dx, dy, a + dx, b + dy) ```
vfc_23747
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/303/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "9 9 5 5 2 1\n", "output": "1 3 9 7\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "100 100 52 50 46 56\n", "output": "17 8 86 92\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "100 100 16 60 42 75\n", "output": "0 0 56 100\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "100 100 28 22 47 50\n", "output": "0 0 94 100\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "100 100 44 36 96 21\n", "output": "0 25 96 46\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "100 100 56 46 1 47\n", "output": "55 0 57 94\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3762
Solve the following coding problem using the programming language python: Fox Ciel studies number theory. She thinks a non-empty set S contains non-negative integers is perfect if and only if for any $a, b \in S$ (a can be equal to b), $(a \text{xor} b) \in S$. Where operation xor means exclusive or operation (http://en.wikipedia.org/wiki/Exclusive_or). Please calculate the number of perfect sets consisting of integers not greater than k. The answer can be very large, so print it modulo 1000000007 (10^9 + 7). -----Input----- The first line contains an integer k (0 ≤ k ≤ 10^9). -----Output----- Print a single integer — the number of required sets modulo 1000000007 (10^9 + 7). -----Examples----- Input 1 Output 2 Input 2 Output 3 Input 3 Output 5 Input 4 Output 6 -----Note----- In example 1, there are 2 such sets: {0} and {0, 1}. Note that {1} is not a perfect set since 1 xor 1 = 0 and {1} doesn't contain zero. In example 4, there are 6 such sets: {0}, {0, 1}, {0, 2}, {0, 3}, {0, 4} and {0, 1, 2, 3}. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from math import factorial MOD = 10**9+7 k = int(input()) bink = list(map(int, bin(k)[2:])) N = len(bink) # dp[i][j][k] = first i bits, j bases, # k = 1 if maxor matches k, 0 else dp = [[[0,0] for j in range(i+2)] for i in range(N+1)] dp[0][0][1] = 1 for i in range(1, N+1): for j in range(i+1): # k = 0 -> 0 dp[i][j][0]+= 2**j * dp[i-1][j][0] # distribute if j: dp[i][j][0]+= dp[i-1][j-1][0] # new base # k = 1 -> 0 odd = 2**(j-1) if j else 0 even = 2**j - odd if bink[i-1] == 1: dp[i][j][0]+= even * dp[i-1][j][1] # distribute even # k = 1 -> 1 if bink[i-1] == 0: dp[i][j][1]+= even * dp[i-1][j][1] # distribute even else: dp[i][j][1]+= odd * dp[i-1][j][1] # distribute odd if j: dp[i][j][1]+= dp[i-1][j-1][1] # new base ans = sum(map(sum, dp[-1])) print(ans % MOD) ```
vfc_23755
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/388/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n", "output": "3\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3763
Solve the following coding problem using the programming language python: Maxim has opened his own restaurant! The restaurant has got a huge table, the table's length is p meters. Maxim has got a dinner party tonight, n guests will come to him. Let's index the guests of Maxim's restaurant from 1 to n. Maxim knows the sizes of all guests that are going to come to him. The i-th guest's size (a_{i}) represents the number of meters the guest is going to take up if he sits at the restaurant table. Long before the dinner, the guests line up in a queue in front of the restaurant in some order. Then Maxim lets the guests in, one by one. Maxim stops letting the guests in when there is no place at the restaurant table for another guest in the queue. There is no place at the restaurant table for another guest in the queue, if the sum of sizes of all guests in the restaurant plus the size of this guest from the queue is larger than p. In this case, not to offend the guest who has no place at the table, Maxim doesn't let any other guest in the restaurant, even if one of the following guests in the queue would have fit in at the table. Maxim is now wondering, what is the average number of visitors who have come to the restaurant for all possible n! orders of guests in the queue. Help Maxim, calculate this number. -----Input----- The first line contains integer n (1 ≤ n ≤ 50) — the number of guests in the restaurant. The next line contains integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 50) — the guests' sizes in meters. The third line contains integer p (1 ≤ p ≤ 50) — the table's length in meters. The numbers in the lines are separated by single spaces. -----Output----- In a single line print a real number — the answer to the problem. The answer will be considered correct, if the absolute or relative error doesn't exceed 10^{ - 4}. -----Examples----- Input 3 1 2 3 3 Output 1.3333333333 -----Note----- In the first sample the people will come in the following orders: (1, 2, 3) — there will be two people in the restaurant; (1, 3, 2) — there will be one person in the restaurant; (2, 1, 3) — there will be two people in the restaurant; (2, 3, 1) — there will be one person in the restaurant; (3, 1, 2) — there will be one person in the restaurant; (3, 2, 1) — there will be one person in the restaurant. In total we get (2 + 1 + 2 + 1 + 1 + 1) / 6 = 8 / 6 = 1.(3). The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import math n = int(input()) a = [int(x) for x in input().split()] p = int(input()) sum=0; for x in range(n): sum+=a[x] if(sum<=p): print(n) else: ans=0 for i in range(n): dp = [[[0 for z in range(55)] for y in range(55)] for x in range(55)] dp[-1][0][0]=1 for j in range(n): if(j==i): for k in range(n): for z in range(p+1): dp[j][k][z]=dp[j-1][k][z] continue for k in range(n): for z in range(p+1): if(z+a[j]<=p): dp[j][k+1][z+a[j]]+=dp[j-1][k][z] dp[j][k][z]+=dp[j-1][k][z] for k in range(n): for z in range(p+1): if(z+a[i]>p): ans+=k*dp[n-1][k][z]*math.factorial(k)*math.factorial(n-k-1) print(ans/math.factorial(n)) ```
vfc_23759
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/261/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n1 2 3\n3\n", "output": "1.3333333333\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "9\n2 2 2 2 2 2 2 1 2\n9\n", "output": "4.5555555556\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7\n2 1 1 2 1 1 2\n2\n", "output": "1.2857142857\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "8\n3 1 5 6 1 5 4 4\n7\n", "output": "1.6250000000\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3764
Solve the following coding problem using the programming language python: Jon Snow now has to fight with White Walkers. He has n rangers, each of which has his own strength. Also Jon Snow has his favourite number x. Each ranger can fight with a white walker only if the strength of the white walker equals his strength. He however thinks that his rangers are weak and need to improve. Jon now thinks that if he takes the bitwise XOR of strengths of some of rangers with his favourite number x, he might get soldiers of high strength. So, he decided to do the following operation k times: Arrange all the rangers in a straight line in the order of increasing strengths. Take the bitwise XOR (is written as $\oplus$) of the strength of each alternate ranger with x and update it's strength. Suppose, Jon has 5 rangers with strengths [9, 7, 11, 15, 5] and he performs the operation 1 time with x = 2. He first arranges them in the order of their strengths, [5, 7, 9, 11, 15]. Then he does the following: The strength of first ranger is updated to $5 \oplus 2$, i.e. 7. The strength of second ranger remains the same, i.e. 7. The strength of third ranger is updated to $9 \oplus 2$, i.e. 11. The strength of fourth ranger remains the same, i.e. 11. The strength of fifth ranger is updated to $15 \oplus 2$, i.e. 13. The new strengths of the 5 rangers are [7, 7, 11, 11, 13] Now, Jon wants to know the maximum and minimum strength of the rangers after performing the above operations k times. He wants your help for this task. Can you help him? -----Input----- First line consists of three integers n, k, x (1 ≤ n ≤ 10^5, 0 ≤ k ≤ 10^5, 0 ≤ x ≤ 10^3) — number of rangers Jon has, the number of times Jon will carry out the operation and Jon's favourite number respectively. Second line consists of n integers representing the strengths of the rangers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^3). -----Output----- Output two integers, the maximum and the minimum strength of the rangers after performing the operation k times. -----Examples----- Input 5 1 2 9 7 11 15 5 Output 13 7 Input 2 100000 569 605 986 Output 986 605 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, k, x = list(map(int, input().split())) rangers = list(map(int, input().split())) for i in range(min(k, 8 + k%4)): rangers.sort() rangers = [ rangers[i] if i%2 else rangers[i]^x for i in range(n)] # print(rangers) rangers.sort() print(rangers[-1], rangers[0]) ```
vfc_23763
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/768/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5 1 2\n9 7 11 15 5\n", "output": "13 7", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 100000 569\n605 986\n", "output": "986 605", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 10 98\n1 58 62 71 55 4 20 17 25 29\n", "output": "127 17", "type": "stdin_stdout" } ] }
apps
verifiable_code
3765
Solve the following coding problem using the programming language python: In one of the games Arkady is fond of the game process happens on a rectangular field. In the game process Arkady can buy extensions for his field, each extension enlarges one of the field sizes in a particular number of times. Formally, there are n extensions, the i-th of them multiplies the width or the length (by Arkady's choice) by a_{i}. Each extension can't be used more than once, the extensions can be used in any order. Now Arkady's field has size h × w. He wants to enlarge it so that it is possible to place a rectangle of size a × b on it (along the width or along the length, with sides parallel to the field sides). Find the minimum number of extensions needed to reach Arkady's goal. -----Input----- The first line contains five integers a, b, h, w and n (1 ≤ a, b, h, w, n ≤ 100 000) — the sizes of the rectangle needed to be placed, the initial sizes of the field and the number of available extensions. The second line contains n integers a_1, a_2, ..., a_{n} (2 ≤ a_{i} ≤ 100 000), where a_{i} equals the integer a side multiplies by when the i-th extension is applied. -----Output----- Print the minimum number of extensions needed to reach Arkady's goal. If it is not possible to place the rectangle on the field with all extensions, print -1. If the rectangle can be placed on the initial field, print 0. -----Examples----- Input 3 3 2 4 4 2 5 4 10 Output 1 Input 3 3 3 3 5 2 3 5 4 2 Output 0 Input 5 5 1 2 3 2 2 3 Output -1 Input 3 4 1 1 3 2 3 2 Output 3 -----Note----- In the first example it is enough to use any of the extensions available. For example, we can enlarge h in 5 times using the second extension. Then h becomes equal 10 and it is now possible to place the rectangle on the field. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys MAXV = 100010 d = [0] * MAXV a, b, h, w, n = list(map(int,input().split())) arr = input().split() for it in range(n): arr[it] = int(arr[it]) # print(arr) # print(a, b, h, w, n) def solve(a, b, h, w, z, product, it): # print(">", a, b, h, w, z, product, it) k = 0 if a % h: k = a // h + 1 else: k = a // h if k <= z and (product // z) * w >= b: print(it) return arr = sorted(arr) arr = arr[::-1] # print(arr) d[1] = 1 solve(a, b, h, w, 1, 1, 0) solve(a, b, w, h, 1, 1, 0) product = 1 xxx = 0 for it in range(1, n + 1): # arr[it - 1] = int(arr[it - 1]) product *= arr[it - 1] # print("=", arr[it - 1]) for j in reversed(list(range(1, MAXV))): if not d[j]: continue x = j * arr[it - 1] # x = min(x, MAXV - 1) if x < MAXV: d[x] = 1 else: if xxx: xxx = min(x, xxx) else: xxx = x if xxx: solve(a, b, h, w, xxx, product, it) solve(a, b, w, h, xxx, product, it) for j in range(MAXV): if d[j]: solve(a, b, h, w, j, product, it) solve(a, b, w, h, j, product, it) print(-1) ```
vfc_23767
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/799/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 3 2 4 4\n2 5 4 10\n", "output": "1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3766
Solve the following coding problem using the programming language python: Have you ever played Hanabi? If not, then you've got to try it out! This problem deals with a simplified version of the game. Overall, the game has 25 types of cards (5 distinct colors and 5 distinct values). Borya is holding n cards. The game is somewhat complicated by the fact that everybody sees Borya's cards except for Borya himself. Borya knows which cards he has but he knows nothing about the order they lie in. Note that Borya can have multiple identical cards (and for each of the 25 types of cards he knows exactly how many cards of this type he has). The aim of the other players is to achieve the state when Borya knows the color and number value of each of his cards. For that, other players can give him hints. The hints can be of two types: color hints and value hints. A color hint goes like that: a player names some color and points at all the cards of this color. Similarly goes the value hint. A player names some value and points at all the cards that contain the value. Determine what minimum number of hints the other players should make for Borya to be certain about each card's color and value. -----Input----- The first line contains integer n (1 ≤ n ≤ 100) — the number of Borya's cards. The next line contains the descriptions of n cards. The description of each card consists of exactly two characters. The first character shows the color (overall this position can contain five distinct letters — R, G, B, Y, W). The second character shows the card's value (a digit from 1 to 5). Borya doesn't know exact order of the cards they lie in. -----Output----- Print a single integer — the minimum number of hints that the other players should make. -----Examples----- Input 2 G3 G3 Output 0 Input 4 G4 R4 R3 B3 Output 2 Input 5 B1 Y1 W1 G1 R1 Output 4 -----Note----- In the first sample Borya already knows for each card that it is a green three. In the second sample we can show all fours and all red cards. In the third sample you need to make hints about any four colors. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python """ Codeforces Round 253 Div 1 Problem A Author : chaotic_iak Language: Python 3.3.4 """ def read(mode=2): # 0: String # 1: List of strings # 2: List of integers inputs = input().strip() if mode == 0: return inputs if mode == 1: return inputs.split() if mode == 2: return [int(x) for x in inputs.split()] def write(s="\n"): if isinstance(s, list): s = " ".join(map(str,s)) s = str(s) print(s, end="") ################################################### SOLUTION import itertools covers = itertools.product([0,1], repeat=10) n, = read() s = read(1) a = [0] * 25 colors = "RGBYW" for i in s: a[colors.index(i[0]) * 5 + int(i[1])-1] |= 1 def check(cover): nonlocal a unknowns = [0] * 11 for i in range(25): if not a[i]: continue id = -1 if not cover[i%5]: id = 5+i//5 if not cover[5+i//5]: if id == -1: id = i%5 else: id = 10 if id > -1: if unknowns[id]: return False unknowns[id] = 1 return True mn = 99 for i in covers: if check(i): mn = min(mn, sum(i)) print(mn) ```
vfc_23771
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/442/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\nG3 G3\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\nG4 R4 R3 B3\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\nB1 Y1 W1 G1 R1\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\nY4 B1 R3 G5 R5 W3 W5 W2 R1 Y1\n", "output": "6\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\nG4 G3 B4\n", "output": "2\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3767
Solve the following coding problem using the programming language python: Nick has n bottles of soda left after his birthday. Each bottle is described by two values: remaining amount of soda a_{i} and bottle volume b_{i} (a_{i} ≤ b_{i}). Nick has decided to pour all remaining soda into minimal number of bottles, moreover he has to do it as soon as possible. Nick spends x seconds to pour x units of soda from one bottle to another. Nick asks you to help him to determine k — the minimal number of bottles to store all remaining soda and t — the minimal time to pour soda into k bottles. A bottle can't store more soda than its volume. All remaining soda should be saved. -----Input----- The first line contains positive integer n (1 ≤ n ≤ 100) — the number of bottles. The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 100), where a_{i} is the amount of soda remaining in the i-th bottle. The third line contains n positive integers b_1, b_2, ..., b_{n} (1 ≤ b_{i} ≤ 100), where b_{i} is the volume of the i-th bottle. It is guaranteed that a_{i} ≤ b_{i} for any i. -----Output----- The only line should contain two integers k and t, where k is the minimal number of bottles that can store all the soda and t is the minimal time to pour the soda into k bottles. -----Examples----- Input 4 3 3 4 3 4 7 6 5 Output 2 6 Input 2 1 1 100 100 Output 1 1 Input 5 10 30 5 6 24 10 41 7 8 24 Output 3 11 -----Note----- In the first example Nick can pour soda from the first bottle to the second bottle. It will take 3 seconds. After it the second bottle will contain 3 + 3 = 6 units of soda. Then he can pour soda from the fourth bottle to the second bottle and to the third bottle: one unit to the second and two units to the third. It will take 1 + 2 = 3 seconds. So, all the soda will be in two bottles and he will spend 3 + 3 = 6 seconds to do it. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python f = lambda: list(map(int, input().split())) n = int(input()) a, b = f(), f() d = [[None] * 10001 for i in range(n)] def g(i, s): if s <= 0: return (0, s) if i == n: return (1e7, 0) if not d[i][s]: x, y = g(i + 1, s - b[i]) d[i][s] = min(g(i + 1, s), (x + 1, y + b[i] - a[i])) return d[i][s] x, y = g(0, sum(a)) print(x, y) ```
vfc_23775
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/730/J", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n3 3 4 3\n4 7 6 5\n", "output": "2 6\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3768
Solve the following coding problem using the programming language python: Alice and Bob decided to eat some fruit. In the kitchen they found a large bag of oranges and apples. Alice immediately took an orange for herself, Bob took an apple. To make the process of sharing the remaining fruit more fun, the friends decided to play a game. They put multiple cards and on each one they wrote a letter, either 'A', or the letter 'B'. Then they began to remove the cards one by one from left to right, every time they removed a card with the letter 'A', Alice gave Bob all the fruits she had at that moment and took out of the bag as many apples and as many oranges as she had before. Thus the number of oranges and apples Alice had, did not change. If the card had written letter 'B', then Bob did the same, that is, he gave Alice all the fruit that he had, and took from the bag the same set of fruit. After the last card way removed, all the fruit in the bag were over. You know how many oranges and apples was in the bag at first. Your task is to find any sequence of cards that Alice and Bob could have played with. -----Input----- The first line of the input contains two integers, x, y (1 ≤ x, y ≤ 10^18, xy > 1) — the number of oranges and apples that were initially in the bag. -----Output----- Print any sequence of cards that would meet the problem conditions as a compressed string of characters 'A' and 'B. That means that you need to replace the segments of identical consecutive characters by the number of repetitions of the characters and the actual character. For example, string AAABAABBB should be replaced by string 3A1B2A3B, but cannot be replaced by 2A1A1B2A3B or by 3AB2A3B. See the samples for clarifications of the output format. The string that you print should consist of at most 10^6 characters. It is guaranteed that if the answer exists, its compressed representation exists, consisting of at most 10^6 characters. If there are several possible answers, you are allowed to print any of them. If the sequence of cards that meet the problem statement does not not exist, print a single word Impossible. -----Examples----- Input 1 4 Output 3B Input 2 2 Output Impossible Input 3 2 Output 1A1B -----Note----- In the first sample, if the row contained three cards with letter 'B', then Bob should give one apple to Alice three times. So, in the end of the game Alice has one orange and three apples, and Bob has one apple, in total it is one orange and four apples. In second sample, there is no answer since one card is not enough for game to finish, and two cards will produce at least three apples or three oranges. In the third sample, cards contain letters 'AB', so after removing the first card Bob has one orange and one apple, and after removal of second card Alice has two oranges and one apple. So, in total it is three oranges and two apples. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys read = lambda: list(map(int, sys.stdin.readline().split())) x, y = read() res = [] c = 'A' while x * y > 1: k = min(x // y, x - 1) if k > 0: res.append('{}{}'.format(k, c)) x, y = y, x - k*y c = 'A' if c == 'B' else 'B' if x == 0 or y == 0: print('Impossible') else: print(''.join(res)) ```
vfc_23779
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/585/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1 4\n", "output": "3B\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 2\n", "output": "Impossible\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3769
Solve the following coding problem using the programming language python: As behooves any intelligent schoolboy, Kevin Sun is studying psycowlogy, cowculus, and cryptcowgraphy at the Bovinia State University (BGU) under Farmer Ivan. During his Mathematics of Olympiads (MoO) class, Kevin was confronted with a weird functional equation and needs your help. For two fixed integers k and p, where p is an odd prime number, the functional equation states that $f(k x \operatorname{mod} p) \equiv k \cdot f(x) \operatorname{mod} p$ for some function $f : \{0,1,2, \cdots, p - 1 \} \rightarrow \{0,1,2, \cdots, p - 1 \}$. (This equation should hold for any integer x in the range 0 to p - 1, inclusive.) It turns out that f can actually be many different functions. Instead of finding a solution, Kevin wants you to count the number of distinct functions f that satisfy this equation. Since the answer may be very large, you should print your result modulo 10^9 + 7. -----Input----- The input consists of two space-separated integers p and k (3 ≤ p ≤ 1 000 000, 0 ≤ k ≤ p - 1) on a single line. It is guaranteed that p is an odd prime number. -----Output----- Print a single integer, the number of distinct functions f modulo 10^9 + 7. -----Examples----- Input 3 2 Output 3 Input 5 4 Output 25 -----Note----- In the first sample, p = 3 and k = 2. The following functions work: f(0) = 0, f(1) = 1, f(2) = 2. f(0) = 0, f(1) = 2, f(2) = 1. f(0) = f(1) = f(2) = 0. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def main(): p, k = list(map(int, input().split())) s = 1 m = pow(10,9)+7 if k == 0: s = pow(p,p-1,m) elif k == 1: s = pow(p,p,m) else: o = 1 n = k while n != 1: n = k*n %p o += 1 c = (p-1)//o s = pow(p,c,m) print(s%m) main() ```
vfc_23783
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/603/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 2\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 4\n", "output": "25\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7 2\n", "output": "49\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7 6\n", "output": "343\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3772
Solve the following coding problem using the programming language python: Mad scientist Mike is building a time machine in his spare time. To finish the work, he needs a resistor with a certain resistance value. However, all Mike has is lots of identical resistors with unit resistance R_0 = 1. Elements with other resistance can be constructed from these resistors. In this problem, we will consider the following as elements: one resistor; an element and one resistor plugged in sequence; an element and one resistor plugged in parallel. [Image] With the consecutive connection the resistance of the new element equals R = R_{e} + R_0. With the parallel connection the resistance of the new element equals $R = \frac{1}{\frac{1}{R_{e}} + \frac{1}{R_{0}}}$. In this case R_{e} equals the resistance of the element being connected. Mike needs to assemble an element with a resistance equal to the fraction $\frac{a}{b}$. Determine the smallest possible number of resistors he needs to make such an element. -----Input----- The single input line contains two space-separated integers a and b (1 ≤ a, b ≤ 10^18). It is guaranteed that the fraction $\frac{a}{b}$ is irreducible. It is guaranteed that a solution always exists. -----Output----- Print a single number — the answer to the problem. Please do not use the %lld specifier to read or write 64-bit integers in С++. It is recommended to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 1 1 Output 1 Input 3 2 Output 3 Input 199 200 Output 200 -----Note----- In the first sample, one resistor is enough. In the second sample one can connect the resistors in parallel, take the resulting element and connect it to a third resistor consecutively. Then, we get an element with resistance $\frac{1}{\frac{1}{1} + \frac{1}{1}} + 1 = \frac{3}{2}$. We cannot make this element using two resistors. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python a, b = list(map(int, input().split())) res = 0 while b != 0: res += a // b a %= b a, b = b, a print(res) ```
vfc_23795
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/343/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1 1\n", "output": "1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3774
Solve the following coding problem using the programming language python: Little C loves number «3» very much. He loves all things about it. Now he is playing a game on a chessboard of size $n \times m$. The cell in the $x$-th row and in the $y$-th column is called $(x,y)$. Initially, The chessboard is empty. Each time, he places two chessmen on two different empty cells, the Manhattan distance between which is exactly $3$. The Manhattan distance between two cells $(x_i,y_i)$ and $(x_j,y_j)$ is defined as $|x_i-x_j|+|y_i-y_j|$. He want to place as many chessmen as possible on the chessboard. Please help him find the maximum number of chessmen he can place. -----Input----- A single line contains two integers $n$ and $m$ ($1 \leq n,m \leq 10^9$) — the number of rows and the number of columns of the chessboard. -----Output----- Print one integer — the maximum number of chessmen Little C can place. -----Examples----- Input 2 2 Output 0 Input 3 3 Output 8 -----Note----- In the first example, the Manhattan distance between any two cells is smaller than $3$, so the answer is $0$. In the second example, a possible solution is $(1,1)(3,2)$, $(1,2)(3,3)$, $(2,1)(1,3)$, $(3,1)(2,3)$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, m = list(map(int, input().split())) if n > m: n, m = m, n if n == 1: print(([0, 0, 0, 0, 1, 2][m % 6] + m // 6 * 3) * 2) return if n == 2: if m == 2: print(0) elif m == 3: print(4) elif m == 7: print(12) else: print(n * m) return print((n * m) // 2 * 2) ```
vfc_23803
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1034/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2 2\n", "output": "0", "type": "stdin_stdout" } ] }
apps
verifiable_code
3776
Solve the following coding problem using the programming language python: You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. -----Input----- The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. -----Output----- The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. -----Examples----- Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09 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()) s = input().split(':') if n == 12: if s[0] == '00': s[0] = '01' elif int(s[0]) > 12 and s[0][1] == '0': s[0] = '10' elif int(s[0]) > 12: s[0] = '0' + s[0][1] else: if int(s[0]) > 23: s[0] = '0' + s[0][1] if int(s[1]) > 59: s[1] = '0' + s[1][1] print(':'.join(s)) ```
vfc_23811
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/722/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "24\n17:30\n", "output": "17:30\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "12\n17:30\n", "output": "07:30\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "24\n99:99\n", "output": "09:09\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "12\n05:54\n", "output": "05:54\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "12\n00:05\n", "output": "01:05\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3778
Solve the following coding problem using the programming language python: To improve the boomerang throwing skills of the animals, Zookeeper has set up an $n \times n$ grid with some targets, where each row and each column has at most $2$ targets each. The rows are numbered from $1$ to $n$ from top to bottom, and the columns are numbered from $1$ to $n$ from left to right. For each column, Zookeeper will throw a boomerang from the bottom of the column (below the grid) upwards. When the boomerang hits any target, it will bounce off, make a $90$ degree turn to the right and fly off in a straight line in its new direction. The boomerang can hit multiple targets and does not stop until it leaves the grid. [Image] In the above example, $n=6$ and the black crosses are the targets. The boomerang in column $1$ (blue arrows) bounces $2$ times while the boomerang in column $3$ (red arrows) bounces $3$ times. The boomerang in column $i$ hits exactly $a_i$ targets before flying out of the grid. It is known that $a_i \leq 3$. However, Zookeeper has lost the original positions of the targets. Thus, he asks you to construct a valid configuration of targets that matches the number of hits for each column, or tell him that no such configuration exists. If multiple valid configurations exist, you may print any of them. -----Input----- The first line contains a single integer $n$ $(1 \leq n \leq 10^5)$. The next line contains $n$ integers $a_1,a_2,\ldots,a_n$ $(0 \leq a_i \leq 3)$. -----Output----- If no configuration of targets exist, print $-1$. Otherwise, on the first line print a single integer $t$ $(0 \leq t \leq 2n)$: the number of targets in your configuration. Then print $t$ lines with two spaced integers each per line. Each line should contain two integers $r$ and $c$ $(1 \leq r,c \leq n)$, where $r$ is the target's row and $c$ is the target's column. All targets should be different. Every row and every column in your configuration should have at most two targets each. -----Examples----- Input 6 2 0 3 0 1 1 Output 5 2 1 2 5 3 3 3 6 5 6 Input 1 0 Output 0 Input 6 3 2 2 2 1 1 Output -1 -----Note----- For the first test, the answer configuration is the same as in the picture from the statement. For the second test, the boomerang is not supposed to hit anything, so we can place $0$ targets. For the third test, the following configuration of targets matches the number of hits, but is not allowed as row $3$ has $4$ targets. [Image] It can be shown for this test case that no valid configuration of targets will result in the given number of target hits. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys readline = sys.stdin.readline N = int(readline()) A = list(map(int, readline().split())) Ans = [] one = [] tt = [] for i in range(N-1, -1, -1): a = A[i] if a == 0: continue if a == 1: Ans.append((i, i)) one.append((i, i)) continue if a == 2: if not one: Ans = None break else: oi, _ = one.pop() Ans.append((oi, i)) tt.append((oi, i)) continue else: if not tt: if not one: Ans = None break else: oi, wi = one.pop() Ans.append((i, wi)) Ans.append((i, i)) tt.append((i, i)) else: oi, wi = tt.pop() Ans.append((i, wi)) Ans.append((i, i)) tt.append((i, i)) continue if Ans is None: print(-1) else: print(len(Ans)) if Ans: print('\n'.join(f'{a+1} {b+1}' for a, b in Ans)) ```
vfc_23819
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1428/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6\n2 0 3 0 1 1\n", "output": "5\n1 1\n1 6\n2 3\n2 5\n3 5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n0\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n3 2 2 2 1 1\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n3 2 1\n", "output": "4\n1 1\n1 2\n2 2\n2 3\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3779
Solve the following coding problem using the programming language python: Astronaut Natasha arrived on Mars. She knows that the Martians are very poor aliens. To ensure a better life for the Mars citizens, their emperor decided to take tax from every tourist who visited the planet. Natasha is the inhabitant of Earth, therefore she had to pay the tax to enter the territory of Mars. There are $n$ banknote denominations on Mars: the value of $i$-th banknote is $a_i$. Natasha has an infinite number of banknotes of each denomination. Martians have $k$ fingers on their hands, so they use a number system with base $k$. In addition, the Martians consider the digit $d$ (in the number system with base $k$) divine. Thus, if the last digit in Natasha's tax amount written in the number system with the base $k$ is $d$, the Martians will be happy. Unfortunately, Natasha does not know the Martians' divine digit yet. Determine for which values $d$ Natasha can make the Martians happy. Natasha can use only her banknotes. Martians don't give her change. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 100\,000$, $2 \le k \le 100\,000$) — the number of denominations of banknotes and the base of the number system on Mars. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — denominations of banknotes on Mars. All numbers are given in decimal notation. -----Output----- On the first line output the number of values $d$ for which Natasha can make the Martians happy. In the second line, output all these values in increasing order. Print all numbers in decimal notation. -----Examples----- Input 2 8 12 20 Output 2 0 4 Input 3 10 10 20 30 Output 1 0 -----Note----- Consider the first test case. It uses the octal number system. If you take one banknote with the value of $12$, you will get $14_8$ in octal system. The last digit is $4_8$. If you take one banknote with the value of $12$ and one banknote with the value of $20$, the total value will be $32$. In the octal system, it is $40_8$. The last digit is $0_8$. If you take two banknotes with the value of $20$, the total value will be $40$, this is $50_8$ in the octal system. The last digit is $0_8$. No other digits other than $0_8$ and $4_8$ can be obtained. Digits $0_8$ and $4_8$ could also be obtained in other ways. The second test case uses the decimal number system. The nominals of all banknotes end with zero, so Natasha can give the Martians only the amount whose decimal notation also ends with zero. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import math n,k=map(int,input().split()) g=k for a in map(int,input().split()): g=math.gcd(g,a) print(k//g) print(' '.join(map(str,range(0,k,g)))) ```
vfc_23823
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1010/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2 8\n12 20\n", "output": "2\n0 4 ", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 10\n10 20 30\n", "output": "1\n0 ", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 10\n20 16 4 16 2\n", "output": "5\n0 2 4 6 8 ", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 5\n4 6 8 6 4 10 2 10 8 6\n", "output": "5\n0 1 2 3 4 ", "type": "stdin_stdout" } ] }
apps
verifiable_code
3783
Solve the following coding problem using the programming language python: You have a team of N people. For a particular task, you can pick any non-empty subset of people. The cost of having x people for the task is x^{k}. Output the sum of costs over all non-empty subsets of people. -----Input----- Only line of input contains two integers N (1 ≤ N ≤ 10^9) representing total number of people and k (1 ≤ k ≤ 5000). -----Output----- Output the sum of costs for all non empty subsets modulo 10^9 + 7. -----Examples----- Input 1 1 Output 1 Input 3 2 Output 24 -----Note----- In the first example, there is only one non-empty subset {1} with cost 1^1 = 1. In the second example, there are seven non-empty subsets. - {1} with cost 1^2 = 1 - {2} with cost 1^2 = 1 - {1, 2} with cost 2^2 = 4 - {3} with cost 1^2 = 1 - {1, 3} with cost 2^2 = 4 - {2, 3} with cost 2^2 = 4 - {1, 2, 3} with cost 3^2 = 9 The total cost is 1 + 1 + 4 + 1 + 4 + 4 + 9 = 24. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, k = list(map(int, input().split())) m = 0x3b9aca07 v = 500000004 r = 0 p = pow(2, n, m) a = [1] + [0] * k for i in range(k): for j in range(i, -1, -1): a[j + 1] += a[j] a[j] = a[j] * j % m for i in range(k + 1): r = (r + p * a[i]) % m p = p * v * (n - i) % m print(r) ```
vfc_23839
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/932/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1 1\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 2\n", "output": "24\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 3\n", "output": "800\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3784
Solve the following coding problem using the programming language python: A never-ending, fast-changing and dream-like world unfolds, as the secret door opens. A world is an unordered graph G, in whose vertex set V(G) there are two special vertices s(G) and t(G). An initial world has vertex set {s(G), t(G)} and an edge between them. A total of n changes took place in an initial world. In each change, a new vertex w is added into V(G), an existing edge (u, v) is chosen, and two edges (u, w) and (v, w) are added into E(G). Note that it's possible that some edges are chosen in more than one change. It's known that the capacity of the minimum s-t cut of the resulting graph is m, that is, at least m edges need to be removed in order to make s(G) and t(G) disconnected. Count the number of non-similar worlds that can be built under the constraints, modulo 10^9 + 7. We define two worlds similar, if they are isomorphic and there is isomorphism in which the s and t vertices are not relabelled. Formally, two worlds G and H are considered similar, if there is a bijection between their vertex sets $f : V(G) \rightarrow V(H)$, such that: f(s(G)) = s(H); f(t(G)) = t(H); Two vertices u and v of G are adjacent in G if and only if f(u) and f(v) are adjacent in H. -----Input----- The first and only line of input contains two space-separated integers n, m (1 ≤ n, m ≤ 50) — the number of operations performed and the minimum cut, respectively. -----Output----- Output one integer — the number of non-similar worlds that can be built, modulo 10^9 + 7. -----Examples----- Input 3 2 Output 6 Input 4 4 Output 3 Input 7 3 Output 1196 Input 31 8 Output 64921457 -----Note----- In the first example, the following 6 worlds are pairwise non-similar and satisfy the constraints, with s(G) marked in green, t(G) marked in blue, and one of their minimum cuts in light blue. [Image] In the second example, the following 3 worlds satisfy the constraints. [Image] The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python mod = int(1e9 + 7) n, m = map(int, input().split()) f = [ [0 for i in range(60)] for j in range(60) ] g = [ [0 for i in range(60)] for j in range(60) ] s = [ [0 for i in range(60)] for j in range(60) ] inv = [ 1 ] f[0][0] = s[0][0] = 1 def pow(x, exp) : res = 1 for i in range(0, 31) : if exp & 1 : res = res * x % mod exp >>= 1 if exp == 0 : break x = x * x % mod return res for i in range(1, n + 1) : inv.append( pow(i, mod - 2) ) for node in range(1, n + 1) : for cut in range(1, n + 1) : tmp = 0 for ln in range(node) : for lc in range(cut - 1, n + 1) : if f[ln][lc] == 0 : continue if lc == cut - 1 : tmp = ( tmp + f[ln][lc] * s[node - ln - 1][cut - 1] ) % mod else : tmp = ( tmp + f[ln][lc] * f[node - ln - 1][cut - 1] ) % mod cnt = 1 if tmp != 0 : cn, cc = 0, 0 for i in range(1, n + 1) : cn += node cc += cut cnt = cnt * (tmp + i - 1) % mod * inv[i] % mod if cn > n or cc > n : break for j in range(n - cn, -1, -1) : for k in range(n - cc, -1, -1) : if f[j][k] == 0 : continue g[j + cn][k + cc] += f[j][k] * cnt g[j + cn][k + cc] %= mod for i in range(n + 1) : for j in range(n + 1) : f[i][j] = (f[i][j] + g[i][j]) % mod g[i][j] = 0 for cut in range(n, -1, -1) : s[node][cut] = ( s[node][cut + 1] + f[node][cut] ) % mod print(f[n][m - 1]) ```
vfc_23843
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/848/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 2\n", "output": "6\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 4\n", "output": "3\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3785
Solve the following coding problem using the programming language python: Pavel loves grid mazes. A grid maze is an n × m rectangle maze where each cell is either empty, or is a wall. You can go from one cell to another only if both cells are empty and have a common side. Pavel drew a grid maze with all empty cells forming a connected area. That is, you can go from any empty cell to any other one. Pavel doesn't like it when his maze has too little walls. He wants to turn exactly k empty cells into walls so that all the remaining cells still formed a connected area. Help him. -----Input----- The first line contains three integers n, m, k (1 ≤ n, m ≤ 500, 0 ≤ k < s), where n and m are the maze's height and width, correspondingly, k is the number of walls Pavel wants to add and letter s represents the number of empty cells in the original maze. Each of the next n lines contains m characters. They describe the original maze. If a character on a line equals ".", then the corresponding cell is empty and if the character equals "#", then the cell is a wall. -----Output----- Print n lines containing m characters each: the new maze that fits Pavel's requirements. Mark the empty cells that you transformed into walls as "X", the other cells must be left without changes (that is, "." and "#"). It is guaranteed that a solution exists. If there are multiple solutions you can output any of them. -----Examples----- Input 3 4 2 #..# ..#. #... Output #.X# X.#. #... Input 5 4 5 #... #.#. .#.. ...# .#.# Output #XXX #X#. X#.. ...# .#.# The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys n, m, k = (int(x) for x in sys.stdin.readline().split(' ')) maze = [] for i in range(n): maze.append(list(sys.stdin.readline().strip())) for y in range(n): for x in range(m): if maze[y][x] == '.': x0, y0 = x, y break else: continue break else: print('no spare room') return stack = [] stack.append((x0,y0)) while stack: x, y = stack[-1] if maze[y][x] == '.': maze[y][x] = '0' if x > 0 and maze[y][x-1] == '.': stack.append((x-1, y)) elif maze[y][x] == '0': maze[y][x] = '1' if y < n-1 and maze[y+1][x] == '.': stack.append((x, y+1)) elif maze[y][x] == '1': maze[y][x] = '2' if x < m-1 and maze[y][x+1] == '.': stack.append((x+1, y)) elif maze[y][x] == '2': maze[y][x] = '3' if y > 0 and maze[y-1][x] == '.': stack.append((x, y-1)) elif maze[y][x] == '3': if k > 0: maze[y][x] = 'X' k -= 1 stack.pop() for y in range(n): for x in range(m): maze[y][x] = '.' if maze[y][x] == '3' else maze[y][x] print("\n".join(["".join(s) for s in maze])) ```
vfc_23847
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/377/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 4 2\n#..#\n..#.\n#...\n", "output": "#.X#\nX.#.\n#...\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 4 5\n#...\n#.#.\n.#..\n...#\n.#.#\n", "output": "#XXX\n#X#.\nX#..\n...#\n.#.#\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 3 2\n...\n.#.\n...\n", "output": "X..\nX#.\n...\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 3 2\n#.#\n...\n#.#\n", "output": "#X#\nX..\n#.#\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7 7 18\n#.....#\n..#.#..\n.#...#.\n...#...\n.#...#.\n..#.#..\n#.....#\n", "output": "#XXXXX#\nXX#X#X.\nX#XXX#.\nXXX#...\nX#...#.\nX.#.#..\n#.....#\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3786
Solve the following coding problem using the programming language python: In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i > 1) is situated at the top of branch, which bottom is p_{i}-th inflorescence and p_{i} < i. Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to p_{a}-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time. Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest. -----Input----- First line of input contains single integer number n (2 ≤ n ≤ 100 000)  — number of inflorescences. Second line of input contains sequence of n - 1 integer numbers p_2, p_3, ..., p_{n} (1 ≤ p_{i} < i), where p_{i} is number of inflorescence into which the apple from i-th inflorescence rolls down. -----Output----- Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest. -----Examples----- Input 3 1 1 Output 1 Input 5 1 2 2 2 Output 3 Input 18 1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4 Output 4 -----Note----- In first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them. In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it. 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()) p = [-1, 0] + [int(x) for x in input().split()] h = [0] * (n+1) count = [0] * n count[0] = 1 max_h = 0 for i in range(2, n+1): h[i] = h[p[i]]+1 count[h[i]]+=1 max_h = max(max_h,h[i]) ans = 0 for i in range(max_h+1): ans += count[i]%2 print(ans) ```
vfc_23851
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/930/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n1 1\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n1 2 2 2\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "18\n1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n1\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n1 2\n", "output": "3\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3788
Solve the following coding problem using the programming language python: Dima the hamster enjoys nibbling different things: cages, sticks, bad problemsetters and even trees! Recently he found a binary search tree and instinctively nibbled all of its edges, hence messing up the vertices. Dima knows that if Andrew, who has been thoroughly assembling the tree for a long time, comes home and sees his creation demolished, he'll get extremely upset. To not let that happen, Dima has to recover the binary search tree. Luckily, he noticed that any two vertices connected by a direct edge had their greatest common divisor value exceed $1$. Help Dima construct such a binary search tree or determine that it's impossible. The definition and properties of a binary search tree can be found here. -----Input----- The first line contains the number of vertices $n$ ($2 \le n \le 700$). The second line features $n$ distinct integers $a_i$ ($2 \le a_i \le 10^9$) — the values of vertices in ascending order. -----Output----- If it is possible to reassemble the binary search tree, such that the greatest common divisor of any two vertices connected by the edge is greater than $1$, print "Yes" (quotes for clarity). Otherwise, print "No" (quotes for clarity). -----Examples----- Input 6 3 6 9 18 36 108 Output Yes Input 2 7 17 Output No Input 9 4 8 10 12 15 18 33 44 81 Output Yes -----Note----- The picture below illustrates one of the possible trees for the first example. [Image] The picture below illustrates one of the possible trees for the third example. [Image] 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 gcd n=int(stdin.readline()) a=[int(x) for x in stdin.readline().split()] c = [] ld=[] rd=[] def check(l, r, e): if r == l: return c[l][e] > 0 if e < l and ld[l][r-l] != 0: return ld[l][r-l] == 1 elif e > r and rd[l][r-l] != 0: return rd[l][r-l] == 1 for i in range(l, r+1): if c[i][e]>0: if i==l or check(l, i-1, i): if i==r or check(i+1, r, i): if e < l: ld[l][r-l] = 1 else: rd[l][r-l] = 1 return True if e < l: ld[l][r - l] = -1 else: rd[l][r - l] = -1 return False for i in range(n): c.append([0]*n) ld.append([0]*n) rd.append([0] * n) for i in range(n): for j in range(i+1,n): if gcd(a[i],a[j]) > 1: c[i][j] = c[j][i] = 1 ans=False for i in range(n): if i == 0 or check(0, i - 1, i): if i == n-1 or check(i + 1, n-1, i): ans = True break if ans: print("Yes") else: print("No") ```
vfc_23859
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1025/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6\n3 6 9 18 36 108\n", "output": "Yes\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n7 17\n", "output": "No\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "9\n4 8 10 12 15 18 33 44 81\n", "output": "Yes\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "22\n2 4 8 16 32 96 288 576 1728 3456 10368 20736 62208 186624 559872 1119744 3359232 10077696 30233088 90699264 181398528 544195584\n", "output": "Yes\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3791
Solve the following coding problem using the programming language python: Some time ago Mister B detected a strange signal from the space, which he started to study. After some transformation the signal turned out to be a permutation p of length n or its cyclic shift. For the further investigation Mister B need some basis, that's why he decided to choose cyclic shift of this permutation which has the minimum possible deviation. Let's define the deviation of a permutation p as $\sum_{i = 1}^{i = n}|p [ i ] - i|$. Find a cyclic shift of permutation p with minimum possible deviation. If there are multiple solutions, print any of them. Let's denote id k (0 ≤ k < n) of a cyclic shift of permutation p as the number of right shifts needed to reach this shift, for example: k = 0: shift p_1, p_2, ... p_{n}, k = 1: shift p_{n}, p_1, ... p_{n} - 1, ..., k = n - 1: shift p_2, p_3, ... p_{n}, p_1. -----Input----- First line contains single integer n (2 ≤ n ≤ 10^6) — the length of the permutation. The second line contains n space-separated integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the elements of the permutation. It is guaranteed that all elements are distinct. -----Output----- Print two integers: the minimum deviation of cyclic shifts of permutation p and the id of such shift. If there are multiple solutions, print any of them. -----Examples----- Input 3 1 2 3 Output 0 0 Input 3 2 3 1 Output 0 1 Input 3 3 2 1 Output 2 1 -----Note----- In the first sample test the given permutation p is the identity permutation, that's why its deviation equals to 0, the shift id equals to 0 as well. In the second sample test the deviation of p equals to 4, the deviation of the 1-st cyclic shift (1, 2, 3) equals to 0, the deviation of the 2-nd cyclic shift (3, 1, 2) equals to 4, the optimal is the 1-st cyclic shift. In the third sample test the deviation of p equals to 4, the deviation of the 1-st cyclic shift (1, 3, 2) equals to 2, the deviation of the 2-nd cyclic shift (2, 1, 3) also equals to 2, so the optimal are both 1-st and 2-nd cyclic shifts. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def main(): n=int(input()) A=list(map(int,input().strip().split(' '))) def brutal(A): n=len(A) for i in range(n): temp=0 pos=0 neg=0 for j in range(n): temp+=abs(A[j]-(j+i)%n) if A[j]-(j+i)%n>0: pos+=1 else: neg+=1 print(temp,i,pos,neg,'ans,shift,+ve,-ve') for i in range(len(A)): A[i]-=1 ans=0 pos=0 neg=0 change=[0 for i in range(len(A))] for i in range(len(A)): ans+=abs(A[i]-i) if A[i]-i>0: pos+=1 else: neg+=1 if A[i]-i>0: change[i]=A[i]-i elif A[i]==i: change[i]=0 else: if A[i]!=0: change[i]=A[i]+n-i else: change[i]=0 MIN=ans index=0 #print(ans) collect=[[] for i in range(n)] for x in range(len(change)): collect[change[x]]+=[x] #print(collect) #print(ans,pos,neg) for s in range(1,n): ans-=abs(A[n-s]-n+1) ans+=abs(A[n-s]-0) neg-=1 ans-=pos ans+=neg if A[n-s]>0: pos+=1 else: neg+=1 pos-=len(collect[s]) neg+=len(collect[s]) #print(ans,pos,neg) if ans<MIN: MIN=ans index=s print(MIN,index) #brutal(A) main() ```
vfc_23871
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/819/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n1 2 3\n", "output": "0 0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n2 3 1\n", "output": "0 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n3 2 1\n", "output": "2 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n1 2\n", "output": "0 0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3792
Solve the following coding problem using the programming language python: Recently, the Fair Nut has written $k$ strings of length $n$, consisting of letters "a" and "b". He calculated $c$ — the number of strings that are prefixes of at least one of the written strings. Every string was counted only one time. Then, he lost his sheet with strings. He remembers that all written strings were lexicographically not smaller than string $s$ and not bigger than string $t$. He is interested: what is the maximum value of $c$ that he could get. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq n \leq 5 \cdot 10^5$, $1 \leq k \leq 10^9$). The second line contains a string $s$ ($|s| = n$) — the string consisting of letters "a" and "b. The third line contains a string $t$ ($|t| = n$) — the string consisting of letters "a" and "b. It is guaranteed that string $s$ is lexicographically not bigger than $t$. -----Output----- Print one number — maximal value of $c$. -----Examples----- Input 2 4 aa bb Output 6 Input 3 3 aba bba Output 8 Input 4 5 abbb baaa Output 8 -----Note----- In the first example, Nut could write strings "aa", "ab", "ba", "bb". These $4$ strings are prefixes of at least one of the written strings, as well as "a" and "b". Totally, $6$ strings. In the second example, Nut could write strings "aba", "baa", "bba". In the third example, there are only two different strings that Nut could write. If both of them are written, $c=8$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def ii(): return int(input()) def mi(): return list(map(int, input().split())) def li(): return list(mi()) # B. The Fair Nut and Strings n, k = mi() s = input().strip() t = input().strip() ans = 0 jj = 0 for i in range(n): if s[i] == t[i]: ans += 1 jj = i + 1 else: break cur = 2 for j in range(jj, n): if s[j] == 'b': cur -= 1 if t[j] == 'a': cur -= 1 if cur >= k: ans += k * (n - j) break ans += cur cur *= 2 print(ans) ```
vfc_23875
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1083/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2 4\naa\nbb\n", "output": "6\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 3\naba\nbba\n", "output": "8\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 5\nabbb\nbaaa\n", "output": "8\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1\na\na\n", "output": "1", "type": "stdin_stdout" } ] }
apps
verifiable_code
3794
Solve the following coding problem using the programming language python: You are given an array of $n$ integers. You need to split all integers into two groups so that the GCD of all integers in the first group is equal to one and the GCD of all integers in the second group is equal to one. The GCD of a group of integers is the largest non-negative integer that divides all the integers in the group. Both groups have to be non-empty. -----Input----- The first line contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array. -----Output----- In the first line print "YES" (without quotes), if it is possible to split the integers into two groups as required, and "NO" (without quotes) otherwise. If it is possible to split the integers, in the second line print $n$ integers, where the $i$-th integer is equal to $1$ if the integer $a_i$ should be in the first group, and $2$ otherwise. If there are multiple solutions, print any. -----Examples----- Input 4 2 3 6 7 Output YES 2 2 1 1 Input 5 6 15 35 77 22 Output YES 2 1 2 1 1 Input 5 6 10 15 1000 75 Output NO The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys def gcd(l): if len(l)==0: return 0 if len(l)==1: return l[0] if len(l)==2: if l[1]==0: return l[0] return gcd([l[1],l[0]%l[1]]) return gcd([gcd(l[:-1]),l[-1]]) def brute_force(l1,l2,l,sol): if len(l)==0: g1=gcd(l1) g2=gcd(l2) return g1==1 and g2==1,sol res,s=brute_force(l1+[l[0]],l2,l[1:],sol+[1]) if res: return True,s return brute_force(l1,l2+[l[0]],l[1:],sol+[2]) def factor(n): res=[] i=2 while i*i<=n: if n%i==0: res.append(i) while n%i==0: n=int(n/i) i+=1 if n!=1: res.append(n) return res def dumpsol(sol): for v in sol: print(v,end=' ') n=int(sys.stdin.readline()) l=sys.stdin.readline().strip().split(" ")[0:n] l=[int(x) for x in l] if n<12: ret,sol=brute_force([],[],l,[]) if ret: print("YES") dumpsol(sol) else: print("NO") return factors={} for i in range(10): for key in factor(l[i]): factors[key]=0 flists={} for f in factors: flists[f]=[] pos=0 found=False for v in l: if v%f!=0: found=True factors[f]+=1 flists[f].append(pos) if (factors[f]>9): break pos+=1 if not found: print("NO") return oftf=[] isoftf={} for f in factors: if factors[f]==0: print("NO") return if factors[f]<10: oftf.append(f) isoftf[f]=1 #print(oftf) sol=[1 for i in range(len(l))] x=l[0] sol[0]=2 oxf=factor(x) #print(oxf) xf=[] nxf=0 isxoftf={} for f in oxf: if f in isoftf: nxf+=1 isxoftf[f]=1 xf.append(f) else: sol[flists[f][0]]=2 nonxf=[] for f in oftf: if not f in isxoftf: nonxf.append(f) masks={} pos=0 #print(xf) #print(nonxf) for f in xf+nonxf: for v in flists[f]: if not v in masks: masks[v]=0 masks[v]|=1<<pos pos+=1 vals=[{} for i in range(len(masks)+1)] vals[0][0]=0 pos=0 mlist=[] for mask in masks: mlist.append(mask) cmask=masks[mask] cmask1=cmask<<10 #print(vals) for v in vals[pos]: vals[pos+1][v|cmask]=v # first number is always in group2 if (mask!=0): vals[pos+1][v|cmask1]=v pos+=1 #print(vals) #print(masks) #print(sol) test_val=((1<<len(xf))-1)|(((1<<len(oftf))-1)<<10) #print(test_val) for v in vals[pos]: if (v&test_val)==test_val: print("YES") #print(pos) while (pos!=0): #print(v) #print(vals[pos]) nv=vals[pos][v] #print(nv) if (nv^v<1024 and nv^v!=0): sol[mlist[pos-1]]=2 v=nv pos-=1 dumpsol(sol) return print("NO") #print(oftf) #print(masks) ```
vfc_23883
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1198/F", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n2 3 6 7\n", "output": "YES\n2 2 1 1 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n6 15 35 77 22\n", "output": "YES\n2 1 2 1 1 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n6 10 15 1000 75\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n8 1 6\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n84 33 80 6\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n1116 1968 609 9549 7067\n", "output": "NO\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3795
Solve the following coding problem using the programming language python: Andrew was very excited to participate in Olympiad of Metropolises. Days flew by quickly, and Andrew is already at the airport, ready to go home. He has $n$ rubles left, and would like to exchange them to euro and dollar bills. Andrew can mix dollar bills and euro bills in whatever way he wants. The price of one dollar is $d$ rubles, and one euro costs $e$ rubles. Recall that there exist the following dollar bills: $1$, $2$, $5$, $10$, $20$, $50$, $100$, and the following euro bills — $5$, $10$, $20$, $50$, $100$, $200$ (note that, in this problem we do not consider the $500$ euro bill, it is hard to find such bills in the currency exchange points). Andrew can buy any combination of bills, and his goal is to minimize the total number of rubles he will have after the exchange. Help him — write a program that given integers $n$, $e$ and $d$, finds the minimum number of rubles Andrew can get after buying dollar and euro bills. -----Input----- The first line of the input contains one integer $n$ ($1 \leq n \leq 10^8$) — the initial sum in rubles Andrew has. The second line of the input contains one integer $d$ ($30 \leq d \leq 100$) — the price of one dollar in rubles. The third line of the input contains integer $e$ ($30 \leq e \leq 100$) — the price of one euro in rubles. -----Output----- Output one integer — the minimum number of rubles Andrew can have after buying dollar and euro bills optimally. -----Examples----- Input 100 60 70 Output 40 Input 410 55 70 Output 5 Input 600 60 70 Output 0 -----Note----- In the first example, we can buy just $1$ dollar because there is no $1$ euro bill. In the second example, optimal exchange is to buy $5$ euro and $1$ dollar. In the third example, optimal exchange is to buy $10$ dollars in one bill. 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()) d = int(input()) e = 5*int(input()) ans = n d, e = min([d, e]), max([d, e]) for i in range(e): if (n-i*d) >= 0: ans = min([ans, (n-i*d)%e]) print(ans) ```
vfc_23887
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1214/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "100\n60\n70\n", "output": "40\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "410\n55\n70\n", "output": "5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "600\n60\n70\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "50\n60\n70\n", "output": "50\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "420\n70\n65\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1750\n45\n50\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3800
Solve the following coding problem using the programming language python: You have a string of decimal digits s. Let's define b_{ij} = s_{i}·s_{j}. Find in matrix b the number of such rectangles that the sum b_{ij} for all cells (i, j) that are the elements of the rectangle equals a in each rectangle. A rectangle in a matrix is a group of four integers (x, y, z, t) (x ≤ y, z ≤ t). The elements of the rectangle are all cells (i, j) such that x ≤ i ≤ y, z ≤ j ≤ t. -----Input----- The first line contains integer a (0 ≤ a ≤ 10^9), the second line contains a string of decimal integers s (1 ≤ |s| ≤ 4000). -----Output----- Print a single integer — the answer to a problem. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 10 12345 Output 6 Input 16 439873893693495623498263984765 Output 40 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def f(t, k): i, j = 0, 1 s, d = 0, t[0] n = len(t) while j <= n: if d > k: d -= t[i] i += 1 elif d == k: if t[i] and (j == n or t[j]): s += 1 else: a, b = i - 1, j - 1 while j < n and t[j] == 0: j += 1 while t[i] == 0: i += 1 s += (i - a) * (j - b) if j < n: d += t[j] d -= t[i] i += 1 j += 1 else: if j < n: d += t[j] j += 1 return s s, n = 0, int(input()) t = list(map(int, input())) if n: k = sum(t) if k == 0: print(0) else: p = [(i, n // i) for i in range(max(1, n // k), int(n ** 0.5) + 1) if n % i == 0] for a, b in p: if a != b: s += 2 * f(t, a) * f(t, b) else: k = f(t, a) s += k * k print(s) else: n = len(t) m = n * (n + 1) s = j = 0 while j < n: if t[j] == 0: i = j j += 1 while j < n and t[j] == 0: j += 1 k = ((j - i) * (j - i + 1)) // 2 s += k j += 1 print((m - s) * s) ```
vfc_23907
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/364/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "10\n12345\n", "output": "6\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3801
Solve the following coding problem using the programming language python: The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes $n$ pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The $i$-th picture has a non-negative weight $w_i$, and the probability of the $i$-th picture being displayed is $\frac{w_i}{\sum_{j=1}^nw_j}$. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add $1$ to its weight; otherwise, she would subtract $1$ from its weight. Nauuo will visit the website $m$ times. She wants to know the expected weight of each picture after all the $m$ visits modulo $998244353$. Can you help her? The expected weight of the $i$-th picture can be denoted by $\frac {q_i} {p_i}$ where $\gcd(p_i,q_i)=1$, you need to print an integer $r_i$ satisfying $0\le r_i<998244353$ and $r_i\cdot p_i\equiv q_i\pmod{998244353}$. It can be proved that such $r_i$ exists and is unique. -----Input----- The first line contains two integers $n$ and $m$ ($1\le n\le 50$, $1\le m\le 50$) — the number of pictures and the number of visits to the website. The second line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($a_i$ is either $0$ or $1$) — if $a_i=0$ , Nauuo does not like the $i$-th picture; otherwise Nauuo likes the $i$-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains $n$ integers $w_1,w_2,\ldots,w_n$ ($1\le w_i\le50$) — the initial weights of the pictures. -----Output----- The output contains $n$ integers $r_1,r_2,\ldots,r_n$ — the expected weights modulo $998244353$. -----Examples----- Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 -----Note----- In the first example, if the only visit shows the first picture with a probability of $\frac 2 3$, the final weights are $(1,1)$; if the only visit shows the second picture with a probability of $\frac1 3$, the final weights are $(2,2)$. So, both expected weights are $\frac2 3\cdot 1+\frac 1 3\cdot 2=\frac4 3$ . Because $332748119\cdot 3\equiv 4\pmod{998244353}$, you need to print $332748119$ instead of $\frac4 3$ or $1.3333333333$. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, $w_1$ will be increased by $1$. So, the expected weight is $1+2=3$. Nauuo is very naughty so she didn't give you any hint of the third example. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python P = 998244353 N, M = list(map(int, input().split())) A = [int(a) for a in input().split()] B = [int(a) for a in input().split()] li = sum([A[i]*B[i] for i in range(N)]) di = sum([(A[i]^1)*B[i] for i in range(N)]) X = [[] for _ in range(M+1)] X[0] = [1] def calc(L): su = sum(L) pl = 0 pd = 0 RE = [] for i in range(len(L)): a = li + i b = di - (len(L) - 1 - i) pd = b * L[i] * pow(su*(a+b), P-2, P) RE.append((pl+pd)%P) pl = a * L[i] * pow(su*(a+b), P-2, P) RE.append(pl%P) return RE for i in range(M): X[i+1] = calc(X[i]) ne = 0 po = 0 for i in range(M+1): po = (po + X[M][i] * (li + i)) % P ne = (ne + X[M][i] * (di - M + i)) % P for i in range(N): print(po * B[i] * pow(li, P-2, P) % P if A[i] else ne * B[i] * pow(di, P-2, P) % P) ```
vfc_23911
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1172/C1", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2 1\n0 1\n2 1\n", "output": "332748119\n332748119\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 2\n1\n1\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 3\n0 1 1\n4 3 5\n", "output": "160955686\n185138929\n974061117\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3803
Solve the following coding problem using the programming language python: A monster is attacking the Cyberland! Master Yang, a braver, is going to beat the monster. Yang and the monster each have 3 attributes: hitpoints (HP), offensive power (ATK) and defensive power (DEF). During the battle, every second the monster's HP decrease by max(0, ATK_{Y} - DEF_{M}), while Yang's HP decreases by max(0, ATK_{M} - DEF_{Y}), where index Y denotes Master Yang and index M denotes monster. Both decreases happen simultaneously Once monster's HP ≤ 0 and the same time Master Yang's HP > 0, Master Yang wins. Master Yang can buy attributes from the magic shop of Cyberland: h bitcoins per HP, a bitcoins per ATK, and d bitcoins per DEF. Now Master Yang wants to know the minimum number of bitcoins he can spend in order to win. -----Input----- The first line contains three integers HP_{Y}, ATK_{Y}, DEF_{Y}, separated by a space, denoting the initial HP, ATK and DEF of Master Yang. The second line contains three integers HP_{M}, ATK_{M}, DEF_{M}, separated by a space, denoting the HP, ATK and DEF of the monster. The third line contains three integers h, a, d, separated by a space, denoting the price of 1 HP, 1 ATK and 1 DEF. All numbers in input are integer and lie between 1 and 100 inclusively. -----Output----- The only output line should contain an integer, denoting the minimum bitcoins Master Yang should spend in order to win. -----Examples----- Input 1 2 1 1 100 1 1 100 100 Output 99 Input 100 100 100 1 1 1 1 1 1 Output 0 -----Note----- For the first sample, prices for ATK and DEF are extremely high. Master Yang can buy 99 HP, then he can beat the monster with 1 HP left. For the second sample, Master Yang is strong enough to beat the monster, so he doesn't need to buy anything. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python H_y,A_y,D_y = list(map(int,input().split())) H_m,A_m,D_m = list(map(int,input().split())) h,a,d = list(map(int,input().split())) ans = 10**20 for A_buy in range(max(0,H_m+D_m-A_y)+1): for D_buy in range(max(0,A_m-D_y)+1): damage = A_y + A_buy - D_m cost = A_buy * a + D_buy * d if damage > 0 and cost < ans: time = (H_m+damage-1)//damage H_left = H_y - time * max(0, A_m - D_y - D_buy) if H_left <= 0: cost += h * (1-H_left) if cost < ans: ans = cost print(ans) ```
vfc_23919
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/487/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1 2 1\n1 100 1\n1 100 100\n", "output": "99\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3804
Solve the following coding problem using the programming language python: Maxim loves to fill in a matrix in a special manner. Here is a pseudocode of filling in a matrix of size (m + 1) × (m + 1): [Image] Maxim asks you to count, how many numbers m (1 ≤ m ≤ n) are there, such that the sum of values in the cells in the row number m + 1 of the resulting matrix equals t. Expression (x xor y) means applying the operation of bitwise excluding "OR" to numbers x and y. The given operation exists in all modern programming languages. For example, in languages C++ and Java it is represented by character "^", in Pascal — by "xor". -----Input----- A single line contains two integers n and t (1 ≤ n, t ≤ 10^12, t ≤ n + 1). Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Output----- In a single line print a single integer — the answer to the problem. -----Examples----- Input 1 1 Output 1 Input 3 2 Output 1 Input 3 3 Output 0 Input 1000000000000 1048576 Output 118606527258 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, t = map(int, input().split()) s = bin(n + 2)[2:] l = len(s) if t & (t - 1): ans = 0 else: t = t.bit_length() f = [[0] * (l + 1) for i in range(l + 1)] for i in range(l + 1): f[i][0] = f[i][i] = 1 for j in range(1, i): f[i][j] = f[i - 1][j - 1] + f[i - 1][j] ans = c = 0 for i in range(l): if s[i] == '1': if t - c <= l - i - 1: ans += f[l - i - 1][t - c] c += 1 if t == 1: ans -= 1 print(ans) ```
vfc_23923
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/261/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1 1\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 2\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 3\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3805
Solve the following coding problem using the programming language python: Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. -----Input----- The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. -----Output----- Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. -----Examples----- Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No -----Note----- The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: [Image] In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: [Image] In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: [Image] The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python l = [] for c in input(): if len(l) > 0 and l[-1] == c: l.pop() else: l.append(c) print('Yes' if len(l) == 0 else 'No') ```
vfc_23927
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/343/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "-++-\n", "output": "Yes\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "+-\n", "output": "No\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "++\n", "output": "Yes\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "-\n", "output": "No\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3807
Solve the following coding problem using the programming language python: Limak is a little polar bear. He plays by building towers from blocks. Every block is a cube with positive integer length of side. Limak has infinitely many blocks of each side length. A block with side a has volume a^3. A tower consisting of blocks with sides a_1, a_2, ..., a_{k} has the total volume a_1^3 + a_2^3 + ... + a_{k}^3. Limak is going to build a tower. First, he asks you to tell him a positive integer X — the required total volume of the tower. Then, Limak adds new blocks greedily, one by one. Each time he adds the biggest block such that the total volume doesn't exceed X. Limak asks you to choose X not greater than m. Also, he wants to maximize the number of blocks in the tower at the end (however, he still behaves greedily). Secondarily, he wants to maximize X. Can you help Limak? Find the maximum number of blocks his tower can have and the maximum X ≤ m that results this number of blocks. -----Input----- The only line of the input contains one integer m (1 ≤ m ≤ 10^15), meaning that Limak wants you to choose X between 1 and m, inclusive. -----Output----- Print two integers — the maximum number of blocks in the tower and the maximum required total volume X, resulting in the maximum number of blocks. -----Examples----- Input 48 Output 9 42 Input 6 Output 6 6 -----Note----- In the first sample test, there will be 9 blocks if you choose X = 23 or X = 42. Limak wants to maximize X secondarily so you should choose 42. In more detail, after choosing X = 42 the process of building a tower is: Limak takes a block with side 3 because it's the biggest block with volume not greater than 42. The remaining volume is 42 - 27 = 15. The second added block has side 2, so the remaining volume is 15 - 8 = 7. Finally, Limak adds 7 blocks with side 1, one by one. So, there are 9 blocks in the tower. The total volume is is 3^3 + 2^3 + 7·1^3 = 27 + 8 + 7 = 42. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python im = int(input()) best_steps = 0 best_length = 0 def rec(m, steps, substracted): nonlocal best_steps, best_length if m == 0: if steps > best_steps: best_steps = steps best_length = substracted elif steps == best_steps: best_length = max(best_length, substracted) return a = 1 while (a + 1)**3 <= m: a += 1 rec(m - a**3, steps + 1, substracted + a**3) if a - 1 != 0: rec(a**3-1-(a-1)**3, steps + 1, substracted + (a-1)**3) rec(im, 0, 0) print(best_steps, best_length) ```
vfc_23935
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/679/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "48\n", "output": "9 42\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n", "output": "6 6\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n", "output": "1 1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3808
Solve the following coding problem using the programming language python: Petya's friends made him a birthday present — a bracket sequence. Petya was quite disappointed with his gift, because he dreamed of correct bracket sequence, yet he told his friends nothing about his dreams and decided to fix present himself. To make everything right, Petya is going to move at most one bracket from its original place in the sequence to any other position. Reversing the bracket (e.g. turning "(" into ")" or vice versa) isn't allowed. We remind that bracket sequence $s$ is called correct if: $s$ is empty; $s$ is equal to "($t$)", where $t$ is correct bracket sequence; $s$ is equal to $t_1 t_2$, i.e. concatenation of $t_1$ and $t_2$, where $t_1$ and $t_2$ are correct bracket sequences. For example, "(()())", "()" are correct, while ")(" and "())" are not. Help Petya to fix his birthday present and understand whether he can move one bracket so that the sequence becomes correct. -----Input----- First of line of input contains a single number $n$ ($1 \leq n \leq 200\,000$) — length of the sequence which Petya received for his birthday. Second line of the input contains bracket sequence of length $n$, containing symbols "(" and ")". -----Output----- Print "Yes" if Petya can make his sequence correct moving at most one bracket. Otherwise print "No". -----Examples----- Input 2 )( Output Yes Input 3 (() Output No Input 2 () Output Yes Input 10 )))))((((( Output No -----Note----- In the first example, Petya can move first bracket to the end, thus turning the sequence into "()", which is correct bracket sequence. In the second example, there is no way to move at most one bracket so that the sequence becomes correct. In the third example, the sequence is already correct and there's no need to move brackets. 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()) s=input() stack=[] for i in s: if i=="(": stack.append(i) elif i==")": if len(stack)>0: if (stack[-1]=="("): stack.pop() else: stack.append(")") else: stack.append(")") if len(stack)==0: print ("Yes") return if len(stack)==2: if stack[0]==")" and stack[1]=="(": print ("Yes") return else: print ("No") return else: print ("No") ```
vfc_23939
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1214/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\n)(\n", "output": "Yes\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n(()\n", "output": "No\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n()\n", "output": "Yes\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n)))))(((((\n", "output": "No\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3809
Solve the following coding problem using the programming language python: One day Greg and his friends were walking in the forest. Overall there were n people walking, including Greg. Soon he found himself in front of a river. The guys immediately decided to get across the river. Luckily, there was a boat by the river bank, just where the guys were standing. We know that the boat can hold people with the total weight of at most k kilograms. Greg immediately took a piece of paper and listed there the weights of all people in his group (including himself). It turned out that each person weights either 50 or 100 kilograms. Now Greg wants to know what minimum number of times the boat needs to cross the river to transport the whole group to the other bank. The boat needs at least one person to navigate it from one bank to the other. As the boat crosses the river, it can have any non-zero number of passengers as long as their total weight doesn't exceed k. Also Greg is wondering, how many ways there are to transport everybody to the other side in the minimum number of boat rides. Two ways are considered distinct if during some ride they have distinct sets of people on the boat. Help Greg with this problem. -----Input----- The first line contains two integers n, k (1 ≤ n ≤ 50, 1 ≤ k ≤ 5000) — the number of people, including Greg, and the boat's weight limit. The next line contains n integers — the people's weights. A person's weight is either 50 kilos or 100 kilos. You can consider Greg and his friends indexed in some way. -----Output----- In the first line print an integer — the minimum number of rides. If transporting everyone to the other bank is impossible, print an integer -1. In the second line print the remainder after dividing the number of ways to transport the people in the minimum number of rides by number 1000000007 (10^9 + 7). If transporting everyone to the other bank is impossible, print integer 0. -----Examples----- Input 1 50 50 Output 1 1 Input 3 100 50 50 100 Output 5 2 Input 2 50 50 50 Output -1 0 -----Note----- In the first test Greg walks alone and consequently, he needs only one ride across the river. In the second test you should follow the plan: transport two 50 kg. people; transport one 50 kg. person back; transport one 100 kg. person; transport one 50 kg. person back; transport two 50 kg. people. That totals to 5 rides. Depending on which person to choose at step 2, we can get two distinct ways. 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 n, k = [int(i) for i in input().split()] a = [int(i) for i in input().split()] c50 = sum([1 for i in a if i == 50]) c100 = sum([1 for i in a if i == 100]) c = [[0] * 51 for i in range(51)] c[0][0] = 1 c[1][0] = 1 c[1][1] = 1 for x in range(2, 51): for y in range(x + 1): c[x][y] = c[x - 1][y - 1] + c[x - 1][y] d = [[[[0, float('inf')] for l in range(2)] for i in range(c100 + 1)] for j in range(c50 + 1)] # d[i][j][c] ответ, когда мы переправили i по 50 кг и j по 100 кг и лодка на берегу c d[0][0][0][0] = 1 d[0][0][0][1] = 0 q = deque() q.append([0, 0, 0]) while len(q) > 0: i, j, shore = q.popleft() for fifty in range(c50 - i + 1 if shore == 0 else i + 1): for hundreds in range(c100 - j + 1 if shore == 0 else j + 1): if fifty * 50 + hundreds * 100 > k or fifty + hundreds == 0: continue i1 = i + fifty if shore == 0 else i - fifty j1 = j + hundreds if shore == 0 else j - hundreds if d[i1][j1][1 ^ shore][1] > d[i][j][shore][1] + 1: d[i1][j1][1 ^ shore][1] = d[i][j][shore][1] + 1 d[i1][j1][1 ^ shore][0] = 0 q.append((i1, j1, 1 ^ shore)) if d[i1][j1][1 ^ shore][1] < d[i][j][shore][1] + 1: continue koeff = (c[c50 - i][fifty] if shore == 0 else c[i][fifty]) * ( c[c100 - j][hundreds] if shore == 0 else c[j][hundreds]) d[i1][j1][1 ^ shore][0] += d[i][j][shore][0] * koeff d[i1][j1][1 ^ shore][0] %= 10 ** 9 + 7 if d[c50][c100][1][1] == float('inf'): print(-1) print(0) else: print(d[c50][c100][1][1]) print(d[c50][c100][1][0]) ```
vfc_23943
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/295/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1 50\n50\n", "output": "1\n1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 100\n50 50 100\n", "output": "5\n2\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3810
Solve the following coding problem using the programming language python: SIHanatsuka - EMber SIHanatsuka - ATONEMENT Back in time, the seven-year-old Nora used to play lots of games with her creation ROBO_Head-02, both to have fun and enhance his abilities. One day, Nora's adoptive father, Phoenix Wyle, brought Nora $n$ boxes of toys. Before unpacking, Nora decided to make a fun game for ROBO. She labelled all $n$ boxes with $n$ distinct integers $a_1, a_2, \ldots, a_n$ and asked ROBO to do the following action several (possibly zero) times: Pick three distinct indices $i$, $j$ and $k$, such that $a_i \mid a_j$ and $a_i \mid a_k$. In other words, $a_i$ divides both $a_j$ and $a_k$, that is $a_j \bmod a_i = 0$, $a_k \bmod a_i = 0$. After choosing, Nora will give the $k$-th box to ROBO, and he will place it on top of the box pile at his side. Initially, the pile is empty. After doing so, the box $k$ becomes unavailable for any further actions. Being amused after nine different tries of the game, Nora asked ROBO to calculate the number of possible different piles having the largest amount of boxes in them. Two piles are considered different if there exists a position where those two piles have different boxes. Since ROBO was still in his infant stages, and Nora was still too young to concentrate for a long time, both fell asleep before finding the final answer. Can you help them? As the number of such piles can be very large, you should print the answer modulo $10^9 + 7$. -----Input----- The first line contains an integer $n$ ($3 \le n \le 60$), denoting the number of boxes. The second line contains $n$ distinct integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 60$), where $a_i$ is the label of the $i$-th box. -----Output----- Print the number of distinct piles having the maximum number of boxes that ROBO_Head can have, modulo $10^9 + 7$. -----Examples----- Input 3 2 6 8 Output 2 Input 5 2 3 4 9 12 Output 4 Input 4 5 7 2 9 Output 1 -----Note----- Let's illustrate the box pile as a sequence $b$, with the pile's bottommost box being at the leftmost position. In the first example, there are $2$ distinct piles possible: $b = [6]$ ($[2, \mathbf{6}, 8] \xrightarrow{(1, 3, 2)} [2, 8]$) $b = [8]$ ($[2, 6, \mathbf{8}] \xrightarrow{(1, 2, 3)} [2, 6]$) In the second example, there are $4$ distinct piles possible: $b = [9, 12]$ ($[2, 3, 4, \mathbf{9}, 12] \xrightarrow{(2, 5, 4)} [2, 3, 4, \mathbf{12}] \xrightarrow{(1, 3, 4)} [2, 3, 4]$) $b = [4, 12]$ ($[2, 3, \mathbf{4}, 9, 12] \xrightarrow{(1, 5, 3)} [2, 3, 9, \mathbf{12}] \xrightarrow{(2, 3, 4)} [2, 3, 9]$) $b = [4, 9]$ ($[2, 3, \mathbf{4}, 9, 12] \xrightarrow{(1, 5, 3)} [2, 3, \mathbf{9}, 12] \xrightarrow{(2, 4, 3)} [2, 3, 12]$) $b = [9, 4]$ ($[2, 3, 4, \mathbf{9}, 12] \xrightarrow{(2, 5, 4)} [2, 3, \mathbf{4}, 12] \xrightarrow{(1, 4, 3)} [2, 3, 12]$) In the third sequence, ROBO can do nothing at all. Therefore, there is only $1$ valid pile, and that pile is empty. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python MOD = 1000000007 def isSubset(a, b): return (a & b) == a def isIntersect(a, b): return (a & b) != 0 # Solve for each weakly connected component (WCC) def cntOrder(s, t): p = len(s) m = len(t) inMask = [0 for i in range(m)] for x in range(p): for i in range(m): if t[i] % s[x] == 0: inMask[i] |= 1 << x cnt = [0 for mask in range(1<<p)] for mask in range(1<<p): for i in range(m): if isSubset(inMask[i], mask): cnt[mask] += 1 dp = [[0 for mask in range(1<<p)] for k in range(m+1)] for i in range(m): dp[1][inMask[i]] += 1 for k in range(m): for mask in range(1<<p): for i in range(m): if not isSubset(inMask[i], mask) and isIntersect(inMask[i], mask): dp[k+1][mask | inMask[i]] = (dp[k+1][mask | inMask[i]] + dp[k][mask]) % MOD dp[k+1][mask] = (dp[k+1][mask] + dp[k][mask] * (cnt[mask] - k)) % MOD return dp[m][(1<<p)-1] def dfs(u): nonlocal a, graph, degIn, visited, s, t visited[u] = True if degIn[u] == 0: s.append(a[u]) else: t.append(a[u]) for v in graph[u]: if not visited[v]: dfs(v) def main(): nonlocal a, graph, degIn, visited, s, t # Reading input n = int(input()) a = list(map(int, input().split())) # Pre-calculate C(n, k) c = [[0 for j in range(n)] for i in range(n)] for i in range(n): c[i][0] = 1 for j in range(1, i+1): c[i][j] = (c[i-1][j-1] + c[i-1][j]) % MOD # Building divisibility graph degIn = [0 for u in range(n)] graph = [[] for u in range(n)] for u in range(n): for v in range(n): if u != v and a[v] % a[u] == 0: graph[u].append(v) graph[v].append(u) degIn[v] += 1 # Solve for each WCC of divisibility graph and combine result ans = 1 curLen = 0 visited = [False for u in range(n)] for u in range(n): if not visited[u]: s = [] t = [] dfs(u) if len(t) > 0: sz = len(t) - 1 cnt = cntOrder(s, t) # Number of orders for current WCC ans = (ans * cnt) % MOD # Number of ways to insert <sz> number to array of <curLen> elements ans = (ans * c[curLen + sz][sz]) % MOD curLen += sz print(ans) def __starting_point(): main() __starting_point() ```
vfc_23947
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1292/F", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n2 6 8\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n2 3 4 9 12\n", "output": "4\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3811
Solve the following coding problem using the programming language python: During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 150\,000$) — the number of pairs. Each of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \le a_i, b_i \le 2 \cdot 10^9$). -----Output----- Print a single integer — the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $-1$. -----Examples----- Input 3 17 18 15 24 12 15 Output 6 Input 2 10 16 7 17 Output -1 Input 5 90 108 45 105 75 40 165 175 33 30 Output 5 -----Note----- In the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $1$ satisfying the conditions. In the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from math import sqrt def primset(n): a = set() while n % 2 == 0: a.add(2) n = n//2 for i in range(3,int(sqrt(n))+1,2): while n % i== 0: a.add(i) n = n//i if n > 2: a.add(n) return a n = int(input()) a, b = map(int, input().split()) primes = primset(a) primes.update(primset(b)) for i in range(1, n): a, b = map(int, input().split()) r = set() for p in primes: if a%p != 0 and b%p != 0: r.add(p) for rem in r: primes.remove(rem) if len(primes) < 1: print(-1) return print(primes.pop()) ```
vfc_23951
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1025/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n17 18\n15 24\n12 15\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n10 16\n7 17\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n90 108\n45 105\n75 40\n165 175\n33 30\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n10 9\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n2 2\n3 3\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n5 15\n125 3\n3 3\n", "output": "3\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3815
Solve the following coding problem using the programming language python: You are given two integers $a$ and $b$. Moreover, you are given a sequence $s_0, s_1, \dots, s_{n}$. All values in $s$ are integers $1$ or $-1$. It's known that sequence is $k$-periodic and $k$ divides $n+1$. In other words, for each $k \leq i \leq n$ it's satisfied that $s_{i} = s_{i - k}$. Find out the non-negative remainder of division of $\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i}$ by $10^{9} + 9$. Note that the modulo is unusual! -----Input----- The first line contains four integers $n, a, b$ and $k$ $(1 \leq n \leq 10^{9}, 1 \leq a, b \leq 10^{9}, 1 \leq k \leq 10^{5})$. The second line contains a sequence of length $k$ consisting of characters '+' and '-'. If the $i$-th character (0-indexed) is '+', then $s_{i} = 1$, otherwise $s_{i} = -1$. Note that only the first $k$ members of the sequence are given, the rest can be obtained using the periodicity property. -----Output----- Output a single integer — value of given expression modulo $10^{9} + 9$. -----Examples----- Input 2 2 3 3 +-+ Output 7 Input 4 1 5 1 - Output 999999228 -----Note----- In the first example: $(\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i})$ = $2^{2} 3^{0} - 2^{1} 3^{1} + 2^{0} 3^{2}$ = 7 In the second example: $(\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i}) = -1^{4} 5^{0} - 1^{3} 5^{1} - 1^{2} 5^{2} - 1^{1} 5^{3} - 1^{0} 5^{4} = -781 \equiv 999999228 \pmod{10^{9} + 9}$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def __starting_point(): a=[int(x) for x in input().split()] N=a[0] A=a[1] B=a[2] K=a[3] s=input() mod = 1000000009 Q = pow(B*pow(A,mod-2,mod)%mod,K,mod) if Q!=1: D = (pow(Q,(N+1)//K,mod)-1)*pow(Q-1,mod-2,mod)%mod else: D = (N+1)//K ans=0 C = pow(A,N,mod) A=pow(A,mod-2,mod) for i in range(K): if s[i]=='+': ans=(ans+C*D)%mod else: ans=(ans-C*D)%mod C=C*B*A%mod print((ans%mod+mod)%mod) __starting_point() ```
vfc_23967
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/963/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2 2 3 3\n+-+\n", "output": "7\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3816
Solve the following coding problem using the programming language python: You are given three sticks with positive integer lengths of a, b, and c centimeters. You can increase length of some of them by some positive integer number of centimeters (different sticks can be increased by a different length), but in total by at most l centimeters. In particular, it is allowed not to increase the length of any stick. Determine the number of ways to increase the lengths of some sticks so that you can form from them a non-degenerate (that is, having a positive area) triangle. Two ways are considered different, if the length of some stick is increased by different number of centimeters in them. -----Input----- The single line contains 4 integers a, b, c, l (1 ≤ a, b, c ≤ 3·10^5, 0 ≤ l ≤ 3·10^5). -----Output----- Print a single integer — the number of ways to increase the sizes of the sticks by the total of at most l centimeters, so that you can make a non-degenerate triangle from it. -----Examples----- Input 1 1 1 2 Output 4 Input 1 2 3 1 Output 2 Input 10 2 1 7 Output 0 -----Note----- In the first sample test you can either not increase any stick or increase any two sticks by 1 centimeter. In the second sample test you can increase either the first or the second stick by one centimeter. Note that the triangle made from the initial sticks is degenerate and thus, doesn't meet the conditions. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python a, b, c, d = list(map(int, input().split(' '))) ans = -(d+1) * (d+2) * (d+3) // 6 for l1 in range(0, d+1): minx = min(d-l1, a-b-c+l1) if minx < 0: continue; else: ans += (minx + 1) * (minx + 2) // 2 a, b, c = b, c, a for l1 in range(0, d+1): minx = min(d-l1, a-b-c+l1) if minx < 0: continue; else: ans += (minx + 1) * (minx + 2) // 2 a, b, c = b, c, a for l1 in range(0, d+1): minx = min(d-l1, a-b-c+l1) if minx < 0: continue; else: ans += (minx + 1) * (minx + 2) // 2 print(-ans) ```
vfc_23971
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/571/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1 1 1 2\n", "output": "4\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3817
Solve the following coding problem using the programming language python: A sequence of non-negative integers a_1, a_2, ..., a_{n} of length n is called a wool sequence if and only if there exists two integers l and r (1 ≤ l ≤ r ≤ n) such that $a_{l} \oplus a_{l + 1} \oplus \cdots \oplus a_{r} = 0$. In other words each wool sequence contains a subsequence of consecutive elements with xor equal to 0. The expression $x \oplus y$ means applying the operation of a bitwise xor to numbers x and y. The given operation exists in all modern programming languages, for example, in languages C++ and Java it is marked as "^", in Pascal — as "xor". In this problem you are asked to compute the number of sequences made of n integers from 0 to 2^{m} - 1 that are not a wool sequence. You should print this number modulo 1000000009 (10^9 + 9). -----Input----- The only line of input contains two space-separated integers n and m (1 ≤ n, m ≤ 10^5). -----Output----- Print the required number of sequences modulo 1000000009 (10^9 + 9) on the only line of output. -----Examples----- Input 3 2 Output 6 -----Note----- Sequences of length 3 made of integers 0, 1, 2 and 3 that are not a wool sequence are (1, 3, 1), (1, 2, 1), (2, 1, 2), (2, 3, 2), (3, 1, 3) and (3, 2, 3). The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, m = map(int, input().split()) s, d = 1, 1000000009 k = pow(2, m, d) - 1 for i in range(n): s, k = (s * k) % d, k - 1 print(s) ```
vfc_23975
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/238/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 2\n", "output": "6\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3819
Solve the following coding problem using the programming language python: Nauuo is a girl who loves playing cards. One day she was playing cards but found that the cards were mixed with some empty ones. There are $n$ cards numbered from $1$ to $n$, and they were mixed with another $n$ empty cards. She piled up the $2n$ cards and drew $n$ of them. The $n$ cards in Nauuo's hands are given. The remaining $n$ cards in the pile are also given in the order from top to bottom. In one operation she can choose a card in her hands and play it — put it at the bottom of the pile, then draw the top card from the pile. Nauuo wants to make the $n$ numbered cards piled up in increasing order (the $i$-th card in the pile from top to bottom is the card $i$) as quickly as possible. Can you tell her the minimum number of operations? -----Input----- The first line contains a single integer $n$ ($1\le n\le 2\cdot 10^5$) — the number of numbered cards. The second line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($0\le a_i\le n$) — the initial cards in Nauuo's hands. $0$ represents an empty card. The third line contains $n$ integers $b_1,b_2,\ldots,b_n$ ($0\le b_i\le n$) — the initial cards in the pile, given in order from top to bottom. $0$ represents an empty card. It is guaranteed that each number from $1$ to $n$ appears exactly once, either in $a_{1..n}$ or $b_{1..n}$. -----Output----- The output contains a single integer — the minimum number of operations to make the $n$ numbered cards piled up in increasing order. -----Examples----- Input 3 0 2 0 3 0 1 Output 2 Input 3 0 2 0 1 0 3 Output 4 Input 11 0 0 0 5 0 0 0 4 0 0 11 9 2 6 0 8 1 7 0 3 0 10 Output 18 -----Note----- Example 1 We can play the card $2$ and draw the card $3$ in the first operation. After that, we have $[0,3,0]$ in hands and the cards in the pile are $[0,1,2]$ from top to bottom. Then, we play the card $3$ in the second operation. The cards in the pile are $[1,2,3]$, in which the cards are piled up in increasing order. Example 2 Play an empty card and draw the card $1$, then play $1$, $2$, $3$ in order. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def pe(s): return print(str(s), file=sys.stderr) def JA(a, sep): return sep.join(map(str, a)) def bs(f, mi, ma): mm = -1 while ma > mi: mm = (ma+mi) // 2 if f(mm): mi = mm + 1 else: ma = mm if f(mm): return mm + 1 return mm def main(): n = I() a = LI() b = LI() if b == list(range(1,n+1)): return 0 ss = set(a) if 1 not in ss: i = b.index(1) bf = 1 for j in range(i,n): if b[j] != j - i + 1: bf = 0 break if bf: t = b[-1] s = ss | set() for j in range(n-t): if t+j+1 not in s: bf = 0 break s.add(b[j]) if bf: return n - t def f(i): s = ss | set(b[:i]) for j in range(1,n+1): if j not in s: return True if i + j <= n: s.add(b[i+j-1]) return False r = bs(f,0,n) return r + n print(main()) ```
vfc_23983
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1172/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n0 2 0\n3 0 1\n", "output": "2", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n0 2 0\n1 0 3\n", "output": "4", "type": "stdin_stdout" }, { "fn_name": null, "input": "11\n0 0 0 5 0 0 0 4 0 0 11\n9 2 6 0 8 1 7 0 3 0 10\n", "output": "18", "type": "stdin_stdout" }, { "fn_name": null, "input": "8\n0 0 0 0 0 0 0 0\n7 8 1 2 3 4 5 6\n", "output": "11", "type": "stdin_stdout" }, { "fn_name": null, "input": "8\n0 0 0 0 4 5 7 8\n0 6 0 0 0 1 2 3\n", "output": "5", "type": "stdin_stdout" }, { "fn_name": null, "input": "8\n0 0 0 0 0 0 7 8\n0 1 2 3 4 5 0 6\n", "output": "11", "type": "stdin_stdout" } ] }
apps
verifiable_code
3820
Solve the following coding problem using the programming language python: You are given two strings $s$ and $t$. The string $s$ consists of lowercase Latin letters and at most one wildcard character '*', the string $t$ consists only of lowercase Latin letters. The length of the string $s$ equals $n$, the length of the string $t$ equals $m$. The wildcard character '*' in the string $s$ (if any) can be replaced with an arbitrary sequence (possibly empty) of lowercase Latin letters. No other character of $s$ can be replaced with anything. If it is possible to replace a wildcard character '*' in $s$ to obtain a string $t$, then the string $t$ matches the pattern $s$. For example, if $s=$"aba*aba" then the following strings match it "abaaba", "abacaba" and "abazzzaba", but the following strings do not match: "ababa", "abcaaba", "codeforces", "aba1aba", "aba?aba". If the given string $t$ matches the given string $s$, print "YES", otherwise print "NO". -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the string $t$, respectively. The second line contains string $s$ of length $n$, which consists of lowercase Latin letters and at most one wildcard character '*'. The third line contains string $t$ of length $m$, which consists only of lowercase Latin letters. -----Output----- Print "YES" (without quotes), if you can obtain the string $t$ from the string $s$. Otherwise print "NO" (without quotes). -----Examples----- Input 6 10 code*s codeforces Output YES Input 6 5 vk*cup vkcup Output YES Input 1 1 v k Output NO Input 9 6 gfgf*gfgf gfgfgf Output NO -----Note----- In the first example a wildcard character '*' can be replaced with a string "force". So the string $s$ after this replacement is "codeforces" and the answer is "YES". In the second example a wildcard character '*' can be replaced with an empty string. So the string $s$ after this replacement is "vkcup" and the answer is "YES". There is no wildcard character '*' in the third example and the strings "v" and "k" are different so the answer is "NO". In the fourth example there is no such replacement of a wildcard character '*' that you can obtain the string $t$ so the answer is "NO". The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python #!/usr/bin/env python3 def solve(n, m, s, t): if '*' in s: l, r = s.split('*') return len(l) + len(r) <= len(t) and t.startswith(l) and t.endswith(r) else: return s == t n, m = list(map(int, input().split())) s = input() t = input() print(['NO', 'YES'][solve(n, m, s, t)]) ```
vfc_23987
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1023/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6 10\ncode*s\ncodeforces\n", "output": "YES\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3821
Solve the following coding problem using the programming language python: Andrey needs one more problem to conduct a programming contest. He has n friends who are always willing to help. He can ask some of them to come up with a contest problem. Andrey knows one value for each of his fiends — the probability that this friend will come up with a problem if Andrey asks him. Help Andrey choose people to ask. As he needs only one problem, Andrey is going to be really upset if no one comes up with a problem or if he gets more than one problem from his friends. You need to choose such a set of people that maximizes the chances of Andrey not getting upset. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 100) — the number of Andrey's friends. The second line contains n real numbers p_{i} (0.0 ≤ p_{i} ≤ 1.0) — the probability that the i-th friend can come up with a problem. The probabilities are given with at most 6 digits after decimal point. -----Output----- Print a single real number — the probability that Andrey won't get upset at the optimal choice of friends. The answer will be considered valid if it differs from the correct one by at most 10^{ - 9}. -----Examples----- Input 4 0.1 0.2 0.3 0.8 Output 0.800000000000 Input 2 0.1 0.2 Output 0.260000000000 -----Note----- In the first sample the best strategy for Andrey is to ask only one of his friends, the most reliable one. In the second sample the best strategy for Andrey is to ask all of his friends to come up with a problem. Then the probability that he will get exactly one problem is 0.1·0.8 + 0.9·0.2 = 0.26. 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()) a = list(map(float, input().split())) a.sort() s = a[-1] p = 1 - a[-1] for i in range(n - 2, -1, -1): if s < s * (1 - a[i]) + a[i] * p: s = s * (1 - a[i]) + a[i] * p p *= (1 - a[i]) print('%.9lf' % s) ```
vfc_23991
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/442/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n0.1 0.2 0.3 0.8\n", "output": "0.800000000000\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n0.1 0.2\n", "output": "0.260000000000\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n0.217266\n", "output": "0.217266000000\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3823
Solve the following coding problem using the programming language python: Jeff got 2n real numbers a_1, a_2, ..., a_2n as a birthday present. The boy hates non-integer numbers, so he decided to slightly "adjust" the numbers he's got. Namely, Jeff consecutively executes n operations, each of them goes as follows: choose indexes i and j (i ≠ j) that haven't been chosen yet; round element a_{i} to the nearest integer that isn't more than a_{i} (assign to a_{i}: ⌊ a_{i} ⌋); round element a_{j} to the nearest integer that isn't less than a_{j} (assign to a_{j}: ⌈ a_{j} ⌉). Nevertheless, Jeff doesn't want to hurt the feelings of the person who gave him the sequence. That's why the boy wants to perform the operations so as to make the absolute value of the difference between the sum of elements before performing the operations and the sum of elements after performing the operations as small as possible. Help Jeff find the minimum absolute value of the difference. -----Input----- The first line contains integer n (1 ≤ n ≤ 2000). The next line contains 2n real numbers a_1, a_2, ..., a_2n (0 ≤ a_{i} ≤ 10000), given with exactly three digits after the decimal point. The numbers are separated by spaces. -----Output----- In a single line print a single real number — the required difference with exactly three digits after the decimal point. -----Examples----- Input 3 0.000 0.500 0.750 1.000 2.000 3.000 Output 0.250 Input 3 4469.000 6526.000 4864.000 9356.383 7490.000 995.896 Output 0.279 -----Note----- In the first test case you need to perform the operations as follows: (i = 1, j = 4), (i = 2, j = 3), (i = 5, j = 6). In this case, the difference will equal |(0 + 0.5 + 0.75 + 1 + 2 + 3) - (0 + 0 + 1 + 1 + 2 + 3)| = 0.25. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python k = 0 ans = 0 n = int(input()) a = input().split() for i in range(2 * n): s = float(a[i]) if s != int(s): k+=1 ans += (int(s) + 1 - s) if ans - int(ans) > 0.5: p = int(ans) + 1 else: p = int(ans) if p > n: p = n if (p + n >= k): print('%.3f'% abs(ans - p)) else: print('%.3f'% abs(ans - k + n)) ```
vfc_23999
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/351/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n0.000 0.500 0.750 1.000 2.000 3.000\n", "output": "0.250\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n4469.000 6526.000 4864.000 9356.383 7490.000 995.896\n", "output": "0.279\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n673.674 9263.142 6780.000 9801.000 4640.000 8244.000\n", "output": "0.184\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n6470.649 8295.000 8486.000 9855.000 223.000 579.549\n", "output": "0.198\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7\n2341.538 9232.119 6646.930 9316.834 5684.000 9078.705 7773.000 3823.674 6357.022 9866.925 310.271 6554.778 8341.098 8407.987\n", "output": "0.119\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3824
Solve the following coding problem using the programming language python: Polycarp takes part in a quadcopter competition. According to the rules a flying robot should: start the race from some point of a field, go around the flag, close cycle returning back to the starting point. Polycarp knows the coordinates of the starting point (x_1, y_1) and the coordinates of the point where the flag is situated (x_2, y_2). Polycarp’s quadcopter can fly only parallel to the sides of the field each tick changing exactly one coordinate by 1. It means that in one tick the quadcopter can fly from the point (x, y) to any of four points: (x - 1, y), (x + 1, y), (x, y - 1) or (x, y + 1). Thus the quadcopter path is a closed cycle starting and finishing in (x_1, y_1) and containing the point (x_2, y_2) strictly inside. [Image] The picture corresponds to the first example: the starting (and finishing) point is in (1, 5) and the flag is in (5, 2). What is the minimal length of the quadcopter path? -----Input----- The first line contains two integer numbers x_1 and y_1 ( - 100 ≤ x_1, y_1 ≤ 100) — coordinates of the quadcopter starting (and finishing) point. The second line contains two integer numbers x_2 and y_2 ( - 100 ≤ x_2, y_2 ≤ 100) — coordinates of the flag. It is guaranteed that the quadcopter starting point and the flag do not coincide. -----Output----- Print the length of minimal path of the quadcopter to surround the flag and return back. -----Examples----- Input 1 5 5 2 Output 18 Input 0 1 0 0 Output 8 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python Q = input().split(" ") F = input().split(" ") xQ = int(Q[0]) yQ = int(Q[1]) xF = int(F[0]) yF = int(F[1]) if xQ == xF: print((abs(yQ-yF)+1)*2+4) elif yQ == yF: print((abs(xQ-xF)+1)*2+4) else: print((abs(xQ-xF)+1)*2+(abs(yQ-yF)+1)*2) ```
vfc_24003
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/883/M", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1 5\n5 2\n", "output": "18\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "0 1\n0 0\n", "output": "8\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "-100 -100\n100 100\n", "output": "804\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "-100 -100\n-100 100\n", "output": "406\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3825
Solve the following coding problem using the programming language python: Let's introduce a number system which is based on a roman digits. There are digits I, V, X, L which correspond to the numbers $1$, $5$, $10$ and $50$ respectively. The use of other roman digits is not allowed. Numbers in this system are written as a sequence of one or more digits. We define the value of the sequence simply as the sum of digits in it. For example, the number XXXV evaluates to $35$ and the number IXI — to $12$. Pay attention to the difference to the traditional roman system — in our system any sequence of digits is valid, moreover the order of digits doesn't matter, for example IX means $11$, not $9$. One can notice that this system is ambiguous, and some numbers can be written in many different ways. Your goal is to determine how many distinct integers can be represented by exactly $n$ roman digits I, V, X, L. -----Input----- The only line of the input file contains a single integer $n$ ($1 \le n \le 10^9$) — the number of roman digits to use. -----Output----- Output a single integer — the number of distinct integers which can be represented using $n$ roman digits exactly. -----Examples----- Input 1 Output 4 Input 2 Output 10 Input 10 Output 244 -----Note----- In the first sample there are exactly $4$ integers which can be represented — I, V, X and L. In the second sample it is possible to represent integers $2$ (II), $6$ (VI), $10$ (VV), $11$ (XI), $15$ (XV), $20$ (XX), $51$ (IL), $55$ (VL), $60$ (XL) and $100$ (LL). The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # add 0,4,9,49 # this is a stupid solution import sys #sys.stdin=open("data.txt") input=sys.stdin.readline n=int(input()) # do a stupid approach dp=[0]*150 s=set([0]) for i in range(150): dp[i]=len(s) s2=set(s) for j in s: s2.add(j+4) s2.add(j+9) s2.add(j+49) s=s2 if 0: for i in range(100): if dp[i+49]-dp[i]!=2401: print(i) if n<150: print(dp[n]) else: stuff=(n//49) while n-stuff*49+49<150: stuff-=1 print(dp[n-stuff*49]+2401*stuff) ```
vfc_24007
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/997/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n", "output": "10\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n", "output": "244\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1000\n", "output": "48753\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3826
Solve the following coding problem using the programming language python: You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def main(): import sys input = sys.stdin.readline n = int(input()) arr = list(map(int, input().split())) dct = {} for x in arr: dct[x] = 0 i = 0 while i != n and dct[arr[i]] == 0: dct[arr[i]] = 1 i += 1 if i == n: print(0) return 0 j = n - 1 while dct[arr[j]] == 0: dct[arr[j]] = 1 j -= 1 ans = j - i + 1 for k in range(i - 1, -1, -1): dct[arr[k]] -= 1 while dct[arr[j]] == 0: dct[arr[j]] = 1 j -= 1 ans = min(ans, j - (k - 1)) print(ans) return 0 main() ```
vfc_24011
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1208/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n1 2 3\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n1 1 2 2\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n1 4 1 4 9\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n7 6 5 4 3 2 1 7 7 7\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n192901559\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n2 1\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3827
Solve the following coding problem using the programming language python: A has a string consisting of some number of lowercase English letters 'a'. He gives it to his friend B who appends some number of letters 'b' to the end of this string. Since both A and B like the characters 'a' and 'b', they have made sure that at this point, at least one 'a' and one 'b' exist in the string. B now gives this string to C and he appends some number of letters 'c' to the end of the string. However, since C is a good friend of A and B, the number of letters 'c' he appends is equal to the number of 'a' or to the number of 'b' in the string. It is also possible that the number of letters 'c' equals both to the number of letters 'a' and to the number of letters 'b' at the same time. You have a string in your hands, and you want to check if it is possible to obtain the string in this way or not. If it is possible to obtain the string, print "YES", otherwise print "NO" (without the quotes). -----Input----- The first and only line consists of a string $S$ ($ 1 \le |S| \le 5\,000 $). It is guaranteed that the string will only consist of the lowercase English letters 'a', 'b', 'c'. -----Output----- Print "YES" or "NO", according to the condition. -----Examples----- Input aaabccc Output YES Input bbacc Output NO Input aabc Output YES -----Note----- Consider first example: the number of 'c' is equal to the number of 'a'. Consider second example: although the number of 'c' is equal to the number of the 'b', the order is not correct. Consider third example: the number of 'c' is equal to the number of 'b'. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python '''input aabc ''' def list_input(): return list(map(int,input().split())) def map_input(): return map(int,input().split()) def map_string(): return input().split() s = input() a = s.count('a') b = s.count('b') c = s.count('c') ans = "YES" if(a == 0 or b == 0): ans = "NO" if(c != a and c != b): ans = "NO" for i in range(len(s)-1): if(s[i] > s[i+1]): ans = "NO" print(ans) ```
vfc_24015
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/960/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "aaabccc\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "bbacc\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "aabc\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "aabbcc\n", "output": "YES\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3828
Solve the following coding problem using the programming language python: An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? -----Input----- The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers p_{i} (1 ≤ p_{i} ≤ n, p_{i} ≠ p_{j} if i ≠ j) — the sequence of the numbers of the cars in the train. -----Output----- Print a single integer — the minimum number of actions needed to sort the railway cars. -----Examples----- Input 5 4 1 2 5 3 Output 2 Input 4 4 1 3 2 Output 2 -----Note----- In the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, p = int(input()), list(map(int, input().split())) a, c, v = [0] * n, 1, 1 for i, pi in enumerate(p): a[pi - 1] = i for i in range(n - 1): if a[i] < a[i + 1]: c += 1 if c > v: v = c else: c = 1 print(n - v) ```
vfc_24019
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/605/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n4 1 2 5 3\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n4 1 3 2\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n1\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3829
Solve the following coding problem using the programming language python: Twilight Sparkle was playing Ludo with her friends Rainbow Dash, Apple Jack and Flutter Shy. But she kept losing. Having returned to the castle, Twilight Sparkle became interested in the dice that were used in the game. The dice has m faces: the first face of the dice contains a dot, the second one contains two dots, and so on, the m-th face contains m dots. Twilight Sparkle is sure that when the dice is tossed, each face appears with probability $\frac{1}{m}$. Also she knows that each toss is independent from others. Help her to calculate the expected maximum number of dots she could get after tossing the dice n times. -----Input----- A single line contains two integers m and n (1 ≤ m, n ≤ 10^5). -----Output----- Output a single real number corresponding to the expected maximum. The answer will be considered correct if its relative or absolute error doesn't exceed 10 ^{ - 4}. -----Examples----- Input 6 1 Output 3.500000000000 Input 6 3 Output 4.958333333333 Input 2 2 Output 1.750000000000 -----Note----- Consider the third test example. If you've made two tosses: You can get 1 in the first toss, and 2 in the second. Maximum equals to 2. You can get 1 in the first toss, and 1 in the second. Maximum equals to 1. You can get 2 in the first toss, and 1 in the second. Maximum equals to 2. You can get 2 in the first toss, and 2 in the second. Maximum equals to 2. The probability of each outcome is 0.25, that is expectation equals to: $(2 + 1 + 2 + 2) \cdot 0.25 = \frac{7}{4}$ You can read about expectation using the following link: http://en.wikipedia.org/wiki/Expected_value The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python """ Codeforces Contest 259 Div 1 Problem A Author : chaotic_iak Language: Python 3.3.4 """ def main(): m,n = read() print(m - sum((i/m)**n for i in range(1,m))) ################################### NON-SOLUTION STUFF BELOW def read(mode=2): # 0: String # 1: List of strings # 2: List of integers inputs = input().strip() if mode == 0: return inputs if mode == 1: return inputs.split() if mode == 2: return map(int, inputs.split()) def read_str(): return read(0) def read_int(): return read(2)[0] def write(s="\n"): if isinstance(s, list): s = " ".join(map(str, s)) s = str(s) print(s, end="") main() ```
vfc_24023
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/453/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6 1\n", "output": "3.500000000000\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6 3\n", "output": "4.958333333333\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 2\n", "output": "1.750000000000\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 4\n", "output": "4.433600000000\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3830
Solve the following coding problem using the programming language python: In the snake exhibition, there are $n$ rooms (numbered $0$ to $n - 1$) arranged in a circle, with a snake in each room. The rooms are connected by $n$ conveyor belts, and the $i$-th conveyor belt connects the rooms $i$ and $(i+1) \bmod n$. In the other words, rooms $0$ and $1$, $1$ and $2$, $\ldots$, $n-2$ and $n-1$, $n-1$ and $0$ are connected with conveyor belts. The $i$-th conveyor belt is in one of three states: If it is clockwise, snakes can only go from room $i$ to $(i+1) \bmod n$. If it is anticlockwise, snakes can only go from room $(i+1) \bmod n$ to $i$. If it is off, snakes can travel in either direction. [Image] Above is an example with $4$ rooms, where belts $0$ and $3$ are off, $1$ is clockwise, and $2$ is anticlockwise. Each snake wants to leave its room and come back to it later. A room is returnable if the snake there can leave the room, and later come back to it using the conveyor belts. How many such returnable rooms are there? -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$): the number of test cases. The description of the test cases follows. The first line of each test case description contains a single integer $n$ ($2 \le n \le 300\,000$): the number of rooms. The next line of each test case description contains a string $s$ of length $n$, consisting of only '<', '>' and '-'. If $s_{i} = $ '>', the $i$-th conveyor belt goes clockwise. If $s_{i} = $ '<', the $i$-th conveyor belt goes anticlockwise. If $s_{i} = $ '-', the $i$-th conveyor belt is off. It is guaranteed that the sum of $n$ among all test cases does not exceed $300\,000$. -----Output----- For each test case, output the number of returnable rooms. -----Example----- Input 4 4 -><- 5 >>>>> 3 <-- 2 <> Output 3 5 3 0 -----Note----- In the first test case, all rooms are returnable except room $2$. The snake in the room $2$ is trapped and cannot exit. This test case corresponds to the picture from the problem statement. In the second test case, all rooms are returnable by traveling on the series of clockwise belts. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def solve(): n = int(input()) s = input() k = '-' tr = True for i in s: if k == '-' and i != '-': k = i if i != '-' and k != i: tr = False if tr: print(n) return 0 ans = 0 for i in range(n): if s[i] == "-" or s[i - 1] == '-': ans += 1 print(ans) for i in range(int(input())): solve() ```
vfc_24027
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1428/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n4\n-><-\n5\n>>>>>\n3\n<--\n2\n<>\n", "output": "3\n5\n3\n0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "36\n2\n--\n2\n>-\n2\n<-\n2\n->\n2\n>>\n2\n<>\n2\n-<\n2\n><\n2\n<<\n3\n---\n3\n>--\n3\n<--\n3\n->-\n3\n>>-\n3\n<>-\n3\n-<-\n3\n><-\n3\n<<-\n3\n-->\n3\n>->\n3\n<->\n3\n->>\n3\n>>>\n3\n<>>\n3\n-<>\n3\n><>\n3\n<<>\n3\n--<\n3\n>-<\n3\n<-<\n3\n-><\n3\n>><\n3\n<><\n3\n-<<\n3\n><<\n3\n<<<\n", "output": "2\n2\n2\n2\n2\n0\n2\n0\n2\n3\n3\n3\n3\n3\n2\n3\n2\n3\n3\n3\n2\n3\n3\n0\n2\n0\n0\n3\n2\n3\n2\n0\n0\n3\n0\n3\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
3831
Solve the following coding problem using the programming language python: Mayor of city S just hates trees and lawns. They take so much space and there could be a road on the place they occupy! The Mayor thinks that one of the main city streets could be considerably widened on account of lawn nobody needs anyway. Moreover, that might help reduce the car jams which happen from time to time on the street. The street is split into n equal length parts from left to right, the i-th part is characterized by two integers: width of road s_{i} and width of lawn g_{i}. [Image] For each of n parts the Mayor should decide the size of lawn to demolish. For the i-th part he can reduce lawn width by integer x_{i} (0 ≤ x_{i} ≤ g_{i}). After it new road width of the i-th part will be equal to s'_{i} = s_{i} + x_{i} and new lawn width will be equal to g'_{i} = g_{i} - x_{i}. On the one hand, the Mayor wants to demolish as much lawn as possible (and replace it with road). On the other hand, he does not want to create a rapid widening or narrowing of the road, which would lead to car accidents. To avoid that, the Mayor decided that width of the road for consecutive parts should differ by at most 1, i.e. for each i (1 ≤ i < n) the inequation |s'_{i} + 1 - s'_{i}| ≤ 1 should hold. Initially this condition might not be true. You need to find the the total width of lawns the Mayor will destroy according to his plan. -----Input----- The first line contains integer n (1 ≤ n ≤ 2·10^5) — number of parts of the street. Each of the following n lines contains two integers s_{i}, g_{i} (1 ≤ s_{i} ≤ 10^6, 0 ≤ g_{i} ≤ 10^6) — current width of road and width of the lawn on the i-th part of the street. -----Output----- In the first line print the total width of lawns which will be removed. In the second line print n integers s'_1, s'_2, ..., s'_{n} (s_{i} ≤ s'_{i} ≤ s_{i} + g_{i}) — new widths of the road starting from the first part and to the last. If there is no solution, print the only integer -1 in the first line. -----Examples----- Input 3 4 5 4 5 4 10 Output 16 9 9 10 Input 4 1 100 100 1 1 100 100 1 Output 202 101 101 101 101 Input 3 1 1 100 100 1 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 t = [] l = [] r = [] for _ in range(int(input())): s, g = map(int, input().split()) t.append(s) l.append(s) r.append(g + s) for i in range(1, len(l)): if l[i] < l[i - 1]: l[i] = l[i - 1] - 1 if r[i] > r[i - 1]: r[i] = r[i - 1] + 1 for i in range(len(l) - 2, -1, -1): if l[i] < l[i + 1]: l[i] = l[i + 1] - 1 if r[i] > r[i + 1]: r[i] = r[i + 1] + 1 if [1 for a, b in zip(l, r) if a > b]: print(-1) else: print(sum([b - a for a, b in zip(t, r)])) print(' '.join(map(str, r))) ```
vfc_24031
{ "difficulty": "competition", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/883/K", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n4 5\n4 5\n4 10\n", "output": "16\n9 9 10 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n1 100\n100 1\n1 100\n100 1\n", "output": "202\n101 101 101 101 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n1 1\n100 100\n1 1\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n21005 10850\n27020 13372\n28183 3724\n22874 13564\n27446 11493\n22522 10012\n24819 11529\n24166 11084\n24539 9211\n24152 9235\n", "output": "71869\n31855 31856 31857 31858 31859 31860 31861 31862 31863 31864 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n1 0\n", "output": "0\n1 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n1 1000000\n", "output": "1000000\n1000001 \n", "type": "stdin_stdout" } ] }