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. Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively. He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$. Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$. Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible. Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal. -----Input----- The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ — the length of the first array. The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ — elements of the array $A$. The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ — the length of the second array. The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$. -----Output----- Print a single integer — the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal. If there is no way to make array equal, print "-1". -----Examples----- Input 5 11 2 3 5 7 4 11 7 3 7 Output 3 Input 2 1 2 1 100 Output -1 Input 3 1 2 3 3 1 2 3 Output 3
{"inputs": ["1\n1\n1\n2\n", "1\n1\n1\n2\n", "1\n2\n1\n2\n", "1\n2\n1\n0\n", "1\n5\n2\n3 2\n", "2\n1 1\n1\n1\n", "1\n5\n2\n5 5\n", "1\n2\n2\n2 3\n"], "outputs": ["-1\n", "-1\n", "1\n", "-1\n", "1\n", "-1\n", "-1\n", "-1\n"]}
573
127
coding
Solve the programming task below in a Python markdown code block. Given two integers $n$ and $\textbf{r}$. In how many ways can $\textbf{r}$ items be chosen from $n$ items? Input Format The first line contains the number of test cases $\mathbf{T}$. Each of the next $\mathbf{T}$ lines contains two integers $n$ and $\textbf{r}$. Constraints $1\leq T\leq10^5$ $1\leq n\leq10^9$ $0\leq r\leq n$ Output Format Output $\mathbf{T}$ lines, containing the required answer for the corresponding test case. Output all answers modulo $142857$. Sample Input 4 2 1 4 0 5 2 10 3 Sample Output 2 1 10 120
{"inputs": ["4\n2 1\n4 0\n5 2\n10 3\n"], "outputs": ["2\n1\n10\n120\n"]}
201
40
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an 8 x 8 matrix representing a chessboard. There is exactly one white rook represented by 'R', some number of white bishops 'B', and some number of black pawns 'p'. Empty squares are represented by '.'. A rook can move any number of squares horizontally or vertically (up, down, left, right) until it reaches another piece or the edge of the board. A rook is attacking a pawn if it can move to the pawn's square in one move. Note: A rook cannot move through other pieces, such as bishops or pawns. This means a rook cannot attack a pawn if there is another piece blocking the path. Return the number of pawns the white rook is attacking.   Please complete the following python code precisely: ```python class Solution: def numRookCaptures(self, board: List[List[str]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate([[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\"p\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\"R\",\".\",\".\",\".\",\"p\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\"p\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"]]) == 3\n assert candidate([[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\"p\",\"p\",\"p\",\"p\",\"p\",\".\",\".\"],[\".\",\"p\",\"p\",\"B\",\"p\",\"p\",\".\",\".\"],[\".\",\"p\",\"B\",\"R\",\"B\",\"p\",\".\",\".\"],[\".\",\"p\",\"p\",\"B\",\"p\",\"p\",\".\",\".\"],[\".\",\"p\",\"p\",\"p\",\"p\",\"p\",\".\",\".\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"]]) == 0\n assert candidate([[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\"p\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\"p\",\".\",\".\",\".\",\".\"],[\"p\",\"p\",\".\",\"R\",\".\",\"p\",\"B\",\".\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\"B\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\"p\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"]]) == 3\n\n\ncheck(Solution().numRookCaptures)"}
206
589
coding
Solve the programming task below in a Python markdown code block. Elon Musk has succesfully built an automated staircase from Earth to Mars. Many people want to go to Mars, but that's not possible due to limited capacity of the staircase and logistics involved. Hence, Elon asks interested candidates to solve a tough challenge. If they solve it, they get a chance to visit Mars, otherwise not. Sam is highly interested in going to Mars. Can you help him solve the challenge? The staircase has N steps. One can either go step by step, or at each step, can take a jump of at most N steps. Elon is interested to know the number of ways in which you can go to Mars. Since the number of steps in stairs can be insanely large, Elon is only interested in the first and the last K digits of number of ways from which he can compute the actual answer with his algorithm. Input Format First line is an integer T that denotes the number of test cases. Next T lines contain 2 integers each, N and K. Output Format T lines. Each line corresponds to one of the test cases and contains the sum of numbers which are formed by first K digit and last K digits of number of ways. Constraints 1<=T<=1000 1<=N<=10^9 1<=K<=9 If S is the number of ways in which one can climb the staircase, then the number of digits in S is greater or equal to the K. Sample Input 2 10 2 12 1 Sample Output 63 10 If there are 10 steps, let's call the number of ways in which one can go is S let S be of the form wxyz. So, summing wx + yz gives 63.
{"inputs": ["2\n10 2 \n12 1\n"], "outputs": ["63\n10\n"]}
383
29
coding
Solve the programming task below in a Python markdown code block. There are N integers written on a blackboard. The i-th integer is A_i. Takahashi will repeatedly perform the following operation on these numbers: * Select a pair of integers, A_i and A_j, that have the same parity (that is, both are even or both are odd) and erase them. * Then, write a new integer on the blackboard that is equal to the sum of those integers, A_i+A_j. Determine whether it is possible to have only one integer on the blackboard. Constraints * 2 ≦ N ≦ 10^5 * 1 ≦ A_i ≦ 10^9 * A_i is an integer. Input The input is given from Standard Input in the following format: N A_1 A_2 … A_N Output If it is possible to have only one integer on the blackboard, print `YES`. Otherwise, print `NO`. Examples Input 3 1 2 3 Output YES Input 5 1 2 3 4 5 Output NO
{"inputs": ["3\n2 2 3", "3\n2 2 6", "3\n2 2 7", "3\n1 2 7", "3\n1 0 7", "3\n1 2 3", "3\n1 0 13", "3\n1 0 19"], "outputs": ["NO\n", "YES\n", "NO\n", "YES\n", "YES\n", "YES", "YES\n", "YES\n"]}
243
111
coding
Solve the programming task below in a Python markdown code block. Pasha has recently bought a new phone jPager and started adding his friends' phone numbers there. Each phone number consists of exactly n digits. Also Pasha has a number k and two sequences of length n / k (n is divisible by k) a_1, a_2, ..., a_{n} / k and b_1, b_2, ..., b_{n} / k. Let's split the phone number into blocks of length k. The first block will be formed by digits from the phone number that are on positions 1, 2,..., k, the second block will be formed by digits from the phone number that are on positions k + 1, k + 2, ..., 2·k and so on. Pasha considers a phone number good, if the i-th block doesn't start from the digit b_{i} and is divisible by a_{i} if represented as an integer. To represent the block of length k as an integer, let's write it out as a sequence c_1, c_2,...,c_{k}. Then the integer is calculated as the result of the expression c_1·10^{k} - 1 + c_2·10^{k} - 2 + ... + c_{k}. Pasha asks you to calculate the number of good phone numbers of length n, for the given k, a_{i} and b_{i}. As this number can be too big, print it modulo 10^9 + 7. -----Input----- The first line of the input contains two integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ min(n, 9)) — the length of all phone numbers and the length of each block, respectively. It is guaranteed that n is divisible by k. The second line of the input contains n / k space-separated positive integers — sequence a_1, a_2, ..., a_{n} / k (1 ≤ a_{i} < 10^{k}). The third line of the input contains n / k space-separated positive integers — sequence b_1, b_2, ..., b_{n} / k (0 ≤ b_{i} ≤ 9). -----Output----- Print a single integer — the number of good phone numbers of length n modulo 10^9 + 7. -----Examples----- Input 6 2 38 56 49 7 3 4 Output 8 Input 8 2 1 22 3 44 5 4 3 2 Output 32400 -----Note----- In the first test sample good phone numbers are: 000000, 000098, 005600, 005698, 380000, 380098, 385600, 385698.
{"inputs": ["1 1\n2\n0\n", "1 1\n2\n0\n", "1 1\n2\n1\n", "4 4\n1122\n2\n", "4 4\n1122\n2\n", "4 4\n1049\n2\n", "4 4\n1384\n2\n", "4 4\n2661\n2\n"], "outputs": ["4\n", "4\n", "5\n", "8\n", "8\n", "9\n", "7\n", "3\n"]}
656
133
coding
Solve the programming task below in a Python markdown code block. # How much is the fish! (- Scooter ) The ocean is full of colorful fishes. We as programmers want to know the hexadecimal value of these fishes. ## Task Take all hexadecimal valid characters (a,b,c,d,e,f) of the given name and XOR them. Return the result as an integer. ## Input The input is always a string, which can contain spaces, upper and lower case letters but no digits. ## Example `fisHex("redlionfish") -> e,d,f -> XOR -> 12` Also feel free to reuse/extend the following starter code: ```python def fisHex(name): ```
{"functional": "_inputs = [['pufferfish'], ['puffers'], ['balloonfish'], ['blowfish'], ['bubblefish'], ['globefish'], ['swellfish'], ['toadfish'], ['toadies'], ['honey toads'], ['sugar toads'], ['sea squab'], [''], ['Aeneus corydoras'], ['African glass catfish'], ['African lungfish'], ['Aholehole'], ['Airbreathing catfish'], ['Airsac catfish'], ['Alaska blackfish'], ['Albacore'], ['Alewife'], ['Alfonsino'], ['Algae eater'], ['Alligatorfish'], ['Asian carps'], ['Asiatic glassfish'], ['Atka mackerel'], ['Atlantic cod'], ['Atlantic herring'], ['Atlantic salmon'], ['Atlantic saury'], ['Atlantic silverside'], ['Australasian salmon'], ['Australian grayling'], ['Australian herrin'], ['Australian lungfish'], ['Australian prowfish'], ['Ayu']]\n_outputs = [[1], [14], [14], [4], [10], [10], [1], [8], [9], [9], [13], [5], [0], [1], [0], [12], [10], [12], [5], [8], [9], [5], [5], [4], [15], [6], [9], [6], [13], [2], [6], [6], [1], [10], [0], [4], [5], [5], [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(fisHex(*i), o[0])"}
145
485
coding
Solve the programming task below in a Python markdown code block. The Kingdom of Takahashi has N towns, numbered 1 through N. There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i. Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there. Help the king by writing a program that answers this question. -----Constraints----- - 2 \leq N \leq 2 \times 10^5 - 1 \leq A_i \leq N - 1 \leq K \leq 10^{18} -----Input----- Input is given from Standard Input in the following format: N K A_1 A_2 \dots A_N -----Output----- Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there. -----Sample Input----- 4 5 3 2 4 1 -----Sample Output----- 4 If we start at Town 1 and use the teleporter 5 times, our travel will be as follows: 1 \to 3 \to 4 \to 1 \to 3 \to 4.
{"inputs": ["4 9\n3 2 4 1", "4 0\n3 2 4 1", "4 0\n3 2 1 1", "4 0\n3 0 4 1", "4 0\n3 1 4 1", "4 5\n3 2 4 1", "4 5\n3 2 4 1\n", "4 16\n3 2 4 1"], "outputs": ["1\n", "1\n", "1\n", "1\n", "1\n", "4", "4\n", "3\n"]}
296
143
coding
Solve the programming task below in a Python markdown code block. You got a job as a marketer in a pet shop, and your current task is to boost sales of cat food. One of the strategies is to sell cans of food in packs with discounts. Suppose you decided to sell packs with $a$ cans in a pack with a discount and some customer wants to buy $x$ cans of cat food. Then he follows a greedy strategy: he buys $\left\lfloor \frac{x}{a} \right\rfloor$ packs with a discount; then he wants to buy the remaining $(x \bmod a)$ cans one by one. $\left\lfloor \frac{x}{a} \right\rfloor$ is $x$ divided by $a$ rounded down, $x \bmod a$ is the remainer of $x$ divided by $a$. But customers are greedy in general, so if the customer wants to buy $(x \bmod a)$ cans one by one and it happens that $(x \bmod a) \ge \frac{a}{2}$ he decides to buy the whole pack of $a$ cans (instead of buying $(x \bmod a)$ cans). It makes you, as a marketer, happy since the customer bought more than he wanted initially. You know that each of the customers that come to your shop can buy any number of cans from $l$ to $r$ inclusive. Can you choose such size of pack $a$ that each customer buys more cans than they wanted initially? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first and only line of each test case contains two integers $l$ and $r$ ($1 \le l \le r \le 10^9$) — the range of the number of cans customers can buy. -----Output----- For each test case, print YES if you can choose such size of pack $a$ that each customer buys more cans than they wanted initially. Otherwise, print NO. You can print each character in any case. -----Example----- Input 3 3 4 1 2 120 150 Output YES NO YES -----Note----- In the first test case, you can take, for example, $a = 5$ as the size of the pack. Then if a customer wants to buy $3$ cans, he'll buy $5$ instead ($3 \bmod 5 = 3$, $\frac{5}{2} = 2.5$). The one who wants $4$ cans will also buy $5$ cans. In the second test case, there is no way to choose $a$. In the third test case, you can take, for example, $a = 80$.
{"inputs": ["3\n3 4\n1 2\n120 150\n", "3\n3 4\n1 2\n120 150\n", "1\n000000101 821042\n", "1\n000000101 321987\n", "1\n000001101 321987\n", "1\n10485760 20971520\n", "1\n20971520 41943040\n", "1\n10485760 20971520\n"], "outputs": ["YES\nNO\nYES\n", "YES\nNO\nYES\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n"]}
612
215
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space. Return a string of the words in reverse order concatenated by a single space. Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.   Please complete the following python code precisely: ```python class Solution: def reverseWords(self, s: str) -> str: ```
{"functional": "def check(candidate):\n assert candidate(s = \"the sky is blue\") == \"blue is sky the\"\n assert candidate(s = \" \u00a0hello world \u00a0\") == \"world hello\"\n assert candidate(s = \"a good \u00a0 example\") == \"example good a\"\n\n\ncheck(Solution().reverseWords)"}
136
88
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a string s of length n where s[i] is either: 'D' means decreasing, or 'I' means increasing. A permutation perm of n + 1 integers of all the integers in the range [0, n] is called a valid permutation if for all valid i: If s[i] == 'D', then perm[i] > perm[i + 1], and If s[i] == 'I', then perm[i] < perm[i + 1]. Return the number of valid permutations perm. Since the answer may be large, return it modulo 109 + 7.   Please complete the following python code precisely: ```python class Solution: def numPermsDISequence(self, s: str) -> int: ```
{"functional": "def check(candidate):\n assert candidate(s = \"DID\") == 5\n\n\ncheck(Solution().numPermsDISequence)"}
176
35
coding
Solve the programming task below in a Python markdown code block. Watson gives Sherlock an array $\mbox{A}$ of $N$ elements and two arrays $\mbox{B}$ and $\mbox{C}$, of $\mbox{M}$ elements each. Then he asks Sherlock to perform the following program: for i = 1 to M do for j = 1 to N do if j % B[i] == 0 then A[j] = A[j] * C[i] endif end do end do This code needs to be optimized. Can you help Sherlock and tell him the resulting array $\mbox{A}$? You should print all the array elements modulo $(10^9+7)$. Input Format The first line contains two integer, $N$ and $\mbox{M}$. The next line contains $N$ integers, the elements of array $\mbox{A}$. The last two lines contain $\mbox{M}$ integers each, the elements of array $\mbox{B}$ and $\mbox{C}$, respectively. Output Format Print $N$ space-separated integers, the elements of array $\mbox{A}$ after performing the program modulo $(10^9+7)$. Constraints $1\leq N,M\leq10^5$ $1\leq B[i]\leq N$ $1\leq A[i],C[i]\leq10^5$ Sample Input 4 3 1 2 3 4 1 2 3 13 29 71 Sample Output 13 754 2769 1508
{"inputs": ["4 3\n1 2 3 4\n1 2 3\n13 29 71\n"], "outputs": ["13 754 2769 1508\n"]}
369
54
coding
Solve the programming task below in a Python markdown code block. Alice and Bob are playing a game on a binary string S of length N. Alice and Bob alternate turns, with Alice going first. On their turn, the current player must choose any index 1 ≤ i < |S| such that S_{i} \neq S_{i+1} and erase either S_{i} or S_{i+1} from the string. This reduces the length of S by one, and the remaining parts of S are concatenated in the same order. The player who is unable to make a move loses. Determine the winner if both players play optimally. ------ Input Format ------ - The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N, the length of the string. - The second line contains the binary string S of length N. ------ Output Format ------ For each test case output Alice if Alice wins the game, otherwise output Bob. The output is not case-sensitive, so for example "Bob", "boB" and "Bob" are all considered equivalent. ------ Constraints ------ $1 ≤ T ≤ 1000$ $2 ≤ N ≤ 10^{5}$ $S$ is a binary string - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 4 1111 3 110 4 1010 ----- Sample Output 1 ------ Bob Alice Bob ----- explanation 1 ------ Test Case $1$: There is no such index $i$ such that $S_{i} \neq S_{i+1}$. So, Alice does not have any move and loses, making Bob the winner. Test Case $2$: Alice can choose $i=2$ (because $S_{2} = 1$ and $S_{3} = 0$) and remove $S_{3}$. After this move, the remaining string is "$11$". Bob now has no moves and thus loses.
{"inputs": ["3\n4\n1111\n3\n110\n4\n1010\n"], "outputs": ["Bob\nAlice\nBob"]}
466
37
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. English description is not available for the problem. Please switch to Chinese.   Please complete the following python code precisely: ```python class Solution: def fraction(self, cont: List[int]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(cont = [3, 2, 0, 2]) == [13, 4]\n assert candidate(cont = [0, 0, 3]) == [3, 1]\n\n\ncheck(Solution().fraction)"}
63
66
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an integer array arr and an integer difference, return the length of the longest subsequence in arr which is an arithmetic sequence such that the difference between adjacent elements in the subsequence equals difference. A subsequence is a sequence that can be derived from arr by deleting some or no elements without changing the order of the remaining elements.   Please complete the following python code precisely: ```python class Solution: def longestSubsequence(self, arr: List[int], difference: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(arr = [1,2,3,4], difference = 1) == 4\n assert candidate(arr = [1,3,5,7], difference = 1) == 1\n assert candidate(arr = [1,5,7,8,5,3,4,2,1], difference = -2) == 4\n\n\ncheck(Solution().longestSubsequence)"}
119
100
coding
Solve the programming task below in a Python markdown code block. Janmansh has to submit 3 assignments for Chingari before 10 pm and he starts to do the assignments at X pm. Each assignment takes him 1 hour to complete. Can you tell whether he'll be able to complete all assignments on time or not? ------ Input Format ------ - The first line will contain T - the number of test cases. Then the test cases follow. - The first and only line of each test case contains one integer X - the time when Janmansh starts doing the assignemnts. ------ Output Format ------ For each test case, output Yes if he can complete the assignments on time. Otherwise, output No. You may print each character of Yes and No in uppercase or lowercase (for example, yes, yEs, YES will be considered identical). ------ Constraints ------ $1 ≤T ≤10$ $1 ≤X ≤9$ ----- Sample Input 1 ------ 2 7 9 ----- Sample Output 1 ------ Yes No ----- explanation 1 ------ Test case-1: He can start at $7$pm and finish by $10$ pm. Therefore he can complete the assignments. Test case-2: He can not complete all the $3$ assignments if he starts at $9$ pm.
{"inputs": ["2\n7\n9\n"], "outputs": ["Yes\nNo\n"]}
278
20
coding
Solve the programming task below in a Python markdown code block. Create an OR function, without use of the 'or' keyword, that takes an list of boolean values and runs OR against all of them. Assume there will be between 1 and 6 variables, and return None for an empty list. Also feel free to reuse/extend the following starter code: ```python def alt_or(lst): ```
{"functional": "_inputs = [[[]], [[False, False, False, False, False, False]], [[False, False, False, False, False, True]], [[False, False, False, False, True, False]], [[False, False, False, False, True, True]], [[False, False, False, True, False, False]], [[False, False, False, True, False, True]], [[False, False, False, True, True, False]], [[False, False, False, True, True, True]], [[False, False, True, False, False, False]], [[False, False, True, False, False, True]], [[False, False, True, False, True, False]], [[False, False, True, False, True, True]], [[False, False, True, True, False, False]], [[False, False, True, True, False, True]], [[False, False, True, True, True, False]], [[False, False, True, True, True, True]], [[False, True, False, False, False, False]], [[False, True, False, False, False, True]], [[False, True, False, False, True, False]], [[False, True, False, False, True, True]], [[False, True, False, True, False, False]], [[False, True, False, True, False, True]], [[False, True, False, True, True, False]], [[False, True, False, True, True, True]], [[False, True, True, False, False, False]], [[False, True, True, False, False, True]], [[False, True, True, False, True, False]], [[False, True, True, False, True, True]], [[False, True, True, True, False, False]], [[False, True, True, True, False, True]], [[False, True, True, True, True, False]], [[False, True, True, True, True, True]], [[True, False, False, False, False, False]], [[True, False, False, False, False, True]], [[True, False, False, False, True, False]], [[True, False, False, False, True, True]], [[True, False, False, True, False, False]], [[True, False, False, True, False, True]], [[True, False, False, True, True, False]], [[True, False, False, True, True, True]], [[True, False, True, False, False, False]], [[True, False, True, False, False, True]], [[True, False, True, False, True, False]], [[True, False, True, False, True, True]], [[True, False, True, True, False, False]], [[True, False, True, True, False, True]], [[True, False, True, True, True, False]], [[True, False, True, True, True, True]], [[True, True, False, False, False, False]], [[True, True, False, False, False, True]], [[True, True, False, False, True, False]], [[True, True, False, False, True, True]], [[True, True, False, True, False, False]], [[True, True, False, True, False, True]], [[True, True, False, True, True, False]], [[True, True, False, True, True, True]], [[True, True, True, False, False, False]], [[True, True, True, False, False, True]], [[True, True, True, False, True, False]], [[True, True, True, False, True, True]], [[True, True, True, True, False, False]], [[True, True, True, True, False, True]], [[True, True, True, True, True, False]], [[True, True, True, True, True, True]], [[False, False, False, False, False]], [[False, False, False, False, True]], [[False, False, False, True, False]], [[False, False, False, True, True]], [[False, False, True, False, False]], [[False, False, True, False, True]], [[False, False, True, True, False]], [[False, False, True, True, True]], [[False, True, False, False, False]], [[False, True, False, False, True]], [[False, True, False, True, False]], [[False, True, False, True, True]], [[False, True, True, False, False]], [[False, True, True, False, True]], [[False, True, True, True, False]], [[False, True, True, True, True]], [[True, False, False, False, False]], [[True, False, False, False, True]], [[True, False, False, True, False]], [[True, False, False, True, True]], [[True, False, True, False, False]], [[True, False, True, False, True]], [[True, False, True, True, False]], [[True, False, True, True, True]], [[True, True, False, False, False]], [[True, True, False, False, True]], [[True, True, False, True, False]], [[True, True, False, True, True]], [[True, True, True, False, False]], [[True, True, True, False, True]], [[True, True, True, True, False]], [[True, True, True, True, True]], [[False, False, False, False]], [[False, False, False, True]], [[False, False, True, False]], [[False, False, True, True]], [[False, True, False, False]], [[False, True, False, True]], [[False, True, True, False]], [[False, True, True, True]], [[True, False, False, False]], [[True, False, False, True]], [[True, False, True, False]], [[True, False, True, True]], [[True, True, False, False]], [[True, True, False, True]], [[True, True, True, False]], [[True, True, True, True]], [[False, False, False]], [[False, False, True]], [[False, True, False]], [[False, True, True]], [[True, False, False]], [[True, False, True]], [[True, True, False]], [[True, True, True]], [[False, False]], [[False, True]], [[True, False]], [[True, True]], [[False]], [[True]]]\n_outputs = [[None], [False], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [False], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [False], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [True], [False], [True], [True], [True], [True], [True], [True], [True], [False], [True], [True], [True], [False], [True]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(alt_or(*i), o[0])"}
84
1,943
coding
Solve the programming task below in a Python markdown code block. In Byteland they have a very strange monetary system. Each Bytelandian gold coin has an integer number written on it. A coin n can be exchanged in a bank into three coins: n/2, n/3 and n/4. But these numbers are all rounded down (the banks have to make a profit). You can also sell Bytelandian coins for American dollars. The exchange rate is 1:1. But you can not buy Bytelandian coins. You have one gold coin. What is the maximum amount of American dollars you can get for it? ------ Input Format ------ The input will contain several test cases (not more than 10). \ Each testcase is a single line with a number n, it is the number written on your coin. ------ Output Format ------ For each test case output a single line, containing the maximum amount of American dollars you can make. ------ Constraints ------ $0 ≤ n ≤ 10^{9}$ ----- Sample Input 1 ------ 12 2 ----- Sample Output 1 ------ 13 2 ----- explanation 1 ------ Test case 1: You can change 12 into 6, 4 and 3, and then change these into $6+4+3 = 13$. \ Test case 2: If you try changing the coin 2 into 3 smaller coins, you will get 1, 0 and 0, and later you can get no more than $1$ out of them. \ It is better just to change the $2$ coin directly into $2$.
{"inputs": ["12\n1", "12\n0", "12\n3", "12\n4", "12\n5", "12\n7", "12\n9", "12\n6"], "outputs": ["13\n1\n", "13\n0\n", "13\n3\n", "13\n4\n", "13\n5\n", "13\n7\n", "13\n9\n", "13\n6\n"]}
350
110
coding
Solve the programming task below in a Python markdown code block. An elementary school student Takahashi has come to a variety store. He has two coins, A-yen and B-yen coins (yen is the currency of Japan), and wants to buy a toy that costs C yen. Can he buy it? Note that he lives in Takahashi Kingdom, and may have coins that do not exist in Japan. -----Constraints----- - All input values are integers. - 1 \leq A, B \leq 500 - 1 \leq C \leq 1000 -----Input----- Input is given from Standard Input in the following format: A B C -----Output----- If Takahashi can buy the toy, print Yes; if he cannot, print No. -----Sample Input----- 50 100 120 -----Sample Output----- Yes He has 50 + 100 = 150 yen, so he can buy the 120-yen toy.
{"inputs": ["12 1 5", "17 1 5", "17 2 5", "57 7 14", "57 1 14", "27 1 14", "12 1 14", "64 17 92"], "outputs": ["Yes\n", "Yes\n", "Yes\n", "Yes\n", "Yes\n", "Yes\n", "No\n", "No\n"]}
219
108
coding
Solve the programming task below in a Python markdown code block. During her tantrums the princess usually smashes some collectable porcelain. Every furious shriek is accompanied with one item smashed. The collection of porcelain is arranged neatly on n shelves. Within each shelf the items are placed in one row, so that one can access only the outermost items — the leftmost or the rightmost item, not the ones in the middle of the shelf. Once an item is taken, the next item on that side of the shelf can be accessed (see example). Once an item is taken, it can't be returned to the shelves. You are given the values of all items. Your task is to find the maximal damage the princess' tantrum of m shrieks can inflict on the collection of porcelain. Input The first line of input data contains two integers n (1 ≤ n ≤ 100) and m (1 ≤ m ≤ 10000). The next n lines contain the values of the items on the shelves: the first number gives the number of items on this shelf (an integer between 1 and 100, inclusive), followed by the values of the items (integers between 1 and 100, inclusive), in the order in which they appear on the shelf (the first number corresponds to the leftmost item, the last one — to the rightmost one). The total number of items is guaranteed to be at least m. Output Output the maximal total value of a tantrum of m shrieks. Examples Input 2 3 3 3 7 2 3 4 1 5 Output 15 Input 1 3 4 4 3 1 2 Output 9 Note In the first case there are two shelves, each with three items. To maximize the total value of the items chosen, one can take two items from the left side of the first shelf and one item from the right side of the second shelf. In the second case there is only one shelf, so all three items are taken from it — two from the left side and one from the right side.
{"inputs": ["1 1\n1 100\n", "1 3\n4 4 5 1 2\n", "1 3\n4 4 5 0 2\n", "1 3\n4 4 3 1 2\n", "3 2\n1 10\n1 2\n1 5\n", "3 2\n1 13\n1 2\n1 5\n", "3 2\n1 14\n1 2\n1 5\n", "3 2\n1 13\n1 2\n1 10\n"], "outputs": ["100\n", "11\n", "11\n", "9\n", "15\n", "18\n", "19\n", "23\n"]}
448
183
coding
Solve the programming task below in a Python markdown code block. Rushitote went to a programming contest to distribute apples and oranges to the contestants. He has N apples and M oranges, which need to be divided equally amongst the contestants. Find the maximum possible number of contestants such that: Every contestant gets an equal number of apples; and Every contestant gets an equal number of oranges. Note that every fruit with Rushitote *must* be distributed, there cannot be any left over. For example, 2 apples and 4 oranges can be distributed equally to two contestants, where each one receives 1 apple and 2 oranges. However, 2 apples and 5 oranges can only be distributed equally to one contestant. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and M — the number of apples and oranges, respectively. ------ Output Format ------ For each test case, output on a new line the answer: the maximum number of contestants such that everyone receives an equal number of apples and an equal number of oranges. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N , M ≤ 10^{9}$ ----- Sample Input 1 ------ 3 1 5 2 4 4 6 ----- Sample Output 1 ------ 1 2 2 ----- explanation 1 ------ Test case $1$: There's only one apple, so distributing to more than one person is impossible. Test case $2$: As explained in the statement, $2$ people can each receive $1$ apple and $2$ oranges. Test case $3$: $2$ people can each receive $2$ apples and $3$ oranges. It's not possible to distribute equally to more than two people.
{"inputs": ["3\n1 5\n2 4\n4 6"], "outputs": ["1\n2\n2"]}
396
28
coding
Solve the programming task below in a Python markdown code block. Alice and Bob play a game. They have a binary string $s$ (a string such that each character in it is either $0$ or $1$). Alice moves first, then Bob, then Alice again, and so on. During their move, the player can choose any number (not less than one) of consecutive equal characters in $s$ and delete them. For example, if the string is $10110$, there are $6$ possible moves (deleted characters are bold): $\textbf{1}0110 \to 0110$; $1\textbf{0}110 \to 1110$; $10\textbf{1}10 \to 1010$; $101\textbf{1}0 \to 1010$; $10\textbf{11}0 \to 100$; $1011\textbf{0} \to 1011$. After the characters are removed, the characters to the left and to the right of the removed block become adjacent. I. e. the following sequence of moves is valid: $10\textbf{11}0 \to 1\textbf{00} \to 1$. The game ends when the string becomes empty, and the score of each player is the number of $1$-characters deleted by them. Each player wants to maximize their score. Calculate the resulting score of Alice. -----Input----- The first line contains one integer $T$ ($1 \le T \le 500$) — the number of test cases. Each test case contains exactly one line containing a binary string $s$ ($1 \le |s| \le 100$). -----Output----- For each test case, print one integer — the resulting score of Alice (the number of $1$-characters deleted by her). -----Example----- Input 5 01111001 0000 111111 101010101 011011110111 Output 4 0 6 3 6 -----Note----- Questions about the optimal strategy will be ignored.
{"inputs": ["5\n01111001\n0000\n111111\n101010101\n011011110111\n", "5\n01111101\n0000\n111111\n101010101\n011011110111\n", "5\n01111101\n0000\n111111\n101010101\n011001110111\n", "5\n01111101\n0000\n111111\n101010101\n011001111111\n", "5\n11111101\n0000\n111111\n101010101\n011001111111\n", "5\n11111101\n0000\n111111\n101010101\n011001111011\n", "5\n11111101\n0000\n111111\n101010111\n011001111011\n", "5\n11111101\n0000\n111111\n101010111\n001001111011\n"], "outputs": ["4\n0\n6\n3\n6\n", "5\n0\n6\n3\n6\n", "5\n0\n6\n3\n5\n", "5\n0\n6\n3\n7\n", "6\n0\n6\n3\n7\n", "6\n0\n6\n3\n6\n", "6\n0\n6\n4\n6\n", "6\n0\n6\n4\n5\n"]}
512
486
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Alice and Bob take turns playing a game, with Alice starting first. Initially, there is a number n on the chalkboard. On each player's turn, that player makes a move consisting of: Choosing any x with 0 < x < n and n % x == 0. Replacing the number n on the chalkboard with n - x. Also, if a player cannot make a move, they lose the game. Return true if and only if Alice wins the game, assuming both players play optimally.   Please complete the following python code precisely: ```python class Solution: def divisorGame(self, n: int) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(n = 2) == True\n assert candidate(n = 3) == False\n\n\ncheck(Solution().divisorGame)"}
151
42
coding
Solve the programming task below in a Python markdown code block. Chef played an interesting game yesterday. This game is played with two variables $X$ and $Y$; initially, $X = Y = 0$. Chef may make an arbitrary number of moves (including zero). In each move, he must perform the following process: - Choose any positive integer $P$ such that $P \cdot P > Y$. - Change $X$ to $P$. - Add $P \cdot P$ to $Y$. Unfortunately, Chef has a bad memory and he has forgotten the moves he made. He only remembers the value of $X$ after the game finished; let's denote it by $X_f$. Can you tell him the maximum possible number of moves he could have made in the game? -----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 $X_f$. -----Output----- For each test case, print a single line containing one integer — the maximum number of moves Chef could have made. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le X_f \le 10^9$ -----Example Input----- 3 3 8 9 -----Example Output----- 3 5 6 -----Explanation----- Example case 2: One possible sequence of values of $X$ is $0 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 5 \rightarrow 8$.
{"inputs": ["3\n3\n8\n9"], "outputs": ["3\n5\n6"]}
342
22
coding
Solve the programming task below in a Python markdown code block. Consider the array $a$ composed of all the integers in the range $[l, r]$. For example, if $l = 3$ and $r = 7$, then $a = [3, 4, 5, 6, 7]$. Given $l$, $r$, and $k$, is it possible for $\gcd(a)$ to be greater than $1$ after doing the following operation at most $k$ times? Choose $2$ numbers from $a$. Permanently remove one occurrence of each of them from the array. Insert their product back into $a$. $\gcd(b)$ denotes the greatest common divisor (GCD) of the integers in $b$. -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The description of test cases follows. The input for each test case consists of a single line containing $3$ non-negative integers $l$, $r$, and $k$ ($1 \leq l \leq r \leq 10^9, \enspace 0 \leq k \leq r - l$). -----Output----- For each test case, print "YES" if it is possible to have the GCD of the corresponding array greater than $1$ by performing at most $k$ operations, and "NO" otherwise (case insensitive). -----Examples----- Input 9 1 1 0 3 5 1 13 13 0 4 4 0 3 7 4 4 10 3 2 4 0 1 7 3 1 5 3 Output NO NO YES YES YES YES NO NO YES -----Note----- For the first test case, $a = [1]$, so the answer is "NO", since the only element in the array is $1$. For the second test case the array is $a = [3, 4, 5]$ and we have $1$ operation. After the first operation the array can change to: $[3, 20]$, $[4, 15]$ or $[5, 12]$ all of which having their greatest common divisor equal to $1$ so the answer is "NO". For the third test case, $a = [13]$, so the answer is "YES", since the only element in the array is $13$. For the fourth test case, $a = [4]$, so the answer is "YES", since the only element in the array is $4$.
{"inputs": ["1\n1 1000000000 999999999\n", "9\n1 1 0\n3 5 1\n13 13 0\n4 4 0\n3 7 4\n4 10 3\n2 4 0\n1 7 3\n1 5 3\n"], "outputs": ["YES\n", "NO\nNO\nYES\nYES\nYES\nYES\nNO\nNO\nYES\n"]}
587
118
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. A transformation sequence from word beginWord to word endWord using a dictionary wordList is a sequence of words beginWord -> s1 -> s2 -> ... -> sk such that: Every adjacent pair of words differs by a single letter. Every si for 1 <= i <= k is in wordList. Note that beginWord does not need to be in wordList. sk == endWord Given two words, beginWord and endWord, and a dictionary wordList, return the number of words in the shortest transformation sequence from beginWord to endWord, or 0 if no such sequence exists.   Please complete the following python code precisely: ```python class Solution: def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(beginWord = \"hit\", endWord = \"cog\", wordList = [\"hot\",\"dot\",\"dog\",\"lot\",\"log\",\"cog\"]) == 5\n assert candidate(beginWord = \"hit\", endWord = \"cog\", wordList = [\"hot\",\"dot\",\"dog\",\"lot\",\"log\"]) == 0\n\n\ncheck(Solution().ladderLength)"}
180
95
coding
Solve the programming task below in a Python markdown code block. You probably know the 42 number as "The answer to life, the universe and everything" according to Douglas Adams' "The Hitchhiker's Guide to the Galaxy". For Freud, the answer was quite different. In the society he lived in, people-women in particular- had to repress their sexual needs and desires. This was simply how the society was at the time. Freud then wanted to study the illnesses created by this, and so he digged to the root of their desires. This led to some of the most important psychoanalytic theories to this day, Freud being the father of psychoanalysis. Now, basically, when a person hears about Freud, s/he hears "sex" because for Freud, everything was basically related to, and explained by sex. In this kata, the toFreud() function will take a string as its argument, and return a string with every word replaced by the explanation to everything, according to Freud. Note that an empty string, or no arguments, should result in the ouput being ""(empty string). Also feel free to reuse/extend the following starter code: ```python def to_freud(sentence): ```
{"functional": "_inputs = [['test'], ['sexy sex'], ['This is a test'], ['This is a longer test'], [\"You're becoming a true freudian expert\"]]\n_outputs = [['sex'], ['sex sex'], ['sex sex sex sex'], ['sex sex sex sex sex'], ['sex sex sex sex sex sex']]\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(to_freud(*i), o[0])"}
254
209
coding
Solve the programming task below in a Python markdown code block. Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
{"inputs": ["5\n5 4 3 2 1 2 3 4 5\n", "0\n1 1 1 1 1 1 1 1 1\n", "5\n5 4 3 2 1 4 3 4 5\n", "0\n1 1 1 1 1 1 1 1 1\n", "5\n5 4 3 2 1 2 3 4 5\n", "50\n5 3 10 2 2 4 3 6 5\n", "50\n5 3 10 2 2 4 3 6 5\n", "50\n5 3 10 2 2 5 3 6 5\n"], "outputs": ["55555\n", "-1\n", "55555\n", "-1\n", "55555\n", "5555555555555555555555555\n", "5555555555555555555555555\n", "5555555555555555555555555\n"]}
311
304
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai. For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1. Return true if you can finish all courses. Otherwise, return false.   Please complete the following python code precisely: ```python class Solution: def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(numCourses = 2, prerequisites = [[1,0]]) == True\n assert candidate(numCourses = 2, prerequisites = [[1,0],[0,1]]) == False\n\n\ncheck(Solution().canFinish)"}
151
61
coding
Solve the programming task below in a Python markdown code block. Tanechka is shopping in the toy shop. There are exactly $n$ toys in the shop for sale, the cost of the $i$-th toy is $i$ burles. She wants to choose two toys in such a way that their total cost is $k$ burles. How many ways to do that does she have? Each toy appears in the shop exactly once. Pairs $(a, b)$ and $(b, a)$ are considered equal. Pairs $(a, b)$, where $a=b$, are not allowed. -----Input----- The first line of the input contains two integers $n$, $k$ ($1 \le n, k \le 10^{14}$) — the number of toys and the expected total cost of the pair of toys. -----Output----- Print the number of ways to choose the pair of toys satisfying the condition above. Print 0, if Tanechka can choose no pair of toys in such a way that their total cost is $k$ burles. -----Examples----- Input 8 5 Output 2 Input 8 15 Output 1 Input 7 20 Output 0 Input 1000000000000 1000000000001 Output 500000000000 -----Note----- In the first example Tanechka can choose the pair of toys ($1, 4$) or the pair of toys ($2, 3$). In the second example Tanechka can choose only the pair of toys ($7, 8$). In the third example choosing any pair of toys will lead to the total cost less than $20$. So the answer is 0. In the fourth example she can choose the following pairs: $(1, 1000000000000)$, $(2, 999999999999)$, $(3, 999999999998)$, ..., $(500000000000, 500000000001)$. The number of such pairs is exactly $500000000000$.
{"inputs": ["8 5\n", "1 1\n", "1 2\n", "1 3\n", "1 4\n", "1 5\n", "1 6\n", "1 7\n"], "outputs": ["2\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
511
86
coding
Solve the programming task below in a Python markdown code block. It's John's birthday; he has brought some candies in distribute among N of his friends. Being a good friend, he decided to distribute all of his candies equally among his friend and he don't want to keep any for himself. He left the task of distributing candies up to his friends, but his friends haven't done a great job in distributing them and now they all have unequal amount of candies. He asked all of his friends to keep all the candies in their hands (A[1] to A[N]) so that he can redistribute the candies. Taking a candy away from someone makes them sad and makes the person who gets it happy. Thus, you do not want to do many operations on the number of candies. Here each operation stands for, taking away 1 candy from someone and giving it to somebody else. After applying the operations, he might not be able to distribute all the candies among all of your friends, and you might need to buy some more candies as well. Buying each candy will be counted as 1 operation as well. John is saving up for his birthday party in the evening and so he wants to spend the least amount of money on candies as possible. Thus, he asks for your help. You have to find the minimum number of operations required to make the distributions equal. -----Input:----- - The first line consists of the number T representing the number of test cases. - Each test case consists of 2 lines. - The first line contains of the number N representing the number of friends. - The second line contains space separated numbers (A[1] to A[N]) representing the number of candies each friend currently has. -----Output:----- For each test case, you much print the minimum number of operations. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^5$ - $0 \leq A[i] \leq 10^9$ -----Sample Input:----- 3 4 7 8 6 4 6 7 3 10 2 3 8 1 10 -----Sample Output:----- 4 10 0
{"inputs": ["3\n4\n7 8 6 4\n6\n7 3 10 2 3 8\n1\n10"], "outputs": ["4\n10\n0"]}
465
47
coding
Solve the programming task below in a Python markdown code block. Chef's current rating is X, and he wants to improve it. It is generally recommended that a person with rating X should solve problems whose difficulty lies in the range [X, X+200], i.e, problems whose difficulty is at least X and at most X+200. You find out that Chef is currently solving problems with a difficulty of Y. Is Chef following the recommended practice or not? ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. The description of the test cases follows. - Each test case consists of a single line of input, containing two space-separated integers X, Y. ------ Output Format ------ For each test case, output on a new line YES if Chef is following the recommended practice style, and NO otherwise. Each letter of the output may be printed in either lowercase or uppercase. For example, the strings YES, yEs, and Yes will be considered identical. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ X, Y ≤ 4000$ ----- Sample Input 1 ------ 5 1300 1500 1201 1402 300 4000 723 805 1330 512 ----- Sample Output 1 ------ YES NO NO YES NO ----- explanation 1 ------ Test case $1$: Chef's current rating is $1300$, so he should solve problems with difficulty lying in $[1300,1500]$. Since $1500$ lies in $[1300,1500]$, Chef is doing his practice in a recommended way :) Test case $2$: Chef's current rating is $1201$, so he should solve problems with difficulty lying in $[1201,1401]$. Since $1402$ does not lie in $[1201,1401]$, Chef is not doing his practice in a recommended way :( Test case $3$: Chef's current rating is $300$, so he should solve problems with difficulty lying in $[300,500]$. Since $4000$ does not lie in $[300,500]$, Chef is not doing his practice in a recommended way :( Test case $4$: Chef's current rating is $723$, so he should solve problems with difficulty lying in $[723,923]$. Since $805$ lies in $[723,923]$, Chef is doing his practice in a recommended way :) Test case $5$: Chef's current rating is $1330$, so he should solve problems with difficulty lying in $[1330,1530]$. Since $512$ does not lie in $[1330,1530]$, Chef is not doing his practice in a recommended way :(
{"inputs": ["5\n1300 1500\n1201 1402\n300 4000\n723 805\n1330 512\n"], "outputs": ["YES\nNO\nNO\nYES\nNO\n"]}
667
68
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Alice has n balloons arranged on a rope. You are given a 0-indexed string colors where colors[i] is the color of the ith balloon. Alice wants the rope to be colorful. She does not want two consecutive balloons to be of the same color, so she asks Bob for help. Bob can remove some balloons from the rope to make it colorful. You are given a 0-indexed integer array neededTime where neededTime[i] is the time (in seconds) that Bob needs to remove the ith balloon from the rope. Return the minimum time Bob needs to make the rope colorful.   Please complete the following python code precisely: ```python class Solution: def minCost(self, colors: str, neededTime: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(colors = \"abaac\", neededTime = [1,2,3,4,5]) == 3\n assert candidate(colors = \"abc\", neededTime = [1,2,3]) == 0\n assert candidate(colors = \"aabaa\", neededTime = [1,2,3,4,1]) == 2\n\n\ncheck(Solution().minCost)"}
174
95
coding
Solve the programming task below in a Python markdown code block. Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special. Alyona is a capricious girl so after she gets the array, she inspects m of its subarrays. Subarray is a set of some subsequent elements of the array. The i-th subarray is described with two integers l_{i} and r_{i}, and its elements are a[l_{i}], a[l_{i} + 1], ..., a[r_{i}]. Alyona is going to find mex for each of the chosen subarrays. Among these m mexes the girl is going to find the smallest. She wants this minimum mex to be as large as possible. You are to find an array a of n elements so that the minimum mex among those chosen by Alyona subarrays is as large as possible. The mex of a set S is a minimum possible non-negative integer that is not in S. -----Input----- The first line contains two integers n and m (1 ≤ n, m ≤ 10^5). The next m lines contain information about the subarrays chosen by Alyona. The i-th of these lines contains two integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n), that describe the subarray a[l_{i}], a[l_{i} + 1], ..., a[r_{i}]. -----Output----- In the first line print single integer — the maximum possible minimum mex. In the second line print n integers — the array a. All the elements in a should be between 0 and 10^9. It is guaranteed that there is an optimal answer in which all the elements in a are between 0 and 10^9. If there are multiple solutions, print any of them. -----Examples----- Input 5 3 1 3 2 5 4 5 Output 2 1 0 2 1 0 Input 4 2 1 4 2 4 Output 3 5 2 0 1 -----Note----- The first example: the mex of the subarray (1, 3) is equal to 3, the mex of the subarray (2, 5) is equal to 3, the mex of the subarray (4, 5) is equal to 2 as well, thus the minumal mex among the subarrays chosen by Alyona is equal to 2.
{"inputs": ["1 1\n1 1\n", "2 1\n2 2\n", "9 1\n1 1\n", "1 1\n1 1\n", "2 1\n2 2\n", "9 1\n1 1\n", "2 1\n2 3\n", "4 2\n1 4\n2 4\n"], "outputs": ["1\n0\n", "1\n0 0\n", "1\n0 0 0 0 0 0 0 0 0\n", "1\n0 ", "1\n0 0 ", "1\n0 0 0 0 0 0 0 0 0 ", "2\n0 1\n", "3\n0 1 2 0\n"]}
541
179
coding
Solve the programming task below in a Python markdown code block. # Introduction and Warm-up (Highly recommended) # [Playing With Lists/Arrays Series](https://www.codewars.com/collections/playing-with-lists-slash-arrays) ___ # Task **_Given_** an *array/list [] of integers* , **_Find_** **_The maximum difference_** *between the successive elements in its sorted form*. ___ # Notes * **_Array/list_** size is *at least 3* . * **_Array/list's numbers_** Will be **mixture of positives and negatives also zeros_** * **_Repetition_** of numbers in *the array/list could occur*. * **_The Maximum Gap_** is *computed Regardless the sign*. ___ # Input >> Output Examples ``` maxGap ({13,10,5,2,9}) ==> return (4) ``` ## **_Explanation_**: * **_The Maximum Gap_** *after sorting the array is* `4` , *The difference between* ``` 9 - 5 = 4 ``` . ___ ``` maxGap ({-3,-27,-4,-2}) ==> return (23) ``` ## **_Explanation_**: * **_The Maximum Gap_** *after sorting the array is* `23` , *The difference between* ` |-4- (-27) | = 23` . * **_Note_** : *Regardless the sign of negativity* . ___ ``` maxGap ({-7,-42,-809,-14,-12}) ==> return (767) ``` ## **_Explanation_**: * **_The Maximum Gap_** *after sorting the array is* `767` , *The difference between* ` | -809- (-42) | = 767` . * **_Note_** : *Regardless the sign of negativity* . ___ ``` maxGap ({-54,37,0,64,640,0,-15}) //return (576) ``` ## **_Explanation_**: * **_The Maximum Gap_** *after sorting the array is* `576` , *The difference between* ` | 64 - 640 | = 576` . * **_Note_** : *Regardless the sign of negativity* . ___ ___ ___ # [Playing with Numbers Series](https://www.codewars.com/collections/playing-with-numbers) # [Playing With Lists/Arrays Series](https://www.codewars.com/collections/playing-with-lists-slash-arrays) # [For More Enjoyable Katas](http://www.codewars.com/users/MrZizoScream/authored) ___ ## ALL translations are welcomed ## Enjoy Learning !! # Zizou Also feel free to reuse/extend the following starter code: ```python def max_gap(numbers): ```
{"functional": "_inputs = [[[13, 10, 2, 9, 5]], [[13, 3, 5]], [[24, 299, 131, 14, 26, 25]], [[-3, -27, -4, -2]], [[-7, -42, -809, -14, -12]], [[12, -5, -7, 0, 290]], [[-54, 37, 0, 64, -15, 640, 0]], [[130, 30, 50]], [[1, 1, 1]], [[-1, -1, -1]]]\n_outputs = [[4], [8], [168], [23], [767], [278], [576], [80], [0], [0]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(max_gap(*i), o[0])"}
657
354
coding
Solve the programming task below in a Python markdown code block. Consider the following game: There are two players, First and Second, sitting in front of a pile of $n$ stones. First always plays first. There is a set, $\mbox{S}$, of $m$ distinct integers defined as $S=\{s_0,s_1,\ldots,s_{m-1}\}$. The players move in alternating turns. During each turn, a player chooses some $s_i\in S$ and splits one of the piles into exactly $s_i$ smaller piles of equal size. If no $s_i$ exists that will split one of the available piles into exactly $s_i$ equal smaller piles, the player loses. Both players always play optimally. Given $n$, $m$, and the contents of $\mbox{S}$, find and print the winner of the game. If First wins, print First; otherwise, print Second. Input Format The first line contains two space-separated integers describing the respective values of $n$ (the size of the initial pile) and $m$ (the size of the set). The second line contains $m$ distinct space-separated integers describing the respective values of $s_0,s_1,\ldots,s_{m-1}$. Constraints $1\leq n\leq10^{18}$ $1\leq m\leq10$ $2\leq s_i\leq10^{18}$ Output Format Print First if the First player wins the game; otherwise, print Second. Sample Input 0 15 3 5 2 3 Sample Output 0 Second Explanation 0 The initial pile has $n=15$ stones, and $S=\{5,2,3\}$. During First's initial turn, they have two options: Split the initial pile into $5$ equal piles, which forces them to lose after the following sequence of turns: Split the initial pile into $3$ equal piles, which forces them to lose after the following sequence of turns: Because First never has any possible move that puts them on the path to winning, we print Second as our answer.
{"inputs": ["15 3\n5 2 3\n"], "outputs": ["Second\n"]}
471
23
coding
Solve the programming task below in a Python markdown code block. Alice and Bob are playing the game of Nim with $n$ piles of stones with sizes $p_0,p_1,\ldots,p_{n-1}$. If Alice plays first, she loses if and only if the 'xor sum' (or 'Nim sum') of the piles is zero, i.e., $p_0\oplus p_1\oplus...\oplus p_{n-1}=0$. Since Bob already knows who will win (assuming optimal play), he decides to cheat by removing some stones in some piles before the game starts. However, to reduce the risk of suspicion, he must keep at least one pile unchanged. Your task is to count the number of ways Bob can remove the stones to force Alice into losing the game. Since the number can be very large, output the number of ways modulo $10^9+7$. Assume that both players will try to optimize their strategy and try to win the game. Input Format The first line of the input contains an integer $n$ denoting the number of piles. The next line contains $n$ space-separated integers $p_0,p_1,\ldots,p_{n-1}$ indicating the sizes of the stone piles. Constraints $3\leq n\leq100$ $0<p[i]<10^9$ Output Format Print a single integer denoting the number of ways Bob can force Alice to lose the game, modulo $10^9+7$. Sample Input 0 3 1 2 3 Sample Output 0 4 Explanation 0 The answer is $4$. The four possible resulting lists of piles is: $[0,2,2]$ $[1,0,1]$ $[1,1,0]$ $[1,2,3]$ Note that $[0,1,1]$ is not allowed since he must keep one pile unchanged. Sample Input 1 10 10 10 1 1 1 1 1 10 10 10 Sample Output 1 321616
{"inputs": ["3\n1 2 3\n", "10\n10 10 1 1 1 1 1 10 10 10\n"], "outputs": ["4\n", "321616\n"]}
467
59
coding
Solve the programming task below in a Python markdown code block. Given is an undirected graph G consisting of N vertices numbered 1 through N and M edges numbered 1 through M. Edge i connects Vertex a_i and Vertex b_i bidirectionally. G is said to be a good graph when both of the conditions below are satisfied. It is guaranteed that G is initially a good graph. - Vertex 1 and Vertex N are not connected. - There are no self-loops and no multi-edges. Taro the first and Jiro the second will play a game against each other. They will alternately take turns, with Taro the first going first. In each player's turn, the player can do the following operation: - Operation: Choose vertices u and v, then add to G an edge connecting u and v bidirectionally. The player whose addition of an edge results in G being no longer a good graph loses. Determine the winner of the game when the two players play optimally. You are given T test cases. Solve each of them. -----Constraints----- - All values in input are integers. - 1 \leq T \leq 10^5 - 2 \leq N \leq 10^{5} - 0 \leq M \leq \min(N(N-1)/2,10^{5}) - 1 \leq a_i,b_i \leq N - The given graph is a good graph. - In one input file, the sum of N and that of M do not exceed 2 \times 10^5. -----Input----- Input is given from Standard Input in the following format: T \mathrm{case}_1 \vdots \mathrm{case}_T Each case is in the following format: N M a_1 b_1 \vdots a_M b_M -----Output----- Print T lines. The i-th line should contain First if Taro the first wins in the i-th test case, and Second if Jiro the second wins in the test case. -----Sample Input----- 3 3 0 6 2 1 2 2 3 15 10 12 14 8 3 10 1 14 6 12 6 1 9 13 1 2 5 3 9 7 2 -----Sample Output----- First Second First - In test case 1, Taro the first wins. Below is one sequence of moves that results in Taro's win: - In Taro the first's turn, he adds an edge connecting Vertex 1 and 2, after which the graph is still good. - Then, whichever two vertices Jiro the second would choose to connect with an edge, the graph would no longer be good. - Thus, Taro wins.
{"inputs": ["1\n100000 0\n", "3\n3 0\n6 2\n1 2\n2 3\n15 10\n12 14\n8 3\n10 1\n14 6\n12 6\n1 9\n13 1\n2 5\n3 9\n7 2\n"], "outputs": ["First\n", "First\nSecond\nFirst\n"]}
609
103
coding
Solve the programming task below in a Python markdown code block. You are given a ternary string (it is a string which consists only of characters '0', '1' and '2'). You can swap any two adjacent (consecutive) characters '0' and '1' (i.e. replace "01" with "10" or vice versa) or any two adjacent (consecutive) characters '1' and '2' (i.e. replace "12" with "21" or vice versa). For example, for string "010210" we can perform the following moves: "010210" $\rightarrow$ "100210"; "010210" $\rightarrow$ "001210"; "010210" $\rightarrow$ "010120"; "010210" $\rightarrow$ "010201". Note than you cannot swap "02" $\rightarrow$ "20" and vice versa. You cannot perform any other operations with the given string excluding described above. You task is to obtain the minimum possible (lexicographically) string by using these swaps arbitrary number of times (possibly, zero). String $a$ is lexicographically less than string $b$ (if strings $a$ and $b$ have the same length) if there exists some position $i$ ($1 \le i \le |a|$, where $|s|$ is the length of the string $s$) such that for every $j < i$ holds $a_j = b_j$, and $a_i < b_i$. -----Input----- The first line of the input contains the string $s$ consisting only of characters '0', '1' and '2', its length is between $1$ and $10^5$ (inclusive). -----Output----- Print a single string — the minimum possible (lexicographically) string you can obtain by using the swaps described above arbitrary number of times (possibly, zero). -----Examples----- Input 100210 Output 001120 Input 11222121 Output 11112222 Input 20 Output 20
{"inputs": ["2\n", "1\n", "1\n", "2\n", "0\n", "20\n", "10\n", "12\n"], "outputs": ["2\n", "1\n", "1\n", "2\n", "0\n", "20\n", "01\n", "12\n"]}
495
76
coding
Solve the programming task below in a Python markdown code block. Mr. Yamada Springfield Tanaka was appointed as Deputy Deputy Director of the National Land Readjustment Business Bureau. Currently, his country is in the midst of a major land readjustment, and if this land readjustment can be completed smoothly, his promotion is certain. However, there are many people who are not happy with his career advancement. One such person was Mr. Sato Seabreeze Suzuki. He has planned to pull Mr. Yamada's foot every time. Again, Mr. Sato put pressure on the organization responsible for the actual land readjustment in order to pull back, making the land readjustment results very confusing. Therefore, the result passed to Mr. Yamada was only information about which straight line divided a square land. At the very least, Mr. Yamada is sure to be fired rather than promoted if he doesn't know just how many divisions the square land has. Your job is how many square regions with vertices (-100, -100), (100, -100), (100, 100), (-100, 100) are divided by a given n straight lines. To save Mr. Yamada from the crisis of dismissal by writing a program to find out. Input The input consists of multiple test cases. The first line of each test case is given the integer n representing the number of straight lines (1 <= n <= 100). Subsequent n lines contain four integers x1, y1, x2, and y2, respectively. These integers represent two different points (x1, y1) and (x2, y2) on a straight line. The two points given are always guaranteed to be points on the sides of the square. The n straight lines given are different from each other, and the straight lines do not overlap. Also, the straight line does not overlap the sides of the square. The end of input is represented by n = 0. Output For each test case, print the number of regions divided by n straight lines in one line. Two points with a distance of less than 10-10 can be considered to match. Also, there is no set of intersections P, Q, R such that | PQ | <10-10, | QR | <10-10 and | PR |> = 10-10. Example Input 2 -100 -20 100 20 -20 -100 20 100 2 -100 -20 -20 -100 20 100 100 20 0 Output 4 3
{"inputs": ["2\n-3 0 000 29\n-2 -100 18 100\n0\n-100 -20 -13 -280\n20 100 100 2\n0", "2\n-3 1 100 34\n-5 -100 18 100\n0\n-100 -20 -13 -284\n20 100 100 3\n0", "2\n-100 0 100 43\n-20 -100 18 100\n0\n-100 -4 -9 -284\n20 100 100 3\n0", "2\n-100 0 101 43\n-20 -100 18 100\n0\n-100 -4 -9 -284\n20 100 100 3\n0", "2\n-100 0 101 43\n-20 -195 18 100\n0\n-100 -4 -9 -284\n20 100 100 3\n0", "2\n-100 0 101 43\n-20 -195 18 100\n0\n-100 -4 -9 -284\n20 100 100 0\n0", "2\n-6 -20 101 3\n-20 -100 35 100\n2\n-4 -34 -20 -135\n20 101 100 20\n0", "2\n-100 0 100 43\n-20 -100 18 100\n0\n-100 -4 -9 -284\n20 000 100 3\n0"], "outputs": ["3\n", "4\n", "4\n", "4\n", "4\n", "4\n", "4\n3\n", "4\n"]}
585
533
coding
Solve the programming task below in a Python markdown code block. Some country is populated by wizards. They want to organize a demonstration. There are n people living in the city, x of them are the wizards who will surely go to the demonstration. Other city people (n - x people) do not support the wizards and aren't going to go to the demonstration. We know that the city administration will react only to the demonstration involving at least y percent of the city people. Having considered the matter, the wizards decided to create clone puppets which can substitute the city people on the demonstration. So all in all, the demonstration will involve only the wizards and their puppets. The city administration cannot tell the difference between a puppet and a person, so, as they calculate the percentage, the administration will consider the city to be consisting of only n people and not containing any clone puppets. Help the wizards and find the minimum number of clones to create to that the demonstration had no less than y percent of the city people. Input The first line contains three space-separated integers, n, x, y (1 ≤ n, x, y ≤ 104, x ≤ n) — the number of citizens in the city, the number of wizards and the percentage the administration needs, correspondingly. Please note that y can exceed 100 percent, that is, the administration wants to see on a demonstration more people that actually live in the city ( > n). Output Print a single integer — the answer to the problem, the minimum number of clones to create, so that the demonstration involved no less than y percent of n (the real total city population). Examples Input 10 1 14 Output 1 Input 20 10 50 Output 0 Input 1000 352 146 Output 1108 Note In the first sample it is necessary that at least 14% of 10 people came to the demonstration. As the number of people should be integer, then at least two people should come. There is only one wizard living in the city and he is going to come. That isn't enough, so he needs to create one clone. In the second sample 10 people should come to the demonstration. The city has 10 wizards. They will all come to the demonstration, so nobody has to create any clones.
{"inputs": ["1 1 1\n", "1 0 1\n", "3 1 69\n", "67 1 3\n", "2 1 69\n", "67 2 6\n", "67 2 9\n", "67 2 3\n"], "outputs": ["0\n", "1\n", "2\n", "2\n", "1\n", "3\n", "5\n", "1\n"]}
505
108
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$. Chef wants you to handle $Q$ queries. There are two types of queries: 1 L R X: for each $i$ ($L ≤ i ≤ R$), add $(X + i - L)^2$ to $A_{i}$ 2 Y: find the current value of $A_{Y}$ ------ Input ------ The first line of the input contains two space-separated integers $N$ and $Q$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. $Q$ lines follow. Each of these lines describes a query in the format described above. ------ Output ------ For each query of the second type, print a single line containing one integer $A_{Y}$. ------ Constraints ------ $1 ≤ N, Q ≤ 10^{5}$ $0 ≤ |A_{i}| ≤ 10^{6}$ for each valid $i$ $1 ≤ L ≤ R ≤ N$ $0 ≤ |X| ≤ 10^{6}$ $1 ≤ Y ≤ N$ ----- Sample Input 1 ------ 5 4 1 10 3 6 5 1 1 3 5 2 3 1 4 5 7 2 5 ----- Sample Output 1 ------ 52 69 ----- explanation 1 ------ - the sequence after the first query is $[1 + 5^{2}, 10 + (5 + 2 - 1)^2, 3 + (5 + 3 - 1)^2, 6, 5] = [26, 46, 52, 6, 5]$ - the second query asks for $A_{3}$, which is $52$ - the sequence after the third query is $[26, 46, 52, 6 + 7^{2}, 5 + (7+1)^2] = [26, 46, 52, 55, 69]$ - the fourth query asks for $A_{5}$, which is $69$
{"inputs": ["5 4\n1 10 3 6 5\n1 1 3 5\n2 3\n1 4 5 7\n2 5"], "outputs": ["52\n69"]}
527
53
coding
Solve the programming task below in a Python markdown code block. Chef has two integers A and B (A ≤ B). Chef can choose any non-negative integer X and add them to both A and B. Find whether it is possible to make A a [divisor] of B. ------ 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 integers A and B. ------ Output Format ------ For each test case, output YES if it is possible to make A a factor of B, NO otherwise. You can print each character of the string in uppercase or lowercase. For example, the strings Yes, YES, yes, and yEs, are all considered identical. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ A ≤ B ≤ 10^{9}$ ----- Sample Input 1 ------ 3 3 6 4 14 9 10 ----- Sample Output 1 ------ YES YES NO ----- explanation 1 ------ Test case $1$: We can choose $X = 0$ and add them to $3$ and $6$. Thus, $3$ is a factor of $6$. Test case $2$: We can choose $X = 1$ and add them to $4$ and $14$. Thus, $4+1 = 5$ is a factor of $14+1 = 15$. Test case $3$: There is no possible value of $X$ to add such that $A$ becomes a factor of $B$.
{"inputs": ["3\n3 6\n4 14\n9 10\n"], "outputs": ["YES\nYES\nNO\n"]}
346
32
coding
Solve the programming task below in a Python markdown code block. Vasya came up with his own weather forecasting method. He knows the information about the average air temperature for each of the last n days. Assume that the average air temperature for each day is integral. Vasya believes that if the average temperatures over the last n days form an arithmetic progression, where the first term equals to the average temperature on the first day, the second term equals to the average temperature on the second day and so on, then the average temperature of the next (n + 1)-th day will be equal to the next term of the arithmetic progression. Otherwise, according to Vasya's method, the temperature of the (n + 1)-th day will be equal to the temperature of the n-th day. Your task is to help Vasya predict the average temperature for tomorrow, i. e. for the (n + 1)-th day. -----Input----- The first line contains a single integer n (2 ≤ n ≤ 100) — the number of days for which the average air temperature is known. The second line contains a sequence of integers t_1, t_2, ..., t_{n} ( - 1000 ≤ t_{i} ≤ 1000) — where t_{i} is the average temperature in the i-th day. -----Output----- Print the average air temperature in the (n + 1)-th day, which Vasya predicts according to his method. Note that the absolute value of the predicted temperature can exceed 1000. -----Examples----- Input 5 10 5 0 -5 -10 Output -15 Input 4 1 1 1 1 Output 1 Input 3 5 1 -5 Output -5 Input 2 900 1000 Output 1100 -----Note----- In the first example the sequence of the average temperatures is an arithmetic progression where the first term is 10 and each following terms decreases by 5. So the predicted average temperature for the sixth day is - 10 - 5 = - 15. In the second example the sequence of the average temperatures is an arithmetic progression where the first term is 1 and each following terms equals to the previous one. So the predicted average temperature in the fifth day is 1. In the third example the average temperatures do not form an arithmetic progression, so the average temperature of the fourth day equals to the temperature of the third day and equals to - 5. In the fourth example the sequence of the average temperatures is an arithmetic progression where the first term is 900 and each the following terms increase by 100. So predicted average temperature in the third day is 1000 + 100 = 1100.
{"inputs": ["2\n1 2\n", "2\n1 2\n", "2\n2 2\n", "2\n8 2\n", "2\n4 2\n", "2\n16 2\n", "3\n2 5 8\n", "3\n2 4 8\n"], "outputs": ["3\n", "3", "2\n", "-4\n", "0\n", "-12\n", "11\n", "8\n"]}
610
108
coding
Solve the programming task below in a Python markdown code block. There are three horses living in a horse land: one gray, one white and one gray-and-white. The horses are really amusing animals, which is why they adore special cards. Each of those cards must contain two integers, the first one on top, the second one in the bottom of the card. Let's denote a card with a on the top and b in the bottom as (a, b). Each of the three horses can paint the special cards. If you show an (a, b) card to the gray horse, then the horse can paint a new (a + 1, b + 1) card. If you show an (a, b) card, such that a and b are even integers, to the white horse, then the horse can paint a new $(\frac{a}{2}, \frac{b}{2})$ card. If you show two cards (a, b) and (b, c) to the gray-and-white horse, then he can paint a new (a, c) card. Polycarpus really wants to get n special cards (1, a_1), (1, a_2), ..., (1, a_{n}). For that he is going to the horse land. He can take exactly one (x, y) card to the horse land, such that 1 ≤ x < y ≤ m. How many ways are there to choose the card so that he can perform some actions in the horse land and get the required cards? Polycarpus can get cards from the horses only as a result of the actions that are described above. Polycarpus is allowed to get additional cards besides the cards that he requires. -----Input----- The first line contains two integers n, m (1 ≤ n ≤ 10^5, 2 ≤ m ≤ 10^9). The second line contains the sequence of integers a_1, a_2, ..., a_{n} (2 ≤ a_{i} ≤ 10^9). Note, that the numbers in the sequence can coincide. The numbers in the lines are separated by single spaces. -----Output----- Print a single integer — the answer to the problem. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 1 6 2 Output 11 Input 1 6 7 Output 14 Input 2 10 13 7 Output 36
{"inputs": ["1 6\n2\n", "1 6\n7\n", "1 6\n3\n", "1 6\n4\n", "1 7\n2\n", "1 6\n2\n", "1 6\n7\n", "1 24\n10\n"], "outputs": ["11\n", "14\n", "11\n", "14\n", "14\n", " 11\n", " 14\n", "161\n"]}
553
117
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has 3 bags that she wants to take on a flight. They weigh A, B, and C kgs respectively. She wants to check-in exactly two of these bags and carry the remaining one bag with her. The airline restrictions says that the total sum of the weights of the bags that are checked-in cannot exceed D kgs and the weight of the bag which is carried cannot exceed E kgs. Find if Chef can take all the three bags on the flight. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each testcase contains a single line of input, five space separated integers A, B, C, D, E. ------ Output Format ------ For each testcase, output in a single line answer "YES" if Chef can take all the three bags with her or "NO" if she cannot. You may print each character of the string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 36000$ $1 ≤ A, B, C ≤ 10$ $15 ≤ D ≤ 20$ $5 ≤ E ≤ 10$ ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 3 1 1 1 15 5 8 7 6 15 5 8 5 7 15 6 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Test case $1$: Chef can check-in the first and second bag (since $1 + 1 = 2 ≤ 15$) and carry the third bag with her (since $1 ≤ 5$). Test case $2$: None of the three bags can be carried in hand without violating the airport restrictions. Test case $3$: Chef can check-in the first and the third bag (since $8 + 7 ≤ 15$) and carry the second bag with her (since $5 ≤ 6$).
{"inputs": ["3\n1 1 1 15 5\n8 7 6 15 5\n8 5 7 15 6"], "outputs": ["YES\nNO\nYES"]}
505
49
coding
Solve the programming task below in a Python markdown code block. Read problems statements in mandarin chinese, russian and vietnamese as well. Chef wants to prepare a Cook-Off contest with 5 problems chosen from a set of N problems. He has marked the difficulty of each problem within a range identified by one of the following strings: "cakewalk", "simple", "easy", "easy-medium", "medium", "medium-hard", "hard". According to this page, a Cook-Off contest must have problems of the following difficulties: exactly one "cakewalk"; exactly one "simple"; exactly one "easy"; one "easy-medium" or one "medium"; one "medium-hard" or one "hard". Given the information about the N problems, can you advise Chef if it's possible to pick 5 problems and form a valid Cook-Off contest? ------ Input ------ The first line of input contains one integer T denoting the number of test cases. T test cases follow. The first line of each test case contains a single integer N. Then N lines follow; the i-th line contains a single string s_{i}, denoting the difficulty of the i-th problem. ------ Output ------ For each test case, output "Yes" if it's possible to pick 5 problems and form a valid Cook-Off contest; otherwise, output "No". ------ Constraints ------ $1 ≤ T ≤ 100000$ $5 ≤ N ≤ 100000$ $sum of N in all test cases ≤ 600000$ $s_{i} can only be one of the following strings: $"cakewalk"$ $"simple"$ $"easy"$ $"easy-medium"$ $"medium"$ $"medium-hard"$ $"hard"$$ Subtask #1 (11 points): $N = 5$ Subtask #2 (27 points): $5 ≤ N ≤ 15$ $T ≤ 100$ Subtask #3 (62 points): $original constraints$ ----- Sample Input 1 ------ 3 5 easy medium medium-hard simple cakewalk 7 simple simple medium medium easy-medium cakewalk easy 7 cakewalk simple easy easy-medium medium medium-hard hard ----- Sample Output 1 ------ Yes No Yes ----- explanation 1 ------ Example case 2: There are neither problems of "medium-hard" level nor "hard" level, so it's impossible to form a Cook-Off contest.
{"inputs": ["3\n5\neasy\nmedium\nmedium-hard\nsimple\ncakewalk\n7\nsimple\nsimple\nmedium\nmedium\neasy-medium\ncakewalk\neasy\n7\ncakewalk\nsimple\neasy\neasy-medium\nmedium\nmedium-hard\nhard"], "outputs": ["Yes\nNo\nYes"]}
576
78
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time.   Please complete the following python code precisely: ```python class Solution: def minPathSum(self, grid: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(grid = [[1,3,1],[1,5,1],[4,2,1]]) == 7\n assert candidate(grid = [[1,2,3],[4,5,6]]) == 12\n\n\ncheck(Solution().minPathSum)"}
100
71
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. "It's totally fun to play troublemakers ― totally." - Lee Pace Sakshi had a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Initially, all cells of this matrix contained $0$-s. Let's denote a cell in row $r$ and column $c$ by $(r, c)$. Sakshi is well-known for troubling others. This time, her friends Nikki and Mansi planned to take revenge and teach her a lesson, so they changed her matrix by performing the following operation $Q$ times: Choose any valid cell $(x, y)$. Add $1$ to all the cells in row $x$. Add $1$ to all the cells in column $y$. For each valid $i$, the cell chosen in the $i$-th operation was $(X_{i}, Y_{i})$. Nikki and Mansi challenged Sakshi to find the number of cells in the resulting matrix which contain odd integers. Sakshi is not good at math, since she has spent all her life troubling others, so this time, she really needs your help. Help Sakshi find the answer. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains three space-separated integers $N$, $M$ and $Q$. $Q$ lines follow. For each $i$ ($1 ≤ i ≤ Q$), the $i$-th of these lines contains two space-separated integers $X_{i}$ and $Y_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the number of cells with odd values. ------ Constraints ------ $1 ≤ T ≤ 300$ $1 ≤ N, M, Q ≤ 10^{5}$ $1 ≤ X_{i} ≤ N$ for each valid $i$ $1 ≤ Y_{i} ≤ M$ for each valid $i$ the sum of $N$ over all test cases does not exceed $3 \cdot 10^{5}$ the sum of $M$ over all test cases does not exceed $3 \cdot 10^{5}$ the sum of $Q$ over all test cases does not exceed $3 \cdot 10^{5}$ ------ Subtasks ------ Subtask #1 (40 points): $1 ≤ N, M, Q ≤ 300$ Subtask #2 (40 points): $1 ≤ T ≤ 3$ $1 ≤ N \cdot M ≤ 10^{6}$ $1 ≤ Q ≤ 10^{5}$ Subtask #3 (20 points): original constraints ----- Sample Input 1 ------ 1 2 2 3 1 1 1 2 2 1 ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1: After applying the first operation, the matrix becomes: 2 1 1 0 After applying the second operation, it becomes: 3 3 1 1 Finally, after applying the third operation, it becomes: 4 3 3 2
{"inputs": ["1\n2 2 3\n1 1\n1 2\n2 1"], "outputs": ["2"]}
749
30
coding
Solve the programming task below in a Python markdown code block. You are given a directed acyclic graph with n vertices and m edges. There are no self-loops or multiple edges between any pair of vertices. Graph can be disconnected. You should assign labels to all vertices in such a way that: Labels form a valid permutation of length n — an integer sequence such that each integer from 1 to n appears exactly once in it. If there exists an edge from vertex v to vertex u then label_{v} should be smaller than label_{u}. Permutation should be lexicographically smallest among all suitable. Find such sequence of labels to satisfy all the conditions. -----Input----- The first line contains two integer numbers n, m (2 ≤ n ≤ 10^5, 1 ≤ m ≤ 10^5). Next m lines contain two integer numbers v and u (1 ≤ v, u ≤ n, v ≠ u) — edges of the graph. Edges are directed, graph doesn't contain loops or multiple edges. -----Output----- Print n numbers — lexicographically smallest correct permutation of labels of vertices. -----Examples----- Input 3 3 1 2 1 3 3 2 Output 1 3 2 Input 4 5 3 1 4 1 2 3 3 4 2 4 Output 4 1 2 3 Input 5 4 3 1 2 1 2 3 4 5 Output 3 1 2 4 5
{"inputs": ["2 1\n2 1\n", "2 1\n2 1\n", "3 3\n1 2\n1 3\n3 2\n", "3 3\n1 2\n1 3\n3 2\n", "5 4\n3 1\n2 1\n2 3\n4 5\n", "5 4\n3 1\n2 1\n2 5\n4 5\n", "5 4\n3 1\n2 1\n2 3\n4 5\n", "4 5\n3 1\n4 1\n2 3\n3 4\n2 4\n"], "outputs": ["2 1 \n", "2 1\n", "1 3 2 \n", "1 3 2\n", "3 1 2 4 5 \n", "3 1 2 4 5\n", "3 1 2 4 5\n", "4 1 2 3 \n"]}
333
232
coding
Solve the programming task below in a Python markdown code block. A flea is sitting at one of the n hassocks, arranged in a circle, at the moment. After minute number k the flea jumps through k - 1 hassoсks (clockwise). For example, after the first minute the flea jumps to the neighboring hassock. You should answer: will the flea visit all the hassocks or not. We assume that flea has infinitely much time for this jumping. Input The only line contains single integer: 1 ≤ n ≤ 1000 — number of hassocks. Output Output "YES" if all the hassocks will be visited and "NO" otherwise. Examples Input 1 Output YES Input 3 Output NO
{"inputs": ["7\n", "5\n", "9\n", "8\n", "8\n", "2\n", "6\n", "4\n"], "outputs": ["NO\n", "NO\n", "NO\n", "YES\n", "YES\n", "YES\n", "NO\n", "YES\n"]}
159
70
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an array of meeting time intervals intervals where intervals[i] = [starti, endi], return the minimum number of conference rooms required.   Please complete the following python code precisely: ```python class Solution: def minMeetingRooms(self, intervals: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(intervals = [[0,30],[5,10],[15,20]]) == 2\n assert candidate(intervals = [[7,10],[2,4]]) == 1\n\n\ncheck(Solution().minMeetingRooms)"}
79
67
coding
Solve the programming task below in a Python markdown code block. Consider a linear function f(x) = Ax + B. Let's define g^{(0)}(x) = x and g^{(}n)(x) = f(g^{(}n - 1)(x)) for n > 0. For the given integer values A, B, n and x find the value of g^{(}n)(x) modulo 10^9 + 7. -----Input----- The only line contains four integers A, B, n and x (1 ≤ A, B, x ≤ 10^9, 1 ≤ n ≤ 10^18) — the parameters from the problem statement. Note that the given value n can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type. -----Output----- Print the only integer s — the value g^{(}n)(x) modulo 10^9 + 7. -----Examples----- Input 3 4 1 1 Output 7 Input 3 4 2 1 Output 25 Input 3 4 3 1 Output 79
{"inputs": ["3 4 1 1\n", "3 4 2 1\n", "3 4 3 1\n", "1 1 1 1\n", "1 1 1 1\n", "3 3 2 1\n", "3 5 2 1\n", "3 4 4 1\n"], "outputs": ["7\n", "25\n", "79\n", "2\n", "2\n", "21\n", "29\n", "241\n"]}
272
124
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Bengali], [Russian], [Mandarin] and [Vietnamese] as well. Naveej is from a tribe that speaks some weird language - their alphabet consists of N distinct characters. He has an array A = [A_{1}, A_{2}, \dots, A_{N}], where A_{i} denotes the number of occurrences of the i-th character with him. He wants to make a [palindromic string] using all the characters he has (every character he has must be used in this string). In order to make this possible, he can perform the following operation: Select an i (1 ≤ i ≤ N) and convert all occurrences of i-th alphabet to any other alphabet of his choice. Note that Naveej just wants to be able to make any palindrome, as long as every character is used. For example, if N = 2 and A = [2, 2] and we consider the characters to be a and b, he can make both abba and baab, but aba is not allowed because it uses only 3 characters. Find the minimum number of operations required such that Naveej can make a palindromic string using all the characters he has. It can be proven that there always exists at least one sequence of operations allowing for the formation of a palindrome. ------ Input Format ------ - The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N - the size of the alphabet. - The second line contains N space-separated integers: A_{1}, A_{2}, ..., A_{N}, where A_{i} is the number of occurrences of the i-th character with Naveej. ------ Output Format ------ For each test case, output a single line containing one integer - the minimum number of operations required so that Naveej can make a palindromic string using all the characters he has. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (100 points): Original constraints ----- Sample Input 1 ------ 2 1 4 3 4 3 1 ----- Sample Output 1 ------ 0 1 ----- explanation 1 ------ - In the first test case, $N = 1$. Let the character be $a$. We can make the following palindromic string: $aaaa$. - In the second test case, $N = 3$. Let the characters be $a$, $b$, $c$. It is initially not possible to make a palindrome with the given occurrences of the characters. We perform 1 operation: Convert all the occurrences of $b$ to $c$. Then, we can make the following palindromic string: $acaccaca$.
{"inputs": ["2\n1\n4\n3\n4 3 1\n"], "outputs": ["0\n1\n"]}
679
28
coding
Solve the programming task below in a Python markdown code block. One very important person has a piece of paper in the form of a rectangle a × b. Also, he has n seals. Each seal leaves an impression on the paper in the form of a rectangle of the size x_{i} × y_{i}. Each impression must be parallel to the sides of the piece of paper (but seal can be rotated by 90 degrees). A very important person wants to choose two different seals and put them two impressions. Each of the selected seals puts exactly one impression. Impressions should not overlap (but they can touch sides), and the total area occupied by them should be the largest possible. What is the largest area that can be occupied by two seals? -----Input----- The first line contains three integer numbers n, a and b (1 ≤ n, a, b ≤ 100). Each of the next n lines contain two numbers x_{i}, y_{i} (1 ≤ x_{i}, y_{i} ≤ 100). -----Output----- Print the largest total area that can be occupied by two seals. If you can not select two seals, print 0. -----Examples----- Input 2 2 2 1 2 2 1 Output 4 Input 4 10 9 2 3 1 1 5 10 9 11 Output 56 Input 3 10 10 6 6 7 7 20 5 Output 0 -----Note----- In the first example you can rotate the second seal by 90 degrees. Then put impression of it right under the impression of the first seal. This will occupy all the piece of paper. In the second example you can't choose the last seal because it doesn't fit. By choosing the first and the third seals you occupy the largest area. In the third example there is no such pair of seals that they both can fit on a piece of paper.
{"inputs": ["2 2 2\n1 2\n2 1\n", "2 1 1\n1 1\n1 1\n", "2 1 2\n1 1\n1 1\n", "1 100 50\n4 20\n", "2 2 4\n3 1\n2 2\n", "2 2 4\n2 3\n2 1\n", "2 4 2\n1 2\n2 3\n", "2 1 4\n1 2\n1 2\n"], "outputs": ["4\n", "0\n", "2\n", "0\n", "0\n", "8\n", "8\n", "4\n"]}
422
166
coding
Solve the programming task below in a Python markdown code block. Let's introduce some definitions that will be needed later. Let $prime(x)$ be the set of prime divisors of $x$. For example, $prime(140) = \{ 2, 5, 7 \}$, $prime(169) = \{ 13 \}$. Let $g(x, p)$ be the maximum possible integer $p^k$ where $k$ is an integer such that $x$ is divisible by $p^k$. For example: $g(45, 3) = 9$ ($45$ is divisible by $3^2=9$ but not divisible by $3^3=27$), $g(63, 7) = 7$ ($63$ is divisible by $7^1=7$ but not divisible by $7^2=49$). Let $f(x, y)$ be the product of $g(y, p)$ for all $p$ in $prime(x)$. For example: $f(30, 70) = g(70, 2) \cdot g(70, 3) \cdot g(70, 5) = 2^1 \cdot 3^0 \cdot 5^1 = 10$, $f(525, 63) = g(63, 3) \cdot g(63, 5) \cdot g(63, 7) = 3^2 \cdot 5^0 \cdot 7^1 = 63$. You have integers $x$ and $n$. Calculate $f(x, 1) \cdot f(x, 2) \cdot \ldots \cdot f(x, n) \bmod{(10^{9} + 7)}$. -----Input----- The only line contains integers $x$ and $n$ ($2 \le x \le 10^{9}$, $1 \le n \le 10^{18}$) — the numbers used in formula. -----Output----- Print the answer. -----Examples----- Input 10 2 Output 2 Input 20190929 1605 Output 363165664 Input 947 987654321987654321 Output 593574252 -----Note----- In the first example, $f(10, 1) = g(1, 2) \cdot g(1, 5) = 1$, $f(10, 2) = g(2, 2) \cdot g(2, 5) = 2$. In the second example, actual value of formula is approximately $1.597 \cdot 10^{171}$. Make sure you print the answer modulo $(10^{9} + 7)$. In the third example, be careful about overflow issue.
{"inputs": ["2 1\n", "2 1\n", "10 2\n", "16 2\n", "15 2\n", "10 2\n", "9 188\n", "9 188\n"], "outputs": ["1\n", "1\n", "2\n", "2\n", "1\n", "2\n", "954137859\n", "954137859\n"]}
677
110
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a string s and an integer k. You can choose one of the first k letters of s and append it at the end of the string. Return the lexicographically smallest string you could have after applying the mentioned step any number of moves.   Please complete the following python code precisely: ```python class Solution: def orderlyQueue(self, s: str, k: int) -> str: ```
{"functional": "def check(candidate):\n assert candidate(s = \"cba\", k = 1) == \"acb\"\n assert candidate(s = \"baaca\", k = 3) == \"aaabc\"\n\n\ncheck(Solution().orderlyQueue)"}
103
58
coding
Solve the programming task below in a Python markdown code block. Count the pairs of length-N sequences consisting of integers between 1 and M (inclusive), A_1, A_2, \cdots, A_{N} and B_1, B_2, \cdots, B_{N}, that satisfy all of the following conditions: - A_i \neq B_i, for every i such that 1\leq i\leq N. - A_i \neq A_j and B_i \neq B_j, for every (i, j) such that 1\leq i < j\leq N. Since the count can be enormous, print it modulo (10^9+7). -----Constraints----- - 1\leq N \leq M \leq 5\times10^5 - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: N M -----Output----- Print the count modulo (10^9+7). -----Sample Input----- 2 2 -----Sample Output----- 2 A_1=1,A_2=2,B_1=2,B_2=1 and A_1=2,A_2=1,B_1=1,B_2=2 satisfy the conditions.
{"inputs": ["1 2", "2 6", "0 2", "2 4", "1 4", "3 4", "1 1", "1 6"], "outputs": ["2\n", "630\n", "1\n", "84\n", "12\n", "264\n", "0\n", "30\n"]}
279
85
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 are also in arr1. Sort the elements of arr1 such that the relative ordering of items in arr1 are the same as in arr2. Elements that do not appear in arr2 should be placed at the end of arr1 in ascending order.   Please complete the following python code precisely: ```python class Solution: def relativeSortArray(self, arr1: List[int], arr2: List[int]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6]) == [2,2,2,1,4,3,3,9,6,7,19]\n assert candidate(arr1 = [28,6,22,8,44,17], arr2 = [22,28,8,6]) == [22,28,8,6,17,44]\n\n\ncheck(Solution().relativeSortArray)"}
131
148
coding
Solve the programming task below in a Python markdown code block. Smith wakes up at the side of a dirty, disused bathroom, his ankle chained to pipes. Next to him is tape-player with a hand-written message "Play Me". He finds a tape in his own back pocket. After putting the tape in the tape-player, he sees a key hanging from a ceiling, chained to some kind of a machine, which is connected to the terminal next to him. After pressing a Play button a rough voice starts playing from the tape: "Listen up Smith. As you can see, you are in pretty tough situation and in order to escape, you have to solve a puzzle. You are given N strings which represent words. Each word is of the maximum length L and consists of characters 'a'-'e'. You are also given M strings which represent patterns. Pattern is a string of length ≤ L and consists of characters 'a'-'e' as well as the maximum 3 characters '?'. Character '?' is an unknown character, meaning it can be equal to any character 'a'-'e', or even an empty character. For each pattern find the number of words that matches with the given pattern. After solving it and typing the result in the terminal, the key will drop from the ceiling and you may escape. Let the game begin." Help Smith escape. Input The first line of input contains two integers N and M (1 ≤ N ≤ 100 000, 1 ≤ M ≤ 5000), representing the number of words and patterns respectively. The next N lines represent each word, and after those N lines, following M lines represent each pattern. Each word and each pattern has a maximum length L (1 ≤ L ≤ 50). Each pattern has no more that three characters '?'. All other characters in words and patters are lowercase English letters from 'a' to 'e'. Output Output contains M lines and each line consists of one integer, representing the number of words that match the corresponding pattern. Example Input 3 1 abc aec ac a?c Output 3 Note If we switch '?' with 'b', 'e' and with empty character, we get 'abc', 'aec' and 'ac' respectively.
{"inputs": ["3 1\nabc\naec\nbc\na?c\n", "3 1\nabc\naec\nbc\nac?\n", "3 1\nacb\naec\nbb\nac?\n", "3 1\nabc\naec\nbb\nac?\n", "1 1\nacb\naec\nbb\nac?\n", "1 1\nacb\naec\nbc\nac?\n", "1 1\nacb\naec\nbc\na?c\n", "3 1\nabc\naec\nac\nc?a\n"], "outputs": ["2\n", "0\n", "1\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
474
160
coding
Solve the programming task below in a Python markdown code block. The Smart Beaver from ABBYY came up with another splendid problem for the ABBYY Cup participants! This time the Beaver invites the contest participants to check out a problem on sorting documents by their subjects. Let's describe the problem: You've got some training set of documents. For each document you know its subject. The subject in this problem is an integer from 1 to 3. Each of these numbers has a physical meaning. For instance, all documents with subject 3 are about trade. You can download the training set of documents at the following link: http://download4.abbyy.com/a2/X2RZ2ZWXBG5VYWAL61H76ZQM/train.zip. The archive contains three directories with names "1", "2", "3". Directory named "1" contains documents on the 1-st subject, directory "2" contains documents on the 2-nd subject, and directory "3" contains documents on the 3-rd subject. Each document corresponds to exactly one file from some directory. All documents have the following format: the first line contains the document identifier, the second line contains the name of the document, all subsequent lines contain the text of the document. The document identifier is used to make installing the problem more convenient and has no useful information for the participants. You need to write a program that should indicate the subject for a given document. It is guaranteed that all documents given as input to your program correspond to one of the three subjects of the training set. Input The first line contains integer id (0 ≤ id ≤ 106) — the document identifier. The second line contains the name of the document. The third and the subsequent lines contain the text of the document. It is guaranteed that the size of any given document will not exceed 10 kilobytes. The tests for this problem are divided into 10 groups. Documents of groups 1 and 2 are taken from the training set, but their identifiers will not match the identifiers specified in the training set. Groups from the 3-rd to the 10-th are roughly sorted by the author in ascending order of difficulty (these groups contain documents which aren't present in the training set). Output Print an integer from 1 to 3, inclusive — the number of the subject the given document corresponds to. Examples
{"inputs": ["21000\nSWISS TO LAUNCH NEW SERIES OF MONEY MARKET PAPER\nZURICH, April 9 - The Swiss Federal Government will launch\na new series of three month money market certificates totalling\naround 150 mln Swiss francs, the National Bank said.\nSubscriptions close April 14 and payment date is April 16.\nThe last series of three month paper issued in March raised\n147.3 mln francs at an issue price of 99.142 pct, giving an\naverage annual yield of 3.501 pct.\n", "21000\nSWISS TO LAUNCH NEW SERIES OF MONEY MARKET PAPER\nZURICH, April 9 - The Swiss Federal Government will launch\na new series of three month money market certificates totalling\naround 150 mln Swiss francs, the National Bank said.\nSubscriptions close April 14 and payment date is April 16.\nThe last series of three month paper issued in March raised\n147.3 mln francs at an issue price of 99.142 pct, giving an\naverage annual dleiy of 3.501 pct.\n", "21000\nSWISS TO LAUNCH NEW SERIES OF MONEY MARKET PAPER\nZURICH, April 9 - The Swiss Federal tnemnrevoG will launch\na new series of three month money market certificates totalling\naround 150 mln Swiss francs, the National Bank said.\nSubscriptions close April 14 and payment date is April 16.\nThe last series of three month paper issued in March raised\n147.3 mln francs at an issue price of 99.142 pct, giving an\naverage annual dleiy of 3.501 pct.\n", "21000\nSWISS TO LAUNCH NEW SERIES OF MONEY MARKET PAPER\nZURICH, April 9 - The Sviss Federal tnemnrevoG will launch\na new series of three month money market certificates totalling\naround 150 mln Swiss francs, the National Bank said.\nSubscriptions close April 14 and payment date is April 16.\nThe last series of three month paper issued in March raised\n147.3 mln francs at an issue price of 99.142 pct, giving an\naverage annual dleiy of 3.501 pct.\n", "21000\nSWISS TO LAUNCH NEW SERIES OF MONEY MARKET PAPER\nZURICH, April 9 - The Sviss Federal tnemnrevoG will launch\na new series of three month money market certificates totalling\naround 150 mln Swiss fraocs, the National Bank said.\nSubscriptions close April 14 and payment date is April 16.\nThe last series of three month paper issued in March raised\n147.3 mln francs at an issue price of 99.142 pct, giving an\naverage annual dleiy of 3.501 pct.\n", "21000\nSWISS TO LAHNCU NEW SERIES OF MONEY MARKET PAPER\nZURICH, April 9 - The Sviss Federal tnemnrevoG will launch\na new series of three month money market certificates totalling\naround 150 mln Swiss fraocs, the National Bank said.\nSubscriptions close April 14 and payment date is April 16.\nThe last series of three month paper issued in March raised\n147.3 mln francs at an issue price of 99.142 pct, giving an\naverage annual dleiy of 3.501 pct.\n", "21000\nSWISS TO LAHNCU NEW SERIES OF MONEY MARKET PAPER\nZURICH, April 9 - The Sviss Federal tnemnrevoG will launch\na new series of three month money market certificates totalling\naround 150 mln Swiss fraocs, the National Bank said.\nSubscriptions close April 14 and payment date is April 16.\nThe last sdries of three month paper issued in March raised\n147.3 mln francs at an issue price of 99.142 pct, giving an\naverage annual dleiy of 3.501 pct.\n", "21000\nSWISS TO LAHNCU NEW SERIES OF MONEY MARKET PAPER\nZURICH, April 9 - The Sviss Federal tnemnrevoG will launch\na new series of three month money market certificates totalling\naround 150 mln Swiss fraocs, the National Bank said.\nSubscriptions close April 14 and payment date is April 16.\nThe last sdries of three month paper issued io March raised\n147.3 mln francs at an issue price of 99.142 pct, giving an\naverage annual dleiy of 3.501 pct.\n"], "outputs": ["1", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n"]}
497
1,158
coding
Solve the programming task below in a Python markdown code block. There are a lot of things which could be cut — trees, paper, "the rope". In this problem you are going to cut a sequence of integers. There is a sequence of integers, which contains the equal number of even and odd numbers. Given a limited budget, you need to make maximum possible number of cuts such that each resulting segment will have the same number of odd and even integers. Cuts separate a sequence to continuous (contiguous) segments. You may think about each cut as a break between two adjacent elements in a sequence. So after cutting each element belongs to exactly one segment. Say, $[4, 1, 2, 3, 4, 5, 4, 4, 5, 5]$ $\to$ two cuts $\to$ $[4, 1 | 2, 3, 4, 5 | 4, 4, 5, 5]$. On each segment the number of even elements should be equal to the number of odd elements. The cost of the cut between $x$ and $y$ numbers is $|x - y|$ bitcoins. Find the maximum possible number of cuts that can be made while spending no more than $B$ bitcoins. -----Input----- First line of the input contains an integer $n$ ($2 \le n \le 100$) and an integer $B$ ($1 \le B \le 100$) — the number of elements in the sequence and the number of bitcoins you have. Second line contains $n$ integers: $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 100$) — elements of the sequence, which contains the equal number of even and odd numbers -----Output----- Print the maximum possible number of cuts which can be made while spending no more than $B$ bitcoins. -----Examples----- Input 6 4 1 2 5 10 15 20 Output 1 Input 4 10 1 3 2 4 Output 0 Input 6 100 1 2 3 4 5 6 Output 2 -----Note----- In the first sample the optimal answer is to split sequence between $2$ and $5$. Price of this cut is equal to $3$ bitcoins. In the second sample it is not possible to make even one cut even with unlimited number of bitcoins. In the third sample the sequence should be cut between $2$ and $3$, and between $4$ and $5$. The total price of the cuts is $1 + 1 = 2$ bitcoins.
{"inputs": ["2 100\n13 78\n", "4 1\n1 2 3 4\n", "4 1\n1 2 1 2\n", "4 4\n1 2 6 7\n", "4 1\n1 2 3 4\n", "4 4\n1 2 6 7\n", "4 1\n1 2 1 2\n", "2 100\n13 78\n"], "outputs": ["0\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "0\n"]}
581
150
coding
Solve the programming task below in a Python markdown code block. Your search for Heidi is over – you finally found her at a library, dressed up as a human. In fact, she has spent so much time there that she now runs the place! Her job is to buy books and keep them at the library so that people can borrow and read them. There are n different books, numbered 1 through n. We will look at the library's operation during n consecutive days. Heidi knows in advance that on the i-th day (1 ≤ i ≤ n) precisely one person will come to the library, request to borrow the book a_{i}, read it in a few hours, and return the book later on the same day. Heidi desperately wants to please all her guests, so she will make sure to always have the book a_{i} available in the library on the i-th day. During the night before the i-th day, she has the option of going to the bookstore (which operates at nights to avoid competition with the library) and buying any book for the price of 1 CHF. Of course, if she already has a book at the library, she does not need to buy it again. Initially, the library contains no books. There is a problem, though. The capacity of the library is k – this means that at any time, there can be at most k books at the library. If buying a new book would cause Heidi to have more than k books, she must first get rid of some book that she already has, in order to make room for the new book. If she later needs a book that she got rid of, she will need to buy that book again. You are given k and the sequence of requests for books a_1, a_2, ..., a_{n}. What is the minimum cost (in CHF) of buying new books to satisfy all the requests? -----Input----- The first line of input will contain two integers n and k (1 ≤ n, k ≤ 80). The second line will contain n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ n) – the sequence of book requests. -----Output----- On a single line print the minimum cost of buying books at the store so as to satisfy all requests. -----Examples----- Input 4 80 1 2 2 1 Output 2 Input 4 1 1 2 2 1 Output 3 Input 4 2 1 2 3 1 Output 3 -----Note----- In the first test case, Heidi is able to keep all books forever. Therefore, she only needs to buy the book 1 before the first day and the book 2 before the second day. In the second test case, she can only keep one book at a time. Therefore she will need to buy new books on the first, second and fourth day. In the third test case, before buying book 3 on the third day, she must decide which of the books 1 and 2 she should get rid of. Of course, she should keep the book 1, which will be requested on the fourth day.
{"inputs": ["1 1\n1\n", "1 1\n1\n", "4 1\n1 2 2 1\n", "4 2\n1 2 3 1\n", "4 2\n1 2 3 2\n", "4 2\n1 2 3 2\n", "4 2\n1 2 3 1\n", "4 1\n1 2 2 1\n"], "outputs": ["1\n", "1\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n"]}
667
138
coding
Solve the programming task below in a Python markdown code block. Chef Palin, as his name suggests, is always very interested in palindromic strings. Recently, he made a pretty interesting discovery on palindromes and that made him feel really Lucky. He came across something known as Lucky Palindromes. He defines a string as being a lucky palindrome if it is a palindrome containing the string "lucky" as a substring. As always, now he wants to turn every string he comes across into a lucky palindrome. Being a chef, he is a man of patience and creativity, so he knows the operation of replacing any character of the string with any other character very well and he can perform this action infinitely many times. He wants you to write a program that can help him convert a given string to a lucky palindrome using the minimum number of operations and if several such lucky palindromes are possible, then output the lexicographically smallest one. ------ Input Format ------ The first line contains a single integer T ≤ 100 the number of testcases. The following T lines each contain a string of length ≤ 1000 and only containing characters 'a'-'z'. ------ Output Format ------ For each line of testcase, your program should output on a single line, the required lucky palindrome along with the minimum number of operations, both separated by a single space. If there is no lucky palindrome possible, then just output "unlucky" in a single line. ----- Sample Input 1 ------ 3 laubcdkey luckycodechef aaaaaaaa ----- Sample Output 1 ------ luckykcul 8 luckycocykcul 6 unlucky
{"inputs": ["3\nlaubcdkey\nluckycodechef\naaaaaaaa", "3\nlaubcdkey\nlubkycodechef\naaaaaaaa", "3\nlaubcdkey\nyubklcodechef\naaaaaaaa", "3\nlatbcdkfy\nfeicedocljbux\naaaaaaba", "3\nytkdadfbl\ntobjlcedechwf\naabaaaba", "3\nytkdadfbl\ntobjlcfdechwf\naabaaaba", "3\nytkdadlbf\ntobjlcfdechwf\naabaaaca", "3\nytjdadlbf\nfwmceefchjtnb\nb`aaaada"], "outputs": ["luckykcul 8\nluckycocykcul 6\nunlucky", "luckykcul 8\nluckycocykcul 7\nunlucky\n", "luckykcul 8\nluckycocykcul 9\nunlucky\n", "luckykcul 8\nfluckyoykculf 10\nunlucky\n", "luckykcul 8\nfluckyeykculf 10\nunlucky\n", "luckykcul 8\nfluckyfykculf 10\nunlucky\n", "ykculucky 8\nfluckyfykculf 10\nunlucky\n", "ykculucky 8\nbluckyfykculb 10\nunlucky\n"]}
351
331
coding
Solve the programming task below in a Python markdown code block. You are given a rooted tree consisting of $n$ vertices numbered from $1$ to $n$. The root is vertex $1$. There is also a string $s$ denoting the color of each vertex: if $s_i = {B}$, then vertex $i$ is black, and if $s_i = {W}$, then vertex $i$ is white. A subtree of the tree is called balanced if the number of white vertices equals the number of black vertices. Count the number of balanced subtrees. A tree is a connected undirected graph without cycles. A rooted tree is a tree with a selected vertex, which is called the root. In this problem, all trees have root $1$. The tree is specified by an array of parents $a_2, \dots, a_n$ containing $n-1$ numbers: $a_i$ is the parent of the vertex with the number $i$ for all $i = 2, \dots, n$. The parent of a vertex $u$ is a vertex that is the next vertex on a simple path from $u$ to the root. The subtree of a vertex $u$ is the set of all vertices that pass through $u$ on a simple path to the root. For example, in the picture below, $7$ is in the subtree of $3$ because the simple path $7 \to 5 \to 3 \to 1$ passes through $3$. Note that a vertex is included in its subtree, and the subtree of the root is the entire tree. The picture shows the tree for $n=7$, $a=[1,1,2,3,3,5]$, and $s={WBBWWBW}$. The subtree at the vertex $3$ is balanced. -----Input----- The first line of input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains an integer $n$ ($2 \le n \le 4000$) — the number of vertices in the tree. The second line of each test case contains $n-1$ integers $a_2, \dots, a_n$ ($1 \le a_i < i$) — the parents of the vertices $2, \dots, n$. The third line of each test case contains a string $s$ of length $n$ consisting of the characters ${B}$ and ${W}$ — the coloring of the tree. It is guaranteed that the sum of the values $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer — the number of balanced subtrees. -----Examples----- Input 3 7 1 1 2 3 3 5 WBBWWBW 2 1 BW 8 1 2 3 4 5 6 7 BWBWBWBW Output 2 1 4 -----Note----- The first test case is pictured in the statement. Only the subtrees at vertices $2$ and $3$ are balanced. In the second test case, only the subtree at vertex $1$ is balanced. In the third test case, only the subtrees at vertices $1$, $3$, $5$, and $7$ are balanced.
{"inputs": ["3\n7\n1 1 2 3 3 5\nWBBWWBW\n2\n1\nBW\n8\n1 2 3 4 5 6 7\nBWBWBWBW\n"], "outputs": ["2\n1\n4\n"]}
729
65
coding
Solve the programming task below in a Python markdown code block. Santa Claus has n candies, he dreams to give them as gifts to children. What is the maximal number of children for whose he can give candies if Santa Claus want each kid should get distinct positive integer number of candies. Santa Class wants to give all n candies he has. -----Input----- The only line contains positive integer number n (1 ≤ n ≤ 1000) — number of candies Santa Claus has. -----Output----- Print to the first line integer number k — maximal number of kids which can get candies. Print to the second line k distinct integer numbers: number of candies for each of k kid. The sum of k printed numbers should be exactly n. If there are many solutions, print any of them. -----Examples----- Input 5 Output 2 2 3 Input 9 Output 3 3 5 1 Input 2 Output 1 2
{"inputs": ["5\n", "9\n", "2\n", "1\n", "3\n", "4\n", "6\n", "7\n"], "outputs": ["2\n1 4 \n", "3\n1 2 6 \n", "1\n2 \n", "1\n1 \n", "2\n1 2 \n", "2\n1 3 \n", "3\n1 2 3 \n", "3\n1 2 4 \n"]}
198
112
coding
Solve the programming task below in a Python markdown code block. Niwango has N cards, numbered 1,2,\ldots,N. He will now arrange these cards in a row. Niwango wants to know if there is a way to arrange the cards while satisfying all the N conditions below. To help him, determine whether such a way exists. If the answer is yes, also find the lexicographically smallest such arrangement. * To the immediate right of Card 1 (if any) is NOT Card a_1. * To the immediate right of Card 2 (if any) is NOT Card a_2. * \vdots * To the immediate right of Card N (if any) is NOT Card a_N. Constraints * 2 \leq N \leq 10^{5} * 1 \leq a_i \leq N * a_i \neq i Input Input is given from Standard Input in the following format: N a_1 a_2 \ldots a_N Output If no arrangements satisfy the conditions, print `-1`. If such arrangements exist, print the lexicographically smallest such arrangement, in the following format: b_1 b_2 \ldots b_N Here, b_i represents the i-th card from the left. Examples Input 4 2 3 4 1 Output 1 3 2 4 Input 2 2 1 Output -1 Input 13 2 3 4 5 6 7 8 9 10 11 12 13 12 Output 1 3 2 4 6 5 7 9 8 10 12 11 13
{"inputs": ["2\n2 1", "4\n2 3 4 0", "4\n4 3 4 1", "4\n2 3 2 1", "4\n3 5 2 1", "4\n2 3 5 0", "4\n2 3 4 1", "13\n1 3 3 0 9 4 9 2 4 6 12 8 8"], "outputs": ["-1", "1 3 2 4\n", "1 2 4 3\n", "1 3 4 2\n", "1 2 3 4\n", "1 3 2 4\n", "1 3 2 4", "1 2 4 3 5 6 7 8 9 10 11 13 12\n"]}
380
205
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a 0-indexed integer array nums of size n and a positive integer k. We call an index i in the range k <= i < n - k good if the following conditions are satisfied: The k elements that are just before the index i are in non-increasing order. The k elements that are just after the index i are in non-decreasing order. Return an array of all good indices sorted in increasing order.   Please complete the following python code precisely: ```python class Solution: def goodIndices(self, nums: List[int], k: int) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(nums = [2,1,1,1,3,4,1], k = 2) == [2,3]\n assert candidate(nums = [2,1,1,2], k = 2) == []\n\n\ncheck(Solution().goodIndices)"}
142
74
coding
Solve the programming task below in a Python markdown code block. Let's consider a rectangular table R consisting of N rows and M columns. Rows are enumerated from 1 to N from top to bottom. Columns are enumerated from 1 to M from left to right. Each element of R is a non-negative integer. R is called steady if the sum of elements in the ith row is not less then the sum of elements in the (i-1)th row for each i where 2 ≤ i ≤ N and the sum of elements in the Nth row is less than or equal to M. Your task is to find the number of different steady tables of size N x M modulo 1 000 000 000. -----Input----- The first line of input contains a single integer T denoting number of test cases. First and the only line of each test case contains two space separated integers N and M denoting the number of rows and columns respectively. -----Output----- For each test case, print a single integer corresponding to the answer. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N, M ≤ 2000 -----Subtasks----- - Subtask 1 : 1 ≤ T ≤ 10 , 1 ≤ N,M ≤ 50 : ( 23 pts ) - Subtask 2 : 1 ≤ T ≤ 10 , 1 ≤ N,M ≤ 500 : ( 29 pts ) - Subtask 3 : 1 ≤ T ≤ 10 , 1 ≤ N,M ≤ 2000 : ( 48 pts ) -----Example----- Input: 3 1 1 2 2 2 3 Output: 2 25 273 -----Explanation----- Test case 1 : There are only 2 such grids possible 0 and 1.
{"inputs": ["3\n1 1\n2 2\n2 3"], "outputs": ["2\n25\n273"]}
399
31
coding
Solve the programming task below in a Python markdown code block. Nicholas has an array a that contains n distinct integers from 1 to n. In other words, Nicholas has a permutation of size n. Nicholas want the minimum element (integer 1) and the maximum element (integer n) to be as far as possible from each other. He wants to perform exactly one swap in order to maximize the distance between the minimum and the maximum elements. The distance between two elements is considered to be equal to the absolute difference between their positions. -----Input----- The first line of the input contains a single integer n (2 ≤ n ≤ 100) — the size of the permutation. The second line of the input contains n distinct integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ n), where a_{i} is equal to the element at the i-th position. -----Output----- Print a single integer — the maximum possible distance between the minimum and the maximum elements Nicholas can achieve by performing exactly one swap. -----Examples----- Input 5 4 5 1 3 2 Output 3 Input 7 1 6 5 3 4 7 2 Output 6 Input 6 6 5 4 3 2 1 Output 5 -----Note----- In the first sample, one may obtain the optimal answer by swapping elements 1 and 2. In the second sample, the minimum and the maximum elements will be located in the opposite ends of the array if we swap 7 and 2. In the third sample, the distance between the minimum and the maximum elements is already maximum possible, so we just perform some unnecessary swap, for example, one can swap 5 and 2.
{"inputs": ["2\n1 2\n", "2\n2 1\n", "2\n1 2\n", "2\n2 1\n", "3\n2 3 1\n", "3\n1 2 3\n", "3\n1 3 2\n", "3\n2 1 3\n"], "outputs": ["1\n", "1\n", "1\n", "1\n", "2\n", "2\n", "2\n", "2\n"]}
375
110
coding
Solve the programming task below in a Python markdown code block. What is an anagram? Well, two words are anagrams of each other if they both contain the same letters. For example: ``` 'abba' & 'baab' == true 'abba' & 'bbaa' == true 'abba' & 'abbba' == false 'abba' & 'abca' == false ``` Write a function that will find all the anagrams of a word from a list. You will be given two inputs a word and an array with words. You should return an array of all the anagrams or an empty array if there are none. For example: anagrams('abba', ['aabb', 'abcd', 'bbaa', 'dada']) => ['aabb', 'bbaa'] anagrams('racer', ['crazer', 'carer', 'racar', 'caers', 'racer']) => ['carer', 'racer'] anagrams('laser', ['lazing', 'lazy', 'lacer']) => [] Also feel free to reuse/extend the following starter code: ```python def anagrams(word, words): ```
{"functional": "_inputs = [['abba', ['aabb', 'abcd', 'bbaa', 'dada']], ['racer', ['crazer', 'carer', 'racar', 'caers', 'racer']], ['a', ['a', 'b', 'c', 'd']], ['ab', ['cc', 'ac', 'bc', 'cd', 'ab', 'ba', 'racar', 'caers', 'racer']], ['abba', ['a', 'b', 'c', 'd', 'aabb', 'bbaa', 'abab', 'baba', 'baab', 'abcd', 'abbba', 'baaab', 'abbab', 'abbaa', 'babaa']], ['big', ['gig', 'dib', 'bid', 'biig']]]\n_outputs = [[['aabb', 'bbaa']], [['carer', 'racer']], [['a']], [['ab', 'ba']], [['aabb', 'bbaa', 'abab', 'baba', 'baab']], [[]]]\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(anagrams(*i), o[0])"}
257
371
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 of size n. You are asked to solve n queries for each integer i in the range 0 <= i < n. To solve the ith query: Find the minimum value in each possible subarray of size i + 1 of the array nums. Find the maximum of those minimum values. This maximum is the answer to the query. Return a 0-indexed integer array ans of size n such that ans[i] is the answer to the ith query. A subarray is a contiguous sequence of elements in an array.   Please complete the following python code precisely: ```python class Solution: def findMaximums(self, nums: List[int]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(nums = [0,1,2,4]) == [4,2,1,0]\n assert candidate(nums = [10,20,50,10]) == [50,20,10,10]\n\n\ncheck(Solution().findMaximums)"}
163
78
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given two integer arrays nums1 and nums2 of lengths m and n respectively. nums1 and nums2 represent the digits of two numbers. You are also given an integer k. Create the maximum number of length k <= m + n from digits of the two numbers. The relative order of the digits from the same array must be preserved. Return an array of the k digits representing the answer.   Please complete the following python code precisely: ```python class Solution: def maxNumber(self, nums1: List[int], nums2: List[int], k: int) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(nums1 = [3,4,6,5], nums2 = [9,1,2,5,8,3], k = 5) == [9,8,6,5,3]\n assert candidate(nums1 = [6,7], nums2 = [6,0,4], k = 5) == [6,7,6,0,4]\n assert candidate(nums1 = [3,9], nums2 = [8,9], k = 3) == [9,8,9]\n\n\ncheck(Solution().maxNumber)"}
141
140
coding
Solve the programming task below in a Python markdown code block. Maki is a house cat. One day she fortunately came at a wonderful-looking dried fish. Since she felt not hungry on that day, she put it up in her bed. However there was a problem; a rat was living in her house, and he was watching for a chance to steal her food. To secure the fish during the time she is asleep, she decided to build some walls to prevent the rat from reaching her bed. Maki's house is represented as a two-dimensional plane. She has hidden the dried fish at (xt, yt). She knows that the lair of the rat is located at (xs, ys ). She has some candidate locations to build walls. The i-th candidate is described by a circle of radius ri centered at (xi, yi). She can build walls at as many candidate locations as she wants, unless they touch or cross each other. You can assume that the size of the fish, the rat’s lair, and the thickness of walls are all very small and can be ignored. Your task is to write a program which determines the minimum number of walls the rat needs to climb over until he can get to Maki's bed from his lair, assuming that Maki made an optimal choice of walls. Input The input is a sequence of datasets. Each dataset corresponds to a single situation and has the following format: n xs ys xt yt x1 y1 r1 ... xn yn rn n is the number of candidate locations where to build walls (1 ≤ n ≤ 1000). (xs, ys ) and (xt , yt ) denote the coordinates of the rat's lair and Maki's bed, respectively. The i-th candidate location is a circle which has radius ri (1 ≤ ri ≤ 10000) and is centered at (xi, yi) (i = 1, 2, ... , n). All coordinate values are integers between 0 and 10000 (inclusive). All candidate locations are distinct and contain neither the rat's lair nor Maki's bed. The positions of the rat's lair and Maki's bed are also distinct. The input is terminated by a line with "0". This is not part of any dataset and thus should not be processed. Output For each dataset, print a single line that contains the minimum number of walls the rat needs to climb over. Example Input 3 0 0 100 100 60 100 50 100 100 10 80 80 50 4 0 0 100 100 50 50 50 150 50 50 50 150 50 150 150 50 0 Output 2 0
{"inputs": ["3\n0 0 100 101\n77 100 50\n110 010 18\n42 80 50\n4\n1 0 110 110\n8 22 50\n25 50 58\n9 154 65\n806 205 50\n0", "3\n0 1 100 101\n60 100 50\n110 010 18\n42 80 50\n4\n1 0 010 100\n8 21 50\n25 50 58\n50 150 65\n409 205 50\n0", "3\n0 0 100 101\n77 100 90\n110 110 18\n42 80 6\n4\n1 0 110 010\n8 22 50\n25 50 58\n145 150 65\n806 205 50\n0", "3\n0 0 100 101\n77 000 50\n110 010 18\n42 80 50\n4\n1 0 010 110\n8 22 50\n25 50 58\n76 150 65\n806 205 50\n0", "3\n0 1 100 101\n60 100 50\n110 010 18\n42 80 50\n4\n1 0 000 100\n8 21 50\n25 50 58\n50 150 65\n409 205 50\n0", "3\n0 1 100 101\n60 100 50\n110 010 18\n42 80 50\n4\n1 0 010 110\n8 21 50\n25 50 58\n50 150 65\n409 205 50\n0", "3\n0 0 100 101\n60 100 50\n110 010 18\n42 80 50\n4\n1 0 010 110\n8 21 50\n25 50 58\n50 150 65\n409 205 50\n0", "3\n0 0 100 101\n60 100 50\n110 010 18\n42 80 50\n4\n1 0 010 110\n8 21 50\n25 50 58\n76 150 65\n409 205 50\n0"], "outputs": ["1\n1\n", "1\n2\n", "2\n1\n", "0\n1\n", "1\n1\n", "1\n2\n", "1\n2\n", "1\n1\n"]}
616
845
coding
Solve the programming task below in a Python markdown code block. Yuki made a sugoroku so that everyone can play at the children's association event. In this sugoroku, squares are lined up in a ring, and each square has an integer of 1 or more written on it. The player chooses a square as a starting point and places his piece. Advance the pieces clockwise by the number written on the square. Advance the pieces clockwise again by the number written on the stopped square. Repeat this, and when the piece stops on the square selected as the starting point, it is "Agari". In reality, depending on how you choose the squares, it may never be "finished". Yuki is trying to count the number of squares that can reach the "Agari" in this sugoroku. Create a program that inputs sugoroku information and reports the number of squares that can reach "Agari". Input The input is given in the following format. N a1 a2 ... aN The number N (1 ≤ N ≤ 100000) of all the cells contained in the sugoroku is given in the first line. On the second line, the number ai (1 ≤ ai ≤ 109) written in each cell is given in turn clockwise. Output The number of squares that can reach "Agari" is output in one line. Examples Input 3 1 1 1 Output 3 Input 3 1 1 2 Output 2 Input 8 2 3 7 3 3 3 4 4 Output 6
{"inputs": ["3\n2 1 1", "3\n1 1 3", "3\n1 2 3", "3\n2 1 0", "3\n1 1 0", "3\n1 2 1", "3\n2 2 1", "3\n1 2 0"], "outputs": ["2\n", "1\n", "3\n", "1\n", "1\n", "2\n", "2\n", "3\n"]}
342
110
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. In Chefland, the speed of light is $c\ \mathrm{m}/\mathrm{s}$, and acceleration due to gravity is $g\ \mathrm{m}/\mathrm{s}^2$. Find the smallest height (in meters) from which Chef should jump such that during his journey down only under the effect of gravity and independent of any air resistance, he achieves the speed of light and verifies Einstein's theory of special relativity. Assume he jumps at zero velocity and at any time, his velocity ($v$) and depth of descent ($H$) are related as $$v^{2} = 2 \cdot g \cdot H.$$ ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. Each test case contains a single line of input, two integers $g$, $c$. ------ Output ------ For each test case, output in a single line the answer to the problem. We can show that under the constraints, the answer is an integer. ----- Sample Input 1 ------ 3 7 1400 5 1000 10 1000 ----- Sample Output 1 ------ 140000 100000 50000 ----- explanation 1 ------ Test Case $1$: For Chef to achieve the speed of light, the minimum height required is $\frac{c^{2}}{2 \cdot g}$ = $\frac{1400 \cdot 1400}{14}$ = $140000$ meters. Test Case $3$: For Chef to achieve the speed of light, the minimum height required is $\frac{c^{2}}{2 \cdot g}$ = $\frac{1000 \cdot 1000}{20}$ = $50000$ meters.
{"inputs": ["3\n7 1400\n5 1000\n10 1000"], "outputs": ["140000\n100000\n50000"]}
445
52
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a list of folders folder, return the folders after removing all sub-folders in those folders. You may return the answer in any order. If a folder[i] is located within another folder[j], it is called a sub-folder of it. A sub-folder of folder[j] must start with folder[j], followed by a "/". For example, "/a/b" is a sub-folder of "/a", but "/b" is not a sub-folder of "/a/b/c". The format of a path is one or more concatenated strings of the form: '/' followed by one or more lowercase English letters. For example, "/leetcode" and "/leetcode/problems" are valid paths while an empty string and "/" are not.   Please complete the following python code precisely: ```python class Solution: def removeSubfolders(self, folder: List[str]) -> List[str]: ```
{"functional": "def check(candidate):\n assert candidate(folder = [\"/a\",\"/a/b\",\"/c/d\",\"/c/d/e\",\"/c/f\"]) == [\"/a\",\"/c/d\",\"/c/f\"]\n assert candidate(folder = [\"/a\",\"/a/b/c\",\"/a/b/d\"]) == [\"/a\"]\n\n\ncheck(Solution().removeSubfolders)"}
196
93
coding
Solve the programming task below in a Python markdown code block. Ronny the robot is watching someone perform the Cups and Balls magic trick. The magician has one ball and three cups, he shows Ronny which cup he hides the ball under (b), he then mixes all the cups around by performing multiple two-cup switches (arr). Ronny can record the switches but can't work out where the ball is. Write a programme to help him do this. Rules: - There will only ever be three cups. - Only two cups will be swapped at a time. - The cups and their switches will be refered to by their index in a row of three, beginning at one. So [[1,2]] means the cup at position one, is swapped with the cup at position two. - Arr will be an array of integers 1 - 3 organised in pairs. - There won't be any empty sub-arrays. - If arr is just an empty array b should be returned. Examples: (b) = 2, (arr) = [[1,2]] The ball is under cup number : 1 ------- (b) = 1, (arr) = [[2,3],[1,2],[1,2]] The ball is under cup number : 1 ------- (b) = 2, (arr) = [[1,3],[1,2],[2,1],[2,3]] The ball is under cup number : 3 Also feel free to reuse/extend the following starter code: ```python def cup_and_balls(b, arr): ```
{"functional": "_inputs = [[2, [[1, 2]]], [1, [[2, 3], [1, 2], [1, 2]]], [2, [[1, 3], [1, 2], [2, 1], [2, 3]]]]\n_outputs = [[1], [1], [3]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(cup_and_balls(*i), o[0])"}
332
220
coding
Solve the programming task below in a Python markdown code block. For the human eye, primary colours are red, green, and blue. Combining 1 drop each of any two primary colours produces a new type of secondary colour. For example, mixing red and green gives yellow, mixing green and blue gives cyan, and, mixing red and blue gives magenta. You have X, Y, and Z drops of red, green, and blue colours respectively. Find the maximum total number of distinct colours (both primary and secondary) you can have at any particular moment. Note: You cannot mix a secondary colour with a primary or another secondary colour to get a new type of colour. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of three space separated integers X, Y, and Z, the number of drops of red, green, and blue colours respectively. ------ Output Format ------ For each test case, output on a new line the maximum total number of colours (both primary and secondary) you can have using the given primary colours. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $0 ≤ X, Y, Z≤ 100$ ----- Sample Input 1 ------ 4 1 0 1 3 3 0 1 1 1 0 0 0 ----- Sample Output 1 ------ 2 3 3 0 ----- explanation 1 ------ Test case $1$: We have $1$ drop each of red and blue colour. If we mix these colours, we will have magenta but no red or blue. Thus, to maximize the total number of colours, it is better that we keep the red and blue colours as it is. The maximum number of colours we can have is $2$. Test case $2$: We have $3$ drops each of red and green colour. We can use $1$ drop each of red and green to have yellow. We still have $2$ drops each of red and green left. Thus, we have $3$ different types of colours now. Test case $3$: If we mix any two colours, we will loose two colours and get only one colour in return. Thus, it is best to keep all colours as it is and keep $3$ different varieties of colours. Test case $4$: There are no types of colours available.
{"inputs": ["4\n1 0 1\n3 3 0\n1 1 1\n0 0 0\n"], "outputs": ["2\n3\n3\n0"]}
512
43
coding
Solve the programming task below in a Python markdown code block. A sequence of integers is called a wonderful sequence if all the integers in it are positive and it is a strictly increasing sequence. Given a sequence of integers, you have to make it a wonderful sequence. For that you can change any element you want, but you should make as less changes as possible in order to make it a wonderful sequence. Input The first line of input is an integer T(T ≤ 5), the number of test cases. Each test case contains 2 lines. The first line of the test case contains an integer (0 < N ≤ 100000), i.e. the number of elements in the original sequence. The second line contains N positive integers, no larger than 2000000000, which forms the original sequence. Output For each test case output the minimal number of elements you must change in the original sequence to make it a wonderful sequence. SAMPLE INPUT 3 2 1 2 3 3 2 1 5 10 5 6 7 8 SAMPLE OUTPUT 0 2 1 Explanation For the 1st test case you needn't to change any elements. For the 2nd test case you can change 3 into 1 and change 1 into 3. For the 3rd test case you can change 10 into 1.
{"inputs": ["3\n2\n1 2\n3\n3 2 1\n5\n10 5 6 7 8", "4\n2\n1 2\n3\n3 2 1\n5\n10 5 6 7 8\n4\n2 2 2 2"], "outputs": ["0\n2\n1", "0\n2\n1\n3"]}
300
92
coding
Solve the programming task below in a Python markdown code block. Chef is baking delicious cookies today! Since Chef is super hungry, he wants to eat at least $N$ cookies. Since Chef is a messy eater, he drops a lot of crumbs. Crumbs of $B$ cookies can be put together to make a new cookie! Given $N$ and $B$, help Chef find out the minimum number of cookies he must initially bake, $A$, to satisfy his hunger. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of two space separated integers $N, B$. -----Output:----- For each test case, print a single integer $A$, the minimum number of cookies Chef must bake initially. -----Constraints----- - $1 \leq T \leq 1000$ - $1 \leq N \leq 1000000000$ - $2 \leq B \leq 1000000000$ -----Sample Input 1:----- 1 3 2 -----Sample Output 1:----- 2 -----Sample Input 2:----- 1 11 2 -----Sample Output 2:----- 6 -----Explanation 2:----- Chef initially make 6 cookies. From the crumbs, Chef makes 3 new cookies with no crumbs left over. From the crumbs of the new cookies, Chef makes 1 new cookie and have crumbs left from one cookie. From the new cookie, Chef gets more crumbs. He adds the crumbs and gets one last cookie. After eating that, there are not enough crumbs left to make a new cookie. So a total of 11 cookies are consumed!
{"inputs": ["1\n3 2", "1\n11 2"], "outputs": ["2", "6"]}
371
27
coding
Solve the programming task below in a Python markdown code block. Treeland is a country with $n$ cities and $n-1$ roads. There is exactly one path between any two cities. The ruler of Treeland wants to implement a self-driving bus system and asks tree-loving Alex to plan the bus routes. Alex decides that each route must contain a subset of connected cities; a subset of cities is connected if the following two conditions are true: There is a path between every pair of cities which belongs to the subset. Every city in the path must belong to the subset. In the figure above, $\{2,3,4,5\}$ is a connected subset, but $\{6,7,9\}$ is not (for the second condition to be true, $8$ would need to be part of the subset). Each self-driving bus will operate within a connected segment of Treeland. A connected segment $[L,R]$ where $1\leq L\leq R\leq n$ is defined by the connected subset of cities $S=\{x\ |x\in Z\ \text{and}\ \ L\leq x\leq R\}$. In the figure above, $[2,5]$ is a connected segment that represents the subset $\{2,3,4,5\}$. Note that a single city can be a segment too. Help Alex to find number of connected segments in Treeland. Input Format The first line contains a single positive integer, $n$. The $n-1$ subsequent lines each contain two positive space-separated integers, $a_i$ and $b_i$, describe an edge connecting two nodes in tree $\mathbf{T}$. Constraints $1\leq n\leq2\times10^5$ $1\leq a_i,b_i\leq n$ Subtasks For $25\%$ score: $1\leq n\leq2\times10^3$ For $50\%$ score: $1\leq n\leq10^4$ Output Format Print a single integer: the number of segments $[L,R]$, which are connected in tree $\mathbf{T}$. Sample Input 3 1 3 3 2 Sample Output 5 Explanation The connected segments for our test case are: $[1,1]$, $[2,2]$, $[3,3]$, $[2,3]$, and $[1,3]$. These segments can be represented by the respective subsets: $\{1\}$, $\{2\}$, $\{3\}$, $\{2,3\}$, and $\{1,2,3\}$. Note: $[1,2]$ is not a connected segment. It represents the subset $\{1,2\}$ and the path between $1$ and $2$ goes through $3$ which is not a member of the subset.
{"inputs": ["3\n1 3\n3 2\n"], "outputs": ["5\n"]}
636
22
coding
Solve the programming task below in a Python markdown code block. The R1 company has recently bought a high rise building in the centre of Moscow for its main office. It's time to decorate the new office, and the first thing to do is to write the company's slogan above the main entrance to the building. The slogan of the company consists of n characters, so the decorators hung a large banner, n meters wide and 1 meter high, divided into n equal squares. The first character of the slogan must be in the first square (the leftmost) of the poster, the second character must be in the second square, and so on. Of course, the R1 programmers want to write the slogan on the poster themselves. To do this, they have a large (and a very heavy) ladder which was put exactly opposite the k-th square of the poster. To draw the i-th character of the slogan on the poster, you need to climb the ladder, standing in front of the i-th square of the poster. This action (along with climbing up and down the ladder) takes one hour for a painter. The painter is not allowed to draw characters in the adjacent squares when the ladder is in front of the i-th square because the uncomfortable position of the ladder may make the characters untidy. Besides, the programmers can move the ladder. In one hour, they can move the ladder either a meter to the right or a meter to the left. Drawing characters and moving the ladder is very tiring, so the programmers want to finish the job in as little time as possible. Develop for them an optimal poster painting plan! -----Input----- The first line contains two integers, n and k (1 ≤ k ≤ n ≤ 100) — the number of characters in the slogan and the initial position of the ladder, correspondingly. The next line contains the slogan as n characters written without spaces. Each character of the slogan is either a large English letter, or digit, or one of the characters: '.', '!', ',', '?'. -----Output----- In t lines, print the actions the programmers need to make. In the i-th line print: "LEFT" (without the quotes), if the i-th action was "move the ladder to the left"; "RIGHT" (without the quotes), if the i-th action was "move the ladder to the right"; "PRINT x" (without the quotes), if the i-th action was to "go up the ladder, paint character x, go down the ladder". The painting time (variable t) must be minimum possible. If there are multiple optimal painting plans, you can print any of them. -----Examples----- Input 2 2 R1 Output PRINT 1 LEFT PRINT R Input 2 1 R1 Output PRINT R RIGHT PRINT 1 Input 6 4 GO?GO! Output RIGHT RIGHT PRINT ! LEFT PRINT O LEFT PRINT G LEFT PRINT ? LEFT PRINT O LEFT PRINT G -----Note----- Note that the ladder cannot be shifted by less than one meter. The ladder can only stand in front of some square of the poster. For example, you cannot shift a ladder by half a meter and position it between two squares. Then go up and paint the first character and the second character.
{"inputs": ["1 1\n!\n", "1 1\n!\n", "2 2\nR1\n", "2 1\nR1\n", "2 1\nOA\n", "2 2\nGW\n", "2 1\nOA\n", "2 2\nGW\n"], "outputs": ["PRINT !\n", "PRINT !\n", "PRINT 1\nLEFT\nPRINT R\n", "PRINT R\nRIGHT\nPRINT 1\n", "PRINT O\nRIGHT\nPRINT A\n", "PRINT W\nLEFT\nPRINT G\n", "PRINT O\nRIGHT\nPRINT A\n", "PRINT W\nLEFT\nPRINT G\n"]}
691
146
coding
Solve the programming task below in a Python markdown code block. The territory of Berland is represented by a rectangular field n × m in size. The king of Berland lives in the capital, located on the upper left square (1, 1). The lower right square has coordinates (n, m). One day the king decided to travel through the whole country and return back to the capital, having visited every square (except the capital) exactly one time. The king must visit the capital exactly two times, at the very beginning and at the very end of his journey. The king can only move to the side-neighboring squares. However, the royal advise said that the King possibly will not be able to do it. But there is a way out — one can build the system of one way teleporters between some squares so that the king could fulfill his plan. No more than one teleporter can be installed on one square, every teleporter can be used any number of times, however every time it is used, it transports to the same given for any single teleporter square. When the king reaches a square with an installed teleporter he chooses himself whether he is or is not going to use the teleport. What minimum number of teleporters should be installed for the king to complete the journey? You should also compose the journey path route for the king. Input The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100, 2 ≤ n · m) — the field size. The upper left square has coordinates (1, 1), and the lower right square has coordinates of (n, m). Output On the first line output integer k — the minimum number of teleporters. Then output k lines each containing 4 integers x1 y1 x2 y2 (1 ≤ x1, x2 ≤ n, 1 ≤ y1, y2 ≤ m) — the coordinates of the square where the teleporter is installed (x1, y1), and the coordinates of the square where the teleporter leads (x2, y2). Then print nm + 1 lines containing 2 numbers each — the coordinates of the squares in the order in which they are visited by the king. The travel path must start and end at (1, 1). The king can move to side-neighboring squares and to the squares where a teleporter leads. Besides, he also should visit the capital exactly two times and he should visit other squares exactly one time. Examples Input 2 2 Output 0 1 1 1 2 2 2 2 1 1 1 Input 3 3 Output 1 3 3 1 1 1 1 1 2 1 3 2 3 2 2 2 1 3 1 3 2 3 3 1 1
{"inputs": ["1 3\n", "1 2\n", "3 2\n", "3 1\n", "6 3\n", "2 3\n", "3 4\n", "2 1\n"], "outputs": ["1\n1 3 1 1\n1 1\n1 2\n1 3\n1 1\n", "0\n1 1\n1 2\n1 1\n", "0\n1 1\n2 1\n3 1\n3 2\n2 2\n1 2\n1 1\n", "1\n3 1 1 1\n1 1\n2 1\n3 1\n1 1\n", "0\n1 1\n1 2\n1 3\n2 3\n2 2\n3 2\n3 3\n4 3\n4 2\n5 2\n5 3\n6 3\n6 2\n6 1\n5 1\n4 1\n3 1\n2 1\n1 1\n", "0\n1 1\n1 2\n1 3\n2 3\n2 2\n2 1\n1 1\n", "0\n1 1\n2 1\n3 1\n3 2\n2 2\n2 3\n3 3\n3 4\n2 4\n1 4\n1 3\n1 2\n1 1\n", "0\n1 1\n2 1\n1 1\n"]}
606
342
coding
Solve the programming task below in a Python markdown code block. There is a tree T with N vertices, numbered 1 through N. For each 1 ≤ i ≤ N - 1, the i-th edge connects vertices a_i and b_i. Snuke is constructing a directed graph T' by arbitrarily assigning direction to each edge in T. (There are 2^{N - 1} different ways to construct T'.) For a fixed T', we will define d(s,\ t) for each 1 ≤ s,\ t ≤ N, as follows: * d(s,\ t) = (The number of edges that must be traversed against the assigned direction when traveling from vertex s to vertex t) In particular, d(s,\ s) = 0 for each 1 ≤ s ≤ N. Also note that, in general, d(s,\ t) ≠ d(t,\ s). We will further define D as the following: 3d2f3f88e8fa23f065c04cd175c14ebf.png Snuke is constructing T' so that D will be the minimum possible value. How many different ways are there to construct T' so that D will be the minimum possible value, modulo 10^9 + 7? Constraints * 2 ≤ N ≤ 1000 * 1 ≤ a_i,\ b_i ≤ N * The given graph is a tree. Input The input is given from Standard Input in the following format: N a_1 b_1 a_2 b_2 : a_{N - 1} b_{N - 1} Output Print the number of the different ways to construct T' so that D will be the minimum possible value, modulo 10^9 + 7. Examples Input 4 1 2 1 3 1 4 Output 2 Input 4 1 2 2 3 3 4 Output 6 Input 6 1 2 1 3 1 4 2 5 2 6 Output 14 Input 10 2 4 2 5 8 3 10 7 1 6 2 8 9 5 8 6 10 6 Output 102
{"inputs": ["4\n1 2\n1 3\n2 4", "4\n1 2\n2 3\n2 4", "4\n1 2\n2 3\n1 4", "4\n1 2\n4 3\n2 4", "4\n1 2\n4 3\n1 4", "4\n1 2\n1 3\n3 4", "4\n1 3\n2 3\n2 4", "4\n1 2\n2 4\n3 4"], "outputs": ["6\n", "2\n", "6\n", "6\n", "6\n", "6\n", "6\n", "6\n"]}
503
158
coding
Solve the programming task below in a Python markdown code block. You have a long fence which consists of $n$ sections. Unfortunately, it is not painted, so you decided to hire $q$ painters to paint it. $i$-th painter will paint all sections $x$ such that $l_i \le x \le r_i$. Unfortunately, you are on a tight budget, so you may hire only $q - 2$ painters. Obviously, only painters you hire will do their work. You want to maximize the number of painted sections if you choose $q - 2$ painters optimally. A section is considered painted if at least one painter paints it. -----Input----- The first line contains two integers $n$ and $q$ ($3 \le n, q \le 5000$) — the number of sections and the number of painters availible for hire, respectively. Then $q$ lines follow, each describing one of the painters: $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$). -----Output----- Print one integer — maximum number of painted sections if you hire $q - 2$ painters. -----Examples----- Input 7 5 1 4 4 5 5 6 6 7 3 5 Output 7 Input 4 3 1 1 2 2 3 4 Output 2 Input 4 4 1 1 2 2 2 3 3 4 Output 3
{"inputs": ["4 3\n1 1\n2 2\n3 4\n", "3 3\n1 3\n1 1\n2 2\n", "6 3\n1 6\n1 3\n4 6\n", "3 3\n1 1\n2 3\n2 3\n", "5 3\n5 5\n1 3\n3 5\n", "3 3\n1 3\n1 2\n2 3\n", "5 3\n1 3\n2 3\n4 5\n", "3 3\n1 3\n2 3\n3 3\n"], "outputs": ["2\n", "3\n", "6\n", "2\n", "3\n", "3\n", "3\n", "3\n"]}
340
182
coding
Solve the programming task below in a Python markdown code block. A strongness of an even number is the number of times we can successively divide by 2 until we reach an odd number starting with an even number n. For example, if n = 12, then * 12 / 2 = 6 * 6 / 2 = 3 So we divided successively 2 times and we reached 3, so the strongness of 12 is `2`. If n = 16 then * 16 / 2 = 8 * 8 / 2 = 4 * 4 / 2 = 2 * 2 / 2 = 1 we divided successively 4 times and we reached 1, so the strongness of 16 is `4` # Task Given a closed interval `[n, m]`, return the even number that is the strongest in the interval. If multiple solutions exist return the smallest strongest even number. Note that programs must run within the allotted server time; a naive solution will probably time out. # Constraints ```if-not:ruby 1 <= n < m <= INT_MAX ``` ```if:ruby 1 <= n < m <= 2^64 ``` # Examples ``` [1, 2] --> 2 # 1 has strongness 0, 2 has strongness 1 [5, 10] --> 8 # 5, 7, 9 have strongness 0; 6, 10 have strongness 1; 8 has strongness 3 [48, 56] --> 48 Also feel free to reuse/extend the following starter code: ```python def strongest_even(n, m): ```
{"functional": "_inputs = [[1, 2], [5, 10], [48, 56], [129, 193], [2, 3], [4, 6], [3, 310], [33, 40], [456445, 678860], [324243, 897653214], [1151592177, 2129680158], [2085422641, 2128923730], [1082012216, 1876572332], [1806570867, 2067832928], [206346325, 1289058842]]\n_outputs = [[2], [8], [48], [192], [2], [4], [256], [40], [524288], [536870912], [1610612736], [2113929216], [1610612736], [1879048192], [1073741824]]\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(strongest_even(*i), o[0])"}
394
472
coding
Solve the programming task below in a Python markdown code block. Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order. According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai. Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date. Input The first line contains a single positive integer n (1 ≤ n ≤ 5000) — the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≤ bi < ai ≤ 109) — the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly. Output Print a single integer — the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date. Examples Input 3 5 2 3 1 4 2 Output 2 Input 3 6 1 5 2 4 3 Output 6 Note In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5. In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
{"inputs": ["1\n2 1\n", "1\n2 2\n", "2\n4 2\n4 1\n", "2\n3 2\n3 2\n", "2\n5 2\n5 1\n", "2\n3 1\n3 2\n", "2\n2 2\n4 1\n", "2\n3 2\n3 1\n"], "outputs": ["1", "2\n", "2", "2", "2", "2", "4\n", "2\n"]}
602
121
coding
Solve the programming task below in a Python markdown code block. This kata is inspired on the problem #50 of the Project Euler. The prime ``` 41``` is the result of the sum of many consecutive primes. In fact, ``` 2 + 3 + 5 + 7 + 11 + 13 = 41 , (6 addens) ``` Furthermore, the prime ``` 41``` is the prime below ``` 100 (val_max)``` that has the longest chain of consecutive prime addens. The prime with longest chain of addens for ```val_max = 500``` is ```499``` with ```17``` addens. In fact: ```3+5+7+11+13+17+19+23+29+31+37+41+43+47+53+59+61= 499``` Find the function ```prime_maxlength_chain()```(primeMaxlengthChain() javascript), that receives an argument ```val_max```, the upper limit, all the found primes should be less than ```val_max``` and outputs this found prime. Let's see some cases: ```python prime_maxlength_chain(100) == [41] prime_maxlength_chain(500) == [499] ``` If we have more than one prime with these features, the function should output an array with the found primes sorted. ```python prime_maxlength_chain(499) == [379, 491] ``` Random Tests for `val_max` (`valMax`) ``` 100 ≤ val_max ≤ 500.000 ``` Enjoy it! Also feel free to reuse/extend the following starter code: ```python def prime_maxlength_chain(n): ```
{"functional": "_inputs = [[100], [200], [500], [1000]]\n_outputs = [[[41]], [[197]], [[499]], [[953]]]\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_maxlength_chain(*i), o[0])"}
405
191
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Imagine you have a special keyboard with the following keys: A: Print one 'A' on the screen. Ctrl-A: Select the whole screen. Ctrl-C: Copy selection to buffer. Ctrl-V: Print buffer on screen appending it after what has already been printed. Given an integer n, return the maximum number of 'A' you can print on the screen with at most n presses on the keys.   Please complete the following python code precisely: ```python class Solution: def maxA(self, n: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(n = 3) == 3\n assert candidate(n = 7) == 9\n\n\ncheck(Solution().maxA)"}
129
43
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 containing distinct numbers, an integer start, and an integer goal. There is an integer x that is initially set to start, and you want to perform operations on x such that it is converted to goal. You can perform the following operation repeatedly on the number x: If 0 <= x <= 1000, then for any index i in the array (0 <= i < nums.length), you can set x to any of the following: x + nums[i] x - nums[i] x ^ nums[i] (bitwise-XOR) Note that you can use each nums[i] any number of times in any order. Operations that set x to be out of the range 0 <= x <= 1000 are valid, but no more operations can be done afterward. Return the minimum number of operations needed to convert x = start into goal, and -1 if it is not possible.   Please complete the following python code precisely: ```python class Solution: def minimumOperations(self, nums: List[int], start: int, goal: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [2,4,12], start = 2, goal = 12) == 2\n assert candidate(nums = [3,5,7], start = 0, goal = -4) == 2\n assert candidate(nums = [2,8,16], start = 0, goal = 1) == -1\n\n\ncheck(Solution().minimumOperations)"}
252
100
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. The product sum of two equal-length arrays a and b is equal to the sum of a[i] * b[i] for all 0 <= i < a.length (0-indexed). For example, if a = [1,2,3,4] and b = [5,2,3,1], the product sum would be 1*5 + 2*2 + 3*3 + 4*1 = 22. Given two arrays nums1 and nums2 of length n, return the minimum product sum if you are allowed to rearrange the order of the elements in nums1.    Please complete the following python code precisely: ```python class Solution: def minProductSum(self, nums1: List[int], nums2: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums1 = [5,3,4,2], nums2 = [4,2,2,5]) == 40\n assert candidate(nums1 = [2,1,4,5,7], nums2 = [3,2,4,8,6]) == 65\n\n\ncheck(Solution().minProductSum)"}
184
88
coding
Solve the programming task below in a Python markdown code block. You have been given a String S. You need to find and print whether this string is a palindrome or not. If yes, print "YES" (without quotes), else print "NO" (without quotes). Input Format The first and only line of input contains the String S. The String shall consist of lowercase English alphabets only. Output Format Print the required answer on a single line. Constraints 1 ≤ |S| ≤ 100 Note String S consists of lowercase English Alphabets only. SAMPLE INPUT aba SAMPLE OUTPUT YES
{"inputs": ["eovbgggijqjdsdhjyojeaujcdyyyxtpjlllowjyarfhxjxwkxpranhtlucoklahtokmqyozlrwhldozgbgpalkqlcsiowyeslusn", "sqfopldohhwnbhhpnbxiwzvkybggkgtftvvaqpejznlxluatcppctaulxlnzjepqavvtftgkggbykvzwixbnphhbnwhhodlpofqs", "fnjzxnxnjplfwzowfdrhrvhegkmoncbkembjoudteqchjwqfzlofyflkmxnooasxulwofjzknthqqxgshvwxdvhdnlzjzdjdiifg", "izvnxvusaemgsgujwjaxkwdyeufbpfgbilfyxozyssuufxwfduudfwxfuussyzoxyflibgfpbfueydwkxajwjugsgmeasuvxnvzi"], "outputs": ["NO", "YES", "NO", "YES"]}
135
256
coding
Solve the programming task below in a Python markdown code block. You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i ⋅ j - k ⋅ (a_i | a_j) over all pairs (i, j) of integers with 1 ≤ i < j ≤ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≤ t ≤ 10 000) — the number of test cases. The first line of each test case contains two integers n (2 ≤ n ≤ 10^5) and k (1 ≤ k ≤ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 ⋅ 10^5. Output For each test case, print a single integer — the maximum possible value of i ⋅ j - k ⋅ (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i ⋅ j - k ⋅ (a_i | a_j). In the first test case, * f(1, 2) = 1 ⋅ 2 - k ⋅ (a_1 | a_2) = 2 - 3 ⋅ (1 | 1) = -1. * f(1, 3) = 1 ⋅ 3 - k ⋅ (a_1 | a_3) = 3 - 3 ⋅ (1 | 3) = -6. * f(2, 3) = 2 ⋅ 3 - k ⋅ (a_2 | a_3) = 6 - 3 ⋅ (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
{"inputs": ["1\n2 1\n2 2\n", "1\n2 1\n2 0\n", "1\n2 1\n0 0\n", "1\n2 1\n2 1\n", "1\n2 2\n2 2\n", "1\n2 1\n1 0\n", "1\n2 2\n2 1\n", "1\n2 2\n0 0\n"], "outputs": ["0\n", "0\n", "2\n", "-1\n", "-2\n", "1\n", "-4\n", "2\n"]}
546
134
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an array of strings words, return the smallest string that contains each string in words as a substring. If there are multiple valid strings of the smallest length, return any of them. You may assume that no string in words is a substring of another string in words.   Please complete the following python code precisely: ```python class Solution: def shortestSuperstring(self, words: List[str]) -> str: ```
{"functional": "def check(candidate):\n assert candidate(words = [\"alex\",\"loves\",\"leetcode\"]) == \"alexlovesleetcode\"\n assert candidate(words = [\"catg\",\"ctaagt\",\"gcta\",\"ttca\",\"atgcatc\"]) == \"gctaagttcatgcatc\"\n\n\ncheck(Solution().shortestSuperstring)"}
103
81
coding
Solve the programming task below in a Python markdown code block. The sequence of $m$ integers is called the permutation if it contains all integers from $1$ to $m$ exactly once. The number $m$ is called the length of the permutation. Dreamoon has two permutations $p_1$ and $p_2$ of non-zero lengths $l_1$ and $l_2$. Now Dreamoon concatenates these two permutations into another sequence $a$ of length $l_1 + l_2$. First $l_1$ elements of $a$ is the permutation $p_1$ and next $l_2$ elements of $a$ is the permutation $p_2$. You are given the sequence $a$, and you need to find two permutations $p_1$ and $p_2$. If there are several possible ways to restore them, you should find all of them. (Note that it is also possible that there will be no ways.) -----Input----- The first line contains an integer $t$ ($1 \le t \le 10\,000$) denoting the number of test cases in the input. Each test case contains two lines. The first line contains one integer $n$ ($2 \leq n \leq 200\,000$): the length of $a$. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n-1$). The total sum of $n$ is less than $200\,000$. -----Output----- For each test case, the first line of output should contain one integer $k$: the number of ways to divide $a$ into permutations $p_1$ and $p_2$. Each of the next $k$ lines should contain two integers $l_1$ and $l_2$ ($1 \leq l_1, l_2 \leq n, l_1 + l_2 = n$), denoting, that it is possible to divide $a$ into two permutations of length $l_1$ and $l_2$ ($p_1$ is the first $l_1$ elements of $a$, and $p_2$ is the last $l_2$ elements of $a$). You can print solutions in any order. -----Example----- Input 6 5 1 4 3 2 1 6 2 4 1 3 2 1 4 2 1 1 3 4 1 3 3 1 12 2 1 3 4 5 6 7 8 9 1 10 2 3 1 1 1 Output 2 1 4 4 1 1 4 2 0 0 1 2 10 0 -----Note----- In the first example, two possible ways to divide $a$ into permutations are $\{1\} + \{4, 3, 2, 1\}$ and $\{1,4,3,2\} + \{1\}$. In the second example, the only way to divide $a$ into permutations is $\{2,4,1,3\} + \{2,1\}$. In the third example, there are no possible ways.
{"inputs": ["6\n5\n1 4 3 2 1\n6\n2 4 1 3 2 1\n4\n2 1 1 3\n4\n1 3 3 1\n12\n2 1 3 4 5 6 7 8 9 1 10 2\n3\n1 1 1\n", "6\n5\n1 4 3 1 1\n6\n2 4 1 3 2 1\n4\n2 1 1 3\n4\n1 3 3 1\n12\n2 1 3 4 5 6 7 8 9 1 10 2\n3\n1 1 1\n", "6\n5\n1 4 3 2 1\n6\n2 4 1 3 3 1\n4\n2 1 1 3\n4\n1 3 3 1\n12\n2 1 3 4 5 6 7 8 9 1 10 2\n3\n1 1 1\n", "6\n5\n1 4 3 1 1\n6\n2 4 1 4 2 1\n4\n2 1 1 3\n4\n1 3 3 1\n12\n2 1 3 4 5 6 7 8 9 1 10 2\n3\n1 1 1\n", "6\n5\n1 4 3 2 1\n6\n2 4 1 3 3 1\n4\n2 1 1 3\n4\n1 3 3 1\n12\n2 1 3 4 3 6 7 8 9 1 10 2\n3\n1 1 1\n", "6\n5\n1 4 3 2 1\n6\n2 4 1 3 3 1\n4\n2 1 1 3\n4\n1 3 3 1\n12\n2 1 3 4 3 6 7 8 9 1 10 3\n3\n1 1 2\n", "6\n5\n1 2 3 2 1\n6\n2 3 1 3 3 1\n4\n2 2 1 3\n4\n1 3 3 1\n12\n2 1 5 4 3 6 7 8 9 1 10 3\n3\n1 1 2\n", "6\n5\n2 4 3 2 1\n6\n2 4 1 3 3 1\n4\n2 1 1 1\n4\n1 3 3 1\n12\n2 1 3 4 3 6 7 8 9 1 10 2\n3\n1 1 1\n"], "outputs": ["2\n1 4\n4 1\n1\n4 2\n0\n0\n1\n2 10\n0\n", "0\n1\n4 2\n0\n0\n1\n2 10\n0\n", "2\n1 4\n4 1\n0\n0\n0\n1\n2 10\n0\n", "0\n0\n0\n0\n1\n2 10\n0\n", "2\n1 4\n4 1\n0\n0\n0\n0\n0\n", "2\n1 4\n4 1\n0\n0\n0\n0\n1\n1 2\n", "2\n2 3\n3 2\n0\n0\n0\n0\n1\n1 2\n", "0\n0\n0\n0\n0\n0\n"]}
743
882
coding
Solve the programming task below in a Python markdown code block. Your task is to write function which takes string and list of delimiters as an input and returns list of strings/characters after splitting given string. Example: ```python multiple_split('Hi, how are you?', [' ']) => ['Hi,', 'how', 'are', 'you?'] multiple_split('1+2-3', ['+', '-']) => ['1', '2', '3'] ``` List of delimiters is optional and can be empty, so take that into account. Important note: Result cannot contain empty string. Also feel free to reuse/extend the following starter code: ```python def multiple_split(string, delimiters=[]): ```
{"functional": "_inputs = [['Hi everybody!', [' ', '!']], ['(1+2)*6-3^9', ['+', '-', '(', ')', '^', '*']], ['Solve_this|kata-very\\\\quickly!', ['!', '|', '\\\\', '_', '-']], ['', []], [''], ['some strange string'], ['1_2_3_huhuh_hahaha', ['_']], ['((1+2))*(6-3)^9', ['+', '-', '(', ')', '^', '*']], ['(((1+2)-(3)))', ['(', ')']]]\n_outputs = [[['Hi', 'everybody']], [['1', '2', '6', '3', '9']], [['Solve', 'this', 'kata', 'very', 'quickly']], [[]], [[]], [['some strange string']], [['1', '2', '3', 'huhuh', 'hahaha']], [['1', '2', '6', '3', '9']], [['1+2', '-', '3']]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(multiple_split(*i), o[0])"}
149
358
coding
Solve the programming task below in a Python markdown code block. During one of the space missions, humans have found an evidence of previous life at one of the planets. They were lucky enough to find a book with birth and death years of each individual that had been living at this planet. What's interesting is that these years are in the range $(1, 10^9)$! Therefore, the planet was named Longlifer. In order to learn more about Longlifer's previous population, scientists need to determine the year with maximum number of individuals that were alive, as well as the number of alive individuals in that year. Your task is to help scientists solve this problem! -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^5$) — the number of people. Each of the following $n$ lines contain two integers $b$ and $d$ ($1 \le b \lt d \le 10^9$) representing birth and death year (respectively) of each individual. -----Output----- Print two integer numbers separated by blank character, $y$  — the year with a maximum number of people alive and $k$  — the number of people alive in year $y$. In the case of multiple possible solutions, print the solution with minimum year. -----Examples----- Input 3 1 5 2 4 5 6 Output 2 2 Input 4 3 4 4 5 4 6 8 10 Output 4 2 -----Note----- You can assume that an individual living from $b$ to $d$ has been born at the beginning of $b$ and died at the beginning of $d$, and therefore living for $d$ - $b$ years.
{"inputs": ["1\n1 2\n", "1\n1 2\n", "1\n1 3\n", "1\n0 3\n", "1\n0 1\n", "1\n0 2\n", "1\n0 6\n", "1\n1 6\n"], "outputs": ["1 1\n", "1 1\n", "1 1\n", "0 1\n", "0 1\n", "0 1\n", "0 1\n", "1 1\n"]}
382
118
coding
Solve the programming task below in a Python markdown code block. Given the names and grades for each student in a class of $N$ students, store them in a nested list and print the name(s) of any student(s) having the second lowest grade. Note: If there are multiple students with the second lowest grade, order their names alphabetically and print each name on a new line. Example $records=[\text{"chi"},20.0],[\text{"beta"},50.0],[\text{"alpha"},50.0]]$ The ordered list of scores is $[20.0,50.0]$, so the second lowest score is $50.0$. There are two students with that score: $[\text{"beta"},\text{"alpha"}]$. Ordered alphabetically, the names are printed as: alpha beta Input Format The first line contains an integer, $N$, the number of students. The $2N$ subsequent lines describe each student over $2$ lines. - The first line contains a student's name. - The second line contains their grade. Constraints $2\leq N\leq5$ There will always be one or more students having the second lowest grade. Output Format Print the name(s) of any student(s) having the second lowest grade in. If there are multiple students, order their names alphabetically and print each one on a new line. Sample Input 0 5 Harry 37.21 Berry 37.21 Tina 37.2 Akriti 41 Harsh 39 Sample Output 0 Berry Harry Explanation 0 There are $5$ students in this class whose names and grades are assembled to build the following list: python students = [['Harry', 37.21], ['Berry', 37.21], ['Tina', 37.2], ['Akriti', 41], ['Harsh', 39]] The lowest grade of $37.2$ belongs to Tina. The second lowest grade of $37.21$ belongs to both Harry and Berry, so we order their names alphabetically and print each name on a new line.
{"inputs": ["5\nHarry\n37.21\nBerry\n37.21\nTina\n37.2\nAkriti\n41\nHarsh\n39\n"], "outputs": ["Berry\nHarry\n"]}
487
53
coding
Solve the programming task below in a Python markdown code block. You are given non-negative integers A, B and C. Does there exist a non-negative integer X such that A \oplus X+ B \oplus X = C \oplus X? As a reminder, \oplus denotes the [bitwise XOR operation]. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The only line of each test case contains three space-separated non-negative integers A, B and C. ------ Output Format ------ For each test case, print on a new line the answer: YES if valid X exists, and NO otherwise. Each character of the output may be printed in either uppercase or lowercase, i.e, the strings Yes, YES, yes, yEs will all be treated as identical. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $0 ≤ A, B, C < 2^{27}$ ----- Sample Input 1 ------ 5 2 5 7 2 3 13 7 0 7 2 7 6 1 6 6 ----- Sample Output 1 ------ YES NO YES YES YES ----- explanation 1 ------ Test case $1$: $X=0$ satisfies the equation. Test case $2$: It can be proved that there does not exist a non-negative integer $X$ which satisfies the equation. Test case $3$: $X=0$ satisfies the equation. Test case $4$: $X=3$ satisfies the equation. Test case $5$: $X=1$ satisfies the equation.
{"inputs": ["5\n2 5 7\n2 3 13\n7 0 7\n2 7 6\n1 6 6\n"], "outputs": ["YES\nNO\nYES\nYES\nYES\n"]}
354
53