task_type
stringclasses
1 value
problem
stringlengths
261
3.34k
answer
stringlengths
35
6.15k
problem_tokens
int64
62
774
answer_tokens
int64
12
2.04k
coding
Solve the programming task below in a Python markdown code block. Slime has a sequence of positive integers $a_1, a_2, \ldots, a_n$. In one operation Orac can choose an arbitrary subsegment $[l \ldots r]$ of this sequence and replace all values $a_l, a_{l + 1}, \ldots, a_r$ to the value of median of $\{a_l, a_{l + 1}, \ldots, a_r\}$. In this problem, for the integer multiset $s$, the median of $s$ is equal to the $\lfloor \frac{|s|+1}{2}\rfloor$-th smallest number in it. For example, the median of $\{1,4,4,6,5\}$ is $4$, and the median of $\{1,7,5,8\}$ is $5$. Slime wants Orac to make $a_1 = a_2 = \ldots = a_n = k$ using these operations. Orac thinks that it is impossible, and he does not want to waste his time, so he decided to ask you if it is possible to satisfy the Slime's requirement, he may ask you these questions several times. -----Input----- The first line of the input is a single integer $t$: the number of queries. The first line of each query contains two integers $n\ (1\le n\le 100\,000)$ and $k\ (1\le k\le 10^9)$, the second line contains $n$ positive integers $a_1,a_2,\dots,a_n\ (1\le a_i\le 10^9)$ The total sum of $n$ is at most $100\,000$. -----Output----- The output should contain $t$ lines. The $i$-th line should be equal to 'yes' if it is possible to make all integers $k$ in some number of operations or 'no', otherwise. You can print each letter in lowercase or uppercase. -----Example----- Input 5 5 3 1 5 2 6 1 1 6 6 3 2 1 2 3 4 3 3 1 2 3 10 3 1 2 3 4 5 6 7 8 9 10 Output no yes yes no yes -----Note----- In the first query, Orac can't turn all elements into $3$. In the second query, $a_1=6$ is already satisfied. In the third query, Orac can select the complete array and turn all elements into $2$. In the fourth query, Orac can't turn all elements into $3$. In the fifth query, Orac can select $[1,6]$ at first and then select $[2,10]$.
{"inputs": ["1\n5 2\n2 1 5 1 1\n", "1\n5 2\n2 1 5 1 1\n", "1\n5 2\n4 1 5 1 1\n", "1\n5 2\n4 1 5 2 1\n", "1\n6 3\n3 1 1 6 1 6\n", "1\n6 3\n4 1 4 1 1 3\n", "1\n6 2\n3 1 3 1 1 2\n", "1\n6 3\n5 1 5 2 2 3\n"], "outputs": ["yes\n", "yes\n", "no\n", "yes\n", "yes\n", "yes\n", "yes\n", "yes\n"]}
643
190
coding
Solve the programming task below in a Python markdown code block. In order to write a string, Atilla needs to first learn all letters that are contained in the string. Atilla needs to write a message which can be represented as a string $s$. He asks you what is the minimum alphabet size required so that one can write this message. The alphabet of size $x$ ($1 \leq x \leq 26$) contains only the first $x$ Latin letters. For example an alphabet of size $4$ contains only the characters ${a}$, ${b}$, ${c}$ and ${d}$. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 100$) — the length of the string. The second line of each test case contains a string $s$ of length $n$, consisting of lowercase Latin letters. -----Output----- For each test case, output a single integer — the minimum alphabet size required to so that Atilla can write his message $s$. -----Examples----- Input 5 1 a 4 down 10 codeforces 3 bcf 5 zzzzz Output 1 23 19 6 26 -----Note----- For the first test case, Atilla needs to know only the character ${a}$, so the alphabet of size $1$ which only contains ${a}$ is enough. For the second test case, Atilla needs to know the characters ${d}$, ${o}$, ${w}$, ${n}$. The smallest alphabet size that contains all of them is $23$ (such alphabet can be represented as the string ${abcdefghijklmnopqrstuvw}$).
{"inputs": ["5\n1\na\n4\ndown\n10\ncodeforces\n3\nbcf\n5\nzzzzz\n"], "outputs": ["1\n23\n19\n6\n26\n"]}
400
52
coding
Solve the programming task below in a Python markdown code block. You are playing a game where your character should overcome different obstacles. The current problem is to come down from a cliff. The cliff has height $h$, and there is a moving platform on each height $x$ from $1$ to $h$. Each platform is either hidden inside the cliff or moved out. At first, there are $n$ moved out platforms on heights $p_1, p_2, \dots, p_n$. The platform on height $h$ is moved out (and the character is initially standing there). If you character is standing on some moved out platform on height $x$, then he can pull a special lever, which switches the state of two platforms: on height $x$ and $x - 1$. In other words, the platform you are currently standing on will hide in the cliff and the platform one unit below will change it state: it will hide if it was moved out or move out if it was hidden. In the second case, you will safely land on it. Note that this is the only way to move from one platform to another. Your character is quite fragile, so it can safely fall from the height no more than $2$. In other words falling from the platform $x$ to platform $x - 2$ is okay, but falling from $x$ to $x - 3$ (or lower) is certain death. Sometimes it's not possible to come down from the cliff, but you can always buy (for donate currency) several magic crystals. Each magic crystal can be used to change the state of any single platform (except platform on height $h$, which is unaffected by the crystals). After being used, the crystal disappears. What is the minimum number of magic crystal you need to buy to safely land on the $0$ ground level? -----Input----- The first line contains one integer $q$ ($1 \le q \le 100$) — the number of queries. Each query contains two lines and is independent of all other queries. The first line of each query contains two integers $h$ and $n$ ($1 \le h \le 10^9$, $1 \le n \le \min(h, 2 \cdot 10^5)$) — the height of the cliff and the number of moved out platforms. The second line contains $n$ integers $p_1, p_2, \dots, p_n$ ($h = p_1 > p_2 > \dots > p_n \ge 1$) — the corresponding moved out platforms in the descending order of their heights. The sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer — the minimum number of magic crystals you have to spend to safely come down on the ground level (with height $0$). -----Example----- Input 4 3 2 3 1 8 6 8 7 6 5 3 2 9 6 9 8 5 4 3 1 1 1 1 Output 0 1 2 0
{"inputs": ["1\n3 2\n3 2\n", "1\n3 2\n3 2\n", "1\n3 2\n3 1\n", "1\n6 2\n6 2\n", "1\n6 2\n6 1\n", "1\n6 2\n6 3\n", "4\n3 2\n3 1\n8 6\n8 7 6 5 3 2\n9 6\n9 8 5 4 3 1\n1 1\n1\n", "4\n1 1\n1\n3 2\n3 1\n8 6\n8 7 6 5 3 2\n9 6\n9 8 5 4 3 1\n"], "outputs": ["1\n", "1\n", "0\n", "1\n", "0\n", "1\n", "0\n1\n2\n0\n", "0\n0\n1\n2\n"]}
674
222
coding
Solve the programming task below in a Python markdown code block. Given an array of integers, find the one that appears an odd number of times. There will always be only one integer that appears an odd number of times. Also feel free to reuse/extend the following starter code: ```python def find_it(seq): ```
{"functional": "_inputs = [[[20, 1, -1, 2, -2, 3, 3, 5, 5, 1, 2, 4, 20, 4, -1, -2, 5]], [[1, 1, 2, -2, 5, 2, 4, 4, -1, -2, 5]], [[20, 1, 1, 2, 2, 3, 3, 5, 5, 4, 20, 4, 5]], [[10]], [[1, 1, 1, 1, 1, 1, 10, 1, 1, 1, 1]]]\n_outputs = [[5], [-1], [5], [10], [10]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(find_it(*i), o[0])"}
66
330
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 ≤ 10^5) — 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- 2\n", "1 1\n+ 1\n", "3 1\n- 2\n", "3 1\n- 3\n", "20 1\n- 16\n", "20 1\n- 16\n"], "outputs": ["1\n1 ", "2\n1 2 ", "2\n1 2 \n", "1\n1 \n", "3\n1 2 3\n", "3\n1 2 3\n", "20\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ", "20\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 \n"]}
614
250
coding
Solve the programming task below in a Python markdown code block. Ambar is a gardener and have many water jugs in his garden. The shape of water jug is a cone placed on the top of a cylinder (the radius and height of cylinder and cone and is "r"). There is a jug for each value of "r'. "r" varies from 1 to "n" ("n" being the input). Help Ambar in finding the cumulative sum of volumes. Answer should be rounded off. value of pi 3.141592653589793 Input : 100 NOTE : You do not need to create a program for this problem you have to write your answers of given input in given code snippet To see how to submit solution please check this link SAMPLE INPUT 1 SAMPLE OUTPUT 4
{"inputs": ["100"], "outputs": ["106824622"]}
181
22
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 with no duplicates. A maximum binary tree can be built recursively from nums using the following algorithm: Create a root node whose value is the maximum value in nums. Recursively build the left subtree on the subarray prefix to the left of the maximum value. Recursively build the right subtree on the subarray suffix to the right of the maximum value. Return the maximum binary tree built from nums.   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 constructMaximumBinaryTree(self, nums: List[int]) -> Optional[TreeNode]: ```
{"functional": "def check(candidate):\n assert is_same_tree(candidate(nums = [3,2,1,6,0,5]), tree_node([6,3,5,None,2,0,None,None,1]))\n assert is_same_tree(candidate(nums = [3,2,1]), tree_node([3,None,2,None,1]))\n\n\ncheck(Solution().constructMaximumBinaryTree)"}
189
90
coding
Solve the programming task below in a Python markdown code block. For a binary string S, define its beauty to be the maximum number of [substrings] S can be partitioned into, such that each substring has strictly more ones than zeroes. For example, the beauty of 11011 is 3, as it can be partitioned as [110][1][1]. If such a partition doesn't exist, the beauty is defined to be 0. Also, define the happiness of a binary string to be the maximum beauty over all its substrings. For example, the happiness of the string S = 010011011 is 3, which is the beauty of its substring 11011. You are given N binary strings S_{1}, S_{2}, \ldots, S_{N}. You have to process Q queries: - Each query consists of two integers L, R, where 1 ≤ L ≤ R ≤ N. - The result of the query is the maximum possible happiness of S_{p_{1}} + S_{p_{2}} + \ldots S_{p_{(R - L + 1)}}, where p is some permutation of \{L, L + 1, \ldots, R - 1, R\} and + denotes the concatenation operation. ------ Input Format ------ The first line of input contains two space-separated integers N and Q, the number of strings and the number of queries. Then, 2N + Q lines follow: - For each 1 ≤ i ≤ N, S_{i} is represented by two lines. The first line contains the length of S_{i} and the second line contains S_{i} itself. - Each of the next Q lines contains two integers, L and R, denoting the query parameters ------ Output Format ------ For each query, print the maximum possible happiness of the concatenation of some permutation of the given interval on a new line. ------ Constraints ------ $1 ≤ N, Q ≤ 10^{5}$ $|S_{i}| ≥ 1$ for each valid $i$. - Every string consists of characters which are either '0' or '1'. - The sum of the lengths of all the input strings doesn't exceed $10^{6}$. ------ subtasks ------ Subtask 1 (30 points): The sum of $R L + 1$ over all queries doesn't exceed $10^{6}$. Subtask 3 (70 points): No further constraints. ----- Sample Input 1 ------ 4 1 1 0 2 11 3 010 4 0110 2 4 ----- Sample Output 1 ------ 3 ----- explanation 1 ------ It is optimal to concatenate as $S_{3} + S_{4} + S_{2}$ to get $010011011$ with a happiness value of $3$.
{"inputs": ["4 1\n1\n0\n2\n11\n3\n010\n4\n0110\n2 4"], "outputs": ["3"]}
633
40
coding
Solve the programming task below in a Python markdown code block. In a classic chase, Tom is running after Jerry as Jerry has eaten Tom's favourite food. Jerry is running at a speed of X metres per second while Tom is chasing him at a speed of Y metres per second. Determine whether Tom will be able to catch Jerry. Note that initially Jerry is not at the same position as Tom. ------ 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 speeds of Jerry and Tom respectively. ------ Output Format ------ For each test case, output on a new line, YES, if Tom will be able to catch Jerry. Otherwise, output NO. You can print each character in uppercase or lowercase. For example NO, no, No, and nO are all considered the same. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤X, Y ≤10$ ----- Sample Input 1 ------ 4 2 3 4 1 1 1 3 5 ----- Sample Output 1 ------ YES NO NO YES ----- explanation 1 ------ Test case $1$: Jerry is running at the speed of $2$ metres per second while Tom is chasing him at the speed of $3$ metres per second. Since Jerry's speed is less than Tom's, Tom will eventually catch Jerry. Test case $2$: Jerry is running at the speed of $4$ metres per second while Tom is chasing him at the speed of $1$ metres per second. Since Jerry's speed is higher than Tom's, Tom will never be able to catch Jerry. Test case $3$: Jerry is running at the speed of $1$ metre per second while Tom is chasing him at the speed of $1$ metre per second. Since Jerry's speed is same as that of Tom's and both of them are not at the same position, Tom will never be able to catch Jerry. Test case $4$: Jerry is running at the speed of $3$ metres per second while Tom is chasing him at the speed of $5$ metres per second. Since Jerry's speed is less than Tom's, Tom will eventually catch Jerry.
{"inputs": ["4\n2 3\n4 1\n1 1\n3 5\n"], "outputs": ["YES\nNO\nNO\nYES\n"]}
475
36
coding
Solve the programming task below in a Python markdown code block. Indraneel's student has given him data from two sets of experiments that the student has performed. Indraneel wants to establish a correlation between the two sets of data. Each data set is a sequence of $N$ numbers. The two data sets do not match number for number, but Indraneel believes that this is because data has been shifted due to inexact tuning of the equipment. For example, consider the following two sequences: $ $ 3 8 4 23 9 11 28 2 3 22 26 8 16 12 $ $ Indraneel observes that if we consider the subsequences $3,4,23,9$ and $2,3,22,8$ and examine their successive differences we get $1,19,-14$. He considers these two subsequences to be "identical". He would like to find the longest such pair of subsequences so that the successive differences are identical. Your task is to help him do this. -----Input:----- The first line of the input will contain a single integer $N$ indicating the number of data points in each of Indraneel's student's data sets. This is followed by two lines, each containing $N$ integers. -----Output:----- The output consists of three lines. The first line of output contains a single integer indicating the length of the longest pair of subsequences (one from each sequence) that has identical successive differences. This is followed by two lines each containing the corresponding subsequences. If there is more than one answer, it suffices to print one. -----Constraints:----- - $1 \leq N \leq 150$. - $0 \leq$ Each data point $\leq 1000$ -----Sample Input----- 7 3 8 4 23 9 11 28 2 3 22 26 8 16 12 -----Sample Output----- 4 3 4 23 9 2 3 22 8
{"inputs": ["7\n3 8 4 23 9 11 28\n2 3 22 26 8 16 12"], "outputs": ["4\n3 4 23 9\n2 3 22 8"]}
470
65
coding
Solve the programming task below in a Python markdown code block. Ahmed Gafer failed to pass the test, but he got the job because of his friendship with Said and Shahhoud. After working in the kitchen for a while, he blew it. The customers didn't like the food anymore and one day he even burned the kitchen. Now the master Chef is very upset. Ahmed isn't useful anymore at being a co-Chef, so S&S decided to give him a last chance. They decided to give Ahmed a new job, and make him work as the cashier of the restaurant. Nevertheless, in order not to repeat their previous mistake, they decided to give him a little test to check if his counting skills are good enough for the job. The problem is as follows: Given a string A of lowercase English letters, Ahmad was asked to find the number of good substrings. A substring A[L, R] is good if: - The length of the substring is exactly 2 and AL = AR, OR - The length of the substring is greater than 2,AL = AR and the substring A[L + 1, R - 1] has only one distinct letter. Anyways, Ahmed struggled trying to find a solution for the problem. Since his mathematical skills are very poor as it turned out, he decided to cheat and contacted you asking for your help. Can you help him in this challenge? -----Input----- The first line of the input contains the integer T, indicating the number of test cases. Each of the following T lines, contains a string A. -----Output----- For each test case, output a single line containing a single number, indicating the number of good substrings. -----Constraints----- - 1 ≤ T ≤ 100 - 1 ≤ |A| ≤ 105 - It's guaranteed that the sum of |A| over all test cases doesn't exceed 5x105. -----Example----- Input: 2 a abba Output: 0 2 -----Explanation----- Example case 2. The good substrings of abba are: { bb } and { abba }.
{"inputs": ["2\na\nabba"], "outputs": ["0\n2"]}
461
19
coding
Solve the programming task below in a Python markdown code block. The Binomial Form of a polynomial has many uses, just as the standard form does. For comparison, if p(x) is in Binomial Form and q(x) is in standard form, we might write p(x) := a0 \* xC0 + a1 \* xC1 + a2 \* xC2 + ... + aN \* xCN q(x) := b0 + b1 \* x + b2 \* x^(2) + ... + bN \* x^(N) Both forms have tricks for evaluating them, but tricks should not be necessary. The most important thing to keep in mind is that aCb can be defined for non-integer values of a; in particular, ``` aCb := a * (a-1) * (a-2) * ... * (a-b+1) / b! // for any value a and integer values b := a! / ((a-b)!b!) // for integer values a,b ``` The inputs to your function are an array which specifies a polynomial in Binomial Form, ordered by highest-degree-first, and also a number to evaluate the polynomial at. An example call might be ```python value_at([1, 2, 7], 3) ``` and the return value would be 16, since 3C2 + 2 * 3C1 + 7 = 16. In more detail, this calculation looks like ``` 1 * xC2 + 2 * xC1 + 7 * xC0 :: x = 3 3C2 + 2 * 3C1 + 7 3 * (3-1) / 2! + 2 * 3 / 1! + 7 3 + 6 + 7 = 16 ``` More information can be found by reading about [Binomial Coefficients](https://en.wikipedia.org/wiki/Binomial_coefficient) or about [Finite Differences](https://en.wikipedia.org/wiki/Finite_difference). Note that while a solution should be able to handle non-integer inputs and get a correct result, any solution should make use of rounding to two significant digits (as the official solution does) since high precision for non-integers is not the point here. Also feel free to reuse/extend the following starter code: ```python def value_at(poly_spec, x): ```
{"functional": "_inputs = [[[1, 2, 7], 3], [[1, 2, 7, 0, 5], 2], [[1, 1, 1, 1, 7, 0, 5], 2], [[1, 2, 7, 0, 5], 0.6], [[1, 2, 7, 0, 5], 0]]\n_outputs = [[16], [12], [12], [4.24], [5.0]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(value_at(*i), o[0])"}
529
263
coding
Solve the programming task below in a Python markdown code block. Madoka is a very strange girl, and therefore she suddenly wondered how many pairs of integers $(a, b)$ exist, where $1 \leq a, b \leq n$, for which $\frac{\operatorname{lcm}(a, b)}{\operatorname{gcd}(a, b)} \leq 3$. In this problem, $\operatorname{gcd}(a, b)$ denotes the greatest common divisor of the numbers $a$ and $b$, and $\operatorname{lcm}(a, b)$ denotes the smallest common multiple of the numbers $a$ and $b$. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Description of the test cases follows. The first and the only line of each test case contains the integer $n$ ($1 \le n \le 10^8$). -----Output----- For each test case output a single integer — the number of pairs of integers satisfying the condition. -----Examples----- Input 6 1 2 3 4 5 100000000 Output 1 4 7 10 11 266666666 -----Note----- For $n = 1$ there is exactly one pair of numbers — $(1, 1)$ and it fits. For $n = 2$, there are only $4$ pairs — $(1, 1)$, $(1, 2)$, $(2, 1)$, $(2, 2)$ and they all fit. For $n = 3$, all $9$ pair are suitable, except $(2, 3)$ and $(3, 2)$, since their $\operatorname{lcm}$ is $6$, and $\operatorname{gcd}$ is $1$, which doesn't fit the condition.
{"inputs": ["6\n1\n2\n3\n4\n5\n100000000\n"], "outputs": ["1\n4\n7\n10\n11\n266666666\n"]}
426
54
coding
Solve the programming task below in a Python markdown code block. You have given an array $a$ of length $n$ and an integer $x$ to a brand new robot. What the robot does is the following: it iterates over the elements of the array, let the current element be $q$. If $q$ is divisible by $x$, the robot adds $x$ copies of the integer $\frac{q}{x}$ to the end of the array, and moves on to the next element. Note that the newly added elements could be processed by the robot later. Otherwise, if $q$ is not divisible by $x$, the robot shuts down. Please determine the sum of all values of the array at the end of the process. -----Input----- The first input line contains a single integer $t$ ($1 \leq t \leq 100$) — the number of test cases. The first line of each test case contains two integers $n$ and $x$ ($1 \leq n \leq 10^5$, $2 \leq x \leq 10^9$) — the length of the array and the value which is used by the robot. The next line contains integers $a_1$, $a_2$, ..., $a_n$ ($1 \leq a_i \leq 10^9$) — the initial values in the array. It is guaranteed that the sum of values $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case output one integer — the sum of all elements at the end of the process. -----Examples----- Input 2 1 2 12 4 2 4 6 8 2 Output 36 44 -----Note----- In the first test case the array initially consists of a single element $[12]$, and $x=2$. After the robot processes the first element, the array becomes $[12, 6, 6]$. Then the robot processes the second element, and the array becomes $[12, 6, 6, 3, 3]$. After the robot processes the next element, the array becomes $[12, 6, 6, 3, 3, 3, 3]$, and then the robot shuts down, since it encounters an element that is not divisible by $x = 2$. The sum of the elements in the resulting array is equal to $36$. In the second test case the array initially contains integers $[4, 6, 8, 2]$, and $x=2$. The resulting array in this case looks like $ [4, 6, 8, 2, 2, 2, 3, 3, 4, 4, 1, 1, 1, 1, 1, 1]$.
{"inputs": ["2\n1 2\n12\n4 2\n4 6 8 2\n", "2\n1 2\n12\n4 2\n4 6 4 2\n", "2\n1 2\n12\n4 2\n7 6 4 2\n", "2\n1 2\n12\n4 2\n7 6 5 2\n", "2\n1 2\n12\n4 2\n7 6 3 2\n", "2\n1 2\n12\n4 2\n7 6 6 2\n", "2\n1 2\n1\n4 2\n2 11 8 2\n", "2\n1 2\n12\n4 4\n7 6 3 1\n"], "outputs": ["36\n44\n", "36\n36\n", "36\n19\n", "36\n20\n", "36\n18\n", "36\n21\n", "1\n25\n", "36\n17\n"]}
626
253
coding
Solve the programming task below in a Python markdown code block. Chef has recently started playing chess, and wants to play as many games as possible. He calculated that playing one game of chess takes at least 20 minutes of his time. Chef has N hours of free time. What is the maximum number of complete chess games he can play in that time? ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of a single line containing a single integer, N. ------ Output Format ------ For each test case, output on a new line the maximum number of complete chess games Chef can play in N hours. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10$ ----- Sample Input 1 ------ 4 1 10 7 3 ----- Sample Output 1 ------ 3 30 21 9 ----- explanation 1 ------ Test case $1$: If every game Chef plays takes $20$ minutes, he can play $3$ games in one hour. Test case $2$: If every game Chef plays takes $20$ minutes, he can play $30$ games in $10$ hours. Test case $3$: If every game Chef plays takes $20$ minutes, he can play $21$ games in $7$ hours. Test case $4$: If every game Chef plays takes $20$ minutes, he can play $9$ games in $3$ hours.
{"inputs": ["4\n1\n10\n7\n3\n"], "outputs": ["3\n30\n21\n9\n"]}
329
31
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an integer array nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in any order. You must write an algorithm that runs in linear runtime complexity and uses only constant extra space.   Please complete the following python code precisely: ```python class Solution: def singleNumber(self, nums: List[int]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(nums = [1,2,1,3,2,5]) == [3,5]\n assert candidate(nums = [-1,0]) == [-1,0]\n assert candidate(nums = [0,1]) == [1,0]\n\n\ncheck(Solution().singleNumber)"}
110
78
coding
Solve the programming task below in a Python markdown code block. In Byteland there are N cities, numbered 1 through N. Some pairs of cities are connected by bi-directional roads in such a way that starting from any one city you can visit all other cities either directly or indirectly. Chef is currently at city A and wants to visit all other cities in Byteland. Chef can only move according to following rule. If Chef is at city A then he continues to move from city A to city B, city B to city C (provided A is directly connected to B, B is directly connected to C) and so on unless there are no more cities leading from current city. If so he jumps back to previous city and repeat the same tour with other cities leading from it which are not visited. Chef repeat this step unless all cities are not visited. Help Chef to count number of ways in which he can visit all other cities . As this number can be large print it modulo 109+7 -----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 a single integer N denoting the number of cities in Byteland. - Next N-1 lines contain two space-separated integers u and v denoting there is bi-directional road between city numbered u and v. - Next line contains a single integer A denoting the city number where Chef is present. -----Output----- - For each test case, output a single line containing number of ways in which Chef can visit all cities modulo 109+7. -----Constraints----- - 1 ≤ T ≤ 5 - 1 ≤ N ≤ 105 - 1 ≤ A ≤ N -----Subtasks----- Subtask #1 : (10 points) - 1 ≤ N ≤ 5 Subtask #2 : (30 points) - 1 ≤ N ≤ 100 Subtask #3 : (60 points) - 1 ≤ N ≤ 105 -----Example----- Input: 2 3 1 2 1 3 1 5 1 2 1 3 2 4 2 5 1 Output: 2 4 -----Explanation----- Example case 1. Chef can visit cities in two ways according to the problem: 1-2-3 and 1-3-2 Example case 1. Chef can visit cities in four ways according to the problem: 1-2-4-5-3 1-2-5-4-3 1-3-2-4-5 1-3-2-5-4
{"inputs": ["2\n3\n1 2\n1 3\n1\n5\n1 2\n1 3\n2 4\n2 5\n1"], "outputs": ["2\n4"]}
578
46
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an m x n matrix, return a new matrix answer where answer[row][col] is the rank of matrix[row][col]. The rank is an integer that represents how large an element is compared to other elements. It is calculated using the following rules: The rank is an integer starting from 1. If two elements p and q are in the same row or column, then: If p < q then rank(p) < rank(q) If p == q then rank(p) == rank(q) If p > q then rank(p) > rank(q) The rank should be as small as possible. The test cases are generated so that answer is unique under the given rules.   Please complete the following python code precisely: ```python class Solution: def matrixRankTransform(self, matrix: List[List[int]]) -> List[List[int]]: ```
{"functional": "def check(candidate):\n assert candidate(matrix = [[1,2],[3,4]]) == [[1,2],[2,3]]\n assert candidate(matrix = [[7,7],[7,7]]) == [[1,1],[1,1]]\n assert candidate(matrix = [[20,-21,14],[-19,4,19],[22,-47,24],[-19,4,19]]) == [[4,2,3],[1,3,4],[5,1,6],[1,3,4]]\n assert candidate(matrix = [[7,3,6],[1,4,5],[9,8,2]]) == [[5,1,4],[1,2,3],[6,3,1]]\n\n\ncheck(Solution().matrixRankTransform)"}
191
186
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 and an integer value. In one operation, you can add or subtract value from any element of nums. For example, if nums = [1,2,3] and value = 2, you can choose to subtract value from nums[0] to make nums = [-1,2,3]. The MEX (minimum excluded) of an array is the smallest missing non-negative integer in it. For example, the MEX of [-1,2,3] is 0 while the MEX of [1,0,3] is 2. Return the maximum MEX of nums after applying the mentioned operation any number of times.   Please complete the following python code precisely: ```python class Solution: def findSmallestInteger(self, nums: List[int], value: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [1,-10,7,13,6,8], value = 5) == 4\n assert candidate(nums = [1,-10,7,13,6,8], value = 7) == 2\n\n\ncheck(Solution().findSmallestInteger)"}
197
79
coding
Solve the programming task below in a Python markdown code block. You are given a string $s$ of lowercase Latin letters. The following operation can be used: select one character (from 'a' to 'z') that occurs at least once in the string. And replace all such characters in the string with the previous one in alphabetical order on the loop. For example, replace all 'c' with 'b' or replace all 'a' with 'z'. And you are given the integer $k$ — the maximum number of operations that can be performed. Find the minimum lexicographically possible string that can be obtained by performing no more than $k$ operations. The string $a=a_1a_2 \dots a_n$ is lexicographically smaller than the string $b = b_1b_2 \dots b_n$ if there exists an index $k$ ($1 \le k \le n$) such that $a_1=b_1$, $a_2=b_2$, ..., $a_{k-1}=b_{k-1}$, but $a_k < b_k$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) —the number of test cases in the test. This is followed by descriptions of the test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the size of the string $s$ and the maximum number of operations that can be performed on the string $s$. The second line of each test case contains a string $s$ of length $n$ consisting of lowercase Latin letters. It is guaranteed that the sum $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output the lexicographically minimal string that can be obtained from the string $s$ by performing no more than $k$ operations. -----Examples----- Input 4 3 2 cba 4 5 fgde 7 5 gndcafb 4 19 ekyv Output aaa agaa bnbbabb aapp -----Note----- None
{"inputs": ["1\n15 1\nwaterwaterwater\n", "4\n3 2\ncba\n4 5\nfgde\n7 5\ngndcafb\n4 19\nekyv\n"], "outputs": ["vatervatervater\n", "aaa\nagaa\nbnbbabb\naapp\n"]}
497
77
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a positive integer num, split it into two non-negative integers num1 and num2 such that: The concatenation of num1 and num2 is a permutation of num. In other words, the sum of the number of occurrences of each digit in num1 and num2 is equal to the number of occurrences of that digit in num. num1 and num2 can contain leading zeros. Return the minimum possible sum of num1 and num2. Notes: It is guaranteed that num does not contain any leading zeros. The order of occurrence of the digits in num1 and num2 may differ from the order of occurrence of num.   Please complete the following python code precisely: ```python class Solution: def splitNum(self, num: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(num = 4325) == 59\n assert candidate(num = 687) == 75\n\n\ncheck(Solution().splitNum)"}
176
50
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. There is a car with capacity empty seats. The vehicle only drives east (i.e., it cannot turn around and drive west). You are given the integer capacity and an array trips where trips[i] = [numPassengersi, fromi, toi] indicates that the ith trip has numPassengersi passengers and the locations to pick them up and drop them off are fromi and toi respectively. The locations are given as the number of kilometers due east from the car's initial location. Return true if it is possible to pick up and drop off all passengers for all the given trips, or false otherwise.   Please complete the following python code precisely: ```python class Solution: def carPooling(self, trips: List[List[int]], capacity: int) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(trips = [[2,1,5],[3,3,7]], capacity = 4) == False\n assert candidate(trips = [[2,1,5],[3,3,7]], capacity = 5) == True\n\n\ncheck(Solution().carPooling)"}
176
73
coding
Solve the programming task below in a Python markdown code block. A and B are preparing themselves for programming contests. An important part of preparing for a competition is sharing programming knowledge from the experienced members to those who are just beginning to deal with the contests. Therefore, during the next team training A decided to make teams so that newbies are solving problems together with experienced participants. A believes that the optimal team of three people should consist of one experienced participant and two newbies. Thus, each experienced participant can share the experience with a large number of people. However, B believes that the optimal team should have two experienced members plus one newbie. Thus, each newbie can gain more knowledge and experience. As a result, A and B have decided that all the teams during the training session should belong to one of the two types described above. Furthermore, they agree that the total number of teams should be as much as possible. There are n experienced members and m newbies on the training session. Can you calculate what maximum number of teams can be formed? -----Input----- The first line contains two integers n and m (0 ≤ n, m ≤ 5·10^5) — the number of experienced participants and newbies that are present at the training session. -----Output----- Print the maximum number of teams that can be formed. -----Examples----- Input 2 6 Output 2 Input 4 5 Output 3 -----Note----- Let's represent the experienced players as XP and newbies as NB. In the first test the teams look as follows: (XP, NB, NB), (XP, NB, NB). In the second test sample the teams look as follows: (XP, NB, NB), (XP, NB, NB), (XP, XP, NB).
{"inputs": ["2 6\n", "4 5\n", "1 1\n", "3 3\n", "0 0\n", "0 1\n", "1 0\n", "0 6\n"], "outputs": ["2\n", "3\n", "0\n", "2\n", "0\n", "0\n", "0\n", "0\n"]}
365
86
coding
Solve the programming task below in a Python markdown code block. You have decided to write a book introducing good restaurants. There are N restaurants that you want to introduce: Restaurant 1, Restaurant 2, ..., Restaurant N. Restaurant i is in city S_i, and your assessment score of that restaurant on a 100-point scale is P_i. No two restaurants have the same score. You want to introduce the restaurants in the following order: - The restaurants are arranged in lexicographical order of the names of their cities. - If there are multiple restaurants in the same city, they are arranged in descending order of score. Print the identification numbers of the restaurants in the order they are introduced in the book. -----Constraints----- - 1 ≤ N ≤ 100 - S is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters. - 0 ≤ P_i ≤ 100 - P_i is an integer. - P_i ≠ P_j (1 ≤ i < j ≤ N) -----Input----- Input is given from Standard Input in the following format: N S_1 P_1 : S_N P_N -----Output----- Print N lines. The i-th line (1 ≤ i ≤ N) should contain the identification number of the restaurant that is introduced i-th in the book. -----Sample Input----- 6 khabarovsk 20 moscow 10 kazan 50 kazan 35 moscow 60 khabarovsk 40 -----Sample Output----- 3 4 6 1 5 2 The lexicographical order of the names of the three cities is kazan < khabarovsk < moscow. For each of these cities, the restaurants in it are introduced in descending order of score. Thus, the restaurants are introduced in the order 3,4,6,1,5,2.
{"inputs": ["1\nv 81\n", "6\nrgkkavbtoa 3\nqooqye -1\n`nz^k 4\njabyp 0\nowdnsk 0\njb`qnvtajl 1", "6\nrgkkavbtoa 3\nqooqye -1\n`mz]k 3\npybaj 0\nowdnsk 0\njb`qnvtajl 1", "6\nqgkkavbtoa 3\nqooqye -1\n`mz]k 3\npybaj 0\nowdnsk 0\njb`qnvtajl 1", "6\naotbvakkgq 3\nqooqye -1\n`mz]k 3\npybaj 0\nowdnsk 0\njb`qnvtajl 1", "6\nrgkkavbtoa 3\nqooqye -1\n`nz^k 2\njpbay 32\nowdnsl 0\njb`qnvtajl 1", "6\n`otbvakkgq 3\nooqqyd -1\n_nz^k 1\noybja 0\nksndwo 1\njb`qnvtajl -2", "6\n`otbvajkgp 3\ndypqnm 0\n_ny^k -2\noybja 0\nlsndwo 0\njj`qnvuabl -5"], "outputs": ["1\n", "3\n4\n6\n5\n2\n1\n", "3\n6\n5\n4\n2\n1\n", "3\n6\n5\n4\n1\n2\n", "3\n1\n6\n5\n4\n2\n", "3\n6\n4\n5\n2\n1\n", "3\n1\n6\n5\n2\n4\n", "3\n1\n2\n6\n5\n4\n"]}
409
445
coding
Solve the programming task below in a Python markdown code block. You are given a string S, each of whose characters is either '0', '1', or '?'. The *badness* of a binary string is defined to be the (absolute) difference between the number of 1s and the number of 0s present in it. For example, 001 has badness |1-2| = 1 and 11111 has badness |5-0| = 5. Your task is to replace every '?' present in S with either '0' or '1', such that the badness of the resulting binary string is minimized. If there are multiple solutions, you may print any of them. ------ 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 contains two lines. - The first line of each test case contains an integer N, the number of characters of the string S. - The second line of each test case contains the string S, whose characters are either '0', '1', or '?'. ------ Output Format ------ For each test case, print a single line containing the binary string you obtained by replacing each '?' with either '0' or '1' such that its badness is minimum. If there are multiple possible strings that minimize badness, you may print any of them. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{5}$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$. ----- Sample Input 1 ------ 4 4 ?101 4 ??10 6 ???111 5 1?0?1 ----- Sample Output 1 ------ 0101 1010 000111 11001 ----- explanation 1 ------ Test Case $1$. There are only two possible strings that can be constructed - $1101$ and $0101$. $1101$ has badness $2$, and $0101$ has badness $0$. Thus, the only possible answer is $0101$. Test Case $2$. There are four possible strings that can be made, namely $\{0010, 0110, 1010, 1110\}$. Their respective badness values are $\{2, 0, 0, 2\}$. The minimum value here is $0$, attained by both $0110$ and $1010$ - and so either of these strings is a valid answer. Test Case $3$. There are eight possible strings, of which $000111$ is the only one with minimum badness (that being $0$). Test Case $4$. There are four possible strings, out of which three of them ($10001$, $10011$, $11001$) have badness $1$, which is minimum. All three of them are possible answers.
{"inputs": ["4\n4\n?101\n4\n??10\n6\n???111\n5\n1?0?1\n"], "outputs": ["0101\n1010\n000111\n11001\n"]}
688
63
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a 0-indexed array nums comprising of n non-negative integers. In one operation, you must: Choose an integer i such that 1 <= i < n and nums[i] > 0. Decrease nums[i] by 1. Increase nums[i - 1] by 1. Return the minimum possible value of the maximum integer of nums after performing any number of operations.   Please complete the following python code precisely: ```python class Solution: def minimizeArrayValue(self, nums: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [3,7,1,6]) == 5\n assert candidate(nums = [10,1]) == 10\n\n\ncheck(Solution().minimizeArrayValue)"}
131
55
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese and Russian as well. Chef wants to implement wildcard pattern matching supporting only the wildcard '?'. The wildcard character '?' can be substituted by any single lower case English letter for matching. He has two strings X and Y of equal length, made up of lower case letters and the character '?'. He wants to know whether the strings X and Y can be matched or not. ------ Input ------ The first line of input contain an integer T denoting the number of test cases. Each test case consists of two lines, the first line contains the string X and the second contains the string Y. ------ Output ------ For each test case, output a single line with the word Yes if the strings can be matched, otherwise output No. ------ Constraints ------ $1 ≤ T ≤ 50$ $Both X and Y have equal length and the length is between 1 and 10.$ $Both X and Y consist of lower case letters and the character '?'.$ ----- Sample Input 1 ------ 2 s?or? sco?? stor? sco?? ----- Sample Output 1 ------ Yes No ----- explanation 1 ------ Test case $1$: One of the possible ways to match both the strings is $\texttt{score}$. This can be done by: - Replace $1^{st}$ and $2^{nd}$ $\texttt{?}$ of string $X$ by characters $\texttt{c}$ and $\texttt{e}$ respectively. - Replace $1^{st}$ and $2^{nd}$ $\texttt{?}$ of string $Y$ by characters $\texttt{r}$ and $\texttt{e}$ respectively. Test case $2$: There exists no way to fill the $\texttt{?}$ such that the strings become equal. Thus, the answer is No.
{"inputs": ["2\ns?or?\nsco??\nstor?\nsco??", "2\ns?o?r\nsco??\nstor?\nsco??", "2\nr?o?s\nsco??\nstor?\nsco??", "2\nr?o?s\nsco??\n?rots\nsco??", "2\nr?o?s\nsco??\n@rots\nsco??", "2\nr?o?s\nsco??\n@rots\n??ocs", "2\nr?o?s\nsco??\n@rots\ntco??", "2\nr?o?s\nsco??\n@rots\nuco??"], "outputs": ["Yes\nNo", "Yes\nNo\n", "No\nNo\n", "No\nNo\n", "No\nNo\n", "No\nNo\n", "No\nNo\n", "No\nNo\n"]}
398
209
coding
Solve the programming task below in a Python markdown code block. Vasya wrote down two strings s of length n and t of length m consisting of small English letters 'a' and 'b'. What is more, he knows that string t has a form "abab...", namely there are letters 'a' on odd positions and letters 'b' on even positions. Suddenly in the morning, Vasya found that somebody spoiled his string. Some letters of the string s were replaced by character '?'. Let's call a sequence of positions i, i + 1, ..., i + m - 1 as occurrence of string t in s, if 1 ≤ i ≤ n - m + 1 and t_1 = s_{i}, t_2 = s_{i} + 1, ..., t_{m} = s_{i} + m - 1. The boy defines the beauty of the string s as maximum number of disjoint occurrences of string t in s. Vasya can replace some letters '?' with 'a' or 'b' (letters on different positions can be replaced with different letter). Vasya wants to make some replacements in such a way that beauty of string s is maximum possible. From all such options, he wants to choose one with the minimum number of replacements. Find the number of replacements he should make. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^5) — the length of s. The second line contains the string s of length n. It contains small English letters 'a', 'b' and characters '?' only. The third line contains a single integer m (1 ≤ m ≤ 10^5) — the length of t. The string t contains letters 'a' on odd positions and 'b' on even positions. -----Output----- Print the only integer — the minimum number of replacements Vasya has to perform to make the beauty of string s the maximum possible. -----Examples----- Input 5 bb?a? 1 Output 2 Input 9 ab??ab??? 3 Output 2 -----Note----- In the first sample string t has a form 'a'. The only optimal option is to replace all characters '?' by 'a'. In the second sample using two replacements we can make string equal to "aba?aba??". It is impossible to get more than two occurrences.
{"inputs": ["1\nb\n1\n", "1\nb\n1\n", "1\nb\n2\n", "3\nb?a\n1\n", "3\nb?a\n1\n", "3\nba?\n1\n", "3\n?ab\n1\n", "5\nbb?a?\n1\n"], "outputs": ["0\n", "0", "0\n", "1\n", "1\n", "1\n", "1\n", "2\n"]}
498
107
coding
Solve the programming task below in a Python markdown code block. Given is a string S, where each character is `0`, `1`, or `?`. Consider making a string S' by replacing each occurrence of `?` with `0` or `1` (we can choose the character for each `?` independently). Let us define the unbalancedness of S' as follows: * (The unbalancedness of S') = \max \\{ The absolute difference between the number of occurrences of `0` and `1` between the l-th and r-th character of S (inclusive) :\ 1 \leq l \leq r \leq |S|\\} Find the minimum possible unbalancedness of S'. Constraints * 1 \leq |S| \leq 10^6 * Each character of S is `0`, `1`, or `?`. Input Input is given from Standard Input in the following format: S Output Print the minimum possible unbalancedness of S'. Examples Input 0?? Output 1 Input 0??0 Output 2 Input ??00????0??0????0?0??00??1???11?1?1???1?11?111???1 Output 4
{"inputs": ["?0?", "?/?", "??0", "??/", "?.?", "??1", "?-?", "?,?"], "outputs": ["1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n"]}
277
66
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an array of strings patterns and a string word, return the number of strings in patterns that exist as a substring in word. A substring is a contiguous sequence of characters within a string.   Please complete the following python code precisely: ```python class Solution: def numOfStrings(self, patterns: List[str], word: str) -> int: ```
{"functional": "def check(candidate):\n assert candidate(patterns = [\"a\",\"abc\",\"bc\",\"d\"], word = \"abc\") == 3\n assert candidate(patterns = [\"a\",\"b\",\"c\"], word = \"aaaaabbbbb\") == 2\n assert candidate(patterns = [\"a\",\"a\",\"a\"], word = \"ab\") == 3\n\n\ncheck(Solution().numOfStrings)"}
90
96
coding
Solve the programming task below in a Python markdown code block. In this Kata, you will be given directions and your task will be to find your way back. ```Perl solve(["Begin on Road A","Right on Road B","Right on Road C","Left on Road D"]) = ['Begin on Road D', 'Right on Road C', 'Left on Road B', 'Left on Road A'] solve(['Begin on Lua Pkwy', 'Right on Sixth Alley', 'Right on 1st Cr']) = ['Begin on 1st Cr', 'Left on Sixth Alley', 'Left on Lua Pkwy'] ``` More examples in test cases. Good luck! Please also try [Simple remove duplicates](https://www.codewars.com/kata/5ba38ba180824a86850000f7) Also feel free to reuse/extend the following starter code: ```python def solve(arr): ```
{"functional": "_inputs = [[['Begin on 3rd Blvd', 'Right on First Road', 'Left on 9th Dr']], [['Begin on Road A', 'Right on Road B', 'Right on Road C', 'Left on Road D']], [['Begin on Road A']]]\n_outputs = [[['Begin on 9th Dr', 'Right on First Road', 'Left on 3rd Blvd']], [['Begin on Road D', 'Right on Road C', 'Left on Road B', 'Left on Road A']], [['Begin on Road A']]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(solve(*i), o[0])"}
204
256
coding
Solve the programming task below in a Python markdown code block. Recently, Greg got a grid with n rows and m columns. Rows are indexed from 1 to n and columns are indexed from 1 to m. The cell $ ( i , j )$ is the cell of intersection of row i and column j. Each cell has a number written on it. The number written on cell $( i , j )$ is equal to $(i+j)$. Now, Greg wants to select some cells from the grid, such that for every pair of selected cells ,the numbers on the cells are co-prime. Determine the maximum number of cells that Greg can select. ------ Input ------ Single line containing integers n and m denoting number of rows and number of columns respectively. ------ Output ------ Single line containing the answer. ------ Constraints ------ $$1 ≤ n,m ≤ 10^{6}$$ ----- Sample Input 1 ------ 3 4 ----- Sample Output 1 ------ 4
{"inputs": ["3 4\n"], "outputs": ["4"]}
212
15
coding
Solve the programming task below in a Python markdown code block. Chef is now a corporate person. He has to attend office regularly. But chef does not want to go to office, rather he wants to stay home and discover different recipes and cook them. In the office where chef works, has two guards who count how many times a person enters into the office building. Though the duty of a guard is 24 hour in a day, but sometimes they fall asleep during their duty and could not track the entry of a person in the office building. But one better thing is that they never fall asleep at the same time. At least one of them remains awake and counts who enters into the office. Now boss of Chef wants to calculate how many times Chef has entered into the building. He asked to the guard and they give him two integers A and B, count of first guard and second guard respectively. Help the boss to count the minimum and maximum number of times Chef could have entered into the office building. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of the T test cases follows. Each test case consists of a line containing two space separated integers A and B. -----Output----- For each test case, output a single line containing two space separated integers, the minimum and maximum number of times Chef could have entered into the office building. -----Constraints----- - 1 ≤ T ≤ 100 - 0 ≤ A, B ≤ 1000000 -----Example----- Input: 1 19 17 Output: 19 36
{"inputs": ["1\n19 17"], "outputs": ["19 36"]}
336
22
coding
Solve the programming task below in a Python markdown code block. Complete the function ```circleArea``` so that it will return the area of a circle with the given ```radius```. Round the returned number to two decimal places (except for Haskell). If the radius is not positive or not a number, return ```false```. Example: ```python circleArea(-1485.86) #returns false circleArea(0) #returns false circleArea(43.2673) #returns 5881.25 circleArea(68) #returns 14526.72 circleArea("number") #returns false ``` Also feel free to reuse/extend the following starter code: ```python def circleArea(r): ```
{"functional": "_inputs = [[0], ['An integer']]\n_outputs = [[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(circleArea(*i), o[0])"}
172
161
coding
Solve the programming task below in a Python markdown code block. Given a string as input, move all of its vowels to the end of the string, in the same order as they were before. Vowels are (in this kata): `a, e, i, o, u` Note: all provided input strings are lowercase. ## Examples ```python "day" ==> "dya" "apple" ==> "pplae" ``` Also feel free to reuse/extend the following starter code: ```python def move_vowels(input): ```
{"functional": "_inputs = [['day'], ['apple'], ['peace'], ['maker'], ['programming'], ['javascript'], ['python'], ['ruby'], ['haskell'], ['clojure'], ['csharp']]\n_outputs = [['dya'], ['pplae'], ['pceae'], ['mkrae'], ['prgrmmngoai'], ['jvscrptaai'], ['pythno'], ['rbyu'], ['hskllae'], ['cljroue'], ['cshrpa']]\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(move_vowels(*i), o[0])"}
121
246
coding
Solve the programming task below in a Python markdown code block. Given is an integer N. Find the number of positive integers less than or equal to N that have an odd number of digits (in base ten without leading zeros). -----Constraints----- - 1 \leq N \leq 10^5 -----Input----- Input is given from Standard Input in the following format: N -----Output----- Print the number of positive integers less than or equal to N that have an odd number of digits. -----Sample Input----- 11 -----Sample Output----- 9 Among the positive integers less than or equal to 11, nine integers have an odd number of digits: 1, 2, \ldots, 9.
{"inputs": ["2", "3", "4", "1", "0", "7", "6", "5"], "outputs": ["2\n", "3\n", "4\n", "1\n", "0\n", "7\n", "6\n", "5\n"]}
152
62
coding
Solve the programming task below in a Python markdown code block. Your task is to make a program takes in a sentence (without puncuation), adds all words to a list and returns the sentence as a string which is the positions of the word in the list. Casing should not matter too. Example ----- `"Ask not what your COUNTRY can do for you ASK WHAT YOU CAN DO FOR YOUR country"` becomes `"01234567802856734"` Another example ----- `"the one bumble bee one bumble the bee"` becomes `"01231203"` Also feel free to reuse/extend the following starter code: ```python def compress(sentence): ```
{"functional": "_inputs = [['The bumble bee'], ['SILLY LITTLE BOYS silly little boys'], ['Ask not what your COUNTRY can do for you ASK WHAT YOU CAN DO FOR YOUR country'], ['The number 0 is such a strange number Strangely it has zero meaning']]\n_outputs = [['012'], ['012012'], ['01234567802856734'], ['012345617891011']]\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(compress(*i), o[0])"}
160
254
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You have n processes forming a rooted tree structure. You are given two integer arrays pid and ppid, where pid[i] is the ID of the ith process and ppid[i] is the ID of the ith process's parent process. Each process has only one parent process but may have multiple children processes. Only one process has ppid[i] = 0, which means this process has no parent process (the root of the tree). When a process is killed, all of its children processes will also be killed. Given an integer kill representing the ID of a process you want to kill, return a list of the IDs of the processes that will be killed. You may return the answer in any order.   Please complete the following python code precisely: ```python class Solution: def killProcess(self, pid: List[int], ppid: List[int], kill: int) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(pid = [1,3,10,5], ppid = [3,0,5,3], kill = 5) == [5,10]\n assert candidate(pid = [1], ppid = [0], kill = 1) == [1]\n\n\ncheck(Solution().killProcess)"}
202
83
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are playing the following Nim Game with your friend: Initially, there is a heap of stones on the table. You and your friend will alternate taking turns, and you go first. On each turn, the person whose turn it is will remove 1 to 3 stones from the heap. The one who removes the last stone is the winner. Given n, the number of stones in the heap, return true if you can win the game assuming both you and your friend play optimally, otherwise return false.   Please complete the following python code precisely: ```python class Solution: def canWinNim(self, n: int) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(n = 4) == False \n assert candidate(n = 1) == True\n assert candidate(n = 2) == True\n\n\ncheck(Solution().canWinNim)"}
152
55
coding
Solve the programming task below in a Python markdown code block. Polycarpus has postcards and photos hung in a row on the wall. He decided to put them away to the closet and hang on the wall a famous painter's picture. Polycarpus does it like that: he goes from the left to the right and removes the objects consecutively. As Polycarpus doesn't want any mix-ups to happen, he will not carry in his hands objects of two different types. In other words, Polycarpus can't carry both postcards and photos simultaneously. Sometimes he goes to the closet and puts the objects there, thus leaving his hands free. Polycarpus must put all the postcards and photos to the closet. He cannot skip objects. What minimum number of times he should visit the closet if he cannot carry more than 5 items? Input The only line of the input data contains a non-empty string consisting of letters "С" and "P" whose length does not exceed 100 characters. If the i-th character in the string is the letter "С", that means that the i-th object (the numbering goes from the left to the right) on Polycarpus' wall is a postcard. And if the i-th character is the letter "P", than the i-th object on the wall is a photo. Output Print the only number — the minimum number of times Polycarpus has to visit the closet. Examples Input CPCPCPC Output 7 Input CCCCCCPPPPPP Output 4 Input CCCCCCPPCPPPPPPPPPP Output 6 Input CCCCCCCCCC Output 2 Note In the first sample Polycarpus needs to take one item to the closet 7 times. In the second sample Polycarpus can first take 3 postcards to the closet; then 3 more. He can take the 6 photos that are left in the similar way, going to the closet twice. In the third sample Polycarpus can visit the closet twice, both times carrying 3 postcards. Then he can take there 2 photos at once, then one postcard and finally, he can carry the last 10 photos if he visits the closet twice. In the fourth sample Polycarpus can visit the closet twice and take there all 10 postcards (5 items during each go).
{"inputs": ["P\n", "C\n", "CP\n", "PC\n", "PPPP\n", "PPPPP\n", "CPCCPPC\n", "CPPCCPC\n"], "outputs": ["1\n", "1\n", "2\n", "2\n", "1\n", "1\n", "5\n", "5\n"]}
503
77
coding
Solve the programming task below in a Python markdown code block. Problem statement A programming contest will be held in the Russian Federation. The contest has N questions and has M participants. Question i has a score a_i, and it is known that participant j's ability is b_j. For problem i and participant j, participant j can always solve problem i if a_i ≤ b_j and only then. The score of a participant through the contest is the sum of the scores of the problems that the person was able to solve. Participant j also sets a target score c_j for this contest. Determine if each participant can score more than the target score. input The input is given in the following format. N a_ {0} a_ {1} a_ {2}… a_ {N−1} M b_ {0} b_ {1} b_ {2}… b_ {M−1} c_ {0} c_ {1} c_ {2}… c_ {M−1} Constraint * All inputs are integers * 1 \ ≤ N \ ≤ 300 \,000 * 1 \ ≤ M \ ≤ 300 \,000 * 0 \ ≤ a_ {i} \ ≤ 1 \, 000 \,000 * 0 \ ≤ b_ {i} \ ≤ 1 \, 000 \,000 * 0 \ ≤ c_ {i} \ ≤ ∑a_k output Print the answer on line M. On the i-line, output `Yes` if the participant i-1 can get more points than the target score, and` No` if not. sample Sample input 1 6 1 2 1 3 4 5 7 1 3 4 5 3 1 0 2 4 5 3 4 5 3 Sample output 1 Yes Yes Yes Yes Yes No No The points obtained by each participant are as follows. * Participant 0: 1 + 1 = 2 * Participant 1: 1 + 2 + 1 + 3 = 7 * Participant 2: 1 + 2 + 1 + 3 + 4 = 11 * Participant 3: 1 + 2 + 1 + 3 + 4 + 5 = 16 * Participant 4: 1 + 2 + 1 + 3 = 7 * Participant 5: 1 + 1 = 2 * Participants 6: 0 Sample input 2 8 1 1 2 3 3 4 6 100 Four 1 3 4 99 1 10 15 120 Sample output 2 Yes Yes No No Example Input 6 1 2 1 3 4 5 7 1 3 4 5 3 1 0 2 4 5 3 4 5 3 Output Yes Yes Yes Yes Yes No No
{"inputs": ["6\n1 2 1 3 4 5\n7\n1 3 4 5 3 1 0\n2 8 5 3 4 5 3", "6\n1 2 1 5 4 5\n7\n1 3 4 7 3 1 0\n2 8 5 3 7 5 3", "6\n1 2 2 5 4 5\n7\n1 3 4 7 3 1 0\n2 8 5 3 7 5 3", "6\n1 2 1 6 3 1\n7\n1 3 4 7 3 1 0\n2 8 5 3 3 0 3", "6\n1 2 2 8 6 1\n7\n2 3 4 2 3 1 0\n1 8 1 3 3 1 1", "6\n1 2 1 5 4 5\n7\n1 3 3 7 3 1 0\n2 8 5 3 7 5 3", "6\n1 2 2 5 4 5\n7\n1 3 4 7 3 1 0\n2 8 5 3 7 5 0", "6\n1 2 1 8 3 1\n7\n0 6 4 4 3 1 0\n1 8 5 3 3 0 3"], "outputs": ["Yes\nNo\nYes\nYes\nYes\nNo\nNo\n", "Yes\nNo\nYes\nYes\nNo\nNo\nNo\n", "No\nNo\nYes\nYes\nNo\nNo\nNo\n", "Yes\nYes\nYes\nYes\nYes\nYes\nNo\n", "Yes\nNo\nYes\nYes\nYes\nYes\nNo\n", "Yes\nNo\nNo\nYes\nNo\nNo\nNo\n", "No\nNo\nYes\nYes\nNo\nNo\nYes\n", "No\nYes\nYes\nYes\nYes\nYes\nNo\n"]}
701
494
coding
Solve the programming task below in a Python markdown code block. Today, Marin is at a cosplay exhibition and is preparing for a group photoshoot! For the group picture, the cosplayers form a horizontal line. A group picture is considered beautiful if for every contiguous segment of at least $2$ cosplayers, the number of males does not exceed the number of females (obviously). Currently, the line has $n$ cosplayers which can be described by a binary string $s$. The $i$-th cosplayer is male if $s_i = 0$ and female if $s_i = 1$. To ensure that the line is beautiful, you can invite some additional cosplayers (possibly zero) to join the line at any position. You can't remove any cosplayer from the line. Marin wants to know the minimum number of cosplayers you need to invite so that the group picture of all the cosplayers is beautiful. She can't do this on her own, so she's asking you for help. Can you help her? -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^3$) — the number of test cases. The first line of each test case contains a positive integer $n$ ($1 \leq n \leq 100$) — the number of cosplayers in the initial line. The second line of each test case contains a binary string $s$ of length $n$ — describing the cosplayers already in line. Each character of the string is either 0 describing a male, or 1 describing a female. Note that there is no limit on the sum of $n$. -----Output----- For each test case, print the minimum number of cosplayers you need to invite so that the group picture of all the cosplayers is beautiful. -----Examples----- Input 9 3 000 3 001 3 010 3 011 3 100 3 101 3 110 3 111 19 1010110000100000101 Output 4 2 1 0 2 0 0 0 17 -----Note----- In the first test case, for each pair of adjacent cosplayers, you can invite two female cosplayers to stand in between them. Then, $000 \rightarrow 0110110$. In the third test case, you can invite one female cosplayer to stand next to the second cosplayer. Then, $010 \rightarrow 0110$.
{"inputs": ["9\n3\n000\n3\n001\n3\n010\n3\n011\n3\n100\n3\n101\n3\n110\n3\n111\n19\n1010110000100000101\n"], "outputs": ["4\n2\n1\n0\n2\n0\n0\n0\n17\n"]}
568
102
coding
Solve the programming task below in a Python markdown code block. There are N+1 towns. The i-th town is being attacked by A_i monsters. We have N heroes. The i-th hero can defeat monsters attacking the i-th or (i+1)-th town, for a total of at most B_i monsters. What is the maximum total number of monsters the heroes can cooperate to defeat? -----Constraints----- - All values in input are integers. - 1 \leq N \leq 10^5 - 1 \leq A_i \leq 10^9 - 1 \leq B_i \leq 10^9 -----Input----- Input is given from Standard Input in the following format: N A_1 A_2 ... A_{N+1} B_1 B_2 ... B_N -----Output----- Print the maximum total number of monsters the heroes can defeat. -----Sample Input----- 2 3 5 2 4 5 -----Sample Output----- 9 If the heroes choose the monsters to defeat as follows, they can defeat nine monsters in total, which is the maximum result. - The first hero defeats two monsters attacking the first town and two monsters attacking the second town. - The second hero defeats three monsters attacking the second town and two monsters attacking the third town.
{"inputs": ["1\n1 1\n1\n", "2\n3 5 2\n7 5", "2\n3 5 1\n7 5", "2\n3 5 1\n0 5", "2\n2 5 1\n0 5", "2\n2 9 1\n0 5", "2\n2 9 1\n0 2", "2\n2 1 1\n0 2"], "outputs": ["1\n", "10\n", "9\n", "5\n", "5\n", "5\n", "2\n", "2\n"]}
282
140
coding
Solve the programming task below in a Python markdown code block. Santosh has a farm at Byteland. He has a very big family to look after. His life takes a sudden turn and he runs into a financial crisis. After giving all the money he has in his hand, he decides to sell his plots. The speciality of his land is that it is rectangular in nature. Santosh comes to know that he will get more money if he sells square shaped plots. So keeping this in mind, he decides to divide his land into minimum possible number of square plots, such that each plot has the same area, and the plots divide the land perfectly. He does this in order to get the maximum profit out of this. So your task is to find the minimum number of square plots with the same area, that can be formed out of the rectangular land, such that they divide it perfectly. -----Input----- - The first line of the input contains $T$, the number of test cases. Then $T$ lines follow. - The first and only line of each test case contains two space-separated integers, $N$ and $M$, the length and the breadth of the land, respectively. -----Output----- For each test case, print the minimum number of square plots with equal area, such that they divide the farm land perfectly, in a new line. -----Constraints----- $1 \le T \le 20$ $1 \le M \le 10000$ $1 \le N \le 10000$ -----Sample Input:----- 2 10 15 4 6 -----SampleOutput:----- 6 6
{"inputs": ["2\n9 4\n2 2", "2\n9 4\n2 3", "2\n5 4\n2 3", "2\n8 4\n2 3", "2\n4 1\n2 3", "2\n8 5\n4 1", "2\n9 8\n2 1", "2\n9 3\n2 2"], "outputs": ["36\n1\n", "36\n6\n", "20\n6\n", "2\n6\n", "4\n6\n", "40\n4\n", "72\n2\n", "3\n1\n"]}
348
147
coding
Solve the programming task below in a Python markdown code block. You're a programmer in a SEO company. The SEO specialist of your company gets the list of all project keywords everyday, then he looks for the longest keys to analyze them. You will get the list with keywords and must write a simple function that returns the biggest search keywords and sorts them in lexicographical order. For instance you might get: ```python 'key1', 'key2', 'key3', 'key n', 'bigkey2', 'bigkey1' ``` And your function should return: ```python "'bigkey1', 'bigkey2'" ``` Don't forget to rate this kata! Thanks :) Also feel free to reuse/extend the following starter code: ```python def the_biggest_search_keys(*keys): ```
{"functional": "_inputs = [['key1', 'key22', 'key333'], ['coding', 'sorting', 'tryruby'], ['small keyword', 'how to coding?', 'very nice kata', 'a lot of keys', 'I like Ruby!!!'], ['pippi']]\n_outputs = [[\"'key333'\"], [\"'sorting', 'tryruby'\"], [\"'I like Ruby!!!', 'how to coding?', 'very nice kata'\"], [\"'pippi'\"]]\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(the_biggest_search_keys(*i), o[0])"}
169
254
coding
Solve the programming task below in a Python markdown code block. Taru likes reading. Every month he gets a copy of the magazine "BIT". The magazine contains information about the latest advancements in technology. Taru reads the book at night and writes the page number to which he has read on a piece of paper so that he can continue from there the next day. But sometimes the page number is not printed or is so dull that it is unreadable. To make matters worse Taru's brother who is really naughty tears of some of the pages of the Magazine and throws them in the dustbin. He remembers the number of leaves he had torn but he does not remember which page numbers got removed. When Taru finds this out he is furious and wants to beat him up. His brother apologizes, and says he won't ever do this again. But Taru did not want to be easy on him and he says "I will leave you only if you help me find the answer to this. I will tell you how many pages (Printed sides) were there in the Magazine plus the pages on which the page numbers were not printed. You already know the number of leaves you tore (T). Can you tell me the expected sum of the page numbers left in the Magazine?" Taru's brother replied "huh!! This is a coding problem". Please help Taru's brother. Note: The magazine is like a standard book with all odd page numbers in front and the successive even page number on its back. If the book contains 6 pages, Page number 1 and Page number 2 are front and back respectively. Tearing a leaf removes both the front and back page numbers. -----Input----- The first line contains the number of test cases t. 3t lines follow. The first line of each test case contains the number of pages (printed sides) in the book. The second line's first integer is F, F integers follow which tell us the numbers of the page numbers not printed. The third line contains a single integer telling us the number of leaves Taru's brother tore. -----Output----- Output one real number correct up to 4 decimal digits which is equal to the expected sum of the page numbers left in the book. -----Constraints----- Number of printed Sides<=2000. All other values abide by the number of printed sides. -----Example----- Input: 2 10 2 1 2 2 10 1 8 0 Output: 31.2000 47.0000
{"inputs": ["2\n10\n2 1 2\n2\n10\n1 8\n0"], "outputs": ["31.2000\n47.0000"]}
537
46
coding
Solve the programming task below in a Python markdown code block. Facetook is a well known social network website, and it will launch a new feature called Facetook Priority Wall. This feature will sort all posts from your friends according to the priority factor (it will be described). This priority factor will be affected by three types of actions: * 1. "X posted on Y's wall" (15 points), * 2. "X commented on Y's post" (10 points), * 3. "X likes Y's post" (5 points). X and Y will be two distinct names. And each action will increase the priority factor between X and Y (and vice versa) by the above value of points (the priority factor between X and Y is the same as the priority factor between Y and X). You will be given n actions with the above format (without the action number and the number of points), and you have to print all the distinct names in these actions sorted according to the priority factor with you. Input The first line contains your name. The second line contains an integer n, which is the number of actions (1 ≤ n ≤ 100). Then n lines follow, it is guaranteed that each one contains exactly 1 action in the format given above. There is exactly one space between each two words in a line, and there are no extra spaces. All the letters are lowercase. All names in the input will consist of at least 1 letter and at most 10 small Latin letters. Output Print m lines, where m is the number of distinct names in the input (excluding yourself). Each line should contain just 1 name. The names should be sorted according to the priority factor with you in the descending order (the highest priority factor should come first). If two or more names have the same priority factor, print them in the alphabetical (lexicographical) order. Note, that you should output all the names that are present in the input data (excluding yourself), even if that person has a zero priority factor. The lexicographical comparison is performed by the standard "<" operator in modern programming languages. The line a is lexicographically smaller than the line b, if either a is the prefix of b, or if exists such an i (1 ≤ i ≤ min(|a|, |b|)), that ai < bi, and for any j (1 ≤ j < i) aj = bj, where |a| and |b| stand for the lengths of strings a and b correspondently. Examples Input ahmed 3 ahmed posted on fatma's wall fatma commented on ahmed's post mona likes ahmed's post Output fatma mona Input aba 1 likes likes posted's post Output likes posted
{"inputs": ["a\n1\nb likes c's post\n", "a\n1\nc likes b's post\n", "aba\n1\nkikes likes posted's post\n", "aba\n1\nlieks likes posted's post\n", "aba\n1\nlheks likes posted's post\n", "aba\n1\nlikes likes posted's post\n", "fgzrn\n3\nzhl likes fgzrn's post\nxryet likes fgzrn's post\nzhl commented on fgzrn's post\n", "ggzrn\n3\nzhl likes fgzrn's post\nxryet likes fgzrn's post\nzhl commented on fgzrn's post\n"], "outputs": ["b\nc\n", "b\nc\n", "kikes\nposted\n", "lieks\nposted\n", "lheks\nposted\n", "likes\nposted\n", "zhl\nxryet\n", "fgzrn\nxryet\nzhl\n"]}
592
218
coding
Solve the programming task below in a Python markdown code block. Takahashi has an empty string S and a variable x whose initial value is 0. Also, we have a string T consisting of `0` and `1`. Now, Takahashi will do the operation with the following two steps |T| times. * Insert a `0` or a `1` at any position of S of his choice. * Then, increment x by the sum of the digits in the odd positions (first, third, fifth, ...) of S. For example, if S is `01101`, the digits in the odd positions are `0`, `1`, `1` from left to right, so x is incremented by 2. Print the maximum possible final value of x in a sequence of operations such that S equals T in the end. Constraints * 1 \leq |T| \leq 2 \times 10^5 * T consists of `0` and `1`. Input Input is given from Standard Input in the following format: T Output Print the maximum possible final value of x in a sequence of operations such that S equals T in the end. Examples Input 1101 Output 5 Input 0111101101 Output 26
{"inputs": ["1001", "1000", "1010", "0010", "0001", "0000", "1110", "1011"], "outputs": ["5\n", "4\n", "6\n", "3\n", "2\n", "0\n", "6\n", "6\n"]}
281
86
coding
Solve the programming task below in a Python markdown code block. The positive odd numbers are sorted in ascending order as $1,3,5,7,9,11,13,15,17,19\ldots$, and grouped as $(1),(3,5),(7,9,11),(13,15,17,19),...$ and so on. Thus, the first group is $\left(1\right)$, the second group is $(3,5)$, the third group is $(7,9,11)$, etc. In general, the $k^{\text{th}}$ group contains the next $\boldsymbol{\mbox{k}}$ elements of the sequence. Given $\boldsymbol{\mbox{k}}$, find the sum of the elements of the $k^{\text{th}}$ group. For example, for $k=3$, the answer is $27$: Complete the function sumOfGroup with input integer $\boldsymbol{\mbox{k}}$. Return the sum of the elements of the $\boldsymbol{\mbox{k}}$th group. Constraints $1\leq k\leq10^6$ Subtasks For $50\%$ of the maximum score, $k\leq10^3$ Sample Input $k=3$ Sample Output $27$ Explanation We have $k=3$. The $3$rd group is $(7,9,11)$ and the sum of its elements is $7+9+11=27$.
{"inputs": ["3\n"], "outputs": ["27\n"]}
342
15
coding
Solve the programming task below in a Python markdown code block. Demiurges Shambambukli and Mazukta love to watch the games of ordinary people. Today, they noticed two men who play the following game. There is a rooted tree on n nodes, m of which are leaves (a leaf is a nodes that does not have any children), edges of the tree are directed from parent to children. In the leaves of the tree integers from 1 to m are placed in such a way that each number appears exactly in one leaf. Initially, the root of the tree contains a piece. Two players move this piece in turns, during a move a player moves the piece from its current nodes to one of its children; if the player can not make a move, the game ends immediately. The result of the game is the number placed in the leaf where a piece has completed its movement. The player who makes the first move tries to maximize the result of the game and the second player, on the contrary, tries to minimize the result. We can assume that both players move optimally well. Demiurges are omnipotent, so before the game they can arbitrarily rearrange the numbers placed in the leaves. Shambambukli wants to rearrange numbers so that the result of the game when both players play optimally well is as large as possible, and Mazukta wants the result to be as small as possible. What will be the outcome of the game, if the numbers are rearranged by Shambambukli, and what will it be if the numbers are rearranged by Mazukta? Of course, the Demiurges choose the best possible option of arranging numbers. -----Input----- The first line contains a single integer n — the number of nodes in the tree (1 ≤ n ≤ 2·10^5). Each of the next n - 1 lines contains two integers u_{i} and v_{i} (1 ≤ u_{i}, v_{i} ≤ n) — the ends of the edge of the tree; the edge leads from node u_{i} to node v_{i}. It is guaranteed that the described graph is a rooted tree, and the root is the node 1. -----Output----- Print two space-separated integers — the maximum possible and the minimum possible result of the game. -----Examples----- Input 5 1 2 1 3 2 4 2 5 Output 3 2 Input 6 1 2 1 3 3 4 1 5 5 6 Output 3 3 -----Note----- Consider the first sample. The tree contains three leaves: 3, 4 and 5. If we put the maximum number 3 at node 3, then the first player moves there and the result will be 3. On the other hand, it is easy to see that for any rearrangement the first player can guarantee the result of at least 2. In the second sample no matter what the arragment is the first player can go along the path that ends with a leaf with number 3.
{"inputs": ["1\n", "1\n", "2\n1 2\n", "2\n1 2\n", "3\n1 2\n1 3\n", "3\n1 2\n1 3\n", "3\n1 2\n2 3\n", "5\n1 2\n1 3\n2 4\n2 5\n"], "outputs": ["1 1\n", "1 1", "1 1\n", "1 1", "2 2\n", "2 2", "1 1\n", "3 2\n"]}
649
131
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. On a single-threaded CPU, we execute a program containing n functions. Each function has a unique ID between 0 and n-1. Function calls are stored in a call stack: when a function call starts, its ID is pushed onto the stack, and when a function call ends, its ID is popped off the stack. The function whose ID is at the top of the stack is the current function being executed. Each time a function starts or ends, we write a log with the ID, whether it started or ended, and the timestamp. You are given a list logs, where logs[i] represents the ith log message formatted as a string "{function_id}:{"start" | "end"}:{timestamp}". For example, "0:start:3" means a function call with function ID 0 started at the beginning of timestamp 3, and "1:end:2" means a function call with function ID 1 ended at the end of timestamp 2. Note that a function can be called multiple times, possibly recursively. A function's exclusive time is the sum of execution times for all function calls in the program. For example, if a function is called twice, one call executing for 2 time units and another call executing for 1 time unit, the exclusive time is 2 + 1 = 3. Return the exclusive time of each function in an array, where the value at the ith index represents the exclusive time for the function with ID i.   Please complete the following python code precisely: ```python class Solution: def exclusiveTime(self, n: int, logs: List[str]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(n = 2, logs = [\"0:start:0\",\"1:start:2\",\"1:end:5\",\"0:end:6\"]) == [3,4]\n assert candidate(n = 1, logs = [\"0:start:0\",\"0:start:2\",\"0:end:5\",\"0:start:6\",\"0:end:6\",\"0:end:7\"]) == [8]\n assert candidate(n = 2, logs = [\"0:start:0\",\"0:start:2\",\"0:end:5\",\"1:start:6\",\"1:end:6\",\"0:end:7\"]) == [7,1]\n\n\ncheck(Solution().exclusiveTime)"}
352
157
coding
Solve the programming task below in a Python markdown code block. We have two distinct integers A and B. Print the integer K such that |A - K| = |B - K|. If such an integer does not exist, print IMPOSSIBLE instead. -----Constraints----- - All values in input are integers. - 0 \leq A,\ B \leq 10^9 - A and B are distinct. -----Input----- Input is given from Standard Input in the following format: A B -----Output----- Print the integer K satisfying the condition. If such an integer does not exist, print IMPOSSIBLE instead. -----Sample Input----- 2 16 -----Sample Output----- 9 |2 - 9| = 7 and |16 - 9| = 7, so 9 satisfies the condition.
{"inputs": ["0 4", "0 2", "1 7", "6 4", "0 3", "0 3\n", "0 1\n", "1 13"], "outputs": ["2\n", "1\n", "4\n", "5\n", "IMPOSSIBLE", "IMPOSSIBLE\n", "IMPOSSIBLE\n", "7\n"]}
176
89
coding
Solve the programming task below in a Python markdown code block. The Fibonacci sequence $F_0, F_1, \ldots$ is a special infinite sequence of non-negative integers, where $F_0 = 0$, $F_1 = 1$ and for each integer $n \ge 2$, $F_n = F_{n-1} + F_{n-2}$. Consider the sequence $D$ of the last decimal digits of the first $N$ Fibonacci numbers, i.e. $D = (F_0 \% 10, F_1 \% 10, \ldots, F_{N-1} \% 10)$. Now, you should perform the following process: - Let $D = (D_1, D_2, \ldots, D_l)$. - If $l = 1$, the process ends. - Create a new sequence $E = (D_2, D_4, \ldots, D_{2 \lfloor l/2 \rfloor})$. In other words, $E$ is the sequence created by removing all odd-indexed elements from $D$. - Change $D$ to $E$. When this process terminates, the sequence $D$ contains only one number. You have to find this number. -----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$. -----Output----- For each test case, print a single line containing one integer ― the last remaining number. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le N \le 10^{18}$ -----Subtasks----- Subtask #1 (20 points): - $1 \le T \le 10^5$ - $1 \le N \le 10^7$ Subtask #2 (80 points): original constraints -----Example Input----- 1 9 -----Example Output----- 3 -----Explanation----- Example case 1: The first $N$ Fibonacci numbers are $(0, 1, 1, 2, 3, 5, 8, 13, 21)$. The sequence $D$ is $(0, 1, 1, 2, 3, 5, 8, 3, 1) \rightarrow (1, 2, 5, 3) \rightarrow (2, 3) \rightarrow (3)$.
{"inputs": ["1\n9"], "outputs": ["3"]}
558
14
coding
Solve the programming task below in a Python markdown code block. Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and n is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves. Input The first line contains an integer n (1 ≤ n ≤ 300) which is the number of students. The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 ≤ bi ≤ 109) which represent the height of the student occupying the i-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. a and b coincide as multisets. Output In the first line print an integer k (0 ≤ k ≤ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pi, pi + 1 (1 ≤ pi ≤ n - 1) means that Vasya should swap students occupying places pi and pi + 1. Examples Input 4 1 2 3 2 3 2 1 2 Output 4 2 3 1 2 3 4 2 3 Input 2 1 100500 1 100500 Output 0
{"inputs": ["4\n2 1 3 2\n3 2 1 2\n", "4\n2 2 3 1\n3 2 1 2\n", "4\n1 2 3 4\n3 2 1 4\n", "4\n2 2 3 2\n3 2 2 2\n", "4\n1 2 3 2\n3 2 1 2\n", "2\n1 100500\n1 100500\n", "1\n800950546\n800950546\n", "1\n873725529\n873725529\n"], "outputs": ["2\n1 2\n2 3\n", "3\n1 2\n3 4\n2 3\n", "3\n2 3\n1 2\n2 3\n", "2\n1 2\n2 3\n", "3\n2 3\n1 2\n2 3\n", "0\n", "0\n", "0\n"]}
498
260
coding
Solve the programming task below in a Python markdown code block. Petya was late for the lesson too. The teacher gave him an additional task. For some array a Petya should find the number of different ways to select non-empty subset of elements from it in such a way that their product is equal to a square of some integer. Two ways are considered different if sets of indexes of elements chosen by these ways are different. Since the answer can be very large, you should find the answer modulo 10^9 + 7. -----Input----- First line contains one integer n (1 ≤ n ≤ 10^5) — the number of elements in the array. Second line contains n integers a_{i} (1 ≤ a_{i} ≤ 70) — the elements of the array. -----Output----- Print one integer — the number of different ways to choose some elements so that their product is a square of a certain integer modulo 10^9 + 7. -----Examples----- Input 4 1 1 1 1 Output 15 Input 4 2 2 2 2 Output 7 Input 5 1 2 4 5 8 Output 7 -----Note----- In first sample product of elements chosen by any way is 1 and 1 = 1^2. So the answer is 2^4 - 1 = 15. In second sample there are six different ways to choose elements so that their product is 4, and only one way so that their product is 16. So the answer is 6 + 1 = 7.
{"inputs": ["1\n64\n", "1\n64\n", "1\n33\n", "1\n42\n", "1\n69\n", "2\n15 4\n", "2\n15 5\n", "2\n70 70\n"], "outputs": ["1\n", "1\n", "0\n", "0\n", "0\n", "1\n", "0\n", "1\n"]}
348
101
coding
Solve the programming task below in a Python markdown code block. Take a string and return a hash with all the ascii values of the characters in the string. Returns nil if the string is empty. The key is the character, and the value is the ascii value of the character. Repeated characters are to be ignored and non-alphebetic characters as well. Also feel free to reuse/extend the following starter code: ```python def char_to_ascii(string): ```
{"functional": "_inputs = [[''], ['a'], ['aaa'], ['hello world'], ['ABaa ^']]\n_outputs = [[None], [{'a': 97}], [{'a': 97}], [{'h': 104, 'e': 101, 'l': 108, 'o': 111, 'w': 119, 'r': 114, 'd': 100}], [{'A': 65, 'B': 66, 'a': 97}]]\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(char_to_ascii(*i), o[0])"}
95
261
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an integer n representing the number of houses on a number line, numbered from 0 to n - 1. Additionally, you are given a 2D integer array offers where offers[i] = [starti, endi, goldi], indicating that ith buyer wants to buy all the houses from starti to endi for goldi amount of gold. As a salesman, your goal is to maximize your earnings by strategically selecting and selling houses to buyers. Return the maximum amount of gold you can earn. Note that different buyers can't buy the same house, and some houses may remain unsold.   Please complete the following python code precisely: ```python class Solution: def maximizeTheProfit(self, n: int, offers: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(n = 5, offers = [[0,0,1],[0,2,2],[1,3,2]]) == 3\n assert candidate(n = 5, offers = [[0,0,1],[0,2,10],[1,3,2]]) == 10\n\n\ncheck(Solution().maximizeTheProfit)"}
179
89
coding
Solve the programming task below in a Python markdown code block. You are given an array a. Some element of this array a_{i} is a local minimum iff it is strictly less than both of its neighbours (that is, a_{i} < a_{i} - 1 and a_{i} < a_{i} + 1). Also the element can be called local maximum iff it is strictly greater than its neighbours (that is, a_{i} > a_{i} - 1 and a_{i} > a_{i} + 1). Since a_1 and a_{n} have only one neighbour each, they are neither local minima nor local maxima. An element is called a local extremum iff it is either local maximum or local minimum. Your task is to calculate the number of local extrema in the given array. -----Input----- The first line contains one integer n (1 ≤ n ≤ 1000) — the number of elements in array a. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 1000) — the elements of array a. -----Output----- Print the number of local extrema in the given array. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 5 2 5 Output 2
{"inputs": ["1\n1\n", "1\n1\n", "1\n2\n", "1\n0\n", "1\n4\n", "1\n3\n", "1\n8\n", "1\n5\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
296
86
coding
Solve the programming task below in a Python markdown code block. Given an integer, $n$, print the following values for each integer $\boldsymbol{i}$ from $\mbox{1}$ to $n$: Decimal Octal Hexadecimal (capitalized) Binary Function Description Complete the print_formatted function in the editor below. print_formatted has the following parameters: int number: the maximum value to print Prints The four values must be printed on a single line in the order specified above for each $\boldsymbol{i}$ from $\mbox{1}$ to $number$. Each value should be space-padded to match the width of the binary value of $number$ and the values should be separated by a single space. Input Format A single integer denoting $n$. Constraints $1\leq n\leq99$ Sample Input 17 Sample Output 1 1 1 1 2 2 2 10 3 3 3 11 4 4 4 100 5 5 5 101 6 6 6 110 7 7 7 111 8 10 8 1000 9 11 9 1001 10 12 A 1010 11 13 B 1011 12 14 C 1100 13 15 D 1101 14 16 E 1110 15 17 F 1111 16 20 10 10000 17 21 11 10001
{"inputs": ["17\n"], "outputs": [" 1 1 1 1\n 2 2 2 10\n 3 3 3 11\n 4 4 4 100\n 5 5 5 101\n 6 6 6 110\n 7 7 7 111\n 8 10 8 1000\n 9 11 9 1001\n 10 12 A 1010\n 11 13 B 1011\n 12 14 C 1100\n 13 15 D 1101\n 14 16 E 1110\n 15 17 F 1111\n 16 20 10 10000\n 17 21 11 10001\n"]}
463
288
coding
Solve the programming task below in a Python markdown code block. Alice recently converted all the positive integers from 1 to 2^{N} - 1 (both inclusive) into binary strings and stored them in an array S. Note that the binary strings do not have leading zeroes. While she was out, Bob sorted all the elements of S in lexicographical increasing order. Let S_{i} denotes the i^{th} string in the sorted array. Alice defined a function F such that F(S_{i}) is equal to the count of 1 in the string S_{i}. For example, F(101) = 2 and F(1000) = 1. Given a positive integer K, find the value of \sum_{i = 1}^K F(S_{i}). String P is lexicographically smaller than string Q if one of the following satisfies: P is a prefix of Q and P \neq Q. There exists an index i such that P_{i} < Q_{i} and for all j < i, P_{j}=Q_{j}. ------ Input Format ------ - The first line will contain an integer T - number of test cases. Then the test cases follow. - The first and only line of each test case contains two integers N and K. ------ Output Format ------ For each test case, output the value \sum_{i = 1}^K F(S_{i}). ------ Constraints ------ $1 ≤ T ≤ 3000$ $1 ≤ N ≤ 50$ $1 ≤ K ≤ 2^{N} - 1$ - Sum of $N$ over all test cases does not exceed $10^{5}$. ----- Sample Input 1 ------ 3 3 2 3 4 3 7 ----- Sample Output 1 ------ 2 5 12 ----- explanation 1 ------ Converting positive integers to Binary Strings: - $1 = 1$ - $2 = 10$ - $3 = 11$ - $4 = 100$ - $5 = 101$ - $6 = 110$ - $7 = 111$ Sorting Binary Strings in lexicographical order: After sorting, the strings will appear in the following order: $[1, 10, 100, 101, 11, 110, 111]$. Test case $1$: $F(S_{1}) + F(S_{2}) = F(1) + F(10) = 1 + 1 = 2$. Test case $2$: $F(S_{1}) + F(S_{2}) + F(S_{3}) + F(S_{4}) = F(1) + F(10) + F(100) + F(101) = 1 + 1 + 1 + 2 = 5$.
{"inputs": ["3\n3 2\n3 4\n3 7"], "outputs": ["2\n5\n12\n"]}
641
30
coding
Solve the programming task below in a Python markdown code block. The task is simply stated. Given an integer n (3 < n < 10^(9)), find the length of the smallest list of [*perfect squares*](https://en.wikipedia.org/wiki/Square_number) which add up to n. Come up with the best algorithm you can; you'll need it! Examples: sum_of_squares(17) = 2 17 = 16 + 1 (4 and 1 are perfect squares). sum_of_squares(15) = 4 15 = 9 + 4 + 1 + 1. There is no way to represent 15 as the sum of three perfect squares. sum_of_squares(16) = 1 16 itself is a perfect square. Time constraints: 5 easy (sample) test cases: n < 20 5 harder test cases: 1000 < n < 15000 5 maximally hard test cases: 5 * 1e8 < n < 1e9 ```if:java 300 random maximally hard test cases: 1e8 < n < 1e9 ``` ```if:c# 350 random maximally hard test cases: 1e8 < n < 1e9 ``` ```if:python 15 random maximally hard test cases: 1e8 < n < 1e9 ``` ```if:ruby 25 random maximally hard test cases: 1e8 < n < 1e9 ``` ```if:javascript 100 random maximally hard test cases: 1e8 < n < 1e9 ``` ```if:crystal 250 random maximally hard test cases: 1e8 < n < 1e9 ``` ```if:cpp Random maximally hard test cases: 1e8 < n < 1e9 ``` Also feel free to reuse/extend the following starter code: ```python def sum_of_squares(n): ```
{"functional": "_inputs = [[15], [16], [17], [18], [19], [2017], [1008], [3456], [4000], [12321], [661915703], [999887641], [999950886], [999951173], [999998999]]\n_outputs = [[4], [1], [2], [2], [3], [2], [4], [3], [2], [1], [4], [1], [3], [2], [4]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(sum_of_squares(*i), o[0])"}
457
301
coding
Solve the programming task below in a Python markdown code block. Are you fond of collecting some kind of stuff? Mike is crazy about collecting stamps. He is an active member of Stamp Collecting Сommunity(SCC). SCC consists of N members which are fond of philately. A few days ago Mike argued with the others from SCC. Mike told them that all stamps of the members could be divided in such a way that i'th member would get i postage stamps. Now Mike wants to know if he was right. The next SCC meeting is tomorrow. Mike still has no answer. So, help Mike! There are N members in the SCC, i'th member has Ci stamps in his collection. Your task is to determine if it is possible to redistribute C1 + C2 + ... + Cn stamps among the members of SCC thus that i'th member would get i stamps. -----Input----- The first line contains one integer N, denoting the number of members of SCC. The second line contains N integers Ci, denoting the numbers of the stamps in the collection of i'th member. -----Output----- The first line should contain YES, if we can obtain the required division, otherwise NO. -----Constraints----- 1 ≤ N ≤ 100 000; 1 ≤ Ci ≤ 109. -----Examples----- Input: 5 7 4 1 1 2 Output: YES Input: 5 1 1 1 1 1 Output: NO
{"inputs": ["5\n1 1 1 1 0", "5\n1 2 1 1 0", "5\n0 2 1 1 0", "5\n0 3 1 1 0", "5\n0 3 1 0 0", "5\n0 3 1 0 1", "5\n0 3 0 0 1", "5\n0 3 0 0 0"], "outputs": ["NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n"]}
312
142
coding
Solve the programming task below in a Python markdown code block. Take debugging to a whole new level: Given a string, remove every *single* bug. This means you must remove all instances of the word 'bug' from within a given string, *unless* the word is plural ('bugs'). For example, given 'obugobugobuoobugsoo', you should return 'ooobuoobugsoo'. Another example: given 'obbugugo', you should return 'obugo'. Note that all characters will be lowercase. Happy squishing! Also feel free to reuse/extend the following starter code: ```python def debug(s): ```
{"functional": "_inputs = [['obugobugobuoobugsoo'], ['obbugugo'], ['bugs bunny'], ['bugs buggy'], ['oaiwjefbugoijoijapsbugsdoibugbugjfoijasdfbugsbug'], ['bugbugbugiahweoifuhiaasnoidfhnbugbugs'], ['bugsbugswaoeifhiauwehfoiwubugshefjnviouah'], ['bugbugbugbug'], ['bugsbugsbugsbugs'], ['buggybugs'], ['oaisjdfowjefpoibugsjsofijeo oi bugs o bug f bug poaj sfd s']]\n_outputs = [['ooobuoobugsoo'], ['obugo'], ['bugs bunny'], ['bugs gy'], ['oaiwjefoijoijapsbugsdoijfoijasdfbugs'], ['iahweoifuhiaasnoidfhnbugs'], ['bugsbugswaoeifhiauwehfoiwubugshefjnviouah'], [''], ['bugsbugsbugsbugs'], ['gybugs'], ['oaisjdfowjefpoibugsjsofijeo oi bugs o f poaj sfd s']]\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(debug(*i), o[0])"}
138
393
coding
Solve the programming task below in a Python markdown code block. Chef will not be able to attend the birthday of his best friend Rock. He promised Rock that this will not be the case on his half birthday. To keep his promise Chef must know Rock’s next half birthday accurately. Being busy, he is assigning this work to you. Half birthday is the day that occurs exactly between two subsequent birthdays. You will be provided with Rock’s birthdate and birth month, you will have to figure out his half birthday. $Note$: Consider every year to be a leap year and all months are displayed in lowercase English characters. -----Input:----- - The first line of the input contains a single integer $T$ denoting the number of test cases. - The description of each of the $T$ test cases contains an integer $d$ followed by a string, denoting month $m$. - Here $d$ denotes day of a month and $m$ denotes the month of a year respectively. -----Output:----- For each test case print an integer $d1$ followed by a string, denoting month $m1$, which overall denotes date and month of Rock’s half birthday. -----Constraints:----- - $1 \leq T \leq 10^5$ - $1 \leq d , d1 \leq 31$ - $january \leq m , m1 \leq december$ -----Sample Input:----- 3 15 january 31 august 10 october -----Sample Output:----- 16 july 1 march 10 april
{"inputs": ["3\n15 january\n31 august\n10 october"], "outputs": ["16 july\n1 march\n10 april"]}
334
36
coding
Solve the programming task below in a Python markdown code block. Let's consider a random permutation p_{1}, p_{2}, ..., p_{N} of numbers 1, 2, ..., N and calculate the value F=(X_{2}+...+X_{N-1})^{K}, where X_{i} equals 1 if one of the following two conditions holds: p_{i-1} < p_{i} > p_{i+1} or p_{i-1} > p_{i} < p_{i+1} and X_{i} equals 0 otherwise. What is the expected value of F? Input Format: The first line contains two integers K and N. Output Format: Print the expected value of F as an irreducible fraction p / q. Follow sample input for more clarification. Constraints: 1000 <= N <= 10^{9} 1 <= K <= 5 Sample input 1 1000 Sample Output 1996 / 3
{"inputs": ["1 1000\n"], "outputs": ["1996 / 3\n"]}
220
25
coding
Solve the programming task below in a Python markdown code block. It's another Start[c]up finals, and that means there is pizza to order for the onsite contestants. There are only 2 types of pizza (obviously not, but let's just pretend for the sake of the problem), and all pizzas contain exactly S slices. It is known that the i-th contestant will eat s_{i} slices of pizza, and gain a_{i} happiness for each slice of type 1 pizza they eat, and b_{i} happiness for each slice of type 2 pizza they eat. We can order any number of type 1 and type 2 pizzas, but we want to buy the minimum possible number of pizzas for all of the contestants to be able to eat their required number of slices. Given that restriction, what is the maximum possible total happiness that can be achieved? -----Input----- The first line of input will contain integers N and S (1 ≤ N ≤ 10^5, 1 ≤ S ≤ 10^5), the number of contestants and the number of slices per pizza, respectively. N lines follow. The i-th such line contains integers s_{i}, a_{i}, and b_{i} (1 ≤ s_{i} ≤ 10^5, 1 ≤ a_{i} ≤ 10^5, 1 ≤ b_{i} ≤ 10^5), the number of slices the i-th contestant will eat, the happiness they will gain from each type 1 slice they eat, and the happiness they will gain from each type 2 slice they eat, respectively. -----Output----- Print the maximum total happiness that can be achieved. -----Examples----- Input 3 12 3 5 7 4 6 7 5 9 5 Output 84 Input 6 10 7 4 7 5 8 8 12 5 8 6 11 6 3 3 7 5 9 6 Output 314 -----Note----- In the first example, you only need to buy one pizza. If you buy a type 1 pizza, the total happiness will be 3·5 + 4·6 + 5·9 = 84, and if you buy a type 2 pizza, the total happiness will be 3·7 + 4·7 + 5·5 = 74.
{"inputs": ["2 9\n6 1 7\n6 7 1\n", "2 9\n6 1 7\n6 7 1\n", "2 9\n5 1 7\n6 7 1\n", "2 9\n5 1 7\n6 9 1\n", "2 10\n7 2 1\n7 1 2\n", "2 10\n9 1 2\n9 2 1\n", "2 10\n9 1 2\n9 2 1\n", "2 10\n7 2 1\n7 1 2\n"], "outputs": ["84\n", "84\n", "77\n", "89\n", "28\n", "36\n", "36\n", "28\n"]}
517
194
coding
Solve the programming task below in a Python markdown code block. ------Read problems statements in Hindi, Mandarin chinese , Russian and Vietnamese as well. ------ Chef likes to play with cards a lot. Today, he's playing a game with three cards. Each card has a letter written on the top face and another (possibly identical) letter written on the bottom face. Chef can arbitrarily reorder the cards and/or flip any of the cards in any way he wishes (in particular, he can leave the cards as they were). He wants to do it in such a way that the letters on the top faces of the cards, read left to right, would spell out the name of his favorite friend Bob. Determine whether it is possible for Chef to spell "bob" with these cards. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single string with length $3$ denoting the characters written on the top faces of the first, second and third card. The second line contains a single string with length $3$ denoting the characters written on the bottom faces of the first, second and third card. ------ Output ------ For each test case, print a single line containing the string "yes" (without quotes) if Chef can spell "bob" or "no" (without quotes) if he cannot. ------ Constraints ------ $1 ≤ T ≤ 20,000$ each string contains only lowercase English letters ------ Subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 3 bob rob dbc ocb boc obc ----- Sample Output 1 ------ yes yes no ----- explanation 1 ------ Example case 1: The top faces of the cards already spell out "bob". Example case 2: Chef can rearrange the cards in the following way to spell "bob": the second card non-flipped, the first card flipped and the third card flipped. Example case 3: There is no way for Chef to spell out "bob".
{"inputs": ["3\nbob\nrob\ndbc\nocb\nboc\nobc"], "outputs": ["yes\nyes\nno"]}
463
32
coding
Solve the programming task below in a Python markdown code block. There are N squares numbered 1 to N from left to right. Each square has a character written on it, and Square i has a letter s_i. Besides, there is initially one golem on each square. Snuke cast Q spells to move the golems. The i-th spell consisted of two characters t_i and d_i, where d_i is `L` or `R`. When Snuke cast this spell, for each square with the character t_i, all golems on that square moved to the square adjacent to the left if d_i is `L`, and moved to the square adjacent to the right if d_i is `R`. However, when a golem tried to move left from Square 1 or move right from Square N, it disappeared. Find the number of golems remaining after Snuke cast the Q spells. Constraints * 1 \leq N,Q \leq 2 \times 10^{5} * |s| = N * s_i and t_i are uppercase English letters. * d_i is `L` or `R`. Input Input is given from Standard Input in the following format: N Q s t_1 d_1 \vdots t_{Q} d_Q Output Print the answer. Examples Input 3 4 ABC A L B L B R A R Output 2 Input 8 3 AABCBDBA A L B R A R Output 5 Input 10 15 SNCZWRCEWB B R R R E R W R Z L S R Q L W L B R C L A L N L E R Z L S L Output 3
{"inputs": ["3 4\nACB\nA L\nB L\nB R\nA R", "3 3\nACB\nB L\nB L\nA R\n@ S", "3 3\nBCA\nB L\nB L\nA R\n@ S", "3 4\nCBB\nA L\nC L\nB R\nB R", "3 4\nACB\nA L\nB L\nB R\n@ R", "3 3\nACB\nA L\nB L\nB R\n@ R", "3 3\nACB\nA L\nB L\nA R\n@ R", "3 3\nACB\nA L\nB L\nA R\n@ S"], "outputs": ["2\n", "3\n", "1\n", "0\n", "2\n", "2\n", "2\n", "2\n"]}
396
198
coding
Solve the programming task below in a Python markdown code block. You are given two strings s and t consisting of lowercase Latin letters. The length of t is 2 (i.e. this string consists only of two characters). In one move, you can choose any character of s and replace it with any lowercase Latin letter. More formally, you choose some i and replace s_i (the character at the position i) with some character from 'a' to 'z'. You want to do no more than k replacements in such a way that maximizes the number of occurrences of t in s as a subsequence. Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. Input The first line of the input contains two integers n and k (2 ≤ n ≤ 200; 0 ≤ k ≤ n) — the length of s and the maximum number of moves you can make. The second line of the input contains the string s consisting of n lowercase Latin letters. The third line of the input contains the string t consisting of two lowercase Latin letters. Output Print one integer — the maximum possible number of occurrences of t in s as a subsequence if you replace no more than k characters in s optimally. Examples Input 4 2 bbaa ab Output 3 Input 7 3 asddsaf sd Output 10 Input 15 6 qwertyhgfdsazxc qa Output 16 Input 7 2 abacaba aa Output 15 Note In the first example, you can obtain the string "abab" replacing s_1 with 'a' and s_4 with 'b'. Then the answer is 3. In the second example, you can obtain the string "ssddsdd" and get the answer 10. In the fourth example, you can obtain the string "aaacaaa" and get the answer 15.
{"inputs": ["2 1\naa\nsb\n", "2 2\nas\nsa\n", "2 1\nas\nsa\n", "2 0\nas\nas\n", "2 1\naa\nbs\n", "2 2\nas\nas\n", "2 1\nar\nsa\n", "2 0\nar\nas\n"], "outputs": ["0\n", "1\n", "0\n", "1\n", "0\n", "1\n", "0\n", "0\n"]}
429
118
coding
Solve the programming task below in a Python markdown code block. In a tournament, $n$ players play against each other exactly once. Each game results in exactly one player winning. There are no ties. You have been given a scorecard containing the scores of each player at the end of the tournament. The score of a player is the total number of games the player won in the tournament. However, the scores of some players might have been erased from the scorecard. How many possible scorecards are consistent with the input scorecard? Input Format The first line contains a single integer $\boldsymbol{\boldsymbol{t}}$ denoting the number of test cases. $\boldsymbol{\boldsymbol{t}}$ test cases follow. The first line of each test case contains a single integer $n$. The second line contains $n$ space-separated integers $s_1,s_2,\ldots,s_n$. $s_i$ denotes the score of the $\boldsymbol{i}$th player. If the score of the $\boldsymbol{i}$th player has been erased, it is represented by $-1$. Constraints $1\leq t\leq20$ $1\leq n\leq40$ $-1\leq s_i<n$ Output Format For each test case, output a single line containing the answer for that test case modulo $10^9+7$. Sample Input 0 5 3 -1 -1 2 3 -1 -1 -1 4 0 1 2 3 2 1 1 4 -1 -1 -1 2 Sample Output 0 2 7 1 0 12 Explanation 0 For the first case, there are 2 scorecards possible: (0,1,2) or (1,0,2). For the second case, the valid scorecards are (1,1,1), (0,1,2), (0,2,1), (1,0,2), (1,2,0), (2,0,1), (2,1,0). For the third case, the only valid scorecard is (0,1,2,3). For the fourth case, there is no valid scorecard. It is not possible for both players to have score of 1. For the fifth case, 6-variations of {(3,1,0)[2]}, and 3 variations each of {(2,2,0)[2]} and {(2,1,1)[2]}.
{"inputs": ["5\n3\n-1 -1 2\n3\n-1 -1 -1\n4\n0 1 2 3\n2\n1 1\n4\n-1 -1 -1 2\n"], "outputs": ["2\n7\n1\n0\n12\n"]}
550
68
coding
Solve the programming task below in a Python markdown code block. The police department of your city has just started its journey. Initially, they don’t have any manpower. So, they started hiring new recruits in groups. Meanwhile, crimes keeps occurring within the city. One member of the police force can investigate only one crime during his/her lifetime. If there is no police officer free (isn't busy with crime) during the occurrence of a crime, it will go untreated. Given the chronological order of crime occurrences and recruit hirings, find the number of crimes which will go untreated. -----Input----- The first line of input will contain an integer n (1 ≤ n ≤ 10^5), the number of events. The next line will contain n space-separated integers. If the integer is -1 then it means a crime has occurred. Otherwise, the integer will be positive, the number of officers recruited together at that time. No more than 10 officers will be recruited at a time. -----Output----- Print a single integer, the number of crimes which will go untreated. -----Examples----- Input 3 -1 -1 1 Output 2 Input 8 1 -1 1 -1 -1 1 1 1 Output 1 Input 11 -1 -1 2 -1 -1 -1 -1 -1 -1 -1 -1 Output 8 -----Note----- Lets consider the second example: Firstly one person is hired. Then crime appears, the last hired person will investigate this crime. One more person is hired. One more crime appears, the last hired person will investigate this crime. Crime appears. There is no free policeman at the time, so this crime will go untreated. One more person is hired. One more person is hired. One more person is hired. The answer is one, as one crime (on step 5) will go untreated.
{"inputs": ["1\n2\n", "1\n1\n", "1\n1\n", "1\n2\n", "1\n3\n", "1\n5\n", "1\n4\n", "1\n7\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
408
86
coding
Solve the programming task below in a Python markdown code block. You are given an array of $n$ integers $a_1, a_2, \ldots, a_n$. After you watched the amazing film "Everything Everywhere All At Once", you came up with the following operation. In one operation, you choose $n-1$ elements of the array and replace each of them with their arithmetic mean (which doesn't have to be an integer). For example, from the array $[1, 2, 3, 1]$ we can get the array $[2, 2, 2, 1]$, if we choose the first three elements, or we can get the array $[\frac{4}{3}, \frac{4}{3}, 3, \frac{4}{3}]$, if we choose all elements except the third. Is it possible to make all elements of the array equal by performing a finite number of such operations? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 200$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($3 \le n \le 50$) — the number of integers. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 100$). -----Output----- For each test case, if it is possible to make all elements equal after some number of operations, output ${YES}$. Otherwise, output ${NO}$. You can output ${YES}$ and ${NO}$ in any case (for example, strings ${yEs}$, ${yes}$, ${Yes}$ will be recognized as a positive response). -----Examples----- Input 4 3 42 42 42 5 1 2 3 4 5 4 4 3 2 1 3 24 2 22 Output YES YES NO NO -----Note----- In the first test case, all elements are already equal. In the second test case, you can choose all elements except the third, their average is $\frac{1 + 2 + 4 + 5}{4} = 3$, so the array will become $[3, 3, 3, 3, 3]$. It's possible to show that it's impossible to make all elements equal in the third and fourth test cases.
{"inputs": ["4\n3\n42 42 42\n5\n1 2 3 4 5\n4\n4 3 2 1\n3\n24 2 22\n"], "outputs": ["YES\nYES\nNO\nNO\n"]}
551
63
coding
Solve the programming task below in a Python markdown code block. Every natural number, ```n```, may have a prime factorization like: We define the arithmetic derivative of ```n```, ```n'``` the value of the expression: Let's do the calculation for ```n = 231525```. ``` 231525 = 3³5²7³ n' = (3*3²)*5²7³ + 3³*(2*5)*7³ + 3³*5²*(3*7²) = 231525 + 92610 + 99225 = 423360 ``` We may make a chain of arithmetic derivatives, starting from a number we apply to the result the transformation and so on until we get the result ```1```. ``` n ---> n' ---> (n')' ---> ((n')')' ---> ..... ---> ((...(n')...)')' ``` Let's do it starting with our number, ```n = 231525``` and making a chain of 5 terms: ``` 231525 ---> 423360 ---> 1899072 ---> 7879680 ---> 51895296 ``` We need a function ```chain_arith_deriv()``` that receives two arguments: ```start``` and ```k```, amount of terms of the chain. The function should output the chain in an array format: ``` chain_arith_deriv(start, k) ---> [start, term1, term2, .....term(k-1)] # a total of k-terms ``` For the case presented above: ```python chain_arith_deriv(231525, 5) == [231525, 423360, 1899072, 7879680, 51895296] ``` The function has to reject prime numbers giving an alerting message ```python chain_arith_deriv(997, 5) == "997 is a prime number" ``` Features of the tests: ``` Number of Random Tests: 50 1000 ≤ start ≤ 99000000 3 ≤ k ≤ 15 ``` Enjoy it and do your best! Also feel free to reuse/extend the following starter code: ```python def chain_arith_deriv(n, k): ```
{"functional": "_inputs = [[231525, 5], [997, 5]]\n_outputs = [[[231525, 423360, 1899072, 7879680, 51895296]], ['997 is a prime number']]\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(chain_arith_deriv(*i), o[0])"}
556
222
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Daanish as always is busy creating and solving his favourite and interesting graph problems. Chef assigns each problem a difficulty — an integer between $1$ and $10$. For each valid $i$, there are $A_{i}$ problems with difficulty $i$. A participant hacked Daanish's account and got access to the problem proposal document. He can delete up to $K$ problems from the document in order to reduce the difficulty of the contest for himself and his friends. Find the smallest possible value of the difficulty of the most difficult problem which remains after removing $K$ problems. ------ 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 $10$ space-separated integers $A_{1}, A_{2}, \ldots, A_{10}$. The second line contains a single integer $K$. ------ Output ------ For each test case, print a single line containing one integer — the minimum difficulty of the most difficult remaining problem. ------ Constraints ------ $1 ≤ T ≤ 2 \cdot 10^{4}$ $0 ≤ A_{i} ≤ 10^{8}$ for each valid $i$ $A_{1} + A_{2} + \ldots + A_{10} > 0$ $0 ≤ K < A_{1} + A_{2} + \ldots + A_{10}$ ------ Subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 5 1 10 1 5 3 2 4 9 0 100 100 2 0 1 0 0 0 0 0 0 10 12 2 0 1 0 0 0 0 10 0 0 0 2 0 1 0 0 0 0 0 0 10 10 1 10 1 5 3 2 4 9 0 100 125 ----- Sample Output 1 ------ 8 1 8 3 2 ----- explanation 1 ------ Example case 1: The participant can remove all $100$ problems with difficulty $10$. Then, there are no problems with difficulties $9$ and $10$, so the maximum difficulty among remaining problems is $8$. Example case 2: The participant can remove all problems with difficulties $3$ and $10$ and any one of the problems with difficulty $1$. Then, the only problem left has difficulty $1$. Example case 3: The participant cannot remove any problem. The document does not contain any problems with difficulties $9$ or $10$, so the maximum difficulty of a problem is $8$.
{"inputs": ["5\n1 10 1 5 3 2 4 9 0 100\n100\n2 0 1 0 0 0 0 0 0 10\n12\n2 0 1 0 0 0 0 10 0 0\n0\n2 0 1 0 0 0 0 0 0 10\n10\n1 10 1 5 3 2 4 9 0 100\n125"], "outputs": ["8\n1\n8\n3\n2"]}
673
145
coding
Solve the programming task below in a Python markdown code block. There are $n$ distinct points on a coordinate line, the coordinate of $i$-th point equals to $x_i$. Choose a subset of the given set of points such that the distance between each pair of points in a subset is an integral power of two. It is necessary to consider each pair of points, not only adjacent. Note that any subset containing one element satisfies the condition above. Among all these subsets, choose a subset with maximum possible size. In other words, you have to choose the maximum possible number of points $x_{i_1}, x_{i_2}, \dots, x_{i_m}$ such that for each pair $x_{i_j}$, $x_{i_k}$ it is true that $|x_{i_j} - x_{i_k}| = 2^d$ where $d$ is some non-negative integer number (not necessarily the same for each pair of points). -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of points. The second line contains $n$ pairwise distinct integers $x_1, x_2, \dots, x_n$ ($-10^9 \le x_i \le 10^9$) — the coordinates of points. -----Output----- In the first line print $m$ — the maximum possible number of points in a subset that satisfies the conditions described above. In the second line print $m$ integers — the coordinates of points in the subset you have chosen. If there are multiple answers, print any of them. -----Examples----- Input 6 3 5 4 7 10 12 Output 3 7 3 5 Input 5 -1 2 5 8 11 Output 1 8 -----Note----- In the first example the answer is $[7, 3, 5]$. Note, that $|7-3|=4=2^2$, $|7-5|=2=2^1$ and $|3-5|=2=2^1$. You can't find a subset having more points satisfying the required property.
{"inputs": ["1\n1\n", "1\n1\n", "1\n2\n", "1\n4\n", "1\n8\n", "1\n7\n", "1\n0\n", "1\n5\n"], "outputs": ["1\n1 \n", "1\n1\n", "1\n2 \n", "1\n4 \n", "1\n8 \n", "1\n7 \n", "1\n0 \n", "1\n5 \n"]}
478
109
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a string s, return the number of different non-empty palindromic subsequences in s. Since the answer may be very large, return it modulo 109 + 7. A subsequence of a string is obtained by deleting zero or more characters from the string. A sequence is palindromic if it is equal to the sequence reversed. Two sequences a1, a2, ... and b1, b2, ... are different if there is some i for which ai != bi.   Please complete the following python code precisely: ```python class Solution: def countPalindromicSubsequences(self, s: str) -> int: ```
{"functional": "def check(candidate):\n assert candidate(s = 'bccb') == 6\n assert candidate(s = 'abcdabcdabcdabcdabcdabcdabcdabcddcbadcbadcbadcbadcbadcbadcbadcba') == 104860361\n\n\ncheck(Solution().countPalindromicSubsequences)"}
156
80
coding
Solve the programming task below in a Python markdown code block. An input string S of length N is transferred through the network using a special protocol. The protocol can send the string through a series of operations. In one operation, we can choose a lowercase english alphabet C and do one of the following: Transfer 1 copy of C through the network. Transfer 2 copies of C through the network. Each of the above transfers take 1 unit of time. Find the minimum time in which we can transfer the entire string S through the network. ------ Input Format ------ - The first line will contain T - the number of test cases. Then the test cases follow. - First line of each test case contains N - the length of the string S. - Second line of each test case contains the string S. ------ Output Format ------ For each test case, output in a single line, the minimum time required to transfer the string. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 10^{5}$ - Sum of $N$ over all test cases does not exceed $10^{5}$. - String $S$ contains lowercase english alphabets only. ----- Sample Input 1 ------ 2 5 cbcdc 6 aabeee ----- Sample Output 1 ------ 5 4 ----- explanation 1 ------ Test Case $1$: String can be transferred through following operations: send $1$ copy of character $\texttt{c}$, send $1$ copy of character $\texttt{b}$, send $1$ copy of character $\texttt{c}$, send $1$ copy of character $\texttt{d}$, and send $1$ copy of character $\texttt{c}$. Thus, total $5$ operations are required. Test Case $2$: String can be transferred through following operations: send $2$ copies of character $\texttt{a}$, send $1$ copy of character $\texttt{b}$, send $1$ copy of character $\texttt{e}$, and send $2$ copies of character $\texttt{e}$. Thus, total $4$ operations are required.
{"inputs": ["2\n5\ncbcdc\n6\naabeee\n"], "outputs": ["5\n4\n"]}
462
27
coding
Solve the programming task below in a Python markdown code block. You are given a tree rooted at node $1$ with $N$ vertices. The $i$$th$ vertex initially has value $A_i (1 \leq i \leq N)$. You are also given $Q$ queries. In each query you are given a vertex $V$. Let $S = \{ S_1 , S_2 , ... S_x \} $ denote the set of vertices such that $S_i$ is in the subtree of $V$, distance between $S_i$ and $V$ is even and $S_i \neq V$ for all $i$. For all $S_i$ , add $A$$S_i$ to $A_V$ and change the value of $A$$S_i$ to zero. Find the values of all the vertices after all queries are performed. Note-The distance between two vertices is defined as the number of edges traversed on the shortest path from one vertex to the other. -----Input:----- - The first line contains an integer $T$ denoting the number of test cases. - The first line of each test case contain two integers $N$ and $Q$. - The second line contains $N$ space separated integers, $A_1, A_2, ..., A_n$ denoting the initial values of the vertices. - The next $N-1$ lines contain two integers $u$ and $v$ denoting an edge between $u$and $v$. - The next $Q$ lines contain a single integer which is the query. -----Output:----- - Print a single line containing $N$ integers for each test case which is the final values of the vertices. -----Constraints:----- - $1\leq T \leq 10$ - $1 \leq N \leq 200000$ - $1 \leq Q \leq 200000$ - $0 \leq A_i \leq 10^9$ - The sum of $N$ over all test cases does not exceed $200000$. - The sum of $Q$ over all test cases does not exceed $200000$. -----Sample Input----- 1 4 3 6 2 7 3 1 2 2 3 3 4 3 2 1 -----Sample Output----- 13 5 0 0 -----Explanation----- Node $3$ has no child in its subtree which is at an even distance so there is no change in the values. Values of nodes after $1st$ query: $6, 2, 7, 3$. Node $4$ is at an even distance in the subtree of node $2$ so $A_4$ gets added to $A_2$ and $A_4$ becomes 0. Values of nodes after $2nd$ query: $6, 5, 7, 0$. Node $3$ is at an even distance in the subtree of node $1$ so $A_3$ gets added to $A_1$ and $A_3$ becomes 0. Values of nodes after $3rd$ query: $13, 5, 0, 0$.
{"inputs": ["1\n4 3\n6 2 7 3\n1 2\n2 3\n3 4\n3\n2\n1"], "outputs": ["13 5 0 0"]}
713
49
coding
Solve the programming task below in a Python markdown code block. We have a string S consisting of lowercase English letters. If the length of S is at most K, print S without change. If the length of S exceeds K, extract the first K characters in S, append ... to the end of them, and print the result. -----Constraints----- - K is an integer between 1 and 100 (inclusive). - S is a string consisting of lowercase English letters. - The length of S is between 1 and 100 (inclusive). -----Input----- Input is given from Standard Input in the following format: K S -----Output----- Print a string as stated in Problem Statement. -----Sample Input----- 7 nikoandsolstice -----Sample Output----- nikoand... nikoandsolstice has a length of 15, which exceeds K=7. We should extract the first 7 characters in this string, append ... to the end of them, and print the result nikoand....
{"inputs": ["1\nz\n", "2\nz\n", "1\nfa\n", "7\necitslosdnaokin", "7\nnikoandrolstice", "7\noikoandrolstice", "7\noikoancrolstice", "7\noikobncsoletics"], "outputs": ["z\n", "z\n", "f...\n", "ecitslo...\n", "nikoand...\n", "oikoand...\n", "oikoanc...\n", "oikobnc...\n"]}
212
119
coding
Solve the programming task below in a Python markdown code block. Read problems statements [Mandarin] , [Bengali] , [Hindi] , [Russian] and [Vietnamese] as well. You are given a grid with $N$ rows and $M$ columns; each cell of this grid is either empty or contains an obstacle. Initially, all cells are colorless. You may perform the following operation an arbitrary number of times (including zero): choose a $2\times 2$ square in the grid and color all four of its cells. Each cell may be colored any number of times. You want all empty cells to be colored and all cells containing obstacles to be colorless. Find out if this state can be achieved. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. The following $N$ lines describe the grid. Each of these lines contains a string with length $M$ containing only characters '.' (denoting an empty cell) and '#' (denoting a cell with an obstacle). ------ Output ------ For each test case, print a single line containing the string "YES" if it possible to color the grid in the required way or "NO" if it is impossible (without quotes). ------ Constraints ------ $1 ≤ T ≤ 10,000$ $2 ≤ N, M ≤ 10^{3}$ the sum of $N\cdot M$ over all test cases does not exceed $5,000,000$ ------ Subtasks ------ Subtask #1 (30 points): only the first row of the grid may contain obstacles Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 2 3 3 ..# ... ... 3 3 ... .#. ... ----- Sample Output 1 ------ YES NO
{"inputs": ["2\n3 3\n..#\n...\n...\n3 3\n...\n.#.\n..."], "outputs": ["YES\nNO"]}
429
36
coding
Solve the programming task below in a Python markdown code block. Gray code is a form of binary encoding where transitions between consecutive numbers differ by only one bit. This is a useful encoding for reducing hardware data hazards with values that change rapidly and/or connect to slower hardware as inputs. It is also useful for generating inputs for Karnaugh maps. Here is an exemple of what the code look like: ``` 0: 0000 1: 0001 2: 0011 3: 0010 4: 0110 5: 0111 6: 0101 7: 0100 8: 1100 ``` The goal of this kata is to build two function bin2gray and gray2bin wich will convert natural binary to Gray Code and vice-versa. We will use the "binary reflected Gray code". The input and output will be arrays of 0 and 1, MSB at index 0. There are "simple" formula to implement these functions. It is a very interesting exercise to find them by yourself. All input will be correct binary arrays. Also feel free to reuse/extend the following starter code: ```python def bin2gray(bits): ```
{"functional": "_inputs = [[[1, 0, 1]], [[1, 1]], [[1]], [[0]], [[1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0]], [[1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0]]]\n_outputs = [[[1, 1, 1]], [[1, 0]], [[1]], [[0]], [[1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1]], [[1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(bin2gray(*i), o[0])"}
279
611
coding
Solve the programming task below in a Python markdown code block. A tree is a graph with n vertices and exactly n - 1 edges; this graph should meet the following condition: there exists exactly one shortest (by number of edges) path between any pair of its vertices. A subtree of a tree T is a tree with both vertices and edges as subsets of vertices and edges of T. You're given a tree with n vertices. Consider its vertices numbered with integers from 1 to n. Additionally an integer is written on every vertex of this tree. Initially the integer written on the i-th vertex is equal to v_{i}. In one move you can apply the following operation: Select the subtree of the given tree that includes the vertex with number 1. Increase (or decrease) by one all the integers which are written on the vertices of that subtree. Calculate the minimum number of moves that is required to make all the integers written on the vertices of the given tree equal to zero. -----Input----- The first line of the input contains n (1 ≤ n ≤ 10^5). Each of the next n - 1 lines contains two integers a_{i} and b_{i} (1 ≤ a_{i}, b_{i} ≤ n; a_{i} ≠ b_{i}) indicating there's an edge between vertices a_{i} and b_{i}. It's guaranteed that the input graph is a tree. The last line of the input contains a list of n space-separated integers v_1, v_2, ..., v_{n} (|v_{i}| ≤ 10^9). -----Output----- Print the minimum number of operations needed to solve the task. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 3 1 2 1 3 1 -1 1 Output 3
{"inputs": ["3\n1 2\n1 3\n2 0 1\n", "3\n1 2\n1 3\n1 -1 1\n", "3\n1 2\n1 3\n1 -1 2\n", "3\n1 2\n1 3\n2 -1 1\n", "3\n1 2\n1 3\n1 -1 3\n", "3\n1 2\n1 3\n2 -1 3\n", "3\n1 2\n1 3\n1 -1 1\n", "5\n2 3\n4 5\n2 5\n1 3\n0 2 1 4 3\n"], "outputs": ["2\n", "3\n", "3\n", "4\n", "5\n", "4\n", "3\n", "8\n"]}
417
194
coding
Solve the programming task below in a Python markdown code block. Write a program to take a character (C) as input and check whether the given character is a vowel or a consonant. NOTE:- Vowels are 'A', 'E', 'I', 'O', 'U'. Rest all alphabets are called consonants. ------ Input Format ------ - First line will contain the character C. ------ Output Format ------ Print "Vowel" if the given character is a vowel, otherwise print "Consonant". ------ Constraints ------ $C$ $will$ $be$ $an$ $upper$ $case$ $English$ $alphabet$ ----- Sample Input 1 ------ Z ----- Sample Output 1 ------ Consonant
{"inputs": ["Z\n"], "outputs": ["Consonant"]}
157
15
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given the head of a singly linked list, return true if it is a palindrome or false otherwise.   Please complete the following python code precisely: ```python # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def isPalindrome(self, head: Optional[ListNode]) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(head = list_node([1,2,2,1])) == True\n assert candidate(head = list_node([1,2])) == False\n\n\ncheck(Solution().isPalindrome)"}
109
53
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You have k servers numbered from 0 to k-1 that are being used to handle multiple requests simultaneously. Each server has infinite computational capacity but cannot handle more than one request at a time. The requests are assigned to servers according to a specific algorithm: The ith (0-indexed) request arrives. If all servers are busy, the request is dropped (not handled at all). If the (i % k)th server is available, assign the request to that server. Otherwise, assign the request to the next available server (wrapping around the list of servers and starting from 0 if necessary). For example, if the ith server is busy, try to assign the request to the (i+1)th server, then the (i+2)th server, and so on. You are given a strictly increasing array arrival of positive integers, where arrival[i] represents the arrival time of the ith request, and another array load, where load[i] represents the load of the ith request (the time it takes to complete). Your goal is to find the busiest server(s). A server is considered busiest if it handled the most number of requests successfully among all the servers. Return a list containing the IDs (0-indexed) of the busiest server(s). You may return the IDs in any order.   Please complete the following python code precisely: ```python class Solution: def busiestServers(self, k: int, arrival: List[int], load: List[int]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(k = 3, arrival = [1,2,3,4,5], load = [5,2,3,3,3] ) == [1] \n assert candidate(k = 3, arrival = [1,2,3,4], load = [1,2,1,2]) == [0]\n assert candidate(k = 3, arrival = [1,2,3], load = [10,12,11]) == [0,1,2]\n assert candidate(k = 3, arrival = [1,2,3,4,8,9,10], load = [5,2,10,3,1,2,2]) == [1]\n assert candidate(k = 1, arrival = [1], load = [1]) == [0]\n\n\ncheck(Solution().busiestServers)"}
323
206
coding
Solve the programming task below in a Python markdown code block. Mike wants to prepare for IMO but he doesn't know geometry, so his teacher gave him an interesting geometry problem. Let's define f([l, r]) = r - l + 1 to be the number of integer points in the segment [l, r] with l ≤ r (say that $f(\varnothing) = 0$). You are given two integers n and k and n closed intervals [l_{i}, r_{i}] on OX axis and you have to find: $\sum_{1 \leq i_{1} < i_{2} < \ldots < i_{k} \leq n} f([ l_{i_{1}}, r_{i_{1}} ] \cap [ l_{i_{2}}, r_{i_{2}} ] \cap \ldots \cap [ l_{i_{k}}, r_{i_{k}} ])$ In other words, you should find the sum of the number of integer points in the intersection of any k of the segments. As the answer may be very large, output it modulo 1000000007 (10^9 + 7). Mike can't solve this problem so he needs your help. You will help him, won't you? -----Input----- The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of segments and the number of segments in intersection groups respectively. Then n lines follow, the i-th line contains two integers l_{i}, r_{i} ( - 10^9 ≤ l_{i} ≤ r_{i} ≤ 10^9), describing i-th segment bounds. -----Output----- Print one integer number — the answer to Mike's problem modulo 1000000007 (10^9 + 7) in the only line. -----Examples----- Input 3 2 1 2 1 3 2 3 Output 5 Input 3 3 1 3 1 3 1 3 Output 3 Input 3 1 1 2 2 3 3 4 Output 6 -----Note----- In the first example: $f([ 1,2 ] \cap [ 1,3 ]) = f([ 1,2 ]) = 2$; $f([ 1,2 ] \cap [ 2,3 ]) = f([ 2,2 ]) = 1$; $f([ 1,3 ] \cap [ 2,3 ]) = f([ 2,3 ]) = 2$. So the answer is 2 + 1 + 2 = 5.
{"inputs": ["1 1\n45 70\n", "1 1\n45 70\n", "1 1\n-35 -8\n", "1 1\n-35 -8\n", "1 1\n45 113\n", "1 1\n-40 -8\n", "1 1\n-79 -51\n", "1 1\n-79 -51\n"], "outputs": ["26\n", " 26\n", "28\n", " 28\n", "69\n", "33\n", "29\n", " 29\n"]}
592
151
coding
Solve the programming task below in a Python markdown code block. The MEX of a set of integers is defined as the smallest non-negative integer that does not belong to this set. For example, $\mathrm{MEX}(\{0,2,3\}) = 1$ and $\mathrm{MEX}(\{1,3\}) = 0$. Chef has a tree with $N$ nodes (numbered $1$ through $N$). The tree is rooted at node $1$. Chef wants to assign a non-negative integer to each node in such a way that each integer between $0$ and $N-1$ (inclusive) is assigned to exactly one node. For each node $u$, consider the integers assigned to the nodes in the subtree of $u$ (including $u$); let $a_u$ denote the MEX of these integers. Chef wants $a_1 + a_2 + \ldots + a_N$ to be as large as possible. Find the maximum possible value of this sum. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains a single integer $N$. - The second line contains $N-1$ space-separated integers $p_1, p_2, \ldots, p_{N-1}$. For each valid $i$, the node $p_i$ is the parent of the node $i+1$. -----Output----- For each test case, print a single line containing one integer ― the maximum sum of subtree MEX-s which can be obtained if you assign the weights optimally. -----Constraints----- - $1 \le T \le 5$ - $2 \le N \le 10^5$ - $1 \le p_i < i$ for each valid $i$ -----Subtasks----- Subtask #1 (100 points): original constraints -----Example Input----- 2 3 1 1 5 1 1 2 2 -----Example Output----- 4 9
{"inputs": ["2\n3\n1 1\n5\n1 1 2 2\n"], "outputs": ["4\n9"]}
452
31
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given the root of a binary tree, return the sum of every tree node's tilt. The tilt of a tree node is the absolute difference between the sum of all left subtree node values and all right subtree node values. If a node does not have a left child, then the sum of the left subtree node values is treated as 0. The rule is similar if the node does not have a right child.   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 findTilt(self, root: Optional[TreeNode]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(root = tree_node([1,2,3])) == 1\n assert candidate(root = tree_node([4,2,9,3,5,None,7])) == 15\n assert candidate(root = tree_node([21,7,14,1,1,2,2,3,3])) == 9\n\n\ncheck(Solution().findTilt)"}
184
96
coding
Solve the programming task below in a Python markdown code block. Check Tutorial tab to know how to to solve. Here is a sample line of code that can be executed in Python: print("Hello, World!") You can just as easily store a string as a variable and then print it to stdout: my_string = "Hello, World!" print(my_string) The above code will print Hello, World! on your screen. Try it yourself in the editor below! Input Format You do not need to read any input in this challenge. Output Format Print Hello, World! to stdout. Sample Output 0 Hello, World!
{"inputs": [""], "outputs": ["Hello World!\n"]}
132
15
coding
Solve the programming task below in a Python markdown code block. You are given an array of integers [A1,A2,…,AN]$[A_1, A_2, \ldots, A_N]$. Let's call adding an element to this array at any position (including the beginning and the end) or removing an arbitrary element from it a modification. It is not allowed to remove an element from the array if it is empty. Find the minimum number of modifications which must be performed so that the resulting array can be partitioned into permutations. Formally, it must be possible to partition elements of the resulting array into zero or more groups (multisets; not necessarily identical) in such a way that each element belongs to exactly one group and for each group, if it contains L$L$ elements, then it must contain only integers 1$1$ through L$L$, each of them exactly once. -----Input----- - The first line of the input contains a single integer T$T$ denoting the number of test cases. The description of T$T$ test cases follows. - The first line of each test case contains a single integer N$N$. - The second line contains N$N$ space-separated integers A1,A2,…,AN$A_1, A_2, \ldots, A_N$. -----Output----- For each test case, print a single line containing one integer ― the minimum required number of modifications. -----Constraints----- - 1≤T≤1,000$1 \le T \le 1,000$ - 1≤N≤106$1 \le N \le 10^6$ - 1≤Ai≤109$1 \le A_i \le 10^9$ for each valid i$i$ - the sum of N$N$ over all test cases does not exceed 106$10^6$ -----Subtasks----- Subtask #1 (50 points): - 1≤N≤1,000$1 \le N \le 1,000$ - the sum of N$N$ over all test cases does not exceed 10,000$10,000$ Subtask #2 (50 points): original constraints -----Example Input----- 2 5 1 4 1 2 2 4 2 3 2 3 -----Example Output----- 1 2
{"inputs": ["2\n5\n1 4 1 2 2\n4\n2 3 2 3"], "outputs": ["1\n2"]}
520
36
coding
Solve the programming task below in a Python markdown code block. You're given an ancient book that unfortunately has a few pages in the wrong position, fortunately your computer has a list of every page number in order from ``1`` to ``n``. You're supplied with an array of numbers, and should return an array with each page number that is out of place. Incorrect page numbers will not appear next to each other. Duplicate incorrect page numbers are possible. Example: ```Given: list = [1,2,10,3,4,5,8,6,7] ``` ``Return: [10,8] `` Your returning list should have the incorrect page numbers in the order they were found. Also feel free to reuse/extend the following starter code: ```python def find_page_number(pages): ```
{"functional": "_inputs = [[[1, 2, 10, 3, 4, 5, 8, 6, 7]], [[1, 2, 3, 4, 50, 5, 6, 7, 51, 8, 40, 9]], [[1, 2, 3000, 3, 4, 5, 8, 6, 7, 8, 100, 9, 40, 10, 11, 13]], [[1, 2, 3, 4, 50, 5, 6, 7, 51, 8, 9]], [[4, 1, 2, 3, 3, 4, 26, 5, 6, 2, 7]]]\n_outputs = [[[10, 8]], [[50, 51, 40]], [[3000, 8, 100, 40, 13]], [[50, 51]], [[4, 3, 26, 2]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(find_page_number(*i), o[0])"}
171
404
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 where the largest integer is unique. Determine whether the largest element in the array is at least twice as much as every other number in the array. If it is, return the index of the largest element, or return -1 otherwise.   Please complete the following python code precisely: ```python class Solution: def dominantIndex(self, nums: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [3,6,1,0]) == 1\n assert candidate(nums = [1,2,3,4]) == -1\n\n\ncheck(Solution().dominantIndex)"}
103
56
coding
Solve the programming task below in a Python markdown code block. There are some candies that need to be distributed to some children as fairly as possible (i.e. the variance of result needs to be as small as possible), but I don't know how to distribute them, so I need your help. Your assignment is to write a function with signature `distribute(m, n)` in which `m` represents how many candies there are, while `n` represents how many children there are. The function should return a container which includes the number of candies each child gains. # Notice 1. *The candy can't be divided into pieces.* 2. The list's order doesn't matter. # Requirements 1. The case `m < 0` is equivalent to `m == 0`. 2. If `n <= 0` the function should return an empty container. 3. If there isn't enough candy to distribute, you should fill the corresponding number with `0`. # Examples ```python distribute(-5, 0) # should be [] distribute( 0, 0) # should be [] distribute( 5, 0) # should be [] distribute(10, 0) # should be [] distribute(15, 0) # should be [] distribute(-5, -5) # should be [] distribute( 0, -5) # should be [] distribute( 5, -5) # should be [] distribute(10, -5) # should be [] distribute(15, -5) # should be [] distribute(-5, 10) # should be [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] distribute( 0, 10) # should be [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] distribute( 5, 10) # should be [1, 1, 1, 1, 1, 0, 0, 0, 0, 0] distribute(10, 10) # should be [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] distribute(15, 10) # should be [2, 2, 2, 2, 2, 1, 1, 1, 1, 1] ``` # Input 1. m: Integer (m <= 100000) 2. n: Integer (n <= 1000) # Output 1. [Integer] Also feel free to reuse/extend the following starter code: ```python def distribute(m, n): ```
{"functional": "_inputs = [[-5, 0], [10, 0], [15, 0], [-5, -5], [10, -5], [15, -5]]\n_outputs = [[[]], [[]], [[]], [[]], [[]], [[]]]\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(distribute(*i), o[0])"}
636
202
coding
Solve the programming task below in a Python markdown code block. There are $n$ people who want to participate in a boat competition. The weight of the $i$-th participant is $w_i$. Only teams consisting of two people can participate in this competition. As an organizer, you think that it's fair to allow only teams with the same total weight. So, if there are $k$ teams $(a_1, b_1)$, $(a_2, b_2)$, $\dots$, $(a_k, b_k)$, where $a_i$ is the weight of the first participant of the $i$-th team and $b_i$ is the weight of the second participant of the $i$-th team, then the condition $a_1 + b_1 = a_2 + b_2 = \dots = a_k + b_k = s$, where $s$ is the total weight of each team, should be satisfied. Your task is to choose such $s$ that the number of teams people can create is the maximum possible. Note that each participant can be in no more than one team. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Then $t$ test cases follow. The first line of the test case contains one integer $n$ ($1 \le n \le 50$) — the number of participants. The second line of the test case contains $n$ integers $w_1, w_2, \dots, w_n$ ($1 \le w_i \le n$), where $w_i$ is the weight of the $i$-th participant. -----Output----- For each test case, print one integer $k$: the maximum number of teams people can compose with the total weight $s$, if you choose $s$ optimally. -----Example----- Input 5 5 1 2 3 4 5 8 6 6 6 6 6 6 8 8 8 1 2 2 1 2 1 1 2 3 1 3 3 6 1 1 3 4 2 2 Output 2 3 4 1 2 -----Note----- In the first test case of the example, we can reach the optimal answer for $s=6$. Then the first boat is used by participants $1$ and $5$ and the second boat is used by participants $2$ and $4$ (indices are the same as weights). In the second test case of the example, we can reach the optimal answer for $s=12$. Then first $6$ participants can form $3$ pairs. In the third test case of the example, we can reach the optimal answer for $s=3$. The answer is $4$ because we have $4$ participants with weight $1$ and $4$ participants with weight $2$. In the fourth test case of the example, we can reach the optimal answer for $s=4$ or $s=6$. In the fifth test case of the example, we can reach the optimal answer for $s=3$. Note that participant with weight $3$ can't use the boat because there is no suitable pair for him in the list.
{"inputs": ["5\n5\n1 2 3 4 5\n8\n6 6 6 6 6 6 8 8\n8\n1 2 2 1 2 1 1 2\n3\n1 3 3\n6\n1 1 3 4 2 2\n", "5\n5\n1 2 3 4 5\n8\n6 6 6 6 6 6 8 8\n8\n1 2 2 1 2 1 1 2\n3\n1 3 3\n6\n1 1 3 4 2 2\n", "4\n14\n2 6 5 9 5 8 13 14 8 5 6 14 5 2\n36\n15 22 27 7 23 36 1 17 33 21 18 7 3 4 32 24 8 19 36 22 17 2 24 10 26 4 30 6 2 6 11 19 18 1 2 20\n2\n2 2\n12\n4 9 10 6 1 9 5 8 12 9 9 12\n", "4\n14\n2 6 5 9 5 8 13 14 8 5 6 14 5 2\n36\n15 22 27 7 23 36 1 17 2 21 18 7 3 4 32 24 8 19 36 22 17 11 24 10 26 4 30 6 2 6 11 19 18 1 2 20\n2\n2 2\n12\n4 9 10 6 1 9 2 8 12 9 9 12\n", "4\n14\n2 6 5 9 5 8 13 14 8 5 6 14 5 2\n36\n15 22 27 7 23 36 1 17 33 21 18 7 3 4 32 24 8 19 36 22 17 11 24 10 26 4 30 6 2 6 11 19 18 1 2 20\n2\n2 2\n12\n4 9 10 6 1 9 5 8 12 9 9 12\n", "4\n14\n2 6 5 9 5 8 13 14 8 5 6 14 5 2\n36\n15 22 27 7 23 36 1 17 33 21 18 7 3 4 32 24 8 19 36 22 17 11 24 10 26 4 30 6 2 6 11 19 18 1 2 20\n2\n2 2\n12\n4 9 10 6 1 9 2 8 12 9 9 12\n", "4\n14\n2 6 5 9 5 8 13 14 8 5 6 14 5 2\n36\n15 22 27 7 23 36 1 17 33 21 18 7 3 4 32 24 8 11 36 22 17 11 24 10 26 4 30 6 2 6 11 19 18 1 2 20\n2\n2 2\n12\n4 9 10 6 1 9 2 8 12 9 9 12\n", "4\n14\n2 6 5 9 5 8 13 14 8 5 6 14 5 2\n36\n15 22 27 7 23 36 1 17 33 21 18 10 3 4 32 24 8 11 36 22 17 11 24 10 26 4 30 6 2 6 11 19 18 1 2 20\n2\n2 2\n12\n4 9 10 6 1 9 2 8 12 9 9 12\n"], "outputs": ["2\n3\n4\n1\n2\n", "2\n3\n4\n1\n2\n", "4\n11\n1\n4\n", "4\n11\n1\n4\n", "4\n11\n1\n4\n", "4\n11\n1\n4\n", "4\n11\n1\n4\n", "4\n12\n1\n4\n"]}
727
1,281
coding
Solve the programming task below in a Python markdown code block. -----Input----- The only line of the input is a string of 7 characters. The first character is letter A, followed by 6 digits. The input is guaranteed to be valid (for certain definition of "valid"). -----Output----- Output a single integer. -----Examples----- Input A221033 Output 21 Input A223635 Output 22 Input A232726 Output 23
{"inputs": ["A221033\n", "A223635\n", "A232726\n", "A102210\n", "A231010\n", "A222222\n", "A555555\n", "A102222\n"], "outputs": ["21\n", "22\n", "23\n", "25\n", "26\n", "13\n", "31\n", "19\n"]}
114
126
coding
Solve the programming task below in a Python markdown code block. # Task We know that some numbers can be split into two primes. ie. `5 = 2 + 3, 10 = 3 + 7`. But some numbers are not. ie. `17, 27, 35`, etc.. Given a positive integer `n`. Determine whether it can be split into two primes. If yes, return the maximum product of two primes. If not, return `0` instead. # Input/Output `[input]` integer `n` A positive integer. `0 ≤ n ≤ 100000` `[output]` an integer The possible maximum product of two primes. or return `0` if it's impossible split into two primes. # Example For `n = 1`, the output should be `0`. `1` can not split into two primes For `n = 4`, the output should be `4`. `4` can split into two primes `2 and 2`. `2 x 2 = 4` For `n = 20`, the output should be `91`. `20` can split into two primes `7 and 13` or `3 and 17`. The maximum product is `7 x 13 = 91` Also feel free to reuse/extend the following starter code: ```python def prime_product(n): ```
{"functional": "_inputs = [[1], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [20], [100]]\n_outputs = [[0], [0], [4], [6], [9], [10], [15], [14], [25], [0], [35], [91], [2491]]\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(prime_product(*i), o[0])"}
311
242
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. For an integer array nums, an inverse pair is a pair of integers [i, j] where 0 <= i < j < nums.length and nums[i] > nums[j]. Given two integers n and k, return the number of different arrays consisting of numbers from 1 to n such that there are exactly k inverse pairs. Since the answer can be huge, return it modulo 109 + 7.   Please complete the following python code precisely: ```python class Solution: def kInversePairs(self, n: int, k: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(n = 3, k = 0) == 1\n assert candidate(n = 3, k = 1) == 2\n\n\ncheck(Solution().kInversePairs)"}
137
54
coding
Solve the programming task below in a Python markdown code block. Notes Template in C Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 1000 * 1 ≤ timei ≤ 50000 * 1 ≤ length of namei ≤ 10 * 1 ≤ Sum of timei ≤ 1000000 Input n q name1 time1 name2 time2 ... namen timen In the first line the number of processes n and the quantum q are given separated by a single space. In the following n lines, names and times for the n processes are given. namei and timei are separated by a single space. Output For each process, prints its name and the time the process finished in order. Example Input 5 100 p1 150 p2 80 p3 200 p4 350 p5 20 Output p2 180 p5 400 p1 450 p3 550 p4 800
{"inputs": ["5 100\np1 54\n2p 80\n3p 200\np4 350\np5 14", "5 100\np1 54\n2p 80\n3p 200\n4p 350\np5 14", "4 100\np1 150\np2 80\np3 240\n4p 350\np5 8", "2 100\np1 54\n2p 80\n3p 200\n4p 350\np5 14", "4 100\np1 150\no2 80\np3 240\n4p 350\np5 8", "4 101\np1 150\no2 80\np3 240\n4p 350\np4 8", "4 101\n1p 150\no2 80\np3 240\n4p 350\np4 8", "4 101\n1p 150\no2 80\np3 240\np4 350\np4 8"], "outputs": ["p1 54\n2p 134\np5 348\n3p 448\np4 698\n", "p1 54\n2p 134\np5 348\n3p 448\n4p 698\n", "p2 180\np1 430\np3 670\n4p 820\n", "p1 54\n2p 134\n", "o2 180\np1 430\np3 670\n4p 820\n", "o2 181\np1 432\np3 672\n4p 820\n", "o2 181\n1p 432\np3 672\n4p 820\n", "o2 181\n1p 432\np3 672\np4 820\n"]}
259
555
coding
Solve the programming task below in a Python markdown code block. Skyscraper "MinatoHarukas" Mr. Port plans to start a new business renting one or more floors of the new skyscraper with one giga floors, MinatoHarukas. He wants to rent as many vertically adjacent floors as possible, because he wants to show advertisement on as many vertically adjacent windows as possible. The rent for one floor is proportional to the floor number, that is, the rent per month for the n-th floor is n times that of the first floor. Here, the ground floor is called the first floor in the American style, and basement floors are out of consideration for the renting. In order to help Mr. Port, you should write a program that computes the vertically adjacent floors satisfying his requirement and whose total rental cost per month is exactly equal to his budget. For example, when his budget is 15 units, with one unit being the rent of the first floor, there are four possible rent plans, 1+2+3+4+5, 4+5+6, 7+8, and 15. For all of them, the sums are equal to 15. Of course in this example the rent of maximal number of the floors is that of 1+2+3+4+5, that is, the rent from the first floor to the fifth floor. Input The input consists of multiple datasets, each in the following format. > b > A dataset consists of one line, the budget of Mr. Port b as multiples of the rent of the first floor. b is a positive integer satisfying 1 < b < 109. The end of the input is indicated by a line containing a zero. The number of datasets does not exceed 1000. Output For each dataset, output a single line containing two positive integers representing the plan with the maximal number of vertically adjacent floors with its rent price exactly equal to the budget of Mr. Port. The first should be the lowest floor number and the second should be the number of floors. Sample Input 15 16 2 3 9699690 223092870 847288609 900660121 987698769 999999999 0 Output for the Sample Input 1 5 16 1 2 1 1 2 16 4389 129 20995 4112949 206 15006 30011 46887 17718 163837 5994 Example Input 15 16 2 3 9699690 223092870 847288609 900660121 987698769 999999999 0 Output 1 5 16 1 2 1 1 2 16 4389 129 20995 4112949 206 15006 30011 46887 17718 163837 5994
{"inputs": ["15\n6\n2\n3\n2360136\n434020716\n332845831\n6280939\n987698769\n999999999\n0", "15\n16\n2\n3\n8678728\n223092870\n332845831\n6280939\n987698769\n999999999\n0", "15\n16\n2\n3\n8678728\n434020716\n332845831\n6280939\n987698769\n999999999\n0", "15\n6\n2\n3\n9699690\n116276261\n840868300\n986291783\n565625340\n92432207\n0", "15\n16\n2\n3\n3971585\n434020716\n332845831\n6280939\n987698769\n999999999\n0", "15\n16\n2\n3\n2360136\n434020716\n332845831\n6280939\n987698769\n999999999\n0", "15\n16\n2\n2\n9699690\n116276261\n932780910\n56225415\n987698769\n999999999\n0", "15\n16\n3\n2\n9699690\n24678504\n932780910\n900660121\n946496661\n999999999\n0"], "outputs": ["1 5\n1 3\n2 1\n1 2\n1000 1392\n24286 13896\n4059055 82\n1674 2246\n46887 17718\n163837 5994\n", "1 5\n16 1\n2 1\n1 2\n2575 2323\n129 20995\n4059055 82\n1674 2246\n46887 17718\n163837 5994\n", "1 5\n16 1\n2 1\n1 2\n2575 2323\n24286 13896\n4059055 82\n1674 2246\n46887 17718\n163837 5994\n", "1 5\n1 3\n2 1\n1 2\n16 4389\n1236935 94\n4204242 200\n163016 5942\n797 32847\n347357 266\n", "1 5\n16 1\n2 1\n1 2\n6437 590\n24286 13896\n4059055 82\n1674 2246\n46887 17718\n163837 5994\n", "1 5\n16 1\n2 1\n1 2\n1000 1392\n24286 13896\n4059055 82\n1674 2246\n46887 17718\n163837 5994\n", "1 5\n16 1\n2 1\n2 1\n16 4389\n1236935 94\n342 42852\n1874166 30\n46887 17718\n163837 5994\n", "1 5\n16 1\n1 2\n2 1\n16 4389\n171307 144\n342 42852\n15006 30011\n14340826 66\n163837 5994\n"]}
753
1,234
coding
Solve the programming task below in a Python markdown code block. Today an outstanding event is going to happen in the forest — hedgehog Filya will come to his old fried Sonya! Sonya is an owl and she sleeps during the day and stay awake from minute l_1 to minute r_1 inclusive. Also, during the minute k she prinks and is unavailable for Filya. Filya works a lot and he plans to visit Sonya from minute l_2 to minute r_2 inclusive. Calculate the number of minutes they will be able to spend together. -----Input----- The only line of the input contains integers l_1, r_1, l_2, r_2 and k (1 ≤ l_1, r_1, l_2, r_2, k ≤ 10^18, l_1 ≤ r_1, l_2 ≤ r_2), providing the segments of time for Sonya and Filya and the moment of time when Sonya prinks. -----Output----- Print one integer — the number of minutes Sonya and Filya will be able to spend together. -----Examples----- Input 1 10 9 20 1 Output 2 Input 1 100 50 200 75 Output 50 -----Note----- In the first sample, they will be together during minutes 9 and 10. In the second sample, they will be together from minute 50 to minute 74 and from minute 76 to minute 100.
{"inputs": ["6 6 5 8 9\n", "1 1 1 1 1\n", "1 2 3 4 5\n", "1 3 3 5 3\n", "1 2 2 3 5\n", "2 4 3 7 3\n", "2 4 1 2 5\n", "4 6 6 8 9\n"], "outputs": ["1\n", "0\n", "0\n", "0\n", "1\n", "1\n", "1\n", "1\n"]}
338
134