task_type
stringclasses
1 value
problem
stringlengths
209
3.39k
answer
stringlengths
35
6.15k
problem_tokens
int64
60
774
answer_tokens
int64
12
2.04k
coding
Solve the programming task below in a Python markdown code block. You need to handle two extraordinarily large integers. The good news is that you don't need to perform any arithmetic operation on them. You just need to compare them and see whether they are equal or one is greater than the other. Given two strings x and y, print either "x< y"(ignore space, editor issues), "x>y" or "x=y" depending on the values represented by x and y. x and y each consist of a decimal integer followed zero or more '!' characters. Each '!' represents the factorial operation. For example, "3!!" represents 3!! = 6! = 720. Input - First line of input contains no. of testcases and each test consist of 2 lines x and y. Ouptut - Print the required output. SAMPLE INPUT 3 0! 1 9!! 999999999 456!!! 123!!!!!! SAMPLE OUTPUT x=y x>y x<y
{"inputs": ["27\n0!!!\n0\n1\n1!!!\n2!!!\n2\n3!!!\n999999999\n0!\n0\n11!\n40000000\n3!!\n721\n3\n2!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n7!!!\n3!!!!\n0!\n2\n1\n2\n0\n0\n3!!!!\n4!!!!\n15!\n999999999\n999999999\n4!!\n7!!!\n5000!!\n40321\n8!\n3!\n5\n5!!\n3!!!\n7!\n3!!\n3!\n7\n6!\n719\n719!!\n6!!!\n6!\n721\n721!!!!\n6!!!!!\n720\n3!!\n0!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n1!!!!!!!", "25\n0!\n1\n9!\n999999999\n9!!\n999999999\n456!!!\n123!!!!!!\n5!\n120\n9!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n999999999!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n0!\n1\n1!\n1\n2!\n2\n6\n3!\n4!\n24\n5!\n120\n720\n6!\n5040!\n7!!\n8!!!\n40320!!\n362880!!!\n9!!!!\n10!!!!!\n3628800!!!!\n39916800!!!!!\n11!!!!!!\n12!!!!!!!\n479001600!!!!!!\n13!\n999999999\n0!!\n1\n1\n1!!\n2!!\n2\n720\n3!!\n4!!\n999999999"], "outputs": ["x>y\nx=y\nx=y\nx>y\nx>y\nx<y\nx<y\nx>y\nx>y\nx<y\nx<y\nx=y\nx<y\nx>y\nx<y\nx>y\nx>y\nx>y\nx<y\nx>y\nx<y\nx>y\nx<y\nx<y\nx>y\nx=y\nx=y", "x=y\nx<y\nx>y\nx<y\nx=y\nx>y\nx=y\nx=y\nx=y\nx=y\nx=y\nx=y\nx=y\nx=y\nx=y\nx=y\nx=y\nx=y\nx=y\nx>y\nx=y\nx=y\nx=y\nx=y\nx>y"]}
218
655
coding
Solve the programming task below in a Python markdown code block. problem Five students, Taro, Jiro, Saburo, Shiro, and Hanako, participated in the JOI High School class. In this class, a final exam was conducted. All five people took the final exam. For students with a final exam score of 40 or higher, the final exam score was used as is. All students with a final exam score of less than 40 received supplementary lessons and scored 40 points. Create a program that calculates the average score of the five students' grades given the final exam scores of the five students. Example Input 10 65 100 30 95 Output 68
{"inputs": ["10\n3\n111\n19\n8", "10\n52\n011\n3\n1", "10\n3\n010\n24\n9", "10\n3\n010\n19\n9", "10\n3\n011\n19\n9", "10\n3\n011\n19\n8", "10\n9\n100\n30\n95", "10\n9\n110\n30\n95"], "outputs": ["54\n", "42\n", "40\n", "40\n", "40\n", "40\n", "63\n", "65\n"]}
155
168
coding
Solve the programming task below in a Python markdown code block. For years you have been working hard in Santa's factory to manufacture gifts for kids in substandard work environments with no pay. You have finally managed to escape the factory and now you seek revenge. You are planning a heist with the Grinch to steal all the gifts which are locked in a safe. Since you have worked in the factory for so many years, you know how to crack the safe. The passcode for the safe is an integer. This passcode keeps changing everyday, but you know a way to crack it. You will be given two numbers A and B . Passcode is the number of X such that 0 ≤ X < B and gcd(A,B) = gcd(A+X,B). Note : gcd(A,B) is the greatest common divisor of A & B. -----Input----- The first line contains the single integer T (1 ≤ T ≤ 50) — the number of test cases. Next T lines contain test cases per line. Each line contains two integers A & B ( 1 ≤ A < B ≤ 1010 ) -----Output----- Print T integers, one for each Test case. For each test case print the appropriate passcode for that day. -----Sample Input----- 3 4 9 5 10 42 9999999967 -----Output----- 6 1 9999999966
{"inputs": ["3\n4 9\n5 10\n42 9999999967"], "outputs": ["6\n1\n9999999966"]}
313
48
coding
Solve the programming task below in a Python markdown code block. Given a triangle of numbers where each number is equal to the sum of the three numbers on top of it, find the first even number in a row. Explanatory Note: The vertex of the triangle (at the top) is 1. The structure of the triangle is shown below. Each number is equal to the sum of the numbers at the following positions: Position X: immediately above it. Position Y: to the immediate left of X. Position Z: to the immediate right of X. If there are no numbers at positions X, Y, or Z, then assume those positions are occupied by a zero (0). This can occur for positions at the edge. Here are four rows of the triangle: 1 1 1 1 1 2 3 2 1 1 3 6 7 6 3 1 Input Format and Constraints First line contains a number T (number of test cases). Each of the next T lines contain a number N (the row number, assuming that the top vertex of the triangle is Row 1). Output Format For each test case, display an integer that denotes the position of the first even number. Note: Assume that the left most number in a row is Position 1. If there is no even number in a row, print -1. Constraints 1<=T<=100 3<=N<=1000000000 Sample Input 2 3 4 Sample Output 2 3
{"inputs": ["2\n3\n4\n"], "outputs": ["2\n3\n"]}
345
20
coding
Solve the programming task below in a Python markdown code block. Having bought his own apartment, Boris decided to paper the walls in every room. Boris's flat has n rooms, each of which has the form of a rectangular parallelepiped. For every room we known its length, width and height of the walls in meters (different rooms can have different dimensions, including height). Boris chose m types of wallpaper to paper the walls of the rooms with (but it is not necessary to use all the types). Each type of wallpaper is sold in rolls of a fixed length and width (the length, naturally, shows how long the unfolded roll will be). In addition, for each type we know the price of one roll of this type. The wallpaper of each type contains strips running along the length of the roll. When gluing the strips must be located strictly vertically (so the roll cannot be rotated, even if the length is less than the width). Besides, a roll can be cut in an arbitrary manner, but the joints of glued pieces should also be vertical. In addition, each room should be papered by only one type of wallpaper. And pieces of the same roll cannot be used to paper different rooms. That is, for each room the rolls are purchased separately. Also, some rolls can be used not completely. After buying an apartment Boris is short of cash, so he wants to spend the minimum money on wallpaper. Help him. Input The first line contains a positive integer n (1 ≤ n ≤ 500) — the number of rooms in Boris's apartment. Each of the next n lines contains three space-separated positive integers — the length, width and height of the walls in a given room in meters, respectively. The next line contains a positive integer m (1 ≤ m ≤ 500) — the number of available wallpaper types. Each of the following m lines contains three space-separated positive integers — the length and width in meters of a given wallpaper and the price of one roll, respectively. All numbers in the input data do not exceed 500. It is guaranteed that each room can be papered using these types of wallpaper. Output Print a single number — the minimum total cost of the rolls. Examples Input 1 5 5 3 3 10 1 100 15 2 320 3 19 500 Output 640 Note Note to the sample: The total length of the walls (the perimeter) of the room is 20 m. One roll of the first type can be cut into pieces to get three vertical 1 meter wide strips, ergo you need 7 rolls of this type, the price equals 700. A roll of the second type can be cut into pieces to get five 2 meter wide strips, we need 2 rolls, the price is 640. One roll of the third type can immediately paper 19 meters out of 20, but we cannot use other types and we have to buy a second roll, the price is 1000.
{"inputs": ["1\n1 1 1\n1\n1 1 1\n", "1\n0 1 1\n1\n1 1 1\n", "1\n9 10 7\n1\n7 1 3\n", "1\n15 10 7\n1\n7 1 3\n", "1\n16 10 7\n1\n7 1 3\n", "1\n16 10 7\n1\n9 1 3\n", "1\n16 10 7\n1\n8 1 3\n", "1\n500 500 1\n1\n1 1 500\n"], "outputs": ["4\n", "2\n", "114\n", "150\n", "156\n", "156\n", "156\n", "1000000\n"]}
645
213
coding
Solve the programming task below in a Python markdown code block. Sereja has got an array, consisting of n integers, a_1, a_2, ..., a_{n}. Sereja is an active boy, so he is now going to complete m operations. Each operation will have one of the three forms: Make v_{i}-th array element equal to x_{i}. In other words, perform the assignment a_{v}_{i} = x_{i}. Increase each array element by y_{i}. In other words, perform n assignments a_{i} = a_{i} + y_{i} (1 ≤ i ≤ n). Take a piece of paper and write out the q_{i}-th array element. That is, the element a_{q}_{i}. Help Sereja, complete all his operations. -----Input----- The first line contains integers n, m (1 ≤ n, m ≤ 10^5). The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9) — the original array. Next m lines describe operations, the i-th line describes the i-th operation. The first number in the i-th line is integer t_{i} (1 ≤ t_{i} ≤ 3) that represents the operation type. If t_{i} = 1, then it is followed by two integers v_{i} and x_{i}, (1 ≤ v_{i} ≤ n, 1 ≤ x_{i} ≤ 10^9). If t_{i} = 2, then it is followed by integer y_{i} (1 ≤ y_{i} ≤ 10^4). And if t_{i} = 3, then it is followed by integer q_{i} (1 ≤ q_{i} ≤ n). -----Output----- For each third type operation print value a_{q}_{i}. Print the values in the order, in which the corresponding queries follow in the input. -----Examples----- Input 10 11 1 2 3 4 5 6 7 8 9 10 3 2 3 9 2 10 3 1 3 10 1 1 10 2 10 2 10 3 1 3 10 3 9 Output 2 9 11 20 30 40 39
{"inputs": ["1 1\n1\n3 1\n", "1 1\n1\n3 1\n", "1 1\n2\n3 1\n", "1 1\n0\n3 1\n", "1 3\n1\n1 1 2\n2 1\n3 1\n", "1 3\n1\n1 1 2\n2 1\n3 1\n", "1 3\n1\n1 1 1\n2 1\n3 1\n", "4 4\n579010521 995684640 1830027497 63411296\n3 3\n3 1\n2 590\n1 1 856879574\n"], "outputs": ["1\n", "1\n", "2\n\n", "0\n\n", "3\n", "3\n", "2\n\n", "1830027497\n579010521\n\n"]}
538
249
coding
Solve the programming task below in a Python markdown code block. # Definition A number is a **_Special Number_** *if it’s digits only consist 0, 1, 2, 3, 4 or 5* **_Given_** a number *determine if it special number or not* . # Warm-up (Highly recommended) # [Playing With Numbers Series](https://www.codewars.com/collections/playing-with-numbers) ___ # Notes * **_The number_** passed will be **_positive_** (N > 0) . * All **single-digit numbers** with in the interval **_[0:5]_** are considered as **_special number_**. ___ # Input >> Output Examples ``` specialNumber(2) ==> return "Special!!" ``` ## Explanation: It's **_a single-digit number_** within the interval **_[0:5]_** . ``` specialNumber(9) ==> return "NOT!!" ``` ## Explanation: Although, it's a single-digit number but **_Outside the interval [0:5]_** . ``` specialNumber(23) ==> return "Special!!" ``` ## Explanation: All **_the number's digits_** formed from the interval **_[0:5]_** digits . ``` specialNumber(39) ==> return "NOT!!" ``` ## Explanation: Although, *there is a digit (3) Within the interval* **_But_** **_the second digit is not (Must be ALL The Number's Digits )_** . ``` specialNumber(59) ==> return "NOT!!" ``` ## Explanation: Although, *there is a digit (5) Within the interval* **_But_** **_the second digit is not (Must be ALL The Number's Digits )_** . ``` specialNumber(513) ==> return "Special!!" ``` ___ ``` specialNumber(709) ==> return "NOT!!" ``` ___ # [For More Enjoyable Katas](http://www.codewars.com/users/MrZizoScream/authored) ### ALL translation are welcomed ## Enjoy Learning !! # Zizou Also feel free to reuse/extend the following starter code: ```python def special_number(number): ```
{"functional": "_inputs = [[2], [3], [5], [9], [7], [23], [79], [32], [39], [55], [11350224]]\n_outputs = [['Special!!'], ['Special!!'], ['Special!!'], ['NOT!!'], ['NOT!!'], ['Special!!'], ['NOT!!'], ['Special!!'], ['NOT!!'], ['Special!!'], ['Special!!']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(special_number(*i), o[0])"}
509
238
coding
Solve the programming task below in a Python markdown code block. There are n stones on the table in a row, each of them can be red, green or blue. Count the minimum number of stones to take from the table so that any two neighboring stones had different colors. Stones in a row are considered neighboring if there are no other stones between them. -----Input----- The first line contains integer n (1 ≤ n ≤ 50) — the number of stones on the table. The next line contains string s, which represents the colors of the stones. We'll consider the stones in the row numbered from 1 to n from left to right. Then the i-th character s equals "R", if the i-th stone is red, "G", if it's green and "B", if it's blue. -----Output----- Print a single integer — the answer to the problem. -----Examples----- Input 3 RRG Output 1 Input 5 RRRRR Output 4 Input 4 BRBG Output 0
{"inputs": ["1\nB\n", "1\nB\n", "1\nC\n", "2\nBG\n", "2\nBB\n", "2\nBB\n", "2\nBG\n", "2\nGB\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "1\n", "1\n", "0\n", "0\n"]}
218
86
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an integer array receiver of length n and an integer k. n players are playing a ball-passing game. You choose the starting player, i. The game proceeds as follows: player i passes the ball to player receiver[i], who then passes it to receiver[receiver[i]], and so on, for k passes in total. The game's score is the sum of the indices of the players who touched the ball, including repetitions, i.e. i + receiver[i] + receiver[receiver[i]] + ... + receiver(k)[i]. Return the maximum possible score. Notes: receiver may contain duplicates. receiver[i] may be equal to i.   Please complete the following python code precisely: ```python class Solution: def getMaxFunctionValue(self, receiver: List[int], k: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(receiver = [2,0,1], k = 4) == 6\n assert candidate(receiver = [1,1,1,2,3], k = 3) == 10\n\n\ncheck(Solution().getMaxFunctionValue)"}
189
68
coding
Solve the programming task below in a Python markdown code block. Three friends are going to meet each other. Initially, the first friend stays at the position $x = a$, the second friend stays at the position $x = b$ and the third friend stays at the position $x = c$ on the coordinate axis $Ox$. In one minute each friend independently from other friends can change the position $x$ by $1$ to the left or by $1$ to the right (i.e. set $x := x - 1$ or $x := x + 1$) or even don't change it. Let's introduce the total pairwise distance — the sum of distances between each pair of friends. Let $a'$, $b'$ and $c'$ be the final positions of the first, the second and the third friend, correspondingly. Then the total pairwise distance is $|a' - b'| + |a' - c'| + |b' - c'|$, where $|x|$ is the absolute value of $x$. Friends are interested in the minimum total pairwise distance they can reach if they will move optimally. Each friend will move no more than once. So, more formally, they want to know the minimum total pairwise distance they can reach after one minute. You have to answer $q$ independent test cases. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 1000$) — the number of test cases. The next $q$ lines describe test cases. The $i$-th test case is given as three integers $a, b$ and $c$ ($1 \le a, b, c \le 10^9$) — initial positions of the first, second and third friend correspondingly. The positions of friends can be equal. -----Output----- For each test case print the answer on it — the minimum total pairwise distance (the minimum sum of distances between each pair of friends) if friends change their positions optimally. Each friend will move no more than once. So, more formally, you have to find the minimum total pairwise distance they can reach after one minute. -----Example----- Input 8 3 3 4 10 20 30 5 5 5 2 4 3 1 1000000000 1000000000 1 1000000000 999999999 3 2 5 3 2 6 Output 0 36 0 0 1999999994 1999999994 2 4
{"inputs": ["5\n1 1 1\n2 2 2\n3 3 3\n4 4 4\n10 5 8\n", "5\n1 1 1\n2 2 2\n3 3 3\n4 4 4\n10 5 8\n", "5\n1 1 1\n2 2 2\n0 3 3\n4 4 4\n10 5 8\n", "5\n1 1 1\n2 4 2\n0 3 3\n4 1 4\n10 5 8\n", "5\n1 1 1\n1 2 2\n3 3 3\n4 4 4\n10 5 8\n", "5\n1 1 1\n2 4 0\n0 3 3\n4 4 4\n10 5 8\n", "5\n1 1 1\n2 4 2\n0 3 3\n4 1 4\n10 8 8\n", "5\n1 1 1\n2 4 2\n0 3 3\n4 1 6\n10 5 7\n"], "outputs": ["0\n0\n0\n0\n6\n", "0\n0\n0\n0\n6\n", "0\n0\n2\n0\n6\n", "0\n0\n2\n2\n6\n", "0\n0\n0\n0\n6\n", "0\n4\n2\n0\n6\n", "0\n0\n2\n2\n0\n", "0\n0\n2\n6\n6\n"]}
586
382
coding
Solve the programming task below in a Python markdown code block. There are K items placed on a grid of squares with R rows and C columns. Let (i, j) denote the square at the i-th row (1 \leq i \leq R) and the j-th column (1 \leq j \leq C). The i-th item is at (r_i, c_i) and has the value v_i. Takahashi will begin at (1, 1), the start, and get to (R, C), the goal. When he is at (i, j), he can move to (i + 1, j) or (i, j + 1) (but cannot move to a non-existent square). He can pick up items on the squares he visits, including the start and the goal, but at most three for each row. It is allowed to ignore the item on a square he visits. Find the maximum possible sum of the values of items he picks up. -----Constraints----- - 1 \leq R, C \leq 3000 - 1 \leq K \leq \min(2 \times 10^5, R \times C) - 1 \leq r_i \leq R - 1 \leq c_i \leq C - (r_i, c_i) \neq (r_j, c_j) (i \neq j) - 1 \leq v_i \leq 10^9 - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: R C K r_1 c_1 v_1 r_2 c_2 v_2 : r_K c_K v_K -----Output----- Print the maximum possible sum of the values of items Takahashi picks up. -----Sample Input----- 2 2 3 1 1 3 2 1 4 1 2 5 -----Sample Output----- 8 He has two ways to get to the goal: - Visit (1, 1), (1, 2), and (2, 2), in this order. In this case, the total value of the items he can pick up is 3 + 5 = 8. - Visit (1, 1), (2, 1), and (2, 2), in this order. In this case, the total value of the items he can pick up is 3 + 4 = 7. Thus, the maximum possible sum of the values of items he picks up is 8.
{"inputs": ["2 2 3\n1 0 3\n2 1 4\n1 2 5", "2 2 3\n1 0 3\n2 1 8\n1 2 5", "3 2 3\n1 0 5\n2 2 4\n1 2 5", "2 2 3\n1 0 5\n2 1 4\n1 2 5", "3 2 3\n1 0 5\n2 1 4\n1 2 5", "2 2 3\n1 1 3\n2 1 4\n1 2 5", "2 2 3\n1 1 3\n2 1 4\n1 2 5\n", "2 5 5\n1 1 3\n2 4 20\n2 2 1\n1 3 4\n1 4 2"], "outputs": ["5\n", "8\n", "9\n", "5\n", "5\n", "8", "8\n", "29\n"]}
559
252
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You have some coins.  The i-th coin has a probability prob[i] of facing heads when tossed. Return the probability that the number of coins facing heads equals target if you toss every coin exactly once.   Please complete the following python code precisely: ```python class Solution: def probabilityOfHeads(self, prob: List[float], target: int) -> float: ```
{"functional": "def check(candidate):\n assert candidate(prob = [0.4], target = 1) == 0.40000\n assert candidate(prob = [0.5,0.5,0.5,0.5,0.5], target = 0) == 0.03125\n\n\ncheck(Solution().probabilityOfHeads)"}
98
87
coding
Solve the programming task below in a Python markdown code block. There is no single treatment that works for every phobia, but some people cure it by being gradually exposed to the phobic situation or object. In this kata we will try curing arachnophobia by drawing primitive spiders. Our spiders will have legs, body, eyes and a mouth. Here are some examples: ``` /\((OOwOO))/\ /╲(((0000w0000)))╱\ ^(oWo)^ ``` You will be given four values: 1) `leg size` where each value stands for its own leg type: `1 for "^ ^", 2 for "/\ /\", 3 for "/╲ ╱\", 4 for "╱╲ ╱╲"` 2) `body size` where each value stands for its own body type: `1 for "( )", 2 for "(( ))", 3 for "((( )))"` 3) `mouth` representing the spider's mouth 4) `eye` representing the spider's eye **Note:** the eyes are symmetric, and their total amount is `2 to the power of body size`. You will also be given only valid data. That's it for the instructions, you can start coding! Also feel free to reuse/extend the following starter code: ```python def draw_spider(leg_size, body_size, mouth, eye): ```
{"functional": "_inputs = [[1, 1, 'W', 'o'], [2, 2, 'w', 'O'], [3, 3, 'w', '0']]\n_outputs = [['^(oWo)^'], ['/\\\\((OOwOO))/\\\\'], ['/\u2572(((0000w0000)))\u2571\\\\']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(draw_spider(*i), o[0])"}
300
226
coding
Solve the programming task below in a Python markdown code block. Implement a class/function, which should parse time expressed as `HH:MM:SS`, or `null/nil/None` otherwise. Any extra characters, or minutes/seconds higher than 59 make the input invalid, and so should return `null/nil/None`. Also feel free to reuse/extend the following starter code: ```python def to_seconds(time): ```
{"functional": "_inputs = [['00:00:00'], ['01:02:03'], ['01:02:60'], ['01:60:03'], ['99:59:59'], ['0:00:00'], ['00:0:00'], ['00:00:0'], ['00:00:00\\n'], ['\\n00:00:00'], ['100:59:59'], ['09:059:59'], ['09:159:59'], ['09:59:059'], ['09:59:159']]\n_outputs = [[0], [3723], [None], [None], [359999], [None], [None], [None], [None], [None], [None], [None], [None], [None], [None]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(to_seconds(*i), o[0])"}
91
357
coding
Solve the programming task below in a Python markdown code block. You will be given a list of integers, $\textbf{arr}$, and a single integer $\boldsymbol{\mbox{k}}$. You must create an array of length $\boldsymbol{\mbox{k}}$ from elements of $\textbf{arr}$ such that its unfairness is minimized. Call that array $arr'$. Unfairness of an array is calculated as $max(ar r')-min(arr')$ Where: - max denotes the largest integer in $ar r'$ . - min denotes the smallest integer in $ar r'$ . Example $arr=[1,4,7,2]$ $k=2$ Pick any two elements, say $ar r'=[4,7]$. $unfairness=max(4,7)-min(4,7)=7-4=3$ Testing for all pairs, the solution $[1,2]$ provides the minimum unfairness. Note: Integers in $\textbf{arr}$ may not be unique. Function Description Complete the maxMin function in the editor below. maxMin has the following parameter(s): int k: the number of elements to select int arr[n]:: an array of integers Returns int: the minimum possible unfairness Input Format The first line contains an integer $n$, the number of elements in array $\textbf{arr}$. The second line contains an integer $\boldsymbol{\mbox{k}}$. Each of the next $n$ lines contains an integer $arr\left[i\right]$ where $0\leq i<n$. Constraints $2\leq n\leq10^5$ $2\leq k\leq n$ $0\leq ar r[i]\leq10^9$ Sample Input 0 7 3 10 100 300 200 1000 20 30 Sample Output 0 20 Explanation 0 Here $k=3$; selecting the $3$ integers $10,20,30$, unfairness equals max(10,20,30) - min(10,20,30) = 30 - 10 = 20 Sample Input 1 10 4 1 2 3 4 10 20 30 40 100 200 Sample Output 1 3 Explanation 1 Here $k=4$; selecting the $4$ integers $1,2,3,4$, unfairness equals max(1,2,3,4) - min(1,2,3,4) = 4 - 1 = 3 Sample Input 2 5 2 1 2 1 2 1 Sample Output 2 0 Explanation 2 Here $k=2$. $arr'=[2,2]$ or $arr'=[1,1]$ give the minimum unfairness of $0$.
{"inputs": ["5\n2\n1\n2\n1\n2\n1\n", "7\n3\n10\n100\n300\n200\n1000\n20\n30\n", "10\n4\n1\n2\n3\n4\n10\n20\n30\n40\n100\n200\n"], "outputs": ["0\n", "20\n", "3\n"]}
675
102
coding
Solve the programming task below in a Python markdown code block. Given two strings, the first being a random string and the second being the same as the first, but with three added characters somewhere in the string (three same characters), Write a function that returns the added character ### E.g ``` string1 = "hello" string2 = "aaahello" // => 'a' ``` The above is just an example; the characters could be anywhere in the string and string2 is actually **shuffled**. ### Another example ``` string1 = "abcde" string2 = "2db2a2ec" // => '2' ``` Note that the added character could also exist in the original string ``` string1 = "aabbcc" string2 = "aacccbbcc" // => 'c' ``` You can assume that string2 will aways be larger than string1, and there will always be three added characters in string2. ```if:c Write the function `added_char()` that takes two strings and return the added character as described above. ``` ```if:javascript Write the function `addedChar()` that takes two strings and return the added character as described above. ``` Also feel free to reuse/extend the following starter code: ```python def added_char(s1, s2): ```
{"functional": "_inputs = [['hello', 'checlclo'], ['aabbcc', 'aacccbbcc'], ['abcde', '2db2a2ec']]\n_outputs = [['c'], ['c'], ['2']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(added_char(*i), o[0])"}
283
189
coding
Solve the programming task below in a Python markdown code block. Read problems statements in mandarin chinese, russian and vietnamese as well. Mike is a network administrator in a university. One of his primary responsibilities in the job is to create an effective firewall so that the students are not able to visit the blocked sites in the network. The network have access to exactly N sites. Some of these can be blocked. The names of the sites will be given in lowercase English letters. The firewall will consist of several filters. A filter is a string that should be a prefix of some blocked site, and it should not be a prefix of any unblocked site. You want to minimize the sum of length of filters in the firewall so that for each of the blocked site, there should be a filter that contains the name of blocked site(filter is a prefix of blocked site). ------ Input ------ The first line of the input contains an integer N denoting the number of sites in the network. Then N lines follow, each of them describes the site. Each line starts from the char С. If the site is unblocked, С is equal to '+', otherwise С is equal to '-'., followed by a string denoting the name of the site. ------ Output ------ If it is not possible to choose set of filters and satisfy all constraints, output a single line containing an integer -1. Otherwise, in the first line print the number of filters K in the firewall. Then print K lines, in each line print the chosen filter. Please dislpay them in lexicographical order. You can prove that answer will be unique. ------ Constraints ------ $1 ≤ N ≤ 2 * 10^{5}$ $The sum of lengths of names of sites does not exceed 2*10^{5}$ $No two sites have same name.$ ------ Subtasks ------ $Subtask #1 (30 points) 1 ≤ N, the sum of lengths of names of sites ≤ 3 * 10^{3}$ $Subtask #2 (70 points) Original constraints$ ----- Sample Input 1 ------ 4 - codeforces + codechef - youtube + google ----- Sample Output 1 ------ 2 codef y ----- explanation 1 ------ You can see that the filter "codef" is a prefix of the blocked site "codeforces", but not a prefix of approved sites "codechef" and "google". Similarly, filter "y" is also a prefix of blocked site "youtube", but not a prefix of the approved sites. You can also see that there exists at least one filter for both the blocked sites "codeforces" and "youtube".
{"inputs": ["4\n- codeforces\n+ codechef\n- youtube\n+ google"], "outputs": ["2\ncodef\ny"]}
561
31
coding
Solve the programming task below in a Python markdown code block. Alice and Bob are playing a game. There are $n$ cells in a row. Initially each cell is either red or blue. Alice goes first. On each turn, Alice chooses two neighbouring cells which contain at least one red cell, and paints that two cells white. Then, Bob chooses two neighbouring cells which contain at least one blue cell, and paints that two cells white. The player who cannot make a move loses. Find the winner if both Alice and Bob play optimally. Note that a chosen cell can be white, as long as the other cell satisfies the constraints. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of test cases follows. For each test case, the first line contains an integer $n$ ($2 \leq n \leq 5 \cdot 10^5$) — the number of cells. The second line contains a string $s$ of length $n$ — the initial state of the cells. The $i$-th cell is red if $s_i = $ R, blue if $s_i = $ B. It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^5$. -----Output----- For each test case, output the name of the winner on a separate line. -----Examples----- Input 8 3 BRB 5 RRBBB 6 RBRBRB 8 BBRRBRRB 6 BRRBRB 12 RBRBRBRBRRBB 12 RBRBRBRBBBRR 4 RBBR Output Bob Bob Alice Alice Alice Alice Bob Bob -----Note----- In the notes, the cell numbers increase from left to right. In the first testcase for Alice, she has two choices: paint the first and the second cells, or paint the second and the third cells. No matter what choice Alice makes, there will be exactly one blue cell after Alice's move. Bob just needs to paint the blue cell and its neighbour, then every cell will be white and Alice can't make a move. So Bob is the winner. In the second testcase no matter what Alice chooses, Bob can choose to paint the fourth and fifth cells in $2$ turns. In the third testcase at first, Alice paints the third and the fourth cells. It doesn't matter if Bob paints the first and the second cells or the fifth and sixth cells, as Alice can paint the other two cells. In the fourth testcase at first, Alice paints the second and the third cells. If Bob paints the fifth and the sixth cells or the fourth and the fifth cells, then Alice paints the seventh and the eighth cells. If Bob paints the seventh and the eighth cells, then Alice paints the fifth and the sixth cells. In the fifth Alice chooses the middle two cells at first, then Bob obviously has only two options, whichever variant he chooses, Alice can choose the other one and win. In the eighth no matter what Alice chooses, Bob can choose the other two symmetrical cells.
{"inputs": ["8\n3\nBRB\n5\nRRBBB\n6\nRBRBRB\n8\nBBRRBRRB\n6\nBRRBRB\n12\nRBRBRBRBRRBB\n12\nRBRBRBRBBBRR\n4\nRBBR\n"], "outputs": ["Bob\nBob\nAlice\nAlice\nAlice\nAlice\nBob\nBob\n"]}
676
89
coding
Solve the programming task below in a Python markdown code block. Chef wants you to distribute candies among $N$ kids who are sitting in a circle. However, he wants to make some kids jealous of others. Thus, he wants you to distribute candies in such a way that there is a difference of at least $K$ candies between two adjacent kids. Given the value of $N$ and $K$, you need to find the minimum number of candies you need to satisfy the given conditions, such that, each kid gets at least one candy. -----Input:----- - First line will contain $T$, the number of testcases. Then the testcases follow. - The only line of each testcase contains two space-separated integers $N$ and $K$. -----Output:----- For each test case, print a single line containing one integer ― the number of candies you need. -----Constraints----- - $1 \leq T \leq 10^6$ - $2 \leq N \leq 10^3$ - $0 \leq K \leq 10^4$ -----Sample Input:----- 1 2 1 -----Sample Output:----- 3 -----EXPLANATION:----- The minimum number of candies required is $3$. One kid needs to have $1$ candy and the other needs to have $2$ candy to have a difference of $1$ candy between them.
{"inputs": ["1\n2 1"], "outputs": ["3"]}
302
16
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You have a stack of n boxes, with widths wi, depths di, and heights hi. The boxes cannot be rotated and can only be stacked on top of one another if each box in the stack is strictly larger than the box above it in width, height, and depth. Implement a method to compute the height of the tallest possible stack. The height of a stack is the sum of the heights of each box. The input use [wi, di, hi] to represents each box. Please complete the following python code precisely: ```python class Solution: def pileBox(self, box: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(box = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]) == 6\n assert candidate(box = [[1, 1, 1], [2, 3, 4], [2, 6, 7], [3, 4, 5]]) == 10\n\n\ncheck(Solution().pileBox)"}
148
101
coding
Solve the programming task below in a Python markdown code block. You have N balls and K boxes. You want to divide the N balls into K boxes such that: Each box contains ≥ 1 balls. No two boxes contain the same number of balls. Determine if it is possible to do so. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first and only line of each test case contains two space-separated integers N and K — the number of balls and the number of boxes respectively. ------ Output Format ------ For each test case, output YES if it is possible to divide the N balls into K boxes such that the conditions are satisfied. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤N ≤10^{9}$ $1 ≤K ≤10^{4}$ ----- Sample Input 1 ------ 4 3 4 30 3 2 2 1 1 ----- Sample Output 1 ------ NO YES NO YES ----- explanation 1 ------ Test Case 1: It is not possible to divide the $3$ balls into $4$ boxes such that each box contains $≥ 1$ balls. Test Case 2: One way to divide the $30$ balls into $3$ boxes is the following: $[5, 9, 16]$. Test Case 3: It is not possible to divide the $2$ balls into $2$ boxes such that no two boxes contain the same number of balls. Test Case 4: We can divide $1$ ball into $1$ box.
{"inputs": ["4\n3 4\n30 3\n2 2\n1 1\n"], "outputs": ["NO\nYES\nNO\nYES\n"]}
389
37
coding
Solve the programming task below in a Python markdown code block. Grandma Laura came to the market to sell some apples. During the day she sold all the apples she had. But grandma is old, so she forgot how many apples she had brought to the market. She precisely remembers she had n buyers and each of them bought exactly half of the apples she had at the moment of the purchase and also she gave a half of an apple to some of them as a gift (if the number of apples at the moment of purchase was odd), until she sold all the apples she had. So each buyer took some integral positive number of apples, but maybe he didn't pay for a half of an apple (if the number of apples at the moment of the purchase was odd). For each buyer grandma remembers if she gave a half of an apple as a gift or not. The cost of an apple is p (the number p is even). Print the total money grandma should have at the end of the day to check if some buyers cheated her. -----Input----- The first line contains two integers n and p (1 ≤ n ≤ 40, 2 ≤ p ≤ 1000) — the number of the buyers and the cost of one apple. It is guaranteed that the number p is even. The next n lines contains the description of buyers. Each buyer is described with the string half if he simply bought half of the apples and with the string halfplus if grandma also gave him a half of an apple as a gift. It is guaranteed that grandma has at least one apple at the start of the day and she has no apples at the end of the day. -----Output----- Print the only integer a — the total money grandma should have at the end of the day. Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type. -----Examples----- Input 2 10 half halfplus Output 15 Input 3 10 halfplus halfplus halfplus Output 55 -----Note----- In the first sample at the start of the day the grandma had two apples. First she sold one apple and then she sold a half of the second apple and gave a half of the second apple as a present to the second buyer.
{"inputs": ["1 2\nhalfplus\n", "1 2\nhalfplus\n", "1 4\nhalfplus\n", "1 0\nhalfplus\n", "2 10\nhalf\nhalfplus\n", "2 12\nhalf\nhalfplus\n", "2 16\nhalf\nhalfplus\n", "2 10\nhalf\nhalfplus\n"], "outputs": ["1\n", "1\n", "2\n", "0\n", "15\n", "18\n", "24\n", "15\n"]}
496
126
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an array of strings words (0-indexed). In one operation, pick two distinct indices i and j, where words[i] is a non-empty string, and move any character from words[i] to any position in words[j]. Return true if you can make every string in words equal using any number of operations, and false otherwise.   Please complete the following python code precisely: ```python class Solution: def makeEqual(self, words: List[str]) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(words = [\"abc\",\"aabc\",\"bc\"]) == True\n assert candidate(words = [\"ab\",\"a\"]) == False\n\n\ncheck(Solution().makeEqual)"}
119
52
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. There are n houses evenly lined up on the street, and each house is beautifully painted. You are given a 0-indexed integer array colors of length n, where colors[i] represents the color of the ith house. Return the maximum distance between two houses with different colors. The distance between the ith and jth houses is abs(i - j), where abs(x) is the absolute value of x.   Please complete the following python code precisely: ```python class Solution: def maxDistance(self, colors: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(colors = [1,1,1,6,1,1,1]) == 3\n assert candidate(colors = [1,8,3,8,3]) == 4\n assert candidate(colors = [0,1]) == 1\n\n\ncheck(Solution().maxDistance)"}
130
77
coding
Solve the programming task below in a Python markdown code block. VK gave a problem to Chef, but Chef is too lazy, so he asked you to solve the problem for him. The statement of the problem follows. Consider an integer with $N$ digits (in decimal notation, without leading zeroes) $D_1, D_2, D_3, \dots, D_N$. Here, $D_1$ is the most significant digit and $D_N$ the least significant. The weight of this integer is defined as ∑i=2N(Di−Di−1).∑i=2N(Di−Di−1).\sum_{i=2}^N (D_i - D_{i-1})\,. You are given integers $N$ and $W$. Find the number of positive integers with $N$ digits (without leading zeroes) and weight equal to $W$. Compute this number modulo $10^9+7$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $N$ and $W$ denoting the number of digits and the required weight. -----Output----- For each test case, print a single line containing one integer — the number of $N$-digit positive integers with weight $W$, modulo $10^9+7$. -----Constraints----- - $1 \le T \le 10^5$ - $2 \le N \le 10^{18}$ - $|W| \le 300$ -----Subtasks----- Subtask #1 (20 points): - $1 \le T \le 10^3$ - $2 \le N \le 10^3$ Subtask #2 (80 points): original constraints -----Example Input----- 1 2 3 -----Example Output----- 6 -----Explanation----- Example case 1: Remember that the digits are arranged from most significant to least significant as $D_1, D_2$. The two-digit integers with weight $3$ are $14, 25, 36, 47, 58, 69$. For example, the weight of $14$ is $D_2-D_1 = 4-1 = 3$. We can see that there are no other possible numbers.
{"inputs": ["1\n2 3"], "outputs": ["6"]}
527
16
coding
Solve the programming task below in a Python markdown code block. You are given two integers $n$ and $m$. Find the $\operatorname{MEX}$ of the sequence $n \oplus 0, n \oplus 1, \ldots, n \oplus m$. Here, $\oplus$ is the bitwise XOR operator . $\operatorname{MEX}$ of the sequence of non-negative integers is the smallest non-negative integer that doesn't appear in this sequence. For example, $\operatorname{MEX}(0, 1, 2, 4) = 3$, and $\operatorname{MEX}(1, 2021) = 0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 30000$) — the number of test cases. The first and only line of each test case contains two integers $n$ and $m$ ($0 \le n, m \le 10^9$). -----Output----- For each test case, print a single integer — the answer to the problem. -----Examples----- Input 5 3 5 4 6 3 2 69 696 123456 654321 Output 4 3 0 640 530866 -----Note----- In the first test case, the sequence is $3 \oplus 0, 3 \oplus 1, 3 \oplus 2, 3 \oplus 3, 3 \oplus 4, 3 \oplus 5$, or $3, 2, 1, 0, 7, 6$. The smallest non-negative integer which isn't present in the sequence i. e. the $\operatorname{MEX}$ of the sequence is $4$. In the second test case, the sequence is $4 \oplus 0, 4 \oplus 1, 4 \oplus 2, 4 \oplus 3, 4 \oplus 4, 4 \oplus 5, 4 \oplus 6$, or $4, 5, 6, 7, 0, 1, 2$. The smallest non-negative integer which isn't present in the sequence i. e. the $\operatorname{MEX}$ of the sequence is $3$. In the third test case, the sequence is $3 \oplus 0, 3 \oplus 1, 3 \oplus 2$, or $3, 2, 1$. The smallest non-negative integer which isn't present in the sequence i. e. the $\operatorname{MEX}$ of the sequence is $0$.
{"inputs": ["5\n4 8\n2 0\n5 2\n25 331\n5594 361646\n", "5\n3 5\n4 6\n3 4\n69 696\n51217 654321\n", "5\n3 5\n2 0\n1 2\n69 696\n87131 654321\n", "5\n6 5\n2 0\n6 2\n39 696\n185099 29706\n", "5\n3 5\n2 0\n1 2\n36 696\n87131 654321\n", "5\n3 8\n2 0\n1 2\n36 696\n87131 654321\n", "5\n6 5\n2 0\n6 1\n39 974\n185099 29706\n", "5\n2 5\n1 1\n1 2\n69 696\n41425 654321\n"], "outputs": ["8\n0\n0\n320\n360448\n", "4\n3\n4\n640\n603106\n", "4\n0\n2\n640\n567296\n", "0\n0\n0\n664\n0\n", "4\n0\n2\n664\n567296\n", "8\n0\n2\n664\n567296\n", "0\n0\n0\n960\n0\n", "4\n2\n2\n640\n612898\n"]}
602
435
coding
Solve the programming task below in a Python markdown code block. There is a hallway of length $N-1$ and you have $M$ workers to clean the floor. Each worker is responsible for segment $[L_{i}, R_{i}]$, i.e., the segment starting at $L_{i}$ and ending at $R_{i}$. The segments might overlap. Every unit of length of the floor should be cleaned by at least one worker. A worker can clean $1$ unit of length of the floor in $1$ unit of time and can start from any position within their segment. A worker can also choose to move in any direction. However, the flow of each worker should be continuous, i.e, they can’t skip any portion and jump to the next one, though they can change their direction. What’s the minimum amount of time required to clean the floor, if the workers work simultaneously? ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains of $M + 1$ lines of input. First line contains $2$ space separated integers $N$, $M$, length of the hallway and number of workers. Each of the next $M$ lines contain $2$ space separated integers $L_{i}$, $R_{i}$, endpoints of the segment under $i^{th}$ worker. ------ Output: ------ For each testcase, output in a single line minimum time required to clean the hallway or $-1$ if it's not possible to clean the entire floor. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $2 ≤ N ≤ 10^{9}$ $1 ≤ M ≤ 10^{5}$ $1 ≤ L_{i} < R_{i} ≤ N$ The sum of $M$ over all tests is atmost $2*10^{5}$ ----- Sample Input 1 ------ 3 10 3 1 10 1 5 6 10 10 1 2 10 10 2 5 10 1 5 ----- Sample Output 1 ------ 3 -1 5 ----- explanation 1 ------ Case $1$: The first worker cleans the segment $[4, 7]$, the second cleans $[1, 4]$ and the third cleans $[7, 10]$ each taking $3$ units of time. So the minimum time required is $3$ units. Case $2$: Segment $[1, 2]$ is not covered by any worker and hence the entire hallway can't be cleaned. Case $3$: The first worker cleans the segment $[5, 10]$ taking $5$ units of time, and the second cleans $[1, 5]$ taking $4$ units of time. So the minimum time required is $max(5, 4) = 5$ units.
{"inputs": ["3\n10 3\n1 10\n1 5\n6 10\n10 1\n2 10\n10 2\n5 10\n1 5"], "outputs": ["3\n-1\n5"]}
630
60
coding
Solve the programming task below in a Python markdown code block. Your colleagues have been good enough(?) to buy you a birthday gift. Even though it is your birthday and not theirs, they have decided to play pass the parcel with it so that everyone has an even chance of winning. There are multiple presents, and you will receive one, but not all are nice... One even explodes and covers you in soil... strange office. To make up for this one present is a dog! Happy days! (do not buy dogs as presents, and if you do, never wrap them). Depending on the number of passes in the game (y), and the present you unwrap (x), return as follows: x == goodpresent --> return x with num of passes added to each charCode (turn to charCode, add y to each, turn back) x == crap || x == empty --> return string sorted alphabetically x == bang --> return string turned to char codes, each code reduced by number of passes and summed to a single figure x == badpresent --> return 'Take this back!' x == dog, return 'pass out from excitement y times' (where y is the value given for y). Also feel free to reuse/extend the following starter code: ```python def present(x,y): ```
{"functional": "_inputs = [['badpresent', 3], ['goodpresent', 9], ['crap', 10], ['bang', 27], ['dog', 23]]\n_outputs = [['Take this back!'], ['pxxmy{n|nw}'], ['acpr'], ['300'], ['pass out from excitement 23 times']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(present(*i), o[0])"}
266
217
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.   Please complete the following python code precisely: ```python class Solution: def partition(self, s: str) -> List[List[str]]: ```
{"functional": "def check(candidate):\n assert candidate(s = \"aab\") == [[\"a\",\"a\",\"b\"],[\"aa\",\"b\"]]\n assert candidate(s = \"a\") == [[\"a\"]]\n\n\ncheck(Solution().partition)"}
75
58
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese and Russian. Given a string S consisting of only 1s and 0s, find the number of substrings which start and end both in 1. In this problem, a substring is defined as a sequence of continuous characters S_{i}, S_{i+1}, ..., S_{j} where 1 ≤ i ≤ j ≤ N. ------ Input ------ First line contains T, the number of testcases. Each testcase consists of N(the length of string) in one line and string in second line. ------ Output ------ For each testcase, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{5}$ $Sum of N over all testcases ≤ 10^{5}$ ----- Sample Input 1 ------ 2 4 1111 5 10001 ----- Sample Output 1 ------ 10 3 ----- explanation 1 ------ Test case $1$: All substrings of this string start and end with 1. The substrings are $\{ 1, 1, 1, 1, 11, 11, 11, 111, 111, 1111 \}$. The total count of these substrings is $10$. Test case $2$: Three substrings of this string start and end with 1. These are $\{1, 1, 10001\}$.
{"inputs": ["2\n4\n1111\n5\n10001", "2\n4\n1111\n5\n10000", "2\n4\n1111\n5\n10100", "2\n4\n1011\n5\n10000", "2\n4\n1110\n5\n10100", "2\n4\n1001\n5\n10000", "2\n4\n1111\n5\n10011", "2\n4\n0001\n2\n10000"], "outputs": ["10\n3", "10\n1\n", "10\n3\n", "6\n1\n", "6\n3\n", "3\n1\n", "10\n6\n", "1\n1\n"]}
346
201
coding
Solve the programming task below in a Python markdown code block. You are given a string $s$. You have to determine whether it is possible to build the string $s$ out of strings aa, aaa, bb and/or bbb by concatenating them. You can use the strings aa, aaa, bb and/or bbb any number of times and in any order. For example: aaaabbb can be built as aa $+$ aa $+$ bbb; bbaaaaabbb can be built as bb $+$ aaa $+$ aa $+$ bbb; aaaaaa can be built as aa $+$ aa $+$ aa; abab cannot be built from aa, aaa, bb and/or bbb. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Each test case consists of one line containing the string $s$ ($1 \le |s| \le 50$), consisting of characters a and/or b. -----Output----- For each test case, print YES if it is possible to build the string $s$. Otherwise, print NO. You may print each letter in any case (for example, YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 8 aaaabbb bbaaaaabbb aaaaaa abab a b aaaab bbaaa Output YES YES YES NO NO NO NO YES -----Note----- The first four test cases of the example are described in the statement.
{"inputs": ["8\naaaabbb\nbbaaaaabbb\naaaaaa\nabab\na\nb\naaaab\nbbaaa\n", "1\nabababababababababababababababababababababababab\n"], "outputs": ["YES\nYES\nYES\nNO\nNO\nNO\nNO\nYES\n", "NO\n"]}
348
88
coding
Solve the programming task below in a Python markdown code block. A string $s$ of length $n$ can be encrypted by the following algorithm: iterate over all divisors of $n$ in decreasing order (i.e. from $n$ to $1$), for each divisor $d$, reverse the substring $s[1 \dots d]$ (i.e. the substring which starts at position $1$ and ends at position $d$). For example, the above algorithm applied to the string $s$="codeforces" leads to the following changes: "codeforces" $\to$ "secrofedoc" $\to$ "orcesfedoc" $\to$ "rocesfedoc" $\to$ "rocesfedoc" (obviously, the last reverse operation doesn't change the string because $d=1$). You are given the encrypted string $t$. Your task is to decrypt this string, i.e., to find a string $s$ such that the above algorithm results in string $t$. It can be proven that this string $s$ always exists and is unique. -----Input----- The first line of input consists of a single integer $n$ ($1 \le n \le 100$) — the length of the string $t$. The second line of input consists of the string $t$. The length of $t$ is $n$, and it consists only of lowercase Latin letters. -----Output----- Print a string $s$ such that the above algorithm results in $t$. -----Examples----- Input 10 rocesfedoc Output codeforces Input 16 plmaetwoxesisiht Output thisisexampletwo Input 1 z Output z -----Note----- The first example is described in the problem statement.
{"inputs": ["1\nz\n", "1\ny\n", "1\nx\n", "1\n{\n", "1\n|\n", "1\nz\n", "2\nir\n", "2\nir\n"], "outputs": ["z\n", "y\n", "x\n", "{\n", "|\n", "z\n", "ri\n", "ri\n"]}
384
86
coding
Solve the programming task below in a Python markdown code block. Chef's favorite game is chess. Today, he invented a new piece and wants to see its strengths. From a cell (X, Y), the new piece can move to any cell of the chessboard such that its color is different from that of (X, Y). The new piece is currently located at cell (A, B). Chef wants to calculate the minimum number of steps required to move it to (P, Q). Note: A chessboard is an 8 \times 8 square board, where the cell at the intersection of the i-th row and j-th column is denoted (i, j). Cell (i, j) is colored white if i+j is even and black if i+j is odd. ------ Input Format ------ - The first line of input contains a single integer T denoting the number of test cases. The description of the test cases follows. - Each test case consists of a single line of input, containing four space-separated integers A, B, P, Q. ------ Output Format ------ For each test case, output a single line containing one integer - the minimum number of moves taken by the new piece to reach (P, Q) from (A, B). ------ Constraints ------ $1 ≤ T ≤ 5000$ $1 ≤ A,B,P,Q ≤ 8$ ----- Sample Input 1 ------ 3 1 1 8 8 5 7 5 8 3 3 3 3 ----- Sample Output 1 ------ 2 1 0 ----- explanation 1 ------ Test Case $1$: $(1,1)$ and $(8,8)$ have the same color, so Chef cannot move the new piece in $1$ move. Chef can first move the piece from $(1,1)$ to $(8,1)$ and then from $(8,1)$ to $(8,8)$, thus taking $2$ moves. Test Case $2$: $(5,7)$ and $(5,8)$ have different colors, so Chef can move the piece from $(5,7)$ to $(5,8)$ in $1$ move.
{"inputs": ["3\n1 1 8 8\n5 7 5 8\n3 3 3 3\n"], "outputs": ["2\n1\n0\n"]}
455
42
coding
Solve the programming task below in a Python markdown code block. Joisino wants to evaluate the formula "A op B". Here, A and B are integers, and the binary operator op is either + or -. Your task is to evaluate the formula instead of her. -----Constraints----- - 1≦A,B≦10^9 - op is either + or -. -----Input----- The input is given from Standard Input in the following format: A op B -----Output----- Evaluate the formula and print the result. -----Sample Input----- 1 + 2 -----Sample Output----- 3 Since 1 + 2 = 3, the output should be 3.
{"inputs": ["0 + 2", "1 - 1", "1 - 7", "2 - 7", "1 - 4", "3 - 7", "9 - 4", "0 - 7"], "outputs": ["2\n", "0\n", "-6\n", "-5\n", "-3\n", "-4\n", "5\n", "-7\n"]}
141
86
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given two strings, word1 and word2. You want to construct a string in the following manner: Choose some non-empty subsequence subsequence1 from word1. Choose some non-empty subsequence subsequence2 from word2. Concatenate the subsequences: subsequence1 + subsequence2, to make the string. Return the length of the longest palindrome that can be constructed in the described manner. If no palindromes can be constructed, return 0. A subsequence of a string s is a string that can be made by deleting some (possibly none) characters from s without changing the order of the remaining characters. A palindrome is a string that reads the same forward as well as backward.   Please complete the following python code precisely: ```python class Solution: def longestPalindrome(self, word1: str, word2: str) -> int: ```
{"functional": "def check(candidate):\n assert candidate(word1 = \"cacb\", word2 = \"cbba\") == 5\n assert candidate(word1 = \"ab\", word2 = \"ab\") == 3\n assert candidate(word1 = \"aa\", word2 = \"bb\") == 0\n\n\ncheck(Solution().longestPalindrome)"}
200
79
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You start at the cell (rStart, cStart) of an rows x cols grid facing east. The northwest corner is at the first row and column in the grid, and the southeast corner is at the last row and column. You will walk in a clockwise spiral shape to visit every position in this grid. Whenever you move outside the grid's boundary, we continue our walk outside the grid (but may return to the grid boundary later.). Eventually, we reach all rows * cols spaces of the grid. Return an array of coordinates representing the positions of the grid in the order you visited them.   Please complete the following python code precisely: ```python class Solution: def spiralMatrixIII(self, rows: int, cols: int, rStart: int, cStart: int) -> List[List[int]]: ```
{"functional": "def check(candidate):\n assert candidate(rows = 1, cols = 4, rStart = 0, cStart = 0) == [[0,0],[0,1],[0,2],[0,3]]\n assert candidate(rows = 5, cols = 6, rStart = 1, cStart = 4) == [[1,4],[1,5],[2,5],[2,4],[2,3],[1,3],[0,3],[0,4],[0,5],[3,5],[3,4],[3,3],[3,2],[2,2],[1,2],[0,2],[4,5],[4,4],[4,3],[4,2],[4,1],[3,1],[2,1],[1,1],[0,1],[4,0],[3,0],[2,0],[1,0],[0,0]]\n\n\ncheck(Solution().spiralMatrixIII)"}
184
215
coding
Solve the programming task below in a Python markdown code block. *Debug*   function `getSumOfDigits` that takes positive integer to calculate sum of it's digits. Assume that argument is an integer. ### Example ``` 123 => 6 223 => 7 1337 => 15 ``` Also feel free to reuse/extend the following starter code: ```python def get_sum_of_digits(num): ```
{"functional": "_inputs = [[123], [223], [0]]\n_outputs = [[6], [7], [0]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(get_sum_of_digits(*i), o[0])"}
98
172
coding
Solve the programming task below in a Python markdown code block. The aim of the kata is to try to show how difficult it can be to calculate decimals of an irrational number with a certain precision. We have chosen to get a few decimals of the number "pi" using the following infinite series (Leibniz 1646–1716): PI / 4 = 1 - 1/3 + 1/5 - 1/7 + ... which gives an approximation of PI / 4. http://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80 To have a measure of the difficulty we will count how many iterations are needed to calculate PI with a given precision. There are several ways to determine the precision of the calculus but to keep things easy we will calculate to within epsilon of your language Math::PI constant. In other words we will stop the iterative process when the absolute value of the difference between our calculation and the Math::PI constant of the given language is less than epsilon. Your function returns an array or an arrayList or a string or a tuple depending on the language (See sample tests) where your approximation of PI has 10 decimals In Haskell you can use the function "trunc10Dble" (see "Your solution"); in Clojure you can use the function "round" (see "Your solution");in OCaml or Rust the function "rnd10" (see "Your solution") in order to avoid discussions about the result. Example : ``` your function calculates 1000 iterations and 3.140592653839794 but returns: iter_pi(0.001) --> [1000, 3.1405926538] ``` Unfortunately, this series converges too slowly to be useful, as it takes over 300 terms to obtain a 2 decimal place precision. To obtain 100 decimal places of PI, it was calculated that one would need to use at least 10^50 terms of this expansion! About PI : http://www.geom.uiuc.edu/~huberty/math5337/groupe/expresspi.html Also feel free to reuse/extend the following starter code: ```python def iter_pi(epsilon): ```
{"functional": "_inputs = [[0.1], [0.01], [0.001], [0.0001], [1e-05], [1e-06]]\n_outputs = [[[10, 3.0418396189]], [[100, 3.1315929036]], [[1000, 3.1405926538]], [[10000, 3.1414926536]], [[100001, 3.1416026535]], [[1000001, 3.1415936536]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(iter_pi(*i), o[0])"}
498
311
coding
Solve the programming task below in a Python markdown code block. Read problems statements in [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. A permutation is an array consisting of $N$ distinct integers from $1$ to $N$ in arbitrary order. Chef has two permutation arrays $A$ and $P$ of length $N$. Chef performs the following three types of operations on the array $A$: $1$ : Permute the array $A$ according to the order defined by permutation $P$.That is, replace $A$ with the array $B$ where $B[P[i]]=A[i]$. $2 \ x \ y$ : Swap the elements at positions $x$ and $y$ in $A$. $3 \ x$ : Output the element at position $x$ in array $A$. For each operation of type $3$, Chef wants you to output the element at position $x$ in array $A$ after all preceding operations have been applied. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. The first line of each test case contains a single integer $N$. The second line contains $N$ integers denoting elements of array $A$. The third line contains $N$ integers denoting elements of array $P$. The fourth line contains a single integer $Q$, denoting the number of operations to be performed. $Q$ lines follow, each describing an operation of one of the three types above. ------ Output ------ For each test case, for each query of type $3$, output the answer in a separate line. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N, Q ≤ 10^{5}$ $1 ≤ A[i],P[i], x, y ≤ N$ $A$ and $P$ are permutations. $x\ne y$ in type $2$ operations. The sum of $N$ and the sum of $Q$ over all test cases do not exceed $10^{5}$. ----- Sample Input 1 ------ 1 3 1 2 3 2 3 1 3 1 2 2 3 3 1 ----- Sample Output 1 ------ 3 ----- explanation 1 ------ Initially, array $A$ is $[1,2,3]$. It changes as follows: $[1,2,3]$ -> $[3,1,2]$ -> $[3,2,1]$. So, we output element at position $1$ in array $A$, which is $3$.
{"inputs": ["1\n 3\n 1 2 3\n 2 3 1\n 3\n 1\n 2 2 3\n 3 1"], "outputs": ["3"]}
568
54
coding
Solve the programming task below in a Python markdown code block. Ezio can manipulate at most X number of guards with the apple of eden. Given that there are Y number of guards, predict if he can safely manipulate all of them. ------ Input Format ------ - First line will contain T, number of test cases. Then the test cases follow. - Each test case contains of a single line of input, two integers X and Y. ------ Output Format ------ For each test case, print \texttt{YES} if it is possible for Ezio to manipulate all the guards. Otherwise, print \texttt{NO}. You may print each character of the string in uppercase or lowercase (for example, the strings \texttt{YeS}, \texttt{yEs}, \texttt{yes} and \texttt{YES} will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ X, Y ≤ 100$ ----- Sample Input 1 ------ 3 5 7 6 6 9 1 ----- Sample Output 1 ------ NO YES YES ----- explanation 1 ------ Test Case $1$: Ezio can manipulate at most $5$ guards. Since there are $7$ guards, he cannot manipulate all of them. Test Case $2$: Ezio can manipulate at most $6$ guards. Since there are $6$ guards, he can manipulate all of them. Test Case $3$: Ezio can manipulate at most $9$ guards. Since there is only $1$ guard, he can manipulate the guard.
{"inputs": ["3\n5 7\n6 6\n9 1"], "outputs": ["NO\nYES\nYES"]}
342
28
coding
Solve the programming task below in a Python markdown code block. This is the easy version of the problem. The only difference is that in this version $n \leq 2000$. You can make hacks only if both versions of the problem are solved. There are $n$ potions in a line, with potion $1$ on the far left and potion $n$ on the far right. Each potion will increase your health by $a_i$ when drunk. $a_i$ can be negative, meaning that potion will decrease will health. You start with $0$ health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative. What is the largest number of potions you can drink? -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 2000$) — the number of potions. The next line contains $n$ integers $a_1$, $a_2$, ... ,$a_n$ ($-10^9 \leq a_i \leq 10^9$) which represent the change in health after drinking that potion. -----Output----- Output a single integer, the maximum number of potions you can drink without your health becoming negative. -----Examples----- Input 6 4 -4 1 -3 1 -3 Output 5 -----Note----- For the sample, you can drink $5$ potions by taking potions $1$, $3$, $4$, $5$ and $6$. It is not possible to drink all $6$ potions because your health will go negative at some point
{"inputs": ["1\n0\n", "6\n4 -4 1 -3 1 -3\n", "12\n-3 -3 -7 -7 -1 -7 3 3 -2 -1 0 -7\n"], "outputs": ["1", "5", "5"]}
362
67
coding
Solve the programming task below in a Python markdown code block. Hemose was shopping with his friends Samez, AhmedZ, AshrafEzz, TheSawan and O_E in Germany. As you know, Hemose and his friends are problem solvers, so they are very clever. Therefore, they will go to all discount markets in Germany. Hemose has an array of $n$ integers. He wants Samez to sort the array in the non-decreasing order. Since it would be a too easy problem for Samez, Hemose allows Samez to use only the following operation: Choose indices $i$ and $j$ such that $1 \le i, j \le n$, and $\lvert i - j \rvert \geq x$. Then, swap elements $a_i$ and $a_j$. Can you tell Samez if there's a way to sort the array in the non-decreasing order by using the operation written above some finite number of times (possibly $0$)? -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ $(1 \leq t \leq 10^5)$. Description of the test cases follows. The first line of each test case contains two integers $n$ and $x$ $(1 \leq x \leq n \leq 10^5)$. The second line of each test case contains $n$ integers $a_1, a_2, ..., a_n$ $(1 \leq a_i \leq 10^9)$. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, you should output a single string. If Samez can sort the array in non-decreasing order using the operation written above, output "YES" (without quotes). Otherwise, output "NO" (without quotes). You can print each letter of "YES" and "NO" in any case (upper or lower). -----Examples----- Input 4 3 3 3 2 1 4 3 1 2 3 4 5 2 5 1 2 3 4 5 4 1 2 3 4 4 Output NO YES YES YES -----Note----- In the first test case, you can't do any operations. In the second test case, the array is already sorted. In the third test case, you can do the operations as follows: $[5,1,2,3,4]$, $swap(a_1,a_3)$ $[2,1,5,3,4]$, $swap(a_2,a_5)$ $[2,4,5,3,1]$, $swap(a_2,a_4)$ $[2,3,5,4,1]$, $swap(a_1,a_5)$ $[1,3,5,4,2]$, $swap(a_2,a_5)$ $[1,2,5,4,3]$, $swap(a_3,a_5)$ $[1,2,3,4,5]$ (Here $swap(a_i, a_j)$ refers to swapping elements at positions $i$, $j$).
{"inputs": ["4\n3 3\n3 2 1\n4 3\n1 2 3 4\n5 2\n5 1 2 3 4\n5 4\n1 2 3 4 4\n"], "outputs": ["NO\nYES\nYES\nYES\n"]}
720
70
coding
Solve the programming task below in a Python markdown code block. Perhaps many have heard that the World Biathlon Championship has finished. Although our hero Valera was not present at this spectacular event himself and only watched it on TV, it excited him so much that he decided to enroll in a biathlon section. Of course, biathlon as any sport, proved very difficult in practice. It takes much time and effort. Workouts, workouts, and workouts, — that's what awaited Valera on his way to great achievements in biathlon. As for the workouts, you all probably know that every professional biathlete should ski fast and shoot precisely at the shooting range. Only in this case you can hope to be successful, because running and shooting are the two main components of biathlon. Valera has been diligent in his ski trainings, which is why he runs really fast, however, his shooting accuracy is nothing to write home about. On a biathlon base where Valera is preparing for the competition, there is a huge rifle range with n targets. Each target have shape of a circle, and the center of each circle is located on the Ox axis. At the last training session Valera made the total of m shots. To make monitoring of his own results easier for him, one rather well-known programmer (of course it is you) was commissioned to write a program that would reveal how many and which targets Valera hit. More specifically, for each target the program must print the number of the first successful shot (in the target), or "-1" if this was not hit. The target is considered hit if the shot is inside the circle or on its boundary. Valera is counting on you and perhaps, thanks to you he will one day win international competitions. Input The first line of the input file contains the integer n (1 ≤ n ≤ 104), which is the number of targets. The next n lines contain descriptions of the targets. Each target is a circle whose center is located on the Ox axis. Each circle is given by its coordinate of the center x ( - 2·104 ≤ x ≤ 2·104) and its radius r (1 ≤ r ≤ 1000). It is guaranteed that no two targets coincide, intersect or are nested into each other, but they can touch each other. The next line contains integer m (1 ≤ m ≤ 2·105), which is the number of shots. Next m lines contain descriptions of the shots, which are points on the plane, given by their coordinates x and y ( - 2·104 ≤ x, y ≤ 2·104). All the numbers in the input are integers. Targets and shots are numbered starting from one in the order of the input. Output Print on the first line a single number, the number of targets hit by Valera. Print on the second line for each of the targets the number of its first hit or "-1" (without quotes) if this number does not exist. Separate numbers with spaces. Examples Input 3 2 1 5 2 10 1 5 0 1 1 3 3 0 4 0 4 0 Output 2 3 3 -1 Input 3 3 2 7 1 11 2 4 2 1 6 0 6 4 11 2 Output 3 1 2 4
{"inputs": ["2\n0 5\n10 5\n2\n7 2\n6 1\n", "2\n0 5\n10 5\n2\n7 2\n0 1\n", "2\n0 5\n10 5\n2\n9 2\n0 1\n", "2\n0 5\n10 7\n2\n9 2\n0 1\n", "3\n1 1\n3 1\n-4 2\n1\n-4 -3\n", "3\n3 2\n7 1\n11 1\n4\n2 1\n6 0\n6 4\n11 2\n", "3\n3 2\n0 1\n11 1\n4\n2 1\n6 0\n6 4\n11 2\n", "3\n3 2\n0 1\n11 1\n4\n2 1\n6 0\n1 4\n11 2\n"], "outputs": ["1\n-1 1 \n", "2\n2 1 \n", "2\n2 1 \n", "2\n2 1 \n", "0\n-1 -1 -1 \n", "2\n1 2 -1 \n", "1\n1 -1 -1 \n", "1\n1 -1 -1 \n"]}
725
312
coding
Solve the programming task below in a Python markdown code block. Sehr Sus is an infinite hexagonal grid as pictured below, controlled by MennaFadali, ZerooCool and Hosssam. They love equilateral triangles and want to create $n$ equilateral triangles on the grid by adding some straight lines. The triangles must all be empty from the inside (in other words, no straight line or hexagon edge should pass through any of the triangles). You are allowed to add straight lines parallel to the edges of the hexagons. Given $n$, what is the minimum number of lines you need to add to create at least $n$ equilateral triangles as described? Adding two red lines results in two new yellow equilateral triangles. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. Then $t$ test cases follow. Each test case contains a single integer $n$ ($1 \le n \le 10^{9}$) — the required number of equilateral triangles. -----Output----- For each test case, print the minimum number of lines needed to have $n$ or more equilateral triangles. -----Examples----- Input 4 1 2 3 4567 Output 2 2 3 83 -----Note----- In the first and second test cases only 2 lines are needed. After adding the first line, no equilateral triangles will be created no matter where it is added. But after adding the second line, two more triangles will be created at once. In the third test case, the minimum needed is 3 lines as shown below.
{"inputs": ["1\n1\n", "2\n10\n9\n", "2\n76\n15\n", "2\n2922\n2251\n", "4\n1\n2\n3\n4567\n", "1\n1000000000\n", "3\n390\n398\n285\n", "5\n66\n6666\n666666\n66666666\n666666666\n"], "outputs": ["2\n", "4\n4\n", "11\n5\n", "67\n59\n", "2\n2\n3\n83\n", "38730\n", "25\n25\n21\n", "10\n100\n1000\n10000\n31623\n"]}
353
210
coding
Solve the programming task below in a Python markdown code block. Your task is to write a program of a simple dictionary which implements the following instructions: * insert str: insert a string str in to the dictionary * find str: if the distionary contains str, then print 'yes', otherwise print 'no' Notes Template in C Constraints * A string consists of 'A', 'C', 'G', or 'T' * 1 ≤ length of a string ≤ 12 * n ≤ 1000000 Input In the first line n, the number of instructions is given. In the following n lines, n instructions are given in the above mentioned format. Output Print yes or no for each find instruction in a line. Examples Input 5 insert A insert T insert C find G find A Output no yes Input 13 insert AAA insert AAC insert AGA insert AGG insert TTT find AAA find CCC find CCC insert CCC find CCC insert T find TTT find T Output yes no no yes yes yes
{"inputs": ["5\ninsert A\ninsert U\ninsert C\nfind G\nfind A", "5\ninsert B\ninsert T\ninsert C\nfind G\nfind A", "5\ninsert A\ninsert U\ninsert F\nfind F\nfind B", "5\ninsert A\ninsert U\ninsert D\nfind G\nfind A", "5\ninsert A\ninsert T\ninsert C\nfind G\nfind @", "5\ninsert A\ninsert U\ninsert D\nfind G\nfind B", "5\ninsert A\ninsert T\ninsert C\nfind F\nfind A", "5\ninsert @\ninsert T\ninsert C\nfind G\nfind A"], "outputs": ["no\nyes\n", "no\nno\n", "yes\nno\n", "no\nyes\n", "no\nno\n", "no\nno\n", "no\nyes\n", "no\nno\n"]}
249
199
coding
Solve the programming task below in a Python markdown code block. Chef has two ranges [A, B] and [C, D]. Chef needs to find the number of integers that belong to at least one of the ranges. Note: A range [P, Q] contains all the integers \{P, P+1, P+2, \dots, Q-1, Q\}. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of a single line containing 4 integers A, B, C, and D, as mentioned in the statement. ------ Output Format ------ For each test case, output on a new line, the number of integers that belong to at least one of the ranges. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ A ≤ B ≤ 8$ $1 ≤ C ≤ D ≤ 8$ ----- Sample Input 1 ------ 4 1 3 5 5 3 8 2 5 1 8 4 6 5 5 5 5 ----- Sample Output 1 ------ 4 7 8 1 ----- explanation 1 ------ Test case $1$: There are $4$ integers that belong to at least one of the ranges. These are $1, 2, 3,$ and $5$. Test case $2$: There are $7$ integers that belong to at least one of the ranges. These are $2, 3, 4, 5, 6, 7,$ and $8$. Test case $3$: There are $8$ integers that belong to at least one of the ranges. These are $1, 2, 3, 4, 5, 6, 7,$ and $8$. Test case $4$: There is only $1$ integer that belong to at least one of the ranges. That integer is $5$.
{"inputs": ["4\n1 3 5 5\n3 8 2 5\n1 8 4 6\n5 5 5 5\n"], "outputs": ["4\n7\n8\n1\n"]}
423
52
coding
Solve the programming task below in a Python markdown code block. In Python, a string can be split on a delimiter. Example: >>> a = "this is a string" >>> a = a.split(" ") # a is converted to a list of strings. >>> print a ['this', 'is', 'a', 'string'] Joining a string is simple: >>> a = "-".join(a) >>> print a this-is-a-string Task You are given a string. Split the string on a " " (space) delimiter and join using a - hyphen. Function Description Complete the split_and_join function in the editor below. split_and_join has the following parameters: string line: a string of space-separated words Returns string: the resulting string Input Format The one line contains a string consisting of space separated words. Sample Input this is a string Sample Output this-is-a-string
{"inputs": ["this is a string \n"], "outputs": ["this-is-a-string\n"]}
198
22
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. There are n cars traveling at different speeds in the same direction along a one-lane road. You are given an array cars of length n, where cars[i] = [positioni, speedi] represents: positioni is the distance between the ith car and the beginning of the road in meters. It is guaranteed that positioni < positioni+1. speedi is the initial speed of the ith car in meters per second. For simplicity, cars can be considered as points moving along the number line. Two cars collide when they occupy the same position. Once a car collides with another car, they unite and form a single car fleet. The cars in the formed fleet will have the same position and the same speed, which is the initial speed of the slowest car in the fleet. Return an array answer, where answer[i] is the time, in seconds, at which the ith car collides with the next car, or -1 if the car does not collide with the next car. Answers within 10-5 of the actual answers are accepted.   Please complete the following python code precisely: ```python class Solution: def getCollisionTimes(self, cars: List[List[int]]) -> List[float]: ```
{"functional": "def check(candidate):\n assert candidate(cars = [[1,2],[2,1],[4,3],[7,2]]) == [1.00000,-1.00000,3.00000,-1.00000]\n assert candidate(cars = [[3,4],[5,4],[6,3],[9,1]]) == [2.00000,1.00000,1.50000,-1.00000]\n\n\ncheck(Solution().getCollisionTimes)"}
268
136
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. A series of highways connect n cities numbered from 0 to n - 1. You are given a 2D integer array highways where highways[i] = [city1i, city2i, tolli] indicates that there is a highway that connects city1i and city2i, allowing a car to go from city1i to city2i and vice versa for a cost of tolli. You are also given an integer discounts which represents the number of discounts you have. You can use a discount to travel across the ith highway for a cost of tolli / 2 (integer division). Each discount may only be used once, and you can only use at most one discount per highway. Return the minimum total cost to go from city 0 to city n - 1, or -1 if it is not possible to go from city 0 to city n - 1.   Please complete the following python code precisely: ```python class Solution: def minimumCost(self, n: int, highways: List[List[int]], discounts: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(n = 5, highways = [[0,1,4],[2,1,3],[1,4,11],[3,2,3],[3,4,2]], discounts = 1) == 9\n assert candidate(n = 4, highways = [[1,3,17],[1,2,7],[3,2,5],[0,1,6],[3,0,20]], discounts = 20) == 8\n assert candidate(n = 4, highways = [[0,1,3],[2,3,2]], discounts = 0) == -1\n\n\ncheck(Solution().minimumCost)"}
239
155
coding
Solve the programming task below in a Python markdown code block. The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer — the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
{"inputs": ["1 1\n1337\n", "1 1\n1337\n", "5 3\n1 2 2 4 5\n", "5 3\n1 2 3 4 5\n", "5 3\n1 2 3 3 3\n", "5 3\n1 2 4 4 5\n", "5 3\n1 2 1 4 5\n", "5 3\n1 3 3 3 3\n"], "outputs": ["0\n", "0\n", "1\n", "2\n", "0\n", "2\n", "1\n", "0\n"]}
376
156
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a binary array nums (0-indexed). We define xi as the number whose binary representation is the subarray nums[0..i] (from most-significant-bit to least-significant-bit). For example, if nums = [1,0,1], then x0 = 1, x1 = 2, and x2 = 5. Return an array of booleans answer where answer[i] is true if xi is divisible by 5.   Please complete the following python code precisely: ```python class Solution: def prefixesDivBy5(self, nums: List[int]) -> List[bool]: ```
{"functional": "def check(candidate):\n assert candidate(nums = [0,1,1]) == [True,False,False]\n assert candidate(nums = [1,1,1]) == [False,False,False]\n\n\ncheck(Solution().prefixesDivBy5)"}
149
60
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given two non-negative integers num1 and num2. In one operation, if num1 >= num2, you must subtract num2 from num1, otherwise subtract num1 from num2. For example, if num1 = 5 and num2 = 4, subtract num2 from num1, thus obtaining num1 = 1 and num2 = 4. However, if num1 = 4 and num2 = 5, after one operation, num1 = 4 and num2 = 1. Return the number of operations required to make either num1 = 0 or num2 = 0.   Please complete the following python code precisely: ```python class Solution: def countOperations(self, num1: int, num2: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(num1 = 2, num2 = 3) == 3\n assert candidate(num1 = 10, num2 = 10) == 1\n\n\ncheck(Solution().countOperations)"}
183
59
coding
Solve the programming task below in a Python markdown code block. The chef was searching for his pen in the garage but he found his old machine with a display and some numbers on it. If some numbers entered then some different output occurs on the display. Chef wants to crack the algorithm that the machine is following. Example to identify the pattern : Input Output 9 36 5 10 1 0 2 1 -----Input:----- - First-line will contain $T$, the number of test cases. Then the test cases follow. - Each test case contains a single line of input, $N$. -----Output:----- For each test case, output in a single line answer as displayed on the screen. -----Constraints----- - $1 \leq T \leq 10^6$ - $1 \leq N \leq 10^6$ -----Sample Input:----- 1 7 -----Sample Output:----- 21
{"inputs": ["1\n7"], "outputs": ["21"]}
207
15
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a m x n matrix mat and an integer k, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for: i - k <= r <= i + k, j - k <= c <= j + k, and (r, c) is a valid position in the matrix.   Please complete the following python code precisely: ```python class Solution: def matrixBlockSum(self, mat: List[List[int]], k: int) -> List[List[int]]: ```
{"functional": "def check(candidate):\n assert candidate(mat = [[1,2,3],[4,5,6],[7,8,9]], k = 1) == [[12,21,16],[27,45,33],[24,39,28]]\n assert candidate(mat = [[1,2,3],[4,5,6],[7,8,9]], k = 2) == [[45,45,45],[45,45,45],[45,45,45]]\n\n\ncheck(Solution().matrixBlockSum)"}
126
140
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given two integer arrays, preorder and postorder where preorder is the preorder traversal of a binary tree of distinct values and postorder is the postorder traversal of the same tree, reconstruct and return the binary tree. If there exist multiple answers, you can return any of them.   Please complete the following python code precisely: ```python # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def constructFromPrePost(self, preorder: List[int], postorder: List[int]) -> Optional[TreeNode]: ```
{"functional": "def check(candidate):\n assert is_same_tree(candidate(preorder = [1,2,4,5,3,6,7], postorder = [4,5,2,6,7,3,1]), tree_node([1,2,3,4,5,6,7]))\n\n\ncheck(Solution().constructFromPrePost)"}
164
82
coding
Solve the programming task below in a Python markdown code block. We have a sandglass that runs for X seconds. The sand drops from the upper bulb at a rate of 1 gram per second. That is, the upper bulb initially contains X grams of sand. How many grams of sand will the upper bulb contains after t seconds? -----Constraints----- - 1≤X≤10^9 - 1≤t≤10^9 - X and t are integers. -----Input----- The input is given from Standard Input in the following format: X t -----Output----- Print the number of sand in the upper bulb after t second. -----Sample Input----- 100 17 -----Sample Output----- 83 17 out of the initial 100 grams of sand will be consumed, resulting in 83 grams.
{"inputs": ["3 0", "1 0", "2 0", "4 0", "48 0", "4 -2", "6 -2", "7 -4"], "outputs": ["3\n", "1\n", "2\n", "4\n", "48\n", "6\n", "8\n", "11\n"]}
176
81
coding
Solve the programming task below in a Python markdown code block. Given an integer N not less than 3, find the sum of the interior angles of a regular polygon with N sides. Print the answer in degrees, but do not print units. Constraints * 3 \leq N \leq 100 Input Input is given from Standard Input in the following format: N Output Print an integer representing the sum of the interior angles of a regular polygon with N sides. Examples Input 3 Output 180 Input 100 Output 17640
{"inputs": ["5", "9", "7", "2", "4", "8", "6", "3"], "outputs": ["540\n", "1260\n", "900\n", "0\n", "360\n", "1080\n", "720\n", "180"]}
127
77
coding
Solve the programming task below in a Python markdown code block. This is the easy version of this problem. The only difference between easy and hard versions is the constraints on $k$ and $m$ (in this version $k=2$ and $m=3$). Also, in this version of the problem, you DON'T NEED to output the answer by modulo. You are given a sequence $a$ of length $n$ consisting of integers from $1$ to $n$. The sequence may contain duplicates (i.e. some elements can be equal). Find the number of tuples of $m = 3$ elements such that the maximum number in the tuple differs from the minimum by no more than $k = 2$. Formally, you need to find the number of triples of indices $i < j < z$ such that $$\max(a_i, a_j, a_z) - \min(a_i, a_j, a_z) \le 2.$$ For example, if $n=4$ and $a=[1,2,4,3]$, then there are two such triples ($i=1, j=2, z=4$ and $i=2, j=3, z=4$). If $n=4$ and $a=[1,1,1,1]$, then all four possible triples are suitable. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^5$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the sequence $a$. The next line contains $n$ integers $a_1, a_2,\ldots, a_n$ ($1 \le a_i \le n$) — the sequence $a$. It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$. -----Output----- Output $t$ answers to the given test cases. Each answer is the required number of triples of elements, such that the maximum value in the triple differs from the minimum by no more than $2$. Note that in difference to the hard version of the problem, you don't need to output the answer by modulo. You must output the exact value of the answer. -----Examples----- Input 4 4 1 2 4 3 4 1 1 1 1 1 1 10 5 6 1 3 2 9 8 1 2 4 Output 2 4 0 15 -----Note----- None
{"inputs": ["2\n2\n2 2\n1\n1\n", "2\n2\n2 2\n1\n1\n", "2\n2\n2 1\n1\n1\n", "3\n1\n1\n1\n1\n1\n1\n", "3\n1\n1\n1\n1\n1\n1\n", "2\n5\n1 1 1 1 1\n4\n1 1 1 1\n", "2\n5\n1 1 1 1 1\n4\n1 1 1 1\n", "2\n5\n1 1 2 1 1\n4\n1 1 1 1\n"], "outputs": ["0\n0\n", "0\n0\n", "0\n0\n", "0\n0\n0\n", "0\n0\n0\n", "10\n4\n", "10\n4\n", "10\n4\n"]}
589
213
coding
Solve the programming task below in a Python markdown code block. Xenia the beginner programmer has a sequence a, consisting of 2^{n} non-negative integers: a_1, a_2, ..., a_2^{n}. Xenia is currently studying bit operations. To better understand how they work, Xenia decided to calculate some value v for a. Namely, it takes several iterations to calculate value v. At the first iteration, Xenia writes a new sequence a_1 or a_2, a_3 or a_4, ..., a_2^{n} - 1 or a_2^{n}, consisting of 2^{n} - 1 elements. In other words, she writes down the bit-wise OR of adjacent elements of sequence a. At the second iteration, Xenia writes the bitwise exclusive OR of adjacent elements of the sequence obtained after the first iteration. At the third iteration Xenia writes the bitwise OR of the adjacent elements of the sequence obtained after the second iteration. And so on; the operations of bitwise exclusive OR and bitwise OR alternate. In the end, she obtains a sequence consisting of one element, and that element is v. Let's consider an example. Suppose that sequence a = (1, 2, 3, 4). Then let's write down all the transformations (1, 2, 3, 4) → (1 or 2 = 3, 3 or 4 = 7) → (3 xor 7 = 4). The result is v = 4. You are given Xenia's initial sequence. But to calculate value v for a given sequence would be too easy, so you are given additional m queries. Each query is a pair of integers p, b. Query p, b means that you need to perform the assignment a_{p} = b. After each query, you need to print the new value v for the new sequence a. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 17, 1 ≤ m ≤ 10^5). The next line contains 2^{n} integers a_1, a_2, ..., a_2^{n} (0 ≤ a_{i} < 2^30). Each of the next m lines contains queries. The i-th line contains integers p_{i}, b_{i} (1 ≤ p_{i} ≤ 2^{n}, 0 ≤ b_{i} < 2^30) — the i-th query. -----Output----- Print m integers — the i-th integer denotes value v for sequence a after the i-th query. -----Examples----- Input 2 4 1 6 3 5 1 4 3 4 1 2 1 2 Output 1 3 3 3 -----Note----- For more information on the bit operations, you can follow this link: http://en.wikipedia.org/wiki/Bitwise_operation
{"inputs": ["1 1\n1 1\n1 1\n", "1 1\n1 1\n1 1\n", "1 1\n2 1\n1 1\n", "2 4\n1 6 3 5\n1 4\n3 4\n1 2\n1 2\n", "2 4\n1 6 3 2\n1 4\n3 4\n1 2\n1 2\n", "2 4\n1 6 4 2\n1 4\n3 4\n1 2\n1 4\n", "2 4\n1 6 2 2\n1 4\n3 4\n1 2\n1 2\n", "2 4\n1 6 3 2\n1 4\n3 5\n1 2\n1 4\n"], "outputs": ["1\n", "1\n", "1\n", "1\n3\n3\n3\n", "5\n0\n0\n0\n", "0\n0\n0\n0\n", "4\n0\n0\n0\n", "5\n1\n1\n1\n"]}
634
260
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a palindromic string of lowercase English letters palindrome, replace exactly one character with any lowercase English letter so that the resulting string is not a palindrome and that it is the lexicographically smallest one possible. Return the resulting string. If there is no way to replace a character to make it not a palindrome, return an empty string. A string a is lexicographically smaller than a string b (of the same length) if in the first position where a and b differ, a has a character strictly smaller than the corresponding character in b. For example, "abcc" is lexicographically smaller than "abcd" because the first position they differ is at the fourth character, and 'c' is smaller than 'd'.   Please complete the following python code precisely: ```python class Solution: def breakPalindrome(self, palindrome: str) -> str: ```
{"functional": "def check(candidate):\n assert candidate(palindrome = \"abccba\") == \"aaccba\"\n assert candidate(palindrome = \"a\") == \"\"\n\n\ncheck(Solution().breakPalindrome)"}
196
52
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. A football competition has just finished. The players have been given points for scoring goals and points for committing fouls. Now, it is up to Alex to find the best player in the tournament. As a programmer, your job is to help Alex by telling him the highest number of points achieved by some player. You are given two sequences $A_{1}, A_{2}, \ldots, A_{N}$ and $B_{1}, B_{2}, \ldots, B_{N}$. For each valid $i$, player $i$ scored $A_{i}$ goals and committed $B_{i}$ fouls. For each goal, the player that scored it gets $20$ points, and for each foul, $10$ points are deducted from the player that committed it. However, if the resulting number of points of some player is negative, this player will be considered to have $0$ points instead. You need to calculate the total number of points gained by each player and tell Alex the maximum of these values. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \ldots, B_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the maximum number of points. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 150$ $0 ≤ A_{i} ≤ 50$ for each valid $i$ $0 ≤ B_{i} ≤ 50$ for each valid $i$ ------ Subtasks ------ Subtask #1 (30 points): $1 ≤ N ≤ 2$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 2 3 40 30 50 2 4 20 1 0 10 ----- Sample Output 1 ------ 800 0 ----- explanation 1 ------ Example case 1: The first player gets $800$ points for scoring goals and has $20$ points deducted for fouls. Likewise, the second player gets $560$ points and the third player gets $800$ points. The third player is the one with the maximum number of points.
{"inputs": ["2\n3\n40 30 50\n2 4 20\n1\n0\n10"], "outputs": ["800\n0"]}
605
41
coding
Solve the programming task below in a Python markdown code block. Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya calls a number almost lucky if it could be evenly divided by some lucky number. Help him find out if the given number n is almost lucky. Input The single line contains an integer n (1 ≤ n ≤ 1000) — the number that needs to be checked. Output In the only line print "YES" (without the quotes), if number n is almost lucky. Otherwise, print "NO" (without the quotes). Examples Input 47 Output YES Input 16 Output YES Input 78 Output NO Note Note that all lucky numbers are almost lucky as any number is evenly divisible by itself. In the first sample 47 is a lucky number. In the second sample 16 is divisible by 4.
{"inputs": ["4\n", "3\n", "8\n", "7\n", "2\n", "1\n", "5\n", "6\n"], "outputs": ["YES\n", "NO\n", "YES\n", "YES\n", "NO\n", "NO\n", "NO\n", "NO\n"]}
241
70
coding
Solve the programming task below in a Python markdown code block. Chef has an array A of length N. Let f(i) denote the sum A_{1} + A_{2} + \dots + A_{i} \, and let g(i) denote the sum A_{i} + A_{i + 1} + \dots + A_{N}. Chef creates another array B of length N such that B_{i} = f(i) + g(i) for all 1 ≤ i ≤ N. Now, Chef has lost the original array A and needs your help to recover it, given array B. It is guaranteed that Chef has obtained the array B from a valid array A. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the array A. - The second line of each test case contains N space-separated integers B_{1}, B_{2}, \dots, B_{N} denoting the array B. ------ Output Format ------ For each testcase, output N space separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. Note that 1 ≤ A_{i} ≤ 10^{5} must hold for all 1 ≤ i ≤ N and it is guaranteed that a valid array A that meets these constraints exists. If there are multiple answers, output any. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 10^{5}$ $1 ≤ B_{i} ≤ 2 \cdot 10^{10}$ - Th sum of $N$ over all test cases does not exceed $10^{5}$. $1 ≤ A_{i} ≤ 10^{5}$ - It is guaranteed that a valid array $A$ always exists. ----- Sample Input 1 ------ 4 1 6 3 7 8 9 4 13 15 13 14 2 25 20 ----- Sample Output 1 ------ 3 1 2 3 2 4 2 3 10 5 ----- explanation 1 ------ Test case 1: For $A = [3]$, $B = [6]$. $B_{1} = f(1) + g(1) = 3 + 3 = 6$. Test case 2: For $A = [1, 2, 3]$, $B = [7, 8, 9]$. - $B_{1} = f(1) + g(1) = \underline{1} + \underline{1 + 2 + 3} = 7$ - $B_{2} = f(2) + g(2) = \underline{1 + 2} + \underline{2 + 3} = 8$ - $B_{3} = f(3) + g(3) = \underline{1 + 2 + 3} + \underline{3} = 9$
{"inputs": ["4\n1\n6\n3\n7 8 9\n4\n13 15 13 14\n2\n25 20\n"], "outputs": ["3 \n1 2 3 \n2 4 2 3 \n10 5 \n"]}
678
71
coding
Solve the programming task below in a Python markdown code block. An array is sorted if it has no inversions A Young Boy You are given an array of $n$ positive integers $a_1,a_2,\ldots,a_n$. In one operation you do the following: Choose any integer $x$. For all $i$ such that $a_i = x$, do $a_i := 0$ (assign $0$ to $a_i$). Find the minimum number of operations required to sort the array in non-decreasing order. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $n$ positive integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order. -----Examples----- Input 5 3 3 3 2 4 1 3 1 3 5 4 1 5 3 2 4 2 4 1 2 1 1 Output 1 2 4 3 0 -----Note----- In the first test case, you can choose $x = 3$ for the operation, the resulting array is $[0, 0, 2]$. In the second test case, you can choose $x = 1$ for the first operation and $x = 3$ for the second operation, the resulting array is $[0, 0, 0, 0]$.
{"inputs": ["5\n3\n3 3 2\n4\n1 3 1 3\n5\n4 1 5 3 2\n4\n2 4 1 2\n1\n1\n"], "outputs": ["1\n2\n4\n3\n0\n"]}
425
66
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given two 0-indexed integer arrays nums1 and nums2, both of length n. You can choose two integers left and right where 0 <= left <= right < n and swap the subarray nums1[left...right] with the subarray nums2[left...right]. For example, if nums1 = [1,2,3,4,5] and nums2 = [11,12,13,14,15] and you choose left = 1 and right = 2, nums1 becomes [1,12,13,4,5] and nums2 becomes [11,2,3,14,15]. You may choose to apply the mentioned operation once or not do anything. The score of the arrays is the maximum of sum(nums1) and sum(nums2), where sum(arr) is the sum of all the elements in the array arr. Return the maximum possible score. A subarray is a contiguous sequence of elements within an array. arr[left...right] denotes the subarray that contains the elements of nums between indices left and right (inclusive).   Please complete the following python code precisely: ```python class Solution: def maximumsSplicedArray(self, nums1: List[int], nums2: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums1 = [60,60,60], nums2 = [10,90,10]) == 210\n assert candidate(nums1 = [20,40,20,70,30], nums2 = [50,20,50,40,20]) == 220\n assert candidate(nums1 = [7,11,13], nums2 = [1,1,1]) == 31\n\n\ncheck(Solution().maximumsSplicedArray)"}
292
134
coding
Solve the programming task below in a Python markdown code block. Each palindrome can be always created from the other palindromes, if a single character is also a palindrome. For example, the string "bobseesanna" can be created by some ways: * bobseesanna = bob + sees + anna * bobseesanna = bob + s + ee + s + anna * bobseesanna = b + o + b + sees + a + n + n + a ... We want to take the value of function CountPal(s) which is the number of different ways to use the palindromes to create the string s by the above method. ------ Input ------ The string s ------ Output ------ The value of function CountPal(s), taking the modulo of 1 000 000 007 (10^{9}+7) ------ Limitations ------ 0 < |s| ≤ 1000 ----- Sample Input 1 ------ bobseesanna ----- Sample Output 1 ------ 18
{"inputs": ["bobseesanna"], "outputs": ["18"]}
228
16
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time, return that integer.   Please complete the following python code precisely: ```python class Solution: def findSpecialInteger(self, arr: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(arr = [1,2,2,6,6,6,6,7,10]) == 6\n\n\ncheck(Solution().findSpecialInteger)"}
84
49
coding
Solve the programming task below in a Python markdown code block. Consider the string `"adfa"` and the following rules: ```Pearl a) each character MUST be changed either to the one before or the one after in alphabet. b) "a" can only be changed to "b" and "z" to "y". ``` From our string, we get: ```Pearl "adfa" -> ["begb","beeb","bcgb","bceb"] Another example: "bd" -> ["ae","ac","ce","cc"] --We see that in each example, one of the possibilities is a palindrome. ``` I was working on the code for this but I couldn't quite figure it out. So far I have: ```python def solve(st): return [all(ord(x) - ord(y) in ["FIX"] for x, y in zip(st, st[::-1]))][0] ``` I'm not sure what three numbers go into the array labelled `["FIX"]`. This is the only thing missing. You will be given a lowercase string and your task is to return `True` if at least one of the possiblities is a palindrome or `False` otherwise. You can use your own code or fix mine. More examples in test cases. Good luck! Also feel free to reuse/extend the following starter code: ```python def solve(st): ```
{"functional": "_inputs = [['abba'], ['abaazaba'], ['abccba'], ['adfa'], ['ae'], ['abzy'], ['ababbaba'], ['sq'], ['kxbkwgyydkcbtjcosgikfdyhuuprubpwthgflucpyylbofvqxkkvqthmdnywpaunfihvupbwpruwfybdmgeuocltdaidyyewmbzm']]\n_outputs = [[True], [False], [True], [True], [False], [False], [True], [True], [True]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(solve(*i), o[0])"}
298
265
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a string s. A split is called good if you can split s into two non-empty strings sleft and sright where their concatenation is equal to s (i.e., sleft + sright = s) and the number of distinct letters in sleft and sright is the same. Return the number of good splits you can make in s.   Please complete the following python code precisely: ```python class Solution: def numSplits(self, s: str) -> int: ```
{"functional": "def check(candidate):\n assert candidate(s = \"aacaba\") == 2\n assert candidate(s = \"abcd\") == 1\n assert candidate(s = \"aaaaa\") == 4\n assert candidate(s = \"acbadbaada\") == 2\n\n\ncheck(Solution().numSplits)"}
123
73
coding
Solve the programming task below in a Python markdown code block. Watson asks Sherlock: Given a string S of N 0's and M 1's, how many unique permutations of this string start with 1? Help Sherlock by printing the answer modulo (10^{9}+7). Input Format First line contains T, the number of test cases. Each test case consists of N and M separated by a space. Output Format For each test case, print the answer modulo (10^{9}+7). Constraints 1 ≤ T ≤ 200 1 ≤ N,M ≤ 1000 Sample Input 2 1 1 2 3 Sample Output 1 6 Explanation Test1: Out of all unique permutations ie. 01 and 10, only second permutation satisfies. Hence, output is 1. Test2: Out of all unique permutations ie. 00111 01011 01101 01110 10011 10101 10110 11001 11010 11100, only 10011 10101 10110 11001 11010 11100 satisfy. Hence, output is 6.
{"inputs": ["2\n1 1\n2 3\n"], "outputs": ["1\n6\n"]}
305
24
coding
Solve the programming task below in a Python markdown code block. Polycarp is a big lover of killing time in social networks. A page with a chatlist in his favourite network is made so that when a message is sent to some friend, his friend's chat rises to the very top of the page. The relative order of the other chats doesn't change. If there was no chat with this friend before, then a new chat is simply inserted to the top of the list. Assuming that the chat list is initially empty, given the sequence of Polycaprus' messages make a list of chats after all of his messages are processed. Assume that no friend wrote any message to Polycarpus. -----Input----- The first line contains integer n (1 ≤ n ≤ 200 000) — the number of Polycarpus' messages. Next n lines enlist the message recipients in the order in which the messages were sent. The name of each participant is a non-empty sequence of lowercase English letters of length at most 10. -----Output----- Print all the recipients to who Polycarp talked to in the order of chats with them, from top to bottom. -----Examples----- Input 4 alex ivan roman ivan Output ivan roman alex Input 8 alina maria ekaterina darya darya ekaterina maria alina Output alina maria ekaterina darya -----Note----- In the first test case Polycarpus first writes to friend by name "alex", and the list looks as follows: alex Then Polycarpus writes to friend by name "ivan" and the list looks as follows: ivan alex Polycarpus writes the third message to friend by name "roman" and the list looks as follows: roman ivan alex Polycarpus writes the fourth message to friend by name "ivan", to who he has already sent a message, so the list of chats changes as follows: ivan roman alex
{"inputs": ["1\nwdi\n", "1\nwdi\n", "1\nwci\n", "1\nwcj\n", "1\nwjc\n", "1\ncjw\n", "2\naa\na\n", "2\naa\na\n"], "outputs": ["wdi\n", "wdi\n", "wci\n", "wcj\n", "wjc\n", "cjw\n", "a\naa\n", "a\naa\n"]}
440
107
coding
Solve the programming task below in a Python markdown code block. Farmer Bob have a big farm, where he growths chickens, rabbits and cows. It is very difficult to count the number of animals for each type manually, so he diceded to buy a system to do it. But he bought a cheap system that can count only total number of heads, total number of legs and total number of horns of animals on the farm. Help Bob to figure out how many chickens, rabbits and cows does he have? All chickens have 2 legs, 1 head and no horns; all rabbits have 4 legs, 1 head and no horns; all cows have 4 legs, 1 head and 2 horns. Your task is to write a function ```Python get_animals_count(legs_number, heads_number, horns_number) ``` ```Csharp Dictionary get_animals_count(int legs_number, int heads_number, int horns_number) ``` , which returns a dictionary ```python {"rabbits" : rabbits_count, "chickens" : chickens_count, "cows" : cows_count} ``` ```Csharp new Dictionary(){{"rabbits", rabbits_count},{"chickens", chickens_count},{"cows", cows_count}} ``` Parameters `legs_number, heads_number, horns_number` are integer, all tests have valid input. Example: ```python get_animals_count(34, 11, 6); # Should return {"rabbits" : 3, "chickens" : 5, "cows" : 3} get_animals_count(154, 42, 10); # Should return {"rabbits" : 30, "chickens" : 7, "cows" : 5} ``` ```Csharp get_animals_count(34, 11, 6); //Should return Dictionary(){{"rabbits", 3},{"chickens", 5},{"cows", 3}} get_animals_count(154, 42, 10); //Should return Dictionary(){{"rabbits", 30},{"chickens", 7},{"cows", 5}} ``` Also feel free to reuse/extend the following starter code: ```python def get_animals_count(legs, heads, horns): ```
{"functional": "_inputs = [[34, 11, 6], [154, 42, 10], [74, 20, 34], [152, 38, 34], [56, 17, 0]]\n_outputs = [[{'rabbits': 3, 'chickens': 5, 'cows': 3}], [{'rabbits': 30, 'chickens': 7, 'cows': 5}], [{'rabbits': 0, 'chickens': 3, 'cows': 17}], [{'rabbits': 21, 'chickens': 0, 'cows': 17}], [{'rabbits': 11, 'chickens': 6, 'cows': 0}]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(get_animals_count(*i), o[0])"}
499
327
coding
Solve the programming task below in a Python markdown code block. problem There are the following two-player card games. * This game uses a total of 2n cards with each integer from 1 to 2n written on it. Here, n is an integer between 1 and 100. * Deal n cards to each of the two. * Put cards into play alternately one by one according to the following rules. * If there are no cards in play, you can play any card you like. * If there are cards in play, you can play a larger number of written cards than the last card in play. * If you can play a card, you must put it into play. * If there is no card that can be issued, it will be a pass and it will be the opponent's turn. At this time, there are no cards in play. * The game starts with no cards in play. * The game ends when either card is exhausted. * The number of cards the opponent has at the end of the game will be scored. Taro and Hanako will play in this game. The game starts with Taro's turn. Both of them always issue the card with the smallest number of cards that can be issued. Create a program that outputs the scores of Taro and Hanako when the cards dealt to Taro are input. input The input consists of multiple datasets. Each dataset is given in the following format. The input is n + 1 lines. The integer n is written on the first line. Each line from the 2nd line to the n + 1th line has one integer written on it, which represents the integer written on the card dealt to Taro. When n is 0, it indicates the end of input. The number of data sets does not exceed 5. output For each dataset, print Taro's score on the first line and Hanako's score on the second line. Examples Input 5 1 7 9 6 10 10 8 7 14 18 4 11 3 17 5 19 0 Output 3 0 2 0 Input None Output None
{"inputs": ["5\n2\n7\n9\n6\n8\n10\n8\n7\n14\n18\n2\n17\n3\n1\n5\n19\n0", "5\n1\n7\n9\n6\n5\n10\n8\n7\n14\n18\n2\n11\n6\n1\n5\n19\n0", "5\n1\n7\n9\n6\n4\n0\n8\n7\n13\n18\n1\n11\n6\n16\n5\n19\n0", "5\n1\n7\n2\n6\n5\n10\n8\n7\n13\n18\n1\n3\n4\n17\n5\n19\n0", "5\n1\n7\n9\n3\n4\n10\n8\n7\n14\n18\n2\n11\n6\n4\n5\n19\n0", "5\n1\n4\n9\n6\n10\n10\n8\n7\n14\n18\n4\n1\n9\n17\n5\n2\n0", "5\n1\n8\n9\n6\n4\n10\n8\n7\n14\n6\n4\n11\n10\n1\n5\n19\n0", "5\n1\n7\n9\n6\n5\n0\n8\n7\n13\n18\n1\n11\n6\n16\n5\n19\n1"], "outputs": ["2\n0\n0\n1\n", "1\n0\n0\n2\n", "0\n1\n", "0\n2\n0\n1\n", "0\n1\n0\n2\n", "2\n0\n0\n4\n", "0\n1\n0\n4\n", "1\n0\n"]}
467
414
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef is playing a card game called Blackjack. He starts with a deck of $N$ cards (numbered $1$ through $N$), where for each valid $i$, the $i$-th card has an integer $A_{i}$ written on it. Then he starts dealing the cards one by one in the order from card $1$ to card $N$. Chef wins if at some moment in time, the sum of all the cards dealt so far is between $X$ and $Y$ inclusive; if this never happens, Chef loses. We want Chef to win the game, so without him noticing, we will swap some pairs of cards (possibly none) before the game starts. Find the smallest number of swaps we need so that Chef would win the game, or determine that it is impossible to make Chef win. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains three space-separated integers $N$, $X$ and $Y$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest required number of swaps or $-1$ if Chef cannot win. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N,X,Y ≤ 10^{3}$ $X ≤ Y$ $1 ≤ A_{i} ≤ 10^{3}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $10^{4}$ ------ Subtasks ------ Subtask #1 (22 points): Chef can win if we make up to $2$ swaps Subtask #2 (34 points): $1 ≤ N,X,Y ≤ 400$ $1 ≤ A_{i} ≤ 400$ for each valid $i$ the sum of $N$ over all test cases does not exceed $4,000$ Subtask #3 (44 points): original constraints ----- Sample Input 1 ------ 3 3 4 5 1 2 3 3 3 4 1 2 3 2 20 30 40 10 ----- Sample Output 1 ------ 1 0 -1 ----- explanation 1 ------ Example case 1: We can swap the last card with one of the first two cards. Example case 2: No swaps are necessary, Chef wins after dealing the first two cards. Example case 3: The order of the cards in the deck does not matter, Chef cannot win.
{"inputs": ["3\n3 4 5\n1 2 3\n3 3 4\n1 2 3\n2 20 30\n40 10"], "outputs": ["1\n0\n-1"]}
636
55
coding
Solve the programming task below in a Python markdown code block. A superhero fights with a monster. The battle consists of rounds, each of which lasts exactly $n$ minutes. After a round ends, the next round starts immediately. This is repeated over and over again. Each round has the same scenario. It is described by a sequence of $n$ numbers: $d_1, d_2, \dots, d_n$ ($-10^6 \le d_i \le 10^6$). The $i$-th element means that monster's hp (hit points) changes by the value $d_i$ during the $i$-th minute of each round. Formally, if before the $i$-th minute of a round the monster's hp is $h$, then after the $i$-th minute it changes to $h := h + d_i$. The monster's initial hp is $H$. It means that before the battle the monster has $H$ hit points. Print the first minute after which the monster dies. The monster dies if its hp is less than or equal to $0$. Print -1 if the battle continues infinitely. -----Input----- The first line contains two integers $H$ and $n$ ($1 \le H \le 10^{12}$, $1 \le n \le 2\cdot10^5$). The second line contains the sequence of integers $d_1, d_2, \dots, d_n$ ($-10^6 \le d_i \le 10^6$), where $d_i$ is the value to change monster's hp in the $i$-th minute of a round. -----Output----- Print -1 if the superhero can't kill the monster and the battle will last infinitely. Otherwise, print the positive integer $k$ such that $k$ is the first minute after which the monster is dead. -----Examples----- Input 1000 6 -100 -200 -300 125 77 -4 Output 9 Input 1000000000000 5 -1 0 0 0 0 Output 4999999999996 Input 10 4 -3 -6 5 4 Output -1
{"inputs": ["1 1\n-1\n", "1 1\n-1\n", "1 2\n1 -1\n", "1 2\n-1 1\n", "1 2\n-1 1\n", "1 2\n1 -1\n", "2 2\n1 -1\n", "1 1\n-1000000\n"], "outputs": ["1\n", "1\n", "-1\n", "1\n", "1\n", "-1\n", "-1\n", "1\n"]}
510
123
coding
Solve the programming task below in a Python markdown code block. A string is called palindrome if it reads the same from left to right and from right to left. For example "kazak", "oo", "r" and "mikhailrubinchikkihcniburliahkim" are palindroms, but strings "abb" and "ij" are not. You are given string s consisting of lowercase Latin letters. At once you can choose any position in the string and change letter in that position to any other lowercase letter. So after each changing the length of the string doesn't change. At first you can change some letters in s. Then you can permute the order of letters as you want. Permutation doesn't count as changes. You should obtain palindrome with the minimal number of changes. If there are several ways to do that you should get the lexicographically (alphabetically) smallest palindrome. So firstly you should minimize the number of changes and then minimize the palindrome lexicographically. -----Input----- The only line contains string s (1 ≤ |s| ≤ 2·10^5) consisting of only lowercase Latin letters. -----Output----- Print the lexicographically smallest palindrome that can be obtained with the minimal number of changes. -----Examples----- Input aabc Output abba Input aabcd Output abcba
{"inputs": ["u\n", "u\n", "v\n", "w\n", "zza\n", "abb\n", "zaz\n", "acc\n"], "outputs": ["u\n", "u\n", "v\n", "w\n", "zaz\n", "bab\n", "zaz\n", "cac\n"]}
281
73
coding
Solve the programming task below in a Python markdown code block. There are $n$ variables and $m$ requirements. Requirements are represented as $\left(x\leq y\right)$, meaning that the $x^{th}$ variable must be less than or equal to the $y^{th}$ variable. Your task is to assign non-negative numbers smaller than $10$ to each variable and then calculate the number of different assignments satisfying all requirements. Two assignments are different if and only if at least one variable is assigned to a different number in both assignments. Print your answer modulo $10^3+7$. Input Format The first line contains $2$ space-separated integers, $n$ and $m$, respectively. Each of the $m$ subsequent lines contains $2$ space-seperated integers describing the respective $\boldsymbol{x}$ and $y$ values for an $\left(x\leq y\right)$ requirement. Constraints $0<n<14$ $0\lt m\lt200$ $0\leq x,y<n$ Output Format Print your answer modulo $10^3+7$. Sample Input 0 6 7 1 3 0 1 2 4 0 4 2 5 3 4 0 2 Sample Output 0 1000 Explanation 0 There are $\boldsymbol{6}$ variables and $7$ requirements. Let the variables be in the array $a\left[6\right]$. Requirements are - $a[1]<=a[3],a[0]<=a[1],a[2]<=a[4],a[0]<=a[4],a[2]<=a[5],a[3]<=a[4],a[0]<=a[2]$ One of the assignments is - $\{1,2,3,4,5,6\}$ Similarly there are $25168$ assignments possible. Result = $25168\:\text{mod}\:1007=1000$.
{"inputs": ["6 7\n1 3\n0 1\n2 4\n0 4\n2 5\n3 4\n0 2\n"], "outputs": ["1000\n"]}
444
47
coding
Solve the programming task below in a Python markdown code block. Well, the series which Stepan watched for a very long time, ended. In total, the series had n episodes. For each of them, Stepan remembers either that he definitely has watched it, or that he definitely hasn't watched it, or he is unsure, has he watched this episode or not. Stepan's dissatisfaction is the maximum number of consecutive series that Stepan did not watch. Your task is to determine according to Stepan's memories if his dissatisfaction could be exactly equal to k. -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 100, 0 ≤ k ≤ n) — the number of episodes in the series and the dissatisfaction which should be checked. The second line contains the sequence which consists of n symbols "Y", "N" and "?". If the i-th symbol equals "Y", Stepan remembers that he has watched the episode number i. If the i-th symbol equals "N", Stepan remembers that he hasn't watched the epizode number i. If the i-th symbol equals "?", Stepan doesn't exactly remember if he has watched the episode number i or not. -----Output----- If Stepan's dissatisfaction can be exactly equal to k, then print "YES" (without qoutes). Otherwise print "NO" (without qoutes). -----Examples----- Input 5 2 NYNNY Output YES Input 6 1 ????NN Output NO -----Note----- In the first test Stepan remembers about all the episodes whether he has watched them or not. His dissatisfaction is 2, because he hasn't watch two episodes in a row — the episode number 3 and the episode number 4. The answer is "YES", because k = 2. In the second test k = 1, Stepan's dissatisfaction is greater than or equal to 2 (because he remembers that he hasn't watch at least two episodes in a row — number 5 and number 6), even if he has watched the episodes from the first to the fourth, inclusive.
{"inputs": ["1 1\nY\n", "1 1\nN\n", "1 1\n?\n", "1 0\n?\n", "1 0\nN\n", "1 0\nY\n", "1 0\nY\n", "1 1\nY\n"], "outputs": ["NO\n", "YES\n", "YES\n", "YES\n", "NO\n", "YES\n", "YES\n", "NO\n"]}
442
102
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. In a linked list of size n, where n is even, the ith node (0-indexed) of the linked list is known as the twin of the (n-1-i)th node, if 0 <= i <= (n / 2) - 1. For example, if n = 4, then node 0 is the twin of node 3, and node 1 is the twin of node 2. These are the only nodes with twins for n = 4. The twin sum is defined as the sum of a node and its twin. Given the head of a linked list with even length, return the maximum twin sum of the linked list.   Please complete the following python code precisely: ```python # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def pairSum(self, head: Optional[ListNode]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(head = list_node([5,4,2,1])) == 6\n assert candidate(head = list_node([4,2,2,3])) == 7\n assert candidate(head = list_node([1,100000])) == 100001\n\n\ncheck(Solution().pairSum)"}
228
85
coding
Solve the programming task below in a Python markdown code block. Little Artem found a grasshopper. He brought it to his house and constructed a jumping area for him. The area looks like a strip of cells 1 × n. Each cell contains the direction for the next jump and the length of that jump. Grasshopper starts in the first cell and follows the instructions written on the cells. Grasshopper stops immediately if it jumps out of the strip. Now Artem wants to find out if this will ever happen. Input The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — length of the strip. Next line contains a string of length n which consists of characters "<" and ">" only, that provide the direction of the jump from the corresponding cell. Next line contains n integers di (1 ≤ di ≤ 109) — the length of the jump from the i-th cell. Output Print "INFINITE" (without quotes) if grasshopper will continue his jumps forever. Otherwise print "FINITE" (without quotes). Examples Input 2 &gt;&lt; 1 2 Output FINITE Input 3 &gt;&gt;&lt; 2 1 1 Output INFINITE Note In the first sample grasshopper starts from the first cell and jumps to the right on the next cell. When he is in the second cell he needs to jump two cells left so he will jump out of the strip. Second sample grasshopper path is 1 - 3 - 2 - 3 - 2 - 3 and so on. The path is infinite.
{"inputs": ["2\n><\n5 5\n", "2\n>>\n1 1\n", "2\n><\n1 2\n", "2\n<>\n5 5\n", "2\n?>\n1 1\n", "2\n<>\n1 2\n", "2\n<>\n4 5\n", "2\n?>\n2 1\n"], "outputs": ["FINITE", "FINITE", "FINITE", "FINITE\n", "FINITE\n", "FINITE\n", "FINITE\n", "FINITE\n"]}
347
123
coding
Solve the programming task below in a Python markdown code block. Takahashi has a string S of length N consisting of digits from 0 through 9. He loves the prime number P. He wants to know how many non-empty (contiguous) substrings of S - there are N \times (N + 1) / 2 of them - are divisible by P when regarded as integers written in base ten. Here substrings starting with a 0 also count, and substrings originated from different positions in S are distinguished, even if they are equal as strings or integers. Compute this count to help Takahashi. -----Constraints----- - 1 \leq N \leq 2 \times 10^5 - S consists of digits. - |S| = N - 2 \leq P \leq 10000 - P is a prime number. -----Input----- Input is given from Standard Input in the following format: N P S -----Output----- Print the number of non-empty (contiguous) substrings of S that are divisible by P when regarded as an integer written in base ten. -----Sample Input----- 4 3 3543 -----Sample Output----- 6 Here S = 3543. There are ten non-empty (contiguous) substrings of S: - 3: divisible by 3. - 35: not divisible by 3. - 354: divisible by 3. - 3543: divisible by 3. - 5: not divisible by 3. - 54: divisible by 3. - 543: divisible by 3. - 4: not divisible by 3. - 43: not divisible by 3. - 3: divisible by 3. Six of these are divisible by 3, so print 6.
{"inputs": ["4 3\n2919", "4 1\n2020", "4 3\n1541", "4 3\n3041", "4 2\n3041", "4 6\n1793", "4 4\n3041", "4 2\n1173"], "outputs": ["4\n", "10\n", "2\n", "3\n", "5\n", "1\n", "6\n", "0\n"]}
400
119
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a sorted array nums of n non-negative integers and an integer maximumBit. You want to perform the following query n times: Find a non-negative integer k < 2maximumBit such that nums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k is maximized. k is the answer to the ith query. Remove the last element from the current array nums. Return an array answer, where answer[i] is the answer to the ith query.   Please complete the following python code precisely: ```python class Solution: def getMaximumXor(self, nums: List[int], maximumBit: int) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(nums = [0,1,1,3], maximumBit = 2) == [0,3,2,3]\n assert candidate(nums = [2,3,4,7], maximumBit = 3) == [5,2,6,5]\n assert candidate(nums = [0,1,2,2,5,7], maximumBit = 3) == [4,3,6,4,6,7]\n\n\ncheck(Solution().getMaximumXor)"}
158
122
coding
Solve the programming task below in a Python markdown code block. Translator's note: in Russia's most widespread grading system, there are four grades: 5, 4, 3, 2, the higher the better, roughly corresponding to A, B, C and F respectively in American grading system. The term is coming to an end and students start thinking about their grades. Today, a professor told his students that the grades for his course would be given out automatically  — he would calculate the simple average (arithmetic mean) of all grades given out for lab works this term and round to the nearest integer. The rounding would be done in favour of the student — $4.5$ would be rounded up to $5$ (as in example 3), but $4.4$ would be rounded down to $4$. This does not bode well for Vasya who didn't think those lab works would influence anything, so he may receive a grade worse than $5$ (maybe even the dreaded $2$). However, the professor allowed him to redo some of his works of Vasya's choosing to increase his average grade. Vasya wants to redo as as few lab works as possible in order to get $5$ for the course. Of course, Vasya will get $5$ for the lab works he chooses to redo. Help Vasya — calculate the minimum amount of lab works Vasya has to redo. -----Input----- The first line contains a single integer $n$ — the number of Vasya's grades ($1 \leq n \leq 100$). The second line contains $n$ integers from $2$ to $5$ — Vasya's grades for his lab works. -----Output----- Output a single integer — the minimum amount of lab works that Vasya has to redo. It can be shown that Vasya can always redo enough lab works to get a $5$. -----Examples----- Input 3 4 4 4 Output 2 Input 4 5 4 5 5 Output 0 Input 4 5 3 3 5 Output 1 -----Note----- In the first sample, it is enough to redo two lab works to make two $4$s into $5$s. In the second sample, Vasya's average is already $4.75$ so he doesn't have to redo anything to get a $5$. In the second sample Vasya has to redo one lab work to get rid of one of the $3$s, that will make the average exactly $4.5$ so the final grade would be $5$.
{"inputs": ["1\n5\n", "1\n2\n", "1\n3\n", "1\n4\n", "1\n2\n", "1\n4\n", "1\n5\n", "1\n3\n"], "outputs": ["0\n", "1\n", "1\n", "1\n", "1\n", "1\n", "0\n", "1\n"]}
566
86
coding
Solve the programming task below in a Python markdown code block. You are given four integers $a$, $b$, $x$ and $y$. Initially, $a \ge x$ and $b \ge y$. You can do the following operation no more than $n$ times: Choose either $a$ or $b$ and decrease it by one. However, as a result of this operation, value of $a$ cannot become less than $x$, and value of $b$ cannot become less than $y$. Your task is to find the minimum possible product of $a$ and $b$ ($a \cdot b$) you can achieve by applying the given operation no more than $n$ times. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains five integers $a$, $b$, $x$, $y$ and $n$ ($1 \le a, b, x, y, n \le 10^9$). Additional constraint on the input: $a \ge x$ and $b \ge y$ always holds. -----Output----- For each test case, print one integer: the minimum possible product of $a$ and $b$ ($a \cdot b$) you can achieve by applying the given operation no more than $n$ times. -----Example----- Input 7 10 10 8 5 3 12 8 8 7 2 12343 43 4543 39 123212 1000000000 1000000000 1 1 1 1000000000 1000000000 1 1 1000000000 10 11 2 1 5 10 11 9 1 10 Output 70 77 177177 999999999000000000 999999999 55 10 -----Note----- In the first test case of the example, you need to decrease $b$ three times and obtain $10 \cdot 7 = 70$. In the second test case of the example, you need to decrease $a$ one time, $b$ one time and obtain $11 \cdot 7 = 77$. In the sixth test case of the example, you need to decrease $a$ five times and obtain $5 \cdot 11 = 55$. In the seventh test case of the example, you need to decrease $b$ ten times and obtain $10 \cdot 1 = 10$.
{"inputs": ["1\n10 10 8 5 3\n", "1\n10 10 8 5 3\n", "1\n10 10 8 1 3\n", "1\n10 10 0 1 3\n", "1\n10 10 10 5 3\n", "1\n10 10 10 5 4\n", "1\n10 20 10 5 3\n", "1\n10 10 10 5 0\n"], "outputs": ["70\n", "70\n", "70\n", "70\n", "70\n", "60\n", "170\n", "100\n"]}
661
180
coding
Solve the programming task below in a Python markdown code block. Recently personal training sessions have finished in the Berland State University Olympiad Programmer Training Centre. By the results of these training sessions teams are composed for the oncoming team contest season. Each team consists of three people. All the students of the Centre possess numbers from 1 to 3n, and all the teams possess numbers from 1 to n. The splitting of students into teams is performed in the following manner: while there are people who are not part of a team, a person with the best total score is chosen among them (the captain of a new team), this person chooses for himself two teammates from those who is left according to his list of priorities. The list of every person's priorities is represented as a permutation from the rest of 3n - 1 students who attend the centre, besides himself. You are given the results of personal training sessions which are a permutation of numbers from 1 to 3n, where the i-th number is the number of student who has won the i-th place. No two students share a place. You are also given the arrangement of the already formed teams in the order in which they has been created. Your task is to determine the list of priorities for the student number k. If there are several priority lists, choose the lexicographically minimal one. Input The first line contains an integer n (1 ≤ n ≤ 105) which is the number of resulting teams. The second line contains 3n space-separated integers from 1 to 3n which are the results of personal training sessions. It is guaranteed that every student appears in the results exactly once. Then follow n lines each containing three integers from 1 to 3n — each line describes the members of a given team. The members of one team can be listed in any order, but the teams themselves are listed in the order in which they were created. It is guaranteed that the arrangement is correct, that is that every student is a member of exactly one team and those teams could really be created from the given results using the method described above. The last line contains number k (1 ≤ k ≤ 3n) which is the number of a student for who the list of priorities should be found. Output Print 3n - 1 numbers — the lexicographically smallest list of priorities for the student number k. The lexicographical comparison is performed by the standard < operator in modern programming languages. The list a is lexicographically less that the list b if exists such an i (1 ≤ i ≤ 3n), that ai < bi, and for any j (1 ≤ j < i) aj = bj. Note, that the list 1 9 10 is lexicographically less than the list 1 10 9. That is, the comparison of lists is different from the comparison of lines. Examples Input 3 5 4 1 2 6 3 7 8 9 5 6 2 9 3 4 1 7 8 4 Output 2 3 5 6 9 1 7 8 Input 3 5 4 1 2 6 3 7 8 9 5 6 2 9 3 4 1 7 8 8 Output 1 2 3 4 5 6 7 9 Input 2 4 1 3 2 5 6 4 6 5 1 2 3 4 Output 5 6 1 2 3
{"inputs": ["1\n2 3 1\n2 3 1\n2\n", "1\n2 3 1\n2 3 1\n1\n", "2\n6 4 5 1 3 2\n6 1 3\n4 5 2\n6\n", "2\n2 3 4 5 6 1\n2 3 4\n5 6 1\n1\n", "2\n4 1 3 2 5 6\n4 6 5\n1 2 3\n2\n", "2\n4 1 3 2 5 6\n4 6 5\n1 2 3\n1\n", "2\n4 1 3 2 5 6\n4 6 5\n1 2 3\n3\n", "2\n4 1 3 2 5 6\n4 6 5\n1 2 3\n5\n"], "outputs": ["1 3\n", "2 3", "1 3 2 4 5\n", "2 3 4 5 6\n", "1 3 4 5 6", "2 3 4 5 6", "1 2 4 5 6", "1 2 3 4 6"]}
761
301
coding
Solve the programming task below in a Python markdown code block. Time to win the lottery! Given a lottery ticket (ticket), represented by an array of 2-value arrays, you must find out if you've won the jackpot. Example ticket: To do this, you must first count the 'mini-wins' on your ticket. Each sub array has both a string and a number within it. If the character code of any of the characters in the string matches the number, you get a mini win. Note you can only have one mini win per sub array. Once you have counted all of your mini wins, compare that number to the other input provided (win). If your total is more than or equal to (win), return 'Winner!'. Else return 'Loser!'. All inputs will be in the correct format. Strings on tickets are not always the same length. Also feel free to reuse/extend the following starter code: ```python def bingo(ticket, win): ```
{"functional": "_inputs = [[[['ABC', 65], ['HGR', 74], ['BYHT', 74]], 2], [[['ABC', 65], ['HGR', 74], ['BYHT', 74]], 1], [[['HGTYRE', 74], ['BE', 66], ['JKTY', 74]], 3]]\n_outputs = [['Loser!'], ['Winner!'], ['Loser!']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(bingo(*i), o[0])"}
203
244
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a m x n matrix mat and an integer threshold, return the maximum side-length of a square with a sum less than or equal to threshold or return 0 if there is no such square.   Please complete the following python code precisely: ```python class Solution: def maxSideLength(self, mat: List[List[int]], threshold: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(mat = [[1,1,3,2,4,3,2],[1,1,3,2,4,3,2],[1,1,3,2,4,3,2]], threshold = 4) == 2\n assert candidate(mat = [[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2]], threshold = 1) == 0\n\n\ncheck(Solution().maxSideLength)"}
94
142
coding
Solve the programming task below in a Python markdown code block. You are given $n$ points with integer coordinates on a coordinate axis $OX$. The coordinate of the $i$-th point is $x_i$. All points' coordinates are distinct and given in strictly increasing order. For each point $i$, you can do the following operation no more than once: take this point and move it by $1$ to the left or to the right (i..e., you can change its coordinate $x_i$ to $x_i - 1$ or to $x_i + 1$). In other words, for each point, you choose (separately) its new coordinate. For the $i$-th point, it can be either $x_i - 1$, $x_i$ or $x_i + 1$. Your task is to determine if you can move some points as described above in such a way that the new set of points forms a consecutive segment of integers, i. e. for some integer $l$ the coordinates of points should be equal to $l, l + 1, \ldots, l + n - 1$. Note that the resulting points should have distinct coordinates. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of points in the set $x$. The second line of the test case contains $n$ integers $x_1 < x_2 < \ldots < x_n$ ($1 \le x_i \le 10^6$), where $x_i$ is the coordinate of the $i$-th point. It is guaranteed that the points are given in strictly increasing order (this also means that all coordinates are distinct). It is also guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — if the set of points from the test case can be moved to form a consecutive segment of integers, print YES, otherwise print NO. -----Examples----- Input 5 2 1 4 3 1 2 3 4 1 2 3 7 1 1000000 3 2 5 6 Output YES YES NO YES YES -----Note----- None
{"inputs": ["1\n1\n29999\n", "5\n2\n1 4\n3\n1 2 3\n4\n1 2 3 7\n1\n1000000\n3\n2 5 6\n"], "outputs": ["YES\n", "YES\nYES\nNO\nYES\nYES\n"]}
584
80
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. Rahul and Rashi are off to the wedding of a close relative. This time they have to travel without their guardians. Rahul got very interested in the arrangement of seats inside the train coach. The entire coach could be viewed as an arrangement of consecutive blocks of size 8. Berth Number Compartment 1 - 8 1 9 - 16 2 17 - 24 3 ... and so on Each of these size-8 blocks are further arranged as: 1LB, 2MB, 3UB, 4LB, 5MB, 6UB, 7SL, 8SU 9LB, 10MB, ... ... ... Here LB denotes lower berth, MB middle berth and UB upper berth. The following berths are called Train-Partners: 3UB | 6UB 2MB | 5MB 1LB | 4LB 7SL | 8SU and the pattern is repeated for every set of 8 berths. Rahul and Rashi are playing this game of finding the train partner of each berth. Can you write a program to do the same? ------ Input ------ The first line of input contains a single integer T, denoting the number of test cases to follow. Each of the next T lines contain a single integer N, the berth number whose neighbor is to be found out. ------ Output ------ The output should contain exactly T lines each containing the berth of the neighbor of the corresponding seat. ------ Constraints ------ ------ Subtasks ------ Subtask #1 (50 points): $1 ≤ T ≤ 8$ $1 ≤ N ≤ 8$ Subtask #2 (50 points): $1 ≤ T ≤ 100$ $1 ≤ N ≤ 500$ ----- Sample Input 1 ------ 3 1 5 3 ----- Sample Output 1 ------ 4LB 2MB 6UB
{"inputs": ["3\n1\n5\n3"], "outputs": ["4LB\n2MB\n6UB"]}
456
25
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has a sequence $A_{1}, A_{2}, \ldots, A_{N}$. He needs to find the number of pairs $(i, j)$ ($1 ≤ i < j ≤ N$) such that $A_{i} + A_{j} = A_{i} \cdot A_{j}$. However, he is busy, so he asks for your help. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the desired number of pairs. ------ Constraints ------ $1 ≤ T ≤ 20$ $1 ≤ N ≤ 40,000$ $0 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (30 points): $1 ≤ N ≤ 500$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 2 3 2 4 2 3 0 2 3 ----- Sample Output 1 ------ 1 0 ----- explanation 1 ------ Example case 1: The only valid pair is $(1, 3)$. Example case 2: We can see that no valid pair exists.
{"inputs": ["2\n3\n2 4 2\n3\n0 2 3"], "outputs": ["1\n0"]}
385
30
coding
Solve the programming task below in a Python markdown code block. In this Kata, you will create a function that converts a string with letters and numbers to the inverse of that string (with regards to Alpha and Numeric characters). So, e.g. the letter `a` will become `1` and number `1` will become `a`; `z` will become `26` and `26` will become `z`. Example: `"a25bz"` would become `"1y226"` Numbers representing letters (`n <= 26`) will always be separated by letters, for all test cases: * `"a26b"` may be tested, but not `"a262b"` * `"cjw9k"` may be tested, but not `"cjw99k"` A list named `alphabet` is preloaded for you: `['a', 'b', 'c', ...]` A dictionary of letters and their number equivalent is also preloaded for you called `alphabetnums = {'a': '1', 'b': '2', 'c': '3', ...}` Also feel free to reuse/extend the following starter code: ```python def AlphaNum_NumAlpha(string): ```
{"functional": "_inputs = [['25abcd26'], ['18zyz14'], ['a1b2c3d4'], ['5a8p17'], ['w6aa4ct24m5'], ['17dh'], ['25gj8sk6r17'], ['18zzz14'], ['y17kg5et11'], ['abcdefghijklmnopqrstuvwxyz'], ['1a2b3c4d5e6f7g8h9i10j11k12l13m14n15o16p17q18r19s20t21u22v23w24x25y26z'], ['h15q4pc6yw23nmx19y'], ['p16k11o25x7m6m20ct9'], ['nv15u19d5fq8'], ['4n3fk22en17ekve'], ['iwantamemewar'], ['7h15cc9s23l11k10sd5'], ['hd2gd14gf2sg5lm8wfv9bb13'], ['13'], ['7k7k7sg3jvh16d'], ['6h19r21a8b'], ['youaredonegoodforyou']]\n_outputs = [['y1234z'], ['r262526n'], ['1a2b3c4d'], ['e1h16q'], ['23f11d320x13e'], ['q48'], ['y710h1911f18q'], ['r262626n'], ['25q117e520k'], ['1234567891011121314151617181920212223242526'], ['a1b2c3d4e5f6g7h8i9j10k11l12m13n14o15p16q17r18s19t20u21v22w23x24y25z26'], ['8o17d163f2523w141324s25'], ['16p11k15y24g13f13t320i'], ['1422o21s4e617h'], ['d14c611v514q511225'], ['92311420113513523118'], ['g8o33i19w12k11j194e'], ['84b74n76b197e1213h23622i22m'], ['m'], ['g11g11g197c10228p4'], ['f8s18u1h2'], ['251521118541514571515461518251521']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(AlphaNum_NumAlpha(*i), o[0])"}
257
887
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a string s representing a valid expression, implement a basic calculator to evaluate it, and return the result of the evaluation. Note: You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as eval().   Please complete the following python code precisely: ```python class Solution: def calculate(self, s: str) -> int: ```
{"functional": "def check(candidate):\n assert candidate(s = \"1 + 1\") == 2\n assert candidate(s = \" 2-1 + 2 \") == 3\n assert candidate(s = \"(1+(4+5+2)-3)+(6+8)\") == 23\n\n\ncheck(Solution().calculate)"}
94
79
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.   Please complete the following python code precisely: ```python class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(nums = [1,1,1,2,2,3], k = 2) == [1,2]\n assert candidate(nums = [1], k = 1) == [1]\n\n\ncheck(Solution().topKFrequent)"}
80
68
coding
Solve the programming task below in a Python markdown code block. You are given a non-empty string s consisting of lowercase letters. Find the number of pairs of non-overlapping palindromic substrings of this string. In a more formal way, you have to find the quantity of tuples (a, b, x, y) such that 1 ≤ a ≤ b < x ≤ y ≤ |s| and substrings s[a... b], s[x... y] are palindromes. A palindrome is a string that can be read the same way from left to right and from right to left. For example, "abacaba", "z", "abba" are palindromes. A substring s[i... j] (1 ≤ i ≤ j ≤ |s|) of string s = s1s2... s|s| is a string sisi + 1... sj. For example, substring s[2...4] of string s = "abacaba" equals "bac". Input The first line of input contains a non-empty string s which consists of lowercase letters ('a'...'z'), s contains at most 2000 characters. Output Output a single number — the quantity of pairs of non-overlapping palindromic substrings of s. Please do not use the %lld format specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d format specifier. Examples Input aa Output 1 Input aaa Output 5 Input abacaba Output 36
{"inputs": ["`a\n", "a`\n", "aa\n", "aa`\n", "a`a\n", "aaa\n", "aabba\n", "bbaab\n"], "outputs": ["1\n", "1\n", "1\n", "4\n", "3\n", "5\n", "18\n", "18\n"]}
338
80
coding
Solve the programming task below in a Python markdown code block. Kuro has just learned about permutations and he is really excited to create a new permutation type. He has chosen $n$ distinct positive integers and put all of them in a set $S$. Now he defines a magical permutation to be: A permutation of integers from $0$ to $2^x - 1$, where $x$ is a non-negative integer. The bitwise xor of any two consecutive elements in the permutation is an element in $S$. Since Kuro is really excited about magical permutations, he wants to create the longest magical permutation possible. In other words, he wants to find the largest non-negative integer $x$ such that there is a magical permutation of integers from $0$ to $2^x - 1$. Since he is a newbie in the subject, he wants you to help him find this value of $x$ and also the magical permutation for that $x$. -----Input----- The first line contains the integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the number of elements in the set $S$. The next line contains $n$ distinct integers $S_1, S_2, \ldots, S_n$ ($1 \leq S_i \leq 2 \cdot 10^5$) — the elements in the set $S$. -----Output----- In the first line print the largest non-negative integer $x$, such that there is a magical permutation of integers from $0$ to $2^x - 1$. Then print $2^x$ integers describing a magical permutation of integers from $0$ to $2^x - 1$. If there are multiple such magical permutations, print any of them. -----Examples----- Input 3 1 2 3 Output 2 0 1 3 2 Input 2 2 3 Output 2 0 2 1 3 Input 4 1 2 3 4 Output 3 0 1 3 2 6 7 5 4 Input 2 2 4 Output 0 0 Input 1 20 Output 0 0 Input 1 1 Output 1 0 1 -----Note----- In the first example, $0, 1, 3, 2$ is a magical permutation since: $0 \oplus 1 = 1 \in S$ $1 \oplus 3 = 2 \in S$ $3 \oplus 2 = 1 \in S$ Where $\oplus$ denotes bitwise xor operation.
{"inputs": ["1\n1", "1\n20", "1\n7\n", "1\n2\n", "1\n4\n", "1\n5\n", "1\n8\n", "1\n3\n"], "outputs": ["1\n0 1", "0\n0", "0\n0\n", "0\n0\n", "0\n0\n", "0\n0\n", "0\n0\n", "0\n0\n"]}
578
101
coding
Solve the programming task below in a Python markdown code block. Like all minions Dave also like to eat bananas. So as to his obsession, he is eating banana, but this time in different way. There are N bananas in dish. Dave wants to eat all of them. He starts eating at speed 1 banana/second. He can increase or decrease speed of eating by one only. The condition is that he must end eating at speed 1 banana/second. So, your task is to find minimum time required to finish all bananas in a dish. -----Input----- First line contain number of test cases T. Next T lines contain one integer N. Where N is number of bananas in a dish. -----Output----- For each test case print minimum time require to eat all N bananas. -----Constraints----- - 1 ≤ T ≤ 100 - 1 ≤ N ≤ 105 -----Example----- Input: 2 2 4 Output: 2 3 -----Explanation----- Example case 1.Dave will start to eat with rate of 1 banana/second. Then only one banana will remain. He will eat that banana in 1 second. Thus, Total time is 2 seconds. Example case 2.Dave starts with rate of 1 banana/second, and then increase it to 2 bananas/second and again decrease it to 1 banana/second. So total time is 3 seconds.
{"inputs": ["2\n2\n4"], "outputs": ["2\n3"]}
295
18
coding
Solve the programming task below in a Python markdown code block. Your task in this Kata is to emulate text justify right in monospace font. You will be given a single-lined text and the expected justification width. The longest word will never be greater than this width. Here are the rules: - Use spaces to fill in the gaps on the left side of the words. - Each line should contain as many words as possible. - Use '\n' to separate lines. - Gap between words can't differ by more than one space. - Lines should end with a word not a space. - '\n' is not included in the length of a line. - Last line should not contain '\n' Example with width=30: ``` Bacon ipsum dolor amet excepteur ut kevin burgdoggen, shankle cupim dolor officia ground round id ullamco deserunt nisi. Commodo tail qui salami, brisket boudin tri-tip. Labore flank laboris, cow enim proident aliqua sed hamburger consequat. Sed consequat ut non bresaola capicola shoulder excepteur veniam, bacon kevin. Pastrami shank laborum est excepteur non eiusmod bresaola flank in nostrud. Corned beef ex pig do kevin filet mignon in irure deserunt ipsum qui duis short loin. Beef ribs dolore meatball officia rump fugiat in enim corned beef non est. ``` If you enjoyed this one and want more of a challenge try https://www.codewars.com/kata/text-align-justify/python If you like bacon ipsum https://baconipsum.com Also feel free to reuse/extend the following starter code: ```python def align_right(text, width): ```
{"functional": "_inputs = [['abc def', 10], ['I take up the whole line', 24], ['Two lines, I am', 10]]\n_outputs = [[' abc def'], ['I take up the whole line'], ['Two lines,\\n I am']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(align_right(*i), o[0])"}
390
203
coding
Solve the programming task below in a Python markdown code block. Tak has N cards. On the i-th (1 \leq i \leq N) card is written an integer x_i. He is selecting one or more cards from these N cards, so that the average of the integers written on the selected cards is exactly A. In how many ways can he make his selection? -----Constraints----- - 1 \leq N \leq 50 - 1 \leq A \leq 50 - 1 \leq x_i \leq 50 - N,\,A,\,x_i are integers. -----Partial Score----- - 200 points will be awarded for passing the test set satisfying 1 \leq N \leq 16. -----Input----- The input is given from Standard Input in the following format: N A x_1 x_2 ... x_N -----Output----- Print the number of ways to select cards such that the average of the written integers is exactly A. -----Sample Input----- 4 8 7 9 8 9 -----Sample Output----- 5 - The following are the 5 ways to select cards such that the average is 8: - Select the 3-rd card. - Select the 1-st and 2-nd cards. - Select the 1-st and 4-th cards. - Select the 1-st, 2-nd and 3-rd cards. - Select the 1-st, 3-rd and 4-th cards.
{"inputs": ["3 8\n6 6 9\n", "4 8\n7 9 8 9\n", "8 5\n3 6 2 8 7 6 5 9\n", "33 3\n3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3\n"], "outputs": ["0\n", "5\n", "19\n", "8589934591\n"]}
331
153
coding
Solve the programming task below in a Python markdown code block. Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to a_{i}. Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard? -----Input----- The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively. The second line contains n integers a_{i} (1 ≤ a_{i} ≤ 2h), the i-th of them is equal to the height of the i-th person. -----Output----- Print a single integer — the minimum possible valid width of the road. -----Examples----- Input 3 7 4 5 14 Output 4 Input 6 1 1 1 1 1 1 1 Output 6 Input 6 5 7 6 8 9 10 5 Output 11 -----Note----- In the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4. In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough. In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11.
{"inputs": ["1 1\n1\n", "1 1\n2\n", "1 1\n1\n", "1 1\n2\n", "1 2\n2\n", "1 2\n4\n", "1 3\n2\n", "1 3\n3\n"], "outputs": ["1\n", "2\n", "1\n", "2\n", "1\n", "2\n", "1\n", "1\n"]}
472
102
coding
Solve the programming task below in a Python markdown code block. There are n positive integers a_1, a_2, ..., a_n. For the one move you can choose any even value c and divide by two all elements that equal c. For example, if a=[6,8,12,6,3,12] and you choose c=6, and a is transformed into a=[3,8,12,3,3,12] after the move. You need to find the minimal number of moves for transforming a to an array of only odd integers (each element shouldn't be divisible by 2). Input The first line of the input contains one integer t (1 ≤ t ≤ 10^4) — the number of test cases in the input. Then t test cases follow. The first line of a test case contains n (1 ≤ n ≤ 2⋅10^5) — the number of integers in the sequence a. The second line contains positive integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9). The sum of n for all test cases in the input doesn't exceed 2⋅10^5. Output For t test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by 2). Example Input 4 6 40 6 40 3 20 1 1 1024 4 2 4 8 16 3 3 1 7 Output 4 10 4 0 Note In the first test case of the example, the optimal sequence of moves can be as follows: * before making moves a=[40, 6, 40, 3, 20, 1]; * choose c=6; * now a=[40, 3, 40, 3, 20, 1]; * choose c=40; * now a=[20, 3, 20, 3, 20, 1]; * choose c=20; * now a=[10, 3, 10, 3, 10, 1]; * choose c=10; * now a=[5, 3, 5, 3, 5, 1] — all numbers are odd. Thus, all numbers became odd after 4 moves. In 3 or fewer moves, you cannot make them all odd.
{"inputs": ["4\n6\n22 3 32 3 20 1\n1\n94\n4\n3 4 8 30\n2\n3 1 7\n", "4\n6\n22 6 18 5 20 1\n1\n311\n4\n2 4 8 16\n3\n3 1 7\n", "4\n6\n22 4 18 5 20 1\n1\n311\n4\n2 4 8 16\n3\n3 1 7\n", "4\n6\n37 6 40 3 20 1\n1\n757\n4\n2 6 8 16\n3\n3 1 7\n", "4\n6\n37 6 40 3 20 1\n1\n757\n4\n2 6 8 20\n3\n3 1 7\n", "4\n6\n40 8 40 3 20 1\n1\n160\n4\n2 4 1 16\n0\n3 1 7\n", "4\n6\n4 6 40 3 34 2\n1\n2038\n4\n2 4 8 30\n2\n3 1 7\n", "4\n6\n40 6 40 3 20 1\n1\n2020\n4\n2 4 8 16\n3\n3 1 7\n"], "outputs": ["8\n1\n4\n0\n", "5\n0\n4\n0\n", "6\n0\n4\n0\n", "4\n0\n5\n0\n", "4\n0\n6\n0\n", "6\n5\n4\n0\n", "7\n1\n4\n0\n", "4\n2\n4\n0\n"]}
582
454