task_type
stringclasses
4 values
problem
stringlengths
14
5.23k
solution
stringlengths
1
8.29k
problem_tokens
int64
9
1.02k
solution_tokens
int64
1
1.98k
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. An undirected graph of n nodes is defined by edgeList, where edgeList[i] = [ui, vi, disi] denotes an edge between nodes ui and vi with distance disi. Note that there may be multiple edges between two nodes. Given an array queries, where queries[j] = [pj, qj, limitj], your task is to determine for each queries[j] whether there is a path between pj and qj such that each edge on the path has a distance strictly less than limitj . Return a boolean array answer, where answer.length == queries.length and the jth value of answer is true if there is a path for queries[j] is true, and false otherwise.   Please complete the following python code precisely: ```python class Solution: def distanceLimitedPathsExist(self, n: int, edgeList: List[List[int]], queries: List[List[int]]) -> List[bool]: ```
{"functional": "def check(candidate):\n assert candidate(n = 3, edgeList = [[0,1,2],[1,2,4],[2,0,8],[1,0,16]], queries = [[0,1,2],[0,2,5]]) == [False,True]\n assert candidate(n = 5, edgeList = [[0,1,10],[1,2,5],[2,3,9],[3,4,13]], queries = [[0,4,14],[1,4,13]]) == [True,False]\n\n\ncheck(Solution().distanceLimitedPathsExist)"}
207
140
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given two integers m and n. Consider an m x n grid where each cell is initially white. You can paint each cell red, green, or blue. All cells must be painted. Return the number of ways to color the grid with no two adjacent cells having the same color. Since the answer can be very large, return it modulo 109 + 7.   Please complete the following python code precisely: ```python class Solution: def colorTheGrid(self, m: int, n: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(m = 1, n = 1) == 3\n assert candidate(m = 1, n = 2) == 6\n assert candidate(m = 5, n = 5) == 580986\n\n\ncheck(Solution().colorTheGrid)"}
130
76
coding
Solve the programming task below in a Python markdown code block. Tattah's youngest brother, Tuftuf, is new to programming. Since his older brother is such a good programmer, his biggest dream is to outshine him. Tuftuf is a student at the German University in Cairo (GUC) where he learns to write programs in Gava. Today, Tuftuf was introduced to Gava's unsigned integer datatypes. Gava has n unsigned integer datatypes of sizes (in bits) a1, a2, ... an. The i-th datatype have size ai bits, so it can represent every integer between 0 and 2ai - 1 inclusive. Tuftuf is thinking of learning a better programming language. If there exists an integer x, such that x fits in some type i (in ai bits) and x·x does not fit in some other type j (in aj bits) where ai < aj, then Tuftuf will stop using Gava. Your task is to determine Tuftuf's destiny. Input The first line contains integer n (2 ≤ n ≤ 105) — the number of Gava's unsigned integer datatypes' sizes. The second line contains a single-space-separated list of n integers (1 ≤ ai ≤ 109) — sizes of datatypes in bits. Some datatypes may have equal sizes. Output Print "YES" if Tuftuf will stop using Gava, and "NO" otherwise. Examples Input 3 64 16 32 Output NO Input 4 4 2 1 3 Output YES Note In the second example, x = 7 (1112) fits in 3 bits, but x2 = 49 (1100012) does not fit in 4 bits.
{"inputs": ["2\n1 2\n", "2\n1 1\n", "2\n2 1\n", "2\n1 3\n", "2\n0 1\n", "2\n0 2\n", "2\n0 4\n", "2\n0 7\n"], "outputs": ["NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n"]}
390
102
coding
Solve the programming task below in a Python markdown code block. Alice and Bob decided to eat some fruit. In the kitchen they found a large bag of oranges and apples. Alice immediately took an orange for herself, Bob took an apple. To make the process of sharing the remaining fruit more fun, the friends decided to play a game. They put multiple cards and on each one they wrote a letter, either 'A', or the letter 'B'. Then they began to remove the cards one by one from left to right, every time they removed a card with the letter 'A', Alice gave Bob all the fruits she had at that moment and took out of the bag as many apples and as many oranges as she had before. Thus the number of oranges and apples Alice had, did not change. If the card had written letter 'B', then Bob did the same, that is, he gave Alice all the fruit that he had, and took from the bag the same set of fruit. After the last card way removed, all the fruit in the bag were over. You know how many oranges and apples was in the bag at first. Your task is to find any sequence of cards that Alice and Bob could have played with. -----Input----- The first line of the input contains two integers, x, y (1 ≤ x, y ≤ 10^18, xy > 1) — the number of oranges and apples that were initially in the bag. -----Output----- Print any sequence of cards that would meet the problem conditions as a compressed string of characters 'A' and 'B. That means that you need to replace the segments of identical consecutive characters by the number of repetitions of the characters and the actual character. For example, string AAABAABBB should be replaced by string 3A1B2A3B, but cannot be replaced by 2A1A1B2A3B or by 3AB2A3B. See the samples for clarifications of the output format. The string that you print should consist of at most 10^6 characters. It is guaranteed that if the answer exists, its compressed representation exists, consisting of at most 10^6 characters. If there are several possible answers, you are allowed to print any of them. If the sequence of cards that meet the problem statement does not not exist, print a single word Impossible. -----Examples----- Input 1 4 Output 3B Input 2 2 Output Impossible Input 3 2 Output 1A1B -----Note----- In the first sample, if the row contained three cards with letter 'B', then Bob should give one apple to Alice three times. So, in the end of the game Alice has one orange and three apples, and Bob has one apple, in total it is one orange and four apples. In second sample, there is no answer since one card is not enough for game to finish, and two cards will produce at least three apples or three oranges. In the third sample, cards contain letters 'AB', so after removing the first card Bob has one orange and one apple, and after removal of second card Alice has two oranges and one apple. So, in total it is three oranges and two apples.
{"inputs": ["1 4\n", "2 2\n", "3 2\n", "2 1\n", "5 3\n", "5 2\n", "8 5\n", "1 3\n"], "outputs": ["3B\n", "Impossible\n", "1A1B\n", "1A\n", "1A1B1A\n", "2A1B\n", "1A1B1A1B\n", "2B\n"]}
671
107
coding
Solve the programming task below in a Python markdown code block. Chef is operating a slush machine. The machine produces slush drinks with $M$ flavors (numbered $1$ through $M$); for each valid $i$, the maximum number of drinks with flavour $i$ the machine can produce is $C_i$. Chef expects $N$ customers to come buy slush drinks today. The customers are numbered $1$ through $N$ in the order in which they buy the drinks. For each valid $i$, the favorite flavour of the $i$-th customer is $D_i$ and this customer is willing to pay $F_i$ units of money for a drink with this flavour, or $B_i$ units of money for a drink with any other flavuor. Whenever a customer wants to buy a drink: - if it is possible to sell this customer a drink with their favourite flavour, Chef must sell them a drink with this flavour - otherwise, Chef must sell this customer a drink, but he may choose its flavour Chef wants to make the maximum possible profit. He is asking you to help him decide the flavours of the drinks he should sell to the customers in order to maximise the profit. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - The second line contains $M$ space-separated integers $C_1, C_2, \ldots, C_M$. - $N$ lines follow. For each valid $i$, the $i$-th of these lines contains three space-separated integers $D_i$, $F_i$ and $B_i$. -----Output----- For each test case, print two lines: - The first of these lines should contain a single integer — the maximum profit. - The second line should contain $N$ space-separated integers denoting the flavours of the drinks Chef should sell, in this order. If there are multiple solutions, you may find any one. -----Constraints----- - $1 \le T \le 1,000$ - $2 \le N, M \le 10^5$ - $1 \le D_i \le M$ for each valid $i$ - $1 \le C_i \le N$ for each valid $i$ - $1 \le B_i < F_i \le 10^9$ for each valid $i$ - $C_1+C_2+\ldots+C_M \ge N$ - the sum of $N$ over all test cases does not exceed $10^6$ - the sum of $M$ over all test cases does not exceed $10^6$ -----Example Input----- 1 5 3 1 2 3 2 6 3 2 10 7 2 50 3 1 10 5 1 7 4 -----Example Output----- 33 2 2 3 1 3
{"inputs": ["1\n5 3\n1 2 3\n2 6 3\n2 10 7\n2 50 3\n1 10 5\n1 7 4"], "outputs": ["33\n2 2 3 1 3"]}
660
66
coding
Solve the programming task below in a Python markdown code block. Today s kilometer long auto race takes place in Berland. The track is represented by a straight line as long as s kilometers. There are n cars taking part in the race, all of them start simultaneously at the very beginning of the track. For every car is known its behavior — the system of segments on each of which the speed of the car is constant. The j-th segment of the i-th car is pair (vi, j, ti, j), where vi, j is the car's speed on the whole segment in kilometers per hour and ti, j is for how many hours the car had been driving at that speed. The segments are given in the order in which they are "being driven on" by the cars. Your task is to find out how many times during the race some car managed to have a lead over another car. A lead is considered a situation when one car appears in front of another car. It is known, that all the leads happen instantly, i. e. there are no such time segment of positive length, during which some two cars drive "together". At one moment of time on one and the same point several leads may appear. In this case all of them should be taken individually. Meetings of cars at the start and finish are not considered to be counted as leads. Input The first line contains two integers n and s (2 ≤ n ≤ 100, 1 ≤ s ≤ 106) — the number of cars and the length of the track in kilometers. Then follow n lines — the description of the system of segments for each car. Every description starts with integer k (1 ≤ k ≤ 100) — the number of segments in the system. Then k space-separated pairs of integers are written. Each pair is the speed and time of the segment. These integers are positive and don't exceed 1000. It is guaranteed, that the sum of lengths of all segments (in kilometers) for each car equals to s; and all the leads happen instantly. Output Print the single number — the number of times some car managed to take the lead over another car during the race. Examples Input 2 33 2 5 1 2 14 1 3 11 Output 1 Input 2 33 2 1 3 10 3 1 11 3 Output 0 Input 5 33 2 1 3 3 10 1 11 3 2 5 3 3 6 2 3 1 10 3 2 6 3 3 5 Output 2
{"inputs": ["2 33\n2 5 1 2 14\n1 3 11\n", "2 33\n2 1 3 10 3\n1 11 3\n", "2 6\n3 1 2 2 1 1 2\n3 2 1 1 2 2 1\n", "2 5\n3 2 1 1 1 2 1\n3 1 1 2 1 1 2\n", "2 5\n3 2 1 1 1 2 1\n3 1 2 2 1 1 1\n", "2 166755\n2 733 187 362 82\n3 813 147 565 57 557 27\n", "5 33\n2 1 3 3 10\n1 11 3\n2 5 3 3 6\n2 3 1 10 3\n2 6 3 3 5\n", "3 228385\n2 307 733 43 78\n2 252 801 157 169\n3 86 346 133 886 467 173\n"], "outputs": [" 1\n", " 0\n", " 0\n", " 0\n", "0\n", " 0\n", " 2\n", " 0\n"]}
575
382
coding
Solve the programming task below in a Python markdown code block. A checksum is an algorithm that scans a packet of data and returns a single number. The idea is that if the packet is changed, the checksum will also change, so checksums are often used for detecting transmission errors, validating document contents, and in many other situations where it is necessary to detect undesirable changes in data. For this problem, you will implement a checksum algorithm called Quicksum. A Quicksum packet allows only uppercase letters and spaces. It always begins and ends with an uppercase letter. Otherwise, spaces and uppercase letters can occur in any combination, including consecutive spaces. A Quicksum is the sum of the products of each character’s position in the packet times the character’s value. A space has a value of zero, while letters have a value equal to their position in the alphabet. So, ```A = 1```, ```B = 2```, etc., through ```Z = 26```. Here are example Quicksum calculations for the packets “ACM” and “A C M”: ACM 1 × 1 + 2 × 3 + 3 × 13 = 46 A C M 1 x 1 + 3 x 3 + 5 * 13 = 75 When the packet doesn't have only uppercase letters and spaces or just spaces the result to quicksum have to be zero (0). AbqTH #5 = 0 Also feel free to reuse/extend the following starter code: ```python def quicksum(packet): ```
{"functional": "_inputs = [['ACM'], ['MID CENTRAL'], ['BBC'], ['???'], ['axg '], ['234 234 WEF ASDF AAA 554211 ???? '], ['A C M'], ['ABCDEFGHIJKLMNOPQRSTUVWXYZ'], ['A B C D E F G H I J K L M N O P Q R S T U V W X Y Z'], ['ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ'], ['Z A'], ['12312 123 123 asd asd 123 $$$$/()='], ['As '], [' ']]\n_outputs = [[46], [650], [15], [0], [0], [0], [75], [6201], [12051], [848640], [33], [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(quicksum(*i), o[0])"}
333
460
coding
Solve the programming task below in a Python markdown code block. ## The Riddle The King of a small country invites 1000 senators to his annual party. As a tradition, each senator brings the King a bottle of wine. Soon after, the Queen discovers that one of the senators is trying to assassinate the King by giving him a bottle of poisoned wine. Unfortunately, they do not know which senator, nor which bottle of wine is poisoned, and the poison is completely indiscernible. However, the King has 10 lab rats. He decides to use them as taste testers to determine which bottle of wine contains the poison. The poison when taken has no effect on the rats, until exactly 24 hours later when the infected rats suddenly die. The King needs to determine which bottle of wine is poisoned by tomorrow, so that the festivities can continue as planned. Hence he only has time for one round of testing, he decides that each rat tastes multiple bottles, according to a certain scheme. ## Your Task You receive an array of integers (`0 to 9`), each of them is the number of a rat which died after tasting the wine bottles. Return the number of the bottle (`1..1000`) which is poisoned. **Good Luck!** *Hint: think of rats as a certain representation of the number of the bottle...* Also feel free to reuse/extend the following starter code: ```python def find(r): ```
{"functional": "_inputs = [[[0]], [[1]], [[2]], [[3]], [[4]], [[5]], [[6]], [[7]], [[8]], [[9]], [[3, 5, 6, 7, 8, 9]], [[0, 3, 5, 4, 9, 8]], [[0, 1, 9, 3, 5]], [[0, 1, 2, 3, 4, 6]], [[0, 1, 3, 4]]]\n_outputs = [[1], [2], [4], [8], [16], [32], [64], [128], [256], [512], [1000], [825], [555], [95], [27]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(find(*i), o[0])"}
304
321
coding
Solve the programming task below in a Python markdown code block. Format any integer provided into a string with "," (commas) in the correct places. **Example:** ``` csharp Kata.NumberFormat(100000); // return "100,000" Kata.NumberFormat(5678545); // return "5,678,545" Kata.NumberFormat(-420902); // return "-420,902" ``` ``` javascript numberFormat(100000); // return '100,000' numberFormat(5678545); // return '5,678,545' numberFormat(-420902); // return '-420,902' ``` ``` cpp numberFormat(100000); // return '100,000' numberFormat(5678545); // return '5,678,545' numberFormat(-420902); // return '-420,902' ``` ``` python number_format(100000); # return '100,000' number_format(5678545); # return '5,678,545' number_format(-420902); # return '-420,902' ``` ``` ruby number_format(100000); # return '100,000' number_format(5678545); # return '5,678,545' number_format(-420902); # return '-420,902' ``` ``` crystal number_format(100000); # return '100,000' number_format(5678545); # return '5,678,545' number_format(-420902); # return '-420,902' ``` Also feel free to reuse/extend the following starter code: ```python def number_format(n): ```
{"functional": "_inputs = [[100000], [5678545], [-420902], [-3], [-1003]]\n_outputs = [['100,000'], ['5,678,545'], ['-420,902'], ['-3'], ['-1,003']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(number_format(*i), o[0])"}
489
221
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin chinese, Russian and Vietnamese as well. Today, Chef was trying to solve a problem he found pretty hard: Given an integer N and a triple of integers (a, b, c), compute the number of triples of positive integers (x, y, z) such that N = x · y · z, x ≤ a, y ≤ b and z ≤ c. Can you help Chef solve this problem? Two triples (x_{1}, y_{1}, z_{1}) and (x_{2}, y_{2}, z_{2}) differ if x_{1} is not equal to x_{2} or y_{1} is not equal to y_{2} or z_{1} is not equal to z_{2}. ------ 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, a, b and c. ------ Output ------ For each test case, print a single line containing one integer — the number of valid triples (x, y, z). ------ Constraints ------ $1 ≤ T ≤ 20$ $1 ≤ N ≤ 10^{9}$ $1 ≤ a, b, c ≤ 10^{6}$ ----- Sample Input 1 ------ 3 100 8 23 11 497296800 1000000 1000000 1000000 1 1 2 3 ----- Sample Output 1 ------ 10 97800 1 ----- explanation 1 ------ Example case 1: There are 10 valid triples (x, y, z): (1, 10, 10), (1, 20, 5), (2, 5, 10), (2, 10, 5), (4, 5, 5), (5, 2, 10), (5, 4, 5), (5, 5, 4), (5, 10, 2), (5, 20, 1).
{"inputs": ["3\n100 8 23 11\n497296800 1000000 1000000 1000000\n1 1 2 3"], "outputs": ["10\n97800\n1"]}
499
75
coding
Solve the programming task below in a Python markdown code block. Imagine that you are given two sticks. You want to end up with three sticks of equal length. You are allowed to cut either or both of the sticks to accomplish this, and can throw away leftover pieces. Write a function, maxlen, that takes the lengths of the two sticks (L1 and L2, both positive values), that will return the maximum length you can make the three sticks. Also feel free to reuse/extend the following starter code: ```python def maxlen(l1, l2): ```
{"functional": "_inputs = [[5, 12], [12, 5], [5, 17], [17, 5], [7, 12], [12, 7]]\n_outputs = [[5], [5], [5.666666666666667], [5.666666666666667], [6.0], [6.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(maxlen(*i), o[0])"}
117
244
coding
Solve the programming task below in a Python markdown code block. Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that i^{th} friend is given a_{i} sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every a_{i}. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (10^9 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (10^9 + 7). -----Input----- The first line contains an integer q representing the number of queries (1 ≤ q ≤ 10^5). Each of the next q lines contains two space space-separated integers n, f (1 ≤ f ≤ n ≤ 10^5). -----Output----- For each query, output a single integer in a line corresponding to the answer of each query. -----Examples----- Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 -----Note----- For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning.
{"inputs": ["1\n1 1\n", "1\n1 1\n", "2\n2 1\n2 2\n", "2\n2 1\n2 2\n", "2\n4 1\n2 2\n", "3\n3 1\n3 2\n3 3\n", "3\n3 1\n3 2\n3 3\n", "3\n5 1\n3 2\n3 3\n"], "outputs": ["1\n", "1\n", "0\n1\n", "0\n1\n", "0\n1\n", "0\n2\n1\n", "0\n2\n1\n", "0\n2\n1\n"]}
514
156
coding
Solve the programming task below in a Python markdown code block. I need to save some money to buy a gift. I think I can do something like that: First week (W0) I save nothing on Sunday, 1 on Monday, 2 on Tuesday... 6 on Saturday, second week (W1) 2 on Monday... 7 on Saturday and so on according to the table below where the days are numbered from 0 to 6. Can you tell me how much I will have for my gift on Saturday evening after I have saved 12? (Your function finance(6) should return 168 which is the sum of the savings in the table). Imagine now that we live on planet XY140Z-n where the days of the week are numbered from 0 to n (integer n > 0) and where I save from week number 0 to week number n included (in the table below n = 6). How much money would I have at the end of my financing plan on planet XY140Z-n? -- |Su|Mo|Tu|We|Th|Fr|Sa| --|--|--|--|--|--|--|--| W6 | | | | | | |12| W5 | | | | | |10|11| W4 | | | | |8 |9 |10| W3 | | | |6 |7 |8 |9 | W2 | | |4 |5 |6 |7 |8 | W1 | |2 |3 |4 |5 |6 |7 | W0 |0 |1 |2 |3 |4 |5 |6 | #Example: ``` finance(5) --> 105 finance(6) --> 168 finance(7) --> 252 finance(5000) --> 62537505000 ``` #Hint: try to avoid nested loops Also feel free to reuse/extend the following starter code: ```python def finance(n): ```
{"functional": "_inputs = [[5], [6], [8], [15], [100], [365], [730], [999], [2000], [4000], [5000]]\n_outputs = [[105], [168], [360], [2040], [515100], [24513765], [195308580], [499999500], [4006002000], [32024004000], [62537505000]]\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(finance(*i), o[0])"}
469
298
coding
Solve the programming task below in a Python markdown code block. A certain type of steel is graded according to the following conditions. 1. Hardness of the steel must be greater than 50 2. Carbon content of the steel must be less than 0.7 3. Tensile strength must be greater than 5600 The grades awarded are as follows: Grade is 10 if all three conditions are met Grade is 9 if conditions (1) and (2) are met Grade is 8 if conditions (2) and (3) are met Grade is 7 if conditions (1) and (3) are met Grade is 6 if only one condition is met Grade is 5 if none of the three conditions are met Write a program to display the grade of the steel, based on the values of hardness, carbon content and tensile strength of the steel, given by the user. ------ Input Format ------ The first line contains an integer T, total number of testcases. Then follow T lines, each line contains three numbers hardness, carbon content and tensile strength of the steel. ------ Output Format ------ For each test case, print Grade of the steel depending on Conditions, in a new line. ------ Constraints ------ - 1 ≤ T ≤ 1000 - 0 ≤ hardness, carbon content, tensile strength ≤ 10000 ----- Sample Input 1 ------ 3 53 0.6 5602 45 0 4500 0 0 0 ----- Sample Output 1 ------ 10 6 6
{"inputs": ["3 \n67 0.6 5602\n45 0 4500\n0 0 0", "3 \n67 0.6 5602\n78 0 4500\n0 0 0", "3 \n53 0.6 5602\n45 0 4500\n0 0 0", "3 \n103 0.6 5602\n78 0 4500\n0 0 0", "3 \n103 0.6 5602\n78 0 4466\n0 0 0", "3 \n103 0.6 7770\n78 0 4466\n0 0 0", "3 \n53 0.6 5602\n45 0 4500\n0 0 0 \n", "3 \n103 0.6 7770\n78 -1 4466\n0 0 0"], "outputs": ["10\n6\n6\n", "10\n9\n6\n", "10\n6\n6\n", "10\n9\n6\n", "10\n9\n6\n", "10\n9\n6\n", "10\n6\n6\n", "10\n9\n6\n"]}
349
340
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin chinese, Russian and Vietnamese as well. Chef has a sequence $A$ consisting of $N$ positive integers. He is playing a game with this sequence. The goal of the game is to maximize the greatest common divisor of all elements of the sequence. Chef may perform the following operation an arbitrary number of times (including zero): Choose a positive integer $d$ and a valid index $i$ such that $A_{i}$ is divisible by $d^{2}$. Divide $A_{i}$ by $d^{2}$. Choose a valid index $j$ (not necessarily different from $i$) and multiply $A_{j}$ by $d$. Help Chef and find the maximum GCD of all integers in the sequence he could obtain if he plays optimally! ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \dots, A_{N}$. ------ Output ------ For each test case, print a single line containing one integer — the maximum possible value of the greatest common divisor of all elements of the sequence. ------ Constraints ------ $1 ≤ T ≤ 5$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{6}$ for each valid $i$ ----- Sample Input 1 ------ 2 2 1 25 5 27 3 15 1024 15 ----- Sample Output 1 ------ 1 6
{"inputs": ["2\n2\n1 25\n5\n27 3 15 1024 15"], "outputs": ["1\n6"]}
384
39
coding
Solve the programming task below in a Python markdown code block. Sumit and Dhiraj are roommates in a hostel of NIT Jamshedpur,One day after completing there boring assignments of Artificial Intelligence, they decided to play a game as it was dhiraj,s idea to play a game so he started explaining the rules of the game to sumit. So the game was as follows-They randomly took a number N after that they find all its divisors.Suppose first sumit took a divisor then dhiraj and so on.The player who will be having the last divisor with him will win the game.Rohit their mutual friend,was observing them play. Can you help Rohit predict the outcome of game? If Sumit wins print "YES" without quotes and "NO" without quotes, if Dhiraj wins the game. -----Input----- Input starts with an integer T denoting the number of test cases Each test case starts with a line containing an integer N the number. -----Output----- Outcome of each game in a separate line -----Constraints----- - 1 ≤ T ≤ 10^3 - 1 ≤ N ≤ 10^18 -----Sub tasks----- - Subtask #1:(10 points) - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 103 - Subtask #2:(25 points) - 1 ≤ T ≤ 50 - 1 ≤ N ≤ 1016 - Subtask #3:(65 points) - 1 ≤ T ≤ 103 - 1 ≤ N ≤ 1018 -----Example----- Input: 2 4 5 Output: YES NO
{"inputs": ["2\n4\n5"], "outputs": ["YES\nNO"]}
360
18
coding
Solve the programming task below in a Python markdown code block. Chef has provided with a sequence of integers of length $N$ arranged in an unsorted fashion. The elements of the sequence are ordered as ${A1,A2,A3.....A'N}$ The task of the chef is to identify the highest and lowest value among the given sequence. It is assured that the sequence given contains the highest and the lowest value always Please help chef in finding the desired value. -----Input:----- - First line will contain $N$, number of elements in the sequence. - Next line contains $N$ integers of the sequence . -----Output:----- Print the HIGHEST and LOWEST value of the sequence respectively. -----Constraints----- - $1 \leq N \leq 100$ - $2 \leq {A1,A2,A3.....A'N} \leq 10^4$ -----Sample Input:----- 5 3 2 7 9 4 -----Sample Output:----- 9 2 -----EXPLANATION:----- This list is : [3,2,7,9,4] so the highest value is 9 and lowest is 2 respectively.
{"inputs": ["5\n3 2 7 9 4"], "outputs": ["9 2"]}
260
24
coding
Solve the programming task below in a Python markdown code block. Even if it's a really easy question, she won't be able to answer it — Perfect Memento in Strict Sense Cirno's perfect bitmasks classroom has just started! Cirno gave her students a positive integer $x$. As an assignment, her students need to find the minimum positive integer $y$, which satisfies the following two conditions: $$x\ {and}\ y > 0$$ $$x\ {xor}\ y > 0$$ Where ${and}$ is the bitwise AND operation , and ${xor}$ is the bitwise XOR operation . Among the students was Mystia, who was truly baffled by all these new operators. Please help her! -----Input----- The first line of input contains a single integer $t$ ($1 \leq t \leq 10^3$) — the number of input test cases. For each test case, the only line of input contains one integer $x$ ($1 \leq x \leq 2^{30}$). -----Output----- For each test case, print a single integer — the minimum number of $y$. -----Examples----- Input 7 1 2 5 9 16 114514 1000000 Output 3 3 1 1 17 2 64 -----Note----- Test case 1: $1\; {and}\; 3=1>0$, $1\; {xor}\; 3=2>0$. Test case 2: $2\; {and}\; 3=2>0$, $2\; {xor}\; 3=1>0$.
{"inputs": ["1\n1073741824\n", "7\n1\n2\n5\n9\n16\n114514\n1000000\n"], "outputs": ["1073741825\n", "3\n3\n1\n1\n17\n2\n64\n"]}
369
82
coding
Solve the programming task below in a Python markdown code block. Welcome This kata is inspired by This Kata We have a string s We have a number n Here is a function that takes your string, concatenates the even-indexed chars to the front, odd-indexed chars to the back. Examples s = "Wow Example!" result = "WwEapeo xml!" s = "I'm JomoPipi" result = "ImJm ii' ooPp" The Task: return the result of the string after applying the function to it n times. example where s = "qwertyuio" and n = 2: after 1 iteration s = "qetuowryi" after 2 iterations s = "qtorieuwy" return "qtorieuwy" Note there's a lot of tests, big strings, and n is greater than a billion so be ready to optimize. after doing this: do it's best friend! # Check out my other kata! Matrix Diagonal Sort OMG String -> N iterations -> String String -> X iterations -> String ANTISTRING Array - squareUp b! Matrix - squareUp b! Infinitely Nested Radical Expressions pipi Numbers! Also feel free to reuse/extend the following starter code: ```python def jumbled_string(s, n): ```
{"functional": "_inputs = [['Such Wow!', 1], ['better example', 2], ['qwertyuio', 2], ['Greetings', 8], ['I like it!', 1234], ['codingisfornerdsyounerd', 10101010], ['this_test_will_hurt_you', 12345678987654321]]\n_outputs = [['Sc o!uhWw'], ['bexltept merae'], ['qtorieuwy'], ['Gtsegenri'], ['Iiei t kl!'], ['criyinodedsgufrnodnoser'], ['ti_etwl_utyuhsts_ilhr_o']]\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(jumbled_string(*i), o[0])"}
293
298
coding
Solve the programming task below in a Python markdown code block. In Takahashi Kingdom, there is an archipelago of N islands, called Takahashi Islands. For convenience, we will call them Island 1, Island 2, ..., Island N. There are M kinds of regular boat services between these islands. Each service connects two islands. The i-th service connects Island a_i and Island b_i. Cat Snuke is on Island 1 now, and wants to go to Island N. However, it turned out that there is no boat service from Island 1 to Island N, so he wants to know whether it is possible to go to Island N by using two boat services. Help him. Constraints * 3 ≤ N ≤ 200 000 * 1 ≤ M ≤ 200 000 * 1 ≤ a_i < b_i ≤ N * (a_i, b_i) \neq (1, N) * If i \neq j, (a_i, b_i) \neq (a_j, b_j). Input Input is given from Standard Input in the following format: N M a_1 b_1 a_2 b_2 : a_M b_M Output If it is possible to go to Island N by using two boat services, print `POSSIBLE`; otherwise, print `IMPOSSIBLE`. Examples Input 3 2 1 2 2 3 Output POSSIBLE Input 4 3 1 2 2 3 3 4 Output IMPOSSIBLE Input 100000 1 1 99999 Output IMPOSSIBLE Input 5 5 1 3 4 5 2 3 2 4 1 4 Output POSSIBLE
{"inputs": ["3 2\n1 2\n0 3", "3 2\n1 2\n2 3", "4 3\n1 3\n2 3\n3 4", "4 3\n1 3\n3 3\n3 4", "4 3\n1 1\n2 3\n3 4", "4 2\n1 3\n2 3\n3 4", "4 3\n1 1\n2 5\n3 4", "4 2\n1 3\n2 3\n0 4"], "outputs": ["IMPOSSIBLE\n", "POSSIBLE", "POSSIBLE\n", "POSSIBLE\n", "IMPOSSIBLE\n", "IMPOSSIBLE\n", "IMPOSSIBLE\n", "IMPOSSIBLE\n"]}
403
186
coding
Solve the programming task below in a Python markdown code block. Zookeeper is playing a game. In this game, Zookeeper must use bombs to bomb a string that consists of letters 'A' and 'B'. He can use bombs to bomb a substring which is either "AB" or "BB". When he bombs such a substring, the substring gets deleted from the string and the remaining parts of the string get concatenated. For example, Zookeeper can use two such operations: AABABBA $\to$ AABBA $\to$ AAA. Zookeeper wonders what the shortest string he can make is. Can you help him find the length of the shortest string? -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ $(1 \leq t \leq 20000)$  — the number of test cases. The description of the test cases follows. Each of the next $t$ lines contains a single test case each, consisting of a non-empty string $s$: the string that Zookeeper needs to bomb. It is guaranteed that all symbols of $s$ are either 'A' or 'B'. It is guaranteed that the sum of $|s|$ (length of $s$) among all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer: the length of the shortest string that Zookeeper can make. -----Example----- Input 3 AAA BABA AABBBABBBB Output 3 2 0 -----Note----- For the first test case, you can't make any moves, so the answer is $3$. For the second test case, one optimal sequence of moves is BABA $\to$ BA. So, the answer is $2$. For the third test case, one optimal sequence of moves is AABBBABBBB $\to$ AABBBABB $\to$ AABBBB $\to$ ABBB $\to$ AB $\to$ (empty string). So, the answer is $0$.
{"inputs": ["3\nAAA\nBABA\nAABBBABBBB\n", "3\nAAA\nAABA\nAABBBABBBB\n", "3\nAAB\nBABA\nAABBBABBBB\n", "3\nAAB\nBABA\nBBBBABBBAA\n", "3\nAAB\nBAAA\nBBBBABBBAA\n", "3\nAAA\nBABB\nAABBBABBBB\n", "3\nAAB\nABAB\nBBBBABBBAA\n", "3\nBAA\nBAAA\nBBBBABBBAA\n"], "outputs": ["3\n2\n0\n", "3\n2\n0\n", "1\n2\n0\n", "1\n2\n2\n", "1\n4\n2\n", "3\n0\n0\n", "1\n0\n2\n", "3\n4\n2\n"]}
438
195
coding
Solve the programming task below in a Python markdown code block. During one of the space missions, humans have found an evidence of previous life at one of the planets. They were lucky enough to find a book with birth and death years of each individual that had been living at this planet. What's interesting is that these years are in the range $(1, 10^9)$! Therefore, the planet was named Longlifer. In order to learn more about Longlifer's previous population, scientists need to determine the year with maximum number of individuals that were alive, as well as the number of alive individuals in that year. Your task is to help scientists solve this problem! -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^5$) — the number of people. Each of the following $n$ lines contain two integers $b$ and $d$ ($1 \le b \lt d \le 10^9$) representing birth and death year (respectively) of each individual. -----Output----- Print two integer numbers separated by blank character, $y$  — the year with a maximum number of people alive and $k$  — the number of people alive in year $y$. In the case of multiple possible solutions, print the solution with minimum year. -----Examples----- Input 3 1 5 2 4 5 6 Output 2 2 Input 4 3 4 4 5 4 6 8 10 Output 4 2 -----Note----- You can assume that an individual living from $b$ to $d$ has been born at the beginning of $b$ and died at the beginning of $d$, and therefore living for $d$ - $b$ years.
{"inputs": ["1\n1 2\n", "1\n1 2\n", "1\n1 3\n", "1\n0 3\n", "1\n0 1\n", "1\n0 2\n", "1\n0 6\n", "1\n1 6\n"], "outputs": ["1 1\n", "1 1\n", "1 1\n", "0 1\n", "0 1\n", "0 1\n", "0 1\n", "1 1\n"]}
382
118
coding
Solve the programming task below in a Python markdown code block. Charlie and Johnny play a game. For every integer $\mbox{X}$ Charlie gives, Johnny has to find the smallest positive integer $\mathbf{Y}$ such that $X\times Y$ ($\mbox{X}$ multiplied by $\mathbf{Y}$) contains only 4s and 0s and starts with one or more 4s followed by zero or more 0s. For example, 404 is an invalid number but 4400, 440, and 444 are valid numbers. If $\boldsymbol{a}$ is the number of 4s and $\boldsymbol{b}$ is the number of 0s, can you print the value of $(2\times a)+b$? Input Format The first line of input contains a single integer $\mathbf{T}$, the number of test cases. $\mathbf{T}$ lines follow, each line containing the integer $\mbox{X}$ as stated above. Output Format For every $\mbox{X}$, print the output $(2\times a)+b$ in a newline as stated in the problem statement. Constraints $1\leq T\leq100$ $1\leq X\leq10^{10}$ Sample Input 3 4 5 80 Sample Output 2 3 4 Explanation For the 1^{st} test case, the smallest such multiple of $4}$ is 4 itself. Hence the value of $\boldsymbol{a}$ will be $\mbox{1}$ and and the value of $\boldsymbol{b}$ will be $\mbox{0}$, and the answer is $(2\times a)+b=2$. For the 2^{nd} test case, $Y=8$ and 40 is the minimum such multiple of $5$. Hence the values of $\boldsymbol{a}$, $\boldsymbol{b}$ and $(2\times a)+b$ will be $\mbox{1}$, $\mbox{1}$ and $3$ respectively.
{"inputs": ["3\n4\n5\n80\n"], "outputs": ["2\n3\n4\n"]}
463
25
coding
Solve the programming task below in a Python markdown code block. William arrived at a conference dedicated to cryptocurrencies. Networking, meeting new people, and using friends' connections are essential to stay up to date with the latest news from the world of cryptocurrencies. The conference has $n$ participants, who are initially unfamiliar with each other. William can introduce any two people, $a$ and $b$, who were not familiar before, to each other. William has $d$ conditions, $i$'th of which requires person $x_i$ to have a connection to person $y_i$. Formally, two people $x$ and $y$ have a connection if there is such a chain $p_1=x, p_2, p_3, \dots, p_k=y$ for which for all $i$ from $1$ to $k - 1$ it's true that two people with numbers $p_i$ and $p_{i + 1}$ know each other. For every $i$ ($1 \le i \le d$) William wants you to calculate the maximal number of acquaintances one person can have, assuming that William satisfied all conditions from $1$ and up to and including $i$ and performed exactly $i$ introductions. The conditions are being checked after William performed $i$ introductions. The answer for each $i$ must be calculated independently. It means that when you compute an answer for $i$, you should assume that no two people have been introduced to each other yet. -----Input----- The first line contains two integers $n$ and $d$ ($2 \le n \le 10^3, 1 \le d \le n - 1$), the number of people, and number of conditions, respectively. Each of the next $d$ lines each contain two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n, x_i \neq y_i$), the numbers of people which must have a connection according to condition $i$. -----Output----- Output $d$ integers. $i$th number must equal the number of acquaintances the person with the maximal possible acquaintances will have, if William performed $i$ introductions and satisfied the first $i$ conditions. -----Examples----- Input 7 6 1 2 3 4 2 4 7 6 6 5 1 7 Output 1 1 3 3 3 6 Input 10 8 1 2 2 3 3 4 1 4 6 7 8 9 8 10 1 4 Output 1 2 3 4 5 5 6 8 -----Note----- The explanation for the first test case: In this explanation, the circles and the numbers in them denote a person with the corresponding number. The line denotes that William introduced two connected people. The person marked with red has the most acquaintances. These are not the only correct ways to introduce people.
{"inputs": ["2 1\n1 2\n", "3 1\n2 3\n", "4 1\n2 4\n", "3 2\n1 3\n3 1\n", "4 2\n2 4\n3 4\n", "4 3\n2 1\n4 1\n1 2\n", "7 6\n1 2\n3 4\n2 4\n7 6\n6 5\n1 7\n", "10 8\n1 2\n2 3\n3 4\n1 4\n6 7\n8 9\n8 10\n1 4\n"], "outputs": ["1\n", "1\n", "1\n", "1\n2\n", "1\n2\n", "1\n2\n3\n", "1\n1\n3\n3\n3\n6\n", "1\n2\n3\n4\n5\n5\n6\n8\n"]}
645
216
coding
Solve the programming task below in a Python markdown code block. The Third Doctor Who once correctly said that travel between parallel universes is "like travelling sideways". However, he incorrectly thought that there were infinite parallel universes, whereas in fact, as we now all know, there will never be more than 250. Heidi recently got her hands on a multiverse observation tool. She was able to see all $n$ universes lined up in a row, with non-existent links between them. She also noticed that the Doctor was in the $k$-th universe. The tool also points out that due to restrictions originating from the space-time discontinuum, the number of universes will never exceed $m$. Obviously, the multiverse is unstable because of free will. Each time a decision is made, one of two events will randomly happen: a new parallel universe is created, or a non-existent link is broken. More specifically, When a universe is created, it will manifest itself between any two adjacent universes or at one of the ends. When a link is broken, it could be cut between any two adjacent universes. After separating the multiverse into two segments, the segment NOT containing the Doctor will cease to exist. Heidi wants to perform a simulation of $t$ decisions. Each time a decision is made, Heidi wants to know the length of the multiverse (i.e. the number of universes), and the position of the Doctor. -----Input----- The first line contains four integers $n$, $k$, $m$ and $t$ ($2 \le k \le n \le m \le 250$, $1 \le t \le 1000$). Each of the following $t$ lines is in one of the following formats: "$1$ $i$" — meaning that a universe is inserted at the position $i$ ($1 \le i \le l + 1$), where $l$ denotes the current length of the multiverse. "$0$ $i$" — meaning that the $i$-th link is broken ($1 \le i \le l - 1$), where $l$ denotes the current length of the multiverse. -----Output----- Output $t$ lines. Each line should contain $l$, the current length of the multiverse and $k$, the current position of the Doctor. It is guaranteed that the sequence of the steps will be valid, i.e. the multiverse will have length at most $m$ and when the link breaking is performed, there will be at least one universe in the multiverse. -----Example----- Input 5 2 10 4 0 1 1 1 0 4 1 2 Output 4 1 5 2 4 2 5 3 -----Note----- The multiverse initially consisted of 5 universes, with the Doctor being in the second. First, link 1 was broken, leaving the multiverse with 4 universes, and the Doctor in the first. Then, a universe was added to the leftmost end of the multiverse, increasing the multiverse length to 5, and the Doctor was then in the second universe. Then, the rightmost link was broken. Finally, a universe was added between the first and the second universe.
{"inputs": ["5 2 7 4\n0 1\n1 2\n0 4\n1 2\n", "5 2 10 4\n0 1\n1 1\n0 4\n1 2\n", "5 2 10 4\n0 1\n1 1\n1 4\n1 2\n", "5 2 10 4\n0 1\n1 1\n1 4\n0 2\n", "5 2 10 4\n1 1\n1 1\n1 4\n0 2\n", "5 2 10 4\n1 1\n1 1\n1 4\n0 1\n", "7 2 10 4\n1 1\n1 1\n1 4\n0 1\n", "7 3 17 4\n1 1\n1 1\n1 4\n0 1\n"], "outputs": ["4 1\n5 1\n4 1\n5 1\n", "4 1\n5 2\n4 2\n5 3\n", "4 1\n5 2\n6 2\n7 3\n", "4 1\n5 2\n6 2\n2 2\n", "6 3\n7 4\n8 5\n6 3\n", "6 3\n7 4\n8 5\n7 4\n", "8 3\n9 4\n10 5\n9 4\n", "8 4\n9 5\n10 6\n9 5\n"]}
699
367
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$. Chef wants you to handle $Q$ queries. There are two types of queries: 1 L R X: for each $i$ ($L ≤ i ≤ R$), add $(X + i - L)^2$ to $A_{i}$ 2 Y: find the current value of $A_{Y}$ ------ Input ------ The first line of the input contains two space-separated integers $N$ and $Q$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. $Q$ lines follow. Each of these lines describes a query in the format described above. ------ Output ------ For each query of the second type, print a single line containing one integer $A_{Y}$. ------ Constraints ------ $1 ≤ N, Q ≤ 10^{5}$ $0 ≤ |A_{i}| ≤ 10^{6}$ for each valid $i$ $1 ≤ L ≤ R ≤ N$ $0 ≤ |X| ≤ 10^{6}$ $1 ≤ Y ≤ N$ ----- Sample Input 1 ------ 5 4 1 10 3 6 5 1 1 3 5 2 3 1 4 5 7 2 5 ----- Sample Output 1 ------ 52 69 ----- explanation 1 ------ - the sequence after the first query is $[1 + 5^{2}, 10 + (5 + 2 - 1)^2, 3 + (5 + 3 - 1)^2, 6, 5] = [26, 46, 52, 6, 5]$ - the second query asks for $A_{3}$, which is $52$ - the sequence after the third query is $[26, 46, 52, 6 + 7^{2}, 5 + (7+1)^2] = [26, 46, 52, 55, 69]$ - the fourth query asks for $A_{5}$, which is $69$
{"inputs": ["5 4\n1 10 3 6 5\n1 1 3 5\n2 3\n1 4 5 7\n2 5"], "outputs": ["52\n69"]}
527
53
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. Let us call a non-empty subset of rows good if the sum of each column of the subset is at most half of the length of the subset. More formally, if the length of the chosen subset of rows is k, then the sum of each column should be at most floor(k / 2). Return an integer array that contains row indices of a good subset sorted in ascending order. If there are multiple good subsets, you can return any of them. If there are no good subsets, return an empty array. A subset of rows of the matrix grid is any matrix that can be obtained by deleting some (possibly none or all) rows from grid.   Please complete the following python code precisely: ```python class Solution: def goodSubsetofBinaryMatrix(self, grid: List[List[int]]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(grid = [[0,1,1,0],[0,0,0,1],[1,1,1,1]]) == [0,1]\n assert candidate(grid = [[0]]) == [0]\n assert candidate(grid = [[1,1,1],[1,1,1]]) == []\n\n\ncheck(Solution().goodSubsetofBinaryMatrix)"}
200
94
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Alice has some number of cards and she wants to rearrange the cards into groups so that each group is of size groupSize, and consists of groupSize consecutive cards. Given an integer array hand where hand[i] is the value written on the ith card and an integer groupSize, return true if she can rearrange the cards, or false otherwise.   Please complete the following python code precisely: ```python class Solution: def isNStraightHand(self, hand: List[int], groupSize: int) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(hand = [1,2,3,6,2,3,4,7,8], groupSize = 3) == True\n assert candidate(hand = [1,2,3,4,5], groupSize = 4) == False\n\n\ncheck(Solution().isNStraightHand)"}
127
80
coding
Solve the programming task below in a Python markdown code block. You are given a following process. There is a platform with $n$ columns. $1 \times 1$ squares are appearing one after another in some columns on this platform. If there are no squares in the column, a square will occupy the bottom row. Otherwise a square will appear at the top of the highest square of this column. When all of the $n$ columns have at least one square in them, the bottom row is being removed. You will receive $1$ point for this, and all the squares left will fall down one row. You task is to calculate the amount of points you will receive. -----Input----- The first line of input contain 2 integer numbers $n$ and $m$ ($1 \le n, m \le 1000$) — the length of the platform and the number of the squares. The next line contain $m$ integer numbers $c_1, c_2, \dots, c_m$ ($1 \le c_i \le n$) — column in which $i$-th square will appear. -----Output----- Print one integer — the amount of points you will receive. -----Example----- Input 3 9 1 1 2 2 2 3 1 2 3 Output 2 -----Note----- In the sample case the answer will be equal to $2$ because after the appearing of $6$-th square will be removed one row (counts of the squares on the platform will look like $[2~ 3~ 1]$, and after removing one row will be $[1~ 2~ 0]$). After the appearing of $9$-th square counts will be $[2~ 3~ 1]$, and after removing one row it will look like $[1~ 2~ 0]$. So the answer will be equal to $2$.
{"inputs": ["1 1\n1\n", "2 1\n2\n", "2 1\n1\n", "2 1\n2\n", "1 1\n1\n", "2 1\n1\n", "2 2\n2 2\n", "4 2\n1 2\n"], "outputs": ["1\n", "0\n", "0\n", "0\n", "1\n", "0\n", "0\n", "0\n"]}
412
106
coding
Solve the programming task below in a Python markdown code block. The chef is trying to decode some pattern problems, Chef wants your help to code it. Chef has one number K to form a new pattern. Help the chef to code this pattern problem. -----Input:----- - First-line will contain $T$, the number of test cases. Then the test cases follow. - Each test case contains a single line of input, one integer $K$. -----Output:----- For each test case, output as the pattern. -----Constraints----- - $1 \leq T \leq 100$ - $1 \leq K \leq 100$ -----Sample Input:----- 4 1 2 3 4 -----Sample Output:----- 1 12 3 123 45 6 1234 567 89 10 -----EXPLANATION:----- No need, else pattern can be decode easily.
{"inputs": ["4\n1\n2\n3\n4"], "outputs": ["1\n12\n3\n123\n45\n6\n1234\n567\n89\n10"]}
207
49
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an integer array nums, return the number of non-empty subarrays with the leftmost element of the subarray not larger than other elements in the subarray. A subarray is a contiguous part of an array.   Please complete the following python code precisely: ```python class Solution: def validSubarrays(self, nums: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [1,4,2,5,3]) == 11\n assert candidate(nums = [3,2,1]) == 3\n assert candidate(nums = [2,2,2]) == 6\n\n\ncheck(Solution().validSubarrays)"}
94
73
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an integer n, return the least number of perfect square numbers that sum to n. A perfect square is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, 1, 4, 9, and 16 are perfect squares while 3 and 11 are not.   Please complete the following python code precisely: ```python class Solution: def numSquares(self, n: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(n = 12) == 3 \n assert candidate(n = 13) == 2\n\n\ncheck(Solution().numSquares)"}
123
47
coding
Solve the programming task below in a Python markdown code block. For a given array $a_1, a_2, a_3, ... , a_N$ of $N$ elements and an integer $S$, find the smallest sub-array size (smallest window length) where the sum of the sub-array is greater than or equal to $S$. If there is not such sub-array, report 0. Constraints * $1 \leq N \leq 10^5$ * $1 \leq S \leq 10^9$ * $1 \leq a_i \leq 10^4$ Input The input is given in the following format. $N$ $S$ $a_1$ $a_2$ ... $a_N$ Output Print the smallest sub-array size in a line. Examples Input 6 4 1 2 1 2 3 2 Output 2 Input 6 6 1 2 1 2 3 2 Output 3 Input 3 7 1 2 3 Output 0
{"inputs": ["3 6\n1 2 3", "3 3\n1 2 3", "3 7\n1 2 3", "3 10\n1 2 3", "3 10\n0 2 3", "3 15\n1 2 3", "3 10\n1 2 0", "6 4\n1 2 0 2 3 2"], "outputs": ["3\n", "1\n", "0", "0\n", "0\n", "0\n", "0\n", "2\n"]}
241
135
coding
Solve the programming task below in a Python markdown code block. You are given a N * N matrix, U. You have to choose 2 sub-matrices A and B made of only 1s of U, such that, they have at least 1 cell in common, and each matrix is not completely engulfed by the other, i.e., If U is of the form and A is of the form and B is of the form then, there exists atleast 1 a_{i, j} : a_{i, j} ∈ A and a_{i,j} ∈ B then, there exists atleast 1 ai1, j1 : ai1, j1 ∈ A and ai1,j1 ∉ B then, there exists atleast 1 ai2, j2 : ai2, j2 ∈ B and ai2,j2 ∉ A a_{x,y} = 1 ∀ a_{x,y} ∈ A a_{x,y} = 1 ∀ a_{x,y} ∈ B How many such (A, B) exist? Input Format The first line of the input contains a number N. N lines follow, each line containing N integers (0/1) NOT separated by any space. Output Format Output the total number of such (A, B) pairs. If the answer is greater than or equal to 10^{9} + 7, then print answer modulo (%) 10^{9} + 7. Constraints 2 ≤ N ≤ 1500 a_{i,j} ∈ [0, 1] : 0 ≤ i, j ≤ N - 1 Sample Input 4 0010 0001 1010 1110 Sample Output 10 Explanation X means the common part of A and B. We can swap A and B to get another answer. 0010 0001 A010 XB10 0010 0001 A010 XBB0 0010 0001 10A0 1BX0 0010 0001 10A0 BBX0 0010 0001 1010 AXB0 TimeLimits Time limit for this challenge is mentioned here
{"inputs": ["4\n0010\n0001\n1010\n1110\n"], "outputs": ["10\n"]}
512
35
coding
Solve the programming task below in a Python markdown code block. Polycarp loves geometric progressions — he collects them. However, as such progressions occur very rarely, he also loves the sequences of numbers where it is enough to delete a single element to get a geometric progression. In this task we shall define geometric progressions as finite sequences of numbers a1, a2, ..., ak, where ai = c·bi - 1 for some real numbers c and b. For example, the sequences [2, -4, 8], [0, 0, 0, 0], [199] are geometric progressions and [0, 1, 2, 3] is not. Recently Polycarp has found a sequence and he can't classify it. Help him to do it. Determine whether it is a geometric progression. If it is not, check if it can become a geometric progression if an element is deleted from it. Input The first line contains an integer n (1 ≤ n ≤ 105) — the number of elements in the given sequence. The second line contains the given sequence. The numbers are space-separated. All the elements of the given sequence are integers and their absolute value does not exceed 104. Output Print 0, if the given sequence is a geometric progression. Otherwise, check if it is possible to make the sequence a geometric progression by deleting a single element. If it is possible, print 1. If it is impossible, print 2. Examples Input 4 3 6 12 24 Output 0 Input 4 -8 -16 24 -32 Output 1 Input 4 0 1 2 3 Output 2
{"inputs": ["1\n2\n", "1\n0\n", "1\n1\n", "1\n3\n", "1\n4\n", "1\n-1\n", "1\n-2\n", "1\n-4\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
370
89
coding
Solve the programming task below in a Python markdown code block. Each army fighting the world war Z is denoted with a word consisting any combination of 26 alphabets. An army is a match for his/her opponent if and only if his name is an anagram of his/her opponent. Your job as a programmer is to help find whether two randomly selected members are match for each other or not. (An anagram is a type of word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once. Your task is to find whether a given pair of words is anagrams to each other. Eg: Silent is anagram to listen.) Input: Number of test cases (t ≤ 10). Each test case consists of a pair of strings (length of string ≤ 1000) separated by a single space. Output: For each test case, print ‘Yes’ if they are a match to each other and ‘No’ otherwise. SAMPLE INPUT 2 listen silent mancap pacmaan SAMPLE OUTPUT Yes No
{"inputs": ["3\nman app\na b\nabcde edcba"], "outputs": ["No\nNo\nYes"]}
236
27
coding
Solve the programming task below in a Python markdown code block. In this kata you should simply determine, whether a given year is a leap year or not. In case you don't know the rules, here they are: * years divisible by 4 are leap years * but years divisible by 100 are **not** leap years * but years divisible by 400 are leap years Additional Notes: * Only valid years (positive integers) will be tested, so you don't have to validate them Examples can be found in the test fixture. Also feel free to reuse/extend the following starter code: ```python def isLeapYear(year): ```
{"functional": "_inputs = [[1984], [2000], [2004], [8], [0], [1234], [1100], [1194]]\n_outputs = [[True], [True], [True], [True], [True], [False], [False], [False]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(isLeapYear(*i), o[0])"}
139
215
coding
Solve the programming task below in a Python markdown code block. You recently received a bag of chocolate sticks for Halloween. To prevent you from compulsively eating all the chocolate sticks in one go, your dietician devises the following fun game. In each move, you choose one of the sticks from your bag. Then, you either eat it, or break it into some number of equally-sized parts and save the pieces for later. The lengths of all sticks must always be integers, so breaking a stick into ${d}$ parts is possible only if ${d}$ is a divisor of the stick's length, and $d>1$. Note that this means that a stick of length ${1}$ cannot be broken anymore, and can only be eaten. For example, a chocolate stick of length ${4}$ will be dealt with as shown below. Given the chocolate sticks you received, determine the length of the longest sequence of moves you can perform. Complete the function longestSequence which takes an integer array ${a}$, denoting the lengths of the chocolate sticks, as input. Return the maximum number of moves you can perform to consume the chocolate sticks according the game. Input Format The first line contains one integer $n$ which is the number of chocolate sticks. The second line contains $n$ space-separated integers $a_1,a_2,\ldots,a_n$, the lengths of the chocolate sticks. Constraints $1\leq n\leq100$ $1\leq a_i\leq10^{12}$ Subtasks For $20\%$ of the total score, $a_i\leq10^{6}$ Output Format Print a single integer which is the maximum number of moves you can perform. Sample Input 0 1 6 Sample Output 0 10 Explanation 0 You break a chocolate stick into three equal parts (1 move), then break each of them in half (3 moves), and then eat all six sticks (6 moves). This gives you 10 moves. Sample Input 1 3 1 7 24 Sample Output 1 55 Explanation 1 You make 1 move using the stick of length 1, then 8 moves using the stick of length 7, and in the end 46 moves using the stick of length 24. This gives 55 moves in total.
{"inputs": ["1\n6\n", "3\n1 7 24\n"], "outputs": ["10\n", "55\n"]}
507
33
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. The width of a sequence is the difference between the maximum and minimum elements in the sequence. Given an array of integers nums, return the sum of the widths of all the non-empty subsequences of nums. Since the answer may be very large, return it modulo 109 + 7. A subsequence is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements. For example, [3,6,2,7] is a subsequence of the array [0,3,1,6,2,2,7].   Please complete the following python code precisely: ```python class Solution: def sumSubseqWidths(self, nums: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [2,1,3]) == 6\n assert candidate(nums = [2]) == 0\n\n\ncheck(Solution().sumSubseqWidths)"}
173
50
coding
Solve the programming task below in a Python markdown code block. You are given two arrays of integers $a_1, a_2, \ldots, a_n$ and $b_1, b_2, \ldots, b_n$. Let's define a transformation of the array $a$: Choose any non-negative integer $k$ such that $0 \le k \le n$. Choose $k$ distinct array indices $1 \le i_1 < i_2 < \ldots < i_k \le n$. Add $1$ to each of $a_{i_1}, a_{i_2}, \ldots, a_{i_k}$, all other elements of array $a$ remain unchanged. Permute the elements of array $a$ in any order. Is it possible to perform some transformation of the array $a$ exactly once, so that the resulting array is equal to $b$? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Descriptions of test cases follow. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) — the size of arrays $a$ and $b$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-100 \le a_i \le 100$). The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($-100 \le b_i \le 100$). -----Output----- For each test case, print "YES" (without quotes) if it is possible to perform a transformation of the array $a$, so that the resulting array is equal to $b$. Print "NO" (without quotes) otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 3 3 -1 1 0 0 0 2 1 0 2 5 1 2 3 4 5 1 2 3 4 5 Output YES NO YES -----Note----- In the first test case, we can make the following transformation: Choose $k = 2$. Choose $i_1 = 1$, $i_2 = 2$. Add $1$ to $a_1$ and $a_2$. The resulting array is $[0, 2, 0]$. Swap the elements on the second and third positions. In the second test case there is no suitable transformation. In the third test case we choose $k = 0$ and do not change the order of elements.
{"inputs": ["1\n1\n2\n1\n", "1\n1\n3\n2\n", "1\n1\n1\n2\n", "1\n1\n3\n1\n", "1\n1\n5\n4\n", "1\n1\n4\n3\n", "1\n1\n1\n0\n", "1\n1\n2\n3\n"], "outputs": ["NO\n", "NO\n", "YES\n", "NO\n", "NO\n", "NO\n", "NO\n", "YES\n"]}
606
118
coding
Solve the programming task below in a Python markdown code block. Bob came to a cash & carry store, put n items into his trolley, and went to the checkout counter to pay. Each item is described by its price ci and time ti in seconds that a checkout assistant spends on this item. While the checkout assistant is occupied with some item, Bob can steal some other items from his trolley. To steal one item Bob needs exactly 1 second. What is the minimum amount of money that Bob will have to pay to the checkout assistant? Remember, please, that it is Bob, who determines the order of items for the checkout assistant. Input The first input line contains number n (1 ≤ n ≤ 2000). In each of the following n lines each item is described by a pair of numbers ti, ci (0 ≤ ti ≤ 2000, 1 ≤ ci ≤ 109). If ti is 0, Bob won't be able to steal anything, while the checkout assistant is occupied with item i. Output Output one number — answer to the problem: what is the minimum amount of money that Bob will have to pay. Examples Input 4 2 10 0 20 1 5 1 3 Output 8 Input 3 0 1 0 10 0 100 Output 111
{"inputs": ["3\n0 1\n0 10\n0 100\n", "4\n2 1\n0 0\n1 5\n1 3\n", "4\n2 0\n0 0\n1 5\n2 3\n", "4\n2 1\n0 0\n1 5\n2 3\n", "4\n2 1\n0 0\n1 5\n4 3\n", "4\n2 1\n1 0\n1 5\n4 3\n", "4\n2 1\n1 0\n1 5\n2 3\n", "4\n2 0\n1 0\n1 5\n2 3\n"], "outputs": ["111", "1\n", "0\n", "1\n", "1\n", "1\n", "1\n", "0\n"]}
293
198
coding
Solve the programming task below in a Python markdown code block. Read problems statements in [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. You are given a tree of $N$ nodes rooted at node $1$. Each node $u$ initially has a positive value $a_{u}$ associated with it. You randomly choose one node in the tree and change its value to zero. If any node $u$ has a value equal to $0$, a random node $v$ will be chosen in the subtree of $u$ ($u \neq v$), and the values $a_{u}$ and $a_{v}$ will be swapped, hence the value of the node $v$ will become zero and the process will continue until the zero is on a leaf node. At this point, we will call the tree a final tree. Note: there is exactly one node with a value equal to zero in a final tree and that node is a leaf node. Your task is to tell how many different final trees are possible. Two final trees are different if there exists at least one node $u$, such that the value of the node $u$ in one final tree differs from the value of the node $u$ in the other final tree. Since the answer can be large, print it modulo $10^{9} +7$. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. The first line of each test case contains an integer $N$, the number of nodes. Each of the next $N-1$ lines contains two integers $u$ and $v$, denoting that there is an undirected edge between the node $u$ and the node $v$. The next line contains $N$ integers $a_{1},\ldots, a_{N}$. ------ Output ------ For each test case, output on a new line, number of different final trees possible, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ u, v ≤ N$ $1≤ a_{i}≤ 10^{9}$ The edges form a tree structure. The sum of $N$ over all test cases does not exceed $10^{6}$. ------ Sample Input ------ 3 1 1 4 1 2 2 3 2 4 1 1 1 2 7 1 2 1 3 2 4 2 5 3 6 3 7 1 2 3 4 1 2 1 ------ Sample Output ------ 1 4 14 ------ Explanation ------ For the first test case, the only possible final tree is when node $1$ is chosen and its value becomes 0. For the second test case, the four possible final trees are: - [ 1, 1, 1, 0 ] - [ 1, 1, 0, 2 ] - [ 1, 2, 1, 0 ] - [ 2, 1, 1, 0 ] ( The $i$-th number in the array denotes the value of node $i$ in the final tree).
{"inputs": ["3\n1\n1\n4\n1 2\n2 3\n2 4\n1 1 1 2\n7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n1 2 3 4 1 2 1\n"], "outputs": ["1\n4\n14\n"]}
716
85
coding
Solve the programming task below in a Python markdown code block. Everybody is worried about Rakesh as the boy does not have much knowledge about the real world. He can not go from one place to another on his own. It's high time he learned to explore the city. He is going to a relative's house situated on the other side of the city on his own. As this is his first time, he is carrying a GPS tracker of a special kind. The tracker continuously sends information to the family of Rakesh about his movement. The information is sent using the following four letters: U, D, R, and L. Those letters indicate the moves taken by Rakesh. The city can be considered as a grid. Rakesh starts his journey from (0, 0) position of the grid. His relative's house is situated at (Rx, Ry). Rakesh can move in four directions: up, down, right, or left indicated by U, D, R, and L respectively. Any position of the city with x ordinate negative or greater than M is considered as dangerous. Also, any position of the city with y ordinate negative or greater than N is considered as dangerous. You will be given the total sequence of Rakesh's movement. You need to determine if Rakesh ended up being at his relative's house, at a dangerous place, or at a random place in the city. To make things clear, - U indicates a move that increases position along y-axis by 1 - D indicates a move that decreases position along y-axis by 1 - R indicates a move that increases position along x-axis by 1 - L indicates a move that decreases position along x-axis by 1 Note that we are interested in the position of Rakesh at the end of his journey only. He may visit some dangerous place or his relative's house at some intermediate point but that won't affect the answer. -----Input----- - The first line of the input contains an integer T denoting the number of test cases. The description of each test case follows. - The first line of each test case contains two integers M and N. - The second line contains two integers Rx and Ry. - The third line contains the length of Rakesh's move sequence. - The next line contains the move sequence containing letters U, D, R, and L only with no space. -----Output----- For each test case, print "Case i: ", and then the answer, where i is the testcase number, 1-indexed. The answer should be any of the following three strings: - "REACHED" if Rakesh could reach his relative's house - "DANGER" if Rakesh ended up being in a dangerous place - "SOMEWHERE" if Rakesh ended up being in somewhere safe place in the city other than his relative's place Don't print any quotation mark. Check the sample output. -----Constraints----- - 1 ≤ T ≤ 10 - 0 ≤ M, N ≤ 10000 - 0 ≤ Rx ≤ M - 0 ≤ Ry ≤ N - 0 ≤ Sum of the lengths of all sequences ≤ 10000 -----Example----- Input: 2 20 20 4 5 13 LLUUUUURRRRRR 10 10 3 4 7 UDUDDRR Output: Case 1: REACHED Case 2: DANGER
{"inputs": ["2\n20 20\n4 5\n13\nLLUUUUURRRRRR\n10 10\n3 4\n7\nUDUDDRR"], "outputs": ["Case 1: REACHED\nCase 2: DANGER"]}
744
62
coding
Solve the programming task below in a Python markdown code block. You are given a string $s$. Each character is either 0 or 1. You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met. You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase? -----Input----- The first line contains one integer $t$ ($1 \le t \le 100$) — the number of test cases. Then $t$ lines follow, each representing a test case. Each line contains one string $s$ ($1 \le |s| \le 100$); each character of $s$ is either 0 or 1. -----Output----- Print $t$ integers, where the $i$-th integer is the answer to the $i$-th testcase (the minimum number of 0's that you have to erase from $s$). -----Example----- Input 3 010011 0 1111000 Output 2 0 0 -----Note----- In the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111).
{"inputs": ["2\n01010\n0\n", "2\n01010\n0\n", "2\n01110\n0\n", "2\n01011\n1\n", "2\n10011\n1\n", "2\n01110\n1\n", "2\n01111\n1\n", "2\n11011\n1\n"], "outputs": ["1\n0\n", "1\n0\n", "0\n0\n", "1\n0\n", "2\n0\n", "0\n0\n", "0\n0\n", "1\n0\n"]}
356
150
coding
Solve the programming task below in a Python markdown code block. Given an array, return the difference between the count of even numbers and the count of odd numbers. `0` will be considered an even number. ``` For example: solve([0,1,2,3]) = 0 because there are two even numbers and two odd numbers. Even - Odd = 2 - 2 = 0. ``` Let's now add two letters to the last example: ``` solve([0,1,2,3,'a','b']) = 0. Again, Even - Odd = 2 - 2 = 0. Ignore letters. ``` The input will be an array of lowercase letters and numbers only. In some languages (Haskell, C++, and others), input will be an array of strings: ``` solve ["0","1","2","3","a","b"] = 0 ``` Good luck! If you like this Kata, please try: [Longest vowel chain](https://www.codewars.com/kata/59c5f4e9d751df43cf000035) [Word values](https://www.codewars.com/kata/598d91785d4ce3ec4f000018) Also feel free to reuse/extend the following starter code: ```python def solve(a): ```
{"functional": "_inputs = [[[0, 1, 2, 3]], [[0, 1, 2, 3, 'a', 'b']], [[0, 15, 'z', 16, 'm', 13, 14, 'c', 9, 10, 13, 'u', 4, 3]], [[13, 6, 8, 15, 4, 8, 13]], [[1, 'a', 17, 8, 'e', 3, 'i', 12, 1]], [[5, 15, 16, 10, 6, 4, 16, 't', 13, 'n', 14, 'k', 'n', 0, 'q', 'd', 7, 9]]]\n_outputs = [[0], [0], [0], [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(solve(*i), o[0])"}
307
357
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a string s, where every two consecutive vertical bars '|' are grouped into a pair. In other words, the 1st and 2nd '|' make a pair, the 3rd and 4th '|' make a pair, and so forth. Return the number of '*' in s, excluding the '*' between each pair of '|'. Note that each '|' will belong to exactly one pair.   Please complete the following python code precisely: ```python class Solution: def countAsterisks(self, s: str) -> int: ```
{"functional": "def check(candidate):\n assert candidate(s = \"l|*e*et|c**o|*de|\") == 2\n assert candidate(s = \"iamprogrammer\") == 0\n assert candidate(s = \"yo|uar|e**|b|e***au|tifu|l\") == 5\n\n\ncheck(Solution().countAsterisks)"}
131
88
coding
Solve the programming task below in a Python markdown code block. Rng is going to a festival. The name of the festival is given to you as a string S, which ends with `FESTIVAL`, from input. Answer the question: "Rng is going to a festival of what?" Output the answer. Here, assume that the name of "a festival of s" is a string obtained by appending `FESTIVAL` to the end of s. For example, `CODEFESTIVAL` is a festival of `CODE`. Constraints * 9 \leq |S| \leq 50 * S consists of uppercase English letters. * S ends with `FESTIVAL`. Input Input is given from Standard Input in the following format: S Output Print the answer to the question: "Rng is going to a festival of what?" Examples Input CODEFESTIVAL Output CODE Input CODEFESTIVALFESTIVAL Output CODEFESTIVAL Input YAKINIKUFESTIVAL Output YAKINIKU
{"inputs": ["CODEFESTIVAM", "CPDEFESTIVAM", "BPDEFDSTIVBM", "MBVITSDFEDPB", "MBWITSDFEDPB", "BPCEFDSTIWBM", "CPCEFDSTJWBM", "WPCEFDSTJCAM"], "outputs": ["CODE\n", "CPDE\n", "BPDE\n", "MBVI\n", "MBWI\n", "BPCE\n", "CPCE\n", "WPCE\n"]}
228
108
coding
Solve the programming task below in a Python markdown code block. Read problems statements in mandarin chinese, russian and vietnamese as well. On the occasion of the birthday, Chef bought a large circular cake and decorated it with k candles placed equally spaced around the circumference of the cake, i.e. the lengths of the arcs that connect any two adjacent candles are equal. Chef enumerated the candles from 1 to k in clockwise manner. He then chose two candles, say A and B and lit them up. Now, he left the kitchen and decided to come back in some time with friends to celebrate the birthday. Meanwhile Chef's little brother came to the kitchen and saw the cake. He wants to light up one of the not lit candles C such that angle ACB is an obtuse angle. He believes that such a pattern of lit candles will be auspicious. Can you find out how many candles C he can light up? ------ Input ------ The first line contains a single integer T, the number of test cases. Each test case is described by a single line containing three space-separated integers k, A, B. ------ Output ------ For each test case, output a single line containing one integer: the answer for the test case. ------ Constraints ------ $1 ≤ T ≤ 10^{5} $ $3 ≤ k ≤ 10^{9} $ $1 ≤ A, B ≤ k $ $ A ≠ B $ ----- Sample Input 1 ------ 3 6 1 3 6 1 4 10 1 5 ----- Sample Output 1 ------ 1 0 3 ----- explanation 1 ------ Example case 1. Only candle with number 2 is suitable. Example case 2. No matter how we choose C the angle would have 90 degrees. Example case 3. Candles with numbers 2,3,4 are suitable.
{"inputs": ["3\n6 1 3\n6 1 4\n10 1 5"], "outputs": ["1\n0\n3"]}
407
35
coding
Solve the programming task below in a Python markdown code block. We have an undirected graph G with N vertices numbered 1 to N and N edges as follows: - For each i=1,2,...,N-1, there is an edge between Vertex i and Vertex i+1. - There is an edge between Vertex X and Vertex Y. For each k=1,2,...,N-1, solve the problem below: - Find the number of pairs of integers (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j in G is k. -----Constraints----- - 3 \leq N \leq 2 \times 10^3 - 1 \leq X,Y \leq N - X+1 < Y - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: N X Y -----Output----- For each k=1, 2, ..., N-1 in this order, print a line containing the answer to the problem. -----Sample Input----- 5 2 4 -----Sample Output----- 5 4 1 0 The graph in this input is as follows: There are five pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 1: (1,2)\,,(2,3)\,,(2,4)\,,(3,4)\,,(4,5). There are four pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 2: (1,3)\,,(1,4)\,,(2,5)\,,(3,5). There is one pair (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 3: (1,5). There are no pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 4.
{"inputs": ["4 1 3", "7 3 4", "9 2 4", "3 2 3", "5 1 4", "4 1 2", "8 4 5", "6 1 3"], "outputs": ["4\n2\n0\n", "6\n5\n4\n3\n2\n1\n", "9\n8\n6\n5\n4\n3\n1\n0\n", "2\n1\n", "5\n4\n1\n0\n", "3\n2\n1\n", "7\n6\n5\n4\n3\n2\n1\n", "6\n4\n3\n2\n0\n"]}
463
154
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. There is a bookstore owner that has a store open for n minutes. You are given an integer array customers of length n where customers[i] is the number of the customers that enter the store at the start of the ith minute and all those customers leave after the end of that minute. During certain minutes, the bookstore owner is grumpy. You are given a binary array grumpy where grumpy[i] is 1 if the bookstore owner is grumpy during the ith minute, and is 0 otherwise. When the bookstore owner is grumpy, the customers entering during that minute are not satisfied. Otherwise, they are satisfied. The bookstore owner knows a secret technique to remain not grumpy for minutes consecutive minutes, but this technique can only be used once. Return the maximum number of customers that can be satisfied throughout the day.   Please complete the following python code precisely: ```python class Solution: def maxSatisfied(self, customers: List[int], grumpy: List[int], minutes: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(customers = [1,0,1,2,1,1,7,5], grumpy = [0,1,0,1,0,1,0,1], minutes = 3) == 16\n assert candidate(customers = [1], grumpy = [0], minutes = 1) == 1\n\n\ncheck(Solution().maxSatisfied)"}
225
97
coding
Solve the programming task below in a Python markdown code block. Do you have in mind the good old TicTacToe? Assuming that you get all the data in one array, you put a space around each value, `|` as a columns separator and multiple `-` as rows separator, with something like `["O", "X", " ", " ", "X", " ", "X", "O", " "]` you should be returning this structure (inclusive of new lines): ``` O | X | ----------- | X | ----------- X | O | ``` Now, to spice up things a bit, we are going to expand our board well beyond a trivial `3` x `3` square and we will accept rectangles of big sizes, still all as a long linear array. For example, for `"O", "X", " ", " ", "X", " ", "X", "O", " ", "O"]` (same as above, just one extra `"O"`) and knowing that the length of each row is `5`, you will be returning ``` O | X | | | X ------------------- | X | O | | O ``` And worry not about missing elements, as the array/list/vector length is always going to be a multiple of the width. Also feel free to reuse/extend the following starter code: ```python def display_board(board, width): ```
{"functional": "_inputs = [[['O', 'X', 'X', 'O'], 2], [['O', 'X', ' ', ' ', 'X', ' ', 'X', 'O', ' '], 3], [['O', 'X', ' ', ' ', 'X', ' ', 'X', 'O', ' ', 'O'], 5], [['O', 'X', ' ', ' ', 'X', ' ', 'X', 'O', ' ', 'O'], 2], [['1', '2', '3', '4', '5', '1', '2', '3', '4', '5', '1', '2', '3', '4', '5', '1', '2', '3', '4', '5', '1', '2', '3', '4', '5', '1', '2', '3', '4', '5', '1', '2', '3', '4', '5', '1'], 6]]\n_outputs = [[' O | X \\n-------\\n X | O '], [' O | X | \\n-----------\\n | X | \\n-----------\\n X | O | '], [' O | X | | | X \\n-------------------\\n | X | O | | O '], [' O | X \\n-------\\n | \\n-------\\n X | \\n-------\\n X | O \\n-------\\n | O '], [' 1 | 2 | 3 | 4 | 5 | 1 \\n-----------------------\\n 2 | 3 | 4 | 5 | 1 | 2 \\n-----------------------\\n 3 | 4 | 5 | 1 | 2 | 3 \\n-----------------------\\n 4 | 5 | 1 | 2 | 3 | 4 \\n-----------------------\\n 5 | 1 | 2 | 3 | 4 | 5 \\n-----------------------\\n 1 | 2 | 3 | 4 | 5 | 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(display_board(*i), o[0])"}
299
602
coding
Solve the programming task below in a Python markdown code block. In this problem the input will consist of a number of lines of English text consisting of the letters of the English alphabet, the punctuation marks ' (apostrophe), . (full stop), , (comma), ; (semicolon), :(colon) and white space characters (blank, newline). Your task is print the words in the text in lexicographic order (that is, dictionary order). Each word should appear exactly once in your list. You can ignore the case (for instance, "The" and "the" are to be treated as the same word). There should be no uppercase letters in the output. For example, consider the following candidate for the input text: This is a sample piece of text to illustrate this problem. The corresponding output would read as: a illustrate is of piece problem sample text this to -----Input format----- - The first line of input contains a single integer $N$, indicating the number of lines in the input. - This is followed by $N$ lines of input text. -----Output format----- - The first line of output contains a single integer $M$ indicating the number of distinct words in the given text. - The next $M$ lines list out these words in lexicographic order. -----Constraints----- - $1 \leq N \leq 10000$ - There are at most 80 characters in each line. - There are at the most 1000 distinct words in the given text. -----Sample Input----- 2 This is a sample piece of text to illustrate this problem. -----Sample Output----- 10 a illustrate is of piece problem sample text this to
{"inputs": ["2\nThis is a sample piece of text to illustrate this\nproblem."], "outputs": ["10\na\nillustrate\nis\nof\npiece\nproblem\nsample\ntext\nthis\nto"]}
374
50
coding
Solve the programming task below in a Python markdown code block. You want to make change for $ n $ cents. Assuming that you have infinite supply of coins of 1, 5, 10 and / or 25 cents coins respectively, find the minimum number of coins you need. Constraints * $ 1 \ le n \ le 10 ^ 9 $ Input $ n $ The integer $ n $ is given in a line. output Print the minimum number of coins you need in a line. Examples Input 100 Output 4 Input 54321 Output 2175
{"inputs": ["40", "41", "000", "010", "110", "111", "011", "581"], "outputs": ["3\n", "4\n", "0\n", "1\n", "5\n", "6\n", "2\n", "25\n"]}
136
77
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given n cuboids where the dimensions of the ith cuboid is cuboids[i] = [widthi, lengthi, heighti] (0-indexed). Choose a subset of cuboids and place them on each other. You can place cuboid i on cuboid j if widthi <= widthj and lengthi <= lengthj and heighti <= heightj. You can rearrange any cuboid's dimensions by rotating it to put it on another cuboid. Return the maximum height of the stacked cuboids.   Please complete the following python code precisely: ```python class Solution: def maxHeight(self, cuboids: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(cuboids = [[50,45,20],[95,37,53],[45,23,12]]) == 190\n assert candidate(cuboids = [[38,25,45],[76,35,3]]) == 76\n assert candidate(cuboids = [[7,11,17],[7,17,11],[11,7,17],[11,17,7],[17,7,11],[17,11,7]]) == 102\n\n\ncheck(Solution().maxHeight)"}
154
152
coding
Solve the programming task below in a Python markdown code block. Alice has recently started playing Chess. Her current rating is X. She noticed that when she wins a game, her rating increases by 8 points. Can you help Alice in finding out the minimum number of games she needs to win in order to make her rating greater than or equal to Y? ------ Input Format ------ - The first line of input will contain an integer T — the number of test cases. The description of T test cases follows. - The first line of each test case contains two integers X and Y, as described in the problem statement. ------ Output Format ------ For each test case, output the minimum number of games that Alice needs to win in order to make her rating greater than or equal to Y. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ X ≤ Y ≤ 10^{4}$ ----- Sample Input 1 ------ 4 10 10 10 17 10 18 10 19 ----- Sample Output 1 ------ 0 1 1 2 ----- explanation 1 ------ Test case $1$: Since Alice's current rating $X$ is already equal to her desired rating $Y$, she doesn't need to win any game. Test case $2$: Alice's current rating is $10$. After winning $1$ game, her rating will become $10+8=18$, which is greater than her desired rating of $17$. Thus, she has to win at least $1$ game. Test case $3$: Alice's current rating is $10$. After winning $1$ game, her rating will become $10+8=18$, which is equal to her desired rating of $18$. Thus, she has to win at least $1$ game. Test case $4$: Alice's current rating is $10$. After winning $1$ game, her rating will become $18$, which is less than her desired rating of $19$. She will need to win one more game in order to make her rating $26$, which is greater than $19$. Thus, she has to win at least $2$ games.
{"inputs": ["4\n10 10\n10 17\n10 18\n10 19\n"], "outputs": ["0\n1\n1\n2"]}
476
43
coding
Solve the programming task below in a Python markdown code block. Read problems statements in [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef is participating in an ICPC regional contest, in which there is a total of $N$ problems (numbered $1$ through $N$) with varying difficulties. For each valid $i$, the $i$-th easiest problem is problem $A_{i}$. After a team solves a problem, a balloon with a colour representing that problem is tied next to their desk. Chef is fond of colours in VIBGYOR, which are representative of the problems with numbers $1$ through $7$. The remaining problems have their own representative colours too. Find the minimum number of problems which Chef's team needs to solve in order to get all the balloons for problems $1$ through $7$ (and possibly some other balloons too) tied next to their desk, if you know that Chef's team knows the difficulties of all problems and solves the problems in increasing order of difficulty. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of problems Chef's team needs to solve. ------ Constraints ------ $1 ≤ T ≤ 10,500$ $7 ≤ N ≤ 15$ $1 ≤ A_{i} ≤ N$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct ----- Sample Input 1 ------ 3 7 1 2 3 4 5 7 6 8 8 7 6 5 4 3 2 1 9 7 4 3 5 6 1 8 2 9 ----- Sample Output 1 ------ 7 8 8 ----- explanation 1 ------ Example case 1: Since there are a total of $7$ problems, Chef's team will have to solve all of them. Example case 2: Problems $1$ through $7$ appear among the first $8$ problems. Example case 3: Problems $1$ through $7$ again appear among the first $8$ problems.
{"inputs": ["3\n7\n1 2 3 4 5 7 6\n8\n8 7 6 5 4 3 2 1\n9\n7 4 3 5 6 1 8 2 9"], "outputs": ["7\n8\n8"]}
548
70
coding
Solve the programming task below in a Python markdown code block. Given an array $a$ of length $n$, you can do at most $k$ operations of the following type on it: choose $2$ different elements in the array, add $1$ to the first, and subtract $1$ from the second. However, all the elements of $a$ have to remain non-negative after this operation. What is lexicographically the smallest array you can obtain? An array $x$ is lexicographically smaller than an array $y$ if there exists an index $i$ such that $x_i<y_i$, and $x_j=y_j$ for all $1 \le j < i$. Less formally, at the first index $i$ in which they differ, $x_i<y_i$. -----Input----- The first line contains an integer $t$ ($1 \le t \le 20$) – the number of test cases you need to solve. The first line of each test case contains $2$ integers $n$ and $k$ ($2 \le n \le 100$, $1 \le k \le 10000$) — the number of elements in the array and the maximum number of operations you can make. The second line contains $n$ space-separated integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($0 \le a_i \le 100$) — the elements of the array $a$. -----Output----- For each test case, print the lexicographically smallest array you can obtain after at most $k$ operations. -----Examples----- Input 2 3 1 3 1 4 2 10 1 0 Output 2 1 5 0 1 -----Note----- In the second test case, we start by subtracting $1$ from the first element and adding $1$ to the second. Then, we can't get any lexicographically smaller arrays, because we can't make any of the elements negative.
{"inputs": ["1\n3 1\n1 8 9\n", "1\n3 1\n3 1 9\n", "1\n3 1\n1 0 1\n", "2\n3 1\n3 1 4\n2 10\n1 0\n"], "outputs": ["0 8 10 \n", "2 1 10 \n", "0 0 2 \n", "2 1 5 \n0 1 \n"]}
438
114
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a 2D integer array nums, return all elements of nums in diagonal order as shown in the below images.   Please complete the following python code precisely: ```python class Solution: def findDiagonalOrder(self, nums: List[List[int]]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(nums = [[1,2,3],[4,5,6],[7,8,9]]) == [1,4,2,7,5,3,8,6,9]\n assert candidate(nums = [[1,2,3,4,5],[6,7],[8],[9,10,11],[12,13,14,15,16]]) == [1,6,2,8,7,3,9,4,12,10,5,13,11,14,15,16]\n assert candidate(nums = [[1,2,3],[4],[5,6,7],[8],[9,10,11]]) == [1,4,2,5,3,8,6,9,7,10,11]\n assert candidate(nums = [[1,2,3,4,5,6]]) == [1,2,3,4,5,6]\n\n\ncheck(Solution().findDiagonalOrder)"}
76
243
coding
Solve the programming task below in a Python markdown code block. Chef recently realized that he needs a haircut, and went to his favorite hair salon. At the salon, he found N customers waiting for their haircuts. From his past experience, Chef knows that the salon takes M minutes per customer. Only one person can get their haircut at a time. For how many minutes will Chef have to wait before he can get his haircut? ------ 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. - The first and only line of each test case contains two space-separated integers N and M, as described in the problem. ------ Output Format ------ For each test case, print a single line containing one integer — the number of minutes that Chef will have to wait before he can get his haircut. ------ Constraints ------ $1 ≤ T ≤ 1000$ $0 ≤ N ≤ 1000$ $1 ≤ M ≤ 1000$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 2 2 15 0 15 ----- Sample Output 1 ------ 30 0 ----- explanation 1 ------ Test case $1$: There are $2$ customers waiting for the haircut and the salon will take $15$ minutes for each customer, hence Chef will have to wait for $30$ minutes before he can get his haircut. Test case $2$: There are $0$ customers waiting for the haircut, therefore the Chef will not have wait to get the haircut.
{"inputs": ["2\n2 15\n0 15\n"], "outputs": ["30\n0\n"]}
349
27
coding
Solve the programming task below in a Python markdown code block. Naturally, the magical girl is very good at performing magic. She recently met her master wizard Devu, who gifted her R potions of red liquid, B potions of blue liquid, and G potions of green liquid. - The red liquid potions have liquid amounts given by r[1], ..., r[R] liters. - The green liquid potions have liquid amounts given by g[1], ..., g[G] liters. - The blue liquid potions have liquid amounts given by b[1], ..., b[B] liters. She want to play with the potions by applying magic tricks on them. In a single magic trick, she will choose a particular color. Then she will pick all the potions of the chosen color and decrease the amount of liquid in them to half (i.e. if initial amount of liquid is x, then the amount after decrement will be x / 2 where division is integer division, e.g. 3 / 2 = 1 and 4 / 2 = 2). Because she has to go out of station to meet her uncle Churu, a wannabe wizard, only M minutes are left for her. In a single minute, she can perform at most one magic trick. Hence, she can perform at most M magic tricks. She would like to minimize the maximum amount of liquid among all of Red, Green and Blue colored potions. Formally Let v be the maximum value of amount of liquid in any potion. We want to minimize the value of v. Please help her. -----Input----- First line of the input contains an integer T denoting the number of test cases. Then for each test case, we have four lines. The first line contains four space separated integers R, G, B, M. The next 3 lines will describe the amount of different color liquids (r, g, b), which are separated by space. -----Output----- For each test case, print a single integer denoting the answer of the problem. -----Constraints----- - 1 ≤ T ≤ 1000 - 1 ≤ R, G, B, M ≤ 100 - 1 ≤ r[i], g[i], b[i] ≤ 10^9 -----Example----- Input: 3 1 1 1 1 1 2 3 1 1 1 1 2 4 6 3 2 2 2 1 2 3 2 4 6 8 Output: 2 4 4 -----Explanation----- Example case 1. Magical girl can pick the blue potion and make its liquid amount half. So the potions will now have amounts 1 2 1. Maximum of these values is 2. Hence answer is 2.
{"inputs": ["3\n1 1 1 1\n1\n2\n3\n1 1 1 1\n2\n4\n6\n3 2 2 2\n1 2 3\n2 4\n6 8", "3\n1 1 1 1\n1\n2\n3\n1 1 1 1\n3\n4\n6\n3 2 2 2\n1 2 3\n2 4\n6 8", "3\n1 1 1 1\n1\n4\n5\n1 1 1 1\n3\n4\n6\n3 2 2 2\n2 2 3\n2 7\n6 8", "3\n1 1 1 1\n1\n4\n5\n1 1 1 2\n3\n4\n6\n3 2 2 2\n2 2 3\n2 7\n6 8", "3\n2 1 1 1\n1\n4\n5\n1 1 1 2\n3\n4\n6\n3 2 2 2\n2 2 5\n2 7\n6 8", "3\n1 1 1 1\n1\n4\n3\n1 1 1 1\n2\n4\n6\n3 2 2 2\n1 2 3\n2 4\n6 8", "3\n1 1 1 1\n1\n2\n3\n1 1 1 1\n3\n0\n6\n3 2 2 2\n1 2 3\n2 4\n6 8", "3\n2 1 1 0\n1\n4\n5\n1 1 1 2\n3\n4\n6\n3 2 2 2\n2 2 3\n2 7\n6 8"], "outputs": ["2\n4\n4", "2\n4\n4\n", "4\n4\n4\n", "4\n3\n4\n", "4\n3\n5\n", "3\n4\n4\n", "2\n3\n4\n", "5\n3\n4\n"]}
591
493
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given two integer arrays nums1 and nums2 of length n, count the pairs of indices (i, j) such that i < j and nums1[i] + nums1[j] > nums2[i] + nums2[j]. Return the number of pairs satisfying the condition.   Please complete the following python code precisely: ```python class Solution: def countPairs(self, nums1: List[int], nums2: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums1 = [2,1,2,1], nums2 = [1,2,1,2]) == 1\n assert candidate(nums1 = [1,10,6,2], nums2 = [1,4,1,5]) == 5\n\n\ncheck(Solution().countPairs)"}
112
82
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given the array restaurants where  restaurants[i] = [idi, ratingi, veganFriendlyi, pricei, distancei]. You have to filter the restaurants using three filters. The veganFriendly filter will be either true (meaning you should only include restaurants with veganFriendlyi set to true) or false (meaning you can include any restaurant). In addition, you have the filters maxPrice and maxDistance which are the maximum value for price and distance of restaurants you should consider respectively. Return the array of restaurant IDs after filtering, ordered by rating from highest to lowest. For restaurants with the same rating, order them by id from highest to lowest. For simplicity veganFriendlyi and veganFriendly take value 1 when it is true, and 0 when it is false.   Please complete the following python code precisely: ```python class Solution: def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(restaurants = [[1,4,1,40,10],[2,8,0,50,5],[3,8,1,30,4],[4,10,0,10,3],[5,1,1,15,1]], veganFriendly = 1, maxPrice = 50, maxDistance = 10) == [3,1,5] \n assert candidate(restaurants = [[1,4,1,40,10],[2,8,0,50,5],[3,8,1,30,4],[4,10,0,10,3],[5,1,1,15,1]], veganFriendly = 0, maxPrice = 50, maxDistance = 10) == [4,3,2,1,5]\n assert candidate(restaurants = [[1,4,1,40,10],[2,8,0,50,5],[3,8,1,30,4],[4,10,0,10,3],[5,1,1,15,1]], veganFriendly = 0, maxPrice = 30, maxDistance = 3) == [4,5]\n\n\ncheck(Solution().filterRestaurants)"}
227
301
coding
Solve the programming task below in a Python markdown code block. Complete the function that returns an array of length `n`, starting with the given number `x` and the squares of the previous number. If `n` is negative or zero, return an empty array/list. ## Examples ``` 2, 5 --> [2, 4, 16, 256, 65536] 3, 3 --> [3, 9, 81] ``` Also feel free to reuse/extend the following starter code: ```python def squares(x, n): ```
{"functional": "_inputs = [[2, 5], [3, 3], [5, 3], [10, 4], [2, 0], [2, -4]]\n_outputs = [[[2, 4, 16, 256, 65536]], [[3, 9, 81]], [[5, 25, 625]], [[10, 100, 10000, 100000000]], [[]], [[]]]\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(squares(*i), o[0])"}
131
260
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an integer n. You roll a fair 6-sided dice n times. Determine the total number of distinct sequences of rolls possible such that the following conditions are satisfied: The greatest common divisor of any adjacent values in the sequence is equal to 1. There is at least a gap of 2 rolls between equal valued rolls. More formally, if the value of the ith roll is equal to the value of the jth roll, then abs(i - j) > 2. Return the total number of distinct sequences possible. Since the answer may be very large, return it modulo 109 + 7. Two sequences are considered distinct if at least one element is different.   Please complete the following python code precisely: ```python class Solution: def distinctSequences(self, n: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(n = 4) == 184\n assert candidate(n = 2) == 22\n\n\ncheck(Solution().distinctSequences)"}
188
47
coding
Solve the programming task below in a Python markdown code block. In Berland each high school student is characterized by academic performance — integer value between 1 and 5. In high school 0xFF there are two groups of pupils: the group A and the group B. Each group consists of exactly n students. An academic performance of each student is known — integer value between 1 and 5. The school director wants to redistribute students between groups so that each of the two groups has the same number of students whose academic performance is equal to 1, the same number of students whose academic performance is 2 and so on. In other words, the purpose of the school director is to change the composition of groups, so that for each value of academic performance the numbers of students in both groups are equal. To achieve this, there is a plan to produce a series of exchanges of students between groups. During the single exchange the director selects one student from the class A and one student of class B. After that, they both change their groups. Print the least number of exchanges, in order to achieve the desired equal numbers of students for each academic performance. -----Input----- The first line of the input contains integer number n (1 ≤ n ≤ 100) — number of students in both groups. The second line contains sequence of integer numbers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 5), where a_{i} is academic performance of the i-th student of the group A. The third line contains sequence of integer numbers b_1, b_2, ..., b_{n} (1 ≤ b_{i} ≤ 5), where b_{i} is academic performance of the i-th student of the group B. -----Output----- Print the required minimum number of exchanges or -1, if the desired distribution of students can not be obtained. -----Examples----- Input 4 5 4 4 4 5 5 4 5 Output 1 Input 6 1 1 1 1 1 1 5 5 5 5 5 5 Output 3 Input 1 5 3 Output -1 Input 9 3 2 5 5 2 3 3 3 2 4 1 4 1 1 2 4 4 1 Output 4
{"inputs": ["1\n5\n3\n", "1\n1\n2\n", "1\n1\n1\n", "1\n1\n1\n", "1\n1\n2\n", "1\n1\n3\n", "1\n2\n3\n", "1\n2\n4\n"], "outputs": ["-1\n", "-1\n", "0\n", "0\n", "-1\n", "-1\n", "-1\n", "-1\n"]}
505
103
coding
Solve the programming task below in a Python markdown code block. A format for expressing an ordered list of integers is to use a comma separated list of either * individual integers * or a range of integers denoted by the starting integer separated from the end integer in the range by a dash, '-'. The range includes all integers in the interval including both endpoints. It is not considered a range unless it spans at least 3 numbers. For example "12,13,15-17" Complete the solution so that it takes a list of integers in increasing order and returns a correctly formatted string in the range format. **Example:** ```python solution([-6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20]) # returns "-6,-3-1,3-5,7-11,14,15,17-20" ``` ```C# RangeExtraction.Extract(new[] {-6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20}); # returns "-6,-3-1,3-5,7-11,14,15,17-20" ``` *Courtesy of rosettacode.org* Also feel free to reuse/extend the following starter code: ```python def solution(args): ```
{"functional": "_inputs = [[[-6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20]], [[-3, -2, -1, 2, 10, 15, 16, 18, 19, 20]], [[1, 2, 3, 4, 5]]]\n_outputs = [['-6,-3-1,3-5,7-11,14,15,17-20'], ['-3--1,2,10,15,16,18-20'], ['1-5']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(solution(*i), o[0])"}
379
324
coding
Solve the programming task below in a Python markdown code block. Codex is about to start and Ram has not done dinner yet. So, he quickly goes to hostel mess and finds a long queue in front of food counter. But somehow he manages to take the food plate and reaches in front of the queue. The plates are divided into sections such that it has 2 rows and N columns. Due to the crowd Ram knows that when he will get out of the queue the food will get mixed. So, he don't want to put food in two consecutive sections column wise but he can put food in two consecutive sections row wise as spacing between the rows is good enough to take food out of the queue safely. If he doesn't like the food, he will not take food. You are given N and you have to tell the number of ways in which food can be taken without getting it mixed. Input Format: First line contains T which denotes number of test cases and each test case represents a single line containing the value of N. Output Format Output the total ways for each input. SAMPLE INPUT 2 1 3 SAMPLE OUTPUT 4 25 Explanation Explanation: Case 1: Plate has 2 rows and 1 column each. So, Ram can Put food in upper section. Put food in lower section. Put food in both section. Do Not food in either section. Case 2: Plate has 2 rows and 3 columns. So, possible ways for one row are PNN, PNP, NNN, NPN, NNP where P represents food taken and N represents food not taken. Total possible ways are 25 because a way to put food in 1 row can correspond to any of 5 ways on other row.
{"inputs": ["19\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19"], "outputs": ["4\n9\n25\n64\n169\n441\n1156\n3025\n7921\n20736\n54289\n142129\n372100\n974169\n2550409\n6677056\n17480761\n45765225\n119814916"]}
367
169
coding
Solve the programming task below in a Python markdown code block. Two people are playing a game with a string $s$, consisting of lowercase latin letters. On a player's turn, he should choose two consecutive equal letters in the string and delete them. For example, if the string is equal to "xaax" than there is only one possible turn: delete "aa", so the string will become "xx". A player not able to make a turn loses. Your task is to determine which player will win if both play optimally. -----Input----- The only line contains the string $s$, consisting of lowercase latin letters ($1 \leq |s| \leq 100\,000$), where $|s|$ means the length of a string $s$. -----Output----- If the first player wins, print "Yes". If the second player wins, print "No". -----Examples----- Input abacaba Output No Input iiq Output Yes Input abba Output No -----Note----- In the first example the first player is unable to make a turn, so he loses. In the second example first player turns the string into "q", then second player is unable to move, so he loses.
{"inputs": ["a\n", "a\n", "`\n", "_\n", "aa\n", "aa\n", "`a\n", "a`\n"], "outputs": ["No\n", "No\n", "No\n", "No\n", "Yes\n", "Yes\n", "No\n", "No\n"]}
266
71
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Mandarin], [Bengali], [Russian], and [Vietnamese] as well. Chef aims to be the richest person in Chefland by his new restaurant franchise. Currently, his assets are worth $A$ billion dollars and have no liabilities. He aims to increase his assets by $X$ billion dollars per year. Also, all the richest people in Chefland are not planning to grow and maintain their current worth. To be the richest person in Chefland, he needs to be worth at least $B$ billion dollars. How many years will it take Chef to reach his goal if his value increases by $X$ billion dollars each year? ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. Each test case contains a single line of input, three integers $A$, $B$, $X$. ------ Output ------ For each test case, output in a single line the answer to the problem. ------ Constraints ------ $1 ≤ T ≤ 21\ 000$ $100 ≤ A < B ≤ 200$ $1 ≤ X ≤ 50$ $X$ divides $B - A$ ------ Subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 3 100 200 10 111 199 11 190 200 10 ----- Sample Output 1 ------ 10 8 1 ----- explanation 1 ------ Test Case $1$: Chef needs to increase his worth by $200 - 100 = 100$ billion dollars and his increment per year being $10$ billion dollars, so it will take him $\frac{100}{10} = 10$ years to do so. Test Case $2$: Chef needs to increase his worth by $199 - 111 = 88$ billion dollars and his increment per year being $11$ billion dollars, so it will take him $\frac{88}{11} = 8$ years to do so. Test Case $3$: Chef needs to increase his worth by $200 - 190 = 10$ billion dollars and his increment per year being $10$ billion dollars, so it will take him $\frac{10}{10} = 1$ year to do so.
{"inputs": ["3\n100 200 10\n111 199 11\n190 200 10"], "outputs": ["10\n8\n1"]}
552
50
coding
Solve the programming task below in a Python markdown code block. In a class of N students, a class test was held. The i^{th} student scored A_{i} marks. It is also known that the scores of all students were distinct. A student passes the test if their score is strictly greater than the passing mark. Given that exactly X students pass in the test, find the maximum value of the passing mark of the test. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and X — the number of students in the class and the number of students that passed in the test. - The next line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}, where the i^{th} integer denotes the marks of the i^{th} student. ------ Output Format ------ For each test case, output on a new line, the maximum value of the passing mark of the test. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100$ $1 ≤ X ≤ N$ $1 ≤ A_{i} ≤ 100$ - All elements of array $A$ are distinct. ----- Sample Input 1 ------ 3 2 2 5 1 4 1 5 1 7 4 4 3 15 70 100 31 ----- Sample Output 1 ------ 0 6 30 ----- explanation 1 ------ Test case $1$: Since both students pass the test, both students scored greater than passing marks. The maximum value of passing marks can be $0$, as both students have marks greater than $0$. Test case $2$: Only one student passes the test. Thus, the third student has passed the test by scoring $7$ marks. The passing marks of the test is $6$. Test case $3$: Since three students pass the test, students $2, 3,$ and $4$ scored greater than passing marks. The maximum value of passing marks can be $30$, three students have marks greater than $30$.
{"inputs": ["3\n2 2\n5 1\n4 1\n5 1 7 4\n4 3\n15 70 100 31\n"], "outputs": ["0\n6\n30\n"]}
486
56
coding
Solve the programming task below in a Python markdown code block. DestinyLand is a really fun place for kids. It is composed of N islands, numbered from 1 to N. The company is planning to divide these N islands into exactly M area, such that, each island will belong to one of the M areas, and each area will contain at least one island. Now the company is going to build bridges between the islands. Each area should be isolated from another. So the bridges can be built, only if both the endpoint islands belong to the same area. That is, in total they will build exactly n-m bridges to maintain connectivity within each area. (Of course, they won't build more than N-M bridges because that cost too much!) For area to area connections, the company planned to build a harbor to one chosen island in each area. Due to heavy traffic, each of the chosen islands should NOT directly connect with more than K bridges. Can you help the company figure out how many bridge and harbor construction layouts are possible? Note that all harbors are the same, all bridges are the same, but islands are distinct. Input Format The first line contains an integer T indicating the number of test cases. For each test case, there are four space separated integers N M K MOD Output Format For each test case, print the number of layouts modulo MOD Constraints 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{9} 1 ≤ M , K ≤ min(N, 100) 1 ≤ MOD ≤ 2*10^{9} Sample Input 3 3 2 1 1000003 3 1 2 1234567891 3 1 1 314159 Sample Output 6 9 6
{"inputs": ["3\n3 2 1 1000003\n3 1 2 1234567891\n3 1 1 314159\n"], "outputs": ["6\n9\n6\n"]}
388
62
coding
Solve the programming task below in a Python markdown code block. You have a statistic of price changes for one product represented as an array of $n$ positive integers $p_0, p_1, \dots, p_{n - 1}$, where $p_0$ is the initial price of the product and $p_i$ is how the price was increased during the $i$-th month. Using these price changes you are asked to calculate the inflation coefficients for each month as the ratio of current price increase $p_i$ to the price at the start of this month $(p_0 + p_1 + \dots + p_{i - 1})$. Your boss said you clearly that the inflation coefficients must not exceed $k$ %, so you decided to increase some values $p_i$ in such a way, that all $p_i$ remain integers and the inflation coefficients for each month don't exceed $k$ %. You know, that the bigger changes — the more obvious cheating. That's why you need to minimize the total sum of changes. What's the minimum total sum of changes you need to make all inflation coefficients not more than $k$ %? -----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 two integers $n$ and $k$ ($2 \le n \le 100$; $1 \le k \le 100$) — the length of array $p$ and coefficient $k$. The second line of each test case contains $n$ integers $p_0, p_1, \dots, p_{n - 1}$ ($1 \le p_i \le 10^9$) — the array $p$. -----Output----- For each test case, print the minimum total sum of changes you need to make all inflation coefficients not more than $k$ %. -----Examples----- Input 2 4 1 20100 1 202 202 3 100 1 1 1 Output 99 0 -----Note----- In the first test case, you can, for example, increase $p_0$ by $50$ and $p_1$ by $49$ and get array $[20150, 50, 202, 202]$. Then you get the next inflation coefficients: $\frac{50}{20150} \le \frac{1}{100}$; $\frac{202}{20150 + 50} \le \frac{1}{100}$; $\frac{202}{20200 + 202} \le \frac{1}{100}$; In the second test case, you don't need to modify array $p$, since the inflation coefficients are already good: $\frac{1}{1} \le \frac{100}{100}$; $\frac{1}{1 + 1} \le \frac{100}{100}$;
{"inputs": ["1\n3 3\n7 2 3\n", "1\n3 3\n7 2 6\n", "1\n3 3\n97 2 2\n", "1\n3 3\n97 2 2\n", "1\n3 3\n17 2 2\n", "1\n3 2\n25 2 2\n", "1\n3 1\n94 2 2\n", "1\n3 2\n25 2 4\n"], "outputs": ["91\n", "191\n", "0\n", "0\n", "50\n", "75\n", "106\n", "173\n"]}
690
165
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Russian here Chef has a special affection for sets of binary strings of equal length which have same numbers of 1's. Given three integers n, k and m, your task is to find the the lexicographically m^{th} smallest string among strings which have length n and have k 1's. If no such string exists output -1. ------ Tips: ------ To see what lexicographic order means . See http://en.wikipedia.org/wiki/Lexicographical_{order} ------ Input ------ Input description. The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows: The first and only line of each test case contains three space separated integers N , K and M ------ Output ------ For each test case output the answer on a separate line . ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 350$ $1 ≤ K ≤ N$ ------ Example ------ Input: 1 3 2 2 Output: 101 ------ Explanation ------ Example case 1. The set of strings in lexicographic order is "011", "101", and "110" ------ Scoring ------ Subtask 1 (41 point): $1 ≤ N ≤ 20$ Subtask 2 (59 points): $1 ≤ N ≤ 350$
{"inputs": ["1\n3 2 2", "1\n3 2 3", "1\n3 3 3", "1\n6 3 3", "1\n6 4 3", "1\n6 4 6", "1\n3 1 3", "1\n9 3 3"], "outputs": ["101", "110\n", "-1\n", "001101\n", "011011\n", "100111\n", "100\n", "000001101\n"]}
323
138
coding
Solve the programming task below in a Python markdown code block. For a given two numbers your mission is to derive a function that evaluates whether two given numbers are **abundant**, **deficient** or **perfect** and whether together they are **amicable**. ### Abundant Numbers An abundant number or excessive number is a number for which the sum of its proper divisors is greater than the number itself. The integer 12 is the first abundant number. Its proper divisors are 1, 2, 3, 4 and 6 for a total of 16 (> 12). ### Deficient Numbers A deficient number is a number for which the sum of its proper divisors is less than the number itself. The first few deficient numbers are: 1, 2, 3, 4, 5, 7, 8, 9. ### Perfect Numbers A perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself. The first perfect number is 6, because 1, 2, and 3 are its proper positive divisors, and 1 + 2 + 3 = 6. ### Amicable Numbers Amicable numbers are two different numbers so related that the sum of the proper divisors of each is equal to the other number. (A proper divisor of a number is a positive factor of that number other than the number itself. For example, the proper divisors of 6 are 1, 2, and 3.) For example, the smallest pair of amicable numbers is (220, 284); for the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110, of which the sum is 284; and the proper divisors of 284 are 1, 2, 4, 71 and 142, of which the sum is 220. ### The Function For a given two numbers, derive function `deficientlyAbundantAmicableNumbers(num1, num2)` which returns a string with first and second word either abundant or deficient depending on whether `num1` or `num2` are abundant, deficient or perfect. The string should finish with either amicable or not amicable depending on the relationship between `num1` and `num2`. e.g. `deficientlyAbundantAmicableNumbers(220, 284)` returns `"abundant deficient amicable"` as 220 is an abundant number, 284 is a deficient number and amicable because 220 and 284 are an amicable number pair. See Part 1 - [Excessively Abundant Numbers](http://www.codewars.com/kata/56a75b91688b49ad94000015) See Part 2 - [The Most Amicable of Numbers](http://www.codewars.com/kata/56b5ebaa26fd54188b000018) Also feel free to reuse/extend the following starter code: ```python def deficiently_abundant_amicable_numbers(n1, n2): ```
{"functional": "_inputs = [[220, 284], [220, 280], [1184, 1210], [220221, 282224], [10744, 10856], [299920, 9284], [999220, 2849], [139815, 122265], [496, 28], [8128, 8128]]\n_outputs = [['abundant deficient amicable'], ['abundant abundant not amicable'], ['abundant deficient amicable'], ['deficient abundant not amicable'], ['abundant deficient amicable'], ['abundant deficient not amicable'], ['abundant deficient not amicable'], ['deficient abundant amicable'], ['perfect perfect not amicable'], ['perfect perfect not amicable']]\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(deficiently_abundant_amicable_numbers(*i), o[0])"}
741
363
coding
Solve the programming task below in a Python markdown code block. The integers 14 and 15, are contiguous (1 the difference between them, obvious) and have the same number of divisors. ```python 14 ----> 1, 2, 7, 14 (4 divisors) 15 ----> 1, 3, 5, 15 (4 divisors) ``` The next pair of contiguous integers with this property is 21 and 22. ```python 21 -----> 1, 3, 7, 21 (4 divisors) 22 -----> 1, 2, 11, 22 (4 divisors) ``` We have 8 pairs of integers below 50 having this property, they are: ```python [[2, 3], [14, 15], [21, 22], [26, 27], [33, 34], [34, 35], [38, 39], [44, 45]] ``` Let's see now the integers that have a difference of 3 between them. There are seven pairs below 100: ```python [[2, 5], [35, 38], [55, 58], [62, 65], [74, 77], [82, 85], [91, 94]] ``` Let's name, diff, the difference between two integers, next and prev, (diff = next - prev) and nMax, an upper bound of the range. We need a special function, count_pairsInt(), that receives two arguments, diff and nMax and outputs the amount of pairs of integers that fulfill this property, all of them being smaller (not smaller or equal) than nMax. Let's see it more clearly with examples. ```python count_pairsInt(1, 50) -----> 8 (See case above) count_pairsInt(3, 100) -----> 7 (See case above) ``` Happy coding!!! Also feel free to reuse/extend the following starter code: ```python def count_pairs_int(diff, n_max): ```
{"functional": "_inputs = [[1, 50], [3, 100], [3, 200], [6, 350], [6, 1000], [7, 1500], [7, 2500], [7, 3000], [9, 4000], [9, 5000], [11, 5000]]\n_outputs = [[8], [7], [18], [86], [214], [189], [309], [366], [487], [622], [567]]\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_pairs_int(*i), o[0])"}
488
293
coding
Solve the programming task below in a Python markdown code block. Your task is to create function```isDivideBy``` (or ```is_divide_by```) to check if an integer number is divisible by each out of two arguments. A few cases: ``` (-12, 2, -6) -> true (-12, 2, -5) -> false (45, 1, 6) -> false (45, 5, 15) -> true (4, 1, 4) -> true (15, -5, 3) -> true ``` Also feel free to reuse/extend the following starter code: ```python def is_divide_by(number, a, b): ```
{"functional": "_inputs = [[8, 2, 4], [12, -3, 4], [8, 3, 4], [48, 2, -5], [-100, -25, 10], [10000, 5, -3], [4, 4, 2], [5, 2, 3], [-96, 25, 17], [33, 1, 33]]\n_outputs = [[True], [True], [False], [False], [True], [False], [True], [False], [False], [True]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(is_divide_by(*i), o[0])"}
174
285
coding
Solve the programming task below in a Python markdown code block. # Introduction: Reversi is a game usually played by 2 people on a 8x8 board. Here we're only going to consider a single 8x1 row. Players take turns placing pieces, which are black on one side and white on the other, onto the board with their colour facing up. If one or more of the opponents pieces are sandwiched by the piece just played and another piece of the current player's colour, the opponents pieces are flipped to the current players colour. Note that the flipping stops when the first piece of the player's colour is reached. # Task: Your task is to take an array of moves and convert this into a string representing the state of the board after all those moves have been played. # Input: The input to your function will be an array of moves. Moves are represented by integers from 0 to 7 corresponding to the 8 squares on the board. Black plays first, and black and white alternate turns. Input is guaranteed to be valid. (No duplicates, all moves in range, but array may be empty) # Output: 8 character long string representing the final state of the board. Use '*' for black and 'O' for white and '.' for empty. # Examples: ```python reversi_row([]) # '........' reversi_row([3]) # '...*....' reversi_row([3,4]) # '...*O...' reversi_row([3,4,5]) # '...***..' ``` Also feel free to reuse/extend the following starter code: ```python def reversi_row(moves): ```
{"functional": "_inputs = [[[]], [[0]], [[0, 1]], [[0, 7, 4]], [[3]], [[3, 4]], [[3, 4, 5]], [[2, 1, 0]], [[0, 1, 4, 3, 2]], [[0, 1, 7, 2, 3]], [[3, 2, 7, 1, 0]], [[3, 4, 5, 6, 0, 2]], [[0, 1, 2, 3, 4, 5, 6, 7]], [[7, 0, 1]], [[0, 7, 6]], [[1, 0, 2, 3, 4, 5, 6, 7]], [[5, 1, 3, 4, 6]], [[1, 7, 0, 5, 6, 4]]]\n_outputs = [['........'], ['*.......'], ['*O......'], ['*...*..O'], ['...*....'], ['...*O...'], ['...***..'], ['***.....'], ['*****...'], ['****...*'], ['****...*'], ['*.OOOOO.'], ['*******O'], ['O*.....*'], ['*.....*O'], ['OOOOOOOO'], ['.O.*O**.'], ['**..OO*O']]\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(reversi_row(*i), o[0])"}
360
456
coding
Solve the programming task below in a Python markdown code block. Jigar got a sequence of n positive integers as his birthday present! He likes consecutive subsequences whose sum is divisible by k. He asks you to write a program to count them for him. Input Format The first line contains T, the number of testcases. T testcases follow. Each testcase consists of 2 lines. The first line contains n and k separated by a single space. And the second line contains n space separated integers. Output Format For each test case, output the number of consecutive subsequenences whose sum is divisible by k in a newline. Constraints 1 ≤ T ≤ 20 1 ≤ n ≤ 10^{6} 1 ≤ k ≤ 100 1 ≤ a[i] ≤ 10^{4} Sample Input 2 5 3 1 2 3 4 1 6 2 1 2 1 2 1 2 Sample Output 4 9 Explanation For 1 2 3 4 1 there exists, 4 subsequences whose sum is divisible by 3, they are 3 1 2 1 2 3 2 3 4 For 1 2 1 2 1 2 there exists, 9 subsequences whose sum is divisible by 2, they are 2 2 2 1 2 1 1 2 1 1 2 1 2 2 1 2 1 1 2 1 2 2 1 2 1 2
{"inputs": ["2\n5 3\n1 2 3 4 1\n6 2\n1 2 1 2 1 2\n"], "outputs": ["4\n9\n"]}
350
46
coding
Solve the programming task below in a Python markdown code block. There is a house with 4 levels. In that house there is an elevator. You can program this elevator to go up or down, depending on what button the user touches inside the elevator. Valid levels must be only these numbers: `0,1,2,3` Valid buttons must be only these strings: `'0','1','2','3'` Possible return values are these numbers: `-3,-2,-1,0,1,2,3` If the elevator is on the ground floor(0th level) and the user touches button '2' the elevator must go 2 levels up, so our function must return 2. If the elevator is on the 3rd level and the user touches button '0' the elevator must go 3 levels down, so our function must return -3. If the elevator is on the 2nd level, and the user touches button '2' the elevator must remain on the same level, so we return 0. We cannot endanger the lives of our passengers, so if we get erronous inputs, our elevator must remain on the same level. So for example: - `goto(2,'4')` must return 0, because there is no button '4' in the elevator. - `goto(4,'0')` must return 0, because there is no level 4. - `goto(3,undefined)` must return 0. - `goto(undefined,'2')` must return 0. - `goto([],'2')` must return 0 because the type of the input level is array instead of a number. - `goto(3,{})` must return 0 because the type of the input button is object instead of a string. Also feel free to reuse/extend the following starter code: ```python def goto(level,button): ```
{"functional": "_inputs = [[0, '0'], [0, '1'], [0, '2'], [0, '3'], [1, '0'], [1, '1'], [1, '2'], [1, '3'], [2, '0'], [2, '1'], [2, '2'], [2, '3'], [3, '0'], [3, '1'], [3, '2'], [3, '3'], [0, '4'], [0, None], [1, '4'], [1, None], [2, '4'], [2, None], [3, '4'], [3, None], [4, '2'], [None, '2'], [[], '2'], [3, {}], ['2', '3'], [2, 3], [1.5, '3'], ['length', 'length']]\n_outputs = [[0], [1], [2], [3], [-1], [0], [1], [2], [-2], [-1], [0], [1], [-3], [-2], [-1], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(goto(*i), o[0])"}
400
432
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese and Russian. Devu is learning Combinatorics in his college. He find it very interesting to calculate number of ways of going to point (c,d) from point (a,b) in co-ordinate plane. We can take horizontal and vertical steps only and can not visit at a point twice. In a step, you can move one unit only. We have to reach to the point (c,d) from the point (a,b) using abs(a-c)+ abs(b-d) steps only. Devu has two sets of points. Set A contains points having X co-ordinate 0 and Y co-ordinates varying from 1 to N(both inclusive). Set B contains points having X co-ordinate K and Y co-ordinates varying from 1 to N(both inclusive). Both sets contains N number of integral points. He wants to calculate the sum of number of ways to going to the each point of set B from the each point of set A . As the answer can be large, print it modulo 1000000007. ------ Input ------ First line of input contains an integer T denoting number of test cases. Next T lines will contain pair of integers N and K ------ Output ------ For each test case, print a single integer representing the answer of that test case. ------ Constraints ------ Subtask #1: 10 points $1 ≤ T ≤ 20, 1 ≤ N ,K ≤ 1000$ Subtask #1: 10 points $1 ≤ T ≤ 20, 1 ≤ N ,K ≤ 10^{6}$ Subtask #3: 80 points $1 ≤ T ≤ 10000, 1 ≤ N,K ≤ 10^{6}$ ----- Sample Input 1 ------ 2 2 2 4 5 ----- Sample Output 1 ------ 8 236 ----- explanation 1 ------ For the first sample case, ways[(0,1)->(2,1)]= 1 ways[(0,2)->(2,2)]= 1 ways[(0,1)->(2,2)]= 3 ways[(0,2)->(2,1)]= 3 So, the total number of ways = 8.
{"inputs": ["2\n2 2\n4 5"], "outputs": ["8\n236"]}
520
24
coding
Solve the programming task below in a Python markdown code block. Emuskald is an avid horticulturist and owns the world's longest greenhouse — it is effectively infinite in length. Over the years Emuskald has cultivated n plants in his greenhouse, of m different plant species numbered from 1 to m. His greenhouse is very narrow and can be viewed as an infinite line, with each plant occupying a single point on that line. Emuskald has discovered that each species thrives at a different temperature, so he wants to arrange m - 1 borders that would divide the greenhouse into m sections numbered from 1 to m from left to right with each section housing a single species. He is free to place the borders, but in the end all of the i-th species plants must reside in i-th section from the left. Of course, it is not always possible to place the borders in such way, so Emuskald needs to replant some of his plants. He can remove each plant from its position and place it anywhere in the greenhouse (at any real coordinate) with no plant already in it. Since replanting is a lot of stress for the plants, help Emuskald find the minimum number of plants he has to replant to be able to place the borders. Input The first line of input contains two space-separated integers n and m (1 ≤ n, m ≤ 5000, n ≥ m), the number of plants and the number of different species. Each of the following n lines contain two space-separated numbers: one integer number si (1 ≤ si ≤ m), and one real number xi (0 ≤ xi ≤ 109), the species and position of the i-th plant. Each xi will contain no more than 6 digits after the decimal point. It is guaranteed that all xi are different; there is at least one plant of each species; the plants are given in order "from left to the right", that is in the ascending order of their xi coordinates (xi < xi + 1, 1 ≤ i < n). Output Output a single integer — the minimum number of plants to be replanted. Examples Input 3 2 2 1 1 2.0 1 3.100 Output 1 Input 3 3 1 5.0 2 5.5 3 6.0 Output 0 Input 6 3 1 14.284235 2 17.921382 1 20.328172 3 20.842331 1 25.790145 1 27.204125 Output 2 Note In the first test case, Emuskald can replant the first plant to the right of the last plant, so the answer is 1. In the second test case, the species are already in the correct order, so no replanting is needed.
{"inputs": ["1 1\n1 0\n", "1 2\n1 0\n", "3 3\n3 0\n1 1\n2 2\n", "3 3\n2 0\n1 1\n3 2\n", "3 3\n3 0\n1 1\n2 4\n", "3 3\n2 0\n1 2\n3 2\n", "3 3\n2 0\n1 4\n3 2\n", "3 2\n2 1\n2 2.0\n1 3.100\n"], "outputs": ["0\n", "0\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n"]}
638
172
coding
Solve the programming task below in a Python markdown code block. An array B of length M consisting of only distinct elements is said to be *good* if the following condition holds: Let i be the index of the maximum element of B. Then, B[1,i] must be in ascending order, i.e, B_{1} < B_{2} < B_{3} < \ldots < B_{i}. For example, [1, 2, 3], [1, 4, 3, 2] and [1, 3, 2] are *good* arrays, while [2, 1, 3] and [3, 2, 4, 1] are not. You are given a permutation P of length N. You have to partition P into some non-empty good [subarrays]. Note that every element of P should be contained in exactly one subarray. Find the total number of valid partitions. Since the answer can be quite large, please print it modulo 998244353. Note: A permutation of length N is an arrangement of the integers \{1, 2, 3, \ldots, N\}. ------ 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 two lines of input. - The first line of each test case contains a single integer N, the length of P. - The second line contains N space-separated distinct integers P_{1}, P_{2}, P_{3} , \ldots, P_{N}. ------ Output Format ------ For each test case, output on a new line the number of valid partitions modulo 998244353. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{6}$ $1 ≤ P_{i} ≤ N$ for each $1 ≤ i ≤ N$. - All elements of $P$ are distinct. - The sum of $N$ over all test cases won't exceed $10^{6}$. ----- Sample Input 1 ------ 2 4 2 1 4 3 5 1 2 3 4 5 ----- Sample Output 1 ------ 6 16 ----- explanation 1 ------ Test case $1$: The $6$ valid partitions are as follows: - $[2], [1], [4], [3]$ - $[2], [1], [4, 3]$ - $[2], [1, 4], [3]$ - $[2], [1, 4, 3]$ - $[2, 1], [4], [3]$ - $[2, 1], [4, 3]$
{"inputs": ["2\n4\n2 1 4 3\n5\n1 2 3 4 5\n"], "outputs": ["6\n16\n"]}
615
39
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a string s and a string array dictionary, return the longest string in the dictionary that can be formed by deleting some of the given string characters. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.   Please complete the following python code precisely: ```python class Solution: def findLongestWord(self, s: str, dictionary: List[str]) -> str: ```
{"functional": "def check(candidate):\n assert candidate(s = \"abpcplea\", dictionary = [\"ale\",\"apple\",\"monkey\",\"plea\"]) == \"apple\"\n assert candidate(s = \"abpcplea\", dictionary = [\"a\",\"b\",\"c\"]) == \"a\"\n\n\ncheck(Solution().findLongestWord)"}
117
78
coding
Solve the programming task below in a Python markdown code block. The Queen of England has n trees growing in a row in her garden. At that, the i-th (1 ≤ i ≤ n) tree from the left has height a_{i} meters. Today the Queen decided to update the scenery of her garden. She wants the trees' heights to meet the condition: for all i (1 ≤ i < n), a_{i} + 1 - a_{i} = k, where k is the number the Queen chose. Unfortunately, the royal gardener is not a machine and he cannot fulfill the desire of the Queen instantly! In one minute, the gardener can either decrease the height of a tree to any positive integer height or increase the height of a tree to any positive integer height. How should the royal gardener act to fulfill a whim of Her Majesty in the minimum number of minutes? -----Input----- The first line contains two space-separated integers: n, k (1 ≤ n, k ≤ 1000). The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 1000) — the heights of the trees in the row. -----Output----- In the first line print a single integer p — the minimum number of minutes the gardener needs. In the next p lines print the description of his actions. If the gardener needs to increase the height of the j-th (1 ≤ j ≤ n) tree from the left by x (x ≥ 1) meters, then print in the corresponding line "+ j x". If the gardener needs to decrease the height of the j-th (1 ≤ j ≤ n) tree from the left by x (x ≥ 1) meters, print on the corresponding line "- j x". If there are multiple ways to make a row of trees beautiful in the minimum number of actions, you are allowed to print any of them. -----Examples----- Input 4 1 1 2 1 5 Output 2 + 3 2 - 4 1 Input 4 1 1 2 3 4 Output 0
{"inputs": ["1 99\n1\n", "1 99\n1\n", "1 65\n1\n", "1 99\n2\n", "3 1\n1 1 2\n", "3 1\n1 1 2\n", "3 1\n2 1 2\n", "3 1\n1 0 2\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "2\n+ 2 1\n+ 3 1\n", "2\n+ 2 1\n+ 3 1\n", "2\n+ 2 2\n+ 3 2\n", "2\n+ 2 2\n+ 3 1\n"]}
464
170
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. You can rearrange the elements of nums to any order (including the given order). Let prefix be the array containing the prefix sums of nums after rearranging it. In other words, prefix[i] is the sum of the elements from 0 to i in nums after rearranging it. The score of nums is the number of positive integers in the array prefix. Return the maximum score you can achieve.   Please complete the following python code precisely: ```python class Solution: def maxScore(self, nums: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [2,-1,0,1,-3,3,-3]) == 6\n assert candidate(nums = [-2,-3,0]) == 0\n\n\ncheck(Solution().maxScore)"}
141
59
coding
Solve the programming task below in a Python markdown code block. There are N Snukes lining up in a row. You are given a string S of length N. The i-th Snuke from the front has two red balls if the i-th character in S is `0`; one red ball and one blue ball if the i-th character in S is `1`; two blue balls if the i-th character in S is `2`. Takahashi has a sequence that is initially empty. Find the number of the possible sequences he may have after repeating the following procedure 2N times, modulo 998244353: * Each Snuke who has one or more balls simultaneously chooses one of his balls and hand it to the Snuke in front of him, or hand it to Takahashi if he is the first Snuke in the row. * Takahashi receives the ball and put it to the end of his sequence. Constraints * 1 \leq |S| \leq 2000 * S consists of `0`,`1` and `2`. Note that the integer N is not directly given in input; it is given indirectly as the length of the string S. Input Input is given from Standard Input in the following format: S Output Print the number of the possible sequences Takahashi may have after repeating the procedure 2N times, modulo 998244353. Examples Input 02 Output 3 Input 1210 Output 55 Input 12001021211100201020 Output 543589959
{"inputs": ["1", "0", "2", "0", "1", "2", "10", "11"], "outputs": ["2\n", "1\n", "1\n", "1\n", "2\n", "1\n", "4\n", "6\n"]}
364
64
coding
Solve the programming task below in a Python markdown code block. A queen is the strongest chess piece. In modern chess the queen can move any number of squares in any horizontal, vertical or diagonal direction (considering that there're no other pieces on its way). The queen combines the options given to the rook and the bishop. There are m queens on a square n × n chessboard. You know each queen's positions, the i-th queen is positioned in the square (ri, ci), where ri is the board row number (numbered from the top to the bottom from 1 to n), and ci is the board's column number (numbered from the left to the right from 1 to n). No two queens share the same position. For each queen one can count w — the number of other queens that the given queen threatens (attacks). For a fixed attack direction only the first queen in this direction is under attack if there are many queens are on the ray of the attack. Obviously, for any queen w is between 0 and 8, inclusive. Print the sequence t0, t1, ..., t8, where ti is the number of queens that threaten exactly i other queens, i.e. the number of queens that their w equals i. Input The first line of the input contains a pair of integers n, m (1 ≤ n, m ≤ 105), where n is the size of the board and m is the number of queens on the board. Then m following lines contain positions of the queens, one per line. Each line contains a pair of integers ri, ci (1 ≤ ri, ci ≤ n) — the queen's position. No two queens stand on the same square. Output Print the required sequence t0, t1, ..., t8, separating the numbers with spaces. Examples Input 8 4 4 3 4 8 6 5 1 6 Output 0 3 0 1 0 0 0 0 0 Input 10 3 1 1 1 2 1 3 Output 0 2 1 0 0 0 0 0 0
{"inputs": ["2 1\n1 1\n", "1 1\n1 1\n", "2 2\n1 1\n1 2\n", "20 3\n1 1\n1 2\n1 3\n", "38 3\n1 1\n1 2\n1 3\n", "10 3\n1 1\n1 2\n1 3\n", "2 4\n2 1\n1 2\n2 2\n1 1\n", "8 4\n4 3\n8 8\n6 5\n1 6\n"], "outputs": ["1 0 0 0 0 0 0 0 0\n", "1 0 0 0 0 0 0 0 0\n", "0 2 0 0 0 0 0 0 0\n", "0 2 1 0 0 0 0 0 0\n", "0 2 1 0 0 0 0 0 0\n", "0 2 1 0 0 0 0 0 0\n", "0 0 0 4 0 0 0 0 0\n", "1 2 1 0 0 0 0 0 0\n"]}
458
301
coding
Solve the programming task below in a Python markdown code block. There are N children, numbered 1, 2, \ldots, N. They have decided to share K candies among themselves. Here, for each i (1 \leq i \leq N), Child i must receive between 0 and a_i candies (inclusive). Also, no candies should be left over. Find the number of ways for them to share candies, modulo 10^9 + 7. Here, two ways are said to be different when there exists a child who receives a different number of candies. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 0 \leq K \leq 10^5 * 0 \leq a_i \leq K Input Input is given from Standard Input in the following format: N K a_1 a_2 \ldots a_N Output Print the number of ways for the children to share candies, modulo 10^9 + 7. Examples Input 3 4 1 2 3 Output 5 Input 1 10 9 Output 0 Input 2 0 0 0 Output 1 Input 4 100000 100000 100000 100000 100000 Output 665683269
{"inputs": ["1 10\n3", "1 10\n9", "1 10\n17", "2 0\n0 0", "3 4\n1 2 3", "4 100000\n101000 100000 100000 100000", "4 101000\n101000 100000 100100 101000", "4 110000\n101000 100000 100000 100000"], "outputs": ["0\n", "0", "1\n", "1", "5", "665683269\n", "744244119\n", "741473440\n"]}
324
218
coding
Solve the programming task below in a Python markdown code block. You are given an array $a$ consisting of $500000$ integers (numbered from $1$ to $500000$). Initially all elements of $a$ are zero. You have to process two types of queries to this array: $1$ $x$ $y$ — increase $a_x$ by $y$; $2$ $x$ $y$ — compute $\sum\limits_{i \in R(x, y)} a_i$, where $R(x, y)$ is the set of all integers from $1$ to $500000$ which have remainder $y$ modulo $x$. Can you process all the queries? -----Input----- The first line contains one integer $q$ ($1 \le q \le 500000$) — the number of queries. Then $q$ lines follow, each describing a query. The $i$-th line contains three integers $t_i$, $x_i$ and $y_i$ ($1 \le t_i \le 2$). If $t_i = 1$, then it is a query of the first type, $1 \le x_i \le 500000$, and $-1000 \le y_i \le 1000$. If $t_i = 2$, then it it a query of the second type, $1 \le x_i \le 500000$, and $0 \le y_i < x_i$. It is guaranteed that there will be at least one query of type $2$. -----Output----- For each query of type $2$ print one integer — the answer to it. -----Example----- Input 5 1 3 4 2 3 0 2 4 3 1 4 -4 2 1 0 Output 4 4 0
{"inputs": ["2\n1 3213 321\n2 3213 0\n", "2\n1 3213 321\n2 3213 0\n", "2\n1 3213 321\n2 6100 0\n", "2\n1 3213 321\n2 2396 0\n", "2\n1 4824 321\n2 6100 0\n", "2\n1 4656 321\n2 2396 0\n", "2\n1 4824 528\n2 6100 0\n", "2\n1 4656 321\n2 2396 1\n"], "outputs": ["321\n", "321\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
427
234
coding
Solve the programming task below in a Python markdown code block. You are given a matrix of size n × n filled with lowercase English letters. You can change no more than k letters in this matrix. Consider all paths from the upper left corner to the lower right corner that move from a cell to its neighboring cell to the right or down. Each path is associated with the string that is formed by all the letters in the cells the path visits. Thus, the length of each string is 2n - 1. Find the lexicographically smallest string that can be associated with a path after changing letters in at most k cells of the matrix. A string a is lexicographically smaller than a string b, if the first different letter in a and b is smaller in a. Input The first line contains two integers n and k (1 ≤ n ≤ 2000, 0 ≤ k ≤ n^2) — the size of the matrix and the number of letters you can change. Each of the next n lines contains a string of n lowercase English letters denoting one row of the matrix. Output Output the lexicographically smallest string that can be associated with some valid path after changing no more than k letters in the matrix. Examples Input 4 2 abcd bcde bcad bcde Output aaabcde Input 5 3 bwwwz hrhdh sepsp sqfaf ajbvw Output aaaepfafw Input 7 6 ypnxnnp pnxonpm nxanpou xnnpmud nhtdudu npmuduh pmutsnz Output aaaaaaadudsnz Note In the first sample test case it is possible to change letters 'b' in cells (2, 1) and (3, 1) to 'a', then the minimum path contains cells (1, 1), (2, 1), (3, 1), (4, 1), (4, 2), (4, 3), (4, 4). The first coordinate corresponds to the row and the second coordinate corresponds to the column.
{"inputs": ["1 0\nz\n", "1 1\ng\n", "1 0\na\n", "1 1\nz\n", "1 0\ng\n", "1 0\ny\n", "1 1\nf\n", "1 2\nf\n"], "outputs": ["z\n", "a\n", "a\n", "a\n", "g\n", "y", "a", "a"]}
458
99
coding
Solve the programming task below in a Python markdown code block. Dhote and Shweta went on a tour by plane for the first time.Dhote was surprised by the conveyor belt at the airport.As Shweta was getting bored Dhote had an idea of playing a game with her.He asked Shweta to count the number of bags whose individual weight is greater than or equal to the half of the total number of bags on the conveyor belt.Shweta got stuck in the puzzle! Help Shweta. -----Input:----- - First line will contain T$T$, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, one integers N$N$. - next line conatins N$N$ integers that represents weight of bag -----Output:----- For each testcase, print required answer in new line . -----Constraints----- - 1≤T≤1000$1 \leq T \leq 1000$ - 1≤N≤105$1 \leq N \leq 10^5$ - 1≤weightofbag≤105$ 1\leq weight of bag \leq 10^5$ -----Sample Input:----- 1 4 1 2 3 4 -----Sample Output:----- 3
{"inputs": ["1\n4\n1 2 3 4"], "outputs": ["3"]}
291
22
coding
Solve the programming task below in a Python markdown code block. Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane. Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y). -----Input----- The first line contains two space-separated integers x and y (|x|, |y| ≤ 100). -----Output----- Print a single integer, showing how many times Valera has to turn. -----Examples----- Input 0 0 Output 0 Input 1 0 Output 0 Input 0 1 Output 2 Input -1 -1 Output 3
{"inputs": ["0 0\n", "1 0\n", "0 1\n", "0 6\n", "1 1\n", "0 6\n", "1 1\n", "2 1\n"], "outputs": ["0\n", "0\n", "2\n", "22\n", "1\n", "22\n", "1\n", "5\n"]}
318
88
coding
Solve the programming task below in a Python markdown code block. Let's suppose you have an array a, a stack s (initially empty) and an array b (also initially empty). You may perform the following operations until both a and s are empty: Take the first element of a, push it into s and remove it from a (if a is not empty); Take the top element from s, append it to the end of array b and remove it from s (if s is not empty). You can perform these operations in arbitrary order. If there exists a way to perform the operations such that array b is sorted in non-descending order in the end, then array a is called stack-sortable. For example, [3, 1, 2] is stack-sortable, because b will be sorted if we perform the following operations: Remove 3 from a and push it into s; Remove 1 from a and push it into s; Remove 1 from s and append it to the end of b; Remove 2 from a and push it into s; Remove 2 from s and append it to the end of b; Remove 3 from s and append it to the end of b. After all these operations b = [1, 2, 3], so [3, 1, 2] is stack-sortable. [2, 3, 1] is not stack-sortable. You are given k first elements of some permutation p of size n (recall that a permutation of size n is an array of size n where each integer from 1 to n occurs exactly once). You have to restore the remaining n - k elements of this permutation so it is stack-sortable. If there are multiple answers, choose the answer such that p is lexicographically maximal (an array q is lexicographically greater than an array p iff there exists some integer k such that for every i < k q_{i} = p_{i}, and q_{k} > p_{k}). You may not swap or change any of first k elements of the permutation. Print the lexicographically maximal permutation p you can obtain. If there exists no answer then output -1. -----Input----- The first line contains two integers n and k (2 ≤ n ≤ 200000, 1 ≤ k < n) — the size of a desired permutation, and the number of elements you are given, respectively. The second line contains k integers p_1, p_2, ..., p_{k} (1 ≤ p_{i} ≤ n) — the first k elements of p. These integers are pairwise distinct. -----Output----- If it is possible to restore a stack-sortable permutation p of size n such that the first k elements of p are equal to elements given in the input, print lexicographically maximal such permutation. Otherwise print -1. -----Examples----- Input 5 3 3 2 1 Output 3 2 1 5 4 Input 5 3 2 3 1 Output -1 Input 5 1 3 Output 3 2 1 5 4 Input 5 2 3 4 Output -1
{"inputs": ["5 1\n3\n", "2 1\n1\n", "2 1\n1\n", "9 1\n6\n", "7 1\n1\n", "5 1\n3\n", "10 1\n6\n", "10 1\n1\n"], "outputs": ["3 2 1 5 4 ", "1 2 ", "1 2 ", "6 5 4 3 2 1 9 8 7 ", "1 7 6 5 4 3 2 ", "3 2 1 5 4 ", "6 5 4 3 2 1 10 9 8 7 ", "1 10 9 8 7 6 5 4 3 2 "]}
682
183
coding
Solve the programming task below in a Python markdown code block. Remainder of Big Integers Given two integers $A$ and $B$, compute the remainder of $\frac{A}{B}$. Input Two integers $A$ and $B$ separated by a space character are given in a line. Output Print the remainder in a line. Constraints * $0 \leq A, B \leq 10^{1000}$ * $B \ne 0$ Sample Input 1 5 8 Sample Output 1 5 Sample Input 2 100 25 Sample Output 2 0 Example Input 5 8 Output 5
{"inputs": ["5 3", "9 3", "9 6", "4 3", "5 7", "6 7", "9 1", "5 1"], "outputs": ["2\n", "0\n", "3\n", "1\n", "5\n", "6\n", "0\n", "0\n"]}
153
78
coding
Solve the programming task below in a Python markdown code block. *** Nova polynomial from roots*** This kata is from a series on polynomial handling. ( [#1](http://www.codewars.com/kata/nova-polynomial-1-add-1) [#2](http://www.codewars.com/kata/570eb07e127ad107270005fe) [#3](http://www.codewars.com/kata/5714041e8807940ff3001140 ) [#4](http://www.codewars.com/kata/571a2e2df24bdfd4e20001f5)) Consider a polynomial in a list where each element in the list element corresponds to the factors. The factor order is the position in the list. The first element is the zero order factor (the constant). p = [a0, a1, a2, a3] signifies the polynomial a0 + a1x + a2x^2 + a3*x^3 In this kata create the polynomial from a list of roots: [r0, r1 ,r2, r3 ] p = (x-r0)(x-r1)(x-r2)(x-r3) note: no roots should return the identity polynomial. ```python poly_from_roots([4]) = [-4, 1] poly_from_roots([0, 0, 0, 0] ) = [0, 0, 0, 0, 1] poly_from_roots([]) = [1] ``` The first katas of this series is preloaded in the code and can be used: [poly_add](http://www.codewars.com/kata/570eb07e127ad107270005fe) [poly_multiply](http://www.codewars.com/kata/570eb07e127ad107270005fe) Also feel free to reuse/extend the following starter code: ```python def poly_from_roots(r): ```
{"functional": "_inputs = [[[4]], [[0, 0, 0, 0]], [[1, 1, 1, 1]], [[-1, 1, 2, -2]], [[]]]\n_outputs = [[[-4, 1]], [[0, 0, 0, 0, 1]], [[1, -4, 6, -4, 1]], [[4, 0, -5, 0, 1]], [[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(poly_from_roots(*i), o[0])"}
468
247
coding
Solve the programming task below in a Python markdown code block. You are given a list of $N$ numbers $a_1,a_2,\ldots,a_n$. For each element at position $\boldsymbol{i}$ ($1\leq i\leq N$), we define $\text{Left}(i)$ and $\textit{Right}(i)$ as: $\text{Left}(i)$ = closest index j such that j < i and $a_j>a_i$. If no such j exists then $\text{Left}(i)$ = 0. $\textit{Right}(i)$ = closest index k such that k > i and $a_k>a_i$. If no such k exists then $\textit{Right}(i)$ = 0. We define $\textit{IndexProduct(i)}$ = $\text{Left}(i)$ * $\textit{Right}(i)$. You need to find out the maximum $\textit{IndexProduct(i)}$ among all i. Input Format The first line contains an integer $N$, the number of integers. The next line contains the $N$ integers describing the list a[1..N]. Constraints $1\leq N\leq10^5$ $1\leq a_i\leq10^9$ Output Format Output the maximum $\textit{IndexProduct}$ among all indices from $1$ to $N$. Sample Input 5 5 4 3 4 5 Sample Output 8 Explanation We can compute the following: $IndexProduct(1)=0$ $IndexProduct(2)=1\times5=5$ $IndexProduct(3)=2\times4=8$ $IndexProduct(4)=1\times5=5$ $IndexProduct(5)=0$ The largest of these is 8, so it is the answer.
{"inputs": ["5\n5 4 3 4 5\n"], "outputs": ["8\n"]}
405
24
coding
Solve the programming task below in a Python markdown code block. Lucy loves to travel. Luckily she is a renowned computer scientist and gets to travel to international conferences using her department's budget. Each year, Society for Exciting Computer Science Research (SECSR) organizes several conferences around the world. Lucy always picks one conference from that list that is hosted in a city she hasn't been to before, and if that leaves her with more than one option, she picks the conference that she thinks would be most relevant for her field of research. Write a function `conferencePicker` that takes in two arguments: - `citiesVisited`, a list of cities that Lucy has visited before, given as an array of strings. - `citiesOffered`, a list of cities that will host SECSR conferences this year, given as an array of strings. `citiesOffered` will already be ordered in terms of the relevance of the conferences for Lucy's research (from the most to the least relevant). The function should return the city that Lucy should visit, as a string. Also note: - You should allow for the possibility that Lucy hasn't visited any city before. - SECSR organizes at least two conferences each year. - If all of the offered conferences are hosted in cities that Lucy has visited before, the function should return `'No worthwhile conferences this year!'` (`Nothing` in Haskell) Example: Also feel free to reuse/extend the following starter code: ```python def conference_picker(cities_visited, cities_offered): ```
{"functional": "_inputs = [[[], ['Philadelphia', 'Osaka', 'Tokyo', 'Melbourne']], [[], ['Brussels', 'Madrid', 'London']], [[], ['Sydney', 'Tokyo']], [['London', 'Berlin', 'Mexico City', 'Melbourne', 'Buenos Aires', 'Hong Kong', 'Madrid', 'Paris'], ['Berlin', 'Melbourne']], [['Beijing', 'Johannesburg', 'Sydney', 'Philadelphia', 'Hong Kong', 'Stockholm', 'Chicago', 'Seoul', 'Mexico City', 'Berlin'], ['Stockholm', 'Berlin', 'Chicago']], [['Rome'], ['Rome']], [['Milan'], ['London']], [['Mexico City', 'Dubai', 'Philadelphia', 'Madrid', 'Houston', 'Chicago', 'Delhi', 'Seoul', 'Mumbai', 'Lisbon', 'Hong Kong', 'Brisbane', 'Stockholm', 'Tokyo', 'San Francisco', 'Rio De Janeiro'], ['Lisbon', 'Mexico City']], [['Gatlantis', \"Baldur's Gate\", 'Gotham City', 'Mystara', 'Washinkyo', 'Central City'], ['Mystara', 'Gatlantis', 'MegaTokyo', 'Genosha', 'Central City', 'Washinkyo', 'Gotham City', \"King's Landing\", 'Waterdeep']], [['Thay', 'Camelot'], ['Waterdeep', 'Washinkyo']]]\n_outputs = [['Philadelphia'], ['Brussels'], ['Sydney'], ['No worthwhile conferences this year!'], ['No worthwhile conferences this year!'], ['No worthwhile conferences this year!'], ['London'], ['No worthwhile conferences this year!'], ['MegaTokyo'], ['Waterdeep']]\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(conference_picker(*i), o[0])"}
311
519
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You have n binary tree nodes numbered from 0 to n - 1 where node i has two children leftChild[i] and rightChild[i], return true if and only if all the given nodes form exactly one valid binary tree. If node i has no left child then leftChild[i] will equal -1, similarly for the right child. Note that the nodes have no values and that we only use the node numbers in this problem.   Please complete the following python code precisely: ```python class Solution: def validateBinaryTreeNodes(self, n: int, leftChild: List[int], rightChild: List[int]) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(n = 4, leftChild = [1,-1,3,-1], rightChild = [2,-1,-1,-1]) == True\n assert candidate(n = 4, leftChild = [1,-1,3,-1], rightChild = [2,3,-1,-1]) == False\n assert candidate(n = 2, leftChild = [1,0], rightChild = [-1,-1]) == False\n\n\ncheck(Solution().validateBinaryTreeNodes)"}
150
118
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. There is a haunted town called HauntedLand. The structure of HauntedLand can be thought of as a grid of size n * m. There is a house in each cell of the grid. Some people have fled from their houses because they were haunted. '.' represents a haunted house whereas '*' represents a house in which people are living. One day, Devu, the famous perfumer came to town with a perfume whose smell can hypnotize people. Devu can put the perfume in at most one of the houses. This takes Devu one second. Then, the perfume spreads from one house (need not be inhabited by people) to all its adjacent houses in one second, and the cycle continues. Two houses are said to be a adjacent to each other, if they share a corner or an edge, i.e., each house (except those on the boundaries) will have 8 adjacent houses. You want to save people from Devu's dark perfumery by sending them a message to flee from the town. So, you need to estimate the minimum amount of time Devu needs to hypnotize all the people? Note that if there are no houses inhabited by people, Devu doesn't need to put perfume in any cell. ------ Input ------ The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows. First line of each test case contains two space separated integers n, m denoting the dimensions of the town. For each of next n lines, each line has m characters (without any space) denoting a row of houses of the town. ------ Output ------ For each test case, output a single integer corresponding to the answer of the problem. ------ Constraints ------ $1 ≤ T ≤ 20$ Subtask #1: (40 points) $1 ≤ n, m ≤ 100$ Subtask #2: (60 points) $1 ≤ n, m ≤ 1000$ ----- Sample Input 1 ------ 2 2 2 *. .. 3 4 .*.. *. .*.. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ In the first example, it will take Devu one second for putting the perfume at the only house. So, the answer is 1. In the second example, He will first put the perfume at the * at cell (1, 1) (assuming 0-based indexing). Now, it will take Devu 1 secs to put perfume. In the next second, the perfume will spread to all of its adjacent cells, thus making each house haunted. So, the answer is 2.
{"inputs": ["2\n2 2\n*.\n..\n3 4\n.*..\n***.\n.*..", "2\n2 2\n*.\n..\n3 4\n.*..\n***.\n.*.."], "outputs": ["1\n2", "1\n2"]}
581
66
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an array nums consisting of positive integers. Split the array into one or more disjoint subarrays such that: Each element of the array belongs to exactly one subarray, and The GCD of the elements of each subarray is strictly greater than 1. Return the minimum number of subarrays that can be obtained after the split. Note that: The GCD of a subarray is the largest positive integer that evenly divides all the elements of the subarray. A subarray is a contiguous part of the array.   Please complete the following python code precisely: ```python class Solution: def minimumSplits(self, nums: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [12,6,3,14,8]) == 2\n assert candidate(nums = [4,12,6,14]) == 1\n\n\ncheck(Solution().minimumSplits)"}
156
62