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.
You are given an array of positive integers $a_1, a_2, \ldots, a_n$.
Make the product of all the numbers in the array (that is, $a_1 \cdot a_2 \cdot \ldots \cdot a_n$) divisible by $2^n$.
You can perform the following operation as many times as you like:
select an arbitrary index $i$ ($1 \leq i \leq n$) and replace the value $a_i$ with $a_i=a_i \cdot i$.
You cannot apply the operation repeatedly to a single index. In other words, all selected values of $i$ must be different.
Find the smallest number of operations you need to perform to make the product of all the elements in the array divisible by $2^n$. Note that such a set of operations does not always exist.
-----Input-----
The first line of the input contains a single integer $t$ $(1 \leq t \leq 10^4$) β the number test cases.
Then the descriptions of the input data sets follow.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains exactly $n$ integers: $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$).
It is guaranteed that the sum of $n$ values over all test cases in a test does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the least number of operations to make the product of all numbers in the array divisible by $2^n$. If the answer does not exist, print -1.
-----Examples-----
Input
6
1
2
2
3 2
3
10 6 11
4
13 17 1 1
5
1 1 12 1 1
6
20 7 14 18 3 5
Output
0
1
1
-1
2
1
-----Note-----
In the first test case, the product of all elements is initially $2$, so no operations needed.
In the second test case, the product of elements initially equals $6$. We can apply the operation for $i = 2$, and then $a_2$ becomes $2\cdot2=4$, and the product of numbers becomes $3\cdot4=12$, and this product of numbers is divided by $2^n=2^2=4$.
In the fourth test case, even if we apply all possible operations, we still cannot make the product of numbers divisible by $2^n$ β it will be $(13\cdot1)\cdot(17\cdot2)\cdot(1\cdot3)\cdot(1\cdot4)=5304$, which does not divide by $2^n=2^4=16$.
In the fifth test case, we can apply operations for $i = 2$ and $i = 4$.
|
{"inputs": ["6\n1\n2\n2\n3 2\n3\n10 6 11\n4\n13 17 1 1\n5\n1 1 12 1 1\n6\n20 7 14 18 3 5\n"], "outputs": ["0\n1\n1\n-1\n2\n1\n"]}
| 699
| 87
|
coding
|
Solve the programming task below in a Python markdown code block.
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations:
d_1 is the distance between the 1-st and the 2-nd station;
d_2 is the distance between the 2-nd and the 3-rd station;
...
d_{n} - 1 is the distance between the n - 1-th and the n-th station;
d_{n} is the distance between the n-th and the 1-st station.
The trains go along the circle line in both directions. Find the shortest distance between stations with numbers s and t.
-----Input-----
The first line contains integer n (3 β€ n β€ 100) β the number of stations on the circle line. The second line contains n integers d_1, d_2, ..., d_{n} (1 β€ d_{i} β€ 100) β the distances between pairs of neighboring stations. The third line contains two integers s and t (1 β€ s, t β€ n) β the numbers of stations, between which you need to find the shortest distance. These numbers can be the same.
The numbers in the lines are separated by single spaces.
-----Output-----
Print a single number β the length of the shortest path between stations number s and t.
-----Examples-----
Input
4
2 3 4 9
1 3
Output
5
Input
4
5 8 2 100
4 1
Output
15
Input
3
1 1 1
3 1
Output
1
Input
3
31 41 59
1 1
Output
0
-----Note-----
In the first sample the length of path 1 β 2 β 3 equals 5, the length of path 1 β 4 β 3 equals 13.
In the second sample the length of path 4 β 1 is 100, the length of path 4 β 3 β 2 β 1 is 15.
In the third sample the length of path 3 β 1 is 1, the length of path 3 β 2 β 1 is 2.
In the fourth sample the numbers of stations are the same, so the shortest distance equals 0.
|
{"inputs": ["3\n1 1 1\n3 1\n", "3\n1 1 1\n2 1\n", "3\n1 1 1\n3 1\n", "4\n2 3 4 9\n1 3\n", "3\n4 37 33\n3 3\n", "4\n1 1 1 1\n2 4\n", "4\n1 1 2 1\n2 4\n", "3\n4 37 33\n3 3\n"], "outputs": ["1\n", "1\n", "1\n", "5\n", "0\n", "2\n", "2\n", "0\n"]}
| 500
| 160
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given a (0-indexed) integer array nums and two integers low and high, return the number of nice pairs.
A nice pair is a pair (i, j) where 0 <= i < j < nums.length and low <= (nums[i] XOR nums[j]) <= high.
Β
Please complete the following python code precisely:
```python
class Solution:
def countPairs(self, nums: List[int], low: int, high: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(nums = [1,4,2,7], low = 2, high = 6) == 6\n assert candidate(nums = [9,8,4,2,1], low = 5, high = 14) == 8\n\n\ncheck(Solution().countPairs)"}
| 115
| 78
|
coding
|
Solve the programming task below in a Python markdown code block.
Recently Luba bought a very interesting book. She knows that it will take t seconds to read the book. Luba wants to finish reading as fast as she can.
But she has some work to do in each of n next days. The number of seconds that Luba has to spend working during i-th day is a_{i}. If some free time remains, she can spend it on reading.
Help Luba to determine the minimum number of day when she finishes reading.
It is guaranteed that the answer doesn't exceed n.
Remember that there are 86400 seconds in a day.
-----Input-----
The first line contains two integers n and t (1 β€ n β€ 100, 1 β€ t β€ 10^6) β the number of days and the time required to read the book.
The second line contains n integers a_{i} (0 β€ a_{i} β€ 86400) β the time Luba has to spend on her work during i-th day.
-----Output-----
Print the minimum day Luba can finish reading the book.
It is guaranteed that answer doesn't exceed n.
-----Examples-----
Input
2 2
86400 86398
Output
2
Input
2 86400
0 86400
Output
1
|
{"inputs": ["1 1\n86399\n", "1 1\n86399\n", "2 2\n86400 86398\n", "2 86400\n0 86400\n", "2 86400\n1 86399\n", "2 86400\n1 86399\n", "2 86400\n0 86399\n", "2 86400\n0 48332\n"], "outputs": ["1\n", "1\n", "2\n", "1\n", "2\n", "2\n", "1\n", "1\n"]}
| 294
| 170
|
coding
|
Solve the programming task below in a Python markdown code block.
The busses in Berland are equipped with a video surveillance system. The system records information about changes in the number of passengers in a bus after stops.
If $x$ is the number of passengers in a bus just before the current bus stop and $y$ is the number of passengers in the bus just after current bus stop, the system records the number $y-x$. So the system records show how number of passengers changed.
The test run was made for single bus and $n$ bus stops. Thus, the system recorded the sequence of integers $a_1, a_2, \dots, a_n$ (exactly one number for each bus stop), where $a_i$ is the record for the bus stop $i$. The bus stops are numbered from $1$ to $n$ in chronological order.
Determine the number of possible ways how many people could be in the bus before the first bus stop, if the bus has a capacity equals to $w$ (that is, at any time in the bus there should be from $0$ to $w$ passengers inclusive).
-----Input-----
The first line contains two integers $n$ and $w$ $(1 \le n \le 1\,000, 1 \le w \le 10^{9})$ β the number of bus stops and the capacity of the bus.
The second line contains a sequence $a_1, a_2, \dots, a_n$ $(-10^{6} \le a_i \le 10^{6})$, where $a_i$ equals to the number, which has been recorded by the video system after the $i$-th bus stop.
-----Output-----
Print the number of possible ways how many people could be in the bus before the first bus stop, if the bus has a capacity equals to $w$. If the situation is contradictory (i.e. for any initial number of passengers there will be a contradiction), print 0.
-----Examples-----
Input
3 5
2 1 -3
Output
3
Input
2 4
-1 1
Output
4
Input
4 10
2 4 1 2
Output
2
-----Note-----
In the first example initially in the bus could be $0$, $1$ or $2$ passengers.
In the second example initially in the bus could be $1$, $2$, $3$ or $4$ passengers.
In the third example initially in the bus could be $0$ or $1$ passenger.
|
{"inputs": ["2 4\n0 1\n", "2 2\n0 1\n", "2 4\n0 0\n", "2 1\n0 0\n", "2 2\n0 0\n", "2 4\n1 0\n", "2 4\n-1 1\n", "2 4\n-1 0\n"], "outputs": ["4\n", "2\n", "5\n", "2\n", "3\n", "4\n", "4\n", "4\n"]}
| 545
| 120
|
coding
|
Solve the programming task below in a Python markdown code block.
The NumPy (Numeric Python) package helps us manipulate large arrays and matrices of numeric data.
To use the NumPy module, we need to import it using:
import numpy
Arrays
A NumPy array is a grid of values. They are similar to lists, except that every element of an array must be the same type.
import numpy
a = numpy.array([1,2,3,4,5])
print a[1] #2
b = numpy.array([1,2,3,4,5],float)
print b[1] #2.0
In the above example, numpy.array() is used to convert a list into a NumPy array. The second argument (float) can be used to set the type of array elements.
Task
You are given a space separated list of numbers.
Your task is to print a reversed NumPy array with the element type float.
Input Format
A single line of input containing space separated numbers.
Output Format
Print the reverse NumPy array with type float.
Sample Input
1 2 3 4 -8 -10
Sample Output
[-10. -8. 4. 3. 2. 1.]
|
{"inputs": ["1 2 3 4 -8 -10\n"], "outputs": ["[-10. -8. 4. 3. 2. 1.]\n"]}
| 272
| 49
|
coding
|
Solve the programming task below in a Python markdown code block.
Problem
Let $ f (x) $ be the sum of each digit when the non-negative integer $ x $ is expressed in binary.
Given a positive integer $ N $, output the largest of $ f (0) $, $ f (1) $, ..., $ f (N) $.
Example of calculating the function $ f (5) $:
When 5 is expressed in binary, it is 101 and the sum of each digit is 1 + 0 + 1 = 2
Therefore, $ f (5) = 2 $.
Note: https://ja.wikipedia.org/wiki/ Binary
Ouput
Outputs the largest of $ f (0) $, $ f (1) $, ..., $ f (N) $ on one line.
Constraints
The input satisfies the following conditions.
* $ 1 \ le N \ le 10 ^ 9 $
Input
The input is given in the following format.
$ N $
A positive integer $ N $ is given on one line.
Examples
Input
2
Output
1
Input
9
Output
3
|
{"inputs": ["6", "1", "5", "4", "3", "8", "7", "2"], "outputs": ["2\n", "1\n", "2\n", "2\n", "2\n", "3\n", "3\n", "1"]}
| 245
| 61
|
coding
|
Solve the programming task below in a Python markdown code block.
Goldbach's conjecture is one of the oldest and best-known unsolved problems in number theory and all of mathematics. It states:
Every even integer greater than 2 can be expressed as the sum of two primes.
For example:
`6 = 3 + 3`
`8 = 3 + 5`
`10 = 3 + 7 = 5 + 5`
`12 = 5 + 7`
Some rules for the conjecture:
- pairs should be descending like [3,5] not [5,3]
- all pairs should be in ascending order based on the first element of the pair:
`[[5, 13], [7, 11]]` is accepted
but `[[7, 11],[5, 13]]` is not accepted.
Write the a function that find all identical pairs of prime numbers:
```python
def goldbach(even_number)
```
You should return an array of containing pairs of primes, like:
```python
[[5, 13], [7, 11]] # even_number = 18
```
or
```python
[[3, 31], [5, 29], [11, 23], [17, 17]] # even_number = 34
```
Also feel free to reuse/extend the following starter code:
```python
def goldbach(n):
```
|
{"functional": "_inputs = [[2], [4], [6], [8], [10], [52], [54], [56], [58], [100], [200], [1000], [5000]]\n_outputs = [[[]], [[[2, 2]]], [[[3, 3]]], [[[3, 5]]], [[[3, 7], [5, 5]]], [[[5, 47], [11, 41], [23, 29]]], [[[7, 47], [11, 43], [13, 41], [17, 37], [23, 31]]], [[[3, 53], [13, 43], [19, 37]]], [[[5, 53], [11, 47], [17, 41], [29, 29]]], [[[3, 97], [11, 89], [17, 83], [29, 71], [41, 59], [47, 53]]], [[[3, 197], [7, 193], [19, 181], [37, 163], [43, 157], [61, 139], [73, 127], [97, 103]]], [[[3, 997], [17, 983], [23, 977], [29, 971], [47, 953], [53, 947], [59, 941], [71, 929], [89, 911], [113, 887], [137, 863], [173, 827], [179, 821], [191, 809], [227, 773], [239, 761], [257, 743], [281, 719], [317, 683], [347, 653], [353, 647], [359, 641], [383, 617], [401, 599], [431, 569], [443, 557], [479, 521], [491, 509]]], [[[7, 4993], [13, 4987], [31, 4969], [43, 4957], [67, 4933], [97, 4903], [139, 4861], [199, 4801], [211, 4789], [241, 4759], [271, 4729], [277, 4723], [337, 4663], [349, 4651], [379, 4621], [397, 4603], [409, 4591], [433, 4567], [439, 4561], [487, 4513], [577, 4423], [643, 4357], [661, 4339], [673, 4327], [727, 4273], [739, 4261], [757, 4243], [769, 4231], [823, 4177], [907, 4093], [997, 4003], [1033, 3967], [1069, 3931], [1093, 3907], [1123, 3877], [1153, 3847], [1231, 3769], [1291, 3709], [1303, 3697], [1327, 3673], [1429, 3571], [1453, 3547], [1459, 3541], [1471, 3529], [1483, 3517], [1489, 3511], [1531, 3469], [1543, 3457], [1567, 3433], [1609, 3391], [1627, 3373], [1657, 3343], [1669, 3331], [1693, 3307], [1699, 3301], [1741, 3259], [1747, 3253], [1783, 3217], [1831, 3169], [1879, 3121], [1933, 3067], [1951, 3049], [1999, 3001], [2029, 2971], [2083, 2917], [2113, 2887], [2143, 2857], [2203, 2797], [2251, 2749], [2269, 2731], [2281, 2719], [2287, 2713], [2293, 2707], [2311, 2689], [2341, 2659], [2383, 2617]]]]\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(goldbach(*i), o[0])"}
| 319
| 1,625
|
coding
|
Solve the programming task below in a Python markdown code block.
There is a square matrix n Γ n, consisting of non-negative integer numbers. You should find such a way on it that
* starts in the upper left cell of the matrix;
* each following cell is to the right or down from the current cell;
* the way ends in the bottom right cell.
Moreover, if we multiply together all the numbers along the way, the result should be the least "round". In other words, it should end in the least possible number of zeros.
Input
The first line contains an integer number n (2 β€ n β€ 1000), n is the size of the matrix. Then follow n lines containing the matrix elements (non-negative integer numbers not exceeding 109).
Output
In the first line print the least number of trailing zeros. In the second line print the correspondent way itself.
Examples
Input
3
1 2 3
4 5 6
7 8 9
Output
0
DDRR
|
{"inputs": ["2\n7 6\n3 8\n", "2\n4 6\n3 8\n", "2\n4 4\n3 8\n", "2\n4 7\n3 8\n", "2\n4 7\n6 8\n", "2\n4 7\n9 8\n", "2\n4 7\n10 8\n", "2\n4 2\n10 8\n"], "outputs": ["0\nDR", "0\nDR\n", "0\nDR\n", "0\nDR\n", "0\nDR\n", "0\nDR\n", "0\nRD\n", "0\nRD\n"]}
| 221
| 151
|
coding
|
Solve the programming task below in a Python markdown code block.
Your job is to build a function which determines whether or not there are double characters in a string (including whitespace characters). For example ```aa```, ``!!`` or ``` ```.
You want the function to return true if the string contains double characters and false if not. The test should not be case sensitive; for example both ```aa``` & ```aA``` return true.
Examples:
```python
double_check("abca")
#returns False
double_check("aabc")
#returns True
double_check("a 11 c d")
#returns True
double_check("AabBcC")
#returns True
double_check("a b c")
#returns True
double_check("a b c d e f g h i h k")
#returns False
double_check("2020")
#returns False
double_check("a!@β¬Β£#$%^&*()_-+=}]{[|\"':;?/>.<,~")
#returns False
```
Also feel free to reuse/extend the following starter code:
```python
def double_check(strng):
```
|
{"functional": "_inputs = [['abca'], ['aabc'], ['a 11 c d'], ['AabBcC'], ['a b c'], ['a b c d e f g h i h k'], ['2020'], [\"a!@\u20ac\u00a3#$%^&*()_-+=}]{[|':;?/>.<,~\"]]\n_outputs = [[False], [True], [True], [True], [True], [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(double_check(*i), o[0])"}
| 260
| 253
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given two integers $a$ and $b$. You may perform any number of operations on them (possibly zero).
During each operation you should choose any positive integer $x$ and set $a := a - x$, $b := b - 2x$ or $a := a - 2x$, $b := b - x$. Note that you may choose different values of $x$ in different operations.
Is it possible to make $a$ and $b$ equal to $0$ simultaneously?
Your program should answer $t$ independent test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases.
Then the test cases follow, each test case is represented by one line containing two integers $a$ and $b$ for this test case ($0 \le a, b \le 10^9$).
-----Output-----
For each test case print the answer to it β YES if it is possible to make $a$ and $b$ equal to $0$ simultaneously, and NO otherwise.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
-----Example-----
Input
3
6 9
1 1
1 2
Output
YES
NO
YES
-----Note-----
In the first test case of the example two operations can be used to make both $a$ and $b$ equal to zero: choose $x = 4$ and set $a := a - x$, $b := b - 2x$. Then $a = 6 - 4 = 2$, $b = 9 - 8 = 1$; choose $x = 1$ and set $a := a - 2x$, $b := b - x$. Then $a = 2 - 2 = 0$, $b = 1 - 1 = 0$.
|
{"inputs": ["1\n29 2\n", "1\n24 2\n", "1\n24 1\n", "1\n21 2\n", "1\n16 0\n", "1\n20 0\n", "1\n31 0\n", "1\n35 0\n"], "outputs": ["NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n"]}
| 438
| 110
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
The product difference between two pairs (a, b) and (c, d) is defined as (a * b) - (c * d).
For example, the product difference between (5, 6) and (2, 7) is (5 * 6) - (2 * 7) = 16.
Given an integer array nums, choose four distinct indices w, x, y, and z such that the product difference between pairs (nums[w], nums[x]) and (nums[y], nums[z]) is maximized.
Return the maximum such product difference.
Β
Please complete the following python code precisely:
```python
class Solution:
def maxProductDifference(self, nums: List[int]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(nums = [5,6,2,7,4]) == 34\n assert candidate(nums = [4,2,5,9,7,4,8]) == 64\n\n\ncheck(Solution().maxProductDifference)"}
| 170
| 66
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order.
Β
Please complete the following python code precisely:
```python
class Solution:
def generateMatrix(self, n: int) -> List[List[int]]:
```
|
{"functional": "def check(candidate):\n assert candidate(n = 3) == [[1,2,3],[8,9,4],[7,6,5]]\n assert candidate(n = 1) == [[1]]\n\n\ncheck(Solution().generateMatrix)"}
| 74
| 63
|
coding
|
Solve the programming task below in a Python markdown code block.
Get excited, folks, because it is time for the final match of Codechef Premier League (CPL)!
Mike and Tracy also want to watch the grand finale, but unfortunately, they could not get tickets to the match. However, Mike is not someone who gives up so easily β he has a plan to watch the match.
The field where the match is played is surrounded by a wall with height $K$. Outside, there are $N$ boxes (numbered $1$ through $N$). For each valid $i$, the $i$-th box has a height $H_i$.
Mike wants to take some boxes and stack them on top of each other to build two towers. The height of each tower is the sum of heights of all the boxes that form it. Of course, no box may be in both towers. The height of each tower should be at least $K$. Then Mike can climb on top of one tower and Tracy on top of the other, and they can watch the match uninterrupted!
While Mike is busy stacking the boxes, Tracy would like to know the smallest number of boxes required to build two towers such that each of them has height at least $K$, or at least that it is impossible to build such towers. Can you help Tracy?
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains $N$ space-separated integers $H_1, H_2, \ldots, H_N$.
-----Output-----
For each test case, print a single line containing one integer β the smallest number of boxes required to build two towers, or $-1$ if it is impossible.
-----Constraints-----
- $1 \leq T \leq 5$
- $1 \leq N, K \leq 4,000$
- $1 \leq H_i \leq 10^5$ for each valid $i$
-----Subtasks-----
Subtask #1 (30 points):
- $1 \leq N, K \leq 100$
- $1 \leq H_i \leq 100$ for each valid $i$
Subtask #2 (70 points): original constraints
-----Example Input-----
2
8 38
7 8 19 7 8 7 10 20
4 5
2 10 4 9
-----Example Output-----
7
2
-----Explanation-----
Example case 1: The first tower can be built with boxes $8 + 10 + 20 = 38$ and the second tower with boxes $7 + 7 + 8 + 19 = 41$. In this case, the box with height $7$ is left unused.
Example case 2: We only need the box with height $10$ for one tower and the box with height $9$ for the other tower.
|
{"inputs": ["2\n8 38\n7 8 19 7 8 7 10 20\n4 5\n2 10 4 9"], "outputs": ["7\n2"]}
| 668
| 51
|
coding
|
Solve the programming task below in a Python markdown code block.
Kulyash believes in equality.
Given an integer N, output a binary string of length N such that:
The count of 01 [subsequences] in the string is equal to the count of 10 subsequences;
The string has at least one occurrence of 0 as well as 1.
If multiple such strings exist, print any. Also, it is guaranteed that corresponding to the given input, an answer always exists.
------ Input Format ------
- First line will contain T, number of test cases. Then the test cases follow.
- Each test case contains of a single line of input, an integer N - the length of the binary string.
------ Output Format ------
For each test case, output any binary string of length N satisfying the given conditions.
------ Constraints ------
$1 β€ T β€ 100$
$3 β€ N β€ 1000$
----- Sample Input 1 ------
2
4
3
----- Sample Output 1 ------
1001
010
----- explanation 1 ------
Test case $1$: A binary string satisfying both the conditions is $1001$. The count of $01$ as well as $10$ subsequences in the string is $2$.
Test case $2$: A binary string satisfying both the conditions is $010$. The count of $01$ as well as $10$ subsequences in the string is $1$.
|
{"inputs": ["2\n4\n3\n"], "outputs": ["1001\n010\n"]}
| 316
| 25
|
coding
|
Solve the programming task below in a Python markdown code block.
This question is similar to the $"Operation$ $on$ $a$ $Tuple"$ problem in this month's Long Challenge but with a slight variation.
Consider the following operations on a triple of integers. In one operation, you should:
- Choose a positive integer $d>0$ and an arithmetic operation - in this case, it will only be addition.
- Choose a subset of elements of the triple.
- Apply arithmetic operation to each of the chosen elements.
For example, if we have a triple $(3,5,7)$, we may choose to add $3$ to the first and third element, and we get $(6,5,10)$ using one operation.
You are given an initial triple $(p,q,r)$ and a target triple $(a,b,c)$. Find the maximum number of operations needed to transform $(p,q,r)$ into $(a,b,c)$ or say the conversion is impossible .
Input:
- The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains three space-separated integers p, q and r.
- The second line contains three space-separated integers a, b and c.Output:
For each test case, print a single line containing one integer β the maximum required number of operations(if the conversion is possible), or else print "-1"
Constraints:
- $1 \leq T \leq 1,000$
- $2 \leq |p|,|q|,|r|,|a|,|b|,|c| \leq 10^9$Sample Input:
1
2 2 1
3 3 2
Sample Output:
3
|
{"inputs": ["1\n2 2 1\n3 3 2"], "outputs": ["3"]}
| 379
| 24
|
coding
|
Solve the programming task below in a Python markdown code block.
Trigrams are a special case of the n-gram, where n is 3. One trigram is a continious sequence of 3 chars in phrase. [Wikipedia](https://en.wikipedia.org/wiki/Trigram)
- return all trigrams for the given phrase
- replace spaces with \_
- return an empty string for phrases shorter than 3
Example:
trigrams('the quick red') == the he\_ e\_q \_qu qui uic ick ck\_ k\_r \_re red
Also feel free to reuse/extend the following starter code:
```python
def trigrams(phrase):
```
|
{"functional": "_inputs = [['the quick red'], ['Hi']]\n_outputs = [['the he_ e_q _qu qui uic ick ck_ k_r _re red'], ['']]\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(trigrams(*i), o[0])"}
| 146
| 179
|
coding
|
Solve the programming task below in a Python markdown code block.
Two countries, Country A and Country B, are at war. As a soldier in Country A, you will lead n soldiers to occupy the territory of Country B.
The territory of Country B is represented by a two-dimensional grid. The first place you occupy is a square on the 2D grid. Each of the soldiers you lead has h_i health. Each soldier can spend 1 health to move. Assuming that the current cell is (a, b), the movement destination is the four directions (a + 1, b), (a-1, b), (a, b + 1), (a, b-1). It is possible to choose. Soldiers will not be able to move from there when their health reaches zero. Any square passed by one or more soldiers can be occupied.
Your job is to find out how many squares you can occupy at most.
However, the size of this two-dimensional grid should be infinitely wide.
Input
The input is given in the following format.
n
h1
..
..
..
hn
Input meets the following constraints
1 β€ n β€ 500
1 β€ hi β€ 10,000,000
Output
Output the answer value on one line
Examples
Input
2
5
5
Output
11
Input
10
10
10
10
10
10
10
10
10
10
10
Output
93
Input
5
1
2
3
4
5
Output
15
|
{"inputs": ["2\n3\n5", "2\n3\n6", "2\n3\n4", "2\n6\n4", "2\n6\n5", "2\n6\n6", "2\n9\n6", "2\n0\n6"], "outputs": ["9\n", "10\n", "8\n", "11\n", "12\n", "13\n", "16\n", "7\n"]}
| 340
| 99
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given two strings a and b of the same length. Choose an index and split both strings at the same index, splitting a into two strings: aprefix and asuffix where a = aprefix + asuffix, and splitting b into two strings: bprefix and bsuffix where b = bprefix + bsuffix. Check if aprefix + bsuffix or bprefix + asuffix forms a palindrome.
When you split a string s into sprefix and ssuffix, either ssuffix or sprefix is allowed to be empty. For example, if s = "abc", then "" + "abc", "a" + "bc", "ab" + "c" , and "abc" + "" are valid splits.
Return true if it is possible to form a palindrome string, otherwise return false.
Notice thatΒ x + y denotes the concatenation of strings x and y.
Β
Please complete the following python code precisely:
```python
class Solution:
def checkPalindromeFormation(self, a: str, b: str) -> bool:
```
|
{"functional": "def check(candidate):\n assert candidate(a = \"x\", b = \"y\") == True\n assert candidate(a = \"abdef\", b = \"fecab\") == True\n assert candidate(a = \"ulacfd\", b = \"jizalu\") == True\n\n\ncheck(Solution().checkPalindromeFormation)"}
| 232
| 74
|
coding
|
Solve the programming task below in a Python markdown code block.
You are standing at the point $0$ on a coordinate line. Your goal is to reach the point $n$. In one minute, you can move by $2$ or by $3$ to the left or to the right (i. e., if your current coordinate is $x$, it can become $x-3$, $x-2$, $x+2$ or $x+3$). Note that the new coordinate can become negative.
Your task is to find the minimum number of minutes required to get from the point $0$ to the point $n$.
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases. Then $t$ lines describing the test cases follow.
The $i$-th of these lines contains one integer $n$ ($1 \le n \le 10^9$) β the goal of the $i$-th test case.
-----Output-----
For each test case, print one integer β the minimum number of minutes required to get from the point $0$ to the point $n$ for the corresponding test case.
-----Examples-----
Input
4
1
3
4
12
Output
2
1
2
4
-----Note-----
None
|
{"inputs": ["1\n56\n", "1\n6942\n", "1\n10241\n", "1\n22812\n", "1\n99999\n", "1\n507348\n", "1\n123451\n", "4\n1\n3\n4\n12\n"], "outputs": ["19\n", "2314\n", "3414\n", "7604\n", "33333\n", "169116\n", "41151\n", "2\n1\n2\n4\n"]}
| 301
| 148
|
coding
|
Solve the programming task below in a Python markdown code block.
Let us begin with an example:
A man has a rather old car being worth $2000.
He saw a secondhand car being worth $8000. He wants to keep his old car until he can buy the secondhand one.
He thinks he can save $1000 each month but the prices of his old
car and of the new one decrease of 1.5 percent per month.
Furthermore this percent of loss increases of `0.5` percent
at the end of every two months.
Our man finds it difficult to make all these calculations.
**Can you help him?**
How many months will it take him to save up enough money to buy the car he wants, and how much money will he have left over?
**Parameters and return of function:**
```
parameter (positive int or float, guaranteed) startPriceOld (Old car price)
parameter (positive int or float, guaranteed) startPriceNew (New car price)
parameter (positive int or float, guaranteed) savingperMonth
parameter (positive float or int, guaranteed) percentLossByMonth
nbMonths(2000, 8000, 1000, 1.5) should return [6, 766] or (6, 766)
```
### Detail of the above example:
```
end month 1: percentLoss 1.5 available -4910.0
end month 2: percentLoss 2.0 available -3791.7999...
end month 3: percentLoss 2.0 available -2675.964
end month 4: percentLoss 2.5 available -1534.06489...
end month 5: percentLoss 2.5 available -395.71327...
end month 6: percentLoss 3.0 available 766.158120825...
return [6, 766] or (6, 766)
```
where `6` is the number of months at **the end of which** he can buy the new car and `766` is the nearest integer to `766.158...` (rounding `766.158` gives `766`).
**Note:**
Selling, buying and saving are normally done at end of month.
Calculations are processed at the end of each considered month
but if, by chance from the start, the value of the old car is bigger than the value of the new one or equal there is no saving to be made, no need to wait so he can at the beginning of the month buy the new car:
```
nbMonths(12000, 8000, 1000, 1.5) should return [0, 4000]
nbMonths(8000, 8000, 1000, 1.5) should return [0, 0]
```
We don't take care of a deposit of savings in a bank:-)
Also feel free to reuse/extend the following starter code:
```python
def nbMonths(startPriceOld, startPriceNew, savingperMonth, percentLossByMonth):
```
|
{"functional": "_inputs = [[2000, 8000, 1000, 1.5], [8000, 12000, 500, 1], [18000, 32000, 1500, 1.25], [7500, 32000, 300, 1.55]]\n_outputs = [[[6, 766]], [[8, 597]], [[8, 332]], [[25, 122]]]\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(nbMonths(*i), o[0])"}
| 725
| 275
|
coding
|
Solve the programming task below in a Python markdown code block.
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (x_{i}, y_{i}). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k β₯ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb.
Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (x_{i}, y_{i}) ( - 10^9 β€ x_{i}, y_{i} β€ 10^9) β the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
-----Output-----
In a single line print a single integer k β the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k β€ 10^6.
-----Examples-----
Input
2
1 1
-1 -1
Output
12
1 1 R
1 1 U
2
1 1 L
1 1 D
3
1 1 L
1 1 D
2
1 1 R
1 1 U
3
Input
3
5 0
0 5
1 0
Output
12
1 1 R
2
1 1 L
3
1 5 R
2
1 5 L
3
1 5 U
2
1 5 D
3
|
{"inputs": ["2\n1 0\n1 0\n", "2\n1 1\n1 0\n", "2\n2 1\n0 -1\n", "2\n2 1\n1 -1\n", "2\n2 2\n1 -1\n", "2\n2 0\n1 -1\n", "2\n3 0\n1 -1\n", "2\n1 0\n1 -1\n"], "outputs": ["8\n1 1 R\n2\n1 1 L\n3\n1 1 R\n2\n1 1 L\n3\n", "10\n1 1 R\n2\n1 1 L\n3\n1 1 R\n1 1 U\n2\n1 1 L\n1 1 D\n3\n", "10\n1 1 D\n2\n1 1 U\n3\n1 2 R\n1 1 U\n2\n1 2 L\n1 1 D\n3\n", "12\n1 1 R\n1 1 D\n2\n1 1 L\n1 1 U\n3\n1 2 R\n1 1 U\n2\n1 2 L\n1 1 D\n3\n", "12\n1 1 R\n1 1 D\n2\n1 1 L\n1 1 U\n3\n1 2 R\n1 2 U\n2\n1 2 L\n1 2 D\n3\n", "10\n1 2 R\n2\n1 2 L\n3\n1 1 R\n1 1 D\n2\n1 1 L\n1 1 U\n3\n", "10\n1 1 R\n1 1 D\n2\n1 1 L\n1 1 U\n3\n1 3 R\n2\n1 3 L\n3\n", "10\n1 1 R\n2\n1 1 L\n3\n1 1 R\n1 1 D\n2\n1 1 L\n1 1 U\n3\n"]}
| 745
| 455
|
coding
|
Solve the programming task below in a Python markdown code block.
It is an interesting exercise to write a program to print out all permutations of $1, 2, β¦, n$. However, since there are $6227020800$ permutations of $1, 2, β¦, 13$, it is unlikely that we would ever run this program on an input of size more than $10$.
However, here is another interesting problem whose solution can also be used to generate permutations. We can order the permutations of $1, 2, β¦, n$ under the lexicographic (or dictionary) order. Here are the permutations of $1,2,3$ in lexicographic order:
123132213231312321123132213231312321 1 \, 2 \, 3 \quad 1 \, 3 \, 2 \quad 2 \, 1 \, 3 \quad 2 \, 3 \, 1 \quad 3 \, 1 \, 2 \quad 3 \, 2 \, 1
The problem we have is the following: given a permutation of $1,2, β¦, n$, generate the next permutation in lexicographic order. For example, for $2 3 1 4$ the answer is $2 3 4 1$.
-----Input:-----
The first line of the input contains two integers, $N$ and $K$. This is followed by $K$ lines, each of which contains one permutation of $1, 2,β¦,N$.
-----Output:-----
The output should consist of $K$ lines. Line $i$ should contain the lexicographically next permutation correponding to the permutation on line $i+1$ in the input.
-----Constraints:-----
- $1 \leq N \leq 1000$.
- $1 \leq K \leq 10$.
-----Sample input-----
3 2
3 1 2
2 3 1
-----Sample output-----
3 2 1
3 1 2
|
{"inputs": ["3 2\n3 1 2\n2 3 1\n"], "outputs": ["3 2 1\n3 1 2 "]}
| 483
| 38
|
coding
|
Solve the programming task below in a Python markdown code block.
A bracket sequence is a string that is one of the following:
- An empty string;
- The concatenation of (, A, and ) in this order, for some bracket sequence A ;
- The concatenation of A and B in this order, for some non-empty bracket sequences A and B /
Given are N strings S_i. Can a bracket sequence be formed by concatenating all the N strings in some order?
-----Constraints-----
- 1 \leq N \leq 10^6
- The total length of the strings S_i is at most 10^6.
- S_i is a non-empty string consisting of ( and ).
-----Input-----
Input is given from Standard Input in the following format:
N
S_1
:
S_N
-----Output-----
If a bracket sequence can be formed by concatenating all the N strings in some order, print Yes; otherwise, print No.
-----Sample Input-----
2
)
(()
-----Sample Output-----
Yes
Concatenating (() and ) in this order forms a bracket sequence.
|
{"inputs": ["2\n)\n()(", "2\n)(\n)(", "2\n((\n)(", "2\n((\n()", "2\n)\n(((", "2\n)(\n))", "2\n)\n())", "2\n(\n((("], "outputs": ["Yes\n", "No\n", "No\n", "No\n", "No\n", "No\n", "No\n", "No\n"]}
| 230
| 98
|
coding
|
Solve the programming task below in a Python markdown code block.
In this kata you must take an input string, reverse the order of the words, and reverse the order of the letters within the words.
But, as a bonus, every test input will end with a punctuation mark (! ? .) and the output should be returned with the mark at the end.
A few examples should help clarify:
```python
esrever("hello world.") == "dlrow olleh."
esrever("Much l33t?") == "t33l hcuM?"
esrever("tacocat!") == "tacocat!"
```
Quick Note: A string will always be passed in (though it may be empty) so no need for error-checking other types.
Also feel free to reuse/extend the following starter code:
```python
def esrever(string):
```
|
{"functional": "_inputs = [['an Easy one?'], ['a small lOan OF 1,000,000 $!'], ['<?> &!.\".'], ['b3tTer p4ss thIS 0ne.'], ['']]\n_outputs = [['eno ysaE na?'], ['$ 000,000,1 FO naOl llams a!'], ['\".!& >?<.'], ['en0 SIht ss4p reTt3b.'], ['']]\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(esrever(*i), o[0])"}
| 185
| 251
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You have n gardens, labeled from 1 to n, and an array paths where paths[i] = [xi, yi] describes a bidirectional path between garden xi to garden yi. In each garden, you want to plant one of 4 types of flowers.
All gardens have at most 3 paths coming into or leaving it.
Your task is to choose a flower type for each garden such that, for any two gardens connected by a path, they have different types of flowers.
Return any such a choice as an array answer, where answer[i] is the type of flower planted in the (i+1)th garden. The flower types are denoted 1, 2, 3, or 4. It is guaranteed an answer exists.
Β
Please complete the following python code precisely:
```python
class Solution:
def gardenNoAdj(self, n: int, paths: List[List[int]]) -> List[int]:
```
|
{"functional": "def check(candidate):\n assert candidate(n = 3, paths = [[1,2],[2,3],[3,1]]) == [1,2,3]\n assert candidate(n = 4, paths = [[1,2],[3,4]]) == [1,2,1,2]\n assert candidate(n = 4, paths = [[1,2],[2,3],[3,4],[4,1],[1,3],[2,4]]) == [1,2,3,4]\n\n\ncheck(Solution().gardenNoAdj)"}
| 209
| 129
|
coding
|
Solve the programming task below in a Python markdown code block.
Did you know you can download more RAM? There is a shop with $n$ different pieces of software that increase your RAM. The $i$-th RAM increasing software takes $a_i$ GB of memory to run (temporarily, once the program is done running, you get the RAM back), and gives you an additional $b_i$ GB of RAM (permanently). Each software can only be used once. Your PC currently has $k$ GB of RAM.
Note that you can't use a RAM-increasing software if it takes more GB of RAM to use than what you currently have.
Since RAM is the most important thing in the world, you wonder, what is the maximum possible amount of RAM achievable?
-----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 100$, $1 \le k \le 1000$). Then two lines follow, each containing $n$ integers describing the arrays $a$ and $b$ ($1 \le a_i, b_i \le 1000$).
-----Output-----
For each test case, output a single line containing the largest amount of RAM you can achieve.
-----Examples-----
Input
4
3 10
20 30 10
9 100 10
5 1
1 1 5 1 1
1 1 1 1 1
5 1
2 2 2 2 2
100 100 100 100 100
5 8
128 64 32 16 8
128 64 32 16 8
Output
29
6
1
256
-----Note-----
In the first test case, you only have enough RAM to run the third software initially, but that increases your RAM to $20$ GB, which allows you to use the first software, increasing your RAM to $29$ GB. The only software left needs $30$ GB of RAM, so you have to stop here.
In the second test case, you can use the first, second, fourth and fifth software that need only $1$ GB of RAM per software to run to increase your RAM to $5$ GB, and then use the last remaining one to increase your RAM to $6$ GB.
In the third test case, all the software need more than $1$ GB of RAM to run, so the amount of RAM you have stays at $1$ GB.
|
{"inputs": ["1\n1 1000\n1000\n1000\n", "4\n3 10\n20 30 10\n9 100 10\n5 1\n1 1 5 1 1\n1 1 1 1 1\n5 1\n2 2 2 2 2\n100 100 100 100 100\n5 8\n128 64 32 16 8\n128 64 32 16 8\n"], "outputs": ["2000\n", "29\n6\n1\n256\n"]}
| 608
| 166
|
coding
|
Solve the programming task below in a Python markdown code block.
A new TV streaming service was recently started in Chefland called the Chef-TV.
A group of N friends in Chefland want to buy Chef-TV subscriptions. We know that 6 people can share one Chef-TV subscription. Also, the cost of one Chef-TV subscription is X rupees.
Determine the minimum total cost that the group of N friends will incur so that everyone in the group is able to use Chef-TV.
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first and only line of each test case contains two integers N and X β the size of the group of friends and the cost of one subscription.
------ Output Format ------
For each test case, output the minimum total cost that the group will incur so that everyone in the group is able to use Chef-TV.
------ Constraints ------
$1 β€ T β€ 1000$
$1 β€ N β€ 100$
$1 β€X β€1000$
----- Sample Input 1 ------
3
1 100
12 250
16 135
----- Sample Output 1 ------
100
500
405
----- explanation 1 ------
- Test case 1: There is only one person in the group. Therefore he will have to buy $1$ subscription. Therefore the total cost incurred is $100$.
- Test case 2: There are $12$ people in the group. Therefore they will have to buy $2$ subscriptions. Therefore the total cost incurred is $500$.
- Test case 3: There are $16$ people in the group. Therefore they will have to buy $3$ subscriptions. Therefore the total cost incurred is $405$.
|
{"inputs": ["3\n1 100\n12 250\n16 135\n"], "outputs": ["100\n500\n405\n"]}
| 398
| 44
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problems statements in Mandarin Chinese and Russian as well.
Given n words w[1..n], which originate from the same stem (e.g. grace, graceful, disgraceful, gracefully), we are interested in the original stem. To simplify the problem, we define the stem as the longest consecutive substring that occurs in all the n words. If there are ties, we will choose the smallest one in the alphabetical (lexicographic) order.
------ Input ------
The first line contains an integer T denoting the total number of test cases.
In each test cases, the first line contains an integer n denoting the number of words. In the second line, n words w[1..n] consisting of lower case characters are given as a single space-spearated list.
------ Output ------
For each test case, output the stem in a new line.
------ Constraints ------
$1 β€ T β€ 10$
$1 β€ n β€ 10$
$1 β€ |w[i]| β€ 20$
----- Sample Input 1 ------
1
4
grace graceful disgraceful gracefully
----- Sample Output 1 ------
grace
----- explanation 1 ------
The stem is grace.
|
{"inputs": ["1\n4\ngrace graceful disgraceful gracefully", "1\n4\necarg graceful disgraceful gracefully", "1\n4\necarg grbceful lufecargsid gracefully", "1\n4\ngeacr graceful disgraceful gracefully", "1\n4\necarg grbceful lufecargsid gradefulmy", "1\n4\nfcgra grbcdful disgraceeul gracefvlmy", "1\n4\ngrace gracefuk disgraceful gracefully", "1\n4\ngracd gracefuk disgraceful gracefully"], "outputs": ["grace", "a\n", "c\n", "ac\n", "e\n", "gr\n", "grace\n", "grac\n"]}
| 262
| 160
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
It's a lockdown. Youβre bored in your house and are playing golf in the hallway.
The hallway has $N + 2$ tiles numbered from $0$ to $N+1$ from left to right. There is a hole on tile number $x$. You hit the ball standing on tile $0$. When you hit the ball, it bounces at lengths of $k$, i.e. the tiles covered by it are $0, k, 2k, \ldots$, and so on until the ball passes tile $N+1$.
If the ball doesn't enter the hole in the first trial, you try again but this time standing on the tile $N+1$. When you hit the ball, it bounces at lengths of $k$, i.e. the tiles covered by it are $(N + 1), (N + 1 - k), (N + 1 - 2k), \ldots$, and so on until the ball passes tile $0$.
Find if the ball will enter the hole, either in its forward journey or backward journey.
Note: The input and output of this problem are large, so prefer using fast input/output methods.
------ Input ------
The first line contains an integer $T$, the number of test cases. Then the test cases follow.
The only line of each test case contains three integers $N, x, k$.
------ Output ------
Output in a single line, the answer, which should be "YES" if the ball enters the hole either in the forward or backward journey and "NO" if not.
You may print each character of the string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical).
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ x, k β€ N β€ 10^{9}$
------ Subtasks ------
Subtask #1 (10 points): $N β€ 10^{2}$
Subtask #2 (90 points): original constraints
----- Sample Input 1 ------
3
5 4 2
5 3 2
5 5 2
----- Sample Output 1 ------
YES
NO
NO
----- explanation 1 ------
In each test case, the tiles covered by the ball for $N = 5$ and $k = 2$ are $\{0, 2, 4, 6\}$ in the forward journey and $\{6, 4, 2, 0\}$ in the backward journey.
Therefore, the answer for the first test case is "YES" since the ball falls in the position of the hole at tile $4$. But the answer for test cases $2$ and $3$ is "NO" since the ball does not fall in the position of the hole.
|
{"inputs": ["3\n5 4 2\n5 3 2\n5 5 2"], "outputs": ["YES\nNO\nNO"]}
| 646
| 34
|
coding
|
Solve the programming task below in a Python markdown code block.
There are $n$ pairs of hard disk drives (HDDs) in a cluster. Each HDD is located at an integer coordinate on an infinite straight line, and each pair consists of one primary HDD and one backup HDD.
Next, you want to place $\boldsymbol{\mbox{k}}$ computers at integer coordinates on the same infinite straight line. Each pair of HDDs must then be connected to a single computer via wires, but a computer can have any number (even zero) of HDDs connected to it. The length of a wire connecting a single HDD to a computer is the absolute value of the distance between their respective coordinates on the infinite line. We consider the total length of wire used to connect all the HDDs to computers to be the sum of the lengths of all the wires used to connect HDDs to computers. Note that both the primary and secondary HDDs in a pair must connect to the same computer.
Given the locations of $n$ pairs (i.e., primary and backup) of HDDs and the value of $\boldsymbol{\mbox{k}}$, place all $\boldsymbol{\mbox{k}}$ computers in such a way that the total length of wire needed to connect each pair of HDDs to computers is minimal. Then print the total length on a new line.
Input Format
The first line contains two space-separated integers denoting the respective values of $n$ (the number of pairs of HDDs) and $\boldsymbol{\mbox{k}}$ (the number of computers).
Each line $\boldsymbol{i}$ of the $n$ subsequent lines contains two space-separated integers describing the respective values of $a_i$ (coordinate of the primary HDD) and $b_i$ (coordinate of the backup HDD) for a pair of HDDs.
Constraints
$2\leq k\leq n\leq10^5$
$4\leq k\times n\leq10^5$
$-10^9\leq a_i,b_i\leq10^9$
Output Format
Print a single integer denoting the minimum total length of wire needed to connect all the pairs of HDDs to computers.
Sample Input
5 2
6 7
-1 1
0 1
5 2
7 3
Sample Output
13
Explanation
For the given Sample Case, it's optimal to place computers at positions $\mbox{0}$ and $\boldsymbol{6}$ on our infinite line. We then connect the second ($a=-1,b=1$) and the third ($a=0,b=1$) pairs of HDDs to the first computer (at position $\mbox{0}$) and then connect the remaining pairs to the second computer (at position $\boldsymbol{6}$).
We calculate the wire lengths needed to connect the drives to each computer. The amount of wire needed to connect the second and third drives to the first computer is $(1+1)+(0+1)=3$, and the amount of wire needed to connect the rest of the drives to the second computer is $(0+1)+(1+4)+(1+3)=10$. When we sum the lengths of wire needed to connect all pairs of drives to the two computers, we get a total length of $3+10=13$. Thus, we print $13$ as our answer.
|
{"inputs": ["5 2\n6 7\n-1 1\n0 1\n5 2\n7 3\n"], "outputs": ["13\n"]}
| 721
| 38
|
coding
|
Solve the programming task below in a Python markdown code block.
The ugliness of a sequence of size N is defined as the number of pairs of neighboring elements which are not equal to each other. Formally, the ugliness of a sequence increase by 1 if for a value of i, 2 β€ i β€ N, the following condition holds true:
A_{i} \neq A_{i-1}.
Examples:
[1, 2, 2, 3], ugliness is 2.
[2, 1, 2, 3,4], ugliness is 4.
You are given a sequence A of length N consisting of the first K natural numbers only. Each of the first K natural numbers occur at least once in the given sequence. For each value of K, you are supposed to remove all of its occurrences from the original sequence and determine the ugliness of the resulting sequence.
Note: An empty sequence has ugliness of 0.
------ Input Format ------
- First line will contain T, the number of test cases. Then the testcases follow.
- Each testcase consist of two lines of input
- First line contains two integers N, K
- Second line contains N space-separated integers, where the i^{th} integer denotes the i^{th} element of the sequence A.
------ Output Format ------
- For each testcase print K space-separated integers denoting the answer for that testcase.
------ Constraints ------
$1 β€ T β€ 1000$
$1 β€ K β€ N β€ 10^{5}$
- For each $i$, $1 β€ A_{i} β€ K$
- Every number from $1$ to $K$ appears atleast once
- Sum of N over all cases doesn't exceed $10^{5}$
----- Sample Input 1 ------
2
8 2
1 2 1 2 2 2 1 2
8 3
1 2 2 1 1 1 2 3
----- Sample Output 1 ------
0 0
1 1 3
----- explanation 1 ------
Test case 1:
- By removing 1, we get the sequence: $[2, 2, 2, 2, 2]$. Since all the elements are the same, ugliness is $0$
- By removing 2, we get the sequence: $[1, 1, 1]$. Since all the elements are the same, ugliness is $0$
Test case 2:
- By removing 1, we get the sequence: $[2, 2, 2, 3]$. Since the 3rd and 4th elements are not equal, ugliness is $1$.
- By removing 2, we get the sequence: $[1, 1, 1, 1, 3]$. Since the 3rd and 4th elements are not equal, ugliness is $1$.
- By removing 3, we get the sequence: $[1, 2, 2, 1, 1, 1, 2]$. Since 1st and 2nd, 3rd and 4th, 6th and 7th are not equal, total ugliness is $3$.
|
{"inputs": ["2\n8 2\n1 2 1 2 2 2 1 2\n8 3\n1 2 2 1 1 1 2 3"], "outputs": ["0 0\n1 1 3\n"]}
| 700
| 61
|
coding
|
Solve the programming task below in a Python markdown code block.
DZY loves collecting special strings which only contain lowercase letters. For each lowercase letter c DZY knows its value w_{c}. For each special string s = s_1s_2... s_{|}s| (|s| is the length of the string) he represents its value with a function f(s), where $f(s) = \sum_{i = 1}^{|s|}(w_{s_{i}} \cdot i)$
Now DZY has a string s. He wants to insert k lowercase letters into this string in order to get the largest possible value of the resulting string. Can you help him calculate the largest possible value he could get?
-----Input-----
The first line contains a single string sΒ (1 β€ |s| β€ 10^3).
The second line contains a single integer kΒ (0 β€ k β€ 10^3).
The third line contains twenty-six integers from w_{a} to w_{z}. Each such number is non-negative and doesn't exceed 1000.
-----Output-----
Print a single integer β the largest possible value of the resulting string DZY could get.
-----Examples-----
Input
abc
3
1 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Output
41
-----Note-----
In the test sample DZY can obtain "abcbbc", value = 1Β·1 + 2Β·2 + 3Β·2 + 4Β·2 + 5Β·2 + 6Β·2 = 41.
|
{"inputs": ["a\n0\n5 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n", "a\n0\n5 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n", "a\n0\n5 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n", "a\n1\n5 1 1 1 1 1 1 0 2 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1\n", "a\n0\n5 1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1\n", "a\n0\n5 1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1\n", "a\n0\n5 2 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1\n", "a\n0\n5 1 1 1 1 1 1 0 2 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1\n"], "outputs": ["5\n", "5\n", "5\n", "15\n", "5\n", "5\n", "5\n", "5\n"]}
| 370
| 503
|
coding
|
Solve the programming task below in a Python markdown code block.
There are three houses on a number line: House 1, 2 and 3, with coordinates A, B and C, respectively. Print `Yes` if we pass the coordinate of House 3 on the straight way from House 1 to House 2 without making a detour, and print `No` otherwise.
Constraints
* 0\leq A,B,C\leq 100
* A, B and C are distinct integers.
Input
Input is given from Standard Input in the following format:
A B C
Output
Print `Yes` if we pass the coordinate of House 3 on the straight way from House 1 to House 2 without making a detour, and print `No` otherwise.
Examples
Input
3 8 5
Output
Yes
Input
7 3 1
Output
No
Input
10 2 4
Output
Yes
Input
31 41 59
Output
No
|
{"inputs": ["8 2 4", "3 3 1", "3 8 7", "1 2 4", "4 3 1", "6 8 7", "1 0 4", "4 2 1"], "outputs": ["Yes\n", "No\n", "Yes\n", "No\n", "No\n", "Yes\n", "No\n", "No\n"]}
| 219
| 94
|
coding
|
Solve the programming task below in a Python markdown code block.
We just discovered a new data structure in our research group: a suffix three!
It's very useful for natural language processing. Given three languages and three suffixes, a suffix three can determine which language a sentence is written in.
It's super simple, 100% accurate, and doesn't involve advanced machine learning algorithms.
Let us tell you how it works.
If a sentence ends with "po" the language is Filipino. If a sentence ends with "desu" or "masu" the language is Japanese. If a sentence ends with "mnida" the language is Korean.
Given this, we need you to implement a suffix three that can differentiate Filipino, Japanese, and Korean.
Oh, did I say three suffixes? I meant four.
-----Input-----
The first line of input contains a single integer $t$ ($1 \leq t \leq 30$) denoting the number of test cases. The next lines contain descriptions of the test cases.
Each test case consists of a single line containing a single string denoting the sentence. Spaces are represented as underscores (the symbol "_") for ease of reading. The sentence has at least $1$ and at most $1000$ characters, and consists only of lowercase English letters and underscores. The sentence has no leading or trailing underscores and no two consecutive underscores. It is guaranteed that the sentence ends with one of the four suffixes mentioned above.
-----Output-----
For each test case, print a single line containing either "FILIPINO", "JAPANESE", or "KOREAN" (all in uppercase, without quotes), depending on the detected language.
-----Example-----
Input
8
kamusta_po
genki_desu
ohayou_gozaimasu
annyeong_hashimnida
hajime_no_ippo
bensamu_no_sentou_houhou_ga_okama_kenpo
ang_halaman_doon_ay_sarisari_singkamasu
si_roy_mustang_ay_namamasu
Output
FILIPINO
JAPANESE
JAPANESE
KOREAN
FILIPINO
FILIPINO
JAPANESE
JAPANESE
-----Note-----
The first sentence ends with "po", so it is written in Filipino.
The second and third sentences end with "desu" and "masu", so they are written in Japanese.
The fourth sentence ends with "mnida", so it is written in Korean.
|
{"inputs": ["8\nkamusta_po\ngenki_desu\nohayou_gozaimasu\nannyeong_hashimnida\nhajime_no_ippo\nbensamu_no_sentou_houhou_ga_okama_kenpo\nang_halaman_doon_ay_sarisari_singkamasu\nsi_roy_mustang_ay_namamasu\n", "8\nkamusta_po\ngenki_desu\nohayou_gozaimasu\nannyeong_hashimnida\nhajime_no_ippo\nbensamu_no_sentou_houhou_ga_okama_kenpo\nang_halaman_doon_ay_sarisari_singkamasu\nsi_roy_muttang_ay_namamasu\n", "8\nkamusta_po\ngenki_desu\nohayou_gozaimasu\nannyeong_hashimnida\ngajime_no_ippo\nbensamu_no_sentou_houhou_ga_okama_kenpo\nang_halaman_doon_ay_sarisari_singkamasu\nsi_roy_mustang_ay_namamasu\n", "8\nkamusta_po\ngenki_desu\nohayou_gozaimasu\nannyeong_hashimnida\nhajime_no_ippo\nbensamu_no_sentou_houhou_ga_okamb_kenpo\nang_halaman_doon_ay_sarisari_singkamasu\nsi_roy_mustang_ay_namamasu\n", "8\nkamusta_po\ngenki_desu\nohayou_gozaimasu\nannyeong_hashimnida\nhajime_no_ippo\nbensamu_no_sentou_houhou_ga_okama_kenpo\nang_halaman_doon_ay_sarisari_singkamasu\nsi_roy_mnttaug_ay_namamasu\n", "8\nkamusta_po\ngenki_desu\nohayou_gozaimasu\nannyeong_hashimnida\nhajime_no_ippo\nbensamu_no_sentou_houhou_ga_okama_kenpo\nang_halaman_coon_ay_sarisari_singkamasu\nsi_roy_mnttaug_ay_namamasu\n", "8\nkamusta_po\ngenki_desu\nohayou_gozaimasu\nannyeong_hasgimnida\nhajime_no_ippo\nbensamu_no_sentou_houhou_ga_okama_kenpo\nang_halaman_doon_ay_sarisari_singkamasu\nsi_roy_muttang_ay_namamasu\n", "8\nkamusta_po\ngenki_desu\nohayou_gozaimasu\nannyeong_hashimnida\nhajimf_no_ippo\nbensamu_no_sentou_houhou_ga_okama_kenpo\nang_halaman_coon_ay_sarisari_singkamasu\nsi_roy_mnttaug_ay_namamasu\n"], "outputs": ["FILIPINO\nJAPANESE\nJAPANESE\nKOREAN\nFILIPINO\nFILIPINO\nJAPANESE\nJAPANESE\n", "FILIPINO\nJAPANESE\nJAPANESE\nKOREAN\nFILIPINO\nFILIPINO\nJAPANESE\nJAPANESE\n", "FILIPINO\nJAPANESE\nJAPANESE\nKOREAN\nFILIPINO\nFILIPINO\nJAPANESE\nJAPANESE\n", "FILIPINO\nJAPANESE\nJAPANESE\nKOREAN\nFILIPINO\nFILIPINO\nJAPANESE\nJAPANESE\n", "FILIPINO\nJAPANESE\nJAPANESE\nKOREAN\nFILIPINO\nFILIPINO\nJAPANESE\nJAPANESE\n", "FILIPINO\nJAPANESE\nJAPANESE\nKOREAN\nFILIPINO\nFILIPINO\nJAPANESE\nJAPANESE\n", "FILIPINO\nJAPANESE\nJAPANESE\nKOREAN\nFILIPINO\nFILIPINO\nJAPANESE\nJAPANESE\n", "FILIPINO\nJAPANESE\nJAPANESE\nKOREAN\nFILIPINO\nFILIPINO\nJAPANESE\nJAPANESE\n"]}
| 541
| 995
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given a Tic-Tac-Toe board as a string array board, return true if and only if it is possible to reach this board position during the course of a valid tic-tac-toe game.
The board is a 3 x 3 array that consists of characters ' ', 'X', and 'O'. The ' ' character represents an empty square.
Here are the rules of Tic-Tac-Toe:
Players take turns placing characters into empty squares ' '.
The first player always places 'X' characters, while the second player always places 'O' characters.
'X' and 'O' characters are always placed into empty squares, never filled ones.
The game ends when there are three of the same (non-empty) character filling any row, column, or diagonal.
The game also ends if all squares are non-empty.
No more moves can be played if the game is over.
Β
Please complete the following python code precisely:
```python
class Solution:
def validTicTacToe(self, board: List[str]) -> bool:
```
|
{"functional": "def check(candidate):\n assert candidate(board = [\"O \",\" \",\" \"]) == False\n assert candidate(board = [\"XOX\",\" X \",\" \"]) == False\n assert candidate(board = [\"XOX\",\"O O\",\"XOX\"]) == True\n\n\ncheck(Solution().validTicTacToe)"}
| 233
| 84
|
coding
|
Solve the programming task below in a Python markdown code block.
Problem
Here is a list of strings. Let's take a break and play with shiritori. Shiritori is performed according to the following rules.
1. First of all, select one of your favorite strings from the list and exclude that string from the list.
2. Next, select one character string from the list in which the last character of the previously selected character string is the first character.
3. Exclude the selected string from the list.
After this, steps 2 and 3 will be repeated alternately.
Now, let's assume that this shiritori is continued until there are no more strings to select from the list in 2. At this time, I want to list all the characters that can be the "last character" of the last selected character string.
Constraints
The input satisfies the following conditions.
* $ 1 \ le N \ lt 10 ^ 4 $
* $ 1 \ le | s_i | \ lt 100 $
* $ s_i \ neq s_j (i \ neq j) $
* The string contains only lowercase letters
Input
$ N $
$ s_1 $
$ s_2 $
$ s_3 $
::
$ s_N $
The number of strings $ N $ is given on the first line.
The following $ N $ line is given a list of strings.
Output
Output the characters that can be the "last character" of the last selected character string in ascending order from a to z in lexicographical order.
Examples
Input
5
you
ate
my
hum
toast
Output
e
t
u
Input
7
she
sells
sea
shells
by
the
seashore
Output
a
e
y
|
{"inputs": ["5\nuoy\nate\nmy\nhum\ntoast", "5\nuoy\nate\nmy\nhum\ntoasu", "5\nuoy\natf\nmy\nhum\ntoasu", "5\nuyp\natf\nmy\nhum\ntoasu", "5\npyu\nauf\nmy\nhum\ntoasu", "5\npyu\nauf\nmy\nmuh\ntoasu", "5\npyu\nfua\nmy\nmuh\ntoasu", "5\npyu\nfua\nmy\nmui\ntoasu"], "outputs": ["e\nt\ny\n", "e\ny\n", "f\ny\n", "f\np\ny\n", "f\nu\ny\n", "f\nh\nu\ny\n", "a\nh\nu\ny\n", "a\ni\nu\ny\n"]}
| 377
| 206
|
coding
|
Solve the programming task below in a Python markdown code block.
## Nova polynomial add
This kata is from a series on polynomial handling. ( [#1](http://www.codewars.com/kata/nova-polynomial-1-add-1) [#2](http://www.codewars.com/kata/570eb07e127ad107270005fe) [#3](http://www.codewars.com/kata/5714041e8807940ff3001140 ) [#4](http://www.codewars.com/kata/571a2e2df24bdfd4e20001f5) )
Consider a polynomial in a list where each element in the list element corresponds to a factor. The factor order is the position in the list. The first element is the zero order factor (the constant).
`p = [a0, a1, a2, a3]` signifies the polynomial `a0 + a1x + a2x^2 + a3*x^3`
In this kata add two polynomials:
```python
poly_add ( [1, 2], [1] ) = [2, 2]
```
Also feel free to reuse/extend the following starter code:
```python
def poly_add(p1, p2):
```
|
{"functional": "_inputs = [[[1], [1]], [[1, 2], [1]], [[1, 2, 3, 4], [4, 3, 2, 1]], [[], []], [[1, 2, 3, 4, 5, 6], []], [[], [1, 2, 3, 4, 5, 6]]]\n_outputs = [[[2]], [[2, 2]], [[5, 5, 5, 5]], [[]], [[1, 2, 3, 4, 5, 6]], [[1, 2, 3, 4, 5, 6]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(poly_add(*i), o[0])"}
| 297
| 290
|
coding
|
Solve the programming task below in a Python markdown code block.
There are four points: $A(x_1, y_1)$, $B(x_2, y_2)$, $C(x_3, y_3)$, and $D(x_4, y_4)$. Write a program which determines whether the line $AB$ and the line $CD$ are parallel. If those two lines are parallel, your program should prints "YES" and if not prints "NO".
Input
Input consists of several datasets. In the first line, you are given the number of datasets $n$ ($n \leq 100$). There will be $n$ lines where each line correspondgs to each dataset. Each dataset consists of eight real numbers:
$x_1$ $y_1$ $x_2$ $y_2$ $x_3$ $y_3$ $x_4$ $y_4$
You can assume that $-100 \leq x_1, y_1, x_2, y_2, x_3, y_3, x_4, y_4 \leq 100$. Each value is a real number with at most 5 digits after the decimal point.
Output
For each dataset, print "YES" or "NO" in a line.
Example
Input
2
0.0 0.0 1.0 1.0 1.0 0.0 2.0 1.0
3.0 2.0 9.0 6.0 13.0 5.0 7.0 9.0
Output
YES
NO
|
{"inputs": ["2\n0.0 0.0 1.0 1.0 1.0 0.0 2.0 1.0\n3.0 2.0 9.0 6.0 13.0 5.0 7.0 9.0", "2\n0.0 0.0 1.0 1.0 1.0 0.0 2.0 1.0\n3.0 2.0 9.680227793087974 6.0 13.0 5.0 7.0 9.0", "2\n0.0 0.0 1.0 1.0 1.0 0.0 2.0 1.0\n3.0 2.0 9.680227793087974 6.0 13.541356008746918 5.0 7.0 9.0", "2\n0.0 0.0 1.0 1.0 1.0 0.0 2.0 1.0\n3.0 2.0 9.680227793087974 6.0 13.541356008746918 5.0 7.0 9.233114893271226", "2\n0.0 0.0 1.0 1.0 1.0 0.0 2.0 1.0\n3.0 2.0 9.680227793087974 6.0 13.541356008746918 5.95543610948743 7.0 9.233114893271226", "2\n0.943552035027174 0.0 1.0 1.0 1.0 0.0 2.0 1.0\n3.0 2.0 9.680227793087974 6.0 13.541356008746918 5.95543610948743 7.0 9.233114893271226", "2\n0.943552035027174 0.0 1.0 1.0 1.0 0.0 2.0 1.0\n3.0 2.0 9.680227793087974 6.222173723926189 13.541356008746918 5.95543610948743 7.0 9.233114893271226", "2\n0.943552035027174 0.0 1.0 1.0 1.0 0.0 2.0 1.0\n3.0 2.0 9.680227793087974 6.222173723926189 13.541356008746918 5.95543610948743 7.0 9.708567095937175"], "outputs": ["YES\nNO", "YES\nNO\n", "YES\nNO\n", "YES\nNO\n", "YES\nNO\n", "NO\nNO\n", "NO\nNO\n", "NO\nNO\n"]}
| 364
| 971
|
coding
|
Solve the programming task below in a Python markdown code block.
Chef's professor is planning to give his class a group assignment. There are 2N students in the class, with distinct roll numbers ranging from 1 to 2N. Chef's roll number is X.
The professor decided to create N groups of 2 students each. The groups were created as follows: the first group consists of roll numbers 1 and 2N, the second group of roll numbers 2 and 2N - 1, and so on, with the final group consisting of roll numbers N and N+1.
Chef wonders who his partner will be. Can you help Chef by telling him the roll number of his partner?
------ 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 and only line of each test case contains two integers N and X, denoting the number of groups that will be formed, and Chef's roll number.
------ Output Format ------
For each test case, output the roll number of the student that will be Chef's partner.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{8}$
$1 β€ X β€ 2N$
----- Sample Input 1 ------
3
2 2
3 1
3 4
----- Sample Output 1 ------
3
6
3
----- explanation 1 ------
Test case $1$: The groups will be $\{(1, 4), (2, 3)\}$. Chef's roll number is $2$, so his partner's roll number will be $3$.
Test case $2$: The groups will be $\{(1, 6), (2, 5), (3, 4)\}$. Chef's roll number is $1$, so his partner's roll number will be $6$.
Test case $3$: The groups will be $\{(1, 6), (2, 5), (3, 4)\}$. Chef's roll number is $4$, so his partner's roll number will be $3$.
|
{"inputs": ["3\n2 2\n3 1\n3 4\n"], "outputs": ["3\n6\n3"]}
| 456
| 29
|
coding
|
Solve the programming task below in a Python markdown code block.
Jzzhu has invented a kind of sequences, they meet the following property:$f_{1} = x ; f_{2} = y ; \forall i(i \geq 2), f_{i} = f_{i - 1} + f_{i + 1}$
You are given x and y, please calculate f_{n} modulo 1000000007 (10^9 + 7).
-----Input-----
The first line contains two integers x and y (|x|, |y| β€ 10^9). The second line contains a single integer n (1 β€ n β€ 2Β·10^9).
-----Output-----
Output a single integer representing f_{n} modulo 1000000007 (10^9 + 7).
-----Examples-----
Input
2 3
3
Output
1
Input
0 -1
2
Output
1000000006
-----Note-----
In the first sample, f_2 = f_1 + f_3, 3 = 2 + f_3, f_3 = 1.
In the second sample, f_2 = - 1; - 1 modulo (10^9 + 7) equals (10^9 + 6).
|
{"inputs": ["2 3\n3\n", "2 3\n6\n", "1 3\n6\n", "3 4\n6\n", "1 2\n6\n", "5 9\n6\n", "1 2\n6\n", "2 3\n6\n"], "outputs": ["1\n", "1000000006\n", "1000000005\n", "1000000006\n", "1000000006\n", "1000000003\n", "1000000006\n", "1000000006\n"]}
| 300
| 165
|
coding
|
Solve the programming task below in a Python markdown code block.
Implement a function that normalizes out of range sequence indexes (converts them to 'in range' indexes) by making them repeatedly 'loop' around the array. The function should then return the value at that index. Indexes that are not out of range should be handled normally and indexes to empty sequences should return undefined/None depending on the language.
For positive numbers that are out of range, they loop around starting at the beginning, so
```python
norm_index_test(seq, len(seq)) # Returns first element
norm_index_test(seq, len(seq) + 1) # Returns second element
norm_index_test(seq, len(seq) + 2) # Returns third element
# And so on...
norm_index_test(seq, len(seq) * 2) # Returns first element
```
For negative numbers, they loop starting from the end, so
```python norm_index_test(seq, len(seq))
norm_index_test(seq, -1) # Returns last element
norm_index_test(seq, -2) # Returns second to last element
norm_index_test(seq, -3) # Returns third to last element
# And so on...
norm_index_test(seq, -len(seq)) # Returns first element
```
Also feel free to reuse/extend the following starter code:
```python
def norm_index_test(seq, ind):
```
|
{"functional": "_inputs = [[[], 10]]\n_outputs = [[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(norm_index_test(*i), o[0])"}
| 292
| 158
|
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. In one second, all occurrences of "01" are simultaneously replaced with "10". This process repeats until no occurrences of "01" exist.
Return the number of seconds needed to complete this process.
Β
Please complete the following python code precisely:
```python
class Solution:
def secondsToRemoveOccurrences(self, s: str) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(s = \"0110101\") == 4\n assert candidate(s = \"11100\") == 0\n\n\ncheck(Solution().secondsToRemoveOccurrences)"}
| 99
| 54
|
coding
|
Solve the programming task below in a Python markdown code block.
Chef has an array of N natural numbers most of them are repeated. Cheffina challenges chef to find all numbers(in ascending order) whose frequency is strictly more than K.
-----Input:-----
- First-line will contain $T$, the number of test cases. Then the test cases follow.
- Each test case contains two lines of input, two integers $N, K$.
- N space-separated natural numbers.
-----Output:-----
For each test case, output in a single line answer.
-----Constraints-----
- $1 \leq T \leq 10$
- $1 \leq N, K \leq 10^5$
- $1 \leq arr[i] \leq 10^5$
-----Sample Input:-----
1
5 1
5 2 1 2 5
-----Sample Output:-----
2 5
|
{"inputs": ["1\n5 1\n5 2 1 2 5"], "outputs": ["2 5"]}
| 196
| 28
|
coding
|
Solve the programming task below in a Python markdown code block.
# Task
Given a string `str`, find the shortest possible string which can be achieved by adding characters to the end of initial string to make it a palindrome.
# Example
For `str = "abcdc"`, the output should be `"abcdcba"`.
# Input/Output
- `[input]` string `str`
A string consisting of lowercase latin letters.
Constraints: `3 β€ str.length β€ 10`.
- `[output]` a string
Also feel free to reuse/extend the following starter code:
```python
def build_palindrome(s):
```
|
{"functional": "_inputs = [['abcdc'], ['ababab']]\n_outputs = [['abcdcba'], ['abababa']]\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(build_palindrome(*i), o[0])"}
| 134
| 167
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given an array of integers arr, find the sum of min(b), where b ranges over every (contiguous) subarray of arr. Since the answer may be large, return the answer modulo 109 + 7.
Β
Please complete the following python code precisely:
```python
class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(arr = [3,1,2,4]) == 17\n assert candidate(arr = [11,81,94,43,3]) == 444\n\n\ncheck(Solution().sumSubarrayMins)"}
| 98
| 67
|
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, return the sum of all possible odd-length subarrays of arr.
A subarray is a contiguous subsequence of the array.
Β
Please complete the following python code precisely:
```python
class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(arr = [1,4,2,5,3]) == 58\n assert candidate(arr = [1,2]) == 3\n assert candidate(arr = [10,11,12]) == 66\n\n\ncheck(Solution().sumOddLengthSubarrays)"}
| 84
| 77
|
coding
|
Solve the programming task below in a Python markdown code block.
Chef recently took a course in linear algebra and learned about linear combinations of vectors. Therefore, in order to test his intelligence, Raj gave him a "fuzzy" problem to solve.
A sequence of integers $B_1, B_2, \ldots, B_M$ generates an integer $K$ if it is possible to find a sequence of integers $C_1, C_2, \ldots, C_M$ such that $C_1 \cdot B_1 + C_2 \cdot B_2 + \ldots + C_M \cdot B_M = K$.
In this problem, Chef has a sequence $A_1, A_2, \ldots, A_N$ and he should answer $Q$ queries. In each query, he is given an integer $K$; the answer to this query is the number of pairs $(l, r)$ such that $1 \le l \le r \le N$ and the subsequence $(A_l, A_{l+1}, \ldots, A_r)$ generates $K$.
Chef has no idea how to solve this problem β can you help him find the answers to all queries?
-----Input-----
- The first line of the input contains a single integer $N$.
- The second line contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$.
- The third line contains a single integer $Q$.
- The following $Q$ lines describe queries. Each of these lines contains a single integer $K$.
-----Output-----
For each query, print a single line containing one integer β the number of contiguous subsequences that generate $K$.
-----Constraints-----
- $1 \le N, Q \le 10^5$
- $1 \le A_i \le 10^9$ for each valid $i$
- $1 \le K \le 10^6$
-----Subtasks-----
Subtask #1 (10 points): $1 \le N \le 1,000$
Subtask #2 (90 points): original constraints
-----Example Input-----
2
2 4
3
1
2
8
-----Example Output-----
0
2
3
-----Explanation-----
The sequence $(2, 4)$ has three contiguous subsequences: $(2)$, $(4)$ and $(2, 4)$.
- In the first query, $1$ cannot be generated by any subsequence.
- In the second query, $2$ is generated by subsequences $(2)$ and $(2, 4)$. For example, for the subsequence $(2, 4)$, we can choose $C_1 = 1$ and $C_2 = 0$.
- In the third query, $8$ is generated by all three subsequences.
|
{"inputs": ["2\n2 4\n3\n1\n2\n8"], "outputs": ["0\n2\n3"]}
| 614
| 28
|
coding
|
Solve the programming task below in a Python markdown code block.
Vova is playing a computer game. There are in total $n$ turns in the game and Vova really wants to play all of them. The initial charge of his laptop battery (i.e. the charge before the start of the game) is $k$.
During each turn Vova can choose what to do: If the current charge of his laptop battery is strictly greater than $a$, Vova can just play, and then the charge of his laptop battery will decrease by $a$; if the current charge of his laptop battery is strictly greater than $b$ ($b<a$), Vova can play and charge his laptop, and then the charge of his laptop battery will decrease by $b$; if the current charge of his laptop battery is less than or equal to $a$ and $b$ at the same time then Vova cannot do anything and loses the game.
Regardless of Vova's turns the charge of the laptop battery is always decreases.
Vova wants to complete the game (Vova can complete the game if after each of $n$ turns the charge of the laptop battery is strictly greater than $0$). Vova has to play exactly $n$ turns. Among all possible ways to complete the game, Vova wants to choose the one where the number of turns when he just plays (first type turn) is the maximum possible. It is possible that Vova cannot complete the game at all.
Your task is to find out the maximum possible number of turns Vova can just play (make the first type turn) or report that Vova cannot complete the game.
You have to answer $q$ independent queries.
-----Input-----
The first line of the input contains one integer $q$ ($1 \le q \le 10^5$) β the number of queries. Each query is presented by a single line.
The only line of the query contains four integers $k, n, a$ and $b$ ($1 \le k, n \le 10^9, 1 \le b < a \le 10^9$) β the initial charge of Vova's laptop battery, the number of turns in the game and values $a$ and $b$, correspondingly.
-----Output-----
For each query print one integer: -1 if Vova cannot complete the game or the maximum number of turns Vova can just play (make the first type turn) otherwise.
-----Example-----
Input
6
15 5 3 2
15 5 4 3
15 5 2 1
15 5 5 1
16 7 5 2
20 5 7 3
Output
4
-1
5
2
0
1
-----Note-----
In the first example query Vova can just play $4$ turns and spend $12$ units of charge and then one turn play and charge and spend $2$ more units. So the remaining charge of the battery will be $1$.
In the second example query Vova cannot complete the game because even if he will play and charge the battery during each turn then the charge of the laptop battery will be $0$ after the last turn.
|
{"inputs": ["1\n1000000000 499999999 3 2\n", "1\n1000000000 999999999 2 1\n", "1\n1000000000 499999999 3 2\n", "1\n1000000000 999999999 2 1\n", "1\n1001000000 499999999 3 2\n", "1\n1000010000 999999999 2 1\n", "1\n1001000000 316833107 3 2\n", "1\n1000000000 499999999 3 1\n"], "outputs": ["1\n", "0\n", "1\n", "0\n", "1000001\n", "10000\n", "316833107\n", "250000000\n"]}
| 689
| 296
|
coding
|
Solve the programming task below in a Python markdown code block.
Bob just learned about bitwise operators. Since Alice is an expert, she decided to play a game, she will give a number $x$ to Bob and will ask some questions:
There will be 4 different kinds of queries:-
-
Alice gives an integer $i$ and Bob has to report the status of the $i^{th}$ bit in $x$, the answer is $"ON"$ if it is on else $"OFF"$.
-
Alice gives an integer $i$ and Bob has to turn on the $i^{th}$ bit in $x$.
-
Alice gives an integer $i$ and Bob has to turn off the $i^{th}$ bit in $x$.
-
Alice gives two integers $p$ and $q$ and in the binary representation of $x$ Bob has to swap the $p^{th}$ and the $q^{th}$ bits.
The value of $x$ changes after any update operation.
positions $i$, $p$, and $q$ are always counted from the right or from the least significant bit.
If anyone of $i$, $p$, or $q$ is greater than the number of bits in the binary representation of $x$, consider $0$ at that position.
-----Input:-----
- First-line will contain $T$, the number of test cases. Then the test cases follow.
- the first line of each test case contains two space-separated integers $x, Q$.
- $2Q$ lines follow.
- first line is an integer, the query type.
- for each query of type 1 to 3, there will be the integer $i$
- for the query of type 4, there will be two space-separated integers, the integers $p, q$
-----Output:-----
For the queries of the first kind, print $"ON"$ or $"OFF"$.
-----Constraints-----
- $1 \leq T \leq 10^3$
- $1 \leq x \leq 10^9$
- $1 \leq Q \leq 10^3$
- $1 \leq i,p,q \leq 30$
-----Sample Input-----
1
2 2
2
1
1
1
-----Sample Output:-----
ON
-----EXPLANATION:-----
the binary representation of 2 is 10
for query 1, we just have to update x to 11 (or 3 in decimal).
for the next query, x is now 3 or 11 in binary so the answer is ON.
|
{"inputs": ["1\n2 2\n2\n1\n1\n1"], "outputs": ["ON"]}
| 551
| 24
|
coding
|
Solve the programming task below in a Python markdown code block.
Kurohashi has never participated in AtCoder Beginner Contest (ABC).
The next ABC to be held is ABC N (the N-th ABC ever held).
Kurohashi wants to make his debut in some ABC x such that all the digits of x in base ten are the same.
What is the earliest ABC where Kurohashi can make his debut?
-----Constraints-----
- 100 \leq N \leq 999
- N is an integer.
-----Input-----
Input is given from Standard Input in the following format:
N
-----Output-----
If the earliest ABC where Kurohashi can make his debut is ABC n, print n.
-----Sample Input-----
111
-----Sample Output-----
111
The next ABC to be held is ABC 111, where Kurohashi can make his debut.
|
{"inputs": ["15", "10", "426", "101", "638", "268", "457", "000"], "outputs": ["111\n", "111\n", "444\n", "111\n", "666\n", "333\n", "555\n", "0\n"]}
| 191
| 90
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a set of strings S. Each string consists of lowercase Latin letters.
For each string in this set, you want to calculate the minimum number of seconds required to type this string. To type a string, you have to start with an empty string and transform it into the string you want to type using the following actions:
* if the current string is t, choose some lowercase Latin letter c and append it to the back of t, so the current string becomes t + c. This action takes 1 second;
* use autocompletion. When you try to autocomplete the current string t, a list of all strings s β S such that t is a prefix of s is shown to you. This list includes t itself, if t is a string from S, and the strings are ordered lexicographically. You can transform t into the i-th string from this list in i seconds. Note that you may choose any string from this list you want, it is not necessarily the string you are trying to type.
What is the minimum number of seconds that you have to spend to type each string from S?
Note that the strings from S are given in an unusual way.
Input
The first line contains one integer n (1 β€ n β€ 10^6).
Then n lines follow, the i-th line contains one integer p_i (0 β€ p_i < i) and one lowercase Latin character c_i. These lines form some set of strings such that S is its subset as follows: there are n + 1 strings, numbered from 0 to n; the 0-th string is an empty string, and the i-th string (i β₯ 1) is the result of appending the character c_i to the string p_i. It is guaranteed that all these strings are distinct.
The next line contains one integer k (1 β€ k β€ n) β the number of strings in S.
The last line contains k integers a_1, a_2, ..., a_k (1 β€ a_i β€ n, all a_i are pairwise distinct) denoting the indices of the strings generated by above-mentioned process that form the set S β formally, if we denote the i-th generated string as s_i, then S = {s_{a_1}, s_{a_2}, ..., s_{a_k}}.
Output
Print k integers, the i-th of them should be equal to the minimum number of seconds required to type the string s_{a_i}.
Examples
Input
10
0 i
1 q
2 g
0 k
1 e
5 r
4 m
5 h
3 p
3 e
5
8 9 1 10 6
Output
2 4 1 3 3
Input
8
0 a
1 b
2 a
2 b
4 a
4 b
5 c
6 d
5
2 3 4 7 8
Output
1 2 2 4 4
Note
In the first example, S consists of the following strings: ieh, iqgp, i, iqge, ier.
|
{"inputs": ["1\n0 z\n1\n1\n", "8\n0 a\n1 b\n2 a\n2 b\n4 a\n4 b\n5 d\n6 d\n5\n2 3 4 7 8\n", "8\n0 a\n1 b\n2 a\n2 b\n4 a\n4 b\n2 c\n6 d\n5\n2 3 4 7 8\n", "8\n0 a\n1 b\n2 a\n2 b\n4 a\n4 b\n5 d\n6 e\n5\n2 3 4 7 8\n", "8\n0 a\n1 b\n2 a\n2 b\n4 a\n4 b\n5 c\n6 d\n5\n2 3 4 7 8\n", "10\n0 i\n1 q\n2 g\n0 k\n1 e\n5 r\n4 m\n5 h\n3 o\n3 e\n5\n8 9 1 10 6\n", "10\n0 i\n1 q\n2 g\n0 k\n1 e\n5 r\n4 m\n4 g\n3 o\n3 e\n5\n8 9 1 10 6\n", "10\n0 i\n1 q\n2 g\n1 k\n1 e\n5 r\n4 m\n4 g\n3 o\n3 e\n5\n8 9 1 10 6\n"], "outputs": ["1 ", "1 2 2 4 4\n", "1 2 2 2 4\n", "1 2 2 4 4\n", "1 2 2 4 4 ", "2 4 1 3 3\n", "2 4 1 3 2\n", "3 4 1 3 2\n"]}
| 666
| 407
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given the sequence of Nucleotides of one strand of DNA through a string S of length N. S contains the character A, T, C, and G only.
Chef knows that:
A is complementary to T.
T is complementary to A.
C is complementary to G.
G is complementary to C.
Using the string S, determine the sequence of the complementary strand of the DNA.
------ Input Format ------
- First line will contain T, number of test cases. Then the test cases follow.
- First line of each test case contains an integer N - denoting the length of string S.
- Second line contains N characters denoting the string S.
------ Output Format ------
For each test case, output the string containing N characters - sequence of nucleotides of the complementary strand.
------ Constraints ------
$1 β€ T β€ 100$
$1 β€ N β€ 100$
$S$ contains A, T, C, and G only
----- Sample Input 1 ------
4
4
ATCG
4
GTCC
5
AAAAA
3
TAC
----- Sample Output 1 ------
TAGC
CAGG
TTTTT
ATG
----- explanation 1 ------
Test case $1$: Based on the rules, the complements of A, T, C, and G are T, A, G, and C respectively. Thus, the complementary string of the given string ATCG is TAGC.
Test case $2$: Based on the rules, the complements of G, T, and C are C, A, and G respectively. Thus, the complementary string of the given string GTCC is CAGG.
Test case $3$: Based on the rules, the complement of A is T. Thus, the complementary string of the given string AAAAA is TTTTT.
Test case $4$: Based on the rules, the complements of T, A, and C are A, T, and G respectively. Thus, the complementary string of the given string TAC is ATG.
|
{"inputs": ["4\n4\nATCG\n4\nGTCC\n5\nAAAAA\n3\nTAC\n"], "outputs": ["TAGC\nCAGG\nTTTTT\nATG\n"]}
| 440
| 46
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
In Olympics, the countries are ranked by the total number of medals won. You are given six integers G_{1}, S_{1}, B_{1}, and G_{2}, S_{2}, B_{2}, the number of gold, silver and bronze medals won by two different countries respectively. Determine which country is ranked better on the leaderboard. It is guaranteed that there will not be a tie between the two countries.
------ 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 G_{1}, S_{1}, B_{1}, and G_{2}, S_{2}, B_{2}.
------ Output Format ------
For each test case, print "1" if the first country is ranked better or "2" otherwise. Output the answer without quotes.
------ Constraints ------
$1 β€ T β€ 1000$
$0 β€ G_{1}, S_{1}, B_{1}, G_{2}, S_{2}, B_{2} β€ 30$
------ subtasks ------
Subtask #1 (100 points): Original constraints
----- Sample Input 1 ------
3
10 20 30 0 29 30
0 0 0 0 0 1
1 1 1 0 0 0
----- Sample Output 1 ------
1
2
1
----- explanation 1 ------
Test case $1$: Total medals for the first country are $10 + 20 + 30 = 60$ and that for the second country are $0 + 29 + 30 = 59$. So the first country is ranked better than the second country.
Test case $2$: Total medals for the first country are $0 + 0 + 0 = 0$ and that for the second country are $0 + 0 + 1 = 1$. So the second country is ranked better than the first country.
|
{"inputs": ["3\n10 20 30 0 29 30\n0 0 0 0 0 1\n1 1 1 0 0 0\n"], "outputs": ["1\n2\n1"]}
| 484
| 58
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given N binary strings S_{1}, S_{2}, \dots, S_{N}, each of length M. You want to concatenate all the N strings in some order to form a single large string of length N \cdot M. Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '0' and '1'.
An *inversion* in a binary string S is a pair of indices (i, j) such that i < j and S_{i} = '1', S_{j} = '0'. For example, the string S = "01010" contains 3 inversions : (2, 3), (2, 5), (4, 5).
Note that you are not allowed to change the order of characters within any of the strings S_{i} - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "00" and "10" you can form "0010" and "1000", but not "0001".
------ Input Format ------
- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N, M.
- N lines follow, the i^{th} of which contains the string S_{i} of length M.
------ Output Format ------
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$2 β€ N β€ 10^{5}$
$1 β€ M β€ 10^{5}$
$2 β€ N \cdot M β€ 2\cdot 10^{5}$
$\lvert S_{i} \rvert = M$
$S_{i}$ contains only characters '$0$' and '$1$'.
- Sum of $N \cdot M$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
----- Sample Output 1 ------
0
2
3
11
----- explanation 1 ------
Test case $1$: Two strings can be formed : $S_{1} + S_{2} = $"$01$", $S_{2} + S_{1} $= "$10$". The first string does not have any inversion while the second has one inversion : $(1, 2)$.
Test case $2$: Both $S_{2} + S_{1} + S_{3} = $"$001001$", $S_{2} + S_{3} + S_{1} = $"$000110$" contain $2$ inversions. It can be verified that no other order results in fewer inversions.
|
{"inputs": ["4\n2 1\n0\n1\n3 2\n10\n00\n01\n2 4\n0010\n1011\n3 4\n1011\n1101\n0101\n"], "outputs": ["0\n2\n3\n11"]}
| 694
| 74
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given an array A of length N.
You have to partition the elements of the array into some [subsequences] such that:
Each element A_{i} (1 β€ i β€ N) belongs to exactly one subsequence.
The [mean] of the mean of subsequences is maximised.
Formally, let S_{1}, S_{2}, \dots, S_{K} denote K subsequences of array A such that each element A_{i} (1 β€ i β€ N) belongs to exactly one subsequence S_{j} (1 β€ j β€ K).
Let X_{j} (1 β€ j β€ K) denote the mean of the elements of subsequence S_{j}. You need to maximise the value \frac{\sum_{j=1}^K X_{j}}{K}.
Print the maximum value. The answer is considered correct if the relative error is less than 10^{-6}.
------ Input Format ------
- First line will contain T, number of test cases. Then the test cases follow.
- First line of each test case consists of a single integer N - denoting the length of array A.
- Second line of each test case consists of N space-separated integers A_{1}, A_{2}, \ldots, A_{N} - denoting the array A.
------ Output Format ------
For each test case, output in a single line, the maximum possible mean of mean of subsequences. The answer is considered correct if the relative error is less than 10^{-6}.
------ Constraints ------
$1 β€ T β€ 1000$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{6}$
- Sum of $N$ over all test cases does not exceed $3 \cdot 10^{5}$.
----- Sample Input 1 ------
3
2
10 20
3
1 2 3
5
50 50 50 50 50
----- Sample Output 1 ------
15
2.25
50
----- explanation 1 ------
Test Case $1$: We can partition the array in $2$ ways - $(\{10\}, \{20\})$ or $(\{10,20\})$. In both cases, mean of mean of subsequences comes out to be $15$.
Test Case $2$: The optimal partition is $(\{1, 2\}, \{3\})$.
- Mean of first subsequence $ = \frac{1+2}{2} = 1.5$.
- Mean of second subsequence $ = \frac{3}{1} = 3$.
Thus, mean of mean of subsequences is $\frac{1.5+3}{2} = \frac{4.5}{2} = 2.25$.
Test Case $3$: Any partition will yield the mean of mean of subsequences as $50$.
|
{"inputs": ["3\n2\n10 20\n3\n1 2 3\n5\n50 50 50 50 50\n"], "outputs": ["15\n2.25\n50\n"]}
| 656
| 56
|
coding
|
Solve the programming task below in a Python markdown code block.
# Task
John was in math class and got bored, so he decided to fold some origami from a rectangular `a Γ b` sheet of paper (`a > b`). His first step is to make a square piece of paper from the initial rectangular piece of paper by folding the sheet along the bisector of the right angle and cutting off the excess part.
After moving the square piece of paper aside, John wanted to make even more squares! He took the remaining (`a-b`) Γ `b` strip of paper and went on with the process until he was left with a square piece of paper.
Your task is to determine how many square pieces of paper John can make.
# Example:
For: `a = 2, b = 1`, the output should be `2`.
Given `a = 2` and `b = 1`, John can fold a `1 Γ 1` then another `1 Γ 1`.
So the answer is `2`.
For: `a = 10, b = 7`, the output should be `6`.
We are given `a = 10` and `b = 7`. The following is the order of squares John folds: `7 Γ 7, 3 Γ 3, 3 Γ 3, 1 Γ 1, 1 Γ 1, and 1 Γ 1`.
Here are pictures for the example cases.
# Input/Output
- `[input]` integer `a`
`2 β€ a β€ 1000`
- `[input]` integer `b`
`1 β€ b < a β€ 1000`
- `[output]` an integer
The maximum number of squares.
Also feel free to reuse/extend the following starter code:
```python
def folding(a,b):
```
|
{"functional": "_inputs = [[2, 1], [10, 7], [3, 1], [4, 1], [3, 2], [4, 2], [1000, 700], [1000, 999]]\n_outputs = [[2], [6], [3], [4], [3], [2], [6], [1000]]\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(folding(*i), o[0])"}
| 397
| 234
|
coding
|
Solve the programming task below in a Python markdown code block.
A wise man told Kerem "Different is good" once, so Kerem wants all things in his life to be different.
Kerem recently got a string s consisting of lowercase English letters. Since Kerem likes it when things are different, he wants all substrings of his string s to be distinct. Substring is a string formed by some number of consecutive characters of the string. For example, string "aba" has substrings "" (empty substring), "a", "b", "a", "ab", "ba", "aba".
If string s has at least two equal substrings then Kerem will change characters at some positions to some other lowercase English letters. Changing characters is a very tiring job, so Kerem want to perform as few changes as possible.
Your task is to find the minimum number of changes needed to make all the substrings of the given string distinct, or determine that it is impossible.
-----Input-----
The first line of the input contains an integer n (1 β€ n β€ 100 000)Β β the length of the string s.
The second line contains the string s of length n consisting of only lowercase English letters.
-----Output-----
If it's impossible to change the string s such that all its substring are distinct print -1. Otherwise print the minimum required number of changes.
-----Examples-----
Input
2
aa
Output
1
Input
4
koko
Output
2
Input
5
murat
Output
0
-----Note-----
In the first sample one of the possible solutions is to change the first character to 'b'.
In the second sample, one may change the first character to 'a' and second character to 'b', so the string becomes "abko".
|
{"inputs": ["1\nq\n", "1\na\n", "1\na\n", "1\nq\n", "1\nb\n", "1\np\n", "1\no\n", "1\nn\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
| 375
| 86
|
coding
|
Solve the programming task below in a Python markdown code block.
You are parking at a parking lot. You can choose from the following two fee plans:
- Plan 1: The fee will be AΓT yen (the currency of Japan) when you park for T hours.
- Plan 2: The fee will be B yen, regardless of the duration.
Find the minimum fee when you park for N hours.
-----Constraints-----
- 1β€Nβ€20
- 1β€Aβ€100
- 1β€Bβ€2000
- All input values are integers.
-----Input-----
Input is given from Standard Input in the following format:
N A B
-----Output-----
When the minimum fee is x yen, print the value of x.
-----Sample Input-----
7 17 120
-----Sample Output-----
119
- If you choose Plan 1, the fee will be 7Γ17=119 yen.
- If you choose Plan 2, the fee will be 120 yen.
Thus, the minimum fee is 119 yen.
|
{"inputs": ["7 17 120\n", "5 20 100\n", "6 18 100\n"], "outputs": ["119\n", "100\n", "100\n"]}
| 234
| 57
|
coding
|
Solve the programming task below in a Python markdown code block.
Ann and Borya have n piles with candies and n is even number. There are a_{i} candies in pile with number i.
Ann likes numbers which are square of some integer and Borya doesn't like numbers which are square of any integer. During one move guys can select some pile with candies and add one candy to it (this candy is new and doesn't belong to any other pile) or remove one candy (if there is at least one candy in this pile).
Find out minimal number of moves that is required to make exactly n / 2 piles contain number of candies that is a square of some integer and exactly n / 2 piles contain number of candies that is not a square of any integer.
-----Input-----
First line contains one even integer n (2 β€ n β€ 200 000)Β β number of piles with candies.
Second line contains sequence of integers a_1, a_2, ..., a_{n} (0 β€ a_{i} β€ 10^9)Β β amounts of candies in each pile.
-----Output-----
Output minimal number of steps required to make exactly n / 2 piles contain number of candies that is a square of some integer and exactly n / 2 piles contain number of candies that is not a square of any integer. If condition is already satisfied output 0.
-----Examples-----
Input
4
12 14 30 4
Output
2
Input
6
0 0 0 0 0 0
Output
6
Input
6
120 110 23 34 25 45
Output
3
Input
10
121 56 78 81 45 100 1 0 54 78
Output
0
-----Note-----
In first example you can satisfy condition in two moves. During each move you should add one candy to second pile. After it size of second pile becomes 16. After that Borya and Ann will have two piles with number of candies which is a square of integer (second and fourth pile) and two piles with number of candies which is not a square of any integer (first and third pile).
In second example you should add two candies to any three piles.
|
{"inputs": ["2\n0 0\n", "2\n1 0\n", "2\n0 1\n", "2\n0 2\n", "2\n2 0\n", "2\n2 0\n", "2\n0 2\n", "2\n0 0\n"], "outputs": ["2\n", "1\n", "1\n", "0\n", "0\n", "0\n", "0\n", "2\n"]}
| 499
| 102
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given an array of non-negative integers nums and an integer k. In one operation, you may choose any element from nums and increment it by 1.
Return the maximum product of nums after at most k operations. Since the answer may be very large, return it modulo 109 + 7. Note that you should maximize the product before taking the modulo.Β
Β
Please complete the following python code precisely:
```python
class Solution:
def maximumProduct(self, nums: List[int], k: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(nums = [0,4], k = 5) == 20\n assert candidate(nums = [6,3,3,2], k = 2) == 216\n\n\ncheck(Solution().maximumProduct)"}
| 129
| 64
|
coding
|
Solve the programming task below in a Python markdown code block.
A conglomerate consists of $n$ companies. To make managing easier, their owners have decided to merge all companies into one. By law, it is only possible to merge two companies, so the owners plan to select two companies, merge them into one, and continue doing so until there is only one company left.
But anti-monopoly service forbids to merge companies if they suspect unfriendly absorption. The criterion they use is the difference in maximum salaries between two companies. Merging is allowed only if the maximum salaries are equal.
To fulfill the anti-monopoly requirements, the owners can change salaries in their companies before merging. But the labor union insists on two conditions: it is only allowed to increase salaries, moreover all the employees in one company must get the same increase.
Sure enough, the owners want to minimize the total increase of all salaries in all companies. Help them find the minimal possible increase that will allow them to merge companies into one.
-----Input-----
The first line contains a single integer $n$Β β the number of companies in the conglomerate ($1 \le n \le 2 \cdot 10^5$). Each of the next $n$ lines describes a company.
A company description start with an integer $m_i$Β β the number of its employees ($1 \le m_i \le 2 \cdot 10^5$). Then $m_i$ integers follow: the salaries of the employees. All salaries are positive and do not exceed $10^9$.
The total number of employees in all companies does not exceed $2 \cdot 10^5$.
-----Output-----
Output a single integer β the minimal total increase of all employees that allows to merge all companies.
-----Example-----
Input
3
2 4 3
2 2 1
3 1 1 1
Output
13
-----Note-----
One of the optimal merging strategies is the following. First increase all salaries in the second company by $2$, and merge the first and the second companies. Now the conglomerate consists of two companies with salaries $[4, 3, 4, 3]$ and $[1, 1, 1]$. To merge them, increase the salaries in the second of those by $3$. The total increase is $2 + 2 + 3 + 3 + 3 = 13$.
|
{"inputs": ["1\n4 2 2 3 2\n", "1\n4 2 2 3 2\n", "1\n4 2 2 3 0\n", "1\n4 3 2 3 0\n", "1\n4 3 2 4 0\n", "1\n4 1 2 4 0\n", "1\n4 2 2 2 2\n", "1\n1 2 2 3 0\n"], "outputs": ["0\n", "0", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
| 508
| 149
|
coding
|
Solve the programming task below in a Python markdown code block.
Luntik came out for a morning stroll and found an array $a$ of length $n$. He calculated the sum $s$ of the elements of the array ($s= \sum_{i=1}^{n} a_i$). Luntik calls a subsequence of the array $a$ nearly full if the sum of the numbers in that subsequence is equal to $s-1$.
Luntik really wants to know the number of nearly full subsequences of the array $a$. But he needs to come home so he asks you to solve that problem!
A sequence $x$ is a subsequence of a sequence $y$ if $x$ can be obtained from $y$ by deletion of several (possibly, zero or all) elements.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases. The next $2 \cdot t$ lines contain descriptions of test cases. The description of each test case consists of two lines.
The first line of each test case contains a single integer $n$ ($1 \le n \le 60$) β the length of the array.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$) β the elements of the array $a$.
-----Output-----
For each test case print the number of nearly full subsequences of the array.
-----Examples-----
Input
5
5
1 2 3 4 5
2
1000 1000
2
1 0
5
3 0 2 1 1
5
2 1 0 3 0
Output
1
0
2
4
4
-----Note-----
In the first test case, $s=1+2+3+4+5=15$, only $(2,3,4,5)$ is a nearly full subsequence among all subsequences, the sum in it is equal to $2+3+4+5=14=15-1$.
In the second test case, there are no nearly full subsequences.
In the third test case, $s=1+0=1$, the nearly full subsequences are $(0)$ and $()$ (the sum of an empty subsequence is $0$).
|
{"inputs": ["5\n5\n1 2 3 4 5\n2\n1000 1000\n2\n1 0\n5\n3 0 2 1 1\n5\n2 1 0 3 0\n", "5\n5\n1 2 3 4 5\n2\n1000 1000\n2\n1 0\n5\n3 0 2 1 0\n5\n2 1 0 3 0\n", "5\n5\n1 4 3 4 5\n2\n1000 1000\n2\n1 0\n5\n3 0 4 1 0\n5\n0 1 0 3 0\n", "5\n5\n1 2 3 4 1\n2\n1000 1000\n2\n1 0\n5\n3 0 2 1 1\n5\n2 1 0 3 0\n", "5\n5\n1 2 3 4 5\n2\n1000 1000\n2\n1 0\n5\n3 0 2 1 0\n5\n2 2 0 3 0\n", "5\n5\n1 4 3 4 2\n2\n1000 1010\n2\n1 0\n5\n3 0 4 2 0\n5\n0 1 0 3 0\n", "5\n5\n1 4 3 4 1\n2\n1110 1010\n2\n1 0\n5\n4 0 4 1 0\n5\n1 1 0 3 0\n", "5\n5\n1 4 3 4 1\n2\n1110 1010\n2\n1 0\n5\n4 0 4 1 0\n5\n1 1 1 3 0\n"], "outputs": ["1\n0\n2\n4\n4\n", "1\n0\n2\n4\n4\n", "1\n0\n2\n4\n8\n", "2\n0\n2\n4\n4\n", "1\n0\n2\n4\n0\n", "1\n0\n2\n0\n8\n", "2\n0\n2\n4\n8\n", "2\n0\n2\n4\n6\n"]}
| 532
| 566
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given an array nums consisting of positive integers.
Starting with score = 0, apply the following algorithm:
Choose the smallest integer of the array that is not marked. If there is a tie, choose the one with the smallest index.
Add the value of the chosen integer to score.
Mark the chosen element and its two adjacent elements if they exist.
Repeat until all the array elements are marked.
Return the score you get after applying the above algorithm.
Β
Please complete the following python code precisely:
```python
class Solution:
def findScore(self, nums: List[int]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(nums = [2,1,3,4,5,2]) == 7\n assert candidate(nums = [2,3,5,1,3,2]) == 5\n\n\ncheck(Solution().findScore)"}
| 140
| 63
|
coding
|
Solve the programming task below in a Python markdown code block.
Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company.
Kefa has n friends, each friend will agree to go to the restaurant if Kefa asks. Each friend is characterized by the amount of money he has and the friendship factor in respect to Kefa. The parrot doesn't want any friend to feel poor compared to somebody else in the company (Kefa doesn't count). A friend feels poor if in the company there is someone who has at least d units of money more than he does. Also, Kefa wants the total friendship factor of the members of the company to be maximum. Help him invite an optimal company!
-----Input-----
The first line of the input contains two space-separated integers, n and d (1 β€ n β€ 10^5, $1 \leq d \leq 10^{9}$) β the number of Kefa's friends and the minimum difference between the amount of money in order to feel poor, respectively.
Next n lines contain the descriptions of Kefa's friends, the (i + 1)-th line contains the description of the i-th friend of type m_{i}, s_{i} (0 β€ m_{i}, s_{i} β€ 10^9) β the amount of money and the friendship factor, respectively.
-----Output-----
Print the maximum total friendship factir that can be reached.
-----Examples-----
Input
4 5
75 5
0 100
150 20
75 1
Output
100
Input
5 100
0 7
11 32
99 10
46 8
87 54
Output
111
-----Note-----
In the first sample test the most profitable strategy is to form a company from only the second friend. At all other variants the total degree of friendship will be worse.
In the second sample test we can take all the friends.
|
{"inputs": ["3 3\n4 5\n0 17\n9 11\n", "1 1000000000\n15 1\n", "1 1000000000\n19 8\n", "1 1000000000\n15 12\n", "3 3\n4 15\n0 17\n9 11\n", "3 3\n4 15\n0 17\n9 11\n", "1 1000000000\n15 12\n", "3 3\n4 15\n0 17\n6 11\n"], "outputs": ["17\n", "1\n", "8\n", "12\n", "17\n", "17", "12", "26\n"]}
| 432
| 207
|
coding
|
Solve the programming task below in a Python markdown code block.
Chef has a garden with $N$ plants arranged in a line in decreasing order of height. Initially the height of the plants are $A_1, A_2, ..., A_N$.
The plants are growing, after each hour the height of the $i$-th plant increases by $i$ millimeters. Find the minimum number of integer hours that Chef must wait to have two plants of the same height.
-----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,..A_N$.
-----Output:-----
For each test case print a single line containing one integer, the minimum number of integer hours that Chef must wait to have two plants of the same height.
-----Constraints-----
- $1 \leq T \leq 1000$
- $2 \leq N \leq 10^5$
- $0\leq A_i \leq 10^{18}$
- $A_i >A_{i+1}$, for each valid $i$
- The Sum of $N$ over all test cases does not exceed $10^6$
-----Sample Input:-----
1
3
8 4 2
-----Sample Output:-----
2
-----EXPLANATION:-----
After $2$ hours there are two plants with the same height.
$[8,4,2] \rightarrow [9,6,5] \rightarrow [10,8,8]$.
|
{"inputs": ["1\n3\n8 4 2"], "outputs": ["2"]}
| 373
| 20
|
coding
|
Solve the programming task below in a Python markdown code block.
A group of $n$ friends living in an $m$-dimensional hyperspace want to meet up at some central location. The hyperspace is in the form of an $m$-dimensional grid, and each person can only move along grid lines. For example, to go from $(0,0)\rightarrow(1,1)$ in a $2$-dimensional space, one possible route is $(0,0)\to(0,1)\to(1,1)$ for a total distance traveled of $2$ units.
Given the coordinates, $(X[0,1,\ldots,m-1])$, for $n$ friends, find a point at which all $n$ friends can meet such that the total sum of the distances traveled by all $n$ friends is minimal. If there are multiple such points, choose the lexicographically smallest one. The point $P_1[0,1,\ldots,m-1]$ is lexicographically smaller than $P_2[0,1,\ldots,m-1]$ if there exists such $j\lt m$ that $\forall i<j P_1[i]=P_2[i]$ and $P_1[j]<P_2[j]$.
Input Format
The first line contains two space-separated integers describing the respective values of $n$ and $m$.
Each line ${i}$ of the $n$ subsequent lines contains $m$ space-separated integers describing the respective coordinates (i.e., $x_0,x_1,\ldots,x_{m-1}$) for friend ${i}$.
Constraints
$1\leq n\leq10^4$
$1\leq m\leq10^2$
$-10^9\leq x_i\leq10^9$
Output Format
Print $m$ space-separated integers describing the coordinates of the meeting point.
Sample Input
3 2
1 1
2 2
3 3
Sample Output
2 2
Explanation
There are $n=3$ friends (we'll call them ${a}$, ${b}$, and ${c}$) located at points $a=(1,1)$, $b=(2,2)$, and $c=(3,3)$. The minimal solution is for friends ${a}$ and ${c}$ to meet at friend ${b}$'s current location; this means ${a}$ travels $2$ units from $(1,1)$ to $(2,2)$, ${c}$ travels $2$ units from $(3,3)$ to $(2,2)$, and ${b}$ stays put at $(2,2)$. The total distance traveled by all friends is $2+0+2=4$, which is minimal. Thus, we print $m=2$ space-separated integers describing the coordinate where the $n=3$ friends meet: 2 2.
|
{"inputs": ["3 2\n1 1\n2 2\n3 3\n"], "outputs": ["2 2\n"]}
| 630
| 30
|
coding
|
Solve the programming task below in a Python markdown code block.
Silver Fox is fighting with N monsters.
The monsters are standing in a row, and we can assume them to be standing on a number line. The i-th monster, standing at the coordinate X_i, has the health of H_i.
Silver Fox can use bombs to attack the monsters.
Using a bomb at the coordinate x decreases the healths of all monsters between the coordinates x-D and x+D (inclusive) by A.
There is no way other than bombs to decrease the monster's health.
Silver Fox wins when all the monsters' healths become 0 or below.
Find the minimum number of bombs needed to win.
-----Constraints-----
- 1 \leq N \leq 2 \times 10^5
- 0 \leq D \leq 10^9
- 1 \leq A \leq 10^9
- 0 \leq X_i \leq 10^9
- 1 \leq H_i \leq 10^9
- X_i are distinct.
- All values in input are integers.
-----Input-----
Input is given from Standard Input in the following format:
N D A
X_1 H_1
:
X_N H_N
-----Output-----
Print the minimum number of bombs needed to win.
-----Sample Input-----
3 3 2
1 2
5 4
9 2
-----Sample Output-----
2
First, let us use a bomb at the coordinate 4 to decrease the first and second monsters' health by 2.
Then, use a bomb at the coordinate 6 to decrease the second and third monsters' health by 2.
Now, all the monsters' healths are 0.
We cannot make all the monsters' health drop to 0 or below with just one bomb.
|
{"inputs": ["3 3 2\n1 4\n5 4\n9 2", "2 3 1\n1 1\n5 6\n9 2", "2 0 1\n1 1\n5 6\n9 2", "2 0 2\n1 1\n5 6\n9 2", "1 0 2\n1 1\n5 6\n9 2", "0 2 1\n1 1\n5 6\n4 3", "3 3 2\n1 2\n1 4\n9 0", "3 3 2\n1 4\n5 5\n9 2"], "outputs": ["3\n", "6\n", "7\n", "4\n", "1\n", "0\n", "2\n", "3\n"]}
| 394
| 190
|
coding
|
Solve the programming task below in a Python markdown code block.
Jack really likes his number five: the trick here is that you have to multiply each number by 5 raised to the number of digits of each numbers, so, for example:
```python
multiply(3)==15
multiply(10)==250
multiply(200)==25000
multiply(0)==0
multiply(-3)==-15
```
Also feel free to reuse/extend the following starter code:
```python
def multiply(n):
```
|
{"functional": "_inputs = [[10], [5], [200], [0], [-2]]\n_outputs = [[250], [25], [25000], [0], [-10]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(multiply(*i), o[0])"}
| 116
| 189
|
coding
|
Solve the programming task below in a Python markdown code block.
Problem
Den, the phone number of Ukunikia Co., Ltd., enters a very long phone number into the phone every day.
One day, too tired, Den came up with a surprising idea.
"Isn't it even a little easier if you rearrange the arrangement of the buttons on the phone ?!"
The phone has squares evenly spaced at $ 3 \ times 3 $, and each of the nine squares has one button from 1 to 9 that can be sorted.
When typing a phone number, Den can do two things with just one hand:
* Move your index finger to touch one of the adjacent buttons on the side of the button you are currently touching.
* Press the button that your index finger is touching.
Initially, the index finger can be placed to touch any of the buttons 1-9.
Mr. Den thinks that the arrangement that can minimize the number of movements of the index finger from pressing the first button to the end of pressing the last button is efficient.
Now, here is the phone number of the customer with a length of $ N $.
What kind of arrangement is most efficient when considering only the customer's phone number?
Make the arrangement by rearranging the buttons.
Constraints
The input satisfies the following conditions.
* $ 1 \ leq N \ leq 10 ^ 5 $
* $ S $ is a string consisting of any number from 1 to 9.
Input
The input is given in the following format.
$ N $
$ S $
The first line gives the customer's phone number length $ N $.
The customer's phone number is given to the first line on the second line.
Output
Output the most efficient placement with no blanks on the 3 lines.
However, if there are multiple possible answers, start from the upper left frame.
one two Three
456
789
When arranging the numbers in the order of, output the one that is the smallest in the dictionary order.
Examples
Input
10
1236547896
Output
123
456
789
Input
11
31415926535
Output
137
456
892
|
{"inputs": ["10\n1598114589", "10\n2555486547", "10\n2722382398", "10\n2713182985", "10\n4333783596", "10\n1859787539", "10\n1888971773", "10\n6843843631"], "outputs": ["142\n853\n967\n", "123\n659\n847\n", "139\n428\n576\n", "317\n482\n659\n", "124\n783\n695\n", "124\n853\n796\n", "173\n892\n456\n", "125\n347\n689\n"]}
| 478
| 238
|
coding
|
Solve the programming task below in a Python markdown code block.
Gru has not been in the limelight for a long time and is, therefore, planning something particularly nefarious. Frustrated by his minions' incapability which has kept him away from the limelight, he has built a transmogrifier β a machine which mutates minions.
Each minion has an intrinsic characteristic value (similar to our DNA), which is an integer. The transmogrifier adds an integer K to each of the minions' characteristic value.
Gru knows that if the new characteristic value of a minion is divisible by 7, then it will have Wolverine-like mutations.
Given the initial characteristic integers of N minions, all of which are then transmogrified, find out how many of them become Wolverine-like.
-----Input Format:-----
The first line contains one integer, T, which is the number of test cases. Each test case is then described in two lines.
The first line contains two integers N and K, as described in the statement.
The next line contains N integers, which denote the initial characteristic values for the minions.
-----Output Format:-----
For each testcase, output one integer in a new line, which is the number of Wolverine-like minions after the transmogrification.
-----Constraints:-----
- 1 β€ T β€ 100
- 1 β€ N β€ 100
- 1 β€ K β€ 100
- All initial characteristic values lie between 1 and 105, both inclusive.
-----Example-----
Input:
1
5 10
2 4 1 35 1
Output:
1
-----Explanation:-----
After transmogrification, the characteristic values become {12,14,11,45,11}, out of which only 14 is divisible by 7. So only the second minion becomes Wolverine-like.
|
{"inputs": ["1\n5 2\n0 4 1 3 1", "1\n5 0\n0 4 0 7 1", "1\n5 0\n1 0 0 0 0", "1\n5 3\n2 4 1 3 1", "1\n5 3\n2 4 0 3 1", "1\n5 3\n0 4 0 3 1", "1\n5 3\n0 4 1 3 1", "1\n5 2\n0 4 1 5 1"], "outputs": ["0\n", "3\n", "4\n", "1\n", "1\n", "1\n", "1\n", "1\n"]}
| 396
| 174
|
coding
|
Solve the programming task below in a Python markdown code block.
Let's call (yet again) a string good if its length is even, and every character in odd position of this string is different from the next character (the first character is different from the second, the third is different from the fourth, and so on). For example, the strings good, string and xyyx are good strings, and the strings bad, aa and aabc are not good. Note that the empty string is considered good.
You are given a string $s$, you have to delete minimum number of characters from this string so that it becomes good.
-----Input-----
The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of characters in $s$.
The second line contains the string $s$, consisting of exactly $n$ lowercase Latin letters.
-----Output-----
In the first line, print one integer $k$ ($0 \le k \le n$) β the minimum number of characters you have to delete from $s$ to make it good.
In the second line, print the resulting string $s$. If it is empty, you may leave the second line blank, or not print it at all.
-----Examples-----
Input
4
good
Output
0
good
Input
4
aabc
Output
2
ab
Input
3
aaa
Output
3
|
{"inputs": ["1\na\n", "1\nf\n", "1\na\n", "1\nf\n", "1\nb\n", "1\ng\n", "1\nc\n", "1\nh\n"], "outputs": ["1\n\n", "1\n\n", "1\n\n", "1\n\n", "1\n\n", "1\n\n", "1\n\n", "1\n\n"]}
| 300
| 94
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a length of string and two thumbtacks. On thumbtack goes into the focus point *Fβ* with coordinates *xβ* and *yβ*, and the other does into point *Fβ* with points *xβ* and *yβ*. The string is then tied at the ends to the thumbtacks and has length *l* excluding the knots at the ends. If you pull the string taught with a pencil and draw around the plane you'll have an ellipse with focuses at *Fβ* and *Fβ*. Given a new point *P*, determine if it falls inside of the ellipse.
You must write a function that takes arguments `f0`, `f1`, `l`, and `p` and returns `true` or `false` depending on whether or not `p` falls inside the ellipse.
Each of `f0`, `f1`, and `p` has has properties `x` and `y` for its coordinates.
You will never be given the case where the string is too short to reach between the points.
Also feel free to reuse/extend the following starter code:
```python
def ellipse_contains_point(f0, f1, l, p):
```
|
{"functional": "_inputs = [[{'x': 0, 'y': 0}, {'x': 0, 'y': 0}, 2, {'x': 0, 'y': 0}], [{'x': 0, 'y': 0}, {'x': 0, 'y': 0}, 2, {'x': 1, 'y': 1}]]\n_outputs = [[True], [False]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(ellipse_contains_point(*i), o[0])"}
| 267
| 236
|
coding
|
Solve the programming task below in a Python markdown code block.
You have a given integer $n$. Find the number of ways to fill all $3 \times n$ tiles with the shape described in the picture below. Upon filling, no empty spaces are allowed. Shapes cannot overlap. $\square$ This picture describes when $n = 4$. The left one is the shape and the right one is $3 \times n$ tiles.
-----Input-----
The only line contains one integer $n$ ($1 \le n \le 60$)Β β the length.
-----Output-----
Print the number of ways to fill.
-----Examples-----
Input
4
Output
4
Input
1
Output
0
-----Note-----
In the first example, there are $4$ possible cases of filling.
In the second example, you cannot fill the shapes in $3 \times 1$ tiles.
|
{"inputs": ["4\n", "1\n", "2\n", "8\n", "5\n", "7\n", "3\n", "6\n"], "outputs": ["4", "0", "2", "16", "0", "0", "0", "8"]}
| 188
| 63
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
On day 1, one person discovers a secret.
You are given an integer delay, which means that each person will share the secret with a new person every day, starting from delay days after discovering the secret. You are also given an integer forget, which means that each person will forget the secret forget days after discovering it. A person cannot share the secret on the same day they forgot it, or on any day afterwards.
Given an integer n, return the number of people who know the secret at the end of day n. Since the answer may be very large, return it modulo 109 + 7.
Β
Please complete the following python code precisely:
```python
class Solution:
def peopleAwareOfSecret(self, n: int, delay: int, forget: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(n = 6, delay = 2, forget = 4) == 5\n assert candidate(n = 4, delay = 1, forget = 3) == 6\n\n\ncheck(Solution().peopleAwareOfSecret)"}
| 183
| 65
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Alice has n candies, where the ith candy is of type candyType[i]. Alice noticed that she started to gain weight, so she visited a doctor.
The doctor advised Alice to only eat n / 2 of the candies she has (n is always even). Alice likes her candies very much, and she wants to eat the maximum number of different types of candies while still following the doctor's advice.
Given the integer array candyType of length n, return the maximum number of different types of candies she can eat if she only eats n / 2 of them.
Β
Please complete the following python code precisely:
```python
class Solution:
def distributeCandies(self, candyType: List[int]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(candyType = [1,1,2,2,3,3]) == 3\n assert candidate(candyType = [1,1,2,3]) == 2\n assert candidate(candyType = [6,6,6,6]) == 1\n\n\ncheck(Solution().distributeCandies)"}
| 165
| 86
|
coding
|
Solve the programming task below in a Python markdown code block.
The spring is coming and it means that a lot of fruits appear on the counters. One sunny day little boy Valera decided to go shopping. He made a list of m fruits he wanted to buy. If Valera want to buy more than one fruit of some kind, he includes it into the list several times.
When he came to the fruit stall of Ashot, he saw that the seller hadn't distributed price tags to the goods, but put all price tags on the counter. Later Ashot will attach every price tag to some kind of fruits, and Valera will be able to count the total price of all fruits from his list. But Valera wants to know now what can be the smallest total price (in case of the most Β«luckyΒ» for him distribution of price tags) and the largest total price (in case of the most Β«unluckyΒ» for him distribution of price tags).
Input
The first line of the input contains two integer number n and m (1 β€ n, m β€ 100) β the number of price tags (which is equal to the number of different kinds of fruits that Ashot sells) and the number of items in Valera's list. The second line contains n space-separated positive integer numbers. Each of them doesn't exceed 100 and stands for the price of one fruit of some kind. The following m lines contain names of the fruits from the list. Each name is a non-empty string of small Latin letters which length doesn't exceed 32. It is guaranteed that the number of distinct fruits from the list is less of equal to n. Also it is known that the seller has in stock all fruits that Valera wants to buy.
Output
Print two numbers a and b (a β€ b) β the minimum and the maximum possible sum which Valera may need to buy all fruits from his list.
Examples
Input
5 3
4 2 1 10 5
apple
orange
mango
Output
7 19
Input
6 5
3 5 1 6 8 1
peach
grapefruit
banana
orange
orange
Output
11 30
|
{"inputs": ["1 4\n1\nu\nu\nu\nu\n", "1 4\n2\nu\nu\nu\nu\n", "3 1\n6 26 22\naga\n", "3 1\n6 26 42\naga\n", "3 1\n6 26 39\naga\n", "3 1\n14 26 22\naag\n", "3 1\n25 26 22\naag\n", "3 1\n25 26 22\naga\n"], "outputs": ["4 4\n", "8 8\n", "6 26\n", "6 42\n", "6 39\n", "14 26\n", "22 26\n", "22 26\n"]}
| 468
| 196
|
coding
|
Solve the programming task below in a Python markdown code block.
B-Mansion and courier
Problem Statement
Taro lives alone in a mansion. Taro, who loves studying, intends to study in his study in the house today. Taro can't concentrate outside the study, so he always studies in the study.
However, on this day, $ N $ of courier service to Taro arrived. $ i $ ($ 1 \ leq i \ leq N $) The arrival time of the third courier is $ a_i $. It is painful to have the delivery person wait at the front door, so Taro decided to be at the front door by the time the courier arrives. Due to the large size of the mansion, it takes $ M $ one way to move between the study and the entrance.
On the other hand, Taro wants to study for as long as possible. Find the maximum amount of time Taro can study in the study from time $ 0 $ to time $ T $.
Taro is in the study at time $ 0 $, and the courier does not arrive earlier than the time $ M $, and the courier does not arrive later than the time $ T $. Also, the time it takes for Taro to receive the courier can be ignored.
Input
Each dataset consists of two lines. The first line consists of three integers $ N, M, T $ separated by blanks. These integers satisfy $ 1 \ leq N \ leq 100 $, $ 1 \ leq M \ leq 10 {,} 000 $, $ 1 \ leq T \ leq 10 {,} 000 $. The second line consists of $ N $ integers $ a_1, a_2, \ dots, a_N $ separated by blanks. Each $ a_i $ fills $ M \ leq a_i \ leq T $ and is also $ a_i <a_ {i + 1} $ ($ 1 \ leq i <N $).
Output
Output an integer representing the maximum amount of time Taro can study on one line.
Sample Input 1
1 1 5
3
Output for the Sample Input 1
3
Sample Input 2
2 1 10
2 7
Output for the Sample Input 2
6
Sample Input 3
2 4 10
6 8
Output for the Sample Input 3
2
Example
Input
1 1 5
3
Output
3
|
{"inputs": ["1 1 5\n6", "1 2 9\n9", "1 1 5\n5", "1 1 5\n1", "1 1 2\n3", "1 2 4\n3", "1 1 0\n1", "1 1 5\n3"], "outputs": ["5\n", "7\n", "4\n", "3\n", "2\n", "1\n", "0\n", "3"]}
| 535
| 109
|
coding
|
Solve the programming task below in a Python markdown code block.
Anna Hazare is a well known social activist in India.
On 5th April, 2011 he started "Lokpal Bill movement".
Chef is very excited about this movement. He is thinking of contributing to it. He gathers his cook-herd and starts thinking about how our community can contribute to this.
All of them are excited about this too, but no one could come up with any idea. Cooks were slightly disappointed with this and went to consult their friends.
One of the geekiest friend gave them the idea of spreading knowledge through Facebook. But we do not want to spam people's wall. So our cook came up with the idea of dividing Facebook users into small friend groups and then identify the most popular friend in each group and post on his / her wall. They started dividing users into groups of friends and identifying the most popular amongst them.
The notoriety of a friend is defined as the averaged distance from all the other friends in his / her group. This measure considers the friend himself, and the trivial distance of '0' that he / she has with himself / herself.
The most popular friend in a group is the friend whose notoriety is least among all the friends in the group.
Distance between X and Y is defined as follows:
Minimum number of profiles that X needs to visit for reaching Y's profile(Including Y's profile). X can open only those profiles which are in the friend list of the current opened profile. For Example:
Suppose A is friend of B.
B has two friends C and D.
E is a friend of D.
Now, the distance between A and B is 1, A and C is 2, C and E is 3.
So, one of our smart cooks took the responsibility of identifying the most popular friend in each group and others will go to persuade them for posting. This cheeky fellow knows that he can release his burden by giving this task as a long contest problem.
Now, he is asking you to write a program to identify the most popular friend among all the friends in each group. Also, our smart cook wants to know the average distance of everyone from the most popular friend.
------ Input ------
Friends in a group are labelled with numbers to hide their Facebook identity. The first line of input contains the number of groups in which users are divided. First line of each group contains the number of friends that belong to that group. i^{th} line of each group contains the space separated friend list of 'i'. You are assured that each friend's profile can be accessed by the profile of every other friend by following some sequence of profile visits.
------ Output ------
Your output contains the most popular friend name label along with the average distance (space separated) from all other friends (including himself / herself) in six digits of precision. There might be multiple most popular friend, in that case output the friend labelled with least number.
------ Note: ------
Each person in a group have atleast one friend and he/she cannot be in his/her own friend list.
Number of friends in a group cannot be more than 100.
There are atmost 100 groups.
----- Sample Input 1 ------
1
6
3
5
1 4
3 5 6
2 4 6
4 5
----- Sample Output 1 ------
4 1.166667
|
{"inputs": ["1\n6\n3\n5\n1 4\n3 5 6\n2 4 6\n4 5"], "outputs": ["4 1.166667"]}
| 715
| 47
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given a string array features where features[i] is a single word that represents the name of a feature of the latest product you are working on. You have made a survey where users have reported which features they like. You are given a string array responses, where each responses[i] is a string containing space-separated words.
The popularity of a feature is the number of responses[i] that contain the feature. You want to sort the features in non-increasing order by their popularity. If two features have the same popularity, order them by their original index in features. Notice that one response could contain the same feature multiple times; this feature is only counted once in its popularity.
Return the features in sorted order.
Β
Please complete the following python code precisely:
```python
class Solution:
def sortFeatures(self, features: List[str], responses: List[str]) -> List[str]:
```
|
{"functional": "def check(candidate):\n assert candidate(features = [\"cooler\",\"lock\",\"touch\"], responses = [\"i like cooler cooler\",\"lock touch cool\",\"locker like touch\"]) == [\"touch\",\"cooler\",\"lock\"]\n assert candidate(features = [\"a\",\"aa\",\"b\",\"c\"], responses = [\"a\",\"a aa\",\"a a a a a\",\"b a\"]) == [\"a\",\"aa\",\"b\",\"c\"]\n\n\ncheck(Solution().sortFeatures)"}
| 198
| 112
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given two integers $a$ and $b$. Moreover, you are given a sequence $s_0, s_1, \dots, s_{n}$. All values in $s$ are integers $1$ or $-1$. It's known that sequence is $k$-periodic and $k$ divides $n+1$. In other words, for each $k \leq i \leq n$ it's satisfied that $s_{i} = s_{i - k}$.
Find out the non-negative remainder of division of $\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i}$ by $10^{9} + 9$.
Note that the modulo is unusual!
-----Input-----
The first line contains four integers $n, a, b$ and $k$ $(1 \leq n \leq 10^{9}, 1 \leq a, b \leq 10^{9}, 1 \leq k \leq 10^{5})$.
The second line contains a sequence of length $k$ consisting of characters '+' and '-'.
If the $i$-th character (0-indexed) is '+', then $s_{i} = 1$, otherwise $s_{i} = -1$.
Note that only the first $k$ members of the sequence are given, the rest can be obtained using the periodicity property.
-----Output-----
Output a single integerΒ β value of given expression modulo $10^{9} + 9$.
-----Examples-----
Input
2 2 3 3
+-+
Output
7
Input
4 1 5 1
-
Output
999999228
-----Note-----
In the first example:
$(\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i})$ = $2^{2} 3^{0} - 2^{1} 3^{1} + 2^{0} 3^{2}$ = 7
In the second example:
$(\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i}) = -1^{4} 5^{0} - 1^{3} 5^{1} - 1^{2} 5^{2} - 1^{1} 5^{3} - 1^{0} 5^{4} = -781 \equiv 999999228 \pmod{10^{9} + 9}$.
|
{"inputs": ["4 1 5 1\n-\n", "4 1 6 1\n-\n", "4 2 6 1\n-\n", "4 1 5 1\n-\n", "1 1 4 2\n-+\n", "1 1 4 2\n-+\n", "2 2 3 3\n+-+\n", "2 2 3 3\n+-+\n"], "outputs": ["999999228\n", "999998454\n", "999998073\n", "999999228\n", "3\n", "3\n", "7\n", "7\n"]}
| 584
| 170
|
coding
|
Solve the programming task below in a Python markdown code block.
Kawashiro Nitori is a girl who loves competitive programming.
One day she found a string and an integer. As an advanced problem setter, she quickly thought of a problem.
Given a string $s$ and a parameter $k$, you need to check if there exist $k+1$ non-empty strings $a_1,a_2...,a_{k+1}$, such that $$s=a_1+a_2+\ldots +a_k+a_{k+1}+R(a_k)+R(a_{k-1})+\ldots+R(a_{1}).$$
Here $+$ represents concatenation. We define $R(x)$ as a reversed string $x$. For example $R(abcd) = dcba$. Note that in the formula above the part $R(a_{k+1})$ is intentionally skipped.
-----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 description contains two integers $n$, $k$ ($1\le n\le 100$, $0\le k\le \lfloor \frac{n}{2} \rfloor$) β the length of the string $s$ and the parameter $k$.
The second line of each test case description contains a single string $s$ of length $n$, consisting of lowercase English letters.
-----Output-----
For each test case, print "YES" (without quotes), if it is possible to find $a_1,a_2,\ldots,a_{k+1}$, and "NO" (without quotes) otherwise.
You can print letters in any case (upper or lower).
-----Examples-----
Input
7
5 1
qwqwq
2 1
ab
3 1
ioi
4 2
icpc
22 0
dokidokiliteratureclub
19 8
imteamshanghaialice
6 3
aaaaaa
Output
YES
NO
YES
NO
YES
NO
NO
-----Note-----
In the first test case, one possible solution is $a_1=qw$ and $a_2=q$.
In the third test case, one possible solution is $a_1=i$ and $a_2=o$.
In the fifth test case, one possible solution is $a_1=dokidokiliteratureclub$.
|
{"inputs": ["7\n5 1\nqwqwq\n2 1\nc`\n3 1\nioi\n4 2\niqcc\n22 0\ndokidokiliteratureclub\n9 8\nimteamshanghaialice\n6 3\naaaaaa\n", "7\n5 1\nqwqwq\n2 1\nab\n3 1\nioi\n4 2\nicpc\n22 0\ndokidokiliteratureclub\n19 8\nimteamshanghaialice\n6 3\naaaaaa\n", "7\n5 1\nqwqwq\n2 1\nab\n3 1\nioi\n4 2\nipcc\n22 0\ndokidokiliteratureclub\n19 8\nimteamshanghaialice\n6 3\naaaaaa\n", "7\n5 1\nqwqwq\n2 1\nab\n3 1\nioi\n4 2\nipcc\n22 0\ndokidokiliteratureclub\n19 0\necilaiahgnahsmaetmi\n6 3\naaaaaa\n", "7\n5 1\nqwqwq\n2 1\nab\n3 1\nioi\n4 2\nipcc\n22 0\ndokidokiliteratureclub\n19 0\necilaiahgnahsmaetmi\n6 0\naaaaaa\n", "7\n5 1\nqwqwq\n2 1\nab\n3 1\nioi\n4 2\nipcc\n22 0\ndokidokiliteratureclub\n19 8\nimteamshanghaialice\n6 2\naaaaaa\n", "7\n5 1\nrwqwq\n2 2\nab\n3 1\nioi\n4 2\nipcc\n22 0\ndokidokiliteratureclub\n19 0\necilbinhgaahsmaetmi\n6 3\naaaaaa\n", "7\n5 1\nwwqqq\n2 1\nab\n3 1\nioi\n4 2\nipcc\n22 0\ndoridokilitekatureclub\n19 0\necilaiahgnahtmaetmi\n6 0\naaaaaa\n"], "outputs": ["YES\nNO\nYES\nNO\nYES\nNO\nNO\n", "YES\nNO\nYES\nNO\nYES\nNO\nNO\n", "YES\nNO\nYES\nNO\nYES\nNO\nNO\n", "YES\nNO\nYES\nNO\nYES\nYES\nNO\n", "YES\nNO\nYES\nNO\nYES\nYES\nYES\n", "YES\nNO\nYES\nNO\nYES\nNO\nYES\n", "NO\nNO\nYES\nNO\nYES\nYES\nNO\n", "NO\nNO\nYES\nNO\nYES\nYES\nYES\n"]}
| 553
| 672
|
coding
|
Solve the programming task below in a Python markdown code block.
In this kata, you've to count lowercase letters in a given string and return the letter count in a hash with 'letter' as key and count as 'value'. The key must be 'symbol' instead of string in Ruby and 'char' instead of string in Crystal.
Example:
```python
letter_count('arithmetics') #=> {"a": 1, "c": 1, "e": 1, "h": 1, "i": 2, "m": 1, "r": 1, "s": 1, "t": 2}
```
Also feel free to reuse/extend the following starter code:
```python
def letter_count(s):
```
|
{"functional": "_inputs = [['codewars'], ['activity'], ['arithmetics'], ['traveller'], ['daydreamer']]\n_outputs = [[{'a': 1, 'c': 1, 'd': 1, 'e': 1, 'o': 1, 'r': 1, 's': 1, 'w': 1}], [{'a': 1, 'c': 1, 'i': 2, 't': 2, 'v': 1, 'y': 1}], [{'a': 1, 'c': 1, 'e': 1, 'h': 1, 'i': 2, 'm': 1, 'r': 1, 's': 1, 't': 2}], [{'a': 1, 'e': 2, 'l': 2, 'r': 2, 't': 1, 'v': 1}], [{'a': 2, 'd': 2, 'e': 2, 'm': 1, 'r': 2, 'y': 1}]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(letter_count(*i), o[0])"}
| 159
| 382
|
coding
|
Solve the programming task below in a Python markdown code block.
Baby Badawy's first words were "AND 0 SUM BIG", so he decided to solve the following problem. Given two integers $n$ and $k$, count the number of arrays of length $n$ such that:
all its elements are integers between $0$ and $2^k-1$ (inclusive);
the bitwise AND of all its elements is $0$;
the sum of its elements is as large as possible.
Since the answer can be very large, print its remainder when divided by $10^9+7$.
-----Input-----
The first line contains an integer $t$ ($1 \le t \le 10$) β the number of test cases you need to solve.
Each test case consists of a line containing two integers $n$ and $k$ ($1 \le n \le 10^{5}$, $1 \le k \le 20$).
-----Output-----
For each test case, print the number of arrays satisfying the conditions. Since the answer can be very large, print its remainder when divided by $10^9+7$.
-----Examples-----
Input
2
2 2
100000 20
Output
4
226732710
-----Note-----
In the first example, the $4$ arrays are:
$[3,0]$,
$[0,3]$,
$[1,2]$,
$[2,1]$.
|
{"inputs": ["1\n2 3\n", "1\n2 3\n", "1\n2 2\n", "1\n0 2\n", "1\n1 3\n", "1\n2 4\n", "1\n5 20\n", "1\n9 20\n"], "outputs": ["8\n", "8\n", "4\n", "0\n", "1\n", "16\n", "430973056\n", "953271190\n"]}
| 321
| 121
|
coding
|
Solve the programming task below in a Python markdown code block.
A chess tournament will be held soon, where $n$ chess players will take part. Every participant will play one game against every other participant. Each game ends in either a win for one player and a loss for another player, or a draw for both players.
Each of the players has their own expectations about the tournament, they can be one of two types:
a player wants not to lose any game (i. e. finish the tournament with zero losses);
a player wants to win at least one game.
You have to determine if there exists an outcome for all the matches such that all the players meet their expectations. If there are several possible outcomes, print any of them. If there are none, report that it's impossible.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 200$) β the number of test cases.
The first line of each test case contains one integer $n$ ($2 \le n \le 50$) β the number of chess players.
The second line contains the string $s$ ($|s| = n$; $s_i \in \{1, 2\}$). If $s_i = 1$, then the $i$-th player has expectations of the first type, otherwise of the second type.
-----Output-----
For each test case, print the answer in the following format:
In the first line, print NO if it is impossible to meet the expectations of all players.
Otherwise, print YES, and the matrix of size $n \times n$ in the next $n$ lines.
The matrix element in the $i$-th row and $j$-th column should be equal to:
+, if the $i$-th player won in a game against the $j$-th player;
-, if the $i$-th player lost in a game against the $j$-th player;
=, if the $i$-th and $j$-th players' game resulted in a draw;
X, if $i = j$.
-----Examples-----
Input
3
3
111
2
21
4
2122
Output
YES
X==
=X=
==X
NO
YES
X--+
+X++
+-X-
--+X
-----Note-----
None
|
{"inputs": ["3\n3\n111\n1\n1\n4\n2122\n", "3\n3\n111\n2\n21\n4\n2122\n", "3\n3\n111\n2\n21\n4\n2122\n", "3\n3\n111\n2\n12\n4\n2122\n", "3\n3\n111\n2\n11\n4\n2122\n", "3\n3\n111\n2\n22\n4\n2122\n", "3\n3\n111\n2\n21\n4\n2122\n"], "outputs": ["YES\nX==\n=X=\n==X\nYES\nX\nYES\nX=+-\n=X==\n-=X+\n+=-X\n", "YES\nX==\n=X=\n==X\nNO\nYES\nX=+-\n=X==\n-=X+\n+=-X\n", "YES\nX==\n=X=\n==X\nNO\nYES\nX=+-\n=X==\n-=X+\n+=-X\n", "YES\nX==\n=X=\n==X\nNO\nYES\nX=+-\n=X==\n-=X+\n+=-X\n", "YES\nX==\n=X=\n==X\nYES\nX=\n=X\nYES\nX=+-\n=X==\n-=X+\n+=-X\n", "YES\nX==\n=X=\n==X\nNO\nYES\nX=+-\n=X==\n-=X+\n+=-X\n", "YES\nX==\n=X=\n==X\nNO\nYES\nX=+-\n=X==\n-=X+\n+=-X\n"]}
| 500
| 411
|
coding
|
Solve the programming task below in a Python markdown code block.
You are the judge at a competitive eating competition and you need to choose a winner!
There are three foods at the competition and each type of food is worth a different amount of points.
Points are as follows:
- Chickenwings: 5 points
- Hamburgers: 3 points
- Hotdogs: 2 points
Write a function that helps you create a scoreboard.
It takes as a parameter a list of objects representing the participants, for example:
```
[
{name: "Habanero Hillary", chickenwings: 5 , hamburgers: 17, hotdogs: 11},
{name: "Big Bob" , chickenwings: 20, hamburgers: 4, hotdogs: 11}
]
```
It should return
"name" and "score" properties sorted by score; if scores are equals, sort alphabetically by name.
```
[
{name: "Big Bob", score: 134},
{name: "Habanero Hillary", score: 98}
]
```
Happy judging!
Also feel free to reuse/extend the following starter code:
```python
def scoreboard(who_ate_what):
```
|
{"functional": "_inputs = [[[{'name': 'Billy The Beast', 'chickenwings': 17, 'hamburgers': 7, 'hotdogs': 8}, {'name': 'Habanero Hillary', 'chickenwings': 5, 'hamburgers': 17, 'hotdogs': 11}, {'name': 'Joey Jaws', 'chickenwings': 8, 'hamburgers': 8, 'hotdogs': 15}, {'name': 'Big Bob', 'chickenwings': 20, 'hamburgers': 4, 'hotdogs': 11}]], [[{'name': 'Big Bob', 'chickenwings': 20, 'hamburgers': 4, 'hotdogs': 11}]], [[{'name': 'Joey Jaws', 'chickenwings': 8, 'hamburgers': 8, 'hotdogs': 15}, {'name': 'Big Bob', 'chickenwings': 20, 'hamburgers': 4, 'hotdogs': 11}]], [[{'name': 'Joey Jaws', 'chickenwings': 0, 'hamburgers': 1, 'hotdogs': 1}, {'name': 'Big Bob', 'chickenwings': 1, 'hamburgers': 0, 'hotdogs': 0}]], [[]]]\n_outputs = [[[{'name': 'Big Bob', 'score': 134}, {'name': 'Billy The Beast', 'score': 122}, {'name': 'Habanero Hillary', 'score': 98}, {'name': 'Joey Jaws', 'score': 94}]], [[{'name': 'Big Bob', 'score': 134}]], [[{'name': 'Big Bob', 'score': 134}, {'name': 'Joey Jaws', 'score': 94}]], [[{'name': 'Big Bob', 'score': 5}, {'name': 'Joey Jaws', 'score': 5}]], [[]]]\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(scoreboard(*i), o[0])"}
| 265
| 604
|
coding
|
Solve the programming task below in a Python markdown code block.
β Hey folks, how do you like this problem?
β That'll do it.
BThero is a powerful magician. He has got $n$ piles of candies, the $i$-th pile initially contains $a_i$ candies. BThero can cast a copy-paste spell as follows: He chooses two piles $(i, j)$ such that $1 \le i, j \le n$ and $i \ne j$. All candies from pile $i$ are copied into pile $j$. Formally, the operation $a_j := a_j + a_i$ is performed.
BThero can cast this spell any number of times he wants to β but unfortunately, if some pile contains strictly more than $k$ candies, he loses his magic power. What is the maximum number of times BThero can cast the spell without losing his power?
-----Input-----
The first line contains one integer $T$ ($1 \le T \le 500$) β the number of test cases.
Each test case consists of two lines: the first line contains two integers $n$ and $k$ ($2 \le n \le 1000$, $2 \le k \le 10^4$); the second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le k$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $1000$, and the sum of $k$ over all test cases does not exceed $10^4$.
-----Output-----
For each test case, print one integer β the maximum number of times BThero can cast the spell without losing his magic power.
-----Example-----
Input
3
2 2
1 1
3 5
1 2 3
3 7
3 2 2
Output
1
5
4
-----Note-----
In the first test case we get either $a = [1, 2]$ or $a = [2, 1]$ after casting the spell for the first time, and it is impossible to cast it again.
|
{"inputs": ["3\n2 2\n1 1\n3 5\n1 2 3\n3 7\n3 2 2\n", "3\n2 2\n2 1\n3 5\n1 2 3\n3 7\n3 2 2\n", "3\n2 2\n1 1\n3 5\n1 2 3\n3 7\n4 2 2\n", "3\n2 2\n2 1\n3 5\n1 3 3\n3 7\n3 2 2\n", "3\n2 4\n1 1\n3 5\n1 2 3\n3 7\n4 2 2\n", "3\n2 2\n2 1\n3 5\n1 3 3\n3 7\n4 2 2\n", "3\n2 2\n2 1\n3 5\n1 4 3\n3 7\n4 2 2\n", "3\n2 2\n1 1\n3 5\n1 2 4\n3 7\n4 2 2\n"], "outputs": ["1\n5\n4\n", "0\n5\n4\n", "1\n5\n3\n", "0\n4\n4\n", "3\n5\n3\n", "0\n4\n3\n", "0\n3\n3\n", "1\n4\n3\n"]}
| 478
| 326
|
coding
|
Solve the programming task below in a Python markdown code block.
There is a train going from Station A to Station B that costs X yen (the currency of Japan).
Also, there is a bus going from Station B to Station C that costs Y yen.
Joisino got a special ticket. With this ticket, she can take the bus for half the fare if she travels from Station A to Station B by train and then travels from Station B to Station C by bus.
How much does it cost to travel from Station A to Station C if she uses this ticket?
-----Constraints-----
- 1 \leq X,Y \leq 100
- Y is an even number.
- All values in input are integers.
-----Input-----
Input is given from Standard Input in the following format:
X Y
-----Output-----
If it costs x yen to travel from Station A to Station C, print x.
-----Sample Input-----
81 58
-----Sample Output-----
110
- The train fare is 81 yen.
- The train fare is 58 β 2=29 yen with the 50% discount.
Thus, it costs 110 yen to travel from Station A to Station C.
|
{"inputs": ["5 0", "1 2", "1 2\n", "3 54", "3 34", "5 34", "5 -1", "51 1"], "outputs": ["5\n", "2\n", "2\n", "30\n", "20\n", "22\n", "4\n", "51\n"]}
| 256
| 87
|
coding
|
Solve the programming task below in a Python markdown code block.
Mr. X has come up with a new string compression algorithm. Consider a string of length N which contains up to K distinct characters. The compression algorithm works as follows: Replace each maximal contiguous substring containing only one distinct character (repeated an arbitrary number of times) and replace it by 2 values: the character and the length of the substring.
For example, the string "aabbaaa" will be compressed to "a, 2, b, 2, a, 3". Thus the length of the compressed string is 6.
Since Mr. X is living in advanced times, the length of any integer is considered to be 1. For example, if a string is compressed to "a, 111, b, 13", then its length after compression is considered to be 4.
To test his algorithm, he needs to know the expected length of the compressed string for given N and K if the input string is randomly uniformly chosen from all possibilities. He wants to run this experiment multiple times for different N, K and needs your help.
-----Input-----
The first line of the input contains an integer T denoting the number of queries. The description of T test cases follows.
The first and only line of each test case contains two integers N and K denoting the number of letters in the input string and the maximum number of distinct characters that can be present in the string.
-----Output-----
For each test case, output a single line containing the expected length of the compressed string.
Your answer will be considered correct if the absolute error is less than 10-2
-----Constraints-----
- 1 β€ T β€ 105
- 1 β€ N, K β€ 109
-----Example-----
Input:
2
3 1
3 2
Output:
2.0
4.0
-----Explanation-----Example case 1:
There is only one string: aaa with compressed string = a, 3. Therefore length = 2
Example case 2
Input strings:
"aaa": "a, 3". Length = 2
"aab": "a, 2, b, 1". Length = 4
"aba": "a, 1, b, 1, a, 1". Length = 6
"abb": "a, 1, b, 2". Length = 4
"baa": "b, 1, a, 2". Length = 4
"bab": "b, 1, a, 1, b, 1". Length = 6
"bba": "b, 2, a, 1". Length = 4
"bbb": "b, 3". Length = 2
Expected value = (2+4+6+4+4+6+4+2)/8 = 32/8 = 4
|
{"inputs": ["2\n3 1\n3 2\n"], "outputs": ["2.0\n4.0"]}
| 615
| 27
|
coding
|
Solve the programming task below in a Python markdown code block.
A person wants to determine the most expensive computer keyboard and USB drive that can be purchased with a give budget. Given price lists for keyboards and USB drives and a budget, find the cost to buy them. If it is not possible to buy both items, return $-1$.
Example
$\boldsymbol{b=60}$
$keyboards=[40,50,60]$
$drives=[5,8,12]$
The person can buy a $40\ \text{keyboard}\ +12\:\text{USB drive}\ =52$, or a $50\:\text{keyboard}\:+8\:\text{USB drive}\:=58$. Choose the latter as the more expensive option and return $58$.
Function Description
Complete the getMoneySpent function in the editor below.
getMoneySpent has the following parameter(s):
int keyboards[n]: the keyboard prices
int drives[m]: the drive prices
int b: the budget
Returns
int: the maximum that can be spent, or $-1$ if it is not possible to buy both items
Input Format
The first line contains three space-separated integers $\boldsymbol{b}$, $n$, and $m$, the budget, the number of keyboard models and the number of USB drive models.
The second line contains $n$ space-separated integers $\textit{keyboard[i]}$, the prices of each keyboard model.
The third line contains $m$ space-separated integers $\textbf{drives}$, the prices of the USB drives.
Constraints
$1\leq n,m\leq1000$
$1\leq b\leq10^{6}$
The price of each item is in the inclusive range $[1,10^{6}]$.
Sample Input 0
10 2 3
3 1
5 2 8
Sample Output 0
9
Explanation 0
Buy the $2^{nd}$ keyboard and the $3^{rd}$ USB drive for a total cost of $8+1=9$.
Sample Input 1
5 1 1
4
5
Sample Output 1
-1
Explanation 1
There is no way to buy one keyboard and one USB drive because $4+5>5$, so return $-1$.
|
{"inputs": ["5 1 1\n4\n5\n", "10 2 3\n3 1\n5 2 8\n"], "outputs": ["-1\n", "9\n"]}
| 514
| 46
|
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, check whether it is a mirror of itself (i.e., symmetric around its center).
Β
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 isSymmetric(self, root: Optional[TreeNode]) -> bool:
```
|
{"functional": "def check(candidate):\n assert candidate(root = tree_node([1,2,2,3,4,4,3])) == True\n assert candidate(root = tree_node([1,2,2,None,3,None,3])) == False\n\n\ncheck(Solution().isSymmetric)"}
| 126
| 68
|
coding
|
Solve the programming task below in a Python markdown code block.
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
{"inputs": ["1 1 1\n1\n", "1 1 1\n0\n", "1 1 1\n0\n", "1 1 1\n1\n", "2 2 1\n0 1\n1 0\n", "2 2 1\n0 1\n1 0\n", "2 2 2\n0 1\n1 0\n", "2 2 2\n0 1\n0 0\n"], "outputs": ["1 0\n", "0 0\n", "0 0", "1 0", "2 0\n", "2 0", "2 0\n", "1 0\n"]}
| 445
| 155
|
coding
|
Solve the programming task below in a Python markdown code block.
Write a program to obtain a number $N$ and increment its value by 1 if the number is divisible by 4 $otherwise$ decrement its value by 1.
-----Input:-----
- First line will contain a number $N$.
-----Output:-----
Output a single line, the new value of the number.
-----Constraints-----
- $0 \leq N \leq 1000$
-----Sample Input:-----
5
-----Sample Output:-----
4
-----EXPLANATION:-----
Since 5 is not divisible by 4 hence, its value is decreased by 1.
|
{"inputs": ["5"], "outputs": ["4"]}
| 137
| 12
|
coding
|
Solve the programming task below in a Python markdown code block.
# Task
**_Given_** a **_Divisor and a Bound_** , *Find the largest integer N* , Such That ,
# Conditions :
* **_N_** is *divisible by divisor*
* **_N_** is *less than or equal to bound*
* **_N_** is *greater than 0*.
___
# Notes
* The **_parameters (divisor, bound)_** passed to the function are *only positive values* .
* *It's guaranteed that* a **divisor is Found** .
___
# Input >> Output Examples
```
maxMultiple (2,7) ==> return (6)
```
## Explanation:
**_(6)_** is divisible by **_(2)_** , **_(6)_** is less than or equal to bound **_(7)_** , and **_(6)_** is > 0 .
___
```
maxMultiple (10,50) ==> return (50)
```
## Explanation:
**_(50)_** *is divisible by* **_(10)_** , **_(50)_** is less than or equal to bound **_(50)_** , and **_(50)_** is > 0 .*
___
```
maxMultiple (37,200) ==> return (185)
```
## Explanation:
**_(185)_** is divisible by **_(37)_** , **_(185)_** is less than or equal to bound **_(200)_** , and **_(185)_** is > 0 .
___
___
## [Playing with Numbers Series](https://www.codewars.com/collections/playing-with-numbers)
# [Playing With Lists/Arrays Series](https://www.codewars.com/collections/playing-with-lists-slash-arrays)
# [Bizarre Sorting-katas](https://www.codewars.com/collections/bizarre-sorting-katas)
# [For More Enjoyable Katas](http://www.codewars.com/users/MrZizoScream/authored)
___
## ALL translations are welcomed
## Enjoy Learning !!
# Zizou
~~~if:java
Java's default return statement can be any `int`, a divisor **will** be found.
~~~
~~~if:nasm
## NASM-specific notes
The function declaration is `int max_multiple(int divisor, int bound)` where the first argument is the divisor and the second one is the bound.
~~~
Also feel free to reuse/extend the following starter code:
```python
def max_multiple(divisor, bound):
```
|
{"functional": "_inputs = [[2, 7], [3, 10], [7, 17], [10, 50], [37, 200], [7, 100]]\n_outputs = [[6], [9], [14], [50], [185], [98]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(max_multiple(*i), o[0])"}
| 574
| 216
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given a string s and two integers x and y. You can perform two types of operations any number of times.
Remove substring "ab" and gain x points.
For example, when removing "ab" from "cabxbae" it becomes "cxbae".
Remove substring "ba" and gain y points.
For example, when removing "ba" from "cabxbae" it becomes "cabxe".
Return the maximum points you can gain after applying the above operations on s.
Β
Please complete the following python code precisely:
```python
class Solution:
def maximumGain(self, s: str, x: int, y: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(s = \"cdbcbbaaabab\", x = 4, y = 5) == 19\n assert candidate(s = \"aabbaaxybbaabb\", x = 5, y = 4) == 20\n\n\ncheck(Solution().maximumGain)"}
| 159
| 74
|
coding
|
Solve the programming task below in a Python markdown code block.
Chef has started developing interest in playing chess, and was learning how the [Queen] moves.
Chef has an empty N \times N chessboard. He places a Queen at (X, Y) and wonders - What are the number of cells that are under attack by the Queen?
Notes:
The top-left cell is (1, 1), the top-right cell is (1, N), the bottom-left cell is (N,1) and the bottom-right cell is (N, N).
The Queen can be moved any number of unoccupied cells in a straight line vertically, horizontally, or diagonally.
The cell on which the Queen is present, is not said to be under attack by the Queen.
------ Input Format ------
- The first line contains a single integer T - the number of test cases. Then the test cases follow.
- The first and only line of each test case contains three integers N, X and Y, as described in the problem statement.
------ Output Format ------
For each test case, output in a single line, the total number of cells that are under attack by the Queen.
------ Constraints ------
$1 β€ T β€ 10^{4}$
$1 β€ N β€ 10^{6}$
$1 β€ X, Y β€ N$
----- Sample Input 1 ------
5
1 1 1
3 2 2
3 2 1
2 2 2
150 62 41
----- Sample Output 1 ------
0
8
6
3
527
----- explanation 1 ------
Test case 1: The only cell on the board is $(1,1)$. Since Queen stands on this cell, it is not under attack.
Test case 2: The Queen can attack the following cells: $\{(1, 1), (1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2), (3, 3)\}$.
Test case 3: The Queen can attack the following cells: $\{(1, 1), (1, 2), (2, 2), (2, 3), (3, 1), (3, 2)\}$.
Test case 4: The Queen can attack the following cells: $\{(1, 1), (1, 2), (2, 1)\}$.
|
{"inputs": ["5\n1 1 1\n3 2 2\n3 2 1\n2 2 2\n150 62 41\n"], "outputs": ["0\n8\n6\n3\n527\n"]}
| 529
| 58
|
coding
|
Solve the programming task below in a Python markdown code block.
Calvin the robot lies in an infinite rectangular grid. Calvin's source code contains a list of n commands, each either 'U', 'R', 'D', or 'L'Β β instructions to move a single square up, right, down, or left, respectively. How many ways can Calvin execute a non-empty contiguous substrings of commands and return to the same square he starts in? Two substrings are considered different if they have different starting or ending indices.
-----Input-----
The first line of the input contains a single positive integer, n (1 β€ n β€ 200)Β β the number of commands.
The next line contains n characters, each either 'U', 'R', 'D', or 'L'Β β Calvin's source code.
-----Output-----
Print a single integerΒ β the number of contiguous substrings that Calvin can execute and return to his starting square.
-----Examples-----
Input
6
URLLDR
Output
2
Input
4
DLUU
Output
0
Input
7
RLRLRLR
Output
12
-----Note-----
In the first case, the entire source code works, as well as the "RL" substring in the second and third characters.
Note that, in the third case, the substring "LR" appears three times, and is therefore counted three times to the total result.
|
{"inputs": ["1\nR\n", "1\nR\n", "2\nDU\n", "2\nDU\n", "2\nUD\n", "4\nDLUU\n", "4\nRRDR\n", "4\nRRDR\n"], "outputs": ["0\n", "0", "1\n", "1", "1\n", "0\n", "0\n", "0"]}
| 293
| 86
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
There is a safe protected by a password. The password is a sequence of n digits where each digit can be in the range [0, k - 1].
The safe has a peculiar way of checking the password. When you enter in a sequence, it checks the most recent n digits that were entered each time you type a digit.
For example, the correct password is "345" and you enter in "012345":
After typing 0, the most recent 3 digits is "0", which is incorrect.
After typing 1, the most recent 3 digits is "01", which is incorrect.
After typing 2, the most recent 3 digits is "012", which is incorrect.
After typing 3, the most recent 3 digits is "123", which is incorrect.
After typing 4, the most recent 3 digits is "234", which is incorrect.
After typing 5, the most recent 3 digits is "345", which is correct and the safe unlocks.
Return any string of minimum length that will unlock the safe at some point of entering it.
Β
Please complete the following python code precisely:
```python
class Solution:
def crackSafe(self, n: int, k: int) -> str:
```
|
{"functional": "def check(candidate):\n assert candidate(n = 1, k = 2) == \"10\"\n assert candidate(n = 2, k = 2) == \"01100\"\n\n\ncheck(Solution().crackSafe)"}
| 289
| 61
|
coding
|
Solve the programming task below in a Python markdown code block.
Integer sequence $\boldsymbol{a}$ having length $2n+2$ is defined as follows:
$a_{0}=0$
$a_{1}=C$
$a_{i+2}=(a_{i+1}+a_i)\ \text{\% M}$, where $0\leq i<2n$
Write a function generator, $gen$, to generate the remaining values for $a_2$ through $a_{2n+1}$. The values returned by $gen$ describe two-dimensional vectors $v_1\ldots v_n$, where each sequential pair of values describes the respective $\boldsymbol{x}$ and $y$ coordinates for some vector $\boldsymbol{\nu}$ in the form $x_1,y_1,x_2,y_2,\ldots,x_n,y_n$. In other words, $v_1=(a_2,a_3),v_2=(a_4,a_5),...,v_n=(a_{2n},a_{2n+1})$.
Let $\mbox{S}$ be the set of scalar products of $v_i$ and $v_j$ for each $1\leq i,j\leq n$, where $i\neq j$.
Determine the number of different residues in $\mbox{S}$ and print the resulting value modulo $\mbox{M}$.
Input Format
A single line of three space-separated positive integers: $\mbox{C}$ (the value of $a_1$), $\mbox{M}$ (the modulus), and $n$ (the number of two-dimensional vectors), respectively.
Constraints
$1\leq C\leq10^9$
$1\leq M\leq10^9$
$1\leq n\leq3\times10^5$
Output Format
Print a single integer denoting the number of different residues $\textbf{\% M}$ in $\mbox{S}$.
Sample Input
4 5 3
Sample Output
2
Explanation
Sequence $a=a_0,a_1,(a_1+a_0)\text{\%}M,(a_2+a_1)\text{\%}M,...,(a_{2n}+a_{2n-1})\text{\%}M\}$
$=\{0,\ 4,\ (4+0)\%5,\ (4+4)\%5,\ (3+4)\%5,\ (2+3)\%5,\ (0+2)\%5,\ (2+0)\%5\}$
$=\{0,4,4,3,2,0,2,2\}$.
This gives us our vectors: $v_1=(4,3)$, $v_2=(2,0)$, and $v_3=(2,2)$.
Scalar product $S_0(v_1,v_2)=8$.
Scalar product $S_2(v_2,v_3)=4$.
Scalar product $S_0(v_1,v_3)=14$.
There are $2$ residues $\%5$ in $\mbox{S}$ (i.e.: $3$ and $4$), so we print the result of $2\%5$ (which is $2$).
|
{"inputs": ["4 5 3\n"], "outputs": ["2\n"]}
| 720
| 18
|
coding
|
Solve the programming task below in a Python markdown code block.
# Task
Mobius function - an important function in number theory. For each given n, it only has 3 values:
```
0 -- if n divisible by square of a prime. Such as: 4, 8, 9
1 -- if n not divisible by any square of a prime
and have even number of prime factor. Such as: 6, 10, 21
-1 -- otherwise. Such as: 3, 5, 7, 30```
Your task is to find mobius(`n`)
# Input/Output
- `[input]` integer `n`
`2 <= n <= 1e12`
- `[output]` an integer
Also feel free to reuse/extend the following starter code:
```python
def mobius(n):
```
|
{"functional": "_inputs = [[10], [9], [8], [100000000001], [7], [5]]\n_outputs = [[1], [0], [0], [0], [-1], [-1]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(mobius(*i), o[0])"}
| 191
| 197
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.