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.
While working at DTL, Ela is very aware of her physical and mental health. She started to practice various sports, such as Archery, Yoga, and Football.
Since she started engaging in sports activities, Ela switches to trying a new sport on days she considers being "Luxury" days. She counts the days since she started these activities, in which the day she starts is numbered as day $1$. A "Luxury" day is the day in which the number of this day is a luxurious number.
An integer $x$ is called a luxurious number if it is divisible by ${\lfloor \sqrt{x} \rfloor}$.
Here $\lfloor r \rfloor$ denotes the "floor" of a real number $r$. In other words, it's the largest integer not greater than $r$.
For example: $8$, $56$, $100$ are luxurious numbers, since $8$ is divisible by $\lfloor \sqrt{8} \rfloor = \lfloor 2.8284 \rfloor = 2$, $56$ is divisible $\lfloor \sqrt{56} \rfloor = \lfloor 7.4833 \rfloor = 7$, and $100$ is divisible by $\lfloor \sqrt{100} \rfloor = \lfloor 10 \rfloor = 10$, respectively. On the other hand $5$, $40$ are not, since $5$ are not divisible by $\lfloor \sqrt{5} \rfloor = \lfloor 2.2361 \rfloor = 2$, and $40$ are not divisible by $\lfloor \sqrt{40} \rfloor = \lfloor 6.3246 \rfloor = 6$.
Being a friend of Ela, you want to engage in these fitness activities with her to keep her and yourself accompanied (and have fun together, of course). Between day $l$ and day $r$, you want to know how many times she changes the activities.
-----Input-----
Each test contains multiple test cases. The first line has the number of test cases $t$ ($1 \le t \le 10\ 000$). The description of the test cases follows.
The only line of each test case contains two integers $l$ and $r$ ($1 \le l \le r \le 10^{18}$) — the intervals at which you want to know how many times Ela changes her sports.
-----Output-----
For each test case, output an integer that denotes the answer.
-----Examples-----
Input
5
8 19
8 20
119 121
1 100000000000000000
1234567891011 1000000000000000000
Output
5
6
2
948683296
2996666667
-----Note-----
In the first test case, $5$ luxury numbers in range $[8, 19]$ are: $8, 9, 12, 15, 16$.
|
{"inputs": ["1\n200274443973227574 485348695690455822\n", "1\n999999999999999999 1000000000000000000\n", "5\n8 19\n8 20\n119 121\n1 100000000000000000\n1234567891011 1000000000000000000\n", "2\n747173626475907138 967354071062227214\n818273638681004942 983018368662317582\n", "2\n1000000000000000000 1000000000000000000\n999999999999999999 1000000000000000000\n", "3\n39999999999999999 39999999999999999\n159999999999999999 159999999999999999\n359999999999999999 359999999999999999\n", "10\n84693792843120399 722933352461265626\n1054414613006002 919996041213106080\n141147461122848960 215953152568344802\n36758866361588362 220526816065597583\n135781915245644528 740842808859507601\n49755792151684097 147448792408078224\n101352931056782000 386376107245094465\n480981404419876442 576250181375319363\n52371684468143237 215493860702464568\n230127790848813226 673588575225785240\n"], "outputs": ["747448170\n", "2\n", "5\n6\n2\n948683296\n2996666667\n", "357448618\n260664120\n", "1\n2\n", "1\n1\n1\n", "1677698687\n2780077469\n267034892\n833630426\n1476708851\n482791652\n909695525\n196749638\n706093670\n1023024167\n"]}
| 728
| 1,013
|
coding
|
Solve the programming task below in a Python markdown code block.
Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell $(0, 0)$. Robot can perform the following four kinds of operations: U — move from $(x, y)$ to $(x, y + 1)$; D — move from $(x, y)$ to $(x, y - 1)$; L — move from $(x, y)$ to $(x - 1, y)$; R — move from $(x, y)$ to $(x + 1, y)$.
Vasya also has got a sequence of $n$ operations. Vasya wants to modify this sequence so after performing it the robot will end up in $(x, y)$.
Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: $maxID - minID + 1$, where $maxID$ is the maximum index of a changed operation, and $minID$ is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices $2$, $5$ and $7$ are changed, so the length of changed subsegment is $7 - 2 + 1 = 6$. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is $1$.
If there are no changes, then the length of changed subsegment is $0$. Changing an operation means replacing it with some operation (possibly the same); Vasya can't insert new operations into the sequence or remove them.
Help Vasya! Tell him the minimum length of subsegment that he needs to change so that the robot will go from $(0, 0)$ to $(x, y)$, or tell him that it's impossible.
-----Input-----
The first line contains one integer number $n~(1 \le n \le 2 \cdot 10^5)$ — the number of operations.
The second line contains the sequence of operations — a string of $n$ characters. Each character is either U, D, L or R.
The third line contains two integers $x, y~(-10^9 \le x, y \le 10^9)$ — the coordinates of the cell where the robot should end its path.
-----Output-----
Print one integer — the minimum possible length of subsegment that can be changed so the resulting sequence of operations moves the robot from $(0, 0)$ to $(x, y)$. If this change is impossible, print $-1$.
-----Examples-----
Input
5
RURUU
-2 3
Output
3
Input
4
RULR
1 1
Output
0
Input
3
UUU
100 100
Output
-1
-----Note-----
In the first example the sequence can be changed to LULUU. So the length of the changed subsegment is $3 - 1 + 1 = 3$.
In the second example the given sequence already leads the robot to $(x, y)$, so the length of the changed subsegment is $0$.
In the third example the robot can't end his path in the cell $(x, y)$.
|
{"inputs": ["1\nR\n0 0\n", "1\nU\n0 1\n", "1\nR\n1 0\n", "1\nR\n0 1\n", "1\nU\n0 0\n", "1\nU\n1 0\n", "1\nR\n1 0\n", "1\nU\n0 1\n"], "outputs": ["-1\n", "0\n", "0\n", "1\n", "-1\n", "1\n", "0\n", "0\n"]}
| 715
| 119
|
coding
|
Solve the programming task below in a Python markdown code block.
Write a function called calculate that takes 3 values. The first and third values are numbers. The second value is a character. If the character is "+" , "-", "*", or "/", the function will return the result of the corresponding mathematical function on the two numbers. If the string is not one of the specified characters, the function should return null (throw an `ArgumentException` in C#).
Keep in mind, you cannot divide by zero. If an attempt to divide by zero is made, return null (throw an `ArgumentException` in C#)/(None in Python).
Also feel free to reuse/extend the following starter code:
```python
def calculate(num1, operation, num2):
```
|
{"functional": "_inputs = [[3.2, '+', 8], [3.2, '-', 8], [3.2, '/', 8], [3.2, '*', 8], [-3, '+', 0], [-3, '-', 0], [-2, '/', -2], [-3, '*', 0], [-3, '/', 0], [-3, 'w', 0], [-3, 'w', 1]]\n_outputs = [[11.2], [-4.8], [0.4], [25.6], [-3], [-3], [1], [0], [None], [None], [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(calculate(*i), o[0])"}
| 153
| 282
|
coding
|
Solve the programming task below in a Python markdown code block.
Phoenix is playing with a new puzzle, which consists of $n$ identical puzzle pieces. Each puzzle piece is a right isosceles triangle as shown below.
A puzzle piece
The goal of the puzzle is to create a square using the $n$ pieces. He is allowed to rotate and move the pieces around, but none of them can overlap and all $n$ pieces must be used (of course, the square shouldn't contain any holes as well). Can he do it?
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains an integer $n$ ($1 \le n \le 10^9$) — the number of puzzle pieces.
-----Output-----
For each test case, if Phoenix can create a square with the $n$ puzzle pieces, print YES. Otherwise, print NO.
-----Examples-----
Input
3
2
4
6
Output
YES
YES
NO
-----Note-----
For $n=2$, Phoenix can create a square like this:
For $n=4$, Phoenix can create a square like this:
For $n=6$, it is impossible for Phoenix to create a square.
|
{"inputs": ["1\n2\n", "1\n82\n", "1\n24\n", "1\n31\n", "1\n56\n", "1\n93\n", "1\n21\n", "1\n883\n"], "outputs": ["YES\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n"]}
| 285
| 94
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given an array $a$ of length $n$. You can perform the following operation several (possibly, zero) times:
Choose $i$, $j$, $b$: Swap the $b$-th digit in the binary representation of $a_i$ and $a_j$.
Find the maximum possible value of $\max(a) - \min(a)$.
In a binary representation, bits are numbered from right (least significant) to left (most significant). Consider that there are an infinite number of leading zero bits at the beginning of any binary representation.
For example, swap the $0$-th bit for $4=100_2$ and $3=11_2$ will result $101_2=5$ and $10_2=2$. Swap the $2$-nd bit for $4=100_2$ and $3=11_2$ will result $000_2=0_2=0$ and $111_2=7$.
Here, $\max(a)$ denotes the maximum element of array $a$ and $\min(a)$ denotes the minimum element of array $a$.
The binary representation of $x$ is $x$ written in base $2$. For example, $9$ and $6$ written in base $2$ are $1001$ and $110$, respectively.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 128$) — the number of testcases.
The first line of each test case contains a single integer $n$ ($3 \le n \le 512$) — the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i < 1024$) — the elements of array $a$.
It's guaranteed that the sum of $n$ over all testcases does not exceed $512$.
-----Output-----
For each testcase, print one integer — the maximum possible value of $\max(a) - \min(a)$.
-----Examples-----
Input
4
3
1 0 1
4
5 5 5 5
5
1 2 3 4 5
7
20 85 100 41 76 49 36
Output
1
0
7
125
-----Note-----
In the first example, it can be shown that we do not need to perform any operations — the maximum value of $\max(a) - \min(a)$ is $1 - 0 = 1$.
In the second example, no operation can change the array — the maximum value of $\max(a) - \min(a)$ is $5 - 5 = 0$.
In the third example, initially $a = [1, 2, 3, 4, 5]$, we can perform one operation taking $i = 2$, $j = 5$, $b = 1$. The array now becomes $a = [1, 0, 3, 4, 7]$. It can be shown that any further operations do not lead to a better answer — therefore the answer is $\max(a) - \min(a) = 7 - 0 = 7$.
|
{"inputs": ["4\n3\n1 0 1\n4\n5 5 5 5\n5\n1 2 3 4 5\n7\n20 85 100 41 76 49 36\n"], "outputs": ["1\n0\n7\n125\n"]}
| 744
| 76
|
coding
|
Solve the programming task below in a Python markdown code block.
.union()
The .union() operator returns the union of a set and the set of elements in an iterable.
Sometimes, the | operator is used in place of .union() operator, but it operates only on the set of elements in set.
Set is immutable to the .union() operation (or | operation).
Example
>>> s = set("Hacker")
>>> print s.union("Rank")
set(['a', 'R', 'c', 'r', 'e', 'H', 'k', 'n'])
>>> print s.union(set(['R', 'a', 'n', 'k']))
set(['a', 'R', 'c', 'r', 'e', 'H', 'k', 'n'])
>>> print s.union(['R', 'a', 'n', 'k'])
set(['a', 'R', 'c', 'r', 'e', 'H', 'k', 'n'])
>>> print s.union(enumerate(['R', 'a', 'n', 'k']))
set(['a', 'c', 'r', 'e', (1, 'a'), (2, 'n'), 'H', 'k', (3, 'k'), (0, 'R')])
>>> print s.union({"Rank":1})
set(['a', 'c', 'r', 'e', 'H', 'k', 'Rank'])
>>> s | set("Rank")
set(['a', 'R', 'c', 'r', 'e', 'H', 'k', 'n'])
Task
The students of District College have subscriptions to English and French newspapers. Some students have subscribed only to English, some have subscribed to only French and some have subscribed to both newspapers.
You are given two sets of student roll numbers. One set has subscribed to the English newspaper, and the other set is subscribed to the French newspaper. The same student could be in both sets. Your task is to find the total number of students who have subscribed to at least one newspaper.
Input Format
The first line contains an integer, $n$, the number of students who have subscribed to the English newspaper.
The second line contains $n$ space separated roll numbers of those students.
The third line contains $\boldsymbol{b}$, the number of students who have subscribed to the French newspaper.
The fourth line contains $\boldsymbol{b}$ space separated roll numbers of those students.
Constraints
$0<\textit{Total number of students in college}<1000$
Output Format
Output the total number of students who have at least one subscription.
Sample Input
9
1 2 3 4 5 6 7 8 9
9
10 1 2 3 11 21 55 6 8
Sample Output
13
Explanation
Roll numbers of students who have at least one subscription:
$1,2,3,4,5,6,7,8,9,10,11,21$ and $55$. Roll numbers: $1,2,3,6$ and $8$ are in both sets so they are only counted once.
Hence, the total is $13$ students.
|
{"inputs": ["9\n1 2 3 4 5 6 7 8 9\n9\n10 1 2 3 11 21 55 6 8\n"], "outputs": ["13\n"]}
| 684
| 57
|
coding
|
Solve the programming task below in a Python markdown code block.
We say that a binary string (a string containing only characters '0' and '1') is pure if it does not contain either of the strings "0101" or "1010" as a subsequence.
Recall that string T is a subsequence of string S if we can delete some of the letters of S (possibly none) such that the resulting string will become T.
You are given a binary string $S$ with length $N$. We want to make this string pure by deleting some (possibly zero) characters from it. What is the minimum number of characters we have to delete?
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first and only line of each test case contains a single string $S$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer — the minimum number of characters we have to delete from $S$.
-----Constraints-----
- $1 \le T \le 40$
- $1 \le N \le 1,000$
- $S$ contains only characters '0' and '1'
-----Example Input-----
4
010111101
1011100001011101
0110
111111
-----Example Output-----
2
3
0
0
-----Explanation-----
Example case 1: We can delete the first and third character of our string. There is no way to make the string pure by deleting only one character.
Example case 3: The given string is already pure, so the answer is zero.
|
{"inputs": ["4\n010111101\n1011100001011101\n0110\n111111"], "outputs": ["2\n3\n0\n0"]}
| 382
| 57
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given an array of integers arr of even length n and an integer k.
We want to divide the array into exactly n / 2 pairs such that the sum of each pair is divisible by k.
Return true If you can find a way to do that or false otherwise.
Please complete the following python code precisely:
```python
class Solution:
def canArrange(self, arr: List[int], k: int) -> bool:
```
|
{"functional": "def check(candidate):\n assert candidate(arr = [1,2,3,4,5,10,6,7,8,9], k = 5) == True\n assert candidate(arr = [1,2,3,4,5,6], k = 7) == True\n assert candidate(arr = [1,2,3,4,5,6], k = 10) == False\n\n\ncheck(Solution().canArrange)"}
| 107
| 107
|
coding
|
Solve the programming task below in a Python markdown code block.
As we all know, Dhoni loves drinking milk. Once he and Sir Jadeja were invited in the inauguration of a Dairy company in Ranchi.
The company had arranged n jars of milk from various breeds of cows , jar number i containing a[i] litres of milk. Since Dhoni loves driking milk more than Sir Jadeja, so Kohli suggested a plan for them. His plan was that each time Dhoni will choose a jar containing the maximum amount of milk. If this jar has less than k litres of milk or if Dhoni has already drunk more than m number of times from this jar, then the milk contained in the jar will be drunk by Sir Jadeja. Sir Jadeja will drink all the milk left in that jar. Otherwise Dhoni will drink exactly k litres of milk from the jar and put it back at its position. Dhoni will do so until he has given all jars to Sir Jadeja.
You have to calculate how much milk Sir Jadega will get after Dhoni satisfies his hunger modulo 1,000,000,007.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains integer N , M, K denoting the number of milk jars, maximum number of time Dhoni will drink from any jar and maximum amount of milk Dhoni will drink at any time respectively. The second line contains N space-separated integers A1, A2, ..., AN denoting the amount of milk in each jar.
-----Output-----
- For each test case, output a single line containing the amount of milk Sir Jadega will get modulo 1,000,000,007.
-----Constraints-----
- 1 ≤ T ≤ 10
- 1 ≤ N ≤ 10^5
- 0 ≤ M ≤ 10^6
- 1 ≤ K ≤ 10^6
- 0 ≤ Ai ≤ 10^9
-----Example-----
Input:
1
3 3 3
15 8 10
Output:
9
|
{"inputs": ["1\n3 3 3\n15 8 10"], "outputs": ["9"]}
| 481
| 26
|
coding
|
Solve the programming task below in a Python markdown code block.
In the previous Kata we discussed the OR case.
We will now discuss the AND case, where rather than calculating the probablility for either of two (or more) possible results, we will calculate the probability of receiving all of the viewed outcomes.
For example, if we want to know the probability of receiving head OR tails in two tosses of a coin, as in the last Kata we add the two probabilities together. However if we want to know the probability of receiving head AND tails, in that order, we have to work differently.
The probability of an AND event is calculated by the following rule:
`P(A ∩ B) = P(A | B) * P(B)`
or
`P(B ∩ A) = P(B | A) * P(A)`
That is, the probability of A and B both occuring is equal to the probability of A given B occuring times the probability of B occuring or vice versa.
If the events are mutually exclusive like in the case of tossing a coin, the probability of A occuring if B has occured is equal to the probability of A occuring by itself. In this case, the probability can be written as the below:
`P(A ∩ B) = P(A) * P(B)`
or
`P(B ∩ A) = P(B) * P(A)`
Applying to the heads and tails case:
`P(H ∩ T) = P(0.5) * P(0.5)`
or
`P(H ∩ T) = P(0.5) * P(0.5)`
The task:
You are given a random bag of 10 balls containing 4 colours. `Red`, `Green`, `Yellow` and `Blue`. You will also be given a sequence of 2 balls of any colour e.g. `Green` and `Red` or `Yellow` and `Yellow`.
You have to return the probability of pulling a ball at random out of the bag and it matching the first colour and then pulling a ball at random out of the bag and it matching the second colour.
You will be given a boolean value of `true` or `false` which indicates whether the balls that is taken out in the first draw is replaced in to the bag for the second draw. Hint: this will determine whether the events are mutually exclusive or not.
You will receive two arrays and a boolean value. The first array contains the colours of the balls in the bag and the second contains the colour of the two balls you have to receive. As per above the final boolean value will indicate whether the bals arel being replaced `true` or not `false`.
Return the probability to 3 decimal places.
e.g. `[["y","r","g","b","b","y","r","g","r","r"],["r","b"],false]`
Other Kata in this series:
Statistics in Kata 1: OR case - Unfair dice
Also feel free to reuse/extend the following starter code:
```python
def ball_probability(balls):
```
|
{"functional": "_inputs = [[[['red', 'blue', 'yellow', 'green', 'red', 'blue', 'yellow', 'green', 'red', 'blue'], ['red', 'blue'], True]], [[['red', 'blue', 'yellow', 'green', 'red', 'blue', 'yellow', 'green', 'red', 'blue'], ['red', 'red'], True]], [[['red', 'red', 'yellow', 'green', 'red', 'red', 'yellow', 'green', 'red', 'red'], ['blue', 'blue'], True]], [[['red', 'blue', 'yellow', 'green', 'red', 'blue', 'yellow', 'green', 'red', 'blue'], ['red', 'blue'], False]], [[['red', 'blue', 'yellow', 'green', 'red', 'blue', 'yellow', 'green', 'red', 'blue'], ['red', 'red'], False]]]\n_outputs = [[0.09], [0.09], [0], [0.1], [0.067]]\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(ball_probability(*i), o[0])"}
| 650
| 370
|
coding
|
Solve the programming task below in a Python markdown code block.
What joy! Petya's parents went on a business trip for the whole year and the playful kid is left all by himself. Petya got absolutely happy. He jumped on the bed and threw pillows all day long, until...
Today Petya opened the cupboard and found a scary note there. His parents had left him with duties: he should water their favourite flower all year, each day, in the morning, in the afternoon and in the evening. "Wait a second!" — thought Petya. He know for a fact that if he fulfills the parents' task in the i-th (1 ≤ i ≤ 12) month of the year, then the flower will grow by ai centimeters, and if he doesn't water the flower in the i-th month, then the flower won't grow this month. Petya also knows that try as he might, his parents won't believe that he has been watering the flower if it grows strictly less than by k centimeters.
Help Petya choose the minimum number of months when he will water the flower, given that the flower should grow no less than by k centimeters.
Input
The first line contains exactly one integer k (0 ≤ k ≤ 100). The next line contains twelve space-separated integers: the i-th (1 ≤ i ≤ 12) number in the line represents ai (0 ≤ ai ≤ 100).
Output
Print the only integer — the minimum number of months when Petya has to water the flower so that the flower grows no less than by k centimeters. If the flower can't grow by k centimeters in a year, print -1.
Examples
Input
5
1 1 1 1 2 2 3 2 2 1 1 1
Output
2
Input
0
0 0 0 0 0 0 0 1 1 2 3 0
Output
0
Input
11
1 1 4 1 1 5 1 1 4 1 1 1
Output
3
Note
Let's consider the first sample test. There it is enough to water the flower during the seventh and the ninth month. Then the flower grows by exactly five centimeters.
In the second sample Petya's parents will believe him even if the flower doesn't grow at all (k = 0). So, it is possible for Petya not to water the flower at all.
|
{"inputs": ["0\n0 0 0 0 0 0 0 0 0 0 0 0\n", "1\n0 0 0 0 0 0 0 0 0 0 0 0\n", "1\n0 0 0 0 0 0 0 0 0 0 1 0\n", "0\n0 0 0 1 0 0 0 1 1 2 3 0\n", "5\n1 1 1 1 2 2 6 2 2 1 1 1\n", "0\n0 0 0 1 0 0 1 1 1 2 3 0\n", "5\n1 1 1 1 0 2 6 2 2 1 1 1\n", "0\n0 0 0 0 0 0 0 1 1 2 3 0\n"], "outputs": ["0\n", "-1\n", "1\n", "0\n", "1\n", "0\n", "1\n", "0\n"]}
| 539
| 262
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given two identical eggs and you have access to a building with n floors labeled from 1 to n.
You know that there exists a floor f where 0 <= f <= n such that any egg dropped at a floor higher than f will break, and any egg dropped at or below floor f will not break.
In each move, you may take an unbroken egg and drop it from any floor x (where 1 <= x <= n). If the egg breaks, you can no longer use it. However, if the egg does not break, you may reuse it in future moves.
Return the minimum number of moves that you need to determine with certainty what the value of f is.
Please complete the following python code precisely:
```python
class Solution:
def twoEggDrop(self, n: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(n = 2) == 2\n assert candidate(n = 100) == 14\n\n\ncheck(Solution().twoEggDrop)"}
| 190
| 48
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problems statements in Mandarin Chinese and Russian as well.
There are N+1 lights. Lights are placed at (0, 0), (1, 0), (2, 0) ... (N, 0). Initially all the lights are on. You want to turn off all of them one after one. You want to follow a special pattern in turning off the lights.
You will start at (0, 0). First, you walk to the right most light that is on, turn it off. Then you walk to the left most light that is on, turn it off. Then again to the right most light that is on and so on. You will stop after turning off all lights. You want to know how much distance you walked in the process. Note that distance between (a,0) and (b,0) is |a-b|.
------ Input ------
The first line of the input contains an integer T denoting the number of test cases. Each test case has a single integer N on separate line.
------ Output ------
For each test case, output the distance you walked.
------ Constraints ------
$1 ≤ T ≤ 10^{5}
$$1 ≤ N ≤ 10^{5}
----- Sample Input 1 ------
2
1
2
----- Sample Output 1 ------
2
5
----- explanation 1 ------
Testcase #2
You are initially at (0, 0)
Right most on-light is (2, 0). Distance = 2.
Now you are at (2, 0).
Left most on-light is (0, 0). Distance = 2.
Now you are at (0, 0)
Right most on-light is (1, 0). Distance = 1.
Now you are at (1, 0) and all lights are turned off.
Total distance walked = 5.
|
{"inputs": ["2\n1\n2", "2\n1\n4", "2\n2\n4", "2\n2\n1", "2\n0\n1", "2\n0\n2", "2\n2\n2", "2\n4\n2"], "outputs": ["2\n5", "2\n14\n", "5\n14\n", "5\n2\n", "0\n2\n", "0\n5\n", "5\n5\n", "14\n5\n"]}
| 408
| 112
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given an integer n, find a sequence that satisfies all of the following:
The integer 1 occurs once in the sequence.
Each integer between 2 and n occurs twice in the sequence.
For every integer i between 2 and n, the distance between the two occurrences of i is exactly i.
The distance between two numbers on the sequence, a[i] and a[j], is the absolute difference of their indices, |j - i|.
Return the lexicographically largest sequence. It is guaranteed that under the given constraints, there is always a solution.
A sequence a is lexicographically larger than a sequence b (of the same length) if in the first position where a and b differ, sequence a has a number greater than the corresponding number in b. For example, [0,1,9,0] is lexicographically larger than [0,1,5,6] because the first position they differ is at the third number, and 9 is greater than 5.
Please complete the following python code precisely:
```python
class Solution:
def constructDistancedSequence(self, n: int) -> List[int]:
```
|
{"functional": "def check(candidate):\n assert candidate(n = 3) == [3,1,2,3,2]\n assert candidate(n = 5) == [5,3,1,4,3,5,2,4,2]\n\n\ncheck(Solution().constructDistancedSequence)"}
| 253
| 71
|
coding
|
Solve the programming task below in a Python markdown code block.
Petya loves football very much. One day, as he was watching a football match, he was writing the players' current positions on a piece of paper. To simplify the situation he depicted it as a string consisting of zeroes and ones. A zero corresponds to players of one team; a one corresponds to players of another team. If there are at least 7 players of some team standing one after another, then the situation is considered dangerous. For example, the situation 00100110111111101 is dangerous and 11110111011101 is not. You are given the current situation. Determine whether it is dangerous or not.
Input
The first input line contains a non-empty string consisting of characters "0" and "1", which represents players. The length of the string does not exceed 100 characters. There's at least one player from each team present on the field.
Output
Print "YES" if the situation is dangerous. Otherwise, print "NO".
Examples
Input
001001
Output
NO
Input
1000000001
Output
YES
|
{"inputs": ["1\n", "2\n", "01\n", "010\n", "110\n", "100\n", "000001\n", "010001\n"], "outputs": ["NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n"]}
| 264
| 87
|
coding
|
Solve the programming task below in a Python markdown code block.
Kicker (table football) is a board game based on football, in which players control the footballers' figures mounted on rods by using bars to get the ball into the opponent's goal. When playing two on two, one player of each team controls the goalkeeper and the full-backs (plays defence), the other player controls the half-backs and forwards (plays attack).
Two teams of company Q decided to battle each other. Let's enumerate players from both teams by integers from 1 to 4. The first and second player play in the first team, the third and the fourth one play in the second team. For each of the four players we know their game skills in defence and attack. The defence skill of the i-th player is a_{i}, the attack skill is b_{i}.
Before the game, the teams determine how they will play. First the players of the first team decide who will play in the attack, and who will play in the defence. Then the second team players do the same, based on the choice of their opponents.
We will define a team's defence as the defence skill of player of the team who plays defence. Similarly, a team's attack is the attack skill of the player of the team who plays attack. We assume that one team is guaranteed to beat the other one, if its defence is strictly greater than the opponent's attack and its attack is strictly greater than the opponent's defence.
The teams of company Q know each other's strengths and therefore arrange their teams optimally. Identify the team that is guaranteed to win (if both teams act optimally) or tell that there is no such team.
-----Input-----
The input contain the players' description in four lines. The i-th line contains two space-separated integers a_{i} and b_{i} (1 ≤ a_{i}, b_{i} ≤ 100) — the defence and the attack skill of the i-th player, correspondingly.
-----Output-----
If the first team can win, print phrase "Team 1" (without the quotes), if the second team can win, print phrase "Team 2" (without the quotes). If no of the teams can definitely win, print "Draw" (without the quotes).
-----Examples-----
Input
1 100
100 1
99 99
99 99
Output
Team 1
Input
1 1
2 2
3 3
2 2
Output
Team 2
Input
3 3
2 2
1 1
2 2
Output
Draw
-----Note-----
Let consider the first test sample. The first team can definitely win if it will choose the following arrangement: the first player plays attack, the second player plays defence.
Consider the second sample. The order of the choosing roles for players makes sense in this sample. As the members of the first team choose first, the members of the second team can beat them (because they know the exact defence value and attack value of the first team).
|
{"inputs": ["1 1\n2 2\n3 3\n2 2\n", "3 3\n2 2\n1 1\n2 2\n", "8 7\n1 5\n7 4\n8 8\n", "8 3\n4 9\n6 1\n5 6\n", "6 2\n7 5\n5 4\n8 6\n", "4 1\n4 3\n6 4\n2 8\n", "8 7\n1 5\n7 4\n8 8\n", "4 1\n4 3\n6 4\n2 8\n"], "outputs": ["Team 2\n", "Draw\n", "Draw\n", "Team 1\n", "Draw\n", "Team 2\n", "Draw\n", "Team 2\n"]}
| 643
| 190
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given an integer array values where values[i] represents the value of the ith sightseeing spot. Two sightseeing spots i and j have a distance j - i between them.
The score of a pair (i < j) of sightseeing spots is values[i] + values[j] + i - j: the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Please complete the following python code precisely:
```python
class Solution:
def maxScoreSightseeingPair(self, values: List[int]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(values = [8,1,5,2,6]) == 11\n assert candidate(values = [1,2]) == 2\n\n\ncheck(Solution().maxScoreSightseeingPair)"}
| 145
| 58
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given two integers $N$ and ${L}}$. Compute the number of unrooted labelled trees with exactly $N$ nodes and exactly ${L}$ leafs. This number can be huge, so please output it's modulo $10^9+7$.
Input Format
The first line of input consists of two space separated integers $N$ and ${L}$.
Constraints
$1\leq L<N\leq10^{6}$
Output Format
Display an answer to the problem on first line of the output.
Sample Input
5 2
Sample Output
60
|
{"inputs": ["5 2"], "outputs": ["60"]}
| 138
| 15
|
coding
|
Solve the programming task below in a Python markdown code block.
For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A.
An increasing subsequence of A is defined by a subsequence {ai0, ai1, ... , aik} where 0 ≤ i0 < i1 < ... < ik < n and ai0 < ai1 < ... < aik.
Constraints
* 1 ≤ n ≤ 100000
* 0 ≤ ai ≤ 109
Input
n
a0
a1
:
an-1
In the first line, an integer n is given. In the next n lines, elements of A are given.
Output
The length of the longest increasing subsequence of A.
Examples
Input
5
5
1
3
2
4
Output
3
Input
3
1
1
1
Output
1
|
{"inputs": ["3\n1\n1\n2", "3\n1\n1\n0", "3\n0\n1\n2", "3\n0\n2\n2", "3\n1\n2\n1", "3\n0\n1\n3", "3\n0\n3\n2", "3\n1\n2\n0"], "outputs": ["2\n", "1\n", "3\n", "2\n", "2\n", "3\n", "2\n", "2\n"]}
| 212
| 110
|
coding
|
Solve the programming task below in a Python markdown code block.
Chef's coding class is very famous in Chefland.
This year X students joined his class and each student will require one chair to sit on. Chef already has Y chairs in his class. Determine the minimum number of new chairs Chef must buy so that every student is able to get one chair to sit on.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first and only line of each test case contains two integers X and Y — the number of students in the class and the number of chairs Chef already has.
------ Output Format ------
For each test case, output the minimum number of extra chairs Chef must buy so that every student gets one chair.
------ Constraints ------
$1 ≤ T ≤ 1000$
$0 ≤ X, Y ≤ 100$
----- Sample Input 1 ------
4
20 14
41 41
35 0
50 100
----- Sample Output 1 ------
6
0
35
0
----- explanation 1 ------
- Test case 1: There are $20$ students in the class and Chef has $14$ chairs already. Therefore Chef must buy $6$ more chairs.
- Test case 2: There are $41$ students in the class and Chef already has exactly $41$ chairs. Therefore Chef does not need to buy any more chairs.
- Test case 3: There are $35$ students in the class and Chef has no chairs initially. Therefore Chef must buy $35$ chairs.
|
{"inputs": ["4\n20 14\n41 41\n35 0\n50 100\n"], "outputs": ["6\n0\n35\n0\n"]}
| 354
| 45
|
coding
|
Solve the programming task below in a Python markdown code block.
The chef is trying to solve some pattern problems, Chef wants your help to code it. Chef has one number K to form a new pattern. Help the chef to code this pattern problem.
-----Input:-----
- First-line will contain $T$, the number of test cases. Then the test cases follow.
- Each test case contains a single line of input, one integer $K$.
-----Output:-----
For each test case, output as the pattern.
-----Constraints-----
- $1 \leq T \leq 100$
- $1 \leq K \leq 100$
-----Sample Input:-----
3
2
3
4
-----Sample Output:-----
0
*1
**2
0
*1
**2
***3
0
*1
**2
***3
****4
-----EXPLANATION:-----
No need, else pattern can be decode easily.
|
{"inputs": ["3\n2\n3\n4\n"], "outputs": ["0\n*1\n**2\n0\n*1\n**2\n***3\n0\n*1\n**2\n***3\n****4"]}
| 207
| 50
|
coding
|
Solve the programming task below in a Python markdown code block.
Little09 and his friends are playing a game. There are $n$ players, and the temperature value of the player $i$ is $i$.
The types of environment are expressed as $0$ or $1$. When two players fight in a specific environment, if its type is $0$, the player with a lower temperature value in this environment always wins; if it is $1$, the player with a higher temperature value in this environment always wins. The types of the $n-1$ environments form a binary string $s$ with a length of $n-1$.
If there are $x$ players participating in the game, there will be a total of $x-1$ battles, and the types of the $x-1$ environments will be the first $x-1$ characters of $s$. While there is more than one player left in the tournament, choose any two remaining players to fight. The player who loses will be eliminated from the tournament. The type of the environment of battle $i$ is $s_i$.
For each $x$ from $2$ to $n$, answer the following question: if all players whose temperature value does not exceed $x$ participate in the game, how many players have a chance to win?
-----Input-----
Each test contains multiple test cases. The first line contains a single integer $t$ ($1\le t \le 10^3$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\leq n\leq 2\cdot 10^5$) — the number of players.
The second line of each test case contains a binary string $s$ with a length $n-1$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case output $n-1$ integers — for each $x$ from $2$ to $n$, output the number of players that have a chance to win.
-----Examples-----
Input
2
4
001
4
101
Output
1 1 3
1 2 3
-----Note-----
In the first test case, for $x=2$ and $x=3$, only the player whose temperature value is $1$ can be the winner. For $x=4$, the player whose temperature value is $2,3,4$ can be the winner.
|
{"inputs": ["2\n4\n001\n4\n101\n"], "outputs": ["1 1 3 \n1 2 3 \n"]}
| 552
| 38
|
coding
|
Solve the programming task below in a Python markdown code block.
Vova plans to go to the conference by train. Initially, the train is at the point $1$ and the destination point of the path is the point $L$. The speed of the train is $1$ length unit per minute (i.e. at the first minute the train is at the point $1$, at the second minute — at the point $2$ and so on).
There are lanterns on the path. They are placed at the points with coordinates divisible by $v$ (i.e. the first lantern is at the point $v$, the second is at the point $2v$ and so on).
There is also exactly one standing train which occupies all the points from $l$ to $r$ inclusive.
Vova can see the lantern at the point $p$ if $p$ is divisible by $v$ and there is no standing train at this position ($p \not\in [l; r]$). Thus, if the point with the lantern is one of the points covered by the standing train, Vova can't see this lantern.
Your problem is to say the number of lanterns Vova will see during the path. Vova plans to go to $t$ different conferences, so you should answer $t$ independent queries.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of queries.
Then $t$ lines follow. The $i$-th line contains four integers $L_i, v_i, l_i, r_i$ ($1 \le L, v \le 10^9$, $1 \le l \le r \le L$) — destination point of the $i$-th path, the period of the lantern appearance and the segment occupied by the standing train.
-----Output-----
Print $t$ lines. The $i$-th line should contain one integer — the answer for the $i$-th query.
-----Example-----
Input
4
10 2 3 7
100 51 51 51
1234 1 100 199
1000000000 1 1 1000000000
Output
3
0
1134
0
-----Note-----
For the first example query, the answer is $3$. There are lanterns at positions $2$, $4$, $6$, $8$ and $10$, but Vova didn't see the lanterns at positions $4$ and $6$ because of the standing train.
For the second example query, the answer is $0$ because the only lantern is at the point $51$ and there is also a standing train at this point.
For the third example query, the answer is $1134$ because there are $1234$ lanterns, but Vova didn't see the lanterns from the position $100$ to the position $199$ inclusive.
For the fourth example query, the answer is $0$ because the standing train covers the whole path.
|
{"inputs": ["1\n8 5 1 5\n", "1\n2 5 1 1\n", "1\n3 4 3 3\n", "1\n2 5 1 1\n", "1\n3 4 3 3\n", "1\n8 5 1 5\n", "1\n1 5 1 1\n", "1\n8 4 1 5\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "1\n"]}
| 681
| 134
|
coding
|
Solve the programming task below in a Python markdown code block.
In this kata you need to write a function that will receive two strings (```n1``` and ```n2```), each representing an integer as a binary number. A third parameter will be provided (```o```) as a string representing one of the following operators: add, subtract, multiply.
Your task is to write the calculate function so that it will perform the arithmetic and the result returned should be a string representing the binary result.
Examples:
```
1 + 1 === 10
10 + 10 === 100
```
Negative binary numbers are usually preceded by several 1's. For this kata, negative numbers can be represented with the negative symbol at the beginning of the string.
Examples of negatives:
```
1 - 10 === -1
10 - 100 === -10
```
Also feel free to reuse/extend the following starter code:
```python
def calculate(n1, n2, o):
```
|
{"functional": "_inputs = [['1', '1', 'add'], ['1', '1', 'subtract'], ['1', '1', 'multiply'], ['10', '10', 'multiply'], ['100', '10', 'subtract']]\n_outputs = [['10'], ['0'], ['1'], ['100'], ['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(calculate(*i), o[0])"}
| 216
| 217
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a string $s$ consisting of $n$ lowercase Latin letters.
You have to remove at most one (i.e. zero or one) character of this string in such a way that the string you obtain will be lexicographically smallest among all strings that can be obtained using this operation.
String $s = s_1 s_2 \dots s_n$ is lexicographically smaller than string $t = t_1 t_2 \dots t_m$ if $n < m$ and $s_1 = t_1, s_2 = t_2, \dots, s_n = t_n$ or there exists a number $p$ such that $p \le min(n, m)$ and $s_1 = t_1, s_2 = t_2, \dots, s_{p-1} = t_{p-1}$ and $s_p < t_p$.
For example, "aaa" is smaller than "aaaa", "abb" is smaller than "abc", "pqr" is smaller than "z".
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the length of $s$.
The second line of the input contains exactly $n$ lowercase Latin letters — the string $s$.
-----Output-----
Print one string — the smallest possible lexicographically string that can be obtained by removing at most one character from the string $s$.
-----Examples-----
Input
3
aaa
Output
aa
Input
5
abcda
Output
abca
-----Note-----
In the first example you can remove any character of $s$ to obtain the string "aa".
In the second example "abca" < "abcd" < "abcda" < "abda" < "acda" < "bcda".
|
{"inputs": ["2\nzz\n", "2\nok\n", "2\nas\n", "2\nab\n", "2\nae\n", "2\ncq\n", "2\naz\n", "2\ncb\n"], "outputs": ["z\n", "k\n", "a\n", "a\n", "a\n", "c\n", "a\n", "b\n"]}
| 411
| 88
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given a 0-indexed array of unique strings words.
A palindrome pair is a pair of integers (i, j) such that:
0 <= i, j < words.length,
i != j, and
words[i] + words[j] (the concatenation of the two strings) is a palindrome.
Return an array of all the palindrome pairs of words.
You must write an algorithm with O(sum of words[i].length) runtime complexity.
Please complete the following python code precisely:
```python
class Solution:
def palindromePairs(self, words: List[str]) -> List[List[int]]:
```
|
{"functional": "def check(candidate):\n assert candidate(words = [\"abcd\",\"dcba\",\"lls\",\"s\",\"sssll\"]) == [[0,1],[1,0],[3,2],[2,4]] \n assert candidate(words = [\"bat\",\"tab\",\"cat\"]) == [[0,1],[1,0]] \n assert candidate(words = [\"a\",\"\"]) == [[0,1],[1,0]]\n\n\ncheck(Solution().palindromePairs)"}
| 145
| 110
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given the array houses where houses[i] is the location of the ith house along a street and an integer k, allocate k mailboxes in the street.
Return the minimum total distance between each house and its nearest mailbox.
The test cases are generated so that the answer fits in a 32-bit integer.
Please complete the following python code precisely:
```python
class Solution:
def minDistance(self, houses: List[int], k: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(houses = [1,4,8,10,20], k = 3) == 5\n assert candidate(houses = [2,3,5,12,18], k = 2) == 9\n assert candidate(houses = [7,4,6,1], k = 1) == 8\n assert candidate(houses = [3,6,14,10], k = 4) == 0\n\n\ncheck(Solution().minDistance)"}
| 114
| 125
|
coding
|
Solve the programming task below in a Python markdown code block.
During the winter holidays, the demand for Christmas balls is exceptionally high. Since it's already 2018, the advances in alchemy allow easy and efficient ball creation by utilizing magic crystals.
Grisha needs to obtain some yellow, green and blue balls. It's known that to produce a yellow ball one needs two yellow crystals, green — one yellow and one blue, and for a blue ball, three blue crystals are enough.
Right now there are A yellow and B blue crystals in Grisha's disposal. Find out how many additional crystals he should acquire in order to produce the required number of balls.
-----Input-----
The first line features two integers A and B (0 ≤ A, B ≤ 10^9), denoting the number of yellow and blue crystals respectively at Grisha's disposal.
The next line contains three integers x, y and z (0 ≤ x, y, z ≤ 10^9) — the respective amounts of yellow, green and blue balls to be obtained.
-----Output-----
Print a single integer — the minimum number of crystals that Grisha should acquire in addition.
-----Examples-----
Input
4 3
2 1 1
Output
2
Input
3 9
1 1 3
Output
1
Input
12345678 87654321
43043751 1000000000 53798715
Output
2147483648
-----Note-----
In the first sample case, Grisha needs five yellow and four blue crystals to create two yellow balls, one green ball, and one blue ball. To do that, Grisha needs to obtain two additional crystals: one yellow and one blue.
|
{"inputs": ["4 3\n2 1 1\n", "3 9\n1 1 3\n", "1 1\n0 1 0\n", "0 0\n0 0 0\n", "4 3\n1 0 1\n", "6 0\n1 1 1\n", "4 0\n1 1 1\n", "0 0\n0 0 0\n"], "outputs": ["2\n", "1\n", "0\n", "0\n", "0\n", "4\n", "4\n", "0\n"]}
| 391
| 134
|
coding
|
Solve the programming task below in a Python markdown code block.
There are N squares in a row. The leftmost square contains the integer A, and the rightmost contains the integer B. The other squares are empty.
Aohashi would like to fill the empty squares with integers so that the following condition is satisfied:
* For any two adjacent squares, the (absolute) difference of the two integers in those squares is between C and D (inclusive).
As long as the condition is satisfied, it is allowed to use arbitrarily large or small integers to fill the squares. Determine whether it is possible to fill the squares under the condition.
Constraints
* 3 \leq N \leq 500000
* 0 \leq A \leq 10^9
* 0 \leq B \leq 10^9
* 0 \leq C \leq D \leq 10^9
* All input values are integers.
Input
Input is given from Standard Input in the following format:
N A B C D
Output
Print `YES` if it is possible to fill the squares under the condition; print `NO` otherwise.
Examples
Input
5 1 5 2 4
Output
YES
Input
4 7 6 4 5
Output
NO
Input
48792 105960835 681218449 90629745 90632170
Output
NO
Input
491995 412925347 825318103 59999126 59999339
Output
YES
|
{"inputs": ["4 7 9 4 6", "4 4 9 4 6", "4 4 9 0 6", "3 4 9 0 5", "3 7 9 0 5", "3 7 9 0 3", "3 7 9 0 2", "5 1 5 2 4"], "outputs": ["YES\n", "YES\n", "YES\n", "YES\n", "YES\n", "YES\n", "YES\n", "YES"]}
| 384
| 125
|
coding
|
Solve the programming task below in a Python markdown code block.
Imagine two rings with numbers on them. The inner ring spins clockwise (decreasing by 1 each spin) and the outer ring spins counter clockwise (increasing by 1 each spin). We start with both rings aligned on 0 at the top, and on each move we spin each ring one increment. How many moves will it take before both rings show the same number at the top again?
The inner ring has integers from 0 to innerMax and the outer ring has integers from 0 to outerMax, where innerMax and outerMax are integers >= 1.
```
e.g. if innerMax is 2 and outerMax is 3 then after
1 move: inner = 2, outer = 1
2 moves: inner = 1, outer = 2
3 moves: inner = 0, outer = 3
4 moves: inner = 2, outer = 0
5 moves: inner = 1, outer = 1
Therefore it takes 5 moves for the two rings to reach the same number
Therefore spinningRings(2, 3) = 5
```
```
e.g. if innerMax is 3 and outerMax is 2 then after
1 move: inner = 3, outer = 1
2 moves: inner = 2, outer = 2
Therefore it takes 2 moves for the two rings to reach the same number
spinningRings(3, 2) = 2
```
---
for a bigger challenge, check out the [Performance Version](https://www.codewars.com/kata/59b0b7cd2a00d219ab0000c5) of this kata by @Voile
Also feel free to reuse/extend the following starter code:
```python
def spinning_rings(inner_max, outer_max):
```
|
{"functional": "_inputs = [[2, 3], [3, 2], [1, 1], [2, 2], [3, 3]]\n_outputs = [[5], [2], [1], [3], [2]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(spinning_rings(*i), o[0])"}
| 404
| 196
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given an integer array nums, find the maximum possible bitwise OR of a subset of nums and return the number of different non-empty subsets with the maximum bitwise OR.
An array a is a subset of an array b if a can be obtained from b by deleting some (possibly zero) elements of b. Two subsets are considered different if the indices of the elements chosen are different.
The bitwise OR of an array a is equal to a[0] OR a[1] OR ... OR a[a.length - 1] (0-indexed).
Please complete the following python code precisely:
```python
class Solution:
def countMaxOrSubsets(self, nums: List[int]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(nums = [3,1]) == 2\n assert candidate(nums = [2,2,2]) == 7\n assert candidate(nums = [3,2,1,5]) == 6\n\n\ncheck(Solution().countMaxOrSubsets)"}
| 160
| 70
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given a positive integer n. Each digit of n has a sign according to the following rules:
The most significant digit is assigned a positive sign.
Each other digit has an opposite sign to its adjacent digits.
Return the sum of all digits with their corresponding sign.
Please complete the following python code precisely:
```python
class Solution:
def alternateDigitSum(self, n: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(n = 521) == 4\n assert candidate(n = 111) == 1\n assert candidate(n = 886996) == 0\n\n\ncheck(Solution().alternateDigitSum)"}
| 102
| 65
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a string S of length N, containing lowercase Latin letters. You are also given an integer K.
You would like to create a new string S' by following the following process:
First, partition S into exactly K non-empty [subsequences] S_{1}, S_{2}, \ldots, S_{K}. Note that every character of S must be present in exactly one of the S_{i}.
Then, create S' as the concatenation of these subsequences, i.e, S' = S_{1} + S_{2} + \ldots + S_{K}, where + denotes string concatenation.
Determine the lexicographically smallest S' that can be created.
------ 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 and K.
- The second line of each test case contains the string S.
------ Output Format ------
For each test case, output on a new line the lexicographically smallest string S' that can be created.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{5}$
$1 ≤ K ≤ N$
$S$ contains only lowercase Latin letters.
- Sum of $N$ over all cases won't exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
6 1
whizzy
14 2
aaacdecdebacde
4 3
dbca
----- Sample Output 1 ------
whizzy
aaaaccdecdebde
abcd
----- explanation 1 ------
Test Case $1$: $K = 1$, so our only option is to set $S' = S_{1} = whizzy$.
Test Case $2$: Partition $S = \textcolor{red}{aaa}\textcolor{blue}{cdecdeb}\textcolor{red}{ac}\textcolor{blue}{de}$ into $S_{1} = \textcolor{red}{aaaac}$ and $S_{2} = \textcolor{blue}{cdecdebde}$, to form $S' = aaaaccdecdebde$.
Test Case $3$: Partition $S = d\textcolor{blue}{bc}\textcolor{red}{a}$ into $S_{1} = \textcolor{red}{a}$, $S_{2} = \textcolor{blue}{bc}$, and $S_{3} = d$ to form $S' = abcd$.
In both test cases $2$ and $3$, it can be shown that no other partition gives a lexicographically smaller $S'$.
|
{"inputs": ["3\n6 1\nwhizzy\n14 2\naaacdecdebacde\n4 3\ndbca\n"], "outputs": ["whizzy\naaaaccdecdebde\nabcd"]}
| 598
| 49
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problems statements in Mandarin Chinese and Russian.
Chef likes to write poetry. Today, he has decided to write a X pages long poetry, but unfortunately his notebook has only Y pages left in it. Thus he decided to buy a new CHEFMATE notebook and went to the stationary shop. Shopkeeper showed him some N notebooks, where the number of pages and price of the i^{th} one are P_{i} pages and C_{i} rubles respectively. Chef has spent some money preparing for Ksen's birthday, and then he has only K rubles left for now.
Chef wants to buy a single notebook such that the price of the notebook should not exceed his budget and he is able to complete his poetry.
Help Chef accomplishing this task. You just need to tell him whether he can buy such a notebook or not. Note that Chef can use all of the Y pages in the current notebook, and Chef can buy only one notebook because Chef doesn't want to use many notebooks.
------ Input ------
The first line of input contains an integer T, denoting the number of test cases. Then T test cases are follow.
The first line of each test case contains four space-separated integers X, Y, K and N, described in the statement. The i^{th} line of the next N lines contains two space-separated integers P_{i} and C_{i}, denoting the number of pages and price of the i^{th} notebook respectively.
------ Output ------
For each test case, Print "LuckyChef" if Chef can select such a notebook, otherwise print "UnluckyChef" (quotes for clarity).
------
------ Constraints -----
$1 ≤ T ≤ 10^{5}$
$1 ≤ Y < X ≤ 10^{3}$
$1 ≤ K ≤ 10^{3}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ P_{i}, C_{i} ≤ 10^{3}$
Subtask 1: 40 points
$Sum of N over all test cases in one test file does not exceed 10^{4}.$
Subtask 2: 60 points
$Sum of N over all test cases in one test file does not exceed 10^{6}.$
----- Sample Input 1 ------
3
3 1 2 2
3 4
2 2
3 1 2 2
2 3
2 3
3 1 2 2
1 1
1 2
----- Sample Output 1 ------
LuckyChef
UnluckyChef
UnluckyChef
----- explanation 1 ------
Example case 1. In this case, Chef wants to write X = 3 pages long poetry, but his notebook has only Y = 1 page. And his budget is K = 2 rubles, and there are N = 2 notebooks in the shop. The first notebook has P1 = 3 pages, but Chef cannot buy it, because its price is C1 = 4 rubles. The second notebook has P2 = 2 pages, and its price is C2 = 2 rubles. Thus Chef can select the second notebook to accomplish the task. He will write 1 page of poetry in the old notebook, and 2 page of poetry in the new notebook.
Example case 2. Chef cannot buy any notebook, because the prices exceed the Chef's budget.
Example case 3. No notebook contains sufficient number of pages required to write poetry.
|
{"inputs": ["3\n3 1 2 2\n3 4\n2 2 \n3 1 2 2\n2 3\n2 3 \n3 1 2 2\n1 1\n1 2"], "outputs": ["LuckyChef\nUnluckyChef\nUnluckyChef"]}
| 753
| 76
|
coding
|
Solve the programming task below in a Python markdown code block.
It has been decided that a programming contest sponsored by company A will be held, so we will post the notice on a bulletin board.
The bulletin board is in the form of a grid with N rows and N columns, and the notice will occupy a rectangular region with H rows and W columns.
How many ways are there to choose where to put the notice so that it completely covers exactly HW squares?
Constraints
* 1 \leq H, W \leq N \leq 100
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
H
W
Output
Print the answer.
Examples
Input
3
2
3
Output
2
Input
100
1
1
Output
10000
Input
5
4
2
Output
8
|
{"inputs": ["3\n4\n3", "3\n3\n3", "3\n3\n1", "3\n2\n1", "3\n0\n1", "3\n0\n2", "3\n1\n1", "3\n2\n2"], "outputs": ["0\n", "1\n", "3\n", "6\n", "12\n", "8\n", "9\n", "4\n"]}
| 192
| 95
|
coding
|
Solve the programming task below in a Python markdown code block.
This is now a little serie :)
Funny Dots
You will get two Integer n (width) and m (height) and your task is to draw following pattern. Each line is seperated with '\n'.
Both integers are equal or greater than 1. No need to check for invalid parameters.
e.g.:
+---+---+---+
+---+ | o | o | o |
dot(1,1) => | o | dot(3,2) => +---+---+---+
+---+ | o | o | o |
+---+---+---+
Serie: ASCII Fun
ASCII Fun #1: X-Shape
ASCII Fun #2: Funny Dots
ASCII Fun #3: Puzzle Tiles
ASCII Fun #4: Build a pyramid
Also feel free to reuse/extend the following starter code:
```python
def dot(n,m):
```
|
{"functional": "_inputs = [[1, 1], [3, 2]]\n_outputs = [['+---+\\n| o |\\n+---+'], ['+---+---+---+\\n| o | o | o |\\n+---+---+---+\\n| o | o | o |\\n+---+---+---+']]\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(dot(*i), o[0])"}
| 207
| 219
|
coding
|
Solve the programming task below in a Python markdown code block.
Snuke has decided to construct a string that starts with A and ends with Z, by taking out a substring of a string s (that is, a consecutive part of s).
Find the greatest length of the string Snuke can construct. Here, the test set guarantees that there always exists a substring of s that starts with A and ends with Z.
-----Constraints-----
- 1 ≦ |s| ≦ 200{,}000
- s consists of uppercase English letters.
- There exists a substring of s that starts with A and ends with Z.
-----Input-----
The input is given from Standard Input in the following format:
s
-----Output-----
Print the answer.
-----Sample Input-----
QWERTYASDFZXCV
-----Sample Output-----
5
By taking out the seventh through eleventh characters, it is possible to construct ASDFZ, which starts with A and ends with Z.
|
{"inputs": ["ZAACZ", "ZCAAZ", "ZCAZB", "ZCABZ", "BZACZ", "ZABCZ", "ZABCZ\n", "QWERTAFSDXZXCV"], "outputs": ["4\n", "3\n", "2\n", "3\n", "3\n", "4", "4\n", "6\n"]}
| 205
| 86
|
coding
|
Solve the programming task below in a Python markdown code block.
There are $n$ students in a school class, the rating of the $i$-th student on Codehorses is $a_i$. You have to form a team consisting of $k$ students ($1 \le k \le n$) such that the ratings of all team members are distinct.
If it is impossible to form a suitable team, print "NO" (without quotes). Otherwise print "YES", and then print $k$ distinct numbers which should be the indices of students in the team you form. If there are multiple answers, print any of them.
-----Input-----
The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 100$) — the number of students and the size of the team you have to form.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 100$), where $a_i$ is the rating of $i$-th student.
-----Output-----
If it is impossible to form a suitable team, print "NO" (without quotes). Otherwise print "YES", and then print $k$ distinct integers from $1$ to $n$ which should be the indices of students in the team you form. All the ratings of the students in the team should be distinct. You may print the indices in any order. If there are multiple answers, print any of them.
Assume that the students are numbered from $1$ to $n$.
-----Examples-----
Input
5 3
15 13 15 15 12
Output
YES
1 2 5
Input
5 4
15 13 15 15 12
Output
NO
Input
4 4
20 10 40 30
Output
YES
1 2 3 4
-----Note-----
All possible answers for the first example: {1 2 5} {2 3 5} {2 4 5}
Note that the order does not matter.
|
{"inputs": ["1 1\n1\n", "1 1\n1\n", "1 1\n2\n", "2 2\n100 9\n", "2 2\n100 99\n", "2 2\n100 99\n", "2 2\n100 27\n", "2 2\n100 100\n"], "outputs": ["YES\n1 \n", "YES\n1", "YES\n1 ", "YES\n1 2 ", "YES\n1 2 \n", "YES\n1 2", "YES\n1 2 ", "NO\n"]}
| 469
| 146
|
coding
|
Solve the programming task below in a Python markdown code block.
"Everything in the universe is balanced. Every disappointment you face in life will be balanced by something good for you! Keep going, never give up."
Let's call a string balanced if all characters that occur in this string occur in it the same number of times.
You are given a string $S$; this string may only contain uppercase English letters. You may perform the following operation any number of times (including zero): choose one letter in $S$ and replace it by another uppercase English letter. Note that even if the replaced letter occurs in $S$ multiple times, only the chosen occurrence of this letter is replaced.
Find the minimum number of operations required to convert the given string to a balanced string.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first and only line of each test case contains a single string $S$.
-----Output-----
For each test case, print a single line containing one integer ― the minimum number of operations.
-----Constraints-----
- $1 \le T \le 10,000$
- $1 \le |S| \le 1,000,000$
- the sum of $|S|$ over all test cases does not exceed $5,000,000$
- $S$ contains only uppercase English letters
-----Subtasks-----
Subtask #1 (20 points):
- $T \le 10$
- $|S| \le 18$
Subtask #2 (80 points): original constraints
-----Example Input-----
2
ABCB
BBC
-----Example Output-----
1
1
-----Explanation-----
Example case 1: We can change 'C' to 'A'. The resulting string is "ABAB", which is a balanced string, since the number of occurrences of 'A' is equal to the number of occurrences of 'B'.
Example case 2: We can change 'C' to 'B' to make the string "BBB", which is a balanced string.
|
{"inputs": ["2\nABCB\nBBC"], "outputs": ["1\n1"]}
| 452
| 19
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a matrix consisting of digits zero and one, its size is n × m. You are allowed to rearrange its rows. What is the maximum area of the submatrix that only consists of ones and can be obtained in the given problem by the described operations?
Let's assume that the rows of matrix a are numbered from 1 to n from top to bottom and the columns are numbered from 1 to m from left to right. A matrix cell on the intersection of the i-th row and the j-th column can be represented as (i, j). Formally, a submatrix of matrix a is a group of four integers d, u, l, r (1 ≤ d ≤ u ≤ n; 1 ≤ l ≤ r ≤ m). We will assume that the submatrix contains cells (i, j) (d ≤ i ≤ u; l ≤ j ≤ r). The area of the submatrix is the number of cells it contains.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 5000). Next n lines contain m characters each — matrix a. Matrix a only contains characters: "0" and "1". Note that the elements of the matrix follow without any spaces in the lines.
Output
Print a single integer — the area of the maximum obtained submatrix. If we cannot obtain a matrix of numbers one, print 0.
Examples
Input
1 1
1
Output
1
Input
2 2
10
11
Output
2
Input
4 3
100
011
000
101
Output
2
|
{"inputs": ["1 1\n0\n", "1 1\n1\n", "2 2\n10\n11\n", "4 3\n101\n011\n000\n101\n", "4 3\n101\n011\n101\n101\n", "4 3\n101\n011\n100\n101\n", "4 3\n100\n111\n000\n101\n", "4 3\n100\n011\n000\n101\n"], "outputs": ["0\n", "1\n", "2\n", "3\n", "4\n", "3\n", "3\n", "2\n"]}
| 357
| 176
|
coding
|
Solve the programming task below in a Python markdown code block.
One day Nikita found the string containing letters "a" and "b" only.
Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b".
Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get?
-----Input-----
The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b".
-----Output-----
Print a single integer — the maximum possible size of beautiful string Nikita can get.
-----Examples-----
Input
abba
Output
4
Input
bab
Output
2
-----Note-----
It the first sample the string is already beautiful.
In the second sample he needs to delete one of "b" to make it beautiful.
|
{"inputs": ["a\n", "b\n", "a\n", "b\n", "ab\n", "ba\n", "bb\n", "aa\n"], "outputs": ["1", "1", "1\n", "1\n", "2", "2", "2", "2"]}
| 236
| 64
|
coding
|
Solve the programming task below in a Python markdown code block.
You have N cards. On the i-th card, an integer A_i is written.
For each j = 1, 2, ..., M in this order, you will perform the following operation once:
Operation: Choose at most B_j cards (possibly zero). Replace the integer written on each chosen card with C_j.
Find the maximum possible sum of the integers written on the N cards after the M operations.
-----Constraints-----
- All values in input are integers.
- 1 \leq N \leq 10^5
- 1 \leq M \leq 10^5
- 1 \leq A_i, C_i \leq 10^9
- 1 \leq B_i \leq N
-----Input-----
Input is given from Standard Input in the following format:
N M
A_1 A_2 ... A_N
B_1 C_1
B_2 C_2
\vdots
B_M C_M
-----Output-----
Print the maximum possible sum of the integers written on the N cards after the M operations.
-----Sample Input-----
3 2
5 1 4
2 3
1 5
-----Sample Output-----
14
By replacing the integer on the second card with 5, the sum of the integers written on the three cards becomes 5 + 5 + 4 = 14, which is the maximum result.
|
{"inputs": ["3 2\n5 1 1\n2 3\n1 5", "3 2\n5 1 1\n2 3\n1 7", "3 2\n5 1 1\n2 3\n1 6", "3 2\n5 1 1\n2 3\n2 6", "3 2\n5 1 1\n2 3\n1 0", "3 2\n5 1 1\n1 2\n1 5", "3 2\n5 0 0\n1 6\n1 5", "3 2\n5 1 1\n0 3\n2 7"], "outputs": ["13\n", "15\n", "14\n", "17\n", "11\n", "12\n", "16\n", "19\n"]}
| 315
| 198
|
coding
|
Solve the programming task below in a Python markdown code block.
Lately, a national version of a bingo game has become very popular in Berland. There are n players playing the game, each player has a card with numbers. The numbers on each card are distinct, but distinct cards can have equal numbers. The card of the i-th player contains m_{i} numbers.
During the game the host takes numbered balls one by one from a bag. He reads the number aloud in a high and clear voice and then puts the ball away. All participants cross out the number if it occurs on their cards. The person who crosses out all numbers from his card first, wins. If multiple people cross out all numbers from their cards at the same time, there are no winners in the game. At the beginning of the game the bag contains 100 balls numbered 1 through 100, the numbers of all balls are distinct.
You are given the cards for each player. Write a program that determines whether a player can win the game at the most favorable for him scenario or not.
-----Input-----
The first line of the input contains integer n (1 ≤ n ≤ 100) — the number of the players. Then follow n lines, each line describes a player's card. The line that describes a card starts from integer m_{i} (1 ≤ m_{i} ≤ 100) that shows how many numbers the i-th player's card has. Then follows a sequence of integers a_{i}, 1, a_{i}, 2, ..., a_{i}, m_{i} (1 ≤ a_{i}, k ≤ 100) — the numbers on the i-th player's card. The numbers in the lines are separated by single spaces.
It is guaranteed that all the numbers on each card are distinct.
-----Output-----
Print n lines, the i-th line must contain word "YES" (without the quotes), if the i-th player can win, and "NO" (without the quotes) otherwise.
-----Examples-----
Input
3
1 1
3 2 4 1
2 10 11
Output
YES
NO
YES
Input
2
1 1
1 1
Output
NO
NO
|
{"inputs": ["1\n1 1\n", "1\n1 1\n", "1\n1 2\n", "2\n1 1\n1 1\n", "2\n1 2\n1 3\n", "2\n1 2\n1 3\n", "2\n1 4\n1 3\n", "2\n1 2\n1 1\n"], "outputs": ["YES\n", "YES\n", "YES\n", "NO\nNO\n", "YES\nYES\n", "YES\nYES\n", "YES\nYES\n", "YES\nYES\n"]}
| 474
| 132
|
coding
|
Solve the programming task below in a Python markdown code block.
Alice is teaching Bob maths via a game called N-guesser.
Alice has a positive integer N which Bob needs to guess. She gives him two pieces of information with which to do this:
A positive integer X, which denotes the sum of divisors of N.
Two positive integers A and B, which denote that the sum of reciprocals of divisors of N is A/B.
Bob either needs to guess N or tell that no such number exists.
It can be shown that if a valid N exists, it is unique.
------ 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.
- Each test case consists of a single line of input, containing three space-separated integers X, A, B.
------ Output Format ------
For each test case, output a new line containing the answer — Alice's number N, or -1 if no such number exists.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ X ≤ 10^{9}$
$1 ≤ A, B ≤ 10^{9}$
$\gcd(A, B) = 1$
----- Sample Input 1 ------
2
4 4 3
4 1 1
----- Sample Output 1 ------
3
-1
----- explanation 1 ------
Test case $1$: The divisors of $3$ are $1$ and $3$. Their sum is $4$ and the sum of their reciprocals is $4/3$.
Test case $2$: It can be proved that no positive integer $N$ exists whose divisors sum to $4$ and reciprocals of divisors sum to $1$.
|
{"inputs": ["2\n4 4 3\n4 1 1\n"], "outputs": ["3\n-1"]}
| 377
| 28
|
coding
|
Solve the programming task below in a Python markdown code block.
Given a sequence A of length N, the weight of this sequence is defined as
\text{weight}(A) = \sum_{i = 1}^{N} i\cdot A_{i}
Given an integer N, we pick a permutation A of [1, 2 \dots N] uniformly at random. What is the expected weight of this permutation?
It can be shown that the answer can be represented as \displaystyle \frac{P}{Q} where P and Q are coprime integers and Q \neq 0 \pmod{998244353}. Print the value of P \cdot Q^{-1} modulo 998244353.
------ Input Format ------
- First line of the input contains T, the number of test cases. Then the test cases follow.
- Each test case contains a single integer on a single line N.
------ Output Format ------
For each test case, print a single integer, the answer to the test case.
------ Constraints ------
$1 ≤ T ≤ 2 \cdot 10^{5}$
$1 ≤ N ≤ 10^{9}$
------ subtasks ------
Subtask 1 (100 points): Original constraints
----- Sample Input 1 ------
3
1
2
3
----- Sample Output 1 ------
1
499122181
12
----- explanation 1 ------
- Test case $1$: There is only one permutation of $[1]$ which is $[1]$ and the weight of this permutation is equal to $1$. So, the expected weight is equal to $1$.
- Test case $2$: There are $2$ permutations of $[1, 2]$ namely
- $[1, 2]$ with weight equal to $5$.
- $[2, 1]$ with weight equal to $4$.
So, the expected weight is equal to $\frac{5 + 4}{2!} = \frac{9}{2}$.
- Test case $3:$
There are $6$ permutations of $[1, 2, 3]$ namely
- $[1, 2, 3]$ with weight equal to $14$.
- $[1, 3, 2]$ with weight equal to $13$.
- $[2, 1, 3]$ with weight equal to $13$.
- $[2, 3, 1]$ with weight equal to $11$.
- $[3, 1, 2]$ with weight equal to $11$.
- $[3, 2, 1]$ with weight equal to $10$.
So, the expected weight is equal to $\frac{14 + 13 + 13 + 11 + 11 + 10}{3!} = 12$.
|
{"inputs": ["3\n1\n2\n3\n"], "outputs": ["1\n499122181\n12\n"]}
| 634
| 33
|
coding
|
Solve the programming task below in a Python markdown code block.
Many years have passed, and n friends met at a party again. Technologies have leaped forward since the last meeting, cameras with timer appeared and now it is not obligatory for one of the friends to stand with a camera, and, thus, being absent on the photo.
Simply speaking, the process of photographing can be described as follows. Each friend occupies a rectangle of pixels on the photo: the i-th of them in a standing state occupies a w_{i} pixels wide and a h_{i} pixels high rectangle. But also, each person can lie down for the photo, and then he will occupy a h_{i} pixels wide and a w_{i} pixels high rectangle.
The total photo will have size W × H, where W is the total width of all the people rectangles, and H is the maximum of the heights. The friends want to determine what minimum area the group photo can they obtain if no more than n / 2 of them can lie on the ground (it would be strange if more than n / 2 gentlemen lie on the ground together, isn't it?..)
Help them to achieve this goal.
-----Input-----
The first line contains integer n (1 ≤ n ≤ 1000) — the number of friends.
The next n lines have two integers w_{i}, h_{i} (1 ≤ w_{i}, h_{i} ≤ 1000) each, representing the size of the rectangle, corresponding to the i-th friend.
-----Output-----
Print a single integer equal to the minimum possible area of the photo containing all friends if no more than n / 2 of them can lie on the ground.
-----Examples-----
Input
3
10 1
20 2
30 3
Output
180
Input
3
3 1
2 2
4 3
Output
21
Input
1
5 10
Output
50
|
{"inputs": ["1\n1 1\n", "1\n1 1\n", "1\n1 1\n", "1\n2 7\n", "1\n0 1\n", "1\n5 10\n", "1\n2 10\n", "1\n5 10\n"], "outputs": ["1\n", "1\n", "1\n", "14\n", "0\n", "50\n", "20\n", "50\n"]}
| 418
| 109
|
coding
|
Solve the programming task below in a Python markdown code block.
As you know, the girl Dora is always looking for something. This time she was given a permutation, and she wants to find such a subsegment of it that none of the elements at its ends is either the minimum or the maximum of the entire subsegment. More formally, you are asked to find the numbers $l$ and $r$ $(1 \leq l \leq r \leq n)$ such that $a_l \neq \min(a_l, a_{l + 1}, \ldots, a_r)$, $a_l \neq \max(a_l, a_{l + 1}, \ldots, a_r)$ and $a_r \neq \min(a_l, a_{l + 1}, \ldots, a_r)$, $a_r \neq \max(a_l, a_{l + 1}, \ldots, a_r)$.
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in any order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ occurs twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$, but $4$ is present in the array).
Help Dora find such a subsegment, or tell her that such a subsegment does not exist.
-----Input-----
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of the test cases follows.
For each test case, the first line contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the length of permutation.
The second line contains $n$ distinct integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the elements of permutation.
It is guarented that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output $-1$ if the desired subsegment does not exist.
Otherwise, output two indexes $l, r$ such that $[a_{l}, a_{l + 1}, \ldots, a_{r}]$ satisfies all conditions.
If there are several solutions, then output any of them.
-----Examples-----
Input
4
3
1 2 3
4
2 1 4 3
7
1 3 2 4 6 5 7
6
2 3 6 5 4 1
Output
-1
1 4
2 6
-1
-----Note-----
In the first and fourth test cases, it can be shown that there are no desired subsegments.
In the second test case, the subsegment $[1, 4]$ satisfies all the conditions, because $\max(a_1, a_2, a_3, a_4) = 4, \min(a_1, a_2, a_3, a_4) = 1$, as we see, all the conditions are met.
In the third test case, the subsegment $[2, 6]$ also satisfies all the conditions described.
|
{"inputs": ["4\n3\n1 2 3\n4\n2 1 4 3\n7\n1 3 2 4 6 5 7\n6\n2 3 6 5 4 1\n"], "outputs": ["-1\n1 4\n2 6\n-1\n"]}
| 755
| 74
|
coding
|
Solve the programming task below in a Python markdown code block.
You are organizing a boxing tournament, where $n$ boxers will participate ($n$ is a power of $2$), and your friend is one of them. All boxers have different strength from $1$ to $n$, and boxer $i$ wins in the match against boxer $j$ if and only if $i$ is stronger than $j$.
The tournament will be organized as follows: $n$ boxers will be divided into pairs; the loser in each pair leaves the tournament, and $\frac{n}{2}$ winners advance to the next stage, where they are divided into pairs again, and the winners in all pairs advance to the next stage, and so on, until only one boxer remains (who is declared the winner).
Your friend really wants to win the tournament, but he may be not the strongest boxer. To help your friend win the tournament, you may bribe his opponents: if your friend is fighting with a boxer you have bribed, your friend wins even if his strength is lower.
Furthermore, during each stage you distribute the boxers into pairs as you wish.
The boxer with strength $i$ can be bribed if you pay him $a_i$ dollars. What is the minimum number of dollars you have to spend to make your friend win the tournament, provided that you arrange the boxers into pairs during each stage as you wish?
-----Input-----
The first line contains one integer $n$ ($2 \le n \le 2^{18}$) — the number of boxers. $n$ is a power of $2$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$, where $a_i$ is the number of dollars you have to pay if you want to bribe the boxer with strength $i$. Exactly one of $a_i$ is equal to $-1$ — it means that the boxer with strength $i$ is your friend. All other values are in the range $[1, 10^9]$.
-----Output-----
Print one integer — the minimum number of dollars you have to pay so your friend wins.
-----Examples-----
Input
4
3 9 1 -1
Output
0
Input
8
11 -1 13 19 24 7 17 5
Output
12
-----Note-----
In the first test case no matter how you will distribute boxers into pairs, your friend is the strongest boxer and anyway wins the tournament.
In the second test case you can distribute boxers as follows (your friend is number $2$):
$1 : 2, 8 : 5, 7 : 3, 6 : 4$ (boxers $2, 8, 7$ and $6$ advance to the next stage);
$2 : 6, 8 : 7$ (boxers $2$ and $8$ advance to the next stage, you have to bribe the boxer with strength $6$);
$2 : 8$ (you have to bribe the boxer with strength $8$);
|
{"inputs": ["4\n3 9 1 -1\n", "4\n1 -1 1 1\n", "4\n1 -1 1 1\n", "4\n3 9 0 -1\n", "4\n0 9 0 -1\n", "4\n3 9 1 -1\n", "2\n-1 1000000000\n", "2\n1000000000 -1\n"], "outputs": ["0", "1", "1\n", "0\n", "0\n", "0", "1000000000", "0"]}
| 665
| 149
|
coding
|
Solve the programming task below in a Python markdown code block.
Mike has n strings s1, s2, ..., sn each consisting of lowercase English letters. In one move he can choose a string si, erase the first character and append it to the end of the string. For example, if he has the string "coolmike", in one move he can transform it into the string "oolmikec".
Now Mike asks himself: what is minimal number of moves that he needs to do in order to make all the strings equal?
Input
The first line contains integer n (1 ≤ n ≤ 50) — the number of strings.
This is followed by n lines which contain a string each. The i-th line corresponding to string si. Lengths of strings are equal. Lengths of each string is positive and don't exceed 50.
Output
Print the minimal number of moves Mike needs in order to make all the strings equal or print - 1 if there is no solution.
Examples
Input
4
xzzwo
zwoxz
zzwox
xzzwo
Output
5
Input
2
molzv
lzvmo
Output
2
Input
3
kc
kc
kc
Output
0
Input
3
aa
aa
ab
Output
-1
Note
In the first sample testcase the optimal scenario is to perform operations in such a way as to transform all strings into "zwoxz".
|
{"inputs": ["1\na\n", "1\nb\n", "1\n`\n", "2\na\nb\n", "2\nb\nb\n", "2\nb\na\n", "2\nc\na\n", "2\naa\naa\n"], "outputs": ["0\n", "0\n", "0\n", "-1\n", "0\n", "-1\n", "-1\n", "0\n"]}
| 306
| 96
|
coding
|
Solve the programming task below in a Python markdown code block.
Your wizard cousin works at a Quidditch stadium and wants you to write a function that calculates the points for the Quidditch scoreboard!
# Story
Quidditch is a sport with two teams. The teams score goals by throwing the Quaffle through a hoop, each goal is worth **10 points**.
The referee also deducts 30 points (**- 30 points**) from the team who are guilty of carrying out any of these fouls: Blatching, Blurting, Bumphing, Haverstacking, Quaffle-pocking, Stooging
The match is concluded when the Snitch is caught, and catching the Snitch is worth **150 points**. Let's say a Quaffle goes through the hoop just seconds after the Snitch is caught, in that case the points of that goal should not end up on the scoreboard seeing as the match is already concluded.
You don't need any prior knowledge of how Quidditch works in order to complete this kata, but if you want to read up on what it is, here's a link: https://en.wikipedia.org/wiki/Quidditch
# Task
You will be given a string with two arguments, the first argument will tell you which teams are playing and the second argument tells you what's happened in the match. Calculate the points and return a string containing the teams final scores, with the team names sorted in the same order as in the first argument.
# Examples:
# Given an input of:
# The expected output would be:
Separate the team names from their respective points with a colon and separate the two teams with a comma.
Good luck!
Also feel free to reuse/extend the following starter code:
```python
def quidditch_scoreboard(teams, actions):
```
|
{"functional": "_inputs = [['Appleby Arrows vs Montrose Magpies', 'Montrose Magpies: Quaffle goal, Montrose Magpies: Quaffle goal, Appleby Arrows: Quaffle goal, Appleby Arrows: Quaffle goal, Montrose Magpies: Haverstacking foul, Appleby Arrows: Quaffle goal, Appleby Arrows: Quaffle goal, Appleby Arrows: Quaffle goal, Appleby Arrows: Quaffle goal, Montrose Magpies: Caught Snitch'], ['Kenmare Kestrels vs Barnton', 'Barnton: Quaffle goal, Kenmare Kestrels: Quaffle goal, Barnton: Quaffle goal, Barnton: Quaffle goal, Barnton: Quaffle goal, Barnton: Quaffle goal, Kenmare Kestrels: Blurting foul, Barnton: Quaffle goal, Barnton: Quaffle goal, Barnton: Quaffle goal, Barnton: Quaffle goal, Barnton: Quaffle goal, Kenmare Kestrels: Caught Snitch'], ['Puddlemere United vs Holyhead Harpies', 'Puddlemere United: Quaffle goal, Holyhead Harpies: Quaffle goal, Holyhead Harpies: Quaffle goal, Puddlemere United: Quaffle goal, Puddlemere United: Quaffle goal, Puddlemere United: Bumphing foul, Holyhead Harpies: Quaffle goal, Holyhead Harpies: Quaffle goal, Puddlemere United: Caught Snitch'], ['Pride of Portree vs Banchory Bangers', 'Pride of Portree: Quaffle goal, Pride of Portree: Caught Snitch'], ['Chudley Cannons vs Tutshill Tornados', 'Chudley Cannons: Blatching foul, Tutshill Tornados: Quaffle goal, Tutshill Tornados: Quaffle goal, Tutshill Tornados: Quaffle goal, Tutshill Tornados: Quaffle goal, Tutshill Tornados: Quaffle goal, Tutshill Tornados: Quaffle goal, Tutshill Tornados: Caught Snitch'], ['Wimbourne Wasps vs Cork', 'Cork: Quaffle goal, Cork: Quaffle-pocking foul, Cork: Quaffle goal, Wimbourne Wasps: Quaffle goal, Cork: Quaffle goal, Wimbourne Wasps: Quaffle goal, Wimbourne Wasps: Quaffle goal, Wimbourne Wasps: Quaffle goal, Cork: Quaffle goal, Wimbourne Wasps: Quaffle goal, Cork: Caught Snitch, Wimbourne Wasps: Quaffle goal'], ['Lancashire vs Ballycastle Bats', 'Lancashire: Quaffle goal, Lancashire: Stooging foul, Lancashire: Quaffle goal, Lancashire: Quaffle goal, Lancashire: Quaffle goal, Lancashire: Quaffle goal, Ballycastle Bats: Quaffle goal, Ballycastle Bats: Quaffle goal, Lancashire: Quaffle goal, Ballycastle Bats: Quaffle goal, Ballycastle Bats: Quaffle goal, Ballycastle Bats: Quaffle goal, Ballycastle Bats: Quaffle goal, Ballycastle Bats: Quaffle goal, Ballycastle Bats: Quaffle goal, Ballycastle Bats: Quaffle goal, Lancashire: Caught Snitch, Ballycastle Bats: Blurting foul'], ['Caerphilly Catapults vs Wigtown Wanderers', 'Caerphilly Catapults: Quaffle goal, Caerphilly Catapults: Quaffle goal, Caerphilly Catapults: Quaffle goal, Caerphilly Catapults: Quaffle goal, Caerphilly Catapults: Quaffle goal, Caerphilly Catapults: Quaffle goal, Wigtown Wanderers: Quaffle goal, Caerphilly Catapults: Quaffle goal, Caerphilly Catapults: Quaffle goal, Caerphilly Catapults: Quaffle goal, Caerphilly Catapults: Quaffle goal, Caerphilly Catapults: Quaffle goal, Caerphilly Catapults: Quaffle goal, Caerphilly Catapults: Quaffle goal, Caerphilly Catapults: Quaffle goal, Caerphilly Catapults: Quaffle goal, Caerphilly Catapults: Quaffle goal, Caerphilly Catapults: Quaffle goal, Wigtown Wanderers: Caught Snitch']]\n_outputs = [['Appleby Arrows: 60, Montrose Magpies: 140'], ['Kenmare Kestrels: 130, Barnton: 100'], ['Puddlemere United: 150, Holyhead Harpies: 40'], ['Pride of Portree: 160, Banchory Bangers: 0'], ['Chudley Cannons: -30, Tutshill Tornados: 210'], ['Wimbourne Wasps: 50, Cork: 160'], ['Lancashire: 180, Ballycastle Bats: 90'], ['Caerphilly Catapults: 170, Wigtown Wanderers: 160']]\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(quidditch_scoreboard(*i), o[0])"}
| 382
| 1,288
|
coding
|
Solve the programming task below in a Python markdown code block.
Bessie the cow has just intercepted a text that Farmer John sent to Burger Queen! However, Bessie is sure that there is a secret message hidden inside.
The text is a string $s$ of lowercase Latin letters. She considers a string $t$ as hidden in string $s$ if $t$ exists as a subsequence of $s$ whose indices form an arithmetic progression. For example, the string aab is hidden in string aaabb because it occurs at indices $1$, $3$, and $5$, which form an arithmetic progression with a common difference of $2$. Bessie thinks that any hidden string that occurs the most times is the secret message. Two occurrences of a subsequence of $S$ are distinct if the sets of indices are different. Help her find the number of occurrences of the secret message!
For example, in the string aaabb, a is hidden $3$ times, b is hidden $2$ times, ab is hidden $6$ times, aa is hidden $3$ times, bb is hidden $1$ time, aab is hidden $2$ times, aaa is hidden $1$ time, abb is hidden $1$ time, aaab is hidden $1$ time, aabb is hidden $1$ time, and aaabb is hidden $1$ time. The number of occurrences of the secret message is $6$.
-----Input-----
The first line contains a string $s$ of lowercase Latin letters ($1 \le |s| \le 10^5$) — the text that Bessie intercepted.
-----Output-----
Output a single integer — the number of occurrences of the secret message.
-----Examples-----
Input
aaabb
Output
6
Input
usaco
Output
1
Input
lol
Output
2
-----Note-----
In the first example, these are all the hidden strings and their indice sets: a occurs at $(1)$, $(2)$, $(3)$ b occurs at $(4)$, $(5)$ ab occurs at $(1,4)$, $(1,5)$, $(2,4)$, $(2,5)$, $(3,4)$, $(3,5)$ aa occurs at $(1,2)$, $(1,3)$, $(2,3)$ bb occurs at $(4,5)$ aab occurs at $(1,3,5)$, $(2,3,4)$ aaa occurs at $(1,2,3)$ abb occurs at $(3,4,5)$ aaab occurs at $(1,2,3,4)$ aabb occurs at $(2,3,4,5)$ aaabb occurs at $(1,2,3,4,5)$ Note that all the sets of indices are arithmetic progressions.
In the second example, no hidden string occurs more than once.
In the third example, the hidden string is the letter l.
|
{"inputs": ["a\n", "z\n", "z\n", "a\n", "b\n", "c\n", "cc\n", "zy\n"], "outputs": ["1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "2\n", "1\n"]}
| 628
| 70
|
coding
|
Solve the programming task below in a Python markdown code block.
In cryptography, Caesar cipher is one of the simplest and most widely known encryption method. Caesar cipher is a type of substitution cipher in which each letter in the text is replaced by a letter some fixed number of positions down the alphabet. For example, with a shift of 1, 'a' would be replaced by 'b', 'b' would become 'c', 'y' would become 'z', 'z' would become 'a', and so on. In that case, a text:
this is a pen
is would become:
uijt jt b qfo
Write a program which reads a text encrypted by Caesar Chipher and prints the corresponding decoded text. The number of shift is secret and it depends on datasets, but you can assume that the decoded text includes any of the following words: "the", "this", or "that".
Input
Input consists of several datasets. Each dataset consists of texts in a line. Input ends with EOF. The text consists of lower-case letters, periods, space, and end-of-lines. Only the letters have been encrypted. A line consists of at most 80 characters.
You may assume that you can create one decoded text which includes any of "the", "this", or "that" from the given input text.
The number of datasets is less than or equal to 20.
Output
Print decoded texts in a line.
Example
Input
xlmw mw xli tmgxyvi xlex m xsso mr xli xvmt.
Output
this is the picture that i took in the trip.
|
{"inputs": ["xlmw mw xli tmgxyvi xlex m xsso mr ilx xvmt.", "xlmw mw xli tmgxyvi xlex m ossx mr ilx xvmt.", "xlmw mw xli tmgxyvi xlex m ossx mr ilx xwmt.", "xlmw mw xli tmgxyvi xlex m orsx mr ilx xwmt.", "xlmw mw xli tmgxyvi xlex l orsx mr ilx xwmt.", "wmlx mw xli tmgxyvi xlex l orsx mr ilx xwmt.", "wmlx mw xli tmgxyvi xlex l ossx mr ilx xwmt.", "wmlx mw xli tmgxyvi xlex l ossx rm ilx xwmt."], "outputs": ["this is the picture that i took in eht trip.\n", "this is the picture that i koot in eht trip.\n", "this is the picture that i koot in eht tsip.\n", "this is the picture that i knot in eht tsip.\n", "this is the picture that h knot in eht tsip.\n", "siht is the picture that h knot in eht tsip.\n", "siht is the picture that h koot in eht tsip.\n", "siht is the picture that h koot ni eht tsip.\n"]}
| 334
| 319
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a binary string A of length N.
You can perform the following type of operation on the string A:
Choose two different indices i and j (1≤ i,j≤ N);
Change A_{i} and A_{j} to A_{i} \oplus A_{j}. Here \oplus represents the [bitwise XOR] operation.
Find the minimum number of operations required to convert the given string into a [palindrome].
------ Input Format ------
- First line of the input contains T, the number of test cases. Then the test cases follow.
- First line of each test case contains an integer N denoting the size of the string.
- Second line of each test case contains a binary string A of length N containing 0s and 1s only.
------ Output Format ------
For each test case, print the minimum number of operations required to make the string a palindrome.
------ Constraints ------
$1 ≤ T ≤ 1000$
$ 1 ≤ N ≤ 2 \cdot 10^{5}$
- Sum of $N$ over all test cases does not exceeds $ 2 \cdot 10^{5} $.
----- Sample Input 1 ------
4
5
11011
7
0111010
1
1
4
1100
----- Sample Output 1 ------
0
1
0
1
----- explanation 1 ------
Test Case $1$ : The given string $11011$ is already a palindrome. So, no operation is required. The answer for the case is $0$.
Test Case $2$ : The given string $0111010$ is not a palindrome.
- Choose the indices $i=3$ and $j=5$. Now, $A_{3} \oplus A_{5} = 1\oplus 0 = 1$. Thus, we set $A_{3}$ and $A_{5}$ equal to $1$.
After the operation, the resulting string is $0111110$ which is a palindrome. The number of operations required is $1$.
Test Case $3$ : The given string $1$ is already a palindrome. So, no operation is required. The answer for the case is $0$.
Test Case $4$ : The given string $1100$ is not a palindrome.
- Choose the indices $i=1$ and $j=2$. Now, $A_{1} \oplus A_{2} = 1\oplus 1 = 0$. Thus, we set $A_{1}$ and $A_{2}$ equal to $0$.
After the operation, the resulting string is $0000$ which is a palindrome. The number of operations required is $1$.
|
{"inputs": ["4\n5\n11011\n7\n0111010\n1\n1\n4\n1100"], "outputs": ["0\n1\n0\n1"]}
| 619
| 47
|
coding
|
Solve the programming task below in a Python markdown code block.
Jim enters a candy shop which has N different types of candies, each candy is of the same price. Jim has enough money to buy K candies. In how many different ways can he purchase K candies if there are infinite candies of each kind?
Input Format
The first line contains an integer T, the number of tests.
This is followed by 2T lines which contain T tests:
The first line (of each testcase) is an integer N and the second line (of each testcase) is an integer K.
Output Format
For each testcase, print the number of ways Jim can buy candies from the shop in a newline. If the answer has more than 9 digits, print the last 9 digits.
Note
This problem may expect you to have solved nCr Table
Constraints
1 <= T <= 200
1 <= N < 1000
1 <= K < 1000
Sample Input
2
4
1
2
3
Sample Output
4
4
Explanation
There are 2 testcases, for the first testcase we have N = 4 and K = 1, as Jim can buy only 1 candy, he can choose to buy any of the 4 types of candies available. Hence, his answer is 4.
For the 2nd testcase, we have N = 2 and K = 3, If we name two chocolates as a and b, he can buy
aaa bbb aab abb
chocolates, hence 4.
|
{"inputs": ["2\n4\n1\n2\n3\n"], "outputs": ["4\n4\n"]}
| 332
| 24
|
coding
|
Solve the programming task below in a Python markdown code block.
Consider the function p(x), where x is an array of m integers, which returns an array y consisting of m + 1 integers such that y_{i} is equal to the sum of first i elements of array x (0 ≤ i ≤ m).
You have an infinite sequence of arrays A^0, A^1, A^2..., where A^0 is given in the input, and for each i ≥ 1 A^{i} = p(A^{i} - 1). Also you have a positive integer k. You have to find minimum possible i such that A^{i} contains a number which is larger or equal than k.
-----Input-----
The first line contains two integers n and k (2 ≤ n ≤ 200000, 1 ≤ k ≤ 10^18). n is the size of array A^0.
The second line contains n integers A^0_0, A^0_1... A^0_{n} - 1 — the elements of A^0 (0 ≤ A^0_{i} ≤ 10^9). At least two elements of A^0 are positive.
-----Output-----
Print the minimum i such that A^{i} contains a number which is larger or equal than k.
-----Examples-----
Input
2 2
1 1
Output
1
Input
3 6
1 1 1
Output
2
Input
3 1
1 0 1
Output
0
|
{"inputs": ["2 2\n1 1\n", "2 2\n1 1\n", "3 6\n1 1 1\n", "3 1\n1 0 1\n", "3 6\n1 1 1\n", "3 1\n1 0 1\n", "3 1\n1 -1 1\n", "3 1\n2 -1 1\n"], "outputs": ["1\n", "1\n", "2\n", "0\n", "2\n", "0\n", "0\n", "0\n"]}
| 327
| 130
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given two integers $a$ and $b$.
In one move, you can choose some integer $k$ from $1$ to $10$ and add it to $a$ or subtract it from $a$. In other words, you choose an integer $k \in [1; 10]$ and perform $a := a + k$ or $a := a - k$. You may use different values of $k$ in different moves.
Your task is to find the minimum number of moves required to obtain $b$ from $a$.
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) — the number of test cases. Then $t$ test cases follow.
The only line of the test case contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$).
-----Output-----
For each test case, print the answer: the minimum number of moves required to obtain $b$ from $a$.
-----Example-----
Input
6
5 5
13 42
18 4
1337 420
123456789 1000000000
100500 9000
Output
0
3
2
92
87654322
9150
-----Note-----
In the first test case of the example, you don't need to do anything.
In the second test case of the example, the following sequence of moves can be applied: $13 \rightarrow 23 \rightarrow 32 \rightarrow 42$ (add $10$, add $9$, add $10$).
In the third test case of the example, the following sequence of moves can be applied: $18 \rightarrow 10 \rightarrow 4$ (subtract $8$, subtract $6$).
|
{"inputs": ["1\n5 5\n", "1\n5 5\n", "1\n7 5\n", "1\n2 5\n", "1\n2 9\n", "1\n2 6\n", "1\n0 6\n", "1\n12 5\n"], "outputs": ["0\n", "0\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n"]}
| 454
| 103
|
coding
|
Solve the programming task below in a Python markdown code block.
For a finite set of integers X, let f(X)=\max X - \min X.
Given are N integers A_1,...,A_N.
We will choose K of them and let S be the set of the integers chosen. If we distinguish elements with different indices even when their values are the same, there are {}_N C_K ways to make this choice. Find the sum of f(S) over all those ways.
Since the answer can be enormous, print it \bmod (10^9+7).
-----Constraints-----
- 1 \leq N \leq 10^5
- 1 \leq K \leq N
- |A_i| \leq 10^9
-----Input-----
Input is given from Standard Input in the following format:
N K
A_1 ... A_N
-----Output-----
Print the answer \bmod (10^9+7).
-----Sample Input-----
4 2
1 1 3 4
-----Sample Output-----
11
There are six ways to choose S: \{1,1\},\{1,3\},\{1,4\},\{1,3\},\{1,4\}, \{3,4\} (we distinguish the two 1s). The value of f(S) for these choices are 0,2,3,2,3,1, respectively, for the total of 11.
|
{"inputs": ["3 1\n1 1 2", "3 1\n1 1 1", "3 1\n1 1 1\n", "4 2\n1 1 2 4", "4 2\n1 1 1 4", "4 2\n0 1 3 4", "4 2\n0 1 2 4", "4 4\n2 2 1 4"], "outputs": ["0\n", "0", "0\n", "10\n", "9\n", "14\n", "13\n", "3\n"]}
| 323
| 139
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a tree with N vertices.
Here, a tree is a kind of graph, and more specifically, a connected undirected graph with N-1 edges, where N is the number of its vertices.
The i-th edge (1≤i≤N-1) connects Vertices a_i and b_i, and has a length of c_i.
You are also given Q queries and an integer K. In the j-th query (1≤j≤Q):
- find the length of the shortest path from Vertex x_j and Vertex y_j via Vertex K.
-----Constraints-----
- 3≤N≤10^5
- 1≤a_i,b_i≤N (1≤i≤N-1)
- 1≤c_i≤10^9 (1≤i≤N-1)
- The given graph is a tree.
- 1≤Q≤10^5
- 1≤K≤N
- 1≤x_j,y_j≤N (1≤j≤Q)
- x_j≠y_j (1≤j≤Q)
- x_j≠K,y_j≠K (1≤j≤Q)
-----Input-----
Input is given from Standard Input in the following format:
N
a_1 b_1 c_1
:
a_{N-1} b_{N-1} c_{N-1}
Q K
x_1 y_1
:
x_{Q} y_{Q}
-----Output-----
Print the responses to the queries in Q lines.
In the j-th line j(1≤j≤Q), print the response to the j-th query.
-----Sample Input-----
5
1 2 1
1 3 1
2 4 1
3 5 1
3 1
2 4
2 3
4 5
-----Sample Output-----
3
2
4
The shortest paths for the three queries are as follows:
- Query 1: Vertex 2 → Vertex 1 → Vertex 2 → Vertex 4 : Length 1+1+1=3
- Query 2: Vertex 2 → Vertex 1 → Vertex 3 : Length 1+1=2
- Query 3: Vertex 4 → Vertex 2 → Vertex 1 → Vertex 3 → Vertex 5 : Length 1+1+1+1=4
|
{"inputs": ["5\n1 2 1\n1 3 1\n2 4 1\n3 5 2\n3 1\n2 4\n2 3\n4 5", "5\n1 2 1\n1 3 1\n2 4 1\n3 5 1\n3 1\n2 3\n2 3\n4 5", "5\n1 2 1\n1 3 2\n2 4 1\n3 5 1\n3 1\n2 3\n2 3\n4 4", "5\n1 2 1\n2 3 2\n2 4 1\n3 5 1\n3 1\n2 3\n2 3\n4 4", "5\n1 2 2\n1 3 1\n2 4 1\n3 5 1\n3 1\n2 4\n2 3\n4 5", "5\n1 2 1\n1 3 1\n2 4 1\n3 5 1\n3 1\n2 4\n2 3\n4 5", "5\n1 2 1\n1 3 1\n2 4 1\n3 5 1\n3 1\n2 4\n2 3\n4 5\n", "7\n1 2 1\n1 3 3\n1 4 5\n1 5 7\n1 6 9\n1 7 11\n3 2\n1 3\n4 5\n5 7"], "outputs": ["3\n2\n5\n", "2\n2\n4\n", "3\n3\n4\n", "4\n4\n4\n", "5\n3\n5\n", "3\n2\n4", "3\n2\n4\n", "5\n14\n20\n"]}
| 521
| 429
|
coding
|
Solve the programming task below in a Python markdown code block.
Problem Statement
Nathan O. Davis is a student at the department of integrated systems.
Today's agenda in the class is audio signal processing. Nathan was given a lot of homework out. One of the homework was to write a program to process an audio signal. He copied the given audio signal to his USB memory and brought it back to his home.
When he started his homework, he unfortunately dropped the USB memory to the floor. He checked the contents of the USB memory and found that the audio signal data got broken.
There are several characteristics in the audio signal that he copied.
* The audio signal is a sequence of $N$ samples.
* Each sample in the audio signal is numbered from $1$ to $N$ and represented as an integer value.
* Each value of the odd-numbered sample(s) is strictly smaller than the value(s) of its neighboring sample(s).
* Each value of the even-numbered sample(s) is strictly larger than the value(s) of its neighboring sample(s).
He got into a panic and asked you for a help. You tried to recover the audio signal from his USB memory but some samples of the audio signal are broken and could not be recovered. Fortunately, you found from the metadata that all the broken samples have the same integer value.
Your task is to write a program, which takes the broken audio signal extracted from his USB memory as its input, to detect whether the audio signal can be recovered uniquely.
Input
The input consists of multiple datasets. The form of each dataset is described below.
> $N$
> $a_{1}$ $a_{2}$ ... $a_{N}$
The first line of each dataset consists of an integer, $N (2 \le N \le 1{,}000)$. $N$ denotes the number of samples in the given audio signal. The second line of each dataset consists of $N$ values separated by spaces. The $i$-th value, $a_{i}$, is either a character `x` or an integer between $-10^9$ and $10^9$, inclusive. It represents the $i$-th sample of the broken audio signal. If $a_{i}$ is a character `x` , it denotes that $i$-th sample in the audio signal is broken. Otherwise it denotes the value of the $i$-th sample.
The end of input is indicated by a single $0$. This is not included in the datasets.
You may assume that the number of the datasets does not exceed $100$.
Output
For each dataset, output the value of the broken samples in one line if the original audio signal can be recovered uniquely. If there are multiple possible values, output `ambiguous`. If there are no possible values, output `none`.
Sample Input
5
1 x 2 4 x
2
x x
2
1 2
2
2 1
2
1000000000 x
4
x 2 1 x
0
Output for the Sample Input
3
none
ambiguous
none
ambiguous
none
Example
Input
5
1 x 2 4 x
2
x x
2
1 2
2
2 1
2
1000000000 x
4
x 2 1 x
0
Output
3
none
ambiguous
none
ambiguous
none
|
{"inputs": ["5\n0 x 2 4 x\n2\nx x\n2\n1 2\n2\n2 1\n2\n1000000000 x\n4\nx 2 1 x\n0", "5\n1 x 2 0 x\n2\nx x\n2\n1 2\n2\n2 1\n2\n1000000000 x\n4\nx 2 1 x\n0", "5\n0 x 2 4 x\n2\nx x\n2\n1 2\n2\n0 1\n2\n1000000000 x\n4\nx 2 1 x\n0", "5\n0 x 3 4 x\n2\nx x\n2\n1 2\n2\n0 1\n2\n1000000000 x\n4\nx 2 1 x\n0", "5\n0 x 2 4 x\n2\nx x\n2\n1 2\n2\n2 1\n2\n1000000000 x\n4\nx 2 0 x\n0", "5\n1 x 2 4 x\n2\nx x\n2\n1 2\n2\n2 1\n0\n1000000000 x\n4\nx 2 1 x\n0", "5\n0 x 2 4 x\n0\nx x\n2\n1 2\n2\n0 1\n2\n1000000000 x\n4\nx 2 1 x\n0", "5\n1 x 2 1 x\n3\nx x\n2\n1 2\n2\n2 0\n2\n1000000000 x\n4\nx 2 0 x\n0"], "outputs": ["3\nnone\nambiguous\nnone\nambiguous\nnone\n", "none\nnone\nambiguous\nnone\nambiguous\nnone\n", "3\nnone\nambiguous\nambiguous\nambiguous\nnone\n", "none\nnone\nambiguous\nambiguous\nambiguous\nnone\n", "3\nnone\nambiguous\nnone\nambiguous\n1\n", "3\nnone\nambiguous\nnone\n", "3\n", "none\nnone\nambiguous\nnone\nambiguous\n1\n"]}
| 735
| 528
|
coding
|
Solve the programming task below in a Python markdown code block.
Takahashi is about to assemble a character figure, consisting of N parts called Part 1, Part 2, ..., Part N and N-1 connecting components. Parts are distinguishable, but connecting components are not.
Part i has d_i holes, called Hole 1, Hole 2, ..., Hole d_i, into which a connecting component can be inserted. These holes in the parts are distinguishable.
Each connecting component will be inserted into two holes in different parts, connecting these two parts. It is impossible to insert multiple connecting components into a hole.
The character figure is said to be complete when it has the following properties:
- All of the N-1 components are used to connect parts.
- Consider a graph with N vertices corresponding to the parts and N-1 undirected edges corresponding to the pairs of vertices connected by a connecting component. Then, this graph is connected.
Two ways A and B to make the figure complete are considered the same when the following is satisfied: for every pair of holes, A uses a connecting component to connect these holes if and only if B uses one to connect them.
Find the number of ways to make the figure complete. Since the answer can be enormous, find the count modulo 998244353.
-----Constraints-----
- All values in input are integers.
- 2 \leq N \leq 2 \times 10^5
- 1 \leq d_i < 998244353
-----Input-----
Input is given from Standard Input in the following format:
N
d_1 d_2 \cdots d_N
-----Output-----
Print the answer.
-----Sample Input-----
3
1 1 3
-----Sample Output-----
6
One way to make the figure complete is to connect Hole 1 in Part 1 and Hole 3 in Part 3 and then connect Hole 1 in Part 2 and Hole 1 in Part 3.
|
{"inputs": ["2\n1 1\n", "3\n1 1 3\n", "3\n1 1 1\n", "5\n2 1 1 1 3\n", "6\n7 3 5 10 6 4\n", "7\n1 3 3 2 3 1 1\n", "7\n1 5 1 2 5 1 2\n", "8\n6 2 9 9 7 6 2 2\n"], "outputs": ["1\n", "6\n", "0\n", "36\n", "389183858\n", "136080\n", "3024000\n", "621274425\n"]}
| 420
| 181
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problems statements in Mandarin Chinese , Russian and Vietnamese as well.
Akhil comes across a string S of length N. He started wondering about the smallest lexicographical subsequence of string S of length K.
A subsequence of a string is formed by deleting some characters (possibly none) from it's original string.
A string A is said to be lexicographically smaller than the string B of the same length if at the first position where A and B differ, A contains a letter which appears earlier in the dictionary than the corresponding letter in B.
------ Input ------
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows:
First line of each test case will contain string S
Second line of each test case will contain an integer K.
------ Output ------
For each test case, output a single line containing the lexicographically smallest subsequence of S of length K.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ K ≤ N$
$S consists of lowercase English alphabet characters, i.e. from 'a' to 'z'.$
------ Subtasks ------
Subtask #1 (10 points) : 1 ≤ |S| ≤ 100
Subtask #2 (20 points) : 1 ≤ |S| ≤ 10^{3}
Subtask #3 (70 points) : 1 ≤ |S| ≤ 10^{5}
----- Sample Input 1 ------
2
abdc
3
bacb
2
----- Sample Output 1 ------
abc
ab
----- explanation 1 ------
Example case 1. "abc" is the smallest lexicographical subsequence out of ["abd", "bdc", "abc", "adc"].
Example case 2. "ab" is the smallest lexicographical subsequence of length 2.
|
{"inputs": ["2\nabdc\n3\nbacb\n2", "2\nabdc\n3\nbcab\n2", "2\ncdba\n3\nbcab\n2", "2\ncdba\n3\nbcba\n2", "2\ncdba\n3\nbcba\n1", "2\ncdba\n3\nbcbb\n1", "2\nbdca\n3\nbcbb\n1", "2\nacdb\n3\nbcbb\n1"], "outputs": ["abc\nab", "abc\nab\n", "cba\nab\n", "cba\nba\n", "cba\na\n", "cba\nb\n", "bca\nb\n", "acb\nb\n"]}
| 408
| 158
|
coding
|
Solve the programming task below in a Python markdown code block.
A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward. Examples of numerical palindromes are:
2332
110011
54322345
For this kata, single digit numbers will not be considered numerical palindromes.
For a given number ```num```, write a function to test if the number contains a numerical palindrome or not and return a boolean (true if it does and false if does not). Return "Not valid" if the input is not an integer or is less than 0.
Note: Palindromes should be found without permutating ```num```.
```
palindrome(5) => false
palindrome(1221) => true
palindrome(141221001) => true
palindrome(1215) => true
palindrome(1294) => false
palindrome("109982") => "Not valid"
```
Also feel free to reuse/extend the following starter code:
```python
def palindrome(num):
```
|
{"functional": "_inputs = [[868], [1321], [1215], [2], [123322367], [1551], [13598], ['ACCDDCCA'], ['1551'], [-4505], [22.22]]\n_outputs = [[True], [False], [True], [False], [True], [True], [False], ['Not valid'], ['Not valid'], ['Not valid'], ['Not valid']]\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(palindrome(*i), o[0])"}
| 256
| 254
|
coding
|
Solve the programming task below in a Python markdown code block.
While playing yet another strategy game, Mans has recruited $n$ Swedish heroes, whose powers which can be represented as an array $a$.
Unfortunately, not all of those mighty heroes were created as capable as he wanted, so that he decided to do something about it. In order to accomplish his goal, he can pick two consecutive heroes, with powers $a_i$ and $a_{i+1}$, remove them and insert a hero with power $-(a_i+a_{i+1})$ back in the same position.
For example if the array contains the elements $[5, 6, 7, 8]$, he can pick $6$ and $7$ and get $[5, -(6+7), 8] = [5, -13, 8]$.
After he will perform this operation $n-1$ times, Mans will end up having only one hero. He wants his power to be as big as possible. What's the largest possible power he can achieve?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 200000$).
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$) — powers of the heroes.
-----Output-----
Print the largest possible power he can achieve after $n-1$ operations.
-----Examples-----
Input
4
5 6 7 8
Output
26
Input
5
4 -5 9 -2 1
Output
15
-----Note-----
Suitable list of operations for the first sample:
$[5, 6, 7, 8] \rightarrow [-11, 7, 8] \rightarrow [-11, -15] \rightarrow [26]$
|
{"inputs": ["1\n8\n", "1\n11\n", "1\n11\n", "1\n22\n", "1\n15\n", "2\n86 5\n", "2\n86 5\n", "2\n86 8\n"], "outputs": ["8\n", "11\n", "11", "22\n", "15\n", "-91\n", "-91", "-94\n"]}
| 415
| 104
|
coding
|
Solve the programming task below in a Python markdown code block.
One day Vasya heard a story: "In the city of High Bertown a bus number 62 left from the bus station. It had n grown-ups and m kids..."
The latter events happen to be of no importance to us. Vasya is an accountant and he loves counting money. So he wondered what maximum and minimum sum of money these passengers could have paid for the ride.
The bus fare equals one berland ruble in High Bertown. However, not everything is that easy — no more than one child can ride for free with each grown-up passenger. That means that a grown-up passenger who rides with his k (k > 0) children, pays overall k rubles: a ticket for himself and (k - 1) tickets for his children. Also, a grown-up can ride without children, in this case he only pays one ruble.
We know that in High Bertown children can't ride in a bus unaccompanied by grown-ups.
Help Vasya count the minimum and the maximum sum in Berland rubles, that all passengers of this bus could have paid in total.
Input
The input file consists of a single line containing two space-separated numbers n and m (0 ≤ n, m ≤ 105) — the number of the grown-ups and the number of the children in the bus, correspondingly.
Output
If n grown-ups and m children could have ridden in the bus, then print on a single line two space-separated integers — the minimum and the maximum possible total bus fare, correspondingly.
Otherwise, print "Impossible" (without the quotes).
Examples
Input
1 2
Output
2 2
Input
0 5
Output
Impossible
Input
2 2
Output
2 3
Note
In the first sample a grown-up rides with two children and pays two rubles.
In the second sample there are only children in the bus, so the situation is impossible.
In the third sample there are two cases:
* Each of the two grown-ups rides with one children and pays one ruble for the tickets. In this case the passengers pay two rubles in total.
* One of the grown-ups ride with two children's and pays two rubles, the another one rides alone and pays one ruble for himself. So, they pay three rubles in total.
|
{"inputs": ["0 1\n", "2 7\n", "7 1\n", "0 0\n", "6 0\n", "1 0\n", "1 1\n", "0 2\n"], "outputs": ["Impossible\n", "7 8\n", "7 7\n", "0 0\n", "6 6\n", "1 1\n", "1 1\n", "Impossible\n"]}
| 503
| 98
|
coding
|
Solve the programming task below in a Python markdown code block.
Snuke lives on an infinite two-dimensional plane. He is going on an N-day trip. At the beginning of Day 1, he is at home. His plan is described in a string S of length N. On Day i(1 ≦ i ≦ N), he will travel a positive distance in the following direction:
* North if the i-th letter of S is `N`
* West if the i-th letter of S is `W`
* South if the i-th letter of S is `S`
* East if the i-th letter of S is `E`
He has not decided each day's travel distance. Determine whether it is possible to set each day's travel distance so that he will be back at home at the end of Day N.
Constraints
* 1 ≦ | S | ≦ 1000
* S consists of the letters `N`, `W`, `S`, `E`.
Input
The input is given from Standard Input in the following format:
S
Output
Print `Yes` if it is possible to set each day's travel distance so that he will be back at home at the end of Day N. Otherwise, print `No`.
Examples
Input
SENW
Output
Yes
Input
NSNNSNSN
Output
Yes
Input
NNEW
Output
No
Input
W
Output
No
|
{"inputs": ["S", "W", "SEWN", "NNDW", "ESWN", "WNES", "NEWS", "ENWS"], "outputs": ["No\n", "No", "Yes\n", "No\n", "Yes\n", "Yes\n", "Yes\n", "Yes\n"]}
| 300
| 67
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given a string s of even length consisting of digits from 0 to 9, and two integers a and b.
You can apply either of the following two operations any number of times and in any order on s:
Add a to all odd indices of s (0-indexed). Digits post 9 are cycled back to 0. For example, if s = "3456" and a = 5, s becomes "3951".
Rotate s to the right by b positions. For example, if s = "3456" and b = 1, s becomes "6345".
Return the lexicographically smallest string you can obtain by applying the above operations any number of times on s.
A string a is lexicographically smaller than a string b (of the same length) if in the first position where a and b differ, string a has a letter that appears earlier in the alphabet than the corresponding letter in b. For example, "0158" is lexicographically smaller than "0190" because the first position they differ is at the third letter, and '5' comes before '9'.
Please complete the following python code precisely:
```python
class Solution:
def findLexSmallestString(self, s: str, a: int, b: int) -> str:
```
|
{"functional": "def check(candidate):\n assert candidate(s = \"5525\", a = 9, b = 2) == \"2050\"\n assert candidate(s = \"74\", a = 5, b = 1) == \"24\"\n assert candidate(s = \"0011\", a = 4, b = 2) == \"0011\"\n\n\ncheck(Solution().findLexSmallestString)"}
| 302
| 105
|
coding
|
Solve the programming task below in a Python markdown code block.
The Mormons are trying to find new followers and in order to do that they embark on missions.
Each time they go on a mission, every Mormons converts a fixed number of people (reach) into followers. This continues and every freshly converted Mormon as well as every original Mormon go on another mission and convert the same fixed number of people each. The process continues until they reach a predefined target number of followers (input into the model).
Converted Mormons are unique so that there's no duplication amongst them.
Create a function Mormons(startingNumber, reach, target) that calculates how many missions Mormons need to embark on, in order to reach their target. While each correct solution will pass, for more fun try to make a recursive function.
All model inputs are valid positive integers.
Also feel free to reuse/extend the following starter code:
```python
def mormons(starting_number, reach, target):
```
|
{"functional": "_inputs = [[40, 2, 120], [40, 2, 121]]\n_outputs = [[1], [2]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(mormons(*i), o[0])"}
| 195
| 179
|
coding
|
Solve the programming task below in a Python markdown code block.
Cara is applying for several different jobs.
The online application forms ask her to respond within a specific character count.
Cara needs to check that her answers fit into the character limit.
Annoyingly, some application forms count spaces as a character, and some don't.
Your challenge:
Write Cara a function `charCheck()` with the arguments:
- `"text"`: a string containing Cara's answer for the question
- `"max"`: a number equal to the maximum number of characters allowed in the answer
- `"spaces"`: a boolean which is `True` if spaces are included in the character count and `False` if they are not
The function `charCheck()` should return an array: `[True, "Answer"]` , where `"Answer"` is equal to the original text, if Cara's answer is short enough.
If her answer `"text"` is too long, return an array: `[False, "Answer"]`.
The second element should be the original `"text"` string truncated to the length of the limit.
When the `"spaces"` argument is `False`, you should remove the spaces from the `"Answer"`.
For example:
- `charCheck("Cara Hertz", 10, True)` should return `[ True, "Cara Hertz" ]`
- `charCheck("Cara Hertz", 9, False)` should return `[ True, "CaraHertz" ]`
- `charCheck("Cara Hertz", 5, True)` should return `[ False, "Cara " ]`
- `charCheck("Cara Hertz", 5, False)` should return `[ False, "CaraH" ]`
Also feel free to reuse/extend the following starter code:
```python
def charCheck(text, mx, spaces):
```
|
{"functional": "_inputs = [['I am applying for the role of Base Manager on Titan.', 60, True], ['I am looking to relocate to the vicinity of Saturn for family reasons.', 70, True], ['As Deputy Base Manager on Phobos for five Martian years, I have significant relevant experience.', 90, False], ['A challenging career moment came with the rapid depletion of water supplies on Phobos.', 80, False], ['But, as I pointed out, anyone complaining about standing downwind was lying. There was no wind.', 75, True], ['I have no notice period on Phobos. I can start immediately.', 50, True]]\n_outputs = [[[True, 'I am applying for the role of Base Manager on Titan.']], [[True, 'I am looking to relocate to the vicinity of Saturn for family reasons.']], [[True, 'AsDeputyBaseManageronPhobosforfiveMartianyears,Ihavesignificantrelevantexperience.']], [[True, 'AchallengingcareermomentcamewiththerapiddepletionofwatersuppliesonPhobos.']], [[False, 'But, as I pointed out, anyone complaining about standing downwind was lying']], [[False, 'I have no notice period on Phobos. I can start imm']]]\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(charCheck(*i), o[0])"}
| 386
| 418
|
coding
|
Solve the programming task below in a Python markdown code block.
You are an immigration officer in the Kingdom of AtCoder. The document carried by an immigrant has some number of integers written on it, and you need to check whether they meet certain criteria.
According to the regulation, the immigrant should be allowed entry to the kingdom if and only if the following condition is satisfied:
* All even numbers written on the document are divisible by 3 or 5.
If the immigrant should be allowed entry according to the regulation, output `APPROVED`; otherwise, print `DENIED`.
Constraints
* All values in input are integers.
* 1 \leq N \leq 100
* 1 \leq A_i \leq 1000
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \dots A_N
Output
If the immigrant should be allowed entry according to the regulation, print `APPROVED`; otherwise, print `DENIED`.
Examples
Input
5
6 7 9 10 31
Output
APPROVED
Input
3
28 27 24
Output
DENIED
|
{"inputs": ["3\n2 8 9", "3\n4 3 47", "3\n4 5 47", "3\n4 5 66", "3\n4 5 99", "3\n4 5 69", "3\n2 5 69", "3\n2 8 69"], "outputs": ["DENIED\n", "DENIED\n", "DENIED\n", "DENIED\n", "DENIED\n", "DENIED\n", "DENIED\n", "DENIED\n"]}
| 253
| 125
|
coding
|
Solve the programming task below in a Python markdown code block.
To give credit where credit is due: This problem was taken from the ACMICPC-Northwest Regional Programming Contest. Thank you problem writers.
You are helping an archaeologist decipher some runes. He knows that this ancient society used a Base 10 system, and that they never start a number with a leading zero. He's figured out most of the digits as well as a few operators, but he needs your help to figure out the rest.
The professor will give you a simple math expression, of the form
```
[number][op][number]=[number]
```
He has converted all of the runes he knows into digits. The only operators he knows are addition (`+`),subtraction(`-`), and multiplication (`*`), so those are the only ones that will appear. Each number will be in the range from -1000000 to 1000000, and will consist of only the digits 0-9, possibly a leading -, and maybe a few ?s. If there are ?s in an expression, they represent a digit rune that the professor doesn't know (never an operator, and never a leading -). All of the ?s in an expression will represent the same digit (0-9), and it won't be one of the other given digits in the expression. No number will begin with a 0 unless the number itself is 0, therefore 00 would not be a valid number.
Given an expression, figure out the value of the rune represented by the question mark. If more than one digit works, give the lowest one. If no digit works, well, that's bad news for the professor - it means that he's got some of his runes wrong. output -1 in that case.
Complete the method to solve the expression to find the value of the unknown rune. The method takes a string as a paramater repressenting the expression and will return an int value representing the unknown rune or -1 if no such rune exists.
~~~if:php
**Most of the time, the professor will be able to figure out most of the runes himself, but sometimes, there may be exactly 1 rune present in the expression that the professor cannot figure out (resulting in all question marks where the digits are in the expression) so be careful ;)**
~~~
Also feel free to reuse/extend the following starter code:
```python
def solve_runes(runes):
```
|
{"functional": "_inputs = [['123?45*?=?'], ['?*123?45=?'], ['??605*-63=-73???5'], ['123?45+?=123?45'], ['?8?170-1?6256=7?2?14'], ['?38???+595???=833444'], ['123?45-?=123?45'], ['-7715?5--484?00=-28?9?5'], ['50685?--1?5630=652?8?'], ['??+??=??'], ['-?56373--9216=-?47157']]\n_outputs = [[0], [0], [1], [0], [9], [2], [0], [6], [4], [-1], [8]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(solve_runes(*i), o[0])"}
| 520
| 362
|
coding
|
Solve the programming task below in a Python markdown code block.
Vasily the Programmer loves romance, so this year he decided to illuminate his room with candles.
Vasily has a candles.When Vasily lights up a new candle, it first burns for an hour and then it goes out. Vasily is smart, so he can make b went out candles into a new candle. As a result, this new candle can be used like any other new candle.
Now Vasily wonders: for how many hours can his candles light up the room if he acts optimally well? Help him find this number.
-----Input-----
The single line contains two integers, a and b (1 ≤ a ≤ 1000; 2 ≤ b ≤ 1000).
-----Output-----
Print a single integer — the number of hours Vasily can light up the room for.
-----Examples-----
Input
4 2
Output
7
Input
6 3
Output
8
-----Note-----
Consider the first sample. For the first four hours Vasily lights up new candles, then he uses four burned out candles to make two new ones and lights them up. When these candles go out (stop burning), Vasily can make another candle. Overall, Vasily can light up the room for 7 hours.
|
{"inputs": ["4 2\n", "6 3\n", "1 2\n", "1 3\n", "1 4\n", "2 2\n", "3 2\n", "3 3\n"], "outputs": ["7\n", "8\n", "1\n", "1\n", "1\n", "3\n", "5\n", "4\n"]}
| 267
| 86
|
coding
|
Solve the programming task below in a Python markdown code block.
At Akabe High School, which is a programmer training school, the roles of competition programmers in team battles are divided into the following three types.
C: | Coder | I am familiar with the language and code.
--- | --- | ---
A: | Algorithm | I am good at logical thinking and think about algorithms.
N: | Navigator | Good at reading comprehension, analyze and debug problems.
At this high school, a team of three people is formed in one of the following formations.
CCA: | Well-balanced and stable
--- | ---
CCC: | Fast-paced type with high risk but expected speed
CAN: | Careful solving of problems accurately
As a coach of the Competitive Programming Department, you take care every year to combine these members and form as many teams as possible. Therefore, create a program that outputs the maximum number of teams that can be created when the number of coders, the number of algorithms, and the number of navigators are given as inputs.
input
The input consists of one dataset. Input data is given in the following format.
Q
c1 a1 n1
c2 a2 n2
::
cQ aQ nQ
Q (0 ≤ Q ≤ 100) on the first line is the number of years for which you want to find the number of teams. The number of people by role in each year is given to the following Q line. Each row is given the number of coders ci (0 ≤ ci ≤ 1000), the number of algorithms ai (0 ≤ ai ≤ 1000), and the number of navigators ni (0 ≤ ni ≤ 1000).
output
Output the maximum number of teams that can be created on one line for each year.
Example
Input
4
3 0 0
1 1 1
9 4 1
0 1 2
Output
1
1
4
0
|
{"inputs": ["4\n3 0 0\n1 1 1\n9 4 2\n0 1 2", "4\n2 0 0\n1 1 1\n9 4 2\n0 1 2", "4\n3 0 1\n1 1 1\n9 4 1\n0 1 2", "4\n3 0 0\n1 2 1\n0 4 2\n0 1 2", "4\n0 0 0\n1 2 1\n0 4 2\n0 1 3", "4\n2 1 0\n2 2 2\n9 8 2\n0 1 3", "4\n0 1 2\n3 0 2\n9 0 0\n0 2 3", "4\n0 0 0\n1 2 1\n1 4 2\n0 0 3"], "outputs": ["1\n1\n5\n0\n", "0\n1\n5\n0\n", "1\n1\n4\n0\n", "1\n1\n0\n0\n", "0\n1\n0\n0\n", "1\n2\n5\n0\n", "0\n1\n3\n0\n", "0\n1\n1\n0\n"]}
| 415
| 302
|
coding
|
Solve the programming task below in a Python markdown code block.
Squid loves painting vertices in graphs.
There is a simple undirected graph consisting of N vertices numbered 1 through N, and M edges. Initially, all the vertices are painted in color 0. The i-th edge bidirectionally connects two vertices a_i and b_i. The length of every edge is 1.
Squid performed Q operations on this graph. In the i-th operation, he repaints all the vertices within a distance of d_i from vertex v_i, in color c_i.
Find the color of each vertex after the Q operations.
Constraints
* 1 ≤ N,M,Q ≤ 10^5
* 1 ≤ a_i,b_i,v_i ≤ N
* a_i ≠ b_i
* 0 ≤ d_i ≤ 10
* 1 ≤ c_i ≤10^5
* d_i and c_i are all integers.
* There are no self-loops or multiple edges in the given graph.
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
:
a_{M} b_{M}
Q
v_1 d_1 c_1
:
v_{Q} d_{Q} c_{Q}
Output
Print the answer in N lines. In the i-th line, print the color of vertex i after the Q operations.
Examples
Input
7 7
1 2
1 3
1 4
4 5
5 6
5 7
2 3
2
6 1 1
1 2 2
Output
2
2
2
2
2
1
0
Input
14 10
1 4
5 7
7 11
4 10
14 7
14 3
6 14
8 11
5 13
8 3
8
8 6 2
9 7 85
6 9 3
6 7 5
10 3 1
12 9 4
9 6 6
8 2 3
Output
1
0
3
1
5
5
3
3
6
1
3
4
5
3
|
{"inputs": ["7 7\n1 2\n1 3\n1 4\n4 5\n5 6\n5 7\n2 3\n2\n6 0 1\n1 2 2", "7 7\n1 2\n1 3\n1 4\n4 5\n5 3\n5 7\n2 3\n2\n6 0 1\n1 2 1", "7 7\n1 2\n1 3\n1 4\n4 6\n5 6\n5 7\n2 3\n2\n6 0 1\n1 2 2", "7 7\n1 2\n1 3\n1 3\n4 5\n5 3\n5 7\n2 3\n2\n6 0 1\n1 2 1", "7 7\n1 1\n2 3\n1 4\n4 5\n5 5\n5 7\n2 3\n2\n6 1 1\n1 2 2", "7 7\n1 2\n1 3\n1 4\n4 5\n5 6\n5 7\n2 3\n2\n6 1 1\n1 2 2", "14 10\n1 4\n5 7\n8 11\n2 6\n14 7\n14 3\n6 5\n9 6\n5 13\n8 3\n8\n4 0 2\n14 7 85\n6 9 0\n6 7 7\n10 3 1\n1 9 4\n9 6 6\n8 2 3", "14 10\n2 4\n5 7\n8 11\n2 6\n14 7\n14 3\n6 5\n9 6\n5 13\n8 3\n8\n4 0 2\n14 7 85\n6 9 0\n6 7 7\n10 3 1\n1 9 4\n9 6 6\n8 2 3"], "outputs": ["2\n2\n2\n2\n2\n1\n0\n", "1\n1\n1\n1\n1\n1\n0\n", "2\n2\n2\n2\n0\n2\n0\n", "1\n1\n1\n0\n1\n1\n0\n", "2\n0\n0\n2\n2\n1\n0\n", "2\n2\n2\n2\n2\n1\n0", "4\n6\n3\n4\n6\n6\n6\n3\n6\n1\n3\n0\n6\n3\n", "4\n6\n3\n6\n6\n6\n6\n3\n6\n1\n3\n0\n6\n3\n"]}
| 499
| 651
|
coding
|
Solve the programming task below in a Python markdown code block.
Sho has an array $a$ consisting of $n$ integers. An operation consists of choosing two distinct indices $i$ and $j$ and removing $a_i$ and $a_j$ from the array.
For example, for the array $[2, 3, 4, 2, 5]$, Sho can choose to remove indices $1$ and $3$. After this operation, the array becomes $[3, 2, 5]$. Note that after any operation, the length of the array is reduced by two.
After he made some operations, Sho has an array that has only distinct elements. In addition, he made operations such that the resulting array is the longest possible.
More formally, the array after Sho has made his operations respects these criteria:
No pairs such that ($i < j$) and $a_i = a_j$ exist.
The length of $a$ is maximized.
Output the length of the final array.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 10^3$) — the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 50$) — the length of the array.
The second line of each test case contains $n$ integers $a_i$ ($1 \leq a_i \leq 10^4$) — the elements of the array.
-----Output-----
For each test case, output a single integer — the length of the final array. Remember that in the final array, all elements are different, and its length is maximum.
-----Examples-----
Input
4
6
2 2 2 3 3 3
5
9 1 9 9 1
4
15 16 16 15
4
10 100 1000 10000
Output
2
1
2
4
-----Note-----
For the first test case Sho can perform operations as follows:
Choose indices $1$ and $5$ to remove. The array becomes $[2, 2, 2, 3, 3, 3] \rightarrow [2, 2, 3, 3]$.
Choose indices $1$ and $4$ to remove. The array becomes $[2, 2, 3, 3] \rightarrow [2, 3]$.
The final array has a length of $2$, so the answer is $2$. It can be proven that Sho cannot obtain an array with a longer length.
For the second test case Sho can perform operations as follows:
Choose indices $3$ and $4$ to remove. The array becomes $[9, 1, 9, 9, 1] \rightarrow [9, 1, 1]$.
Choose indices $1$ and $3$ to remove. The array becomes $[9, 1, 1] \rightarrow [1]$.
The final array has a length of $1$, so the answer is $1$. It can be proven that Sho cannot obtain an array with a longer length.
|
{"inputs": ["1\n2\n1 9\n", "4\n6\n2 2 2 3 3 3\n5\n9 1 9 9 1\n4\n15 16 16 15\n4\n10 100 1000 10000\n"], "outputs": ["2\n", "2\n1\n2\n4\n"]}
| 690
| 94
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
An image smoother is a filter of the size 3 x 3 that can be applied to each cell of an image by rounding down the average of the cell and the eight surrounding cells (i.e., the average of the nine cells in the blue smoother). If one or more of the surrounding cells of a cell is not present, we do not consider it in the average (i.e., the average of the four cells in the red smoother).
Given an m x n integer matrix img representing the grayscale of an image, return the image after applying the smoother on each cell of it.
Please complete the following python code precisely:
```python
class Solution:
def imageSmoother(self, img: List[List[int]]) -> List[List[int]]:
```
|
{"functional": "def check(candidate):\n assert candidate(img = [[1,1,1],[1,0,1],[1,1,1]]) == [[0, 0, 0],[0, 0, 0], [0, 0, 0]]\n assert candidate(img = [[100,200,100],[200,50,200],[100,200,100]]) == [[137,141,137],[141,138,141],[137,141,137]]\n\n\ncheck(Solution().imageSmoother)"}
| 171
| 155
|
coding
|
Solve the programming task below in a Python markdown code block.
String can be called correct if it consists of characters "0" and "1" and there are no redundant leading zeroes. Here are some examples: "0", "10", "1001".
You are given a correct string s.
You can perform two different operations on this string: swap any pair of adjacent characters (for example, "101" $\rightarrow$ "110"); replace "11" with "1" (for example, "110" $\rightarrow$ "10").
Let val(s) be such a number that s is its binary representation.
Correct string a is less than some other correct string b iff val(a) < val(b).
Your task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all).
-----Input-----
The first line contains integer number n (1 ≤ n ≤ 100) — the length of string s.
The second line contains the string s consisting of characters "0" and "1". It is guaranteed that the string s is correct.
-----Output-----
Print one string — the minimum correct string that you can obtain from the given one.
-----Examples-----
Input
4
1001
Output
100
Input
1
1
Output
1
-----Note-----
In the first example you can obtain the answer by the following sequence of operations: "1001" $\rightarrow$ "1010" $\rightarrow$ "1100" $\rightarrow$ "100".
In the second example you can't obtain smaller answer no matter what operations you use.
|
{"inputs": ["1\n1\n", "1\n0\n", "1\n0\n", "1\n1\n", "2\n10\n", "2\n11\n", "2\n11\n", "2\n10\n"], "outputs": ["1\n", "0\n", "0\n", "1\n", "10\n", "1\n", "1\n", "10\n"]}
| 373
| 92
|
coding
|
Solve the programming task below in a Python markdown code block.
Create a function
```python
has_two_cube_sums(n)
```
which checks if a given number `n` can be written as the sum of two cubes in two different ways: `n = a³+b³ = c³+d³`.
All the numbers `a`, `b`, `c` and `d` should be different and greater than `0`.
E.g. 1729 = 9³+10³ = 1³+12³.
```python
has_two_cube_sums(1729); // true
has_two_cube_sums(42); // false
```
Also feel free to reuse/extend the following starter code:
```python
def has_two_cube_sums(n):
```
|
{"functional": "_inputs = [[1], [1729], [42], [4103], [4102], [4104], [4105], [4106], [0], [46163]]\n_outputs = [[False], [True], [False], [False], [False], [True], [False], [False], [False], [False]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(has_two_cube_sums(*i), o[0])"}
| 171
| 234
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given an integer n representing an array colors of length n where all elements are set to 0's meaning uncolored. You are also given a 2D integer array queries where queries[i] = [indexi, colori]. For the ith query:
Set colors[indexi] to colori.
Count adjacent pairs in colors set to the same color (regardless of colori).
Return an array answer of the same length as queries where answer[i] is the answer to the ith query.
Please complete the following python code precisely:
```python
class Solution:
def colorTheArray(self, n: int, queries: List[List[int]]) -> List[int]:
```
|
{"functional": "def check(candidate):\n assert candidate(n = 4, queries = [[0,2],[1,2],[3,1],[1,1],[2,1]]) == [0,1,1,0,2]\n assert candidate(n = 1, queries = [[0,100000]]) == [0]\n\n\ncheck(Solution().colorTheArray)"}
| 158
| 89
|
coding
|
Solve the programming task below in a Python markdown code block.
Let's define a periodic infinite sequence S$S$ (0$0$-indexed) with period K$K$ using the formula Si=(i%K)+1$S_i = (i \% K) + 1$.
Chef has found a sequence of positive integers A$A$ with length N$N$ buried underground. He suspects that it is a contiguous subsequence of some periodic sequence. Unfortunately, some elements of A$A$ are unreadable. Can you tell Chef the longest possible period K$K$ of an infinite periodic sequence which contains A$A$ (after suitably filling in the unreadable elements) as a contiguous subsequence?
-----Input-----
- The first line of the input contains a single integer T$T$ denoting the number of test cases. The description of T$T$ test cases follows.
- The first line of each test case contains a single integer N$N$.
- The second line contains N$N$ space-separated integers A1,A2,…,AN$A_1, A_2, \dots, A_N$. Unreadable elements are denoted by −1$-1$.
-----Output-----
For each test case, print a single line.
- If the period can be arbitrarily large, this line should contain a single string "inf".
- Otherwise, if A$A$ cannot be a contiguous subsequence of a periodic sequence, it should contain a single string "impossible".
- Otherwise, it should contain a single integer — the maximum possible period.
-----Constraints-----
- 1≤T≤100$1 \le T \le 100$
- 2≤N≤105$2 \le N \le 10^5$
- the sum of N$N$ over all test cases does not exceed 106$10^6$
- for each valid i$i$, 1≤Ai≤106$1 \le A_i \le 10^6$ or Ai=−1$A_i = -1$
-----Subtasks-----
Subtask #1 (50 points):
- 2≤N≤1,000$2 \le N \le 1,000$
- the sum of N$N$ over all test cases does not exceed 10,000$10,000$
Subtask #2 (50 points): original constraints
-----Example Input-----
3
3
-1 -1 -1
5
1 -1 -1 4 1
4
4 6 7 -1
-----Example Output-----
inf
4
impossible
|
{"inputs": ["3\n3\n-1 -1 -1\n5\n1 -1 -1 4 1\n4\n4 6 7 -1"], "outputs": ["inf\n4\nimpossible"]}
| 568
| 48
|
coding
|
Solve the programming task below in a Python markdown code block.
Little Susie loves strings. Today she calculates distances between them. As Susie is a small girl after all, her strings contain only digits zero and one. She uses the definition of Hamming distance:
We will define the distance between two strings s and t of the same length consisting of digits zero and one as the number of positions i, such that s_{i} isn't equal to t_{i}.
As besides everything else Susie loves symmetry, she wants to find for two strings s and t of length n such string p of length n, that the distance from p to s was equal to the distance from p to t.
It's time for Susie to go to bed, help her find such string p or state that it is impossible.
-----Input-----
The first line contains string s of length n.
The second line contains string t of length n.
The length of string n is within range from 1 to 10^5. It is guaranteed that both strings contain only digits zero and one.
-----Output-----
Print a string of length n, consisting of digits zero and one, that meets the problem statement. If no such string exist, print on a single line "impossible" (without the quotes).
If there are multiple possible answers, print any of them.
-----Examples-----
Input
0001
1011
Output
0011
Input
000
111
Output
impossible
-----Note-----
In the first sample different answers are possible, namely — 0010, 0011, 0110, 0111, 1000, 1001, 1100, 1101.
|
{"inputs": ["0\n0\n", "0\n1\n", "1\n1\n", "1\n0\n", "1\n0\n", "0\n0\n", "0\n1\n", "1\n1\n"], "outputs": ["0\n", "impossible\n", "1\n", "impossible\n", "impossible\n", "0", "impossible\n", "1"]}
| 376
| 88
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$.
A move consists of the following steps:
- Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$.
- Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$.
For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them.
-----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 $M$.
- $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces.
-----Output-----
For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible.
-----Constraints-----
- $1 \le T \le 100$
- $1 \le N, M \le 1,000$
- $A_{i, j} \in \{0, 1\}$ for each valid $i, j$
- the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$
-----Example Input-----
1
3 3
010
000
001
-----Example Output-----
1 0 1
2 1 1
1 1 0
|
{"inputs": ["1\n3 3\n010\n000\n001"], "outputs": ["1 0 1\n2 1 1\n1 1 0"]}
| 620
| 44
|
coding
|
Solve the programming task below in a Python markdown code block.
A triple of numbers is said to be poor when two of those numbers are equal but the other number is different from those two numbers.
You will be given three integers A, B, and C. If this triple is poor, print Yes; otherwise, print No.
-----Constraints-----
- A, B, and C are all integers between 1 and 9 (inclusive).
-----Input-----
Input is given from Standard Input in the following format:
A B C
-----Output-----
If the given triple is poor, print Yes; otherwise, print No.
-----Sample Input-----
5 7 5
-----Sample Output-----
Yes
A and C are equal, but B is different from those two numbers, so this triple is poor.
|
{"inputs": ["2 3 4", "1 4 4", "7 9 6", "4 3 4", "1 4 8", "7 4 6", "4 5 4", "0 4 8"], "outputs": ["No\n", "Yes\n", "No\n", "Yes\n", "No\n", "No\n", "Yes\n", "No\n"]}
| 162
| 94
|
coding
|
Solve the programming task below in a Python markdown code block.
The new operating system BerOS has a nice feature. It is possible to use any number of characters '/' as a delimiter in path instead of one traditional '/'. For example, strings //usr///local//nginx/sbin// and /usr/local/nginx///sbin are equivalent. The character '/' (or some sequence of such characters) at the end of the path is required only in case of the path to the root directory, which can be represented as single character '/'.
A path called normalized if it contains the smallest possible number of characters '/'.
Your task is to transform a given path to the normalized form.
Input
The first line of the input contains only lowercase Latin letters and character '/' — the path to some directory. All paths start with at least one character '/'. The length of the given line is no more than 100 characters, it is not empty.
Output
The path in normalized form.
Examples
Input
//usr///local//nginx/sbin
Output
/usr/local/nginx/sbin
|
{"inputs": ["/\n", "////\n", "/a/b/c\n", "/b/b/c\n", "/c/b/c\n", "/aa//b/aa\n", "/a//aa/a//\n", "////a//b/////g\n"], "outputs": ["/\n", "/\n", "/a/b/c\n", "/b/b/c\n", "/c/b/c\n", "/aa/b/aa\n", "/a/aa/a\n", "/a/b/g\n"]}
| 214
| 107
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given a weighted undirected connected graph with n vertices numbered from 0 to n - 1, and an array edges where edges[i] = [ai, bi, weighti] represents a bidirectional and weighted edge between nodes ai and bi. A minimum spanning tree (MST) is a subset of the graph's edges that connects all vertices without cycles and with the minimum possible total edge weight.
Find all the critical and pseudo-critical edges in the given graph's minimum spanning tree (MST). An MST edge whose deletion from the graph would cause the MST weight to increase is called a critical edge. On the other hand, a pseudo-critical edge is that which can appear in some MSTs but not all.
Note that you can return the indices of the edges in any order.
Please complete the following python code precisely:
```python
class Solution:
def findCriticalAndPseudoCriticalEdges(self, n: int, edges: List[List[int]]) -> List[List[int]]:
```
|
{"functional": "def check(candidate):\n assert candidate(n = 5, edges = [[0,1,1],[1,2,1],[2,3,2],[0,3,2],[0,4,3],[3,4,3],[1,4,6]]) == [[0,1],[2,3,4,5]]\n assert candidate(n = 4, edges = [[0,1,1],[1,2,1],[2,3,1],[0,3,1]]) == [[],[0,1,2,3]]\n\n\ncheck(Solution().findCriticalAndPseudoCriticalEdges)"}
| 228
| 141
|
coding
|
Solve the programming task below in a Python markdown code block.
Let's denote d(n) as the number of divisors of a positive integer n. You are given three integers a, b and c. Your task is to calculate the following sum:
$\sum_{i = 1}^{a} \sum_{j = 1}^{b} \sum_{k = 1}^{c} d(i \cdot j \cdot k)$
Find the sum modulo 1073741824 (2^30).
-----Input-----
The first line contains three space-separated integers a, b and c (1 ≤ a, b, c ≤ 100).
-----Output-----
Print a single integer — the required sum modulo 1073741824 (2^30).
-----Examples-----
Input
2 2 2
Output
20
Input
5 6 7
Output
1520
-----Note-----
For the first example.
d(1·1·1) = d(1) = 1; d(1·1·2) = d(2) = 2; d(1·2·1) = d(2) = 2; d(1·2·2) = d(4) = 3; d(2·1·1) = d(2) = 2; d(2·1·2) = d(4) = 3; d(2·2·1) = d(4) = 3; d(2·2·2) = d(8) = 4.
So the result is 1 + 2 + 2 + 3 + 2 + 3 + 3 + 4 = 20.
|
{"inputs": ["2 2 2\n", "5 6 7\n", "1 5 1\n", "1 5 1\n", "2 1 2\n", "2 2 2\n", "5 6 7\n", "71 2 1\n"], "outputs": ["20\n", "1520\n", "10\n", "10\n", "8\n", "20\n", "1520\n", "811\n"]}
| 390
| 115
|
coding
|
Solve the programming task below in a Python markdown code block.
Given two integers, $\boldsymbol{l}$ and $\textbf{r}$, find the maximal value of $\boldsymbol{a}$ xor $\boldsymbol{b}$, written $a\oplus b$, where $\boldsymbol{a}$ and $\boldsymbol{b}$ satisfy the following condition:
$l\leq a\leq b\leq r$
For example, if $l=11$ and $r=12$, then
$\textbf{11}\oplus11=\textbf{0}$
$\textbf{11}\oplus12=7$
$12\oplus12=0$
Our maximum value is $7$.
Function Description
Complete the maximizingXor function in the editor below. It must return an integer representing the maximum value calculated.
maximizingXor has the following parameter(s):
l: an integer, the lower bound, inclusive
r: an integer, the upper bound, inclusive
Input Format
The first line contains the integer $\boldsymbol{l}$.
The second line contains the integer $\textbf{r}$.
Constraints
$1\leq l\leq r\leq10$^{3}
Output Format
Return the maximal value of the xor operations for all permutations of the integers from $\boldsymbol{l}$ to $\textbf{r}$, inclusive.
Sample Input 0
10
15
Sample Output 0
7
Explanation 0
Here $l=10$ and $r=15$. Testing all pairs:
$10\oplus10=0$
$10\oplus11=1$
$\textbf{10}\oplus12=\textbf{6}$
$\textbf{10}\oplus13=7$
$\textbf{10}\oplus\textbf{14}=4$
$10\oplus15=5$
$\textbf{11}\oplus11=\textbf{0}$
$\textbf{11}\oplus12=7$
$11\oplus13=6$
$11\oplus14=5$
$11\oplus15=4$
$12\oplus12=0$
$12\oplus13=1$
$\textbf{12}\oplus\textbf{14}=2$
$12\oplus15=3$
$13\oplus13=0$
$13\oplus14=3$
$13\oplus15=2$
$14\oplus14=0$
$14\oplus15=1$
$15\oplus15=0$
Two pairs, (10, 13) and (11, 12) have the xor value 7, and this is maximal.
Sample Input 1
11
100
Sample Output 1
127
|
{"inputs": ["10\n15\n", "11\n100\n"], "outputs": ["7\n", "127\n"]}
| 686
| 33
|
coding
|
Solve the programming task below in a Python markdown code block.
Taro came to a square to look for treasure. There are many treasures buried in this square, but Taro has the latest machines, so he knows everything about where the treasures are buried. Since the square is very wide Taro decided to look for the treasure to decide the area, but the treasure is what treasure does not know immediately whether or not there in the area for a lot. So Taro decided to count the number of treasures in that area.
Constraints
> 1 ≤ n ≤ 5000
> 1 ≤ m ≤ 5 × 105
> | xi |, | yi | ≤ 109 (1 ≤ i ≤ n)
> | xi1 |, | yi1 |, | xi2 |, | yi2 | ≤ 109 (1 ≤ i ≤ m)
> xi1 ≤ xi2, yi1 ≤ yi2 (1 ≤ i ≤ m)
>
* All inputs are given as integers
Input
> n m
> x1 y1
> x2 y2
> ...
> xn yn
> x11 y11 x12 y12
> x21 y21 x22 y22
> ...
> xm1 ym1 xm2 ym2
>
* n represents the number of treasures buried in the square
* m represents the number of regions to examine
* The 2nd to n + 1 lines represent the coordinates where each treasure is buried.
* The n + 2nd to n + m + 1 lines represent each area to be examined.
* The positive direction of the x-axis represents the east and the positive direction of the y-axis represents the north.
* Each region is a rectangle, xi1 and yi1 represent the coordinates of the southwestern apex of the rectangle, and xi2 and yi2 represent the coordinates of the northeastern apex of the rectangle.
Output
> C1
> C2
> ...
> Cm
>
* Output the number of treasures contained in each area to each line
Examples
Input
3 1
1 1
2 4
5 3
0 0 5 5
Output
3
Input
4 2
-1 1
0 3
4 0
2 1
-3 1 5 1
4 0 4 0
Output
2
1
Input
2 3
0 0
0 0
-1 -1 1 1
0 0 2 2
1 1 4 4
Output
2
2
0
Input
5 5
10 5
-3 -8
2 11
6 0
-1 3
-3 1 3 13
-1 -1 9 5
-3 -8 10 11
0 0 5 5
-10 -9 15 10
Output
2
2
5
0
4
|
{"inputs": ["3 1\n1 1\n2 6\n5 3\n0 0 5 5", "3 1\n0 2\n2 4\n0 6\n0 0 0 5", "3 1\n0 2\n2 4\n0 6\n0 0 1 5", "3 1\n0 1\n2 4\n0 6\n0 0 1 5", "3 1\n0 1\n2 4\n0 6\n0 0 0 5", "3 1\n0 1\n2 8\n0 6\n0 0 0 5", "3 1\n1 1\n2 6\n5 3\n0 0 1 5", "3 1\n1 1\n2 4\n5 3\n0 0 5 5"], "outputs": ["2\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "3"]}
| 655
| 237
|
coding
|
Solve the programming task below in a Python markdown code block.
The government of Berland decided to improve network coverage in his country. Berland has a unique structure: the capital in the center and $n$ cities in a circle around the capital. The capital already has a good network coverage (so the government ignores it), but the $i$-th city contains $a_i$ households that require a connection.
The government designed a plan to build $n$ network stations between all pairs of neighboring cities which will maintain connections only for these cities. In other words, the $i$-th network station will provide service only for the $i$-th and the $(i + 1)$-th city (the $n$-th station is connected to the $n$-th and the $1$-st city).
All network stations have capacities: the $i$-th station can provide the connection to at most $b_i$ households.
Now the government asks you to check can the designed stations meet the needs of all cities or not — that is, is it possible to assign each household a network station so that each network station $i$ provides the connection to at most $b_i$ households.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^6$) — the number of cities and stations.
The second line of each test case contains $n$ integers ($1 \le a_i \le 10^9$) — the number of households in the $i$-th city.
The third line of each test case contains $n$ integers ($1 \le b_i \le 10^9$) — the capacities of the designed stations.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $10^6$.
-----Output-----
For each test case, print YES, if the designed stations can meet the needs of all cities, or NO otherwise (case insensitive).
-----Example-----
Input
5
3
2 3 4
3 3 3
3
3 3 3
2 3 4
4
2 3 4 5
3 7 2 2
4
4 5 2 3
2 3 2 7
2
1 1
10 10
Output
YES
YES
NO
YES
YES
-----Note-----
In the first test case: the first network station can provide $2$ connections to the first city and $1$ connection to the second city; the second station can provide $2$ connections to the second city and $1$ connection to the third city; the third station can provide $3$ connections to the third city.
In the second test case: the $1$-st station can provide $2$ connections to the $1$-st city; the $2$-nd station can provide $3$ connections to the $2$-nd city; the $3$-rd station can provide $3$ connections to the $3$-rd city and $1$ connection to the $1$-st station.
In the third test case, the fourth city needs $5$ connections, but the third and the fourth station has $4$ connections in total.
|
{"inputs": ["1\n4\n3 3 3 3\n3 3 3 2\n", "1\n4\n3 3 3 3\n3 3 3 2\n", "1\n4\n3 3 3 4\n3 3 3 2\n", "1\n4\n3 3 3 4\n3 3 6 2\n", "1\n4\n3 3 3 4\n3 3 3 3\n", "1\n4\n3 3 3 4\n3 3 4 3\n", "1\n4\n3 3 3 8\n3 3 4 3\n", "1\n4\n3 3 3 15\n3 3 4 3\n"], "outputs": ["NO\n", "NO\n", "NO\n", "YES\n", "NO\n", "YES\n", "NO\n", "NO\n"]}
| 742
| 215
|
coding
|
Solve the programming task below in a Python markdown code block.
Chouti is working on a strange math problem.
There was a sequence of $n$ positive integers $x_1, x_2, \ldots, x_n$, where $n$ is even. The sequence was very special, namely for every integer $t$ from $1$ to $n$, $x_1+x_2+...+x_t$ is a square of some integer number (that is, a perfect square).
Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $x_2, x_4, x_6, \ldots, x_n$. The task for him is to restore the original sequence. Again, it's your turn to help him.
The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
-----Input-----
The first line contains an even number $n$ ($2 \le n \le 10^5$).
The second line contains $\frac{n}{2}$ positive integers $x_2, x_4, \ldots, x_n$ ($1 \le x_i \le 2 \cdot 10^5$).
-----Output-----
If there are no possible sequence, print "No".
Otherwise, print "Yes" and then $n$ positive integers $x_1, x_2, \ldots, x_n$ ($1 \le x_i \le 10^{13}$), where $x_2, x_4, \ldots, x_n$ should be same as in input data. If there are multiple answers, print any.
Note, that the limit for $x_i$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $1 \le x_i \le 10^{13}$.
-----Examples-----
Input
6
5 11 44
Output
Yes
4 5 16 11 64 44
Input
2
9900
Output
Yes
100 9900
Input
6
314 1592 6535
Output
No
-----Note-----
In the first example $x_1=4$ $x_1+x_2=9$ $x_1+x_2+x_3=25$ $x_1+x_2+x_3+x_4=36$ $x_1+x_2+x_3+x_4+x_5=100$ $x_1+x_2+x_3+x_4+x_5+x_6=144$ All these numbers are perfect squares.
In the second example, $x_1=100$, $x_1+x_2=10000$. They are all perfect squares. There're other answers possible. For example, $x_1=22500$ is another answer.
In the third example, it is possible to show, that no such sequence exists.
|
{"inputs": ["2\n1\n", "2\n1\n", "2\n780\n", "2\n524\n", "2\n203\n", "2\n9900\n", "2\n9900\n", "4\n35 15\n"], "outputs": ["No\n", "No\n", "Yes\n4 780\n", "Yes\n16900 524\n", "Yes\n121 203\n", "Yes\n100 9900\n", "Yes\n100 9900 ", "Yes\n1 35 13 15\n"]}
| 690
| 154
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given a binary tree, determine if it is height-balanced.
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 isBalanced(self, root: Optional[TreeNode]) -> bool:
```
|
{"functional": "def check(candidate):\n assert candidate(root = tree_node([3,9,20,None,None,15,7])) == True\n assert candidate(root = tree_node([1,2,2,3,3,None,None,4,4])) == False\n assert candidate(root = tree_node([])) == True\n\n\ncheck(Solution().isBalanced)"}
| 114
| 84
|
coding
|
Solve the programming task below in a Python markdown code block.
To write a research paper, you should definitely follow the structured format. This format, in many cases, is strictly defined, and students who try to write their papers have a hard time with it.
One of such formats is related to citations. If you refer several pages of a material, you should enumerate their page numbers in ascending order. However, enumerating many page numbers waste space, so you should use the following abbreviated notation:
When you refer all pages between page a and page b (a < b), you must use the notation "a-b". For example, when you refer pages 1, 2, 3, 4, you must write "1-4" not "1 2 3 4". You must not write, for example, "1-2 3-4", "1-3 4", "1-3 2-4" and so on. When you refer one page and do not refer the previous and the next page of that page, you can write just the number of that page, but you must follow the notation when you refer successive pages (more than or equal to 2). Typically, commas are used to separate page numbers, in this problem we use space to separate the page numbers.
You, a kind senior, decided to write a program which generates the abbreviated notation for your junior who struggle with the citation.
Constraints
* 1 ≤ n ≤ 50
Input
Input consists of several datasets.
The first line of the dataset indicates the number of pages n.
Next line consists of n integers. These integers are arranged in ascending order and they are differ from each other.
Input ends when n = 0.
Output
For each dataset, output the abbreviated notation in a line. Your program should not print extra space. Especially, be careful about the space at the end of line.
Example
Input
5
1 2 3 5 6
3
7 8 9
0
Output
1-3 5-6
7-9
|
{"inputs": ["5\n1 1 3 5 6\n3\n7 8 9\n0", "5\n1 1 6 5 6\n3\n7 8 9\n0", "5\n1 1 6 4 6\n3\n7 8 9\n0", "5\n1 1 6 4 9\n3\n7 8 9\n0", "5\n1 1 2 4 9\n3\n7 8 9\n0", "5\n1 2 2 4 9\n3\n7 8 9\n0", "5\n1 2 3 5 6\n3\n3 8 9\n0", "5\n1 1 3 5 6\n3\n8 8 9\n0"], "outputs": ["1 1 3 5-6\n7-9\n", "1 1 6 5-6\n7-9\n", "1 1 6 4 6\n7-9\n", "1 1 6 4 9\n7-9\n", "1 1-2 4 9\n7-9\n", "1-2 2 4 9\n7-9\n", "1-3 5-6\n3 8-9\n", "1 1 3 5-6\n8 8-9\n"]}
| 432
| 320
|
coding
|
Solve the programming task below in a Python markdown code block.
### Background
We **all** know about "balancing parentheses" (plus brackets, braces and chevrons) and even balancing characters that are identical.
Read that last sentence again, I balanced different characters and identical characters twice and you didn't even notice... :)
### Kata
Your challenge in this kata is to write a piece of code to validate that a supplied string is balanced.
You must determine if all that is open is then closed, and nothing is closed which is not already open!
You will be given a string to validate, and a second string, where each pair of characters defines an opening and closing sequence that needs balancing.
You may assume that the second string always has an even number of characters.
### Example
```python
# In this case '(' opens a section, and ')' closes a section
is_balanced("(Sensei says yes!)", "()") # => True
is_balanced("(Sensei says no!", "()") # => False
# In this case '(' and '[' open a section, while ')' and ']' close a section
is_balanced("(Sensei [says] yes!)", "()[]") # => True
is_balanced("(Sensei [says) no!]", "()[]") # => False
# In this case a single quote (') both opens and closes a section
is_balanced("Sensei says 'yes'!", "''") # => True
is_balanced("Sensei say's no!", "''") # => False
```
Also feel free to reuse/extend the following starter code:
```python
def is_balanced(source, caps):
```
|
{"functional": "_inputs = [['(Sensei says yes!)', '()'], ['(Sensei says no!', '()'], ['(Sensei [says] yes!)', '()[]'], ['(Sensei [says) no!]', '()[]'], ['Sensei says -yes-!', '--'], ['Sensei -says no!', '--'], ['Hello Mother can you hear me?', '()'], ['(Hello Mother can you hear me?)', '()'], ['(Hello Mother can you hear me?', ''], ['(Hello Mother can you hear me?', '()'], ['(Hello Mother can you hear me?))', '()'], [')Hello Mother can you hear me?', '()'], ['(Hello Mother can you hear me?)[Monkeys, in my pockets!!]', '()[]'], ['(Hello Mother can you hear me?)[Monkeys, in my pockets!!](Gosh!!)', '()[]'], ['Hello Mother can you hear me?)[Monkeys, in my pockets!!]', '()[]'], ['(Hello Mother can you hear me?[Monkeys, in my pockets!!]', '()[]'], ['(Hello Mother can you hear me?)Monkeys, in my pockets!!]', '()[]'], ['(Hello Mother can you hear me?)[Monkeys, in my pockets!!', '()[]'], ['((Hello))', '()'], ['(((Hello)))', '()'], ['((()Hello()))', '()'], ['((()Hello())', '()'], ['(()Hello()))', '()'], ['([{-Hello!-}])', '()[]{}'], ['([{([{Hello}])}])', '()[]{}'], ['([{-Hello!-})]', '()[]{}'], ['-Hello Mother can you hear me?-', '--'], ['-Hello Mother can you hear me?', '--'], ['Hello Mother can you hear me?-', '--'], ['-abcd-e@fghi@', '--@@'], ['abcd-e@fghi@', '--@@'], ['-abcde@fghi@', '--@@'], ['-abcd-efghi@', '--@@'], ['-abcd-e@fghi', '--@@'], ['-a@b@cd@e@fghi-', '--@@'], ['-ab@cd@e@fghi-', '--@@'], ['-a@bcd@e@fghi-', '--@@'], ['-a@b@cde@fghi-', '--@@'], ['-a@b@cd@efghi-', '--@@'], ['a@b@cd@e@fghi-', '--@@'], ['-a@b@cd@e@fghi', '--@@']]\n_outputs = [[True], [False], [True], [False], [True], [False], [True], [True], [True], [False], [False], [False], [True], [True], [False], [False], [False], [False], [True], [True], [True], [False], [False], [True], [True], [False], [True], [False], [False], [True], [False], [False], [False], [False], [True], [False], [False], [False], [False], [False], [False]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(is_balanced(*i), o[0])"}
| 351
| 811
|
coding
|
Solve the programming task below in a Python markdown code block.
One day Dima and Alex had an argument about the price and quality of laptops. Dima thinks that the more expensive a laptop is, the better it is. Alex disagrees. Alex thinks that there are two laptops, such that the price of the first laptop is less (strictly smaller) than the price of the second laptop but the quality of the first laptop is higher (strictly greater) than the quality of the second laptop.
Please, check the guess of Alex. You are given descriptions of n laptops. Determine whether two described above laptops exist.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 10^5) — the number of laptops.
Next n lines contain two integers each, a_{i} and b_{i} (1 ≤ a_{i}, b_{i} ≤ n), where a_{i} is the price of the i-th laptop, and b_{i} is the number that represents the quality of the i-th laptop (the larger the number is, the higher is the quality).
All a_{i} are distinct. All b_{i} are distinct.
-----Output-----
If Alex is correct, print "Happy Alex", otherwise print "Poor Alex" (without the quotes).
-----Examples-----
Input
2
1 2
2 1
Output
Happy Alex
|
{"inputs": ["1\n1 1\n", "1\n1 1\n", "2\n1 2\n2 1\n", "2\n1 1\n2 2\n", "2\n2 1\n1 2\n", "2\n2 1\n1 2\n", "2\n1 1\n2 2\n", "2\n1 3\n2 1\n"], "outputs": ["Poor Alex\n", "Poor Alex\n", "Happy Alex\n", "Poor Alex\n", "Happy Alex\n", "Happy Alex\n", "Poor Alex\n", "Happy Alex\n"]}
| 287
| 134
|
coding
|
Solve the programming task below in a Python markdown code block.
The Little Elephant from the Zoo of Lviv likes listening to music.
There are N songs, numbered from 1 to N, in his MP3-player. The song i is described by a pair of integers B_{i} and L_{i} - the band (represented as integer) that performed that song and the length of that song in seconds. The Little Elephant is going to listen all the songs exactly once in some order.
The sweetness of the song is equal to the product of the length of that song and the number of different bands listened before (including the current playing song).
Help the Little Elephant to find the order that maximizes the total sweetness of all N songs. Print that sweetness.
------ Input ------
The first line of the input contains single integer T, denoting the number of test cases. Then T test cases follow. The first line of each test case contains single integer N, denoting the number of the songs. The next N lines describe the songs in the MP3-player. The i-th line contains two space-sparated integers B_{i} and L_{i}.
------ Output ------
For each test, output the maximum total sweetness.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 100000 (10^{5})$
$1 ≤ B_{i}, L_{i} ≤ 1000000000 (10^{9})$
----- Sample Input 1 ------
2
3
1 2
2 2
3 2
3
2 3
1 2
2 4
----- Sample Output 1 ------
12
16
----- explanation 1 ------
In the first sample: if he listens the songs in given order, thenB1=1, L1=2: the sweetness = 2 * 1 = 2B2=2, L2=2: the sweetness = 2 * 2 = 4B3=3, L3=2: the sweetness = 2 * 3 = 6So the total sweetness is 12. In this case, you can check the total sweetness does not depend on the order of the songs.
In the second sample: if he listens the songs in given order, thenB1=2, L1=3: the sweetness = 3 * 1 = 3B2=1, L2=2: the sweetness = 2 * 2 = 4B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 15. However, he listens the song 2 firstly, thenB2=1, L2=2: the sweetness = 2 * 1 = 2B1=2, L1=3: the sweetness = 3 * 2 = 6B3=2, L3=4: the sweetness = 4 * 2 = 8So the total sweetness is 16, and it is the maximum total sweetness.
|
{"inputs": ["2\n3\n1 2\n2 2\n3 2\n3\n2 3\n1 2\n2 4", "2\n3\n1 2\n2 2\n3 2\n3\n2 3\n1 2\n2 8", "2\n3\n1 2\n2 2\n3 2\n3\n2 3\n1 2\n2 2", "2\n3\n1 1\n2 2\n3 2\n3\n2 3\n1 2\n2 2", "2\n3\n1 2\n2 2\n4 2\n3\n2 3\n1 2\n2 4", "2\n3\n1 1\n2 2\n3 2\n3\n2 3\n1 2\n2 8", "2\n3\n1 2\n2 2\n3 2\n3\n2 3\n1 2\n2 3", "2\n3\n1 1\n2 2\n1 2\n3\n2 3\n1 2\n2 2"], "outputs": ["12\n16", "12\n24\n", "12\n12\n", "11\n12\n", "12\n16\n", "11\n24\n", "12\n14\n", "9\n12\n"]}
| 653
| 316
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given a real number between 0 and 1 (e.g., 0.72) that is passed in as a double, print the binary representation. If the number cannot be represented accurately in binary with at most 32 characters, print "ERROR".
Please complete the following python code precisely:
```python
class Solution:
def printBin(self, num: float) -> str:
```
|
{"functional": "def check(candidate):\n assert candidate(0.625) == \"0.101\"\n assert candidate(0.1) == \"ERROR\"\n\n\ncheck(Solution().printBin)"}
| 100
| 51
|
coding
|
Solve the programming task below in a Python markdown code block.
A trick of fate caused Hatsumi and Taku to come to know each other. To keep the encounter in memory, they decided to calculate the difference between their ages. But the difference in ages varies depending on the day it is calculated. While trying again and again, they came to notice that the difference of their ages will hit a maximum value even though the months move on forever.
Given the birthdays for the two, make a program to report the maximum difference between their ages. The age increases by one at the moment the birthday begins. If the birthday coincides with the 29th of February in a leap year, the age increases at the moment the 1st of March arrives in non-leap years.
Input
The input is given in the following format.
y_1 m_1 d_1
y_2 m_2 d_2
The first and second lines provide Hatsumi’s and Taku’s birthdays respectively in year y_i (1 ≤ y_i ≤ 3000), month m_i (1 ≤ m_i ≤ 12), and day d_i (1 ≤ d_i ≤ Dmax) format. Where Dmax is given as follows:
* 28 when February in a non-leap year
* 29 when February in a leap-year
* 30 in April, June, September, and November
* 31 otherwise.
It is a leap year if the year represented as a four-digit number is divisible by 4. Note, however, that it is a non-leap year if divisible by 100, and a leap year if divisible by 400.
Output
Output the maximum difference between their ages.
Examples
Input
1999 9 9
2001 11 3
Output
3
Input
2008 2 29
2015 3 1
Output
8
|
{"inputs": ["0 1 2\n2 0 0", "0 0 2\n4 0 0", "0 1 2\n1 0 0", "0 1 2\n12 0 0", "0 -1 2\n2 0 0", "7 0 1\n-1 0 0", "-9 0 3\n58 0 1", "0 1 -7\n96 0 0"], "outputs": ["2\n", "4\n", "1\n", "12\n", "3\n", "9\n", "67\n", "96\n"]}
| 415
| 149
|
coding
|
Solve the programming task below in a Python markdown code block.
Simple enough this one - you will be given an array. The values in the array will either be numbers or strings, or a mix of both. You will not get an empty array, nor a sparse one.
Your job is to return a single array that has first the numbers sorted in ascending order, followed by the strings sorted in alphabetic order. The values must maintain their original type.
Note that numbers written as strings are strings and must be sorted with the other strings.
Also feel free to reuse/extend the following starter code:
```python
def db_sort(arr):
```
|
{"functional": "_inputs = [[[6, 2, 3, 4, 5]], [[14, 32, 3, 5, 5]], [[1, 2, 3, 4, 5]], [['Banana', 'Orange', 'Apple', 'Mango', 0, 2, 2]], [['C', 'W', 'W', 'W', 1, 2, 0]], [['Hackathon', 'Katathon', 'Code', 'CodeWars', 'Laptop', 'Macbook', 'JavaScript', 1, 5, 2]], [[66, 't', 101, 0, 1, 1]], [[78, 117, 110, 99, 104, 117, 107, 115, 4, 6, 5, 'west']], [[101, 45, 75, 105, 99, 107, 'y', 'no', 'yes', 1, 2, 4]], [[80, 117, 115, 104, 45, 85, 112, 115, 6, 7, 2]], [[1, 1, 1, 1, 1, 2, '1', '2', 'three', 1, 2, 3]], [[78, 33, 22, 44, 88, 9, 6, 0, 5, 0]], [[1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3]], [[82, 18, 72, 1, 11, 12, 12, 12, 12, 115, 667, 12, 2, 8, 3]], [['t', 'e', 's', 't', 3, 4, 1]], [['what', 'a', 'great', 'kata', 1, 2, 2]], [[66, 'codewars', 11, 'alex loves pushups', 2, 3, 0]], [['come', 'on', 110, '2500', 10, '!', 7, 15, 5, 6, 6]], [[\"when's\", 'the', 'next', 'Katathon?', 9, 7, 0, 1, 2]], [[8, 7, 5, 'bored', 'of', 'writing', 'tests', 115, 6, 7, 0]], [['anyone', 'want', 'to', 'hire', 'me?', 2, 4, 1]]]\n_outputs = [[[2, 3, 4, 5, 6]], [[3, 5, 5, 14, 32]], [[1, 2, 3, 4, 5]], [[0, 2, 2, 'Apple', 'Banana', 'Mango', 'Orange']], [[0, 1, 2, 'C', 'W', 'W', 'W']], [[1, 2, 5, 'Code', 'CodeWars', 'Hackathon', 'JavaScript', 'Katathon', 'Laptop', 'Macbook']], [[0, 1, 1, 66, 101, 't']], [[4, 5, 6, 78, 99, 104, 107, 110, 115, 117, 117, 'west']], [[1, 2, 4, 45, 75, 99, 101, 105, 107, 'no', 'y', 'yes']], [[2, 6, 7, 45, 80, 85, 104, 112, 115, 115, 117]], [[1, 1, 1, 1, 1, 1, 2, 2, 3, '1', '2', 'three']], [[0, 0, 5, 6, 9, 22, 33, 44, 78, 88]], [[1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 8, 9]], [[1, 2, 3, 8, 11, 12, 12, 12, 12, 12, 18, 72, 82, 115, 667]], [[1, 3, 4, 'e', 's', 't', 't']], [[1, 2, 2, 'a', 'great', 'kata', 'what']], [[0, 2, 3, 11, 66, 'alex loves pushups', 'codewars']], [[5, 6, 6, 7, 10, 15, 110, '!', '2500', 'come', 'on']], [[0, 1, 2, 7, 9, 'Katathon?', 'next', 'the', \"when's\"]], [[0, 5, 6, 7, 7, 8, 115, 'bored', 'of', 'tests', 'writing']], [[1, 2, 4, 'anyone', 'hire', 'me?', 'to', 'want']]]\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(db_sort(*i), o[0])"}
| 131
| 1,457
|
coding
|
Solve the programming task below in a Python markdown code block.
Given a positive integer, $n$, find and print the number of pairs of positive integers $(a,b)$, where $a<b$, that exist such that the equation $x\cdot a+y\cdot b=n$ (where $\boldsymbol{x}$ and $y$ are positive integers) has at least one solution.
Input Format
A single positive integer denoting $n$.
Constraints
$4\leq n\leq3\times10^5$
Output Format
Print a single integer denoting the number of such pairs.
Sample Input 0
4
Sample Output 0
2
Explanation 0
There are two such $(a,b)$ pairs: $(1,2)$ and $(1,3)$.
|
{"inputs": ["4\n"], "outputs": ["2\n"]}
| 166
| 14
|
coding
|
Solve the programming task below in a Python markdown code block.
New Year is coming in Line World! In this world, there are n cells numbered by integers from 1 to n, as a 1 × n board. People live in cells. However, it was hard to move between distinct cells, because of the difficulty of escaping the cell. People wanted to meet people who live in other cells.
So, user tncks0121 has made a transportation system to move between these cells, to celebrate the New Year. First, he thought of n - 1 positive integers a_1, a_2, ..., a_{n} - 1. For every integer i where 1 ≤ i ≤ n - 1 the condition 1 ≤ a_{i} ≤ n - i holds. Next, he made n - 1 portals, numbered by integers from 1 to n - 1. The i-th (1 ≤ i ≤ n - 1) portal connects cell i and cell (i + a_{i}), and one can travel from cell i to cell (i + a_{i}) using the i-th portal. Unfortunately, one cannot use the portal backwards, which means one cannot move from cell (i + a_{i}) to cell i using the i-th portal. It is easy to see that because of condition 1 ≤ a_{i} ≤ n - i one can't leave the Line World using portals.
Currently, I am standing at cell 1, and I want to go to cell t. However, I don't know whether it is possible to go there. Please determine whether I can go to cell t by only using the construted transportation system.
-----Input-----
The first line contains two space-separated integers n (3 ≤ n ≤ 3 × 10^4) and t (2 ≤ t ≤ n) — the number of cells, and the index of the cell which I want to go to.
The second line contains n - 1 space-separated integers a_1, a_2, ..., a_{n} - 1 (1 ≤ a_{i} ≤ n - i). It is guaranteed, that using the given transportation system, one cannot leave the Line World.
-----Output-----
If I can go to cell t using the transportation system, print "YES". Otherwise, print "NO".
-----Examples-----
Input
8 4
1 2 1 2 1 2 1
Output
YES
Input
8 5
1 2 1 2 1 1 1
Output
NO
-----Note-----
In the first sample, the visited cells are: 1, 2, 4; so we can successfully visit the cell 4.
In the second sample, the possible cells to visit are: 1, 2, 4, 6, 7, 8; so we can't visit the cell 5, which we want to visit.
|
{"inputs": ["3 2\n1 1\n", "3 2\n2 1\n", "3 3\n1 1\n", "3 3\n2 1\n", "3 2\n2 1\n", "3 3\n2 1\n", "3 2\n1 1\n", "3 3\n1 1\n"], "outputs": ["YES\n", "NO\n", "YES\n", "YES\n", "NO", "YES", "YES", "YES"]}
| 614
| 114
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
The variance of a string is defined as the largest difference between the number of occurrences of any 2 characters present in the string. Note the two characters may or may not be the same.
Given a string s consisting of lowercase English letters only, return the largest variance possible among all substrings of s.
A substring is a contiguous sequence of characters within a string.
Please complete the following python code precisely:
```python
class Solution:
def largestVariance(self, s: str) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(s = \"aababbb\") == 3\n assert candidate(s = \"abcde\") == 0\n\n\ncheck(Solution().largestVariance)"}
| 121
| 48
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.