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. Vasya plays the Need For Brake. He plays because he was presented with a new computer wheel for birthday! Now he is sure that he will win the first place in the championship in his favourite racing computer game! n racers take part in the championship, which consists of a number of races. After each race racers are arranged from place first to n-th (no two racers share the same place) and first m places are awarded. Racer gains bi points for i-th awarded place, which are added to total points, obtained by him for previous races. It is known that current summary score of racer i is ai points. In the final standings of championship all the racers will be sorted in descending order of points. Racers with an equal amount of points are sorted by increasing of the name in lexicographical order. Unfortunately, the championship has come to an end, and there is only one race left. Vasya decided to find out what the highest and lowest place he can take up as a result of the championship. Input The first line contains number n (1 ≤ n ≤ 105) — number of racers. Each of the next n lines contains si and ai — nick of the racer (nonempty string, which consist of no more than 20 lowercase Latin letters) and the racer's points (0 ≤ ai ≤ 106). Racers are given in the arbitrary order. The next line contains the number m (0 ≤ m ≤ n). Then m nonnegative integer numbers bi follow. i-th number is equal to amount of points for the i-th awarded place (0 ≤ bi ≤ 106). The last line contains Vasya's racer nick. Output Output two numbers — the highest and the lowest place Vasya can take up as a result of the championship. Examples Input 3 teama 10 teamb 20 teamc 40 2 10 20 teama Output 2 3 Input 2 teama 10 teamb 10 2 10 10 teamb Output 2 2
{"inputs": ["2\nteama 10\nteamb 10\n2\n10 10\nteamb\n", "3\nteama 10\nteamb 20\nteamc 40\n2\n10 20\nteama\n", "5\nteamc 8\nteamd 7\nteame 15\nteama 3\nteamb 21\n3\n1 2 3\nteamb\n", "5\nteamc 4\nteamd 7\nteame 15\nteama 3\nteamb 21\n3\n1 2 3\nteamb\n", "5\ntaemc 4\nteamd 7\nteame 15\nteama 3\nteamb 21\n3\n1 2 3\nteamb\n", "5\ntaemc 4\nteamd 7\nteame 15\nteama 3\nteamb 21\n3\n1 2 6\nteamb\n", "5\nteamc 8\nteamd 7\nteame 15\nteama 2\nteamb 21\n3\n1 2 3\nteamb\n", "5\nteamc 4\nueamd 7\nteame 15\nteama 3\nteamb 21\n3\n1 2 3\nteamb\n"], "outputs": ["2 2", "2 3", "1 1", "1 1\n", "1 1\n", "1 1\n", "1 1\n", "1 1\n"]}
464
377
coding
Solve the programming task below in a Python markdown code block. Given an integer a as input, print the value a + a^2 + a^3. -----Constraints----- - 1 \leq a \leq 10 - a is an integer. -----Input----- Input is given from Standard Input in the following format: a -----Output----- Print the value a + a^2 + a^3 as an integer. -----Sample Input----- 2 -----Sample Output----- 14 When a = 2, we have a + a^2 + a^3 = 2 + 2^2 + 2^3 = 2 + 4 + 8 = 14. Print the answer as an input. Outputs such as 14.0 will be judged as incorrect.
{"inputs": ["1", "0", "3", "4", "6", "7", "5", "9"], "outputs": ["3\n", "0\n", "39\n", "84\n", "258\n", "399\n", "155\n", "819\n"]}
169
72
coding
Solve the programming task below in a Python markdown code block. ```if-not:swift Write simple .camelCase method (`camel_case` function in PHP, `CamelCase` in C# or `camelCase` in Java) for strings. All words must have their first letter capitalized without spaces. ``` ```if:swift Write a simple `camelCase` function for strings. All words must have their first letter capitalized and all spaces removed. ``` For instance: ```python camelcase("hello case") => HelloCase camelcase("camel case word") => CamelCaseWord ``` ```c# using Kata; "hello case".CamelCase(); // => "HelloCase" "camel case word".CamelCase(); // => "CamelCaseWord" ``` Don't forget to rate this kata! Thanks :) Also feel free to reuse/extend the following starter code: ```python def camel_case(string): ```
{"functional": "_inputs = [['test case'], ['camel case method'], ['say hello '], [' camel case word'], ['']]\n_outputs = [['TestCase'], ['CamelCaseMethod'], ['SayHello'], ['CamelCaseWord'], ['']]\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(camel_case(*i), o[0])"}
196
191
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an integer array nums. The unique elements of an array are the elements that appear exactly once in the array. Return the sum of all the unique elements of nums.   Please complete the following python code precisely: ```python class Solution: def sumOfUnique(self, nums: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [1,2,3,2]) == 4\n assert candidate(nums = [1,1,1,1,1]) == 0\n assert candidate(nums = [1,2,3,4,5]) == 15\n\n\ncheck(Solution().sumOfUnique)"}
85
79
coding
Solve the programming task below in a Python markdown code block. E869120 found a chest which is likely to contain treasure. However, the chest is locked. In order to open it, he needs to enter a string S consisting of lowercase English letters. He also found a string S', which turns out to be the string S with some of its letters (possibly all or none) replaced with ?. One more thing he found is a sheet of paper with the following facts written on it: - Condition 1: The string S contains a string T as a contiguous substring. - Condition 2: S is the lexicographically smallest string among the ones that satisfy Condition 1. Print the string S. If such a string does not exist, print UNRESTORABLE. -----Constraints----- - 1 \leq |S'|, |T| \leq 50 - S' consists of lowercase English letters and ?. - T consists of lowercase English letters. -----Input----- Input is given from Standard Input in the following format: S T' -----Output----- Print the string S. If such a string does not exist, print UNRESTORABLE instead. -----Sample Input----- ?tc???? coder -----Sample Output----- atcoder There are 26 strings that satisfy Condition 1: atcoder, btcoder, ctcoder,..., ztcoder. Among them, the lexicographically smallest is atcoder, so we can say S = atcoder.
{"inputs": [">q???d?>\n_ba", "d>>>???q\na`a", "d>>>???q\naaa", "??p??d??\nabb", "??p??d??\n`bb", "??p??d??\n`ab", "??p??d??\nba`", "??p??d??\nb``"], "outputs": [">q_bada>\n", "d>>>a`aq\n", "d>>>aaaq\n", "UNRESTORABLE\n", "UNRESTORABLE\n", "UNRESTORABLE\n", "UNRESTORABLE\n", "UNRESTORABLE\n"]}
313
146
coding
Solve the programming task below in a Python markdown code block. Iahub and Iahubina went to a picnic in a forest full of trees. Less than 5 minutes passed before Iahub remembered of trees from programming. Moreover, he invented a new problem and Iahubina has to solve it, otherwise Iahub won't give her the food. Iahub asks Iahubina: can you build a rooted tree, such that each internal node (a node with at least one son) has at least two sons; node i has c_{i} nodes in its subtree? Iahubina has to guess the tree. Being a smart girl, she realized that it's possible no tree can follow Iahub's restrictions. In this way, Iahub will eat all the food. You need to help Iahubina: determine if there's at least one tree following Iahub's restrictions. The required tree must contain n nodes. -----Input----- The first line of the input contains integer n (1 ≤ n ≤ 24). Next line contains n positive integers: the i-th number represents c_{i} (1 ≤ c_{i} ≤ n). -----Output----- Output on the first line "YES" (without quotes) if there exist at least one tree following Iahub's restrictions, otherwise output "NO" (without quotes). -----Examples----- Input 4 1 1 1 4 Output YES Input 5 1 1 5 2 1 Output NO
{"inputs": ["1\n1\n", "1\n1\n", "2\n1 2\n", "2\n1 2\n", "2\n2 2\n", "4\n1 1 1 4\n", "4\n1 1 1 3\n", "4\n1 1 1 3\n"], "outputs": ["YES", "YES\n", "NO", "NO\n", "NO\n", "YES", "NO", "NO\n"]}
327
106
coding
Solve the programming task below in a Python markdown code block. There are two small spaceship, surrounded by two groups of enemy larger spaceships. The space is a two-dimensional plane, and one group of the enemy spaceships is positioned in such a way that they all have integer y-coordinates, and their x-coordinate is equal to -100, while the second group is positioned in such a way that they all have integer y-coordinates, and their x-coordinate is equal to 100. Each spaceship in both groups will simultaneously shoot two laser shots (infinite ray that destroys any spaceship it touches), one towards each of the small spaceships, all at the same time. The small spaceships will be able to avoid all the laser shots, and now want to position themselves at some locations with x=0 (with not necessarily integer y-coordinates), such that the rays shot at them would destroy as many of the enemy spaceships as possible. Find the largest numbers of spaceships that can be destroyed this way, assuming that the enemy spaceships can't avoid laser shots. Input The first line contains two integers n and m (1 ≤ n, m ≤ 60), the number of enemy spaceships with x = -100 and the number of enemy spaceships with x = 100, respectively. The second line contains n integers y_{1,1}, y_{1,2}, …, y_{1,n} (|y_{1,i}| ≤ 10 000) — the y-coordinates of the spaceships in the first group. The third line contains m integers y_{2,1}, y_{2,2}, …, y_{2,m} (|y_{2,i}| ≤ 10 000) — the y-coordinates of the spaceships in the second group. The y coordinates are not guaranteed to be unique, even within a group. Output Print a single integer – the largest number of enemy spaceships that can be destroyed. Examples Input 3 9 1 2 3 1 2 3 7 8 9 11 12 13 Output 9 Input 5 5 1 2 3 4 5 1 2 3 4 5 Output 10 Note In the first example the first spaceship can be positioned at (0, 2), and the second – at (0, 7). This way all the enemy spaceships in the first group and 6 out of 9 spaceships in the second group will be destroyed. In the second example the first spaceship can be positioned at (0, 3), and the second can be positioned anywhere, it will be sufficient to destroy all the enemy spaceships.
{"inputs": ["1 1\n0\n0\n", "1 1\n1\n1\n", "1 1\n0\n1\n", "1 1\n5\n5\n", "1 1\n0\n-1\n", "2 2\n0 2\n0 1\n", "1 5\n1\n1 2 3 4 5\n", "3 3\n0 0 0\n0 0 0\n"], "outputs": ["2\n", "2\n", "2\n", "2\n", "2\n", "4\n", "3\n", "6\n"]}
582
139
coding
Solve the programming task below in a Python markdown code block. Given are an integer K and integers a_1,\dots, a_K. Determine whether a sequence P satisfying below exists. If it exists, find the lexicographically smallest such sequence. * Every term in P is an integer between 1 and K (inclusive). * For each i=1,\dots, K, P contains a_i occurrences of i. * For each term in P, there is a contiguous subsequence of length K that contains that term and is a permutation of 1,\dots, K. Constraints * 1 \leq K \leq 100 * 1 \leq a_i \leq 1000 \quad (1\leq i\leq K) * a_1 + \dots + a_K\leq 1000 * All values in input are integers. Input Input is given from Standard Input in the following format: K a_1 a_2 \dots a_K Output If there is no sequence satisfying the conditions, print `-1`. Otherwise, print the lexicographically smallest sequence satisfying the conditions. Examples Input 3 2 4 3 Output 2 1 3 2 2 3 1 2 3 Input 4 3 2 3 2 Output 1 2 3 4 1 3 1 2 4 3 Input 5 3 1 4 1 5 Output -1
{"inputs": ["3\n1 1 1", "3\n1 1 2", "3\n1 2 2", "3\n2 1 2", "3\n2 1 1", "3\n2 2 4", "3\n2 4 4", "3\n2 2 1"], "outputs": ["1 2 3 \n", "3 1 2 3 \n", "2 3 1 2 3 \n", "1 3 2 1 3 \n", "1 2 3 1 \n", "3 1 2 3 3 1 2 3 \n", "2 3 1 2 3 2 3 1 2 3 \n", "1 2 3 1 2 \n"]}
329
190
coding
Solve the programming task below in a Python markdown code block. Bear Limak wants to become the largest of bears, or at least to become larger than his brother Bob. Right now, Limak and Bob weigh a and b respectively. It's guaranteed that Limak's weight is smaller than or equal to his brother's weight. Limak eats a lot and his weight is tripled after every year, while Bob's weight is doubled after every year. After how many full years will Limak become strictly larger (strictly heavier) than Bob? -----Input----- The only line of the input contains two integers a and b (1 ≤ a ≤ b ≤ 10) — the weight of Limak and the weight of Bob respectively. -----Output----- Print one integer, denoting the integer number of years after which Limak will become strictly larger than Bob. -----Examples----- Input 4 7 Output 2 Input 4 9 Output 3 Input 1 1 Output 1 -----Note----- In the first sample, Limak weighs 4 and Bob weighs 7 initially. After one year their weights are 4·3 = 12 and 7·2 = 14 respectively (one weight is tripled while the other one is doubled). Limak isn't larger than Bob yet. After the second year weights are 36 and 28, so the first weight is greater than the second one. Limak became larger than Bob after two years so you should print 2. In the second sample, Limak's and Bob's weights in next years are: 12 and 18, then 36 and 36, and finally 108 and 72 (after three years). The answer is 3. Remember that Limak wants to be larger than Bob and he won't be satisfied with equal weights. In the third sample, Limak becomes larger than Bob after the first year. Their weights will be 3 and 2 then.
{"inputs": ["4 7\n", "4 9\n", "1 1\n", "4 6\n", "1 1\n", "1 2\n", "1 3\n", "1 4\n"], "outputs": ["2\n", "3\n", "1\n", "2\n", "1\n", "2\n", "3\n", "4\n"]}
418
86
coding
Solve the programming task below in a Python markdown code block. Write a function `count_vowels` to count the number of vowels in a given string. ### Notes: - Return `nil` or `None` for non-string inputs. - Return `0` if the parameter is omitted. ### Examples: ```python count_vowels("abcdefg") => 2 count_vowels("aAbcdeEfg") => 4 count_vowels(12) => None ``` Also feel free to reuse/extend the following starter code: ```python def count_vowels(s = ''): ```
{"functional": "_inputs = [['abcdefg'], ['asdfdsafdsafds'], [''], ['asdfdsaf asdfsdaf 13243242 dsafdsafds'], ['aeiouAEIOU'], ['1213'], ['12 3'], ['ewqriwetruituofdsajflsd23423r5043'], ['asdfviosdfopsiewrwwer asdf asdfdsaf)(asdflkajdsf '], ['asdfdsaf asdfsasdfdsafgjlsdjf asdfdsf daf 13243242 dsafdsafds'], ['aeiouAEdsafdsf asdfw2434&***OU'], [12], [[]], [{}]]\n_outputs = [[2], [3], [0], [6], [10], [0], [0], [8], [12], [9], [11], [None], [None], [None]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(count_vowels(*i), o[0])"}
133
375
coding
Solve the programming task below in a Python markdown code block. The working hours of Chef’s kitchen are from X pm to Y pm (1 ≤ X < Y ≤ 12). Find the number of hours Chef works. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two space-separated integers X and Y — the starting and ending time of working hours respectively. ------ Output Format ------ For each test case, output on a new line, the number of hours Chef works. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ X < Y ≤ 12$ ----- Sample Input 1 ------ 4 1 2 3 7 9 11 2 10 ----- Sample Output 1 ------ 1 4 2 8 ----- explanation 1 ------ Test case $1$: Chef starts working at $1$ pm and works till $2$ pm. Thus, he works for $1$ hour. Test case $2$: Chef starts working at $3$ pm and works till $7$ pm. Thus, he works for $4$ hours. Test case $3$: Chef starts working at $9$ pm and works till $11$ pm. Thus, he works for $2$ hours. Test case $4$: Chef starts working at $2$ pm and works till $10$ pm. Thus, he works for $8$ hours.
{"inputs": ["4\n1 2\n3 7\n9 11\n2 10\n"], "outputs": ["1\n4\n2\n8\n"]}
319
38
coding
Solve the programming task below in a Python markdown code block. Given the coordinates (x, y) of a point in 2-D plane. Find if it is possible to reach (x, y) from (0, 0). The only possible moves from any coordinate (i, j) are as follows: Go to the point with coordinates (i + 1, j + 1). Go to the point with coordinates (i + 1, j - 1) Go to the point with coordinates (i - 1, j + 1). Go to the point with coordinates (i - 1, j - 1). ------ Input Format ------ - First line will contain T, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, two integers x, y. ------ Output Format ------ For each test case, print YES if it is possible to reach (x, y) from (0, 0), otherwise print NO. 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 ≤ 2\cdot10^{4}$ $-10^{9} ≤ x, y ≤ 10^{9}$ ----- Sample Input 1 ------ 6 0 2 1 2 -1 -3 -1 0 -3 1 2 -1 ----- Sample Output 1 ------ YES NO YES NO YES NO ----- explanation 1 ------ Test case $1$: A valid sequence of moves can be: $\;(0, 0) \rightarrow (1, 1) \rightarrow (0, 2)$. Test case $2$: There is no possible way to reach the point $(1, 2)$ from $(0, 0)$. Test case $3$: A valid sequence of moves can be: $\;(0, 0) \rightarrow (-1, -1) \rightarrow (0, -2) \rightarrow (-1, -3)$.
{"inputs": ["6\n0 2\n1 2\n-1 -3\n-1 0\n-3 1\n2 -1\n"], "outputs": ["YES\nNO\nYES\nNO\nYES\nNO\n"]}
457
51
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese and Russian. Mike likes to invent new functions. The latest one he has invented is called SuperFunction. Let's consider how it can be calculated: You are given two integers N and K. SuperFunction of N and K equals to the sum of K'th powers of the positive numbers, which are coprime with N and also not greater than N. I.e. SuperFuntion of 6 and 3 equals to 1^{3} + 5^{3} = 126. Mike knows how to calculate SuperFunction in case N and K are rather small integers. Now he wants you to write him a solution, which can calculate SuperFuntion for bigger constraints. As far as SuperFunction could be extremely huge, you are asked to do all the calculation under modulo M. Help Mike! ------ Input ------ The first line contains three integers N, K and M. ------ Output ------ The first line of contains the only integer, denoting SuperFuntion of N and K under modulo M. ------ Example ------ Input: 5 2 100 Output: 30 ------ Explanation ------ In the test case N equals to 5, K equals to 2, M equals to 100. The answer is (1^{2} + 2^{2} + 3^{2} + 4^{2}) mod 100 = 30. ------ Scoring ------ Subtask 1 (12 points): 1 ≤ N ≤ 100, 1 ≤ K ≤ 2, 1 ≤ M ≤ 40000; Subtask 2 (28 points): 1 ≤ N ≤ 1 000 000, 1 ≤ K ≤ 2, 1 ≤ M ≤ 10^{9}; Subtask 3 (30 points): 1 ≤ N ≤ 1 000 000 000 000, 1 ≤ K ≤ 3, 1 ≤ M ≤ 10^{9}; Subtask 4 (30 points): 1 ≤ N ≤ 1 000 000 000 000, 1 ≤ K ≤ 10, 1 ≤ M ≤ 10^{18}.
{"inputs": ["5 2 100"], "outputs": ["30"]}
509
19
coding
Solve the programming task below in a Python markdown code block. bhargav has dream to study his MS is foreign country. So, He started his preparation for GRE. There are huge number of words that he has to prepare for. So, firs he wanted to group all synonyms and antonyms. As there are huge number of words that he has to group. So, he asked help to group all synonyms and antonyms of a word. it is called antonym if the first letter of a current number is 3 steps upper than reference then it is antonym if the first letter of the reference number is same as the current first letter of a word then it is synonym. and the length should be same for the antonym and synonym INPUT: first line should contain a word W. next line contains no of words N. next N lines consists of words that are to be grouped. OUTPUT print no of synonyms S followed by antonyms A. SAMPLE INPUT hasrfk 4 heyfgs jhajyrbf kayufs hetinf SAMPLE OUTPUT 2 1
{"inputs": ["ashtrick\n25\namberhed\ndouglasb\nashmartin\nroogle\neglownasht\ndutterwo\naverists\ndueglors\naeufahef\neutifhre\naedsrntf\naleifksc\nalejfisj\neshmarin\naksiefjs\ndharanis\nqueofjsl\ndileeeep\nprashant\naravinds\nakhilesh\ndheufncs\najeufjvjs\naeuigvsr\naskhtakuen"], "outputs": ["10 6"]}
227
138
coding
Solve the programming task below in a Python markdown code block. Little penguin Polo loves his home village. The village has n houses, indexed by integers from 1 to n. Each house has a plaque containing an integer, the i-th house has a plaque containing integer pi (1 ≤ pi ≤ n). Little penguin Polo loves walking around this village. The walk looks like that. First he stands by a house number x. Then he goes to the house whose number is written on the plaque of house x (that is, to house px), then he goes to the house whose number is written on the plaque of house px (that is, to house ppx), and so on. We know that: 1. When the penguin starts walking from any house indexed from 1 to k, inclusive, he can walk to house number 1. 2. When the penguin starts walking from any house indexed from k + 1 to n, inclusive, he definitely cannot walk to house number 1. 3. When the penguin starts walking from house number 1, he can get back to house number 1 after some non-zero number of walks from a house to a house. You need to find the number of ways you may write the numbers on the houses' plaques so as to fulfill the three above described conditions. Print the remainder after dividing this number by 1000000007 (109 + 7). Input The single line contains two space-separated integers n and k (1 ≤ n ≤ 1000, 1 ≤ k ≤ min(8, n)) — the number of the houses and the number k from the statement. Output In a single line print a single integer — the answer to the problem modulo 1000000007 (109 + 7). Examples Input 5 2 Output 54 Input 7 4 Output 1728
{"inputs": ["8 5\n", "9 8\n", "2 2\n", "2 1\n", "8 8\n", "1 1\n", "3 3\n", "8 1\n"], "outputs": ["16875\n", "2097152\n", "2\n", "1\n", "2097152\n", "1\n", "9\n", "823543\n"]}
418
107
coding
Solve the programming task below in a Python markdown code block. Sonya decided to organize an exhibition of flowers. Since the girl likes only roses and lilies, she decided that only these two kinds of flowers should be in this exhibition. There are $n$ flowers in a row in the exhibition. Sonya can put either a rose or a lily in the $i$-th position. Thus each of $n$ positions should contain exactly one flower: a rose or a lily. She knows that exactly $m$ people will visit this exhibition. The $i$-th visitor will visit all flowers from $l_i$ to $r_i$ inclusive. The girl knows that each segment has its own beauty that is equal to the product of the number of roses and the number of lilies. Sonya wants her exhibition to be liked by a lot of people. That is why she wants to put the flowers in such way that the sum of beauties of all segments would be maximum possible. -----Input----- The first line contains two integers $n$ and $m$ ($1\leq n, m\leq 10^3$) — the number of flowers and visitors respectively. Each of the next $m$ lines contains two integers $l_i$ and $r_i$ ($1\leq l_i\leq r_i\leq n$), meaning that $i$-th visitor will visit all flowers from $l_i$ to $r_i$ inclusive. -----Output----- Print the string of $n$ characters. The $i$-th symbol should be «0» if you want to put a rose in the $i$-th position, otherwise «1» if you want to put a lily. If there are multiple answers, print any. -----Examples----- Input 5 3 1 3 2 4 2 5 Output 01100 Input 6 3 5 6 1 4 4 6 Output 110010 -----Note----- In the first example, Sonya can put roses in the first, fourth, and fifth positions, and lilies in the second and third positions; in the segment $[1\ldots3]$, there are one rose and two lilies, so the beauty is equal to $1\cdot 2=2$; in the segment $[2\ldots4]$, there are one rose and two lilies, so the beauty is equal to $1\cdot 2=2$; in the segment $[2\ldots5]$, there are two roses and two lilies, so the beauty is equal to $2\cdot 2=4$. The total beauty is equal to $2+2+4=8$. In the second example, Sonya can put roses in the third, fourth, and sixth positions, and lilies in the first, second, and fifth positions; in the segment $[5\ldots6]$, there are one rose and one lily, so the beauty is equal to $1\cdot 1=1$; in the segment $[1\ldots4]$, there are two roses and two lilies, so the beauty is equal to $2\cdot 2=4$; in the segment $[4\ldots6]$, there are two roses and one lily, so the beauty is equal to $2\cdot 1=2$. The total beauty is equal to $1+4+2=7$.
{"inputs": ["1 1\n1 1\n", "2 1\n1 2\n", "1 1\n1 1\n", "2 1\n1 2\n", "2 1\n1 1\n", "1 1\n2 1\n", "6 2\n1 6\n1 4\n", "6 2\n1 6\n1 4\n"], "outputs": ["0\n", "01\n", "0", "01", "01\n", "0\n", "010101\n", "010101"]}
754
136
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given two integer arrays nums1 and nums2. We write the integers of nums1 and nums2 (in the order they are given) on two separate horizontal lines. We may draw connecting lines: a straight line connecting two numbers nums1[i] and nums2[j] such that: nums1[i] == nums2[j], and the line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting line cannot intersect even at the endpoints (i.e., each number can only belong to one connecting line). Return the maximum number of connecting lines we can draw in this way.   Please complete the following python code precisely: ```python class Solution: def maxUncrossedLines(self, nums1: List[int], nums2: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums1 = [1,4,2], nums2 = [1,2,4]) == 2\n assert candidate(nums1 = [2,5,1,2,5], nums2 = [10,5,2,1,5,2]) == 3\n assert candidate(nums1 = [1,3,7,1,7,5], nums2 = [1,9,2,5,1]) == 2\n\n\ncheck(Solution().maxUncrossedLines)"}
184
124
coding
Solve the programming task below in a Python markdown code block. Slava plays his favorite game "Peace Lightning". Now he is flying a bomber on a very specific map. Formally, map is a checkered field of size 1 × n, the cells of which are numbered from 1 to n, in each cell there can be one or several tanks. Slava doesn't know the number of tanks and their positions, because he flies very high, but he can drop a bomb in any cell. All tanks in this cell will be damaged. If a tank takes damage for the first time, it instantly moves to one of the neighboring cells (a tank in the cell n can only move to the cell n - 1, a tank in the cell 1 can only move to the cell 2). If a tank takes damage for the second time, it's counted as destroyed and never moves again. The tanks move only when they are damaged for the first time, they do not move by themselves. Help Slava to destroy all tanks using as few bombs as possible. -----Input----- The first line contains a single integer n (2 ≤ n ≤ 100 000) — the size of the map. -----Output----- In the first line print m — the minimum number of bombs Slava needs to destroy all tanks. In the second line print m integers k_1, k_2, ..., k_{m}. The number k_{i} means that the i-th bomb should be dropped at the cell k_{i}. If there are multiple answers, you can print any of them. -----Examples----- Input 2 Output 3 2 1 2 Input 3 Output 4 2 1 3 2
{"inputs": ["2\n", "3\n", "4\n", "6\n", "5\n", "4\n", "6\n", "5\n"], "outputs": ["3\n2 1 2 ", "4\n2 1 3 2 ", "6\n2 4 1 3 2 4 ", "9\n2 4 6 1 3 5 2 4 6 ", "7\n2 4 1 3 5 2 4 ", "6\n2 4 1 3 2 4\n", "9\n2 4 6 1 3 5 2 4 6\n", "7\n2 4 1 3 5 2 4\n"]}
362
167
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You have n computers. You are given the integer n and a 0-indexed integer array batteries where the ith battery can run a computer for batteries[i] minutes. You are interested in running all n computers simultaneously using the given batteries. Initially, you can insert at most one battery into each computer. After that and at any integer time moment, you can remove a battery from a computer and insert another battery any number of times. The inserted battery can be a totally new battery or a battery from another computer. You may assume that the removing and inserting processes take no time. Note that the batteries cannot be recharged. Return the maximum number of minutes you can run all the n computers simultaneously.   Please complete the following python code precisely: ```python class Solution: def maxRunTime(self, n: int, batteries: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(n = 2, batteries = [3,3,3]) == 4\n assert candidate(n = 2, batteries = [1,1,1,1]) == 2\n\n\ncheck(Solution().maxRunTime)"}
193
64
coding
Solve the programming task below in a Python markdown code block. Chef has three baskets and two of them have multiple balls(Natural numbers written on them). The first basket has N balls, the second basket has M balls and the third basket is empty. Chef starts choosing all the unique balls(only occurring once in both the baskets) and puts into the third basket and throw the repetitive ones. Print numbers on the balls of the third basket in ascending order. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains three lines of input. - First-line has two integers $N, M$. - Second-line with N space-separated numbers. - Third-line with M space-separated numbers. -----Output:----- For each testcase, output in a single line answer. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^5$ - $1 \leq M \leq 10^5$ -----Sample Input:----- 1 4 5 1 2 3 4 2 3 4 5 6 -----Sample Output:----- 1 5 6
{"inputs": ["1\n4 5\n1 2 3 4\n2 3 4 5 6"], "outputs": ["1 5 6"]}
259
38
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 of length n. nums contains a valid split at index i if the following are true: The sum of the first i + 1 elements is greater than or equal to the sum of the last n - i - 1 elements. There is at least one element to the right of i. That is, 0 <= i < n - 1. Return the number of valid splits in nums.   Please complete the following python code precisely: ```python class Solution: def waysToSplitArray(self, nums: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [10,4,-8,7]) == 2\n assert candidate(nums = [2,3,1,0]) == 2\n\n\ncheck(Solution().waysToSplitArray)"}
142
58
coding
Solve the programming task below in a Python markdown code block. Consider the infinite sequence s of positive integers, created by repeating the following steps: 1. Find the lexicographically smallest triple of positive integers (a, b, c) such that * a ⊕ b ⊕ c = 0, where ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR). * a, b, c are not in s. Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2]. 2. Append a, b, c to s in this order. 3. Go back to the first step. You have integer n. Find the n-th element of s. You have to answer t independent test cases. A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b. Input The first line contains a single integer t (1 ≤ t ≤ 10^5) — the number of test cases. Each of the next t lines contains a single integer n (1≤ n ≤ 10^{16}) — the position of the element you want to know. Output In each of the t lines, output the answer to the corresponding test case. Example Input 9 1 2 3 4 5 6 7 8 9 Output 1 2 3 4 8 12 5 10 15 Note The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
{"inputs": ["9\n2\n2\n3\n4\n5\n6\n7\n8\n9\n", "9\n2\n2\n3\n4\n9\n6\n7\n8\n9\n", "9\n1\n2\n3\n4\n5\n6\n7\n8\n9\n", "9\n2\n2\n3\n4\n5\n6\n7\n8\n17\n", "9\n3\n2\n3\n4\n5\n9\n15\n8\n7\n", "9\n1\n2\n3\n4\n5\n12\n7\n8\n9\n", "9\n3\n2\n3\n4\n9\n9\n15\n8\n7\n", "9\n2\n2\n3\n4\n5\n12\n7\n8\n9\n"], "outputs": ["2\n2\n3\n4\n8\n12\n5\n10\n15\n", "2\n2\n3\n4\n15\n12\n5\n10\n15\n", "1\n2\n3\n4\n8\n12\n5\n10\n15\n", "2\n2\n3\n4\n8\n12\n5\n10\n32\n", "3\n2\n3\n4\n8\n15\n14\n10\n5\n", "1\n2\n3\n4\n8\n13\n5\n10\n15\n", "3\n2\n3\n4\n15\n15\n14\n10\n5\n", "2\n2\n3\n4\n8\n13\n5\n10\n15\n"]}
435
373
coding
Solve the programming task below in a Python markdown code block. Take 2 strings `s1` and `s2` including only letters from `a`to `z`. Return a new **sorted** string, the longest possible, containing distinct letters, - each taken only once - coming from s1 or s2. # Examples: ``` a = "xyaabbbccccdefww" b = "xxxxyyyyabklmopq" longest(a, b) -> "abcdefklmopqwxy" a = "abcdefghijklmnopqrstuvwxyz" longest(a, a) -> "abcdefghijklmnopqrstuvwxyz" ``` Also feel free to reuse/extend the following starter code: ```python def longest(s1, s2): ```
{"functional": "_inputs = [['aretheyhere', 'yestheyarehere'], ['loopingisfunbutdangerous', 'lessdangerousthancoding'], ['inmanylanguages', 'theresapairoffunctions'], ['lordsofthefallen', 'gamekult'], ['codewars', 'codewars'], ['agenerationmustconfrontthelooming', 'codewarrs']]\n_outputs = [['aehrsty'], ['abcdefghilnoprstu'], ['acefghilmnoprstuy'], ['adefghklmnorstu'], ['acdeorsw'], ['acdefghilmnorstuw']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(longest(*i), o[0])"}
150
278
coding
Solve the programming task below in a Python markdown code block. Polycarp had an array $a$ of $3$ positive integers. He wrote out the sums of all non-empty subsequences of this array, sorted them in non-decreasing order, and got an array $b$ of $7$ integers. For example, if $a = \{1, 4, 3\}$, then Polycarp wrote out $1$, $4$, $3$, $1 + 4 = 5$, $1 + 3 = 4$, $4 + 3 = 7$, $1 + 4 + 3 = 8$. After sorting, he got an array $b = \{1, 3, 4, 4, 5, 7, 8\}.$ Unfortunately, Polycarp lost the array $a$. He only has the array $b$ left. Help him to restore the array $a$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 5000$) — the number of test cases. Each test case consists of one line which contains $7$ integers $b_1, b_2, \dots, b_7$ ($1 \le b_i \le 10^9$; $b_i \le b_{i+1}$). Additional constraint on the input: there exists at least one array $a$ which yields this array $b$ as described in the statement. -----Output----- For each test case, print $3$ integers — $a_1$, $a_2$ and $a_3$. If there can be several answers, print any of them. -----Examples----- Input 5 1 3 4 4 5 7 8 1 2 3 4 5 6 7 300000000 300000000 300000000 600000000 600000000 600000000 900000000 1 1 2 999999998 999999999 999999999 1000000000 1 2 2 3 3 4 5 Output 1 4 3 4 1 2 300000000 300000000 300000000 999999998 1 1 1 2 2 -----Note----- The subsequence of the array $a$ is a sequence that can be obtained from $a$ by removing zero or more of its elements. Two subsequences are considered different if index sets of elements included in them are different. That is, the values of the elements don't matter in the comparison of subsequences. In particular, any array of length $3$ has exactly $7$ different non-empty subsequences.
{"inputs": ["1\n1 2 3 69696875 69696876 69696877 69696878\n", "1\n1 1 2 69696880 69696881 69696881 69696882\n", "1\n1 2 3 69696887 69696888 69696889 69696890\n", "1\n1 1 2 69696883 69696884 69696884 69696885\n", "1\n1 1 2 69696884 69696885 69696885 69696886\n", "1\n1 1 2 69696880 69696881 69696881 69696882\n", "1\n1 2 3 69696875 69696876 69696877 69696878\n", "1\n1 1 2 69696884 69696885 69696885 69696886\n"], "outputs": ["1 2 69696875\n", "1 1 69696880\n", "1 2 69696887\n", "1 1 69696883\n", "1 1 69696884\n", "1 1 69696880\n", "1 2 69696875\n", "1 1 69696884\n"]}
681
494
coding
Solve the programming task below in a Python markdown code block. Alas, finding one's true love is not easy. Masha has been unsuccessful in that yet. Her friend Dasha told Masha about a way to determine the phone number of one's Prince Charming through arithmancy. The phone number is divined like that. First one needs to write down one's own phone numbers. For example, let's suppose that Masha's phone number is 12345. After that one should write her favorite digit from 0 to 9 under the first digit of her number. That will be the first digit of the needed number. For example, Masha's favorite digit is 9. The second digit is determined as a half sum of the second digit of Masha's number and the already written down first digit from her beloved one's number. In this case the arithmetic average equals to (2 + 9) / 2 = 5.5. Masha can round the number up or down, depending on her wishes. For example, she chooses the digit 5. Having written down the resulting digit under the second digit of her number, Masha moves to finding the third digit in the same way, i.e. finding the half sum the the third digit of her number and the second digit of the new number. The result is (5 + 3) / 2 = 4. In this case the answer is unique. Thus, every i-th digit is determined as an arithmetic average of the i-th digit of Masha's number and the i - 1-th digit of her true love's number. If needed, the digit can be rounded up or down. For example, Masha can get: 12345 95444 Unfortunately, when Masha tried dialing the number, she got disappointed: as it turned out, the number was unavailable or outside the coverage area. But Masha won't give up. Perhaps, she rounded to a wrong digit or chose the first digit badly. That's why she keeps finding more and more new numbers and calling them. Count the number of numbers Masha calls. Masha calls all the possible numbers that can be found by the described means of arithmancy, except for, perhaps, her own one. Input The first line contains nonempty sequence consisting of digits from 0 to 9 — Masha's phone number. The sequence length does not exceed 50. Output Output the single number — the number of phone numbers Masha will dial. Examples Input 12345 Output 48 Input 09 Output 15
{"inputs": ["3\n", "2\n", "0\n", "1\n", "4\n", "55\n", "15\n", "09\n"], "outputs": ["9\n", "9\n", "9\n", "9\n", "9\n", "14\n", "15\n", "15\n"]}
554
76
coding
Solve the programming task below in a Python markdown code block. Chef is playing a game which contains a binary string. He can perform one operation only: - Toggle the i_th bit of the string (0 to 1 or 1 to 0) By performing operations on the string (can be zero times), you have to convert the string with no adjacent bit being the same. Can you help chef ? -----Input:----- - First line will contain $T$, number of test cases. Then the test cases follow. - First line of each test case, contains the size of the string $N$ - Seond line contains a single line of input, the binary string. -----Output:----- For each testcase, output in a single line answer - the minimum operations. -----Constraints----- - $1 \leq T \leq 100$ - $2 \leq |S| \leq 10^3$ -----Sample Input:----- 1 4 1011 -----Sample Output:----- 1
{"inputs": ["1\n4\n1011"], "outputs": ["1"]}
217
19
coding
Solve the programming task below in a Python markdown code block. Alice likes word "nineteen" very much. She has a string s and wants the string to contain as many such words as possible. For that reason she can rearrange the letters of the string. For example, if she has string "xiineteenppnnnewtnee", she can get string "xnineteenppnineteenw", containing (the occurrences marked) two such words. More formally, word "nineteen" occurs in the string the number of times you can read it starting from some letter of the string. Of course, you shouldn't skip letters. Help her to find the maximum number of "nineteen"s that she can get in her string. -----Input----- The first line contains a non-empty string s, consisting only of lowercase English letters. The length of string s doesn't exceed 100. -----Output----- Print a single integer — the maximum number of "nineteen"s that she can get in her string. -----Examples----- Input nniinneetteeeenn Output 2 Input nneteenabcnneteenabcnneteenabcnneteenabcnneteenabcii Output 2 Input nineteenineteen Output 2
{"inputs": ["n\n", "n\n", "m\n", "ni\n", "ni\n", "in\n", "nine\n", "nine\n"], "outputs": ["0", "0\n", "0\n", "0", "0\n", "0\n", "0", "0\n"]}
271
67
coding
Solve the programming task below in a Python markdown code block. HackerLand University has the following grading policy: Every student receives a $\textit{grade}$ in the inclusive range from $0$ to $100$. Any $\textit{grade}$ less than $\boldsymbol{40}$ is a failing grade. Sam is a professor at the university and likes to round each student's $\textit{grade}$ according to these rules: If the difference between the $\textit{grade}$ and the next multiple of $5$ is less than $3$, round $\textit{grade}$ up to the next multiple of $5$. If the value of $\textit{grade}$ is less than $38$, no rounding occurs as the result will still be a failing grade. Examples $grade=84$ round to $85$ (85 - 84 is less than 3) $grade=29$ do not round (result is less than 40) $grade=57$ do not round (60 - 57 is 3 or higher) Given the initial value of $\textit{grade}$ for each of Sam's $n$ students, write code to automate the rounding process. Function Description Complete the function gradingStudents in the editor below. gradingStudents has the following parameter(s): int grades[n]: the grades before rounding Returns int[n]: the grades after rounding as appropriate Input Format The first line contains a single integer, $n$, the number of students. Each line $\boldsymbol{i}$ of the $n$ subsequent lines contains a single integer, $grades[i]$. Constraints $1\leq n\leq60$ $0\leq\textit{grades}[i]\leq100$ Sample Input 0 4 73 67 38 33 Sample Output 0 75 67 40 33 Explanation 0 Student $1$ received a $73$, and the next multiple of $5$ from $73$ is $75$. Since $75-73<3$, the student's grade is rounded to $75$. Student $2$ received a $67$, and the next multiple of $5$ from $67$ is $70$. Since $70-67=3$, the grade will not be modified and the student's final grade is $67$. Student $3$ received a $38$, and the next multiple of $5$ from $38$ is $\boldsymbol{40}$. Since $40-38<3$, the student's grade will be rounded to $\boldsymbol{40}$. Student $4$ received a grade below $33$, so the grade will not be modified and the student's final grade is $33$.
{"inputs": ["4\n73\n67\n38\n33\n"], "outputs": ["75\n67\n40\n33\n"]}
623
36
coding
Solve the programming task below in a Python markdown code block. Congratulations! That Special Someone has given you their phone number. But WAIT, is it a valid number? Your task is to write a function that verifies whether a given string contains a valid British mobile (cell) phone number or not. If valid, return 'In with a chance'. If invalid, or if you're given an empty string, return 'Plenty more fish in the sea'. A number can be valid in the following ways: Here in the UK mobile numbers begin with '07' followed by 9 other digits, e.g. '07454876120'. Sometimes the number is preceded by the country code, the prefix '+44', which replaces the '0' in ‘07’, e.g. '+447454876120'. And sometimes you will find numbers with dashes in-between digits or on either side, e.g. '+44--745---487-6120' or '-074-54-87-61-20-'. As you can see, dashes may be consecutive. Good Luck Romeo/Juliette! Also feel free to reuse/extend the following starter code: ```python def validate_number(s): ```
{"functional": "_inputs = [['07454876120'], ['0754876120'], ['0745--487-61-20'], ['+447535514555'], ['-07599-51-4555'], ['075335440555'], ['+337535512555'], ['00535514555'], ['+447+4435512555'], ['+44']]\n_outputs = [['In with a chance'], ['Plenty more fish in the sea'], ['In with a chance'], ['In with a chance'], ['In with a chance'], ['Plenty more fish in the sea'], ['Plenty more fish in the sea'], ['Plenty more fish in the sea'], ['Plenty more fish in the sea'], ['Plenty more fish in the sea']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(validate_number(*i), o[0])"}
279
361
coding
Solve the programming task below in a Python markdown code block. Write a function that checks the braces status in a string, and return `True` if all braces are properly closed, or `False` otherwise. Available types of brackets: `()`, `[]`, `{}`. **Please note, you need to write this function without using regex!** ## Examples ```python '([[some](){text}here]...)' => True '{([])}' => True '()[]{}()' => True '(...[]...{(..())}[abc]())' => True '1239(df){' => False '[()])' => False ')12[x]34(' => False ``` Don't forget to rate this kata! Thanks :) Also feel free to reuse/extend the following starter code: ```python def braces_status(s): ```
{"functional": "_inputs = [['[()]'], ['{[]}'], ['{[()]}'], ['([)]'], ['([[some](){text}here]...)'], ['}'], ['[()]]'], ['[()]{('], ['()[]{}()'], ['[[[[']]\n_outputs = [[True], [True], [True], [False], [True], [False], [False], [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(braces_status(*i), o[0])"}
198
232
coding
Solve the programming task below in a Python markdown code block. Michael is accused of violating the social distancing rules and creating a risk of spreading coronavirus. He is now sent to prison. Luckily, Michael knows exactly what the prison looks like from the inside, especially since it's very simple. The prison can be represented as a rectangle $a\times b$ which is divided into $ab$ cells, each representing a prison cell, common sides being the walls between cells, and sides on the perimeter being the walls leading to freedom. Before sentencing, Michael can ask his friends among the prison employees to make (very well hidden) holes in some of the walls (including walls between cells and the outermost walls). Michael wants to be able to get out of the prison after this, no matter which cell he is placed in. However, he also wants to break as few walls as possible. Your task is to find out the smallest number of walls to be broken so that there is a path to the outside from every cell after this. -----Input----- The first line contains a single integer $t$ ($1\leq t\leq 100$) — the number of test cases. Each of the following $t$ lines contains two integers $a$ and $b$ ($1\leq a, b\leq 100$), representing a corresponding test case. -----Output----- For each test case print the single integer on a separate line — the answer to the problem. -----Examples----- Input 2 2 2 1 3 Output 4 3 -----Note----- Some possible escape plans for the example test cases are shown below. Broken walls are shown in gray, not broken walls are shown in black.
{"inputs": ["2\n2 2\n1 3\n"], "outputs": ["4\n3\n"]}
358
24
coding
Solve the programming task below in a Python markdown code block. This is an easy version of the problem. In this version, all cards have the same color. Alice has $n$ cards, each card is white, and the cards are stacked in a deck. Alice deals the cards to herself and to Bob, dealing at once several cards from the top of the deck in the following order: one card to herself, two cards to Bob, three cards to Bob, four cards to herself, five cards to herself, six cards to Bob, seven cards to Bob, eight cards to herself, and so on. In other words, on the $i$-th step, Alice deals $i$ top cards from the deck to one of the players; on the first step, she deals the cards to herself and then alternates the players every two steps. When there aren't enough cards at some step, Alice deals all the remaining cards to the current player, and the process stops. First Alice's steps in a deck of many cards. How many cards will Alice and Bob have at the end? -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 200$). The description of the test cases follows The only line of each test case contains a single integer $n$ ($1 \le n \le 10^6$) — the number of cards. -----Output----- For each test case print two integers — the number of cards in the end for each player — in this order: cards Alice has, cards Bob has. -----Examples----- Input 5 10 6 17 8 1000000 Output 5 5 1 5 10 7 3 5 500202 499798 -----Note----- None
{"inputs": ["5\n10\n6\n17\n8\n1000000\n"], "outputs": ["5 5\n1 5\n10 7\n3 5\n500202 499798\n"]}
396
61
coding
Solve the programming task below in a Python markdown code block. Let's define the permutation of length n as an array p = [p1, p2, ..., pn] consisting of n distinct integers from range from 1 to n. We say that this permutation maps value 1 into the value p1, value 2 into the value p2 and so on. Kyota Ootori has just learned about cyclic representation of a permutation. A cycle is a sequence of numbers such that each element of this sequence is being mapped into the next element of this sequence (and the last element of the cycle is being mapped into the first element of the cycle). The cyclic representation is a representation of p as a collection of cycles forming p. For example, permutation p = [4, 1, 6, 2, 5, 3] has a cyclic representation that looks like (142)(36)(5) because 1 is replaced by 4, 4 is replaced by 2, 2 is replaced by 1, 3 and 6 are swapped, and 5 remains in place. Permutation may have several cyclic representations, so Kyoya defines the standard cyclic representation of a permutation as follows. First, reorder the elements within each cycle so the largest element is first. Then, reorder all of the cycles so they are sorted by their first element. For our example above, the standard cyclic representation of [4, 1, 6, 2, 5, 3] is (421)(5)(63). Now, Kyoya notices that if we drop the parenthesis in the standard cyclic representation, we get another permutation! For instance, [4, 1, 6, 2, 5, 3] will become [4, 2, 1, 5, 6, 3]. Kyoya notices that some permutations don't change after applying operation described above at all. He wrote all permutations of length n that do not change in a list in lexicographic order. Unfortunately, his friend Tamaki Suoh lost this list. Kyoya wishes to reproduce the list and he needs your help. Given the integers n and k, print the permutation that was k-th on Kyoya's list. Input The first line will contain two integers n, k (1 ≤ n ≤ 50, 1 ≤ k ≤ min{1018, l} where l is the length of the Kyoya's list). Output Print n space-separated integers, representing the permutation that is the answer for the question. Examples Input 4 3 Output 1 3 2 4 Input 10 1 Output 1 2 3 4 5 6 7 8 9 10 Note The standard cycle representation is (1)(32)(4), which after removing parenthesis gives us the original permutation. The first permutation on the list would be [1, 2, 3, 4], while the second permutation would be [1, 2, 4, 3].
{"inputs": ["2 2\n", "9 1\n", "4 2\n", "1 1\n", "5 8\n", "3 3\n", "5 1\n", "7 2\n"], "outputs": ["2 1\n", "1 2 3 4 5 6 7 8 9\n", "1 2 4 3\n", "1\n", "2 1 4 3 5\n", "2 1 3\n", "1 2 3 4 5\n", "1 2 3 4 5 7 6\n"]}
647
142
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,4,4,5,6,7] might become: [4,5,6,7,0,1,4] if it was rotated 4 times. [0,1,4,4,5,6,7] if it was rotated 7 times. Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]]. Given the sorted rotated array nums that may contain duplicates, return the minimum element of this array. You must decrease the overall operation steps as much as possible.   Please complete the following python code precisely: ```python class Solution: def findMin(self, nums: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [1,3,5]) == 1\n assert candidate(nums = [2,2,2,0,1]) == 0\n\n\ncheck(Solution().findMin)"}
226
55
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. Return the length of the shortest subarray to remove. A subarray is a contiguous subsequence of the array.   Please complete the following python code precisely: ```python class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(arr = [1,2,3,10,4,2,3,5]) == 3\n assert candidate(arr = [5,4,3,2,1]) == 4\n assert candidate(arr = [1,2,3]) == 0\n assert candidate(arr = [1]) == 0\n\n\ncheck(Solution().findLengthOfShortestSubarray)"}
105
99
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. Chef is a private detective. He was asked to investigate a case of murder in the city of Frangton. Chef arrived in Frangton to find out that the mafia was involved in the case. Chef spent some time watching for people that belong to the clan and was able to build a map of relationships between them. He knows that a mafia's organizational structure consists of a single Don, heading a hierarchical criminal organization. Each member reports exactly to one other member of the clan. It's obvious that there are no cycles in the reporting system of the mafia. There are N people in the clan, for simplicity indexed from 1 to N, and Chef knows who each of them report to. Member i reports to member R_{i}. Now, Chef needs to identfy all potential killers to continue his investigation. Having considerable knowledge about the mafia's activities, Chef knows that the killer must be a minor criminal, that is, one of the members who nobody reports to. Please find the list of potential killers for Chef. Since Don reports to nobody, his R_{i} will be equal to 0. ------ Input ------ The first line of input contains one integer N. Next line has N space-separated integers, the i^{th} integer denotes R_{i} — the person whom the i^{th} member reports to. ------ Output ------ Output a list of space-separated integers in ascending order — the indices of potential killers. ------ Constraints ------ $1 ≤ N ≤ 10^{5}$ $1 ≤ R_{i} ≤ N except for Don, whose R_{i} equals to 0.$ $It is guaranteed that there are no cycles in the reporting structure.$ ------ Subtasks ------ $Subtask #1 [50 points]: N ≤ 10000$ $Subtask #2 [50 points]: No additional constraints$ ----- Sample Input 1 ------ 6 0 1 1 2 2 3 ----- Sample Output 1 ------ 4 5 6 ----- explanation 1 ------ The reporting structure:
{"inputs": ["6\n0 1 1 2 2 3"], "outputs": ["4 5 6"]}
455
28
coding
Solve the programming task below in a Python markdown code block. You are given an array A of length N. You can perform the following operation on the array, as long as it has more than one element: Choose any two adjacent elements, remove them from the array and insert their sum at that position. Formally, if the current length of the array is |A|, you can choose an index 1 ≤ i < |A|, and transform the array into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} + A_{i+1}, A_{i+2}, \ldots, A_{N}]. Note that after each operation, the length of array decreases by exactly 1. Print the minimum number of operations to be applied on array A such that all the elements in the resulting array are equal. See sample explanation for more details. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of two lines of input. - The first line contains an integer N. - The second line contains N space-separated integers, the elements of array A. ------ Output Format ------ For each test case, output on a new line the minimum number of operations required to make all the elements equal. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $2 ≤ N ≤ 3 \cdot 10^{4}$ $-3 \cdot 10^{4} ≤ A_{i} ≤ 3 \cdot 10^{4}$ - Sum of $N$ over all test cases does not exceed $3 \cdot 10^{4}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 4 3 5 2 3 2 6 9 5 3 6 5 4 9 3 3 3 3 ----- Sample Output 1 ------ 1 1 2 0 ----- explanation 1 ------ Test case $1$: It is optimal to remove $A_{2}$ and $A_{3}$ in the first operation, after which the array becomes $[5,5]$ — all of whose elements are equal. Test case $2$: Remove $A_{1}$ and $A_{2}$ after which the array becomes $[15]$, which contains equal elements because it is of length $1$. Test case $3$: First remove $A_{3}$ and $A_{4}$ after which the updated array $A$ becomes $[3,6,9,9]$. Now remove $A_{1}$ and $A_{2}$ after which the array becomes $[9,9,9]$. Test case $4$: The array elements are already equal.
{"inputs": ["4\n3\n5 2 3\n2\n6 9\n5\n3 6 5 4 9\n3\n3 3 3\n"], "outputs": ["1\n1\n2\n0\n"]}
616
54
coding
Solve the programming task below in a Python markdown code block. Problem Statement 2 players, Cyborg and Geno are playing a game on a matrix. In each turn, the player choses a number from the matrix which is not selected yet and adds the number to his sum. In the end, the player with the largest sum wins the game. Assuming both players play optimally and Cyborg starts the game, predict who will be the winner of the game. Assume that the initial sum is 0 for both the players. ------ Input section ------ The first line contains T, the number of test cases. The description of T test cases follows. Each test case begin with 2 integers, N and M, denoting the number of rows and columns in the matrix. The next N lines contains M space separated integers denoting the numbers present in the matrix. ------ Output section ------ For each testcase, output "Cyborg", "Geno" or "Draw" based on the outcome of the game. ------ Input constraints ------ 1 ≤ T ≤ 20 1 ≤ N, M ≤ 50 0 ≤ A[i][j] ≤ 100, where A[i][j] denotes the element of the matrix in i^{th} row and j^{th} column. ----- Sample Input 1 ------ 3 1 1 3 2 3 4 4 4 4 4 4 2 3 4 3 4 4 4 4 ----- Sample Output 1 ------ Cyborg Draw Cyborg ----- explanation 1 ------ In the first test case, Cyborg wins by selecting the only number present in the matrix. In the second test case, both Cyborg and Geno will end up with a sum of 12, irrespective of how they chose the numbers. Hence, it results in a draw. In the third test case, when with the player play optimally, Cyborg will end up with sum of 12 while Geno will end up with sum of 11. So, Cyborg wins the game.
{"inputs": ["3\n1 1\n3\n2 3\n4 4 4\n4 4 4\n2 3\n4 3 4\n4 4 4"], "outputs": ["Cyborg\nDraw\nCyborg"]}
439
56
coding
Solve the programming task below in a Python markdown code block. Your friend is developing a computer game. He has already decided how the game world should look like — it should consist of n locations connected by m two-way passages. The passages are designed in such a way that it should be possible to get from any location to any other location. Of course, some passages should be guarded by the monsters (if you just can go everywhere without any difficulties, then it's not fun, right?). Some crucial passages will be guarded by really fearsome monsters, requiring the hero to prepare for battle and designing his own tactics of defeating them (commonly these kinds of monsters are called bosses). And your friend wants you to help him place these bosses. The game will start in location s and end in location t, but these locations are not chosen yet. After choosing these locations, your friend will place a boss in each passage such that it is impossible to get from s to t without using this passage. Your friend wants to place as much bosses as possible (because more challenges means more fun, right?), so he asks you to help him determine the maximum possible number of bosses, considering that any location can be chosen as s or as t. Input The first line contains two integers n and m (2 ≤ n ≤ 3 ⋅ 10^5, n - 1 ≤ m ≤ 3 ⋅ 10^5) — the number of locations and passages, respectively. Then m lines follow, each containing two integers x and y (1 ≤ x, y ≤ n, x ≠ y) describing the endpoints of one of the passages. It is guaranteed that there is no pair of locations directly connected by two or more passages, and that any location is reachable from any other location. Output Print one integer — the maximum number of bosses your friend can place, considering all possible choices for s and t. Examples Input 5 5 1 2 2 3 3 1 4 1 5 2 Output 2 Input 4 3 1 2 4 3 3 2 Output 3
{"inputs": ["4 3\n1 2\n4 2\n3 2\n", "4 3\n1 2\n4 3\n3 2\n", "5 5\n1 2\n2 3\n3 1\n4 1\n5 4\n", "5 5\n1 2\n2 3\n3 1\n4 1\n5 3\n", "5 5\n1 2\n2 3\n3 1\n4 1\n5 2\n", "5 6\n1 5\n2 3\n3 5\n2 1\n2 5\n2 4\n", "5 6\n1 5\n2 3\n3 5\n2 1\n2 5\n1 4\n", "5 6\n1 5\n3 3\n3 5\n2 2\n2 5\n2 4\n"], "outputs": ["2\n", "3\n", "2\n", "2\n", "2\n", "1\n", "1\n", "3\n"]}
444
242
coding
Solve the programming task below in a Python markdown code block. Just like in the ["father" kata](http://www.codewars.com/kata/find-fibonacci-last-digit/), you will have to return the last digit of the nth element in the Fibonacci sequence (starting with 1,1, to be extra clear, not with 0,1 or other numbers). You will just get much bigger numbers, so good luck bruteforcing your way through it ;) ```python last_fib_digit(1) == 1 last_fib_digit(2) == 1 last_fib_digit(3) == 2 last_fib_digit(1000) == 5 last_fib_digit(1000000) == 5 ``` ``` haskell lastFibDigit 1 == 1 lastFibDigit 2 == 1 lastFibDigit 3 == 2 lastFibDigit 1000 == 5 lastFibDigit 1000000 == 5 ``` Also feel free to reuse/extend the following starter code: ```python def last_fib_digit(n): ```
{"functional": "_inputs = [[1], [21], [302], [4003], [50004], [600005], [7000006], [80000007], [900000008], [1000000009]]\n_outputs = [[1], [6], [1], [7], [8], [5], [3], [8], [1], [9]]\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(last_fib_digit(*i), o[0])"}
261
255
coding
Solve the programming task below in a Python markdown code block. The growth of Computer Science has forced the scientific community to award Nobel Prize in CS starting from this year. Chef knows that the Nobel community is going to award the prize to that person whose research is different from others (ie. no other researcher should work on the same topic). If there are multiple such people, who work on unique topics, then they will all share the prize. It might also happen that no one wins this time. Chef also knows the N researchers which the community who will be considered for the prize, and the topics in which each of them work. In total the CS field can be divided into M broad topics. Given the topics in which each of the N researchers are working on, in the form of an array A, and given that Chef can master any topic instantly, find whether he can win the prize. That is, can the Chef choose to work on some topic which will guarantee that he will win the prize? Chef doesn't mind sharing the prize with others. ------ Input Format ------ - First line will contain T, number of testcases. Then the testcases follow. - Each testcase contains of two lines of input. - First line contains two space separated integers N, M, number of researchers excluding Chef and total number of fields respectively. - Second line contains N space separated integers A_{1}, A_{2}, \ldots A_{N}, research topic of the researchers. ------ Output Format ------ For each testcase, output in a single line answer, "Yes" if Chef can win the prize and "No" if not. You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ N, M ≤ 10^{5}$ $1 ≤ A_{i} ≤ M$ - Sum of $N$ over all tests is at most $10^{6}$ - Sum of $M$ over all tests is at most $10^{6}$ ----- Sample Input 1 ------ 3 4 4 1 2 3 4 5 4 4 2 1 1 1 4 4 4 4 4 4 ----- Sample Output 1 ------ No Yes Yes ----- explanation 1 ------ TestCase 1: Since all the $4$ available topics have been taken up, Chef can't choose a distinct topic and hence can't win the prize. No one will win the prize this year. TestCase 2: Since only $3$ distinct topics out of the $4$ available have been taken up, Chef can choose the remaining one, i.e, topic $3$ to win the prize jointly with the first and the second researcher. TestCase 3: Since only $1$ distinct topic out of the $4$ available has been taken up, Chef can choose any of the remaining ones to win the prize.
{"inputs": ["3\n4 4\n1 2 3 4\n5 4\n4 2 1 1 1\n4 4\n4 4 4 4"], "outputs": ["No\nYes\nYes"]}
630
54
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a binary tree root, return the maximum sum of all keys of any sub-tree which is also a Binary Search Tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greater than the node's key. Both the left and right subtrees must also be binary search trees.   Please complete the following python code precisely: ```python # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def maxSumBST(self, root: Optional[TreeNode]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(root = tree_node([1,4,3,2,4,2,5,None,None,None,None,None,None,4,6])) == 20\n assert candidate(root = tree_node([4,3,None,1,2])) == 2\n assert candidate(root = tree_node([-4,-2,-5])) == 0\n assert candidate(root = tree_node([2,1,3])) == 6\n assert candidate(root = tree_node([5,4,8,3,None,6,3])) == 7\n\n\ncheck(Solution().maxSumBST)"}
187
139
coding
Solve the programming task below in a Python markdown code block. Chef has his lunch only between 1 pm and 4 pm (both inclusive). Given that the current time is X pm, find out whether it is *lunchtime* for Chef. ------ Input Format ------ - The first line of input will contain a single integer T, the number of test cases. Then the test cases follow. - Each test case contains a single line of input, containing one integer X. ------ Output Format ------ For each test case, print in a single line \texttt{YES} if it is lunchtime for Chef. Otherwise, print \texttt{NO}. You may print each character of the string in either uppercase or lowercase (for example, the strings \texttt{YeS}, \texttt{yEs}, \texttt{yes} and \texttt{YES} will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 12$ $1 ≤ X ≤ 12$ ----- Sample Input 1 ------ 3 1 7 3 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Test case $1$: Lunchtime is between $1$ pm and $4$ pm (both inclusive). Since $1$ pm lies within lunchtime, the answer is $\texttt{YES}$. Test case $2$: Lunchtime is between $1$ pm and $4$ pm (both inclusive). Since $7$ pm lies outside lunchtime, the answer is $\texttt{NO}$. Test case $3$: Lunchtime is between $1$ pm and $4$ pm (both inclusive). Since $3$ pm lies within lunchtime, the answer is $\texttt{YES}$.
{"inputs": ["3\n1\n7\n3"], "outputs": ["YES\nNO\nYES"]}
373
22
coding
Solve the programming task below in a Python markdown code block. You are given a pair of integers $(a, b)$ and an integer $x$. You can change the pair in two different ways: set (assign) $a := |a - b|$; set (assign) $b := |a - b|$, where $|a - b|$ is the absolute difference between $a$ and $b$. The pair $(a, b)$ is called $x$-magic if $x$ is obtainable either as $a$ or as $b$ using only the given operations (i.e. the pair $(a, b)$ is $x$-magic if $a = x$ or $b = x$ after some number of operations applied). You can apply the operations any number of times (even zero). Your task is to find out if the pair $(a, b)$ is $x$-magic or not. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The next $t$ lines describe test cases. The only line of the test case contains three integers $a$, $b$ and $x$ ($1 \le a, b, x \le 10^{18}$). -----Output----- For the $i$-th test case, print YES if the corresponding pair $(a, b)$ is $x$-magic and NO otherwise. -----Examples----- Input 8 6 9 3 15 38 7 18 8 8 30 30 30 40 50 90 24 28 20 365 216 52 537037812705867558 338887693834423551 3199921013340 Output YES YES YES YES NO YES YES YES -----Note----- None
{"inputs": ["1\n4398046511104 4398050705408 8388608\n", "8\n6 9 3\n15 38 7\n18 8 8\n30 30 30\n40 50 90\n24 28 20\n365 216 52\n537037812705867558 338887693834423551 3199921013340\n"], "outputs": ["YES\n", "YES\nYES\nYES\nYES\nNO\nYES\nYES\nYES\n"]}
466
183
coding
Solve the programming task below in a Python markdown code block. Chef has N laddus of K sweetness each. Chef wants to eat all laddus, but Chef is restricted with the given condition that he must not eat two adjacent laddus. Chef starts calculating the maximum sweetness that he will get from the laddus. Find the maximum sweetness that chef gets at the end of all calculations. -----Input:----- - First-line will contain $T$, the number of test cases. Then the test cases follow. - Each test case contains a single line of input, two integers $N, K$. -----Output:----- For each test case, output in a single line answer as Maximum sweetness the chef will have. -----Constraints----- - $1 \leq T \leq 10^5$ - $1 \leq N \leq 10^5$ - $1 \leq K \leq 10^5$ -----Sample Input:----- 2 1 2 4 3 -----Sample Output:----- 2 6 -----EXPLANATION:----- For 1) Chef will get only 1 laddu with sweetness 2. For 2) Chef will have multiple ways as [1,3], [2,4], [1,4] with sweetness 6 [1],[2],[3],[4] with sweetness 3. Maximum sweetness will be 6.
{"inputs": ["2\n1 2\n4 3"], "outputs": ["2\n6"]}
301
22
coding
Solve the programming task below in a Python markdown code block. Long long ago, there was a thief. Looking for treasures, he was running about all over the world. One day, he heard a rumor that there were islands that had large amount of treasures, so he decided to head for there. Finally he found n islands that had treasures and one island that had nothing. Most of islands had seashore and he can land only on an island which had nothing. He walked around the island and found that there was an old bridge between this island and each of all other n islands. He tries to visit all islands one by one and pick all the treasures up. Since he is afraid to be stolen, he visits with bringing all treasures that he has picked up. He is a strong man and can bring all the treasures at a time, but the old bridges will break if he cross it with taking certain or more amount of treasures. Please write a program that judges if he can collect all the treasures and can be back to the island where he land on by properly selecting an order of his visit. Constraints * 1 ≤ n ≤ 25 Input Input consists of several datasets. The first line of each dataset contains an integer n. Next n lines represents information of the islands. Each line has two integers, which means the amount of treasures of the island and the maximal amount that he can take when he crosses the bridge to the islands, respectively. The end of input is represented by a case with n = 0. Output For each dataset, if he can collect all the treasures and can be back, print "Yes" Otherwise print "No" Example Input 3 2 3 3 6 1 2 3 2 3 3 5 1 2 0 Output Yes No
{"inputs": ["3\n2 3\n3 6\n1 2\n3\n2 3\n3 5\n2 2\n0", "3\n2 3\n3 6\n2 2\n3\n3 3\n3 6\n2 2\n0", "3\n2 1\n3 6\n1 2\n3\n2 3\n3 5\n0 2\n0", "3\n4 5\n3 8\n1 2\n0\n7 3\n3 5\n2 1\n0", "3\n2 3\n3 6\n1 2\n3\n0 3\n3 5\n1 2\n0", "3\n2 3\n3 6\n1 2\n3\n3 3\n3 5\n2 2\n0", "3\n2 3\n3 6\n1 2\n3\n3 3\n3 6\n2 2\n0", "3\n2 3\n3 8\n2 2\n3\n3 3\n3 6\n2 2\n0"], "outputs": ["Yes\nNo\n", "No\nNo\n", "No\nYes\n", "Yes\n", "Yes\nYes\n", "Yes\nNo\n", "Yes\nNo\n", "No\nNo\n"]}
377
300
coding
Solve the programming task below in a Python markdown code block. Let $s$ be some string consisting of symbols "0" or "1". Let's call a string $t$ a substring of string $s$, if there exists such number $1 \leq l \leq |s| - |t| + 1$ that $t = s_l s_{l+1} \ldots s_{l + |t| - 1}$. Let's call a substring $t$ of string $s$ unique, if there exist only one such $l$. For example, let $s = $"1010111". A string $t = $"010" is an unique substring of $s$, because $l = 2$ is the only one suitable number. But, for example $t = $"10" isn't a unique substring of $s$, because $l = 1$ and $l = 3$ are suitable. And for example $t =$"00" at all isn't a substring of $s$, because there is no suitable $l$. Today Vasya solved the following problem at the informatics lesson: given a string consisting of symbols "0" and "1", the task is to find the length of its minimal unique substring. He has written a solution to this problem and wants to test it. He is asking you to help him. You are given $2$ positive integers $n$ and $k$, such that $(n \bmod 2) = (k \bmod 2)$, where $(x \bmod 2)$ is operation of taking remainder of $x$ by dividing on $2$. Find any string $s$ consisting of $n$ symbols "0" or "1", such that the length of its minimal unique substring is equal to $k$. -----Input----- The first line contains two integers $n$ and $k$, separated by spaces ($1 \leq k \leq n \leq 100\,000$, $(k \bmod 2) = (n \bmod 2)$). -----Output----- Print a string $s$ of length $n$, consisting of symbols "0" and "1". Minimal length of the unique substring of $s$ should be equal to $k$. You can find any suitable string. It is guaranteed, that there exists at least one such string. -----Examples----- Input 4 4 Output 1111 Input 5 3 Output 01010 Input 7 3 Output 1011011 -----Note----- In the first test, it's easy to see, that the only unique substring of string $s = $"1111" is all string $s$, which has length $4$. In the second test a string $s = $"01010" has minimal unique substring $t =$"101", which has length $3$. In the third test a string $s = $"1011011" has minimal unique substring $t =$"110", which has length $3$.
{"inputs": ["4 4\n", "5 3\n", "7 3\n", "1 1\n", "2 2\n", "3 3\n", "3 1\n", "4 2\n"], "outputs": ["1111", "01010", "0010010", "1", "11", "111", "010", "0101"]}
673
99
coding
Solve the programming task below in a Python markdown code block. You are given two strings a and b. You have to remove the minimum possible number of consecutive (standing one after another) characters from string b in such a way that it becomes a subsequence of string a. It can happen that you will not need to remove any characters at all, or maybe you will have to remove all of the characters from b and make it empty. Subsequence of string s is any such string that can be obtained by erasing zero or more characters (not necessarily consecutive) from string s. -----Input----- The first line contains string a, and the second line — string b. Both of these strings are nonempty and consist of lowercase letters of English alphabet. The length of each string is no bigger than 10^5 characters. -----Output----- On the first line output a subsequence of string a, obtained from b by erasing the minimum number of consecutive characters. If the answer consists of zero characters, output «-» (a minus sign). -----Examples----- Input hi bob Output - Input abca accepted Output ac Input abacaba abcdcba Output abcba -----Note----- In the first example strings a and b don't share any symbols, so the longest string that you can get is empty. In the second example ac is a subsequence of a, and at the same time you can obtain it by erasing consecutive symbols cepted from string b.
{"inputs": ["a\na\n", "a\nb\n", "a\nb\n", "a\na\n", "b\nb\n", "aa\na\n", "a\nab\n", "b\ncb\n"], "outputs": ["a\n", "-\n", "-", "a", "b\n", "a\n", "a\n", "b\n"]}
312
83
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an undirected graph defined by an integer n, the number of nodes, and a 2D integer array edges, the edges in the graph, where edges[i] = [ui, vi] indicates that there is an undirected edge between ui and vi. You are also given an integer array queries. Let incident(a, b) be defined as the number of edges that are connected to either node a or b. The answer to the jth query is the number of pairs of nodes (a, b) that satisfy both of the following conditions: a < b incident(a, b) > queries[j] Return an array answers such that answers.length == queries.length and answers[j] is the answer of the jth query. Note that there can be multiple edges between the same two nodes.   Please complete the following python code precisely: ```python class Solution: def countPairs(self, n: int, edges: List[List[int]], queries: List[int]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(n = 4, edges = [[1,2],[2,4],[1,3],[2,3],[2,1]], queries = [2,3]) == [6,5]\n assert candidate(n = 5, edges = [[1,5],[1,5],[3,4],[2,5],[1,3],[5,1],[2,3],[2,5]], queries = [1,2,3,4,5]) == [10,10,9,8,6]\n\n\ncheck(Solution().countPairs)"}
224
135
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given two strings s and goal, return true if you can swap two letters in s so the result is equal to goal, otherwise, return false. Swapping letters is defined as taking two indices i and j (0-indexed) such that i != j and swapping the characters at s[i] and s[j]. For example, swapping at indices 0 and 2 in "abcd" results in "cbad".   Please complete the following python code precisely: ```python class Solution: def buddyStrings(self, s: str, goal: str) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(s = \"ab\", goal = \"ba\") == True\n assert candidate(s = \"ab\", goal = \"ab\") == False\n assert candidate(s = \"aa\", goal = \"aa\") == True\n\n\ncheck(Solution().buddyStrings)"}
136
68
coding
Solve the programming task below in a Python markdown code block. You are given an array A of N integers. You can do the following two types of operations any (possibly zero) number of times: Pick two indices i and j (1 ≤ i,j ≤ |A|, i \neq j). Change A_{j} := A_{j} + A_{i} and remove the i^{th} element from the array. Pick an index i (1 ≤ i ≤ |A|). Split A_{i} into two positive integers X and Y such that X+Y=A_{i}. Remove the i^{th} element from the array and append elements X and Y to the array. Find the maximum number of distinct elements present in the array after performing any number of operations of the above two types. ------ Input Format ------ - The first line contains an integer T denoting the number of test cases. The T test cases then follow. - The first line of each test case contains an integer N - the size of the array. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}. ------ Output Format ------ For each test case, output the maximum number of distinct elements present in the array after performing any number of operations of the above two types. ------ Constraints ------ $1 ≤T ≤100$ $2 ≤N ≤1000$ $1 ≤A_{i} ≤10^{5}$ ----- Sample Input 1 ------ 2 3 1 2 4 4 1 1 3 4 ----- Sample Output 1 ------ 3 3 ----- explanation 1 ------ - Test case $1$: The maximum number of distinct elements that can be achieved by performing some finite number of operations on the given array is $3$. Some examples of the final array are: - $[1, 2, 4]$ : Perform no operation on the given array. - $[1, 2, 1, 3]$ : Perform operation $2$. Choose $i = 3$. Here, $A_{3} = 4$. Break it as $X = 1$ and $Y = 3$. On removing $A_{3}$ and appending $X$ and $Y$, we get $[1, 2, 1, 3]$. This array has $3$ distinct elements. - Test case $2$: The maximum number of distinct elements that can be achieved by performing some finite number of operations on the given array is $3$. Some examples of the final array are: - $[1, 1, 3, 4]$ : Perform no operation on the given array. - $[1, 1, 3, 2, 2]$ : Perform operation $2$. Choose $i = 4$. Here, $A_{4} = 4$. Break it as $X = 2$ and $Y = 2$. On removing $A_{4}$ and appending $X$ and $Y$, we get $[1, 1, 3, 2, 2]$. This array has $3$ distinct elements. - $[2, 3, 4]$ : Perform operation $1$. Choose $i = 1$ and $j=2$. On changing $A_{2} := A_{1}+A_{2} = 1+1=2$ and removing $A_{1}$, we get $[2, 3, 4]$. This array has $3$ distinct elements.
{"inputs": ["2\n3\n1 2 4\n4\n1 1 3 4"], "outputs": ["3\n3"]}
762
32
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are playing a game that has n levels numbered from 0 to n - 1. You are given a 0-indexed integer array damage where damage[i] is the amount of health you will lose to complete the ith level. You are also given an integer armor. You may use your armor ability at most once during the game on any level which will protect you from at most armor damage. You must complete the levels in order and your health must be greater than 0 at all times to beat the game. Return the minimum health you need to start with to beat the game.   Please complete the following python code precisely: ```python class Solution: def minimumHealth(self, damage: List[int], armor: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(damage = [2,7,4,3], armor = 4) == 13\n assert candidate(damage = [2,5,3,4], armor = 7) == 10\n assert candidate(damage = [3,3,3], armor = 0) == 10\n\n\ncheck(Solution().minimumHealth)"}
171
92
coding
Solve the programming task below in a Python markdown code block. Mahmoud and Ehab play a game called the even-odd game. Ehab chooses his favorite integer n and then they take turns, starting from Mahmoud. In each player's turn, he has to choose an integer a and subtract it from n such that: 1 ≤ a ≤ n. If it's Mahmoud's turn, a has to be even, but if it's Ehab's turn, a has to be odd. If the current player can't choose any number satisfying the conditions, he loses. Can you determine the winner if they both play optimally? -----Input----- The only line contains an integer n (1 ≤ n ≤ 10^9), the number at the beginning of the game. -----Output----- Output "Mahmoud" (without quotes) if Mahmoud wins and "Ehab" (without quotes) otherwise. -----Examples----- Input 1 Output Ehab Input 2 Output Mahmoud -----Note----- In the first sample, Mahmoud can't choose any integer a initially because there is no positive even integer less than or equal to 1 so Ehab wins. In the second sample, Mahmoud has to choose a = 2 and subtract it from n. It's Ehab's turn and n = 0. There is no positive odd integer less than or equal to 0 so Mahmoud wins.
{"inputs": ["1\n", "2\n", "5\n", "3\n", "4\n", "6\n", "7\n", "8\n"], "outputs": ["Ehab", "Mahmoud", "Ehab", "Ehab", "Mahmoud", "Mahmoud", "Ehab", "Mahmoud"]}
295
74
coding
Solve the programming task below in a Python markdown code block. You are given an integer $n$. In one move, you can either multiply $n$ by two or divide $n$ by $6$ (if it is divisible by $6$ without the remainder). Your task is to find the minimum number of moves needed to obtain $1$ from $n$ or determine if it's impossible to do that. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 10^9$). -----Output----- For each test case, print the answer — the minimum number of moves needed to obtain $1$ from $n$ if it's possible to do that or -1 if it's impossible to obtain $1$ from $n$. -----Example----- Input 7 1 2 3 12 12345 15116544 387420489 Output 0 -1 2 -1 -1 12 36 -----Note----- Consider the sixth test case of the example. The answer can be obtained by the following sequence of moves from the given integer $15116544$: Divide by $6$ and get $2519424$; divide by $6$ and get $419904$; divide by $6$ and get $69984$; divide by $6$ and get $11664$; multiply by $2$ and get $23328$; divide by $6$ and get $3888$; divide by $6$ and get $648$; divide by $6$ and get $108$; multiply by $2$ and get $216$; divide by $6$ and get $36$; divide by $6$ and get $6$; divide by $6$ and get $1$.
{"inputs": ["1\n64693265\n", "1\n10872979\n", "1\n21674312\n", "1\n32114318\n", "1\n54097276\n", "1\n65971871\n", "1\n93108643\n", "1\n30365546\n"], "outputs": ["-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n"]}
500
143
coding
Solve the programming task below in a Python markdown code block. There are two types of burgers in your restaurant — hamburgers and chicken burgers! To assemble a hamburger you need two buns and a beef patty. To assemble a chicken burger you need two buns and a chicken cutlet. You have $b$ buns, $p$ beef patties and $f$ chicken cutlets in your restaurant. You can sell one hamburger for $h$ dollars and one chicken burger for $c$ dollars. Calculate the maximum profit you can achieve. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 100$) – the number of queries. The first line of each query contains three integers $b$, $p$ and $f$ ($1 \le b, ~p, ~f \le 100$) — the number of buns, beef patties and chicken cutlets in your restaurant. The second line of each query contains two integers $h$ and $c$ ($1 \le h, ~c \le 100$) — the hamburger and chicken burger prices in your restaurant. -----Output----- For each query print one integer — the maximum profit you can achieve. -----Example----- Input 3 15 2 3 5 10 7 5 2 10 12 1 100 100 100 100 Output 40 34 0 -----Note----- In first query you have to sell two hamburgers and three chicken burgers. Your income is $2 \cdot 5 + 3 \cdot 10 = 40$. In second query you have to ell one hamburgers and two chicken burgers. Your income is $1 \cdot 10 + 2 \cdot 12 = 34$. In third query you can not create any type of burgers because because you have only one bun. So your income is zero.
{"inputs": ["1\n0 4 4\n3 0\n", "1\n0 0 4\n3 0\n", "1\n0 0 4\n2 0\n", "1\n0 0 3\n2 0\n", "1\n1 0 3\n2 0\n", "1\n1 0 1\n2 0\n", "1\n1 0 1\n3 0\n", "1\n1 0 1\n4 0\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
435
150
coding
Solve the programming task below in a Python markdown code block. Nearly each project of the F company has a whole team of developers working on it. They often are in different rooms of the office in different cities and even countries. To keep in touch and track the results of the project, the F company conducts shared online meetings in a Spyke chat. One day the director of the F company got hold of the records of a part of an online meeting of one successful team. The director watched the record and wanted to talk to the team leader. But how can he tell who the leader is? The director logically supposed that the leader is the person who is present at any conversation during a chat meeting. In other words, if at some moment of time at least one person is present on the meeting, then the leader is present on the meeting. You are the assistant director. Given the 'user logged on'/'user logged off' messages of the meeting in the chronological order, help the director determine who can be the leader. Note that the director has the record of only a continuous part of the meeting (probably, it's not the whole meeting). Input The first line contains integers n and m (1 ≤ n, m ≤ 105) — the number of team participants and the number of messages. Each of the next m lines contains a message in the format: * '+ id': the record means that the person with number id (1 ≤ id ≤ n) has logged on to the meeting. * '- id': the record means that the person with number id (1 ≤ id ≤ n) has logged off from the meeting. Assume that all the people of the team are numbered from 1 to n and the messages are given in the chronological order. It is guaranteed that the given sequence is the correct record of a continuous part of the meeting. It is guaranteed that no two log on/log off events occurred simultaneously. Output In the first line print integer k (0 ≤ k ≤ n) — how many people can be leaders. In the next line, print k integers in the increasing order — the numbers of the people who can be leaders. If the data is such that no member of the team can be a leader, print a single number 0. Examples Input 5 4 + 1 + 2 - 2 - 1 Output 4 1 3 4 5 Input 3 2 + 1 - 2 Output 1 3 Input 2 4 + 1 - 1 + 2 - 2 Output 0 Input 5 6 + 1 - 1 - 3 + 3 + 4 - 4 Output 3 2 3 5 Input 2 4 + 1 - 2 + 2 - 1 Output 0
{"inputs": ["1 1\n+ 1\n", "2 1\n- 2\n", "2 1\n+ 1\n", "2 1\n+ 2\n", "20 1\n- 16\n", "2 2\n- 1\n+ 1\n", "6 2\n+ 1\n- 2\n", "6 2\n+ 1\n- 4\n"], "outputs": ["1\n1 \n", "2\n1 2 \n", "2\n1 2 \n", "2\n1 2 \n", "20\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 \n", "2\n1 2 \n", "4\n3 4 5 6 \n", "4\n2 3 5 6 \n"]}
613
226
coding
Solve the programming task below in a Python markdown code block. The BFS algorithm is defined as follows. Consider an undirected graph with vertices numbered from $1$ to $n$. Initialize $q$ as a new queue containing only vertex $1$, mark the vertex $1$ as used. Extract a vertex $v$ from the head of the queue $q$. Print the index of vertex $v$. Iterate in arbitrary order through all such vertices $u$ that $u$ is a neighbor of $v$ and is not marked yet as used. Mark the vertex $u$ as used and insert it into the tail of the queue $q$. If the queue is not empty, continue from step 2. Otherwise finish. Since the order of choosing neighbors of each vertex can vary, it turns out that there may be multiple sequences which BFS can print. In this problem you need to check whether a given sequence corresponds to some valid BFS traversal of the given tree starting from vertex $1$. The tree is an undirected graph, such that there is exactly one simple path between any two vertices. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) which denotes the number of nodes in the tree. The following $n - 1$ lines describe the edges of the tree. Each of them contains two integers $x$ and $y$ ($1 \le x, y \le n$) — the endpoints of the corresponding edge of the tree. It is guaranteed that the given graph is a tree. The last line contains $n$ distinct integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) — the sequence to check. -----Output----- Print "Yes" (quotes for clarity) if the sequence corresponds to some valid BFS traversal of the given tree and "No" (quotes for clarity) otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 4 1 2 1 3 2 4 1 2 3 4 Output Yes Input 4 1 2 1 3 2 4 1 2 4 3 Output No -----Note----- Both sample tests have the same tree in them. In this tree, there are two valid BFS orderings: $1, 2, 3, 4$, $1, 3, 2, 4$. The ordering $1, 2, 4, 3$ doesn't correspond to any valid BFS order.
{"inputs": ["1\n1\n", "1\n1\n", "2\n1 2\n2 1\n", "2\n2 1\n1 2\n", "2\n2 1\n2 1\n", "2\n1 2\n2 1\n", "2\n2 1\n1 2\n", "2\n2 1\n2 1\n"], "outputs": ["Yes", "Yes\n", "No", "Yes", "No", "No\n", "Yes\n", "No\n"]}
562
118
coding
Solve the programming task below in a Python markdown code block. Chef's company wants to make ATM PINs for its users, so that they could use the PINs for withdrawing their hard-earned money. One of these users is Reziba, who lives in an area where a lot of robberies take place when people try to withdraw their money. Chef plans to include a safety feature in the PINs: if someone inputs the reverse of their own PIN in an ATM machine, the Crime Investigation Department (CID) are immediately informed and stop the robbery. However, even though this was implemented by Chef, some people could still continue to get robbed. The reason is that CID is only informed if the reverse of a PIN is different from that PIN (so that there wouldn't be false reports of robberies). You know that a PIN consists of $N$ decimal digits. Find the probability that Reziba could get robbed. Specifically, it can be proven that this probability can be written as a fraction $P/Q$, where $P \ge 0$ and $Q > 0$ are coprime integers; you should compute $P$ and $Q$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains a single integer $N$ denoting the length of each PIN. -----Output----- For each test case, print a single line containing two space-separated integers — the numerator $P$ and denominator $Q$ of the probability. -----Constraints----- - $1 \le T \le 100$ - $1 \le N \le 10^5$ -----Subtasks----- Subtask #1 (10 points): $N \le 18$ Subtask #2 (20 points): $N \le 36$ Subtask #3 (70 points): original constraints -----Example Input----- 1 1 -----Example Output----- 1 1 -----Explanation----- Example case 1: A PIN containing only one number would fail to inform the CID, since when it's input in reverse, the ATM detects the same PIN as the correct one. Therefore, Reziba can always get robbed — the probability is $1 = 1/1$.
{"inputs": ["1\n1"], "outputs": ["1 1"]}
490
16
coding
Solve the programming task below in a Python markdown code block. You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types: increase $x$ by $1$; decrease $x$ by $1$. You are given $m$ queries of the following format: query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases. Then the description of $t$ testcases follows. The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries. The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively. Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query. The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order. -----Examples----- Input 2 8 4 -+--+--+ 1 8 2 8 2 5 1 1 4 10 +-++ 1 1 1 2 2 2 1 3 2 3 3 3 1 4 2 4 3 4 4 4 Output 1 2 4 4 3 3 4 2 3 2 1 2 2 2 -----Note----- The instructions that remain for each query of the first testcase are: empty program — $x$ was only equal to $0$; "-" — $x$ had values $0$ and $-1$; "---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them; "+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$.
{"inputs": ["2\n8 4\n-+--+--+\n1 8\n2 8\n2 5\n1 1\n4 10\n+-++\n1 1\n1 2\n2 2\n1 3\n2 3\n3 3\n1 4\n2 4\n3 4\n4 4\n", "2\n8 4\n-+--+--+\n2 8\n2 8\n2 5\n1 1\n4 10\n+-++\n1 1\n1 2\n2 2\n1 3\n2 3\n3 3\n1 4\n2 4\n3 4\n4 4\n", "2\n8 4\n-+--+--+\n1 8\n2 8\n2 5\n2 1\n4 10\n+-++\n1 1\n1 2\n2 2\n1 3\n2 3\n3 3\n1 4\n2 4\n3 4\n4 4\n", "2\n8 4\n-+--+--+\n2 8\n2 8\n2 5\n1 1\n4 10\n+-++\n1 2\n1 2\n2 2\n1 3\n2 3\n3 3\n2 4\n2 4\n3 4\n4 4\n", "2\n8 4\n-+--+--+\n2 8\n2 8\n2 5\n1 1\n4 10\n+-++\n2 2\n1 1\n2 2\n1 3\n2 3\n3 3\n2 4\n2 4\n3 4\n4 4\n", "2\n8 4\n-+--+--+\n2 8\n2 8\n2 5\n1 1\n4 10\n+-++\n2 2\n1 1\n2 2\n1 3\n2 4\n3 3\n2 4\n2 4\n3 4\n4 4\n", "2\n8 4\n-+--+--+\n2 8\n2 8\n2 2\n1 1\n4 10\n+-++\n2 2\n1 1\n2 2\n1 3\n2 4\n3 3\n2 4\n2 4\n3 4\n4 4\n", "2\n8 4\n-+--+--+\n1 8\n2 8\n2 5\n2 1\n4 10\n+-++\n1 1\n1 2\n2 2\n1 3\n2 3\n3 3\n1 4\n2 4\n1 4\n4 4\n"], "outputs": ["1\n2\n4\n4\n3\n3\n4\n2\n3\n2\n1\n2\n2\n2\n", "2\n2\n4\n4\n3\n3\n4\n2\n3\n2\n1\n2\n2\n2\n", "1\n2\n4\n4\n3\n3\n4\n2\n3\n2\n1\n2\n2\n2\n", "2\n2\n4\n4\n3\n3\n4\n2\n3\n2\n2\n2\n2\n2\n", "2\n2\n4\n4\n4\n3\n4\n2\n3\n2\n2\n2\n2\n2\n", "2\n2\n4\n4\n4\n3\n4\n2\n2\n2\n2\n2\n2\n2\n", "2\n2\n5\n4\n4\n3\n4\n2\n2\n2\n2\n2\n2\n2\n", "1\n2\n4\n4\n3\n3\n4\n2\n3\n2\n1\n2\n1\n2\n"]}
628
878
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an integer num, return three consecutive integers (as a sorted array) that sum to num. If num cannot be expressed as the sum of three consecutive integers, return an empty array.   Please complete the following python code precisely: ```python class Solution: def sumOfThree(self, num: int) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(num = 33) == [10,11,12]\n assert candidate(num = 4) == []\n\n\ncheck(Solution().sumOfThree)"}
87
53
coding
Solve the programming task below in a Python markdown code block. You are given $n$ words. Some words may repeat. For each word, output its number of occurrences. The output order should correspond with the input order of appearance of the word. See the sample input/output for clarification. Note: Each input line ends with a "\n" character. Constraints: $1\leq n\leq10^5$ The sum of the lengths of all the words do not exceed $10^{6}$ All the words are composed of lowercase English letters only. Input Format The first line contains the integer, $n$. The next $n$ lines each contain a word. Output Format Output $2$ lines. On the first line, output the number of distinct words from the input. On the second line, output the number of occurrences for each distinct word according to their appearance in the input. Sample Input 4 bcdef abcdefg bcde bcdef Sample Output 3 2 1 1 Explanation There are $3$ distinct words. Here, "bcdef" appears twice in the input at the first and last positions. The other words appear once each. The order of the first appearances are "bcdef", "abcdefg" and "bcde" which corresponds to the output.
{"inputs": ["4\nbcdef\nabcdefg\nbcde\nbcdef\n"], "outputs": ["3\n2 1 1\n"]}
284
32
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an array of unique integers salary where salary[i] is the salary of the ith employee. Return the average salary of employees excluding the minimum and maximum salary. Answers within 10-5 of the actual answer will be accepted.   Please complete the following python code precisely: ```python class Solution: def average(self, salary: List[int]) -> float: ```
{"functional": "def check(candidate):\n assert candidate(salary = [4000,3000,1000,2000]) == 2500.00000\n assert candidate(salary = [1000,2000,3000]) == 2000.00000\n assert candidate(salary = [6000,5000,4000,3000,2000,1000]) == 3500.00000\n assert candidate(salary = [8000,9000,2000,3000,6000,1000]) == 4750.00000\n\n\ncheck(Solution().average)"}
96
193
coding
Solve the programming task below in a Python markdown code block. You are given an array $a$ of $n$ integers. You are allowed to perform the following operation on it as many times as you want (0 or more times): Choose $2$ indices $i$,$j$ where $1 \le i < j \le n$ and replace $a_k$ for all $i \leq k \leq j$ with $|a_i - a_j|$ Print the maximum sum of all the elements of the final array that you can obtain in such a way. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the elements of array $a$. It's guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the sum of the final array. -----Examples----- Input 3 3 1 1 1 2 9 1 3 4 9 5 Output 3 16 18 -----Note----- In the first test case, it is not possible to achieve a sum $> 3$ by using these operations, therefore the maximum sum is $3$. In the second test case, it can be shown that the maximum sum achievable is $16$. By using operation $(1,2)$ we transform the array from $[9,1]$ into $[8,8]$, thus the sum of the final array is $16$. In the third test case, it can be shown that it is not possible to achieve a sum $> 18$ by using these operations, therefore the maximum sum is $18$.
{"inputs": ["3\n3\n1 1 1\n2\n9 1\n3\n4 9 5\n"], "outputs": ["3\n16\n18\n"]}
468
42
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a 0-indexed 2D integer array grid of size m x n which represents a field. Each cell has one of three values: 0 represents grass, 1 represents fire, 2 represents a wall that you and fire cannot pass through. You are situated in the top-left cell, (0, 0), and you want to travel to the safehouse at the bottom-right cell, (m - 1, n - 1). Every minute, you may move to an adjacent grass cell. After your move, every fire cell will spread to all adjacent cells that are not walls. Return the maximum number of minutes that you can stay in your initial position before moving while still safely reaching the safehouse. If this is impossible, return -1. If you can always reach the safehouse regardless of the minutes stayed, return 109. Note that even if the fire spreads to the safehouse immediately after you have reached it, it will be counted as safely reaching the safehouse. A cell is adjacent to another cell if the former is directly north, east, south, or west of the latter (i.e., their sides are touching).   Please complete the following python code precisely: ```python class Solution: def maximumMinutes(self, grid: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(grid = [[0,2,0,0,0,0,0],[0,0,0,2,2,1,0],[0,2,0,0,1,2,0],[0,0,2,2,2,0,2],[0,0,0,0,0,0,0]]) == 3\n assert candidate(grid = [[0,0,0,0],[0,1,2,0],[0,2,0,0]]) == -1\n assert candidate(grid = [[0,0,0],[2,2,0],[1,2,0]]) == 1000000000\n\n\ncheck(Solution().maximumMinutes)"}
286
170
coding
Solve the programming task below in a Python markdown code block. ###Instructions Write a function that takes a negative or positive integer, which represents the number of minutes before (-) or after (+) Sunday midnight, and returns the current day of the week and the current time in 24hr format ('hh:mm') as a string. ```python day_and_time(0) should return 'Sunday 00:00' day_and_time(-3) should return 'Saturday 23:57' day_and_time(45) should return 'Sunday 00:45' day_and_time(759) should return 'Sunday 12:39' day_and_time(1236) should return 'Sunday 20:36' day_and_time(1447) should return 'Monday 00:07' day_and_time(7832) should return 'Friday 10:32' day_and_time(18876) should return 'Saturday 02:36' day_and_time(259180) should return 'Thursday 23:40' day_and_time(-349000) should return 'Tuesday 15:20' ``` Also feel free to reuse/extend the following starter code: ```python def day_and_time(mins): ```
{"functional": "_inputs = [[0], [-3], [45], [759], [1236], [1447], [7832], [18876], [259180], [-349000]]\n_outputs = [['Sunday 00:00'], ['Saturday 23:57'], ['Sunday 00:45'], ['Sunday 12:39'], ['Sunday 20:36'], ['Monday 00:07'], ['Friday 10:32'], ['Saturday 02:36'], ['Thursday 23:40'], ['Tuesday 15:20']]\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(day_and_time(*i), o[0])"}
304
295
coding
Solve the programming task below in a Python markdown code block. Niwango created a playlist of N songs. The title and the duration of the i-th song are s_i and t_i seconds, respectively. It is guaranteed that s_1,\ldots,s_N are all distinct. Niwango was doing some work while playing this playlist. (That is, all the songs were played once, in the order they appear in the playlist, without any pause in between.) However, he fell asleep during his work, and he woke up after all the songs were played. According to his record, it turned out that he fell asleep at the very end of the song titled X. Find the duration of time when some song was played while Niwango was asleep. Constraints * 1 \leq N \leq 50 * s_i and X are strings of length between 1 and 100 (inclusive) consisting of lowercase English letters. * s_1,\ldots,s_N are distinct. * There exists an integer i such that s_i = X. * 1 \leq t_i \leq 1000 * t_i is an integer. Input Input is given from Standard Input in the following format: N s_1 t_1 \vdots s_{N} t_N X Output Print the answer. Examples Input 3 dwango 2 sixth 5 prelims 25 dwango Output 30 Input 1 abcde 1000 abcde Output 0 Input 15 ypnxn 279 kgjgwx 464 qquhuwq 327 rxing 549 pmuduhznoaqu 832 dagktgdarveusju 595 wunfagppcoi 200 dhavrncwfw 720 jpcmigg 658 wrczqxycivdqn 639 mcmkkbnjfeod 992 htqvkgkbhtytsz 130 twflegsjz 467 dswxxrxuzzfhkp 989 szfwtzfpnscgue 958 pmuduhznoaqu Output 6348
{"inputs": ["1\nabcde 0000\nabcde", "1\nabcde 1000\nabcde", "3\ndwango 2\nsixth 5\nprelims 4\ndwango", "3\ndwango 2\nsixth 5\nprelims 7\ndwango", "3\ndwango 2\nsixth 0\nprelims 7\ndwango", "3\ndwango 2\nshxth 5\nsmilerp 1\ndwango", "3\ndwango 2\nshxth 10\nsmilerp 7\ndwango", "3\ndwango 2\nsixth 5\nprelims 25\ndwango"], "outputs": ["0\n", "0", "9\n", "12\n", "7\n", "6\n", "17\n", "30"]}
516
215
coding
Solve the programming task below in a Python markdown code block. Suppose there is a X x Y x Z 3D matrix A of numbers having coordinates (i, j, k) where 0 ≤ i < X, 0 ≤ j < Y, 0 ≤ k < Z. Now another X x Y x Z matrix B is defined from A such that the (i, j, k) element of B is the sum of all the the numbers in A in the cuboid defined by the (0, 0, 0) and (i, j, k) elements as the diagonally opposite vertices. In other word (i, j, k) in B is the sum of numbers of A having coordinates (a, b, c) such that 0 ≤ a ≤ i, 0 ≤ b ≤ j, 0 ≤ c ≤ k. The problem is that given B, you have to find out A. ------ Input ------ The first line of input will contain the number of test cases ( ≤ 10). That many test cases will follow in subsequent lines. The first line of each test case will contain the numbers X Y Z (0 ≤ X, Y, Z ≤ 100). After that there will be X x Y lines each containing Z numbers of B. The first line contains the numbers (0, 0, 0), (0, 0, 1)..., (0, 0, Z-1). The second line has the numbers (0, 1, 0), (0, 1, 1)..., (0, 1, Z-1) and so on. The (Y+1)^{th} line will have the numbers (1, 0, 0), (1, 0, 1)..., (1, 0, Z-1) and so on. ------ Output ------ For each test case print the numbers of A in exactly the same fashion as the input. ----- Sample Input 1 ------ 2 3 1 1 1 8 22 1 2 3 0 9 13 18 45 51 ----- Sample Output 1 ------ 1 7 14 0 9 4 18 18 2
{"inputs": ["2\n3 1 1\n1 \n8 \n22 \n1 2 3\n0 9 13 \n18 45 51", "2\n3 1 1\n1 \n8 \n22 \n1 2 3\n0 9 13 \n18 45 51"], "outputs": ["1 \n7 \n14 \n0 9 4 \n18 18 2", "1 \n7 \n14 \n0 9 4 \n18 18 2"]}
484
142
coding
Solve the programming task below in a Python markdown code block. Vasya had a strictly increasing sequence of positive integers a_1, ..., a_{n}. Vasya used it to build a new sequence b_1, ..., b_{n}, where b_{i} is the sum of digits of a_{i}'s decimal representation. Then sequence a_{i} got lost and all that remained is sequence b_{i}. Vasya wonders what the numbers a_{i} could be like. Of all the possible options he likes the one sequence with the minimum possible last number a_{n}. Help Vasya restore the initial sequence. It is guaranteed that such a sequence always exists. -----Input----- The first line contains a single integer number n (1 ≤ n ≤ 300). Next n lines contain integer numbers b_1, ..., b_{n}  — the required sums of digits. All b_{i} belong to the range 1 ≤ b_{i} ≤ 300. -----Output----- Print n integer numbers, one per line — the correct option for numbers a_{i}, in order of following in sequence. The sequence should be strictly increasing. The sum of digits of the i-th number should be equal to b_{i}. If there are multiple sequences with least possible number a_{n}, print any of them. Print the numbers without leading zeroes. -----Examples----- Input 3 1 2 3 Output 1 2 3 Input 3 3 2 1 Output 3 11 100
{"inputs": ["1\n6\n", "1\n6\n", "1\n8\n", "1\n2\n", "1\n1\n", "1\n3\n", "1\n4\n", "1\n5\n"], "outputs": ["6\n", "6\n", "8\n", "2\n", "1\n", "3\n", "4\n", "5\n"]}
332
86
coding
Solve the programming task below in a Python markdown code block. You are given an array $a$ consisting of $n$ integers. In one move, you can choose two indices $1 \le i, j \le n$ such that $i \ne j$ and set $a_i := a_j$. You can perform such moves any number of times (possibly, zero). You can choose different indices in different operations. The operation := is the operation of assignment (i.e. you choose $i$ and $j$ and replace $a_i$ with $a_j$). Your task is to say if it is possible to obtain an array with an odd (not divisible by $2$) sum of elements. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2000$) — the number of test cases. The next $2t$ lines describe test cases. The first line of the test case contains one integer $n$ ($1 \le n \le 2000$) — the number of elements in $a$. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2000$), where $a_i$ is the $i$-th element of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$ ($\sum n \le 2000$). -----Output----- For each test case, print the answer on it — "YES" (without quotes) if it is possible to obtain the array with an odd sum of elements, and "NO" otherwise. -----Example----- Input 5 2 2 3 4 2 2 8 8 3 3 3 3 4 5 5 5 5 4 1 1 1 1 Output YES NO YES NO NO
{"inputs": ["1\n1\n75\n", "1\n1\n96\n", "1\n1\n21\n", "1\n1\n27\n", "1\n1\n30\n", "1\n1\n22\n", "1\n1\n25\n", "1\n1\n83\n"], "outputs": ["YES\n", "NO\n", "YES\n", "YES\n", "NO\n", "NO\n", "YES\n", "YES\n"]}
443
110
coding
Solve the programming task below in a Python markdown code block. problem Hanako is playing with n (4 ≤ n ≤ 10) cards side by side. Each card has one integer between 1 and 99. Hanako chose k cards (2 ≤ k ≤ 4) from these cards and arranged them in a horizontal row to make an integer. How many kinds of integers can Hanako make in total? For example, consider that you are given five cards of 1, 2, 3, 13, 21 and choose three of them to make an integer. By arranging 2, 1, and 13 in this order, we can make an integer 2113. Also, by arranging 21, 1, and 3 in this order, the same integer 2113 can be created. In this way, the same integer may be created from a combination of different cards. Given the integers written on n cards, create a program to find the number of integers that can be created by selecting k cards from them and arranging them in a horizontal row. input The input consists of multiple datasets. Each dataset is given in the following format. Each dataset consists of 2 + n rows. The number of cards n (4 ≤ n ≤ 10) is written on the first line, and the number of cards to be selected k (2 ≤ k ≤ 4) is written on the second line. On the 2 + i line (1 ≤ i ≤ n), the integer from 1 to 99 written on the i-th card is written. When both n and k are 0, it indicates the end of input. The number of data sets does not exceed 5. output For each data set, the number of integers that Hanako can create is output on one line. Examples Input 4 2 1 2 12 1 6 3 72 2 12 7 2 1 0 0 Output 7 68 Input None Output None
{"inputs": ["4\n1\n1\n3\n12\n1\n6\n3\n8\n2\n0\n7\n2\n1\n0\n0", "4\n1\n1\n3\n12\n1\n6\n1\n8\n2\n0\n7\n2\n1\n0\n0", "4\n1\n1\n3\n17\n2\n6\n3\n8\n2\n0\n7\n2\n1\n0\n0", "4\n1\n2\n3\n12\n1\n6\n1\n8\n2\n0\n7\n2\n1\n0\n0", "4\n1\n1\n3\n17\n2\n6\n4\n8\n0\n0\n7\n4\n1\n0\n0", "4\n1\n2\n3\n12\n1\n6\n1\n8\n2\n1\n7\n2\n1\n0\n0", "4\n1\n1\n1\n12\n1\n6\n3\n9\n2\n7\n5\n2\n1\n0\n0", "4\n1\n0\n3\n17\n1\n6\n3\n8\n2\n0\n0\n2\n0\n0\n0"], "outputs": ["3\n72\n", "3\n5\n", "4\n72\n", "4\n5\n", "4\n192\n", "4\n4\n", "2\n72\n", "4\n19\n"]}
441
332
coding
Solve the programming task below in a Python markdown code block. Takahashi throws N dice, each having K sides with all integers from 1 to K. The dice are NOT pairwise distinguishable. For each i=2,3,...,2K, find the following value modulo 998244353: - The number of combinations of N sides shown by the dice such that the sum of no two different sides is i. Note that the dice are NOT distinguishable, that is, two combinations are considered different when there exists an integer k such that the number of dice showing k is different in those two. -----Constraints----- - 1 \leq K \leq 2000 - 2 \leq N \leq 2000 - K and N are integers. -----Input----- Input is given from Standard Input in the following format: K N -----Output----- Print 2K-1 integers. The t-th of them (1\leq t\leq 2K-1) should be the answer for i=t+1. -----Sample Input----- 3 3 -----Sample Output----- 7 7 4 7 7 - For i=2, the combinations (1,2,2),(1,2,3),(1,3,3),(2,2,2),(2,2,3),(2,3,3),(3,3,3) satisfy the condition, so the answer is 7. - For i=3, the combinations (1,1,1),(1,1,3),(1,3,3),(2,2,2),(2,2,3),(2,3,3),(3,3,3) satisfy the condition, so the answer is 7. - For i=4, the combinations (1,1,1),(1,1,2),(2,3,3),(3,3,3) satisfy the condition, so the answer is 4.
{"inputs": ["3 6", "1 5", "2 3", "5 3", "5 5", "3 5", "4 8", "6 6"], "outputs": ["13\n13\n4\n13\n13\n", "0\n", "2\n2\n2\n", "30\n30\n25\n25\n20\n25\n25\n30\n30\n", "91\n91\n61\n61\n36\n61\n61\n91\n91\n", "11\n11\n4\n11\n11\n", "81\n81\n32\n32\n32\n81\n81\n", "336\n336\n231\n231\n146\n146\n146\n231\n231\n336\n336\n"]}
420
217
coding
Solve the programming task below in a Python markdown code block. The squirrel Chokudai has N acorns. One day, he decides to do some trades in multiple precious metal exchanges to make more acorns. His plan is as follows: 1. Get out of the nest with N acorns in his hands. 2. Go to Exchange A and do some trades. 3. Go to Exchange B and do some trades. 4. Go to Exchange A and do some trades. 5. Go back to the nest. In Exchange X (X = A, B), he can perform the following operations any integer number of times (possibly zero) in any order: * Lose g_{X} acorns and gain 1 gram of gold. * Gain g_{X} acorns and lose 1 gram of gold. * Lose s_{X} acorns and gain 1 gram of silver. * Gain s_{X} acorns and lose 1 gram of silver. * Lose b_{X} acorns and gain 1 gram of bronze. * Gain b_{X} acorns and lose 1 gram of bronze. Naturally, he cannot perform an operation that would leave him with a negative amount of acorns, gold, silver, or bronze. What is the maximum number of acorns that he can bring to the nest? Note that gold, silver, or bronze brought to the nest would be worthless because he is just a squirrel. Constraints * 1 \leq N \leq 5000 * 1 \leq g_{X} \leq 5000 * 1 \leq s_{X} \leq 5000 * 1 \leq b_{X} \leq 5000 * All values in input are integers. Input Input is given from Standard Input in the following format: N g_A s_A b_A g_B s_B b_B Output Print the maximum number of acorns that Chokudai can bring to the nest. Example Input 23 1 1 1 2 1 1 Output 46
{"inputs": ["23\n1 1 1\n2 2 1", "23\n1 2 1\n2 1 1", "23\n1 2 1\n2 0 1", "23\n0 2 1\n2 0 1", "23\n1 1 2\n1 1 0", "23\n1 1 1\n2 0 1", "23\n1 4 1\n2 1 1", "23\n1 1 2\n2 3 0"], "outputs": ["46\n", "92\n", "48\n", "27\n", "25\n", "47\n", "184\n", "71\n"]}
451
175
coding
Solve the programming task below in a Python markdown code block. In this kata you need to build a function to return either `true/True` or `false/False` if a string can be seen as the repetition of a simpler/shorter subpattern or not. For example: ```cpp,java hasSubpattern("a") == false; //no repeated pattern hasSubpattern("aaaa") == true; //created repeating "a" hasSubpattern("abcd") == false; //no repeated pattern hasSubpattern("abababab") == true; //created repeating "ab" hasSubpattern("ababababa") == false; //cannot be entirely reproduced repeating a pattern ``` ```python has_subpattern("a") == False #no repeated pattern has_subpattern("aaaa") == True #created repeating "a" has_subpattern("abcd") == False #no repeated pattern has_subpattern("abababab") == True #created repeating "ab" has_subpattern("ababababa") == False #cannot be entirely reproduced repeating a pattern ``` Strings will never be empty and can be composed of any character (just consider upper- and lowercase letters as different entities) and can be pretty long (keep an eye on performances!). If you liked it, go for the [next kata](https://www.codewars.com/kata/string-subpattern-recognition-ii/) of the series! Also feel free to reuse/extend the following starter code: ```python def has_subpattern(string): ```
{"functional": "_inputs = [['a'], ['aaaa'], ['abcd'], ['abababab'], ['ababababa'], ['123a123a123a'], ['123A123a123a'], ['abbaabbaabba'], ['abbabbabba'], ['abcdabcabcd']]\n_outputs = [[False], [True], [False], [True], [False], [True], [False], [True], [False], [False]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(has_subpattern(*i), o[0])"}
321
247
coding
Solve the programming task below in a Python markdown code block. Read problems statements in mandarin chinese, russian and vietnamese as well. You are given N integers: A_{1}, A_{2}, ..., A_{N}. You need to count the number of pairs of indices (i, j) such that 1 ≤ i < j ≤ N and A_{i} | A_{j} ≤ max(A_{i}, A_{j}). Note: A_{i} | A_{j} refers to bitwise OR. ------ Input ------ The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows. The first line of each testcase contains a single integer: N The second line of each testcase contains N integers: A_{1}, A_{2}, ..., A_{N}. ------ Output ------ For each test case, output a single line containing the answer for that test case. ------ Constraints ------ $1 ≤ T ≤ 20$ $1 ≤ N ≤ 10^{6} $ $0 ≤ A_{i} ≤ 10^{6}$ $1 ≤ Sum of N over all test cases ≤ 10^{6} $ ------ Subtasks ------ Subtask #1 (20 points): $1 ≤ N ≤ 10^{3} $ Subtask #2 (80 points): $Original constraints$ ----- Sample Input 1 ------ 1 3 1 2 3 ----- Sample Output 1 ------ 2 ----- explanation 1 ------ There are three possible pairs of indices which satisfy 1 ? i j ? N: (1, 2), (1, 3) and (2, 3). Let us see which of those satisfy Ai | Aj ? max(Ai, Aj): (1, 2): A1 | A2 = 1 | 2 = (01)2 | (10)2 = (11)2 = 3. But max(A1, A2) = max(1, 2) = 2, and 3 ? 2 is not correct. Hence this is not a valid pair. (1, 3): A1 | A3 = 1 | 3 = (01)2 | (11)2 = (11)2 = 3. And max(A1, A3) = max(1, 3) = 3, and 3 ? 3 is correct. Hence this is a valid pair. (2, 3): A2 | A3 = 2 | 3 = (10)2 | (11)2 = (11)2 = 3. And max(A2, A3) = max(2, 3) = 3, and 3 ? 3 is correct. Hence this is a valid pair. So there are a total of 2 valid pairs, and hence the answer is 2.
{"inputs": ["1\n3\n1 2 3"], "outputs": ["2"]}
629
20
coding
Solve the programming task below in a Python markdown code block. A factory produces thimbles in bulk. Typically, it can produce up to a thimbles a day. However, some of the machinery is defective, so it can currently only produce b thimbles each day. The factory intends to choose a k-day period to do maintenance and construction; it cannot produce any thimbles during this time, but will be restored to its full production of a thimbles per day after the k days are complete. Initially, no orders are pending. The factory receives updates of the form di, ai, indicating that ai new orders have been placed for the di-th day. Each order requires a single thimble to be produced on precisely the specified day. The factory may opt to fill as many or as few of the orders in a single batch as it likes. As orders come in, the factory owner would like to know the maximum number of orders he will be able to fill if he starts repairs on a given day pi. Help the owner answer his questions. Input The first line contains five integers n, k, a, b, and q (1 ≤ k ≤ n ≤ 200 000, 1 ≤ b < a ≤ 10 000, 1 ≤ q ≤ 200 000) — the number of days, the length of the repair time, the production rates of the factory, and the number of updates, respectively. The next q lines contain the descriptions of the queries. Each query is of one of the following two forms: * 1 di ai (1 ≤ di ≤ n, 1 ≤ ai ≤ 10 000), representing an update of ai orders on day di, or * 2 pi (1 ≤ pi ≤ n - k + 1), representing a question: at the moment, how many orders could be filled if the factory decided to commence repairs on day pi? It's guaranteed that the input will contain at least one query of the second type. Output For each query of the second type, print a line containing a single integer — the maximum number of orders that the factory can fill over all n days. Examples Input 5 2 2 1 8 1 1 2 1 5 3 1 2 1 2 2 1 4 2 1 3 2 2 1 2 3 Output 3 6 4 Input 5 4 10 1 6 1 1 5 1 5 5 1 3 2 1 5 2 2 1 2 2 Output 7 1 Note Consider the first sample. We produce up to 1 thimble a day currently and will produce up to 2 thimbles a day after repairs. Repairs take 2 days. For the first question, we are able to fill 1 order on day 1, no orders on days 2 and 3 since we are repairing, no orders on day 4 since no thimbles have been ordered for that day, and 2 orders for day 5 since we are limited to our production capacity, for a total of 3 orders filled. For the third question, we are able to fill 1 order on day 1, 1 order on day 2, and 2 orders on day 5, for a total of 4 orders.
{"inputs": ["1 1 2 1 1\n2 1\n", "1 1 2 0 1\n2 1\n", "1 1 3 0 1\n2 1\n", "2 1 3 0 1\n2 1\n", "1 1 2 2 1\n2 1\n", "2 1 3 0 1\n2 2\n", "1 1 2 4 1\n2 1\n", "1 1 2 3 1\n2 1\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
737
166
coding
Solve the programming task below in a Python markdown code block. The country of Byteland contains N cities and N - 1 bidirectional roads between them such that there is a path between any two cities. The cities are numbered (0,...,N - 1). The people were very unhappy about the time it took to commute, especially salesmen who had to go about every city selling goods. So it was decided that new roads would be built between any two "somewhat near" cities. Any two cities in Bytleland that can be reached by traveling on exactly two old roads are known as "somewhat near" each other. Now a salesman situated in city 0, just like any other typical salesman, has to visit all cities exactly once and return back to city 0 in the end. In how many ways can he do this? Input Format The first line contains the number of test cases T. T test cases follow. The first line contains N, the number of cities in Byteland. The following N - 1 lines contain the description of the roads. The ith line contains two integers ai and bi, meaning that there was originally a road connecting cities with numbers ai and bi. Constraints 1 <= T <= 20 1 <= N <= 10000 0 <= ai,bi < N   Output Format Output T lines, one corresponding to each test case containing the required answer for that test case. Since the answers can be huge, output them modulo 1000000007. Sample Input 2 3 0 1 1 2 5 0 1 1 2 2 3 2 4 Sample Output 2 4 Explanation For the first case, a new road was build between cities 0 and 2. Now, the salesman has two tour possibilities: 0-1-2-0 or 0-2-1-0.
{"inputs": ["2\n3\n0 1\n1 2\n5\n0 1\n1 2\n2 3\n2 4\n"], "outputs": ["2\n4\n"]}
408
44
coding
Solve the programming task below in a Python markdown code block. You are given an array A of length N containing the elements -1 and 1 only. Determine if it is possible to rearrange the array A in such a way that A_{i} is not the geometric mean of A_{i-1} and A_{i+1}, for all i such that 2 ≤ i ≤ N-1. Y is said to be the geometric mean of X and Z if Y^{2} = X \cdot Z. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output Yes if it is possible to rearrange A in such a way that A_{i} is not the geometric mean of A_{i-1} and A_{i+1}, where 2 ≤ i ≤ N-1. Otherwise output No. You may print each character of Yes and No in uppercase or lowercase (for example, yes, yEs, YES will be considered identical). ------ Constraints ------ $1 ≤ T ≤ 200$ $3 ≤ N ≤ 1000$ $A_{i} \in \{ -1, 1 \}$ ----- Sample Input 1 ------ 3 5 1 1 1 -1 -1 3 1 1 1 6 1 -1 -1 -1 -1 1 ----- Sample Output 1 ------ Yes No Yes ----- explanation 1 ------ Test case 1: We can rearrange the array $A$ to $[1, 1, -1, -1, 1]$. One can see that ${A_{i}}^2 \neq A_{i-1} \cdot A_{i+1}$, for any $2 ≤ i ≤ N-1$. Test case 2: None of the rearrangements of $A$ satisy the given condition.
{"inputs": ["3\n5\n1 1 1 -1 -1\n3\n1 1 1\n6\n1 -1 -1 -1 -1 1\n"], "outputs": ["Yes\nNo\nYes\n"]}
478
52
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. There is an undirected tree with n nodes labeled from 0 to n - 1. You are given a 0-indexed integer array nums of length n where nums[i] represents the value of the ith node. You are also given a 2D integer array edges of length n - 1 where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree. You are allowed to delete some edges, splitting the tree into multiple connected components. Let the value of a component be the sum of all nums[i] for which node i is in the component. Return the maximum number of edges you can delete, such that every connected component in the tree has the same value.   Please complete the following python code precisely: ```python class Solution: def componentValue(self, nums: List[int], edges: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [6,2,2,2,6], edges = [[0,1],[1,2],[1,3],[3,4]] ) == 2 \n assert candidate(nums = [2], edges = []) == 0\n\n\ncheck(Solution().componentValue)"}
203
75
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Fruits are available at some positions on an infinite x-axis. You are given a 2D integer array fruits where fruits[i] = [positioni, amounti] depicts amounti fruits at the position positioni. fruits is already sorted by positioni in ascending order, and each positioni is unique. You are also given an integer startPos and an integer k. Initially, you are at the position startPos. From any position, you can either walk to the left or right. It takes one step to move one unit on the x-axis, and you can walk at most k steps in total. For every position you reach, you harvest all the fruits at that position, and the fruits will disappear from that position. Return the maximum total number of fruits you can harvest.   Please complete the following python code precisely: ```python class Solution: def maxTotalFruits(self, fruits: List[List[int]], startPos: int, k: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(fruits = [[2,8],[6,3],[8,6]], startPos = 5, k = 4) == 9\n assert candidate(fruits = [[0,9],[4,1],[5,7],[6,2],[7,4],[10,9]], startPos = 5, k = 4) == 14\n assert candidate(fruits = [[0,3],[6,4],[8,5]], startPos = 3, k = 2) == 0\n\n\ncheck(Solution().maxTotalFruits)"}
216
134
coding
Solve the programming task below in a Python markdown code block. Chef's dog Snuffles has so many things to play with! This time around, Snuffles has an array A containing N integers: A1, A2, ..., AN. Bad news: Snuffles only loves to play with an array in which all the elements are equal. Good news: We have a mover of size D. ! A mover of size D is a tool which helps to change arrays. Chef can pick two existing elements Ai and Aj from the array, such that i + D = j and subtract 1 from one of these elements (the element should have its value at least 1), and add 1 to the other element. In effect, a single operation of the mover, moves a value of 1 from one of the elements to the other. Chef wants to find the minimum number of times she needs to use the mover of size D to make all the elements of the array A equal. Help her find this out. -----Input----- - The first line of the input contains an integer T, denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two integers N and D, denoting the number of elements in the array and the size of the mover. - The second line of each testcase contains N space-separated integers: A1, A2, ..., AN, denoting the initial elements of the array. -----Output----- - For each test case, output a single line containing the minimum number of uses or -1 if it is impossible to do what Snuffles wants. -----Constraints----- - 1 ≤ T ≤ 10 - 2 ≤ N ≤ 105 - 1 ≤ D < N - 1 ≤ Ai ≤ 109 -----Subtasks----- - Subtask 1 (30 points) : N ≤ 103 - Subtask 2 (70 points) : Original constraints -----Example----- Input: 3 5 2 1 4 5 2 3 3 1 1 4 1 4 2 3 4 3 5 Output: 3 2 -1 -----Explanation----- Testcase 1: Here is a possible sequence of usages of the mover: - Move 1 from A3 to A1 - Move 1 from A3 to A1 - Move 1 from A2 to A4 At the end, the array becomes (3, 3, 3, 3, 3), which Snuffles likes. And you cannot achieve this in fewer moves. Hence the answer is 3. Testcase 2: Here is a possible sequence of usages of the mover: - Move 1 from A2 to A1 - Move 1 from A2 to A3 At the end, the array becomes (2, 2, 2), which Snuffles likes. And you cannot achieve this in fewer moves. Hence the answer is 2. Testcase 3: It is impossible to make all the elements equal. Hence the answer is -1.
{"inputs": ["3\n5 2\n1 4 5 2 3\n3 1\n1 4 1\n4 2\n3 4 3 5"], "outputs": ["3\n2\n-1"]}
660
53
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a string date representing a Gregorian calendar date formatted as YYYY-MM-DD, return the day number of the year.   Please complete the following python code precisely: ```python class Solution: def dayOfYear(self, date: str) -> int: ```
{"functional": "def check(candidate):\n assert candidate(date = \"2019-01-09\") == 9\n assert candidate(date = \"2019-02-10\") == 41\n\n\ncheck(Solution().dayOfYear)"}
71
62
coding
Solve the programming task below in a Python markdown code block. Polycarpus has a ribbon, its length is n. He wants to cut the ribbon in a way that fulfils the following two conditions: * After the cutting each ribbon piece should have length a, b or c. * After the cutting the number of ribbon pieces should be maximum. Help Polycarpus and find the number of ribbon pieces after the required cutting. Input The first line contains four space-separated integers n, a, b and c (1 ≤ n, a, b, c ≤ 4000) — the length of the original ribbon and the acceptable lengths of the ribbon pieces after the cutting, correspondingly. The numbers a, b and c can coincide. Output Print a single number — the maximum possible number of ribbon pieces. It is guaranteed that at least one correct ribbon cutting exists. Examples Input 5 5 3 2 Output 2 Input 7 5 5 2 Output 2 Note In the first example Polycarpus can cut the ribbon in such way: the first piece has length 2, the second piece has length 3. In the second example Polycarpus can cut the ribbon in such way: the first piece has length 5, the second piece has length 2.
{"inputs": ["4 4 4 4\n", "4 6 4 9\n", "8 3 8 4\n", "1 1 1 1\n", "2 2 9 6\n", "5 1 3 3\n", "6 2 4 1\n", "4 3 4 4\n"], "outputs": ["1\n", "1\n", "2\n", "1\n", "1\n", "5\n", "6\n", "1\n"]}
284
118
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given the root of a binary tree containing digits from 0 to 9 only. Each root-to-leaf path in the tree represents a number. For example, the root-to-leaf path 1 -> 2 -> 3 represents the number 123. Return the total sum of all root-to-leaf numbers. Test cases are generated so that the answer will fit in a 32-bit integer. A leaf node is a node with no children.   Please complete the following python code precisely: ```python # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def sumNumbers(self, root: Optional[TreeNode]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(root = tree_node([1,2,3])) == 25\n assert candidate(root = tree_node([4,9,0,5,1])) == 1026\n\n\ncheck(Solution().sumNumbers)"}
197
63
coding
Solve the programming task below in a Python markdown code block. If we write out the digits of "60" as English words we get "sixzero"; the number of letters in "sixzero" is seven. The number of letters in "seven" is five. The number of letters in "five" is four. The number of letters in "four" is four: we have reached a stable equilibrium. Note: for integers larger than 9, write out the names of each digit in a single word (instead of the proper name of the number in English). For example, write 12 as "onetwo" (instead of twelve), and 999 as "nineninenine" (instead of nine hundred and ninety-nine). For any integer between 0 and 999, return an array showing the path from that integer to a stable equilibrium: Also feel free to reuse/extend the following starter code: ```python def numbers_of_letters(n): ```
{"functional": "_inputs = [[1], [2], [3], [4], [12], [37], [311], [999]]\n_outputs = [[['one', 'three', 'five', 'four']], [['two', 'three', 'five', 'four']], [['three', 'five', 'four']], [['four']], [['onetwo', 'six', 'three', 'five', 'four']], [['threeseven', 'onezero', 'seven', 'five', 'four']], [['threeoneone', 'oneone', 'six', 'three', 'five', 'four']], [['nineninenine', 'onetwo', 'six', 'three', 'five', 'four']]]\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(numbers_of_letters(*i), o[0])"}
203
295
coding
Solve the programming task below in a Python markdown code block. There are $N$ cities on a circle, numbered $1$ through $N$. For each $i$ ($1 \le i \le N-1$), cities $i$ and $i+1$ are directly connected by a bidirectional road with length $A_i$, and cities $N$ and $1$ are also directly connected by a bidirectional road with length $A_N$. However, we do not know the lengths of some roads. For each city $i$, we do know that it has an opposite city — formally, there is a city $j \neq i$ such that the clockwise distance between cities $i$ and $j$ is equal to the counterclockwise distance between these cities. Please find the lengths of all roads in such a way that the above condition is satisfied and the sum of lengths of all roads is minimised. -----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 the input contains a single integer $N$. - The second line contains $N$ space-separated integers $A_1, A_2, \dots, A_N$. For each valid $i$, $A_i = -1$ denotes that the length of road $i$ is unknown. -----Output----- For each test case, print a line containing the string "NO" if there is no solution or "YES" otherwise. If a solution exists, print a second line containing $N$ space-separated positive integers — the lengths of all roads in your solution. Each of these integers should be $\le 10^9$. If there are multiple solutions, you may print any one. -----Constraints----- - $1 \le T \le 100$ - $3 \le N \le 10^5$ - $1 \le A_i \le 10^9$ or $A_i = -1$ for each valid $i$ - the sum of $N$ for all test cases does not exceed $3\cdot 10^5$ -----Subtasks----- Subtask #1 (10 points): $N \le 4$ Subtask #2 (20 points): $A_i = \pm 1$ for each valid $i$ Subtask #3 (70 points): original constraints -----Example Input----- 4 4 1 1 1 1 4 1 1 1 2 4 1 -1 -1 4 4 1 -1 2 -1 -----Example Output----- YES 1 1 1 1 NO YES 1 4 1 4 NO
{"inputs": ["4\n4\n1 1 1 1\n4\n1 1 1 2\n4\n1 -1 -1 4\n4\n1 -1 2 -1"], "outputs": ["YES\n1 1 1 1\nNO\nYES\n1 4 1 4\nNO"]}
587
74
coding
Solve the programming task below in a Python markdown code block. You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----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 A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
{"inputs": ["2\n2 3\n1 2\n3 2\n1 -2 1\n\n"], "outputs": ["9\n2"]}
466
34
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are an administrator of a popular quiz website. Every day, you hold a quiz on the website. There are $N$ users (numbered $1$ through $N$) and $M$ admins (numbered $N+1$ through $N+M$). For each quiz, each user is allowed to attempt it at most once. Sometimes, admins attempt the quizzes for testing purposes too; an admin may attempt a quiz any number of times. On a certain day, the quiz was attempted $K$ times. For each $i$ ($1 ≤ i ≤ K$), the $i$-th of these attempts was made by the person with number $L_{i}$. Find all the users who attempted the quiz more than once, in order to disqualify them. ------ 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 three space-separated integers $N$, $M$ and $K$. The second line contains $K$ space-separated integers $L_{1}, L_{2}, \ldots, L_{K}$. ------ Output ------ For each test case, print a single line containing an integer $D$ denoting the number of users who should be disqualified, followed by a space and $D$ space-separated integers denoting their numbers in ascending order. ------ Constraints ------ $1 ≤ T ≤ 10$ $0 ≤ N, M ≤ 10^{5}$ $1 ≤ N + M, K ≤ 10^{5}$ $1 ≤ L_{i} ≤ N + M$ for each valid $i$ ----- Sample Input 1 ------ 3 1 1 1 1 1 1 2 1 1 5 5 10 2 5 2 5 2 4 10 10 10 10 ----- Sample Output 1 ------ 0 1 1 2 2 5 ----- explanation 1 ------ Example case 1: Since there is only a single attempt during the whole day, there cannot be any user who made more than one attempt. Example case 2: User $1$ attempted the quiz twice. Example case 3: Users $2$ and $5$ attempted the quiz multiple times. The admin with number $10$ also attempted the quiz multiple times, but admins are allowed to do that.
{"inputs": ["3\n1 1 1\n1\n1 1 2\n1 1\n5 5 10\n2 5 2 5 2 4 10 10 10 10"], "outputs": ["0\n1 1\n2 2 5"]}
570
71
coding
Solve the programming task below in a Python markdown code block. You are given a square grid of size N, with rows numbered from 0 to N - 1 starting from the top and columns numbered from 0 to N - 1 starting from the left. A cell (u, v) refers to the cell that is on the u^{th} row and the v^{th} column. Each cell contains an integer - 0 or 1. You can pick any cell and flip the number in all the cells (including the picked cell) within the Manhattan distance D from the picked cell. A flip here means changing the number from 0 to 1 and vice-versa. The manhattan distance from the cell (u, v) to the cell (x, y) is equal to $|u-x|+|v-y|$ where $\left|i\right|$ is the absolute value of i. Your mission is to change all values in the grid to zero without using more than N×N flips. Input Format The first line of the input contains two integers N and D separated by a single space. Each line in the next N lines contains N integers separated by a single space which are either 0 or 1. the i_{th} number on the j_{th} line is the number on the cell (i - 1, j - 1) of the grid. Constraints 1 ≤ N ≤ 20 0 ≤ D ≤ 40 Output Format If there is no solution, your output should contain exactly a single string "Impossible" (without quotes). If a solution exists, print out the string "Possible" (without quotes) in the first line of your output. In the second line, print out an integer M which represent the number of operations that you need. Each line in the next M lines should contain a pair of integers separated by a single space representing the cell that you picked for the corresponding operation. Note that if there is more than one solution you can pick any one of them. Sample Input:#00 3 1 0 1 0 1 1 1 0 1 0 Sample Output:#00 Possible 1 1 1 Sample Input:#01 3 2 1 0 1 1 1 0 0 0 0 Sample Output:#01 Impossible Explanation In the first testcase, we can perform the first operation in the center cell, this will flip all the elements to 0 within 1 manhattan distance. In the second testcase, we cannot make it an all 0 matrix under 9 moves. Hence, Impossible.
{"inputs": ["3 1\n0 1 0\n1 1 1\n0 1 0\n", "3 2\n1 0 1 \n1 1 0 \n0 0 0 \n"], "outputs": ["Possible\n1\n1 1\n", "Impossible\n"]}
563
71
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an array of integers, return the maximum sum for a non-empty subarray (contiguous elements) with at most one element deletion. In other words, you want to choose a subarray and optionally delete one element from it so that there is still at least one element left and the sum of the remaining elements is maximum possible. Note that the subarray needs to be non-empty after deleting one element.   Please complete the following python code precisely: ```python class Solution: def maximumSum(self, arr: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(arr = [1,-2,0,3]) == 4\n assert candidate(arr = [1,-2,-2,3]) == 3\n assert candidate(arr = [-1,-1,-1,-1]) == -1\n\n\ncheck(Solution().maximumSum)"}
133
73
coding
Solve the programming task below in a Python markdown code block. This function should take two string parameters: a person's name (`name`) and a quote of theirs (`quote`), and return a string attributing the quote to the person in the following format: ```python '[name] said: "[quote]"' ``` For example, if `name` is `'Grae'` and `'quote'` is `'Practice makes perfect'` then your function should return the string ```python 'Grae said: "Practice makes perfect"' ``` Unfortunately, something is wrong with the instructions in the function body. Your job is to fix it so the function returns correctly formatted quotes. Click the "Train" button to get started, and be careful with your quotation marks. Also feel free to reuse/extend the following starter code: ```python def quotable(name, quote): ```
{"functional": "_inputs = [['Grae', 'Practice makes perfect'], ['Dan', 'Get back to work, Grae'], ['Alex', 'Python is great fun'], ['Bethany', 'Yes, way more fun than R'], ['Darrell', 'What the heck is this thing?']]\n_outputs = [['Grae said: \"Practice makes perfect\"'], ['Dan said: \"Get back to work, Grae\"'], ['Alex said: \"Python is great fun\"'], ['Bethany said: \"Yes, way more fun than R\"'], ['Darrell said: \"What the heck is this thing?\"']]\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(quotable(*i), o[0])"}
185
273
coding
Solve the programming task below in a Python markdown code block. The chef is trying to solve some pattern problems, Chef wants your help to code it. Chef has one number K to form a new pattern. Help the chef to code this pattern problem. -----Input:----- - First-line will contain $T$, the number of test cases. Then the test cases follow. - Each test case contains a single line of input, one integer $K$. -----Output:----- For each test case, output as the pattern. -----Constraints----- - $1 \leq T \leq 100$ - $1 \leq K \leq 100$ -----Sample Input:----- 3 2 3 4 -----Sample Output:----- 12 21 123 231 312 1234 2341 3412 4123 -----EXPLANATION:----- No need, else pattern can be decode easily.
{"inputs": ["3\n2\n3\n4"], "outputs": ["12\n21\n123\n231\n312\n1234\n2341\n3412\n4123"]}
212
54
coding
Solve the programming task below in a Python markdown code block. Let's call a set of positive integers a_1, a_2, ..., a_k quadratic if the product of the factorials of its elements is a square of an integer, i. e. ∏_{i=1}^{k} a_i! = m^2, for some integer m. You are given a positive integer n. Your task is to find a quadratic subset of a set 1, 2, ..., n of maximum size. If there are multiple answers, print any of them. Input A single line contains a single integer n (1 ≤ n ≤ 10^6). Output In the first line, print a single integer — the size of the maximum subset. In the second line, print the subset itself in an arbitrary order. Examples Input 1 Output 1 1 Input 4 Output 3 1 3 4 Input 7 Output 4 1 4 5 6 Input 9 Output 7 1 2 4 5 6 7 9
{"inputs": ["5\n", "2\n", "0\n", "9\n", "7\n", "1\n", "4\n", "27\n"], "outputs": ["3\n1 3 4\n", "1\n1\n", "0\n\n", "7\n1 2 4 5 6 7 8 ", "4\n1 4 5 6 ", "1\n1 ", "3\n1 3 4 ", "25\n1 2 3 4 5 6 7 8 9 10 12 13 14 15 16 17 18 19 20 21 22 23 24 26 27 "]}
236
173
coding
Solve the programming task below in a Python markdown code block. Mr. Chanek is currently participating in a science fair that is popular in town. He finds an exciting puzzle in the fair and wants to solve it. There are $N$ atoms numbered from $1$ to $N$. These atoms are especially quirky. Initially, each atom is in normal state. Each atom can be in an excited. Exciting atom $i$ requires $D_i$ energy. When atom $i$ is excited, it will give $A_i$ energy. You can excite any number of atoms (including zero). These atoms also form a peculiar one-way bond. For each $i$, $(1 \le i < N)$, if atom $i$ is excited, atom $E_i$ will also be excited at no cost. Initially, $E_i$ = $i+1$. Note that atom $N$ cannot form a bond to any atom. Mr. Chanek must change exactly $K$ bonds. Exactly $K$ times, Mr. Chanek chooses an atom $i$, $(1 \le i < N)$ and changes $E_i$ to a different value other than $i$ and the current $E_i$. Note that an atom's bond can remain unchanged or changed more than once. Help Mr. Chanek determine the maximum energy that he can achieve! note: You must first change exactly $K$ bonds before you can start exciting atoms. -----Input----- The first line contains two integers $N$ $K$ $(4 \le N \le 10^5, 0 \le K < N)$, the number of atoms, and the number of bonds that must be changed. The second line contains $N$ integers $A_i$ $(1 \le A_i \le 10^6)$, which denotes the energy given by atom $i$ when on excited state. The third line contains $N$ integers $D_i$ $(1 \le D_i \le 10^6)$, which denotes the energy needed to excite atom $i$. -----Output----- A line with an integer that denotes the maximum number of energy that Mr. Chanek can get. -----Example----- Input 6 1 5 6 7 8 10 2 3 5 6 7 1 10 Output 35 -----Note----- An optimal solution to change $E_5$ to 1 and then excite atom 5 with energy 1. It will cause atoms 1, 2, 3, 4, 5 be excited. The total energy gained by Mr. Chanek is (5 + 6 + 7 + 8 + 10) - 1 = 35. Another possible way is to change $E_3$ to 1 and then exciting atom 3 (which will excite atom 1, 2, 3) and exciting atom 4 (which will excite atom 4, 5, 6). The total energy gained by Mr. Chanek is (5 + 6 + 7 + 8 + 10 + 2) - (6 + 7) = 25 which is not optimal.
{"inputs": ["4 2\n1 2 4 8\n1 5 3 5\n", "4 2\n1 2 4 8\n5 1 3 5\n", "4 2\n1 2 4 8\n5 3 1 5\n", "4 2\n1 2 4 8\n6 5 3 1\n", "4 3\n1 2 4 8\n1 5 3 5\n", "4 3\n1 2 4 8\n5 1 3 5\n", "4 3\n1 2 4 8\n5 3 1 5\n", "4 3\n1 2 4 8\n6 5 3 1\n"], "outputs": ["14\n", "14\n", "14\n", "12\n", "14\n", "14\n", "14\n", "12\n"]}
680
222
coding
Solve the programming task below in a Python markdown code block. Efim just received his grade for the last test. He studies in a special school and his grade can be equal to any positive decimal fraction. First he got disappointed, as he expected a way more pleasant result. Then, he developed a tricky plan. Each second, he can ask his teacher to round the grade at any place after the decimal point (also, he can ask to round to the nearest integer). There are t seconds left till the end of the break, so Efim has to act fast. Help him find what is the maximum grade he can get in no more than t seconds. Note, that he can choose to not use all t seconds. Moreover, he can even choose to not round the grade at all. In this problem, classic rounding rules are used: while rounding number to the n-th digit one has to take a look at the digit n + 1. If it is less than 5 than the n-th digit remain unchanged while all subsequent digits are replaced with 0. Otherwise, if the n + 1 digit is greater or equal to 5, the digit at the position n is increased by 1 (this might also change some other digits, if this one was equal to 9) and all subsequent digits are replaced with 0. At the end, all trailing zeroes are thrown away. For example, if the number 1.14 is rounded to the first decimal place, the result is 1.1, while if we round 1.5 to the nearest integer, the result is 2. Rounding number 1.299996121 in the fifth decimal place will result in number 1.3. Input The first line of the input contains two integers n and t (1 ≤ n ≤ 200 000, 1 ≤ t ≤ 109) — the length of Efim's grade and the number of seconds till the end of the break respectively. The second line contains the grade itself. It's guaranteed that the grade is a positive number, containing at least one digit after the decimal points, and it's representation doesn't finish with 0. Output Print the maximum grade that Efim can get in t seconds. Do not print trailing zeroes. Examples Input 6 1 10.245 Output 10.25 Input 6 2 10.245 Output 10.3 Input 3 100 9.2 Output 9.2 Note In the first two samples Efim initially has grade 10.245. During the first second Efim can obtain grade 10.25, and then 10.3 during the next second. Note, that the answer 10.30 will be considered incorrect. In the third sample the optimal strategy is to not perform any rounding at all.
{"inputs": ["3 1\n0.1\n", "3 1\n0.9\n", "3 3\n9.9\n", "3 1\n9.5\n", "3 1\n9.9\n", "3 2\n0.9\n", "3 0\n0.1\n", "3 2\n9.5\n"], "outputs": ["0.1", "1", "10", "10", "10", "1", "0.1\n", "10"]}
622
119
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese and Russian as well. Euler's phi function for a positive integer N is usually denoted as φ(N) and defined as the number of positive integers less than or equal to N that are coprime with N. Let's call a positive integer N a super number if N can be divided by φ(N) without a remainder. e.g. 2 is a super number (since 2 mod φ(2) = 0), while 3 is not (since 3 mod φ(3) = 1). You are given two positive integers L and R. Your task is to find count of super numbers in the range [L, R]. ------ Input ------ The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. Each test case is described by a single line containing two positive integers L and R. ------ Output ------ For each test case, output a single line containing one integer: the number of super numbers in the range. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ L ≤ R ≤ 10^{18}$ ----- Sample Input 1 ------ 3 2 3 90 95 12 21 ----- Sample Output 1 ------ 1 0 3 ----- explanation 1 ------ In the first example, 2 is a super number while 3 is not (as explained in the statement). So, the number of super numbers in the range [2, 3] will be equal to 1.
{"inputs": ["3\n4 3\n1 57\n8 23", "3\n4 3\n90 95\n1 21", "3\n4 3\n90 95\n4 21", "3\n4 5\n90 89\n8 21", "3\n1 3\n86 95\n12 8", "3\n4 5\n90 89\n8 11", "3\n2 3\n90 95\n6 21", "3\n4 5\n90 95\n1 21"], "outputs": ["0\n13\n4\n", "0\n0\n8\n", "0\n0\n6\n", "1\n0\n4\n", "2\n0\n0\n", "1\n0\n1\n", "1\n0\n5\n", "1\n0\n8\n"]}
351
214
coding
Solve the programming task below in a Python markdown code block. In a New York City coffee house called Central Perk, we're introduced to six friends: chef Monica Geller, data controller Chandler Bing who lives across the hall from Monica, Chandler's roommate/actor Joey Tribbiani, Monica's brother Ross Geller, new ageist Phoebe Buffay, and "born to be a wife" Rachel Green. Chef wants to divide N cookies between the six friends such that: Each friend gets at least one cookie. No two friends get the same number of cookies. Find if it is possible for Chef to do so. ------ Input Format ------ - First line will contain T, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, one integers N. ------ Output Format ------ For each test case print "YES" if Chef can divide the cookies according to the given rules, else print "NO". You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10^{3}$ $1 ≤ N ≤ 10^{3}$ ----- Sample Input 1 ------ 3 42 5 909 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Test case 1: One possible way of distribution would be $2, 4, 6, 8, 10, 12$. Test case 2: It is impossible to divide $5$ cookies among six friends according to given rules.
{"inputs": ["3\n42\n5\n909"], "outputs": ["YES\nNO\nYES"]}
355
25
coding
Solve the programming task below in a Python markdown code block. Alan decided to get in shape for the summer, so he created a precise workout plan to follow. His plan is to go to a different gym every day during the next N days and lift $X[i]$ grams on day $i$. In order to improve his workout performance at the gym, he can buy exactly one pre-workout drink at the gym he is currently in and it will improve his performance by $A$ grams permanently and immediately. In different gyms these pre-workout drinks can cost different amounts $C[i]$ because of the taste and the gym's location but its permanent workout gains are the same. Before the first day of starting his workout plan, Alan knows he can lift a maximum of $K$ grams. Help Alan spend a minimum total amount of money in order to reach his workout plan. If there is no way for him to complete his workout plan successfully output $-1$. -----Input----- The first one contains two integer numbers, integers $N$ $(1 \leq N \leq 10^5)$ and $K$ $(1 \leq K \leq 10^5)$ – representing number of days in the workout plan and how many grams he can lift before starting his workout plan respectively. The second line contains $N$ integer numbers $X[i]$ $(1 \leq X[i] \leq 10^9)$ separated by a single space representing how many grams Alan wants to lift on day $i$. The third line contains one integer number $A$ $(1 \leq A \leq 10^9)$ representing permanent performance gains from a single drink. The last line contains $N$ integer numbers $C[i]$ $(1 \leq C[i] \leq 10^9)$ , representing cost of performance booster drink in the gym he visits on day $i$. -----Output----- One integer number representing minimal money spent to finish his workout plan. If he cannot finish his workout plan, output -1. -----Examples----- Input 5 10000 10000 30000 30000 40000 20000 20000 5 2 8 3 6 Output 5 Input 5 10000 10000 40000 30000 30000 20000 10000 5 2 8 3 6 Output -1 -----Note----- First example: After buying drinks on days 2 and 4 Alan can finish his workout plan. Second example: Alan cannot lift 40000 grams on day 2.
{"inputs": ["5 46\n38 4 11 1 49\n100\n13 55 29 31 977\n", "5 46\n38 4 11 1 49\n100\n135 55 29 31 977\n", "5 49\n38 23 11 1 49\n50\n135 55 24 31 977\n", "5 49\n38 4 11 1 49\n100\n135 55 24 31 977\n", "5 49\n38 4 11 1 49\n100\n135 55 29 31 977\n", "5 1\n1 1 1 2 9\n1000000000\n10 20 30 7 50\n", "5 49\n38 8 11 17 49\n50\n135 55 24 31 977\n", "4 49\n38 23 11 1 49\n50\n135 55 24 31 977\n"], "outputs": ["13\n", "29\n", "0\n", "0\n", "0\n", "7\n", "0\n", "0\n"]}
592
367
coding
Solve the programming task below in a Python markdown code block. Lots of geeky customers visit our chef's restaurant everyday. So, when asked to fill the feedback form, these customers represent the feedback using a binary string (i.e a string that contains only characters '0' and '1'. Now since chef is not that great in deciphering binary strings, he has decided the following criteria to classify the feedback as Good or Bad : If the string contains the substring "010" or "101", then the feedback is Good, else it is Bad. Note that, to be Good it is not necessary to have both of them as substring. So given some binary strings, you need to output whether according to the chef, the strings are Good or Bad. -----Input----- The first line contains an integer T denoting the number of feedbacks. Each of the next T lines contains a string composed of only '0' and '1'. -----Output----- For every test case, print in a single line Good or Bad as per the Chef's method of classification. -----Constraints----- - 1 ≤ T ≤ 100 - 1 ≤ |S| ≤ 105 Sum of length of all strings in one test file will not exceed 6*106. -----Example----- Input: 2 11111110 10101010101010 Output: Bad Good -----Explanation----- Example case 1. The string doesn't contain 010 or 101 as substrings. Example case 2. The string contains both 010 and 101 as substrings.
{"inputs": ["2\n11111110\n10101010101010", "2\n11101110\n10101010101010", "2\n11111110\n10101010111010", "2\n01010010\n10000000111111", "2\n11111111\n11000001111100", "2\n11101110\n10101010111010", "2\n11111110\n10100010111010", "2\n01111110\n10100010111010"], "outputs": ["Bad\nGood", "Good\nGood\n", "Bad\nGood\n", "Good\nBad\n", "Bad\nBad\n", "Good\nGood\n", "Bad\nGood\n", "Bad\nGood\n"]}
363
269
coding
Solve the programming task below in a Python markdown code block. Given are 1-digit positive integers a and b. Consider these two strings: the concatenation of b copies of the digit a, and the concatenation of a copies of the digit b. Which of these is lexicographically smaller? -----Constraints----- - 1 \leq a \leq 9 - 1 \leq b \leq 9 - a and b are integers. -----Input----- Input is given from Standard Input in the following format: a b -----Output----- Print the lexicographically smaller of the two strings. (If the two strings are equal, print one of them.) -----Sample Input----- 4 3 -----Sample Output----- 3333 We have two strings 444 and 3333. Between them, 3333 is the lexicographically smaller.
{"inputs": ["7 2", "4 6", "7 4", "7 8", "5 7", "1 3", "5 2", "6 6"], "outputs": ["2222222\n", "444444\n", "4444444\n", "77777777\n", "5555555\n", "111\n", "22222\n", "666666\n"]}
188
119
coding
Solve the programming task below in a Python markdown code block. The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness". The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves. Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case? Input The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i. Output Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator. Examples Input 2 1 1 Output 2 Input 2 2 2 Output 5 Input 1 10 Output 10 Note Note to the second sample. In the worst-case scenario you will need five clicks: * the first click selects the first variant to the first question, this answer turns out to be wrong. * the second click selects the second variant to the first question, it proves correct and we move on to the second question; * the third click selects the first variant to the second question, it is wrong and we go back to question 1; * the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; * the fifth click selects the second variant to the second question, it proves correct, the test is finished.
{"inputs": ["1\n18\n", "1\n24\n", "1\n29\n", "1\n46\n", "1\n27\n", "1\n10\n", "2\n2 1\n", "2\n2 2\n"], "outputs": ["18\n", "24\n", "29\n", "46\n", "27\n", "10", "3\n", "5"]}
545
100