task_type
stringclasses 1
value | problem
stringlengths 261
3.34k
| answer
stringlengths 35
6.15k
| problem_tokens
int64 62
774
| answer_tokens
int64 12
2.04k
|
|---|---|---|---|---|
coding
|
Solve the programming task below in a Python markdown code block.
Little Dima has two sequences of points with integer coordinates: sequence (a_1, 1), (a_2, 2), ..., (a_{n}, n) and sequence (b_1, 1), (b_2, 2), ..., (b_{n}, n).
Now Dima wants to count the number of distinct sequences of points of length 2·n that can be assembled from these sequences, such that the x-coordinates of points in the assembled sequence will not decrease. Help him with that. Note that each element of the initial sequences should be used exactly once in the assembled sequence.
Dima considers two assembled sequences (p_1, q_1), (p_2, q_2), ..., (p_{2·}n, q_{2·}n) and (x_1, y_1), (x_2, y_2), ..., (x_{2·}n, y_{2·}n) distinct, if there is such i (1 ≤ i ≤ 2·n), that (p_{i}, q_{i}) ≠ (x_{i}, y_{i}).
As the answer can be rather large, print the remainder from dividing the answer by number m.
-----Input-----
The first line contains integer n (1 ≤ n ≤ 10^5). The second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9). The third line contains n integers b_1, b_2, ..., b_{n} (1 ≤ b_{i} ≤ 10^9). The numbers in the lines are separated by spaces.
The last line contains integer m (2 ≤ m ≤ 10^9 + 7).
-----Output-----
In the single line print the remainder after dividing the answer to the problem by number m.
-----Examples-----
Input
1
1
2
7
Output
1
Input
2
1 2
2 3
11
Output
2
-----Note-----
In the first sample you can get only one sequence: (1, 1), (2, 1).
In the second sample you can get such sequences : (1, 1), (2, 2), (2, 1), (3, 2); (1, 1), (2, 1), (2, 2), (3, 2). Thus, the answer is 2.
|
{"inputs": ["1\n1\n2\n7\n", "1\n1\n2\n7\n", "2\n1 2\n1 2\n4\n", "2\n1 2\n2 2\n4\n", "2\n1 2\n2 2\n4\n", "2\n1 2\n1 2\n4\n", "2\n1 2\n2 3\n11\n", "2\n1 2\n2 6\n11\n"], "outputs": ["1\n", "1\n", "1\n", "3\n", "3\n", "1\n", "2\n", "2\n"]}
| 541
| 144
|
coding
|
Solve the programming task below in a Python markdown code block.
Gerald has n younger brothers and their number happens to be even. One day he bought n^2 candy bags. One bag has one candy, one bag has two candies, one bag has three candies and so on. In fact, for each integer k from 1 to n^2 he has exactly one bag with k candies.
Help him give n bags of candies to each brother so that all brothers got the same number of candies.
-----Input-----
The single line contains a single integer n (n is even, 2 ≤ n ≤ 100) — the number of Gerald's brothers.
-----Output-----
Let's assume that Gerald indexes his brothers with numbers from 1 to n. You need to print n lines, on the i-th line print n integers — the numbers of candies in the bags for the i-th brother. Naturally, all these numbers should be distinct and be within limits from 1 to n^2. You can print the numbers in the lines in any order.
It is guaranteed that the solution exists at the given limits.
-----Examples-----
Input
2
Output
1 4
2 3
-----Note-----
The sample shows Gerald's actions if he has two brothers. In this case, his bags contain 1, 2, 3 and 4 candies. He can give the bags with 1 and 4 candies to one brother and the bags with 2 and 3 to the other brother.
|
{"inputs": ["2\n", "4\n", "6\n", "8\n", "4\n", "6\n", "8\n", "2\n"], "outputs": ["1 4\n2 3\n", "1 16 2 15\n3 14 4 13\n5 12 6 11\n7 10 8 9\n", "1 36 2 35 3 34\n4 33 5 32 6 31\n7 30 8 29 9 28\n10 27 11 26 12 25\n13 24 14 23 15 22\n16 21 17 20 18 19\n", "1 64 2 63 3 62 4 61\n5 60 6 59 7 58 8 57\n9 56 10 55 11 54 12 53\n13 52 14 51 15 50 16 49\n17 48 18 47 19 46 20 45\n21 44 22 43 23 42 24 41\n25 40 26 39 27 38 28 37\n29 36 30 35 31 34 32 33\n", "1 16 2 15\n3 14 4 13\n5 12 6 11\n7 10 8 9\n", "1 36 2 35 3 34\n4 33 5 32 6 31\n7 30 8 29 9 28\n10 27 11 26 12 25\n13 24 14 23 15 22\n16 21 17 20 18 19\n", "1 64 2 63 3 62 4 61\n5 60 6 59 7 58 8 57\n9 56 10 55 11 54 12 53\n13 52 14 51 15 50 16 49\n17 48 18 47 19 46 20 45\n21 44 22 43 23 42 24 41\n25 40 26 39 27 38 28 37\n29 36 30 35 31 34 32 33\n", "1 4\n2 3\n"]}
| 311
| 712
|
coding
|
Solve the programming task below in a Python markdown code block.
There are 2 trains A and B and N stations in a line from 1 to N in order. There is also an array P of length N-1 such that P_{i} (1≤ i < N) denotes the amount of time any train takes to go from the i-th station to the (i+1)-th station.
Initially, both trains are at station 1. Train A departs from station 1 and stops directly at station N. For safety purposes, it is maintained that train B cannot depart from station i unless train A has already reached station (i+1) (1 ≤ i < N).
Find the minimum time after which train B reaches station N, once train A departs from station 1.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains an integer N, denoting number of stations.
- The next line contains N-1 space-separated integers, P_{1},P_{2},\ldots ,P_{N-1}.
------ Output Format ------
For each test case, output on a new line the minimum time after which train B reaches station N.
------ Constraints ------
$1 ≤ T ≤ 100$
$2 ≤ N ≤ 10^{5}$
$1 ≤ P_{i} ≤ 10^{3}$
- The sum of $N$ over all test cases won't exceed $10^{5}$.
----- Sample Input 1 ------
3
2
4
3
3 5
4
5 2 6
----- Sample Output 1 ------
8
13
19
----- explanation 1 ------
Test case $1$: $A$ reaches station $2$ at $t=4$ and at this time $B$ departs from station $1$ and reaches station $2$ at $t=4+4=8$.
Test case $2$: Following is the timeline of two trains-
- At $t=3$, $A$ reaches station $2$ and $B$ departs from station $1$.
- At $t=6$, $B$ reaches station $2$ but $A$ has not yet reached station $3$, so train $B$ will wait at station $2$.
- At $t=8$, $A$ reaches station $3$ and $B$ departs from station $2$.
- At $t=8+5=13$, train $B$ reaches station $3$.
|
{"inputs": ["3\n2\n4\n3\n3 5\n4\n5 2 6\n"], "outputs": ["8\n13\n19\n"]}
| 567
| 38
|
coding
|
Solve the programming task below in a Python markdown code block.
It is important to use strong passwords to make the Internet more secure. At the same time, it is very important not to reuse the same password. No matter how strong your password is, if the plaintext is leaked in one place, it will be very easy to break. Of course, if all applications hash passwords and use SALT properly, such damage will not occur even if leaked, but there are still applications that store passwords in clear text.
Well, the contest is about to begin. I have to register a team account ...
problem
A string representing the password is given. Check if the character string meets all of the following conditions, and if it meets all of the following conditions, output "VALID", and if there is an item that does not meet even one, output "INVALID".
* String length is 6 or more
* Contains one or more numbers
* Contains one or more uppercase letters
* Contains one or more lowercase letters
input
A string representing the password is given on one line.
output
Print "VALID" if the password string meets all the conditions in question, and "INVALID" if there are conditions that do not.
Constraint
* The password string must be between 1 and 20 characters.
* Password string contains only uppercase letters, lowercase letters, and numbers
Input / output example
Input 1
password
Output 1
INVALID
It is a password that is widely used all over the world.
Input 2
AizuCamp2013
Output 2
VALID
Great password.
Input 3
1234
Output 3
INVALID
Widely used as a PIN.
Input 4
NaBiO3
Output 4
VALID
It is barely 6 characters or more.
Example
Input
password
Output
INVALID
|
{"inputs": ["passxord", "parsxosd", "parsxpsd", "qarsxpsd", "qarxspsd", "dspsxraq", "qarxspsc", "cspsxraq"], "outputs": ["INVALID\n", "INVALID\n", "INVALID\n", "INVALID\n", "INVALID\n", "INVALID\n", "INVALID\n", "INVALID\n"]}
| 385
| 87
|
coding
|
Solve the programming task below in a Python markdown code block.
Books are indexed. Write a program which reads a list of pairs of a word and a page number, and prints the word and a list of the corresponding page numbers.
You can assume that a word consists of at most 30 characters, and the page number is less than or equal to 1000. The number of pairs of a word and a page number is less than or equal to 100. A word never appear in a page more than once.
The words should be printed in alphabetical order and the page numbers should be printed in ascending order.
Input
word page_number
::
::
Output
word
a_list_of_the_page_number
word
a_list_of_the_Page_number
::
::
Example
Input
style 12
even 25
introduction 3
easy 9
style 7
document 13
style 21
even 18
Output
document
13
easy
9
even
18 25
introduction
3
style
7 12 21
|
{"inputs": ["mtzse 6\nveen 1\nnntroductioi 0\nsf_z 1\nstyle 32\ntndmucod 0\nstyld 5\nnuee 0", "mtzse 6\nveen 0\nnntroductioi 0\nsf_y 1\nstyle 17\ntndmucod 13\nstyld 7\nnuee 0", "mtzse 6\nveen 0\nnntroductioi 0\nsf_y 1\nstyle 32\ntndmucod 13\nstyld 7\nnuee 0", "mtzse 6\nveen 0\nnntroductioi 0\nsf_y 1\nstyle 32\ntndmucod 13\nstyld 5\nnuee 0", "mtzse 6\nveen 0\nnntroductioi 0\nsf_z 1\nstyle 32\ntndmucod 13\nstyld 5\nnuee 0", "mtzse 6\nveen 1\nnntroductioi 0\nsf_z 1\nstyle 32\ntndmucod 13\nstyld 5\nnuee 0", "mtzse 11\nveen 1\nnntroductioi 0\nsf_z 1\nstyle 32\ntndmucod 0\nstyld 5\nnuee 0", "mtzsd 11\nveen 1\nnntroductioi 0\nsf_z 1\nstyle 32\ntndmucod 0\nstyld 5\nnuee 0"], "outputs": ["mtzse\n6\nnntroductioi\n0\nnuee\n0\nsf_z\n1\nstyld\n5\nstyle\n32\ntndmucod\n0\nveen\n1\n", "mtzse\n6\nnntroductioi\n0\nnuee\n0\nsf_y\n1\nstyld\n7\nstyle\n17\ntndmucod\n13\nveen\n0\n", "mtzse\n6\nnntroductioi\n0\nnuee\n0\nsf_y\n1\nstyld\n7\nstyle\n32\ntndmucod\n13\nveen\n0\n", "mtzse\n6\nnntroductioi\n0\nnuee\n0\nsf_y\n1\nstyld\n5\nstyle\n32\ntndmucod\n13\nveen\n0\n", "mtzse\n6\nnntroductioi\n0\nnuee\n0\nsf_z\n1\nstyld\n5\nstyle\n32\ntndmucod\n13\nveen\n0\n", "mtzse\n6\nnntroductioi\n0\nnuee\n0\nsf_z\n1\nstyld\n5\nstyle\n32\ntndmucod\n13\nveen\n1\n", "mtzse\n11\nnntroductioi\n0\nnuee\n0\nsf_z\n1\nstyld\n5\nstyle\n32\ntndmucod\n0\nveen\n1\n", "mtzsd\n11\nnntroductioi\n0\nnuee\n0\nsf_z\n1\nstyld\n5\nstyle\n32\ntndmucod\n0\nveen\n1\n"]}
| 237
| 828
|
coding
|
Solve the programming task below in a Python markdown code block.
*Gormint aunty is very upset with the government and Imran wants to prove that his government is not sold out. Gormint aunty gives an ethical problem which only an honest person could solve. Help Imran solve this question.*
You are given two integers N and K. Find the minimum number of colors needed to color a circular array of size N such that the following condition is satisfied:
For any subarray of size K, there should not be any repeating colors.
Also construct one such array. If there are many possible arrays, you may print any one of them. Each color will be represented by a unique number between 1 and X, where X is the number of distinct colors needed to build this array.
Note: A circular array is one wherein the first element is adjacent to the last. For example, given the circular array [1,2,3], all possible subarrays of size 2 are: [1,2], [2,3], [3,1].
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- The first and only line of each test case contains two space-separated integers N and K, as described in the statement.
------ Output Format ------
For each test case, output two lines:
- The first line should contain a single integer X, denoting the minimum number of colors needed to build the array.
- The second line should contain N space-separated integers denoting the elements of the array. Each element of the array should be an integer between 1 and X, denoting the color of this element; and no subarray of this array of length K should contain repeated colors. If there are multiple such arrays, print any of them.
------ Constraints ------
$1 ≤ T ≤ 10^{4}$
$1 ≤ N ≤ 5 \cdot 10^{5}$
$1 ≤ K ≤ N$
- The sum of $N$ over all test cases won't exceed $5 \cdot 10^{5}$.
----- Sample Input 1 ------
3
5 4
3 1
6 3
----- Sample Output 1 ------
5
1 2 3 4 5
1
1 1 1
3
1 2 3 1 2 3
----- explanation 1 ------
Test case $1$: All elements of the given array are distinct, so it obviously satisfies the condition.
It can be shown that in this case, $4$ colors is not enough to construct a solution.
Test case $2$: Every subarray of length $1$ contains a single element, and hence no repeated colors.
|
{"inputs": ["3\n5 4\n3 1\n6 3\n"], "outputs": ["5\n1 2 3 4 5\n1\n1 1 1\n3\n1 2 3 1 2 3"]}
| 574
| 57
|
coding
|
Solve the programming task below in a Python markdown code block.
Your company was appointed to lay new asphalt on the highway of length $n$. You know that every day you can either repair one unit of the highway (lay new asphalt over one unit of the highway) or skip repairing.
Skipping the repair is necessary because of the climate. The climate in your region is periodical: there are $g$ days when the weather is good and if you lay new asphalt these days it becomes high-quality pavement; after that, the weather during the next $b$ days is bad, and if you lay new asphalt these days it becomes low-quality pavement; again $g$ good days, $b$ bad days and so on.
You can be sure that you start repairing at the start of a good season, in other words, days $1, 2, \dots, g$ are good.
You don't really care about the quality of the highway, you just want to make sure that at least half of the highway will have high-quality pavement. For example, if the $n = 5$ then at least $3$ units of the highway should have high quality; if $n = 4$ then at least $2$ units should have high quality.
What is the minimum number of days is needed to finish the repair of the whole highway?
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 10^4$) — the number of test cases.
Next $T$ lines contain test cases — one per line. Each line contains three integers $n$, $g$ and $b$ ($1 \le n, g, b \le 10^9$) — the length of the highway and the number of good and bad days respectively.
-----Output-----
Print $T$ integers — one per test case. For each test case, print the minimum number of days required to repair the whole highway if at least half of it should have high quality.
-----Example-----
Input
3
5 1 1
8 10 10
1000000 1 1000000
Output
5
8
499999500000
-----Note-----
In the first test case, you can just lay new asphalt each day, since days $1, 3, 5$ are good.
In the second test case, you can also lay new asphalt each day, since days $1$-$8$ are good.
|
{"inputs": ["3\n6 1 1\n9 2 9\n1000000 1 1000000\n", "3\n6 1 2\n9 2 9\n1000000 1 1000000\n", "3\n5 1 2\n9 1 4\n1000001 1 1000100\n", "3\n5 1 2\n9 2 4\n1000001 1 1000100\n", "3\n5 1 1\n9 2 4\n1000000 1 1000000\n", "3\n4 1 1\n8 6 6\n1000001 2 1000000\n", "3\n3 1 2\n9 2 4\n1000001 1 1000100\n", "3\n5 1 1\n9 2 2\n1000000 1 1000000\n"], "outputs": ["6\n23\n499999500000\n", "7\n23\n499999500000\n", "7\n21\n500050500001\n", "7\n13\n500050500001\n", "5\n13\n499999500000\n", "4\n8\n250000500001\n", "4\n13\n500050500001\n", "5\n9\n499999500000\n"]}
| 530
| 436
|
coding
|
Solve the programming task below in a Python markdown code block.
Takahashi has come to an integer shop to buy an integer.
The shop sells the integers from 1 through 10^9. The integer N is sold for A \times N + B \times d(N) yen (the currency of Japan), where d(N) is the number of digits in the decimal notation of N.
Find the largest integer that Takahashi can buy when he has X yen. If no integer can be bought, print 0.
-----Constraints-----
- All values in input are integers.
- 1 \leq A \leq 10^9
- 1 \leq B \leq 10^9
- 1 \leq X \leq 10^{18}
-----Input-----
Input is given from Standard Input in the following format:
A B X
-----Output-----
Print the greatest integer that Takahashi can buy. If no integer can be bought, print 0.
-----Sample Input-----
10 7 100
-----Sample Output-----
9
The integer 9 is sold for 10 \times 9 + 7 \times 1 = 97 yen, and this is the greatest integer that can be bought.
Some of the other integers are sold for the following prices:
- 10: 10 \times 10 + 7 \times 2 = 114 yen
- 100: 10 \times 100 + 7 \times 3 = 1021 yen
- 12345: 10 \times 12345 + 7 \times 5 = 123485 yen
|
{"inputs": ["10 7 100\n", "2 1 100000000000\n", "1234 56789 314159265\n", "5 826766933 11100722310\n", "18 395432867 10840610026\n", "1000000000 1000000000 100\n", "384836991 191890310 91047313\n", "782177068 404011431 1793399970\n"], "outputs": ["9\n", "1000000000\n", "254309\n", "731963982\n", "404539679\n", "0\n", "0\n", "1\n"]}
| 373
| 266
|
coding
|
Solve the programming task below in a Python markdown code block.
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.
For example, some $-9$-binary ("minus nine" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).
The boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.
And if $p=-9$ we can represent $7$ as one number $(2^4-9)$.
Note that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).
-----Input-----
The only line contains two integers $n$ and $p$ ($1 \leq n \leq 10^9$, $-1000 \leq p \leq 1000$).
-----Output-----
If it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.
-----Examples-----
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
-----Note-----
$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.
In the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.
In the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.
In the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible.
|
{"inputs": ["1 1\n", "1 0\n", "5 2\n", "9 8\n", "3 2\n", "2 1\n", "9 4\n", "1 0\n"], "outputs": ["-1\n", "1\n", "-1\n", "1\n", "1\n", "1\n", "-1\n", "1\n"]}
| 670
| 87
|
coding
|
Solve the programming task below in a Python markdown code block.
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way.
First, he splits the Banh-mi into $n$ parts, places them on a row and numbers them from $1$ through $n$. For each part $i$, he defines the deliciousness of the part as $x_i \in \{0, 1\}$. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the $i$-th part then his enjoyment of the Banh-mi will increase by $x_i$ and the deliciousness of all the remaining parts will also increase by $x_i$. The initial enjoyment of JATC is equal to $0$.
For example, suppose the deliciousness of $3$ parts are $[0, 1, 0]$. If JATC eats the second part then his enjoyment will become $1$ and the deliciousness of remaining parts will become $[1, \_, 1]$. Next, if he eats the first part then his enjoyment will become $2$ and the remaining parts will become $[\_, \_, 2]$. After eating the last part, JATC's enjoyment will become $4$.
However, JATC doesn't want to eat all the parts but to save some for later. He gives you $q$ queries, each of them consisting of two integers $l_i$ and $r_i$. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range $[l_i, r_i]$ in some order.
All the queries are independent of each other. Since the answer to the query could be very large, print it modulo $10^9+7$.
-----Input-----
The first line contains two integers $n$ and $q$ ($1 \le n, q \le 100\,000$).
The second line contains a string of $n$ characters, each character is either '0' or '1'. The $i$-th character defines the deliciousness of the $i$-th part.
Each of the following $q$ lines contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) — the segment of the corresponding query.
-----Output-----
Print $q$ lines, where $i$-th of them contains a single integer — the answer to the $i$-th query modulo $10^9 + 7$.
-----Examples-----
Input
4 2
1011
1 4
3 4
Output
14
3
Input
3 2
111
1 2
3 3
Output
3
1
-----Note-----
In the first example: For query $1$: One of the best ways for JATC to eats those parts is in this order: $1$, $4$, $3$, $2$. For query $2$: Both $3$, $4$ and $4$, $3$ ordering give the same answer.
In the second example, any order of eating parts leads to the same answer.
|
{"inputs": ["1 1\n0\n1 1\n", "1 1\n0\n1 1\n", "1 1\n1\n1 1\n", "3 2\n111\n1 2\n3 3\n", "3 2\n011\n1 2\n3 3\n", "3 2\n011\n1 2\n2 3\n", "3 2\n110\n1 2\n3 3\n", "3 2\n111\n1 2\n2 3\n"], "outputs": ["0\n", "0\n", "1\n", "3\n1\n", "2\n1\n", "2\n3\n", "3\n0\n", "3\n3\n"]}
| 742
| 174
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
There is an n x n grid, with the top-left cell at (0, 0) and the bottom-right cell at (n - 1, n - 1). You are given the integer n and an integer array startPos where startPos = [startrow, startcol] indicates that a robot is initially at cell (startrow, startcol).
You are also given a 0-indexed string s of length m where s[i] is the ith instruction for the robot: 'L' (move left), 'R' (move right), 'U' (move up), and 'D' (move down).
The robot can begin executing from any ith instruction in s. It executes the instructions one by one towards the end of s but it stops if either of these conditions is met:
The next instruction will move the robot off the grid.
There are no more instructions left to execute.
Return an array answer of length m where answer[i] is the number of instructions the robot can execute if the robot begins executing from the ith instruction in s.
Please complete the following python code precisely:
```python
class Solution:
def executeInstructions(self, n: int, startPos: List[int], s: str) -> List[int]:
```
|
{"functional": "def check(candidate):\n assert candidate(n = 3, startPos = [0,1], s = \"RRDDLU\") == [1,5,4,3,1,0]\n assert candidate(n = 2, startPos = [1,1], s = \"LURD\") == [4,1,0,0]\n assert candidate(n = 1, startPos = [0,0], s = \"LRUD\") == [0,0,0,0]\n\n\ncheck(Solution().executeInstructions)"}
| 272
| 121
|
coding
|
Solve the programming task below in a Python markdown code block.
Mysterious Chefland… Recently, Chef realised that Discuss, the educational system of Chefland, is out of date. Therefore, he is trying to find ways to update the infrastructure in the country. One possible way is to move all materials from Discuss to Discourse.
Chef will have access to Discourse if his knowledge and power become exactly equal to $N$ and $M$ respectively. Initially, he has power $1$ and knowledge $1$.
Chef can perform actions of the following types to improve his skills:
- solve a problem — increase his knowledge by $X$
- do a push-up — increase his power by $Y$
- install ShareChat to keep in touch with friends — increase both knowledge and power by $1$
Chef can only install ShareChat at most once. The remaining actions may be performed any number of times and the actions may be performed in any order.
Help Chef find out whether it is possible to move from Discuss to Discourse.
-----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 four space-separated integers $N$, $M$, $X$ and $Y$.
-----Output-----
For each test case, print a single line containing the string "Chefirnemo" if it is possible to reach the required knowledge and power or "Pofik" if it is impossible.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le N, M, X, Y \le 10^9$
-----Subtasks-----
Subtask #1 (30 points): $1 \le N, M, X, Y \le 100$
Subtask #2 (70 points): original constraints
-----Example Input-----
5
2 2 1 2
11 10 5 9
11 11 5 9
12 11 5 9
1 2 1 100
-----Example Output-----
Chefirnemo
Chefirnemo
Pofik
Chefirnemo
Pofik
-----Explanation-----
Example case 2: We add $Y=9$ once to the power to get power $10$. We add $X=5$ twice to the knowledge to get knowledge $11$.
Example case 3: We can see that it is impossible to reach power $M=11$ no matter which or how many operations we do. Note that the ShareChat operation will increase both knowledge and power by $1$, and hence it will still be impossible to attain the given values of knowledge and power at the same time.
Example case 4: We can reach knowledge $11$ and power $10$ like in example case 2, the only difference is that we also use the ShareChat operation to increase both by $1$.
|
{"inputs": ["5\n2 2 1 2\n11 10 5 9\n11 11 5 9\n12 11 5 9\n1 2 1 100"], "outputs": ["Chefirnemo\nChefirnemo\nPofik\nChefirnemo\nPofik"]}
| 639
| 81
|
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 transparent three-dimensional table, the height equals to H and the base is a two-dimensional table N∙M.
The layers of the 3D table are numbered from 1 to H. Each layer is a two-dimensional table, the rows are numbered from 1 to N and the columns are numbered from 1 to M. A pair (i, j) (1 ≤ i ≤ N, 1 ≤ j ≤ M) corresponds to the cell at the intersection of i'th row and j'th column of a two-dimensional table.
We can paint cells of the three-dimensional table, but not more than T_{k} cells on k'th layer can be painted. Painting of one cell on k'th layer costs C_{k}. Your task is to find the minimum cost of painting the table thus that it can't be seen throught from the top (there is no cell which is unpainted on every layer). Formally you need to find a painting, that for any pair (i, j) (1 ≤ i ≤ N, 1 ≤ j ≤ M) there is a layer k (1 ≤ k ≤ H), where cell (i, j) is painted. If there is no such a painting then the answer is "Impossible".
------ Input ------
The first line contains three integers N, M and H.
Each of the next H lines contains two integers T_{k} and C_{k}.
------ Output ------
The output should contain the minimum cost of the painting if there is one, otherwise output Impossible without quotes.
------ Constraints ------
1 ≤ N∙M ≤ 10^{12};
1 ≤ H ≤ 100000;
0 ≤ T_{k} ≤ 10^{12}, for each layer;
0 ≤ C_{k} ≤ 10^{5}, for each layer.
----- Sample Input 1 ------
3 4 2
6 1
6 2
----- Sample Output 1 ------
18
----- explanation 1 ------
In the first example, we should paint 6 cells on the first layer and 6 cells on the second layer.
----- Sample Input 2 ------
2 2 3
1 1
1 2
1 3
----- Sample Output 2 ------
Impossible
----- explanation 2 ------
In the second example, there are not enough cells to paint the table properly.
|
{"inputs": ["3 4 2\n6 1\n6 2", "2 2 3\n1 1\n1 2\n1 3", "1 2 3\n1 1\n1 2\n1 3", "1 2 3\n1 1\n1 1\n1 3", "1 2 3\n0 1\n1 1\n0 5", "1 2 3\n1 1\n1 3\n1 3", "1 2 3\n1 0\n1 1\n2 5", "1 2 3\n0 1\n2 3\n1 3"], "outputs": ["18", "Impossible", "3\n", "2\n", "Impossible\n", "4\n", "1\n", "6\n"]}
| 528
| 185
|
coding
|
Solve the programming task below in a Python markdown code block.
Deepak like strings which are in flip flop in nature. For example, he likes XYXYX, while he doesn't like XXYX. Now he want to convert every string into a string which he likes. for this, he only delete the character in the string.
Now find the minimum number of delete operation which is required to covert a string that deepak likes.
Input Format :
The first line contains an integer N i.e the number of test cases.
Next N lines contain a string each.
Output Format :
For each test case, print the minimum delete operation required.
Constraints :
1 ≤ N ≤ 10
1 ≤ length of String ≤ 10^5
SAMPLE INPUT
5
XXXX
YYYYY
XYXYXYXY
YXYXYX
XXXYYY
SAMPLE OUTPUT
3
4
0
0
4
Explanation
XXXX => X, 3 deletions
YYYYY => Y, 4 deletions
XYXYXYXY => XYXYXYXY, 0 deletions
YXYXYXYX => YXYXYXYX, 0 deletions
XXXYYY => XY, 4 deletions
|
{"inputs": ["5\nXXXX\nYYYYY\nXYXYXYXY\nYXYXYX\nXXXYYY"], "outputs": ["3\n4\n0\n0\n4"]}
| 252
| 38
|
coding
|
Solve the programming task below in a Python markdown code block.
in Chefland, there is a very famous street where $N$ types of street food (numbered $1$ through $N$) are offered. For each valid $i$, there are $S_i$ stores that offer food of the $i$-th type, the price of one piece of food of this type is $V_i$ (the same in each of these stores) and each day, $P_i$ people come to buy it; each of these people wants to buy one piece of food of the $i$-th type.
Chef is planning to open a new store at this street, where he would offer food of one of these $N$ types. Chef assumes that the people who want to buy the type of food he'd offer will split equally among all stores that offer it, and if this is impossible, i.e. the number of these people $p$ is not divisible by the number of these stores $s$, then only $\left\lfloor\frac{p}{s}\right\rfloor$ people will buy food from Chef.
Chef wants to maximise his daily profit. Help Chef choose which type of food to offer and find the maximum daily profit he can make.
-----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$.
- $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains three space-separated integers $S_i$, $P_i$ and $V_i$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum profit.
-----Constraints-----
- $1 \le T \le 100$
- $1 \le N \le 100$
- $1 \le S_i, V_i, P_i \le 10,000$ for each valid $i$
-----Subtasks-----
Subtask #1 (100 points): original constraints
-----Example Input-----
2
3
4 6 8
2 6 6
1 4 3
1
7 7 4
-----Example Output-----
12
0
-----Explanation-----
Example case 1: Chef should offer food of the second type. On each day, two people would buy from him, so his daily profit would be $12$.
Example case 2: Chef has no option other than to offer the only type of food, but he does not expect anyone to buy from him anyway, so his daily profit is $0$.
|
{"inputs": ["2\n3\n4 6 8\n2 6 6\n1 4 3\n1\n7 7 4"], "outputs": ["12\n0"]}
| 581
| 43
|
coding
|
Solve the programming task below in a Python markdown code block.
Eugeny has array a = a_1, a_2, ..., a_{n}, consisting of n integers. Each integer a_{i} equals to -1, or to 1. Also, he has m queries: Query number i is given as a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). The response to the query will be integer 1, if the elements of array a can be rearranged so as the sum a_{l}_{i} + a_{l}_{i} + 1 + ... + a_{r}_{i} = 0, otherwise the response to the query will be integer 0.
Help Eugeny, answer all his queries.
-----Input-----
The first line contains integers n and m (1 ≤ n, m ≤ 2·10^5). The second line contains n integers a_1, a_2, ..., a_{n} (a_{i} = -1, 1). Next m lines contain Eugene's queries. The i-th line contains integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n).
-----Output-----
Print m integers — the responses to Eugene's queries in the order they occur in the input.
-----Examples-----
Input
2 3
1 -1
1 1
1 2
2 2
Output
0
1
0
Input
5 5
-1 1 1 1 -1
1 1
2 3
3 5
2 5
1 5
Output
0
1
0
1
0
|
{"inputs": ["1 1\n1\n1 1\n", "1 1\n1\n1 1\n", "1 1\n-1\n1 1\n", "1 1\n-1\n1 1\n", "1 1\n-1\n0 1\n", "1 1\n-1\n1 2\n", "1 1\n-1\n1 3\n", "1 1\n-1\n-1 1\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
| 369
| 141
|
coding
|
Solve the programming task below in a Python markdown code block.
This time no story, no theory. The examples below show you how to write function `accum`:
**Examples:**
```
accum("abcd") -> "A-Bb-Ccc-Dddd"
accum("RqaEzty") -> "R-Qq-Aaa-Eeee-Zzzzz-Tttttt-Yyyyyyy"
accum("cwAt") -> "C-Ww-Aaa-Tttt"
```
The parameter of accum is a string which includes only letters from `a..z` and `A..Z`.
Also feel free to reuse/extend the following starter code:
```python
def accum(s):
```
|
{"functional": "_inputs = [['ZpglnRxqenU'], ['NyffsGeyylB'], ['MjtkuBovqrU'], ['EvidjUnokmM'], ['HbideVbxncC'], ['VwhvtHtrxfE'], ['KurgiKmkphY'], ['NctlfBlnmfH'], ['WegunHvbdmV'], ['VoywwSpqidE'], ['VbaixFpxdcO'], ['OlyqvYwkuzF'], ['JrhfdMtchiH'], ['JiwpcSwslvW'], ['EagpiEvmabJ'], ['RznlcEmuxxP'], ['OrggaExarzP'], ['DriraMtedfB'], ['BjxseRxgtjT'], ['EquhxOswchE']]\n_outputs = [['Z-Pp-Ggg-Llll-Nnnnn-Rrrrrr-Xxxxxxx-Qqqqqqqq-Eeeeeeeee-Nnnnnnnnnn-Uuuuuuuuuuu'], ['N-Yy-Fff-Ffff-Sssss-Gggggg-Eeeeeee-Yyyyyyyy-Yyyyyyyyy-Llllllllll-Bbbbbbbbbbb'], ['M-Jj-Ttt-Kkkk-Uuuuu-Bbbbbb-Ooooooo-Vvvvvvvv-Qqqqqqqqq-Rrrrrrrrrr-Uuuuuuuuuuu'], ['E-Vv-Iii-Dddd-Jjjjj-Uuuuuu-Nnnnnnn-Oooooooo-Kkkkkkkkk-Mmmmmmmmmm-Mmmmmmmmmmm'], ['H-Bb-Iii-Dddd-Eeeee-Vvvvvv-Bbbbbbb-Xxxxxxxx-Nnnnnnnnn-Cccccccccc-Ccccccccccc'], ['V-Ww-Hhh-Vvvv-Ttttt-Hhhhhh-Ttttttt-Rrrrrrrr-Xxxxxxxxx-Ffffffffff-Eeeeeeeeeee'], ['K-Uu-Rrr-Gggg-Iiiii-Kkkkkk-Mmmmmmm-Kkkkkkkk-Ppppppppp-Hhhhhhhhhh-Yyyyyyyyyyy'], ['N-Cc-Ttt-Llll-Fffff-Bbbbbb-Lllllll-Nnnnnnnn-Mmmmmmmmm-Ffffffffff-Hhhhhhhhhhh'], ['W-Ee-Ggg-Uuuu-Nnnnn-Hhhhhh-Vvvvvvv-Bbbbbbbb-Ddddddddd-Mmmmmmmmmm-Vvvvvvvvvvv'], ['V-Oo-Yyy-Wwww-Wwwww-Ssssss-Ppppppp-Qqqqqqqq-Iiiiiiiii-Dddddddddd-Eeeeeeeeeee'], ['V-Bb-Aaa-Iiii-Xxxxx-Ffffff-Ppppppp-Xxxxxxxx-Ddddddddd-Cccccccccc-Ooooooooooo'], ['O-Ll-Yyy-Qqqq-Vvvvv-Yyyyyy-Wwwwwww-Kkkkkkkk-Uuuuuuuuu-Zzzzzzzzzz-Fffffffffff'], ['J-Rr-Hhh-Ffff-Ddddd-Mmmmmm-Ttttttt-Cccccccc-Hhhhhhhhh-Iiiiiiiiii-Hhhhhhhhhhh'], ['J-Ii-Www-Pppp-Ccccc-Ssssss-Wwwwwww-Ssssssss-Lllllllll-Vvvvvvvvvv-Wwwwwwwwwww'], ['E-Aa-Ggg-Pppp-Iiiii-Eeeeee-Vvvvvvv-Mmmmmmmm-Aaaaaaaaa-Bbbbbbbbbb-Jjjjjjjjjjj'], ['R-Zz-Nnn-Llll-Ccccc-Eeeeee-Mmmmmmm-Uuuuuuuu-Xxxxxxxxx-Xxxxxxxxxx-Ppppppppppp'], ['O-Rr-Ggg-Gggg-Aaaaa-Eeeeee-Xxxxxxx-Aaaaaaaa-Rrrrrrrrr-Zzzzzzzzzz-Ppppppppppp'], ['D-Rr-Iii-Rrrr-Aaaaa-Mmmmmm-Ttttttt-Eeeeeeee-Ddddddddd-Ffffffffff-Bbbbbbbbbbb'], ['B-Jj-Xxx-Ssss-Eeeee-Rrrrrr-Xxxxxxx-Gggggggg-Ttttttttt-Jjjjjjjjjj-Ttttttttttt'], ['E-Qq-Uuu-Hhhh-Xxxxx-Oooooo-Sssssss-Wwwwwwww-Ccccccccc-Hhhhhhhhhh-Eeeeeeeeeee']]\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(accum(*i), o[0])"}
| 143
| 1,064
|
coding
|
Solve the programming task below in a Python markdown code block.
Luke is daydreaming in Math class. He has a sheet of graph paper with $n$ rows and $m$ columns, and he imagines that there is an army base in each cell for a total of $n\cdot m$ bases. He wants to drop supplies at strategic points on the sheet, marking each drop point with a red dot. If a base contains at least one package inside or on top of its border fence, then it's considered to be supplied. For example:
Given $n$ and $m$, what's the minimum number of packages that Luke must drop to supply all of his bases?
Example
$n=2$
$m=3$
Packages can be dropped at the corner between cells (0, 0), (0, 1), (1, 0) and (1, 1) to supply ${4}$ bases. Another package can be dropped at a border between (0, 2) and (1, 2). This supplies all bases using $2$ packages.
Function Description
Complete the gameWithCells function in the editor below.
gameWithCells has the following parameters:
int n: the number of rows in the game
int m: the number of columns in the game
Returns
int: the minimum number of packages required
Input Format
Two space-separated integers describing the respective values of $n$ and $m$.
Constraints
$0<n,m\leq1000$
Sample Input 0
2 2
Sample Output 0
1
Explanation 0
Luke has four bases in a $2\times2$ grid. If he drops a single package where the walls of all four bases intersect, then those four cells can access the package:
Because he managed to supply all four bases with a single supply drop, we print ${1}$ as our answer.
|
{"inputs": ["2 2\n"], "outputs": ["1\n"]}
| 402
| 16
|
coding
|
Solve the programming task below in a Python markdown code block.
Indian summer is such a beautiful time of the year! A girl named Alyona is walking in the forest and picking a bouquet from fallen leaves. Alyona is very choosy — she doesn't take a leaf if it matches the color and the species of the tree of one of the leaves she already has. Find out how many leaves Alyona has picked.
Input
The first line contains an integer n (1 ≤ n ≤ 100) — the number of leaves Alyona has found. The next n lines contain the leaves' descriptions. Each leaf is characterized by the species of the tree it has fallen from and by the color. The species of the trees and colors are given in names, consisting of no more than 10 lowercase Latin letters. A name can not be an empty string. The species of a tree and the color are given in each line separated by a space.
Output
Output the single number — the number of Alyona's leaves.
Examples
Input
5
birch yellow
maple red
birch yellow
maple yellow
maple green
Output
4
Input
3
oak yellow
oak yellow
oak yellow
Output
1
|
{"inputs": ["1\nqvwli hz\n", "1\nqvilw hz\n", "1\nqvilw gz\n", "1\nqvilw zg\n", "1\nqvhlw zg\n", "1\nqvhlw gz\n", "1\nqvhlv gz\n", "1\nrvhlv gz\n"], "outputs": ["1", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n"]}
| 259
| 111
|
coding
|
Solve the programming task below in a Python markdown code block.
# Task
Get the digits sum of `n`th number from the [Look-and-Say sequence](http://en.wikipedia.org/wiki/Look-and-say_sequence)(1-based).
`1, 11, 21, 1211, 111221, 312211, 13112221, 1113213211, ...`
# Input/Output
`[input]` integer `n`
`n`th number in the sequence to get where `1 <= n <= 55` and `n=1 is "1"`.
[output] an integer
The sum of digits in `n`th number from the `Look-and-Say` sequence.
# Example
For `n = 2`, the output shoule be 2.
`"11" --> 1 + 1 --> 2`
For `n = 3`, the output shoule be 3.
`"21" --> 2 + 1 --> 3`
For `n = 4`, the output shoule be 5.
`"1211" --> 1 + 2 + 1 + 1 --> 5`
Also feel free to reuse/extend the following starter code:
```python
def look_and_say_and_sum(n):
```
|
{"functional": "_inputs = [[1], [2], [3], [4], [5]]\n_outputs = [[1], [2], [3], [5], [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(look_and_say_and_sum(*i), o[0])"}
| 308
| 183
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given an array $a$ of $n$ elements. You can apply the following operation to it any number of times:
Select some subarray from $a$ of even size $2k$ that begins at position $l$ ($1\le l \le l+2\cdot{k}-1\le n$, $k \ge 1$) and for each $i$ between $0$ and $k-1$ (inclusive), assign the value $a_{l+k+i}$ to $a_{l+i}$.
For example, if $a = [2, 1, 3, 4, 5, 3]$, then choose $l = 1$ and $k = 2$, applying this operation the array will become $a = [3, 4, 3, 4, 5, 3]$.
Find the minimum number of operations (possibly zero) needed to make all the elements of the array equal.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^4$) — the number of test cases. Description of the test cases follows.
The first line of each test case contains an integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the length of the array.
The second line of each test case consists of $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq n$) — the elements of the array $a$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
Print $t$ lines, each line containing the answer to the corresponding test case — the minimum number of operations needed to make equal all the elements of the array with the given operation.
-----Examples-----
Input
5
3
1 1 1
2
2 1
5
4 4 4 2 4
4
4 2 1 3
1
1
Output
0
1
1
2
0
-----Note-----
In the first test, all elements are equal, therefore no operations are needed.
In the second test, you can apply one operation with $k=1$ and $l=1$, set $a_1 := a_2$, and the array becomes $[1, 1]$ with $1$ operation.
In the third test, you can apply one operation with $k=1$ and $l=4$, set $a_4 := a_5$, and the array becomes $[4, 4, 4, 4, 4]$.
In the fourth test, you can apply one operation with $k=1$ and $l=3$, set $a_3 := a_4$, and the array becomes $[4, 2, 3, 3]$, then you can apply another operation with $k=2$ and $l=1$, set $a_1 := a_3$, $a_2 := a_4$, and the array becomes $[3, 3, 3, 3]$.
In the fifth test, there is only one element, therefore no operations are needed.
|
{"inputs": ["5\n3\n1 1 1\n2\n2 1\n5\n4 4 4 2 4\n4\n4 2 1 3\n1\n1\n"], "outputs": ["0\n1\n1\n2\n0\n"]}
| 733
| 62
|
coding
|
Solve the programming task below in a Python markdown code block.
Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special.
Alyona is a capricious girl so after she gets the array, she inspects m of its subarrays. Subarray is a set of some subsequent elements of the array. The i-th subarray is described with two integers li and ri, and its elements are a[li], a[li + 1], ..., a[ri].
Alyona is going to find mex for each of the chosen subarrays. Among these m mexes the girl is going to find the smallest. She wants this minimum mex to be as large as possible.
You are to find an array a of n elements so that the minimum mex among those chosen by Alyona subarrays is as large as possible.
The mex of a set S is a minimum possible non-negative integer that is not in S.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 105).
The next m lines contain information about the subarrays chosen by Alyona. The i-th of these lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n), that describe the subarray a[li], a[li + 1], ..., a[ri].
Output
In the first line print single integer — the maximum possible minimum mex.
In the second line print n integers — the array a. All the elements in a should be between 0 and 109.
It is guaranteed that there is an optimal answer in which all the elements in a are between 0 and 109.
If there are multiple solutions, print any of them.
Examples
Input
5 3
1 3
2 5
4 5
Output
2
1 0 2 1 0
Input
4 2
1 4
2 4
Output
3
5 2 0 1
Note
The first example: the mex of the subarray (1, 3) is equal to 3, the mex of the subarray (2, 5) is equal to 3, the mex of the subarray (4, 5) is equal to 2 as well, thus the minumal mex among the subarrays chosen by Alyona is equal to 2.
|
{"inputs": ["1 1\n1 1\n", "9 1\n1 1\n", "2 1\n2 2\n", "6 2\n4 6\n2 5\n", "4 2\n1 4\n2 4\n", "4 2\n1 4\n2 3\n", "4 2\n2 4\n2 4\n", "4 2\n1 2\n2 3\n"], "outputs": ["1\n0\n", "1\n0 0 0 0 0 0 0 0 0\n", "1\n0 0\n", "3\n0 1 2 0 1 2\n", "3\n0 1 2 0\n", "2\n0 1 0 1 \n", "3\n0 1 2 0 \n", "2\n0 1 0 1 \n"]}
| 505
| 209
|
coding
|
Solve the programming task below in a Python markdown code block.
Chef 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.
Let F(X) equals to the number of lucky digits in decimal representation of X. Chef wants to know the number of such integers X, that L ≤ X ≤ R and F(X) is a lucky number. Help him and calculate that number modulo 109+7.
-----Input-----
First line contains one integer T, the number of test cases. Each of the following T lines contains two space separated positive integers L and R.
-----Output-----
For each of the T test cases print one integer, the number of such X, that L ≤ X ≤ R and F(X) is a lucky number, modulo 1000000007.
-----Constraints-----
1 ≤ T ≤ 10
1 ≤ L ≤ R ≤ 101000
-----Example-----
Input:
4
1 100
1 10000
1 100000
4444 4447
Output:
0
16
640
2
-----Notes-----
First test case: of course, any number of less than 4 digits can't contain lucky number of lucky digits, so the answer is 0.
Second test case: 16 required numbers are 4444 4447 4474 4477 4744 4747 4774 4777 7444 7447 7474 7477 7744 7747 7774 7777.
Third test case: there are 640 required lucky numbers. Some of them are 4474, 14747, 41474, 77277, 44407, 74749.
Fourth test case: the only two required numbers are 4444 and 4447.
|
{"inputs": ["4\n1 100\n1 10000\n1 100000\n4444 4447\n\n"], "outputs": ["0\n16\n640\n2"]}
| 493
| 56
|
coding
|
Solve the programming task below in a Python markdown code block.
It is the easy version of the problem. The difference is that in this version, there are no nodes with already chosen colors.
Theofanis is starving, and he wants to eat his favorite food, sheftalia. However, he should first finish his homework. Can you help him with this problem?
You have a perfect binary tree of $2^k - 1$ nodes — a binary tree where all vertices $i$ from $1$ to $2^{k - 1} - 1$ have exactly two children: vertices $2i$ and $2i + 1$. Vertices from $2^{k - 1}$ to $2^k - 1$ don't have any children. You want to color its vertices with the $6$ Rubik's cube colors (White, Green, Red, Blue, Orange and Yellow).
Let's call a coloring good when all edges connect nodes with colors that are neighboring sides in the Rubik's cube.
A picture of Rubik's cube and its 2D map.
More formally:
a white node can not be neighboring with white and yellow nodes;
a yellow node can not be neighboring with white and yellow nodes;
a green node can not be neighboring with green and blue nodes;
a blue node can not be neighboring with green and blue nodes;
a red node can not be neighboring with red and orange nodes;
an orange node can not be neighboring with red and orange nodes;
You want to calculate the number of the good colorings of the binary tree. Two colorings are considered different if at least one node is colored with a different color.
The answer may be too large, so output the answer modulo $10^9+7$.
-----Input-----
The first and only line contains the integers $k$ ($1 \le k \le 60$) — the number of levels in the perfect binary tree you need to color.
-----Output-----
Print one integer — the number of the different colorings modulo $10^9+7$.
-----Examples-----
Input
3
Output
24576
Input
14
Output
934234
-----Note-----
In the picture below, you can see one of the correct colorings of the first example.
|
{"inputs": ["3\n", "1\n", "2\n", "4\n", "2\n", "1\n", "4\n", "5\n"], "outputs": ["24576\n", "6\n", "96\n", "610612729\n", "96\n", "6\n", "610612729\n", "218379003\n"]}
| 482
| 100
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given an n × m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the table
abcd
edfg
hijk
we obtain the table:
acd
efg
hjk
A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good.
-----Input-----
The first line contains two integers — n and m (1 ≤ n, m ≤ 100).
Next n lines contain m small English letters each — the characters of the table.
-----Output-----
Print a single number — the minimum number of columns that you need to remove in order to make the table good.
-----Examples-----
Input
1 10
codeforces
Output
0
Input
4 4
case
care
test
code
Output
2
Input
5 4
code
forc
esco
defo
rces
Output
4
-----Note-----
In the first sample the table is already good.
In the second sample you may remove the first and third column.
In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).
Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t.
|
{"inputs": ["2 1\nb\na\n", "2 1\nb\na\n", "2 1\nc\na\n", "2 1\nc\nb\n", "2 2\nfb\nye\n", "2 2\nfb\nye\n", "2 2\nbf\nye\n", "2 3\naxc\nbxa\n"], "outputs": ["1\n", "1", "1\n", "1\n", "0\n", "0", "0\n", "0\n"]}
| 378
| 118
|
coding
|
Solve the programming task below in a Python markdown code block.
# Task
Given an array `arr`, find the rank of the element at the ith position.
The `rank` of the arr[i] is a value equal to the number of elements `less than or equal to` arr[i] standing before arr[i], plus the number of elements `less than` arr[i] standing after arr[i].
# Example
For `arr = [2,1,2,1,2], i = 2`, the result should be `3`.
There are 2 elements `less than or equal to` arr[2] standing before arr[2]:
`arr[0] <= arr[2]`
`arr[1] <= arr[2]`
There is only 1 element `less than` arr[2] standing after arr[2]:
`arr[3] < arr[2]`
So the result is `2 + 1 = 3`.
# Input/Output
- `[input]` integer array `arr`
An array of integers.
`3 <= arr.length <= 50.`
- `[input]` integer `i`
Index of the element whose rank is to be found.
- `[output]` an integer
Rank of the element at the ith position.
Also feel free to reuse/extend the following starter code:
```python
def rank_of_element(arr,i):
```
|
{"functional": "_inputs = [[[2, 1, 2, 1, 2], 2], [[2, 1, 2, 2, 2], 2], [[3, 2, 3, 4, 1], 0], [[3, 2, 3, 4, 1], 1], [[3, 2, 3, 4, 1], 2]]\n_outputs = [[3], [2], [2], [1], [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(rank_of_element(*i), o[0])"}
| 304
| 254
|
coding
|
Solve the programming task below in a Python markdown code block.
There is enough money available on ATM in nominal value 10, 20, 50, 100, 200 and 500 dollars.
You are given money in nominal value of `n` with `1<=n<=1500`.
Try to find minimal number of notes that must be used to repay in dollars, or output -1 if it is impossible.
Good Luck!!!
Also feel free to reuse/extend the following starter code:
```python
def solve(n):
```
|
{"functional": "_inputs = [[770], [550], [10], [1250], [125], [666], [42]]\n_outputs = [[4], [2], [1], [4], [-1], [-1], [-1]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(solve(*i), o[0])"}
| 121
| 203
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.
Consider a very long K-digit number N with digits d_{0}, d_{1}, ..., d_{K-1} (in decimal notation; d_{0} is the most significant and d_{K-1} the least significant digit). This number is so large that we can't give it to you on the input explicitly; instead, you are only given its starting digits and a way to construct the remainder of the number.
Specifically, you are given d_{0} and d_{1}; for each i ≥ 2, d_{i} is the sum of all preceding (more significant) digits, modulo 10 — more formally, the following formula must hold:
Determine if N is a multiple of 3.
------ Input ------
The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
The first and only line of each test case contains three space-separated integers K, d_{0} and d_{1}.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the number N is a multiple of 3 or "NO" (without quotes) otherwise.
------ Constraints ------
$1 ≤ T ≤ 1000$
$2 ≤ K ≤ 10^{12}$
$1 ≤ d_{0} ≤ 9$
$0 ≤ d_{1} ≤ 9$
----- Sample Input 1 ------
3
5 3 4
13 8 1
760399384224 5 1
----- Sample Output 1 ------
NO
YES
YES
----- explanation 1 ------
Example case 1: The whole number N is 34748, which is not divisible by 3, so the answer is NO.
Example case 2: The whole number N is 8198624862486, which is divisible by 3, so the answer is YES.
|
{"inputs": ["3\n5 3 4\n13 8 1\n760399384224 5 1"], "outputs": ["NO\nYES\nYES"]}
| 455
| 46
|
coding
|
Solve the programming task below in a Python markdown code block.
Chef has two integers X and S. He is interested in sequences of non-negative integers such that the sum of their elements is S and the [bitwise OR] of their elements is X. Chef would like to know the shortest possible length such a sequence can have.
Formally, find the smallest positive integer N such that there exists a sequence A = [A_{1}, A_{2}, \ldots, A_{N}] satisfying the following conditions:
Each A_{i} is a non-negative integer
A_{1} + A_{2} + \ldots + A_{N} = S
A_{1} \vee A_{2} \vee \ldots \vee A_{N} = X
where \vee denotes the bitwise OR operation.
If there does not exist any sequence which satisfies the above conditions, output -1. Otherwise, output the minimum possible length of such a sequence.
------ 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 X and S — the required bitwise OR and sum, respectively.
------ Output Format ------
For each test case, output a single line containing one integer — the shortest possible length of such a sequence, or -1 if no such sequence exists.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ X ≤ S ≤ 10^{9}$
------ subtasks ------
Subtask #1 (100 points): Original constraints
----- Sample Input 1 ------
3
13 23
6 13
1 25
----- Sample Output 1 ------
3
-1
25
----- explanation 1 ------
Test Case $1$: One of the valid arrays of length $3$ is $[9,9,5]$. It can be shown that there is no valid array with length $< 3$.
Test Case $2$: There does not exist any sequence whose sum is $13$ and bitwise OR is $6$.
Test Case $3$: The valid array of minimum length contains $25$ ones.
|
{"inputs": ["3\n13 23\n6 13\n1 25\n"], "outputs": ["3\n-1\n25\n"]}
| 483
| 36
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a positive integer N and an array A of size N. There are N lists L_{1}, L_{2} \ldots L_{N}. Initially, L_{i} = [A_{i}].
You can perform the following operation any number of times as long as there are at least 2 lists:
Select 2 (non-empty) lists L_{i} and L_{j} (i \neq j)
Append L_{j} to L_{i} and remove the list L_{j}. Note that this means L_{j} cannot be chosen in any future operation.
Find the minimum number of operations required to obtain a set of lists that satisfies the following conditions:
The first element and last element of each list are equal.
The first element of all the lists is the same.
Print -1 if it is not possible to achieve this via any sequence of operations.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer: the minimum number of operations required to obtain an array of lists that satisfies the given conditions.
Print -1 if it is impossible to achieve such an array of lists.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 2 \cdot 10^{5}$
$1 ≤ A_{i} ≤ N$
- Sum of $N$ over all test cases doesn't exceed $2 \cdot 10^{5}$
------ subtasks ------
Subtask 1(100 points): Original constraints
----- Sample Input 1 ------
3
1
1
2
1 2
3
1 1 2
----- Sample Output 1 ------
0
-1
2
----- explanation 1 ------
Test case $1$: There is only one list $[1]$, and it trivially satisfies the condition so no operations are required.
Test case $2$: There are only $2$ ways to do an operation - either take list $[1]$ and append it to list $[2]$ or take list $[2]$ and append it to list $[1]$. In both cases, it is not possible to satisfy both given conditions at the same time. Hence, the answer is $-1$.
Test case $3$: Here is one possible order of operations:
- Select the $3$rd list $[2]$ and append it to the $1$st list $[1]$.
- Then, select the $2$nd list $[1]$ and append it to the $1$st list $[1, 2]$.
Finally, we are left with the single list $[1, 2, 1]$ which satisfies the given conditions. It can be verified that it is impossible to do this using less than $2$ operations.
|
{"inputs": ["3\n1\n1\n2\n1 2\n3\n1 1 2\n"], "outputs": ["0\n-1\n2\n"]}
| 678
| 37
|
coding
|
Solve the programming task below in a Python markdown code block.
There are N people. The name of the i-th person is S_i.
We would like to choose three people so that the following conditions are met:
- The name of every chosen person begins with M, A, R, C or H.
- There are no multiple people whose names begin with the same letter.
How many such ways are there to choose three people, disregarding order?
Note that the answer may not fit into a 32-bit integer type.
-----Constraints-----
- 1 \leq N \leq 10^5
- S_i consists of uppercase English letters.
- 1 \leq |S_i| \leq 10
- S_i \neq S_j (i \neq j)
-----Input-----
Input is given from Standard Input in the following format:
N
S_1
:
S_N
-----Output-----
If there are x ways to choose three people so that the given conditions are met, print x.
-----Sample Input-----
5
MASHIKE
RUMOI
OBIRA
HABORO
HOROKANAI
-----Sample Output-----
2
We can choose three people with the following names:
- MASHIKE, RUMOI, HABORO
- MASHIKE, RUMOI, HOROKANAI
Thus, we have two ways.
|
{"inputs": ["1\nAAA\n", "5\nM\nA\nR\nC\nH\n", "4\nZY\nZZZ\nZ\nZZZZZZZZZZ", "4\nZY\nZZY\nZ\nZZZZZZZZZZ", "4\nZY\nYZZ\nZ\nZZZZZZZZZZ", "4\nZY\nYZZ\nY\nZZZZZZZZZZ", "4\nZY\nZYZ\nY\nZZZZZZZZZZ", "4\nZY\nZYZ\nY\nZYZZZZZZZZ"], "outputs": ["0\n", "10\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
| 294
| 155
|
coding
|
Solve the programming task below in a Python markdown code block.
Having learned the multiplication table, Takahashi can multiply two integers between 1 and 9 (inclusive) together.
Given an integer N, determine whether N can be represented as the product of two integers between 1 and 9. If it can, print Yes; if it cannot, print No.
-----Constraints-----
- 1 \leq N \leq 100
- N is an integer.
-----Input-----
Input is given from Standard Input in the following format:
N
-----Output-----
If N can be represented as the product of two integers between 1 and 9 (inclusive), print Yes; if it cannot, print No.
-----Sample Input-----
10
-----Sample Output-----
Yes
10 can be represented as, for example, 2 \times 5.
|
{"inputs": ["2", "1", "0", "3", "6", "4", "7", "8"], "outputs": ["Yes\n", "Yes\n", "No\n", "Yes\n", "Yes\n", "Yes\n", "Yes\n", "Yes\n"]}
| 178
| 62
|
coding
|
Solve the programming task below in a Python markdown code block.
For given integers m and n, compute mn (mod 1,000,000,007). Here, A (mod M) is the remainder when A is divided by M.
Constraints
* 1 ≤ m ≤ 100
* 1 ≤ n ≤ 109
Input
m n
Two integers m and n are given in a line.
Output
Print mn (mod 1,000,000,007) in a line.
Examples
Input
2 3
Output
8
Input
5 8
Output
390625
|
{"inputs": ["2 1", "5 9", "2 2", "7 9", "3 3", "1 3", "3 1", "0 1"], "outputs": ["2\n", "1953125\n", "4\n", "40353607\n", "27\n", "1\n", "3\n", "0\n"]}
| 147
| 92
|
coding
|
Solve the programming task below in a Python markdown code block.
Complete the function/method so that it returns the url with anything after the anchor (`#`) removed.
## Examples
```python
# returns 'www.codewars.com'
remove_url_anchor('www.codewars.com#about')
# returns 'www.codewars.com?page=1'
remove_url_anchor('www.codewars.com?page=1')
```
Also feel free to reuse/extend the following starter code:
```python
def remove_url_anchor(url):
```
|
{"functional": "_inputs = [['www.codewars.com#about'], ['www.codewars.com/katas/?page=1#about'], ['www.codewars.com/katas/']]\n_outputs = [['www.codewars.com'], ['www.codewars.com/katas/?page=1'], ['www.codewars.com/katas/']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(remove_url_anchor(*i), o[0])"}
| 112
| 213
|
coding
|
Solve the programming task below in a Python markdown code block.
The Head Chef is receiving a lot of orders for cooking the best of the problems lately. For this, he organized an hiring event to hire some talented Chefs. He gave the following problem to test the skills of the participating Chefs. Can you solve this problem and be eligible for getting hired by Head Chef.
A non-negative number n is said to be magical if it satisfies the following property. Let S denote the multi-set of numbers corresponding to the non-empty subsequences of the digits of the number n in decimal representation. Please note that the numbers in the set S can have leading zeros. Let us take an element s of the multi-set S, prod(s) denotes the product of all the digits of number s in decimal representation.
The number n will be called magical if sum of prod(s) for all elements s in S, is even.
For example, consider a number 246, its all possible non-empty subsequence will be S = {2, 4, 6, 24, 46, 26, 246}. Products of digits of these subsequences will be {prod(2) = 2, prod(4) = 4, prod(6) = 6, prod(24) = 8, prod(46) = 24, prod(26) = 12, prod(246) = 48, i.e. {2, 4, 6, 8, 24, 12, 48}. Sum of all of these is 104, which is even. Hence 246 is a magical number.
Please note that multi-set S can contain repeated elements, e.g. if number is 55, then S = {5, 5, 55}. Products of digits of these subsequences will be {prod(5) = 5, prod(5) = 5, prod(55) = 25}, i.e. {5, 5, 25}. Sum of all of these is 35 which is odd. Hence 55 is not a
magical number.
Consider a number 204, then S = {2, 0, 4, 20, 04, 24, 204}. Products of digits of these subsequences will be {2, 0, 4, 0, 0, 8, 0}. Sum of all these elements will be 14 which is even. So 204 is a magical number.
The task was to simply find the Kth magical number.
-----Input-----
- First line of the input contains an integer T denoting the number of test cases.
- Each of the next T lines contains a single integer K.
-----Output-----
For each test case, print a single integer corresponding to the Kth magical number.
-----Constraints-----
- 1 ≤ T ≤ 105
- 1 ≤ K ≤ 1012.
-----Subtasks-----
Subtask #1 : (20 points)
- 1 ≤ T ≤ 100
- 1 ≤ K ≤ 104.
Subtask 2 : (80 points) Original Constraints
-----Example-----
Input:
2
2
5
Output:
2
8
-----Explanation-----
Example case 1.
2 is the 2nd magical number, since it satisfies the property of the magical number. The first magical number will be of course 0.
|
{"inputs": ["2\n2\n5\n\n"], "outputs": ["2\n8"]}
| 756
| 20
|
coding
|
Solve the programming task below in a Python markdown code block.
Introduction
Mr. Safety loves numeric locks and his Nokia 3310. He locked almost everything in his house. He is so smart and he doesn't need to remember the combinations. He has an algorithm to generate new passcodes on his Nokia cell phone.
Task
Can you crack his numeric locks? Mr. Safety's treasures wait for you. Write an algorithm to open his numeric locks. Can you do it without his Nokia 3310?
Input
The `str` or `message` (Python) input string consists of lowercase and upercase characters. It's a real object that you want to unlock.
Output
Return a string that only consists of digits.
Example
```
unlock("Nokia") // => 66542
unlock("Valut") // => 82588
unlock("toilet") // => 864538
```
Also feel free to reuse/extend the following starter code:
```python
def unlock(message):
```
|
{"functional": "_inputs = [['Nokia'], ['Valut'], ['toilet'], ['waterheater'], ['birdhouse']]\n_outputs = [['66542'], ['82588'], ['864538'], ['92837432837'], ['247346873']]\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(unlock(*i), o[0])"}
| 226
| 215
|
coding
|
Solve the programming task below in a Python markdown code block.
Chef has fallen in love with Cheffina, and wants to buy N gifts for her. On reaching the gift shop, Chef got to know the following two things:
The cost of each gift is 1 coin.
On the purchase of every 4^{th} gift, Chef gets the 5^{th} gift free of cost.
What is the minimum number of coins that Chef will require in order to come out of the shop carrying N gifts?
------ Input Format ------
- The first line of input will contain an integer T — the number of test cases. The description of T test cases follows.
- The first and only line of each test case contains an integer N, the number of gifts in the shop.
------ Output Format ------
For each test case, output on a new line the minimum number of coins that Chef will require to obtain all N gifts.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{9}$
----- Sample Input 1 ------
2
5
4
----- Sample Output 1 ------
4
4
----- explanation 1 ------
Test case $1$: After purchasing $4$ gifts, Chef will get the $5^{th}$ gift free of cost. Hence Chef only requires $4$ coins in order to get $5$ gifts.
Test case $2$: Chef will require $4$ coins in order to get $4$ gifts.
|
{"inputs": ["2\n5\n4"], "outputs": ["4\n4"]}
| 314
| 18
|
coding
|
Solve the programming task below in a Python markdown code block.
Given a word w, rearrange the letters of w to construct another word s in such a way that s is lexicographic-ally greater than w. In case of multiple possible answers, find the lexicographic-ally smallest one.
Input Format
The first line of input contains t, the number of test cases. Each of the next t lines contains w.
Output Format
For each testcase, output a string lexicographically bigger than w in a separate line. In case of multiple possible answers, print the lexicographically smallest one, and if no answer exists, print no answer.
Constraints
1≤t≤105
1≤|w|≤100
w will contain only lower-case English letters and its length will not exceed 100.
SAMPLE INPUT
5
ab
bb
hefg
dhck
dkhc
SAMPLE OUTPUT
ba
no answer
hegf
dhkc
hcdk
Explanation
Test case 1:
There exists only one string greater than 'ab' which can be built by rearranging 'ab'. That is 'ba'.
**Test case 2:**
Not possible to rearrange 'bb' and get a lexicographically greater string.
**Test case 3:**
'hegf' is the next string lexicographically greater than 'hefg'.
**Test case 4:**
'dhkc' is the next string lexicographically greater than 'dhck'.
**Test case 5:**
'hcdk' is the next string lexicographically greater than 'dkhc'.
|
{"inputs": ["5\nab\nbb\nhefg\ndhck\ndkhc"], "outputs": ["ba\nno answer\nhegf\ndhkc\nhcdk"]}
| 339
| 41
|
coding
|
Solve the programming task below in a Python markdown code block.
Sunake is in the form of a polygonal line consisting of n vertices (without self-intersection). First, Sunake-kun's i-th vertex is at (xi, yi). You can move continuously by translating or rotating, but you cannot deform (change the length of the polygonal line or the angle between the two line segments). y = 0 is the wall, and there is a small hole at (0, 0). Determine if you can move through this hole so that the whole thing meets y <0.
Constraints
* 2 ≤ n ≤ 1000
* 0 ≤ xi ≤ 109
* 1 ≤ yi ≤ 109
* Lines do not have self-intersections
* None of the three points are on the same straight line
* (xi, yi) ≠ (xi + 1, yi + 1)
* All inputs are integers
Input
n
x1 y1
.. ..
xn yn
Output
Output "Possible" if you can move through the hole, and "Impossible" if you can't.
Examples
Input
4
0 1
1 1
1 2
2 2
Output
Possible
Input
11
63 106
87 143
102 132
115 169
74 145
41 177
56 130
28 141
19 124
0 156
22 183
Output
Impossible
|
{"inputs": ["4\n0 1\n1 1\n1 2\n3 2", "4\n0 2\n1 1\n1 2\n3 2", "4\n0 2\n1 1\n2 2\n3 2", "4\n0 1\n1 1\n1 2\n2 2", "11\n124 186\n8 143\n6 675\n0 186\n248 50\n-3 561\n154 66\n13 19\n37 0\n1 294\n1 30", "11\n124 186\n8 143\n6 675\n0 186\n248 50\n-3 561\n154 66\n13 19\n51 0\n1 294\n1 30", "11\n124 186\n0 143\n6 675\n0 186\n248 50\n-3 561\n154 66\n13 19\n51 0\n1 294\n1 30", "11\n68 186\n-1 143\n6 130\n-1 186\n248 47\n-3 561\n154 66\n13 4\n80 0\n1 294\n2 30"], "outputs": ["Possible\n", "Possible\n", "Possible\n", "Possible", "Impossible\n", "Impossible\n", "Impossible\n", "Impossible\n"]}
| 352
| 405
|
coding
|
Solve the programming task below in a Python markdown code block.
There are some animals in a garden. Each of them is a crane with two legs or a turtle with four legs.
Takahashi says: "there are X animals in total in the garden, and they have Y legs in total." Determine whether there is a combination of numbers of cranes and turtles in which this statement is correct.
-----Constraints-----
- 1 \leq X \leq 100
- 1 \leq Y \leq 100
- All values in input are integers.
-----Input-----
Input is given from Standard Input in the following format:
X Y
-----Output-----
If there is a combination of numbers of cranes and turtles in which the statement is correct, print Yes; otherwise, print No.
-----Sample Input-----
3 8
-----Sample Output-----
Yes
The statement "there are 3 animals in total in the garden, and they have 8 legs in total" is correct if there are two cranes and one turtle. Thus, there is a combination of numbers of cranes and turtles in which the statement is correct.
|
{"inputs": ["2 2", "6 8", "3 2", "3 1", "6 3", "5 1", "3 3", "5 2"], "outputs": ["No\n", "No\n", "No\n", "No\n", "No\n", "No\n", "No\n", "No\n"]}
| 238
| 78
|
coding
|
Solve the programming task below in a Python markdown code block.
Binod and his family live in Codingland. They have a festival called N-Halloween.
The festival is celebrated for N consecutive days. On each day Binod gets some candies from his mother. He may or may not take them. On a given day , Binod will be sad if he collected candies on that day and he does not have at least X candies remaining from the candies he collected that day at the end of that day . His friend Pelu who is very fond of candies asks for X candies from Binod on a day if Binod collects any candy on that day. He does so immediately after Binod collects them. Binod being a good friend never denies him.
Given a list a where ai denotes the number of candies Binod may take on the ith day and Q queries having a number X , find the maximum number of candies Binod can collect so that he won't be sad after the festival ends.
Input format:
First-line contains two integers N and Q denoting the number of festival days and number of queries respectively.
The second line is the array whose ith element is the maximum number of candies Binod may
collect on that day.
Each of the next Q lines contains the number X i.e the minimum number of candies Binod wants to have at the end of every day on which he collects any candy.
Output format:
For every query output, a single integer denoting the max number of candies Binod may
collect (for the given X ) to be happy after the festival ends.
Constraints:
1<=N,Q<=105
1<=Q<=105
0<=ai<=109
0<=X<=109
Sample Input :
5 2
4 6 5 8 7
1
2
Sample Output :
30
30
Sample Input:
6 3
20 10 12 3 30 5
2
6
13
Sample Output
77
62
30
Explanation:
In the first query of sample input 1, Binod can collect the maximum number of given chocolates for a given day each day as after the end of every day Binod will have a number of collected
chocolates on that day greater than equal to 1 even after giving 1 chocolate to Pelu. So the
the answer is 4+6+5+8+7=30.
|
{"inputs": ["5 2\n4 6 5 8 7\n1\n2", "6 3\n20 10 12 3 30 5\n2\n6\n13"], "outputs": ["30\n30", "77\n62\n30"]}
| 533
| 70
|
coding
|
Solve the programming task below in a Python markdown code block.
You're given a string containing a sequence of words separated with whitespaces. Let's say it is a sequence of patterns: a name and a corresponding number - like this:
```"red 1 yellow 2 black 3 white 4"```
You want to turn it into a different **string** of objects you plan to work with later on - like this:
```"[{name : 'red', id : '1'}, {name : 'yellow', id : '2'}, {name : 'black', id : '3'}, {name : 'white', id : '4'}]"```
Doing this manually is a pain. So you've decided to write a short function that would make the computer do the job for you. Keep in mind, the pattern isn't necessarily a word and a number. Consider anything separeted by a whitespace, just don't forget: an array of objects with two elements: name and id.
As a result you'll have a string you may just copy-paste whenever you feel like defining a list of objects - now without the need to put in names, IDs, curly brackets, colon signs, screw up everything, fail searching for a typo and begin anew. This might come in handy with large lists.
Also feel free to reuse/extend the following starter code:
```python
def words_to_object(s):
```
|
{"functional": "_inputs = [['red 1 yellow 2 black 3 white 4'], ['1 red 2 white 3 violet 4 green'], ['1 1 2 2 3 3 4 4'], ['#@&fhds 123F3f 2vn2# 2%y6D @%fd3 @!#4fs W@R^g WE56h%'], ['']]\n_outputs = [[\"[{name : 'red', id : '1'}, {name : 'yellow', id : '2'}, {name : 'black', id : '3'}, {name : 'white', id : '4'}]\"], [\"[{name : '1', id : 'red'}, {name : '2', id : 'white'}, {name : '3', id : 'violet'}, {name : '4', id : 'green'}]\"], [\"[{name : '1', id : '1'}, {name : '2', id : '2'}, {name : '3', id : '3'}, {name : '4', id : '4'}]\"], [\"[{name : '#@&fhds', id : '123F3f'}, {name : '2vn2#', id : '2%y6D'}, {name : '@%fd3', id : '@!#4fs'}, {name : 'W@R^g', id : 'WE56h%'}]\"], ['[]']]\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(words_to_object(*i), o[0])"}
| 291
| 466
|
coding
|
Solve the programming task below in a Python markdown code block.
Recently, the Fair Nut has written $k$ strings of length $n$, consisting of letters "a" and "b". He calculated $c$ — the number of strings that are prefixes of at least one of the written strings. Every string was counted only one time.
Then, he lost his sheet with strings. He remembers that all written strings were lexicographically not smaller than string $s$ and not bigger than string $t$. He is interested: what is the maximum value of $c$ that he could get.
A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$.
-----Input-----
The first line contains two integers $n$ and $k$ ($1 \leq n \leq 5 \cdot 10^5$, $1 \leq k \leq 10^9$).
The second line contains a string $s$ ($|s| = n$) — the string consisting of letters "a" and "b.
The third line contains a string $t$ ($|t| = n$) — the string consisting of letters "a" and "b.
It is guaranteed that string $s$ is lexicographically not bigger than $t$.
-----Output-----
Print one number — maximal value of $c$.
-----Examples-----
Input
2 4
aa
bb
Output
6
Input
3 3
aba
bba
Output
8
Input
4 5
abbb
baaa
Output
8
-----Note-----
In the first example, Nut could write strings "aa", "ab", "ba", "bb". These $4$ strings are prefixes of at least one of the written strings, as well as "a" and "b". Totally, $6$ strings.
In the second example, Nut could write strings "aba", "baa", "bba".
In the third example, there are only two different strings that Nut could write. If both of them are written, $c=8$.
|
{"inputs": ["1 1\na\na\n", "1 1\na\na\n", "2 4\naa\nbb\n", "2 4\nab\nbb\n", "2 4\nba\nbb\n", "2 5\nab\nbb\n", "2 4\naa\nbb\n", "3 3\naba\nbba\n"], "outputs": ["1", "1\n", "6\n", "5\n", "3\n", "5\n", "6\n", "8\n"]}
| 503
| 118
|
coding
|
Solve the programming task below in a Python markdown code block.
Teacher brought a fruit basket for three students. The basket contains only Apples, Mangoes and Oranges. Student A knows a value $a$, the total number of Apples and Mangoes in the Basket, B knows a value $b$, the total number of Mangoes and Oranges in the basket and Student C knows a value $c$, the total number of Oranges and Apples in the Basket. Since the teacher brought it he knows a value $d$ , the total number of fruits in the basket. You have to determine the exact number of Apples , Mangoes and Oranges in the basket separately.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- Each testcase contains of a single line of input, four integers $a,b,c,d$.
-----Output:-----
For each testcase, output in a single line the number of Apples , Mangoes and Oranges in this order.
-----Constraints-----
- $1 \leq T \leq 100$
- $0 \leq a \leq 1000$
- $0 \leq b \leq 1000$
- $0 \leq c \leq 1000$
- $0 \leq d \leq 1000$
-The Solution always exisits
-----Sample Input:-----
2
7 9 8 12
3 8 7 9
-----Sample Output:-----
3 4 5
1 2 6
|
{"inputs": ["2\n7 9 8 12\n3 8 7 9"], "outputs": ["3 4 5\n1 2 6"]}
| 342
| 39
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
A transaction is possibly invalid if:
the amount exceeds $1000, or;
if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
You are given an array of strings transaction where transactions[i] consists of comma-separated values representing the name, time (in minutes), amount, and city of the transaction.
Return a list of transactions that are possibly invalid. You may return the answer in any order.
Please complete the following python code precisely:
```python
class Solution:
def invalidTransactions(self, transactions: List[str]) -> List[str]:
```
|
{"functional": "def check(candidate):\n assert candidate(transactions = [\"alice,20,800,mtv\",\"alice,50,100,beijing\"]) == [\"alice,20,800,mtv\",\"alice,50,100,beijing\"]\n assert candidate(transactions = [\"alice,20,800,mtv\",\"alice,50,1200,mtv\"]) == [\"alice,50,1200,mtv\"]\n assert candidate(transactions = [\"alice,20,800,mtv\",\"bob,50,1200,mtv\"]) == [\"bob,50,1200,mtv\"]\n\n\ncheck(Solution().invalidTransactions)"}
| 144
| 185
|
coding
|
Solve the programming task below in a Python markdown code block.
# Task
An IP address contains four numbers(0-255) and separated by dots. It can be converted to a number by this way:
Given a string `s` represents a number or an IP address. Your task is to convert it to another representation(`number to IP address` or `IP address to number`).
You can assume that all inputs are valid.
# Example
Example IP address: `10.0.3.193`
Convert each number to a 8-bit binary string
(may needs to pad leading zeros to the left side):
```
10 --> 00001010
0 --> 00000000
3 --> 00000011
193 --> 11000001
```
Combine these four strings: `00001010 00000000 00000011 11000001` and then convert them to a decimal number:
`167773121`
# Input/Output
`[input]` string `s`
A number or IP address in string format.
`[output]` a string
A converted number or IP address in string format.
# Example
For `s = "10.0.3.193"`, the output should be `"167773121"`.
For `s = "167969729"`, the output should be `"10.3.3.193"`.
Also feel free to reuse/extend the following starter code:
```python
def numberAndIPaddress(s):
```
|
{"functional": "_inputs = [['10.0.3.193'], ['167969729']]\n_outputs = [['167773121'], ['10.3.3.193']]\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(numberAndIPaddress(*i), o[0])"}
| 391
| 196
|
coding
|
Solve the programming task below in a Python markdown code block.
$n$ people gathered to hold a jury meeting of the upcoming competition, the $i$-th member of the jury came up with $a_i$ tasks, which they want to share with each other.
First, the jury decides on the order which they will follow while describing the tasks. Let that be a permutation $p$ of numbers from $1$ to $n$ (an array of size $n$ where each integer from $1$ to $n$ occurs exactly once).
Then the discussion goes as follows:
If a jury member $p_1$ has some tasks left to tell, then they tell one task to others. Otherwise, they are skipped.
If a jury member $p_2$ has some tasks left to tell, then they tell one task to others. Otherwise, they are skipped.
...
If a jury member $p_n$ has some tasks left to tell, then they tell one task to others. Otherwise, they are skipped.
If there are still members with tasks left, then the process repeats from the start. Otherwise, the discussion ends.
A permutation $p$ is nice if none of the jury members tell two or more of their own tasks in a row.
Count the number of nice permutations. The answer may be really large, so print it modulo $998\,244\,353$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of the test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — number of jury members.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the number of problems that the $i$-th member of the jury came up with.
The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print one integer — the number of nice permutations, taken modulo $998\,244\,353$.
-----Examples-----
Input
4
2
1 2
3
5 5 5
4
1 3 3 7
6
3 4 2 1 3 3
Output
1
6
0
540
-----Note-----
Explanation of the first test case from the example:
There are two possible permutations, $p = [1, 2]$ and $p = [2, 1]$. For $p = [1, 2]$, the process is the following:
the first jury member tells a task;
the second jury member tells a task;
the first jury member doesn't have any tasks left to tell, so they are skipped;
the second jury member tells a task.
So, the second jury member has told two tasks in a row (in succession), so the permutation is not nice.
For $p = [2, 1]$, the process is the following:
the second jury member tells a task;
the first jury member tells a task;
the second jury member tells a task.
So, this permutation is nice.
|
{"inputs": ["1\n3\n11 11 154\n", "1\n3\n11 11 154\n", "1\n3\n13 11 154\n", "1\n3\n11 20 154\n", "1\n3\n13 11 297\n", "1\n3\n19 11 297\n", "1\n3\n38 11 297\n", "4\n2\n1 2\n3\n5 5 5\n4\n1 3 3 7\n6\n3 4 2 1 3 3\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "1\n6\n0\n540\n"]}
| 709
| 200
|
coding
|
Solve the programming task below in a Python markdown code block.
Nezzar's favorite digit among $1,\ldots,9$ is $d$. He calls a positive integer lucky if $d$ occurs at least once in its decimal representation.
Given $q$ integers $a_1,a_2,\ldots,a_q$, for each $1 \le i \le q$ Nezzar would like to know if $a_i$ can be equal to a sum of several (one or more) lucky numbers.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 9$) — the number of test cases.
The first line of each test case contains two integers $q$ and $d$ ($1 \le q \le 10^4$, $1 \le d \le 9$).
The second line of each test case contains $q$ integers $a_1,a_2,\ldots,a_q$ ($1 \le a_i \le 10^9$).
-----Output-----
For each integer in each test case, print "YES" in a single line if $a_i$ can be equal to a sum of lucky numbers. Otherwise, print "NO".
You can print letters in any case (upper or lower).
-----Examples-----
Input
2
3 7
24 25 27
10 7
51 52 53 54 55 56 57 58 59 60
Output
YES
NO
YES
YES
YES
NO
YES
YES
YES
YES
YES
YES
NO
-----Note-----
In the first test case, $24 = 17 + 7$, $27$ itself is a lucky number, $25$ cannot be equal to a sum of lucky numbers.
|
{"inputs": ["2\n3 2\n6 47 1\n10 3\n50 52 2 43 113 92 9 73 42 60\n", "2\n3 7\n24 44 40\n10 7\n4 52 4 54 64 54 66 2 25 60\n", "2\n3 2\n5 47 1\n10 3\n50 52 2 43 113 92 9 73 42 60\n", "2\n3 7\n10 25 27\n10 5\n13 52 53 2 55 56 57 58 9 8\n", "2\n3 2\n5 47 1\n10 3\n50 52 1 43 113 92 8 73 42 60\n", "2\n3 2\n24 44 10\n10 7\n4 52 4 54 64 54 66 1 25 60\n", "2\n3 2\n24 44 10\n10 7\n4 52 4 54 64 54 66 1 25 49\n", "2\n3 2\n24 44 10\n10 7\n4 52 4 54 64 54 66 1 47 49\n"], "outputs": ["YES\nYES\nNO\nYES\nYES\nNO\nYES\nYES\nYES\nYES\nYES\nYES\nYES\n", "YES\nYES\nNO\nNO\nYES\nNO\nYES\nYES\nYES\nYES\nNO\nNO\nNO\n", "NO\nYES\nNO\nYES\nYES\nNO\nYES\nYES\nYES\nYES\nYES\nYES\nYES\n", "NO\nNO\nYES\nNO\nYES\nYES\nNO\nYES\nYES\nYES\nYES\nNO\nNO\n", "NO\nYES\nNO\nYES\nYES\nNO\nYES\nYES\nYES\nNO\nYES\nYES\nYES\n", "YES\nYES\nYES\nNO\nYES\nNO\nYES\nYES\nYES\nYES\nNO\nNO\nNO\n", "YES\nYES\nYES\nNO\nYES\nNO\nYES\nYES\nYES\nYES\nNO\nNO\nYES\n", "YES\nYES\nYES\nNO\nYES\nNO\nYES\nYES\nYES\nYES\nNO\nYES\nYES\n"]}
| 399
| 622
|
coding
|
Solve the programming task below in a Python markdown code block.
Jack and Jill got into a serious fight. Jack didn't not belive Jill when she said that her memory was far better than the others. He asked her to prove it as follows :-
- Jack would dictate a long list of numbers to her. At any point after he has dictated at least "k" numbers, he can ask her what is the k-th minimum number (the k-th number if the numbers are arranged in increasing order) in the list so far.
- She succeeds in proving herself if she can tell the k-th minimum number every time he asks.
- The number of queries (dictate a new number or ask her the k-th minimum of the numbers given till then) has to be pre-decided before he starts asking.
Jill succeeded in proving herself by convincing Jack quite a few times. Now, Jill remembered that Jack had claimed that he was good at programming. It's her turn to ask him to prove his skill. Jack doesn't want to lose to her and comes to you for help. He asks you to make the program for him.
-----Input-----
Jill has agreed to let Jack decide the format of the input and here's what he has decided. First line of the input contains n, the number of queries. Next line contains k. Next n lines contain a query each, say q. If q >= 0 then the number is being 'dictated' to the system and needs to be remembered. If q = -1, then your program should return the k-th minimum number presented so far . It is given that 'q' can be -1 only if at least k numbers have already been given.
-----Output-----
For every q = -1, you have to output the k-th minimum number given as input so far (not counting -1 as an input)
-----Example-----
Input1:
6
2
3
2
-1
-1
1
-1
Output1:
3
3
2
Input2:
10
5
3
6
1
4
2
9
3
1
0
-1
Output2:
3
|
{"inputs": ["6\n2\n3\n2\n-1\n-1\n1\n-1", "10\n5\n3\n6\n1\n4\n2\n9\n3\n1\n0\n-1"], "outputs": ["3\n3\n2", "3"]}
| 460
| 63
|
coding
|
Solve the programming task below in a Python markdown code block.
Chef is playing badminton today. The service rules of this singles game of badminton are as follows:
1. The player who starts the game serves from the right side of their court.
2. Whenever a player wins a point, they serve next.
3. If the server has won an even number of points during a game, then they will serve from the right side of the service court for the subsequent point.
Chef will be the player who begins the game.
Given the number of points P obtained by Chef at the end of the game, please determine how many times Chef served from the right side of the court.
Please see the sample cases below for explained examples.
------ 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 containing one integer P, the points obtained by Chef.
------ Output Format ------
For each test case, output in a single line the number of times Chef served from the right side of the court.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$0 ≤ P ≤ 10^{9}$
----- Sample Input 1 ------
4
2
9
53
746
----- Sample Output 1 ------
2
5
27
374
----- explanation 1 ------
Test case $1$: Chef obtained $2$ points at the end of the game. This means he served two times from the right side of the court, once when his score was $0$ and once again when his score was $2$.
Test case $2$: Chef obtained $9$ points at the end of the game. This means he served $5$ times from the right side of the court. The points when he had to serve from right were: $0,2,4,6,8$.
|
{"inputs": ["4\n2\n9\n53\n746"], "outputs": ["2\n5\n27\n374"]}
| 411
| 32
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problem statement in Mandarin chinese and Vietnamese.
Suzumo is the coach of the ChefLand OI team. Physical condition is very important in any olympiad, so he wants to make the children run a bit as a warmup.
The team consists of $N$ children numbered $1$ through $N$ standing at some positions on the $x$-axis. For each valid $i$, the initial $x$-coordinate of the $i$-th kid is $x_{i}$, the running velocity of the $i$-th kid is constant and equal to $v_{i}$.
Suzumo wants to assign a running direction (left or right, i.e. in the direction of decreasing or increasing $x$-coordinate) to each kid; the children start running at time $0$ in the assigned directions. Afterwards, Suzumo will measure the smallest time $t$ at which some kid passes another one. Help Suzumo compute the maximum time $t$ if he can assign the directions arbitrarily!
Note: Child $i$ *passes* child $j$ at time $t_{ij}$ if their coordinates satisfy $x_{i} < x_{j}$ at any time $t < t_{ij}$ and $x_{i} > x_{j}$ at any time $t > t_{ij}$, or if they satisfy $x_{i} > x_{j}$ at any time $t < t_{ij}$ and $x_{i} < x_{j}$ at any time $t > t_{ij}$.
------ 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$.
$N$ lines follow. For each valid $i$, the $i$-th of these lines contains two space-separated integers $x_{i}$ and $v_{i}$.
------ Output ------
For each test case, print a single line containing one real number — the maximum possible time $t$, or $-1$ if it is possible to assign directions in such a way that no kid ever passes another. Your answer will be considered correct if it has an absolute or relative error less than or equal to $10^{-6}$.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 50$
$1 ≤ x_{i}, v_{i} ≤ 10^{9}$ for each valid $i$
no two children have the same initial positions
----- Sample Input 1 ------
1
3
10 10
20 30
30 10
----- Sample Output 1 ------
0.5
----- explanation 1 ------
Example case 1: One optimal assignment of directions is left, right, right.
|
{"inputs": ["1\n3\n10 10\n20 30\n30 10"], "outputs": ["0.5"]}
| 624
| 34
|
coding
|
Solve the programming task below in a Python markdown code block.
The Little Elephant loves Ukraine very much. Most of all he loves town Rozdol (ukr. "Rozdil").
However, Rozdil is dangerous to settle, so the Little Elephant wants to go to some other town. The Little Elephant doesn't like to spend much time on travelling, so for his journey he will choose a town that needs minimum time to travel to. If there are multiple such cities, then the Little Elephant won't go anywhere.
For each town except for Rozdil you know the time needed to travel to this town. Find the town the Little Elephant will go to or print "Still Rozdil", if he stays in Rozdil.
Input
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of cities. The next line contains n integers, separated by single spaces: the i-th integer represents the time needed to go from town Rozdil to the i-th town. The time values are positive integers, not exceeding 109.
You can consider the cities numbered from 1 to n, inclusive. Rozdil is not among the numbered cities.
Output
Print the answer on a single line — the number of the town the Little Elephant will go to. If there are multiple cities with minimum travel time, print "Still Rozdil" (without the quotes).
Examples
Input
2
7 4
Output
2
Input
7
7 4 47 100 4 9 12
Output
Still Rozdil
Note
In the first sample there are only two cities where the Little Elephant can go. The travel time for the first town equals 7, to the second one — 4. The town which is closest to Rodzil (the only one) is the second one, so the answer is 2.
In the second sample the closest cities are cities two and five, the travelling time to both of them equals 4, so the answer is "Still Rozdil".
|
{"inputs": ["1\n47\n", "1\n76\n", "1\n71\n", "1\n22\n", "1\n31\n", "1\n16\n", "1\n15\n", "1\n127\n"], "outputs": ["1", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n"]}
| 433
| 94
|
coding
|
Solve the programming task below in a Python markdown code block.
There were $N$ students (numbered $1$ through $N$) participating in the Indian Programming Camp (IPC) and they watched a total of $K$ lectures (numbered $1$ through $K$). For each student $i$ and each lecture $j$, the $i$-th student watched the $j$-th lecture for $T_{i, j}$ minutes.
Additionally, for each student $i$, we know that this student asked the question, "What is the criteria for getting a certificate?" $Q_i$ times.
The criteria for getting a certificate is that a student must have watched at least $M$ minutes of lectures in total and they must have asked the question no more than $10$ times.
Find out how many participants are eligible for a certificate.
-----Input-----
- The first line of the input contains three space-separated integers $N$, $M$ and $K$.
- $N$ lines follow. For each valid $i$, the $i$-th of these lines contains $K+1$ space-separated integers $T_{i, 1}, T_{i, 2}, \ldots, T_{i, K}, Q_i$.
-----Output-----
Print a single line containing one integer — the number of participants eligible for a certificate.
-----Constraints-----
- $1 \le N, K \le 1,000$
- $1 \le M \le 10^6$
- $1 \le Q_i \le 10^6$ for each valid $i$
- $1 \le T_{i, j} \le 1,000$ for each valid $i$ and $j$
-----Example Input-----
4 8 4
1 2 1 2 5
3 5 1 3 4
1 2 4 5 11
1 1 1 3 12
-----Example Output-----
1
-----Explanation-----
- Participant $1$ watched $1 + 2 + 1 + 2 = 6$ minutes of lectures and asked the question $5$ times. Since $6 < M$, this participant does not receive a certificate.
- Participant $2$ watched $3 + 5 + 1 + 3 = 12$ minutes of lectures and asked the question $4$ times. Since $12 \ge M$ and $4 \le 10$, this participant receives a certificate.
- Participant $3$ watched $1 + 2 + 4 + 5 = 12$ minutes of lectures and asked the question $11$ times. Since $12 \ge M$ but $11 > 10$, this participant does not receive a certificate.
- Participant $4$ watched $1 + 1 + 1 + 3 = 6$ minutes of lectures and asked the question $12$ times. Since $6 < M$ and $12 > 10$, this participant does not receive a certificate.
Only participant $2$ receives a certificate.
|
{"inputs": ["4 8 4\n1 2 1 2 5\n3 5 1 3 4\n1 2 4 5 11\n1 1 1 3 12"], "outputs": ["1"]}
| 663
| 58
|
coding
|
Solve the programming task below in a Python markdown code block.
The chef is trying to decode some pattern problems, Chef wants your help to code it. Chef has one number K to form a new pattern. Help the chef to code this pattern problem.
-----Input:-----
- First-line will contain $T$, the number of test cases. Then the test cases follow.
- Each test case contains a single line of input, one integer $K$.
-----Output:-----
For each test case, output as the pattern.
-----Constraints-----
- $1 \leq T \leq 100$
- $1 \leq K \leq 100$
-----Sample Input:-----
4
1
2
3
4
-----Sample Output:-----
0
01
10
012
101
210
0123
1012
2101
3210
-----EXPLANATION:-----
No need, else pattern can be decode easily.
|
{"inputs": ["4\n1\n2\n3\n4"], "outputs": ["0\n01\n10\n012\n101\n210\n0123\n1012\n2101\n3210"]}
| 216
| 58
|
coding
|
Solve the programming task below in a Python markdown code block.
For a permutation P of integers from 1 to N, let's define its value as (1 \oplus P_{1}) + (2 \oplus P_{2}) + \ldots + (N \oplus P_{N}).
Given N, find the maximum possible value of the permutation of integers from 1 to N.
As a reminder, \oplus denotes the bitwise XOR operation
------ Input Format ------
The first line of the input contains a single integer T - the number of test cases. The description of test cases follows.
The only line of each test case contains a single integer N.
------ Output Format ------
For each test case, output the maximum possible value of the permutation of integers from 1 to N.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{9}$.
------ subtasks ------
Subtask 1 (60 points): The sum of $N$ over all test cases doesn't exceed $10^{6}$.
Subtask 2 (40 points): No additional constraints.
----- Sample Input 1 ------
5
1
2
3
4
29012022
----- Sample Output 1 ------
0
6
6
20
841697449540506
----- explanation 1 ------
For $N = 1$, the only such permutation is $P = (1)$, its value is $1 \oplus 1 = 0$.
For $N = 2$, the permutation with the best value is $P = (2, 1)$, with value $1 \oplus 2 + 2 \oplus 1 = 6$.
For $N = 3$, the permutation with the best value is $P = (2, 1, 3)$, with value $1 \oplus 2 + 2 \oplus 1 + 3 \oplus 3 = 6$.
For $N = 4$, the permutation with the best value is $P = (2, 1, 4, 3)$, with value $1 \oplus 2 + 2 \oplus 1 + 3 \oplus 4 + 4 \oplus 3 = 20$.
|
{"inputs": ["5\n1\n2\n3\n4\n29012022"], "outputs": ["0\n6\n6\n20\n841697449540506"]}
| 510
| 52
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree.
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 buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
```
|
{"functional": "def check(candidate):\n assert is_same_tree(candidate(preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]), tree_node([3,9,20,None,None,15,7]))\n assert is_same_tree(candidate(preorder = [-1], inorder = [-1]), tree_node([-1]))\n\n\ncheck(Solution().buildTree)"}
| 141
| 99
|
coding
|
Solve the programming task below in a Python markdown code block.
-----Input-----
The input contains a single integer $a$ ($1 \le a \le 99$).
-----Output-----
Output "YES" or "NO".
-----Examples-----
Input
5
Output
YES
Input
13
Output
NO
Input
24
Output
NO
Input
46
Output
YES
|
{"inputs": ["5\n", "1\n", "2\n", "3\n", "4\n", "4\n", "2\n", "3\n"], "outputs": ["YES\n", "NO\n", "YES\n", "YES\n", "YES\n", "YES", "YES", "YES"]}
| 86
| 67
|
coding
|
Solve the programming task below in a Python markdown code block.
Chef has just found a recipe book, where every dish consists of exactly four ingredients.
He is going to choose some two dishes and prepare them for dinner.
Of course, he likes diversity and wants to know whether the two dishes are similar.
Two dishes are called similar if at least half of their ingredients are the same.
In other words, at least two of four ingredients of the first dish should also be present in the second dish.
The order of ingredients doesn't matter.
Your task is to examine T pairs of dishes.
For each pair, check if the two dishes are similar and print "similar" or "dissimilar" accordingly.
-----Input-----
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains four distinct strings, denoting ingredients needed for the first dish.
Each ingredient is represented by a string of length between 2 and 10 inclusive, consisting of lowercase English letters.
The second line of each test case describes the second dish in the same format.
-----Output-----
For each test case, output a single line containing the answer — "similar" if at least half of the ingredients are same, and "dissimilar" otherwise (without the quotes).
-----Constraints-----
- 1 ≤ T ≤ 200
- The length of each string will be between 2 and 10 inclusive.
-----Example-----
Input:
5
eggs sugar flour salt
sugar eggs milk flour
aa ab ac ad
ac ad ae af
cookies sugar grass lemon
lemon meat chili wood
one two three four
one two three four
gibberish jibberish lalalalala popopopopo
jibberisz gibberisz popopopopu lalalalalu
Output:
similar
similar
dissimilar
similar
dissimilar
-----Explanation-----
Test case 1. The first dish consists of ingredients: eggs, sugar, flour, salt, while the second dish consists of: sugar, eggs, milk, flour. Three of four ingredients are present in both dishes (eggs, sugar, flour) so the two dishes are similar.
Test case 2. This example shows that strings in the input don't necessarily represent real food.
The answer is again "similar", because two ingredients are present in both dishes ("ac" and "ad").
Test case 3. The only common ingredient is lemon, so the answer is "dissimilar".
Remember that at least two ingredients should be present in both dishes.
|
{"inputs": ["5\neggs sugar flour salt\nsugar eggs milk flour\naa ab ac ad\nac ad ae af\ncookies sugar grass lemon\nlemon meat chili wood\none two three four\none two three four\ngibberish jibberish lalalalala popopopopo\njibberisz gibberisz popopopopu lalalalalu"], "outputs": ["similar\nsimilar\ndissimilar\nsimilar\ndissimilar"]}
| 544
| 103
|
coding
|
Solve the programming task below in a Python markdown code block.
Tokitsukaze has a sequence $a$ of length $n$. For each operation, she selects two numbers $a_i$ and $a_j$ ($i \ne j$; $1 \leq i,j \leq n$).
If $a_i = a_j$, change one of them to $0$.
Otherwise change both of them to $\min(a_i, a_j)$.
Tokitsukaze wants to know the minimum number of operations to change all numbers in the sequence to $0$. It can be proved that the answer always exists.
-----Input-----
The first line contains a single positive integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
For each test case, the first line contains a single integer $n$ ($2 \leq n \leq 100$) — the length of the sequence $a$.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i \leq 100$) — the sequence $a$.
-----Output-----
For each test case, print a single integer — the minimum number of operations to change all numbers in the sequence to $0$.
-----Examples-----
Input
3
3
1 2 3
3
1 2 2
3
1 2 0
Output
4
3
2
-----Note-----
In the first test case, one of the possible ways to change all numbers in the sequence to $0$:
In the $1$-st operation, $a_1 < a_2$, after the operation, $a_2 = a_1 = 1$. Now the sequence $a$ is $[1,1,3]$.
In the $2$-nd operation, $a_1 = a_2 = 1$, after the operation, $a_1 = 0$. Now the sequence $a$ is $[0,1,3]$.
In the $3$-rd operation, $a_1 < a_2$, after the operation, $a_2 = 0$. Now the sequence $a$ is $[0,0,3]$.
In the $4$-th operation, $a_2 < a_3$, after the operation, $a_3 = 0$. Now the sequence $a$ is $[0,0,0]$.
So the minimum number of operations is $4$.
|
{"inputs": ["3\n3\n1 2 3\n3\n1 2 2\n3\n1 2 0\n"], "outputs": ["4\n3\n2\n"]}
| 550
| 42
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a string A. Find a string B, where B is a palindrome and A is a subsequence of B.
A subsequence of a string is a string that can be derived from it by deleting some (not necessarily consecutive) characters without changing the order of the remaining characters. For example, "cotst" is a subsequence of "contest".
A palindrome is a string that reads the same forward or backward.
The length of string B should be at most 10^4. It is guaranteed that there always exists such string.
You do not need to find the shortest answer, the only restriction is that the length of string B should not exceed 10^4.
-----Input-----
First line contains a string A (1 ≤ |A| ≤ 10^3) consisting of lowercase Latin letters, where |A| is a length of A.
-----Output-----
Output single line containing B consisting of only lowercase Latin letters. You do not need to find the shortest answer, the only restriction is that the length of string B should not exceed 10^4. If there are many possible B, print any of them.
-----Examples-----
Input
aba
Output
aba
Input
ab
Output
aabaa
-----Note-----
In the first example, "aba" is a subsequence of "aba" which is a palindrome.
In the second example, "ab" is a subsequence of "aabaa" which is a palindrome.
|
{"inputs": ["ab\n", "ab\n", "ba\n", "ac\n", "ab\n", "aba\n", "aba\n", "abb\n"], "outputs": ["abba", "abba\n", "baab\n", "acca\n", "abba\n", "abaaba", "abaaba\n", "abbbba\n"]}
| 314
| 77
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given a 0-indexed integer array stones sorted in strictly increasing order representing the positions of stones in a river.
A frog, initially on the first stone, wants to travel to the last stone and then return to the first stone. However, it can jump to any stone at most once.
The length of a jump is the absolute difference between the position of the stone the frog is currently on and the position of the stone to which the frog jumps.
More formally, if the frog is at stones[i] and is jumping to stones[j], the length of the jump is |stones[i] - stones[j]|.
The cost of a path is the maximum length of a jump among all jumps in the path.
Return the minimum cost of a path for the frog.
Please complete the following python code precisely:
```python
class Solution:
def maxJump(self, stones: List[int]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(stones = [0,2,5,6,7]) == 5\n assert candidate(stones = [0,3,9]) == 9\n\n\ncheck(Solution().maxJump)"}
| 204
| 57
|
coding
|
Solve the programming task below in a Python markdown code block.
The Resistance is trying to take control over as many planets of a particular solar system as possible. Princess Heidi is in charge of the fleet, and she must send ships to some planets in order to maximize the number of controlled planets.
The Galaxy contains N planets, connected by bidirectional hyperspace tunnels in such a way that there is a unique path between every pair of the planets.
A planet is controlled by the Resistance if there is a Resistance ship in its orbit, or if the planet lies on the shortest path between some two planets that have Resistance ships in their orbits.
Heidi has not yet made up her mind as to how many ships to use. Therefore, she is asking you to compute, for every K = 1, 2, 3, ..., N, the maximum number of planets that can be controlled with a fleet consisting of K ships.
-----Input-----
The first line of the input contains an integer N (1 ≤ N ≤ 10^5) – the number of planets in the galaxy.
The next N - 1 lines describe the hyperspace tunnels between the planets. Each of the N - 1 lines contains two space-separated integers u and v (1 ≤ u, v ≤ N) indicating that there is a bidirectional hyperspace tunnel between the planets u and v. It is guaranteed that every two planets are connected by a path of tunnels, and that each tunnel connects a different pair of planets.
-----Output-----
On a single line, print N space-separated integers. The K-th number should correspond to the maximum number of planets that can be controlled by the Resistance using a fleet of K ships.
-----Examples-----
Input
3
1 2
2 3
Output
1 3 3
Input
4
1 2
3 2
4 2
Output
1 3 4 4
-----Note-----
Consider the first example. If K = 1, then Heidi can only send one ship to some planet and control it. However, for K ≥ 2, sending ships to planets 1 and 3 will allow the Resistance to control all planets.
|
{"inputs": ["3\n1 2\n2 3\n", "3\n1 2\n2 3\n", "4\n1 2\n3 2\n4 2\n", "4\n1 2\n3 2\n4 1\n", "4\n1 3\n3 2\n4 2\n", "4\n1 3\n3 2\n4 1\n", "4\n1 2\n3 2\n4 2\n", "19\n2 19\n7 15\n8 10\n16 1\n12 5\n11 5\n6 18\n12 14\n14 15\n2 6\n9 14\n4 17\n16 10\n4 2\n7 18\n3 2\n9 13\n11 10\n"], "outputs": ["1 3 3 ", "1 3 3 ", "1 3 4 4 ", "1 4 4 4\n", "1 4 4 4\n", "1 4 4 4\n", "1 3 4 4 ", "1 14 16 17 18 19 19 19 19 19 19 19 19 19 19 19 19 19 19 "]}
| 446
| 326
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given an integer array nums and an integer k, return the number of subarrays of nums where the greatest common divisor of the subarray's elements is k.
A subarray is a contiguous non-empty sequence of elements within an array.
The greatest common divisor of an array is the largest integer that evenly divides all the array elements.
Please complete the following python code precisely:
```python
class Solution:
def subarrayGCD(self, nums: List[int], k: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(nums = [9,3,1,2,6,3], k = 3) == 4\n assert candidate(nums = [4], k = 7) == 0\n\n\ncheck(Solution().subarrayGCD)"}
| 120
| 65
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problem statements in [Mandarin], [Bengali], [Russian], and [Vietnamese] as well.
Chef and Divyam are playing a game. They start with a rational number $X$, such that $0 < X < 1$.
In each turn, Chef chooses a positive real number $Y$. Then Divyam either adds or subtracts $Y$ to $X$. So $X$ is replaced with either $X+Y$ or $X-Y$. If this number becomes $0$ or $1$, then Chef wins. Otherwise, the game continues.
You are given $X$, and you have to determine whether Chef can win in a finite number of moves. More formally, does there exist a finite number $N$ so that Chef has a strategy that is guaranteed to win in at most $N$ moves?
Here, $X$ will be given in the form $\frac{A}{B}$, where $A$ and $B$ are positive integers, $A < B$ and $\gcd(A,B)=1$.
------ 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 $A$ and $B$.
------ Output ------
For each test case, print a single line containing the string "Yes" if Chef can win the game in a finite number of moves and "No" otherwise (without quotes). You can print "Yes" or "No" in any case.
------ Constraints ------
$1 ≤ T ≤ 10^{6}$
$1 ≤ A < B ≤ 10^{18}$
$\gcd(A, B)=1$
------ Subtasks ------
Subtask #1 (100 points): Original constraints
----- Sample Input 1 ------
2
1 2
2 3
----- Sample Output 1 ------
Yes
No
----- explanation 1 ------
Test case 1: Chef will choose $Y=\frac{1}{2}$, so whatever Divyam does, the number will become $0$ or $1$. Hence Chef will win in a finite number of moves.
Test case 2: No matter what Chef chooses, it can be shown that Divyam can make sure Chef never wins.
|
{"inputs": ["2\n1 2\n2 3"], "outputs": ["Yes\nNo"]}
| 514
| 22
|
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.
There are $N$ students standing in a row and numbered $1$ through $N$ from left to right. You are given a string $S$ with length $N$, where for each valid $i$, the $i$-th character of $S$ is 'x' if the $i$-th student is a girl or 'y' if this student is a boy. Students standing next to each other in the row are friends.
The students are asked to form pairs for a dance competition. Each pair must consist of a boy and a girl. Two students can only form a pair if they are friends. Each student can only be part of at most one pair. What is the maximum number of pairs that can be formed?
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains a single string $S$.
------ Output ------
For each test case, print a single line containing one integer ― the maximum number of pairs.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 10^{5}$
$|S| = N$
$S$ contains only characters 'x' and 'y'
the sum of $N$ over all test cases does not exceed $3 \cdot 10^{5}$
------ Subtasks ------
Subtask #1 (100 points): original constraints
----- Sample Input 1 ------
3
xy
xyxxy
yy
----- Sample Output 1 ------
1
2
0
----- explanation 1 ------
Example case 1: There is only one possible pair: (first student, second student).
Example case 2: One of the ways to form two pairs is: (first student, second student) and (fourth student, fifth student).
Another way to form two pairs is: (second student, third student) and (fourth student, fifth student).
|
{"inputs": ["3\nxy\nxyxxy\nyy"], "outputs": ["1\n2\n0"]}
| 473
| 24
|
coding
|
Solve the programming task below in a Python markdown code block.
Valera wanted to prepare a Codesecrof round. He's already got one problem and he wants to set a time limit (TL) on it.
Valera has written n correct solutions. For each correct solution, he knows its running time (in seconds). Valera has also wrote m wrong solutions and for each wrong solution he knows its running time (in seconds).
Let's suppose that Valera will set v seconds TL in the problem. Then we can say that a solution passes the system testing if its running time is at most v seconds. We can also say that a solution passes the system testing with some "extra" time if for its running time, a seconds, an inequality 2a ≤ v holds.
As a result, Valera decided to set v seconds TL, that the following conditions are met: v is a positive integer; all correct solutions pass the system testing; at least one correct solution passes the system testing with some "extra" time; all wrong solutions do not pass the system testing; value v is minimum among all TLs, for which points 1, 2, 3, 4 hold.
Help Valera and find the most suitable TL or else state that such TL doesn't exist.
-----Input-----
The first line contains two integers n, m (1 ≤ n, m ≤ 100). The second line contains n space-separated positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 100) — the running time of each of the n correct solutions in seconds. The third line contains m space-separated positive integers b_1, b_2, ..., b_{m} (1 ≤ b_{i} ≤ 100) — the running time of each of m wrong solutions in seconds.
-----Output-----
If there is a valid TL value, print it. Otherwise, print -1.
-----Examples-----
Input
3 6
4 5 2
8 9 6 10 7 11
Output
5
Input
3 1
3 4 5
6
Output
-1
|
{"inputs": ["1 1\n4\n9\n", "1 1\n2\n4\n", "1 1\n4\n9\n", "1 1\n2\n4\n", "1 1\n8\n9\n", "1 1\n1\n9\n", "1 1\n1\n0\n", "1 1\n0\n0\n"], "outputs": ["8", "-1\n", "8\n", "-1\n", "-1\n", "2\n", "-1\n", "-1\n"]}
| 463
| 117
|
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 of the same length. A pair of indices (i,j) is called beautiful if|nums1[i] - nums1[j]| + |nums2[i] - nums2[j]| is the smallest amongst all possible indices pairs where i < j.
Return the beautiful pair. In the case that there are multiple beautiful pairs, return the lexicographically smallest pair.
Note that
|x| denotes the absolute value of x.
A pair of indices (i1, j1) is lexicographically smaller than (i2, j2) if i1 < i2 or i1 == i2 and j1 < j2.
Please complete the following python code precisely:
```python
class Solution:
def beautifulPair(self, nums1: List[int], nums2: List[int]) -> List[int]:
```
|
{"functional": "def check(candidate):\n assert candidate(nums1 = [1,2,3,2,4], nums2 = [2,3,1,2,3]) == [0,3]\n assert candidate(nums1 = [1,2,4,3,2,5], nums2 = [1,4,2,3,5,1]) == [1,4]\n\n\ncheck(Solution().beautifulPair)"}
| 200
| 99
|
coding
|
Solve the programming task below in a Python markdown code block.
There are $n$ points $1,2,\ldots,n$, each point $i$ has a number $a_i$ on it. You're playing a game on them. Initially, you are at point $1$. When you are at point $i$, take following steps:
If $1\le i\le n$, go to $i+a_i$,
Otherwise, the game ends.
Before the game begins, you can choose two integers $x$ and $y$ satisfying $1\le x\le n$, $-n \le y \le n$ and replace $a_x$ with $y$ (set $a_x := y$). Find the number of distinct pairs $(x,y)$ such that the game that you start after making the change ends in a finite number of steps.
Notice that you do not have to satisfy $a_x\not=y$.
-----Input-----
Each test contains multiple test cases. The first line contains an integer $t$ ($1\le t\le 10^4)$ — the number of test cases.
The first line of each test case contains one integer $n$ ($1\le n\le 2\cdot 10^5$) — the number of points.
The second line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($-n \le a_i \le n$) — the numbers on the axis.
It's guaranteed that the sum of $n$ does not exceed $2\cdot 10^5$.
-----Output-----
For each test case, print a line containing a single integer — the number of distinct pairs $(x,y)$ with which the game ends.
-----Examples-----
Input
9
1
0
2
-1 0
2
1 -1
2
1 1
3
-1 -2 -1
3
1 -2 -1
4
-1 4 -2 1
5
1 1 1 1 -4
5
1 1 1 1 1
Output
2
8
6
7
20
17
34
30
40
-----Note-----
In the first test case, the pairs $(x,y)$ with which the game ends are $(1,-1)$ and $(1,1)$, corresponding to the routes $1\rightarrow 0$ and $1\rightarrow 2$. Note that $(1,2)$ is invalid since when $n=1$, $y=2$ violates $-n\le y\le n$. $(1,0)$ is also invalid since you will go from $1$ to $1$ forever.
In the second test case, the pairs are $(1,-2),(1,-1),(1,2),(2,-2),(2,-1),(2,0),(2,1),(2,2)$.
In the fourth test case, the pairs are $(1,-2),(1,-1),(1,1),(1,2),(2,-2),(2,1),(2,2)$.
|
{"inputs": ["9\n1\n0\n2\n-1 0\n2\n1 -1\n2\n1 1\n3\n-1 -2 -1\n3\n1 -2 -1\n4\n-1 4 -2 1\n5\n1 1 1 1 -4\n5\n1 1 1 1 1\n"], "outputs": ["2\n8\n6\n7\n20\n17\n34\n30\n40\n"]}
| 663
| 110
|
coding
|
Solve the programming task below in a Python markdown code block.
Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform n operations. Each operation is one of the following: Add the integer x_{i} to the first a_{i} elements of the sequence. Append an integer k_{i} to the end of the sequence. (And hence the size of the sequence increases by 1) Remove the last element of the sequence. So, the size of the sequence decreases by one. Note, that this operation can only be done if there are at least two elements in the sequence.
After each operation, the cows would like to know the average of all the numbers in the sequence. Help them!
-----Input-----
The first line contains a single integer n (1 ≤ n ≤ 2·10^5) — the number of operations. The next n lines describe the operations. Each line will start with an integer t_{i} (1 ≤ t_{i} ≤ 3), denoting the type of the operation (see above). If t_{i} = 1, it will be followed by two integers a_{i}, x_{i} (|x_{i}| ≤ 10^3; 1 ≤ a_{i}). If t_{i} = 2, it will be followed by a single integer k_{i} (|k_{i}| ≤ 10^3). If t_{i} = 3, it will not be followed by anything.
It is guaranteed that all operations are correct (don't touch nonexistent elements) and that there will always be at least one element in the sequence.
-----Output-----
Output n lines each containing the average of the numbers in the sequence after the corresponding operation.
The answer will be considered correct if its absolute or relative error doesn't exceed 10^{ - 6}.
-----Examples-----
Input
5
2 1
3
2 3
2 1
3
Output
0.500000
0.000000
1.500000
1.333333
1.500000
Input
6
2 1
1 2 20
2 2
1 2 -3
3
3
Output
0.500000
20.500000
14.333333
12.333333
17.500000
17.000000
-----Note-----
In the second sample, the sequence becomes $\{0 \} \rightarrow \{0,1 \} \rightarrow \{20,21 \} \rightarrow \{20,21,2 \} \rightarrow \{17,18,2 \} \rightarrow \{17,18 \} \rightarrow \{17 \}$
|
{"inputs": ["1\n2 1\n", "1\n2 0\n", "1\n2 1\n", "1\n2 0\n", "1\n2 -1\n", "1\n1 1 1\n", "1\n1 1 0\n", "1\n1 1 0\n"], "outputs": ["0.500000\n", "0.000000\n", "0.500000\n", "0.000000\n", "-0.500000\n", "1.000000\n", "0.000000\n", "0.000000\n"]}
| 649
| 164
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given the root of a binary tree, calculate the vertical order traversal of the binary tree.
For each node at position (row, col), its left and right children will be at positions (row + 1, col - 1) and (row + 1, col + 1) respectively. The root of the tree is at (0, 0).
The vertical order traversal of a binary tree is a list of top-to-bottom orderings for each column index starting from the leftmost column and ending on the rightmost column. There may be multiple nodes in the same row and same column. In such a case, sort these nodes by their values.
Return the vertical order traversal of the binary tree.
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 verticalTraversal(self, root: Optional[TreeNode]) -> List[List[int]]:
```
|
{"functional": "def check(candidate):\n assert candidate(root = tree_node([3,9,20,None,None,15,7])) == [[9],[3,15],[20],[7]]\n assert candidate(root = tree_node([1,2,3,4,5,6,7])) == [[4],[2],[1,5,6],[3],[7]]\n assert candidate(root = tree_node([1,2,3,4,6,5,7])) == [[4],[2],[1,5,6],[3],[7]]\n\n\ncheck(Solution().verticalTraversal)"}
| 247
| 137
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a string s consisting of lowercase English letters and a number k. Let's call a string consisting of lowercase English letters beautiful if the number of occurrences of each letter in that string is divisible by k. You are asked to find the lexicographically smallest beautiful string of length n, which is lexicographically greater or equal to string s. If such a string does not exist, output -1.
A string a is lexicographically smaller than a string b if and only if one of the following holds:
* a is a prefix of b, but a ≠ b;
* in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b.
Input
The first line contains a single integer T (1 ≤ T ≤ 10 000) — the number of test cases.
The next 2 ⋅ T lines contain the description of test cases. The description of each test case consists of two lines.
The first line of the description contains two integers n and k (1 ≤ k ≤ n ≤ 10^5) — the length of string s and number k respectively.
The second line contains string s consisting of lowercase English letters.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case output in a separate line lexicographically smallest beautiful string of length n, which is greater or equal to string s, or -1 if such a string does not exist.
Example
Input
4
4 2
abcd
3 1
abc
4 3
aaaa
9 3
abaabaaaa
Output
acac
abc
-1
abaabaaab
Note
In the first test case "acac" is greater than or equal to s, and each letter appears 2 or 0 times in it, so it is beautiful.
In the second test case each letter appears 0 or 1 times in s, so s itself is the answer.
We can show that there is no suitable string in the third test case.
In the fourth test case each letter appears 0, 3, or 6 times in "abaabaaab". All these integers are divisible by 3.
|
{"inputs": ["4\n4 2\ndcba\n3 1\nabc\n4 3\naaaa\n9 3\nabaabaaaa\n", "4\n4 4\nabcd\n3 1\nabc\n4 3\naaaa\n9 3\nabaabaaaa\n", "4\n4 2\nabcd\n3 1\nabc\n4 1\naaaa\n9 3\nabaabaaaa\n", "4\n4 2\ndcba\n3 1\nabc\n4 3\naaaa\n9 3\nabbabaaaa\n", "4\n4 4\nabcd\n3 1\nabc\n4 1\naaaa\n9 3\nabaabaaaa\n", "4\n4 2\ndcba\n3 1\nabc\n4 3\naaaa\n9 1\nabaabaaaa\n", "4\n4 4\nabcd\n3 1\nabd\n4 3\naaaa\n9 3\nabaabaaaa\n", "4\n4 1\ndcba\n3 1\nabc\n4 3\naaaa\n9 3\nabbabaaaa\n"], "outputs": ["dccd\nabc\n-1\nabaabaaab\n", "bbbb\nabc\n-1\nabaabaaab\n", "acac\nabc\naaaa\nabaabaaab\n", "dccd\nabc\n-1\nabbabaaaa\n", "bbbb\nabc\naaaa\nabaabaaab\n", "dccd\nabc\n-1\nabaabaaaa\n", "bbbb\nabd\n-1\nabaabaaab\n", "dcba\nabc\n-1\nabbabaaaa\n"]}
| 490
| 362
|
coding
|
Solve the programming task below in a Python markdown code block.
The MarkiT online virtual market startup wants to organize its grand opening in NIT Patna.
but they want maximum crowd for their inauguration. So the manager told this to Praveen a student in NITP who suggested them:
The first-year students come to campus every x hour,
Second-year students come to campus every y hour,
Third-year students come to campus every z hour and
Fourth-year is very busy so they don't come regularly.
So Praveen being very clever told him the no of times in n days he can have an audience of all year student (1st,2nd & 3rd) at max. So can you code what Praveen has done?
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- Each testcase contains of a 2 line of input, first line contain one integers $N$ (No of Days).
-Next line contain 3 space separated integer the value of x y z
-----Output:-----
For each testcase, output in a single line answer the no of times audience consists of all year.
-----Constraints-----
- $1 \leq T \leq 1000$
- $1 \leq N \leq 10^8$
- $1 \leq x,y,z \leq 10^5$
-----Sample Input:-----
1
10
8 10 6
-----Sample Output:-----
2
-----EXPLANATION:-----
First favourable condition will come on 5th day and Second on 10th day.
|
{"inputs": ["1\n10\n8 10 6"], "outputs": ["2"]}
| 351
| 22
|
coding
|
Solve the programming task below in a Python markdown code block.
Madeline has an array $a$ of $n$ integers. A pair $(u, v)$ of integers forms an inversion in $a$ if:
$1 \le u < v \le n$. $a_u > a_v$.
Madeline recently found a magical paper, which allows her to write two indices $u$ and $v$ and swap the values $a_u$ and $a_v$. Being bored, she decided to write a list of pairs $(u_i, v_i)$ with the following conditions:
all the pairs in the list are distinct and form an inversion in $a$. all the pairs that form an inversion in $a$ are in the list. Starting from the given array, if you swap the values at indices $u_1$ and $v_1$, then the values at indices $u_2$ and $v_2$ and so on, then after all pairs are processed, the array $a$ will be sorted in non-decreasing order.
Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them.
-----Input-----
The first line of the input contains a single integer $n$ ($1 \le n \le 1000$) — the length of the array.
Next line contains $n$ integers $a_1,a_2,...,a_n$ $(1 \le a_i \le 10^9)$ — elements of the array.
-----Output-----
Print -1 if no such list exists. Otherwise in the first line you should print a single integer $m$ ($0 \le m \le \dfrac{n(n-1)}{2}$) — number of pairs in the list.
The $i$-th of the following $m$ lines should contain two integers $u_i, v_i$ ($1 \le u_i < v_i\le n$).
If there are multiple possible answers, you may find any of them.
-----Examples-----
Input
3
3 1 2
Output
2
1 3
1 2
Input
4
1 8 1 6
Output
2
2 4
2 3
Input
5
1 1 1 2 2
Output
0
-----Note-----
In the first sample test case the array will change in this order $[3,1,2] \rightarrow [2,1,3] \rightarrow [1,2,3]$.
In the second sample test case it will be $[1,8,1,6] \rightarrow [1,6,1,8] \rightarrow [1,1,6,8]$.
In the third sample test case the array is already sorted.
|
{"inputs": ["1\n1\n", "1\n1\n", "1\n0\n", "1\n2\n", "1\n-1\n", "1\n-2\n", "1\n-3\n", "1\n-4\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
| 598
| 90
|
coding
|
Solve the programming task below in a Python markdown code block.
Madoka's father just reached $1$ million subscribers on Mathub! So the website decided to send him a personalized award — The Mathhub's Bit Button!
The Bit Button is a rectangular table with $n$ rows and $m$ columns with $0$ or $1$ in each cell. After exploring the table Madoka found out that:
A subrectangle $A$ is contained in a subrectangle $B$ if there's no cell contained in $A$ but not contained in $B$.
Two subrectangles intersect if there is a cell contained in both of them.
A subrectangle is called black if there's no cell with value $0$ inside it.
A subrectangle is called nice if it's black and it's not contained in another black subrectangle.
The table is called elegant if there are no two nice intersecting subrectangles.
For example, in the first illustration the red subrectangle is nice, but in the second one it's not, because it's contained in the purple subrectangle.
Help Madoka to determine whether the table is elegant.
-----Input-----
Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 200$) — the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n, m$ ($1 \le n, m \le 100$).
The next $n$ lines contain strings of length $m$ consisting of zeros and ones — the description of the table.
It is guaranteed that the sum of the values of $n$ and the sum of the values of $m$ for all test cases do not exceed $777$.
-----Output-----
For each test case print "YES" if its table is elegant or print "NO" otherwise.
You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as positive answer).
-----Examples-----
Input
5
3 3
100
011
011
3 3
110
111
110
1 5
01111
4 5
11111
01010
01000
01000
3 2
11
00
11
Output
YES
NO
YES
NO
YES
-----Note-----
In the second test case the table is not elegant, because the red and the purple subrectangles are nice and intersect.
In the fourth test case the table is not elegant, because the red and the purple subrectangles are nice and intersect.
|
{"inputs": ["5\n3 3\n100\n011\n011\n3 3\n110\n111\n110\n1 5\n01111\n4 5\n11111\n01010\n01000\n01000\n3 2\n11\n00\n11\n"], "outputs": ["YES\nNO\nYES\nNO\nYES\n"]}
| 585
| 105
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given two very long integers a, b (leading zeroes are allowed). You should check what number a or b is greater or determine that they are equal.
The input size is very large so don't use the reading of symbols one by one. Instead of that use the reading of a whole line or token.
As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. Don't use the function input() in Python2 instead of it use the function raw_input().
-----Input-----
The first line contains a non-negative integer a.
The second line contains a non-negative integer b.
The numbers a, b may contain leading zeroes. Each of them contains no more than 10^6 digits.
-----Output-----
Print the symbol "<" if a < b and the symbol ">" if a > b. If the numbers are equal print the symbol "=".
-----Examples-----
Input
9
10
Output
<
Input
11
10
Output
>
Input
00012345
12345
Output
=
Input
0123
9
Output
>
Input
0123
111
Output
>
|
{"inputs": ["9\n9\n", "1\n2\n", "9\n0\n", "0\n1\n", "0\n0\n", "1\n0\n", "0\n0\n", "9\n9\n"], "outputs": ["=\n", "<\n", ">\n", "<\n", "=\n", ">\n", "=\n", "=\n"]}
| 295
| 86
|
coding
|
Solve the programming task below in a Python markdown code block.
A permutation of size n is an array of size n such that each integer from 1 to n occurs exactly once in this array. An inversion in a permutation p is a pair of indices (i, j) such that i > j and a_{i} < a_{j}. For example, a permutation [4, 1, 3, 2] contains 4 inversions: (2, 1), (3, 1), (4, 1), (4, 3).
You are given a permutation a of size n and m queries to it. Each query is represented by two indices l and r denoting that you have to reverse the segment [l, r] of the permutation. For example, if a = [1, 2, 3, 4] and a query l = 2, r = 4 is applied, then the resulting permutation is [1, 4, 3, 2].
After each query you have to determine whether the number of inversions is odd or even.
-----Input-----
The first line contains one integer n (1 ≤ n ≤ 1500) — the size of the permutation.
The second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ n) — the elements of the permutation. These integers are pairwise distinct.
The third line contains one integer m (1 ≤ m ≤ 2·10^5) — the number of queries to process.
Then m lines follow, i-th line containing two integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) denoting that i-th query is to reverse a segment [l_{i}, r_{i}] of the permutation. All queries are performed one after another.
-----Output-----
Print m lines. i-th of them must be equal to odd if the number of inversions in the permutation after i-th query is odd, and even otherwise.
-----Examples-----
Input
3
1 2 3
2
1 2
2 3
Output
odd
even
Input
4
1 2 4 3
4
1 1
1 4
1 4
2 3
Output
odd
odd
odd
even
-----Note-----
The first example:
after the first query a = [2, 1, 3], inversion: (2, 1); after the second query a = [2, 3, 1], inversions: (3, 1), (3, 2).
The second example:
a = [1, 2, 4, 3], inversion: (4, 3); a = [3, 4, 2, 1], inversions: (3, 1), (4, 1), (3, 2), (4, 2), (4, 3); a = [1, 2, 4, 3], inversion: (4, 3); a = [1, 4, 2, 3], inversions: (3, 2), (4, 2).
|
{"inputs": ["3\n1 2 3\n2\n1 2\n2 3\n", "3\n1 2 3\n2\n1 2\n2 6\n", "3\n1 2 3\n2\n1 2\n1 3\n", "3\n1 2 3\n2\n1 2\n2 5\n", "3\n1 2 3\n2\n1 3\n2 6\n", "3\n1 2 3\n2\n1 2\n2 3\n", "3\n2 1 3\n3\n2 3\n1 1\n1 3\n", "3\n2 1 3\n3\n2 3\n1 1\n1 3\n"], "outputs": ["odd\neven\n", "odd\nodd\n", "odd\neven\n", "odd\nodd\n", "odd\nodd\n", "odd\neven\n", "even\neven\nodd\n", "even\neven\nodd\n"]}
| 690
| 231
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a string of characters, or numbers. Find the minimum number of characters to be inserted into the string in order to obtain a palindrome.
A palindrome is a word, phrase, number, or other sequence of symbols or elements that reads the same forward or reversed.
For example, the string abcbd can be transformed into a palindrome ("dabcbad" or "adbcbda"). However, inserting fewer than 2 characters will not produce a palindrome.
Input Format:
First line contains test cases and second line contains an integer 'n' specifying the length of the string, where 3 ≤ n ≤ 20
Second line contains a string of length n.
Note:
Upper-case and lower-case characters are considered as different. Elements of the string are either English alphabets or numerals.
Output Format:
One line containing the minimum number of insertions required to make the string a palindrome
SAMPLE INPUT
2
5
nitin
7
aabbaab
SAMPLE OUTPUT
0
1
|
{"inputs": ["1\n20\neeeeeeeeeeeeeeeeeeee", "1\n20\n0ziG30WBD79ow1E0fu1X", "1\n20\nWbHRKhSxoS18CnGxola3"], "outputs": ["0", "17", "17"]}
| 222
| 75
|
coding
|
Solve the programming task below in a Python markdown code block.
Modify the spacify function so that it returns the given string with spaces inserted between each character.
```python
spacify("hello world") # returns "h e l l o w o r l d"
```
Also feel free to reuse/extend the following starter code:
```python
def spacify(string):
```
|
{"functional": "_inputs = [[''], ['a'], ['Pippi']]\n_outputs = [[''], ['a'], ['P i p p i']]\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(spacify(*i), o[0])"}
| 80
| 170
|
coding
|
Solve the programming task below in a Python markdown code block.
For a given array $a$ consisting of $n$ integers and a given integer $m$ find if it is possible to reorder elements of the array $a$ in such a way that $\sum_{i=1}^{n}{\sum_{j=i}^{n}{\frac{a_j}{j}}}$ equals $m$? It is forbidden to delete elements as well as insert new elements. Please note that no rounding occurs during division, for example, $\frac{5}{2}=2.5$.
-----Input-----
The first line contains a single integer $t$ — the number of test cases ($1 \le t \le 100$). The test cases follow, each in two lines.
The first line of a test case contains two integers $n$ and $m$ ($1 \le n \le 100$, $0 \le m \le 10^6$). The second line contains integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^6$) — the elements of the array.
-----Output-----
For each test case print "YES", if it is possible to reorder the elements of the array in such a way that the given formula gives the given value, and "NO" otherwise.
-----Example-----
Input
2
3 8
2 5 1
4 4
0 1 2 3
Output
YES
NO
-----Note-----
In the first test case one of the reorders could be $[1, 2, 5]$. The sum is equal to $(\frac{1}{1} + \frac{2}{2} + \frac{5}{3}) + (\frac{2}{2} + \frac{5}{3}) + (\frac{5}{3}) = 8$. The brackets denote the inner sum $\sum_{j=i}^{n}{\frac{a_j}{j}}$, while the summation of brackets corresponds to the sum over $i$.
|
{"inputs": ["1\n1 1\n2\n", "1\n1 0\n1\n", "1\n1 1\n0\n", "1\n1 2\n4\n", "1\n1 0\n2\n", "1\n1 0\n5\n", "1\n1 2\n4\n", "1\n1 0\n5\n"], "outputs": ["NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n"]}
| 449
| 118
|
coding
|
Solve the programming task below in a Python markdown code block.
Ram has invented a magic sequence. Each element of the sequence is defined by the same recursive definition - take some linear combination of previous elements (whose coefficients are fixed) and add to them the n-th powers of some integers. Formally: Xn = Xn-1*a1 + ... + Xn-i*ai + b1*d1^n + ... + bj*dj^n, for some integer constants p,q,a1,...,ap,b1,..., bq,d1,..., dq. Of course, as the values can quickly grow, he computed them modulo a fixed value: 10^6. He wrote many consecutive values of the sequence, but then he lost most of his work. All he has now, is 10 consecutive values taken from somewhere in the sequence (he doesn't know at what n they begin), and the recursive rule. And he would like to recover the sequence, or at the very least, to be able to write the next 10 values taken from the sequence.
-----Input-----
First, two integers, 0<=p<=4, 0<=q<=4. Then come the descriptions of the coefficients, -100 <= a1,...,ap,b1,..., bq,d1,..., dq <= 100. Then, the following 10 integers are Xn,X(n+1),...,X(n+9) for some unknown n.
-----Output-----
10 integers - X(n+10),X(n+11),...,X(n+19)
-----Example-----
Input:
1 1
1
1
1
11 12 13 14 15 16 17 18 19 20
Output:
21 22 23 24 25 26 27 28 29 30
Input:
1 1
1
1
2
1 3 7 15 31 63 127 255 511 1023
Output:
2047 4095 8191 16383 32767 65535 131071 262143 524287 48575
Input:
2 0
1 1
1 1 2 3 5 8 13 21 34 55
Output:
89 144 233 377 610 987 1597 2584 4181 6765
|
{"inputs": ["2 0\n1 1\n1 1 2 3 5 8 13 21 34 55", "1 1\n1\n1\n1\n11 12 13 14 15 16 17 18 19 20", "1 1\n1\n1\n2\n1 3 7 15 31 63 127 255 511 1023"], "outputs": ["89 144 233 377 610 987 1597 2584 4181 6765", "21 22 23 24 25 26 27 28 29 30", "2047 4095 8191 16383 32767 65535 131071 262143 524287 48575"]}
| 590
| 259
|
coding
|
Solve the programming task below in a Python markdown code block.
Two friends Chef and Chefina are currently on floors A and B respectively. They hear an announcement that prizes are being distributed on the ground floor and so decide to reach the ground floor as soon as possible.
Chef can climb down X floors per minute while Chefina can climb down Y floors per minute. Determine who will reach the ground floor first (ie. floor number 0). In case both reach the ground floor together, print Both.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- The first line of each test case contains four space-separated integers A, B, X, and Y — the current floor of Chef, the current floor of Chefina, speed of Chef and speed of Chefina in floors per minute respectively.
------ Output Format ------
For each test case, output on a new line:
- Chef if Chef reaches the ground floor first.
- Chefina if she reaches the ground floor first.
- Both if both reach the ground floor at the same time.
You may print each character of the string in uppercase or lowercase. For example, the strings CHEF, chef, Chef, and chEF are all considered the same.
------ Constraints ------
$1 ≤ T ≤ 2500$
$1 ≤ A, B ≤ 100$
$1 ≤ X, Y ≤ 10$
----- Sample Input 1 ------
4
2 2 2 2
4 2 1 5
3 2 4 1
3 2 2 1
----- Sample Output 1 ------
Both
Chefina
Chef
Chef
----- explanation 1 ------
Test case $1$: Chef is on the second floor and has a speed of $2$ floors per minute. Thus, Chef takes $1$ minute to reach the ground floor. Chefina is on the second floor and and has a speed of $2$ floors per minute. Thus, Chefina takes $1$ minute to reach the ground floor. Both Chef and Chefina reach the ground floor at the same time.
Test case $2$: Chef is on the fourth floor and has a speed of $1$ floor per minute. Thus, Chef takes $4$ minute to reach the ground floor. Chefina is on the second floor and and has a speed of $5$ floors per minute. Thus, Chefina takes $0.4$ minutes to reach the ground floor. Chefina reaches the ground floor first.
Test case $3$: Chef is on the third floor and has a speed of $4$ floors per minute. Thus, Chef takes $0.75$ minutes to reach the ground floor. Chefina is on the second floor and and has a speed of $1$ floor per minute. Thus, Chefina takes $2$ minutes to reach the ground floor. Chef reaches the ground floor first.
Test case $4$: Chef is on the third floor and has a speed of $2$ floors per minute. Thus, Chef takes $1.5$ minutes to reach the ground floor. Chefina is on the second floor and and has a speed of $1$ floor per minute. Thus, Chefina takes $2$ minutes to reach the ground floor. Chef reaches the ground floor first.
|
{"inputs": ["4\n2 2 2 2\n4 2 1 5\n3 2 4 1\n3 2 2 1\n"], "outputs": ["Both\nChefina\nChef\nChef"]}
| 694
| 52
|
coding
|
Solve the programming task below in a Python markdown code block.
Polycarp likes squares and cubes of positive integers. Here is the beginning of the sequence of numbers he likes: $1$, $4$, $8$, $9$, ....
For a given number $n$, count the number of integers from $1$ to $n$ that Polycarp likes. In other words, find the number of such $x$ that $x$ is a square of a positive integer number or a cube of a positive integer number (or both a square and a cube simultaneously).
-----Input-----
The first line contains an integer $t$ ($1 \le t \le 20$) — the number of test cases.
Then $t$ lines contain the test cases, one per line. Each of the lines contains one integer $n$ ($1 \le n \le 10^9$).
-----Output-----
For each test case, print the answer you are looking for — the number of integers from $1$ to $n$ that Polycarp likes.
-----Examples-----
Input
6
10
1
25
1000000000
999999999
500000000
Output
4
1
6
32591
32590
23125
-----Note-----
None
|
{"inputs": ["1\n8\n", "1\n64\n", "1\n26\n", "1\n65\n", "1\n49\n", "1\n26\n", "1\n49\n", "1\n64\n"], "outputs": ["3\n", "10\n", "6\n", "10\n", "9\n", "6\n", "9\n", "10\n"]}
| 296
| 96
|
coding
|
Solve the programming task below in a Python markdown code block.
In Chefland, precipitation is measured using a rain gauge in millimetre per hour.
Chef categorises rainfall as:
LIGHT, if rainfall is less than 3 millimetre per hour.
MODERATE, if rainfall is greater than equal to 3 millimetre per hour and less than 7 millimetre per hour.
HEAVY if rainfall is greater than equal to 7 millimetre per hour.
Given that it rains at X millimetre per hour on a day, find whether the rain is LIGHT, MODERATE, or HEAVY.
------ 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 integer X — the rate of rainfall in millimetre per hour.
------ Output Format ------
For each test case, output on a new line, whether the rain is LIGHT, MODERATE, or HEAVY.
You may print each character in lowercase or uppercase. For example, LIGHT, light, Light, and liGHT, are all identical.
------ Constraints ------
$1 ≤ T ≤ 20$
$1 ≤ X ≤ 20$
----- Sample Input 1 ------
4
1
20
3
7
----- Sample Output 1 ------
LIGHT
HEAVY
MODERATE
HEAVY
----- explanation 1 ------
Test case $1$: The rate of precipitation is less than $3$. Thus, the rain is LIGHT.
Test case $2$: The rate of precipitation is greater than equal to $7$. Thus, the rain is HEAVY.
Test case $3$: The rate of precipitation is greater than equal to $3$ and less than $7$. Thus, the rain is MODERATE.
Test case $4$: The rate of precipitation is greater than equal to $7$. Thus, the rain is HEAVY.
|
{"inputs": ["4\n1\n20\n3\n7\n"], "outputs": ["LIGHT\nHEAVY\nMODERATE\nHEAVY"]}
| 413
| 34
|
coding
|
Solve the programming task below in a Python markdown code block.
-----
ARRAY AND DISTINCT ELEMENTS
-----
Chef is multitalented but he mistakenly took part in 2 contest which will take place
at the same time. So while chef is busy at one cooking contest, he wants you to
take part in coding contest. Chef wants u to solve this program for him.
You have been given an array of size n. You have to calculate a subarray of size k
with maximum sum having distinct elements same as original array.
-----Input Format-----
First line contains no. of test cases. Second line contains n and k. Third line
contains array of n integers.
-----Output-----
Print maximum possible sum as stated in question
-----Example Text Case-----
Input:
1
10 6
8 8 3 5 3 8 5 7 7 7
Output:
37
|
{"inputs": ["1\n10 6\n8 8 3 5 3 8 5 7 7 7"], "outputs": ["37"]}
| 192
| 38
|
coding
|
Solve the programming task below in a Python markdown code block.
```if:python
Note: Python may currently have some performance issues. If you find them, please let me know and provide suggestions to improve the Python version! It's my weakest language... any help is much appreciated :)
```
Artlessly stolen and adapted from Hackerrank.
Kara Danvers is new to CodeWars, and eager to climb up in the ranks. We want to determine Kara's rank as she progresses up the leaderboard.
This kata uses Dense Ranking, so any identical scores count as the same rank (e.g, a scoreboard of `[100, 97, 97, 90, 82, 80, 72, 72, 60]` corresponds with rankings of `[1, 2, 2, 3, 4, 5, 6, 6, 7]`
You are given an array, `scores`, of leaderboard scores, descending, and another array, `kara`, representing Kara's Codewars score over time, ascending. Your function should return an array with each item corresponding to the rank of Kara's current score on the leaderboard.
**Note:** This kata's performance requirements are significantly steeper than the Hackerrank version. Some arrays will contain millions of elements; optimize your code so you don't time out. If you're timing out before 200 tests are completed, you've likely got the wrong code complexity. If you're timing out around 274 tests (there are 278), you likely need to make some tweaks to how you're handling the arrays.
Examples:
(For the uninitiated, Kara Danvers is Supergirl. This is important, because Kara thinks and moves so fast that she can complete a kata within microseconds. Naturally, latency being what it is, she's already opened many kata across many, many tabs, and solves them one by one on a special keyboard so she doesn't have to wait hundreds of milliseconds in between solving them. As a result, the only person's rank changing on the leaderboard is Kara's, so we don't have to worry about shifting values of other codewarriors. Thanks, Supergirl.)
Good luck! Please upvote if you enjoyed it :)
Also feel free to reuse/extend the following starter code:
```python
def leaderboard_climb(arr, kara):
```
|
{"functional": "_inputs = [[[100, 90, 90, 80], [70, 80, 105]], [[982, 490, 339, 180], [180, 250, 721, 2500]], [[1982, 490, 339, 180], [180, 250, 721, 880]], [[1079, 490, 339, 180], [180, 250, 1200, 1980]]]\n_outputs = [[[4, 3, 1]], [[4, 4, 2, 1]], [[4, 4, 2, 2]], [[4, 4, 1, 1]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(leaderboard_climb(*i), o[0])"}
| 505
| 351
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given the root of a binary tree, return the same tree where every subtree (of the given tree) not containing a 1 has been removed.
A subtree of a node node is node plus every node that is a descendant of node.
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 pruneTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
```
|
{"functional": "def check(candidate):\n assert is_same_tree(candidate(root = tree_node([1,None,0,0,1])), tree_node([1,None,0,None,1]))\n assert is_same_tree(candidate(root = tree_node([1,0,1,0,0,0,1])), tree_node([1,None,1,None,1]))\n assert is_same_tree(candidate(root = tree_node([1,1,0,1,1,0,1,0])), tree_node([1,1,0,1,1,None,1]))\n\n\ncheck(Solution().pruneTree)"}
| 150
| 136
|
coding
|
Solve the programming task below in a Python markdown code block.
Byteland is a beautiful land known because of its beautiful trees.
Misha has found a binary tree with $n$ vertices, numbered from $1$ to $n$. A binary tree is an acyclic connected bidirectional graph containing $n$ vertices and $n - 1$ edges. Each vertex has a degree at most $3$, whereas the root is the vertex with the number $1$ and it has a degree at most $2$.
Unfortunately, the root got infected.
The following process happens $n$ times:
Misha either chooses a non-infected (and not deleted) vertex and deletes it with all edges which have an end in this vertex or just does nothing.
Then, the infection spreads to each vertex that is connected by an edge to an already infected vertex (all already infected vertices remain infected).
As Misha does not have much time to think, please tell him what is the maximum number of vertices he can save from the infection (note that deleted vertices are not counted as saved).
-----Input-----
There are several test cases in the input data. The first line contains a single integer $t$ ($1\leq t\leq 5000$) — the number of test cases. This is followed by the test cases description.
The first line of each test case contains one integer $n$ ($2\leq n\leq 3\cdot 10^5$) — the number of vertices of the tree.
The $i$-th of the following $n-1$ lines in the test case contains two positive integers $u_i$ and $v_i$ ($1 \leq u_i, v_i \leq n$), meaning that there exists an edge between them in the graph.
It is guaranteed that the graph is a binary tree rooted at $1$. It is also guaranteed that the sum of $n$ over all test cases won't exceed $3\cdot 10^5$.
-----Output-----
For each test case, output the maximum number of vertices Misha can save.
-----Examples-----
Input
4
2
1 2
4
1 2
2 3
2 4
7
1 2
1 5
2 3
2 4
5 6
5 7
15
1 2
2 3
3 4
4 5
4 6
3 7
2 8
1 9
9 10
9 11
10 12
10 13
11 14
11 15
Output
0
2
2
10
-----Note-----
In the first test case, the only possible action is to delete vertex $2$, after which we save $0$ vertices in total.
In the second test case, if we delete vertex $2$, we can save vertices $3$ and $4$.
|
{"inputs": ["4\n2\n1 2\n4\n1 2\n2 3\n2 4\n7\n1 2\n1 5\n2 3\n2 4\n5 6\n5 7\n15\n1 2\n2 3\n3 4\n4 5\n4 6\n3 7\n2 8\n1 9\n9 10\n9 11\n10 12\n10 13\n11 14\n11 15\n"], "outputs": ["0\n2\n2\n10\n"]}
| 628
| 136
|
coding
|
Solve the programming task below in a Python markdown code block.
Given are positive integers N and K.
Determine if the 3N integers K, K+1, ..., K+3N-1 can be partitioned into N triples (a_1,b_1,c_1), ..., (a_N,b_N,c_N) so that the condition below is satisfied. Any of the integers K, K+1, ..., K+3N-1 must appear in exactly one of those triples.
* For every integer i from 1 to N, a_i + b_i \leq c_i holds.
If the answer is yes, construct one such partition.
Constraints
* 1 \leq N \leq 10^5
* 1 \leq K \leq 10^9
Input
Input is given from Standard Input in the following format:
N K
Output
If it is impossible to partition the integers satisfying the condition, print `-1`. If it is possible, print N triples in the following format:
a_1 b_1 c_1
:
a_N b_N c_N
Output
If it is impossible to partition the integers satisfying the condition, print `-1`. If it is possible, print N triples in the following format:
a_1 b_1 c_1
:
a_N b_N c_N
Examples
Input
1 1
Output
1 2 3
Input
3 3
Output
-1
|
{"inputs": ["3 6", "1 0", "1 2", "6 6", "0 2", "5 6", "7 6", "0 1"], "outputs": ["-1\n", "0 1 2\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n"]}
| 311
| 83
|
coding
|
Solve the programming task below in a Python markdown code block.
There are n beacons located at distinct positions on a number line. The i-th beacon has position a_{i} and power level b_{i}. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance b_{i} inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.
Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.
-----Input-----
The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons.
The i-th of next n lines contains two integers a_{i} and b_{i} (0 ≤ a_{i} ≤ 1 000 000, 1 ≤ b_{i} ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so a_{i} ≠ a_{j} if i ≠ j.
-----Output-----
Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added.
-----Examples-----
Input
4
1 9
3 1
6 1
7 4
Output
1
Input
7
1 1
2 1
3 1
4 1
5 1
6 1
7 1
Output
3
-----Note-----
For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.
For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42.
|
{"inputs": ["1\n0 1\n", "1\n0 1\n", "1\n1 1\n", "1\n0 2\n", "1\n1 2\n", "1\n0 4\n", "1\n1 3\n", "1\n0 3\n"], "outputs": ["0\n", "0", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
| 486
| 101
|
coding
|
Solve the programming task below in a Python markdown code block.
Rachel has a date and tells everyone she is not mad at Ross but doesn't have any feelings for him anymore either. Seeing her date for the first time, everyone notices that he looks exactly like Ross. However, Rachel refuses to believe so.
Rachel makes a list of N characteristics and assigns a score to both Ross and Russ for each of the characteristics. Ross' i^{th} characteristic has a score equal to A_{i} and Russ' i^{th} characteristic has a score equal to B_{i}. Rachel decides that Russ looks exactly like Ross if the following condition is satisfied for at least X distinct values of j, 1 ≤ j ≤ N :
|A_{j} - B_{j}| ≤ K.
Help Rachel in finding if both are alike.
------ Input Format ------
- First line will contain T, number of testcases. Then the testcases follow.
- First line for each test case contains three integers N, X and K, denoting the number of characteristics, the minimum number of characteristics and maximum possible score difference.
- Second line for each test case contains N integers denoting array A.
- Third line for each test case contains N integers denoting array B.
------ Output Format ------
For each test case print "YES" if they are alike, else print "NO".
You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical).
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 10^{3}$
$1 ≤ A_{i}, B_{i} ≤ 10^{3}$
$1 ≤ X ≤ n$
$0 ≤ K ≤ 10^{3}$
----- Sample Input 1 ------
3
4 2 2
1 7 7 5
1 8 1 2
5 1 3
9 8 7 2 5
5 4 1 8 9
3 3 0
2 3 4
2 3 4
----- Sample Output 1 ------
YES
NO
YES
----- explanation 1 ------
Test case 1: The values of absolute difference between scores of Ross' and Russ' characteristics $1$ and $2$ are $0$ and $1$ respectively, both these values are less than $K = 2$. Thus, Ross and Russ are alike.
Test case 2: The values of absolute difference between the scores of *ALL* of the Ross' and Russ' characteristics are greater than $3$.
|
{"inputs": ["3\n4 2 2\n1 7 7 5\n1 8 1 2\n5 1 3\n9 8 7 2 5\n5 4 1 8 9\n3 3 0\n2 3 4\n2 3 4\n"], "outputs": ["YES\nNO\nYES\n"]}
| 564
| 84
|
coding
|
Solve the programming task below in a Python markdown code block.
Truncate the given string (first argument) if it is longer than the given maximum length (second argument). Return the truncated string with a `"..."` ending.
Note that inserting the three dots to the end will add to the string length.
However, if the given maximum string length num is less than or equal to 3, then the addition of the three dots does not add to the string length in determining the truncated string.
## Examples
```
('codewars', 9) ==> 'codewars'
('codewars', 7) ==> 'code...'
('codewars', 2) ==> 'co...'
```
[Taken from FCC](https://www.freecodecamp.com/challenges/truncate-a-string)
Also feel free to reuse/extend the following starter code:
```python
def truncate_string(str,n):
```
|
{"functional": "_inputs = [['pippi', 3], ['Peter Piper picked a peck of pickled peppers', 14], ['A-tisket a-tasket A green and yellow basket', 43], ['A-tisket a-tasket A green and yellow basket', 45], ['A-', 1], ['Chingel loves his Angel so much!!!', 27], ['I like ice-cream.Do you?', 19], ['Seems like you have passed the final test. Congratulations', 53]]\n_outputs = [['pip...'], ['Peter Piper...'], ['A-tisket a-tasket A green and yellow basket'], ['A-tisket a-tasket A green and yellow basket'], ['A...'], ['Chingel loves his Angel ...'], ['I like ice-cream...'], ['Seems like you have passed the final test. Congrat...']]\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(truncate_string(*i), o[0])"}
| 193
| 332
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given N integers \{A_{1}, A_{2}, \ldots, A_{N}\}. Determine whether they can be reordered such that each pair of consecutive differences differ by a factor of 2.
Formally, determine whether there exists a rearrangement of the given integers into an array [B_{1}, B_{2}, \ldots, B_{N}] such that, for each 2 ≤ i ≤ N-1, at least one of the following two conditions holds:
B_{i} - B_{i-1} = 2\cdot (B_{i+1} - B_{i}), *or*
2\cdot (B_{i} - B_{i-1}) = B_{i+1} - B_{i}
Note that different conditions can hold at different indices — the only restriction is that at each index, at least one of the given conditions must hold. Please see the sample tests below for examples.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases. The description of T test cases follows.
- Each test case consists of two lines of input.
- The first line of each test case contains a single integer, N.
- The second line of each test case contains N space-separated integers, denoting A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, output in a single line the answer — \texttt{YES} if a rearrangement that satisfies the given conditions exists, and \texttt{NO} otherwise.
The output is not case sensitive, so for example the strings \texttt{YES, Yes, yES}, etc. will all be treated as correct.
------ Constraints ------
$1 ≤ T ≤ 100$
$3 ≤ N ≤ 10^{5}$
$0 ≤ A_{i} ≤ 10^{9}$
- The sum of $N$ across all test cases won't exceed $10^{5}$
----- Sample Input 1 ------
4
3
5 2 4
5
2 1 16 8 4
5
97 98 100 96 88
6
16 19 18 21 24 22
----- Sample Output 1 ------
Yes
Yes
No
Yes
----- explanation 1 ------
Test case $1$: Rearrange the numbers to form $[5, 4, 2]$. The consecutive differences are $[4-5, 2-4] = [-1, -2]$, and $-2 = 2\cdot (-1)$.
Test case $2$: One possible rearrangement is $[1, 2, 4, 8, 16]$. The consecutive differences are consecutive powers of $2$.
Test case $3$: No rearrangement of the numbers satisfies the condition. For example, one rearrangement is $[97, 98, 100, 96, 88]$ with consecutive differences $[1, 2, -4, -8]$. $2$ and $-4$ do not differ by a factor of $2$ (they differ by a factor of $-2$), so this is not a valid arrangement.
|
{"inputs": ["4\n3\n5 2 4\n5\n2 1 16 8 4\n5\n97 98 100 96 88\n6\n16 19 18 21 24 22"], "outputs": ["Yes\nYes\nNo\nYes\n"]}
| 729
| 78
|
coding
|
Solve the programming task below in a Python markdown code block.
Polycarp has interviewed Oleg and has written the interview down without punctuation marks and spaces to save time. Thus, the interview is now a string s consisting of n lowercase English letters.
There is a filler word ogo in Oleg's speech. All words that can be obtained from ogo by adding go several times to the end of it are also considered to be fillers. For example, the words ogo, ogogo, ogogogo are fillers, but the words go, og, ogog, ogogog and oggo are not fillers.
The fillers have maximal size, for example, for ogogoo speech we can't consider ogo a filler and goo as a normal phrase. We should consider ogogo as a filler here.
To print the interview, Polycarp has to replace each of the fillers with three asterisks. Note that a filler word is replaced with exactly three asterisks regardless of its length.
Polycarp has dealt with this problem in no time. Can you do the same? The clock is ticking!
-----Input-----
The first line contains a positive integer n (1 ≤ n ≤ 100) — the length of the interview.
The second line contains the string s of length n, consisting of lowercase English letters.
-----Output-----
Print the interview text after the replacement of each of the fillers with "***". It is allowed for the substring "***" to have several consecutive occurences.
-----Examples-----
Input
7
aogogob
Output
a***b
Input
13
ogogmgogogogo
Output
***gmg***
Input
9
ogoogoogo
Output
*********
-----Note-----
The first sample contains one filler word ogogo, so the interview for printing is "a***b".
The second sample contains two fillers ogo and ogogogo. Thus, the interview is transformed to "***gmg***".
|
{"inputs": ["1\no\n", "1\na\n", "1\ng\n", "1\nz\n", "1\no\n", "1\ng\n", "1\nz\n", "1\na\n"], "outputs": ["o\n", "a\n", "g\n", "z\n", "o\n", "g\n", "z\n", "a\n"]}
| 411
| 86
|
coding
|
Solve the programming task below in a Python markdown code block.
An array A is called *weird* if it can be sorted in non-decreasing order by applying the given operation any number of times:
Select any index i (1 ≤ i ≤ |A|) and set A_{i} := -A_{i}.
For example: A = [2, 1, 3] is *weird* since after applying the operation at i = 1, A becomes [-2, 1, 3] which is sorted.
JJ has a permutation P of length N. He wants to find the number of subarrays of P which are *weird*. Can you help him?
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the size of the permutation P.
- The second line of each test case contains N space-separated integers P_{1}, P_{2}, \dots, P_{N} denoting the permutation P.
------ Output Format ------
For each test case, output on a new line the number of *weird* subarrays of P.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$P$ is a permutation.
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
3
2 3 1
3
2 1 3
4
1 2 3 4
----- Sample Output 1 ------
5
6
10
----- explanation 1 ------
Test Case 1: Weird subarrays of $P$ are: $[2], [3], [1], [2, 3], [3, 1]$.
Test Case 2: Weird subarrays of $P$ are: $[2], [1], [3], [2, 1], [1, 3], [2, 1, 3]$.
|
{"inputs": ["3\n3\n2 3 1\n3\n2 1 3\n4\n1 2 3 4\n"], "outputs": ["5\n6\n10\n"]}
| 449
| 45
|
coding
|
Solve the programming task below in a Python markdown code block.
Polycarp was presented with some sequence of integers $a$ of length $n$ ($1 \le a_i \le n$). A sequence can make Polycarp happy only if it consists of different numbers (i.e. distinct numbers).
In order to make his sequence like this, Polycarp is going to make some (possibly zero) number of moves.
In one move, he can:
remove the first (leftmost) element of the sequence.
For example, in one move, the sequence $[3, 1, 4, 3]$ will produce the sequence $[1, 4, 3]$, which consists of different numbers.
Determine the minimum number of moves he needs to make so that in the remaining sequence all elements are different. In other words, find the length of the smallest prefix of the given sequence $a$, after removing which all values in the sequence will be unique.
-----Input-----
The first line of the input contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
Each test case consists of two lines.
The first line contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the given sequence $a$.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — elements of the given sequence $a$.
It is guaranteed that the sum of $n$ values over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case print your answer on a separate line — the minimum number of elements that must be removed from the beginning of the sequence so that all remaining elements are different.
-----Examples-----
Input
5
4
3 1 4 3
5
1 1 1 1 1
1
1
6
6 5 4 3 2 1
7
1 2 1 7 1 2 1
Output
1
4
0
0
5
-----Note-----
The following are the sequences that will remain after the removal of prefixes:
$[1, 4, 3]$;
$[1]$;
$[1]$;
$[6, 5, 4, 3, 2, 1]$;
$[2, 1]$.
It is easy to see that all the remaining sequences contain only distinct elements. In each test case, the shortest matching prefix was removed.
|
{"inputs": ["1\n6\n1 1 4 5 1 4\n", "1\n7\n1 6 5 4 3 2 1\n", "5\n4\n3 1 4 3\n5\n1 1 1 1 1\n1\n1\n6\n6 5 4 3 2 1\n7\n1 2 1 7 1 2 1\n"], "outputs": ["3\n", "1\n", "1\n4\n0\n0\n5\n"]}
| 565
| 124
|
coding
|
Solve the programming task below in a Python markdown code block.
There are N robots numbered 1 to N placed on a number line. Robot i is placed at coordinate X_i. When activated, it will travel the distance of D_i in the positive direction, and then it will be removed from the number line. All the robots move at the same speed, and their sizes are ignorable.
Takahashi, who is a mischievous boy, can do the following operation any number of times (possibly zero) as long as there is a robot remaining on the number line.
- Choose a robot and activate it. This operation cannot be done when there is a robot moving.
While Robot i is moving, if it touches another robot j that is remaining in the range [X_i, X_i + D_i) on the number line, Robot j also gets activated and starts moving. This process is repeated recursively.
How many possible sets of robots remaining on the number line are there after Takahashi does the operation some number of times? Compute this count modulo 998244353, since it can be enormous.
-----Constraints-----
- 1 \leq N \leq 2 \times 10^5
- -10^9 \leq X_i \leq 10^9
- 1 \leq D_i \leq 10^9
- X_i \neq X_j (i \neq j)
- All values in input are integers.
-----Input-----
Input is given from Standard Input in the following format:
N
X_1 D_1
:
X_N D_N
-----Output-----
Print the number of possible sets of robots remaining on the number line, modulo 998244353.
-----Sample Input-----
2
1 5
3 3
-----Sample Output-----
3
There are three possible sets of robots remaining on the number line: \{1, 2\}, \{1\}, and \{\}.
These can be achieved as follows:
- If Takahashi activates nothing, the robots \{1, 2\} will remain.
- If Takahashi activates Robot 1, it will activate Robot 2 while moving, after which there will be no robots on the number line. This state can also be reached by activating Robot 2 and then Robot 1.
- If Takahashi activates Robot 2 and finishes doing the operation, the robot \{1\} will remain.
|
{"inputs": ["2\n1 5\n4 3", "2\n1 5\n3 3", "2\n1 5\n3 3\n", "3\n6 5\n-1 7\n3 3", "3\n0 5\n-1 7\n3 3", "3\n6 8\n-1 10\n3 3", "3\n6 5\n-1 10\n3 3", "3\n6 5\n-1 10\n3 3\n"], "outputs": ["3\n", "3", "3\n", "6\n", "4\n", "5\n", "5", "5\n"]}
| 524
| 154
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given the root of a binary tree, return the average value of the nodes on each level in the form of an array. Answers within 10-5 of the actual answer will be accepted.
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 averageOfLevels(self, root: Optional[TreeNode]) -> List[float]:
```
|
{"functional": "def check(candidate):\n assert candidate(root = tree_node([3,9,20,None,None,15,7])) == [3.00000,14.50000,11.00000]\n assert candidate(root = tree_node([3,9,20,15,7])) == [3.00000,14.50000,11.00000]\n\n\ncheck(Solution().averageOfLevels)"}
| 142
| 120
|
coding
|
Solve the programming task below in a Python markdown code block.
Naruto has recently joined the Ninja Academy for higher education, while filling the admission form he is required to choose minimum $X$ subjects out of a list of $N$ subjects. He is not very interested in studies but wants to get a good overall CPI. However, every subject has a different weightage/credit. The credit (denoted as $C$_{i}) of a particular course is equal to the position in which it appears in the subject list, i.e, the first course has a credit=1, the second course has a credit=2, and so on.
Naruto knows his capabilities, and so he has come up with the scores ($S$_{i}) he can achieve in every course. He wants to maximise his CPI, which can be calculated as the weighted mean of all the subjects taken, denoted as
__Sum(C_{i}*S_{i})/Sum(C_{i})__ for all i s.t Naruto has taken up course i
------ Input: ------
First line will contain $T$, number of testcases. Then the testcases follow.
First line of every test case contains two integers, $N$ and $X$
Next line of the test case contains $N$ space separated integers S_{i}, denoting the score he can get in every subject
------ Output: ------
For each testcase, output in a single line the maximum cpi he can get after choosing the subjects. Your output will be considered correct if it has an absolute error of less than 10^{-6}.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 1000$
$1 ≤ X ≤ N$
$1 ≤ S$_{i}$ ≤ 10000$
------ Sample Input: ------
1
5 2
50 40 30 20 10
------ Sample Output: ------
43.3333333333
------ Explanation: ------
He can achieve the max credit by picking the first two subjects, which produces a CPI of (50 * 1+40 * 2)/(1+2) = 43.333333333
|
{"inputs": ["1 \n5 2 \n50 40 30 20 10 "], "outputs": ["43.3333333333\n\n"]}
| 486
| 51
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a rebus of form ? + ? - ? + ? = n, consisting of only question marks, separated by arithmetic operation '+' and '-', equality and positive integer n. The goal is to replace each question mark with some positive integer from 1 to n, such that equality holds.
Input
The only line of the input contains a rebus. It's guaranteed that it contains no more than 100 question marks, integer n is positive and doesn't exceed 1 000 000, all letters and integers are separated by spaces, arithmetic operations are located only between question marks.
Output
The first line of the output should contain "Possible" (without quotes) if rebus has a solution and "Impossible" (without quotes) otherwise.
If the answer exists, the second line should contain any valid rebus with question marks replaced by integers from 1 to n. Follow the format given in the samples.
Examples
Input
? + ? - ? + ? + ? = 42
Output
Possible
9 + 13 - 39 + 28 + 31 = 42
Input
? - ? = 1
Output
Impossible
Input
? = 1000000
Output
Possible
1000000 = 1000000
|
{"inputs": ["? - ? = 2\n", "? - ? = 1\n", "? = 1000010\n", "? = 1010010\n", "? = 1100000\n", "? = 1000011\n", "? = 1010011\n", "? = 1010001\n"], "outputs": ["Impossible", "Impossible\n", "Possible\n1000010 = 1000010", "Possible\n1010010 = 1010010", "Possible\n1100000 = 1100000", "Possible\n1000011 = 1000011", "Possible\n1010011 = 1010011", "Possible\n1010001 = 1010001"]}
| 296
| 222
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a string S of length N consisting of lowercase English letters.
We will cut this string at one position into two strings X and Y.
Here, we would like to maximize the number of different letters contained in both X and Y.
Find the largest possible number of different letters contained in both X and Y when we cut the string at the optimal position.
-----Constraints-----
- 2 \leq N \leq 100
- |S| = N
- S consists of lowercase English letters.
-----Input-----
Input is given from Standard Input in the following format:
N
S
-----Output-----
Print the largest possible number of different letters contained in both X and Y.
-----Sample Input-----
6
aabbca
-----Sample Output-----
2
If we cut the string between the third and fourth letters into X = aab and Y = bca, the letters contained in both X and Y are a and b.
There will never be three or more different letters contained in both X and Y, so the answer is 2.
|
{"inputs": ["6\n`abbca", "6\nb`^dca", "6\nacbba`", "6\n`abbda", "6\nadbba`", "6\nadbbb`", "6\nadbcb`", "6\n`bbbda"], "outputs": ["2\n", "0\n", "2\n", "2\n", "2\n", "1\n", "1\n", "1\n"]}
| 229
| 96
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.