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. Andrzej was given a task: There are n jars with pills. In every jar there is a different type of pill and the amount of pills in each jar is infinite. One type of pill makes a person glow about 30 minutes after taking and none of the other types has any effect. His job is to determine, in which jar are the pills that make a person glow. But there is one catch, he only has 35 minutes to do so.(so he can't take a pill, wait for the results and then take another one, because he wouldn't be able to see the results) Fortunetely, he can take any number of friends he needs with him. On completing the task Andrzej receives one million dollars. You know that Andrzej is very honest, so he will split the money equally with his friends. Your job is to determine how many friends does Andrzej need to complete the task.(He also wants to make the highest amount of money.) For example for n = 2 The answer is 0 because he doesn't need any friends, he just needs to take a pill from the first jar and wait for the effects. For another example for n = 4 The answer is 1 because having pills A B C D Andrzej can take pills A B and the friend can take pills B C Also feel free to reuse/extend the following starter code: ```python def friends(n): ```
{"functional": "_inputs = [[0], [1], [2], [4], [3], [16]]\n_outputs = [[0], [0], [0], [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(friends(*i), o[0])"}
309
185
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given the array nums, obtain a subsequence of the array whose sum of elements is strictly greater than the sum of the non included elements in such subsequence.  If there are multiple solutions, return the subsequence with minimum size and if there still exist multiple solutions, return the subsequence with the maximum total sum of all its elements. A subsequence of an array can be obtained by erasing some (possibly zero) elements from the array.  Note that the solution with the given constraints is guaranteed to be unique. Also return the answer sorted in non-increasing order.   Please complete the following python code precisely: ```python class Solution: def minSubsequence(self, nums: List[int]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(nums = [4,3,10,9,8]) == [10,9] \n assert candidate(nums = [4,4,7,6,7]) == [7,7,6] \n\n\ncheck(Solution().minSubsequence)"}
168
72
coding
Solve the programming task below in a Python markdown code block. Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. -----Input----- The first line contains an integer n (1 ≤ n ≤ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right. -----Output----- Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 -----Note----- In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
{"inputs": ["1\n1\n", "1\n0\n", "1\n0\n", "1\n1\n", "2\n0 1\n", "2\n1 0\n", "2\n0 0\n", "2\n1 1\n"], "outputs": ["0", "0", "0\n", "0\n", "0", "1", "0", "0"]}
456
88
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Russian] and [Mandarin Chinese]. Value of an array A of length L is defined as the sum of (A_{i} \oplus i) for all 0 ≤ i < L, where \oplus denotes [bitwise xor operation]. Note that array indices start from zero. You are given an integer N and an array A consisting of 2^{N} integers where A_{i} = i for all 0 ≤ i < 2^{N}. Example : For N = 1, you have an array A of length 2^{1} = 2 and A = [0, 1]. For N = 2, you have an array A of length 2^{2} = 4 and A = [0, 1, 2, 3]. You can do at most K operations on this array. In one operation, you can choose two indices i and j (0 ≤ i, j < 2^{N}) and swap A_{i} and A_{j} (i.e. A_{i} becomes A_{j} and vice versa). What is the maximum value of array A you can obtain after at most K operations? ------ Input Format ------ - First line will contain T, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, two integers N, K. ------ Output Format ------ For each testcase, output in a single line the maximum value of array after doing at most K operations. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 30$ $0 ≤ K ≤ 10^{9}$ ----- Sample Input 1 ------ 3 2 0 2 1 10 100 ----- Sample Output 1 ------ 0 6 204600 ----- explanation 1 ------ - In the first test case, for $N = 2$, you have the array $A = [0, 1, 2, 3]$. No swap operation is allowed hence value of array $A = (0 \oplus 0) + (1 \oplus 1) + (2 \oplus 2) + (3 \oplus 3) = 0 + 0 + 0 + 0 = 0$. - In the second test case, initially the array $A = [0, 1, 2, 3]$. If you swap $A_{1}$ and $A_{2}$, $A$ becomes $[0, 2, 1, 3]$. Now value of array $A = (0 \oplus 0) + (2 \oplus 1) + (1 \oplus 2) + (3 \oplus 3) = 0 + 3 + 3 + 0 = 6$. There is no possible way such that value of array $A$ becomes greater than $6$ using one swap operation.
{"inputs": ["3\n2 0\n2 1\n10 100"], "outputs": ["0\n6\n204600"]}
658
36
coding
Solve the programming task below in a Python markdown code block. # Task You are given a string `s`. Every letter in `s` appears once. Consider all strings formed by rearranging the letters in `s`. After ordering these strings in dictionary order, return the middle term. (If the sequence has a even length `n`, define its middle term to be the `(n/2)`th term.) # Example For `s = "abc"`, the result should be `"bac"`. ``` The permutations in order are: "abc", "acb", "bac", "bca", "cab", "cba" So, The middle term is "bac".``` # Input/Output - `[input]` string `s` unique letters (`2 <= length <= 26`) - `[output]` a string middle permutation. Also feel free to reuse/extend the following starter code: ```python def middle_permutation(string): ```
{"functional": "_inputs = [['abc'], ['abcd'], ['abcdx'], ['abcdxg'], ['abcdxgz']]\n_outputs = [['bac'], ['bdca'], ['cbxda'], ['cxgdba'], ['dczxgba']]\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(middle_permutation(*i), o[0])"}
204
191
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given two positive integers n and target. An integer is considered beautiful if the sum of its digits is less than or equal to target. Return the minimum non-negative integer x such that n + x is beautiful. The input will be generated such that it is always possible to make n beautiful.   Please complete the following python code precisely: ```python class Solution: def makeIntegerBeautiful(self, n: int, target: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(n = 16, target = 6) == 4\n assert candidate(n = 467, target = 6) == 33\n assert candidate(n = 1, target = 1) == 0\n\n\ncheck(Solution().makeIntegerBeautiful)"}
112
75
coding
Solve the programming task below in a Python markdown code block. Sereja has a string A consisting of n lower case English letters. Sereja calls two strings X and Y each of length n similar if they can be made equal by applying the following operation at most once in each of them. - Chose any two position i, j in the string (i can be equal to j too). Swap the characters at position i with character at position j. For example strings "abcd" and "acbd" are similar, strings "ab" and "ab" are similar, but strings "abcde" and "bcdea" are not similar. Note that strings "abc" and "cab" are also similar, as you can swap 'a' and 'c' in the first string to get "cba" and 'a' and 'b' in the second string to get "cba". Now Sereja is interested in finding number of ordered pairs of non similar strings X and Y such that they can be constructed from a given string A by permutation of its characters. As answer could be large, please output your answer modulo (109 + 7). Note A string s (of size n) is said to be constructed from string t (also of size n) by permutation of its characters if there exists a permutation P (of length n), such that s[i] = t[P[i]] for each i from 1 to n. -----Input----- - First line contain integer T - number of test cases. - For each of the next T lines: - Each line contains a string A as defined in the problem. -----Output----- For each test case, output answer modulo 1000000007 (109 + 7) in separate line. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ n ≤ 10^5 -----Constraints----- - Subtask #1: 1 ≤ n ≤ 10 (25 points) - Subtask #2: 1 ≤ n ≤ 100 (25 points) - Subtask #3: 1 ≤ n ≤ 1000 (25 points) - Subtask #4: original constraints (25 points) -----Example----- Input: 2 z abcd Output: 0 144
{"inputs": ["2\nz\nabcd\n\n"], "outputs": ["0\n144"]}
504
22
coding
Solve the programming task below in a Python markdown code block. Polycarp likes numbers that are divisible by 3. He has a huge number $s$. Polycarp wants to cut from it the maximum number of numbers that are divisible by $3$. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after $m$ such cuts, there will be $m+1$ parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by $3$. For example, if the original number is $s=3121$, then Polycarp can cut it into three parts with two cuts: $3|1|21$. As a result, he will get two numbers that are divisible by $3$. Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid. What is the maximum number of numbers divisible by $3$ that Polycarp can obtain? -----Input----- The first line of the input contains a positive integer $s$. The number of digits of the number $s$ is between $1$ and $2\cdot10^5$, inclusive. The first (leftmost) digit is not equal to 0. -----Output----- Print the maximum number of numbers divisible by $3$ that Polycarp can get by making vertical cuts in the given number $s$. -----Examples----- Input 3121 Output 2 Input 6 Output 1 Input 1000000000000000000000000000000000 Output 33 Input 201920181 Output 4 -----Note----- In the first example, an example set of optimal cuts on the number is 3|1|21. In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by $3$. In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and $33$ digits 0. Each of the $33$ digits 0 forms a number that is divisible by $3$. In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers $0$, $9$, $201$ and $81$ are divisible by $3$.
{"inputs": ["6\n", "4\n", "4\n", "5\n", "8\n", "1\n", "2\n", "7\n"], "outputs": ["1\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
621
70
coding
Solve the programming task below in a Python markdown code block. After learning about polynomial hashing, Heidi decided to learn about shift-xor hashing. In particular, she came across this interesting problem. Given a bitstring $y \in \{0,1\}^n$ find out the number of different $k$ ($0 \leq k < n$) such that there exists $x \in \{0,1\}^n$ for which $y = x \oplus \mbox{shift}^k(x).$ In the above, $\oplus$ is the xor operation and $\mbox{shift}^k$ is the operation of shifting a bitstring cyclically to the right $k$ times. For example, $001 \oplus 111 = 110$ and $\mbox{shift}^3(00010010111000) = 00000010010111$. -----Input----- The first line contains an integer $n$ ($1 \leq n \leq 2 \cdot 10^5$), the length of the bitstring $y$. The second line contains the bitstring $y$. -----Output----- Output a single integer: the number of suitable values of $k$. -----Example----- Input 4 1010 Output 3 -----Note----- In the first example: $1100\oplus \mbox{shift}^1(1100) = 1010$ $1000\oplus \mbox{shift}^2(1000) = 1010$ $0110\oplus \mbox{shift}^3(0110) = 1010$ There is no $x$ such that $x \oplus x = 1010$, hence the answer is $3$.
{"inputs": ["1\n0\n", "1\n1\n", "1\n0\n", "1\n1\n", "2\n00\n", "2\n10\n", "2\n01\n", "2\n11\n"], "outputs": ["1\n", "0\n", "1", "0", "2\n", "0\n", "0\n", "1\n"]}
431
88
coding
Solve the programming task below in a Python markdown code block. Andy got a box of candies for Christmas. In fact, he discovered that the box contained several identical smaller boxes, and they could contain even smaller boxes, and so on. Formally, we say that candies are boxes of level 0, and for 1 ≤ i ≤ n, a level i box contains ai boxes of level i - 1. The largest box has level n. Andy realized that it can take quite a long time to open all the boxes before he actually gets to eat some candies, so he put the box aside in frustration. But today being his birthday, some friends came to visit Andy, and Andy decided to share some candies with them. In order to do that, he must open some of the boxes. Naturally, Andy can not open a box that is still inside an unopened box. If Andy wants to retrieve X candies, what is the least number of boxes he must open? You must help him answer many such queries. Each query is independent. -----Input----- - The first line contains two integers n and m, which refer to the level of the largest box, and the number of queries respectively. - The second line contains n integers a1, ..., an. - The third line contains m integers X1, ..., Xm. -----Output----- - Print m integers each in a new line, ith of them equal to the smallest number of boxes Andy must open in order to retrieve at least Xi candies. -----Constraints----- - 1 ≤ n,m ≤ 300000 - 1 ≤ ai ≤ 109 - 1 ≤ Xi ≤ 1012 - It is guaranteed that the total number of candies is at least Xi for all i -----Example----- Input 1: 5 1 1 1 1 1 1 1 Output 1: 5 Input 2: 3 3 3 3 3 2 8 13 Output 2: 3 5 8 -----Explanation----- Testcase 1: The only candy is contained in five levels of boxes. Testcase 2: In the third query, for 13 candies, Andy should open the largest box, two level-2 boxes, and finally five of six available level-1 boxes. Each of those boxes will contain 3 level-0 boxes (which are candies). So he'll have 15 candies in total, but he needs only 13 of them.
{"inputs": ["5 1\n1 1 1 1 1\n1", "3 3\n3 3 3\n2 8 13"], "outputs": ["5", "3\n5\n8"]}
554
51
coding
Solve the programming task below in a Python markdown code block. Write a program which performs the following operations to a binary search tree $T$ by adding the find operation to A: Binary Search Tree I. * insert $k$: Insert a node containing $k$ as key into $T$. * find $k$: Report whether $T$ has a node containing $k$. * print: Print the keys of the binary search tree by inorder tree walk and preorder tree walk respectively. Constraints * The number of operations $\leq 500,000$ * The number of print operations $\leq 10$. * $-2,000,000,000 \leq key \leq 2,000,000,000$ * The height of the binary tree does not exceed 100 if you employ the above pseudo code. * The keys in the binary search tree are all different. Input In the first line, the number of operations $m$ is given. In the following $m$ lines, operations represented by insert $k$, find $k$ or print are given. Output For each find $k$ operation, print "yes" if $T$ has a node containing $k$, "no" if not. In addition, for each print operation, print a list of keys obtained by inorder tree walk and preorder tree walk in a line respectively. Put a space character before each key. Example Input 10 insert 30 insert 88 insert 12 insert 1 insert 20 find 12 insert 17 insert 25 find 16 print Output yes no 1 12 17 20 25 30 88 30 12 1 20 17 25 88
{"inputs": ["10\ninsert 30\ninsert 88\ninsert 3\ninsert 2\ninsert 20\nfind 12\ninsert 1\ninsert 24\nfind 2\nprint", "10\ninsert 30\ninsert 88\ninsert 3\ninsert 2\ninsert 20\nfind 12\ninsert 1\ninsert 24\nfind 16\nprint", "10\ninsert 30\ninsert 88\ninsert 12\ninsert 2\ninsert 20\nfind 8\ninsert 1\ninsert 24\nfind 16\nprint", "10\ninsert 30\ninsert 88\ninsert 12\ninsert 1\ninsert 29\nfind 15\ninsert 9\ninsert 25\nfind 4\nprint", "10\ninsert 30\ninsert 88\ninsert 12\ninsert 2\ninsert 20\nfind 8\ninsert 1\ninsert 24\nfind 12\nprint", "10\ninsert 49\ninsert 8\ninsert 9\ninsert 0\ninsert 29\nfind 12\ninsert 17\ninsert 14\nfind 16\nprint", "10\ninsert 30\ninsert 88\ninsert 12\ninsert 1\ninsert 29\nfind 15\ninsert 8\ninsert 25\nfind 4\nprint", "10\ninsert 30\ninsert 88\ninsert 7\ninsert 0\ninsert 29\nfind 12\ninsert 8\ninsert 25\nfind 16\nprint"], "outputs": ["no\nyes\n 1 2 3 20 24 30 88\n 30 3 2 1 20 24 88\n", "no\nno\n 1 2 3 20 24 30 88\n 30 3 2 1 20 24 88\n", "no\nno\n 1 2 12 20 24 30 88\n 30 12 2 1 20 24 88\n", "no\nno\n 1 9 12 25 29 30 88\n 30 12 1 9 29 25 88\n", "no\nyes\n 1 2 12 20 24 30 88\n 30 12 2 1 20 24 88\n", "no\nno\n 0 8 9 14 17 29 49\n 49 8 0 9 29 17 14\n", "no\nno\n 1 8 12 25 29 30 88\n 30 12 1 8 29 25 88\n", "no\nno\n 0 7 8 25 29 30 88\n 30 7 0 29 8 25 88\n"]}
413
749
coding
Solve the programming task below in a Python markdown code block. In your class, you have started lessons about "arithmetic progression". Because you are also a programmer, you have decided to write a function. This function, arithmetic_sequence_sum(a, r, n), should return the sum of the first (n) elements of a sequence in which each element is the sum of the given integer (a), and a number of occurences of the given integer (r), based on the element's position within the sequence. For example: arithmetic_sequence_sum(2, 3, 5) should return 40: ``` 1 2 3 4 5 a + (a+r) + (a+r+r) + (a+r+r+r) + (a+r+r+r+r) 2 + (2+3) + (2+3+3) + (2+3+3+3) + (2+3+3+3+3) = 40 ``` Also feel free to reuse/extend the following starter code: ```python def arithmetic_sequence_sum(a, r, n): ```
{"functional": "_inputs = [[3, 2, 20], [2, 2, 10], [1, -2, 10]]\n_outputs = [[440], [110], [-80]]\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(arithmetic_sequence_sum(*i), o[0])"}
242
194
coding
Solve the programming task below in a Python markdown code block. We call an quadruple of positive integers, $(W,X,Y,Z)$, beautiful if the following condition is true: $W\oplus X\oplus Y\oplus Z\neq0$ Note: $\theta$ is the bitwise XOR operator. Given $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{D}$, count the number of beautiful quadruples of the form $(W,X,Y,Z)$ where the following constraints hold: $1\leq W\leq A$ $1\leq X\leq B$ $1\leq Y\leq C$ $1\leq z\leq D$ When you count the number of beautiful quadruples, you should consider two quadruples as same if the following are true: They contain same integers. Number of times each integers occur in the quadruple is same. For example $(1,1,1,2)$ and $(1,1,2,1)$ should be considered as same. Input Format A single line with four space-separated integers describing the respective values of $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{D}$. Constraints $1\leq A,B,C,D\leq3000$ For $50\%$ of the maximum score, $1\leq A,B,C,D\leq50$ Output Format Print the number of beautiful quadruples. Sample Input 1 2 3 4 Sample Output 11 Explanation There are $\mbox{11}$ beautiful quadruples for this input: $(1,1,1,2)$ $(1,1,1,3)$ $(1,1,1,4)$ $(1,1,2,3)$ $(1,1,2,4)$ $(1,1,3,4)$ $(1,2,2,2)$ $(1,2,2,3)$ $(1,2,2,4)$ $(1,2,3,3)$ $(1,2,3,4)$ Thus, we print $\mbox{11}$ as our output. Note that $(1,1,1,2)$ is same as $(1,1,2,1)$.
{"inputs": ["1 2 3 4\n"], "outputs": ["11\n"]}
518
21
coding
Solve the programming task below in a Python markdown code block. Alice has an array A of length N which is initially a *permutation*. She dislikes K numbers which are B_{1}, B_{2}, \ldots, B_{K} all of which are distinct. Therefore, she removes all the occurrences of these numbers from A. The order of the remaining elements of the A does not change. Can you find out the resulting array A? Note: A *permutation* of length N is an array where every integer from 1 to N occurs exactly once. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the array A. - The second line of each test case contains N integers A_{1}, A_{2}, \ldots, A_{N} denoting the array A. - The third line of each test case contains an integer K — the size of the array B. - The fourth line of each test case contains K integers B_{1}, B_{2}, \ldots, B_{K} denoting the numbers which Alice dislikes. ------ Output Format ------ For each test case, output the resulting array A after the removal of all occurrences of B_{1}, B_{2}, \ldots B_{K}. It is guaranteed that there will be at least one element in the resulting array. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ K < N ≤ 10^{5}$ $1 ≤A_{i}, B_{i} ≤N$ $A$ is initially a *permutation*. $B_{i} \ne B_{j}$ when $(i \ne j)$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 4 4 1 3 2 2 3 1 9 5 2 9 1 8 6 4 3 7 3 5 8 9 5 3 4 5 1 2 2 2 3 ----- Sample Output 1 ------ 4 2 2 1 6 4 3 7 4 5 1 ----- explanation 1 ------ Test Case 1: Here $A = [4, 1, 3, 2]$ and $B = [3, 1]$. The resulting array $A$ after removing all the numbers which Alice dislikes is $[4, 2]$. Note that here $[2, 4]$ is an incorrect answer since the order of elements should be the same as in the original array. Test Case 2: Here $A = [5, 2, 9, 1, 8, 6, 4, 3, 7]$ and $B = [5, 8, 9]$. The resulting array $A$ after removing all the numbers which Alice dislikes is $[2, 1, 6, 4, 3, 7]$. Test Case 3: Here $A = [3, 4, 5, 1, 2]$ and $B = [2, 3]$. The resulting array $A$ after removing all the numbers which Alice dislikes is $[4, 5, 1]$.
{"inputs": ["3\n4\n4 1 3 2\n2\n3 1\n9\n5 2 9 1 8 6 4 3 7\n3\n5 8 9\n5\n3 4 5 1 2\n2\n2 3\n"], "outputs": ["4 2\n2 1 6 4 3 7\n4 5 1\n"]}
740
96
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given the root of a binary tree and an integer targetSum, return all root-to-leaf paths where the sum of the node values in the path equals targetSum. Each path should be returned as a list of the node values, not node references. A root-to-leaf path is a path starting from the root and ending at any leaf node. A leaf is a node with no children.   Please complete the following python code precisely: ```python # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]: ```
{"functional": "def check(candidate):\n assert candidate(root = tree_node([5,4,8,11,None,13,4,7,2,None,None,5,1]), targetSum = 22) == [[5,4,11,2],[5,8,4,5]]\n assert candidate(root = tree_node([1,2,3]), targetSum = 5) == []\n assert candidate(root = tree_node([1,2]), targetSum = 0) == []\n\n\ncheck(Solution().pathSum)"}
187
126
coding
Solve the programming task below in a Python markdown code block. There are N people numbered from 1 to N such that: Exactly one of these people is a thief and always lies; All the others are honest and always tell the truth. If the i^{th} person claims that the thief is one person out of L_{i}, L_{i}+1, L_{i}+2, \cdots, R_{i}, determine how many people could be the thief. It is guaranteed that at least one person can be the thief. ------ Input Format ------ - First line will contain T, the number of test cases. Then the test cases follow. - First line of each test case will contain N, the number of people. N lines follow. - The i^{th} line contains two space-separated integers - L_{i} and R_{i}, the range of people amongst which the i^{th} person claims the thief is. ------ Output Format ------ - For each test case, in the first line, output K, the number of possible thieves. - In the next K lines, output the list of people that could be the thieves, in ascending order. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $3 ≤ N ≤ 10^{5}$ $1 ≤ L_{i} ≤ R_{i} ≤ N$ - Sum of $N$ over all test cases does not exceed $10^{5}$. - It is guaranteed that at least one person can be the thief. ----- Sample Input 1 ------ 1 4 2 2 1 1 1 3 1 3 ----- Sample Output 1 ------ 2 1 2 ----- explanation 1 ------ Test case $1$: Two people (numbered $1$ and $2$) can be thief: - Person $1$ may be the thief because, the other $3$ people claim that he may be the thief, whereas, he lies and claims that person $2$ is the thief. - Person $2$ may be the thief because, the other $3$ people claim that he may be the thief, whereas, he lies and claims that person $1$ is the thief. - Person $3$ cannot be the thief because, he claims that the thief lies in the range $[1, 3]$. If he were the thief, then his statement should be false. However, since 3 lies in $[1, 3]$, his statement would be true, which results in a contradiction. Furthermore, the first $2$ people do not claim that he is the thief. Therefore, he cannot be the thief. - Person $4$ cannot be the thief because the first $3$ people do not claim that he is the thief.
{"inputs": ["1\n4\n2 2\n1 1\n1 3\n1 3"], "outputs": ["2\n1\n2"]}
593
34
coding
Solve the programming task below in a Python markdown code block. We have N sticks with negligible thickness. The length of the i-th stick is A_i. Snuke wants to select four different sticks from these sticks and form a rectangle (including a square), using the sticks as its sides. Find the maximum possible area of the rectangle. -----Constraints----- - 4 \leq N \leq 10^5 - 1 \leq A_i \leq 10^9 - A_i is an integer. -----Input----- Input is given from Standard Input in the following format: N A_1 A_2 ... A_N -----Output----- Print the maximum possible area of the rectangle. If no rectangle can be formed, print 0. -----Sample Input----- 6 3 1 2 4 2 1 -----Sample Output----- 2 1 \times 2 rectangle can be formed.
{"inputs": ["4\n1 2 0 4", "4\n1 2 0 5", "4\n1 1 0 5", "4\n0 1 0 5", "4\n0 1 0 3", "4\n1 2 3 4", "4\n1 2 3 4\n", "4\n-1 1 0 3"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0", "0\n", "0\n"]}
195
127
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. Phantasialand boasts of its famous theme park. The park is frequently visited. It is quite large park that some tourists visit it more than once to fully appreciate its offerings. One day, our Chefs decided to visit the park. There are total n Chefs, i-th of them wants to visit the park t_{i} times. Usually, the entry ticket for the park is very expensive. Today, being a weekend, park had an interesting offer for the visitors, "1x Zahlen, 2x Spaß" (pay once, visit twice), i.e. you can get a second free visit after the first paid visit. The procedure for visiting the park and availing the offer is as follows. First time visitors should buy a ticket at the entrance of the park. Along with the ticket, you are offered an option of availing a voucher if you want a second visit. Enter the theme park, enjoy your visit. While returning make sure to sign your name in the voucher. Any unsigned voucher will not allowed to take out of the park. After the visit is done, the ticket counter takes back your ticket. If it is your second time visit, then the counter will take back your voucher. No new voucher will be provided to you as you have already availed the offer. You can avail the offer as many times as you wish in a day, i.e. offer is applicable for each visit with a paid ticket. Obviously, this procedure has a flaw. The counter doesn't ask you to sign your name on the voucher at the time of providing it to make sure that the person buying the ticket is the one signing the voucher. So, if more than one Chefs enter the park, they can exchange their vouchers while they are inside the park. Chefs thought of exploiting this flow. They wanted to buy minimum number of tickets. Can you help them in finding how many minimum tickets they should buy? Let us take an example. There are two Chef's, Alice and Bob. Alice wants to visit the park three times and Bob only once. For their first visits, each of them buys a ticket and obtains their vouchers and visits the park. After they have entered their park, Bob gives his voucher to Alice. Alice signs her name on her own voucher and on the voucher given by Bob. In this way, she has two vouchers, which she can use to visit the park two more times. So, in total by buying two tickets, Alice can visit three times and Bob once. ------ Input ------ The first line of the input contains a single integer n denoting the number of Chefs. The second line contains n space-separated integers t_{1}, t_{2}, ..., t_{n}, where t_{i} denotes the number of times i-th Chef wants to visit the park. ------ Output ------ Output a single integer corresponding to the minimum number of tickets Chefs needs to buy. ------ Constraints ------ $1 ≤ n ≤ 10^{5}$ $1 ≤ t_{i} ≤ 10^{4}$ ----- Sample Input 1 ------ 2 3 1 ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Example case 1. This example is already explained in the problem statement. ----- Sample Input 2 ------ 4 1 2 3 3 ----- Sample Output 2 ------ 5 ----- explanation 2 ------
{"inputs": ["2\n3 1", "2\n4 1", "2\n3 0", "2\n4 0", "2\n3 2", "2\n7 1", "2\n4 2", "2\n3 1"], "outputs": ["2", "3\n", "2\n", "2\n", "3\n", "4\n", "3\n", "2\n"]}
725
93
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese and Russian. In a far away dystopian world, the measure of the quality of a person’s life is the numbers of likes he gets for an article about their life. For a person to stay alive, he has to acquire at least L number of likes before D days pass. People in this world employ various techniques to increase the number of likes. One of the famous ones is to dis-like and re-like their own article once per day. On doing so you can assume that the number of likes for the post increase by a constant factor C. So if one starts with S likes on Day-1, he would have D2 = S + C * S likes on Day-2, D3 = D2 + D2 * C on Day-3 etc. You are to answer if the person would survive at the end of Day-D or not. Input First line contains a single positive integer T denoting the number of test cases. The following T lines represent a test case each. Each test case contains 4 space-separated integers L, D, S and C. Output For each test case, print a single line containing “ALIVE AND KICKING” if the person would live, otherwise print, “DEAD AND ROTTING”. Constraints 1 ≤ T ≤ 1000 1 ≤ L ≤ 1000000000 1 ≤ D ≤ 1000000000 1 ≤ S ≤ 1000000000 1 ≤ C ≤ 1000000000 ----- Sample Input 1 ------ 2 5 1 5 1 10 2 2 2 ----- Sample Output 1 ------ ALIVE AND KICKING DEAD AND ROTTING ----- explanation 1 ------ In the first case by the end of Day-1 we would be having S that is 5 number of likes, as it is ? L, the answer is ALIVE AND KICKING. In the second case, D2 =S + C*S, therefore D2 = 2 + 2 * 2 = 6, as 6 is less than 10, the answer is DEAD AND ROTTING.
{"inputs": ["2\n5 1 5 1\n9 4 3 2", "2\n5 2 5 1\n9 4 3 2", "2\n5 1 5 1\n4 2 2 2", "2\n5 1 5 1\n2 4 3 2", "2\n5 2 5 1\n9 3 3 2", "2\n5 3 5 1\n9 4 3 2", "2\n5 2 5 1\n1 4 5 2", "2\n5 4 5 1\n9 4 3 2"], "outputs": ["ALIVE AND KICKING\nALIVE AND KICKING\n", "ALIVE AND KICKING\nALIVE AND KICKING\n", "ALIVE AND KICKING\nALIVE AND KICKING\n", "ALIVE AND KICKING\nALIVE AND KICKING\n", "ALIVE AND KICKING\nALIVE AND KICKING\n", "ALIVE AND KICKING\nALIVE AND KICKING\n", "ALIVE AND KICKING\nALIVE AND KICKING\n", "ALIVE AND KICKING\nALIVE AND KICKING\n"]}
495
286
coding
Solve the programming task below in a Python markdown code block. Mike decided to teach programming to children in an elementary school. He knows that it is not an easy task to interest children in that age to code. That is why he decided to give each child two sweets. Mike has $n$ sweets with sizes $a_1, a_2, \ldots, a_n$. All his sweets have different sizes. That is, there is no such pair $(i, j)$ ($1 \leq i, j \leq n$) such that $i \ne j$ and $a_i = a_j$. Since Mike has taught for many years, he knows that if he gives two sweets with sizes $a_i$ and $a_j$ to one child and $a_k$ and $a_p$ to another, where $(a_i + a_j) \neq (a_k + a_p)$, then a child who has a smaller sum of sizes will be upset. That is, if there are two children who have different sums of sweets, then one of them will be upset. Apparently, Mike does not want somebody to be upset. Mike wants to invite children giving each of them two sweets. Obviously, he can't give one sweet to two or more children. His goal is to invite as many children as he can. Since Mike is busy preparing to his first lecture in the elementary school, he is asking you to find the maximum number of children he can invite giving each of them two sweets in such way that nobody will be upset. -----Input----- The first line contains one integer $n$ ($2 \leq n \leq 1\,000$) — the number of sweets Mike has. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^5$) — the sizes of the sweets. It is guaranteed that all integers are distinct. -----Output----- Print one integer — the maximum number of children Mike can invite giving each of them two sweets in such way that nobody will be upset. -----Examples----- Input 8 1 8 3 11 4 9 2 7 Output 3 Input 7 3 1 7 11 9 2 12 Output 2 -----Note----- In the first example, Mike can give $9+2=11$ to one child, $8+3=11$ to another one, and $7+4=11$ to the third child. Therefore, Mike can invite three children. Note that it is not the only solution. In the second example, Mike can give $3+9=12$ to one child and $1+11$ to another one. Therefore, Mike can invite two children. Note that it is not the only solution.
{"inputs": ["2\n2 1\n", "2\n2 1\n", "2\n3 1\n", "2\n3 2\n", "2\n5 2\n", "2\n7 2\n", "2\n4 1\n", "2\n1 2\n"], "outputs": ["1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n"]}
616
102
coding
Solve the programming task below in a Python markdown code block. Today, hedgehog Filya went to school for the very first time! Teacher gave him a homework which Filya was unable to complete without your help. Filya is given an array of non-negative integers a_1, a_2, ..., a_{n}. First, he pick an integer x and then he adds x to some elements of the array (no more than once), subtract x from some other elements (also, no more than once) and do no change other elements. He wants all elements of the array to be equal. Now he wonders if it's possible to pick such integer x and change some elements of the array using this x in order to make all elements equal. -----Input----- The first line of the input contains an integer n (1 ≤ n ≤ 100 000) — the number of integers in the Filya's array. The second line contains n integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9) — elements of the array. -----Output----- If it's impossible to make all elements of the array equal using the process given in the problem statement, then print "NO" (without quotes) in the only line of the output. Otherwise print "YES" (without quotes). -----Examples----- Input 5 1 3 3 2 1 Output YES Input 5 1 2 3 4 5 Output NO -----Note----- In the first sample Filya should select x = 1, then add it to the first and the last elements of the array and subtract from the second and the third elements.
{"inputs": ["1\n0\n", "1\n0\n", "1\n1\n", "2\n1 2\n", "2\n4 2\n", "2\n2 3\n", "2\n4 2\n", "2\n2 3\n"], "outputs": ["YES\n", "YES\n", "YES\n", "YES\n", "YES\n", "YES\n", "YES\n", "YES\n"]}
366
96
coding
Solve the programming task below in a Python markdown code block. Construct a function 'coordinates', that will return the distance between two points on a cartesian plane, given the x and y coordinates of each point. There are two parameters in the function, ```p1``` and ```p2```. ```p1``` is a list ```[x1,y1]``` where ```x1``` and ```y1``` are the x and y coordinates of the first point. ```p2``` is a list ```[x2,y2]``` where ```x2``` and ```y2``` are the x and y coordinates of the second point. The distance between the two points should be rounded to the `precision` decimal if provided, otherwise to the nearest integer. Also feel free to reuse/extend the following starter code: ```python def coordinates(p1, p2, precision=0): ```
{"functional": "_inputs = [[[3, 6], [14, 6]], [[-2, 5], [-2, -10], 2], [[1, 2], [4, 6]], [[5, 3], [10, 15], 2], [[-2, -10], [6, 5]], [[4, 7], [6, 2], 3], [[-4, -5], [-5, -4], 1], [[3, 8], [3, 8]], [[-3, 8], [3, 8]], [[3, 8], [3, -8]]]\n_outputs = [[11], [15], [5], [13], [17], [5.385], [1.4], [0], [6], [16]]\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(coordinates(*i), o[0])"}
185
329
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. You are given a rooted tree with $N$ nodes (numbered $1$ through $N$); the root is node $1$. For each valid $i$, node $i$ has weight $w_{i}$, which is either $0$ or $1$. We want to traverse the tree using depth first search. The order in which the nodes are visited is not uniquely defined, since we may visit the children of each node in an arbitrary order. Formally, the pseudocode of DFS-traversal is: function DFS(node n): node n is visited for each node s (s is a son of n) in some order: call DFS(s) return call DFS(root) For each possible DFS-traversal of the tree, consider the sequence of weights of nodes in the order in which they are visited; each node is visited exactly once, so this sequence has length $N$. Calculate the number of inversions for each such sequence. The minimum of these numbers is the *treeversion* of our tree. Find the treeversion of the given tree. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $w_{1}, w_{2}, \ldots, w_{N}$. Each of the following $N-1$ lines contains two space-separated integers $x$ and $y$ denoting that nodes $x$ and $y$ are connected by an edge. ------ Output ------ For each test case, print a single line containing one integer — the treeversion of the given tree. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $1 ≤ N ≤ 10^{5}$ $0 ≤ w_{i} ≤ 1$ for each valid $i$ $1 ≤ x, y ≤ N$ the graph on the input is a tree the sum of $N$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (50 points): $1 ≤ N ≤ 1,000$ the sum of $N$ over all test cases does not exceed $10,000$ Subtask #2 (50 points): original constraints ----- Sample Input 1 ------ 1 3 1 0 1 1 2 1 3 ----- Sample Output 1 ------ 1
{"inputs": ["1\n3\n1 0 1\n1 2\n1 3"], "outputs": ["1"]}
585
28
coding
Solve the programming task below in a Python markdown code block. Tokitsukaze and CSL are playing a little game of stones. In the beginning, there are n piles of stones, the i-th pile of which has a_i stones. The two players take turns making moves. Tokitsukaze moves first. On each turn the player chooses a nonempty pile and removes exactly one stone from the pile. A player loses if all of the piles are empty before his turn, or if after removing the stone, two piles (possibly empty) contain the same number of stones. Supposing that both players play optimally, who will win the game? Consider an example: n=3 and sizes of piles are a_1=2, a_2=3, a_3=0. It is impossible to choose the empty pile, so Tokitsukaze has two choices: the first and the second piles. If she chooses the first pile then the state will be [1, 3, 0] and it is a good move. But if she chooses the second pile then the state will be [2, 2, 0] and she immediately loses. So the only good move for her is to choose the first pile. Supposing that both players always take their best moves and never make mistakes, who will win the game? Note that even if there are two piles with the same number of stones at the beginning, Tokitsukaze may still be able to make a valid first move. It is only necessary that there are no two piles with the same number of stones after she moves. Input The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of piles. The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_1, a_2, …, a_n ≤ 10^9), which mean the i-th pile has a_i stones. Output Print "sjfnb" (without quotes) if Tokitsukaze will win, or "cslnb" (without quotes) if CSL will win. Note the output characters are case-sensitive. Examples Input 1 0 Output cslnb Input 2 1 0 Output cslnb Input 2 2 2 Output sjfnb Input 3 2 3 1 Output sjfnb Note In the first example, Tokitsukaze cannot take any stone, so CSL will win. In the second example, Tokitsukaze can only take a stone from the first pile, and then, even though they have no stone, these two piles will have the same number of stones, which implies CSL will win. In the third example, Tokitsukaze will win. Here is one of the optimal ways: * Firstly, Tokitsukaze can choose the first pile and take a stone from that pile. * Then, CSL can only choose the first pile, because if he chooses the second pile, he will lose immediately. * Finally, Tokitsukaze can choose the second pile, and then CSL will have no choice but to lose. In the fourth example, they only have one good choice at any time, so Tokitsukaze can make the game lasting as long as possible and finally win.
{"inputs": ["1\n1\n", "1\n2\n", "1\n3\n", "1\n5\n", "1\n4\n", "1\n6\n", "1\n9\n", "1\n0\n"], "outputs": ["sjfnb\n", "cslnb\n", "sjfnb\n", "sjfnb\n", "cslnb\n", "cslnb\n", "sjfnb\n", "cslnb\n"]}
702
102
coding
Solve the programming task below in a Python markdown code block. Read problems statements [Mandarin] , [Bengali] , [Hindi] , [Russian] and [Vietnamese] as well. Chef has bought a new robot, which will be used for delivering dishes to his customers. He started testing the robot by letting it move on a line. Initially, the robot is placed at the coordinate $x = X$. Then, it should execute a sequence of $N$ commands, described by a string $S$ with length $N$. Each character of this string is either 'L' or 'R', denoting that the robot should walk one step to the left (decreasing $x$ by $1$) or to the right (increasing $x$ by $1$), respectively. How many distinct points are visited by the robot when it has executed all the commands? A point $p$ is visited by the robot if $p$ is an integer and the robot's position before or after executing some command is $x = p$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $X$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing one integer ― the number of points visited by the robot. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100$ $|X| ≤ 1,000,000$ $S$ contains only characters 'L' and 'R' ------ Subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 2 6 10 RRLLLL 2 0 LL ----- Sample Output 1 ------ 5 3 ----- explanation 1 ------ Example case 1: The robot followed the path $10 \rightarrow 11 \rightarrow 12 \rightarrow 11 \rightarrow 10 \rightarrow 9 \rightarrow 8$. Example case 2: The robot followed the path $0 \rightarrow -1 \rightarrow -2$.
{"inputs": ["2\n6 10\nRRLLLL\n2 0\nLL"], "outputs": ["5\n3"]}
497
28
coding
Solve the programming task below in a Python markdown code block. Vasya has an array a_1, a_2, ..., a_n. You don't know this array, but he told you m facts about this array. The i-th fact is a triple of numbers t_i, l_i and r_i (0 ≤ t_i ≤ 1, 1 ≤ l_i < r_i ≤ n) and it means: * if t_i=1 then subbarray a_{l_i}, a_{l_i + 1}, ..., a_{r_i} is sorted in non-decreasing order; * if t_i=0 then subbarray a_{l_i}, a_{l_i + 1}, ..., a_{r_i} is not sorted in non-decreasing order. A subarray is not sorted if there is at least one pair of consecutive elements in this subarray such that the former is greater than the latter. For example if a = [2, 1, 1, 3, 2] then he could give you three facts: t_1=1, l_1=2, r_1=4 (the subarray [a_2, a_3, a_4] = [1, 1, 3] is sorted), t_2=0, l_2=4, r_2=5 (the subarray [a_4, a_5] = [3, 2] is not sorted), and t_3=0, l_3=3, r_3=5 (the subarray [a_3, a_5] = [1, 3, 2] is not sorted). You don't know the array a. Find any array which satisfies all the given facts. Input The first line contains two integers n and m (2 ≤ n ≤ 1000, 1 ≤ m ≤ 1000). Each of the next m lines contains three integers t_i, l_i and r_i (0 ≤ t_i ≤ 1, 1 ≤ l_i < r_i ≤ n). If t_i = 1 then subbarray a_{l_i}, a_{l_i + 1}, ... , a_{r_i} is sorted. Otherwise (if t_i = 0) subbarray a_{l_i}, a_{l_i + 1}, ... , a_{r_i} is not sorted. Output If there is no array that satisfies these facts in only line print NO (in any letter case). If there is a solution, print YES (in any letter case). In second line print n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9) — the array a, satisfying all the given facts. If there are multiple satisfying arrays you can print any of them. Examples Input 7 4 1 1 3 1 2 5 0 5 6 1 6 7 Output YES 1 2 2 3 5 4 4 Input 4 2 1 1 4 0 2 3 Output NO
{"inputs": ["2 1\n1 1 2\n", "5 1\n0 1 3\n", "2 1\n0 1 2\n", "4 1\n0 1 2\n", "4 1\n0 1 1\n", "124 1\n1 1 2\n", "124 1\n0 1 2\n", "152 1\n0 1 2\n"], "outputs": ["YES\n2 2 \n", "YES\n5 4 3 2 1 \n", "YES\n2 1 \n", "YES\n4 3 2 1 ", "NO\n", "YES\n124 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 \n", "YES\n124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 ", "YES\n152 151 150 149 148 147 146 145 144 143 142 141 140 139 138 137 136 135 134 133 132 131 130 129 128 127 126 125 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 "]}
676
1,446
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an m x n binary matrix grid. A move consists of choosing any row or column and toggling each value in that row or column (i.e., changing all 0's to 1's, and all 1's to 0's). Every row of the matrix is interpreted as a binary number, and the score of the matrix is the sum of these numbers. Return the highest possible score after making any number of moves (including zero moves).   Please complete the following python code precisely: ```python class Solution: def matrixScore(self, grid: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(grid = [[0,0,1,1],[1,0,1,0],[1,1,0,0]]) == 39\n assert candidate(grid = [[0]]) == 1\n\n\ncheck(Solution().matrixScore)"}
144
66
coding
Solve the programming task below in a Python markdown code block. There is a house with $n$ flats situated on the main street of Berlatov. Vova is watching this house every night. The house can be represented as an array of $n$ integer numbers $a_1, a_2, \dots, a_n$, where $a_i = 1$ if in the $i$-th flat the light is on and $a_i = 0$ otherwise. Vova thinks that people in the $i$-th flats are disturbed and cannot sleep if and only if $1 < i < n$ and $a_{i - 1} = a_{i + 1} = 1$ and $a_i = 0$. Vova is concerned by the following question: what is the minimum number $k$ such that if people from exactly $k$ pairwise distinct flats will turn off the lights then nobody will be disturbed? Your task is to find this number $k$. -----Input----- The first line of the input contains one integer $n$ ($3 \le n \le 100$) — the number of flats in the house. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_i \in \{0, 1\}$), where $a_i$ is the state of light in the $i$-th flat. -----Output----- Print only one integer — the minimum number $k$ such that if people from exactly $k$ pairwise distinct flats will turn off the light then nobody will be disturbed. -----Examples----- Input 10 1 1 0 1 1 0 1 0 1 0 Output 2 Input 5 1 1 0 0 0 Output 0 Input 4 1 1 1 1 Output 0 -----Note----- In the first example people from flats $2$ and $7$ or $4$ and $7$ can turn off the light and nobody will be disturbed. It can be shown that there is no better answer in this example. There are no disturbed people in second and third examples.
{"inputs": ["3\n0 0 0\n", "3\n1 0 1\n", "3\n1 1 1\n", "3\n0 1 1\n", "3\n1 1 0\n", "3\n1 0 0\n", "3\n0 1 0\n", "3\n0 0 1\n"], "outputs": ["0\n", "1\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
471
118
coding
Solve the programming task below in a Python markdown code block. In 2937, DISCO creates a new universe called DISCOSMOS to celebrate its 1000-th anniversary. DISCOSMOS can be described as an H \times W grid. Let (i, j) (1 \leq i \leq H, 1 \leq j \leq W) denote the square at the i-th row from the top and the j-th column from the left. At time 0, one robot will be placed onto each square. Each robot is one of the following three types: * Type-H: Does not move at all. * Type-R: If a robot of this type is in (i, j) at time t, it will be in (i, j+1) at time t+1. If it is in (i, W) at time t, however, it will be instead in (i, 1) at time t+1. (The robots do not collide with each other.) * Type-D: If a robot of this type is in (i, j) at time t, it will be in (i+1, j) at time t+1. If it is in (H, j) at time t, however, it will be instead in (1, j) at time t+1. There are 3^{H \times W} possible ways to place these robots. In how many of them will every square be occupied by one robot at times 0, T, 2T, 3T, 4T, and all subsequent multiples of T? Since the count can be enormous, compute it modulo (10^9 + 7). Constraints * 1 \leq H \leq 10^9 * 1 \leq W \leq 10^9 * 1 \leq T \leq 10^9 * H, W, T are all integers. Input Input is given from Standard Input in the following format: H W T Output Print the number of ways to place the robots that satisfy the condition, modulo (10^9 + 7). Examples Input 2 2 1 Output 9 Input 869 120 1001 Output 672919729
{"inputs": ["2 2 1", "7 48 1110", "3 29 1101", "5 48 1110", "3 22 1101", "5 70 1110", "3 35 1101", "11 6 1111"], "outputs": ["9", "404785941\n", "582975580\n", "220543434\n", "899722747\n", "284885353\n", "133144039\n", "533072736\n"]}
508
177
coding
Solve the programming task below in a Python markdown code block. Dreamoon is a big fan of the Codeforces contests. One day, he claimed that he will collect all the places from $1$ to $54$ after two more rated contests. It's amazing! Based on this, you come up with the following problem: There is a person who participated in $n$ Codeforces rounds. His place in the first round is $a_1$, his place in the second round is $a_2$, ..., his place in the $n$-th round is $a_n$. You are given a positive non-zero integer $x$. Please, find the largest $v$ such that this person can collect all the places from $1$ to $v$ after $x$ more rated contests. In other words, you need to find the largest $v$, such that it is possible, that after $x$ more rated contests, for each $1 \leq i \leq v$, there will exist a contest where this person took the $i$-th place. For example, if $n=6$, $x=2$ and $a=[3,1,1,5,7,10]$ then answer is $v=5$, because if on the next two contest he will take places $2$ and $4$, then he will collect all places from $1$ to $5$, so it is possible to get $v=5$. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 5$) denoting the number of test cases in the input. Each test case contains two lines. The first line contains two integers $n, x$ ($1 \leq n, x \leq 100$). The second line contains $n$ positive non-zero integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 100$). -----Output----- For each test case print one line containing the largest $v$, such that it is possible that after $x$ other contests, for each $1 \leq i \leq v$, there will exist a contest where this person took the $i$-th place. -----Example----- Input 5 6 2 3 1 1 5 7 10 1 100 100 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 4 57 80 60 40 20 Output 5 101 2 2 60 -----Note----- The first test case is described in the statement. In the second test case, the person has one hundred future contests, so he can take place $1,2,\ldots,99$ and place $101$ on them in some order, to collect places $1,2,\ldots,101$.
{"inputs": ["1\n1 5\n6\n", "1\n1 1\n2\n", "1\n1 1\n2\n", "1\n1 5\n6\n", "1\n1 9\n6\n", "1\n1 3\n6\n", "1\n1 3\n9\n", "1\n1 5\n4\n"], "outputs": ["6\n", "2\n", "2\n", "6\n", "10\n", "3\n", "3\n", "6\n"]}
652
119
coding
Solve the programming task below in a Python markdown code block. Division of Big Integers Given two integers $A$ and $B$, compute the quotient, $\frac{A}{B}$. Round down to the nearest decimal. Input Two integers $A$ and $B$ separated by a space character are given in a line. Output Print the quotient in a line. Constraints * $-1 \times 10^{1000} \leq A, B \leq 10^{1000}$ * $B \ne 0$ Sample Input 1 5 8 Sample Output 1 0 Sample Input 2 100 25 Sample Output 2 4 Sample Input 3 -1 3 Sample Output 3 0 Sample Input 4 12 -3 Sample Output 4 -4 Example Input 5 8 Output 0
{"inputs": ["9 6", "2 1", "1 4", "2 4", "2 2", "5 3", "5 6", "9 8"], "outputs": ["1\n", "2\n", "0\n", "0\n", "1\n", "1\n", "0\n", "1\n"]}
205
78
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given two positive integer arrays nums and target, of the same length. In one operation, you can choose any two distinct indices i and j where 0 <= i, j < nums.length and: set nums[i] = nums[i] + 2 and set nums[j] = nums[j] - 2. Two arrays are considered to be similar if the frequency of each element is the same. Return the minimum number of operations required to make nums similar to target. The test cases are generated such that nums can always be similar to target.   Please complete the following python code precisely: ```python class Solution: def makeSimilar(self, nums: List[int], target: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [8,12,6], target = [2,14,10]) == 2\n assert candidate(nums = [1,2,5], target = [4,1,3]) == 1\n assert candidate(nums = [1,1,1,1,1], target = [1,1,1,1,1]) == 0\n\n\ncheck(Solution().makeSimilar)"}
166
105
coding
Solve the programming task below in a Python markdown code block. Lucas numbers are numbers in a sequence defined like this: ``` L(0) = 2 L(1) = 1 L(n) = L(n-1) + L(n-2) ``` Your mission is to complete the function that returns the `n`th term of this sequence. **Note:** It should work for negative numbers as well; how you do this is you flip the equation around, so for negative numbers: `L(n) = L(n+2) - L(n+1)` ## Examples ``` L(-10) = 123 L(-5) = -11 L(-1) = -1 L(0) = 2 L(1) = 1 L(5) = 11 L(10) = 123 ``` Also feel free to reuse/extend the following starter code: ```python def lucasnum(n): ```
{"functional": "_inputs = [[-10], [-1]]\n_outputs = [[123], [-1]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(lucasnum(*i), o[0])"}
227
165
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef's apartment consists of $M$ floors (numbered $1$ through $M$), and there's an elevator that is used to move between different floors. The elevator is connected with a computer which registers its movement in a sequence $B$. Whenever the elevator moves to a different floor, the computer appends the new floor number to sequence $B$. Currently, the sequence $B$ has $N$ elements. Unfortunately, the computer is infected with a virus which replaced some elements of $B$ by $-1$s. Chef now wants to know what could be the minimum number of times the elevator has changed its direction. That is, how many times the elevator was going up then started going down and vice versa. Help chef by answering his question or determine that the sequence $B$ is invalid. ------ Input: ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two integers $N$ and $M$. The second line contains $N$ space-separated integers $B_{1}, B_{2}, \ldots, B_{N}$. ------ Output: ------ For each test case, print a single line containing one integer ― the minimum number of times the elevator has changed its direction or $-1$ if the given $B$ sequence is invalid. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 10^{5}$ $2 ≤ M ≤ 10^{5}$ $1 ≤ B_{i} ≤ M$ or $B_{i} = -1$ for each valid $i$ sum of $N$ over all test-cases doesn't exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (50 points): $N ≤ 1,000$ the sum of $N$ over all test cases does not exceed $10,000$ Subtask #2 (50 points): Original constraints ------ Sample Input: ------ 5 4 5 2 3 4 3 4 5 2 -1 4 3 4 5 1 -1 -1 5 5 4 -1 -1 3 -1 -1 5 5 -1 -1 3 -1 -1 ------ Sample Output: ------ 1 1 -1 1 0
{"inputs": ["5\n4 5\n2 3 4 3\n4 5\n2 -1 4 3\n4 5\n1 -1 -1 5\n5 4\n-1 -1 3 -1 -1\n5 5\n-1 -1 3 -1 -1\n"], "outputs": ["1\n1\n-1\n1\n0\n"]}
569
89
coding
Solve the programming task below in a Python markdown code block. A list of names is taken as input, in which a particular name can occur multiple times. You need to arrange these names as they will appear in the dictionary and also print the number of times the arranged names appear in the list taken as input. Input: The first line of input contains an integer, t, which denotes the number of names that will follow. Then, t lines follow, each containing a name, in the form of a character string S. Output: The output contains the names as they would appear in the dictionary, followed by the frequency of that name in the list. Constraints: 1 ≤ t ≤ 100000 1 ≤ |S| ≤30 S contains only lower case characters. SAMPLE INPUT 3 ritesh sahil ritesh SAMPLE OUTPUT ritesh 2 sahil 1 Explanation Test Case #1: As the name starts from 'r' comes first then 's' in dictionary and in this case the 'ritesh' name is given 2 times and 'sahil' is given only once so their frequency is coming and remove the duplicate rows.
{"inputs": ["3\nritesh\nsahil\nritesh"], "outputs": ["ritesh 2\nsahil 1"]}
252
31
coding
Solve the programming task below in a Python markdown code block. # Task Consider the following algorithm for constructing 26 strings S(1) .. S(26): ``` S(1) = "a"; For i in [2, 3, ..., 26]: S(i) = S(i - 1) + character(i) + S(i - 1).``` For example: ``` S(1) = "a" S(2) = S(1) + "b" + S(1) = "a" + "b" + "a" = "aba" S(3) = S(2) + "c" + S(2) = "aba" + "c" +"aba" = "abacaba" ... S(26) = S(25) + "z" + S(25)``` Finally, we got a long string S(26). Your task is to find the `k`th symbol (indexing from 1) in the string S(26). All strings consist of lowercase letters only. # Input / Output - `[input]` integer `k` 1 ≤ k < 2^(26) - `[output]` a string(char in C#) the `k`th symbol of S(26) Also feel free to reuse/extend the following starter code: ```python def abacaba(k): ```
{"functional": "_inputs = [[1], [2], [3], [4], [5], [6], [7], [8], [12], [16]]\n_outputs = [['a'], ['b'], ['a'], ['c'], ['a'], ['b'], ['a'], ['d'], ['c'], ['e']]\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(abacaba(*i), o[0])"}
308
211
coding
Solve the programming task below in a Python markdown code block. *CodeChef recently revamped its [practice page] to make it easier for users to identify the next problems they should solve by introducing some new features:* *Recent Contest Problems - Contains only problems from the last 2 contests* *Separate Un-Attempted, Attempted, and All tabs* *Problem Difficulty Rating - The Recommended dropdown menu has various difficulty ranges so that you can attempt the problems most suited to your experience* *Popular Topics and Tags* Chef has been participating regularly in rated contests but missed the last two contests due to his college exams. He now wants to solve them and so he visits the practice page to view these [problems]. Given a list of N contest codes, where each contest code is either START38 or LTIME108, help Chef count how many problems were featured in each of the contests. ------ Input Format ------ - First line will contain T, number of test cases. Then the test cases follow. - Each test case contains of two lines of input. - First line of input contains the total count of problems that appeared in the two recent contests - N. - Second line of input contains the list of N contest codes. Each code is either START38 or LTIME108, to which each problem belongs. ------ Output Format ------ For each test case, output two integers in a single new line - the first integer should be the number of problems in START38, and the second integer should be the number of problems in LTIME108. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 1000$ - Each of the contest codes will be either START38 or LTIME108. ----- Sample Input 1 ------ 4 3 START38 LTIME108 START38 4 LTIME108 LTIME108 LTIME108 START38 2 LTIME108 LTIME108 6 START38 LTIME108 LTIME108 LTIME108 START38 LTIME108 ----- Sample Output 1 ------ 2 1 1 3 0 2 2 4 ----- explanation 1 ------ Test case $1$: There are $2$ START38s in the input, which means that there were $2$ problems in START38. Similarly, there was $1$ problem in LTIME108. Test case $2$: There is $1$ START38 in the input, which means that there was $1$ problem in START38. Similarly, there were $3$ problems in LTIME108. Test case $3$: There are no START38s in the input, which means that were $0$ problems in START38. Similarly, there were $2$ problems in LTIME108. Test case $4$: There are $2$ START38s in the input, which means that there were $2$ problems in START38. Similarly, there were $4$ problems in LTIME108.
{"inputs": ["4\n3\nSTART38 LTIME108 START38\n4\nLTIME108 LTIME108 LTIME108 START38\n2\nLTIME108 LTIME108\n6\nSTART38 LTIME108 LTIME108 LTIME108 START38 LTIME108\n"], "outputs": ["2 1\n1 3\n0 2\n2 4\n"]}
666
105
coding
Solve the programming task below in a Python markdown code block. Chef was driving on a highway at a speed of X km/hour. To avoid accidents, there are fine imposed on overspeeding as follows: No fine if the speed of the car ≤ 70 km/hour. Rs 500 fine if the speed of the car is strictly greater than 70 and ≤ 100. Rs 2000 fine if the speed of the car is strictly greater than 100. Determine the fine Chef needs to pay. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of a single integer X denoting the speed of Chef's car. ------ Output Format ------ For each test case, output the fine paid by Chef. ------ Constraints ------ $1 ≤ T ≤ 200$ $1 ≤ X ≤ 200$ ----- Sample Input 1 ------ 7 40 110 70 100 69 101 85 ----- Sample Output 1 ------ 0 2000 0 500 0 2000 500 ----- explanation 1 ------ Test case $1$: The speed is $≤ 70$. Thus, Chef does not need to pay any fine. Test case $2$: The speed is greater than $100$. Thus, Chef needs to pay $2000$ as fine. Test case $3$: The speed is $≤ 70$. Thus, Chef does not need to pay any fine. Test case $4$: The speed is greater than $70$ and $≤ 100$. Thus, Chef needs to pay $500$ as fine amount. Test case $5$: The speed is $≤ 70$. Thus, Chef does not need to pay any fine. Test case $6$: The speed is greater than $100$. Thus, Chef needs to pay $2000$ as fine. Test case $7$: The speed is greater than $70$ and $≤ 100$. Thus, Chef needs to pay $500$ as fine amount.
{"inputs": ["7\n40\n110\n70\n100\n69\n101\n85\n"], "outputs": ["0\n2000\n0\n500\n0\n2000\n500\n"]}
481
60
coding
Solve the programming task below in a Python markdown code block. In this kata, you will do addition and subtraction on a given string. The return value must be also a string. **Note:** the input will not be empty. ## Examples ``` "1plus2plus3plus4" --> "10" "1plus2plus3minus4" --> "2" ``` Also feel free to reuse/extend the following starter code: ```python def calculate(s): ```
{"functional": "_inputs = [['1plus2plus3plus4'], ['1minus2minus3minus4'], ['1plus2plus3minus4'], ['1minus2plus3minus4'], ['1plus2minus3plus4minus5']]\n_outputs = [['10'], ['-8'], ['2'], ['-2'], ['-1']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(calculate(*i), o[0])"}
103
211
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Mandarin], [Vietnamese], and [Russian] as well. Chef loves Chess and has thus invented a new piece named "Disabled King". Let's denote the cell at the intersection of the i-th column from the left and j-th row from the top by (i, j). If he is currently in cell (x,y), the disabled king can move to the following positions in one move (provided that he remains in the chessboard): (x,y+1) (x,y-1) (x+1,y+1) (x+1,y-1) (x-1,y+1) (x-1,y-1) In short, the Disabled King cannot move horizontally. In an N \times N chessboard, the Disabled King is currently situated at the top-left corner (cell (1, 1)) and wants to reach the top-right corner (cell (N, 1)). Determine the minimum number of moves in which the task can be accomplished. ------ Input Format ------ - The first line will contain T, the number of test cases. Then the test cases follow. - Each test case contains a single integer N in a separate line. ------ Output Format ------ Output the minimum number of moves to get from the top-left cell to the top-right one. ------ Constraints ------ $1 ≤ T ≤ 500$ $2 ≤ N ≤ 500$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ 2 2 ----- explanation 1 ------ Test case 1: Initially chef is at $(1, 1)$. He cannot directly move to $(2, 1)$ as the disabled king cannot move horizontally. So he needs at least $2$ moves to reach $(2, 1)$. And that can be achieved by first moving to $(1, 2)$ and then moving to $(2, 1)$ from there. Test case 2: Clearly we cannot reach $(3, 1)$ from $(1, 1)$ in just one move. We require at least $2$ moves. And this can be achieved by first moving to $(2, 2)$ and then moving to $(3, 1)$ from there.
{"inputs": ["2\n2\n3\n"], "outputs": ["2\n2\n"]}
478
20
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a 2D array of integers envelopes where envelopes[i] = [wi, hi] represents the width and the height of an envelope. One envelope can fit into another if and only if both the width and height of one envelope are greater than the other envelope's width and height. Return the maximum number of envelopes you can Russian doll (i.e., put one inside the other). Note: You cannot rotate an envelope.   Please complete the following python code precisely: ```python class Solution: def maxEnvelopes(self, envelopes: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(envelopes = [[5,4],[6,4],[6,7],[2,3]]) == 3\n assert candidate(envelopes = [[1,1],[1,1],[1,1]]) == 1\n\n\ncheck(Solution().maxEnvelopes)"}
139
73
coding
Solve the programming task below in a Python markdown code block. Problem statement JOI decided to start a new social game from tomorrow. In this social game, you can log in up to once a day, and you will get A coins each time you log in. Also, if you log in for 7 consecutive days from Monday to Sunday, you will get an additional B coins each time. No other coins will be given. Tomorrow is Monday. Find the minimum number of times JOI must log in to get at least C coins. Constraint * 1 ≤ A ≤ 1000 * 0 ≤ B ≤ 1000 * 1 ≤ C ≤ 1000000 (= 10 ^ 6) Input Output input Input is given from standard input in the following format. A B C output Output the minimum number of times JOI must log in to get at least C coins. <!- Subtask 1. (40 points) B = 0 2. (60 points) There are no additional restrictions. -> Input / output example Input example 1 3 0 10 Output example 1 Four * I want to get 3 coins per login and collect 10 coins. * JOI can get 12 coins by logging in for 4 consecutive days from Monday. * Since you cannot get more than 10 coins by logging in 3 times or less, the minimum number of times JOI must log in is 4. Therefore, 4 is output. Input example 2 1 2 10 Output example 2 8 * You can get 1 coin for each login. Apart from that, you can get 2 coins by logging in for a week in a row. I want to collect 10 coins. * If you log in consecutively from Monday to Sunday, you will get 2 coins in addition to 7 daily coins, so you will get a total of 9 coins. Therefore, if you log in one more time, you will get 10 coins. * Since you cannot get more than 10 coins by logging in 7 times or less, the minimum number of times JOI must log in is 8. Therefore, 8 is output. Creative Commons License Information Olympics Japan Committee work "18th Japan Information Olympics JOI 2018/2019 Qualifying Competition Tasks" Example Input 3 0 10 Output 4
{"inputs": ["4 0 2", "0 1 3", "3 0 19", "2 0 19", "4 0 19", "8 0 22", "6 0 43", "2 0 31"], "outputs": ["1\n", "21\n", "7\n", "10\n", "5\n", "3\n", "8\n", "16\n"]}
536
103
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a strictly increasing sequence of integers $A_{1}, A_{2}, \ldots, A_{N}$. Your task is to compress this sequence. The *compressed form* of this sequence is a sequence of ranges separated by commas (characters ','). A *range* is either an integer or a pair of integers separated by three dots (the string "..."). When each range a...b in the compressed form is decompressed into the subsequence $(a, a+1, \ldots, b)$, we should obtain the (comma-separated) sequence $A$ again. For each maximal contiguous subsequence $(a, a+1, \ldots, b)$ of $A$ such that $b ≥ a+2$, the compressed form of $A$ must contain the range a...b; if $b ≤ a+1$, such a sequence should not be compressed into a range. A contiguous subsequence is maximal if it cannot be extended by at least one element of $A$ next to it. It can be proved that the compressed form of any sequence is unique (i.e. well-defined). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. ------ Output ------ For each test case, print a single line containing one string ― the compressed form of the given sequence. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100$ $1 ≤ A_{i} ≤ 1,000$ for each valid $i$ $A_{1} < A_{2} < \ldots < A_{N}$ ------ Subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 3 12 1 2 3 5 6 8 9 10 11 12 15 17 4 4 5 7 8 1 4 ----- Sample Output 1 ------ 1...3,5,6,8...12,15,17 4,5,7,8 4 ----- explanation 1 ------ Example case 1: - $(1, 2, 3)$ is a contiguous subsequence with length $3$, so it is replaced by the string "1...3" - $(5, 6)$ is a contiguous subsequence, but only with length $2$, so it is not compressed into a range - $(8, 9, 10, 11, 12)$ is a contiguous subsequence with length $5$, so it is replaced by "8...12" - the elements $15$, $17$ are unaffected
{"inputs": ["3\n12\n1 2 3 5 6 8 9 10 11 12 15 17\n4\n4 5 7 8\n1\n4"], "outputs": ["1...3,5,6,8...12,15,17\n4,5,7,8\n4"]}
677
85
coding
Solve the programming task below in a Python markdown code block. You are given strings S and T consisting of lowercase English letters. You can perform the following operation on S any number of times: Operation: Choose two distinct lowercase English letters c_1 and c_2, then replace every occurrence of c_1 with c_2, and every occurrence of c_2 with c_1. Determine if S and T can be made equal by performing the operation zero or more times. -----Constraints----- - 1 \leq |S| \leq 2 \times 10^5 - |S| = |T| - S and T consists of lowercase English letters. -----Input----- Input is given from Standard Input in the following format: S T -----Output----- If S and T can be made equal, print Yes; otherwise, print No. -----Sample Input----- azzel apple -----Sample Output----- Yes azzel can be changed to apple, as follows: - Choose e as c_1 and l as c_2. azzel becomes azzle. - Choose z as c_1 and p as c_2. azzle becomes apple.
{"inputs": ["x\nz\n", "h\nh\n", "aaa\nxxy\n", "uvan\naimq\n", "azzel\napqle", "azzle\napqle", "azzle\napqld", "azzle\ndlqpa"], "outputs": ["Yes\n", "Yes\n", "No\n", "Yes\n", "No\n", "No\n", "No\n", "No\n"]}
246
97
coding
Solve the programming task below in a Python markdown code block. Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n × m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office. Input The first line contains 2 space-separated numbers n and m (1 ≤ n, m ≤ 25) — the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free. Output Output one number — the maximum possible perimeter of a bargaining table for Bob's office room. Examples Input 3 3 000 010 000 Output 8 Input 5 4 1100 0000 0000 0000 0000 Output 16
{"inputs": ["1 1\n0\n", "3 3\n000\n110\n000\n", "4 2\n00\n10\n11\n00\n", "3 3\n000\n010\n010\n", "3 3\n000\n010\n011\n", "3 3\n100\n010\n011\n", "3 3\n101\n010\n011\n", "3 3\n000\n010\n000\n"], "outputs": ["4", "8", "6", "8\n", "8\n", "6\n", "6\n", "8"]}
317
168
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. For a string sequence, a string word is k-repeating if word concatenated k times is a substring of sequence. The word's maximum k-repeating value is the highest value k where word is k-repeating in sequence. If word is not a substring of sequence, word's maximum k-repeating value is 0. Given strings sequence and word, return the maximum k-repeating value of word in sequence.   Please complete the following python code precisely: ```python class Solution: def maxRepeating(self, sequence: str, word: str) -> int: ```
{"functional": "def check(candidate):\n assert candidate(sequence = \"ababc\", word = \"ab\") == 2\n assert candidate(sequence = \"ababc\", word = \"ba\") == 1\n assert candidate(sequence = \"ababc\", word = \"ac\") == 0\n\n\ncheck(Solution().maxRepeating)"}
135
74
coding
Solve the programming task below in a Python markdown code block. Let's consider a permutation P = {$p_{1}$,$ p_{2}$, ..., $p_{N}$} of the set of N = {1, 2, 3, ..., N} elements . P is called a magic set if it satisfies both of the following constraints: Given a set of K integers, the elements in positions $a_{1}$, $a_{2}$, ..., $a_{K}$ are less than their adjacent elements, i.e., $p_{ai-1} > p_{ai} < p_{ai+1}$ Given a set of L integers, elements in positions $b_{1}$, $b_{2}$, ..., $b_{L}$ are greater than their adjacent elements, i.e., $p_{bi-1} < p_{bi} > p_{bi+1}$ How many such magic sets are there? Input Format The first line of input contains three integers N, K, L separated by a single space. The second line contains K integers, $a_{1}$, $a_{2$}, ... $a_{K}$ each separated by single space. the third line contains L integers, $b_{1}$, $b_{2}$, ... $b_{L}$ each separated by single space. Output Format Output the answer modulo 1000000007 (10^{9}+7). Constraints 3 <= N <= 5000 1 <= K, L <= 5000 2 <= $a_{i}$, $b_{j}$ <= N-1, where i ∈ [1, K] AND j ∈ [1, L] Sample Input #00 4 1 1 2 3 Sample Output #00 5 Explanation #00 Here, N = 4 $a_{1}$ = 2 and $b_{1}$ = 3. The 5 permutations of {1,2,3,4} that satisfy the condition are 2 1 4 3 3 2 4 1 4 2 3 1 3 1 4 2 4 1 3 2 Sample Input #01 10 2 2 2 4 3 9 Sample Output #01 161280
{"inputs": ["4 1 1\n2\n3\n", "10 2 2\n2 4\n3 9\n"], "outputs": ["5\n", "161280\n"]}
520
48
coding
Solve the programming task below in a Python markdown code block. We saw the little game Marmot made for Mole's lunch. Now it's Marmot's dinner time and, as we all know, Marmot eats flowers. At every dinner he eats some red and white flowers. Therefore a dinner can be represented as a sequence of several flowers, some of them white and some of them red. But, for a dinner to be tasty, there is a rule: Marmot wants to eat white flowers only in groups of size k. Now Marmot wonders in how many ways he can eat between a and b flowers. As the number of ways could be very large, print it modulo 1000000007 (10^9 + 7). -----Input----- Input contains several test cases. The first line contains two integers t and k (1 ≤ t, k ≤ 10^5), where t represents the number of test cases. The next t lines contain two integers a_{i} and b_{i} (1 ≤ a_{i} ≤ b_{i} ≤ 10^5), describing the i-th test. -----Output----- Print t lines to the standard output. The i-th line should contain the number of ways in which Marmot can eat between a_{i} and b_{i} flowers at dinner modulo 1000000007 (10^9 + 7). -----Examples----- Input 3 2 1 3 2 3 4 4 Output 6 5 5 -----Note----- For K = 2 and length 1 Marmot can eat (R). For K = 2 and length 2 Marmot can eat (RR) and (WW). For K = 2 and length 3 Marmot can eat (RRR), (RWW) and (WWR). For K = 2 and length 4 Marmot can eat, for example, (WWWW) or (RWWR), but for example he can't eat (WWWR).
{"inputs": ["1 1\n1 3\n", "1 1\n1 3\n", "1 1\n2 3\n", "1 2\n2 3\n", "1 1\n3 3\n", "1 2\n2 4\n", "1 2\n3 6\n", "1 1\n1 1\n"], "outputs": ["14\n", "14\n", "12\n", "5\n", "8\n", "10\n", "29\n", "2\n"]}
446
123
coding
Solve the programming task below in a Python markdown code block. Tom is interested in power consumption of his favourite laptop. His laptop has three modes. In normal mode laptop consumes P1 watt per minute. T1 minutes after Tom moved the mouse or touched the keyboard for the last time, a screensaver starts and power consumption changes to P2 watt per minute. Finally, after T2 minutes from the start of the screensaver, laptop switches to the "sleep" mode and consumes P3 watt per minute. If Tom moves the mouse or touches the keyboard when the laptop is in the second or in the third mode, it switches to the first (normal) mode. Tom's work with the laptop can be divided into n time periods [l1, r1], [l2, r2], ..., [ln, rn]. During each interval Tom continuously moves the mouse and presses buttons on the keyboard. Between the periods Tom stays away from the laptop. Find out the total amount of power consumed by the laptop during the period [l1, rn]. Input The first line contains 6 integer numbers n, P1, P2, P3, T1, T2 (1 ≤ n ≤ 100, 0 ≤ P1, P2, P3 ≤ 100, 1 ≤ T1, T2 ≤ 60). The following n lines contain description of Tom's work. Each i-th of these lines contains two space-separated integers li and ri (0 ≤ li < ri ≤ 1440, ri < li + 1 for i < n), which stand for the start and the end of the i-th period of work. Output Output the answer to the problem. Examples Input 1 3 2 1 5 10 0 10 Output 30 Input 2 8 4 2 5 10 20 30 50 100 Output 570
{"inputs": ["1 5 2 1 6 3\n2 4\n", "1 0 2 1 6 9\n1 4\n", "1 5 0 1 6 3\n3 4\n", "1 2 0 1 6 3\n3 4\n", "1 5 2 1 6 3\n1 4\n", "1 3 2 1 5 9\n1 4\n", "1 3 2 1 6 9\n1 4\n", "1 5 0 1 6 3\n2 4\n"], "outputs": ["10\n", "0\n", "5\n", "2\n", "15\n", "9\n", "9\n", "10\n"]}
413
185
coding
Solve the programming task below in a Python markdown code block. You are at a water bowling training. There are l people who play with their left hand, r people, who play with their right hand, and a ambidexters, who can play with left or right hand. The coach decided to form a team of even number of players, exactly half of the players should play with their right hand, and exactly half of the players should play with their left hand. One player should use only on of his hands. Ambidexters play as well with their right hand as with their left hand. In the team, an ambidexter can play with their left hand, or with their right hand. Please find the maximum possible size of the team, where equal number of players use their left and right hands, respectively. -----Input----- The only line contains three integers l, r and a (0 ≤ l, r, a ≤ 100) — the number of left-handers, the number of right-handers and the number of ambidexters at the training. -----Output----- Print a single even integer — the maximum number of players in the team. It is possible that the team can only have zero number of players. -----Examples----- Input 1 4 2 Output 6 Input 5 5 5 Output 14 Input 0 2 0 Output 0 -----Note----- In the first example you can form a team of 6 players. You should take the only left-hander and two ambidexters to play with left hand, and three right-handers to play with right hand. The only person left can't be taken into the team. In the second example you can form a team of 14 people. You have to take all five left-handers, all five right-handers, two ambidexters to play with left hand and two ambidexters to play with right hand.
{"inputs": ["1 4 2\n", "5 5 5\n", "0 2 0\n", "0 0 0\n", "1 1 1\n", "1 2 1\n", "1 2 2\n", "2 2 2\n"], "outputs": ["6\n", "14\n", "0\n", "0\n", "2\n", "4\n", "4\n", "6\n"]}
409
103
coding
Solve the programming task below in a Python markdown code block. Transformation Write a program which performs a sequence of commands to a given string $str$. The command is one of: * print a b: print from the a-th character to the b-th character of $str$ * reverse a b: reverse from the a-th character to the b-th character of $str$ * replace a b p: replace from the a-th character to the b-th character of $str$ with p Note that the indices of $str$ start with 0. Constraints * $1 \leq $ length of $str \leq 1000$ * $1 \leq q \leq 100$ * $0 \leq a \leq b < $ length of $str$ * for replace command, $b - a + 1 = $ length of $p$ Input In the first line, a string $str$ is given. $str$ consists of lowercase letters. In the second line, the number of commands q is given. In the next q lines, each command is given in the above mentioned format. Output For each print command, print a string in a line. Examples Input abcde 3 replace 1 3 xyz reverse 0 2 print 1 4 Output xaze Input xyz 3 print 0 2 replace 0 2 abc print 0 2 Output xyz abc
{"inputs": ["xyz\n3\nprint 0 2\nreplace 1 2 abc\nprint 0 2", "xyz\n1\nprint 0 2\nreplace 0 2 abc\nprint 0 2", "xyz\n3\nprint 0 1\nreplace 1 2 abc\nprint 0 2", "wyz\n1\nprint 0 2\nreplace 0 2 abc\nprint 0 2", "xyz\n3\nprint 0 2\nreplace 1 2 abb\nprint 0 4", "zyx\n3\nprint 0 2\nreplace 0 2 abc\nprint 0 2", "xyz\n3\nprint 0 2\nreplace 1 2 abc\nprint 0 1", "xyz\n2\nprint 0 1\nreplace 1 2 abc\nprint 0 2"], "outputs": ["xyz\nxab\n", "xyz\n", "xy\nxab\n", "wyz\n", "xyz\nxabb\n", "zyx\nabc\n", "xyz\nxa\n", "xy\n"]}
317
245
coding
Solve the programming task below in a Python markdown code block. Petya is the most responsible worker in the Research Institute. So he was asked to make a very important experiment: to melt the chocolate bar with a new laser device. The device consists of a rectangular field of n × m cells and a robotic arm. Each cell of the field is a 1 × 1 square. The robotic arm has two lasers pointed at the field perpendicularly to its surface. At any one time lasers are pointed at the centres of some two cells. Since the lasers are on the robotic hand, their movements are synchronized — if you move one of the lasers by a vector, another one moves by the same vector. The following facts about the experiment are known: * initially the whole field is covered with a chocolate bar of the size n × m, both lasers are located above the field and are active; * the chocolate melts within one cell of the field at which the laser is pointed; * all moves of the robotic arm should be parallel to the sides of the field, after each move the lasers should be pointed at the centres of some two cells; * at any one time both lasers should be pointed at the field. Petya doesn't want to become a second Gordon Freeman. You are given n, m and the cells (x1, y1) and (x2, y2), where the lasers are initially pointed at (xi is a column number, yi is a row number). Rows are numbered from 1 to m from top to bottom and columns are numbered from 1 to n from left to right. You are to find the amount of cells of the field on which the chocolate can't be melted in the given conditions. Input The first line contains one integer number t (1 ≤ t ≤ 10000) — the number of test sets. Each of the following t lines describes one test set. Each line contains integer numbers n, m, x1, y1, x2, y2, separated by a space (2 ≤ n, m ≤ 109, 1 ≤ x1, x2 ≤ n, 1 ≤ y1, y2 ≤ m). Cells (x1, y1) and (x2, y2) are distinct. Output Each of the t lines of the output should contain the answer to the corresponding input test set. Examples Input 2 4 4 1 1 3 3 4 3 1 1 2 2 Output 8 2
{"inputs": ["1\n3 3 3 2 1 1\n", "1\n4 5 2 2 4 2\n", "1\n2 5 1 5 2 2\n", "1\n5 6 3 4 4 2\n", "1\n3 5 2 4 3 5\n", "1\n2 6 2 6 2 3\n", "1\n2 2 1 2 2 1\n", "1\n7 3 6 2 5 2\n"], "outputs": ["5\n", "0\n", "6\n", "4\n", "2\n", "0\n", "2\n", "0\n"]}
536
166
coding
Solve the programming task below in a Python markdown code block. The tram in Berland goes along a straight line from the point 0 to the point s and back, passing 1 meter per t_1 seconds in both directions. It means that the tram is always in the state of uniform rectilinear motion, instantly turning around at points x = 0 and x = s. Igor is at the point x_1. He should reach the point x_2. Igor passes 1 meter per t_2 seconds. Your task is to determine the minimum time Igor needs to get from the point x_1 to the point x_2, if it is known where the tram is and in what direction it goes at the moment Igor comes to the point x_1. Igor can enter the tram unlimited number of times at any moment when his and the tram's positions coincide. It is not obligatory that points in which Igor enter and exit the tram are integers. Assume that any boarding and unboarding happens instantly. Igor can move arbitrary along the line (but not faster than 1 meter per t_2 seconds). He can also stand at some point for some time. -----Input----- The first line contains three integers s, x_1 and x_2 (2 ≤ s ≤ 1000, 0 ≤ x_1, x_2 ≤ s, x_1 ≠ x_2) — the maximum coordinate of the point to which the tram goes, the point Igor is at, and the point he should come to. The second line contains two integers t_1 and t_2 (1 ≤ t_1, t_2 ≤ 1000) — the time in seconds in which the tram passes 1 meter and the time in seconds in which Igor passes 1 meter. The third line contains two integers p and d (1 ≤ p ≤ s - 1, d is either 1 or $- 1$) — the position of the tram in the moment Igor came to the point x_1 and the direction of the tram at this moment. If $d = - 1$, the tram goes in the direction from the point s to the point 0. If d = 1, the tram goes in the direction from the point 0 to the point s. -----Output----- Print the minimum time in seconds which Igor needs to get from the point x_1 to the point x_2. -----Examples----- Input 4 2 4 3 4 1 1 Output 8 Input 5 4 0 1 2 3 1 Output 7 -----Note----- In the first example it is profitable for Igor to go by foot and not to wait the tram. Thus, he has to pass 2 meters and it takes 8 seconds in total, because he passes 1 meter per 4 seconds. In the second example Igor can, for example, go towards the point x_2 and get to the point 1 in 6 seconds (because he has to pass 3 meters, but he passes 1 meters per 2 seconds). At that moment the tram will be at the point 1, so Igor can enter the tram and pass 1 meter in 1 second. Thus, Igor will reach the point x_2 in 7 seconds in total.
{"inputs": ["4 2 4\n3 4\n1 1\n", "5 4 0\n1 2\n3 1\n", "2 0 2\n1 1\n1 1\n", "5 4 2\n1 2\n3 1\n", "4 2 4\n3 4\n2 1\n", "6 4 2\n1 2\n3 1\n", "3 1 3\n1 2\n1 1\n", "2 2 0\n7 3\n1 1\n"], "outputs": ["8\n", "7\n", "2\n", "4\n", "6\n", "4\n", "2\n", "6\n"]}
699
166
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given two integer arrays of equal length target and arr. In one step, you can select any non-empty subarray of arr and reverse it. You are allowed to make any number of steps. Return true if you can make arr equal to target or false otherwise.   Please complete the following python code precisely: ```python class Solution: def canBeEqual(self, target: List[int], arr: List[int]) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(target = [1,2,3,4], arr = [2,4,1,3]) == True\n assert candidate(target = [7], arr = [7]) == True\n assert candidate(target = [3,7,9], arr = [3,7,11]) == False\n\n\ncheck(Solution().canBeEqual)"}
110
89
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given the root of a binary tree and two integers val and depth, add a row of nodes with value val at the given depth depth. Note that the root node is at depth 1. The adding rule is: Given the integer depth, for each not null tree node cur at the depth depth - 1, create two tree nodes with value val as cur's left subtree root and right subtree root. cur's original left subtree should be the left subtree of the new left subtree root. cur's original right subtree should be the right subtree of the new right subtree root. If depth == 1 that means there is no depth depth - 1 at all, then create a tree node with value val as the new root of the whole original tree, and the original tree is the new root's left subtree.   Please complete the following python code precisely: ```python # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]: ```
{"functional": "def check(candidate):\n assert is_same_tree(candidate(root = tree_node([4,2,6,3,1,5]), val = 1, depth = 2), tree_node([4,1,1,2,None,None,6,3,1,5]))\n assert is_same_tree(candidate(root = tree_node([4,2,None,3,1]), val = 1, depth = 3), tree_node([4,2,None,1,1,3,None,None,1]))\n\n\ncheck(Solution().addOneRow)"}
275
126
coding
Solve the programming task below in a Python markdown code block. After many years of research, Ikta has acquired the ability to predict the future! The time and money he spent on this research was enormous, but it's finally time to be rewarded. To get the money back, Ikta decided to start investing in stocks. Ikta does not currently own any shares, but owns x yen. He has invested in n stocks, and has succeeded in predicting the stock price for d days from today. As a result, it was surprisingly found that there was no intraday stock price fluctuation for d days from today. In other words, we know the stock price pi, j yen of the stock j (1 ≤ j ≤ n) on the i (1 ≤ i ≤ d) day when today is the first day. Ikta is free to buy and sell stocks each day. That is, the following operations (purchase / sale) can be performed at any time in any order and any number of times. However, the amount of money held and the number of units held of shares before and after each operation must be non-negative integers. * Purchase: On day i, select one stock type j (1 ≤ j ≤ n) and pay pi, j yen in possession to obtain 1 unit of stock j. * Sale: On day i, select one stock type j (1 ≤ j ≤ n) and pay 1 unit of stock j to get pi, j yen. (While he was absorbed in his research, the securities trading system made great progress and there were no transaction fees.) Ikta had studied information science at university, but had forgotten everything he had learned at university before he could devote himself to future prediction research. Instead of him, write a program that maximizes your money on the final day. Input The input is given in the following format. n d x p1,1 ... p1, n ... pd, 1 ... pd, n * n: Number of stock types * d: days * x: Money on the first day * pi, j: Stock price of stock j on day i (today is the first day) Constraints Each variable being input is an integer that satisfies the following constraints. * 1 ≤ n ≤ 10 * 1 ≤ d ≤ 10 * 1 ≤ x, pi, j ≤ 105 * It is guaranteed that the amount of money you have on the last day will be 105 or less. Output Output the last day's money in one line when you invest optimally. Examples Input 2 2 5 3 2 5 4 Output 9 Input 1 2 5 6 10000 Output 5 Input 2 3 5 4 5 6 3 8 5 Output 11 Input 3 3 10 10 9 6 8 7 3 7 5 1 Output 10
{"inputs": ["1 2 5\n4\n10000", "2 2 6\n3 2\n5 4", "1 2 7\n4\n10000", "1 1 5\n6\n10000", "2 1 8\n6\n10000", "1 2 5\n6\n10000", "2 2 5\n3 2\n5 4", "2 3 5\n4 5\n6 3\n8 6"], "outputs": ["10001\n", "12\n", "10003\n", "5\n", "8\n", "5", "9", "13\n"]}
636
170
coding
Solve the programming task below in a Python markdown code block. Polycarp watched TV-show where k jury members one by one rated a participant by adding him a certain number of points (may be negative, i. e. points were subtracted). Initially the participant had some score, and each the marks were one by one added to his score. It is known that the i-th jury member gave a_{i} points. Polycarp does not remember how many points the participant had before this k marks were given, but he remembers that among the scores announced after each of the k judges rated the participant there were n (n ≤ k) values b_1, b_2, ..., b_{n} (it is guaranteed that all values b_{j} are distinct). It is possible that Polycarp remembers not all of the scores announced, i. e. n < k. Note that the initial score wasn't announced. Your task is to determine the number of options for the score the participant could have before the judges rated the participant. -----Input----- The first line contains two integers k and n (1 ≤ n ≤ k ≤ 2 000) — the number of jury members and the number of scores Polycarp remembers. The second line contains k integers a_1, a_2, ..., a_{k} ( - 2 000 ≤ a_{i} ≤ 2 000) — jury's marks in chronological order. The third line contains n distinct integers b_1, b_2, ..., b_{n} ( - 4 000 000 ≤ b_{j} ≤ 4 000 000) — the values of points Polycarp remembers. Note that these values are not necessarily given in chronological order. -----Output----- Print the number of options for the score the participant could have before the judges rated the participant. If Polycarp messes something up and there is no options, print "0" (without quotes). -----Examples----- Input 4 1 -5 5 0 20 10 Output 3 Input 2 2 -2000 -2000 3998000 4000000 Output 1 -----Note----- The answer for the first example is 3 because initially the participant could have - 10, 10 or 15 points. In the second example there is only one correct initial score equaling to 4 002 000.
{"inputs": ["1 1\n-27\n61769\n", "1 1\n1\n-164538\n", "1 1\n1\n-4000000\n", "1 1\n1\n-4000000\n", "1 1\n1\n-1911669\n", "1 1\n1\n-1398610\n", "1 1\n1\n-1396982\n", "1 1\n-659\n61769\n"], "outputs": ["1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n"]}
542
172
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a 0-indexed n x n integer matrix grid, return the number of pairs (ri, cj) such that row ri and column cj are equal. A row and column pair is considered equal if they contain the same elements in the same order (i.e., an equal array).   Please complete the following python code precisely: ```python class Solution: def equalPairs(self, grid: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(grid = [[3,2,1],[1,7,6],[2,7,7]]) == 1\n assert candidate(grid = [[3,1,2,2],[1,4,4,5],[2,4,2,2],[2,4,2,2]]) == 3\n\n\ncheck(Solution().equalPairs)"}
108
89
coding
Solve the programming task below in a Python markdown code block. N teams have come to participate in a competitive coding event called “Binary Battles”. It is a [single-elimination tournament] consisting of several rounds. Note: It is known that N is a power of 2. In one round, each team will be paired up with and compete against one of the other teams. If there are X teams before the start of a round, \frac{X}{2} matches are held simultaneously during the round between \frac{X}{2} pairs of teams. The winning team of each match will move on to the next round, while the losing team of each match will be eliminated. There are no ties involved. The next round will then take place in the same format between the remaining teams. The process will continue until only one team remains, which will be declared the overall winner. The organizers want to find the total time the event will take to complete. It is given that each round spans A minutes, and that there is a break of B minutes between every two rounds (no break after the last round). For example, consider a case when N = 4, A = 10 and B = 5. The first round will consist of two matches and will take 10 minutes to complete. Two teams move on to round 2 and the other two get eliminated. Then there is a break of 5 minutes. The two remaining teams compete in round 2, which lasts 10 more minutes. The team that wins is declared the overall winner. Thus the total time taken is 10 + 5 + 10 = 25 minutes. Can you help the organizers determine how long the event will take to finish? ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. Then the test cases follow. - The first and only line of each test case contains three space-separated integers N, A and B respectively — the number of teams, the duration of each round and the length of the breaks between rounds. ------ Output Format ------ For each test case, output on a new line the time taken in minutes for the whole event to finish. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $2 ≤ N ≤ 2^{20}$ $1 ≤ A ≤ 100$ $1 ≤ B ≤ 100$ $N$ is a power of $2$. ----- Sample Input 1 ------ 4 4 10 5 16 30 5 32 45 15 1024 23 9 ----- Sample Output 1 ------ 25 135 285 311 ----- explanation 1 ------ Test case 1: As explained above, the total time the competition will take is $10 + 5 + 10 = 25$ minutes. Test case 2: $4$ rounds will take place. The total time it will take is $30 + 5 + 30 + 5 + 30 + 5 + 30 = 135$ minutes.
{"inputs": ["4\n4 10 5\n16 30 5\n32 45 15\n1024 23 9\n"], "outputs": ["25\n135\n285\n311"]}
675
60
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. There is an undirected graph with n nodes numbered from 0 to n - 1 (inclusive). You are given a 0-indexed integer array values where values[i] is the value of the ith node. You are also given a 0-indexed 2D integer array edges, where each edges[j] = [uj, vj, timej] indicates that there is an undirected edge between the nodes uj and vj, and it takes timej seconds to travel between the two nodes. Finally, you are given an integer maxTime. A valid path in the graph is any path that starts at node 0, ends at node 0, and takes at most maxTime seconds to complete. You may visit the same node multiple times. The quality of a valid path is the sum of the values of the unique nodes visited in the path (each node's value is added at most once to the sum). Return the maximum quality of a valid path. Note: There are at most four edges connected to each node.   Please complete the following python code precisely: ```python class Solution: def maximalPathQuality(self, values: List[int], edges: List[List[int]], maxTime: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(values = [0,32,10,43], edges = [[0,1,10],[1,2,15],[0,3,10]], maxTime = 49) == 75\n assert candidate(values = [5,10,15,20], edges = [[0,1,10],[1,2,10],[0,3,10]], maxTime = 30) == 25\n assert candidate(values = [1,2,3,4], edges = [[0,1,10],[1,2,11],[2,3,12],[1,3,13]], maxTime = 50) == 7\n assert candidate(values = [0,1,2], edges = [[1,2,10]], maxTime = 10) == 0\n\n\ncheck(Solution().maximalPathQuality)"}
272
216
coding
Solve the programming task below in a Python markdown code block. Vasya studies positional numeral systems. Unfortunately, he often forgets to write the base of notation in which the expression is written. Once he saw a note in his notebook saying a + b = ?, and that the base of the positional notation wasn’t written anywhere. Now Vasya has to choose a base p and regard the expression as written in the base p positional notation. Vasya understood that he can get different results with different bases, and some bases are even invalid. For example, expression 78 + 87 in the base 16 positional notation is equal to FF16, in the base 15 positional notation it is equal to 11015, in the base 10 one — to 16510, in the base 9 one — to 1769, and in the base 8 or lesser-based positional notations the expression is invalid as all the numbers should be strictly less than the positional notation base. Vasya got interested in what is the length of the longest possible expression value. Help him to find this length. The length of a number should be understood as the number of numeric characters in it. For example, the length of the longest answer for 78 + 87 = ? is 3. It is calculated like that in the base 15 (11015), base 10 (16510), base 9 (1769) positional notations, for example, and in some other ones. Input The first letter contains two space-separated numbers a and b (1 ≤ a, b ≤ 1000) which represent the given summands. Output Print a single number — the length of the longest answer. Examples Input 78 87 Output 3 Input 1 1 Output 2
{"inputs": ["9 7\n", "3 1\n", "1 2\n", "1 3\n", "2 3\n", "3 2\n", "2 2\n", "2 1\n"], "outputs": ["2\n", "2\n", "2\n", "2\n", "2\n", "2\n", "2\n", "2\n"]}
402
86
coding
Solve the programming task below in a Python markdown code block. >When no more interesting kata can be resolved, I just choose to create the new kata, to solve their own, to enjoy the process --myjinxin2015 said # Description: Given two array of integers(`arr1`,`arr2`). Your task is going to find a pair of numbers(an element in arr1, and another element in arr2), their difference is as big as possible(absolute value); Again, you should to find a pair of numbers, their difference is as small as possible. Return the maximum and minimum difference values by an array: `[ max difference, min difference ]` For example: ``` Given arr1 = [3,10,5], arr2 = [20,7,15,8] should return [17,2] because 20 - 3 = 17, 10 - 8 = 2 ``` # Note: - arr1 and arr2 contains only integers(positive, negative or 0); - arr1 and arr2 may have different lengths, they always has at least one element; - All inputs are valid. - This is a simple version, if you want some challenges, [try the challenge version](https://www.codewars.com/kata/583c592928a0c0449d000099). # Some Examples ``` maxAndMin([3,10,5],[20,7,15,8]) === [17,2] maxAndMin([3],[20]) === [17,17] maxAndMin([3,10,5],[3,10,5]) === [7,0] maxAndMin([1,2,3,4,5],[6,7,8,9,10]) === [9,1] ``` Also feel free to reuse/extend the following starter code: ```python def max_and_min(arr1,arr2): ```
{"functional": "_inputs = [[[3, 10, 5], [20, 7, 15, 8]], [[3], [20]], [[3, 10, 5], [3, 10, 5]], [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]]\n_outputs = [[[17, 2]], [[17, 17]], [[7, 0]], [[9, 1]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(max_and_min(*i), o[0])"}
436
258
coding
Solve the programming task below in a Python markdown code block. Chef wants to impress Chefina by giving her the maximum number of gifts possible. Chef is in a gift shop having N items where the cost of the i^{th} item is equal to A_{i}. Chef has K amount of money and a 50 \% off discount coupon that he can use for at most one of the items he buys. If the cost of an item is equal to X, then, after applying the coupon on that item, Chef only has to pay {\bf \lceil \frac{X}{2} \right\rceil} (rounded up to the nearest integer) amount for that item. Help Chef find the maximum number of items he can buy with K amount of money and a 50 \% discount coupon given that he can use the coupon on at most one item. ------ 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. - The first line of each test case contains two space-separated integers N and K. - The next line contains N space-separated integers, where the i^{th} integer A_{i} denotes the cost of the i^{th} item. ------ Output Format ------ For each test case, print a single line containing one integer ― the maximum number of items Chef can buy. ------ Constraints ------ $1 ≤ T ≤ 10^{3}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ $0 ≤ K ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $2\cdot10^{5}$. ----- Sample Input 1 ------ 3 1 4 5 3 15 4 4 5 3 10 6 7 4 ----- Sample Output 1 ------ 1 3 2 ----- explanation 1 ------ Test case $1$: After applying the discount, Chef can buy the only available item at ${\lceil \frac{5}{2} \right\rceil} = 3$. Test case $2$: Chef can buy all three items even without using the coupon. Test case $3$: After applying coupon on the third item, Chef can buy the second and the third item at $7 + {\lceil \frac{4}{2} \right\rceil} = $ $ 7 + 2 = 9$. It is not possible for Chef to buy more than two items.
{"inputs": ["3\n1 4\n5\n3 15\n4 4 5\n3 10\n6 7 4"], "outputs": ["1\n3\n2"]}
543
44
coding
Solve the programming task below in a Python markdown code block. =====Problem Statement===== For this challenge, you are given two complex numbers, and you have to print the result of their addition, subtraction, multiplication, division and modulus operations. The real and imaginary precision part should be correct up to two decimal places. =====Input Format===== One line of input: The real and imaginary part of a number separated by a space. =====Output Format===== For two complex numbers C and D, the output should be in the following sequence on separate lines: C+D C-D C*D C/D mod(C) mod(D) For complex numbers with non-zero real (A) and complex part (B), the output should be in the following format: Replace the plus symbol (+) with a minus symbol (-) when B<0. For complex numbers with a zero complex part i.e. real numbers, the output should be: A+0.00i For complex numbers where the real part is zero and the complex part is non-zero, the output should be: 0.00+Bi Also feel free to reuse/extend the following starter code: ```python import math class Complex(object): def __init__(self, real, imaginary): def __add__(self, no): def __sub__(self, no): def __mul__(self, no): def __truediv__(self, no): def mod(self): def __str__(self): if self.imaginary == 0: result = "%.2f+0.00i" % (self.real) elif self.real == 0: if self.imaginary >= 0: result = "0.00+%.2fi" % (self.imaginary) else: result = "0.00-%.2fi" % (abs(self.imaginary)) elif self.imaginary > 0: result = "%.2f+%.2fi" % (self.real, self.imaginary) else: result = "%.2f-%.2fi" % (self.real, abs(self.imaginary)) return result if __name__ == '__main__': c = map(float, input().split()) d = map(float, input().split()) x = Complex(*c) y = Complex(*d) print(*map(str, [x+y, x-y, x*y, x/y, x.mod(), y.mod()]), sep='\n') ```
{"inputs": ["2 1\n5 6", "5.9 6\n9 10"], "outputs": ["7.00+7.00i\n-3.00-5.00i\n4.00+17.00i\n0.26-0.11i\n2.24+0.00i\n7.81+0.00i", "14.90+16.00i\n-3.10-4.00i\n-6.90+113.00i\n0.62-0.03i\n8.41+0.00i\n13.45+0.00i"]}
528
170
coding
Solve the programming task below in a Python markdown code block. You are given an array A of N positive integers. In one operation, you can do the following: Choose integers i and j (1 ≤ i < j ≤ N), such that A_{i} = A_{j}; For all k (i < k < j), change the value of A_{k} to A_{i}. Find out whether A can have at most 2 distinct values after using any (possibly zero) number of operations. ------ 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 lines of input: - The first line of each test case contains N - the size of the array. - The next line contains N integers, A_{1}, A_{2}, A_{3}, \ldots, A_{N} - the elements of the array. ------ Output Format ------ For each test case, print YES if A can have at most 2 distinct values after using any (possibly zero) number of operations and NO otherwise. You may print each character of the string in uppercase or lowercase (for example, the strings YES, yEs, yes, and yeS will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ over all test cases won't exceed $10^{5}$. ----- Sample Input 1 ------ 4 5 5 9 5 5 5 3 1 2 3 4 1 2 1 3 4 1 2 3 1 ----- Sample Output 1 ------ YES NO YES YES ----- explanation 1 ------ Test case $1$: The array $A$ already has $2$ distinct elements. Test case $2$: It is impossible to make operations such that $A$ has $2$ distinct elements. Test case $3$: We can make an operation as: - Choose $i = 1$ and $j = 3$. Thus, we change $A_{2}$ to $A_{1} = 1$. The final array is $[1,1,1,3]$ which has two distinct elements. Test case $4$: We can make an operation as: - Choose $i = 1$ and $j = 4$. Thus, we change $A_{2}$ and $A_{3}$ to $A_{1} = 1$. The final array is $[1,1,1,1]$ which has one distinct element.
{"inputs": ["4\n5\n5 9 5 5 5\n3\n1 2 3\n4\n1 2 1 3\n4\n1 2 3 1\n"], "outputs": ["YES\nNO\nYES\nYES\n"]}
582
60
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an array nums that consists of non-negative integers. Let us define rev(x) as the reverse of the non-negative integer x. For example, rev(123) = 321, and rev(120) = 21. A pair of indices (i, j) is nice if it satisfies all of the following conditions: 0 <= i < j < nums.length nums[i] + rev(nums[j]) == nums[j] + rev(nums[i]) Return the number of nice pairs of indices. Since that number can be too large, return it modulo 109 + 7.   Please complete the following python code precisely: ```python class Solution: def countNicePairs(self, nums: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [42,11,1,97]) == 2\n assert candidate(nums = [13,10,35,24,76]) == 4\n\n\ncheck(Solution().countNicePairs)"}
179
66
coding
Solve the programming task below in a Python markdown code block. In a very ancient country the following game was popular. Two people play the game. Initially first player writes a string s1, consisting of exactly nine digits and representing a number that does not exceed a. After that second player looks at s1 and writes a string s2, consisting of exactly nine digits and representing a number that does not exceed b. Here a and b are some given constants, s1 and s2 are chosen by the players. The strings are allowed to contain leading zeroes. If a number obtained by the concatenation (joining together) of strings s1 and s2 is divisible by mod, then the second player wins. Otherwise the first player wins. You are given numbers a, b, mod. Your task is to determine who wins if both players play in the optimal manner. If the first player wins, you are also required to find the lexicographically minimum winning move. Input The first line contains three integers a, b, mod (0 ≤ a, b ≤ 109, 1 ≤ mod ≤ 107). Output If the first player wins, print "1" and the lexicographically minimum string s1 he has to write to win. If the second player wins, print the single number "2". Examples Input 1 10 7 Output 2 Input 4 0 9 Output 1 000000001 Note The lexical comparison of strings is performed by the < operator in modern programming languages. String x is lexicographically less than string y if exists such i (1 ≤ i ≤ 9), that xi < yi, and for any j (1 ≤ j < i) xj = yj. These strings always have length 9.
{"inputs": ["0 3 3\n", "0 0 1\n", "0 1 1\n", "3 0 3\n", "1 1 1\n", "1 0 3\n", "0 1 3\n", "1 0 7\n"], "outputs": ["2\n", "2\n", "2\n", "1 000000001\n", "2\n", "1 000000001\n", "2\n", "1 000000001\n"]}
378
132
coding
Solve the programming task below in a Python markdown code block. Triangular number is the amount of points that can fill equilateral triangle. Example: the number 6 is a triangular number because all sides of a triangle has the same amount of points. ``` Hint! T(n) = n * (n + 1) / 2, n - is the size of one side. T(n) - is the triangular number. ``` Given a number 'T' from interval [1; 2147483646], find if it is triangular number or not. Also feel free to reuse/extend the following starter code: ```python def is_triangular(t): ```
{"functional": "_inputs = [[1], [3], [6], [10], [15], [21], [28], [2], [7], [14], [27]]\n_outputs = [[True], [True], [True], [True], [True], [True], [True], [False], [False], [False], [False]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(is_triangular(*i), o[0])"}
146
221
coding
Solve the programming task below in a Python markdown code block. You will receive an uncertain amount of integers in a certain order ```k1, k2, ..., kn```. You form a new number of n digits in the following way: you take one of the possible digits of the first given number, ```k1```, then the same with the given number ```k2```, repeating the same process up to ```kn``` and you concatenate these obtained digits(in the order that were taken) obtaining the new number. As you can see, we have many possibilities. Let's see the process above explained with three given numbers: ``` k1 = 23, k2 = 17, k3 = 89 Digits Combinations Obtained Number ('2', '1', '8') 218 <---- Minimum ('2', '1', '9') 219 ('2', '7', '8') 278 ('2', '7', '9') 279 ('3', '1', '8') 318 ('3', '1', '9') 319 ('3', '7', '8') 378 ('3', '7', '9') 379 <---- Maximum Total Sum = 2388 (8 different values) ``` We need the function that may work in this way: ```python proc_seq(23, 17, 89) == [8, 218, 379, 2388] ``` See this special case and deduce how the function should handle the cases which have many repetitions. ```python proc_seq(22, 22, 22, 22) == [1, 2222] # we have only one obtained number, the minimum, maximum and total sum coincide ``` The sequence of numbers will have numbers of n digits only. Numbers formed by leading zeroes will be discarded. ```python proc_seq(230, 15, 8) == [4, 218, 358, 1152] ``` Enjoy it!! You will never receive the number 0 and all the numbers will be in valid format. Also feel free to reuse/extend the following starter code: ```python def proc_seq(*args): ```
{"functional": "_inputs = [[23, 17, 89], [22, 22, 22, 22], [230, 15, 8]]\n_outputs = [[[8, 218, 379, 2388]], [[1, 2222]], [[4, 218, 358, 1152]]]\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(proc_seq(*i), o[0])"}
535
235
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a 0-indexed array words consisting of distinct strings. The string words[i] can be paired with the string words[j] if: The string words[i] is equal to the reversed string of words[j]. 0 <= i < j < words.length. Return the maximum number of pairs that can be formed from the array words. Note that each string can belong in at most one pair.   Please complete the following python code precisely: ```python class Solution: def maximumNumberOfStringPairs(self, words: List[str]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(words = [\"cd\",\"ac\",\"dc\",\"ca\",\"zz\"]) == 2\n assert candidate(words = [\"ab\",\"ba\",\"cc\"]) == 1\n assert candidate(words = [\"aa\",\"ab\"]) == 0\n\n\ncheck(Solution().maximumNumberOfStringPairs)"}
134
77
coding
Solve the programming task below in a Python markdown code block. Leonardo Fibonacci in a rare portrait of his younger days I assume you are all familiar with the famous Fibonacci sequence, having to get each number as the sum of the previous two (and typically starting with either `[0,1]` or `[1,1]` as the first numbers). While there are plenty of variation on it ([including](https://www.codewars.com/kata/tribonacci-sequence) [a few](https://www.codewars.com/kata/fibonacci-tribonacci-and-friends) [I wrote](https://www.codewars.com/kata/triple-shiftian-numbers/)), usually the catch is all the same: get a starting (signature) list of numbers, then proceed creating more with the given rules. What if you were to get to get two parameters, one with the signature (starting value) and the other with the number you need to sum at each iteration to obtain the next one? And there you have it, getting 3 parameters: * a signature of length `length` * a second parameter is a list/array of indexes of the last `length` elements you need to use to obtain the next item in the sequence (consider you can end up not using a few or summing the same number multiple times)' in other words, if you get a signature of length `5` and `[1,4,2]` as indexes, at each iteration you generate the next number by summing the 2nd, 5th and 3rd element (the ones with indexes `[1,4,2]`) of the last 5 numbers * a third and final parameter is of course which sequence element you need to return (starting from zero, I don't want to bother you with adding/removing 1s or just coping with the fact that after years on CodeWars we all count as computers do): ```python custom_fib([1,1],[0,1],2) == 2 #classical fibonacci! custom_fib([1,1],[0,1],3) == 3 #classical fibonacci! custom_fib([1,1],[0,1],4) == 5 #classical fibonacci! custom_fib([3,5,2],[0,1,2],4) == 17 #similar to my Tribonacci custom_fib([7,3,4,1],[1,1],6) == 2 #can you figure out how it worked ;)? ``` Also feel free to reuse/extend the following starter code: ```python def custom_fib(signature, indexes, n): ```
{"functional": "_inputs = [[[1, 1], [0, 1], 2], [[1, 1], [0, 1], 3], [[1, 1], [0, 1], 4], [[3, 5, 2], [0, 1, 2], 4], [[7, 3, 4, 1], [1, 1], 6]]\n_outputs = [[2], [3], [5], [17], [2]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(custom_fib(*i), o[0])"}
556
252
coding
Solve the programming task below in a Python markdown code block. You are given two strings $s$ and $t$, each of length $n$ and consisting of lowercase Latin alphabets. You want to make $s$ equal to $t$. You can perform the following operation on $s$ any number of times to achieve it — Choose any substring of $s$ and rotate it clockwise once, that is, if the selected substring is $s[l,l+1...r]$, then it becomes $s[r,l,l + 1 ... r - 1]$. All the remaining characters of $s$ stay in their position. For example, on rotating the substring $[2,4]$ , string "abcde" becomes "adbce". A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. Find the minimum number of operations required to convert $s$ to $t$, or determine that it's impossible. -----Input----- The first line of the input contains a single integer $t$ $(1\leq t \leq 2000)$ — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ $(1\leq n \leq 2000)$ — the length of the strings. The second and the third lines contain strings $s$ and $t$ respectively. The sum of $n$ over all the test cases does not exceed $2000$. -----Output----- For each test case, output the minimum number of operations to convert $s$ to $t$. If it is not possible to convert $s$ to $t$, output $-1$ instead. -----Example----- Input 6 1 a a 2 ab ba 3 abc cab 3 abc cba 4 abab baba 4 abcc aabc Output 0 1 1 2 1 -1 -----Note----- For the $1$-st test case, since $s$ and $t$ are equal, you don't need to apply any operation. For the $2$-nd test case, you only need to apply one operation on the entire string ab to convert it to ba. For the $3$-rd test case, you only need to apply one operation on the entire string abc to convert it to cab. For the $4$-th test case, you need to apply the operation twice: first on the entire string abc to convert it to cab and then on the substring of length $2$ beginning at the second character to convert it to cba. For the $5$-th test case, you only need to apply one operation on the entire string abab to convert it to baba. For the $6$-th test case, it is not possible to convert string $s$ to $t$.
{"inputs": ["1\n1\na\na\n", "1\n1\na\na\n", "1\n1\na\nb\n", "1\n1\nb\na\n", "3\n2\naa\naa\n2\nab\nab\n2\nab\nba\n", "3\n2\naa\naa\n2\nab\nab\n2\nab\nba\n", "3\n2\naa\naa\n2\nab\nab\n2\naa\nba\n", "3\n2\naa\naa\n2\nba\nab\n2\naa\nba\n"], "outputs": ["0\n", "0\n", "-1\n", "-1\n", "0\n0\n1\n", "0\n0\n1\n", "0\n0\n-1\n", "0\n1\n-1\n"]}
661
184
coding
Solve the programming task below in a Python markdown code block. Aleksey has $n$ friends. He is also on a vacation right now, so he has $m$ days to play this new viral cooperative game! But since it's cooperative, Aleksey will need one teammate in each of these $m$ days. On each of these days some friends will be available for playing, and all others will not. On each day Aleksey must choose one of his available friends to offer him playing the game (and they, of course, always agree). However, if any of them happens to be chosen strictly more than $\left\lceil\dfrac{m}{2}\right\rceil$ times, then all other friends are offended. Of course, Aleksey doesn't want to offend anyone. Help him to choose teammates so that nobody is chosen strictly more than $\left\lceil\dfrac{m}{2}\right\rceil$ times. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10000$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1\leq n, m\leq 100000$) standing for the number of friends and the number of days to play, respectively. The $i$-th of the following $m$ lines contains an integer $k_i$ ($1\leq k_i\leq n$), followed by $k_i$ distinct integers $f_{i1}$, ..., $f_{ik_i}$ ($1\leq f_{ij}\leq n$), separated by spaces — indices of available friends on the day $i$. It is guaranteed that the sums of $n$ and $m$ over all test cases do not exceed $100000$. It's guaranteed that the sum of all $k_i$ over all days of all test cases doesn't exceed $200000$. -----Output----- Print an answer for each test case. If there is no way to achieve the goal, print "NO". Otherwise, in the first line print "YES", and in the second line print $m$ space separated integers $c_1$, ..., $c_m$. Each $c_i$ must denote the chosen friend on day $i$ (and therefore must be one of $f_{ij}$). No value must occur more than $\left\lceil\dfrac{m}{2}\right\rceil$ times. If there is more than one possible answer, print any of them. -----Examples----- Input 2 4 6 1 1 2 1 2 3 1 2 3 4 1 2 3 4 2 2 3 1 3 2 2 1 1 1 1 Output YES 1 2 1 1 2 3 NO -----Note----- None
{"inputs": ["2\n4 6\n1 1\n2 1 2\n3 1 2 3\n4 1 2 3 4\n2 2 3\n1 3\n2 2\n1 1\n1 1\n"], "outputs": ["YES\n1 2 1 1 2 3 \nNO\n"]}
645
83
coding
Solve the programming task below in a Python markdown code block. Dr. S. De teaches computer architecture in NIT Patna. Whenever he comes across any good question(with complexity $k$), he gives that question to students within roll number range $i$ and $j$ At the start of semester he assigns score of $10$ to every student in his class if a student submits a question of complexity $k$, his score gets multiplied by $k$ This month he gave $M$ questions and he is wondering what will be mean of maximum scores of all the student. He is busy in improving his finger print attendance module, can you help him? Input file may be large so try to use fast input output -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of a First line of input, two integers $N, M$ i.e. Number of students in the class and number of questions given in this month. - Next $M$ lines contains 3 integers -$i, j, k$ i.e. starting roll number, end roll number and complexity of the question -----Output:----- For each testcase, output in a single line answer - $floor$ value of Mean of maximum possible score for all students. -----Constraints----- - $1 \leq T \leq 100$ - $1 \leq N, M \leq 10^5$ - $1 \leq i \leq j \leq N$ - $1 \leq k \leq 100$ -----Subtasks----- Subtask1 - $1 \leq T \leq 10$ - $1 \leq N, M \leq 10^4$ Subtask2 - Original Constraints -----Sample Input:----- 1 5 3 1 3 5 2 5 2 3 4 7 -----Sample Output:----- 202 -----EXPLANATION:----- Initial score of students will be : $[10, 10, 10, 10, 10]$ after solving question 1 scores will be: $[50, 50, 50, 10, 10]$ after solving question 2 scores will be: $[50, 100, 100, 20, 20]$ after solving question 1 scores will be: $[50, 100, 700, 140, 20]$ Hence after all questions mean of maximum scores will $(50+100+700+140+20)/5 = 202$
{"inputs": ["1\n5 3\n1 3 5\n2 5 2\n3 4 7"], "outputs": ["202"]}
596
36
coding
Solve the programming task below in a Python markdown code block. Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya wonders eagerly what minimum lucky number has the sum of digits equal to n. Help him cope with the task. Input The single line contains an integer n (1 ≤ n ≤ 106) — the sum of digits of the required lucky number. Output Print on the single line the result — the minimum lucky number, whose sum of digits equals n. If such number does not exist, print -1. Examples Input 11 Output 47 Input 10 Output -1
{"inputs": ["7\n", "4\n", "5\n", "3\n", "1\n", "2\n", "6\n", "9\n"], "outputs": ["7\n", "4\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n"]}
185
70
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin chinese, Russian and Vietnamese as well. Today, Chef was trying to solve a problem he found pretty hard: Given an integer N and a triple of integers (a, b, c), compute the number of triples of positive integers (x, y, z) such that N = x · y · z, x ≤ a, y ≤ b and z ≤ c. Can you help Chef solve this problem? Two triples (x_{1}, y_{1}, z_{1}) and (x_{2}, y_{2}, z_{2}) differ if x_{1} is not equal to x_{2} or y_{1} is not equal to y_{2} or z_{1} is not equal to z_{2}. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first and only line of each test case contains four space-separated integers N, a, b and c. ------ Output ------ For each test case, print a single line containing one integer — the number of valid triples (x, y, z). ------ Constraints ------ $1 ≤ T ≤ 20$ $1 ≤ N ≤ 10^{9}$ $1 ≤ a, b, c ≤ 10^{6}$ ----- Sample Input 1 ------ 3 100 8 23 11 497296800 1000000 1000000 1000000 1 1 2 3 ----- Sample Output 1 ------ 10 97800 1 ----- explanation 1 ------ Example case 1: There are 10 valid triples (x, y, z): (1, 10, 10), (1, 20, 5), (2, 5, 10), (2, 10, 5), (4, 5, 5), (5, 2, 10), (5, 4, 5), (5, 5, 4), (5, 10, 2), (5, 20, 1).
{"inputs": ["3\n100 8 23 11\n497296800 1000000 1000000 1000000\n1 1 2 3"], "outputs": ["10\n97800\n1"]}
499
75
coding
Solve the programming task below in a Python markdown code block. A pair of positive integers $(a,b)$ is called special if $\lfloor \frac{a}{b} \rfloor = a mod b$. Here, $\lfloor \frac{a}{b} \rfloor$ is the result of the integer division between $a$ and $b$, while $a mod b$ is its remainder. You are given two integers $x$ and $y$. Find the number of special pairs $(a,b)$ such that $1\leq a \leq x$ and $1 \leq b \leq y$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The only line of the description of each test case contains two integers $x$, $y$ ($1 \le x,y \le 10^9$). -----Output----- For each test case print the answer on a single line. -----Examples----- Input 9 3 4 2 100 4 3 50 3 12 4 69 420 12345 6789 123456 789 12345678 9 Output 1 0 2 3 5 141 53384 160909 36 -----Note----- In the first test case, the only special pair is $(3, 2)$. In the second test case, there are no special pairs. In the third test case, there are two special pairs: $(3, 2)$ and $(4, 3)$.
{"inputs": ["1\n3 1\n", "1\n3 1\n", "9\n8 11\n1 110\n6 3\n69 2\n1 2\n92 79\n8276 17989\n164 789\n6179509 2\n", "9\n8 6\n1 110\n5 3\n36 2\n1 2\n92 420\n8276 17989\n164 789\n16177647 9\n", "9\n8 11\n1 110\n6 3\n36 2\n1 2\n92 92\n8276 17989\n164 789\n20448260 4\n", "9\n8 11\n1 110\n6 3\n36 2\n1 2\n92 79\n8276 17989\n164 789\n20448260 4\n", "9\n8 6\n1 110\n5 3\n36 2\n1 2\n92 420\n8276 17989\n164 789\n20448260 9\n", "9\n8 6\n1 110\n6 3\n36 2\n1 2\n92 420\n8276 17989\n164 789\n20448260 9\n"], "outputs": ["0\n", "0\n", "7\n0\n2\n1\n0\n192\n37833\n414\n1\n", "6\n0\n2\n1\n0\n204\n37833\n414\n36\n", "7\n0\n2\n1\n0\n204\n37833\n414\n6\n", "7\n0\n2\n1\n0\n192\n37833\n414\n6\n", "6\n0\n2\n1\n0\n204\n37833\n414\n36\n", "6\n0\n2\n1\n0\n204\n37833\n414\n36\n"]}
377
584
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese and Russian. Aditi recently discovered a new magic trick. First, she gives you an integer N and asks you to think an integer between 1 and N. Then she gives you a bundle of cards each having a sorted list (in ascending order) of some distinct integers written on it. The integers in all the lists are between 1 and N. Note that the same integer may appear in more than one card. Now, she shows you these cards one by one and asks whether the number you thought is written on the card or not. After that, she immediately tells you the integer you had thought of. Seeing you thoroughly puzzled, she explains that she can apply the trick so fast because she is just adding the first integer written on the cards that contain the integer you had thought of, and then gives the sum as the answer. She calls a bundle interesting if when the bundle is lexicographically sorted, no two consecutive cards have any number in common. Now she challenges you to find out the minimum number of cards she will need for making an interesting bundle such that the magic trick will work every time. ------ Input ------ The first line of the input contains an integer T denoting the number of test cases. Each test case contains a line with a single integer N. ------ Output ------ For each test case, output a line containing a single integer denoting the minimum number of cards required. ------ Constraints ------ 1 ≤ T ≤ 10^{5} 1 ≤ N ≤ 10^{18} ------ Sub tasks ------ Subtask #1: 1 ≤ T ≤ 10, 1 ≤ N ≤ 10 (5 points) Subtask #2: 1 ≤ T ≤ 100, 1 ≤ N ≤ 1000 (10 points) Subtask #3: Original Constraints (85 points) ----- Sample Input 1 ------ 2 1 4 ----- Sample Output 1 ------ 1 3 ----- explanation 1 ------ In example 1, only 1 card containing {1} will work. In example 2, make 3 cards containing {1,4}, {2} and {3,4}. Assume you thought of 1, then you will select the 1st card {1,4}, then she will correctly figure out the integer you thought being 1. Assume you thought of 2, then you will select the 2nd card {2}, then she will correctly figure out the integer you thought being 2. Assume you thought of 3, then you will select the 3rd card {3,4}, then she will correctly figure out the integer you thought being 3. Assume you thought of 4, then you will select 1st card {1,4} and 3rd card {3,4}, then she will calculate the sum of the first integers of the two card 1 + 3 = 4, and she will answer it. Thus her trick will work well in every case. And we can check it easily that the cards are sorted in lexicographical order and two consecutive cards have no common integers.
{"inputs": ["2\n1\n4", "2\n1\n3", "2\n2\n4", "2\n2\n8", "2\n4\n4", "2\n2\n7", "2\n4\n8", "2\n8\n8"], "outputs": ["1\n3", "1\n3\n", "2\n3\n", "2\n5\n", "3\n3\n", "2\n4\n", "3\n5\n", "5\n5\n"]}
676
109
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a positive integer n, find the sum of all integers in the range [1, n] inclusive that are divisible by 3, 5, or 7. Return an integer denoting the sum of all numbers in the given range satisfying the constraint.   Please complete the following python code precisely: ```python class Solution: def sumOfMultiples(self, n: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(n = 7) == 21\n assert candidate(n = 10) == 40\n assert candidate(n = 9) == 30\n\n\ncheck(Solution().sumOfMultiples)"}
103
61
coding
Solve the programming task below in a Python markdown code block. A penguin Rocher has $n$ sticks. He has exactly one stick with length $i$ for all $1 \le i \le n$. He can connect some sticks. If he connects two sticks that have lengths $a$ and $b$, he gets one stick with length $a + b$. Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections. He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create? -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case, the only line contains a single integer $n$ ($1 \le n \le 10^{9}$). -----Output----- For each test case, print a single integer  — the answer to the problem. -----Example----- Input 4 1 2 3 4 Output 1 1 2 2 -----Note----- In the third case, he can connect two sticks with lengths $1$ and $2$ and he will get one stick with length $3$. So, he will have two sticks with lengths $3$. In the fourth case, he can connect two sticks with lengths $1$ and $3$ and he will get one stick with length $4$. After that, he will have three sticks with lengths $\{2, 4, 4\}$, so two sticks have the same length, and one stick has the other length.
{"inputs": ["4\n1\n2\n3\n4\n", "4\n1\n2\n3\n8\n", "4\n1\n2\n1\n4\n", "4\n2\n2\n3\n5\n", "4\n1\n2\n4\n4\n", "4\n1\n3\n1\n4\n", "4\n1\n2\n5\n4\n", "4\n1\n3\n3\n5\n"], "outputs": ["1\n1\n2\n2\n", "1\n1\n2\n4\n", "1\n1\n1\n2\n", "1\n1\n2\n3\n", "1\n1\n2\n2\n", "1\n2\n1\n2\n", "1\n1\n3\n2\n", "1\n2\n2\n3\n"]}
399
182
coding
Solve the programming task below in a Python markdown code block. There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d_1 and that of between second and third team will be d_2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. -----Input----- The first line of the input contains a single integer corresponding to number of test cases t (1 ≤ t ≤ 10^5). Each of the next t lines will contain four space-separated integers n, k, d_1, d_2 (1 ≤ n ≤ 10^12; 0 ≤ k ≤ n; 0 ≤ d_1, d_2 ≤ k) — data for the current test case. -----Output----- For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). -----Examples----- Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no -----Note----- Sample 1. There has not been any match up to now (k = 0, d_1 = 0, d_2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d_1 = 0 and d_2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d_1 = 1, d_2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
{"inputs": ["5\n3 0 0 0\n3 3 0 0\n6 4 1 0\n6 3 3 0\n3 3 3 2\n", "5\n3 0 0 0\n3 3 0 0\n6 4 1 0\n6 6 3 0\n3 3 3 2\n", "5\n3 0 0 0\n3 5 0 0\n6 3 1 0\n6 6 3 0\n1 3 3 4\n", "5\n3 0 0 0\n3 3 0 0\n6 4 1 0\n6 6 0 0\n3 3 3 2\n", "5\n5 0 0 0\n3 3 0 0\n6 4 1 0\n6 6 3 0\n3 3 3 4\n", "5\n3 0 0 0\n3 3 0 0\n6 4 1 0\n6 6 3 0\n3 3 3 4\n", "5\n3 0 0 0\n3 5 0 0\n6 4 1 0\n6 6 3 0\n3 3 3 4\n", "5\n3 0 0 0\n3 5 0 0\n6 4 2 0\n6 6 3 0\n3 3 3 4\n"], "outputs": ["yes\nyes\nyes\nno\nno\n", "yes\nyes\nyes\nno\nno\n", "yes\nno\nno\nno\nno\n", "yes\nyes\nyes\nyes\nno\n", "no\nyes\nyes\nno\nno\n", "yes\nyes\nyes\nno\nno\n", "yes\nno\nyes\nno\nno\n", "yes\nno\nyes\nno\nno\n"]}
644
454
coding
Solve the programming task below in a Python markdown code block. Chef got into a fight with the evil Dr Doof. Dr Doof has decided to destroy all even numbers from the universe using his Evil-Destroy-inator. Chef has $N$ integers with him. To stop Doof, Chef has to find an odd number which is an integer multiple of all $N$ numbers that he has with him. Find if it is possible for Chef to prevent Dr Doof from destroying the even numbers. Formally, given $N$ positive integers, find if there exists an odd number which is an integer multiple of all the given $N$ numbers. If yes, print "YES", otherwise "NO". You can print any letter in any case. -----Input----- - First line contains $T$, number of testcases. Each testcase consists of $2$ lines. - The first line of each test case consists of a positive integer $N$, denoting the number of positive integers Chef has. - The second line of each test case contains $N$ space separated integers $A_i$ each denoting an integer that Chef has with him. -----Output----- For every test case, if there exists such an odd number, print "YES" on a separate line, otherwise "NO". The judge is case insensitive. That means, your code can print any letter in any case ( "Yes", "yes" or "YES" are all accepted). -----Constraints----- - $1 \leq T \leq 10^3$ - $1 \leq N \leq 10^3$ - $1 \leq A_i \leq 10^3$ -----Sample Input----- 2 5 1 2 5 4 3 1 7 -----Sample Output----- NO YES -----Explanation----- For test $1$: There exists no odd number. For test $2$: The possible odd numbers can be $7$, $21$, $49$, $315$, …
{"inputs": ["2\n5\n1 2 5 4 3\n1\n7"], "outputs": ["NO\nYES"]}
419
30
coding
Solve the programming task below in a Python markdown code block. You are required to create a simple calculator that returns the result of addition, subtraction, multiplication or division of two numbers. Your function will accept three arguments: The first and second argument should be numbers. The third argument should represent a sign indicating the operation to perform on these two numbers. ```if-not:csharp if the variables are not numbers or the sign does not belong to the list above a message "unknown value" must be returned. ``` ```if:csharp If the sign is not a valid sign, throw an ArgumentException. ``` # Example: ```python calculator(1, 2, '+') => 3 calculator(1, 2, '$') # result will be "unknown value" ``` Good luck! Also feel free to reuse/extend the following starter code: ```python def calculator(x,y,op): ```
{"functional": "_inputs = [[6, 2, '+'], [4, 3, '-'], [5, 5, '*'], [5, 4, '/'], [6, '$', '+'], [6, 2, '&'], [4, 3, '\\\\'], ['a', 3, '+'], [6, 2, '='], [6, 2, '\\t'], [':', ',', '+']]\n_outputs = [[8], [1], [25], [1.25], ['unknown value'], ['unknown value'], ['unknown value'], ['unknown value'], ['unknown value'], ['unknown value'], ['unknown value']]\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(calculator(*i), o[0])"}
188
279
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a binary string s and a positive integer k. Return the length of the longest subsequence of s that makes up a binary number less than or equal to k. Note: The subsequence can contain leading zeroes. The empty string is considered to be equal to 0. A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.   Please complete the following python code precisely: ```python class Solution: def longestSubsequence(self, s: str, k: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(s = \"1001010\", k = 5) == 5\n assert candidate(s = \"00101001\", k = 1) == 6\n\n\ncheck(Solution().longestSubsequence)"}
138
68
coding
Solve the programming task below in a Python markdown code block. You have an array of numbers. Your task is to sort ascending odd numbers but even numbers must be on their places. Zero isn't an odd number and you don't need to move it. If you have an empty array, you need to return it. *Example* ```python sort_array([5, 3, 2, 8, 1, 4]) == [1, 3, 2, 8, 5, 4] ``` Also feel free to reuse/extend the following starter code: ```python def sort_array(source_array): ```
{"functional": "_inputs = [[[5, 3, 2, 8, 1, 4, 11]], [[2, 22, 37, 11, 4, 1, 5, 0]], [[1, 111, 11, 11, 2, 1, 5, 0]], [[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]], [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]], [[0, 1, 2, 3, 4, 9, 8, 7, 6, 5]]]\n_outputs = [[[1, 3, 2, 8, 5, 4, 11]], [[2, 22, 1, 5, 4, 11, 37, 0]], [[1, 1, 5, 11, 2, 11, 111, 0]], [[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]], [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]], [[0, 1, 2, 3, 4, 5, 8, 7, 6, 9]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(sort_array(*i), o[0])"}
135
482
coding
Solve the programming task below in a Python markdown code block. There are N towers in the town where Ikta lives. Each tower is given a different number from 0 to N-1, and the tower with number i is called tower i. Curious Ikta was interested in the height of the N towers and decided to make a table T showing the magnitude relationship between them. T has N × N elements, and each element Ti, j (0 ≤ i, j ≤ N − 1) is defined as follows. * Ti, j = −1 ⇔ The height of tower i is smaller than the height of tower j * Ti, j = 0 ⇔ The height of tower i is equal to the height of tower j * Ti, j = 1 ⇔ The height of tower i is larger than the height of tower j As a survey to create Table T, Ikta repeated N-1 times to select two towers and compare their heights. We know the following about Ikta's investigation. * If tower ai and tower bi were selected in the i-th comparison (1 ≤ i ≤ \ N − 1), the height of tower ai was larger than the height of tower bi. That is, Tai, bi = 1, Tbi, ai = −1. * Each tower has been compared to a tower larger than itself at most once. Unfortunately, it is not always possible to uniquely determine the contents of Table T based on the information obtained from Ikta's research. If the table T is consistent with Ikta's research and there is a combination of tower heights in which T is defined, we will call T the correct table. Please calculate how many kinds of correct tables you can think of and tell Ikta. However, although the heights of the two towers compared are different from each other, not all towers are different from each other. Input The input is given in the following format. N a1 b1 ... aN−1 bN−1 N represents the number of towers. ai, bi (1 ≤ i ≤ N − 1) indicates that the tower ai is higher than the tower bi. Constraints Each variable being input satisfies the following constraints. * 1 ≤ N ≤ 200 * 0 ≤ ai, bi <N * ai ≠ bi * Different towers may be the same height. * Ikta's findings are not inconsistent, and there is at least one Table T that is consistent with Ikta's findings. Output Output the remainder of dividing the number of possible correct number of tables T by 1,000,000,007. Examples Input 3 0 1 1 2 Output 1 Input 3 0 1 0 2 Output 3 Input 1 Output 1 Input 7 0 1 1 2 2 3 3 4 0 5 0 6 Output 91
{"inputs": ["1", "3\n0 2\n0 1", "3\n1 2\n0 1", "3\n1 0\n0 2", "3\n1 1\n0 2", "3\n1 1\n1 2", "3\n0 1\n2 2", "3\n1 0\n2 2"], "outputs": ["1", "3\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n"]}
622
117
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. There are a row of n houses, each house can be painted with one of the k colors. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color. The cost of painting each house with a certain color is represented by an n x k cost matrix costs. For example, costs[0][0] is the cost of painting house 0 with color 0; costs[1][2] is the cost of painting house 1 with color 2, and so on... Return the minimum cost to paint all houses.   Please complete the following python code precisely: ```python class Solution: def minCostII(self, costs: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(costs = [[1,5,3],[2,9,4]]) == 5\n assert candidate(costs = [[1,3],[2,4]]) == 5\n\n\ncheck(Solution().minCostII)"}
176
62
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin chinese, Russian and Vietnamese as well. Chef Tobby is trying to run a code given to him by Bhuvan for an experiment they want to include in the manuscript to be submitted to a conference. The deadline to submit the manuscript is within a couple of hours and Chef Tobby needs to finish the experiments before then. The code given by Bhuvan is the following which runs given an array of N integers and another integer K : void recurse ( array a, int n ) { // n = size of array define array b currently empty consider all 2^{n} subsets of a[] { x = bitwise OR of elements in the subsets add x into "b" if it is not present yet } if (sizeof( b ) == 1 < k) { printf(“Won”); return; } recurse ( b, sizeof( b ) ); } Chef Tobby tried to run an experiment with only one integer in the array with value 2 and K = 3. To his horror, he found out that the algorithm is resulting in an infinite loop. He is livid with the possibility that the algorithm can lead to infinite loops for certain cases. On closer inspection he determines that it might be possible to insert additional elements in the initial array to subvert the problem. Since time is very less, Chef Tobby would like to insert the minimum number of elements. Chef Tobby has to finish writing the paper, so he asks his graduate student Leamas to fix it. Leamas has no idea how to fix the problem so he asks you for help. ------ Input section ------ The first line contains T, the number of test cases. Each test case consists of 2 lines. The first line contains 2 integers N and K, denoting the number of elements in the array and parameter mentioned in problem statement. Next line contains N space separated integers, denoting the elements of the array. ------ Output section ------ Output the minimum number of elements that need to be inserted so that inifinite loop can be avoided. ------ Input constraints ------ 1 ≤ T ≤ 10 1 ≤ Sum of N over all test cases ≤ 10^{5} 1 ≤ K ≤ 20 0 ≤ A[i] ≤ 2^{K}-1, where A[i] denotes the i^{th} element of the array. ----- Sample Input 1 ------ 1 2 2 3 1 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ You can win the game by inserting the element 2 into the array. ----- Sample Input 2 ------ 1 7 3 3 7 5 4 6 2 1 ----- Sample Output 2 ------ 0 ----- explanation 2 ------ The initial array will result will terminate in the first step of algorithm only. Thus there is no need to insert any new element.
{"inputs": ["1\n2 2\n3 1", "1\n7 3\n3 7 5 4 6 2 1"], "outputs": ["1", "0"]}
628
44
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a 0-indexed string s consisting of only lowercase English letters. In one operation, you can change any character of s to any other character. Return true if you can make s a palindrome after performing exactly one or two operations, or return false otherwise.   Please complete the following python code precisely: ```python class Solution: def makePalindrome(self, s: str) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(s = \"abcdba\") == True\n assert candidate(s = \"aa\") == True\n assert candidate(s = \"abcdef\") == False\n\n\ncheck(Solution().makePalindrome)"}
102
53
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given two strings sentence1 and sentence2, each representing a sentence composed of words. A sentence is a list of words that are separated by a single space with no leading or trailing spaces. Each word consists of only uppercase and lowercase English characters. Two sentences s1 and s2 are considered similar if it is possible to insert an arbitrary sentence (possibly empty) inside one of these sentences such that the two sentences become equal. Note that the inserted sentence must be separated from existing words by spaces. For example, s1 = "Hello Jane" and s2 = "Hello my name is Jane" can be made equal by inserting "my name is" between "Hello" and "Jane" in s1. s1 = "Frog cool" and s2 = "Frogs are cool" are not similar, since although there is a sentence "s are" inserted into s1, it is not separated from "Frog" by a space. Given two sentences sentence1 and sentence2, return true if sentence1 and sentence2 are similar. Otherwise, return false.   Please complete the following python code precisely: ```python class Solution: def areSentencesSimilar(self, sentence1: str, sentence2: str) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(sentence1 = \"My name is Haley\", sentence2 = \"My Haley\") == True\n assert candidate(sentence1 = \"of\", sentence2 = \"A lot of words\") == False\n assert candidate(sentence1 = \"Eating right now\", sentence2 = \"Eating\") == True\n assert candidate(sentence1 = \"Luky\", sentence2 = \"Lucccky\") == False\n\n\ncheck(Solution().areSentencesSimilar)"}
276
109
coding
Solve the programming task below in a Python markdown code block. ## Number of people in the bus There is a bus moving in the city, and it takes and drop some people in each bus stop. You are provided with a list (or array) of integer arrays (or tuples). Each integer array has two items which represent number of people get into bus (The first item) and number of people get off the bus (The second item) in a bus stop. Your task is to return number of people who are still in the bus after the last bus station (after the last array). Even though it is the last bus stop, the bus is not empty and some people are still in the bus, and they are probably sleeping there :D Take a look on the test cases. Please keep in mind that the test cases ensure that the number of people in the bus is always >= 0. So the return integer can't be negative. The second value in the first integer array is 0, since the bus is empty in the first bus stop. Also feel free to reuse/extend the following starter code: ```python def number(bus_stops): ```
{"functional": "_inputs = [[[[10, 0], [3, 5], [5, 8]]], [[[3, 0], [9, 1], [4, 10], [12, 2], [6, 1], [7, 10]]], [[[3, 0], [9, 1], [4, 8], [12, 2], [6, 1], [7, 8]]], [[[0, 0]]]]\n_outputs = [[5], [17], [21], [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(number(*i), o[0])"}
238
267
coding
Solve the programming task below in a Python markdown code block. Caisa is going to have a party and he needs to buy the ingredients for a big chocolate cake. For that he is going to the biggest supermarket in town. Unfortunately, he has just s dollars for sugar. But that's not a reason to be sad, because there are n types of sugar in the supermarket, maybe he able to buy one. But that's not all. The supermarket has very unusual exchange politics: instead of cents the sellers give sweets to a buyer as a change. Of course, the number of given sweets always doesn't exceed 99, because each seller maximizes the number of dollars in the change (100 cents can be replaced with a dollar). Caisa wants to buy only one type of sugar, also he wants to maximize the number of sweets in the change. What is the maximum number of sweets he can get? Note, that Caisa doesn't want to minimize the cost of the sugar, he only wants to get maximum number of sweets as change. -----Input----- The first line contains two space-separated integers n, s (1 ≤ n, s ≤ 100). The i-th of the next n lines contains two integers x_{i}, y_{i} (1 ≤ x_{i} ≤ 100; 0 ≤ y_{i} < 100), where x_{i} represents the number of dollars and y_{i} the number of cents needed in order to buy the i-th type of sugar. -----Output----- Print a single integer representing the maximum number of sweets he can buy, or -1 if he can't buy any type of sugar. -----Examples----- Input 5 10 3 90 12 0 9 70 5 50 7 0 Output 50 Input 5 5 10 10 20 20 30 30 40 40 50 50 Output -1 -----Note----- In the first test sample Caisa can buy the fourth type of sugar, in such a case he will take 50 sweets as a change.
{"inputs": ["1 2\n1 0\n", "1 2\n5 0\n", "1 9\n9 0\n", "1 1\n1 0\n", "1 9\n9 1\n", "1 2\n2 1\n", "1 1\n1 0\n", "1 2\n5 0\n"], "outputs": ["0\n", "-1\n", "0\n", "0\n", "-1\n", "-1\n", "0\n", "-1\n"]}
465
118
coding
Solve the programming task below in a Python markdown code block. Consider a playoff tournament where $2^n$ athletes compete. The athletes are numbered from $1$ to $2^n$. The tournament is held in $n$ stages. In each stage, the athletes are split into pairs in such a way that each athlete belongs exactly to one pair. In each pair, the athletes compete against each other, and exactly one of them wins. The winner of each pair advances to the next stage, the athlete who was defeated gets eliminated from the tournament. The pairs are formed as follows: in the first stage, athlete $1$ competes against athlete $2$; $3$ competes against $4$; $5$ competes against $6$, and so on; in the second stage, the winner of the match "$1$–$2$" competes against the winner of the match "$3$–$4$"; the winner of the match "$5$–$6$" competes against the winner of the match "$7$–$8$", and so on; the next stages are held according to the same rules. When athletes $x$ and $y$ compete, the winner is decided as follows: if $x+y$ is odd, the athlete with the lower index wins (i. e. if $x < y$, then $x$ wins, otherwise $y$ wins); if $x+y$ is even, the athlete with the higher index wins. The following picture describes the way the tournament with $n = 3$ goes. Your task is the following one: given the integer $n$, determine the index of the athlete who wins the tournament. -----Input----- The first line contains one integer $t$ ($1 \le t \le 30$) — the number of test cases. Each test case consists of one line containing one integer $n$ ($1 \le n \le 30$). -----Output----- For each test case, print one integer — the index of the winner of the tournament. -----Examples----- Input 2 3 1 Output 7 1 -----Note----- The case $n = 3$ is shown in the picture from the statement. If $n = 1$, then there's only one match between athletes $1$ and $2$. Since $1 + 2 = 3$ is an odd number, the athlete with the lower index wins. So, the athlete $1$ is the winner.
{"inputs": ["2\n3\n1\n", "9\n2\n2\n2\n2\n2\n2\n2\n2\n2\n", "30\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n", "30\n30\n30\n30\n30\n30\n30\n30\n30\n30\n30\n30\n30\n30\n30\n30\n30\n30\n30\n30\n30\n30\n30\n30\n30\n30\n30\n30\n30\n30\n30\n"], "outputs": ["7\n1\n", "3\n3\n3\n3\n3\n3\n3\n3\n3\n", "1\n3\n7\n15\n31\n63\n127\n255\n511\n1023\n2047\n4095\n8191\n16383\n32767\n65535\n131071\n262143\n524287\n1048575\n2097151\n4194303\n8388607\n16777215\n33554431\n67108863\n134217727\n268435455\n536870911\n1073741823\n", "1073741823\n1073741823\n1073741823\n1073741823\n1073741823\n1073741823\n1073741823\n1073741823\n1073741823\n1073741823\n1073741823\n1073741823\n1073741823\n1073741823\n1073741823\n1073741823\n1073741823\n1073741823\n1073741823\n1073741823\n1073741823\n1073741823\n1073741823\n1073741823\n1073741823\n1073741823\n1073741823\n1073741823\n1073741823\n1073741823\n"]}
517
763
coding
Solve the programming task below in a Python markdown code block. In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick. He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings "abc" and "abca" suit him, while the string "aba" doesn't. He also want the number of letters 'c' in his string to be as little as possible. -----Input----- The first line contains single integer n (1 ≤ n ≤ 2·10^5) — the length of the string. -----Output----- Print the string that satisfies all the constraints. If there are multiple answers, print any of them. -----Examples----- Input 2 Output aa Input 3 Output bba -----Note----- A palindrome is a sequence of characters which reads the same backward and forward.
{"inputs": ["2\n", "3\n", "1\n", "6\n", "4\n", "6\n", "1\n", "3\n"], "outputs": ["aa\n", "aab\n", "a\n", "aabbaa\n", "aabb\n", "aabbaa\n", "a\n", "aab\n"]}
236
75
coding
Solve the programming task below in a Python markdown code block. There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = `E`, and west if S_i = `W`. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. Constraints * 2 \leq N \leq 3 \times 10^5 * |S| = N * S_i is `E` or `W`. Input Input is given from Standard Input in the following format: N S Output Print the minimum number of people who have to change their directions. Examples Input 5 WEEWW Output 1 Input 12 WEWEWEEEWWWE Output 4 Input 8 WWWWWEEE Output 3
{"inputs": ["5\nWWEEW", "5\nEWEWW", "5\nWWEWE", "5\nWEWEW", "5\nWEWWE", "5\nEWWWE", "5\nEWWEW", "5\nWWWEE"], "outputs": ["2\n", "1\n", "2\n", "2\n", "2\n", "1\n", "1\n", "2\n"]}
275
93
coding
Solve the programming task below in a Python markdown code block. Coding decimal numbers with factorials is a way of writing out numbers in a base system that depends on factorials, rather than powers of numbers. In this system, the last digit is always `0` and is in base 0!. The digit before that is either `0 or 1` and is in base 1!. The digit before that is either `0, 1, or 2` and is in base 2!, etc. More generally, the nth-to-last digit is always `0, 1, 2, ..., n` and is in base n!. Read more about it at: http://en.wikipedia.org/wiki/Factorial_number_system ## Example The decimal number `463` is encoded as `"341010"`, because: 463 = 3×5! + 4×4! + 1×3! + 0×2! + 1×1! + 0×0! If we are limited to digits `0..9`, the biggest number we can encode is 10!-1 (= 3628799). So we extend `0..9` with letters `A..Z`. With these 36 digits we can now encode numbers up to 36!-1 (= 3.72 × 10^(41)) ## Task We will need two functions. The first one will receive a decimal number and return a string with the factorial representation. ~~~if:java **Note:** the input number is at most a long. ~~~ The second one will receive a string with a factorial representation and produce the decimal representation. Given numbers will always be positive. Also feel free to reuse/extend the following starter code: ```python def dec2FactString(nb): ```
{"functional": "_inputs = [[463], [2982], [36288000]]\n_outputs = [['341010'], ['4041000'], ['A0000000000']]\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(dec2FactString(*i), o[0])"}
392
201
coding
Solve the programming task below in a Python markdown code block. A very passive-aggressive co-worker of yours was just fired. While he was gathering his things, he quickly inserted a bug into your system which renamed everything to what looks like jibberish. He left two notes on his desk, one reads: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" while the other reads: "Uif usjdl up uijt lbub jt tjnqmf kvtu sfqmbdf fwfsz mfuufs xjui uif mfuufs uibu dpnft cfgpsf ju". Rather than spending hours trying to find the bug itself, you decide to try and decode it. If the input is not a string, your function must return "Input is not a string". Your function must be able to handle capital and lower case letters. You will not need to worry about punctuation. Also feel free to reuse/extend the following starter code: ```python def one_down(txt): ```
{"functional": "_inputs = [['Ifmmp'], ['Uif usjdl up uijt lbub jt tjnqmf'], [45], ['XiBu BcPvU dSbAz UfYu'], [['Hello there', 'World']], ['BMM DBQT NBZCF'], ['qVAamFt BsF gVo'], ['CodeWars RockZ'], [''], ['J ipqf zpv bsf ibwjoh b ojdf ebz']]\n_outputs = [['Hello'], ['The trick to this kata is simple'], ['Input is not a string'], ['WhAt AbOuT cRaZy TeXt'], ['Input is not a string'], ['ALL CAPS MAYBE'], ['pUZzlEs ArE fUn'], ['BncdVzqr QnbjY'], [''], ['I hope you are having a nice day']]\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(one_down(*i), o[0])"}
204
328
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef wants to give a gift to Chefina to celebrate their anniversary. Of course, he has a sequence $a_{1}, a_{2}, \ldots, a_{N}$ ready for this occasion. Since the half-heart necklace is kind of cliche, he decided to cut his sequence into two pieces and give her a piece instead. Formally, he wants to choose an integer $l$ ($1 ≤ l < N$) and split the sequence into its prefix with length $l$ and its suffix with length $N-l$. Chef wants his gift to be *cute*; he thinks that it will be cute if the product of the elements in Chefina's piece is coprime with the product of the elements in his piece. Can you tell him where to cut the sequence? Find the smallest valid $l$ such that Chef's gift would be cute. ------ 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 the integer $N$. The second line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. ------ Output ------ For each test case, print a single line containing one integer $l$ where Chef should cut the sequence. It is guaranteed that a solution exists for the given test data. ------ Constraints ------ $1 ≤ T ≤ 20$ $2 ≤ N ≤ 10^{5}$ $2 ≤ a_{i} ≤ 10^{5}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $3 \cdot 10^{5}$ ------ Subtasks ------ Subtask #1 (25 points): $N ≤ 200$ the sum of $N$ over all test cases does not exceed $600$ Subtask #2 (40 points): $N ≤ 2,000$ the sum of $N$ over all test cases does not exceed $6,000$ Subtask #3 (35 points): original constraints ----- Sample Input 1 ------ 1 4 2 3 4 5 ----- Sample Output 1 ------ 3
{"inputs": ["1\n4\n2 3 4 5"], "outputs": ["3"]}
531
22
coding
Solve the programming task below in a Python markdown code block. Chef is the event manager of his college. He has been assigned the task to manage the upcoming tech fest. There are $K$ rooms where the event can take place, and at a particular time only one event can be organized in a room for a particular time interval. Each event coordinator has their strictly preferred room $P_i$, and if the room is already occupied he simply cancels that event.Chef wants to maximize the total number of events,and so he allows or disallows certain events in order to achieve the task . Chef is busy handling his events so the chef needs your help . Given a list of $N$ events with their start time $S_i$,end time $E_i$ and preferred room $P_i$,you need to calculate the maximum number of events that can take place. Note that the $i$th event wants to occupy the $p_i$ room from [$s_i$, $f_i$) . -----Input:----- The first line contains an integer $T$ denoting the number of test cases . Each of the next $T$ lines contains two integers $N$ and $K$ , the number of events and the number of rooms respectively . Each of the next $N$ lines contains three integers $s_i$ ,$e_i$ and $p_i$,the start time ,end time and the preferred room of ith event. -----Output:----- Print the maximum number of events that can take place. -----Constraints----- - $1 \leq T \leq 100$ - $1 \leq N \leq 10^3$ - $1 \leq K \leq 10^5$ - $1 \leq Si < Ei \leq 10^9$ - $1 \leq Pi \leq K$ -----Sample Input:----- 1 4 2 1 10 1 10 20 2 15 50 2 20 30 2 -----Sample Output:----- 3 -----EXPLANATION:----- Chef can allow events 1st ,2nd and 4th,to get the maximum 3.
{"inputs": ["1\n4 2\n1 10 1\n10 20 2\n15 50 2\n20 30 2"], "outputs": ["3"]}
468
47
coding
Solve the programming task below in a Python markdown code block. HQ9+ is a joke programming language which has only four one-character instructions: * "H" prints "Hello, World!", * "Q" prints the source code of the program itself, * "9" prints the lyrics of "99 Bottles of Beer" song, * "+" increments the value stored in the internal accumulator. Instructions "H" and "Q" are case-sensitive and must be uppercase. The characters of the program which are not instructions are ignored. You are given a program written in HQ9+. You have to figure out whether executing this program will produce any output. Input The input will consist of a single line p which will give a program in HQ9+. String p will contain between 1 and 100 characters, inclusive. ASCII-code of each character of p will be between 33 (exclamation mark) and 126 (tilde), inclusive. Output Output "YES", if executing the program will produce any output, and "NO" otherwise. Examples Input Hi! Output YES Input Codeforces Output NO Note In the first case the program contains only one instruction — "H", which prints "Hello, World!". In the second case none of the program characters are language instructions.
{"inputs": ["Q\n", "~\n", "8\n", "H\n", "!\n", "9\n", "3\n", "+\n"], "outputs": ["YES\n", "NO\n", "NO\n", "YES\n", "NO\n", "YES\n", "NO\n", "NO\n"]}
280
70