task_type
stringclasses
1 value
problem
stringlengths
261
3.34k
answer
stringlengths
35
6.15k
problem_tokens
int64
62
774
answer_tokens
int64
12
2.04k
coding
Solve the programming task below in a Python markdown code block. Vishesh has gone to watch the new Spider-Man movie, but he is having troubles choosing which Popcorn-and-Coke combo to buy. There are three combos A, B, and C available at the counter. You are given the time (in minute) for which each Popcorn bucket and Coke cup lasts. Given that Vishesh's satisfaction from a combo is defined as the total lasting time (in minute) of the Popcorn bucket and the Coke cup, find the maximum satisfaction he can get by buying a single combo. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The first line of each test case contains two space-separated integers A_{1} and A_{2}, meaning that for combo A, the Popcorn bucket lasts A_{1} minutes and the Coke cup lasts A_{2} minutes. - The second line of each test case contains two space-separated integers B_{1} and B_{2}, meaning that for combo B, the Popcorn bucket lasts B_{1} minutes and the Coke cup lasts B_{2} minutes. - The third line of each test case contains two space-separated integers C_{1} and C_{2}, meaning that for combo C, the Popcorn bucket lasts C_{1} minutes and the Coke cup lasts C_{2} minutes. ------ Output Format ------ For each test case, output on a single line the maximum satisfaction Vishesh can get. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ A_{1}, A_{2}, B_{1}, B_{2}, C_{1}, C_{2} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 3 6 5 10 8 7 99 1 55 55 33 51 54 146 5436 627 1527 5421 ----- Sample Output 1 ------ 15 110 6948 ----- explanation 1 ------ - Test case $1$: - For combo $A$, the Popcorn bucket lasts $3$ minutes, and the Coke cup lasts $6$ minutes, so Vishesh gets a satisfaction of $3 + 6 = 9$ if he buys this combo. - For combo $B$, the Popcorn bucket lasts $5$ minutes, and the Coke cup lasts $10$ minutes, so Vishesh gets a satisfaction of $5 + 10 = 15$ if he buys this combo. - For combo $C$, the Popcorn bucket lasts $8$ minutes, and the Coke cup lasts $7$ minutes, so Vishesh gets a satisfaction of $8 + 7 = 15$ if he buys this combo. Therefore Vishesh's maximum satisfaction is $15$.
{"inputs": ["3\n3 6\n5 10\n8 7\n99 1\n55 55\n33 51\n54 146\n5436 627\n1527 5421\n"], "outputs": ["15\n110\n6948\n"]}
623
80
coding
Solve the programming task below in a Python markdown code block. A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward. Examples of numerical palindromes are: `2332, 110011, 54322345` For a given number ```num```, write a function which returns an array of all the numerical palindromes contained within each number. The array should be sorted in ascending order and any duplicates should be removed. In this kata, single digit numbers and numbers which start or end with zeros (such as `010` and `00`) are **NOT** considered valid numerical palindromes. If `num` contains no valid palindromes, return `"No palindromes found"`. Otherwise, return `"Not valid"` if the input is not an integer or is less than `0`. ## Examples ``` palindrome(1221) => [22, 1221] palindrome(34322122) => [22, 212, 343, 22122] palindrome(1001331) => [33, 1001, 1331] palindrome(1294) => "No palindromes found" palindrome("1221") => "Not valid" ``` --- ### Other Kata in this Series: Numerical Palindrome #1 Numerical Palindrome #1.5 Numerical Palindrome #2 Numerical Palindrome #3 Numerical Palindrome #3.5 Numerical Palindrome #4 Numerical Palindrome #5 Also feel free to reuse/extend the following starter code: ```python def palindrome(num): ```
{"functional": "_inputs = [[2], [34322122], [10015885], [4444], [1002001], [141221001], [1551], [13598], ['ACCDDCCA'], ['1551'], [-4505]]\n_outputs = [['No palindromes found'], [[22, 212, 343, 22122]], [[88, 1001, 5885]], [[44, 444, 4444]], [[1002001]], [[22, 141, 1001, 1221]], [[55, 1551]], ['No palindromes found'], ['Not valid'], ['Not valid'], ['Not valid']]\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(palindrome(*i), o[0])"}
407
348
coding
Solve the programming task below in a Python markdown code block. It is a simplified version of problem F2. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 2$). -----Output----- For each test case output on a separate line $x$ — the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 4 1 1 221 2 177890 2 998244353 1 Output 1 221 181111 999999999 -----Note----- None
{"inputs": ["1\n4320 2\n", "1\n9319 2\n", "1\n5952 2\n", "1\n7778 2\n", "1\n5878 2\n", "1\n4025 2\n", "1\n89258 1\n", "1\n76353 2\n"], "outputs": ["4333\n", "9333\n", "5955\n", "7778\n", "5885\n", "4040\n", "99999\n", "76666\n"]}
373
154
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. Compute: floor[1*e] + floor[2*e] + floor[3*e] + ... + floor[n*e], where floor[x] is the largest integer that is not greater than x, and e is Euler's number: 2.7182818284... ------ Input ------ A single line which contains a single integer: n. ------ Output ------ A single line which contains a single integer which should be the answer. ------ Constraints ------ 1 ≤ n ≤ 10^{4000} ------ Subtasks ------ $Subtask #1 (50 points): 1 ≤ n ≤ 10^{100} $$Subtask #2 (50 points): Original constraints.$ ----- Sample Input 1 ------ 3 ----- Sample Output 1 ------ 15 ----- explanation 1 ------ floor[1*e] = floor[2.71828..] = 2. floor[2*e] = floor[5.43656..] = 5. floor[3*e] = floor[8.15484..] = 8. So the answer is 2+5+8=15. Note Source code limit is 1000 bytes.
{"inputs": ["3"], "outputs": ["15"]}
296
13
coding
Solve the programming task below in a Python markdown code block. Alice and Brown loves games. Today, they will play the following game. In this game, there are two piles initially consisting of X and Y stones, respectively. Alice and Bob alternately perform the following operation, starting from Alice: - Take 2i stones from one of the piles. Then, throw away i of them, and put the remaining i in the other pile. Here, the integer i (1≤i) can be freely chosen as long as there is a sufficient number of stones in the pile. The player who becomes unable to perform the operation, loses the game. Given X and Y, determine the winner of the game, assuming that both players play optimally. -----Constraints----- - 0 ≤ X, Y ≤ 10^{18} -----Input----- Input is given from Standard Input in the following format: X Y -----Output----- Print the winner: either Alice or Brown. -----Sample Input----- 2 1 -----Sample Output----- Brown Alice can do nothing but taking two stones from the pile containing two stones. As a result, the piles consist of zero and two stones, respectively. Then, Brown will take the two stones, and the piles will consist of one and zero stones, respectively. Alice will be unable to perform the operation anymore, which means Brown's victory.
{"inputs": ["1 0", "6 0", "2 0", "1 8", "4 0", "1 5", "3 0", "0 7"], "outputs": ["Brown\n", "Alice\n", "Alice\n", "Alice\n", "Alice\n", "Alice\n", "Alice\n", "Alice\n"]}
281
78
coding
Solve the programming task below in a Python markdown code block. There is BMI (Body Mass Index) as an index showing the degree of obesity. The BMI value is calculated using the following formula. BMI = weight (kg) / (height (m)) 2 The closer the BMI value is to the standard value, the more "ideal body shape" is considered. Therefore, if the standard value of BMI is 22, create a program that inputs the information of the target person and outputs the information of the person closest to the "ideal body shape". Assuming that the number of target persons is n, each target person is assigned a reception number p that is an integer value between 1 and n so that there is no duplication. Input A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format: n p1 h1 w1 p2 h2 w2 :: pn hn wn Number of subjects n (n ≤ 1000) on the first line, reception number pi (1 ≤ pi ≤ n) of the i-th subject on the following n lines, height hi in centimeters (1 ≤ hi ≤ 200), Given a kilogram of body weight wi (1 ≤ wi ≤ 200). All inputs are given as integers. Output For each data set, the reception number (integer) of the person closest to the "ideal body shape" is output on one line. If there are two or more people who are closest to the "ideal body shape", the one with the smaller reception number will be output. Example Input 6 1 165 66 2 178 60 3 180 72 4 160 65 5 185 62 6 182 62 3 3 160 65 2 180 70 1 170 75 0 Output 3 2
{"inputs": ["6\n4 179 10\n0 657 0\n4 24 36\n11 2 75\n1 13 111\n10 336 74\n3\n2 15 4\n-1 76 6\n2 139 34\n0", "6\n4 216 10\n0 657 0\n4 24 36\n11 2 68\n1 13 011\n10 336 74\n3\n2 15 4\n0 76 12\n1 139 34\n0", "6\n2 58 11\n1 178 30\n3 78 72\n4 42 65\n7 101 62\n6 47 50\n3\n-1 147 129\n1 3 14\n0 80 24\n0", "6\n2 32 66\n1 178 118\n3 82 72\n4 116 65\n5 101 62\n6 47 50\n3\n0 147 129\n1 3 14\n0 80 24\n0", "6\n2 32 66\n1 178 118\n3 82 72\n4 116 65\n5 101 62\n6 47 50\n3\n0 147 129\n1 3 14\n1 80 24\n0", "6\n4 216 10\n0 1204 0\n4 24 40\n1 2 75\n1 5 011\n10 336 010\n3\n2 15 4\n-1 76 12\n1 139 34\n0", "6\n2 58 11\n1 178 30\n3 78 72\n4 42 65\n7 101 62\n6 47 50\n3\n-1 147 129\n1 3 14\n-1 80 24\n0", "6\n1 32 66\n1 178 118\n3 180 72\n4 60 65\n5 218 62\n6 47 50\n3\n1 147 129\n1 3 14\n1 72 24\n0"], "outputs": ["10\n2\n", "10\n0\n", "2\n0\n", "1\n0\n", "1\n1\n", "4\n-1\n", "2\n-1\n", "3\n1\n"]}
441
727
coding
Solve the programming task below in a Python markdown code block. Description Lets imagine a yoga classroom as a Square 2D Array of Integers ```classroom```, with each integer representing a person, and the value representing their skill level. ``` classroom = [ [3,2,1,3], [1,3,2,1], [1,1,1,2], ] poses = [1,7,5,9,10,21,4,3] ``` During a yoga class the instructor gives a list of integers ```poses``` representing a yoga pose that each person in the class will attempt to complete. A person can complete a yoga pose if the sum of their row and their skill level is greater than or equal to the value of the pose. Task Your task is to return the total amount poses completed for the entire ```classroom```. Example ``` classroom = [ [1,1,0,1], #sum = 3 [2,0,6,0], #sum = 8 [0,2,2,0], #sum = 4 ] poses = [4, 0, 20, 10] 3 people in row 1 can complete the first pose Everybody in row 1 can complete the second pose Nobody in row 1 can complete the third pose Nobody in row 1 can complete the fourth pose The total poses completed for row 1 is 7 You'll need to return the total for all rows and all poses. ``` Translations are welcomed! Also feel free to reuse/extend the following starter code: ```python def yoga(classroom, poses): ```
{"functional": "_inputs = [[[[0, 0], [0, 0]], [1, 1, 0, 1, 2, 3, 0, 1, 5]], [[], [1, 3, 4]], [[[0, 0], [0, 0]], []], [[], []]]\n_outputs = [[8], [0], [0], [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(yoga(*i), o[0])"}
365
229
coding
Solve the programming task below in a Python markdown code block. ## Task You have to write three functions namely - `PNum, GPNum and SPNum` (JS, Coffee), `p_num, g_p_num and s_p_num` (Python and Ruby), `pNum, gpNum and spNum` (Java, C#), `p-num, gp-num and sp-num` (Clojure) - to check whether a given argument `n` is a Pentagonal, Generalized Pentagonal, or Square Pentagonal Number, and return `true` if it is and `false` otherwise. ### Description `Pentagonal Numbers` - The nth pentagonal number Pn is the number of distinct dots in a pattern of dots consisting of the outlines of regular pentagons with sides up to n dots (means the side contains n number of dots), when the pentagons are overlaid so that they share one corner vertex. > First few Pentagonal Numbers are: 1, 5, 12, 22... `Generalized Pentagonal Numbers` - All the Pentagonal Numbers along with the number of dots inside the outlines of all the pentagons of a pattern forming a pentagonal number pentagon are called Generalized Pentagonal Numbers. > First few Generalized Pentagonal Numbers are: 0, 1, 2, 5, 7, 12, 15, 22... `Square Pentagonal Numbers` - The numbers which are Pentagonal Numbers and are also a perfect square are called Square Pentagonal Numbers. > First few are: 1, 9801, 94109401... ### Examples #### Note: * Pn = Nth Pentagonal Number * Gpn = Nth Generalized Pentagonal Number ^ ^ ^ ^ ^ P1=1 P2=5 P3=12 P4=22 P5=35 //Total number of distinct dots used in the Pattern Gp2=1 Gp4=5 Gp6=12 Gp8=22 //All the Pentagonal Numbers are Generalised Gp1=0 Gp3=2 Gp5=7 Gp7=15 //Total Number of dots inside the outermost Pentagon Also feel free to reuse/extend the following starter code: ```python def p_num(n): ```
{"functional": "_inputs = [[0], [1], [2], [3], [4], [5], [100]]\n_outputs = [[False], [True], [False], [False], [False], [True], [False]]\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(p_num(*i), o[0])"}
528
192
coding
Solve the programming task below in a Python markdown code block. # Task Let's consider a table consisting of `n` rows and `n` columns. The cell located at the intersection of the i-th row and the j-th column contains number i × j. The rows and columns are numbered starting from 1. You are given a positive integer `x`. Your task is to count the number of cells in a table that contain number `x`. # Example For `n = 5 and x = 5`, the result should be `2`. The table looks like: ``` 1 2 3 4 (5) 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 (5) 10 15 20 25``` There are two number `5` in it. For `n = 10 and x = 5`, the result should be 2. For `n = 6 and x = 12`, the result should be 4. ``` 1 2 3 4 5 6 2 4 6 8 10 (12) 3 6 9 (12) 15 18 4 8 (12) 16 20 24 5 10 15 20 25 30 6 (12) 18 24 30 36 ``` # Input/Output - `[input]` integer `n` `1 ≤ n ≤ 10^5.` - `[input]` integer `x` `1 ≤ x ≤ 10^9.` - `[output]` an integer The number of times `x` occurs in the table. Also feel free to reuse/extend the following starter code: ```python def count_number(n, x): ```
{"functional": "_inputs = [[5, 5], [10, 5], [6, 12], [6, 169], [100000, 1000000000]]\n_outputs = [[2], [2], [4], [0], [16]]\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(count_number(*i), o[0])"}
489
212
coding
Solve the programming task below in a Python markdown code block. Assume `"#"` is like a backspace in string. This means that string `"a#bc#d"` actually is `"bd"` Your task is to process a string with `"#"` symbols. ## Examples ``` "abc#d##c" ==> "ac" "abc##d######" ==> "" "#######" ==> "" "" ==> "" ``` Also feel free to reuse/extend the following starter code: ```python def clean_string(s): ```
{"functional": "_inputs = [['abjd####jfk#'], ['gfh#jds###d#dsd####dasdaskhj###dhkjs####df##s##d##'], ['831####jns###s#cas/*####-5##s##6+yqw87e##hfklsd-=-28##fds##'], ['######831###dhkj####jd#dsfsdnjkf###d####dasns'], [''], ['#######'], ['####gfdsgf##hhs#dg####fjhsd###dbs########afns#######sdanfl##db#####s#a'], ['#hskjdf#gd'], ['hsk48hjjdfgd']]\n_outputs = [['jf'], ['gdasda'], ['6+yqw8hfklsd-=-f'], ['jdsfdasns'], [''], [''], ['sa'], ['hskjdgd'], ['hsk48hjjdfgd']]\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(clean_string(*i), o[0])"}
118
350
coding
Solve the programming task below in a Python markdown code block. a is an array of n positive integers, all of which are not greater than n. You have to process q queries to this array. Each query is represented by two numbers p and k. Several operations are performed in each query; each operation changes p to p + ap + k. There operations are applied until p becomes greater than n. The answer to the query is the number of performed operations. Input The first line contains one integer n (1 ≤ n ≤ 100000). The second line contains n integers — elements of a (1 ≤ ai ≤ n for each i from 1 to n). The third line containts one integer q (1 ≤ q ≤ 100000). Then q lines follow. Each line contains the values of p and k for corresponding query (1 ≤ p, k ≤ n). Output Print q integers, ith integer must be equal to the answer to ith query. Example Input 3 1 1 1 3 1 1 2 1 3 1 Output 2 1 1 Note Consider first example: In first query after first operation p = 3, after second operation p = 5. In next two queries p is greater than n after the first operation.
{"inputs": ["3\n1 1 1\n3\n1 1\n2 1\n1 1\n", "3\n1 1 1\n3\n2 1\n2 1\n1 1\n", "3\n2 1 1\n3\n1 1\n2 1\n1 1\n", "3\n1 1 2\n3\n2 1\n2 1\n1 1\n", "3\n1 1 1\n3\n2 2\n2 1\n1 1\n", "3\n1 1 0\n3\n2 1\n2 1\n1 1\n", "3\n1 1 1\n3\n1 1\n2 1\n3 1\n", "10\n3 5 4 3 7 8 6 7 2 3\n10\n4 5\n2 10\n4 3\n9 9\n9 2\n5 1\n6 4\n1 1\n5 6\n6 4\n"], "outputs": ["2\n1\n2\n", "1\n1\n2\n", "1\n1\n1\n", "1\n1\n2\n", "1\n1\n2\n", "1\n1\n2\n", "2\n1\n1\n", "1\n1\n2\n1\n1\n1\n1\n2\n1\n1\n"]}
280
321
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given the root of a binary tree, return the preorder traversal of its nodes' values.   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 preorderTraversal(self, root: Optional[TreeNode]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(root = tree_node([1,None,2,3])) == [1,2,3]\n assert candidate(root = tree_node([])) == []\n assert candidate(root = tree_node([1])) == [1]\n assert candidate(root = tree_node([1,2])) == [1,2]\n assert candidate(root = tree_node([1,None,2])) == [1,2]\n\n\ncheck(Solution().preorderTraversal)"}
119
111
coding
Solve the programming task below in a Python markdown code block. In this kata you will be given a sequence of the dimensions of rectangles ( sequence with width and length ) and circles ( radius - just a number ). Your task is to return a new sequence of dimensions, sorted ascending by area. For example, ```python seq = [ (4.23, 6.43), 1.23, 3.444, (1.342, 3.212) ] # [ rectangle, circle, circle, rectangle ] sort_by_area(seq) => [ ( 1.342, 3.212 ), 1.23, ( 4.23, 6.43 ), 3.444 ] ``` This kata inspired by [Sort rectangles and circles by area](https://www.codewars.com/kata/sort-rectangles-and-circles-by-area/). Also feel free to reuse/extend the following starter code: ```python def sort_by_area(seq): ```
{"functional": "_inputs = [[[]]]\n_outputs = [[[]]]\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(sort_by_area(*i), o[0])"}
224
155
coding
Solve the programming task below in a Python markdown code block. **Debug** a function called calculate that takes 3 values. The first and third values are numbers. The second value is a character. If the character is "+" , "-", "\*", or "/", the function will return the result of the corresponding mathematical function on the two numbers. If the string is not one of the specified characters, the function should return null. ``` calculate(2,"+", 4); //Should return 6 calculate(6,"-", 1.5); //Should return 4.5 calculate(-4,"*", 8); //Should return -32 calculate(49,"/", -7); //Should return -7 calculate(8,"m", 2); //Should return null calculate(4,"/",0) //should return null ``` Kind of a fork (not steal :)) of [Basic Calculator][1] kata by [TheDoctor][2]. [1]: http://www.codewars.com/kata/basic-calculator/javascript [2]: http://www.codewars.com/users/528c45adbd9daa384300068d Also feel free to reuse/extend the following starter code: ```python def calculate(a, o, b): ```
{"functional": "_inputs = [[2, '+', 4], [6, '-', 1.5], [-4, '*', 8], [49, '/', -7], [8, 'm', 2], [4, '/', 0], [3.2, '+', 8], [3.2, '-', 8], [3.2, '/', 8], [3.2, '*', 8], [-3, '+', 0], [-3, '-', 0], [-3, '/', 0], [-2, '/', -2], [-2, 'codewars', -2], [-3, '*', 0], [0, '*', 0], [0, '**', 0], [-3, 'w', 0], [0, '/', 0]]\n_outputs = [[6], [4.5], [-32], [-7], [None], [None], [11.2], [-4.8], [0.4], [25.6], [-3], [-3], [None], [1], [None], [0], [0], [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(calculate(*i), o[0])"}
277
384
coding
Solve the programming task below in a Python markdown code block. The semester is already ending, so Danil made an effort and decided to visit a lesson on harmony analysis to know how does the professor look like, at least. Danil was very bored on this lesson until the teacher gave the group a simple task: find 4 vectors in 4-dimensional space, such that every coordinate of every vector is 1 or - 1 and any two vectors are orthogonal. Just as a reminder, two vectors in n-dimensional space are considered to be orthogonal if and only if their scalar product is equal to zero, that is: $\sum_{i = 1}^{n} a_{i} \cdot b_{i} = 0$. Danil quickly managed to come up with the solution for this problem and the teacher noticed that the problem can be solved in a more general case for 2^{k} vectors in 2^{k}-dimensinoal space. When Danil came home, he quickly came up with the solution for this problem. Can you cope with it? -----Input----- The only line of the input contains a single integer k (0 ≤ k ≤ 9). -----Output----- Print 2^{k} lines consisting of 2^{k} characters each. The j-th character of the i-th line must be equal to ' * ' if the j-th coordinate of the i-th vector is equal to - 1, and must be equal to ' + ' if it's equal to + 1. It's guaranteed that the answer always exists. If there are many correct answers, print any. -----Examples----- Input 2 Output ++** +*+* ++++ +**+ -----Note----- Consider all scalar products in example: Vectors 1 and 2: ( + 1)·( + 1) + ( + 1)·( - 1) + ( - 1)·( + 1) + ( - 1)·( - 1) = 0 Vectors 1 and 3: ( + 1)·( + 1) + ( + 1)·( + 1) + ( - 1)·( + 1) + ( - 1)·( + 1) = 0 Vectors 1 and 4: ( + 1)·( + 1) + ( + 1)·( - 1) + ( - 1)·( - 1) + ( - 1)·( + 1) = 0 Vectors 2 and 3: ( + 1)·( + 1) + ( - 1)·( + 1) + ( + 1)·( + 1) + ( - 1)·( + 1) = 0 Vectors 2 and 4: ( + 1)·( + 1) + ( - 1)·( - 1) + ( + 1)·( - 1) + ( - 1)·( + 1) = 0 Vectors 3 and 4: ( + 1)·( + 1) + ( + 1)·( - 1) + ( + 1)·( - 1) + ( + 1)·( + 1) = 0
{"inputs": ["2\n", "1\n", "3\n", "0\n", "4\n", "2\n", "4\n", "1\n"], "outputs": ["++++\n+*+*\n++**\n+**+\n", "++\n+*\n", "++++++++\n+*+*+*+*\n++**++**\n+**++**+\n++++****\n+*+**+*+\n++****++\n+**+*++*\n", "+\n", "++++++++++++++++\n+*+*+*+*+*+*+*+*\n++**++**++**++**\n+**++**++**++**+\n++++****++++****\n+*+**+*++*+**+*+\n++****++++****++\n+**+*++*+**+*++*\n++++++++********\n+*+*+*+**+*+*+*+\n++**++****++**++\n+**++**+*++**++*\n++++********++++\n+*+**+*+*+*++*+*\n++****++**++++**\n+**+*++**++*+**+\n", "++++\n+*+*\n++**\n+**+\n", "++++++++++++++++\n+*+*+*+*+*+*+*+*\n++**++**++**++**\n+**++**++**++**+\n++++****++++****\n+*+**+*++*+**+*+\n++****++++****++\n+**+*++*+**+*++*\n++++++++********\n+*+*+*+**+*+*+*+\n++**++****++**++\n+**++**+*++**++*\n++++********++++\n+*+**+*+*+*++*+*\n++****++**++++**\n+**+*++**++*+**+\n", "++\n+*\n"]}
721
468
coding
Solve the programming task below in a Python markdown code block. There is a rooted tree (see Notes) with N vertices numbered 1 to N. Each of the vertices, except the root, has a directed edge coming from its parent. Note that the root may not be Vertex 1. Takahashi has added M new directed edges to this graph. Each of these M edges, u \rightarrow v, extends from some vertex u to its descendant v. You are given the directed graph with N vertices and N-1+M edges after Takahashi added edges. More specifically, you are given N-1+M pairs of integers, (A_1, B_1), ..., (A_{N-1+M}, B_{N-1+M}), which represent that the i-th edge extends from Vertex A_i to Vertex B_i. Restore the original rooted tree. Constraints * 3 \leq N * 1 \leq M * N + M \leq 10^5 * 1 \leq A_i, B_i \leq N * A_i \neq B_i * If i \neq j, (A_i, B_i) \neq (A_j, B_j). * The graph in input can be obtained by adding M edges satisfying the condition in the problem statement to a rooted tree with N vertices. Input Input is given from Standard Input in the following format: N M A_1 B_1 : A_{N-1+M} B_{N-1+M} Output Print N lines. In the i-th line, print `0` if Vertex i is the root of the original tree, and otherwise print the integer representing the parent of Vertex i in the original tree. Note that it can be shown that the original tree is uniquely determined. Examples Input 3 1 1 2 1 3 2 3 Output 0 1 2 Input 6 3 2 1 2 3 4 1 4 2 6 1 2 6 4 6 6 5 Output 6 4 2 0 6 2
{"inputs": ["3 1\n1 2\n2 3\n2 3", "3 1\n1 2\n1 3\n2 3", "6 3\n2 1\n2 3\n4 1\n4 2\n6 1\n2 6\n3 6\n6 5", "6 3\n2 1\n2 3\n4 1\n4 2\n6 1\n2 6\n3 6\n4 5", "6 3\n3 1\n4 3\n4 1\n4 2\n6 1\n4 6\n3 6\n4 5", "6 3\n2 1\n2 3\n4 1\n4 2\n6 1\n2 6\n3 6\n1 5", "6 3\n2 1\n2 3\n4 1\n4 2\n6 1\n2 6\n3 6\n3 5", "6 3\n3 1\n4 3\n4 1\n4 2\n6 2\n4 6\n3 6\n4 5"], "outputs": ["0\n1\n2\n", "0\n1\n2", "6\n4\n2\n0\n6\n3\n", "6\n4\n2\n0\n4\n3\n", "6\n4\n4\n0\n4\n3\n", "6\n4\n2\n0\n1\n3\n", "6\n4\n2\n0\n3\n3\n", "3\n6\n4\n0\n4\n3\n"]}
466
361
coding
Solve the programming task below in a Python markdown code block. You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n \le 3 \cdot 10^5, -100 \le x \le 100$) — the length of array $a$ and the integer $x$ respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) — the array $a$. -----Output----- Print one integer — the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$. -----Examples----- Input 5 -2 -3 8 -2 1 -6 Output 22 Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Output 42 Input 5 10 -1 -2 -3 -4 -5 Output 0 -----Note----- In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
{"inputs": ["1 0\n1\n", "1 1\n1\n", "1 4\n7\n", "1 6\n5\n", "1 2\n5\n", "1 6\n5\n", "1 2\n5\n", "1 4\n7\n"], "outputs": ["1\n", "1\n", "28\n", "30\n", "10\n", "30", "10", "28"]}
476
105
coding
Solve the programming task below in a Python markdown code block. You are the gym teacher in the school. There are $n$ students in the row. And there are two rivalling students among them. The first one is in position $a$, the second in position $b$. Positions are numbered from $1$ to $n$ from left to right. Since they are rivals, you want to maximize the distance between them. If students are in positions $p$ and $s$ respectively, then distance between them is $|p - s|$. You can do the following operation at most $x$ times: choose two adjacent (neighbouring) students and swap them. Calculate the maximum distance between two rivalling students after at most $x$ swaps. -----Input----- The first line contains one integer $t$ ($1 \le t \le 100$) — the number of test cases. The only line of each test case contains four integers $n$, $x$, $a$ and $b$ ($2 \le n \le 100$, $0 \le x \le 100$, $1 \le a, b \le n$, $a \neq b$) — the number of students in the row, the number of swaps which you can do, and positions of first and second rivaling students respectively. -----Output----- For each test case print one integer — the maximum distance between two rivaling students which you can obtain. -----Example----- Input 3 5 1 3 2 100 33 100 1 6 0 2 3 Output 2 99 1 -----Note----- In the first test case you can swap students in positions $3$ and $4$. And then the distance between the rivals is equal to $|4 - 2| = 2$. In the second test case you don't have to swap students. In the third test case you can't swap students.
{"inputs": ["1\n5 2 3 2\n", "1\n5 2 3 2\n", "1\n7 2 3 2\n", "1\n7 0 3 2\n", "1\n7 1 3 2\n", "1\n3 1 1 2\n", "1\n5 2 1 2\n", "1\n6 1 3 2\n"], "outputs": ["3\n", "3\n", "3\n", "1\n", "2\n", "2\n", "3\n", "2\n"]}
422
134
coding
Solve the programming task below in a Python markdown code block. A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward. Examples of numerical palindromes are: * 232 * 110011 * 54322345 Complete the function to test if the given number (`num`) **can be rearranged** to form a numerical palindrome or not. Return a boolean (`true` if it can be rearranged to a palindrome, and `false` if it cannot). Return `"Not valid"` if the input is not an integer or is less than 0. For this kata, single digit numbers are **NOT** considered numerical palindromes. ## Examples ``` 5 => false 2121 => true 1331 => true 3357665 => true 1294 => false "109982" => "Not valid" -42 => "Not valid" ``` Also feel free to reuse/extend the following starter code: ```python def palindrome(num): ```
{"functional": "_inputs = [[5], [1212], ['ololo'], [1331], [194], [111222], ['Hello world!'], [3357665], ['357665'], [-42]]\n_outputs = [[False], [True], ['Not valid'], [True], [False], [False], ['Not valid'], [True], ['Not valid'], ['Not valid']]\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(palindrome(*i), o[0])"}
256
241
coding
Solve the programming task below in a Python markdown code block. A and B are preparing themselves for programming contests. B loves to debug his code. But before he runs the solution and starts debugging, he has to first compile the code. Initially, the compiler displayed n compilation errors, each of them is represented as a positive integer. After some effort, B managed to fix some mistake and then another one mistake. However, despite the fact that B is sure that he corrected the two errors, he can not understand exactly what compilation errors disappeared — the compiler of the language which B uses shows errors in the new order every time! B is sure that unlike many other programming languages, compilation errors for his programming language do not depend on each other, that is, if you correct one error, the set of other error does not change. Can you help B find out exactly what two errors he corrected? -----Input----- The first line of the input contains integer n (3 ≤ n ≤ 10^5) — the initial number of compilation errors. The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9) — the errors the compiler displayed for the first time. The third line contains n - 1 space-separated integers b_1, b_2, ..., b_{n} - 1 — the errors displayed at the second compilation. It is guaranteed that the sequence in the third line contains all numbers of the second string except for exactly one. The fourth line contains n - 2 space-separated integers с_1, с_2, ..., с_{n} - 2 — the errors displayed at the third compilation. It is guaranteed that the sequence in the fourth line contains all numbers of the third line except for exactly one. -----Output----- Print two numbers on a single line: the numbers of the compilation errors that disappeared after B made the first and the second correction, respectively. -----Examples----- Input 5 1 5 8 123 7 123 7 5 1 5 1 7 Output 8 123 Input 6 1 4 3 3 5 7 3 7 5 4 3 4 3 7 5 Output 1 3 -----Note----- In the first test sample B first corrects the error number 8, then the error number 123. In the second test sample B first corrects the error number 1, then the error number 3. Note that if there are multiple errors with the same number, B can correct only one of them in one step.
{"inputs": ["3\n1 2 3\n3 2\n2\n", "3\n1 2 3\n3 2\n2\n", "3\n2 2 3\n3 2\n2\n", "3\n2 1 3\n3 2\n2\n", "3\n2 2 3\n3 2\n3\n", "3\n84 7 9\n9 84\n9\n", "3\n84 30 9\n9 84\n9\n", "3\n84 30 9\n9 84\n9\n"], "outputs": ["1\n3\n", "1\n3\n", "2\n3\n", "1\n3\n", "2\n2\n", "7\n84\n", "30\n84\n", "30\n84\n"]}
563
195
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a 0-indexed integer array buses of length n, where buses[i] represents the departure time of the ith bus. You are also given a 0-indexed integer array passengers of length m, where passengers[j] represents the arrival time of the jth passenger. All bus departure times are unique. All passenger arrival times are unique. You are given an integer capacity, which represents the maximum number of passengers that can get on each bus. When a passenger arrives, they will wait in line for the next available bus. You can get on a bus that departs at x minutes if you arrive at y minutes where y <= x, and the bus is not full. Passengers with the earliest arrival times get on the bus first. More formally when a bus arrives, either: If capacity or fewer passengers are waiting for a bus, they will all get on the bus, or The capacity passengers with the earliest arrival times will get on the bus. Return the latest time you may arrive at the bus station to catch a bus. You cannot arrive at the same time as another passenger. Note: The arrays buses and passengers are not necessarily sorted.   Please complete the following python code precisely: ```python class Solution: def latestTimeCatchTheBus(self, buses: List[int], passengers: List[int], capacity: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(buses = [10,20], passengers = [2,17,18,19], capacity = 2) == 16\n assert candidate(buses = [20,30,10], passengers = [19,13,26,4,25,11,21], capacity = 2) == 20\n\n\ncheck(Solution().latestTimeCatchTheBus)"}
295
108
coding
Solve the programming task below in a Python markdown code block. In ChefLand, human brain speed is measured in bits per second (bps). Chef has a threshold limit of X bits per second above which his calculations are prone to errors. If Chef is currently working at Y bits per second, is he prone to errors? If Chef is prone to errors print YES, otherwise print NO. ------ Input Format ------ The only line of input contains two space separated integers X and Y — the threshold limit and the rate at which Chef is currently working at. ------ Output Format ------ If Chef is prone to errors print YES, otherwise print NO. You may print each character of the string in uppercase or lowercase (for example, the strings yes, Yes, yEs, and YES will all be treated as identical). ------ Constraints ------ $1 ≤ X, Y ≤ 100$ ----- Sample Input 1 ------ 7 9 ----- Sample Output 1 ------ YES ----- explanation 1 ------ Chef's current brain speed of $9$ bps is greater than the threshold of $7$ bps, hence Chef is prone to errors. ----- Sample Input 2 ------ 6 6 ----- Sample Output 2 ------ NO ----- explanation 2 ------ Chef's current brain speed of $6$ bps is not greater than the threshold of $6$ bps, hence Chef is not prone to errors. ----- Sample Input 3 ------ 31 53 ----- Sample Output 3 ------ YES ----- explanation 3 ------ Chef's current brain speed of $53$ bps is greater than the threshold of $31$ bps, hence Chef is prone to errors. ----- Sample Input 4 ------ 53 8 ----- Sample Output 4 ------ NO ----- explanation 4 ------ Chef's current brain speed of $8$ bps is not greater than the threshold of $53$ bps, hence Chef is not prone to errors.
{"inputs": ["7 9\n", "6 6\n", "53 8\n", "31 53\n"], "outputs": ["YES", "NO", "NO", "YES\n"]}
413
46
coding
Solve the programming task below in a Python markdown code block. Petya has k matches, placed in n matchboxes lying in a line from left to right. We know that k is divisible by n. Petya wants all boxes to have the same number of matches inside. For that, he can move a match from its box to the adjacent one in one move. How many such moves does he need to achieve the desired configuration? -----Input----- The first line contains integer n (1 ≤ n ≤ 50000). The second line contains n non-negative numbers that do not exceed 10^9, the i-th written number is the number of matches in the i-th matchbox. It is guaranteed that the total number of matches is divisible by n. -----Output----- Print the total minimum number of moves. -----Examples----- Input 6 1 6 2 5 3 7 Output 12
{"inputs": ["1\n0\n", "1\n0\n", "1\n1\n", "1\n2\n", "1\n4\n", "1\n8\n", "1\n6\n", "1\n3\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
194
86
coding
Solve the programming task below in a Python markdown code block. The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below. The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1, a2, ..., ak (i.e. some prefix of the sequence) equal to zero for some fixed k (k < n), and this should be done in the smallest possible number of moves. One move is choosing an integer i (1 ≤ i ≤ n) such that ai > 0 and an integer t (t ≥ 0) such that i + 2t ≤ n. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of ai + 2t is increased by 1. For example, let n = 4 and a = (1, 0, 1, 2), then it is possible to make move i = 3, t = 0 and get a = (1, 0, 0, 3) or to make move i = 1, t = 1 and get a = (0, 0, 2, 2) (the only possible other move is i = 1, t = 0). You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1 ≤ k < n). Input The first input line contains a single integer n. The second line contains n integers ai (0 ≤ ai ≤ 104), separated by single spaces. The input limitations for getting 20 points are: * 1 ≤ n ≤ 300 The input limitations for getting 50 points are: * 1 ≤ n ≤ 2000 The input limitations for getting 100 points are: * 1 ≤ n ≤ 105 Output Print exactly n - 1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams, or the %I64d specifier. Examples Input 4 1 0 1 2 Output 1 1 3 Input 8 1 2 3 4 5 6 7 8 Output 1 3 6 10 16 24 40
{"inputs": ["4\n1 0 0 2\n", "4\n0 0 0 1\n", "4\n1 0 0 1\n", "4\n1 0 1 2\n", "5\n4 1 4 7 6\n", "5\n2 1 4 7 6\n", "5\n2 1 4 4 6\n", "5\n2 1 4 1 6\n"], "outputs": ["1\n1\n2\n", "0\n0\n0\n", "1\n1\n2\n", "1\n1\n3\n", "4\n5\n9\n17\n", "2\n3\n7\n15\n", "2\n3\n7\n12\n", "2\n3\n7\n9\n"]}
583
185
coding
Solve the programming task below in a Python markdown code block. You're given an array $b$ of length $n$. Let's define another array $a$, also of length $n$, for which $a_i = 2^{b_i}$ ($1 \leq i \leq n$). Valerii says that every two non-intersecting subarrays of $a$ have different sums of elements. You want to determine if he is wrong. More formally, you need to determine if there exist four integers $l_1,r_1,l_2,r_2$ that satisfy the following conditions: $1 \leq l_1 \leq r_1 \lt l_2 \leq r_2 \leq n$; $a_{l_1}+a_{l_1+1}+\ldots+a_{r_1-1}+a_{r_1} = a_{l_2}+a_{l_2+1}+\ldots+a_{r_2-1}+a_{r_2}$. If such four integers exist, you will prove Valerii wrong. Do they exist? An array $c$ is a subarray of an array $d$ if $c$ can be obtained from $d$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). Description of the test cases follows. The first line of every test case contains a single integer $n$ ($2 \le n \le 1000$). The second line of every test case contains $n$ integers $b_1,b_2,\ldots,b_n$ ($0 \le b_i \le 10^9$). -----Output----- For every test case, if there exist two non-intersecting subarrays in $a$ that have the same sum, output YES on a separate line. Otherwise, output NO on a separate line. Also, note that each letter can be in any case. -----Example----- Input 2 6 4 3 0 1 2 0 2 2 5 Output YES NO -----Note----- In the first case, $a = [16,8,1,2,4,1]$. Choosing $l_1 = 1$, $r_1 = 1$, $l_2 = 2$ and $r_2 = 6$ works because $16 = (8+1+2+4+1)$. In the second case, you can verify that there is no way to select to such subarrays.
{"inputs": ["1\n3\n1 1 2\n", "1\n3\n1 1 1\n", "1\n3\n1 2 3\n", "1\n3\n1 2 3\n", "1\n3\n1 1 1\n", "1\n3\n1 1 2\n", "1\n3\n1 2 2\n", "1\n3\n0 2 1\n"], "outputs": ["YES\n", "YES\n", "NO\n", "NO\n", "YES\n", "YES\n", "YES\n", "NO\n"]}
605
134
coding
Solve the programming task below in a Python markdown code block. Given A, B, and C as the sides of a triangle, find whether the triangle is *scalene*. Note: A triangle is said to be *scalene* if all three sides of the triangle are distinct. It is guaranteed that the sides represent a valid triangle. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of three space-separated integers A, B, and C — the length of the three sides of the triangle. ------ Output Format ------ For each test case, output on a new line, YES, if the triangle is *scalene*, and NO otherwise. You may print each character of the string in uppercase or lowercase. For example, YES, yes, Yes, and yEs are all considered identical. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ A ≤B ≤C ≤ 10$ $C < (A+B)$ ----- Sample Input 1 ------ 4 2 3 4 1 2 2 2 2 2 3 5 6 ----- Sample Output 1 ------ YES NO NO YES ----- explanation 1 ------ Test case $1$: The side lengths are $2, 3, $ and $4$. Since no two side lengths are equal, the triangle is scalene. Test case $2$: The side lengths are $1, 2, $ and $2$. The sides $B$ and $C$ have the same length. Thus, the triangle is not scalene. Test case $3$: The side lengths are $2, 2, $ and $2$. The sides $A, B,$ and $C$ have the same length. Thus, the triangle is not scalene. Test case $4$: The side lengths are $3, 5, $ and $6$. Since no two side lengths are equal, the triangle is scalene.
{"inputs": ["4\n2 3 4\n1 2 2\n2 2 2\n3 5 6\n"], "outputs": ["YES\nNO\nNO\nYES"]}
428
43
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, of equal length n. In one operation, you can swap the values of any two indices of nums1. The cost of this operation is the sum of the indices. Find the minimum total cost of performing the given operation any number of times such that nums1[i] != nums2[i] for all 0 <= i <= n - 1 after performing all the operations. Return the minimum total cost such that nums1 and nums2 satisfy the above condition. In case it is not possible, return -1.   Please complete the following python code precisely: ```python class Solution: def minimumTotalCost(self, nums1: List[int], nums2: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums1 = [1,2,3,4,5], nums2 = [1,2,3,4,5]) == 10\n assert candidate(nums1 = [2,2,2,1,3], nums2 = [1,2,2,3,3]) == 10\n assert candidate(nums1 = [1,2,2], nums2 = [1,2,2]) == -1\n\n\ncheck(Solution().minimumTotalCost)"}
176
119
coding
Solve the programming task below in a Python markdown code block. Write a program which manipulates a sequence A = {a0, a1, . . . , an-1} with the following operations: * find(s, t): report the minimum element in as, as+1, . . . ,at. * update(i, x): change ai to x. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * If comi is 0, then 0 ≤ xi < n, 0 ≤ yi < 231-1. * If comi is 1, then 0 ≤ xi < n, 0 ≤ yi < n. Input n q com0 x0 y0 com1 x1 y1 ... comq−1 xq−1 yq−1 In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, q queries are given where com represents the type of queries. '0' denotes update(xi, yi) and '1' denotes find(xi, yi). Output For each find operation, print the minimum element. Examples Input 3 5 0 0 1 0 1 2 0 2 3 1 0 2 1 1 2 Output 1 2 Input 1 3 1 0 0 0 0 5 1 0 0 Output 2147483647 5
{"inputs": ["1 3\n1 0 1\n0 0 5\n0 0 0", "1 3\n1 0 0\n0 0 5\n1 0 0", "1 3\n1 -1 0\n0 0 5\n1 0 0", "1 3\n1 -1 0\n0 0 9\n1 0 0", "1 3\n1 -1 0\n0 0 5\n0 0 0", "1 3\n1 -1 0\n0 0 4\n1 0 0", "1 3\n1 -1 0\n1 0 9\n1 0 0", "1 3\n1 -1 1\n0 0 5\n0 0 0"], "outputs": ["2147483647\n", "2147483647\n5", "2147483647\n5\n", "2147483647\n9\n", "2147483647\n", "2147483647\n4\n", "2147483647\n2147483647\n2147483647\n", "2147483647\n"]}
375
323
coding
Solve the programming task below in a Python markdown code block. Akash is stuck in a N \times N grid, where N is odd. The rows of the grid are numbered 1 to N from top to bottom, and the columns are numbered 1 to N from left to right. The cell at the intersection of the i-th row and j-th column will be denoted (i, j). The grid has a unique center cell — ((N+1)/2, (N+1)/2). For example, when N = 5 the center is cell (3, 3). Akash is currently at cell (x_{s}, y_{s}). He would like to reach the exit of the grid, which is located at the center. It is guaranteed that (x_{s}, y_{s}) is not the center. Suppose Akash is at cell (x, y). He can make the following movements: He can freely move along diagonals, i.e, to cells (x-1, y-1), (x-1, y+1), (x+1, y-1), (x+1, y+1) He can move one step horizontally or vertically with the cost of 1 gold coin, i.e, to cells (x, y-1), (x, y+1), (x-1, y), (x+1, y) Note that Akash is not allowed to make a move that will take him out of bounds of the grid. Akash would like to minimize the number of coins required to reach the center. Please help him find this number. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line of input, containing three space-separated integers N, x_{s}, y_{s} — the size of the grid and the coordinates of Akash's starting cell. ------ Output Format ------ For each test case, output in a single line the minimum number of gold coins Akash needs to reach the center. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $3 ≤ N < 2 \cdot 10^{4}$ $N$ is always odd. $1 ≤ x_{s}, y_{s} ≤ N$ ----- Sample Input 1 ------ 2 3 2 1 5 3 1 ----- Sample Output 1 ------ 1 0 ----- explanation 1 ------ Test case 1: For a $3 \times 3$ grid, the center is at $(2,2)$. It is not possible to reach $(2,2)$ from $(2,1)$ using only diagonal moves. So, Akash will directly go to the center using 1 gold coin. Test case 2: $N = 5$, so the center is $(3, 3)$. Akash can go from $(3,1)$ to $(2,2)$ with a diagonal move, then from $(2,2)$ to $(3, 3)$ with another diagonal move. So, he needs zero coins. .png)
{"inputs": ["2\n3 2 1\n5 3 1"], "outputs": ["1\n0\n"]}
669
27
coding
Solve the programming task below in a Python markdown code block. You're given a string of dominos. For each slot, there are 3 options: * "|" represents a standing domino * "/" represents a knocked over domino * " " represents a space where there is no domino For example: ```python "||| ||||//| |/" ``` What you must do is find the resulting string if the first domino is pushed over. Now, tipping a domino will cause the next domino to its right to fall over as well, but if a domino is already tipped over, or there is a domino missing, the reaction will stop. So in out example above, the result would be: "/// ||||//| |/" since the reaction would stop as soon as it gets to a space. Also feel free to reuse/extend the following starter code: ```python def domino_reaction(s): ```
{"functional": "_inputs = [['||| ||||//| |/'], ['|||||'], [' ///'], [''], [' '], [' |'], ['||||| |||'], ['|||||/|||']]\n_outputs = [['/// ||||//| |/'], ['/////'], [' ///'], [''], [' '], [' |'], ['///// |||'], ['//////|||']]\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(domino_reaction(*i), o[0])"}
200
219
coding
Solve the programming task below in a Python markdown code block. A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b. You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars. -----Input----- You are given four integers V_1, V_2, V_3, V_{m}(1 ≤ V_{i} ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V_1 > V_2 > V_3. -----Output----- Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes). -----Examples----- Input 50 30 10 10 Output 50 30 10 Input 100 50 10 21 Output -1 -----Note----- In first test case all conditions for cars' sizes are satisfied. In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20.
{"inputs": ["3 2 1 1\n", "5 4 3 1\n", "4 3 2 1\n", "98 2 1 1\n", "27 3 2 3\n", "13 7 6 2\n", "15 6 4 5\n", "21 3 1 3\n"], "outputs": ["4\n3\n1\n", "-1\n", "4\n3\n2\n", "98\n3\n1\n", "-1\n", "-1\n", "15\n11\n5\n", "-1\n"]}
429
142
coding
Solve the programming task below in a Python markdown code block. Write a function that accepts two parameters, i) a string (containing a list of words) and ii) an integer (n). The function should alphabetize the list based on the nth letter of each word. The letters should be compared case-insensitive. If both letters are the same, order them normally (lexicographically), again, case-insensitive. example: ```javascript function sortIt('bid, zag', 2) //=> 'zag, bid' ``` ```ruby function sortIt('bid, zag', 2) //=> 'zag, bid' ``` ```python function sortIt('bid, zag', 2) #=> 'zag, bid' ``` The length of all words provided in the list will be >= n. The format will be "x, x, x". In Haskell you'll get a list of `String`s instead. Also feel free to reuse/extend the following starter code: ```python def sort_it(list_, n): ```
{"functional": "_inputs = [['bill, bell, ball, bull', 2], ['words, wordz, wordy, wording', 5], ['he, hi, ha, ho', 2], ['zephyr, yellow, wax, a, ba, cat', 1], ['hello, how, are, you, doing, today', 3]]\n_outputs = [['ball, bell, bill, bull'], ['wording, words, wordy, wordz'], ['ha, he, hi, ho'], ['a, ba, cat, wax, yellow, zephyr'], ['today, are, doing, hello, you, how']]\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(sort_it(*i), o[0])"}
220
278
coding
Solve the programming task below in a Python markdown code block. Parsa has a humongous tree on $n$ vertices. On each vertex $v$ he has written two integers $l_v$ and $r_v$. To make Parsa's tree look even more majestic, Nima wants to assign a number $a_v$ ($l_v \le a_v \le r_v$) to each vertex $v$ such that the beauty of Parsa's tree is maximized. Nima's sense of the beauty is rather bizarre. He defines the beauty of the tree as the sum of $|a_u - a_v|$ over all edges $(u, v)$ of the tree. Since Parsa's tree is too large, Nima can't maximize its beauty on his own. Your task is to find the maximum possible beauty for Parsa's tree. -----Input----- The first line contains an integer $t$ $(1\le t\le 250)$ — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ $(2\le n\le 10^5)$ — the number of vertices in Parsa's tree. The $i$-th of the following $n$ lines contains two integers $l_i$ and $r_i$ $(1 \le l_i \le r_i \le 10^9)$. Each of the next $n-1$ lines contains two integers $u$ and $v$ $(1 \le u , v \le n, u\neq v)$ meaning that there is an edge between the vertices $u$ and $v$ in Parsa's tree. It is guaranteed that the given graph is a tree. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print the maximum possible beauty for Parsa's tree. -----Examples----- Input 3 2 1 6 3 8 1 2 3 1 3 4 6 7 9 1 2 2 3 6 3 14 12 20 12 19 2 12 10 17 3 17 3 2 6 5 1 5 2 6 4 6 Output 7 8 62 -----Note----- The trees in the example: In the first test case, one possible assignment is $a = \{1, 8\}$ which results in $|1 - 8| = 7$. In the second test case, one of the possible assignments is $a = \{1, 5, 9\}$ which results in a beauty of $|1 - 5| + |5 - 9| = 8$
{"inputs": ["3\n2\n1 7\n3 8\n1 2\n3\n1 3\n10 6\n7 9\n1 2\n2 3\n6\n3 1\n12 20\n6 19\n3 16\n5 17\n3 17\n3 3\n6 5\n1 5\n2 6\n4 6\n", "3\n2\n0 6\n3 8\n1 2\n3\n1 3\n4 6\n7 9\n1 2\n2 3\n6\n3 14\n12 21\n12 19\n3 12\n11 0\n3 0\n3 5\n6 5\n1 5\n2 6\n4 6\n", "3\n2\n1 6\n3 2\n1 2\n3\n1 3\n4 6\n7 9\n1 2\n2 3\n6\n3 14\n8 20\n12 19\n2 2\n10 17\n3 17\n3 2\n6 5\n1 6\n2 6\n4 6\n", "3\n2\n1 6\n3 6\n1 2\n3\n1 3\n4 6\n7 9\n1 2\n2 3\n6\n4 7\n12 20\n12 19\n2 12\n11 3\n0 17\n3 2\n6 5\n1 5\n2 6\n4 6\n", "3\n2\n1 6\n3 8\n1 2\n3\n1 5\n4 6\n2 9\n1 2\n2 3\n6\n3 14\n12 20\n5 5\n2 19\n10 17\n3 17\n3 5\n6 5\n1 6\n2 6\n4 6\n", "3\n2\n1 6\n3 8\n1 2\n3\n1 3\n10 6\n7 9\n1 2\n2 3\n6\n4 14\n9 29\n6 19\n5 20\n5 17\n3 19\n3 3\n6 5\n1 5\n2 6\n4 6\n", "3\n2\n1 6\n3 6\n1 2\n3\n1 0\n4 6\n7 2\n1 2\n2 3\n6\n4 2\n4 20\n21 32\n2 14\n10 17\n3 17\n3 1\n6 5\n1 5\n2 6\n4 6\n", "3\n2\n1 6\n3 6\n1 2\n3\n1 0\n4 6\n7 2\n1 2\n2 3\n6\n4 2\n4 20\n21 32\n2 14\n10 10\n3 17\n3 1\n6 5\n1 5\n2 6\n4 6\n"], "outputs": ["7\n12\n60\n", "8\n8\n66\n", "4\n8\n56\n", "5\n8\n58\n", "7\n9\n70\n", "7\n12\n70\n", "5\n10\n87\n", "5\n10\n73\n"]}
619
826
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an array of integers nums, you start with an initial positive value startValue. In each iteration, you calculate the step by step sum of startValue plus elements in nums (from left to right). Return the minimum positive value of startValue such that the step by step sum is never less than 1.   Please complete the following python code precisely: ```python class Solution: def minStartValue(self, nums: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [-3,2,-3,4,2]) == 5\n assert candidate(nums = [1,2]) == 1\n assert candidate(nums = [1,-2,-3]) == 5\n\n\ncheck(Solution().minStartValue)"}
116
70
coding
Solve the programming task below in a Python markdown code block. When l is an odd number, the median of l numbers a_1, a_2, ..., a_l is the (\frac{l+1}{2})-th largest value among a_1, a_2, ..., a_l. You are given N numbers X_1, X_2, ..., X_N, where N is an even number. For each i = 1, 2, ..., N, let the median of X_1, X_2, ..., X_N excluding X_i, that is, the median of X_1, X_2, ..., X_{i-1}, X_{i+1}, ..., X_N be B_i. Find B_i for each i = 1, 2, ..., N. -----Constraints----- - 2 \leq N \leq 200000 - N is even. - 1 \leq X_i \leq 10^9 - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: N X_1 X_2 ... X_N -----Output----- Print N lines. The i-th line should contain B_i. -----Sample Input----- 4 2 4 4 3 -----Sample Output----- 4 3 3 4 - Since the median of X_2, X_3, X_4 is 4, B_1 = 4. - Since the median of X_1, X_3, X_4 is 3, B_2 = 3. - Since the median of X_1, X_2, X_4 is 3, B_3 = 3. - Since the median of X_1, X_2, X_3 is 4, B_4 = 4.
{"inputs": ["2\n1 2\n", "4\n2 4 4 3\n", "6\n5 5 4 4 3 3\n"], "outputs": ["2\n1\n", "4\n3\n3\n4\n", "4\n4\n4\n4\n4\n4\n"]}
396
72
coding
Solve the programming task below in a Python markdown code block. There are n lights aligned in a row. These lights are numbered 1 to n from left to right. Initially some of the lights are switched on. Shaass wants to switch all the lights on. At each step he can switch a light on (this light should be switched off at that moment) if there's at least one adjacent light which is already switched on. He knows the initial state of lights and he's wondering how many different ways there exist to switch all the lights on. Please find the required number of ways modulo 1000000007 (10^9 + 7). -----Input----- The first line of the input contains two integers n and m where n is the number of lights in the sequence and m is the number of lights which are initially switched on, (1 ≤ n ≤ 1000, 1 ≤ m ≤ n). The second line contains m distinct integers, each between 1 to n inclusive, denoting the indices of lights which are initially switched on. -----Output----- In the only line of the output print the number of different possible ways to switch on all the lights modulo 1000000007 (10^9 + 7). -----Examples----- Input 3 1 1 Output 1 Input 4 2 1 4 Output 2 Input 11 2 4 8 Output 6720
{"inputs": ["3 1\n1\n", "1 1\n1\n", "1 1\n1\n", "5 1\n1\n", "4 1\n1\n", "3 1\n1\n", "4 2\n1 4\n", "4 2\n1 3\n"], "outputs": ["1\n", "1\n", "1", "1\n", "1\n", "1", "2\n", "2\n"]}
317
104
coding
Solve the programming task below in a Python markdown code block. Alice guesses the strings that Bob made for her. At first, Bob came up with the secret string $a$ consisting of lowercase English letters. The string $a$ has a length of $2$ or more characters. Then, from string $a$ he builds a new string $b$ and offers Alice the string $b$ so that she can guess the string $a$. Bob builds $b$ from $a$ as follows: he writes all the substrings of length $2$ of the string $a$ in the order from left to right, and then joins them in the same order into the string $b$. For example, if Bob came up with the string $a$="abac", then all the substrings of length $2$ of the string $a$ are: "ab", "ba", "ac". Therefore, the string $b$="abbaac". You are given the string $b$. Help Alice to guess the string $a$ that Bob came up with. It is guaranteed that $b$ was built according to the algorithm given above. It can be proved that the answer to the problem is unique. -----Input----- The first line contains a single positive integer $t$ ($1 \le t \le 1000$) — the number of test cases in the test. Then $t$ test cases follow. Each test case consists of one line in which the string $b$ is written, consisting of lowercase English letters ($2 \le |b| \le 100$) — the string Bob came up with, where $|b|$ is the length of the string $b$. It is guaranteed that $b$ was built according to the algorithm given above. -----Output----- Output $t$ answers to test cases. Each answer is the secret string $a$, consisting of lowercase English letters, that Bob came up with. -----Example----- Input 4 abbaac ac bccddaaf zzzzzzzzzz Output abac ac bcdaf zzzzzz -----Note----- The first test case is explained in the statement. In the second test case, Bob came up with the string $a$="ac", the string $a$ has a length $2$, so the string $b$ is equal to the string $a$. In the third test case, Bob came up with the string $a$="bcdaf", substrings of length $2$ of string $a$ are: "bc", "cd", "da", "af", so the string $b$="bccddaaf".
{"inputs": ["1\nassaad\n", "1\nassaad\n", "1\nbssaad\n", "1\ndaassb\n", "2\nabbaac\nac\n", "2\nabbaac\nac\n", "2\nabbaad\nac\n", "2\nabbaad\nca\n"], "outputs": ["asad\n", "asad\n", "bsad\n", "dasb\n", "abac\nac\n", "abac\nac\n", "abad\nac\n", "abad\nca\n"]}
558
120
coding
Solve the programming task below in a Python markdown code block. You are given an integer array $a$ of length $n$. Does there exist an array $b$ consisting of $n+1$ positive integers such that $a_i=\gcd (b_i,b_{i+1})$ for all $i$ ($1 \leq i \leq n$)? Note that $\gcd(x, y)$ denotes the greatest common divisor (GCD) of integers $x$ and $y$. -----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 an integer $n$ ($1 \leq n \leq 10^5$) — the length of the array $a$. The second line of each test case contains $n$ space-separated integers $a_1,a_2,\ldots,a_n$ representing the array $a$ ($1 \leq a_i \leq 10^4$). It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case, output "YES" if such $b$ exists, otherwise output "NO". You can print each letter in any case (upper or lower). -----Examples----- Input 4 1 343 2 4 2 3 4 2 4 4 1 1 1 1 Output YES YES NO YES -----Note----- In the first test case, we can take $b=[343,343]$. In the second test case, one possibility for $b$ is $b=[12,8,6]$. In the third test case, it can be proved that there does not exist any array $b$ that fulfills all the conditions.
{"inputs": ["1\n3\n6 3 2\n", "1\n3\n6 9 2\n", "1\n3\n12 4 3\n", "1\n3\n10 5 4\n", "1\n3\n35 7 5\n", "1\n3\n96 64 3\n", "1\n3\n22 11 18\n", "1\n3\n101 1 101\n"], "outputs": ["NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n"]}
423
146
coding
Solve the programming task below in a Python markdown code block. You are given an array $a$ consisting of $n$ non-negative integers. It is guaranteed that $a$ is sorted from small to large. For each operation, we generate a new array $b_i=a_{i+1}-a_{i}$ for $1 \le i < n$. Then we sort $b$ from small to large, replace $a$ with $b$, and decrease $n$ by $1$. After performing $n-1$ operations, $n$ becomes $1$. You need to output the only integer in array $a$ (that is to say, you need to output $a_1$). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $n$ ($2\le n\le 10^5$) — the length of the array $a$. The second line contains $n$ integers $a_1,a_2,\dots,a_n$ ($0\le a_1\le \ldots\le a_n \le 5\cdot 10^5$) — the array $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2.5\cdot 10^5$, and the sum of $a_n$ over all test cases does not exceed $5\cdot 10^5$. -----Output----- For each test case, output the answer on a new line. -----Examples----- Input 5 3 1 10 100 4 4 8 9 13 5 0 0 0 8 13 6 2 4 8 16 32 64 7 0 0 0 0 0 0 0 Output 81 3 1 2 0 -----Note----- To simplify the notes, let $\operatorname{sort}(a)$ denote the array you get by sorting $a$ from small to large. In the first test case, $a=[1,10,100]$ at first. After the first operation, $a=\operatorname{sort}([10-1,100-10])=[9,90]$. After the second operation, $a=\operatorname{sort}([90-9])=[81]$. In the second test case, $a=[4,8,9,13]$ at first. After the first operation, $a=\operatorname{sort}([8-4,9-8,13-9])=[1,4,4]$. After the second operation, $a=\operatorname{sort}([4-1,4-4])=[0,3]$. After the last operation, $a=\operatorname{sort}([3-0])=[3]$.
{"inputs": ["1\n4\n1 145 10441 497785\n", "1\n4\n1 143 10297 497785\n", "5\n3\n1 10 100\n4\n4 8 9 13\n5\n0 0 0 8 13\n6\n2 4 8 16 32 64\n7\n0 0 0 0 0 0 0\n", "1\n104\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 102 6504 500000\n", "1\n143\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 145 10443 498077\n", "1\n144\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 145 10443 498077\n", "1\n145\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 145 10443 498077\n", "1\n146\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 145 10443 498077\n"], "outputs": ["466896\n", "467322\n", "81\n3\n1\n2\n0\n", "183892\n", "1\n", "1\n", "1\n", "1\n"]}
666
1,642
coding
Solve the programming task below in a Python markdown code block. We all know how to calculate $a^{b}$ using $\boldsymbol{b}$ operations by multiplying $1$ by $\boldsymbol{a}$ a total of $\boldsymbol{b}$ times. The drawback to this method is that $\boldsymbol{b}$ can be large, which makes exponentiation very slow. There is a well known method called Russian Peasant Multiplication that you can read about here. Now let's use this to raise some complex numbers to powers! You're given $\textit{q}$ queries where each query consists of four integers: $\boldsymbol{a}$, $\boldsymbol{b}$, $\boldsymbol{\mbox{k}}$, and $m$. For each query, calculate $(a+b\cdot i)^k=c+d\cdot i$ (where $\boldsymbol{i}$ is an imaginary unit) and then print the respective values of $c\ \textbf{mod}\ m$ and $d\:\textbf{mod}\:m$ as two space-separated integers on a new line. Input Format The first line contains a single integer, $\textit{q}$, denoting the number of queries. Each of the $\textit{q}$ subsequent lines describes a query in the form of four space-separated integers: $\boldsymbol{a}$, $\boldsymbol{b}$, $\boldsymbol{\mbox{k}}$, and $m$ (respectively). Constraints $1\leq q\leq10^5$ $0\leq k\leq10^{18}$ $2\leq m\leq10^9$ $0\leq a,b\leq m$ Output Format For each query, print the two space-separated integers denoting the respective values of $c\ \textbf{mod}\ m$ and $d\:\textbf{mod}\:m$ on a new line. Sample Input 3 2 0 9 1000 0 1 5 10 8 2 10 1000000000 Sample Output 512 0 0 1 880332800 927506432 Explanation In the first query, we have $\boldsymbol{a}=2$, $\boldsymbol{b=0}$, $k=9$, $m=1000$. We calculate the following: $2^9=512$ $i^5=i$
{"inputs": ["3\n2 0 9 1000\n0 1 5 10\n8 2 10 1000000000\n"], "outputs": ["512 0\n0 1\n880332800 927506432\n"]}
554
80
coding
Solve the programming task below in a Python markdown code block. In a coding contest, there are prizes for the top rankers. The prize scheme is as follows: Top 10 participants receive rupees X each. Participants with rank 11 to 100 (both inclusive) receive rupees Y each. Find the total prize money over all the contestants. ------ 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 - the prize for top 10 rankers and the prize for ranks 11 to 100 respectively. ------ Output Format ------ For each test case, output the total prize money over all the contestants. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ Y ≤ X ≤ 1000$ ----- Sample Input 1 ------ 4 1000 100 1000 1000 80 1 400 30 ----- Sample Output 1 ------ 19000 100000 890 6700 ----- explanation 1 ------ Test Case $1$: Top $10$ participants receive rupees $1000$ and next $90$ participants receive rupees $100$ each. So, total prize money $= 10 \cdot 1000 + 90 \cdot 100 = 19000$. Test Case $2$: Top $10$ participants receive rupees $1000$ and next $90$ participants receive rupees $1000$ each. So, total prize money $= 10 \cdot 1000 + 90 \cdot 1000 = 100000$. Test Case $3$: Top $10$ participants receive rupees $80$ and next $90$ participants receive rupee $1$ each. So, total prize money $= 10 \cdot 80 + 90 \cdot 1 = 890$. Test Case $4$: Top $10$ participants receive rupees $400$ and next $90$ participants receive rupees $30$ each. So, total prize money $= 10 \cdot 400 + 90 \cdot 30 = 6700$.
{"inputs": ["4\n1000 100\n1000 1000\n80 1\n400 30\n"], "outputs": ["19000\n100000\n890\n6700\n"]}
543
65
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. Chef is playing a game with his friend Misha. They have a pile containg N coins. Players take alternate turns, removing some coins from the pile. On each turn, a player can remove either one coin or coins equal to some prime power (i.e. p^{x} coins, where p - prime number and x - positive integer). Game ends when the pile becomes empty. The player who can not make a move in his turn loses. Chef plays first. Your task is to find out who will win the game, provided that both of the player play optimally. ------ Input ------ The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains one integer N. ------ Output ------ For each test case, output a single line containing one word - the name of the winner of the game. Print "Chef" (without quotes) if Chef wins the game, print "Misha" (without quotes) otherwise. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 10^{9}$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≤ N ≤ 10$ Subtask #2 (30 points): $1 ≤ N ≤ 10^{4}$ Subtask #3 (50 points): No additional constraints. ----- Sample Input 1 ------ 2 1 8 ----- Sample Output 1 ------ Chef Chef ----- explanation 1 ------ Example case 1. Chef will remove the only coin from the pile and will win the game. Example case 2. Chef will remove all 8 coins from the pile and win the game. Chef can remove 8 coins because 8 is a prime power, as 8 = 23.
{"inputs": ["2\n1\n8", "2\n0\n8", "2\n0\n6", "2\n0\n1", "2\n0\n0", "2\n1\n0", "2\n1\n1", "2\n0\n2"], "outputs": ["Chef\nChef", "Misha\nChef\n", "Misha\nMisha\n", "Misha\nChef\n", "Misha\nMisha\n", "Chef\nMisha\n", "Chef\nChef\n", "Misha\nChef\n"]}
417
117
coding
Solve the programming task below in a Python markdown code block. Chef usually likes to play cricket, but now, he is bored of playing it too much, so he is trying new games with strings. Chef's friend Dustin gave him binary strings $S$ and $R$, each with length $N$, and told him to make them identical. However, unlike Dustin, Chef does not have any superpower and Dustin lets Chef perform only operations of one type: choose any pair of integers $(i, j)$ such that $1 \le i, j \le N$ and swap the $i$-th and $j$-th character of $S$. He may perform any number of operations (including zero). For Chef, this is much harder than cricket and he is asking for your help. Tell him whether it is possible to change the string $S$ to the target string $R$ only using operations of the given type. -----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 a binary string $S$. - The third line contains a binary string $R$. -----Output----- For each test case, print a single line containing the string "YES" if it is possible to change $S$ to $R$ or "NO" if it is impossible (without quotes). -----Constraints----- - $1 \le T \le 400$ - $1 \le N \le 100$ - $|S| = |R| = N$ - $S$ and $R$ will consist of only '1' and '0' -----Example Input----- 2 5 11000 01001 3 110 001 -----Example Output----- YES NO -----Explanation----- Example case 1: Chef can perform one operation with $(i, j) = (1, 5)$. Then, $S$ will be "01001", which is equal to $R$. Example case 2: There is no sequence of operations which would make $S$ equal to $R$.
{"inputs": ["2\n5\n11000\n01001\n3\n110\n001"], "outputs": ["YES\nNO"]}
478
38
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given two sparse matrices mat1 of size m x k and mat2 of size k x n, return the result of mat1 x mat2. You may assume that multiplication is always possible.   Please complete the following python code precisely: ```python class Solution: def multiply(self, mat1: List[List[int]], mat2: List[List[int]]) -> List[List[int]]: ```
{"functional": "def check(candidate):\n assert candidate(mat1 = [[1,0,0],[-1,0,3]], mat2 = [[7,0,0],[0,0,0],[0,0,1]]) == [[7,0,0],[-7,0,3]]\n assert candidate(mat1 = [[0]], mat2 = [[0]]) == [[0]]\n\n\ncheck(Solution().multiply)"}
97
96
coding
Solve the programming task below in a Python markdown code block. Given a string, s, return a new string that orders the characters in order of frequency. The returned string should have the same number of characters as the original string. Make your transformation stable, meaning characters that compare equal should stay in their original order in the string s. ```python most_common("Hello world") => "lllooHe wrd" most_common("Hello He worldwrd") => "lllHeo He wordwrd" ``` Explanation: In the `hello world` example, there are 3 `'l'`characters, 2 `'o'`characters, and one each of `'H'`, `'e'`, `' '`, `'w'`, `'r'`, and `'d'`characters. Since `'He wrd'`are all tied, they occur in the same relative order that they do in the original string, `'Hello world'`. Note that ties don't just happen in the case of characters occuring once in a string. See the second example, `most_common("Hello He worldwrd")`should return `'lllHeo He wordwrd'`, not `'lllHHeeoo wwrrdd'`. **This is a key behavior if this method were to be used to transform a string on multiple passes.** Also feel free to reuse/extend the following starter code: ```python def most_common(s): ```
{"functional": "_inputs = [['Hello world'], [''], ['wubz dermatoglyphics'], ['Four score and seven years ago'], ['Hello He worldwrd']]\n_outputs = [['lllooHe wrd'], [''], ['wubz dermatoglyphics'], [' eeeeorsorasarsaonnFucdvyg'], ['lllHeo He wordwrd']]\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(most_common(*i), o[0])"}
305
222
coding
Solve the programming task below in a Python markdown code block. We have 3 equations with 3 unknowns x, y, and z and we are to solve for these unknowns. Equations 4x -3y +z = -10, 2x +y +3z = 0, and -x +2y -5z = 17 will be passed in as an array of [[4, -3, 1, -10], [2, 1, 3, 0], [-1, 2, -5, 17]] and the result should be returned as an array like [1, 4, -2] (i.e. [x, y, z]). Note: In C++ do not use new or malloc to allocate memory for the returned variable as allocated memory will not be freed in the Test Cases. Setting the variable as static will do. Also feel free to reuse/extend the following starter code: ```python def solve_eq(eq): ```
{"functional": "_inputs = [[[[4, -3, 1, -10], [2, 1, 3, 0], [-1, 2, -5, 17]]], [[[2, 1, 3, 10], [-3, -2, 7, 5], [3, 3, -4, 7]]], [[[3, 2, 0, 7], [-4, 0, 3, -6], [0, -2, -6, -10]]], [[[4, 2, -5, -21], [2, -2, 1, 7], [4, 3, -1, -1]]]]\n_outputs = [[[1, 4, -2]], [[-1, 6, 2]], [[3, -1, 2]], [[1, 0, 5]]]\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_eq(*i), o[0])"}
215
340
coding
Solve the programming task below in a Python markdown code block. #### Background: A linear regression line has an equation in the form `$Y = a + bX$`, where `$X$` is the explanatory variable and `$Y$` is the dependent variable. The parameter `$b$` represents the *slope* of the line, while `$a$` is called the *intercept* (the value of `$y$` when `$x = 0$`). For more details visit the related [wikipedia page](http://en.wikipedia.org/wiki/Simple_linear_regression). --- --- #### Task: The function that you have to write accepts two list/array, `$x$` and `$y$`, representing the coordinates of the points to regress (so that, for example, the first point has coordinates (`x[0], y[0]`)). Your function should return a tuple (in Python) or an array (any other language) of two elements: `a` (intercept) and `b` (slope) in this order. You must round your result to the first 4 decimal digits #### Formula: `$x_i$` and `$y_i$` is `$x$` and `$y$` co-ordinate of `$i$`-th point; `$n$` is length of input. `$a = \dfrac{\sum x_i^2\cdot \sum y_i - \sum x_i \cdot\sum x_iy_i}{n\sum x_i^2 - (\sum x_i)^2}$` `$b = \dfrac{n\sum x_i y_i - \sum x_i \cdot \sum y_i}{n\sum x^2_i - (\sum x_i)^2}$` #### Examples: ```python regressionLine([25,30,35,40,45,50], [78,70,65,58,48,42]) == (114.381, -1.4457) regressionLine([56,42,72,36,63,47,55,49,38,42,68,60], [147,125,160,118,149,128,150,145,115,140,152,155]) == (80.7777, 1.138) ``` ---- Also feel free to reuse/extend the following starter code: ```python def regressionLine(x, y): ```
{"functional": "_inputs = [[[25, 30, 35, 40, 45, 50], [78, 70, 65, 58, 48, 42]], [[56, 42, 72, 36, 63, 47, 55, 49, 38, 42, 68, 60], [147, 125, 160, 118, 149, 128, 150, 145, 115, 140, 152, 155]], [[0, 10, 20, 30, 40], [0.51, 0.55, 0.57, 0.59, 0.63]]]\n_outputs = [[[114.381, -1.4457]], [[80.7777, 1.138]], [[0.514, 0.0028]]]\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(regressionLine(*i), o[0])"}
574
402
coding
Solve the programming task below in a Python markdown code block. Final kata of the series (highly recommended to compute [layers](https://www.codewars.com/kata/progressive-spiral-number-position/) and [branch](https://www.codewars.com/kata/progressive-spiral-number-branch/) first to get a good idea), this is a blatant ripoff of [the one offered on AoC](http://adventofcode.com/2017/day/3). Given a number, return the Manhattan distance considering the core of the spiral (the `1` cell) as 0 and counting each step up, right, down or left to reach a given cell. For example, using our beloved 5x5 square: ``` 17 16 15 14 13 4 3 2 3 4 18 05 04 03 12 3 2 1 2 3 19 06 01 02 11 => 2 1 0 1 2 20 07 08 09 10 3 2 1 2 3 21 22 23 24 25 4 3 2 3 4 ``` And thus your code should behave like this: ```python distance(1) == 0 distance(5) == 2 distance(25) == 4 distance(30) == 5 distance(50) == 7 ``` Just be ready for larger numbers, as usual always positive. *[Dedicated to [swiftest learner I met in a long while](https://www.codewars.com/users/irbekrm/)]* Also feel free to reuse/extend the following starter code: ```python def distance(n): ```
{"functional": "_inputs = [[1], [5], [25], [30], [96], [50], [66], [306], [307], [308]]\n_outputs = [[0], [2], [4], [5], [5], [7], [7], [17], [18], [17]]\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(distance(*i), o[0])"}
414
221
coding
Solve the programming task below in a Python markdown code block. Consider a game, wherein the player has to guess a target word. All the player knows is the length of the target word. To help them in their goal, the game will accept guesses, and return the number of letters that are in the correct position. Write a method that, given the correct word and the player's guess, returns this number. For example, here's a possible thought process for someone trying to guess the word "dog": ```cs CountCorrectCharacters("dog", "car"); //0 (No letters are in the correct position) CountCorrectCharacters("dog", "god"); //1 ("o") CountCorrectCharacters("dog", "cog"); //2 ("o" and "g") CountCorrectCharacters("dog", "cod"); //1 ("o") CountCorrectCharacters("dog", "bog"); //2 ("o" and "g") CountCorrectCharacters("dog", "dog"); //3 (Correct!) ``` ```python count_correct_characters("dog", "car"); #0 (No letters are in the correct position) count_correct_characters("dog", "god"); #1 ("o") count_correct_characters("dog", "cog"); #2 ("o" and "g") count_correct_characters("dog", "cod"); #1 ("o") count_correct_characters("dog", "bog"); #2 ("o" and "g") count_correct_characters("dog", "dog"); #3 (Correct!) ``` The caller should ensure that the guessed word is always the same length as the correct word, but since it could cause problems if this were not the case, you need to check for this eventuality: ```cs //Throw an InvalidOperationException if the two parameters are of different lengths. ``` ```python #Raise an exception if the two parameters are of different lengths. ``` You may assume, however, that the two parameters will always be in the same case. Also feel free to reuse/extend the following starter code: ```python def count_correct_characters(correct, guess): ```
{"functional": "_inputs = [['dog', 'car'], ['dog', 'god'], ['dog', 'cog'], ['dog', 'cod'], ['dog', 'bog'], ['dog', 'dog'], ['abcde', 'abcde'], ['same', 'same'], ['z', 'z']]\n_outputs = [[0], [1], [2], [1], [2], [3], [5], [4], [1]]\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(count_correct_characters(*i), o[0])"}
431
234
coding
Solve the programming task below in a Python markdown code block. There is a farm whose length and width are A yard and B yard, respectively. A farmer, John, made a vertical road and a horizontal road inside the farm from one border to another, as shown below: (The gray part represents the roads.) What is the area of this yard excluding the roads? Find it. -----Note----- It can be proved that the positions of the roads do not affect the area. -----Constraints----- - A is an integer between 2 and 100 (inclusive). - B is an integer between 2 and 100 (inclusive). -----Input----- Input is given from Standard Input in the following format: A B -----Output----- Print the area of this yard excluding the roads (in square yards). -----Sample Input----- 2 2 -----Sample Output----- 1 In this case, the area is 1 square yard.
{"inputs": ["6 7", "2 1", "0 2", "0 4", "9 2", "2 4", "0 0", "7 2"], "outputs": ["30\n", "0\n", "-1\n", "-3\n", "8\n", "3\n", "1\n", "6\n"]}
192
79
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a 0-indexed binary array nums of length n. nums can be divided at index i (where 0 <= i <= n) into two arrays (possibly empty) numsleft and numsright: numsleft has all the elements of nums between index 0 and i - 1 (inclusive), while numsright has all the elements of nums between index i and n - 1 (inclusive). If i == 0, numsleft is empty, while numsright has all the elements of nums. If i == n, numsleft has all the elements of nums, while numsright is empty. The division score of an index i is the sum of the number of 0's in numsleft and the number of 1's in numsright. Return all distinct indices that have the highest possible division score. You may return the answer in any order.   Please complete the following python code precisely: ```python class Solution: def maxScoreIndices(self, nums: List[int]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(nums = [0,0,1,0]) == [2,4]\n assert candidate(nums = [0,0,0]) == [3]\n assert candidate(nums = [1,1]) == [0]\n\n\ncheck(Solution().maxScoreIndices)"}
227
73
coding
Solve the programming task below in a Python markdown code block. You are given two arrays $a$ and $b$, both of length $n$. All elements of both arrays are from $0$ to $n-1$. You can reorder elements of the array $b$ (if you want, you may leave the order of elements as it is). After that, let array $c$ be the array of length $n$, the $i$-th element of this array is $c_i = (a_i + b_i) \% n$, where $x \% y$ is $x$ modulo $y$. Your task is to reorder elements of the array $b$ to obtain the lexicographically minimum possible array $c$. Array $x$ of length $n$ is lexicographically less than array $y$ of length $n$, if there exists such $i$ ($1 \le i \le n$), that $x_i < y_i$, and for any $j$ ($1 \le j < i$) $x_j = y_j$. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of elements in $a$, $b$ and $c$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i < n$), where $a_i$ is the $i$-th element of $a$. The third line of the input contains $n$ integers $b_1, b_2, \dots, b_n$ ($0 \le b_i < n$), where $b_i$ is the $i$-th element of $b$. -----Output----- Print the lexicographically minimum possible array $c$. Recall that your task is to reorder elements of the array $b$ and obtain the lexicographically minimum possible array $c$, where the $i$-th element of $c$ is $c_i = (a_i + b_i) \% n$. -----Examples----- Input 4 0 1 2 1 3 2 1 1 Output 1 0 0 2 Input 7 2 5 1 5 3 4 3 2 4 3 5 6 5 1 Output 0 0 0 1 0 2 4
{"inputs": ["1\n0\n0\n", "1\n0\n0\n", "2\n0 0\n1 1\n", "2\n1 1\n0 1\n", "2\n1 1\n0 1\n", "2\n0 0\n1 1\n", "2\n0 1\n0 1\n", "2\n1 0\n1 1\n"], "outputs": ["0 \n", "0 ", "1 1 \n", "0 1 \n", "0 1 ", "1 1 ", "0 0\n", "0 1\n"]}
533
138
coding
Solve the programming task below in a Python markdown code block. One day, Niwango-kun, an employee of Dwango Co., Ltd., found an integer sequence (a_1, ..., a_N) of length N. He is interested in properties of the sequence a. For a nonempty contiguous subsequence a_l, ..., a_r (1 \leq l \leq r \leq N) of the sequence a, its beauty is defined as a_l + ... + a_r. Niwango-kun wants to know the maximum possible value of the bitwise AND of the beauties of K nonempty contiguous subsequences among all N(N+1)/2 nonempty contiguous subsequences. (Subsequences may share elements.) Find the maximum possible value for him. Constraints * 2 \leq N \leq 1000 * 1 \leq a_i \leq 10^9 * 1 \leq K \leq N(N+1)/2 * All numbers given in input are integers Input Input is given from Standard Input in the following format: N K a_1 a_2 ... a_N Output Print the answer. Examples Input 4 2 2 5 2 5 Output 12 Input 8 4 9 1 8 2 7 5 6 4 Output 32
{"inputs": ["4 4\n2 5 2 5", "4 2\n2 5 3 5", "4 4\n2 5 2 1", "4 2\n4 5 2 5", "4 2\n2 8 2 5", "4 2\n2 5 2 5", "8 4\n9 1 8 2 7 5 6 6", "8 4\n9 1 8 2 7 8 6 6"], "outputs": ["6\n", "13\n", "2\n", "9\n", "12\n", "12", "32\n", "33\n"]}
300
162
coding
Solve the programming task below in a Python markdown code block. An array $[b_1, b_2, \ldots, b_m]$ is a palindrome, if $b_i = b_{m+1-i}$ for each $i$ from $1$ to $m$. Empty array is also a palindrome. An array is called kalindrome, if the following condition holds: It's possible to select some integer $x$ and delete some of the elements of the array equal to $x$, so that the remaining array (after gluing together the remaining parts) is a palindrome. Note that you don't have to delete all elements equal to $x$, and you don't have to delete at least one element equal to $x$. For example : $[1, 2, 1]$ is kalindrome because you can simply not delete a single element. $[3, 1, 2, 3, 1]$ is kalindrome because you can choose $x = 3$ and delete both elements equal to $3$, obtaining array $[1, 2, 1]$, which is a palindrome. $[1, 2, 3]$ is not kalindrome. You are given an array $[a_1, a_2, \ldots, a_n]$. Determine if $a$ is kalindrome or not. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the array. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) — elements of the array. It's guaranteed that the sum of $n$ over all test cases won't exceed $2 \cdot 10^5$. -----Output----- For each test case, print YES if $a$ is kalindrome and NO otherwise. You can print each letter in any case. -----Examples----- Input 4 1 1 2 1 2 3 1 2 3 5 1 4 4 1 4 Output YES YES NO YES -----Note----- In the first test case, array $[1]$ is already a palindrome, so it's a kalindrome as well. In the second test case, we can choose $x = 2$, delete the second element, and obtain array $[1]$, which is a palindrome. In the third test case, it's impossible to obtain a palindrome. In the fourth test case, you can choose $x = 4$ and delete the fifth element, obtaining $[1, 4, 4, 1]$. You also can choose $x = 1$, delete the first and the fourth elements, and obtain $[4, 4, 4]$.
{"inputs": ["4\n1\n1\n2\n1 2\n3\n1 2 3\n5\n1 4 4 1 4\n"], "outputs": ["YES\nYES\nNO\nYES\n"]}
659
50
coding
Solve the programming task below in a Python markdown code block. Monk visits the land of Islands. There are a total of N islands numbered from 1 to N. Some pairs of islands are connected to each other by Bidirectional bridges running over water. Monk hates to cross these bridges as they require a lot of efforts. He is standing at Island #1 and wants to reach the Island #N. Find the minimum the number of bridges that he shall have to cross, if he takes the optimal route. Input: First line contains T. T testcases follow. First line of each test case contains two space-separated integers N, M. Each of the next M lines contains two space-separated integers X and Y , denoting that there is a bridge between Island X and Island Y. Output: Print the answer to each test case in a new line. Constraints: 1 ≤ T ≤ 10 1 ≤ N ≤ 10^4 1 ≤ M ≤ 10^5 1 ≤ X, Y ≤ N SAMPLE INPUT 2 3 2 1 2 2 3 4 4 1 2 2 3 3 4 4 2 SAMPLE OUTPUT 2 2
{"inputs": ["2\n3 2\n1 2\n2 3\n4 5\n1 2\n2 3\n3 4\n4 2\n1 3"], "outputs": ["2\n2"]}
259
50
coding
Solve the programming task below in a Python markdown code block. Bhallaladeva was an evil king who ruled the kingdom of Maahishmati. He wanted to erect a 100ft golden statue of himself and he looted gold from several places for this. He even looted his own people, by using the following unfair strategy: There are N houses in Maahishmati, and the ith house has Ai gold plates. Each gold plate costs exactly 1 Nimbda, which is the unit of currency in the kingdom of Maahishmati. Bhallaladeva would choose an integer K, and loots all the houses in several steps. In each step: - He would choose a house i which hasn't been looted yet, pay the owner exactly Ai Nimbdas, and take away all the gold plates in that house (Hence, he also ends up looting this house). - He would now choose atmost K houses which haven't been looted yet and take away all the gold plates from these houses without paying a single Nimbda (Yes, he takes all of them for free). He repeats the above steps until all the N houses have been looted. Your task is to devise a strategy for Bhallaladeva to loot the houses in some order, so that the number of nimbdas he has to pay is minimium. You'll also be given multiple values of K (Q of them to be precise), and you need to find the minimum number of nimbdas for each of these values. -----Input----- The first line of input consists of a single integer N denoting the number of houses in Maahishmati. The second line of input consists of N space separated integers denoting A1, A2, ..., AN, where Ai denotes the number of gold plates in the ith house. The third line of input consists of a single integer Q denoting the number of values of K to follow. Each of the following Q lines consist of a single integer, where the value on the ith line denotes the value of K for the ith query. -----Output----- Output exactly Q integers on separate lines, where the output on the ith line denotes the answer for the ith value of K. -----Constraints----- - 1 ≤ N ≤ 105 - 1 ≤ Q ≤ 105 - 0 ≤ K ≤ N-1 - 1 ≤ Ai ≤ 104 -----Example----- Input: 4 3 2 1 4 2 0 2 Output: 10 3 -----Explanation----- For the first query, K = 0. Hence, Bhallaladeva cannot take gold plates from any of the houses for free. It will cost him 3 + 2 + 1 + 4 = 10 nimbdas. For the second query, K = 2. In the first step Bhallaladeva can pay 2 nimbdas for gold plates in house number 2, and take the gold in houses 1 and 4 for free (Note that house 1 has 3 gold plates and house 4 has 4 gold plates). Now, he has looted houses 1, 2 & 4. Now in the second step, he loots house 3, by paying 1 nimbda. Hence, the total cost = 1 + 2 = 3. Note that there might be multiple ways to achieve the minimum cost, and we have explained only one of them.
{"inputs": ["4\n3 2 1 4\n2\n0\n2"], "outputs": ["10\n3"]}
736
29
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin chinese, Russian and Vietnamese as well. You are given a sequence A_{1}, A_{2}, ..., A_{N} and Q queries. In each query, you are given two parameters L and R; you have to find the smallest integer X such that 0 ≤ X < 2^{31} and the value of (A_{L} xor X) + (A_{L+1} xor X) + ... + (A_{R} xor X) is maximum possible. Note: xor denotes the bitwise xor operation. ------ Input ------ The first line of the input contains two space-separated integers N and Q denoting the number of elements in A and the number of queries respectively. The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}. Each of the next Q lines contains two space-separated integers L and R describing one query. ------ Output ------ For each query, print a single line containing one integer — the minimum value of X. ------ Constraints ------ $1 ≤ N ≤ 10^{5}$ $1 ≤ Q ≤ 10^{5}$ $0 ≤ A_{i} < 2^{31} for each valid i$ ------ Subtasks ------ Subtask #1 (18 points): $1 ≤ N ≤ 10^{3}$ $1 ≤ Q ≤ 10^{3}$ $0 ≤ A_{i} < 2^{10} for each valid i$ Subtask #2 (82 points): original constraints ----- Sample Input 1 ------ 5 3 20 11 18 2 13 1 3 3 5 2 4 ----- Sample Output 1 ------ 2147483629 2147483645 2147483645
{"inputs": ["5 3\n20 11 18 2 13\n1 3\n3 5\n2 4"], "outputs": ["2147483629\n2147483645\n2147483645"]}
417
71
coding
Solve the programming task below in a Python markdown code block. Assume that you started to store items in progressively expanding square location, like this for the first 9 numbers: ``` 05 04 03 06 01 02 07 08 09 ``` And like this for the expanding to include up to the first 25 numbers: ``` 17 16 15 14 13 18 05 04 03 12 19 06 01 02 11 20 07 08 09 10 21 22 23 24 25 ``` You might easily notice that the first - and innermost - layer containes only one number (`01`), the second one - immediately around it - contains 8 numbers (number in the `02-09` range) and so on. Your task is to create a function that given a number `n` simply returns the number of layers required to store up to `n` (included). ```python layers(1) == 1 layers(5) == 2 layers(25) == 3 layers(30) == 4 layers(50) == 5 ``` **Fair warning:** you will always and only get positive integers, but be ready for bigger numbers in the tests! If you had fun with this, also try some follow up kata: [progressive spiral number branch](https://www.codewars.com/kata/progressive-spiral-number-branch/) and [progressive spiral number distance](https://www.codewars.com/kata/progressive-spiral-number-distance/). *[Base idea taken from [here](http://adventofcode.com/2017/day/3)]* Also feel free to reuse/extend the following starter code: ```python def layers(n): ```
{"functional": "_inputs = [[1], [5], [25], [30], [50]]\n_outputs = [[1], [2], [3], [4], [5]]\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(layers(*i), o[0])"}
430
180
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. A set of real numbers can be represented as the union of several disjoint intervals, where each interval is in the form [a, b). A real number x is in the set if one of its intervals [a, b) contains x (i.e. a <= x < b). You are given a sorted list of disjoint intervals intervals representing a set of real numbers as described above, where intervals[i] = [ai, bi] represents the interval [ai, bi). You are also given another interval toBeRemoved. Return the set of real numbers with the interval toBeRemoved removed from intervals. In other words, return the set of real numbers such that every x in the set is in intervals but not in toBeRemoved. Your answer should be a sorted list of disjoint intervals as described above.   Please complete the following python code precisely: ```python class Solution: def removeInterval(self, intervals: List[List[int]], toBeRemoved: List[int]) -> List[List[int]]: ```
{"functional": "def check(candidate):\n assert candidate(intervals = [[0,2],[3,4],[5,7]], toBeRemoved = [1,6]) == [[0,1],[6,7]]\n assert candidate(intervals = [[0,5]], toBeRemoved = [2,3]) == [[0,2],[3,5]]\n assert candidate(intervals = [[-5,-4],[-3,-2],[1,2],[3,5],[8,9]], toBeRemoved = [-1,4]) == [[-5,-4],[-3,-2],[4,5],[8,9]]\n\n\ncheck(Solution().removeInterval)"}
221
149
coding
Solve the programming task below in a Python markdown code block. Please note the non-standard memory limit. There are n problems numbered with integers from 1 to n. i-th problem has the complexity c_i = 2^i, tag tag_i and score s_i. After solving the problem i it's allowed to solve problem j if and only if IQ < |c_i - c_j| and tag_i ≠ tag_j. After solving it your IQ changes and becomes IQ = |c_i - c_j| and you gain |s_i - s_j| points. Any problem can be the first. You can solve problems in any order and as many times as you want. Initially your IQ = 0. Find the maximum number of points that can be earned. Input The first line contains a single integer t (1 ≤ t ≤ 100) — the number of test cases. The first line of each test case contains an integer n (1 ≤ n ≤ 5000) — the number of problems. The second line of each test case contains n integers tag_1, tag_2, …, tag_n (1 ≤ tag_i ≤ n) — tags of the problems. The third line of each test case contains n integers s_1, s_2, …, s_n (1 ≤ s_i ≤ 10^9) — scores of the problems. It's guaranteed that sum of n over all test cases does not exceed 5000. Output For each test case print a single integer — the maximum number of points that can be earned. Example Input 5 4 1 2 3 4 5 10 15 20 4 1 2 1 2 5 10 15 20 4 2 2 4 1 2 8 19 1 2 1 1 6 9 1 1 666 Output 35 30 42 0 0 Note In the first test case optimal sequence of solving problems is as follows: 1. 1 → 2, after that total score is 5 and IQ = 2 2. 2 → 3, after that total score is 10 and IQ = 4 3. 3 → 1, after that total score is 20 and IQ = 6 4. 1 → 4, after that total score is 35 and IQ = 14 In the second test case optimal sequence of solving problems is as follows: 1. 1 → 2, after that total score is 5 and IQ = 2 2. 2 → 3, after that total score is 10 and IQ = 4 3. 3 → 4, after that total score is 15 and IQ = 8 4. 4 → 1, after that total score is 35 and IQ = 14 In the third test case optimal sequence of solving problems is as follows: 1. 1 → 3, after that total score is 17 and IQ = 6 2. 3 → 4, after that total score is 35 and IQ = 8 3. 4 → 2, after that total score is 42 and IQ = 12
{"inputs": ["5\n4\n0 2 1 7\n5 12 29 8\n4\n1 1 0 2\n5 3 33 20\n4\n4 3 9 1\n0 9 5 2\n2\n1 1\n0 2\n1\n1\n666\n", "5\n4\n0 2 1 7\n5 12 29 8\n4\n2 1 0 2\n5 3 33 20\n4\n4 3 9 1\n0 9 5 2\n2\n1 1\n0 2\n1\n1\n666\n", "5\n4\n1 3 0 4\n5 9 15 20\n4\n1 2 2 2\n6 10 15 20\n4\n2 2 4 1\n2 8 19 2\n2\n2 1\n5 9\n1\n1\n25\n", "5\n4\n1 3 1 8\n5 7 15 27\n4\n0 2 2 2\n5 8 19 20\n4\n0 2 6 1\n1 8 19 2\n2\n1 1\n6 9\n1\n2\n900\n", "5\n4\n1 3 0 4\n5 9 15 20\n4\n1 2 2 2\n6 10 15 20\n4\n4 2 4 1\n2 8 19 2\n2\n2 1\n5 10\n1\n1\n1\n", "5\n4\n1 0 1 8\n8 7 28 5\n4\n1 1 0 2\n5 10 19 20\n4\n2 2 6 1\n0 0 30 2\n2\n1 1\n6 9\n1\n1\n441\n", "5\n4\n1 3 0 4\n5 9 15 20\n4\n1 2 2 3\n6 10 15 20\n4\n4 2 4 1\n2 8 19 2\n2\n2 1\n5 10\n1\n1\n1\n", "5\n4\n1 1 1 8\n8 7 28 5\n4\n1 1 0 2\n5 10 19 20\n4\n2 2 6 1\n0 0 30 2\n2\n1 1\n6 9\n1\n1\n441\n"], "outputs": ["56\n73\n24\n0\n0\n", "56\n62\n24\n0\n0\n", "35\n23\n40\n4\n0\n", "50\n29\n48\n0\n0\n", "35\n23\n40\n5\n0\n", "48\n38\n62\n0\n0\n", "35\n32\n40\n5\n0\n", "26\n38\n62\n0\n0\n"]}
741
772
coding
Solve the programming task below in a Python markdown code block. A binary string is called a self-destructing string if it can reduced to an empty string by performing the following operation some number of times (possibly zero): Choose a valid integer $i$ such that the $i$-th character of the current string is different from the $i+1$-th character, and remove these two characters from the string. You are given a binary string $s$. Your task is to convert $s$ to a self-destructing string. To do that, you may perform the following operation any number of times (possibly zero): Choose an integer $i$ ($1 \le i \le |s|-1$) such that the $i$-th character of $s$ is different from the $i+1$-th character, and invert one of these characters (inverting a character means changing '0' to '1' or '1' to '0', e.g. the string "01" can be changed to "00"). Find the smallest number of operations required to convert $s$ to a self-destructing string or determine that it is impossible. -----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 a single string $s$. -----Output----- For each test case, print a single line containing one integer ― the smallest required number of operations or $-1$ if it is impossible to convert $s$ to a self-destructing string. -----Constraints----- - $1 \le T \le 1,000$ - $1 \le |s| \le 10^6$ - $s$ contains only characters '0' and '1' - the sum of $|s|$ over all test cases does not exceed $10^6$ -----Example Input----- 3 001101 1101 110 -----Example Output----- 0 1 -1 -----Explanation----- Example case 1: We already have a self-destructing string because "001101" can be converted to "0101", to "01" and finally to an empty string. Example case 2: We can choose $i=3$ (since $s_3 \neq s_4$) and then invert $s_4$ to obtain "1100", which is a self-destructing string. Example case 3: It can be shown that "110" cannot be converted to a self-destructing string.
{"inputs": ["3\n001101\n1101\n110"], "outputs": ["0\n1\n-1"]}
564
33
coding
Solve the programming task below in a Python markdown code block. Two players A and B have a list of $n$ integers each. They both want to maximize the subtraction between their score and their opponent's score. In one turn, a player can either add to his score any element from his list (assuming his list is not empty), the element is removed from the list afterward. Or remove an element from his opponent's list (assuming his opponent's list is not empty). Note, that in case there are equal elements in the list only one of them will be affected in the operations above. For example, if there are elements $\{1, 2, 2, 3\}$ in a list and you decided to choose $2$ for the next turn, only a single instance of $2$ will be deleted (and added to the score, if necessary). The player A starts the game and the game stops when both lists are empty. Find the difference between A's score and B's score at the end of the game, if both of the players are playing optimally. Optimal play between two players means that both players choose the best possible strategy to achieve the best possible outcome for themselves. In this problem, it means that each player, each time makes a move, which maximizes the final difference between his score and his opponent's score, knowing that the opponent is doing the same. -----Input----- The first line of input contains an integer $n$ ($1 \le n \le 100\,000$) — the sizes of the list. The second line contains $n$ integers $a_i$ ($1 \le a_i \le 10^6$), describing the list of the player A, who starts the game. The third line contains $n$ integers $b_i$ ($1 \le b_i \le 10^6$), describing the list of the player B. -----Output----- Output the difference between A's score and B's score ($A-B$) if both of them are playing optimally. -----Examples----- Input 2 1 4 5 1 Output 0 Input 3 100 100 100 100 100 100 Output 0 Input 2 2 1 5 6 Output -3 -----Note----- In the first example, the game could have gone as follows: A removes $5$ from B's list. B removes $4$ from A's list. A takes his $1$. B takes his $1$. Hence, A's score is $1$, B's score is $1$ and difference is $0$. There is also another optimal way of playing: A removes $5$ from B's list. B removes $4$ from A's list. A removes $1$ from B's list. B removes $1$ from A's list. The difference in the scores is still $0$. In the second example, irrespective of the moves the players make, they will end up with the same number of numbers added to their score, so the difference will be $0$.
{"inputs": ["2\n1 4\n5 1\n", "2\n2 1\n5 6\n", "2\n2 1\n5 2\n", "2\n1 4\n5 1\n", "2\n2 1\n5 6\n", "3\n100 100 100\n100 100 100\n", "3\n100 100 100\n100 100 101\n", "3\n100 100 100\n100 100 100\n"], "outputs": ["0", "-3", "0\n", "0\n", "-3\n", "0", "0\n", "0\n"]}
678
179
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a string s containing only three types of characters: '(', ')' and '*', return true if s is valid. The following rules define a valid string: Any left parenthesis '(' must have a corresponding right parenthesis ')'. Any right parenthesis ')' must have a corresponding left parenthesis '('. Left parenthesis '(' must go before the corresponding right parenthesis ')'. '*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string "".   Please complete the following python code precisely: ```python class Solution: def checkValidString(self, s: str) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(s = \"()\") == True\n assert candidate(s = \"(*)\") == True\n assert candidate(s = \"(*))\") == True\n\n\ncheck(Solution().checkValidString)"}
138
54
coding
Solve the programming task below in a Python markdown code block. There are N positive integers A_1, A_2, ..., A_N. Takahashi can perform the following operation on these integers any number of times: * Choose 1 \leq i \leq N and multiply the value of A_i by -2. Notice that he multiplies it by minus two. He would like to make A_1 \leq A_2 \leq ... \leq A_N holds. Find the minimum number of operations required. If it is impossible, print `-1`. Constraints * 1 \leq N \leq 200000 * 1 \leq A_i \leq 10^9 Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output Print the answer. Examples Input 4 3 1 4 1 Output 3 Input 5 1 2 3 4 5 Output 0 Input 8 657312726 129662684 181537270 324043958 468214806 916875077 825989291 319670097 Output 7
{"inputs": ["4\n2 1 4 1", "4\n3 1 4 4", "4\n2 1 5 1", "4\n2 1 4 2", "4\n3 1 4 2", "4\n3 1 4 8", "4\n2 1 4 8", "4\n2 1 1 2"], "outputs": ["3\n", "1\n", "5\n", "3\n", "3\n", "1\n", "1\n", "1\n"]}
312
126
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given four integer arrays nums1, nums2, nums3, and nums4 all of length n, return the number of tuples (i, j, k, l) such that: 0 <= i, j, k, l < n nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0   Please complete the following python code precisely: ```python class Solution: def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]) == 2\n assert candidate(nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]) == 1\n\n\ncheck(Solution().fourSumCount)"}
141
90
coding
Solve the programming task below in a Python markdown code block. # Task **_Given_** a **_list of digits_**, *return the **_smallest number_** that could be formed from these digits, using the digits only once (ignore duplicates).* ___ # Notes: * Only **_positive integers_** *will be passed to the function (> 0 ), no negatives or zeros.* ___ # Input >> Output Examples ``` minValue ({1, 3, 1}) ==> return (13) ``` ## Explanation: **_(13)_** *is the minimum number could be formed from* **_{1, 3, 1}_** , *Without duplications* ___ ``` minValue({5, 7, 5, 9, 7}) ==> return (579) ``` ## Explanation: **_(579)_** *is the minimum number could be formed from* **_{5, 7, 5, 9, 7}_** , *Without duplications* ___ ``` minValue({1, 9, 3, 1, 7, 4, 6, 6, 7}) return ==> (134679) ``` ## Explanation: **_(134679)_** *is the minimum number could be formed from* **_{1, 9, 3, 1, 7, 4, 6, 6, 7}_** , *Without duplications* ___ ___ ## [Playing with Numbers Series](https://www.codewars.com/collections/playing-with-numbers) # [Playing With Lists/Arrays Series](https://www.codewars.com/collections/playing-with-lists-slash-arrays) # [Bizarre Sorting-katas](https://www.codewars.com/collections/bizarre-sorting-katas) # [For More Enjoyable Katas](http://www.codewars.com/users/MrZizoScream/authored) ___ ## ALL translations are welcomed ## Enjoy Learning !! # Zizou Also feel free to reuse/extend the following starter code: ```python def min_value(digits): ```
{"functional": "_inputs = [[[1, 3, 1]], [[4, 7, 5, 7]], [[4, 8, 1, 4]], [[5, 7, 9, 5, 7]], [[6, 7, 8, 7, 6, 6]], [[5, 6, 9, 9, 7, 6, 4]], [[1, 9, 1, 3, 7, 4, 6, 6, 7]], [[3, 6, 5, 5, 9, 8, 7, 6, 3, 5, 9]]]\n_outputs = [[13], [457], [148], [579], [678], [45679], [134679], [356789]]\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(min_value(*i), o[0])"}
472
342
coding
Solve the programming task below in a Python markdown code block. Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store — "PriceFixed". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $2$ rubles per item. For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!). Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100000$) — the number of products. Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) — the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product. The sum of all $a_i$ does not exceed $10^{14}$. -----Output----- Output the minimum sum that Lena needs to make all purchases. -----Examples----- Input 3 3 4 1 3 1 5 Output 8 Input 5 2 7 2 8 1 2 2 4 1 8 Output 12 -----Note----- In the first example, Lena can purchase the products in the following way: one item of product $3$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $1$ for $2$ rubles, one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased), one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased). In total, she spends $8$ rubles. It can be proved that it is impossible to spend less. In the second example Lena can purchase the products in the following way: one item of product $1$ for $2$ rubles, two items of product $2$ for $2$ rubles for each, one item of product $5$ for $2$ rubles, one item of product $3$ for $1$ ruble, two items of product $4$ for $1$ ruble for each, one item of product $1$ for $1$ ruble. In total, she spends $12$ rubles.
{"inputs": ["1\n8 2\n", "1\n8 2\n", "1\n8 4\n", "1\n8 1\n", "1\n6 1\n", "1\n4 1\n", "1\n16 1\n", "2\n7 7\n1 4\n"], "outputs": ["10\n", "10\n", "12\n", "9\n", "7\n", "5\n", "17\n", "14\n"]}
710
112
coding
Solve the programming task below in a Python markdown code block. Alice has recently started playing Chess. Her current rating is X. She noticed that when she wins a game, her rating increases by 8 points. Can you help Alice in finding out the minimum number of games she needs to win in order to make her rating greater than or equal to Y? ------ Input Format ------ - The first line of input will contain an integer T — the number of test cases. The description of T test cases follows. - The first line of each test case contains two integers X and Y, as described in the problem statement. ------ Output Format ------ For each test case, output the minimum number of games that Alice needs to win in order to make her rating greater than or equal to Y. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ X ≤ Y ≤ 10^{4}$ ----- Sample Input 1 ------ 4 10 10 10 17 10 18 10 19 ----- Sample Output 1 ------ 0 1 1 2 ----- explanation 1 ------ Test case $1$: Since Alice's current rating $X$ is already equal to her desired rating $Y$, she doesn't need to win any game. Test case $2$: Alice's current rating is $10$. After winning $1$ game, her rating will become $10+8=18$, which is greater than her desired rating of $17$. Thus, she has to win at least $1$ game. Test case $3$: Alice's current rating is $10$. After winning $1$ game, her rating will become $10+8=18$, which is equal to her desired rating of $18$. Thus, she has to win at least $1$ game. Test case $4$: Alice's current rating is $10$. After winning $1$ game, her rating will become $18$, which is less than her desired rating of $19$. She will need to win one more game in order to make her rating $26$, which is greater than $19$. Thus, she has to win at least $2$ games.
{"inputs": ["4\n10 10\n10 17\n10 18\n10 19\n"], "outputs": ["0\n1\n1\n2"]}
476
43
coding
Solve the programming task below in a Python markdown code block. In some game by Playrix it takes t minutes for an oven to bake k carrot cakes, all cakes are ready at the same moment t minutes after they started baking. Arkady needs at least n cakes to complete a task, but he currently don't have any. However, he has infinitely many ingredients and one oven. Moreover, Arkady can build one more similar oven to make the process faster, it would take d minutes to build the oven. While the new oven is being built, only old one can bake cakes, after the new oven is built, both ovens bake simultaneously. Arkady can't build more than one oven. Determine if it is reasonable to build the second oven, i.e. will it decrease the minimum time needed to get n cakes or not. If the time needed with the second oven is the same as with one oven, then it is unreasonable. -----Input----- The only line contains four integers n, t, k, d (1 ≤ n, t, k, d ≤ 1 000) — the number of cakes needed, the time needed for one oven to bake k cakes, the number of cakes baked at the same time, the time needed to build the second oven. -----Output----- If it is reasonable to build the second oven, print "YES". Otherwise print "NO". -----Examples----- Input 8 6 4 5 Output YES Input 8 6 4 6 Output NO Input 10 3 11 4 Output NO Input 4 2 1 4 Output YES -----Note----- In the first example it is possible to get 8 cakes in 12 minutes using one oven. The second oven can be built in 5 minutes, so after 6 minutes the first oven bakes 4 cakes, the second oven bakes 4 more ovens after 11 minutes. Thus, it is reasonable to build the second oven. In the second example it doesn't matter whether we build the second oven or not, thus it takes 12 minutes to bake 8 cakes in both cases. Thus, it is unreasonable to build the second oven. In the third example the first oven bakes 11 cakes in 3 minutes, that is more than needed 10. It is unreasonable to build the second oven, because its building takes more time that baking the needed number of cakes using the only oven.
{"inputs": ["8 6 4 5\n", "8 6 4 6\n", "4 2 1 4\n", "1 1 1 1\n", "3 1 1 1\n", "4 2 1 6\n", "5 5 4 4\n", "3 2 1 1\n"], "outputs": ["YES\n", "NO\n", "YES\n", "NO\n", "YES\n", "NO\n", "YES\n", "YES\n"]}
524
118
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a positive integer num. You may swap any two digits of num that have the same parity (i.e. both odd digits or both even digits). Return the largest possible value of num after any number of swaps.   Please complete the following python code precisely: ```python class Solution: def largestInteger(self, num: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(num = 1234) == 3412\n assert candidate(num = 65875) == 87655\n\n\ncheck(Solution().largestInteger)"}
93
58
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a string moves of length n consisting only of characters 'L', 'R', and '_'. The string represents your movement on a number line starting from the origin 0. In the ith move, you can choose one of the following directions: move to the left if moves[i] = 'L' or moves[i] = '_' move to the right if moves[i] = 'R' or moves[i] = '_' Return the distance from the origin of the furthest point you can get to after n moves.   Please complete the following python code precisely: ```python class Solution: def furthestDistanceFromOrigin(self, moves: str) -> int: ```
{"functional": "def check(candidate):\n assert candidate(moves = \"L_RL__R\") == 3\n assert candidate(moves = \"_R__LL_\") == 5\n assert candidate(moves = \"_______\") == 7\n\n\ncheck(Solution().furthestDistanceFromOrigin)"}
159
70
coding
Solve the programming task below in a Python markdown code block. Create a function `sierpinski` to generate an ASCII representation of a Sierpinski triangle of order **N**. Seperate each line with `\n`. You don't have to check the input value. The output should look like this: sierpinski(4) * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Also feel free to reuse/extend the following starter code: ```python def sierpinski(n): ```
{"functional": "_inputs = [[1], [2], [3]]\n_outputs = [[' * \\n* *'], [' * \\n * * \\n * * \\n* * * *'], [' * \\n * * \\n * * \\n * * * * \\n * * \\n * * * * \\n * * * * \\n* * * * * * * *']]\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(sierpinski(*i), o[0])"}
235
249
coding
Solve the programming task below in a Python markdown code block. Vika has an infinite sheet of squared paper. Initially all squares are white. She introduced a two-dimensional coordinate system on this sheet and drew n black horizontal and vertical segments parallel to the coordinate axes. All segments have width equal to 1 square, that means every segment occupy some set of neighbouring squares situated in one row or one column. Your task is to calculate the number of painted cells. If a cell was painted more than once, it should be calculated exactly once. Input The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of segments drawn by Vika. Each of the next n lines contains four integers x1, y1, x2 and y2 ( - 109 ≤ x1, y1, x2, y2 ≤ 109) — the coordinates of the endpoints of the segments drawn by Vika. It is guaranteed that all the segments are parallel to coordinate axes. Segments may touch, overlap and even completely coincide. Output Print the number of cells painted by Vika. If a cell was painted more than once, it should be calculated exactly once in the answer. Examples Input 3 0 1 2 1 1 4 1 2 0 3 2 3 Output 8 Input 4 -2 -1 2 -1 2 1 -2 1 -1 -2 -1 2 1 2 1 -2 Output 16 Note In the first sample Vika will paint squares (0, 1), (1, 1), (2, 1), (1, 2), (1, 3), (1, 4), (0, 3) and (2, 3).
{"inputs": ["1\n1 1 1 1\n", "1\n1 1 1 0\n", "1\n1 1 1 -1\n", "3\n0 1 4 1\n1 4 1 2\n0 3 2 3\n", "3\n0 1 2 1\n1 6 1 2\n0 3 3 3\n", "3\n0 1 2 1\n1 2 1 2\n0 3 3 3\n", "3\n0 1 2 1\n1 6 1 2\n0 3 2 3\n", "3\n1 1 2 1\n1 6 1 2\n0 3 3 3\n"], "outputs": ["1", "2\n", "3\n", "10\n", "11\n", "8\n", "10\n", "10\n"]}
395
217
coding
Solve the programming task below in a Python markdown code block. Imagine if there were no order of operations. Instead, you would do the problem from left to right. For example, the equation `$a +b *c /d$` would become `$(((a+b)*c)//d)$` (`Math.floor(((a+b)*c)/d)` in JS). Return `None`/`null` (depending on your language) if the equation is `""`. ### Task: Given an equation with a random amount of spaces greater than or equal to zero between each number and operation, return the result without order of operations. Note that if two numbers are spaces apart, act as if they were one number: `1 3` = `13`. However, if given something `% 0` or something `/ 0`, return `None/null`. More about order of operations: [here](https://en.wikipedia.org/wiki/Order_of_operations#:~:text=In%20the%20United%20States%2C%20the,Excuse%20My%20Dear%20Aunt%20Sally%22.) ### Key: - `^` represents `**` ```if:python - `/` represents `//` or `math.floor` because the result will always be an integer ``` ```if:javascript - `/` should always be rounded down(`Math.floor`) because the result will always be an integer ``` ### Operations allowed: `+, -, * , /, ^, %` ### Example: `no_order(2 + 3 - 4 * 1 ^ 3) returns 1` because: ``` 2 + 3 - 4 * 1 ^ 3 = 2 + 3 - 4 * 1 ^ 3 = 5 - 4 * 1 ^ 3 = 1 * 1 ^ 3 = 1 ^ 3 = 1 ``` Also feel free to reuse/extend the following starter code: ```python def no_order(equation): ```
{"functional": "_inputs = [['2 + 3- 4*1 ^ 3'], ['7 * 3 - 3/ 10 0'], ['1 20% 0 + 9'], ['6 9* 2+6 / 0']]\n_outputs = [[1], [0], [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(no_order(*i), o[0])"}
449
220
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Your friend is typing his name into a keyboard. Sometimes, when typing a character c, the key might get long pressed, and the character will be typed 1 or more times. You examine the typed characters of the keyboard. Return True if it is possible that it was your friends name, with some characters (possibly none) being long pressed.   Please complete the following python code precisely: ```python class Solution: def isLongPressedName(self, name: str, typed: str) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(name = \"alex\", typed = \"aaleex\") == True\n assert candidate(name = \"saeed\", typed = \"ssaaedd\") == False\n\n\ncheck(Solution().isLongPressedName)"}
123
59
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a binary string s, return true if the longest contiguous segment of 1's is strictly longer than the longest contiguous segment of 0's in s, or return false otherwise. For example, in s = "110100010" the longest continuous segment of 1s has length 2, and the longest continuous segment of 0s has length 3. Note that if there are no 0's, then the longest continuous segment of 0's is considered to have a length 0. The same applies if there is no 1's.   Please complete the following python code precisely: ```python class Solution: def checkZeroOnes(self, s: str) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(s = \"1101\") == True\n assert candidate(s = \"111000\") == False\n assert candidate(s = \"110100010\") == False\n\n\ncheck(Solution().checkZeroOnes)"}
169
70
coding
Solve the programming task below in a Python markdown code block. Create a function that finds the [integral](https://en.wikipedia.org/wiki/Integral) of the expression passed. In order to find the integral all you need to do is add one to the `exponent` (the second argument), and divide the `coefficient` (the first argument) by that new number. For example for `3x^2`, the integral would be `1x^3`: we added 1 to the exponent, and divided the coefficient by that new number). Notes: * The output should be a string. * The coefficient and exponent is always a positive integer. ## Examples ``` 3, 2 --> "1x^3" 12, 5 --> "2x^6" 20, 1 --> "10x^2" 40, 3 --> "10x^4" 90, 2 --> "30x^3" ``` Also feel free to reuse/extend the following starter code: ```python def integrate(coefficient, exponent): ```
{"functional": "_inputs = [[3, 2], [12, 5], [20, 1], [40, 3], [90, 2]]\n_outputs = [['1x^3'], ['2x^6'], ['10x^2'], ['10x^4'], ['30x^3']]\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(integrate(*i), o[0])"}
238
215
coding
Solve the programming task below in a Python markdown code block. Given a matrix (H × W) which contains only 1 and 0, find the area of the largest square matrix which only contains 0s. Constraints * 1 ≤ H, W ≤ 1,400 Input H W c1,1 c1,2 ... c1,W c2,1 c2,2 ... c2,W : cH,1 cH,2 ... cH,W In the first line, two integers H and W separated by a space character are given. In the following H lines, ci,j, elements of the H × W matrix, are given. Output Print the area (the number of 0s) of the largest square. Example Input 4 5 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 Output 4
{"inputs": ["4 5\n0 0 1 0 0\n1 0 0 0 1\n0 0 0 1 0\n0 0 0 1 0", "4 5\n0 0 1 0 0\n1 0 1 0 1\n0 0 0 1 0\n0 0 0 1 0", "3 5\n0 0 1 0 0\n1 0 0 0 1\n0 0 0 1 0\n0 0 0 1 0", "4 5\n0 0 1 0 0\n1 0 0 0 0\n0 0 0 1 0\n0 0 0 1 0", "4 5\n0 0 1 0 0\n1 0 0 0 1\n0 0 0 1 0\n0 -1 0 1 0", "4 5\n0 0 1 0 0\n1 0 0 0 0\n0 0 0 1 -1\n0 0 0 1 0", "4 5\n0 0 1 0 0\n1 0 0 0 1\n0 0 0 1 0\n0 -1 0 0 0", "4 5\n1 0 1 0 0\n1 0 0 0 0\n0 0 0 1 -1\n0 0 0 1 0"], "outputs": ["4\n", "4\n", "4\n", "4", "4\n", "4\n", "4\n", "4\n"]}
214
397
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given two arrays of strings that represent two inclusive events that happened on the same day, event1 and event2, where: event1 = [startTime1, endTime1] and event2 = [startTime2, endTime2]. Event times are valid 24 hours format in the form of HH:MM. A conflict happens when two events have some non-empty intersection (i.e., some moment is common to both events). Return true if there is a conflict between two events. Otherwise, return false.   Please complete the following python code precisely: ```python class Solution: def haveConflict(self, event1: List[str], event2: List[str]) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(event1 = [\"01:15\",\"02:00\"], event2 = [\"02:00\",\"03:00\"]) == True\n assert candidate(event1 = [\"01:00\",\"02:00\"], event2 = [\"01:20\",\"03:00\"]) == True\n assert candidate(event1 = [\"10:00\",\"11:00\"], event2 = [\"14:00\",\"15:00\"]) == False\n\n\ncheck(Solution().haveConflict)"}
160
145
coding
Solve the programming task below in a Python markdown code block. Integers in Python can be as big as the bytes in your machine's memory. There is no limit in size as there is: $2^{31}-1$ (c++ int) or $2^{63}-1$ (C++ long long int). As we know, the result of $a^{b}$ grows really fast with increasing $\boldsymbol{b}$. Let's do some calculations on very large integers. Task Read four numbers, $\boldsymbol{\alpha}$, $\boldsymbol{b}$, $\boldsymbol{c}$, and $\boldsymbol{d}$, and print the result of $a^b+c^d$. Input Format Integers $\boldsymbol{\alpha}$, $\boldsymbol{b}$, $\boldsymbol{c}$, and $\boldsymbol{d}$ are given on four separate lines, respectively. Constraints $1\leq a\leq1000$ $1\leq b\leq1000$ $1\leq c\leq1000$ $1\leq d\leq1000$ Output Format Print the result of $a^b+c^d$ on one line. Sample Input 9 29 7 27 Sample Output 4710194409608608369201743232 Note: This result is bigger than $2^{63}-1$. Hence, it won't fit in the long long int of C++ or a 64-bit integer.
{"inputs": ["9\n29\n7\n27\n"], "outputs": ["4710194409608608369201743232 \n"]}
361
51
coding
Solve the programming task below in a Python markdown code block. Ash is on his way to becoming the Pokemon Master. His pokemon can perform the following moves: - Tackle - Deal damage worth $X$ points - Grow - Increase damage by $Y$ points i.e. $X$ = $X$ + $Y$ But, it can only perform Grow first (0 or more times) and then tackle (0 or more) times after which it cannot perform Grow again. That is, it cannot perform the Grow operation once it has performed the tackle operation. A pokemon can be caught only if it’s health is exactly 1. A wild pokemon has appeared and has health worth $H$ points. Find the minimum number of moves required to catch it or say that it is not possible. -----Input:----- - The first line of the input consists of a single integer $T$ denoting the number of test cases. - Each test case consists of 3 space-separated integers $H$, $X$ and $Y$. -----Output:----- - For each test case, print a single line containing one integer - the minimum number of moves required to catch the pokemon if it is possible to catch it else print -1. -----Constraints----- - 1 <= $T$ <= 103 - 1 <= $X$, $Y$ < $H$ <= 109 -----Subtasks----- Subtask #1 (30 points): - 1 <= $X$, $Y$ < $H$ <= 1000 Subtask #2 (70 points): - Original Constraints -----Sample Input:----- 2 101 10 10 11 3 3 -----Sample Output:----- 6 -1 -----EXPLANATION:----- - Example Case 1: Ash can make use of Grow once. So $X$ = 10 + 10 = 20 Then he can do Tackle 5 times to decrease $H$ to 1. OR Ash can make use of Grow 4 times. So $X$ = 10 + 4*10 = 50 Then he can do Tackle 2 times to decrease $H$ to 1. Hence, a total of 6 moves are required either way which is minimum. - Example Case 2: No matter how many times Ash uses Grow or Tackle, pokemon can never be caught.
{"inputs": ["2\n101 10 10\n11 3 3"], "outputs": ["6\n-1"]}
525
32
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. There are some prizes on the X-axis. You are given an integer array prizePositions that is sorted in non-decreasing order, where prizePositions[i] is the position of the ith prize. There could be different prizes at the same position on the line. You are also given an integer k. You are allowed to select two segments with integer endpoints. The length of each segment must be k. You will collect all prizes whose position falls within at least one of the two selected segments (including the endpoints of the segments). The two selected segments may intersect. For example if k = 2, you can choose segments [1, 3] and [2, 4], and you will win any prize i that satisfies 1 <= prizePositions[i] <= 3 or 2 <= prizePositions[i] <= 4. Return the maximum number of prizes you can win if you choose the two segments optimally.   Please complete the following python code precisely: ```python class Solution: def maximizeWin(self, prizePositions: List[int], k: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(prizePositions = [1,1,2,2,3,3,5], k = 2) == 7\n assert candidate(prizePositions = [1,2,3,4], k = 0) == 2\n\n\ncheck(Solution().maximizeWin)"}
239
76
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Mandarin], [Bengali], [Russian], and [Vietnamese] as well. You are given an integer N. Consider the sequence containing the integers 1, 2, \ldots, N in increasing order (each exactly once). Find the maximum length of its contiguous subsequence with an even sum. ------ Input Format ------ - 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 a single integer N. ------ Output Format ------ For each test case, print a single line containing one integer --- the maximum length of a contiguous subsequence with even sum. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $2 ≤ N ≤ 10^{4}$ ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 3 3 4 5 ----- Sample Output 1 ------ 3 4 4 ----- explanation 1 ------ Example case 1: The optimal choice is to choose the entire sequence, since the sum of all its elements is $1 + 2 + 3 = 6$, which is even. Example case 3: One of the optimal choices is to choose the subsequence $[1, 2, 3, 4]$, which has an even sum.
{"inputs": ["3\n3\n4\n5"], "outputs": ["3\n4\n4"]}
318
22
coding
Solve the programming task below in a Python markdown code block. problem There is a mysterious device $ M $, and if you put Tanuki and Fox in this device, one animal will come out from the device (hereinafter, Tanuki will be $ T $ and Fox will be $ F $). $ M (x, y) $ represents an animal that came out by putting animals in the device $ M $ in the order of $ x, y $. As a result of various trials, the following was found. $ M (T, T) = T $ $ M (T, F) = F $ $ M (F, T) = T $ $ M (F, F) = T $ You put the animals in a row $ P_1, P_2, ..., P_N $ into the device as follows. $ M (.... M (M (P_1, P_2), P_3) ...., P_N) $ Answer the last animal that appears. output Output the last animal character $ T $ or $ F $, and also a newline at the end. Example Input 3 F T T Output T
{"inputs": ["3\nF S T", "3\nF R T", "3\nG S T", "3\nG R T", "3\nH R T", "3\nF U T", "3\nE S T", "3\nE T T"], "outputs": ["T\n", "T\n", "T\n", "T\n", "T\n", "T\n", "T\n", "T\n"]}
245
94
coding
Solve the programming task below in a Python markdown code block. Given are positive integers N, M, Q, and Q quadruples of integers ( a_i , b_i , c_i , d_i ). Consider a sequence A satisfying the following conditions: - A is a sequence of N positive integers. - 1 \leq A_1 \leq A_2 \le \cdots \leq A_N \leq M. Let us define a score of this sequence as follows: - The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i. (If there is no such i, the score is 0.) Find the maximum possible score of A. -----Constraints----- - All values in input are integers. - 2 ≤ N ≤ 10 - 1 \leq M \leq 10 - 1 \leq Q \leq 50 - 1 \leq a_i < b_i \leq N ( i = 1, 2, ..., Q ) - 0 \leq c_i \leq M - 1 ( i = 1, 2, ..., Q ) - (a_i, b_i, c_i) \neq (a_j, b_j, c_j) (where i \neq j) - 1 \leq d_i \leq 10^5 ( i = 1, 2, ..., Q ) -----Input----- Input is given from Standard Input in the following format: N M Q a_1 b_1 c_1 d_1 : a_Q b_Q c_Q d_Q -----Output----- Print the maximum possible score of A. -----Sample Input----- 3 4 3 1 3 3 100 1 2 2 10 2 3 2 10 -----Sample Output----- 110 When A = \{1, 3, 4\}, its score is 110. Under these conditions, no sequence has a score greater than 110, so the answer is 110.
{"inputs": ["10 10 1\n1 10 9 1", "10 10 1\n1 10 9 1\n", "10 10 1\n1 10 12 1", "3 4 3\n2 3 3 100\n1 2 2 10\n2 3 2 10", "3 4 3\n1 1 3 100\n1 2 2 10\n2 3 2 10", "3 4 2\n2 3 1 100\n1 2 2 10\n2 3 2 14", "3 4 2\n2 3 3 101\n1 2 2 16\n2 3 2 14", "3 4 3\n1 3 3 100\n1 2 2 10\n2 3 2 10"], "outputs": ["1", "1\n", "0\n", "100\n", "10\n", "110\n", "101\n", "110"]}
462
276
coding
Solve the programming task below in a Python markdown code block. In this kata, your task is to create a function that takes a single list as an argument and returns a flattened list. The input list will have a maximum of one level of nesting (list(s) inside of a list). ```python # no nesting [1, 2, 3] # one level of nesting [1, [2, 3]] ``` --- # Examples ```python >>> flatten_me(['!', '?']) ['!', '?'] >>> flatten_me([1, [2, 3], 4]) [1, 2, 3, 4] >>> flatten_me([['a', 'b'], 'c', ['d']]) ['a', 'b', 'c', 'd'] >>> flatten_me([[True, False], ['!'], ['?'], [71, '@']]) [True, False, '!', '?', 71, '@'] ``` Good luck! Also feel free to reuse/extend the following starter code: ```python def flatten_me(lst): ```
{"functional": "_inputs = [[[1, [2, 3], 4]], [[['a', 'b'], 'c', ['d']]], [['!', '?']]]\n_outputs = [[[1, 2, 3, 4]], [['a', 'b', 'c', 'd']], [['!', '?']]]\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(flatten_me(*i), o[0])"}
228
209
coding
Solve the programming task below in a Python markdown code block. In the city of Ultima Thule job applicants are often offered an IQ test. The test is as follows: the person gets a piece of squared paper with a 4 × 4 square painted on it. Some of the square's cells are painted black and others are painted white. Your task is to repaint at most one cell the other color so that the picture has a 2 × 2 square, completely consisting of cells of the same color. If the initial picture already has such a square, the person should just say so and the test will be completed. Your task is to write a program that determines whether it is possible to pass the test. You cannot pass the test if either repainting any cell or no action doesn't result in a 2 × 2 square, consisting of cells of the same color. -----Input----- Four lines contain four characters each: the j-th character of the i-th line equals "." if the cell in the i-th row and the j-th column of the square is painted white, and "#", if the cell is black. -----Output----- Print "YES" (without the quotes), if the test can be passed and "NO" (without the quotes) otherwise. -----Examples----- Input #### .#.. #### .... Output YES Input #### .... #### .... Output NO -----Note----- In the first test sample it is enough to repaint the first cell in the second row. After such repainting the required 2 × 2 square is on the intersection of the 1-st and 2-nd row with the 1-st and 2-nd column.
{"inputs": ["####\n.#..\n####\n....\n", "####\n....\n####\n....\n", "....\n....\n....\n....\n", "###.\n...#\n###.\n...#\n", ".##.\n#..#\n.##.\n#..#\n", ".#.#\n#.#.\n.#.#\n#.#.\n", "##..\n..##\n##..\n..##\n", "#.#.\n#.#.\n.#.#\n.#.#\n"], "outputs": ["YES\n", "NO\n", "YES\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n"]}
345
162
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an m x n grid. Each cell of the grid has a sign pointing to the next cell you should visit if you are currently in this cell. The sign of grid[i][j] can be: 1 which means go to the cell to the right. (i.e go from grid[i][j] to grid[i][j + 1]) 2 which means go to the cell to the left. (i.e go from grid[i][j] to grid[i][j - 1]) 3 which means go to the lower cell. (i.e go from grid[i][j] to grid[i + 1][j]) 4 which means go to the upper cell. (i.e go from grid[i][j] to grid[i - 1][j]) Notice that there could be some signs on the cells of the grid that point outside the grid. You will initially start at the upper left cell (0, 0). A valid path in the grid is a path that starts from the upper left cell (0, 0) and ends at the bottom-right cell (m - 1, n - 1) following the signs on the grid. The valid path does not have to be the shortest. You can modify the sign on a cell with cost = 1. You can modify the sign on a cell one time only. Return the minimum cost to make the grid have at least one valid path.   Please complete the following python code precisely: ```python class Solution: def minCost(self, grid: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(grid = [[1,1,1,1],[2,2,2,2],[1,1,1,1],[2,2,2,2]]) == 3\n assert candidate(grid = [[1,1,3],[3,2,2],[1,1,4]]) == 0\n assert candidate(grid = [[1,2],[4,3]]) == 1\n assert candidate(grid = [[2,2,2],[2,2,2]]) == 3\n assert candidate(grid = [[4]]) == 0\n\n\ncheck(Solution().minCost)"}
340
141
coding
Solve the programming task below in a Python markdown code block. Given is a string S consisting of `0` and `1`. Find the number of strings, modulo 998244353, that can result from applying the following operation on S zero or more times: * Remove the two characters at the beginning of S, erase one of them, and reinsert the other somewhere in S. This operation can be applied only when S has two or more characters. Constraints * 1 \leq |S| \leq 300 * S consists of `0` and `1`. Input Input is given from Standard Input in the following format: S Output Print the number of strings, modulo 998244353, that can result from applying the operation on S zero or more times. Examples Input 0001 Output 8 Input 110001 Output 24 Input 11101111011111000000000110000001111100011111000000001111111110000000111111111 Output 697354558
{"inputs": ["0000", "0010", "0110", "0111", "0011", "0001", "110011", "110010"], "outputs": ["4\n", "8\n", "11\n", "10\n", "9\n", "8", "24\n", "25\n"]}
296
93
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. Sebi likes solving Rubik's cube a lot. He spends a lot of time in getting expertize in solving not only the 3 * 3 * 3 cube, but also the cubes of higher dimensions like 4 * 4 * 4, 5 * 5 * 5 and so on. Chefland has a very famous toy shop which sells Rubik's cubes. This shop has interesting rules. Each day it sells cubes of a fixed dimension. Sebi has to buy new cubes daily primarily due to two reasons, one he handles the cube very roughly and the other that he solves the cube so many times during the day. Today the shop is selling K * K * K size Rubik's cubes. In the morning, Sebi bought a cube from the shop. He had just started playing with it, suddenly his cute little sisters asked him to give them C units of the cube. Sebi's did not want to disappoint his sisters, so he immediately disassembled the cube into K * K * K units and gave C of those to his sisters. Now Sebi wants to solve the Rubik's cube again, so he thought of going to market and buy some cubes so that he can create a Rubik's cube from those. The newly created cube can be of any dimension. For achieving that, he can disassemble and reassemble the cubes in the way he wants, but he does not want to waste any units of the cubes. Can you please tell whether Sebi will be able to build such a Rubik's cube or not? ------ Input ------ The first line of the input contains an integer T denoting the number of the test cases. Each test case contains two space separated integers K, C as defined in the statement. ------ Output ------ For each test case, output a single line containing "YES" or "NO" (without quotes) corresponding to the situation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $0 ≤ C ≤ K^{3}$ $2 ≤ K ≤ 100$ ----- Sample Input 1 ------ 2 2 5 2 2 ----- Sample Output 1 ------ YES NO ----- explanation 1 ------ Example case 1. In this case Sebi bought a Rubik's of size 2 * 2 * 2. His sisters wanted 5 units of cube. He disassembled the cube into 8 units immediately and gave 5 of them to his sisters. Now he is left with 3 units. He buys 3 cubes at the shop. Now, after disassembling, he has 3 + 3 * 8 = 27 cubes. By making use of these 27 cubes, he can make a Rubik's cube of size 3 * 3 * 3.
{"inputs": ["2\n2 5\n2 2"], "outputs": ["YES\nNO"]}
615
22
coding
Solve the programming task below in a Python markdown code block. You are given a string $s$, consisting only of characters '0' or '1'. Let $|s|$ be the length of $s$. You are asked to choose some integer $k$ ($k > 0$) and find a sequence $a$ of length $k$ such that: $1 \le a_1 < a_2 < \dots < a_k \le |s|$; $a_{i-1} + 1 < a_i$ for all $i$ from $2$ to $k$. The characters at positions $a_1, a_2, \dots, a_k$ are removed, the remaining characters are concatenated without changing the order. So, in other words, the positions in the sequence $a$ should not be adjacent. Let the resulting string be $s'$. $s'$ is called sorted if for all $i$ from $2$ to $|s'|$ $s'_{i-1} \le s'_i$. Does there exist such a sequence $a$ that the resulting string $s'$ is sorted? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases. Then the descriptions of $t$ testcases follow. The only line of each testcase contains a string $s$ ($2 \le |s| \le 100$). Each character is either '0' or '1'. -----Output----- For each testcase print "YES" if there exists a sequence $a$ such that removing the characters at positions $a_1, a_2, \dots, a_k$ and concatenating the parts without changing the order produces a sorted string. Otherwise, print "NO". You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer). -----Examples----- Input 5 10101011011 0000 11111 110 1100 Output YES YES YES YES NO -----Note----- In the first testcase you can choose a sequence $a=[1,3,6,9]$. Removing the underlined letters from "10101011011" will produce a string "0011111", which is sorted. In the second and the third testcases the sequences are already sorted. In the fourth testcase you can choose a sequence $a=[3]$. $s'=$ "11", which is sorted. In the fifth testcase there is no way to choose a sequence $a$ such that $s'$ is sorted.
{"inputs": ["9\n11\n11\n11\n11\n11\n11\n11\n11\n11\n", "9\n11\n11\n11\n11\n11\n11\n11\n11\n11\n", "5\n10101011011\n0000\n11111\n110\n1100\n", "5\n10101011011\n1000\n11111\n110\n1100\n", "5\n10101011011\n1000\n11111\n110\n0100\n", "5\n10101011001\n1000\n11111\n110\n0101\n", "5\n10101011001\n1001\n11111\n110\n1100\n", "5\n10101011011\n0011\n11000\n101\n1100\n"], "outputs": ["YES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\n", "YES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\n", "YES\nYES\nYES\nYES\nNO\n", "YES\nYES\nYES\nYES\nNO\n", "YES\nYES\nYES\nYES\nYES\n", "NO\nYES\nYES\nYES\nYES\n", "NO\nYES\nYES\nYES\nNO\n", "YES\nYES\nNO\nYES\nNO\n"]}
595
396
coding
Solve the programming task below in a Python markdown code block. Gerald got a very curious hexagon for his birthday. The boy found out that all the angles of the hexagon are equal to $120^{\circ}$. Then he measured the length of its sides, and found that each of them is equal to an integer number of centimeters. There the properties of the hexagon ended and Gerald decided to draw on it. He painted a few lines, parallel to the sides of the hexagon. The lines split the hexagon into regular triangles with sides of 1 centimeter. Now Gerald wonders how many triangles he has got. But there were so many of them that Gerald lost the track of his counting. Help the boy count the triangles. -----Input----- The first and the single line of the input contains 6 space-separated integers a_1, a_2, a_3, a_4, a_5 and a_6 (1 ≤ a_{i} ≤ 1000) — the lengths of the sides of the hexagons in centimeters in the clockwise order. It is guaranteed that the hexagon with the indicated properties and the exactly such sides exists. -----Output----- Print a single integer — the number of triangles with the sides of one 1 centimeter, into which the hexagon is split. -----Examples----- Input 1 1 1 1 1 1 Output 6 Input 1 2 1 2 1 2 Output 13 -----Note----- This is what Gerald's hexagon looks like in the first sample: $\theta$ And that's what it looks like in the second sample: $A$
{"inputs": ["1 1 1 1 1 1\n", "1 2 1 2 1 2\n", "2 4 5 3 3 6\n", "7 5 4 8 4 5\n", "3 2 1 4 1 2\n", "7 1 7 3 5 3\n", "9 2 9 3 8 3\n", "1 6 1 5 2 5\n"], "outputs": ["6\n", "13\n", "83\n", "175\n", "25\n", "102\n", "174\n", "58\n"]}
352
160
coding
Solve the programming task below in a Python markdown code block. Alice and Bob love playing one-dimensional battle ships. They play on the field in the form of a line consisting of n square cells (that is, on a 1 × n table). At the beginning of the game Alice puts k ships on the field without telling their positions to Bob. Each ship looks as a 1 × a rectangle (that is, it occupies a sequence of a consecutive squares of the field). The ships cannot intersect and even touch each other. After that Bob makes a sequence of "shots". He names cells of the field and Alice either says that the cell is empty ("miss"), or that the cell belongs to some ship ("hit"). But here's the problem! Alice like to cheat. May be that is why she responds to each Bob's move with a "miss". Help Bob catch Alice cheating — find Bob's first move, such that after it you can be sure that Alice cheated. -----Input----- The first line of the input contains three integers: n, k and a (1 ≤ n, k, a ≤ 2·10^5) — the size of the field, the number of the ships and the size of each ship. It is guaranteed that the n, k and a are such that you can put k ships of size a on the field, so that no two ships intersect or touch each other. The second line contains integer m (1 ≤ m ≤ n) — the number of Bob's moves. The third line contains m distinct integers x_1, x_2, ..., x_{m}, where x_{i} is the number of the cell where Bob made the i-th shot. The cells are numbered from left to right from 1 to n. -----Output----- Print a single integer — the number of such Bob's first move, after which you can be sure that Alice lied. Bob's moves are numbered from 1 to m in the order the were made. If the sought move doesn't exist, then print "-1". -----Examples----- Input 11 3 3 5 4 8 6 1 11 Output 3 Input 5 1 3 2 1 5 Output -1 Input 5 1 3 1 3 Output 1
{"inputs": ["5 1 3\n1\n3\n", "1 1 1\n1\n1\n", "1 1 1\n1\n1\n", "2 1 1\n1\n1\n", "2 1 2\n1\n1\n", "3 1 2\n1\n1\n", "5 1 3\n1\n2\n", "5 1 2\n1\n1\n"], "outputs": ["1\n", "1\n", "1\n", "-1\n", "1\n", "-1\n", "-1\n", "-1\n"]}
484
134
coding
Solve the programming task below in a Python markdown code block. A magic number is a number formed by concatenation of numbers 1, 14 and 144. We can use each of these numbers any number of times. Therefore 14144, 141414 and 1411 are magic numbers but 1444, 514 and 414 are not. You're given a number. Determine if it is a magic number or not. -----Input----- The first line of input contains an integer n, (1 ≤ n ≤ 10^9). This number doesn't contain leading zeros. -----Output----- Print "YES" if n is a magic number or print "NO" if it's not. -----Examples----- Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO
{"inputs": ["1\n", "9\n", "4\n", "1\n", "4\n", "9\n", "8\n", "2\n"], "outputs": ["YES\n", "NO\n", "NO\n", "YES\n", "NO\n", "NO\n", "NO\n", "NO\n"]}
202
70
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a 0-indexed integer array nums of length n. The number of ways to partition nums is the number of pivot indices that satisfy both conditions: 1 <= pivot < n nums[0] + nums[1] + ... + nums[pivot - 1] == nums[pivot] + nums[pivot + 1] + ... + nums[n - 1] You are also given an integer k. You can choose to change the value of one element of nums to k, or to leave the array unchanged. Return the maximum possible number of ways to partition nums to satisfy both conditions after changing at most one element.   Please complete the following python code precisely: ```python class Solution: def waysToPartition(self, nums: List[int], k: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [2,-1,2], k = 3) == 1\n assert candidate(nums = [0,0,0], k = 1) == 2\n assert candidate(nums = [22,4,-25,-20,-15,15,-16,7,19,-10,0,-13,-14], k = -33) == 4\n\n\ncheck(Solution().waysToPartition)"}
184
114
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an m x n binary matrix matrix. You can choose any number of columns in the matrix and flip every cell in that column (i.e., Change the value of the cell from 0 to 1 or vice versa). Return the maximum number of rows that have all values equal after some number of flips.   Please complete the following python code precisely: ```python class Solution: def maxEqualRowsAfterFlips(self, matrix: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(matrix = [[0,1],[1,1]]) == 1\n assert candidate(matrix = [[0,1],[1,0]]) == 2\n assert candidate(matrix = [[0,0,0],[0,0,1],[1,1,0]]) == 2\n\n\ncheck(Solution().maxEqualRowsAfterFlips)"}
118
87
coding
Solve the programming task below in a Python markdown code block. # MOD 256 without the MOD operator The MOD-operator % (aka mod/modulus/remainder): ``` Returns the remainder of a division operation. The sign of the result is the same as the sign of the first operand. (Different behavior in Python!) ``` The short unbelievable mad story for this kata: I wrote a program and needed the remainder of the division by 256. And then it happened: The "5"/"%"-Key did not react. It must be broken! So I needed a way to: ``` Calculate the remainder of the division by 256 without the %-operator. ``` Also here some examples: ``` Input 254 -> Result 254 Input 256 -> Result 0 Input 258 -> Result 2 Input -258 -> Result -2 (in Python: Result: 254!) ``` It is always expected the behavior of the MOD-Operator of the language! The input number will always between -10000 and 10000. For some languages the %-operator will be blocked. If it is not blocked and you know how to block it, tell me and I will include it. For all, who say, this would be a duplicate: No, this is no duplicate! There are two katas, in that you have to write a general method for MOD without %. But this kata is only for MOD 256. And so you can create also other specialized solutions. ;-) Of course you can use the digit "5" in your solution. :-) I'm very curious for your solutions and the way you solve it. I found several interesting "funny" ways. Have fun coding it and please don't forget to vote and rank this kata! :-) I have also created other katas. Take a look if you enjoyed this kata! Also feel free to reuse/extend the following starter code: ```python def mod256_without_mod(number): ```
{"functional": "_inputs = [[254], [256], [258], [-254], [-256], [-258]]\n_outputs = [[254], [0], [2], [2], [0], [254]]\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(mod256_without_mod(*i), o[0])"}
437
204
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. An integer has monotone increasing digits if and only if each pair of adjacent digits x and y satisfy x <= y. Given an integer n, return the largest number that is less than or equal to n with monotone increasing digits.   Please complete the following python code precisely: ```python class Solution: def monotoneIncreasingDigits(self, n: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(n = 10) == 9\n assert candidate(n = 1234) == 1234\n assert candidate(n = 332) == 299\n\n\ncheck(Solution().monotoneIncreasingDigits)"}
96
69
coding
Solve the programming task below in a Python markdown code block. One player came to a casino and found a slot machine where everything depends only on how he plays. The rules follow. A positive integer a is initially on the screen. The player can put a coin into the machine and then add 1 to or subtract 1 from any two adjacent digits. All digits must remain from 0 to 9 after this operation, and the leading digit must not equal zero. In other words, it is forbidden to add 1 to 9, to subtract 1 from 0 and to subtract 1 from the leading 1. Once the number on the screen becomes equal to b, the player wins the jackpot. a and b have the same number of digits. Help the player to determine the minimal number of coins he needs to spend in order to win the jackpot and tell how to play. Input The first line contains a single integer n (2 ≤ n ≤ 10^5) standing for the length of numbers a and b. The next two lines contain numbers a and b, each one on a separate line (10^{n-1} ≤ a, b < 10^n). Output If it is impossible to win the jackpot, print a single integer -1. Otherwise, the first line must contain the minimal possible number c of coins the player has to spend. min(c, 10^5) lines should follow, i-th of them containing two integers d_i and s_i (1≤ d_i≤ n - 1, s_i = ± 1) denoting that on the i-th step the player should add s_i to the d_i-th and (d_i + 1)-st digits from the left (e. g. d_i = 1 means that two leading digits change while d_i = n - 1 means that there are two trailing digits which change). Please notice that the answer may be very big and in case c > 10^5 you should print only the first 10^5 moves. Your answer is considered correct if it is possible to finish your printed moves to win the jackpot in the minimal possible number of coins. In particular, if there are multiple ways to do this, you can output any of them. Examples Input 3 223 322 Output 2 1 1 2 -1 Input 2 20 42 Output 2 1 1 1 1 Input 2 35 44 Output -1 Note In the first example, we can make a +1 operation on the two first digits, transforming number 223 into 333, and then make a -1 operation on the last two digits, transforming 333 into 322. It's also possible to do these operations in reverse order, which makes another correct answer. In the last example, one can show that it's impossible to transform 35 into 44.
{"inputs": ["2\n28\n94\n", "2\n99\n11\n", "2\n37\n97\n", "2\n85\n96\n", "2\n64\n97\n", "2\n37\n94\n", "2\n19\n97\n", "2\n54\n96\n"], "outputs": ["-1\n", "8\n1 -1\n1 -1\n1 -1\n1 -1\n1 -1\n1 -1\n1 -1\n1 -1\n", "-1\n", "1\n1 1\n", "3\n1 1\n1 1\n1 1\n", "-1\n", "-1\n", "-1\n"]}
629
167
coding
Solve the programming task below in a Python markdown code block. The chef is trying to solve some pattern problems, Chef wants your help to code it. Chef has one number K to form a new pattern. Help the chef to code this pattern problem. -----Input:----- - First-line will contain $T$, the number of test cases. Then the test cases follow. - Each test case contains a single line of input, one integer $K$. -----Output:----- For each test case, output as the pattern. -----Constraints----- - $1 \leq T \leq 100$ - $1 \leq K \leq 100$ -----Sample Input:----- 2 2 4 -----Sample Output:----- 2 21 210 21 2 4 43 432 4321 43210 4321 432 43 4 -----EXPLANATION:----- No need, else pattern can be decode easily.
{"inputs": ["2\n2\n4"], "outputs": ["2\n21\n210\n21\n2\n4\n43\n432\n4321\n43210\n4321\n432\n43\n4"]}
220
62