task_type
stringclasses
1 value
problem
stringlengths
209
3.39k
answer
stringlengths
35
6.15k
problem_tokens
int64
60
774
answer_tokens
int64
12
2.04k
coding
Solve the programming task below in a Python markdown code block. There is a directed graph with N vertices and M edges. The i-th edge (1≤i≤M) points from vertex a_i to vertex b_i, and has a weight c_i. We will play the following single-player game using this graph and a piece. Initially, the piece is placed at vertex 1, and the score of the player is set to 0. The player can move the piece as follows: - When the piece is placed at vertex a_i, move the piece along the i-th edge to vertex b_i. After this move, the score of the player is increased by c_i. The player can end the game only when the piece is placed at vertex N. The given graph guarantees that it is possible to traverse from vertex 1 to vertex N. When the player acts optimally to maximize the score at the end of the game, what will the score be? If it is possible to increase the score indefinitely, print inf. -----Constraints----- - 2≤N≤1000 - 1≤M≤min(N(N-1),2000) - 1≤a_i,b_i≤N (1≤i≤M) - a_i≠b_i (1≤i≤M) - a_i≠a_j or b_i≠b_j (1≤i<j≤M) - -10^9≤c_i≤10^9 (1≤i≤M) - c_i is an integer. - In the given graph, there exists a path from vertex 1 to vertex N. -----Input----- Input is given from Standard Input in the following format: N M a_1 b_1 c_1 a_2 b_2 c_2 : a_M b_M c_M -----Output----- Print the maximum possible score at the end of the game, if it is finite. If it is possible to increase the score indefinitely, print inf. -----Sample Input----- 3 3 1 2 4 2 3 3 1 3 5 -----Sample Output----- 7 There are two ways to move the piece to vertex N=3: - vertex 1 → vertex 2 → vertex 3 : score 4+3=7 - vertex 1 → vertex 3 : score 5 Thus, the maximum possible score at the end of the game is 7.
{"inputs": ["2 2\n1 2 2\n2 1 1", "2 2\n1 2 1\n2 1 1", "2 2\n1 2 1\n2 1 1\n", "3 3\n1 2 4\n2 2 3\n1 3 5", "3 3\n1 2 6\n2 3 0\n1 3 5", "3 3\n2 1 6\n2 3 1\n1 3 1", "3 2\n1 2 6\n1 3 0\n1 3 5", "3 3\n2 1 3\n2 3 1\n1 3 2"], "outputs": ["inf\n", "inf", "inf\n", "5\n", "6\n", "1\n", "0\n", "2\n"]}
517
204
coding
Solve the programming task below in a Python markdown code block. We get more and more news about DDoS-attacks of popular websites. Arseny is an admin and he thinks that a website is under a DDoS-attack if the total number of requests for a some period of time exceeds $100 \cdot t$, where $t$ — the number of seconds in this time segment. Arseny knows statistics on the number of requests per second since the server is booted. He knows the sequence $r_1, r_2, \dots, r_n$, where $r_i$ — the number of requests in the $i$-th second after boot. Determine the length of the longest continuous period of time, which Arseny considers to be a DDoS-attack. A seeking time period should not go beyond the boundaries of the segment $[1, n]$. -----Input----- The first line contains $n$ ($1 \le n \le 5000$) — number of seconds since server has been booted. The second line contains sequence of integers $r_1, r_2, \dots, r_n$ ($0 \le r_i \le 5000$), $r_i$ — number of requests in the $i$-th second. -----Output----- Print the only integer number — the length of the longest time period which is considered to be a DDoS-attack by Arseny. If it doesn't exist print 0. -----Examples----- Input 5 100 200 1 1 1 Output 3 Input 5 1 2 3 4 5 Output 0 Input 2 101 99 Output 1
{"inputs": ["1\n41\n", "1\n41\n", "1\n75\n", "1\n101\n", "1\n101\n", "1\n001\n", "1\n000\n", "1\n654\n"], "outputs": ["0\n", "0\n", "0\n", "1\n", "1\n", "0\n", "0\n", "1\n"]}
376
99
coding
Solve the programming task below in a Python markdown code block. We guessed some integer number $x$. You are given a list of almost all its divisors. Almost all means that there are all divisors except $1$ and $x$ in the list. Your task is to find the minimum possible integer $x$ that can be the guessed number, or say that the input data is contradictory and it is impossible to find such number. You have to answer $t$ independent queries. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 25$) — the number of queries. Then $t$ queries follow. The first line of the query contains one integer $n$ ($1 \le n \le 300$) — the number of divisors in the list. The second line of the query contains $n$ integers $d_1, d_2, \dots, d_n$ ($2 \le d_i \le 10^6$), where $d_i$ is the $i$-th divisor of the guessed number. It is guaranteed that all values $d_i$ are distinct. -----Output----- For each query print the answer to it. If the input data in the query is contradictory and it is impossible to find such number $x$ that the given list of divisors is the list of almost all its divisors, print -1. Otherwise print the minimum possible $x$. -----Example----- Input 2 8 8 2 12 6 4 24 16 3 1 2 Output 48 4
{"inputs": ["2\n1\n25\n2\n5 25\n", "2\n1\n25\n2\n5 25\n", "2\n1\n31\n2\n5 25\n", "1\n2\n141440 554400\n", "1\n2\n141440 554400\n", "2\n8\n8 2 12 6 4 24 16 3\n1\n2\n", "2\n8\n8 2 12 6 4 24 16 3\n1\n2\n", "2\n5\n999983 999979 999961 999959 999952\n1\n23\n"], "outputs": ["-1\n125\n", "-1\n125\n", "961\n125\n", "-1\n", "-1\n", "48\n4\n", "48\n4\n", "-1\n529\n"]}
347
255
coding
Solve the programming task below in a Python markdown code block. # Task Yesterday you found some shoes in your room. Each shoe is described by two values: ``` type indicates if it's a left or a right shoe; size is the size of the shoe. ``` Your task is to check whether it is possible to pair the shoes you found in such a way that each pair consists of a right and a left shoe of an equal size. # Example For: ``` shoes = [[0, 21], [1, 23], [1, 21], [0, 23]] ``` the output should be `true;` For: ``` shoes = [[0, 21], [1, 23], [1, 21], [1, 23]] ``` the output should be `false.` # Input/Output - `[input]` 2D integer array `shoes` Array of shoes. Each shoe is given in the format [type, size], where type is either 0 or 1 for left and right respectively, and size is a positive integer. Constraints: `2 ≤ shoes.length ≤ 50, 1 ≤ shoes[i][1] ≤ 100.` - `[output]` a boolean value `true` if it is possible to pair the shoes, `false` otherwise. Also feel free to reuse/extend the following starter code: ```python def pair_of_shoes(shoes): ```
{"functional": "_inputs = [[[[0, 21], [1, 23], [1, 21], [0, 23]]], [[[0, 21], [1, 23], [1, 21], [1, 23]]], [[[0, 23], [1, 21], [1, 23], [0, 21], [1, 22], [0, 22]]], [[[0, 23], [1, 21], [1, 23], [0, 21]]], [[[0, 23], [1, 21], [1, 22], [0, 21]]], [[[0, 23]]], [[[0, 23], [1, 23]]], [[[0, 23], [1, 23], [1, 23], [0, 23]]], [[[0, 23], [1, 22]]], [[[0, 23], [1, 23], [1, 23], [0, 23], [0, 23], [0, 23]]]]\n_outputs = [[True], [False], [True], [True], [False], [False], [True], [True], [False], [False]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(pair_of_shoes(*i), o[0])"}
333
450
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin chinese, Russian and Vietnamese as well. Chef has decided to start a new firm called PatentChef. However, he's stuck with some big legal issues. Their firm has received offers from a lot of companies, so Chef told his friend Junior Chef to look over some patent cases and solve them as quickly as he can. Junior Chef is very smart and has an eye for every little detail. He quickly found a case and went ahead to solve it. The patent case is as follows: There are N patents to be filed for a company. Chef’s firm has the first M months of the year 2018 to finish this task. (The months in a year are numbered 1 through 12.) Chef's firm has K workers (including Junior Chef) available to work on this case. Each worker can prepare exactly one patent per month. Junior Chef assigns work to workers starting from the first month of the year. He can have any workers work on this case on any month provided that they're chosen according to the following conditions: Each worker can only work on this patent case for at most one month. Each worker has to work either on an even month or an odd month of the year. You are given a string S with length K and the following meaning: for each valid i, if the i-th character of S is E, worker i has to work on an even month; if it's O, this worker has to work on an odd month. At most X workers can work on this patent case each month. Determine whether Chef’s firm and Junior Chef can finish this patent case in time. ------ 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 four space-separated integers N, M, X and K. The second line contains a single string S. ------ Output ------ For each test case, print a single line containing the string "yes" if it's possible to finish the patent case or "no" otherwise (without quotes). ------ Constraints ------ $1 ≤ T ≤ 1000$ $0 ≤ X ≤ 10^{6}$ $1 ≤ N, K ≤ 10^{6}$ $0 ≤ M ≤ 12$ $1 ≤ sum of K over all test cases ≤ 10^{7}$ $|S| = K$ $each character of S is either E or O$ ------ Subtasks ------ Subtask #1 (20 points): 1 ≤ N ≤ M ≤ 12 Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 2 4 4 2 4 EEOO 4 3 1 4 EEOO ----- Sample Output 1 ------ yes no ----- explanation 1 ------ Example case 1: The firm can prepare 2 patents per month, so in the first month (odd), workers 3 and 4 can work on 2 patents, and in the second month (even), workers 1 and 2 can work on the remaining 2 patents. Example case 2: There is no way for multiple workers to work on the same (even or odd) month. Hence, it's impossible to finish 4 patents in 3 months.
{"inputs": ["2\n4 4 2 4\nEEOO\n4 3 1 4\nEEOO"], "outputs": ["yes\nno"]}
714
38
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. A sentence consists of lowercase letters ('a' to 'z'), digits ('0' to '9'), hyphens ('-'), punctuation marks ('!', '.', and ','), and spaces (' ') only. Each sentence can be broken down into one or more tokens separated by one or more spaces ' '. A token is a valid word if all three of the following are true: It only contains lowercase letters, hyphens, and/or punctuation (no digits). There is at most one hyphen '-'. If present, it must be surrounded by lowercase characters ("a-b" is valid, but "-ab" and "ab-" are not valid). There is at most one punctuation mark. If present, it must be at the end of the token ("ab,", "cd!", and "." are valid, but "a!b" and "c.," are not valid). Please complete the following python code precisely: ```python class Solution: def countValidWords(self, sentence: str) -> int: ```
{"functional": "def check(candidate):\n assert candidate(sentence = \"cat and dog\") == 3\n assert candidate(sentence = \"!this 1-s b8d!\") == 0\n assert candidate(sentence = \"alice and bob are playing stone-game10\") == 5\n\n\ncheck(Solution().countValidWords)"}
224
77
coding
Solve the programming task below in a Python markdown code block. You are given a sequence $a_1, a_2, \dots, a_n$ consisting of $n$ integers. You may perform the following operation on this sequence: choose any element and either increase or decrease it by one. Calculate the minimum possible difference between the maximum element and the minimum element in the sequence, if you can perform the aforementioned operation no more than $k$ times. -----Input----- The first line contains two integers $n$ and $k$ $(2 \le n \le 10^{5}, 1 \le k \le 10^{14})$ — the number of elements in the sequence and the maximum number of times you can perform the operation, respectively. The second line contains a sequence of integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^{9})$. -----Output----- Print the minimum possible difference between the maximum element and the minimum element in the sequence, if you can perform the aforementioned operation no more than $k$ times. -----Examples----- Input 4 5 3 1 7 5 Output 2 Input 3 10 100 100 100 Output 0 Input 10 9 4 5 5 7 5 4 5 2 4 3 Output 1 -----Note----- In the first example you can increase the first element twice and decrease the third element twice, so the sequence becomes $[3, 3, 5, 5]$, and the difference between maximum and minimum is $2$. You still can perform one operation after that, but it's useless since you can't make the answer less than $2$. In the second example all elements are already equal, so you may get $0$ as the answer even without applying any operations.
{"inputs": ["3 3\n7 12 8\n", "3 3\n7 12 8\n", "4 5\n3 1 7 5\n", "4 5\n3 1 7 5\n", "5 1\n6 4 6 7 7\n", "5 1\n6 4 6 7 7\n", "5 1\n6 4 6 11 7\n", "3 10\n100 100 100\n"], "outputs": ["2\n", "2\n", "2\n", "2\n", "2\n", "2\n", "6\n", "0\n"]}
411
160
coding
Solve the programming task below in a Python markdown code block. Given a complete binary tree with the height of H, we index the nodes respectively top-down and left-right from 1. The i-th node stores a positive integer Vi. Define Pi as follows: Pi=Vi if the i-th node is a leaf, otherwise Pi=max(Vi*PL, Vi*PR), where L and R are the indices of the left and right children of i, respectively. Your task is to caculate the value of P1. -----Input----- There are several test cases (fifteen at most), each formed as follows: - The first line contains a positive integer H (H ≤ 15). - The second line contains 2H-1 positive integers (each having a value of 109 at most), the i-th integer shows the value of Vi. The input is ended with H = 0. -----Output----- For each test case, output on a line an integer which is the respective value of P1 found, by modulo of 1,000,000,007. -----Example----- Input: 2 1 2 3 3 3 1 5 2 6 4 7 0 Output: 3 105 Explanation: The second test case is constructed as follows: 3 / \ / \ 1 5 / \ / \ 2 6 4 7
{"inputs": ["2\n1 2 3\n3\n3 1 5 2 6 4 7\n0"], "outputs": ["3\n105"]}
306
40
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an integer num. You will apply the following steps exactly two times: Pick a digit x (0 <= x <= 9). Pick another digit y (0 <= y <= 9). The digit y can be equal to x. Replace all the occurrences of x in the decimal representation of num by y. The new integer cannot have any leading zeros, also the new integer cannot be 0. Let a and b be the results of applying the operations to num the first and second times, respectively. Return the max difference between a and b.   Please complete the following python code precisely: ```python class Solution: def maxDiff(self, num: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(num = 555) == 888\n assert candidate(num = 9) == 8\n assert candidate(num = 123456) == 820000\n assert candidate(num = 10000) == 80000\n assert candidate(num = 9288) == 8700\n\n\ncheck(Solution().maxDiff)"}
160
107
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given the root of a binary tree, return its maximum depth. A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.   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 maxDepth(self, root: Optional[TreeNode]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(root = tree_node([3,9,20,None,None,15,7])) == 3\n assert candidate(root = tree_node([1,None,2])) == 2\n\n\ncheck(Solution().maxDepth)"}
141
62
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list. The first node is considered odd, and the second node is even, and so on. Note that the relative order inside both the even and odd groups should remain as it was in the input. You must solve the problem in O(1) extra space complexity and O(n) time complexity.   Please complete the following python code precisely: ```python # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def oddEvenList(self, head: Optional[ListNode]) -> Optional[ListNode]: ```
{"functional": "def check(candidate):\n assert is_same_list(candidate(head = list_node([1,2,3,4,5])), list_node([1,3,5,2,4]))\n assert is_same_list(candidate(head = list_node([2,1,3,5,6,4,7])), list_node([2,3,6,7,1,5,4]))\n\n\ncheck(Solution().oddEvenList)"}
184
100
coding
Solve the programming task below in a Python markdown code block. Did you know that the yummy golden triangle was introduced in India as early as 13th century ? By the way, I'm referring to the popular South Asian snack, Samosa. I guess its hard to code while thinking of Samosa, especially if you are very hungry now ; so lets not get in to any recipe or eating game. You have N boxes of Samosas, where each box is a cube. To pack a box, you need to use a rubber band ( pseudo-circular, elastic band ) by placing it around the box ( along 4 faces of the cube ). A (R1,R2)-rubber band has initial radius R1 and it can stretch at max to radius R2 without breaking. You can pack a cubical box of side length L using a rubber band of circumference 4 * L ( see Notes for clarity). Given M rubber bands along with their initial radius and max radius, we need to match ( assign ) some rubber bands to boxes. A box needs at least one rubber band to pack it and of course, each rubber band can be used to pack at most one box. Find the maximum number of boxes we can pack. ------ Notes ------ A pseudo-circular rubber band having a radius R has circumference of 2 * K * R , where K is a constant = 22 / 7. So, a (R1,R2) rubber band can be used to pack a cubical box of side length L, only if 2 * K * R1 ≤ 4 * L ≤ 2 * K * R2 ------ Input ------ First line contains an integer T ( number of test cases, around 20 ). T cases follow. Each test case starts with an integer N ( number of boxes, 1 ≤ N ≤ 1000 ). Next line contains N integers, the side lengths L of the N boxes ( 1 ≤ L ≤ 100,000,000 ). Next line contains an integer M ( number of rubber bands, 1 ≤ M ≤ 1000 ). Each of the next M lines contains two integers R1 R2 ( 1 ≤ R1 ≤ R2 ≤ 100,000,000 ), separated by a space. ------ Output ------ For each test case, output the maximum number of boxes you can pack, in a new line. ----- Sample Input 1 ------ 1 4 10 20 34 55 4 7 14 7 21 14 21 7 35 ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Only 1 test case here, and a possible answer can be, using (7,14) rubber band to pack box L = 10, and using (7,35) rubber band to pack box L = 55. We cannot pack more than 2 boxes.
{"inputs": ["1\n2\n10 20 34 55\n4\n7 5\n2 4\n21 6\n7 2", "1\n2\n12 20 34 55\n4\n7 5\n2 4\n21 6\n7 2", "1\n2\n12 20 34 55\n2\n7 5\n2 4\n21 6\n7 2", "1\n2\n12 20 34 55\n1\n7 5\n2 4\n21 6\n7 2", "1\n2\n10 20 34 55\n4\n7 14\n7 4\n14 6\n7 2", "1\n2\n10 20 34 55\n4\n7 14\n7 4\n21 6\n7 2", "1\n2\n10 20 34 55\n4\n7 14\n2 4\n21 6\n7 2", "1\n2\n12 20 34 55\n1\n7 10\n2 4\n21 6\n7 2"], "outputs": ["0\n", "0\n", "0\n", "0\n", "1\n", "1\n", "1\n", "1\n"]}
631
330
coding
Solve the programming task below in a Python markdown code block. Chef is given two strings A and B of length N containing lowercase English letters. Chef can rearrange both the strings in any way he wants. He wants to minimize the length of LCS ([Longest Common Subsequence] of both the strings. Find the minimum length of LCS of A and B if he optimally rearranges both the strings. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains an integer N - denoting the length of A and B. - The second line of each test case contains a string A. - The third line of each test case contains a string B. ------ Output Format ------ For each test case, output the minimum length of LCS Chef can obtain. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 10^{5}$ $A$ and $B$ contain lowercase English letters - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 4 aaaa aaab 1 c z 5 abcde cdefg ----- Sample Output 1 ------ 3 0 1 ----- explanation 1 ------ Test case 1: $\mathrm{LCS}(\texttt{aaaa}, \texttt{aaab}) = \texttt{aaa}$. It can be shown that no other rearrangement of the strings produces a smaller LCS. Test case 3: $\mathrm{LCS}(\texttt{acbed}, \texttt{gdefc}) = \texttt{c}$. Note that multiple longest common subsequences exist for this arrangement. It can be shown that no other rearrangement of the strings produces a smaller LCS.
{"inputs": ["3\n4\naaaa\naaab\n1\nc\nz\n5\nabcde\ncdefg\n"], "outputs": ["3\n0\n1\n"]}
413
40
coding
Solve the programming task below in a Python markdown code block. Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that i^{th} friend is given a_{i} sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every a_{i}. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (10^9 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (10^9 + 7). -----Input----- The first line contains an integer q representing the number of queries (1 ≤ q ≤ 10^5). Each of the next q lines contains two space space-separated integers n, f (1 ≤ f ≤ n ≤ 10^5). -----Output----- For each query, output a single integer in a line corresponding to the answer of each query. -----Examples----- Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 -----Note----- For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning.
{"inputs": ["1\n1 1\n", "1\n1 1\n", "2\n2 1\n2 2\n", "2\n2 1\n2 2\n", "2\n4 1\n2 2\n", "3\n3 1\n3 2\n3 3\n", "3\n3 1\n3 2\n3 3\n", "3\n5 1\n3 2\n3 3\n"], "outputs": ["1\n", "1\n", "0\n1\n", "0\n1\n", "0\n1\n", "0\n2\n1\n", "0\n2\n1\n", "0\n2\n1\n"]}
514
156
coding
Solve the programming task below in a Python markdown code block. Istiak is learning about arithmetic progressions. Today, he wrote an arithmetic sequence on a piece of paper. Istiak was very happy that he managed to write an arithmetic sequence and went out for lunch. Istiak's friend Rafsan likes to irritate him by playing silly pranks on him. This time, he could have chosen one element of Istiak's sequence and changed it. When Istiak came back, he was devastated to see his sequence ruined — it became a sequence $a_1, a_2, \ldots, a_N$ (possibly identical to the original sequence, if Rafsan did not change anything, in which case Istiak is just overreacting). Help him recover the original sequence. Formally, you have to find an arithmetic sequence $b_1, b_2, \ldots, b_N$ which differs from $a$ in at most one position. $b$ is said to be an arithmetic sequence if there is a real number $d$ such that $b_i - b_{i-1} = d$ for each $i$ ($2 \le i \le N$). If there are multiple valid solutions, you may find any one. -----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 $N$ space-separated integers $b_1, b_2, \ldots, b_N$. It is guaranteed that a valid solution exists. -----Constraints----- - $4 \le N \le 10^5$ - $|a_i| \le 10^9$ for each valid $i$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 3 4 1 3 10 7 5 -10 -5 0 5 10 4 2 2 2 10 -----Example Output----- 1 3 5 7 -10 -5 0 5 10 2 2 2 2 -----Explanation----- Example case 1: Rafsan changed the third element from $5$ to $10$. Example case 2: No elements were changed. Example case 3: Rafsan changed the fourth element from $2$ to $10$.
{"inputs": ["3\n4\n1 3 10 7\n5\n-10 -5 0 5 10\n4\n2 2 2 10"], "outputs": ["1 3 5 7\n-10 -5 0 5 10\n2 2 2 2"]}
580
76
coding
Solve the programming task below in a Python markdown code block. ----- RANJANA QUIZ ----- Prof. Ranjana decided to conduct a quiz in her class. She divided all the students of her class into groups of three. Consider that no student was left out after the division. She gave different sets of questions to every group. A set is said to be unique if there is no other team that received the same number of maths, science and english questions. In every set, maximum questions for each group were related to maths, then science, and the least number of questions were related to English. Total number of questions given to each team can be different. After the test, the CR of the class asked every team to report the number of questions they got on each subject. The CR wants to know the number of unique sets of questions that were given to the teams, the problem is that all the students have just submitted the number of questions of each subject but in no particular order. Help the CR to find the number of unique sets -----Input Format----- Line 1 contains the number of teams ‘n’. In the next n lines, each line contains three space separated integers,i.e, the number of questions of each subject(in no particular order). employee -----Output----- Print the number of unique sets -----Example Text Case----- Input: 5 6 5 4 2 3 7 4 6 5 7 2 3 5 3 1 Output: 1
{"inputs": ["5\n6 5 4\n2 3 7\n4 6 5\n7 2 3\n5 3 1"], "outputs": ["1"]}
326
42
coding
Solve the programming task below in a Python markdown code block. We are still with squared integers. Given 4 integers `a, b, c, d` we form the sum of the squares of `a` and `b` and then the sum of the squares of `c` and `d`. We multiply the two sums hence a number `n` and we try to decompose `n` in a sum of two squares `e` and `f` (e and f integers >= 0) so that `n = e² + f²`. More: `e` and `f` must result only from sums (or differences) of products between on the one hand `(a, b)` and on the other `(c, d)` each of `a, b, c, d` taken only once. For example, prod2sum(1, 2, 1, 3) should return [[1, 7], [5, 5]]) because ``` 1==1*3-1*2 7==2*3+1*1 5==1*2+1*3 ``` Suppose we have `a = 1, b = 2, c = 1, d = 3`. First we calculate the sums `1² + 2² = 5 and 1² + 3² = 10` hence `n = 50`. `50 = 1² + 7² or 50 = 7² + 1²` (we'll consider that these two solutions are the same) or `50 = 5² + 5²`. The return of our function will be an array of subarrays (in C an array of Pairs) sorted on the first elements of the subarrays. In each subarray the lower element should be the first. `prod2sum(1, 2, 1, 3) should return [[1, 7], [5, 5]]` `prod2sum(2, 3, 4, 5) should return [[2, 23], [7, 22]]` because `(2² + 3²) * (4² + 5²) = 533 = (7² + 22²) = (23² + 2²)` `prod2sum(1, 2, 2, 3) should return [[1, 8], [4, 7]]` `prod2sum(1, 1, 3, 5) should return [[2, 8]]` (there are not always 2 solutions). ##Hint Take a sheet of paper and with a bit of algebra try to write the product of squared numbers in another way. Also feel free to reuse/extend the following starter code: ```python def prod2sum(a, b, c, d): ```
{"functional": "_inputs = [[1, 2, 1, 3], [2, 3, 4, 5], [1, 2, 2, 3], [1, 1, 3, 5], [10, 11, 12, 13], [1, 20, -4, -5], [100, 100, 100, 100], [0, 0, 0, 0], [-14, 12, -10, 8], [7, 96, -1, 81], [112, 0, 0, 1]]\n_outputs = [[[[1, 7], [5, 5]]], [[[2, 23], [7, 22]]], [[[1, 8], [4, 7]]], [[[2, 8]]], [[[2, 263], [23, 262]]], [[[75, 104], [85, 96]]], [[[0, 20000]]], [[[0, 0]]], [[[8, 236], [44, 232]]], [[[471, 7783], [663, 7769]]], [[[0, 112]]]]\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(prod2sum(*i), o[0])"}
629
454
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an array of strings words, return the first palindromic string in the array. If there is no such string, return an empty string "". A string is palindromic if it reads the same forward and backward.   Please complete the following python code precisely: ```python class Solution: def firstPalindrome(self, words: List[str]) -> str: ```
{"functional": "def check(candidate):\n assert candidate(words = [\"abc\",\"car\",\"ada\",\"racecar\",\"cool\"]) == \"ada\"\n assert candidate(words = [\"notapalindrome\",\"racecar\"]) == \"racecar\"\n assert candidate(words = [\"def\",\"ghi\"]) == \"\"\n\n\ncheck(Solution().firstPalindrome)"}
96
81
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Your laptop keyboard is faulty, and whenever you type a character 'i' on it, it reverses the string that you have written. Typing other characters works as expected. You are given a 0-indexed string s, and you type each character of s using your faulty keyboard. Return the final string that will be present on your laptop screen.   Please complete the following python code precisely: ```python class Solution: def finalString(self, s: str) -> str: ```
{"functional": "def check(candidate):\n assert candidate(s = \"string\") == \"rtsng\"\n assert candidate(s = \"poiinter\") == \"ponter\"\n\n\ncheck(Solution().finalString)"}
119
48
coding
Solve the programming task below in a Python markdown code block. Some time ago Mister B detected a strange signal from the space, which he started to study. After some transformation the signal turned out to be a permutation p of length n or its cyclic shift. For the further investigation Mister B need some basis, that's why he decided to choose cyclic shift of this permutation which has the minimum possible deviation. Let's define the deviation of a permutation p as $\sum_{i = 1}^{i = n}|p [ i ] - i|$. Find a cyclic shift of permutation p with minimum possible deviation. If there are multiple solutions, print any of them. Let's denote id k (0 ≤ k < n) of a cyclic shift of permutation p as the number of right shifts needed to reach this shift, for example: k = 0: shift p_1, p_2, ... p_{n}, k = 1: shift p_{n}, p_1, ... p_{n} - 1, ..., k = n - 1: shift p_2, p_3, ... p_{n}, p_1. -----Input----- First line contains single integer n (2 ≤ n ≤ 10^6) — the length of the permutation. The second line contains n space-separated integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the elements of the permutation. It is guaranteed that all elements are distinct. -----Output----- Print two integers: the minimum deviation of cyclic shifts of permutation p and the id of such shift. If there are multiple solutions, print any of them. -----Examples----- Input 3 1 2 3 Output 0 0 Input 3 2 3 1 Output 0 1 Input 3 3 2 1 Output 2 1 -----Note----- In the first sample test the given permutation p is the identity permutation, that's why its deviation equals to 0, the shift id equals to 0 as well. In the second sample test the deviation of p equals to 4, the deviation of the 1-st cyclic shift (1, 2, 3) equals to 0, the deviation of the 2-nd cyclic shift (3, 1, 2) equals to 4, the optimal is the 1-st cyclic shift. In the third sample test the deviation of p equals to 4, the deviation of the 1-st cyclic shift (1, 3, 2) equals to 2, the deviation of the 2-nd cyclic shift (2, 1, 3) also equals to 2, so the optimal are both 1-st and 2-nd cyclic shifts.
{"inputs": ["2\n1 2\n", "2\n2 1\n", "2\n1 2\n", "2\n2 1\n", "3\n1 2 3\n", "3\n2 3 1\n", "3\n3 2 1\n", "3\n1 3 3\n"], "outputs": ["0 0\n", "0 1\n", "0 0\n", "0 1\n", "0 0\n", "0 1\n", "2 1\n", "1 0\n"]}
588
126
coding
Solve the programming task below in a Python markdown code block. Description F, who likes to dance, decided to practice a hardcore dance called JUMP STYLE at a certain dance hall. The floor is tiled in an N × N grid. To support the dance practice, each tile has the coordinates of the tile to jump to the next step. F has strong motor nerves through daily practice and can jump to any tile on the floor. F realized that if he continued this dance for a long enough time, he would eventually enter a steady state and just loop on the same route. F wondered how many such loops existed on this floor and decided to talk to you, the programmer. Input The input consists of multiple test cases. Each test case begins with one line containing the length N of one side of the floor. (1 ≤ N ≤ 100) The following N lines represent the coordinates of the jump destination written on each tile on the floor as follows. \\ begin {array} {ccccccc} x_ {0,0} & y_ {0,0} & x_ {1,0} & y_ {1,0} & \\ cdots & x_ {N-1,0} & y_ {N-1,0} \ \\\ x_ {0,1} & y_ {0,1} & x_ {1,1} & y_ {1,1} & \\ cdots & x_ {N-1,1} & y_ {N-1,1} \ \\\ \\ vdots & \\ vdots & \\ vdots & \\ vdots & \\ ddots & \\ vdots & \\ vdots \\\\ x_ {0, N-1} & y_ {0, N-1} & x_ {1, N-1} & y_ {1, N-1} & \\ cdots & x_ {N-1, N-1} & y_ {N-1, N-1} \\ end {array} x_ {i, j} and \\ y_ {i, j} represent the x-coordinate and y-coordinate of the jump destination written on the tile at the coordinate (i, j), respectively. The coordinates are 0-origin, and the values ​​of all coordinates are integers greater than or equal to 0 and less than N. The input ends with a line consisting of only 0s. Output For each test case, the number of different loops is output in one line. Example Input 1 0 0 2 1 1 0 1 1 0 0 0 2 1 1 0 1 1 1 1 0 3 0 1 2 2 2 1 0 2 1 2 2 1 0 0 0 1 1 1 4 3 2 2 0 3 2 2 1 1 1 0 3 1 1 3 1 0 3 2 3 3 0 2 3 1 1 1 1 3 2 1 3 0 Output 1 2 1 2 3
{"inputs": ["1\n0 0\n2\n1 1 0 1\n1 0 0 0\n2\n1 1 0 1\n1 1 1 0\n3\n0 1 2 2 2 0\n0 2 1 2 2 1\n0 0 0 1 1 1\n4\n3 2 2 0 3 2 2 1\n1 1 0 3 1 1 3 1\n0 3 2 3 3 0 2 3\n1 1 1 1 3 2 1 3\n0", "1\n0 0\n2\n1 1 0 1\n1 0 0 0\n2\n1 1 0 1\n1 1 1 0\n3\n0 1 2 2 2 0\n0 2 1 2 2 1\n0 0 0 1 1 0\n4\n3 2 2 0 3 2 2 1\n1 1 0 3 1 1 3 1\n0 3 2 3 3 0 2 3\n1 1 1 1 3 2 1 3\n0", "1\n0 0\n2\n1 1 0 1\n1 0 0 0\n2\n1 1 0 1\n1 1 1 0\n3\n0 1 2 2 2 2\n0 2 1 2 2 1\n0 0 0 1 1 0\n4\n3 2 2 0 3 2 2 1\n1 1 0 3 1 1 3 0\n0 3 2 3 3 0 2 3\n1 1 1 1 3 2 1 3\n0", "1\n0 0\n2\n1 1 0 1\n1 1 0 0\n2\n1 1 0 1\n1 1 1 0\n3\n0 1 2 2 2 1\n0 2 1 2 2 1\n0 0 0 1 1 1\n4\n3 2 2 0 3 2 2 1\n1 1 0 3 1 1 3 1\n0 3 2 3 3 0 2 3\n1 1 1 1 3 2 1 3\n0", "1\n0 0\n2\n1 1 0 1\n1 0 0 0\n2\n1 1 0 1\n1 1 1 0\n3\n0 1 2 2 2 2\n0 2 1 2 2 1\n0 0 0 1 1 1\n4\n3 2 2 0 3 2 2 1\n1 1 0 3 1 1 3 0\n0 3 2 3 3 0 2 3\n1 1 1 1 3 2 1 3\n0", "1\n0 0\n2\n1 1 0 1\n0 0 0 0\n2\n1 1 0 1\n1 1 1 0\n3\n0 1 2 2 2 2\n0 2 1 2 2 1\n0 0 0 1 1 0\n4\n3 2 2 0 3 2 2 1\n1 1 0 3 1 2 3 0\n0 3 2 3 3 0 2 3\n1 1 1 1 3 2 1 3\n0", "1\n0 0\n0\n1 1 0 1\n1 0 0 0\n2\n1 1 0 1\n1 1 1 0\n3\n0 1 2 2 2 0\n0 2 1 2 2 1\n0 1 0 1 1 0\n4\n3 2 2 0 3 2 2 1\n1 2 0 3 1 1 3 1\n0 3 2 3 3 0 2 3\n1 1 1 1 3 2 1 3\n0", "1\n0 0\n2\n1 1 0 1\n1 0 0 0\n2\n1 1 0 1\n1 1 1 0\n3\n0 1 2 2 2 2\n0 2 1 2 2 1\n0 0 0 1 1 0\n4\n3 2 2 0 3 2 2 1\n1 1 0 3 1 2 3 0\n0 3 1 3 3 0 3 3\n1 1 1 1 3 2 1 3\n0"], "outputs": ["1\n2\n1\n3\n3\n", "1\n2\n1\n4\n3\n", "1\n2\n1\n3\n2\n", "1\n1\n1\n2\n3\n", "1\n2\n1\n2\n2\n", "1\n1\n1\n3\n2\n", "1\n", "1\n2\n1\n3\n1\n"]}
688
1,286
coding
Solve the programming task below in a Python markdown code block. Takahashi has a lot of peculiar devices. These cylindrical devices receive balls from left and right. Each device is in one of the two states A and B, and for each state, the device operates as follows: * When a device in state A receives a ball from either side (left or right), the device throws out the ball from the same side, then immediately goes into state B. * When a device in state B receives a ball from either side, the device throws out the ball from the other side, then immediately goes into state A. The transition of the state of a device happens momentarily and always completes before it receives another ball. Takahashi built a contraption by concatenating N of these devices. In this contraption, * A ball that was thrown out from the right side of the i-th device from the left (1 \leq i \leq N-1) immediately enters the (i+1)-th device from the left side. * A ball that was thrown out from the left side of the i-th device from the left (2 \leq i \leq N) immediately enters the (i-1)-th device from the right side. The initial state of the i-th device from the left is represented by the i-th character in a string S. From this situation, Takahashi performed the following K times: put a ball into the leftmost device from the left side, then wait until the ball comes out of the contraption from either end. Here, it can be proved that the ball always comes out of the contraption after a finite time. Find the state of each device after K balls are processed. Constraints * 1 \leq N \leq 200,000 * 1 \leq K \leq 10^9 * |S|=N * Each character in S is either `A` or `B`. Input The input is given from Standard Input in the following format: N K S Output Print a string that represents the state of each device after K balls are processed. The string must be N characters long, and the i-th character must correspond to the state of the i-th device from the left. Examples Input 5 1 ABAAA Output BBAAA Input 5 2 ABAAA Output ABBBA Input 4 123456789 AABB Output BABA
{"inputs": ["5 1\nAAAAA", "5 2\nAAABA", "5 1\nAAABA", "5 3\nAAABA", "5 1\nAAAAB", "5 2\nAAAAA", "5 1\nAAABB", "5 3\nAAAAA"], "outputs": ["BAAAA\n", "BBABA\n", "BAABA\n", "ABABA\n", "BAAAB\n", "BBBBA\n", "BAABB\n", "AAABA\n"]}
525
112
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. An integer n is strictly palindromic if, for every base b between 2 and n - 2 (inclusive), the string representation of the integer n in base b is palindromic. Given an integer n, return true if n is strictly palindromic and false otherwise. A string is palindromic if it reads the same forward and backward.   Please complete the following python code precisely: ```python class Solution: def isStrictlyPalindromic(self, n: int) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(n = 9) == False\n assert candidate(n = 4) == False\n\n\ncheck(Solution().isStrictlyPalindromic)"}
129
46
coding
Solve the programming task below in a Python markdown code block. Given an array A of length N, your task is to find the element which repeats in A maximum number of times as well as the corresponding count. In case of ties, choose the smaller element first. ------ Input ------ First line of input contains an integer T, denoting the number of test cases. Then follows description of T cases. Each case begins with a single integer N, the length of A. Then follow N space separated integers in next line. Assume that 1 ≤ T ≤ 100, 1 ≤ N ≤ 100 and for all i in [1..N] : 1 ≤ A[i] ≤ 10000 ------ Output ------ For each test case, output two space separated integers V & C. V is the value which occurs maximum number of times and C is its count. ----- Sample Input 1 ------ 2 5 1 2 3 2 5 6 1 2 2 1 1 2 ----- Sample Output 1 ------ 2 2 1 3 ----- explanation 1 ------ In first case 2 occurs twice whereas all other elements occur only once. In second case, both 1 and 2 occur 3 times but 1 is smaller than 2.
{"inputs": ["2\n5\n1 2 3 2 5\n6\n1 2 2 1 1 2"], "outputs": ["2 2\n1 3"]}
282
44
coding
Solve the programming task below in a Python markdown code block. Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $\lfloor \frac{w \cdot a}{b} \rfloor$ dollars. Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x_1, x_2, ..., x_{n}. Number x_{i} is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save. -----Input----- The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 10^5; 1 ≤ a, b ≤ 10^9). The second line of input contains n space-separated integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 10^9). -----Output----- Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day. -----Examples----- Input 5 1 4 12 6 11 9 1 Output 0 2 3 1 1 Input 3 1 2 1 2 3 Output 1 0 1 Input 1 1 1 1 Output 0
{"inputs": ["1 1 1\n1\n", "1 1 1\n1\n", "3 1 2\n1 2 3\n", "3 1 2\n1 0 3\n", "3 1 3\n1 0 3\n", "3 1 3\n2 0 3\n", "3 1 4\n1 0 3\n", "3 1 2\n1 1 1\n"], "outputs": ["0 ", "0\n", "1 0 1 ", "1 0 1\n", "1 0 0\n", "2 0 0\n", "1 0 3\n", "1 1 1\n"]}
412
164
coding
Solve the programming task below in a Python markdown code block. Your friend Ivan asked you to help him rearrange his desktop. The desktop can be represented as a rectangle matrix of size $n \times m$ consisting of characters '.' (empty cell of the desktop) and '*' (an icon). The desktop is called good if all its icons are occupying some prefix of full columns and, possibly, the prefix of the next column (and there are no icons outside this figure). In other words, some amount of first columns will be filled with icons and, possibly, some amount of first cells of the next (after the last full column) column will be also filled with icons (and all the icons on the desktop belong to this figure). This is pretty much the same as the real life icons arrangement. In one move, you can take one icon and move it to any empty cell in the desktop. Ivan loves to add some icons to his desktop and remove them from it, so he is asking you to answer $q$ queries: what is the minimum number of moves required to make the desktop good after adding/removing one icon? Note that queries are permanent and change the state of the desktop. -----Input----- The first line of the input contains three integers $n$, $m$ and $q$ ($1 \le n, m \le 1000; 1 \le q \le 2 \cdot 10^5$) — the number of rows in the desktop, the number of columns in the desktop and the number of queries, respectively. The next $n$ lines contain the description of the desktop. The $i$-th of them contains $m$ characters '.' and '*' — the description of the $i$-th row of the desktop. The next $q$ lines describe queries. The $i$-th of them contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n; 1 \le y_i \le m$) — the position of the cell which changes its state (if this cell contained the icon before, then this icon is removed, otherwise an icon appears in this cell). -----Output----- Print $q$ integers. The $i$-th of them should be the minimum number of moves required to make the desktop good after applying the first $i$ queries. -----Examples----- Input 4 4 8 ..** .*.. *... ...* 1 3 2 3 3 1 2 3 3 4 4 3 2 3 2 2 Output 3 4 4 3 4 5 5 5 Input 2 5 5 *...* ***** 1 3 2 2 1 3 1 5 2 3 Output 2 3 3 3 2 -----Note----- None
{"inputs": ["1 1 1\n*\n1 1\n", "2 5 5\n*...*\n*****\n1 3\n2 2\n1 3\n1 5\n2 3\n", "4 4 8\n..**\n.*..\n*...\n...*\n1 3\n2 3\n3 1\n2 3\n3 4\n4 3\n2 3\n2 2\n"], "outputs": ["0\n", "2\n3\n3\n3\n2\n", "3\n4\n4\n3\n4\n5\n5\n5\n"]}
605
142
coding
Solve the programming task below in a Python markdown code block. Every day, Mike goes to his job by a bus, where he buys a ticket. On the ticket, there is a letter-code that can be represented as a string of upper-case Latin letters. Mike believes that the day will be successful in case exactly two different letters in the code alternate. Otherwise, he believes that the day will be unlucky. Please see note section for formal definition of alternating code. You are given a ticket code. Please determine, whether the day will be successful for Mike or not. Print "YES" or "NO" (without quotes) corresponding to the situation. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first and only line of each test case contains a single string S denoting the letter code on the ticket. -----Output----- For each test case, output a single line containing "YES" (without quotes) in case the day will be successful and "NO" otherwise. -----Note----- Two letters x, y where x != y are said to be alternating in a code, if code is of form "xyxyxy...". -----Constraints----- - 1 ≤ T ≤ 100 - S consists only of upper-case Latin letters Subtask 1 (50 points): - |S| = 2 Subtask 2 (50 points): - 2 ≤ |S| ≤ 100 -----Example----- Input: 2 ABABAB ABC Output: YES NO
{"inputs": ["2\nABABAB\nABC"], "outputs": ["YES\nNO"]}
331
20
coding
Solve the programming task below in a Python markdown code block. Jzzhu have n non-negative integers a1, a2, ..., an. We will call a sequence of indexes i1, i2, ..., ik (1 ≤ i1 < i2 < ... < ik ≤ n) a group of size k. Jzzhu wonders, how many groups exists such that ai1 & ai2 & ... & aik = 0 (1 ≤ k ≤ n)? Help him and print this number modulo 1000000007 (109 + 7). Operation x & y denotes bitwise AND operation of two numbers. Input The first line contains a single integer n (1 ≤ n ≤ 106). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 106). Output Output a single integer representing the number of required groups modulo 1000000007 (109 + 7). Examples Input 3 2 3 3 Output 0 Input 4 0 1 2 3 Output 10 Input 6 5 2 0 5 2 1 Output 53
{"inputs": ["1\n1\n", "1\n0\n", "1\n2\n", "1\n4\n", "1\n8\n", "1\n14\n", "2\n1 0\n", "2\n0 1\n"], "outputs": ["0\n", "1\n", "0", "0", "0", "0", "2\n", "2\n"]}
265
87
coding
Solve the programming task below in a Python markdown code block. Two players (numbered $\mbox{1}$ and $2$) are playing a game of Tower Breakers! The rules of the game are as follows: Player $\mbox{1}$ always moves first, and both players always move optimally. Initially there are $N$ towers of various heights. The players move in alternating turns. In each turn, a player can choose a tower of height $\mbox{X}$ and reduce its height to $\mathbf{Y}$, where $1\leq Y<X$ and $\mathbf{Y}$ evenly divides $\mbox{X}$. If the current player is unable to make any move, they lose the game. Given the value of $N$ and the respective height values for all towers, can you determine who will win? If the first player wins, print $\mbox{1}$; otherwise, print $2$. Input Format The first line contains an integer, $\mathbf{T}$, denoting the number of test cases. Each of the $2T$ subsequent lines defines a test case. Each test case is described over the following two lines: An integer, $N$, denoting the number of towers. $N$ space-separated integers, $h_0,h_1,\ldots,h_{N-1}$, where each $h_i$ describes the height of tower $\boldsymbol{i}$. Constraints $1\leq T\leq100$ $1\leq N\leq100$ $1\leq h_i\leq10^6$ Output Format For each test case, print a single integer denoting the winner (i.e., either $\mbox{1}$ or $2$) on a new line. Sample Input 2 2 1 2 3 1 2 3 Sample Output 1 2 Explanation Test Case 0: Player $\mbox{1}$ reduces the second tower to height $\mbox{1}$ and subsequently wins. Test Case 1: There are two possible moves: Reduce the second tower to $\mbox{1}$ Reduce the third tower to $\mbox{1}$. Whichever move player $\mbox{1}$ makes, player $2$ will make the other move. Thus, player $2$ wins.
{"inputs": ["2\n2 \n1 2\n3 \n1 2 3\n"], "outputs": ["1\n2\n"]}
507
32
coding
Solve the programming task below in a Python markdown code block. Oh no! You have stumbled upon a mysterious signal consisting of beeps of various lengths, and it is of utmost importance that you find out the secret message hidden in the beeps. There are long and short beeps, the longer ones roughly three times as long as the shorter ones. Hmm... that sounds familiar. That's right: your job is to implement a decoder for the Morse alphabet. Rather than dealing with actual beeps, we will use a common string encoding of Morse. A long beep is represened by a dash (`-`) and a short beep by a dot (`.`). A series of long and short beeps make up a letter, and letters are separated by spaces (` `). Words are separated by double spaces. You should implement the International Morse Alphabet. You need to support letters a-z and digits 0-9 as follows: a .- h .... o --- u ..- 1 .---- 6 -.... b -... i .. p .--. v ...- 2 ..--- 7 --... c -.-. j .--- q --.- w .-- 3 ...-- 8 ---.. d -.. k -.- r .-. x -..- 4 ....- 9 ----. e . l .-.. s ... y -.-- 5 ..... 0 ----- f ..-. m -- t - z --.. g --. n -. ## Examples .... . .-.. .-.. --- .-- --- .-. .-.. -.. → "hello world" .---- ... - .- -. -.. ..--- -. -.. → "1st and 2nd" ```if:python A dictionnary `TOME` is preloaded for you, with the information above to convert morse code to letters. ``` ```if:javascrip An object `TOME` is preloaded for you, with the information above to convert morse code to letters. ``` ```if:ruby A Hashmap `$dict` is preloaded for you, with the information above to convert morse code to letters. ``` Also feel free to reuse/extend the following starter code: ```python def decode(s): ```
{"functional": "_inputs = [['.... . .-.. .-.. --- .-- --- .-. .-.. -..'], ['.---- ... - .- -. -.. ..--- -. -..'], ['.. .- -- .- - . ... -'], ['.- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --.. ----- .---- ..--- ...-- ....- ..... -.... --... ---.. ----.'], ['']]\n_outputs = [['hello world'], ['1st and 2nd'], ['i am a test'], ['abcdefghijklmnopqrstuvwxyz0123456789'], ['']]\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(decode(*i), o[0])"}
520
304
coding
Solve the programming task below in a Python markdown code block. Create a function that takes a list of one or more non-negative integers, and arranges them such that they form the largest possible number. Examples: `largestArrangement([4, 50, 8, 145])` returns 8504145 (8-50-4-145) `largestArrangement([4, 40, 7])` returns 7440 (7-4-40) `largestArrangement([4, 46, 7])` returns 7464 (7-46-4) `largestArrangement([5, 60, 299, 56])` returns 60565299 (60-56-5-299) `largestArrangement([5, 2, 1, 9, 50, 56])` returns 95655021 (9-56-5-50-21) Also feel free to reuse/extend the following starter code: ```python def largest_arrangement(numbers): ```
{"functional": "_inputs = [[[50, 2, 1, 9]], [[3655, 89]], [[8]], [[12, 13, 89, 155, 8, 26, 0]], [[76, 4, 3, 81, 514, 6, 716]], [[817, 6879, 163, 348, 8, 22, 47]], [[411, 742, 89, 691, 284]], [[587, 625, 638, 898, 122]], [[797, 535, 210, 87]], [[5, 2, 1, 9, 50, 56]], [[197, 853, 819]], [[23044, 2, 7626, 914, 7800]], [[451, 850, 85, 283, 4, 734, 605, 499, 249]], [[304, 12, 206, 584, 78, 69, 864, 860]], [[8346, 991, 25, 4, 67]], [[298, 268, 58, 598, 702, 603, 597]], [[422, 995, 500, 202, 772, 230, 258, 144, 752]], [[618, 514, 863, 195, 965, 262]], [[141, 63, 51, 966, 520, 48, 82, 14, 397]], [[756, 688, 8, 657, 912]], [[70, 7, 81, 28, 336, 246, 817, 77, 4, 550]], [[150, 398, 919, 890, 447, 285]], [[783, 19, 88, 5, 7]], [[10, 327, 6, 70, 13, 83, 482, 77]], [[8, 6, 590, 70]], [[6, 73, 79, 356, 7]], [[64, 29, 5, 9, 982, 3]], [[3487, 103559, 243]], [[7, 78, 79, 72, 709, 7, 94]]]\n_outputs = [[95021], [893655], [8], [8982615513120], [8176716651443], [881768794734822163], [89742691411284], [898638625587122], [87797535210], [95655021], [853819197], [91478007626230442], [858507346054994514283249], [864860786958430420612], [991834667425], [70260359859758298268], [995772752500422258230202144], [965863618514262195], [9668263520514839714141], [9128756688657], [8181777770550433628246], [919890447398285150], [887837519], [83777064823271310], [8706590], [797736356], [9982645329], [3487243103559], [9479787772709]]\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(largest_arrangement(*i), o[0])"}
262
1,354
coding
Solve the programming task below in a Python markdown code block. You are given a weighted graph with $N$ nodes and $M$ edges. Some of the nodes are marked as special nodes. Your task is to find the shortest pairwise distance between any two different special nodes. -----Input----- - The first line of the input contains three space-separated integers $N$, $M$ and $K$ denoting the number of nodes, the number of edges, and the number of special nodes. - The next line contains $K$ space-separated distinct integers $A_{1}$, $A_{2}$, $\ldots$, $A_{K}$, denoting the special nodes. - The next $M$ lines each contain three space-separated integers - $X$, $Y$, $Z$, denoting an edge connecting the nodes $X$ and $Y$, with weight $Z$. -----Output----- Output the shortest pairwise distance between any two different special nodes. -----Constraints----- - The given graph is connected. - The given graph doesn't contain self loops and multiple edges. - $1 \leq A_{i} \leq N$ - $1 \leq Z_{j} \leq 10^{4}$ - $1 \leq X_{j}, Y_{j} \leq N$ -----Subtasks----- Subtask #1 (20 points): - $2 \leq N \leq 300$ - $N-1 \leq M \leq \frac{N \cdot (N-1)}{2}$ - $2 \leq K \leq N$ Subtask #2 (25 points): - $2 \leq N \leq 10^5$ - $N-1 \leq M \leq 10^5$ - $2 \leq K \leq 10$ Subtask #3 (55 points): - $2 \leq N \leq 10^5$ - $N-1 \leq M \leq 3 \cdot 10^5$ - $2 \leq K \leq 10^4$ -----Example Input----- 5 5 3 1 3 5 1 2 3 2 3 4 3 4 1 4 5 8 1 5 19 -----Example Output----- 7 -----Explanation----- Nodes $1$, $3$, and $5$ are special nodes. Shortest distance between nodes $1$ and $3$ is $7$, and that between nodes $3$ and $5$ is $9$. Shortest distance between nodes $1$ and $5$ is $16$. Minimum of these distances is $7$. Hence answer is $7$.
{"inputs": ["5 5 3\n1 3 5\n1 2 3\n2 3 4\n3 4 1\n4 5 8\n1 5 19"], "outputs": ["7"]}
597
53
coding
Solve the programming task below in a Python markdown code block. In Python, code blocks don't have explicit begin/end or curly braces to mark beginning and end of the block. Instead, code blocks are defined by indentation. We will consider an extremely simplified subset of Python with only two types of statements. Simple statements are written in a single line, one per line. An example of a simple statement is assignment. For statements are compound statements: they contain one or several other statements. For statement consists of a header written in a separate line which starts with "for" prefix, and loop body. Loop body is a block of statements indented one level further than the header of the loop. Loop body can contain both types of statements. Loop body can't be empty. You are given a sequence of statements without indentation. Find the number of ways in which the statements can be indented to form a valid Python program. -----Input----- The first line contains a single integer N (1 ≤ N ≤ 5000) — the number of commands in the program. N lines of the program follow, each line describing a single command. Each command is either "f" (denoting "for statement") or "s" ("simple statement"). It is guaranteed that the last line is a simple statement. -----Output----- Output one line containing an integer - the number of ways the given sequence of statements can be indented modulo 10^9 + 7. -----Examples----- Input 4 s f f s Output 1 Input 4 f s f s Output 2 -----Note----- In the first test case, there is only one way to indent the program: the second for statement must be part of the body of the first one. simple statement for statement for statement simple statement In the second test case, there are two ways to indent the program: the second for statement can either be part of the first one's body or a separate statement following the first one. for statement simple statement for statement simple statement or for statement simple statement for statement simple statement
{"inputs": ["1\ns\n", "1\ns\n", "2\nf\ns\n", "2\ns\ns\n", "2\nf\ns\n", "2\ns\ns\n", "3\nf\nf\ns\n", "3\nf\nf\ns\n"], "outputs": ["1\n", "1", "1\n", "1\n", "1", "1", "1\n", "1"]}
450
98
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You have n tasks and m workers. Each task has a strength requirement stored in a 0-indexed integer array tasks, with the ith task requiring tasks[i] strength to complete. The strength of each worker is stored in a 0-indexed integer array workers, with the jth worker having workers[j] strength. Each worker can only be assigned to a single task and must have a strength greater than or equal to the task's strength requirement (i.e., workers[j] >= tasks[i]). Additionally, you have pills magical pills that will increase a worker's strength by strength. You can decide which workers receive the magical pills, however, you may only give each worker at most one magical pill. Given the 0-indexed integer arrays tasks and workers and the integers pills and strength, return the maximum number of tasks that can be completed.   Please complete the following python code precisely: ```python class Solution: def maxTaskAssign(self, tasks: List[int], workers: List[int], pills: int, strength: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(tasks = [3,2,1], workers = [0,3,3], pills = 1, strength = 1) == 3\n assert candidate(tasks = [5,4], workers = [0,0,0], pills = 1, strength = 5) == 1\n assert candidate(tasks = [10,15,30], workers = [0,10,10,10,10], pills = 3, strength = 10) == 2\n assert candidate(tasks = [5,9,8,5,9], workers = [1,6,4,2,6], pills = 1, strength = 5) == 3\n\n\ncheck(Solution().maxTaskAssign)"}
233
178
coding
Solve the programming task below in a Python markdown code block. You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? - For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. - For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. -----Constraints----- - 2 ≤ n ≤ 10^5 - |a_i| ≤ 10^9 - Each 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 minimum necessary count of operations. -----Sample Input----- 4 1 -3 1 0 -----Sample Output----- 4 For example, the given sequence can be transformed into 1, -2, 2, -2 by four operations. The sums of the first one, two, three and four terms are 1, -1, 1 and -1, respectively, which satisfy the conditions.
{"inputs": ["4\n1 -3 1 0\n", "5\n3 -6 4 -5 7\n", "6\n-1 4 3 2 -5 4\n"], "outputs": ["4\n", "0\n", "8\n"]}
300
61
coding
Solve the programming task below in a Python markdown code block. Given N integers A_1, ..., A_N, compute A_1 \times ... \times A_N. However, if the result exceeds 10^{18}, print -1 instead. -----Constraints----- - 2 \leq N \leq 10^5 - 0 \leq A_i \leq 10^{18} - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: N A_1 ... A_N -----Output----- Print the value A_1 \times ... \times A_N as an integer, or -1 if the value exceeds 10^{18}. -----Sample Input----- 2 1000000000 1000000000 -----Sample Output----- 1000000000000000000 We have 1000000000 \times 1000000000 = 1000000000000000000.
{"inputs": ["3\n011 1 315218201", "3\n001 188 468791915", "3\n011 188 468791915", "3\n011 291 468791915", "3\n011 133 468791915", "3\n011 133 735631313", "3\n011 133 315218201", "3\n101 2947 468791915"], "outputs": ["3467400211\n", "88132880020\n", "969461680220\n", "1500602919915\n", "685842571645\n", "1076228610919\n", "461164228063\n", "139534507124005\n"]}
257
295
coding
Solve the programming task below in a Python markdown code block. You have a string S consisting of N uppercase English letters. You are allowed to perform at most one operation of following kind: Choose any position in the string, remove the character at that position and insert it back to any other place in the string. Find the lexicographically smallest string you can achieve. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains the single integer N denoting length of string S. The second line contains the string S. -----Output----- For each test case, output a single line containing the answer to the corresponding test case. -----Constraints----- - 1 ≤ T ≤ 50 - 1 ≤ N ≤ 50 - S will consist of uppercase English letters. -----Example----- Input: 2 4 DCBA 7 XYZZYZZ Output: ADCB XYYZZZZ -----Explanation----- Example case 1. The optimal solution here is to choose the last character and put it in the beginning of the string. So the answer will be ADCB Example case 2. The optimal solution here is to choose the 5-th character (1-based index) and put it between the 2-nd and the 3-rd characters. So the answer will be XYYZZZZ
{"inputs": ["2\n4\nDCBA\n7\nXYZZYZZ", "2\n4\nDCBA\n7\nXYZZYZY", "2\n4\nDCBA\n7\nXZZZYZZ", "2\n4\nABCD\n7\nXYZZYZY", "2\n4\nCBAD\n7\nXYZZYZY", "2\n4\nCBAD\n7\nYZYZZYX", "2\n4\nCDAB\n7\nYZYZZYX", "2\n4\nCDAB\n7\nYZYZZYW"], "outputs": ["ADCB\nXYYZZZZ", "ADCB\nXYYZZYZ\n", "ADCB\nXYZZZZZ\n", "ABCD\nXYYZZYZ\n", "ACBD\nXYYZZYZ\n", "ACBD\nXYZYZZY\n", "ACDB\nXYZYZZY\n", "ACDB\nWYZYZZY\n"]}
297
206
coding
Solve the programming task below in a Python markdown code block. In this Kata, you will be given an array of integers and your task is to return the number of arithmetic progressions of size `3` that are possible from that list. In each progression, the differences between the elements must be the same. ``` [1, 2, 3, 5, 7, 9] ==> 5 // [1, 2, 3], [1, 3, 5], [1, 5, 9], [3, 5, 7], and [5, 7, 9] ``` All inputs will be sorted. More examples in test cases. Good luck! Also feel free to reuse/extend the following starter code: ```python def solve(arr): ```
{"functional": "_inputs = [[[1, 2, 3, 4, 5]], [[1, 2, 3, 5, 7, 9]], [[0, 5, 8, 9, 11, 13, 14, 16, 17, 19]], [[0, 1, 2, 3, 5, 6, 7, 11, 13, 15, 17, 19]], [[0, 1, 4, 5, 7, 9, 10, 13, 15, 16, 18, 19]], [[0, 1, 2, 3, 5, 8, 11, 13, 14, 16, 18, 19]]]\n_outputs = [[4], [5], [10], [17], [15], [13]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(solve(*i), o[0])"}
172
364
coding
Solve the programming task below in a Python markdown code block. You have a fence consisting of $n$ vertical boards. The width of each board is $1$. The height of the $i$-th board is $a_i$. You think that the fence is great if there is no pair of adjacent boards having the same height. More formally, the fence is great if and only if for all indices from $2$ to $n$, the condition $a_{i-1} \neq a_i$ holds. Unfortunately, it is possible that now your fence is not great. But you can change it! You can increase the length of the $i$-th board by $1$, but you have to pay $b_i$ rubles for it. The length of each board can be increased any number of times (possibly, zero). Calculate the minimum number of rubles you have to spend to make the fence great again! You have to answer $q$ independent queries. -----Input----- The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$) — the number of queries. The first line of each query contains one integers $n$ ($1 \le n \le 3 \cdot 10^5$) — the number of boards in the fence. The following $n$ lines of each query contain the descriptions of the boards. The $i$-th line contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^9$) — the length of the $i$-th board and the price for increasing it by $1$, respectively. It is guaranteed that sum of all $n$ over all queries not exceed $3 \cdot 10^5$. It is guaranteed that answer to each query will not exceed $10^{18}$. -----Output----- For each query print one integer — the minimum number of rubles you have to spend to make the fence great. -----Example----- Input 3 3 2 4 2 1 3 5 3 2 3 2 10 2 6 4 1 7 3 3 2 6 1000000000 2 Output 2 9 0 -----Note----- In the first query you have to increase the length of second board by $2$. So your total costs if $2 \cdot b_2 = 2$. In the second query you have to increase the length of first board by $1$ and the length of third board by $1$. So your total costs if $1 \cdot b_1 + 1 \cdot b_3 = 9$. In the third query the fence is great initially, so you don't need to spend rubles.
{"inputs": ["3\n3\n2 4\n2 1\n3 5\n3\n2 3\n2 2\n2 6\n4\n1 7\n3 3\n2 6\n1000000000 2\n", "3\n3\n2 4\n2 0\n3 5\n3\n2 3\n2 2\n2 6\n4\n1 7\n3 3\n2 6\n1000000000 4\n", "3\n3\n2 4\n2 1\n0 5\n3\n2 3\n2 2\n1 6\n4\n1 7\n3 3\n2 6\n1000000000 2\n", "3\n3\n2 4\n2 0\n3 5\n3\n2 3\n2 2\n2 6\n4\n1 7\n3 3\n3 6\n1000000000 4\n", "3\n3\n2 4\n2 1\n3 5\n3\n2 3\n2 2\n2 0\n4\n1 7\n3 3\n3 6\n1000000000 8\n", "3\n3\n2 4\n2 1\n3 5\n3\n2 3\n2 0\n2 0\n4\n1 7\n6 2\n2 6\n1000000000 8\n", "3\n3\n3 4\n2 1\n3 5\n3\n2 3\n2 3\n2 0\n4\n2 7\n6 2\n2 8\n1000000000 8\n", "3\n3\n2 4\n2 1\n0 5\n3\n2 3\n2 2\n1 8\n4\n1 7\n3 3\n3 6\n1000000000 2\n"], "outputs": ["2\n2\n0\n", "0\n2\n0\n", "1\n2\n0\n", "0\n2\n3\n", "2\n2\n3\n", "2\n0\n0\n", "0\n3\n0\n", "1\n2\n3\n"]}
605
542
coding
Solve the programming task below in a Python markdown code block. There is a legend in the IT City college. A student that failed to answer all questions on the game theory exam is given one more chance by his professor. The student has to play a game with the professor. The game is played on a square field consisting of n × n cells. Initially all cells are empty. On each turn a player chooses and paint an empty cell that has no common sides with previously painted cells. Adjacent corner of painted cells is allowed. On the next turn another player does the same, then the first one and so on. The player with no cells to paint on his turn loses. The professor have chosen the field size n and allowed the student to choose to be the first or the second player in the game. What should the student choose to win the game? Both players play optimally. -----Input----- The only line of the input contains one integer n (1 ≤ n ≤ 10^18) — the size of the field. -----Output----- Output number 1, if the player making the first turn wins when both players play optimally, otherwise print number 2. -----Examples----- Input 1 Output 1 Input 2 Output 2
{"inputs": ["1\n", "2\n", "3\n", "4\n", "5\n", "4\n", "5\n", "3\n"], "outputs": ["1", "2", "1", "2", "1", "2", "1", "1"]}
258
62
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. An integer x is numerically balanced if for every digit d in the number x, there are exactly d occurrences of that digit in x. Given an integer n, return the smallest numerically balanced number strictly greater than n.   Please complete the following python code precisely: ```python class Solution: def nextBeautifulNumber(self, n: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(n = 1) == 22\n assert candidate(n = 1000) == 1333\n assert candidate(n = 3000) == 3133\n\n\ncheck(Solution().nextBeautifulNumber)"}
93
69
coding
Solve the programming task below in a Python markdown code block. You are given two integers a and b. In one turn, you can do one of the following operations: * Take an integer c (c > 1 and a should be divisible by c) and replace a with a/c; * Take an integer c (c > 1 and b should be divisible by c) and replace b with b/c. Your goal is to make a equal to b using exactly k turns. For example, the numbers a=36 and b=48 can be made equal in 4 moves: * c=6, divide b by c ⇒ a=36, b=8; * c=2, divide a by c ⇒ a=18, b=8; * c=9, divide a by c ⇒ a=2, b=8; * c=4, divide b by c ⇒ a=2, b=2. For the given numbers a and b, determine whether it is possible to make them equal using exactly k turns. Input The first line contains one integer t (1 ≤ t ≤ 10^4). Then t test cases follow. Each test case is contains three integers a, b and k (1 ≤ a, b, k ≤ 10^9). Output For each test case output: * "Yes", if it is possible to make the numbers a and b equal in exactly k turns; * "No" otherwise. The strings "Yes" and "No" can be output in any case. Example Input 8 36 48 2 36 48 3 36 48 4 2 8 1 2 8 2 1000000000 1000000000 1000000000 1 2 1 2 2 1 Output YES YES YES YES YES NO YES NO
{"inputs": ["1\n8 5469 4\n", "1\n8 5469 1\n", "1\n7 5469 1\n", "1\n8 2465 4\n", "1\n8 5469 2\n", "1\n9 5469 1\n", "1\n8 2661 4\n", "1\n9 5469 2\n"], "outputs": ["YES\n", "NO\n", "NO\n", "YES\n", "YES\n", "NO\n", "YES\n", "YES\n"]}
445
142
coding
Solve the programming task below in a Python markdown code block. You are given an array $a$ consisting of $n$ integers $a_1, a_2, \dots , a_n$. In one operation you can choose two elements of the array and replace them with the element equal to their sum (it does not matter where you insert the new element). For example, from the array $[2, 1, 4]$ you can obtain the following arrays: $[3, 4]$, $[1, 6]$ and $[2, 5]$. Your task is to find the maximum possible number of elements divisible by $3$ that are in the array after performing this operation an arbitrary (possibly, zero) number of times. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of queries. The first line of each query contains one integer $n$ ($1 \le n \le 100$). The second line of each query contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$). -----Output----- For each query print one integer in a single line — the maximum possible number of elements divisible by $3$ that are in the array after performing described operation an arbitrary (possibly, zero) number of times. -----Example----- Input 2 5 3 1 2 3 1 7 1 1 1 1 1 2 2 Output 3 3 -----Note----- In the first query of the example you can apply the following sequence of operations to obtain $3$ elements divisible by $3$: $[3, 1, 2, 3, 1] \rightarrow [3, 3, 3, 1]$. In the second query you can obtain $3$ elements divisible by $3$ with the following sequence of operations: $[1, 1, 1, 1, 1, 2, 2] \rightarrow [1, 1, 1, 1, 2, 3] \rightarrow [1, 1, 1, 3, 3] \rightarrow [2, 1, 3, 3] \rightarrow [3, 3, 3]$.
{"inputs": ["1\n5\n3 3 3 3 3\n", "1\n5\n3 3 3 3 3\n", "1\n5\n3 3 0 3 3\n", "1\n5\n3 3 1 3 3\n", "1\n5\n3 0 1 4 3\n", "1\n5\n2 1 0 2 2\n", "1\n5\n0 3 0 3 3\n", "1\n5\n3 0 1 3 3\n"], "outputs": ["5\n", "5\n", "5\n", "4\n", "3\n", "2\n", "5\n", "4\n"]}
522
166
coding
Solve the programming task below in a Python markdown code block. You have an array A of size N containing only positive numbers. You have to output the maximum possible value of A[i]%A[j] where 1<=i,j<=N. -----Input----- The first line of each test case contains a single integer N denoting the size of the array. The next N lines contains integers A1, A2, ..., AN denoting the numbers -----Output----- Output a single integer answering what is asked in the problem. -----Subtask 1 (20 points)----- - 1 ≤ N ≤ 5000 - 1 ≤ A[i] ≤ 2*(10^9) -----Subtask 2 (80 points)----- - 1 ≤ N ≤ 1000000 - 1 ≤ A[i] ≤ 2*(10^9) -----Example----- Input: 2 1 2 Output: 1 -----Explanation----- There will be four values, A[0]%A[0] = 0, A[0]%A[1]=1, A[1]%A[0]=0, A[1]%A[1]=0, and hence the output will be the maximum among them all, that is 1.
{"inputs": ["2\n1\n2"], "outputs": ["1"]}
272
16
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s.   Please complete the following python code precisely: ```python class Solution: def minCut(self, s: str) -> int: ```
{"functional": "def check(candidate):\n assert candidate(s = \"aab\") == 1\n assert candidate(s = \"a\") == 0\n assert candidate(s = \"ab\") == 1\n\n\ncheck(Solution().minCut)"}
78
55
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Consider a matrix M with dimensions width * height, such that every cell has value 0 or 1, and any square sub-matrix of M of size sideLength * sideLength has at most maxOnes ones. Return the maximum possible number of ones that the matrix M can have.   Please complete the following python code precisely: ```python class Solution: def maximumNumberOfOnes(self, width: int, height: int, sideLength: int, maxOnes: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(width = 3, height = 3, sideLength = 2, maxOnes = 1) == 4\n assert candidate(width = 3, height = 3, sideLength = 2, maxOnes = 2) == 6\n\n\ncheck(Solution().maximumNumberOfOnes)"}
127
81
coding
Solve the programming task below in a Python markdown code block. There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i. You can rearrange these children just one time in any order you like. When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points. Find the maximum total happiness points the children can earn. -----Constraints----- - 2 \leq N \leq 2000 - 1 \leq A_i \leq 10^9 - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: N A_1 A_2 ... A_N -----Output----- Print the maximum total happiness points the children can earn. -----Sample Input----- 4 1 3 4 2 -----Sample Output----- 20 If we move the 1-st child from the left to the 3-rd position from the left, the 2-nd child to the 4-th position, the 3-rd child to the 1-st position, and the 4-th child to the 2-nd position, the children earns 1 \times |1-3|+3 \times |2-4|+4 \times |3-1|+2 \times |4-2|=20 happiness points in total.
{"inputs": ["4\n1 4 4 2", "4\n1 8 4 2", "4\n2 8 4 2", "4\n4 8 4 1", "4\n1 0 4 2", "4\n1 4 4 0", "4\n2 8 0 1", "4\n1 3 4 2"], "outputs": ["22\n", "30\n", "32\n", "34\n", "15\n", "18\n", "23\n", "20"]}
320
133
coding
Solve the programming task below in a Python markdown code block. Chef has closed his restaurant and decided to run a fruit stand instead. His signature dish is a fruit chaat consisting of 2 bananas and 1 apple. He currently has X bananas and Y apples. How many chaats can he make with the fruits he currently has? ------ Input Format ------ - The first line will contain T, the number of testcases. Then the testcases follow. - Each testcase consists of a single line containing two space separated integers - X and Y ------ Output Format ------ For each testcase, output the answer on a new line. ------ Constraints ------ $1 ≤ T ≤ 100$ $0 ≤ X, Y ≤ 100$ ----- Sample Input 1 ------ 3 72 50 38 93 51 4 ----- Sample Output 1 ------ 36 19 4 ----- explanation 1 ------ Test Case $1$: Chef can make $36$ chaats using $72$ bananas and $36$ apples. Test Case $2$: Chef can make $19$ chaats using $38$ bananas and $19$ apples. Test Case $3$: Chef can make $4$ chaats using $8$ bananas and $4$ apples.
{"inputs": ["3\n72 50\n38 93\n51 4"], "outputs": ["36\n19\n4\n"]}
281
36
coding
Solve the programming task below in a Python markdown code block. Today, Mezo is playing a game. Zoma, a character in that game, is initially at position $x = 0$. Mezo starts sending $n$ commands to Zoma. There are two possible commands: 'L' (Left) sets the position $x: =x - 1$; 'R' (Right) sets the position $x: =x + 1$. Unfortunately, Mezo's controller malfunctions sometimes. Some commands are sent successfully and some are ignored. If the command is ignored then the position $x$ doesn't change and Mezo simply proceeds to the next command. For example, if Mezo sends commands "LRLR", then here are some possible outcomes (underlined commands are sent successfully): "LRLR" — Zoma moves to the left, to the right, to the left again and to the right for the final time, ending up at position $0$; "LRLR" — Zoma recieves no commands, doesn't move at all and ends up at position $0$ as well; "LRLR" — Zoma moves to the left, then to the left again and ends up in position $-2$. Mezo doesn't know which commands will be sent successfully beforehand. Thus, he wants to know how many different positions may Zoma end up at. -----Input----- The first line contains $n$ $(1 \le n \le 10^5)$ — the number of commands Mezo sends. The second line contains a string $s$ of $n$ commands, each either 'L' (Left) or 'R' (Right). -----Output----- Print one integer — the number of different positions Zoma may end up at. -----Example----- Input 4 LRLR Output 5 -----Note----- In the example, Zoma may end up anywhere between $-2$ and $2$.
{"inputs": ["1\nL\n", "1\nR\n", "1\nR\n", "1\nL\n", "2\nLR\n", "2\nLR\n", "2\nRL\n", "4\nLRLR\n"], "outputs": ["2\n", "2\n", "2\n", "2\n", "3\n", "3\n", "3\n", "5\n"]}
416
88
coding
Solve the programming task below in a Python markdown code block. VK news recommendation system daily selects interesting publications of one of $n$ disjoint categories for each user. Each publication belongs to exactly one category. For each category $i$ batch algorithm selects $a_i$ publications. The latest A/B test suggests that users are reading recommended publications more actively if each category has a different number of publications within daily recommendations. The targeted algorithm can find a single interesting publication of $i$-th category within $t_i$ seconds. What is the minimum total time necessary to add publications to the result of batch algorithm execution, so all categories have a different number of publications? You can't remove publications recommended by the batch algorithm. -----Input----- The first line of input consists of single integer $n$ — the number of news categories ($1 \le n \le 200\,000$). The second line of input consists of $n$ integers $a_i$ — the number of publications of $i$-th category selected by the batch algorithm ($1 \le a_i \le 10^9$). The third line of input consists of $n$ integers $t_i$ — time it takes for targeted algorithm to find one new publication of category $i$ ($1 \le t_i \le 10^5)$. -----Output----- Print one integer — the minimal required time for the targeted algorithm to get rid of categories with the same size. -----Examples----- Input 5 3 7 9 7 8 5 2 5 7 5 Output 6 Input 5 1 2 3 4 5 1 1 1 1 1 Output 0 -----Note----- In the first example, it is possible to find three publications of the second type, which will take 6 seconds. In the second example, all news categories contain a different number of publications.
{"inputs": ["1\n1\n7\n", "1\n2\n7\n", "1\n2\n4\n", "1\n1\n72\n", "1\n5\n16\n", "1\n3\n31\n", "1\n1\n72\n", "1\n3\n31\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
409
107
coding
Solve the programming task below in a Python markdown code block. Chef has scored A, B, and C marks in 3 different subjects respectively. Chef will fail if the average score of any two subjects is less than 35. Determine whether Chef will pass or fail. ------ Input Format ------ - First line will contain T, number of test cases. Then the test cases follow. - Each test case contains of a single line of input, three integers A, B, C, denoting the Chef's score in the three subjects. ------ Output Format ------ For each test case, if Chef will pass, print PASS, otherwise print FAIL. You may print each character of the string in uppercase or lowercase (for example, the strings pass, Pass, pAss, and PASS will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 1000$ $0 ≤ A,B,C ≤ 100$ ----- Sample Input 1 ------ 4 23 47 52 28 36 80 0 100 0 35 35 35 ----- Sample Output 1 ------ Pass Fail Fail Pass ----- explanation 1 ------ Test case $1$: The average of the first two subjects is $35$, the average of the first and last subject is $37.5$, whereas the average of the last two subjects is $49.5$. Since all averages are greater than or equal to $35$, Chef will pass. Test case $2$: Since the average of the first two subjects is $32$ which is less than $35$, Chef will fail. Test case $3$: Since the average of the first and last subjects subjects is $0$ which is less than $35$, Chef will fail. Test case $4$: Since the average of any two subjects is $35$ which is greater than or equal to $35$, Chef will pass.
{"inputs": ["4\n23 47 52\n28 36 80\n0 100 0\n35 35 35\n"], "outputs": ["Pass\nFail\nFail\nPass"]}
418
54
coding
Solve the programming task below in a Python markdown code block. You are given a positive integer N. You have to split each digit of N into either of two non-empty subsequences A or B. For example, if N = 104, some possible values of (A, B) can be (10, 4), (14, 0) and (1, 4). Note that, after separating the digits into A and B, these subsequences are considered as positive integers and thus leading zeros can be omitted. Let us define a function F(X,Y) = (X+Y) \%2. Find out whether it is possible to find two non-empty subsequences A and B formed out of N such that F(A,B) = 0. ------ Input Format ------ - First line will contain T, number of test cases. Then the test cases follow. - Each test case contains of a single line of input, one integer N. ------ Output Format ------ For each test case, print \texttt{YES} if it is possible to find two non-empty subsequences A and B formed out of N such that F(A,B) = 0. Otherwise, print \texttt{NO}. You may print each character of the string in uppercase or lowercase (for example, the strings \texttt{YeS}, \texttt{yEs}, \texttt{yes} and \texttt{YES} will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 1000$ $10 ≤ N ≤ 10^{9}$ ------ subtasks ------ Subtask 1 (100 points): Original constraints. ----- Sample Input 1 ------ 2 10 73452 ----- Sample Output 1 ------ NO YES ----- explanation 1 ------ Test Case $1$: The only two possibilities are: - $A = 0, B = 1$: Here $F(A,B) = (A + B) \% 2 = (0 + 1) \% 2 = 1 \% 2 = 1$. - $A = 1, B = 0$: Here $F(A,B) = (A + B) \% 2 = (1 + 0) \% 2 = 1 \% 2 = 1$. Hence it's not possible to achieve $F(A, B) = 0$. Test Case $2$: One of the possible ways to split the digits of $N$ such that $F(A,B) = 0$ is: - $A = 74, B = 352$: Here $F(A,B) = (A + B) \% 2 = (74 +352) \% 2 = 426 \% 2 = 0$.
{"inputs": ["2\n10\n73452"], "outputs": ["NO\nYES\n"]}
599
24
coding
Solve the programming task below in a Python markdown code block. Let's dive into decorators! You are given $N$ mobile numbers. Sort them in ascending order then print them in the standard format shown below: +91 xxxxx xxxxx The given mobile numbers may have $+91$, $91$ or $\mbox{0}$ written before the actual $10$ digit number. Alternatively, there may not be any prefix at all. Input Format The first line of input contains an integer $N$, the number of mobile phone numbers. $N$ lines follow each containing a mobile number. Output Format Print $N$ mobile numbers on separate lines in the required format. Sample Input 3 07895462130 919875641230 9195969878 Sample Output +91 78954 62130 +91 91959 69878 +91 98756 41230 Concept Like most other programming languages, Python has the concept of closures. Extending these closures gives us decorators, which are an invaluable asset. You can learn about decorators in 12 easy steps here. To solve the above question, make a list of the mobile numbers and pass it to a function that sorts the array in ascending order. Make a decorator that standardizes the mobile numbers and apply it to the function.
{"inputs": ["3\n07895462130\n919875641230\n9195969878\n"], "outputs": ["+91 78954 62130\n+91 91959 69878\n+91 98756 41230\n"]}
322
96
coding
Solve the programming task below in a Python markdown code block. You've had a baby. Well done. Nice isn't it? Life destroying... but in a good way. Part of your new routine is lying awake at night worrying that you've either lost the baby... or that you have more than 1! Given a string of words (x), you need to calculate how many babies are in it. To count as a baby you must have all of the letters in baby ('b', 'a', 'b', 'y'). That counts as 1. They do not need to be in order in the string. Upper and lower case letters count. Examples: If there are no babies in the string - you lost the baby!! Return a different value, as shown below: ```if-not:kotlin 'none here' = "Where's the baby?!" '' = "Where's the baby?!" ``` ```if:kotlin "none here" = null "" = null ``` Also feel free to reuse/extend the following starter code: ```python def baby_count(x): ```
{"functional": "_inputs = [['baby'], ['abby'], ['baby baby baby'], ['Why the hell are there so many babies?!'], ['Anyone remember life before babies?'], ['Happy babies boom ba by?'], ['b a b y'], [''], ['none here']]\n_outputs = [[1], [1], [3], [1], [1], [2], [1], [\"Where's the baby?!\"], [\"Where's the baby?!\"]]\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(baby_count(*i), o[0])"}
227
238
coding
Solve the programming task below in a Python markdown code block. A manufacturing project consists of exactly $K$ tasks. The board overviewing the project wants to hire $K$ teams of workers — one for each task. All teams begin working simultaneously. Obviously, there must be at least one person in each team. For a team of $A$ workers, it takes exactly $A$ days to complete the task they are hired for. Each team acts independently, unaware of the status of other teams (whether they have completed their tasks or not), and submits their result for approval on the $A$-th day. However, the board approves the project only if all $K$ teams complete their tasks on the same day — it rejects everything submitted on any other day. The day after a team finds out that its result was rejected, it resumes work on the same task afresh. Therefore, as long as a team of $A$ workers keeps getting rejected, it submits a new result of their task for approval on the $A$-th, $2A$-th, $3A$-th day etc. The board wants to hire workers in such a way that it takes exactly $X$ days to complete the project. Find the smallest number of workers it needs to hire. -----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 two space-separated integers $K$ and $X$. -----Output----- For each test case, print a single line containing one integer — the smallest required number of workers. -----Constraints----- - $1 \le T \le 40$ - $2 \le K, X \le 10^6$ -----Example Input----- 2 2 3 2 6 -----Example Output----- 4 5 -----Explanation----- Example case 1: We can hire a team of $3$ workers for task $1$ and $1$ worker for task $2$. The one-man team working on task $2$ completes it and submits the result for approval on each day, but it is rejected on the first and second day. On the third day, the team working on task $1$ also completes their task, so the project gets approved after exactly $3$ days. Example case 2: We can hire a team of $3$ workers for task $1$ and a team of $2$ workers for task $2$.
{"inputs": ["2\n2 3\n2 6"], "outputs": ["4\n5"]}
527
22
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin chinese, Russian and Vietnamese as well. When the final results for the employement competition were announced, Nour Eddin Ramadan was thrilled to find his name among the accepted co-Chefs. However, after a while Nour started wasting time at the kitchen. He would usually spend hours talking to girls, and he even invited a girl to the kitchen pretending that he is the most valuable employer at Chef's restaurant. When Chef found out about it, he decided that it was time to teach Nour a lesson. Before going home that night, Chef called Nour to his office and asked him to solve the following problem, saying that he better come to work tomorrow with a solution for it, or otherwise, it would be better if he doesn't come at all: Given T queries, where each query contains a single number Y, Nour has to find the number of pairs A and B, such that the following equation holds true: A^{2} + B ≤ Y Where A is any positive integer, and B holds (1 ≤ B ≤ 700). Nour was really busy that night (Yeah he got that girl to go out with him after all!), and he asked for your help.   ------ Input ------ The first line contains T denoting the number of test cases. T lines follows, Each line contains a single integer Y denoting the right side of the formula. ------ Output ------ For each test print a single line, containing a single number, indicating the answer for the test. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ Y ≤ 10^{10}$ $Note that A must be any positive integer, and B must hold 1 ≤ B ≤ 700.$ ----- Sample Input 1 ------ 4 2 3 4 10000000000 ----- Sample Output 1 ------ 1 2 3 69999300
{"inputs": ["4\n2\n3\n4\n10000000000"], "outputs": ["1\n2\n3\n69999300"]}
430
43
coding
Solve the programming task below in a Python markdown code block. There is an array with some numbers. All numbers are equal except for one. Try to find it! ```python find_uniq([ 1, 1, 1, 2, 1, 1 ]) == 2 find_uniq([ 0, 0, 0.55, 0, 0 ]) == 0.55 ``` It’s guaranteed that array contains at least 3 numbers. The tests contain some very huge arrays, so think about performance. This is the first kata in series: 1. Find the unique number (this kata) 2. [Find the unique string](https://www.codewars.com/kata/585d8c8a28bc7403ea0000c3) 3. [Find The Unique](https://www.codewars.com/kata/5862e0db4f7ab47bed0000e5) Also feel free to reuse/extend the following starter code: ```python def find_uniq(arr): ```
{"functional": "_inputs = [[[1, 1, 1, 2, 1, 1]], [[0, 0, 0.55, 0, 0]], [[4, 4, 4, 3, 4, 4, 4, 4]], [[5, 5, 5, 5, 4, 5, 5, 5]], [[6, 6, 6, 6, 6, 5, 6, 6]], [[7, 7, 7, 7, 7, 7, 6, 7]], [[8, 8, 8, 8, 8, 8, 8, 7]], [[3, 3, 3, 3, 3, 3, 3, 2]], [[2, 2, 2, 2, 2, 2, 2, 1]], [[0, 1, 1, 1, 1, 1, 1, 1]]]\n_outputs = [[2], [0.55], [3], [4], [5], [6], [7], [2], [1], [0]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(find_uniq(*i), o[0])"}
238
410
coding
Solve the programming task below in a Python markdown code block. There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left. These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}. We say the grid is a good grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N: - If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same. - If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different. Here, X \% Y represents X modulo Y. We will repaint zero or more squares so that the grid will be a good grid. For a square, the wrongness when the color of the square is X before repainting and Y after repainting, is D_{X,Y}. Find the minimum possible sum of the wrongness of all the squares. -----Constraints----- - 1 \leq N \leq 500 - 3 \leq C \leq 30 - 1 \leq D_{i,j} \leq 1000 (i \neq j),D_{i,j}=0 (i=j) - 1 \leq c_{i,j} \leq C - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: N C D_{1,1} ... D_{1,C} : D_{C,1} ... D_{C,C} c_{1,1} ... c_{1,N} : c_{N,1} ... c_{N,N} -----Output----- If the minimum possible sum of the wrongness of all the squares is x, print x. -----Sample Input----- 2 3 0 1 1 1 0 1 1 4 0 1 2 3 3 -----Sample Output----- 3 - Repaint (1,1) to Color 2. The wrongness of (1,1) becomes D_{1,2}=1. - Repaint (1,2) to Color 3. The wrongness of (1,2) becomes D_{2,3}=1. - Repaint (2,2) to Color 1. The wrongness of (2,2) becomes D_{3,1}=1. In this case, the sum of the wrongness of all the squares is 3. Note that D_{i,j} \neq D_{j,i} is possible.
{"inputs": ["2 3\n0 1 1\n1 1 1\n1 4 0\n1 2\n3 3", "2 3\n0 1 1\n1 1 1\n2 4 0\n1 2\n3 3", "2 3\n0 1 1\n1 0 1\n1 4 0\n1 2\n1 3", "2 3\n0 2 1\n1 1 1\n2 4 0\n1 2\n3 3", "2 3\n0 1 1\n1 0 1\n1 4 0\n1 2\n2 3", "2 3\n1 3 1\n1 1 1\n2 4 0\n1 2\n3 3", "2 3\n2 3 1\n2 1 1\n3 4 0\n1 2\n3 3", "2 3\n1 1 1\n1 1 1\n1 4 0\n1 2\n3 3"], "outputs": ["3\n", "4\n", "1\n", "5\n", "0\n", "6\n", "7\n", "3\n"]}
617
286
coding
Solve the programming task below in a Python markdown code block. $N$ sages are sitting around a round table with $N$ seats. Each sage holds chopsticks with his dominant hand to eat his dinner. The following happens in this situation. * If sage $i$ is right-handed and a left-handed sage sits on his right, a level of frustration $w_i$ occurs to him. A right-handed sage on his right does not cause such frustration at all. * If sage $i$ is left-handed and a right-handed sage sits on his left, a level of frustration $w_i$ occurs to him. A left-handed sage on his left does not cause such frustration at all. You wish you could minimize the total amount of frustration by clever sitting order arrangement. Given the number of sages with his dominant hand information, make a program to evaluate the minimum frustration achievable. Input The input is given in the following format. $N$ $a_1$ $a_2$ $...$ $a_N$ $w_1$ $w_2$ $...$ $w_N$ The first line provides the number of sages $N$ ($3 \leq N \leq 10$). The second line provides an array of integers $a_i$ (0 or 1) which indicate if the $i$-th sage is right-handed (0) or left-handed (1). The third line provides an array of integers $w_i$ ($1 \leq w_i \leq 1000$) which indicate the level of frustration the $i$-th sage bears. Output Output the minimum total frustration the sages bear. Examples Input 5 1 0 0 1 0 2 3 5 1 2 Output 3 Input 3 0 0 0 1 2 3 Output 0
{"inputs": ["3\n0 0 0\n1 1 3", "3\n0 1 0\n1 1 2", "3\n0 0 1\n1 2 5", "3\n0 0 1\n1 0 7", "3\n0 0 1\n2 1 7", "3\n0 0 1\n1 1 8", "3\n0 0 0\n1 1 2", "3\n0 0 0\n1 2 5"], "outputs": ["0\n", "2\n", "6\n", "7\n", "8\n", "9\n", "0\n", "0\n"]}
400
158
coding
Solve the programming task below in a Python markdown code block. You are given two integers A and B. Find the largest value among A+B, A-B and A \times B. -----Constraints----- - -1000 \leq A,B \leq 1000 - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: A B -----Output----- Print the largest value among A+B, A-B and A \times B. -----Sample Input----- 3 1 -----Sample Output----- 4 3+1=4, 3-1=2 and 3 \times 1=3. The largest among them is 4.
{"inputs": ["6 1", "6 0", "9 1", "3 0", "9 0", "4 0", "5 0", "3 5"], "outputs": ["7\n", "6\n", "10\n", "3\n", "9\n", "4\n", "5\n", "15\n"]}
149
80
coding
Solve the programming task below in a Python markdown code block. Petya has got an interesting flower. Petya is a busy person, so he sometimes forgets to water it. You are given $n$ days from Petya's live and you have to determine what happened with his flower in the end. The flower grows as follows: If the flower isn't watered for two days in a row, it dies. If the flower is watered in the $i$-th day, it grows by $1$ centimeter. If the flower is watered in the $i$-th and in the $(i-1)$-th day ($i > 1$), then it grows by $5$ centimeters instead of $1$. If the flower is not watered in the $i$-th day, it does not grow. At the beginning of the $1$-st day the flower is $1$ centimeter tall. What is its height after $n$ days? -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). Description of the test cases follows. The first line of each test case contains the only integer $n$ ($1 \leq n \leq 100$). The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_i = 0$ or $a_i = 1$). If $a_i = 1$, the flower is watered in the $i$-th day, otherwise it is not watered. -----Output----- For each test case print a single integer $k$ — the flower's height after $n$ days, or $-1$, if the flower dies. -----Examples----- Input 4 3 1 0 1 3 0 1 1 4 1 0 0 1 1 0 Output 3 7 -1 1 -----Note----- None
{"inputs": ["1\n10\n1 1 1 1 1 1 1 1 1 1\n", "4\n3\n1 0 1\n3\n0 1 1\n4\n1 0 0 1\n1\n0\n"], "outputs": ["47\n", "3\n7\n-1\n1\n"]}
441
83
coding
Solve the programming task below in a Python markdown code block. You received a whatsup message from an unknown number. Could it be from that girl/boy with a foreign accent you met yesterday evening? Write a simple regex to check if the string contains the word hallo in different languages. These are the languages of the possible people you met the night before: * hello - english * ciao - italian * salut - french * hallo - german * hola - spanish * ahoj - czech republic * czesc - polish By the way, how cool is the czech republic hallo!! PS. you can assume the input is a string. PPS. to keep this a beginner exercise you don't need to check if the greeting is a subset of word ('Hallowen' can pass the test) PS. regex should be case insensitive to pass the tests Also feel free to reuse/extend the following starter code: ```python def validate_hello(greetings): ```
{"functional": "_inputs = [['hello'], ['ciao bella!'], ['salut'], ['hallo, salut'], ['hombre! Hola!'], [\"Hallo, wie geht's dir?\"], ['AHOJ!'], ['czesc'], ['meh'], ['Ahoj']]\n_outputs = [[True], [True], [True], [True], [True], [True], [True], [True], [False], [True]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(validate_hello(*i), o[0])"}
210
236
coding
Solve the programming task below in a Python markdown code block. There are N mountains in a circle, called Mountain 1, Mountain 2, ..., Mountain N in clockwise order. N is an odd number. Between these mountains, there are N dams, called Dam 1, Dam 2, ..., Dam N. Dam i (1 \leq i \leq N) is located between Mountain i and i+1 (Mountain N+1 is Mountain 1). When Mountain i (1 \leq i \leq N) receives 2x liters of rain, Dam i-1 and Dam i each accumulates x liters of water (Dam 0 is Dam N). One day, each of the mountains received a non-negative even number of liters of rain. As a result, Dam i (1 \leq i \leq N) accumulated a total of A_i liters of water. Find the amount of rain each of the mountains received. We can prove that the solution is unique under the constraints of this problem. -----Constraints----- - All values in input are integers. - 3 \leq N \leq 10^5-1 - N is an odd number. - 0 \leq A_i \leq 10^9 - The situation represented by input can occur when each of the mountains receives a non-negative even number of liters of rain. -----Input----- Input is given from Standard Input in the following format: N A_1 A_2 ... A_N -----Output----- Print N integers representing the number of liters of rain Mountain 1, Mountain 2, ..., Mountain N received, in this order. -----Sample Input----- 3 2 2 4 -----Sample Output----- 4 0 4 If we assume Mountain 1, 2, and 3 received 4, 0, and 4 liters of rain, respectively, it is consistent with this input, as follows: - Dam 1 should have accumulated \frac{4}{2} + \frac{0}{2} = 2 liters of water. - Dam 2 should have accumulated \frac{0}{2} + \frac{4}{2} = 2 liters of water. - Dam 3 should have accumulated \frac{4}{2} + \frac{4}{2} = 4 liters of water.
{"inputs": ["3\n2 0 4", "3\n2 0 6", "3\n2 0 0", "3\n0 0 6", "3\n2 1 5", "3\n2 2 4", "3\n2 2 4\n", "5\n3 8 1 5 5"], "outputs": ["6 -2 2\n", "8 -4 4\n", "2 2 -2\n", "6 -6 6\n", "6 -2 4\n", "4 0 4", "4 0 4\n", "-4 10 6 -4 14\n"]}
493
152
coding
Solve the programming task below in a Python markdown code block. Little Joty has got a task to do. She has a line of n tiles indexed from 1 to n. She has to paint them in a strange pattern. An unpainted tile should be painted Red if it's index is divisible by a and an unpainted tile should be painted Blue if it's index is divisible by b. So the tile with the number divisible by a and b can be either painted Red or Blue. After her painting is done, she will get p chocolates for each tile that is painted Red and q chocolates for each tile that is painted Blue. Note that she can paint tiles in any order she wants. Given the required information, find the maximum number of chocolates Joty can get. -----Input----- The only line contains five integers n, a, b, p and q (1 ≤ n, a, b, p, q ≤ 10^9). -----Output----- Print the only integer s — the maximum number of chocolates Joty can get. Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type. -----Examples----- Input 5 2 3 12 15 Output 39 Input 20 2 3 3 5 Output 51
{"inputs": ["1 1 1 1 1\n", "1 2 2 2 2\n", "2 1 3 3 3\n", "3 1 1 3 3\n", "4 1 5 4 3\n", "8 8 1 1 1\n", "6 6 2 8 2\n", "5 2 2 4 1\n"], "outputs": ["1\n", "0\n", "6\n", "9\n", "16\n", "8\n", "12\n", "8\n"]}
300
136
coding
Solve the programming task below in a Python markdown code block. Mr. Dango's family has extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in near future. They all have warm and gracious personality and are close each other. They usually communicate by a phone. Of course, They are all taking a family plan. This family plan is such a thing: when a choose b, and b choose a as a partner, a family plan can be applied between them and then the calling fee per unit time between them discounted to f(a, b), which is cheaper than a default fee. Each person can apply a family plan at most 2 times, but no same pair of persons can apply twice. Now, choosing their partner appropriately, all members of Mr. Dango's family applied twice. Since there are huge number of people, it is very difficult to send a message to all family members by a phone call. Mr. Dang have decided to make a phone calling network that is named 'clan' using the family plan. Let us present a definition of clan. Let S be an any subset of all phone calls that family plan is applied. Clan is S such that: 1. For any two persons (let them be i and j), if i can send a message to j through phone calls that family plan is applied (directly or indirectly), then i can send a message to j through only phone calls in S (directly or indirectly). 2. Meets condition 1 and a sum of the calling fee per unit time in S is minimized. Clan allows to send a message efficiently. For example, we suppose that one have sent a message through all calls related to him in the clan. Additionaly we suppose that every people follow a rule, "when he/she receives a message through a call in clan, he/she relays the message all other neibors in respect to clan." Then, we can prove that this message will surely be derivered to every people that is connected by all discounted calls, and that the message will never be derivered two or more times to same person. By the way, you are given information about application of family plan of Mr. Dango's family. Please write a program that calculates that in how many ways a different clan can be constructed. You should output the answer modulo 10007 because it may be very big. Constraints * 3 ≤ n ≤ 100,000 Input The input consists of several datasets. The first line of each dataset contains an integer n, which indicates the number of members in the family. Next n lines represents information of the i-th member with four integers. The first two integers respectively represent b[0] (the partner of i) and f(i, b[0]) (the calling fee per unit time between i and b[0]). The following two integers represent b[1] and f(i, b[1]) in the same manner. Input terminates with a dataset where n = 0. Output For each dataset, output the number of clan modulo 10007. Example Input 3 1 1 2 3 0 1 2 2 1 2 0 3 7 1 2 2 1 0 2 3 2 0 1 3 1 2 1 1 2 5 3 6 2 4 3 6 1 4 2 5 1 0 Output 1 2
{"inputs": ["3\n1 1 2 3\n0 1 2 2\n1 2 0 3\n7\n1 2 2 1\n0 2 3 2\n0 1 3 1\n2 1 1 2\n5 3 4 2\n4 3 6 1\n4 2 5 1\n0", "3\n1 2 2 3\n0 1 2 2\n1 2 0 3\n7\n1 2 0 1\n0 2 3 2\n0 0 3 2\n2 2 1 2\n5 3 4 2\n4 3 6 1\n4 0 5 1\n0", "3\n1 1 2 3\n0 0 2 2\n1 2 0 3\n0\n1 2 2 1\n0 2 3 2\n0 1 3 0\n2 1 1 2\n5 3 4 2\n4 3 6 1\n4 2 5 1\n0", "3\n1 1 2 3\n0 1 2 2\n1 2 0 3\n7\n1 2 2 1\n0 2 3 2\n0 1 3 1\n2 1 1 2\n5 3 4 2\n4 3 6 2\n4 2 5 1\n0", "3\n1 1 2 3\n0 1 2 2\n1 2 0 3\n7\n1 2 2 1\n0 2 3 2\n0 1 3 0\n2 1 1 2\n5 3 4 2\n4 3 6 2\n4 2 5 1\n0", "3\n1 2 2 3\n0 1 2 2\n1 2 0 3\n7\n1 2 2 1\n0 2 3 2\n0 1 3 1\n2 1 1 2\n5 3 4 2\n4 3 6 1\n4 2 5 1\n0", "3\n1 1 2 3\n0 1 2 2\n1 2 0 3\n7\n1 2 2 1\n0 2 3 2\n0 1 3 1\n2 1 1 2\n5 3 4 2\n4 3 6 0\n4 2 5 1\n0", "3\n1 1 2 3\n0 1 2 0\n1 2 0 3\n7\n1 2 2 1\n0 2 3 2\n0 1 3 1\n2 1 1 2\n5 3 6 2\n4 3 6 1\n4 2 5 1\n0"], "outputs": ["1\n2\n", "1\n3\n", "1\n", "1\n2\n", "1\n2\n", "1\n2\n", "1\n2\n", "1\n2\n"]}
763
748
coding
Solve the programming task below in a Python markdown code block. You are given a set of $n$ segments on the axis $Ox$, each segment has integer endpoints between $1$ and $m$ inclusive. Segments may intersect, overlap or even coincide with each other. Each segment is characterized by two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le m$) — coordinates of the left and of the right endpoints. Consider all integer points between $1$ and $m$ inclusive. Your task is to print all such points that don't belong to any segment. The point $x$ belongs to the segment $[l; r]$ if and only if $l \le x \le r$. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n, m \le 100$) — the number of segments and the upper bound for coordinates. The next $n$ lines contain two integers each $l_i$ and $r_i$ ($1 \le l_i \le r_i \le m$) — the endpoints of the $i$-th segment. Segments may intersect, overlap or even coincide with each other. Note, it is possible that $l_i=r_i$, i.e. a segment can degenerate to a point. -----Output----- In the first line print one integer $k$ — the number of points that don't belong to any segment. In the second line print exactly $k$ integers in any order — the points that don't belong to any segment. All points you print should be distinct. If there are no such points at all, print a single integer $0$ in the first line and either leave the second line empty or do not print it at all. -----Examples----- Input 3 5 2 2 1 2 5 5 Output 2 3 4 Input 1 7 1 7 Output 0 -----Note----- In the first example the point $1$ belongs to the second segment, the point $2$ belongs to the first and the second segments and the point $5$ belongs to the third segment. The points $3$ and $4$ do not belong to any segment. In the second example all the points from $1$ to $7$ belong to the first segment.
{"inputs": ["1 7\n1 7\n", "1 7\n5 5\n", "1 2\n1 1\n", "1 7\n5 5\n", "1 2\n1 1\n", "1 9\n5 5\n", "1 3\n1 1\n", "1 7\n1 7\n"], "outputs": ["0\n\n", "6\n1 2 3 4 6 7 \n", "1\n2 \n", "6\n1 2 3 4 6 7\n", "1\n2\n", "8\n1 2 3 4 6 7 8 9\n", "2\n2 3\n", "0\n\n"]}
505
170
coding
Solve the programming task below in a Python markdown code block. In this kata, the number 0 is infected. You are given a list. Every turn, any item in the list that is adjacent to a 0 becomes infected and transforms into a 0. How many turns will it take for the whole list to become infected? ``` [0,1,1,0] ==> [0,0,0,0] All infected in 1 turn. [1,1,0,1,1] --> [1,0,0,0,1] --> [0,0,0,0,0] All infected in 2 turns [0,1,1,1] --> [0,0,1,1] --> [0,0,0,1] --> [0,0,0,0] All infected in 3 turns. ``` All lists will contain at least one item, and at least one zero, and the only items will be 0s and 1s. Lists may be very very long, so pure brute force approach will not work. Also feel free to reuse/extend the following starter code: ```python def infected_zeroes(lst): ```
{"functional": "_inputs = [[[0]], [[0, 1, 1, 0]], [[0, 1, 1, 1, 0]], [[1, 1, 0, 1, 1]], [[0, 1, 1, 1]], [[1, 1, 1, 0]]]\n_outputs = [[0], [1], [2], [2], [3], [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(infected_zeroes(*i), o[0])"}
254
238
coding
Solve the programming task below in a Python markdown code block. Takahashi recorded his daily life for the last few days as a integer sequence of length 2N, as follows: * a_1, b_1, a_2, b_2, ... , a_N, b_N This means that, starting from a certain time T, he was: * sleeping for exactly a_1 seconds * then awake for exactly b_1 seconds * then sleeping for exactly a_2 seconds * : * then sleeping for exactly a_N seconds * then awake for exactly b_N seconds In this record, he waked up N times. Takahashi is wondering how many times he waked up early during the recorded period. Here, he is said to wake up early if he wakes up between 4:00 AM and 7:00 AM, inclusive. If he wakes up more than once during this period, each of these awakenings is counted as waking up early. Unfortunately, he forgot the time T. Find the maximum possible number of times he waked up early during the recorded period. For your information, a day consists of 86400 seconds, and the length of the period between 4:00 AM and 7:00 AM is 10800 seconds. Constraints * 1 \leq N \leq 10^5 * 1 \leq a_i, b_i \leq 10^5 * a_i and b_i are integers. Input The input is given from Standard Input in the following format: N a_1 b_1 a_2 b_2 : a_N b_N Output Print the maximum possible number of times he waked up early during the recorded period. Examples Input 3 28800 57600 28800 57600 57600 28800 Output 2 Input 10 28800 57600 4800 9600 6000 1200 600 600 300 600 5400 600 6000 5760 6760 2880 6000 12000 9000 600 Output 5
{"inputs": ["3\n11001 2265\n499 81568\n5242 218", "3\n10000 2054\n499 42643\n5242 146", "3\n10000 2054\n499 42643\n5242 218", "3\n10000 2054\n499 64506\n5242 218", "3\n11000 2054\n499 64506\n5242 218", "3\n11000 2265\n499 64506\n5242 218", "3\n11001 2265\n499 64506\n5242 218", "3\n11162 10322\n5814 7716\n84163 87"], "outputs": ["3\n", "2\n", "2\n", "2\n", "2\n", "2\n", "2\n", "2\n"]}
531
303
coding
Solve the programming task below in a Python markdown code block. Arkady invited Anna for a dinner to a sushi restaurant. The restaurant is a bit unusual: it offers $n$ pieces of sushi aligned in a row, and a customer has to choose a continuous subsegment of these sushi to buy. The pieces of sushi are of two types: either with tuna or with eel. Let's denote the type of the $i$-th from the left sushi as $t_i$, where $t_i = 1$ means it is with tuna, and $t_i = 2$ means it is with eel. Arkady does not like tuna, Anna does not like eel. Arkady wants to choose such a continuous subsegment of sushi that it has equal number of sushi of each type and each half of the subsegment has only sushi of one type. For example, subsegment $[2, 2, 2, 1, 1, 1]$ is valid, but subsegment $[1, 2, 1, 2, 1, 2]$ is not, because both halves contain both types of sushi. Find the length of the longest continuous subsegment of sushi Arkady can buy. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100\,000$) — the number of pieces of sushi. The second line contains $n$ integers $t_1$, $t_2$, ..., $t_n$ ($t_i = 1$, denoting a sushi with tuna or $t_i = 2$, denoting a sushi with eel), representing the types of sushi from left to right. It is guaranteed that there is at least one piece of sushi of each type. Note that it means that there is at least one valid continuous segment. -----Output----- Print a single integer — the maximum length of a valid continuous segment. -----Examples----- Input 7 2 2 2 1 1 2 2 Output 4 Input 6 1 2 1 2 1 2 Output 2 Input 9 2 2 1 1 1 2 2 2 2 Output 6 -----Note----- In the first example Arkady can choose the subsegment $[2, 2, 1, 1]$ or the subsegment $[1, 1, 2, 2]$ with length $4$. In the second example there is no way but to choose one of the subsegments $[2, 1]$ or $[1, 2]$ with length $2$. In the third example Arkady's best choice is the subsegment $[1, 1, 1, 2, 2, 2]$.
{"inputs": ["2\n1 2\n", "2\n2 1\n", "2\n1 2\n", "2\n2 1\n", "3\n1 1 2\n", "3\n2 1 1\n", "3\n1 1 2\n", "3\n2 1 1\n"], "outputs": ["2\n", "2\n", "2\n", "2\n", "2\n", "2\n", "2\n", "2\n"]}
596
110
coding
Solve the programming task below in a Python markdown code block. A new task for you! You have to create a method, that corrects a given time string. There was a problem in addition, so many of the time strings are broken. Time-Format is european. So from "00:00:00" to "23:59:59". Some examples: "09:10:01" -> "09:10:01" "11:70:10" -> "12:10:10" "19:99:99" -> "20:40:39" "24:01:01" -> "00:01:01" If the input-string is null or empty return exactly this value! (empty string for C++) If the time-string-format is invalid, return null. (empty string for C++) Have fun coding it and please don't forget to vote and rank this kata! :-) I have created other katas. Have a look if you like coding and challenges. Also feel free to reuse/extend the following starter code: ```python def time_correct(t): ```
{"functional": "_inputs = [[None], [''], ['001122'], ['00;11;22'], ['0a:1c:22'], ['09:10:01'], ['11:70:10'], ['19:99:99'], ['24:01:01'], ['52:01:01'], ['14:59:94']]\n_outputs = [[None], [''], [None], [None], [None], ['09:10:01'], ['12:10:10'], ['20:40:39'], ['00:01:01'], ['04:01:01'], ['15:00:34']]\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(time_correct(*i), o[0])"}
260
315
coding
Solve the programming task below in a Python markdown code block. The girl Taylor has a beautiful calendar for the year y. In the calendar all days are given with their days of week: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday. The calendar is so beautiful that she wants to know what is the next year after y when the calendar will be exactly the same. Help Taylor to find that year. Note that leap years has 366 days. The year is leap if it is divisible by 400 or it is divisible by 4, but not by 100 (https://en.wikipedia.org/wiki/Leap_year). -----Input----- The only line contains integer y (1000 ≤ y < 100'000) — the year of the calendar. -----Output----- Print the only integer y' — the next year after y when the calendar will be the same. Note that you should find the first year after y with the same calendar. -----Examples----- Input 2016 Output 2044 Input 2000 Output 2028 Input 50501 Output 50507 -----Note----- Today is Monday, the 13th of June, 2016.
{"inputs": ["2016\n", "2000\n", "1000\n", "1900\n", "1899\n", "1004\n", "2100\n", "1313\n"], "outputs": ["2044\n", "2028\n", "1006\n", "1906\n", "1905\n", "1032\n", "2106\n", "1319\n"]}
274
118
coding
Solve the programming task below in a Python markdown code block. For Diwali, Chef arranges all $K$ laddus in a row in his sweet shop. Whenever a customer comes to buy laddus, chef follows a rule that each customer must buy all laddus on odd position. After the selection of the laddu, a new row is formed, and again out of these only laddus on odd position are selected. This continues until the chef left with the last laddu. Find out the position of that last laddu in the original row. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, one integer $K$. -----Output:----- For each testcase, print the position of that laddu who is left, in the original row. -----Constraints----- - $1 \leq T \leq 10^5$ - $1 \leq K \leq 10^5$ -----Sample Input:----- 3 1 5 8 -----Sample Output:----- 1 4 8 -----EXPLANATION:----- For 1) Only one laddu which is last so print 1. For 2) Customer 1: [1, 3, 5] New row = [2, 4] Customer 2: [2] Last laddu = 4
{"inputs": ["3\n1\n5\n8"], "outputs": ["1\n4\n8"]}
309
22
coding
Solve the programming task below in a Python markdown code block. Consider the following arithmetic progression with n terms: * x, x + d, x + 2d, \ldots, x + (n-1)d What is the product of all terms in this sequence? Compute the answer modulo 1\ 000\ 003. You are given Q queries of this form. In the i-th query, compute the answer in case x = x_i, d = d_i, n = n_i. Constraints * 1 \leq Q \leq 10^5 * 0 \leq x_i, d_i \leq 1\ 000\ 002 * 1 \leq n_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: Q x_1 d_1 n_1 : x_Q d_Q n_Q Output Print Q lines. In the i-th line, print the answer for the i-th query. Example Input 2 7 2 4 12345 67890 2019 Output 9009 916936
{"inputs": ["2\n7 3 4\n12345 30036 63", "2\n7 3 4\n12345 57737 63", "2\n7 1 1\n9796 9219 1485", "2\n7 2 4\n12345 57737 63", "2\n7 1 1\n9796 9219 2637", "2\n3 2 4\n12345 57737 63", "2\n3 1 4\n12345 57737 63", "2\n3 1 0\n12345 57737 63"], "outputs": ["14560\n487009\n", "14560\n462686\n", "7\n37900\n", "9009\n462686\n", "7\n487631\n", "945\n462686\n", "360\n462686\n", "1\n462686\n"]}
276
300
coding
Solve the programming task below in a Python markdown code block. ## The Riddle The King of a small country invites 1000 senators to his annual party. As a tradition, each senator brings the King a bottle of wine. Soon after, the Queen discovers that one of the senators is trying to assassinate the King by giving him a bottle of poisoned wine. Unfortunately, they do not know which senator, nor which bottle of wine is poisoned, and the poison is completely indiscernible. However, the King has 10 lab rats. He decides to use them as taste testers to determine which bottle of wine contains the poison. The poison when taken has no effect on the rats, until exactly 24 hours later when the infected rats suddenly die. The King needs to determine which bottle of wine is poisoned by tomorrow, so that the festivities can continue as planned. Hence he only has time for one round of testing, he decides that each rat tastes multiple bottles, according to a certain scheme. ## Your Task You receive an array of integers (`0 to 9`), each of them is the number of a rat which died after tasting the wine bottles. Return the number of the bottle (`1..1000`) which is poisoned. **Good Luck!** *Hint: think of rats as a certain representation of the number of the bottle...* Also feel free to reuse/extend the following starter code: ```python def find(r): ```
{"functional": "_inputs = [[[0]], [[1]], [[2]], [[3]], [[4]], [[5]], [[6]], [[7]], [[8]], [[9]], [[3, 5, 6, 7, 8, 9]], [[0, 3, 5, 4, 9, 8]], [[0, 1, 9, 3, 5]], [[0, 1, 2, 3, 4, 6]], [[0, 1, 3, 4]]]\n_outputs = [[1], [2], [4], [8], [16], [32], [64], [128], [256], [512], [1000], [825], [555], [95], [27]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(find(*i), o[0])"}
304
321
coding
Solve the programming task below in a Python markdown code block. Your friend recently gave you some slimes for your birthday. You have n slimes all initially with value 1. You are going to play a game with these slimes. Initially, you put a single slime by itself in a row. Then, you will add the other n - 1 slimes one by one. When you add a slime, you place it at the right of all already placed slimes. Then, while the last two slimes in the row have the same value v, you combine them together to create a slime with value v + 1. You would like to see what the final state of the row is after you've added all n slimes. Please print the values of the slimes in the row from left to right. -----Input----- The first line of the input will contain a single integer, n (1 ≤ n ≤ 100 000). -----Output----- Output a single line with k integers, where k is the number of slimes in the row after you've finished the procedure described in the problem statement. The i-th of these numbers should be the value of the i-th slime from the left. -----Examples----- Input 1 Output 1 Input 2 Output 2 Input 3 Output 2 1 Input 8 Output 4 -----Note----- In the first sample, we only have a single slime with value 1. The final state of the board is just a single slime with value 1. In the second sample, we perform the following steps: Initially we place a single slime in a row by itself. Thus, row is initially 1. Then, we will add another slime. The row is now 1 1. Since two rightmost slimes have the same values, we should replace these slimes with one with value 2. Thus, the final state of the board is 2. In the third sample, after adding the first two slimes, our row is 2. After adding one more slime, the row becomes 2 1. In the last sample, the steps look as follows: 1 2 2 1 3 3 1 3 2 3 2 1 4
{"inputs": ["1\n", "2\n", "3\n", "8\n", "4\n", "5\n", "6\n", "8\n"], "outputs": ["1\n", "2\n", "2 1\n", "4\n", "3\n", "3 1\n", "3 2\n", "4 \n"]}
485
77
coding
Solve the programming task below in a Python markdown code block. In "Takahashi-ya", a ramen restaurant, a bowl of ramen costs 700 yen (the currency of Japan), plus 100 yen for each kind of topping (boiled egg, sliced pork, green onions). A customer ordered a bowl of ramen and told which toppings to put on his ramen to a clerk. The clerk took a memo of the order as a string S. S is three characters long, and if the first character in S is o, it means the ramen should be topped with boiled egg; if that character is x, it means the ramen should not be topped with boiled egg. Similarly, the second and third characters in S mean the presence or absence of sliced pork and green onions on top of the ramen. Write a program that, when S is given, prints the price of the corresponding bowl of ramen. -----Constraints----- - S is a string of length 3. - Each character in S is o or x. -----Input----- Input is given from Standard Input in the following format: S -----Output----- Print the price of the bowl of ramen corresponding to S. -----Sample Input----- oxo -----Sample Output----- 900 The price of a ramen topped with two kinds of toppings, boiled egg and green onions, is 700 + 100 \times 2 = 900 yen.
{"inputs": ["pxo", "yxx", "opo", "oxp", "xyx", "opn", "xpo", "xzx"], "outputs": ["800\n", "700\n", "900\n", "800\n", "700\n", "800\n", "800\n", "700\n"]}
305
85
coding
Solve the programming task below in a Python markdown code block. You are given N integers A_{1}, A_{2}, \ldots, A_{N}. You can perform the following operation any number of times: Select any index i such that 1 ≤ i ≤ (N-1); Subtract 1 from A_{i} and 2 from A_{i+1}. Find the smallest possible value of |A_{1}| + |A_{2}| + \ldots + |A_{N}| you can achieve after performing any number of given 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 multiple lines of input. - The first line of each test case contains a single integer N — the number of integers. - The second line of each test case contains N integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case, output a single integer — the smallest possible value of |A_{1}| + |A_{2}| + \ldots + |A_{N}| you can achieve after performing any number of given operations. ------ Constraints ------ $1 ≤ T ≤ 100000$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $-10^{9} ≤A_{i} ≤10^{9}$ - The sum of $N$ over all test cases won't exceed $2 \cdot 10^{5}$. ----- Sample Input 1 ------ 4 2 2 4 3 1 1 1 6 -4 2 -4 2 -4 2 1 -100000000 ----- Sample Output 1 ------ 0 2 15 100000000 ----- explanation 1 ------ Test case $1$: We can apply the operation for $i = 1$. Thus, the array becomes $A = [1, 2]$. On applying the operation again for $i=1$, the array becomes $A = [0, 0]$. Thus, the value of $|A_{1}| + |A_{2}| = 0$. It can be shown that this is the smallest possible value of $|A_{1}| + |A_{2}|$. Test case $2$: Apply the operation for $i = 1$. The array becomes $A = [0, -1, 1]$. Thus, the value $|A_{1}+|A_{2}|+|A_{3}| = 0+1+1 = 2$. Test case $3$: We apply three operations on indices $1, 3,$ and $5$ respectively. Thus, the final array becomes $A = [-5, 0, -5, 0, -5, 0]$. The value $|A_{1}| + |A_{2}| + \ldots + |A_{6}| = 15$. Test case $4$: We cannot apply any operation on the array as we cannot choose any valid index.
{"inputs": ["4\n2\n2 4\n3\n1 1 1\n6\n-4 2 -4 2 -4 2\n1\n-100000000\n"], "outputs": ["0\n2\n15\n100000000\n"]}
684
71
coding
Solve the programming task below in a Python markdown code block. International Carpenters Professionals Company (ICPC) is a top construction company with a lot of expert carpenters. What makes ICPC a top company is their original language. The syntax of the language is simply given in CFG as follows: S -> SS | (S) | )S( | ε In other words, a right parenthesis can be closed by a left parenthesis and a left parenthesis can be closed by a right parenthesis in this language. Alex, a grad student mastering linguistics, decided to study ICPC's language. As a first step of the study, he needs to judge whether a text is well-formed in the language or not. Then, he asked you, a great programmer, to write a program for the judgement. Alex's request is as follows: You have an empty string S in the beginning, and construct longer string by inserting a sequence of '(' or ')' into the string. You will receive q queries, each of which consists of three elements (p, c, n), where p is the position to insert, n is the number of characters to insert and c is either '(' or ')', the character to insert. For each query, your program should insert c repeated by n times into the p-th position of S from the beginning. Also it should output, after performing each insert operation, "Yes" if S is in the language and "No" if S is not in the language. Please help Alex to support his study, otherwise he will fail to graduate the college. Input The first line contains one integer q (1 \leq q \leq 10^5) indicating the number of queries, follows q lines of three elements, p_i, c_i, n_i, separated by a single space (1 \leq i \leq q, c_i = '(' or ')', 0 \leq p_i \leq length of S before i-th query, 1 \leq n \leq 2^{20}). It is guaranteed that all the queries in the input are valid. Output For each query, output "Yes" if S is in the language and "No" if S is not in the language. Examples Input 3 0 ( 10 10 ) 5 10 ) 5 Output No No Yes Input 3 0 ) 10 10 ( 5 10 ( 5 Output No No Yes Input 3 0 ( 10 10 ) 20 0 ( 10 Output No No Yes
{"inputs": ["3\n0 ) 0\n7 * 0\n1 ( 0", "3\n0 ( 8\n10 ) 2\n0 ( 3", "3\n0 ( 8\n10 ) 4\n0 ( 3", "3\n0 ( 8\n10 ) 4\n0 ' 3", "3\n0 ( 3\n10 ) 4\n0 ' 3", "3\n-1 ) 0\n8 ( 19\n4 ' 2", "3\n-2 ) 0\n7 ) 0\n4 ( 11", "3\n0 ( 8\n10 ) 5\n10 ( 5"], "outputs": ["Yes\nYes\nYes\n", "No\nNo\nNo\n", "No\nNo\nNo\n", "No\nNo\nNo\n", "No\nNo\nNo\n", "Yes\nNo\nNo\n", "Yes\nYes\nNo\n", "No\nNo\nNo\n"]}
555
224
coding
Solve the programming task below in a Python markdown code block. The time is 2020. There is data that saves the qualifying results of PC Koshien 2020. This data stores the reference number and the number of correct answers assigned to each team. Here, the ranking is determined by the number of correct answers, and the ranking is given in descending order of the number of correct answers, such as 1st, 2nd, and so on. Enter the qualifying result data and reference number from the keyboard, and create a program that outputs the ranking of the team with that number. Note In the data of the input example, if the teams are arranged in order of the number of correct answers: 3,30 1,20 2,20 6,20 4,10 5,10 It will be. Here, in order to determine the ranking based on the number of correct answers, the 30-question correct answer team is ranked 1st, the 20-question correct answer team is ranked 2nd, and the 10-question correct answer team is ranked 3rd. Please note that this is different from the usual ranking with the correct team in 5th place). Input The input data consists of two parts. The first half is the qualifying result data, and the second half is the inquiry of the team number for which you want to know the ranking. The format of the qualifying result data is as follows. p1, s1 p2, s2 ... ... 0,0 pi (1 ≤ pi ≤ 100) and si (0 ≤ si ≤ 30) are integers representing the reference number and the number of correct answers for the i-th team, respectively. It is assumed that the input of this data is completed when both the reference number and the number of correct answers are 0. Then multiple inquiries in the second half are given. The inquiry format is as follows. q1 q2 :: Each query is given the reference number qi (1 ≤ qi ≤ 30) on one line. Please process this until the end of the input. The number of inquiries does not exceed 100. Output For each inquiry, print the team ranking on one line. Example Input 1,20 2,20 3,30 4,10 5,10 6,20 0,0 1 2 4 5 Output 2 2 3 3
{"inputs": ["1,20\n2,20\n3,30\n4,10\n5,10\n6,20\n0,0\n1\n2\n4\n1", "1,20\n2,20\n3,30\n4,10\n5,10\n0,26\n0,0\n1\n2\n4\n1", "1,20\n2,20\n3,30\n4,10\n5,10\n0,26\n0,0\n0\n2\n4\n1", "1,20\n2,20\n3,30\n4,10\n5,10\n0,26\n0,0\n1\n2\n4\n0", "1,20\n2,20\n3,30\n4,10\n5,10\n0,26\n0,0\n1\n3\n4\n0", "1,20\n2,20\n3,30\n4,10\n5,10\n0,26\n0,0\n1\n3\n3\n0", "1,20\n2,20\n3,30\n4,10\n6,11\n0,26\n0,0\n0\n2\n4\n2", "1,20\n2,20\n3,30\n4,10\n5,10\n6,10\n0,0\n1\n2\n4\n5"], "outputs": ["2\n2\n3\n2\n", "3\n3\n4\n3\n", "2\n3\n4\n3\n", "3\n3\n4\n2\n", "3\n1\n4\n2\n", "3\n1\n1\n2\n", "2\n3\n5\n3\n", "2\n2\n3\n3\n"]}
537
430
coding
Solve the programming task below in a Python markdown code block. It's the academic year's end, fateful moment of your school report. The averages must be calculated. All the students come to you and entreat you to calculate their average for them. Easy ! You just need to write a script. Return the average of the given array rounded **down** to its nearest integer. The array will never be empty. Also feel free to reuse/extend the following starter code: ```python def get_average(marks): ```
{"functional": "_inputs = [[[2, 2, 2, 2]], [[1, 5, 87, 45, 8, 8]], [[2, 5, 13, 20, 16, 16, 10]], [[1, 2, 15, 15, 17, 11, 12, 17, 17, 14, 13, 15, 6, 11, 8, 7]]]\n_outputs = [[2], [25], [11], [11]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(get_average(*i), o[0])"}
107
280
coding
Solve the programming task below in a Python markdown code block. You are given an array A and an integer X. You are allowed to perform the following operation on the array: Select two distinct indices i and j and set both A_{i} and A_{j} as ((A_{i} \oplus A_{j}) \mid X) simultaneously. Here \oplus and \mid denote the [bitwise XOR] and [bitwise OR] operations respectively. Find the minimum number of operations required to make all elements of the array odd. If it is not possible to do so, print -1 instead. ------ 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 two space-separated integers N, the size of the array and X. - The next line contains N space-separated integers denoting the elements of the array A. ------ Output Format ------ For each test case, output on a new line, the minimum number of operations required to make all elements of the array odd. If it is not possible to do so, print -1 instead. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $2 ≤ N ≤ 10^{5}$ $0 ≤ X < 2^{30}$ $0 ≤ A_{i} < 2^{30}$ - The sum of $N$ over all test cases won't exceed $10^{5}$. ----- Sample Input 1 ------ 2 3 6 5 7 9 5 4 2 3 4 17 9 ----- Sample Output 1 ------ 0 2 ----- explanation 1 ------ Test case $1$: All elements are already odd. Thus, the number of operation required will be $0$. Test case $2$: We can make all elements odd using $2$ operations - - In first operation, we choose $i=1, j=5$. Here, $(A_{i}\oplus A_{j})\mid X=(2 \oplus 9)\mid 4 = 11 \mid 4 = 15$. Thus, the new array becomes $[15,3,4,17,15]$. - In second operation, we choose $i=4, j=3$. Here, $(A_{i}\oplus A_{j})\mid X=(17 \oplus 4)\mid 4 = 21 \mid 4 = 21$. Thus, the new array becomes $[15,3,21, 21,15]$. All elements of the array are odd now. It can be shown that we cannot achieve this in less than $2$ operations.
{"inputs": ["2\n3 6\n5 7 9\n5 4\n2 3 4 17 9\n"], "outputs": ["0\n2"]}
605
40
coding
Solve the programming task below in a Python markdown code block. We are given a sequence of coplanar points and see all the possible triangles that may be generated which all combinations of three points. We have the following list of points with the cartesian coordinates of each one: ``` Points [x, y] A [1, 2] B [3, 3] C [4, 1] D [1, 1] E [4, -1] ``` With these points we may have the following triangles: ```ABC, ABD, ABE, ACD, ACE, ADE, BCD, BCE, BDE, CDE.``` There are three special ones: ```ABC, ACD and CDE```, that have an angle of 90°. All is shown in the picture below: We need to count all the rectangle triangles that may be formed by a given list of points. The case decribed above will be: ```python count_rect_triang([[1, 2],[3, 3],[4, 1],[1, 1],[4, -1]]) == 3 ``` Observe this case: ```python count_rect_triang([[1, 2],[4, -1],[3, 3],[4, -1],[4, 1],[1, 1],[4, -1], [4, -1], [3, 3], [1, 2]]) == 3 ``` If no rectangle triangles may be generated the function will output ```0```. Enjoy it! Also feel free to reuse/extend the following starter code: ```python def count_rect_triang(points): ```
{"functional": "_inputs = [[[[1, 2], [3, 3], [4, 1], [1, 1], [4, -1]]], [[[1, 2], [4, -1], [3, 3], [4, -1], [4, 1], [1, 1], [4, -1], [4, -1], [3, 3], [1, 2]]]]\n_outputs = [[3], [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(count_rect_triang(*i), o[0])"}
362
249
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese and Vietnamese as well. It is well-known that ∑sqrt(a_{i}), a_{i} ∈ N is a root of some integer-coefficient polynomial. For example: sqrt(2) is a root of polynomial: x^{2} − 2. Now, your task is to find not only such polynomial but also the minimal one. When comparing two polynomials, firstly, we consider their degree and then the coefficient of the highest term, and then the second highest term and so on. (Note that we consider only polynomial with the coefficient of highest term is positive) ------ Input ------ First line contains an integer T denotes the number of test cases. Each test case is described by an integer n and followed by n space-separated integers a_{i}. ------ Output: ------ Each test case print an integer k denotes the degree of polynomial in a single line. Then next line print k + 1 space-separated integers modulo 10^{9} + 7, coefficients from lowest to highest term. ------ Constraints ------ 1 ≤ T ≤ 5, 1 ≤ n ≤ 15 a_{i} are n distinct primes, 1 ≤ a_{i} ≤ 10^{9} ------ Subtasks ------ $Subtask #1: (10 points): n ≤ 3$ $Subtask #2: (20 points): n ≤ 5$ $Subtask #3: (30 points): n ≤ 10$ $Subtask #4: (40 points): n ≤ 15$ ------ Example ------ Input: 2 1 2 2 2 3 Output: 2 1000000005 0 1 4 1 0 999999997 0 1 ------ Explanation ------ The first polynomial is x^{2} − 2, and the second one is x^{4} − 10x^{2} + 1.
{"inputs": ["2\n1\n2\n2\n2 3"], "outputs": ["2\n1000000005 0 1\n4\n1 0 999999997 0 1"]}
440
57
coding
Solve the programming task below in a Python markdown code block. For this problem you must create a program that says who ate the last cookie. If the input is a string then "Zach" ate the cookie. If the input is a float or an int then "Monica" ate the cookie. If the input is anything else "the dog" ate the cookie. The way to return the statement is: "Who ate the last cookie? It was (name)!" Ex: Input = "hi" --> Output = "Who ate the last cookie? It was Zach! (The reason you return Zach is because the input is a string) Note: Make sure you return the correct message with correct spaces and punctuation. Please leave feedback for this kata. Cheers! Also feel free to reuse/extend the following starter code: ```python def cookie(x): ```
{"functional": "_inputs = [['Ryan'], [2.3], [26], [True], ['True'], [False], [1.98528462]]\n_outputs = [['Who ate the last cookie? It was Zach!'], ['Who ate the last cookie? It was Monica!'], ['Who ate the last cookie? It was Monica!'], ['Who ate the last cookie? It was the dog!'], ['Who ate the last cookie? It was Zach!'], ['Who ate the last cookie? It was the dog!'], ['Who ate the last cookie? It was Monica!']]\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(cookie(*i), o[0])"}
175
266
coding
Solve the programming task below in a Python markdown code block. Not so long ago, Vlad came up with an interesting function: $f_a(x)=\left\lfloor\frac{x}{a}\right\rfloor + x mod a$, where $\left\lfloor\frac{x}{a}\right\rfloor$ is $\frac{x}{a}$, rounded down, $x mod a$ — the remainder of the integer division of $x$ by $a$. For example, with $a=3$ and $x=11$, the value $f_3(11) = \left\lfloor\frac{11}{3}\right\rfloor + 11 mod 3 = 3 + 2 = 5$. The number $a$ is fixed and known to Vlad. Help Vlad find the maximum value of $f_a(x)$ if $x$ can take any integer value from $l$ to $r$ inclusive ($l \le x \le r$). -----Input----- The first line of input data contains an integer $t$ ($1 \le t \le 10^4$) — the number of input test cases. This is followed by $t$ lines, each of which contains three integers $l_i$, $r_i$ and $a_i$ ($1 \le l_i \le r_i \le 10^9, 1 \le a_i \le 10^9$) — the left and right boundaries of the segment and the fixed value of $a$. -----Output----- For each test case, output one number on a separate line — the maximum value of the function on a given segment for a given $a$. -----Examples----- Input 5 1 4 3 5 8 4 6 10 6 1 1000000000 1000000000 10 12 8 Output 2 4 5 999999999 5 -----Note----- In the first sample: $f_3(1) = \left\lfloor\frac{1}{3}\right\rfloor + 1 mod 3 = 0 + 1 = 1$, $f_3(2) = \left\lfloor\frac{2}{3}\right\rfloor + 2 mod 3 = 0 + 2 = 2$, $f_3(3) = \left\lfloor\frac{3}{3}\right\rfloor + 3 mod 3 = 1 + 0 = 1$, $f_3(4) = \left\lfloor\frac{4}{3}\right\rfloor + 4 mod 3 = 1 + 1 = 2$ As an answer, obviously, $f_3(2)$ and $f_3(4)$ are suitable.
{"inputs": ["1\n1 100 55\n", "1\n6 999998 9\n", "1\n87 95 696969\n", "3\n1 100 54\n1 100 55\n1 100 56\n", "5\n1 4 3\n5 8 4\n6 10 6\n1 1000000000 1000000000\n10 12 8\n"], "outputs": ["54\n", "111118\n", "95\n", "53\n54\n55\n", "2\n4\n5\n999999999\n5\n"]}
631
187
coding
Solve the programming task below in a Python markdown code block. Given is a positive integer N. We will choose an integer K between 2 and N (inclusive), then we will repeat the operation below until N becomes less than K. - Operation: if K divides N, replace N with N/K; otherwise, replace N with N-K. In how many choices of K will N become 1 in the end? -----Constraints----- - 2 \leq N \leq 10^{12} - N is an integer. -----Input----- Input is given from Standard Input in the following format: N -----Output----- Print the number of choices of K in which N becomes 1 in the end. -----Sample Input----- 6 -----Sample Output----- 3 There are three choices of K in which N becomes 1 in the end: 2, 5, and 6. In each of these choices, N will change as follows: - When K=2: 6 \to 3 \to 1 - When K=5: 6 \to 1 - When K=6: 6 \to 1
{"inputs": ["9", "5", "2", "3", "4", "6", "6\n", "2\n"], "outputs": ["5\n", "3\n", "1\n", "2\n", "3\n", "3", "3\n", "1\n"]}
239
63
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a string s, find the longest palindromic subsequence's length in s. A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.   Please complete the following python code precisely: ```python class Solution: def longestPalindromeSubseq(self, s: str) -> int: ```
{"functional": "def check(candidate):\n assert candidate(s = \"bbbab\") == 4\n assert candidate(s = \"cbbd\") == 2\n\n\ncheck(Solution().longestPalindromeSubseq)"}
96
49
coding
Solve the programming task below in a Python markdown code block. You're playing a game with a friend involving a bag of marbles. In the bag are ten marbles: * 1 smooth red marble * 4 bumpy red marbles * 2 bumpy yellow marbles * 1 smooth yellow marble * 1 bumpy green marble * 1 smooth green marble You can see that the probability of picking a smooth red marble from the bag is `1 / 10` or `0.10` and the probability of picking a bumpy yellow marble is `2 / 10` or `0.20`. The game works like this: your friend puts her hand in the bag, chooses a marble (without looking at it) and tells you whether it's bumpy or smooth. Then you have to guess which color it is before she pulls it out and reveals whether you're correct or not. You know that the information about whether the marble is bumpy or smooth changes the probability of what color it is, and you want some help with your guesses. Write a function `color_probability()` that takes two arguments: a color (`'red'`, `'yellow'`, or `'green'`) and a texture (`'bumpy'` or `'smooth'`) and returns the probability as a decimal fraction accurate to two places. The probability should be a string and should discard any digits after the 100ths place. For example, `2 / 3` or `0.6666666666666666` would become the string `'0.66'`. Note this is different from rounding. As a complete example, `color_probability('red', 'bumpy')` should return the string `'0.57'`. Also feel free to reuse/extend the following starter code: ```python def color_probability(color, texture): ```
{"functional": "_inputs = [['red', 'bumpy'], ['green', 'bumpy'], ['yellow', 'smooth'], ['red', 'smooth'], ['green', 'smooth'], ['yellow', 'bumpy']]\n_outputs = [['0.57'], ['0.14'], ['0.33'], ['0.33'], ['0.33'], ['0.28']]\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(color_probability(*i), o[0])"}
401
223
coding
Solve the programming task below in a Python markdown code block. You have a collection of lovely poems. Unfortuantely they aren't formatted very well. They're all on one line, like this: ``` Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. ``` What you want is to present each sentence on a new line, so that it looks like this: ``` Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. ``` Write a function, `format_poem()` that takes a string like the one line example as an argument and returns a new string that is formatted across multiple lines with each new sentence starting on a new line when you print it out. Try to solve this challenge with the [str.split()](https://docs.python.org/3/library/stdtypes.html#str.split) and the [str.join()](https://docs.python.org/3/library/stdtypes.html#str.join) string methods. Every sentence will end with a period, and every new sentence will have one space before the previous period. Be careful about trailing whitespace in your solution. Also feel free to reuse/extend the following starter code: ```python def format_poem(poem): ```
{"functional": "_inputs = [['Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated.'], [\"Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules.\"], ['Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess.'], [\"There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now.\"], [\"If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!\"]]\n_outputs = [['Beautiful is better than ugly.\\nExplicit is better than implicit.\\nSimple is better than complex.\\nComplex is better than complicated.'], [\"Flat is better than nested.\\nSparse is better than dense.\\nReadability counts.\\nSpecial cases aren't special enough to break the rules.\"], ['Although practicality beats purity.\\nErrors should never pass silently.\\nUnless explicitly silenced.\\nIn the face of ambiguity, refuse the temptation to guess.'], [\"There should be one-- and preferably only one --obvious way to do it.\\nAlthough that way may not be obvious at first unless you're Dutch.\\nNow is better than never.\\nAlthough never is often better than *right* now.\"], [\"If the implementation is hard to explain, it's a bad idea.\\nIf the implementation is easy to explain, it may be a good idea.\\nNamespaces are one honking great idea -- let's do more of those!\"]]\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(format_poem(*i), o[0])"}
270
547
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given the head of a linked list. Delete the middle node, and return the head of the modified linked list. The middle node of a linked list of size n is the ⌊n / 2⌋th node from the start using 0-based indexing, where ⌊x⌋ denotes the largest integer less than or equal to x. For n = 1, 2, 3, 4, and 5, the middle nodes are 0, 1, 1, 2, and 2, respectively.   Please complete the following python code precisely: ```python # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]: ```
{"functional": "def check(candidate):\n assert is_same_list(candidate(head = list_node([1,3,4,7,1,2,6])), list_node([1,3,4,1,2,6]))\n assert is_same_list(candidate(head = list_node([1,2,3,4])), list_node([1,2,4]))\n assert is_same_list(candidate(head = list_node([2,1])), list_node([2]))\n\n\ncheck(Solution().deleteMiddle)"}
205
113
coding
Solve the programming task below in a Python markdown code block. Consider the following pseudocode, run on an array $A=[a_0,a_1,...,a_{n-1}]$ of length $n$: rep := 0 while A not empty: B := [] for x in A, y in A: if x != y: append absolute_value(x - y) to B A := B rep := rep + 1 Given the values of $n$ and array $\mbox{A}$, compute and print the final value of ${\mbox{rep}}$ after the pseudocode above terminates; if the loop will never terminate, print -1 instead. Input Format The first line contains a single integer, $n$, denoting the length of array $\mbox{A}$. The second line contains $n$ space-separated integers describing the respective values of $a_0,a_1,\ldots,a_{n-1}$. Constraints $1\leq n\leq10^5$ $1\leq a_i\leq5\times10^4\ \text{}\forall\ 1\leq i\leq n$ Output Format Print the final value of ${\mbox{rep}}$ after the pseudocode terminates; if the loop will never terminate, print -1 instead. Sample Input 0 3 1 3 4 Sample Output 0 4 Explanation 0 After the first loop, $\mbox{A}$ becomes $[2,3,2,1,3,1]$. After the second loop, the array only contains $\mbox{1}$'s and $2$'s. After the third loop, the array only contains $\mbox{1}$'s. After the fourth loop, the array is empty. Because the value of ${\mbox{rep}}$ is incremented after each loop, $rep=4$ at the time the loop terminates. Thus, we print 4 as our answer.
{"inputs": ["3\n1 3 4\n"], "outputs": ["4\n"]}
438
20
coding
Solve the programming task below in a Python markdown code block. Chef had a sequence of positive integers with length $N + K$. He managed to calculate the arithmetic average of all elements of this sequence (let's denote it by $V$), but then, his little brother deleted $K$ elements from it. All deleted elements had the same value. Chef still knows the remaining $N$ elements — a sequence $A_1, A_2, \ldots, A_N$. Help him with restoring the original sequence by finding the value of the deleted elements or deciding that there is some mistake and the described scenario is impossible. Note that the if it is possible for the deleted elements to have the same value, then it can be proven that it is unique. Also note that this value must be a positive integer. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains three space-separated integers $N$, $K$ and $V$. - The second line contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$. -----Output----- For each test case, print a single line containing one integer — the value of the deleted elements, or $-1$ if there is a mistake. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, K \le 100$ - $1 \le V \le 10^5$ - $1 \le A_i \le 10^5$ for each valid $i$ -----Subtasks----- Subtask #1 (100 points): original constraints -----Example Input----- 3 3 3 4 2 7 3 3 1 4 7 6 5 3 3 4 2 8 3 -----Example Output----- 4 -1 -1
{"inputs": ["3\n3 3 4\n2 7 3\n3 1 4\n7 6 5\n3 3 4\n2 8 3"], "outputs": ["4\n-1\n-1"]}
424
54
coding
Solve the programming task below in a Python markdown code block. Pay attention to the non-standard memory limit in this problem. In order to cut off efficient solutions from inefficient ones in this problem, the time limit is rather strict. Prefer to use compiled statically typed languages (e.g. C++). If you use Python, then submit solutions on PyPy. Try to write an efficient solution. The array $a=[a_1, a_2, \ldots, a_n]$ ($1 \le a_i \le n$) is given. Its element $a_i$ is called special if there exists a pair of indices $l$ and $r$ ($1 \le l < r \le n$) such that $a_i = a_l + a_{l+1} + \ldots + a_r$. In other words, an element is called special if it can be represented as the sum of two or more consecutive elements of an array (no matter if they are special or not). Print the number of special elements of the given array $a$. For example, if $n=9$ and $a=[3,1,4,1,5,9,2,6,5]$, then the answer is $5$: $a_3=4$ is a special element, since $a_3=4=a_1+a_2=3+1$; $a_5=5$ is a special element, since $a_5=5=a_2+a_3=1+4$; $a_6=9$ is a special element, since $a_6=9=a_1+a_2+a_3+a_4=3+1+4+1$; $a_8=6$ is a special element, since $a_8=6=a_2+a_3+a_4=1+4+1$; $a_9=5$ is a special element, since $a_9=5=a_2+a_3=1+4$. Please note that some of the elements of the array $a$ may be equal — if several elements are equal and special, then all of them should be counted in the answer. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$) — the number of test cases in the input. Then $t$ test cases follow. Each test case is given in two lines. The first line contains an integer $n$ ($1 \le n \le 8000$) — the length of the array $a$. The second line contains integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of the values of $n$ for all test cases in the input does not exceed $8000$. -----Output----- Print $t$ numbers — the number of special elements for each of the given arrays. -----Example----- Input 5 9 3 1 4 1 5 9 2 6 5 3 1 1 2 5 1 1 1 1 1 8 8 7 6 5 4 3 2 1 1 1 Output 5 1 0 4 0
{"inputs": ["7\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n", "7\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n", "7\n1\n1\n1\n1\n1\n0\n1\n1\n1\n1\n1\n1\n1\n1\n", "7\n1\n1\n1\n1\n1\n1\n1\n0\n1\n1\n1\n1\n1\n1\n", "7\n1\n1\n1\n1\n1\n1\n1\n0\n1\n1\n1\n1\n1\n0\n", "7\n1\n0\n1\n1\n1\n1\n1\n0\n1\n1\n1\n1\n1\n1\n", "5\n9\n3 1 4 1 5 9 2 6 5\n3\n1 1 2\n5\n1 1 1 1 1\n8\n8 7 6 5 4 3 2 1\n1\n1\n", "5\n9\n3 1 4 1 5 9 2 6 5\n3\n1 1 2\n5\n1 1 2 1 1\n8\n8 7 6 5 4 3 2 1\n1\n1\n"], "outputs": ["0\n0\n0\n0\n0\n0\n0\n", "0\n0\n0\n0\n0\n0\n0\n", "0\n0\n0\n0\n0\n0\n0\n", "0\n0\n0\n0\n0\n0\n0\n", "0\n0\n0\n0\n0\n0\n0\n", "0\n0\n0\n0\n0\n0\n0\n", "5\n1\n0\n4\n0\n", "5\n1\n1\n4\n0\n"]}
718
450
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. There are n people and 40 types of hats labeled from 1 to 40. Given a 2D integer array hats, where hats[i] is a list of all hats preferred by the ith person. Return the number of ways that the n people wear different hats to each other. Since the answer may be too large, return it modulo 109 + 7.   Please complete the following python code precisely: ```python class Solution: def numberWays(self, hats: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(hats = [[3,4],[4,5],[5]]) == 1\n assert candidate(hats = [[3,5,1],[3,5]]) == 4\n assert candidate(hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]) == 24\n assert candidate(hats = [[1,2,3],[2,3,5,6],[1,3,7,9],[1,8,9],[2,5,7]]) == 111\n\n\ncheck(Solution().numberWays)"}
130
153
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a 2D integer array intervals, where intervals[i] = [lefti, righti] describes the ith interval starting at lefti and ending at righti (inclusive). The size of an interval is defined as the number of integers it contains, or more formally righti - lefti + 1. You are also given an integer array queries. The answer to the jth query is the size of the smallest interval i such that lefti <= queries[j] <= righti. If no such interval exists, the answer is -1. Return an array containing the answers to the queries.   Please complete the following python code precisely: ```python class Solution: def minInterval(self, intervals: List[List[int]], queries: List[int]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(intervals = [[1,4],[2,4],[3,6],[4,4]], queries = [2,3,4,5]) == [3,3,1,4]\n assert candidate(intervals = [[2,3],[2,5],[1,8],[20,25]], queries = [2,19,5,22]) == [2,-1,4,6]\n\n\ncheck(Solution().minInterval)"}
179
113
coding
Solve the programming task below in a Python markdown code block. For a string S, let f(S) be the lexicographically smallest cyclic shift of S. For example, if S = `babca`, f(S) = `ababc` because this is the smallest among all cyclic shifts (`babca`, `abcab`, `bcaba`, `cabab`, `ababc`). You are given three integers X, Y, and Z. You want to construct a string T that consists of exactly X `a`s, exactly Y `b`s, and exactly Z `c`s. If there are multiple such strings, you want to choose one that maximizes f(T) lexicographically. Compute the lexicographically largest possible value of f(T). Constraints * 1 \leq X + Y + Z \leq 50 * X, Y, Z are non-negative integers. Input Input is given from Standard Input in the following format: X Y Z Output Print the answer. Examples Input 2 2 0 Output abab Input 1 1 1 Output acb
{"inputs": ["2 1 0", "1 1 0", "0 2 0", "1 0 1", "2 0 0", "1 0 0", "2 2 1", "2 0 1"], "outputs": ["aab\n", "ab\n", "bb\n", "ac\n", "aa\n", "a\n", "abbac\n", "aac\n"]}
236
95
coding
Solve the programming task below in a Python markdown code block. *This is the advanced version of the [Minimum and Maximum Product of k Elements](https://www.codewars.com/kata/minimum-and-maximum-product-of-k-elements/) kata.* --- Given a list of **integers** and a positive integer `k` (> 0), find the minimum and maximum possible product of `k` elements taken from the list. If you cannot take enough elements from the list, return `None`/`nil`. ## Examples ```python numbers = [1, -2, -3, 4, 6, 7] k = 1 ==> -3, 7 k = 2 ==> -21, 42 # -3*7, 6*7 k = 3 ==> -126, 168 # -3*6*7, 4*6*7 k = 7 ==> None # there are only 6 elements in the list ``` Note: the test lists can contain up to 500 elements, so a naive approach will not work. --- ## My other katas If you enjoyed this kata then please try [my other katas](https://www.codewars.com/collections/katas-created-by-anter69)! :-) #### *Translations are welcome!* Also feel free to reuse/extend the following starter code: ```python def find_min_max_product(arr, k): ```
{"functional": "_inputs = [[[0, 1, 2, 3, 4], 2], [[0, -1, -2, -3, -4], 2], [[0, -1, -2, -3, -4], 3], [[-1, -2, -3, -4], 2], [[-1, -2, -3, -4], 3], [[], 2], [[-4, -10, -1], 2], [[0, 6, 3, 5, 4], 4], [[5, 4, 3, 3, 6], 2]]\n_outputs = [[[0, 12]], [[0, 12]], [[-24, 0]], [[2, 12]], [[-24, -6]], [None], [[4, 40]], [[0, 360]], [[9, 30]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(find_min_max_product(*i), o[0])"}
322
353
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an encoded string, return its decoded string. The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer. You may assume that the input string is always valid; there are no extra white spaces, square brackets are well-formed, etc. Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there will not be input like 3a or 2[4]. The test cases are generated so that the length of the output will never exceed 105.   Please complete the following python code precisely: ```python class Solution: def decodeString(self, s: str) -> str: ```
{"functional": "def check(candidate):\n assert candidate(s = \"3[a]2[bc]\") == \"aaabcbc\"\n assert candidate(s = \"3[a2[c]]\") == \"accaccacc\"\n assert candidate(s = \"2[abc]3[cd]ef\") == \"abcabccdcdcdef\"\n assert candidate(s = \"abc3[cd]xyz\") == \"abccdcdcdxyz\"\n\n\ncheck(Solution().decodeString)"}
183
106
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. Consider the following function, where + denotes string concatenation. function BinaryConcatenation(integer X, integer Y): string binX = binary representation of X without leading zeroes string binY = binary representation of Y without leading zeroes string binXplusY = binX + binY string binYplusX = binY + binX integer XplusY = Convert binary representation binXplusY to integer integer YplusX = Convert binary representation binYplusX to integer return XplusY - YplusX You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$, Find the maximum value of BinaryConcatenation($A_{i}$, $A_{j}$) over all valid $i$ and $j$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. ------ Output ------ For each test case, print a single line containing one integer — the maximum of BinaryConcatenation. ------ Constraints ------ $1 ≤ T ≤ 10^{3}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} < 2^{30}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (50 points): the sum of $N$ over all test cases does not exceed $1,000$ Subtask #2 (50 points): original constraints ----- Sample Input 1 ------ 2 2 5 9 7 1 2 4 8 16 64 128 ----- Sample Output 1 ------ 12 127 ----- explanation 1 ------ Example case 1: The maximum value is $12 =$ BinaryConcatenation($5$, $9$). The function computes the following: - binX = "101" - binY = "1001" - binXplusY = "1011001" - binYplusX = "1001101" - XplusY = $89$ - YplusX = $77$ - the return value is $89-77 = 12$ Example case 2: The maximum value is $127 =$ BinaryConcatenation($1$, $128$).
{"inputs": ["2\n2\n5 9\n7\n1 2 4 8 16 64 128"], "outputs": ["12\n127"]}
624
43
coding
Solve the programming task below in a Python markdown code block. E869120 has A 1-yen coins and infinitely many 500-yen coins. Determine if he can pay exactly N yen using only these coins. -----Constraints----- - N is an integer between 1 and 10000 (inclusive). - A is an integer between 0 and 1000 (inclusive). -----Input----- Input is given from Standard Input in the following format: N A -----Output----- If E869120 can pay exactly N yen using only his 1-yen and 500-yen coins, print Yes; otherwise, print No. -----Sample Input----- 2018 218 -----Sample Output----- Yes We can pay 2018 yen with four 500-yen coins and 18 1-yen coins, so the answer is Yes.
{"inputs": ["2763\n1", "37\n308", "2763\n2", "37\n135", "37\n244", "37\n290", "37\n381", "37\n605"], "outputs": ["No\n", "Yes\n", "No\n", "Yes\n", "Yes\n", "Yes\n", "Yes\n", "Yes\n"]}
201
102