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
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an integer array jobs, where jobs[i] is the amount of time it takes to complete the ith job. There are k workers that you can assign jobs to. Each job should be assigned to exactly one worker. The working time of a worker is the sum of the time it takes to complete all jobs assigned to them. Your goal is to devise an optimal assignment such that the maximum working time of any worker is minimized. Return the minimum possible maximum working time of any assignment.   Please complete the following python code precisely: ```python class Solution: def minimumTimeRequired(self, jobs: List[int], k: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(jobs = [3,2,3], k = 3) == 3\n assert candidate(jobs = [1,2,4,7,8], k = 2) == 11\n\n\ncheck(Solution().minimumTimeRequired)"}
153
69
coding
Solve the programming task below in a Python markdown code block. The only difference between the easy and hard versions is that the given string $s$ in the easy version is initially a palindrome, this condition is not always true for the hard version. A palindrome is a string that reads the same left to right and right to left. For example, "101101" is a palindrome, while "0101" is not. Alice and Bob are playing a game on a string $s$ (which is initially a palindrome in this version) of length $n$ consisting of the characters '0' and '1'. Both players take alternate turns with Alice going first. In each turn, the player can perform one of the following operations: Choose any $i$ ($1 \le i \le n$), where $s[i] =$ '0' and change $s[i]$ to '1'. Pay 1 dollar. Reverse the whole string, pay 0 dollars. This operation is only allowed if the string is currently not a palindrome, and the last operation was not reverse. That is, if Alice reverses the string, then Bob can't reverse in the next move, and vice versa. Reversing a string means reordering its letters from the last to the first. For example, "01001" becomes "10010" after reversing. The game ends when every character of string becomes '1'. The player who spends minimum dollars till this point wins the game and it is a draw if both spend equal dollars. If both players play optimally, output whether Alice wins, Bob wins, or if it is a draw. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^3$). Then $t$ test cases follow. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$). The second line of each test case contains the string $s$ of length $n$, consisting of the characters '0' and '1'. It is guaranteed that the string $s$ is a palindrome and contains at least one '0'. Note that there is no limit on the sum of $n$ over test cases. -----Output----- For each test case print a single word in a new line: "ALICE", if Alice will win the game, "BOB", if Bob will win the game, "DRAW", if the game ends in a draw. -----Examples----- Input 2 4 1001 1 0 Output BOB BOB -----Note----- In the first test case of the example, in the $1$-st move Alice has to perform the $1$-st operation, since the string is currently a palindrome. in the $2$-nd move Bob reverses the string. in the $3$-rd move Alice again has to perform the $1$-st operation. All characters of the string are '1', game over. Alice spends $2$ dollars while Bob spends $0$ dollars. Hence, Bob always wins.
{"inputs": ["2\n4\n1001\n1\n0\n"], "outputs": ["BOB\nBOB\n"]}
656
29
coding
Solve the programming task below in a Python markdown code block. You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
{"inputs": ["4\n3\n2 5 3\n5\n3 3 2 2 2\n6\n1 6 2 5 2 3\n5\n1 6 2 4 1\n", "4\n3\n2 5 3\n5\n3 3 2 2 2\n6\n1 6 0 5 2 3\n5\n1 6 2 4 1\n", "4\n3\n2 5 3\n5\n3 3 3 2 2\n6\n1 6 2 5 2 3\n5\n1 6 2 4 1\n", "4\n3\n2 5 3\n5\n3 3 2 0 2\n6\n1 6 0 5 2 3\n5\n1 6 2 4 1\n", "4\n3\n0 5 3\n5\n3 3 2 2 2\n6\n1 6 2 5 2 3\n5\n1 6 2 4 1\n", "4\n3\n2 5 3\n5\n3 3 2 2 2\n6\n0 6 0 5 2 3\n5\n1 6 2 4 1\n", "4\n3\n2 5 3\n5\n3 3 3 2 2\n6\n1 6 2 5 2 3\n5\n1 6 2 4 0\n", "4\n3\n2 5 3\n5\n3 3 2 0 2\n6\n1 6 0 5 2 3\n5\n2 6 2 4 1\n"], "outputs": ["0\n0\n1\n0\n", "0\n0\n1\n0\n", "0\n0\n1\n0\n", "0\n0\n1\n0\n", "0\n0\n1\n0\n", "0\n0\n1\n0\n", "0\n0\n1\n0\n", "0\n0\n1\n0\n"]}
516
486
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a string text and an array of strings words, return an array of all index pairs [i, j] so that the substring text[i...j] is in words. Return the pairs [i, j] in sorted order (i.e., sort them by their first coordinate, and in case of ties sort them by their second coordinate).   Please complete the following python code precisely: ```python class Solution: def indexPairs(self, text: str, words: List[str]) -> List[List[int]]: ```
{"functional": "def check(candidate):\n assert candidate(text = \"thestoryofleetcodeandme\", words = [\"story\",\"fleet\",\"leetcode\"]) == [[3,7],[9,13],[10,17]]\n assert candidate(text = \"ababa\", words = [\"aba\",\"ab\"]) == [[0,1],[0,2],[2,3],[2,4]]\n\n\ncheck(Solution().indexPairs)"}
124
100
coding
Solve the programming task below in a Python markdown code block. Complete the function that takes a string as an input, and return a list of all the unpaired characters (i.e. they show up an odd number of times in the string), in the order they were encountered as an array. In case of multiple appearances to choose from, take the last occurence of the unpaired character. **Notes:** * A wide range of characters is used, and some of them may not render properly in your browser. * Your solution should be linear in terms of string length to pass the last test. ## Examples ``` "Hello World" --> ["H", "e", " ", "W", "r", "l", "d"] "Codewars" --> ["C", "o", "d", "e", "w", "a", "r", "s"] "woowee" --> [] "wwoooowweeee" --> [] "racecar" --> ["e"] "Mamma" --> ["M"] "Mama" --> ["M", "m"] ``` Also feel free to reuse/extend the following starter code: ```python def odd_one_out(s): ```
{"functional": "_inputs = [['Hello World'], ['Codewars'], ['woowee'], ['wwoooowweeee'], ['racecar'], ['Mamma'], ['Mama'], ['\u00bc x 4 = 1'], ['\u00bc x 4 = 1 and \u00bd x 4 = 2']]\n_outputs = [[['H', 'e', ' ', 'W', 'r', 'l', 'd']], [['C', 'o', 'd', 'e', 'w', 'a', 'r', 's']], [[]], [[]], [['e']], [['M']], [['M', 'm']], [['\u00bc', 'x', '4', '=', '1']], [['\u00bc', '1', 'a', 'n', 'd', '\u00bd', '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(odd_one_out(*i), o[0])"}
262
329
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given two 0-indexed arrays nums1 and nums2 and a 2D array queries of queries. There are three types of queries: For a query of type 1, queries[i] = [1, l, r]. Flip the values from 0 to 1 and from 1 to 0 in nums1 from index l to index r. Both l and r are 0-indexed. For a query of type 2, queries[i] = [2, p, 0]. For every index 0 <= i < n, set nums2[i] = nums2[i] + nums1[i] * p. For a query of type 3, queries[i] = [3, 0, 0]. Find the sum of the elements in nums2. Return an array containing all the answers to the third type queries.   Please complete the following python code precisely: ```python class Solution: def handleQuery(self, nums1: List[int], nums2: List[int], queries: List[List[int]]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(nums1 = [1,0,1], nums2 = [0,0,0], queries = [[1,1,1],[2,1,0],[3,0,0]]) == [3]\n assert candidate(nums1 = [1], nums2 = [5], queries = [[2,0,0],[3,0,0]]) == [5]\n\n\ncheck(Solution().handleQuery)"}
249
103
coding
Solve the programming task below in a Python markdown code block. There are N flowers arranged in a row. For each i (1 \leq i \leq N), the height and the beauty of the i-th flower from the left is h_i and a_i, respectively. Here, h_1, h_2, \ldots, h_N are all distinct. Taro is pulling out some flowers so that the following condition is met: * The heights of the remaining flowers are monotonically increasing from left to right. Find the maximum possible sum of the beauties of the remaining flowers. Constraints * All values in input are integers. * 1 \leq N \leq 2 × 10^5 * 1 \leq h_i \leq N * h_1, h_2, \ldots, h_N are all distinct. * 1 \leq a_i \leq 10^9 Input Input is given from Standard Input in the following format: N h_1 h_2 \ldots h_N a_1 a_2 \ldots a_N Output Print the maximum possible sum of the beauties of the remaining flowers. Examples Input 4 3 1 4 2 10 20 30 40 Output 60 Input 1 1 10 Output 10 Input 5 1 2 3 4 5 1000000000 1000000000 1000000000 1000000000 1000000000 Output 5000000000 Input 9 4 2 5 8 3 6 1 7 9 6 8 8 4 6 3 5 7 5 Output 31
{"inputs": ["1\n1\n10", "4\n3 1 3 2\n6 6 0 7", "4\n3 1 4 2\n15 6 2 64", "4\n3 1 4 2\n18 5 0 24", "4\n3 1 3 2\n20 6 0 37", "4\n3 1 4 2\n18 5 0 35", "4\n3 1 3 2\n6 12 0 37", "4\n3 1 3 2\n5 22 2 37"], "outputs": ["10", "13\n", "70\n", "29\n", "43\n", "40\n", "49\n", "59\n"]}
428
198
coding
Solve the programming task below in a Python markdown code block. D: Shiritori Compression problem Ebi-chan and Kana-chan did a shiritori. Ebi-chan is looking at the memo of the word that came out in Shiritori. Ebi-chan wants to remove redundant continuous subsequences from this word sequence w_1, w_2, ..., w_N until they no longer exist. The redundant continuous subsequence that Ebi-chan thinks is defined as follows. * The continuous subsequence w_i, ..., w_ {j-1} is redundant when i, j satisfying the following is present. * For the subscripts i, j with i <j, the first letters of the words w_i and w_j are equal. At this time, Ebi-chan removes words with subscript i or more and j-1 or less. For example, consider the following word string: * apple → editor → random → me → edge It is compressed as follows: * apple → edge Since the first letter of editor and edge matches with e, the words from editor to just before edge (me) are stripped. Please note that Ebi-chan's criteria do not consider the same trailing character to be redundant. For example, in the compressed word string above, the last letter of apple and edge is both e, but this does not remove the word. The following examples are also possible. * orange → eel → luck * banana → at → tomb → bus → sound → does → some * peach → hero → owl → loop → proof → fish → he Each of these can be compressed as follows: Note that in each case, the last word is saved. * orange → eel → luck * Already compressed. * bus → some * You can compress banana to bus and sound to some. * peach → he * You can also compress proof → fish → he, but note that the number of words after compression is different. Ebi-chan's memos are given, so please output the minimum value L of the length (number of words) of the word string obtained by compressing them. You can assume that "the same word appears multiple times" or "the first letter of a word does not match the last letter of the previous word" does not occur. Input format N w_1 ... w_N The number of words N is given on the first line, and the i-th word is given on the 1 + i line. Constraint * 1 \ leq N \ leq 10 ^ 5 * 1 \ leq $ \ sum_ {i = 1} ^ N $ | w_i | \ leq 10 ^ 6 * Each letter of w_i is lowercase Output format Print L on one line. No other extra characters should be included. Input example 1 7 banana at tomb bus sound does does some Output example 1 2 This is the example given above. Input example 2 7 peach hero owl loop proof fish he Output example 2 2 Another example given above. Example Input 7 banana at tomb bus sound does some Output 2
{"inputs": ["7\nbanana\nta\ntomb\nbus\nsound\ndoes\nsome", "7\nananab\nta\nsomb\nbus\nsound\ndoes\nsemo", "7\nbanaoa\nta\ntomb\nbus\nsound\ndoes\nsome", "7\nbanaoa\nta\nuomb\nbus\nsound\ndoes\nsome", "7\nbanaoa\nua\ntomb\nbus\nsound\ndoes\nsome", "7\nbanaoa\nua\ntomb\nbus\nspund\ndoes\nsome", "7\nbanaoa\nua\ntomb\nbus\nspund\nseod\nsome", "7\nbanaoa\nua\ntomb\nbus\nspund\nsepd\nsome"], "outputs": ["2\n", "3\n", "2\n", "2\n", "2\n", "2\n", "2\n", "2\n"]}
684
211
coding
Solve the programming task below in a Python markdown code block. You have been assigned to develop a filter for bad messages in the in-game chat. A message is a string $S$ of length $n$, consisting of lowercase English letters and characters ')'. The message is bad if the number of characters ')' at the end of the string strictly greater than the number of remaining characters. For example, the string ")bc)))" has three parentheses at the end, three remaining characters, and is not considered bad. -----Input----- The first line contains the number of test cases $t$ ($1 \leq t \leq 100$). Description of the $t$ test cases follows. The first line of each test case contains an integer $n$ ($1 \leq n \leq 100$). The second line of each test case contains a string $S$ of length $n$, consisting of lowercase English letters and characters ')'. -----Output----- For each of $t$ test cases, print "Yes" if the string is bad. Otherwise, print "No". You can print each letter in any case (upper or lower). -----Examples----- Input 5 2 )) 12 gl))hf)))))) 9 gege))))) 14 )aa))b)))))))) 1 ) Output Yes No Yes Yes Yes -----Note----- None
{"inputs": ["1\n1\nk\n", "1\n1\ng\n", "1\n1\na\n", "1\n4\naaaa\n", "1\n5\naaaaa\n", "1\n7\nabcd)))\n", "1\n10\nsa)ttttttt\n", "2\n6\naaaaa)\n6\n))))))\n"], "outputs": ["No\n", "No\n", "No\n", "No\n", "No\n", "No\n", "No\n", "No\nYes\n"]}
290
121
coding
Solve the programming task below in a Python markdown code block. Vanya is managed to enter his favourite site Codehorses. Vanya uses n distinct passwords for sites at all, however he can't remember which one exactly he specified during Codehorses registration. Vanya will enter passwords in order of non-decreasing their lengths, and he will enter passwords of same length in arbitrary order. Just when Vanya will have entered the correct password, he is immediately authorized on the site. Vanya will not enter any password twice. Entering any passwords takes one second for Vanya. But if Vanya will enter wrong password k times, then he is able to make the next try only 5 seconds after that. Vanya makes each try immediately, that is, at each moment when Vanya is able to enter password, he is doing that. Determine how many seconds will Vanya need to enter Codehorses in the best case for him (if he spends minimum possible number of second) and in the worst case (if he spends maximum possible amount of seconds). -----Input----- The first line of the input contains two integers n and k (1 ≤ n, k ≤ 100) — the number of Vanya's passwords and the number of failed tries, after which the access to the site is blocked for 5 seconds. The next n lines contains passwords, one per line — pairwise distinct non-empty strings consisting of latin letters and digits. Each password length does not exceed 100 characters. The last line of the input contains the Vanya's Codehorses password. It is guaranteed that the Vanya's Codehorses password is equal to some of his n passwords. -----Output----- Print two integers — time (in seconds), Vanya needs to be authorized to Codehorses in the best case for him and in the worst case respectively. -----Examples----- Input 5 2 cba abc bb1 abC ABC abc Output 1 15 Input 4 100 11 22 1 2 22 Output 3 4 -----Note----- Consider the first sample case. As soon as all passwords have the same length, Vanya can enter the right password at the first try as well as at the last try. If he enters it at the first try, he spends exactly 1 second. Thus in the best case the answer is 1. If, at the other hand, he enters it at the last try, he enters another 4 passwords before. He spends 2 seconds to enter first 2 passwords, then he waits 5 seconds as soon as he made 2 wrong tries. Then he spends 2 more seconds to enter 2 wrong passwords, again waits 5 seconds and, finally, enters the correct password spending 1 more second. In summary in the worst case he is able to be authorized in 15 seconds. Consider the second sample case. There is no way of entering passwords and get the access to the site blocked. As soon as the required password has length of 2, Vanya enters all passwords of length 1 anyway, spending 2 seconds for that. Then, in the best case, he immediately enters the correct password and the answer for the best case is 3, but in the worst case he enters wrong password of length 2 and only then the right one, spending 4 seconds at all.
{"inputs": ["1 1\na1\na1\n", "1 1\na1\na1\n", "1 2\na1\na1\n", "1 100\na1\na1\n", "2 1\n1\n12\n12\n", "2 1\na\nab\nab\n", "2 1\n1\n12\n12\n", "2 1\na\nab\nab\n"], "outputs": ["1 1\n", "1 1\n", "1 1\n", "1 1\n", "7 7\n", "7 7\n", "7 7\n", "7 7\n"]}
709
156
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a 0-indexed string s. You are also given a 0-indexed string queryCharacters of length k and a 0-indexed array of integer indices queryIndices of length k, both of which are used to describe k queries. The ith query updates the character in s at index queryIndices[i] to the character queryCharacters[i]. Return an array lengths of length k where lengths[i] is the length of the longest substring of s consisting of only one repeating character after the ith query is performed.   Please complete the following python code precisely: ```python class Solution: def longestRepeating(self, s: str, queryCharacters: str, queryIndices: List[int]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(s = \"babacc\", queryCharacters = \"bcb\", queryIndices = [1,3,3]) == [3,3,4]\n assert candidate(s = \"abyzz\", queryCharacters = \"aa\", queryIndices = [2,1]) == [2,3]\n\n\ncheck(Solution().longestRepeating)"}
166
86
coding
Solve the programming task below in a Python markdown code block. The Chef has a huge square napkin of size 2n X 2n. He folds the napkin n-3 times. Each time he folds its bottom side over its top side, and then its right side over its left side. After each fold, the side length of the napkin is reduced by half. The Chef continues folding until there remains a 8x8 sheet, lying flat on a table. Oh, did I forget to mention that the Chef was cooking a new brown colored curry while folding the napkin. He drops some brown colored gravy onto some cells in the folded 8x8 napkin. When he drops the gravy, it soaks through all the cells below it. Now the Chef unfolds the napkin to its original size. There are now many curry stained brown colored cells in the napkin. They form several separate regions, each of which is connected. Could you help the Chef count how many regions of brown cells are there in the napkin? Note that two cells are adjacent if they share a common edge (they are not considered adjacent if they only share a corner). Two cells are connected if we can go from one cell to the other via adjacent cells. A region is a maximal set of cells such that every two of its cells are connected. Please see the example test case for more details. -----Input----- The first line contains t, the number of test cases (about 50). Then t test cases follow. Each test case has the following form: - The first line contains N (3 ≤ N ≤ 109) - Then, 8 lines follow. Each line is a string of 8 characters, 0 or 1, where 1 denotes a stained brown cell in the folded napkin. -----Output----- For each test case, print a single number that is the number of disconnected brown regions in the unfolded napkin. Since the result may be a very large number, you only need to print its remainder when dividing by 21945. -----Example----- Input: 3 3 01000010 11000001 00000000 00011000 00011000 00010100 00001000 00000000 4 01000010 11000001 00000000 00011000 00011000 00010100 00001000 00000000 1000000000 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 Output: 6 22 1 -----Output details----- Case 1 and 2: There are 6 brown regions in the 8x8 napkin. If we unfold it once, it has 22 brown regions: 11 regions in the top half and 11 regions in the bottom half (as shown in the figure above). Case 3: All cells of the napkin are stained, so there is always one brown region, no matter how many times the Chef unfolds the napkin.
{"inputs": ["3\n3\n01000010\n11000001\n00000000\n00011000\n00011000\n00010100\n00001000\n00000000\n4\n01000010\n11000001\n00000000\n00011000\n00011000\n00010100\n00001000\n00000000\n1000000000\n11111111\n11111111\n11111111\n11111111\n11111111\n11111111\n11111111\n11111111\n\n"], "outputs": ["6\n22\n1"]}
771
250
coding
Solve the programming task below in a Python markdown code block. You are given n k-digit integers. You have to rearrange the digits in the integers so that the difference between the largest and the smallest number was minimum. Digits should be rearranged by the same rule in all integers. Input The first line contains integers n and k — the number and digit capacity of numbers correspondingly (1 ≤ n, k ≤ 8). Next n lines contain k-digit positive integers. Leading zeroes are allowed both in the initial integers and the integers resulting from the rearranging of digits. Output Print a single number: the minimally possible difference between the largest and the smallest number after the digits are rearranged in all integers by the same rule. Examples Input 6 4 5237 2753 7523 5723 5327 2537 Output 2700 Input 3 3 010 909 012 Output 3 Input 7 5 50808 36603 37198 44911 29994 42543 50156 Output 20522 Note In the first sample, if we rearrange the digits in numbers as (3,1,4,2), then the 2-nd and the 4-th numbers will equal 5237 and 2537 correspondingly (they will be maximum and minimum for such order of digits). In the second sample, if we swap the second digits and the first ones, we get integers 100, 99 and 102.
{"inputs": ["1 2\n95\n", "1 2\n96\n", "1 2\n54\n", "1 3\n289\n", "1 3\n250\n", "2 1\n2\n0\n", "2 1\n2\n1\n", "3 1\n1\n2\n2\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "2\n", "1\n", "1\n"]}
372
117
coding
Solve the programming task below in a Python markdown code block. You are given three integers A, B and C. Find the minimum number of operations required to make A, B and C all equal by repeatedly performing the following two kinds of operations in any order: - Choose two among A, B and C, then increase both by 1. - Choose one among A, B and C, then increase it by 2. It can be proved that we can always make A, B and C all equal by repeatedly performing these operations. -----Constraints----- - 0 \leq A,B,C \leq 50 - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: A B C -----Output----- Print the minimum number of operations required to make A, B and C all equal. -----Sample Input----- 2 5 4 -----Sample Output----- 2 We can make A, B and C all equal by the following operations: - Increase A and C by 1. Now, A, B, C are 3, 5, 5, respectively. - Increase A by 2. Now, A, B, C are 5, 5, 5, respectively.
{"inputs": ["2 5 4\n", "2 6 3\n", "0 0 0\n", "0 0 1\n", "0 0 50\n", "31 41 5\n", "0 50 50\n", "0 49 49\n"], "outputs": ["2\n", "5\n", "0\n", "1\n", "50\n", "23\n", "25\n", "26\n"]}
259
113
coding
Solve the programming task below in a Python markdown code block. 12:17 (UTC): The sample input 1 and 2 were swapped. The error is now fixed. We are very sorry for your inconvenience. There are N children in AtCoder Kindergarten, conveniently numbered 1 through N. Mr. Evi will distribute C indistinguishable candies to the children. If child i is given a candies, the child's happiness will become x_i^a, where x_i is the child's excitement level. The activity level of the kindergarten is the product of the happiness of all the N children. For each possible way to distribute C candies to the children by giving zero or more candies to each child, calculate the activity level of the kindergarten. Then, calculate the sum over all possible way to distribute C candies. This sum can be seen as a function of the children's excitement levels x_1,..,x_N, thus we call it f(x_1,..,x_N). You are given integers A_i,B_i (1≦i≦N). Find modulo 10^9+7. -----Constraints----- - 1≦N≦400 - 1≦C≦400 - 1≦A_i≦B_i≦400 (1≦i≦N) -----Partial Score----- - 400 points will be awarded for passing the test set satisfying A_i=B_i (1≦i≦N). -----Input----- The input is given from Standard Input in the following format: N C A_1 A_2 ... A_N B_1 B_2 ... B_N -----Output----- Print the value of modulo 10^9+7. -----Sample Input----- 2 3 1 1 1 1 -----Sample Output----- 4 This case is included in the test set for the partial score, since A_i=B_i. We only have to consider the sum of the activity level of the kindergarten where the excitement level of both child 1 and child 2 are 1 (f(1,1)). - If child 1 is given 0 candy, and child 2 is given 3 candies, the activity level of the kindergarten is 1^0*1^3=1. - If child 1 is given 1 candy, and child 2 is given 2 candies, the activity level of the kindergarten is 1^1*1^2=1. - If child 1 is given 2 candies, and child 2 is given 1 candy, the activity level of the kindergarten is 1^2*1^1=1. - If child 1 is given 3 candies, and child 2 is given 0 candy, the activity level of the kindergarten is 1^3*1^0=1. Thus, f(1,1)=1+1+1+1=4, and the sum over all f is also 4.
{"inputs": ["1 2\n1\n4", "1 4\n1\n4", "1 3\n1\n3", "1 4\n1\n1", "1 3\n1\n5", "1 4\n1\n2", "1 3\n1\n9", "1 1\n1\n4"], "outputs": ["30\n", "354\n", "36\n", "1\n", "225\n", "17\n", "2025\n", "10\n"]}
622
121
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a string s, return the last substring of s in lexicographical order.   Please complete the following python code precisely: ```python class Solution: def lastSubstring(self, s: str) -> str: ```
{"functional": "def check(candidate):\n assert candidate(s = \"abab\") == \"bab\"\n assert candidate(s = \"leetcode\") == \"tcode\"\n\n\ncheck(Solution().lastSubstring)"}
64
47
coding
Solve the programming task below in a Python markdown code block. Did you know that Chwee kueh, a cuisine of Singapore, means water rice cake ? Its a variety of the most popular South Indian savory cake, only that we call it here idli :). The tastiest idlis are made in Chennai, by none other than our famous chef, Dexter Murugan. Being very popular, he is flown from Marina to Miami, to serve idlis in the opening ceremony of icpc world finals ( which is happening right now ! ). There are N students and they are initially served with some idlis. Some of them are angry because they got less idlis than some other. Dexter decides to redistribute the idlis so they all get equal number of idlis finally. He recollects his father's code, "Son, if you ever want to redistribute idlis, follow this method. While there are two persons with unequal number of idlis, repeat the following step. Select two persons A and B, A having the maximum and B having the minimum number of idlis, currently. If there are multiple ways to select A (similarly B), select any one randomly. Let A and B have P and Q number of idlis respectively and R = ceil( ( P - Q ) / 2 ), Transfer R idlis from A to B." Given the initial number of idlis served to each student, find the number of times Dexter has to repeat the above step. If he can not distribute idlis equally by following the above method, print -1. ------ Notes ------ ceil(x) is the smallest integer that is not less than x. ------ Input ------ First line contains an integer T ( number of test cases, around 20 ). T cases follows. Each case starts with an integer N ( 1 ≤ N ≤ 3000 ). Next line contains an array A of N integers separated by spaces, the initial number of idlis served ( 0 ≤ A[i] ≤ N ) ------ Output ------ For each case, output the number of times Dexter has to repeat the given step to distribute idlis equally or -1 if its not possible. Note:There are multiple test sets, and the judge shows the sum of the time taken over all test sets of your submission, if Accepted. Time limit on each test set is 3 sec ----- Sample Input 1 ------ 3 4 1 2 2 3 2 1 2 7 1 2 3 4 5 6 7 ----- Sample Output 1 ------ 1 -1 3 ----- explanation 1 ------ Case 1 : { 1, 2, 2, 3}. Maximum 3, Minimum 1. R = ceil((3-1)/2) = 1. Transfer 1 idli from person having 3 idlis to the person having 1 idli. Each of them has 2 idlis now, so just 1 step is enough. Case 2 : {1,2} R = ceil((2-1)/2) = 1. {1,2} -> {2,1} -> {1,2} .... they can never get equal idlis :( Case 3 : Sorted arrays, in the order encountered {1, 2, 3, 4, 5, 6, 7} -> {2, 3, 4, 4, 4, 5, 6} -> {3, 4, 4, 4, 4, 4, 5} -> {4, 4, 4, 4, 4, 4, 4}
{"inputs": ["3\n4\n1 2 2 3\n2\n1 2\n7\n1 2 3 4 5 6 7", "3\n4\n1 2 2 3\n2\n1 2\n7\n1 0 3 4 5 6 7", "3\n4\n1 4 2 3\n2\n1 2\n7\n1 0 3 4 5 6 7", "3\n4\n0 2 2 3\n2\n1 2\n7\n1 2 3 4 5 6 7", "3\n4\n1 1 1 1\n2\n1 2\n7\n1 4 0 1 6 6 7", "3\n4\n1 4 2 3\n2\n1 2\n7\n1 0 4 4 6 6 7", "3\n4\n1 2 2 3\n2\n1 2\n7\n1 4 3 2 5 6 7", "3\n4\n1 4 2 1\n2\n1 2\n7\n1 0 4 4 6 6 7"], "outputs": ["1\n-1\n3", "1\n-1\n-1\n", "-1\n-1\n-1\n", "-1\n-1\n3\n", "0\n-1\n-1\n", "-1\n-1\n4\n", "1\n-1\n3\n", "2\n-1\n4\n"]}
774
360
coding
Solve the programming task below in a Python markdown code block. A list of names is taken as input, in which a particular name can occur multiple times. You need to arrange these names as they will appear in the dictionary and also print the number of times the arranged names appear in the list taken as input. Input: The first line of input contains an integer, t, which denotes the number of names that will follow. Then, t lines follow, each containing a name, in the form of a character string S. Output: The output contains the names as they would appear in the dictionary, followed by the frequency of that name in the list. Constraints: 1 ≤ t ≤ 100000 1 ≤ |S| ≤30 S contains only lower case characters. SAMPLE INPUT 3 ritesh sahil ritesh SAMPLE OUTPUT ritesh 2 sahil 1 Explanation Test Case #1: As the name starts from 'r' comes first then 's' in dictionary and in this case the 'ritesh' name is given 2 times and 'sahil' is given only once so their frequency is coming and remove the duplicate rows.
{"inputs": ["3\nritesh\nsahil\nritesh"], "outputs": ["ritesh 2\nsahil 1"]}
252
31
coding
Solve the programming task below in a Python markdown code block. Being a crewmate in the Among Us game, you will have to solve a task to win against the imposter. The task : You are given a certain number of bulbs(Say n) which are initially in OFF position. The bulbs are numbered from 1 to n. There will be n current fluctuations in such a way that in the 1st fluctuation all bulbs are toggled, in the 2nd fluctuation every 2nd bulb is toggled, in the 3rd fluctuation every 3rd bulb is toggled and so on. Your job as a crewmate is to find the number of bulbs which are ON at the end of n fluctuations. But, the imposter has a special power which alerts it when a bulb with a number divisible by 3 is kept ON at the end of n fluctuations. So, you will have to switch off the bulbs with numbers divisible by 3 and give the final result to complete the task and win the game. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, an integer $n$. -----Output:----- - For each testcase, output a single integer - the number of bulbs that will remain ON after $n$ fluctuations. -----Constraints :----- - $1 \leq T \leq 1000$ - $2 \leq n \leq 10^9$ -----Sample Input:----- 2 2 20 -----Sample Output:----- 1 3
{"inputs": ["2\n2\n20"], "outputs": ["1\n3"]}
341
19
coding
Solve the programming task below in a Python markdown code block. White Falcon was amazed by what she can do with heavy-light decomposition on trees. As a resut, she wants to improve her expertise on heavy-light decomposition. Her teacher gave her an another assignment which requires path updates. As always, White Falcon needs your help with the assignment. You are given a tree with $N$ nodes and each node's value $val_i$ is initially $0$. Let's denote the path from node $\mbox{u}$ to node $\boldsymbol{\nu}$ like this: $p_1,p_2,p_3,\ldots,p_k$, where $p_1=u$ and $p_k=v$, and $p_i$ and $\textbf{p}_{i+1}$ are connected. The problem asks you to operate the following two types of queries on the tree: "1 u v x" Add $\boldsymbol{x}$ to $val_{p_1}$, $2x$ to $val_{p_2}$, $3x$ to $val_{p_3}$, ..., $kx$ to $val_{p_k}$. "2 u v" print the sum of the nodes' values on the path between $\mbox{u}$ and $\boldsymbol{\nu}$ at modulo $10^9+7$. Input Format First line cosists of two integers $N$ and $Q$ seperated by a space. Following $N-1$ lines contains two integers which denote the undirectional edges of the tree. Following $Q$ lines contains one of the query types described above. Note: Nodes are numbered by using 0-based indexing. Constraints $1\le N,Q\le50000$ $0\leq x<10^9+7$ Output Format For every query of second type print a single integer. Sample Input 3 2 0 1 1 2 1 0 2 1 2 1 2 Sample Output 5 Explanation After the first type of query, $val_0=1,val_1=2,val_2=3$. Hence the answer of the second query is $2+3=5$.
{"inputs": ["3 2\n0 1\n1 2\n1 0 2 1\n2 1 2\n"], "outputs": ["5\n"]}
475
38
coding
Solve the programming task below in a Python markdown code block. Walter White and Jesse Pinkman (a drug addict) both love to play with chemicals. One day they were playing with some chemicals to make an energy drink. Unknowingly they made a highly powerful drink. To test the drink on others also they called some of their friends and gave a drop of it to everyone. Now they all were feeling highly energetic and thought of an unique game to play with each other. After pondering for a while, Jesse came up with an extraordinary idea of competing in a race around a circular globe with N checkpoints each of one unit. Walter and all their other friends agreed with it.They divided themselves in $2$ teams with $N$ teammates in each team.This race has two commencing points $A$ and $B$ strictly facing each other. Walter and his team commences from $A$ point and other team starts from $B$. Both the teams start running at the same time clockwise around the globe. Speed of every player is constant throughout the race. If a player has a speed $X$ then it means that he covers a distance of $X$ units in one second.The race ends when some member of one team overtakes all members of opposite team at any point of time. Now you have to tell if any team will win the race or not.They all are stubborn and can run forever just to win the race. Help them to know if it is possible in anyway that the race will come to an end. For Clarity, you can visualize the path as a circular paths where $A$ and $B$ are opposite ends of diameter. It can be proven that the actual circumference of circle do not affect the answer. It is also possible that someone don't run at all.Keep in mind that the fastest one wins the race so does the code. -----Input:------ - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains a single integer $N$ number of teammates in both team. - The second line contains $N$ space-separated integers $A_1, A_2 \ldots A_N$ denoting speed of A's Team - The third line contains $N$ space-separated integers $B_1, B_2 \ldots B_N$ denoting speed of B's Team -----Output:------ For each test case, print a single line denoting YES if the race ends at any point of time else NO -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^5$ - $0 \leq A_i \leq 2^{15}$ - $0 \leq B_i \leq 2^{15}$ -----Subtasks----- Subtask #1 (30 points): - $1 \le N \le 20$ - $0 \le A_i \le 11$ - $0 \le B_i \le 11$ Subtask #2 (70 points): - Original constraints -----Sample input:----- 1 5 1 2 3 4 5 2 7 8 9 9 -----Sample output----- YES -----Sample Explanation:------ Team B can overtake all members of Team A.
{"inputs": ["1\n5\n1 2 3 4 5\n2 7 8 9 9\n"], "outputs": ["YES"]}
724
35
coding
Solve the programming task below in a Python markdown code block. Alice and Bob are playing a game. They start with a positive integer $n$ and take alternating turns doing operations on it. Each turn a player can subtract from $n$ one of its divisors that isn't $1$ or $n$. The player who cannot make a move on his/her turn loses. Alice always moves first. Note that they subtract a divisor of the current number in each turn. You are asked to find out who will win the game if both players play optimally. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Then $t$ test cases follow. Each test case contains a single integer $n$ ($1 \leq n \leq 10^9$) — the initial number. -----Output----- For each test case output "Alice" if Alice will win the game or "Bob" if Bob will win, if both players play optimally. -----Examples----- Input 4 1 4 12 69 Output Bob Alice Alice Bob -----Note----- In the first test case, the game ends immediately because Alice cannot make a move. In the second test case, Alice can subtract $2$ making $n = 2$, then Bob cannot make a move so Alice wins. In the third test case, Alice can subtract $3$ so that $n = 9$. Bob's only move is to subtract $3$ and make $n = 6$. Now, Alice can subtract $3$ again and $n = 3$. Then Bob cannot make a move, so Alice wins.
{"inputs": ["4\n1\n4\n12\n69\n"], "outputs": ["Bob\nAlice\nAlice\nBob\n"]}
363
30
coding
Solve the programming task below in a Python markdown code block. Yash has recently learnt about the Fibonacci sequence and is very excited about it. He calls a sequence Fibonacci-ish if the sequence consists of at least two elements f_0 and f_1 are arbitrary f_{n} + 2 = f_{n} + 1 + f_{n} for all n ≥ 0. You are given some sequence of integers a_1, a_2, ..., a_{n}. Your task is rearrange elements of this sequence in such a way that its longest possible prefix is Fibonacci-ish sequence. -----Input----- The first line of the input contains a single integer n (2 ≤ n ≤ 1000) — the length of the sequence a_{i}. The second line contains n integers a_1, a_2, ..., a_{n} (|a_{i}| ≤ 10^9). -----Output----- Print the length of the longest possible Fibonacci-ish prefix of the given sequence after rearrangement. -----Examples----- Input 3 1 2 -1 Output 3 Input 5 28 35 7 14 21 Output 4 -----Note----- In the first sample, if we rearrange elements of the sequence as - 1, 2, 1, the whole sequence a_{i} would be Fibonacci-ish. In the second sample, the optimal way to rearrange elements is $7$, $14$, $21$, $35$, 28.
{"inputs": ["2\n2 2\n", "2\n0 0\n", "2\n0 0\n", "2\n2 2\n", "2\n2 4\n", "2\n4 4\n", "2\n1 4\n", "2\n1 3\n"], "outputs": ["2\n", "2\n", "2\n", "2\n", "2\n", "2\n", "2\n", "2\n"]}
328
102
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a 0-indexed integer array nums. Swaps of adjacent elements are able to be performed on nums. A valid array meets the following conditions: The largest element (any of the largest elements if there are multiple) is at the rightmost position in the array. The smallest element (any of the smallest elements if there are multiple) is at the leftmost position in the array. Return the minimum swaps required to make nums a valid array.   Please complete the following python code precisely: ```python class Solution: def minimumSwaps(self, nums: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [3,4,5,5,3,1]) == 6\n assert candidate(nums = [9]) == 0\n\n\ncheck(Solution().minimumSwaps)"}
142
54
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese and Russian. Chef Anup is making dishes. Each dish consists of N ingredients, and quantity of each ingredient is an integer between 1 and K inclusive. Relative quality of 2 dishes is determined by their lexicographic order. Dish A is of lower quality than dish B if there is a position i (1≤i≤N) such that A_{j} = B_{j} for all j < i and A_{i} < B_{i}. E.g., if N = 2 and K = 2, then the possible dishes in lexicographic order are (1,1), (1,2), (2,1), (2,2). Given an integer L, determine the L^{th} possible dish in increasing order of quality. ------ Input Format ------ The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. Each test case description consists of a single line containing three integers N, K and L as described above. ------ Output Format ------ For each test case, print N space-separated integers in a single line describing the quantities of the ingredients for the Lth dish. ------ Constraints ------ $1 ≤ T ≤ 10$ $L will not exceed the total number of possible dishes.$ ------ subtasks ------ $For 20 points : N = 3, 2 ≤ K ≤ 102, 1 ≤ L ≤ 106$ $For 30 points : 1 ≤ N ≤ 102, 2 ≤ K ≤ 102, 1 ≤ L ≤ 104$ $For 50 points : 1 ≤ N ≤ 103, 2 ≤ K ≤ 103, 1 ≤ L ≤ 1018$ ----- Sample Input 1 ------ 4 3 3 1 3 3 2 3 3 3 3 3 4 ----- Sample Output 1 ------ 1 1 1 1 1 2 1 1 3 1 2 1 ----- explanation 1 ------ First 4 dishes in order have ingredients (1,1,1), (1,1,2), (1,1,3), and (1,2,1). ----- Sample Input 2 ------ 4 4 3 1 4 3 2 4 3 3 4 3 4 ----- Sample Output 2 ------ 1 1 1 1 1 1 1 2 1 1 1 3 1 1 2 1 ----- explanation 2 ------ First 4 dishes in order have ingredients (1,1,1,1), (1,1,1,2), (1,1,1,3), and (1,1,2,1).
{"inputs": ["4\n3 3 1\n3 3 2\n3 3 3\n3 3 4", "4\n4 3 1\n4 3 2\n4 3 3\n4 3 4"], "outputs": ["1 1 1\n1 1 2\n1 1 3\n1 2 1", "1 1 1 1\n1 1 1 2\n1 1 1 3\n1 1 2 1"]}
631
118
coding
Solve the programming task below in a Python markdown code block. Given an array of n$n$ integers : A1,A2,...,An$ A_1, A_2,... , A_n$, find the longest size subsequence which satisfies the following property: The xor of adjacent integers in the subsequence must be non-decreasing. -----Input:----- - First line contains an integer n$n$, denoting the length of the array. - Second line will contain n$n$ space separated integers, denoting the elements of the array. -----Output:----- Output a single integer denoting the longest size of subsequence with the given property. -----Constraints----- - 1≤n≤103$1 \leq n \leq 10^3$ - 0≤Ai≤1018$0 \leq A_i \leq 10^{18}$ -----Sample Input:----- 8 1 200 3 0 400 4 1 7 -----Sample Output:----- 6 -----EXPLANATION:----- The subsequence of maximum length is {1, 3, 0, 4, 1, 7} with Xor of adjacent indexes as {2,3,4,5,6} (non-decreasing)
{"inputs": ["8\n1 200 3 0 400 4 1 7"], "outputs": ["6"]}
275
32
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an array of binary strings strs and two integers m and n. Return the size of the largest subset of strs such that there are at most m 0's and n 1's in the subset. A set x is a subset of a set y if all elements of x are also elements of y.   Please complete the following python code precisely: ```python class Solution: def findMaxForm(self, strs: List[str], m: int, n: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(strs = [\"10\", \"0001\", \"111001\", \"1\", \"0\"], m = 5, n = 3) == 4\n assert candidate(strs = [\"10\", \"0\", \"1\"], m = 1, n = 1) == 2\n\n\ncheck(Solution().findMaxForm)"}
122
98
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef is teaching a cooking course. There are $N$ students attending the course, numbered $1$ through $N$. Before each lesson, Chef has to take attendance, i.e. call out the names of students one by one and mark which students are present. Each student has a first name and a last name. In order to save time, Chef wants to call out only the first names of students. However, whenever there are multiple students with the same first name, Chef has to call out the full names (both first and last names) of all these students. For each student that does not share the first name with any other student, Chef may still call out only this student's first name. Help Chef decide, for each student, whether he will call out this student's full name or only the first name. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of the following $N$ lines contains two space-separated strings denoting the first and last name of student $i$. ------ Output ------ For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should describe how Chef calls out the $i$-th student's name ― it should contain either the first name or the first and last name separated by a space. ------ Constraints ------ $1 ≤ T ≤ 100$ $2 ≤ N ≤ 100$ all first and last names contain only lowercase English letters the lengths of all first and last names are between $1$ and $10$ inclusive ------ Subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 1 4 hasan jaddouh farhod khakimiyon kerim kochekov hasan khateeb ----- Sample Output 1 ------ hasan jaddouh farhod kerim hasan khateeb
{"inputs": ["1\n4\nhasan jaddouh\nfarhod khakimiyon\nkerim kochekov\nhasan khateeb"], "outputs": ["hasan jaddouh\nfarhod\nkerim\nhasan khateeb"]}
512
61
coding
Solve the programming task below in a Python markdown code block. Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays. An array is Rainbow if it has the following structure: - First a1 elements equal 1. - Next a2 elements equal 2. - Next a3 elements equal 3. - Next a4 elements equal 4. - Next a5 elements equal 5. - Next a6 elements equal 6. - Next a7 elements equal 7. - Next a6 elements equal 6. - Next a5 elements equal 5. - Next a4 elements equal 4. - Next a3 elements equal 3. - Next a2 elements equal 2. - Next a1 elements equal 1. - ai can be any non-zero positive integer. - There are no other elements in array. Help Chef in finding out if the given array is a Rainbow Array or not. -----Input----- - The first line of the input contains an integer T denoting the number of test cases. - The first line of each test case contains an integer N, denoting the number of elements in the given array. - The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array. -----Output----- - For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not. -----Constraints----- - 1 ≤ T ≤ 100 - 7 ≤ N ≤ 100 - 1 ≤ Ai ≤ 10 -----Subtasks----- - Subtask 1 (100 points) : Original constraints -----Example----- Input 3 19 1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1 14 1 2 3 4 5 6 7 6 5 4 3 2 1 1 13 1 2 3 4 5 6 8 6 5 4 3 2 1 Output yes no no -----Explanation----- The first example satisfies all the conditions. The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end. The third one has no elements with value 7 after elements with value 6.
{"inputs": ["3\n19\n1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1\n14\n1 2 3 4 5 6 7 6 5 4 3 2 1 1\n13\n1 2 3 4 5 6 8 6 5 4 3 2 1"], "outputs": ["yes\nno\nno"]}
552
117
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef went to Australia and saw the destruction caused by bushfires, which made him sad, so he decided to help the animals by feeding them fruits. First, he went to purchase fruits from Pintu. Pintu sells $M$ different types of fruits (numbered $1$ through $M$). He sells them in $N$ baskets (numbered $1$ through $N$), where for each valid $i$, the $i$-th basket costs $P_{i}$ and it contains fruits of type $F_{i}$. Chef does not have too much money, so he cannot afford to buy everything; instead, he wants to choose one of the $M$ available types and purchase all the baskets containing fruits of that type. Help Chef choose the type of fruit to buy such that he buys at least one basket and the total cost of the baskets he buys is the smallest possible. ------ 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 $N$ and $M$. The second line contains $N$ space-separated integers $F_{1}, F_{2}, \ldots, F_{N}$. The third line contains $N$ space-separated integers $P_{1}, P_{2}, \ldots, P_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum price Chef must pay. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $1 ≤ M, N ≤ 50$ $1 ≤ F_{i} ≤ M$ for each valid $i$ $0 ≤ P_{i} ≤ 50$ for each valid $i$ ------ Subtasks ------ Subtask #1 (30 points): $M = 2$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 1 6 4 1 2 3 3 2 2 7 3 9 1 1 1 ----- Sample Output 1 ------ 5 ----- explanation 1 ------ Example case 1: - The sum of all baskets with fruits of type $1$ is $7$. - The sum of all baskets with fruits of type $2$ is $5$. - The sum of all baskets with fruits of type $3$ is $10$. - The sum of all baskets with fruits of type $4$ is $0$ because there are no such baskets. Chef can only choose fruits of type $1$, $2$ or $3$. Therefore, the minimum price he has to pay is $5$.
{"inputs": ["1\n6 4\n1 2 3 3 2 2\n7 3 9 1 1 1"], "outputs": ["5"]}
628
40
coding
Solve the programming task below in a Python markdown code block. Chef has recently learnt some new facts about the famous number π. For example, he was surprised that ordinary fractions are sometimes used to represent this number approximately. For example, 22/7, 355/113 or even 103993/33102. Soon, by calculating the value of 22/7 and 355/113 on paper Chef became quite disappointed because these values are not precise enough. For example, 22/7 differs in the third digit after the decimal point. So, these values are definitely should not be used for serious calculations. However, Chef doesn't know anything about 103993/33102. This fraction is quite inconvenient to calculate on paper. Chef is curious how precise this value is. So he asks you to help him and to calculate the first K digits after the decimal point of such an approximation of π. He consider this ordinary fraction as infinite decimal fraction so formally he asks you to calculate this approximation truncated to the first K digits after the decimal point. ------ Input ------ The first line of the input contains an integer T, denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a single integer K. ------ Output ------ For each test case output a single line containing the value of 103993/33102 truncated to the first K digits after the decimal point. Note that for K = 0 you should output just "3" without decimal point (quotes are for clarity). ------ Constraints ------ $0 ≤ K ≤ 10^{6}$ $1 ≤ T ≤ 2000$ $The sum of K over the input does not exceed 10^{6}$ ----- Sample Input 1 ------ 3 0 6 20 ----- Sample Output 1 ------ 3 3.141592 3.14159265301190260407 ----- explanation 1 ------ Example case 1. Here K = 0 so we don't need to output any digits after the decimal point. The decimal point itself also should not be output. Example case 2. Note that here we truncate (not round) the actual value of 103993/33102 to 6 digits after the decimal point. As you see from example case 3 rounded value here differs from truncated one. Example case 3. This example is only to show that this approximation of ? is also far from perfect :)
{"inputs": ["3\n1\n0\n0", "3\n1\n1\n0", "3\n2\n1\n0", "3\n2\n1\n1", "3\n0\n1\n1", "3\n1\n1\n1", "3\n1\n1\n2", "3\n1\n0\n1"], "outputs": ["3.1\n3\n3\n", "3.1\n3.1\n3\n", "3.14\n3.1\n3\n", "3.14\n3.1\n3.1\n", "3\n3.1\n3.1\n", "3.1\n3.1\n3.1\n", "3.1\n3.1\n3.14\n", "3.1\n3\n3.1\n"]}
566
181
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese and Russian. Little Elephant and his friends are going to a party. Each person has his own collection of T-Shirts. There are 100 different kind of T-Shirts. Each T-Shirt has a unique id between 1 and 100. No person has two T-Shirts of the same ID. They want to know how many arrangements are there in which no two persons wear same T-Shirt. One arrangement is considered different from another arrangement if there is at least one person wearing a different kind of T-Shirt in another arrangement. ------ Input ------ First line of the input contains a single integer T denoting number of test cases. Then T test cases follow. For each test case, first line contains an integer N, denoting the total number of persons. Each of the next N lines contains at least 1 and at most 100 space separated distinct integers, denoting the ID's of the T-Shirts i^{th} person has. ------ Output ------ For each test case, print in single line the required number of ways modulo 1000000007 = 10^{9}+7. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10$ ------ Example ------ Input: 2 2 3 5 8 100 3 5 100 1 2 5 100 Output: 4 4 ------ Explanation ------ For the first case, 4 possible ways are (3,8), (3,100), (5,8) and (5,100).For the second case, 4 possible ways are (5,2,100), (100,2,5), (1,2,100), and (1,2,5).
{"inputs": ["2\n2\n3 5\n8 100\n3\n5 100 1\n2\n5 100"], "outputs": ["4\n4"]}
411
44
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given two numbers arr1 and arr2 in base -2, return the result of adding them together. Each number is given in array format:  as an array of 0s and 1s, from most significant bit to least significant bit.  For example, arr = [1,1,0,1] represents the number (-2)^3 + (-2)^2 + (-2)^0 = -3.  A number arr in array, format is also guaranteed to have no leading zeros: either arr == [0] or arr[0] == 1. Return the result of adding arr1 and arr2 in the same format: as an array of 0s and 1s with no leading zeros.   Please complete the following python code precisely: ```python class Solution: def addNegabinary(self, arr1: List[int], arr2: List[int]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(arr1 = [1,1,1,1,1], arr2 = [1,0,1]) == [1,0,0,0,0]\n assert candidate(arr1 = [0], arr2 = [0]) == [0]\n assert candidate(arr1 = [0], arr2 = [1]) == [1]\n\n\ncheck(Solution().addNegabinary)"}
210
101
coding
Solve the programming task below in a Python markdown code block. Peter likes to travel by train. He likes it so much that on the train he falls asleep. Once in summer Peter was going by train from city A to city B, and as usual, was sleeping. Then he woke up, started to look through the window and noticed that every railway station has a flag of a particular colour. The boy started to memorize the order of the flags' colours that he had seen. But soon he fell asleep again. Unfortunately, he didn't sleep long, he woke up and went on memorizing the colours. Then he fell asleep again, and that time he slept till the end of the journey. At the station he told his parents about what he was doing, and wrote two sequences of the colours that he had seen before and after his sleep, respectively. Peter's parents know that their son likes to fantasize. They give you the list of the flags' colours at the stations that the train passes sequentially on the way from A to B, and ask you to find out if Peter could see those sequences on the way from A to B, or from B to A. Remember, please, that Peter had two periods of wakefulness. Peter's parents put lowercase Latin letters for colours. The same letter stands for the same colour, different letters — for different colours. Input The input data contains three lines. The first line contains a non-empty string, whose length does not exceed 105, the string consists of lowercase Latin letters — the flags' colours at the stations on the way from A to B. On the way from B to A the train passes the same stations, but in reverse order. The second line contains the sequence, written by Peter during the first period of wakefulness. The third line contains the sequence, written during the second period of wakefulness. Both sequences are non-empty, consist of lowercase Latin letters, and the length of each does not exceed 100 letters. Each of the sequences is written in chronological order. Output Output one of the four words without inverted commas: * «forward» — if Peter could see such sequences only on the way from A to B; * «backward» — if Peter could see such sequences on the way from B to A; * «both» — if Peter could see such sequences both on the way from A to B, and on the way from B to A; * «fantasy» — if Peter could not see such sequences. Examples Input atob a b Output forward Input aaacaaa aca aa Output both Note It is assumed that the train moves all the time, so one flag cannot be seen twice. There are no flags at stations A and B.
{"inputs": ["a\na\na\n", "a\na\n`\n", "ab\nb\na\n", "aab\nb\naa\n", "`ab\nb\naa\n", "atob\na\na\n", "`ab\nb\n`a\n", "atnb\na\na\n"], "outputs": ["fantasy\n", "fantasy\n", "backward\n", "backward\n", "fantasy\n", "fantasy\n", "fantasy\n", "fantasy\n"]}
583
111
coding
Solve the programming task below in a Python markdown code block. Salem gave you $n$ sticks with integer positive lengths $a_1, a_2, \ldots, a_n$. For every stick, you can change its length to any other positive integer length (that is, either shrink or stretch it). The cost of changing the stick's length from $a$ to $b$ is $|a - b|$, where $|x|$ means the absolute value of $x$. A stick length $a_i$ is called almost good for some integer $t$ if $|a_i - t| \le 1$. Salem asks you to change the lengths of some sticks (possibly all or none), such that all sticks' lengths are almost good for some positive integer $t$ and the total cost of changing is minimum possible. The value of $t$ is not fixed in advance and you can choose it as any positive integer. As an answer, print the value of $t$ and the minimum cost. If there are multiple optimal choices for $t$, print any of them. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 1000$) — the number of sticks. The second line contains $n$ integers $a_i$ ($1 \le a_i \le 100$) — the lengths of the sticks. -----Output----- Print the value of $t$ and the minimum possible cost. If there are multiple optimal choices for $t$, print any of them. -----Examples----- Input 3 10 1 4 Output 3 7 Input 5 1 1 2 2 3 Output 2 0 -----Note----- In the first example, we can change $1$ into $2$ and $10$ into $4$ with cost $|1 - 2| + |10 - 4| = 1 + 6 = 7$ and the resulting lengths $[2, 4, 4]$ are almost good for $t = 3$. In the second example, the sticks lengths are already almost good for $t = 2$, so we don't have to do anything.
{"inputs": ["1\n5\n", "1\n1\n", "1\n5\n", "1\n1\n", "1\n8\n", "1\n14\n", "2\n1 2\n", "2\n1 3\n"], "outputs": ["4 0\n", "1 0\n", "4 0\n", "1 0\n", "7 0\n", "13 0\n", "1 0\n", "2 0\n"]}
478
108
coding
Solve the programming task below in a Python markdown code block. The Fair Nut found a string $s$. The string consists of lowercase Latin letters. The Nut is a curious guy, so he wants to find the number of strictly increasing sequences $p_1, p_2, \ldots, p_k$, such that: For each $i$ ($1 \leq i \leq k$), $s_{p_i} =$ 'a'. For each $i$ ($1 \leq i < k$), there is such $j$ that $p_i < j < p_{i + 1}$ and $s_j =$ 'b'. The Nut is upset because he doesn't know how to find the number. Help him. This number should be calculated modulo $10^9 + 7$. -----Input----- The first line contains the string $s$ ($1 \leq |s| \leq 10^5$) consisting of lowercase Latin letters. -----Output----- In a single line print the answer to the problem — the number of such sequences $p_1, p_2, \ldots, p_k$ modulo $10^9 + 7$. -----Examples----- Input abbaa Output 5 Input baaaa Output 4 Input agaa Output 3 -----Note----- In the first example, there are $5$ possible sequences. $[1]$, $[4]$, $[5]$, $[1, 4]$, $[1, 5]$. In the second example, there are $4$ possible sequences. $[2]$, $[3]$, $[4]$, $[5]$. In the third example, there are $3$ possible sequences. $[1]$, $[3]$, $[4]$.
{"inputs": ["agaa\n", "bgaa\n", "aagb\n", "agab\n", "agbb\n", "bgbb\n", "bbbg\n", "gbbb\n"], "outputs": ["3", "2\n", "2\n", "2\n", "1\n", "0\n", "0\n", "0\n"]}
396
78
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese and Russian. You are given a multiset of N integers. Please find such a nonempty subset of it that the sum of the subset's elements is divisible by N. Otherwise, state that this subset doesn't exist. ------ 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 line of each test consists of a single integer N - the size of the multiset. The second line of each test contains N single space separated integers - the multiset's elements. ------ Output ------ For each test case output: -1 if the required subset doesn't exist If the required subset exists, output two lines. Output the size of the subset on the first line and output the list of indices of the multiset's element that form the required subset. Of course, any number can be taken in the subset no more than once. If there are several such subsets, you can output any. ------ Constraints ------ $1 ≤ The sum of N over all the test cases ≤ 10^{5}$ $Each element of the multiset is a positive integer, not exceeding 10^{9}.$ $1 ≤ N ≤ 15 : 37 points. $ $1 ≤ N ≤ 1000 : 24 points.$ $1 ≤ N ≤ 10^{5} : 39 points. $ ------ Example ------ Input: 1 3 4 6 10 Output: 1 2 ------ Explanation ------ We can pick {6} as the subset, then its sum is 6 and this is divisible by 3 - the size of the initial multiset.
{"inputs": ["1\n3\n4 4 4", "1\n3\n2 5 1", "1\n3\n4 6 4", "1\n3\n4 6 0", "1\n3\n4 6 3", "1\n3\n2 2 3", "1\n3\n4 4 0", "1\n3\n7 6 3"], "outputs": ["3\n1 2 3 \n", "2\n2 3 \n", "1\n2 \n", "1\n2 \n", "1\n2 \n", "1\n3 \n", "1\n3 \n", "1\n2 \n"]}
377
164
coding
Solve the programming task below in a Python markdown code block. As you may know, once some people pass their teens, they jokingly only celebrate their 20th or 21st birthday, forever. With some maths skills, that's totally possible - you only need to select the correct number base! For example, if they turn 32, that's exactly 20 - in base 16... Already 39? That's just 21, in base 19! Your task is to translate the given age to the much desired 20 (or 21) years, and indicate the number base, in the format specified below. **Note:** input will be always > 21 ### Examples: ``` 32 --> "32? That's just 20, in base 16!" 39 --> "39? That's just 21, in base 19!" ``` *Hint: if you don't know (enough) about [numeral systems](https://en.wikipedia.org/wiki/Numeral_system) and [radix](https://en.wikipedia.org/wiki/Radix), just observe the pattern!* --- ## My other katas If you enjoyed this kata then please try [my other katas](https://www.codewars.com/collections/katas-created-by-anter69)! :-) --- ### *Translations are welcome!* Also feel free to reuse/extend the following starter code: ```python def womens_age(n): ```
{"functional": "_inputs = [[32], [39], [22], [65], [83]]\n_outputs = [[\"32? That's just 20, in base 16!\"], [\"39? That's just 21, in base 19!\"], [\"22? That's just 20, in base 11!\"], [\"65? That's just 21, in base 32!\"], [\"83? That's just 21, in base 41!\"]]\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(womens_age(*i), o[0])"}
323
268
coding
Solve the programming task below in a Python markdown code block. In the evening, after the contest Ilya was bored, and he really felt like maximizing. He remembered that he had a set of n sticks and an instrument. Each stick is characterized by its length l_{i}. Ilya decided to make a rectangle from the sticks. And due to his whim, he decided to make rectangles in such a way that maximizes their total area. Each stick is used in making at most one rectangle, it is possible that some of sticks remain unused. Bending sticks is not allowed. Sticks with lengths a_1, a_2, a_3 and a_4 can make a rectangle if the following properties are observed: a_1 ≤ a_2 ≤ a_3 ≤ a_4 a_1 = a_2 a_3 = a_4 A rectangle can be made of sticks with lengths of, for example, 3 3 3 3 or 2 2 4 4. A rectangle cannot be made of, for example, sticks 5 5 5 7. Ilya also has an instrument which can reduce the length of the sticks. The sticks are made of a special material, so the length of each stick can be reduced by at most one. For example, a stick with length 5 can either stay at this length or be transformed into a stick of length 4. You have to answer the question — what maximum total area of the rectangles can Ilya get with a file if makes rectangles from the available sticks? -----Input----- The first line of the input contains a positive integer n (1 ≤ n ≤ 10^5) — the number of the available sticks. The second line of the input contains n positive integers l_{i} (2 ≤ l_{i} ≤ 10^6) — the lengths of the sticks. -----Output----- The first line of the output must contain a single non-negative integer — the maximum total area of the rectangles that Ilya can make from the available sticks. -----Examples----- Input 4 2 4 4 2 Output 8 Input 4 2 2 3 5 Output 0 Input 4 100003 100004 100005 100006 Output 10000800015
{"inputs": ["2\n2 3\n", "2\n2 3\n", "3\n2 3 5\n", "3\n2 3 5\n", "3\n2 3 4\n", "4\n2 4 4 2\n", "4\n2 2 3 5\n", "1\n1000000\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "8\n", "0\n", "0\n"]}
515
120
coding
Solve the programming task below in a Python markdown code block. Alex, Bob, and, Chef are standing on the coordinate plane. Chef is standing at the origin (coordinates (0, 0)) while the location of Alex and Bob are (X_{1}, Y_{1}) and (X_{2}, Y_{2}) respectively. Amongst Alex and Bob, find out who is at a farther distance from Chef or determine if both are at the same distance from Chef. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The first and only line of each test case contains four space-separated integers X_{1}, Y_{1}, X_{2}, and Y_{2} — the coordinates of Alex and Bob. ------ Output Format ------ For each test case, output on a new line: - ALEX, if Alex is at a farther distance from Chef. - BOB, if Bob is at a farther distance from Chef. - EQUAL, if both are standing at the same distance from Chef. You may print each character in uppercase or lowercase. For example, Bob, BOB, bob, and bOB, are all considered identical. ------ Constraints ------ $1 ≤ T ≤ 2000$ $-1000 ≤ X_{1}, Y_{1}, X_{2}, Y_{2} ≤ 1000$ ----- Sample Input 1 ------ 3 -1 0 3 4 3 4 -4 -3 8 -6 0 5 ----- Sample Output 1 ------ BOB EQUAL ALEX ----- explanation 1 ------ Test case $1$: Alex is at a distance $1$ from Chef while Bob is at distance $5$ from Chef. Thus, Bob is at a farther distance. Test case $2$: Alex is at a distance $5$ from Chef and Bob is also at distance $5$ from Chef. Thus, both are at the same distance. Test case $3$: Alex is at a distance $10$ from Chef while Bob is at distance $5$ from Chef. Thus, Alex is at a farther distance.
{"inputs": ["3\n-1 0 3 4\n3 4 -4 -3\n8 -6 0 5\n"], "outputs": ["BOB\nEQUAL\nALEX"]}
458
44
coding
Solve the programming task below in a Python markdown code block. Kundu is true tree lover. Tree is a connected graph having N vertices and N-1 edges. Today when he got a tree, he colored each edge with one of either red(r) or black(b) color. He is interested in knowing how many triplets(a,b,c) of vertices are there , such that, there is atleast one edge having red color on all the three paths i.e. from vertex a to b, vertex b to c and vertex c to a . Note that (a,b,c), (b,a,c) and all such permutations will be considered as the same triplet. If the answer is greater than $10^{9}$ + 7, print the answer modulo (%) $10^{9}$ + 7. Input Format The first line contains an integer N, i.e., the number of vertices in tree. The next N-1 lines represent edges: 2 space separated integers denoting an edge followed by a color of the edge. A color of an edge is denoted by a small letter of English alphabet, and it can be either red(r) or black(b). Output Format Print a single number i.e. the number of triplets. Constraints 1 ≤ N ≤ $10^{5}$ A node is numbered between 1 to N. Sample Input 5 1 2 b 2 3 r 3 4 r 4 5 b Sample Output 4 Explanation Given tree is something like this. (2,3,4) is one such triplet because on all paths i.e 2 to 3, 3 to 4 and 2 to 4 there is atleast one edge having red color. (2,3,5), (1,3,4) and (1,3,5) are other such triplets. Note that (1,2,3) is NOT a triplet, because the path from 1 to 2 does not have an edge with red color.
{"inputs": ["5\n1 2 b\n2 3 r\n3 4 r\n4 5 b\n"], "outputs": ["4\n"]}
432
34
coding
Solve the programming task below in a Python markdown code block. At the start of each season, every player in a football team is assigned their own unique squad number. Due to superstition or their history certain numbers are more desirable than others. Write a function generateNumber() that takes two arguments, an array of the current squad numbers (squad) and the new player's desired number (n). If the new player's desired number is not already taken, return n, else if the desired number can be formed by adding two digits between 1 and 9, return the number formed by joining these two digits together. E.g. If 2 is taken, return 11 because 1 + 1 = 2. Otherwise return null. Note: Often there will be several different ways to form a replacement number. In these cases the number with lowest first digit should be given priority. E.g. If n = 15, but squad already contains 15, return 69, not 78. Also feel free to reuse/extend the following starter code: ```python def generate_number(squad, n): ```
{"functional": "_inputs = [[[1, 2, 3, 4, 6, 9, 10, 15, 69], 11], [[1, 2, 3, 4, 6, 9, 10, 11, 15, 69], 11], [[1, 2, 3, 4, 6, 9, 10, 11, 15, 29, 69], 11], [[1, 2, 3, 4, 6, 9, 10, 11, 15, 29, 38, 47, 56, 65, 69, 74, 83, 92], 11], [[1, 2, 3, 4, 6, 9, 10, 11, 15, 18, 23, 69], 18], [[1, 2, 3, 4, 6, 9, 10, 15, 69], 34], [[1, 2, 3, 4, 6, 9, 10, 11, 15, 29, 34, 38, 47, 56, 65, 69, 74, 83, 92], 34], [[1, 2, 3, 4, 6, 9, 10, 11, 15, 18, 27, 29, 34, 36, 38, 45, 47, 54, 56, 63, 65, 69, 72, 74, 81, 83, 92], 9], [[1, 2, 3, 4, 6, 9, 10, 11, 15, 18, 27, 29, 34, 36, 38, 45, 47, 54, 56, 63, 65, 69, 72, 74, 81, 83, 90, 92], 9]]\n_outputs = [[11], [29], [38], [None], [99], [34], [None], [None], [None]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(generate_number(*i), o[0])"}
235
733
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an integer array nums and an integer k, return true if it is possible to divide this array into k non-empty subsets whose sums are all equal.   Please complete the following python code precisely: ```python class Solution: def canPartitionKSubsets(self, nums: List[int], k: int) -> bool: ```
{"functional": "def check(candidate):\n assert candidate( nums = [4, 3, 2, 3, 5, 2, 1], k = 4) == True\n\n\ncheck(Solution().canPartitionKSubsets)"}
86
58
coding
Solve the programming task below in a Python markdown code block. Three guys play a game: first, each person writes down $n$ distinct words of length $3$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have? -----Input----- The input consists of multiple test cases. The first line contains an integer $t$ ($1 \leq t \leq 100$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $n$ ($1 \leq n \leq 1000$) — the number of words written by each person. The following three lines each contain $n$ distinct strings — the words written by each person. Each string consists of $3$ lowercase English characters. -----Output----- For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the $i$-th integer should be the number of points earned by the $i$-th guy. -----Examples----- Input 3 1 abc def abc 3 orz for qaq qaq orz for cod for ces 5 iat roc hem ica lly bac ter iol ogi sts bac roc lly iol iat Output 1 3 1 2 2 6 9 11 5 -----Note----- In the first test case: The word ${abc}$ was written by the first and third guys — they each get $1$ point. The word ${def}$ was written by the second guy only — he gets $3$ points.
{"inputs": ["1\n1\niiy\niiy\niiy\n", "3\n1\nabc\ndef\nabc\n3\norz for qaq\nqaq orz for\ncod for ces\n5\niat roc hem ica lly\nbac ter iol ogi sts\nbac roc lly iol iat\n"], "outputs": ["0 0 0 \n", "1 3 1 \n2 2 6 \n9 11 5 \n"]}
417
111
coding
Solve the programming task below in a Python markdown code block. There are two popular keyboard layouts in Berland, they differ only in letters positions. All the other keys are the same. In Berland they use alphabet with 26 letters which coincides with English alphabet. You are given two strings consisting of 26 distinct letters each: all keys of the first and the second layouts in the same order. You are also given some text consisting of small and capital English letters and digits. It is known that it was typed in the first layout, but the writer intended to type it in the second layout. Print the text if the same keys were pressed in the second layout. Since all keys but letters are the same in both layouts, the capitalization of the letters should remain the same, as well as all other characters. -----Input----- The first line contains a string of length 26 consisting of distinct lowercase English letters. This is the first layout. The second line contains a string of length 26 consisting of distinct lowercase English letters. This is the second layout. The third line contains a non-empty string s consisting of lowercase and uppercase English letters and digits. This is the text typed in the first layout. The length of s does not exceed 1000. -----Output----- Print the text if the same keys were pressed in the second layout. -----Examples----- Input qwertyuiopasdfghjklzxcvbnm veamhjsgqocnrbfxdtwkylupzi TwccpQZAvb2017 Output HelloVKCup2017 Input mnbvcxzlkjhgfdsapoiuytrewq asdfghjklqwertyuiopzxcvbnm 7abaCABAABAcaba7 Output 7uduGUDUUDUgudu7
{"inputs": ["ayvguplhjsoiencbkxdrfwmqtz\nkhzvtbspcndierqumlojyagfwx\n3\n", "ayvguplhjsoiencbkxdrfwmqtz\nkhzvtbspcndierqumlojyagfwx\n3\n", "ayvguplhjsoiebcnkxdrfwmqtz\nkhzvtbspcndierqumlojyagfwx\n3\n", "ayvguplhjsoiebcnkxdrfwmqtz\nkhzvtbspcndierqumlojyagfwx\n1\n", "ayvguplhjsoiebcnkxdrfwmqtz\nkhzvtbspcndierqumlojyagfwx\n0\n", "agvyuolhjroiebcnkwerfwmqty\nkhyvtbspdndierqumlowzagfjx\n2\n", "ayvguplhjsoiebcnkxdrfwmqtz\nkhzvtbspdndierqumlojyagfwx\n0\n", "ayvguolhjsoiebcnkxdrfwmqtz\nkhzvtbspdndierqumlojyagfwx\n0\n"], "outputs": ["3\n", "3\n", "3\n", "1\n", "0\n", "2\n", "0\n", "0\n"]}
389
325
coding
Solve the programming task below in a Python markdown code block. The well-known Fibonacci sequence $F_0, F_1, F_2,\ldots $ is defined as follows: $F_0 = 0, F_1 = 1$. For each $i \geq 2$: $F_i = F_{i - 1} + F_{i - 2}$. Given an increasing arithmetic sequence of positive integers with $n$ elements: $(a, a + d, a + 2\cdot d,\ldots, a + (n - 1)\cdot d)$. You need to find another increasing arithmetic sequence of positive integers with $n$ elements $(b, b + e, b + 2\cdot e,\ldots, b + (n - 1)\cdot e)$ such that: $0 < b, e < 2^{64}$, for all $0\leq i < n$, the decimal representation of $a + i \cdot d$ appears as substring in the last $18$ digits of the decimal representation of $F_{b + i \cdot e}$ (if this number has less than $18$ digits, then we consider all its digits). -----Input----- The first line contains three positive integers $n$, $a$, $d$ ($1 \leq n, a, d, a + (n - 1) \cdot d < 10^6$). -----Output----- If no such arithmetic sequence exists, print $-1$. Otherwise, print two integers $b$ and $e$, separated by space in a single line ($0 < b, e < 2^{64}$). If there are many answers, you can output any of them. -----Examples----- Input 3 1 1 Output 2 1 Input 5 1 2 Output 19 5 -----Note----- In the first test case, we can choose $(b, e) = (2, 1)$, because $F_2 = 1, F_3 = 2, F_4 = 3$. In the second test case, we can choose $(b, e) = (19, 5)$ because: $F_{19} = 4181$ contains $1$; $F_{24} = 46368$ contains $3$; $F_{29} = 514229$ contains $5$; $F_{34} = 5702887$ contains $7$; $F_{39} = 63245986$ contains $9$.
{"inputs": ["3 1 1\n", "5 1 2\n", "2 1 1\n", "2 2 1\n", "2 2 1\n", "5 1 2\n", "2 1 1\n", "3 1 1\n"], "outputs": ["4417573500000000001 4417573500000000000\n", "4417573500000000001 8835147000000000000\n", "4417573500000000001 4417573500000000000\n", "8835147000000000001 4417573500000000000\n", "8835147000000000001 4417573500000000000\n", "4417573500000000001 8835147000000000000\n", "4417573500000000001 4417573500000000000\n", "4417573500000000001 4417573500000000000\n"]}
585
406
coding
Solve the programming task below in a Python markdown code block. Polycarp calls an array dense if the greater of any two adjacent elements is not more than twice bigger than the smaller. More formally, for any $i$ ($1 \le i \le n-1$), this condition must be satisfied: $$\frac{\max(a[i], a[i+1])}{\min(a[i], a[i+1])} \le 2$$ For example, the arrays $[1, 2, 3, 4, 3]$, $[1, 1, 1]$ and $[5, 10]$ are dense. And the arrays $[5, 11]$, $[1, 4, 2]$, $[6, 6, 1]$ are not dense. You are given an array $a$ of $n$ integers. What is the minimum number of numbers you need to add to an array to make it dense? You can insert numbers anywhere in the array. If the array is already dense, no numbers need to be added. For example, if $a=[4,2,10,1]$, then the answer is $5$, and the array itself after inserting elements into it may look like this: $a=[4,2,\underline{\textbf{3}},\underline{\textbf{5}},10,\underline{\textbf{6}},\underline{\textbf{4}},\underline{\textbf{2}},1]$ (there are other ways to build such $a$). -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$). Then $t$ test cases follow. The first line of each test case contains one integer $n$ ($2 \le n \le 50$) — the length of the array $a$. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 50$). -----Output----- For each test case, output one integer — the minimum number of numbers that must be added to the array to make it dense. -----Examples----- Input 6 4 4 2 10 1 2 1 3 2 6 1 3 1 4 2 5 1 2 3 4 3 12 4 31 25 50 30 20 34 46 42 16 15 16 Output 5 1 2 1 0 3 -----Note----- The first test case is explained in the statements. In the second test case, you can insert one element, $a=[1,\underline{\textbf{2}},3]$. In the third test case, you can insert two elements, $a=[6,\underline{\textbf{4}},\underline{\textbf{2}},1]$. In the fourth test case, you can insert one element, $a=[1,\underline{\textbf{2}},4,2]$. In the fifth test case, the array $a$ is already dense.
{"inputs": ["6\n4\n4 2 10 1\n2\n1 3\n2\n6 1\n3\n1 4 2\n5\n1 2 3 4 3\n12\n4 27 9 1 30 20 34 46 42 16 3 16\n", "6\n4\n4 2 10 1\n2\n1 3\n2\n6 1\n3\n2 4 2\n5\n1 2 3 4 3\n12\n4 27 9 1 30 20 34 46 42 16 3 16\n", "6\n4\n4 2 10 1\n2\n1 3\n2\n6 1\n3\n2 4 2\n5\n1 2 3 4 3\n12\n4 27 9 1 30 20 54 46 42 16 3 16\n", "6\n4\n4 3 10 1\n2\n1 3\n2\n6 1\n3\n1 4 3\n5\n1 1 3 4 3\n12\n4 31 3 50 2 4 34 46 42 16 15 21\n", "6\n4\n4 3 10 1\n2\n1 3\n2\n6 1\n3\n1 5 3\n5\n1 1 3 4 3\n12\n4 31 3 50 2 4 34 46 42 16 15 21\n", "6\n4\n4 4 10 1\n2\n1 3\n2\n6 1\n3\n1 4 2\n5\n1 2 3 4 3\n12\n8 2 25 3 30 20 34 46 42 16 3 16\n", "6\n4\n4 2 10 1\n2\n1 3\n2\n6 1\n3\n1 4 2\n5\n1 2 3 4 3\n12\n4 43 9 1 30 20 34 46 42 16 3 16\n", "6\n4\n4 2 10 1\n2\n1 3\n2\n6 1\n3\n2 4 2\n5\n1 2 3 4 3\n12\n4 27 9 1 30 20 34 46 42 22 3 16\n"], "outputs": ["5\n1\n2\n1\n0\n15\n", "5\n1\n2\n0\n0\n15\n", "5\n1\n2\n0\n0\n16\n", "4\n1\n2\n1\n1\n17\n", "4\n1\n2\n2\n1\n17\n", "4\n1\n2\n1\n0\n15\n", "5\n1\n2\n1\n0\n17\n", "5\n1\n2\n0\n0\n14\n"]}
688
782
coding
Solve the programming task below in a Python markdown code block. The brothers Hiroshi and Kenjiro came to Lake Inawashiro for fishing. The two decided to score as follows and compete with the total score of the fish they caught. * One char is a point * One yamame trout b points * Add c points for every 10 chars * Add d points for every 20 yamame trout Create a program to determine which one wins or draws based on the number of fish caught by Hiroshi and Kenjiro. Input The input is given in the following format. h1 h2 k1 k2 a b c d The first line gives the number of chars h1 (0 ≤ h1 ≤ 100) and the number of yamame trout h2 (0 ≤ h2 ≤ 100) caught by Hiroshi. The second line gives the number of chars k1 (0 ≤ k1 ≤ 100) and the number of yamame trout k2 (0 ≤ k2 ≤ 100) caught by Kenjiro. On the third line, the score for each char is a (1 ≤ a ≤ 100), the score for each yamame trout is b (1 ≤ b ≤ 100), and the additional score for every 10 chars is c (0 ≤ c ≤ 100). ), An additional score d (0 ≤ d ≤ 100) is given for every 20 chars. Output If Hiro wins, hiroshi, if Kenjiro wins, kenjiro, and if a tie, even, output on one line. Examples Input 5 1 3 1 1 2 5 5 Output hiroshi Input 5 1 4 2 1 2 5 5 Output kenjiro Input 0 20 10 0 1 1 10 0 Output even
{"inputs": ["9 1\n3 1\n1 2 5 5", "5 1\n4 1\n1 2 5 5", "9 1\n3 1\n1 2 5 3", "5 1\n4 1\n1 2 5 1", "9 2\n3 1\n1 2 5 3", "5 1\n4 1\n1 2 5 0", "9 2\n2 1\n1 2 5 3", "5 1\n4 1\n1 0 5 0"], "outputs": ["hiroshi\n", "hiroshi\n", "hiroshi\n", "hiroshi\n", "hiroshi\n", "hiroshi\n", "hiroshi\n", "hiroshi\n"]}
430
182
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a 0-indexed string s and a dictionary of words dictionary. You have to break s into one or more non-overlapping substrings such that each substring is present in dictionary. There may be some extra characters in s which are not present in any of the substrings. Return the minimum number of extra characters left over if you break up s optimally.   Please complete the following python code precisely: ```python class Solution: def minExtraChar(self, s: str, dictionary: List[str]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(s = \"leetscode\", dictionary = [\"leet\",\"code\",\"leetcode\"]) == 1\n assert candidate(s = \"sayhelloworld\", dictionary = [\"hello\",\"world\"]) == 3\n\n\ncheck(Solution().minExtraChar)"}
129
68
coding
Solve the programming task below in a Python markdown code block. Suppose you have a string S which has length N and is indexed from 0 to N−1. String R is the reverse of the string S. The string S is funny if the condition |Si−Si−1|=|Ri−Ri−1| is true for every i from 1 to N−1. (Note: Given a string str, stri denotes the ascii value of the ith character (0-indexed) of str. |x| denotes the absolute value of an integer x) SAMPLE INPUT 2 acxz bcxz SAMPLE OUTPUT Funny Not Funny Explanation Consider the 1st testcase acxz : c-a = x-z = 2 z-x = a-c = 2 Consider the 2st testcase bcxz |c-b| != |x-z|
{"inputs": ["2\nacxz\nbcxz", "2\nivvkxq\nivvkx", "10\njkotzxzxrxtzytlruwrxytyzsuzytwyzxuzytryzuzysxvsmupouysywywqlhg\neklrywzvpxtvoptlrskmskszvwzsuzxrtvyzwruqvyxusqwupnurqmtltnltsmuyxqoksyurpwqpv\nefhpuvqvnuwpvwysvnunostvpqvxtxsvqwqvsxtxvqpvtsonunvsywvpwunvqvupxzy\notytmpszumnryqvxpvnvxyvpvprumnvsqwqwtsqyqksqvnuqpxszwzsxsx\nbhmptlqswsvoqsvzyzwoqtvowpyqxpwurpxutswtrpwzvrpkswzuo\nrvovprxzvwrxpwpzsltzutxztrxqxt\nceiosyrtztvnqsuozrxvtqywqwyrxtnjh\ndjnsyzxszryqworuxpqvqwquvotzsqvoupwvztzupowtqnvpxqyrwutzuys\nkovzuywsuvwxuxtwzryzuxyvouvyskoqtwryszxqqxzsyrwtqoksyvuovyxuzyrzwtxuxwvuswuqvryu\nptvzstvotxqyvzrwyqryzrpkswzryupwutmigc"], "outputs": ["Funny\nNot Funny", "Not Funny\nNot Funny", "Funny\nNot Funny\nFunny\nFunny\nNot Funny\nFunny\nNot Funny\nNot Funny\nFunny\nFunny"]}
188
400
coding
Solve the programming task below in a Python markdown code block. A sequence of positive integers is called great for a positive integer $x$, if we can split it into pairs in such a way that in each pair the first number multiplied by $x$ is equal to the second number. More formally, a sequence $a$ of size $n$ is great for a positive integer $x$, if $n$ is even and there exists a permutation $p$ of size $n$, such that for each $i$ ($1 \le i \le \frac{n}{2}$) $a_{p_{2i-1}} \cdot x = a_{p_{2i}}$. Sam has a sequence $a$ and a positive integer $x$. Help him to make the sequence great: find the minimum possible number of positive integers that should be added to the sequence $a$ to make it great for the number $x$. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 20000$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$, $x$ ($1 \le n \le 2 \cdot 10^5$, $2 \le x \le 10^6$). The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case print a single integer — the minimum number of integers that can be added to the end of $a$ to make it a great sequence for the number $x$. -----Examples----- Input 4 4 4 1 16 4 4 6 2 1 2 2 2 4 7 5 3 5 2 3 5 15 9 10 10 10 10 20 1 100 200 2000 3 Output 0 2 3 3 -----Note----- In the first test case, Sam got lucky and the sequence is already great for the number $4$ because you can divide it into such pairs: $(1, 4)$, $(4, 16)$. Thus we can add $0$ numbers. In the second test case, you can add numbers $1$ and $14$ to the sequence, then you can divide all $8$ integers into such pairs: $(1, 2)$, $(1, 2)$, $(2, 4)$, $(7, 14)$. It is impossible to add less than $2$ integers to fix the sequence.
{"inputs": ["1\n2 42951\n100000 132704\n", "1\n2 8\n600349032 507824960\n", "1\n2 50000\n90000 205032704\n", "1\n2 70000\n70000 605032704\n", "1\n2 800000\n800000 49872896\n", "1\n2 5\n705032704 1000000000\n", "1\n2 444\n902178848 98765432\n", "1\n2 500\n10000000 705032704\n"], "outputs": ["2\n", "2\n", "2\n", "2\n", "2\n", "2\n", "2\n", "2\n"]}
630
264
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given the head of a linked list with n nodes. For each node in the list, find the value of the next greater node. That is, for each node, find the value of the first node that is next to it and has a strictly larger value than it. Return an integer array answer where answer[i] is the value of the next greater node of the ith node (1-indexed). If the ith node does not have a next greater node, set answer[i] = 0.   Please complete the following python code precisely: ```python # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def nextLargerNodes(self, head: Optional[ListNode]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(head = list_node([2,1,5])) == [5,5,0]\n assert candidate(head = list_node([2,7,4,3,5])) == [7,0,5,5,0]\n\n\ncheck(Solution().nextLargerNodes)"}
195
75
coding
Solve the programming task below in a Python markdown code block. Chef has a number N, Cheffina challenges the chef to check the divisibility of all the permutation of N by 3. If any of the permutations is divisible by 3 then print 1 else print 0. -----Input:----- - First-line will contain $T$, the number of test cases. Then the test cases follow. - Each test case contains a single line of input, two integers $N$. -----Output:----- For each test case, output in a single line answer 1 or 0. -----Constraints----- - $1 \leq T \leq 10^6$ - $1 \leq N \leq 10^6$ -----Sample Input:----- 2 18 308 -----Sample Output:----- 1 0
{"inputs": ["2\n18\n308"], "outputs": ["1\n0"]}
179
21
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a string s. Return all the words vertically in the same order in which they appear in s. Words are returned as a list of strings, complete with spaces when is necessary. (Trailing spaces are not allowed). Each word would be put on only one column and that in one column there will be only one word.   Please complete the following python code precisely: ```python class Solution: def printVertically(self, s: str) -> List[str]: ```
{"functional": "def check(candidate):\n assert candidate(s = \"HOW ARE YOU\") == [\"HAY\",\"ORO\",\"WEU\"]\n assert candidate(s = \"TO BE OR NOT TO BE\") == [\"TBONTB\",\"OEROOE\",\" T\"]\n assert candidate(s = \"CONTEST IS COMING\") == [\"CIC\",\"OSO\",\"N M\",\"T I\",\"E N\",\"S G\",\"T\"]\n\n\ncheck(Solution().printVertically)"}
118
110
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a string s, return true if s is a good string, or false otherwise. A string s is good if all the characters that appear in s have the same number of occurrences (i.e., the same frequency).   Please complete the following python code precisely: ```python class Solution: def areOccurrencesEqual(self, s: str) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(s = \"abacbc\") == True\n assert candidate(s = \"aaabb\") == False\n\n\ncheck(Solution().areOccurrencesEqual)"}
93
45
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Mandarin], [Russian], and [Vietnamese] as well. You are given a sequence of integers A_{1}, A_{2}, \ldots, A_{N} and an integer K. Find the number of contiguous subsequences A_{L}, A_{L+1}, \ldots, A_{R} such that R-L+1 ≥ K and the K-th element of the subsequence (A_{L+K-1}) is equal to the maximum of all elements of the entire sequence. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer --- the number of contiguous subsequences satisfying the given conditions. ------ Constraints ------ $1 ≤T ≤200$ $1 ≤K ≤N ≤2 \cdot 10^{5}$ $|A_{i}| ≤10^{5}$ for each valid $i$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask #1 (10 points) $T ≤10$ $N ≤100$ Subtask #2 (90 points) original constraints ----- Sample Input 1 ------ 1 5 3 1 2 3 4 5 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example case 1: $(3, 4, 5)$ is the only contiguous subsequence such that its $3$-rd element is equal to the maximum of the whole sequence (which is $5$).
{"inputs": ["1\n5 3\n1 2 3 4 5"], "outputs": ["1"]}
425
26
coding
Solve the programming task below in a Python markdown code block. For integers b (b \geq 2) and n (n \geq 1), let the function f(b,n) be defined as follows: * f(b,n) = n, when n < b * f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b), when n \geq b Here, {\rm floor}(n / b) denotes the largest integer not exceeding n / b, and n \ {\rm mod} \ b denotes the remainder of n divided by b. Less formally, f(b,n) is equal to the sum of the digits of n written in base b. For example, the following hold: * f(10,\,87654)=8+7+6+5+4=30 * f(100,\,87654)=8+76+54=138 You are given integers n and s. Determine if there exists an integer b (b \geq 2) such that f(b,n)=s. If the answer is positive, also find the smallest such b. Constraints * 1 \leq n \leq 10^{11} * 1 \leq s \leq 10^{11} * n,\,s are integers. Input The input is given from Standard Input in the following format: n s Output If there exists an integer b (b \geq 2) such that f(b,n)=s, print the smallest such b. If such b does not exist, print `-1` instead. Examples Input 87654 30 Output 10 Input 87654 138 Output 100 Input 87654 45678 Output -1 Input 31415926535 1 Output 31415926535 Input 1 31415926535 Output -1
{"inputs": ["87654\n9", "87654\n8", "87654\n1", "87654\n2", "87654\n3", "87654\n4", "87654\n7", "87654\n5"], "outputs": ["2\n", "3\n", "87654\n", "43827\n", "29218\n", "43826\n", "12522\n", "87650\n"]}
468
134
coding
Solve the programming task below in a Python markdown code block. Almir had a small sequence $A_1, A_2, \ldots, A_N$. He decided to make $K$ copies of this sequence and concatenate them, forming a sequence $X_1, X_2, \ldots, X_{NK}$; for each valid $i$ and $j$ ($0 \le j < K$), $X_{j \cdot N + i} = A_i$. For example, if $A = (1, 2, 3)$ and $K = 4$, the final sequence is $X = (1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3)$. A pair $(i, j)$, where $1 \le i < j \le N$, is an inversion if $X_i > X_j$. Find the number of inversions in the final sequence $X$. -----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 $N$ and $K$. - The second line contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$. -----Output----- For each test case, print a single line containing one integer ― the number of inversions in the sequence $X$. -----Constraints----- - $1 \le T \le 1,000$ - $1 \le N \le 100$ - $1 \le K \le 10^6$ - $1 \le A_i \le 10^9$ for each valid $i$ -----Subtasks----- Subtask #1 (100 points): original constraints -----Example Input----- 2 3 3 2 1 3 4 100 99 2 1000 24 -----Example Output----- 12 30000
{"inputs": ["2\n3 3\n2 1 3\n4 100\n99 2 1000 24\n"], "outputs": ["12\n30000"]}
459
49
coding
Solve the programming task below in a Python markdown code block. $2^n$ teams participate in a playoff tournament. The tournament consists of $2^n - 1$ games. They are held as follows: in the first phase of the tournament, the teams are split into pairs: team $1$ plays against team $2$, team $3$ plays against team $4$, and so on (so, $2^{n-1}$ games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only $2^{n-1}$ teams remain. If only one team remains, it is declared the champion; otherwise, the second phase begins, where $2^{n-2}$ games are played: in the first one of them, the winner of the game "$1$ vs $2$" plays against the winner of the game "$3$ vs $4$", then the winner of the game "$5$ vs $6$" plays against the winner of the game "$7$ vs $8$", and so on. This process repeats until only one team remains. The skill level of the $i$-th team is $p_i$, where $p$ is a permutation of integers $1$, $2$, ..., $2^n$ (a permutation is an array where each element from $1$ to $2^n$ occurs exactly once). You are given a string $s$ which consists of $n$ characters. These characters denote the results of games in each phase of the tournament as follows: if $s_i$ is equal to 0, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the lower skill level wins; if $s_i$ is equal to 1, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the higher skill level wins. Let's say that an integer $x$ is winning if it is possible to find a permutation $p$ such that the team with skill $x$ wins the tournament. Find all winning integers. -----Input----- The first line contains one integer $n$ ($1 \le n \le 18$). The second line contains the string $s$ of length $n$ consisting of the characters 0 and/or 1. -----Output----- Print all the winning integers $x$ in ascending order. -----Examples----- Input 3 101 Output 4 5 6 7 Input 1 1 Output 2 Input 2 01 Output 2 3 -----Note----- None
{"inputs": ["1\n1\n", "1\n0\n", "2\n01\n", "2\n00\n", "2\n11\n", "3\n101\n", "3\n000\n", "3\n010\n"], "outputs": ["2 ", "1 ", "2 3 ", "1 ", "4 ", "4 5 6 7 ", "1 ", "2 3 4 5 "]}
574
102
coding
Solve the programming task below in a Python markdown code block. Little beaver is a beginner programmer, so informatics is his favorite subject. Soon his informatics teacher is going to have a birthday and the beaver has decided to prepare a present for her. He planted n flowers in a row on his windowsill and started waiting for them to grow. However, after some time the beaver noticed that the flowers stopped growing. The beaver thinks it is bad manners to present little flowers. So he decided to come up with some solutions. There are m days left to the birthday. The height of the i-th flower (assume that the flowers in the row are numbered from 1 to n from left to right) is equal to a_{i} at the moment. At each of the remaining m days the beaver can take a special watering and water w contiguous flowers (he can do that only once at a day). At that each watered flower grows by one height unit on that day. The beaver wants the height of the smallest flower be as large as possible in the end. What maximum height of the smallest flower can he get? -----Input----- The first line contains space-separated integers n, m and w (1 ≤ w ≤ n ≤ 10^5; 1 ≤ m ≤ 10^5). The second line contains space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9). -----Output----- Print a single integer — the maximum final height of the smallest flower. -----Examples----- Input 6 2 3 2 2 2 2 1 1 Output 2 Input 2 5 1 5 8 Output 9 -----Note----- In the first sample beaver can water the last 3 flowers at the first day. On the next day he may not to water flowers at all. In the end he will get the following heights: [2, 2, 2, 3, 2, 2]. The smallest flower has height equal to 2. It's impossible to get height 3 in this test.
{"inputs": ["1 1 1\n1\n", "1 1 1\n1\n", "1 1 1\n0\n", "1 0 1\n2\n", "1 2 1\n1\n", "1 0 1\n1\n", "2 5 1\n5 8\n", "2 5 1\n8 8\n"], "outputs": ["2\n", "2\n", "1\n", "2\n", "3\n", "1\n", "9\n", "10\n"]}
451
123
coding
Solve the programming task below in a Python markdown code block. Welcome to the exciting class of Professor Manasa. In each lecture she used to play some game while teaching a new concept. Today's topic is Set Theory. For today's game, she had given a set A = {a_{1}, a_{2}, ...a_{N}} of N integers to her students and asked them to play the game as follows. At each step of the game she calls a random student and asks him/her to select a non-empty subset from set A such that this subset had not been selected earlier and the sum of subset should be even. This game ends when all possible subsets had been selected. Manasa needs your help in counting the total number of times students can be called assuming each student gives the right answer. While it is given that if two numbers are same in the given set, they have different colors. It means that if a1 = a2, then choosing a1 and choosing a2 will be considered as different sets. Note Two subsets are different if there exists an element (a_{k}) that is present in one subset but not in other. Let's say set A = {a_{1}, a_{2}, a_{3}} = {2, 2, 3}, then all possible subsets are {}, {a_{1}}, {a_{2}}, {a_{3}}, {a_{1}, a_{2}}, {a_{1}, a_{3}}, {a_{2}, a_{3}}, {a_{1}, a_{2}, a_{3}} which is equivalent to {}, {2}, {2}, {3}, {2, 2}, {2, 3}, {2, 3}, {2, 2, 3}. Students can be called multiple times. Input Format The first line contains an integer N i.e. size of set A. Next line will contain N integers, each representing an element of A. Output Format Print number of time students are called. As this number can be very large you have to print the answer modulo (10^{9} + 7). Constraints 1 ≤ N ≤ 10^{5} 0 ≤ a_{i} ≤ 10^{4} , where i ∈ [1 .. N] Sample Input 00 4 2 4 6 1 Sample Output 00 7 Sample Input 01 3 1 2 2 Sample Output 01 3 Explanation There are 7 different ways in which a non-empty subset, with even sum, can be selected, i.e., {2}, {4}, {6}, {2, 4}, {2, 6}, {4, 6}, {2, 4, 6}. For second sample test case, there are 3 different ways in which a non-empty subset, with even sum, can be selected, i.e., {a_{2}}, {a_{3}}, {a_{2}, a_{3}} which is equivalent to {2}, {2}, {2,2}.
{"inputs": ["3\n1 2 2\n", "4\n2 4 6 1\n"], "outputs": ["3\n", "7\n"]}
656
36
coding
Solve the programming task below in a Python markdown code block. Let's define $f(x)$ for a positive integer $x$ as the length of the base-10 representation of $x$ without leading zeros. I like to call it a digital logarithm. Similar to a digital root, if you are familiar with that. You are given two arrays $a$ and $b$, each containing $n$ positive integers. In one operation, you do the following: pick some integer $i$ from $1$ to $n$; assign either $f(a_i)$ to $a_i$ or $f(b_i)$ to $b_i$. Two arrays are considered similar to each other if you can rearrange the elements in both of them, so that they are equal (e. g. $a_i = b_i$ for all $i$ from $1$ to $n$). What's the smallest number of operations required to make $a$ and $b$ similar to each other? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of elements in each of the arrays. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i < 10^9$). The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_j < 10^9$). The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print the smallest number of operations required to make $a$ and $b$ similar to each other. -----Examples----- Input 4 1 1 1000 4 1 2 3 4 3 1 4 2 3 2 9 3 1 100 9 10 75019 709259 5 611271314 9024533 81871864 9 3 6 4865 9503 2 371245467 6 7 37376159 8 364036498 52295554 169 Output 2 0 2 18 -----Note----- In the first testcase, you can apply the digital logarithm to $b_1$ twice. In the second testcase, the arrays are already similar to each other. In the third testcase, you can first apply the digital logarithm to $a_1$, then to $b_2$.
{"inputs": ["4\n1\n1\n1000\n4\n1 2 3 4\n3 1 4 2\n3\n2 9 3\n1 100 9\n10\n75019 709259 5 611271314 9024533 81871864 9 3 6 4865\n9503 2 371245467 6 7 37376159 8 364036498 52295554 169\n"], "outputs": ["2\n0\n2\n18\n"]}
646
175
coding
Solve the programming task below in a Python markdown code block. Tattah is asleep if and only if Tattah is attending a lecture. This is a well-known formula among Tattah's colleagues. On a Wednesday afternoon, Tattah was attending Professor HH's lecture. At 12:21, right before falling asleep, he was staring at the digital watch around Saher's wrist. He noticed that the digits on the clock were the same when read from both directions i.e. a palindrome. In his sleep, he started dreaming about such rare moments of the day when the time displayed on a digital clock is a palindrome. As soon as he woke up, he felt destined to write a program that finds the next such moment. However, he still hasn't mastered the skill of programming while sleeping, so your task is to help him. Input The first and only line of the input starts with a string with the format "HH:MM" where "HH" is from "00" to "23" and "MM" is from "00" to "59". Both "HH" and "MM" have exactly two digits. Output Print the palindromic time of day that comes soonest after the time given in the input. If the input time is palindromic, output the soonest palindromic time after the input time. Examples Input 12:21 Output 13:31 Input 23:59 Output 00:00
{"inputs": ["04:50\n", "02:17\n", "12:14\n", "03:44\n", "00:17\n", "15:51\n", "23:31\n", "07:56\n"], "outputs": ["05:50\n", "02:20\n", "12:21\n", "04:40\n", "01:10\n", "20:02\n", "23:32\n", "10:01\n"]}
320
134
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an array of strings names, and an array heights that consists of distinct positive integers. Both arrays are of length n. For each index i, names[i] and heights[i] denote the name and height of the ith person. Return names sorted in descending order by the people's heights.   Please complete the following python code precisely: ```python class Solution: def sortPeople(self, names: List[str], heights: List[int]) -> List[str]: ```
{"functional": "def check(candidate):\n assert candidate(names = [\"Mary\",\"John\",\"Emma\"], heights = [180,165,170]) == [\"Mary\",\"Emma\",\"John\"]\n assert candidate(names = [\"Alice\",\"Bob\",\"Bob\"], heights = [155,185,150]) == [\"Bob\",\"Alice\",\"Bob\"]\n\n\ncheck(Solution().sortPeople)"}
115
99
coding
Solve the programming task below in a Python markdown code block. Alice and Bob decided to eat some fruit. In the kitchen they found a large bag of oranges and apples. Alice immediately took an orange for herself, Bob took an apple. To make the process of sharing the remaining fruit more fun, the friends decided to play a game. They put multiple cards and on each one they wrote a letter, either 'A', or the letter 'B'. Then they began to remove the cards one by one from left to right, every time they removed a card with the letter 'A', Alice gave Bob all the fruits she had at that moment and took out of the bag as many apples and as many oranges as she had before. Thus the number of oranges and apples Alice had, did not change. If the card had written letter 'B', then Bob did the same, that is, he gave Alice all the fruit that he had, and took from the bag the same set of fruit. After the last card way removed, all the fruit in the bag were over. You know how many oranges and apples was in the bag at first. Your task is to find any sequence of cards that Alice and Bob could have played with. Input The first line of the input contains two integers, x, y (1 ≤ x, y ≤ 1018, xy > 1) — the number of oranges and apples that were initially in the bag. Output Print any sequence of cards that would meet the problem conditions as a compressed string of characters 'A' and 'B. That means that you need to replace the segments of identical consecutive characters by the number of repetitions of the characters and the actual character. For example, string AAABAABBB should be replaced by string 3A1B2A3B, but cannot be replaced by 2A1A1B2A3B or by 3AB2A3B. See the samples for clarifications of the output format. The string that you print should consist of at most 106 characters. It is guaranteed that if the answer exists, its compressed representation exists, consisting of at most 106 characters. If there are several possible answers, you are allowed to print any of them. If the sequence of cards that meet the problem statement does not not exist, print a single word Impossible. Examples Input 1 4 Output 3B Input 2 2 Output Impossible Input 3 2 Output 1A1B Note In the first sample, if the row contained three cards with letter 'B', then Bob should give one apple to Alice three times. So, in the end of the game Alice has one orange and three apples, and Bob has one apple, in total it is one orange and four apples. In second sample, there is no answer since one card is not enough for game to finish, and two cards will produce at least three apples or three oranges. In the third sample, cards contain letters 'AB', so after removing the first card Bob has one orange and one apple, and after removal of second card Alice has two oranges and one apple. So, in total it is three oranges and two apples.
{"inputs": ["1 3\n", "5 3\n", "2 1\n", "8 5\n", "5 2\n", "3 5\n", "1 2\n", "3 3\n"], "outputs": ["2B", "1A1B1A", "1A", "1A1B1A1B", "2A1B", "1B1A1B\n", "1B\n", "Impossible\n"]}
664
104
coding
Solve the programming task below in a Python markdown code block. To make Yalalovichik even more satisfied and happy, Jafar decided to invent Yalalovichik strings. A string is called a Yalalovichik string if the set of all of its distinct non-empty substrings is equal to the set of all of its distinct non-empty subsequences. You are given a string S$S$. You need to find the number of its distinct non-empty substrings which are Yalalovichik strings. Note: A string A$A$ is called a subsequence of a string B$B$ if A$A$ can be formed by erasing some characters (possibly none) from B$B$. A string A$A$ is called a substring of a string B$B$ if it can be formed by erasing some characters (possibly none) from the beginning of B$B$ and some (possibly none) from the end of B$B$. Two substrings or subsequences are considered different if they are different strings. -----Input----- - The first line of the input contains a single integer T$T$ denoting the number of test cases. The description of T$T$ test cases follows. - The first line of each test case contains a single integer N=|S|$N = |S|$. - The second line contains the string S$S$. -----Output----- For each test case, print a single line containing one integer — the number of distinct Yalalovichik substrings of S$S$. -----Constraints----- - 1≤T≤100$1 \le T \le 100$ - 1≤N≤106$1 \le N \le 10^6$ - the sum of N$N$ over all test cases does not exceed 2⋅106$2 \cdot 10^6$ - S$S$ contains only lowercase English letters -----Example Input----- 1 3 xxx -----Example Output----- 3 -----Explanation----- Example case 1: The distinct Yalalovichik substrings are "x", "xx" and "xxx".
{"inputs": ["1\n3\nxxx"], "outputs": ["3"]}
456
16
coding
Solve the programming task below in a Python markdown code block. So you've found a meeting room - phew! You arrive there ready to present, and find that someone has taken one or more of the chairs!! You need to find some quick.... check all the other meeting rooms to see if all of the chairs are in use. Your meeting room can take up to 8 chairs. `need` will tell you how many have been taken. You need to find that many. ```if-not:java Find the spare chairs from the array of meeting rooms. Each meeting room array will have the number of occupants as a string. Each occupant is represented by 'X'. The room array will also have an integer telling you how many chairs there are in the room. ``` ```if:java Find the spare chairs from the array of meeting rooms. ~~~java public class Room { public final String occupants; // number of occupants, each occupant is represented by 'X' public final int chairs; // number of chairs in the room } ~~~ ``` You should return an array of integers that shows how many chairs you take from each room in order, up until you have the required amount. ```if-not:java example: [['XXX', 3], ['XXXXX', 6], ['XXXXXX', 9], ['XXX',2]] when you need 4 chairs: ``` ```if:java example: `[Room("XXX", 3), Room("XXXXX", 6), Room("XXXXXX", 9), Room("XXX",2)]` when you need 4 chairs: ``` result -- > `[0, 1, 3]` (no chairs free in room 0, take 1 from room 1, take 3 from room 2. No need to consider room 4 as you have your 4 chairs already. If you need no chairs, return `'Game On'`. If there aren't enough spare chairs available, return `'Not enough!'` More in this series: The Office I - Outed The Office II - Boredeom Score The Office III - Broken Photocopier The Office IV - Find a Meeting Room Also feel free to reuse/extend the following starter code: ```python def meeting(rooms, number): ```
{"functional": "_inputs = [[[['XXX', 3], ['XXXXX', 6], ['XXXXXX', 9]], 4], [[['XXX', 1], ['XXXXXX', 6], ['X', 2], ['XXXXXX', 8], ['X', 3], ['XXX', 1]], 5], [[['XX', 2], ['XXXX', 6], ['XXXXX', 4]], 0], [[['XX', 2], ['XXXX', 6], ['XXXXX', 4]], 8], [[['XX', 2], ['XXXX', 6], ['XXXXX', 4]], 2]]\n_outputs = [[[0, 1, 3]], [[0, 0, 1, 2, 2]], ['Game On'], ['Not enough!'], [[0, 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(meeting(*i), o[0])"}
486
322
coding
Solve the programming task below in a Python markdown code block. The finalists of the "Russian Code Cup" competition in 2214 will be the participants who win in one of the elimination rounds. The elimination rounds are divided into main and additional. Each of the main elimination rounds consists of c problems, the winners of the round are the first n people in the rating list. Each of the additional elimination rounds consists of d problems. The winner of the additional round is one person. Besides, k winners of the past finals are invited to the finals without elimination. As a result of all elimination rounds at least n·m people should go to the finals. You need to organize elimination rounds in such a way, that at least n·m people go to the finals, and the total amount of used problems in all rounds is as small as possible. -----Input----- The first line contains two integers c and d (1 ≤ c, d ≤ 100) — the number of problems in the main and additional rounds, correspondingly. The second line contains two integers n and m (1 ≤ n, m ≤ 100). Finally, the third line contains an integer k (1 ≤ k ≤ 100) — the number of the pre-chosen winners. -----Output----- In the first line, print a single integer — the minimum number of problems the jury needs to prepare. -----Examples----- Input 1 10 7 2 1 Output 2 Input 2 2 2 1 2 Output 0
{"inputs": ["2 2\n2 1\n2\n", "8 9\n2 2\n3\n", "5 5\n8 8\n7\n", "5 7\n9 1\n8\n", "4 1\n5 3\n8\n", "2 1\n3 4\n2\n", "5 1\n6 3\n5\n", "2 1\n3 4\n2\n"], "outputs": ["0\n", "8\n", "40\n", "5\n", "6\n", "7\n", "11\n", "7\n"]}
324
136
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an array of integers arr. We want to select three indices i, j and k where (0 <= i < j <= k < arr.length). Let's define a and b as follows: a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1] b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k] Note that ^ denotes the bitwise-xor operation. Return the number of triplets (i, j and k) Where a == b.   Please complete the following python code precisely: ```python class Solution: def countTriplets(self, arr: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(arr = [2,3,1,6,7]) == 4\n assert candidate(arr = [1,1,1,1,1]) == 10\n assert candidate(arr = [2,3]) == 0\n assert candidate(arr = [1,3,5,7,9]) == 3\n assert candidate(arr = [7,11,12,9,5,2,7,17,22]) == 8\n\n\ncheck(Solution().countTriplets)"}
156
127
coding
Solve the programming task below in a Python markdown code block. Amicable numbers are two different numbers so related that the sum of the proper divisors of each is equal to the other number. (A proper divisor of a number is a positive factor of that number other than the number itself. For example, the proper divisors of 6 are 1, 2, and 3.) For example, the smallest pair of amicable numbers is (220, 284); for the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110, of which the sum is 284; and the proper divisors of 284 are 1, 2, 4, 71 and 142, of which the sum is 220. Derive function ```amicableNumbers(num1, num2)``` which returns ```true/True``` if pair ```num1 num2``` are amicable, ```false/False``` if not. See more at https://en.wikipedia.org/wiki/Amicable_numbers Also feel free to reuse/extend the following starter code: ```python def amicable_numbers(n1,n2): ```
{"functional": "_inputs = [[220, 284], [220, 280], [1184, 1210], [220221, 282224], [10744, 10856], [299920, 9284], [999220, 2849], [122265, 139815]]\n_outputs = [[True], [False], [True], [False], [True], [False], [False], [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(amicable_numbers(*i), o[0])"}
283
280
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given $N$ sets of integers $A_{1}, A_{2}, \ldots, A_{N}$. For each valid $i$, let's denote the elements of the set $A_{i}$ by $\{c_{i,1}, c_{i,2}, \ldots, c_{i,|A_{i}|}\}$. Find the number of ways to choose a sequence $(a_{1}, a_{2}, \ldots, a_{N})$ such that: $a_{i} \in A_{i}$ for each valid $i$ $a_{i} \neq a_{i+1}$ for each valid $i$ and $a_{1} \neq a_{N}$ Since this number may be large, compute it modulo $998,244,353$. ------ Input ------ The first line of the input contains a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains an integer $|A_{i}|$ followed by a space and $|A_{i}|$ space-separated non-negative integers $c_{i,1}, c_{i,2}, \ldots, c_{i,|A_{i}|}$. ------ Output ------ Print a single line containing one integer ― the number of ways to choose $(a_{1}, a_{2}, \ldots, a_{N})$, modulo $998,244,353$. ------ Constraints ------ $2 ≤ N ≤ 200,000$ $|A_{i}| ≥ 1$ for each valid $i$ $|A_{1}| + |A_{2}| + \ldots + |A_{N}| ≤ 200,000$ $1 ≤ c_{i,j} ≤ 200000$ for each valid $i, j$ ------ Subtasks ------ Subtask #1 (20 points): $N ≤ 100$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 3 3 1 2 3 2 1 2 2 2 3 ----- Sample Output 1 ------ 3 ----- explanation 1 ------ There are three possible sequences: $(1, 2, 3)$, $(2, 1, 3)$ and $(3, 1, 2)$.
{"inputs": ["3\n3 1 2 3\n2 1 2\n2 2 3"], "outputs": ["3"]}
578
32
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. There is an undirected graph with n nodes, numbered from 0 to n - 1. You are given a 0-indexed integer array scores of length n where scores[i] denotes the score of node i. You are also given a 2D integer array edges where edges[i] = [ai, bi] denotes that there exists an undirected edge connecting nodes ai and bi. A node sequence is valid if it meets the following conditions: There is an edge connecting every pair of adjacent nodes in the sequence. No node appears more than once in the sequence. The score of a node sequence is defined as the sum of the scores of the nodes in the sequence. Return the maximum score of a valid node sequence with a length of 4. If no such sequence exists, return -1.   Please complete the following python code precisely: ```python class Solution: def maximumScore(self, scores: List[int], edges: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(scores = [5,2,9,8,4], edges = [[0,1],[1,2],[2,3],[0,2],[1,3],[2,4]]) == 24\n assert candidate(scores = [9,20,6,4,11,12], edges = [[0,3],[5,3],[2,4],[1,3]]) == -1\n\n\ncheck(Solution().maximumScore)"}
217
111
coding
Solve the programming task below in a Python markdown code block. We define the "unfairness" of a list/array as the *minimal* difference between max(x1,x2,...xk) and min(x1,x2,...xk), for all possible combinations of k elements you can take from the list/array; both minimum and maximum of an empty list/array are considered to be 0. **More info and constraints:** * lists/arrays can contain values repeated more than once, plus there are usually more combinations that generate the required minimum; * the list/array's length can be any value from 0 to 10^(6); * the value of k will range from 0 to the length of the list/array, * the minimum unfairness of an array/list with less than 2 elements is 0. For example: ```python min_unfairness([30,100,1000,150,60,250,10,120,20],3)==20 #from max(30,10,20)-min(30,10,20)==20, minimum unfairness in this sample min_unfairness([30,100,1000,150,60,250,10,120,20],5)==90 #from max(30,100,60,10,20)-min(30,100,60,10,20)==90, minimum unfairness in this sample min_unfairness([1,1,1,1,1,1,1,1,1,2,2,2,2,2,2],10)==1 #from max(1,1,1,1,1,1,1,1,1,2)-min(1,1,1,1,1,1,1,1,1,2)==1, minimum unfairness in this sample min_unfairness([1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],10)==0 #from max(1,1,1,1,1,1,1,1,1,1)-min(1,1,1,1,1,1,1,1,1,1)==0, minimum unfairness in this sample min_unfairness([1,1,-1],2)==0 #from max(1,1)-min(1,1)==0, minimum unfairness in this sample ``` **Note:** shamelessly taken from [here](https://www.hackerrank.com/challenges/angry-children), where it was created and debatably categorized and ranked. Also feel free to reuse/extend the following starter code: ```python def min_unfairness(arr,k): ```
{"functional": "_inputs = [[[30, 100, 1000, 150, 60, 250, 10, 120, 20], 3], [[30, 100, 1000, 150, 60, 250, 10, 120, 20], 5], [[1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2], 10], [[1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2], 9], [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 10], [[1, 1, -1], 2], [[1, 1], 2], [[30, 100, 1000, 150, 60, 250, 10, 120, 20], 1], [[30, 100, 1000, 150, 60, 250, 10, 120, 20], 0], [[], 0]]\n_outputs = [[20], [90], [1], [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(min_unfairness(*i), o[0])"}
639
546
coding
Solve the programming task below in a Python markdown code block. Given an integer N. Integers A and B are chosen randomly in the range [1..N]. Calculate the probability that the Greatest Common Divisor(GCD) of A and B equals to B. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. Each test case consists of a single integer N on a separate line. -----Output----- For each test case, output a single line containing probability as an irreducible fraction. -----Example----- Input: 3 1 2 3 Output: 1/1 3/4 5/9 -----Constraints----- 1<=T<=103 1<=N<=109
{"inputs": ["3\n1\n2\n3", "3\n1\n2\n3"], "outputs": ["1/1\n3/4\n5/9", "1/1\n3/4\n5/9"]}
163
50
coding
Solve the programming task below in a Python markdown code block. Sumit and Dhiraj are roommates in a hostel of NIT Jamshedpur,One day after completing there boring assignments of Artificial Intelligence, they decided to play a game as it was dhiraj,s idea to play a game so he started explaining the rules of the game to sumit. So the game was as follows-They randomly took a number N after that they find all its divisors.Suppose first sumit took a divisor then dhiraj and so on.The player who will be having the last divisor with him will win the game.Rohit their mutual friend,was observing them play. Can you help Rohit predict the outcome of game? If Sumit wins print "YES" without quotes and "NO" without quotes, if Dhiraj wins the game. -----Input----- Input starts with an integer T denoting the number of test cases Each test case starts with a line containing an integer N the number. -----Output----- Outcome of each game in a separate line -----Constraints----- - 1 ≤ T ≤ 10^3 - 1 ≤ N ≤ 10^18 -----Sub tasks----- - Subtask #1:(10 points) - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 103 - Subtask #2:(25 points) - 1 ≤ T ≤ 50 - 1 ≤ N ≤ 1016 - Subtask #3:(65 points) - 1 ≤ T ≤ 103 - 1 ≤ N ≤ 1018 -----Example----- Input: 2 4 5 Output: YES NO
{"inputs": ["2\n4\n5"], "outputs": ["YES\nNO"]}
360
18
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an undirected tree consisting of n vertices numbered from 1 to n. A frog starts jumping from vertex 1. In one second, the frog jumps from its current vertex to another unvisited vertex if they are directly connected. The frog can not jump back to a visited vertex. In case the frog can jump to several vertices, it jumps randomly to one of them with the same probability. Otherwise, when the frog can not jump to any unvisited vertex, it jumps forever on the same vertex. The edges of the undirected tree are given in the array edges, where edges[i] = [ai, bi] means that exists an edge connecting the vertices ai and bi. Return the probability that after t seconds the frog is on the vertex target. Answers within 10-5 of the actual answer will be accepted.   Please complete the following python code precisely: ```python class Solution: def frogPosition(self, n: int, edges: List[List[int]], t: int, target: int) -> float: ```
{"functional": "def check(candidate):\n assert candidate(n = 7, edges = [[1,2],[1,3],[1,7],[2,4],[2,6],[3,5]], t = 2, target = 4) == 0.16666666666666666 \n assert candidate(n = 7, edges = [[1,2],[1,3],[1,7],[2,4],[2,6],[3,5]], t = 1, target = 7) == 0.3333333333333333\n\n\ncheck(Solution().frogPosition)"}
229
153
coding
Solve the programming task below in a Python markdown code block. Every Friday and Saturday night, farmer counts amount of sheep returned back to his farm (sheep returned on Friday stay and don't leave for a weekend). Sheep return in groups each of the days -> you will be given two arrays with these numbers (one for Friday and one for Saturday night). Entries are always positive ints, higher than zero. Farmer knows the total amount of sheep, this is a third parameter. You need to return the amount of sheep lost (not returned to the farm) after final sheep counting on Saturday. Example 1: Input: {1, 2}, {3, 4}, 15 --> Output: 5 Example 2: Input: {3, 1, 2}, {4, 5}, 21 --> Output: 6 Good luck! :-) Also feel free to reuse/extend the following starter code: ```python def lostSheep(friday,saturday,total): ```
{"functional": "_inputs = [[[1, 2], [3, 4], 15], [[3, 1, 2], [4, 5], 21], [[5, 1, 4], [5, 4], 29], [[11, 23, 3, 4, 15], [7, 14, 9, 21, 15], 300], [[2, 7, 13, 17], [23, 56, 44, 12, 1, 2, 1], 255], [[2, 5, 8], [11, 23, 3, 4, 15, 112, 12, 4], 355], [[1, 1, 1, 2, 1, 2], [2, 1, 2, 1, 2, 1], 30], [[5, 10, 15], [11, 23, 3, 4, 15], 89], [[3, 6, 9, 12], [3, 2, 1, 2, 3, 1], 44]]\n_outputs = [[5], [6], [10], [178], [77], [156], [13], [3], [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(lostSheep(*i), o[0])"}
209
475
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a string s, return the maximum number of unique substrings that the given string can be split into. You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique. A substring is a contiguous sequence of characters within a string.   Please complete the following python code precisely: ```python class Solution: def maxUniqueSplit(self, s: str) -> int: ```
{"functional": "def check(candidate):\n assert candidate(s = \"ababccc\") == 5\n assert candidate(s = \"aba\") == 2\n assert candidate(s = \"aa\") == 1\n\n\ncheck(Solution().maxUniqueSplit)"}
131
58
coding
Solve the programming task below in a Python markdown code block. There are N balls in a two-dimensional plane. The i-th ball is at coordinates (x_i, y_i). We will collect all of these balls, by choosing two integers p and q such that p \neq 0 or q \neq 0 and then repeating the following operation: * Choose a ball remaining in the plane and collect it. Let (a, b) be the coordinates of this ball. If we collected a ball at coordinates (a - p, b - q) in the previous operation, the cost of this operation is 0. Otherwise, including when this is the first time to do this operation, the cost of this operation is 1. Find the minimum total cost required to collect all the balls when we optimally choose p and q. Constraints * 1 \leq N \leq 50 * |x_i|, |y_i| \leq 10^9 * If i \neq j, x_i \neq x_j or y_i \neq y_j. * All values in input are integers. Input Input is given from Standard Input in the following format: N x_1 y_1 : x_N y_N Output Print the minimum total cost required to collect all the balls. Examples Input 2 1 1 2 2 Output 1 Input 3 1 4 4 6 7 8 Output 1 Input 4 1 1 1 2 2 1 2 2 Output 2
{"inputs": ["2\n1 2\n2 2", "2\n2 1\n2 2", "2\n1 2\n2 4", "2\n1 1\n2 2", "3\n1 4\n3 6\n7 8", "3\n1 4\n4 6\n8 8", "3\n1 4\n4 6\n7 8", "3\n1 4\n4 6\n11 8"], "outputs": ["1\n", "1\n", "1\n", "1", "2\n", "2\n", "1", "2\n"]}
342
141
coding
Solve the programming task below in a Python markdown code block. One day Alex decided to remember childhood when computers were not too powerful and lots of people played only default games. Alex enjoyed playing Minesweeper that time. He imagined that he saved world from bombs planted by terrorists, but he rarely won. Alex has grown up since then, so he easily wins the most difficult levels. This quickly bored him, and he thought: what if the computer gave him invalid fields in the childhood and Alex could not win because of it? He needs your help to check it. A Minesweeper field is a rectangle $n \times m$, where each cell is either empty, or contains a digit from $1$ to $8$, or a bomb. The field is valid if for each cell: if there is a digit $k$ in the cell, then exactly $k$ neighboring cells have bombs. if the cell is empty, then all neighboring cells have no bombs. Two cells are neighbors if they have a common side or a corner (i. e. a cell has at most $8$ neighboring cells). -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 100$) — the sizes of the field. The next $n$ lines contain the description of the field. Each line contains $m$ characters, each of them is "." (if this cell is empty), "*" (if there is bomb in this cell), or a digit from $1$ to $8$, inclusive. -----Output----- Print "YES", if the field is valid and "NO" otherwise. You can choose the case (lower or upper) for each letter arbitrarily. -----Examples----- Input 3 3 111 1*1 111 Output YES Input 2 4 *.*. 1211 Output NO -----Note----- In the second example the answer is "NO" because, if the positions of the bombs are preserved, the first line of the field should be *2*1. You can read more about Minesweeper in Wikipedia's article.
{"inputs": ["1 1\n4\n", "1 1\n*\n", "1 1\n8\n", "1 1\n.\n", "1 1\n*\n", "1 1\n.\n", "1 1\n4\n", "1 1\n8\n"], "outputs": ["NO", "YES", "NO", "YES", "YES", "YES", "NO", "NO"]}
449
94
coding
Solve the programming task below in a Python markdown code block. Chef has a calculator which has two screens and two buttons. Initially, each screen shows the number zero. Pressing the first button increments the number on the first screen by 1, and each click of the first button consumes 1 unit of energy. Pressing the second button increases the number on the second screen by the number which is currently appearing on the first screen. Each click of the second button consumes B units of energy. Initially the calculator has N units of energy. Now chef wonders what the maximum possible number is, that he gets on the second screen of the calculator, with the limited energy. -----Input----- The first line of the input contains an integer T denoting the number of test cases. Each test case is described using a single line containing two integers, N and B. -----Output----- For each test case, output a single line containing the answer to this test case. -----Constraints----- - 1 ≤ T ≤ 10,000 - 1 ≤ N, B ≤ 1,000,000,000 -----Subtasks----- - Subtask 1 (20 points): 1 ≤ N, B ≤ 1,000 - Subtask 2 (80 points): Original constraints -----Example----- Input: 3 10 2 8 5 6 1 Output: 12 3 9 -----Explanation----- Example case 1. There are 10 units of energy available. Pressing second button takes 2 units of energy. Chef can achieve 12 on the second screen as follows. - Press first button to get scores (1, 0). 9 units of energey is left. - Press first button to get scores (2, 0). 8 units of energy remaining. - Press first button to get scores (3, 0). 7 units of energy remaining. - Press first button to get scores (4, 0). 6 units of energy remaining. - Press second button to get scores (4, 4). 4 units of energy remaining. - Press second button to get scores (4, 8). 2 units of energy remaining. - Press second button to get scores (4, 12). 0 units of energy remaining.
{"inputs": ["3\n10 2\n8 5\n6 1"], "outputs": ["12\n3\n9"]}
489
30
coding
Solve the programming task below in a Python markdown code block. There are N cats. We number them from 1 through N. Each of the cats wears a hat. Cat i says: "there are exactly a_i different colors among the N - 1 hats worn by the cats except me." Determine whether there exists a sequence of colors of the hats that is consistent with the remarks of the cats. Constraints * 2 ≤ N ≤ 10^5 * 1 ≤ a_i ≤ N-1 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print `Yes` if there exists a sequence of colors of the hats that is consistent with the remarks of the cats; print `No` otherwise. Examples Input 3 1 2 2 Output Yes Input 3 1 1 2 Output No Input 5 4 3 4 3 4 Output No Input 3 2 2 2 Output Yes Input 4 2 2 2 2 Output Yes Input 5 3 3 3 3 3 Output No
{"inputs": ["3\n1 1 3", "3\n0 2 2", "3\n4 2 2", "3\n1 0 3", "3\n0 2 4", "3\n4 0 2", "3\n0 2 8", "3\n0 0 2"], "outputs": ["No\n", "No\n", "No\n", "No\n", "No\n", "No\n", "No\n", "No\n"]}
260
110
coding
Solve the programming task below in a Python markdown code block. Given the triangle of consecutive odd numbers: ``` 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 ... ``` Calculate the row sums of this triangle from the row index (starting at index 1) e.g.: ```python row_sum_odd_numbers(1); # 1 row_sum_odd_numbers(2); # 3 + 5 = 8 ``` ```if:nasm row_sum_odd_numbers: ``` Also feel free to reuse/extend the following starter code: ```python def row_sum_odd_numbers(n): ```
{"functional": "_inputs = [[1], [2], [13], [19], [41], [42], [74], [86], [93], [101]]\n_outputs = [[1], [8], [2197], [6859], [68921], [74088], [405224], [636056], [804357], [1030301]]\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(row_sum_odd_numbers(*i), o[0])"}
174
254
coding
Solve the programming task below in a Python markdown code block. Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have $n$ cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. -----Input----- The first line contains an integer $n$ — the number of cards with digits that you have ($1 \leq n \leq 100$). The second line contains a string of $n$ digits (characters "0", "1", ..., "9") $s_1, s_2, \ldots, s_n$. The string will not contain any other characters, such as leading or trailing spaces. -----Output----- If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. -----Examples----- Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 -----Note----- In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{"inputs": ["1\n0\n", "1\n8\n", "1\n8\n", "1\n0\n", "1\n1\n", "1\n2\n", "8\n12345678\n", "8\n12345678\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
465
100
coding
Solve the programming task below in a Python markdown code block. Assume you are creating a webshop and you would like to help the user in the search. You have products with brands, prices and name. You have the history of opened products (the most recently opened being the first item). Your task is to create a list of brands ordered by popularity, if two brands have the same popularity level then choose the one which was opened last from the two and second the other one. Product popularity is calculated from the history. If a product is more times in the history than it is more popular. Your function will have one parameter which will be always an array/list of object. example product: { name: "Phone", price: 25, brand: "Fake brand" } Also feel free to reuse/extend the following starter code: ```python def sorted_brands(history): ```
{"functional": "_inputs = [[[]]]\n_outputs = [[[]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(sorted_brands(*i), o[0])"}
184
155
coding
Solve the programming task below in a Python markdown code block. Once upon a time a little frog whose name was Vasya decided to travel around his home swamp. Overall there are n mounds on the swamp, located on one line. The distance between the neighboring mounds is one meter. Vasya wants to visit all the mounds in one day; besides, he wants to visit each one exactly once. For that he makes a route plan, to decide the order in which to jump on the mounds. Vasya can pick any mound as the first one. He thinks it boring to jump two times at the same distance. That's why he wants any two jumps on his route to have different lengths. Help Vasya the Frog and make the plan for him. Input The single line contains a number n (1 ≤ n ≤ 104) which is the number of mounds. Output Print n integers pi (1 ≤ pi ≤ n) which are the frog's route plan. * All the pi's should be mutually different. * All the |pi–pi + 1|'s should be mutually different (1 ≤ i ≤ n - 1). If there are several solutions, output any. Examples Input 2 Output 1 2 Input 3 Output 1 3 2
{"inputs": ["5\n", "6\n", "4\n", "1\n", "8\n", "7\n", "3\n", "2\n"], "outputs": ["1 5 2 4 3", "1 6 2 5 3 4 ", "1 4 2 3 ", "1", "1 8 2 7 3 6 4 5\n", "1 7 2 6 3 5 4\n", "1 3 2", "1 2 "]}
282
121
coding
Solve the programming task below in a Python markdown code block. Kuro and Shiro are playing with a board composed of n squares lining up in a row. The squares are numbered 1 to n from left to right, and Square s has a mark on it. First, for each square, Kuro paints it black or white with equal probability, independently from other squares. Then, he puts on Square s a stone of the same color as the square. Kuro and Shiro will play a game using this board and infinitely many black stones and white stones. In this game, Kuro and Shiro alternately put a stone as follows, with Kuro going first: - Choose an empty square adjacent to a square with a stone on it. Let us say Square i is chosen. - Put on Square i a stone of the same color as the square. - If there are squares other than Square i that contain a stone of the same color as the stone just placed, among such squares, let Square j be the one nearest to Square i. Change the color of every stone between Square i and Square j to the color of Square i. The game ends when the board has no empty square. Kuro plays optimally to maximize the number of black stones at the end of the game, while Shiro plays optimally to maximize the number of white stones at the end of the game. For each of the cases s=1,\dots,n, find the expected value, modulo 998244353, of the number of black stones at the end of the game. -----Notes----- When the expected value in question is represented as an irreducible fraction p/q, there uniquely exists an integer r such that rq=p ~(\text{mod } 998244353) and 0 \leq r \lt 998244353, which we ask you to find. -----Constraints----- - 1 \leq n \leq 2\times 10^5 -----Input----- Input is given from Standard Input in the following format: n -----Output----- Print n values. The i-th value should be the expected value, modulo 998244353, of the number of black stones at the end of the game for the case s=i. -----Sample Input----- 3 -----Sample Output----- 499122178 499122178 499122178 Let us use b to represent a black square and w to represent a white square. There are eight possible boards: www, wwb, wbw, wbb, bww, bwb, bbw, and bbb, which are chosen with equal probability. For each of these boards, there will be 0, 1, 0, 2, 1, 3, 2, and 3 black stones at the end of the game, respectively, regardless of the value of s. Thus, the expected number of stones is (0+1+0+2+1+3+2+3)/8 = 3/2, and the answer is r = 499122178, which satisfies 2r = 3 ~(\text{mod } 998244353) and 0 \leq r \lt 998244353.
{"inputs": ["3\n", "1\n", "2\n", "4\n", "5\n", "6\n", "7\n", "8\n"], "outputs": ["499122178\n499122178\n499122178\n", "499122177\n", "1\n1\n", "2\n2\n2\n2\n", "499122179\n499122179\n311951363\n499122179\n499122179\n", "3\n3\n904658948\n904658948\n3\n3\n", "499122180\n499122180\n452329476\n140378116\n452329476\n499122180\n499122180\n", "4\n4\n974848005\n818872325\n818872325\n974848005\n4\n4\n"]}
722
302
coding
Solve the programming task below in a Python markdown code block. You are given a string $s$ consisting of exactly $n$ characters, and each character is either '0', '1' or '2'. Such strings are called ternary strings. Your task is to replace minimum number of characters in this string with other characters to obtain a balanced ternary string (balanced ternary string is a ternary string such that the number of characters '0' in this string is equal to the number of characters '1', and the number of characters '1' (and '0' obviously) is equal to the number of characters '2'). Among all possible balanced ternary strings you have to obtain the lexicographically (alphabetically) smallest. Note that you can neither remove characters from the string nor add characters to the string. Also note that you can replace the given characters only with characters '0', '1' and '2'. It is guaranteed that the answer exists. -----Input----- The first line of the input contains one integer $n$ ($3 \le n \le 3 \cdot 10^5$, $n$ is divisible by $3$) — the number of characters in $s$. The second line contains the string $s$ consisting of exactly $n$ characters '0', '1' and '2'. -----Output----- Print one string — the lexicographically (alphabetically) smallest balanced ternary string which can be obtained from the given one with minimum number of replacements. Because $n$ is divisible by $3$ it is obvious that the answer exists. And it is obvious that there is only one possible answer. -----Examples----- Input 3 121 Output 021 Input 6 000000 Output 001122 Input 6 211200 Output 211200 Input 6 120110 Output 120120
{"inputs": ["3\n121\n", "3\n111\n", "3\n002\n", "3\n000\n", "3\n001\n", "3\n022\n", "3\n211\n", "3\n020\n"], "outputs": ["021\n", "012\n", "012\n", "012\n", "021\n", "012\n", "201\n", "021\n"]}
419
118
coding
Solve the programming task below in a Python markdown code block. Gohan has found his way into Dr. Gero's lab. There, he finds a circuit. He starts to toy with it. He finds the inverse transverse function $(V_{in}/V_{out})$ i.e (itf) of the circuit. Dr. Gero finds out about this. He gives Gohan an $s$-controller, through which he can randomly change the values of $s$. He finds out that each value of itf can be achieved through multiple values of $s$. But for a single particular value of itf, he finds out that this is not true. That is, just for this particular value of itf, there is only a single value of $s$ which achieves it. To avoid punishment, Gohan must complete a task given to him by Dr.Gero, which is to find this particular value of itf. He needs your help in doing that. HINT to solve these type of circuits. In these circuits, the capacitor and the inductor can be treated as normal resistors, with resistances as follows: The resistance of inductor is directly taken as (s * L) and that of capacitor is 1/(s * C). ------ Input: ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains 4 integers $R, L, C, V_{in}$, separated by single spaces. ------ Output: ------ For each test case, print a single line containing the answer. Your answer will be judged right if it is at most $10^{-2}$ away from the correct answer. ------ Constraints ------ $1 ≤ T ≤ 2$ $1 ≤ R ≤ 10^{5}$ $1 ≤ L ≤ 10^{5}$ $1 ≤ C ≤ 10^{5}$ $1 ≤ V_{in} ≤ 10^{9}$ ----- Sample Input 1 ------ 2 4 5 78 60 4 5 6 3 ----- Sample Output 1 ------ -61.4 -3.8
{"inputs": ["2\n4 5 78 60\n4 5 6 3"], "outputs": ["-61.4\n-3.8"]}
478
39
coding
Solve the programming task below in a Python markdown code block. There are n stone quarries in Petrograd. Each quarry owns mi dumpers (1 ≤ i ≤ n). It is known that the first dumper of the i-th quarry has xi stones in it, the second dumper has xi + 1 stones in it, the third has xi + 2, and the mi-th dumper (the last for the i-th quarry) has xi + mi - 1 stones in it. Two oligarchs play a well-known game Nim. Players take turns removing stones from dumpers. On each turn, a player can select any dumper and remove any non-zero amount of stones from it. The player who cannot take a stone loses. Your task is to find out which oligarch will win, provided that both of them play optimally. The oligarchs asked you not to reveal their names. So, let's call the one who takes the first stone «tolik» and the other one «bolik». Input The first line of the input contains one integer number n (1 ≤ n ≤ 105) — the amount of quarries. Then there follow n lines, each of them contains two space-separated integers xi and mi (1 ≤ xi, mi ≤ 1016) — the amount of stones in the first dumper of the i-th quarry and the number of dumpers at the i-th quarry. Output Output «tolik» if the oligarch who takes a stone first wins, and «bolik» otherwise. Examples Input 2 2 1 3 2 Output tolik Input 4 1 1 1 1 1 1 1 1 Output bolik
{"inputs": ["2\n2 0\n3 2\n", "2\n2 0\n5 2\n", "2\n2 0\n1 2\n", "2\n2 0\n2 2\n", "2\n2 0\n4 2\n", "2\n3 0\n4 2\n", "2\n2 1\n3 2\n", "4\n1 1\n1 1\n1 1\n1 1\n"], "outputs": ["tolik\n", "tolik\n", "tolik\n", "tolik\n", "tolik\n", "tolik\n", "tolik\n", "bolik\n"]}
364
150
coding
Solve the programming task below in a Python markdown code block. You are given n switches and m lamps. The i-th switch turns on some subset of the lamps. This information is given as the matrix a consisting of n rows and m columns where a_{i}, j = 1 if the i-th switch turns on the j-th lamp and a_{i}, j = 0 if the i-th switch is not connected to the j-th lamp. Initially all m lamps are turned off. Switches change state only from "off" to "on". It means that if you press two or more switches connected to the same lamp then the lamp will be turned on after any of this switches is pressed and will remain its state even if any switch connected to this lamp is pressed afterwards. It is guaranteed that if you push all n switches then all m lamps will be turned on. Your think that you have too many switches and you would like to ignore one of them. Your task is to say if there exists such a switch that if you will ignore (not use) it but press all the other n - 1 switches then all the m lamps will be turned on. -----Input----- The first line of the input contains two integers n and m (1 ≤ n, m ≤ 2000) — the number of the switches and the number of the lamps. The following n lines contain m characters each. The character a_{i}, j is equal to '1' if the i-th switch turns on the j-th lamp and '0' otherwise. It is guaranteed that if you press all n switches all m lamps will be turned on. -----Output----- Print "YES" if there is a switch that if you will ignore it and press all the other n - 1 switches then all m lamps will be turned on. Print "NO" if there is no such switch. -----Examples----- Input 4 5 10101 01000 00111 10000 Output YES Input 4 5 10100 01000 00110 00101 Output NO
{"inputs": ["1 1\n1\n", "1 1\n1\n", "2 1\n0\n1\n", "2 1\n0\n1\n", "2 1\n1\n1\n", "1 5\n11111\n", "2 2\n10\n11\n", "2 2\n01\n11\n"], "outputs": ["NO\n", "NO\n", "YES\n", "YES\n", "YES\n", "NO\n", "YES\n", "YES\n"]}
454
120
coding
Solve the programming task below in a Python markdown code block. There are some stones on Bob's table in a row, and each of them can be red, green or blue, indicated by the characters `R`, `G`, and `B`. Help Bob find the minimum number of stones he needs to remove from the table so that the stones in each pair of adjacent stones have different colours. Examples: ``` "RGBRGBRGGB" => 1 "RGGRGBBRGRR" => 3 "RRRRGGGGBBBB" => 9 ``` Also feel free to reuse/extend the following starter code: ```python def solution(stones): ```
{"functional": "_inputs = [['RRGGBB'], ['RGBRGB'], ['BGRBBGGBRRR'], ['GBBBGGRRGRB'], ['GBRGGRBBBBRRGGGB']]\n_outputs = [[3], [0], [4], [4], [7]]\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(solution(*i), o[0])"}
144
198
coding
Solve the programming task below in a Python markdown code block. You are given three integers $x, y$ and $n$. Your task is to find the maximum integer $k$ such that $0 \le k \le n$ that $k \bmod x = y$, where $\bmod$ is modulo operation. Many programming languages use percent operator % to implement it. In other words, with given $x, y$ and $n$ you need to find the maximum possible integer from $0$ to $n$ that has the remainder $y$ modulo $x$. You have to answer $t$ independent test cases. It is guaranteed that such $k$ exists for each test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 5 \cdot 10^4$) — the number of test cases. The next $t$ lines contain test cases. The only line of the test case contains three integers $x, y$ and $n$ ($2 \le x \le 10^9;~ 0 \le y < x;~ y \le n \le 10^9$). It can be shown that such $k$ always exists under the given constraints. -----Output----- For each test case, print the answer — maximum non-negative integer $k$ such that $0 \le k \le n$ and $k \bmod x = y$. It is guaranteed that the answer always exists. -----Example----- Input 7 7 5 12345 5 0 4 10 5 15 17 8 54321 499999993 9 1000000000 10 5 187 2 0 999999999 Output 12339 0 15 54306 999999995 185 999999998 -----Note----- In the first test case of the example, the answer is $12339 = 7 \cdot 1762 + 5$ (thus, $12339 \bmod 7 = 5$). It is obvious that there is no greater integer not exceeding $12345$ which has the remainder $5$ modulo $7$.
{"inputs": ["1\n13 2 61\n", "1\n13 0 61\n", "1\n13 0 39\n", "1\n31 2 104\n", "1\n33 6 100\n", "1\n31 2 104\n", "1\n33 6 100\n", "1\n23 2 104\n"], "outputs": ["54\n", "52\n", "39\n", "95\n", "72\n", "95\n", "72\n", "94\n"]}
531
147
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a year year and a month month, return the number of days of that month.   Please complete the following python code precisely: ```python class Solution: def numberOfDays(self, year: int, month: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(year = 1992, month = 7) == 31\n assert candidate(year = 2000, month = 2) == 29\n assert candidate(year = 1900, month = 2) == 28\n\n\ncheck(Solution().numberOfDays)"}
69
82
coding
Solve the programming task below in a Python markdown code block. You are given two binary strings A and B, each of length N. You can perform the following operation on string A any number of times: Select a [prime number] X. Choose any substring of string A having length X and reverse the substring. Determine whether you can make the string A equal to B using any (possibly zero) number of operations. A substring is obtained by deleting some (possibly zero) elements from the beginning and some (possibly zero) elements from the end of the string. ------ 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 an integer N — the length of the strings A and B. - The second line contains the binary string A. - The third line contains the binary string B. ------ Output Format ------ For each test case, output on a new line, YES, if you can make the string A equal to B using any number of operations and NO otherwise. You can print each character in uppercase or lowercase. For example, YES, yes, Yes, and yES are all identical. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 10^{5}$ $A_{i}$ and $B_{i}$ contain $0$ and $1$ only. - The sum of $N$ over all test cases won't exceed $10^{5}$. ----- Sample Input 1 ------ 4 2 00 00 4 1001 0111 5 11000 10010 5 11000 11010 ----- Sample Output 1 ------ YES NO YES NO ----- explanation 1 ------ Test case $1$: Both the strings are equal. Thus, we do not need any operations. Test case $2$: It can be shown that we cannot make the string $A$ equal to $B$ using any number of operations. Test case $3$: Choose $X = 3$ and reverse the substring $A[2,4] = 100$. Thus, the string $A$ becomes $10010$ which is equal to $B$. Test case $4$: It can be shown that we cannot make the string $A$ equal to $B$ using any number of operations.
{"inputs": ["4\n2\n00\n00\n4\n1001\n0111\n5\n11000\n10010\n5\n11000\n11010\n"], "outputs": ["YES\nNO\nYES\nNO\n"]}
533
68
coding
Solve the programming task below in a Python markdown code block. For given two lines s1 and s2, print "2" if they are parallel, "1" if they are orthogonal, or "0" otherwise. s1 crosses points p0 and p1, and s2 crosses points p2 and p3. Constraints * 1 ≤ q ≤ 1000 * -10000 ≤ xpi, ypi ≤ 10000 * p0 ≠ p1 and p2 ≠ p3. Input The entire input looks like: q (the number of queries) 1st query 2nd query ... qth query Each query consists of integer coordinates of the points p0, p1, p2, p3 in the following format: xp0 yp0 xp1 yp1 xp2 yp2 xp3 yp3 Output For each query, print "2", "1" or "0". Example Input 3 0 0 3 0 0 2 3 2 0 0 3 0 1 1 1 4 0 0 3 0 1 1 2 2 Output 2 1 0
{"inputs": ["3\n0 0 3 0 0 2 3 2\n0 0 3 0 1 1 1 7\n0 0 3 0 1 1 2 2", "3\n0 0 3 1 0 2 3 2\n0 0 3 0 1 1 1 7\n0 0 3 0 1 1 2 2", "3\n0 0 3 1 0 2 3 2\n0 1 3 0 1 1 1 7\n0 0 3 0 1 1 2 2", "3\n0 0 3 1 0 2 3 2\n0 1 3 0 1 0 1 7\n0 0 3 0 1 1 2 1", "3\n0 0 3 0 0 2 3 2\n0 0 3 0 2 0 2 7\n0 0 1 0 1 1 1 2", "3\n1 0 3 1 0 2 3 2\n0 0 3 0 2 0 2 7\n0 0 1 0 1 1 1 2", "3\n0 0 3 0 0 2 3 2\n0 0 3 0 1 1 1 7\n0 0 3 0 1 1 4 2", "3\n0 0 3 0 0 2 3 2\n0 0 3 0 2 1 1 7\n0 0 3 0 1 1 4 2"], "outputs": ["2\n1\n0\n", "0\n1\n0\n", "0\n0\n0\n", "0\n0\n2\n", "2\n1\n1\n", "0\n1\n1\n", "2\n1\n0\n", "2\n0\n0\n"]}
263
478
coding
Solve the programming task below in a Python markdown code block. There are N blocks arranged in a row, numbered 1 to N from left to right. Each block has a weight, and the weight of Block i is A_i. Snuke will perform the following operation on these blocks N times: * Choose one block that is still not removed, and remove it. The cost of this operation is the sum of the weights of the blocks that are connected to the block being removed (including itself). Here, two blocks x and y ( x \leq y ) are connected when, for all z ( x \leq z \leq y ), Block z is still not removed. There are N! possible orders in which Snuke removes the blocks. For all of those N! orders, find the total cost of the N operations, and calculate the sum of those N! total costs. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 10^5 * 1 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output For all of the N! orders, find the total cost of the N operations, and print the sum of those N! total costs, modulo 10^9+7. Examples Input 2 1 2 Output 9 Input 4 1 1 1 1 Output 212 Input 10 1 2 4 8 16 32 64 128 256 512 Output 880971923
{"inputs": ["2\n1 1", "2\n2 1", "2\n2 2", "2\n1 0", "2\n1 2", "2\n1 -1", "4\n1 2 1 1", "4\n1 2 2 1"], "outputs": ["6\n", "9\n", "12\n", "3\n", "9", "0\n", "268\n", "324\n"]}
388
106
coding
Solve the programming task below in a Python markdown code block. We're giving away nice huge bags containing number tiles! A bag we want to present to you contains n tiles. Each of them has a single number written on it — either 1 or 2. However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequence, in any order you wish. We will then compute the sums of all prefixes in the sequence, and then count how many of these sums are prime numbers. If you want to keep the prize, you will need to maximize the number of primes you get. Can you win the prize? Hurry up, the bags are waiting! Input The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of number tiles in the bag. The following line contains n space-separated integers a_1, a_2, ..., a_n (a_i ∈ \{1, 2\}) — the values written on the tiles. Output Output a permutation b_1, b_2, ..., b_n of the input sequence (a_1, a_2, ..., a_n) maximizing the number of the prefix sums being prime numbers. If there are multiple optimal permutations, output any. Examples Input 5 1 2 1 2 1 Output 1 1 1 2 2 Input 9 1 1 2 1 1 1 2 1 1 Output 1 1 1 2 1 1 1 2 1 Note The first solution produces the prefix sums 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, \mathbf{\color{blue}{7}} (four primes constructed), while the prefix sums in the second solution are 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, 6, \mathbf{\color{blue}{7}}, 8, 10, \mathbf{\color{blue}{11}} (five primes). Primes are marked bold and blue. In each of these cases, the number of produced primes is maximum possible.
{"inputs": ["1\n2\n", "1\n1\n", "2\n1 1\n", "2\n1 2\n", "2\n2 2\n", "2\n2 1\n", "3\n1 1 1\n", "3\n1 2 2\n"], "outputs": ["2\n\n", "1\n\n", "1 1\n\n", "2 1\n", "2 2\n\n", "2 1\n", "1 1 1\n\n", "2 1 2\n"]}
508
123
coding
Solve the programming task below in a Python markdown code block. itertools.combinations(iterable, r) This tool returns the $\textbf{r}$ length subsequences of elements from the input iterable. Combinations are emitted in lexicographic sorted order. So, if the input iterable is sorted, the combination tuples will be produced in sorted order. Sample Code >>> from itertools import combinations >>> >>> print list(combinations('12345',2)) [('1', '2'), ('1', '3'), ('1', '4'), ('1', '5'), ('2', '3'), ('2', '4'), ('2', '5'), ('3', '4'), ('3', '5'), ('4', '5')] >>> >>> A = [1,1,3,3,3] >>> print list(combinations(A,4)) [(1, 1, 3, 3), (1, 1, 3, 3), (1, 1, 3, 3), (1, 3, 3, 3), (1, 3, 3, 3)] Task You are given a string $\mbox{S}$. Your task is to print all possible combinations, up to size $\boldsymbol{\mbox{k}}$, of the string in lexicographic sorted order. Input Format A single line containing the string $\mbox{S}$ and integer value $\boldsymbol{\mbox{k}}$ separated by a space. Constraints $0<k\leq len(S)$ The string contains only UPPERCASE characters. Output Format Print the different combinations of string $\mbox{S}$ on separate lines. Sample Input HACK 2 Sample Output A C H K AC AH AK CH CK HK
{"inputs": ["HACK 2\n"], "outputs": ["A\nC\nH\nK\nAC\nAH\nAK\nCH\nCK\nHK\n"]}
393
35
coding
Solve the programming task below in a Python markdown code block. A Smith number is a composite number, the sum of whose digits is the sum of the digits of its prime factors obtained as a result of prime factorization (excluding ${1}$). The first few such numbers are ${4}$, $22$, $27$, $58$, $85$, ${94}$, and ${121}$. Example: $378=2\times3\times3\times3\times7$ So, its prime factors are $2$, $3$, $3$, $3$, and $7$. The sum of its digits is $(3+7+8)=18$. The sum of the digits of its factors is $(2+3+3+3+7)=18$. Similarly, $4937775$ is a Smith number. $4937775=3\times5\times5\times65837$, and the sum of its digits is the same as the sum of the digits of its prime factors: $4+9+3+7+7+7+5=3+5+5+6+5+8+3+7=42$. Task: Write a program to check whether a given integer is a Smith number. Input Format There will be only one line of input: $N$, the number which needs to be checked. Constraints: $0<N<2,147,483,647$ (max value of an integer of the size of ${4}$ bytes) Output Format ${1}$ if the number is a Smith number. ${0}$ if the number is a not Smith number. Sample Input 378 Sample Output 1 Explanation Its prime factors are $2$, $3$, $3$, $3$, and $7$. The sum of its digits is $(3+7+8)=18$. The sum of the digits of its factors is $(2+3+3+3+7)=18$.
{"inputs": ["378\n"], "outputs": ["1\n"]}
450
16