task_type
stringclasses 1
value | problem
stringlengths 333
3.04k
| answer
stringlengths 38
3.95k
| problem_tokens
int64 69
769
| answer_tokens
int64 14
1.46k
|
|---|---|---|---|---|
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
|
coding
|
Solve the programming task below in a Python markdown code block.
Problem statement
Jota knows only the characters from `a` to` z` and the addition symbol `+`. Jota has an older sister, Tachiko. In addition to that, Tatsuko knows multiplication `*`, parentheses `(`, `)`, and integers (numbers) between `1` and` 9`. However, I don't know how to use multiple parentheses and how to multiply inside parentheses.
For example, if you have the following formula:
1. `a + a + a`
2. `a + 4 * (b + c)`
3. `a + 3 * (b + 2 * (c + d))`
4. `a-b`
5. `a / b`
6. `11 * a`
Of these, Jota-kun can write only 1., and Tachiko-san can write 1. and 2. Neither of 3 to 6 can write.
One day, Jota wrote a polynomial $ S $ with a length of $ n $ as a string. Tachiko, a short coder, wants to rewrite $ S $ as a string into a shorter identity polynomial $ T $, using multiplication, parentheses, and integers greater than or equal to `1` and less than or equal to` 9`. .. But it doesn't seem that easy.
Now, instead of Mr. Tachiko, write a program that creates the shortest $ T $ as a character string and outputs that length.
Constraint
$ 1 \ leq N \ leq 499 $
$ N $ is odd
$ S $ consists only of lowercase letters from `a` to` z` and `+`
The first letter of $ S $ is the alphabet, followed by the alternating `+` and $ 1 $ letters of the alphabet.
$ S $ contains less than $ 9 $ of the same alphabet
sample
Sample input 1
Five
a + a + a
Sample output 1
3
The $ 3 $ character of `a * 3` is the shortest.
Sample input 2
9
a + a + b + a + b
Sample output 2
7
The $ 7 $ character of `a * 3 + 2 * b` or` a * 3 + b + b` is the shortest.
Sample input 3
11
a + a + b + a + b + b
Sample output 3
7
$ 7 $ character in `(a + b) * 3`
Sample input 4
3
a + a
Sample output 4
3
$ 3 $ character with `a + a` or` a * 2`
input
$ N $
$ S $
output
Print the answer on the $ 1 $ line.
Example
Input
5
a+a+a
Output
3
|
{"inputs": ["5\nb+a+a", "5\nb+b+b", "5\na+a+b", "5\na+b+b", "5\nb+b+a", "5\nc+a+a", "5\nb+a+b", "5\nc+a+b"], "outputs": ["5\n", "3\n", "5\n", "5\n", "5\n", "5\n", "5\n", "5\n"]}
| 617
| 94
|
coding
|
Solve the programming task below in a Python markdown code block.
Call a number stepping if adjacent digits, as well as the first and last digits, differ by one. How many n-digit base 11 stepping numbers are there? Give your answer modulo 4294967143.
For example, 8789A9 is a 6-digit base 11 stepping number. 9A0A and 234 are not stepping.
Input
The first line contains an integer T, the number of test cases (about 20000). Each of the next T lines contains an integer n (2≤ n < 264).
Output
Output the answer to each test case on a separate line.
SAMPLE INPUT
4
2
4
6
10
SAMPLE OUTPUT
19
54
171
1958
|
{"inputs": ["4\n2\n4\n6\n10"], "outputs": ["19\n54\n171\n1958"]}
| 188
| 34
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given two integer arrays nums1 and nums2 sorted in non-decreasing order and an integer k.
Define a pair (u, v) which consists of one element from the first array and one element from the second array.
Return the k pairs (u1, v1), (u2, v2), ..., (uk, vk) with the smallest sums.
Please complete the following python code precisely:
```python
class Solution:
def kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]:
```
|
{"functional": "def check(candidate):\n assert candidate(nums1 = [1,7,11], nums2 = [2,4,6], k = 3) == [[1,2],[1,4],[1,6]]\n assert candidate(nums1 = [1,1,2], nums2 = [1,2,3], k = 2) == [[1,1],[1,1]]\n\n\ncheck(Solution().kSmallestPairs)"}
| 140
| 106
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given an array pairs, where pairs[i] = [xi, yi], and:
There are no duplicates.
xi < yi
Let ways be the number of rooted trees that satisfy the following conditions:
The tree consists of nodes whose values appeared in pairs.
A pair [xi, yi] exists in pairs if and only if xi is an ancestor of yi or yi is an ancestor of xi.
Note: the tree does not have to be a binary tree.
Two ways are considered to be different if there is at least one node that has different parents in both ways.
Return:
0 if ways == 0
1 if ways == 1
2 if ways > 1
A rooted tree is a tree that has a single root node, and all edges are oriented to be outgoing from the root.
An ancestor of a node is any node on the path from the root to that node (excluding the node itself). The root has no ancestors.
Please complete the following python code precisely:
```python
class Solution:
def checkWays(self, pairs: List[List[int]]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(pairs = [[1,2],[2,3]]) == 1\n assert candidate(pairs = [[1,2],[2,3],[1,3]]) == 2\n assert candidate(pairs = [[1,2],[2,3],[2,4],[1,5]]) == 0\n\n\ncheck(Solution().checkWays)"}
| 243
| 89
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given a string s and an integer k, partition s into k substrings such that the letter changes needed to make each substring a semi-palindrome are minimized.
Return the minimum number of letter changes required.
A semi-palindrome is a special type of string that can be divided into palindromes based on a repeating pattern. To check if a string is a semi-palindrome:
Choose a positive divisor d of the string's length. d can range from 1 up to, but not including, the string's length. For a string of length 1, it does not have a valid divisor as per this definition, since the only divisor is its length, which is not allowed.
For a given divisor d, divide the string into groups where each group contains characters from the string that follow a repeating pattern of length d. Specifically, the first group consists of characters at positions 1, 1 + d, 1 + 2d, and so on; the second group includes characters at positions 2, 2 + d, 2 + 2d, etc.
The string is considered a semi-palindrome if each of these groups forms a palindrome.
Consider the string "abcabc":
The length of "abcabc" is 6. Valid divisors are 1, 2, and 3.
For d = 1: The entire string "abcabc" forms one group. Not a palindrome.
For d = 2:
Group 1 (positions 1, 3, 5): "acb"
Group 2 (positions 2, 4, 6): "bac"
Neither group forms a palindrome.
For d = 3:
Group 1 (positions 1, 4): "aa"
Group 2 (positions 2, 5): "bb"
Group 3 (positions 3, 6): "cc"
All groups form palindromes. Therefore, "abcabc" is a semi-palindrome.
Please complete the following python code precisely:
```python
class Solution:
def minimumChanges(self, s: str, k: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(s = \"abcac\", k = 2) == 1\n assert candidate(s = \"abcdef\", k = 2) == 2\n assert candidate(s = \"aabbaa\", k = 3) == 0\n\n\ncheck(Solution().minimumChanges)"}
| 464
| 73
|
coding
|
Solve the programming task below in a Python markdown code block.
There are two groups, one of $N$ boys and the other of $N$ girls numbered from $1$ to $N$.
You are given two arrays $A$ and $B$ containing $N$ numbers each, denoting the height of boys and girls in the group.
You have to form $N$ couples, where each couple will consist of $1$ boy and $1$ girl.
Each couple has a LIKENESS VALUE, where
LIKENESS VALUE = height of girl in the couple + height of boy in that couple.
You have to form $N$ couples in such a way that the maximum of LIKENESS VALUE of all the couples is minimum.
Note:- No boy or girl can form more than one couple.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a single integer $N$:- number of boys and number of girls in the group.
The second line of each test case contains $N$ space-separated integers, denoting the height of $N$ boys.
The third line of each test case contains $N$ space-separated integers, denoting the height of $N$ girls.
------ Output ------
For each test case, print the maximum LIKENESS VALUE in a new line.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 2*10^{4}$
$1 ≤ A_{i}, B_{i} ≤ 10^{9}$ , for all $1 ≤ i ≤ N $
----- Sample Input 1 ------
1
3
4 5 1
2 2 2
----- Sample Output 1 ------
7
----- explanation 1 ------
You can form couples of boy and girl with index pair $(1,1), (2,2), (3,3)$. So, the Likeness value of the three couples will be $(4+2), (5+2), (1+2) = 6,7,3$. The maximum value will be $7$ and there are no other ways that can minimize the maximum value$(7)$.
|
{"inputs": ["1\n3\n4 5 1\n2 2 2"], "outputs": ["7"]}
| 482
| 26
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
We are playing the Guessing Game. The game will work as follows:
I pick a number between 1 and n.
You guess a number.
If you guess the right number, you win the game.
If you guess the wrong number, then I will tell you whether the number I picked is higher or lower, and you will continue guessing.
Every time you guess a wrong number x, you will pay x dollars. If you run out of money, you lose the game.
Given a particular n, return the minimum amount of money you need to guarantee a win regardless of what number I pick.
Please complete the following python code precisely:
```python
class Solution:
def getMoneyAmount(self, n: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(n = 10) == 16\n assert candidate(n = 1) == 0\n assert candidate(n = 2) == 1\n\n\ncheck(Solution().getMoneyAmount)"}
| 180
| 58
|
coding
|
Solve the programming task below in a Python markdown code block.
You have a tree of $n$ vertices. You are going to convert this tree into $n$ rubber bands on infinitely large plane. Conversion rule follows:
For every pair of vertices $a$ and $b$, rubber bands $a$ and $b$ should intersect if and only if there is an edge exists between $a$ and $b$ in the tree.
Shape of rubber bands must be a simple loop. In other words, rubber band is a loop which doesn't self-intersect.
Now let's define following things:
Rubber band $a$ includes rubber band $b$, if and only if rubber band $b$ is in rubber band $a$'s area, and they don't intersect each other.
Sequence of rubber bands $a_{1}, a_{2}, \ldots, a_{k}$ ($k \ge 2$) are nested, if and only if for all $i$ ($2 \le i \le k$), $a_{i-1}$ includes $a_{i}$.
This is an example of conversion. Note that rubber bands $5$ and $6$ are nested.
It can be proved that is it possible to make a conversion and sequence of nested rubber bands under given constraints.
What is the maximum length of sequence of nested rubber bands can be obtained from given tree? Find and print it.
-----Input-----
The first line contains integer $n$ ($3 \le n \le 10^{5}$) — the number of vertices in tree.
The $i$-th of the next $n-1$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$) — it means there is an edge between $a_{i}$ and $b_{i}$. It is guaranteed that given graph forms tree of $n$ vertices.
-----Output-----
Print the answer.
-----Examples-----
Input
6
1 3
2 3
3 4
4 5
4 6
Output
4
Input
4
1 2
2 3
3 4
Output
2
-----Note-----
In the first sample, you can obtain a nested sequence of $4$ rubber bands($1$, $2$, $5$, and $6$) by the conversion shown below. Of course, there are other conversions exist to make a nested sequence of $4$ rubber bands. However, you cannot make sequence of $5$ or more nested rubber bands with given tree.
You can see one of the possible conversions for the second sample below.
|
{"inputs": ["3\n1 3\n2 3\n", "3\n1 3\n2 3\n", "3\n1 2\n2 3\n", "4\n1 2\n2 3\n3 4\n", "4\n1 2\n2 4\n3 4\n", "4\n1 3\n2 4\n3 4\n", "4\n1 2\n1 4\n3 4\n", "4\n1 4\n2 4\n3 4\n"], "outputs": ["2\n", "2\n", "2\n", "2\n", "2\n", "2\n", "2\n", "3\n"]}
| 559
| 154
|
coding
|
Solve the programming task below in a Python markdown code block.
Chef wants to make a feast. In order to do that, he needs a lot of different ingredients. Each ingredient has a certain tastiness; the tastiness of each ingredient may be any positive integer. Initially, for each tastiness between $K$ and $K+N-1$ (inclusive), Chef has an infinite supply of ingredients with this tastiness.
The ingredients have a special property: any two of them can be mixed to create a new ingredient. If the original ingredients had tastiness $x$ and $y$ (possibly $x = y$), the new ingredient has tastiness $x+y$. The ingredients created this way may be used to mix other ingredients as well. Chef is free to mix ingredients in any way he chooses any number of times.
Let's call a tastiness $v$ ($v > 0$) unreachable if there is no way to obtain an ingredient with tastiness $v$; otherwise, tastiness $v$ is reachable. Chef wants to make ingredients with all reachable values of tastiness and he would like to know the number of unreachable values. Help him solve this problem. Since the answer may be large, compute it modulo $1,000,000,007$ ($10^9+7$).
Note that there is an infinite number of reachable values of tastiness, but it can be proven that the number of unreachable values is always finite for $N \ge 2$.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first and only line of each test case contains two space-separated integers $N$ and $K$.
-----Output-----
For each test case, print a single line containing one integer — the number of unreachable values of tastiness, modulo $1,000,000,007$.
-----Constraints-----
- $1 \le T \le 10^5$
- $2 \le N \le 10^{18}$
- $1 \le K \le 10^{18}$
-----Subtasks-----
Subtask #1 (20 points): $N = 2$
Subtask #2 (80 points): original constraints
-----Example Input-----
2
2 1
3 3
-----Example Output-----
0
2
-----Explanation-----
Example case 1: It is possible to obtain ingredients with all values of tastiness.
Example case 2: Ingredients with tastiness $1$ and $2$ cannot be made.
|
{"inputs": ["2\n2 1\n3 3"], "outputs": ["0\n2"]}
| 556
| 22
|
coding
|
Solve the programming task below in a Python markdown code block.
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya was delivered a string s, containing only digits. He needs to find a string that
* represents a lucky number without leading zeroes,
* is not empty,
* is contained in s as a substring the maximum number of times.
Among all the strings for which the three conditions given above are fulfilled, Petya only needs the lexicographically minimum one. Find this string for Petya.
Input
The single line contains a non-empty string s whose length can range from 1 to 50, inclusive. The string only contains digits. The string can contain leading zeroes.
Output
In the only line print the answer to Petya's problem. If the sought string does not exist, print "-1" (without quotes).
Examples
Input
047
Output
4
Input
16
Output
-1
Input
472747
Output
7
Note
The lexicographical comparison of strings is performed by the < operator in the modern programming languages. String x is lexicographically less than string y either if x is a prefix of y, or exists such i (1 ≤ i ≤ min(|x|, |y|)), that xi < yi and for any j (1 ≤ j < i) xj = yj. Here |a| denotes the length of string a.
In the first sample three conditions are fulfilled for strings "4", "7" and "47". The lexicographically minimum one is "4".
In the second sample s has no substrings which are lucky numbers.
In the third sample the three conditions are only fulfilled for string "7".
|
{"inputs": ["7\n", "4\n", "5\n", "9\n", "8\n", "2\n", "3\n", "0\n"], "outputs": ["7\n", "4\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n"]}
| 417
| 70
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given two strings a and b, return the length of the longest uncommon subsequence between a and b. If no such uncommon subsequence exists, return -1.
An uncommon subsequence between two strings is a string that is a subsequence of exactly one of them.
Please complete the following python code precisely:
```python
class Solution:
def findLUSlength(self, a: str, b: str) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(a = \"aaa\", b = \"bbb\") == 3\n assert candidate(a = \"aaa\", b = \"aaa\") == -1\n\n\ncheck(Solution().findLUSlength)"}
| 107
| 55
|
coding
|
Solve the programming task below in a Python markdown code block.
Faizal and Sultan are having a one-on-one faceoff. Before stepping into the fight, each of them smoked a special kind of weed from Jhalwa. Smoking k grams of this weed grants the smoker the ability to resurrect k times after dying. Sultan smoked M grams of this weed, and Faizal smoked N grams.
However, Faizal was addicted to this kind of weed since his childhood, and so developed a side effect: if Faizal dies and is resurrected, he temporarily loses his ability to resurrect; and only gains it back after killing Sultan at least once (given that he hasn't resurrected N times already). Sultan has no such restriction.
The fight ends whenever one of the two is killed without being able to resurrect. In other words, either Sultan is killed M+1 times, or Faizal is killed N+1 times, or Faizal is killed twice in a row.
Any possible fight can be described by the sequence of deaths - for example, with N = 2 and M = 2, the string FSSFS encodes the fight where:
1. First, Sultan kills Faizal. Faizal resurrects, and temporarily loses his ability to resurrect.
2. Then, Faizal kills Sultan. Sultan resurrects, and Faizal's resurrection is now active.
3. Faizal kills Sultan again. Sultan resurrects.
4. Sultan kills Faizal, who resurrects. Now Sultan has no more chances to revive.
5. Faizal kills Sultan, who stays dead; and the fight ends.
Faizal's wife Mohsina is getting anxious about the fight and asks for your help in finding the number of different ways the fight between Faizal and Sultan can end up happening. Because this value can be quite large, compute it modulo 10^{9} + 7.
Two fights are considered different if and only if the sequence of deaths corresponding to those fights are different. For example, the sequence SF is different from the sequence FS and hence these represent different fights.
------ 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 two space-separated integers N and M.
------ Output Format ------
For each test case, output a single line containing the answer - the possible number of fights between Faizal and Sultan, modulo 10^{9} + 7.
------ Constraints ------
$1 ≤ T ≤ 2000$
$0 ≤ N ≤ 10^{5}$
$0 ≤ M ≤ 10^{5}$
- Sum of $N$ over all test cases will not exceed $10^{6}$
- Sum of $M$ over all test cases will not exceed $10^{6}$
----- Sample Input 1 ------
3
2 1
12 6
6969 8563
----- Sample Output 1 ------
7
255
988402422
----- explanation 1 ------
Test Case 1: The death sequences corresponding to the $7$ possible fights are:
- SS
- SFS
- FSS
- FSFS
- FF
- SFF
- FSFF
|
{"inputs": ["3\n2 1\n12 6\n6969 8563"], "outputs": ["7\n255\n988402422\n"]}
| 711
| 46
|
coding
|
Solve the programming task below in a Python markdown code block.
```if:python,php
In this kata you will have to write a function that takes `litres` and `price_per_litre` as arguments. Purchases of 2 or more litres get a discount of 5 cents per litre, purchases of 4 or more litres get a discount of 10 cents per litre, and so on every two litres, up to a maximum discount of 25 cents per litre. But total discount per litre cannot be more than 25 cents. Return the toal cost rounded to 2 decimal places. Also you can guess that there will not be negative or non-numeric inputs.
Good Luck!
```
```if:csharp,java,javascript
In this kata you will have to write a function that takes `litres` and `pricePerLitre` as arguments. Purchases of 2 or more litres get a discount of 5 cents per litre, purchases of 4 or more litres get a discount of 10 cents per litre, and so on every two litres, up to a maximum discount of 25 cents per litre. But total discount per litre cannot be more than 25 cents. Return the toal cost rounded to 2 decimal places. Also you can guess that there will not be negative or non-numeric inputs.
Good Luck!
```
Also feel free to reuse/extend the following starter code:
```python
def fuel_price(litres, price_per_liter):
```
|
{"functional": "_inputs = [[10, 21.5], [40, 10], [15, 5.83]]\n_outputs = [[212.5], [390], [83.7]]\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(fuel_price(*i), o[0])"}
| 315
| 195
|
coding
|
Solve the programming task below in a Python markdown code block.
[Haikus](https://en.wikipedia.org/wiki/Haiku_in_English) are short poems in a three-line format, with 17 syllables arranged in a 5–7–5 pattern. Your task is to check if the supplied text is a haiku or not.
### About syllables
[Syllables](https://en.wikipedia.org/wiki/Syllable) are the phonological building blocks of words. *In this kata*, a syllable is a part of a word including a vowel ("a-e-i-o-u-y") or a group of vowels (e.g. "ou", "ee", "ay"). A few examples: "tea", "can", "to·day", "week·end", "el·e·phant".
**However**, silent "E"s **do not** create syllables. *In this kata*, an "E" is considered silent if it's alone at the end of the word, preceded by one (or more) consonant(s) and there is at least one other syllable in the word. Examples: "age", "ar·range", "con·crete"; but not in "she", "blue", "de·gree".
Some more examples:
* one syllable words: "cat", "cool", "sprout", "like", "eye", "squeeze"
* two syllables words: "ac·count", "hon·est", "beau·ty", "a·live", "be·cause", "re·store"
## Examples
```
An old silent pond...
A frog jumps into the pond,
splash! Silence again.
```
...should return `True`, as this is a valid 5–7–5 haiku:
```
An old si·lent pond... # 5 syllables
A frog jumps in·to the pond, # 7
splash! Si·lence a·gain. # 5
```
Another example:
```
Autumn moonlight -
a worm digs silently
into the chestnut.
```
...should return `False`, because the number of syllables per line is not correct:
```
Au·tumn moon·light - # 4 syllables
a worm digs si·lent·ly # 6
in·to the chest·nut. # 5
```
---
## My other katas
If you enjoyed this kata then please try [my other katas](https://www.codewars.com/collections/katas-created-by-anter69)! :-)
Also feel free to reuse/extend the following starter code:
```python
def is_haiku(text):
```
|
{"functional": "_inputs = [['An old silent pond...\\nA frog jumps into the pond,\\nsplash! Silence again.'], ['An old silent pond...\\nA frog jumps into the pond, splash!\\nSilence again.'], ['An old silent pond...\\nA frog jumps into the pond,\\nsplash!\\nSilence again.'], ['An old silent pond... A frog jumps into the pond, splash! Silence again.'], ['Autumn moonlight -\\na worm digs silently\\ninto the chestnut.'], [''], ['\\n\\n'], ['My code is cool, right?\\nJava # Pyhton ; Ruby // Go:\\nI know them all, yay! ;-)'], ['Edge case the urge come;\\nFurthermore eye the garage.\\nLike literature!'], ['a e i o u\\noo ee ay ie ey oa ie\\ny a e i o']]\n_outputs = [[True], [False], [False], [False], [False], [False], [False], [True], [True], [True]]\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_haiku(*i), o[0])"}
| 571
| 370
|
coding
|
Solve the programming task below in a Python markdown code block.
Sereja and Dima play a game. The rules of the game are very simple. The players have n cards in a row. Each card contains a number, all numbers on the cards are distinct. The players take turns, Sereja moves first. During his turn a player can take one card: either the leftmost card in a row, or the rightmost one. The game ends when there is no more cards. The player who has the maximum sum of numbers on his cards by the end of the game, wins.
Sereja and Dima are being greedy. Each of them chooses the card with the larger number during his move.
Inna is a friend of Sereja and Dima. She knows which strategy the guys are using, so she wants to determine the final score, given the initial state of the game. Help her.
-----Input-----
The first line contains integer n (1 ≤ n ≤ 1000) — the number of cards on the table. The second line contains space-separated numbers on the cards from left to right. The numbers on the cards are distinct integers from 1 to 1000.
-----Output-----
On a single line, print two integers. The first number is the number of Sereja's points at the end of the game, the second number is the number of Dima's points at the end of the game.
-----Examples-----
Input
4
4 1 2 10
Output
12 5
Input
7
1 2 3 4 5 6 7
Output
16 12
-----Note-----
In the first sample Sereja will take cards with numbers 10 and 2, so Sereja's sum is 12. Dima will take cards with numbers 4 and 1, so Dima's sum is 5.
|
{"inputs": ["1\n3\n", "1\n1\n", "1\n1\n", "1\n3\n", "1\n2\n", "1\n4\n", "1\n0\n", "4\n4 1 2 10\n"], "outputs": ["3 0\n", "1 0\n", "1 0\n", "3 0\n", "2 0\n", "4 0\n", "0 0\n", "12 5\n"]}
| 403
| 110
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given an integer array pref of size n. Find and return the array arr of size n that satisfies:
pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i].
Note that ^ denotes the bitwise-xor operation.
It can be proven that the answer is unique.
Please complete the following python code precisely:
```python
class Solution:
def findArray(self, pref: List[int]) -> List[int]:
```
|
{"functional": "def check(candidate):\n assert candidate(pref = [5,2,0,3,1]) == [5,7,2,3,2]\n assert candidate(pref = [13]) == [13]\n\n\ncheck(Solution().findArray)"}
| 111
| 63
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problems statements in Mandarin Chinese and Russian as well.
You are given a function f which is defined as :
Your task is to find the value of
where M is given in input.
------ Input Format ------
First line contains T, the number of test cases.
First line of each test case contain 3 space separated integers N, M and Q.
Next Q line follows, each line contain r.
------ Output Format ------
For each test case, output Q lines, each line containing the required answer.
------ Constraints ------
2 ≤ N ≤ 10^{6}
1 ≤ M ≤ 10^{9}
2 ≤ Sum of N over all test cases ≤ 10^{6}
1 ≤ Sum of Q over all test cases ≤ 2*10^{5}
1 ≤ T ≤ 10^{5}
1 < r < N
Sample Input
2
5 114 1
2
50 3874 3
31
17
21
Sample Output
72
3718
624
1144
Explanation for first test case
f[1] = 1
f[2] = 2
f[3] = 1*2^{2} * 3 = 12
f[4] =1*2^{3}*3^{2}*4 = 8*9*4 = 288
f[5] = 1*2^{4}*3^{3}*4^{2}*5 =34560
value of f[5] / (f[2]*f[3]) = 1440 and 1440 %114 is 72
|
{"inputs": ["2\n5 114 1\n2\n50 3874 3\n31\n17\n21"], "outputs": ["72\n3718\n624\n1144"]}
| 381
| 56
|
coding
|
Solve the programming task below in a Python markdown code block.
Given an array A having N elements, a subarray S of A is called good if XOR(S) ≥ K, where XOR(S) denotes the [bitwise XOR] of all the elements of the subarray S.
Find the length of the smallest subarray S which is good. If there is no good subarray, print -1 instead.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of the T test cases follows.
- The first line of each test case contains two space-separated integers N and K - as described in the problem statement.
- The second line of each test case contains N space-separated integers A_{1}, A_{2},..., A_{N}, A_{i} representing the elements of the array A.
------ Output Format ------
For each test case, output in a single line, the length of the shortest subarray which is good. If there is no good subarray, print -1.
------ Constraints ------
$1 ≤ T ≤ 2 \cdot 10^{4}$
$1 ≤ N ≤ 10^{5}$
$0 ≤ A_{i} < 2^{30}$
$0 ≤ K < 2^{31}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
5 15
1 4 2 8 1
5 7
1 4 2 8 1
5 20
1 4 2 8 1
----- Sample Output 1 ------
4
1
-1
----- explanation 1 ------
Test case $1$:
For $S = [A_{1}, A_{2}, A_{3}, A_{4}]$, $XOR(S) = 1\oplus 4\oplus 2\oplus 8 = 15$.
Similarly, for $S = [A_{2}, A_{3}, A_{4}, A_{5}]$, $XOR(S) = 4\oplus 2\oplus 8\oplus 1 = 15$.
These are the only two subarrays which are good. The length of these subarrays is $4$. It can be proven that no subarray of length less than $4$ is good.
Test case $2$: There are several good subarrays. Some examples are $S = [A_{4}]$, $S = [A_{1}, A_{2}, A_{3}]$,
and $S = [A_{3}, A_{4}]$. The length of the smallest subarray is $1$, and that subarray is $[A_{4}]$. It can be proven that no subarray of length less than $1$ is good.
Test case $3$: There is no subarray $S$ such that $XOR(S) ≥ 20$. Hence, there is no good subarray.
|
{"inputs": ["3\n5 15\n1 4 2 8 1\n5 7\n1 4 2 8 1\n5 20\n1 4 2 8 1"], "outputs": ["4\n1\n-1"]}
| 657
| 61
|
coding
|
Solve the programming task below in a Python markdown code block.
Mister B once received a gift: it was a book about aliens, which he started read immediately. This book had c pages.
At first day Mister B read v_0 pages, but after that he started to speed up. Every day, starting from the second, he read a pages more than on the previous day (at first day he read v_0 pages, at second — v_0 + a pages, at third — v_0 + 2a pages, and so on). But Mister B is just a human, so he physically wasn't able to read more than v_1 pages per day.
Also, to refresh his memory, every day, starting from the second, Mister B had to reread last l pages he read on the previous day. Mister B finished the book when he read the last page for the first time.
Help Mister B to calculate how many days he needed to finish the book.
-----Input-----
First and only line contains five space-separated integers: c, v_0, v_1, a and l (1 ≤ c ≤ 1000, 0 ≤ l < v_0 ≤ v_1 ≤ 1000, 0 ≤ a ≤ 1000) — the length of the book in pages, the initial reading speed, the maximum reading speed, the acceleration in reading speed and the number of pages for rereading.
-----Output-----
Print one integer — the number of days Mister B needed to finish the book.
-----Examples-----
Input
5 5 10 5 4
Output
1
Input
12 4 12 4 1
Output
3
Input
15 1 100 0 0
Output
15
-----Note-----
In the first sample test the book contains 5 pages, so Mister B read it right at the first day.
In the second sample test at first day Mister B read pages number 1 - 4, at second day — 4 - 11, at third day — 11 - 12 and finished the book.
In third sample test every day Mister B read 1 page of the book, so he finished in 15 days.
|
{"inputs": ["1 1 1 0 0\n", "4 1 2 2 0\n", "1 5 5 1 1\n", "1 2 3 0 0\n", "1 1 1 1 0\n", "9 1 4 2 0\n", "4 2 2 0 1\n", "8 3 4 2 0\n"], "outputs": ["1\n", "3\n", "1\n", "1\n", "1\n", "4\n", "3\n", "3\n"]}
| 482
| 134
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given n item's value and label as two integer arrays values and labels. You are also given two integers numWanted and useLimit.
Your task is to find a subset of items with the maximum sum of their values such that:
The number of items is at most numWanted.
The number of items with the same label is at most useLimit.
Return the maximum sum.
Please complete the following python code precisely:
```python
class Solution:
def largestValsFromLabels(self, values: List[int], labels: List[int], numWanted: int, useLimit: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(values = [5,4,3,2,1], labels = [1,1,2,2,3], numWanted = 3, useLimit = 1) == 9\n assert candidate(values = [5,4,3,2,1], labels = [1,3,3,3,2], numWanted = 3, useLimit = 2) == 12\n assert candidate(values = [9,8,8,7,6], labels = [0,0,0,1,1], numWanted = 3, useLimit = 1) == 16\n\n\ncheck(Solution().largestValsFromLabels)"}
| 145
| 162
|
coding
|
Solve the programming task below in a Python markdown code block.
Round any given number to the closest 0.5 step
I.E.
```
solution(4.2) = 4
solution(4.3) = 4.5
solution(4.6) = 4.5
solution(4.8) = 5
```
Round **up** if number is as close to previous and next 0.5 steps.
```
solution(4.75) == 5
```
Also feel free to reuse/extend the following starter code:
```python
def solution(n):
```
|
{"functional": "_inputs = [[4.2], [4.25], [4.4], [4.6], [4.75], [4.8], [4.5], [4.55], [4.74], [4.74999999999], [4.74999999991]]\n_outputs = [[4], [4.5], [4.5], [4.5], [5], [5], [4.5], [4.5], [4.5], [4.5], [4.5]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(solution(*i), o[0])"}
| 131
| 275
|
coding
|
Solve the programming task below in a Python markdown code block.
International Abbreviation Olympiad takes place annually starting from 1989. Each year the competition receives an abbreviation of form IAO'y, where y stands for some number of consequent last digits of the current year. Organizers always pick an abbreviation with non-empty string y that has never been used before. Among all such valid abbreviations they choose the shortest one and announce it to be the abbreviation of this year's competition.
For example, the first three Olympiads (years 1989, 1990 and 1991, respectively) received the abbreviations IAO'9, IAO'0 and IAO'1, while the competition in 2015 received an abbreviation IAO'15, as IAO'5 has been already used in 1995.
You are given a list of abbreviations. For each of them determine the year it stands for.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the number of abbreviations to process.
Then n lines follow, each containing a single abbreviation. It's guaranteed that each abbreviation contains at most nine digits.
Output
For each abbreviation given in the input, find the year of the corresponding Olympiad.
Examples
Input
5
IAO'15
IAO'2015
IAO'1
IAO'9
IAO'0
Output
2015
12015
1991
1989
1990
Input
4
IAO'9
IAO'99
IAO'999
IAO'9999
Output
1989
1999
2999
9999
|
{"inputs": ["1\nIAO'001\n", "1\nIAO'089\n", "1\nIAO'000\n", "1\nIAO'2000\n", "1\nIAO'2015\n", "1\nIAO'1015\n", "1\nIAO'3015\n", "1\nIAO'1014\n"], "outputs": ["3001\n", "3089\n", "3000\n", "12000\n", "12015\n", "11015\n", "13015\n", "11014\n"]}
| 395
| 160
|
coding
|
Solve the programming task below in a Python markdown code block.
It's almost summertime, so Big Cat and Little Cat are getting in shape. They decide the core of their fitness plan is to start jogging every day.
Their city consists of $N$ intersections connected by $\mbox{M}$ bidirectional roads. The cats decide that their jogging route should be cyclic (i.e., starting and ending at the same intersection) and consist of $4$ different roads.
The cats also love exploring new places, so each day they want to choose a new route to jog on that is not equal to any of their previous routes. Two routes are considered to be equal if their sets of component roads are equal.
Given a map of the city, can you help our heroic cats determine the maximum number of days they can go jogging so that every route traveled is different?
Input Format
The first line contains a pair of space-separated integers, $N$ (the number of intersections) and $\mbox{M}$ (the number of roads), respectively.
Each line $\boldsymbol{i}$ of the $\mbox{M}$ subsequent lines contains a pair of space-separated integers, $X_i$ and $Y_i$, defining a bidirectional road connecting intersections $X_i$ and $Y_i$.
Constraints
$1\leq N\leq5\cdot10^4$
$1\leq M\leq10^5$
$1\leq X_i,Y_i\leq N$
Each bidirectional road connects $2$ distinct intersections (i.e., no road connects an intersection to itself).
Each pair of intersections is directly connected by no more than $1$ road.
Output Format
Print the maximum number of days for which the cats can go jogging without repeating a route.
Sample Input
4 6
1 2
2 3
3 4
4 1
1 3
2 4
Sample Output
3
Explanation
There are $3$ different routes:
$1\to2\to3\to4\to1$
$1\to3\to2\to4\to1$
$1\to2\to4\to3\to1$
Recall that each route is a set of intersections forming a cycle, so each unique route is the same regardless of which city on the route the cats start out at. Thus, we print $3$ (the number of routes) as our answer.
|
{"inputs": ["4 6\n1 2\n2 3\n3 4\n4 1\n1 3\n2 4\n"], "outputs": ["3\n"]}
| 513
| 40
|
coding
|
Solve the programming task below in a Python markdown code block.
For a given weighted graph G(V, E) and a source r, find the source shortest path to each vertex from the source (SSSP: Single Source Shortest Path).
Constraints
* 1 ≤ |V| ≤ 100000
* 0 ≤ di ≤ 10000
* 0 ≤ |E| ≤ 500000
* There are no parallel edges
* There are no self-loops
Input
An edge-weighted graph G (V, E) and the source r.
|V| |E| r
s0 t0 d0
s1 t1 d1
:
s|E|-1 t|E|-1 d|E|-1
|V| is the number of vertices and |E| is the number of edges in G. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively. r is the source of the graph.
si and ti represent source and target vertices of i-th edge (directed) and di represents the cost of the i-th edge.
Output
Print the costs of SSSP in the following format.
c0
c1
:
c|V|-1
The output consists of |V| lines. Print the cost of the shortest path from the source r to each vertex 0, 1, ... |V|-1 in order. If there is no path from the source to a vertex, print INF.
Examples
Input
4 5 0
0 1 1
0 2 4
1 2 2
2 3 1
1 3 5
Output
0
1
3
4
Input
4 6 1
0 1 1
0 2 4
2 0 1
1 2 2
3 1 1
3 2 5
Output
3
0
2
INF
|
{"inputs": ["4 5 0\n0 1 1\n0 2 4\n1 2 2\n2 3 1\n1 3 2", "4 5 0\n0 1 1\n0 2 4\n1 2 0\n2 3 1\n1 3 5", "4 5 0\n0 1 1\n1 2 4\n1 1 0\n2 3 1\n1 3 2", "4 5 0\n0 1 1\n1 2 4\n1 1 0\n2 3 1\n0 3 2", "4 5 0\n0 1 1\n1 2 5\n1 1 0\n2 3 1\n0 3 2", "4 5 0\n0 2 1\n0 3 4\n1 2 2\n2 2 2\n1 3 2", "4 5 0\n0 2 2\n0 3 4\n1 3 2\n3 2 2\n1 3 2", "4 5 0\n0 2 2\n0 3 4\n0 3 2\n3 2 2\n1 3 2"], "outputs": ["0\n1\n3\n3\n", "0\n1\n1\n2\n", "0\n1\n5\n3\n", "0\n1\n5\n2\n", "0\n1\n6\n2\n", "0\nINF\n1\n4\n", "0\nINF\n2\n4\n", "0\nINF\n2\n2\n"]}
| 426
| 382
|
coding
|
Solve the programming task below in a Python markdown code block.
The Robot is in a rectangular maze of size n × m. Each cell of the maze is either empty or occupied by an obstacle. The Robot can move between neighboring cells on the side left (the symbol "L"), right (the symbol "R"), up (the symbol "U") or down (the symbol "D"). The Robot can move to the cell only if it is empty. Initially, the Robot is in the empty cell.
Your task is to find lexicographically minimal Robot's cycle with length exactly k, which begins and ends in the cell where the Robot was initially. It is allowed to the Robot to visit any cell many times (including starting).
Consider that Robot's way is given as a line which consists of symbols "L", "R", "U" and "D". For example, if firstly the Robot goes down, then left, then right and up, it means that his way is written as "DLRU".
In this task you don't need to minimize the length of the way. Find the minimum lexicographical (in alphabet order as in the dictionary) line which satisfies requirements above.
-----Input-----
The first line contains three integers n, m and k (1 ≤ n, m ≤ 1000, 1 ≤ k ≤ 10^6) — the size of the maze and the length of the cycle.
Each of the following n lines contains m symbols — the description of the maze. If the symbol equals to "." the current cell is empty. If the symbol equals to "*" the current cell is occupied by an obstacle. If the symbol equals to "X" then initially the Robot is in this cell and it is empty. It is guaranteed that the symbol "X" is found in the maze exactly once.
-----Output-----
Print the lexicographically minimum Robot's way with the length exactly k, which starts and ends in the cell where initially Robot is. If there is no such way, print "IMPOSSIBLE"(without quotes).
-----Examples-----
Input
2 3 2
.**
X..
Output
RL
Input
5 6 14
..***.
*...X.
..*...
..*.**
....*.
Output
DLDDLLLRRRUURU
Input
3 3 4
***
*X*
***
Output
IMPOSSIBLE
-----Note-----
In the first sample two cyclic ways for the Robot with the length 2 exist — "UD" and "RL". The second cycle is lexicographically less.
In the second sample the Robot should move in the following way: down, left, down, down, left, left, left, right, right, right, up, up, right, up.
In the third sample the Robot can't move to the neighboring cells, because they are occupied by obstacles.
|
{"inputs": ["1 1 1\nX\n", "1 1 1\nX\n", "1 1 2\nX\n", "1 1 1\nX\n", "1 1 2\nX\n", "1 2 2\nX.\n", "1 2 2\n.X\n", "1 2 2\nX*\n"], "outputs": ["IMPOSSIBLE\n", "IMPOSSIBLE\n", "IMPOSSIBLE\n", "IMPOSSIBLE\n", "IMPOSSIBLE\n", "RL\n", "LR\n", "IMPOSSIBLE\n"]}
| 595
| 138
|
coding
|
Solve the programming task below in a Python markdown code block.
There is a grid with N rows and N columns of squares. Let (i, j) be the square at the i-th row from the top and the j-th column from the left.
Each of the central (N-2) \times (N-2) squares in the grid has a black stone on it.
Each of the 2N - 1 squares on the bottom side and the right side has a white stone on it.
Q queries are given. We ask you to process them in order.
There are two kinds of queries. Their input format and description are as follows:
- 1 x: Place a white stone on (1, x). After that, for each black stone between (1, x) and the first white stone you hit if you go down from (1, x), replace it with a white stone.
- 2 x: Place a white stone on (x, 1). After that, for each black stone between (x, 1) and the first white stone you hit if you go right from (x, 1), replace it with a white stone.
How many black stones are there on the grid after processing all Q queries?
-----Constraints-----
- 3 \leq N \leq 2\times 10^5
- 0 \leq Q \leq \min(2N-4,2\times 10^5)
- 2 \leq x \leq N-1
- Queries are pairwise distinct.
-----Input-----
Input is given from Standard Input in the following format:
N Q
Query_1
\vdots
Query_Q
-----Output-----
Print how many black stones there are on the grid after processing all Q queries.
-----Sample Input-----
5 5
1 3
2 3
1 4
2 2
1 2
-----Sample Output-----
1
After each query, the grid changes in the following way:
|
{"inputs": ["39 0", "630 0", "279 0", "328 0", "272 0", "173 0", "218 0", "674 0"], "outputs": ["1369", "394384", "76729", "106276", "72900", "29241", "46656", "451584"]}
| 419
| 119
|
coding
|
Solve the programming task below in a Python markdown code block.
In this kata you're expected to sort an array of 32-bit integers in ascending order of the number of **on** bits they have.
E.g Given the array **[7, 6, 15, 8]**
- 7 has **3 on** bits (000...0**111**)
- 6 has **2 on** bits (000...00**11**)
- 15 has **4 on** bits (000...**1111**)
- 8 has **1 on** bit (000...**1**000)
So the array in sorted order would be **[8, 6, 7, 15]**.
In cases where two numbers have the same number of bits, compare their real values instead.
E.g between 10 **(...1010)** and 12 **(...1100)**, they both have the same number of **on** bits '**2**' but the integer 10 is less than 12 so it comes first in sorted order.
Your task is to write the function `sortBybit()` that takes an array of integers and sort them as described above.
```if-not:haskell
Note: Your function should modify the input rather than creating a new array.
```
Also feel free to reuse/extend the following starter code:
```python
def sort_by_bit(arr):
```
|
{"functional": "_inputs = [[[3, 8, 3, 6, 5, 7, 9, 1]], [[9, 4, 5, 3, 5, 7, 2, 56, 8, 2, 6, 8, 0]]]\n_outputs = [[[1, 8, 3, 3, 5, 6, 9, 7]], [[0, 2, 2, 4, 8, 8, 3, 5, 5, 6, 9, 7, 56]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(sort_by_bit(*i), o[0])"}
| 321
| 277
|
coding
|
Solve the programming task below in a Python markdown code block.
Vladik was bored on his way home and decided to play the following game. He took n cards and put them in a row in front of himself. Every card has a positive integer number not exceeding 8 written on it. He decided to find the longest subsequence of cards which satisfies the following conditions:
the number of occurrences of each number from 1 to 8 in the subsequence doesn't differ by more then 1 from the number of occurrences of any other number. Formally, if there are c_{k} cards with number k on them in the subsequence, than for all pairs of integers $i \in [ 1,8 ], j \in [ 1,8 ]$ the condition |c_{i} - c_{j}| ≤ 1 must hold. if there is at least one card with number x on it in the subsequence, then all cards with number x in this subsequence must form a continuous segment in it (but not necessarily a continuous segment in the original sequence). For example, the subsequence [1, 1, 2, 2] satisfies this condition while the subsequence [1, 2, 2, 1] doesn't. Note that [1, 1, 2, 2] doesn't satisfy the first condition.
Please help Vladik to find the length of the longest subsequence that satisfies both conditions.
-----Input-----
The first line contains single integer n (1 ≤ n ≤ 1000) — the number of cards in Vladik's sequence.
The second line contains the sequence of n positive integers not exceeding 8 — the description of Vladik's sequence.
-----Output-----
Print single integer — the length of the longest subsequence of Vladik's sequence that satisfies both conditions.
-----Examples-----
Input
3
1 1 1
Output
1
Input
8
8 7 6 5 4 3 2 1
Output
8
Input
24
1 8 1 2 8 2 3 8 3 4 8 4 5 8 5 6 8 6 7 8 7 8 8 8
Output
17
-----Note-----
In the first sample all the numbers written on the cards are equal, so you can't take more than one card, otherwise you'll violate the first condition.
|
{"inputs": ["1\n8\n", "1\n8\n", "1\n7\n", "1\n6\n", "2\n5 4\n", "2\n5 4\n", "2\n5 1\n", "2\n5 2\n"], "outputs": ["1", "1", "1\n", "1\n", "2", "2", "2\n", "2\n"]}
| 520
| 90
|
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 type this string using your keyboard.
Initially, you have an empty string. Until you type the whole string, you may perform the following operation: add a character to the end of the string.
Besides, at most once you may perform one additional operation: copy the string and append it to itself.
For example, if you have to type string abcabca, you can type it in 7 operations if you type all the characters one by one. However, you can type it in 5 operations if you type the string abc first and then copy it and type the last character.
If you have to type string aaaaaaaaa, the best option is to type 4 characters one by one, then copy the string, and then type the remaining character.
Print the minimum number of operations you need to type the given string.
-----Input-----
The first line of the input containing only one integer number n (1 ≤ n ≤ 100) — the length of the string you have to type. The second line containing the string s consisting of n lowercase Latin letters.
-----Output-----
Print one integer number — the minimum number of operations you need to type the given string.
-----Examples-----
Input
7
abcabca
Output
5
Input
8
abcdefgh
Output
8
-----Note-----
The first test described in the problem statement.
In the second test you can only type all the characters one by one.
|
{"inputs": ["1\na\n", "1\ns\n", "1\ns\n", "1\na\n", "1\nt\n", "1\n`\n", "2\naa\n", "2\naa\n"], "outputs": ["1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "2\n", "2\n"]}
| 322
| 86
|
coding
|
Solve the programming task below in a Python markdown code block.
For an array of non-negative integers $a$ of size $n$, we construct another array $d$ as follows: $d_1 = a_1$, $d_i = |a_i - a_{i - 1}|$ for $2 \le i \le n$.
Your task is to restore the array $a$ from a given array $d$, or to report that there are multiple possible arrays.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 100$) — the size of the arrays $a$ and $d$.
The second line contains $n$ integers $d_1, d_2, \dots, d_n$ ($0 \le d_i \le 100$) — the elements of the array $d$.
It can be shown that there always exists at least one suitable array $a$ under these constraints.
-----Output-----
For each test case, print the elements of the array $a$, if there is only one possible array $a$. Otherwise, print $-1$.
-----Examples-----
Input
3
4
1 0 2 5
3
2 6 3
5
0 0 0 0 0
Output
1 1 3 8
-1
0 0 0 0 0
-----Note-----
In the second example, there are two suitable arrays: $[2, 8, 5]$ and $[2, 8, 11]$.
|
{"inputs": ["1\n2\n5 5\n", "1\n2\n1 1\n", "1\n2\n3 3\n", "1\n2\n2 2\n", "1\n2\n4 4\n", "1\n2\n7 7\n", "1\n2\n2 3\n", "1\n2\n6 6\n"], "outputs": ["-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "2 5\n", "-1\n"]}
| 373
| 121
|
coding
|
Solve the programming task below in a Python markdown code block.
------ Problem Statement ------
Write a program that accepts sets of three numbers, and prints the second-maximum number among the three.
------ Input ------
First line contains the number of triples, N.
The next N lines which follow each have three space separated integers.
------ Output ------
For each of the N triples, output one new line which contains the second-maximum integer among the three.
------ Constraints ------
$1 ≤ N ≤ 6$
$1 ≤ every integer ≤ 10000$
$The three integers in a single triplet are all distinct. That is, no two of them are equal.
----- Sample Input 1 ------
3
1 2 3
10 15 5
100 999 500
----- Sample Output 1 ------
2
10
500
|
{"inputs": ["3\n1 2 3\n10 15 5\n100 999 500"], "outputs": ["2\n10\n500"]}
| 189
| 45
|
coding
|
Solve the programming task below in a Python markdown code block.
Gildong has bought a famous painting software cfpaint. The working screen of cfpaint is square-shaped consisting of $n$ rows and $n$ columns of square cells. The rows are numbered from $1$ to $n$, from top to bottom, and the columns are numbered from $1$ to $n$, from left to right. The position of a cell at row $r$ and column $c$ is represented as $(r, c)$. There are only two colors for the cells in cfpaint — black and white.
There is a tool named eraser in cfpaint. The eraser has an integer size $k$ ($1 \le k \le n$). To use the eraser, Gildong needs to click on a cell $(i, j)$ where $1 \le i, j \le n - k + 1$. When a cell $(i, j)$ is clicked, all of the cells $(i', j')$ where $i \le i' \le i + k - 1$ and $j \le j' \le j + k - 1$ become white. In other words, a square with side equal to $k$ cells and top left corner at $(i, j)$ is colored white.
A white line is a row or a column without any black cells.
Gildong has worked with cfpaint for some time, so some of the cells (possibly zero or all) are currently black. He wants to know the maximum number of white lines after using the eraser exactly once. Help Gildong find the answer to his question.
-----Input-----
The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 2000$) — the number of rows and columns, and the size of the eraser.
The next $n$ lines contain $n$ characters each without spaces. The $j$-th character in the $i$-th line represents the cell at $(i,j)$. Each character is given as either 'B' representing a black cell, or 'W' representing a white cell.
-----Output-----
Print one integer: the maximum number of white lines after using the eraser exactly once.
-----Examples-----
Input
4 2
BWWW
WBBW
WBBW
WWWB
Output
4
Input
3 1
BWB
WWB
BWB
Output
2
Input
5 3
BWBBB
BWBBB
BBBBB
BBBBB
WBBBW
Output
2
Input
2 2
BW
WB
Output
4
Input
2 1
WW
WW
Output
4
-----Note-----
In the first example, Gildong can click the cell $(2, 2)$, then the working screen becomes: BWWW
WWWW
WWWW
WWWB
Then there are four white lines — the $2$-nd and $3$-rd row, and the $2$-nd and $3$-rd column.
In the second example, clicking the cell $(2, 3)$ makes the $2$-nd row a white line.
In the third example, both the $2$-nd column and $5$-th row become white lines by clicking the cell $(3, 2)$.
|
{"inputs": ["1 1\nB\n", "1 1\nW\n", "1 1\nW\n", "1 1\nB\n", "2 2\nBW\nWB\n", "2 1\nWW\nWW\n", "2 1\nBB\nWW\n", "2 1\nBB\nBB\n"], "outputs": ["2\n", "2\n", "2\n", "2\n", "4\n", "4\n", "2\n", "0\n"]}
| 721
| 110
|
coding
|
Solve the programming task below in a Python markdown code block.
The only difference between easy and hard versions is constraints.
You are given a sequence $a$ consisting of $n$ positive integers.
Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are $a$ and $b$, $a$ can be equal $b$) and is as follows: $[\underbrace{a, a, \dots, a}_{x}, \underbrace{b, b, \dots, b}_{y}, \underbrace{a, a, \dots, a}_{x}]$. There $x, y$ are integers greater than or equal to $0$. For example, sequences $[]$, $[2]$, $[1, 1]$, $[1, 2, 1]$, $[1, 2, 2, 1]$ and $[1, 1, 2, 1, 1]$ are three block palindromes but $[1, 2, 3, 2, 1]$, $[1, 2, 1, 2, 1]$ and $[1, 2]$ are not.
Your task is to choose the maximum by length subsequence of $a$ that is a three blocks palindrome.
You have to answer $t$ independent test cases.
Recall that the sequence $t$ is a a subsequence of the sequence $s$ if $t$ can be derived from $s$ by removing zero or more elements without changing the order of the remaining elements. For example, if $s=[1, 2, 1, 3, 1, 2, 1]$, then possible subsequences are: $[1, 1, 1, 1]$, $[3]$ and $[1, 2, 1, 3, 1, 2, 1]$, but not $[3, 2, 3]$ and $[1, 1, 1, 1, 2]$.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 200$), where $a_i$ is the $i$-th element of $a$. Note that the maximum value of $a_i$ can be up to $200$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer — the maximum possible length of some subsequence of $a$ that is a three blocks palindrome.
-----Example-----
Input
6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1
Output
7
2
4
1
1
3
|
{"inputs": ["6\n8\n1 2 2 2 3 2 1 1\n3\n1 3 1\n4\n1 2 3 2\n1\n26\n2\n2 1\n3\n2 1 1\n", "6\n8\n1 1 1 2 3 2 1 1\n3\n1 3 1\n4\n1 2 3 2\n1\n26\n2\n3 1\n3\n3 1 2\n", "6\n8\n1 1 2 2 3 2 1 1\n3\n1 3 1\n4\n1 2 3 1\n1\n26\n2\n2 1\n3\n2 1 1\n", "6\n8\n1 1 2 2 3 2 1 1\n3\n1 3 1\n4\n1 2 3 2\n1\n26\n2\n2 1\n3\n2 1 1\n", "6\n8\n1 1 2 2 3 2 1 1\n3\n1 3 1\n4\n1 2 3 2\n1\n26\n2\n2 1\n3\n3 1 1\n", "6\n8\n1 1 2 2 3 2 1 1\n3\n1 3 1\n4\n1 2 3 2\n1\n26\n2\n3 1\n3\n3 1 1\n", "6\n8\n1 1 2 2 3 2 1 1\n3\n1 3 1\n4\n1 2 3 2\n1\n26\n2\n3 2\n3\n3 1 1\n", "6\n8\n1 1 2 2 3 1 1 1\n3\n1 3 1\n4\n1 2 3 2\n1\n26\n2\n3 1\n3\n3 1 1\n"], "outputs": ["6\n3\n3\n1\n1\n2\n", "6\n3\n3\n1\n1\n1\n", "7\n3\n3\n1\n1\n2\n", "7\n3\n3\n1\n1\n2\n", "7\n3\n3\n1\n1\n2\n", "7\n3\n3\n1\n1\n2\n", "7\n3\n3\n1\n1\n2\n", "6\n3\n3\n1\n1\n2\n"]}
| 757
| 590
|
coding
|
Solve the programming task below in a Python markdown code block.
We have a 3 \times 3 grid. A number c_{i, j} is written in the square (i, j), where (i, j) denotes the square at the i-th row from the top and the j-th column from the left.
According to Takahashi, there are six integers a_1, a_2, a_3, b_1, b_2, b_3 whose values are fixed, and the number written in the square (i, j) is equal to a_i + b_j.
Determine if he is correct.
-----Constraints-----
- c_{i, j} \ (1 \leq i \leq 3, 1 \leq j \leq 3) is an integer between 0 and 100 (inclusive).
-----Input-----
Input is given from Standard Input in the following format:
c_{1,1} c_{1,2} c_{1,3}
c_{2,1} c_{2,2} c_{2,3}
c_{3,1} c_{3,2} c_{3,3}
-----Output-----
If Takahashi's statement is correct, print Yes; otherwise, print No.
-----Sample Input-----
1 0 1
2 1 2
1 0 1
-----Sample Output-----
Yes
Takahashi is correct, since there are possible sets of integers such as: a_1=0,a_2=1,a_3=0,b_1=1,b_2=0,b_3=1.
|
{"inputs": ["1 0 1\n4 1 2\n1 0 1", "1 8 6\n2 9 7\n1 7 7", "0 8 8\n0 8 8\n0 0 8", "2 2 2\n2 1 0\n2 2 2", "1 0 1\n4 1 2\n0 0 1", "1 8 6\n4 9 7\n1 7 7", "0 8 2\n0 8 8\n0 0 8", "1 0 1\n4 1 4\n0 0 1"], "outputs": ["No\n", "No\n", "No\n", "No\n", "No\n", "No\n", "No\n", "No\n"]}
| 347
| 190
|
coding
|
Solve the programming task below in a Python markdown code block.
At the store, the salespeople want to make all prices round.
In this problem, a number that is a power of $10$ is called a round number. For example, the numbers $10^0 = 1$, $10^1 = 10$, $10^2 = 100$ are round numbers, but $20$, $110$ and $256$ are not round numbers.
So, if an item is worth $m$ bourles (the value of the item is not greater than $10^9$), the sellers want to change its value to the nearest round number that is not greater than $m$. They ask you: by how many bourles should you decrease the value of the item to make it worth exactly $10^k$ bourles, where the value of $k$ — is the maximum possible ($k$ — any non-negative integer).
For example, let the item have a value of $178$-bourles. Then the new price of the item will be $100$, and the answer will be $178-100=78$.
-----Input-----
The first line of input data contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases .
Each test case is a string containing a single integer $m$ ($1 \le m \le 10^9$) — the price of the item.
-----Output-----
For each test case, output on a separate line a single integer $d$ ($0 \le d < m$) such that if you reduce the cost of the item by $d$ bourles, the cost of the item will be the maximal possible round number. More formally: $m - d = 10^k$, where $k$ — the maximum possible non-negative integer.
-----Examples-----
Input
7
1
2
178
20
999999999
9000
987654321
Output
0
1
78
10
899999999
8000
887654321
-----Note-----
In the example:
$1 - 0 = 10^0$,
$2 - 1 = 10^0$,
$178 - 78 = 10^2$,
$20 - 10 = 10^1$,
$999999999 - 899999999 = 10^8$,
$9000 - 8000 = 10^3$,
$987654321 - 887654321 = 10^8$.
Note that in each test case, we get the maximum possible round number.
|
{"inputs": ["4\n1\n1\n1\n1\n", "1\n100000000\n", "14\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n", "7\n1\n2\n178\n20\n999999999\n9000\n987654321\n", "7\n100\n40000000\n89657\n326894\n2325566\n74444\n200055\n"], "outputs": ["0\n0\n0\n0\n", "0\n", "0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n", "0\n1\n78\n10\n899999999\n8000\n887654321\n", "0\n30000000\n79657\n226894\n1325566\n64444\n100055\n"]}
| 651
| 284
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given a 0-indexed m x n matrix grid consisting of positive integers.
You can start at any cell in the first column of the matrix, and traverse the grid in the following way:
From a cell (row, col), you can move to any of the cells: (row - 1, col + 1), (row, col + 1) and (row + 1, col + 1) such that the value of the cell you move to, should be strictly bigger than the value of the current cell.
Return the maximum number of moves that you can perform.
Please complete the following python code precisely:
```python
class Solution:
def maxMoves(self, grid: List[List[int]]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(grid = [[2,4,3,5],[5,4,9,3],[3,4,2,11],[10,9,13,15]]) == 3\n assert candidate(grid = [[3,2,4],[2,1,9],[1,1,7]]) == 0\n\n\ncheck(Solution().maxMoves)"}
| 172
| 93
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
A sentence is a list of words that are separated by a single space with no leading or trailing spaces.
For example, "Hello World", "HELLO", "hello world hello world" are all sentences.
Words consist of only uppercase and lowercase English letters. Uppercase and lowercase English letters are considered different.
A sentence is circular if:
The last character of a word is equal to the first character of the next word.
The last character of the last word is equal to the first character of the first word.
For example, "leetcode exercises sound delightful", "eetcode", "leetcode eats soul" are all circular sentences. However, "Leetcode is cool", "happy Leetcode", "Leetcode" and "I like Leetcode" are not circular sentences.
Given a string sentence, return true if it is circular. Otherwise, return false.
Please complete the following python code precisely:
```python
class Solution:
def isCircularSentence(self, sentence: str) -> bool:
```
|
{"functional": "def check(candidate):\n assert candidate(sentence = \"leetcode exercises sound delightful\") == True\n assert candidate(sentence = \"eetcode\") == True\n assert candidate(sentence = \"Leetcode is cool\") == False\n\n\ncheck(Solution().isCircularSentence)"}
| 222
| 60
|
coding
|
Solve the programming task below in a Python markdown code block.
# Story&Task
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all houses in the city were built in one row.
Let's enumerate all the houses from left to right, starting with 0. A house is considered to be luxurious if the number of floors in it is strictly greater than in each house with larger number. In other words, a house is luxurious if the number of floors in it is strictly greater than in all houses, located to the right from it.
The new architect is interested in n questions, the ith of them is the following: "how many floors should be added to the ith house to make it luxurious?" (For each i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
# Input/Output
- `[input]` integer array `houses`
Array of positive integers, representing the number of floors in each house.
The ith element is the number of houses in the ith house.
`1 ≤ houses.length ≤ 1000`
- `[output]` an integer array
An array has the same length as input array, the ith element represents the number of floors that should be added to the ith house to make it luxurious.
# Example
For `houses = [1,2,3,1,2]`, the output should be `[3,2,0,2,0]`.
```
For houses[0], 3 floors should be added,
then its floors is strictly greater than all houses of right side.
For houses[1], 2 floors should be added.
For houses[2], no need to add floor
because it's already the luxurious.
For houses[3], 2 floors need to added
For houses[4], no house on the right,
so no need to add any floor.
```
Also feel free to reuse/extend the following starter code:
```python
def luxhouse(houses):
```
|
{"functional": "_inputs = [[[1, 2, 3, 1, 2]], [[3, 2, 1, 4]], [[1, 2, 3]], [[3, 2, 1]], [[1, 1, 1]]]\n_outputs = [[[3, 2, 0, 2, 0]], [[2, 3, 4, 0]], [[3, 2, 0]], [[0, 0, 0]], [[1, 1, 0]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(luxhouse(*i), o[0])"}
| 466
| 257
|
coding
|
Solve the programming task below in a Python markdown code block.
Chef got his dream seat in F1 and secured a 3^{rd} place in his debut race. He now wants to know the time gap between him and the winner of the race.
You are his chief engineer and you only know the time gap between Chef and the runner up of the race, given as A seconds, and the time gap between the runner up and the winner of the race, given as B seconds.
Please calculate and inform Chef about the time gap between him and the winner of the race.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of a single line of input containing two space-separated integers A and B denoting the time gap between Chef and the runner up and the time gap between the runner up and the winner respectively.
------ Output Format ------
For each test case, output the time gap between Chef and the winner of the race.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ A, B ≤ 10$
----- Sample Input 1 ------
4
1 1
2 5
3 2
5 6
----- Sample Output 1 ------
2
7
5
11
----- explanation 1 ------
Test case $1$: The time gap between Chef and runner up is $1$ second. The time gap between runner up and the winner is $1$ second. Thus, the time gap between Chef and the winner is $1+1=2$ seconds.
Test case $2$: The time gap between Chef and runner up is $2$ seconds. The time gap between runner up and the winner is $5$ second. Thus, the time gap between Chef and the winner is $2+5=7$ seconds.
Test case $3$: The time gap between Chef and runner up is $3$ seconds. The time gap between runner up and the winner is $2$ second. Thus, the time gap between Chef and the winner is $3+2=5$ seconds.
Test case $4$: The time gap between Chef and runner up is $5$ seconds. The time gap between runner up and the winner is $6$ second. Thus, the time gap between Chef and the winner is $5+6=11$ seconds.
|
{"inputs": ["4\n1 1\n2 5\n3 2\n5 6\n"], "outputs": ["2\n7\n5\n11\n"]}
| 500
| 37
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given two 0-indexed integer arrays nums1 and nums2, each of length n, and a 1-indexed 2D array queries where queries[i] = [xi, yi].
For the ith query, find the maximum value of nums1[j] + nums2[j] among all indices j (0 <= j < n), where nums1[j] >= xi and nums2[j] >= yi, or -1 if there is no j satisfying the constraints.
Return an array answer where answer[i] is the answer to the ith query.
Please complete the following python code precisely:
```python
class Solution:
def maximumSumQueries(self, nums1: List[int], nums2: List[int], queries: List[List[int]]) -> List[int]:
```
|
{"functional": "def check(candidate):\n assert candidate(nums1 = [4,3,1,2], nums2 = [2,4,9,5], queries = [[4,1],[1,3],[2,5]]) == [6,10,7]\n assert candidate(nums1 = [3,2,5], nums2 = [2,3,4], queries = [[4,4],[3,2],[1,1]]) == [9,9,9]\n assert candidate(nums1 = [2,1], nums2 = [2,3], queries = [[3,3]]) == [-1]\n\n\ncheck(Solution().maximumSumQueries)"}
| 178
| 150
|
coding
|
Solve the programming task below in a Python markdown code block.
In the online judge system, a judge file may include multiple datasets to check whether the submitted program outputs a correct answer for each test case. This task is to practice solving a problem with multiple datasets.
Write a program which reads an integer x and print it as is. Note that multiple datasets are given for this problem.
Constraints
* 1 ≤ x ≤ 10000
* The number of datasets ≤ 10000
Input
The input consists of multiple datasets. Each dataset consists of an integer x in a line.
The input ends with an integer 0. You program should not process (print) for this terminal symbol.
Output
For each dataset, print x in the following format:
Case i: x
where i is the case number which starts with 1. Put a single space between "Case" and i. Also, put a single space between ':' and x.
Example
Input
3
5
11
7
8
19
0
Output
Case 1: 3
Case 2: 5
Case 3: 11
Case 4: 7
Case 5: 8
Case 6: 19
|
{"inputs": ["3\n5\n9\n6\n2\n2\n0", "3\n5\n9\n6\n3\n2\n0", "3\n5\n9\n5\n3\n2\n0", "3\n5\n9\n1\n3\n2\n0", "3\n5\n9\n1\n4\n2\n0", "3\n5\n9\n2\n4\n2\n0", "3\n5\n9\n4\n2\n19\n0", "3\n5\n5\n3\n8\n19\n0"], "outputs": ["Case 1: 3\nCase 2: 5\nCase 3: 9\nCase 4: 6\nCase 5: 2\nCase 6: 2\n", "Case 1: 3\nCase 2: 5\nCase 3: 9\nCase 4: 6\nCase 5: 3\nCase 6: 2\n", "Case 1: 3\nCase 2: 5\nCase 3: 9\nCase 4: 5\nCase 5: 3\nCase 6: 2\n", "Case 1: 3\nCase 2: 5\nCase 3: 9\nCase 4: 1\nCase 5: 3\nCase 6: 2\n", "Case 1: 3\nCase 2: 5\nCase 3: 9\nCase 4: 1\nCase 5: 4\nCase 6: 2\n", "Case 1: 3\nCase 2: 5\nCase 3: 9\nCase 4: 2\nCase 5: 4\nCase 6: 2\n", "Case 1: 3\nCase 2: 5\nCase 3: 9\nCase 4: 4\nCase 5: 2\nCase 6: 19\n", "Case 1: 3\nCase 2: 5\nCase 3: 5\nCase 4: 3\nCase 5: 8\nCase 6: 19\n"]}
| 264
| 482
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problems statements in Mandarin Chinese and Russian.
Chef is judging a game called "Broken telephone". There are total N players taking part in the game. They are all sitting in a line. In the start of the game, first player is given a secret message written on a sheet of paper. Then they keep sending the message by whispering it to the player sitting immediate right to one and so on until it reaches the last person.
Finally, the message received by the last player is compared with the message said by first player. If these messages aren't equal, there is someone who has misheard the message or whispered it wrongly to the next player. If messages is equal, then the players win and receive a tasty chocolate.
Note that first player receives the message on a sheet of paper, thus he cannot mishear it.
As Chef wants to be sure that every player has fulfilled his/ her role in the game, so he asks everyone to state their received messages after the end of the game. You are given an array A of N integers denoting messages received by each person.
Please help Chef to find the number of players that could mishear the message or whisper it wrongly.
------ Input ------
The first line of the input contains an integer T denoting the number of test cases.
The first line of each test case contains a single integer N denoting the number of players
The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N} denoting the messages of players.
------ Output ------
For each test case, output a single line containing an integer corresponding to the number of players that could mishear the message or whisper it wrongly.
------
------ Constraints -----
$1 ≤ T ≤ 5$
$1 ≤ A_{i} ≤ 10^{9}$
Subtask 1: 40 points
$2 ≤ N ≤ 10^{3}$
Subtask 2: 60 points
$2 ≤ N ≤ 10^{5}$
----- Sample Input 1 ------
3
7
1 1 1 3 3 3 2
5
1 3 1 1 1
4
5 5 5 5
----- Sample Output 1 ------
4
3
0
----- explanation 1 ------
Example 1: The 3-rd, 4-th, 6-th and 7-th player could mishear the message or whisper it wrongly.
Example 2: First 3 players could mishear the message or whisper it wrongly.
|
{"inputs": ["3\n7\n1 1 1 3 3 3 2\n5\n1 3 1 1 1\n4\n5 5 5 5", "3\n7\n1 1 1 3 3 3 2\n5\n1 3 1 1 2\n4\n5 5 5 5", "3\n7\n1 1 1 3 3 3 3\n5\n1 3 1 1 2\n4\n5 5 5 5", "3\n7\n1 1 1 3 3 3 2\n5\n1 0 1 1 1\n4\n5 5 5 5", "3\n7\n1 1 1 3 3 3 2\n5\n1 3 1 1 2\n4\n9 5 5 5", "3\n7\n1 1 1 3 3 3 3\n5\n1 3 1 1 2\n4\n5 5 9 5", "3\n7\n1 2 1 3 3 3 2\n5\n1 0 1 1 1\n4\n5 5 5 5", "3\n7\n1 1 1 3 3 3 3\n5\n1 3 1 1 2\n4\n9 5 5 5"], "outputs": ["4\n3\n0", "4\n5\n0\n", "2\n5\n0\n", "4\n3\n0\n", "4\n5\n2\n", "2\n5\n3\n", "6\n3\n0\n", "2\n5\n2\n"]}
| 547
| 397
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Chef calls a sequence *good* if it does not contain any two adjacent identical elements.
Initially, Chef has a sequence $a_{1}, a_{2}, \ldots, a_{N}$. He likes to change the sequence every now and then. You should process $Q$ queries. In each query, Chef chooses an index $x$ and changes the $x$-th element of the sequence to $y$. After each query, can you find the length of the longest good subsequence of the current sequence?
Note that the queries are not independent.
------ 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 $Q$.
The second line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$.
$Q$ lines follow. Each of these lines contains two space-separated integers $x$ and $y$ describing a query.
------ Output ------
After each query, print a single line containing one integer ― the length of the longest good subsequence.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N, Q ≤ 10^{5}$
$1 ≤ a_{i} ≤ 10^{9}$ for each valid $i$
$1 ≤ x ≤ N$
$1 ≤ y ≤ 10^{9}$
------ Subtasks ------
Subtask #1 (35 points): $Q = 1$
Subtask #2 (65 points): original constraints
----- Sample Input 1 ------
1
5 2
1 1 2 5 2
1 3
4 2
----- Sample Output 1 ------
5
3
|
{"inputs": ["1\n5 2\n1 1 2 5 2\n1 3\n4 2"], "outputs": ["5\n3"]}
| 435
| 36
|
coding
|
Solve the programming task below in a Python markdown code block.
The eval() expression is a very powerful built-in function of Python. It helps in evaluating an expression. The expression can be a Python statement, or a code object.
For example:
>>> eval("9 + 5")
14
>>> x = 2
>>> eval("x + 3")
5
Here, eval() can also be used to work with Python keywords or defined functions and variables. These would normally be stored as strings.
For example:
>>> type(eval("len"))
<type 'builtin_function_or_method'>
Without eval()
>>> type("len")
<type 'str'>
Task
You are given an expression in a line. Read that line as a string variable, such as var, and print the result using eval(var).
NOTE: Python2 users, please import from __future__ import print_function.
Constraint
Input string is less than 100 characters.
Sample Input
print(2 + 3)
Sample Output
5
|
{"inputs": ["print(2 + 3)\n"], "outputs": ["5\n"]}
| 214
| 20
|
coding
|
Solve the programming task below in a Python markdown code block.
You have a binary string S of length N. In one operation you can select a substring of S and reverse it. For example, on reversing the substring S[2,4] for S=11000, we change 1 \textcolor{red}{100} 0 \rightarrow 1 \textcolor{red}{001} 0.
Find the minimum number of operations required to sort this binary string.
It can be proven that the string can always be sorted using the above operation finite number of times.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of 2 lines of input.
- The first line of each test case contains a single integer N — the length of the binary string.
- The second line of each test case contains a binary string S of length N.
------ Output Format ------
For each test case, output on a new line — the minimum number of operations required to sort the binary string.
------ Constraints ------
$1 ≤ T ≤ 2\cdot 10^{5}$
$1 ≤ N ≤ 2\cdot 10^{5}$
- Sum of $N$ over all test cases does not exceed $10^{6}$.
- String $S$ consists of only '$0$'s and '$1$'s.
----- Sample Input 1 ------
4
3
000
4
1001
4
1010
6
010101
----- Sample Output 1 ------
0
1
2
2
----- explanation 1 ------
Test case $1$: The string is already sorted, hence, zero operations are required to sort it.
Test case $2$: We can sort the string in the following way: $\textcolor{red}{100} 1$ $\rightarrow$ $0011$.
Test case $3$: We can sort the string in the following way:
$1 \textcolor{red}{01} 0$ $\rightarrow$ $\textcolor{red}{1100}$ $\rightarrow$ $0011$.
It can be proven that this string cannot be sorted in less than $2$ operations.
Test case $4$: We can sort the string in the following way:
$0 \textcolor{red}{1010}1$ $\rightarrow$ $00 \textcolor{red}{10}11$ $\rightarrow$ $000111$.
It can be proven that this string cannot be sorted in less than $2$ operations.
|
{"inputs": ["4\n3\n000\n4\n1001\n4\n1010\n6\n010101"], "outputs": ["0\n1\n2\n2"]}
| 577
| 47
|
coding
|
Solve the programming task below in a Python markdown code block.
The capital of Berland looks like a rectangle of size n × m of the square blocks of same size.
Fire!
It is known that k + 1 blocks got caught on fire (k + 1 ≤ n·m). Those blocks are centers of ignition. Moreover positions of k of these centers are known and one of these stays unknown. All k + 1 positions are distinct.
The fire goes the following way: during the zero minute of fire only these k + 1 centers of ignition are burning. Every next minute the fire goes to all neighbouring blocks to the one which is burning. You can consider blocks to burn for so long that this time exceeds the time taken in the problem. The neighbouring blocks are those that touch the current block by a side or by a corner.
Berland Fire Deparment wants to estimate the minimal time it takes the fire to lighten up the whole city. Remember that the positions of k blocks (centers of ignition) are known and (k + 1)-th can be positioned in any other block.
Help Berland Fire Department to estimate the minimal time it takes the fire to lighten up the whole city.
-----Input-----
The first line contains three integers n, m and k (1 ≤ n, m ≤ 10^9, 1 ≤ k ≤ 500).
Each of the next k lines contain two integers x_{i} and y_{i} (1 ≤ x_{i} ≤ n, 1 ≤ y_{i} ≤ m) — coordinates of the i-th center of ignition. It is guaranteed that the locations of all centers of ignition are distinct.
-----Output-----
Print the minimal time it takes the fire to lighten up the whole city (in minutes).
-----Examples-----
Input
7 7 3
1 2
2 1
5 5
Output
3
Input
10 5 1
3 3
Output
2
-----Note-----
In the first example the last block can have coordinates (4, 4).
In the second example the last block can have coordinates (8, 3).
|
{"inputs": ["8 5 1\n3 3\n", "10 5 1\n3 3\n", "10 5 1\n3 5\n", "10 5 1\n3 3\n", "5 1 2\n4 1\n5 1\n", "5 1 2\n4 1\n5 1\n", "5 1 2\n4 1\n1 1\n", "5 1 2\n4 1\n2 1\n"], "outputs": ["2\n", "2\n", "4\n", "2\n", "1\n", "1\n", "1\n", "1\n"]}
| 445
| 153
|
coding
|
Solve the programming task below in a Python markdown code block.
This is a problem that involves adding numbers to items in a list.
In a list you will have to add the item's remainder when divided by a given divisor to each item.
For example if the item is 40 and the divisor is 3 you would have to add 1 since 40 minus the closest multiple of 3 which is 39 is 1. So the 40 in the list will become 41. You would have to return the modified list in this problem.
For this problem you will receive a divisor called `div` as well as simple list of whole numbers called `nums`. Good luck and happy coding.
# Examples
```python
nums = [2, 7, 5, 9, 100, 34, 32, 0], div = 3
==> [4, 8, 7, 9, 101, 35, 34, 0]
nums = [1000, 999, 998, 997], div = 5
==> [1000, 1003, 1001, 999]
nums = [], div = 2
==> []
```
**Note:** random tests check lists containing up to 10000 elements.
Also feel free to reuse/extend the following starter code:
```python
def solve(nums,div):
```
|
{"functional": "_inputs = [[[2, 7, 5, 9, 100, 34, 32, 0], 3], [[], 2], [[1000, 999, 998, 997], 5], [[0, 0, 0, 0], 5], [[4, 3, 2, 1], 5], [[33, 23, 45, 78, 65], 10]]\n_outputs = [[[4, 8, 7, 9, 101, 35, 34, 0]], [[]], [[1000, 1003, 1001, 999]], [[0, 0, 0, 0]], [[8, 6, 4, 2]], [[36, 26, 50, 86, 70]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(solve(*i), o[0])"}
| 326
| 359
|
coding
|
Solve the programming task below in a Python markdown code block.
We have a string s consisting of lowercase English letters. Snuke is partitioning s into some number of non-empty substrings. Let the subtrings obtained be s_1, s_2, ..., s_N from left to right. (Here, s = s_1 + s_2 + ... + s_N holds.) Snuke wants to satisfy the following condition:
* For each i (1 \leq i \leq N), it is possible to permute the characters in s_i and obtain a palindrome.
Find the minimum possible value of N when the partition satisfies the condition.
Constraints
* 1 \leq |s| \leq 2 \times 10^5
* s consists of lowercase English letters.
Input
Input is given from Standard Input in the following format:
s
Output
Print the minimum possible value of N when the partition satisfies the condition.
Examples
Input
aabxyyzz
Output
2
Input
byebye
Output
1
Input
abcdefghijklmnopqrstuvwxyz
Output
26
Input
abcabcxabcx
Output
3
|
{"inputs": ["byeybe", "eybcxb", "ebyeyb", "eybeyb", "eybdyb", "eybcyb", "byecxb", "cyebxb"], "outputs": ["1\n", "6\n", "1\n", "1\n", "2\n", "2\n", "6\n", "4\n"]}
| 242
| 80
|
coding
|
Solve the programming task below in a Python markdown code block.
The only difference between easy and hard versions is the size of the input.
You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'.
You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...".
A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not.
You have to answer $q$ independent queries.
-----Input-----
The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow.
The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring.
The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'.
It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...".
-----Example-----
Input
3
5 2
BGGGG
5 3
RBRGR
5 5
BBBRR
Output
1
0
3
-----Note-----
In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB".
In the second example, the substring is "BRG".
|
{"inputs": ["3\n5 2\nBGGGG\n5 3\nRBRGR\n5 5\nBBBRR\n", "3\n5 2\nBGGGG\n5 3\nRBRGR\n5 5\nBBRBR\n", "3\n5 3\nBGGGG\n5 3\nRBRGR\n5 5\nBBBRR\n", "3\n5 2\nBGGGG\n5 4\nRBRGR\n5 5\nBBRBR\n", "3\n5 3\nBGGGG\n5 3\nRBRGR\n5 2\nBBBRR\n", "3\n5 2\nBGGGG\n5 4\nRBRGR\n5 5\nBRBRB\n", "3\n5 1\nBGGGG\n5 2\nRBRGR\n5 5\nBBRBR\n", "3\n5 1\nBGGGG\n5 4\nRBRGR\n5 5\nBBRBR\n"], "outputs": ["1\n0\n3\n", "1\n0\n2\n", "1\n0\n3\n", "1\n1\n2\n", "1\n0\n0\n", "1\n1\n3\n", "0\n0\n2\n", "0\n1\n2\n"]}
| 591
| 294
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given three piles of casino chips: white, green and black chips:
* the first pile contains only white chips
* the second pile contains only green chips
* the third pile contains only black chips
Each day you take exactly two chips of different colors and head to the casino. You can choose any color, but you are not allowed to take two chips of the same color in a day.
You will be given an array representing the number of chips of each color and your task is to return the maximum number of days you can pick the chips. Each day you need to take exactly two chips.
```python
solve([1,1,1]) = 1, because after you pick on day one, there will be only one chip left
solve([1,2,1] = 2, you can pick twice; you pick two chips on day one then on day two
solve([4,1,1]) = 2
```
```javascript
solve([1,1,1]) = 1, because after you pick on day one, there will be only one chip left
solve([1,2,1]) = 2, you can pick twice; you pick two chips on day one then on day two
solve([4,1,1]) = 2
```
```go
solve([1,1,1]) = 1, because after you pick on day one, there will be only one chip left
solve([1,2,1]) = 2, you can pick twice; you pick two chips on day one then on day two
solve([4,1,1]) = 2
```
```ruby
solve([1,1,1]) = 1, because after you pick on day one, there will be only one chip left
solve([1,2,1]) = 2, you can pick twice; you pick two chips on day, two chips on day two
solve([4,1,1]) = 2
```
More examples in the test cases. Good luck!
Brute force is not the way to go here. Look for a simplifying mathematical approach.
Also feel free to reuse/extend the following starter code:
```python
def solve(arr):
```
|
{"functional": "_inputs = [[[1, 1, 1]], [[1, 2, 1]], [[4, 1, 1]], [[8, 2, 8]], [[8, 1, 4]], [[7, 4, 10]], [[12, 12, 12]], [[1, 23, 2]]]\n_outputs = [[1], [2], [2], [9], [5], [10], [18], [3]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(solve(*i), o[0])"}
| 482
| 251
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
There exists an undirected and initially unrooted tree with n nodes indexed from 0 to n - 1. You are given the integer n and a 2D integer array edges of length n - 1, where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree.
Each node has an associated price. You are given an integer array price, where price[i] is the price of the ith node.
The price sum of a given path is the sum of the prices of all nodes lying on that path.
The tree can be rooted at any node root of your choice. The incurred cost after choosing root is the difference between the maximum and minimum price sum amongst all paths starting at root.
Return the maximum possible cost amongst all possible root choices.
Please complete the following python code precisely:
```python
class Solution:
def maxOutput(self, n: int, edges: List[List[int]], price: List[int]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(n = 6, edges = [[0,1],[1,2],[1,3],[3,4],[3,5]], price = [9,8,7,6,10,5]) == 24\n assert candidate(n = 3, edges = [[0,1],[1,2]], price = [1,1,1]) == 2\n\n\ncheck(Solution().maxOutput)"}
| 223
| 103
|
coding
|
Solve the programming task below in a Python markdown code block.
Professor GukiZ doesn't accept string as they are. He likes to swap some letters in string to obtain a new one.
GukiZ has strings a, b, and c. He wants to obtain string k by swapping some letters in a, so that k should contain as many non-overlapping substrings equal either to b or c as possible. Substring of string x is a string formed by consecutive segment of characters from x. Two substrings of string x overlap if there is position i in string x occupied by both of them.
GukiZ was disappointed because none of his students managed to solve the problem. Can you help them and find one of possible strings k?
Input
The first line contains string a, the second line contains string b, and the third line contains string c (1 ≤ |a|, |b|, |c| ≤ 105, where |s| denotes the length of string s).
All three strings consist only of lowercase English letters.
It is possible that b and c coincide.
Output
Find one of possible strings k, as described in the problem statement. If there are multiple possible answers, print any of them.
Examples
Input
aaa
a
b
Output
aaa
Input
pozdravstaklenidodiri
niste
dobri
Output
nisteaadddiiklooprrvz
Input
abbbaaccca
ab
aca
Output
ababacabcc
Note
In the third sample, this optimal solutions has three non-overlaping substrings equal to either b or c on positions 1 – 2 (ab), 3 – 4 (ab), 5 – 7 (aca). In this sample, there exist many other optimal solutions, one of them would be acaababbcc.
|
{"inputs": ["aaa\na\nb\n", "cumurcumur\num\ncur\n", "cumurcumur\nul\ncur\n", "abbbaaccca\nba\naca\n", "cunurcumur\nul\ncur\n", "abbbaaccca\nab\naca\n", "xxxbbbcccoca\nca\ncb\n", "xxxbbbcccoca\nca\nca\n"], "outputs": ["aaa\n", "umumcurcur\n", "curcurmmuu", "babaacabcc", "curcurmnuu", "ababacabcc\n", "cacbcbcboxxx\n", "cabbbcccoxxx"]}
| 390
| 150
|
coding
|
Solve the programming task below in a Python markdown code block.
There is a programing contest named SnakeUp, 2n people want to compete for it. In order to attend this contest, people need to form teams of exactly two people. You are given the strength of each possible combination of two people. All the values of the strengths are distinct.
Every contestant hopes that he can find a teammate so that their team’s strength is as high as possible. That is, a contestant will form a team with highest strength possible by choosing a teammate from ones who are willing to be a teammate with him/her. More formally, two people A and B may form a team if each of them is the best possible teammate (among the contestants that remain unpaired) for the other one.
Can you determine who will be each person’s teammate?
-----Input-----
There are 2n lines in the input.
The first line contains an integer n (1 ≤ n ≤ 400) — the number of teams to be formed.
The i-th line (i > 1) contains i - 1 numbers a_{i}1, a_{i}2, ... , a_{i}(i - 1). Here a_{ij} (1 ≤ a_{ij} ≤ 10^6, all a_{ij} are distinct) denotes the strength of a team consisting of person i and person j (people are numbered starting from 1.)
-----Output-----
Output a line containing 2n numbers. The i-th number should represent the number of teammate of i-th person.
-----Examples-----
Input
2
6
1 2
3 4 5
Output
2 1 4 3
Input
3
487060
3831 161856
845957 794650 976977
83847 50566 691206 498447
698377 156232 59015 382455 626960
Output
6 5 4 3 2 1
-----Note-----
In the first sample, contestant 1 and 2 will be teammates and so do contestant 3 and 4, so the teammate of contestant 1, 2, 3, 4 will be 2, 1, 4, 3 respectively.
|
{"inputs": ["1\n1000000\n", "1\n1000000\n", "1\n1000001\n", "1\n1000100\n", "1\n0000001\n", "1\n1000110\n", "1\n0001001\n", "1\n1001110\n"], "outputs": ["2 1\n", "2 1\n", "2 1 ", "2 1 ", "2 1 ", "2 1 ", "2 1 ", "2 1 "]}
| 531
| 145
|
coding
|
Solve the programming task below in a Python markdown code block.
There are N integers a_1, a_2, ..., a_N not less than 1. The values of a_1, a_2, ..., a_N are not known, but it is known that a_1 \times a_2 \times ... \times a_N = P.
Find the maximum possible greatest common divisor of a_1, a_2, ..., a_N.
Constraints
* 1 \leq N \leq 10^{12}
* 1 \leq P \leq 10^{12}
Input
Input is given from Standard Input in the following format:
N P
Output
Print the answer.
Examples
Input
3 24
Output
2
Input
5 1
Output
1
Input
1 111
Output
111
Input
4 972439611840
Output
206
|
{"inputs": ["5 2", "1 2", "1 3", "1 5", "1 4", "1 8", "1 6", "1 9"], "outputs": ["1\n", "2\n", "3\n", "5\n", "4\n", "8\n", "6\n", "9\n"]}
| 212
| 78
|
coding
|
Solve the programming task below in a Python markdown code block.
Let's arrange a deck of cards. Your task is to sort totally n cards. A card consists of a part of a suit (S, H, C or D) and an number. Write a program which sorts such cards based on the following pseudocode:
Partition(A, p, r)
1 x = A[r]
2 i = p-1
3 for j = p to r-1
4 do if A[j] <= x
5 then i = i+1
6 exchange A[i] and A[j]
7 exchange A[i+1] and A[r]
8 return i+1
Quicksort(A, p, r)
1 if p < r
2 then q = Partition(A, p, r)
3 run Quicksort(A, p, q-1)
4 run Quicksort(A, q+1, r)
Here, A is an array which represents a deck of cards and comparison operations are performed based on the numbers.
Your program should also report the stability of the output for the given input (instance). Here, 'stability of the output' means that: cards with the same value appear in the output in the same order as they do in the input (instance).
Constraints
* 1 ≤ n ≤ 100,000
* 1 ≤ the number of a card ≤ 109
* There are no identical card in the input
Input
The first line contains an integer n, the number of cards.
n cards are given in the following lines. Each card is given in a line and represented by a pair of a character and an integer separated by a single space.
Output
In the first line, print the stability ("Stable" or "Not stable") of this output.
In the following lines, print the arranged cards in the same manner of that of the input.
Examples
Input
6
D 3
H 2
D 1
S 3
D 2
C 1
Output
Not stable
D 1
C 1
D 2
H 2
D 3
S 3
Input
2
S 1
H 1
Output
Stable
S 1
H 1
|
{"inputs": ["2\nS 1\nH 2", "2\nS 0\nH 1", "2\nS 2\nH 2", "2\nS 1\nH 1", "6\nD 3\nH 2\nD 1\nS 3\nC 2\nC 1", "6\nD 3\nH 2\nD 1\nS 3\nC 2\nC 2", "6\nD 3\nH 2\nC 1\nS 3\nC 2\nC 2", "6\nD 5\nH 2\nC 1\nS 3\nC 2\nC 2"], "outputs": ["Stable\nS 1\nH 2\n", "Stable\nS 0\nH 1\n", "Stable\nS 2\nH 2\n", "Stable\nS 1\nH 1", "Not stable\nD 1\nC 1\nC 2\nH 2\nD 3\nS 3\n", "Stable\nD 1\nH 2\nC 2\nC 2\nD 3\nS 3\n", "Stable\nC 1\nH 2\nC 2\nC 2\nD 3\nS 3\n", "Stable\nC 1\nH 2\nC 2\nC 2\nS 3\nD 5\n"]}
| 485
| 325
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
A decimal number can be converted to its Hexspeak representation by first converting it to an uppercase hexadecimal string, then replacing all occurrences of the digit '0' with the letter 'O', and the digit '1' with the letter 'I'. Such a representation is valid if and only if it consists only of the letters in the set {'A', 'B', 'C', 'D', 'E', 'F', 'I', 'O'}.
Given a string num representing a decimal integer n, return the Hexspeak representation of n if it is valid, otherwise return "ERROR".
Please complete the following python code precisely:
```python
class Solution:
def toHexspeak(self, num: str) -> str:
```
|
{"functional": "def check(candidate):\n assert candidate(num = \"257\") == \"IOI\"\n assert candidate(num = \"3\") == \"ERROR\"\n\n\ncheck(Solution().toHexspeak)"}
| 170
| 50
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given a string s consisting of n characters which are either 'X' or 'O'.
A move is defined as selecting three consecutive characters of s and converting them to 'O'. Note that if a move is applied to the character 'O', it will stay the same.
Return the minimum number of moves required so that all the characters of s are converted to 'O'.
Please complete the following python code precisely:
```python
class Solution:
def minimumMoves(self, s: str) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(s = \"XXX\") == 1\n assert candidate(s = \"XXOX\") == 2\n assert candidate(s = \"OOOO\") == 0\n\n\ncheck(Solution().minimumMoves)"}
| 124
| 57
|
coding
|
Solve the programming task below in a Python markdown code block.
# Write Number in Expanded Form
You will be given a number and you will need to return it as a string in [Expanded Form](https://www.mathplacementreview.com/arithmetic/whole-numbers.php#expanded-form). For example:
```python
expanded_form(12) # Should return '10 + 2'
expanded_form(42) # Should return '40 + 2'
expanded_form(70304) # Should return '70000 + 300 + 4'
```
NOTE: All numbers will be whole numbers greater than 0.
If you liked this kata, check out [part 2](https://www.codewars.com/kata/write-number-in-expanded-form-part-2)!!
Also feel free to reuse/extend the following starter code:
```python
def expanded_form(num):
```
|
{"functional": "_inputs = [[2], [12], [42], [70304], [4982342]]\n_outputs = [['2'], ['10 + 2'], ['40 + 2'], ['70000 + 300 + 4'], ['4000000 + 900000 + 80000 + 2000 + 300 + 40 + 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(expanded_form(*i), o[0])"}
| 192
| 250
|
coding
|
Solve the programming task below in a Python markdown code block.
We have an integer sequence A of length N, where A_1 = X, A_{i+1} = A_i + D (1 \leq i < N ) holds.
Takahashi will take some (possibly all or none) of the elements in this sequence, and Aoki will take all of the others.
Let S and T be the sum of the numbers taken by Takahashi and Aoki, respectively. How many possible values of S - T are there?
-----Constraints-----
- -10^8 \leq X, D \leq 10^8
- 1 \leq N \leq 2 \times 10^5
- All values in input are integers.
-----Input-----
Input is given from Standard Input in the following format:
N X D
-----Output-----
Print the number of possible values of S - T.
-----Sample Input-----
3 4 2
-----Sample Output-----
8
A is (4, 6, 8).
There are eight ways for (Takahashi, Aoki) to take the elements: ((), (4, 6, 8)), ((4), (6, 8)), ((6), (4, 8)), ((8), (4, 6))), ((4, 6), (8))), ((4, 8), (6))), ((6, 8), (4))), and ((4, 6, 8), ()).
The values of S - T in these ways are -18, -10, -6, -2, 2, 6, 10, and 18, respectively, so there are eight possible values of S - T.
|
{"inputs": ["3 3 2", "3 3 3", "0 3 2", "2 4 3", "3 4 2", "3 4 2\n", "7 5 5\n", "8 6 0\n"], "outputs": ["8\n", "7\n", "1\n", "4\n", "8", "8\n", "29\n", "9\n"]}
| 372
| 97
|
coding
|
Solve the programming task below in a Python markdown code block.
Much cooler than your run-of-the-mill Fibonacci numbers, the Triple Shiftian are so defined: `T[n] = 4 * T[n-1] - 5 * T[n-2] + 3 * T[n-3]`.
You are asked to create a function which accept a base with the first 3 numbers and then returns the nth element.
```python
triple_shiftian([1,1,1],25) == 1219856746
triple_shiftian([1,2,3],25) == 2052198929
triple_shiftian([6,7,2],25) == -2575238999
triple_shiftian([3,2,1],35) == 23471258855679
triple_shiftian([1,9,2],2) == 2
```
*Note: this is meant to be an interview quiz, so the description is scarce in detail on purpose*
Special thanks to the [first person I met in person here in London just because of CW](http://www.codewars.com/users/webtechalex) and that assisted me during the creation of this kata ;)
Also feel free to reuse/extend the following starter code:
```python
def triple_shiftian(base,n):
```
|
{"functional": "_inputs = [[[1, 1, 1], 25], [[1, 2, 3], 25], [[3, 2, 1], 25], [[6, 7, 2], 25], [[1, 1, 1], 35], [[1, 2, 3], 35], [[3, 2, 1], 35], [[6, 7, 2], 35], [[3, 2, 1], 0], [[6, 7, 2], 2]]\n_outputs = [[1219856746], [2052198929], [2827228055], [-2575238999], [10127083068293], [17037073417493], [23471258855679], [-21379280887590], [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(triple_shiftian(*i), o[0])"}
| 312
| 396
|
coding
|
Solve the programming task below in a Python markdown code block.
Given an array a_1, a_2, ..., a_{n} of n integers, find the largest number in the array that is not a perfect square.
A number x is said to be a perfect square if there exists an integer y such that x = y^2.
-----Input-----
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of elements in the array.
The second line contains n integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — the elements of the array.
It is guaranteed that at least one element of the array is not a perfect square.
-----Output-----
Print the largest number in the array which is not a perfect square. It is guaranteed that an answer always exists.
-----Examples-----
Input
2
4 2
Output
2
Input
8
1 2 4 8 16 32 64 576
Output
32
-----Note-----
In the first sample case, 4 is a perfect square, so the largest number in the array that is not a perfect square is 2.
|
{"inputs": ["1\n2\n", "1\n2\n", "1\n-1\n", "1\n-1\n", "1\n-2\n", "1\n-4\n", "1\n-6\n", "1\n-8\n"], "outputs": ["2\n", "2\n", "-1\n", "-1\n", "-2\n", "-4\n", "-6\n", "-8\n"]}
| 270
| 92
|
coding
|
Solve the programming task below in a Python markdown code block.
The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For instance:
The MEX of [2, 2, 1] is 0 because 0 does not belong to the array.
The MEX of [3, 1, 0, 1] is 2 because 0 and 1 belong to the array, but 2 does not.
The MEX of [0, 3, 1, 2] is 4 because 0, 1, 2 and 3 belong to the array, but 4 does not.
You're given an array A containing 2\cdot N integers where 0 ≤ A_{i} ≤ N. Is it possible to reorder the elements of the array in such a way that the MEX of the first N elements is equal to the MEX of the last N elements?
------ Input Format ------
- The first line contains T denoting the number of test cases. Then the test cases follow.
- The first line of each test case contains a single integer N.
- The second line contains 2\cdot N space-separated integers A_{1}, A_{2}, \dots, A_{2\cdot N}.
------ Output Format ------
For each test case, print YES if there is a valid reordering of the given array and NO otherwise.
You may print each character of the string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical).
------ Constraints ------
$1 ≤ T ≤ 10^{4}$
$1 ≤ N ≤ 10^{5}$
$0 ≤ A_{i} ≤ N$
- Sum of $N$ over all test cases does not exceed $2\cdot 10^{5}$.
----- Sample Input 1 ------
4
2
0 0 0 1
2
0 0 1 1
3
1 3 2 3 3 2
3
0 0 1 1 1 2
----- Sample Output 1 ------
NO
YES
YES
NO
----- explanation 1 ------
Test case $1$: There is no way to reorder the elements of the array $[0,0,0,1]$ which satisfies the given conditions.
Test case $2$: One possible reordering is $[0,1,0,1]$. Here the MEX of the first half is MEX$([0,1])=2$ and the MEX of the second half is MEX$([0,1])=2$.
Test case $3$: The given array already satisfies the conditions. Here the MEX of the first half is MEX$([1,3,2])=0$ and the MEX of the second half is MEX$([3,3,2])=0$.
|
{"inputs": ["4\n2\n0 0 0 1\n2\n0 0 1 1\n3\n1 3 2 3 3 2\n3\n0 0 1 1 1 2\n"], "outputs": ["NO\nYES\nYES\nNO\n"]}
| 632
| 68
|
coding
|
Solve the programming task below in a Python markdown code block.
A team of students from the city S is sent to the All-Berland Olympiad in Informatics. Traditionally, they go on the train. All students have bought tickets in one carriage, consisting of n compartments (each compartment has exactly four people). We know that if one compartment contain one or two students, then they get bored, and if one compartment contain three or four students, then the compartment has fun throughout the entire trip.
The students want to swap with other people, so that no compartment with students had bored students. To swap places with another person, you need to convince him that it is really necessary. The students can not independently find the necessary arguments, so they asked a sympathetic conductor for help. The conductor can use her life experience to persuade any passenger to switch places with some student.
However, the conductor does not want to waste time persuading the wrong people, so she wants to know what is the minimum number of people necessary to persuade her to change places with the students. Your task is to find the number.
After all the swaps each compartment should either have no student left, or have a company of three or four students.
-----Input-----
The first line contains integer n (1 ≤ n ≤ 10^6) — the number of compartments in the carriage. The second line contains n integers a_1, a_2, ..., a_{n} showing how many students ride in each compartment (0 ≤ a_{i} ≤ 4). It is guaranteed that at least one student is riding in the train.
-----Output-----
If no sequence of swapping seats with other people leads to the desired result, print number "-1" (without the quotes). In another case, print the smallest number of people you need to persuade to swap places.
-----Examples-----
Input
5
1 2 2 4 3
Output
2
Input
3
4 1 1
Output
2
Input
4
0 3 0 4
Output
0
|
{"inputs": ["1\n1\n", "1\n4\n", "1\n1\n", "1\n4\n", "1\n2\n", "2\n1 1\n", "2\n2 3\n", "2\n1 4\n"], "outputs": ["-1\n", "0\n", "-1\n", "0\n", "-1\n", "-1\n", "-1\n", "-1\n"]}
| 424
| 93
|
coding
|
Solve the programming task below in a Python markdown code block.
Priyanka works for an international toy company that ships by container. Her task is to the determine the lowest cost way to combine her orders for shipping. She has a list of item weights. The shipping company has a requirement that all items loaded in a container must weigh less than or equal to 4 units plus the weight of the minimum weight item. All items meeting that requirement will be shipped in one container.
What is the smallest number of containers that can be contracted to ship the items based on the given list of weights?
For example, there are items with weights $w=[1,2,3,4,5,10,11,12,13]$. This can be broken into two containers: $[1,2,3,4,5]$ and $[10,11,12,13]$. Each container will contain items weighing within $\begin{array}{c}4\end{array}$ units of the minimum weight item.
Function Description
Complete the toys function in the editor below. It should return the minimum number of containers required to ship.
toys has the following parameter(s):
w: an array of integers that represent the weights of each order to ship
Input Format
The first line contains an integer $n$, the number of orders to ship.
The next line contains $n$ space-separated integers, $w[1],w[2],\ldots,w[n]$, representing the orders in a weight array.
Constraints
$1\leq n\leq10^5$
$0\leq w[i]\leq10^4,where\ i\in[1,n]$
Output Format
Return the integer value of the number of containers Priyanka must contract to ship all of the toys.
Sample Input
8
1 2 3 21 7 12 14 21
Sample Output
4
Explanation
The first container holds items weighing $\mbox{I}$, $2$ and $3$. (weights in range $1\ldots5$)
The second container holds the items weighing $21$ units. ($21\ldots25$)
The third container holds the item weighing $7$ units. ($7...11$)
The fourth container holds the items weighing $12$ and $\mathbf{14}$ units. ($12...14$)
$\begin{array}{c}4\end{array}$ containers are required.
|
{"inputs": ["8\n1 2 3 21 7 12 14 21\n"], "outputs": ["4\n"]}
| 554
| 34
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given an array of strings ideas that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:
Choose 2 distinct names from ideas, call them ideaA and ideaB.
Swap the first letters of ideaA and ideaB with each other.
If both of the new names are not found in the original ideas, then the name ideaA ideaB (the concatenation of ideaA and ideaB, separated by a space) is a valid company name.
Otherwise, it is not a valid name.
Return the number of distinct valid names for the company.
Please complete the following python code precisely:
```python
class Solution:
def distinctNames(self, ideas: List[str]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(ideas = [\"coffee\",\"donuts\",\"time\",\"toffee\"]) == 6\n assert candidate(ideas = [\"lack\",\"back\"]) == 0\n\n\ncheck(Solution().distinctNames)"}
| 176
| 60
|
coding
|
Solve the programming task below in a Python markdown code block.
The enmity between Logan and Donald Pierce never ceases to exist.
However, once Logan and Donald came face to face. Donald got agitated and asked Logan to prove his true mettle by solving a puzzle.
Logan is provided an array Arr[] of N integers. He can select only a single integer from the array and can perform certain special operations on it any number of time.
An operation can be one of the following type :
Decrement the integer by 1 provided that it is greater than 0
Increment the integer by 1
Donald asks Logan to tell the least number of operations required to obtain the smallest possible XOR sum of all the integers.
Since Logan is currently in hurry to meet Charles and Canibal, he asks you guys to solve this problem. Help him!!
------ Input ------
First line contains an integer T denoting number of test cases.
First line of each test case contains an integer N.
Second line of each test case contains N space separated integers
------ Output ------
For each test case, print an integer denoting the minimum number of required operations on a separate line.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 10^{5}$
$0 ≤ Arr[i] ≤ 4294967296$
----- Sample Input 1 ------
1
4
1 1 1 1
----- Sample Output 1 ------
0
----- explanation 1 ------
Since the XOR sum of all numbers is 0 and can't be reduced further, the answer is 0.
|
{"inputs": ["1\n4\n1 1 1 1"], "outputs": ["0"]}
| 349
| 22
|
coding
|
Solve the programming task below in a Python markdown code block.
Sereja has two sequences a and b and number p. Sequence a consists of n integers a1, a2, ..., an. Similarly, sequence b consists of m integers b1, b2, ..., bm. As usual, Sereja studies the sequences he has. Today he wants to find the number of positions q (q + (m - 1)·p ≤ n; q ≥ 1), such that sequence b can be obtained from sequence aq, aq + p, aq + 2p, ..., aq + (m - 1)p by rearranging elements.
Sereja needs to rush to the gym, so he asked to find all the described positions of q.
Input
The first line contains three integers n, m and p (1 ≤ n, m ≤ 2·105, 1 ≤ p ≤ 2·105). The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109). The next line contains m integers b1, b2, ..., bm (1 ≤ bi ≤ 109).
Output
In the first line print the number of valid qs. In the second line, print the valid values in the increasing order.
Examples
Input
5 3 1
1 2 3 2 1
1 2 3
Output
2
1 3
Input
6 3 2
1 3 2 2 3 1
1 2 3
Output
2
1 2
|
{"inputs": ["1 1 2\n1\n2\n", "1 1 1\n1\n1\n", "2 2 1\n1 2\n1 2\n", "2 2 1\n1 2\n2 1\n", "2 2 1\n1 2\n4 1\n", "2 2 1\n1 1\n4 1\n", "2 2 1\n2 1\n4 1\n", "2 2 1\n2 1\n4 2\n"], "outputs": ["0\n", "1\n1 ", "1\n1 ", "1\n1 ", "0\n", "0\n", "0\n", "0\n"]}
| 341
| 161
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given an integer n, return a binary string representing its representation in base -2.
Note that the returned string should not have leading zeros unless the string is "0".
Please complete the following python code precisely:
```python
class Solution:
def baseNeg2(self, n: int) -> str:
```
|
{"functional": "def check(candidate):\n assert candidate(n = 2) == \"110\"\n assert candidate(n = 3) == \"111\"\n assert candidate(n = 4) == \"100\"\n\n\ncheck(Solution().baseNeg2)"}
| 82
| 65
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.