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
Please solve the programming task below using a self-contained code snippet in a markdown code block. The imbalance number of a 0-indexed integer array arr of length n is defined as the number of indices in sarr = sorted(arr) such that: 0 <= i < n - 1, and sarr[i+1] - sarr[i] > 1 Here, sorted(arr) is the function that returns the sorted version of arr. Given a 0-indexed integer array nums, return the sum of imbalance numbers of all its subarrays. A subarray is a contiguous non-empty sequence of elements within an array. Β  Please complete the following python code precisely: ```python class Solution: def sumImbalanceNumbers(self, nums: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [2,3,1,4]) == 3\n assert candidate(nums = [1,3,3,3,5]) == 8\n\n\ncheck(Solution().sumImbalanceNumbers)"}
161
59
coding
Solve the programming task below in a Python markdown code block. # Let's watch a parade! ## Brief You're going to watch a parade, but you only care about one of the groups marching. The parade passes through the street where your house is. Your house is at number `location` of the street. Write a function `parade_time` that will tell you the times when you need to appear to see all appearences of that group. ## Specifications You'll be given: * A `list` of `string`s containing `groups` in the parade, in order of appearance. A group may appear multiple times. You want to see all the parts of your favorite group. * An positive `integer` with the `location` on the parade route where you'll be watching. * An positive `integer` with the `speed` of the parade * A `string` with the `pref`ferred group you'd like to see You need to return the time(s) you need to be at the parade to see your favorite group as a `list` of `integer`s. It's possible the group won't be at your `location` at an exact `time`. In that case, just be sure to get there right before it passes (i.e. the largest integer `time` before the float passes the `position`). ## Example ```python groups = ['A','B','C','D','E','F'] location = 3 speed = 2 pref = 'C' v Locations: |0|1|2|3|4|5|6|7|8|9|... F E D C B A | | time = 0 > > F E D C B A | | time = 1 > > F E D C B|A| time = 2 > > F E D|C|B A time = 3 ^ parade_time(['A','B','C','D','E','F'], 3, 2,'C']) == [3] ``` Also feel free to reuse/extend the following starter code: ```python def parade_time(groups, location, speed, pref): ```
{"functional": "_inputs = [[['a', 'b', 'c', 'd', 'e', 'f'], 3, 2, 'c'], [['c', 'b', 'c', 'd', 'e', 'f'], 3, 2, 'c'], [['a', 'b', 'c', 'd', 'e', 'f'], 3, 2, 'g'], [['a', 'b', 'c', 'c', 'e', 'c'], 7, 1, 'c'], [['a', 'b', 'c', 'd', 'e', 'f'], 1, 2, 'b'], [['a', 'b', 'c', 'd', 'e', 'b'], 1, 2, 'b']]\n_outputs = [[[3]], [[2, 3]], [[]], [[10, 11, 13]], [[1]], [[1, 3]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(parade_time(*i), o[0])"}
467
344
coding
Solve the programming task below in a Python markdown code block. You are given names of two days of the week. Please, determine whether it is possible that during some non-leap year the first day of some month was equal to the first day of the week you are given, while the first day of the next month was equal to the second day of the week you are given. Both months should belong to one year. In this problem, we consider the Gregorian calendar to be used. The number of months in this calendar is equal to 12. The number of days in months during any non-leap year is: 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31. Names of the days of the week are given with lowercase English letters: "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday". -----Input----- The input consists of two lines, each of them containing the name of exactly one day of the week. It's guaranteed that each string in the input is from the set "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday". -----Output----- Print "YES" (without quotes) if such situation is possible during some non-leap year. Otherwise, print "NO" (without quotes). -----Examples----- Input monday tuesday Output NO Input sunday sunday Output YES Input saturday tuesday Output YES -----Note----- In the second sample, one can consider February 1 and March 1 of year 2015. Both these days were Sundays. In the third sample, one can consider July 1 and August 1 of year 2017. First of these two days is Saturday, while the second one is Tuesday.
{"inputs": ["sunday\nsunday\n", "monday\nmonday\n", "monday\nfriday\n", "monday\nsunday\n", "friday\nmonday\n", "friday\nsunday\n", "sunday\nmonday\n", "sunday\nfriday\n"], "outputs": ["YES\n", "YES\n", "NO\n", "NO\n", "YES\n", "YES\n", "NO\n", "NO\n"]}
427
102
coding
Solve the programming task below in a Python markdown code block. You have $2n$ integers $1, 2, \dots, 2n$. You have to redistribute these $2n$ elements into $n$ pairs. After that, you choose $x$ pairs and take minimum elements from them, and from the other $n - x$ pairs, you take maximum elements. Your goal is to obtain the set of numbers $\{b_1, b_2, \dots, b_n\}$ as the result of taking elements from the pairs. What is the number of different $x$-s ($0 \le x \le n$) such that it's possible to obtain the set $b$ if for each $x$ you can choose how to distribute numbers into pairs and from which $x$ pairs choose minimum elements? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the integer $n$ ($1 \le n \le 2 \cdot 10^5$). The second line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_1 < b_2 < \dots < b_n \le 2n$) β€” the set you'd like to get. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one number β€” the number of different $x$-s such that it's possible to obtain the set $b$. -----Examples----- Input 3 1 1 5 1 4 5 9 10 2 3 4 Output 1 3 1 -----Note----- In the first test case, $x = 1$ is the only option: you have one pair $(1, 2)$ and choose the minimum from this pair. In the second test case, there are three possible $x$-s. If $x = 1$, then you can form the following pairs: $(1, 6)$, $(2, 4)$, $(3, 5)$, $(7, 9)$, $(8, 10)$. You can take minimum from $(1, 6)$ (equal to $1$) and the maximum elements from all other pairs to get set $b$. If $x = 2$, you can form pairs $(1, 2)$, $(3, 4)$, $(5, 6)$, $(7, 9)$, $(8, 10)$ and take the minimum elements from $(1, 2)$, $(5, 6)$ and the maximum elements from the other pairs. If $x = 3$, you can form pairs $(1, 3)$, $(4, 6)$, $(5, 7)$, $(2, 9)$, $(8, 10)$ and take the minimum elements from $(1, 3)$, $(4, 6)$, $(5, 7)$. In the third test case, $x = 0$ is the only option: you can form pairs $(1, 3)$, $(2, 4)$ and take the maximum elements from both of them.
{"inputs": ["3\n1\n1\n5\n2 3 5 7 8\n2\n2 4\n", "3\n1\n1\n5\n2 4 5 7 8\n2\n1 2\n", "3\n1\n1\n5\n2 4 5 6 8\n2\n1 2\n", "3\n1\n2\n5\n3 4 5 7 9\n2\n2 4\n", "3\n1\n1\n5\n2 4 5 7 9\n2\n1 4\n", "3\n1\n1\n5\n1 4 5 7 8\n2\n1 2\n", "3\n1\n2\n5\n1 4 5 7 8\n2\n1 2\n", "3\n1\n1\n5\n1 4 5 7 9\n2\n1 4\n"], "outputs": ["1\n3\n2\n", "1\n3\n1\n", "1\n3\n1\n", "1\n3\n2\n", "1\n4\n1\n", "1\n3\n1\n", "1\n3\n1\n", "1\n4\n1\n"]}
729
278
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 of length n. The distinct difference array of nums is an array diff of length n such that diff[i] is equal to the number of distinct elements in the suffix nums[i + 1, ..., n - 1] subtracted from the number of distinct elements in the prefix nums[0, ..., i]. Return the distinct difference array of nums. Note that nums[i, ..., j] denotes the subarray of nums starting at index i and ending at index j inclusive. Particularly, if i > j then nums[i, ..., j] denotes an empty subarray. Β  Please complete the following python code precisely: ```python class Solution: def distinctDifferenceArray(self, nums: List[int]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(nums = [1,2,3,4,5]) == [-3,-1,1,3,5]\n assert candidate(nums = [3,2,3,4,2]) == [-2,-1,0,2,3]\n\n\ncheck(Solution().distinctDifferenceArray)"}
175
78
coding
Solve the programming task below in a Python markdown code block. # Task You are given an array of integers `a` and a non-negative number of operations `k`, applied to the array. Each operation consists of two parts: ``` find the maximum element value of the array; replace each element a[i] with (maximum element value - a[i]).``` How will the array look like after `k` such operations? # Example For `a = [-4, 0, -1, 0]` and `k = 2`, the output should be `[0, 4, 3, 4]`. ``` initial array: [-4, 0, -1, 0] 1st operation: find the maximum value --> 0 replace each element: --> [(0 - -4), (0 - 0), (0 - -1), (0 - 0)] --> [4, 0, 1, 0] 2nd operation: find the maximum value --> 4 replace each element: --> [(4 - 4), (4 - 0), (4 - 1), (4 - 0)] --> [0, 4, 3, 4] ``` For `a = [0, -1, 0, 0, -1, -1, -1, -1, 1, -1]` and `k = 1`, the output should be `[1, 2, 1, 1, 2, 2, 2, 2, 0, 2]`. ``` initial array: [0, -1, 0, 0, -1, -1, -1, -1, 1, -1] 1st operation: find the maximum value --> 1 replace each element: --> [(1-0),(1- -1),(1-0),(1-0),(1- -1),(1- -1),(1- -1),(1- -1),(1-1),(1- -1)] --> [1, 2, 1, 1, 2, 2, 2, 2, 0, 2] ``` # Input/Output - `[input]` integer array a The initial array. Constraints: `1 <= a.length <= 100` `-100 <= a[i] <= 100` - `[input]` integer `k` non-negative number of operations. Constraints: `0 <= k <= 100000` - [output] an integer array The array after `k` operations. Also feel free to reuse/extend the following starter code: ```python def array_operations(a, k): ```
{"functional": "_inputs = [[[-4, 0, -1, 0], 2], [[0, -1, 0, 0, -1, -1, -1, -1, 1, -1], 1], [[-1], 10000]]\n_outputs = [[[0, 4, 3, 4]], [[1, 2, 1, 1, 2, 2, 2, 2, 0, 2]], [[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(array_operations(*i), o[0])"}
604
253
coding
Solve the programming task below in a Python markdown code block. You are given an infinite checkered field. You should get from a square (x1; y1) to a square (x2; y2). Using the shortest path is not necessary. You can move on the field squares in four directions. That is, when you are positioned in any square, you can move to any other side-neighboring one. A square (x; y) is considered bad, if at least one of the two conditions is fulfilled: * |x + y| ≑ 0 (mod 2a), * |x - y| ≑ 0 (mod 2b). Your task is to find the minimum number of bad cells one will have to visit on the way from (x1; y1) to (x2; y2). Input The only line contains integers a, b, x1, y1, x2 and y2 β€” the parameters of the bad squares, the coordinates of the initial and the final squares correspondingly (2 ≀ a, b ≀ 109 and |x1|,|y1|,|x2|,|y2| ≀ 109). It is guaranteed that the initial and the final square aren't bad. Output Print a single number β€” the minimum number of bad cells that one will have to visit in order to travel from square (x1; y1) to square (x2; y2). Examples Input 2 2 1 0 0 1 Output 1 Input 2 2 10 11 0 1 Output 5 Input 2 4 3 -1 3 7 Output 2 Note In the third sample one of the possible paths in (3;-1)->(3;0)->(3;1)->(3;2)->(4;2)->(4;3)->(4;4)->(4;5)->(4;6)->(4;7)->(3;7). Squares (3;1) and (4;4) are bad.
{"inputs": ["2 2 1 0 0 1\n", "5 3 6 3 3 12\n", "5 3 6 3 1 12\n", "2 4 3 -1 3 7\n", "3 4 -8 5 6 -3\n", "4 4 3 2 10 -1\n", "3 5 -4 -7 5 0\n", "1 5 -4 -7 5 0\n"], "outputs": ["1", "2", "2\n", "2", "3", "1", "2", "8\n"]}
453
147
coding
Solve the programming task below in a Python markdown code block. problem JOI, who has been suffering from his winter vacation homework every time, decided to do his homework systematically this time. Homework is a national language and math drill, with a national language drill on page A and a math drill on page B. JOI can advance the Japanese drill up to C pages and the math drill up to D pages a day, but if he does his homework, he can't play that day. There are L days during the winter vacation, and JOI must finish his homework during the winter vacation. Create a program that asks how many days JOI can play during the winter vacation. input The input consists of 5 lines, with one positive integer written on each line. The integer L (2 ≀ L ≀ 40) is written on the first line, which indicates the number of days of winter vacation. The integer A (1 ≀ A ≀ 1000) is written on the second line, which indicates the number of pages of the Japanese drill. The integer B (1 ≀ B ≀ 1000) is written on the third line, which indicates the number of pages of the math drill. The integer C (1 ≀ C ≀ 100) is written on the 4th line, which indicates the maximum number of pages of a national language drill that JOI can advance in a day. The integer D (1 ≀ D ≀ 100) is written on the 5th line, which indicates the maximum number of pages of the math drill that JOI can advance in a day. However, given the input data, JOI is guaranteed to be able to finish his homework during the winter vacation and play for at least a day. Example Input 20 25 30 6 8 Output 15
{"inputs": ["20\n2\n6\n3\n1", "20\n4\n0\n6\n1", "20\n8\n9\n7\n1", "20\n4\n6\n3\n1", "20\n4\n6\n6\n1", "20\n1\n4\n6\n6", "20\n4\n0\n6\n2", "20\n1\n4\n6\n2"], "outputs": ["14\n", "19\n", "11\n", "14\n", "14\n", "19\n", "19\n", "18\n"]}
386
142
coding
Solve the programming task below in a Python markdown code block. You get any card as an argument. Your task is to return a suit of this card. Our deck (is preloaded): ```python DECK = ['2S','3S','4S','5S','6S','7S','8S','9S','10S','JS','QS','KS','AS', '2D','3D','4D','5D','6D','7D','8D','9D','10D','JD','QD','KD','AD', '2H','3H','4H','5H','6H','7H','8H','9H','10H','JH','QH','KH','AH', '2C','3C','4C','5C','6C','7C','8C','9C','10C','JC','QC','KC','AC'] ``` ```python ('3C') -> return 'clubs' ('3D') -> return 'diamonds' ('3H') -> return 'hearts' ('3S') -> return 'spades' ``` Also feel free to reuse/extend the following starter code: ```python def define_suit(card): ```
{"functional": "_inputs = [['3C'], ['QS'], ['9D'], ['JH']]\n_outputs = [['clubs'], ['spades'], ['diamonds'], ['hearts']]\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(define_suit(*i), o[0])"}
267
179
coding
Solve the programming task below in a Python markdown code block. Recently Polycarpus has learned the "bitwise AND" operation (which is also called "AND") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation. For that Polycarpus came to school a little earlier and wrote on the board a sequence of non-negative integers a_1, a_2, ..., a_{n}. He also wrote a square matrix b of size n Γ— n. The element of matrix b that sits in the i-th row in the j-th column (we'll denote it as b_{ij}) equals: the "bitwise AND" of numbers a_{i} and a_{j} (that is, b_{ij} = a_{i}Β &Β a_{j}), if i β‰  j; -1, if i = j. Having written out matrix b, Polycarpus got very happy and wiped a off the blackboard. But the thing is, the teacher will want this sequence to check whether Polycarpus' calculations were correct. Polycarus urgently needs to restore the removed sequence of integers, or else he won't prove that he can count correctly. Help Polycarpus, given matrix b, restore the sequence of numbers a_1, a_2, ..., a_{n}, that he has removed from the board. Polycarpus doesn't like large numbers, so any number in the restored sequence mustn't exceed 10^9. -----Input----- The first line contains a single integer n (1 ≀ n ≀ 100) β€” the size of square matrix b. Next n lines contain matrix b. The i-th of these lines contains n space-separated integers: the j-th number represents the element of matrix b_{ij}. It is guaranteed, that for all i (1 ≀ i ≀ n) the following condition fulfills: b_{ii} = -1. It is guaranteed that for all i, j (1 ≀ i, j ≀ n;Β i β‰  j) the following condition fulfills: 0 ≀ b_{ij} ≀ 10^9, b_{ij} = b_{ji}. -----Output----- Print n non-negative integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 10^9) β€” the sequence that Polycarpus wiped off the board. Separate the numbers by whitespaces. It is guaranteed that there is sequence a that satisfies the problem conditions. If there are multiple such sequences, you are allowed to print any of them. -----Examples----- Input 1 -1 Output 0 Input 3 -1 18 0 18 -1 0 0 0 -1 Output 18 18 0 Input 4 -1 128 128 128 128 -1 148 160 128 148 -1 128 128 160 128 -1 Output 128 180 148 160 -----Note----- If you do not know what is the "bitwise AND" operation please read: http://en.wikipedia.org/wiki/Bitwise_operation.
{"inputs": ["1\n0\n", "1\n-1\n", "1\n-1\n", "2\n0 0\n0 0\n", "2\n0 1\n1 0\n", "2\n0 1\n1 1\n", "2\n1 1\n1 0\n", "2\n-1 0\n0 0\n"], "outputs": ["0\n", "0 ", "0 ", "0 0 ", "1 1 ", "1 1 ", "1 1 ", "0 0 "]}
708
123
coding
Solve the programming task below in a Python markdown code block. You are given an n Γ— m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the table abcd edfg hijk we obtain the table: acd efg hjk A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good. Input The first line contains two integers β€” n and m (1 ≀ n, m ≀ 100). Next n lines contain m small English letters each β€” the characters of the table. Output Print a single number β€” the minimum number of columns that you need to remove in order to make the table good. Examples Input 1 10 codeforces Output 0 Input 4 4 case care test code Output 2 Input 5 4 code forc esco defo rces Output 4 Note In the first sample the table is already good. In the second sample you may remove the first and third column. In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition). Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t.
{"inputs": ["2 1\nb\na\n", "2 1\nb\n`\n", "2 2\nfb\nye\n", "2 2\nfb\ney\n", "2 2\nfb\nez\n", "2 2\nfb\nze\n", "2 2\nfc\nez\n", "2 3\naxc\nbxa\n"], "outputs": ["1\n", "1\n", "0\n", "1\n", "1\n", "0\n", "1\n", "0\n"]}
379
120
coding
Solve the programming task below in a Python markdown code block. Chef is playing a video game, and is now fighting the final boss. The boss has H health points. Each attack of Chef reduces the health of the boss by X. Chef also has a special attack that can be used at most once, and will decrease the health of the boss by Y. Chef wins when the health of the boss is ≀ 0. What is the minimum number of attacks needed by Chef to win? ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The first and only line of each test case will contain three space-separated integers H, X, Y β€” the parameters described in the statement. ------ Output Format ------ For each test case, output on a new line the minimum number of attacks needed by Chef to win. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ X < Y ≀ H ≀ 100$ ----- Sample Input 1 ------ 4 100 25 40 100 29 45 46 1 2 78 15 78 ----- Sample Output 1 ------ 4 3 45 1 ----- explanation 1 ------ Test case $1$: Chef can attack the boss $4$ times normally. This results in $25 + 25 + 25 + 25 = 100$ damage, which is enough to defeat the boss. Test case $2$: Chef can attack the boss $2$ times normally, then use the special attack. This results in $29 + 29 + 45 = 103$ damage, which is enough to defeat the boss. Test case $3$: Chef can proceed as follows: - First, use the special attack. This leaves the boss with $46 - 2 = 44$ health. - Then, use $44$ normal attacks to defeat the boss, since each one does $1$ damage. This takes a total of $44 + 1 = 45$ attacks. Test case $4$: Chef can use the special attack to immediately bring the health of the boss to zero, hence only needing one attack.
{"inputs": ["4\n100 25 40\n100 29 45\n46 1 2\n78 15 78\n"], "outputs": ["4\n3\n45\n1\n"]}
487
57
coding
Solve the programming task below in a Python markdown code block. Your friend has invited you to a party, and tells you to meet them in the line to get in. The one problem is it's a masked party. Everyone in line is wearing a colored mask, including your friend. Find which people in line could be your friend. Your friend has told you that he will be wearing a `RED` mask, and has **TWO** other friends with him, both wearing `BLUE` masks. Input to the function will be an array of strings, each representing a colored mask. For example: ```python line = ['blue','blue','red','red','blue','green'] ``` The output of the function should be the number of people who could possibly be your friend. ```python friend_find(['blue','blue','red','red','blue','green','chipmunk']) # return 1 ``` Also feel free to reuse/extend the following starter code: ```python def friend_find(line): ```
{"functional": "_inputs = [[['blue', 'blue', 'red', 'red', 'blue', 'green']], [['blue', 'red', 'blue', 'blue', 'red', 'blue', 'red']], [['brown', 'brown', 'red', 'green']], [['red', 'blue', 'blue', 'red']], [['red', 'blue']], [['blue', 'green', 'red', 'green', 'blue', 'blue', 'red']], [['blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue']], [['blue', 'red', 'blue', 'red', 'red', 'blue', 'red']]]\n_outputs = [[1], [2], [0], [2], [0], [1], [0], [1]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(friend_find(*i), o[0])"}
211
307
coding
Solve the programming task below in a Python markdown code block. Today Vasya visited a widely known site and learned that the continuation of his favourite game Codecraft II will appear after exactly k months. He looked at the calendar and learned that at the moment is the month number s. Vasya immediately got interested in what month Codecraft III will appear. Help him understand that. All the twelve months in Vasya's calendar are named using their usual English names: January, February, March, April, May, June, July, August, September, October, November, December. Input The first input line contains the name of the current month. It is guaranteed that it is a proper English name of one of twelve months. The first letter is uppercase, the rest are lowercase. The second line contains integer k (0 ≀ k ≀ 100) β€” the number of months left till the appearance of Codecraft III. Output Print starting from an uppercase letter the name of the month in which the continuation of Codeforces II will appear. The printed name must be contained in the list January, February, March, April, May, June, July, August, September, October, November, December. Examples Input November 3 Output February Input May 24 Output May
{"inputs": ["May\n4\n", "May\n7\n", "May\n5\n", "July\n4\n", "May\n44\n", "July\n2\n", "June\n1\n", "May\n48\n"], "outputs": ["September\n", "December\n", "October\n", "November\n", "January\n", "September\n", "July\n", "May\n"]}
270
88
coding
Solve the programming task below in a Python markdown code block. Define n!! as n!! = 1 \* 3 \* 5 \* ... \* n if n is odd, n!! = 2 \* 4 \* 6 \* ... \* n if n is even. Hence 8!! = 2 \* 4 \* 6 \* 8 = 384, there is no zero at the end. 30!! has 3 zeros at the end. For a positive integer n, please count how many zeros are there at the end of n!!. Example: count\_zeros\_n\_double\_fact(30) should return 3 Also feel free to reuse/extend the following starter code: ```python def count_zeros_n_double_fact(n): ```
{"functional": "_inputs = [[8], [30], [487], [500]]\n_outputs = [[0], [3], [0], [62]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(count_zeros_n_double_fact(*i), o[0])"}
183
181
coding
Solve the programming task below in a Python markdown code block. Chef has an array A of length N. He calls an index i (1 ≀ i ≀ N) *good* if there exists some j \neq i such that A_{i} = A_{j}. Chef can perform the following operation at most once: Choose any [subsequence] of the array A and add any positive integer to all the elements of the chosen subsequence. Determine the maximum number of good indices Chef can get. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines of input. - The first line of each test case contains a single integer N denoting the length of the array A. - The second line contains N space-separated integers denoting the array A. ------ Output Format ------ For each test case, output the maximum number of good indices Chef can get after performing the given operation at most once. ------ Constraints ------ $1 ≀ T ≀ 500$ $2 ≀ N ≀ 1000$ $1 ≀ A_{i} ≀ 1000$ - The sum of $N$ over all test cases won't exceed $2000$. ----- Sample Input 1 ------ 3 6 1 3 5 2 4 8 4 2 3 4 5 7 1 1 2 2 3 4 4 ----- Sample Output 1 ------ 4 4 7
{"inputs": ["3\n6\n1 3 5 2 4 8\n4\n2 3 4 5\n7\n1 1 2 2 3 4 4\n"], "outputs": ["4\n4\n7\n"]}
329
58
coding
Solve the programming task below in a Python markdown code block. *Words that contain many consecutive consonants, like "schtschurowskia", are generally considered somewhat hard to pronounce.* We say that a word is *hard to pronounce* if it contains 4 or more consonants in a row; otherwise it is *easy to pronounce*. For example, "apple" and "polish" are easy to pronounce, but "schtschurowskia" is hard to pronounce. You are given a string S consisting of N lowercase Latin characters. Determine whether it is easy to pronounce or not based on the rule above β€” print YES if it is easy to pronounce and NO otherwise. For the purposes of this problem, the vowels are the characters \{a, e, i, o, u\} and the consonants are the other 21 characters. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines of input. - The first line of each test case contains a single integer N, the length of string S. - The second line of each test case contains the string S. ------ Output Format ------ For each test case, output on a new line the answer β€” YES if S is easy to pronounce, and NO otherwise. Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yeS, yes, and YeS will all be treated as identical. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ N ≀ 100$ $S$ contains only lowercase Latin characters, i.e, the characters $\{a, b, c, \ldots, z\}$ ----- Sample Input 1 ------ 5 5 apple 15 schtschurowskia 6 polish 5 tryst 3 cry ----- Sample Output 1 ------ YES NO YES NO YES ----- explanation 1 ------ Test case $1$: "$\text{apple}$" doesn't have $4$ or move consecutive consonants, which makes it easy to pronounce. Test case $2$: "$\text{\textcolor{red}{schtsch}urowskia}$" has $7$ consecutive consonants, which makes it hard to pronounce. Test case $3$: $\text{polish}$ doesn't contain $4$ or more consecutive consonants, so it's easy to pronounce. Test case $4$: $\text{\textcolor{red}{tryst}}$ contains $5$ consecutive consonants, making it hard to pronounce. Test case $5$: $\text{cry}$ doesn't contain any vowels, but its length is less than $4$ so it's still easy to pronounce.
{"inputs": ["5\n5\napple\n15\nschtschurowskia\n6\npolish\n5\ntryst\n3\ncry"], "outputs": ["YES\nNO\nYES\nNO\nYES\n"]}
592
50
coding
Solve the programming task below in a Python markdown code block. Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer β€” the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
{"inputs": ["2\n2 1\n", "2\n1 1\n", "2\n1 2\n", "2\n1 2\n", "2\n2 2\n", "2\n1 2\n", "2\n2 1\n", "2\n1 1\n"], "outputs": ["2\n", "1\n", "3\n", "3\n", "2\n", "3\n", "2\n", "1\n"]}
701
102
coding
Solve the programming task below in a Python markdown code block. You are given N points (x_i,y_i) located on a two-dimensional plane. Consider a subset S of the N points that forms a convex polygon. Here, we say a set of points S forms a convex polygon when there exists a convex polygon with a positive area that has the same set of vertices as S. All the interior angles of the polygon must be strictly less than 180Β°. For example, in the figure above, {A,C,E} and {B,D,E} form convex polygons; {A,C,D,E}, {A,B,C,E}, {A,B,C}, {D,E} and {} do not. For a given set S, let n be the number of the points among the N points that are inside the convex hull of S (including the boundary and vertices). Then, we will define the score of S as 2^{n-|S|}. Compute the scores of all possible sets S that form convex polygons, and find the sum of all those scores. However, since the sum can be extremely large, print the sum modulo 998244353. -----Constraints----- - 1≀N≀200 - 0≀x_i,y_i<10^4 (1≀i≀N) - If iβ‰ j, x_iβ‰ x_j or y_iβ‰ y_j. - x_i and y_i are integers. -----Input----- The input is given from Standard Input in the following format: N x_1 y_1 x_2 y_2 : x_N y_N -----Output----- Print the sum of all the scores modulo 998244353. -----Sample Input----- 4 0 0 0 1 1 0 1 1 -----Sample Output----- 5 We have five possible sets as S, four sets that form triangles and one set that forms a square. Each of them has a score of 2^0=1, so the answer is 5.
{"inputs": ["1\n0 100", "1\n0 101", "1\n0 111", "1\n1 111", "1\n2 111", "1\n2 101", "1\n4 101", "1\n0 001"], "outputs": ["0", "0", "0", "0", "0", "0", "0", "0"]}
433
102
coding
Solve the programming task below in a Python markdown code block. [Generala](https://en.wikipedia.org/wiki/Generala) is a dice game popular in South America. It's very similar to [Yahtzee](https://en.wikipedia.org/wiki/Yahtzee) but with a different scoring approach. It is played with 5 dice, and the possible results are: | Result | Points | Rules | Samples | |---------------|--------|------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------| | GENERALA | 50 | When all rolled dice are of the same value. | 66666, 55555, 44444, 11111, 22222, 33333. | | POKER | 40 | Four rolled dice are of the same value. | 44441, 33233, 22262. | | FULLHOUSE | 30 | Three rolled dice are of the same value, the remaining two are of a different value, but equal among themselves. | 12121, 44455, 66116. | | STRAIGHT | 20 | Rolled dice are in sequential order. Dice with value `1` is a wildcard that can be used at the beginning of the straight, or at the end of it. | 12345, 23456, 34561, 13654, 62534. | | Anything else | 0 | Anything else will return `0` points. | 44421, 61623, 12346. | Please note that dice are not in order; for example `12543` qualifies as a `STRAIGHT`. Also, No matter what string value you get for the dice, you can always reorder them any order you need to make them qualify as a `STRAIGHT`. I.E. `12453`, `16543`, `15364`, `62345` all qualify as valid `STRAIGHT`s. Complete the function that is given the rolled dice as a string of length `5` and return the points scored in that roll. You can safely assume that provided parameters will be valid: * String of length 5, * Each character will be a number between `1` and `6` Also feel free to reuse/extend the following starter code: ```python def points(dice): ```
{"functional": "_inputs = [['55555'], ['44444'], ['44441'], ['33233'], ['22262'], ['12121'], ['44455'], ['66116'], ['12345'], ['23456'], ['34561'], ['13564'], ['62534'], ['44421'], ['61623'], ['12346']]\n_outputs = [[50], [50], [40], [40], [40], [30], [30], [30], [20], [20], [20], [20], [20], [0], [0], [0]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(points(*i), o[0])"}
578
320
coding
Solve the programming task below in a Python markdown code block. Uppal-balu is busy in extraction of gold from ores. He got an assignment from his favorite professor MH sir, but he got stuck in a problem. Can you help Uppal-balu to solve the problem. You are given an array $a_1$, $a_2$, $\dots$, $a_n$ of length $n$. You can perform the following operations on it: - Choose an index $i$ ($1 \leq i \leq n$), and replace $a_i$ by $a_{i} + 1$ or $a_{i} - 1$, which means add or subtract one to the element at index $i$. Beauty of the array is defined as maximum length of a subarray containing numbers which give same remainder upon dividing it by $k$ (that is, ($a_l$ mod $k$) = ($a_{l+1}$ mod $k$) = $\dots$ = ($a_r$ mod $k$) for some $1 \leq l \leq r \leq n$). You need to calculate the beauty of the array $a_1$, $a_2$, $\dots$, $a_n$ after applying at most $m$ operations. NOTE: - The subarray of a is a contiguous part of the array $a$, i. e. the array $a_l$, $a_{l+1}$, $\dots$, $a_r$ for some $1 \leq l \leq r \leq n$. - $x$ mod $y$ means the remainder of $x$ after dividing it by $y$. -----Input:----- - First line of input contains 3 integers $n$, $m$ and $k$. - The second line contains $n$ integers $a_1$, $a_2$, $\dots$, $a_n$. -----Output:----- Output in a single line the beauty of the array after applying at most m operations. -----Constraints----- - $1 \leq n,m \leq 2\cdot10^{5}$ - $1 \leq k \leq 7$ - $1 \leq a_i \leq 10^9$ -----Sample Input 1:----- 7 3 4 8 2 3 7 8 1 1 -----Sample Output 1:----- 5 -----Sample Input 2:----- 8 3 5 7 2 1 3 6 5 6 2 -----Sample Output 2:----- 5
{"inputs": ["7 3 4\n8 2 3 7 8 1 1", "8 3 5\n7 2 1 3 6 5 6 2"], "outputs": ["5", "5"]}
559
56
coding
Solve the programming task below in a Python markdown code block. Mandarin chinese , Russian and Vietnamese as well. Chef is organising a contest with $P$ problems (numbered $1$ through $P$). Each problem has $S$ subtasks (numbered $1$ through $S$). The difficulty of a problem can be calculated as follows: - Let's denote the score of the $k$-th subtask of this problem by $SC_k$ and the number of contestants who solved it by $NS_k$. - Consider the subtasks sorted in the order of increasing score. - Calculate the number $n$ of valid indices $k$ such that $NS_k > NS_{k + 1}$. - For problem $i$, the difficulty is a pair of integers $(n, i)$. You should sort the problems in the increasing order of difficulty levels. Since difficulty level is a pair, problem $a$ is more difficult than problem $b$ if the number $n$ is greater for problem $a$ than for problem $b$, or if $a > b$ and $n$ is the same for problems $a$ and $b$. -----Input----- - The first line of the input contains two space-separated integers $P$ and $S$ denoting the number of problems and the number of subtasks in each problem. - $2P$ lines follow. For each valid $i$, the $2i-1$-th of these lines contains $S$ space-separated integers $SC_1, SC_2, \dots, SC_S$ denoting the scores of the $i$-th problem's subtasks, and the $2i$-th of these lines contains $S$ space-separated integers $NS_1, NS_2, \dots, NS_S$ denoting the number of contestants who solved the $i$-th problem's subtasks. -----Output----- Print $P$ lines containing one integer each β€” the indices of the problems in the increasing order of difficulty. -----Constraints----- - $1 \le P \le 100,000$ - $2 \le S \le 30$ - $1 \le SC_i \le 100$ for each valid $i$ - $1 \le NS_i \le 1,000$ for each valid $i$ - in each problem, the scores of all subtasks are unique -----Subtasks----- Subtask #1 (25 points): $S = 2$ Subtask #2 (75 points): original constraints -----Example Input----- 3 3 16 24 60 498 861 589 14 24 62 72 557 819 16 15 69 435 779 232 -----Example Output----- 2 1 3
{"inputs": ["3 3\n16 24 60\n498 861 589\n14 24 62\n72 557 819\n16 15 69\n435 779 232"], "outputs": ["2\n1\n3"]}
624
80
coding
Solve the programming task below in a Python markdown code block. Chef is given two integers A and C such that A ≀ C. Chef wants to find whether there exists any integer B such that A, B, and C are in [arithmetic progression]. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two space-separated integers A and C, the given integers. ------ Output Format ------ For each test case, output -1 if there exists no integer B such that A, B, and C are in arithmetic progression. Else, output the value of B. ------ Constraints ------ $1 ≀ T ≀ 60$ $1 ≀ A ≀ C ≀ 10$ ----- Sample Input 1 ------ 4 3 5 6 6 2 7 1 9 ----- Sample Output 1 ------ 4 6 -1 5 ----- explanation 1 ------ Test case $1$: Considering $B = 4$, the three integers are in arithmetic progression as $B-A = C-B = 1$. Test case $2$: Considering $B = 6$, the three integers are in arithmetic progression as $B-A = C-B = 0$. Test case $3$: There exists no integral value of $B$ such that $A, B, $ and $C$ are in arithmetic progression. Test case $4$: Considering $B = 5$, the three integers are in arithmetic progression as $B-A = C-B = 4$.
{"inputs": ["4\n3 5\n6 6\n2 7\n1 9\n"], "outputs": ["4\n6\n-1\n5\n"]}
331
37
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a 0-indexed m x n binary matrix grid. You can move from a cell (row, col) to any of the cells (row + 1, col) or (row, col + 1) that has the value 1.Β The matrix is disconnected if there is no path from (0, 0) to (m - 1, n - 1). You can flip the value of at most one (possibly none) cell. You cannot flip the cells (0, 0) and (m - 1, n - 1). Return true if it is possible to make the matrix disconnect or false otherwise. Note that flipping a cell changes its value from 0 to 1 or from 1 to 0. Β  Please complete the following python code precisely: ```python class Solution: def isPossibleToCutPath(self, grid: List[List[int]]) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(grid = [[1,1,1],[1,0,0],[1,1,1]]) == True\n assert candidate(grid = [[1,1,1],[1,0,1],[1,1,1]]) == False\n\n\ncheck(Solution().isPossibleToCutPath)"}
211
76
coding
Solve the programming task below in a Python markdown code block. Meera bought a house on Mars, and plans to decorate it with chains of alien flowers. Each flower is either red ($\mbox{R}$) or blue ($\mbox{B}$), and Meera knows how many occurrences of RR, RB, BB, and BR she wants to see in a chain. The diagram below shows a flower chain of length $10$: In this example, RR occurs $2$ times (at positions ${0}$ and ${4}$), RB occurs $3$ times (at positions $1$, $5$, and $7$), BB occurs ${1}$ time (at position $2$), and BR occurs $3$ times (at positions $3$, ${6}$, and $8$). Meera wants your help determining how many different chains with positive length can be made. Given $A,B,C$, and $\mbox{D}$, find the number of different chains having occurrences of RR, RB, BB and BR equal to inputs $A,B,C$, and $\mbox{D}$, respectively. As the answer can be very large, your printed output should be ${answer}\%(10^9+7)$. Input Format One line of space-separated, non-negative integers: $\mbox{A}$ (occurrences of RR), $\mbox{B}$ (occurrences of RB), $\mbox{C}$ (occurrences of BB), and $\mbox{D}$ (occurrences of BR), respectively. Constraints For $20\%$ Points: ${0}\leq A,B,C,D\leq4$ For $50\%$ Points: $0\leq A,B,C,D\leq10^2$ For ${100%}$ Points: $0\leq A,B,C,D\leq10^5$ Output Format Find the number of chains having $A,B,C$, and $\mbox{D}$ occurrences of RR, RB, BB, and BR, respectively, and print the $\textit{answer}\% (10^9+7)$. Sample Input 1 1 2 1 Sample Output 5 Explanation The $5$ flower chains having exactly $1,1,2$, and ${1}$ occurrences of RR, RB, BB and BR are:
{"inputs": ["1 1 2 1\n"], "outputs": ["5\n"]}
502
20
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. The numeric value of a lowercase character is defined as its position (1-indexed) in the alphabet, so the numeric value of a is 1, the numeric value of b is 2, the numeric value of c is 3, and so on. The numeric value of a string consisting of lowercase characters is defined as the sum of its characters' numeric values. For example, the numeric value of the string "abe" is equal to 1 + 2 + 5 = 8. You are given two integers n and k. Return the lexicographically smallest string with length equal to n and numeric value equal to k. Note that a string x is lexicographically smaller than string y if x comes before y in dictionary order, that is, either x is a prefix of y, or if i is the first position such that x[i] != y[i], then x[i] comes before y[i] in alphabetic order. Β  Please complete the following python code precisely: ```python class Solution: def getSmallestString(self, n: int, k: int) -> str: ```
{"functional": "def check(candidate):\n assert candidate(n = 3, k = 27) == \"aay\"\n assert candidate(n = 5, k = 73) == \"aaszz\"\n\n\ncheck(Solution().getSmallestString)"}
246
62
coding
Solve the programming task below in a Python markdown code block. Arrow is getting paranoid about Malcom merlyn, his arch-enemy. All his efforts to subdue Merlyn have been in vain. These days Arrow is working on a problem with John Diggle. Diggle mentioned that the Felicity has been facing weird problem with their supercomputer, 'The ECHELON', recently. This afternoon, Arrow received a note from Merlyn, Saying that he has infected 'The ECHELON' with a virus. Moreover, the note had the number X printed on it. After doing some calculations, Arrow's friend Felicity figured out that the key to remove the virus is the largest Decent Number having X digits. A Decent Number has the following properties: 3, 5 or both as its digits. No other digit is allowed. Number of times 3 appears is divisible by 5. Number of times 5 appears is divisible by 3. Meanwhile, the counter to the destruction of 'The ECHELON' is running very fast. Can you save 'The ECHELON', and find the key before Arrow's friend Felicity? Input Format The 1st line will contain an integer T, the number of test cases. This is followed by T lines, each containing an integer X. i.e. the number of digits in the number. Output Format Largest Decent Number having X digits. If no such number exists, tell Arrow that he is wrong and print -1. Constraints 1 ≀ T ≀ 20 1 ≀ X ≀ 100000 SAMPLE INPUT 4 1 3 5 11 SAMPLE OUTPUT -1 555 33333 55555533333 Explanation For N=1, there is no such number. For N=3, 555 is the only possible number. For N=5, 33333 is the only possible number. For N=11, 55555533333 and all permutations of these digits are valid numbers; among them, the given number is the largest one.
{"inputs": ["4\n1\n3\n5\n11"], "outputs": ["-1\n555\n33333\n55555533333"]}
455
44
coding
Solve the programming task below in a Python markdown code block. Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. There are n chocolates, and you are given an array of n numbers where the i-th number A_{i} is the flavour type of the i-th chocolate. Sebrina wants to eat as many different types of chocolates as possible, but she also has to save at least x number of chocolates for her little brother. Find the maximum possible number of distinct flavour types Sebrina can have. ------ Input Format ------ The first line contains an integer T --- the number of test cases. - The first line of each test case consists of two integers n, x - The number of chocolates Sabrina has and the number of chocolates she has to save for her brother, respectively. - The second line contains n integers A_{1},\ldots, A_{n}, where the i-th chocolate has type A_{i}. ------ Output Format ------ For each test case, output a single integer denoting the maximum possible number of distinct chocolate flavours Sabrina can eat. ------ Constraints ------ $1≀T≀10$ $1 ≀x ≀ n ≀2 \cdot 10^{5} $ $1 ≀A_{i} ≀10^{9}$ - Sum of $n$ over all test cases do not exceed $ 2 \cdot 10^{5} $ ----- Sample Input 1 ------ 3 2 1 1 2 4 2 1 1 1 1 5 3 50 50 50 100 100 ----- Sample Output 1 ------ 1 1 2 ----- explanation 1 ------ Test case $1$: In the first case, the maximum number of chocolates Sebrina can have is $1$ as she has to leave $1$ chocolate for her brother. Thus, the maximum number of distinct chocolates is also $1$. Test case $2$: Sebrina has to leave $2$ chocolates for her brother. She can eat any $2$ chocolates. Since all the chocolates have the same flavor, it does not matter which chocolates she eats. The maximum number of distinct chocolates would be $1$. Test case $3$: Sebrina needs to save $3$ chocolates for her brother. She can eat any $2$ chocolates. To maximize the number of distinct chocolates, she can eat chocolates $1$ and $5$ with flavors $50$ and $100$ respectively. Thus maximum number of distinct chocolates is $2$.
{"inputs": ["3\n2 1\n1 2\n4 2\n1 1 1 1\n5 3\n50 50 50 100 100"], "outputs": ["1\n1\n2"]}
553
57
coding
Solve the programming task below in a Python markdown code block. Yaroslav thinks that two strings s and w, consisting of digits and having length n are non-comparable if there are two numbers, i and j (1 ≀ i, j ≀ n), such that s_{i} > w_{i} and s_{j} < w_{j}. Here sign s_{i} represents the i-th digit of string s, similarly, w_{j} represents the j-th digit of string w. A string's template is a string that consists of digits and question marks ("?"). Yaroslav has two string templates, each of them has length n. Yaroslav wants to count the number of ways to replace all question marks by some integers in both templates, so as to make the resulting strings incomparable. Note that the obtained strings can contain leading zeroes and that distinct question marks can be replaced by distinct or the same integers. Help Yaroslav, calculate the remainder after dividing the described number of ways by 1000000007 (10^9 + 7). -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the length of both templates. The second line contains the first template β€” a string that consists of digits and characters "?". The string's length equals n. The third line contains the second template in the same format. -----Output----- In a single line print the remainder after dividing the answer to the problem by number 1000000007 (10^9 + 7). -----Examples----- Input 2 90 09 Output 1 Input 2 11 55 Output 0 Input 5 ????? ????? Output 993531194 -----Note----- The first test contains no question marks and both strings are incomparable, so the answer is 1. The second test has no question marks, but the given strings are comparable, so the answer is 0.
{"inputs": ["1\n?\n?\n", "1\n?\n?\n", "2\n90\n09\n", "2\n11\n55\n", "2\n12\n?9\n", "2\n12\n?9\n", "2\n39\n09\n", "2\n11\n61\n"], "outputs": ["0\n", "0", "1\n", "0\n", "1\n", "1", "0\n", "0\n"]}
432
112
coding
Solve the programming task below in a Python markdown code block. Your goal in this kata is to implement a difference function, which subtracts one list from another and returns the result. It should remove all values from list `a`, which are present in list `b`. ```python array_diff([1,2],[1]) == [2] ``` If a value is present in `b`, all of its occurrences must be removed from the other: ```python array_diff([1,2,2,2,3],[2]) == [1,3] ``` ~~~ if:c NOTE: In C, assign return array length to pointer *z ~~~ Also feel free to reuse/extend the following starter code: ```python def array_diff(a, b): ```
{"functional": "_inputs = [[[1, 2], [1]], [[1, 2, 2], [1]], [[1, 2, 2], [2]], [[1, 2, 2], []], [[], [1, 2]]]\n_outputs = [[[2]], [[2, 2]], [[1]], [[1, 2, 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(array_diff(*i), o[0])"}
163
223
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are givenΒ an integer nΒ which is the length of a 0-indexed array nums, and a 0-indexed 2D-array ranges, which is a list of sub-ranges of numsΒ (sub-ranges may overlap). Each row ranges[i] has exactly 2 cells: ranges[i][0], which shows the start of the ith range (inclusive) ranges[i][1], which shows the end of the ith range (inclusive) These ranges cover some cells of numsΒ and leaveΒ some cells uncovered. Your task is to find all of the uncovered ranges with maximal length. Return a 2D-array answer of the uncovered ranges, sorted by the starting point in ascending order. By all of theΒ uncovered ranges with maximal length, we mean satisfying two conditions: Each uncovered cell should belong to exactly one sub-range There should not existΒ two ranges (l1, r1) and (l2, r2) such that r1 + 1 = l2 Β  Please complete the following python code precisely: ```python class Solution: def findMaximalUncoveredRanges(self, n: int, ranges: List[List[int]]) -> List[List[int]]: ```
{"functional": "def check(candidate):\n assert candidate(n = 10, ranges = [[3,5],[7,8]]) == [[0,2],[6,6],[9,9]]\n assert candidate(n = 3, ranges = [[0,2]]) == []\n assert candidate(n = 7, ranges = [[2,4],[0,3]]) == [[5,6]]\n\n\ncheck(Solution().findMaximalUncoveredRanges)"}
264
105
coding
Solve the programming task below in a Python markdown code block. The itertools module standardizes a core set of fast, memory efficient tools that are useful by themselves or in combination. Together, they form an iterator algebra making it possible to construct specialized tools succinctly and efficiently in pure Python. To read more about the functions in this module, check out their documentation here. You are given a list of $N$ lowercase English letters. For a given integer $\mbox{K}$, you can select any $\mbox{K}$ indices (assume $\mbox{I}$-based indexing) with a uniform probability from the list. Find the probability that at least one of the $\mbox{K}$ indices selected will contain the letter: '$\boldsymbol{\alpha}$'. Input Format The input consists of three lines. The first line contains the integer $N$, denoting the length of the list. The next line consists of $N$ space-separated lowercase English letters, denoting the elements of the list. The third and the last line of input contains the integer $\mbox{K}$, denoting the number of indices to be selected. Output Format Output a single line consisting of the probability that at least one of the $\mbox{K}$ indices selected contains the letter:'$\boldsymbol{\alpha}$'. Note: The answer must be correct up to 3 decimal places. Constraints $1\leq N\leq10$ $1\leq K\leq N$ All the letters in the list are lowercase English letters. Sample Input 4 a a c d 2 Sample Output 0.8333 Explanation All possible unordered tuples of length $2$ comprising of indices from $\mbox{I}$ to $\begin{array}{c}A\end{array}$ are: $(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)$ Out of these $\boldsymbol{6}$ combinations, $5$ of them contain either index $\mbox{I}$ or index $2$ which are the indices that contain the letter '$\boldsymbol{\alpha}$'. Hence, the answer is $\frac{5}{6}$.
{"inputs": ["4 \na a c d\n2\n"], "outputs": ["0.8333\n"]}
477
26
coding
Solve the programming task below in a Python markdown code block. You are given nonnegative integers a and b (a ≀ b), and a positive integer x. Among the integers between a and b, inclusive, how many are divisible by x? -----Constraints----- - 0 ≀ a ≀ b ≀ 10^{18} - 1 ≀ x ≀ 10^{18} -----Input----- The input is given from Standard Input in the following format: a b x -----Output----- Print the number of the integers between a and b, inclusive, that are divisible by x. -----Sample Input----- 4 8 2 -----Sample Output----- 3 There are three integers between 4 and 8, inclusive, that are divisible by 2: 4, 6 and 8.
{"inputs": ["0 5 2", "0 2 2", "0 2 4", "9 7 2", "1 3 4", "4 8 2", "9 9 2", "0 5 1"], "outputs": ["3\n", "2\n", "1\n", "-1\n", "0\n", "3", "0", "6"]}
167
91
coding
Solve the programming task below in a Python markdown code block. Four-digit palindromes start with `[1001,1111,1221,1331,1441,1551,1551,...]` and the number at position `2` is `1111`. You will be given two numbers `a` and `b`. Your task is to return the `a-digit` palindrome at position `b` if the palindromes were arranged in increasing order. Therefore, `palin(4,2) = 1111`, because that is the second element of the `4-digit` palindrome series. More examples in the test cases. Good luck! If you like palindrome Katas, please try: [Palindrome integer composition](https://www.codewars.com/kata/599b1a4a3c5292b4cc0000d5) [Life without primes](https://www.codewars.com/kata/59f8750ac374cba8f0000033) Also feel free to reuse/extend the following starter code: ```python def palin(a,b): ```
{"functional": "_inputs = [[2, 2], [3, 10], [4, 5], [5, 19], [6, 3], [6, 20], [7, 3]]\n_outputs = [[22], [191], [1441], [11811], [102201], [119911], [1002001]]\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(palin(*i), o[0])"}
267
240
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an array points where points[i] = [xi, yi] is the coordinates of the ith point on a 2D plane. Multiple points can have the same coordinates. You are also given an array queries where queries[j] = [xj, yj, rj] describes a circle centered at (xj, yj) with a radius of rj. For each query queries[j], compute the number of points inside the jth circle. Points on the border of the circle are considered inside. Return an array answer, where answer[j] is the answer to the jth query. Β  Please complete the following python code precisely: ```python class Solution: def countPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(points = [[1,3],[3,3],[5,3],[2,2]], queries = [[2,3,1],[4,3,1],[1,1,2]]) == [3,2,2]\n assert candidate(points = [[1,1],[2,2],[3,3],[4,4],[5,5]], queries = [[1,2,2],[2,2,2],[4,3,2],[4,3,3]]) == [2,3,2,4]\n\n\ncheck(Solution().countPoints)"}
181
135
coding
Solve the programming task below in a Python markdown code block. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k β‰₯ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≀ n ≀ 2Β·105) β€” the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≀ pi ≀ n) β€” the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer β€” the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1.
{"inputs": ["1\n1\n0\n", "1\n1\n1\n", "2\n1 2\n0 0\n", "2\n1 2\n1 1\n", "2\n2 1\n0 1\n", "2\n1 2\n1 0\n", "2\n2 1\n1 1\n", "2\n1 2\n0 1\n"], "outputs": ["1\n", "0\n", "3\n", "3\n", "0\n", "2\n", "1\n", "2\n"]}
511
126
coding
Solve the programming task below in a Python markdown code block. Hardy wants to test Ramanujam of his Mathematical skills. So he gives him a puzzle which is described below. Puzzle: There are 100 doors, numbered 1,2,....100 .Initially all doors are closed. Now a person comes(lets call him the 1st person)he opens the first door and leaves. Now a second guy comes and checks the status of second door(open/close) and does the following for every second door starting from door 2, If the door is closed,open it. If the door is already open, leave it as such. After performing the above task, he restores the second door to its initial state(open/close) and leaves. Now this process goes on till a 100th guy, doing the same procedure ie, The ith person checks the initial status of door numbered i, and then starting from door i, he does the following procedure to every ith door. If the door is closed,open it. If the door is already open, leave it as such. After performing the above task, he restores the ith door to its initial state.(open/close). Ramanujam now tells that there is a pattern followed in the doors closed and hence he can easily,find if a door is closed or opened. Hearing this Hardy tries to increase the difficulty of the puzzle saying that there are now 500000 doors instead of 100 and he wishes to know, the number of doors that are closed till xth door, given x.Can you help Ramanujam solve this problem? Input Format: The first line contains two integers n and t,the number of doors present and the number of test cases respectively. Then t lines follow.Each line contains a single integer x.The output should correspond to the value after all 'n' people have done their work. Output Format: For each input test case, output a number y which indicates the number of closed doors till door numbered x. Constraints: 1 ≀ t ≀ 100 2 ≀ n ≀ 500000 1 ≀ x ≀ n SAMPLE INPUT 4 2 2 3 SAMPLE OUTPUT 1 2 Explanation In the given test case,after person one, the first door alone is open. After second person,the door numbered 2 and 4 are opened and then 2 is closed.(Initially door 2 was closed) After person 3 , no change in the configuration as he first opens 3 and then closes 3. After person 4, no changes as he leaves it as such.Now doors 1 and 4 are open and 2 and 3 are open.Thus for 2, the output is 1 and for 3 the output is 2, corresponding to 2,3.
{"inputs": ["400012 31\n10194\n59030\n6930\n39402\n24765\n38154\n147756\n25399\n221328\n30059\n71528\n128184\n18710\n79764\n47450\n176355\n8061\n7335\n82824\n61380\n286810\n634\n7410\n153984\n2277\n46980\n12568\n18390\n173054\n20181\n184416"], "outputs": ["1252\n5968\n890\n4148\n2739\n4027\n13656\n2800\n19724\n3250\n7084\n11999\n2135\n7811\n4892\n16026\n1013\n935\n8095\n6175\n24982\n115\n939\n14186\n338\n4849\n1500\n2106\n15758\n2283\n16696"]}
594
363
coding
Solve the programming task below in a Python markdown code block. You are given a string $s$, consisting only of Latin letters 'a', and a string $t$, consisting of lowercase Latin letters. In one move, you can replace any letter 'a' in the string $s$ with a string $t$. Note that after the replacement string $s$ might contain letters other than 'a'. You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large. Two strings are considered different if they have different length, or they differ at some index. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^4$) β€” the number of testcases. The first line of each testcase contains a non-empty string $s$, consisting only of Latin letters 'a'. The length of $s$ doesn't exceed $50$. The second line contains a non-empty string $t$, consisting of lowercase Latin letters. The length of $t$ doesn't exceed $50$. -----Output----- For each testcase, print the number of different strings $s$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number. -----Examples----- Input 3 aaaa a aa abc a b Output 1 -1 2 -----Note----- In the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one. In the second example, you can replace the second letter 'a' with "abc". String $s$ becomes equal to "aabc". Then the second letter 'a' again. String $s$ becomes equal to "aabcbc". And so on, generating infinitely many different strings. In the third example, you can either leave string $s$ as is, performing zero moves, or replace the only 'a' with "b". String $s$ becomes equal to "b", so you can't perform more moves on it.
{"inputs": ["3\naaaa\na\naa\nabc\na\nb\n"], "outputs": ["1\n-1\n2\n"]}
469
31
coding
Solve the programming task below in a Python markdown code block. Because my other two parts of the serie were pretty well received I decided to do another part. Puzzle Tiles You will get two Integer n (width) and m (height) and your task is to draw following pattern. Each line is seperated with '\n'. Both integers are equal or greater than 1. No need to check for invalid parameters. There are no whitespaces at the end of each line. e.g.: \_( )\_\_ \_( )\_\_ \_( )\_\_ \_( )\_\_ \_| \_| \_| \_| \_| (\_ \_ (\_ \_ (\_ \_ (\_ \_ (\_ |\_\_( )\_|\_\_( )\_|\_\_( )\_|\_\_( )\_| |\_ |\_ |\_ |\_ |\_ puzzleTiles(4,3)=> \_) \_ \_) \_ \_) \_ \_) \_ \_) |\_\_( )\_|\_\_( )\_|\_\_( )\_|\_\_( )\_| \_| \_| \_| \_| \_| (\_ \_ (\_ \_ (\_ \_ (\_ \_ (\_ |\_\_( )\_|\_\_( )\_|\_\_( )\_|\_\_( )\_| For more informations take a look in the test cases! Serie: ASCII Fun ASCII Fun #1: X-Shape ASCII Fun #2: Funny Dots ASCII Fun #3: Puzzle Tiles ASCII Fun #4: Build a pyramid Also feel free to reuse/extend the following starter code: ```python def puzzle_tiles(width, height): ```
{"functional": "_inputs = [[1, 1], [3, 2]]\n_outputs = [[' _( )__\\n _| _|\\n(_ _ (_\\n |__( )_|'], [' _( )__ _( )__ _( )__\\n _| _| _| _|\\n(_ _ (_ _ (_ _ (_\\n |__( )_|__( )_|__( )_|\\n |_ |_ |_ |_\\n _) _ _) _ _) _ _)\\n |__( )_|__( )_|__( )_|']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(puzzle_tiles(*i), o[0])"}
395
263
coding
Solve the programming task below in a Python markdown code block. Chef has an array A of length N. He can perform the following operation on A: 1) Select an index i (1 ≀ i ≀ N - 1) and select an integer X (1 ≀ X < 2^{20}). \\ 2) Set A_{i} := (A_{i} \oplus X) and A_{i + 1} := (A_{i + 1} \oplus X). (Here, \oplus denotes the [bitwise XOR operation] Determine if Chef can make all the elements of A equal by applying the above operation any number of times (possibly zero). ------ Input Format ------ - The first line contains a single integer T β€” the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N β€” the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output YES if it is possible to make all the elements of A equal by applying the given operation any number of times. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{5}$ $0 ≀A_{i} < 2^{20}$ - The sum of $N$ over all test cases won't exceed $2 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 4 3 2 2 3 6 0 1 2 3 4 5 3 1 2 4 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Test Case 1: We can apply the following operation: - $[3, 2, 2, 3] \xrightarrow{i = 2, X = 1} [3, 3, 3, 3]$ Test Case 2: It can be proved that we can not make all the elements of $A$ equal by applying the given operation. Test Case 3: We can apply the following operations: - $[1, 2, 4] \xrightarrow{i = 1, X = 6} [7, 4, 4] \xrightarrow{i = 2, X = 3} [7, 7, 7]$
{"inputs": ["3\n4\n3 2 2 3\n6\n0 1 2 3 4 5\n3\n1 2 4\n"], "outputs": ["YES\nNO\nYES\n"]}
567
50
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol βŠ•) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K βŠ• F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ K, A_{i} ≀ 1000$ $Subtask 1 (30 points):1 ≀ N ≀ 20$ $Subtask 2 (70 points):1 ≀ N ≀ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
{"inputs": ["1\n3 4\n1 2 3", "1\n3 4\n0 2 3", "1\n0 4\n0 2 2", "1\n0 8\n0 2 0", "1\n3 1\n1 2 3", "1\n0 4\n0 0 0", "1\n0 8\n0 3 0", "1\n0 4\n0 2 3"], "outputs": ["7", "7\n", "6\n", "10\n", "3\n", "4\n", "11\n", "7\n"]}
428
143
coding
Solve the programming task below in a Python markdown code block. How many different ways can you make change for an amount, given a list of coins? In this problem, your code will need to efficiently compute the answer. Task Write a program that, given An amount N and types of infinite available coins M. A list of M coins - C={C1,C2,C3,..,CM} Prints out how many different ways you can make change from the coins to STDOUT. The problem can be formally stated: Given a value N, if we want to make change for N cents, and we have infinite supply of each of C={C1,C2,…,CM} valued coins, how many ways can we make the change? The order of coins doesn’t matter. Constraints 1≀Ci≀50 1≀N≀250 1≀M≀50 The list of coins will contain distinct integers. Input Format First line will contain 2 integer N and M respectively. Second line contain M integer that represent list of distinct coins that are available in infinite amount. Output Format One integer which is the number of ways in which we can get a sum of N from the given infinite supply of M types of coins. SAMPLE INPUT 4 3 1 2 3 SAMPLE OUTPUT 4 Explanation For N=4 and C={1,2,3} there are four solutions: {1,1,1,1},{1,1,2},{2,2},{1,3}
{"inputs": ["4 3\n1 2 3", "240 23\n23 20 35 42 19 3 34 9 28 38 13 41 26 14 27 39 24 37 46 29 43 1 21", "250 26\n8 47 13 24 25 31 32 35 3 19 40 48 1 4 17 38 22 30 33 15 44 46 36 9 20 49"], "outputs": ["4", "127101770", "3542323427"]}
319
198
coding
Solve the programming task below in a Python markdown code block. A string S of an odd length is said to be a strong palindrome if and only if all of the following conditions are satisfied: - S is a palindrome. - Let N be the length of S. The string formed by the 1-st through ((N-1)/2)-th characters of S is a palindrome. - The string consisting of the (N+3)/2-st through N-th characters of S is a palindrome. Determine whether S is a strong palindrome. -----Constraints----- - S consists of lowercase English letters. - The length of S is an odd number between 3 and 99 (inclusive). -----Input----- Input is given from Standard Input in the following format: S -----Output----- If S is a strong palindrome, print Yes; otherwise, print No. -----Sample Input----- akasaka -----Sample Output----- Yes - S is akasaka. - The string formed by the 1-st through the 3-rd characters is aka. - The string formed by the 5-th through the 7-th characters is aka. All of these are palindromes, so S is a strong palindrome.
{"inputs": ["ddd\n", "gvg\n", "llvee", "elvle", "elvld", "elvdl", "eludl", "ldule"], "outputs": ["Yes\n", "Yes\n", "No\n", "No\n", "No\n", "No\n", "No\n", "No\n"]}
248
76
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. There exists an array arr of length nums.length, where arr[i] is the sum of |i - j| over all j such that nums[j] == nums[i] and j != i. If there is no such j, set arr[i] to be 0. Return the array arr. Β  Please complete the following python code precisely: ```python class Solution: def distance(self, nums: List[int]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(nums = [1,3,1,1,2]) == [5,0,3,4,0]\n assert candidate(nums = [0,5,3]) == [0,0,0]\n\n\ncheck(Solution().distance)"}
121
68
coding
Solve the programming task below in a Python markdown code block. One day, Wet Shark was given an array $X=\{x_1,x_2,\ldots,x_m\}$. As always, he started playing with its subsequences. When you came to know about this habit, you presented him a task of finding all pairs of subsequences, $(A,B)$, which satisfies all of the following constraints. We will represent a pair of subsequence as $A=\{x_{a_1},x_{a_2},...,x_{a_n}\}$ and $B=\{x_{b_1},x_{b_2},...,x_{b_n}\}$ $\mbox{A}$ and $\mbox{B}$ must be of same length, i.e., $|A|=|B|$. $\sum\limits_{i=1}^{n}(x_{a_{i}}+x_{b_{i}})=r$ $\sum\limits_{i=1}^{n}(x_{a_{i}}-x_{b_{i}})=s$ Please help Wet Shark determine how many possible subsequences $\mbox{A}$ and $\mbox{B}$ can exist. Because the number of choices may be big, output your answer modulo $10^9+7=1000000007$. Note: Two segments are different if there's exists at least one index $\boldsymbol{i}$ such that element $x_i$ is present in exactly one of them. Both subsequences can overlap each other. Subsequences do not necessarily have to be distinct Input Format The first line consists of 3 space-separated integers $m$, $\textbf{r}$, $\boldsymbol{\mathrm{~S~}}$, where $m$ denotes the length of the original array, $\mbox{X}$, and $\textbf{r}$ and $\boldsymbol{\mathrm{~S~}}$ are as defined above. The next line contains $m$ space-separated integers, $x_1,x_2,\ldots,x_m$ , representing the elements of $\mbox{X}$. Constraints $1\leq m\leq100$ $0\leq r,\:\textit{s}\leq2000$ $1\leq x_i\leq2000$ Output Format Output total number of pairs of subsequences, $(A,B)$, satisfying the above conditions. As the number can be large, output it's modulo $10^9+7=1000000007$ Sample Input 0 4 5 3 1 1 1 4 Sample Output 0 3 Explanation 0 For array $X=\{x_1,x_2,x_3,x_4\}=\{1,1,1,4\}$ there are three pairs of subsequences: $A=\{x_4\}=\{4\};B=\{x_1\}=\{1\}$ $A=\{x_4\}=\{4\};B=\{x_2\}=\{1\}$ $A=\{x_4\}=\{4\};B=\{x_3\}=\{1\}$
{"inputs": ["4 5 3\n1 1 1 4\n"], "outputs": ["3\n"]}
717
26
coding
Solve the programming task below in a Python markdown code block. Vasya has a string $s$ of length $n$ consisting only of digits 0 and 1. Also he has an array $a$ of length $n$. Vasya performs the following operation until the string becomes empty: choose some consecutive substring of equal characters, erase it from the string and glue together the remaining parts (any of them can be empty). For example, if he erases substring 111 from string 111110 he will get the string 110. Vasya gets $a_x$ points for erasing substring of length $x$. Vasya wants to maximize his total points, so help him with this! -----Input----- The first line contains one integer $n$ ($1 \le n \le 100$) β€” the length of string $s$. The second line contains string $s$, consisting only of digits 0 and 1. The third line contains $n$ integers $a_1, a_2, \dots a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the number of points for erasing the substring of length $i$. -----Output----- Print one integer β€” the maximum total points Vasya can get. -----Examples----- Input 7 1101001 3 4 9 100 1 2 3 Output 109 Input 5 10101 3 10 15 15 15 Output 23 -----Note----- In the first example the optimal sequence of erasings is: 1101001 $\rightarrow$ 111001 $\rightarrow$ 11101 $\rightarrow$ 1111 $\rightarrow$ $\varnothing$. In the second example the optimal sequence of erasings is: 10101 $\rightarrow$ 1001 $\rightarrow$ 11 $\rightarrow$ $\varnothing$.
{"inputs": ["1\n1\n1337\n", "1\n1\n1337\n", "5\n10011\n8 7 55 0 4\n", "5\n10111\n8 7 60 0 5\n", "5\n01111\n5 1 37 3 5\n", "5\n10011\n8 7 71 0 4\n", "5\n10011\n8 7 55 0 5\n", "5\n10111\n8 7 55 0 5\n"], "outputs": ["1337\n", "1337\n", "71\n", "76\n", "47\n", "87\n", "71\n", "71\n"]}
450
198
coding
Solve the programming task below in a Python markdown code block. Heidi the Cow is aghast: cracks in the northern Wall? Zombies gathering outside, forming groups, preparing their assault? This must not happen! Quickly, she fetches her HC^2 (Handbook of Crazy Constructions) and looks for the right chapter: How to build a wall: Take a set of bricks. Select one of the possible wall designs. Computing the number of possible designs is left as an exercise to the reader. Place bricks on top of each other, according to the chosen design. This seems easy enough. But Heidi is a Coding Cow, not a Constructing Cow. Her mind keeps coming back to point 2b. Despite the imminent danger of a zombie onslaught, she wonders just how many possible walls she could build with up to n bricks. A wall is a set of wall segments as defined in the easy version. How many different walls can be constructed such that the wall consists of at least 1 and at most n bricks? Two walls are different if there exist a column c and a row r such that one wall has a brick in this spot, and the other does not. Along with n, you will be given C, the width of the wall (as defined in the easy version). Return the number of different walls modulo 10^6 + 3. -----Input----- The first line contains two space-separated integers n and C, 1 ≀ n ≀ 500000, 1 ≀ C ≀ 200000. -----Output----- Print the number of different walls that Heidi could build, modulo 10^6 + 3. -----Examples----- Input 5 1 Output 5 Input 2 2 Output 5 Input 3 2 Output 9 Input 11 5 Output 4367 Input 37 63 Output 230574 -----Note----- The number 10^6 + 3 is prime. In the second sample case, the five walls are: B B B., .B, BB, B., and .B In the third sample case, the nine walls are the five as in the second sample case and in addition the following four: B B B B B B B., .B, BB, and BB
{"inputs": ["5 1\n", "2 2\n", "3 2\n", "1 1\n", "1 1\n", "2 1\n", "3 4\n", "3 1\n"], "outputs": ["5\n", "5\n", "9\n", "1\n", "1\n", "2\n", "34\n", "3\n"]}
505
87
coding
Solve the programming task below in a Python markdown code block. In programming you know the use of the logical negation operator (**!**), it reverses the meaning of a condition. Your task is to complete the function 'negationValue()' that takes a string of negations with a value and returns what the value would be if those negations were applied to it. ```python negation_value("!", False) #=> True negation_value("!!!!!", True) #=> False negation_value("!!", []) #=> False ``` Do not use the `eval()` function or the `Function()` constructor in JavaScript. Note: Always return a boolean value, even if there're no negations. Also feel free to reuse/extend the following starter code: ```python def negation_value(s, val): ```
{"functional": "_inputs = [['!', False], ['!', True], ['!!!', []]]\n_outputs = [[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(negation_value(*i), o[0])"}
169
172
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. Chef and his friend John have not see each other for years. They are both are looking forward to meeting again in the city they both grew up in. The city has N public squares that they can meet in. The squares are numbered from 1 to N and are connected with N-1 roads. Each road has its own length It is known that between two squares there is exactly one path that goes through the roads and squares such that no square appears in the path more than once. Roads do not intersect each other and it takes 0 time to pass through a square. Chef and John have previously met in squares A_{1}, A_{2}, ..., A_{M} so they will meet in one of these squares again. To make this meeting more adventurous they do not agree on the square beforehand. Rather, Chef will pick a random square C from this list of squares and John will independently pick a random square J from this list of squares. Both random choices will be taken uniformly over the list of squares. In the day before the meeting, Chef spoke with his roomate about their plan and asked him to calculate the expected distance between squares that Chef and John randomly pick. Please remember that Chef's roommate knows neither C nor J. Help him calculate this expected distance. ------ Input ------ The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains two space-separated integers N and M. The next N-1 lines contain three space-separated integers V, U, and L describing a road with length L between squares V and U. The final line of each case contains M space-separated integers A_{1}, A_{2}, ..., A_{M}. ------ Output ------ For each test case, output a single line containing two integers numer and denom separated by a space. These indicate the fraction numer/denom giving expected distance between the squares randomly chosen by Chef and John. This fraction should be reduced so that gcd(numer, denom) = 1 and denom β‰₯ 1. ------ Constraints ------ $1 ≀ T ≀ 5$ $1 ≀ N ≀ 50000$ $1 ≀ M ≀ N$ $1 ≀ V ≀ N$ $1 ≀ U ≀ N$ $1 ≀ L ≀ 10000$ $1 ≀ A_{i} ≀ N$ $The squares A_{i} are distinct.$ $Subtask #1 [40 points]: N ≀ 1000$ $Subtask #2 [60 points]: No additional constraints$ ----- Sample Input 1 ------ 2 6 2 1 3 1 2 3 2 3 4 3 4 5 4 4 6 5 1 5 6 6 1 3 1 2 3 2 3 4 3 4 5 4 4 6 5 1 2 3 4 5 6 ----- Sample Output 1 ------ 4 1 29 6
{"inputs": ["2\n6 2\n1 3 1\n2 3 2\n3 4 3\n4 5 4\n4 6 5\n1 5\n6 6\n1 3 1\n2 3 2\n3 4 3\n4 5 4\n4 6 5\n1 2 3 4 5 6"], "outputs": ["4 1\n29 6"]}
671
103
coding
Solve the programming task below in a Python markdown code block. The brave Knight came to the King and asked permission to marry the princess. The King knew that the Knight was brave, but he also wanted to know if he was smart enough. So he asked him to solve the following task. There is a permutation $p_i$ of numbers from 1 to $2n$. You can make two types of operations. Swap $p_1$ and $p_2$, $p_3$ and $p_4$, ..., $p_{2n-1}$ and $p_{2n}$. Swap $p_1$ and $p_{n+1}$, $p_2$ and $p_{n+2}$, ..., $p_{n}$ and $p_{2n}$. The task is to find the minimal number of operations required to sort the given permutation. The Knight was not that smart actually, but quite charming, so the princess asks you to help him to solve the King's task. -----Input----- The first line contains the integer $n$ ($1\le n\le 1000$). The second line contains $2n$ integers $p_i$ β€” the permutation of numbers from 1 to $2n$. -----Output----- Print one integer β€” the minimal number of operations required to sort the permutation. If it is impossible to sort the permutation using these operations, print $-1$. -----Examples----- Input 3 6 3 2 5 4 1 Output 3 Input 2 3 4 2 1 Output -1 Input 4 1 2 3 4 5 6 7 8 Output 0 -----Note----- In the first example, you can sort the permutation in three operations: Make operation 1: $3, 6, 5, 2, 1, 4$. Make operation 2: $2, 1, 4, 3, 6, 5$. Make operation 1: $1, 2, 3, 4, 5, 6$.
{"inputs": ["2\n3 4 2 1\n", "2\n1 4 4 2\n", "2\n1 2 4 2\n", "2\n3 4 2 1\n", "3\n6 3 2 5 4 1\n", "3\n5 4 1 6 3 2\n", "3\n5 4 1 6 3 2\n", "3\n6 1 2 5 4 2\n"], "outputs": ["-1\n", "-1\n", "-1\n", "\n-1\n", "3\n", "2\n", "2\n", "-1\n"]}
453
153
coding
Solve the programming task below in a Python markdown code block. Create a function that will return true if all numbers in the sequence follow the same counting pattern. If the sequence of numbers does not follow the same pattern, the function should return false. Sequences will be presented in an array of varied length. Each array will have a minimum of 3 numbers in it. The sequences are all simple and will not step up in varying increments such as a Fibonacci sequence. JavaScript examples: Also feel free to reuse/extend the following starter code: ```python def validate_sequence(sequence): ```
{"functional": "_inputs = [[[1, 2, 3, 4, 5, 8, 7, 8, 9]], [[2, 8, 6, 7, 4, 3, 1, 5, 9]], [[1, 2, 3, 4, 5, 6, 7, 8, 9]], [[0, 1, 2, 3, 4, 5, 6, 7, 8]], [[1, 3, 5, 7, 9, 11, 13, 15]], [[1, 3, 5, 7, 8, 12, 14, 16]], [[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]], [[0, 2, 4, 6, 8]], [[2, 4, 6, 8, 10]], [[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]], [[2, 4, 6, 8, 10, 12, 14, 16, 18, 22]], [[2, 4, 6, 8, 10, 12, 13, 16, 18, 20]], [[3, 7, 9]], [[3, 6, 9]], [[3, 6, 8]], [[50, 100, 200, 400, 800]], [[50, 100, 150, 200, 250]], [[100, 200, 300, 400, 500, 600]]]\n_outputs = [[False], [False], [True], [True], [True], [False], [False], [True], [True], [True], [False], [False], [False], [True], [False], [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(validate_sequence(*i), o[0])"}
118
641
coding
Solve the programming task below in a Python markdown code block. $n$ fishermen have just returned from a fishing vacation. The $i$-th fisherman has caught a fish of weight $a_i$. Fishermen are going to show off the fish they caught to each other. To do so, they firstly choose an order in which they show their fish (each fisherman shows his fish exactly once, so, formally, the order of showing fish is a permutation of integers from $1$ to $n$). Then they show the fish they caught according to the chosen order. When a fisherman shows his fish, he might either become happy, become sad, or stay content. Suppose a fisherman shows a fish of weight $x$, and the maximum weight of a previously shown fish is $y$ ($y = 0$ if that fisherman is the first to show his fish). Then: if $x \ge 2y$, the fisherman becomes happy; if $2x \le y$, the fisherman becomes sad; if none of these two conditions is met, the fisherman stays content. Let's call an order in which the fishermen show their fish emotional if, after all fishermen show their fish according to this order, each fisherman becomes either happy or sad. Calculate the number of emotional orders modulo $998244353$. -----Input----- The first line contains one integer $n$ ($2 \le n \le 5000$). The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print one integer β€” the number of emotional orders, taken modulo $998244353$. -----Examples----- Input 4 1 1 4 9 Output 20 Input 4 4 3 2 1 Output 0 Input 3 4 2 1 Output 6 Input 8 42 1337 13 37 420 666 616 97 Output 19200
{"inputs": ["2\n1 1\n", "2\n1 1\n", "3\n4 2 1\n", "3\n4 9 1\n", "3\n4 3 1\n", "3\n4 6 1\n", "3\n4 2 1\n", "4\n1 1 4 9\n"], "outputs": ["0\n", "0\n", "6\n", "6\n", "0\n", "0\n", "6\n", "20\n"]}
469
117
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) is a bad pair if i < j and j - i != nums[j] - nums[i]. Return the total number of bad pairs in nums. Β  Please complete the following python code precisely: ```python class Solution: def countBadPairs(self, nums: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [4,1,3,3]) == 5\n assert candidate(nums = [1,2,3,4,5]) == 0\n\n\ncheck(Solution().countBadPairs)"}
100
58
coding
Solve the programming task below in a Python markdown code block. Louise and Richard have developed a numbers game. They pick a number and check to see if it is a power of $2$. If it is, they divide it by $2$. If not, they reduce it by the next lower number which is a power of $2$. Whoever reduces the number to $1$ wins the game. Louise always starts. Given an initial value, determine who wins the game. Example $n=132$ It's Louise's turn first. She determines that $132$ is not a power of $2$. The next lower power of $2$ is $\textbf{128}$, so she subtracts that from $132$ and passes $4$ to Richard. $4$ is a power of $2$, so Richard divides it by $2$ and passes $2$ to Louise. Likewise, $2$ is a power so she divides it by $2$ and reaches $1$. She wins the game. Update If they initially set counter to $1$, Richard wins. Louise cannot make a move so she loses. Function Description Complete the counterGame function in the editor below. counterGame has the following parameter(s): int n: the initial game counter value Returns string: either Richard or Louise Input Format The first line contains an integer $\boldsymbol{\boldsymbol{t}}$, the number of testcases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the initial value for each game. Constraints $1\leq t\leq10$ $1\leq n\leq2^{64}-1$ Sample Input 0 1 6 Sample Output 0 Richard Explanation 0 $\boldsymbol{6}$ is not a power of $2$ so Louise reduces it by the largest power of $2$ less than $\boldsymbol{6}$:$6-4=2$. $2$ is a power of $2$ so Richard divides by $2$ to get $1$ and wins the game.
{"inputs": ["1\n6\n"], "outputs": ["Richard\n"]}
466
16
coding
Solve the programming task below in a Python markdown code block. In your class, you have started lessons about [arithmetic progression](https://en.wikipedia.org/wiki/Arithmetic_progression). Since you are also a programmer, you have decided to write a function that will return the first `n` elements of the sequence with the given common difference `d` and first element `a`. Note that the difference may be zero! The result should be a string of numbers, separated by comma and space. ## Example ```python # first element: 1, difference: 2, how many: 5 arithmetic_sequence_elements(1, 2, 5) == "1, 3, 5, 7, 9" ``` ~~~if:fortran *NOTE: In Fortran, your returned string is* **not** *permitted to contain redundant leading/trailing whitespace.* ~~~ Also feel free to reuse/extend the following starter code: ```python def arithmetic_sequence_elements(a, r, n): ```
{"functional": "_inputs = [[1, 2, 5], [1, 0, 5], [1, -3, 10], [100, -10, 10]]\n_outputs = [['1, 3, 5, 7, 9'], ['1, 1, 1, 1, 1'], ['1, -2, -5, -8, -11, -14, -17, -20, -23, -26'], ['100, 90, 80, 70, 60, 50, 40, 30, 20, 10']]\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(arithmetic_sequence_elements(*i), o[0])"}
218
298
coding
Solve the programming task below in a Python markdown code block. You are given a complete undirected graph. For each pair of vertices you are given the length of the edge that connects them. Find the shortest paths between each pair of vertices in the graph and return the length of the longest of them. -----Input----- The first line of the input contains a single integer N (3 ≀ N ≀ 10). The following N lines each contain N space-separated integers. jth integer in ith line a_{ij} is the length of the edge that connects vertices i and j. a_{ij} = a_{ji}, a_{ii} = 0, 1 ≀ a_{ij} ≀ 100 for i β‰  j. -----Output----- Output the maximum length of the shortest path between any pair of vertices in the graph. -----Examples----- Input 3 0 1 1 1 0 4 1 4 0 Output 2 Input 4 0 1 2 3 1 0 4 5 2 4 0 6 3 5 6 0 Output 5 -----Note----- You're running short of keywords, so you can't use some of them:define do for foreach while repeat until if then else elif elsif elseif case switch
{"inputs": ["3\n0 1 1\n1 0 4\n1 4 0\n", "3\n0 1 1\n1 0 1\n1 1 0\n", "3\n0 1 1\n1 0 1\n1 1 0\n", "3\n0 1 1\n1 0 8\n1 4 0\n", "3\n0 1 1\n1 0 4\n1 4 0\n", "3\n0 72 17\n72 0 8\n17 8 0\n", "3\n0 99 73\n99 0 8\n73 8 0\n", "3\n0 99 73\n99 0 8\n73 8 0\n"], "outputs": ["2\n", "1\n", "1", "2\n", "2", "25\n", "81\n", "81"]}
289
226
coding
Solve the programming task below in a Python markdown code block. You have a sequence A composed of N positive integers: A_{1}, A_{2}, \cdots, A_{N}. You will now successively do the following Q operations: - In the i-th operation, you replace every element whose value is B_{i} with C_{i}. For each i (1 \leq i \leq Q), find S_{i}: the sum of all elements in A just after the i-th operation. -----Constraints----- - All values in input are integers. - 1 \leq N, Q, A_{i}, B_{i}, C_{i} \leq 10^{5} - B_{i} \neq C_{i} -----Input----- Input is given from Standard Input in the following format: N A_{1} A_{2} \cdots A_{N} Q B_{1} C_{1} B_{2} C_{2} \vdots B_{Q} C_{Q} -----Output----- Print Q integers S_{i} to Standard Output in the following format: S_{1} S_{2} \vdots S_{Q} Note that S_{i} may not fit into a 32-bit integer. -----Sample Input----- 4 1 2 3 4 3 1 2 3 4 2 4 -----Sample Output----- 11 12 16 Initially, the sequence A is 1,2,3,4. After each operation, it becomes the following: - 2, 2, 3, 4 - 2, 2, 4, 4 - 4, 4, 4, 4
{"inputs": ["4\n1 2 3 3\n3\n1 2\n3 4\n2 4", "4\n2 1 1 1\n3\n1 2\n2 1\n3 5", "4\n1 2 3 3\n3\n1 2\n3 4\n0 4", "4\n0 1 1 1\n3\n1 2\n2 1\n3 5", "4\n1 3 3 3\n3\n1 2\n3 4\n0 4", "4\n1 3 4 3\n3\n1 2\n3 4\n0 4", "4\n0 2 4 3\n3\n1 2\n3 4\n0 4", "4\n0 2 4 4\n3\n1 4\n3 4\n0 4"], "outputs": ["10\n12\n16\n", "8\n4\n4\n", "10\n12\n12\n", "6\n3\n3\n", "11\n14\n14\n", "12\n14\n14\n", "9\n10\n14\n", "10\n10\n14\n"]}
380
287
coding
Solve the programming task below in a Python markdown code block. A sequence a0, a1, ..., at - 1 is called increasing if ai - 1 < ai for each i: 0 < i < t. You are given a sequence b0, b1, ..., bn - 1 and a positive integer d. In each move you may choose one element of the given sequence and add d to it. What is the least number of moves required to make the given sequence increasing? Input The first line of the input contains two integer numbers n and d (2 ≀ n ≀ 2000, 1 ≀ d ≀ 106). The second line contains space separated sequence b0, b1, ..., bn - 1 (1 ≀ bi ≀ 106). Output Output the minimal number of moves needed to make the sequence increasing. Examples Input 4 2 1 3 3 2 Output 3
{"inputs": ["2 1\n1 2\n", "2 1\n1 1\n", "2 7\n1 1\n", "2 1\n2 5\n", "2 2\n1 1\n", "2 1\n4 5\n", "2 2\n1 0\n", "2 12\n1 1\n"], "outputs": ["0\n", "1\n", "1\n", "0\n", "1\n", "0\n", "1\n", "1\n"]}
200
119
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a binary string s and a positive integer n, return true if the binary representation of all the integers in the range [1, n] are substrings of s, or false otherwise. A substring is a contiguous sequence of characters within a string. Β  Please complete the following python code precisely: ```python class Solution: def queryString(self, s: str, n: int) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(s = \"0110\", n = 3) == True\n assert candidate(s = \"0110\", n = 4) == False\n\n\ncheck(Solution().queryString)"}
101
56
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a matrix $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). You need to fill the matrix with candies in such a way that: For each cell, the number of candies in this cell is an integer between $0$ and $X$ inclusive. For each pair of adjacent cells, the number of candies in one cell plus the number of candies in the other cell does not exceed $Y$. Two cells are considered adjacent if they share a side. Find the maximum number of candies you can place in the given matrix. ------ 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 four space-separated integers $N$, $M$, $X$ and $Y$. ------ Output ------ For each test case, print a single line containing one integer ― the maximum number of candies. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ N, M ≀ 100$ $1 ≀ X, Y ≀ 10^{4}$ ------ Subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 2 4 4 4 6 3 4 4 5 ----- Sample Output 1 ------ 48 30
{"inputs": ["2\n4 4 4 6\n3 4 4 5"], "outputs": ["48\n30"]}
360
32
coding
Solve the programming task below in a Python markdown code block. You have a card deck of $n$ cards, numbered from top to bottom, i. e. the top card has index $1$ and bottom card β€” index $n$. Each card has its color: the $i$-th card has color $a_i$. You should process $q$ queries. The $j$-th query is described by integer $t_j$. For each query you should: find the highest card in the deck with color $t_j$, i. e. the card with minimum index; print the position of the card you found; take the card and place it on top of the deck. -----Input----- The first line contains two integers $n$ and $q$ ($2 \le n \le 3 \cdot 10^5$; $1 \le q \le 3 \cdot 10^5$) β€” the number of cards in the deck and the number of queries. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 50$) β€” the colors of cards. The third line contains $q$ integers $t_1, t_2, \dots, t_q$ ($1 \le t_j \le 50$) β€” the query colors. It's guaranteed that queries ask only colors that are present in the deck. -----Output----- Print $q$ integers β€” the answers for each query. -----Examples----- Input 7 5 2 1 1 4 3 3 1 3 2 1 1 4 Output 5 2 3 1 5 -----Note----- Description of the sample: the deck is $[2, 1, 1, 4, \underline{3}, 3, 1]$ and the first card with color $t_1 = 3$ has position $5$; the deck is $[3, \underline{2}, 1, 1, 4, 3, 1]$ and the first card with color $t_2 = 2$ has position $2$; the deck is $[2, 3, \underline{1}, 1, 4, 3, 1]$ and the first card with color $t_3 = 1$ has position $3$; the deck is $[\underline{1}, 2, 3, 1, 4, 3, 1]$ and the first card with color $t_4 = 1$ has position $1$; the deck is $[1, 2, 3, 1, \underline{4}, 3, 1]$ and the first card with color $t_5 = 4$ has position $5$.
{"inputs": ["5 1\n1 2 3 4 5\n1\n", "5 1\n1 2 3 4 5\n1\n", "5 1\n1 4 3 4 5\n1\n", "5 1\n1 2 3 4 5\n2\n", "5 1\n2 5 1 8 5\n1\n", "5 1\n1 4 3 4 6\n1\n", "5 1\n1 2 3 4 6\n1\n", "5 1\n1 4 3 5 5\n1\n"], "outputs": ["1 ", "1 ", "1 ", "2 ", "3 ", "1 ", "1 ", "1 "]}
609
175
coding
Solve the programming task below in a Python markdown code block. M-kun is a brilliant air traffic controller. On the display of his radar, there are N airplanes numbered 1, 2, ..., N, all flying at the same altitude. Each of the airplanes flies at a constant speed of 0.1 per second in a constant direction. The current coordinates of the airplane numbered i are (X_i, Y_i), and the direction of the airplane is as follows: * if U_i is `U`, it flies in the positive y direction; * if U_i is `R`, it flies in the positive x direction; * if U_i is `D`, it flies in the negative y direction; * if U_i is `L`, it flies in the negative x direction. To help M-kun in his work, determine whether there is a pair of airplanes that will collide with each other if they keep flying as they are now. If there is such a pair, find the number of seconds after which the first collision will happen. We assume that the airplanes are negligibly small so that two airplanes only collide when they reach the same coordinates simultaneously. Constraints * 1 \leq N \leq 200000 * 0 \leq X_i, Y_i \leq 200000 * U_i is `U`, `R`, `D`, or `L`. * The current positions of the N airplanes, (X_i, Y_i), are all distinct. * N, X_i, and Y_i are integers. Input Input is given from Standard Input in the following format: N X_1 Y_1 U_1 X_2 Y_2 U_2 X_3 Y_3 U_3 : X_N Y_N U_N Output If there is a pair of airplanes that will collide with each other if they keep flying as they are now, print an integer representing the number of seconds after which the first collision will happen. If there is no such pair, print `SAFE`. Examples Input 2 11 1 U 11 47 D Output 230 Input 4 20 30 U 30 20 R 20 10 D 10 20 L Output SAFE Input 8 168 224 U 130 175 R 111 198 D 121 188 L 201 116 U 112 121 R 145 239 D 185 107 L Output 100
{"inputs": ["2\n10 -1 U\n3 0 D", "2\n10 -1 U\n2 0 D", "2\n20 1 U\n11 47 D", "2\n20 1 U\n11 37 D", "2\n20 0 U\n11 37 D", "2\n20 0 U\n12 37 D", "2\n20 0 U\n12 70 D", "2\n10 0 U\n3 103 D"], "outputs": ["SAFE\n", "SAFE\n", "SAFE\n", "SAFE\n", "SAFE\n", "SAFE\n", "SAFE\n", "SAFE\n"]}
574
162
coding
Solve the programming task below in a Python markdown code block. You are given two arrays A and B, both of length N. You would like to choose exactly K distinct indices i_{1},i_{2}, \ldots, i_{K} such that \min(A_{i_{1}}+A_{i_{2}}+ \ldots + A_{i_{K}}, B_{i_{1}}+B_{i_{2}}+\ldots + B_{i_{K}}) is maximized. Find this maximum value. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of 3 lines of input. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the elements of A. - The third line contains N space-separated integers B_{1}, B_{2}, \ldots, B_{N} denoting the elements of B. ------ Output Format ------ For each test case, output a new line containing one integer β€” the required maximum value. ------ Constraints ------ $1 ≀ T ≀ 20$ $2 ≀ N ≀ 40$ $1 ≀ K ≀ N$ $1 ≀ A_{i}, B_{i} ≀ 40$ ----- Sample Input 1 ------ 3 5 3 4 2 3 1 4 3 2 5 5 1 4 2 1 2 3 4 4 3 2 1 6 3 8 10 3 6 7 2 4 8 4 1 1 6 ----- Sample Output 1 ------ 9 5 18 ----- explanation 1 ------ Test Case $1$: One optimal choice of indices is $i_{1} = 1, i_{2} = 3, i_{3} = 5$. This gives $A_{1} + A_{3} + A_{5} = 11$ and $B_{1} + B_{3} + B_{5} = 9$. Test Case $2$: One optimal choice of indices is $i_{1} = 1, i_{2} = 4$. Test Case $3$: One optimal choice of indices is $i_{1} = 1, i_{2} = 2, i_{3} = 6$.
{"inputs": ["3\n5 3\n4 2 3 1 4\n3 2 5 5 1\n4 2\n1 2 3 4\n4 3 2 1\n6 3\n8 10 3 6 7 2\n4 8 4 1 1 6\n"], "outputs": ["9\n5\n18\n"]}
553
92
coding
Solve the programming task below in a Python markdown code block. We have a large square grid with H rows and W columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell. However, she cannot enter the cells in the intersection of the bottom A rows and the leftmost B columns. (That is, there are AΓ—B forbidden cells.) There is no restriction on entering the other cells. Find the number of ways she can travel to the bottom-right cell. Since this number can be extremely large, print the number modulo 10^9+7. -----Constraints----- - 1 ≦ H, W ≦ 100,000 - 1 ≦ A < H - 1 ≦ B < W -----Input----- The input is given from Standard Input in the following format: H W A B -----Output----- Print the number of ways she can travel to the bottom-right cell, modulo 10^9+7. -----Sample Input----- 2 3 1 1 -----Sample Output----- 2 We have a 2Γ—3 grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".
{"inputs": ["2 3 1 1\n", "10 7 3 4\n", "100000 100000 99999 99999\n", "100000 100000 44444 55555\n"], "outputs": ["2\n", "3570\n", "1\n", "738162020\n"]}
285
110
coding
Solve the programming task below in a Python markdown code block. Chef goes to the supermarket to buy some items. Luckily there's a sale going on under which Chef gets the following offer: If Chef buys 3 items then he gets the item (out of those 3 items) having the lowest price as free. For e.g. if Chef bought 3 items with the cost 6, 2 and 4, then he would get the item with cost 2 as free. So he would only have to pay the cost of the other two items which will be 6 + 4 = 10. Chef buys 3 items having prices A, B and C respectively. What is the amount of money Chef needs to pay? ------ Input Format ------ - The first line will contain an integer T - number of test cases. Then the test cases follow. - The first and only line of each test case contains three integers A, B, C - the prices of the items bought by Chef. ------ Output Format ------ For each test case, output the price paid by Chef. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ A, B, C ≀ 10$ ----- Sample Input 1 ------ 3 6 2 4 3 3 3 8 4 4 ----- Sample Output 1 ------ 10 6 12 ----- explanation 1 ------ Test case-1: Explained in the problem statement. Test case-2: Since all the three items have the same price, Chef will get one of them free and will have to pay the cost of the other two items which will be $3 + 3 = 6$. Test case-3: Chef will get one of the items having price $4$ as free and will have to pay the cost of the other two items which will be $8 + 4 = 12$.
{"inputs": ["3\n6 2 4\n3 3 3\n8 4 4\n"], "outputs": ["10\n6\n12\n"]}
402
38
coding
Solve the programming task below in a Python markdown code block. Do you know that The Chef has a special interest in palindromes? Yes he does! Almost all of the dishes in his restaurant is named by a palindrome strings. The problem is that a name of a dish should not be too long, so The Chef has only limited choices when naming a new dish. For the given positive integer N, your task is to calculate the number of palindrome strings of length not exceeding N, that contain only lowercase letters of English alphabet (letters from 'a' to 'z', inclusive). Recall that a palindrome is a string that reads the same left to right as right to left (as in "radar"). For example: - For N = 1, we have 26 different palindromes of length not exceeding N: "a", "b", ..., "z". - For N = 2 we have 52 different palindromes of length not exceeding N: "a", "b", ..., "z", "aa", "bb", ..., "zz". - For N = 3 we have 728 different palindromes of length not exceeding N: "a", "b", ..., "z", "aa", "bb", ..., "zz", "aaa", "aba", ..., "aza", "bab", "bbb", ..., "bzb", ..., "zaz", "zbz", ..., "zzz". Since the answer can be quite large you should output it modulo 1000000007 (109 + 7). Yes, we know, most of you already hate this modulo, but there is nothing we can do with it :) -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a single integer N. -----Output----- For each test case, output a single line containing the answer for the corresponding test case. -----Constrains----- - 1 ≀ T ≀ 1000 - 1 ≀ N ≀ 109 -----Example----- Input: 5 1 2 3 4 100 Output: 26 52 728 1404 508533804 -----Explanation----- The first three examples are explained in the problem statement above.
{"inputs": ["5\n1\n3\n3\n4\n100", "5\n1\n2\n3\n4\n110", "5\n1\n2\n1\n4\n110", "5\n1\n4\n1\n4\n110", "5\n1\n2\n3\n4\n101", "5\n1\n3\n3\n4\n110", "5\n1\n2\n3\n4\n111", "5\n1\n2\n2\n4\n110"], "outputs": ["26\n728\n728\n1404\n508533804\n", "26\n52\n728\n1404\n316452997\n", "26\n52\n26\n1404\n316452997\n", "26\n1404\n26\n1404\n316452997\n", "26\n52\n728\n1404\n865206338\n", "26\n728\n728\n1404\n316452997\n", "26\n52\n728\n1404\n772115461\n", "26\n52\n52\n1404\n316452997\n"]}
504
343
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. Princess Rupsa saw one of her friends playing a special game. The game goes as follows: N+1 numbers occur sequentially (one at a time) from A_{0} to A_{N}. You must write the numbers on a sheet of paper, such that A_{0} is written first. The other numbers are written according to an inductive rule β€” after A_{i-1} numbers have been written in a row, then A_{i} can be written at either end of the row. That is, you first write A_{0}, and then A_{1} can be written on its left or right to make A_{0}A_{1} or A_{1}A_{0}, and so on. A_{i} must be written before writing A_{j}, for every i < j. For a move in which you write a number A_{i} (i>0), your points increase by the product of A_{i} and its neighbour. (Note that for any move it will have only one neighbour as you write the number at an end). Total score of a game is the score you attain after placing all the N + 1 numbers. Princess Rupsa wants to find out the sum of scores obtained by all possible different gameplays. Two gameplays are different, if after writing down all N + 1 numbers, when we read from left to right, there exists some position i, at which the gameplays have a_{j} and a_{k} written at the i^{th} position such that j β‰  k. But since she has recently found her true love, a frog Prince, and is in a hurry to meet him, you must help her solve the problem as fast as possible. Since the answer can be very large, print the answer modulo 10^{9} + 7. ------ Input ------ The first line of the input contains an integer T denoting the number of test cases. The first line of each test case contains a single integer N. The second line contains N + 1 space-separated integers denoting A_{0} to A_{N}. ------ Output ------ For each test case, output a single line containing an integer denoting the answer. ------ Constraints ------ 1 ≀ T ≀ 10 1 ≀ N ≀ 10^{5} 1 ≀ A_{i} ≀ 10^{9} ------ Sub tasks ------ Subtask #1: 1 ≀ N ≀ 10 (10 points) Subtask #2: 1 ≀ N ≀ 1000 (20 points) Subtask #3: Original Constraints (70 points) ----- Sample Input 1 ------ 2 1 1 2 2 1 2 1 ----- Sample Output 1 ------ 4 14 ----- explanation 1 ------ There are 2 possible gameplays. A0A1 which gives score of 2 and A1A0 which also gives score of 2. So the answer is 2 + 2 = 4
{"inputs": ["2\n1\n1 2\n2\n1 2 1", "2\n1\n1 2\n2\n2 2 1", "2\n1\n1 2\n2\n2 2 0", "2\n1\n1 0\n2\n2 2 0", "2\n1\n1 0\n2\n3 2 0", "2\n1\n1 2\n2\n1 2 2", "2\n1\n1 2\n2\n2 3 1", "2\n1\n1 2\n2\n2 1 0"], "outputs": ["4\n14", "4\n24\n", "4\n16\n", "0\n16\n", "0\n24\n", "4\n20\n", "4\n34\n", "4\n8\n"]}
667
196
coding
Solve the programming task below in a Python markdown code block. Alexandra has a paper strip with n numbers on it. Let's call them a_{i} from left to right. Now Alexandra wants to split it into some pieces (possibly 1). For each piece of strip, it must satisfy: Each piece should contain at least l numbers. The difference between the maximal and the minimal number on the piece should be at most s. Please help Alexandra to find the minimal number of pieces meeting the condition above. -----Input----- The first line contains three space-separated integers n, s, l (1 ≀ n ≀ 10^5, 0 ≀ s ≀ 10^9, 1 ≀ l ≀ 10^5). The second line contains n integers a_{i} separated by spaces ( - 10^9 ≀ a_{i} ≀ 10^9). -----Output----- Output the minimal number of strip pieces. If there are no ways to split the strip, output -1. -----Examples----- Input 7 2 2 1 3 1 2 4 1 2 Output 3 Input 7 2 2 1 100 1 100 1 100 1 Output -1 -----Note----- For the first sample, we can split the strip into 3 pieces: [1, 3, 1], [2, 4], [1, 2]. For the second sample, we can't let 1 and 100 be on the same piece, so no solution exists.
{"inputs": ["1 0 1\n0\n", "1 0 1\n0\n", "1 0 1\n0\n", "1 100 2\n42\n", "1 100 2\n42\n", "1 100 2\n14\n", "1 100 2\n40\n", "1 0 1\n-98876039\n"], "outputs": ["1\n", "1\n", "1", "-1\n", "-1", "-1\n", "-1\n", "1\n"]}
338
136
coding
Solve the programming task below in a Python markdown code block. When Chef was born, his parents took him to the famous monk Doctor Strange to know whether he will land himself in heaven after his life or not. According to Strange, Chef will live for $L$ years in total. If he wants to go to heaven, he must spend at least $50\%$ of his life years doing good deeds. He also shows them his future using a string $S$ of length $L$ where $S_{i} = 0$ means the $i$-th year will be counted as bad as per the rule books of heaven and $S_{i} = 1$ means the $i$-th year will be counted as good. Also, Strange can use his special powers to make Chef end his life earlier than that planned by god, i.e, he can choose some $L'$ ($1≀ L'≀ L$) and make him live for only $L' $ years. Strange wants Chef to succeed, so if there is any choice of $L'$ that allows Chef to go to heaven, he will do so. Tell whether Chef can go to heaven. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. Each test case contains two lines of input. The first line contains a single integer $L$. The second line contains a string $S$ of length $L$, consisting of symbols 0 and 1. ------ Output ------ For each test case, output the answer in a single line: "YES" if Chef can go to heaven and "NO" if not (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≀ L ≀ 10^{5}$ The sum of $L$ over all tests does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 3 2 10 3 001 4 0100 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Test case 1: If Chef lives for the complete $2$ years, he will have a total of $1$ good year which is $\frac{1 * 100}{2} = 50\%$ of his life, and hence he will go to heaven. Test case 2: There's no way Chef can go to heaven. Test case 3: If Chef lives for $2$ years, he will have a total of $1$ good year which is $\frac{1 * 100}{2} = 50\%$ of his life, and hence he will go to heaven.
{"inputs": ["3\n2\n10\n3\n001\n4\n0100"], "outputs": ["YES\nNO\nYES"]}
622
34
coding
Solve the programming task below in a Python markdown code block. The score of an array $v_1,v_2,\ldots,v_n$ is defined as the number of indices $i$ ($1 \le i \le n$) such that $v_1+v_2+\ldots+v_i = 0$. You are given an array $a_1,a_2,\ldots,a_n$ of length $n$. You can perform the following operation multiple times: select an index $i$ ($1 \le i \le n$) such that $a_i=0$; then replace $a_i$ by an arbitrary integer. What is the maximum possible score of $a$ that can be obtained by performing a sequence of such operations? -----Input----- Each test contains multiple test cases. 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 one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the length of the array $a$. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($-10^9 \le a_i \le 10^9$) β€” array $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the maximum possible score of the array $a$ after performing a sequence of operations. -----Examples----- Input 5 5 2 0 1 -1 0 3 1000000000 1000000000 0 4 0 0 0 0 8 3 0 2 -10 10 -30 30 0 9 1 0 0 1 -1 0 1 0 -1 Output 3 1 4 4 5 -----Note----- In the first test case, it is optimal to change the value of $a_2$ to $-2$ in one operation. The resulting array $a$ will be $[2,-2,1,-1,0]$, with a score of $3$: $a_1+a_2=2-2=0$; $a_1+a_2+a_3+a_4=2-2+1-1=0$; $a_1+a_2+a_3+a_4+a_5=2-2+1-1+0=0$. In the second test case, it is optimal to change the value of $a_3$ to $-2000000000$, giving us an array with a score of $1$. In the third test case, it is not necessary to perform any operations.
{"inputs": ["5\n5\n2 0 1 -1 0\n3\n1000000000 1000000000 0\n4\n0 0 0 0\n8\n3 0 2 -10 10 -30 30 0\n9\n1 0 0 1 -1 0 1 0 -1\n"], "outputs": ["3\n1\n4\n4\n5\n"]}
642
112
coding
Solve the programming task below in a Python markdown code block. You are given four integer values $a$, $b$, $c$ and $m$. Check if there exists a string that contains: $a$ letters 'A'; $b$ letters 'B'; $c$ letters 'C'; no other letters; exactly $m$ pairs of adjacent equal letters (exactly $m$ such positions $i$ that the $i$-th letter is equal to the $(i+1)$-th one). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. Each of the next $t$ lines contains the description of the testcase β€” four integers $a$, $b$, $c$ and $m$ ($1 \le a, b, c \le 10^8$; $0 \le m \le 10^8$). -----Output----- For each testcase print "YES" if there exists a string that satisfies all the requirements. Print "NO" if there are no such strings. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). -----Examples----- Input 3 2 2 1 0 1 1 1 1 1 2 3 2 Output YES NO YES -----Note----- In the first testcase strings "ABCAB" or "BCABA" satisfy the requirements. There exist other possible strings. In the second testcase there's no way to put adjacent equal letters if there's no letter that appears at least twice. In the third testcase string "CABBCC" satisfies the requirements. There exist other possible strings.
{"inputs": ["1\n2 1 4 4\n", "1\n1 1 4 4\n", "1\n1 1 1 46\n", "1\n1 1 1 46\n", "1\n1 1 2 46\n", "1\n1 1 2 55\n", "1\n1 1 3 55\n", "1\n1 2 3 55\n"], "outputs": ["YES\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n"]}
378
140
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. Note:Β A leaf is a node with no children. Β  Please complete the following python code precisely: ```python # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def minDepth(self, root: Optional[TreeNode]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(root = tree_node([3,9,20,None,None,15,7])) == 2\n assert candidate(root = tree_node([2,None,3,None,4,None,5,None,6])) == 5\n\n\ncheck(Solution().minDepth)"}
145
71
coding
Solve the programming task below in a Python markdown code block. This is an easier version of the problem. In this version $n \le 1000$ The outskirts of the capital are being actively built up in Berland. The company "Kernel Panic" manages the construction of a residential complex of skyscrapers in New Berlskva. All skyscrapers are built along the highway. It is known that the company has already bought $n$ plots along the highway and is preparing to build $n$ skyscrapers, one skyscraper per plot. Architects must consider several requirements when planning a skyscraper. Firstly, since the land on each plot has different properties, each skyscraper has a limit on the largest number of floors it can have. Secondly, according to the design code of the city, it is unacceptable for a skyscraper to simultaneously have higher skyscrapers both to the left and to the right of it. Formally, let's number the plots from $1$ to $n$. Then if the skyscraper on the $i$-th plot has $a_i$ floors, it must hold that $a_i$ is at most $m_i$ ($1 \le a_i \le m_i$). Also there mustn't be integers $j$ and $k$ such that $j < i < k$ and $a_j > a_i < a_k$. Plots $j$ and $k$ are not required to be adjacent to $i$. The company wants the total number of floors in the built skyscrapers to be as large as possible. Help it to choose the number of floors for each skyscraper in an optimal way, i.e. in such a way that all requirements are fulfilled, and among all such construction plans choose any plan with the maximum possible total number of floors. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 1000$)Β β€” the number of plots. The second line contains the integers $m_1, m_2, \ldots, m_n$ ($1 \leq m_i \leq 10^9$)Β β€” the limit on the number of floors for every possible number of floors for a skyscraper on each plot. -----Output----- Print $n$ integers $a_i$Β β€” the number of floors in the plan for each skyscraper, such that all requirements are met, and the total number of floors in all skyscrapers is the maximum possible. If there are multiple answers possible, print any of them. -----Examples----- Input 5 1 2 3 2 1 Output 1 2 3 2 1 Input 3 10 6 8 Output 10 6 6 -----Note----- In the first example, you can build all skyscrapers with the highest possible height. In the second test example, you cannot give the maximum height to all skyscrapers as this violates the design code restriction. The answer $[10, 6, 6]$ is optimal. Note that the answer of $[6, 6, 8]$ also satisfies all restrictions, but is not optimal.
{"inputs": ["1\n1\n", "1\n1\n", "1\n2\n", "3\n4 3 8\n", "3\n10 6 8\n", "3\n10 3 8\n", "3\n4 3 16\n", "3\n4 6 16\n"], "outputs": ["1 \n", "1\n", "2\n", "3 3 8\n", "10 6 6 \n", "10 3 3\n", "3 3 16\n", "4 6 16\n"]}
675
136
coding
Solve the programming task below in a Python markdown code block. You are given a valid XML document, and you have to print the maximum level of nesting in it. Take the depth of the root as $\mbox{0}$. Input Format The first line contains $N$, the number of lines in the XML document. The next $N$ lines follow containing the XML document. Output Format Output a single line, the integer value of the maximum level of nesting in the XML document. Sample Input 6 <feed xml:lang='en'> <title>HackerRank</title> <subtitle lang='en'>Programming challenges</subtitle> <link rel='alternate' type='text/html' href='http://hackerrank.com/'/> <updated>2013-12-25T12:00:00</updated> </feed> Sample Output 1 Explanation Here, the root is a feed tag, which has depth of $\mbox{0}$. The tags title, subtitle, link and updated all have a depth of $\mbox{1}$. Thus, the maximum depth is $\mbox{1}$.
{"inputs": ["6\n<feed xml:lang='en'>\n <title>HackerRank</title>\n <subtitle lang='en'>Programming challenges</subtitle>\n <link rel='alternate' type='text/html' href='http://hackerrank.com/'/>\n <updated>2013-12-25T12:00:00</updated>\n</feed>\n"], "outputs": ["1\n"]}
250
101
coding
Solve the programming task below in a Python markdown code block. Chef Ada has a matrix with $N$ rows (numbered $1$ through $N$ from top to bottom) and $N$ columns (numbered $1$ through $N$ from left to right) containing all integers between $1$ and $N^2$ inclusive. For each valid $i$ and $j$, let's denote the cell in the $i$-th row and $j$-th column by $(i,j)$. Ada wants to sort the matrix in row-major order ― for each valid $i$ and $j$, she wants the cell $(i, j)$ to contain the value $(i-1) \cdot N + j$. In one operation, Ada should choose an integer $L$ and transpose the submatrix between rows $1$ and $L$ and columns $1$ and $L$ (inclusive). Formally, for each $i$ and $j$ ($1 \le i, j \le L$), the value in the cell $(i, j)$ after this operation is equal to the value in $(j, i)$ before it. The initial state of the matrix is chosen in such a way that it is possible to sort it using a finite number of operations (possibly zero). Find the smallest number of operations required to sort the matrix. -----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 next $N$ lines describe the matrix. For each valid $i$, the $i$-th of these lines contains $N$ space-separated integers ― the initial values in cells $(i, 1), (i, 2), \ldots, (i, N)$. -----Output----- For each test case, print a single line containing one integer ― the smallest number of operations required to sort the matrix. -----Constraints----- - $4 \le N \le 64$ - the sum of $N^2$ over all test files does not exceed $3 \cdot 10^5$ -----Subtasks----- Subtask #1 (10 points): - $T \le 50$ - $N = 4$ Subtask #2 (90 points): original constraints -----Example Input----- 1 4 1 2 9 13 5 6 10 14 3 7 11 15 4 8 12 16 -----Example Output----- 2 -----Explanation----- Example case 1: After the first operation, with $L = 2$, the resulting matrix is 1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16 After the second operation, with $L = 4$, the matrix becomes sorted 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
{"inputs": ["1\n4\n1 2 9 13\n5 6 10 14\n3 7 11 15\n4 8 12 16"], "outputs": ["2"]}
678
53
coding
Solve the programming task below in a Python markdown code block. JJ has an array A of length N and an integer X. JJ can perform the following operation at most once: Select a [subsequence] of A and add X to all the elements of that subsequence. For example, if A = [2, 1, 6, 3, 5] and X = 7, we can select the subsequence [2, 3, 5] and add X to all the elements. Now the array A becomes [2 + 7, 1, 6, 3 + 7, 5 + 7] = [9, 1, 6, 10, 12]. JJ wants to maximize the value of \displaystyle \sum_{i = 2}^{N} (A_{i - 1} \oplus A_{i}). Can you help him to do so? Here, \oplus denotes the [bitwise XOR operation]. & & kya & ------ Input Format ------ - The first line contains a single integer T β€” the number of test cases. Then the test cases follow. - The first line of each test case contains two space-separated integers N and X β€” the size of the array A and the parameter X mentioned in the statement. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the maximum value of \displaystyle \sum_{i = 2}^{n} (A_{i - 1} \oplus A_{i}) which can be obtained after applying the given operation at most once. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ N ≀ 10^{5}$ $1 ≀X ≀10^{9}$ $1 ≀A_{i} ≀10^{9}$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 1 1 2 4 1 2 2 3 3 5 2 5 3 6 2 8 ----- Sample Output 1 ------ 3 15 43 ----- explanation 1 ------ Test case $1$: It is optimal to not perform the given operation. So the answer will equal $1 \oplus 2 = 3$. Test case $2$: It is optimal to add $X = 1$ to the $2^{nd}$ and the $3^{rd}$ element. So $A$ will become $[2, 3, 4, 3]$ and the answer will be $(2 \oplus 3) + (3 \oplus 4) + (4 \oplus 3) = 15$.
{"inputs": ["3\n2 1\n1 2\n4 1\n2 2 3 3\n5 2\n5 3 6 2 8\n"], "outputs": ["3\n15\n43\n"]}
629
54
coding
Solve the programming task below in a Python markdown code block. Given an array of integers `a` and integers `t` and `x`, count how many elements in the array you can make equal to `t` by **increasing** / **decreasing** it by `x` (or doing nothing). *EASY!* ```python # ex 1 a = [11, 5, 3] t = 7 x = 2 count(a, t, x) # => 3 ``` - you can make 11 equal to 7 by subtracting 2 twice - you can make 5 equal to 7 by adding 2 - you can make 3 equal to 7 by adding 2 twice ```python # ex 2 a = [-4,6,8] t = -7 x = -3 count(a, t, x) # => 2 ``` ## Constraints **-10^(18) <= a[i],t,x <= 10^(18)** **3 <= |a| <= 10^(4)** Also feel free to reuse/extend the following starter code: ```python def count(a, t, x): ```
{"functional": "_inputs = [[[11, 5, 3], 7, 2], [[-4, 6, 8], -7, -3]]\n_outputs = [[3], [2]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(count(*i), o[0])"}
268
185
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an m x n grid of characters board and a string word, return true if word exists in the grid. The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once. Β  Please complete the following python code precisely: ```python class Solution: def exist(self, board: List[List[str]], word: str) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(board = [[\"A\",\"B\",\"C\",\"E\"],[\"S\",\"F\",\"C\",\"S\"],[\"A\",\"D\",\"E\",\"E\"]], word = \"ABCCED\") == True\n assert candidate(board = [[\"A\",\"B\",\"C\",\"E\"],[\"S\",\"F\",\"C\",\"S\"],[\"A\",\"D\",\"E\",\"E\"]], word = \"SEE\") == True\n assert candidate(board = [[\"A\",\"B\",\"C\",\"E\"],[\"S\",\"F\",\"C\",\"S\"],[\"A\",\"D\",\"E\",\"E\"]], word = \"ABCB\") == False\n\n\ncheck(Solution().exist)"}
108
153
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. We have two special characters: The first character can be represented by one bit 0. The second character can be represented by two bits (10 or 11). Given a binary array bits that ends with 0, return true if the last character must be a one-bit character. Β  Please complete the following python code precisely: ```python class Solution: def isOneBitCharacter(self, bits: List[int]) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(bits = [1,1,1,0]) == False\n\n\ncheck(Solution().isOneBitCharacter)"}
109
38
coding
Solve the programming task below in a Python markdown code block. Two players are playing a game on a $15\times15$ chessboard. The rules of the game are as follows: The game starts with $\boldsymbol{\mbox{k}}$ coins located at one or more $(x,y)$ coordinates on the board (a single cell may contain more than one coin). The coordinate of the upper left cell is $(1,1)$, and the coordinate of the lower right cell is $(15,15)$. In each move, a player must move a single coin from some cell $(x,y)$ to one of the following locations: $(x-2,y+1)$ $(x-2,y-1)$ $(x+1,y-2)$ $(x-1,y-2)$. Note: The coin must remain inside the confines of the board. The players move in alternating turns. The first player who is unable to make a move loses the game. The figure below shows all four possible moves: Note: While the figure shows a $8\times8$ board, this game is played on a $15\times15$ board. Given the value of $\boldsymbol{\mbox{k}}$ and the initial coordinate(s) of $\boldsymbol{\mbox{k}}$ coins, determine which player will win the game. Assume both players always move optimally. Input Format The first line contains an integer, $\mathbf{T}$, denoting the number of test cases. Each test case is defined as follows over the subsequent lines: The first line contains an integer, $\boldsymbol{\mbox{k}}$, denoting the number of coins on the board. Each line $\boldsymbol{i}$ (where $0\leq i<k$) of the $\boldsymbol{\mbox{k}}$ subsequent lines contains $2$ space-separated integers describing the respective values of $x_i$ and $y_i$ of the coordinate where coin $k_i$ is located. Note: Recall that a cell can have more than one coin (i.e., any cell can have $0$ to $\boldsymbol{\mbox{k}}$ coins in it at any given time). Constraints $1\leq T\leq1000$ $1\leq k\leq1000$ $1\leq x_i,y_i\leq15$, where $0\leq i<k$. Output Format On a new line for each test case, print $\textbf{First}$ if the first player is the winner; otherwise, print $\textbf{Second}$. Sample Input 2 3 5 4 5 8 8 2 6 7 1 7 2 7 3 7 4 7 4 7 4 Sample Output First Second
{"inputs": ["2\n3\n5 4\n5 8\n8 2\n6\n7 1\n7 2\n7 3\n7 4\n7 4\n7 4\n"], "outputs": ["First\nSecond\n"]}
604
56
coding
Solve the programming task below in a Python markdown code block. Can the greatest common divisor and bitwise operations have anything in common? It is time to answer this question. Suppose you are given a positive integer $a$. You want to choose some integer $b$ from $1$ to $a - 1$ inclusive in such a way that the greatest common divisor (GCD) of integers $a \oplus b$ and $a \> \& \> b$ is as large as possible. In other words, you'd like to compute the following function: $$f(a) = \max_{0 < b < a}{gcd(a \oplus b, a \> \& \> b)}.$$ Here $\oplus$ denotes the bitwise XOR operation, and $\&$ denotes the bitwise AND operation. The greatest common divisor of two integers $x$ and $y$ is the largest integer $g$ such that both $x$ and $y$ are divided by $g$ without remainder. You are given $q$ integers $a_1, a_2, \ldots, a_q$. For each of these integers compute the largest possible value of the greatest common divisor (when $b$ is chosen optimally). -----Input----- The first line contains an integer $q$ ($1 \le q \le 10^3$)Β β€” the number of integers you need to compute the answer for. After that $q$ integers are given, one per line: $a_1, a_2, \ldots, a_q$ ($2 \le a_i \le 2^{25} - 1$)Β β€” the integers you need to compute the answer for. -----Output----- For each integer, print the answer in the same order as the integers are given in input. -----Example----- Input 3 2 3 5 Output 3 1 7 -----Note----- For the first integer the optimal choice is $b = 1$, then $a \oplus b = 3$, $a \> \& \> b = 0$, and the greatest common divisor of $3$ and $0$ is $3$. For the second integer one optimal choice is $b = 2$, then $a \oplus b = 1$, $a \> \& \> b = 2$, and the greatest common divisor of $1$ and $2$ is $1$. For the third integer the optimal choice is $b = 2$, then $a \oplus b = 7$, $a \> \& \> b = 0$, and the greatest common divisor of $7$ and $0$ is $7$.
{"inputs": ["1\n4\n", "1\n7\n", "1\n2\n", "1\n9\n", "1\n8\n", "1\n6\n", "1\n3\n", "1\n5\n"], "outputs": ["7\n", "1\n", "3\n", "15\n", "15\n", "7\n", "1\n", "7\n"]}
574
88
coding
Solve the programming task below in a Python markdown code block. You are given an integer array $a_1, a_2, \ldots, a_n$. The array $b$ is called to be a subsequence of $a$ if it is possible to remove some elements from $a$ to get $b$. Array $b_1, b_2, \ldots, b_k$ is called to be good if it is not empty and for every $i$ ($1 \le i \le k$) $b_i$ is divisible by $i$. Find the number of good subsequences in $a$ modulo $10^9 + 7$. Two subsequences are considered different if index sets of numbers included in them are different. That is, the values ​of the elements ​do not matter in the comparison of subsequences. In particular, the array $a$ has exactly $2^n - 1$ different subsequences (excluding an empty subsequence). -----Input----- The first line contains an integer $n$ ($1 \le n \le 100\,000$)Β β€” the length of the array $a$. The next line contains integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^6$). -----Output----- Print exactly one integerΒ β€” the number of good subsequences taken modulo $10^9 + 7$. -----Examples----- Input 2 1 2 Output 3 Input 5 2 2 1 22 14 Output 13 -----Note----- In the first example, all three non-empty possible subsequences are good: $\{1\}$, $\{1, 2\}$, $\{2\}$ In the second example, the possible good subsequences are: $\{2\}$, $\{2, 2\}$, $\{2, 22\}$, $\{2, 14\}$, $\{2\}$, $\{2, 22\}$, $\{2, 14\}$, $\{1\}$, $\{1, 22\}$, $\{1, 14\}$, $\{22\}$, $\{22, 14\}$, $\{14\}$. Note, that some subsequences are listed more than once, since they occur in the original array multiple times.
{"inputs": ["1\n7\n", "1\n32\n", "2\n1 2\n", "1\n688\n", "2\n1 2\n", "1\n1469\n", "1\n2185\n", "1\n89751\n"], "outputs": ["1\n", "1\n", "3", "1\n", "3\n", "1\n", "1\n", "1\n"]}
535
102
coding
Solve the programming task below in a Python markdown code block. Connect the countless points with lines, till we reach the faraway yonder. There are n points on a coordinate plane, the i-th of which being (i, y_{i}). Determine whether it's possible to draw two parallel and non-overlapping lines, such that every point in the set lies on exactly one of them, and each of them passes through at least one point in the set. -----Input----- The first line of input contains a positive integer n (3 ≀ n ≀ 1 000) β€” the number of points. The second line contains n space-separated integers y_1, y_2, ..., y_{n} ( - 10^9 ≀ y_{i} ≀ 10^9) β€” the vertical coordinates of each point. -----Output----- Output "Yes" (without quotes) if it's possible to fulfill the requirements, and "No" otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 5 7 5 8 6 9 Output Yes Input 5 -1 -2 0 0 -5 Output No Input 5 5 4 3 2 1 Output No Input 5 1000000000 0 0 0 0 Output Yes -----Note----- In the first example, there are five points: (1, 7), (2, 5), (3, 8), (4, 6) and (5, 9). It's possible to draw a line that passes through points 1, 3, 5, and another one that passes through points 2, 4 and is parallel to the first one. In the second example, while it's possible to draw two lines that cover all points, they cannot be made parallel. In the third example, it's impossible to satisfy both requirements at the same time.
{"inputs": ["4\n1 0 3 0\n", "4\n1 0 1 4\n", "4\n0 0 4 0\n", "4\n0 0 1 3\n", "4\n1 3 4 4\n", "4\n2 2 4 5\n", "4\n1 3 3 6\n", "4\n1 3 4 4\n"], "outputs": ["No\n", "Yes\n", "Yes\n", "Yes\n", "Yes\n", "Yes\n", "No\n", "Yes\n"]}
424
134
coding
Solve the programming task below in a Python markdown code block. # Longest Palindromic Substring (Linear) A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g., 'madam' or 'racecar'. Even the letter 'x' is considered a palindrome. For this Kata, you are given a string ```s```. Write a function that returns the longest _contiguous_ palindromic substring in ```s``` (it could be the entire string). In the event that there are multiple longest palindromic substrings, return the first to occur. I'm not trying to trick you here: - You can assume that all inputs are valid strings. - Only the letters a-z will be used, all lowercase (your solution should, in theory, extend to more than just the letters a-z though). **NOTE:** Quadratic asymptotic complexity _(O(N^2))_ or above will __NOT__ work here. ----- ## Examples ### Basic Tests ``` Input: "babad" Output: "bab" (Note: "bab" occurs before "aba") ``` ``` Input: "abababa" Output: "abababa" ``` ``` Input: "cbbd" Output: "bb" ``` ### Edge Cases ``` Input: "ab" Output: "a" ``` ``` Input: "" Output: "" ``` ----- ## Testing Along with the example tests given: - There are **500** tests using strings of length in range [1 - 1,000] - There are **50** tests using strings of length in range [1,000 - 10,000] - There are **5** tests using strings of length in range [10,000 - 100,000] All test cases can be passed within 10 seconds, but non-linear solutions will time out every time. _Linear performance is essential_. ## Good Luck! ----- This problem was inspired by [this](https://leetcode.com/problems/longest-palindromic-substring/) challenge on LeetCode. Except this is the performance version :^) Also feel free to reuse/extend the following starter code: ```python def longest_palindrome(s): ```
{"functional": "_inputs = [['babad'], ['madam'], ['dde'], ['ababbab'], ['abababa'], ['banana'], ['abba'], ['cbbd'], ['zz'], ['dddd'], [''], ['abcdefghijklmnopqrstuvwxyz'], ['ttaaftffftfaafatf'], ['bbaaacc'], ['m']]\n_outputs = [['bab'], ['madam'], ['dd'], ['babbab'], ['abababa'], ['anana'], ['abba'], ['bb'], ['zz'], ['dddd'], [''], ['a'], ['aaftffftfaa'], ['aaa'], ['m']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(longest_palindrome(*i), o[0])"}
489
271
coding
Solve the programming task below in a Python markdown code block. Your car is old, it breaks easily. The shock absorbers are gone and you think it can handle about 15 more bumps before it dies totally. Unfortunately for you, your drive is very bumpy! Given a string showing either flat road ("\_") or bumps ("n"), work out if you make it home safely. 15 bumps or under, return "Woohoo!", over 15 bumps return "Car Dead". Also feel free to reuse/extend the following starter code: ```python def bumps(road): ```
{"functional": "_inputs = [['n'], ['_nnnnnnn_n__n______nn__nn_nnn'], ['______n___n_'], ['nnnnnnnnnnnnnnnnnnnnn']]\n_outputs = [['Woohoo!'], ['Car Dead'], ['Woohoo!'], ['Car Dead']]\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(bumps(*i), o[0])"}
124
209
coding
Solve the programming task below in a Python markdown code block. White Falcon just solved the data structure problem below using heavy-light decomposition. Can you help her find a new solution that doesn't require implementing any fancy techniques? There are $2$ types of query operations that can be performed on a tree: 1 u x: Assign $\boldsymbol{x}$ as the value of node $\mbox{u}$. 2 u v: Print the sum of the node values in the unique path from node $\mbox{u}$ to node $\boldsymbol{\nu}$. Given a tree with $N$ nodes where each node's value is initially $0$, execute $Q$ queries. Input Format The first line contains $2$ space-separated integers, $N$ and $Q$, respectively. The $N-1$ subsequent lines each contain $2$ space-separated integers describing an undirected edge in the tree. Each of the $Q$ subsequent lines contains a query you must execute. Constraints $1\leq N,Q\leq10^5$ $1\leq x\leq1000$ It is guaranteed that the input describes a connected tree with $N$ nodes. Nodes are enumerated with $0$-based indexing. Output Format For each type-$2$ query, print its integer result on a new line. Sample Input 3 3 0 1 1 2 1 0 1 1 1 2 2 0 2 Sample Output 3 Explanation After the first $2$ queries, the value of node $n_0=1$ and the value of node $n_1=2$. The third query requires us to print the sum of the node values in the path from nodes $0$ to $2$, which is $1+2+0=3$. Thus, we print $3$ on a new line.
{"inputs": ["3 3\n0 1\n1 2\n1 0 1\n1 1 2\n2 0 2\n"], "outputs": ["3\n"]}
404
42
coding
Solve the programming task below in a Python markdown code block. Unary is a minimalistic Brainfuck dialect in which programs are written using only one token. Brainfuck programs use 8 commands: "+", "-", "[", "]", "<", ">", "." and "," (their meaning is not important for the purposes of this problem). Unary programs are created from Brainfuck programs using the following algorithm. First, replace each command with a corresponding binary code, using the following conversion table: * ">" β†’ 1000, * "<" β†’ 1001, * "+" β†’ 1010, * "-" β†’ 1011, * "." β†’ 1100, * "," β†’ 1101, * "[" β†’ 1110, * "]" β†’ 1111. Next, concatenate the resulting binary codes into one binary number in the same order as in the program. Finally, write this number using unary numeral system β€” this is the Unary program equivalent to the original Brainfuck one. You are given a Brainfuck program. Your task is to calculate the size of the equivalent Unary program, and print it modulo 1000003 (106 + 3). Input The input will consist of a single line p which gives a Brainfuck program. String p will contain between 1 and 100 characters, inclusive. Each character of p will be "+", "-", "[", "]", "<", ">", "." or ",". Output Output the size of the equivalent Unary program modulo 1000003 (106 + 3). Examples Input ,. Output 220 Input ++++[&gt;,.&lt;-] Output 61425 Note To write a number n in unary numeral system, one simply has to write 1 n times. For example, 5 written in unary system will be 11111. In the first example replacing Brainfuck commands with binary code will give us 1101 1100. After we concatenate the codes, we'll get 11011100 in binary system, or 220 in decimal. That's exactly the number of tokens in the equivalent Unary program.
{"inputs": [".\n", "+\n", ">\n", "<\n", ",\n", "-\n", "[\n", "]\n"], "outputs": ["12\n", "10\n", "8\n", "9\n", "13\n", "11\n", "14\n", "15\n"]}
509
77
coding
Solve the programming task below in a Python markdown code block. Alice, the president of club FCB, wants to build a team for the new volleyball tournament. The team should consist of p players playing in p different positions. She also recognizes the importance of audience support, so she wants to select k people as part of the audience. There are n people in Byteland. Alice needs to select exactly p players, one for each position, and exactly k members of the audience from this pool of n people. Her ultimate goal is to maximize the total strength of the club. The i-th of the n persons has an integer a_{i} associated with him β€” the strength he adds to the club if he is selected as a member of the audience. For each person i and for each position j, Alice knows s_{i, j} β€” the strength added by the i-th person to the club if he is selected to play in the j-th position. Each person can be selected at most once as a player or a member of the audience. You have to choose exactly one player for each position. Since Alice is busy, she needs you to help her find the maximum possible strength of the club that can be achieved by an optimal choice of players and the audience. Input The first line contains 3 integers n,p,k (2 ≀ n ≀ 10^{5}, 1 ≀ p ≀ 7, 1 ≀ k, p+k ≀ n). The second line contains n integers a_{1},a_{2},…,a_{n}. (1 ≀ a_{i} ≀ 10^{9}). The i-th of the next n lines contains p integers s_{i, 1}, s_{i, 2}, ..., s_{i, p}. (1 ≀ s_{i,j} ≀ 10^{9}) Output Print a single integer {res} β€” the maximum possible strength of the club. Examples Input 4 1 2 1 16 10 3 18 19 13 15 Output 44 Input 6 2 3 78 93 9 17 13 78 80 97 30 52 26 17 56 68 60 36 84 55 Output 377 Input 3 2 1 500 498 564 100002 3 422332 2 232323 1 Output 422899 Note In the first sample, we can select person 1 to play in the 1-st position and persons 2 and 3 as audience members. Then the total strength of the club will be equal to a_{2}+a_{3}+s_{1,1}.
{"inputs": ["2 1 1\n30 9\n46\n73\n", "2 1 1\n30 9\n46\n87\n", "2 1 1\n76 91\n34\n77\n", "2 1 1\n76 146\n34\n77\n", "2 1 1\n76 291\n34\n77\n", "2 1 1\n76 291\n35\n77\n", "2 1 1\n131 291\n35\n77\n", "4 1 2\n2 4 10 3\n18\n19\n23\n3\n"], "outputs": ["103", "117\n", "153", "180\n", "325\n", "326\n", "326\n", "32\n"]}
617
222
coding
Solve the programming task below in a Python markdown code block. Vasya likes everything infinite. Now he is studying the properties of a sequence s, such that its first element is equal to a (s_1 = a), and the difference between any two neighbouring elements is equal to c (s_{i} - s_{i} - 1 = c). In particular, Vasya wonders if his favourite integer b appears in this sequence, that is, there exists a positive integer i, such that s_{i} = b. Of course, you are the person he asks for a help. -----Input----- The first line of the input contain three integers a, b and c ( - 10^9 ≀ a, b, c ≀ 10^9)Β β€” the first element of the sequence, Vasya's favorite number and the difference between any two neighbouring elements of the sequence, respectively. -----Output----- If b appears in the sequence s print "YES" (without quotes), otherwise print "NO" (without quotes). -----Examples----- Input 1 7 3 Output YES Input 10 10 0 Output YES Input 1 -4 5 Output NO Input 0 60 50 Output NO -----Note----- In the first sample, the sequence starts from integers 1, 4, 7, so 7 is its element. In the second sample, the favorite integer of Vasya is equal to the first element of the sequence. In the third sample all elements of the sequence are greater than Vasya's favorite integer. In the fourth sample, the sequence starts from 0, 50, 100, and all the following elements are greater than Vasya's favorite integer.
{"inputs": ["1 7 3\n", "0 1 0\n", "0 0 0\n", "0 0 1\n", "0 0 2\n", "0 1 0\n", "0 1 1\n", "0 1 2\n"], "outputs": ["YES\n", "NO\n", "YES\n", "YES\n", "YES\n", "NO\n", "YES\n", "NO\n"]}
378
102
coding
Solve the programming task below in a Python markdown code block. This is a story of a world somewhere far from the earth. In this world, the land is parted into a number of countries ruled by empires. This world is not very peaceful: they have been involved in army race. They are competing in production of missiles in particular. Nevertheless, no countries have started wars for years. Actually they have a reason they can’t get into wars - they have missiles much more than enough to destroy the entire world. Once a war would begin among countries, none of them could remain. These missiles have given nothing but scare to people. The competition has caused big financial and psychological pressure to countries. People have been tired. Military have been tired. Even empires have been tired. No one wishes to keep on missile production. So empires and diplomats of all countries held meetings quite a few times toward renouncement of missiles and abandon of further production. The meetings were quite difficult as they have different matters. However, they overcame such difficulties and finally came to the agreement of a treaty. The points include: * Each country will dispose all the missiles of their possession by a certain date. * The war potential should not be different by greater than a certain amount d among all countries. Let us describe more details on the second point. Each missile has its capability, which represents how much it can destroy the target. The war potential of each country is measured simply by the sum of capability over missiles possessed by that country. The treaty requires the difference to be not greater than d between the maximum and minimum potential of all the countries. Unfortunately, it is not clear whether this treaty is feasible. Every country is going to dispose their missiles only in the order of time they were produced, from the oldest to the newest. Some missiles have huge capability, and disposal of them may cause unbalance in potential. Your task is to write a program to see this feasibility. Input The input is a sequence of datasets. Each dataset is given in the following format: n d m1 c1,1 ... c1,m1 ... mn cn,1 ... cn,mn The first line contains two positive integers n and d, the number of countries and the tolerated difference of potential (n ≀ 100, d ≀ 1000). Then n lines follow. The i-th line begins with a non-negative integer mi, the number of the missiles possessed by the i-th country. It is followed by a sequence of mi positive integers. The j-th integer ci,j represents the capability of the j-th newest missile of the i-th country (ci,j ≀ 1000). These integers are separated by a single space. Note that the country disposes their missiles in the reverse order of the given sequence. The number of missiles is not greater than 10000. Also, you may assume the difference between the maximum and minimum potential does not exceed d in any dataset. The input is terminated by a line with two zeros. This line should not be processed. Output For each dataset, print a single line. If they can dispose all their missiles according to the treaty, print "Yes" (without quotes). Otherwise, print "No". Note that the judge is performed in a case-sensitive manner. No extra space or character is allowed. Example Input 3 3 3 4 1 1 2 1 5 2 3 3 3 3 3 2 3 1 2 1 5 2 3 3 0 0 Output Yes No
{"inputs": ["3 3\n3 8 1 1\n2 1 5\n2 3 3\n3 3\n3 2 3 1\n2 1 5\n2 3 3\n0 0", "3 6\n3 8 1 1\n2 1 5\n2 3 3\n3 3\n3 2 5 1\n2 1 8\n2 3 3\n0 0", "3 3\n3 4 1 1\n2 1 5\n2 3 3\n3 6\n3 2 5 1\n2 1 5\n2 3 3\n0 0", "3 3\n3 8 1 1\n2 1 6\n2 3 3\n3 3\n3 2 3 1\n2 2 5\n2 2 3\n0 0", "3 3\n3 8 1 1\n2 1 5\n2 3 3\n3 3\n3 2 5 1\n2 1 5\n2 3 3\n0 0", "3 3\n3 8 1 1\n2 1 5\n2 3 3\n3 3\n3 2 5 1\n2 1 8\n2 3 3\n0 0", "3 6\n3 8 1 1\n2 1 6\n2 3 3\n3 3\n3 2 5 1\n2 1 8\n2 3 3\n0 0", "3 3\n3 8 1 1\n2 1 5\n2 4 3\n3 3\n3 2 3 1\n2 1 5\n2 3 3\n0 0"], "outputs": ["No\nNo\n", "Yes\nNo\n", "Yes\nYes\n", "No\nYes\n", "No\nNo\n", "No\nNo\n", "No\nNo\n", "No\nNo\n"]}
742
478
coding
Solve the programming task below in a Python markdown code block. Read problems statements in [Russian] and [Bengali]. There's a zebra crossing appearing in the middle of nowhere with N blocks in it. The colors of the zebra crossing is represented by a binary string S, where S_{i} is 1 if the i-th block from the left is white, and 0 if the block is black. Chef really wants to play with the zebra crossing. Although the given zebra crossing might not have alternate black and white blocks, Chef wants to follow the alternating white-black color pattern while crossing it. Initially, Chef stands at block 1. Chef has to jump exactly K times, and in each jump he has to move forward and jump to a different color than that previously occupied by Chef. More formally, suppose that Chef is currently at block i and wants to jump to block j then following conditions should hold: i < j S_{i} \neq S_{j} Output the farthest block Chef can reach with exactly K jumps. If Chef cannot jump exactly K times, output -1. ------ Input Format ------ - The first line contains an integer T denoting the number of test cases. The T test cases then follow. - The first line of each test case contains two integers N and K. - The second line of each test case consists of a binary string of length N denoting the color of blocks of the zebra crossing. ------ Output Format ------ For each test case, output the farthest block Chef can reach with exactly K jumps, or -1 in case Chef cannot jump exactly K times. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $2 ≀ N ≀ 10^{3}$ $1 ≀ K ≀ N$ - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$ ----- Sample Input 1 ------ 3 6 2 100101 5 1 10111 6 1 000000 ----- Sample Output 1 ------ 6 2 -1 ----- explanation 1 ------ - For the first test case, Chef can jump in the following order: $1 \rightarrow 5 \rightarrow 6$. - For the second test case, Chef can jump in the following order: $1 \rightarrow 2$. - For the third test case, Chef cannot make any jumps.
{"inputs": ["3\n6 2\n100101\n5 1\n10111\n6 1\n000000\n"], "outputs": ["6\n2\n-1\n"]}
519
51
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a 0-indexed permutation of n integers nums. A permutation is called semi-ordered if the first number equals 1 and the last number equals n. You can perform the below operation as many times as you want until you make nums a semi-ordered permutation: Pick two adjacent elements in nums, then swap them. Return the minimum number of operations to make nums a semi-ordered permutation. A permutation is a sequence of integers from 1 to n of length n containing each number exactly once. Β  Please complete the following python code precisely: ```python class Solution: def semiOrderedPermutation(self, nums: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [2,1,4,3]) == 2\n assert candidate(nums = [2,4,1,3]) == 3\n assert candidate(nums = [1,3,4,2,5]) == 0\n\n\ncheck(Solution().semiOrderedPermutation)"}
154
77
coding
Solve the programming task below in a Python markdown code block. ZS the Coder loves to read the dictionary. He thinks that a word is nice if there exists a substring (contiguous segment of letters) of it of length 26 where each letter of English alphabet appears exactly once. In particular, if the string has length strictly less than 26, no such substring exists and thus it is not nice. Now, ZS the Coder tells you a word, where some of its letters are missing as he forgot them. He wants to determine if it is possible to fill in the missing letters so that the resulting word is nice. If it is possible, he needs you to find an example of such a word as well. Can you help him? -----Input----- The first and only line of the input contains a single string s (1 ≀ |s| ≀ 50 000), the word that ZS the Coder remembers. Each character of the string is the uppercase letter of English alphabet ('A'-'Z') or is a question mark ('?'), where the question marks denotes the letters that ZS the Coder can't remember. -----Output----- If there is no way to replace all the question marks with uppercase letters such that the resulting word is nice, then print - 1 in the only line. Otherwise, print a string which denotes a possible nice word that ZS the Coder learned. This string should match the string from the input, except for the question marks replaced with uppercase English letters. If there are multiple solutions, you may print any of them. -----Examples----- Input ABC??FGHIJK???OPQR?TUVWXY? Output ABCDEFGHIJKLMNOPQRZTUVWXYS Input WELCOMETOCODEFORCESROUNDTHREEHUNDREDANDSEVENTYTWO Output -1 Input ?????????????????????????? Output MNBVCXZLKJHGFDSAQPWOEIRUYT Input AABCDEFGHIJKLMNOPQRSTUVW??M Output -1 -----Note----- In the first sample case, ABCDEFGHIJKLMNOPQRZTUVWXYS is a valid answer beacuse it contains a substring of length 26 (the whole string in this case) which contains all the letters of the English alphabet exactly once. Note that there are many possible solutions, such as ABCDEFGHIJKLMNOPQRSTUVWXYZ or ABCEDFGHIJKLMNOPQRZTUVWXYS. In the second sample case, there are no missing letters. In addition, the given string does not have a substring of length 26 that contains all the letters of the alphabet, so the answer is - 1. In the third sample case, any string of length 26 that contains all letters of the English alphabet fits as an answer.
{"inputs": ["A\n", "A\n", "B\n", "C\n", "AA\n", "AB\n", "AA\n", "AB\n"], "outputs": ["-1", "-1\n", "-1", "-1", "-1", "-1", "-1\n", "-1\n"]}
584
66
coding
Solve the programming task below in a Python markdown code block. You are given a decimal representation of an integer $x$ without leading zeros. You have to perform the following reduction on it exactly once: take two neighboring digits in $x$ and replace them with their sum without leading zeros (if the sum is $0$, it's represented as a single $0$). For example, if $x = 10057$, the possible reductions are: choose the first and the second digits $1$ and $0$, replace them with $1+0=1$; the result is $1057$; choose the second and the third digits $0$ and $0$, replace them with $0+0=0$; the result is also $1057$; choose the third and the fourth digits $0$ and $5$, replace them with $0+5=5$; the result is still $1057$; choose the fourth and the fifth digits $5$ and $7$, replace them with $5+7=12$; the result is $10012$. What's the largest number that can be obtained? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. Each testcase consists of a single integer $x$ ($10 \le x < 10^{200000}$). $x$ doesn't contain leading zeros. The total length of the decimal representations of $x$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print a single integer β€” the largest number that can be obtained after the reduction is applied exactly once. The number should not contain leading zeros. -----Examples----- Input 2 10057 90 Output 10012 9 -----Note----- The first testcase of the example is already explained in the statement. In the second testcase, there is only one possible reduction: the first and the second digits.
{"inputs": ["2\n10057\n90\n"], "outputs": ["10012\n9\n"]}
446
29
coding
Solve the programming task below in a Python markdown code block. Lot of museum allow you to be a member, for a certain amount `amount_by_year` you can have unlimitted acces to the museum. In this kata you should complete a function in order to know after how many visit it will be better to take an annual pass. The function take 2 arguments `annual_price` and `individual_price`. Also feel free to reuse/extend the following starter code: ```python def how_many_times(annual_price, individual_price): ```
{"functional": "_inputs = [[40, 15], [30, 10], [80, 15]]\n_outputs = [[3], [3], [6]]\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(how_many_times(*i), o[0])"}
112
183
coding
Solve the programming task below in a Python markdown code block. You are given an integer $n$. Let's define $s(n)$ as the string "BAN" concatenated $n$ times. For example, $s(1)$ = "BAN", $s(3)$ = "BANBANBAN". Note that the length of the string $s(n)$ is equal to $3n$. Consider $s(n)$. You can perform the following operation on $s(n)$ any number of times (possibly zero): Select any two distinct indices $i$ and $j$ $(1 \leq i, j \leq 3n, i \ne j)$. Then, swap $s(n)_i$ and $s(n)_j$. You want the string "BAN" to not appear in $s(n)$ as a subsequence. What's the smallest number of operations you have to do to achieve this? Also, find one such shortest sequence of operations. A string $a$ is a subsequence of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ $(1 \leq t \leq 100)$ β€” the number of test cases. The description of the test cases follows. The only line of each test case contains a single integer $n$ $(1 \leq n \leq 100)$. -----Output----- For each test case, in the first line output $m$ ($0 \le m \le 10^5$) β€” the minimum number of operations required. It's guaranteed that the objective is always achievable in at most $10^5$ operations under the constraints of the problem. Then, output $m$ lines. The $k$-th of these lines should contain two integers $i_k$, $j_k$ $(1\leq i_k, j_k \leq 3n, i_k \ne j_k)$ denoting that you want to swap characters at indices $i_k$ and $j_k$ at the $k$-th operation. After all $m$ operations, "BAN" must not appear in $s(n)$ as a subsequence. If there are multiple possible answers, output any. -----Examples----- Input 2 1 2 Output 1 1 2 1 2 6 -----Note----- In the first testcase, $s(1) = $ "BAN", we can swap $s(1)_1$ and $s(1)_2$, converting $s(1)$ to "ABN", which does not contain "BAN" as a subsequence. In the second testcase, $s(2) = $ "BANBAN", we can swap $s(2)_2$ and $s(2)_6$, converting $s(2)$ to "BNNBAA", which does not contain "BAN" as a subsequence.
{"inputs": ["2\n1\n2\n"], "outputs": ["1\n1 3\n1\n1 6\n"]}
655
28
coding
Solve the programming task below in a Python markdown code block. Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q_1q_2... q_{k}. The algorithm consists of two steps: Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s_1s_2... s_{n}, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring s_{l}_{i}s_{l}_{i} + 1... s_{r}_{i} (1 ≀ l_{i} ≀ r_{i} ≀ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (l_{i}, r_{i}) determine if the algorithm works correctly on this test or not. -----Input----- The first line contains non-empty string s, its length (n) doesn't exceed 10^5. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≀ m ≀ 10^5) β€” the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers l_{i}, r_{i} (1 ≀ l_{i} ≀ r_{i} ≀ n). -----Output----- For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. -----Examples----- Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO -----Note----- In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
{"inputs": ["x\n1\n1 1\n", "x\n1\n1 1\n", "x\n1\n1 0\n", "zyxxxxxxyyz\n5\n5 5\n1 3\n1 6\n1 4\n3 6\n", "zyxxxxxxyyz\n5\n5 5\n1 3\n1 11\n1 4\n3 6\n", "zyxxxxxxyyz\n5\n5 5\n1 3\n1 11\n1 4\n3 1\n", "zyxxxxxxyyz\n5\n2 5\n1 3\n1 11\n1 4\n3 1\n", "zyxxxxxxyyz\n5\n5 5\n1 3\n1 11\n1 4\n3 2\n"], "outputs": ["YES\n", "YES\n", "YES\n", "YES\nYES\nNO\nYES\nNO\n", "YES\nYES\nNO\nYES\nNO\n", "YES\nYES\nNO\nYES\nYES\n", "NO\nYES\nNO\nYES\nYES\n", "YES\nYES\nNO\nYES\nYES\n"]}
576
262
coding
Solve the programming task below in a Python markdown code block. To pass the series of gates guarded by the owls, Kenneth needs to present them each with a highly realistic portrait of one. Unfortunately, he is absolutely rubbish at drawing, and needs some code to return a brand new portrait with a moment's notice. All owl heads look like this: ''0v0'' Such beautiful eyes! However, they differ in their plumage, which is always symmetrical, eg.: VVHVAV''0v0''VAVHVV or YYAYAH8XH''0v0''HX8HAYAYY So Kenneth needs a method that will take a garble of text generated by mashing at his keyboard (numbers and letters, but he knows how to avoid punctuation etc.) for a bit and give him a symmetrical owl with a lovely little face, with a truly symmetrical plumage made of uppercase letters and numbers. (To be clear, the acceptable characters for the plumage are 8,W,T,Y,U,I,O,A,H,X,V and M.) Also feel free to reuse/extend the following starter code: ```python def owl_pic(text): ```
{"functional": "_inputs = [['xwe'], ['kuawd6r8q27y87t93r76352475437'], ['t6ggggggggWw'], ['xweWXo']]\n_outputs = [[\"XW''0v0''WX\"], [\"UAW8Y8T''0v0''T8Y8WAU\"], [\"TWW''0v0''WWT\"], [\"XWWXO''0v0''OXWWX\"]]\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(owl_pic(*i), o[0])"}
250
259
coding
Solve the programming task below in a Python markdown code block. You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends. In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like. Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all. A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself. -----Input----- The only line contains four positive integers cnt_1, cnt_2, x, y (1 ≀ cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 ≀ 10^9; 2 ≀ x < y ≀ 3Β·10^4)Β β€” the numbers that are described in the statement. It is guaranteed that numbers x, y are prime. -----Output----- Print a single integer β€” the answer to the problem. -----Examples----- Input 3 1 2 3 Output 5 Input 1 3 2 3 Output 4 -----Note----- In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend. In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
{"inputs": ["3 1 2 3\n", "1 3 2 3\n", "1 1 2 3\n", "1 2 2 3\n", "2 1 2 3\n", "2 1 2 3\n", "1 2 2 3\n", "1 1 2 3\n"], "outputs": ["5\n", "4\n", "2\n", "3\n", "3\n", "3\n", "3\n", "2\n"]}
461
118
coding
Solve the programming task below in a Python markdown code block. Famous wizard Sumar moonji kumaru is stuck in a huge room and has to save Hermione Granger from a monster. Kumaru is at location P1 given by integral coordinates (x_{1},y_{1}) and Hermione is at location P2 given by integral coordinates (x_{2},y_{2}). Sadly P1 and P2 are the only points at which floating rocks are present. Rest of the room is without floor and underneath is hot lava. Kumaru has to go from P1 to P2 but there are no floating rocks to walk on. Kumaru knows a spell that can make the rocks appear but only on the integral coordinates on the straight line joining P1 and P2. How many rocks can appear at locations (x,y) on the line segment between P1 and P2 (excluding P1 and P2) which satisfy the condition that both x and y are integers? Input Format The first line contains a single integer T, the number of test cases. T lines follow. Each of the following T lines contains one test case each. Each test case contains 4 integers x_{1}, y_{1}, x_{2} and y_{2} separated by a single space. Output Format A single line containing the number of rocks. Constraints 1 <= T <= 10^{5} -10^{9} <= x_{1}, y_{1}, x_{2}, y_{2} <= 10^{9} Sample input 3 0 2 4 0 2 2 5 5 1 9 8 16 Sample Output 1 2 6 Explanation Case 1: As shown in the figure, between (0,2) and (4,0) there's only 1 integral point (2,1) hence 1 rock. Case 2: Between (2,2) and (5,5) lies (3,3) and (4,4), hence 2 rocks. Case 3: Between (1,9) and (8,16) there lies 6 rocks at positions (2,10) (3,11) (4,12) (5,13) (6,14) (7,15).
{"inputs": ["3\n0 2 4 0\n2 2 5 5\n1 9 8 16\n"], "outputs": ["1\n2\n6\n"]}
499
43