task_type
stringclasses
1 value
problem
stringlengths
209
3.39k
answer
stringlengths
35
6.15k
problem_tokens
int64
60
774
answer_tokens
int64
12
2.04k
coding
Solve the programming task below in a Python markdown code block. One rainy gloomy evening when all modules hid in the nearby cafes to drink hot energetic cocktails, the Hexadecimal virus decided to fly over the Mainframe to look for a Great Idea. And she has found one! Why not make her own Codeforces, with blackjack and other really cool stuff? Many people will surely be willing to visit this splendid shrine of high culture. In Mainframe a standard pack of 52 cards is used to play blackjack. The pack contains cards of 13 values: 2, 3, 4, 5, 6, 7, 8, 9, 10, jacks, queens, kings and aces. Each value also exists in one of four suits: hearts, diamonds, clubs and spades. Also, each card earns some value in points assigned to it: cards with value from two to ten earn from 2 to 10 points, correspondingly. An ace can either earn 1 or 11, whatever the player wishes. The picture cards (king, queen and jack) earn 10 points. The number of points a card earns does not depend on the suit. The rules of the game are very simple. The player gets two cards, if the sum of points of those cards equals n, then the player wins, otherwise the player loses. The player has already got the first card, it's the queen of spades. To evaluate chances for victory, you should determine how many ways there are to get the second card so that the sum of points exactly equals n. Input The only line contains n (1 ≀ n ≀ 25) β€” the required sum of points. Output Print the numbers of ways to get the second card in the required way if the first card is the queen of spades. Examples Input 12 Output 4 Input 20 Output 15 Input 10 Output 0 Note In the first sample only four two's of different suits can earn the required sum of points. In the second sample we can use all tens, jacks, queens and kings; overall it's 15 cards, as the queen of spades (as any other card) is only present once in the pack of cards and it's already in use. In the third sample there is no card, that would add a zero to the current ten points.
{"inputs": ["6\n", "5\n", "9\n", "2\n", "1\n", "3\n", "4\n", "8\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
507
70
coding
Solve the programming task below in a Python markdown code block. # Task You are a lifelong fan of your local football club, and proud to say you rarely miss a game. Even though you're a superfan, you still hate boring games. Luckily, boring games often end in a draw, at which point the winner is determined by a penalty shoot-out, which brings some excitement to the viewing experience. Once, in the middle of a penalty shoot-out, you decided to count the lowest total number of shots required to determine the winner. So, given the number of shots each team has already made and the current score, `how soon` can the game end? If you are not familiar with penalty shoot-out rules, here they are: `Teams take turns to kick from the penalty mark until each has taken five kicks. However, if one side has scored more successful kicks than the other could possibly reach with all of its remaining kicks, the shoot-out immediately ends regardless of the number of kicks remaining.` `If at the end of these five rounds of kicks the teams have scored an equal number of successful kicks, additional rounds of one kick each will be used until the tie is broken.` # Input/Output `[input]` integer `shots` An integer, the number of shots each team has made thus far. `0 ≀ shots ≀ 100.` `[input]` integer array `score` An array of two integers, where score[0] is the current score of the first team and score[1] - of the second team. `score.length = 2,` `0 ≀ score[i] ≀ shots.` `[output]` an integer The minimal possible total number of shots required to determine the winner. # Example For `shots = 2 and score = [1, 2]`, the output should be `3`. The possible 3 shots can be: ``` shot1: the first team misses the penalty shot2: the second team scores shot3: the first one misses again``` then, score will be [1, 3]. As the first team can't get 2 more points in the last remaining shot until the end of the initial five rounds, the winner is determined. For `shots = 10 and score = [10, 10]`, the output should be `2`. If one of the teams misses the penalty and the other one scores, the game ends. Also feel free to reuse/extend the following starter code: ```python def penaltyShots(shots, score): ```
{"functional": "_inputs = [[2, [1, 2]], [10, [10, 10]], [5, [5, 5]], [0, [0, 0]], [1, [0, 0]], [1, [0, 1]], [1, [1, 1]], [3, [3, 3]], [3, [1, 3]], [100, [100, 100]]]\n_outputs = [[3], [2], [2], [6], [5], [4], [5], [3], [1], [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(penaltyShots(*i), o[0])"}
536
280
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an array of strings words and a string pref. Return the number of strings in words that contain pref as a prefix. A prefix of a string s is any leading contiguous substring of s. Β  Please complete the following python code precisely: ```python class Solution: def prefixCount(self, words: List[str], pref: str) -> int: ```
{"functional": "def check(candidate):\n assert candidate(words = [\"pay\",\"attention\",\"practice\",\"attend\"], pref = \"at\") == 2\n assert candidate(words = [\"leetcode\",\"win\",\"loops\",\"success\"], pref = \"code\") == 0\n\n\ncheck(Solution().prefixCount)"}
93
69
coding
Solve the programming task below in a Python markdown code block. Chef has $K$ chocolates and he wants to distribute them to $N$ people (numbered $1$ through $N$). These people are standing in a line in such a way that for each $i$ ($1 \le i \le N-1$), person $i$ and person $i+1$ are adjacent. First, consider some way to distribute chocolates such that for each valid $i$, the number of chocolates the $i$-th person would receive from Chef is $A_i$ and the sum $S_1 = \sum_{i=1}^{N-1} \left|A_i - A_{i+1}\right|$ is minimum possible. Of course, each person must receive a non-negative integer number of chocolates. Then, Chef wants to create a new sequence $B_1, B_2, \ldots, B_N$ by rearranging (permuting) the elements of the sequence $A_1, A_2, \ldots, A_N$. For each valid $i$, the number of chocolates the $i$-th person actually receives from Chef is $B_i$. Chef wants to distribute the chocolates (choose $B_1, B_2, \ldots, B_N$ by permuting the sequence $A$ and give $B_i$ chocolates to the $i$-th person for each valid $i$) in such a way that $S_2 = \sum_{i=1}^{N-1} \left|B_i – B_{i+1}\right|$ is maximum possible. You need to find the maximum value of $S_2$. It is guaranteed that $S_2$ does not depend on the exact choice of the sequence $A_1, A_2, \ldots, A_N$, as long as it is a sequence that minimises $S_1$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains a single integer $N$. - The second line contains a single integer $K$. -----Output----- For each test case, print a single line containing one integer β€” the maximum value of the sum $S_2$. -----Constraints----- - $1 \le T \le 10$ - $2 \le N \le 10^5$ - $2 \le K \le 10^{100,000}$ -----Subtasks----- Subtask #1 (30 points): $2 \le N, K \le 1,000$ Subtask #2 (70 points): original constraints -----Example Input----- 1 3 2 -----Example Output----- 2 -----Explanation----- Example case 1: To minimise $S_1$, Chef could give $1$ chocolate to person $1$ and $1$ chocolate to person $2$, so $S_1 = |1-1| + |1-0| = 1$. To maximise $S_2$, Chef can give $1$ chocolate to person $1$ and $1$ chocolate to person $3$, since the sequence $B = (1, 0, 1)$ is a permutation of $A = (1, 1, 0)$. Then, $S_2 = |1-0| + |0-1| = 2$.
{"inputs": ["1\n3\n2"], "outputs": ["2"]}
754
16
coding
Solve the programming task below in a Python markdown code block. The snakes want to build a temple for Lord Cobra. There are multiple strips of land that they are looking at, but not all of them are suitable. They need the strip of land to resemble a coiled Cobra. You need to find out which strips do so. Formally, every strip of land, has a length. Suppose the length of the i-th strip is is Ni, then there will be Ni integers, Hi1, Hi2, .. HiNi, which represent the heights of the ground at various parts of the strip, in sequential order. That is, the strip has been divided into Ni parts and the height of each part is given. This strip is valid, if and only if all these conditions are satisfied: - There should be an unique 'centre' part. This is where the actual temple will be built. By centre, we mean that there should be an equal number of parts to the left of this part, and to the right of this part. - Hi1 = 1 - The heights keep increasing by exactly 1, as you move from the leftmost part, to the centre part. - The heights should keep decreasing by exactly 1, as you move from the centre part to the rightmost part. Note that this means that HiNi should also be 1. Your job is to look at every strip and find if it's valid or not. -----Input----- - The first line contains a single integer, S, which is the number of strips you need to look at. The description of each of the S strips follows - The first line of the i-th strip's description will contain a single integer: Ni, which is the length and number of parts into which it has been divided. - The next line contains Ni integers: Hi1, Hi2, .., HiNi. These represent the heights of the various parts in the i-th strip. -----Output----- - For each strip, in a new line, output "yes" if is a valid strip, and "no", if it isn't. -----Constraints----- - 1 ≀ S ≀ 100 - 3 ≀ Ni ≀ 100 - 1 ≀ Hij ≀ 100 -----Example----- Input: 7 5 1 2 3 2 1 7 2 3 4 5 4 3 2 5 1 2 3 4 3 5 1 3 5 3 1 7 1 2 3 4 3 2 1 4 1 2 3 2 4 1 2 2 1 Output: yes no no no yes no no -----Explanation----- In the first strip, all the conditions are satisfied, hence it is valid. In the second strip, it does not start with a 1, and hence is invalid. In the third strip, it keeps increasing even past the centre, instead of decreasing. Hence invalid. The fourth strip does not increase and decrease by exactly 1. Hence invalid. The fifth satisfies all conditions and hence is valid. The sixth and seventh strip do not have a 'centre' part. Because for every part, there are either more parts to its right than its left, or more parts on its left than its right. Hence both the strips are invalid.
{"inputs": ["7\n5\n1 2 3 2 1\n7\n2 3 4 5 4 3 2\n5\n1 2 3 4 3\n5\n1 3 5 3 1\n7\n1 2 3 4 3 2 1\n4\n1 2 3 2\n4\n1 2 2 1"], "outputs": ["yes\nno\nno\nno\nyes\nno\nno"]}
710
112
coding
Solve the programming task below in a Python markdown code block. Implement `String#to_cents`, which should parse prices expressed as `$1.23` and return number of cents, or in case of bad format return `nil`. Also feel free to reuse/extend the following starter code: ```python def to_cents(amount): ```
{"functional": "_inputs = [[''], ['1'], ['1.23'], ['$1'], ['$1.23'], ['$99.99'], ['$12345678.90'], ['$9.69'], ['$9.70'], ['$9.71'], ['$1.23\\n'], ['\\n$1.23'], ['$0.69'], ['$9.69$4.3.7'], ['$9.692']]\n_outputs = [[None], [None], [None], [None], [123], [9999], [1234567890], [969], [970], [971], [None], [None], [69], [None], [None]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(to_cents(*i), o[0])"}
71
315
coding
Solve the programming task below in a Python markdown code block. International Abbreviation Olympiad takes place annually starting from 1989. Each year the competition receives an abbreviation of form IAO'y, where y stands for some number of consequent last digits of the current year. Organizers always pick an abbreviation with non-empty string y that has never been used before. Among all such valid abbreviations they choose the shortest one and announce it to be the abbreviation of this year's competition. For example, the first three Olympiads (years 1989, 1990 and 1991, respectively) received the abbreviations IAO'9, IAO'0 and IAO'1, while the competition in 2015 received an abbreviation IAO'15, as IAO'5 has been already used in 1995. You are given a list of abbreviations. For each of them determine the year it stands for. -----Input----- The first line of the input contains a single integer n (1 ≀ n ≀ 1000)Β β€” the number of abbreviations to process. Then n lines follow, each containing a single abbreviation. It's guaranteed that each abbreviation contains at most nine digits. -----Output----- For each abbreviation given in the input, find the year of the corresponding Olympiad. -----Examples----- Input 5 IAO'15 IAO'2015 IAO'1 IAO'9 IAO'0 Output 2015 12015 1991 1989 1990 Input 4 IAO'9 IAO'99 IAO'999 IAO'9999 Output 1989 1999 2999 9999
{"inputs": ["1\nIAO'001\n", "1\nIAO'089\n", "1\nIAO'089\n", "1\nIAO'001\n", "1\nIAO'088\n", "1\nIAO'002\n", "1\nIAO'102\n", "1\nIAO'079\n"], "outputs": ["3001\n", "3089\n", "3089\n", "3001\n", "3088\n", "3002\n", "2102\n", "3079\n"]}
399
150
coding
Solve the programming task below in a Python markdown code block. You are given a binary string S of length N. You are allowed to perform the following types of operations on string S: Delete any one character from S, and concatenate the remaining parts of the string. For example, if we delete the third character of S = 1101, it becomes S = 111. Flip all the characters of S. For example, if we flip all character of S = 1101, it becomes S = 0010. Given that you can use either type of operation any number of times, find the minimum number of operations required to make all characters of the string S equal to 0. ------ 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 string. - The next line contains a binary string S of length N. ------ Output Format ------ For each test case, output on a new line, the minimum number of operations required to make all characters of the string S equal to 0. ------ Constraints ------ $1 ≀ T ≀ 2000$ $1 ≀ N ≀ 10^{5}$ $S$ contains $0$ and $1$ only. - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 4 2 01 3 101 3 111 4 0000 ----- Sample Output 1 ------ 1 2 1 0 ----- explanation 1 ------ Test case $1$: You can use one operation to delete the second character of the string $S$. Thus, the string becomes $0$. Note that all characters of this string are $0$ and thus, it satisfies the conditions. Test case $2$: You can perform the following operations: - Operation $1$: Flip all characters of the string. Thus, string becomes $010$. - Operation $2$: Delete the second character of the string. Thus, string becomes $00$. Note that we have obtained a string having all characters as $0$ in two operations. It can be shown that this is the minimum number of operations required. Test case $3$: You can use one operation to flip all characters of the string $S$. Thus, the string becomes $000$. Note that all characters of this string are $0$ and thus, it satisfies the conditions. Test case $4$: The existing string satisfies the conditions. Thus, we require zero operations.
{"inputs": ["4\n2\n01\n3\n101\n3\n111\n4\n0000\n"], "outputs": ["1\n2\n1\n0\n"]}
576
44
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an integer x, return true if x is a palindrome, and false otherwise. Β  Please complete the following python code precisely: ```python class Solution: def isPalindrome(self, x: int) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(x = 121) == True\n assert candidate(x = -121) == False\n assert candidate(x = 10) == False\n\n\ncheck(Solution().isPalindrome)"}
64
57
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. A pangram is a sentence where every letter of the English alphabet appears at least once. Given a string sentence containing only lowercase English letters, return true if sentence is a pangram, or false otherwise. Β  Please complete the following python code precisely: ```python class Solution: def checkIfPangram(self, sentence: str) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(sentence = \"thequickbrownfoxjumpsoverthelazydog\") == True\n assert candidate(sentence = \"leetcode\") == False\n\n\ncheck(Solution().checkIfPangram)"}
91
54
coding
Solve the programming task below in a Python markdown code block. collections.OrderedDict An OrderedDict is a dictionary that remembers the order of the keys that were inserted first. If a new entry overwrites an existing entry, the original insertion position is left unchanged. Example Code >>> from collections import OrderedDict >>> >>> ordinary_dictionary = {} >>> ordinary_dictionary['a'] = 1 >>> ordinary_dictionary['b'] = 2 >>> ordinary_dictionary['c'] = 3 >>> ordinary_dictionary['d'] = 4 >>> ordinary_dictionary['e'] = 5 >>> >>> print ordinary_dictionary {'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4} >>> >>> ordered_dictionary = OrderedDict() >>> ordered_dictionary['a'] = 1 >>> ordered_dictionary['b'] = 2 >>> ordered_dictionary['c'] = 3 >>> ordered_dictionary['d'] = 4 >>> ordered_dictionary['e'] = 5 >>> >>> print ordered_dictionary OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)]) Task You are the manager of a supermarket. You have a list of $N$ items together with their prices that consumers bought on a particular day. Your task is to print each item_name and net_price in order of its first occurrence. item_name = Name of the item. net_price = Quantity of the item sold multiplied by the price of each item. Input Format The first line contains the number of items, $N$. The next $N$ lines contains the item's name and price, separated by a space. Constraints $0<N\leq100$ Output Format Print the item_name and net_price in order of its first occurrence. Sample Input 9 BANANA FRIES 12 POTATO CHIPS 30 APPLE JUICE 10 CANDY 5 APPLE JUICE 10 CANDY 5 CANDY 5 CANDY 5 POTATO CHIPS 30 Sample Output BANANA FRIES 12 POTATO CHIPS 60 APPLE JUICE 20 CANDY 20 Explanation BANANA FRIES: Quantity bought: $\mbox{I}$, Price: $12$ Net Price: $12$ POTATO CHIPS: Quantity bought: $2$, Price: $30$ Net Price: $\mbox{60}$ APPLE JUICE: Quantity bought: $2$, Price: $10$ Net Price: $\textbf{20}$ CANDY: Quantity bought: $\begin{array}{c}A\end{array}$, Price: $5$ Net Price: $\textbf{20}$
{"inputs": ["9\nBANANA FRIES 12\nPOTATO CHIPS 30\nAPPLE JUICE 10\nCANDY 5\nAPPLE JUICE 10\nCANDY 5\nCANDY 5\nCANDY 5\nPOTATO CHIPS 30\n"], "outputs": ["BANANA FRIES 12\nPOTATO CHIPS 60\nAPPLE JUICE 20\nCANDY 20\n"]}
632
112
coding
Solve the programming task below in a Python markdown code block. Let be `n` an integer prime with `10` e.g. `7`. `1/7 = 0.142857 142857 142857 ...`. We see that the decimal part has a cycle: `142857`. The length of this cycle is `6`. In the same way: `1/11 = 0.09 09 09 ...`. Cycle length is `2`. # Task Given an integer n (n > 1), the function cycle(n) returns the length of the cycle if n and 10 are coprimes, otherwise returns -1. # Examples: ``` cycle(5) = -1 cycle(13) = 6 -> 0.076923 076923 0769 cycle(21) = 6 -> 0.047619 047619 0476 cycle(27) = 3 -> 0.037 037 037 037 0370 cycle(33) = 2 -> 0.03 03 03 03 03 03 03 03 cycle(37) = 3 -> 0.027 027 027 027 027 0 cycle(94) = -1 cycle(22) = -1 since 1/22 ~ 0.0 45 45 45 45 ... ``` Also feel free to reuse/extend the following starter code: ```python def cycle(n) : ```
{"functional": "_inputs = [[3], [33], [18118], [69], [197], [65], [97], [19], [111], [53], [59], [93], [51], [159], [183], [167], [94], [133], [218713], [38127], [431541], [221193], [1234567]]\n_outputs = [[1], [2], [-1], [22], [98], [-1], [96], [18], [3], [13], [58], [15], [16], [13], [60], [166], [-1], [18], [9744], [6230], [726], [3510], [34020]]\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(cycle(*i), o[0])"}
402
365
coding
Solve the programming task below in a Python markdown code block. A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from  - 5Β·1018 to 5Β·1018 inclusive, or to find out that such points do not exist. Input The first line contains three integers A, B and C ( - 2Β·109 ≀ A, B, C ≀ 2Β·109) β€” corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0. Output If the required point exists, output its coordinates, otherwise output -1. SAMPLE INPUT 2 5 3 SAMPLE OUTPUT 6 -3
{"inputs": ["2 5 3"], "outputs": ["6 -3"]}
201
18
coding
Solve the programming task below in a Python markdown code block. There is a circular pond with a perimeter of K meters, and N houses around them. The i-th house is built at a distance of A_i meters from the northmost point of the pond, measured clockwise around the pond. When traveling between these houses, you can only go around the pond. Find the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses. -----Constraints----- - 2 \leq K \leq 10^6 - 2 \leq N \leq 2 \times 10^5 - 0 \leq A_1 < ... < A_N < K - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: K N A_1 A_2 ... A_N -----Output----- Print the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses. -----Sample Input----- 20 3 5 10 15 -----Sample Output----- 10 If you start at the 1-st house and go to the 2-nd and 3-rd houses in this order, the total distance traveled will be 10.
{"inputs": ["7 3\n0 5 17", "7 3\n0 4 17", "7 3\n0 4 28", "7 3\n0 8 28", "7 3\n1 8 31", "9 3\n0 5 17", "7 3\n0 8 17", "7 3\n0 2 28"], "outputs": ["-5\n", "-6\n", "-17\n", "-13\n", "-16\n", "-3\n", "-2\n", "-19\n"]}
276
139
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an m x n matrix mat that has its rows sorted in non-decreasing order and an integer k. You are allowed to choose exactly one element from each row to form an array. Return the kth smallest array sum among all possible arrays. Β  Please complete the following python code precisely: ```python class Solution: def kthSmallest(self, mat: List[List[int]], k: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(mat = [[1,3,11],[2,4,6]], k = 5) == 7\n assert candidate(mat = [[1,3,11],[2,4,6]], k = 9) == 17\n assert candidate(mat = [[1,10,10],[1,4,5],[2,3,6]], k = 7) == 9\n assert candidate(mat = [[1,1,10],[2,2,9]], k = 7) == 12\n\n\ncheck(Solution().kthSmallest)"}
107
142
coding
Solve the programming task below in a Python markdown code block. The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2). You are given a sequence $a$ consisting of $n$ integers. You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it). For example, for the sequence $[1, 2, 4, 3, 2]$ the answer is $4$ (you take $1$ and the sequence becomes $[2, 4, 3, 2]$, then you take the rightmost element $2$ and the sequence becomes $[2, 4, 3]$, then you take $3$ and the sequence becomes $[2, 4]$ and then you take $4$ and the sequence becomes $[2]$, the obtained increasing sequence is $[1, 2, 3, 4]$). -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of elements in $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- In the first line of the output print $k$ β€” the maximum number of elements in a strictly increasing sequence you can obtain. In the second line print a string $s$ of length $k$, where the $j$-th character of this string $s_j$ should be 'L' if you take the leftmost element during the $j$-th move and 'R' otherwise. If there are multiple answers, you can print any. -----Examples----- Input 5 1 2 4 3 2 Output 4 LRRR Input 7 1 3 5 6 5 4 2 Output 6 LRLRRR Input 3 2 2 2 Output 1 R Input 4 1 2 4 3 Output 4 LLRR -----Note----- The first example is described in the problem statement.
{"inputs": ["1\n1\n", "1\n1\n", "2\n1 1\n", "2\n2 1\n", "2\n1 2\n", "2\n1 1\n", "2\n1 2\n", "2\n2 1\n"], "outputs": ["1\nR\n", "1\nR\n", "1\nR\n", "2\nRR\n", "2\nLR\n", "1\nR", "2\nLR\n", "2\nRR\n"]}
568
113
coding
Solve the programming task below in a Python markdown code block. Imagine there's a big cube consisting of n^3 small cubes. Calculate, how many small cubes are not visible from outside. For example, if we have a cube which has 4 cubes in a row, then the function should return 8, because there are 8 cubes inside our cube (2 cubes in each dimension) Also feel free to reuse/extend the following starter code: ```python def not_visible_cubes(n): ```
{"functional": "_inputs = [[0], [1], [2], [3], [4], [5], [7], [12], [18], [10002]]\n_outputs = [[0], [0], [0], [1], [8], [27], [125], [1000], [4096], [1000000000000]]\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(not_visible_cubes(*i), o[0])"}
103
237
coding
Solve the programming task below in a Python markdown code block. You are given a directed graph with $n$ vertices and $m$ directed edges without self-loops or multiple edges. Let's denote the $k$-coloring of a digraph as following: you color each edge in one of $k$ colors. The $k$-coloring is good if and only if there no cycle formed by edges of same color. Find a good $k$-coloring of given digraph with minimum possible $k$. -----Input----- The first line contains two integers $n$ and $m$ ($2 \le n \le 5000$, $1 \le m \le 5000$) β€” the number of vertices and edges in the digraph, respectively. Next $m$ lines contain description of edges β€” one per line. Each edge is a pair of integers $u$ and $v$ ($1 \le u, v \le n$, $u \ne v$) β€” there is directed edge from $u$ to $v$ in the graph. It is guaranteed that each ordered pair $(u, v)$ appears in the list of edges at most once. -----Output----- In the first line print single integer $k$ β€” the number of used colors in a good $k$-coloring of given graph. In the second line print $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_i \le k$), where $c_i$ is a color of the $i$-th edge (in order as they are given in the input). If there are multiple answers print any of them (you still have to minimize $k$). -----Examples----- Input 4 5 1 2 1 3 3 4 2 4 1 4 Output 1 1 1 1 1 1 Input 3 3 1 2 2 3 3 1 Output 2 1 1 2
{"inputs": ["2 1\n2 1\n", "2 1\n2 1\n", "2 2\n2 1\n1 2\n", "2 2\n2 1\n1 2\n", "10 2\n8 7\n4 5\n", "10 2\n8 7\n10 5\n", "10 2\n8 7\n10 5\n", "10 2\n4 7\n10 5\n"], "outputs": ["1\n1 \n", "1\n1\n", "2\n2 1 \n", "2\n2 1\n", "1\n1 1\n", "1\n1 1 \n", "1\n1 1\n", "1\n1 1\n"]}
435
180
coding
Solve the programming task below in a Python markdown code block. The summer is at its peak in Chefland. Chef is planning to purchase a water cooler to keep his room cool. He has two options available: Rent a cooler at the cost of X coins per month. Purchase a cooler for Y coins. Chef wonders what is the maximum number of months for which he can rent the cooler such that the cost of renting is strictly less than the cost of purchasing it. ------ Input Format ------ - The first line of input will contain an integer T β€” the number of test cases. The description of T test cases follows. - The first and only line of each test case contains two integers X and Y, as described in the problem statement. ------ Output Format ------ For each test case, output the maximum number of months for which he can rent the cooler such that the cost of renting is strictly less than the cost of purchasing it. If Chef should not rent a cooler at all, output 0. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ X, Y ≀ 10^{9}$ ----- Sample Input 1 ------ 2 5 12 5 5 ----- Sample Output 1 ------ 2 0 ----- explanation 1 ------ Test case $1$: Cost of renting the cooler $= 5$ coins per month. Cost of purchasing the cooler $= 12$ coins. So, Chef can rent the cooler for $2$ months at the cost of $10$ coins, which is strictly less than $12$ coins. Test case $2$: Cost of renting the cooler $= 5$ coins per month. Cost of purchasing the cooler $= 5$ coins. If Chef rents the cooler for $1$ month, it will cost $5$ coins, which is not strictly less than the cost of purchasing it. So, Chef should not rent the cooler.
{"inputs": ["2\n5 12\n5 5"], "outputs": ["2\n0"]}
406
23
coding
Solve the programming task below in a Python markdown code block. Tokitsukaze and CSL are playing a little game of stones. In the beginning, there are $n$ piles of stones, the $i$-th pile of which has $a_i$ stones. The two players take turns making moves. Tokitsukaze moves first. On each turn the player chooses a nonempty pile and removes exactly one stone from the pile. A player loses if all of the piles are empty before his turn, or if after removing the stone, two piles (possibly empty) contain the same number of stones. Supposing that both players play optimally, who will win the game? Consider an example: $n=3$ and sizes of piles are $a_1=2$, $a_2=3$, $a_3=0$. It is impossible to choose the empty pile, so Tokitsukaze has two choices: the first and the second piles. If she chooses the first pile then the state will be $[1, 3, 0]$ and it is a good move. But if she chooses the second pile then the state will be $[2, 2, 0]$ and she immediately loses. So the only good move for her is to choose the first pile. Supposing that both players always take their best moves and never make mistakes, who will win the game? Note that even if there are two piles with the same number of stones at the beginning, Tokitsukaze may still be able to make a valid first move. It is only necessary that there are no two piles with the same number of stones after she moves. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of piles. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_1, a_2, \ldots, a_n \le 10^9$), which mean the $i$-th pile has $a_i$ stones. -----Output----- Print "sjfnb" (without quotes) if Tokitsukaze will win, or "cslnb" (without quotes) if CSL will win. Note the output characters are case-sensitive. -----Examples----- Input 1 0 Output cslnb Input 2 1 0 Output cslnb Input 2 2 2 Output sjfnb Input 3 2 3 1 Output sjfnb -----Note----- In the first example, Tokitsukaze cannot take any stone, so CSL will win. In the second example, Tokitsukaze can only take a stone from the first pile, and then, even though they have no stone, these two piles will have the same number of stones, which implies CSL will win. In the third example, Tokitsukaze will win. Here is one of the optimal ways: Firstly, Tokitsukaze can choose the first pile and take a stone from that pile. Then, CSL can only choose the first pile, because if he chooses the second pile, he will lose immediately. Finally, Tokitsukaze can choose the second pile, and then CSL will have no choice but to lose. In the fourth example, they only have one good choice at any time, so Tokitsukaze can make the game lasting as long as possible and finally win.
{"inputs": ["1\n0\n", "1\n1\n", "1\n2\n", "1\n3\n", "1\n4\n", "1\n5\n", "1\n2\n", "1\n5\n"], "outputs": ["cslnb\n", "sjfnb\n", "cslnb\n", "sjfnb\n", "cslnb\n", "sjfnb\n", "cslnb\n", "sjfnb\n"]}
737
102
coding
Solve the programming task below in a Python markdown code block. In the Kingdom of AtCoder, people use a language called Taknese, which uses lowercase English letters. In Taknese, the plural form of a noun is spelled based on the following rules: - If a noun's singular form does not end with s, append s to the end of the singular form. - If a noun's singular form ends with s, append es to the end of the singular form. You are given the singular form S of a Taknese noun. Output its plural form. -----Constraints----- - S is a string of length 1 between 1000, inclusive. - S contains only lowercase English letters. -----Input----- Input is given from Standard Input in the following format: S -----Output----- Print the plural form of the given Taknese word. -----Sample Input----- apple -----Sample Output----- apples apple ends with e, so its plural form is apples.
{"inputs": ["a\n", "s\n", "aox", "bsu", "xoa", "bsv", "xo`", "csv"], "outputs": ["as\n", "ses\n", "aoxs\n", "bsus\n", "xoas\n", "bsvs\n", "xo`s\n", "csvs\n"]}
202
75
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given an integer $C$. Let $d$ be the smallest integer such that $2^{d}$ is strictly greater than $C$. Consider all pairs of non-negative integers $(A,B)$ such that $A,B < 2^{d}$ and $A \oplus B = C$ ($\oplus$ denotes the bitwise XOR operation). Find the maximum value of $A \cdot B$ over all these pairs. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single integer $C$. ------ Output ------ For each test case, print a single line containing one integer ― the maximum possible product $A \cdot B$. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ C ≀ 10^{9}$ ------ Subtasks ------ Subtask #1 (30 points): $1 ≀ C ≀ 10^{3}$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 2 13 10 ----- Sample Output 1 ------ 70 91 ----- explanation 1 ------ Example case 1: The binary representation of $13$ is "1101". We can use $A = 10$ ("1010" in binary) and $B = 7$ ("0111" in binary). This gives us the product $70$. No other valid pair $(A, B)$ can give us a larger product. Example case 2: The binary representation of $10$ is "1010". We can use $A = 13$ ("1101") and $B = 7$ ("0111"). This gives us the maximum product $91$.
{"inputs": ["2\n13\n10"], "outputs": ["70\n91"]}
452
22
coding
Solve the programming task below in a Python markdown code block. *** No Loops Allowed *** You will be given an array (a) and a value (x). All you need to do is check whether the provided array contains the value, without using a loop. Array can contain numbers or strings. X can be either. Return true if the array contains the value, false if not. With strings you will need to account for case. Looking for more, loop-restrained fun? Check out the other kata in the series: https://www.codewars.com/kata/no-loops-1-small-enough https://www.codewars.com/kata/no-loops-3-copy-within Also feel free to reuse/extend the following starter code: ```python def check(a, x): ```
{"functional": "_inputs = [[[66, 101], 66], [[80, 117, 115, 104, 45, 85, 112, 115], 45], [['t', 'e', 's', 't'], 'e'], [['what', 'a', 'great', 'kata'], 'kat'], [[66, 'codewars', 11, 'alex loves pushups'], 'alex loves pushups'], [['come', 'on', 110, '2500', 10, '!', 7, 15], 'Come'], [[\"when's\", 'the', 'next', 'Katathon?', 9, 7], 'Katathon?'], [[8, 7, 5, 'bored', 'of', 'writing', 'tests', 115], 45], [['anyone', 'want', 'to', 'hire', 'me?'], 'me?']]\n_outputs = [[True], [True], [True], [False], [True], [False], [True], [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(check(*i), o[0])"}
166
395
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a 0-indexed array of positive integers tasks, representing tasks that need to be completed in order, where tasks[i] represents the type of the ith task. You are also given a positive integer space, which represents the minimum number of days that must pass after the completion of a task before another task of the same type can be performed. Each day, until all tasks have been completed, you must either: Complete the next task from tasks, or Take a break. Return the minimum number of days needed to complete all tasks. Β  Please complete the following python code precisely: ```python class Solution: def taskSchedulerII(self, tasks: List[int], space: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(tasks = [1,2,1,2,3,1], space = 3) == 9\n assert candidate(tasks = [5,8,8,5], space = 2) == 6\n\n\ncheck(Solution().taskSchedulerII)"}
164
70
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a string s and an integer array indices of the same length. The string s will be shuffled such that the character at the ith position moves to indices[i] in the shuffled string. Return the shuffled string. Β  Please complete the following python code precisely: ```python class Solution: def restoreString(self, s: str, indices: List[int]) -> str: ```
{"functional": "def check(candidate):\n assert candidate(s = \"codeleet\", indices = [4,5,6,7,0,2,1,3]) == \"leetcode\"\n assert candidate(s = \"abc\", indices = [0,1,2]) == \"abc\"\n\n\ncheck(Solution().restoreString)"}
97
74
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a positive integer array nums. The element sum is the sum of all the elements in nums. The digit sum is the sum of all the digits (not necessarily distinct) that appear in nums. Return the absolute difference between the element sum and digit sum of nums. Note that the absolute difference between two integers x and y is defined as |x - y|. Β  Please complete the following python code precisely: ```python class Solution: def differenceOfSum(self, nums: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [1,15,6,3]) == 9\n assert candidate(nums = [1,2,3,4]) == 0\n\n\ncheck(Solution().differenceOfSum)"}
124
57
coding
Solve the programming task below in a Python markdown code block. Your friend won't stop texting his girlfriend. It's all he does. All day. Seriously. The texts are so mushy too! The whole situation just makes you feel ill. Being the wonderful friend that you are, you hatch an evil plot. While he's sleeping, you take his phone and change the autocorrect options so that every time he types "you" or "u" it gets changed to "your sister." Write a function called autocorrect that takes a string and replaces all instances of "you" or "u" (not case sensitive) with "your sister" (always lower case). Return the resulting string. Here's the slightly tricky part: These are text messages, so there are different forms of "you" and "u". For the purposes of this kata, here's what you need to support: "youuuuu" with any number of u characters tacked onto the end "u" at the beginning, middle, or end of a string, but NOT part of a word "you" but NOT as part of another word like youtube or bayou Also feel free to reuse/extend the following starter code: ```python def autocorrect(input): ```
{"functional": "_inputs = [['I miss you!'], ['u want to go to the movies?'], [\"Can't wait to see youuuuu\"], ['I want to film the bayou with you and put it on youtube'], ['You should come over Friday night'], ['You u youville utube you youyouyou uuu raiyou united youuuu u you']]\n_outputs = [['I miss your sister!'], ['your sister want to go to the movies?'], [\"Can't wait to see your sister\"], ['I want to film the bayou with your sister and put it on youtube'], ['your sister should come over Friday night'], ['your sister your sister youville utube your sister youyouyou uuu raiyou united your sister your sister your sister']]\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(autocorrect(*i), o[0])"}
261
307
coding
Solve the programming task below in a Python markdown code block. MoEngage noticed that some users are not aware of the practice page on CodeChef, and some others are not aware of the rated contests on CodeChef. So, MoEngage wants to send an email to the users depending on which of the following groups they fall into: 1. If the user has never submitted on the practice page then send an email with the text: https://www.codechef.com/practice 2. If the user has submitted on the practice page, but never participated in a contest then send an email with the text: https://www.codechef.com/contests 3. If the user has submitted on the practice page as well as participated in a contest then send an email with the text: https://discuss.codechef.com so that they may discuss with other members of the community. Help MoEngage by writing a program that takes as input two integers A and B where: A = 1 if the user has submitted on the practice page and 0 otherwise. B = 1 if the user has participated in a contest and 0 otherwise. Output the appropriate link to be displayed in the email. ------ Input Format ------ - The input consists of a single line with two space-separated integers A and B. ------ Output Format ------ - Print one of the following strings as per the problem statement. - https://www.codechef.com/practice - https://www.codechef.com/contests - https://discuss.codechef.com ------ Constraints ------ $A$ is either $0$ or $1$. $B$ is either $0$ or $1$. ----- Sample Input 1 ------ 0 1 ----- Sample Output 1 ------ https://www.codechef.com/practice ----- explanation 1 ------ The user has not submitted on the practice page, so the program must output the link to the practice page. ----- Sample Input 2 ------ 1 0 ----- Sample Output 2 ------ https://www.codechef.com/contests ----- explanation 2 ------ The user has submitted on the practice page, but has not participated in a contest, so the program must output the link to the contest page.
{"inputs": ["0 1", "1 0"], "outputs": ["https://www.codechef.com/practice", "https://www.codechef.com/contests"]}
465
37
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an array points where points[i] = [xi, yi] represents a point on the X-Y plane, return true if these points are a boomerang. A boomerang is a set of three points that are all distinct and not in a straight line. Β  Please complete the following python code precisely: ```python class Solution: def isBoomerang(self, points: List[List[int]]) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(points = [[1,1],[2,3],[3,2]]) == True\n assert candidate(points = [[1,1],[2,2],[3,3]]) == False\n\n\ncheck(Solution().isBoomerang)"}
106
63
coding
Solve the programming task below in a Python markdown code block. You are given an array $a$ of $n$ non-negative integers. In one operation you can change any number in the array to any other non-negative integer. Let's define the cost of the array as $\operatorname{DIFF}(a) - \operatorname{MEX}(a)$, where $\operatorname{MEX}$ of a set of non-negative integers is the smallest non-negative integer not present in the set, and $\operatorname{DIFF}$ is the number of different numbers in the array. For example, $\operatorname{MEX}(\{1, 2, 3\}) = 0$, $\operatorname{MEX}(\{0, 1, 2, 4, 5\}) = 3$. You should find the minimal cost of the array $a$ if you are allowed to make at most $k$ operations. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 10^5$, $0 \le k \le 10^5$) β€” the length of the array $a$ and the number of operations that you are allowed to make. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$) β€” the elements of the array $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case output a single integer β€” minimal cost that it is possible to get making at most $k$ operations. -----Examples----- Input 4 4 1 3 0 1 2 4 1 0 2 4 5 7 2 4 13 0 0 13 1337 1000000000 6 2 1 2 8 0 0 0 Output 0 1 2 0 -----Note----- In the first test case no operations are needed to minimize the value of $\operatorname{DIFF} - \operatorname{MEX}$. In the second test case it is possible to replace $5$ by $1$. After that the array $a$ is $[0,\, 2,\, 4,\, 1]$, $\operatorname{DIFF} = 4$, $\operatorname{MEX} = \operatorname{MEX}(\{0, 1, 2, 4\}) = 3$, so the answer is $1$. In the third test case one possible array $a$ is $[4,\, 13,\, 0,\, 0,\, 13,\, 1,\, 2]$, $\operatorname{DIFF} = 5$, $\operatorname{MEX} = 3$. In the fourth test case one possible array $a$ is $[1,\, 2,\, 3,\, 0,\, 0,\, 0]$.
{"inputs": ["1\n5 1\n5 4 3 1 2\n", "1\n5 1\n1 2 3 4 5\n", "1\n10 5\n0 1 2 3 100 101 102 102 102 102\n", "5\n5 1\n1 2 3 4 5\n5 1\n1 2 3 4 5\n5 1\n1 2 3 4 5\n5 1\n1 2 3 4 5\n5 1\n1 2 3 4 5\n", "4\n4 1\n3 0 1 2\n4 1\n0 2 4 5\n7 2\n4 13 0 0 13 1337 1000000000\n6 2\n1 2 8 0 0 0\n"], "outputs": ["0\n", "0\n", "1\n", "0\n0\n0\n0\n0\n", "0\n1\n2\n0\n"]}
733
267
coding
Solve the programming task below in a Python markdown code block. You are given a string $\mbox{S}$. $\mbox{S}$ contains alphanumeric characters only. Your task is to sort the string $\mbox{S}$ in the following manner: All sorted lowercase letters are ahead of uppercase letters. All sorted uppercase letters are ahead of digits. All sorted odd digits are ahead of sorted even digits. Input Format A single line of input contains the string $\mbox{S}$. Constraints $0<len(S)<1000$ Output Format Output the sorted string $\mbox{S}$. Sample Input Sorting1234 Sample Output ginortS1324
{"inputs": ["Sorting1234\n"], "outputs": ["ginortS1324\n"]}
150
24
coding
Solve the programming task below in a Python markdown code block. Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0. There are also n cards, each card has 2 attributes: length l_{i} and cost c_{i}. If she pays c_{i} dollars then she can apply i-th card. After applying i-th card she becomes able to make jumps of length l_{i}, i. e. from cell x to cell (x - l_{i}) or cell (x + l_{i}). She wants to be able to jump to any cell on the tape (possibly, visiting some intermediate cells). For achieving this goal, she wants to buy some cards, paying as little money as possible. If this is possible, calculate the minimal cost. -----Input----- The first line contains an integer n (1 ≀ n ≀ 300), number of cards. The second line contains n numbers l_{i} (1 ≀ l_{i} ≀ 10^9), the jump lengths of cards. The third line contains n numbers c_{i} (1 ≀ c_{i} ≀ 10^5), the costs of cards. -----Output----- If it is impossible to buy some cards and become able to jump to any cell, output -1. Otherwise output the minimal cost of buying such set of cards. -----Examples----- Input 3 100 99 9900 1 1 1 Output 2 Input 5 10 20 30 40 50 1 1 1 1 1 Output -1 Input 7 15015 10010 6006 4290 2730 2310 1 1 1 1 1 1 1 10 Output 6 Input 8 4264 4921 6321 6984 2316 8432 6120 1026 4264 4921 6321 6984 2316 8432 6120 1026 Output 7237 -----Note----- In first sample test, buying one card is not enough: for example, if you buy a card with length 100, you can't jump to any cell whose index is not a multiple of 100. The best way is to buy first and second card, that will make you be able to jump to any cell. In the second sample test, even if you buy all cards, you can't jump to any cell whose index is not a multiple of 10, so you should output -1.
{"inputs": ["1\n1\n1\n", "1\n2\n2\n", "1\n1\n1\n", "1\n2\n2\n", "1\n1\n2\n", "1\n4\n2\n", "1\n6\n2\n", "1\n5\n2\n"], "outputs": ["1\n", "-1\n", "1\n", "-1\n", "2\n", "-1\n", "-1\n", "-1\n"]}
628
102
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is a palindrome. Return the minimal number of characters that you need to change to divide the string. Β  Please complete the following python code precisely: ```python class Solution: def palindromePartition(self, s: str, k: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(s = \"abc\", k = 2) == 1\n assert candidate(s = \"aabbc\", k = 3) == 0\n assert candidate(s = \"leetcode\", k = 8) == 0\n\n\ncheck(Solution().palindromePartition)"}
116
72
coding
Solve the programming task below in a Python markdown code block. A permutation of length n is an array containing each integer from 1 to n exactly once. For example, q = [4, 5, 1, 2, 3] is a permutation. For the permutation q the square of permutation is the permutation p that p[i] = q[q[i]] for each i = 1... n. For example, the square of q = [4, 5, 1, 2, 3] is p = q^2 = [2, 3, 4, 5, 1]. This problem is about the inverse operation: given the permutation p you task is to find such permutation q that q^2 = p. If there are several such q find any of them. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^6) β€” the number of elements in permutation p. The second line contains n distinct integers p_1, p_2, ..., p_{n} (1 ≀ p_{i} ≀ n) β€” the elements of permutation p. -----Output----- If there is no permutation q such that q^2 = p print the number "-1". If the answer exists print it. The only line should contain n different integers q_{i} (1 ≀ q_{i} ≀ n) β€” the elements of the permutation q. If there are several solutions print any of them. -----Examples----- Input 4 2 1 4 3 Output 3 4 2 1 Input 4 2 1 3 4 Output -1 Input 5 2 3 4 5 1 Output 4 5 1 2 3
{"inputs": ["1\n1\n", "1\n1\n", "1\n1\n", "4\n2 1 4 3\n", "4\n2 1 3 4\n", "4\n1 2 4 3\n", "4\n1 4 2 3\n", "4\n3 1 2 4\n"], "outputs": ["1\n", "1\n", "1\n", "3 4 2 1\n", "-1\n", "-1\n", "1 3 4 2\n", "2 3 1 4\n"]}
371
134
coding
Solve the programming task below in a Python markdown code block. You are given a straight line, $a\cdot x+b\cdot y=c$. Find the point closest to the origin that also satisfies the following properties: $\boldsymbol{x}$ and $y$ are integers. $\boldsymbol{x}$ is greater than zero. If more than one solution exists satisfying $\mbox{1}$ and $2$, then choose the point in which $\boldsymbol{x}$ is minimal. Given $\textit{q}$ queries consisting of $a_i$, $b_i$, and $c_i$, find and print the point satisfying the above conditions for each respective query. Each point must be printed on a new line as two space-separated integers denoting the point's respective $x_i$ and $y_i$ values. Note: It is guaranteed that there will always be integral points on the line. Input Format The first line contains an integer, $\textit{q}$, denoting the number of queries. Each line $\boldsymbol{i}$ of the $\textit{q}$ subsequent lines contains three space-separated integers describing the respective values of $a_i$, $b_i$, and $c_i$ for the query. Constraints $1\leq q\leq10^5$ $1\leq a\leq10^8$ $1\leq b\leq10^8$ $1\leq c\leq10^8$ Output Format For each query, print $2$ space-separated integers on a new line denoting the respective values of $x_i$ and $y_i$ for the point satisfying the $i^{\mbox{th}}$ query. Sample Input 1 2 3 1 Sample Output 2 -1 Explanation Given the line $2\cdot x+3\cdot y=1$, the point $(2,-1)$ is on the line and satisfies the conditions specified above. Thus, we print the coordinate as two space-separated integers on a new line.
{"inputs": ["1\n2 3 1\n"], "outputs": ["2 -1\n"]}
439
22
coding
Solve the programming task below in a Python markdown code block. A burglar got into a matches warehouse and wants to steal as many matches as possible. In the warehouse there are m containers, in the i-th container there are ai matchboxes, and each matchbox contains bi matches. All the matchboxes are of the same size. The burglar's rucksack can hold n matchboxes exactly. Your task is to find out the maximum amount of matches that a burglar can carry away. He has no time to rearrange matches in the matchboxes, that's why he just chooses not more than n matchboxes so that the total amount of matches in them is maximal. Input The first line of the input contains integer n (1 ≀ n ≀ 2Β·108) and integer m (1 ≀ m ≀ 20). The i + 1-th line contains a pair of numbers ai and bi (1 ≀ ai ≀ 108, 1 ≀ bi ≀ 10). All the input numbers are integer. Output Output the only number β€” answer to the problem. Examples Input 7 3 5 10 2 5 3 6 Output 62 Input 3 3 1 3 2 2 3 1 Output 7
{"inputs": ["1 1\n1 2\n", "2 1\n2 1\n", "4 1\n2 2\n", "3 1\n2 1\n", "4 1\n4 2\n", "4 1\n4 4\n", "2 1\n4 4\n", "5 1\n10 5\n"], "outputs": ["2\n", "2\n", "4\n", "2\n", "8\n", "16\n", "8\n", "25\n"]}
273
121
coding
Solve the programming task below in a Python markdown code block. If there is an integer not less than 0 satisfying the following conditions, print the smallest such integer; otherwise, print -1. - The integer has exactly N digits in base ten. (We assume 0 to be a 1-digit integer. For other integers, leading zeros are not allowed.) - The s_i-th digit from the left is c_i. \left(i = 1, 2, \cdots, M\right) -----Constraints----- - All values in input are integers. - 1 \leq N \leq 3 - 0 \leq M \leq 5 - 1 \leq s_i \leq N - 0 \leq c_i \leq 9 -----Input----- Input is given from Standard Input in the following format: N M s_1 c_1 \vdots s_M c_M -----Output----- Print the answer. -----Sample Input----- 3 3 1 7 3 2 1 7 -----Sample Output----- 702 702 satisfies the conditions - its 1-st and 3-rd digits are 7 and 2, respectively - while no non-negative integer less than 702 satisfies them.
{"inputs": ["3 0\n", "1 0\n", "3 1\n2 0", "3 1\n0 0", "3 0\n1 0", "3 0\n2 0", "3 0\n2 1", "3 0\n3 1"], "outputs": ["100\n", "0\n", "100\n", "100\n", "100\n", "100\n", "100\n", "100\n"]}
277
118
coding
Solve the programming task below in a Python markdown code block. Jim is doing his discrete maths homework which requires him to repeatedly calculate ^{n}C_{r}(n choose r) for different values of n. Knowing that this is time consuming, he goes to his sister June for help. June, being a computer science student knows how to convert this into a computer program and generate the answers quickly. She tells him, by storing the lower values of ^{n}C_{r}(n choose r), one can calculate the higher values using a very simple formula. If you are June, how will you calculate ^{n}C_{r} values for different values of n? Since ${}^{n}C_{r}$values will be large you have to calculate them modulo $\mathbf{10^{9}}$. Input Format The first line contains the number of test cases T. T lines follow each containing an integer n. Output Format For each n output the list of ^{n}C_{0} to ^{n}C_{n} each separated by a single space in a new line. If the number is large, print only the last 9 digits. i.e. modulo 10^{9} Constraints 1<=T<=200 1<=n< 1000 Sample Input 3 2 4 5 Sample Output 1 2 1 1 4 6 4 1 1 5 10 10 5 1 Explanation For 2 we can check ^{2}C_{0} ^{2}C_{1} and ^{2}C_{2} are 1, 2 and 1 respectively. The other inputs' answer follow similar pattern.
{"inputs": ["3\n2\n4\n5\n"], "outputs": ["1 2 1\n1 4 6 4 1\n1 5 10 10 5 1\n"]}
378
48
coding
Solve the programming task below in a Python markdown code block. Complete the function `power_of_two`/`powerOfTwo` (or equivalent, depending on your language) that determines if a given non-negative integer is a [power of two](https://en.wikipedia.org/wiki/Power_of_two). From the corresponding Wikipedia entry: > *a power of two is a number of the form 2^(n) where **n** is an integer, i.e. the result of exponentiation with number two as the base and integer **n** as the exponent.* You may assume the input is always valid. ## Examples ~~~if-not:nasm ```python power_of_two(1024) ==> True power_of_two(4096) ==> True power_of_two(333) ==> False ``` ~~~ ~~~if:nasm ``` mov edi, 0 call power_of_two ; returns false (zero) mov edi, 16 call power_of_two ; returns true (non-zero) mov edi, 100 call power_of_two ; returns false mov edi, 1024 call power_of_two ; returns true mov edi, 20000 call power_of_two ; returns false ``` ~~~ Beware of certain edge cases - for example, `1` is a power of `2` since `2^0 = 1` and `0` is not a power of `2`. Also feel free to reuse/extend the following starter code: ```python def power_of_two(x): ```
{"functional": "_inputs = [[0], [1], [536870912]]\n_outputs = [[False], [True], [True]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(power_of_two(*i), o[0])"}
344
175
coding
Solve the programming task below in a Python markdown code block. There are X+Y+Z people, conveniently numbered 1 through X+Y+Z. Person i has A_i gold coins, B_i silver coins and C_i bronze coins. Snuke is thinking of getting gold coins from X of those people, silver coins from Y of the people and bronze coins from Z of the people. It is not possible to get two or more different colors of coins from a single person. On the other hand, a person will give all of his/her coins of the color specified by Snuke. Snuke would like to maximize the total number of coins of all colors he gets. Find the maximum possible number of coins. Constraints * 1 \leq X * 1 \leq Y * 1 \leq Z * X+Y+Z \leq 10^5 * 1 \leq A_i \leq 10^9 * 1 \leq B_i \leq 10^9 * 1 \leq C_i \leq 10^9 Input Input is given from Standard Input in the following format: X Y Z A_1 B_1 C_1 A_2 B_2 C_2 : A_{X+Y+Z} B_{X+Y+Z} C_{X+Y+Z} Output Print the maximum possible total number of coins of all colors he gets. Examples Input 1 2 1 2 4 4 3 2 1 7 6 7 5 2 3 Output 18 Input 3 3 2 16 17 1 2 7 5 2 16 12 17 7 7 13 2 10 12 18 3 16 15 19 5 6 2 Output 110 Input 6 2 4 33189 87907 277349742 71616 46764 575306520 8801 53151 327161251 58589 4337 796697686 66854 17565 289910583 50598 35195 478112689 13919 88414 103962455 7953 69657 699253752 44255 98144 468443709 2332 42580 752437097 39752 19060 845062869 60126 74101 382963164 Output 3093929975
{"inputs": ["1 2 1\n2 4 4\n0 2 1\n7 6 7\n5 2 3", "1 2 1\n2 4 4\n1 2 1\n7 6 2\n5 3 3", "1 2 1\n2 4 6\n0 2 1\n7 6 7\n5 2 3", "1 2 1\n2 4 4\n1 2 1\n7 0 2\n5 3 3", "1 2 1\n2 4 6\n0 2 1\n7 6 7\n9 2 3", "1 2 1\n2 4 7\n3 2 2\n7 6 7\n5 4 3", "1 2 1\n2 4 4\n3 2 1\n7 6 7\n5 2 3", "1 2 1\n2 4 4\n1 2 1\n7 6 11\n5 3 3"], "outputs": ["18\n", "17\n", "19\n", "16\n", "23\n", "20\n", "18", "22\n"]}
712
294
coding
Solve the programming task below in a Python markdown code block. Takahashi loves the number 7 and multiples of K. Where is the first occurrence of a multiple of K in the sequence 7,77,777,\ldots? (Also see Output and Sample Input/Output below.) If the sequence contains no multiples of K, print -1 instead. -----Constraints----- - 1 \leq K \leq 10^6 - K is an integer. -----Input----- Input is given from Standard Input in the following format: K -----Output----- Print an integer representing the position of the first occurrence of a multiple of K. (For example, if the first occurrence is the fourth element of the sequence, print 4.) -----Sample Input----- 101 -----Sample Output----- 4 None of 7, 77, and 777 is a multiple of 101, but 7777 is.
{"inputs": ["3", "1", "9", "2", "2\n", "1\n", "3\n", "7\n"], "outputs": ["3\n", "1\n", "9\n", "-1", "-1\n", "1\n", "3\n", "1\n"]}
202
65
coding
Solve the programming task below in a Python markdown code block. Given a string, capitalize the letters that occupy even indexes and odd indexes separately, and return as shown below. Index `0` will be considered even. For example, `capitalize("abcdef") = ['AbCdEf', 'aBcDeF']`. See test cases for more examples. The input will be a lowercase string with no spaces. Good luck! If you like this Kata, please try: [Indexed capitalization](https://www.codewars.com/kata/59cfc09a86a6fdf6df0000f1) [Even-odd disparity](https://www.codewars.com/kata/59c62f1bdcc40560a2000060) Also feel free to reuse/extend the following starter code: ```python def capitalize(s): ```
{"functional": "_inputs = [['abcdef'], ['codewars'], ['abracadabra'], ['codewarriors'], ['indexinglessons'], ['codingisafunactivity']]\n_outputs = [[['AbCdEf', 'aBcDeF']], [['CoDeWaRs', 'cOdEwArS']], [['AbRaCaDaBrA', 'aBrAcAdAbRa']], [['CoDeWaRrIoRs', 'cOdEwArRiOrS']], [['InDeXiNgLeSsOnS', 'iNdExInGlEsSoNs']], [['CoDiNgIsAfUnAcTiViTy', 'cOdInGiSaFuNaCtIvItY']]]\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(capitalize(*i), o[0])"}
192
291
coding
Solve the programming task below in a Python markdown code block. Recently, Norge found a string $s = s_1 s_2 \ldots s_n$ consisting of $n$ lowercase Latin letters. As an exercise to improve his typing speed, he decided to type all substrings of the string $s$. Yes, all $\frac{n (n + 1)}{2}$ of them! A substring of $s$ is a non-empty string $x = s[a \ldots b] = s_{a} s_{a + 1} \ldots s_{b}$ ($1 \leq a \leq b \leq n$). For example, "auto" and "ton" are substrings of "automaton". Shortly after the start of the exercise, Norge realized that his keyboard was broken, namely, he could use only $k$ Latin letters $c_1, c_2, \ldots, c_k$ out of $26$. After that, Norge became interested in how many substrings of the string $s$ he could still type using his broken keyboard. Help him to find this number. -----Input----- The first line contains two space-separated integers $n$ and $k$ ($1 \leq n \leq 2 \cdot 10^5$, $1 \leq k \leq 26$) β€” the length of the string $s$ and the number of Latin letters still available on the keyboard. The second line contains the string $s$ consisting of exactly $n$ lowercase Latin letters. The third line contains $k$ space-separated distinct lowercase Latin letters $c_1, c_2, \ldots, c_k$ β€” the letters still available on the keyboard. -----Output----- Print a single number β€” the number of substrings of $s$ that can be typed using only available letters $c_1, c_2, \ldots, c_k$. -----Examples----- Input 7 2 abacaba a b Output 12 Input 10 3 sadfaasdda f a d Output 21 Input 7 1 aaaaaaa b Output 0 -----Note----- In the first example Norge can print substrings $s[1\ldots2]$, $s[2\ldots3]$, $s[1\ldots3]$, $s[1\ldots1]$, $s[2\ldots2]$, $s[3\ldots3]$, $s[5\ldots6]$, $s[6\ldots7]$, $s[5\ldots7]$, $s[5\ldots5]$, $s[6\ldots6]$, $s[7\ldots7]$.
{"inputs": ["7 1\naaaaaaa\nb\n", "7 1\naaaaaba\nb\n", "7 1\naaaaaba\na\n", "7 1\naaaaaba\nc\n", "7 1\naaaaaaa\nb\n", "7 2\nabacaba\na b\n", "7 2\nabacaba\na c\n", "7 2\nabacaba\na d\n"], "outputs": ["0\n", "1\n", "16\n", "0\n", "0\n", "12\n", "8\n", "4\n"]}
604
134
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a list ofΒ preferencesΒ forΒ nΒ friends, where n is always even. For each person i,Β preferences[i]Β containsΒ a list of friendsΒ sorted in the order of preference. In other words, a friend earlier in the list is more preferred than a friend later in the list.Β Friends inΒ each list areΒ denoted by integers from 0 to n-1. All the friends are divided into pairs.Β The pairings areΒ given in a listΒ pairs,Β where pairs[i] = [xi, yi] denotes xiΒ is paired with yi and yi is paired with xi. However, this pairing may cause some of the friends to be unhappy.Β A friend xΒ is unhappy if xΒ is paired with yΒ and there exists a friend uΒ whoΒ is paired with vΒ but: xΒ prefers uΒ over y,Β and uΒ prefers xΒ over v. Return the number of unhappy friends. Β  Please complete the following python code precisely: ```python class Solution: def unhappyFriends(self, n: int, preferences: List[List[int]], pairs: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(n = 4, preferences = [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]], pairs = [[0, 1], [2, 3]]) == 2\n assert candidate(n = 2, preferences = [[1], [0]], pairs = [[1, 0]]) == 0\n assert candidate(n = 4, preferences = [[1, 3, 2], [2, 3, 0], [1, 3, 0], [0, 2, 1]], pairs = [[1, 3], [0, 2]]) == 4\n\n\ncheck(Solution().unhappyFriends)"}
264
176
coding
Solve the programming task below in a Python markdown code block. On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}Β·k for 1 ≀ j ≀ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k. Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task? -----Input----- The first line contains two integers n and S (1 ≀ n ≀ 10^5 and 1 ≀ S ≀ 10^9)Β β€” the number of souvenirs in the market and Sagheer's budget. The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^5)Β β€” the base costs of the souvenirs. -----Output----- On a single line, print two integers k, TΒ β€” the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs. -----Examples----- Input 3 11 2 3 5 Output 2 11 Input 4 100 1 2 5 6 Output 4 54 Input 1 7 7 Output 0 0 -----Note----- In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items. In the second example, he can buy all items as they will cost him [5, 10, 17, 22]. In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.
{"inputs": ["1 7\n7\n", "1 7\n5\n", "1 1\n1\n", "1 1\n1\n", "1 7\n5\n", "1 7\n9\n", "1 3\n9\n", "1 7\n7\n"], "outputs": ["0 0\n", "1 6\n", "0 0\n", "0 0", "1 6", "0 0\n", "0 0\n", "0 0"]}
550
115
coding
Solve the programming task below in a Python markdown code block. You are given an expression of the form $a{+}b$, where $a$ and $b$ are integers from $0$ to $9$. You have to evaluate it and print the result. -----Input----- The first line contains one integer $t$ ($1 \le t \le 100$) β€” the number of test cases. Each test case consists of one line containing an expression of the form $a{+}b$ ($0 \le a, b \le 9$, both $a$ and $b$ are integers). The integers are not separated from the $+$ sign. -----Output----- For each test case, print one integer β€” the result of the expression. -----Examples----- Input 4 4+2 0+0 3+7 8+9 Output 6 0 10 17 -----Note----- None
{"inputs": ["1\n5+5\n", "1\n1+1\n", "1\n2+2\n", "1\n5+6\n", "1\n1+4\n", "1\n0+0\n", "1\n2+3\n", "1\n3+1\n"], "outputs": ["10\n", "2\n", "4\n", "11\n", "5\n", "0\n", "5\n", "4\n"]}
199
104
coding
Solve the programming task below in a Python markdown code block. The mysterious pathogen of ACM Nation multiplies mysteriously each day. Maria was able to decipher the pattern but unable to completely solve the problem. The study of this equation will help in combating their growth. The summation is - S = a + aa + aa*a + ...... a^n times Where a is the number of pathogen on the first day and S on the nth day. Input : a and n are taken as input from the user. First line will contain a,followed by n. Output: a single integer depicting the sum Sample Input: 2 4 Sample Output: 30 SAMPLE INPUT 2 4 SAMPLE OUTPUT 30
{"inputs": ["1\n5", "2\n6"], "outputs": ["5", "126"]}
157
24
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an array words of size n consisting of non-empty strings. We define the score of a string term as the number of strings words[i] such that term is a prefix of words[i]. For example, if words = ["a", "ab", "abc", "cab"], then the score of "ab" is 2, since "ab" is a prefix of both "ab" and "abc". Return an array answer of size n where answer[i] is the sum of scores of every non-empty prefix of words[i]. Note that a string is considered as a prefix of itself. Β  Please complete the following python code precisely: ```python class Solution: def sumPrefixScores(self, words: List[str]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(words = [\"abc\",\"ab\",\"bc\",\"b\"]) == [5,4,3,2]\n assert candidate(words = [\"abcd\"]) == [4]\n\n\ncheck(Solution().sumPrefixScores)"}
173
62
coding
Solve the programming task below in a Python markdown code block. Chef likes arrays a lot. Today, he found an array A consisting of N positive integers. Let L denote the sorted (in non-increasing order) list of size N*(N+1)/2 containing the sums of all possible contiguous subarrays of A. Chef is interested in finding the first K elements from the list L. Can you help him in accomplishing this task? -----Input----- There is only a single test case per input file. The first line of input contains two space separated integer numbers N and K denoting the size of the array and the number of the maximal sums you need to find. The following line contains N space separated integer numbers denoting the array A. -----Output----- Output K space separated integers where the ith integer denotes the ith element of L. -----Constraints----- - 1 ≀ N ≀ 105 - 1 ≀ K ≀ min(N*(N+1)/2, 105) - 1 ≀ Ai ≀ 109 -----Subtasks----- - Subtask 1 (47 pts) : 1 ≀ N ≀ 1000, 1 ≀ K ≀ min{N*(N+1)/2, 105} - Subtask 2 (53 pts) : 1 ≀ N ≀ 105, 1 ≀ K ≀ min{N*(N+1)/2, 105} -----Example----- Input 13 4 1 3 4 Output 18 7 4 4 Input 23 3 10 2 7 Output 219 12 10 -----Explanation----- Test 1: The first 4 elements of it are [8, 7, 4, 4].
{"inputs": ["3 4\n1 3 4", "3 3\n10 2 7"], "outputs": ["8 7 4 4", "19 12 10"]}
383
48
coding
Solve the programming task below in a Python markdown code block. Let N be a positive even number. We have a permutation of (1, 2, ..., N), p = (p_1, p_2, ..., p_N). Snuke is constructing another permutation of (1, 2, ..., N), q, following the procedure below. First, let q be an empty sequence. Then, perform the following operation until p becomes empty: - Select two adjacent elements in p, and call them x and y in order. Remove x and y from p (reducing the length of p by 2), and insert x and y, preserving the original order, at the beginning of q. When p becomes empty, q will be a permutation of (1, 2, ..., N). Find the lexicographically smallest permutation that can be obtained as q. -----Constraints----- - N is an even number. - 2 ≀ N ≀ 2 Γ— 10^5 - p is a permutation of (1, 2, ..., N). -----Input----- Input is given from Standard Input in the following format: N p_1 p_2 ... p_N -----Output----- Print the lexicographically smallest permutation, with spaces in between. -----Sample Input----- 4 3 2 4 1 -----Sample Output----- 3 1 2 4 The solution above is obtained as follows:pq(3, 2, 4, 1)()↓↓(3, 1)(2, 4)↓↓()(3, 1, 2, 4)
{"inputs": ["2\n0 2", "2\n0 1", "2\n0 0", "2\n2 2", "2\n1 0", "2\n1 1", "2\n1 2", "2\n1 2\n"], "outputs": ["0 2 ", "0 1 ", "0 0 ", "2 2 ", "1 0 ", "1 1 ", "1 2", "1 2\n"]}
337
104
coding
Solve the programming task below in a Python markdown code block. Billy investigates the question of applying greedy algorithm to different spheres of life. At the moment he is studying the application of greedy algorithm to the problem about change. There is an amount of n coins of different face values, and the coins of each value are not limited in number. The task is to collect the sum x with the minimum amount of coins. Greedy algorithm with each its step takes the coin of the highest face value, not exceeding x. Obviously, if among the coins' face values exists the face value 1, any sum x can be collected with the help of greedy algorithm. However, greedy algorithm does not always give the optimal representation of the sum, i.e. the representation with the minimum amount of coins. For example, if there are face values {1, 3, 4} and it is asked to collect the sum 6, greedy algorithm will represent the sum as 4 + 1 + 1, while the optimal representation is 3 + 3, containing one coin less. By the given set of face values find out if there exist such a sum x that greedy algorithm will collect in a non-optimal way. If such a sum exists, find out the smallest of these sums. Input The first line contains an integer n (1 ≀ n ≀ 400) β€” the amount of the coins' face values. The second line contains n integers ai (1 ≀ ai ≀ 109), describing the face values. It is guaranteed that a1 > a2 > ... > an and an = 1. Output If greedy algorithm collects any sum in an optimal way, output -1. Otherwise output the smallest sum that greedy algorithm collects in a non-optimal way. Examples Input 5 25 10 5 2 1 Output -1 Input 3 4 3 1 Output 6
{"inputs": ["1\n1\n", "2\n2 1\n", "2\n4 1\n", "3\n3 2 1\n", "3\n4 2 1\n", "3\n4 3 1\n", "4\n5 4 3 1\n", "3\n25 15 1\n"], "outputs": ["-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "6\n", "7\n", "30\n"]}
400
116
coding
Solve the programming task below in a Python markdown code block. C: Divisor Game problem tsutaj is trying to play about a few games. In a divisor game, a natural number N of 2 or more is given first, and then the game progresses according to the following procedure. * Declare one integer from the divisors of N other than N. However, at this time, it is not possible to declare a divisor of an integer that has already been declared. * Repeat this as long as there is an integer that can be declared, and if there is nothing that can be declared, the game ends. Find the minimum and maximum number of possible declarations before the end of the game. Input format Input is given on one line. N Constraint * 2 \ leq N \ leq 10 ^ {12} Output format Output the minimum and maximum number of declarations on one line separated by spaces. Input example 1 18 Output example 1 twenty five The following is an example of setting the number of declarations to 2. * Declare 9. * Declare 6. (6 is not a divisor of 9, so it can be declared) If you do this, any integer that is a divisor of 18 and not 18 will be a divisor of the integers you have declared, and the game will end. Note that you cannot declare anything that is a divisor of an integer that you have already declared. For example, you cannot declare 3 after declaring 9. Because 3 is a divisor of 9. Input example 2 99 Output example 2 twenty five Input example 3 10000000019 Output example 3 1 1 The input may not fit in a 32-bit integer type. Example Input 18 Output 2 5
{"inputs": ["2", "8", "7", "4", "5", "3", "6", "9"], "outputs": ["1 1\n", "1 3\n", "1 1\n", "1 2\n", "1 1\n", "1 1\n", "2 3\n", "1 2\n"]}
398
78
coding
Solve the programming task below in a Python markdown code block. Write function describeList which returns "empty" if the list is empty or "singleton" if it contains only one element or "longer"" if more. Also feel free to reuse/extend the following starter code: ```python def describeList(list): ```
{"functional": "_inputs = [[[]], [[1]], [[1, 2, 5, 4]], [['one']], [['one', 'two']], [[[1, 2]]]]\n_outputs = [['empty'], ['singleton'], ['longer'], ['singleton'], ['longer'], ['singleton']]\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(describeList(*i), o[0])"}
66
203
coding
Solve the programming task below in a Python markdown code block. The integer ```64``` is the first integer that has all of its digits even and furthermore, is a perfect square. The second one is ```400``` and the third one ```484```. Give the numbers of this sequence that are in the range ```[a,b] ```(both values inclusive) Examples: ``` python even_digit_squares(100, 1000) == [400, 484] # the output should be sorted. even_digit_squares(1000, 4000) == [] ``` Features of the random tests for ```even_digit_squares(a, b)``` ``` number of Tests = 167 maximum value for a = 1e10 maximum value for b = 1e12 ``` You do not have to check the entries, ```a``` and ```b``` always positive integers and ```a < b``` Happy coding!! Also feel free to reuse/extend the following starter code: ```python def even_digit_squares(a, b): ```
{"functional": "_inputs = [[100, 1000], [1000, 4000], [10000, 40000]]\n_outputs = [[[400, 484]], [[]], [[26244, 28224, 40000]]]\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(even_digit_squares(*i), o[0])"}
243
221
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an array of positive integers nums, return an array answer that consists of the digits of each integer in nums after separating them in the same order they appear in nums. To separate the digits of an integer is to get all the digits it has in the same order. For example, for the integer 10921, the separation of its digits is [1,0,9,2,1]. Β  Please complete the following python code precisely: ```python class Solution: def separateDigits(self, nums: List[int]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(nums = [13,25,83,77]) == [1,3,2,5,8,3,7,7]\n assert candidate(nums = [7,1,3,9]) == [7,1,3,9]\n\n\ncheck(Solution().separateDigits)"}
133
82
coding
Solve the programming task below in a Python markdown code block. Given is a simple undirected graph with N vertices and M edges. Its vertices are numbered 1, 2, \ldots, N and its edges are numbered 1, 2, \ldots, M. On Vertex i (1 \leq i \leq N) two integers A_i and B_i are written. Edge i (1 \leq i \leq M) connects Vertices U_i and V_i. Snuke picks zero or more vertices and delete them. Deleting Vertex i costs A_i. When a vertex is deleted, edges that are incident to the vertex are also deleted. The score after deleting vertices is calculated as follows: - The score is the sum of the scores of all connected components. - The score of a connected component is the absolute value of the sum of B_i of the vertices in the connected component. Snuke's profit is (score) - (the sum of costs). Find the maximum possible profit Snuke can gain. -----Constraints----- - 1 \leq N \leq 300 - 1 \leq M \leq 300 - 1 \leq A_i \leq 10^6 - -10^6 \leq B_i \leq 10^6 - 1 \leq U_i,V_i \leq N - The given graph does not contain self loops or multiple edges. - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: N M A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N U_1 V_1 U_2 V_2 \vdots U_M V_M -----Output----- Print the maximum possible profit Snuke can gain. -----Sample Input----- 4 4 4 1 2 3 0 2 -3 1 1 2 2 3 3 4 4 2 -----Sample Output----- 1 Deleting Vertex 2 costs 1. After that, the graph is separated into two connected components. The score of the component consisting of Vertex 1 is |0| = 0. The score of the component consisting of Vertices 3 and 4 is |(-3) + 1| = 2. Therefore, Snuke's profit is 0 + 2 - 1 = 1. He cannot gain more than 1, so the answer is 1.
{"inputs": ["4 2\n1 1 1 1\n1 1 -1 -1\n1 2\n3 4\n", "4 4\n4 1 2 3\n0 2 -3 1\n1 2\n2 3\n3 4\n4 2\n", "2 1\n273747 996499\n565109 -976650\n2 1\n", "4 6\n14121 332351 255931 165916\n198395 696843 64607 388580\n4 1\n4 2\n3 1\n4 3\n2 3\n2 1\n", "11 6\n826528 600459 788854 191285 93501 904786 49857 946608 481594 523737 115487\n-148945 987487 939990 -167409 188794 -772456 138230 594061 916490 -830385 638518\n5 11\n1 8\n2 3\n1 2\n6 4\n6 5\n", "10 12\n733454 729489 956011 464983 822120 364691 271012 762026 751760 965431\n-817837 -880667 -822819 -131079 740891 581865 -191711 -383018 273044 476880\n3 1\n4 1\n6 9\n3 8\n1 6\n10 5\n5 6\n1 5\n4 3\n7 1\n7 4\n10 3\n", "17 14\n535836 509708 756257 869453 300399 358080 208973 646826 593906 405949 510117 986050 962850 333074 460007 827934 799886\n-502029 730543 -621110 232599 -377576 -217563 -38719 196448 -518799 -151275 336135 -482639 938220 157437 -92054 115308 -222157\n8 7\n17 6\n10 17\n2 5\n10 1\n11 7\n3 13\n1 6\n12 14\n16 9\n14 5\n10 4\n12 10\n15 14\n", "13 68\n499398 28287 404314 878535 50767 981874 848690 518110 436 269908 380073 863780 795274\n894076 -224247 339937 -135404 2070 750535 -509609 -861554 593495 291869 238517 -922749 -545575\n4 9\n4 12\n7 12\n2 1\n11 9\n12 13\n6 5\n8 7\n13 1\n8 9\n11 13\n4 6\n11 2\n8 6\n10 5\n7 13\n8 3\n8 5\n9 1\n11 3\n3 4\n2 12\n3 12\n11 10\n12 5\n6 9\n11 8\n4 1\n8 4\n2 6\n5 1\n2 9\n11 4\n10 4\n10 9\n4 7\n10 1\n11 1\n11 6\n10 13\n5 13\n11 5\n13 9\n2 7\n6 7\n5 9\n12 9\n10 3\n8 13\n2 10\n7 9\n3 5\n10 6\n2 5\n4 5\n2 4\n8 12\n3 13\n2 3\n3 9\n2 13\n10 12\n10 7\n3 7\n2 8\n12 1\n7 5\n4 13\n"], "outputs": ["4\n", "1\n", "702903\n", "1348425\n", "5742580\n", "2306209\n", "2922290\n", "1098337\n"]}
547
1,460
coding
Solve the programming task below in a Python markdown code block. Isaac has to buy a new HackerPhone for his girlfriend Amy. He is exploring the shops in the town to compare the prices whereupon he finds a shop located on the first floor of a building, that has a unique pricing policy. There are N steps leading to the shop. A numbered ball is placed on each of the steps. The shopkeeper gives Isaac a fair coin and asks him to toss the coin before climbing each step. If the result of the toss is a 'Heads', Isaac should pick up the ball, else leave it and proceed to the next step. The shopkeeper then asks Isaac to find the sum of all the numbers he has picked up (let's say S). The price of the HackerPhone is then the expected value of S. Help Isaac find the price of the HackerPhone. Input Format The first line of the input contains an integer N, the number of steps. N lines follow, which are the numbers written on the ball on each step. Output Format A single line containing expected value. Note : Expected value must be printed as a decimal number having exactly one digit after decimal. It is guaranteed that the correct answer will have at most one digit after decimal. Constraints 1 <= N <= 40 1 <= number on any ball <=10^{9} Sample Input #00: 3 1 1 2 Sample Output #00: 2.0 Sample Input #01: 4 1 2 2 2 Sample Output #01: 3.5 Explanation In the first case there can be 8 different ways depending on the ball choice. So, we can multiply sum for each way by its probability to occur i.e. 1/8 and sum up to get the expected value as 2. Similarly in the second case we have 4 items , there can be 16 ways and following the expected value one gets is 3.5.
{"inputs": ["3\n1 \n1\n2\n", "4\n1 \n2\n2\n2\n"], "outputs": ["2.0\n", "3.5\n"]}
416
42
coding
Solve the programming task below in a Python markdown code block. Recently, Vladimir got bad mark in algebra again. To avoid such unpleasant events in future he decided to train his arithmetic skills. He wrote four integer numbers a, b, c, d on the blackboard. During each of the next three minutes he took two numbers from the blackboard (not necessarily adjacent) and replaced them with their sum or their product. In the end he got one number. Unfortunately, due to the awful memory he forgot that number, but he remembers four original numbers, sequence of the operations and his surprise because of the very small result. Help Vladimir remember the forgotten number: find the smallest number that can be obtained from the original numbers by the given sequence of operations. Input First line contains four integers separated by space: 0 ≀ a, b, c, d ≀ 1000 β€” the original numbers. Second line contains three signs ('+' or '*' each) separated by space β€” the sequence of the operations in the order of performing. ('+' stands for addition, '*' β€” multiplication) Output Output one integer number β€” the minimal result which can be obtained. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d). Examples Input 1 1 1 1 + + * Output 3 Input 2 2 2 2 * * + Output 8 Input 1 2 3 4 * + + Output 9
{"inputs": ["3 2 2 2\n* * +\n", "1 2 3 4\n* + *\n", "2 2 2 2\n* * +\n", "1 1 1 1\n+ + *\n", "1 2 3 4\n* + +\n", "15 1 3 1\n* * +\n", "8 1 7 14\n+ + +\n", "7 0 8 15\n+ + *\n"], "outputs": ["10\n", "10\n", "8\n", "3\n", "9\n", "18\n", "30\n", "0\n"]}
329
162
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a network of n nodes, labeled from 1 to n. You are also given times, a list of travel times as directed edges times[i] = (ui, vi, wi), where ui is the source node, vi is the target node, and wi is the time it takes for a signal to travel from source to target. We will send a signal from a given node k. Return the minimum time it takes for all the n nodes to receive the signal. If it is impossible for all the n nodes to receive the signal, return -1. Β  Please complete the following python code precisely: ```python class Solution: def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(times = [[2,1,1],[2,3,1],[3,4,1]], n = 4, k = 2) == 2\n assert candidate(times = [[1,2,1]], n = 2, k = 1) == 1\n assert candidate(times = [[1,2,1]], n = 2, k = 2) == -1\n\n\ncheck(Solution().networkDelayTime)"}
174
110
coding
Solve the programming task below in a Python markdown code block. An accordion is a string (yes, in the real world accordions are musical instruments, but let's forget about it for a while) which can be represented as a concatenation of: an opening bracket (ASCII code $091$), a colon (ASCII code $058$), some (possibly zero) vertical line characters (ASCII code $124$), another colon, and a closing bracket (ASCII code $093$). The length of the accordion is the number of characters in it. For example, [::], [:||:] and [:|||:] are accordions having length $4$, $6$ and $7$. (:|:), {:||:}, [:], ]:||:[ are not accordions. You are given a string $s$. You want to transform it into an accordion by removing some (possibly zero) characters from it. Note that you may not insert new characters or reorder existing ones. Is it possible to obtain an accordion by removing characters from $s$, and if so, what is the maximum possible length of the result? -----Input----- The only line contains one string $s$ ($1 \le |s| \le 500000$). It consists of lowercase Latin letters and characters [, ], : and |. -----Output----- If it is not possible to obtain an accordion by removing some characters from $s$, print $-1$. Otherwise print maximum possible length of the resulting accordion. -----Examples----- Input |[a:b:|] Output 4 Input |]:[|:] Output -1
{"inputs": ["[\n", "|\n", "]\n", ":\n", "b\n", "c\n", "s\n", "z\n"], "outputs": ["-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n"]}
342
71
coding
Solve the programming task below in a Python markdown code block. You are given a string S of numbers. Dr. Dragoon has eaten up some numbers of the string. You decide to fix the string by putting the numbers 0 and 1 in the empty spaces. The base cost for putting the β€˜0’ is x and putting β€˜1’ is y . The cost of putting a number is the base cost multiplied by the number of times you use the letter till that position (including the position ). The local mayor is happy with your work and commissions you for every number you put. More specifically , if you put , say 0 in 5th position, the mayor will count the number of occurrences of 0 till 5th position and pay you the same amount. You wonder what the minimum cost for fixing the string is. Note – The answer can also be negative, they denote that you got an overall profit. Input Format: The input consists of 2 lines. First-line consists of the string of numbers S. β€˜?’ denotes an empty position. The second line consists of 2 integers x,y, the base cost of 0 and 1. Output Format: Output 1 integer, the minimum cost of fixing the string. Constraints : 1<=|S|<=100 1<=x,y<=100 Sample Input: 501?1? 6 5 Sample Output: 6 Sample Input: 1001?11? 5 23 Sample Output: 8 Explanation : Fill 0 in the first place, the net cost will be 6-2=4. In the second place fill, 1 the net cost will be 5-3=2 Hence total cost is 4+2=6. In second test case we will fill both spaces with zero , filling first zero costs 1*5-3=2 and filling the other zero costs 2*5 - 4 =6 , so total being 8.
{"inputs": ["501?1?\n6 5", "1001?11?\n5 23"], "outputs": ["6", "8"]}
443
40
coding
Solve the programming task below in a Python markdown code block. Look for the Winner! The citizens of TKB City are famous for their deep love in elections and vote counting. Today they hold an election for the next chairperson of the electoral commission. Now the voting has just been closed and the counting is going to start. The TKB citizens have strong desire to know the winner as early as possible during vote counting. The election candidate receiving the most votes shall be the next chairperson. Suppose for instance that we have three candidates A, B, and C and ten votes. Suppose also that we have already counted six of the ten votes and the vote counts of A, B, and C are four, one, and one, respectively. At this moment, every candidate has a chance to receive four more votes and so everyone can still be the winner. However, if the next vote counted is cast for A, A is ensured to be the winner since A already has five votes and B or C can have at most four votes at the end. In this example, therefore, the TKB citizens can know the winner just when the seventh vote is counted. Your mission is to write a program that receives every vote counted, one by one, identifies the winner, and determines when the winner gets ensured. Input The input consists of at most 1500 datasets, each consisting of two lines in the following format. n c1 c2 … cn n in the first line represents the number of votes, and is a positive integer no greater than 100. The second line represents the n votes, separated by a space. Each ci (1 ≀ i ≀ n) is a single uppercase letter, i.e. one of 'A' through 'Z'. This represents the election candidate for which the i-th vote was cast. Counting shall be done in the given order from c1 to cn. You should assume that at least two stand as candidates even when all the votes are cast for one candidate. The end of the input is indicated by a line containing a zero. Output For each dataset, unless the election ends in a tie, output a single line containing an uppercase letter c and an integer d separated by a space: c should represent the election winner and d should represent after counting how many votes the winner is identified. Otherwise, that is, if the election ends in a tie, output a single line containing `TIE'. Sample Input 1 A 4 A A B B 5 L M N L N 6 K K K K K K 6 X X X Y Z X 10 A A A B A C A C C B 10 U U U U U V V W W W 0 Output for the Sample Input A 1 TIE TIE K 4 X 5 A 7 U 8 Example Input 1 A 4 A A B B 5 L M N L N 6 K K K K K K 6 X X X Y Z X 10 A A A B A C A C C B 10 U U U U U V V W W W 0 Output A 1 TIE TIE K 4 X 5 A 7 U 8
{"inputs": ["1\nA\n4\nA A B C\n5\nL M N L O\n1\nJ K K K J K\n0\nX Y Y Y Z X\n8\nB B A B A C A D C B\n1\nV V U U U W V W V V\n0", "1\nA\n4\nA A B B\n5\nL M N L N\n6\nK K K K K K\n6\nX X X Y Z X\n10\nA A A B A C A C C B\n1\nV U U U U V V W V W\n0", "1\nA\n4\nA A B B\n5\nL M N L N\n6\nK K K K K K\n6\nX X X Y Z X\n10\nA A A B A C A C C B\n1\nU U U U U V V W V W\n0", "1\nA\n4\nA A B B\n5\nL M N L N\n1\nJ K K K K K\n0\nX X X Y Z X\n10\nB A A B A C A C C B\n0\nV U U U U V V W V W\n0", "1\nA\n4\nA A C B\n5\nL M N L N\n1\nJ K K K K K\n0\nX X X Y Y X\n10\nB A A B A C A C C B\n0\nV U U U U V V W W W\n0", "1\nA\n4\nA A C B\n5\nK M N L N\n1\nJ K K K K K\n0\nX X X Y Y X\n10\nB A A B A C A D C B\n0\nV U U U U V V W W W\n0", "1\nA\n4\nB A C B\n5\nL M M M N\n2\nK K K K J K\n0\nX Y W Y Z Y\n4\nB A A B B C B C D B\n10\nV U U U U V X W V W\n0", "1\nA\n4\nA A B C\n5\nL M L L O\n1\nJ K K K I K\n0\nX Y Y Y Z X\n13\nB B A B A C A D C B\n1\nV V U U U W W W V V\n0"], "outputs": ["A 1\nA 4\nL 5\nJ 1\n", "A 1\nTIE\nTIE\nK 4\nX 5\nA 7\nV 1\n", "A 1\nTIE\nTIE\nK 4\nX 5\nA 7\nU 1\n", "A 1\nTIE\nTIE\nJ 1\n", "A 1\nA 4\nTIE\nJ 1\n", "A 1\nA 4\nN 5\nJ 1\n", "A 1\nB 4\nM 4\nK 2\n", "A 1\nA 4\nL 4\nJ 1\n"]}
699
702
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, column, and both diagonals all have the same sum. Given a row x col grid of integers, how many 3 x 3 magic square subgrids are there? Note: while a magic square can only contain numbers from 1 to 9, grid may contain numbers up to 15. Β  Please complete the following python code precisely: ```python class Solution: def numMagicSquaresInside(self, grid: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(grid = [[4,3,8,4],[9,5,1,9],[2,7,6,2]]) == 1\n assert candidate(grid = [[8]]) == 0\n\n\ncheck(Solution().numMagicSquaresInside)"}
146
68
coding
Solve the programming task below in a Python markdown code block. Chef is interested to solve series problems. Chef wants to solve a series problem but he can't solve it till now.Can you help Chef to solve the series problem? - In series problem, the series goes as follows 1,9,31,73,141 . . . . . . . . Your task is to find the Nth term of series. For larger value of $N$ answer becomes very large, So your output should be performed $N$th term modulo 1000000007 ($10^9+7$ ). -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of a single integer $N$. -----Output:----- For each testcase, output in a single line answer i.e. The $N$th term of series modulo 1000000007. -----Constraints----- - $1 \leq T \leq 10^5$ - $1 \leq N \leq 10^9$ -----Sample Input:----- 2 8 10 -----Sample Output:----- 561 1081
{"inputs": ["2\n8\n10"], "outputs": ["561\n1081"]}
271
24
coding
Solve the programming task below in a Python markdown code block. You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$). You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$. It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$. Note that you don't have to spend all your $s$ dollars on salaries. You have to answer $t$ test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) β€” the number of test cases. The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) β€” the number of employees and the amount of money you have. The value $n$ is not divisible by $2$. The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$). It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$. -----Output----- For each test case print one integer β€” the maximum median salary that you can obtain. -----Example----- Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 -----Note----- In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$. In the second test case, you have to pay $1337$ dollars to the only employee. In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
{"inputs": ["1\n1 1000000000\n1000000000 1000000000\n", "1\n1 1000000000\n1000000000 1000000000\n", "3\n3 26\n10 12\n1 4\n10 11\n1 945\n1 1000010000\n5 26\n4 4\n0 2\n6 8\n9 6\n2 7\n", "3\n3 26\n6 12\n1 4\n10 11\n1 1337\n1 1000000000\n5 26\n4 4\n0 4\n3 8\n2 6\n2 7\n", "3\n3 26\n7 12\n1 2\n10 14\n1 1337\n1 1000000000\n5 26\n4 3\n0 4\n3 8\n2 6\n0 7\n", "3\n3 26\n7 12\n1 2\n10 14\n1 1337\n1 1000000000\n5 26\n4 3\n0 4\n6 8\n2 6\n0 7\n", "3\n3 26\n8 12\n1 4\n10 11\n1 1337\n1 1000010000\n3 29\n4 4\n2 2\n6 8\n5 6\n2 7\n", "3\n3 26\n7 12\n1 2\n10 14\n1 1337\n1 1000000000\n5 26\n4 3\n0 5\n6 8\n2 6\n0 7\n"], "outputs": ["1000000000\n", "1000000000\n", "11\n945\n6\n", "11\n1337\n6\n", "12\n1337\n6\n", "12\n1337\n6\n", "11\n1337\n4\n", "12\n1337\n6\n"]}
740
595
coding
Solve the programming task below in a Python markdown code block. Andi and Budi were given an assignment to tidy up their bookshelf of $n$ books. Each book is represented by the book title β€” a string $s_i$ numbered from $1$ to $n$, each with length $m$. Andi really wants to sort the book lexicographically ascending, while Budi wants to sort it lexicographically descending. Settling their fight, they decided to combine their idea and sort it asc-desc-endingly, where the odd-indexed characters will be compared ascendingly, and the even-indexed characters will be compared descendingly. A string $a$ occurs before a string $b$ in asc-desc-ending order if and only if in the first position where $a$ and $b$ differ, the following holds: if it is an odd position, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$; if it is an even position, the string $a$ has a letter that appears later in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n \cdot m \leq 10^6$). The $i$-th of the next $n$ lines contains a string $s_i$ consisting of $m$ uppercase Latin letters β€” the book title. The strings are pairwise distinct. -----Output----- Output $n$ integers β€” the indices of the strings after they are sorted asc-desc-endingly. -----Examples----- Input 5 2 AA AB BB BA AZ Output 5 2 1 3 4 -----Note----- The following illustrates the first example.
{"inputs": ["5 2\nAA\nAB\nBB\nBA\nAZ\n", "5 2\nAA\nAB\nBB\nBA\nZA\n", "5 2\nAA\nAB\nCB\nBA\nAZ\n", "2 2\nAA\nAB\nCB\nBA\nAZ\n", "2 2\nAA\nBC\nCB\nAB\nAZ\n", "1 2\nAA\nAC\nBC\nBA\nZA\n", "5 2\nAA\nAB\nBC\nBA\nAZ\n", "3 2\nAA\nAB\nCB\nAB\nAZ\n"], "outputs": ["5 2 1 3 4\n", "2 1 3 4 5\n", "5 2 1 4 3\n", "2 1\n", "1 2\n", "1\n", "5 2 1 3 4\n", "2 1 3\n"]}
370
206
coding
Solve the programming task below in a Python markdown code block. Valerian was captured by Shapur. The victory was such a great one that Shapur decided to carve a scene of Valerian's defeat on a mountain. So he had to find the best place to make his victory eternal! He decided to visit all n cities of Persia to find the best available mountain, but after the recent war he was too tired and didn't want to traverse a lot. So he wanted to visit each of these n cities at least once with smallest possible traverse. Persian cities are connected with bidirectional roads. You can go from any city to any other one using these roads and there is a unique path between each two cities. All cities are numbered 1 to n. Shapur is currently in the city 1 and he wants to visit all other cities with minimum possible traverse. He can finish his travels in any city. Help Shapur find how much He should travel. Input First line contains a single natural number n (1 ≀ n ≀ 105) β€” the amount of cities. Next n - 1 lines contain 3 integer numbers each xi, yi and wi (1 ≀ xi, yi ≀ n, 0 ≀ wi ≀ 2 Γ— 104). xi and yi are two ends of a road and wi is the length of that road. Output A single integer number, the minimal length of Shapur's travel. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). Examples Input 3 1 2 3 2 3 4 Output 7 Input 3 1 2 3 1 3 3 Output 9
{"inputs": ["1\n", "2\n2 1 5\n", "2\n2 1 89\n", "2\n2 1 17\n", "2\n2 1 140\n", "3\n1 2 0\n2 3 0\n", "3\n1 2 3\n2 3 5\n", "3\n1 2 3\n2 3 3\n"], "outputs": ["0", "5", "89", "17\n", "140\n", "0", "8\n", "6\n"]}
377
134
coding
Solve the programming task below in a Python markdown code block. ### Preface You are currently working together with a local community to build a school teaching children how to code. First plans have been made and the community wants to decide on the best location for the coding school. In order to make this decision data about the location of students and potential locations is collected. ### Problem In order to be able to attract and teach as many students as possible we want to minimize the total traveling distance for potential students. The streets system is organized in a traditional grid system and students can only travel horizontally or vertically (not diagonal). The locations of interested students is given as an array with the first value of each entry presenting the x coordinate and the second value presenting the y coordinate: ```python students = [[3,7],[2,2],[14,1], ...]; ``` Potential locations are passed as an array of objects with an unique id, a x and y coordinate: ```python locations = [{"id": 1, "x": 3, "y": 4}, {"id": 2, "x": 8, "y": 2}, ...]; ``` Your task is now to evaluate which of the school locations would be best to minimize the distance for all potential students to the school. The desired output should consist of a string indicating the ID of the best suitable location and the x and y coordinates in the following form: ``` "The best location is number 1 with the coordinates x = 3 and y = 4" ``` Also feel free to reuse/extend the following starter code: ```python def optimum_location(students, locations): ```
{"functional": "_inputs = [[[[3, 7], [2, 2], [14, 1]], [{'id': 1, 'x': 3, 'y': 4}, {'id': 2, 'x': 8, 'y': 2}]], [[[54, 7], [1, 211], [14, 44], [12, 5], [14, 7]], [{'id': 1, 'x': 44, 'y': 55}, {'id': 2, 'x': 12, 'y': 57}, {'id': 3, 'x': 23, 'y': 66}]], [[[152, 7], [1, 211], [14, 56], [12, 4], [142, 7]], [{'id': 1, 'x': 63, 'y': 55}, {'id': 2, 'x': 55, 'y': 21}, {'id': 3, 'x': 144, 'y': 12}]], [[[152, 742], [466, 2211], [1412, 564], [142, 444], [142, 744]], [{'id': 1, 'x': 1263, 'y': 525}, {'id': 2, 'x': 55, 'y': 21}, {'id': 3, 'x': 537, 'y': 1244}]], [[[7, 66], [71, 7], [0, 94], [16, 93], [33, 49], [49, 81], [17, 2], [95, 71], [32, 14], [31, 41], [92, 72], [12, 79]], [{'y': 38, 'x': 32, 'id': 1}, {'y': 49, 'x': 73, 'id': 2}, {'y': 85, 'x': 50, 'id': 3}, {'y': 2, 'x': 79, 'id': 4}, {'y': 20, 'x': 44, 'id': 5}, {'y': 56, 'x': 17, 'id': 6}, {'y': 43, 'x': 26, 'id': 7}, {'y': 61, 'x': 89, 'id': 8}, {'y': 18, 'x': 15, 'id': 9}, {'y': 34, 'x': 41, 'id': 10}, {'y': 27, 'x': 99, 'id': 11}]]]\n_outputs = [['The best location is number 1 with the coordinates x = 3 and y = 4'], ['The best location is number 2 with the coordinates x = 12 and y = 57'], ['The best location is number 2 with the coordinates x = 55 and y = 21'], ['The best location is number 3 with the coordinates x = 537 and y = 1244'], ['The best location is number 6 with the coordinates x = 17 and y = 56']]\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(optimum_location(*i), o[0])"}
345
954
coding
Solve the programming task below in a Python markdown code block. Emuskald needs a fence around his farm, but he is too lazy to build it himself. So he purchased a fence-building robot. He wants the fence to be a regular polygon. The robot builds the fence along a single path, but it can only make fence corners at a single angle a. Will the robot be able to build the fence Emuskald wants? In other words, is there a regular polygon which angles are equal to a? -----Input----- The first line of input contains an integer t (0 < t < 180) β€” the number of tests. Each of the following t lines contains a single integer a (0 < a < 180) β€” the angle the robot can make corners at measured in degrees. -----Output----- For each test, output on a single line "YES" (without quotes), if the robot can build a fence Emuskald wants, and "NO" (without quotes), if it is impossible. -----Examples----- Input 3 30 60 90 Output NO YES YES -----Note----- In the first test case, it is impossible to build the fence, since there is no regular polygon with angle $30^{\circ}$. In the second test case, the fence is a regular triangle, and in the last test case β€” a square.
{"inputs": ["3\n4\n11\n6\n", "3\n6\n5\n46\n", "3\n6\n11\n6\n", "3\n28\n1\n62\n", "3\n6\n17\n46\n", "3\n9\n24\n21\n", "3\n7\n24\n21\n", "3\n30\n11\n6\n"], "outputs": ["NO\nNO\nNO\n", "NO\nNO\nNO\n", "NO\nNO\nNO\n", "NO\nNO\nNO\n", "NO\nNO\nNO\n", "NO\nNO\nNO\n", "NO\nNO\nNO\n", "NO\nNO\nNO\n"]}
290
163
coding
Solve the programming task below in a Python markdown code block. Chef likes inequalities. Please help him to solve next one. Given four integers a, b, c, d. Find number of solutions x < y, where a ≀ x ≀ b and c ≀ y ≀ d and x, y integers. -----Input----- The first line contains an integer T denoting number of tests. First line of each test case contains four positive integer numbers a, b, c and d. -----Output----- For each test case, output a single number each in separate line denoting number of integer solutions as asked in the problem. -----Constraints----- - 1 ≀ T ≀ 20 - 1 ≀ a, b, c, d ≀ 106 -----Subtasks----- - Subtask #1: (30 points) 1 ≀ a, b, c, d ≀ 103. - Subtask #2: (70 points) Original constraints. -----Example----- Input:1 2 3 3 4 Output:3 Input:1 2 999999 1 1000000 Output:499998500001
{"inputs": ["1\n2 3 3 4", "1\n2 999999 1 1000000"], "outputs": ["3", "499998500001"]}
258
56
coding
Solve the programming task below in a Python markdown code block. Gordon Ramsay shouts. He shouts and swears. There may be something wrong with him. Anyway, you will be given a string of four words. Your job is to turn them in to Gordon language. Rules: Obviously the words should be Caps, Every word should end with '!!!!', Any letter 'a' or 'A' should become '@', Any other vowel should become '*'. Also feel free to reuse/extend the following starter code: ```python def gordon(a): ```
{"functional": "_inputs = [['What feck damn cake'], ['are you stu pid'], ['i am a chef'], ['dont you talk tome'], ['how dare you feck']]\n_outputs = [['WH@T!!!! F*CK!!!! D@MN!!!! C@K*!!!!'], ['@R*!!!! Y**!!!! ST*!!!! P*D!!!!'], ['*!!!! @M!!!! @!!!! CH*F!!!!'], ['D*NT!!!! Y**!!!! T@LK!!!! T*M*!!!!'], ['H*W!!!! D@R*!!!! Y**!!!! F*CK!!!!']]\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(gordon(*i), o[0])"}
115
262
coding
Solve the programming task below in a Python markdown code block. Igor is a post-graduate student of chemistry faculty in Berland State University (BerSU). He needs to conduct a complicated experiment to write his thesis, but laboratory of BerSU doesn't contain all the materials required for this experiment. Fortunately, chemical laws allow material transformations (yes, chemistry in Berland differs from ours). But the rules of transformation are a bit strange. Berland chemists are aware of n materials, numbered in the order they were discovered. Each material can be transformed into some other material (or vice versa). Formally, for each i (2 ≀ i ≀ n) there exist two numbers x_{i} and k_{i} that denote a possible transformation: k_{i} kilograms of material x_{i} can be transformed into 1 kilogram of material i, and 1 kilogram of material i can be transformed into 1 kilogram of material x_{i}. Chemical processing equipment in BerSU allows only such transformation that the amount of resulting material is always an integer number of kilograms. For each i (1 ≀ i ≀ n) Igor knows that the experiment requires a_{i} kilograms of material i, and the laboratory contains b_{i} kilograms of this material. Is it possible to conduct an experiment after transforming some materials (or none)? -----Input----- The first line contains one integer number n (1 ≀ n ≀ 10^5) β€” the number of materials discovered by Berland chemists. The second line contains n integer numbers b_1, b_2... b_{n} (1 ≀ b_{i} ≀ 10^12) β€” supplies of BerSU laboratory. The third line contains n integer numbers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^12) β€” the amounts required for the experiment. Then n - 1 lines follow. j-th of them contains two numbers x_{j} + 1 and k_{j} + 1 that denote transformation of (j + 1)-th material (1 ≀ x_{j} + 1 ≀ j, 1 ≀ k_{j} + 1 ≀ 10^9). -----Output----- Print YES if it is possible to conduct an experiment. Otherwise print NO. -----Examples----- Input 3 1 2 3 3 2 1 1 1 1 1 Output YES Input 3 3 2 1 1 2 3 1 1 1 2 Output NO
{"inputs": ["3\n1 2 3\n3 2 1\n1 1\n1 1\n", "3\n3 2 1\n1 2 3\n1 1\n1 2\n", "3\n3 2 1\n1 2 3\n1 1\n1 4\n", "3\n1 2 3\n3 2 0\n1 1\n1 1\n", "3\n3 2 1\n1 2 3\n1 1\n2 4\n", "3\n3 0 1\n1 2 3\n1 1\n2 4\n", "3\n3 2 1\n1 2 3\n1 1\n1 2\n", "3\n1 2 3\n3 2 1\n1 1\n1 1\n"], "outputs": ["YES\n", "NO\n", "NO\n", "YES\n", "NO\n", "NO\n", "NO\n", "YES\n"]}
541
230
coding
Solve the programming task below in a Python markdown code block. Soroush and Keshi each have a labeled and rooted tree on n vertices. Both of their trees are rooted from vertex 1. Soroush and Keshi used to be at war. After endless decades of fighting, they finally became allies to prepare a Codeforces round. To celebrate this fortunate event, they decided to make a memorial graph on n vertices. They add an edge between vertices u and v in the memorial graph if both of the following conditions hold: * One of u or v is the ancestor of the other in Soroush's tree. * Neither of u or v is the ancestor of the other in Keshi's tree. Here vertex u is considered ancestor of vertex v, if u lies on the path from 1 (the root) to the v. Popping out of nowhere, Mashtali tried to find the maximum clique in the memorial graph for no reason. He failed because the graph was too big. Help Mashtali by finding the size of the maximum clique in the memorial graph. As a reminder, clique is a subset of vertices of the graph, each two of which are connected by an edge. Input The first line contains an integer t (1≀ t≀ 3 β‹… 10^5) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains an integer n (2≀ n≀ 3 β‹… 10^5). The second line of each test case contains n-1 integers a_2, …, a_n (1 ≀ a_i < i), a_i being the parent of the vertex i in Soroush's tree. The third line of each test case contains n-1 integers b_2, …, b_n (1 ≀ b_i < i), b_i being the parent of the vertex i in Keshi's tree. It is guaranteed that the given graphs are trees. It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case print a single integer β€” the size of the maximum clique in the memorial graph. Example Input 4 4 1 2 3 1 2 3 5 1 2 3 4 1 1 1 1 6 1 1 1 1 2 1 2 1 2 2 7 1 1 3 4 4 5 1 2 1 4 2 5 Output 1 4 1 3 Note In the first and third test cases, you can pick any vertex. In the second test case, one of the maximum cliques is \{2, 3, 4, 5\}. In the fourth test case, one of the maximum cliques is \{3, 4, 6\}.
{"inputs": ["4\n4\n1 2 3\n1 2 3\n5\n1 2 3 4\n1 1 1 1\n6\n1 2 1 1 2\n1 2 1 2 2\n7\n1 1 3 4 4 5\n1 2 1 4 2 5\n", "4\n4\n1 2 3\n1 2 3\n5\n1 2 3 4\n1 1 1 1\n6\n1 2 1 1 2\n1 2 1 2 2\n7\n1 1 3 4 4 5\n1 2 1 4 4 5\n", "4\n4\n1 2 3\n1 2 3\n5\n1 4 3 4\n1 1 1 1\n6\n1 1 1 1 2\n1 2 1 2 2\n7\n1 1 3 4 4 5\n1 2 1 4 2 5\n", "4\n4\n1 2 3\n1 2 3\n5\n1 2 3 4\n1 1 2 1\n6\n1 2 1 1 2\n1 2 1 2 2\n7\n1 1 3 4 4 5\n1 2 1 4 4 5\n", "4\n4\n1 2 3\n1 2 3\n5\n1 2 3 4\n1 1 1 2\n6\n1 1 1 1 2\n1 2 1 2 2\n7\n1 1 3 4 4 5\n1 2 1 4 2 5\n", "4\n4\n1 2 3\n1 2 3\n5\n1 2 3 4\n1 1 1 1\n6\n1 1 1 1 2\n1 2 2 2 2\n7\n1 1 3 4 4 5\n1 2 1 2 2 6\n", "4\n4\n1 2 3\n1 2 3\n5\n1 2 2 4\n1 1 2 1\n6\n1 2 1 1 2\n1 2 2 4 2\n7\n1 1 3 4 4 5\n1 2 1 4 4 5\n", "4\n4\n1 2 3\n1 1 3\n5\n1 2 3 4\n1 1 1 1\n6\n1 1 1 1 1\n1 2 2 2 2\n7\n1 1 3 4 1 5\n1 2 1 4 2 5\n"], "outputs": ["1\n4\n1\n3\n", "1\n4\n1\n2\n", "1\n1\n1\n3\n", "1\n3\n1\n2\n", "1\n3\n1\n3\n", "1\n4\n1\n4\n", "1\n2\n1\n2\n", "2\n4\n1\n2\n"]}
631
758
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given two integers tomatoSlices and cheeseSlices. The ingredients of different burgers are as follows: Jumbo Burger: 4 tomato slices and 1 cheese slice. Small Burger: 2 Tomato slices and 1 cheese slice. Return [total_jumbo, total_small] so that the number of remaining tomatoSlices equal to 0 and the number of remaining cheeseSlices equal to 0. If it is not possible to make the remaining tomatoSlices and cheeseSlices equal to 0 return []. Β  Please complete the following python code precisely: ```python class Solution: def numOfBurgers(self, tomatoSlices: int, cheeseSlices: int) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(tomatoSlices = 16, cheeseSlices = 7) == [1,6]\n assert candidate(tomatoSlices = 17, cheeseSlices = 4) == []\n assert candidate(tomatoSlices = 4, cheeseSlices = 17) == []\n assert candidate(tomatoSlices = 0, cheeseSlices = 0) == [0,0]\n assert candidate(tomatoSlices = 2, cheeseSlices = 1) == [0,1]\n\n\ncheck(Solution().numOfBurgers)"}
163
149
coding
Solve the programming task below in a Python markdown code block. You are given a string S of length 3 consisting of a, b and c. Determine if S can be obtained by permuting abc. -----Constraints----- - |S|=3 - S consists of a, b and c. -----Input----- Input is given from Standard Input in the following format: S -----Output----- If S can be obtained by permuting abc, print Yes; otherwise, print No. -----Sample Input----- bac -----Sample Output----- Yes Swapping the first and second characters in bac results in abc.
{"inputs": ["bba", "cab", "abb", "aab", "aac", "bbb", "baa", "caa"], "outputs": ["No\n", "Yes\n", "No\n", "No\n", "No\n", "No\n", "No\n", "No\n"]}
125
64
coding
Solve the programming task below in a Python markdown code block. Pair of gloves ============= Winter is coming, you must prepare your ski holidays. The objective of this kata is to determine the number of pair of gloves you can constitute from the gloves you have in your drawer. A pair of gloves is constituted of two gloves of the same color. You are given an array containing the color of each glove. You must return the number of pair you can constitute. You must not change the input array. Example : ```python my_gloves = ["red","green","red","blue","blue"] number_of_pairs(my_gloves) == 2;// red and blue red_gloves = ["red","red","red","red","red","red"]; number_of_pairs(red_gloves) == 3; // 3 red pairs ``` Also feel free to reuse/extend the following starter code: ```python def number_of_pairs(gloves): ```
{"functional": "_inputs = [[['red', 'red']], [['red', 'green', 'blue']], [['gray', 'black', 'purple', 'purple', 'gray', 'black']], [[]], [['red', 'green', 'blue', 'blue', 'red', 'green', 'red', 'red', 'red']]]\n_outputs = [[1], [0], [3], [0], [4]]\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(number_of_pairs(*i), o[0])"}
200
228
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a 0-indexed array nums consisting of positive integers, representing targets on a number line. You are also given an integer space. You have a machine which can destroy targets. Seeding the machine with some nums[i] allows it to destroy all targets with values that can be represented as nums[i] + c * space, where c is any non-negative integer. You want to destroy the maximum number of targets in nums. Return the minimum value of nums[i] you can seed the machine with to destroy the maximum number of targets. Β  Please complete the following python code precisely: ```python class Solution: def destroyTargets(self, nums: List[int], space: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [3,7,8,1,1,5], space = 2) == 1\n assert candidate(nums = [1,3,5,2,4,6], space = 2) == 1\n assert candidate(nums = [6,2,5], space = 100) == 2\n\n\ncheck(Solution().destroyTargets)"}
163
96
coding
Solve the programming task below in a Python markdown code block. A certain type of steel is graded according to the following conditions. 1. Hardness of the steel must be greater than 50 2. Carbon content of the steel must be less than 0.7 3. Tensile strength must be greater than 5600 The grades awarded are as follows: Grade is 10 if all three conditions are met Grade is 9 if conditions (1) and (2) are met Grade is 8 if conditions (2) and (3) are met Grade is 7 if conditions (1) and (3) are met Grade is 6 if only one condition is met Grade is 5 if none of the three conditions are met Write a program to display the grade of the steel, based on the values of hardness, carbon content and tensile strength of the steel, given by the user. ------ Input Format ------ The first line contains an integer T, total number of testcases. Then follow T lines, each line contains three numbers hardness, carbon content and tensile strength of the steel. ------ Output Format ------ For each test case, print Grade of the steel depending on Conditions, in a new line. ------ Constraints ------ - 1 ≀ T ≀ 1000 - 0 ≀ hardness, carbon content, tensile strength ≀ 10000 ----- Sample Input 1 ------ 3 53 0.6 5602 45 0 4500 0 0 0 ----- Sample Output 1 ------ 10 6 6
{"inputs": ["3 \n67 0.6 5602\n45 0 4500\n0 0 0", "3 \n67 0.6 5602\n78 0 4500\n0 0 0", "3 \n53 0.6 5602\n45 0 4500\n0 0 0", "3 \n103 0.6 5602\n78 0 4500\n0 0 0", "3 \n103 0.6 5602\n78 0 4466\n0 0 0", "3 \n103 0.6 7770\n78 0 4466\n0 0 0", "3 \n53 0.6 5602\n45 0 4500\n0 0 0 \n", "3 \n103 0.6 7770\n78 -1 4466\n0 0 0"], "outputs": ["10\n6\n6\n", "10\n9\n6\n", "10\n6\n6\n", "10\n9\n6\n", "10\n9\n6\n", "10\n9\n6\n", "10\n6\n6\n", "10\n9\n6\n"]}
349
340
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. Eugene loves sequences, especially arithmetic progressions. One day he was asked to solve a difficult problem. If a sequence of numbers A_{1}, A_{2}, ... , A_{N} form an arithmetic progression A, he was asked to calculate sum of F(A_{i}), for L ≀ i ≀ R. F(X) is defined as: If X < 10 then F(X) = X. Else F(X) = F(sum_{of}_digits(X)). Example: F(1378) = F(1+3+7+8) = F(19) = F(1 + 9) = F(10) = F(1+0) = F(1) = 1 ------ Input ------ The first line of the input contains an integer T denoting the number of test cases. Each test case is described in one line containing four integers: A_{1} denoting the first element of the arithmetic progression A, D denoting the common difference between successive members of A, and L and R as described in the problem statement. ------ Output ------ For each test case, output a single line containing one integer denoting sum of F(A_{i}). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ A_{1} ≀ 10^{9}$ $0 ≀ D ≀ 10^{9}$ $1 ≀ R ≀ 10^{18}$ $1 ≀ L ≀ R$ ------ Subtasks ------ $Subtask 1: 0 ≀ D ≀ 100, 1 ≀ A_{1} ≀ 10^{9}, 1 ≀ R ≀ 100 - 15 points$ $Subtask 2: 0 ≀ D ≀ 10^{9}, 1 ≀ A_{1} ≀ 10^{9}, 1 ≀ R ≀ 10^{6} - 25 points$ $Subtask 3: Original constraints - 60 points$ ----- Sample Input 1 ------ 2 1 1 1 3 14 7 2 4 ----- Sample Output 1 ------ 6 12 ----- explanation 1 ------ Example case 1. A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...} A1 = 1 A2 = 2 A3 = 3 F(A1) = 1 F(A2) = 2 F(A3) = 3 1+2+3=6 Example case 2. A = {14, 21, 28, 35, 42, 49, 56, 63, 70, 77, ...} A2 = 21 A3 = 28 A4 = 35 F(A2) = 3 F(A3) = 1 F(A4) = 8 3+1+8=12
{"inputs": ["2\n1 1 0 4\n4 7 2 4", "2\n1 1 0 4\n2 7 2 4", "2\n1 1 1 3\n14 7 2 4", "2\n1 1 1 2\n14 7 2 4", "2\n1 1 1 4\n14 7 2 4", "2\n1 1 0 4\n14 7 2 4", "2\n1 1 1 4\n2 10 2 0", "2\n1 1 1 4\n3 29 2 0"], "outputs": ["19\n18\n", "19\n21\n", "6\n12", "3\n12\n", "10\n12\n", "19\n12\n", "10\n-2\n", "10\n-3\n"]}
703
225
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an integer array nums, return true if nums is consecutive, otherwise return false. An array is consecutive if it contains every number in the range [x, x + n - 1] (inclusive), where x is the minimum number in the array and n is the length of the array. Β  Please complete the following python code precisely: ```python class Solution: def isConsecutive(self, nums: List[int]) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(nums = [1,3,4,2]) == True\n assert candidate(nums = [1,3]) == False\n assert candidate(nums = [3,5,4]) == True\n\n\ncheck(Solution().isConsecutive)"}
109
65
coding
Solve the programming task below in a Python markdown code block. Santa has $n$ candies and he wants to gift them to $k$ kids. He wants to divide as many candies as possible between all $k$ kids. Santa can't divide one candy into parts but he is allowed to not use some candies at all. Suppose the kid who recieves the minimum number of candies has $a$ candies and the kid who recieves the maximum number of candies has $b$ candies. Then Santa will be satisfied, if the both conditions are met at the same time: $b - a \le 1$ (it means $b = a$ or $b = a + 1$); the number of kids who has $a+1$ candies (note that $a+1$ not necessarily equals $b$) does not exceed $\lfloor\frac{k}{2}\rfloor$ (less than or equal to $\lfloor\frac{k}{2}\rfloor$). $\lfloor\frac{k}{2}\rfloor$ is $k$ divided by $2$ and rounded down to the nearest integer. For example, if $k=5$ then $\lfloor\frac{k}{2}\rfloor=\lfloor\frac{5}{2}\rfloor=2$. Your task is to find the maximum number of candies Santa can give to kids so that he will be satisfied. You have to answer $t$ independent test cases. -----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 describe test cases. The $i$-th test case contains two integers $n$ and $k$ ($1 \le n, k \le 10^9$) β€” the number of candies and the number of kids. -----Output----- For each test case print the answer on it β€” the maximum number of candies Santa can give to kids so that he will be satisfied. -----Example----- Input 5 5 2 19 4 12 7 6 2 100000 50010 Output 5 18 10 6 75015 -----Note----- In the first test case, Santa can give $3$ and $2$ candies to kids. There $a=2, b=3,a+1=3$. In the second test case, Santa can give $5, 5, 4$ and $4$ candies. There $a=4,b=5,a+1=5$. The answer cannot be greater because then the number of kids with $5$ candies will be $3$. In the third test case, Santa can distribute candies in the following way: $[1, 2, 2, 1, 1, 2, 1]$. There $a=1,b=2,a+1=2$. He cannot distribute two remaining candies in a way to be satisfied. In the fourth test case, Santa can distribute candies in the following way: $[3, 3]$. There $a=3, b=3, a+1=4$. Santa distributed all $6$ candies.
{"inputs": ["1\n49845 2\n", "1\n96290 2\n", "1\n51548 1\n", "1\n25702 1\n", "1\n25113 1\n", "1\n49777 3\n", "1\n38068 2\n", "1\n49845 1\n"], "outputs": ["49845\n", "96290\n", "51548\n", "25702\n", "25113\n", "49777\n", "38068\n", "49845\n"]}
700
166
coding
Solve the programming task below in a Python markdown code block. You are given array of integers, your task will be to count all pairs in that array and return their count. **Notes:** * Array can be empty or contain only one value; in this case return `0` * If there are more pairs of a certain number, count each pair only once. E.g.: for `[0, 0, 0, 0]` the return value is `2` (= 2 pairs of `0`s) * Random tests: maximum array length is 1000, range of values in array is between 0 and 1000 ## Examples ``` [1, 2, 5, 6, 5, 2] --> 2 ``` ...because there are 2 pairs: `2` and `5` ``` [1, 2, 2, 20, 6, 20, 2, 6, 2] --> 4 ``` ...because there are 4 pairs: `2`, `20`, `6` and `2` (again) Also feel free to reuse/extend the following starter code: ```python def duplicates(arr): ```
{"functional": "_inputs = [[[1, 2, 2, 20, 6, 20, 2, 6, 2]], [[1000, 1000]], [[]], [[54]]]\n_outputs = [[4], [1], [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(duplicates(*i), o[0])"}
271
207
coding
Solve the programming task below in a Python markdown code block. You are given two integer arrays a and b of length n. You can reverse at most one subarray (continuous subsegment) of the array a. Your task is to reverse such a subarray that the sum βˆ‘_{i=1}^n a_i β‹… b_i is maximized. Input The first line contains one integer n (1 ≀ n ≀ 5000). The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^7). The third line contains n integers b_1, b_2, ..., b_n (1 ≀ b_i ≀ 10^7). Output Print single integer β€” maximum possible sum after reversing at most one subarray (continuous subsegment) of a. Examples Input 5 2 3 2 1 3 1 3 2 4 2 Output 29 Input 2 13 37 2 4 Output 174 Input 6 1 8 7 6 3 6 5 9 6 8 8 6 Output 235 Note In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β‹… 1 + 3 β‹… 3 + 2 β‹… 2 + 3 β‹… 4 + 1 β‹… 2 = 29. In the second example, you don't need to use the reverse operation. 13 β‹… 2 + 37 β‹… 4 = 174. In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β‹… 5 + 8 β‹… 9 + 3 β‹… 6 + 6 β‹… 8 + 7 β‹… 8 + 6 β‹… 6 = 235.
{"inputs": ["1\n1\n1\n", "1\n2\n1\n", "1\n2\n0\n", "2\n13 37\n0 4\n", "2\n13 71\n0 4\n", "2\n13 37\n2 4\n", "3\n0 2 5\n5 45 22\n", "3\n0 2 5\n5 69 22\n"], "outputs": ["1\n", "2\n", "0\n", "148\n", "284\n", "\n174\n", "269\n", "389\n"]}
476
151
coding
Solve the programming task below in a Python markdown code block. It is the World Cup Finals. Chef only finds a match interesting if the skill difference of the competing teams is *less than or equal to* D. Given that the skills of the teams competing in the final are X and Y respectively, determine whether Chef will find the game interesting or not. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of testcases. The description of T testcases follows. - Each testcase consists of a single line of input containing three space-separated integers X, Y, and D β€” the skill levels of the teams and the maximum skill difference. ------ Output Format ------ For each testcase, output "YES" if Chef will find the game interesting, else output "NO" (without the quotes). The checker is case-insensitive, so "YeS" and "nO" etc. are also acceptable. ------ Constraints ------ $1 ≀ T ≀ 2000$ $1 ≀ X, Y ≀ 100$ $0 ≀ D ≀ 100$ ----- Sample Input 1 ------ 3 5 3 4 5 3 1 5 5 0 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Test case $1$: The skill difference between the teams is $2$, which is less than the maximum allowed difference of $4$. Test case $2$: The skill difference between the teams is $2$, which is more than the maximum allowed difference of $1$.
{"inputs": ["3\n5 3 4\n5 3 1\n5 5 0\n"], "outputs": ["YES\nNO\nYES\n"]}
335
36
coding
Solve the programming task below in a Python markdown code block. It is nighttime and Joe the Elusive got into the country's main bank's safe. The safe has n cells positioned in a row, each of them contains some amount of diamonds. Let's make the problem more comfortable to work with and mark the cells with positive numbers from 1 to n from the left to the right. Unfortunately, Joe didn't switch the last security system off. On the plus side, he knows the way it works. Every minute the security system calculates the total amount of diamonds for each two adjacent cells (for the cells between whose numbers difference equals 1). As a result of this check we get an n - 1 sums. If at least one of the sums differs from the corresponding sum received during the previous check, then the security system is triggered. Joe can move the diamonds from one cell to another between the security system's checks. He manages to move them no more than m times between two checks. One of the three following operations is regarded as moving a diamond: moving a diamond from any cell to any other one, moving a diamond from any cell to Joe's pocket, moving a diamond from Joe's pocket to any cell. Initially Joe's pocket is empty, and it can carry an unlimited amount of diamonds. It is considered that before all Joe's actions the system performs at least one check. In the morning the bank employees will come, which is why Joe has to leave the bank before that moment. Joe has only k minutes left before morning, and on each of these k minutes he can perform no more than m operations. All that remains in Joe's pocket, is considered his loot. Calculate the largest amount of diamonds Joe can carry with him. Don't forget that the security system shouldn't be triggered (even after Joe leaves the bank) and Joe should leave before morning. Input The first line contains integers n, m and k (1 ≀ n ≀ 104, 1 ≀ m, k ≀ 109). The next line contains n numbers. The i-th number is equal to the amount of diamonds in the i-th cell β€” it is an integer from 0 to 105. Output Print a single number β€” the maximum number of diamonds Joe can steal. Examples Input 2 3 1 2 3 Output 0 Input 3 2 2 4 1 3 Output 2 Note In the second sample Joe can act like this: The diamonds' initial positions are 4 1 3. During the first period of time Joe moves a diamond from the 1-th cell to the 2-th one and a diamond from the 3-th cell to his pocket. By the end of the first period the diamonds' positions are 3 2 2. The check finds no difference and the security system doesn't go off. During the second period Joe moves a diamond from the 3-rd cell to the 2-nd one and puts a diamond from the 1-st cell to his pocket. By the end of the second period the diamonds' positions are 2 3 1. The check finds no difference again and the security system doesn't go off. Now Joe leaves with 2 diamonds in his pocket.
{"inputs": ["1 1 1\n0\n", "2 1 1\n0\n", "3 1 1\n0\n", "2 0 1\n2 3\n", "2 0 1\n0 3\n", "2 3 1\n2 3\n", "3 2 2\n4 1 3\n", "1 6 19\n110000\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "2\n", "114\n"]}
677
136
coding
Solve the programming task below in a Python markdown code block. Create a program to determine the positional relationship between a triangle and a circle on a plane. All target figures shall include boundaries. The triangle is given the position of three vertices, and the circle is given the position of the center and the radius. The position is given by a set of two integers in a Cartesian coordinate system. The radius is also given as an integer. Input A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format: x1 y1 x2 y2 x3 y3 xc yc r The first to third lines are given the coordinates xi, yi of the i-th vertex of the triangle. The coordinates of the center of the circle xc, yc are given on the 4th line, and the radius r of the circle is given on the 5th line. All inputs given are integers greater than or equal to 1 and less than or equal to 10,000. The number of datasets does not exceed 100. Output The judgment result is output to one line in the following format for each input data set. If the circle is included in the triangle a If the triangle is included in the circle b In other cases, if there is an intersection, c D if there is no intersection Example Input 1 1 3 1 3 3 3 2 3 3 12 9 3 11 12 8 7 5 15 3 17 7 22 5 7 6 4 6 11 8 2 16 9 10 8 2 0 0 Output b c d a
{"inputs": ["4 0\n3 1\n3 3\n3 3\n1\n4 3\n8 2\n4 12\n8 9\n5\n0 0\n52 2\n8 5\n2 2\n5\n3 7\n23 2\n4 9\n37 1\n0\n0 0", "2 0\n3 1\n3 3\n3 3\n1\n4 3\n16 2\n4 12\n8 9\n5\n5 0\n10 2\n8 5\n7 2\n5\n3 7\n23 2\n16 9\n37 1\n0\n0 0", "2 0\n3 1\n3 3\n3 3\n1\n4 3\n16 2\n4 12\n8 9\n5\n5 0\n10 2\n8 5\n7 2\n0\n3 7\n23 2\n16 9\n37 1\n0\n0 0", "2 1\n3 1\n3 3\n3 2\n3\n3 12\n9 3\n11 12\n8 7\n5\n5 3\n17 7\n22 5\n7 6\n5\n6 11\n8 2\n16 9\n15 8\n0\n0 0", "2 1\n3 1\n3 3\n3 4\n3\n3 12\n9 3\n11 12\n8 7\n5\n5 3\n17 7\n22 5\n7 6\n5\n6 11\n8 2\n16 9\n15 8\n0\n0 0", "2 1\n3 1\n3 3\n3 4\n3\n3 12\n9 3\n11 12\n8 7\n5\n5 3\n17 7\n22 5\n7 6\n5\n6 11\n8 2\n16 9\n10 8\n0\n0 0", "1 1\n3 1\n3 3\n3 2\n3\n3 8\n9 3\n11 12\n8 7\n9\n15 3\n17 7\n22 5\n5 6\n5\n6 11\n8 2\n16 9\n13 8\n2\n0 0", "2 1\n3 1\n2 3\n3 2\n4\n3 12\n9 3\n11 12\n3 6\n3\n5 3\n17 7\n22 5\n7 6\n7\n6 11\n8 2\n16 9\n19 8\n0\n0 0"], "outputs": ["c\nc\n", "c\nc\nb\nd\n", "c\nc\na\nd\n", "b\nc\nc\nd\n", "c\nc\nc\nd\n", "c\nc\nc\na\n", "b\nb\nd\nc\n", "b\nd\nc\nd\n"]}
393
754
coding
Solve the programming task below in a Python markdown code block. All cities of Lineland are located on the Ox coordinate axis. Thus, each city is associated with its position x_{i} β€” a coordinate on the Ox axis. No two cities are located at a single point. Lineland residents love to send letters to each other. A person may send a letter only if the recipient lives in another city (because if they live in the same city, then it is easier to drop in). Strange but true, the cost of sending the letter is exactly equal to the distance between the sender's city and the recipient's city. For each city calculate two values ​​min_{i} and max_{i}, where min_{i} is the minimum cost of sending a letter from the i-th city to some other city, and max_{i} is the the maximum cost of sending a letter from the i-th city to some other city -----Input----- The first line of the input contains integer n (2 ≀ n ≀ 10^5) β€” the number of cities in Lineland. The second line contains the sequence of n distinct integers x_1, x_2, ..., x_{n} ( - 10^9 ≀ x_{i} ≀ 10^9), where x_{i} is the x-coordinate of the i-th city. All the x_{i}'s are distinct and follow in ascending order. -----Output----- Print n lines, the i-th line must contain two integers min_{i}, max_{i}, separated by a space, where min_{i} is the minimum cost of sending a letter from the i-th city, and max_{i} is the maximum cost of sending a letter from the i-th city. -----Examples----- Input 4 -5 -2 2 7 Output 3 12 3 9 4 7 5 12 Input 2 -1 1 Output 2 2 2 2
{"inputs": ["2\n0 0\n", "2\n0 1\n", "2\n0 2\n", "2\n0 3\n", "2\n-1 1\n", "2\n-2 1\n", "2\n-4 1\n", "2\n-6 1\n"], "outputs": ["0 0\n0 0\n", "1 1\n1 1\n", "2 2\n2 2\n", "3 3\n3 3\n", "2 2\n2 2\n", "3 3\n3 3\n", "5 5\n5 5\n", "7 7\n7 7\n"]}
414
154
coding
Solve the programming task below in a Python markdown code block. Christmas is coming, and Santa has a long list to go through, to find who deserves presents for the big day. Go through a list of children, and return a list containing every child who appeared on Santa's list. Do not add any child more than once. Output should be sorted. ~~~if:java For java, use Lists. ~~~ Comparison should be case sensitive and the returned list should contain only one copy of each name: `"Sam"` and `"sam"` are different, but `"sAm"` and `"sAm"` are not. Also feel free to reuse/extend the following starter code: ```python def find_children(santas_list, children): ```
{"functional": "_inputs = [[['Jason', 'Jackson', 'Jordan', 'Johnny'], ['Jason', 'Jordan', 'Jennifer']], [['Jason', 'Jackson', 'Johnson', 'JJ'], ['Jason', 'James', 'JJ']], [['jASon', 'JAsoN', 'JaSON', 'jasON'], ['JasoN', 'jASOn', 'JAsoN', 'jASon', 'JASON']]]\n_outputs = [[['Jason', 'Jordan']], [['JJ', 'Jason']], [['JAsoN', 'jASon']]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(find_children(*i), o[0])"}
152
258
coding
Solve the programming task below in a Python markdown code block. You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course. You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $p$ units and your followerΒ β€” at most $f$ units. In the blacksmith shop, you found $cnt_s$ swords and $cnt_w$ war axes. Each sword weights $s$ units and each war axeΒ β€” $w$ units. You don't care what to take, since each of them will melt into one steel ingot. What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$)Β β€” the number of test cases. The first line of each test case contains two integers $p$ and $f$ ($1 \le p, f \le 10^9$)Β β€” yours and your follower's capacities. The second line of each test case contains two integers $cnt_s$ and $cnt_w$ ($1 \le cnt_s, cnt_w \le 2 \cdot 10^5$)Β β€” the number of swords and war axes in the shop. The third line of each test case contains two integers $s$ and $w$ ($1 \le s, w \le 10^9$)Β β€” the weights of each sword and each war axe. It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry. -----Example----- Input 3 33 27 6 10 5 6 100 200 10 10 5 5 1 19 1 3 19 5 Output 11 20 3 -----Note----- In the first test case: you should take $3$ swords and $3$ war axes: $3 \cdot 5 + 3 \cdot 6 = 33 \le 33$ and your followerΒ β€” $3$ swords and $2$ war axes: $3 \cdot 5 + 2 \cdot 6 = 27 \le 27$. $3 + 3 + 3 + 2 = 11$ weapons in total. In the second test case, you can take all available weapons even without your follower's help, since $5 \cdot 10 + 5 \cdot 10 \le 100$. In the third test case, you can't take anything, but your follower can take $3$ war axes: $3 \cdot 5 \le 19$.
{"inputs": ["1\n8 7\n2 4\n7 3\n", "1\n8 7\n2 4\n7 3\n", "1\n8 7\n2 4\n2 3\n", "1\n8 7\n3 4\n2 3\n", "1\n15 10\n200000 4\n15 1000000000\n", "1\n15 10\n200000 4\n15 1000000000\n", "1\n13 10\n200000 4\n15 1000000000\n", "1\n13 10\n200000 4\n15 1000001000\n"], "outputs": ["4\n", "4\n", "5\n", "6\n", "1\n", "1\n", "0\n", "0\n"]}
668
234
coding
Solve the programming task below in a Python markdown code block. Toad Rash has a binary string $s$. A binary string consists only of zeros and ones. Let $n$ be the length of $s$. Rash needs to find the number of such pairs of integers $l$, $r$ that $1 \leq l \leq r \leq n$ and there is at least one pair of integers $x$, $k$ such that $1 \leq x, k \leq n$, $l \leq x < x + 2k \leq r$, and $s_x = s_{x+k} = s_{x+2k}$. Find this number of pairs for Rash. -----Input----- The first line contains the string $s$ ($1 \leq |s| \leq 300\,000$), consisting of zeros and ones. -----Output----- Output one integer: the number of such pairs of integers $l$, $r$ that $1 \leq l \leq r \leq n$ and there is at least one pair of integers $x$, $k$ such that $1 \leq x, k \leq n$, $l \leq x < x + 2k \leq r$, and $s_x = s_{x+k} = s_{x+2k}$. -----Examples----- Input 010101 Output 3 Input 11001100 Output 0 -----Note----- In the first example, there are three $l$, $r$ pairs we need to count: $1$, $6$; $2$, $6$; and $1$, $5$. In the second example, there are no values $x$, $k$ for the initial string, so the answer is $0$.
{"inputs": ["0\n", "0\n", "1\n", "00\n", "01\n", "00\n", "01\n", "000\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "1\n"]}
398
76
coding
Solve the programming task below in a Python markdown code block. In the beginning, Takahashi has A cookies, and Aoki has B cookies. They will perform the following operation alternately, starting from Takahashi: * If the number of cookies in his hand is odd, eat one of those cookies; if the number is even, do nothing. Then, give one-half of the cookies in his hand to the other person. Find the numbers of cookies Takahashi and Aoki respectively have after performing K operations in total. Constraints * 1 \leq A,B \leq 10^9 * 1 \leq K \leq 100 * A,B and K are integers. Input Input is given from Standard Input in the following format: A B K Output Print the number of cookies Takahashi has, and the number of cookies Aoki has, in this order, after performing K operations in total. Examples Input 5 4 2 Output 5 3 Input 3 3 3 Output 1 3 Input 314159265 358979323 84 Output 448759046 224379523
{"inputs": ["3 2 3", "5 4 1", "0 2 3", "8 4 1", "0 0 3", "8 4 2", "3 3 3", "5 4 2"], "outputs": ["1 2\n", "2 6\n", "0 1\n", "4 8\n", "0 0\n", "8 4\n", "1 3", "5 3"]}
276
108
coding
Solve the programming task below in a Python markdown code block. What's in a name? ..Or rather, what's a name in? For us, a particular string is where we are looking for a name. Task Test whether or not the string contains all of the letters which spell a given name, in order. The format A function passing two strings, searching for one (the name) within the other. ``function nameInStr(str, name){ return true || false }`` Examples nameInStr("Across the rivers", "chris") --> true ^ ^ ^^ ^ c h ri s Contains all of the letters in "chris", in order. ---------------------------------------------------------- nameInStr("Next to a lake", "chris") --> false Contains none of the letters in "chris". -------------------------------------------------------------------- nameInStr("Under a sea", "chris") --> false ^ ^ r s Contains only some of the letters in "chris". -------------------------------------------------------------------- nameInStr("A crew that boards the ship", "chris") --> false cr h s i cr h s i c h r s i ... Contains all of the letters in "chris", but not in order. -------------------------------------------------------------------- nameInStr("A live son", "Allison") --> false ^ ^^ ^^^ A li son Contains all of the correct letters in "Allison", in order, but not enough of all of them (missing an 'l'). Note: testing will _not_ be case-sensitive. Also feel free to reuse/extend the following starter code: ```python def name_in_str(str, name): ```
{"functional": "_inputs = [['Across the rivers', 'chris'], ['Next to a lake', 'chris'], ['Under a sea', 'chris'], ['A crew that boards the ship', 'chris'], ['A live son', 'Allison'], ['Just enough nice friends', 'Jennifer'], ['thomas', 'Thomas'], ['pippippi', 'Pippi'], ['pipipp', 'Pippi'], ['ppipip', 'Pippi']]\n_outputs = [[True], [False], [False], [False], [False], [False], [True], [True], [False], [False]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(name_in_str(*i), o[0])"}
385
270
coding
Solve the programming task below in a Python markdown code block. Ostap already settled down in Rio de Janiero suburb and started to grow a tree in his garden. Recall that a tree is a connected undirected acyclic graph. Ostap's tree now has n vertices. He wants to paint some vertices of the tree black such that from any vertex u there is at least one black vertex v at distance no more than k. Distance between two vertices of the tree is the minimum possible number of edges of the path between them. As this number of ways to paint the tree can be large, Ostap wants you to compute it modulo 109 + 7. Two ways to paint the tree are considered different if there exists a vertex that is painted black in one way and is not painted in the other one. Input The first line of the input contains two integers n and k (1 ≀ n ≀ 100, 0 ≀ k ≀ min(20, n - 1)) β€” the number of vertices in Ostap's tree and the maximum allowed distance to the nearest black vertex. Don't miss the unusual constraint for k. Each of the next n - 1 lines contain two integers ui and vi (1 ≀ ui, vi ≀ n) β€” indices of vertices, connected by the i-th edge. It's guaranteed that given graph is a tree. Output Print one integer β€” the remainder of division of the number of ways to paint the tree by 1 000 000 007 (109 + 7). Examples Input 2 0 1 2 Output 1 Input 2 1 1 2 Output 3 Input 4 1 1 2 2 3 3 4 Output 9 Input 7 2 1 2 2 3 1 4 4 5 1 6 6 7 Output 91 Note In the first sample, Ostap has to paint both vertices black. In the second sample, it is enough to paint only one of two vertices, thus the answer is 3: Ostap can paint only vertex 1, only vertex 2, vertices 1 and 2 both. In the third sample, the valid ways to paint vertices are: {1, 3}, {1, 4}, {2, 3}, {2, 4}, {1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}, {1, 2, 3, 4}.
{"inputs": ["1 0\n", "2 2\n1 2\n", "2 1\n1 2\n", "2 0\n1 2\n", "4 1\n1 2\n2 3\n3 4\n", "5 1\n1 2\n4 3\n5 3\n2 3\n", "7 2\n1 2\n2 3\n1 4\n4 5\n1 6\n6 7\n", "10 5\n6 4\n4 1\n7 5\n8 7\n2 6\n3 6\n8 6\n10 3\n4 9\n"], "outputs": ["1", "3\n", "3", "1", "9", "15", "91", "1023"]}
559
186
coding
Solve the programming task below in a Python markdown code block. Haiku is a short form of Japanese poetry. A Haiku consists of three phrases with 5, 7 and 5 syllables, in this order. Iroha is looking for X,Y,Z-Haiku (defined below) in integer sequences. Consider all integer sequences of length N whose elements are between 1 and 10, inclusive. Out of those 10^N sequences, how many contain an X,Y,Z-Haiku? Here, an integer sequence a_0, a_1, ..., a_{N-1} is said to contain an X,Y,Z-Haiku if and only if there exist four indices x, y, z, w (0 ≦ x < y < z < w ≦ N) such that all of the following are satisfied: - a_x + a_{x+1} + ... + a_{y-1} = X - a_y + a_{y+1} + ... + a_{z-1} = Y - a_z + a_{z+1} + ... + a_{w-1} = Z Since the answer can be extremely large, print the number modulo 10^9+7. -----Constraints----- - 3 ≦ N ≦ 40 - 1 ≦ X ≦ 5 - 1 ≦ Y ≦ 7 - 1 ≦ Z ≦ 5 -----Input----- The input is given from Standard Input in the following format: N X Y Z -----Output----- Print the number of the sequences that contain an X,Y,Z-Haiku, modulo 10^9+7. -----Sample Input----- 3 5 7 5 -----Sample Output----- 1 Here, the only sequence that contains a 5,7,5-Haiku is [5, 7, 5].
{"inputs": ["5 4 3 3", "5 4 2 3", "0 4 2 3", "8 5 7 5", "3 3 7 5", "7 4 2 3", "5 8 3 3", "8 4 2 3"], "outputs": ["460\n", "435\n", "0\n", "1761124\n", "1\n", "78735\n", "574\n", "962894\n"]}
404
131
coding
Solve the programming task below in a Python markdown code block. Taro is an elementary school student who has just learned multiplication. Somehow, he likes multiplication, so when he sees numbers, he wants to multiply. He seems to like to do the following for integers greater than or equal to 0. (Processing flow) * Procedure 1. If a certain integer n greater than or equal to 0 is a single digit in decimal notation, the process ends there. Otherwise go to step 2 * Step 2. When an integer n of 10 or more is displayed in decimal, it is possible to break it into two numbers by inserting a break between some digits (for example, 2012-> 20, 12). For possible cutting methods like this, multiply the two obtained numbers and set the largest one as the next n, and return to step 1. (For details, see "Supplementary information on step 2" below.) Taro seems to like this process, but I can't predict how many times step 2 should be repeated, and I think it may have to be done infinitely. So I asked Taro's older brother and college student how many times this step 2 should be done for an integer n greater than or equal to 0. Your job is to give Q integers greater than or equal to 0 N1 .. NQ, so find out how many steps 2 will be performed on each integer before the end of the process. If you need an infinite number of steps, output -1. Supplement on step 2 You should also take into account those that have a 0 at the beginning of the digit as a result of the isolation. For example, when n = 1024, 1 * 024, 10 * 24, and 102 * 4 are calculated as 24,240,408, respectively, so 408 is selected and this is the next n. Constraints > 1 ≀ Q ≀ 100 > 0 ≀ Ni ≀ 106 > Input > Q > N1 > N2 > ... > NQ > * Q represents the number of integers greater than or equal to 0 given * Ni is an integer greater than or equal to 0 that Taro is interested in, and represents the i-th one. Output Output Q integers separated by line breaks > R1 > R2 > .. > RQ > * Ri represents the number of times step 2 is executed before the processing is completed for Ni. * Ri is -1 if step 2 needs to be performed infinitely for Ni Examples Input 3 9 99 123 Output 0 2 3 Input 2 999999 1000000 Output 12 1
{"inputs": ["3\n2\n2\n4", "3\n0\n42\n4", "3\n0\n37\n4", "3\n16\n2\n7", "3\n29\n2\n7", "3\n9\n99\n39", "3\n3\n127\n5", "3\n1\n169\n6"], "outputs": ["0\n0\n0\n", "0\n1\n0\n", "0\n2\n0\n", "1\n0\n0\n", "2\n0\n0\n", "0\n2\n3\n", "0\n3\n0\n", "0\n4\n0\n"]}
618
152
coding
Solve the programming task below in a Python markdown code block. Sheldon, Leonard, Penny, Rajesh and Howard are in the queue for a "Double Cola" drink vending machine; there are no other people in the queue. The first one in the queue (Sheldon) buys a can, drinks it and doubles! The resulting two Sheldons go to the end of the queue. Then the next in the queue (Leonard) buys a can, drinks it and gets to the end of the queue as two Leonards, and so on. This process continues ad infinitum. For example, Penny drinks the third can of cola and the queue will look like this: Rajesh, Howard, Sheldon, Sheldon, Leonard, Leonard, Penny, Penny. Write a program that will print the name of a man who will drink the n-th can. Note that in the very beginning the queue looks like that: Sheldon, Leonard, Penny, Rajesh, Howard. The first person is Sheldon. Input The input data consist of a single integer n (1 ≀ n ≀ 109). It is guaranteed that the pretests check the spelling of all the five names, that is, that they contain all the five possible answers. Output Print the single line β€” the name of the person who drinks the n-th can of cola. The cans are numbered starting from 1. Please note that you should spell the names like this: "Sheldon", "Leonard", "Penny", "Rajesh", "Howard" (without the quotes). In that order precisely the friends are in the queue initially. SAMPLE INPUT 500000000 SAMPLE OUTPUT Howard
{"inputs": ["841", "38614"], "outputs": ["Leonard", "Howard"]}
355
25
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. A pair of indices i, j where 0 <=Β i < j < nums.length is called beautiful if the first digit of nums[i] and the last digit of nums[j] are coprime. Return the total number of beautiful pairs in nums. Two integers x and y are coprime if there is no integer greater than 1 that divides both of them. In other words, x and y are coprime if gcd(x, y) == 1, where gcd(x, y) is the greatest common divisor of x and y. Β  Please complete the following python code precisely: ```python class Solution: def countBeautifulPairs(self, nums: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [2,5,1,4]) == 5\n assert candidate(nums = [11,21,12]) == 2\n\n\ncheck(Solution().countBeautifulPairs)"}
174
57
coding
Solve the programming task below in a Python markdown code block. In this Kata, you will be given a mathematical string and your task will be to remove all braces as follows: ```Haskell solve("x-(y+z)") = "x-y-z" solve("x-(y-z)") = "x-y+z" solve("u-(v-w-(x+y))-z") = "u-v+w+x+y-z" solve("x-(-y-z)") = "x+y+z" ``` There are no spaces in the expression. Only two operators are given: `"+" or "-"`. More examples in test cases. Good luck! Also feel free to reuse/extend the following starter code: ```python def solve(s): ```
{"functional": "_inputs = [['a-(b)'], ['a-(-b)'], ['a+(b)'], ['a+(-b)'], ['(((((((((-((-(((n))))))))))))))'], ['(((a-((((-(-(f)))))))))'], ['((((-(-(-(-(m-g))))))))'], ['(((((((m-(-(((((t)))))))))))))'], ['-x'], ['-(-(x))'], ['-((-x))'], ['-(-(-x))'], ['-(-(x-y))'], ['-(x-y)'], ['x-(y+z)'], ['x-(y-z)'], ['x-(-y-z)'], ['x-(-((-((((-((-(-(-y)))))))))))'], ['u-(v-w+(x+y))-z'], ['x-(s-(y-z))-(a+b)'], ['u+(g+v)+(r+t)'], ['q+(s-(x-o))-(t-(w-a))'], ['u-(v-w-(x+y))-z'], ['v-(l+s)-(t+y)-(c+f)+(b-(n-p))']]\n_outputs = [['a-b'], ['a+b'], ['a+b'], ['a-b'], ['n'], ['a-f'], ['m-g'], ['m+t'], ['-x'], ['x'], ['x'], ['-x'], ['x-y'], ['-x+y'], ['x-y-z'], ['x-y+z'], ['x+y+z'], ['x-y'], ['u-v+w-x-y-z'], ['x-s+y-z-a-b'], ['u+g+v+r+t'], ['q+s-x+o-t+w-a'], ['u-v+w+x+y-z'], ['v-l-s-t-y-c-f+b-n+p']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(solve(*i), o[0])"}
153
493
coding
Solve the programming task below in a Python markdown code block. A newly opened multinational brand has decided to base their company logo on the three most common characters in the company name. They are now trying out various combinations of company names and logos based on this condition. Given a string $\boldsymbol{\mathrm{~S~}}$, which is the company name in lowercase letters, your task is to find the top three most common characters in the string. Print the three most common characters along with their occurrence count. Sort in descending order of occurrence count. If the occurrence count is the same, sort the characters in alphabetical order. For example, according to the conditions described above, $\textbf{G00GLE}$ would have it's logo with the letters $\textbf{G},\textbf{C},\textbf{E}$. Input Format A single line of input containing the string $\mbox{S}$. Constraints $3<len(S)\leq10^4$ $\mbox{S}$ has at least $3$ distinct characters Output Format Print the three most common characters along with their occurrence count each on a separate line. Sort output in descending order of occurrence count. If the occurrence count is the same, sort the characters in alphabetical order. Sample Input 0 aabbbccde Sample Output 0 b 3 a 2 c 2 Explanation 0 $\textsf{aabbccde}$ Here, b occurs $3$ times. It is printed first. Both a and c occur $2$ times. So, a is printed in the second line and c in the third line because a comes before c in the alphabet. Note: The string $\mbox{S}$ has at least $3$ distinct characters.
{"inputs": ["aabbbccde\n"], "outputs": ["b 3\na 2\nc 2\n"]}
380
27
coding
Solve the programming task below in a Python markdown code block. Raj is suffering from shot term memory loss so he is unable to remember his laptop password but he has a list of some string and the only thing that he remember about his password is alphanumeric and also that all the characters are unique. Given a list of strings, your task is to find a valid password. -----Input----- Each String contains lower case alphabets and 0-9. -----Output----- print "Invalid"(without quotes) if password is not valid else print "Valid"(without quotes) and stop processing input after it. -----Constraints----- 1<=length of string <=100 -----Example----- Input: absdbads asdjenfef tyerbet abc564 Output: Invalid Invalid Invalid Valid
{"inputs": ["absdbads\nasdjenfef\ntyerbet\nabc564"], "outputs": ["Invalid\nInvalid\nInvalid\nValid"]}
168
33