task_type stringclasses 4
values | problem stringlengths 14 5.23k | solution stringlengths 1 8.29k | problem_tokens int64 9 1.02k | solution_tokens int64 1 1.98k |
|---|---|---|---|---|
coding | Solve the programming task below in a Python markdown code block.
In Chefland, each chicken has X legs and each duck has Y legs. Chef's farm can have exactly one type of bird.
Given that the birds on the farm have a total of Z legs:
Print CHICKEN, if the farm can have only chickens but not ducks.
Print DUCK, if the farm can have only ducks but not chickens.
Print ANY, if the farm can have either chickens or ducks.
Print NONE, if the farm can have neither chickens nor ducks.
------ Input Format ------
- The first line will contain T, the number of test cases. Then the test cases follow.
- Each test case consists of a single line of input, containing three space-separated integers X, Y, Z.
------ Output Format ------
For each test case, output in a single line the type of bird on the farm.
- Print CHICKEN, if the farm can have only chickens but not ducks.
- Print DUCK, if the farm can have only ducks but not chickens.
- Print ANY, if the farm can have either chickens or ducks.
- Print NONE, if the farm can have neither chickens nor ducks.
You may print each character of the string in uppercase or lowercase (for example, the strings \texttt{AnY}, \texttt{anY}, \texttt{any} and \texttt{ANY} will all be treated as identical).
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ X, Y, Z ≤ 1000$
----- Sample Input 1 ------
3
2 3 5
2 2 2
3 4 6
----- Sample Output 1 ------
NONE
ANY
CHICKEN
----- explanation 1 ------
Test case $1$: There are $5$ legs on the farm. Each chicken has $2$ legs and each duck has $3$ legs. Thus, the farm can have neither chickens nor ducks.
Test case $2$: There are $2$ legs on the farm. Each chicken has $2$ legs and each duck has $2$ legs. Thus, the farm can have any bird among chicken and duck.
Test case $3$: There are $6$ legs on the farm. Each chicken has $3$ legs and each duck has $4$ legs. Thus, the farm can have chicken only. | {"inputs": ["3\n2 3 5\n2 2 2\n3 4 6"], "outputs": ["NONE\nANY\nCHICKEN"]} | 501 | 36 |
coding | Solve the programming task below in a Python markdown code block.
There are n points on a straight line, and the i-th point among them is located at x_{i}. All these coordinates are distinct.
Determine the number m — the smallest number of points you should add on the line to make the distances between all neighboring points equal.
-----Input-----
The first line contains a single integer n (3 ≤ n ≤ 100 000) — the number of points.
The second line contains a sequence of integers x_1, x_2, ..., x_{n} ( - 10^9 ≤ x_{i} ≤ 10^9) — the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order.
-----Output-----
Print a single integer m — the smallest number of points you should add on the line to make the distances between all neighboring points equal.
-----Examples-----
Input
3
-5 10 5
Output
1
Input
6
100 200 400 300 600 500
Output
0
Input
4
10 9 0 -1
Output
8
-----Note-----
In the first example you can add one point with coordinate 0.
In the second example the distances between all neighboring points are already equal, so you shouldn't add anything. | {"inputs": ["3\n1 4 7\n", "3\n1 4 6\n", "3\n1 2 6\n", "3\n1 3 6\n", "3\n1 4 7\n", "3\n1 3 6\n", "3\n1 4 6\n", "3\n1 2 6\n"], "outputs": ["0\n", "3\n", "3\n", "3\n", "0\n", "3\n", "3\n", "3\n"]} | 301 | 118 |
coding | Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given two integer arrays, source and target, both of length n. You are also given an array allowedSwaps where each allowedSwaps[i] = [ai, bi] indicates that you are allowed to swap the elements at index ai and index bi (0-indexed) of array source. Note that you can swap elements at a specific pair of indices multiple times and in any order.
The Hamming distance of two arrays of the same length, source and target, is the number of positions where the elements are different. Formally, it is the number of indices i for 0 <= i <= n-1 where source[i] != target[i] (0-indexed).
Return the minimum Hamming distance of source and target after performing any amount of swap operations on array source.
Please complete the following python code precisely:
```python
class Solution:
def minimumHammingDistance(self, source: List[int], target: List[int], allowedSwaps: List[List[int]]) -> int:
``` | {"functional": "def check(candidate):\n assert candidate(source = [1,2,3,4], target = [2,1,4,5], allowedSwaps = [[0,1],[2,3]]) == 1\n assert candidate(source = [1,2,3,4], target = [1,3,2,4], allowedSwaps = []) == 2\n assert candidate(source = [5,1,2,4,3], target = [1,5,4,2,3], allowedSwaps = [[0,4],[4,2],[1,3],[1,4]]) == 0\n\n\ncheck(Solution().minimumHammingDistance)"} | 224 | 151 |
coding | Solve the programming task below in a Python markdown code block.
Palindrome
Problem Statement
Find the number of palindromes closest to the integer n.
Note that the non-negative integer x is the number of palindromes, which means that the character string in which x is expressed in decimal notation and the character string in which it is inverted are equal.
For example, 0,7,33,10301 is the number of palindromes, and 32,90,1010 is not the number of palindromes.
Constraints
* 1 ≤ n ≤ 10 ^ 4
Input
Input follows the following format. All given numbers are integers.
n
Output
Output the number of palindromes closest to n.
If there are multiple such numbers, output the smallest one.
Examples
Input
13
Output
11
Input
7447
Output
7447
Input
106
Output
101 | {"inputs": ["4", "8", "7", "6", "5", "1", "2", "3"], "outputs": ["4\n", "8\n", "7\n", "6\n", "5\n", "1\n", "2\n", "3\n"]} | 215 | 62 |
coding | Please solve the programming task below using a self-contained code snippet in a markdown code block.
A cell (r, c) of an excel sheet is represented as a string "<col><row>" where:
<col> denotes the column number c of the cell. It is represented by alphabetical letters.
For example, the 1st column is denoted by 'A', the 2nd by 'B', the 3rd by 'C', and so on.
<row> is the row number r of the cell. The rth row is represented by the integer r.
You are given a string s in the format "<col1><row1>:<col2><row2>", where <col1> represents the column c1, <row1> represents the row r1, <col2> represents the column c2, and <row2> represents the row r2, such that r1 <= r2 and c1 <= c2.
Return the list of cells (x, y) such that r1 <= x <= r2 and c1 <= y <= c2. The cells should be represented as strings in the format mentioned above and be sorted in non-decreasing order first by columns and then by rows.
Please complete the following python code precisely:
```python
class Solution:
def cellsInRange(self, s: str) -> List[str]:
``` | {"functional": "def check(candidate):\n assert candidate(s = \"K1:L2\") == [\"K1\",\"K2\",\"L1\",\"L2\"]\n assert candidate(s = \"A1:F1\") == [\"A1\",\"B1\",\"C1\",\"D1\",\"E1\",\"F1\"]\n\n\ncheck(Solution().cellsInRange)"} | 287 | 81 |
coding | Solve the programming task below in a Python markdown code block.
One day Prof. Slim decided to leave the kingdom of the GUC to join the kingdom of the GIU. He was given an easy online assessment to solve before joining the GIU. Citizens of the GUC were happy sad to see the prof leaving, so they decided to hack into the system and change the online assessment into a harder one so that he stays at the GUC. After a long argument, they decided to change it into the following problem.
Given an array of $n$ integers $a_1,a_2,\ldots,a_n$, where $a_{i} \neq 0$, check if you can make this array sorted by using the following operation any number of times (possibly zero). An array is sorted if its elements are arranged in a non-decreasing order.
select two indices $i$ and $j$ ($1 \le i,j \le n$) such that $a_i$ and $a_j$ have different signs. In other words, one must be positive and one must be negative.
swap the signs of $a_{i}$ and $a_{j}$. For example if you select $a_i=3$ and $a_j=-2$, then they will change to $a_i=-3$ and $a_j=2$.
Prof. Slim saw that the problem is still too easy and isn't worth his time, so he decided to give it to you to solve.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^{5}$) — the length of the array $a$.
The next line contain $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_{i} \le 10^9$, $a_{i} \neq 0$) separated by spaces describing elements of the array $a$.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print "YES" if the array can be sorted in the non-decreasing order, otherwise print "NO". You can print each letter in any case (upper or lower).
-----Examples-----
Input
4
7
7 3 2 -11 -13 -17 -23
6
4 10 25 47 71 96
6
71 -35 7 -4 -11 -25
6
-45 9 -48 -67 -55 7
Output
NO
YES
YES
NO
-----Note-----
In the first test case, there is no way to make the array sorted using the operation any number of times.
In the second test case, the array is already sorted.
In the third test case, we can swap the sign of the $1$-st element with the sign of the $5$-th element, and the sign of the $3$-rd element with the sign of the $6$-th element, this way the array will be sorted.
In the fourth test case, there is no way to make the array sorted using the operation any number of times. | {"inputs": ["4\n7\n7 3 2 -11 -13 -17 -23\n6\n4 10 25 47 71 96\n6\n71 -35 7 -4 -11 -25\n6\n-45 9 -48 -67 -55 7\n", "8\n2\n-59 -9\n1\n11\n3\n-41 -26 -2\n3\n22 33 54\n3\n-56 -6 55\n5\n36 15 9 14 -71\n3\n-7 40 47\n5\n-19 54 -5 7 -29\n"], "outputs": ["NO\nYES\nYES\nNO\n", "YES\nYES\nYES\nYES\nYES\nNO\nYES\nNO\n"]} | 739 | 207 |
coding | Solve the programming task below in a Python markdown code block.
In a recent [breakthrough] in mathematics, the proof utilized a concept called Height.
Consider a fraction \frac{a}{b}. Its Height is defined as the maximum of its numerator and denominator. So, for example, the Height of \frac{3}{19} would be 19, and the Height of \frac{27}{4} would be 27.
Given a and b, find the Height of \frac{a}{b}.
------ Input Format ------
The only line of input contains two integers, a and b.
------ Output Format ------
Output a single integer, which is the Height of \frac{a}{b}.
------ Constraints ------
$1 ≤ a, b ≤ 100$
$a$ and $b$ do not have any common factors.
----- Sample Input 1 ------
3 19
----- Sample Output 1 ------
19
----- explanation 1 ------
The maximum of $\{3, 19\}$ is $19$. Hence the Height of $\frac{3}{19}$ is $19$.
----- Sample Input 2 ------
27 4
----- Sample Output 2 ------
27
----- explanation 2 ------
The maximum of $\{27, 4\}$ is $27$. Hence the Height of $\frac{27}{4}$ is $27$. | {"inputs": ["3 19\n", "27 4\n"], "outputs": ["19\n", "27\n"]} | 306 | 30 |
coding | Solve the programming task below in a Python markdown code block.
Two players play a simple game. Each player is provided with a box with balls. First player's box contains exactly n_1 balls and second player's box contains exactly n_2 balls. In one move first player can take from 1 to k_1 balls from his box and throw them away. Similarly, the second player can take from 1 to k_2 balls from his box in his move. Players alternate turns and the first player starts the game. The one who can't make a move loses. Your task is to determine who wins if both players play optimally.
-----Input-----
The first line contains four integers n_1, n_2, k_1, k_2. All numbers in the input are from 1 to 50.
This problem doesn't have subproblems. You will get 3 points for the correct submission.
-----Output-----
Output "First" if the first player wins and "Second" otherwise.
-----Examples-----
Input
2 2 1 2
Output
Second
Input
2 1 1 1
Output
First
-----Note-----
Consider the first sample test. Each player has a box with 2 balls. The first player draws a single ball from his box in one move and the second player can either take 1 or 2 balls from his box in one move. No matter how the first player acts, the second player can always win if he plays wisely. | {"inputs": ["2 2 1 2\n", "2 1 1 1\n", "5 7 4 1\n", "5 7 1 4\n", "1 1 1 1\n", "5 7 1 4\n", "5 7 4 1\n", "1 1 1 1\n"], "outputs": ["Second\n", "First\n", "Second\n", "Second\n", "Second\n", "Second\n", "Second\n", "Second\n"]} | 310 | 118 |
coding | Solve the programming task below in a Python markdown code block.
For given integers m and n, compute mn (mod 1,000,000,007). Here, A (mod M) is the remainder when A is divided by M.
Constraints
* 1 ≤ m ≤ 100
* 1 ≤ n ≤ 109
Input
m n
Two integers m and n are given in a line.
Output
Print mn (mod 1,000,000,007) in a line.
Examples
Input
2 3
Output
8
Input
5 8
Output
390625 | {"inputs": ["2 1", "5 9", "2 2", "7 9", "3 3", "1 3", "3 1", "0 1"], "outputs": ["2\n", "1953125\n", "4\n", "40353607\n", "27\n", "1\n", "3\n", "0\n"]} | 147 | 92 |
coding | Solve the programming task below in a Python markdown code block.
The new "Avengers" movie has just been released! There are a lot of people at the cinema box office standing in a huge line. Each of them has a single `100`, `50` or `25` dollar bill. An "Avengers" ticket costs `25 dollars`.
Vasya is currently working as a clerk. He wants to sell a ticket to every single person in this line.
Can Vasya sell a ticket to every person and give change if he initially has no money and sells the tickets strictly in the order people queue?
Return `YES`, if Vasya can sell a ticket to every person and give change with the bills he has at hand at that moment. Otherwise return `NO`.
### Examples:
```csharp
Line.Tickets(new int[] {25, 25, 50}) // => YES
Line.Tickets(new int[] {25, 100}) // => NO. Vasya will not have enough money to give change to 100 dollars
Line.Tickets(new int[] {25, 25, 50, 50, 100}) // => NO. Vasya will not have the right bills to give 75 dollars of change (you can't make two bills of 25 from one of 50)
```
```python
tickets([25, 25, 50]) # => YES
tickets([25, 100]) # => NO. Vasya will not have enough money to give change to 100 dollars
tickets([25, 25, 50, 50, 100]) # => NO. Vasya will not have the right bills to give 75 dollars of change (you can't make two bills of 25 from one of 50)
```
```cpp
tickets({25, 25, 50}) // => YES
tickets({25, 100}) // => NO. Vasya will not have enough money to give change to 100 dollars
tickets({25, 25, 50, 50, 100}) // => NO. Vasya will not have the right bills to give 75 dollars of change (you can't make two bills of 25 from one of 50)
```
Also feel free to reuse/extend the following starter code:
```python
def tickets(people):
``` | {"functional": "_inputs = [[[25, 25, 50]], [[25, 25, 50, 100]], [[25, 100]], [[25, 25, 25, 25, 25, 25, 25, 25, 25, 25]], [[50, 50, 50, 50, 50, 50, 50, 50, 50, 50]], [[100, 100, 100, 100, 100, 100, 100, 100, 100, 100]], [[25, 25, 25, 25, 50, 100, 50]], [[50, 100, 100]], [[25, 25, 100]], [[25, 25, 25, 25, 25, 25, 25, 50, 50, 50, 100, 100, 100, 100]], [[25, 25, 50, 50, 100]], [[25, 50, 50]], [[25, 25, 25, 100]], [[25, 50, 25, 100]], [[25, 25, 25, 25, 25, 100, 100]], [[25, 50, 100, 25, 25, 25, 50]], [[25, 50, 25, 50, 100, 25, 25, 50]], [[25, 50, 25, 100, 25, 25, 50, 100, 25, 25, 25, 100, 25, 25, 50, 100, 25, 50, 25, 100, 25, 50, 50, 50]], [[25, 25, 25, 100, 25, 25, 25, 100, 25, 25, 50, 100, 25, 25, 50, 100, 50, 50]], [[25, 50, 25, 100, 25, 25, 50, 100, 25, 50, 25, 100, 50, 25]]]\n_outputs = [['YES'], ['YES'], ['NO'], ['YES'], ['NO'], ['NO'], ['YES'], ['NO'], ['NO'], ['NO'], ['NO'], ['NO'], ['YES'], ['YES'], ['NO'], ['NO'], ['NO'], ['NO'], ['NO'], ['NO']]\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(tickets(*i), o[0])"} | 551 | 887 |
coding | Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given an integer array arr, return the length of a maximum size turbulent subarray of arr.
A subarray is turbulent if the comparison sign flips between each adjacent pair of elements in the subarray.
More formally, a subarray [arr[i], arr[i + 1], ..., arr[j]] of arr is said to be turbulent if and only if:
For i <= k < j:
arr[k] > arr[k + 1] when k is odd, and
arr[k] < arr[k + 1] when k is even.
Or, for i <= k < j:
arr[k] > arr[k + 1] when k is even, and
arr[k] < arr[k + 1] when k is odd.
Please complete the following python code precisely:
```python
class Solution:
def maxTurbulenceSize(self, arr: List[int]) -> int:
``` | {"functional": "def check(candidate):\n assert candidate(arr = [9,4,2,10,7,8,8,1,9]) == 5\n assert candidate(arr = [4,8,12,16]) == 2\n assert candidate(arr = [100]) == 1\n\n\ncheck(Solution().maxTurbulenceSize)"} | 205 | 85 |
coding | Solve the programming task below in a Python markdown code block.
Is the number even?
If the numbers is even return `true`. If it's odd, return `false`.
Oh yeah... the following symbols/commands have been disabled!
use of ```%```
use of ```.even?``` in Ruby
use of ```mod``` in Python
Also feel free to reuse/extend the following starter code:
```python
def is_even(n):
``` | {"functional": "_inputs = [[2], [3], [14], [15], [26], [27]]\n_outputs = [[True], [False], [True], [False], [True], [False]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(is_even(*i), o[0])"} | 94 | 188 |
coding | Solve the programming task below in a Python markdown code block.
We will define Ginkgo numbers and multiplication on Ginkgo numbers.
A Ginkgo number is a pair <m, n> where m and n are integers. For example, <1, 1>, <-2, 1> and <-3,-1> are Ginkgo numbers.
The multiplication on Ginkgo numbers is defined by <m, n> * <x, y> = <mx − ny, my + nx>. For example, <1, 1> * <-2, 1> = <-3,-1>.
A Ginkgo number <m, n> is called a divisor of a Ginkgo number <p, q> if there exists a Ginkgo number <x, y> such that <m, n> * <x, y> = <p, q>.
For any Ginkgo number <m, n>, Ginkgo numbers <1, 0>, <0, 1>, <-1, 0>, <0,-1>, <m, n>, <-n,m>, <-m,-n> and <n,-m> are divisors of <m, n>. If m2+n2 > 1, these Ginkgo numbers are distinct. In other words, any Ginkgo number such that m2 + n2 > 1 has at least eight divisors.
A Ginkgo number <m, n> is called a prime if m2+n2 > 1 and it has exactly eight divisors. Your mission is to check whether a given Ginkgo number is a prime or not.
The following two facts might be useful to check whether a Ginkgo number is a divisor of another Ginkgo number.
* Suppose m2 + n2 > 0. Then, <m, n> is a divisor of <p, q> if and only if the integer m2 + n2 is a common divisor of mp + nq and mq − np.
* If <m, n> * <x, y> = <p, q>, then (m2 + n2)(x2 + y2) = p2 + q2.
Input
The first line of the input contains a single integer, which is the number of datasets.
The rest of the input is a sequence of datasets. Each dataset is a line containing two integers m and n, separated by a space. They designate the Ginkgo number <m, n>. You can assume 1 < m2 + n2 < 20000.
Output
For each dataset, output a character 'P' in a line if the Ginkgo number is a prime. Output a character 'C' in a line otherwise.
Example
Input
8
10 0
0 2
-3 0
4 2
0 -13
-4 1
-2 -1
3 -1
Output
C
C
P
C
C
P
P
C | {"inputs": ["8\n2 0\n1 2\n-3 1\n4 2\n0 -13\n-4 1\n-2 0\n2 0", "8\n10 -1\n0 2\n0 3\n4 0\n2 -25\n-4 0\n-2 0\n2 0", "8\n10 -1\n0 2\n0 3\n8 0\n2 -23\n-4 0\n-2 1\n2 0", "8\n2 0\n1 2\n-3 1\n4 2\n0 -13\n-4 1\n-2 0\n2 -1", "8\n10 -1\n0 2\n0 3\n4 1\n2 -23\n-4 0\n-2 0\n2 0", "8\n4 0\n0 2\n-3 1\n4 2\n0 -32\n-4 0\n-3 0\n2 -1", "8\n2 0\n1 4\n-3 1\n4 2\n0 -13\n-8 2\n-5 -1\n2 0", "8\n2 0\n1 4\n-3 1\n4 2\n0 -23\n-8 2\n-5 -1\n2 0"], "outputs": ["C\nP\nC\nC\nC\nP\nC\nC\n", "P\nC\nP\nC\nC\nC\nC\nC\n", "P\nC\nP\nC\nC\nC\nP\nC\n", "C\nP\nC\nC\nC\nP\nC\nP\n", "P\nC\nP\nP\nC\nC\nC\nC\n", "C\nC\nC\nC\nC\nC\nP\nP\n", "C\nP\nC\nC\nC\nC\nC\nC\n", "C\nP\nC\nC\nP\nC\nC\nC\n"]} | 635 | 462 |
coding | Solve the programming task below in a Python markdown code block.
You have array a that contains all integers from 1 to n twice. You can arbitrary permute any numbers in a.
Let number i be in positions x_{i}, y_{i} (x_{i} < y_{i}) in the permuted array a. Let's define the value d_{i} = y_{i} - x_{i} — the distance between the positions of the number i. Permute the numbers in array a to minimize the value of the sum $s = \sum_{i = 1}^{n}(n - i) \cdot|d_{i} + i - n$.
-----Input-----
The only line contains integer n (1 ≤ n ≤ 5·10^5).
-----Output-----
Print 2n integers — the permuted array a that minimizes the value of the sum s.
-----Examples-----
Input
2
Output
1 1 2 2
Input
1
Output
1 1 | {"inputs": ["2\n", "1\n", "3\n", "4\n", "4\n", "3\n", "8\n", "6\n"], "outputs": ["1 1 2 2\n", "1 1\n", "1 3 1 2 2 3\n", "1 3 3 1 2 4 2 4\n", "1 3 3 1 2 4 2 4 ", "1 3 1 2 2 3 ", "1 3 5 7 7 5 3 1 2 4 6 8 6 4 2 8 \n", "1 3 5 5 3 1 2 4 6 4 2 6 \n"]} | 216 | 178 |
coding | Solve the programming task below in a Python markdown code block.
Ostap already settled down in Rio de Janiero suburb and started to grow a tree in his garden. Recall that a tree is a connected undirected acyclic graph.
Ostap's tree now has n vertices. He wants to paint some vertices of the tree black such that from any vertex u there is at least one black vertex v at distance no more than k. Distance between two vertices of the tree is the minimum possible number of edges of the path between them.
As this number of ways to paint the tree can be large, Ostap wants you to compute it modulo 109 + 7. Two ways to paint the tree are considered different if there exists a vertex that is painted black in one way and is not painted in the other one.
Input
The first line of the input contains two integers n and k (1 ≤ n ≤ 100, 0 ≤ k ≤ min(20, n - 1)) — the number of vertices in Ostap's tree and the maximum allowed distance to the nearest black vertex. Don't miss the unusual constraint for k.
Each of the next n - 1 lines contain two integers ui and vi (1 ≤ ui, vi ≤ n) — indices of vertices, connected by the i-th edge. It's guaranteed that given graph is a tree.
Output
Print one integer — the remainder of division of the number of ways to paint the tree by 1 000 000 007 (109 + 7).
Examples
Input
2 0
1 2
Output
1
Input
2 1
1 2
Output
3
Input
4 1
1 2
2 3
3 4
Output
9
Input
7 2
1 2
2 3
1 4
4 5
1 6
6 7
Output
91
Note
In the first sample, Ostap has to paint both vertices black.
In the second sample, it is enough to paint only one of two vertices, thus the answer is 3: Ostap can paint only vertex 1, only vertex 2, vertices 1 and 2 both.
In the third sample, the valid ways to paint vertices are: {1, 3}, {1, 4}, {2, 3}, {2, 4}, {1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}, {1, 2, 3, 4}. | {"inputs": ["1 0\n", "2 2\n1 2\n", "2 1\n1 2\n", "2 0\n1 2\n", "4 1\n1 2\n2 3\n3 4\n", "5 1\n1 2\n4 3\n5 3\n2 3\n", "7 2\n1 2\n2 3\n1 4\n4 5\n1 6\n6 7\n", "10 5\n6 4\n4 1\n7 5\n8 7\n2 6\n3 6\n8 6\n10 3\n4 9\n"], "outputs": ["1", "3\n", "3", "1", "9", "15", "91", "1023"]} | 559 | 186 |
coding | Solve the programming task below in a Python markdown code block.
There are N blocks arranged in a row. Let us paint these blocks.
We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways.
Find the number of ways to paint the blocks under the following conditions:
- For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors.
- There may be at most K pairs of adjacent blocks that are painted in the same color.
Since the count may be enormous, print it modulo 998244353.
-----Constraints-----
- All values in input are integers.
- 1 \leq N, M \leq 2 \times 10^5
- 0 \leq K \leq N - 1
-----Input-----
Input is given from Standard Input in the following format:
N M K
-----Output-----
Print the answer.
-----Sample Input-----
3 2 1
-----Sample Output-----
6
The following ways to paint the blocks satisfy the conditions: 112, 121, 122, 211, 212, and 221. Here, digits represent the colors of the blocks. | {"inputs": ["6 2 1", "5 3 1", "5 3 2", "3 2 0", "7 2 1", "2 2 1", "7 3 2", "5 4 2"], "outputs": ["12\n", "144\n", "216\n", "2\n", "14\n", "4\n", "1488\n", "972\n"]} | 283 | 105 |
coding | Solve the programming task below in a Python markdown code block.
Humpy, the little elephant, has his birthday coming up. He invited all his cousins but doesn’t know how many of them are really coming as some of them are having exams coming up. He will only get to know how many of them are coming on the day of his birthday.
He ordered sugarcane for his party, of length L.
Humpy’s mom decided that she will be dividing the sugarcane among Humty and his friends in a way such that they get the sugarcane in ratio of their ages.
Your task is to determine whether it is possible to serve sugarcane to everyone as integral multiples of their ages.
-----INPUT-----
First line of input contains an integer N, denoting the number of test cases.
Then N test cases follow.
The first line of each test case contains three integers K, L and E. K denoting the number of friends coming; L denoting the length of the sugarcane and E denoting the age of the little elephant. Next line has K space separated integers denoting the age of friends who came to the party.
-----OUTPUT-----
For each test case, output “YES” (without quotes) if everyone gets their part as integral multiples of their ages; otherwise output “NO”(without quotes).
-----CONSTRAINTS-----
- 1 <= T<=30
- 1 <= K<=1000
- 1 <= L<=1000000
- 1 <= E<=100000
- 1 <= Age of Cousins<=100000
-----Example-----
Input:
2
4 10 2
2 2 3 1
4 12 3
6 5 7 3
Output:
YES
NO | {"inputs": ["2\n4 10 2\n2 2 3 1\n4 12 3\n6 5 7 3"], "outputs": ["YES\nNO"]} | 383 | 44 |
coding | Solve the programming task below in a Python markdown code block.
There is an empty array.
The following N operations will be performed to insert integers into the array.
In the i-th operation (1≤i≤N), b_i copies of an integer a_i are inserted into the array.
Find the K-th smallest integer in the array after the N operations.
For example, the 4-th smallest integer in the array \{1,2,2,3,3,3\} is 3.
-----Constraints-----
- 1≤N≤10^5
- 1≤a_i,b_i≤10^5
- 1≤K≤b_1…+…b_n
- All input values are integers.
-----Input-----
Input is given from Standard Input in the following format:
N K
a_1 b_1
:
a_N b_N
-----Output-----
Print the K-th smallest integer in the array after the N operations.
-----Sample Input-----
3 4
1 1
2 2
3 3
-----Sample Output-----
3
The resulting array is the same as the one in the problem statement. | {"inputs": ["3 4\n1 1\n2 2\n3 2", "3 4\n1 1\n2 3\n3 2", "3 4\n1 1\n4 3\n3 2", "3 5\n2 2\n1 1\n8 3", "3 3\n2 2\n1 0\n5 3", "3 3\n2 2\n1 0\n7 3", "3 4\n1 0\n2 1\n6 6", "3 0\n2 2\n0 0\n3 6"], "outputs": ["3\n", "2\n", "4\n", "8\n", "5\n", "7\n", "6\n", "0\n"]} | 244 | 174 |
coding | Solve the programming task below in a Python markdown code block.
The semester is already ending, so Danil made an effort and decided to visit a lesson on harmony analysis to know how does the professor look like, at least. Danil was very bored on this lesson until the teacher gave the group a simple task: find 4 vectors in 4-dimensional space, such that every coordinate of every vector is 1 or - 1 and any two vectors are orthogonal. Just as a reminder, two vectors in n-dimensional space are considered to be orthogonal if and only if their scalar product is equal to zero, that is: $\sum_{i = 1}^{n} a_{i} \cdot b_{i} = 0$.
Danil quickly managed to come up with the solution for this problem and the teacher noticed that the problem can be solved in a more general case for 2^{k} vectors in 2^{k}-dimensinoal space. When Danil came home, he quickly came up with the solution for this problem. Can you cope with it?
-----Input-----
The only line of the input contains a single integer k (0 ≤ k ≤ 9).
-----Output-----
Print 2^{k} lines consisting of 2^{k} characters each. The j-th character of the i-th line must be equal to ' * ' if the j-th coordinate of the i-th vector is equal to - 1, and must be equal to ' + ' if it's equal to + 1. It's guaranteed that the answer always exists.
If there are many correct answers, print any.
-----Examples-----
Input
2
Output
++**
+*+*
++++
+**+
-----Note-----
Consider all scalar products in example: Vectors 1 and 2: ( + 1)·( + 1) + ( + 1)·( - 1) + ( - 1)·( + 1) + ( - 1)·( - 1) = 0 Vectors 1 and 3: ( + 1)·( + 1) + ( + 1)·( + 1) + ( - 1)·( + 1) + ( - 1)·( + 1) = 0 Vectors 1 and 4: ( + 1)·( + 1) + ( + 1)·( - 1) + ( - 1)·( - 1) + ( - 1)·( + 1) = 0 Vectors 2 and 3: ( + 1)·( + 1) + ( - 1)·( + 1) + ( + 1)·( + 1) + ( - 1)·( + 1) = 0 Vectors 2 and 4: ( + 1)·( + 1) + ( - 1)·( - 1) + ( + 1)·( - 1) + ( - 1)·( + 1) = 0 Vectors 3 and 4: ( + 1)·( + 1) + ( + 1)·( - 1) + ( + 1)·( - 1) + ( + 1)·( + 1) = 0 | {"inputs": ["2\n", "1\n", "3\n", "0\n", "4\n", "2\n", "4\n", "1\n"], "outputs": ["++++\n+*+*\n++**\n+**+\n", "++\n+*\n", "++++++++\n+*+*+*+*\n++**++**\n+**++**+\n++++****\n+*+**+*+\n++****++\n+**+*++*\n", "+\n", "++++++++++++++++\n+*+*+*+*+*+*+*+*\n++**++**++**++**\n+**++**++**++**+\n++++****++++****\n+*+**+*++*+**+*+\n++****++++****++\n+**+*++*+**+*++*\n++++++++********\n+*+*+*+**+*+*+*+\n++**++****++**++\n+**++**+*++**++*\n++++********++++\n+*+**+*+*+*++*+*\n++****++**++++**\n+**+*++**++*+**+\n", "++++\n+*+*\n++**\n+**+\n", "++++++++++++++++\n+*+*+*+*+*+*+*+*\n++**++**++**++**\n+**++**++**++**+\n++++****++++****\n+*+**+*++*+**+*+\n++****++++****++\n+**+*++*+**+*++*\n++++++++********\n+*+*+*+**+*+*+*+\n++**++****++**++\n+**++**+*++**++*\n++++********++++\n+*+**+*+*+*++*+*\n++****++**++++**\n+**+*++**++*+**+\n", "++\n+*\n"]} | 721 | 468 |
coding | Solve the programming task below in a Python markdown code block.
B: Twins
One twin was angry that it was not well known which of them was the older brother and which was the younger brother.
Create a program that outputs "square1001" when "ani" is entered and "e869120" when "otouto" is entered.
input
You will be given either the string "ani" or "otouto" as input.
output
Please output "e869120" or "square1001" according to the problem statement. Don't forget the last line break.
Input example 1
ani
Output example 1
square1001
Input example 2
otouto
Output example 2
e869120
Example
Input
ani
Output
square1001 | {"inputs": ["ani"], "outputs": ["square1001"]} | 187 | 16 |
coding | Solve the programming task below in a Python markdown code block.
In this kata we want to convert a string into an integer. The strings simply represent the numbers in words.
Examples:
* "one" => 1
* "twenty" => 20
* "two hundred forty-six" => 246
* "seven hundred eighty-three thousand nine hundred and nineteen" => 783919
Additional Notes:
* The minimum number is "zero" (inclusively)
* The maximum number, which must be supported is 1 million (inclusively)
* The "and" in e.g. "one hundred and twenty-four" is optional, in some cases it's present and in others it's not
* All tested numbers are valid, you don't need to validate them
Also feel free to reuse/extend the following starter code:
```python
def parse_int(string):
``` | {"functional": "_inputs = [['zero'], ['one'], ['two'], ['three'], ['four'], ['five'], ['six'], ['seven'], ['eight'], ['nine'], ['ten'], ['twenty'], ['twenty-one'], ['thirty-seven'], ['forty-six'], ['fifty-nine'], ['sixty-eight'], ['seventy-two'], ['eighty-three'], ['ninety-four'], ['one hundred'], ['one hundred one'], ['one hundred and one'], ['one hundred sixty-nine'], ['two hundred and ninety-nine'], ['seven hundred thirty-six'], ['two thousand'], ['one thousand three hundred and thirty-seven'], ['ten thousand'], ['twenty-six thousand three hundred and fifty-nine'], ['thirty-five thousand'], ['ninety-nine thousand nine hundred and ninety-nine'], ['six hundred sixty-six thousand six hundred sixty-six'], ['seven hundred thousand'], ['two hundred thousand three'], ['two hundred thousand and three'], ['two hundred three thousand'], ['five hundred thousand three hundred'], ['eight hundred eighty-eight thousand eight hundred and eighty-eight'], ['one million']]\n_outputs = [[0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [20], [21], [37], [46], [59], [68], [72], [83], [94], [100], [101], [101], [169], [299], [736], [2000], [1337], [10000], [26359], [35000], [99999], [666666], [700000], [200003], [200003], [203000], [500300], [888888], [1000000]]\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(parse_int(*i), o[0])"} | 190 | 565 |
coding | Solve the programming task below in a Python markdown code block.
This is the easy version of Problem F. The only difference between the easy version and the hard version is the constraints.
We will call a non-empty string balanced if it contains the same number of plus and minus signs. For example: strings "+--+" and "++-+--" are balanced, and strings "+--", "--" and "" are not balanced.
We will call a string promising if the string can be made balanced by several (possibly zero) uses of the following operation:
replace two adjacent minus signs with one plus sign.
In particular, every balanced string is promising. However, the converse is not true: not every promising string is balanced.
For example, the string "-+---" is promising, because you can replace two adjacent minuses with plus and get a balanced string "-++-", or get another balanced string "-+-+".
How many non-empty substrings of the given string $s$ are promising? Each non-empty promising substring must be counted in the answer as many times as it occurs in string $s$.
Recall that a substring is a sequence of consecutive characters of the string. For example, for string "+-+" its substring are: "+-", "-+", "+", "+-+" (the string is a substring of itself) and some others. But the following strings are not its substring: "--", "++", "-++".
-----Input-----
The first line of the input contains an integer $t$ ($1 \le t \le 500$) —the number of test cases in the test.
Then the descriptions of test cases follow.
Each test case of input data consists of two lines. The first line consists of the number $n$ ($1 \le n \le 3000$): the length of $s$.
The second line of the test case contains the string $s$ of length $n$, consisting only of characters "+" and "-".
It is guaranteed that the sum of values $n$ over all test cases does not exceed $3000$.
-----Output-----
For each test case, print a single number: the number of the promising non-empty substrings of string $s$. Each non-empty promising substring must be counted in the answer as many times as it occurs in string $s$.
-----Examples-----
Input
5
3
+-+
5
-+---
4
----
7
--+---+
6
+++---
Output
2
4
2
7
4
-----Note-----
The following are the promising substrings for the first three test cases in the example:
$s[1 \dots 2]$="+-", $s[2 \dots 3]$="-+";
$s[1 \dots 2]$="-+", $s[2 \dots 3]$="+-", $s[1 \dots 5]$="-+---", $s[3 \dots 5]$="---";
$s[1 \dots 3]$="---", $s[2 \dots 4]$="---". | {"inputs": ["5\n3\n+-+\n5\n-+---\n4\n----\n7\n--+---+\n6\n+++---\n"], "outputs": ["2\n4\n2\n7\n4\n"]} | 631 | 51 |
coding | Solve the programming task below in a Python markdown code block.
An integer N is a multiple of 9 if and only if the sum of the digits in the decimal representation of N is a multiple of 9.
Determine whether N is a multiple of 9.
-----Constraints-----
- 0 \leq N < 10^{200000}
- N is an integer.
-----Input-----
Input is given from Standard Input in the following format:
N
-----Output-----
If N is a multiple of 9, print Yes; otherwise, print No.
-----Sample Input-----
123456789
-----Sample Output-----
Yes
The sum of these digits is 1+2+3+4+5+6+7+8+9=45, which is a multiple of 9, so 123456789 is a multiple of 9. | {"inputs": ["1", "2", "3", "9", "6", "8", "4", "0"], "outputs": ["No\n", "No\n", "No\n", "Yes\n", "No\n", "No\n", "No\n", "Yes"]} | 193 | 61 |
coding | Solve the programming task below in a Python markdown code block.
“I am not in danger, Skyler. I am the danger. A guy opens his door and gets shot, and you think that of me? No! I am the one who knocks!”
Skyler fears Walter and ponders escaping to Colorado. Walter wants to clean his lab as soon as possible and then go back home to his wife.
In order clean his lab, he has to achieve cleaning level of lab as $Y$. The current cleaning level of the lab is $X$.
He must choose one positive odd integer $a$ and one positive even integer $b$. Note that, he cannot change $a$ or $b$ once he starts cleaning.
He can perform any one of the following operations for one round of cleaning:
- Replace $X$ with $X+a$.
- Replace $X$ with $X-b$.
Find minimum number of rounds (possibly zero) to make lab clean.
-----Input:-----
- First line will contain $T$, number of test cases. $T$ testcases follow :
- Each test case contains two space separated integers $X, Y$.
-----Output:-----
For each test case, output an integer denoting minimum number of rounds to clean the lab.
-----Constraints-----
- $1 \leq T \leq 10^5$
- $ |X|,|Y| \leq 10^9$
-----Sample Input:-----
3
0 5
4 -5
0 10000001
-----Sample Output:-----
1
2
1
-----EXPLANATION:-----
-
For the first testcase, you can convert $X$ to $Y$ by choosing $a=5$ and $b=2$.
It will cost minimum of $1$ cleaning round. You can select any other combination of $a, b$ satisfying above condition but will take minimum of $1$ cleaning round in any case.
-
For the second testcase, you can convert $X$ to $Y$ by choosing $a=1$ and $b=10$. In first round they will replace $X$ to $X+a$ and then in second round replace to $X-b$. You can perform only one operation in one round. | {"inputs": ["3\n0 5\n4 -5\n0 10000001"], "outputs": ["1\n2\n1"]} | 483 | 35 |
coding | Solve the programming task below in a Python markdown code block.
Chef has with him an array A of length N. In one move, he can delete any element from A.
Find the minimum number of deletions Chef must make so that the following condition holds:
Let B denote the resulting array, and M be the length of B.
Then, B_{i} \oplus B_{j} ≤ 1 for every 1 ≤ i, j ≤ M.
Here, \oplus denotes the [bitwise XOR] operation.
For example, [3, 3, 3] and [6, 7, 6, 7] are valid final arrays, while [1, 2] and [6, 7, 8] are not (because 1 \oplus 2 = 3 and 7\oplus 8 = 15, respectively).
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains a single integer N, denoting the length of array A.
- The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} — the elements of array A.
------ Output Format ------
For each test case, output on a new line the answer: the minimum number of deletions required so that the given condition is satisfied.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 3\cdot 10^{5}$
$0 ≤ A_{i} ≤ N$
- The sum of $N$ over all test cases won't exceed $3\cdot 10^{5}$.
----- Sample Input 1 ------
4
4
2 2 2 2
5
3 4 3 4 4
5
1 2 3 4 0
6
5 5 5 6 6 6
----- Sample Output 1 ------
0
2
3
3
----- explanation 1 ------
Test case $1$: The given array already satisfies the condition, no deletions need to be done.
Test case $2$: Chef can delete both the $3$'s to make the array $[4, 4, 4]$, which satisfies the condition.
Test case $3$: Chef can use three moves as follows:
- Delete the $1$, the array is now $[2, 3, 4, 0]$.
- Delete the $4$, the array is now $[2, 3, 0]$.
- Delete the $0$, the array is now $[2, 3]$ which satisfies the condition.
It can be verified that using two or less deletions cannot give an array that satisfies the condition.
Test case $4$: Chef must either delete all the $5$'s or all the $6$'s. | {"inputs": ["4\n4\n2 2 2 2\n5\n3 4 3 4 4\n5\n1 2 3 4 0\n6\n5 5 5 6 6 6\n"], "outputs": ["0\n2\n3\n3\n"]} | 635 | 68 |
coding | Solve the programming task below in a Python markdown code block.
A first-year student, came to your college. Being a good senior, you must tell him if it is possible to go from College Main Gate to Hostel for him.
The college can be visualized on a 2D-plane. Suppose the College Main Gate is situated at origin i.e. at the coordinates (0, 0) and the Hostel is situated at the coordinates (x,y).
As the first-year student wants to explore the college campus further, in one move, he will increase or decrease any coordinate (either the x-coordinate or the y-coordinate) by a value of exactly K.
Is it possible for the first-year student to reach the Hostel?
------ Input Format ------
- First line of input contains a single integer T, denoting the number of test cases. Then the description of the T test case follows.
- Each test case contains one line of input.
- The first line contains three integers x, y, K.
------ Output Format ------
For each test case, output a single line answer containing a "YES" or "NO" (without quotes).
You may print each character of each string in uppercase or lowercase (for example, the strings without quotes "yEs", "yes", "Yes" and "YES" will all be treated as identical).
------ Constraints ------
$1 ≤ T ≤ 600$
$-1000 ≤ x, y ≤ 1000$
$1 ≤ K ≤ 1000$
----- Sample Input 1 ------
4
1 2 1
3 -5 2
-9 -6 3
-18 12 5
----- Sample Output 1 ------
YES
NO
YES
NO
----- explanation 1 ------
Test case 1:
Here $K = 1$, One of the paths that the first-year student can follow is described as: $(0, 0)$ to $(1, 0)$ to $(1, 1)$ to $(1, 2)$. Hence, he can reach the hostel coordinate at $(x, y) = (1, 2)$ with the $K = 1$ constraint.
Test case 2:
As here $K = 2$, it can be proved that there is no path to move from $(0, 0)$ to $(3, -5$) by only increasing or decreasing any coordinate by exactly $K = 2$.
Test case 3:
Here $K = 3$, One of the paths that the first-year student can follow is described as: $(0, 0)$ to $(-3, 0)$ to $(-3, -3)$ to $(-3, -6)$ to $(-6, -6)$ to $(-9, -6)$. Hence, he can reach the hostel coordinate at $(x, y) = (-9, -6)$ with the $K = 3$ constraint. | {"inputs": ["4\n1 2 1\n3 -5 2\n-9 -6 3\n-18 12 5\n"], "outputs": ["YES\nNO\nYES\nNO"]} | 622 | 47 |
coding | Solve the programming task below in a Python markdown code block.
Write a function that returns the count of characters that have to be removed in order to get a string with no consecutive repeats.
*Note:* This includes any characters
## Examples
```python
'abbbbc' => 'abc' # answer: 3
'abbcca' => 'abca' # answer: 2
'ab cca' => 'ab ca' # answer: 1
```
Also feel free to reuse/extend the following starter code:
```python
def count_repeats(txt):
``` | {"functional": "_inputs = [['abcdefg'], ['aabbccddeeffgg'], ['abcdeefee'], ['122453124'], ['@*$##^^^*)*'], ['abmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmvxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa']]\n_outputs = [[0], [7], [2], [1], [3], [1177]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(count_repeats(*i), o[0])"} | 132 | 390 |
coding | Solve the programming task below in a Python markdown code block.
Diana is planning to make a very long journey. Her journey consists of $N$ bus routes, numbered from $1 to N$ in the order she must take them. The buses themselves are very fast but do not run often. The $i-th$ bus route only runs every $Xi$ days.
More specifically, she can only take the $i-th$ bus on day $Xi, 2Xi, 3Xi$, and so on. Since the buses are very fast, she can take multiple buses on the same day.
Diana must finish her journey by day D, but she would like to start the journey as late as possible. What is the latest day she could take the first bus, and still finish her journey by day $D$?
It is guaranteed that it is possible for Diana to finish her journey by day $D$.
-----Input:-----
The first line of the input gives the number of test cases, $T$. $T$ test cases follow. Each test case begins with a line containing the two integers N and D. Then, another line follows containing $N$ integers, the $i-th$ one is $Xi$.
-----Output:-----
For each test case, output one line containing an integer $Y$, where $Y$ is the latest day she could take the first bus, and still finish her journey by day $D$.
-----Constraints-----
- $1 \leq T \leq 100$
- $1 \leq Xi \leq D.$
- $1 \leq N \leq 1000.$
It is guaranteed that it is possible for Diana to finish her journey by day $D$.
-----Sample Input:-----
3
3 10
3 7 2
4 100
11 10 5 50
1 1
1
-----Sample Output:-----
6
99
1
-----EXPLANATION:-----
In Sample $Case 1$, there are $N = 3$ bus routes and Bucket must arrive by day $D = 10$. She could:
- Take the 1st bus on day 6 $(X1 = 3)$,
- Take the 2nd bus on day 7 $(X2 = 7)$ and
- Take the 3rd bus on day 8 $(X3 = 2)$.
In Sample $Case 2$, there are $N = 4$ bus routes and Bucket must arrive by day $D = 100$. She could:
- Take the 1st bus on day 99 $(X1 = 11)$,
- Take the 2nd bus on day 100$ (X2 = 10)$,
- Take the 3rd bus on day 100 $(X3 = 5)$ and
- Take the 4th bus on day 100 $(X4 = 50)$,
In Sample Case 3, there is $N = 1$ bus route, and Bucket must arrive by day $D = 1$. She could:
- Take the 1st bus on day 1 $(X1 = 1)$. | {"inputs": ["3\n3 10\n3 7 2\n4 100\n11 10 5 50\n1 1\n1"], "outputs": ["6\n99\n1"]} | 688 | 51 |
coding | Solve the programming task below in a Python markdown code block.
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
It's a lockdown. You’re bored in your house and are playing golf in the hallway.
The hallway has $N + 2$ tiles numbered from $0$ to $N+1$ from left to right. There is a hole on tile number $x$. You hit the ball standing on tile $0$. When you hit the ball, it bounces at lengths of $k$, i.e. the tiles covered by it are $0, k, 2k, \ldots$, and so on until the ball passes tile $N+1$.
If the ball doesn't enter the hole in the first trial, you try again but this time standing on the tile $N+1$. When you hit the ball, it bounces at lengths of $k$, i.e. the tiles covered by it are $(N + 1), (N + 1 - k), (N + 1 - 2k), \ldots$, and so on until the ball passes tile $0$.
Find if the ball will enter the hole, either in its forward journey or backward journey.
Note: The input and output of this problem are large, so prefer using fast input/output methods.
------ Input ------
The first line contains an integer $T$, the number of test cases. Then the test cases follow.
The only line of each test case contains three integers $N, x, k$.
------ Output ------
Output in a single line, the answer, which should be "YES" if the ball enters the hole either in the forward or backward journey and "NO" if not.
You may print each character of the string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical).
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ x, k ≤ N ≤ 10^{9}$
------ Subtasks ------
Subtask #1 (10 points): $N ≤ 10^{2}$
Subtask #2 (90 points): original constraints
----- Sample Input 1 ------
3
5 4 2
5 3 2
5 5 2
----- Sample Output 1 ------
YES
NO
NO
----- explanation 1 ------
In each test case, the tiles covered by the ball for $N = 5$ and $k = 2$ are $\{0, 2, 4, 6\}$ in the forward journey and $\{6, 4, 2, 0\}$ in the backward journey.
Therefore, the answer for the first test case is "YES" since the ball falls in the position of the hole at tile $4$. But the answer for test cases $2$ and $3$ is "NO" since the ball does not fall in the position of the hole. | {"inputs": ["3\n5 4 2\n5 3 2\n5 5 2"], "outputs": ["YES\nNO\nNO"]} | 646 | 34 |
coding | Solve the programming task below in a Python markdown code block.
Vasya has a non-negative integer n. He wants to round it to nearest integer, which ends up with 0. If n already ends up with 0, Vasya considers it already rounded.
For example, if n = 4722 answer is 4720. If n = 5 Vasya can round it to 0 or to 10. Both ways are correct.
For given n find out to which integer will Vasya round it.
-----Input-----
The first line contains single integer n (0 ≤ n ≤ 10^9) — number that Vasya has.
-----Output-----
Print result of rounding n. Pay attention that in some cases answer isn't unique. In that case print any correct answer.
-----Examples-----
Input
5
Output
0
Input
113
Output
110
Input
1000000000
Output
1000000000
Input
5432359
Output
5432360
-----Note-----
In the first example n = 5. Nearest integers, that ends up with zero are 0 and 10. Any of these answers is correct, so you can print 0 or 10. | {"inputs": ["5\n", "9\n", "1\n", "0\n", "3\n", "4\n", "6\n", "7\n"], "outputs": ["0\n", "10\n", "0\n", "0\n", "0\n", "0\n", "10\n", "10\n"]} | 289 | 73 |
coding | Solve the programming task below in a Python markdown code block.
Vasya’s elder brother Petya loves playing computer games. In one of his favourite computer games Petya reached the final level where a fight with the boss take place.
While playing the game Petya found spell scrolls and now he is about to use them. Let’s describe the way fighting goes on this level:
1) The boss has two parameters: max — the initial amount of health and reg — regeneration rate per second.
2) Every scroll also has two parameters: powi — spell power measured in percents — the maximal amount of health counted off the initial one, which allows to use the scroll (i.e. if the boss has more than powi percent of health the scroll cannot be used); and dmgi the damage per second inflicted upon the boss if the scroll is used. As soon as a scroll is used it disappears and another spell is cast upon the boss that inflicts dmgi of damage per second upon him until the end of the game.
During the battle the actions per second are performed in the following order: first the boss gets the damage from all the spells cast upon him, then he regenerates reg of health (at the same time he can’t have more than max of health), then the player may use another scroll (no more than one per second).
The boss is considered to be defeated if at the end of a second he has nonpositive ( ≤ 0) amount of health.
Help Petya to determine whether he can win with the set of scrolls available to him and if he can, determine the minimal number of seconds he needs to do it.
Input
The first line contains three integers N, max and reg (1 ≤ N, max, reg ≤ 1000) –– the amount of scrolls and the parameters of the boss. The next N lines contain two integers powi and dmgi each — the parameters of the i-th scroll (0 ≤ powi ≤ 100, 1 ≤ dmgi ≤ 2000).
Output
In case Petya can’t complete this level, output in the single line NO.
Otherwise, output on the first line YES. On the second line output the minimal time after which the boss can be defeated and the number of used scrolls. In the next lines for each used scroll output space-separated number of seconds passed from the start of the battle to the moment the scroll was used and the number of the scroll. Scrolls are numbered starting from 1 in the input order. The first scroll is considered to be available to be used after 0 seconds.
Output scrolls in the order they were used. It is not allowed to use scrolls after the boss is defeated.
Examples
Input
2 10 3
100 3
99 1
Output
NO
Input
2 100 10
100 11
90 9
Output
YES
19 2
0 1
10 2 | {"inputs": ["2 10 3\n100 3\n99 1\n", "2 100 2\n100 2\n100 2\n", "2 1000 1\n100 1\n100 1\n", "2 1000 1\n100 1\n100 2\n", "2 1001 0\n101 1\n100 2\n", "2 1000 1\n101 1\n100 2\n", "2 1001 1\n101 1\n100 2\n", "2 100 10\n100 11\n90 9\n"], "outputs": ["NO\n", "YES\n51 2\n0 1\n1 2\n", "YES\n1001 2\n0 1\n1 2\n", "YES\n501 2\n0 2\n1 1\n", "YES\n334 2\n0 2\n1 1\n", "YES\n501 2\n0 2\n1 1\n", "YES\n501 2\n0 2\n1 1\n", "YES\n19 2\n0 1\n10 2\n"]} | 628 | 316 |
coding | Solve the programming task below in a Python markdown code block.
Implement the function unique_in_order which takes as argument a sequence and returns a list of items without any elements with the same value next to each other and preserving the original order of elements.
For example:
```python
unique_in_order('AAAABBBCCDAABBB') == ['A', 'B', 'C', 'D', 'A', 'B']
unique_in_order('ABBCcAD') == ['A', 'B', 'C', 'c', 'A', 'D']
unique_in_order([1,2,2,3,3]) == [1,2,3]
```
Also feel free to reuse/extend the following starter code:
```python
def unique_in_order(iterable):
``` | {"functional": "_inputs = [[''], ['A'], ['AA'], ['AAAABBBCCDAABBB'], ['AADD'], ['AAD'], ['ADD'], ['ABBCcAD'], [[1, 2, 3, 3]], [['a', 'b', 'b']]]\n_outputs = [[[]], [['A']], [['A']], [['A', 'B', 'C', 'D', 'A', 'B']], [['A', 'D']], [['A', 'D']], [['A', 'D']], [['A', 'B', 'C', 'c', 'A', 'D']], [[1, 2, 3]], [['a', 'b']]]\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(unique_in_order(*i), o[0])"} | 164 | 283 |
coding | Solve the programming task below in a Python markdown code block.
Let's call a sequence of integers $x_1, x_2, \dots, x_k$ MEX-correct if for all $i$ ($1 \le i \le k$) $|x_i - \operatorname{MEX}(x_1, x_2, \dots, x_i)| \le 1$ holds. Where $\operatorname{MEX}(x_1, \dots, x_k)$ is the minimum non-negative integer that doesn't belong to the set $x_1, \dots, x_k$. For example, $\operatorname{MEX}(1, 0, 1, 3) = 2$ and $\operatorname{MEX}(2, 1, 5) = 0$.
You are given an array $a$ consisting of $n$ non-negative integers. Calculate the number of non-empty MEX-correct subsequences of a given array. The number of subsequences can be very large, so print it modulo $998244353$.
Note: a subsequence of an array $a$ is a sequence $[a_{i_1}, a_{i_2}, \dots, a_{i_m}]$ meeting the constraints $1 \le i_1 < i_2 < \dots < i_m \le n$. If two different ways to choose the sequence of indices $[i_1, i_2, \dots, i_m]$ yield the same subsequence, the resulting subsequence should be counted twice (i. e. two subsequences are different if their sequences of indices $[i_1, i_2, \dots, i_m]$ are not the same).
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 5 \cdot 10^5$).
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le n$).
The sum of $n$ over all test cases doesn't exceed $5 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the number of non-empty MEX-correct subsequences of a given array, taken modulo $998244353$.
-----Examples-----
Input
4
3
0 2 1
2
1 0
5
0 0 0 0 0
4
0 1 2 3
Output
4
2
31
7
-----Note-----
In the first example, the valid subsequences are $[0]$, $[1]$, $[0,1]$ and $[0,2]$.
In the second example, the valid subsequences are $[0]$ and $[1]$.
In the third example, any non-empty subsequence is valid. | {"inputs": ["4\n3\n0 2 1\n2\n1 0\n5\n0 0 0 0 0\n4\n0 1 2 3\n", "4\n3\n0 2 0\n2\n1 0\n5\n0 0 0 0 0\n4\n0 1 2 3\n", "4\n3\n0 2 1\n2\n1 0\n5\n0 0 0 0 0\n4\n0 1 1 3\n", "4\n3\n0 2 0\n2\n1 0\n5\n0 0 1 0 0\n4\n0 1 2 3\n", "4\n3\n0 2 1\n2\n1 1\n5\n0 0 0 0 0\n4\n0 1 1 3\n", "4\n3\n1 2 0\n2\n1 0\n5\n0 0 1 0 0\n4\n0 1 2 3\n", "4\n3\n0 2 1\n2\n1 0\n5\n0 0 0 0 0\n4\n0 1 2 3\n", "1\n30\n0 0 0 2 0 0 0 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2\n"], "outputs": ["4\n2\n31\n7\n", "5\n2\n31\n7\n", "4\n2\n31\n10\n", "5\n2\n19\n7\n", "4\n3\n31\n10\n", "2\n2\n19\n7\n", "4\n2\n31\n7\n", "998244352\n"]} | 664 | 444 |
coding | Solve the programming task below in a Python markdown code block.
Find the length of the longest contiguous segment in an array, in which if a given element $K$ is inserted, $K$ becomes the second largest element of that subarray.
-----Input:-----
- The first line will contain $T$, number of test cases. Then the test cases follow.
- The first line of each test case contains two integers $N$ and $K$.
- The next line contains N space-separated integers Ai denoting the elements of the array.
-----Output:-----
Print a single line corresponding to each test case — the length of the largest segment.
-----Constraints-----
- $1 \leq T \leq 10$
- $1 \leq N \leq 10^6$
- $1 \leq Ai, K \leq 10^9$
- Sum of N across all test cases doesn't exceed $10^6$
-----Sample Input:-----
2
5 3
2 4 2 4 2
8 5
9 3 5 7 8 11 17 2
-----Sample Output:-----
5
3
-----EXPLANATION:-----
If 3 is inserted at anywhere in the array, it is the second largest element. Hence the maximum length is 5.
If 5 is inserted anywhere between 1st and 4th element, it is the second largest element. The length of such subarray is 3. | {"inputs": ["2\n5 3\n2 4 2 4 2\n8 5\n9 3 5 7 8 11 17 2"], "outputs": ["5\n3"]} | 318 | 50 |
coding | Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given a positive integer n, find the pivot integer x such that:
The sum of all elements between 1 and x inclusively equals the sum of all elements between x and n inclusively.
Return the pivot integer x. If no such integer exists, return -1. It is guaranteed that there will be at most one pivot index for the given input.
Please complete the following python code precisely:
```python
class Solution:
def pivotInteger(self, n: int) -> int:
``` | {"functional": "def check(candidate):\n assert candidate(n = 8) == 6\n assert candidate(n = 1) == 1\n assert candidate(n = 4) == -1\n\n\ncheck(Solution().pivotInteger)"} | 119 | 55 |
coding | Solve the programming task below in a Python markdown code block.
Recently, the Fair Nut has written $k$ strings of length $n$, consisting of letters "a" and "b". He calculated $c$ — the number of strings that are prefixes of at least one of the written strings. Every string was counted only one time.
Then, he lost his sheet with strings. He remembers that all written strings were lexicographically not smaller than string $s$ and not bigger than string $t$. He is interested: what is the maximum value of $c$ that he could get.
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$ and $k$ ($1 \leq n \leq 5 \cdot 10^5$, $1 \leq k \leq 10^9$).
The second line contains a string $s$ ($|s| = n$) — the string consisting of letters "a" and "b.
The third line contains a string $t$ ($|t| = n$) — the string consisting of letters "a" and "b.
It is guaranteed that string $s$ is lexicographically not bigger than $t$.
-----Output-----
Print one number — maximal value of $c$.
-----Examples-----
Input
2 4
aa
bb
Output
6
Input
3 3
aba
bba
Output
8
Input
4 5
abbb
baaa
Output
8
-----Note-----
In the first example, Nut could write strings "aa", "ab", "ba", "bb". These $4$ strings are prefixes of at least one of the written strings, as well as "a" and "b". Totally, $6$ strings.
In the second example, Nut could write strings "aba", "baa", "bba".
In the third example, there are only two different strings that Nut could write. If both of them are written, $c=8$. | {"inputs": ["1 1\na\na\n", "1 1\na\na\n", "2 4\naa\nbb\n", "2 4\nab\nbb\n", "2 4\nba\nbb\n", "2 5\nab\nbb\n", "2 4\naa\nbb\n", "3 3\naba\nbba\n"], "outputs": ["1", "1\n", "6\n", "5\n", "3\n", "5\n", "6\n", "8\n"]} | 503 | 118 |
coding | Solve the programming task below in a Python markdown code block.
Tonio has a keyboard with only two letters, "V" and "K".
One day, he has typed out a string s with only these two letters. He really likes it when the string "VK" appears, so he wishes to change at most one letter in the string (or do no changes) to maximize the number of occurrences of that string. Compute the maximum number of times "VK" can appear as a substring (i. e. a letter "K" right after a letter "V") in the resulting string.
-----Input-----
The first line will contain a string s consisting only of uppercase English letters "V" and "K" with length not less than 1 and not greater than 100.
-----Output-----
Output a single integer, the maximum number of times "VK" can appear as a substring of the given string after changing at most one character.
-----Examples-----
Input
VK
Output
1
Input
VV
Output
1
Input
V
Output
0
Input
VKKKKKKKKKVVVVVVVVVK
Output
3
Input
KVKV
Output
1
-----Note-----
For the first case, we do not change any letters. "VK" appears once, which is the maximum number of times it could appear.
For the second case, we can change the second character from a "V" to a "K". This will give us the string "VK". This has one occurrence of the string "VK" as a substring.
For the fourth case, we can change the fourth character from a "K" to a "V". This will give us the string "VKKVKKKKKKVVVVVVVVVK". This has three occurrences of the string "VK" as a substring. We can check no other moves can give us strictly more occurrences. | {"inputs": ["V\n", "K\n", "K\n", "V\n", "VK\n", "VV\n", "KV\n", "KK\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "1\n", "1\n", "0\n", "1\n"]} | 393 | 70 |
coding | Solve the programming task below in a Python markdown code block.
The Little Elephant has an integer a, written in the binary notation. He wants to write this number on a piece of paper.
To make sure that the number a fits on the piece of paper, the Little Elephant ought to delete exactly one any digit from number a in the binary record. At that a new number appears. It consists of the remaining binary digits, written in the corresponding order (possible, with leading zeroes).
The Little Elephant wants the number he is going to write on the paper to be as large as possible. Help him find the maximum number that he can obtain after deleting exactly one binary digit and print it in the binary notation.
Input
The single line contains integer a, written in the binary notation without leading zeroes. This number contains more than 1 and at most 105 digits.
Output
In the single line print the number that is written without leading zeroes in the binary notation — the answer to the problem.
Examples
Input
101
Output
11
Input
110010
Output
11010
Note
In the first sample the best strategy is to delete the second digit. That results in number 112 = 310.
In the second sample the best strategy is to delete the third or fourth digits — that results in number 110102 = 2610. | {"inputs": ["11\n", "111\n", "110\n", "100\n", "101\n", "1111\n", "1011\n", "1001\n"], "outputs": ["1\n", "11\n", "11", "10", "11\n", "111\n", "111", "101"]} | 297 | 94 |
coding | Solve the programming task below in a Python markdown code block.
Vasya is studying in the last class of school and soon he will take exams. He decided to study polynomials. Polynomial is a function P(x) = a_0 + a_1x^1 + ... + a_{n}x^{n}. Numbers a_{i} are called coefficients of a polynomial, non-negative integer n is called a degree of a polynomial.
Vasya has made a bet with his friends that he can solve any problem with polynomials. They suggested him the problem: "Determine how many polynomials P(x) exist with integer non-negative coefficients so that $P(t) = a$, and $P(P(t)) = b$, where $t, a$ and b are given positive integers"?
Vasya does not like losing bets, but he has no idea how to solve this task, so please help him to solve the problem.
-----Input-----
The input contains three integer positive numbers $t, a, b$ no greater than 10^18.
-----Output-----
If there is an infinite number of such polynomials, then print "inf" without quotes, otherwise print the reminder of an answer modulo 10^9 + 7.
-----Examples-----
Input
2 2 2
Output
2
Input
2 3 3
Output
1 | {"inputs": ["2 2 2\n", "2 3 3\n", "1 1 1\n", "7 8 9\n", "3 3 3\n", "1 5 5\n", "1 2 2\n", "1 2 5\n"], "outputs": ["2\n", "1\n", "inf\n", "1\n", "2\n", "1\n", "1\n", "1\n"]} | 286 | 102 |
coding | Solve the programming task below in a Python markdown code block.
You are given two integers $n$ and $k$.
You should create an array of $n$ positive integers $a_1, a_2, \dots, a_n$ such that the sum $(a_1 + a_2 + \dots + a_n)$ is divisible by $k$ and maximum element in $a$ is minimum possible.
What is the minimum possible maximum element in $a$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first and only line of each test case contains two integers $n$ and $k$ ($1 \le n \le 10^9$; $1 \le k \le 10^9$).
-----Output-----
For each test case, print one integer — the minimum possible maximum element in array $a$ such that the sum $(a_1 + \dots + a_n)$ is divisible by $k$.
-----Examples-----
Input
4
1 5
4 3
8 8
8 17
Output
5
2
1
3
-----Note-----
In the first test case $n = 1$, so the array consists of one element $a_1$ and if we make $a_1 = 5$ it will be divisible by $k = 5$ and the minimum possible.
In the second test case, we can create array $a = [1, 2, 1, 2]$. The sum is divisible by $k = 3$ and the maximum is equal to $2$.
In the third test case, we can create array $a = [1, 1, 1, 1, 1, 1, 1, 1]$. The sum is divisible by $k = 8$ and the maximum is equal to $1$. | {"inputs": ["1\n7 99999999\n", "1\n2 33554431\n", "1\n2 69696969\n", "1\n7 99999999\n", "1\n2 69696969\n", "1\n2 33554431\n", "1\n2 54472221\n", "1\n7 49552848\n"], "outputs": ["14285715\n", "16777216\n", "34848485\n", "14285715\n", "34848485\n", "16777216\n", "27236111\n", "7078979\n"]} | 418 | 213 |
coding | Solve the programming task below in a Python markdown code block.
# Task
The game starts with `n` people standing in a circle. The presenter counts `m` people starting with the first one, and gives `1` coin to each of them. The rest of the players receive `2` coins each. After that, the last person who received `1` coin leaves the circle, giving everything he had to the next participant (who will also be considered "the first in the circle" in the next round). This process continues until only 1 person remains.
Determine the `number` of this person and how many `coins` he has at the end of the game.
The result should be a pair of values in the format: `[person, coins]`.
## Example:
```
n = 8, m = 3
People
1 2 3 1 2 ∙ 1 2 ∙ ∙ 2 ∙ ∙ 2 ∙ ∙ ∙ ∙ ∙ ∙ ∙
8 4 => 8 4 => 8 4 => 8 4 => 8 4 => 8 4 => ∙ 4 => 7
7 6 5 7 6 5 7 ∙ 5 7 ∙ 5 7 ∙ ∙ 7 ∙ ∙ 7 ∙ ∙
Coins
0 0 0 1 1 ∙ 3 3 ∙ ∙ 9 ∙ ∙ 10 ∙ ∙ ∙ ∙ ∙ ∙ ∙
0 0 => 2 3 => 4 4 => 5 6 => 7 7 => 8 20 => ∙ 30 => 51
0 0 0 2 2 2 7 ∙ 3 8 ∙ 5 16 ∙ ∙ 17 ∙ ∙ 18 ∙ ∙
```
## Notes:
* The presenter can do several full circles during one counting.
* In this case, the same person will receive `1` coin multiple times, i.e. no matter how many people are left in the circle, at least `m` coins will always be distributed among the participants during the round.
* If a person had already received `1` coin (possibly multiple times), he won't receive any additional `2` coins **at once(!)** during that round.
* People are counted starting with `1`.
* `m` can be **bigger** than `n` at start
Also feel free to reuse/extend the following starter code:
```python
def find_last(n, m):
``` | {"functional": "_inputs = [[5, 1], [8, 3], [75, 34], [82, 49], [73, 38], [86, 71], [61, 17], [42, 38], [29, 5], [64, 49], [61, 20], [88, 52]]\n_outputs = [[[5, 24]], [[7, 51]], [[35, 4238]], [[48, 5091]], [[61, 3996]], [[10, 6275]], [[26, 3000]], [[12, 1578]], [[28, 740]], [[43, 3327]], [[32, 2922]], [[59, 5856]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(find_last(*i), o[0])"} | 653 | 352 |
coding | Solve the programming task below in a Python markdown code block.
# Summation
Write a program that finds the summation of every number from 1 to num. The number will always be a positive integer greater than 0.
For example:
```if-not:racket
~~~
summation(2) -> 3
1 + 2
summation(8) -> 36
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8
~~~
```
```if:racket
~~~racket
(summation 2) ; 3 (+ 1 2)
(summation 8) ; 36 (+ 1 2 3 4 5 6 7 8)
~~~
```
Also feel free to reuse/extend the following starter code:
```python
def summation(num):
``` | {"functional": "_inputs = [[1], [8], [22], [100], [213]]\n_outputs = [[1], [36], [253], [5050], [22791]]\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(summation(*i), o[0])"} | 189 | 194 |
coding | Solve the programming task below in a Python markdown code block.
Polycarp takes part in a math show. He is given n tasks, each consists of k subtasks, numbered 1 through k. It takes him t_{j} minutes to solve the j-th subtask of any task. Thus, time required to solve a subtask depends only on its index, but not on the task itself. Polycarp can solve subtasks in any order.
By solving subtask of arbitrary problem he earns one point. Thus, the number of points for task is equal to the number of solved subtasks in it. Moreover, if Polycarp completely solves the task (solves all k of its subtasks), he recieves one extra point. Thus, total number of points he recieves for the complete solution of the task is k + 1.
Polycarp has M minutes of time. What is the maximum number of points he can earn?
-----Input-----
The first line contains three integer numbers n, k and M (1 ≤ n ≤ 45, 1 ≤ k ≤ 45, 0 ≤ M ≤ 2·10^9).
The second line contains k integer numbers, values t_{j} (1 ≤ t_{j} ≤ 1000000), where t_{j} is the time in minutes required to solve j-th subtask of any task.
-----Output-----
Print the maximum amount of points Polycarp can earn in M minutes.
-----Examples-----
Input
3 4 11
1 2 3 4
Output
6
Input
5 5 10
1 2 4 8 16
Output
7
-----Note-----
In the first example Polycarp can complete the first task and spend 1 + 2 + 3 + 4 = 10 minutes. He also has the time to solve one subtask of the second task in one minute.
In the second example Polycarp can solve the first subtask of all five tasks and spend 5·1 = 5 minutes. Also he can solve the second subtasks of two tasks and spend 2·2 = 4 minutes. Thus, he earns 5 + 2 = 7 points in total. | {"inputs": ["1 1 0\n2\n", "1 1 1\n1\n", "2 1 0\n2\n", "1 1 0\n4\n", "1 1 0\n5\n", "1 1 3\n5\n", "1 1 3\n5\n", "1 1 0\n5\n"], "outputs": ["0\n", "2\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]} | 476 | 118 |
coding | Solve the programming task below in a Python markdown code block.
Complete the function that takes two numbers as input, ```num``` and ```nth``` and return the `nth` digit of `num` (counting from right to left).
## Note
- If ```num``` is negative, ignore its sign and treat it as a positive value
- If ```nth``` is not positive, return `-1`
- Keep in mind that `42 = 00042`. This means that ```findDigit(42, 5)``` would return `0`
## Examples
```
findDigit(5673, 4) returns 5
findDigit(129, 2) returns 2
findDigit(-2825, 3) returns 8
findDigit(-456, 4) returns 0
findDigit(0, 20) returns 0
findDigit(65, 0) returns -1
findDigit(24, -8) returns -1
```
Also feel free to reuse/extend the following starter code:
```python
def find_digit(num, nth):
``` | {"functional": "_inputs = [[5673, 4], [129, 2], [-2825, 3], [0, 20], [65, 0], [24, -8], [-456, 5], [-1234, 2], [-5540, 1], [678998, 0], [-67854, -57], [0, -3]]\n_outputs = [[5], [2], [8], [0], [-1], [-1], [0], [3], [0], [-1], [-1], [-1]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(find_digit(*i), o[0])"} | 253 | 285 |
coding | Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given an integer array target and an integer n.
You have an empty stack with the two following operations:
"Push": pushes an integer to the top of the stack.
"Pop": removes the integer on the top of the stack.
You also have a stream of the integers in the range [1, n].
Use the two stack operations to make the numbers in the stack (from the bottom to the top) equal to target. You should follow the following rules:
If the stream of the integers is not empty, pick the next integer from the stream and push it to the top of the stack.
If the stack is not empty, pop the integer at the top of the stack.
If, at any moment, the elements in the stack (from the bottom to the top) are equal to target, do not read new integers from the stream and do not do more operations on the stack.
Return the stack operations needed to build target following the mentioned rules. If there are multiple valid answers, return any of them.
Please complete the following python code precisely:
```python
class Solution:
def buildArray(self, target: List[int], n: int) -> List[str]:
``` | {"functional": "def check(candidate):\n assert candidate(target = [1,3], n = 3) == [\"Push\",\"Push\",\"Pop\",\"Push\"]\n assert candidate(target = [1,2,3], n = 3) == [\"Push\",\"Push\",\"Push\"]\n assert candidate(target = [1,2], n = 4) == [\"Push\",\"Push\"]\n\n\ncheck(Solution().buildArray)"} | 260 | 99 |
coding | Solve the programming task below in a Python markdown code block.
You are given a matrix A that consists of N rows and M columns. Every number in the matrix is either zero or one. Calculate the number of such triples (i, j, h) where for all the pairs (x, y), where both x and y belong to [1; h] if y ≥ x, A[i+x-1][j+y-1] equals to one. Of course, the square (i, j, i+h-1, j+h-1) should be inside of this matrix. In other words, we're asking you to calculate the amount of square submatrices of a given matrix which have ones on and above their main diagonal.
------ Input ------
The first line of the input consists of two integers - N and M.
The following N lines describe the matrix. Each line consists of M characters that are either zero or one.
------ Output ------
Output should consist of a single integer - the answer to the problem.
------ Example ------
Input:
2 3
011
111
Output:
6
------ Scoring ------
Subtask 1 (9 points): 1 ≤ N,M ≤ 2000, All the numbers in the matrix are equal to one.
Subtask 2 (12 points): 1 ≤ N,M ≤ 10.
Subtask 3 (34 points): 1 ≤ N,M ≤ 30.
Subtask 4 (17 points): 1 ≤ N,M ≤ 150.
Subtask 5 (28 points): 1 ≤ N,M ≤ 2000. | {"inputs": ["2 3\n011\n111"], "outputs": ["6"]} | 350 | 22 |
coding | Solve the programming task below in a Python markdown code block.
Polycarp's workday lasts exactly $n$ minutes. He loves chocolate bars and can eat one bar in one minute. Today Polycarp has $k$ bars at the beginning of the workday.
In some minutes of the workday Polycarp has important things to do and in such minutes he is not able to eat a chocolate bar. In other minutes he can either eat or not eat one chocolate bar. It is guaranteed, that in the first and in the last minutes of the workday Polycarp has no important things to do and he will always eat bars in this minutes to gladden himself at the begining and at the end of the workday. Also it is guaranteed, that $k$ is strictly greater than $1$.
Your task is to determine such an order of eating chocolate bars that the maximum break time between eating bars is as minimum as possible.
Consider that Polycarp eats a bar in the minute $x$ and the next bar in the minute $y$ ($x < y$). Then the break time is equal to $y - x - 1$ minutes. It is not necessary for Polycarp to eat all bars he has.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \le n \le 200\,000$, $2 \le k \le n$) — the length of the workday in minutes and the number of chocolate bars, which Polycarp has in the beginning of the workday.
The second line contains the string with length $n$ consisting of zeros and ones. If the $i$-th symbol in the string equals to zero, Polycarp has no important things to do in the minute $i$ and he can eat a chocolate bar. In the other case, Polycarp is busy in the minute $i$ and can not eat a chocolate bar. It is guaranteed, that the first and the last characters of the string are equal to zero, and Polycarp always eats chocolate bars in these minutes.
-----Output-----
Print the minimum possible break in minutes between eating chocolate bars.
-----Examples-----
Input
3 3
010
Output
1
Input
8 3
01010110
Output
3
-----Note-----
In the first example Polycarp can not eat the chocolate bar in the second minute, so the time of the break equals to one minute.
In the second example Polycarp will eat bars in the minutes $1$ and $8$ anyway, also he needs to eat the chocolate bar in the minute $5$, so that the time of the maximum break will be equal to $3$ minutes. | {"inputs": ["2 2\n00\n", "2 2\n00\n", "2 3\n00\n", "3 3\n010\n", "3 2\n010\n", "3 2\n000\n", "3 3\n000\n", "3 3\n000\n"], "outputs": ["0\n", "0", "0\n", "1\n", "1\n", "1\n", "0\n", "0"]} | 574 | 113 |
coding | Solve the programming task below in a Python markdown code block.
A number K$K$ is said to be magical if it can be represented as a power of 2 only.That is K$K$=2x$2^{x}$ for some natural number x$x$.
Given a string of digits S$S$ of length N$N$, Let P be a valid arrangement of S.
By valid arrangement we mean that it should not have any leading zeroes.
Find the sum of all such distinct Pi's, which, when treated as a number is magical.
Two arrangements are said to be distinct, if there is atleast one index where they differ.
As the sum can be very large, print the sum modulo 109+7$10^{9}+7$.
-----Input:-----
-The first line of the input contains a single integer T$T$ denoting the number of test cases.
-Only line of each test case contains a string S$S$ of length N$N$, consisting only of digits between 0 to 9.
-----Output:-----
For each test case, print a single integer denoting the sum of all such magical Pi′s$Pi's$ modulo 109+7$10^{9}+7$.
If no such Pi$Pi$ exists print "-1".
-----Constraints-----
- 1≤T≤1000$1 \leq T \leq 1000$
- 2≤N≤1000$2 \leq N \leq 1000$
- String only consists of digits between 0 to 9, both inclusive.
-----Subtasks-----
- 10 points : 1≤N≤5$1 \leq N \leq 5$
- 40 points : 1≤N≤100$1 \leq N \leq 100$
- 50 points : Original Constraints
-----Sample Input:-----
2
35566
31
-----Sample Output:-----
65536
-1
-----EXPLANATION:-----
Only arrangement is 65536.
No arrangement of 31 gives us a power of two. | {"inputs": ["2\n35566\n31"], "outputs": ["65536\n-1"]} | 471 | 28 |
coding | Solve the programming task below in a Python markdown code block.
The Chef has a huge square napkin of size 2n X 2n. He folds the napkin n-3 times. Each time he folds its bottom side over its top side, and then its right side over its left side. After each fold, the side length of the napkin is reduced by half. The Chef continues folding until there remains a 8x8 sheet, lying flat on a table.
Oh, did I forget to mention that the Chef was cooking a new brown colored curry while folding the napkin. He drops some brown colored gravy onto some cells in the folded 8x8 napkin. When he drops the gravy, it soaks through all the cells below it.
Now the Chef unfolds the napkin to its original size. There are now many curry stained brown colored cells in the napkin. They form several separate regions, each of which is connected. Could you help the Chef count how many regions of brown cells are there in the napkin?
Note that two cells are adjacent if they share a common edge (they are not considered adjacent if they only share a corner). Two cells are connected if we can go from one cell to the other via adjacent cells. A region is a maximal set of cells such that every two of its cells are connected.
Please see the example test case for more details.
-----Input-----
The first line contains t, the number of test cases (about 50). Then t test cases follow. Each test case has the following form:
- The first line contains N (3 ≤ N ≤ 109)
- Then, 8 lines follow. Each line is a string of 8 characters, 0 or 1, where 1 denotes a stained brown cell in the folded napkin.
-----Output-----
For each test case, print a single number that is the number of disconnected brown regions in the unfolded napkin. Since the result may be a very large number, you only need to print its remainder when dividing by 21945.
-----Example-----
Input:
3
3
01000010
11000001
00000000
00011000
00011000
00010100
00001000
00000000
4
01000010
11000001
00000000
00011000
00011000
00010100
00001000
00000000
1000000000
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
Output:
6
22
1
-----Output details-----
Case 1 and 2: There are 6 brown regions in the 8x8 napkin. If we unfold it once, it has 22 brown regions: 11 regions in the top half and 11 regions in the bottom half (as shown in the figure above).
Case 3: All cells of the napkin are stained, so there is always one brown region, no matter how many times the Chef unfolds the napkin. | {"inputs": ["3\n3\n01000010\n11000001\n00000000\n00011000\n00011000\n00010100\n00001000\n00000000\n4\n01000010\n11000001\n00000000\n00011000\n00011000\n00010100\n00001000\n00000000\n1000000000\n11111111\n11111111\n11111111\n11111111\n11111111\n11111111\n11111111\n11111111\n\n"], "outputs": ["6\n22\n1"]} | 771 | 250 |
coding | Solve the programming task below in a Python markdown code block.
Platypus Perry is on a mission again. This time, Dr. Heinz Doofenshmirtz has plotted a bomb in the centre of Danville town. He wishes to rebuild the town.
We need to defuse the bomb for Perry. As always, Dr. Heinz has given perry the key combination to defuse the bomb, but unfortunately Perry has not been able to get out of doctor's evil claws.
Time is running out, and the key is to be built using two numbers Dr. Heinz gave to Perry.
The key is a series of numbers generated by multiplying second number B with P digits from the first number A, from left to right. Here, P is the number of digits in B.
INPUT:
First line gives T, total number of test cases.
T lines follow, each with 2 numbers A and B.
OUTPUT:
For each Test case, print all the numbers generated by multiplying P digits of A with B.
For example, if the number A is 12345 and B is 11, 11 is multiplied by 12 then 23, then 34 and so on.
Constraints:
1 ≤ T ≤ 10
1 ≤ A,B ≤ 10^9
SAMPLE INPUT
1
98765 23
SAMPLE OUTPUT
2254
2001
1748
1495
Explanation
2254 = 98 X 23
2001 = 87 X 23
and so on. | {"inputs": ["2\n987654321 123\n120000000 22"], "outputs": ["121401\n107748\n94095\n80442\n66789\n53136\n39483\n264\n440\n0\n0\n0\n0\n0\n0"]} | 345 | 101 |
coding | Solve the programming task below in a Python markdown code block.
Find the 2nd largest integer in array
If the array has no 2nd largest integer then return nil.
Reject all non integers elements and then find the 2nd largest integer in array
find_2nd_largest([1,2,3]) => 2
find_2nd_largest([1,1,1,1,1]) => nil
because all elements are same. Largest no. is 1. and there is no 2nd largest no.
find_2nd_largest([1,'a','2',3,3,4,5,'b']) => 4
as after rejecting non integers array will be [1,3,3,4,5]
Largest no. is 5. and 2nd largest is 4.
Return nil if there is no 2nd largest integer.
Take care of big numbers as well
Also feel free to reuse/extend the following starter code:
```python
def find_2nd_largest(arr):
``` | {"functional": "_inputs = [[[1, 2, 3]], [[1, 1, 1, 1, 1, 1, 1]], [[1, 'a', '2', 3, 3, 4, 5, 'b']], [[1, 'a', '2', 3, 3, 3333333333333333333334, 544444444444444444444444444444, 'b']]]\n_outputs = [[2], [None], [4], [3333333333333333333334]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(find_2nd_largest(*i), o[0])"} | 222 | 314 |
coding | Please solve the programming task below using a self-contained code snippet in a markdown code block.
The demons had captured the princess and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of m x n rooms laid out in a 2D grid. Our valiant knight was initially positioned in the top-left room and must fight his way through dungeon to rescue the princess.
The knight has an initial health point represented by a positive integer. If at any point his health point drops to 0 or below, he dies immediately.
Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).
To reach the princess as quickly as possible, the knight decides to move only rightward or downward in each step.
Return the knight's minimum initial health so that he can rescue the princess.
Note that any room can contain threats or power-ups, even the first room the knight enters and the bottom-right room where the princess is imprisoned.
Please complete the following python code precisely:
```python
class Solution:
def calculateMinimumHP(self, dungeon: List[List[int]]) -> int:
``` | {"functional": "def check(candidate):\n assert candidate(dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]) == 7\n assert candidate(dungeon = [[0]]) == 1\n\n\ncheck(Solution().calculateMinimumHP)"} | 258 | 66 |
coding | Solve the programming task below in a Python markdown code block.
This is a hard version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different.
You are given a string $s$ consisting of $n$ lowercase Latin letters.
You have to color all its characters the minimum number of colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$).
After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times.
The goal is to make the string sorted, i.e. all characters should be in alphabetical order.
Your task is to find the minimum number of colors which you have to color the given string in so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $s$.
The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters.
-----Output-----
In the first line print one integer $res$ ($1 \le res \le n$) — the minimum number of colors in which you have to color the given string so that after coloring it can become sorted by some sequence of swaps.
In the second line print any possible coloring that can be used to sort the string using some sequence of swaps described in the problem statement. The coloring is the array $c$ of length $n$, where $1 \le c_i \le res$ and $c_i$ means the color of the $i$-th character.
-----Examples-----
Input
9
abacbecfd
Output
2
1 1 2 1 2 1 2 1 2
Input
8
aaabbcbb
Output
2
1 2 1 2 1 2 1 1
Input
7
abcdedc
Output
3
1 1 1 1 1 2 3
Input
5
abcde
Output
1
1 1 1 1 1 | {"inputs": ["1\nr\n", "1\nr\n", "1\ns\n", "2\ntj\n", "2\ntj\n", "2\nti\n", "2\nit\n", "5\nabcde\n"], "outputs": ["1\n1 \n", "1\n1 \n", "1\n1 \n", "2\n1 2 \n", "2\n1 2 \n", "2\n1 2 \n", "1\n1 1 \n", "1\n1 1 1 1 1 \n"]} | 514 | 130 |
coding | Solve the programming task below in a Python markdown code block.
Takahashi will take part in an eating contest. Teams of N members will compete in this contest, and Takahashi's team consists of N players numbered 1 through N from youngest to oldest. The consumption coefficient of Member i is A_i.
In the contest, N foods numbered 1 through N will be presented, and the difficulty of Food i is F_i. The details of the contest are as follows:
- A team should assign one member to each food, and should not assign the same member to multiple foods.
- It will take x \times y seconds for a member to finish the food, where x is the consumption coefficient of the member and y is the difficulty of the dish.
- The score of a team is the longest time it takes for an individual member to finish the food.
Before the contest, Takahashi's team decided to do some training. In one set of training, a member can reduce his/her consumption coefficient by 1, as long as it does not go below 0. However, for financial reasons, the N members can do at most K sets of training in total.
What is the minimum possible score of the team, achieved by choosing the amounts of members' training and allocating the dishes optimally?
-----Constraints-----
- All values in input are integers.
- 1 \leq N \leq 2 \times 10^5
- 0 \leq K \leq 10^{18}
- 1 \leq A_i \leq 10^6\ (1 \leq i \leq N)
- 1 \leq F_i \leq 10^6\ (1 \leq i \leq N)
-----Input-----
Input is given from Standard Input in the following format:
N K
A_1 A_2 ... A_N
F_1 F_2 ... F_N
-----Output-----
Print the minimum possible score of the team.
-----Sample Input-----
3 5
4 2 1
2 3 1
-----Sample Output-----
2
They can achieve the score of 2, as follows:
- Member 1 does 4 sets of training and eats Food 2 in (4-4) \times 3 = 0 seconds.
- Member 2 does 1 set of training and eats Food 3 in (2-1) \times 1 = 1 second.
- Member 3 does 0 sets of training and eats Food 1 in (1-0) \times 2 = 2 seconds.
They cannot achieve a score of less than 2, so the answer is 2. | {"inputs": ["3 1\n4 2 1\n2 3 1", "3 6\n4 2 1\n2 3 1", "3 1\n8 2 1\n2 3 1", "3 8\n4 1 1\n2 3 1", "3 1\n2 2 1\n2 3 1", "3 6\n4 4 1\n2 3 1", "3 2\n8 2 1\n2 3 1", "3 1\n8 2 1\n1 3 1"], "outputs": ["4\n", "1\n", "7\n", "0\n", "3\n", "2\n", "6\n", "7\n"]} | 565 | 174 |
coding | Solve the programming task below in a Python markdown code block.
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types:
increase $x$ by $1$;
decrease $x$ by $1$.
You are given $m$ queries of the following format:
query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the description of $t$ testcases follows.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries.
The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively.
Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order.
-----Examples-----
Input
2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
1
2
4
4
3
3
4
2
3
2
1
2
2
2
-----Note-----
The instructions that remain for each query of the first testcase are:
empty program — $x$ was only equal to $0$;
"-" — $x$ had values $0$ and $-1$;
"---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them;
"+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$. | {"inputs": ["2\n8 4\n-+--+--+\n1 8\n2 8\n2 5\n1 1\n4 10\n+-++\n1 1\n1 2\n2 2\n1 3\n2 3\n3 3\n1 4\n2 4\n3 4\n4 4\n", "2\n8 4\n-+--+--+\n2 8\n2 8\n2 5\n1 1\n4 10\n+-++\n1 1\n1 2\n2 2\n1 3\n2 3\n3 3\n1 4\n2 4\n3 4\n4 4\n", "2\n8 4\n-+--+--+\n1 8\n2 8\n2 5\n2 1\n4 10\n+-++\n1 1\n1 2\n2 2\n1 3\n2 3\n3 3\n1 4\n2 4\n3 4\n4 4\n", "2\n8 4\n-+--+--+\n2 8\n2 8\n2 5\n1 1\n4 10\n+-++\n1 2\n1 2\n2 2\n1 3\n2 3\n3 3\n2 4\n2 4\n3 4\n4 4\n", "2\n8 4\n-+--+--+\n2 8\n2 8\n2 5\n1 1\n4 10\n+-++\n2 2\n1 1\n2 2\n1 3\n2 3\n3 3\n2 4\n2 4\n3 4\n4 4\n", "2\n8 4\n-+--+--+\n2 8\n2 8\n2 5\n1 1\n4 10\n+-++\n2 2\n1 1\n2 2\n1 3\n2 4\n3 3\n2 4\n2 4\n3 4\n4 4\n", "2\n8 4\n-+--+--+\n2 8\n2 8\n2 2\n1 1\n4 10\n+-++\n2 2\n1 1\n2 2\n1 3\n2 4\n3 3\n2 4\n2 4\n3 4\n4 4\n", "2\n8 4\n-+--+--+\n1 8\n2 8\n2 5\n2 1\n4 10\n+-++\n1 1\n1 2\n2 2\n1 3\n2 3\n3 3\n1 4\n2 4\n1 4\n4 4\n"], "outputs": ["1\n2\n4\n4\n3\n3\n4\n2\n3\n2\n1\n2\n2\n2\n", "2\n2\n4\n4\n3\n3\n4\n2\n3\n2\n1\n2\n2\n2\n", "1\n2\n4\n4\n3\n3\n4\n2\n3\n2\n1\n2\n2\n2\n", "2\n2\n4\n4\n3\n3\n4\n2\n3\n2\n2\n2\n2\n2\n", "2\n2\n4\n4\n4\n3\n4\n2\n3\n2\n2\n2\n2\n2\n", "2\n2\n4\n4\n4\n3\n4\n2\n2\n2\n2\n2\n2\n2\n", "2\n2\n5\n4\n4\n3\n4\n2\n2\n2\n2\n2\n2\n2\n", "1\n2\n4\n4\n3\n3\n4\n2\n3\n2\n1\n2\n1\n2\n"]} | 628 | 878 |
coding | Solve the programming task below in a Python markdown code block.
The title is a reference to the very first Educational Round from our writers team, Educational Round 18.
There is a bag, containing colored balls. There are $n$ different colors of balls, numbered from $1$ to $n$. There are $\mathit{cnt}_i$ balls of color $i$ in the bag. The total amount of balls in the bag is odd (e. g. $\mathit{cnt}_1 + \mathit{cnt}_2 + \dots + \mathit{cnt}_n$ is odd).
In one move, you can choose two balls with different colors and take them out of the bag.
At some point, all the remaining balls in the bag will have the same color. That's when you can't make moves anymore.
Find any possible color of the remaining balls.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 20$) — the number of colors.
The second line contains $n$ integers $\mathit{cnt}_1, \mathit{cnt}_2, \dots, \mathit{cnt}_n$ ($1 \le \mathit{cnt}_i \le 100$) — the amount of balls of each color in the bag.
The total amount of balls in the bag is odd (e. g. $\mathit{cnt}_1 + \mathit{cnt}_2 + \dots + \mathit{cnt}_n$ is odd).
-----Output-----
For each testcase, print a single integer — any possible color of the remaining balls, after you made some moves and can't make moves anymore.
-----Examples-----
Input
3
3
1 1 1
1
9
2
4 7
Output
3
1
2
-----Note-----
In the first testcase, your first and only move can be one of the following:
take balls with colors $1$ and $2$;
take balls with colors $1$ and $3$;
take balls with colors $2$ and $3$.
After the move, exactly one ball will remain. Its color can be $3, 2$ or $1$ depending on the move.
In the second testcase, you can't make moves at all — there is only color of balls already. This color is $1$.
In the third testcase, you can keep removing one ball of color $1$ and one ball of color $2$ until there are no more balls of color $1$. At the end, three balls of color $2$ remain. | {"inputs": ["1\n3\n1 2 4\n", "3\n3\n1 1 1\n1\n9\n2\n4 7\n"], "outputs": ["3\n", "1\n1\n2\n"]} | 586 | 52 |
coding | Solve the programming task below in a Python markdown code block.
You have two strings, $a$ and $\boldsymbol{b}$. Find a string, $\boldsymbol{\mathrm{~S~}}$, such that:
$\boldsymbol{\mathrm{~S~}}$ can be expressed as $s=s_{a}+s_{b}$ where $\boldsymbol{s_{a}}$ is a non-empty substring of $a$ and $s_{b}$ is a non-empty substring of $\boldsymbol{b}$.
$\boldsymbol{\mathrm{~S~}}$ is a palindromic string.
The length of $\boldsymbol{\mathrm{~S~}}$ is as long as possible.
For each of the $\textit{q}$ pairs of strings ($a_i$ and $b_i$) received as input, find and print string $s_i$ on a new line. If you're able to form more than one valid string $s_i$, print whichever one comes first alphabetically. If there is no valid answer, print $-1$ instead.
Input Format
The first line contains a single integer, $\textit{q}$, denoting the number of queries. The subsequent lines describe each query over two lines:
The first line contains a single string denoting $\boldsymbol{a}$.
The second line contains a single string denoting $\boldsymbol{b}$.
Constraints
$1\leq q\leq10$
$1\leq|a|,|b|\leq10^5$
$\boldsymbol{\alpha}$ and $\boldsymbol{b}$ contain only lowercase English letters.
Sum of |a| over all queries does not exceed $2\times10^5$
Sum of |b| over all queries does not exceed $2\times10^5$
Output Format
For each pair of strings ($a_i$ and $b_i$), find some $s_i$ satisfying the conditions above and print it on a new line. If there is no such string, print $-1$ instead.
Sample Input
3
bac
bac
abc
def
jdfh
fds
Sample Output
aba
-1
dfhfd
Explanation
We perform the following three queries:
Concatenate $s_a=\textbf{"a"}$ with $s_b=\text{"ba''}$ to create $s=\text{"aba"}$.
We're given $a=\text{"abc"}$ and $s_a=\text{"def"}$; because both strings are composed of unique characters, we cannot use them to form a palindromic string. Thus, we print $-1$.
Concatenate $s_a=\text{"dfh"}$ with $s_b=\text{"fd“}$ to create $s=\text{"dfhfd"}$. Note that we chose these particular substrings because the length of string $\boldsymbol{\mathrm{~S~}}$ must be maximal. | {"inputs": ["3\nbac\nbac\nabc\ndef\njdfh\nfds\n"], "outputs": ["aba\n-1\ndfhfd\n"]} | 625 | 35 |
coding | Solve the programming task below in a Python markdown code block.
Read problems statements in Mandarin Chinese and Russian.
Spring is interesting season of year. Chef is thinking about different things, but last time he thinks about interesting game - "Strange Matrix".
Chef has a matrix that consists of n rows, each contains m elements. Initially, the element a_{i}_{j} of matrix equals j. (1 ≤ i ≤ n, 1 ≤ j ≤ m).
Then p times some element a_{i}_{j} is increased by 1.
Then Chef needs to calculate the following:
For each row he tries to move from the last element (with number m) to the first one (with the number 1).
While staying in a_{i}_{j} Chef can only move to a_{i}_{j - 1} only if a_{i}_{j - 1} ≤ a_{i}_{j}.
The cost of such a movement is a_{i}_{j} - a_{i}_{j - 1}.
Otherwise Chef can't move and lose (in this row).
If Chef can move from the last element of the row to the first one, then the answer is the total cost of all the movements.
If Chef can't move from the last element of the row to the first one, then the answer is -1.
Help Chef to find answers for all the rows after P commands of increasing.
------ Input ------
The first line contains three integers n, m and p denoting the number of rows, the number of elements a single row and the number of increasing commands.
Each of next p lines contains two integers i and j denoting that the element a_{i}_{j } is increased by one.
------ Output ------
For each row in a single line print the answer after the P increasing commands.
------ Constraints ------
$1 ≤ n, m, p ≤ 10 ^ 5$
$1 ≤ i ≤ n$
$1 ≤ j ≤ m$
----- Sample Input 1 ------
4 4 6
2 2
3 2
3 2
4 3
4 4
4 3
----- Sample Output 1 ------
3
3
-1
4
----- explanation 1 ------
Here is the whole matrix after P commands:
1 2 3 4
1 3 3 4
1 4 3 4
1 2 5 5
Explanations to the answer:
The first line is without changes: 4-3=1, 3-2=1, 2-1=1. answer = 3.
The second line: 4-3=1, 3-3=0, 3-1=2. The answer is 3.
The third line: 4-3=1, 3-4=-1, Chef can't move to the first number here. Therefore, the answer is -1.
The fourth line: 5-5=0, 5-2=3, 2-1=1. The answer is 4. | {"inputs": ["4 4 6\n2 2\n3 2 \n3 2 \n4 3\n4 4\n4 3", "4 4 6\n3 2\n3 2 \n3 2 \n4 3\n4 4\n4 3", "4 4 6\n3 2\n3 2 \n3 2 \n4 3\n4 4\n4 1", "4 4 6\n3 2\n3 3 \n3 2 \n4 3\n4 4\n4 3", "4 6 6\n3 2\n2 3 \n3 2 \n4 3\n4 4\n4 3", "4 6 2\n3 2\n2 3 \n3 2 \n4 3\n4 4\n4 3", "3 6 2\n1 2\n3 0 \n6 2 \n4 3\n1 1\n5 3", "3 4 2\n1 2\n3 0 \n6 2 \n4 3\n1 1\n5 3"], "outputs": ["3\n3\n-1\n4", "3\n3\n-1\n4\n", "3\n3\n-1\n3\n", "3\n3\n3\n4\n", "5\n5\n-1\n5\n", "5\n5\n5\n5\n", "5\n5\n5\n", "3\n3\n3\n"]} | 661 | 349 |
coding | Solve the programming task below in a Python markdown code block.
Recently in JEC ants have become huge, the Principal is on a journey to snipe them !!
Principal has limited $N$ practice Bullets to practice so that he can be sure to kill ants.
- The Practice ground has max length $L$.
- There is a Limit X such that if the bullet is fired beyond this, it will destroy and it wont be of any further use.
- Bullet can be reused if fired in a range strictly less than X.
He wants to find minimum number of shots taken to find the distance X by using $N$ bullets.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- Each testcase contains of a single line of input, two integers $N, L$.
-----Output:-----
For each testcase, output in a single line answer the minimum number of shots to find the distance X.
-----Constraints-----
- $1 \leq T \leq 10$
- $2 \leq N,L \leq 100$
*N is always less than equal to square root of L
-----Subtasks-----
- 10 points : $ N = 1$
- 40 points : $ N = 2$
- 50 points : Original Constraints.
-----Sample Input:-----
2
1 10
2 10
-----Sample Output:-----
10
4
-----EXPLANATION:-----
-
There is only single bullet which is to be fired from distance 1 to 10 to get the distance X so in the worst case it can take up to 10 shots to find the distance X.
-
there are 2 bullets and distance 10 meters suppose if distance X is 10 we can get to that by firing first bullet at 4 then 7 then 9 then at 10 it will break it took only 4 turns, and if the distance X was 3, we can get that by firing first bullet at 4 it will get destroyed than we use 2nd bullet at 1 , 2, 3 and 2nd bullet will also break it also took 4 turns. You can check for any position minimum number of turns will be at most 4. | {"inputs": ["2\n1 10\n2 10"], "outputs": ["10\n4"]} | 489 | 25 |
coding | Solve the programming task below in a Python markdown code block.
Chef's pizza is the tastiest pizza to exist, and the reason for that is his special, juicy homegrown tomatoes.
Tomatoes can be grown in rectangular patches of any side lengths. However, Chef only has a limited amount of land.
Consider the entire town of Chefville to be consisting of cells in a rectangular grid of positive coordinates.
Chef own all cells (x,y)$(x, y)$ that satisfy x∗y≤N$x*y \leq N$
As an example if N=4$N = 4$, Chef owns the following cells:
(1,1),(1,2),(1,3),(1,4),(2,1),(2,2),(3,1),(4,1)$(1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (3, 1), (4, 1) $
Chef can only grow tomatoes in rectangular patches consisting only of cells which belong to him. Also, if he uses a cell, he must use it entirely. He cannot use only a portion of it.
Help Chef find the number of unique patches of rectangular land that he can grow tomatoes in! Since this number can be very large, output it modulo 1000000007$1000000007$.
-----Input:-----
- The first line of the input contains T$T$, the number of test cases.
- The next T$T$ lines of input contains one integer N$N$.
-----Output:-----
For each testcase, output the number of ways modulo 1000000007$1000000007$.
-----Constraints-----
- 1≤T≤5$1 \leq T \leq 5$
- 1≤N≤1010$1 \leq N \leq 10^{10}$
-----Sample Input:-----
2
4
10000000000
-----Sample Output:-----
23
227374950 | {"inputs": ["2\n4\n10000000000"], "outputs": ["23\n227374950"]} | 473 | 37 |
coding | Solve the programming task below in a Python markdown code block.
Sometimes one has to spell email addresses over the phone. Then one usually pronounces a dot as dot, an at sign as at. As a result, we get something like vasyaatgmaildotcom. Your task is to transform it into a proper email address (vasya@gmail.com).
It is known that a proper email address contains only such symbols as . @ and lower-case Latin letters, doesn't start with and doesn't end with a dot. Also, a proper email address doesn't start with and doesn't end with an at sign. Moreover, an email address contains exactly one such symbol as @, yet may contain any number (possible, zero) of dots.
You have to carry out a series of replacements so that the length of the result was as short as possible and it was a proper email address. If the lengths are equal, you should print the lexicographically minimal result.
Overall, two variants of replacement are possible: dot can be replaced by a dot, at can be replaced by an at.
Input
The first line contains the email address description. It is guaranteed that that is a proper email address with all the dots replaced by dot an the at signs replaced by at. The line is not empty and its length does not exceed 100 symbols.
Output
Print the shortest email address, from which the given line could be made by the described above replacements. If there are several solutions to that problem, print the lexicographically minimal one (the lexicographical comparison of the lines are implemented with an operator < in modern programming languages).
In the ASCII table the symbols go in this order: . @ ab...z
Examples
Input
vasyaatgmaildotcom
Output
vasya@gmail.com
Input
dotdotdotatdotdotat
Output
dot..@..at
Input
aatt
Output
a@t | {"inputs": ["aatt\n", "atatat\n", "doatdt\n", "taatta\n", "eoatdt\n", "taatua\n", "epatdt\n", "tbatua\n"], "outputs": ["a@t\n", "at@at\n", "do@dt\n", "ta@ta\n", "eo@dt\n", "ta@ua\n", "ep@dt\n", "tb@ua\n"]} | 398 | 99 |
coding | Solve the programming task below in a Python markdown code block.
You're given a sequence of N distinct integers. You need to find a subsequence of length K with the maximum possible median.
The median of a sequence is the value of the element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is selected. For example, if the sequence is [3, 4, 2, 1, 5], then the median is 3 since after sorting, it will look like [1, 2, 3, 4, 5] and 3 is the only middle element. The median of [4, 2, 1, 5] is 2 since after sorting, the value 2 is the left of the two middle elements 2 and 4.
A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if the sequence is [2, 1, 4, 5, 7], then [1, 5, 7], [2, 1, 4], [5] are some valid subsequences but [4, 1, 2], [1, 6, 7] are not.
------ Input Format ------
- The first line will contain T, the number of test cases. The description of test cases follow.
- The first line of each test case consists of two space-separated integers N, the length of the sequence, and K, the length of the subsequence to be found.
- The second line of each test case consists of N distinct space-separated integers A_{i}, the elements of the sequence.
------ Output Format ------
- For each test case, output two lines.
- The first line should consist of the median of the subsequence with the maximum median.
- The second line should consist of K space-separated integers, representing a subsequence with that median. If there are multiple such subsequences, print any.
------ Constraints ------
$1 ≤ T ≤ 30$
$1 ≤ K ≤ N ≤ 2 \times 10^{5}$
$1 ≤ A_{i} ≤ 10^{9}$
- Sum of $N$ over all test cases won't exceed $5 \times 10^{5}$
----- Sample Input 1 ------
1
5 3
1 4 3 6 5
----- Sample Output 1 ------
5
1 6 5 | {"inputs": ["1\n5 3\n1 4 3 6 5"], "outputs": ["5\n1 6 5"]} | 542 | 32 |
coding | Solve the programming task below in a Python markdown code block.
Kiyora has $n$ whiteboards numbered from $1$ to $n$. Initially, the $i$-th whiteboard has the integer $a_i$ written on it.
Koxia performs $m$ operations. The $j$-th operation is to choose one of the whiteboards and change the integer written on it to $b_j$.
Find the maximum possible sum of integers written on the whiteboards after performing all $m$ operations.
-----Input-----
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. The description of test cases follows.
The first line of each test case contains two integers $n$ and $m$ ($1 \le n,m \le 100$).
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
The third line of each test case contains $m$ integers $b_1, b_2, \ldots, b_m$ ($1 \le b_i \le 10^9$).
-----Output-----
For each test case, output a single integer — the maximum possible sum of integers written on whiteboards after performing all $m$ operations.
-----Examples-----
Input
4
3 2
1 2 3
4 5
2 3
1 2
3 4 5
1 1
100
1
5 3
1 1 1 1 1
1000000000 1000000000 1000000000
Output
12
9
1
3000000002
-----Note-----
In the first test case, Koxia can perform the operations as follows:
Choose the $1$-st whiteboard and rewrite the integer written on it to $b_1=4$.
Choose the $2$-nd whiteboard and rewrite to $b_2=5$.
After performing all operations, the numbers on the three whiteboards are $4$, $5$ and $3$ respectively, and their sum is $12$. It can be proven that this is the maximum possible sum achievable.
In the second test case, Koxia can perform the operations as follows:
Choose the $2$-nd whiteboard and rewrite to $b_1=3$.
Choose the $1$-st whiteboard and rewrite to $b_2=4$.
Choose the $2$-nd whiteboard and rewrite to $b_3=5$.
The sum is $4 + 5 = 9$. It can be proven that this is the maximum possible sum achievable. | {"inputs": ["4\n3 2\n1 2 3\n4 5\n2 3\n1 2\n3 4 5\n1 1\n100\n1\n5 3\n1 1 1 1 1\n1000000000 1000000000 1000000000\n"], "outputs": ["12\n9\n1\n3000000002\n"]} | 631 | 115 |
coding | Solve the programming task below in a Python markdown code block.
You are given an array A of N integers . You want to convert the array A into a permutation of N integers. To do this, you can apply the following operation on array A exactly once .
Pick an integer X, where 1 ≤ X ≤ 2\cdot 10^{7}. For each i , 1 ≤ i ≤ N , replace A_{i} by A_{i} \% X.
Find out whether it is possible to convert A into a permutation of N integers. If it is possible, print YES (all uppercase alphabets) and any possible value of X for which it is possible. Otherwise, print NO (all uppercase alphabets).
Note - An array of N integers is called a permutation if it contains all integers from 1 to N exactly once.
------ Input Format ------
- The first line contains an integer T denoting the number of test cases. The T test cases then follow.
- 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 testcase, output the required answer in a single line. If an answer exists, print YES and an integer X (1 ≤ X ≤ 2 \cdot 10^{7}) separated by a single space. Otherwise, print NO.
Note that the strings are case-sensitive and must be in uppercase alphabets.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{7}$
- Sum of $N$ over all cases won't exceed $10^{5}$
------ subtasks ------
Subtask #1 (100 points): original constraints
----- Sample Input 1 ------
2
3
2 7 1
2
2 2
----- Sample Output 1 ------
YES 4
NO
----- explanation 1 ------
- In the first case, choosing $X=4$ yields $A$ as $[ 2\% 4, 7\% 4 , 1\% 4] = [2,3,1]$ which is a permutation of $N=3$.
- In the second case, we can't make $A_{i}$ = $1$ for any $i$ and for any value of $X$. | {"inputs": ["2\n3\n2 7 1\n2\n2 2"], "outputs": ["YES 4\nNO"]} | 528 | 30 |
coding | Solve the programming task below in a Python markdown code block.
In Berland, $n$ different types of banknotes are used. Banknotes of the $i$-th type have denomination $10^{a_i}$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $1$.
Let's denote $f(s)$ as the minimum number of banknotes required to represent exactly $s$ burles. For example, if the denominations of banknotes used in Berland are $1$, $10$ and $100$, then $f(59) = 14$: $9$ banknotes with denomination of $1$ burle and $5$ banknotes with denomination of $10$ burles can be used to represent exactly $9 \cdot 1 + 5 \cdot 10 = 59$ burles, and there's no way to do it with fewer banknotes.
For a given integer $k$, find the minimum positive number of burles $s$ that cannot be represented with $k$ or fewer banknotes (that is, $f(s) > k$).
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 10; 1 \le k \le 10^9$).
The next line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 = a_1 < a_2 < \dots < a_n \le 9$).
-----Output-----
For each test case, print one integer — the minimum positive number of burles $s$ that cannot be represented with $k$ or fewer banknotes.
-----Examples-----
Input
4
3 13
0 1 2
2 777
0 4
3 255
0 1 3
10 1000000000
0 1 2 3 4 5 6 7 8 9
Output
59
778
148999
999999920999999999
-----Note-----
None | {"inputs": ["4\n3 1\n0 1 2\n2 777\n0 2\n3 60\n0 1 3\n10 0000000010\n0 1 2 3 4 5 6 7 8 9\n", "4\n3 1\n0 1 2\n2 777\n0 2\n3 60\n0 1 3\n10 0000000011\n0 1 2 3 4 5 6 7 8 9\n", "4\n3 2\n0 1 2\n2 777\n0 2\n3 60\n0 1 3\n10 0000000010\n0 1 2 3 4 5 6 7 8 9\n", "4\n3 2\n0 1 2\n2 983\n0 2\n3 60\n0 1 3\n10 0000000010\n0 1 2 3 4 5 6 7 8 9\n", "4\n3 2\n0 1 2\n2 235\n0 2\n3 60\n0 1 3\n10 0000000010\n0 1 2 3 4 5 6 7 8 9\n", "4\n3 1\n0 1 2\n2 777\n0 2\n3 60\n0 1 3\n10 0000000110\n0 1 2 3 4 5 6 7 8 9\n", "4\n3 19\n0 1 2\n2 777\n0 3\n3 31\n0 1 2\n3 0000000000\n0 1 2 3 4 5 6 7 8 9\n", "4\n3 1\n0 1 2\n2 641\n0 2\n3 60\n0 1 3\n10 0000000110\n0 1 2 3 4 5 6 7 8 9\n"], "outputs": ["2\n67999\n529\n29\n", "2\n67999\n529\n39\n", "3\n67999\n529\n29\n", "3\n88599\n529\n29\n", "3\n13799\n529\n29\n", "2\n67999\n529\n30999999999\n", "299\n778\n1499\n1\n", "2\n54399\n529\n30999999999\n"]} | 518 | 712 |
coding | Solve the programming task below in a Python markdown code block.
Multiplication of Big Integers
Given two integers $A$ and $B$, compute the product, $A \times B$.
Input
Two integers $A$ and $B$ separated by a space character are given in a line.
Output
Print the product in a line.
Constraints
* $-1 \times 10^{1000} \leq A, B \leq 10^{1000}$
Sample Input 1
5 8
Sample Output 1
40
Sample Input 2
100 25
Sample Output 2
2500
Sample Input 3
-1 0
Sample Output 3
0
Sample Input 4
12 -3
Sample Output 4
-36
Example
Input
5 8
Output
40 | {"inputs": ["1 1", "9 1", "6 1", "6 2", "6 4", "5 8", "5 16", "0 16"], "outputs": ["1\n", "9\n", "6\n", "12\n", "24\n", "40", "80\n", "0\n"]} | 195 | 83 |
coding | Solve the programming task below in a Python markdown code block.
Bob is a lazy man.
He needs you to create a method that can determine how many ```letters``` and ```digits``` are in a given string.
Example:
"hel2!lo" --> 6
"wicked .. !" --> 6
"!?..A" --> 1
Also feel free to reuse/extend the following starter code:
```python
def count_letters_and_digits(s):
``` | {"functional": "_inputs = [['n!!ice!!123'], ['de?=?=tttes!!t'], [''], ['!@#$%^&`~.'], ['u_n_d_e_r__S_C_O_R_E']]\n_outputs = [[7], [8], [0], [0], [10]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(count_letters_and_digits(*i), o[0])"} | 97 | 210 |
coding | Solve the programming task below in a Python markdown code block.
We have a chocolate bar partitioned into H horizontal rows and W vertical columns of squares.
The square (i, j) at the i-th row from the top and the j-th column from the left is dark if S_{i,j} is 0, and white if S_{i,j} is 1.
We will cut the bar some number of times to divide it into some number of blocks. In each cut, we cut the whole bar by a line running along some boundaries of squares from end to end of the bar.
How many times do we need to cut the bar so that every block after the cuts has K or less white squares?
-----Constraints-----
- 1 \leq H \leq 10
- 1 \leq W \leq 1000
- 1 \leq K \leq H \times W
- S_{i,j} is 0 or 1.
-----Input-----
Input is given from Standard Input in the following format:
H W K
S_{1,1}S_{1,2}...S_{1,W}
:
S_{H,1}S_{H,2}...S_{H,W}
-----Output-----
Print the number of minimum times the bar needs to be cut so that every block after the cuts has K or less white squares.
-----Sample Input-----
3 5 4
11100
10001
00111
-----Sample Output-----
2
For example, cutting between the 1-st and 2-nd rows and between the 3-rd and 4-th columns - as shown in the figure to the left - works.
Note that we cannot cut the bar in the ways shown in the two figures to the right. | {"inputs": ["3 5 8\n11100\n10101\n00111", "3 5 8\n11100\n00101\n00111", "3 5 4\n11100\n10001\n00110", "3 5 8\n11100\n10111\n00111", "3 0 8\n11100\n00101\n00111", "5 0 8\n11100\n00101\n00111", "8 0 8\n11100\n00101\n00111", "8 0 8\n11100\n00101\n01111"], "outputs": ["1\n", "0\n", "1\n", "1\n", "0\n", "0\n", "0\n", "0\n"]} | 383 | 238 |
coding | Solve the programming task below in a Python markdown code block.
Nauuo is a girl who loves playing cards.
One day she was playing cards but found that the cards were mixed with some empty ones.
There are n cards numbered from 1 to n, and they were mixed with another n empty cards. She piled up the 2n cards and drew n of them. The n cards in Nauuo's hands are given. The remaining n cards in the pile are also given in the order from top to bottom.
In one operation she can choose a card in her hands and play it — put it at the bottom of the pile, then draw the top card from the pile.
Nauuo wants to make the n numbered cards piled up in increasing order (the i-th card in the pile from top to bottom is the card i) as quickly as possible. Can you tell her the minimum number of operations?
Input
The first line contains a single integer n (1≤ n≤ 2⋅ 10^5) — the number of numbered cards.
The second line contains n integers a_1,a_2,…,a_n (0≤ a_i≤ n) — the initial cards in Nauuo's hands. 0 represents an empty card.
The third line contains n integers b_1,b_2,…,b_n (0≤ b_i≤ n) — the initial cards in the pile, given in order from top to bottom. 0 represents an empty card.
It is guaranteed that each number from 1 to n appears exactly once, either in a_{1..n} or b_{1..n}.
Output
The output contains a single integer — the minimum number of operations to make the n numbered cards piled up in increasing order.
Examples
Input
3
0 2 0
3 0 1
Output
2
Input
3
0 2 0
1 0 3
Output
4
Input
11
0 0 0 5 0 0 0 4 0 0 11
9 2 6 0 8 1 7 0 3 0 10
Output
18
Note
Example 1
We can play the card 2 and draw the card 3 in the first operation. After that, we have [0,3,0] in hands and the cards in the pile are [0,1,2] from top to bottom.
Then, we play the card 3 in the second operation. The cards in the pile are [1,2,3], in which the cards are piled up in increasing order.
Example 2
Play an empty card and draw the card 1, then play 1, 2, 3 in order. | {"inputs": ["1\n0\n1\n", "1\n1\n0\n", "2\n0 0\n1 2\n", "2\n0 0\n2 1\n", "2\n0 1\n0 2\n", "2\n0 2\n0 1\n", "2\n1 0\n0 2\n", "3\n0 0 0\n2 3 1\n"], "outputs": ["0\n", "1\n", "0\n", "4\n", "3\n", "1\n", "3\n", "6\n"]} | 584 | 130 |
coding | Solve the programming task below in a Python markdown code block.
Iahub got bored, so he invented a game to be played on paper.
He writes n integers a1, a2, ..., an. Each of those integers can be either 0 or 1. He's allowed to do exactly one move: he chooses two indices i and j (1 ≤ i ≤ j ≤ n) and flips all values ak for which their positions are in range [i, j] (that is i ≤ k ≤ j). Flip the value of x means to apply operation x = 1 - x.
The goal of the game is that after exactly one move to obtain the maximum number of ones. Write a program to solve the little game of Iahub.
```
@param {Array} line of the input there are n integers: a1, a2, ..., an. 1 ≤ n ≤ 100. It is guaranteed that each of those n values is either 0 or 1
@return {Integer} the maximal number of 1s that can be obtained after exactly one move
```
Examples :
```
[1, 0, 0, 1, 0, 0] => 5
[1, 0, 0, 1] => 4
```
Note:
In the first case, flip the segment from 2 to 6 (i = 2, j = 6). That flip changes the sequence, it becomes: [1 1 1 0 1 1]. So, it contains four ones. There is no way to make the whole sequence equal to [1 1 1 1 1 1].
In the second case, flipping only the second and the third element (i = 2, j = 3) will turn all numbers into 1.
(c)ll931110 & fchirica
Also feel free to reuse/extend the following starter code:
```python
def flipping_game(num):
``` | {"functional": "_inputs = [[[1, 0, 0, 1, 0, 0]], [[1, 0, 0, 1]], [[1]], [[0]], [[1, 0, 0, 0, 1, 0, 0, 0]], [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], [[0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1]]]\n_outputs = [[5], [4], [0], [1], [7], [18], [22], [18]]\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(flipping_game(*i), o[0])"} | 492 | 434 |
coding | Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given an integer array ribbons, where ribbons[i] represents the length of the ith ribbon, and an integer k. You may cut any of the ribbons into any number of segments of positive integer lengths, or perform no cuts at all.
For example, if you have a ribbon of length 4, you can:
Keep the ribbon of length 4,
Cut it into one ribbon of length 3 and one ribbon of length 1,
Cut it into two ribbons of length 2,
Cut it into one ribbon of length 2 and two ribbons of length 1, or
Cut it into four ribbons of length 1.
Your goal is to obtain k ribbons of all the same positive integer length. You are allowed to throw away any excess ribbon as a result of cutting.
Return the maximum possible positive integer length that you can obtain k ribbons of, or 0 if you cannot obtain k ribbons of the same length.
Please complete the following python code precisely:
```python
class Solution:
def maxLength(self, ribbons: List[int], k: int) -> int:
``` | {"functional": "def check(candidate):\n assert candidate(ribbons = [9,7,5], k = 3) == 5\n assert candidate(ribbons = [7,5,9], k = 4) == 4\n assert candidate(ribbons = [5,7,9], k = 22) == 0\n\n\ncheck(Solution().maxLength)"} | 250 | 88 |
coding | Solve the programming task below in a Python markdown code block.
.task {
background: #fff;
color: #333;
padding: 25px;
box-sizing: border-box;
font-family: Comfortaa, sans-serif !important;
position: relative;
}
.task code, .task_code {
display: inline-block;
padding: 0;
border-radius: 2px;
margin: 0;
position: relative;
top: 6px;
background: bisque;
border: 1px solid #f9d2a4;
padding: 3px;
line-height: 1em;
}
.task_code {
top: -1px;
}
.task_header {
color: #333 !important;
margin-top: 0;
font-size: 30px !important;
}
.task_inner {
box-shadow: 0 2px 11px -3px rgba(0,0,0,.6);
padding: 20px;
border-radius: 3px;
}
.task_devil {
float: right;
margin-left: 15px;
shape-outside: circle(150px);
}
.task_devil img {
max-width: 150px;
}
.task_part-header {
font-size: 20px !important;
font-weight: 800;
color: #333;
padding: 20px 0 10px;
}
.task_part-header:first-of-type {
padding-top: 0;
}
.task_list {
padding: 0;
margin: 10px 0;
padding-left: 30px;
}
.task_list ul {
padding: 0;
margin: 0;
}
.font-effect-canvas-print { color: black; }
Devil's Sequence
Problem
Robodevil likes to do some mathematics between rehearsals of his orchestra. Today he invented devilish sequence No. 1729:
x0 = 0,
x1 = 1,
xn = (xn - 1 + xn - 2) / 2.
For example, x10 = 0.666015625. Robodevil became interested at once how many `sixes` there were at the beginning of an arbitrary xn. In 6 nanoseconds, he had a formula. Can you do the same?
Input
You are given an integer n; `2 ≤ n ≤ 100000`.
Output
Output the number of sixes at the beginning of xn in decimal notation.
Example
Input
Output
10
3
Also feel free to reuse/extend the following starter code:
```python
def count_sixes(n):
``` | {"functional": "_inputs = [[10]]\n_outputs = [[3]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(count_sixes(*i), o[0])"} | 606 | 157 |
coding | Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given an m x n grid. Each cell of the grid has a sign pointing to the next cell you should visit if you are currently in this cell. The sign of grid[i][j] can be:
1 which means go to the cell to the right. (i.e go from grid[i][j] to grid[i][j + 1])
2 which means go to the cell to the left. (i.e go from grid[i][j] to grid[i][j - 1])
3 which means go to the lower cell. (i.e go from grid[i][j] to grid[i + 1][j])
4 which means go to the upper cell. (i.e go from grid[i][j] to grid[i - 1][j])
Notice that there could be some signs on the cells of the grid that point outside the grid.
You will initially start at the upper left cell (0, 0). A valid path in the grid is a path that starts from the upper left cell (0, 0) and ends at the bottom-right cell (m - 1, n - 1) following the signs on the grid. The valid path does not have to be the shortest.
You can modify the sign on a cell with cost = 1. You can modify the sign on a cell one time only.
Return the minimum cost to make the grid have at least one valid path.
Please complete the following python code precisely:
```python
class Solution:
def minCost(self, grid: List[List[int]]) -> int:
``` | {"functional": "def check(candidate):\n assert candidate(grid = [[1,1,1,1],[2,2,2,2],[1,1,1,1],[2,2,2,2]]) == 3\n assert candidate(grid = [[1,1,3],[3,2,2],[1,1,4]]) == 0\n assert candidate(grid = [[1,2],[4,3]]) == 1\n assert candidate(grid = [[2,2,2],[2,2,2]]) == 3\n assert candidate(grid = [[4]]) == 0\n\n\ncheck(Solution().minCost)"} | 340 | 141 |
coding | Solve the programming task below in a Python markdown code block.
The [Chinese zodiac](https://en.wikipedia.org/wiki/Chinese_zodiac) is a repeating cycle of 12 years, with each year being represented by an animal and its reputed attributes. The lunar calendar is divided into cycles of 60 years each, and each year has a combination of an animal and an element. There are 12 animals and 5 elements; the animals change each year, and the elements change every 2 years. The current cycle was initiated in the year of 1984 which was the year of the Wood Rat.
Since the current calendar is Gregorian, I will only be using years from the epoch 1924.
*For simplicity I am counting the year as a whole year and not from January/February to the end of the year.*
##Task
Given a year, return the element and animal that year represents ("Element Animal").
For example I'm born in 1998 so I'm an "Earth Tiger".
```animals``` (or ```$animals``` in Ruby) is a preloaded array containing the animals in order:
```['Rat', 'Ox', 'Tiger', 'Rabbit', 'Dragon', 'Snake', 'Horse', 'Goat', 'Monkey', 'Rooster', 'Dog', 'Pig']```
```elements``` (or ```$elements``` in Ruby) is a preloaded array containing the elements in order:
```['Wood', 'Fire', 'Earth', 'Metal', 'Water']```
Tell me your zodiac sign and element in the comments. Happy coding :)
Also feel free to reuse/extend the following starter code:
```python
def chinese_zodiac(year):
``` | {"functional": "_inputs = [[1965], [1938], [1998], [2016], [1924], [1968], [2162], [6479], [3050], [6673], [6594], [9911], [2323], [3448], [1972]]\n_outputs = [['Wood Snake'], ['Earth Tiger'], ['Earth Tiger'], ['Fire Monkey'], ['Wood Rat'], ['Earth Monkey'], ['Water Dog'], ['Earth Goat'], ['Metal Dog'], ['Water Rooster'], ['Wood Tiger'], ['Metal Goat'], ['Water Rabbit'], ['Earth Rat'], ['Water Rat']]\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(chinese_zodiac(*i), o[0])"} | 365 | 301 |
coding | Solve the programming task below in a Python markdown code block.
We have a 3×3 square grid, where each square contains a lowercase English letters.
The letter in the square at the i-th row from the top and j-th column from the left is c_{ij}.
Print the string of length 3 that can be obtained by concatenating the letters in the squares on the diagonal connecting the top-left and bottom-right corner of the grid, from the top-left to bottom-right.
-----Constraints-----
- Input consists of lowercase English letters.
-----Input-----
Input is given from Standard Input in the following format:
c_{11}c_{12}c_{13}
c_{21}c_{22}c_{23}
c_{31}c_{32}c_{33}
-----Output-----
Print the string of length 3 that can be obtained by concatenating the letters on the diagonal connecting the top-left and bottom-right corner of the grid, from the top-left to bottom-right.
-----Sample Input-----
ant
obe
rec
-----Sample Output-----
abc
The letters in the squares on the diagonal connecting the top-left and bottom-right corner of the grid are a, b and c from top-right to bottom-left. Concatenate these letters and print abc. | {"inputs": ["ant\nobe\nrfc", "ddu\ncat\nion", "ant\nobe\nrcf", "ddu\ncat\nioo", "ddu\ncat\nooi", "una\nobe\nrcf", "edu\nbau\nooi", "una\npbe\nfdr"], "outputs": ["abc\n", "dan\n", "abf\n", "dao\n", "dai\n", "ubf\n", "eai\n", "ubr\n"]} | 267 | 113 |
coding | Solve the programming task below in a Python markdown code block.
There is a robot staying at $X=0$ on the $Ox$ axis. He has to walk to $X=n$. You are controlling this robot and controlling how he goes. The robot has a battery and an accumulator with a solar panel.
The $i$-th segment of the path (from $X=i-1$ to $X=i$) can be exposed to sunlight or not. The array $s$ denotes which segments are exposed to sunlight: if segment $i$ is exposed, then $s_i = 1$, otherwise $s_i = 0$.
The robot has one battery of capacity $b$ and one accumulator of capacity $a$. For each segment, you should choose which type of energy storage robot will use to go to the next point (it can be either battery or accumulator). If the robot goes using the battery, the current charge of the battery is decreased by one (the robot can't use the battery if its charge is zero). And if the robot goes using the accumulator, the current charge of the accumulator is decreased by one (and the robot also can't use the accumulator if its charge is zero).
If the current segment is exposed to sunlight and the robot goes through it using the battery, the charge of the accumulator increases by one (of course, its charge can't become higher than it's maximum capacity).
If accumulator is used to pass some segment, its charge decreases by 1 no matter if the segment is exposed or not.
You understand that it is not always possible to walk to $X=n$. You want your robot to go as far as possible. Find the maximum number of segments of distance the robot can pass if you control him optimally.
-----Input-----
The first line of the input contains three integers $n, b, a$ ($1 \le n, b, a \le 2 \cdot 10^5$) — the robot's destination point, the battery capacity and the accumulator capacity, respectively.
The second line of the input contains $n$ integers $s_1, s_2, \dots, s_n$ ($0 \le s_i \le 1$), where $s_i$ is $1$ if the $i$-th segment of distance is exposed to sunlight, and $0$ otherwise.
-----Output-----
Print one integer — the maximum number of segments the robot can pass if you control him optimally.
-----Examples-----
Input
5 2 1
0 1 0 1 0
Output
5
Input
6 2 1
1 0 0 1 0 1
Output
3
-----Note-----
In the first example the robot can go through the first segment using the accumulator, and charge levels become $b=2$ and $a=0$. The second segment can be passed using the battery, and charge levels become $b=1$ and $a=1$. The third segment can be passed using the accumulator, and charge levels become $b=1$ and $a=0$. The fourth segment can be passed using the battery, and charge levels become $b=0$ and $a=1$. And the fifth segment can be passed using the accumulator.
In the second example the robot can go through the maximum number of segments using battery two times and accumulator one time in any order. | {"inputs": ["1 1 1\n0\n", "1 1 1\n0\n", "2 1 1\n0 0\n", "2 1 1\n0 0\n", "2 0 1\n0 0\n", "3 1 1\n1 1 1\n", "3 1 1\n1 1 1\n", "4 1 1\n1 1 1 0\n"], "outputs": ["1\n", "1\n", "2\n", "2\n", "1\n", "3\n", "3\n", "3\n"]} | 706 | 138 |
coding | Solve the programming task below in a Python markdown code block.
# Task
Given an array of 2^(k) integers (for some integer `k`), perform the following operations until the array contains only one element:
```
On the 1st, 3rd, 5th, etc.
iterations (1-based) replace each pair of consecutive elements with their sum;
On the 2nd, 4th, 6th, etc.
iterations replace each pair of consecutive elements with their product.
```
After the algorithm has finished, there will be a single element left in the array. Return that element.
# Example
For inputArray = [1, 2, 3, 4, 5, 6, 7, 8], the output should be 186.
We have `[1, 2, 3, 4, 5, 6, 7, 8] -> [3, 7, 11, 15] -> [21, 165] -> [186]`, so the answer is 186.
# Input/Output
- `[input]` integer array `arr`
Constraints: 2^(1) ≤ arr.length ≤ 2^(5), -9 ≤ arr[i] ≤ 99.
- `[output]` an integer
Also feel free to reuse/extend the following starter code:
```python
def array_conversion(arr):
``` | {"functional": "_inputs = [[[1, 2, 3, 4, 5, 6, 7, 8]], [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], [[3, 3, 5, 5]]]\n_outputs = [[186], [64], [60]]\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(array_conversion(*i), o[0])"} | 310 | 245 |
coding | Solve the programming task below in a Python markdown code block.
We have an N \times N square grid.
We will paint each square in the grid either black or white.
If we paint exactly A squares white, how many squares will be painted black?
-----Constraints-----
- 1 \leq N \leq 100
- 0 \leq A \leq N^2
-----Inputs-----
Input is given from Standard Input in the following format:
N
A
-----Outputs-----
Print the number of squares that will be painted black.
-----Sample Input-----
3
4
-----Sample Output-----
5
There are nine squares in a 3 \times 3 square grid.
Four of them will be painted white, so the remaining five squares will be painted black. | {"inputs": ["3\n2", "3\n3", "3\n0", "3\n1", "3\n6", "3\n5", "3\n7", "3\n9"], "outputs": ["7\n", "6\n", "9\n", "8\n", "3\n", "4\n", "2\n", "0\n"]} | 163 | 78 |
coding | Solve the programming task below in a Python markdown code block.
Print the K-th element of the following sequence of length 32:
1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51
Constraints
* 1 \leq K \leq 32
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
K
Output
Print the K-th element.
Examples
Input
6
Output
2
Input
27
Output
5 | {"inputs": ["8", "1", "5", "8", "1", "5", "6", "16"], "outputs": ["5\n", "1\n", "1\n", "5\n", "1\n", "1\n", "2", "14\n"]} | 191 | 63 |
coding | Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given a string s, find the length of the longest substring without repeating characters.
Please complete the following python code precisely:
```python
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
``` | {"functional": "def check(candidate):\n assert candidate(s = \"abcabcbb\") == 3\n assert candidate(s = \"bbbbb\") == 1\n assert candidate(s = \"pwwkew\") == 3\n\n\ncheck(Solution().lengthOfLongestSubstring)"} | 66 | 64 |
coding | Solve the programming task below in a Python markdown code block.
## Check Digits
Some numbers are more important to get right during data entry than others: a common example is product codes.
To reduce the possibility of mistakes, product codes can be crafted in such a way that simple errors are detected. This is done by calculating a single-digit value based on the product number, and then appending that digit to the product number to arrive at the product code.
When the product code is checked, the check digit value is stripped off and recalculated. If the supplied value does not match the recalculated value, the product code is rejected.
A simple scheme for generating self-check digits, described here, is called Modulus 11 Self-Check.
## Calculation method
Each digit in the product number is assigned a multiplication factor. The factors are assigned ***from right to left***, starting at `2` and counting up. For numbers longer than six digits, the factors restart at `2` after `7` is reached. The product of each digit and its factor is calculated, and the products summed. For example:
```python
digit : 1 6 7 7 0 3 6 2 5
factor : 4 3 2 7 6 5 4 3 2
--- --- --- --- --- --- --- --- ---
4 + 18 + 14 + 49 + 0 + 15 + 24 + 6 + 10 = 140
```
Then the sum of the products is divided by the prime number `11`. The remainder is inspected, and:
* if the remainder is `0`, the check digit is also `0`
* if the remainder is `1`, the check digit is replaced by an uppercase `X`
* for all others, the remainder is subtracted from `11`
The result is the **check digit**.
## Your task
Your task is to implement this algorithm and return the input number with the correct check digit appended.
## Examples
```python
input: "036532"
product sum = 2*2 + 3*3 + 5*4 + 6*5 + 3*6 + 0*7 = 81
remainder = 81 mod 11 = 4
check digit = 11 - 4 = 7
output: "0365327"
```
Also feel free to reuse/extend the following starter code:
```python
def add_check_digit(number):
``` | {"functional": "_inputs = [['036532'], ['12388878'], ['111111111'], ['9735597355'], ['2356'], ['6789']]\n_outputs = [['0365327'], ['123888782'], ['1111111118'], ['97355973550'], ['23566'], ['6789X']]\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(add_check_digit(*i), o[0])"} | 573 | 261 |
coding | Please solve the programming task below using a self-contained code snippet in a markdown code block.
The diameter of a tree is the number of edges in the longest path in that tree.
There is an undirected tree of n nodes labeled from 0 to n - 1. You are given a 2D array edges where edges.length == n - 1 and edges[i] = [ai, bi] indicates that there is an undirected edge between nodes ai and bi in the tree.
Return the diameter of the tree.
Please complete the following python code precisely:
```python
class Solution:
def treeDiameter(self, edges: List[List[int]]) -> int:
``` | {"functional": "def check(candidate):\n assert candidate(edges = [[0,1],[0,2]]) == 2\n assert candidate(edges = [[0,1],[1,2],[2,3],[1,4],[4,5]]) == 4\n\n\ncheck(Solution().treeDiameter)"} | 138 | 68 |
coding | Solve the programming task below in a Python markdown code block.
Two foxes Jiro and Saburo are playing a game called 1D Reversi. This game is played on a board, using black and white stones. On the board, stones are placed in a row, and each player places a new stone to either end of the row. Similarly to the original game of Reversi, when a white stone is placed, all black stones between the new white stone and another white stone, turn into white stones, and vice versa.
In the middle of a game, something came up and Saburo has to leave the game. The state of the board at this point is described by a string S. There are |S| (the length of S) stones on the board, and each character in S represents the color of the i-th (1 ≦ i ≦ |S|) stone from the left. If the i-th character in S is B, it means that the color of the corresponding stone on the board is black. Similarly, if the i-th character in S is W, it means that the color of the corresponding stone is white.
Jiro wants all stones on the board to be of the same color. For this purpose, he will place new stones on the board according to the rules. Find the minimum number of new stones that he needs to place.
-----Constraints-----
- 1 ≦ |S| ≦ 10^5
- Each character in S is B or W.
-----Input-----
The input is given from Standard Input in the following format:
S
-----Output-----
Print the minimum number of new stones that Jiro needs to place for his purpose.
-----Sample Input-----
BBBWW
-----Sample Output-----
1
By placing a new black stone to the right end of the row of stones, all white stones will become black. Also, by placing a new white stone to the left end of the row of stones, all black stones will become white.
In either way, Jiro's purpose can be achieved by placing one stone. | {"inputs": ["BWBBW", "WWBBB", "WBBWB", "WBBXB", "BXBBW", "BXBWB", "BXWBB", "WXBBB"], "outputs": ["3\n", "1\n", "3\n", "3\n", "3\n", "4\n", "3\n", "2\n"]} | 427 | 78 |
coding | Solve the programming task below in a Python markdown code block.
Tom has finally taken over the business empire and now looking for
a new Name of the business to make a new start.
Joe (Tom's dear friend) suggested a string $S$ consisting of
Uppercase and lowercase letters
Tom wants to make some changes as per the following criteria:
1) String should $not$ have any vowels .
2) Every other uppercase consonant(other characters except vowels) should
be in lowercase
For ex:
If the consonant character is Z then it should be z
3) There should be a character "." before each consonant.
Help Tom to make the required Changes.
-----Input:-----
- First line will contain string $S$,This string only consists of uppercase and lowercase letters.
-----Output:-----
Print the resulting string. It is guaranteed that this string is not empty.
-----Constraints-----
- Length of string is in [1 .. 100]
-----Sample Input:-----
$CodeSprInT$
-----Sample Output:-----
.c.d.s.p.r.n.t
-----EXPLANATION:-----
C is a consonant and it is in uppercase so turn it in lower case and add a “.” before it
o is a vowel so it is deleted
d is a consonant and in lowercase so just add a “.” before it
e is a vowel so it is deleted
S is a consonant and it is in uppercase so turn it in lower case and add a “.” before it
p is a consonant and in lowercase so just add a “.” before it
r is a consonant and in lowercase so just add a “.” before it
I is a vowel so it is deleted
n is a consonant and in lowercase so just add a “.” before it
T is a consonant and it is in uppercase so turn it in lower case and add a “.” before it | {"inputs": ["CodeSprInT"], "outputs": [".c.d.s.p.r.n.t"]} | 408 | 22 |
coding | Solve the programming task below in a Python markdown code block.
Chouti is working on a strange math problem.
There was a sequence of $n$ positive integers $x_1, x_2, \ldots, x_n$, where $n$ is even. The sequence was very special, namely for every integer $t$ from $1$ to $n$, $x_1+x_2+...+x_t$ is a square of some integer number (that is, a perfect square).
Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $x_2, x_4, x_6, \ldots, x_n$. The task for him is to restore the original sequence. Again, it's your turn to help him.
The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
-----Input-----
The first line contains an even number $n$ ($2 \le n \le 10^5$).
The second line contains $\frac{n}{2}$ positive integers $x_2, x_4, \ldots, x_n$ ($1 \le x_i \le 2 \cdot 10^5$).
-----Output-----
If there are no possible sequence, print "No".
Otherwise, print "Yes" and then $n$ positive integers $x_1, x_2, \ldots, x_n$ ($1 \le x_i \le 10^{13}$), where $x_2, x_4, \ldots, x_n$ should be same as in input data. If there are multiple answers, print any.
Note, that the limit for $x_i$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $1 \le x_i \le 10^{13}$.
-----Examples-----
Input
6
5 11 44
Output
Yes
4 5 16 11 64 44
Input
2
9900
Output
Yes
100 9900
Input
6
314 1592 6535
Output
No
-----Note-----
In the first example $x_1=4$ $x_1+x_2=9$ $x_1+x_2+x_3=25$ $x_1+x_2+x_3+x_4=36$ $x_1+x_2+x_3+x_4+x_5=100$ $x_1+x_2+x_3+x_4+x_5+x_6=144$ All these numbers are perfect squares.
In the second example, $x_1=100$, $x_1+x_2=10000$. They are all perfect squares. There're other answers possible. For example, $x_1=22500$ is another answer.
In the third example, it is possible to show, that no such sequence exists. | {"inputs": ["2\n1\n", "2\n1\n", "2\n780\n", "2\n524\n", "2\n203\n", "2\n9900\n", "2\n9900\n", "4\n35 15\n"], "outputs": ["No\n", "No\n", "Yes\n4 780\n", "Yes\n16900 524\n", "Yes\n121 203\n", "Yes\n100 9900\n", "Yes\n100 9900 ", "Yes\n1 35 13 15\n"]} | 690 | 154 |
coding | Please solve the programming task below using a self-contained code snippet in a markdown code block.
Alice and Bob continue their games with stones. There is a row of n stones, and each stone has an associated value. You are given an integer array stones, where stones[i] is the value of the ith stone.
Alice and Bob take turns, with Alice starting first. On each turn, the player may remove any stone from stones. The player who removes a stone loses if the sum of the values of all removed stones is divisible by 3. Bob will win automatically if there are no remaining stones (even if it is Alice's turn).
Assuming both players play optimally, return true if Alice wins and false if Bob wins.
Please complete the following python code precisely:
```python
class Solution:
def stoneGameIX(self, stones: List[int]) -> bool:
``` | {"functional": "def check(candidate):\n assert candidate(stones = [2,1]) == True\n assert candidate(stones = [2]) == False\n assert candidate(stones = [5,1,2,4,3]) == False\n\n\ncheck(Solution().stoneGameIX)"} | 179 | 66 |
coding | Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.)
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 swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
``` | {"functional": "def check(candidate):\n assert is_same_list(candidate(head = list_node([1,2,3,4])), list_node([2,1,4,3]))\n assert is_same_list(candidate(head = list_node([])), list_node([]))\n assert is_same_list(candidate(head = list_node([1])), list_node([1]))\n\n\ncheck(Solution().swapPairs)"} | 134 | 89 |
coding | Solve the programming task below in a Python markdown code block.
Let's call an array good if there is an element in the array that equals to the sum of all other elements. For example, the array $a=[1, 3, 3, 7]$ is good because there is the element $a_4=7$ which equals to the sum $1 + 3 + 3$.
You are given an array $a$ consisting of $n$ integers. Your task is to print all indices $j$ of this array such that after removing the $j$-th element from the array it will be good (let's call such indices nice).
For example, if $a=[8, 3, 5, 2]$, the nice indices are $1$ and $4$: if you remove $a_1$, the array will look like $[3, 5, 2]$ and it is good; if you remove $a_4$, the array will look like $[8, 3, 5]$ and it is good.
You have to consider all removals independently, i. e. remove the element, check if the resulting array is good, and return the element into the array.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of elements in the array $a$.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^6$) — elements of the array $a$.
-----Output-----
In the first line print one integer $k$ — the number of indices $j$ of the array $a$ such that after removing the $j$-th element from the array it will be good (i.e. print the number of the nice indices).
In the second line print $k$ distinct integers $j_1, j_2, \dots, j_k$ in any order — nice indices of the array $a$.
If there are no such indices in the array $a$, just print $0$ in the first line and leave the second line empty or do not print it at all.
-----Examples-----
Input
5
2 5 1 2 2
Output
3
4 1 5
Input
4
8 3 5 2
Output
2
1 4
Input
5
2 1 2 4 3
Output
0
-----Note-----
In the first example you can remove any element with the value $2$ so the array will look like $[5, 1, 2, 2]$. The sum of this array is $10$ and there is an element equals to the sum of remaining elements ($5 = 1 + 2 + 2$).
In the second example you can remove $8$ so the array will look like $[3, 5, 2]$. The sum of this array is $10$ and there is an element equals to the sum of remaining elements ($5 = 3 + 2$). You can also remove $2$ so the array will look like $[8, 3, 5]$. The sum of this array is $16$ and there is an element equals to the sum of remaining elements ($8 = 3 + 5$).
In the third example you cannot make the given array good by removing exactly one element. | {"inputs": ["2\n4 5\n", "2\n2 2\n", "2\n1 1\n", "2\n1 5\n", "2\n1 2\n", "2\n5 1\n", "2\n1 5\n", "2\n1 1\n"], "outputs": ["0\n\n", "0\n\n", "0\n\n", "0\n\n", "0\n\n", "0\n\n", "0\n\n", "0\n\n"]} | 756 | 110 |
coding | Solve the programming task below in a Python markdown code block.
Chef is playing an easier variation of the board game ‘Risk’ with his friend Mike. There is an $N * M$ grid, depicting the world map. Each cell of the grid is either $1$ or $0$ where $1$ denotes that there is land on this cell, while $0$ denotes water.
In a turn, a player can capture an uncaptured cell that has land, and keep capturing neighbouring cells that share a side with it if they are on land. A cell once captured by a player can't be captured again by another player. The game continues till no uncaptured cell has land. Each player wants to be in control of as many cells of land as possible in total when the game finishes. Find the maximum number of cells of land that Chef can capture if he plays second, and both players play optimally.
------ Input: ------
First line will contain $T$, number of testcases. Then the testcases follow.
Each testcase contains $N + 1$ lines of input.
First line will contain $2$ space separated integers $N$ and $M$ denoting the size of the grid.
Each of the next $N$ lines contains a binary string of length $M$ depicting the map.
------ Output: ------
For each testcase, output in a single line answer to the problem.
------ Constraints ------
$1 ≤ N, M ≤ 10^{5}$
Each character in the grid is either $0$ or $1$
There's atleast $1$ land on the grid
Sum of $N * M$ over all testcases is almost $10^{6}$.
----- Sample Input 1 ------
3
4 4
1001
0110
0110
1001
2 2
11
11
3 3
100
010
001
----- Sample Output 1 ------
2
0
1
----- explanation 1 ------
In test case 1, if both players play optimally, it takes $5$ steps to finish the game:
Step 1: First Mike chooses the cells $\{(2, 2), (2, 3), (3, 2), (3, 3)\}$ adding $4$ to his score.
Step 2: Second Chef chooses the cell $\{(1, 1)\}$ adding $1$ to his score.
Step 3: Next Mike chooses the cell $\{(1, 4)\}$ adding $1$ to his score.
Step 4: Next Chef chooses the cell $\{(4, 1)\}$ adding $1$ to his score.
Step 5: Finally, Mike chooses the cell $\{(4, 4)\}$ adding $1$ to his score.
Hence total score of Chef is $1 + 1 = 2$. | {"inputs": ["3\n4 4\n1001\n0110\n0110\n1001\n2 2\n11\n11\n3 3\n100\n010\n001"], "outputs": ["2\n0\n1"]} | 617 | 66 |
coding | Solve the programming task below in a Python markdown code block.
You are given an array of n integers a1... an. The cost of a subsegment is the number of unordered pairs of distinct indices within the subsegment that contain equal elements. Split the given array into k non-intersecting non-empty subsegments so that the sum of their costs is minimum possible. Each element should be present in exactly one subsegment.
Input
The first line contains two integers n and k (2 ≤ n ≤ 105, 2 ≤ k ≤ min (n, 20)) — the length of the array and the number of segments you need to split the array into.
The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n) — the elements of the array.
Output
Print single integer: the minimum possible total cost of resulting subsegments.
Examples
Input
7 3
1 1 3 3 3 2 1
Output
1
Input
10 2
1 2 1 2 1 2 1 2 1 2
Output
8
Input
13 3
1 2 2 2 1 2 1 1 1 2 2 1 1
Output
9
Note
In the first example it's optimal to split the sequence into the following three subsegments: [1], [1, 3], [3, 3, 2, 1]. The costs are 0, 0 and 1, thus the answer is 1.
In the second example it's optimal to split the sequence in two equal halves. The cost for each half is 4.
In the third example it's optimal to split the sequence in the following way: [1, 2, 2, 2, 1], [2, 1, 1, 1, 2], [2, 1, 1]. The costs are 4, 4, 1. | {"inputs": ["2 2\n2 2\n", "7 3\n1 1 3 3 4 2 1\n", "7 3\n1 1 3 3 1 2 1\n", "7 3\n1 1 2 3 4 2 1\n", "7 3\n1 2 2 3 4 2 1\n", "7 3\n1 1 3 3 6 2 1\n", "7 3\n1 2 2 3 4 1 1\n", "7 6\n1 1 3 3 1 2 1\n"], "outputs": ["0", "0\n", "1\n", "0\n", "0\n", "0\n", "0\n", "0\n"]} | 429 | 187 |
coding | Solve the programming task below in a Python markdown code block.
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.
-----Output-----
If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
-----Examples-----
Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1
-----Note-----
In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150. | {"inputs": ["1\n", "1\n", "2\n", "6\n", "10\n", "25\n", "52\n", "57\n"], "outputs": ["-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "0\n", "1\n", "1\n"]} | 315 | 75 |
coding | Solve the programming task below in a Python markdown code block.
Most of the time when rounding a given number, it is customary to round to some multiple of a power of 10. However, there is no reason why we cannot use another multiple to do our rounding to. For example, you could round to the nearest multiple of 7, or the nearest multiple of 3.
Given an integer N and an integer B, round N to the nearest value which is a multiple of B. If N is exactly halfway between two multiples of B, return the larger value.
Input:- First line contains number of test cases and each test case two interger values N and B.
Ouput:- Print the desired output.
SAMPLE INPUT
3
5 10
4 10
100 3
SAMPLE OUTPUT
10
0
99
Explanation
Testcase 3:-100 is closer to 99 than 102. | {"inputs": ["26\n5 10\n4 10\n100 3\n123456 7\n49 7\n158854 50\n822992 101\n691238 345\n682373 49\n643378 53\n328697 62\n446656 228\n368684 130\n530193 371\n864163 325\n708578 207\n915093 485\n711860 28\n234554 366\n309089 25\n595700 393\n10 6\n9 19\n4 10\n11 2\n11 3"], "outputs": ["10\n0\n99\n123459\n49\n158850\n822948\n691380\n682374\n643367\n328724\n446652\n368680\n530159\n864175\n708561\n915195\n711872\n234606\n309100\n595788\n12\n0\n0\n12\n12"]} | 201 | 379 |
coding | Solve the programming task below in a Python markdown code block.
Chef recently learned about ratios and proportions. He wrote some positive integers a, b, c, d on a paper. Chef wants to know whether he can shuffle these numbers so as to make some proportion? Formally, four numbers x, y, z, w are said to make a proportion if ratio of x : y is same as that of z : w.
-----Input-----
Only line of the input contains four space separated positive integers - a, b, c, d.
-----Output-----
Print "Possible" if it is possible to shuffle a, b, c, d to make proportion, otherwise "Impossible" (without quotes).
-----Constraints-----
- 1 ≤ a, b, c, d ≤ 1000
-----Example-----
Input:
1 2 4 2
Output:
Possible
-----Explanation-----
By swapping 4 and the second 2, we get 1 2 2 4. Note that 1 2 2 4 make proportion as 1 : 2 = 2 : 4. Hence answer is "Possible" | {"inputs": ["1 2 4 2"], "outputs": ["Possible"]} | 236 | 18 |
coding | Solve the programming task below in a Python markdown code block.
One company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received n resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the number of variants of group composition to evaluate.
-----Input-----
The only line of the input contains one integer n (7 ≤ n ≤ 777) — the number of potential employees that sent resumes.
-----Output-----
Output one integer — the number of different variants of group composition.
-----Examples-----
Input
7
Output
29 | {"inputs": ["7\n", "8\n", "9\n", "8\n", "9\n", "7\n", "10\n", "10\n"], "outputs": ["29", "92", "246", "92\n", "246\n", "29\n", "582", "582\n"]} | 144 | 80 |
coding | Solve the programming task below in a Python markdown code block.
poly
The poly tool returns the coefficients of a polynomial with the given sequence of roots.
print numpy.poly([-1, 1, 1, 10]) #Output : [ 1 -11 9 11 -10]
roots
The roots tool returns the roots of a polynomial with the given coefficients.
print numpy.roots([1, 0, -1]) #Output : [-1. 1.]
polyint
The polyint tool returns an antiderivative (indefinite integral) of a polynomial.
print numpy.polyint([1, 1, 1]) #Output : [ 0.33333333 0.5 1. 0. ]
polyder
The polyder tool returns the derivative of the specified order of a polynomial.
print numpy.polyder([1, 1, 1, 1]) #Output : [3 2 1]
polyval
The polyval tool evaluates the polynomial at specific value.
print numpy.polyval([1, -2, 0, 2], 4) #Output : 34
polyfit
The polyfit tool fits a polynomial of a specified order to a set of data using a least-squares approach.
print numpy.polyfit([0,1,-1, 2, -2], [0,1,1, 4, 4], 2)
#Output : [ 1.00000000e+00 0.00000000e+00 -3.97205465e-16]
The functions polyadd, polysub, polymul, and polydiv also handle proper addition,
subtraction, multiplication, and division of polynomial coefficients, respectively.
Task
You are given the coefficients of a polynomial $\mbox{P}$.
Your task is to find the value of $\mbox{P}$ at point $\boldsymbol{x}$.
Input Format
The first line contains the space separated value of the coefficients in $\mbox{P}$.
The second line contains the value of $\boldsymbol{x}$.
Output Format
Print the desired value.
Sample Input
1.1 2 3
0
Sample Output
3.0 | {"inputs": ["1.1 2 3\n0\n"], "outputs": ["3.0\n"]} | 508 | 24 |
coding | Solve the programming task below in a Python markdown code block.
This time the Berland Team Olympiad in Informatics is held in a remote city that can only be reached by one small bus. Bus has n passenger seats, seat i can be occupied only by a participant from the city a_{i}.
Today the bus has completed m trips, each time bringing n participants. The participants were then aligned in one line in the order they arrived, with people from the same bus standing in the order of their seats (i. e. if we write down the cities where the participants came from, we get the sequence a_1, a_2, ..., a_{n} repeated m times).
After that some teams were formed, each consisting of k participants form the same city standing next to each other in the line. Once formed, teams left the line. The teams were formed until there were no k neighboring participants from the same city.
Help the organizers determine how many participants have left in the line after that process ended. We can prove that answer doesn't depend on the order in which teams were selected.
-----Input-----
The first line contains three integers n, k and m (1 ≤ n ≤ 10^5, 2 ≤ k ≤ 10^9, 1 ≤ m ≤ 10^9).
The second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5), where a_{i} is the number of city, person from which must take seat i in the bus.
-----Output-----
Output the number of remaining participants in the line.
-----Examples-----
Input
4 2 5
1 2 3 1
Output
12
Input
1 9 10
1
Output
1
Input
3 2 10
1 2 1
Output
0
-----Note-----
In the second example, the line consists of ten participants from the same city. Nine of them will form a team. At the end, only one participant will stay in the line. | {"inputs": ["1 5 8\n1\n", "1 9 10\n1\n", "1 9 13\n1\n", "1 2 13\n1\n", "1 5 13\n1\n", "1 3 13\n1\n", "1 8 10\n1\n", "1 9 10\n2\n"], "outputs": ["3\n", "1\n", "4\n", "1\n", "3\n", "1\n", "2\n", "1\n"]} | 440 | 125 |
coding | Solve the programming task below in a Python markdown code block.
Apple considers any iPhone with a battery health of 80\% or above, to be in *optimal* condition.
Given that your iPhone has X\% battery health, find whether it is in *optimal* condition.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- The first and only line of each test case contains an integer X — the battery health.
------ Output Format ------
For each test case, output on a new line, YES, if the battery is in *optimal* condition, and NO otherwise.
You may print each character in uppercase or lowercase. For example, NO, no, No and nO, are all considered identical.
------ Constraints ------
$1 ≤ T ≤ 100$
$0 ≤ X ≤ 100$
----- Sample Input 1 ------
4
97
42
80
10
----- Sample Output 1 ------
YES
NO
YES
NO
----- explanation 1 ------
Test case $1$: The battery health is $97 \%$ which is greater than equal to $80 \%$. Thus, the battery is in optimal condition.
Test case $2$: The battery health is $42 \%$ which is less than $80 \%$. Thus, the battery is not in optimal condition.
Test case $3$: The battery health is $80 \%$ which is greater than equal to $80 \%$. Thus, the battery is in optimal condition.
Test case $4$: The battery health is $10 \%$ which is less than $80 \%$. Thus, the battery is not in optimal condition. | {"inputs": ["4\n97\n42\n80\n10\n"], "outputs": ["YES\nNO\nYES\nNO\n"]} | 367 | 32 |
coding | Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.
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 deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
``` | {"functional": "def check(candidate):\n assert is_same_list(candidate(head = list_node([1,1,2])), list_node([1,2]))\n assert is_same_list(candidate(head = list_node([1,1,2,3,3])), list_node([1,2,3]))\n\n\ncheck(Solution().deleteDuplicates)"} | 119 | 77 |
coding | Solve the programming task below in a Python markdown code block.
Create a __moreZeros__ function which will __receive a string__ for input, and __return an array__ (or null terminated string in C) containing only the characters from that string whose __binary representation of its ASCII value__ consists of _more zeros than ones_.
You should __remove any duplicate characters__, keeping the __first occurence__ of any such duplicates, so they are in the __same order__ in the final array as they first appeared in the input string.
Examples
All input will be valid strings of length > 0. Leading zeros in binary should not be counted.
Also feel free to reuse/extend the following starter code:
```python
def more_zeros(s):
``` | {"functional": "_inputs = [['abcde'], ['thequickbrownfoxjumpsoverthelazydog'], ['THEQUICKBROWNFOXJUMPSOVERTHELAZYDOG'], ['abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_'], ['DIGEST'], ['abcdabcd'], ['Forgiveness is the fragrance that the violet sheds on the heal that has crushed it']]\n_outputs = [[['a', 'b', 'd']], [['h', 'b', 'p', 'a', 'd']], [['T', 'H', 'E', 'Q', 'I', 'C', 'B', 'R', 'F', 'X', 'J', 'P', 'L', 'A', 'D']], [['a', 'b', 'd', 'h', 'p', 'A', 'B', 'C', 'D', 'E', 'F', 'H', 'I', 'J', 'L', 'P', 'Q', 'R', 'T', 'X', '0']], [['D', 'I', 'E', 'T']], [['a', 'b', 'd']], [['F', ' ', 'h', 'a', 'd']]]\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(more_zeros(*i), o[0])"} | 157 | 393 |
coding | Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given the array orders, which represents the orders that customers have done in a restaurant. More specifically orders[i]=[customerNamei,tableNumberi,foodItemi] where customerNamei is the name of the customer, tableNumberi is the table customer sit at, and foodItemi is the item customer orders.
Return the restaurant's “display table”. The “display table” is a table whose row entries denote how many of each food item each table ordered. The first column is the table number and the remaining columns correspond to each food item in alphabetical order. The first row should be a header whose first column is “Table”, followed by the names of the food items. Note that the customer names are not part of the table. Additionally, the rows should be sorted in numerically increasing order.
Please complete the following python code precisely:
```python
class Solution:
def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
``` | {"functional": "def check(candidate):\n assert candidate(orders = [[\"David\",\"3\",\"Ceviche\"],[\"Corina\",\"10\",\"Beef Burrito\"],[\"David\",\"3\",\"Fried Chicken\"],[\"Carla\",\"5\",\"Water\"],[\"Carla\",\"5\",\"Ceviche\"],[\"Rous\",\"3\",\"Ceviche\"]]) == [[\"Table\",\"Beef Burrito\",\"Ceviche\",\"Fried Chicken\",\"Water\"],[\"3\",\"0\",\"2\",\"1\",\"0\"],[\"5\",\"0\",\"1\",\"0\",\"1\"],[\"10\",\"1\",\"0\",\"0\",\"0\"]] \n assert candidate(orders = [[\"James\",\"12\",\"Fried Chicken\"],[\"Ratesh\",\"12\",\"Fried Chicken\"],[\"Amadeus\",\"12\",\"Fried Chicken\"],[\"Adam\",\"1\",\"Canadian Waffles\"],[\"Brianna\",\"1\",\"Canadian Waffles\"]]) == [[\"Table\",\"Canadian Waffles\",\"Fried Chicken\"],[\"1\",\"2\",\"0\"],[\"12\",\"0\",\"3\"]] \n assert candidate(orders = [[\"Laura\",\"2\",\"Bean Burrito\"],[\"Jhon\",\"2\",\"Beef Burrito\"],[\"Melissa\",\"2\",\"Soda\"]]) == [[\"Table\",\"Bean Burrito\",\"Beef Burrito\",\"Soda\"],[\"2\",\"1\",\"1\",\"1\"]]\n\n\ncheck(Solution().displayTable)"} | 218 | 316 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.