task_type
stringclasses 1
value | problem
stringlengths 209
3.39k
| answer
stringlengths 35
6.15k
| problem_tokens
int64 60
774
| answer_tokens
int64 12
2.04k
|
|---|---|---|---|---|
coding
|
Solve the programming task below in a Python markdown code block.
Mr. Krabs loves money. He enters a bank and sees that there are n rooms arranged in a row. The only rooms with doors are the two rooms on either corner. Initially, all the rooms are locked (including the corner ones). To enter a room, it must be unlocked with a key.
Mr. Krabs is able to steal k keys from the sleeping night guard. Each key can be used to unlock only one room. To reach one of the inner rooms, he has to unlock an adjacent room first.
Eg: If there are 5 rooms labelled: 1 2 3 4 5
Only Rooms 1 and 5 can be accessed initially. To unlock room 2, either room 1 or room 3 must be unlocked. Similarly to unlock Room 4, he must unlock room 5 or room 3 first.
Each room has some money in it. The money present in room i is given by A[i]. Using the k keys, calculate what is the maximum possible amount of money obtainable.
-----Input:-----
- First line will contain n, number of rooms.
- Second line will contain space separated integers, denoting A[i], money present in each room.
- Third line will contain an integer, k, the number of keys available.
-----Output:-----
Maximum money obtained.
-----Constraints-----
- 1 <= Number of Rooms <= 10^5
- 1 <= A[i] <= 10^4
- 1 <= k <= Number of Rooms
-----Subtasks-----
- 40 points : 1 <= Number of Rooms <= 100
- 60 points : Number of Rooms > 100
-----Sample Input:-----
7
1 2 3 4 5 6 1
3
-----Sample Output:-----
12
-----Explanation:-----
For the first step, Mr. Krabs can only unlock the room at the left corner or the right corner. If he unlocks the room in the right corner, he obtains 1 dollar. He can then go on to unlock the second last room and gain 6 dollars, for a total of 7 dollars. Finally he can unlock the third last room and get 5 dollars, for a total of 12 dollars.
This is the maximum amount of money he can get with only 3 keys.
|
{"inputs": ["7\n1 2 3 4 5 6 1\n3"], "outputs": ["12"]}
| 504
| 29
|
coding
|
Solve the programming task below in a Python markdown code block.
I'm sure you're familiar with factorials – that is, the product of an integer and all the integers below it.
For example, `5! = 120`, as `5 * 4 * 3 * 2 * 1 = 120`
Your challenge is to create a function that takes any number and returns the number that it is a factorial of. So, if your function receives `120`, it should return `"5!"` (as a string).
Of course, not every number is a factorial of another. In this case, your function would return `"None"` (as a string).
### Examples
* `120` will return `"5!"`
* `24` will return `"4!"`
* `150` will return `"None"`
Also feel free to reuse/extend the following starter code:
```python
def reverse_factorial(num):
```
|
{"functional": "_inputs = [[120], [3628800], [150]]\n_outputs = [['5!'], ['10!'], ['None']]\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(reverse_factorial(*i), o[0])"}
| 204
| 180
|
coding
|
Solve the programming task below in a Python markdown code block.
There are n pictures delivered for the new exhibition. The i-th painting has beauty a_{i}. We know that a visitor becomes happy every time he passes from a painting to a more beautiful one.
We are allowed to arranged pictures in any order. What is the maximum possible number of times the visitor may become happy while passing all pictures from first to last? In other words, we are allowed to rearrange elements of a in any order. What is the maximum possible number of indices i (1 ≤ i ≤ n - 1), such that a_{i} + 1 > a_{i}.
-----Input-----
The first line of the input contains integer n (1 ≤ n ≤ 1000) — the number of painting.
The second line contains the sequence a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 1000), where a_{i} means the beauty of the i-th painting.
-----Output-----
Print one integer — the maximum possible number of neighbouring pairs, such that a_{i} + 1 > a_{i}, after the optimal rearrangement.
-----Examples-----
Input
5
20 30 10 50 40
Output
4
Input
4
200 100 100 200
Output
2
-----Note-----
In the first sample, the optimal order is: 10, 20, 30, 40, 50.
In the second sample, the optimal order is: 100, 200, 100, 200.
|
{"inputs": ["1\n995\n", "1\n995\n", "1\n1000\n", "1\n1000\n", "2\n179 5\n", "2\n251 75\n", "2\n251 42\n", "2\n251 30\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "1\n", "1\n", "1\n", "1\n"]}
| 362
| 115
|
coding
|
Solve the programming task below in a Python markdown code block.
Karan likes the number 4 very much.
Impressed by the power of this number, Karan has begun to look for occurrences of four anywhere. He has a list of T integers, for each of them he wants to calculate the number of occurrences of the digit 4 in the decimal representation. He is too busy now, so please help him.
------ Input Format ------
The first line of input consists of a single integer T, denoting the number of integers in Karan's list.
Then, there are T lines, each of them contain a single integer from the list.
------ Output Format ------
Output T lines. Each of these lines should contain the number of occurrences of the digit 4 in the respective integer from Karan's list.
------ Constraints ------
1 ≤ T ≤ 10^{5}
(Subtask 1): 0 ≤ Numbers from the list ≤ 9 - 33 points.
(Subtask 2): 0 ≤ Numbers from the list ≤ 109 - 67 points.
----- Sample Input 1 ------
5
447474
228
6664
40
81
----- Sample Output 1 ------
4
0
1
1
0
|
{"inputs": ["5\n447474\n228\n6664\n40\n81\n"], "outputs": ["4\n0\n1\n1\n0\n"]}
| 273
| 44
|
coding
|
Solve the programming task below in a Python markdown code block.
Alice and Bob are playing One Card Poker.
One Card Poker is a two-player game using playing cards.
Each card in this game shows an integer between 1 and 13, inclusive.
The strength of a card is determined by the number written on it, as follows:
Weak 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < 10 < 11 < 12 < 13 < 1 Strong
One Card Poker is played as follows:
- Each player picks one card from the deck. The chosen card becomes the player's hand.
- The players reveal their hands to each other. The player with the stronger card wins the game.
If their cards are equally strong, the game is drawn.
You are watching Alice and Bob playing the game, and can see their hands.
The number written on Alice's card is A, and the number written on Bob's card is B.
Write a program to determine the outcome of the game.
-----Constraints-----
- 1≦A≦13
- 1≦B≦13
- A and B are integers.
-----Input-----
The input is given from Standard Input in the following format:
A B
-----Output-----
Print Alice if Alice will win. Print Bob if Bob will win. Print Draw if the game will be drawn.
-----Sample Input-----
8 6
-----Sample Output-----
Alice
8 is written on Alice's card, and 6 is written on Bob's card.
Alice has the stronger card, and thus the output should be Alice.
|
{"inputs": ["8 7", "2 1", "0 0", "8 3", "3 1", "7 3", "4 1", "7 1"], "outputs": ["Alice\n", "Bob\n", "Draw\n", "Alice\n", "Bob\n", "Alice\n", "Bob\n", "Bob\n"]}
| 344
| 78
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given an array of integers arr, return true if and only if it is a valid mountain array.
Recall that arr is a mountain array if and only if:
arr.length >= 3
There exists some i with 0 < i < arr.length - 1 such that:
arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
Please complete the following python code precisely:
```python
class Solution:
def validMountainArray(self, arr: List[int]) -> bool:
```
|
{"functional": "def check(candidate):\n assert candidate(arr = [2,1]) == False\n assert candidate(arr = [3,5,5]) == False\n assert candidate(arr = [0,3,2,1]) == True\n\n\ncheck(Solution().validMountainArray)"}
| 151
| 65
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
There is a class with m students and n exams. You are given a 0-indexed m x n integer matrix score, where each row represents one student and score[i][j] denotes the score the ith student got in the jth exam. The matrix score contains distinct integers only.
You are also given an integer k. Sort the students (i.e., the rows of the matrix) by their scores in the kth (0-indexed) exam from the highest to the lowest.
Return the matrix after sorting it.
Please complete the following python code precisely:
```python
class Solution:
def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:
```
|
{"functional": "def check(candidate):\n assert candidate(score = [[10,6,9,1],[7,5,11,2],[4,8,3,15]], k = 2) == [[7,5,11,2],[10,6,9,1],[4,8,3,15]]\n assert candidate(score = [[3,4],[5,6]], k = 0) == [[5,6],[3,4]]\n\n\ncheck(Solution().sortTheStudents)"}
| 164
| 120
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
We define str = [s, n] as the string str which consists of the string s concatenated n times.
For example, str == ["abc", 3] =="abcabcabc".
We define that string s1 can be obtained from string s2 if we can remove some characters from s2 such that it becomes s1.
For example, s1 = "abc" can be obtained from s2 = "abdbec" based on our definition by removing the bolded underlined characters.
You are given two strings s1 and s2 and two integers n1 and n2. You have the two strings str1 = [s1, n1] and str2 = [s2, n2].
Return the maximum integer m such that str = [str2, m] can be obtained from str1.
Please complete the following python code precisely:
```python
class Solution:
def getMaxRepetitions(self, s1: str, n1: int, s2: str, n2: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(s1 = \"acb\", n1 = 4, s2 = \"ab\", n2 = 2) == 2\n assert candidate(s1 = \"acb\", n1 = 1, s2 = \"acb\", n2 = 1) == 1\n\n\ncheck(Solution().getMaxRepetitions)"}
| 234
| 84
|
coding
|
Solve the programming task below in a Python markdown code block.
Let's dive into the interesting topic of regular expressions! You are given some input, and you are required to check whether they are valid mobile numbers.
A valid mobile number is a ten digit number starting with a $7,8$ or $9$.
Concept
A valid mobile number is a ten digit number starting with a $7,8$ or $9$.
Regular expressions are a key concept in any programming language. A quick explanation with Python examples is available here. You could also go through the link below to read more about regular expressions in Python.
https://developers.google.com/edu/python/regular-expressions
Input Format
The first line contains an integer $N$, the number of inputs.
$N$ lines follow, each containing some string.
Constraints
$1\leq N\leq10$
$2\leq len(Number)\leq15$
Output Format
For every string listed, print "YES" if it is a valid mobile number and "NO" if it is not on separate lines. Do not print the quotes.
Sample Input
2
9587456281
1252478965
Sample Output
YES
NO
|
{"inputs": ["2\n9587456281\n1252478965\n"], "outputs": ["YES\nNO\n"]}
| 269
| 38
|
coding
|
Solve the programming task below in a Python markdown code block.
Write
```python
remove(text, what)
```
that takes in a string ```str```(```text``` in Python) and an object/hash/dict/Dictionary ```what``` and returns a string with the chars removed in ```what```.
For example:
```python
remove('this is a string',{'t':1, 'i':2}) == 'hs s a string'
# remove from 'this is a string' the first 1 't' and the first 2 i's.
remove('hello world',{'x':5, 'i':2}) == 'hello world'
# there are no x's or i's, so nothing gets removed
remove('apples and bananas',{'a':50, 'n':1}) == 'pples d bnns'
# we don't have 50 a's, so just remove it till we hit end of string.
```
Also feel free to reuse/extend the following starter code:
```python
def remove(text, what):
```
|
{"functional": "_inputs = [['this is a string', {'t': 1, 'i': 2}], ['hello world', {'x': 5, 'i': 2}], ['apples and bananas', {'a': 50, 'n': 1}], ['a', {'a': 1, 'n': 1}], ['codewars', {'c': 5, 'o': 1, 'd': 1, 'e': 1, 'w': 1, 'z': 1, 'a': 1, 'r': 1, 's': 1}]]\n_outputs = [['hs s a string'], ['hello world'], ['pples d bnns'], [''], ['']]\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(remove(*i), o[0])"}
| 222
| 296
|
coding
|
Solve the programming task below in a Python markdown code block.
"Everybody! Doremy's Perfect Math Class is about to start! Come and do your best if you want to have as much IQ as me!" In today's math class, Doremy is teaching everyone subtraction. Now she gives you a quiz to prove that you are paying attention in class.
You are given a set $S$ containing positive integers. You may perform the following operation some (possibly zero) number of times:
choose two integers $x$ and $y$ from the set $S$ such that $x > y$ and $x - y$ is not in the set $S$.
add $x-y$ into the set $S$.
You need to tell Doremy the maximum possible number of integers in $S$ if the operations are performed optimally. It can be proven that this number is finite.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 10^4$) — the number of test cases. The description of the test cases follows.
The first line contains an integer $n$ ($2 \le n\le 10^5$) — the size of the set $S$.
The second line contains $n$ integers $a_1,a_2,\dots,a_n$ ($1\le a_1 < a_2 < \cdots < a_n \le 10^9$) — the positive integers in $S$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot 10^5$.
-----Output-----
For each test case, you need to output the maximum possible number of integers in $S$. It can be proven that this value is finite.
-----Examples-----
Input
2
2
1 2
3
5 10 25
Output
2
5
-----Note-----
In the first test case, no such $x$ and $y$ exist. The maximum possible number of integers in $S$ is $2$.
In the second test case,
$S={5,10,25\}$ at first. You can choose $x=25$, $y=10$, then add $x-y=15$ to the set.
$S={5,10,15,25\}$ now. You can choose $x=25$, $y=5$, then add $x-y=20$ to the set.
$S={5,10,15,20,25\}$ now. You can not perform any operation now.
After performing all operations, the number of integers in $S$ is $5$. It can be proven that no other sequence of operations allows $S$ to contain more than $5$ integers.
|
{"inputs": ["2\n2\n1 2\n3\n5 10 25\n"], "outputs": ["2\n5\n"]}
| 610
| 32
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problem statements in [Mandarin], [Bengali], [Russian], and [Vietnamese] as well.
Chef is a software developer, so he has to switch between different languages sometimes. Each programming language has some features, which are represented by integers here.
Currently, Chef has to use a language with two given features A and B. He has two options --- switching to a language with two features A_{1} and B_{1}, or to a language with two features A_{2} and B_{2}. All four features of these two languages are pairwise distinct.
Tell Chef whether he can use the first language, the second language or neither of these languages (if no single language has all the required features).
------ Input Format ------
- The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first and only line of each test case contains six space-separated integers A, B, A_{1}, B_{1}, A_{2}, B_{2}.
------ Output Format ------
For each test case, print a single line containing the integer 1 if Chef should switch to the first language, or 2 if Chef should switch to the second language, or 0 if Chef cannot switch to either language.
------ Constraints ------
$1 ≤ T ≤ 288$
$1 ≤ A, B, A_{1}, B_{1}, A_{2}, B_{2} ≤ 4$
$A, B$ are distinct
$A_{1}, B_{1}, A_{2}, B_{2}$ are pairwise distinct
------ subtasks ------
Subtask #1 (100 points): original constraints
----- Sample Input 1 ------
3
1 2 2 1 3 4
3 4 2 1 4 3
1 2 1 3 2 4
----- Sample Output 1 ------
1
2
0
----- explanation 1 ------
Example case 1: The first language has the required features --- features $1$ and $2$.
Example case 2: The second language has the required features --- features $3$ and $4$.
Example case 3: Neither language has both features $1$ and $2$.
|
{"inputs": ["3\n1 2 2 1 3 4\n3 4 2 1 4 3\n1 2 1 3 2 4"], "outputs": ["1\n2\n0"]}
| 494
| 52
|
coding
|
Solve the programming task below in a Python markdown code block.
At Aizu Shingakujuku, students are divided into classes by conducting a proficiency test when they enter the cram school. The test consists of three subjects: Mathematics, English, and Japanese, and students are divided into A, B, and C classes. The level of class A is the highest and then decreases in order.
The classification decision is based on the table below.
Condition | Class
--- | ---
There are 100 subjects | A
Average score of math and English is 90 points or more | A
Average score of 3 subjects is 80 points or more | A
Average score of 3 subjects is 70 points or more | B
Average score of 3 subjects is 50 points or more and math or English is 80 points or more | B
Does not meet the above conditions | C
If you meet multiple conditions, you will be divided into higher level classes.
Number of students n (1 ≤ n ≤ 10000), math score pmi (0 ≤ pmi ≤ 100) for each student, English score pei (0 ≤ pei ≤ 100), national language score pji (0 ≤ pji ≤ 100) Create a program that outputs classes A, B, and C (half-width alphabetic characters) for each student.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
pm1 pe1 pj1
pm2 pe2 pj2
::
pmn pen pjn
All inputs are given as integers. The number of datasets does not exceed 1000.
Output
Outputs each student's class in turn for each input dataset.
Example
Input
4
100 70 20
98 86 55
80 34 36
65 79 65
2
99 81 20
66 72 90
0
Output
A
A
B
C
A
B
|
{"inputs": ["4\n110 16 20\n98 86 19\n80 8 39\n65 79 65\n2\n99 81 9\n66 72 90\n0", "4\n010 163 20\n98 86 5\n80 34 33\n65 79 65\n2\n49 81 20\n97 1 4\n0", "4\n110 11 20\n98 86 19\n80 10 39\n65 0 65\n2\n99 81 9\n66 102 90\n0", "4\n100 64 20\n51 86 18\n80 39 36\n65 79 65\n2\n99 81 6\n76 72 90\n0", "4\n100 70 20\n98 86 55\n80 34 39\n65 79 65\n2\n99 81 20\n66 72 90\n0", "4\n100 70 20\n98 86 19\n80 34 39\n65 79 65\n2\n99 75 20\n66 72 90\n0", "4\n100 70 20\n98 86 19\n80 34 39\n65 79 65\n2\n34 75 20\n66 72 90\n0", "4\n010 77 20\n98 86 19\n80 34 33\n65 79 65\n2\n120 81 38\n66 47 4\n0"], "outputs": ["C\nA\nC\nC\nA\nB\n", "B\nA\nC\nC\nB\nC\n", "C\nA\nC\nC\nA\nA\n", "A\nB\nB\nC\nA\nB\n", "A\nA\nB\nC\nA\nB\n", "A\nA\nB\nC\nB\nB\n", "A\nA\nB\nC\nC\nB\n", "C\nA\nC\nC\nA\nC\n"]}
| 465
| 608
|
coding
|
Solve the programming task below in a Python markdown code block.
Consider the number triangle below, in which each number is equal to the number above plus the number to the left. If there is no number above, assume it's a `0`.
The triangle has `5` rows and the sum of the last row is `sum([1,4,9,14,14]) = 42`.
You will be given an integer `n` and your task will be to return the sum of the last row of a triangle of `n` rows.
In the example above:
More examples in test cases. Good luck!
```if:javascript
### Note
This kata uses native arbitrary precision integer numbers ( `BigInt`, `1n` ).
Unfortunately, the testing framework and even native `JSON` do not fully support them yet.
`console.log(1n)` and `(1n).toString()` work and can be used for debugging.
We apologise for the inconvenience.
```
Also feel free to reuse/extend the following starter code:
```python
def solve(n):
```
|
{"functional": "_inputs = [[4], [5], [6], [7], [8], [20]]\n_outputs = [[14], [42], [132], [429], [1430], [6564120420]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(solve(*i), o[0])"}
| 227
| 203
|
coding
|
Solve the programming task below in a Python markdown code block.
Iahub likes trees very much. Recently he discovered an interesting tree named propagating tree. The tree consists of n nodes numbered from 1 to n, each node i having an initial value a_{i}. The root of the tree is node 1.
This tree has a special property: when a value val is added to a value of node i, the value -val is added to values of all the children of node i. Note that when you add value -val to a child of node i, you also add -(-val) to all children of the child of node i and so on. Look an example explanation to understand better how it works.
This tree supports two types of queries:
"1 x val" — val is added to the value of node x; "2 x" — print the current value of node x.
In order to help Iahub understand the tree better, you must answer m queries of the preceding type.
-----Input-----
The first line contains two integers n and m (1 ≤ n, m ≤ 200000). The second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 1000). Each of the next n–1 lines contains two integers v_{i} and u_{i} (1 ≤ v_{i}, u_{i} ≤ n), meaning that there is an edge between nodes v_{i} and u_{i}.
Each of the next m lines contains a query in the format described above. It is guaranteed that the following constraints hold for all queries: 1 ≤ x ≤ n, 1 ≤ val ≤ 1000.
-----Output-----
For each query of type two (print the value of node x) you must print the answer to the query on a separate line. The queries must be answered in the order given in the input.
-----Examples-----
Input
5 5
1 2 1 1 2
1 2
1 3
2 4
2 5
1 2 3
1 1 2
2 1
2 2
2 4
Output
3
3
0
-----Note-----
The values of the nodes are [1, 2, 1, 1, 2] at the beginning.
Then value 3 is added to node 2. It propagates and value -3 is added to it's sons, node 4 and node 5. Then it cannot propagate any more. So the values of the nodes are [1, 5, 1, - 2, - 1].
Then value 2 is added to node 1. It propagates and value -2 is added to it's sons, node 2 and node 3. From node 2 it propagates again, adding value 2 to it's sons, node 4 and node 5. Node 3 has no sons, so it cannot propagate from there. The values of the nodes are [3, 3, - 1, 0, 1].
You can see all the definitions about the tree at the following link: http://en.wikipedia.org/wiki/Tree_(graph_theory)
|
{"inputs": ["5 5\n1 2 1 1 2\n1 2\n1 3\n2 4\n2 5\n1 2 3\n1 1 2\n2 1\n2 2\n2 4\n", "5 5\n1 2 1 1 2\n1 2\n1 3\n2 4\n2 5\n1 2 3\n1 1 2\n2 1\n2 2\n2 4\n", "10 10\n137 197 856 768 825 894 86 174 2 326\n7 8\n4 7\n8 9\n7 10\n1 2\n2 4\n3 6\n3 5\n2 3\n1 9 624\n2 1\n2 4\n1 6 505\n1 8 467\n1 3 643\n2 1\n1 8 631\n2 1\n1 7 244\n", "10 10\n137 197 856 768 825 894 86 174 218 326\n7 8\n4 7\n8 9\n7 10\n1 2\n1 4\n3 6\n3 5\n2 3\n1 9 624\n2 1\n2 4\n1 6 32\n1 8 467\n1 3 643\n2 1\n1 3 631\n2 4\n1 7 364\n", "10 10\n137 197 856 768 825 894 86 174 218 326\n7 8\n4 7\n8 9\n7 10\n1 2\n2 4\n3 6\n3 5\n2 3\n1 9 624\n2 1\n2 4\n1 6 505\n1 8 467\n1 3 643\n2 1\n1 8 631\n2 4\n1 7 244\n", "10 10\n137 197 856 768 825 894 86 174 218 326\n7 8\n4 7\n8 9\n7 10\n1 2\n2 4\n3 6\n3 5\n2 3\n1 9 624\n2 1\n2 4\n1 6 505\n1 8 467\n1 3 643\n2 1\n1 8 631\n2 4\n1 7 244\n", "10 10\n137 197 856 768 825 894 86 174 218 326\n7 8\n4 7\n8 9\n7 10\n1 2\n1 4\n3 6\n3 5\n2 3\n1 9 624\n2 1\n2 4\n1 6 505\n1 8 467\n1 3 643\n2 1\n1 8 631\n2 4\n1 7 244\n", "10 10\n137 197 856 768 825 894 86 174 218 326\n7 8\n4 7\n8 9\n7 10\n1 2\n2 4\n3 6\n3 5\n2 3\n1 9 624\n2 1\n2 4\n1 6 505\n1 8 467\n1 3 643\n2 1\n1 8 631\n2 1\n1 7 244\n"], "outputs": ["3\n3\n0\n", "3\n3\n0\n", "137\n768\n137\n137\n", "137\n768\n137\n768\n", "137\n768\n137\n768\n", "137\n768\n137\n768\n", "137\n768\n137\n768\n", "137\n768\n137\n137\n"]}
| 694
| 1,127
|
coding
|
Solve the programming task below in a Python markdown code block.
Let $\mathsf{AND}$ denote the bitwise AND operation , and $\mathsf{OR}$ denote the bitwise OR operation .
You are given an array $a$ of length $n$ and a non-negative integer $k$. You can perform at most $k$ operations on the array of the following type:
Select an index $i$ ($1 \leq i \leq n$) and replace $a_i$ with $a_i$ $\mathsf{OR}$ $2^j$ where $j$ is any integer between $0$ and $30$ inclusive. In other words, in an operation you can choose an index $i$ ($1 \leq i \leq n$) and set the $j$-th bit of $a_i$ to $1$ ($0 \leq j \leq 30$).
Output the maximum possible value of $a_1$ $\mathsf{AND}$ $a_2$ $\mathsf{AND}$ $\dots$ $\mathsf{AND}$ $a_n$ after performing at most $k$ operations.
-----Input-----
The first line of the input contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The description of test cases follows.
The first line of each test case contains the integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $0 \le k \le 10^9$).
Then a single line follows, containing $n$ integers describing the arrays $a$ ($0 \leq a_i < 2^{31}$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single line containing the maximum possible $\mathsf{AND}$ value of $a_1$ $\mathsf{AND}$ $a_2$ $\mathsf{AND}$ $\dots$ $\mathsf{AND}$ $a_n$ after performing at most $k$ operations.
-----Examples-----
Input
4
3 2
2 1 1
7 0
4 6 6 28 6 6 12
1 30
0
4 4
3 1 3 1
Output
2
4
2147483646
1073741825
-----Note-----
For the first test case, we can set the bit $1$ ($2^1$) of the last $2$ elements using the $2$ operations, thus obtaining the array [$2$, $3$, $3$], which has $\mathsf{AND}$ value equal to $2$.
For the second test case, we can't perform any operations so the answer is just the $\mathsf{AND}$ of the whole array which is $4$.
|
{"inputs": ["4\n3 2\n2 1 1\n7 0\n4 6 6 28 6 6 12\n1 30\n0\n4 4\n3 1 3 1\n"], "outputs": ["2\n4\n2147483646\n1073741825\n"]}
| 646
| 87
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are a hiker preparing for an upcoming hike. You are given heights, a 2D array of size rows x columns, where heights[row][col] represents the height of cell (row, col). You are situated in the top-left cell, (0, 0), and you hope to travel to the bottom-right cell, (rows-1, columns-1) (i.e., 0-indexed). You can move up, down, left, or right, and you wish to find a route that requires the minimum effort.
A route's effort is the maximum absolute difference in heights between two consecutive cells of the route.
Return the minimum effort required to travel from the top-left cell to the bottom-right cell.
Please complete the following python code precisely:
```python
class Solution:
def minimumEffortPath(self, heights: List[List[int]]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(heights = [[1,2,2],[3,8,2],[5,3,5]]) == 2\n assert candidate(heights = [[1,2,3],[3,8,4],[5,3,5]]) == 1\n assert candidate(heights = [[1,2,1,1,1],[1,2,1,2,1],[1,2,1,2,1],[1,2,1,2,1],[1,1,1,2,1]]) == 0\n\n\ncheck(Solution().minimumEffortPath)"}
| 200
| 140
|
coding
|
Solve the programming task below in a Python markdown code block.
A string is made of only lowercase latin letters (a,b,c,d,.....,z). Can you find the length of the lexicographically smallest string such that it has exactly ${K}$ sub-strings, each of which are palindromes?
Input Format
The first line of input contains single integer ${T}$ - the number of testcases.
T lines follow, each containing the integer ${K}$.
Constraints
$1\leq T\leq100$
$1\leq K\leq10^{12}$
Output Format
Output exactly ${T}$ lines. Each line should contain single integer - the length of the lexicographically smallest string.
Sample Input
2
10
17
Sample Output
4
7
Explanation
for $K=10$, one of the smallest possible strings that satisfies the property is aaaa.
All $10$ palindromes are
a,a,a,a
aa, aa, aa
aaa, aaa
aaaa
Note
Two sub-strings with different indices are both counted.
|
{"inputs": ["2\n10\n17\n"], "outputs": ["4\n7\n"]}
| 242
| 22
|
coding
|
Solve the programming task below in a Python markdown code block.
There are $n$ benches in the Berland Central park. It is known that $a_i$ people are currently sitting on the $i$-th bench. Another $m$ people are coming to the park and each of them is going to have a seat on some bench out of $n$ available.
Let $k$ be the maximum number of people sitting on one bench after additional $m$ people came to the park. Calculate the minimum possible $k$ and the maximum possible $k$.
Nobody leaves the taken seat during the whole process.
-----Input-----
The first line contains a single integer $n$ $(1 \le n \le 100)$ — the number of benches in the park.
The second line contains a single integer $m$ $(1 \le m \le 10\,000)$ — the number of people additionally coming to the park.
Each of the next $n$ lines contains a single integer $a_i$ $(1 \le a_i \le 100)$ — the initial number of people on the $i$-th bench.
-----Output-----
Print the minimum possible $k$ and the maximum possible $k$, where $k$ is the maximum number of people sitting on one bench after additional $m$ people came to the park.
-----Examples-----
Input
4
6
1
1
1
1
Output
3 7
Input
1
10
5
Output
15 15
Input
3
6
1
6
5
Output
6 12
Input
3
7
1
6
5
Output
7 13
-----Note-----
In the first example, each of four benches is occupied by a single person. The minimum $k$ is $3$. For example, it is possible to achieve if two newcomers occupy the first bench, one occupies the second bench, one occupies the third bench, and two remaining — the fourth bench. The maximum $k$ is $7$. That requires all six new people to occupy the same bench.
The second example has its minimum $k$ equal to $15$ and maximum $k$ equal to $15$, as there is just a single bench in the park and all $10$ people will occupy it.
|
{"inputs": ["1\n10\n5\n", "1\n10\n2\n", "1\n10\n5\n", "2\n1\n5\n1\n", "2\n1\n5\n1\n", "2\n2\n5\n1\n", "2\n1\n10\n1\n", "2\n1\n10\n1\n"], "outputs": ["15 15\n", "12 12\n", "15 15\n", "5 6\n", "5 6\n", "5 7\n", "10 11\n", "10 11\n"]}
| 494
| 143
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.
Chef is an intern at Zoozle. He has a co-intern named Ajar who's good at maths. Chef wants to impress Ajar with his zoozliness, so he decided to play the following game with A
Consider $n$ force vectors in a 2D plane.
First, Ajar uniformly randomly chooses a non-negative real *magnitude* for each vector such that the sum of all magnitudes is equal to $k$. (More formally, each valid $n$-tuple of magnitudes can be chosen with the same probability.)
Then, Chef must choose a *direction* for each force vector in such a way that the net force (vector sum of all forces) is equal to zero. Chef wins the game if he is able to do that; otherwise, Ajar wins the game.
Since Chef knows that it may not always be possible to choose the directions such that he wins the game, he would like to know the probability that he'll be able to win if he plays optimally. Can you help him?
It can be proven that this probability can be written as a fraction $P/Q$, where $P ≥ 0$ and $Q > 0$ are coprime integers. Since we are in the 21st century, nobody is interested in fractions. Therefore, you should compute $P\cdot Q^{-1}$ modulo $10^{9}+7$, where $Q^{-1}$ denotes the modular inverse of $Q$ modulo $10^{9}+7$. (It is guaranteed that this inverse exists and is unique.)
------ Input ------
The first line of the input contains a single integer $n$ denoting the number of force vectors.
The second line contains a single integer $k$ denoting the sum of magnitudes of all force vectors.
------ Output ------
Print a single line containing one integer — the value of $P\cdot Q^{-1}$ modulo $10^{9}+7$.
------ Constraints ------
$1 ≤ k ≤ 100$
$2 ≤ n ≤ 3\cdot10^{5}$
------ Subtasks ------
Subtask 1 (20 points): $2 ≤ n ≤ 50$
Subtask 2 (80 points): original constraints
----- Sample Input 1 ------
5
5
----- Sample Output 1 ------
187500002
----- explanation 1 ------
Answer = $11/16 = 11\cdot16^{-1} = 187500002$
|
{"inputs": ["5\n5"], "outputs": ["187500002"]}
| 566
| 22
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a multiset $S$ initially consisting of $n$ distinct non-negative integers. A multiset is a set, that can contain some elements multiple times.
You will perform the following operation $k$ times:
Add the element $\lceil\frac{a+b}{2}\rceil$ (rounded up) into $S$, where $a = \operatorname{mex}(S)$ and $b = \max(S)$. If this number is already in the set, it is added again.
Here $\operatorname{max}$ of a multiset denotes the maximum integer in the multiset, and $\operatorname{mex}$ of a multiset denotes the smallest non-negative integer that is not present in the multiset. For example:
$\operatorname{mex}(\{1,4,0,2\})=3$;
$\operatorname{mex}(\{2,5,1\})=0$.
Your task is to calculate the number of distinct elements in $S$ after $k$ operations will be done.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 100$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers $n$, $k$ ($1\le n\le 10^5$, $0\le k\le 10^9$) — the initial size of the multiset $S$ and how many operations you need to perform.
The second line of each test case contains $n$ distinct integers $a_1,a_2,\dots,a_n$ ($0\le a_i\le 10^9$) — the numbers in the initial multiset.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, print the number of distinct elements in $S$ after $k$ operations will be done.
-----Examples-----
Input
5
4 1
0 1 3 4
3 1
0 1 4
3 0
0 1 4
3 2
0 1 2
3 2
1 2 3
Output
4
4
3
5
3
-----Note-----
In the first test case, $S={0,1,3,4\}$, $a=\operatorname{mex}(S)=2$, $b=\max(S)=4$, $\lceil\frac{a+b}{2}\rceil=3$. So $3$ is added into $S$, and $S$ becomes $\{0,1,3,3,4\}$. The answer is $4$.
In the second test case, $S={0,1,4\}$, $a=\operatorname{mex}(S)=2$, $b=\max(S)=4$, $\lceil\frac{a+b}{2}\rceil=3$. So $3$ is added into $S$, and $S$ becomes $\{0,1,3,4\}$. The answer is $4$.
|
{"inputs": ["1\n1 0\n0\n", "1\n1 0\n0\n", "1\n1 0\n1\n", "1\n1 1\n4\n", "1\n1 0\n2\n", "1\n1 1\n1\n", "1\n1 2\n4\n", "1\n1 0\n4\n"], "outputs": ["1\n", "1\n", "1\n", "2\n", "1\n", "1\n", "2\n", "1\n"]}
| 700
| 118
|
coding
|
Solve the programming task below in a Python markdown code block.
Little Walter likes playing with his toy scales. He has $N$ types of weights. The $i^{\mbox{th}}$ weight type has weight $a_i$. There are infinitely many weights of each type.
Recently, Walter defined a function, $F(X)$, denoting the number of different ways to combine several weights so their total weight is equal to $\mbox{X}$. Ways are considered to be different if there is a type which has a different number of weights used in these two ways.
For example, if there are $3$ types of weights with corresonding weights $1$, $1$, and $2$, then there are $4$ ways to get a total weight of $2$:
Use $2$ weights of type $1$.
Use $2$ weights of type $2$.
Use $1$ weight of type $1$ and $1$ weight of type $2$.
Use $1$ weight of type $3$.
Given $N$, $L$, $\mbox{R}$, and $a_1,a_2,\ldots,a_N$, can you find the value of $F(L)+F(L+1)+\text{}...+F(R)$?
Input Format
The first line contains a single integer, $N$, denoting the number of types of weights.
The second line contains $N$ space-separated integers describing the values of $a_1,a_2,\ldots,a_N$, respectively
The third line contains two space-separated integers denoting the respective values of $L$ and $\mbox{R}$.
Constraints
$1\leq N\leq10$
$0<a_i\leq10^5$
$a_1\times a_2\times\text{}\text{}\text{}\text{}\text{}\text{}\text{}\text{}\text{}\text{}\text{}\text{}a_N\leq10^5$
$1\leq L\leq R\leq10^{17}$
Note: The time limit for C/C++ is $1$ second, and for Java it's $2$ seconds.
Output Format
Print a single integer denoting the answer to the question. As this value can be very large, your answer must be modulo $10^9+7$.
Sample Input
3
1 2 3
1 6
Sample Output
22
Explanation
$F(1)=1$
$F(2)=2$
$F(3)=3$
$F(4)=4$
$F(5)=5$
$F(6)=7$
|
{"inputs": ["3\n1 2 3\n1 6\n"], "outputs": ["22\n"]}
| 567
| 25
|
coding
|
Solve the programming task below in a Python markdown code block.
Jeff's got n cards, each card contains either digit 0, or digit 5. Jeff can choose several cards and put them in a line so that he gets some number. What is the largest possible number divisible by 90 Jeff can make from the cards he's got?
Jeff must make the number without leading zero. At that, we assume that number 0 doesn't contain any leading zeroes. Jeff doesn't have to use all the cards.
-----Input-----
The first line contains integer n (1 ≤ n ≤ 10^3). The next line contains n integers a_1, a_2, ..., a_{n} (a_{i} = 0 or a_{i} = 5). Number a_{i} represents the digit that is written on the i-th card.
-----Output-----
In a single line print the answer to the problem — the maximum number, divisible by 90. If you can't make any divisible by 90 number from the cards, print -1.
-----Examples-----
Input
4
5 0 5 0
Output
0
Input
11
5 5 5 5 5 5 5 5 0 5 5
Output
5555555550
-----Note-----
In the first test you can make only one number that is a multiple of 90 — 0.
In the second test you can make number 5555555550, it is a multiple of 90.
|
{"inputs": ["1\n5\n", "1\n0\n", "1\n0\n", "1\n5\n", "2\n0 0\n", "2\n0 5\n", "2\n0 5\n", "2\n0 0\n"], "outputs": ["-1\n", "0\n", "0\n", "-1\n", "0\n", "0\n", "0\n", "0\n"]}
| 336
| 95
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given three integers A, B and C.
Determine if there exists an equilateral triangle whose sides have lengths A, B and C.
Constraints
* All values in input are integers.
* 1 \leq A,B,C \leq 100
Input
Input is given from Standard Input in the following format:
A B C
Output
If there exists an equilateral triangle whose sides have lengths A, B and C, print `Yes`; otherwise, print `No`.
Examples
Input
2 2 2
Output
Yes
Input
3 4 5
Output
No
|
{"inputs": ["2 4 2", "0 0 0", "3 4 2", "2 6 2", "3 4 1", "2 7 2", "3 0 1", "1 7 2"], "outputs": ["No\n", "Yes\n", "No\n", "No\n", "No\n", "No\n", "No\n", "No\n"]}
| 141
| 94
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.
You are given an integer sequence $A$ with length $N$.
Find the number of (unordered) pairs of elements such that the average of these two elements is also present in the sequence. Formally, find the number of pairs $(i,j)$ such that $1 ≤ i < j ≤ N$ and there is an index $k$ ($1 ≤ k ≤ N$) for which $2A_{k} = A_{i}+A_{j}$ holds.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \dots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer — the number of valid pairs $(i,j)$.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 10^{5}$
$|A_{i}| ≤ 10^{3}$ for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $1 ≤ N ≤ 10^{3}$
Subtask #2 (70 points): $1 ≤ N ≤ 10^{5}$
----- Sample Input 1 ------
3
2
2 2
3
2 1 3
6
4 2 5 1 3 5
----- Sample Output 1 ------
1
1
7
----- explanation 1 ------
Example case 1: We can take $k$ for $A_{k}$ to be either 1 or 2, since $A_{k}=2$ either way. Since the problem asks for unordered pairs, $(1,2)$ is the only possible valid pair (the same as $(2,1)$). Hence, the answer is 1.
Example case 2: We see that $A_{1} = 2 = (1+3)/2$. No other valid pair exists.
|
{"inputs": ["3\n2\n2 2\n3\n2 1 3\n6\n4 2 5 1 3 5"], "outputs": ["1\n1\n7"]}
| 486
| 44
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given a string s, return the length of the longest repeating substrings. If no repeating substring exists, return 0.
Please complete the following python code precisely:
```python
class Solution:
def longestRepeatingSubstring(self, s: str) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(\"abcd\") == 0\n assert candidate(\"abbaba\") == 2\n assert candidate(\"aabcaabdaab\") == 3\n assert candidate(\"aaaaa\") == 4\n\n\ncheck(Solution().longestRepeatingSubstring)"}
| 74
| 68
|
coding
|
Solve the programming task below in a Python markdown code block.
An array $a$ is good if for all pairs of adjacent elements, $a_i$ and $a_{i+1}$ ($1\le i \lt n$) are of different parity. Note that an array of size $1$ is trivially good.
You are given an array of size $n$.
In one operation you can select any pair of adjacent elements in which both elements are of the same parity, delete them, and insert their product in the same position.
Find the minimum number of operations to form a good array.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). The description of the test cases follows.
The first line of each test case contains an integer $n$ ($1 \le n \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}$).
-----Output-----
For each test case print an integer, the minimum number of operations required to form a good array.
-----Examples-----
Input
3
5
1 7 11 2 13
4
1 2 3 4
6
1 1 1 2 2 3
Output
2
0
3
-----Note-----
Consider the first test case. Select the $2$-nd and the $3$-rd integers and apply the operation on them. The array changes from $[1, {7}, {11}, 2, 13]$ to $[1, {77}, 2, 13]$. Next, select the $1$-st and the $2$-nd integers, array changes from $[{1}, {77}, 2, 13]$ to $[{77}, 2, 13]$. Thus we require $2$ operations. It can be proved that this is the minimum number of operations.
In the second test case, the given array is already good. So we require $0$ operations.
|
{"inputs": ["1\n1\n9\n", "1\n10\n4 4 4 4 4 4 4 4 4 5\n", "3\n5\n1 7 11 2 13\n4\n1 2 3 4\n6\n1 1 1 2 2 3\n", "2\n10\n1 1 1 1 1 5 5 5 5 5\n11\n9 9 9 9 9 10 9 9 9 9 9\n", "1\n20\n1 2 3 4 4 5 5 6 6 7 7 9 9 9 9 10 10 124 241 241\n", "30\n1\n1\n1\n2\n2\n1 1\n2\n1 2\n2\n2 1\n2\n2 2\n3\n1 1 1\n3\n1 1 2\n3\n1 2 1\n3\n1 2 2\n3\n2 1 1\n3\n2 1 2\n3\n2 2 1\n3\n2 2 2\n4\n1 1 1 1\n4\n1 1 1 2\n4\n1 1 2 1\n4\n1 1 2 2\n4\n1 2 1 1\n4\n1 2 1 2\n4\n1 2 2 1\n4\n1 2 2 2\n4\n2 1 1 1\n4\n2 1 1 2\n4\n2 1 2 1\n4\n2 1 2 2\n4\n2 2 1 1\n4\n2 2 1 2\n4\n2 2 2 1\n4\n2 2 2 2\n"], "outputs": ["0\n", "8\n", "2\n0\n3\n", "9\n8\n", "11\n", "0\n0\n1\n0\n0\n1\n2\n1\n0\n1\n1\n0\n1\n2\n3\n2\n1\n2\n1\n0\n1\n2\n2\n1\n0\n1\n2\n1\n2\n3\n"]}
| 472
| 541
|
coding
|
Solve the programming task below in a Python markdown code block.
Nian is a monster which lives deep in the oceans. Once a year, it shows up on the land, devouring livestock and even people. In order to keep the monster away, people fill their villages with red colour, light, and cracking noise, all of which frighten the monster out of coming.
Little Tommy has n lanterns and Big Banban has m lanterns. Tommy's lanterns have brightness a_1, a_2, ..., a_{n}, and Banban's have brightness b_1, b_2, ..., b_{m} respectively.
Tommy intends to hide one of his lanterns, then Banban picks one of Tommy's non-hidden lanterns and one of his own lanterns to form a pair. The pair's brightness will be the product of the brightness of two lanterns.
Tommy wants to make the product as small as possible, while Banban tries to make it as large as possible.
You are asked to find the brightness of the chosen pair if both of them choose optimally.
-----Input-----
The first line contains two space-separated integers n and m (2 ≤ n, m ≤ 50).
The second line contains n space-separated integers a_1, a_2, ..., a_{n}.
The third line contains m space-separated integers b_1, b_2, ..., b_{m}.
All the integers range from - 10^9 to 10^9.
-----Output-----
Print a single integer — the brightness of the chosen pair.
-----Examples-----
Input
2 2
20 18
2 14
Output
252
Input
5 3
-1 0 1 2 3
-1 0 1
Output
2
-----Note-----
In the first example, Tommy will hide 20 and Banban will choose 18 from Tommy and 14 from himself.
In the second example, Tommy will hide 3 and Banban will choose 2 from Tommy and 1 from himself.
|
{"inputs": ["2 2\n15 18\n0 4\n", "2 2\n20 18\n2 14\n", "2 2\n20 18\n0 14\n", "2 2\n15 18\n0 14\n", "2 2\n20 18\n2 14\n", "5 3\n0 0 1 0 3\n0 0 1\n", "5 3\n-1 0 1 0 3\n0 0 1\n", "5 3\n-1 0 1 2 3\n-1 0 1\n"], "outputs": ["60\n", "252\n", "252\n", "210\n", "252\n", "1\n", "1\n", "2\n"]}
| 439
| 200
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given a binary string s consisting only of zeroes and ones.
A substring of s is considered balanced if all zeroes are before ones and the number of zeroes is equal to the number of ones inside the substring. Notice that the empty substring is considered a balanced substring.
Return the length of the longest balanced substring of s.
A substring is a contiguous sequence of characters within a string.
Please complete the following python code precisely:
```python
class Solution:
def findTheLongestBalancedSubstring(self, s: str) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(s = \"01000111\") == 6\n assert candidate(s = \"00111\") == 4\n assert candidate(s = \"111\") == 0\n\n\ncheck(Solution().findTheLongestBalancedSubstring)"}
| 129
| 73
|
coding
|
Solve the programming task below in a Python markdown code block.
Write a program that, given a word, computes the scrabble score for that word.
## Letter Values
You'll need these:
```
Letter Value
A, E, I, O, U, L, N, R, S, T 1
D, G 2
B, C, M, P 3
F, H, V, W, Y 4
K 5
J, X 8
Q, Z 10
```
```if:ruby,javascript,cfml
There will be a preloaded hashtable `$dict` with all these values: `$dict["E"] == 1`.
```
```if:haskell
There will be a preloaded list of `(Char, Int)` tuples called `dict` with all these values.
```
```if:python
There will be a preloaded dictionary `dict_scores` with all these values: `dict_scores["E"] == 1`
```
## Examples
```
"cabbage" --> 14
```
"cabbage" should be scored as worth 14 points:
- 3 points for C
- 1 point for A, twice
- 3 points for B, twice
- 2 points for G
- 1 point for E
And to total:
`3 + 2*1 + 2*3 + 2 + 1` = `3 + 2 + 6 + 3` = `14`
Empty string should return `0`. The string can contain spaces and letters (upper and lower case), you should calculate the scrabble score only of the letters in that string.
```
"" --> 0
"STREET" --> 6
"st re et" --> 6
"ca bba g e" --> 14
```
Also feel free to reuse/extend the following starter code:
```python
def scrabble_score(st):
```
|
{"functional": "_inputs = [[''], ['a'], ['street'], ['STREET'], [' a'], ['st re et'], ['f'], ['quirky'], ['MULTIBILLIONAIRE'], ['alacrity']]\n_outputs = [[0], [1], [6], [6], [1], [6], [4], [22], [20], [13]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(scrabble_score(*i), o[0])"}
| 430
| 223
|
coding
|
Solve the programming task below in a Python markdown code block.
Henry and Derek are waiting on a room, eager to join the Snackdown 2016 Qualifier Round. They decide to pass the time by playing a game.
In this game's setup, they write N positive integers on a blackboard. Then the players take turns, starting with Henry. In a turn, a player selects one of the integers, divides it by 2, 3, 4, 5 or 6, and then takes the floor to make it an integer again. If the integer becomes 0, it is erased from the board. The player who makes the last move wins.
Henry and Derek are very competitive, so aside from wanting to win Snackdown, they also want to win this game. Assuming they play with the optimal strategy, your task is to predict who wins the game.
-----Input-----
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a single integer N denoting the number of integers they wrote on the board. The second line contains N space-separated integers A1, A2, ..., AN denoting the integers themselves.
-----Output-----
For each test case, output a single line containing either “Henry” or “Derek” (without quotes), denoting the winner of the game.
-----Constraints-----
- 1 ≤ T ≤ 1000
- 1 ≤ N ≤ 100
- 1 ≤ Ai ≤ 1018
-----Example-----
Input:2
2
3 4
3
1 3 5
Output:Henry
Derek
-----Explanation-----
Example case 1. In this test case, the numbers on the board are [3,4]. Henry can win by selecting 4 and then dividing it by 2. The integers on the board are now [3,2]. Derek now has a couple of choices:
- Derek can divide 2 by 3, 4, 5 or 6, making it 0 and removing it. Now only one integer remains on the board, 3, and Henry can just divide it by 6 to finish, and win, the game.
- Derek can divide 3 by 4, 5 or 6, making it 0 and removing it. Now only one integer remains on the board, 2, and Henry can just divide it by 6 to finish, and win, the game.
- Derek can divide 2 by 2. Now the integers are [1,3]. Henry can respond by dividing 3 by 3. The integers are now [1,1]. Now Derek has no choice but to divide 1 by 2, 3, 4, 5 or 6 and remove it (because it becomes 0). Henry can respond by dividing the remaining 1 by 2 to finish, and win, the game.
- Derek can divide 3 by 2 or 3. Now the integers are [1,2]. Henry can respond by dividing 2 by 2. The integers are now [1,1]. This leads to a situation as in the previous case and Henry wins.
|
{"inputs": ["2\n2\n3 4\n3\n1 3 5"], "outputs": ["Henry\nDerek"]}
| 682
| 29
|
coding
|
Solve the programming task below in a Python markdown code block.
A permutation p of size n is the sequence p_1, p_2, ..., p_{n}, consisting of n distinct integers, each of them is from 1 to n (1 ≤ p_{i} ≤ n).
A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition p_{p}_{i} = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
-----Input-----
The first line contains integer n (1 ≤ n ≤ 10^5) — the required permutation size.
-----Output-----
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) after a space — the required permutation.
If there are multiple answers, you can print any of them.
-----Examples-----
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4
|
{"inputs": ["1\n", "2\n", "4\n", "5\n", "3\n", "6\n", "7\n", "8\n"], "outputs": ["1 \n", "-1\n", "2 4 1 3 \n", "2 5 3 1 4 \n", "-1\n", "-1\n", "-1\n", "2 8 4 6 3 5 1 7 \n"]}
| 259
| 102
|
coding
|
Solve the programming task below in a Python markdown code block.
A city has been infected by a contagious virus.
In a survey, it was found that A out of the N people living in the city are currently infected.
It has been observed that the only way for a person to get infected is if he comes in contact with an already infected person, and both of them are NOT wearing a mask.
The mayor of the city wants to make a new Mask Policy and find out the minimum number of people that will be required to wear a mask to avoid the further spread of the virus.
Help the mayor in finding this number.
Note: The only aim of the mayor is to stop virus spread, not to mask every infected person.
------ Input Format ------
- The first line contains T - number of test cases. Then the test cases follow.
- The first and only line of each test case contains two integers N and A - the total number of people living in the city and the number of people already affected by the virus respectively.
------ Output Format ------
For each test case, output the minimum number of people that will be required to wear a mask so as to curb the virus spread.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$2 ≤ N ≤ 400$
$1 ≤ A < N$
----- Sample Input 1 ------
3
2 1
3 2
3 1
----- Sample Output 1 ------
1
1
1
----- explanation 1 ------
Test Case #1: There is $1$ infected person and $1$ uninfected person. We can ask any of them to wear a mask, and no more infections will occur. Hence the answer is $1$.
Test Case #2: There are $2$ infected people and $1$ uninfected person. We can ask the uninfected person to wear a mask, and no more infections will occur. Hence the answer is $1$.
Test Case #3: There is $1$ infected person and $2$ uninfected people. We can ask the single infected person to wear a mask, and no more infections will occur. Hence the answer is $1$.
|
{"inputs": ["3\n2 1\n3 2\n3 1"], "outputs": ["1\n1\n1"]}
| 456
| 28
|
coding
|
Solve the programming task below in a Python markdown code block.
You are a person who is always fond of eating candies. Your friend gave you a candy of length N, to eat during the break period of your school.
You start eating this candy from one of the ends. But as it is not your candy, your friend told you to eat exactly K unit length of candy during each bite. You will stop eating if the candy's length becomes 0. This means that you have eaten the entire candy.
If at some point of time, the candy's length is positive, but less than K, you *cannot* take a bite thereafter.
Can you eat the complete candy? If yes, print the number bites it will take, else print -1.
------ Input Format ------
- First line will contain T, number of testcases. Then the testcases follow.
- Each testcase contains of two spaced integers N, K.
------ Output Format ------
For each testcase, print the minimum number of bites you need to eat the complete candy. Print -1 if it is
not possible to do so.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$0 ≤ N ≤ 10^{6}$
$1 ≤ K ≤ 10^{6}$
----- Sample Input 1 ------
3
3 1
3 2
0 3
----- Sample Output 1 ------
3
-1
0
----- explanation 1 ------
Test case 1:
When you eat your first bite of length $1$, the length of the candy will become $2$. After the second bite, the candy's length will be $1$ and finally after the third bite, candy's length will be $0$, and hence you eat the entire candy!
Test case 2:
After the first bite, candy's length will become $1$. You cannot go for the second bite as candy's length is positive and less than $2$.
Test case 3:
Candy's length is already $0$, hence it can be said that you have eaten the entire candy!
|
{"inputs": ["3\n3 1\n3 2\n0 3\n"], "outputs": ["3\n-1\n0\n"]}
| 436
| 31
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problems statements in Mandarin Chinese and Russian.
Time flies fast, Chef’s sons are getting older, stronger and higher. Chef has got two sons and they don’t go well with each other. Their relations are getting worse day by day. Concerned Chef has now decided to separate them. Chef is very rich and he has got N cities (N is even). His sons are all he has and he must divide the cities into his sons equally. There are some roads running between the cities. Chef has to destruct all the roads between cities of first and second sons. The destruction of road costis a lot of money. So Chef wants to save as much as he can. So you have to help Chef in finding out minimal cost to split cities to his sons.
------ Input ------
First line contains two space separated integers N, M denoting the number of cities and the number of the roads respectively.
Each i^{th} of the following M lines contains three integers a_{i}, b_{i} and c_{i} denoting the road between the city with the number a_{i} and the city with the number b_{i}. The cost for it's destruction is c_{i}.
------ Output ------
In a single line, output N / 2 numbers – the numbers of cities that will belong to the first son after the partition.
------ Scoring ------
Your score will be sum of all c_{i} of all edges that would be destroyed by Chef. Your score for the problem will be sum of scores for each test files.
Your solution will be tested against only on 20% of the test files during the contest and will be rejudged against 100% after the end of competition.
------ Constraints ------
$1 ≤ N ≤ 10^{6}$
$1 ≤ M ≤ 10^{6}$
$0 ≤ a_{i}, b_{i} < N$
$1 ≤ c_{i} ≤ 10^{9}$
------ Subtasks ------
Subtask 1:
$1 ≤ N ≤ 100$
$1 ≤ M ≤ 1000$
Subtask 2:
$100 ≤ N ≤ 2*10^{4}$
$1000 ≤ M ≤ 2*10^{5}$
Subtask 3:
$2*10^{4} ≤ N ≤ 10^{5}$
$2*10^{5} ≤ M ≤ 4*10^{5}$
Subtask 4:
$10^{5} ≤ N ≤ 10^{6}$
$4*10^{5} ≤ M ≤ 10^{6}$
------ Example ------
Input:
4 4
0 1 1
1 2 2
2 3 3
3 0 4
Output:
1 2
------ Test data generation ------
Four plans are used:
$Complete graph with random edge weights$
$Complete bipartite graph with random edge weights$
$Random tree$
$Fully random graph$
The first two sections subtasks are generated with the usage of all 4 of them, third section is generated with the usage only of the fourth plan, and fourth section is generated with the plans number 3 and 4.
|
{"inputs": ["4 4\n0 1 1 \n1 2 2\n2 3 3\n3 0 4"], "outputs": ["1 2"]}
| 704
| 41
|
coding
|
Solve the programming task below in a Python markdown code block.
Alice and Bob play a game. There is a paper strip which is divided into n + 1 cells numbered from left to right starting from 0. There is a chip placed in the n-th cell (the last one).
Players take turns, Alice is first. Each player during his or her turn has to move the chip 1, 2 or k cells to the left (so, if the chip is currently in the cell i, the player can move it into cell i - 1, i - 2 or i - k). The chip should not leave the borders of the paper strip: it is impossible, for example, to move it k cells to the left if the current cell has number i < k. The player who can't make a move loses the game.
Who wins if both participants play optimally?
Alice and Bob would like to play several games, so you should determine the winner in each game.
-----Input-----
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of games. Next T lines contain one game per line. All games are independent.
Each of the next T lines contains two integers n and k (0 ≤ n ≤ 10^9, 3 ≤ k ≤ 10^9) — the length of the strip and the constant denoting the third move, respectively.
-----Output-----
For each game, print Alice if Alice wins this game and Bob otherwise.
-----Example-----
Input
4
0 3
3 3
3 4
4 4
Output
Bob
Alice
Bob
Alice
|
{"inputs": ["1\n9 3\n", "1\n8 2\n", "1\n3 7\n", "1\n2 7\n", "1\n0 4\n", "1\n25 6\n", "1\n24 6\n", "1\n25 6\n"], "outputs": ["Alice\n", "Alice\n", "Bob\n", "Alice\n", "Bob\n", "Alice\n", "Bob\n", "Alice\n"]}
| 344
| 105
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given two integer arrays nums1 and nums2, return the maximum length of a subarray that appears in both arrays.
Please complete the following python code precisely:
```python
class Solution:
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(nums1 = [1,2,3,2,1], nums2 = [3,2,1,4,7]) == 3\n assert candidate(nums1 = [0,0,0,0,0], nums2 = [0,0,0,0,0]) == 5\n\n\ncheck(Solution().findLength)"}
| 79
| 89
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a set S of strings consisting of `0` and `1`, and an integer K.
Find the longest string that is a subsequence of K or more different strings in S. If there are multiple strings that satisfy this condition, find the lexicographically smallest such string.
Here, S is given in the format below:
* The data directly given to you is an integer N, and N+1 strings X_0,X_1,...,X_N. For every i (0\leq i\leq N), the length of X_i is 2^i.
* For every pair of two integers (i,j) (0\leq i\leq N,0\leq j\leq 2^i-1), the j-th character of X_i is `1` if and only if the binary representation of j with i digits (possibly with leading zeros) belongs to S. Here, the first and last characters in X_i are called the 0-th and (2^i-1)-th characters, respectively.
* S does not contain a string with length N+1 or more.
Here, a string A is a subsequence of another string B when there exists a sequence of integers t_1 < ... < t_{|A|} such that, for every i (1\leq i\leq |A|), the i-th character of A and the t_i-th character of B is equal.
Constraints
* 0 \leq N \leq 20
* X_i(0\leq i\leq N) is a string of length 2^i consisting of `0` and `1`.
* 1 \leq K \leq |S|
* K is an integer.
Input
Input is given from Standard Input in the following format:
N K
X_0
:
X_N
Output
Print the lexicographically smallest string among the longest strings that are subsequences of K or more different strings in S.
Examples
Input
3 4
1
01
1011
01001110
Output
10
Input
4 6
1
01
1011
10111010
1101110011111101
Output
100
Input
2 5
0
11
1111
Output
|
{"inputs": ["2 5\n0\n11\n1111", "3 0\n1\n3\n1111\n01000110", "3 2\n1\n2\n1011\n01001110", "3 2\n1\n2\n1011\n01000110", "3 2\n1\n2\n1111\n01000110", "3 2\n1\n3\n1111\n01000110", "3 0\n1\n3\n1111\n00000110", "3 2\n1\n2\n1111\n01001110"], "outputs": ["", "000\n", "00\n", "00\n", "00\n", "00\n", "000\n", "00\n"]}
| 531
| 221
|
coding
|
Solve the programming task below in a Python markdown code block.
A remote island chain contains n islands, labeled 1 through n. Bidirectional bridges connect the islands to form a simple cycle — a bridge connects islands 1 and 2, islands 2 and 3, and so on, and additionally a bridge connects islands n and 1. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.
The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: First, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.
Determine if it is possible for the islanders to arrange the statues in the desired order.
Input
The first line contains a single integer n (2 ≤ n ≤ 200 000) — the total number of islands.
The second line contains n space-separated integers ai (0 ≤ ai ≤ n - 1) — the statue currently placed on the i-th island. If ai = 0, then the island has no statue. It is guaranteed that the ai are distinct.
The third line contains n space-separated integers bi (0 ≤ bi ≤ n - 1) — the desired statues of the ith island. Once again, bi = 0 indicates the island desires no statue. It is guaranteed that the bi are distinct.
Output
Print "YES" (without quotes) if the rearrangement can be done in the existing network, and "NO" otherwise.
Examples
Input
3
1 0 2
2 0 1
Output
YES
Input
2
1 0
0 1
Output
YES
Input
4
1 2 3 0
0 3 2 1
Output
NO
Note
In the first sample, the islanders can first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3.
In the second sample, the islanders can simply move statue 1 from island 1 to island 2.
In the third sample, no sequence of movements results in the desired position.
|
{"inputs": ["2\n0 1\n0 1\n", "2\n1 0\n1 0\n", "2\n1 0\n0 1\n", "3\n0 1 2\n0 1 2\n", "3\n0 1 2\n1 0 2\n", "3\n0 2 1\n2 0 1\n", "3\n0 2 1\n1 2 0\n", "3\n2 0 1\n1 0 2\n"], "outputs": ["YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES"]}
| 508
| 146
|
coding
|
Solve the programming task below in a Python markdown code block.
Princess Artapoelc greeted her guests by either kissing on the cheek (K) or hugging (H). From the first guest
she kisses, she has a
compulsion to necessarily kiss every alternate guest from that first kissed guest. That is if the guests are G_{1},
G_{2}, ..., G_{i}, G_{i+1}, ..., G_{n} and if she first kissed G_{i} then she must necessarily kiss
G_{i+2}, G_{i+4}, G_{i+6} ... till the last
possible guest. Your task is to determine in how many ways she can greet N guests.
Input
First line of the input contains T (T ≤ 1000) denoting the number of test cases.
T
lines follow each containing a single integer N (1 ≤ N ≤ 10^{9}) denoting the number of guests.
Output
For each case the output should be a single integer representing the number of ways Artapoelc can greet N guests. As the
answer can be large
print it modulo 1000000007.
----- Sample Input 1 ------
3
1
2
3
----- Sample Output 1 ------
2
4
6
----- explanation 1 ------
In the first case the possible ways are
K, H
Second case:
KH, HK, HH, KK
Third case:
HHH, HHK, HKH, HKK, KHK, KKK
|
{"inputs": ["3\n1\n2\n3"], "outputs": ["2\n4\n6"]}
| 337
| 22
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given an array of integers nums, return the number of good pairs.
A pair (i, j) is called good if nums[i] == nums[j] and i < j.
Please complete the following python code precisely:
```python
class Solution:
def numIdenticalPairs(self, nums: List[int]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(nums = [1,2,3,1,1,3]) == 4\n assert candidate(nums = [1,1,1,1]) == 6\n assert candidate(nums = [1,2,3]) == 0\n\n\ncheck(Solution().numIdenticalPairs)"}
| 87
| 77
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a sequence of positive integers of length N, a = (a_1, a_2, ..., a_N).
Your objective is to remove some of the elements in a so that a will be a good sequence.
Here, an sequence b is a good sequence when the following condition holds true:
- For each element x in b, the value x occurs exactly x times in b.
For example, (3, 3, 3), (4, 2, 4, 1, 4, 2, 4) and () (an empty sequence) are good sequences, while (3, 3, 3, 3) and (2, 4, 1, 4, 2) are not.
Find the minimum number of elements that needs to be removed so that a will be a good sequence.
-----Constraints-----
- 1 \leq N \leq 10^5
- a_i is an integer.
- 1 \leq a_i \leq 10^9
-----Input-----
Input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
-----Output-----
Print the minimum number of elements that needs to be removed so that a will be a good sequence.
-----Sample Input-----
4
3 3 3 3
-----Sample Output-----
1
We can, for example, remove one occurrence of 3. Then, (3, 3, 3) is a good sequence.
|
{"inputs": ["4\n3 3 3 3\n", "5\n2 4 1 4 2\n", "1\n1000000000\n", "6\n1 2 2 3 3 3\n", "8\n2 7 1 8 2 8 1 8\n"], "outputs": ["1\n", "2\n", "1\n", "0\n", "5\n"]}
| 331
| 103
|
coding
|
Solve the programming task below in a Python markdown code block.
You want to arrange n integers a_1, a_2, ..., a_{n} in some order in a row. Let's define the value of an arrangement as the sum of differences between all pairs of adjacent integers.
More formally, let's denote some arrangement as a sequence of integers x_1, x_2, ..., x_{n}, where sequence x is a permutation of sequence a. The value of such an arrangement is (x_1 - x_2) + (x_2 - x_3) + ... + (x_{n} - 1 - x_{n}).
Find the largest possible value of an arrangement. Then, output the lexicographically smallest sequence x that corresponds to an arrangement of the largest possible value.
-----Input-----
The first line of the input contains integer n (2 ≤ n ≤ 100). The second line contains n space-separated integers a_1, a_2, ..., a_{n} (|a_{i}| ≤ 1000).
-----Output-----
Print the required sequence x_1, x_2, ..., x_{n}. Sequence x should be the lexicographically smallest permutation of a that corresponds to an arrangement of the largest possible value.
-----Examples-----
Input
5
100 -100 50 0 -50
Output
100 -50 0 50 -100
-----Note-----
In the sample test case, the value of the output arrangement is (100 - ( - 50)) + (( - 50) - 0) + (0 - 50) + (50 - ( - 100)) = 200. No other arrangement has a larger value, and among all arrangements with the value of 200, the output arrangement is the lexicographically smallest one.
Sequence x_1, x_2, ... , x_{p} is lexicographically smaller than sequence y_1, y_2, ... , y_{p} if there exists an integer r (0 ≤ r < p) such that x_1 = y_1, x_2 = y_2, ... , x_{r} = y_{r} and x_{r} + 1 < y_{r} + 1.
|
{"inputs": ["2\n0 0\n", "2\n0 0\n", "2\n0 1\n", "2\n0 2\n", "2\n0 4\n", "2\n0 3\n", "2\n0 -1\n", "2\n-1 -1\n"], "outputs": ["0 0 \n", "0 0\n", "1 0\n", "2 0\n", "4 0\n", "3 0\n", "0 -1\n", "-1 -1\n"]}
| 499
| 120
|
coding
|
Solve the programming task below in a Python markdown code block.
Implement `String#ipv4_address?`, which should return true if given object is an IPv4 address - four numbers (0-255) separated by dots.
It should only accept addresses in canonical representation, so no leading `0`s, spaces etc.
Also feel free to reuse/extend the following starter code:
```python
def ipv4_address(address):
```
|
{"functional": "_inputs = [[''], ['127.0.0.1'], ['0.0.0.0'], ['255.255.255.255'], ['10.20.30.40'], ['10.256.30.40'], ['10.20.030.40'], ['127.0.1'], ['127.0.0.0.1'], ['..255.255'], ['127.0.0.1\\n'], ['\\n127.0.0.1'], [' 127.0.0.1'], ['127.0.0.1 '], [' 127.0.0.1 '], ['127.0.0.1.'], ['.127.0.0.1'], ['127..0.1']]\n_outputs = [[False], [True], [True], [True], [True], [False], [False], [False], [False], [False], [False], [False], [False], [False], [False], [False], [False], [False]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(ipv4_address(*i), o[0])"}
| 89
| 411
|
coding
|
Solve the programming task below in a Python markdown code block.
You have an $n \times n$ chessboard and $k$ rooks. Rows of this chessboard are numbered by integers from $1$ to $n$ from top to bottom and columns of this chessboard are numbered by integers from $1$ to $n$ from left to right. The cell $(x, y)$ is the cell on the intersection of row $x$ and collumn $y$ for $1 \leq x \leq n$ and $1 \leq y \leq n$.
The arrangement of rooks on this board is called good, if no rook is beaten by another rook.
A rook beats all the rooks that shares the same row or collumn with it.
The good arrangement of rooks on this board is called not stable, if it is possible to move one rook to the adjacent cell so arrangement becomes not good. Otherwise, the good arrangement is stable. Here, adjacent cells are the cells that share a side.
Such arrangement of $3$ rooks on the $4 \times 4$ chessboard is good, but it is not stable: the rook from $(1, 1)$ can be moved to the adjacent cell $(2, 1)$ and rooks on cells $(2, 1)$ and $(2, 4)$ will beat each other.
Please, find any stable arrangement of $k$ rooks on the $n \times n$ chessboard or report that there is no such arrangement.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 100$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($1 \leq k \leq n \leq 40$) — the size of the chessboard and the number of rooks.
-----Output-----
If there is a stable arrangement of $k$ rooks on the $n \times n$ chessboard, output $n$ lines of symbols . and R. The $j$-th symbol of the $i$-th line should be equals R if and only if there is a rook on the cell $(i, j)$ in your arrangement.
If there are multiple solutions, you may output any of them.
If there is no stable arrangement, output $-1$.
-----Examples-----
Input
5
3 2
3 3
1 1
5 2
40 33
Output
..R
...
R..
-1
R
.....
R....
.....
....R
.....
-1
-----Note-----
In the first test case, you should find stable arrangement of $2$ rooks on the $3 \times 3$ chessboard. Placing them in cells $(3, 1)$ and $(1, 3)$ gives stable arrangement.
In the second test case it can be shown that it is impossbile to place $3$ rooks on the $3 \times 3$ chessboard to get stable arrangement.
|
{"inputs": ["5\n3 2\n3 3\n1 1\n5 2\n40 33\n"], "outputs": ["R..\n...\n..R\n-1\nR\nR....\n.....\n..R..\n.....\n.....\n-1\n"]}
| 658
| 67
|
coding
|
Solve the programming task below in a Python markdown code block.
Your task is to generate the Fibonacci sequence to `n` places, with each alternating value as `"skip"`. For example:
`"1 skip 2 skip 5 skip 13 skip 34"`
Return the result as a string
You can presume that `n` is always a positive integer between (and including) 1 and 64.
Also feel free to reuse/extend the following starter code:
```python
def skiponacci(n):
```
|
{"functional": "_inputs = [[1], [5], [7]]\n_outputs = [['1'], ['1 skip 2 skip 5'], ['1 skip 2 skip 5 skip 13']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(skiponacci(*i), o[0])"}
| 109
| 182
|
coding
|
Solve the programming task below in a Python markdown code block.
Takahashi has two positive integers A and B.
It is known that A plus B equals N. Find the minimum possible value of "the sum of the digits of A" plus "the sum of the digits of B" (in base 10).
Constraints
* 2 ≤ N ≤ 10^5
* N is an integer.
Input
Input is given from Standard Input in the following format:
N
Output
Print the minimum possible value of "the sum of the digits of A" plus "the sum of the digits of B".
Examples
Input
15
Output
6
Input
100000
Output
10
|
{"inputs": ["3", "5", "7", "4", "9", "8", "2", "6"], "outputs": ["3\n", "5\n", "7\n", "4\n", "9\n", "8\n", "2\n", "6\n"]}
| 152
| 62
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given the array nums consisting of n positive integers. You computed the sum of all non-empty continuous subarrays from the array and then sorted them in non-decreasing order, creating a new array of n * (n + 1) / 2 numbers.
Return the sum of the numbers from index left to index right (indexed from 1), inclusive, in the new array. Since the answer can be a huge number return it modulo 109 + 7.
Please complete the following python code precisely:
```python
class Solution:
def rangeSum(self, nums: List[int], n: int, left: int, right: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(nums = [1,2,3,4], n = 4, left = 1, right = 5) == 13 \n assert candidate(nums = [1,2,3,4], n = 4, left = 3, right = 4) == 6\n assert candidate(nums = [1,2,3,4], n = 4, left = 1, right = 10) == 50\n\n\ncheck(Solution().rangeSum)"}
| 158
| 122
|
coding
|
Solve the programming task below in a Python markdown code block.
In this Kata, you will be given two integers `n` and `k` and your task is to remove `k-digits` from `n` and return the lowest number possible, without changing the order of the digits in `n`. Return the result as a string.
Let's take an example of `solve(123056,4)`. We need to remove `4` digits from `123056` and return the lowest possible number. The best digits to remove are `(1,2,3,6)` so that the remaining digits are `'05'`. Therefore, `solve(123056,4) = '05'`.
Note also that the order of the numbers in `n` does not change: `solve(1284569,2) = '12456',` because we have removed `8` and `9`.
More examples in the test cases.
Good luck!
Also feel free to reuse/extend the following starter code:
```python
def solve(n, k):
```
|
{"functional": "_inputs = [[123056, 1], [123056, 2], [123056, 3], [123056, 4], [1284569, 1], [1284569, 2], [1284569, 3], [1284569, 4]]\n_outputs = [['12056'], ['1056'], ['056'], ['05'], ['124569'], ['12456'], ['1245'], ['124']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(solve(*i), o[0])"}
| 242
| 288
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given an integer n representing the number of nodes in a perfect binary tree consisting of nodes numbered from 1 to n. The root of the tree is node 1 and each node i in the tree has two children where the left child is the node 2 * i and the right child is 2 * i + 1.
Each node in the tree also has a cost represented by a given 0-indexed integer array cost of size n where cost[i] is the cost of node i + 1. You are allowed to increment the cost of any node by 1 any number of times.
Return the minimum number of increments you need to make the cost of paths from the root to each leaf node equal.
Note:
A perfect binary tree is a tree where each node, except the leaf nodes, has exactly 2 children.
The cost of a path is the sum of costs of nodes in the path.
Please complete the following python code precisely:
```python
class Solution:
def minIncrements(self, n: int, cost: List[int]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(n = 7, cost = [1,5,2,2,3,3,1]) == 6\n assert candidate(n = 3, cost = [5,3,3]) == 0\n\n\ncheck(Solution().minIncrements)"}
| 240
| 70
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given an integer N.
Construct any permutation A of N integers such that the value LCM(1 + A_{1} , 2 + A_{2}, \ldots, N + A_{N}) is minimised.
Note:
LCM denotes the [least common multiple] of the integers.
A permutation of N integers contains all integers from 1 to N exactly once.
------ Input Format ------
- First line will contain T, number of test cases. Then the test cases follow.
- Each test case contains a single line of input, one integer N.
------ Output Format ------
For each test case, output a permutation that minimises the value LCM(1 + A_{1} , 2 + A_{2}, \ldots, N + A_{N}).
------ Constraints ------
$1 ≤ T ≤ 1000$
$2 ≤ N ≤ 1000$
----- Sample Input 1 ------
1
2
----- Sample Output 1 ------
2 1
----- explanation 1 ------
Test case $1$: The permutation for which the value is minimised is $[2, 1]$. The minimum value is $LCM(1+2, 2+1) = LCM(3, 3) = 3$.
|
{"inputs": ["1\n2\n"], "outputs": ["2 1 \n"]}
| 286
| 19
|
coding
|
Solve the programming task below in a Python markdown code block.
Snuke has decided to use a robot to clean his room.
There are N pieces of trash on a number line. The i-th piece from the left is at position x_i. We would like to put all of them in a trash bin at position 0.
For the positions of the pieces of trash, 0 < x_1 < x_2 < ... < x_{N} \leq 10^{9} holds.
The robot is initially at position 0. It can freely move left and right along the number line, pick up a piece of trash when it comes to the position of that piece, carry any number of pieces of trash and put them in the trash bin when it comes to position 0. It is not allowed to put pieces of trash anywhere except in the trash bin.
The robot consumes X points of energy when the robot picks up a piece of trash, or put pieces of trash in the trash bin. (Putting any number of pieces of trash in the trash bin consumes X points of energy.) Also, the robot consumes (k+1)^{2} points of energy to travel by a distance of 1 when the robot is carrying k pieces of trash.
Find the minimum amount of energy required to put all the N pieces of trash in the trash bin.
Constraints
* 1 \leq N \leq 2 \times 10^{5}
* 0 < x_1 < ... < x_N \leq 10^9
* 1 \leq X \leq 10^9
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X
x_1 x_2 ... x_{N}
Output
Print the answer.
Examples
Input
2 100
1 10
Output
355
Input
5 1
1 999999997 999999998 999999999 1000000000
Output
19999999983
Input
10 8851025
38 87 668 3175 22601 65499 90236 790604 4290609 4894746
Output
150710136
Input
16 10
1 7 12 27 52 75 731 13856 395504 534840 1276551 2356789 9384806 19108104 82684732 535447408
Output
3256017715
|
{"inputs": ["2 100\n1 13", "2 100\n1 25", "2 101\n1 25", "2 101\n1 42", "2 101\n1 10", "2 100\n0 13", "2 001\n1 25", "2 101\n0 10"], "outputs": ["370\n", "430\n", "433\n", "518\n", "358\n", "365\n", "133\n", "353\n"]}
| 656
| 150
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given a 0-indexed integer array nums of length n.
The sum score of nums at an index i where 0 <= i < n is the maximum of:
The sum of the first i + 1 elements of nums.
The sum of the last n - i elements of nums.
Return the maximum sum score of nums at any index.
Please complete the following python code precisely:
```python
class Solution:
def maximumSumScore(self, nums: List[int]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(nums = [4,3,-2,5]) == 10\n assert candidate(nums = [-3,-5]) == -3\n\n\ncheck(Solution().maximumSumScore)"}
| 121
| 53
|
coding
|
Solve the programming task below in a Python markdown code block.
Your job is to change the given string `s` using a non-negative integer `n`.
Each bit in `n` will specify whether or not to swap the case for each alphabetic character in `s`: if the bit is `1`, swap the case; if its `0`, leave it as is. When you finished with the last bit of `n`, start again with the first bit.
You should skip the checking of bits when a non-alphabetic character is encountered, but they should be preserved in their original positions.
## Examples
```
swap('Hello world!', 11) --> 'heLLO wORLd!'
```
...because `11` is `1011` in binary, so the 1st, 3rd, 4th, 5th, 7th, 8th and 9th alphabetical characters have to be swapped:
```
H e l l o w o r l d !
1 0 1 1 1 0 1 1 1 0
^ ^ ^ ^ ^ ^ ^
```
More examples:
```
swap("gOOd MOrniNg", 7864) --> "GooD MorNIng"
swap('', 11345) --> ''
swap('the lord of the rings', 0) --> 'the lord of the rings'
```
Also feel free to reuse/extend the following starter code:
```python
def swap(s,n):
```
|
{"functional": "_inputs = [['Hello world!', 11], ['the quick broWn fox leapt over the fence', 9], ['eVerybody likes ice cReam', 85], ['gOOd MOrniNg', 7864], ['how are you today?', 12345], ['the lord of the rings', 0], ['', 11345]]\n_outputs = [['heLLO wORLd!'], ['The QUicK BrowN foX LeaPT ovER thE FenCE'], ['EVErYbODy LiKeS IcE creAM'], ['GooD MorNIng'], ['HOw are yoU TOdaY?'], ['the lord of the rings'], ['']]\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(swap(*i), o[0])"}
| 337
| 306
|
coding
|
Solve the programming task below in a Python markdown code block.
In some other world, today is Christmas.
Mr. Takaha decides to make a multi-dimensional burger in his party. A level-L burger (L is an integer greater than or equal to 0) is the following thing:
- A level-0 burger is a patty.
- A level-L burger (L \geq 1) is a bun, a level-(L-1) burger, a patty, another level-(L-1) burger and another bun, stacked vertically in this order from the bottom.
For example, a level-1 burger and a level-2 burger look like BPPPB and BBPPPBPBPPPBB (rotated 90 degrees), where B and P stands for a bun and a patty.
The burger Mr. Takaha will make is a level-N burger. Lunlun the Dachshund will eat X layers from the bottom of this burger (a layer is a patty or a bun). How many patties will she eat?
-----Constraints-----
- 1 \leq N \leq 50
- 1 \leq X \leq ( the total number of layers in a level-N burger )
- N and X are integers.
-----Input-----
Input is given from Standard Input in the following format:
N X
-----Output-----
Print the number of patties in the bottom-most X layers from the bottom of a level-N burger.
-----Sample Input-----
2 7
-----Sample Output-----
4
There are 4 patties in the bottom-most 7 layers of a level-2 burger (BBPPPBPBPPPBB).
|
{"inputs": ["2 1", "4 7", "0 1", "1 0", "8 7", "1 2", "2 5", "4 1"], "outputs": ["0\n", "3\n", "1\n", "0\n", "0\n", "1\n", "3\n", "0\n"]}
| 347
| 78
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?
Constraints
* 1 \leq N \leq 10^5
* 1 \leq A_i \leq 10^9
* Each A_i is an integer.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the minimum possible number of subarrays after division of A.
Examples
Input
6
1 2 3 2 2 1
Output
2
Input
9
1 2 1 2 1 2 1 2 1
Output
5
Input
7
1 2 3 2 1 999999999 1000000000
Output
3
|
{"inputs": ["6\n1 2 3 2 0 1", "6\n1 2 3 2 0 0", "6\n0 2 3 2 0 0", "6\n0 2 3 1 0 0", "6\n1 2 3 2 2 1", "6\n0 2 3 1 -1 0", "6\n0 2 3 0 -1 0", "6\n0 2 3 -1 -1 0"], "outputs": ["3\n", "2\n", "2\n", "2\n", "2", "3\n", "3\n", "2\n"]}
| 238
| 157
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
There are n servers numbered from 0 to n - 1 connected by undirected server-to-server connections forming a network where connections[i] = [ai, bi] represents a connection between servers ai and bi. Any server can reach other servers directly or indirectly through the network.
A critical connection is a connection that, if removed, will make some servers unable to reach some other server.
Return all critical connections in the network in any order.
Please complete the following python code precisely:
```python
class Solution:
def criticalConnections(self, n: int, connections: List[List[int]]) -> List[List[int]]:
```
|
{"functional": "def check(candidate):\n assert candidate(n = 4, connections = [[0,1],[1,2],[2,0],[1,3]]) == [[1,3]]\n assert candidate(n = 2, connections = [[0,1]]) == [[0,1]]\n\n\ncheck(Solution().criticalConnections)"}
| 144
| 77
|
coding
|
Solve the programming task below in a Python markdown code block.
Awruk is taking part in elections in his school. It is the final round. He has only one opponent — Elodreip. The are $n$ students in the school. Each student has exactly $k$ votes and is obligated to use all of them. So Awruk knows that if a person gives $a_i$ votes for Elodreip, than he will get exactly $k - a_i$ votes from this person. Of course $0 \le k - a_i$ holds.
Awruk knows that if he loses his life is over. He has been speaking a lot with his friends and now he knows $a_1, a_2, \dots, a_n$ — how many votes for Elodreip each student wants to give. Now he wants to change the number $k$ to win the elections. Of course he knows that bigger $k$ means bigger chance that somebody may notice that he has changed something and then he will be disqualified.
So, Awruk knows $a_1, a_2, \dots, a_n$ — how many votes each student will give to his opponent. Help him select the smallest winning number $k$. In order to win, Awruk needs to get strictly more votes than Elodreip.
-----Input-----
The first line contains integer $n$ ($1 \le n \le 100$) — the number of students in the school.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 100$) — the number of votes each student gives to Elodreip.
-----Output-----
Output the smallest integer $k$ ($k \ge \max a_i$) which gives Awruk the victory. In order to win, Awruk needs to get strictly more votes than Elodreip.
-----Examples-----
Input
5
1 1 1 5 1
Output
5
Input
5
2 2 3 2 2
Output
5
-----Note-----
In the first example, Elodreip gets $1 + 1 + 1 + 5 + 1 = 9$ votes. The smallest possible $k$ is $5$ (it surely can't be less due to the fourth person), and it leads to $4 + 4 + 4 + 0 + 4 = 16$ votes for Awruk, which is enough to win.
In the second example, Elodreip gets $11$ votes. If $k = 4$, Awruk gets $9$ votes and loses to Elodreip.
|
{"inputs": ["1\n1\n", "1\n1\n", "1\n2\n", "1\n100\n", "2\n1 1\n", "2\n1 4\n", "2\n1 1\n", "1\n100\n"], "outputs": ["3", "3\n", "5\n", "201", "3", "6", "3\n", "201\n"]}
| 579
| 96
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given two strings word1 and word2, return the minimum number of operations required to convert word1 to word2.
You have the following three operations permitted on a word:
Insert a character
Delete a character
Replace a character
Please complete the following python code precisely:
```python
class Solution:
def minDistance(self, word1: str, word2: str) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(word1 = \"horse\", word2 = \"ros\") == 3\n assert candidate(word1 = \"intention\", word2 = \"execution\") == 5\n\n\ncheck(Solution().minDistance)"}
| 100
| 58
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given an integer array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Please complete the following python code precisely:
```python
class Solution:
def minSetSize(self, arr: List[int]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(arr = [3,3,3,3,5,5,5,2,2,7]) == 2\n assert candidate(arr = [7,7,7,7,7,7]) == 1\n\n\ncheck(Solution().minSetSize)"}
| 97
| 72
|
coding
|
Solve the programming task below in a Python markdown code block.
You have an array $a$ of size $n$ consisting only of zeroes and ones. You can do the following operation:
choose two indices $1 \le i , j \le n$, $i \ne j$,
add $a_{i}$ to $a_{j}$,
remove $a_{i}$ from $a$.
Note that elements of $a$ can become bigger than $1$ after performing some operations. Also note that $n$ becomes $1$ less after the operation.
What is the minimum number of operations needed to make $a$ non-decreasing, i. e. that each element is not less than the previous element?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains an integer $n$ ($1 \le n \le 10^5$), the size of array $a$.
Next line contains $n$ integers $a_{1}, a_{2}, \ldots a_{n}$ ($a_i$ is $0$ or $1$), elements of array $a$.
It's guaranteed that sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case print a single integer, minimum number of operations needed to make $a$ non-decreasing.
-----Examples-----
Input
4
8
0 0 1 1 1 1 1 1
5
1 0 0 1 1
2
1 0
11
1 1 0 0 1 0 0 1 1 1 0
Output
0
1
1
3
-----Note-----
In the first test case, $a$ is already non-decreasing, so you don't need to do any operations and the answer is $0$.
In the second test case, you can perform an operation for $i = 1$ and $j = 5$, so $a$ will be equal to $[0, 0, 1, 2]$ and it becomes non-decreasing.
In the third test case, you can perform an operation for $i = 2$ and $j = 1$, so $a$ will be equal to $[1]$ and it becomes non-decreasing.
|
{"inputs": ["4\n8\n0 0 1 1 1 1 1 1\n5\n1 0 0 1 1\n2\n1 0\n11\n1 1 0 0 1 0 0 1 1 1 0\n"], "outputs": ["0\n1\n1\n3\n"]}
| 535
| 81
|
coding
|
Solve the programming task below in a Python markdown code block.
Constraints
* 1 ≤ |V| ≤ 100
* 0 ≤ |E| ≤ 9900
* -2 × 107 ≤ di ≤ 2 × 107
* There are no parallel edges
* There are no self-loops
Input
An edge-weighted graph G (V, E).
|V| |E|
s0 t0 d0
s1 t1 d1
:
s|E|-1 t|E|-1 d|E|-1
|V| is the number of vertices and |E| is the number of edges in G. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target vertices of i-th edge (directed) and di represents the cost of the i-th edge.
Output
If the graph contains a negative cycle (a cycle whose sum of edge costs is a negative value), print
NEGATIVE CYCLE
in a line.
Otherwise, print
D0,0 D0,1 ... D0,|V|-1
D1,0 D1,1 ... D1,|V|-1
:
D|V|-1,0 D1,1 ... D|V|-1,|V|-1
The output consists of |V| lines. For each ith line, print the cost of the shortest path from vertex i to each vertex j (j = 0, 1, ... |V|-1) respectively. If there is no path from vertex i to vertex j, print "INF". Print a space between the costs.
Examples
Input
4 6
0 1 1
0 2 5
1 2 2
1 3 4
2 3 1
3 2 7
Output
0 1 3 4
INF 0 2 3
INF INF 0 1
INF INF 7 0
Input
4 6
0 1 1
0 2 -5
1 2 2
1 3 4
2 3 1
3 2 7
Output
0 1 -5 -4
INF 0 2 3
INF INF 0 1
INF INF 7 0
Input
4 6
0 1 1
0 2 5
1 2 2
1 3 4
2 3 1
3 2 -7
Output
NEGATIVE CYCLE
|
{"inputs": ["4 6\n0 1 1\n0 2 5\n1 2 3\n1 3 4\n2 3 1\n3 2 7", "4 6\n0 1 1\n0 2 5\n1 2 3\n0 3 4\n2 3 1\n3 2 7", "4 6\n0 1 1\n0 2 5\n1 2 3\n0 3 4\n2 3 1\n0 2 7", "4 6\n0 1 1\n0 2 5\n1 2 3\n0 2 4\n2 3 1\n0 2 7", "4 6\n0 1 1\n0 2 0\n1 2 2\n0 3 4\n2 3 1\n3 2 7", "4 6\n0 1 1\n0 2 5\n1 2 3\n0 3 3\n2 3 1\n3 2 7", "4 6\n0 1 1\n0 2 5\n1 3 3\n0 2 4\n2 3 1\n0 2 7", "4 6\n0 1 1\n0 2 0\n1 2 2\n0 2 4\n2 3 1\n3 2 7"], "outputs": ["0 1 4 5\nINF 0 3 4\nINF INF 0 1\nINF INF 7 0\n", "0 1 4 4\nINF 0 3 4\nINF INF 0 1\nINF INF 7 0\n", "0 1 4 4\nINF 0 3 4\nINF INF 0 1\nINF INF INF 0\n", "0 1 4 5\nINF 0 3 4\nINF INF 0 1\nINF INF INF 0\n", "0 1 0 1\nINF 0 2 3\nINF INF 0 1\nINF INF 7 0\n", "0 1 4 3\nINF 0 3 4\nINF INF 0 1\nINF INF 7 0\n", "0 1 7 4\nINF 0 INF 3\nINF INF 0 1\nINF INF INF 0\n", "0 1 3 4\nINF 0 2 3\nINF INF 0 1\nINF INF 7 0\n"]}
| 555
| 586
|
coding
|
Solve the programming task below in a Python markdown code block.
You will receive 3 points for solving this problem.
Manao is designing the genetic code for a new type of algae to efficiently produce fuel. Specifically, Manao is focusing on a stretch of DNA that encodes one protein. The stretch of DNA is represented by a string containing only the characters 'A', 'T', 'G' and 'C'.
Manao has determined that if the stretch of DNA contains a maximal sequence of consecutive identical nucleotides that is of even length, then the protein will be nonfunctional. For example, consider a protein described by DNA string "GTTAAAG". It contains four maximal sequences of consecutive identical nucleotides: "G", "TT", "AAA", and "G". The protein is nonfunctional because sequence "TT" has even length.
Manao is trying to obtain a functional protein from the protein he currently has. Manao can insert additional nucleotides into the DNA stretch. Each additional nucleotide is a character from the set {'A', 'T', 'G', 'C'}. Manao wants to determine the minimum number of insertions necessary to make the DNA encode a functional protein.
-----Input-----
The input consists of a single line, containing a string s of length n (1 ≤ n ≤ 100). Each character of s will be from the set {'A', 'T', 'G', 'C'}.
This problem doesn't have subproblems. You will get 3 points for the correct submission.
-----Output-----
The program should print on one line a single integer representing the minimum number of 'A', 'T', 'G', 'C' characters that are required to be inserted into the input string in order to make all runs of identical characters have odd length.
-----Examples-----
Input
GTTAAAG
Output
1
Input
AACCAACCAAAAC
Output
5
-----Note-----
In the first example, it is sufficient to insert a single nucleotide of any type between the two 'T's in the sequence to restore the functionality of the protein.
|
{"inputs": ["A\n", "G\n", "T\n", "C\n", "C\n", "G\n", "A\n", "T\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
| 434
| 70
|
coding
|
Solve the programming task below in a Python markdown code block.
There are two lists of different length. The first one consists of keys, the second one consists of values. Write a function ```createDict(keys, values)``` that returns a dictionary created from keys and values. If there are not enough values, the rest of keys should have a ```None``` value. If there not enough keys, just ignore the rest of values.
Example 1:
```python
keys = ['a', 'b', 'c', 'd']
values = [1, 2, 3]
createDict(keys, values) # returns {'a': 1, 'b': 2, 'c': 3, 'd': None}
```
Example 2:
```python
keys = ['a', 'b', 'c']
values = [1, 2, 3, 4]
createDict(keys, values) # returns {'a': 1, 'b': 2, 'c': 3}
```
Also feel free to reuse/extend the following starter code:
```python
def createDict(keys, values):
```
|
{"functional": "_inputs = [[[], []], [[], [1]], [['a'], []], [['a', 'b', 'c'], [1, 2, 3]], [['a', 'b', 'c', 'd', 'e'], [1, 2]], [['a', 'b'], [1, 2, 3, 4]]]\n_outputs = [[{}], [{}], [{'a': None}], [{'a': 1, 'b': 2, 'c': 3}], [{'a': 1, 'b': 2, 'c': None, 'd': None, 'e': None}], [{'a': 1, 'b': 2}]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(createDict(*i), o[0])"}
| 236
| 288
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given a 0-indexed integer array nums. You can apply the following operation any number of times:
Pick any element from nums and put it at the end of nums.
The prefix sum array of nums is an array prefix of the same length as nums such that prefix[i] is the sum of all the integers nums[j] where j is in the inclusive range [0, i].
Return the minimum number of operations such that the prefix sum array does not contain negative integers. The test cases are generated such that it is always possible to make the prefix sum array non-negative.
Please complete the following python code precisely:
```python
class Solution:
def makePrefSumNonNegative(self, nums: List[int]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(nums = [2,3,-5,4]) == 0\n assert candidate(nums = [3,-5,-2,6]) == 1\n\n\ncheck(Solution().makePrefSumNonNegative)"}
| 169
| 58
|
coding
|
Solve the programming task below in a Python markdown code block.
# Task
Your task is to find the similarity of given sorted arrays `a` and `b`, which is defined as follows:
you take the number of elements which are present in both arrays and divide it by the number of elements which are present in at least one array.
It also can be written as a formula `similarity(A, B) = #(A ∩ B) / #(A ∪ B)`, where `#(C)` is the number of elements in C, `∩` is intersection of arrays, `∪` is union of arrays.
This is known as `Jaccard similarity`.
The result is guaranteed to fit any floating-point type without rounding.
# Example
For `a = [1, 2, 4, 6, 7]` and `b = [2, 3, 4, 7]`:
```
elements [2, 4, 7] are present in both arrays;
elements [1, 2, 3, 4, 6, 7] are present in at least one of the arrays.
So the similarity equals to 3 / 6 = 0.5.```
# Input/Output
- `[input]` integer array `a`
A `sorted` array of positive integers.
All elements are `different` and are `less than 100`.
`1 ≤ a.length ≤ 100`
- `[input]` integer array `b`
An array in the same format as `a`.
- `[output]` a float number
The similarity of the arrays.
```Haskell
In Haskell the two arrays are passed as a touple.
```
Also feel free to reuse/extend the following starter code:
```python
def similarity(a, b):
```
|
{"functional": "_inputs = [[[1, 2, 3], [1, 2, 3]], [[1, 2, 3], [4, 5, 6]], [[1, 2, 4, 6, 7], [2, 3, 4, 7]], [[1, 2, 6, 8, 9], [0, 1, 4, 5, 6, 8, 9]], [[0, 1, 3, 4, 5, 6, 9, 14, 15, 16, 17, 18, 19], [1, 4, 10, 12, 13, 14, 15, 16]]]\n_outputs = [[1], [0], [0.5], [0.5], [0.3125]]\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(similarity(*i), o[0])"}
| 397
| 346
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given a string s, determine if it is valid.
A string s is valid if, starting with an empty string t = "", you can transform t into s after performing the following operation any number of times:
Insert string "abc" into any position in t. More formally, t becomes tleft + "abc" + tright, where t == tleft + tright. Note that tleft and tright may be empty.
Return true if s is a valid string, otherwise, return false.
Please complete the following python code precisely:
```python
class Solution:
def isValid(self, s: str) -> bool:
```
|
{"functional": "def check(candidate):\n assert candidate(s = \"aabcbc\") == True\n assert candidate(s = \"abcabcababcc\") == True\n assert candidate(s = \"abccba\") == False\n\n\ncheck(Solution().isValid)"}
| 148
| 58
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Chef has 3 boxes of sizes A, B, and C respectively. He puts the boxes in bags of size D (A ≤ B ≤ C ≤ D). Find the minimum number of bags Chef needs so that he can put each box in a bag. A bag can contain more than one box if the sum of sizes of boxes in the bag does not exceed the size of the bag.
------ Input Format ------
- The first line contains T denoting the number of test cases. Then the test cases follow.
- Each test case contains four integers A, B, C, and D on a single line denoting the sizes of the boxes and bags.
------ Output Format ------
For each test case, output on a single line the minimum number of bags Chef needs.
------ Constraints ------
$1≤ T ≤ 100$
$1 ≤ A ≤ B ≤ C ≤ D ≤ 100$
------ subtasks ------
Subtask 1 (100 points): Original constraints
----- Sample Input 1 ------
3
2 3 5 10
1 2 3 5
3 3 4 4
----- Sample Output 1 ------
1
2
3
----- explanation 1 ------
Test case $1$: The sum of sizes of boxes is $2 + 3 + 5 = 10$ which is equal to the size of a bag. Hence Chef can put all three boxes in a single bag.
Test case $2$: Chef can put boxes of size $1$ and $3$ in one bag and box of size $2$ in another bag.
Test case $3$: Chef puts all the boxes in separate bags as there is no way to put more than one box in a single bag.
|
{"inputs": ["3\n2 3 5 10\n1 2 3 5\n3 3 4 4\n"], "outputs": ["1\n2\n3\n"]}
| 405
| 43
|
coding
|
Solve the programming task below in a Python markdown code block.
Jenny is 9 years old. She is the youngest detective in North America. Jenny is a 3rd grader student, so when a new mission comes up, she gets a code to decipher in a form of a sticker (with numbers) in her math notebook and a comment (a sentence) in her writing notebook. All she needs to do is to figure out one word, from there she already knows what to do.
And here comes your role - you can help Jenny find out what the word is!
In order to find out what the word is, you should use the sticker (array of 3 numbers) to retrive 3 letters from the comment (string) that create the word.
- Each of the numbers in the array refers to the position of a letter in the string, in increasing order.
- Spaces are not places, you need the actual letters. No spaces.
- The returned word should be all lowercase letters.
- if you can't find one of the letters using the index numbers, return "No mission today". Jenny would be very sad, but that's life... :(
Example:
input: [5, 0, 3], "I Love You"
output: "ivy"
(0 = "i", 3 = "v", 5 = "y")
Also feel free to reuse/extend the following starter code:
```python
def missing(nums, str):
```
|
{"functional": "_inputs = [[[0, 3, 5], 'I love you'], [[7, 10, 1], 'see you later'], [[29, 31, 8], 'The quick brown fox jumps over the lazy dog'], [[12, 4, 6], 'Good Morning'], [[1, 16, 21], 'A purple pig and a green donkey flew a kite in the middle of the night'], [[35, 8, 20], 'A song can make or ruin your day if you let it get to you'], [[20, 3, 27], 'I love eating toasted cheese and tuna'], [[50, 4, 6], 'Hi everybody'], [[8, 31, 28], 'If I do not like something I will stay away from it'], [[12, 22, 28], 'Where do random thoughts come from'], [[41, 7, 18], 'The tape got stuck on my lips so I could not talk anymore'], [[33, 8, 12], 'My turtle Jim got out of his cage and ate a banana'], [[18, 25, 45], 'are you going to have a funnel birthday cake for your next birthday'], [[5, 25, 31], 'all moms and dads sat around drinking coffee'], [[24, 36, 8], 'My pen broke and now I have blue ink all over my dress']]\n_outputs = [['ivy'], ['ear'], ['bay'], ['No mission today'], ['pen'], ['mug'], ['vet'], ['No mission today'], ['law'], ['mom'], ['gym'], ['job'], ['fix'], ['mic'], ['key']]\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(missing(*i), o[0])"}
| 304
| 526
|
coding
|
Solve the programming task below in a Python markdown code block.
Manao works on a sports TV. He's spent much time watching the football games of some country. After a while he began to notice different patterns. For example, each team has two sets of uniforms: home uniform and guest uniform. When a team plays a game at home, the players put on the home uniform. When a team plays as a guest on somebody else's stadium, the players put on the guest uniform. The only exception to that rule is: when the home uniform color of the host team matches the guests' uniform, the host team puts on its guest uniform as well. For each team the color of the home and guest uniform is different.
There are n teams taking part in the national championship. The championship consists of n·(n - 1) games: each team invites each other team to its stadium. At this point Manao wondered: how many times during the championship is a host team going to put on the guest uniform? Note that the order of the games does not affect this number.
You know the colors of the home and guest uniform for each team. For simplicity, the colors are numbered by integers in such a way that no two distinct colors have the same number. Help Manao find the answer to his question.
-----Input-----
The first line contains an integer n (2 ≤ n ≤ 30). Each of the following n lines contains a pair of distinct space-separated integers h_{i}, a_{i} (1 ≤ h_{i}, a_{i} ≤ 100) — the colors of the i-th team's home and guest uniforms, respectively.
-----Output-----
In a single line print the number of games where the host team is going to play in the guest uniform.
-----Examples-----
Input
3
1 2
2 4
3 4
Output
1
Input
4
100 42
42 100
5 42
100 5
Output
5
Input
2
1 2
1 2
Output
0
-----Note-----
In the first test case the championship consists of 6 games. The only game with the event in question is the game between teams 2 and 1 on the stadium of team 2.
In the second test sample the host team will have to wear guest uniform in the games between teams: 1 and 2, 2 and 1, 2 and 3, 3 and 4, 4 and 2 (the host team is written first).
|
{"inputs": ["2\n1 2\n1 2\n", "2\n0 2\n1 2\n", "2\n1 2\n1 2\n", "2\n46 6\n6 46\n", "2\n46 6\n6 46\n", "2\n46 6\n12 46\n", "2\n75 6\n12 46\n", "2\n75 6\n12 34\n"], "outputs": ["0\n", "0\n", "0\n", "2\n", "2\n", "1\n", "0\n", "0\n"]}
| 537
| 147
|
coding
|
Solve the programming task below in a Python markdown code block.
International Women's Day is coming soon! Polycarp is preparing for the holiday.
There are $n$ candy boxes in the shop for sale. The $i$-th box contains $d_i$ candies.
Polycarp wants to prepare the maximum number of gifts for $k$ girls. Each gift will consist of exactly two boxes. The girls should be able to share each gift equally, so the total amount of candies in a gift (in a pair of boxes) should be divisible by $k$. In other words, two boxes $i$ and $j$ ($i \ne j$) can be combined as a gift if $d_i + d_j$ is divisible by $k$.
How many boxes will Polycarp be able to give? Of course, each box can be a part of no more than one gift. Polycarp cannot use boxes "partially" or redistribute candies between them.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5, 1 \le k \le 100$) — the number the boxes and the number the girls.
The second line of the input contains $n$ integers $d_1, d_2, \dots, d_n$ ($1 \le d_i \le 10^9$), where $d_i$ is the number of candies in the $i$-th box.
-----Output-----
Print one integer — the maximum number of the boxes Polycarp can give as gifts.
-----Examples-----
Input
7 2
1 2 2 3 2 4 10
Output
6
Input
8 2
1 2 2 3 2 4 6 10
Output
8
Input
7 3
1 2 2 3 2 4 5
Output
4
-----Note-----
In the first example Polycarp can give the following pairs of boxes (pairs are presented by indices of corresponding boxes): $(2, 3)$; $(5, 6)$; $(1, 4)$.
So the answer is $6$.
In the second example Polycarp can give the following pairs of boxes (pairs are presented by indices of corresponding boxes): $(6, 8)$; $(2, 3)$; $(1, 4)$; $(5, 7)$.
So the answer is $8$.
In the third example Polycarp can give the following pairs of boxes (pairs are presented by indices of corresponding boxes): $(1, 2)$; $(6, 7)$.
So the answer is $4$.
|
{"inputs": ["1 2\n21\n", "1 2\n41\n", "0 2\n41\n", "0 2\n10\n", "1 2\n24\n", "1 2\n60\n", "0 2\n24\n", "0 2\n60\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
| 596
| 110
|
coding
|
Solve the programming task below in a Python markdown code block.
We have a grid with A horizontal rows and B vertical columns, with the squares painted white. On this grid, we will repeatedly apply the following operation:
* Assume that the grid currently has a horizontal rows and b vertical columns. Choose "vertical" or "horizontal".
* If we choose "vertical", insert one row at the top of the grid, resulting in an (a+1) \times b grid.
* If we choose "horizontal", insert one column at the right end of the grid, resulting in an a \times (b+1) grid.
* Then, paint one of the added squares black, and the other squares white.
Assume the grid eventually has C horizontal rows and D vertical columns. Find the number of ways in which the squares can be painted in the end, modulo 998244353.
Constraints
* 1 \leq A \leq C \leq 3000
* 1 \leq B \leq D \leq 3000
* A, B, C, and D are integers.
Input
Input is given from Standard Input in the following format:
A B C D
Output
Print the number of ways in which the squares can be painted in the end, modulo 998244353.
Examples
Input
1 1 2 2
Output
3
Input
2 1 3 4
Output
65
Input
31 41 59 265
Output
387222020
|
{"inputs": ["2 0 3 4", "1 0 2 2", "2 0 3 0", "1 0 2 3", "2 0 3 1", "2 0 3 2", "1 0 2 1", "2 0 6 2"], "outputs": ["130\n", "3\n", "0\n", "7\n", "2\n", "10\n", "1\n", "94\n"]}
| 346
| 114
|
coding
|
Solve the programming task below in a Python markdown code block.
Misha and Vasya participated in a Codeforces contest. Unfortunately, each of them solved only one problem, though successfully submitted it at the first attempt. Misha solved the problem that costs a points and Vasya solved the problem that costs b points. Besides, Misha submitted the problem c minutes after the contest started and Vasya submitted the problem d minutes after the contest started. As you know, on Codeforces the cost of a problem reduces as a round continues. That is, if you submit a problem that costs p points t minutes after the contest started, you get $\operatorname{max}(\frac{3p}{10}, p - \frac{p}{250} \times t)$ points.
Misha and Vasya are having an argument trying to find out who got more points. Help them to find out the truth.
-----Input-----
The first line contains four integers a, b, c, d (250 ≤ a, b ≤ 3500, 0 ≤ c, d ≤ 180).
It is guaranteed that numbers a and b are divisible by 250 (just like on any real Codeforces round).
-----Output-----
Output on a single line:
"Misha" (without the quotes), if Misha got more points than Vasya.
"Vasya" (without the quotes), if Vasya got more points than Misha.
"Tie" (without the quotes), if both of them got the same number of points.
-----Examples-----
Input
500 1000 20 30
Output
Vasya
Input
1000 1000 1 1
Output
Tie
Input
1500 1000 176 177
Output
Misha
|
{"inputs": ["250 250 0 0\n", "250 250 0 0\n", "146 250 0 0\n", "146 175 0 0\n", "229 175 0 0\n", "250 3500 0 0\n", "250 3500 0 0\n", "250 4386 0 0\n"], "outputs": ["Tie\n", "Tie", "Vasya\n", "Vasya\n", "Misha\n", "Vasya\n", "Vasya", "Vasya\n"]}
| 401
| 164
|
coding
|
Solve the programming task below in a Python markdown code block.
Given two positive integers a and b, we define f(a, b) = \text{lcm}(a, b) - \gcd(a, b), where \text{lcm} denotes the [lowest common multiple] and \gcd denotes the [greatest common divisor].
Chef has a positive integer N. He wonders, what is the maximum value of f(a, b) over all pairs (a, b) such that a and b are positive integers, and a + b = N?
------ Input Format ------
- The first line of input will contain an integer T — the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N, as described in the problem statement.
------ Output Format ------
For each test case, output the maximum value of f(a, b) that can be obtained while satisfying the conditions on a and b.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$2 ≤ N ≤ 10^{9}$
----- Sample Input 1 ------
3
3
4
6
----- Sample Output 1 ------
1
2
4
----- explanation 1 ------
Test case $1$: There are two possible pairs of $(a, b)$: $(1, 2)$ and $(2, 1)$. For both of these pairs, we have $\text{lcm}(a, b) = 2$ and $\gcd(a, b) = 1$, which gives $f(a, b) = 1$.
Test case $2$: For $(1, 3)$, we have $\text{lcm}(1, 3) = 3$ and $\gcd(1, 3) = 1$, giving $f(1, 3) = 3 - 1 = 2$. It can be shown that this is the maximum possible value of $f(a, b)$ that can be obtained while satisfying the above conditions.
Test case $3$: For $(1, 5)$, we have $\text{lcm}(1, 5) = 5$ and $\gcd(1, 5) = 1$, giving $f(1, 5) = 5 - 1 = 4$. It can be shown that this is the maximum possible value of $f(a, b)$ that can be obtained while satisfying the above conditions.
|
{"inputs": ["3\n3\n4\n6\n"], "outputs": ["1\n2\n4\n"]}
| 517
| 24
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given an array of positive integers arr (not necessarily distinct), return the lexicographically largest permutation that is smaller than arr, that can be made with exactly one swap. If it cannot be done, then return the same array.
Note that a swap exchanges the positions of two numbers arr[i] and arr[j]
Please complete the following python code precisely:
```python
class Solution:
def prevPermOpt1(self, arr: List[int]) -> List[int]:
```
|
{"functional": "def check(candidate):\n assert candidate(arr = [3,2,1]) == [3,1,2]\n assert candidate(arr = [1,1,5]) == [1,1,5]\n assert candidate(arr = [1,9,4,6,7]) == [1,7,4,6,9]\n\n\ncheck(Solution().prevPermOpt1)"}
| 114
| 92
|
coding
|
Solve the programming task below in a Python markdown code block.
Reforms continue entering Berland. For example, during yesterday sitting the Berland Parliament approved as much as n laws (each law has been assigned a unique number from 1 to n). Today all these laws were put on the table of the President of Berland, G.W. Boosch, to be signed.
This time mr. Boosch plans to sign 2k laws. He decided to choose exactly two non-intersecting segments of integers from 1 to n of length k and sign all laws, whose numbers fall into these segments. More formally, mr. Boosch is going to choose two integers a, b (1 ≤ a ≤ b ≤ n - k + 1, b - a ≥ k) and sign all laws with numbers lying in the segments [a; a + k - 1] and [b; b + k - 1] (borders are included).
As mr. Boosch chooses the laws to sign, he of course considers the public opinion. Allberland Public Opinion Study Centre (APOSC) conducted opinion polls among the citizens, processed the results into a report and gave it to the president. The report contains the absurdity value for each law, in the public opinion. As mr. Boosch is a real patriot, he is keen on signing the laws with the maximum total absurdity. Help him.
-----Input-----
The first line contains two integers n and k (2 ≤ n ≤ 2·10^5, 0 < 2k ≤ n) — the number of laws accepted by the parliament and the length of one segment in the law list, correspondingly. The next line contains n integers x_1, x_2, ..., x_{n} — the absurdity of each law (1 ≤ x_{i} ≤ 10^9).
-----Output-----
Print two integers a, b — the beginning of segments that mr. Boosch should choose. That means that the president signs laws with numbers from segments [a; a + k - 1] and [b; b + k - 1]. If there are multiple solutions, print the one with the minimum number a. If there still are multiple solutions, print the one with the minimum b.
-----Examples-----
Input
5 2
3 6 1 1 6
Output
1 4
Input
6 2
1 1 1 1 1 1
Output
1 3
-----Note-----
In the first sample mr. Boosch signs laws with numbers from segments [1;2] and [4;5]. The total absurdity of the signed laws equals 3 + 6 + 1 + 6 = 16.
In the second sample mr. Boosch signs laws with numbers from segments [1;2] and [3;4]. The total absurdity of the signed laws equals 1 + 1 + 1 + 1 = 4.
|
{"inputs": ["2 1\n1 1\n", "2 1\n1 1\n", "2 1\n2 1\n", "2 1\n2 2\n", "4 1\n1 2 2 2\n", "4 1\n1 2 2 2\n", "4 1\n1 1 2 2\n", "4 1\n1 2 2 4\n"], "outputs": ["1 2\n", "1 2\n", "1 2\n", "1 2\n", "2 3\n", "2 3\n", "3 4\n", "2 4\n"]}
| 634
| 150
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well.
Chef went to the store in order to buy one can of coke. In the store, they offer $N$ cans of coke (numbered $1$ through $N$). For each valid $i$, the current temperature of the $i$-th can is $C_{i}$ and its price is $P_{i}$.
After buying a can of coke, Chef wants to immediately start walking home; when he arrives, he wants to immediately drink the whole can. It takes Chef $M$ minutes to get home from the store.
The ambient temperature outside is $K$. When a can of coke is outside, its temperature approaches the ambient temperature. Specifically, if its temperature is $t$ at some point in time:
if $t > K+1$, then one minute later, its temperature will be $t-1$
if $t < K-1$, then one minute later, its temperature will be $t+1$
if $K-1 ≤ t ≤ K+1$, then one minute later, its temperature will be $K$
When Chef drinks coke from a can, he wants its temperature to be between $L$ and $R$ (inclusive). Find the cheapest can for which this condition is satisfied or determine that there is no such can.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains five space-separated integers $N$, $M$, $K$, $L$ and $R$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains two space-separated integers $C_{i}$ and $P_{i}$.
------ Output ------
For each test case, print a single line containing one integer — the price of the can Chef should buy, or $-1$ if it is impossible to buy a can such that Chef's condition is satisfied.
------ Constraints ------
$1 ≤ T ≤ 1,000$
$1 ≤ N ≤ 100$
$1 ≤ M ≤ 100$
$|C_{i}| ≤ 50$ for each valid $i$
$|K| ≤ 50$
$-50 ≤ L ≤ R ≤ 50$
$1 ≤ P_{i} ≤ 10^{6}$ for each valid $i$
----- Sample Input 1 ------
2
3 2 5 4 6
1 6
2 8
8 10
3 5 10 20 30
21 20
22 22
23 23
----- Sample Output 1 ------
8
-1
----- explanation 1 ------
Example case 1: Chef should buy the second can (with price $8$), even though the first can is cheaper. If Chef bought the first can, its temperature would be $3$ when he got home, and that is outside the range $[4, 6]$.
Example case 2: No matter which can Chef buys, when he gets home, its temperature will be less than $20$, so there is no suitable can available in the store.
|
{"inputs": ["2\n3 2 5 4 6\n1 6\n2 8\n8 10\n3 5 10 20 30\n21 20\n22 22\n23 23"], "outputs": ["8\n-1"]}
| 738
| 69
|
coding
|
Solve the programming task below in a Python markdown code block.
Imagine that there is a group of three friends: A, B and С. A owes B 20 rubles and B owes C 20 rubles. The total sum of the debts is 40 rubles. You can see that the debts are not organized in a very optimal manner. Let's rearrange them like that: assume that A owes C 20 rubles and B doesn't owe anything to anybody. The debts still mean the same but the total sum of the debts now equals 20 rubles.
This task is a generalisation of a described example. Imagine that your group of friends has n people and you know the debts between the people. Optimize the given debts without changing their meaning. In other words, finally for each friend the difference between the total money he should give and the total money he should take must be the same. Print the minimum sum of all debts in the optimal rearrangement of the debts. See the notes to the test samples to better understand the problem.
-----Input-----
The first line contains two integers n and m (1 ≤ n ≤ 100; 0 ≤ m ≤ 10^4). The next m lines contain the debts. The i-th line contains three integers a_{i}, b_{i}, c_{i} (1 ≤ a_{i}, b_{i} ≤ n; a_{i} ≠ b_{i}; 1 ≤ c_{i} ≤ 100), which mean that person a_{i} owes person b_{i} c_{i} rubles.
Assume that the people are numbered by integers from 1 to n.
It is guaranteed that the same pair of people occurs at most once in the input. The input doesn't simultaneously contain pair of people (x, y) and pair of people (y, x).
-----Output-----
Print a single integer — the minimum sum of debts in the optimal rearrangement.
-----Examples-----
Input
5 3
1 2 10
2 3 1
2 4 1
Output
10
Input
3 0
Output
0
Input
4 3
1 2 1
2 3 1
3 1 1
Output
0
-----Note-----
In the first sample, you can assume that person number 1 owes 8 rubles to person number 2, 1 ruble to person number 3 and 1 ruble to person number 4. He doesn't owe anybody else anything. In the end, the total debt equals 10.
In the second sample, there are no debts.
In the third sample, you can annul all the debts.
|
{"inputs": ["3 0\n", "1 0\n", "3 0\n", "4 3\n1 2 1\n2 3 1\n3 1 1\n", "4 3\n1 4 1\n2 3 1\n4 2 2\n", "4 3\n1 4 1\n2 3 1\n4 2 2\n", "4 2\n1 2 1\n2 3 1\n3 1 1\n", "4 2\n1 2 1\n2 3 1\n3 1 2\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "2\n", "2\n", "1\n", "1\n"]}
| 573
| 176
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given an array $a$ consisting of $n$ integers. Let's denote monotonic renumeration of array $a$ as an array $b$ consisting of $n$ integers such that all of the following conditions are met:
$b_1 = 0$; for every pair of indices $i$ and $j$ such that $1 \le i, j \le n$, if $a_i = a_j$, then $b_i = b_j$ (note that if $a_i \ne a_j$, it is still possible that $b_i = b_j$); for every index $i \in [1, n - 1]$ either $b_i = b_{i + 1}$ or $b_i + 1 = b_{i + 1}$.
For example, if $a = [1, 2, 1, 2, 3]$, then two possible monotonic renumerations of $a$ are $b = [0, 0, 0, 0, 0]$ and $b = [0, 0, 0, 0, 1]$.
Your task is to calculate the number of different monotonic renumerations of $a$. The answer may be large, so print it modulo $998244353$.
-----Input-----
The first line contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of elements in $a$.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
-----Output-----
Print one integer — the number of different monotonic renumerations of $a$, taken modulo $998244353$.
-----Examples-----
Input
5
1 2 1 2 3
Output
2
Input
2
100 1
Output
2
Input
4
1 3 3 7
Output
4
|
{"inputs": ["2\n100 1\n", "2\n000 1\n", "2\n000 2\n", "2\n010 2\n", "2\n011 2\n", "2\n100 1\n", "4\n1 3 3 7\n", "4\n1 3 1 7\n"], "outputs": ["2\n", "2\n", "2\n", "2\n", "2\n", "2\n", "4\n", "2\n"]}
| 461
| 122
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given an integer n, which indicates that there are n courses labeled from 1 to n. You are also given an array relations where relations[i] = [prevCoursei, nextCoursei], representing a prerequisite relationship between course prevCoursei and course nextCoursei: course prevCoursei has to be taken before course nextCoursei.
In one semester, you can take any number of courses as long as you have taken all the prerequisites in the previous semester for the courses you are taking.
Return the minimum number of semesters needed to take all courses. If there is no way to take all the courses, return -1.
Please complete the following python code precisely:
```python
class Solution:
def minimumSemesters(self, n: int, relations: List[List[int]]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(n = 3, relations = [[1,3],[2,3]]) == 2\n assert candidate(n = 3, relations = [[1,2],[2,3],[3,1]]) == -1\n\n\ncheck(Solution().minimumSemesters)"}
| 184
| 70
|
coding
|
Solve the programming task below in a Python markdown code block.
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.
Tokitsukaze has a binary string $s$ of length $n$, consisting only of zeros and ones, $n$ is even.
Now Tokitsukaze divides $s$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $s$ is considered good if the lengths of all subsegments are even.
For example, if $s$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $2$, $2$, $4$ respectively, which are all even numbers, so "11001111" is good. Another example, if $s$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $3$, $2$, $2$, $3$. Obviously, "1110011000" is not good.
Tokitsukaze wants to make $s$ good by changing the values of some positions in $s$. Specifically, she can perform the operation any number of times: change the value of $s_i$ to '0' or '1'($1 \leq i \leq n$). Can you tell her the minimum number of operations to make $s$ good?
-----Input-----
The first contains a single positive integer $t$ ($1 \leq t \leq 10000$) — the number of test cases.
For each test case, the first line contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) — the length of $s$, it is guaranteed that $n$ is even.
The second line contains a binary string $s$ of length $n$, consisting only of zeros and ones.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print a single line with one integer — the minimum number of operations to make $s$ good.
-----Examples-----
Input
5
10
1110011000
8
11001111
2
00
2
11
6
100110
Output
3
0
0
0
3
-----Note-----
In the first test case, one of the ways to make $s$ good is the following.
Change $s_3$, $s_6$ and $s_7$ to '0', after that $s$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $2$ and $8$ respectively. There are other ways to operate $3$ times to make $s$ good, such as "1111110000", "1100001100", "1111001100".
In the second, third and fourth test cases, $s$ is good initially, so no operation is required.
|
{"inputs": ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110\n"], "outputs": ["3\n0\n0\n0\n3\n"]}
| 756
| 66
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
There is a grid of size 10^{5} \times 10^{5}, covered completely in railway tracks. Tom is riding in a train, currently in cell (a, b), and Jerry is tied up in a different cell (c, d), unable to move. The train has no breaks. It shall move exactly K steps, and then its fuel will run out and it shall stop. In one step, the train must move to one of its neighboring cells, sharing a side. Tom can’t move without the train, as the grid is covered in tracks. Can Tom reach Jerry’s cell after exactly K steps?
Note: Tom can go back to the same cell multiple times.
------ Input Format ------
- The first line contains an integer T, the number of test cases. Then the test cases follow.
- Each test case contains a single line of input, five integers a, b, c, d, K.
------ Output Format ------
For each testcase, output in a single line "YES" if Tom can reach Jerry's cell in exactly K moves 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}$
$0 ≤ a, b, c, d ≤ 10^{5}$
$(a, b) \ne (c, d)$
$1 ≤ K ≤ 2 \cdot 10^{5}$
------ subtasks ------
Subtask #1 (100 points): original constraints
----- Sample Input 1 ------
3
1 1 2 2 2
1 1 2 3 4
1 1 1 0 3
----- Sample Output 1 ------
YES
NO
YES
----- explanation 1 ------
Test Case $1$: A possible sequence of moves is $(1, 1) \to (1, 2) \to (2, 2)$.
Test Case $2$: There is a possible sequence in $3$ moves, but not in exactly $4$ moves.
Test Case $3$: A possible sequence of moves is $(1, 1) \to (1, 0) \to (0, 0) \to (1, 0)$.
|
{"inputs": ["3\n1 1 2 2 2\n1 1 2 3 4\n1 1 1 0 3"], "outputs": ["YES\nNO\nYES"]}
| 548
| 46
|
coding
|
Solve the programming task below in a Python markdown code block.
problem
Given the squares of $ R * C $. Each square is either an empty square or a square with a hole. The given square meets the following conditions.
* The cells with holes are connected. (You can move a square with a hole in the cross direction to any square with a hole)
* Empty cells are connected.
You can generate rectangular tiles of any length with a width of $ 1 $. I would like to install multiple tiles to fill all the holes in the square. When installing tiles, the following restrictions must be observed.
* Tiles can only be installed vertically or horizontally in the $ 2 $ direction.
* Do not install more than one tile on one square.
* There should be no tiles on the squares without holes.
Please answer the minimum number of tiles when all the squares with holes are filled with tiles while observing the above restrictions.
output
Output the minimum number of times. Please also output a line break at the end.
Example
Input
5 5
.....
.#.#.
.###.
.#.#.
.....
Output
3
|
{"inputs": ["3 5\n.....\n.#.#.\n.###.\n.#.#.\n.....", "3 1\n.....\n.#.#.\n.###.\n.#.#.\n.....", "2 5\n.....\n.#.#.\n.###.\n.#.#.\n.....", "4 2\n.....\n.#.#/\n.###.\n.#.#.\n.....", "0 1\n.....\n.#.#.\n.###.\n.#.#.\n.....", "4 1\n.....\n.#.#.\n.###.\n.#.#.\n.....", "0 1\n.....\n.#.#.\n.##\".\n.#.#.\n.....", "2 1\n.....\n.#.#.\n.###.\n.#.#.\n....."], "outputs": ["3\n", "0\n", "2\n", "1\n", "0\n", "0\n", "0\n", "0\n"]}
| 230
| 215
|
coding
|
Solve the programming task below in a Python markdown code block.
At the annual meeting of Board of Directors of Acme Inc. If everyone attending shakes hands exactly one time with every other attendee, how many handshakes are there?
Example
$n=3$
There are $3$ attendees, ${p1}$, $p2$ and $p3$. ${p1}$ shakes hands with $p2$ and $p3$, and $p2$ shakes hands with $p3$. Now they have all shaken hands after $3$ handshakes.
Function Description
Complete the handshakes function in the editor below.
handshakes has the following parameter:
int n: the number of attendees
Returns
int: the number of handshakes
Input Format
The first line contains the number of test cases ${t}}$.
Each of the following ${t}}$ lines contains an integer, $n$.
Constraints
$1\leq t\leq1000$
$0<n<10^6$
Sample Input
2
1
2
Sample Output
0
1
Explanation
Case 1 : The lonely board member shakes no hands, hence 0.
Case 2 : There are 2 board members, so 1 handshake takes place.
|
{"inputs": ["2\n1\n2\n"], "outputs": ["0\n1\n"]}
| 280
| 20
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a permutation $p_1, p_2, \ldots, p_n$ of length $n$. You have to choose two integers $l,r$ ($1 \le l \le r \le n$) and reverse the subsegment $[l,r]$ of the permutation. The permutation will become $p_1,p_2, \dots, p_{l-1},p_r,p_{r-1}, \dots, p_l,p_{r+1},p_{r+2}, \dots ,p_n$.
Find the lexicographically smallest permutation that can be obtained by performing exactly one reverse operation on the initial permutation.
Note that for two distinct permutations of equal length $a$ and $b$, $a$ is lexicographically smaller than $b$ if at the first position they differ, $a$ has the smaller element.
A permutation is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
-----Input-----
Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases. Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 500$) — the length of the permutation.
The second line of each test case contains $n$ integers $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$) — the elements of the permutation.
-----Output-----
For each test case print the lexicographically smallest permutation you can obtain.
-----Examples-----
Input
4
1
1
3
2 1 3
4
1 4 2 3
5
1 2 3 4 5
Output
1
1 2 3
1 2 4 3
1 2 3 4 5
-----Note-----
In the first test case, the permutation has length $1$, so the only possible segment is $[1,1]$. The resulting permutation is $[1]$.
In the second test case, we can obtain the identity permutation by reversing the segment $[1,2]$. The resulting permutation is $[1,2,3]$.
In the third test case, the best possible segment is $[2,3]$. The resulting permutation is $[1,2,4,3]$.
In the fourth test case, there is no lexicographically smaller permutation, so we can leave it unchanged by choosing the segment $[1,1]$. The resulting permutation is $[1,2,3,4,5]$.
|
{"inputs": ["4\n1\n1\n3\n2 1 3\n4\n1 4 2 3\n5\n1 2 3 4 5\n"], "outputs": ["1 \n1 2 3 \n1 2 4 3 \n1 2 3 4 5 \n"]}
| 658
| 76
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where ans[i] = nums[nums[i]] for each 0 <= i < nums.length and return it.
A zero-based permutation nums is an array of distinct integers from 0 to nums.length - 1 (inclusive).
Please complete the following python code precisely:
```python
class Solution:
def buildArray(self, nums: List[int]) -> List[int]:
```
|
{"functional": "def check(candidate):\n assert candidate(nums = [0,2,1,5,3,4]) == [0,1,2,4,5,3]\n assert candidate(nums = [5,0,1,2,3,4]) == [4,5,0,1,2,3]\n\n\ncheck(Solution().buildArray)"}
| 114
| 85
|
coding
|
Solve the programming task below in a Python markdown code block.
A hero is on his way to the castle to complete his mission. However, he's been told that the castle is surrounded with a couple of powerful dragons! each dragon takes 2 bullets to be defeated, our hero has no idea how many bullets he should carry.. Assuming he's gonna grab a specific given number of bullets and move forward to fight another specific given number of dragons, will he survive?
Return True if yes, False otherwise :)
Also feel free to reuse/extend the following starter code:
```python
def hero(bullets, dragons):
```
|
{"functional": "_inputs = [[10, 5], [7, 4], [4, 5], [100, 40], [1500, 751], [0, 1]]\n_outputs = [[True], [False], [False], [True], [False], [False]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(hero(*i), o[0])"}
| 125
| 210
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given an array arr of 4 digits, find the latest 24-hour time that can be made using each digit exactly once.
24-hour times are formatted as "HH:MM", where HH is between 00 and 23, and MM is between 00 and 59. The earliest 24-hour time is 00:00, and the latest is 23:59.
Return the latest 24-hour time in "HH:MM" format. If no valid time can be made, return an empty string.
Please complete the following python code precisely:
```python
class Solution:
def largestTimeFromDigits(self, arr: List[int]) -> str:
```
|
{"functional": "def check(candidate):\n assert candidate(arr = [1,2,3,4]) == \"23:41\"\n assert candidate(arr = [5,5,5,5]) == \"\"\n assert candidate(arr = [0,0,0,0]) == \"00:00\"\n assert candidate(arr = [0,0,1,0]) == \"10:00\"\n\n\ncheck(Solution().largestTimeFromDigits)"}
| 168
| 109
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given an array of integers preorder, which represents the preorder traversal of a BST (i.e., binary search tree), construct the tree and return its root.
It is guaranteed that there is always possible to find a binary search tree with the given requirements for the given test cases.
A binary search tree is a binary tree where for every node, any descendant of Node.left has a value strictly less than Node.val, and any descendant of Node.right has a value strictly greater than Node.val.
A preorder traversal of a binary tree displays the value of the node first, then traverses Node.left, then traverses Node.right.
Please complete the following python code precisely:
```python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def bstFromPreorder(self, preorder: List[int]) -> Optional[TreeNode]:
```
|
{"functional": "def check(candidate):\n assert is_same_tree(candidate(preorder = [8,5,1,7,10,12]), tree_node([8,5,10,1,7,None,12]))\n\n\ncheck(Solution().bstFromPreorder)"}
| 227
| 65
|
coding
|
Solve the programming task below in a Python markdown code block.
Burenka and Tonya are playing an old Buryat game with a chip on a board of $n \times m$ cells.
At the beginning of the game, the chip is located in the lower left corner of the board. In one move, the player can move the chip to the right or up by any odd number of cells (but you cannot move the chip both to the right and up in one move). The one who cannot make a move loses.
Burenka makes the first move, the players take turns. Burenka really wants to win the game, but she is too lazy to come up with a strategy, so you are invited to solve the difficult task of finding it. Name the winner of the game (it is believed that Burenka and Tonya are masters of playing with chips, so they always move in the optimal way).
Chip's starting cell is green, the only cell from which chip can't move is red. if the chip is in the yellow cell, then blue cells are all options to move the chip in one move.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The following is a description of the input data sets.
The only line of each test case contains two integers $n$ and $m$ ($1 \leq n, m \leq 10^9$) — the dimensions of the game board.
-----Output-----
For each test case print a single line — the name of the winner of the game ("Burenka" or "Tonya").
-----Examples-----
Input
6
1 1
1 4
5 6
2 2
6 3
999999999 1000000000
Output
Tonya
Burenka
Burenka
Tonya
Burenka
Burenka
-----Note-----
In the first case, Burenka has no move, so Tonya wins.
In the second case, Burenka can move $3$ cells to the right, after which Tony will not be able to make a move, which means that Burenka wins.
In the third case, Burenka can move $5$ squares to the right. Then we can say that we have a game on a board of $1 \times 5$ cells, and Tonya is the first player. In such game the second player wins, so in the original one Burenka will win.
|
{"inputs": ["6\n1 1\n1 4\n5 6\n2 2\n6 3\n999999999 1000000000\n"], "outputs": ["Tonya\nBurenka\nBurenka\nTonya\nBurenka\nBurenka\n"]}
| 549
| 75
|
coding
|
Solve the programming task below in a Python markdown code block.
Emily and Mia are friends. Emily got Mia’s essay paper, but since she is a prankster, she decided to meddle with the words present in the paper. She changes all the words in the paper into palindromes. To do this, she follows two rules:
- In one operation she can only reduce the value of an alphabet by 1, i.e. she can change ‘d’ to ‘c’, but she cannot change ‘c’ to ‘d’ or ‘d’ to ‘b’.
- The alphabet ‘a’ will not be reduced any further.
Each reduction in the value of any alphabet is counted as a single operation. Find the minimum number of operations required to convert a given string into a palindrome.
-----Input:-----
- The first line contains an integer $T$, denoting the number of test cases.
- Each test case consists of a string $S$ containing only lowercase characters with no spaces.
-----Output:-----
For each test case on a new line, print the minimum number of operations for the corresponding test case.
-----Constraints-----
- $1<=T<=10$
- $1<=|S|<=10^7$, where $|S|$ denotes length of string S.
-----Sample Input:-----
4
abc
abcba
abcd
cba
-----Sample Output:-----
2
0
4
2
-----EXPLANATION:-----
For the first test case string = “abc”
c->b->a so the string become “aba” which is a palindrome. For this we perform 2 operations
|
{"inputs": ["4\nabc\nabcba\nabcd\ncba"], "outputs": ["2\n0\n4\n2"]}
| 340
| 28
|
coding
|
Solve the programming task below in a Python markdown code block.
Hanuman has been a great admirer of Ram. However, there is one thing that Hanuman finds really difficult when talking to Ram. Hanuman can only understand sentences in reverse order but Ram,though an incarnation of God,is somehow unable to speak in reverse.
Help Ram by creating a program that will generate the sentence(string S) given by Ram in reverse order word by word.
INPUT:
An integer T (1 ≤ T ≤ 1000) : number of testcases
Each test case is represented by a sentence (1 ≤ length_of_sentence ≤ 100)
OUTPUT:
For each test case T, output a sentence which is in the reverse order of that of the user given sentence(word by word and not letter by letter)
SAMPLE INPUT
3
I have always been fond of you, Hanuman
SpacelessSentence
A B C D 5
SAMPLE OUTPUT
Hanuman you, of fond been always have I
SpacelessSentence
5 D C B A
|
{"inputs": ["9\nI have always been fond of you, Hanuman\nSpacelessSentence\nHello how are ou?\n.\na . gg\nt t his is a random sentence\nabcdef\nreverse me please\ntestcase 1"], "outputs": ["Hanuman you, of fond been always have I \nSpacelessSentence \nou? are how Hello \n. \ngg . a \nsentence random a is his t t \nabcdef \nplease me reverse \n1 testcase"]}
| 216
| 108
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given an n x n binary matrix grid. You are allowed to change at most one 0 to be 1.
Return the size of the largest island in grid after applying this operation.
An island is a 4-directionally connected group of 1s.
Please complete the following python code precisely:
```python
class Solution:
def largestIsland(self, grid: List[List[int]]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(grid = [[1,0],[0,1]]) == 3\n assert candidate(grid = [[1,1],[1,0]]) == 4\n assert candidate(grid = [[1,1],[1,1]]) == 4\n\n\ncheck(Solution().largestIsland)"}
| 105
| 75
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given a string expression representing an expression of fraction addition and subtraction, return the calculation result in string format.
The final result should be an irreducible fraction. If your final result is an integer, change it to the format of a fraction that has a denominator 1. So in this case, 2 should be converted to 2/1.
Please complete the following python code precisely:
```python
class Solution:
def fractionAddition(self, expression: str) -> str:
```
|
{"functional": "def check(candidate):\n assert candidate(expression = \"-1/2+1/2\") == \"0/1\"\n assert candidate(expression = \"-1/2+1/2+1/3\") == \"1/3\"\n assert candidate(expression = \"1/3-1/2\") == \"-1/6\"\n\n\ncheck(Solution().fractionAddition)"}
| 118
| 90
|
coding
|
Solve the programming task below in a Python markdown code block.
Today is Chef's birthday. His mom decided to surprise him with a truly fantastic gift: his favourite binary string B. But, unfortunately, all the stocks of binary string B have been sold out, and only a binary string A (A ≠ B) is available in the market.
She purchases the string A and tries to convert it to string B by applying any of following three operations zero or more times.
AND Operation:
She will choose a pair of indices i and j such that i != j and perform following sequence of operations.
- result = Ai & Aj
- Ai = result & Ai
- Aj = result & Aj
OR Operation:
She will choose a pair of indices i and j such that i != j and perform following sequence of operations.
- result = Ai | Aj
- Ai = result | Ai
- Aj = result | Aj
XOR Operation:
She will choose a pair of indices i and j such that i != j and perform following sequence of operations.
- result = Ai ^ Aj
- Ai = result ^ Ai
- Aj = result ^ Aj
Chef's mom is eagerly waiting to surprise him with his favourite gift and therefore, she wants to convert string A to string B as fast as possible. Can you please help her by telling her the minimum number of operations she will require? If it is impossible to do so, then let Chef's mom know about it.
-----Input-----
First line of input contains a single integer T denoting the number of test cases. T test cases follow.
First line of each test case, will contain binary string A.
Second line of each test case, will contain binary string B.
-----Output-----
For each test case, Print "Lucky Chef" (without quotes) in first line and minimum number of operations required to convert string A to sting B in second line if conversion is possible. Print "Unlucky Chef" (without quotes) in a new line otherwise.
-----Constraints-----
- 1 ≤ T ≤ 105
- 1 ≤ |A| ≤ 106
- 1 ≤ |B| ≤ 106
- A != B
- |A| = |B|
- sum of |A| over all test cases does not exceed 106
- sum of |B| over all test cases does not exceed 106
-----Subtasks-----
- Subtask #1 (40 points) : Sum of |A| & |B| over all test cases does not exceed 103
- Subtask #2 (60 points) : Sum of |A| & |B| over all test cases does not exceed 106
-----Example-----
Input
2
101
010
1111
1010
Output
Lucky Chef
2
Unlucky Chef
-----Explanation-----
Example case 1.
- Applying XOR operation with indices i = 1 and j = 2. Resulting string will be 011.
- Then, Applying AND operation with indices i = 1 and j = 3. Resulting string will be 010.
Example case 2.
- It is impossible to convert string A to string B.
|
{"inputs": ["2\n101\n010\n1111\n1010", "2\n101\n011\n1111\n1010", "2\n001\n111\n1111\n1010", "2\n111\n110\n1111\n1010", "2\n110\n110\n1111\n1010", "2\n110\n110\n1011\n1010", "2\n010\n110\n0011\n1010", "2\n000\n110\n0011\n1010"], "outputs": ["Lucky Chef\n2\nUnlucky Chef", "Lucky Chef\n1\nUnlucky Chef\n", "Lucky Chef\n2\nUnlucky Chef\n", "Unlucky Chef\nUnlucky Chef\n", "Lucky Chef\n0\nUnlucky Chef\n", "Lucky Chef\n0\nLucky Chef\n1\n", "Lucky Chef\n1\nLucky Chef\n1\n", "Unlucky Chef\nLucky Chef\n1\n"]}
| 693
| 278
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a string S consisting of lowercase English letters.
Another string T is initially empty.
Determine whether it is possible to obtain S = T by performing the following operation an arbitrary number of times:
- Append one of the following at the end of T: dream, dreamer, erase and eraser.
-----Constraints-----
- 1≦|S|≦10^5
- S consists of lowercase English letters.
-----Input-----
The input is given from Standard Input in the following format:
S
-----Output-----
If it is possible to obtain S = T, print YES. Otherwise, print NO.
-----Sample Input-----
erasedream
-----Sample Output-----
YES
Append erase and dream at the end of T in this order, to obtain S = T.
|
{"inputs": ["dreamerer\n", "erasedream\n", "dreameraser\n"], "outputs": ["NO\n", "YES\n", "YES\n"]}
| 171
| 35
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given a 0-indexed array nums and a non-negative integer k.
In one operation, you can do the following:
Choose an index i that hasn't been chosen before from the range [0, nums.length - 1].
Replace nums[i] with any integer from the range [nums[i] - k, nums[i] + k].
The beauty of the array is the length of the longest subsequence consisting of equal elements.
Return the maximum possible beauty of the array nums after applying the operation any number of times.
Note that you can apply the operation to each index only once.
A subsequence of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the order of the remaining elements.
Please complete the following python code precisely:
```python
class Solution:
def maximumBeauty(self, nums: List[int], k: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(nums = [4,6,1,2], k = 2) == 3\n assert candidate(nums = [1,1,1,1], k = 10) == 4\n\n\ncheck(Solution().maximumBeauty)"}
| 206
| 66
|
coding
|
Solve the programming task below in a Python markdown code block.
This year $p$ footballers and $q$ cricketers have been invited to participate in IPL (Indian Programming League) as guests. You have to accommodate them in $r$ rooms such that-
- No room may remain empty.
- A room may contain either only footballers or only cricketers, not both.
- No cricketers are allowed to stay alone in a room.
Find the number of ways to place the players. Note though, that all the rooms are identical. But each of the cricketers and footballers are unique.
Since the number of ways can be very large, print the answer modulo $998,244,353$.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first and only line of each test case contains three space-separated integers $p$, $q$ and $r$ denoting the number of footballers, cricketers and rooms.
-----Output-----
For each test case, output the number of ways to place the players modulo $998,244,353$.
-----Constraints-----
- $1 \le T \le 100$
- $1 \le p, q, r \le 100$
-----Example Input-----
4
2 1 4
2 4 4
2 5 4
2 8 4
-----Example Output-----
0
3
10
609
-----Explanation-----
Example case 2: Three possible ways are:
- {Footballer 1}, {Footballer 2}, {Cricketer 1, Cricketer 2}, {Cricketer 3, Cricketer 4}
- {Footballer 1}, {Footballer 2}, {Cricketer 1, Cricketer 3}, {Cricketer 2, Cricketer 4}
- {Footballer 1}, {Footballer 2}, {Cricketer 1, Cricketer 4}, {Cricketer 2, Cricketer 3}
Please note that the rooms are identical.
|
{"inputs": ["4\n2 1 4\n2 4 4\n2 5 4\n2 8 4"], "outputs": ["0\n3\n10\n609"]}
| 479
| 45
|
coding
|
Solve the programming task below in a Python markdown code block.
$\textit{ABC}$ is a right triangle, $90^{\circ}$ at $\mbox{B}$.
Therefore, $\angle ABC=90°$.
Point $\mbox{M}$ is the midpoint of hypotenuse $\mbox{AC}$.
You are given the lengths $\boldsymbol{AB}$ and $\mbox{BC}$.
Your task is to find $\measuredangle MBC$ (angle $\theta^o$, as shown in the figure) in degrees.
Input Format
The first line contains the length of side $\boldsymbol{AB}$.
The second line contains the length of side $\mbox{BC}$.
Constraints
$\textbf{0}<AB\leq100$
$\textbf{0}<BC\leq100$
Lengths $\boldsymbol{AB}$ and $\mbox{BC}$ are natural numbers.
Output Format
Output $\measuredangle MBC$ in degrees.
Note: Round the angle to the nearest integer.
Examples:
If angle is 56.5000001°, then output 57°.
If angle is 56.5000000°, then output 57°.
If angle is 56.4999999°, then output 56°.
$0^\circ<\theta^\circ<90^\circ$
Sample Input
10
10
Sample Output
45°
|
{"inputs": ["10\n10\n"], "outputs": ["45\u00b0\n"]}
| 334
| 24
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given the root of a binary tree, return the sum of values of nodes with an even-valued grandparent. If there are no nodes with an even-valued grandparent, return 0.
A grandparent of a node is the parent of its parent if it exists.
Please complete the following python code precisely:
```python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def sumEvenGrandparent(self, root: TreeNode) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(root = tree_node([6,7,8,2,7,1,3,9,None,1,4,None,None,None,5])) == 18\n\n\ncheck(Solution().sumEvenGrandparent)"}
| 157
| 60
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.