task_type
stringclasses
1 value
problem
stringlengths
209
3.39k
answer
stringlengths
35
6.15k
problem_tokens
int64
60
774
answer_tokens
int64
12
2.04k
coding
Solve the programming task below in a Python markdown code block. You would like to get the 'weight' of a name by getting the sum of the ascii values. However you believe that capital letters should be worth more than mere lowercase letters. Spaces, numbers, or any other character are worth 0. Normally in ascii a has a value of 97 A has a value of 65 ' ' has a value of 32 0 has a value of 48 To find who has the 'weightier' name you will switch all the values so: A will be 97 a will be 65 ' ' will be 0 0 will be 0 etc... For example Joe will have a weight of 254, instead of 286 using normal ascii values. Also feel free to reuse/extend the following starter code: ```python def get_weight(name): ```
{"functional": "_inputs = [['Joe'], ['CJ'], ['cj'], ['George Washington'], ['Billy Bob Joe'], ['King George II'], ['r2d2'], ['R2D2'], ['C3PO']]\n_outputs = [[254], [205], [141], [1275], [909], [1012], [150], [214], [322]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(get_weight(*i), o[0])"}
206
236
coding
Solve the programming task below in a Python markdown code block. Monocarp is playing a MMORPG. There are two commonly used types of currency in this MMORPG — gold coins and silver coins. Monocarp wants to buy a new weapon for his character, and that weapon costs $n$ silver coins. Unfortunately, right now, Monocarp has no coins at all. Monocarp can earn gold coins by completing quests in the game. Each quest yields exactly one gold coin. Monocarp can also exchange coins via the in-game trading system. Monocarp has spent days analyzing the in-game economy; he came to the following conclusion: it is possible to sell one gold coin for $a$ silver coins (i. e. Monocarp can lose one gold coin to gain $a$ silver coins), or buy one gold coin for $b$ silver coins (i. e. Monocarp can lose $b$ silver coins to gain one gold coin). Now Monocarp wants to calculate the minimum number of quests that he has to complete in order to have at least $n$ silver coins after some abuse of the in-game economy. Note that Monocarp can perform exchanges of both types (selling and buying gold coins for silver coins) any number of times. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Each test case consists of one line containing three integers $n$, $a$ and $b$ ($1 \le n \le 10^7$; $1 \le a, b \le 50$). -----Output----- For each test case, print one integer — the minimum possible number of quests Monocarp has to complete. -----Examples----- Input 4 100 25 30 9999997 25 50 52 50 48 49 50 1 Output 4 400000 1 1 -----Note----- In the first test case of the example, Monocarp should complete $4$ quests, and then sell $4$ gold coins for $100$ silver coins. In the second test case, Monocarp should complete $400000$ quests, and then sell $400000$ gold coins for $10$ million silver coins. In the third test case, Monocarp should complete $1$ quest, sell the gold coin for $50$ silver coins, buy a gold coin for $48$ silver coins, and then sell it again for $50$ coins. So, he will have $52$ silver coins. In the fourth test case, Monocarp should complete $1$ quest and then sell the gold coin he has obtained for $50$ silver coins.
{"inputs": ["4\n100 25 30\n9999997 25 50\n52 50 48\n49 50 1\n"], "outputs": ["4\n400000\n1\n1\n"]}
612
66
coding
Solve the programming task below in a Python markdown code block. You are given integers N and K. Find the number of triples (a,b,c) of positive integers not greater than N such that a+b,b+c and c+a are all multiples of K. The order of a,b,c does matter, and some of them can be the same. Constraints * 1 \leq N,K \leq 2\times 10^5 * N and K are integers. Input Input is given from Standard Input in the following format: N K Output Print the number of triples (a,b,c) of positive integers not greater than N such that a+b,b+c and c+a are all multiples of K. Examples Input 3 2 Output 9 Input 5 3 Output 1 Input 31415 9265 Output 27 Input 35897 932 Output 114191
{"inputs": ["4 3", "3 2", "5 3", "56 6", "20 6", "25 4", "25 7", "74 1"], "outputs": ["1\n", "9", "1", "1458\n", "54\n", "432\n", "27\n", "405224\n"]}
211
93
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ items in his shop (numbered $1$ through $N$); for each valid $i$, the price of the $i$-th item is $P_{i}$. Since Chef has very loyal customers, all $N$ items are guaranteed to be sold regardless of their price. However, the government introduced a *price ceiling* $K$, which means that for each item $i$ such that $P_{i} > K$, its price should be reduced from $P_{i}$ to $K$. Chef's *revenue* is the sum of prices of all the items he sells. Find the amount of revenue which Chef loses because of this price ceiling. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. The second line contains $N$ space-separated integers $P_{1}, P_{2}, \ldots, P_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the amount of lost revenue. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 10,000$ $1 ≤ P_{i} ≤ 1,000$ for each valid $i$ $1 ≤ K ≤ 1,000$ ------ Subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 3 5 4 10 2 3 4 5 7 15 1 2 3 4 5 6 7 5 5 10 9 8 7 6 ----- Sample Output 1 ------ 7 0 15 ----- explanation 1 ------ Test Case 1: The initial revenue is $10 + 2 + 3 + 4 + 5 = 24$. Because of the price ceiling, $P_{1}$ decreases from $10$ to $4$ and $P_{5}$ decreases from $5$ to $4$. The revenue afterwards is $4 + 2 + 3 + 4 + 4 = 17$ and the lost revenue is $24 - 17 = 7$. Test Case 2: The initial revenue is $1 + 2 + 3 + 4 + 5 + 6 + 7 = 28$. For each valid $i$, $P_{i} ≤ 15$, so there are no changes, the revenue after introduction of the price ceiling is the same and there is zero lost revenue. Test Case 3: The initial revenue is $10 + 9 + 8 + 7 + 6 = 40$. Since $P_{i} > 5$ for each valid $i$, the prices of all items decrease to $5$. The revenue afterwards is $5 \cdot 5 = 25$ and the lost revenue is $40 - 25 = 15$.
{"inputs": ["3\n5 4\n10 2 3 4 5\n7 15\n1 2 3 4 5 6 7\n5 5\n10 9 8 7 6"], "outputs": ["7\n0\n15"]}
720
66
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an array of strings queries and a string pattern, return a boolean array answer where answer[i] is true if queries[i] matches pattern, and false otherwise. A query word queries[i] matches pattern if you can insert lowercase English letters pattern so that it equals the query. You may insert each character at any position and you may not insert any characters.   Please complete the following python code precisely: ```python class Solution: def camelMatch(self, queries: List[str], pattern: str) -> List[bool]: ```
{"functional": "def check(candidate):\n assert candidate(queries = [\"FooBar\",\"FooBarTest\",\"FootBall\",\"FrameBuffer\",\"ForceFeedBack\"], pattern = \"FB\") == [True,False,True,True,False]\n assert candidate(queries = [\"FooBar\",\"FooBarTest\",\"FootBall\",\"FrameBuffer\",\"ForceFeedBack\"], pattern = \"FoBa\") == [True,False,True,False,False]\n assert candidate(queries = [\"FooBar\",\"FooBarTest\",\"FootBall\",\"FrameBuffer\",\"ForceFeedBack\"], pattern = \"FoBaT\") == [False,True,False,False,False]\n\n\ncheck(Solution().camelMatch)"}
126
142
coding
Solve the programming task below in a Python markdown code block. Story: In the realm of numbers, the apocalypse has arrived. Hordes of zombie numbers have infiltrated and are ready to turn everything into undead. The properties of zombies are truly apocalyptic: they reproduce themselves unlimitedly and freely interact with each other. Anyone who equals them is doomed. Out of an infinite number of natural numbers, only a few remain. This world needs a hero who leads remaining numbers in hope for survival: The highest number to lead those who still remain. Briefing: There is a list of positive natural numbers. Find the largest number that cannot be represented as the sum of this numbers, given that each number can be added unlimited times. Return this number, either 0 if there are no such numbers, or -1 if there are an infinite number of them. Example: ``` Let's say [3,4] are given numbers. Lets check each number one by one: 1 - (no solution) - good 2 - (no solution) - good 3 = 3 won't go 4 = 4 won't go 5 - (no solution) - good 6 = 3+3 won't go 7 = 3+4 won't go 8 = 4+4 won't go 9 = 3+3+3 won't go 10 = 3+3+4 won't go 11 = 3+4+4 won't go 13 = 3+3+3+4 won't go ``` ...and so on. So 5 is the biggest 'good'. return 5 Test specs: Random cases will input up to 10 numbers with up to 1000 value Special thanks: Thanks to Voile-sama, mathsisfun-sama, and Avanta-sama for heavy assistance. And to everyone who tried and beaten the kata ^_^ Also feel free to reuse/extend the following starter code: ```python def survivor(zombies): ```
{"functional": "_inputs = [[[7, 11]], [[1, 7, 15]], [[2, 10]], [[687, 829, 998]], [[]], [[1]]]\n_outputs = [[59], [0], [-1], [45664], [-1], [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(survivor(*i), o[0])"}
420
216
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
Solve the programming task below in a Python markdown code block. You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. -----Input----- The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. -----Output----- The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them. -----Examples----- Input 24 17:30 Output 17:30 Input 12 17:30 Output 07:30 Input 24 99:99 Output 09:09
{"inputs": ["24\n17:30\n", "12\n17:30\n", "24\n99:99\n", "12\n05:54\n", "12\n00:05\n", "24\n23:80\n", "24\n73:16\n", "12\n03:77\n"], "outputs": ["17:30\n", "07:30\n", "09:09\n", "05:54\n", "01:05\n", "23:00\n", "03:16\n", "03:07\n"]}
412
158
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a 0-indexed integer array nums. A subarray of nums is called continuous if: Let i, i + 1, ..., j be the indices in the subarray. Then, for each pair of indices i <= i1, i2 <= j, 0 <= |nums[i1] - nums[i2]| <= 2. Return the total number of continuous subarrays. A subarray is a contiguous non-empty sequence of elements within an array.   Please complete the following python code precisely: ```python class Solution: def continuousSubarrays(self, nums: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [5,4,2,4]) == 8\n assert candidate(nums = [1,2,3]) == 6\n\n\ncheck(Solution().continuousSubarrays)"}
147
54
coding
Solve the programming task below in a Python markdown code block. You are given two arrays `arr1` and `arr2`, where `arr2` always contains integers. Write the function `find_array(arr1, arr2)` such that: For `arr1 = ['a', 'a', 'a', 'a', 'a']`, `arr2 = [2, 4]` `find_array returns ['a', 'a']` For `arr1 = [0, 1, 5, 2, 1, 8, 9, 1, 5]`, `arr2 = [1, 4, 7]` `find_array returns [1, 1, 1]` For `arr1 = [0, 3, 4]`, `arr2 = [2, 6]` `find_array returns [4]` For `arr1=["a","b","c","d"]` , `arr2=[2,2,2]`, `find_array returns ["c","c","c"]` For `arr1=["a","b","c","d"]`, `arr2=[3,0,2]` `find_array returns ["d","a","c"]` If either `arr1` or `arr2` is empty, you should return an empty arr (empty list in python, empty vector in c++). Note for c++ use std::vector arr1, arr2. Also feel free to reuse/extend the following starter code: ```python def find_array(arr1, arr2): ```
{"functional": "_inputs = [[['a', 'a', 'a', 'a', 'a'], [2, 4]], [[0, 1, 5, 2, 1, 8, 9, 1, 5], [1, 4, 7]], [[1, 2, 3, 4, 5], [0]], [['this', 'is', 'test'], [0, 1, 2]], [[0, 3, 4], [2, 6]], [[1], []], [[], [2]], [[], []]]\n_outputs = [[['a', 'a']], [[1, 1, 1]], [[1]], [['this', 'is', 'test']], [[4]], [[]], [[]], [[]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(find_array(*i), o[0])"}
335
308
coding
Solve the programming task below in a Python markdown code block. Valera's lifelong ambition was to be a photographer, so he bought a new camera. Every day he got more and more clients asking for photos, and one day Valera needed a program that would determine the maximum number of people he can serve. The camera's memory is d megabytes. Valera's camera can take photos of high and low quality. One low quality photo takes a megabytes of memory, one high quality photo take b megabytes of memory. For unknown reasons, each client asks him to make several low quality photos and several high quality photos. More formally, the i-th client asks to make xi low quality photos and yi high quality photos. Valera wants to serve as many clients per day as possible, provided that they will be pleased with his work. To please the i-th client, Valera needs to give him everything he wants, that is, to make xi low quality photos and yi high quality photos. To make one low quality photo, the camera must have at least a megabytes of free memory space. Similarly, to make one high quality photo, the camera must have at least b megabytes of free memory space. Initially the camera's memory is empty. Valera also does not delete photos from the camera so that the camera's memory gradually fills up. Calculate the maximum number of clients Valera can successfully serve and print the numbers of these clients. Input The first line contains two integers n and d (1 ≤ n ≤ 105, 1 ≤ d ≤ 109) — the number of clients and the camera memory size, correspondingly. The second line contains two integers a and b (1 ≤ a ≤ b ≤ 104) — the size of one low quality photo and of one high quality photo, correspondingly. Next n lines describe the clients. The i-th line contains two integers xi and yi (0 ≤ xi, yi ≤ 105) — the number of low quality photos and high quality photos the i-th client wants, correspondingly. All numbers on all lines are separated by single spaces. Output On the first line print the answer to the problem — the maximum number of clients that Valera can successfully serve. Print on the second line the numbers of the client in any order. All numbers must be distinct. If there are multiple answers, print any of them. The clients are numbered starting with 1 in the order in which they are defined in the input data. Examples Input 3 10 2 3 1 4 2 1 1 0 Output 2 3 2 Input 3 6 6 6 1 1 1 0 1 0 Output 1 2
{"inputs": ["1 22\n3 5\n1 3\n", "1 22\n0 5\n1 3\n", "1 22\n0 5\n0 3\n", "1 22\n0 2\n0 3\n", "1 100\n6 10\n14 8\n", "1 110\n6 10\n14 8\n", "1 100\n6 10\n14 19\n", "2 160\n6 9\n11 9\n6 6\n"], "outputs": ["1\n1\n", "1\n1 \n", "1\n1 \n", "1\n1 \n", "0\n\n", "0\n\n", "0\n\n", "1\n2\n"]}
577
190
coding
Solve the programming task below in a Python markdown code block. Manasa loves the nim game, in which there are $n$ buckets, each having $A_i$ balls. Two players play alternately. Each turn consists of removing some non-zero number of balls from one of the bucket. A player with lack of moves looses. But, Manasa having played it so many times, she gets bored one day. So she wants to change the rules of the game. She loves prime numbers, so she makes a new rule: any player can only remove a prime number of balls from a bucket. But there are infinite number prime numbers. So to keep the game simple, a player can only remove $\boldsymbol{x}$ balls from a bucket if $\boldsymbol{x}$ belongs to the set $S=\{2,3,5,7,11,13\}.$ The whole game can now be described as follows: There are $n$ buckets, and the $k^{\text{th}}$ bucket contains $A_{k}$ balls. A player can choose a bucket and remove $\boldsymbol{x}$ balls from that bucket where $\boldsymbol{x}$ belongs to $\mbox{S}$. A player loses if there are no more available moves. Manasa plays the first move against Sandy. Who will win if both of them play optimally? Input Format The first line contains an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each test case consists of two lines. The first line contains a single integer $n$. The second line contain $n$ space-separated integers $A_{1},A_{2},\ldots,A_{n}$. Constraints $1\leq t\leq10$ $1\leq n\leq10^4$ $1\leq A_k\leq10^{18}$ Output Format Print a single line containing the name of the winner: Manasa or Sandy. Sample Input 0 2 2 10 10 3 2 2 3 Sample Output 0 Sandy Manasa Explanation 0 For the first testcase: Since both the buckets have same number of balls, Manasa can choose any one of them for her first move. If Manasa selects to remove $2,3,5$ or $7$ balls to remove from first bucket. Now, Sandy can always counter her move by removing $7,7,5,3$ balls from first bucket if it's left with $8,7,5,3$ balls respectively. Now, there are no valid moves left for first bucket. The same thing repeats for second bucket and Sandy wins. For the second testcase: Manasa removes $3$ balls from the third bucket. Now, if Sandy choose the remove $2$ balls from second bucket Manasa will empty the first bucket and if Sandy choose the remove $2$ balls from first bucket, Manasa will empty second one. Hence, Manasa wins.
{"inputs": ["2\n2\n10 10\n3\n2 2 3\n"], "outputs": ["Sandy\nManasa\n"]}
641
34
coding
Solve the programming task below in a Python markdown code block. In distant future on Earth day lasts for n hours and that's why there are n timezones. Local times in adjacent timezones differ by one hour. For describing local time, hours numbers from 1 to n are used, i.e. there is no time "0 hours", instead of it "n hours" is used. When local time in the 1-st timezone is 1 hour, local time in the i-th timezone is i hours. Some online programming contests platform wants to conduct a contest that lasts for an hour in such a way that its beginning coincides with beginning of some hour (in all time zones). The platform knows, that there are a_{i} people from i-th timezone who want to participate in the contest. Each person will participate if and only if the contest starts no earlier than s hours 00 minutes local time and ends not later than f hours 00 minutes local time. Values s and f are equal for all time zones. If the contest starts at f hours 00 minutes local time, the person won't participate in it. Help platform select such an hour, that the number of people who will participate in the contest is maximum. -----Input----- The first line contains a single integer n (2 ≤ n ≤ 100 000) — the number of hours in day. The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10 000), where a_{i} is the number of people in the i-th timezone who want to participate in the contest. The third line contains two space-separated integers s and f (1 ≤ s < f ≤ n). -----Output----- Output a single integer — the time of the beginning of the contest (in the first timezone local time), such that the number of participants will be maximum possible. If there are many answers, output the smallest among them. -----Examples----- Input 3 1 2 3 1 3 Output 3 Input 5 1 2 3 4 1 1 3 Output 4 -----Note----- In the first example, it's optimal to start competition at 3 hours (in first timezone). In this case, it will be 1 hour in the second timezone and 2 hours in the third timezone. Only one person from the first timezone won't participate. In second example only people from the third and the fourth timezones will participate.
{"inputs": ["2\n5 1\n1 2\n", "2\n5 1\n1 2\n", "3\n1 2 3\n1 3\n", "3\n1 4 3\n1 3\n", "3\n1 2 3\n1 3\n", "2\n909 8422\n1 2\n", "5\n1 2 3 4 1\n1 3\n", "2\n5072 8422\n1 2\n"], "outputs": ["1\n", "1", "3\n", "3\n", "3", "2\n", "4\n", "2\n"]}
533
155
coding
Solve the programming task below in a Python markdown code block. Vasya has a grid with $2$ rows and $n$ columns. He colours each cell red, green, or blue. Vasya is colourblind and can't distinguish green from blue. Determine if Vasya will consider the two rows of the grid to be coloured the same. -----Input----- The input consists of multiple test cases. The first line contains an integer $t$ ($1 \leq t \leq 100$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $n$ ($1 \leq n \leq 100$) — the number of columns of the grid. The following two lines each contain a string consisting of $n$ characters, each of which is either R, G, or B, representing a red, green, or blue cell, respectively — the description of the grid. -----Output----- For each test case, output "YES" if Vasya considers the grid's two rows to be identical, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer). -----Examples----- Input 6 2 RG RB 4 GRBG GBGB 5 GGGGG BBBBB 7 BBBBBBB RRRRRRR 8 RGBRRGBR RGGRRBGR 1 G G Output YES NO YES NO YES YES -----Note----- In the first test case, Vasya sees the second cell of each row as the same because the second cell of the first row is green and the second cell of the second row is blue, so he can't distinguish these two cells. The rest of the rows are equal in colour. Therefore, Vasya will say that the two rows are coloured the same, even though they aren't. In the second test case, Vasya can see that the two rows are different. In the third test case, every cell is green or blue, so Vasya will think they are the same.
{"inputs": ["1\n5\nRRRRR\nRRRRR\n", "1\n7\nGGGRRBR\nGGGRRBB\n", "1\n7\nGRBBBBR\nGGGRRGG\n", "1\n30\nRBBBBBRRBRBBBBBRBBBBBRBBBBRBBB\nBBBRRBRRBBRBBBBRBBBBBRBBBBRBBB\n", "6\n2\nRG\nRB\n4\nGRBG\nGBGB\n5\nGGGGG\nBBBBB\n7\nBBBBBBB\nRRRRRRR\n8\nRGBRRGBR\nRGGRRBGR\n1\nG\nG\n", "1\n55\nBBBBBBBBBBBBBBBBBBBGGGGGGGGGGGGGGGGGGGGGGGGGGGGBBBBGBGB\nBBBBBBBBBBBBBBBBBBBGGGGGGGGGGGGGGGGGGGGGGGGGGGGBBBBGBGB\n"], "outputs": ["YES\n", "NO\n", "NO\n", "NO\n", "YES\nNO\nYES\nNO\nYES\nYES\n", "YES\n"]}
474
238
coding
Solve the programming task below in a Python markdown code block. # Task There are `n` bears in the orchard and they picked a lot of apples. They distribute apples like this: ``` The first bear divided the apple into n piles, each with the same number. leaving an apple at last, and the apple was thrown away. Then he took 1 pile and left the n-1 pile. The second bear divided the apple into n piles, each with the same number. leaving an apple at last, and the apple was thrown away. Then he took 1 pile and left the n-1 pile. and so on.. ``` Hmm.. your task is coming, please calculate the minimum possible number of these apples (before they start distributing) # Input/Output `[input]` integer `n` The number of bears. `2 <= n <= 9` (JS, Crystal and C) `2 <= n <= 50` (Ruby and Python) `[output]` an integer The minimum possible number of apples. # Example For `n = 5`, the output should be `3121`. ``` 5 bears distribute apples: 1st bear divided the apples into 5 piles, each pile has 624 apples, and 1 apple was thrown away. 3121 - 1 = 624 x 5 1st bear took 1 pile of apples, 2496 apples left. 3121 - 1 - 624 = 2496 2nd bear divided the apples into 5 piles, each pile has 499 apples, and 1 apple was thrown away. 2496 - 1 = 499 x 5 2nd bear took 1 pile of apples, 1996 apples left. 2496 - 1 - 499 = 1996 3rd bear divided the apples into 5 piles, each pile has 399 apples, and 1 apple was thrown away. 1996 - 1 = 399 x 5 3rd bear took 1 pile of apples, 1596 apples left. 1996 - 1 - 399 = 1596 4th bear divided the apples into 5 piles, each pile has 319 apples, and 1 apple was thrown away. 1596 - 1 = 319 x 5 4th bear took 1 pile of apples, 1276 apples left. 1596 - 1 - 319 = 1276 5th bear divided the apples into 5 piles, each pile has 255 apples, and 1 apple was thrown away. 1276 - 1 = 255 x 5 5th bear took 1 pile of apples, 1020 apples left. 1276 - 1 - 255 = 1020 ``` Also feel free to reuse/extend the following starter code: ```python def how_many_apples(n): ```
{"functional": "_inputs = [[2], [5]]\n_outputs = [[7], [3121]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(how_many_apples(*i), o[0])"}
677
166
coding
Solve the programming task below in a Python markdown code block. You are given an integer sequence $1, 2, \dots, n$. You have to divide it into two sets $A$ and $B$ in such a way that each element belongs to exactly one set and $|sum(A) - sum(B)|$ is minimum possible. The value $|x|$ is the absolute value of $x$ and $sum(S)$ is the sum of elements of the set $S$. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^9$). -----Output----- Print one integer — the minimum possible value of $|sum(A) - sum(B)|$ if you divide the initial sequence $1, 2, \dots, n$ into two sets $A$ and $B$. -----Examples----- Input 3 Output 0 Input 5 Output 1 Input 6 Output 1 -----Note----- Some (not all) possible answers to examples: In the first example you can divide the initial sequence into sets $A = \{1, 2\}$ and $B = \{3\}$ so the answer is $0$. In the second example you can divide the initial sequence into sets $A = \{1, 3, 4\}$ and $B = \{2, 5\}$ so the answer is $1$. In the third example you can divide the initial sequence into sets $A = \{1, 4, 5\}$ and $B = \{2, 3, 6\}$ so the answer is $1$.
{"inputs": ["3\n", "5\n", "6\n", "1\n", "2\n", "4\n", "4\n", "2\n"], "outputs": ["0\n", "1\n", "1\n", "1\n", "1\n", "0\n", "0\n", "1\n"]}
360
70
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. There is a 1-based binary matrix where 0 represents land and 1 represents water. You are given integers row and col representing the number of rows and columns in the matrix, respectively. Initially on day 0, the entire matrix is land. However, each day a new cell becomes flooded with water. You are given a 1-based 2D array cells, where cells[i] = [ri, ci] represents that on the ith day, the cell on the rith row and cith column (1-based coordinates) will be covered with water (i.e., changed to 1). You want to find the last day that it is possible to walk from the top to the bottom by only walking on land cells. You can start from any cell in the top row and end at any cell in the bottom row. You can only travel in the four cardinal directions (left, right, up, and down). Return the last day where it is possible to walk from the top to the bottom by only walking on land cells.   Please complete the following python code precisely: ```python class Solution: def latestDayToCross(self, row: int, col: int, cells: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(row = 2, col = 2, cells = [[1,1],[2,1],[1,2],[2,2]]) == 2\n assert candidate(row = 2, col = 2, cells = [[1,1],[1,2],[2,1],[2,2]]) == 1\n assert candidate(row = 3, col = 3, cells = [[1,2],[2,1],[3,3],[2,2],[1,1],[1,3],[2,3],[3,2],[3,1]]) == 3\n\n\ncheck(Solution().latestDayToCross)"}
270
149
coding
Solve the programming task below in a Python markdown code block. Write a program to determine if a string contains only unique characters. Return true if it does and false otherwise. The string may contain any of the 128 ASCII characters. Characters are case-sensitive, e.g. 'a' and 'A' are considered different characters. Also feel free to reuse/extend the following starter code: ```python def has_unique_chars(str): ```
{"functional": "_inputs = [[' nAa'], ['abcdef'], ['++-']]\n_outputs = [[False], [True], [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(has_unique_chars(*i), o[0])"}
91
171
coding
Solve the programming task below in a Python markdown code block. In programming languages like C/C++, a goto statement provides an unconditional jump from the "goto" to a labeled statement. For example, a statement "goto CHECK_NUM;" is executed, control of the program jumps to CHECK_NUM. Using these constructs, you can implement, for example, loops. Note that use of goto statement is highly discouraged, because it is difficult to trace the control flow of a program which includes goto. Write a program which does precisely the same thing as the following program (this example is wrtten in C++). Let's try to write the program without goto statements. void call(int n){ int i = 1; CHECK_NUM: int x = i; if ( x % 3 == 0 ){ cout << " " << i; goto END_CHECK_NUM; } INCLUDE3: if ( x % 10 == 3 ){ cout << " " << i; goto END_CHECK_NUM; } x /= 10; if ( x ) goto INCLUDE3; END_CHECK_NUM: if ( ++i <= n ) goto CHECK_NUM; cout << endl; } Constraints * 3 ≤ n ≤ 10000 Input An integer n is given in a line. Output Print the output result of the above program for given integer n. Example Input 30 Output 3 6 9 12 13 15 18 21 23 24 27 30
{"inputs": ["9", "7", "4", "53", "17", "28", "13", "25"], "outputs": [" 3 6 9\n", " 3 6\n", " 3\n", " 3 6 9 12 13 15 18 21 23 24 27 30 31 32 33 34 35 36 37 38 39 42 43 45 48 51 53\n", " 3 6 9 12 13 15\n", " 3 6 9 12 13 15 18 21 23 24 27\n", " 3 6 9 12 13\n", " 3 6 9 12 13 15 18 21 23 24\n"]}
318
233
coding
Solve the programming task below in a Python markdown code block. Athenaeus has just finished creating his latest musical composition and will present it tomorrow to the people of Athens. Unfortunately, the melody is rather dull and highly likely won't be met with a warm reception. His song consists of $n$ notes, which we will treat as positive integers. The diversity of a song is the number of different notes it contains. As a patron of music, Euterpe watches over composers and guides them throughout the process of creating new melodies. She decided to help Athenaeus by changing his song to make it more diverse. Being a minor goddess, she cannot arbitrarily change the song. Instead, for each of the $n$ notes in the song, she can either leave it as it is or increase it by $1$. Given the song as a sequence of integers describing the notes, find out the maximal, achievable diversity. -----Input----- The input consists of multiple test cases. The first line contains an integer $t$ ($1 \leq t \leq 10000$) — the number of test cases. Then $t$ test cases follow, each one is described in two lines. In the first line of each test case there is a single integer $n$ ($1 \leq n \leq 10^5$) denoting the length of the song. The next line contains a sequence of $n$ integers $x_1, x_2, \ldots, x_n$ $(1 \leq x_1 \leq x_2 \leq \ldots \leq x_n \leq 2 \cdot n)$, describing the song. The sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case, you should output a single line containing precisely one integer, the maximal diversity of the song, i.e. the maximal possible number of different elements in the final sequence. -----Examples----- Input 5 6 1 2 2 2 5 6 2 4 4 6 1 1 3 4 4 5 1 1 6 1 1 1 2 2 2 Output 5 2 6 1 3 -----Note----- In the first test case, Euterpe can increase the second, fifth and sixth element to obtain the sequence $1, \underline{3}, 2, 2, \underline{6}, \underline{7}$, which has $5$ different elements (increased elements are underlined). In the second test case, Euterpe can increase the first element to obtain the sequence $\underline{5}, 4$, which has $2$ different elements. In the third test case, Euterpe can increase the second, fifth and sixth element to obtain the sequence $1, \underline{2}, 3, 4, \underline{5}, \underline{6}$, which has $6$ different elements.
{"inputs": ["5\n6\n1 2 2 2 5 6\n2\n4 4\n6\n1 1 3 4 4 5\n1\n1\n6\n1 1 1 2 2 2\n", "5\n6\n1 2 2 2 5 6\n2\n2 4\n6\n1 1 3 4 4 5\n1\n1\n6\n1 1 1 2 2 2\n", "5\n6\n1 2 2 2 5 6\n2\n4 4\n6\n1 1 3 4 4 5\n1\n1\n6\n1 1 1 2 2 3\n", "5\n6\n1 2 2 4 5 6\n2\n2 4\n6\n1 1 3 4 4 5\n1\n1\n6\n1 1 2 2 2 2\n", "5\n6\n1 2 2 4 5 6\n2\n2 4\n6\n1 1 1 4 4 5\n1\n1\n6\n1 1 2 2 2 2\n", "5\n6\n1 2 2 4 5 6\n2\n2 4\n6\n1 1 3 4 4 5\n1\n1\n6\n1 2 2 2 2 3\n", "5\n6\n2 4 4 4 4 6\n2\n2 4\n6\n1 2 3 4 5 5\n1\n1\n6\n1 2 2 2 2 2\n", "5\n6\n2 2 4 4 5 6\n2\n2 4\n6\n1 4 4 4 5 5\n1\n1\n6\n1 2 2 2 2 2\n"], "outputs": ["5\n2\n6\n1\n3\n", "5\n2\n6\n1\n3\n", "5\n2\n6\n1\n4\n", "6\n2\n6\n1\n3\n", "6\n2\n5\n1\n3\n", "6\n2\n6\n1\n4\n", "4\n2\n6\n1\n3\n", "6\n2\n4\n1\n3\n"]}
640
550
coding
Solve the programming task below in a Python markdown code block. Luba is surfing the Internet. She currently has n opened tabs in her browser, indexed from 1 to n from left to right. The mouse cursor is currently located at the pos-th tab. Luba needs to use the tabs with indices from l to r (inclusive) for her studies, and she wants to close all the tabs that don't belong to this segment as fast as possible. Each second Luba can either try moving the cursor to the left or to the right (if the cursor is currently at the tab i, then she can move it to the tab max(i - 1, a) or to the tab min(i + 1, b)) or try closing all the tabs to the left or to the right of the cursor (if the cursor is currently at the tab i, she can close all the tabs with indices from segment [a, i - 1] or from segment [i + 1, b]). In the aforementioned expressions a and b denote the minimum and maximum index of an unclosed tab, respectively. For example, if there were 7 tabs initially and tabs 1, 2 and 7 are closed, then a = 3, b = 6. What is the minimum number of seconds Luba has to spend in order to leave only the tabs with initial indices from l to r inclusive opened? -----Input----- The only line of input contains four integer numbers n, pos, l, r (1 ≤ n ≤ 100, 1 ≤ pos ≤ n, 1 ≤ l ≤ r ≤ n) — the number of the tabs, the cursor position and the segment which Luba needs to leave opened. -----Output----- Print one integer equal to the minimum number of seconds required to close all the tabs outside the segment [l, r]. -----Examples----- Input 6 3 2 4 Output 5 Input 6 3 1 3 Output 1 Input 5 2 1 5 Output 0 -----Note----- In the first test Luba can do the following operations: shift the mouse cursor to the tab 2, close all the tabs to the left of it, shift the mouse cursor to the tab 3, then to the tab 4, and then close all the tabs to the right of it. In the second test she only needs to close all the tabs to the right of the current position of the cursor. In the third test Luba doesn't need to do anything.
{"inputs": ["6 3 2 4\n", "6 3 1 3\n", "5 2 1 5\n", "6 4 2 5\n", "7 4 2 5\n", "6 5 2 4\n", "5 4 2 4\n", "6 6 3 5\n"], "outputs": ["5\n", "1\n", "0\n", "6\n", "6\n", "5\n", "4\n", "5\n"]}
528
118
coding
Solve the programming task below in a Python markdown code block. Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
{"inputs": ["2\n1\n7\n"], "outputs": ["0\n3\n"]}
585
20
coding
Solve the programming task below in a Python markdown code block. How Many Divisors? Write a program which reads three integers a, b and c, and prints the number of divisors of c between a and b. Constraints * 1 ≤ a, b, c ≤ 10000 * a ≤ b Input Three integers a, b and c are given in a line separated by a single space. Output Print the number of divisors in a line. Example Input 5 14 80 Output 3
{"inputs": ["6 2 4", "6 2 1", "6 1 1", "6 0 1", "4 0 0", "1 0 0", "2 0 0", "1 1 0"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "1\n"]}
116
94
coding
Solve the programming task below in a Python markdown code block. You will be given the prime factors of a number as an array. E.g: ```[2,2,2,3,3,5,5,13]``` You need to find the number, n, to which that prime factorization belongs. It will be: ``` n = 2³.3².5².13 = 23400 ``` Then, generate the divisors of this number. Your function ```get_num() or getNum()``` will receive an array with potentially unordered prime factors and should output: an array with the found integer n at index 0, the amount of total divisors (both prime and compound numbers) at index 1, followed the smallest factor (index 2, and the biggest one (last element) We will see the example given above with the only difference that the array of the prime factors is unordered. The list of divisors for that number (23400) is: ``` 2, 3, 4, 5, 6, 8, 9, 10, 12, 13, 15, 18, 20, 24, 25, 26, 30, 36, 39, 40, 45, 50, 52, 60, 65, 72, 75, 78, 90, 100, 104, 117, 120, 130, 150, 156, 180, 195, 200, 225, 234, 260, 300, 312, 325, 360, 390, 450, 468, 520, 585, 600, 650, 780, 900, 936, 975, 1170, 1300, 1560, 1800, 1950, 2340, 2600, 2925, 3900, 4680, 5850, 7800, 11700 (not considering the integer 23400 itself) ``` There is a total amount of ```71``` divisors. The smallest divisor is ```2``` and the highest ```11700```. So the expected output will be: ``` get_num([2,13,2,5,2,5,3,3]) == [23400, 71, 2, 11700] ``` Enjoy! Also feel free to reuse/extend the following starter code: ```python def get_num(arr): ```
{"functional": "_inputs = [[[2, 3, 5, 5]], [[2, 3, 3, 3, 7]], [[3, 3, 3, 11]], [[2, 13, 2, 5, 2, 5, 3, 3]]]\n_outputs = [[[150, 11, 2, 75]], [[378, 15, 2, 189]], [[297, 7, 3, 99]], [[23400, 71, 2, 11700]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(get_num(*i), o[0])"}
668
282
coding
Solve the programming task below in a Python markdown code block. Today at the lesson Vitya learned a very interesting function — mex. Mex of a sequence of numbers is the minimum non-negative number that is not present in the sequence as element. For example, mex([4, 33, 0, 1, 1, 5]) = 2 and mex([1, 2, 3]) = 0. Vitya quickly understood all tasks of the teacher, but can you do the same? You are given an array consisting of n non-negative integers, and m queries. Each query is characterized by one number x and consists of the following consecutive steps: * Perform the bitwise addition operation modulo 2 (xor) of each array element with the number x. * Find mex of the resulting array. Note that after each query the array changes. Input First line contains two integer numbers n and m (1 ≤ n, m ≤ 3·105) — number of elements in array and number of queries. Next line contains n integer numbers ai (0 ≤ ai ≤ 3·105) — elements of then array. Each of next m lines contains query — one integer number x (0 ≤ x ≤ 3·105). Output For each query print the answer on a separate line. Examples Input 2 2 1 3 1 3 Output 1 0 Input 4 3 0 1 5 6 1 2 4 Output 2 0 0 Input 5 4 0 1 5 6 7 1 1 4 5 Output 2 2 0 2
{"inputs": ["2 2\n1 6\n1\n3\n", "2 2\n2 5\n1\n1\n", "2 2\n1 3\n2\n3\n", "2 2\n1 6\n1\n0\n", "2 2\n1 6\n1\n1\n", "2 2\n1 3\n1\n1\n", "2 2\n1 5\n1\n1\n", "2 2\n2 5\n2\n1\n"], "outputs": ["1\n0\n", "0\n0\n", "0\n1\n", "1\n1\n", "1\n0\n", "1\n0\n", "1\n0\n", "1\n0\n"]}
364
166
coding
Solve the programming task below in a Python markdown code block. Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this integer is between 1 and 100 000, inclusive. It is possible that some cards have the same integers on them. Vasily decided to sort the cards. To do this, he repeatedly takes the top card from the deck, and if the number on it equals the minimum number written on the cards in the deck, then he places the card away. Otherwise, he puts it under the deck and takes the next card from the top, and so on. The process ends as soon as there are no cards in the deck. You can assume that Vasily always knows the minimum number written on some card in the remaining deck, but doesn't know where this card (or these cards) is. You are to determine the total number of times Vasily takes the top card from the deck. -----Input----- The first line contains single integer n (1 ≤ n ≤ 100 000) — the number of cards in the deck. The second line contains a sequence of n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 100 000), where a_{i} is the number written on the i-th from top card in the deck. -----Output----- Print the total number of times Vasily takes the top card from the deck. -----Examples----- Input 4 6 3 1 2 Output 7 Input 1 1000 Output 1 Input 7 3 3 3 3 3 3 3 Output 7 -----Note----- In the first example Vasily at first looks at the card with number 6 on it, puts it under the deck, then on the card with number 3, puts it under the deck, and then on the card with number 1. He places away the card with 1, because the number written on it is the minimum among the remaining cards. After that the cards from top to bottom are [2, 6, 3]. Then Vasily looks at the top card with number 2 and puts it away. After that the cards from top to bottom are [6, 3]. Then Vasily looks at card 6, puts it under the deck, then at card 3 and puts it away. Then there is only one card with number 6 on it, and Vasily looks at it and puts it away. Thus, in total Vasily looks at 7 cards.
{"inputs": ["1\n1000\n", "1\n1001\n", "1\n1011\n", "1\n1111\n", "1\n0111\n", "1\n1010\n", "1\n1110\n", "1\n1100\n"], "outputs": ["1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n"]}
553
110
coding
Solve the programming task below in a Python markdown code block. Given is a permutation P of \{1, 2, \ldots, N\}. For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest value among P_L, P_{L+1}, \ldots, P_R. Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}. -----Constraints----- - 2 \le N \le 10^5 - 1 \le P_i \le N - P_i \neq P_j (i \neq j) - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: N P_1 P_2 \ldots P_N -----Output----- Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}. -----Sample Input----- 3 2 3 1 -----Sample Output----- 5 X_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.
{"inputs": ["2\n1 2\n", "3\n1 3 2", "3\n3 1 2", "3\n3 2 1", "3\n2 1 3", "3\n1 2 3", "3\n1 3 2", "3\n1 2 3"], "outputs": ["1\n", "5\n", "4\n", "5\n", "4\n", "5\n", "5\n", "5\n"]}
302
109
coding
Solve the programming task below in a Python markdown code block. Your task is to make function, which returns the sum of a sequence of integers. The sequence is defined by 3 non-negative values: **begin**, **end**, **step**. If **begin** value is greater than the **end**, function should returns **0** *Examples* ~~~if-not:nasm ~~~ This is the first kata in the series: 1) Sum of a sequence (this kata) 2) [Sum of a Sequence [Hard-Core Version]](https://www.codewars.com/kata/sum-of-a-sequence-hard-core-version/javascript) Also feel free to reuse/extend the following starter code: ```python def sequence_sum(begin_number, end_number, step): ```
{"functional": "_inputs = [[2, 6, 2], [1, 5, 1], [1, 5, 3], [0, 15, 3], [16, 15, 3], [2, 24, 22], [2, 2, 2], [2, 2, 1], [1, 15, 3], [15, 1, 3]]\n_outputs = [[12], [15], [5], [45], [0], [26], [2], [2], [35], [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(sequence_sum(*i), o[0])"}
161
280
coding
Solve the programming task below in a Python markdown code block. Cheffina challanges chef to rearrange the given array as arr[i] > arr[i+1] < arr[i+2] > arr[i+3].. and so on…, i.e. also arr[i] < arr[i+2] and arr[i+1] < arr[i+3] and arr[i] < arr[i+3] so on.. Chef accepts the challenge, chef starts coding but his code is not compiling help him to write new code. -----Input:----- - First-line will contain $T$, the number of test cases. Then the test cases follow. - Each test case contains two lines of input, First $N$ as the size of the array. - N space-separated distinct integers. -----Output:----- For each test case, output in a single line answer given to the Chefffina. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^5$ - $1 \leq arr[i] \leq 10^5$ -----Sample Input:----- 2 4 4 1 6 3 5 4 5 1 6 3 -----Sample Output:----- 3 1 6 4 3 1 5 4 6
{"inputs": ["2\n4\n4 1 6 3\n5\n4 5 1 6 3"], "outputs": ["3 1 6 4\n3 1 5 4 6"]}
290
50
coding
Solve the programming task below in a Python markdown code block. The Romans have attacked again. This time they are much more than the Persians but Shapur is ready to defeat them. He says: "A lion is never afraid of a hundred sheep". Nevertheless Shapur has to find weaknesses in the Roman army to defeat them. So he gives the army a weakness number. In Shapur's opinion the weakness of an army is equal to the number of triplets i, j, k such that i < j < k and ai > aj > ak where ax is the power of man standing at position x. The Roman army has one special trait — powers of all the people in it are distinct. Help Shapur find out how weak the Romans are. Input The first line of input contains a single number n (3 ≤ n ≤ 106) — the number of men in Roman army. Next line contains n different positive integers ai (1 ≤ i ≤ n, 1 ≤ ai ≤ 109) — powers of men in the Roman army. Output A single integer number, the weakness of the Roman army. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). Examples Input 3 3 2 1 Output 1 Input 3 2 3 1 Output 0 Input 4 10 8 3 1 Output 4 Input 4 1 5 4 3 Output 1
{"inputs": ["3\n5 2 1\n", "3\n4 3 1\n", "3\n4 2 1\n", "3\n3 2 1\n", "3\n2 3 1\n", "3\n2 7 11\n", "4\n7 8 3 1\n", "4\n1 5 0 4\n"], "outputs": ["1\n", "1\n", "1\n", "1", "0", "0", "2\n", "0\n"]}
337
120
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a digit string s that consists of digits from 0 to 9. A string is called semi-repetitive if there is at most one adjacent pair of the same digit. For example, "0010", "002020", "0123", "2002", and "54944" are semi-repetitive while the following are not: "00101022" (adjacent same digit pairs are 00 and 22), and "1101234883" (adjacent same digit pairs are 11 and 88). Return the length of the longest semi-repetitive substring of s.   Please complete the following python code precisely: ```python class Solution: def longestSemiRepetitiveSubstring(self, s: str) -> int: ```
{"functional": "def check(candidate):\n assert candidate(s = \"52233\") == 4\n assert candidate(s = \"5494\") == 4\n assert candidate(s = \"1111111\") == 2\n\n\ncheck(Solution().longestSemiRepetitiveSubstring)"}
206
74
coding
Solve the programming task below in a Python markdown code block. You have an array $a_1, a_2, \dots, a_n$. Let's call some subarray $a_l, a_{l + 1}, \dots , a_r$ of this array a subpermutation if it contains all integers from $1$ to $r-l+1$ exactly once. For example, array $a = [2, 2, 1, 3, 2, 3, 1]$ contains $6$ subarrays which are subpermutations: $[a_2 \dots a_3]$, $[a_2 \dots a_4]$, $[a_3 \dots a_3]$, $[a_3 \dots a_5]$, $[a_5 \dots a_7]$, $[a_7 \dots a_7]$. You are asked to calculate the number of subpermutations. -----Input----- The first line contains one integer $n$ ($1 \le n \le 3 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le n$). This array can contain the same integers. -----Output----- Print the number of subpermutations of the array $a$. -----Examples----- Input 8 2 4 1 3 4 2 1 2 Output 7 Input 5 1 1 2 1 2 Output 6 -----Note----- There are $7$ subpermutations in the first test case. Their segments of indices are $[1, 4]$, $[3, 3]$, $[3, 6]$, $[4, 7]$, $[6, 7]$, $[7, 7]$ and $[7, 8]$. In the second test case $6$ subpermutations exist: $[1, 1]$, $[2, 2]$, $[2, 3]$, $[3, 4]$, $[4, 4]$ and $[4, 5]$.
{"inputs": ["5\n1 1 2 1 2\n", "5\n1 1 2 1 2\n", "8\n2 4 1 3 4 2 1 2\n", "8\n2 4 1 3 4 3 1 2\n", "8\n2 7 1 3 4 3 1 2\n", "8\n2 8 2 3 4 3 1 2\n", "8\n2 8 1 3 4 6 1 2\n", "8\n2 4 1 3 4 6 2 2\n"], "outputs": ["6\n", "6\n", "7\n", "6\n", "5\n", "4\n", "3\n", "2\n"]}
480
186
coding
Solve the programming task below in a Python markdown code block. Given integers ${b}$ and $a$, find the smallest integer ${h}$, such that there exists a triangle of height ${h}$, base ${b}$, having an area of at least $a$. Example $b=4$ ${a=6}$ The minimum height ${h}$ is $3$. One example is a triangle formed at points (0, 0), (4, 0), (2, 3). Function Description Complete the lowestTriangle function in the editor below. lowestTriangle has the following parameters: int b: the base of the triangle int a: the minimum area of the triangle Returns int: the minimum integer height to form a triangle with an area of at least $a$ Input Format There are two space-separated integers ${b}$ and $a$, on a single line. Constraints $1\leq b\leq10^{6}$ $1\leq a\leq10^{6}$ Sample Input 0 2 2 Sample Output 0 2 Explanation 0 The task is to find the smallest integer height of the triangle with base $2$ and area at least $2$. It turns out, that there are triangles with height $2$, base $2$ and area $2$, for example a triangle with corners in the following points: $(1,1),(3,1),(1,3)$: It can be proved that there is no triangle with integer height smaller than $2$, base $2$ and area at least $2$. Sample Input 1 17 100 Sample Output 1 12 Explanation 1 The task is to find the smallest integer height of the triangle with base $17$ and area at least $100$.It turns out,that there are triangels with height $12$,base $17$ and area $102$ for example a triangle with corners in the following points: $(2,2),(19,2),(16,14)$. It can be proved that there is no triangle with integer height smaller than $12$, base $17$ and area at least $100$
{"inputs": ["2 2\n", "17 100\n"], "outputs": ["2\n", "12\n"]}
485
30
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. There are n children standing in a line. Each child is assigned a rating value given in the integer array ratings. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more candies than their neighbors. Return the minimum number of candies you need to have to distribute the candies to the children.   Please complete the following python code precisely: ```python class Solution: def candy(self, ratings: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(ratings = [1,0,2]) == 5\n assert candidate(ratings = [1,2,2]) == 4\n\n\ncheck(Solution().candy)"}
122
53
coding
Solve the programming task below in a Python markdown code block. Complete the function that calculates the derivative of a polynomial. A polynomial is an expression like: 3x^(4) - 2x^(2) + x - 10 ### How to calculate the derivative: * Take the exponent and multiply it with the coefficient * Reduce the exponent by 1 For example: 3x^(4) --> (4*3)x^((4-1)) = 12x^(3) ### Good to know: * The derivative of a constant is 0 (e.g. 100 --> 0) * Anything to the 0th exponent equals 1 (e.g. x^(0) = 1) * The derivative of the sum of two function is the sum of the derivatives Notes: * The exponentiation is marked with "^" * Exponents are always integers and >= 0 * Exponents are written only if > 1 * There are no spaces around the operators * Leading "+" signs are omitted See the examples below. ## Examples ```python derivative("-100") = "0" derivative("4x+1") = "4" # = 4x^0 + 0 derivative("-x^2+3x+4") = "-2x+3" # = -2x^1 + 3x^0 + 0 ``` Also feel free to reuse/extend the following starter code: ```python def derivative(eq): ```
{"functional": "_inputs = [['4x+1'], ['-4x-1'], ['x^2+2x+1'], ['0'], ['-100'], ['-x^2+3x+4'], ['-x^5-x^4-x^3'], ['10x^9+10x^3+10x'], ['100x^5+12x^3-3x-3'], ['-1000x^7+200x^4+6x^2+x+1000']]\n_outputs = [['4'], ['-4'], ['2x+2'], ['0'], ['0'], ['-2x+3'], ['-5x^4-4x^3-3x^2'], ['90x^8+30x^2+10'], ['500x^4+36x^2-3'], ['-7000x^6+800x^3+12x+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(derivative(*i), o[0])"}
327
357
coding
Solve the programming task below in a Python markdown code block. Given an integer N, Chef wants to find the smallest positive integer M such that the bitwise XOR of M and M+1 is N. If no such M exists output -1. -----Input----- The first line of input contain an integer T denoting the number of test cases. Each of the following T lines contains an integer N for that test case. -----Output----- For each test case, output a single line containing the number M or -1 as described above. -----Constraints----- - 1 ≤ T ≤ 5000 - 1 ≤ N ≤ 230 -----Example----- Input: 1 3 Output: 1 -----Explanation-----First Example : M desired in the problem would be 1. As bitwise XOR of 1 and 2 is equal to 3.
{"inputs": ["1\n3", "1\n1", "1\n2", "1\n7", "1\n6", "1\n4", "1\n8", "1\n5"], "outputs": ["1", "2\n", "-1\n", "3\n", "-1\n", "-1\n", "-1\n", "-1\n"]}
179
77
coding
Solve the programming task below in a Python markdown code block. Computation of the date either previous or forthcoming dates is quiet easy. But it is quiet difficult to calculate the day from a particular given date. You are required to find a day from a particular date given to you. -----Input----- It consists of a single line entry consisting of date in format dd mm yyyy. i.e. the input line consists of the three numbers written in order followed by spaces. Eg. Input for 18-12-1990 is be written as 18 12 1990 -----Output----- It consists of single line output showing the day for that particular date. -----Example----- Input: 14 3 2012 Output: Wednesday
{"inputs": ["14 3 2012"], "outputs": ["Wednesday"]}
162
20
coding
Solve the programming task below in a Python markdown code block. There are n squirrel(s) waiting below the feet of m chestnut tree(s). The first chestnut of the i-th tree will fall right after T_{i} second(s), and one more every P_{i} second(s) after that. The “big mama” of squirrels wants them to bring their nest no less than k chestnuts to avoid the big storm coming, as fast as possible! So they are discussing to wait below which trees to take enough chestnuts in the shortest time. Time to move to the positions is zero, and the squirrels move nowhere after that. ------ Request ------ Calculate the shortest time (how many seconds more) the squirrels can take enough chestnuts. ------ Input ------ The first line contains an integer t, the number of test cases, for each: The first line contains the integers m,n,k, respectively. The second line contains the integers T_{i} (i=1..m), respectively. The third line contains the integers P_{i} (i=1..m), respectively. (Each integer on a same line is separated by at least one space character, there is no added line between test cases) ------ Output ------ For each test cases, output in a single line an integer which is the shortest time calculated. ------ Limitations ------ 0 0 07 0i,P_{i}≤100 ----- Sample Input 1 ------ 2 3 2 5 5 1 2 1 2 1 3 2 5 5 1 2 1 1 1 ----- Sample Output 1 ------ 4 3 ----- explanation 1 ------ Test case $1$: Two squirrels wait below trees $2$ and $3$. - After $1$ second, a chestnut from tree $2$ falls. Total chestnuts the squirrels have is $1$. - After $2$ seconds, a chestnut from tree $3$ falls. Total chestnuts the squirrels have is $2$. - After $3$ seconds, two chestnuts fall from trees $2$ and $3$. Total chestnuts the squirrels have is $4$. - After $4$ seconds, a chestnut from tree $3$ falls. Total chestnuts the squirrels have is $5$. Total time to gather $5$ chestnuts is $5$ seconds. Test case $2$: Two squirrels wait below trees $2$ and $3$. - After $1$ second, a chestnut from tree $2$ falls. Total chestnuts the squirrels have is $1$. - After $2$ seconds, two chestnuts fall from trees $2$ and $3$. Total chestnuts the squirrels have is $3$. - After $3$ seconds, two chestnuts fall from trees $2$ and $3$. Total chestnuts the squirrels have is $5$. Total time to gather $5$ chestnuts is $3$ seconds.
{"inputs": ["2\n3 2 5\n5 1 2\n1 2 1\n3 2 5\n5 1 2\n1 1 1"], "outputs": ["4\n3"]}
642
50
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. In an alien language, surprisingly, they also use English lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters. Given a sequence of words written in the alien language, and the order of the alphabet, return true if and only if the given words are sorted lexicographically in this alien language.   Please complete the following python code precisely: ```python class Solution: def isAlienSorted(self, words: List[str], order: str) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(words = [\"hello\",\"leetcode\"], order = \"hlabcdefgijkmnopqrstuvwxyz\") == True\n assert candidate(words = [\"word\",\"world\",\"row\"], order = \"worldabcefghijkmnpqstuvxyz\") == False\n assert candidate(words = [\"apple\",\"app\"], order = \"abcdefghijklmnopqrstuvwxyz\") == False\n\n\ncheck(Solution().isAlienSorted)"}
124
99
coding
Solve the programming task below in a Python markdown code block. # Task: Based on the received dimensions, `a` and `b`, of an ellipse, calculare its area and perimeter. ## Example: ```python Input: ellipse(5,2) Output: "Area: 31.4, perimeter: 23.1" ``` **Note:** The perimeter approximation formula you should use: `π * (3/2(a+b) - sqrt(ab))` ___ ## Have fun :) Also feel free to reuse/extend the following starter code: ```python def ellipse(a, b): ```
{"functional": "_inputs = [[5, 2], [6, 8], [13, 1]]\n_outputs = [['Area: 31.4, perimeter: 23.1'], ['Area: 150.8, perimeter: 44.2'], ['Area: 40.8, perimeter: 54.6']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(ellipse(*i), o[0])"}
128
219
coding
Solve the programming task below in a Python markdown code block. A has a string consisting of some number of lowercase English letters 'a'. He gives it to his friend B who appends some number of letters 'b' to the end of this string. Since both A and B like the characters 'a' and 'b', they have made sure that at this point, at least one 'a' and one 'b' exist in the string. B now gives this string to C and he appends some number of letters 'c' to the end of the string. However, since C is a good friend of A and B, the number of letters 'c' he appends is equal to the number of 'a' or to the number of 'b' in the string. It is also possible that the number of letters 'c' equals both to the number of letters 'a' and to the number of letters 'b' at the same time. You have a string in your hands, and you want to check if it is possible to obtain the string in this way or not. If it is possible to obtain the string, print "YES", otherwise print "NO" (without the quotes). -----Input----- The first and only line consists of a string $S$ ($ 1 \le |S| \le 5\,000 $). It is guaranteed that the string will only consist of the lowercase English letters 'a', 'b', 'c'. -----Output----- Print "YES" or "NO", according to the condition. -----Examples----- Input aaabccc Output YES Input bbacc Output NO Input aabc Output YES -----Note----- Consider first example: the number of 'c' is equal to the number of 'a'. Consider second example: although the number of 'c' is equal to the number of the 'b', the order is not correct. Consider third example: the number of 'c' is equal to the number of 'b'.
{"inputs": ["c\n", "a\n", "b\n", "c\n", "b\n", "a\n", "cc\n", "bc\n"], "outputs": ["NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n"]}
421
70
coding
Solve the programming task below in a Python markdown code block. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b_1, b_2, ..., b_{n}, consisting of zeros and ones. Each second Pavel move skewer on position i to position p_{i}, and if b_{i} equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. -----Input----- The first line contain the integer n (1 ≤ n ≤ 2·10^5) — the number of skewers. The second line contains a sequence of integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b_1, b_2, ..., b_{n} consisting of zeros and ones, according to which Pavel wants to reverse the skewers. -----Output----- Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. -----Examples----- Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 -----Note----- In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1.
{"inputs": ["1\n1\n0\n", "1\n1\n1\n", "1\n1\n0\n", "1\n1\n1\n", "2\n1 2\n0 0\n", "2\n2 1\n0 0\n", "2\n1 2\n0 1\n", "2\n2 1\n1 0\n"], "outputs": ["1\n", "0\n", "1\n", "0\n", "3\n", "1\n", "2\n", "0\n"]}
541
118
coding
Solve the programming task below in a Python markdown code block. You have N coins each of which has a value ai. Find the number of combinations that result when you choose K different coins in such a way that the total value of the coins is greater than or equal to L and less than or equal to R. Constraints * 1 ≤ K ≤ N ≤ 40 * 1 ≤ ai ≤ 1016 * 1 ≤ L ≤ R ≤ 1016 * All input values are given in integers Input The input is given in the following format. N K L R a1 a2 ... aN Output Print the number of combinations in a line. Examples Input 2 2 1 9 5 1 Output 1 Input 5 2 7 19 3 5 4 2 2 Output 5
{"inputs": ["2 2 2 9\n5 1", "2 2 2 9\n9 1", "2 1 1 9\n6 1", "2 2 2 9\n9 2", "2 2 2 9\n1 2", "2 3 2 9\n1 2", "2 3 2 9\n1 0", "2 3 1 9\n1 0"], "outputs": ["1\n", "0\n", "2\n", "0\n", "1\n", "0\n", "0\n", "0\n"]}
192
142
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given two strings first and second, consider occurrences in some text of the form "first second third", where second comes immediately after first, and third comes immediately after second. Return an array of all the words third for each occurrence of "first second third".   Please complete the following python code precisely: ```python class Solution: def findOcurrences(self, text: str, first: str, second: str) -> List[str]: ```
{"functional": "def check(candidate):\n assert candidate(text = \"alice is a good girl she is a good student\", first = \"a\", second = \"good\") == [\"girl\",\"student\"]\n assert candidate(text = \"we will we will rock you\", first = \"we\", second = \"will\") == [\"we\",\"rock\"]\n\n\ncheck(Solution().findOcurrences)"}
109
89
coding
Solve the programming task below in a Python markdown code block. Given are an integer N and four characters c_{\mathrm{AA}}, c_{\mathrm{AB}}, c_{\mathrm{BA}}, and c_{\mathrm{BB}}. Here, it is guaranteed that each of those four characters is A or B. Snuke has a string s, which is initially AB. Let |s| denote the length of s. Snuke can do the four kinds of operations below zero or more times in any order: - Choose i such that 1 \leq i < |s|, s_{i} = A, s_{i+1} = A and insert c_{\mathrm{AA}} between the i-th and (i+1)-th characters of s. - Choose i such that 1 \leq i < |s|, s_{i} = A, s_{i+1} = B and insert c_{\mathrm{AB}} between the i-th and (i+1)-th characters of s. - Choose i such that 1 \leq i < |s|, s_{i} = B, s_{i+1} = A and insert c_{\mathrm{BA}} between the i-th and (i+1)-th characters of s. - Choose i such that 1 \leq i < |s|, s_{i} = B, s_{i+1} = B and insert c_{\mathrm{BB}} between the i-th and (i+1)-th characters of s. Find the number, modulo (10^9+7), of strings that can be s when Snuke has done the operations so that the length of s becomes N. -----Constraints----- - 2 \leq N \leq 1000 - Each of c_{\mathrm{AA}}, c_{\mathrm{AB}}, c_{\mathrm{BA}}, and c_{\mathrm{BB}} is A or B. -----Input----- Input is given from Standard Input in the following format: N c_{\mathrm{AA}} c_{\mathrm{AB}} c_{\mathrm{BA}} c_{\mathrm{BB}} -----Output----- Print the number, modulo (10^9+7), of strings that can be s when Snuke has done the operations so that the length of s becomes N. -----Sample Input----- 4 A B B A -----Sample Output----- 2 - There are two strings that can be s when Snuke is done: ABAB and ABBB.
{"inputs": ["4\nA\nB\nB\nA\n", "2\nB\nA\nA\nA\n", "2\nA\nB\nA\nA\n", "2\nB\nB\nA\nA\n", "2\nA\nA\nB\nA\n", "2\nB\nA\nB\nA\n", "2\nA\nB\nB\nA\n", "2\nB\nB\nB\nA\n"], "outputs": ["2\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n"]}
547
134
coding
Solve the programming task below in a Python markdown code block. Chef has a string S with him. Chef is happy if the string contains a contiguous substring of length strictly greater than 2 in which all its characters are vowels. Determine whether Chef is happy or not. Note that, in english alphabet, vowels are a, e, i, o, and u. ------ Input Format ------ - First line will contain T, number of test cases. Then the test cases follow. - Each test case contains of a single line of input, a string S. ------ Output Format ------ For each test case, if Chef is happy, print HAPPY else print SAD. You may print each character of the string in uppercase or lowercase (for example, the strings hAppY, Happy, haPpY, and HAPPY will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 1000$ $3 ≤ |S| ≤ 1000$, where $|S|$ is the length of $S$. $S$ will only contain lowercase English letters. ----- Sample Input 1 ------ 4 aeiou abxy aebcdefghij abcdeeafg ----- Sample Output 1 ------ Happy Sad Sad Happy ----- explanation 1 ------ Test case $1$: Since the string aeiou is a contiguous substring and consists of all vowels and has a length $> 2$, Chef is happy. Test case $2$: Since none of the contiguous substrings of the string consist of all vowels and have a length $> 2$, Chef is sad. Test case $3$: Since none of the contiguous substrings of the string consist of all vowels and have a length $> 2$, Chef is sad. Test case $4$: Since the string eea is a contiguous substring and consists of all vowels and has a length $> 2$, Chef is happy.
{"inputs": ["4\naeiou\nabxy\naebcdefghij\nabcdeeafg\n"], "outputs": ["Happy\nSad\nSad\nHappy\n"]}
405
38
coding
Solve the programming task below in a Python markdown code block. Let's write all the positive integer numbers one after another from $1$ without any delimiters (i.e. as a single string). It will be the infinite sequence starting with 123456789101112131415161718192021222324252627282930313233343536... Your task is to print the $k$-th digit of this sequence. -----Input----- The first and only line contains integer $k$ ($1 \le k \le 10^{12}$) — the position to process ($1$-based index). -----Output----- Print the $k$-th digit of the resulting infinite sequence. -----Examples----- Input 7 Output 7 Input 21 Output 5
{"inputs": ["7\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "8\n"], "outputs": ["7\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "8\n"]}
206
70
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a string s, return the length of the longest substring between two equal characters, excluding the two characters. If there is no such substring return -1. A substring is a contiguous sequence of characters within a string.   Please complete the following python code precisely: ```python class Solution: def maxLengthBetweenEqualCharacters(self, s: str) -> int: ```
{"functional": "def check(candidate):\n assert candidate(s = \"aa\") == 0\n assert candidate(s = \"abca\") == 2\n assert candidate(s = \"cbzxy\") == -1\n assert candidate(s = \"cabbac\") == 4\n\n\ncheck(Solution().maxLengthBetweenEqualCharacters)"}
93
74
coding
Solve the programming task below in a Python markdown code block. The ship crashed into a reef and is sinking. Now the entire crew must be evacuated. All n crew members have already lined up in a row (for convenience let's label them all from left to right with positive integers from 1 to n) and await further instructions. However, one should evacuate the crew properly, in a strict order. Specifically: The first crew members to leave the ship are rats. Then women and children (both groups have the same priority) leave the ship. After that all men are evacuated from the ship. The captain leaves the sinking ship last. If we cannot determine exactly who should leave the ship first for any two members of the crew by the rules from the previous paragraph, then the one who stands to the left in the line leaves the ship first (or in other words, the one whose number in the line is less). For each crew member we know his status as a crew member, and also his name. All crew members have different names. Determine the order in which to evacuate the crew. Input The first line contains an integer n, which is the number of people in the crew (1 ≤ n ≤ 100). Then follow n lines. The i-th of those lines contains two words — the name of the crew member who is i-th in line, and his status on the ship. The words are separated by exactly one space. There are no other spaces in the line. The names consist of Latin letters, the first letter is uppercase, the rest are lowercase. The length of any name is from 1 to 10 characters. The status can have the following values: rat for a rat, woman for a woman, child for a child, man for a man, captain for the captain. The crew contains exactly one captain. Output Print n lines. The i-th of them should contain the name of the crew member who must be the i-th one to leave the ship. Examples Input 6 Jack captain Alice woman Charlie man Teddy rat Bob child Julia woman Output Teddy Alice Bob Julia Charlie Jack
{"inputs": ["1\nA captain\n", "2\nA captain\nB man\n", "2\nX captain\nY rat\n", "2\nX captain\nZ rat\n", "2\nX child\nY captain\n", "2\nB woman\nA captain\n", "2\nX child\nW captain\n", "2\nB woman\n@ captain\n"], "outputs": ["A\n", "B\nA\n", "Y\nX\n", "Z\nX\n", "X\nY\n", "B\nA\n", "X\nW\n", "B\n@\n"]}
448
129
coding
Solve the programming task below in a Python markdown code block. You are given an array A of length N. The *interesting value* of a subarray is defined as the product of the maximum and minimum elements of the subarray. Find the minimum and maximum *interesting value* over all subarrays for the given array. Note: A subarray is obtained by deletion of several (possibly zero) elements from the beginning of the array and several (possibly zero) elements from the end of the array. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The first line of each test case contains an integer N - the length of the array A. - The second line of each test case contains N space-separated integers A_{1},A_{2},\ldots,A_{N}. ------ Output Format ------ For each test case, output two space-separated integers on a new line the minimum and maximum *interesting value* over all subarrays for the given array. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 10^{5}$ $-10^{9} ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ over all test cases won't exceed $3 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 2 2 2 3 5 0 9 ----- Sample Output 1 ------ 4 4 0 81 ----- explanation 1 ------ Test case $1$: The minimum *interesting value* possible is $4$. A subarray with *interesting value* equal to $4$ is $[2,2]$. Here, both minimum and maximum elements of the subarray are $2$. It can be proven that no subarray of the array has *interesting value* less than $4$. The maximum *interesting value* possible is $4$. A subarray with *interesting value* equal to $4$ is $[2,2]$. Here, both minimum and maximum elements of the subarray are $2$. It can be proven that no subarray of the array has *interesting value* greater than $4$. Test case $2$: The minimum *interesting value* possible is $0$. A subarray with *interesting value* equal to $0$ is $[5, 0, 9]$. Here, minimum element is $0$ and maximum element is $9$. It can be proven that no subarray of the array has *interesting value* less than $0$. The maximum *interesting value* possible is $81$. A subarray with *interesting value* equal to $81$ is $[9]$. Here, both minimum and maximum elements of the subarray are $9$. It can be proven that no subarray of the array has *interesting value* more than $81$.
{"inputs": ["2\n2\n2 2\n3\n5 0 9\n"], "outputs": ["4 4\n0 81"]}
620
34
coding
Solve the programming task below in a Python markdown code block. M-kun is a competitor in AtCoder, whose highest rating is X. In this site, a competitor is given a kyu (class) according to his/her highest rating. For ratings from 400 through 1999, the following kyus are given: * From 400 through 599: 8-kyu * From 600 through 799: 7-kyu * From 800 through 999: 6-kyu * From 1000 through 1199: 5-kyu * From 1200 through 1399: 4-kyu * From 1400 through 1599: 3-kyu * From 1600 through 1799: 2-kyu * From 1800 through 1999: 1-kyu What kyu does M-kun have? Constraints * 400 \leq X \leq 1999 * X is an integer. Input Input is given from Standard Input in the following format: X Output Print the kyu M-kun has, as an integer. For example, if he has 8-kyu, print `8`. Examples Input 725 Output 7 Input 1600 Output 2
{"inputs": ["543", "609", "982", "554", "417", "979", "424", "470"], "outputs": ["8\n", "7\n", "6\n", "8\n", "8\n", "6\n", "8\n", "8\n"]}
328
78
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. Monocarp wants to watch $n$ videos. Each video is only one minute long, but its size may be arbitrary. The $i$-th video has the size $a_i$ megabytes. All videos are published on the Internet. A video should be downloaded before it can be watched. Monocarp has poor Internet connection — it takes exactly $1$ minute to download $1$ megabyte of data, so it will require $a_i$ minutes to download the $i$-th video. Monocarp's computer has a hard disk of $m$ megabytes. The disk is used to store the downloaded videos. Once Monocarp starts the download of a video of size $s$, the $s$ megabytes are immediately reserved on a hard disk. If there are less than $s$ megabytes left, the download cannot be started until the required space is freed. Each single video can be stored on the hard disk, since $a_i \le m$ for all $i$. Once the download is started, it cannot be interrupted. It is not allowed to run two or more downloads in parallel. Once a video is fully downloaded to the hard disk, Monocarp can watch it. Watching each video takes exactly $1$ minute and does not occupy the Internet connection, so Monocarp can start downloading another video while watching the current one. When Monocarp finishes watching a video, he doesn't need it on the hard disk anymore, so he can delete the video, instantly freeing the space it occupied on a hard disk. Deleting a video takes negligible time. Monocarp wants to watch all $n$ videos as quickly as possible. The order of watching does not matter, since Monocarp needs to watch all of them anyway. Please calculate the minimum possible time required for that. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$; $1 \le m \le 10^9$) — the number of videos Monocarp wants to watch and the size of the hard disk, respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le m$) — the sizes of the videos. -----Output----- Print one integer — the minimum time required to watch all $n$ videos. -----Examples----- Input 5 6 1 2 3 4 5 Output 16 Input 5 5 1 2 3 4 5 Output 17 Input 4 3 1 3 2 3 Output 12 -----Note----- None
{"inputs": ["1 1\n1\n", "1 294\n132\n", "2 15\n14 1\n", "2 15\n14 2\n", "4 3\n1 3 2 3\n", "5 6\n1 2 3 4 5\n", "5 5\n1 2 3 4 5\n", "4 100\n11 3 3 6\n"], "outputs": ["2\n", "133\n", "16\n", "18\n", "12\n", "16\n", "17\n", "24\n"]}
584
153
coding
Solve the programming task below in a Python markdown code block. You are given three sticks with positive integer lengths of a, b, and c centimeters. You can increase length of some of them by some positive integer number of centimeters (different sticks can be increased by a different length), but in total by at most l centimeters. In particular, it is allowed not to increase the length of any stick. Determine the number of ways to increase the lengths of some sticks so that you can form from them a non-degenerate (that is, having a positive area) triangle. Two ways are considered different, if the length of some stick is increased by different number of centimeters in them. -----Input----- The single line contains 4 integers a, b, c, l (1 ≤ a, b, c ≤ 3·10^5, 0 ≤ l ≤ 3·10^5). -----Output----- Print a single integer — the number of ways to increase the sizes of the sticks by the total of at most l centimeters, so that you can make a non-degenerate triangle from it. -----Examples----- Input 1 1 1 2 Output 4 Input 1 2 3 1 Output 2 Input 10 2 1 7 Output 0 -----Note----- In the first sample test you can either not increase any stick or increase any two sticks by 1 centimeter. In the second sample test you can increase either the first or the second stick by one centimeter. Note that the triangle made from the initial sticks is degenerate and thus, doesn't meet the conditions.
{"inputs": ["1 1 1 2\n", "1 2 3 1\n", "1 2 1 5\n", "2 7 8 4\n", "1 2 1 5\n", "2 7 8 4\n", "1 2 2 5\n", "2 7 8 0\n"], "outputs": ["4\n", "2\n", "20\n", "25\n", "20\n", "25\n", "23\n", "1\n"]}
344
123
coding
Solve the programming task below in a Python markdown code block. There's a chip in the point $(0, 0)$ of the coordinate plane. In one operation, you can move the chip from some point $(x_1, y_1)$ to some point $(x_2, y_2)$ if the Euclidean distance between these two points is an integer (i.e. $\sqrt{(x_1-x_2)^2+(y_1-y_2)^2}$ is integer). Your task is to determine the minimum number of operations required to move the chip from the point $(0, 0)$ to the point $(x, y)$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 3000$) — number of test cases. The single line of each test case contains two integers $x$ and $y$ ($0 \le x, y \le 50$) — the coordinates of the destination point. -----Output----- For each test case, print one integer — the minimum number of operations required to move the chip from the point $(0, 0)$ to the point $(x, y)$. -----Examples----- Input 3 8 6 0 0 9 15 Output 1 0 2 -----Note----- In the first example, one operation $(0, 0) \rightarrow (8, 6)$ is enough. $\sqrt{(0-8)^2+(0-6)^2}=\sqrt{64+36}=\sqrt{100}=10$ is an integer. In the second example, the chip is already at the destination point. In the third example, the chip can be moved as follows: $(0, 0) \rightarrow (5, 12) \rightarrow (9, 15)$. $\sqrt{(0-5)^2+(0-12)^2}=\sqrt{25+144}=\sqrt{169}=13$ and $\sqrt{(5-9)^2+(12-15)^2}=\sqrt{16+9}=\sqrt{25}=5$ are integers.
{"inputs": ["3\n8 6\n0 0\n9 15\n"], "outputs": ["1\n0\n2\n"]}
467
31
coding
Solve the programming task below in a Python markdown code block. We have a sandglass consisting of two bulbs, bulb A and bulb B. These bulbs contain some amount of sand. When we put the sandglass, either bulb A or B lies on top of the other and becomes the upper bulb. The other bulb becomes the lower bulb. The sand drops from the upper bulb to the lower bulb at a rate of 1 gram per second. When the upper bulb no longer contains any sand, nothing happens. Initially at time 0, bulb A is the upper bulb and contains a grams of sand; bulb B contains X-a grams of sand (for a total of X grams). We will turn over the sandglass at time r_1,r_2,..,r_K. Assume that this is an instantaneous action and takes no time. Here, time t refer to the time t seconds after time 0. You are given Q queries. Each query is in the form of (t_i,a_i). For each query, assume that a=a_i and find the amount of sand that would be contained in bulb A at time t_i. -----Constraints----- - 1≤X≤10^9 - 1≤K≤10^5 - 1≤r_1<r_2< .. <r_K≤10^9 - 1≤Q≤10^5 - 0≤t_1<t_2< .. <t_Q≤10^9 - 0≤a_i≤X (1≤i≤Q) - All input values are integers. -----Input----- The input is given from Standard Input in the following format: X K r_1 r_2 .. r_K Q t_1 a_1 t_2 a_2 : t_Q a_Q -----Output----- For each query, print the answer in its own line. -----Sample Input----- 180 3 60 120 180 3 30 90 61 1 180 180 -----Sample Output----- 60 1 120 In the first query, 30 out of the initial 90 grams of sand will drop from bulb A, resulting in 60 grams. In the second query, the initial 1 gram of sand will drop from bulb A, and nothing will happen for the next 59 seconds. Then, we will turn over the sandglass, and 1 second after this, bulb A contains 1 gram of sand at the time in question.
{"inputs": ["180\n3\n60 120 180\n3\n30 16\n61 1\n7 282", "180\n3\n85 120 180\n3\n2 9\n74 1\n238 180", "180\n3\n60 74 180\n3\n30 16\n61 1\n180 180", "180\n3\n60 74 173\n3\n30 16\n61 1\n180 316", "180\n3\n60 140 180\n3\n30 16\n61 1\n74 316", "180\n3\n30 32 180\n3\n30 16\n34 1\n180 316", "180\n3\n85 45 180\n3\n30 90\n45 1\n180 180", "180\n3\n60 74 180\n2\n30 16\n61 1\n180 441"], "outputs": ["0\n1\n67\n", "7\n0\n128\n", "0\n1\n28\n", "0\n1\n42\n", "0\n1\n134\n", "0\n0\n4\n", "60\n0\n0\n", "0\n1\n"]}
545
378
coding
Solve the programming task below in a Python markdown code block. A car number in Berland consists of exactly n digits. A number is called beautiful if it has at least k equal digits. Vasya wants to change the digits in his car's number so that the number became beautiful. To replace one of n digits Vasya has to pay the sum of money, equal to the absolute difference between the old digit and the new one. Help Vasya: find the minimum sum of money he should pay to make the number of his car beautiful. You should also find the resulting beautiful number. If there are several such numbers, then print the lexicographically minimum one. Input The first line contains two space-separated integers n and k (2 ≤ n ≤ 104, 2 ≤ k ≤ n) which represent how many digits the number has and how many equal digits a beautiful number should have. The second line consists of n digits. It describes the old number of Vasya's car. It is guaranteed that the number contains no spaces and only contains digits. Output On the first line print the minimum sum of money Vasya needs to change the number. On the second line print the car's new number. If there are several solutions, print the lexicographically minimum one. Examples Input 6 5 898196 Output 4 888188 Input 3 2 533 Output 0 533 Input 10 6 0001112223 Output 3 0000002223 Note In the first sample replacing the second digit with an "8" costs |9 - 8| = 1. Replacing the fifth digit with an "8" costs the same. Replacing the sixth digit costs |6 - 8| = 2. As a result, Vasya will pay 1 + 1 + 2 = 4 for a beautiful number "888188". The lexicographical comparison of strings is performed by the < operator in modern programming languages. The string x is lexicographically smaller than the string y, if there exists such i (1 ≤ i ≤ n), that xi < yi, and for any j (1 ≤ j < i) xj = yj. The strings compared in this problem will always have the length n.
{"inputs": ["2 2\n11\n", "2 2\n80\n", "2 2\n09\n", "2 2\n10\n", "2 2\n85\n", "2 2\n15\n", "2 2\n24\n", "2 1\n24\n"], "outputs": ["0\n11\n", "8\n00\n", "9\n00\n", "1\n00\n", "3\n55\n", "4\n11\n", "2\n22\n", "0\n24\n"]}
510
134
coding
Solve the programming task below in a Python markdown code block. Shaun is very much interested in Subarrays. Shaun wants to count the number of subarrays in his chosen array with sum being a multiple of $10^9$. Since, Shaun is interested in huge numbers.He chose his array such that it contains only $10^8$ and $9*10^8$ as its elements. Help shaun to count the number of required subarrays. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - First line of each testcase contains one integer $N$,size of array $A$. - Second line of each testcase contains $N$ space separated array elements -----Output:----- For each testcase, output in a single line number of subarrays with sum being multiple of $10^9$. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^5$ - $A[i]$=$10^8$ , $9*10^8$ -----Sample Input:----- 2 3 100000000 900000000 100000000 1 900000000 -----Sample Output:----- 2 0
{"inputs": ["2\n3\n100000000 900000000 100000000\n1\n900000000"], "outputs": ["2\n0"]}
295
58
coding
Solve the programming task below in a Python markdown code block. Snuke found a record of a tree with N vertices in ancient ruins. The findings are as follows: * The vertices of the tree were numbered 1,2,...,N, and the edges were numbered 1,2,...,N-1. * Edge i connected Vertex a_i and b_i. * The length of each edge was an integer between 1 and 10^{18} (inclusive). * The sum of the shortest distances from Vertex i to Vertex 1,...,N was s_i. From the information above, restore the length of each edge. The input guarantees that it is possible to determine the lengths of the edges consistently with the record. Furthermore, it can be proved that the length of each edge is uniquely determined in such a case. Constraints * 2 \leq N \leq 10^{5} * 1 \leq a_i,b_i \leq N * 1 \leq s_i \leq 10^{18} * The given graph is a tree. * All input values are integers. * It is possible to consistently restore the lengths of the edges. * In the restored graph, the length of each edge is an integer between 1 and 10^{18} (inclusive). Input Input is given from Standard Input in the following format: N a_1 b_1 : a_{N-1} b_{N-1} s_1 s_2 ... s_{N} Output Print N-1 lines. The i-th line must contain the length of Edge i. Examples Input 4 1 2 2 3 3 4 8 6 6 8 Output 1 2 1 Input 5 1 2 1 3 1 4 1 5 10 13 16 19 22 Output 1 2 3 4 Input 15 9 10 9 15 15 4 4 13 13 2 13 11 2 14 13 6 11 1 1 12 12 3 12 7 2 5 14 8 1154 890 2240 883 2047 2076 1590 1104 1726 1791 1091 1226 841 1000 901 Output 5 75 2 6 7 50 10 95 9 8 78 28 89 8
{"inputs": ["4\n1 2\n2 3\n3 4\n8 6 6 8", "5\n1 2\n1 3\n1 2\n1 5\n9 21 16 4 22", "5\n1 2\n1 2\n1 2\n1 5\n2 13 16 7 28", "5\n1 2\n1 2\n1 2\n1 4\n2 13 16 7 28", "5\n1 2\n1 3\n1 3\n1 5\n9 21 16 5 22", "5\n1 2\n1 3\n1 2\n1 4\n8 21 29 19 6", "5\n1 2\n1 2\n1 4\n1 5\n2 13 16 19 5", "5\n1 2\n1 3\n1 4\n1 5\n5 13 16 19 22"], "outputs": ["1\n2\n1", "4\n2\n4\n4\n", "3\n3\n3\n8\n", "3\n3\n3\n1\n", "4\n2\n2\n4\n", "4\n7\n4\n3\n", "3\n3\n5\n1\n", "2\n3\n4\n5\n"]}
606
331
coding
Solve the programming task below in a Python markdown code block. There were n groups of students which came to write a training contest. A group is either one person who can write the contest with anyone else, or two people who want to write the contest in the same team. The coach decided to form teams of exactly three people for this training. Determine the maximum number of teams of three people he can form. It is possible that he can't use all groups to form teams. For groups of two, either both students should write the contest, or both should not. If two students from a group of two will write the contest, they should be in the same team. -----Input----- The first line contains single integer n (2 ≤ n ≤ 2·10^5) — the number of groups. The second line contains a sequence of integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 2), where a_{i} is the number of people in group i. -----Output----- Print the maximum number of teams of three people the coach can form. -----Examples----- Input 4 1 1 2 1 Output 1 Input 2 2 2 Output 0 Input 7 2 2 2 1 1 1 1 Output 3 Input 3 1 1 1 Output 1 -----Note----- In the first example the coach can form one team. For example, he can take students from the first, second and fourth groups. In the second example he can't make a single team. In the third example the coach can form three teams. For example, he can do this in the following way: The first group (of two people) and the seventh group (of one person), The second group (of two people) and the sixth group (of one person), The third group (of two people) and the fourth group (of one person).
{"inputs": ["2\n2 2\n", "2\n1 1\n", "2\n2 1\n", "2\n1 2\n", "2\n2 1\n", "2\n1 1\n", "2\n1 2\n", "2\n2 2\n"], "outputs": ["0\n", "0\n", "1\n", "1\n", "1", "0", "1", "0"]}
414
98
coding
Solve the programming task below in a Python markdown code block. You are given several queries. In the i-th query you are given a single positive integer ni. You are to represent ni as a sum of maximum possible number of composite summands and print this maximum number, or print -1, if there are no such splittings. An integer greater than 1 is composite, if it is not prime, i.e. if it has positive divisors not equal to 1 and the integer itself. Input The first line contains single integer q (1 ≤ q ≤ 105) — the number of queries. q lines follow. The (i + 1)-th line contains single integer ni (1 ≤ ni ≤ 109) — the i-th query. Output For each query print the maximum possible number of summands in a valid splitting to composite summands, or -1, if there are no such splittings. Examples Input 1 12 Output 3 Input 2 6 8 Output 1 2 Input 3 1 2 3 Output -1 -1 -1 Note 12 = 4 + 4 + 4 = 4 + 8 = 6 + 6 = 12, but the first splitting has the maximum possible number of summands. 8 = 4 + 4, 6 can't be split into several composite summands. 1, 2, 3 are less than any composite number, so they do not have valid splittings.
{"inputs": ["1\n8\n", "1\n15\n", "1\n19\n", "1\n12\n", "2\n3\n7\n", "2\n3\n4\n", "2\n3\n1\n", "2\n6\n8\n"], "outputs": ["2\n", "2\n", "3\n", "3\n", "-1\n-1\n", "-1\n1\n", "-1\n-1\n", "1\n2\n"]}
333
107
coding
Solve the programming task below in a Python markdown code block. Limak is a grizzly bear who desires power and adoration. He wants to win in upcoming elections and rule over the Bearland. There are n candidates, including Limak. We know how many citizens are going to vote for each candidate. Now i-th candidate would get a_{i} votes. Limak is candidate number 1. To win in elections, he must get strictly more votes than any other candidate. Victory is more important than everything else so Limak decided to cheat. He will steal votes from his opponents by bribing some citizens. To bribe a citizen, Limak must give him or her one candy - citizens are bears and bears like candies. Limak doesn't have many candies and wonders - how many citizens does he have to bribe? -----Input----- The first line contains single integer n (2 ≤ n ≤ 100) - number of candidates. The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 1000) - number of votes for each candidate. Limak is candidate number 1. Note that after bribing number of votes for some candidate might be zero or might be greater than 1000. -----Output----- Print the minimum number of citizens Limak must bribe to have strictly more votes than any other candidate. -----Examples----- Input 5 5 1 11 2 8 Output 4 Input 4 1 8 8 8 Output 6 Input 2 7 6 Output 0 -----Note----- In the first sample Limak has 5 votes. One of the ways to achieve victory is to bribe 4 citizens who want to vote for the third candidate. Then numbers of votes would be 9, 1, 7, 2, 8 (Limak would have 9 votes). Alternatively, Limak could steal only 3 votes from the third candidate and 1 vote from the second candidate to get situation 9, 0, 8, 2, 8. In the second sample Limak will steal 2 votes from each candidate. Situation will be 7, 6, 6, 6. In the third sample Limak is a winner without bribing any citizen.
{"inputs": ["2\n7 6\n", "2\n1 1\n", "2\n1 1\n", "2\n1 2\n", "2\n3 6\n", "2\n7 6\n", "2\n3 11\n", "2\n96 97\n"], "outputs": ["0\n", "1\n", "1", "1\n", "2\n", "0", "5\n", "1\n"]}
497
103
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Hindi],[Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. One day, Saeed was teaching a string compression algorithm. This algorithm finds all maximal substrings which contains only one character repeated one or more times (a substring is maximal if it we cannot add one character to its left or right without breaking this property) and replaces each such substring by the string "cK", where $K$ is the length of the substring and $c$ is the only character it contains. For example, "aabaaa" is compressed to "a2b1a3". Saeed wanted to check if the students understood the algorithm, so he wrote a string $S$ on the board and asked the students if the algorithm is effective on $S$, i.e. if the string created by compressing $S$ is strictly shorter than $S$. Help them answer this question. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing the string "YES" if the algorithm is effective on $S$ or "NO" if it is not. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ |S| ≤ 10^{3}$ $S$ may consist of only lowercase English letters. ------ Subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 3 bbbbbbbbbbaa c aaaaaaaaaabcdefgh ----- Sample Output 1 ------ YES NO NO ----- explanation 1 ------ Example case 1: - The compressed string of "bbbbbbbbbbaa" is "b10a2", which is shorter. - The compressed string of "c" is "c1", which is not shorter than "c". - The compressed string of "aaaaaaaaaabcdefgh" is "a10b1c1d1e1f1g1h1", which is not shorter than "aaaaaaaaaabcdefgh" (both strings have length $17$).
{"inputs": ["3\nbbbbbbbbbbaa\nc\naaaaaaaaaabcdefgh"], "outputs": ["YES\nNO\nNO"]}
498
28
coding
Solve the programming task below in a Python markdown code block. Chef has an array A of length N. In one operation, Chef can choose any two distinct indices i, j (1 ≤ i, j ≤ N, i \neq j) and either change A_{i} to A_{j} or change A_{j} to A_{i}. Find the minimum number of operations required to make all the elements of the array equal. ------ Input Format ------ - First line will contain T, number of test cases. Then the test cases follow. - First line of each test case consists of an integer N - denoting the size of array A. - Second line of each test case consists of N space-separated integers A_{1}, A_{2}, \dots, A_{N} - denoting the array A. ------ Output Format ------ For each test case, output the minimum number of operations required to make all the elements equal. ------ Constraints ------ $1 ≤ T ≤ 100$ $2 ≤ N ≤ 1000$ $1 ≤ A_{i} ≤ 1000$ ----- Sample Input 1 ------ 4 3 1 2 3 4 5 5 5 5 4 2 2 1 1 3 1 1 2 ----- Sample Output 1 ------ 2 0 2 1 ----- explanation 1 ------ Test Case $1$: You can make all the elements equal in $2$ operations. In the first operation, you can choose indices $1, 2$ and convert $A_{1}$ to $A_{2}$. So the array becomes $[2, 2, 3]$. Now you can choose indices $1, 3$ and convert $A_{3}$ to $A_{1}$, so the final array becomes $[2, 2, 2]$. Test Case $2$: Since all the elements are already equal there is no need to perform any operation. Test Case $3$: You can make all the elements equal in $2$ operations. In the first operation, you can choose indices $1, 3$ and convert $A_{1}$ to $A_{3}$. So the array becomes $[1, 2, 1, 1]$. Now you can choose indices $1, 2$ and convert $A_{2}$ to $A_{1}$, so the final array becomes $[1, 1, 1, 1]$. Test Case $4$: You can make all the elements equal in $1$ operation. You can pick indices $2, 3$ and convert $A_{3}$ to $A_{2}$ after which the array becomes $[1, 1, 1]$.
{"inputs": ["4\n3\n1 2 3\n4\n5 5 5 5\n4\n2 2 1 1\n3\n1 1 2\n"], "outputs": ["2\n0\n2\n1\n"]}
590
56
coding
Solve the programming task below in a Python markdown code block. There are N piles of stones. The i-th pile has A_i stones. Aoki and Takahashi are about to use them to play the following game: - Starting with Aoki, the two players alternately do the following operation: - Operation: Choose one pile of stones, and remove one or more stones from it. - When a player is unable to do the operation, he loses, and the other player wins. When the two players play optimally, there are two possibilities in this game: the player who moves first always wins, or the player who moves second always wins, only depending on the initial number of stones in each pile. In such a situation, Takahashi, the second player to act, is trying to guarantee his win by moving at least zero and at most (A_1 - 1) stones from the 1-st pile to the 2-nd pile before the game begins. If this is possible, print the minimum number of stones to move to guarantee his victory; otherwise, print -1 instead. -----Constraints----- - 2 \leq N \leq 300 - 1 \leq A_i \leq 10^{12} -----Input----- Input is given from Standard Input in the following format: N A_1 \ldots A_N -----Output----- Print the minimum number of stones to move to guarantee Takahashi's win; otherwise, print -1 instead. -----Sample Input----- 2 5 3 -----Sample Output----- 1 Without moving stones, if Aoki first removes 2 stones from the 1-st pile, Takahashi cannot win in any way. If Takahashi moves 1 stone from the 1-st pile to the 2-nd before the game begins so that both piles have 4 stones, Takahashi can always win by properly choosing his actions.
{"inputs": ["2\n3 6", "2\n5 5", "2\n7 5", "2\n5 1", "2\n7 7", "2\n3 1", "2\n7 1", "2\n3 2"], "outputs": ["-1\n", "0\n", "1\n", "2\n", "0\n", "1\n", "3\n", "-1\n"]}
402
95
coding
Solve the programming task below in a Python markdown code block. You probably know the "like" system from Facebook and other pages. People can "like" blog posts, pictures or other items. We want to create the text that should be displayed next to such an item. Implement a function `likes :: [String] -> String`, which must take in input array, containing the names of people who like an item. It must return the display text as shown in the examples: ```python likes([]) # must be "no one likes this" likes(["Peter"]) # must be "Peter likes this" likes(["Jacob", "Alex"]) # must be "Jacob and Alex like this" likes(["Max", "John", "Mark"]) # must be "Max, John and Mark like this" likes(["Alex", "Jacob", "Mark", "Max"]) # must be "Alex, Jacob and 2 others like this" ``` For 4 or more names, the number in `and 2 others` simply increases. Also feel free to reuse/extend the following starter code: ```python def likes(names): ```
{"functional": "_inputs = [[[]], [['Peter']], [['Jacob', 'Alex']], [['Max', 'John', 'Mark']], [['Alex', 'Jacob', 'Mark', 'Max']]]\n_outputs = [['no one likes this'], ['Peter likes this'], ['Jacob and Alex like this'], ['Max, John and Mark like this'], ['Alex, Jacob and 2 others like this']]\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(likes(*i), o[0])"}
231
220
coding
Solve the programming task below in a Python markdown code block. Vasya got really tired of these credits (from problem F) and now wants to earn the money himself! He decided to make a contest to gain a profit. Vasya has $n$ problems to choose from. They are numbered from $1$ to $n$. The difficulty of the $i$-th problem is $d_i$. Moreover, the problems are given in the increasing order by their difficulties. The difficulties of all tasks are pairwise distinct. In order to add the $i$-th problem to the contest you need to pay $c_i$ burles to its author. For each problem in the contest Vasya gets $a$ burles. In order to create a contest he needs to choose a consecutive subsegment of tasks. So the total earnings for the contest are calculated as follows: if Vasya takes problem $i$ to the contest, he needs to pay $c_i$ to its author; for each problem in the contest Vasya gets $a$ burles; let $gap(l, r) = \max\limits_{l \le i < r} (d_{i + 1} - d_i)^2$. If Vasya takes all the tasks with indices from $l$ to $r$ to the contest, he also needs to pay $gap(l, r)$. If $l = r$ then $gap(l, r) = 0$. Calculate the maximum profit that Vasya can earn by taking a consecutive segment of tasks. -----Input----- The first line contains two integers $n$ and $a$ ($1 \le n \le 3 \cdot 10^5$, $1 \le a \le 10^9$) — the number of proposed tasks and the profit for a single problem, respectively. Each of the next $n$ lines contains two integers $d_i$ and $c_i$ ($1 \le d_i, c_i \le 10^9, d_i < d_{i+1}$). -----Output----- Print one integer — maximum amount of burles Vasya can earn. -----Examples----- Input 5 10 1 15 5 3 6 11 7 2 11 22 Output 13 Input 3 5 1 8 2 19 3 11 Output 0
{"inputs": ["1 10\n1 13\n", "1 10\n1 13\n", "1 16\n1 13\n", "1 235\n15 210\n", "1 235\n15 210\n", "1 235\n23 210\n", "1 235\n23 340\n", "3 5\n1 8\n2 19\n3 11\n"], "outputs": ["0\n", " 0\n", "3\n", "25\n", " 25\n", "25\n", "0\n", "0\n"]}
519
161
coding
Solve the programming task below in a Python markdown code block. You are given a string q. A sequence of k strings s_1, s_2, ..., s_{k} is called beautiful, if the concatenation of these strings is string q (formally, s_1 + s_2 + ... + s_{k} = q) and the first characters of these strings are distinct. Find any beautiful sequence of strings or determine that the beautiful sequence doesn't exist. -----Input----- The first line contains a positive integer k (1 ≤ k ≤ 26) — the number of strings that should be in a beautiful sequence. The second line contains string q, consisting of lowercase Latin letters. The length of the string is within range from 1 to 100, inclusive. -----Output----- If such sequence doesn't exist, then print in a single line "NO" (without the quotes). Otherwise, print in the first line "YES" (without the quotes) and in the next k lines print the beautiful sequence of strings s_1, s_2, ..., s_{k}. If there are multiple possible answers, print any of them. -----Examples----- Input 1 abca Output YES abca Input 2 aaacas Output YES aaa cas Input 4 abc Output NO -----Note----- In the second sample there are two possible answers: {"aaaca", "s"} and {"aaa", "cas"}.
{"inputs": ["1\nk\n", "1\nk\n", "1\nl\n", "1\nm\n", "1\nj\n", "1\nzz\n", "26\na\n", "2\nab\n"], "outputs": ["YES\nk\n", "YES\nk\n", "YES\nl\n", "YES\nm\n", "YES\nj\n", "YES\nzz\n", "NO\n", "YES\na\nb\n"]}
312
103
coding
Solve the programming task below in a Python markdown code block. Cat Snuke is learning to write characters. Today, he practiced writing digits 1 and 9, but he did it the other way around. You are given a three-digit integer n written by Snuke. Print the integer obtained by replacing each digit 1 with 9 and each digit 9 with 1 in n. -----Constraints----- - 111 \leq n \leq 999 - n is an integer consisting of digits 1 and 9. -----Input----- Input is given from Standard Input in the following format: n -----Output----- Print the integer obtained by replacing each occurrence of 1 with 9 and each occurrence of 9 with 1 in n. -----Sample Input----- 119 -----Sample Output----- 991 Replace the 9 in the ones place with 1, the 1 in the tens place with 9 and the 1 in the hundreds place with 9. The answer is 991.
{"inputs": ["191", "111", "199", "111", "199", "191", "119", "999"], "outputs": ["919\n", "999\n", "911\n", "999\n", "911\n", "919\n", "991", "111"]}
219
92
coding
Solve the programming task below in a Python markdown code block. Jabber ID on the national Berland service «Babber» has a form <username>@<hostname>[/resource], where * <username> — is a sequence of Latin letters (lowercase or uppercase), digits or underscores characters «_», the length of <username> is between 1 and 16, inclusive. * <hostname> — is a sequence of word separated by periods (characters «.»), where each word should contain only characters allowed for <username>, the length of each word is between 1 and 16, inclusive. The length of <hostname> is between 1 and 32, inclusive. * <resource> — is a sequence of Latin letters (lowercase or uppercase), digits or underscores characters «_», the length of <resource> is between 1 and 16, inclusive. The content of square brackets is optional — it can be present or can be absent. There are the samples of correct Jabber IDs: mike@codeforces.com, 007@en.codeforces.com/contest. Your task is to write program which checks if given string is a correct Jabber ID. Input The input contains of a single line. The line has the length between 1 and 100 characters, inclusive. Each characters has ASCII-code between 33 and 127, inclusive. Output Print YES or NO. Examples Input mike@codeforces.com Output YES Input john.smith@codeforces.ru/contest.icpc/12 Output NO
{"inputs": ["@\n", "/\n", ".\n", "@ops\n", "$@ru\n", "o@ps\n", "%@ru\n", "@mike\n"], "outputs": ["NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "YES\n", "NO\n", "NO\n"]}
343
75
coding
Solve the programming task below in a Python markdown code block. Zonal Computing Olympiad 2015, 29 Nov 2014 An interval is a pair of positive integers [a, b] with a ≤ b. It is meant to denote the set of integers that lie between the values a and b. For example [3,5] denotes the set {3,4,5} while the interval [3, 3] denotes the set {3}. We say that an interval [a, b] is covered by an integer i, if i belongs to the set defined by [a, b]. For example interval [3, 5] is covered by 3 and so is the interval [3, 3]. Given a set of intervals I, and a set of integers S we say that I is covered by S if for each interval [a, b] in I there is an integer i in S such that [a, b] is covered by i. For example, the set {[3, 5], [3, 3]} is covered by the set {3}. The set of intervals {[6, 9], [3, 5], [4, 8]} is covered by the set {4, 5, 8}. It is also covered by the set {4, 7}. We would like to compute, for any set of intervals I, the size of the smallest set S that covers it. You can check that for the set of intervals {[6, 9], [3, 5], [4, 8]} the answer is 2 while for the set of intervals {[3, 5], [3, 3]} the answer is 1. Your program should take the set of intervals as input and output the size of the smallest set that covers it as the answer. -----Input format----- The first line contains a single integer N, giving the number of intervals in the input. This is followed by N lines, each containing two integers separated by a space describing an interval, with the first integer guaranteed to be less than or equal to the second integer. -----Output format----- Output a single integer giving the size of the smallest set of integers that covers the given set of intervals. -----Test data----- You may assume that all integers in the input are in the range 1 to 10^8 inclusive. Subtask 1 (100 marks) : 1 ≤ N ≤ 5000. -----Sample Input 1----- 2 3 5 3 3 -----Sample Output 1----- 1 -----Sample Input 2----- 3 6 9 3 5 4 8 -----Sample Output 2----- 2
{"inputs": ["2\n3 5\n3 3", "3\n6 9\n3 5\n4 8"], "outputs": ["1", "2"]}
571
38
coding
Solve the programming task below in a Python markdown code block. Your friend gave you a dequeue D as a birthday present. D is a horizontal cylinder that contains a row of N jewels. The values of the jewels are V_1, V_2, ..., V_N from left to right. There may be jewels with negative values. In the beginning, you have no jewel in your hands. You can perform at most K operations on D, chosen from the following, at most K times (possibly zero): - Operation A: Take out the leftmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty. - Operation B: Take out the rightmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty. - Operation C: Choose a jewel in your hands and insert it to the left end of D. You cannot do this operation when you have no jewel in your hand. - Operation D: Choose a jewel in your hands and insert it to the right end of D. You cannot do this operation when you have no jewel in your hand. Find the maximum possible sum of the values of jewels in your hands after the operations. -----Constraints----- - All values in input are integers. - 1 \leq N \leq 50 - 1 \leq K \leq 100 - -10^7 \leq V_i \leq 10^7 -----Input----- Input is given from Standard Input in the following format: N K V_1 V_2 ... V_N -----Output----- Print the maximum possible sum of the values of jewels in your hands after the operations. -----Sample Input----- 6 4 -10 8 2 1 2 6 -----Sample Output----- 14 After the following sequence of operations, you have two jewels of values 8 and 6 in your hands for a total of 14, which is the maximum result. - Do operation A. You take out the jewel of value -10 from the left end of D. - Do operation B. You take out the jewel of value 6 from the right end of D. - Do operation A. You take out the jewel of value 8 from the left end of D. - Do operation D. You insert the jewel of value -10 to the right end of D.
{"inputs": ["1 1\n1\n", "2 40\n770493 0\n", "1 100\n4847850\n", "6 2\n0 9 0 -1 3 2", "6 4\n-10 8 2 0 2 6", "6 4\n-10 3 2 0 2 6", "6 4\n-10 3 4 0 2 6", "6 2\n-10 3 6 0 2 6"], "outputs": ["1\n", "770493\n", "4847850\n", "9\n", "14\n", "10\n", "12\n", "8\n"]}
504
185
coding
Solve the programming task below in a Python markdown code block. One day n friends met at a party, they hadn't seen each other for a long time and so they decided to make a group photo together. Simply speaking, the process of taking photos can be described as follows. On the photo, each photographed friend occupies a rectangle of pixels: the i-th of them occupies the rectangle of width w_{i} pixels and height h_{i} pixels. On the group photo everybody stands in a line, thus the minimum pixel size of the photo including all the photographed friends, is W × H, where W is the total sum of all widths and H is the maximum height of all the photographed friends. As is usually the case, the friends made n photos — the j-th (1 ≤ j ≤ n) photo had everybody except for the j-th friend as he was the photographer. Print the minimum size of each made photo in pixels. -----Input----- The first line contains integer n (2 ≤ n ≤ 200 000) — the number of friends. Then n lines follow: the i-th line contains information about the i-th friend. The line contains a pair of integers w_{i}, h_{i} (1 ≤ w_{i} ≤ 10, 1 ≤ h_{i} ≤ 1000) — the width and height in pixels of the corresponding rectangle. -----Output----- Print n space-separated numbers b_1, b_2, ..., b_{n}, where b_{i} — the total number of pixels on the minimum photo containing all friends expect for the i-th one. -----Examples----- Input 3 1 10 5 5 10 1 Output 75 110 60 Input 3 2 1 1 2 2 1 Output 6 4 6
{"inputs": ["2\n1 5\n2 3\n", "2\n2 3\n1 1\n", "2\n1 1\n1 2\n", "2\n1 1\n1 2\n", "2\n2 3\n1 1\n", "2\n1 5\n2 3\n", "2\n2 6\n1 1\n", "2\n2 5\n2 3\n"], "outputs": ["6 5 ", "1 6 ", "2 1 ", "2 1 ", "1 6 ", "6 5 ", "1 12\n", "6 10\n"]}
395
146
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. In an infinite chess board with coordinates from -infinity to +infinity, you have a knight at square [0, 0]. A knight has 8 possible moves it can make, as illustrated below. Each move is two squares in a cardinal direction, then one square in an orthogonal direction. Return the minimum number of steps needed to move the knight to the square [x, y]. It is guaranteed the answer exists.   Please complete the following python code precisely: ```python class Solution: def minKnightMoves(self, x: int, y: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(x = 2, y = 1) == 1\n assert candidate(x = 5, y = 5) == 4\n\n\ncheck(Solution().minKnightMoves)"}
137
54
coding
Solve the programming task below in a Python markdown code block. Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. [Flappy Bird] is on a screen with height $H$ pixels (a 2D plane where the $y$-axis corresponds to the vertical direction). There are $N$ vertical obstacles in the room (numbered $1$ through $N$); for each valid $i$, the coordinates of the *endpoint* of the $i$-th obstacle are $(x_{i}, a_{i})$. There are two types of obstacles: Type 0: A line segment between the points $(x_{i}, 0)$ and $(x_{i}, a_{i})$, i.e. upwards from the floor. Type 1: A line segment between the points $(x_{i}, H)$ and $(x_{i}, a_{i})$, i.e. downwards from the ceiling. For each obstacle, you need to find the number of endpoints of other obstacles (not including itself) that are visible from its endpoint. Two endpoints are visible from each other if the line segment joining them does not intersect or touch any other obstacle. Note that each obstacle has exactly one endpoint. ------ 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 $H$ and $N$. The following $N$ lines describe the obstacles. For each valid $i$, the $i$-th of these lines contains three space-separated integers $t_{i}$, $x_{i}$ and $a_{i}$, where $t_{i}$ is the type of the $i$-th obstacle. ------ Output ------ For each test case, print a single line containing $N$ space-separated integers. For each valid $i$, the $i$-th of these integers should be the number of endpoints visible from the $i$-th endpoint. ------ Constraints ------ $1 ≤ T ≤ 200$ $1 ≤ H ≤ 10^{9}$ $1 ≤ N ≤ 2,000$ $t_{i} \in \{0, 1\}$ for each valid $i$ $1 ≤ x_{i} ≤ 10^{9}$ for each valid $i$ $1 ≤ a_{i} ≤ H-1$ for each valid $i$ no two obstacles touch or intersect the sum of $N$ over all test cases does not exceed $20,000$ ----- Sample Input 1 ------ 1 10 4 0 2 5 1 5 6 1 8 8 0 11 2 ----- Sample Output 1 ------ 2 3 2 3 ----- explanation 1 ------ Example case 1:
{"inputs": ["1\n10 4\n0 2 5\n1 5 6\n1 8 8\n0 11 2"], "outputs": ["2 3 2 3"]}
635
48
coding
Solve the programming task below in a Python markdown code block. There is a square field of size $n \times n$ in which two cells are marked. These cells can be in the same row or column. You are to mark two more cells so that they are the corners of a rectangle with sides parallel to the coordinate axes. For example, if $n=4$ and a rectangular field looks like this (there are asterisks in the marked cells): $$ \begin{matrix} . & . & * & . \\ . & . & . & . \\ * & . & . & . \\ . & . & . & . \\ \end{matrix} $$ Then you can mark two more cells as follows $$ \begin{matrix} * & . & * & . \\ . & . & . & . \\ * & . & * & . \\ . & . & . & . \\ \end{matrix} $$ If there are several possible solutions, then print any of them. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 400$). Then $t$ test cases follow. The first row of each test case contains a single integer $n$ ($2 \le n \le 400$) — the number of rows and columns in the table. The following $n$ lines each contain $n$ characters '.' or '*' denoting empty and marked cells, respectively. It is guaranteed that the sums of $n$ for all test cases do not exceed $400$. It is guaranteed that there are exactly two asterisks on the field. They can be in the same row/column. It is guaranteed that the solution exists. -----Output----- For each test case, output $n$ rows of $n$ characters — a field with four asterisks marked corresponding to the statements. If there multiple correct answers, print any of them. -----Examples----- Input 6 4 ..*. .... *... .... 2 *. .* 2 .* .* 3 *.* ... ... 5 ..... ..*.. ..... .*... ..... 4 .... .... *... *... Output *.*. .... *.*. .... ** ** ** ** *.* *.* ... ..... .**.. ..... .**.. ..... .... .... **.. **.. -----Note----- None
{"inputs": ["6\n4\n..*.\n....\n*...\n....\n2\n*.\n.*\n2\n.*\n.*\n3\n*.*\n...\n...\n5\n.....\n..*..\n.....\n.*...\n.....\n4\n....\n....\n*...\n*...\n", "6\n4\n..*.\n....\n*...\n....\n2\n*.\n.*\n2\n*.\n.*\n3\n*.*\n...\n...\n5\n.....\n..*..\n.....\n.*...\n.....\n4\n....\n....\n*...\n*...\n", "6\n4\n..*.\n....\n...*\n....\n2\n*.\n.*\n2\n*.\n.*\n3\n*.*\n...\n...\n5\n.....\n..*..\n.....\n.*...\n.....\n4\n....\n....\n*...\n*...\n", "6\n4\n..*.\n....\n...*\n....\n2\n*.\n.*\n2\n*.\n.*\n3\n*.*\n...\n...\n5\n.....\n..*..\n.....\n.*...\n.....\n4\n....\n....\n...*\n*...\n", "6\n4\n..*.\n....\n.*..\n....\n2\n*.\n.*\n2\n*.\n.*\n3\n*.*\n...\n...\n5\n.....\n..*..\n.....\n.*...\n.....\n4\n....\n....\n*...\n*...\n", "6\n4\n..*.\n....\n.*..\n....\n2\n*.\n.*\n2\n*.\n.*\n3\n*.*\n...\n...\n5\n.....\n..*..\n.....\n.*...\n.....\n4\n....\n....\n*...\n...*\n", "6\n4\n..*.\n....\n*...\n....\n2\n*.\n.*\n2\n*.\n.*\n3\n*.*\n...\n...\n5\n.....\n..*..\n.....\n.*...\n.....\n4\n....\n....\n*...\n...*\n", "6\n4\n.*..\n....\n...*\n....\n2\n*.\n.*\n2\n*.\n.*\n3\n*.*\n...\n...\n5\n.....\n..*..\n.....\n.*...\n.....\n4\n....\n....\n...*\n*...\n"], "outputs": ["*.*.\n....\n*.*.\n....\n**\n**\n**\n**\n*.*\n*.*\n...\n.....\n.**..\n.....\n.**..\n.....\n....\n....\n**..\n**..\n", "*.*.\n....\n*.*.\n....\n**\n**\n**\n**\n*.*\n*.*\n...\n.....\n.**..\n.....\n.**..\n.....\n....\n....\n**..\n**..\n", "..**\n....\n..**\n....\n**\n**\n**\n**\n*.*\n*.*\n...\n.....\n.**..\n.....\n.**..\n.....\n....\n....\n**..\n**..\n", "..**\n....\n..**\n....\n**\n**\n**\n**\n*.*\n*.*\n...\n.....\n.**..\n.....\n.**..\n.....\n....\n....\n*..*\n*..*\n", ".**.\n....\n.**.\n....\n**\n**\n**\n**\n*.*\n*.*\n...\n.....\n.**..\n.....\n.**..\n.....\n....\n....\n**..\n**..\n", ".**.\n....\n.**.\n....\n**\n**\n**\n**\n*.*\n*.*\n...\n.....\n.**..\n.....\n.**..\n.....\n....\n....\n*..*\n*..*\n", "*.*.\n....\n*.*.\n....\n**\n**\n**\n**\n*.*\n*.*\n...\n.....\n.**..\n.....\n.**..\n.....\n....\n....\n*..*\n*..*\n", ".*.*\n....\n.*.*\n....\n**\n**\n**\n**\n*.*\n*.*\n...\n.....\n.**..\n.....\n.**..\n.....\n....\n....\n*..*\n*..*\n"]}
493
1,159
coding
Solve the programming task below in a Python markdown code block. ### Tongues Gandalf's writings have long been available for study, but no one has yet figured out what language they are written in. Recently, due to programming work by a hacker known only by the code name ROT13, it has been discovered that Gandalf used nothing but a simple letter substitution scheme, and further, that it is its own inverse|the same operation scrambles the message as unscrambles it. This operation is performed by replacing vowels in the sequence `'a' 'i' 'y' 'e' 'o' 'u'` with the vowel three advanced, cyclicly, while preserving case (i.e., lower or upper). Similarly, consonants are replaced from the sequence `'b' 'k' 'x' 'z' 'n' 'h' 'd' 'c' 'w' 'g' 'p' 'v' 'j' 'q' 't' 's' 'r' 'l' 'm' 'f'` by advancing ten letters. So for instance the phrase `'One ring to rule them all.'` translates to `'Ita dotf ni dyca nsaw ecc.'` The fascinating thing about this transformation is that the resulting language yields pronounceable words. For this problem, you will write code to translate Gandalf's manuscripts into plain text. Your job is to write a function that decodes Gandalf's writings. ### Input The function will be passed a string for the function to decode. Each string will contain up to 100 characters, representing some text written by Gandalf. All characters will be plain ASCII, in the range space (32) to tilde (126). ### Output For each string passed to the decode function return its translation. Also feel free to reuse/extend the following starter code: ```python def tongues(code): ```
{"functional": "_inputs = [['Ita dotf ni dyca nsaw ecc.'], ['Tim oh nsa nowa gid ecc fiir wat ni liwa ni nsa eor ig nsaod liytndu.'], ['Giydhlida etr hakat uaedh efi iyd gidagensadh pdiyfsn ytni nsoh'], ['litnotatn e tam tenoit.'], ['Nsa zyolv pdimt gij xywbar ikad nsa cequ rifh.'], ['Tywpadh (1234567890) etr bytlnyenoit, nsau hsiycr pins pa ytlsetfar!'], [' '], ['Nsoh oh tin Vcotfit pyn on liycr pa e roggadatn gidaoft cetfyefa.'], ['0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789'], ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'], ['mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm'], ['z'], [''], ['****************************************************************************************************'], ['q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1']]\n_outputs = [['One ring to rule them all.'], ['Now is the time for all good men to come to the aid of their country.'], ['Fourscore and seven years ago our forefathers brought unto this'], ['continent a new nation.'], ['The quick brown fox jumped over the lazy dogs.'], ['Numbers (1234567890) and punctuation, they should both be unchanged!'], [' '], ['This is not Klingon but it could be a different foreign language.'], ['0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789'], ['eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'], ['wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww'], ['q'], [''], ['****************************************************************************************************'], ['z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1']]\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(tongues(*i), o[0])"}
402
1,010
coding
Solve the programming task below in a Python markdown code block. These days, many boys use beautiful girls' photos as dps in Social Networking Profiles. So it is pretty hard to guess whether it is a fake profile or not.Last year, Yogesh went to a forum and had a nice chat with a beauty (he thought so). After that they talked very often and eventually they became a couple in the network. But yesterday, he came to see "her" in the real world and found out "she" is actually a very strong man! Our hero is very sad and he is too tired to love again now. So he came up with a way to recognize users' genders by their user names. This is his method: if the number of distinct characters in one's user name is odd, then he is a male, otherwise she is a female. You are given the string that denotes the user name, please help Yogesh to determine the gender of this user by his method. Input The first line contains a non-empty string, that contains only lowercase English letters — the user name. This string contains at most 100 letters. Output If it is a female by Yogesh's method, print "CHAT WITH HER!" (without the quotes), otherwise, print "IGNORE HIM!" (without the quotes). SAMPLE INPUT payal SAMPLE OUTPUT CHAT WITH HER!
{"inputs": ["rahul", "palak", "payal", "anisha"], "outputs": ["IGNORE HIM!", "CHAT WITH HER!", "CHAT WITH HER!", "IGNORE HIM!"]}
281
41
coding
Solve the programming task below in a Python markdown code block. Today's Random Number E869120 You ran a campaign called "Today's Random Numbers" for N days. This is a project to generate a random number once a day and post the value on Twitter. The "random numbers of the day" on day $ 1, 2, 3, \ dots, N $ were $ A_1, A_2, A_3, \ dots, A_N $, respectively. E869120 You would be happy if today's random number is higher than yesterday's random number. How many times in the $ N $ day did E869120 make you happy with "Today's Random Numbers"? input Input is given from standard input in the following format. $ N $ $ A_1 $ $ A_2 $ $ A_3 $ $ \ dots $ $ A_N $ output Print out the number of times E869120 was pleased with "Today's Random Numbers" in one line in $ N $ days. However, insert a line break at the end. Constraint * $ 1 \ leq N \ leq 100000 \ (= 10 ^ 5) $ * $ 1 \ leq A_i \ leq 1000000000 \ (= 10 ^ 9) $ * All inputs are integers. Input example 1 Five 8 6 9 1 20 Output example 1 2 On the 3rd and 5th days, E869120 is happy. Input example 2 6 3 3 4 3 3 4 Output example 2 2 On the 3rd and 6th days, E869120 is happy. Input example 3 Ten 10 9 8 7 6 5 4 3 2 1 Output example 3 0 E869120 You will not be happy with "Today's Random Numbers". Example Input 5 8 6 9 1 20 Output 2
{"inputs": ["5\n1 1 0 0 0", "5\n0 0 0 2 1", "5\n0 0 1 2 1", "5\n0 0 1 2 2", "5\n0 0 1 4 2", "5\n0 0 2 4 2", "5\n0 0 2 4 1", "5\n0 0 0 4 1"], "outputs": ["0\n", "1\n", "2\n", "2\n", "2\n", "2\n", "2\n", "1\n"]}
478
142
coding
Solve the programming task below in a Python markdown code block. The prime number sequence starts with: `2,3,5,7,11,13,17,19...`. Notice that `2` is in position `one`. `3` occupies position `two`, which is a prime-numbered position. Similarly, `5`, `11` and `17` also occupy prime-numbered positions. We shall call primes such as `3,5,11,17` dominant primes because they occupy prime-numbered positions in the prime number sequence. Let's call this `listA`. As you can see from listA, for the prime range `range(0,10)`, there are `only two` dominant primes (`3` and `5`) and the sum of these primes is: `3 + 5 = 8`. Similarly, as shown in listA, in the `range (6,20)`, the dominant primes in this range are `11` and `17`, with a sum of `28`. Given a `range (a,b)`, what is the sum of dominant primes within that range? Note that `a <= range <= b` and `b` will not exceed `500000`. Good luck! If you like this Kata, you will enjoy: [Simple Prime Streaming](https://www.codewars.com/kata/5a908da30025e995880000e3) [Sum of prime-indexed elements](https://www.codewars.com/kata/59f38b033640ce9fc700015b) [Divisor harmony](https://www.codewars.com/kata/59bf97cd4f98a8b1cd00007e) Also feel free to reuse/extend the following starter code: ```python def solve(a,b): ```
{"functional": "_inputs = [[0, 10], [2, 200], [200, 2000], [500, 10000], [1000, 100000], [2000, 200000], [3000, 400000], [4000, 500000]]\n_outputs = [[8], [1080], [48132], [847039], [52114889], [183035206], [650120994], [972664400]]\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])"}
425
309
coding
Solve the programming task below in a Python markdown code block. You are in a grid of dimensions N \times M. You are allowed to perform two types of operations: Go down, left, up, or right each for a cost of X. Formally, if you are at the cell (i,j) of the grid, you can go to either of the cells (i + 1, j), (i, j - 1), (i - 1, j) or (i, j + 1) at a cost of X. Go diagonally down-left, down-right, up-left, up-right, for a cost of Y. Formally, if you are at the cell (i,j) of the grid, you can go to either of the cells (i + 1, j - 1), (i + 1, j + 1), (i - 1, j - 1) or (i - 1, j + 1) at a cost of Y. You cannot exit the grid at any point. Find the minimum cost of going to the bottom-right corner (N, M) from the top-left corner (1, 1). \textbf{Note}: Since input-output is large, prefer using fast input-output methods. ------ Input Format ------ - First line will contain T, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, four space separated integers integers: N, M, X, Y. ------ Output Format ------ For each testcase, output the answer in a new line. ------ Constraints ------ $1 ≤ T ≤ 5 \cdot 10^{5}$ $1 ≤ N, M, X, Y ≤ 10^{6}$ ----- Sample Input 1 ------ 3 5 6 2 5 4 7 5 6 7 8 6 5 ----- Sample Output 1 ------ 18 33 36 ----- explanation 1 ------ Test Case $1$: We can go $4$ steps down from $(1, 1)$ to $(5, 1)$ and then $5$ steps right to $(5, 6)$. The total cost is $2 \times 9 = 18$ Test Case $2$: We can go $3$ steps diagonally down-right from $(1, 1)$ to $(4, 4)$ and then $3$ steps right to $(4, 7)$. The total cost is $3 \times 6 + 3 \times 5 = 18 + 15 = 33$ Test Case $3$: We can go $6$ steps diagonally down-right from $(1, 1)$ to $(7, 7)$ and then $1$ step right to $(7, 8)$. The total cost is $5 \times 6 + 6 = 36$
{"inputs": ["3\n5 6 2 5\n4 7 5 6\n7 8 6 5"], "outputs": ["18\n33\n36"]}
625
43
coding
Solve the programming task below in a Python markdown code block. Jaggu is a little kid and he likes playing with water balloons. He took 1 million ( $10^{6} $) empty buckets and he filled the bucket with water balloons under the instruction of his sister Ishika. His sister gives him two types of commands: R pos1 pos2 which implies that jaggu needs to tell her what is the total number of water balloons in the bucket from pos1 to pos2 (both included). U pos M plus which implies that he has to work like the function Update(pos,M,plus) void Update(int pos,int M,int plus) { int N=1000000; //1 million for (int i=1;i<=50;i++) { int back = pos for(int j=1;j<=1000;j++) { add M water ballons at bucket pos int s,in=__builtin_popcount(pos); for(int k=0;;k++) { s=pos+pow(2,k) if( __builtin_popcount(s) <= in ) { in = __builtin_popcount(s) pos = s; if(pos>N) break; add M water ballons at bucket pos } } pos = pos - N } pos = back+plus; if(pos>N) pos-=N; } } Jaggu is too lazy to put the water ballons in the bucket. Afraid that he might be caught for not doing what his sister told him to do so, he asks your help to provide correct answers for each of his sister's query. . Input Format First line contains Q, number of queries to follow. Next Q line follows , which can be either an Update Query or Report Query.Each Update Query is followed by atleast 1 report query. Output Format For each report query , output the answer in a separate line. Constraints 1 ≤ Q ≤ 2 *$ 10^{5}$ 1 ≤ pos1,pos2,pos ≤ $10^{6}$ pos1 ≤ pos2 1 ≤ M ≤ 10 1 ≤ plus ≤ 999999 Sample Input 2 U 692778 7 291188 R 636916 747794 Sample Output 378 Explanation Follow the code above to get the answer. Note Input is randomly generated. __builtin_popcount(x) gives the number of set bits in binary representation of x. pow(2,k) denotes 2 raised to k , i.e. exponentiation of 2. Timelimit is 3 times the timelimit mentioned here
{"inputs": ["2\nU 692778 7 291188\nR 636916 747794\n"], "outputs": ["378 \n"]}
594
51
coding
Solve the programming task below in a Python markdown code block. Madoka is going to enroll in "TSUNS PTU". But she stumbled upon a difficult task during the entrance computer science exam: A number is called good if it is a multiple of $d$. A number is called beatiful if it is good and it cannot be represented as a product of two good numbers. Notice that a beautiful number must be good. Given a good number $x$, determine whether it can be represented in at least two different ways as a product of several (possibly, one) beautiful numbers. Two ways are different if the sets of numbers used are different. Solve this problem for Madoka and help her to enroll in the best school in Russia! -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 100$) — number of test cases. Below comes their description. Each test case consists of two integers $x$ and $d$, separated by a space ($2 \leq x, d \leq 10^9$). It is guaranteed that $x$ is a multiple of $d$. -----Output----- For each set of input data, output "NO" if the number cannot be represented in at least two ways. Otherwise, output "YES". You can output each letter in any case (for example, "YES", "Yes", "yes", "yEs", "yEs" will be recognized as a positive answer). -----Examples----- Input 8 6 2 12 2 36 2 8 2 1000 10 2376 6 128 4 16384 4 Output NO NO YES NO YES YES NO YES -----Note----- In the first example, $6$ can be represented as $6$, $1 \cdot 6$, $2 \cdot 3$. But $3$ and $1$ are not a good numbers because they are not divisible by $2$, so there is only one way. In the second example, $12$ can be represented as $6 \cdot 2$, $12$, $3 \cdot 4$, or $3 \cdot 2 \cdot 2$. The first option is suitable. The second is— no, because $12$ is not beautiful number ($12 = 6 \cdot 2$). The third and fourth are also not suitable, because $3$ is not good number. In the third example, $36$ can be represented as $18 \cdot 2$ and $6 \cdot 6$. Therefore it can be decomposed in at least two ways.
{"inputs": ["8\n6 2\n12 2\n36 2\n8 2\n1000 10\n2376 6\n128 4\n16384 4\n"], "outputs": ["NO\nNO\nYES\nNO\nYES\nYES\nNO\nYES\n"]}
578
75
coding
Solve the programming task below in a Python markdown code block. Chef has a permutation P of length 2 \cdot N. He can perform the following operation on P: Select an index i (1 ≤ i < 2 \cdot N) and swap P_{i} and P_{i + 1}. Chef would call the permutation P *good*, if the maximum element of the first half of P is less than the minimum element of the second half of P. Formally \displaystyle \max_{(1 ≤ i ≤ N)} P_{i} < \min_{(N < i ≤ 2 \cdot N)} P_{i}. Find the minimum number of operations Chef needs to apply to make P *good*. Note: A permutation of length N is an array where every integer from 1 to N occurs exactly once. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — half the size of the permutation P. - The second line of each test case contains 2 \cdot N space-separated integers P_{1}, P_{2}, \dots, P_{2 \cdot N} denoting the permutation P. ------ Output Format ------ For each test case, output the minimum number of operations required to make P *good*. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{5}$ $P$ is a permutation of length $2 \cdot N$ - The sum of $N$ over all test cases won't exceed $2 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 3 2 1 4 3 1 2 3 4 5 6 3 6 5 4 3 2 1 ----- Sample Output 1 ------ 2 0 9 ----- explanation 1 ------ Test case $1$: We can perform the following operations: - Apply operation at $i = 1$: $[3, 2, 1, 4] \rightarrow [2, 3, 1, 4]$ - Apply operation at $i = 2$: $[2, 3, 1, 4] \rightarrow [2, 1, 3, 4]$ Test case $2$: $P$ is already good.
{"inputs": ["3\n2\n3 2 1 4\n3\n1 2 3 4 5 6\n3\n6 5 4 3 2 1\n"], "outputs": ["2\n0\n9\n"]}
522
56
coding
Solve the programming task below in a Python markdown code block. Rational numbers are numbers represented by ratios of two integers. For a prime number p, one of the elementary theorems in the number theory is that there is no rational number equal to √p. Such numbers are called irrational numbers. It is also known that there are rational numbers arbitrarily close to √p Now, given a positive integer n, we define a set Qn of all rational numbers whose elements are represented by ratios of two positive integers both of which are less than or equal to n. For example, Q4 is a set of 11 rational numbers {1/1, 1/2, 1/3, 1/4, 2/1, 2/3, 3/1, 3/2, 3/4, 4/1, 4/3}. 2/2, 2/4, 3/3, 4/2 and 4/4 are not included here because they are equal to 1/1, 1/2, 1/1, 2/1 and 1/1, respectively. Your job is to write a program that reads two integers p and n and reports two rational numbers x / y and u / v, where u / v < √p < x / y and there are no other elements of Qn between u/v and x/y. When n is greater than √p, such a pair of rational numbers always exists. Input The input consists of lines each of which contains two positive integers, a prime number p and an integer n in the following format. p n They are separated by a space character. You can assume that p and n are less than 10000, and that n is greater than √p. The end of the input is indicated by a line consisting of two zeros. Output For each input line, your program should output a line consisting of the two rational numbers x / y and u / v (x / y > u / v) separated by a space character in the following format. x/y u/v They should be irreducible. For example, 6/14 and 15/3 are not accepted. They should be reduced to 3/7 and 5/1, respectively. Example Input 2 5 3 10 5 100 0 0 Output 3/2 4/3 7/4 5/3 85/38 38/17
{"inputs": ["3 4\n3 8\n5 100\n0 0", "3 5\n3 7\n7 101\n0 0", "3 8\n3 8\n5 100\n0 0", "6 8\n3 8\n5 100\n0 0", "3 2\n3 7\n7 101\n0 0", "3 3\n3 7\n7 100\n0 0", "2 6\n3 5\n5 100\n0 0", "3 2\n3 8\n5 100\n0 0"], "outputs": ["2/1 3/2\n7/4 5/3\n85/38 38/17\n", "2/1 5/3\n7/4 5/3\n45/17 82/31\n", "7/4 5/3\n7/4 5/3\n85/38 38/17\n", "5/2 7/3\n7/4 5/3\n85/38 38/17\n", "2/1 1/1\n7/4 5/3\n45/17 82/31\n", "2/1 3/2\n7/4 5/3\n45/17 82/31\n", "3/2 4/3\n2/1 5/3\n85/38 38/17\n", "2/1 1/1\n7/4 5/3\n85/38 38/17\n"]}
543
398
coding
Solve the programming task below in a Python markdown code block. A positive number $x$ of length $n$ in base $p$ ($2 \le p \le 10^9$) is written on the blackboard. The number $x$ is given as a sequence $a_1, a_2, \dots, a_n$ ($0 \le a_i < p$) — the digits of $x$ in order from left to right (most significant to least significant). Dmitry is very fond of all the digits of this number system, so he wants to see each of them at least once. In one operation, he can: take any number $x$ written on the board, increase it by $1$, and write the new value $x + 1$ on the board. For example, $p=5$ and $x=234_5$. Initially, the board contains the digits $2$, $3$ and $4$; Dmitry increases the number $234_5$ by $1$ and writes down the number $240_5$. On the board there are digits $0, 2, 3, 4$; Dmitry increases the number $240_5$ by $1$ and writes down the number $241_5$. Now the board contains all the digits from $0$ to $4$. Your task is to determine the minimum number of operations required to make all the digits from $0$ to $p-1$ appear on the board at least once. -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 2 \cdot 10^3$) — the number of test cases. The descriptions of the input test cases follow. The first line of description of each test case contains two integers $n$ ($1 \le n \le 100$) and $p$ ($2 \le p \le 10^9$) — the length of the number and the base of the number system. The second line of the description of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i < p$) — digits of $x$ in number system with base $p$ It is guaranteed that the number $x$ does not contain leading zeros (that is, $a_1>0$). -----Output----- For each test case print a single integer — the minimum number of operations required for Dmitry to get all the digits on the board from $0$ to $p-1$. It can be shown that this always requires a finite number of operations. -----Examples----- Input 11 2 3 1 2 4 2 1 1 1 1 6 6 1 2 3 4 5 0 5 2 1 0 1 0 1 3 10 1 2 3 5 1000 4 1 3 2 5 3 5 2 3 4 4 4 3 2 3 0 1 3 2 5 5 1 2 2 2 4 3 4 1 0 1 Output 1 1 0 0 7 995 2 1 1 1 2 -----Note----- None
{"inputs": ["1\n7 8\n7 7 7 7 7 7 7\n", "11\n2 3\n1 2\n4 2\n1 1 1 1\n6 6\n1 2 3 4 5 0\n5 2\n1 0 1 0 1\n3 10\n1 2 3\n5 1000\n4 1 3 2 5\n3 5\n2 3 4\n4 4\n3 2 3 0\n1 3\n2\n5 5\n1 2 2 2 4\n3 4\n1 0 1\n"], "outputs": ["7\n", "1\n1\n0\n0\n7\n995\n2\n1\n1\n1\n2\n"]}
746
193
coding
Solve the programming task below in a Python markdown code block. An area named Renus, is divided into $(N \times M)$ cells. According to archaeological survey the area contains huge amount of treasure. Some cells out of $(N \times M)$ cells contain treasure. But problem is, you can't go to every cell as some of the cells are blocked. For every $a_{ij}$ cell($1 \leq i \leq N$,$1 \leq j \leq M$), your task is to find the distance of the nearest cell having treasure. Note: - You can only traverse up, down, left and right from a given cell. - Diagonal movements are not allowed. - Cells having treasure can't be blocked, only empty cells ( cells without treasure) can be blocked. -----Input Format:------ - First line contains $T$, the number of test cases. - Second line contains two space-separated integers $N\ and\ M$. - Third line contains a single integer $X$ denoting number of cells having treasures, followed by $X$ lines containing two space-separated integers $x_i$ and $y_i$ denoting position of row and column of $i^{th}$ treasure, for every $1\leq i \leq X$ - The next line contains a single integer $Y$ denoting the number of cells that are blocked, and it is followed by subsequent $Y$ lines containing two space-separated integers $u_i$ and $v_i$ denoting position of row and column of blocked cells , for every $1\leq i \leq Y$ -----Constraints:------ - $1\le T \le 100$ - $1 \le N, M \le 200$ - $1 \le X < N*M$ - $0 \le Y <(N*M) - X$ - $1 \le x_i,u_j \le N, for\ every\ 1 \le i \le X\ and\ 1 \le j \le Y$ - $1 \le y_i,v_j \le M, for\ every\ 1 \le i \le X\ and\ 1 \le j \le Y$ -----Output Format:------ For each test case print a $N \times M$ matrix where each cell consists of distance of nearest treasure. Cells that are blocked will show "$X$" (without quotes). Also cells that doesn't have access to any treasure will show "$-1$" (without quotes). Note: Co-ordinate of top left cell is $(1,1)$. -----Sample Input----- 1 3 3 2 1 1 1 3 2 2 1 2 2 -----Sample Output----- 0 1 0 X X 1 4 3 2 -----Explanation:----- - Coordinates (1,1) and (1,3) shows "0" because they contain treasure and nearest distance is 0. - Coordinates (2,1) and (2,2) shows "X" as they are blocked. - Rest shows distance of nearest cell having treasure.
{"inputs": ["1\n3 3\n2\n1 1\n1 3\n2\n2 1\n2 2"], "outputs": ["0 1 0\nX X 1\n4 3 2"]}
666
51
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given the string s, return the size of the longest substring containing each vowel an even number of times. That is, 'a', 'e', 'i', 'o', and 'u' must appear an even number of times.   Please complete the following python code precisely: ```python class Solution: def findTheLongestSubstring(self, s: str) -> int: ```
{"functional": "def check(candidate):\n assert candidate(s = \"eleetminicoworoep\") == 13\n assert candidate(s = \"leetcodeisgreat\") == 5\n assert candidate(s = \"bcbcbc\") == 6\n\n\ncheck(Solution().findTheLongestSubstring)"}
98
69
coding
Solve the programming task below in a Python markdown code block. A spy needs your help to encrypt a 5 letter message. To encrypt the message the following steps are need to be done: 1)Each letter of the message will be converted to it's equivalent number (i.e A=0, B=1,..Z=25) 2)98, 57, 31, 45, 46 are the 5 numbers in the key. 3)Add the equivalent number of the first letter of the message to the first number of the key, then the equivalent number of the second letter of the message to the second number of the key and so on. 4)Take modulo 26 of the sum of each expression. 5)Convert the newly obtained numbers into it's equivalent letters (i.e 0=A, 1=B,…25=Z) Print the final encrypted string. Note: It is guaranteed that the given string only contains Only Upper-case Latin Letters. -----Input:----- - The first line of the input contains a single Integer $T$. $T$ denoting the number of testcases, description of each testcases contains. - The first line of the each testcase contains a single string $S$. $S$ denoting the string which you need to encrypt -----Output:----- - For each test case, print encypted string i.e result of above operation. Note: Each testcase output must be printed on new line -----Constraints:----- - $1 \leq T \leq 100$ - $1 \leq S \leq 5$ -----Sample Input:----- 2 HELLO HELL -----Sample Output:----- BJQEI BJQE -----Explanation:----- - For 1st Testcase : The conversion of "HELLO" to its equivalent number will be 7, 4, 11, 11, 14 respectively. Then we add each equivalent number of the message with corresponding number in the key: 7 + 98 = 105 4 + 57 = 61 11 + 31 = 42 11 + 45 = 56 14 + 46 = 60 Further we take the sum: 105 modulo 26 = 1 61 modulo 26 = 9 42 modulo 26 = 16 56 modulo 26 = 4 60 modulo 26 = 8 Finally, convert the newly obtained numbers into it's equivalent letters: B J Q E I Print the final string: BJQEI
{"inputs": ["2\nHELLO\nHELL"], "outputs": ["BJQEI\nBJQE"]}
568
22
coding
Solve the programming task below in a Python markdown code block. Everything is great about Ilya's city, except the roads. The thing is, the only ZooVille road is represented as n holes in a row. We will consider the holes numbered from 1 to n, from left to right. Ilya is really keep on helping his city. So, he wants to fix at least k holes (perharps he can fix more) on a single ZooVille road. The city has m building companies, the i-th company needs ci money units to fix a road segment containing holes with numbers of at least li and at most ri. The companies in ZooVille are very greedy, so, if they fix a segment containing some already fixed holes, they do not decrease the price for fixing the segment. Determine the minimum money Ilya will need to fix at least k holes. Input The first line contains three integers n, m, k (1 ≤ n ≤ 300, 1 ≤ m ≤ 105, 1 ≤ k ≤ n). The next m lines contain the companies' description. The i-th line contains three integers li, ri, ci (1 ≤ li ≤ ri ≤ n, 1 ≤ ci ≤ 109). Output Print a single integer — the minimum money Ilya needs to fix at least k holes. If it is impossible to fix at least k holes, print -1. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 10 4 6 7 9 11 6 9 13 7 7 7 3 5 6 Output 17 Input 10 7 1 3 4 15 8 9 8 5 6 8 9 10 6 1 4 2 1 4 10 8 10 13 Output 2 Input 10 1 9 5 10 14 Output -1
{"inputs": ["1 1 1\n1 1 1\n", "10 1 3\n5 10 8\n", "10 1 3\n5 10 1\n", "10 1 3\n5 10 14\n", "10 1 3\n5 10 21\n", "10 1 9\n5 10 24\n", "14 1 9\n5 10 24\n", "10 1 9\n5 10 14\n"], "outputs": ["1\n", "8\n", "1\n", "14\n", "21\n", "-1\n", "-1\n", "-1\n"]}
464
171
coding
Solve the programming task below in a Python markdown code block. Chef and his friend is standing on the X-axis at the points X_{1} and X_{2} respectively. Chef moves one step forward each second (that is he moves to X_{1} + 1 after the 1^{st} second, X_{1} + 2 after the 2^{nd} second, and so on), whereas his friend moves 2 steps forward each second (that is he moves to X_{2} + 2 after the 1^{st} second, X_{2} + 4 after the 2^{nd} second, and so on). You need to determine if Chef will be able to meet his friend or not. You can assume that Chef and his friend both keep on moving for a long indefinite amount of time and also that they move simultaneously. ------ Input Format ------ - The first line contains T - the number of test cases. Then the test cases follow. - The first line of each test case contain two space-separated integers X_{1} and X_{2} - the position of Chef and his friend at the start respectively. ------ Output Format ------ For each test case, output on a single line YES (without quotes) if Chef can meet his friend, and NO otherwise. You may print each character of the string in uppercase or lowercase (for example, the strings YeS, YEs, yes and yeS will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ X_{1}, X_{2} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 1 1 1 7 7 1 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ - Test case $1$: Chef and his friend are on the same coordinate initially. Hence, they have already met. - Test case $2$: No matter for how long they move Chef and his friend will never meet. - Test case $3$: After $6$ seconds, Chef and his friend both will be at $X = 13$.
{"inputs": ["3\n1 1\n1 7\n7 1\n"], "outputs": ["YES\nNO\nYES"]}
454
29
coding
Solve the programming task below in a Python markdown code block. A + B is often used as an example of the easiest problem possible to show some contest platform. However, some scientists have observed that sometimes this problem is not so easy to get accepted. Want to try? -----Input----- The input contains two integers a and b (0 ≤ a, b ≤ 10^3), separated by a single space. -----Output----- Output the sum of the given integers. -----Examples----- Input 5 14 Output 19 Input 381 492 Output 873
{"inputs": ["5 14\n", "1 14\n", "0 14\n", "0 52\n", "0 87\n", "5 14\n", "-1 14\n", "72 49\n"], "outputs": ["19\n", "15\n", "14\n", "52\n", "87\n", "19\n", "13\n", "121\n"]}
126
104
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. A cinema has n rows of seats, numbered from 1 to n and there are ten seats in each row, labelled from 1 to 10 as shown in the figure above. Given the array reservedSeats containing the numbers of seats already reserved, for example, reservedSeats[i] = [3,8] means the seat located in row 3 and labelled with 8 is already reserved. Return the maximum number of four-person groups you can assign on the cinema seats. A four-person group occupies four adjacent seats in one single row. Seats across an aisle (such as [3,3] and [3,4]) are not considered to be adjacent, but there is an exceptional case on which an aisle split a four-person group, in that case, the aisle split a four-person group in the middle, which means to have two people on each side.   Please complete the following python code precisely: ```python class Solution: def maxNumberOfFamilies(self, n: int, reservedSeats: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(n = 3, reservedSeats = [[1,2],[1,3],[1,8],[2,6],[3,1],[3,10]]) == 4\n assert candidate(n = 2, reservedSeats = [[2,1],[1,8],[2,6]]) == 2\n assert candidate(n = 4, reservedSeats = [[4,3],[1,4],[4,6],[1,7]]) == 4\n\n\ncheck(Solution().maxNumberOfFamilies)"}
253
122
coding
Solve the programming task below in a Python markdown code block. In this simple exercise, you will build a program that takes a value, `integer `, and returns a list of its multiples up to another value, `limit `. If `limit` is a multiple of ```integer```, it should be included as well. There will only ever be positive integers passed into the function, not consisting of 0. The limit will always be higher than the base. For example, if the parameters passed are `(2, 6)`, the function should return `[2, 4, 6]` as 2, 4, and 6 are the multiples of 2 up to 6. If you can, try writing it in only one line of code. Also feel free to reuse/extend the following starter code: ```python def find_multiples(integer, limit): ```
{"functional": "_inputs = [[5, 25], [1, 2], [5, 7], [4, 27], [11, 54]]\n_outputs = [[[5, 10, 15, 20, 25]], [[1, 2]], [[5]], [[4, 8, 12, 16, 20, 24]], [[11, 22, 33, 44]]]\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_multiples(*i), o[0])"}
181
249
coding
Solve the programming task below in a Python markdown code block. In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i < n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously. In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles. The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible. Input The first line contains integer n (1 ≤ n ≤ 103) — the boulevard's length in tiles. The second line contains n space-separated integers ai — the number of days after which the i-th tile gets destroyed (1 ≤ ai ≤ 103). Output Print a single number — the sought number of days. Examples Input 4 10 3 5 10 Output 5 Input 5 10 2 8 3 5 Output 5 Note In the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it. In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted.
{"inputs": ["1\n1\n", "1\n17\n", "2\n1 2\n", "1\n500\n", "1\n987\n", "1\n585\n", "1\n435\n", "1\n474\n"], "outputs": ["1\n", "17\n", "1\n", "500\n", "987\n", "585\n", "435\n", "474\n"]}
577
110
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an array of integers arr and an integer k. Find the least number of unique integers after removing exactly k elements.   Please complete the following python code precisely: ```python class Solution: def findLeastNumOfUniqueInts(self, arr: List[int], k: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(arr = [5,5,4], k = 1) == 1\n assert candidate(arr = [4,3,1,1,3,3,2], k = 3) == 2\n\n\ncheck(Solution().findLeastNumOfUniqueInts)"}
85
74
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. In combinatorial mathematics, a derangement is a permutation of the elements of a set, such that no element appears in its original position. You are given an integer n. There is originally an array consisting of n integers from 1 to n in ascending order, return the number of derangements it can generate. Since the answer may be huge, return it modulo 109 + 7.   Please complete the following python code precisely: ```python class Solution: def findDerangement(self, n: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(n = 3) == 2\n assert candidate(n = 2) == 1\n\n\ncheck(Solution().findDerangement)"}
131
44
coding
Solve the programming task below in a Python markdown code block. Polycarp starts his own business. Tomorrow will be the first working day of his car repair shop. For now the car repair shop is very small and only one car can be repaired at a given time. Polycarp is good at marketing, so he has already collected n requests from clients. The requests are numbered from 1 to n in order they came. The i-th request is characterized by two values: s_{i} — the day when a client wants to start the repair of his car, d_{i} — duration (in days) to repair the car. The days are enumerated from 1, the first day is tomorrow, the second day is the day after tomorrow and so on. Polycarp is making schedule by processing requests in the order from the first to the n-th request. He schedules the i-th request as follows: If the car repair shop is idle for d_{i} days starting from s_{i} (s_{i}, s_{i} + 1, ..., s_{i} + d_{i} - 1), then these days are used to repair a car of the i-th client. Otherwise, Polycarp finds the first day x (from 1 and further) that there are d_{i} subsequent days when no repair is scheduled starting from x. In other words he chooses the smallest positive x that all days x, x + 1, ..., x + d_{i} - 1 are not scheduled for repair of any car. So, the car of the i-th client will be repaired in the range [x, x + d_{i} - 1]. It is possible that the day x when repair is scheduled to start will be less than s_{i}. Given n requests, you are asked to help Polycarp schedule all of them according to the rules above. -----Input----- The first line contains integer n (1 ≤ n ≤ 200) — the number of requests from clients. The following n lines contain requests, one request per line. The i-th request is given as the pair of integers s_{i}, d_{i} (1 ≤ s_{i} ≤ 10^9, 1 ≤ d_{i} ≤ 5·10^6), where s_{i} is the preferred time to start repairing the i-th car, d_{i} is the number of days to repair the i-th car. The requests should be processed in the order they are given in the input. -----Output----- Print n lines. The i-th line should contain two integers — the start day to repair the i-th car and the finish day to repair the i-th car. -----Examples----- Input 3 9 2 7 3 2 4 Output 9 10 1 3 4 7 Input 4 1000000000 1000000 1000000000 1000000 100000000 1000000 1000000000 1000000 Output 1000000000 1000999999 1 1000000 100000000 100999999 1000001 2000000
{"inputs": ["1\n1 1\n", "1\n1 1\n", "2\n6 3\n9 3\n", "2\n6 3\n9 1\n", "2\n6 1\n9 1\n", "2\n6 1\n9 2\n", "2\n6 1\n5 2\n", "2\n6 1\n2 2\n"], "outputs": ["1 1\n", "1 1\n", "6 8\n9 11\n", "6 8\n9 9\n", "6 6\n9 9\n", "6 6\n9 10\n", "6 6\n1 2\n", "6 6\n2 3\n"]}
749
168
coding
Solve the programming task below in a Python markdown code block. Amit is going on a date and he wants to gift his date an array of positive numbers. But he is running short on money. He already has an array of numbers in design. Cost of an array of numbers is the sum of elements in it. But he wants to minimize the cost of making it. So he does the following number of operations one by one for any number of times: He chooses two adjacent elements ,replace them by one element with value = XOR of the two numbers. This operation reduces length of array (and elements are re-numerated accordingly) Find the minimum amount of money that Amit needs to spend to gift his date. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of $2$ lines of input, first line contains a single integer $N$ and the second line contains $N$ elements - $A1,A2,A3,.....,AN$ -----Output:----- For each testcase, output in a single line answer denoting the minimum cost -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^5$ - $0 \leq Ai \leq 10^9$ for $1\leq i \leq N$ -----Sample Input:----- 3 5 8 4 1 5 0 5 1 2 4 0 8 2 10 10 -----Sample Output:----- 8 15 0 -----EXPLANATION:----- For first case, This array is : $[8,4,1,5,0] -> [8,4,4,0] -> [8,0,0]$. Sum=$8$ So the answer is 8.
{"inputs": ["3\n5\n8 4 1 5 0\n5\n1 2 4 0 8\n2\n10 10"], "outputs": ["8\n15\n0"]}
402
49
coding
Solve the programming task below in a Python markdown code block. Write a function that flattens an `Array` of `Array` objects into a flat `Array`. Your function must only do one level of flattening. ```python flatten [1,2,3] # => [1,2,3] flatten [[1,2,3],["a","b","c"],[1,2,3]] # => [1,2,3,"a","b","c",1,2,3] flatten [[[1,2,3]]] # => [[1,2,3]] ``` Also feel free to reuse/extend the following starter code: ```python def flatten(lst): ```
{"functional": "_inputs = [[[[[3], [4], [5]], [9], [9, 9], [8], [[1, 2, 3]]]], [[[[3], [4], [5]], [9], [9, 9], [8], [[1, 2, 3], [77777]]]], [[[[3], [4], [5]], [9], [9, 9], [8], [[1, 2, 3], [77777]], 'a']], [[[[3], [4], [5]], [9], [9, 9], [8], [[1, 2, 3], [77777]], [['a']]]]]\n_outputs = [[[[3], [4], [5], 9, 9, 9, 8, [1, 2, 3]]], [[[3], [4], [5], 9, 9, 9, 8, [1, 2, 3], [77777]]], [[[3], [4], [5], 9, 9, 9, 8, [1, 2, 3], [77777], 'a']], [[[3], [4], [5], 9, 9, 9, 8, [1, 2, 3], [77777], ['a']]]]\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(flatten(*i), o[0])"}
150
455