task_type
stringclasses
1 value
problem
stringlengths
261
3.34k
answer
stringlengths
35
6.15k
problem_tokens
int64
62
774
answer_tokens
int64
12
2.04k
coding
Solve the programming task below in a Python markdown code block. Problem statement Given the string $ S $. Find the number of all anagrams in $ S $ that are palindromic. An anagram of the string $ X $ is an anagram of $ Y $, which means that $ X $ is equal to $ Y $, or that the rearranged characters of $ X $ are equal to $ Y $. For example, for the string abcd, abcd and cbda are anagrams, but abed, cab and abcdd are not anagrams. When the string $ X $ is a palindrome, it means that the reverse reading of $ X $ is equal to $ X $ itself. For example, abc and ab are not palindromes, and a and abccba are palindromes. Constraint * $ 1 \ leq | S | \ leq 40 $ ($ | S | $ is the length of the string $ S $) * $ S $ contains only lowercase letters. * The answer is guaranteed to be less than $ 2 ^ {63} $. input Input follows the following format. $ S $ output Output the number on one line. Examples Input ab Output 0 Input abba Output 2
{"inputs": ["ac", "cc", "bc", "cb", "ca", "db", "bd", "be"], "outputs": ["0\n", "1\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
271
62
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a string s, where every two consecutive vertical bars '|' are grouped into a pair. In other words, the 1st and 2nd '|' make a pair, the 3rd and 4th '|' make a pair, and so forth. Return the number of '*' in s, excluding the '*' between each pair of '|'. Note that each '|' will belong to exactly one pair.   Please complete the following python code precisely: ```python class Solution: def countAsterisks(self, s: str) -> int: ```
{"functional": "def check(candidate):\n assert candidate(s = \"l|*e*et|c**o|*de|\") == 2\n assert candidate(s = \"iamprogrammer\") == 0\n assert candidate(s = \"yo|uar|e**|b|e***au|tifu|l\") == 5\n\n\ncheck(Solution().countAsterisks)"}
131
88
coding
Solve the programming task below in a Python markdown code block. Sereja has an n × m rectangular table a, each cell of the table contains a zero or a number one. Sereja wants his table to meet the following requirement: each connected component of the same values forms a rectangle with sides parallel to the sides of the table. Rectangles should be filled with cells, that is, if a component form a rectangle of size h × w, then the component must contain exactly hw cells. A connected component of the same values is a set of cells of the table that meet the following conditions: every two cells of the set have the same value; the cells of the set form a connected region on the table (two cells are connected if they are adjacent in some row or some column of the table); it is impossible to add any cell to the set unless we violate the two previous conditions. Can Sereja change the values of at most k cells of the table so that the table met the described requirement? What minimum number of table cells should he change in this case? -----Input----- The first line contains integers n, m and k (1 ≤ n, m ≤ 100; 1 ≤ k ≤ 10). Next n lines describe the table a: the i-th of them contains m integers a_{i}1, a_{i}2, ..., a_{im} (0 ≤ a_{i}, j ≤ 1) — the values in the cells of the i-th row. -----Output----- Print -1, if it is impossible to meet the requirement. Otherwise, print the minimum number of cells which should be changed. -----Examples----- Input 5 5 2 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 Output 1 Input 3 4 1 1 0 0 0 0 1 1 1 1 1 1 0 Output -1 Input 3 4 1 1 0 0 1 0 1 1 0 1 0 0 1 Output 0
{"inputs": ["1 1 1\n1\n", "1 1 1\n1\n", "0 1 1\n1\n", "1 1 1\n0\n", "2 2 1\n1 1\n1 0\n", "2 2 1\n1 1\n1 0\n", "2 2 1\n1 0\n1 0\n", "8 1 4\n0\n0\n0\n1\n0\n1\n1\n0\n"], "outputs": ["0\n", "0", "0\n", "0\n", "1\n", "1", "0\n", "0\n"]}
481
148
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are painting a fence of n posts with k different colors. You must paint the posts following these rules: Every post must be painted exactly one color. There cannot be three or more consecutive posts with the same color. Given the two integers n and k, return the number of ways you can paint the fence.   Please complete the following python code precisely: ```python class Solution: def numWays(self, n: int, k: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(n = 3, k = 2) == 6\n assert candidate(n = 1, k = 1) == 1\n assert candidate(n = 7, k = 2) == 42\n\n\ncheck(Solution().numWays)"}
115
72
coding
Solve the programming task below in a Python markdown code block. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of points on a plane. Each of the next n lines contains two real coordinates x_{i} and y_{i} of the $i^{\text{th}}$ point, specified with exactly 2 fractional digits. All coordinates are between - 1000 and 1000, inclusive. -----Output----- Output a single real number θ — the answer to the problem statement. The absolute or relative error of your answer should be at most 10^{ - 2}. -----Examples----- Input 8 -2.14 2.06 -1.14 2.04 -2.16 1.46 -2.14 0.70 -1.42 0.40 -0.94 -0.48 -1.42 -1.28 -2.16 -1.62 Output 5.410 Input 5 2.26 1.44 2.28 0.64 2.30 -0.30 1.58 0.66 3.24 0.66 Output 5.620 Input 8 6.98 2.06 6.40 1.12 5.98 0.24 5.54 -0.60 7.16 0.30 7.82 1.24 8.34 0.24 8.74 -0.76 Output 5.480 Input 5 10.44 2.06 10.90 0.80 11.48 -0.48 12.06 0.76 12.54 2.06 Output 6.040 Input 8 16.94 2.42 15.72 2.38 14.82 1.58 14.88 0.50 15.76 -0.16 16.86 -0.20 17.00 0.88 16.40 0.92 Output 6.040 Input 7 20.62 3.00 21.06 2.28 21.56 1.36 21.66 0.56 21.64 -0.52 22.14 2.32 22.62 3.04 Output 6.720
{"inputs": ["1\n1.00 1.01\n", "1\n0.00 0.01\n", "1\n0.00 0.01\n", "1\n1.00 1.01\n", "1\n792.52 879.16\n", "1\n792.52 879.16\n", "1\n1000.00 999.99\n", "1\n1000.00 999.99\n"], "outputs": ["6.010\n", "5.010\n", "5.010", "6.010", "884.160\n", "884.160", "1004.990\n", "1004.990"]}
655
206
coding
Solve the programming task below in a Python markdown code block. Read problems statements in [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. Chef's son Chefu found some matches in the kitchen and he immediately starting playing with them. The first thing Chefu wanted to do was to calculate the result of his homework — the sum of $A$ and $B$, and write it using matches. Help Chefu and tell him the number of matches needed to write the result. Digits are formed using matches in the following way: ------ 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 $A$ and $B$. ------ Output ------ For each test case, print a single line containing one integer — the number of matches needed to write the result ($A+B$). ------ Constraints ------ $1 ≤ T ≤ 1,000$ $1 ≤ A, B ≤ 10^{6}$ ----- Sample Input 1 ------ 3 123 234 10101 1010 4 4 ----- Sample Output 1 ------ 13 10 7 ----- explanation 1 ------ Example case 1: The result is $357$. We need $5$ matches to write the digit $3$, $5$ matches to write the digit $5$ and $3$ matches to write the digit $7$. Example case 2: The result is $11111$. We need $2$ matches to write each digit $1$, which is $2 \cdot 5 = 10$ matches in total.
{"inputs": ["3\n123 234\n10101 1010\n4 4"], "outputs": ["13\n10\n7"]}
386
41
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. Chef invited N of his friends in his birthday party. All the friends are numbered from 1 to N. Some of the friends might know each other. You are given this information by M pairs each of form (a_{i}, b_{i}), denoting that the persons a_{i} and b_{i} know each other. Chef wants all of his guests to seat at the two tables set up for dinner. He wants that all the people sitting at a table must know each other, otherwise they will feel awkward with mutual eating habits. Chef is okay if a table is not occupied by any person. Chef is worried whether all the guests can seat at the dinner tables in the desired way. Please help Chef fast to identify whether his worry is real or not!! The delicacies at the table are getting cold. ------ Input ------ The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains two space-separated integers N and M, denoting the number of Chef's friends and the number of pairs representing information whether two person know each other or not. The next M lines contain two space-separated integers a_{i} and b_{i}, denoting that persons a_{i} and b_{i} know each other. ------ Output ------ For each test case, output a single line containing word "YES" (without quotes) if Chef can divide all of his friends into two groups that in each group all the people know each other and "NO" (without quotes) otherwise. ------ Constraints ------ $1 ≤ T ≤ 10^{3}$ $1 ≤ N ≤ 10^{3}$ $0 ≤ M ≤ 10^{6}$ $1 ≤ a_{i}, b_{i} ≤ N$ $The sum of N over all test cases in a single test file does not exceed 10^{4}$ $The sum of M over all test cases in a single test file does not exceed 10^{6}$ ------ Subtasks ------ Subtask #1 (30 pts) $1 ≤ N ≤ 10$ Subtask #2 (70 pts) $Original constraints$ ----- Sample Input 1 ------ 3 3 2 1 2 2 3 4 3 1 2 2 3 2 4 6 7 1 2 1 3 2 3 2 4 4 5 4 6 5 6 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Example case 1. Table 1: Person 1. Table 2: Persons 2 and 3. Example case 2. All guests can't have a seat according to the rule. Example case 3. Table 1: Persons 1, 2, 3. Table 2: Persons 4, 5, 6.
{"inputs": ["3\n3 2\n1 2\n2 3\n4 3\n1 2\n2 3\n2 4\n6 7\n1 2\n1 3\n2 3\n2 4\n4 5\n4 6\n5 6"], "outputs": ["YES\nNO\nYES"]}
652
76
coding
Solve the programming task below in a Python markdown code block. The game of Berland poker is played with a deck of $n$ cards, $m$ of which are jokers. $k$ players play this game ($n$ is divisible by $k$). At the beginning of the game, each player takes $\frac{n}{k}$ cards from the deck (so each card is taken by exactly one player). The player who has the maximum number of jokers is the winner, and he gets the number of points equal to $x - y$, where $x$ is the number of jokers in the winner's hand, and $y$ is the maximum number of jokers among all other players. If there are two or more players with maximum number of jokers, all of them are winners and they get $0$ points. Here are some examples: $n = 8$, $m = 3$, $k = 2$. If one player gets $3$ jokers and $1$ plain card, and another player gets $0$ jokers and $4$ plain cards, then the first player is the winner and gets $3 - 0 = 3$ points; $n = 4$, $m = 2$, $k = 4$. Two players get plain cards, and the other two players get jokers, so both of them are winners and get $0$ points; $n = 9$, $m = 6$, $k = 3$. If the first player gets $3$ jokers, the second player gets $1$ joker and $2$ plain cards, and the third player gets $2$ jokers and $1$ plain card, then the first player is the winner, and he gets $3 - 2 = 1$ point; $n = 42$, $m = 0$, $k = 7$. Since there are no jokers, everyone gets $0$ jokers, everyone is a winner, and everyone gets $0$ points. Given $n$, $m$ and $k$, calculate the maximum number of points a player can get for winning the game. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 500$) — the number of test cases. Then the test cases follow. Each test case contains three integers $n$, $m$ and $k$ ($2 \le n \le 50$, $0 \le m \le n$, $2 \le k \le n$, $k$ is a divisors of $n$). -----Output----- For each test case, print one integer — the maximum number of points a player can get for winning the game. -----Example----- Input 4 8 3 2 4 2 4 9 6 3 42 0 7 Output 3 0 1 0 -----Note----- Test cases of the example are described in the statement.
{"inputs": ["1\n49 7 7\n", "1\n49 7 7\n", "1\n48 7 4\n", "1\n48 3 6\n", "1\n49 9 7\n", "1\n48 0 6\n", "1\n71 9 7\n", "1\n48 1 4\n"], "outputs": ["7\n", "7\n", "7\n", "3\n", "6\n", "0\n", "9\n", "1\n"]}
637
126
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a 0-indexed 2D integer matrix grid of size n * m, we define a 0-indexed 2D matrix p of size n * m as the product matrix of grid if the following condition is met: Each element p[i][j] is calculated as the product of all elements in grid except for the element grid[i][j]. This product is then taken modulo 12345. Return the product matrix of grid.   Please complete the following python code precisely: ```python class Solution: def constructProductMatrix(self, grid: List[List[int]]) -> List[List[int]]: ```
{"functional": "def check(candidate):\n assert candidate(grid = [[1,2],[3,4]]) == [[24,12],[8,6]]\n assert candidate(grid = [[12345],[2],[1]]) == [[2],[0],[0]]\n\n\ncheck(Solution().constructProductMatrix)"}
146
74
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an array of integers arr, you are initially positioned at the first index of the array. In one step you can jump from index i to index: i + 1 where: i + 1 < arr.length. i - 1 where: i - 1 >= 0. j where: arr[i] == arr[j] and i != j. Return the minimum number of steps to reach the last index of the array. Notice that you can not jump outside of the array at any time.   Please complete the following python code precisely: ```python class Solution: def minJumps(self, arr: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(arr = [100,-23,-23,404,100,23,23,23,3,404]) == 3\n assert candidate(arr = [7]) == 0\n assert candidate(arr = [7,6,9,6,9,6,9,7]) == 1\n\n\ncheck(Solution().minJumps)"}
156
101
coding
Solve the programming task below in a Python markdown code block. Alice and Bob are playing a game. They are given an array $A$ of length $N$. The array consists of integers. They are building a sequence together. In the beginning, the sequence is empty. In one turn a player can remove a number from the left or right side of the array and append it to the sequence. The rule is that the sequence they are building must be strictly increasing. The winner is the player that makes the last move. Alice is playing first. Given the starting array, under the assumption that they both play optimally, who wins the game? -----Input----- The first line contains one integer $N$ ($1 \leq N \leq 2*10^5$) - the length of the array $A$. The second line contains $N$ integers $A_1$, $A_2$,...,$A_N$ ($0 \leq A_i \leq 10^9$) -----Output----- The first and only line of output consists of one string, the name of the winner. If Alice won, print "Alice", otherwise, print "Bob". -----Examples----- Input 1 5 Output Alice Input 3 5 4 5 Output Alice Input 6 5 8 2 1 10 9 Output Bob -----Note----- None
{"inputs": ["1\n5\n", "1\n2\n", "1\n3\n", "1\n1\n", "1\n0\n", "1\n4\n", "1\n8\n", "1\n6\n"], "outputs": ["Alice\n", "Alice\n", "Alice\n", "Alice\n", "Alice\n", "Alice\n", "Alice\n", "Alice\n"]}
299
86
coding
Solve the programming task below in a Python markdown code block. A little bear Limak plays a game. He has five cards. There is one number written on each card. Each number is a positive integer. Limak can discard (throw out) some cards. His goal is to minimize the sum of numbers written on remaining (not discarded) cards. He is allowed to at most once discard two or three cards with the same number. Of course, he won't discard cards if it's impossible to choose two or three cards with the same number. Given five numbers written on cards, cay you find the minimum sum of numbers on remaining cards? -----Input----- The only line of the input contains five integers t_1, t_2, t_3, t_4 and t_5 (1 ≤ t_{i} ≤ 100) — numbers written on cards. -----Output----- Print the minimum possible sum of numbers written on remaining cards. -----Examples----- Input 7 3 7 3 20 Output 26 Input 7 9 3 1 8 Output 28 Input 10 10 10 10 10 Output 20 -----Note----- In the first sample, Limak has cards with numbers 7, 3, 7, 3 and 20. Limak can do one of the following. Do nothing and the sum would be 7 + 3 + 7 + 3 + 20 = 40. Remove two cards with a number 7. The remaining sum would be 3 + 3 + 20 = 26. Remove two cards with a number 3. The remaining sum would be 7 + 7 + 20 = 34. You are asked to minimize the sum so the answer is 26. In the second sample, it's impossible to find two or three cards with the same number. Hence, Limak does nothing and the sum is 7 + 9 + 1 + 3 + 8 = 28. In the third sample, all cards have the same number. It's optimal to discard any three cards. The sum of two remaining numbers is 10 + 10 = 20.
{"inputs": ["7 9 3 1 8\n", "8 7 1 8 7\n", "7 7 7 8 8\n", "8 8 8 2 2\n", "8 8 2 2 2\n", "1 1 1 1 1\n", "8 1 8 8 8\n", "1 1 1 8 1\n"], "outputs": ["28\n", "15\n", "16\n", "4\n", "6\n", "2\n", "9\n", "9\n"]}
487
137
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an integer array nums. In one move, you can pick an index i where 0 <= i < nums.length and increment nums[i] by 1. Return the minimum number of moves to make every value in nums unique. The test cases are generated so that the answer fits in a 32-bit integer.   Please complete the following python code precisely: ```python class Solution: def minIncrementForUnique(self, nums: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [1,2,2]) == 1\n assert candidate(nums = [3,2,1,2,1,7]) == 6\n\n\ncheck(Solution().minIncrementForUnique)"}
117
59
coding
Solve the programming task below in a Python markdown code block. Tracy loves Donuts. She purchased a lots of Donuts for her birthday party. She learnt to calculate the area of the circle a few days back and she is fascinated to know the area of the donuts as well !! Help her finding the area of the Donuts….. -----Input:----- - First line will contain, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, where in you have to provide the RADIUS of the Donuts. -----Output:----- For each testcase, output in a single line answer is the AREA of the Donut. -----Constraints----- 1 <= Radius <= 20. -----Sample Input:----- 2 5 12 -----Sample Output:----- 78.5 452.16
{"inputs": ["2\n5\n12"], "outputs": ["78.5\n452.16"]}
179
27
coding
Solve the programming task below in a Python markdown code block. Consider the infinite sequence $s$ of positive integers, created by repeating the following steps: Find the lexicographically smallest triple of positive integers $(a, b, c)$ such that $a \oplus b \oplus c = 0$, where $\oplus$ denotes the bitwise XOR operation. $a$, $b$, $c$ are not in $s$. Here triple of integers $(a_1, b_1, c_1)$ is considered to be lexicographically smaller than triple $(a_2, b_2, c_2)$ if sequence $[a_1, b_1, c_1]$ is lexicographically smaller than sequence $[a_2, b_2, c_2]$. Append $a$, $b$, $c$ to $s$ in this order. Go back to the first step. You have integer $n$. Find the $n$-th element of $s$. You have to answer $t$ independent test cases. A sequence $a$ is lexicographically smaller than a sequence $b$ if in the first position where $a$ and $b$ differ, the sequence $a$ has a smaller element than the corresponding element in $b$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. Each of the next $t$ lines contains a single integer $n$ ($1\le n \le 10^{16}$) — the position of the element you want to know. -----Output----- In each of the $t$ lines, output the answer to the corresponding test case. -----Example----- Input 9 1 2 3 4 5 6 7 8 9 Output 1 2 3 4 8 12 5 10 15 -----Note----- The first elements of $s$ are $1, 2, 3, 4, 8, 12, 5, 10, 15, \dots $
{"inputs": ["9\n1\n2\n3\n4\n5\n6\n7\n8\n9\n", "9\n1\n3\n3\n4\n5\n6\n7\n8\n9\n", "9\n1\n5\n3\n4\n5\n6\n7\n8\n9\n", "9\n1\n3\n3\n2\n5\n6\n7\n8\n9\n", "9\n1\n3\n3\n2\n7\n6\n7\n8\n9\n", "9\n2\n3\n3\n2\n7\n6\n7\n8\n9\n", "9\n1\n2\n3\n4\n6\n6\n7\n8\n9\n", "9\n1\n3\n3\n4\n3\n6\n7\n8\n9\n"], "outputs": ["1\n2\n3\n4\n8\n12\n5\n10\n15\n", "1\n3\n3\n4\n8\n12\n5\n10\n15\n", "1\n8\n3\n4\n8\n12\n5\n10\n15\n", "1\n3\n3\n2\n8\n12\n5\n10\n15\n", "1\n3\n3\n2\n5\n12\n5\n10\n15\n", "2\n3\n3\n2\n5\n12\n5\n10\n15\n", "1\n2\n3\n4\n12\n12\n5\n10\n15\n", "1\n3\n3\n4\n3\n12\n5\n10\n15\n"]}
469
367
coding
Solve the programming task below in a Python markdown code block. Chef wants to store some important numerical data on his personal computer. He is using a new data type that can store values only from 0 till N both inclusive. If this data type receives a value greater than N then it is cyclically converted to fit into the range 0 to N. For example: Value N+1 will be stored as 0. Value N+2 will be stored as 1. and so on... Given X, the value chef wants to store in this new data type. Determine what will be the actual value in memory after storing X. ------ Input Format ------ - First line will contain T, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, two space separated integers N, X - the maximum value a data type can store and the value Chef wants to store in the data type respectively. ------ Output Format ------ For each testcase, output in a single line the value which will be actually stored in memory. ------ Constraints ------ $1 ≤ T ≤ 3000$ $1 ≤ N ≤ 50$ $0 ≤ X ≤ 50$ ----- Sample Input 1 ------ 5 15 0 15 10 11 12 27 37 50 49 ----- Sample Output 1 ------ 0 10 0 9 49 ----- explanation 1 ------ Test Case $1$: The data type can store values from $0$ to $15$. If we try to put $0$ in this data type, then the stored value will be the same, that is $0$. Test Case $2$: The data type can store values from $0$ to $15$. If we try to put $10$ in this data type, then the stored value will be the same, that is $10$. Test Case $3$: The data type can store values from $0$ to $11$. If we try to put $12$ in this data type, then the stored value will cyclically come back to $0$. Hence the output is $0$. Test Case $4$: The data type can store values from $0$ to $27$. If we try to put $37$ in this data type, then the stored value will cyclically convert to $9$. Hence the output is $9$.
{"inputs": ["5\n15 0\n15 10\n11 12\n27 37\n50 49"], "outputs": ["0\n10\n0\n9\n49"]}
519
51
coding
Solve the programming task below in a Python markdown code block. A star map in Berland is a checked field n × m squares. In each square there is or there is not a star. The favourite constellation of all Berland's astronomers is the constellation of the Cross. This constellation can be formed by any 5 stars so, that for some integer x (radius of the constellation) the following is true: * the 2nd is on the same vertical line as the 1st, but x squares up * the 3rd is on the same vertical line as the 1st, but x squares down * the 4th is on the same horizontal line as the 1st, but x squares left * the 5th is on the same horizontal line as the 1st, but x squares right Such constellations can be very numerous, that's why they are numbered with integers from 1 on the following principle: when two constellations are compared, the one with a smaller radius gets a smaller index; if their radii are equal — the one, whose central star if higher than the central star of the other one; if their central stars are at the same level — the one, whose central star is to the left of the central star of the other one. Your task is to find the constellation with index k by the given Berland's star map. Input The first line contains three integers n, m and k (1 ≤ n, m ≤ 300, 1 ≤ k ≤ 3·107) — height and width of the map and index of the required constellation respectively. The upper-left corner has coordinates (1, 1), and the lower-right — (n, m). Then there follow n lines, m characters each — description of the map. j-th character in i-th line is «*», if there is a star in the corresponding square, and «.» if this square is empty. Output If the number of the constellations is less than k, output -1. Otherwise output 5 lines, two integers each — coordinates of the required constellation. Output the stars in the following order: central, upper, lower, left, right. Examples Input 5 6 1 ....*. ...*** ....*. ..*... .***.. Output 2 5 1 5 3 5 2 4 2 6 Input 5 6 2 ....*. ...*** ....*. ..*... .***.. Output -1 Input 7 7 2 ...*... ....... ...*... *.***.* ...*... ....... ...*... Output 4 4 1 4 7 4 4 1 4 7
{"inputs": ["5 5 3\n*.***\n.****\n..***\n*.***\n.**.*\n", "5 5 1\n.....\n.....\n.*..*\n*.*..\n....*\n", "5 5 4\n*.***\n.****\n..***\n*.***\n.**.*\n", "5 5 7\n*.***\n.****\n..***\n*.***\n.**.*\n", "5 5 3\n*.***\n.****\n..***\n***.*\n.**.*\n", "5 6 1\n....*.\n...***\n....*.\n..*...\n.*.*.*\n", "5 6 1\n....*.\n...***\n....*.\n...*..\n.*.*.*\n", "5 6 1\n....*.\n***...\n....*.\n...*..\n.*.*.*\n"], "outputs": ["3 4\n2 4\n4 4\n3 3\n3 5\n", "-1\n", "-1\n", "-1\n", "-1\n", "2 5\n1 5\n3 5\n2 4\n2 6\n", "2 5\n1 5\n3 5\n2 4\n2 6\n", "-1\n"]}
589
319
coding
Solve the programming task below in a Python markdown code block. Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step t_{i} (steps are numbered from 1) Xenia watches spies numbers l_{i}, l_{i} + 1, l_{i} + 2, ..., r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). -----Input----- The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 10^5; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers t_{i}, l_{i}, r_{i} (1 ≤ t_{i} ≤ 10^9, 1 ≤ l_{i} ≤ r_{i} ≤ n). It is guaranteed that t_1 < t_2 < t_3 < ... < t_{m}. -----Output----- Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. -----Examples----- Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
{"inputs": ["5 1 1 5\n1 1 1\n", "5 1 1 5\n1 1 1\n", "10 1 1 10\n1 5 6\n", "10 1 1 10\n1 5 6\n", "10 1 1 10\n1 9 6\n", "10 1 1 10\n1 2 6\n", "10 1 1 10\n1 7 6\n", "2 3 2 1\n1 1 2\n2 1 2\n4 1 2\n"], "outputs": ["XRRRR\n", "XRRRR", "RRRRRRRRR\n", "RRRRRRRRR", "RRRRRRRRR\n", "XRRRRRRRRR\n", "RRRRRRRRR\n", "XXL\n"]}
719
212
coding
Solve the programming task below in a Python markdown code block. A certain business maintains a list of all its customers' names. The list is arranged in order of importance, with the last customer in the list being the most important. Now, he want to create a new list sorted alphabetically according to customers' last names, but among customers with the same last name he want the more important ones to appear earlier in the new list. Alphabetical order (and equality of last names) should not be case sensitive. Input:- First line contains no. of test cases and first line of each test case contains n i.e. no. of elements and next n lines contains contains a name. Output:- Print the new list with each element in a new line. SAMPLE INPUT 2 5 Tom Jones ADAMS BOB ADAMS Tom Jones STEVE jONeS 3 Trudy Trudy TRUDY SAMPLE OUTPUT BOB ADAMS ADAMS STEVE jONeS Tom Jones Tom Jones TRUDY Trudy Trudy
{"inputs": ["4\n6\na b c d\nA\nThomas a\nd d\ned edgars\nal Adams\n1\nTurnipHeadJohnson\n3\na a a a a a a a a a\nA A A A A A A A A A\nbob\n5\nBob Mackey\nMack Bobby\nbobby\nMack Mackey\nMackey", "4\n10\nAlan\naLan\nalAn\nalaN\nALan\nAlAn\nAlaN\naLAn\naLaN\nalAN\n4\nabcdefghijklmnop T\nabccefghijklmnop T\nt tt ttt tttt\nTTTT TTT TT T\n4\nKenny G\nKenny Gee\nA DeForest Hoar\nC A R Hoare\n3\nk a\nm a\nk a"], "outputs": ["Thomas a\nA\nal Adams\nd d\na b c d\ned edgars\nTurnipHeadJohnson\nA A A A A A A A A A\na a a a a a a a a a\nbob\nbobby\nMack Bobby\nMackey\nMack Mackey\nBob Mackey", "alAN\naLaN\naLAn\nAlaN\nAlAn\nALan\nalaN\nalAn\naLan\nAlan\nTTTT TTT TT T\nabccefghijklmnop T\nabcdefghijklmnop T\nt tt ttt tttt\nKenny G\nKenny Gee\nA DeForest Hoar\nC A R Hoare\nk a\nm a\nk a"]}
227
341
coding
Solve the programming task below in a Python markdown code block. You are given an array consisting of $n$ integers $a_1, a_2, \dots , a_n$ and an integer $x$. It is guaranteed that for every $i$, $1 \le a_i \le x$. Let's denote a function $f(l, r)$ which erases all values such that $l \le a_i \le r$ from the array $a$ and returns the resulting array. For example, if $a = [4, 1, 1, 4, 5, 2, 4, 3]$, then $f(2, 4) = [1, 1, 5]$. Your task is to calculate the number of pairs $(l, r)$ such that $1 \le l \le r \le x$ and $f(l, r)$ is sorted in non-descending order. Note that the empty array is also considered sorted. -----Input----- The first line contains two integers $n$ and $x$ ($1 \le n, x \le 10^6$) — the length of array $a$ and the upper limit for its elements, respectively. The second line contains $n$ integers $a_1, a_2, \dots a_n$ ($1 \le a_i \le x$). -----Output----- Print the number of pairs $1 \le l \le r \le x$ such that $f(l, r)$ is sorted in non-descending order. -----Examples----- Input 3 3 2 3 1 Output 4 Input 7 4 1 3 1 2 2 4 3 Output 6 -----Note----- In the first test case correct pairs are $(1, 1)$, $(1, 2)$, $(1, 3)$ and $(2, 3)$. In the second test case correct pairs are $(1, 3)$, $(1, 4)$, $(2, 3)$, $(2, 4)$, $(3, 3)$ and $(3, 4)$.
{"inputs": ["1 1\n1\n", "1 1\n1\n", "2 3\n1 1\n", "2 3\n3 2\n", "2 3\n3 2\n", "2 3\n1 1\n", "2 4\n3 2\n", "2 20\n1 8\n"], "outputs": ["1\n", "1\n", "6\n", "5\n", "5\n", "6\n", "8\n", "210\n"]}
461
117
coding
Solve the programming task below in a Python markdown code block. Create a program that takes two dates as input and outputs the number of days between the two dates. Date 1 (y1, m1, d1) is the same as or earlier than date 2 (y2, m2, d2). Date 1 is included in the number of days, not date 2. Also, take the leap year into account when calculating. The leap year conditions are as follows. * The year is divisible by 4. * However, a year divisible by 100 is not a leap year. * However, a year divisible by 400 is a leap year. Input Given multiple datasets. The format of each dataset is as follows: y1 m1 d1 y2 m2 d2 When any of y1, m1, d1, y2, m2, and d2 is a negative number, the input ends. The number of datasets does not exceed 50. Output Output the number of days on one line for each dataset. Example Input 2006 9 2 2006 9 3 2006 9 2 2006 11 11 2004 1 1 2005 1 1 2000 1 1 2006 1 1 2000 1 1 2101 1 1 -1 -1 -1 -1 -1 -1 Output 1 70 366 2192 36890
{"inputs": ["2006 3 2 522 6 3\n2006 8 1 89 11 11\n2004 1 2 362 1 2\n3334 2 1 2180 1 1\n2000 2 1 2101 1 1\n-1 -1 -1 -1 -1 -1", "2006 9 2 522 9 2\n2006 9 1 19 11 11\n2004 1 2 2005 1 1\n1153 1 1 2006 1 1\n2000 1 1 2101 1 1\n-1 -1 -1 -1 -1 -1", "2006 9 2 522 9 3\n2006 9 1 2006 11 11\n2004 1 2 2005 1 1\n218 1 1 2006 1 1\n2000 1 1 2101 1 1\n-1 -1 -1 -1 -1 -1", "2006 9 2 522 9 3\n2006 9 1 2006 11 11\n2004 1 2 2005 1 1\n218 2 1 2006 1 1\n2000 1 1 2101 1 1\n-1 -1 -1 -1 -1 -1", "2006 9 2 522 9 3\n2006 9 1 2006 11 11\n2004 1 2 2005 1 2\n218 2 1 2006 1 1\n2000 1 1 2101 1 1\n-1 -1 -1 -1 -1 -1", "2006 9 2 522 9 3\n2006 9 1 3224 11 11\n2004 1 2 2005 1 2\n218 2 1 2006 1 1\n2000 1 1 2101 1 1\n-1 -1 -1 -1 -1 -1", "2006 9 2 522 8 3\n2006 8 1 1174 11 11\n2004 1 2 362 1 1\n3334 2 1 2180 1 1\n2000 1 1 2101 1 1\n-1 -1 -1 -1 -1 -1", "2006 3 2 522 8 3\n2006 8 1 1174 11 11\n2004 1 2 362 1 1\n3334 2 1 2180 1 1\n2000 1 1 2101 1 1\n-1 -1 -1 -1 -1 -1"], "outputs": ["-541927\n-700067\n-599728\n-421521\n36859\n", "-542020\n-725666\n365\n311552\n36890\n", "-542019\n71\n365\n653054\n36890\n", "-542019\n71\n365\n653023\n36890\n", "-542019\n71\n366\n653023\n36890\n", "-542019\n444937\n366\n653023\n36890\n", "-542050\n-303780\n-599729\n-421521\n36890\n", "-541866\n-303780\n-599729\n-421521\n36890\n"]}
356
1,107
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given the root of a binary tree with n nodes. Each node is uniquely assigned a value from 1 to n. You are also given an integer startValue representing the value of the start node s, and a different integer destValue representing the value of the destination node t. Find the shortest path starting from node s and ending at node t. Generate step-by-step directions of such path as a string consisting of only the uppercase letters 'L', 'R', and 'U'. Each letter indicates a specific direction: 'L' means to go from a node to its left child node. 'R' means to go from a node to its right child node. 'U' means to go from a node to its parent node. Return the step-by-step directions of the shortest path from node s to node t.   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 getDirections(self, root: Optional[TreeNode], startValue: int, destValue: int) -> str: ```
{"functional": "def check(candidate):\n assert candidate(root = tree_node([5,1,2,3,None,6,4]), startValue = 3, destValue = 6) == \"UURL\"\n assert candidate(root = tree_node([2,1]), startValue = 2, destValue = 1) == \"L\"\n\n\ncheck(Solution().getDirections)"}
275
87
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value. If target is not found in the array, return [-1, -1]. You must write an algorithm with O(log n) runtime complexity.   Please complete the following python code precisely: ```python class Solution: def searchRange(self, nums: List[int], target: int) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(nums = [5,7,7,8,8,10], target = 8) == [3,4]\n assert candidate(nums = [5,7,7,8,8,10], target = 6) == [-1,-1]\n assert candidate(nums = [], target = 0) == [-1,-1]\n\n\ncheck(Solution().searchRange)"}
109
99
coding
Solve the programming task below in a Python markdown code block. Given a string of words (x), you need to return an array of the words, sorted alphabetically by the final character in each. If two words have the same last letter, they returned array should show them in the order they appeared in the given string. All inputs will be valid. Also feel free to reuse/extend the following starter code: ```python def last(s): ```
{"functional": "_inputs = [['man i need a taxi up to ubud'], ['what time are we climbing up the volcano'], ['take me to semynak'], ['massage yes massage yes massage'], ['take bintang and a dance please']]\n_outputs = [[['a', 'need', 'ubud', 'i', 'taxi', 'man', 'to', 'up']], [['time', 'are', 'we', 'the', 'climbing', 'volcano', 'up', 'what']], [['take', 'me', 'semynak', 'to']], [['massage', 'massage', 'massage', 'yes', 'yes']], [['a', 'and', 'take', 'dance', 'please', 'bintang']]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(last(*i), o[0])"}
92
297
coding
Solve the programming task below in a Python markdown code block. Polycarp found a rectangular table consisting of $n$ rows and $m$ columns. He noticed that each cell of the table has its number, obtained by the following algorithm "by columns": cells are numbered starting from one; cells are numbered from left to right by columns, and inside each column from top to bottom; number of each cell is an integer one greater than in the previous cell. For example, if $n = 3$ and $m = 5$, the table will be numbered as follows: $$ \begin{matrix} 1 & 4 & 7 & 10 & 13 \\ 2 & 5 & 8 & 11 & 14 \\ 3 & 6 & 9 & 12 & 15 \\ \end{matrix} $$ However, Polycarp considers such numbering inconvenient. He likes the numbering "by rows": cells are numbered starting from one; cells are numbered from top to bottom by rows, and inside each row from left to right; number of each cell is an integer one greater than the number of the previous cell. For example, if $n = 3$ and $m = 5$, then Polycarp likes the following table numbering: $$ \begin{matrix} 1 & 2 & 3 & 4 & 5 \\ 6 & 7 & 8 & 9 & 10 \\ 11 & 12 & 13 & 14 & 15 \\ \end{matrix} $$ Polycarp doesn't have much time, so he asks you to find out what would be the cell number in the numbering "by rows", if in the numbering "by columns" the cell has the number $x$? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow. Each test case consists of a single line containing three integers $n$, $m$, $x$ ($1 \le n, m \le 10^6$, $1 \le x \le n \cdot m$), where $n$ and $m$ are the number of rows and columns in the table, and $x$ is the cell number. Note that the numbers in some test cases do not fit into the $32$-bit integer type, so you must use at least the $64$-bit integer type of your programming language. -----Output----- For each test case, output the cell number in the numbering "by rows". -----Examples----- Input 5 1 1 1 2 2 3 3 5 11 100 100 7312 1000000 1000000 1000000000000 Output 1 2 9 1174 1000000000000 -----Note----- None
{"inputs": ["5\n1 1 1\n2 2 3\n3 6 1\n110 100 498\n1000000 1000000 1000000000000\n", "5\n1 1 1\n2 2 3\n3 6 1\n110 100 498\n1000000 1100000 1000000000000\n", "5\n1 1 1\n2 2 3\n3 6 1\n110 100 507\n1000000 1000000 1000000000000\n", "5\n1 1 1\n2 2 3\n3 6 1\n100 100 498\n1000000 1100000 1000000000000\n", "5\n1 1 1\n2 2 3\n3 6 1\n110 101 507\n1000000 1000000 1000000000000\n", "5\n1 1 1\n2 4 3\n3 6 1\n110 100 605\n1000000 1000000 1000000000000\n", "5\n1 1 1\n7 4 3\n3 7 5\n110 100 877\n1000010 1000110 1000000100100\n", "5\n1 1 1\n7 4 2\n3 7 5\n110 100 877\n1000010 1000110 1000000100100\n"], "outputs": ["1\n2\n1\n5705\n1000000000000\n", "1\n2\n1\n5705\n1099999900000\n", "1\n2\n1\n6605\n1000000000000\n", "1\n2\n1\n9705\n1099999900000\n", "1\n2\n1\n6671\n1000000000000\n", "1\n2\n1\n5406\n1000000000000\n", "1\n9\n9\n10608\n100211021881\n", "1\n5\n9\n10608\n100211021881\n"]}
654
734
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an integer array score of size n, where score[i] is the score of the ith athlete in a competition. All the scores are guaranteed to be unique. The athletes are placed based on their scores, where the 1st place athlete has the highest score, the 2nd place athlete has the 2nd highest score, and so on. The placement of each athlete determines their rank: The 1st place athlete's rank is "Gold Medal". The 2nd place athlete's rank is "Silver Medal". The 3rd place athlete's rank is "Bronze Medal". For the 4th place to the nth place athlete, their rank is their placement number (i.e., the xth place athlete's rank is "x"). Return an array answer of size n where answer[i] is the rank of the ith athlete.   Please complete the following python code precisely: ```python class Solution: def findRelativeRanks(self, score: List[int]) -> List[str]: ```
{"functional": "def check(candidate):\n assert candidate(score = [5,4,3,2,1]) == [\"Gold Medal\",\"Silver Medal\",\"Bronze Medal\",\"4\",\"5\"]\n assert candidate(score = [10,3,8,9,4]) == [\"Gold Medal\",\"5\",\"Bronze Medal\",\"Silver Medal\",\"4\"]\n\n\ncheck(Solution().findRelativeRanks)"}
226
92
coding
Solve the programming task below in a Python markdown code block. Greg has an array a = a_1, a_2, ..., a_{n} and m operations. Each operation looks as: l_{i}, r_{i}, d_{i}, (1 ≤ l_{i} ≤ r_{i} ≤ n). To apply operation i to the array means to increase all array elements with numbers l_{i}, l_{i} + 1, ..., r_{i} by value d_{i}. Greg wrote down k queries on a piece of paper. Each query has the following form: x_{i}, y_{i}, (1 ≤ x_{i} ≤ y_{i} ≤ m). That means that one should apply operations with numbers x_{i}, x_{i} + 1, ..., y_{i} to the array. Now Greg is wondering, what the array a will be after all the queries are executed. Help Greg. -----Input----- The first line contains integers n, m, k (1 ≤ n, m, k ≤ 10^5). The second line contains n integers: a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^5) — the initial array. Next m lines contain operations, the operation number i is written as three integers: l_{i}, r_{i}, d_{i}, (1 ≤ l_{i} ≤ r_{i} ≤ n), (0 ≤ d_{i} ≤ 10^5). Next k lines contain the queries, the query number i is written as two integers: x_{i}, y_{i}, (1 ≤ x_{i} ≤ y_{i} ≤ m). The numbers in the lines are separated by single spaces. -----Output----- On a single line print n integers a_1, a_2, ..., a_{n} — the array after executing all the queries. Separate the printed numbers by spaces. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier. -----Examples----- Input 3 3 3 1 2 3 1 2 1 1 3 2 2 3 4 1 2 1 3 2 3 Output 9 18 17 Input 1 1 1 1 1 1 1 1 1 Output 2 Input 4 3 6 1 2 3 4 1 2 1 2 3 2 3 4 4 1 2 1 3 2 3 1 2 1 3 2 3 Output 5 18 31 20
{"inputs": ["1 1 1\n1\n1 1 1\n1 1\n", "1 1 1\n0\n1 1 0\n1 1\n", "1 1 1\n0\n1 1 0\n1 1\n", "1 1 1\n1\n1 1 0\n1 1\n", "1 1 1\n0\n1 1 0\n1 0\n", "1 1 1\n1\n1 1 1\n1 1\n", "3 3 3\n1 2 3\n1 2 1\n1 3 2\n2 3 4\n1 2\n1 3\n2 3\n", "3 3 3\n1 2 3\n1 2 1\n2 3 2\n2 3 4\n1 2\n1 3\n2 3\n"], "outputs": ["2\n", "0\n", "0 \n", "1\n", "0\n", "2 \n", "9 18 17\n", "3 18 17\n"]}
604
260
coding
Solve the programming task below in a Python markdown code block. Peter wrote on the board a strictly increasing sequence of positive integers a_1, a_2, ..., a_{n}. Then Vasil replaced some digits in the numbers of this sequence by question marks. Thus, each question mark corresponds to exactly one lost digit. Restore the the original sequence knowing digits remaining on the board. -----Input----- The first line of the input contains integer n (1 ≤ n ≤ 10^5) — the length of the sequence. Next n lines contain one element of the sequence each. Each element consists only of digits and question marks. No element starts from digit 0. Each element has length from 1 to 8 characters, inclusive. -----Output----- If the answer exists, print in the first line "YES" (without the quotes). Next n lines must contain the sequence of positive integers — a possible variant of Peter's sequence. The found sequence must be strictly increasing, it must be transformed from the given one by replacing each question mark by a single digit. All numbers on the resulting sequence must be written without leading zeroes. If there are multiple solutions, print any of them. If there is no answer, print a single line "NO" (without the quotes). -----Examples----- Input 3 ? 18 1? Output YES 1 18 19 Input 2 ?? ? Output NO Input 5 12224 12??5 12226 ?0000 ?00000 Output YES 12224 12225 12226 20000 100000
{"inputs": ["2\n5\n7\n", "2\n3\n7\n", "2\n6\n7\n", "2\n??\n?\n", "2\n50\n5\n", "2\n50\n5\n", "2\n25\n5\n", "2\n23\n5\n"], "outputs": ["YES\n5\n7\n", "YES\n3\n7\n", "YES\n6\n7\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n"]}
363
119
coding
Solve the programming task below in a Python markdown code block. Let's define a function $f(x)$ ($x$ is a positive integer) as follows: write all digits of the decimal representation of $x$ backwards, then get rid of the leading zeroes. For example, $f(321) = 123$, $f(120) = 21$, $f(1000000) = 1$, $f(111) = 111$. Let's define another function $g(x) = \dfrac{x}{f(f(x))}$ ($x$ is a positive integer as well). Your task is the following: for the given positive integer $n$, calculate the number of different values of $g(x)$ among all numbers $x$ such that $1 \le x \le n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 100$) — the number of test cases. Each test case consists of one line containing one integer $n$ ($1 \le n < 10^{100}$). This integer is given without leading zeroes. -----Output----- For each test case, print one integer — the number of different values of the function $g(x)$, if $x$ can be any integer from $[1, n]$. -----Examples----- Input 5 4 37 998244353 1000000007 12345678901337426966631415 Output 1 2 9 10 26 -----Note----- Explanations for the two first test cases of the example: if $n = 4$, then for every integer $x$ such that $1 \le x \le n$, $\dfrac{x}{f(f(x))} = 1$; if $n = 37$, then for some integers $x$ such that $1 \le x \le n$, $\dfrac{x}{f(f(x))} = 1$ (for example, if $x = 23$, $f(f(x)) = 23$,$\dfrac{x}{f(f(x))} = 1$); and for other values of $x$, $\dfrac{x}{f(f(x))} = 10$ (for example, if $x = 30$, $f(f(x)) = 3$, $\dfrac{x}{f(f(x))} = 10$). So, there are two different values of $g(x)$.
{"inputs": ["1\n1\n", "1\n1\n", "1\n2\n", "1\n4\n", "1\n3\n", "1\n6\n", "1\n5\n", "1\n8\n"], "outputs": ["1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n"]}
574
86
coding
Solve the programming task below in a Python markdown code block. Given a sequence of N elements, Chef defines a function F(i) (1 ≤ i ≤ N) as the median of the first i elements of the sequence. Chef wants to get a *permutation* P of length N such that P_{i} = F(i) \forall 1 ≤ i ≤ N. Can you help Chef in achieving this? Note that: The *median* of a sequence is the middle element in the sorted order of the sequence. If N is even, choose the element at \frac{N}{2} position in the sorted order. If N is odd, choose the middle element. A *permutation* of length N is an array of N integers P = [P_{1}, P_{2}, \ldots, P_{N}] such that every integer from 1 to N (inclusive) appears in it exactly once. For example, [2,5,4,1,3] is a permutation of length 5 while [2, 5, 2, 1, 3] is not. ------ Input Format ------ - First line will contain T, number of test cases. Then the test cases follows. - Each test case consists of a single line of input, an integer N, the length of the required permutation. ------ Output Format ------ For each test case, output in a new line, N space-separated integers, denoting a *permutation* satisfying the condition. ------ Constraints ------ $1 ≤ T ≤ 700$ $1 ≤ N ≤ 10^{5}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 1 2 3 ----- Sample Output 1 ------ 1 2 1 3 1 2 ----- explanation 1 ------ Test Case $1$: $[1]$ is the only permutation and it satisfies the condition as the median of the sequence $[1]$ is $1$. Test Case $2$: The possible permutations of length $2$ are $[1, 2]$ and $[2, 1]$. The only permutation that satisfies the condition is $[2, 1]$. Here, $P_{1} = 2 = median([2])$ and $P_{2} = 1 = median([2, 1])$. Test Case $3$: There are $6$ possible permutations out of which only $[3,1,2]$ satisfies the above condition. Here, $P_{1} = 3 = median([3]), P_{2} = 1 = median([3, 1]),$ and $P_{3} = 2 = median([3, 1, 2])$.
{"inputs": ["3\n1\n2\n3\n"], "outputs": ["1\n2 1 \n3 1 2"]}
595
30
coding
Solve the programming task below in a Python markdown code block. A lot of people associate Logo programming language with turtle graphics. In this case the turtle moves along the straight line and accepts commands "T" ("turn around") and "F" ("move 1 unit forward"). You are given a list of commands that will be given to the turtle. You have to change exactly n commands from the list (one command can be changed several times). How far from the starting point can the turtle move after it follows all the commands of the modified list? Input The first line of input contains a string commands — the original list of commands. The string commands contains between 1 and 100 characters, inclusive, and contains only characters "T" and "F". The second line contains an integer n (1 ≤ n ≤ 50) — the number of commands you have to change in the list. Output Output the maximum distance from the starting point to the ending point of the turtle's path. The ending point of the turtle's path is turtle's coordinate after it follows all the commands of the modified list. Examples Input FT 1 Output 2 Input FFFTFFF 2 Output 6 Note In the first example the best option is to change the second command ("T") to "F" — this way the turtle will cover a distance of 2 units. In the second example you have to change two commands. One of the ways to cover maximal distance of 6 units is to change the fourth command and first or last one.
{"inputs": ["F\n1\n", "FT\n1\n", "TTFFF\n49\n", "FFFTFFF\n3\n", "FFFTFFF\n2\n", "FFFFTFTF\n1\n", "FTTTFTFF\n8\n", "FFFFTFTF\n0\n"], "outputs": ["0\n", "2\n", "4\n", "7\n", "6\n", "5\n", "8\n", "4\n"]}
323
101
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an array of n integers nums, a 132 pattern is a subsequence of three integers nums[i], nums[j] and nums[k] such that i < j < k and nums[i] < nums[k] < nums[j]. Return true if there is a 132 pattern in nums, otherwise, return false.   Please complete the following python code precisely: ```python class Solution: def find132pattern(self, nums: List[int]) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(nums = [1,2,3,4]) == False\n assert candidate(nums = [3,1,4,2]) == True\n assert candidate(nums = [-1,3,2,0]) == True\n\n\ncheck(Solution().find132pattern)"}
120
73
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Mandarin], [Bengali], [Russian], and [Vietnamese] as well. Chef is very adventurous, so he asked Bob to give him a task. Bob gave him a sequence of blocks with heights A_{1}, A_{2}, \ldots, A_{N}. Chef is at the first block and he has to reach the N-th block using the minimum number of moves to complete the task. In one move, he can jump from the i-th block to the j-th block only if the following conditions are satisfied: i < j A_{i} ≥ A_{j} for all k (i < k < j), A_{k} ≤ A_{j} You have to find the minimum number of moves Chef needs to perform to reach the last block, or determine that it is impossible. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer --- the minimum number of moves or -1 if it is impossible to reach the last block. ------ Constraints ------ $1 ≤ T ≤ 100$ $2 ≤ N ≤ 10^{5}$ $0 ≤ A_{i} ≤ 10^{5}$ for each valid $i$ - the sum of $N$ over all test cases does not exceed $10^{6}$ ------ subtasks ------ Subtask #1 (30 points): $2 ≤ N ≤ 1,000$ the sum of $N$ over all test cases does not exceed $5 \cdot 10^{4}$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 2 5 9 15 8 13 8 9 20 16 13 9 17 11 15 8 7 ----- Sample Output 1 ------ -1 4 ----- explanation 1 ------ Example case 1: There is no way to move from the first block (with height $9$) to any other block. Example case 2: The only sequence of $4$ moves is $20 \rightarrow 17 \rightarrow 15 \rightarrow 8 \rightarrow 7$. For example, in the first move, all the heights between $20$ and $17$ do not exceed $17$, so all conditions are satisfied.
{"inputs": ["2\n5\n9 15 8 13 8\n9\n20 16 13 9 17 11 15 8 7"], "outputs": ["-1\n4"]}
589
55
coding
Solve the programming task below in a Python markdown code block. For a positive integer n * If n is even, divide by 2. * If n is odd, multiply by 3 and add 1. If you repeat the above operation, the result will be 1. A problem called "Colatz conjecture" is that repeating this operation for any positive integer n will always result in 1. This problem is an unsolved problem, also known as the "Kakutani problem" in Japan. It is known that there is no counterexample for a very large number 3 × 253 = 27,021,597,764,222,976 using a computer, but it has not been mathematically proven. Create a program that takes the integer n as an input and outputs the number of operations that are repeated until the result is 1. The integer n should be an integer that is 1 or more and the value in the middle of repeating the above calculation is 1000000 or less. For example, if you receive 3 as input, the operation column will be 3 → 10 → 5 → 16 → 8 → 4 → 2 → 1 Therefore, 7 is output, which is the number of operations (the number of arrows above). Input A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. One integer n (n ≤ 1000000) is given on one row for each dataset. The number of datasets does not exceed 50. Output Outputs the number of operations for each dataset on one line. Example Input 3 10 0 Output 7 6
{"inputs": ["3\n3\n0", "3\n2\n0", "3\n9\n0", "3\n6\n0", "3\n1\n0", "3\n0\n0", "3\n4\n0", "3\n5\n0"], "outputs": ["7\n7\n", "7\n1\n", "7\n19\n", "7\n8\n", "7\n0\n", "7\n", "7\n2\n", "7\n5\n"]}
382
109
coding
Solve the programming task below in a Python markdown code block. Find the largest square number not exceeding N. Here, a square number is an integer that can be represented as the square of an integer. -----Constraints----- - 1 \leq N \leq 10^9 - N is an integer. -----Input----- Input is given from Standard Input in the following format: N -----Output----- Print the largest square number not exceeding N. -----Sample Input----- 10 -----Sample Output----- 9 10 is not square, but 9 = 3 × 3 is. Thus, we print 9.
{"inputs": ["7", "2", "0", "70", "12", "16", "26", "38"], "outputs": ["4\n", "1\n", "0\n", "64\n", "9\n", "16\n", "25\n", "36\n"]}
131
71
coding
Solve the programming task below in a Python markdown code block. Six friends go on a trip and are looking for accommodation. After looking for hours, they find a hotel which offers two types of rooms — double rooms and triple rooms. A double room costs Rs. X, while a triple room costs Rs. Y. The friends can either get three double rooms or get two triple rooms. Find the minimum amount they will have to pay to accommodate all six of them. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first and only line of each test case contains two integers X and Y - the cost of a double room and the cost of a triple room. ------ Output Format ------ For each testcase, output the minimum amount required to accommodate all the six friends. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ X < Y ≤ 100$ ----- Sample Input 1 ------ 3 10 15 6 8 4 8 ----- Sample Output 1 ------ 30 16 12 ----- explanation 1 ------ Test case 1: The friends can take three double rooms and thus pay a total of Rs. $30$. Test case 2: The friends can take two triple rooms and thus pay a total of Rs. $16$. Test case 3: The friends can take three double rooms and thus pay a total of Rs. $12$.
{"inputs": ["3\n10 15\n6 8\n4 8\n"], "outputs": ["30\n16\n12\n"]}
318
35
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Alice has an undirected tree with n nodes labeled from 0 to n - 1. The tree is represented as a 2D integer array edges of length n - 1 where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree. Alice wants Bob to find the root of the tree. She allows Bob to make several guesses about her tree. In one guess, he does the following: Chooses two distinct integers u and v such that there exists an edge [u, v] in the tree. He tells Alice that u is the parent of v in the tree. Bob's guesses are represented by a 2D integer array guesses where guesses[j] = [uj, vj] indicates Bob guessed uj to be the parent of vj. Alice being lazy, does not reply to each of Bob's guesses, but just says that at least k of his guesses are true. Given the 2D integer arrays edges, guesses and the integer k, return the number of possible nodes that can be the root of Alice's tree. If there is no such tree, return 0.   Please complete the following python code precisely: ```python class Solution: def rootCount(self, edges: List[List[int]], guesses: List[List[int]], k: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(edges = [[0,1],[1,2],[1,3],[4,2]], guesses = [[1,3],[0,1],[1,0],[2,4]], k = 3) == 3\n assert candidate(edges = [[0,1],[1,2],[2,3],[3,4]], guesses = [[1,0],[3,4],[2,1],[3,2]], k = 1) == 5\n\n\ncheck(Solution().rootCount)"}
295
119
coding
Solve the programming task below in a Python markdown code block. For each bill you pay using CRED, you earn X CRED coins. At CodeChef store, each bag is worth 100 CRED coins. Chef pays Y number of bills using CRED. Find the maximum number of bags he can get from the CodeChef store. ------ 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, two integers X and Y. ------ Output Format ------ For each test case, output in a single line - the maximum number of bags Chef can get from the CodeChef store. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ X,Y ≤ 1000$ ------ subtasks ------ Subtask 1 (100 points): Original constraints. ----- Sample Input 1 ------ 3 10 10 20 4 70 7 ----- Sample Output 1 ------ 1 0 4 ----- explanation 1 ------ Test Case $1$: For each bill payment, one receives $10$ CRED coins. Chef pays $10$ bills using CRED. Thus, he receives $100$ CRED coins. Chef can get $1$ bag from the CodeChef store using these coins. Test Case $2$: For each bill payment, one receives $20$ CRED coins. Chef pays $4$ bills using CRED. Thus, he receives $80$ CRED coins. Chef cannot get any bag from the CodeChef store using these coins. Test Case $3$: For each bill payment, one receives $70$ CRED coins. Chef pays $7$ bills using CRED. Thus, he receives $490$ CRED coins. Chef can get at most $4$ bags from the CodeChef store using these coins.
{"inputs": ["3\n10 10\n20 4\n70 7"], "outputs": ["1\n0\n4"]}
410
32
coding
Solve the programming task below in a Python markdown code block. Alice and Bob have received three big piles of candies as a gift. Now they want to divide these candies as fair as possible. To do this, Alice takes one pile of candies, then Bob takes one of the other two piles. The last pile is split between Alice and Bob as they want: for example, it is possible that Alice takes the whole pile, and Bob gets nothing from it. After taking the candies from the piles, if Alice has more candies than Bob, she discards some candies so that the number of candies she has is equal to the number of candies Bob has. Of course, Bob does the same if he has more candies. Alice and Bob want to have as many candies as possible, and they plan the process of dividing candies accordingly. Please calculate the maximum number of candies Alice can have after this division process (of course, Bob will have the same number of candies). You have to answer $q$ independent queries. Let's see the following example: $[1, 3, 4]$. Then Alice can choose the third pile, Bob can take the second pile, and then the only candy from the first pile goes to Bob — then Alice has $4$ candies, and Bob has $4$ candies. Another example is $[1, 10, 100]$. Then Alice can choose the second pile, Bob can choose the first pile, and candies from the third pile can be divided in such a way that Bob takes $54$ candies, and Alice takes $46$ candies. Now Bob has $55$ candies, and Alice has $56$ candies, so she has to discard one candy — and after that, she has $55$ candies too. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 1000$) — the number of queries. Then $q$ queries follow. The only line of the query contains three integers $a, b$ and $c$ ($1 \le a, b, c \le 10^{16}$) — the number of candies in the first, second and third piles correspondingly. -----Output----- Print $q$ lines. The $i$-th line should contain the answer for the $i$-th query — the maximum number of candies Alice can have after the division, if both Alice and Bob act optimally (of course, Bob will have the same number of candies). -----Example----- Input 4 1 3 4 1 10 100 10000000000000000 10000000000000000 10000000000000000 23 34 45 Output 4 55 15000000000000000 51
{"inputs": ["1\n111 2 3\n", "1\n111 2 3\n", "1\n111 4 3\n", "1\n111 3 3\n", "1\n111 6 3\n", "1\n101 6 1\n", "1\n101 8 1\n", "1\n001 8 0\n"], "outputs": ["58\n", "58\n", "59\n", "58\n", "60\n", "54\n", "55\n", "4\n"]}
642
141
coding
Solve the programming task below in a Python markdown code block. The Tower of Hanoi problem involves 3 towers. A number of rings decreasing in size are placed on one tower. All rings must then be moved to another tower, but at no point can a larger ring be placed on a smaller ring. Your task: Given a number of rings, return the MINIMUM number of moves needed to move all the rings from one tower to another. Reference: Tower of Hanoi, Courtesy of Coolmath Games NB: This problem may seem very complex, but in reality there is an amazingly simple formula to calculate the minimum number. Just Learn how to solve the problem via the above link (if you are not familiar with it), and then think hard. Your solution should be in no way extraordinarily long and complex. The Kata ranking is for figuring out the solution, but the coding skills required are minimal. Also feel free to reuse/extend the following starter code: ```python def tower_of_hanoi(rings): ```
{"functional": "_inputs = [[4], [5], [10], [50]]\n_outputs = [[15], [31], [1023], [1125899906842623]]\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(tower_of_hanoi(*i), o[0])"}
213
197
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a 0-indexed integer array nums of length n and an integer target, return the number of pairs (i, j) where 0 <= i < j < n and nums[i] + nums[j] < target.   Please complete the following python code precisely: ```python class Solution: def countPairs(self, nums: List[int], target: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [-1,1,2,3,1], target = 2) == 3\n assert candidate(nums = [-6,2,5,-2,-7,-1,3], target = -2) == 10\n\n\ncheck(Solution().countPairs)"}
99
74
coding
Solve the programming task below in a Python markdown code block. Valery is very interested in magic. Magic attracts him so much that he sees it everywhere. He explains any strange and weird phenomenon through intervention of supernatural forces. But who would have thought that even in a regular array of numbers Valera manages to see something beautiful and magical. Valera absolutely accidentally got a piece of ancient parchment on which an array of numbers was written. He immediately thought that the numbers in this array were not random. As a result of extensive research Valera worked out a wonderful property that a magical array should have: an array is defined as magic if its minimum and maximum coincide. He decided to share this outstanding discovery with you, but he asks you for help in return. Despite the tremendous intelligence and wit, Valera counts very badly and so you will have to complete his work. All you have to do is count the number of magical subarrays of the original array of numbers, written on the parchment. Subarray is defined as non-empty sequence of consecutive elements. Input The first line of the input data contains an integer n (1 ≤ n ≤ 105). The second line contains an array of original integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). Output Print on the single line the answer to the problem: the amount of subarrays, which are magical. Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator). Examples Input 4 2 1 1 4 Output 5 Input 5 -2 -2 -2 0 1 Output 8 Note Notes to sample tests: Magical subarrays are shown with pairs of indices [a;b] of the beginning and the end. In the first sample: [1;1], [2;2], [3;3], [4;4], [2;3]. In the second sample: [1;1], [2;2], [3;3], [4;4], [5;5], [1;2], [2;3], [1;3].
{"inputs": ["1\n8\n", "1\n9\n", "1\n1\n", "1\n0\n", "1\n10\n", "1\n11\n", "2\n5 6\n", "2\n1 6\n"], "outputs": ["1\n", "1\n", "1\n", "1\n", "1", "1\n", "2", "2\n"]}
473
90
coding
Solve the programming task below in a Python markdown code block. You will be given a two-dimensional array with row consisting values 0 or 1. A move consists of choosing any column or row, and toggling all the 0’s as 1’s and 1’s as 0’s. After making the required moves, every row represents a binary number and the score of the matrix will be sum of all the numbers represented as binary numbers in each row. Find the highest possible score. $Example:$ Input: 0 0 1 1 1 0 1 0 1 1 0 0 Output: 39 Explanation: Toggled to 1 1 1 1 1 0 0 1 1 1 1 1 0b1111 + 0b1001 + 0b1111 = 15 + 9 + 15 = 39 -----Input:----- - First line will contains $m$, $n$ for the size of the 2-D array. - Contains $m$ lines of $n$ space-separated values each. -----Output:----- Single integer which is the maximum score obtained by the sum of binary numbers. -----Constraints----- - $1 \leq m, n \leq 20$ - $A[i][j] = 1$ or $0$ -----Sample Input:----- 3 4 0 0 1 1 1 0 1 0 1 1 0 0 -----Sample Output:----- 39
{"inputs": ["3 4\n0 0 1 1\n1 0 1 0\n1 1 0 0"], "outputs": ["39"]}
344
39
coding
Solve the programming task below in a Python markdown code block. You have N cards placed in front of you on the table. The i^{th} card has the number A_{i} written on it. In one move, you can remove any one card from the remaining cards on the table. Find the minimum number of moves required so that all the cards remaining on the table have the same number written on them. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the number of cards on the table. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} where A_{i} is the number written on the i^{th} card. ------ Output Format ------ For each test case, output the minimum number of moves required so that all the cards remaining on the table have the same number written on them. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤N ≤100$ $1 ≤A_{i} ≤10$ ----- Sample Input 1 ------ 3 5 1 1 2 2 3 4 8 8 8 8 6 5 6 7 8 9 10 ----- Sample Output 1 ------ 3 0 5 ----- explanation 1 ------ Test case $1$: The minimum number of moves required such that all remaining cards have same values is $3$: - Move $1$: Remove a card with number $1$. Remaining cards are $[1, 2, 2, 3]$. - Move $2$: Remove a card with number $1$. Remaining cards are $[2, 2, 3]$. - Move $3$: Remove a card with number $3$. Remaining cards are $[2, 2]$. Test case $2$: All cards have the same number initially. Thus, no moves are required. Test case $3$: The minimum number of moves required such that all remaining cards have same values is $5$: - Move $1$: Remove a card with number $5$. Remaining cards are $[6, 7, 8, 9, 10]$. - Move $2$: Remove a card with number $6$. Remaining cards are $[7, 8, 9, 10]$. - Move $3$: Remove a card with number $7$. Remaining cards are $[8, 9, 10]$. - Move $4$: Remove a card with number $8$. Remaining cards are $[9, 10]$. - Move $5$: Remove a card with number $9$. Remaining cards are $[10]$.
{"inputs": ["3\n5\n1 1 2 2 3\n4\n8 8 8 8\n6\n5 6 7 8 9 10\n"], "outputs": ["3\n0\n5\n"]}
602
55
coding
Solve the programming task below in a Python markdown code block. In a small but very proud high school it was decided to win ACM ICPC. This goal requires to compose as many teams of three as possible, but since there were only 6 students who wished to participate, the decision was to build exactly two teams. After practice competition, participant number i got a score of a_{i}. Team score is defined as sum of scores of its participants. High school management is interested if it's possible to build two teams with equal scores. Your task is to answer that question. -----Input----- The single line contains six integers a_1, ..., a_6 (0 ≤ a_{i} ≤ 1000) — scores of the participants -----Output----- Print "YES" (quotes for clarity), if it is possible to build teams with equal score, and "NO" otherwise. You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES"). -----Examples----- Input 1 3 2 1 2 1 Output YES Input 1 1 1 1 1 99 Output NO -----Note----- In the first sample, first team can be composed of 1st, 2nd and 6th participant, second — of 3rd, 4th and 5th: team scores are 1 + 3 + 1 = 2 + 1 + 2 = 5. In the second sample, score of participant number 6 is too high: his team score will be definitely greater.
{"inputs": ["1 3 2 1 2 1\n", "0 0 0 0 0 0\n", "4 4 4 4 5 4\n", "2 2 2 2 2 1\n", "1 1 1 1 1 5\n", "8 1 1 3 3 0\n", "1 1 2 2 3 3\n", "1 2 2 5 2 5\n"], "outputs": ["YES\n", "YES\n", "NO\n", "NO\n", "NO\n", "NO\n", "YES\n", "NO\n"]}
340
150
coding
Solve the programming task below in a Python markdown code block. Consider a table of size $n \times m$, initially fully white. Rows are numbered $1$ through $n$ from top to bottom, columns $1$ through $m$ from left to right. Some square inside the table with odd side length was painted black. Find the center of this square. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 115$) — the number of rows and the number of columns in the table. The $i$-th of the next $n$ lines contains a string of $m$ characters $s_{i1} s_{i2} \ldots s_{im}$ ($s_{ij}$ is 'W' for white cells and 'B' for black cells), describing the $i$-th row of the table. -----Output----- Output two integers $r$ and $c$ ($1 \le r \le n$, $1 \le c \le m$) separated by a space — the row and column numbers of the center of the black square. -----Examples----- Input 5 6 WWBBBW WWBBBW WWBBBW WWWWWW WWWWWW Output 2 4 Input 3 3 WWW BWW WWW Output 2 1
{"inputs": ["1 1\nB\n", "1 1\nB\n", "1 1\nB\n", "1 4\nWWBW\n", "1 4\nWWBW\n", "1 4\nWWWB\n", "1 4\nBWWW\n", "1 4\nWBWW\n"], "outputs": ["1 1\n", "1 1\n", "1 1\n", "1 3\n", "1 3\n", "1 4\n", "1 1\n", "1 2\n"]}
295
123
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.   Please complete the following python code precisely: ```python class Solution: def threeSumClosest(self, nums: List[int], target: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [-1,2,1,-4], target = 1) == 2\n assert candidate(nums = [0,0,0], target = 1) == 0\n\n\ncheck(Solution().threeSumClosest)"}
101
65
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an array of strings wordsDict and two strings that already exist in the array word1 and word2, return the shortest distance between the occurrence of these two words in the list. Note that word1 and word2 may be the same. It is guaranteed that they represent two individual words in the list.   Please complete the following python code precisely: ```python class Solution: def shortestWordDistance(self, wordsDict: List[str], word1: str, word2: str) -> int: ```
{"functional": "def check(candidate):\n assert candidate(wordsDict = [\"practice\", \"makes\", \"perfect\", \"coding\", \"makes\"], word1 = \"makes\", word2 = \"coding\") == 1\n assert candidate(wordsDict = [\"practice\", \"makes\", \"perfect\", \"coding\", \"makes\"], word1 = \"makes\", word2 = \"makes\") == 3\n\n\ncheck(Solution().shortestWordDistance)"}
122
99
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. There are n cars at given miles away from the starting mile 0, traveling to reach the mile target. You are given two integer array position and speed, both of length n, where position[i] is the starting mile of the ith car and speed[i] is the speed of the ith car in miles per hour. A car cannot pass another car, but it can catch up and then travel next to it at the speed of the slower car. A car fleet is a car or cars driving next to each other. The speed of the car fleet is the minimum speed of any car in the fleet. If a car catches up to a car fleet at the mile target, it will still be considered as part of the car fleet. Return the number of car fleets that will arrive at the destination.   Please complete the following python code precisely: ```python class Solution: def carFleet(self, target: int, position: List[int], speed: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]) == 3\n\n\ncheck(Solution().carFleet)"}
219
60
coding
Solve the programming task below in a Python markdown code block. The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2000$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2000$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2000$ ($\sum n \le 2000$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
{"inputs": ["1\n9 3\nRBGGGRBBGRRBBGGGGB\n", "1\n7 2\nBGGGGBBRRGBBRGGGBR\n", "1\n18 2\nRBGGGRBBGRRBBGGGGB\n", "1\n18 2\nRBGGGRBBGRRBBGGGGB\n", "1\n18 2\nBGGGGBBRRGBBRGGGBR\n", "1\n18 3\nRBGGGRBBGRRBBGGGGB\n", "1\n18 7\nBGGGGBBRRGBBRGGGBR\n", "1\n14 2\nBGGGGBBRRGBBRGGGBR\n"], "outputs": ["1\n", "0\n", "0\n", "0\n", "0\n", "1\n", "4\n", "0\n"]}
575
200
coding
Solve the programming task below in a Python markdown code block. During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables. Now she has a table filled with integers. The table consists of n rows and m columns. By a_{i}, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if a_{i}, j ≤ a_{i} + 1, j for all i from 1 to n - 1. Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j that a_{i}, j ≤ a_{i} + 1, j for all i from l to r - 1 inclusive. Alyona is too small to deal with this task and asks you to help! -----Input----- The first line of the input contains two positive integers n and m (1 ≤ n·m ≤ 100 000) — the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table. Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for a_{i}, j (1 ≤ a_{i}, j ≤ 10^9). The next line of the input contains an integer k (1 ≤ k ≤ 100 000) — the number of task that teacher gave to Alyona. The i-th of the next k lines contains two integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). -----Output----- Print "Yes" to the i-th line of the output if the table consisting of rows from l_{i} to r_{i} inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No". -----Example----- Input 5 4 1 2 3 5 3 1 3 2 4 5 2 3 5 5 3 2 4 4 3 4 6 1 1 2 5 4 5 3 5 1 3 1 5 Output Yes No Yes Yes Yes No -----Note----- In the sample, the whole table is not sorted in any column. However, rows 1–3 are sorted in column 1, while rows 4–5 are sorted in column 3.
{"inputs": ["1 1\n1\n1\n1 1\n", "1 1\n1\n1\n1 1\n", "1 1\n2\n1\n1 1\n", "5 4\n1 2 3 5\n3 1 3 2\n4 5 2 3\n5 5 3 2\n4 4 3 4\n6\n1 1\n2 5\n4 5\n3 5\n1 3\n1 5\n", "5 4\n1 2 3 5\n3 1 3 2\n4 5 2 3\n5 5 3 2\n4 4 4 4\n6\n1 1\n2 5\n4 5\n3 5\n1 3\n1 5\n", "5 4\n1 2 3 5\n3 1 3 2\n4 5 2 3\n4 5 4 2\n4 4 4 5\n6\n1 1\n2 5\n4 5\n5 5\n1 3\n1 5\n", "5 4\n1 2 3 5\n3 1 3 2\n4 5 2 2\n5 5 3 2\n4 4 3 4\n6\n1 1\n2 5\n4 5\n3 5\n1 3\n1 5\n", "5 4\n1 2 3 5\n3 1 3 2\n4 5 2 3\n5 5 4 2\n4 4 4 4\n6\n1 1\n2 5\n4 5\n3 5\n1 3\n1 5\n"], "outputs": ["Yes\n", "Yes\n", "Yes\n", "Yes\nNo\nYes\nYes\nYes\nNo\n", "Yes\nNo\nYes\nYes\nYes\nNo\n", "Yes\nYes\nYes\nYes\nYes\nYes\n", "Yes\nYes\nYes\nYes\nYes\nNo\n", "Yes\nNo\nYes\nYes\nYes\nNo\n"]}
607
490
coding
Solve the programming task below in a Python markdown code block. You've come to your favorite store Infinitesco to buy some ice tea. The store sells ice tea in bottles of different volumes at different costs. Specifically, a 0.25-liter bottle costs Q yen, a 0.5-liter bottle costs H yen, a 1-liter bottle costs S yen, and a 2-liter bottle costs D yen. The store has an infinite supply of bottles of each type. You want to buy exactly N liters of ice tea. How many yen do you have to spend? Constraints * 1 \leq Q, H, S, D \leq 10^8 * 1 \leq N \leq 10^9 * All input values are integers. Input Input is given from Standard Input in the following format: Q H S D N Output Print the smallest number of yen you have to spend to buy exactly N liters of ice tea. Examples Input 20 30 70 90 3 Output 150 Input 10000 1000 100 10 1 Output 100 Input 10 100 1000 10000 1 Output 40 Input 12345678 87654321 12345678 87654321 123456789 Output 1524157763907942
{"inputs": ["6 3 -1 0\n3", "5 39 -1 4\n1", "5 39 -2 4\n1", "5 39 -4 4\n1", "22 12 1 2\n2", "22 12 1 2\n3", "4 21 63 7\n10", "20 30 70 83\n3"], "outputs": ["-3\n", "-1\n", "-2\n", "-4\n", "2\n", "3\n", "35\n", "143\n"]}
352
144
coding
Solve the programming task below in a Python markdown code block. Valera has got n domino pieces in a row. Each piece consists of two halves — the upper one and the lower one. Each of the halves contains a number from 1 to 6. Valera loves even integers very much, so he wants the sum of the numbers on the upper halves and the sum of the numbers on the lower halves to be even. To do that, Valera can rotate the dominoes by 180 degrees. After the rotation the upper and the lower halves swap places. This action takes one second. Help Valera find out the minimum time he must spend rotating dominoes to make his wish come true. -----Input----- The first line contains integer n (1 ≤ n ≤ 100), denoting the number of dominoes Valera has. Next n lines contain two space-separated integers x_{i}, y_{i} (1 ≤ x_{i}, y_{i} ≤ 6). Number x_{i} is initially written on the upper half of the i-th domino, y_{i} is initially written on the lower half. -----Output----- Print a single number — the minimum required number of seconds. If Valera can't do the task in any time, print - 1. -----Examples----- Input 2 4 2 6 4 Output 0 Input 1 2 3 Output -1 Input 3 1 4 2 3 4 4 Output 1 -----Note----- In the first test case the sum of the numbers on the upper halves equals 10 and the sum of the numbers on the lower halves equals 6. Both numbers are even, so Valera doesn't required to do anything. In the second sample Valera has only one piece of domino. It is written 3 on the one of its halves, therefore one of the sums will always be odd. In the third case Valera can rotate the first piece, and after that the sum on the upper halves will be equal to 10, and the sum on the lower halves will be equal to 8.
{"inputs": ["1\n2 3\n", "1\n2 4\n", "1\n1 1\n", "1\n1 2\n", "1\n2 2\n", "1\n1 1\n", "1\n1 2\n", "1\n2 2\n"], "outputs": ["-1\n", "0\n", "-1\n", "-1\n", "0\n", "-1\n", "-1\n", "0\n"]}
454
103
coding
Solve the programming task below in a Python markdown code block. AtCoder Inc. has decided to lock the door of its office with a 3-digit PIN code. The company has an N-digit lucky number, S. Takahashi, the president, will erase N-3 digits from S and concatenate the remaining 3 digits without changing the order to set the PIN code. How many different PIN codes can he set this way? Both the lucky number and the PIN code may begin with a 0. Constraints * 4 \leq N \leq 30000 * S is a string of length N consisting of digits. Input Input is given from Standard Input in the following format: N S Output Print the number of different PIN codes Takahashi can set. Examples Input 4 0224 Output 3 Input 6 123123 Output 17 Input 19 3141592653589793238 Output 329
{"inputs": ["4\n0224", "6\n123123", "19\n5546189040285140044", "19\n4753841743850076999", "19\n9039528710159876563", "19\n3616863455241776042", "19\n2785343766571511696", "19\n2922045480066417189"], "outputs": ["3", "17", "273\n", "311\n", "412\n", "322\n", "239\n", "281\n"]}
227
211
coding
Solve the programming task below in a Python markdown code block. Mr. Anant Asankhya is the manager at the INFINITE hotel. The hotel has an infinite amount of rooms. One fine day, a finite number of tourists come to stay at the hotel. The tourists consist of: → A Captain. → An unknown group of families consisting of $\mbox{K}$ members per group where $\mbox{K}$ ≠ $\mbox{I}$. The Captain was given a separate room, and the rest were given one room per group. Mr. Anant has an unordered list of randomly arranged room entries. The list consists of the room numbers for all of the tourists. The room numbers will appear $\mbox{K}$ times per group except for the Captain's room. Mr. Anant needs you to help him find the Captain's room number. The total number of tourists or the total number of groups of families is not known to you. You only know the value of $\mbox{K}$ and the room number list. Input Format The first line consists of an integer, $\mbox{K}$, the size of each group. The second line contains the unordered elements of the room number list. Constraints $1<K<1000$ Output Format Output the Captain's room number. Sample Input 5 1 2 3 6 5 4 4 2 5 3 6 1 6 5 3 2 4 1 2 5 1 4 3 6 8 4 3 1 5 6 2 Sample Output 8 Explanation The list of room numbers contains $31$ elements. Since $\mbox{K}$ is $5$, there must be $\boldsymbol{6}$ groups of families. In the given list, all of the numbers repeat $5$ times except for room number $8$. Hence, $8$ is the Captain's room number.
{"inputs": ["5\n1 2 3 6 5 4 4 2 5 3 6 1 6 5 3 2 4 1 2 5 1 4 3 6 8 4 3 1 5 6 2 \n"], "outputs": ["8\n"]}
419
77
coding
Solve the programming task below in a Python markdown code block. Happy PMP is freshman and he is learning about algorithmic problems. He enjoys playing algorithmic games a lot. One of the seniors gave Happy PMP a nice game. He is given two permutations of numbers 1 through n and is asked to convert the first one to the second. In one move he can remove the last number from the permutation of numbers and inserts it back in an arbitrary position. He can either insert last number between any two consecutive numbers, or he can place it at the beginning of the permutation. Happy PMP has an algorithm that solves the problem. But it is not fast enough. He wants to know the minimum number of moves to convert the first permutation to the second. Input The first line contains a single integer n (1 ≤ n ≤ 2·105) — the quantity of the numbers in the both given permutations. Next line contains n space-separated integers — the first permutation. Each number between 1 to n will appear in the permutation exactly once. Next line describe the second permutation in the same format. Output Print a single integer denoting the minimum number of moves required to convert the first permutation to the second. Examples Input 3 3 2 1 1 2 3 Output 2 Input 5 1 2 3 4 5 1 5 2 3 4 Output 1 Input 5 1 5 2 3 4 1 2 3 4 5 Output 3 Note In the first sample, he removes number 1 from end of the list and places it at the beginning. After that he takes number 2 and places it between 1 and 3. In the second sample, he removes number 5 and inserts it after 1. In the third sample, the sequence of changes are like this: * 1 5 2 3 4 * 1 4 5 2 3 * 1 3 4 5 2 * 1 2 3 4 5 So he needs three moves.
{"inputs": ["1\n1\n1\n", "1\n2\n2\n", "1\n4\n4\n", "1\n0\n0\n", "1\n8\n8\n", "1\n9\n9\n", "1\n5\n5\n", "1\n11\n11\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
460
104
coding
Solve the programming task below in a Python markdown code block. B: Periodic Sequence- problem Dr. Period, a professor at H University, is studying a property called the cycle that is supposed to be hidden in all things. As a generally known basic cycle, a cycle hidden in a sequence may be considered. That is, if the sequence S = S_1, S_2, ..., S_N of length N satisfies the following properties, it has a period t (t \ ≤ N). For 1 \ ≤ i \ ≤ N − t, S_i = S_ {i + t}. Now, Dr. Period is paying attention to a sequence that can be described more simply using a period. For example, if a sequence of length N has a period t (\ ≤ N) and you can write N = kt using an integer k, then that sequence is a sequence of length t S_1, ..., S_t is k. It can be described that the pieces are continuous. When Dr. Period could describe a sequence as an example, he decided to say that the sequence was a k-part. Dr. Period is interested in the k-part with the largest k. So, as an assistant, you are tasked with writing a program that takes a sequence as input and outputs the largest k when it is a k-part. Create a program that exactly meets Dr. Period's demands. Input format N S_1 ... S_N The first row is given the integer N, which represents the length of the sequence. In the second row, the integer S_i (1 \ ≤ i \ ≤ N) representing each element of the sequence of length N is given, separated by blanks. Also, the inputs satisfy 1 \ ≤ N \ ≤ 200,000 and 1 \ ≤ S_i \ ≤ 100,000 (1 \ ≤ i \ ≤ N). Output format For a given sequence, output the maximum value of k when it is k-part in one row. Input example 1 6 1 2 3 1 2 3 Output example 1 2 Input example 2 12 1 2 1 2 1 2 1 2 1 2 1 2 Output example 2 6 Input example 3 6 1 2 3 4 5 6 Output example 3 1 Example Input 6 1 2 3 1 2 3 Output 2
{"inputs": ["6\n1 2 3 1 1 3", "6\n1 2 3 1 0 3", "6\n2 2 3 1 0 3", "6\n2 2 3 2 0 3", "6\n4 2 3 2 0 3", "6\n4 2 3 2 0 4", "6\n4 4 3 2 0 4", "6\n7 4 3 2 0 4"], "outputs": ["1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n"]}
530
158
coding
Solve the programming task below in a Python markdown code block. *** Nova polynomial from roots*** This kata is from a series on polynomial handling. ( [#1](http://www.codewars.com/kata/nova-polynomial-1-add-1) [#2](http://www.codewars.com/kata/570eb07e127ad107270005fe) [#3](http://www.codewars.com/kata/5714041e8807940ff3001140 ) [#4](http://www.codewars.com/kata/571a2e2df24bdfd4e20001f5)) Consider a polynomial in a list where each element in the list element corresponds to the factors. The factor order is the position in the list. The first element is the zero order factor (the constant). p = [a0, a1, a2, a3] signifies the polynomial a0 + a1x + a2x^2 + a3*x^3 In this kata create the polynomial from a list of roots: [r0, r1 ,r2, r3 ] p = (x-r0)(x-r1)(x-r2)(x-r3) note: no roots should return the identity polynomial. ```python poly_from_roots([4]) = [-4, 1] poly_from_roots([0, 0, 0, 0] ) = [0, 0, 0, 0, 1] poly_from_roots([]) = [1] ``` The first katas of this series is preloaded in the code and can be used: [poly_add](http://www.codewars.com/kata/570eb07e127ad107270005fe) [poly_multiply](http://www.codewars.com/kata/570eb07e127ad107270005fe) Also feel free to reuse/extend the following starter code: ```python def poly_from_roots(r): ```
{"functional": "_inputs = [[[4]], [[0, 0, 0, 0]], [[1, 1, 1, 1]], [[-1, 1, 2, -2]], [[]]]\n_outputs = [[[-4, 1]], [[0, 0, 0, 0, 1]], [[1, -4, 6, -4, 1]], [[4, 0, -5, 0, 1]], [[1]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(poly_from_roots(*i), o[0])"}
468
247
coding
Solve the programming task below in a Python markdown code block. Vasya has found a piece of paper with a coordinate system written on it. There are n distinct squares drawn in this coordinate system. Let's number the squares with integers from 1 to n. It turned out that points with coordinates (0, 0) and (ai, ai) are the opposite corners of the i-th square. Vasya wants to find such integer point (with integer coordinates) of the plane, that belongs to exactly k drawn squares. We'll say that a point belongs to a square, if the point is located either inside the square, or on its boundary. Help Vasya find a point that would meet the described limits. Input The first line contains two space-separated integers n, k (1 ≤ n, k ≤ 50). The second line contains space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109). It is guaranteed that all given squares are distinct. Output In a single line print two space-separated integers x and y (0 ≤ x, y ≤ 109) — the coordinates of the point that belongs to exactly k squares. If there are multiple answers, you are allowed to print any of them. If there is no answer, print "-1" (without the quotes). Examples Input 4 3 5 1 3 4 Output 2 1 Input 3 1 2 4 1 Output 4 0 Input 4 50 5 1 10 2 Output -1
{"inputs": ["1 1\n1\n", "1 1\n2\n", "1 2\n10\n", "2 1\n1 2\n", "2 1\n2 1\n", "2 1\n2 3\n", "2 2\n7 2\n", "2 1\n0 1\n"], "outputs": ["1 1\n", "2 2\n", "-1\n", "2 2\n", "2 2\n", "3 3\n", "2 2\n", "1 1\n"]}
339
127
coding
Solve the programming task below in a Python markdown code block. You are given three integers $n$, $l$, and $r$. You need to construct an array $a_1,a_2,\dots,a_n$ ($l\le a_i\le r$) such that $\gcd(i,a_i)$ are all distinct or report there's no solution. Here $\gcd(x, y)$ denotes the greatest common divisor (GCD) of integers $x$ and $y$. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 10^4$) — the number of test cases. The description of the test cases follows. The first line contains three integers $n$, $l$, $r$ ($1 \le n \le 10^5$, $1\le l\le r\le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case, if there is no solution, print "NO" (without quotes). You can print letters in any case (upper or lower). Otherwise, print "YES" (without quotes). In the next line, print $n$ integers $a_1,a_2,\ldots,a_n$ — the array you construct. If there are multiple solutions, you may output any. -----Examples----- Input 4 5 1 5 9 1000 2000 10 30 35 1 1000000000 1000000000 Output YES 1 2 3 4 5 YES 1145 1926 1440 1220 1230 1350 1001 1000 1233 NO YES 1000000000 -----Note----- In the first test case, $\gcd(1,a_1),\gcd(2,a_2),\ldots,\gcd(5,a_5)$ are equal to $1$, $2$, $3$, $4$, $5$, respectively.
{"inputs": ["1\n100000 1 99999\n", "1\n100000 50021 100000\n", "4\n5 1 5\n9 1000 2000\n10 30 35\n1 1000000000 1000000000\n"], "outputs": ["NO\n", "NO\n", "YES\n1 2 3 4 5\nYES\n1000 1000 1002 1000 1000 1002 1001 1000 1008\nNO\nYES\n1000000000\n"]}
494
188
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an array of integers nums, calculate the pivot index of this array. The pivot index is the index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the index's right. If the index is on the left edge of the array, then the left sum is 0 because there are no elements to the left. This also applies to the right edge of the array. Return the leftmost pivot index. If no such index exists, return -1.   Please complete the following python code precisely: ```python class Solution: def pivotIndex(self, nums: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [1, 7, 3, 6, 5, 6]) == 3\n assert candidate(nums = [1, 2, 3]) == -1\n assert candidate(nums = [2, 1, -1]) == 0\n\n\ncheck(Solution().pivotIndex)"}
155
82
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given 3 positives numbers a, b and c. Return the minimum flips required in some bits of a and b to make ( a OR b == c ). (bitwise OR operation). Flip operation consists of change any single bit 1 to 0 or change the bit 0 to 1 in their binary representation.   Please complete the following python code precisely: ```python class Solution: def minFlips(self, a: int, b: int, c: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(a = 2, b = 6, c = 5) == 3\n assert candidate(a = 4, b = 2, c = 7) == 1\n assert candidate(a = 1, b = 2, c = 3) == 0\n\n\ncheck(Solution().minFlips)"}
128
86
coding
Solve the programming task below in a Python markdown code block. Lucy loves to travel. Luckily she is a renowned computer scientist and gets to travel to international conferences using her department's budget. Each year, Society for Exciting Computer Science Research (SECSR) organizes several conferences around the world. Lucy always picks one conference from that list that is hosted in a city she hasn't been to before, and if that leaves her with more than one option, she picks the conference that she thinks would be most relevant for her field of research. Write a function `conferencePicker` that takes in two arguments: - `citiesVisited`, a list of cities that Lucy has visited before, given as an array of strings. - `citiesOffered`, a list of cities that will host SECSR conferences this year, given as an array of strings. `citiesOffered` will already be ordered in terms of the relevance of the conferences for Lucy's research (from the most to the least relevant). The function should return the city that Lucy should visit, as a string. Also note: - You should allow for the possibility that Lucy hasn't visited any city before. - SECSR organizes at least two conferences each year. - If all of the offered conferences are hosted in cities that Lucy has visited before, the function should return `'No worthwhile conferences this year!'` (`Nothing` in Haskell) Example: Also feel free to reuse/extend the following starter code: ```python def conference_picker(cities_visited, cities_offered): ```
{"functional": "_inputs = [[[], ['Philadelphia', 'Osaka', 'Tokyo', 'Melbourne']], [[], ['Brussels', 'Madrid', 'London']], [[], ['Sydney', 'Tokyo']], [['London', 'Berlin', 'Mexico City', 'Melbourne', 'Buenos Aires', 'Hong Kong', 'Madrid', 'Paris'], ['Berlin', 'Melbourne']], [['Beijing', 'Johannesburg', 'Sydney', 'Philadelphia', 'Hong Kong', 'Stockholm', 'Chicago', 'Seoul', 'Mexico City', 'Berlin'], ['Stockholm', 'Berlin', 'Chicago']], [['Rome'], ['Rome']], [['Milan'], ['London']], [['Mexico City', 'Dubai', 'Philadelphia', 'Madrid', 'Houston', 'Chicago', 'Delhi', 'Seoul', 'Mumbai', 'Lisbon', 'Hong Kong', 'Brisbane', 'Stockholm', 'Tokyo', 'San Francisco', 'Rio De Janeiro'], ['Lisbon', 'Mexico City']], [['Gatlantis', \"Baldur's Gate\", 'Gotham City', 'Mystara', 'Washinkyo', 'Central City'], ['Mystara', 'Gatlantis', 'MegaTokyo', 'Genosha', 'Central City', 'Washinkyo', 'Gotham City', \"King's Landing\", 'Waterdeep']], [['Thay', 'Camelot'], ['Waterdeep', 'Washinkyo']]]\n_outputs = [['Philadelphia'], ['Brussels'], ['Sydney'], ['No worthwhile conferences this year!'], ['No worthwhile conferences this year!'], ['No worthwhile conferences this year!'], ['London'], ['No worthwhile conferences this year!'], ['MegaTokyo'], ['Waterdeep']]\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(conference_picker(*i), o[0])"}
311
519
coding
Solve the programming task below in a Python markdown code block. Alice likes all the numbers which are divisible by A. Bob does not like the numbers which are divisible by B and likes all the remaining numbers. Determine the smallest number greater than or equal to N which is liked by both Alice and Bob. Output -1 if no such number exists. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first and only line of each test case contains three space-separated integers A, B and N — the parameters mentioned in the problem statment. ------ Output Format ------ For each test case, output the smallest number ≥ N which is divisible by A and is not divisible by B. Output -1 if no such number exists. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ A, B, N ≤ 10^{9}$ ----- Sample Input 1 ------ 3 5 2 11 4 3 24 7 7 100 ----- Sample Output 1 ------ 15 28 -1 ----- explanation 1 ------ Test case $1$: $15$ is the smallest number $≥$ $11$ which is divisible by $5$ and is not divisible by $2$. Test case $2$: $28$ is the smallest number $≥$ $24$ which is divisible by $4$ and is not divisible by $3$. Test case $3$: There does not exist any number which is divisible by $A = 7$ and is not divisible by $B = 7$.
{"inputs": ["3\n5 2 11\n4 3 24\n7 7 100\n"], "outputs": ["15\n28\n-1\n"]}
352
43
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an array arr of positive integers sorted in a strictly increasing order, and an integer k. Return the kth positive integer that is missing from this array.   Please complete the following python code precisely: ```python class Solution: def findKthPositive(self, arr: List[int], k: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(arr = [2,3,4,7,11], k = 5) == 9\n assert candidate(arr = [1,2,3,4], k = 2) == 6\n\n\ncheck(Solution().findKthPositive)"}
86
70
coding
Solve the programming task below in a Python markdown code block. Dolphin is planning to generate a small amount of a certain chemical substance C. In order to generate the substance C, he must prepare a solution which is a mixture of two substances A and B in the ratio of M_a:M_b. He does not have any stock of chemicals, however, so he will purchase some chemicals at a local pharmacy. The pharmacy sells N kinds of chemicals. For each kind of chemical, there is exactly one package of that chemical in stock. The package of chemical i contains a_i grams of the substance A and b_i grams of the substance B, and is sold for c_i yen (the currency of Japan). Dolphin will purchase some of these packages. For some reason, he must use all contents of the purchased packages to generate the substance C. Find the minimum amount of money required to generate the substance C. If it is not possible to generate the substance C by purchasing any combination of packages at the pharmacy, report that fact. -----Constraints----- - 1≦N≦40 - 1≦a_i,b_i≦10 - 1≦c_i≦100 - 1≦M_a,M_b≦10 - gcd(M_a,M_b)=1 - a_i, b_i, c_i, M_a and M_b are integers. -----Input----- The input is given from Standard Input in the following format: N M_a M_b a_1 b_1 c_1 a_2 b_2 c_2 : a_N b_N c_N -----Output----- Print the minimum amount of money required to generate the substance C. If it is not possible to generate the substance C, print -1 instead. -----Sample Input----- 3 1 1 1 2 1 2 1 2 3 3 10 -----Sample Output----- 3 The amount of money spent will be minimized by purchasing the packages of chemicals 1 and 2. In this case, the mixture of the purchased chemicals will contain 3 grams of the substance A and 3 grams of the substance B, which are in the desired ratio: 3:3=1:1. The total price of these packages is 3 yen.
{"inputs": ["0 4 6\n10 2 5", "1 -1 4\n10 3 5", "1 0 20\n8 9 10", "1 -1 4\n10 3 6", "0 1 25\n5 0 10", "0 1 25\n5 0 16", "0 2 25\n5 0 10", "0 4 10\n10 2 5"], "outputs": ["-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n"]}
477
156
coding
Solve the programming task below in a Python markdown code block. There are N bikes and M cars on the road. Each bike has 2 tyres. Each car has 4 tyres. Find the total number of tyres on the road. ------ Input Format ------ - The first line will contain T - the number of test cases. Then the test cases follow. - The first and only line of each test case contains two integers N, M. ------ Output Format ------ For each test case, output in a single line, the total number of tyres on the road. ------ Constraints ------ $1 ≤ T ≤ 1000$ $0 ≤ N, M ≤ 100$ ----- Sample Input 1 ------ 2 2 1 3 0 ----- Sample Output 1 ------ 8 6 ----- explanation 1 ------ Test Case $1$: There are $2$ bikes and $1$ car. Each bike has $2$ tyres, so there are $2\cdot 2 = 4$ bike tyres. Similarly, each car has $4$ tyres, so there are $1\cdot 4 = 4$ car tyres. Adding the tyres of all vehicles, we get $4+4=8$ tyres in total. Test Case $2$: There are $3$ bikes and $0$ cars. Each bike has $2$ tyres, so there are $3\cdot 2 = 6$ bike tyres. There are no cars, so there are $0\cdot 4 = 0$ car tyres. Adding the tyres of all vehicles, we get $6+0=6$ tyres in total.
{"inputs": ["2\n2 1\n3 0\n"], "outputs": ["8\n6\n"]}
346
24
coding
Solve the programming task below in a Python markdown code block. In this kata you need to create a function that takes a 2D array/list of non-negative integer pairs and returns the sum of all the "saving" that you can have getting the [LCM](https://en.wikipedia.org/wiki/Least_common_multiple) of each couple of number compared to their simple product. For example, if you are given: ``` [[15,18], [4,5], [12,60]] ``` Their product would be: ``` [270, 20, 720] ``` While their respective LCM would be: ``` [90, 20, 60] ``` Thus the result should be: ``` (270-90)+(20-20)+(720-60)==840 ``` This is a kata that I made, among other things, to let some of my trainees familiarize with the [euclidean algorithm](https://en.wikipedia.org/wiki/Euclidean_algorithm), a really neat tool to have on your belt ;) Also feel free to reuse/extend the following starter code: ```python def sum_differences_between_products_and_LCMs(pairs): ```
{"functional": "_inputs = [[[[15, 18], [4, 5], [12, 60]]], [[[1, 1], [0, 0], [13, 91]]], [[[15, 7], [4, 5], [19, 60]]], [[[20, 50], [10, 10], [50, 20]]], [[]]]\n_outputs = [[840], [1092], [0], [1890], [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(sum_differences_between_products_and_LCMs(*i), o[0])"}
269
272
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an integer array nums, your goal is to make all elements in nums equal. To complete one operation, follow these steps: Find the largest value in nums. Let its index be i (0-indexed) and its value be largest. If there are multiple elements with the largest value, pick the smallest i. Find the next largest value in nums strictly smaller than largest. Let its value be nextLargest. Reduce nums[i] to nextLargest. Return the number of operations to make all elements in nums equal.   Please complete the following python code precisely: ```python class Solution: def reductionOperations(self, nums: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [5,1,3]) == 3\n assert candidate(nums = [1,1,1]) == 0\n assert candidate(nums = [1,1,2,2,3]) == 4\n\n\ncheck(Solution().reductionOperations)"}
154
72
coding
Solve the programming task below in a Python markdown code block. You are given a rooted tree, consisting of $n$ vertices. The vertices are numbered from $1$ to $n$, the root is the vertex $1$. You can perform the following operation at most $k$ times: choose an edge $(v, u)$ of the tree such that $v$ is a parent of $u$; remove the edge $(v, u)$; add an edge $(1, u)$ (i. e. make $u$ with its subtree a child of the root). The height of a tree is the maximum depth of its vertices, and the depth of a vertex is the number of edges on the path from the root to it. For example, the depth of vertex $1$ is $0$, since it's the root, and the depth of all its children is $1$. What's the smallest height of the tree that can be achieved? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of testcases. The first line of each testcase contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $0 \le k \le n - 1$) — the number of vertices in the tree and the maximum number of operations you can perform. The second line contains $n-1$ integers $p_2, p_3, \dots, p_n$ ($1 \le p_i < i$) — the parent of the $i$-th vertex. Vertex $1$ is the root. The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print a single integer — the smallest height of the tree that can achieved by performing at most $k$ operations. -----Examples----- Input 5 5 1 1 1 2 2 5 2 1 1 2 2 6 0 1 2 3 4 5 6 1 1 2 3 4 5 4 3 1 1 1 Output 2 1 5 3 1 -----Note----- None
{"inputs": ["5\n5 1\n1 1 2 2\n5 2\n1 1 2 2\n6 0\n1 2 3 4 5\n6 1\n1 2 3 4 5\n4 3\n1 1 1\n"], "outputs": ["2\n1\n5\n3\n1\n"]}
491
84
coding
Solve the programming task below in a Python markdown code block. Chef likes to solve difficult tasks. This time, he tried to solve the Big Famous Unsolvable $A+B=C$. One of his friends played a prank on Chef and randomly shuffled the bits in $A$ and $B$ (independently in each number). However, the funny thing is that the sum of the resulting numbers remained $C$ even after shuffling. Chef is now wondering: in how many ways is it possible to shuffle the bits of $A$ and the bits of $B$ such that their sum after shuffling is equal to $C$? Let's denote the integers obtained by shuffling the bits of $A$ and $B$ by $A_s$ and $B_s$ respectively; two ways $(A_{s1}, B_{s1})$ and $(A_{s2}, B_{s2})$ are considered distinct if $A_{s1} \neq A_{s2}$ or $B_{s1} \neq B_{s2}$. It is allowed to add any number (possibly zero) of leading zeroes, i.e. bits $0$, to $A$ and any number of leading zeroes to $B$ before shuffling the bits. -----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 three space-separated integers $A$, $B$ and $C$. -----Output----- For each test case, print a single line containing one integer — the number of ways to shuffle the bits. -----Constraints----- - $1 \le T \le 1,000$ - $1 \le A, B, C \le 10^9$ - $A+B = C$ -----Subtasks----- Subtask #1 (50 points): $1 \le A, B, C \le 10^5$ Subtask #2 (50 points): original constraints -----Example Input----- 2 1 2 3 369 428 797 -----Example Output----- 2 56 -----Explanation----- Example case 1: We can consider $A=01$ and $B=10$ in binary. Then, there are two possible ways: swapping the two bits of $A$ and the two bits of $B$ ($A_s=10$, $B_s=01$ in binary, $2$ and $1$ in decimal representation) or not shuffling any bits.
{"inputs": ["2\n1 2 3\n369 428 797"], "outputs": ["2\n56"]}
555
33
coding
Solve the programming task below in a Python markdown code block. Sunuke-kun's dictionary contains the words s1, ..., sn, which consist of n lowercase letters. This satisfies s1 <... <sn when compared in lexicographical order. Unfortunately, some characters are faint and unreadable. Unreadable characters are represented by?. Find out how many ways to restore the dictionary by replacing? With lowercase letters, even with mod 1,000,000,007. Constraints * 1 ≤ n ≤ 50 * 1 ≤ | si | ≤ 20 * The characters that appear in si are lowercase letters or? Input n s1 .. .. sn Output Print the answer on one line. Examples Input 2 ?sum??mer c??a??mp Output 703286064 Input 3 snuje ????e snule Output 1
{"inputs": ["3\nsnuje\n????e\neluns", "3\nsunje\n????e\neluns", "3\nsunje\n@???e\neluns", "3\nsunje\n@???e\nsnule", "3\njunse\n@???e\nsnule", "3\nesnuj\n@???e\nsnule", "3\nesnuj\n@?e??\nsnule", "3\nesnuj\n@?f??\nsnule"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
205
148
coding
Solve the programming task below in a Python markdown code block. Ternary numeric notation is quite popular in Berland. To telegraph the ternary number the Borze alphabet is used. Digit 0 is transmitted as «.», 1 as «-.» and 2 as «--». You are to decode the Borze code, i.e. to find out the ternary number given its representation in Borze alphabet. Input The first line contains a number in Borze code. The length of the string is between 1 and 200 characters. It's guaranteed that the given string is a valid Borze code of some ternary number (this number can have leading zeroes). Output Output the decoded ternary number. It can have leading zeroes. Examples Input .-.-- Output 012 Input --. Output 20 Input -..-.-- Output 1012
{"inputs": [".\n", "-.\n", "--\n", "..\n", ".--\n", "--.\n", ".--.\n", ".-..\n"], "outputs": ["0\n", "1\n", "2\n", "00\n", "02\n", "20\n", "020\n", "010\n"]}
193
81
coding
Solve the programming task below in a Python markdown code block. This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) — the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
{"inputs": ["4 5\nabcd\n", "4 11\nddcd\n", "4 11\nzzaz\n", "5 10\ndcdcd\n", "5 10\ncbcbc\n", "5 10\nbbabb\n", "8 16\ndbcadabc\n", "8 20\nccaccaca\n"], "outputs": ["aaaaa\n", "ddcddcddcdd\n", "zzazzazzazz\n", "dcdcdcdcdc\n", "cbcbcbcbcb\n", "bbabbabbab\n", "dbcadabcdbcadabc\n", "ccaccacaccaccacaccac\n"]}
486
155
coding
Solve the programming task below in a Python markdown code block. You are given a sorted array $a_1, a_2, \dots, a_n$ (for each index $i > 1$ condition $a_i \ge a_{i-1}$ holds) and an integer $k$. You are asked to divide this array into $k$ non-empty consecutive subarrays. Every element in the array should be included in exactly one subarray. Let $max(i)$ be equal to the maximum in the $i$-th subarray, and $min(i)$ be equal to the minimum in the $i$-th subarray. The cost of division is equal to $\sum\limits_{i=1}^{k} (max(i) - min(i))$. For example, if $a = [2, 4, 5, 5, 8, 11, 19]$ and we divide it into $3$ subarrays in the following way: $[2, 4], [5, 5], [8, 11, 19]$, then the cost of division is equal to $(4 - 2) + (5 - 5) + (19 - 8) = 13$. Calculate the minimum cost you can obtain by dividing the array $a$ into $k$ non-empty consecutive subarrays. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 3 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($ 1 \le a_i \le 10^9$, $a_i \ge a_{i-1}$). -----Output----- Print the minimum cost you can obtain by dividing the array $a$ into $k$ nonempty consecutive subarrays. -----Examples----- Input 6 3 4 8 15 16 23 42 Output 12 Input 4 4 1 3 3 7 Output 0 Input 8 1 1 1 2 3 5 8 13 21 Output 20 -----Note----- In the first test we can divide array $a$ in the following way: $[4, 8, 15, 16], [23], [42]$.
{"inputs": ["1 1\n1\n", "1 1\n4\n", "1 1\n2\n", "1 1\n1\n", "1 1\n4\n", "1 1\n2\n", "1 1\n0\n", "1 1\n20\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
531
103
coding
Solve the programming task below in a Python markdown code block. The chef was playing with numbers and he found that natural number N can be obtained by sum various unique natural numbers, For challenging himself chef wrote one problem statement, which he decided to solve in future. Problem statement: N can be obtained as the sum of Kth power of integers in multiple ways, find total number ways? After that Cheffina came and read what chef wrote in the problem statement, for having some fun Cheffina made some changes in the problem statement as. New problem statement: N can be obtained as the sum of Kth power of unique +ve integers in multiple ways, find total number ways? But, the chef is now confused, how to solve a new problem statement, help the chef to solve this new problem statement. -----Input:----- - First-line will contain $T$, the number of test cases. Then the test cases follow. - Each test case contains a single line of input, two integers $N, K$. -----Output:----- For each test case, output in a single line answer to the problem statement. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 1000$ - $1 \leq K \leq 6$ -----Sample Input:----- 2 4 1 38 2 -----Sample Output:----- 2 1 -----EXPLANATION:----- For 1) 4 can be obtained by as [ 4^1 ], [1^1, 3^1], [2^1, 2^1]. (here ^ stands for power) But here [2^1, 2^1] is not the valid way because it is not made up of unique +ve integers. For 2) 38 can be obtained in the way which is [2^2, 3^2, 5^2] = 4 + 9 + 25
{"inputs": ["2\n4 1\n38 2"], "outputs": ["2\n1"]}
420
23
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You have prepared four problems. The difficulty levels of the problems are A_{1}, A_{2}, A_{3}, A_{4} respectively. A problem set comprises at least two problems and no two problems in a problem set should have the same difficulty level. A problem can belong to at most one problem set. Find the maximum number of problem sets you can create using the four problems. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first and only line of each test case contains four space-separated integers A_{1}, A_{2}, A_{3}, A_{4}, denoting the difficulty level of four problems. ------ Output Format ------ For each test case, print a single line containing one integer - the maximum number of problem sets you can create using the four problems. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ A_{1}, A_{2}, A_{3}, A_{4} ≤ 10$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 3 1 4 3 2 4 5 5 5 2 2 2 2 ----- Sample Output 1 ------ 2 1 0 ----- explanation 1 ------ Test case $1$: You can prepare the first problem set using the first two problems and the second problem set using the next two problems. So the problem sets will be $[1, 4]$ and $[3, 2]$. Test case $2$: You can prepare one problem set using one problem having a difficulty level of $4$ and the other having a difficulty level of $5$. There is no way to prepare more than one problem set. Test case $3$: There is no way to prepare a problem set.
{"inputs": ["3\n1 4 3 2\n4 5 5 5\n2 2 2 2"], "outputs": ["2\n1\n0"]}
445
40
coding
Solve the programming task below in a Python markdown code block. We have N switches with "on" and "off" state, and M bulbs. The switches are numbered 1 to N, and the bulbs are numbered 1 to M. Bulb i is connected to k_i switches: Switch s_{i1}, s_{i2}, ..., and s_{ik_i}. It is lighted when the number of switches that are "on" among these switches is congruent to p_i modulo 2. How many combinations of "on" and "off" states of the switches light all the bulbs? -----Constraints----- - 1 \leq N, M \leq 10 - 1 \leq k_i \leq N - 1 \leq s_{ij} \leq N - s_{ia} \neq s_{ib} (a \neq b) - p_i is 0 or 1. - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: N M k_1 s_{11} s_{12} ... s_{1k_1} : k_M s_{M1} s_{M2} ... s_{Mk_M} p_1 p_2 ... p_M -----Output----- Print the number of combinations of "on" and "off" states of the switches that light all the bulbs. -----Sample Input----- 2 2 2 1 2 1 2 0 1 -----Sample Output----- 1 - Bulb 1 is lighted when there is an even number of switches that are "on" among the following: Switch 1 and 2. - Bulb 2 is lighted when there is an odd number of switches that are "on" among the following: Switch 2. There are four possible combinations of states of (Switch 1, Switch 2): (on, on), (on, off), (off, on) and (off, off). Among them, only (on, on) lights all the bulbs, so we should print 1.
{"inputs": ["10 1\n3 7 3 6\n0\n", "10 1\n4 3 5 2 4\n1\n", "2 2\n2 1 2\n1 2\n0 0", "4 2\n2 1 2\n1 2\n0 1", "4 0\n1 1 2\n1 2\n0 1", "7 2\n2 1 2\n1 1\n0 1", "4 2\n1 1 2\n1 2\n0 1", "4 2\n2 1 2\n1 1\n0 1"], "outputs": ["512\n", "512\n", "1\n", "4\n", "16\n", "32\n", "4\n", "4\n"]}
452
194
coding
Solve the programming task below in a Python markdown code block. Consider a pyramid made up of blocks. Each layer of the pyramid is a rectangle of blocks, and the dimensions of these rectangles increment as you descend the pyramid. So, if a layer is a `3x6` rectangle of blocks, then the next layer will be a `4x7` rectangle of blocks. A `1x10` layer will be on top of a `2x11` layer on top of a `3x12` layer, and so on. ## Task Given the dimensions of a pyramid's topmost layer `w,l`, and its height `h` (aka the number of layers), return the total number of blocks in the pyramid. ## Examples `num_blocks(1, 1, 2)` will return `5`. This pyramid starts with a `1x1` layer and has 2 layers total. So, there is 1 block in the first layer, and `2x2=4` blocks in the second. Thus, 5 is the total number of blocks. `num_blocks(2, 4, 3)` will return `47`. This pyramid has 3 layers: `2x4`, `3x5`, and `4x6`. So, there are `47` blocks total. ## Notes All parameters will always be postive nonzero integers. Efficiency is important. There will be: * 100 'small' cases with `w`, `l`, and `h` below `20`. * 100 'big' cases with `w`, `l`, and `h` between `1e9` and `1e10`. Also feel free to reuse/extend the following starter code: ```python def num_blocks(w, l, h): ```
{"functional": "_inputs = [[1, 1, 2], [2, 4, 3], [1, 10, 10], [20, 30, 40]]\n_outputs = [[5], [47], [880], [83540]]\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(num_blocks(*i), o[0])"}
385
208
coding
Solve the programming task below in a Python markdown code block. Recall that string $a$ is a subsequence of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly zero or all) characters. For example, for the string $a$="wowwo", the following strings are subsequences: "wowwo", "wowo", "oo", "wow", "", and others, but the following are not subsequences: "owoo", "owwwo", "ooo". The wow factor of a string is the number of its subsequences equal to the word "wow". Bob wants to write a string that has a large wow factor. However, the "w" key on his keyboard is broken, so he types two "v"s instead. Little did he realise that he may have introduced more "w"s than he thought. Consider for instance the string "ww". Bob would type it as "vvvv", but this string actually contains three occurrences of "w": "vvvv" "vvvv" "vvvv" For example, the wow factor of the word "vvvovvv" equals to four because there are four wows: "vvvovvv" "vvvovvv" "vvvovvv" "vvvovvv" Note that the subsequence "vvvovvv" does not count towards the wow factor, as the "v"s have to be consecutive. For a given string $s$, compute and output its wow factor. Note that it is not guaranteed that it is possible to get $s$ from another string replacing "w" with "vv". For example, $s$ can be equal to "vov". -----Input----- The input contains a single non-empty string $s$, consisting only of characters "v" and "o". The length of $s$ is at most $10^6$. -----Output----- Output a single integer, the wow factor of $s$. -----Examples----- Input vvvovvv Output 4 Input vvovooovovvovoovoovvvvovovvvov Output 100 -----Note----- The first example is explained in the legend.
{"inputs": ["v\n", "o\n", "o\n", "v\n", "n\n", "u\n", "p\n", "t\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
473
70
coding
Solve the programming task below in a Python markdown code block. Tonight, Chef would like to hold a party for his $N$ friends. All friends are invited and they arrive at the party one by one in an arbitrary order. However, they have certain conditions — for each valid $i$, when the $i$-th friend arrives at the party and sees that at that point, strictly less than $A_i$ other people (excluding Chef) have joined the party, this friend leaves the party; otherwise, this friend joins the party. Help Chef estimate how successful the party can be — find the maximum number of his friends who could join the party (for an optimal choice of the order of arrivals). -----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 number of Chef's friends who could join the party. -----Constraints----- - $1 \le T \le 1,000$ - $1 \le N \le 10^5$ - the sum of $N$ over all test cases does not exceed $10^6$ -----Example Input----- 3 2 0 0 6 3 1 0 0 5 5 3 1 2 3 -----Example Output----- 2 4 0 -----Explanation----- Example case 1: Chef has two friends. Both of them do not require anyone else to be at the party before they join, so they will both definitely join the party. Example case 2: At the beginning, friends $3$ and $4$ can arrive and join the party, since they do not require anyone else to be at the party before they join. After that, friend $2$ can arrive; this friend would see that there are two people at the party and therefore also join. Then, friend $1$ will also join, so in the end, there would be $4$ people attending the party. Example case 3: No one will attend the party because each of Chef's friends will find zero people at the party and leave, regardless of the order in which they arrive.
{"inputs": ["3\n2\n0 0\n6\n3 1 0 0 5 5\n3\n1 2 3"], "outputs": ["2\n4\n0"]}
516
44
coding
Solve the programming task below in a Python markdown code block. Well, here is another math class task. In mathematics, GCD is the greatest common divisor, and it's an easy task to calculate the GCD between two positive integers. A common divisor for two positive numbers is a number which both numbers are divisible by. But your teacher wants to give you a harder task, in this task you have to find the greatest common divisor d between two integers a and b that is in a given range from low to high (inclusive), i.e. low ≤ d ≤ high. It is possible that there is no common divisor in the given range. You will be given the two integers a and b, then n queries. Each query is a range from low to high and you have to answer each query. Input The first line contains two integers a and b, the two integers as described above (1 ≤ a, b ≤ 109). The second line contains one integer n, the number of queries (1 ≤ n ≤ 104). Then n lines follow, each line contains one query consisting of two integers, low and high (1 ≤ low ≤ high ≤ 109). Output Print n lines. The i-th of them should contain the result of the i-th query in the input. If there is no common divisor in the given range for any query, you should print -1 as a result for this query. Examples Input 9 27 3 1 5 10 11 9 11 Output 3 -1 9
{"inputs": ["84 36\n1\n18 32\n", "48 4\n2\n8 100\n9 59\n", "48 4\n2\n8 100\n8 59\n", "48 72\n2\n8 29\n29 37\n", "48 72\n2\n8 29\n15 37\n", "48 72\n2\n8 58\n15 37\n", "48 72\n2\n8 98\n15 37\n", "48 72\n2\n8 192\n9 37\n"], "outputs": ["-1\n", "-1\n-1\n", "-1\n-1\n", "24\n-1\n", "24\n24\n", "24\n24\n", "24\n24\n", "24\n24\n"]}
329
226
coding
Solve the programming task below in a Python markdown code block. Takahashi has N cards. The i-th of these cards has an integer A_i written on it. Takahashi will choose an integer K, and then repeat the following operation some number of times: - Choose exactly K cards such that the integers written on them are all different, and eat those cards. (The eaten cards disappear.) For each K = 1,2, \ldots, N, find the maximum number of times Takahashi can do the operation. -----Constraints----- - 1 \le N \le 3 \times 10^5 - 1 \le A_i \le N - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: N A_1 A_2 \ldots A_N -----Output----- Print N integers. The t-th (1 \le t \le N) of them should be the answer for the case K=t. -----Sample Input----- 3 2 1 2 -----Sample Output----- 3 1 0 For K = 1, we can do the operation as follows: - Choose the first card to eat. - Choose the second card to eat. - Choose the third card to eat. For K = 2, we can do the operation as follows: - Choose the first and second cards to eat. For K = 3, we cannot do the operation at all. Note that we cannot choose the first and third cards at the same time.
{"inputs": ["3\n2 2 2", "3\n2 2 1", "3\n2 1 2", "3\n2 1 2\n", "4\n1 3 1 3", "4\n1 3 3 2", "4\n2 3 2 2", "4\n2 3 4 1"], "outputs": ["3\n0\n0\n", "3\n1\n0\n", "3\n1\n0", "3\n1\n0\n", "4\n2\n0\n0\n", "4\n2\n1\n0\n", "4\n1\n0\n0\n", "4\n2\n1\n1\n"]}
325
158
coding
Solve the programming task below in a Python markdown code block. # Task Your task is to sort the characters in a string according to the following rules: ``` - Rule1: English alphabets are arranged from A to Z, case insensitive. ie. "Type" --> "epTy" - Rule2: If the uppercase and lowercase of an English alphabet exist at the same time, they are arranged in the order of oringal input. ie. "BabA" --> "aABb" - Rule3: non English alphabet remain in their original position. ie. "By?e" --> "Be?y" ``` # Input/Output `[input]` string `s` A non empty string contains any characters(English alphabets or non English alphabets). `[output]` a string A sorted string according to the rules above. # Example For `s = "cba"`, the output should be `"abc"`. For `s = "Cba"`, the output should be `"abC"`. For `s = "cCBbAa"`, the output should be `"AaBbcC"`. For `s = "c b a"`, the output should be `"a b c"`. For `s = "-c--b--a-"`, the output should be `"-a--b--c-"`. For `s = "Codewars"`, the output should be `"aCdeorsw"`. Also feel free to reuse/extend the following starter code: ```python def sort_string(s): ```
{"functional": "_inputs = [['a'], ['cba'], ['Cba'], ['cCBbAa'], ['!'], ['c b a'], ['-c--b--a-'], ['cbaCcC'], ['Codewars'], [' MkWD{RB=//k-^ J@,xH Vfi uAz+$ kV _[ }a!}%pSBwn !kKB (b q PQF +}wS .kfU r wFNEs#NsR UVMdG']]\n_outputs = [['a'], ['abc'], ['abC'], ['AaBbcC'], ['!'], ['a b c'], ['-a--b--c-'], ['abcCcC'], ['aCdeorsw'], [' AaBB{Bb=//D-^ d@,Ef FfF GHi+$ Jk _[ }k!}%kkKkM !MnN (N p PqQ +}Rr .RSS s suUUV#VVW wwwxz']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(sort_string(*i), o[0])"}
338
359
coding
Solve the programming task below in a Python markdown code block. `This kata is the first of the ADFGX Ciphers, the harder version can be found `here. The ADFGX Cipher is a pretty well-known Cryptographic tool, and is essentially a modified Polybius Square. Rather than having numbers as coordinates on the table, it has the letters: `A, D, F, G, X` Also, because this is the first step, and to help simplify things, you won't have to worry about a key, or the corresponding columnar transposition. In this kata ;) All you have to do is encrypt and decrypt a string into `ADFGX` format. `adfgx_encrypt() and adfgx_decrypt()` will be passed a string, `plaintext` and `ciphertext` respectively, and an adfgx`square`, for which will guide the operations. Now for some examples to clear confusion: ```python adfgx_encrypt("helloworld", "bchigklnmoqprstuvwxyzadef") A D F G X A b c h i g D k l n m o F q p r s t -> square (PLEASE NOTE, j SHOULD BE TREATED AS i) G u v w x y X z a d e f "helloworld" -> plaintext EVALUATES TO: F -> "AF" A h -------------- G -> "XG" X e AND SO FORTH... #Results in: adfgx_encrypt("helloworld", "bchigklnmoqprstuvwxyzadef") == "AFXGDDDDDXGFDXFFDDXF" ``` Now decryption: ```python adfgx_decrypt("FGXGADGDXGFXAXXGFGFGAADGXG", "aczlmuqngoipvstkrwfxhdbey) A D F G X A a c z l m D u q n g o F i p v s t -> square (PLEASE NOTE, j SHOULD BE TREATED AS i) G k r w f x X h d b e y "FGXGADGDXGFXAXXGFGFGAADGXG" -> ciphertext "FG" == "s" "XG" == "e" AND SO ON: adfgx_decrypt("FGXGADGDXGFXAXXGFGFGAADGXG", "aczlmuqngoipvstkrwfxhdbey) == "secretmessage" ``` PLEASE NOTE: ALL INPUT WILL BE VALID, NO NEED TO ERROR CHECK :D What are you waiting for?! Go create `adfgx_encrypt() and adfgx_decrypt()`! Good Luck! Also feel free to reuse/extend the following starter code: ```python def adfgx_encrypt(plaintext, square): ```
{"functional": "_inputs = [['helloworld', 'bchigklnmoqprstuvwxyzadef']]\n_outputs = [['AFXGDDDDDXGFDXFFDDXF']]\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(adfgx_encrypt(*i), o[0])"}
629
181
coding
Solve the programming task below in a Python markdown code block. Polycarp has come up with a new game to play with you. He calls it "A missing bigram". A bigram of a word is a sequence of two adjacent letters in it. For example, word "abbaaba" contains bigrams "ab", "bb", "ba", "aa", "ab" and "ba". The game goes as follows. First, Polycarp comes up with a word, consisting only of lowercase letters 'a' and 'b'. Then, he writes down all its bigrams on a whiteboard in the same order as they appear in the word. After that, he wipes one of them off the whiteboard. Finally, Polycarp invites you to guess what the word that he has come up with was. Your goal is to find any word such that it's possible to write down all its bigrams and remove one of them, so that the resulting sequence of bigrams is the same as the one Polycarp ended up with. The tests are generated in such a way that the answer exists. If there are multiple answers, you can print any of them. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2000$) — the number of testcases. The first line of each testcase contains a single integer $n$ ($3 \le n \le 100$) — the length of the word Polycarp has come up with. The second line of each testcase contains $n-2$ bigrams of that word, separated by a single space. Each bigram consists of two letters, each of them is either 'a' or 'b'. Additional constraint on the input: there exists at least one string such that it is possible to write down all its bigrams, except one, so that the resulting sequence is the same as the sequence in the input. In other words, the answer exists. -----Output----- For each testcase print a word, consisting of $n$ letters, each of them should be either 'a' or 'b'. It should be possible to write down all its bigrams and remove one of them, so that the resulting sequence of bigrams is the same as the one Polycarp ended up with. The tests are generated in such a way that the answer exists. If there are multiple answers, you can print any of them. -----Examples----- Input 4 7 ab bb ba aa ba 7 ab ba aa ab ba 3 aa 5 bb ab bb Output abbaaba abaabaa baa bbabb -----Note----- The first two testcases from the example are produced from the word "abbaaba". As listed in the statement, it contains bigrams "ab", "bb", "ba", "aa", "ab" and "ba". In the first testcase, the $5$-th bigram is removed. In the second testcase, the $2$-nd bigram is removed. However, that sequence could also have been produced from the word "abaabaa". It contains bigrams "ab", "ba", "aa", "ab", "ba" and "aa". The missing bigram is the $6$-th one. In the third testcase, all of "baa", "aab" and "aaa" are valid answers.
{"inputs": ["4\n7\nab bb ba aa ba\n7\nab ba aa ab ba\n3\naa\n5\nbb ab bb\n", "4\n7\nab bb ba aa ba\n7\nab ba aa ab ba\n3\naa\n5\nbb ab bb\n", "4\n7\nab bb ba aa ba\n7\nab ba aa ab ab\n3\naa\n5\nbb ab bb\n", "4\n7\nab bb ba aa ba\n7\nab ba aa ab ba\n3\naa\n5\nbb ba bb\n", "4\n7\nab bb ba aa ba\n7\nab ba aa bb ba\n3\naa\n5\nbb ab bb\n", "4\n7\nab bb ba aa b`\n7\nab ba aa ab ab\n3\naa\n5\nbb ab bb\n", "4\n7\nab bb ba aa ba\n7\nab ba aa ab ab\n3\naa\n5\nbb ab ba\n", "4\n7\nab bb ba aa aa\n7\nab ba aa ab ab\n3\naa\n5\nbb ab ba\n"], "outputs": ["abbaaba\nabaabaa\naaa\nbbabb\n", "abbaaba\nabaabaa\naaa\nbbabb\n", "abbaaba\nabaabab\naaa\nbbabb\n", "abbaaba\nabaabaa\naaa\nbbabb\n", "abbaaba\nabaabba\naaa\nbbabb\n", "abbaab`\nabaabab\naaa\nbbabb\n", "abbaaba\nabaabab\naaa\nbbaba\n", "abbaaaa\nabaabab\naaa\nbbaba\n"]}
711
368
coding
Solve the programming task below in a Python markdown code block. A programming competition site AtCode provides algorithmic problems. Each problem is allocated a score based on its difficulty. Currently, for each integer i between 1 and D (inclusive), there are p_i problems with a score of 100i points. These p_1 + … + p_D problems are all of the problems available on AtCode. A user of AtCode has a value called total score. The total score of a user is the sum of the following two elements: - Base score: the sum of the scores of all problems solved by the user. - Perfect bonuses: when a user solves all problems with a score of 100i points, he/she earns the perfect bonus of c_i points, aside from the base score (1 ≤ i ≤ D). Takahashi, who is the new user of AtCode, has not solved any problem. His objective is to have a total score of G or more points. At least how many problems does he need to solve for this objective? -----Constraints----- - 1 ≤ D ≤ 10 - 1 ≤ p_i ≤ 100 - 100 ≤ c_i ≤ 10^6 - 100 ≤ G - All values in input are integers. - c_i and G are all multiples of 100. - It is possible to have a total score of G or more points. -----Input----- Input is given from Standard Input in the following format: D G p_1 c_1 : p_D c_D -----Output----- Print the minimum number of problems that needs to be solved in order to have a total score of G or more points. Note that this objective is always achievable (see Constraints). -----Sample Input----- 2 700 3 500 5 800 -----Sample Output----- 3 In this case, there are three problems each with 100 points and five problems each with 200 points. The perfect bonus for solving all the 100-point problems is 500 points, and the perfect bonus for solving all the 200-point problems is 800 points. Takahashi's objective is to have a total score of 700 points or more. One way to achieve this objective is to solve four 200-point problems and earn a base score of 800 points. However, if we solve three 100-point problems, we can earn the perfect bonus of 500 points in addition to the base score of 300 points, for a total score of 800 points, and we can achieve the objective with fewer problems.
{"inputs": ["1 100\n1 100\n", "2 431\n3 500\n5 800", "2 110\n3 500\n5 800", "2 431\n3 896\n5 800", "2 400\n3 500\n5 800", "2 700\n3 500\n5 800", "2 700\n3 500\n5 800\n", "2 400\n3 500\n5 800\n"], "outputs": ["1\n", "3\n", "1\n", "3\n", "2", "3", "3\n", "2\n"]}
575
185
coding
Solve the programming task below in a Python markdown code block. The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v_1 meters per second, and in the end it is v_2 meters per second. We know that this section of the route took exactly t seconds to pass. Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters. -----Input----- The first line contains two integers v_1 and v_2 (1 ≤ v_1, v_2 ≤ 100) — the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively. The second line contains two integers t (2 ≤ t ≤ 100) — the time when the car moves along the segment in seconds, d (0 ≤ d ≤ 10) — the maximum value of the speed change between adjacent seconds. It is guaranteed that there is a way to complete the segment so that: the speed in the first second equals v_1, the speed in the last second equals v_2, the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d. -----Output----- Print the maximum possible length of the path segment in meters. -----Examples----- Input 5 6 4 2 Output 26 Input 10 10 10 0 Output 100 -----Note----- In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters. In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters.
{"inputs": ["5 6\n4 2\n", "1 1\n5 1\n", "1 1\n5 2\n", "1 1\n2 1\n", "1 2\n2 1\n", "1 5\n3 2\n", "2 1\n2 2\n", "1 1\n3 5\n"], "outputs": ["26", "9", "13", "2", "3", "9", "3", "8"]}
457
112
coding
Solve the programming task below in a Python markdown code block. There is a sequence X of length N, where every element is initially 0. Let X_i denote the i-th element of X. You are given a sequence A of length N. The i-th element of A is A_i. Determine if we can make X equal to A by repeating the operation below. If we can, find the minimum number of operations required. * Choose an integer i such that 1\leq i\leq N-1. Replace the value of X_{i+1} with the value of X_i plus 1. Constraints * 1 \leq N \leq 2 \times 10^5 * 0 \leq A_i \leq 10^9(1\leq i\leq N) * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 : A_N Output If we can make X equal to A by repeating the operation, print the minimum number of operations required. If we cannot, print -1. Examples Input 4 0 1 1 2 Output 3 Input 3 1 2 1 Output -1 Input 9 0 1 1 0 1 2 2 1 2 Output 8
{"inputs": ["3\n0\n2\n1", "3\n0\n1\n0", "3\n0\n0\n0", "3\n0\n2\n2", "3\n0\n1\n2", "3\n0\n2\n0", "3\n1\n1\n0", "3\n1\n0\n0"], "outputs": ["-1\n", "1\n", "0\n", "-1\n", "2\n", "-1\n", "-1\n", "-1\n"]}
297
111
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. There is a long table with a line of plates and candles arranged on top of it. You are given a 0-indexed string s consisting of characters '*' and '|' only, where a '*' represents a plate and a '|' represents a candle. You are also given a 0-indexed 2D integer array queries where queries[i] = [lefti, righti] denotes the substring s[lefti...righti] (inclusive). For each query, you need to find the number of plates between candles that are in the substring. A plate is considered between candles if there is at least one candle to its left and at least one candle to its right in the substring. For example, s = "||**||**|*", and a query [3, 8] denotes the substring "*||**|". The number of plates between candles in this substring is 2, as each of the two plates has at least one candle in the substring to its left and right. Return an integer array answer where answer[i] is the answer to the ith query.   Please complete the following python code precisely: ```python class Solution: def platesBetweenCandles(self, s: str, queries: List[List[int]]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(s = \"**|**|***|\", queries = [[2,5],[5,9]]) == [2,3]\n assert candidate(s = \"***|**|*****|**||**|*\", queries = [[1,17],[4,5],[14,17],[5,11],[15,16]]) == [9,0,0,0,0]\n\n\ncheck(Solution().platesBetweenCandles)"}
272
112
coding
Solve the programming task below in a Python markdown code block. You are given two positive integers $x$ and $y$. You can perform the following operation with $x$: write it in its binary form without leading zeros, add $0$ or $1$ to the right of it, reverse the binary form and turn it into a decimal number which is assigned as the new value of $x$. For example: $34$ can be turned into $81$ via one operation: the binary form of $34$ is $100010$, if you add $1$, reverse it and remove leading zeros, you will get $1010001$, which is the binary form of $81$. $34$ can be turned into $17$ via one operation: the binary form of $34$ is $100010$, if you add $0$, reverse it and remove leading zeros, you will get $10001$, which is the binary form of $17$. $81$ can be turned into $69$ via one operation: the binary form of $81$ is $1010001$, if you add $0$, reverse it and remove leading zeros, you will get $1000101$, which is the binary form of $69$. $34$ can be turned into $69$ via two operations: first you turn $34$ into $81$ and then $81$ into $69$. Your task is to find out whether $x$ can be turned into $y$ after a certain number of operations (possibly zero). -----Input----- The only line of the input contains two integers $x$ and $y$ ($1 \le x, y \le 10^{18}$). -----Output----- Print YES if you can make $x$ equal to $y$ and NO if you can't. -----Examples----- Input 3 3 Output YES Input 7 4 Output NO Input 2 8 Output NO Input 34 69 Output YES Input 8935891487501725 71487131900013807 Output YES -----Note----- In the first example, you don't even need to do anything. The fourth example is described in the statement.
{"inputs": ["3 3\n", "7 4\n", "2 8\n", "4 4\n", "8 8\n", "2 2\n", "6 1\n", "4 5\n"], "outputs": ["YES\n", "NO\n", "NO\n", "YES\n", "YES\n", "YES\n", "NO\n", "NO\n"]}
531
86
coding
Solve the programming task below in a Python markdown code block. Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array of N integers. Find the *minimum* number of integers you need to delete from the array such that the absolute difference between each pair of integers in the remaining array will become equal. ------ Input Format ------ - The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains an integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the minimum number of integers to be deleted to satisfy the given condition. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 1 2 5 2 5 1 2 2 4 1 2 1 2 ----- Sample Output 1 ------ 0 2 2 ----- explanation 1 ------ Test case $1$: There is only one pair of integers and the absolute difference between them is $|A_{1} - A_{2}| = |1 - 2| = 1$. So there is no need to delete any integer from the given array. Test case $2$: If the integers $1$ and $5$ are deleted, the array A becomes $[2, 2, 2]$ and the absolute difference between each pair of integers is $0$. There is no possible way to delete less than two integers to satisfy the given condition.
{"inputs": ["3\n2\n1 2\n5\n2 5 1 2 2\n4\n1 2 1 2"], "outputs": ["0\n2\n2"]}
423
44
coding
Solve the programming task below in a Python markdown code block. Your city has n junctions. There are m one-way roads between the junctions. As a mayor of the city, you have to ensure the security of all the junctions. To ensure the security, you have to build some police checkposts. Checkposts can only be built in a junction. A checkpost at junction i can protect junction j if either i = j or the police patrol car can go to j from i and then come back to i. Building checkposts costs some money. As some areas of the city are more expensive than others, building checkpost at some junctions might cost more money than other junctions. You have to determine the minimum possible money needed to ensure the security of all the junctions. Also you have to find the number of ways to ensure the security in minimum price and in addition in minimum number of checkposts. Two ways are different if any of the junctions contains a checkpost in one of them and do not contain in the other. -----Input----- In the first line, you will be given an integer n, number of junctions (1 ≤ n ≤ 10^5). In the next line, n space-separated integers will be given. The i^{th} integer is the cost of building checkpost at the i^{th} junction (costs will be non-negative and will not exceed 10^9). The next line will contain an integer m (0 ≤ m ≤ 3·10^5). And each of the next m lines contains two integers u_{i} and v_{i} (1 ≤ u_{i}, v_{i} ≤ n; u ≠ v). A pair u_{i}, v_{i} means, that there is a one-way road which goes from u_{i} to v_{i}. There will not be more than one road between two nodes in the same direction. -----Output----- Print two integers separated by spaces. The first one is the minimum possible money needed to ensure the security of all the junctions. And the second one is the number of ways you can ensure the security modulo 1000000007 (10^9 + 7). -----Examples----- Input 3 1 2 3 3 1 2 2 3 3 2 Output 3 1 Input 5 2 8 0 6 0 6 1 4 1 3 2 4 3 4 4 5 5 1 Output 8 2 Input 10 1 3 2 2 1 3 1 4 10 10 12 1 2 2 3 3 1 3 4 4 5 5 6 5 7 6 4 7 3 8 9 9 10 10 9 Output 15 6 Input 2 7 91 2 1 2 2 1 Output 7 1
{"inputs": ["1\n0\n0\n", "1\n0\n0\n", "1\n1000000000\n0\n", "1\n1000000000\n0\n", "2\n7 91\n2\n1 2\n2 1\n", "2\n6 91\n2\n1 2\n2 1\n", "2\n7 91\n2\n1 2\n2 1\n", "3\n1 2 3\n3\n1 2\n2 3\n3 2\n"], "outputs": ["0 1\n", "0 1\n", "1000000000 1\n", "1000000000 1\n", "7 1\n", "6 1\n", "7 1\n", "3 1\n"]}
656
203
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. There is an integer array perm that is a permutation of the first n positive integers, where n is always odd. It was encoded into another integer array encoded of length n - 1, such that encoded[i] = perm[i] XOR perm[i + 1]. For example, if perm = [1,3,2], then encoded = [2,1]. Given the encoded array, return the original array perm. It is guaranteed that the answer exists and is unique.   Please complete the following python code precisely: ```python class Solution: def decode(self, encoded: List[int]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(encoded = [3,1]) == [1,2,3]\n assert candidate(encoded = [6,5,4,6]) == [2,4,1,5,3]\n\n\ncheck(Solution().decode)"}
145
64
coding
Solve the programming task below in a Python markdown code block. -----General Statement:----- Given the actual high and low temperatures for the day and the normal high and low temperatures for that day, calculate the average difference from normal. -----Input:----- The first line of the data set for this problem is an integer that represents the number of data sets that follow. Each data set is on a separate line and consists of today’s high, today’s low, normal high, and normal low – in that order. -----Output:----- If the average difference is negative, do not output the negative sign (-). Output the amount of deviation from normal, followed by the words DEGREE(S) ABOVE NORMAL, or by the words DEGREE(S) BELOW NORMAL. Round to 1 decimal place. A trailing zero is required if the average is an integer. The output is to be formatted exactly like that for the sample output given below. -----Assumptions:----- Temperatures are in the range –15..100 degrees. The average temperature difference will not be zero. -----Discussion:----- Determine the average of the difference of the high temperatures and the difference of the low temperatures. -----Sample Input:----- 3 75 45 78 40 50 32 45 30 56 48 62 45 -----Sample Output:----- 1.0 DEGREE(S) ABOVE NORMAL 3.5 DEGREE(S) ABOVE NORMAL 1.5 DEGREE(S) BELOW NORMAL
{"inputs": ["3\n75 45 78 40\n50 32 45 30\n56 48 62 45"], "outputs": ["1.0 DEGREE(S) ABOVE NORMAL\n3.5 DEGREE(S) ABOVE NORMAL\n1.5 DEGREE(S) BELOW NORMAL"]}
320
76
coding
Solve the programming task below in a Python markdown code block. Chef wants to buy a new laptop. However, he is confused about which laptop to buy out of 10 different laptops. He asks his N friends for their recommendation. The i^{th} friend recommends the Chef to buy the {A_{i}}^{th} laptop (1 ≤ A_{i} ≤ 10). Chef will buy the laptop which is recommended by maximum number of friends. Determine which laptop Chef buys. Print CONFUSED if there are multiple laptops having maximum number of recommendations. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the number of Chef's friends. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} where A_{i} denotes the recommendation of the i^{th} friend. ------ Output Format ------ For each test case, output in a single line, the laptop which has the maximum number of recommendations. Print CONFUSED if there are multiple laptops having maximum number of recommendations. You may print each character of CONFUSED in uppercase or lowercase (for example, Confused, coNFused, CONFused will be considered identical). ------ Constraints ------ $1 ≤ T ≤ 200$ $1 ≤ N ≤ 1000$ $1 ≤A_{i} ≤10$ ----- Sample Input 1 ------ 4 5 4 4 4 2 1 7 1 2 3 4 5 6 6 6 2 2 3 3 10 8 4 7 7 8 8 ----- Sample Output 1 ------ 4 6 CONFUSED CONFUSED ----- explanation 1 ------ Test case 1: Laptop $4$ has the maximum number of recommendations. Therefore, Chef will buy the $4^{th}$ laptop. Test case 2: Laptop $6$ has the maximum number of recommendations. Therefore, Chef will buy the $6^{th}$ laptop. Test case 3: Laptops $2$, $3$ have the maximum number of recommendations. Therefore, Chef will still be CONFUSED. Test case 4: Laptops $7$, $8$ have the maximum number of recommendations. Therefore, Chef will still be CONFUSED.
{"inputs": ["4\n5\n4 4 4 2 1\n7\n1 2 3 4 5 6 6\n6\n2 2 3 3 10 8\n4\n7 7 8 8\n"], "outputs": ["4\n6\nCONFUSED\nCONFUSED\n"]}
515
75
coding
Solve the programming task below in a Python markdown code block. You are given an array A, consisting of N integers and an array B, consisting of M integers. The subsequence of A is the array that can be obtained by picking the elements at the arbitrary sorted set of positions from A. Your task is to count the number of such subsequences C of A that: - C contains exactly M elements. - The array (C+B) is non-decreasing. Here by + operation, we mean element-wise sum. For example, the array (4, 8, 5) plus the array (10, 20, 30) is (14, 28, 35). Formally, (C+B) is an array of size M such that (C+B)i = Ci + Bi. In case some subsequence appears more that once, you should counts it as many times as it appears. Formally, two subarrays of an array a, (ai_1, ai_2, ... ,ai_n) and (aj_1, aj_2, ... ,aj_m) will be considered different if either their lengths are different i.e. n != m or there exists an index k such that such that i_k != j_k. Since the answer can be very large, we ask you to calculate it, modulo 109+7. -----Input----- The first line of input contains a pair of space separated integers N and M, denoting the number of elements in the array A and the number of elements in the array B. The second line contains N space-separated integers Ai, denoting the array A. The third line contains M space-separated integers Bj, denoting the array B. -----Output----- Output a single line containing the number of subsequences C as asked in the problem, modulo 109+7. -----Constraints----- - 1 ≤ Ai, Bi ≤ 109 - 1 ≤ M ≤ N -----Subtasks----- - Subtask #1 (33 points): 1 ≤ N ≤ 50, 1 ≤ M ≤ 5 - Subtask #2 (33 points): 1 ≤ N ≤ 500, 1 ≤ M ≤ 50 - Subtask #3 (34 points): 1 ≤ N ≤ 2000, 1 ≤ M ≤ 1000 -----Example----- Input #1: 5 3 1 5 2 4 7 7 9 6 Output #1: 4 Input #2: 4 2 7 7 7 7 3 4 Output #2: 6 -----Explanation----- Example case 1. The suitable subsequences are (1, 2, 7), (1, 4, 7), (5, 4, 7), (2, 4, 7). Example case 2. The suitable subsequence is (7, 7), and it appears 6 times: - at indices (1, 2) - at indices (1, 3) - at indices (1, 4) - at indices (2, 3) - at indices (2, 4) - at indices (3, 4) So, the answer is 6.
{"inputs": ["4 2\n7 7 7 7\n3 4\n\n", "5 3\n1 5 2 4 7\n7 9 6\n\n"], "outputs": ["6", "4"]}
698
54
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Balanced strings are those that have an equal quantity of 'L' and 'R' characters. Given a balanced string s, split it into some number of substrings such that: Each substring is balanced. Return the maximum number of balanced strings you can obtain.   Please complete the following python code precisely: ```python class Solution: def balancedStringSplit(self, s: str) -> int: ```
{"functional": "def check(candidate):\n assert candidate(s = \"RLRRLLRLRL\") == 4\n assert candidate(s = \"RLRRRLLRLL\") == 2\n assert candidate(s = \"LLLLRRRR\") == 1\n\n\ncheck(Solution().balancedStringSplit)"}
101
67
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given two integers m and n, which represent the dimensions of a matrix. You are also given the head of a linked list of integers. Generate an m x n matrix that contains the integers in the linked list presented in spiral order (clockwise), starting from the top-left of the matrix. If there are remaining empty spaces, fill them with -1. Return the generated matrix.   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 spiralMatrix(self, m: int, n: int, head: Optional[ListNode]) -> List[List[int]]: ```
{"functional": "def check(candidate):\n assert candidate(m = 3, n = 5, head = list_node([3,0,2,6,8,1,7,9,4,2,5,5,0])) == [[3,0,2,6,8],[5,0,-1,-1,1],[5,2,4,9,7]]\n assert candidate(m = 1, n = 4, head = list_node([0,1,2])) == [[0,1,2,-1]]\n\n\ncheck(Solution().spiralMatrix)"}
178
134
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a 0-indexed integer array costs where costs[i] is the cost of hiring the ith worker. You are also given two integers k and candidates. We want to hire exactly k workers according to the following rules: You will run k sessions and hire exactly one worker in each session. In each hiring session, choose the worker with the lowest cost from either the first candidates workers or the last candidates workers. Break the tie by the smallest index. For example, if costs = [3,2,7,7,1,2] and candidates = 2, then in the first hiring session, we will choose the 4th worker because they have the lowest cost [3,2,7,7,1,2]. In the second hiring session, we will choose 1st worker because they have the same lowest cost as 4th worker but they have the smallest index [3,2,7,7,2]. Please note that the indexing may be changed in the process. If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index. A worker can only be chosen once. Return the total cost to hire exactly k workers.   Please complete the following python code precisely: ```python class Solution: def totalCost(self, costs: List[int], k: int, candidates: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4) == 11\n assert candidate(costs = [1,2,4,1], k = 3, candidates = 3) == 4\n\n\ncheck(Solution().totalCost)"}
308
93