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. Vanya and his friend Vova play a computer game where they need to destroy n monsters to pass a level. Vanya's character performs attack with frequency x hits per second and Vova's character performs attack with frequency y hits per second. Each character spends fixed time to raise a weapon and then he hits (the time to raise the weapon is 1 / x seconds for the first character and 1 / y seconds for the second one). The i-th monster dies after he receives a_{i} hits. Vanya and Vova wonder who makes the last hit on each monster. If Vanya and Vova make the last hit at the same time, we assume that both of them have made the last hit. -----Input----- The first line contains three integers n,x,y (1 ≤ n ≤ 10^5, 1 ≤ x, y ≤ 10^6) — the number of monsters, the frequency of Vanya's and Vova's attack, correspondingly. Next n lines contain integers a_{i} (1 ≤ a_{i} ≤ 10^9) — the number of hits needed do destroy the i-th monster. -----Output----- Print n lines. In the i-th line print word "Vanya", if the last hit on the i-th monster was performed by Vanya, "Vova", if Vova performed the last hit, or "Both", if both boys performed it at the same time. -----Examples----- Input 4 3 2 1 2 3 4 Output Vanya Vova Vanya Both Input 2 1 1 1 2 Output Both Both -----Note----- In the first sample Vanya makes the first hit at time 1 / 3, Vova makes the second hit at time 1 / 2, Vanya makes the third hit at time 2 / 3, and both boys make the fourth and fifth hit simultaneously at the time 1. In the second sample Vanya and Vova make the first and second hit simultaneously at time 1.
{"inputs": ["1 1 1\n1\n", "1 1 1\n1\n", "1 1 2\n1\n", "2 1 1\n1\n2\n", "2 1 2\n1\n2\n", "2 1 2\n1\n4\n", "2 1 1\n1\n1\n", "2 1 1\n1\n2\n"], "outputs": ["Both\n", "Both\n", "Vova\n", "Both\nBoth\n", "Vova\nBoth\n", "Vova\nVova\n", "Both\nBoth\n", "Both\nBoth\n"]}
449
142
coding
Solve the programming task below in a Python markdown code block. # Story You and a group of friends are earning some extra money in the school holidays by re-painting the numbers on people's letterboxes for a small fee. Since there are 10 of you in the group each person just concentrates on painting one digit! For example, somebody will paint only the ```1```'s, somebody else will paint only the ```2```'s and so on... But at the end of the day you realise not everybody did the same amount of work. To avoid any fights you need to distribute the money fairly. That's where this Kata comes in. # Kata Task Given the ```start``` and ```end``` letterbox numbers, write a method to return the frequency of all 10 digits painted. # Example For ```start``` = 125, and ```end``` = 132 The letterboxes are * 125 = ```1```, ```2```, ```5``` * 126 = ```1```, ```2```, ```6``` * 127 = ```1```, ```2```, ```7``` * 128 = ```1```, ```2```, ```8``` * 129 = ```1```, ```2```, ```9``` * 130 = ```1```, ```3```, ```0``` * 131 = ```1```, ```3```, ```1``` * 132 = ```1```, ```3```, ```2``` The digit frequencies are 1 x ```0```, 9 x ```1```, 6 x ```2``` etc... and so the method would return ```[1,9,6,3,0,1,1,1,1,1]``` # Notes * 0 < ```start``` <= ```end``` * In C, the returned value will be free'd. Also feel free to reuse/extend the following starter code: ```python def paint_letterboxes(start, finish): ```
{"functional": "_inputs = [[125, 132], [1001, 1001]]\n_outputs = [[[1, 9, 6, 3, 0, 1, 1, 1, 1, 1]], [[2, 2, 0, 0, 0, 0, 0, 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(paint_letterboxes(*i), o[0])"}
445
232
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a string s consisting of words and spaces, return the length of the last word in the string. A word is a maximal substring consisting of non-space characters only.   Please complete the following python code precisely: ```python class Solution: def lengthOfLastWord(self, s: str) -> int: ```
{"functional": "def check(candidate):\n assert candidate(s = \"Hello World\") == 5\n assert candidate(s = \" fly me to the moon \") == 4\n assert candidate(s = \"luffy is still joyboy\") == 6\n\n\ncheck(Solution().lengthOfLastWord)"}
83
72
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. The demons had captured the princess and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of m x n rooms laid out in a 2D grid. Our valiant knight was initially positioned in the top-left room and must fight his way through dungeon to rescue the princess. The knight has an initial health point represented by a positive integer. If at any point his health point drops to 0 or below, he dies immediately. Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers). To reach the princess as quickly as possible, the knight decides to move only rightward or downward in each step. Return the knight's minimum initial health so that he can rescue the princess. Note that any room can contain threats or power-ups, even the first room the knight enters and the bottom-right room where the princess is imprisoned.   Please complete the following python code precisely: ```python class Solution: def calculateMinimumHP(self, dungeon: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]) == 7\n assert candidate(dungeon = [[0]]) == 1\n\n\ncheck(Solution().calculateMinimumHP)"}
258
66
coding
Solve the programming task below in a Python markdown code block. You are given an array of `n+1` integers `1` through `n`. In addition there is a single duplicate integer. The array is unsorted. An example valid array would be `[3, 2, 5, 1, 3, 4]`. It has the integers `1` through `5` and `3` is duplicated. `[1, 2, 4, 5, 5]` would not be valid as it is missing `3`. You should return the duplicate value as a single integer. Also feel free to reuse/extend the following starter code: ```python def find_dup(arr): ```
{"functional": "_inputs = [[[1, 1, 2, 3]], [[1, 2, 2, 3]], [[5, 4, 3, 2, 1, 1]], [[1, 3, 2, 5, 4, 5, 7, 6]], [[8, 2, 6, 3, 7, 2, 5, 1, 4]]]\n_outputs = [[1], [2], [1], [5], [2]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(find_dup(*i), o[0])"}
149
256
coding
Solve the programming task below in a Python markdown code block. Chef has two piles of stones with him, one has n1 stones and the other has n2 stones. Fired up by boredom, he invented a game with the two piles. Before the start of the game Chef chooses an integer m. In the j-th move: - He chooses a number xj such that 1 ≤ xj ≤ m, and removes xj stones from both the piles (this is only possible when both the piles have ≥ xj stones). - The number chosen must be unique over all the moves in the game. That is, for all k < j, xj ≠ xk. The game stops when Chef is unable to make any more moves. Chef wants to make the moves in such a way that the sum of the number of stones remaining in the two piles is minimized. Please help Chef find this. -----Input----- - The first line of input contains an integer T denoting the number of test cases. - Each test case consists of 1 line with three integers — n1, n2 and m — separated by single spaces. -----Output----- For each test case, output a single line containing the minimum sum of the number of stones of two piles. -----Constraints----- Subtask 1 : (5 pts) - 1 ≤ T ≤ 100 - 0 ≤ m ≤ 18 - 0 ≤ n1, n2 ≤ 100 Subtask 2 : (25 pts) - 1 ≤ T ≤ 1000 - 0 ≤ m ≤ 10000 - 0 ≤ n1, n2 ≤ 10000 Subtask 3 : (70 pts) - 1 ≤ T ≤ 105 - 0 ≤ m ≤ 109 - 0 ≤ n1, n2 ≤ 1018 -----Example----- Input:3 1 1 1 1 2 1 4 5 2 Output:0 1 3 -----Explanation----- Example case 1. : Remove 1 stone from each of the piles. Now 0 stones are remaining, so chef cannot remove any more stones from the piles. Hence, answer is 0+0 = 0 Example case 2. : Again, remove 1 stone from both the piles to get (0,1) stones. Now chef cannot remove any more stones from pile 1, so he stops. Hence, answer is 0+1 = 1. Example case 3. : First remove 1 stone from both the piles to get (3,4) stones. Now, remove 2 stones from both the piles so that (1,2) stones are remaining. Now chef cannot remove any more stones owing to the condition that he cannot remove the same number of stones twice. So, the answer is 1+2 = 3.
{"inputs": ["3\n1 1 1\n1 2 1\n4 5 2"], "outputs": ["0\n1\n3"]}
622
34
coding
Solve the programming task below in a Python markdown code block. A permutation $p_1,p_2...p_N$ of $\{1, 2, ..., N\}$ is beautiful if $p_i \& p_{i+1}$ is greater than 0 for every $1 \leq i < N$ . You are given an integer $N$, and your task is toconstruct a beautiful permutation of length $N$ or determine that it's impossible. Note that $a \& b$ denotes the bitwise AND of $a$ and $b$. -----Input:----- First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains a single line of input, an integer $N$. -----Output:----- For each test case output $-1$ if there is no suitable permutation of length $N$, otherwise output $N$ integers in a single line which form a beautiful permutation. If there are multiple answers output any of them. -----Constraints----- - $1 \leq N \leq 10^5$ - The sum of $N$ over all test cases does not exceed $10^6$ -----Subtasks----- - 50 points : $1 \leq N,T \leq 9$ - 50 points : Original constraints -----Sample Input:----- 3 4 3 5 -----Sample Output:----- -1 1 3 2 2 3 1 5 4
{"inputs": ["3\n4\n3\n5"], "outputs": ["-1\n1 3 2\n2 3 1 5 4"]}
314
35
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. A matrix diagonal is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix's end. For example, the matrix diagonal starting from mat[2][0], where mat is a 6 x 3 matrix, includes cells mat[2][0], mat[3][1], and mat[4][2]. Given an m x n matrix mat of integers, sort each matrix diagonal in ascending order and return the resulting matrix.   Please complete the following python code precisely: ```python class Solution: def diagonalSort(self, mat: List[List[int]]) -> List[List[int]]: ```
{"functional": "def check(candidate):\n assert candidate(mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]]) == [[1,1,1,1],[1,2,2,2],[1,2,3,3]]\n assert candidate(mat = [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]]) == [[5,17,4,1,52,7],[11,11,25,45,8,69],[14,23,25,44,58,15],[22,27,31,36,50,66],[84,28,75,33,55,68]]\n\n\ncheck(Solution().diagonalSort)"}
157
258
coding
Solve the programming task below in a Python markdown code block. Like any good boss, the Chef has delegated all cooking jobs to his employees so he can take care of other tasks. Occasionally, one of the cooks needs a tool that is out of reach. In some of these cases, the cook cannot leave their workstation to get the tool because they have to closely watch their food. In such cases, the Chef simply fetches the tool for the cook. Unfortunately, many different cooks have simultaneously requested a tool they cannot reach. Thankfully, no two cooks requested the same tool. Nobody else is available to help the Chef so he has to deliver all of the tools himself. He has to plan a trip around the kitchen in such a way that he picks up each tool and delivers it to the requesting cook. Since the Chef has two hands, he may carry up to two tools at once. Once the last item has been delivered, the Chef also has to return to his starting position. This must be done as fast as possible so the Chef wants to do this while traveling the minimum possible distance. ------ Input ------ The first line contains a single integer T ≤ 20 indicating the number of test cases. Each test case begins with a single integer n, between 1 and 8, indicating the number of requests being made. The following n lines describe the locations of cooks and the tools they request. The i'th such line begins with two integers c_{x},c_{y} describing the location of the cook and ends with two more integers t_{x},t_{y} describing the location of the corresponding requested tool. No tool will be located at the same location as the cook who requests it. The values c_{x}, c_{y}, t_{x}, and t_{y} corresponding to any cook or tool will always lie between 0 and 1000 (inclusive). Finally, the kitchen is laid out into square workstations on a grid so the distance between two points x,y and x',y' is precisely their Manhattan distance |x-x'| + |y-y'|. ------ Output ------ The output for each test case consists of a single line. This line should display a single integer indicating the minimum distance the Chef must travel to deliver the tools to the cooks and return to his start location 0,0. Of course, he may only deliver a tool to a cook if he has already picked up the tool and he may not carry more than two tools at a time. ----- Sample Input 1 ------ 3 2 1 0 0 1 0 0 1 1 3 0 3 0 1 0 4 0 2 0 5 0 3 3 0 1 0 2 0 1 0 2 0 1 0 2 ----- Sample Output 1 ------ 4 10 6 ----- explanation 1 ------ In the second case, the Chef dropped of the first tool to the first cook and then picked up the tool for the third cook. In the last test case, there are three different cooks requesting three different tools. It just so happens that all three tools are at the same location. Still, the Chef can only carry two of the tools from this location at once.
{"inputs": ["3\n2\n1 0 0 1\n0 0 1 1\n3\n0 3 0 1\n0 4 0 2\n0 5 0 3\n3\n0 1 0 2\n0 1 0 2\n0 1 0 2"], "outputs": ["4\n10\n6"]}
683
87
coding
Solve the programming task below in a Python markdown code block. Vishal Wants to buy 2 gifts for his best friend whose name is Annabelle(her age is 20), So they both went for shopping in a store. But Annabelle gave, Vishal a condition that she will accept this gifts only when the total price of the gifts is the same as her age times 100. The store contains, a list of items whose prices are also displayed, Now Vishal is asking for your help to buy gifts, as he wants to impress Annabelle this time. Note: Vishal cannot buy the same item more than once. -----Input:----- - The first line of the input contains a single integer $T$. $T$ denoting the number of test cases. The description of $T$ test cases is as follows. - The next line of the input contains a single integer $N$. $N$ denotes the total number of items in store. - The next line of the input contains $N$ space-separated integers $A1, A2, A3...An$ where $ith$ number denotes the price of $ith$ element. -----Output:----- - For each test-case print "Accepted"(without quotes) if the gifts are accepted by Annabelle, else print "Rejected"(without quotes) -----Constraints:----- - $1 \leq T \leq 10^3$ - $1 \leq N \leq 10^5$ - $1 \leq A1, A2, A3...An \leq 10^7$ -----Sample Input:----- 1 5 10 2 1000 50 1000 -----Sample Output:----- Accepted -----Explanation:----- - As the given list of items have 2 items whose price sum up to the age times 100 of Annabelle i.e. 1000+1000 = (20 *100)
{"inputs": ["1\n5\n10 2 1000 50 1000"], "outputs": ["Accepted"]}
420
32
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Alice and Bob want to water n plants in their garden. The plants are arranged in a row and are labeled from 0 to n - 1 from left to right where the ith plant is located at x = i. Each plant needs a specific amount of water. Alice and Bob have a watering can each, initially full. They water the plants in the following way: Alice waters the plants in order from left to right, starting from the 0th plant. Bob waters the plants in order from right to left, starting from the (n - 1)th plant. They begin watering the plants simultaneously. It takes the same amount of time to water each plant regardless of how much water it needs. Alice/Bob must water the plant if they have enough in their can to fully water it. Otherwise, they first refill their can (instantaneously) then water the plant. In case both Alice and Bob reach the same plant, the one with more water currently in his/her watering can should water this plant. If they have the same amount of water, then Alice should water this plant. Given a 0-indexed integer array plants of n integers, where plants[i] is the amount of water the ith plant needs, and two integers capacityA and capacityB representing the capacities of Alice's and Bob's watering cans respectively, return the number of times they have to refill to water all the plants.   Please complete the following python code precisely: ```python class Solution: def minimumRefill(self, plants: List[int], capacityA: int, capacityB: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(plants = [2,2,3,3], capacityA = 5, capacityB = 5) == 1\n assert candidate(plants = [2,2,3,3], capacityA = 3, capacityB = 4) == 2\n assert candidate(plants = [5], capacityA = 10, capacityB = 8) == 0\n\n\ncheck(Solution().minimumRefill)"}
343
108
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an integer array nums. The range of a subarray of nums is the difference between the largest and smallest element in the subarray. Return the sum of all subarray ranges of nums. A subarray is a contiguous non-empty sequence of elements within an array.   Please complete the following python code precisely: ```python class Solution: def subArrayRanges(self, nums: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [1,2,3]) == 4\n assert candidate(nums = [1,3,3]) == 4\n assert candidate(nums = [4,-2,-3,4,1]) == 59\n\n\ncheck(Solution().subArrayRanges)"}
105
73
coding
Solve the programming task below in a Python markdown code block. CSS colors are defined using a hexadecimal (HEX) notation for the combination of Red, Green, and Blue color values (RGB). Specifications of HEX Color Code ■ It must start with a '#' symbol. ■ It can have $3$ or $\boldsymbol{6}$ digits. ■ Each digit is in the range of $\mbox{o}$ to $\mbox{F}$. ($1,2,3,4,5,6,7,8,9,0,A,B,C,D,E$ and $\mbox{F}$). ■ $A-F$ letters can be lower case. ($a,b,c,d,e$ and $\mbox{f}$ are also valid digits). Examples Valid Hex Color Codes #FFF #025 #F0A1FB Invalid Hex Color Codes #fffabg #abcf #12365erff You are given $N$ lines of CSS code. Your task is to print all valid Hex Color Codes, in order of their occurrence from top to bottom. CSS Code Pattern Selector { Property: Value; } Input Format The first line contains $N$, the number of code lines. The next $N$ lines contains CSS Codes. Constraints $0<N<50$ Output Format Output the color codes with '#' symbols on separate lines. Sample Input 11 #BED { color: #FfFdF8; background-color:#aef; font-size: 123px; background: -webkit-linear-gradient(top, #f9f9f9, #fff); } #Cab { background-color: #ABC; border: 2px dashed #fff; } Sample Output #FfFdF8 #aef #f9f9f9 #fff #ABC #fff Explanation #BED and #Cab satisfy the Hex Color Code criteria, but they are used as selectors and not as color codes in the given CSS. Hence, the valid color codes are: #FfFdF8 #aef #f9f9f9 #fff #ABC #fff Note: There are no comments ( // or /* */) in CSS Code.
{"inputs": ["11\n#BED\n{\n color: #FfFdF8; background-color:#aef;\n font-size: 123px;\n background: -webkit-linear-gradient(top, #f9f9f9, #fff);\n}\n#Cab\n{\n background-color: #ABC;\n border: 2px dashed #fff;\n} \n"], "outputs": ["#FfFdF8\n#aef\n#f9f9f9\n#fff\n#ABC\n#fff\n"]}
489
124
coding
Solve the programming task below in a Python markdown code block. You are given $n$ lengths of segments that need to be placed on an infinite axis with coordinates. The first segment is placed on the axis so that one of its endpoints lies at the point with coordinate $0$. Let's call this endpoint the "start" of the first segment and let's call its "end" as that endpoint that is not the start. The "start" of each following segment must coincide with the "end" of the previous one. Thus, if the length of the next segment is $d$ and the "end" of the previous one has the coordinate $x$, the segment can be placed either on the coordinates $[x-d, x]$, and then the coordinate of its "end" is $x - d$, or on the coordinates $[x, x+d]$, in which case its "end" coordinate is $x + d$. The total coverage of the axis by these segments is defined as their overall union which is basically the set of points covered by at least one of the segments. It's easy to show that the coverage will also be a segment on the axis. Determine the minimal possible length of the coverage that can be obtained by placing all the segments on the axis without changing their order. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. The next $2t$ lines contain descriptions of the test cases. The first line of each test case description contains an integer $n$ ($1 \le n \le 10^4$) — the number of segments. The second line of the description contains $n$ space-separated integers $a_i$ ($1 \le a_i \le 1000$) — lengths of the segments in the same order they should be placed on the axis. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^4$. -----Output----- Print $t$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer — the minimal possible length of the axis coverage. -----Examples----- Input 6 2 1 3 3 1 2 3 4 6 2 3 9 4 6 8 4 5 7 1 2 4 6 7 7 3 8 8 6 5 1 2 2 3 6 Output 3 3 9 9 7 8 -----Note----- In the third sample test case the segments should be arranged as follows: $[0, 6] \rightarrow [4, 6] \rightarrow [4, 7] \rightarrow [-2, 7]$. As you can see, the last segment $[-2, 7]$ covers all the previous ones, and the total length of coverage is $9$. In the fourth sample test case the segments should be arranged as $[0, 6] \rightarrow [-2, 6] \rightarrow [-2, 2] \rightarrow [2, 7]$. The union of these segments also occupies the area $[-2, 7]$ and has the length of $9$.
{"inputs": ["6\n2\n1 3\n3\n1 2 3\n4\n6 2 3 9\n4\n6 8 4 5\n7\n1 2 4 6 7 7 3\n8\n8 6 5 1 2 2 3 6\n", "6\n2\n1 3\n3\n1 2 3\n4\n2 2 3 9\n4\n6 8 4 5\n7\n1 2 4 6 7 7 3\n8\n8 6 5 1 2 2 3 6\n", "6\n2\n1 3\n3\n1 2 6\n4\n6 2 3 9\n4\n6 8 4 5\n7\n1 2 4 6 7 7 3\n8\n8 6 5 1 2 2 3 6\n", "6\n2\n1 3\n3\n1 2 3\n4\n2 2 3 9\n4\n6 8 4 5\n7\n1 2 4 6 7 7 3\n8\n7 6 5 1 2 2 3 6\n", "6\n2\n1 3\n3\n1 2 6\n4\n6 2 3 9\n4\n6 8 4 9\n7\n1 2 4 6 7 7 3\n8\n8 6 5 1 2 2 3 6\n", "6\n2\n1 3\n3\n1 2 0\n4\n2 2 3 9\n4\n6 8 4 5\n7\n1 2 4 6 7 7 3\n8\n7 6 5 1 2 2 3 6\n", "6\n2\n1 4\n3\n1 2 3\n4\n2 2 3 9\n4\n6 8 4 5\n7\n1 0 4 6 4 7 3\n8\n8 2 5 1 2 4 6 6\n", "6\n2\n1 5\n3\n1 2 6\n4\n6 2 3 9\n4\n6 8 4 5\n7\n1 2 4 6 7 7 3\n8\n8 6 5 1 2 2 3 6\n"], "outputs": ["3\n3\n9\n9\n7\n8\n", "3\n3\n9\n9\n7\n8\n", "3\n6\n9\n9\n7\n8\n", "3\n3\n9\n9\n7\n7\n", "3\n6\n9\n12\n7\n8\n", "3\n2\n9\n9\n7\n7\n", "4\n3\n9\n9\n9\n8\n", "5\n6\n9\n9\n7\n8\n"]}
702
695
coding
Solve the programming task below in a Python markdown code block. problem AOR Ika is studying to pass the test. AOR Ika-chan solved the $ N $ question. After that, round the solved problem according to the following procedure. 1. Check the correctness of the answer. 2. If the answer is correct, write a circle mark, and if it is incorrect, write a cross mark on the answer sheet. AOR Ika faints because of the fear of failing the test the moment she finds that the answer is wrong for $ 2 $ in a row. And no further rounding is possible. Syncope occurs between steps $ 1 $ and $ 2 $. You will be given an integer $ N $, which represents the number of questions AOR Ika has solved, and a string $ S $, which is a length $ N $ and represents the correctness of the answer. The string consists of'o'and'x', with'o' indicating the correct answer and'x' indicating the incorrect answer. The $ i $ letter indicates the correctness of the $ i $ question, and AOR Ika-chan rounds the $ 1 $ question in order. Please output the number of questions that AOR Ika-chan can write the correctness. output Output the number of questions that AOR Ika-chan could write in the $ 1 $ line. Also, output a line break at the end. Example Input 3 oxx Output 2
{"inputs": ["3\nnxx", "3\nnwx", "3\nxxo", "3\nxwn", "3\nowx", "3\npxx", "3\npyx", "3\npzx"], "outputs": ["2\n", "3\n", "1\n", "3\n", "3\n", "2\n", "3\n", "3\n"]}
306
85
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an array of points on the X-Y plane points where points[i] = [xi, yi], return the area of the largest triangle that can be formed by any three different points. Answers within 10-5 of the actual answer will be accepted.   Please complete the following python code precisely: ```python class Solution: def largestTriangleArea(self, points: List[List[int]]) -> float: ```
{"functional": "def check(candidate):\n assert candidate(points = [[0,0],[0,1],[1,0],[0,2],[2,0]]) == 2.00000\n assert candidate(points = [[1,0],[0,0],[0,1]]) == 0.50000\n\n\ncheck(Solution().largestTriangleArea)"}
103
85
coding
Solve the programming task below in a Python markdown code block. You are given four points $A,B,C$ and $\mbox{D}$ in a 3-dimensional Cartesian coordinate system. You are required to print the angle between the plane made by the points $A,B,C$ and $B,C,D$ in degrees(not radians). Let the angle be $\textit{PHI}$. $\textit{Cos}(PHI)=(X.Y)/|X||Y|$ where $X=\textit{AB}$ x $\mbox{BC}$ and $\textbf{Y}=\textit{BC}$ x $\boldsymbol{\mathrm{CD}}$. Here, $X.Y$ means the dot product of $\mbox{X}$ and $\mathbf{Y}$, and $\boldsymbol{AB}$ x $\mbox{BC}$ means the cross product of vectors $\boldsymbol{AB}$ and $\mbox{BC}$. Also, $\textit{AB}=B-A$. Input Format One line of input containing the space separated floating number values of the $X,Y$ and $\mbox{z}$ coordinates of a point. Output Format Output the angle correct up to two decimal places. Sample Input 0 4 5 1 7 6 0 5 9 1 7 2 Sample Output 8.19
{"inputs": ["0 4 5\n1 7 6\n0 5 9\n1 7 2\n"], "outputs": ["8.19\n"]}
290
39
coding
Solve the programming task below in a Python markdown code block. John Doe started thinking about graphs. After some thought he decided that he wants to paint an undirected graph, containing exactly k cycles of length 3. A cycle of length 3 is an unordered group of three distinct graph vertices a, b and c, such that each pair of them is connected by a graph edge. John has been painting for long, but he has not been a success. Help him find such graph. Note that the number of vertices there shouldn't exceed 100, or else John will have problems painting it. Input A single line contains an integer k (1 ≤ k ≤ 105) — the number of cycles of length 3 in the required graph. Output In the first line print integer n (3 ≤ n ≤ 100) — the number of vertices in the found graph. In each of next n lines print n characters "0" and "1": the i-th character of the j-th line should equal "0", if vertices i and j do not have an edge between them, otherwise it should equal "1". Note that as the required graph is undirected, the i-th character of the j-th line must equal the j-th character of the i-th line. The graph shouldn't contain self-loops, so the i-th character of the i-th line must equal "0" for all i. Examples Input 1 Output 3 011 101 110 Input 10 Output 5 01111 10111 11011 11101 11110
{"inputs": ["5\n", "7\n", "4\n", "9\n", "6\n", "2\n", "8\n", "3\n"], "outputs": ["5\n01111\n10111\n11010\n11100\n11000\n", "5\n01111\n10111\n11011\n11100\n11100\n", "4\n0111\n1011\n1101\n1110\n", "7\n0111111\n1011111\n1101100\n1110000\n1110000\n1100000\n1100000\n", "6\n011111\n101111\n110100\n111000\n110000\n110000\n", "4\n0111\n1011\n1100\n1100\n", "6\n011111\n101111\n110110\n111000\n111000\n110000\n", "5\n01111\n10111\n11000\n11000\n11000\n"]}
356
340
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. We define the string base to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so base will look like this: "...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....". Given a string s, return the number of unique non-empty substrings of s are present in base.   Please complete the following python code precisely: ```python class Solution: def findSubstringInWraproundString(self, s: str) -> int: ```
{"functional": "def check(candidate):\n assert candidate(s = \"a\") == 1\n assert candidate(s = \"cac\") == 2\n assert candidate(s = \"zab\") == 6\n\n\ncheck(Solution().findSubstringInWraproundString)"}
102
60
coding
Solve the programming task below in a Python markdown code block. You are given a permutation P of length N. You can perform the following operation on P: Choose any i and j such that i < j and P_{i} < P_{j}, and remove exactly one of them from the array. After removing, the remaining parts of the array are concatenated. Find out if it is possible to apply the given operation any number of times such that only one element remains in the array. Note: A permutation of length N is an array where every element from 1 to N occurs exactly once. ------ Input Format ------ - The first line contains T - the number of test cases. Then the test cases follow. - The first line of each test case contains N - the length of the permutation P. - The second line of each test case contains N space-separated integers P_{1}, P_{2}, \dots, P_{N} - elements of the permutation. ------ Output Format ------ For each test case, output YES if it is possible to reduce the array to a single element using the given operation. Otherwise, output 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 ≤T ≤3 \cdot 10^{5}$ $1 ≤N ≤3 \cdot 10^{5}$ $1 ≤P_{i} ≤N$ $P_{1}, P_{2}, \dots, P_{N}$ form a permutation of length $N$ $\sum N$ over all test cases is at most $3 \cdot 10^{5}$ ----- Sample Input 1 ------ 3 4 3 1 2 4 3 2 3 1 6 5 2 3 6 1 4 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ - Test case $1$: We can apply operations as follows (the bolded elements are the pair chosen for that operation): $ [\textbf{3}, \text{1}, \text{2}, \textbf{4}] \rightarrow [\textbf{1}, \text{2}, \textbf{4}] \rightarrow [\textbf{2}, \textbf{4}] \rightarrow [\text{4}]- Test case $2$: It can be shown that it is not possible to remove all elements except one element from the array. - Test case $3$: We can apply operations as follows: $ [\textbf{5}, \text{2}, \text{3}, \textbf{6}, \text{1}, \text{4}] \rightarrow [\text{2}, \textbf{3}, \textbf{6}, \text{1}, \text{4}] \rightarrow [\textbf{2}, \text{3}, \text{1}, \textbf{4}] \rightarrow [\textbf{3}, \text{1}, \textbf{4}] \rightarrow [\textbf{1}, \textbf{4}] \rightarrow [\text{4}]
{"inputs": ["3\n4\n3 1 2 4\n3\n2 3 1\n6\n5 2 3 6 1 4\n"], "outputs": ["YES\nNO\nYES\n"]}
675
50
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an array items, where each items[i] = [typei, colori, namei] describes the type, color, and name of the ith item. You are also given a rule represented by two strings, ruleKey and ruleValue. The ith item is said to match the rule if one of the following is true: ruleKey == "type" and ruleValue == typei. ruleKey == "color" and ruleValue == colori. ruleKey == "name" and ruleValue == namei. Return the number of items that match the given rule.   Please complete the following python code precisely: ```python class Solution: def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int: ```
{"functional": "def check(candidate):\n assert candidate(items = [[\"phone\",\"blue\",\"pixel\"],[\"computer\",\"silver\",\"lenovo\"],[\"phone\",\"gold\",\"iphone\"]], ruleKey = \"color\", ruleValue = \"silver\") == 1\n assert candidate(items = [[\"phone\",\"blue\",\"pixel\"],[\"computer\",\"silver\",\"phone\"],[\"phone\",\"gold\",\"iphone\"]], ruleKey = \"type\", ruleValue = \"phone\") == 2\n\n\ncheck(Solution().countMatches)"}
179
112
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese and Russian. Recently Chef bought a bunch of robot-waiters. And now he needs to know how much to pay for the electricity that robots use for their work. All waiters serve food from the kitchen (which is in the point (0, 0)) and carry it to some table (which is in some point (x, y)) in a shortest way. But this is a beta version of robots and they can only do the next moves: turn right and make a step forward or turn left and make a step forward. Initially they look in direction of X-axis. Your task is to calculate for each query the number of moves they’ll do to reach corresponding table. ------ Input ------ The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. For each test case there is a sing line containing two space-separated integers - x and y. ------ Output ------ For each test case, output a single line containing number of moves that robot will make to reach point (x, y) ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $-10^{9} ≤ x, y ≤ 10^{9}$   ----- Sample Input 1 ------ 2 3 3 3 4 ----- Sample Output 1 ------ 6 7 ------ Explanation 0 ------ Example case 1. Sequence of moves would be LRLRLR
{"inputs": ["2\n3 3\n3 4", "2\n3 3\n3 0", "2\n5 3\n3 0", "2\n5 3\n6 0", "2\n5 3\n6 1", "2\n5 2\n6 1", "2\n3 2\n6 1", "2\n1 2\n6 1"], "outputs": ["6\n7", "6\n7\n", "10\n7\n", "10\n12\n", "10\n13\n", "11\n13\n", "7\n13\n", "3\n13\n"]}
321
150
coding
Solve the programming task below in a Python markdown code block. Gerald is very particular to eight point sets. He thinks that any decent eight point set must consist of all pairwise intersections of three distinct integer vertical straight lines and three distinct integer horizontal straight lines, except for the average of these nine points. In other words, there must be three integers x_1, x_2, x_3 and three more integers y_1, y_2, y_3, such that x_1 < x_2 < x_3, y_1 < y_2 < y_3 and the eight point set consists of all points (x_{i}, y_{j}) (1 ≤ i, j ≤ 3), except for point (x_2, y_2). You have a set of eight points. Find out if Gerald can use this set? -----Input----- The input consists of eight lines, the i-th line contains two space-separated integers x_{i} and y_{i} (0 ≤ x_{i}, y_{i} ≤ 10^6). You do not have any other conditions for these points. -----Output----- In a single line print word "respectable", if the given set of points corresponds to Gerald's decency rules, and "ugly" otherwise. -----Examples----- Input 0 0 0 1 0 2 1 0 1 2 2 0 2 1 2 2 Output respectable Input 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 Output ugly Input 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 Output ugly
{"inputs": ["0 0\n0 1\n0 2\n1 0\n1 2\n2 0\n2 1\n2 2\n", "0 0\n1 0\n2 0\n3 0\n4 0\n5 0\n6 0\n7 0\n", "1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n", "0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n", "0 0\n1 0\n0 1\n1 1\n0 2\n1 2\n0 3\n1 3\n", "0 0\n2 1\n1 0\n0 2\n2 2\n1 0\n2 1\n0 2\n", "0 0\n2 1\n1 0\n0 2\n2 2\n1 0\n2 1\n0 2\n", "0 1\n0 1\n0 2\n1 1\n1 2\n2 1\n2 1\n2 2\n"], "outputs": ["respectable\n", "ugly\n", "ugly\n", "ugly\n", "ugly\n", "ugly\n", "ugly\n", "ugly\n"]}
388
318
coding
Solve the programming task below in a Python markdown code block. In Takahashi Kingdom, there is an archipelago of N islands, called Takahashi Islands. For convenience, we will call them Island 1, Island 2, ..., Island N. There are M kinds of regular boat services between these islands. Each service connects two islands. The i-th service connects Island a_i and Island b_i. Cat Snuke is on Island 1 now, and wants to go to Island N. However, it turned out that there is no boat service from Island 1 to Island N, so he wants to know whether it is possible to go to Island N by using two boat services. Help him. Constraints * 3 ≤ N ≤ 200 000 * 1 ≤ M ≤ 200 000 * 1 ≤ a_i < b_i ≤ N * (a_i, b_i) \neq (1, N) * If i \neq j, (a_i, b_i) \neq (a_j, b_j). Input Input is given from Standard Input in the following format: N M a_1 b_1 a_2 b_2 : a_M b_M Output If it is possible to go to Island N by using two boat services, print `POSSIBLE`; otherwise, print `IMPOSSIBLE`. Examples Input 3 2 1 2 2 3 Output POSSIBLE Input 4 3 1 2 2 3 3 4 Output IMPOSSIBLE Input 100000 1 1 99999 Output IMPOSSIBLE Input 5 5 1 3 4 5 2 3 2 4 1 4 Output POSSIBLE
{"inputs": ["3 2\n1 2\n0 3", "3 2\n1 2\n2 3", "4 3\n1 3\n2 3\n3 4", "4 3\n1 3\n3 3\n3 4", "4 3\n1 1\n2 3\n3 4", "4 2\n1 3\n2 3\n3 4", "4 3\n1 1\n2 5\n3 4", "4 2\n1 3\n2 3\n0 4"], "outputs": ["IMPOSSIBLE\n", "POSSIBLE", "POSSIBLE\n", "POSSIBLE\n", "IMPOSSIBLE\n", "IMPOSSIBLE\n", "IMPOSSIBLE\n", "IMPOSSIBLE\n"]}
403
186
coding
Solve the programming task below in a Python markdown code block. Chef is playing a game on the non-negative x-axis. It takes him $1$ second to reach from $i^{th}$ position to $(i-1)^{th}$ position or $(i+1)^{th}$ position. The chef never goes to the negative x-axis. Also, Chef doesn't stop at any moment of time. The movement of chef can be described as follows. At the start he is standing at $x=0$ at time $0$. In the first round, he moves towards $x=1$ and comes back to the $x=0$ position. In the second round, he moves towards the $x=2$ and comes back again to $x=0$. Generalizing, in the $k^{th}$ round, he moves from $x=0$ to $x=k$ and then returns back to $x=0$ at the end of the round. This goes on as the game progresses. For Example, the path of Chef for $3^{rd}$ round is given below. $0 - 1 - 2 - 3 - 2 - 1 - 0$ The overall path followed by Chef would look somewhat like this: $0 - 1 - 0 - 1 - 2 - 1 - 0 - 1 - 2 - 3 - 2 - 1 - 0 - 1 - 2 - 3 - 4 - 3 - …$ You are given two non-negative integers $N$ and $K$. You have to tell the time at which Chef arrives at $x=N$ for the $K^{th}$ time. Note - Chef can not skip a position while visiting the positions. ------ Input: ------ The first line contains $T$ the number of test cases. Then the test cases follow. Each test case contains a single line of two integers $N$ and $K$. ------ Output: ------ For each test case, print a single line containing one integer -- the time taken by the chef to arrive at $x=N$ for the $K^{th}$ time by modulo $1,000,000,007$. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $0 ≤ N ≤ 10^{9}$ $1 ≤ K ≤ 10^{9}$ ----- Sample Input 1 ------ 5 0 1 1 1 2 1 1 3 4 6 ----- Sample Output 1 ------ 0 1 4 5 46 ----- explanation 1 ------ Test Case 1: Chef starts the journey from the $N = 0$ at time $t = 0$ and it's the first time $(K = 1)$, he is here. So, the answer is $0$. Test Case 2: Chef starts the journey from the $N = 0$ at time $t = 0$ then goes to $N = 1$ at $t = 1$ and it's the first time $(K = 1)$, he is here. So, the answer is $1$. Test Case 4: The path followed by Chef to reach $1$ for the third time is given below. $0 - 1 - 0 - 1 - 2 - 1$ He reaches $1$ for the third time at $t=5$.
{"inputs": ["5\n0 1\n1 1\n2 1\n1 3\n4 6"], "outputs": ["0\n1\n4\n5\n46"]}
748
41
coding
Solve the programming task below in a Python markdown code block. Sunita has lots of tasks pending and she has no time to complete. She needs your help and wants you complete the task. You are given a list of integers and two values $N$ and $K$ $-$ the size of array of integers and the numbers of partitions to be made respectively. You have to partition the list of integers without changing the order of elements ,into exactly $K$ parts. Calculate Greatest Common Divisor of all $K$ partition and sum up the gcd values for each partition. Maximize the sum obtained. Can you help Sunita ? -----Input:----- - 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 $N, K$. - Next line contains $N$ integers $-$ the list of integers. -----Output:----- For each test case, output in a single line integer $-$ the maximal result. -----Constraints----- - $1 \leq T \leq 100$ - $1 \leq N, K \leq 250$ - $1 \leq K \leq N$ - $1 \leq A[i] \leq 1e5$ -----Sample Input:----- 1 4 2 5 6 3 2 -----Sample Output:----- 6 -----EXPLANATION:----- [5] [6 3 2] is the best partition [5 + GCD(6,3,2)] = 6
{"inputs": ["1\n4 2\n5 6 3 2"], "outputs": ["6"]}
340
24
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. There is a haunted town called HauntedLand. The structure of HauntedLand can be thought of as a grid of size n * m. There is a house in each cell of the grid. Some people have fled from their houses because they were haunted. '.' represents a haunted house whereas '*' represents a house in which people are living. One day, Devu, the famous perfumer came to town with a perfume whose smell can hypnotize people. Devu can put the perfume in at most one of the houses. This takes Devu one second. Then, the perfume spreads from one house (need not be inhabited by people) to all its adjacent houses in one second, and the cycle continues. Two houses are said to be a adjacent to each other, if they share a corner or an edge, i.e., each house (except those on the boundaries) will have 8 adjacent houses. You want to save people from Devu's dark perfumery by sending them a message to flee from the town. So, you need to estimate the minimum amount of time Devu needs to hypnotize all the people? Note that if there are no houses inhabited by people, Devu doesn't need to put perfume in any cell. ------ Input ------ The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows. First line of each test case contains two space separated integers n, m denoting the dimensions of the town. For each of next n lines, each line has m characters (without any space) denoting a row of houses of the town. ------ Output ------ For each test case, output a single integer corresponding to the answer of the problem. ------ Constraints ------ $1 ≤ T ≤ 20$ Subtask #1: (40 points) $1 ≤ n, m ≤ 100$ Subtask #2: (60 points) $1 ≤ n, m ≤ 1000$ ----- Sample Input 1 ------ 2 2 2 *. .. 3 4 .*.. *. .*.. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ In the first example, it will take Devu one second for putting the perfume at the only house. So, the answer is 1. In the second example, He will first put the perfume at the * at cell (1, 1) (assuming 0-based indexing). Now, it will take Devu 1 secs to put perfume. In the next second, the perfume will spread to all of its adjacent cells, thus making each house haunted. So, the answer is 2.
{"inputs": ["2\n2 2\n*.\n..\n3 4\n.*..\n***.\n.*..", "2\n2 2\n*.\n..\n3 4\n.*..\n***.\n.*.."], "outputs": ["1\n2", "1\n2"]}
581
66
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. There is a 2D grid of size n x n where each cell of this grid has a lamp that is initially turned off. You are given a 2D array of lamp positions lamps, where lamps[i] = [rowi, coli] indicates that the lamp at grid[rowi][coli] is turned on. Even if the same lamp is listed more than once, it is turned on. When a lamp is turned on, it illuminates its cell and all other cells in the same row, column, or diagonal. You are also given another 2D array queries, where queries[j] = [rowj, colj]. For the jth query, determine whether grid[rowj][colj] is illuminated or not. After answering the jth query, turn off the lamp at grid[rowj][colj] and its 8 adjacent lamps if they exist. A lamp is adjacent if its cell shares either a side or corner with grid[rowj][colj]. Return an array of integers ans, where ans[j] should be 1 if the cell in the jth query was illuminated, or 0 if the lamp was not.   Please complete the following python code precisely: ```python class Solution: def gridIllumination(self, n: int, lamps: List[List[int]], queries: List[List[int]]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(n = 5, lamps = [[0,0],[4,4]], queries = [[1,1],[1,0]]) == [1,0]\n assert candidate(n = 5, lamps = [[0,0],[4,4]], queries = [[1,1],[1,1]]) == [1,1]\n assert candidate(n = 5, lamps = [[0,0],[0,4]], queries = [[0,4],[0,1],[1,4]]) == [1,1,0]\n\n\ncheck(Solution().gridIllumination)"}
298
138
coding
Solve the programming task below in a Python markdown code block. problem Chairman K is a regular customer of the JOI pizza shop in the center of JOI city. For some reason, he decided to start a life-saving life this month. So he wanted to order the pizza with the highest calories per dollar among the pizzas he could order at the JOI pizza store. Let's call such a pizza the "best pizza". The "best pizza" is not limited to one type. At JOI Pizza, you can freely choose from N types of toppings and order the ones placed on the basic dough. You cannot put more than one topping of the same type. You can also order a pizza that doesn't have any toppings on the dough. The price of the dough is $ A and the price of the toppings is $ B. The price of pizza is the sum of the price of the dough and the price of the toppings. That is, the price of a pizza with k types of toppings (0 ≤ k ≤ N) is A + k x B dollars. The total calorie of the pizza is the sum of the calories of the dough and the calories of the toppings placed. Create a program to find the number of calories per dollar for the "best pizza" given the price of the dough and the price of the toppings, and the calorie value of the dough and each topping. input The input consists of N + 3 lines. On the first line, one integer N (1 ≤ N ≤ 100) representing the number of topping types is written. On the second line, two integers A and B (1 ≤ A ≤ 1000, 1 ≤ B ≤ 1000) are written with a blank as a delimiter. A is the price of the dough and B is the price of the toppings. On the third line, one integer C (1 ≤ C ≤ 10000) representing the number of calories in the dough is written. On the 3 + i line (1 ≤ i ≤ N), one integer Di (1 ≤ Di ≤ 10000) representing the number of calories in the i-th topping is written. output Print the number of calories per dollar for the "best pizza" in one line. However, round down the numbers after the decimal point and output as an integer value. Input / output example Input example 1 3 12 2 200 50 300 100 Output example 1 37 In I / O Example 1, with the second and third toppings, 200 + 300 + 100 = 600 calories gives a pizza of $ 12 + 2 x 2 = $ 16. This pizza has 600/16 = 37.5 calories per dollar. Since this is the "best pizza", we output 37, rounded down to the nearest whole number of 37.5. Input example 2 Four 20 3 900 300 100 400 1300 Output example 2 100 The question text and the data used for the automatic referee are the question text and the test data for scoring, which are created and published by the Japan Committee for Information Olympics. Example Input 3 12 2 200 50 300 100 Output 37
{"inputs": ["3\n2 3\n7\n0\n37\n110", "3\n2 1\n7\n0\n37\n110", "3\n2 1\n7\n0\n37\n010", "3\n2 1\n2\n0\n35\n010", "3\n2 2\n2\n0\n35\n000", "3\n5 2\n1\n0\n45\n101", "3\n1 2\n1\n0\n45\n101", "3\n2 1\n8\n0\n37\n010"], "outputs": ["23\n", "39\n", "14\n", "12\n", "9\n", "16\n", "34\n", "15\n"]}
737
189
coding
Solve the programming task below in a Python markdown code block. Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is v_{i}. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She will tell you two numbers, l and r (1 ≤ l ≤ r ≤ n), and you should tell her $\sum_{i = l}^{r} v_{i}$. Let u_{i} be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and r (1 ≤ l ≤ r ≤ n), and you should tell her $\sum_{i = l}^{r} u_{i}$. For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy. -----Input----- The first line contains an integer n (1 ≤ n ≤ 10^5). The second line contains n integers: v_1, v_2, ..., v_{n} (1 ≤ v_{i} ≤ 10^9) — costs of the stones. The third line contains an integer m (1 ≤ m ≤ 10^5) — the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and r (1 ≤ l ≤ r ≤ n; 1 ≤ type ≤ 2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one. -----Output----- Print m lines. Each line must contain an integer — the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input. -----Examples----- Input 6 6 4 2 7 2 7 3 2 3 6 1 3 4 1 1 6 Output 24 9 28 Input 4 5 5 2 3 10 1 2 4 2 1 4 1 1 1 2 1 4 2 1 2 1 1 1 1 3 3 1 1 3 1 4 4 1 2 2 Output 10 15 5 15 5 5 2 12 3 5 -----Note----- Please note that the answers to the questions may overflow 32-bit integer type.
{"inputs": ["1\n1\n2\n1 1 1\n2 1 1\n", "1\n1\n2\n1 1 1\n2 1 1\n", "6\n6 4 2 7 2 7\n3\n2 3 6\n1 3 4\n1 1 6\n", "6\n6 6 3 8 5 4\n3\n2 2 3\n2 1 5\n1 1 5\n", "6\n6 6 3 8 5 4\n3\n2 2 3\n2 1 5\n1 1 5\n", "6\n6 6 3 8 5 6\n3\n2 2 3\n2 1 5\n1 1 5\n", "6\n1 6 3 8 5 6\n3\n2 2 3\n2 1 5\n1 1 5\n", "6\n1 1 3 8 5 6\n3\n2 2 3\n2 1 5\n1 1 5\n"], "outputs": ["1\n1\n", "1\n1\n", "24\n9\n28\n", "9\n24\n28\n", "9\n24\n28\n", "11\n26\n28\n", "8\n21\n23\n", "4\n16\n18\n"]}
603
335
coding
Solve the programming task below in a Python markdown code block. Given a string `s` of uppercase letters, your task is to determine how many strings `t` (also uppercase) with length equal to that of `s` satisfy the followng conditions: * `t` is lexicographical larger than `s`, and * when you write both `s` and `t` in reverse order, `t` is still lexicographical larger than `s`. ```Haskell For example: solve('XYZ') = 5. They are: YYZ, ZYZ, XZZ, YZZ, ZZZ ``` String lengths are less than `5000`. Return you answer `modulo 10^9+7 (= 1000000007)`. More examples in test cases. Good luck! Also feel free to reuse/extend the following starter code: ```python def solve(s): ```
{"functional": "_inputs = [['XYZ'], ['ABC'], ['ABCD'], ['ZAZ'], ['XYZA']]\n_outputs = [[5], [16174], [402230], [25], [34480]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(solve(*i), o[0])"}
200
195
coding
Solve the programming task below in a Python markdown code block. Furik loves writing all sorts of problems, especially such that he can't solve himself. You've got one of his problems, the one Furik gave to Rubik. And Rubik asks you to solve it. There is integer n and array a, consisting of ten integers, indexed by numbers from 0 to 9. Your task is to count the number of positive integers with the following properties: * the number's length does not exceed n; * the number doesn't have leading zeroes; * digit i (0 ≤ i ≤ 9) occurs in the number at least a[i] times. Input The first line contains integer n (1 ≤ n ≤ 100). The next line contains 10 integers a[0], a[1], ..., a[9] (0 ≤ a[i] ≤ 100) — elements of array a. The numbers are separated by spaces. Output On a single line print the remainder of dividing the answer to the problem by 1000000007 (109 + 7). Examples Input 1 0 0 0 0 0 0 0 0 0 1 Output 1 Input 2 1 1 0 0 0 0 0 0 0 0 Output 1 Input 3 1 1 0 0 0 0 0 0 0 0 Output 36 Note In the first sample number 9 meets the requirements. In the second sample number 10 meets the requirements. In the third sample numbers 10, 110, 210, 120, 103 meet the requirements. There are other suitable numbers, 36 in total.
{"inputs": ["6\n2 2 2 0 0 0 0 0 0 0\n", "4\n1 1 2 0 0 0 0 0 0 0\n", "8\n1 0 1 0 1 1 2 2 0 0\n", "6\n0 1 0 1 1 0 0 0 1 0\n", "6\n1 1 0 1 2 1 0 0 0 0\n", "7\n0 0 2 2 1 0 1 0 0 0\n", "6\n1 1 1 1 1 1 0 0 0 0\n", "4\n0 0 1 0 1 0 0 0 1 1\n"], "outputs": ["60\n", "9\n", "8820\n", "23160\n", "300\n", "9660\n", "600\n", "24\n"]}
399
246
coding
Solve the programming task below in a Python markdown code block. Demodogs from the Upside-down have attacked Hawkins again. El wants to reach Mike and also kill as many Demodogs in the way as possible. Hawkins can be represented as an $n \times n$ grid. The number of Demodogs in a cell at the $i$-th row and the $j$-th column is $i \cdot j$. El is at position $(1, 1)$ of the grid, and she has to reach $(n, n)$ where she can find Mike. The only directions she can move are the right (from $(i, j)$ to $(i, j + 1)$) and the down (from $(i, j)$ to $(i + 1, j)$). She can't go out of the grid, as there are doors to the Upside-down at the boundaries. Calculate the maximum possible number of Demodogs $\mathrm{ans}$ she can kill on the way, considering that she kills all Demodogs in cells she visits (including starting and finishing cells). Print $2022 \cdot \mathrm{ans}$ modulo $10^9 + 7$. Modulo $10^9 + 7$ because the result can be too large and multiplied by $2022$ because we are never gonna see it again! (Note, you firstly multiply by $2022$ and only after that take the remainder.) -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). Description of the test cases follows. The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^9$) — the size of the grid. -----Output----- For each test case, print a single integer — the maximum number of Demodogs that can be killed multiplied by $2022$, modulo $10^9 + 7$. -----Examples----- Input 4 2 3 50 1000000000 Output 14154 44484 171010650 999589541 -----Note----- In the first test case, for any path chosen by her the number of Demodogs to be killed would be $7$, so the answer would be $2022 \cdot 7 = 14154$.
{"inputs": ["4\n2\n3\n50\n1000000000\n"], "outputs": ["14154\n44484\n171010650\n999589541\n"]}
545
62
coding
Solve the programming task below in a Python markdown code block. To make it difficult to withdraw money, a certain bank allows its customers to withdraw only one of the following amounts in one operation: - 1 yen (the currency of Japan) - 6 yen, 6^2(=36) yen, 6^3(=216) yen, ... - 9 yen, 9^2(=81) yen, 9^3(=729) yen, ... At least how many operations are required to withdraw exactly N yen in total? It is not allowed to re-deposit the money you withdrew. -----Constraints----- - 1 \leq N \leq 100000 - N is an integer. -----Input----- Input is given from Standard Input in the following format: N -----Output----- If at least x operations are required to withdraw exactly N yen in total, print x. -----Sample Input----- 127 -----Sample Output----- 4 By withdrawing 1 yen, 9 yen, 36(=6^2) yen and 81(=9^2) yen, we can withdraw 127 yen in four operations.
{"inputs": ["1", "0", "8", "2", "5", "6", "4", "9"], "outputs": ["1\n", "0\n", "3\n", "2\n", "5\n", "1\n", "4\n", "1\n"]}
257
62
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an array of positive integers arr. Perform some operations (possibly none) on arr so that it satisfies these conditions: The value of the first element in arr must be 1. The absolute difference between any 2 adjacent elements must be less than or equal to 1. In other words, abs(arr[i] - arr[i - 1]) <= 1 for each i where 1 <= i < arr.length (0-indexed). abs(x) is the absolute value of x. There are 2 types of operations that you can perform any number of times: Decrease the value of any element of arr to a smaller positive integer. Rearrange the elements of arr to be in any order. Return the maximum possible value of an element in arr after performing the operations to satisfy the conditions.   Please complete the following python code precisely: ```python class Solution: def maximumElementAfterDecrementingAndRearranging(self, arr: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(arr = [2,2,1,2,1]) == 2\n assert candidate(arr = [100,1,1000]) == 3\n assert candidate(arr = [1,2,3,4,5]) == 5\n\n\ncheck(Solution().maximumElementAfterDecrementingAndRearranging)"}
222
89
coding
Solve the programming task below in a Python markdown code block. A sequence of $n$ numbers is called a permutation if it contains all integers from $1$ to $n$ exactly once. For example, the sequences [$3, 1, 4, 2$], [$1$] and [$2,1$] are permutations, but [$1,2,1$], [$0,1$] and [$1,3,4$] — are not. Polycarp lost his favorite permutation and found only some of its elements — the numbers $b_1, b_2, \dots b_m$. He is sure that the sum of the lost elements equals $s$. Determine whether one or more numbers can be appended to the given sequence $b_1, b_2, \dots b_m$ such that the sum of the added numbers equals $s$, and the resulting new array is a permutation? -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 100$) —the number of test cases. Then the descriptions of the test cases follow. The first line of each test set contains two integers $m$ and $s$ ($1 \le m \le 50$, $1 \le s \le 1000$)—-the number of found elements and the sum of forgotten numbers. The second line of each test set contains $m$ different integers $b_1, b_2 \dots b_m$ ($1 \le b_i \le 50$) — the elements Polycarp managed to find. -----Output----- Print $t$ lines, each of which is the answer to the corresponding test set. Print as the answer YES if you can append several elements to the array $b$, that their sum equals $s$ and the result will be a permutation. Output NO otherwise. You can output the answer in any case (for example, yEs, yes, Yes and YES will be recognized as positive answer). -----Examples----- Input 5 3 13 3 1 4 1 1 1 3 3 1 4 2 2 1 4 3 5 6 1 2 3 4 5 Output YES NO YES NO YES -----Note----- In the test case of the example, $m=3, s=13, b=[3,1,4]$. You can append to $b$ the numbers $6,2,5$, the sum of which is $6+2+5=13$. Note that the final array will become $[3,1,4,6,2,5]$, which is a permutation. In the second test case of the example, $m=1, s=1, b=[1]$. You cannot append one or more numbers to $[1]$ such that their sum equals $1$ and the result is a permutation. In the third test case of the example, $m=3, s=3, b=[1,4,2]$. You can append the number $3$ to $b$. Note that the resulting array will be $[1,4,2,3]$, which is a permutation.
{"inputs": ["1\n1 2\n4\n", "1\n2 4\n5 1\n", "1\n3 13\n4 1 3\n", "1\n7 489\n5 29 12 16 25 36 18\n", "2\n1 9\n2\n9 9\n11 6 5 13 7 10 2 3 12\n", "5\n3 13\n3 1 4\n1 1\n1\n3 3\n1 4 2\n2 1\n4 3\n5 6\n1 2 3 4 5\n", "1\n50 870\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50\n"], "outputs": ["NO\n", "NO\n", "YES\n", "NO\n", "NO\nNO\n", "YES\nNO\nYES\nNO\nYES\n", "YES\n"]}
695
352
coding
Solve the programming task below in a Python markdown code block. -----Problem----- Suppose there is a circle. There are N Juice shops on that circle. Juice shops are numbered 0 to N-1 (both inclusive). You have two pieces of information corresponding to each of the juice shop: (1) the amount of Juice that a particular Juice shop can provide and (2) the distance from that juice shop to the next juice shop. Initially, there is a man with a bottle of infinite capacity carrying no juice. He can start the tour at any of the juice shops. Calculate the first point from where the man will be able to complete the circle. Consider that the man will stop at every Juice Shop. The man will move one kilometer for each litre of the juice. -----Input----- - The first line will contain the value of N. - The next N lines will contain a pair of integers each, i.e. the amount of juice that a juice shop can provide(in litres) and the distance between that juice shop and the next juice shop. -----Output----- An integer which will be the smallest index of the juice shop from which he can start the tour. -----Constraints----- - 1 ≤ N ≤ 105 - 1 ≤ amt of juice, distance ≤ 109 -----Sample Input----- 3 1 5 10 3 3 4 -----Sample Output----- 1 -----Explanation----- He can start the tour from the SECOND Juice shop. p { text-align:justify }
{"inputs": ["3\n1 5\n10 3\n3 4"], "outputs": ["1"]}
328
25
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an m x n matrix, return true if the matrix is Toeplitz. Otherwise, return false. A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements.   Please complete the following python code precisely: ```python class Solution: def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]]) == True\n assert candidate(matrix = [[1,2],[2,2]]) == False\n\n\ncheck(Solution().isToeplitzMatrix)"}
96
72
coding
Solve the programming task below in a Python markdown code block. Permutation p is an ordered set of integers p_1, p_2, ..., p_{n}, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as p_{i}. We'll call number n the size or the length of permutation p_1, p_2, ..., p_{n}. We'll call position i (1 ≤ i ≤ n) in permutation p_1, p_2, ..., p_{n} good, if |p[i] - i| = 1. Count the number of permutations of size n with exactly k good positions. Print the answer modulo 1000000007 (10^9 + 7). -----Input----- The single line contains two space-separated integers n and k (1 ≤ n ≤ 1000, 0 ≤ k ≤ n). -----Output----- Print the number of permutations of length n with exactly k good positions modulo 1000000007 (10^9 + 7). -----Examples----- Input 1 0 Output 1 Input 2 1 Output 0 Input 3 2 Output 4 Input 4 1 Output 6 Input 7 4 Output 328 -----Note----- The only permutation of size 1 has 0 good positions. Permutation (1, 2) has 0 good positions, and permutation (2, 1) has 2 positions. Permutations of size 3: (1, 2, 3) — 0 positions $(1,3,2)$ — 2 positions $(2,1,3)$ — 2 positions $(2,3,1)$ — 2 positions $(3,1,2)$ — 2 positions (3, 2, 1) — 0 positions
{"inputs": ["1 0\n", "2 1\n", "3 2\n", "4 1\n", "7 4\n", "7 7\n", "8 4\n", "8 5\n"], "outputs": ["1\n", "0\n", "4\n", "6\n", "328\n", "0\n", "2658\n", "688\n"]}
425
93
coding
Solve the programming task below in a Python markdown code block. Pashmak decided to give Parmida a pair of flowers from the garden. There are n flowers in the garden and the i-th of them has a beauty number b_{i}. Parmida is a very strange girl so she doesn't want to have the two most beautiful flowers necessarily. She wants to have those pairs of flowers that their beauty difference is maximal possible! Your task is to write a program which calculates two things: The maximum beauty difference of flowers that Pashmak can give to Parmida. The number of ways that Pashmak can pick the flowers. Two ways are considered different if and only if there is at least one flower that is chosen in the first way and not chosen in the second way. -----Input----- The first line of the input contains n (2 ≤ n ≤ 2·10^5). In the next line there are n space-separated integers b_1, b_2, ..., b_{n} (1 ≤ b_{i} ≤ 10^9). -----Output----- The only line of output should contain two integers. The maximum beauty difference and the number of ways this may happen, respectively. -----Examples----- Input 2 1 2 Output 1 1 Input 3 1 4 5 Output 4 1 Input 5 3 1 2 3 1 Output 2 4 -----Note----- In the third sample the maximum beauty difference is 2 and there are 4 ways to do this: choosing the first and the second flowers; choosing the first and the fifth flowers; choosing the fourth and the second flowers; choosing the fourth and the fifth flowers.
{"inputs": ["2\n1 2\n", "2\n1 1\n", "2\n5 6\n", "2\n3 2\n", "2\n1 1\n", "2\n3 2\n", "2\n5 6\n", "2\n0 1\n"], "outputs": ["1 1", "0 1", "1 1", "1 1", "0 1\n", "1 1\n", "1 1\n", "1 1\n"]}
363
114
coding
Solve the programming task below in a Python markdown code block. You are given a binary string S. In one second, the following scenario happens simultaneously and independently for all the bits which are set to 1 in the string: Change the bit from 1 to 0. If the left neighbour exists and is 0, change it to 1. If the right neighbour exists and is 0, change it to 1. For example, if S = 010 initially, then after 1 second, S = 101 (the 1 bit and both its neighbours were changed). After another second, S = 010. Here, the first and the last bit were changed to 0 because earlier they were 1. The middle bit was changed because it was 0 earlier and it was a neighbour of a 1 bit. Find out the string S after K seconds. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and K — the length of string S and the number of seconds. - The next line describes the string S. ------ Output Format ------ For each test case, output the string S after exactly K seconds. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 10^{5}$ $1 ≤ K ≤ 10^{9}$ - The sum of $N$ over all test cases won't exceed $10^{6}$. $S$ can only contain the characters $0$ or $1$. ----- Sample Input 1 ------ 3 3 1 101 5 2 10001 14 3 10011010111000 ----- Sample Output 1 ------ 010 10101 01100101010101 ----- explanation 1 ------ Test case $1$: The middle bit is changed to $1$ since it had a neighbouring set bit (in this case both left and right) and both the set bits are changed to $0$. Hence, after one second, it is $101$. Test case $2$: After first second, the string $S$ will be $01010$. After another second , the string becomes $10101$.
{"inputs": ["3\n3 1\n101\n5 2\n10001\n14 3\n10011010111000"], "outputs": ["010\n10101\n01100101010101"]}
532
73
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 and a positive integer k. You must choose a subsequence of indices from nums1 of length k. For chosen indices i0, i1, ..., ik - 1, your score is defined as: The sum of the selected elements from nums1 multiplied with the minimum of the selected elements from nums2. It can defined simply as: (nums1[i0] + nums1[i1] +...+ nums1[ik - 1]) * min(nums2[i0] , nums2[i1], ... ,nums2[ik - 1]). Return the maximum possible score. A subsequence of indices of an array is a set that can be derived from the set {0, 1, ..., n-1} by deleting some or no elements.   Please complete the following python code precisely: ```python class Solution: def maxScore(self, nums1: List[int], nums2: List[int], k: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums1 = [1,3,3,2], nums2 = [2,1,3,4], k = 3) == 12\n assert candidate(nums1 = [4,2,3,1,1], nums2 = [7,5,10,9,6], k = 1) == 30\n\n\ncheck(Solution().maxScore)"}
236
98
coding
Solve the programming task below in a Python markdown code block. Cirno_9baka has a paper tape with $n$ cells in a row on it. As he thinks that the blank paper tape is too dull, he wants to paint these cells with $m$ kinds of colors. For some aesthetic reasons, he thinks that the $i$-th color must be used exactly $a_i$ times, and for every $k$ consecutive cells, their colors have to be distinct. Help Cirno_9baka to figure out if there is such a way to paint the cells. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10000$) — the number of test cases. The description of test cases follows. The first line of each test case contains three integers $n$, $m$, $k$ ($1 \leq k \leq n \leq 10^9$, $1 \leq m \leq 10^5$, $m \leq n$). Here $n$ denotes the number of cells, $m$ denotes the number of colors, and $k$ means that for every $k$ consecutive cells, their colors have to be distinct. The second line of each test case contains $m$ integers $a_1, a_2, \cdots , a_m$ ($1 \leq a_i \leq n$) — the numbers of times that colors have to be used. It's guaranteed that $a_1 + a_2 + \ldots + a_m = n$. It is guaranteed that the sum of $m$ over all test cases does not exceed $10^5$. -----Output----- For each test case, output "YES" if there is at least one possible coloring scheme; otherwise, output "NO". You may print each letter in any case (for example, "YES", "Yes", "yes", and "yEs" will all be recognized as positive answers). -----Examples----- Input 2 12 6 2 1 1 1 1 1 7 12 6 2 2 2 2 2 2 2 Output NO YES -----Note----- In the first test case, there is no way to color the cells satisfying all the conditions. In the second test case, we can color the cells as follows: $(1, 2, 1, 2, 3, 4, 3, 4, 5, 6, 5, 6)$. For any $2$ consecutive cells, their colors are distinct.
{"inputs": ["1\n7 3 3\n3 3 1\n", "1\n12 5 5\n3 3 3 2 1\n", "2\n12 6 2\n1 1 1 1 1 7\n12 6 2\n2 2 2 2 2 2\n", "10\n2 1 1\n2\n2 1 1\n2\n1 1 1\n1\n1 1 1\n1\n1 1 1\n1\n2 1 1\n2\n1 1 1\n1\n1 1 1\n1\n2 1 1\n2\n2 1 1\n2\n", "10\n1 1 1\n1\n1 1 1\n1\n2 1 1\n2\n2 1 1\n2\n2 1 1\n2\n2 1 1\n2\n2 1 1\n2\n2 1 1\n2\n1 1 1\n1\n2 1 1\n2\n"], "outputs": ["NO\n", "NO\n", "NO\nYES\n", "YES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\n", "YES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\n"]}
569
313
coding
Solve the programming task below in a Python markdown code block. Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Bitsetbaba Ashiq works in a power grid, which consists of $2^{K}$ power stations (numbered $0$ through $2^{K}-1$). Initially, there are no connections between the power stations. One day, Ashiq's boss gave him $M$ integers $x_{1}, x_{2}, \ldots, x_{M}$ and asked Ashiq to add connections between some power stations. For each two different power stations $u$ and $v$, Ashiq should connect them directly if there is a valid index $i$ such that $u \oplus v = x_{i}$. Here, $\oplus$ denotes the bitwise XOR operation. To confirm whether Ashiq has completed the task correctly, he should report to his boss the number of connected components in the resulting power grid. Two power stations $u$ and $v$ are in the same connected component if they are connected directly or indirectly (through other stations). Ashiq is lazy and he does not want to actually physically perform the task, but he also wants to keep his job. Therefore, he asks for your help. Can you help him find the final number of connected components? ------ 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 two space-separated integers $K$ and $M$. The second line contains $M$ space-separated integers $x_{1}, x_{2}, \ldots, x_{M}$. ------ Output ------ For each test case, print a single line containing one integer — the number of connected components. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ K ≤ 30$ $1 ≤ M ≤ 10^{5}$ $0 ≤ x_{i} < 2^{K}$ for each valid $i$ the sum of $M$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 2 2 1 0 2 1 2 ----- Sample Output 1 ------ 4 2 ----- explanation 1 ------ Example case 1: There are four connected components; each of them contains one of the four nodes. Example case 2: There are two connections — one between nodes $0$ and $2$ and the other between nodes $1$ and $3$. Thus, there are two connected components.
{"inputs": ["2\n2 1\n0\n2 1\n2"], "outputs": ["4\n2"]}
574
26
coding
Solve the programming task below in a Python markdown code block. Watson gives four 3-dimensional points to Sherlock and asks him if they all lie in the same plane. Your task here is to help Sherlock. Input Format First line contains T, the number of testcases. Each test case consists of four lines. Each line contains three integers, denoting x_{i} y_{i} z_{i}. Output Format For each test case, print YES or NO whether all four points lie in same plane or not, respectively. Constraints 1 ≤ T ≤ 10^{4} -10^{3} ≤ x_{i},y_{i},z_{i} ≤ 10^{3} Sample Input 1 1 2 0 2 3 0 4 0 0 0 0 0 Sample Output YES Explanation All points have z_{i} = 0, hence they lie in the same plane, and output is YES
{"inputs": ["1\n1 2 0\n2 3 0\n4 0 0\n0 0 0\n"], "outputs": ["YES\n"]}
213
38
coding
Solve the programming task below in a Python markdown code block. *This section assumes that you understand the basics discussed in HTML Parser - Part 1 .handle_comment(data) This method is called when a comment is encountered (e.g. <!--comment-->). The data argument is the content inside the comment tag: from HTMLParser import HTMLParser class MyHTMLParser(HTMLParser): def handle_comment(self, data): print "Comment :", data .handle_data(data) This method is called to process arbitrary data (e.g. text nodes and the content of <script>...</script> and <style>...</style>). The data argument is the text content of HTML. from HTMLParser import HTMLParser class MyHTMLParser(HTMLParser): def handle_data(self, data): print "Data :", data Task You are given an HTML code snippet of $N$ lines. Your task is to print the single-line comments, multi-line comments and the data. Print the result in the following format: >>> Single-line Comment Comment >>> Data My Data >>> Multi-line Comment Comment_multiline[0] Comment_multiline[1] >>> Data My Data >>> Single-line Comment: Note: Do not print data if data == '\n'. Input Format The first line contains integer $N$, the number of lines in the HTML code snippet. The next $N$ lines contains HTML code. Constraints $0<N<100$ Output Format Print the single-line comments, multi-line comments and the data in order of their occurrence from top to bottom in the snippet. Format the answers as explained in the problem statement. Sample Input 4 <!--[if IE 9]>IE9-specific content <![endif]--> <div> Welcome to HackerRank</div> <!--[if IE 9]>IE9-specific content<![endif]--> Sample Output >>> Multi-line Comment [if IE 9]>IE9-specific content <![endif] >>> Data Welcome to HackerRank >>> Single-line Comment [if IE 9]>IE9-specific content<![endif]
{"inputs": ["4\n<!--[if IE 9]>IE9-specific content\n<![endif]-->\n<div> Welcome to HackerRank</div>\n<!--[if IE 9]>IE9-specific content<![endif]-->\n"], "outputs": [">>> Multi-line Comment\n[if IE 9]>IE9-specific content\n<![endif]\n>>> Data\n Welcome to HackerRank\n>>> Single-line Comment\n[if IE 9]>IE9-specific content<![endif]\n"]}
440
103
coding
Solve the programming task below in a Python markdown code block. > **Note**: This kata is a translation of this (Java) one: http://www.codewars.com/kata/rotate-array. I have not translated this first one as usual because I did not solved it, and I fear not being able to solve it (Java is **not** my cup of... tea). @cjmcgraw, if you want to use my translation on your kata feel free to use it. Create a function named "rotate" that takes an array and returns a new one with the elements inside rotated n spaces. If n is greater than 0 it should rotate the array to the right. If n is less than 0 it should rotate the array to the left. If n is 0, then it should return the array unchanged. Example: ```python data = [1, 2, 3, 4, 5]; rotate(data, 1) # => [5, 1, 2, 3, 4] rotate(data, 2) # => [4, 5, 1, 2, 3] rotate(data, 3) # => [3, 4, 5, 1, 2] rotate(data, 4) # => [2, 3, 4, 5, 1] rotate(data, 5) # => [1, 2, 3, 4, 5] rotate(data, 0) # => [1, 2, 3, 4, 5] rotate(data, -1) # => [2, 3, 4, 5, 1] rotate(data, -2) # => [3, 4, 5, 1, 2] rotate(data, -3) # => [4, 5, 1, 2, 3] rotate(data, -4) # => [5, 1, 2, 3, 4] rotate(data, -5) # => [1, 2, 3, 4, 5] ``` Furthermore the method should take ANY array of objects and perform this operation on them: ```python rotate(['a', 'b', 'c'], 1) # => ['c', 'a', 'b'] rotate([1.0, 2.0, 3.0], 1) # => [3.0, 1.0, 2.0] rotate([True, True, False], 1) # => [False, True, True] ``` Finally the rotation shouldn't be limited by the indices available in the array. Meaning that if we exceed the indices of the array it keeps rotating. Example: ```python data = [1, 2, 3, 4, 5] rotate(data, 7) # => [4, 5, 1, 2, 3] rotate(data, 11) # => [5, 1, 2, 3, 4] rotate(data, 12478) # => [3, 4, 5, 1, 2] ``` Also feel free to reuse/extend the following starter code: ```python def rotate(arr, n): ```
{"functional": "_inputs = [[['a', 'b', 'c'], 1], [[1.0, 2.0, 3.0], 1], [[True, True, False], 1]]\n_outputs = [[['c', 'a', 'b']], [[3.0, 1.0, 2.0]], [[False, True, True]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(rotate(*i), o[0])"}
703
221
coding
Solve the programming task below in a Python markdown code block. Given is a permutation P_1, \ldots, P_N of 1, \ldots, N. Find the number of integers i (1 \leq i \leq N) that satisfy the following condition: - For any integer j (1 \leq j \leq i), P_i \leq P_j. -----Constraints----- - 1 \leq N \leq 2 \times 10^5 - P_1, \ldots, P_N is a permutation of 1, \ldots, N. - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: N P_1 ... P_N -----Output----- Print the number of integers i that satisfy the condition. -----Sample Input----- 5 4 2 5 1 3 -----Sample Output----- 3 i=1, 2, and 4 satisfy the condition, but i=3 does not - for example, P_i > P_j holds for j = 1. Similarly, i=5 does not satisfy the condition, either. Thus, there are three integers that satisfy the condition.
{"inputs": ["1\n0", "1\n1", "1\n1\n", "1\n-1", "1\n-2", "1\n-4", "4\n4 3 2 1", "4\n4 3 2 1\n"], "outputs": ["1\n", "1", "1\n", "1\n", "1\n", "1\n", "4", "4\n"]}
260
93
coding
Solve the programming task below in a Python markdown code block. ## Task: You have to write a function `pattern` which returns the following Pattern(See Examples) upto n number of rows. * Note:```Returning``` the pattern is not the same as ```Printing``` the pattern. ### Rules/Note: * The pattern should be created using only unit digits. * If `n < 1` then it should return "" i.e. empty string. * `The length of each line is same`, and is equal to the number of characters in a line i.e `n`. * Range of Parameters (for the sake of CW Compiler) : + `n ∈ (-50,150]` ### Examples: + pattern(8): 88888888 87777777 87666666 87655555 87654444 87654333 87654322 87654321 + pattern(17): 77777777777777777 76666666666666666 76555555555555555 76544444444444444 76543333333333333 76543222222222222 76543211111111111 76543210000000000 76543210999999999 76543210988888888 76543210987777777 76543210987666666 76543210987655555 76543210987654444 76543210987654333 76543210987654322 76543210987654321 [List of all my katas]("http://www.codewars.com/users/curious_db97/authored") Also feel free to reuse/extend the following starter code: ```python def pattern(n): ```
{"functional": "_inputs = [[1], [2], [3], [7], [17], [27], [0], [-89]]\n_outputs = [['1'], ['22\\n21'], ['333\\n322\\n321'], ['7777777\\n7666666\\n7655555\\n7654444\\n7654333\\n7654322\\n7654321'], ['77777777777777777\\n76666666666666666\\n76555555555555555\\n76544444444444444\\n76543333333333333\\n76543222222222222\\n76543211111111111\\n76543210000000000\\n76543210999999999\\n76543210988888888\\n76543210987777777\\n76543210987666666\\n76543210987655555\\n76543210987654444\\n76543210987654333\\n76543210987654322\\n76543210987654321'], ['777777777777777777777777777\\n766666666666666666666666666\\n765555555555555555555555555\\n765444444444444444444444444\\n765433333333333333333333333\\n765432222222222222222222222\\n765432111111111111111111111\\n765432100000000000000000000\\n765432109999999999999999999\\n765432109888888888888888888\\n765432109877777777777777777\\n765432109876666666666666666\\n765432109876555555555555555\\n765432109876544444444444444\\n765432109876543333333333333\\n765432109876543222222222222\\n765432109876543211111111111\\n765432109876543210000000000\\n765432109876543210999999999\\n765432109876543210988888888\\n765432109876543210987777777\\n765432109876543210987666666\\n765432109876543210987655555\\n765432109876543210987654444\\n765432109876543210987654333\\n765432109876543210987654322\\n765432109876543210987654321'], [''], ['']]\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(pattern(*i), o[0])"}
637
1,373
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 matrix grid consisting of positive integers. Perform the following operation until grid becomes empty: Delete the element with the greatest value from each row. If multiple such elements exist, delete any of them. Add the maximum of deleted elements to the answer. Note that the number of columns decreases by one after each operation. Return the answer after performing the operations described above.   Please complete the following python code precisely: ```python class Solution: def deleteGreatestValue(self, grid: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(grid = [[1,2,4],[3,3,1]]) == 8\n assert candidate(grid = [[10]]) == 10\n\n\ncheck(Solution().deleteGreatestValue)"}
129
57
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an array of integers nums and an integer k, return the number of contiguous subarrays where the product of all the elements in the subarray is strictly less than k.   Please complete the following python code precisely: ```python class Solution: def numSubarrayProductLessThanK(self, nums: List[int], k: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [10,5,2,6], k = 100) == 8\n assert candidate(nums = [1,2,3], k = 0) == 0\n\n\ncheck(Solution().numSubarrayProductLessThanK)"}
92
71
coding
Solve the programming task below in a Python markdown code block. You are given a string S of length 2 or 3 consisting of lowercase English letters. If the length of the string is 2, print it as is; if the length is 3, print the string after reversing it. Constraints * The length of S is 2 or 3. * S consists of lowercase English letters. Input Input is given from Standard Input in the following format: S Output If the length of S is 2, print S as is; if the length is 3, print S after reversing it. Examples Input abc Output cba Input ac Output ac
{"inputs": ["bc", "cb", "cc", "dc", "cd", "ec", "fc", "cf"], "outputs": ["bc\n", "cb\n", "cc\n", "dc\n", "cd\n", "ec\n", "fc\n", "cf\n"]}
144
62
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]. The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. You must write an algorithm that runs in O(n) time and without using the division operation.   Please complete the following python code precisely: ```python class Solution: def productExceptSelf(self, nums: List[int]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(nums = [1,2,3,4]) == [24,12,8,6]\n assert candidate(nums = [-1,1,0,-3,3]) == [0,0,9,0,0]\n\n\ncheck(Solution().productExceptSelf)"}
122
76
coding
Solve the programming task below in a Python markdown code block. Every day we can send from the server a certain limit of e-mails. Task: Write a function that will return the integer number of e-mails sent in the percentage of the limit. Example: ``` limit - 1000; emails sent - 101; return - 10%; // becouse integer from 10,1 = 10 ``` Arguments: Integer, limit; Integer, number of e-mails sent today; When: the argument ```$sent = 0```, then return the message: "No e-mails sent"; the argument ```$sent >= $limit```, then return the message: "Daily limit is reached"; the argument ```$limit is empty```, then default ```$limit = 1000``` emails; Good luck! Also feel free to reuse/extend the following starter code: ```python def get_percentage(sent, limit=1000): ```
{"functional": "_inputs = [[101, 1000], [256, 500], [259], [0], [1000, 1000]]\n_outputs = [['10%'], ['51%'], ['25%'], ['No e-mails sent'], ['Daily limit is reached']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(get_percentage(*i), o[0])"}
209
216
coding
Solve the programming task below in a Python markdown code block. Alice and Bob are decorating a Christmas Tree. Alice wants only $3$ types of ornaments to be used on the Christmas Tree: yellow, blue and red. They have $y$ yellow ornaments, $b$ blue ornaments and $r$ red ornaments. In Bob's opinion, a Christmas Tree will be beautiful if: the number of blue ornaments used is greater by exactly $1$ than the number of yellow ornaments, and the number of red ornaments used is greater by exactly $1$ than the number of blue ornaments. That is, if they have $8$ yellow ornaments, $13$ blue ornaments and $9$ red ornaments, we can choose $4$ yellow, $5$ blue and $6$ red ornaments ($5=4+1$ and $6=5+1$). Alice wants to choose as many ornaments as possible, but she also wants the Christmas Tree to be beautiful according to Bob's opinion. In the example two paragraphs above, we would choose $7$ yellow, $8$ blue and $9$ red ornaments. If we do it, we will use $7+8+9=24$ ornaments. That is the maximum number. Since Alice and Bob are busy with preparing food to the New Year's Eve, they are asking you to find out the maximum number of ornaments that can be used in their beautiful Christmas Tree! It is guaranteed that it is possible to choose at least $6$ ($1+2+3=6$) ornaments. -----Input----- The only line contains three integers $y$, $b$, $r$ ($1 \leq y \leq 100$, $2 \leq b \leq 100$, $3 \leq r \leq 100$) — the number of yellow, blue and red ornaments. It is guaranteed that it is possible to choose at least $6$ ($1+2+3=6$) ornaments. -----Output----- Print one number — the maximum number of ornaments that can be used. -----Examples----- Input 8 13 9 Output 24 Input 13 3 6 Output 9 -----Note----- In the first example, the answer is $7+8+9=24$. In the second example, the answer is $2+3+4=9$.
{"inputs": ["1 2 3\n", "9 5 5\n", "1 3 4\n", "6 5 7\n", "1 5 4\n", "3 2 4\n", "3 3 5\n", "3 2 3\n"], "outputs": ["6", "12", "6", "15", "6", "6", "9", "6"]}
513
96
coding
Solve the programming task below in a Python markdown code block. There are $n$ students standing in a circle in some order. The index of the $i$-th student is $p_i$. It is guaranteed that all indices of students are distinct integers from $1$ to $n$ (i. e. they form a permutation). Students want to start a round dance. A clockwise round dance can be started if the student $2$ comes right after the student $1$ in clockwise order (there are no students between them), the student $3$ comes right after the student $2$ in clockwise order, and so on, and the student $n$ comes right after the student $n - 1$ in clockwise order. A counterclockwise round dance is almost the same thing — the only difference is that the student $i$ should be right after the student $i - 1$ in counterclockwise order (this condition should be met for every $i$ from $2$ to $n$). For example, if the indices of students listed in clockwise order are $[2, 3, 4, 5, 1]$, then they can start a clockwise round dance. If the students have indices $[3, 2, 1, 4]$ in clockwise order, then they can start a counterclockwise round dance. Your task is to determine whether it is possible to start a round dance. Note that the students cannot change their positions before starting the dance; they cannot swap or leave the circle, and no other student can enter the circle. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 200$) — the number of queries. Then $q$ queries follow. The first line of the query contains one integer $n$ ($1 \le n \le 200$) — the number of students. The second line of the query contains a permutation of indices $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$), where $p_i$ is the index of the $i$-th student (in clockwise order). It is guaranteed that all $p_i$ are distinct integers from $1$ to $n$ (i. e. they form a permutation). -----Output----- For each query, print the answer on it. If a round dance can be started with the given order of students, print "YES". Otherwise print "NO". -----Example----- Input 5 4 1 2 3 4 3 1 3 2 5 1 2 3 5 4 1 1 5 3 2 1 5 4 Output YES YES NO YES YES
{"inputs": ["1\n11\n11 2 3 4 5 6 7 8 9 10 1\n", "1\n11\n11 2 1 4 5 6 7 8 9 10 1\n", "1\n11\n11 2 1 4 5 6 7 9 9 10 1\n", "1\n11\n11 2 3 4 5 6 7 4 9 10 1\n", "1\n11\n11 2 3 4 5 6 7 13 9 10 1\n", "1\n11\n11 2 3 4 5 6 7 13 5 10 1\n", "1\n11\n11 2 3 6 5 6 7 13 5 10 1\n", "1\n11\n11 2 6 6 5 6 7 13 5 10 1\n"], "outputs": ["NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n"]}
605
290
coding
Solve the programming task below in a Python markdown code block. Given is a positive integer N. Find the number of pairs (A, B) of positive integers not greater than N that satisfy the following condition: - When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B. -----Constraints----- - 1 \leq N \leq 2 \times 10^5 - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: N -----Output----- Print the answer. -----Sample Input----- 25 -----Sample Output----- 17 The following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22), (3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21), (21,12), (22,2), and (22,22).
{"inputs": ["0", "6", "9", "2", "3", "7", "5", "8"], "outputs": ["0\n", "6\n", "9\n", "2\n", "3\n", "7\n", "5\n", "8\n"]}
262
62
coding
Solve the programming task below in a Python markdown code block. There are N sweets in the store. The cost of the i^{th} sweet is A_{i} rupees. Chef is a regular customer, so after buying the i^{th} sweet, he gets a cashback of B_{i} rupees. Chef has R rupees. He is fond of all the sweets, so he wants you to calculate the maximum number of sweets he can buy. Note that he can buy the same type of sweet multiple times, as long as he has the money to do so. ------ Input Format ------ - The first line of input will contain T, the number of test cases. - Each test case consists of three lines of input. - The first line of each test case contains two space-separated integers N and R — the number of sweets in the shop and the amount of money Chef has. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains N space-separated integers B_{1}, B_{2}, \ldots, B_{N}. ------ Output Format ------ For each query, print on a new line the maximum number of sweets Chef can buy. ------ Constraints ------ $1 ≤T ≤2 \cdot 10^{5}$ $1 ≤N ≤2 \cdot 10^{5}$ $1 ≤B_{i} < A_{i} ≤10^{9}$ $1 ≤R ≤ 10^{9}$ - It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 3 3 3 2 3 4 1 1 1 2 4 5 4 1 2 4 10 4 4 5 5 1 2 4 2 ----- Sample Output 1 ------ 2 1 7 ----- explanation 1 ------ Test case $1$: Chef buys the first sweet, which costs $2$ rupees and has a cashback of $1$ rupee. He now has $3 - 2 + 1 = 2$ rupees remaining. He once again buys the first sweet, which leaves him with $1$ rupee, at which point no more sweets can be bought. Test case $2$: Chef buys the second sweet once.
{"inputs": ["3\n3 3\n2 3 4\n1 1 1\n2 4\n5 4\n1 2\n4 10\n4 4 5 5\n1 2 4 2"], "outputs": ["2\n1\n7\n"]}
526
66
coding
Solve the programming task below in a Python markdown code block. Chef has a binary tree. The binary tree consists of 1 or more nodes. Each node has a unique integer id. Each node has up to 2 children, which are identified by their ids, and each node is the child of at most 1 other node. A node X is considered to be an ancestor of node Y if node Y is a child of node X or if there is some node Z for which X is an ancestor of Z and Y is a child of Z. No node is an ancestor of itself. A special node called the root node is an ancestor of all other nodes. Chef has forgotten which node of his tree is the root, and wants you to help him to figure it out. Unfortunately, Chef's knowledge of the tree is incomplete. He does not remember the ids of the children of each node, but only remembers the sum of the ids of the children of each node. ------ Input ------ Input begins with an integer T, the number of test cases. Each test case begins with an integer N, the number of nodes in the tree. N lines follow with 2 integers each: the id of a node, and the sum of the ids of its children. The second number will be 0 if the node has no children. ------ Output ------ For each test case, output on a line a space separated list of all possible values for the id of the root node in increasing order. It is guaranteed that at least one such id exists for each test case. ------ Constraints ------ $1 ≤ T ≤ 50$ $1 ≤ N ≤ 30$ $All node ids are between 1 and 1000, inclusive$ ------ Sample Input ------ 2 1 4 0 6 1 5 2 0 3 0 4 0 5 5 6 5 ------ Sample Output ------ 4 6 ------ Explanation ------ In the first sample test case, there is only one node, which is clearly the root. In the second test case, there are two non-isomorphic trees that satisfy the constraints, as seen in the following picture: 6 6 \ / \ 5 1 4 / \ \ 1 4 5 / \ / \ 2 3 2 3
{"inputs": ["2\n1\n4 0\n6\n1 5\n2 0\n3 0\n4 0\n5 5\n6 5", "2\n1\n4 0\n6\n1 8\n2 0\n3 0\n4 0\n5 5\n6 5", "2\n1\n4 0\n6\n0 8\n2 0\n3 0\n4 0\n5 5\n6 5", "2\n1\n4 0\n6\n1 8\n2 0\n3 0\n4 0\n5 5\n6 4", "2\n1\n4 0\n6\n1 8\n2 0\n3 0\n4 0\n5 5\n6 7", "2\n1\n0 0\n6\n1 8\n2 0\n3 0\n4 0\n5 5\n6 4", "2\n1\n4 0\n6\n1 5\n2 1\n3 0\n4 0\n5 5\n6 5", "2\n1\n1 0\n6\n1 8\n2 0\n3 0\n4 0\n5 5\n6 4"], "outputs": ["4\n6", "4\n3\n", "4\n2\n", "4\n4\n", "4\n1\n", "0\n4\n", "4\n5\n", "1\n4\n"]}
497
333
coding
Solve the programming task below in a Python markdown code block. On an $8 \times 8$ grid, some horizontal rows have been painted red, and some vertical columns have been painted blue, in some order. The stripes are drawn sequentially, one after the other. When the stripe is drawn, it repaints all the cells through which it passes. Determine which color was used last. The red stripe was painted after the blue one, so the answer is R. -----Input----- The first line of the input contains a single integer $t$ ($1 \leq t \leq 4000$) — the number of test cases. The description of test cases follows. There is an empty line before each test case. Each test case consists of $8$ lines, each containing $8$ characters. Each of these characters is either 'R', 'B', or '.', denoting a red square, a blue square, and an unpainted square, respectively. It is guaranteed that the given field is obtained from a colorless one by drawing horizontal red rows and vertical blue columns. At least one stripe is painted. -----Output----- For each test case, output 'R' if a red stripe was painted last, and 'B' if a blue stripe was painted last (without quotes). -----Examples----- Input 4 ....B... ....B... ....B... RRRRRRRR ....B... ....B... ....B... ....B... RRRRRRRB B......B B......B B......B B......B B......B B......B RRRRRRRB RRRRRRBB .B.B..BB RRRRRRBB .B.B..BB .B.B..BB RRRRRRBB .B.B..BB .B.B..BB ........ ........ ........ RRRRRRRR ........ ........ ........ ........ Output R B B R -----Note----- The first test case is pictured in the statement. In the second test case, the first blue column is painted first, then the first and last red rows, and finally the last blue column. Since a blue stripe is painted last, the answer is B.
{"inputs": ["4\n\n....B...\n....B...\n....B...\nRRRRRRRR\n....B...\n....B...\n....B...\n....B...\n\nRRRRRRRB\nB......B\nB......B\nB......B\nB......B\nB......B\nB......B\nRRRRRRRB\n\nRRRRRRBB\n.B.B..BB\nRRRRRRBB\n.B.B..BB\n.B.B..BB\nRRRRRRBB\n.B.B..BB\n.B.B..BB\n\n........\n........\n........\nRRRRRRRR\n........\n........\n........\n........\n"], "outputs": ["R\nB\nB\nR\n"]}
459
157
coding
Solve the programming task below in a Python markdown code block. There are N slimes standing on a number line. The i-th slime from the left is at position x_i. It is guaruanteed that 1 \leq x_1 < x_2 < \ldots < x_N \leq 10^{9}. Niwango will perform N-1 operations. The i-th operation consists of the following procedures: * Choose an integer k between 1 and N-i (inclusive) with equal probability. * Move the k-th slime from the left, to the position of the neighboring slime to the right. * Fuse the two slimes at the same position into one slime. Find the total distance traveled by the slimes multiplied by (N-1)! (we can show that this value is an integer), modulo (10^{9}+7). If a slime is born by a fuse and that slime moves, we count it as just one slime. Constraints * 2 \leq N \leq 10^{5} * 1 \leq x_1 < x_2 < \ldots < x_N \leq 10^{9} * x_i is an integer. Input Input is given from Standard Input in the following format: N x_1 x_2 \ldots x_N Output Print the answer. Examples Input 3 1 2 3 Output 5 Input 12 161735902 211047202 430302156 450968417 628894325 707723857 731963982 822804784 880895728 923078537 971407775 982631932 Output 750927044
{"inputs": ["3\n1 2 5", "3\n1 3 5", "3\n1 3 3", "3\n1 0 3", "3\n1 2 3", "3\n1 2 10", "3\n1 3 10", "3\n0 2 10"], "outputs": ["11\n", "10\n", "4\n", "7\n", "5", "26\n", "25\n", "28\n"]}
442
117
coding
Solve the programming task below in a Python markdown code block. Polycarp has a cat and his cat is a real gourmet! Dependent on a day of the week he eats certain type of food: on Mondays, Thursdays and Sundays he eats fish food; on Tuesdays and Saturdays he eats rabbit stew; on other days of week he eats chicken stake. Polycarp plans to go on a trip and already packed his backpack. His backpack contains: $a$ daily rations of fish food; $b$ daily rations of rabbit stew; $c$ daily rations of chicken stakes. Polycarp has to choose such day of the week to start his trip that his cat can eat without additional food purchases as long as possible. Print the maximum number of days the cat can eat in a trip without additional food purchases, if Polycarp chooses the day of the week to start his trip optimally. -----Input----- The first line of the input contains three positive integers $a$, $b$ and $c$ ($1 \le a, b, c \le 7\cdot10^8$) — the number of daily rations of fish food, rabbit stew and chicken stakes in Polycarps backpack correspondingly. -----Output----- Print the maximum number of days the cat can eat in a trip without additional food purchases, if Polycarp chooses the day of the week to start his trip optimally. -----Examples----- Input 2 1 1 Output 4 Input 3 2 2 Output 7 Input 1 100 1 Output 3 Input 30 20 10 Output 39 -----Note----- In the first example the best day for start of the trip is Sunday. In this case, during Sunday and Monday the cat will eat fish food, during Tuesday — rabbit stew and during Wednesday — chicken stake. So, after four days of the trip all food will be eaten. In the second example Polycarp can start his trip in any day of the week. In any case there are food supplies only for one week in Polycarps backpack. In the third example Polycarp can start his trip in any day, excluding Wednesday, Saturday and Sunday. In this case, the cat will eat three different dishes in three days. Nevertheless that after three days of a trip there will be $99$ portions of rabbit stew in a backpack, can cannot eat anything in fourth day of a trip.
{"inputs": ["2 1 1\n", "3 2 2\n", "4 1 2\n", "2 8 9\n", "3 1 4\n", "3 2 1\n", "5 4 3\n", "1 1 2\n"], "outputs": ["4\n", "7\n", "6\n", "6\n", "6\n", "6\n", "12\n", "4\n"]}
525
103
coding
Solve the programming task below in a Python markdown code block. There are $n$ positive integers $a_1, a_2, \dots, a_n$. For the one move you can choose any even value $c$ and divide by two all elements that equal $c$. For example, if $a=[6,8,12,6,3,12]$ and you choose $c=6$, and $a$ is transformed into $a=[3,8,12,3,3,12]$ after the move. You need to find the minimal number of moves for transforming $a$ to an array of only odd integers (each element shouldn't be divisible by $2$). -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. Then $t$ test cases follow. The first line of a test case contains $n$ ($1 \le n \le 2\cdot10^5$) — the number of integers in the sequence $a$. The second line contains positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$. -----Output----- For $t$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $2$). -----Example----- Input 4 6 40 6 40 3 20 1 1 1024 4 2 4 8 16 3 3 1 7 Output 4 10 4 0 -----Note----- In the first test case of the example, the optimal sequence of moves can be as follows: before making moves $a=[40, 6, 40, 3, 20, 1]$; choose $c=6$; now $a=[40, 3, 40, 3, 20, 1]$; choose $c=40$; now $a=[20, 3, 20, 3, 20, 1]$; choose $c=20$; now $a=[10, 3, 10, 3, 10, 1]$; choose $c=10$; now $a=[5, 3, 5, 3, 5, 1]$ — all numbers are odd. Thus, all numbers became odd after $4$ moves. In $3$ or fewer moves, you cannot make them all odd.
{"inputs": ["4\n6\n40 6 40 3 20 1\n1\n1024\n4\n2 4 8 16\n3\n3 1 7\n"], "outputs": ["4\n10\n4\n0\n"]}
633
64
coding
Solve the programming task below in a Python markdown code block. When Mr. Kay was browsing a certain SNS as usual, the problem that "there are people who can solve IQ150 or more" came to the timeline. Mr. Kay has an IQ of over 150, so he solved the problem in an instant without even looking at it. For him, he doesn't have to work on such a mystery. It is enough to leave it to the computer. problem The following mysterious mathematical formula was written in the problem. * \\ (5 + 3 = 28 \\) * \\ (9 + 1 = 810 \\) * \\ (8 + 6 = 214 \\) * \\ (5 + 4 = 19 \\) * \\ (2 + 2 = 4 \\) * \\ (15 + 8 = 723 \\) * \\ (7 + 9 = -216 \\) * \\ (3 + 0 = 33 \\) When thinking about the above operator \\ (+ \\), for a positive integer \\ (a \\) \\ (x \ geq 0, y \ geq 0 \\) and \\ (x + y =) Find the number of integer pairs \\ (x, y \\) such that a \\). input A positive integer \\ (a \\) is given on one line. output Output the number of pairs \\ ((x, y) \\) that satisfy \\ (a = x + y, x \ geq 0, y \ geq 0 \\) on one line. Constraint * \\ (1 \ leq a \ leq 10 ^ 9 (= 1000000000) \\) Input / output example Input 1 19 Output 1 1 There is one way of \\ (5 + 4 \\). Input 2 twenty two Output 2 2 There are two types: \\ (11 + 11 \\) and \\ (2 + 0 \\). Input 3 1 Output 3 0 \\ (1 + 0 = 11, 0 + 1 = -11 \\). \\ (1 \\) cannot be generated. Input 4 101 Output 4 0 Note that \\ (1 --0 \\) is not \\ (101 \\). Input 5 660233276 Output 5 Four Example Input 19 Output 1
{"inputs": ["5", "2", "7", "1", "4", "8", "3", "6"], "outputs": ["0\n", "1\n", "0\n", "0\n", "1\n", "1\n", "0\n", "1\n"]}
549
62
coding
Solve the programming task below in a Python markdown code block. # Task Write a function that accepts `msg` string and returns local tops of string from the highest to the lowest. The string's tops are from displaying the string in the below way: ``` 7891012 TUWvXY 6 3 ABCDE S Z 5 lmno z F R 1 4 abc k p v G Q 2 3 .34..9 d...j q....x H.....P 3......2 125678 efghi rstuwy IJKLMNO 45678901 ``` The next top is always 1 character higher than the previous one. For the above example, the solution for the `123456789abcdefghijklmnopqrstuwyxvzABCDEFGHIJKLMNOPQRSTUWvXYZ123456789012345678910123` input string is `7891012TUWvXYABCDElmnoabc34`. - When the `msg` string is empty, return an empty string. - The input strings may be very long. Make sure your solution has good performance. - The (.)dots on the sample dispaly of string are only there to help you to understand the pattern Check the test cases for more samples. # Series - [String tops](https://www.codewars.com/kata/59b7571bbf10a48c75000070) - [Square string tops](https://www.codewars.com/kata/5aa3e2b0373c2e4b420009af) Also feel free to reuse/extend the following starter code: ```python def tops(msg): ```
{"functional": "_inputs = [[''], ['abcde'], ['123456789abcdefghijklmnopqrstuwyxvzABCDEFGHIJKLMNOPQRSTU'], ['123456789abcdefghijklmnopqrstuwyxvzABCDEFGHIJKLMNOPQRSTUWvXYZ123456789012345678910123']]\n_outputs = [[''], ['cd'], ['TUABCDElmnoabc34'], ['7891012TUWvXYABCDElmnoabc34']]\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(tops(*i), o[0])"}
433
256
coding
Solve the programming task below in a Python markdown code block. $Riggi$ is a spy of $KBI$ and he is on a secret mission, he is spying an Underworld Don $Anit$. $Riggi$ is spying him since 5 years and he is not able to find any evidence against $Anit$. $KBI$ told $Riggi$ they will send him a code string in a special format that will inform him whether he has continue or return back from mission. According to $KBI$ firstly he has to find what is the original string and then he has to check string is palindrome or not . If its a palindrome then he has to leave the mission and return back else continue spying $Anit$. Rules to find original string : 1:-If in Code string any integer(N) followed by a string of alphabets that starts with '+' and ends with '-' then he has to repeat that string N times , like 3+acg- = acgacgacg. 2:-If there is no integer present before string then print the string is repeated 1 time. like bc=bc. Example of conversion from Code string to original string : 2+ac-3+kb-j=acackbkbkbj -----Input:----- - Code string $S$ is taken as input . -----Output:----- - Print string $Continue$ if code string $S$ is not a palindrome else print $Return$ . -----Constraints----- - $1 \leq S \leq 1000$ - $1 \leq N \leq 1000$ -----Sample Input 1:----- 3+xy-bb3+yx- -----Sample Output 1:----- Return -----Sample Input 2:----- 3+xy-bb3+xy- -----Sample Output 2:----- Continue -----EXPLANATION:----- Sample 1:- original string will be xyxyxybbyxyxyx which is a palindrome hence print $Return$. Sample 2:- original string will be xyxyxybbxyxyxy which is not a palindrome hence print $Continue$.
{"inputs": ["3+xy-bb3+yx-", "3+xy-bb3+xy-"], "outputs": ["Return", "Continue"]}
467
33
coding
Solve the programming task below in a Python markdown code block. Rupsa really loves triangles. One day she came across an equilateral triangle having length of each side as an integer N. She started wondering if it was possible to transform the triangle keeping two sides fixed and alter the third side such that it still remains a triangle, but the altered side will have its length as an even integer, and the line drawn from the opposite vertex to the mid-point of the altered side is of integral length. Since Rupsa is in a hurry to record a song for Chef as he really loves her songs, you must help her solve the problem as fast as possible. -----Input----- The first line of input contains an integer T denoting the number of test cases. Each test-case contains a single integer N. -----Output----- For each test case, output "YES" if the triangle transformation is possible, otherwise "NO" (quotes for clarity only, do not output). -----Constraints----- - 1 ≤ T ≤ 106 - 1 ≤ N ≤ 5 x 106 -----Sub tasks----- - Subtask #1: 1 ≤ T ≤ 100, 1 ≤ N ≤ 104 (10 points) - Subtask #2: 1 ≤ T ≤ 104, 1 ≤ N ≤ 106 (30 points) - Subtask #3: Original Constraints (60 points) -----Example----- Input:2 5 3 Output:YES NO -----Explanation----- - In test case 1, make the length of any one side 6, and it will suffice.
{"inputs": ["2\n5\n3"], "outputs": ["YES\nNO"]}
342
18
coding
Solve the programming task below in a Python markdown code block. Kostya likes Codeforces contests very much. However, he is very disappointed that his solutions are frequently hacked. That's why he decided to obfuscate (intentionally make less readable) his code before upcoming contest. To obfuscate the code, Kostya first looks at the first variable name used in his program and replaces all its occurrences with a single symbol a, then he looks at the second variable name that has not been replaced yet, and replaces all its occurrences with b, and so on. Kostya is well-mannered, so he doesn't use any one-letter names before obfuscation. Moreover, there are at most 26 unique identifiers in his programs. You are given a list of identifiers of some program with removed spaces and line breaks. Check if this program can be a result of Kostya's obfuscation. -----Input----- In the only line of input there is a string S of lowercase English letters (1 ≤ |S| ≤ 500) — the identifiers of a program with removed whitespace characters. -----Output----- If this program can be a result of Kostya's obfuscation, print "YES" (without quotes), otherwise print "NO". -----Examples----- Input abacaba Output YES Input jinotega Output NO -----Note----- In the first sample case, one possible list of identifiers would be "number string number character number string number". Here how Kostya would obfuscate the program: replace all occurences of number with a, the result would be "a string a character a string a", replace all occurences of string with b, the result would be "a b a character a b a", replace all occurences of character with c, the result would be "a b a c a b a", all identifiers have been replaced, thus the obfuscation is finished.
{"inputs": ["a\n", "z\n", "b\n", "x\n", "c\n", "z\n", "c\n", "b\n"], "outputs": ["YES\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n"]}
408
70
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a m x n matrix grid which is sorted in non-increasing order both row-wise and column-wise, return the number of negative numbers in grid.   Please complete the following python code precisely: ```python class Solution: def countNegatives(self, grid: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]) == 8\n assert candidate(grid = [[3,2],[1,0]]) == 0\n\n\ncheck(Solution().countNegatives)"}
81
80
coding
Solve the programming task below in a Python markdown code block. problem Once upon a time there were settlements and many people lived there. People built buildings of various shapes and sizes. But those buildings have already been lost, only the literature and the pillars found in the ruins. Was a clue to the location of the building. There is a description of the temple in the literature. The temple was exactly square when viewed from above, and there were pillars at its four corners. It is unknown which direction the temple was facing. Also, on the sides and inside. I don't know if there were any pillars. Archaeologists believed that among the pillars found in the ruins, the one with the largest square area must be the temple. Since the coordinates of the position of the pillar are given, find the square with the largest area among the four pillars and write a program to output the area. Note that the sides of the square are not always parallel to the coordinate axes. Be careful. input The input consists of multiple datasets. Each dataset is given in the following format. The input ends on a line containing one zero. On the first line, the number n of pillars found in the ruins is written. In each of the n lines from the 2nd line to the n + 1st line, the x and y coordinates of the column are written separated by blanks. A pillar never appears more than once. n is an integer that satisfies 1 ≤ n ≤ 3000, and the x and y coordinates of the column are integers greater than or equal to 0 and less than or equal to 5000. Of the scoring data, 30% of the points satisfy 1 ≤ n ≤ 100, and 60% of the points satisfy 1 ≤ n ≤ 500. The number of datasets does not exceed 10. output Outputs one integer for each dataset. If there is a square with four columns, it outputs the area of ​​the largest of those squares, and if no such square exists. Output 0. Examples Input 10 9 4 4 3 1 1 4 2 2 4 5 8 4 0 5 3 0 5 5 2 10 9 4 4 3 1 1 4 2 2 4 5 8 4 0 5 3 0 5 5 2 0 Output 10 10 Input None Output None
{"inputs": ["10\n9 4\n4 3\n1 1\n4 2\n2 4\n5 8\n4 0\n5 3\n0 5\n5 2\n10\n2 4\n4 3\n1 1\n4 2\n2 4\n5 8\n4 0\n5 3\n0 5\n5 2\n0", "10\n9 4\n4 3\n1 1\n4 2\n2 4\n5 8\n4 1\n5 3\n0 5\n5 2\n10\n2 4\n4 3\n1 1\n4 2\n2 4\n5 8\n4 0\n5 3\n0 5\n5 2\n0", "10\n9 4\n4 4\n1 1\n4 2\n2 5\n5 8\n4 1\n5 3\n0 5\n5 2\n10\n2 4\n4 3\n1 1\n4 2\n2 4\n5 8\n4 0\n5 3\n0 5\n3 2\n0", "10\n9 4\n4 3\n1 1\n4 2\n2 5\n8 8\n4 1\n5 3\n0 5\n5 2\n10\n2 3\n4 3\n1 1\n4 2\n3 4\n5 8\n4 0\n5 3\n0 5\n3 2\n0", "10\n9 4\n4 3\n1 0\n4 2\n2 5\n8 8\n4 1\n5 3\n0 5\n5 0\n10\n2 3\n4 3\n1 1\n4 2\n3 4\n5 8\n4 0\n5 3\n0 5\n3 2\n0", "10\n9 1\n4 6\n1 1\n4 3\n2 4\n5 8\n4 2\n7 3\n0 5\n5 2\n10\n2 0\n4 3\n1 1\n4 2\n2 4\n5 8\n4 0\n5 3\n1 5\n5 2\n0", "10\n9 1\n4 6\n1 1\n4 3\n2 4\n5 8\n4 2\n7 3\n0 5\n5 2\n10\n2 0\n4 3\n1 1\n4 2\n2 4\n5 8\n4 1\n5 3\n1 5\n5 2\n0", "10\n9 4\n4 3\n1 1\n4 2\n2 5\n5 8\n4 1\n5 3\n0 5\n5 2\n10\n2 4\n4 3\n1 1\n4 2\n2 4\n5 8\n4 0\n5 3\n0 5\n5 2\n0"], "outputs": ["10\n10\n", "1\n10\n", "0\n10\n", "1\n2\n", "0\n2\n", "17\n10\n", "17\n1\n", "1\n10\n"]}
535
774
coding
Solve the programming task below in a Python markdown code block. Sakuzyo - Imprinting A.R.C. Markland-N is a tall building with $n$ floors numbered from $1$ to $n$. Between each two adjacent floors in the building, there is a staircase connecting them. It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal. ConneR's office is at floor $s$ of the building. On each floor (including floor $s$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $k$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there. CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant. Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way! -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases in the test. Then the descriptions of $t$ test cases follow. The first line of a test case contains three integers $n$, $s$ and $k$ ($2 \le n \le 10^9$, $1 \le s \le n$, $1 \le k \le \min(n-1, 1000)$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $k$ distinct integers $a_1, a_2, \ldots, a_k$ ($1 \le a_i \le n$) — the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $k$ over all test cases does not exceed $1000$. -----Output----- For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $s$ to a floor with an open restaurant. -----Example----- Input 5 5 2 3 1 2 3 4 3 3 4 1 2 10 2 6 1 2 3 4 5 7 2 1 1 2 100 76 8 76 75 36 67 41 74 10 77 Output 2 0 4 0 2 -----Note----- In the first example test case, the nearest floor with an open restaurant would be the floor $4$. In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere. In the third example test case, the closest open restaurant is on the $6$-th floor.
{"inputs": ["1\n10 1 1\n1\n", "1\n10 1 1\n1\n", "1\n10 9 2\n8 2\n", "1\n10 9 2\n8 2\n", "1\n110 8 1\n32\n", "1\n100 32 1\n32\n", "1\n100 32 1\n32\n", "1\n100 55 1\n32\n"], "outputs": ["1\n", "1\n", "0\n", "0\n", "0\n", "1\n", "1\n", "0\n"]}
660
157
coding
Solve the programming task below in a Python markdown code block. ##Background - the Collatz Conjecture: Imagine you are given a positive integer, `n`, then: * if `n` is even, calculate: `n / 2` * if `n` is odd, calculate: `3 * n + 1` Repeat until your answer is `1`. The Collatz conjecture states that performing this operation repeatedly, you will always eventually reach `1`. You can try creating Collatz sequences with [this](http://www.codewars.com/kata/5286b2e162056fd0cb000c20) kata. For further information, see the [wiki page](https://en.wikipedia.org/wiki/Collatz_conjecture). ##Now! Your task: **Given an array of positive integers, return the integer whose Collatz sequence is the longest.** Example: ```python longest_collatz([2, 4, 3])==3 ``` Explanation: The Collatz sequence for `2` has a length of `1`, the sequence for `4` has a length of `2`, and the sequence for `3` has a length of `7`. So from our array, the integer `3` is the one with the longest Collatz sequence. Hence, your function should return `3`. ##Note: There may be more than one answer, i.e. two or more integers produce the longest Collatz sequence, because they happen to have sequences of the same length. **In this case, your function should return the integer that appears first in the array.** Example: Given an array: `[2, 5, 32]`, both `5` and `32` have Collatz sequences of length 5. These are also the longest sequences from our array. In this case, our function returns `5`, because `5` comes before `32` in our array. Also feel free to reuse/extend the following starter code: ```python def longest_collatz(input_array): ```
{"functional": "_inputs = [[[1, 5, 27, 4]], [[64, 64, 27, 64]], [[75, 226, 113, 340]], [[340, 113, 226, 75]], [[75, 113, 226, 75]]]\n_outputs = [[27], [27], [75], [75], [75]]\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(longest_collatz(*i), o[0])"}
436
255
coding
Solve the programming task below in a Python markdown code block. The following problem is well-known: given integers n and m, calculate $2^{n} \operatorname{mod} m$, where 2^{n} = 2·2·...·2 (n factors), and $x \operatorname{mod} y$ denotes the remainder of division of x by y. You are asked to solve the "reverse" problem. Given integers n and m, calculate $m \operatorname{mod} 2^{n}$. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^8). The second line contains a single integer m (1 ≤ m ≤ 10^8). -----Output----- Output a single integer — the value of $m \operatorname{mod} 2^{n}$. -----Examples----- Input 4 42 Output 10 Input 1 58 Output 0 Input 98765432 23456789 Output 23456789 -----Note----- In the first example, the remainder of division of 42 by 2^4 = 16 is equal to 10. In the second example, 58 is divisible by 2^1 = 2 without remainder, and the answer is 0.
{"inputs": ["1\n1\n", "1\n2\n", "2\n1\n", "2\n2\n", "2\n3\n", "2\n4\n", "2\n5\n", "3\n8\n"], "outputs": ["1\n", "0\n", "1\n", "2\n", "3\n", "0\n", "1\n", "0\n"]}
298
86
coding
Solve the programming task below in a Python markdown code block. There were $n$ types of swords in the theater basement which had been used during the plays. Moreover there were exactly $x$ swords of each type. $y$ people have broken into the theater basement and each of them has taken exactly $z$ swords of some single type. Note that different people might have taken different types of swords. Note that the values $x, y$ and $z$ are unknown for you. The next morning the director of the theater discovers the loss. He counts all swords — exactly $a_i$ swords of the $i$-th type are left untouched. The director has no clue about the initial number of swords of each type in the basement, the number of people who have broken into the basement and how many swords each of them have taken. For example, if $n=3$, $a = [3, 12, 6]$ then one of the possible situations is $x=12$, $y=5$ and $z=3$. Then the first three people took swords of the first type and the other two people took swords of the third type. Note that you don't know values $x, y$ and $z$ beforehand but know values of $n$ and $a$. Thus he seeks for your help. Determine the minimum number of people $y$, which could have broken into the theater basement, and the number of swords $z$ each of them has taken. -----Input----- The first line of the input contains one integer $n$ $(2 \le n \le 2 \cdot 10^{5})$ — the number of types of swords. The second line of the input contains the sequence $a_1, a_2, \dots, a_n$ $(0 \le a_i \le 10^{9})$, where $a_i$ equals to the number of swords of the $i$-th type, which have remained in the basement after the theft. It is guaranteed that there exists at least one such pair of indices $(j, k)$ that $a_j \neq a_k$. -----Output----- Print two integers $y$ and $z$ — the minimum number of people which could have broken into the basement and the number of swords each of them has taken. -----Examples----- Input 3 3 12 6 Output 5 3 Input 2 2 9 Output 1 7 Input 7 2 1000000000 4 6 8 4 2 Output 2999999987 2 Input 6 13 52 0 13 26 52 Output 12 13 -----Note----- In the first example the minimum value of $y$ equals to $5$, i.e. the minimum number of people who could have broken into the basement, is $5$. Each of them has taken $3$ swords: three of them have taken $3$ swords of the first type, and two others have taken $3$ swords of the third type. In the second example the minimum value of $y$ is $1$, i.e. the minimum number of people who could have broken into the basement, equals to $1$. He has taken $7$ swords of the first type.
{"inputs": ["2\n2 9\n", "2\n1 9\n", "2\n2 9\n", "2\n1 15\n", "2\n0 15\n", "2\n0 13\n", "3\n3 8 6\n", "3\n3 8 0\n"], "outputs": ["1 7\n", "1 8\n", "1 7\n", "1 14\n", "1 15\n", "1 13\n", "7 1\n", "13 1\n"]}
717
129
coding
Solve the programming task below in a Python markdown code block. The season for Snuke Festival has come again this year. First of all, Ringo will perform a ritual to summon Snuke. For the ritual, he needs an altar, which consists of three parts, one in each of the three categories: upper, middle and lower. He has N parts for each of the three categories. The size of the i-th upper part is A_i, the size of the i-th middle part is B_i, and the size of the i-th lower part is C_i. To build an altar, the size of the middle part must be strictly greater than that of the upper part, and the size of the lower part must be strictly greater than that of the middle part. On the other hand, any three parts that satisfy these conditions can be combined to form an altar. How many different altars can Ringo build? Here, two altars are considered different when at least one of the three parts used is different. -----Constraints----- - 1 \leq N \leq 10^5 - 1 \leq A_i \leq 10^9(1\leq i\leq N) - 1 \leq B_i \leq 10^9(1\leq i\leq N) - 1 \leq C_i \leq 10^9(1\leq i\leq N) - All input values are integers. -----Input----- Input is given from Standard Input in the following format: N A_1 ... A_N B_1 ... B_N C_1 ... C_N -----Output----- Print the number of different altars that Ringo can build. -----Sample Input----- 2 1 5 2 4 3 6 -----Sample Output----- 3 The following three altars can be built: - Upper: 1-st part, Middle: 1-st part, Lower: 1-st part - Upper: 1-st part, Middle: 1-st part, Lower: 2-nd part - Upper: 1-st part, Middle: 2-nd part, Lower: 2-nd part
{"inputs": ["2\n1 5\n2 4\n3 6\n", "3\n1 1 1\n2 2 2\n3 3 3\n", "6\n3 14 159 2 6 53\n58 9 79 323 84 6\n2643 383 2 79 50 288\n"], "outputs": ["3\n", "27\n", "87\n"]}
470
116
coding
Solve the programming task below in a Python markdown code block. Chef has a new customer and he wants to prepare his order as soon as possible. While preparing, he sees that the mint sauce is finished. He has to run upstairs to get it from other kitchen. Chef is currently on the X^{th} stair. He has to climb all the way up to the Y^{th} stair in minimum number of steps. In one step, Chef can do one of the following things: Climb a single stair. ( i.e go from the X^{th} stair to the (X+1)^{th} stair. ) Climb two stairs only if the final stair falls at a prime number position. ( i.e. go from the X^{th} stair to the (X+2)^{th} stair, only if (X + 2) is a prime number) Help Chef reach the Y^{th} stair from the X^{th} stair in minimum number of steps. \textbf{See Explanation for more clarity.} Note: The input files are large. The use of Fast I/O is recommended. ------ Input Format ------ - The first line contains an integer T denoting the number of test cases. The T test cases then follow. - The first line of each test case contains X and Y denoting the starting stair and ending stair respectively. ------ Output Format ------ - Output a single integer representing minimum number of steps chef can take to reach from X^{th} to Y^{th} stair. ------ Constraints ------ $1 ≤ T ≤ 10^{6}$ $1 ≤ X < Y ≤ 10^{7}$ ----- Sample Input 1 ------ 4 2 10 5 12 34 43 57 63 ----- Sample Output 1 ------ 6 5 6 4 ----- explanation 1 ------ Test case $1$: Chef starts from $2^{nd}$, goes to $3^{rd}$ stair, then to $5^{th}$ stair as $5$ or $(3+2)$ is prime number. Now, from $5^{th}$ stair, Chef goes to $7^{th}$ stair as $7$ or $(5+2)$ is a prime number, then Chef goes to $8^{th}$ stair, then to $9^{th}$ stair and finally to $10^{th}$ stair. This approach takes a total of $6$ steps which is the minimum possible number of steps Chef can take to reach the $10^{th}$ stair starting from the $2^{nd}$ stair. Test case $3$: Starting from the $34^{th}$ stair, Chef moves through the stairs as following. $34$ → $35$ → $37$ → $38$ → $39$ → $41$ → $43$.
{"inputs": ["4\n2 10\n5 12\n34 43\n57 63"], "outputs": ["6\n5\n6\n4"]}
611
40
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left and right child subtrees. A binary tree X is flip equivalent to a binary tree Y if and only if we can make X equal to Y after some number of flip operations. Given the roots of two binary trees root1 and root2, return true if the two trees are flip equivalent or false otherwise.   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 flipEquiv(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(root1 = tree_node([1,2,3,4,5,6,None,None,None,7,8]), root2 = tree_node([1,3,2,None,6,4,5,None,None,None,None,8,7])) == True\n\n\ncheck(Solution().flipEquiv)"}
195
78
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. Chef has array A consisting of N integers. He calls a sub-array of the array good if the bitwise OR of all the elements in it is greater or equal than number K. Please help him to find out how many sub-arrays of array A are good! ------ Input ------ The first line of input contains T - number of test cases. T test cases follow. For each test case, first line contains an integer N - number of elements in array, and integer K. Second line contains N space separated non-negative integers denoting array A. ------ Output ------ For each test case, output an integer corresponding to the number of good sub-arrays of A in separate line. ------ Constraints ------ 1 ≤ T, N, ≤ 100,000 0 ≤ A_{i}, K ≤ 10^{9} 1 ≤ Sum over all values of N in the input in a single test file ≤ 100,000 ----- Sample Input 1 ------ 2 3 3 1 2 3 3 6 3 4 5 ----- Sample Output 1 ------ 4 2
{"inputs": ["2\n3 3\n1 2 3\n3 6\n3 4 5"], "outputs": ["4\n2"]}
267
34
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a personal information string s, representing either an email address or a phone number. Return the masked personal information using the below rules. Email address: An email address is: A name consisting of uppercase and lowercase English letters, followed by The '@' symbol, followed by The domain consisting of uppercase and lowercase English letters with a dot '.' somewhere in the middle (not the first or last character). To mask an email: The uppercase letters in the name and domain must be converted to lowercase letters. The middle letters of the name (i.e., all but the first and last letters) must be replaced by 5 asterisks "*****". Phone number: A phone number is formatted as follows: The phone number contains 10-13 digits. The last 10 digits make up the local number. The remaining 0-3 digits, in the beginning, make up the country code. Separation characters from the set {'+', '-', '(', ')', ' '} separate the above digits in some way. To mask a phone number: Remove all separation characters. The masked phone number should have the form: "***-***-XXXX" if the country code has 0 digits. "+*-***-***-XXXX" if the country code has 1 digit. "+**-***-***-XXXX" if the country code has 2 digits. "+***-***-***-XXXX" if the country code has 3 digits. "XXXX" is the last 4 digits of the local number.   Please complete the following python code precisely: ```python class Solution: def maskPII(self, s: str) -> str: ```
{"functional": "def check(candidate):\n assert candidate(s = \"LeetCode@LeetCode.com\") == \"l*****e@leetcode.com\"\n assert candidate(s = \"AB@qq.com\") == \"a*****b@qq.com\"\n assert candidate(s = \"1(234)567-890\") == \"***-***-7890\"\n\n\ncheck(Solution().maskPII)"}
360
94
coding
Solve the programming task below in a Python markdown code block. There are n boxes with colored balls on the table. Colors are numbered from 1 to n. i-th box contains a_{i} balls, all of which have color i. You have to write a program that will divide all balls into sets such that: each ball belongs to exactly one of the sets, there are no empty sets, there is no set containing two (or more) balls of different colors (each set contains only balls of one color), there are no two sets such that the difference between their sizes is greater than 1. Print the minimum possible number of sets. -----Input----- The first line contains one integer number n (1 ≤ n ≤ 500). The second line contains n integer numbers a_1, a_2, ... , a_{n} (1 ≤ a_{i} ≤ 10^9). -----Output----- Print one integer number — the minimum possible number of sets. -----Examples----- Input 3 4 7 8 Output 5 Input 2 2 7 Output 4 -----Note----- In the first example the balls can be divided into sets like that: one set with 4 balls of the first color, two sets with 3 and 4 balls, respectively, of the second color, and two sets with 4 balls of the third color.
{"inputs": ["1\n1\n", "1\n1\n", "2\n2 7\n", "2\n9 6\n", "2\n9 6\n", "2\n9 1\n", "2\n2 3\n", "2\n3 5\n"], "outputs": ["1\n", "1\n", "4\n", "5\n", "5\n", "6\n", "2\n", "3\n"]}
294
98
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Write a method to compute all permutations of a string whose characters are not necessarily unique. The list of permutations should not have duplicates. Please complete the following python code precisely: ```python class Solution: def permutation(self, S: str) -> List[str]: ```
{"functional": "def check(candidate):\n assert candidate(S = \"qqe\") == [\"eqq\",\"qeq\",\"qqe\"]\n assert candidate(S = \"ab\") == [\"ab\", \"ba\"]\n\n\ncheck(Solution().permutation)"}
72
60
coding
Solve the programming task below in a Python markdown code block. You love array, don't you? You are given two integer N and M, along with an array B of length N containing pairwise distinct integers. You have to find the number of array A of length N satisfying the follow conditions: 1. 1 ≤ A_{i} ≤ M. 2. A_{i} \neq A_{j} for all 1 ≤ i < j ≤ N. 3. A_{i} \neq B_{i} for all 1 ≤ i ≤ N. Since the answer can be very large, output it under modulo 10^{9} + 7. ------ Input Format ------ - The first line of the input contains two space-separated integers N and M. - The second line of the input contains N separated integers B_{1}, B_{2}, \dots, B_{N} - the array B. ------ Output Format ------ Output on a single line the number of satisfactory array modulo 10^{9} + 7. ------ Constraints ------ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ M ≤ 3 \cdot 10^{5}$ $1 ≤ B_{i} ≤ 10^{9}$ $B_{i} \neq B_{j}$ for all $1 ≤i < j ≤N$. ------ subtasks ------ Subtask $1$ (30 points): $1 ≤ R ≤ 10\,000$ Subtask $2$ (70 points): $1 ≤ R ≤ 10^{9}$ ----- Sample Input 1 ------ 3 4 5 1 2 ----- Sample Output 1 ------ 14 ----- explanation 1 ------ All satisfactory arrays are: - $[1, 2, 3]$ - $[2, 3, 1]$ - $[3, 2, 1]$ - $[1, 2, 4]$ - $[2, 4, 1]$ - $[4, 2, 1]$ - $[1, 3, 4]$ - $[1, 4, 3]$ - $[3, 4, 1]$ - $[4, 3, 1]$ - $[2, 3, 4]$ - $[2, 4, 3]$ - $[3, 2, 4]$ - $[4, 2, 3]$ ----- Sample Input 2 ------ 2 2 1 3 ----- Sample Output 2 ------ 1 ----- explanation 2 ------ The only satisfactory array is: - $[2, 1]$
{"inputs": ["2 2\n1 3", "3 4\n5 1 2"], "outputs": ["1", "14"]}
594
33
coding
Solve the programming task below in a Python markdown code block. The professor is conducting a course on Discrete Mathematics to a class of N students. He is angry at the lack of their discipline, and he decides to cancel the class if there are fewer than K students present after the class starts. Given the arrival time of each student, your task is to find out if the class gets cancelled or not. INPUT The first line of the input contains T, the number of test cases. Each test case contains two lines. The first line of each test case contains two space-separated integers, N and K. The next line contains N space-separated integers, a1,a2,…,aN, representing the arrival time of each student. If the arrival time of a given student is a non-positive integer (ai≤0), then the student enters before the class starts. If the arrival time of a given student is a positive integer (ai>0), the student enters after the class has started. OUTPUT For each testcase, print "YES" (without quotes) if the class gets cancelled and "NO" (without quotes) otherwise. CONSTRAINT 1≤T≤10 1≤N≤1000 1≤N≤ −100≤ai≤100,where i∈[1,N] NOTE If a student enters the class exactly when it starts (ai=0), the student is considered to have entered before the class has started. SAMPLE INPUT 2 4 3 -1 -3 4 2 4 2 0 -1 2 1 SAMPLE OUTPUT YES NO Explanation For the first test case, K=3, i.e., the professor wants at least 3 students to be in class but there are only 2 who have arrived on time (−3 and −1), hence the class gets cancelled. For the second test case, K=2, i.e, the professor wants at least 2 students to be in class and there are 2 who have arrived on time (0 and −1), hence the class does not get cancelled.
{"inputs": ["2\n4 3\n-1 -3 4 2\n4 2\n0 -1 2 1", "10\n10 4\n-93 -86 49 -62 -90 -63 40 72 11 67\n10 10\n23 -35 -2 58 -67 -56 -42 -73 -19 37\n10 9\n13 91 56 -62 96 -5 -84 -36 -46 -13\n10 8\n45 67 64 -50 -8 78 84 -51 99 60\n10 7\n26 94 -95 34 67 -97 17 52 1 86\n10 2\n19 29 -17 -71 -75 -27 -56 -53 65 83\n10 10\n-32 -3 -70 8 -40 -96 -18 -46 -21 -79\n10 9\n-50 0 64 14 -56 -91 -65 -36 51 -28\n10 6\n-58 -29 -35 -18 43 -28 -76 43 -13 6\n10 1\n88 -17 -96 43 83 99 25 90 -39 86"], "outputs": ["YES\nNO", "NO\nYES\nYES\nYES\nYES\nNO\nYES\nYES\nNO\nNO"]}
440
412
coding
Solve the programming task below in a Python markdown code block. To become a millionaire, M-kun has decided to make money by trading in the next N days. Currently, he has 1000 yen and no stocks - only one kind of stock is issued in the country where he lives. He is famous across the country for his ability to foresee the future. He already knows that the price of one stock in the next N days will be as follows: * A_1 yen on the 1-st day, A_2 yen on the 2-nd day, ..., A_N yen on the N-th day. In the i-th day, M-kun can make the following trade any number of times (possibly zero), within the amount of money and stocks that he has at the time. * Buy stock: Pay A_i yen and receive one stock. * Sell stock: Sell one stock for A_i yen. What is the maximum possible amount of money that M-kun can have in the end by trading optimally? Constraints * 2 \leq N \leq 80 * 100 \leq A_i \leq 200 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N Output Print the maximum possible amount of money that M-kun can have in the end, as an integer. Examples Input 7 100 130 130 130 115 115 150 Output 1685 Input 6 200 180 160 140 120 100 Output 1000 Input 2 157 193 Output 1216
{"inputs": ["2\n1 2", "2\n2 3", "2\n2 5", "2\n4 5", "2\n2 7", "2\n7 8", "2\n2 6", "2\n3 5"], "outputs": ["2000\n", "1500\n", "2500\n", "1250\n", "3500\n", "1142\n", "3000\n", "1666\n"]}
398
118
coding
Solve the programming task below in a Python markdown code block. Two boys decided to compete in text typing on the site "Key races". During the competition, they have to type a text consisting of s characters. The first participant types one character in v_1 milliseconds and has ping t_1 milliseconds. The second participant types one character in v_2 milliseconds and has ping t_2 milliseconds. If connection ping (delay) is t milliseconds, the competition passes for a participant as follows: Exactly after t milliseconds after the start of the competition the participant receives the text to be entered. Right after that he starts to type it. Exactly t milliseconds after he ends typing all the text, the site receives information about it. The winner is the participant whose information on the success comes earlier. If the information comes from both participants at the same time, it is considered that there is a draw. Given the length of the text and the information about participants, determine the result of the game. -----Input----- The first line contains five integers s, v_1, v_2, t_1, t_2 (1 ≤ s, v_1, v_2, t_1, t_2 ≤ 1000) — the number of characters in the text, the time of typing one character for the first participant, the time of typing one character for the the second participant, the ping of the first participant and the ping of the second participant. -----Output----- If the first participant wins, print "First". If the second participant wins, print "Second". In case of a draw print "Friendship". -----Examples----- Input 5 1 2 1 2 Output First Input 3 3 1 1 1 Output Second Input 4 5 3 1 5 Output Friendship -----Note----- In the first example, information on the success of the first participant comes in 7 milliseconds, of the second participant — in 14 milliseconds. So, the first wins. In the second example, information on the success of the first participant comes in 11 milliseconds, of the second participant — in 5 milliseconds. So, the second wins. In the third example, information on the success of the first participant comes in 22 milliseconds, of the second participant — in 22 milliseconds. So, it is be a draw.
{"inputs": ["5 1 2 1 2\n", "3 3 1 1 1\n", "4 5 3 1 5\n", "1 1 1 1 1\n", "8 8 1 1 1\n", "1 2 8 8 5\n", "1 2 8 9 8\n", "2 9 8 8 9\n"], "outputs": ["First\n", "Second\n", "Friendship\n", "Friendship\n", "Second\n", "Friendship\n", "First\n", "Friendship\n"]}
506
138
coding
Solve the programming task below in a Python markdown code block. This is a simple game you must have played around with during your school days, calculating FLAMES of you and your crush! Given the names of two people, cancel out the common letters (repeated occurrence of a letter is treated separately, so 2A's in one name and one A in the other would cancel one A in each name), count the total number of remaining letters (n) and repeatedly cut the letter in the word FLAMES which hits at the nth number when we count from F in cyclic manner. For example: NAME 1: SHILPA NAME 2: AAMIR After cutting the common letters: NAME 1: SHILPA NAME 2: AAMIR Total number of letters left=7 FLAMES, start counting from F : 1=F, 2=L, 3=A, 4=M, 5=E, 6=S,7=F...So cut F FLAMES: repeat this process with remaining letters of FLAMES for number 7 (start count from the letter after the last letter cut) . In the end, one letter remains. Print the result corresponding to the last letter: F=FRIENDS L=LOVE A=ADORE M=MARRIAGE E=ENEMIES S=SISTER -----Input----- The no. of test cases (<100) two names (may include spaces) for each test case. -----Output----- FLAMES result (Friends/Love/...etc) for each test case -----Example----- Input: 2 SHILPA AAMIR MATT DENISE Output: ENEMIES LOVE By: Chintan, Asad, Ashayam, Akanksha
{"inputs": ["2\nSHILPA\nAAMIR\nMATT\nDENISE\n\n\n"], "outputs": ["ENEMIES\nLOVE"]}
376
34
coding
Solve the programming task below in a Python markdown code block. The *fuel economy* of a car is the distance which it can travel on one litre of fuel. The base fuel economy (i.e, its fuel economy when there is only one person - the driver - in the car) of a certain car is M kilometres per litre. It was also observed that every extra passenger in the car *decreases* the fuel economy by 1 kilometre per litre. P people want to take this car for a journey. They know that the car currently has V litres of fuel in its tank. What is the maximum distance this car can travel under the given conditions, assuming that all P people always stay in the car and no refuelling can be done? Note that among the P people is also a driver, i.e, there are exactly P people in the car. ------ 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 P, M, and V - the number of people, base fuel economy, and amount of fuel present in the car, respectively. ------ Output Format ------ For each test case, output a single line containing one integer - the maximum distance that the car can travel. ------ Constraints ------ $1 ≤ T ≤ 3 \cdot 10^{4}$ $1 ≤ P ≤ 5$ $6 ≤ M ≤ 56$ $1 ≤ V ≤ 100$ ----- Sample Input 1 ------ 3 5 10 20 1 6 10 4 6 1 ----- Sample Output 1 ------ 120 60 3 ----- explanation 1 ------ Test Case $1$: There are $5$ people in the car, and its base fuel economy is $10$. Thus, its effective fuel economy is $10 - 4 = 6$, since there are $4$ people present other than the driver. There are $20$ litres of fuel in the car, so the maximum distance that can be travelled is $6\cdot 20 = 120$. Test Case $2$: The effective fuel economy is $6$, and the maximum distance that can be travelled is $6\cdot 10 = 60$. Test Case $3$: The effective fuel economy is $3$, and the maximum distance that can be travelled is $3\cdot 1 = 3$.
{"inputs": ["3\n5 10 20\n1 6 10\n4 6 1"], "outputs": ["120\n60\n3"]}
542
40
coding
Solve the programming task below in a Python markdown code block. You have been appointed as the designer for your school's computer network. In total, there are N computers in the class, and M computer-to-computer connections need to be made. Also, there are three mandatory conditions the design should fulfill. The first requirement is that any computer in the network should be able to communicate with any other computer through the connections, possibly, through some other computers. Network attacks are possible, so the second requirement is that even if any one computer from the network gets disabled so that the rest of the computers are unable to communicate with it, the rest of the computers can still communicate with each other. In other words, the first requirement still holds for any subset of (N-1) computers. The third requirement is that there shouldn't be any irrelevant connections in the network. We will call a connection irrelevant if and only if after its' removal, the above two requirements are still held. Given N, M, please build a network with N computers and M connections, or state that it is impossible. -----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 first and only line of each test case contains a pair of space-separated integers N and M denoting the number of computers and the number of connections. -----Output----- Output T blocks. If it is impossible to construct a network with the given parameters for the corresponding test case, output just -1 -1. Otherwise, output M lines, each of which contains a space-separated pairs of integers denoting the IDs of the computers that should be connected. Note that multiple connections between any pair of computers and connections connecting a computer to itself are implicitly not allowed due to the third requirement. -----Constraints----- - 1 ≤ T ≤ 1000 - 1 ≤ M ≤ N * (N - 1) / 2 - 1 ≤ Sum of all N ≤ 1000 - Subtask 1 (21 point): 1 ≤ N ≤ 4 - Subtask 2 (79 points): 1 ≤ N ≤ 100 -----Example----- Input:2 10 1 5 5 Output:-1 -1 1 2 2 3 3 4 4 5 5 1 -----Explanation----- Example case 1. There are not enough connections even to satisfy the first requirement. Example case 2. The obtained network satisfies all the requirements.
{"inputs": ["2\n10 1\n5 5"], "outputs": ["-1 -1\n1 2\n2 3\n3 4\n4 5\n5 1"]}
528
44
coding
Solve the programming task below in a Python markdown code block. For a given polygon g, print "1" if g is a convex polygon, "0" otherwise. Here, in a convex polygon, all interior angles are less than or equal to 180 degrees. g is represented by a sequence of points p1, p2,..., pn where line segments connecting pi and pi+1 (1 ≤ i ≤ n-1) are sides of the polygon. The line segment connecting pn and p1 is also a side of the polygon. Constraints * 3 ≤ n ≤ 100 * -10000 ≤ xi, yi ≤ 10000 * No point of the polygon will occur more than once. * Two sides of the polygon can intersect only at a common endpoint. Input g is given by coordinates of the points p1,..., pn in the following format: n x1 y1 x2 y2 : xn yn The first integer n is the number of points. The coordinate of a point pi is given by two integers xi and yi. The coordinates of points are given in the order of counter-clockwise visit of them. Output Print "1" or "0" in a line. Examples Input 4 0 0 3 1 2 3 0 3 Output 1 Input 5 0 0 2 0 1 1 2 2 0 2 Output 0
{"inputs": ["4\n0 0\n3 0\n2 1\n0 3", "4\n0 0\n3 1\n2 1\n0 3", "4\n0 0\n3 0\n2 0\n0 3", "4\n0 0\n3 1\n2 0\n0 3", "4\n0 0\n3 1\n2 1\n0 2", "4\n0 0\n3 1\n2 3\n0 3", "4\n0 0\n3 1\n2 1\n-1 2", "4\n0 0\n3 1\n2 1\n-1 4"], "outputs": ["1\n", "0\n", "0\n", "0\n", "0\n", "1", "0\n", "0\n"]}
316
191
coding
Solve the programming task below in a Python markdown code block. You will be given a string (x) featuring a cat 'C' and a mouse 'm'. The rest of the string will be made up of '.'. You need to find out if the cat can catch the mouse from it's current position. The cat can jump over three characters. So: C.....m returns 'Escaped!' <-- more than three characters between C...m returns 'Caught!' <-- as there are three characters between the two, the cat can jump. Also feel free to reuse/extend the following starter code: ```python def cat_mouse(x): ```
{"functional": "_inputs = [['C....m'], ['C..m'], ['C.....m'], ['C.m'], ['m...C']]\n_outputs = [['Escaped!'], ['Caught!'], ['Escaped!'], ['Caught!'], ['Caught!']]\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(cat_mouse(*i), o[0])"}
135
194
coding
Solve the programming task below in a Python markdown code block. A country has coins with denominations ```python coins_list = d1 < d2 < · · · < dn. ``` You want to make change for n cents, using the smallest number of coins. ```python # Example 1: U.S. coins d1 = 1 d2 = 5 d3 = 10 d4 = 25 ## Optimal change for 37 cents – 1 quarter, 1 dime, 2 pennies. # Example 2: Alien Planet Z coins Z_coin_a = 1 Z_coin_b = 3 Z_coin_c = 4 ## Optimal change for 6 cents - 2 Z_coin_b's ``` Write a function that will take a list of coin denominations and a desired amount and provide the least amount of coins needed. Also feel free to reuse/extend the following starter code: ```python def loose_change(coins_list, amount_of_change): ```
{"functional": "_inputs = [[[1, 5, 10, 25], 37], [[1, 3, 4], 6], [[25, 5, 10, 1, 21], 63], [[1, 4, 5, 10], 8], [[1, 2, 5, 10, 20, 50, 100, 200], 93]]\n_outputs = [[4], [2], [3], [2], [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(loose_change(*i), o[0])"}
215
267
coding
Solve the programming task below in a Python markdown code block. Your goal is to implement the method **meanVsMedian** which accepts an *odd-length* array of integers and returns one of the following: * 'mean' - in case **mean** value is **larger than** median value * 'median' - in case **median** value is **larger than** mean value * 'same' - in case both mean and median share the **same value** Reminder: [Median](https://en.wikipedia.org/wiki/Median) Array will always be valid (odd-length >= 3) Also feel free to reuse/extend the following starter code: ```python def mean_vs_median(numbers): ```
{"functional": "_inputs = [[[1, 1, 1]], [[1, 2, 37]], [[7, 14, -70]], [[-10, 20, 5]]]\n_outputs = [['same'], ['mean'], ['median'], ['same']]\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(mean_vs_median(*i), o[0])"}
147
203
coding
Solve the programming task below in a Python markdown code block. For two permutations p and q of the integers from 1 through N, let f(p,q) be the permutation that satisfies the following: * The p_i-th element (1 \leq i \leq N) in f(p,q) is q_i. Here, p_i and q_i respectively denote the i-th element in p and q. You are given two permutations p and q of the integers from 1 through N. We will now define a sequence {a_n} of permutations of the integers from 1 through N, as follows: * a_1=p, a_2=q * a_{n+2}=f(a_n,a_{n+1}) ( n \geq 1 ) Given a positive integer K, find a_K. Constraints * 1 \leq N \leq 10^5 * 1 \leq K \leq 10^9 * p and q are permutations of the integers from 1 through N. Input Input is given from Standard Input in the following format: N K p_1 ... p_N q_1 ... q_N Output Print N integers, with spaces in between. The i-th integer (1 \leq i \leq N) should be the i-th element in a_K. Examples Input 3 3 1 2 3 3 2 1 Output 3 2 1 Input 5 5 4 5 1 2 3 3 2 1 5 4 Output 4 3 2 1 5 Input 10 1000000000 7 10 6 5 4 2 9 1 3 8 4 1 9 2 3 7 8 10 6 5 Output 7 9 4 8 2 5 1 6 10 3
{"inputs": ["3 1\n1 2 3\n3 2 1", "3 6\n1 2 3\n3 2 1", "3 9\n2 1 3\n3 2 1", "3 4\n1 2 3\n3 2 1", "3 2\n1 2 3\n3 2 1", "3 9\n1 2 3\n3 2 1", "3 8\n1 2 3\n3 2 1", "3 7\n1 2 3\n3 2 1"], "outputs": ["1 2 3", "3 2 1", "2 3 1", "1 2 3", "3 2 1", "3 2 1", "3 2 1", "1 2 3"]}
433
198
coding
Solve the programming task below in a Python markdown code block. You are given a number $k$ and a string $s$ of length $n$, consisting of the characters '.' and '*'. You want to replace some of the '*' characters with 'x' characters so that the following conditions are met: The first character '*' in the original string should be replaced with 'x'; The last character '*' in the original string should be replaced with 'x'; The distance between two neighboring replaced characters 'x' must not exceed $k$ (more formally, if you replaced characters at positions $i$ and $j$ ($i < j$) and at positions $[i+1, j-1]$ there is no "x" symbol, then $j-i$ must be no more than $k$). For example, if $n=7$, $s=$.**.*** and $k=3$, then the following strings will satisfy the conditions above: .xx.*xx; .x*.x*x; .xx.xxx. But, for example, the following strings will not meet the conditions: .**.*xx (the first character '*' should be replaced with 'x'); .x*.xx* (the last character '*' should be replaced with 'x'); .x*.*xx (the distance between characters at positions $2$ and $6$ is greater than $k=3$). Given $n$, $k$, and $s$, find the minimum number of '*' characters that must be replaced with 'x' in order to meet the above conditions. -----Input----- The first line contains one integer $t$ ($1 \le t \le 500$). Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le k \le n \le 50$). The second line of each test case contains a string $s$ of length $n$, consisting of the characters '.' and '*'. It is guaranteed that there is at least one '*' in the string $s$. It is guaranteed that the distance between any two neighboring '*' characters does not exceed $k$. -----Output----- For each test case output the minimum number of '*' characters that must be replaced with 'x' characters in order to satisfy the conditions above. -----Examples----- Input 5 7 3 .**.*** 5 1 ..*.. 5 2 *.*.* 3 2 *.* 1 1 * Output 3 1 3 2 1 -----Note----- None
{"inputs": ["5\n7 3\n.**.***\n5 1\n..*..\n5 2\n*.*.*\n3 2\n*.*\n1 1\n*\n", "5\n7 3\n.**.***\n5 2\n..*..\n5 2\n*.*.*\n3 2\n*.*\n1 1\n*\n", "5\n7 5\n.**.***\n5 1\n..*..\n5 2\n*.*.*\n3 2\n*.*\n1 1\n*\n", "5\n7 3\n***..**\n5 1\n*....\n5 2\n*.*.*\n3 2\n*.*\n1 1\n*\n", "5\n7 4\n.****.*\n5 2\n..*..\n5 4\n*.*.*\n3 2\n*.*\n1 2\n*\n", "5\n7 5\n.**.***\n5 2\n..*..\n5 4\n*.*.*\n3 2\n*.*\n1 1\n*\n", "5\n7 2\n*.****.\n5 2\n..*..\n5 4\n*.*.*\n3 2\n**.\n1 3\n*\n", "5\n7 3\n.****.*\n5 1\n..*..\n5 2\n*.*.*\n3 2\n*.*\n1 1\n*\n"], "outputs": ["3\n1\n3\n2\n1\n", "3\n1\n3\n2\n1\n", "2\n1\n3\n2\n1\n", "4\n1\n3\n2\n1\n", "3\n1\n2\n2\n1\n", "2\n1\n2\n2\n1\n", "4\n1\n2\n2\n1\n", "3\n1\n3\n2\n1\n"]}
531
456
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given two groups of points where the first group has size1 points, the second group has size2 points, and size1 >= size2. The cost of the connection between any two points are given in an size1 x size2 matrix where cost[i][j] is the cost of connecting point i of the first group and point j of the second group. The groups are connected if each point in both groups is connected to one or more points in the opposite group. In other words, each point in the first group must be connected to at least one point in the second group, and each point in the second group must be connected to at least one point in the first group. Return the minimum cost it takes to connect the two groups.   Please complete the following python code precisely: ```python class Solution: def connectTwoGroups(self, cost: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(cost = [[15, 96], [36, 2]]) == 17\n assert candidate(cost = [[1, 3, 5], [4, 1, 1], [1, 5, 3]]) == 4\n assert candidate(cost = [[2, 5, 1], [3, 4, 7], [8, 1, 2], [6, 2, 4], [3, 8, 8]]) == 10\n\n\ncheck(Solution().connectTwoGroups)"}
202
136
coding
Solve the programming task below in a Python markdown code block. JJ has three integers A, B, and N. He can apply the following operation on A: Select an integer X such that 1 ≤ X < N and set A := A \oplus X. (Here, \oplus denotes the [bitwise XOR operation].) JJ wants to make A equal to B. Determine the minimum number of operations required to do so. Print -1 if it is not possible. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first and only line of each test case contains three integers A, B, and N — the parameters mentioned in the statement. ------ Output Format ------ For each test case, output the minimum number of operations required to make A equal to B. Output -1 if it is not possible to do so. ------ Constraints ------ $1 ≤ T ≤ 1000$ $0 ≤ A, B < 2^{30}$ $1 ≤ N ≤2^{30}$ ------ subtasks ------ Subtask 1 (30 points): $N$ is a power of $2$. Subtask 2 (70 points): Original Constraints. ----- Sample Input 1 ------ 3 5 5 2 3 7 8 8 11 1 ----- Sample Output 1 ------ 0 1 -1 ----- explanation 1 ------ - Test Case $1$: $A$ is already equal to $B$. Hence we do not need to perform any operation. - Test Case $2$: We can perform the operation with $X = 4$ which is $< 8$. Therefore $A = 3 \oplus 4 = 7$. Thus, only one operation is required. - Test Case $3$: We can show that it is not possible to make $A$ equal to $B$. ----- Sample Input 2 ------ 2 24 27 3 4 5 1000 ----- Sample Output 2 ------ 2 1 ----- explanation 2 ------ Note that the above sample case belongs to subtask $2$. - Test Case 1: We can first perform the operation with $X = 1$ which is $< 3$. Therefore $A = 24 \oplus 1 = 25$. Then we can perform the operation with $X = 2$ which is $< 3$. Therefore $A = 25 \oplus 2 = 27$. Therefore we can make $A$ equal to $B$ in $2$ operations. It can be shown that it is not possible to do this in less than $2$ operations.
{"inputs": ["2\n24 27 3\n4 5 1000\n", "3\n5 5 2\n3 7 8\n8 11 1\n"], "outputs": ["2\n1", "0\n1\n-1\n"]}
592
64
coding
Solve the programming task below in a Python markdown code block. Chef has an array A of length N. An index i is called *strong* if we can change the [gcd] of the whole array just by changing the value of A_{i}. Determine the number of *strong* indices in the array. ------ Input Format ------ - First line will contain T, number of test cases. Then the test cases follow. - First line of each test case contains an integer N denoting the size of the array A. - Second line contains N space separated integers A_{1}, A_{2}, \dots, A_{N} - denoting the array A. ------ Output Format ------ For each test case, output the number of strong indices in the array. ------ Constraints ------ $1 ≤ T ≤ 5 \cdot 10^{4}$ $2 ≤ N ≤ 3 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases do not exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 3 5 10 20 4 3 5 7 11 4 2 2 2 2 ----- Sample Output 1 ------ 3 0 4 ----- explanation 1 ------ Test Case $1$: All the indices are strong. - For index $1$ you can change the element to $10$ which changes the gcd of the array to $10$. - For index $2$ you can change the element to $12$ which changes the gcd of the array to $1$. - For index $3$ you can change the element to $12$ which changes the gcd of the array to $1$. Test Case $2$: No index is strong. If you change any single element, gcd still remains the same. Test Case $3$: All the indices are strong. You can change any element to $3$. This changes the gcd of the array to $1$.
{"inputs": ["3\n3\n5 10 20\n4\n3 5 7 11\n4\n2 2 2 2\n"], "outputs": ["3\n0\n4\n"]}
444
49
coding
Solve the programming task below in a Python markdown code block. An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? Input The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train. Output Print a single integer — the minimum number of actions needed to sort the railway cars. Examples Input 5 4 1 2 5 3 Output 2 Input 4 4 1 3 2 Output 2 Note In the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train.
{"inputs": ["1\n1\n", "2\n1 2\n", "2\n2 1\n", "3\n3 2 1\n", "3\n1 2 3\n", "3\n3 1 2\n", "3\n1 3 2\n", "3\n2 3 1\n"], "outputs": ["0", "0", "1", "2", "0", "1", "1", "1"]}
276
102
coding
Solve the programming task below in a Python markdown code block. You are given two integers $a$ and $m$. Calculate the number of integers $x$ such that $0 \le x < m$ and $\gcd(a, m) = \gcd(a + x, m)$. Note: $\gcd(a, b)$ is the greatest common divisor of $a$ and $b$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 50$) — the number of test cases. Next $T$ lines contain test cases — one per line. Each line contains two integers $a$ and $m$ ($1 \le a < m \le 10^{10}$). -----Output----- Print $T$ integers — one per test case. For each test case print the number of appropriate $x$-s. -----Example----- Input 3 4 9 5 10 42 9999999967 Output 6 1 9999999966 -----Note----- In the first test case appropriate $x$-s are $[0, 1, 3, 4, 6, 7]$. In the second test case the only appropriate $x$ is $0$.
{"inputs": ["3\n4 9\n5 10\n42 9999999967\n", "3\n4 5\n5 10\n42 9999999967\n", "3\n4 5\n8 10\n42 9999999967\n", "3\n4 9\n5 10\n42 9999999967\n", "3\n4 14\n5 10\n42 9999999967\n", "3\n4 27\n5 10\n42 9999999967\n", "3\n4 45\n5 10\n42 9999999967\n", "3\n4 75\n5 10\n42 9999999967\n"], "outputs": ["6\n1\n9999999966\n", "4\n1\n9999999966\n", "4\n4\n9999999966\n", "6\n1\n9999999966\n", "6\n1\n9999999966\n", "18\n1\n9999999966\n", "24\n1\n9999999966\n", "40\n1\n9999999966\n"]}
282
365
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Vietnamese], [Bengali], [Mandarin Chinese], and [Russian] as well. Chef is planning a heist in the reserve bank of Chefland. They are planning to hijack the bank for $D$ days and print the money. The initial rate of printing the currency is $P$ dollars per day and they increase the production by $Q$ dollars after every interval of $d$ days. For example, after $d$ days the rate is $P+Q$ dollars per day, and after $2d$ days the rate is $P+2Q$ dollars per day, and so on. Output the amount of money they will be able to print in the given period. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. Each test case contains a single line of input, four integers $D, d, P, Q$. ------ Output ------ For each test case, output in a single line the answer to the problem. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ d ≤ D ≤ 10^{6}$ $1 ≤ P, Q ≤ 10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $d ≤ D ≤ 100$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 3 2 1 1 1 3 2 1 1 5 2 1 2 ----- Sample Output 1 ------ 3 4 13 ----- explanation 1 ------ Test Case $1$: - On the first day, the rate of production is $1$ dollar per day so $1$ dollar is printed on the first day. - On the second day, the rate of production is $1 + 1 = 2$ dollars per day so $2$ dollars are printed on the second day. - The total amount of money printed in $2$ days is $1 + 2 = 3$ dollars. Test Case $2$: - For the first two days, the rate of production is $1$ dollar per day so $1 \cdot 2 = 2$ dollars are printed on the first two days. - On the third day, the rate of production is $1 + 1 = 2$ dollars per day so $2$ dollars are printed on the third day. - The total amount of money printed in $3$ days is $2 + 2 = 4$ dollars. Test Case $3$: - For the first two days, the rate of production is $1$ dollar per day so $1 \cdot 2 = 2$ dollars are printed on the first two days. - On the next two days, the rate of production is $1 + 2 = 3$ dollars per day so $3 \cdot 2 = 6$ dollars are printed on the next two days. - On the last day, the rate of production is $3 + 2 = 5$ dollars per day so $5$ dollars are printed on the last day. - The total amount of money printed in $5$ days is $2 + 6 + 5 = 13$ dollars.
{"inputs": ["3\n2 1 1 1\n3 2 1 1\n5 2 1 2"], "outputs": ["3\n4\n13"]}
718
41