task_type
stringclasses 1
value | problem
stringlengths 209
3.39k
| answer
stringlengths 35
6.15k
| problem_tokens
int64 60
774
| answer_tokens
int64 12
2.04k
|
|---|---|---|---|---|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules:
In the beginning, you have the permutation P=[1,2,3,...,m].
For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i].
Return an array containing the result for the given queries.
Please complete the following python code precisely:
```python
class Solution:
def processQueries(self, queries: List[int], m: int) -> List[int]:
```
|
{"functional": "def check(candidate):\n assert candidate(queries = [3,1,2,1], m = 5) == [2,1,2,1] \n assert candidate(queries = [4,1,2,2], m = 4) == [3,1,2,0]\n assert candidate(queries = [7,5,5,8,3], m = 8) == [6,5,0,7,5]\n\n\ncheck(Solution().processQueries)"}
| 175
| 117
|
coding
|
Solve the programming task below in a Python markdown code block.
F: Tea Party
Yun decided to hold a tea party at the company.
The shop sells $ N $ sets of bread, each containing $ A_1, A_2, A_3, \ dots, A_N $.
Yun decided to make a sandwich by combining two breads into a pair.
Yun-san is very careful, so I want to make sure that I don't have any leftover bread.
Calculate how many sandwiches you can make at most.
input
The first line is given the integer $ N $.
On the second line, $ N $ integers $ A_1, A_2, A_3, \ dots, A_N $ are given, separated by blanks.
output
Output the maximum number of sandwiches you can make. However, insert a line break at the end.
Constraint
* $ N $ is an integer greater than or equal to $ 1 $ and less than or equal to $ 100 $
* $ A_1, A_2, A_3, \ dots, A_N $ are integers between $ 1 $ and $ 100 $
Input example 1
Five
2 3 5 6 7
Output example 1
Ten
Buy the first, third, fourth, and fifth sets to get $ 20 $ in bread.
If you buy all the sets, you will end up with $ 23 $ of bread and you will have a surplus.
Input example 2
Four
3 5 6 8
Output example 2
11
Example
Input
5
2 3 5 6 7
Output
10
|
{"inputs": ["5\n4 3 5 6 7", "5\n2 3 3 6 7", "5\n6 2 5 0 3", "5\n9 2 2 0 6", "5\n0 0 7 6 0", "5\n0 0 7 1 0", "5\n0 9 2 0 2", "5\n0 0 7 0 0"], "outputs": ["11\n", "9\n", "8\n", "5\n", "3\n", "4\n", "2\n", "0\n"]}
| 359
| 143
|
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, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.
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 rightSideView(self, root: Optional[TreeNode]) -> List[int]:
```
|
{"functional": "def check(candidate):\n assert candidate(root = tree_node([1,2,3,None,5,None,4])) == [1,3,4]\n assert candidate(root = tree_node([1,None,3])) == [1,3]\n assert candidate(root = tree_node([])) == []\n\n\ncheck(Solution().rightSideView)"}
| 135
| 82
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given the head of a singly linked list, return the middle node of the linked list.
If there are two middle nodes, return the second middle node.
Please complete the following python code precisely:
```python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
```
|
{"functional": "def check(candidate):\n assert is_same_list(candidate(head = list_node([1,2,3,4,5])), list_node([3,4,5]))\n assert is_same_list(candidate(head = list_node([1,2,3,4,5,6])), list_node([4,5,6]))\n\n\ncheck(Solution().middleNode)"}
| 122
| 85
|
coding
|
Solve the programming task below in a Python markdown code block.
Takahashi has A cookies, and Aoki has B cookies.
Takahashi will do the following action K times:
- If Takahashi has one or more cookies, eat one of his cookies.
- Otherwise, if Aoki has one or more cookies, eat one of Aoki's cookies.
- If they both have no cookies, do nothing.
In the end, how many cookies will Takahashi and Aoki have, respectively?
-----Constraints-----
- 0 \leq A \leq 10^{12}
- 0 \leq B \leq 10^{12}
- 0 \leq K \leq 10^{12}
- All values in input are integers.
-----Input-----
Input is given from Standard Input in the following format:
A B K
-----Output-----
Print the numbers of Takahashi's and Aoki's cookies after K actions.
-----Sample Input-----
2 3 3
-----Sample Output-----
0 2
Takahashi will do the following:
- He has two cookies, so he eats one of them.
- Now he has one cookie left, and he eats it.
- Now he has no cookies left, but Aoki has three, so Takahashi eats one of them.
Thus, in the end, Takahashi will have 0 cookies, and Aoki will have 2.
|
{"inputs": ["2 3 1", "2 3 2", "2 3 0", "2 6 0", "4 6 0", "8 6 0", "0 6 0", "0 9 0"], "outputs": ["1 3\n", "0 3\n", "2 3\n", "2 6\n", "4 6\n", "8 6\n", "0 6\n", "0 9\n"]}
| 302
| 110
|
coding
|
Solve the programming task below in a Python markdown code block.
Chef has learned a new technique for comparing two recipes. A recipe contains a list of ingredients in increasing order of the times they will be processed. An ingredient is represented by a letter 'a'-'z'. The i-th letter in a recipe denotes the i-th ingredient. An ingredient can be used multiple times in a recipe.
The technique is as follows. Compare two recipes by comparing their respective lists. If the sets of ingredients used in both recipes are equal and each ingredient is used the same number of times in both of them (processing order does not matter), they are declared as granama recipes. ("granama" is the Chef-ian word for "similar".)
Chef took two recipes he invented yesterday. He wanted to compare them using the technique. Unfortunately, Chef forgot to keep track of the number of times each ingredient has been used in a recipe. He only compared the ingredients but NOT their frequencies. More precisely, Chef considers two recipes as granama if there are no ingredients which are used in one recipe and not used in the other recipe.
Your task is to report whether Chef has correctly classified the two recipes (as granama or not granama) although he forgot to keep track of the frequencies.
------ Input ------
The first line of the input contains a single integer T denoting the number of test cases. The description for T test cases follows. Each test case consists of a single line containing two space-separated strings R and S denoting the two recipes.
------ Output ------
For each test case, output a single line containing "YES" (quotes for clarity) if Chef correctly classified the two recipes as granama or not granama. Otherwise, output a single line containing "NO" (quotes for clarity) if Chef declared two recipes as granama when they actually are not.
------ Constraints ------
1 ≤ T ≤ 100
1 ≤ |R|, |S| ≤ 1000
----- Sample Input 1 ------
3
alex axle
paradise diapers
alice bob
----- Sample Output 1 ------
YES
NO
YES
----- explanation 1 ------
Example case 1: Chef declared them as granama recipes. They are actually granama because the sets of ingredients and the number of times each ingredient has been used are equal. The Chef got it right!
Example case 2: Chef declared them as granama recipes because both sets of ingredients are equal. But they are NOT granama since ingredient 'a' has been used twice in the first recipe but only once in the second. The Chef was incorrect!
Example case 3: Chef declare them as not granama. They are not granama as the sets of ingredients are different. Hence, the Chef was right!
|
{"inputs": ["3\nalex axle\nparadise diapers\nalice bob", "3\nalex axle\nesidarap diapers\nalice bob", "3\nelax axle\nesidarap djapers\nalice bob", "3\nelax axle\nesidarap diapers\nalice bob", "3\nelax ayle\nesidarap djapers\nalice bob", "3\nelax ayle\nesidarap srepajd\nalice bob", "3\nelax ayle\netidarap srepajd\nalice bob", "3\nelax aely\netidarap srepajd\nalice bob"], "outputs": ["YES\nNO\nYES", "YES\nNO\nYES\n", "YES\nYES\nYES\n", "YES\nNO\nYES\n", "YES\nYES\nYES\n", "YES\nYES\nYES\n", "YES\nYES\nYES\n", "YES\nYES\nYES\n"]}
| 571
| 202
|
coding
|
Solve the programming task below in a Python markdown code block.
There is a famous olympiad, which has more than a hundred participants. The Olympiad consists of two stages: the elimination stage, and the final stage. At least a hundred participants will advance to the final stage. The elimination stage in turn consists of two contests.
A result of the elimination stage is the total score in two contests, but, unfortunately, the jury lost the final standings and has only standings for the first and for the second contest separately.
In each contest, the participants are ranked by their point score in non-increasing order. When two participants have a tie (earned the same score), they are ranked by their passport number (in accordance with local regulations, all passport numbers are distinct).
In the first contest, the participant on the 100-th place scored $a$ points. Also, the jury checked all participants from the 1-st to the 100-th place (inclusive) in the first contest and found out that all of them have at least $b$ points in the second contest.
Similarly, for the second contest, the participant on the 100-th place has $c$ points. And the jury checked that all the participants from the 1-st to the 100-th place (inclusive) have at least $d$ points in the first contest.
After two contests, all participants are ranked by their total score in two contests in non-increasing order. When participants have the same total score, tie-breaking with passport numbers is used. The cutoff score to qualify to the final stage is the total score of the participant on the 100-th place.
Given integers $a$, $b$, $c$, $d$, please help the jury determine the smallest possible value of the cutoff score.
-----Input-----
You need to process $t$ test cases.
The first line contains an integer $t$ ($1 \leq t \leq 3025$) — the number of test cases. Then descriptions of $t$ test cases follow.
The first line of each test case contains four integers $a$, $b$, $c$, $d$ ($0 \le a,\,b,\,c,\,d \le 9$; $d \leq a$; $b \leq c$).
One can show that for any test case satisfying the constraints above, there is at least one olympiad scenario possible.
-----Output-----
For each test case print a single integer — the smallest possible cutoff score in some olympiad scenario satisfying the given information.
-----Example-----
Input
2
1 2 2 1
4 8 9 2
Output
3
12
-----Note-----
For the first test case, consider the following olympiad scenario: there are $101$ participants in the elimination stage, each having $1$ point for the first contest and $2$ points for the second contest. Hence the total score of the participant on the 100-th place is $3$.
For the second test case, consider the following olympiad scenario: there are $50$ participants with points $5$ and $9$ for the first and second contest respectively; $50$ participants with points $4$ and $8$ for the first and second contest respectively; and $50$ participants with points $2$ and $9$ for the first and second contest respectively. Hence the total point score of the participant on the 100-th place is $12$.
|
{"inputs": ["1\n0 0 0 0\n", "1\n0 0 0 0\n", "1\n0 0 1 0\n", "1\n0 0 2 0\n", "1\n4 0 0 0\n", "1\n1 0 0 0\n", "1\n2 0 0 0\n", "1\n1 0 1 0\n"], "outputs": ["0\n", "0\n", "1\n", "2\n", "4\n", "1\n", "2\n", "1\n"]}
| 750
| 134
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.
You are given a convex polygon with N vertices.
You have to find any ⌊N/10⌋ distinct points with integer coordinates that lie strictly inside the polygon, or determine that such a set of points doesn't exist.
Note: ⌊⌋ denotes the floor function, typically used in integer division.
------ 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 denoting the number of vertices of the polygon.
The following N lines describe the vertices of the polygon in anticlockwise order. Each of these lines contains two space-separated integers x and y denoting the coordinates of one vertex.
------ Output ------
For each test case, if a valid set of points doesn't exist, print a single line containing the integer -1. Otherwise, print ⌊N/10⌋ lines. Each of these lines should contain two space-separated integers denoting the coordinates of one point.
The coordinates of all points should be integers with absolute value not exceeding 10^{9}. If there are multiple solutions, you may print any one.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$10 ≤ N ≤ 10^{5}$
$sum of N over all test cases ≤ 5 · 10^{5}$
$|x|, |y| ≤ 10^{9}$
$no three vertices of the polygon will be collinear$
------ Subtasks ------
Subtask #1 (30 points): 1 ≤ T ≤ 100, 10 ≤
N ≤ 100
Subtask #2 (70 points): original constraints
------ Example ------
------ Input Format ------
1
11
0 0
1 1
2 3
2 5
0 10
-2 10
-5 9
-8 7
-8 4
-6 1
-2 0
------ Output Format ------
0 1
------ Explanation ------
Example case 1: The polygon is shown in the picture:
You are required to output exactly ⌊N/10⌋ = ⌊11/10⌋ = 1 point.
One possible point that you can output is (0, 1). You can also output (0, 2), (-1, 3) or (1, 3).
However, (0, 0) is a wrong answer, because this point lies on the border of the convex polygon.
If you output (-1, -1), that's also a wrong answer, because this point lies outside the convex polygon. You can't output (1.1, 1.5) either, because it doesn't have integer coordinates, though it lies strictly inside the polygon.
|
{"inputs": ["1\n11\n0 0\n1 1\n2 3\n2 5\n0 10\n-2 10\n-5 9\n-8 7\n-8 4\n-6 1\n-2 0"], "outputs": ["0 1"]}
| 635
| 69
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given n × m table. Each cell of the table is colored white or black. Find the number of non-empty sets of cells such that:
All cells in a set have the same color. Every two cells in a set share row or column.
-----Input-----
The first line of input contains integers n and m (1 ≤ n, m ≤ 50) — the number of rows and the number of columns correspondingly.
The next n lines of input contain descriptions of rows. There are m integers, separated by spaces, in each line. The number equals 0 if the corresponding cell is colored white and equals 1 if the corresponding cell is colored black.
-----Output-----
Output single integer — the number of non-empty sets from the problem description.
-----Examples-----
Input
1 1
0
Output
1
Input
2 3
1 0 1
0 1 0
Output
8
-----Note-----
In the second example, there are six one-element sets. Additionally, there are two two-element sets, the first one consists of the first and the third cells of the first row, the second one consists of the first and the third cells of the second row. To sum up, there are 8 sets.
|
{"inputs": ["1 1\n0\n", "1 1\n0\n", "2 2\n1 1\n1 1\n", "2 2\n1 1\n1 1\n", "2 2\n1 0\n1 1\n", "2 3\n1 0 1\n0 1 0\n", "2 3\n1 0 1\n1 1 0\n", "2 3\n1 1 1\n0 1 0\n"], "outputs": ["1\n", "1\n", "8\n", "8\n", "6\n", "8\n", "9\n", "12\n"]}
| 277
| 151
|
coding
|
Solve the programming task below in a Python markdown code block.
Given the string S and m queries. The i-th query is given by the two strings xi and yi.
For each query, answer the longest substring of the string S, starting with xi and ending with yi.
For the string S, | S | represents the length of S. Also, the fact that the character string T is a substring of the character string S means that a certain integer i exists and Tj = Si + j is satisfied for 1 ≤ j ≤ | T |. Where Tj represents the jth character of T.
Constraints
* 1 ≤ | S | ≤ 2 x 105
* 1 ≤ m ≤ 105
* 1 ≤ | xi |, | yi |
* $ \ sum ^ m_ {i = 1} $ (| xi | + | yi |) ≤ 2 x 105
* S and xi, yi consist only of half-width lowercase letters.
Input
The input is given in the following format.
S
m
x1 y1
x2 y2
::
xm ym
* The character string S is given on the first line.
* The number of queries m is given in the second line.
* Of the m lines from the 3rd line, the i-th query string xi, yi is given on the i-th line, separated by spaces.
Output
Answer the maximum substring length in the following format.
len1
len2
::
lenm
Output the longest substring length leni that satisfies the condition for the i-th query on the i-th line of the m lines from the first line. If there is no such substring, output 0.
Examples
Input
abracadabra
5
ab a
a a
b c
ac ca
z z
Output
11
11
4
3
0
Input
howistheprogress
4
ist prog
s ss
how is
the progress
Output
9
12
5
11
Input
icpcsummertraining
9
mm m
icpc summer
train ing
summer mm
i c
i i
g g
train i
summer er
Output
2
10
8
0
4
16
1
6
6
|
{"inputs": ["abracadabsa\n5\nab a\na a\nb c\nac ca\nz z", "abracadabsa\n5\nab a\na b\nb c\nac ca\nz z", "abracadabsa\n5\nab a\na c\nb c\nac ca\nz z", "abracadabsa\n5\nab a\na c\na c\nac ca\nz z", "abracadabsa\n5\nba a\na c\na c\nac ca\nz y", "abracadabsa\n5\nba a\na c\nb c\nac ca\nz y", "abracadabsa\n2\nba a\na c\nb c\nac ca\nz z", "abracadabsa\n2\nb` a\na d\nb c\nad ab\nz z"], "outputs": ["11\n11\n4\n3\n0\n", "11\n9\n4\n3\n0\n", "11\n5\n4\n3\n0\n", "11\n5\n5\n3\n0\n", "0\n5\n5\n3\n0\n", "0\n5\n4\n3\n0\n", "0\n5\n", "0\n7\n"]}
| 491
| 288
|
coding
|
Solve the programming task below in a Python markdown code block.
A mischievous notice has arrived from the primitive slow-life organization "Akaruda". Akaruda is famous for mischief such as throwing a pie at the face of a VIP, but recently it has become more radical, such as using gunpowder to sprinkle rat fireworks at the reception venue. The notice is the following text.
--- Personal computer Takes human time. not good.
When the short and long hands of the clock meet, Akaruda justice is done.
Slow life great.
It's terrifying and I'm not sure, but it seems to mean that the mischief is carried out when the minute hand and the minute hand of the clock overlap.
To be wary of this mischief, create a program that inputs the time and outputs "alert" if the short hand and the long hand are close, "safe" if they are far, and "warning" otherwise. However, "close" means that the angle between the short hand and the long hand is 0 ° or more and less than 30 °, and "far" means that the angle is 90 ° or more and 180 ° or less. The time should be between 00:00 and 11:59.
Input
The input is given in the following format:
n
hh1: mm1
hh2: mm2
::
hhn: mmn
The number of times to be judged on the first line n (1 ≤ n ≤ 10000), and the i-th time hhi: mmi on the second and subsequent lines are given to each line.
Output
Output the judgment result of the i-th time safe, warning, or alert in order on one line.
Example
Input
4
02:15
06:01
11:55
10:40
Output
alert
safe
alert
warning
|
{"inputs": ["4\n02:15\n05:01\n11:55\n10:40", "4\n02:15\n05:01\n11:54\n10:40", "4\n12:15\n05:01\n11:54\n10:40", "4\n02:15\n05:01\n11:55\n11:40", "4\n12:05\n05:01\n11:44\n00:41", "4\n02:15\n10:50\n11:54\n10:40", "4\n03:05\n06:11\n11:55\n10:41", "4\n03:15\n11:60\n11:55\n00:40"], "outputs": ["alert\nsafe\nalert\nwarning\n", "alert\nsafe\nwarning\nwarning\n", "warning\nsafe\nwarning\nwarning\n", "alert\nsafe\nalert\nsafe\n", "alert\nsafe\nwarning\nsafe\n", "alert\nalert\nwarning\nwarning\n", "warning\nsafe\nalert\nwarning\n", "alert\nalert\nalert\nsafe\n"]}
| 403
| 302
|
coding
|
Solve the programming task below in a Python markdown code block.
Vasya has the sequence consisting of n integers. Vasya consider the pair of integers x and y k-interesting, if their binary representation differs from each other exactly in k bits. For example, if k = 2, the pair of integers x = 5 and y = 3 is k-interesting, because their binary representation x=101 and y=011 differs exactly in two bits.
Vasya wants to know how many pairs of indexes (i, j) are in his sequence so that i < j and the pair of integers a_{i} and a_{j} is k-interesting. Your task is to help Vasya and determine this number.
-----Input-----
The first line contains two integers n and k (2 ≤ n ≤ 10^5, 0 ≤ k ≤ 14) — the number of integers in Vasya's sequence and the number of bits in which integers in k-interesting pair should differ.
The second line contains the sequence a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^4), which Vasya has.
-----Output-----
Print the number of pairs (i, j) so that i < j and the pair of integers a_{i} and a_{j} is k-interesting.
-----Examples-----
Input
4 1
0 3 2 1
Output
4
Input
6 0
200 100 100 100 200 200
Output
6
-----Note-----
In the first test there are 4 k-interesting pairs: (1, 3), (1, 4), (2, 3), (2, 4).
In the second test k = 0. Consequently, integers in any k-interesting pair should be equal to themselves. Thus, for the second test there are 6 k-interesting pairs: (1, 5), (1, 6), (2, 3), (2, 4), (3, 4), (5, 6).
|
{"inputs": ["2 0\n1 1\n", "2 0\n0 0\n", "2 1\n0 1\n", "2 1\n0 2\n", "2 1\n0 1\n", "2 0\n1 1\n", "2 1\n0 2\n", "2 0\n0 0\n"], "outputs": ["1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n"]}
| 473
| 118
|
coding
|
Solve the programming task below in a Python markdown code block.
A hypergraph is a generalization of a graph, where an edge can connect any number of vertices. A k-uniform hypergraph is a hypergraph such that all its hyperedges have size k. For more information, see Wikipedia.
Let's call a particular hypergraph a hypertree if it is connected (that is, you can move from any vertex to any other vertex using only its hyperedges) and removing any of its hyperedges makes the hypergraph disconnected (note that this definition of hypertrees differs from the standard one).
Given just one integer N, find the number of 3-uniform hypertrees on N vertices. Two 3-uniform hypertrees are considered different if a hyperedge (u, v, w) exists such that it is present in exactly one of these hypertrees (note that the order of vertices in the hyperedge doesn't matter, and neither does the order of hyperedges in the hypertree).
-----Input-----
The first line of the input contains an integer T -- the number of test cases (at most 15). Then T lines follow, each contains an integer N (3 ≤ N ≤ 17).
Important! Please include all code used in solving this problem in your solution.
-----Output-----
For each test case output one line containing the requested number. It's guaranteed that this number won't exceed 263-1.
-----Examples-----
Input:
4
3
4
5
8
Output:
1
6
25
93268
Explanation:
There is just one 3-uniform hypertree on 3 vertices: {(1,2,3)}. There are six of them on 4 vertices: {(1,2,3), (1,2,4)}, {(1,2,3), (1,3,4)}, {(1,2,3), (2,3,4)}, {(1,2,4), (1,3,4)}, {(1,2,4), (2,3,4)}, {(1,3,4), (2,3,4)}. Two of the 25 possible hypertrees on 5 vertices are {(1,2,3), (3,4,5)} and {(1,2,3), (1,2,4), (1,2,5)}.
|
{"inputs": ["4\n3\n4\n5\n8\n\n"], "outputs": ["1\n6\n25\n93268"]}
| 492
| 33
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.
Trans bought a calculator at creatnx's store. Unfortunately, it is fake. It has many bugs. One of them is adding two numbers without carrying. Example expression: 12 + 9 will have result 11 in his calculator. Given an expression in the form a + b, please output result from that calculator.
------ Input ------
The first line contains an integer T denote the number of test cases. Each test case contains two integers a, b in a single line.
------ Output ------
Each test case, print answer in a single line.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ a, b ≤ 10^{9}$
Subtasks:
$Subtask #1 (30 points): 1 ≤ a, b ≤ 9$
$Subtask #2 (70 points): original constrains$
----- Sample Input 1 ------
2
12 9
25 25
----- Sample Output 1 ------
11
40
|
{"inputs": ["2\n12 9\n25 25"], "outputs": ["11\n40"]}
| 241
| 27
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given an array a_1, a_2, ..., a_n and an array b_1, b_2, ..., b_n.
For one operation you can sort in non-decreasing order any subarray a[l ... r] of the array a.
For example, if a = [4, 2, 2, 1, 3, 1] and you choose subbarray a[2 ... 5], then the array turns into [4, 1, 2, 2, 3, 1].
You are asked to determine whether it is possible to obtain the array b by applying this operation any number of times (possibly zero) to the array a.
Input
The first line contains one integer t (1 ≤ t ≤ 3 ⋅ 10^5) — the number of queries.
The first line of each query contains one integer n (1 ≤ n ≤ 3 ⋅ 10^5).
The second line of each query contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n).
The third line of each query contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ n).
It is guaranteed that ∑ n ≤ 3 ⋅ 10^5 over all queries in a test.
Output
For each query print YES (in any letter case) if it is possible to obtain an array b and NO (in any letter case) otherwise.
Example
Input
4
7
1 7 1 4 4 5 6
1 1 4 4 5 7 6
5
1 1 3 3 5
1 1 3 3 5
2
1 1
1 2
3
1 2 3
3 2 1
Output
YES
YES
NO
NO
Note
In first test case the can sort subarray a_1 ... a_5, then a will turn into [1, 1, 4, 4, 7, 5, 6], and then sort subarray a_5 ... a_6.
|
{"inputs": ["1\n2\n2 1\n1 1\n", "1\n2\n2 1\n2 2\n", "1\n2\n1 2\n1 1\n", "1\n3\n3 1 2\n2 1 3\n", "1\n3\n2 1 3\n1 3 2\n", "1\n3\n1 3 2\n2 1 3\n", "1\n3\n2 2 2\n1 1 1\n", "1\n3\n3 2 1\n1 3 2\n"], "outputs": ["NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "YES\n"]}
| 480
| 170
|
coding
|
Solve the programming task below in a Python markdown code block.
From an array A containing N integers, you construct a binary string S of length (N - 1) as follows. For all 1 ≤ i < N:
If A_{i} < A_{i+1}, then S_{i} = 0.
If A_{i} > A_{i+1}, then S_{i} = 1.
Given the string S, determine the count of indices i (1 ≤ i ≤ N) such that it is possible for A_{i} to be the maximum element of the array A.
------ 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 array A.
- The second line of each test case contains the binary string S of length (N - 1) containing 0s and 1s only.
------ Output Format ------
For each test case, output the count of indices i (1 ≤ i ≤ N) such that it is possible for A_{i} to be the maximum element of the array A.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$2 ≤ N ≤ 10^{5}$
- Sum of $N$ over all test cases does not exceed $10^{5}$.
----- Sample Input 1 ------
3
2
0
7
000111
6
11100
----- Sample Output 1 ------
1
1
2
----- explanation 1 ------
Test case $1$: Here $A_{1} < A_{2}$. Therefore clearly only $A_{2}$ can be the maximum element of $A$.
Test case $2$: Here $A_{1} < A_{2}$, $A_{2} < A_{3}$, $A_{3} < A_{4}$, $A_{4} > A_{5}$, $A_{5} > A_{6}$ and $A_{6} > A_{7}$. Therefore, clearly only $A_{4}$ can be the maximum element of $A$.
Test case $3$: Here $A_{1} > A_{2}$, $A_{2} > A_{3}$, $A_{3} > A_{4}$, $A_{4} < A_{5}$ and $A_{5} < A_{6}$. Therefore $A_{1}$ and $A_{6}$ both can be the maximum elements of $A$.
|
{"inputs": ["3\n2\n0\n7\n000111\n6\n11100\n"], "outputs": ["1\n1\n2\n"]}
| 554
| 39
|
coding
|
Solve the programming task below in a Python markdown code block.
There is a square grid with N rows and M columns. Each square contains an integer: 0 or 1. The square at the i-th row from the top and the j-th column from the left contains a_{ij}.
Among the 2^{N+M} possible pairs of a subset A of the rows and a subset B of the columns, find the number of the pairs that satisfy the following condition, modulo 998244353:
* The sum of the |A||B| numbers contained in the intersection of the rows belonging to A and the columns belonging to B, is odd.
Constraints
* 1 \leq N,M \leq 300
* 0 \leq a_{i,j} \leq 1(1\leq i\leq N,1\leq j\leq M)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M
a_{11} ... a_{1M}
:
a_{N1} ... a_{NM}
Output
Print the number of the pairs of a subset of the rows and a subset of the columns that satisfy the condition, modulo 998244353.
Examples
Input
2 2
0 1
1 0
Output
6
Input
2 3
0 0 0
0 1 0
Output
8
|
{"inputs": ["2 2\n1 1\n1 0", "2 2\n1 0\n1 0", "2 2\n1 0\n2 0", "1 2\n1 0\n2 0", "2 2\n2 1\n1 0", "2 2\n2 0\n1 0", "1 2\n2 0\n2 0", "2 2\n0 1\n1 0"], "outputs": ["6\n", "4\n", "4\n", "2\n", "6\n", "4\n", "2\n", "6"]}
| 321
| 141
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given two images, img1 and img2, represented as binary, square matrices of size n x n. A binary matrix has only 0s and 1s as values.
We translate one image however we choose by sliding all the 1 bits left, right, up, and/or down any number of units. We then place it on top of the other image. We can then calculate the overlap by counting the number of positions that have a 1 in both images.
Note also that a translation does not include any kind of rotation. Any 1 bits that are translated outside of the matrix borders are erased.
Return the largest possible overlap.
Please complete the following python code precisely:
```python
class Solution:
def largestOverlap(self, img1: List[List[int]], img2: List[List[int]]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(img1 = [[1,1,0],[0,1,0],[0,1,0]], img2 = [[0,0,0],[0,1,1],[0,0,1]]) == 3\n assert candidate(img1 = [[1]], img2 = [[1]]) == 1\n assert candidate(img1 = [[0]], img2 = [[0]]) == 0\n\n\ncheck(Solution().largestOverlap)"}
| 190
| 109
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given a binary string s. You are allowed to perform two types of operations on the string in any sequence:
Type-1: Remove the character at the start of the string s and append it to the end of the string.
Type-2: Pick any character in s and flip its value, i.e., if its value is '0' it becomes '1' and vice-versa.
Return the minimum number of type-2 operations you need to perform such that s becomes alternating.
The string is called alternating if no two adjacent characters are equal.
For example, the strings "010" and "1010" are alternating, while the string "0100" is not.
Please complete the following python code precisely:
```python
class Solution:
def minFlips(self, s: str) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(s = \"111000\") == 2\n assert candidate(s = \"010\") == 0\n assert candidate(s = \"1110\") == 1\n\n\ncheck(Solution().minFlips)"}
| 194
| 66
|
coding
|
Solve the programming task below in a Python markdown code block.
Vasya’s elder brother Petya loves playing computer games. In one of his favourite computer games Petya reached the final level where a fight with the boss take place.
While playing the game Petya found spell scrolls and now he is about to use them. Let’s describe the way fighting goes on this level:
1) The boss has two parameters: max — the initial amount of health and reg — regeneration rate per second.
2) Every scroll also has two parameters: powi — spell power measured in percents — the maximal amount of health counted off the initial one, which allows to use the scroll (i.e. if the boss has more than powi percent of health the scroll cannot be used); and dmgi the damage per second inflicted upon the boss if the scroll is used. As soon as a scroll is used it disappears and another spell is cast upon the boss that inflicts dmgi of damage per second upon him until the end of the game.
During the battle the actions per second are performed in the following order: first the boss gets the damage from all the spells cast upon him, then he regenerates reg of health (at the same time he can’t have more than max of health), then the player may use another scroll (no more than one per second).
The boss is considered to be defeated if at the end of a second he has nonpositive ( ≤ 0) amount of health.
Help Petya to determine whether he can win with the set of scrolls available to him and if he can, determine the minimal number of seconds he needs to do it.
Input
The first line contains three integers N, max and reg (1 ≤ N, max, reg ≤ 1000) –– the amount of scrolls and the parameters of the boss. The next N lines contain two integers powi and dmgi each — the parameters of the i-th scroll (0 ≤ powi ≤ 100, 1 ≤ dmgi ≤ 2000).
Output
In case Petya can’t complete this level, output in the single line NO.
Otherwise, output on the first line YES. On the second line output the minimal time after which the boss can be defeated and the number of used scrolls. In the next lines for each used scroll output space-separated number of seconds passed from the start of the battle to the moment the scroll was used and the number of the scroll. Scrolls are numbered starting from 1 in the input order. The first scroll is considered to be available to be used after 0 seconds.
Output scrolls in the order they were used. It is not allowed to use scrolls after the boss is defeated.
Examples
Input
2 10 3
100 3
99 1
Output
NO
Input
2 100 10
100 11
90 9
Output
YES
19 2
0 1
10 2
|
{"inputs": ["2 10 3\n100 3\n99 1\n", "2 100 2\n100 2\n100 2\n", "2 1000 1\n100 1\n100 1\n", "2 1000 1\n100 1\n100 2\n", "2 1001 0\n101 1\n100 2\n", "2 1000 1\n101 1\n100 2\n", "2 1001 1\n101 1\n100 2\n", "2 100 10\n100 11\n90 9\n"], "outputs": ["NO\n", "YES\n51 2\n0 1\n1 2\n", "YES\n1001 2\n0 1\n1 2\n", "YES\n501 2\n0 2\n1 1\n", "YES\n334 2\n0 2\n1 1\n", "YES\n501 2\n0 2\n1 1\n", "YES\n501 2\n0 2\n1 1\n", "YES\n19 2\n0 1\n10 2\n"]}
| 628
| 316
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a positive decimal number x.
Your task is to convert it to the "simple exponential notation".
Let x = a·10^{b}, where 1 ≤ a < 10, then in general case the "simple exponential notation" looks like "aEb". If b equals to zero, the part "Eb" should be skipped. If a is an integer, it should be written without decimal point. Also there should not be extra zeroes in a and b.
-----Input-----
The only line contains the positive decimal number x. The length of the line will not exceed 10^6. Note that you are given too large number, so you can't use standard built-in data types "float", "double" and other.
-----Output-----
Print the only line — the "simple exponential notation" of the given number x.
-----Examples-----
Input
16
Output
1.6E1
Input
01.23400
Output
1.234
Input
.100
Output
1E-1
Input
100.
Output
1E2
|
{"inputs": ["1\n", "1\n", "2\n", "3\n", "4\n", "16\n", ".1\n", "1.\n"], "outputs": ["1\n", "1\n", "2\n", "3\n", "4\n", "1.6E1\n", "1E-1\n", "1\n"]}
| 247
| 79
|
coding
|
Solve the programming task below in a Python markdown code block.
We have a string S of length N consisting of uppercase English letters.
How many times does ABC occur in S as contiguous subsequences (see Sample Inputs and Outputs)?
-----Constraints-----
- 3 \leq N \leq 50
- S consists of uppercase English letters.
-----Input-----
Input is given from Standard Input in the following format:
N
S
-----Output-----
Print number of occurrences of ABC in S as contiguous subsequences.
-----Sample Input-----
10
ZABCDBABCQ
-----Sample Output-----
2
Two contiguous subsequences of S are equal to ABC: the 2-nd through 4-th characters, and the 7-th through 9-th characters.
|
{"inputs": ["3\nABC\n", "3\nDEF\n", "10\nQCBABDCBAZ", "10\nZABCDB@BCQ", "10\nZABCCB@BDQ", "10\nZABCCB@CDQ", "10\nQDB@BCCBAZ", "10\nZABCCQ@BDB"], "outputs": ["1\n", "0\n", "0\n", "1\n", "1\n", "1\n", "0\n", "1\n"]}
| 157
| 120
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given a 1-indexed integer array nums of length n.
An element nums[i] of nums is called special if i divides n, i.e. n % i == 0.
Return the sum of the squares of all special elements of nums.
Please complete the following python code precisely:
```python
class Solution:
def sumOfSquares(self, nums: List[int]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(nums = [1,2,3,4]) == 21\n assert candidate(nums = [2,7,1,19,18,3]) == 63\n\n\ncheck(Solution().sumOfSquares)"}
| 103
| 65
|
coding
|
Solve the programming task below in a Python markdown code block.
Write a program which print coordinates $(x_i, y_i)$ of given $n$ points on the plane by the following criteria.
1. first by $x$-coordinate
2. in case of a tie, by $y$-coordinate
Constraints
* $1 \leq n \leq 100,000$
* $-1,000,000,000 \leq x_i, y_i \leq 1,000,000,000$
Input
The input is given in the following format.
$n$
$x_0 \; y_0$
$x_1 \; y_1$
:
$x_{n-1} \; y_{n-1}$
In the first line, the number of points $n$ is given. In the following $n$ lines, coordinates of each point are given.
Output
Print coordinate of given points in order.
Example
Input
5
4 7
5 5
2 3
6 8
2 1
Output
2 1
2 3
4 7
5 5
6 8
|
{"inputs": ["5\n4 7\n5 5\n2 3\n5 8\n2 1", "5\n4 7\n5 5\n2 6\n5 8\n2 1", "5\n4 7\n5 5\n2 3\n6 8\n2 1", "5\n4 7\n5 5\n2 12\n5 8\n2 1", "5\n0 7\n5 5\n2 12\n5 8\n2 1", "5\n0 6\n8 9\n3 1\n6 16\n0 0", "5\n0 7\n5 5\n2 12\n5 16\n2 1", "5\n0 7\n5 5\n3 12\n5 16\n2 1"], "outputs": ["2 1\n2 3\n4 7\n5 5\n5 8\n", "2 1\n2 6\n4 7\n5 5\n5 8\n", "2 1\n2 3\n4 7\n5 5\n6 8", "2 1\n2 12\n4 7\n5 5\n5 8\n", "0 7\n2 1\n2 12\n5 5\n5 8\n", "0 0\n0 6\n3 1\n6 16\n8 9\n", "0 7\n2 1\n2 12\n5 5\n5 16\n", "0 7\n2 1\n3 12\n5 5\n5 16\n"]}
| 262
| 379
|
coding
|
Solve the programming task below in a Python markdown code block.
Consider having a cow that gives a child every year from her fourth year of life on and all her subsequent children do the same.
After n years how many cows will you have?
Return null if n is not an integer.
Note: Assume all the cows are alive after n years.
Also feel free to reuse/extend the following starter code:
```python
def count_cows(n):
```
|
{"functional": "_inputs = [[1], [3], [5]]\n_outputs = [[1], [2], [4]]\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(count_cows(*i), o[0])"}
| 92
| 167
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given an array with $N$ integers: $A[1], A[2], \ldots, A[N]$ (where $N$ is even). You are allowed to permute the elements however you want. Say, after permuting the elements, you end up with the array $A'[1], A'[2], \ldots, A'[N]$. Your goal is to maximize the following sum:
|A′[1]−A′[2]| + |A′[3]−A′[4]| + ... + |A′[N−1]−A′[N]||A′[1]−A′[2]| + |A′[3]−A′[4]| + ... + |A′[N−1]−A′[N]|
|A'[1] - A'[2]| \ + \ |A'[3] - A'[4]| \ + \ ... \ + \ |A'[N - 1] - A'[N]|
Here, $|x|$ denotes the absolute value of $x$.
You have to print the maximum sum achievable.
-----Input-----
- The first line contains $T$, the number of test cases.
- Each test case starts with an integer $N$ in the first line.
- The second line of each test case contains $N$ space separated integers, denoting the values of array $A$.
-----Output-----
For each test case, output the maximum sum achievable in a new line.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le N \le 10^5$
- $N$ is even
- $|A[i]| \le 10^9$
- Sum of $N$ over all test cases $\le 2 * 10^5$
-----Example Input 1-----
1
4
1 -3 2 -3
-----Example Output 1-----
9
-----Explanation 1-----
The original array is {$1, -3, 2, -3$}. Suppose you permute it and get the array {$2, 1, -3, -3$}. Then the corresponding sum would be $|2 - 1| \ + \ |-3 - (-3)| = 1 + 0 = 1$.
But suppose you permute it differently and get the array {$-3, 2, 1, -3$}. Then the corresponding sum would be $|-3 - 2| \ + \ |1 - (-3)| = 5 + 4 = 9$. You can check that you cannot do any better, and hence the answer is 9.
|
{"inputs": ["1\n4\n1 -3 2 -3"], "outputs": ["9"]}
| 583
| 22
|
coding
|
Solve the programming task below in a Python markdown code block.
Auction
square1001 You were watching a certain auction.
An auction is a transaction in which when there are a large number of buyers and the number of items is limited, the one with the highest price is given the right to buy. (From the 7th edition of the Shinmei Kokugo Dictionary)
The rules of the auction here are as follows.
1. Repeat steps 2 to 6 below until the item is exhausted.
2. Bring out new items and show them to customers
3. One of the customers puts the item at the price they like
4. Price higher than the price currently on the item by one of the customers
5. Repeat step 4 until there are no more customers to perform the action of 4.
6. The customer who puts the highest price on the item wins the bid
square1001 You recorded all the prices attached to the items during this auction in chronological order.
According to the record, $ N $ items are priced, starting from the beginning with $ A_1, A_2, A_3, \ dots, A_N $.
E869120 You are wondering how many items were put up for sale in this auction.
square1001 From the list you made, find the minimum and maximum possible number of items for sale in this auction.
input
Input is given from standard input in the following format.
$ N $
$ A_1 $ $ A_2 $ $ A_3 $ $ \ cdots $ $ A_N $
output
Please output the minimum and maximum possible number of items listed in this auction in this order, separated by line breaks.
However, insert a line break at the end.
Constraint
* $ 1 \ leq N \ leq 100000 \ (= 10 ^ 5) $
* $ 1 \ leq A_i \ leq 1000000000 \ (= 10 ^ 9) $
* All inputs are integers.
Input example 1
Five
8 6 9 1 20
Output example 1
3
Five
When three items are sold in order at price 8, price 9, and price 20, the minimum number of items is 3.
Input example 2
6
3 3 4 3 3 4
Output example 2
Four
6
Input example 3
8
5 5 4 4 3 3 2 2
Output example 3
8
8
Example
Input
5
8 6 9 1 20
Output
3
5
|
{"inputs": ["5\n8 6 9 1 0", "5\n0 6 9 1 0", "5\n1 1 1 1 0", "5\n0 1 1 2 4", "5\n0 6 6 1 0", "5\n0 5 6 1 0", "5\n0 5 6 2 0", "5\n0 5 6 3 0"], "outputs": ["4\n5\n", "3\n5\n", "5\n5\n", "2\n5\n", "4\n5\n", "3\n5\n", "3\n5\n", "3\n5\n"]}
| 577
| 158
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problem statements in [Mandarin], [Bengali], [Russian], and [Vietnamese] as well.
Chef will have N guests in his house today. He wants to serve at least one dish to each of the N guests. Chef can make two types of dishes. He needs one fruit and one vegetable to make the first type of dish and one vegetable and one fish to make the second type of dish. Now Chef has A fruits, B vegetables, and C fishes in his house. Can he prepare at least N dishes in total?
------ Input Format ------
- First line will contain T, number of testcases. Then the testcases follow.
- Each testcase contains of a single line of input, four integers N, A, B, C.
------ Output Format ------
For each test case, print "YES" if Chef can prepare at least N dishes, otherwise print "NO". Print the output without quotes.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N, A, B, C ≤ 100$
------ subtasks ------
Subtask 1 (100 points): Original constraints
----- Sample Input 1 ------
4
2 1 2 1
3 2 2 2
4 2 6 3
3 1 3 1
----- Sample Output 1 ------
YES
NO
YES
NO
----- explanation 1 ------
Test case $1$: Chef prepares one dish of the first type using one fruit and one vegetable and another dish of the second type using one vegetable and one fish.
Test case $2$: Chef prepares two dishes of the first type using two fruit and two vegetable. Now all the vegetables are exhausted, hence he can't prepare any other dishes.
Test case $3$: Chef can prepare a total of $5$ dishes, two dishes of the first type and three dishes of the second type.
|
{"inputs": ["4\n2 1 2 1\n3 2 2 2\n4 2 6 3\n3 1 3 1\n"], "outputs": ["YES\nNO\nYES\nNO"]}
| 412
| 51
|
coding
|
Solve the programming task below in a Python markdown code block.
Consider a football tournament where n teams participate. Each team has two football kits: for home games, and for away games. The kit for home games of the i-th team has color x_{i} and the kit for away games of this team has color y_{i} (x_{i} ≠ y_{i}).
In the tournament, each team plays exactly one home game and exactly one away game with each other team (n(n - 1) games in total). The team, that plays the home game, traditionally plays in its home kit. The team that plays an away game plays in its away kit. However, if two teams has the kits of the same color, they cannot be distinguished. In this case the away team plays in its home kit.
Calculate how many games in the described tournament each team plays in its home kit and how many games it plays in its away kit.
-----Input-----
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of teams. Next n lines contain the description of the teams. The i-th line contains two space-separated numbers x_{i}, y_{i} (1 ≤ x_{i}, y_{i} ≤ 10^5; x_{i} ≠ y_{i}) — the color numbers for the home and away kits of the i-th team.
-----Output-----
For each team, print on a single line two space-separated integers — the number of games this team is going to play in home and away kits, correspondingly. Print the answers for the teams in the order they appeared in the input.
-----Examples-----
Input
2
1 2
2 1
Output
2 0
2 0
Input
3
1 2
2 1
1 3
Output
3 1
4 0
2 2
|
{"inputs": ["2\n1 2\n2 1\n", "2\n1 2\n1 2\n", "2\n1 2\n3 4\n", "2\n1 2\n1 2\n", "2\n1 2\n3 4\n", "2\n1 2\n1 0\n", "2\n1 4\n2 1\n", "2\n1 2\n3 0\n"], "outputs": ["2 0\n2 0\n", "1 1\n1 1\n", "1 1\n1 1\n", "1 1\n1 1\n", "1 1\n1 1\n", "1 1\n1 1\n", "1 1\n2 0\n", "1 1\n1 1\n"]}
| 400
| 182
|
coding
|
Solve the programming task below in a Python markdown code block.
# Convert number to reversed array of digits
Given a random non-negative number, you have to return the digits of this number within an array in reverse order.
## Example:
```
348597 => [7,9,5,8,4,3]
```
Also feel free to reuse/extend the following starter code:
```python
def digitize(n):
```
|
{"functional": "_inputs = [[35231], [23582357], [984764738], [45762893920], [548702838394]]\n_outputs = [[[1, 3, 2, 5, 3]], [[7, 5, 3, 2, 8, 5, 3, 2]], [[8, 3, 7, 4, 6, 7, 4, 8, 9]], [[0, 2, 9, 3, 9, 8, 2, 6, 7, 5, 4]], [[4, 9, 3, 8, 3, 8, 2, 0, 7, 8, 4, 5]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(digitize(*i), o[0])"}
| 93
| 339
|
coding
|
Solve the programming task below in a Python markdown code block.
C: Only one subsequence --Unique Subsequence-
problem
One day Ebi-chan noticed that a text string T of length n and a pattern string P (m \ leq n) of length m were placed on the desk. Ebi-chan loves the "only one subsequence" that appears in strings, so she immediately began investigating whether P was the only subsequence of T.
The property that P is the only subsequence of T is expressed as follows: Now write the i-th character of the string X as X_i.
* A sequence of length m S = (s_1, ..., s_m) (where s_1 <s_2 <... <s_m) and for each i (i = 1, ..., m) T_ {s_i} The sequence S with = P_i is uniquely determined.
For several hours after starting the investigation, Ebi-chan was staring at the string, but it seems that she was tired because the string was too long. If you couldn't see it, you decided to help Ebi-chan. For Ebi-chan, write a program that outputs “yes” if P is only one subsequence of T, and “no” otherwise.
Input format
T
P
The first line is given the text string T. The second line is given the pattern string P.
Constraint
* 1 \ leq | P | \ leq | T | \ leq 5 \ times 10 ^ 5
* T and P are composed of lowercase letters ‘a’-’z’.
Output format
Print “yes” if P is a single subsequence of T, otherwise “no” on one line.
Input example 1
aizucamp
azu
Output example 1
yes
Input example 2
abracadabra
rada
Output example 2
no
Input example 3
hokkaido
dekai
Output example 3
no
Example
Input
aizucamp
azu
Output
yes
|
{"inputs": ["aizucamp\nuza", "aziucamp\nazu", "azjucamp\nazu", "azjucamp\nazt", "azjucamp\nzat", "pmacujza\nzat", "pmacuiza\nzat", "pmicuaza\nzat"], "outputs": ["no\n", "yes\n", "yes\n", "no\n", "no\n", "no\n", "no\n", "no\n"]}
| 433
| 107
|
coding
|
Solve the programming task below in a Python markdown code block.
A monster is attacking the Cyberland!
Master Yang, a braver, is going to beat the monster. Yang and the monster each have 3 attributes: hitpoints (HP), offensive power (ATK) and defensive power (DEF).
During the battle, every second the monster's HP decrease by max(0, ATKY - DEFM), while Yang's HP decreases by max(0, ATKM - DEFY), where index Y denotes Master Yang and index M denotes monster. Both decreases happen simultaneously Once monster's HP ≤ 0 and the same time Master Yang's HP > 0, Master Yang wins.
Master Yang can buy attributes from the magic shop of Cyberland: h bitcoins per HP, a bitcoins per ATK, and d bitcoins per DEF.
Now Master Yang wants to know the minimum number of bitcoins he can spend in order to win.
Input
The first line contains three integers HPY, ATKY, DEFY, separated by a space, denoting the initial HP, ATK and DEF of Master Yang.
The second line contains three integers HPM, ATKM, DEFM, separated by a space, denoting the HP, ATK and DEF of the monster.
The third line contains three integers h, a, d, separated by a space, denoting the price of 1 HP, 1 ATK and 1 DEF.
All numbers in input are integer and lie between 1 and 100 inclusively.
Output
The only output line should contain an integer, denoting the minimum bitcoins Master Yang should spend in order to win.
Examples
Input
1 2 1
1 100 1
1 100 100
Output
99
Input
100 100 100
1 1 1
1 1 1
Output
0
Note
For the first sample, prices for ATK and DEF are extremely high. Master Yang can buy 99 HP, then he can beat the monster with 1 HP left.
For the second sample, Master Yang is strong enough to beat the monster, so he doesn't need to buy anything.
|
{"inputs": ["1 1 1\n1 1 1\n1 1 1\n", "1 0 1\n1 1 1\n1 1 1\n", "1 0 2\n1 1 1\n1 1 1\n", "1 100 1\n1 1 1\n1 1 1\n", "79 1 1\n1 1 10\n1 1 100\n", "79 1 1\n2 1 10\n1 1 100\n", "79 1 0\n2 1 10\n1 1 100\n", "1 10 29\n1 1 43\n1 1 100\n"], "outputs": ["1", "2\n", "2\n", "0", "10", "10\n", "10\n", "34"]}
| 456
| 217
|
coding
|
Solve the programming task below in a Python markdown code block.
In this Kata, two players, Alice and Bob, are playing a palindrome game. Alice starts with `string1`, Bob starts with `string2`, and the board starts out as an empty string. Alice and Bob take turns; during a turn, a player selects a letter from his or her string, removes it from the string, and appends it to the board; if the board becomes a palindrome (of length >= 2), the player wins. Alice makes the first move. Since Bob has the disadvantage of playing second, then he wins automatically if letters run out or the board is never a palindrome. Note also that each player can see the other player's letters.
The problem will be presented as `solve(string1,string2)`. Return 1 if Alice wins and 2 it Bob wins.
For example:
```Haskell
solve("abc","baxy") = 2 -- There is no way for Alice to win. If she starts with 'a', Bob wins by playing 'a'. The same case with 'b'. If Alice starts with 'c', Bob still wins because a palindrome is not possible. Return 2.
solve("eyfjy","ooigvo") = 1 -- Alice plays 'y' and whatever Bob plays, Alice wins by playing another 'y'. Return 1.
solve("abc","xyz") = 2 -- No palindrome is possible, so Bob wins; return 2
solve("gzyqsczkctutjves","hpaqrfwkdntfwnvgs") = 1 -- If Alice plays 'g', Bob wins by playing 'g'. Alice must be clever. She starts with 'z'. She knows that since she has two 'z', the win is guaranteed. Note that she also has two 's'. But she cannot play that. Can you see why?
solve("rmevmtw","uavtyft") = 1 -- Alice wins by playing 'm'. Can you see why?
```
Palindrome lengths should be at least `2` characters. More examples in the test cases.
Good luck!
Also feel free to reuse/extend the following starter code:
```python
def solve(str1, str2):
```
|
{"functional": "_inputs = [['abc', 'xyz'], ['abc', 'axy'], ['abc', 'bax'], ['btzgd', 'svjyb'], ['eyfjy', 'ooigv'], ['mctimp', 'eyqbnh'], ['qtkxttl', 'utvohqk']]\n_outputs = [[2], [2], [2], [2], [1], [1], [2]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(solve(*i), o[0])"}
| 472
| 233
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given an array consisting of $n$ integers $a_1$, $a_2$, ..., $a_n$. Initially $a_x = 1$, all other elements are equal to $0$.
You have to perform $m$ operations. During the $i$-th operation, you choose two indices $c$ and $d$ such that $l_i \le c, d \le r_i$, and swap $a_c$ and $a_d$.
Calculate the number of indices $k$ such that it is possible to choose the operations so that $a_k = 1$ in the end.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Then the description of $t$ testcases follow.
The first line of each test case contains three integers $n$, $x$ and $m$ ($1 \le n \le 10^9$; $1 \le m \le 100$; $1 \le x \le n$).
Each of next $m$ lines contains the descriptions of the operations; the $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$).
-----Output-----
For each test case print one integer — the number of indices $k$ such that it is possible to choose the operations so that $a_k = 1$ in the end.
-----Example-----
Input
3
6 4 3
1 6
2 3
5 5
4 1 2
2 4
1 2
3 3 2
2 3
1 2
Output
6
2
3
-----Note-----
In the first test case, it is possible to achieve $a_k = 1$ for every $k$. To do so, you may use the following operations: swap $a_k$ and $a_4$; swap $a_2$ and $a_2$; swap $a_5$ and $a_5$.
In the second test case, only $k = 1$ and $k = 2$ are possible answers. To achieve $a_1 = 1$, you have to swap $a_1$ and $a_1$ during the second operation. To achieve $a_2 = 1$, you have to swap $a_1$ and $a_2$ during the second operation.
|
{"inputs": ["1\n16 771 1\n2 146\n", "1\n152 771 0\n3 282\n", "1\n65 1380 0\n3 282\n", "1\n13 1380 0\n3 282\n", "1\n152 771 1\n2 146\n", "1\n152 771 0\n13 15\n", "1\n239 771 0\n3 282\n", "1\n65 2224 0\n3 282\n"], "outputs": ["1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n"]}
| 556
| 197
|
coding
|
Solve the programming task below in a Python markdown code block.
You are standing on the $\mathit{OX}$-axis at point $0$ and you want to move to an integer point $x > 0$.
You can make several jumps. Suppose you're currently at point $y$ ($y$ may be negative) and jump for the $k$-th time. You can:
either jump to the point $y + k$
or jump to the point $y - 1$.
What is the minimum number of jumps you need to reach the point $x$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first and only line of each test case contains the single integer $x$ ($1 \le x \le 10^6$) — the destination point.
-----Output-----
For each test case, print the single integer — the minimum number of jumps to reach $x$. It can be proved that we can reach any integer point $x$.
-----Examples-----
Input
5
1
2
3
4
5
Output
1
3
2
3
4
-----Note-----
In the first test case $x = 1$, so you need only one jump: the $1$-st jump from $0$ to $0 + 1 = 1$.
In the second test case $x = 2$. You need at least three jumps:
the $1$-st jump from $0$ to $0 + 1 = 1$;
the $2$-nd jump from $1$ to $1 + 2 = 3$;
the $3$-rd jump from $3$ to $3 - 1 = 2$;
Two jumps are not enough because these are the only possible variants:
the $1$-st jump as $-1$ and the $2$-nd one as $-1$ — you'll reach $0 -1 -1 =-2$;
the $1$-st jump as $-1$ and the $2$-nd one as $+2$ — you'll reach $0 -1 +2 = 1$;
the $1$-st jump as $+1$ and the $2$-nd one as $-1$ — you'll reach $0 +1 -1 = 0$;
the $1$-st jump as $+1$ and the $2$-nd one as $+2$ — you'll reach $0 +1 +2 = 3$;
In the third test case, you need two jumps: the $1$-st one as $+1$ and the $2$-nd one as $+2$, so $0 + 1 + 2 = 3$.
In the fourth test case, you need three jumps: the $1$-st one as $-1$, the $2$-nd one as $+2$ and the $3$-rd one as $+3$, so $0 - 1 + 2 + 3 = 4$.
|
{"inputs": ["1\n8\n", "1\n6\n", "1\n3\n", "1\n1\n", "1\n4\n", "1\n7\n", "1\n10\n", "1\n10\n"], "outputs": ["4\n", "3\n", "2\n", "1\n", "3\n", "4\n", "4\n", "4\n"]}
| 672
| 88
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given an integer N.
Find the number of the positive divisors of N!, modulo 10^9+7.
-----Constraints-----
- 1≤N≤10^3
-----Input-----
The input is given from Standard Input in the following format:
N
-----Output-----
Print the number of the positive divisors of N!, modulo 10^9+7.
-----Sample Input-----
3
-----Sample Output-----
4
There are four divisors of 3! =6: 1, 2, 3 and 6. Thus, the output should be 4.
|
{"inputs": ["8", "0", "2", "4", "9", "5", "7", "6"], "outputs": ["96\n", "1\n", "2\n", "8\n", "160\n", "16\n", "60\n", "30"]}
| 139
| 67
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a permutation p_1, p_2, ... , p_n (an array where each integer from 1 to n appears exactly once). The weight of the i-th element of this permutation is a_i.
At first, you separate your permutation into two non-empty sets — prefix and suffix. More formally, the first set contains elements p_1, p_2, ... , p_k, the second — p_{k+1}, p_{k+2}, ... , p_n, where 1 ≤ k < n.
After that, you may move elements between sets. The operation you are allowed to do is to choose some element of the first set and move it to the second set, or vice versa (move from the second set to the first). You have to pay a_i dollars to move the element p_i.
Your goal is to make it so that each element of the first set is less than each element of the second set. Note that if one of the sets is empty, this condition is met.
For example, if p = [3, 1, 2] and a = [7, 1, 4], then the optimal strategy is: separate p into two parts [3, 1] and [2] and then move the 2-element into first set (it costs 4). And if p = [3, 5, 1, 6, 2, 4], a = [9, 1, 9, 9, 1, 9], then the optimal strategy is: separate p into two parts [3, 5, 1] and [6, 2, 4], and then move the 2-element into first set (it costs 1), and 5-element into second set (it also costs 1).
Calculate the minimum number of dollars you have to spend.
Input
The first line contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the length of permutation.
The second line contains n integers p_1, p_2, ... , p_n (1 ≤ p_i ≤ n). It's guaranteed that this sequence contains each element from 1 to n exactly once.
The third line contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9).
Output
Print one integer — the minimum number of dollars you have to spend.
Examples
Input
3
3 1 2
7 1 4
Output
4
Input
4
2 4 1 3
5 9 8 3
Output
3
Input
6
3 5 1 6 2 4
9 1 9 9 1 9
Output
2
|
{"inputs": ["2\n2 1\n9 5\n", "2\n1 2\n9 9\n", "2\n2 1\n14 5\n", "2\n2 1\n14 7\n", "2\n2 1\n14 3\n", "2\n2 1\n26 7\n", "2\n2 1\n28 7\n", "2\n1 2\n17 9\n"], "outputs": ["5\n", "0\n", "5\n", "7\n", "3\n", "7\n", "7\n", "0\n"]}
| 599
| 140
|
coding
|
Solve the programming task below in a Python markdown code block.
JJ gives you a number N and challenges you to construct a permutation P of length N such that:
|P_{i + 1} - P_{i}| ≥ 2 for all 1 ≤ i ≤ N - 1 (Here |x| denotes the absolute value of x)
Can you complete JJ's challenge?
As a reminder, a permutation of length N is an array where every integer from 1 to N occurs exactly once.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first and only line of each test case contains one integer N — the length of the permutation P to be constructed.
------ Output Format ------
For each test case, output N space-separated integers P_{1}, P_{2}, \ldots, P_{N} denoting the permutation P which satisfies the given conditions.
If no such permutation exists, output -1.
If there are multiple answers, print any.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$2 ≤ N ≤ 10^{5}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$
----- Sample Input 1 ------
2
2
6
----- Sample Output 1 ------
-1
1 4 2 5 3 6
----- explanation 1 ------
Test Case 1: It can be proven that no permutation of length $2$ exists that satisfies the given conditions.
Test Case 2: $[1, 4, 2, 5, 3, 6]$ satisfies the given condition, since $|P_{i + 1} - P_{i}| ≥ 2$ for all $1 ≤ i ≤ N - 1$
|
{"inputs": ["2\n2\n6\n"], "outputs": ["-1\n1 4 2 5 3 6\n"]}
| 393
| 31
|
coding
|
Solve the programming task below in a Python markdown code block.
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side.
Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$.
You should follow several rules to build the fence:
the consecutive sections should have a common side of length at least $1$;
the first and the last sections should stand on the corresponding ground levels;
the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer);
One of possible fences (blue color) for the first test case
Is it possible to build a fence that meets all rules?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section.
The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section.
It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO.
You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
-----Examples-----
Input
3
6 3
0 0 2 5 1 1
2 3
0 2
3 2
3 0 2
Output
YES
YES
NO
-----Note-----
In the first test case, one of the possible fences is shown in the picture.
In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled.
In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
|
{"inputs": ["1\n2 2\n2 8\n", "1\n2 2\n4 8\n", "1\n2 2\n3 9\n", "1\n2 4\n1 6\n", "1\n2 3\n0 9\n", "1\n2 5\n2 9\n", "1\n2 2\n2 9\n", "1\n2 3\n0 8\n"], "outputs": ["NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n"]}
| 659
| 134
|
coding
|
Solve the programming task below in a Python markdown code block.
There are n villages in a Chefland. Some of the villages have electricity facilities, other doesn't. You can consider the villages arranged in line in the order 1 to n from left to right. i-th of village can be considered at xi coordinates.
Chef decided that electricity should be provided to all the villages. So, he decides to buy some amount of electric wires to connect the villeges without electricity to some villages with electricity. As Chef does not want to spend too much amount of money on wires, can you find out minimum amount of length of wire Chef should buy.
-----Input-----
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains an integer n denoting number of villages in Chefland.
Second line will contain a string of length n containing '0' or '1's only. If i-th character of the string is '1', then it denotes that i-th village has electricity.
Next line contains n space separated integers denoting the x coordinates of the villages in the order from village 1 to n
-----Output-----
For each test case, output a single line containing a integer corresponding to the minimum length of wire Chef needs to buy.
-----Constraints-----
- 1 ≤ T ≤ 10
- It is guaranteed that there will be at least one village which will have electricity.
- 1 ≤ x1 < x2 < ... < xn ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ N ≤ 1000
Subtask #2 : 70 points
- 1 ≤ N ≤ 105
-----Example-----
Input
2
2
01
1 2
3
100
1 5 6
Output:
1
5
-----Explanation-----
In the first example, first village does not have electricity. If we put a wire between village 1 and 2 of length 1, then both the villages will have electricity.
In the second example,
We can a draw a wire from first village to third village, passing through second village. Its total length will be 5. Now all the villages will have electricity. This is the minimum length of wire you will require.
|
{"inputs": ["2\n2\n01\n1 2\n3\n100\n1 5 6"], "outputs": ["1\n5"]}
| 491
| 35
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.
Chef and his little brother are playing with sticks. Initially, Chef has $N$ sticks with lengths $A_{1}, A_{2}, \dots, A_{N}$. He should give his little brother at least $K$ of these sticks. Afterwards, Chef's brother should choose four sticks and use them to form a rectangle (a square is also a rectangle). Chef does not want his brother to break any of the sticks, so each of the chosen sticks should be one side of the rectangle.
Chef's little brother wants the rectangle he makes to have the maximum possible area, while Chef wants it to have the minimum possible area. Therefore, Chef wants to choose the sticks to give his brother in such a way that they cannot be used to form a rectangle; if it's impossible, then he wants to choose the sticks in such a way that the maximum area of a rectangle his little brother can form is minimum possible.
Can you find the area of the resulting rectangle (or determine that it won't be possible to form any) if both Chef and his little brother play optimally?
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $K$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \dots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer — the area of the resulting rectangle or $-1$ if Chef can give his brother sticks that cannot be used to form any rectangle.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 10^{5}$
$0 ≤ K ≤ N$
the sum of $N$ over all test cases does not exceed $10^{5}$
$1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$
----- Sample Input 1 ------
2
7 6
1 2 3 1 2 2 1
5 4
1 2 1 2 3
----- Sample Output 1 ------
2
-1
----- explanation 1 ------
Example case 1: Chef should give his brother all sticks except one. Regardless of the length of that one stick, Chef's brother can choose sticks with lengths $1, 2, 1, 2$ to create a rectangle with area $1 \cdot 2 = 2$.
Example case 2: If Chef gives his brother sticks with lengths $2, 1, 2, 3$, then there is no way to form a rectangle.
|
{"inputs": ["2\n7 6\n1 2 3 1 2 2 1\n5 4\n1 2 1 2 3"], "outputs": ["2\n-1"]}
| 605
| 47
|
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, and an integer K. Print the string obtained by replacing every character in S that differs from the K-th character of S, with `*`.
Constraints
* 1 \leq K \leq N\leq 10
* S is a string of length N consisting of lowercase English letters.
* N and K are integers.
Input
Input is given from Standard Input in the following format:
N
S
K
Output
Print the string obtained by replacing every character in S that differs from the K-th character of S, with `*`.
Examples
Input
5
error
2
Output
*rr*r
Input
6
eleven
5
Output
e*e*e*
Input
9
education
7
Output
******i**
|
{"inputs": ["5\norrer\n2", "5\nrorre\n2", "5\norrer\n1", "5\nrerro\n1", "5\nrerro\n2", "5\nerror\n4", "5\norrer\n4", "5\nserro\n1"], "outputs": ["*rr*r\n", "*o***\n", "o****\n", "r*rr*\n", "*e***\n", "***o*\n", "***e*\n", "s****\n"]}
| 188
| 116
|
coding
|
Solve the programming task below in a Python markdown code block.
Four players participate in the playoff tournament. The tournament is held according to the following scheme: the first player will play with the second, and the third player with the fourth, then the winners of the pairs will play in the finals of the tournament.
It is known that in a match between two players, the one whose skill is greater will win. The skill of the $i$-th player is equal to $s_i$ and all skill levels are pairwise different (i. e. there are no two identical values in the array $s$).
The tournament is called fair if the two players with the highest skills meet in the finals.
Determine whether the given tournament is fair.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
A single line of test case contains four integers $s_1, s_2, s_3, s_4$ ($1 \le s_i \le 100$) — skill of the players. It is guaranteed that all the numbers in the array are different.
-----Output-----
For each testcase, output YES if the tournament is fair, or NO otherwise.
-----Examples-----
Input
4
3 7 9 5
4 5 6 9
5 3 8 1
6 5 3 2
Output
YES
NO
YES
NO
-----Note-----
Consider the example:
in the first test case, players $2$ and $3$ with skills $7$ and $9$ advance to the finals;
in the second test case, players $2$ and $4$ with skills $5$ and $9$ advance to the finals. The player with skill $6$ does not advance, but the player with skill $5$ advances to the finals, so the tournament is not fair;
in the third test case, players $1$ and $3$ with skills $5$ and $8$ advance to the finals;
in the fourth test case, players $1$ and $3$ with skills $6$ and $3$ advance to the finals. The player with skill $5$ does not advance, but the player with skill $3$ advances to the finals, so the tournament is not fair.
|
{"inputs": ["1\n8 6 2 7\n", "1\n8 6 2 7\n", "1\n8 6 0 7\n", "1\n9 4 0 7\n", "1\n2 4 0 1\n", "1\n1 6 2 7\n", "1\n9 6 0 7\n", "1\n9 4 0 6\n"], "outputs": ["YES\n", "YES\n", "YES\n", "YES\n", "NO\n", "YES\n", "YES\n", "YES\n"]}
| 494
| 134
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given a 0-indexed 2D integer array grid of size m x n. Each cell has one of two values:
0 represents an empty cell,
1 represents an obstacle that may be removed.
You can move up, down, left, or right from and to an empty cell.
Return the minimum number of obstacles to remove so you can move from the upper left corner (0, 0) to the lower right corner (m - 1, n - 1).
Please complete the following python code precisely:
```python
class Solution:
def minimumObstacles(self, grid: List[List[int]]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(grid = [[0,1,1],[1,1,0],[1,1,0]]) == 2\n assert candidate(grid = [[0,1,0,0,0],[0,1,0,1,0],[0,0,0,1,0]]) == 0\n\n\ncheck(Solution().minimumObstacles)"}
| 150
| 88
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a table consisting of n rows and m columns. Each cell of the table contains a number, 0 or 1. In one move we can choose some row of the table and cyclically shift its values either one cell to the left, or one cell to the right.
To cyclically shift a table row one cell to the right means to move the value of each cell, except for the last one, to the right neighboring cell, and to move the value of the last cell to the first cell. A cyclical shift of a row to the left is performed similarly, but in the other direction. For example, if we cyclically shift a row "00110" one cell to the right, we get a row "00011", but if we shift a row "00110" one cell to the left, we get a row "01100".
Determine the minimum number of moves needed to make some table column consist only of numbers 1.
Input
The first line contains two space-separated integers: n (1 ≤ n ≤ 100) — the number of rows in the table and m (1 ≤ m ≤ 104) — the number of columns in the table. Then n lines follow, each of them contains m characters "0" or "1": the j-th character of the i-th line describes the contents of the cell in the i-th row and in the j-th column of the table.
It is guaranteed that the description of the table contains no other characters besides "0" and "1".
Output
Print a single number: the minimum number of moves needed to get only numbers 1 in some column of the table. If this is impossible, print -1.
Examples
Input
3 6
101010
000100
100000
Output
3
Input
2 3
111
000
Output
-1
Note
In the first sample one way to achieve the goal with the least number of moves is as follows: cyclically shift the second row to the right once, then shift the third row to the left twice. Then the table column before the last one will contain only 1s.
In the second sample one can't shift the rows to get a column containing only 1s.
|
{"inputs": ["1 1\n0\n", "1 1\n1\n", "3 1\n1\n1\n0\n", "3 1\n0\n0\n0\n", "3 1\n1\n2\n0\n", "2 3\n111\n010\n", "2 3\n110\n010\n", "2 3\n111\n000\n"], "outputs": ["-1", "0", "-1", "-1", "-1\n", "0\n", "0\n", "-1"]}
| 508
| 128
|
coding
|
Solve the programming task below in a Python markdown code block.
A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward. Examples of numerical palindromes are:
2332
110011
54322345
For a given number ```num```, return its closest numerical palindrome which can either be smaller or larger than ```num```. If there are 2 possible values, the larger value should be returned. If ```num``` is a numerical palindrome itself, return it.
For this kata, single digit numbers will NOT be considered numerical palindromes.
Also, you know the drill - be sure to return "Not valid" if the input is not an integer or is less than 0.
```
palindrome(8) => 11
palindrome(281) => 282
palindrome(1029) => 1001
palindrome(1221) => 1221
palindrome("1221") => "Not valid"
```
```Haskell
In Haskell the function should return a Maybe Int with Nothing for cases where the argument is less than zero.
```
Other Kata in this Series:
Numerical Palindrome #1
Numerical Palindrome #1.5
Numerical Palindrome #2
Numerical Palindrome #3
Numerical Palindrome #3.5
Numerical Palindrome #4
Numerical Palindrome #5
Also feel free to reuse/extend the following starter code:
```python
def palindrome(num):
```
|
{"functional": "_inputs = [[8], [281], [1029], [1221], ['BGHHGB'], ['11029'], [-1029]]\n_outputs = [[11], [282], [1001], [1221], ['Not valid'], ['Not valid'], ['Not valid']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(palindrome(*i), o[0])"}
| 344
| 220
|
coding
|
Solve the programming task below in a Python markdown code block.
Complete the function that accepts a valid string and returns an integer.
Wait, that would be too easy! Every character of the string should be converted to the hex value of its ascii code, then the result should be the sum of the numbers in the hex strings (ignore letters).
## Examples
```
"Yo" ==> "59 6f" ==> 5 + 9 + 6 = 20
"Hello, World!" ==> 91
"Forty4Three" ==> 113
```
Also feel free to reuse/extend the following starter code:
```python
def hex_hash(code):
```
|
{"functional": "_inputs = [['kcxnjsklsHskjHDkl7878hHJk'], [''], ['ThisIsATest!'], ['dhsajkbfyewquilb4y83q903ybr8q9apf7\\\\9ph79qw0-eq230br[wq87r0=18-[#20r370B 7Q0RFP23B79037902RF79WQ0[]]]']]\n_outputs = [[218], [0], [120], [802]]\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(hex_hash(*i), o[0])"}
| 146
| 279
|
coding
|
Solve the programming task below in a Python markdown code block.
Chef has an array of N natural numbers. Cheffina challenges the chef to choose the two numbers from the array and following the condition as the area of the rectangle formed from the two numbers is maximum. Cheffina also asks the chef to choose two numbers different from the previous two to form the rectangle with a minimum area.
-----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, $N$.
- N space-separated natural numbers.
-----Output:-----
For each test case, output in one line answers maximum and minimum area of a rectangle.
-----Constraints-----
- $1 \leq T \leq 10$
- $4 \leq N \leq 10^5$
- $1 \leq arr[i] \leq 10^6$
-----Sample Input:-----
1
5
4 2 1 5 3
-----Sample Output:-----
20 2
|
{"inputs": ["1\n5\n4 2 1 5 3"], "outputs": ["20 2"]}
| 228
| 27
|
coding
|
Solve the programming task below in a Python markdown code block.
AtCoDeer the deer has N square tiles. The tiles are numbered 1 through N, and the number given to each tile is written on one side of the tile. Also, each corner of each tile is painted in one of the 1000 colors, which are represented by the integers 0 between 999. The top-left, top-right, bottom-right and bottom-left corner of the tile with the number i are painted in color C_{i,0}, C_{i,1}, C_{i,2} and C_{i,3}, respectively, when seen in the direction of the number written on the tile (See Figure 1).
Figure 1: The correspondence between the colors of a tile and the input
AtCoDeer is constructing a cube using six of these tiles, under the following conditions:
- For each tile, the side with the number must face outward.
- For each vertex of the cube, the three corners of the tiles that forms it must all be painted in the same color.
Help him by finding the number of the different cubes that can be constructed under the conditions.
Since each tile has a number written on it, two cubes are considered different if the set of the used tiles are different, or the tiles are used in different directions, even if the formation of the colors are the same. (Each tile can be used in one of the four directions, obtained by 90° rotations.) Two cubes are considered the same only if rotating one in the three dimensional space can obtain an exact copy of the other, including the directions of the tiles.
Figure 2: The four directions of a tile
-----Constraints-----
- 6≦N≦400
- 0≦C_{i,j}≦999 (1≦i≦N , 0≦j≦3)
-----Input-----
The input is given from Standard Input in the following format:
N
C_{1,0} C_{1,1} C_{1,2} C_{1,3}
C_{2,0} C_{2,1} C_{2,2} C_{2,3}
:
C_{N,0} C_{N,1} C_{N,2} C_{N,3}
-----Output-----
Print the number of the different cubes that can be constructed under the conditions.
-----Sample Input-----
6
0 1 2 3
0 4 6 1
1 6 7 2
2 7 5 3
6 4 5 7
4 0 3 5
-----Sample Output-----
1
The cube below can be constructed.
|
{"inputs": ["6\n0 1 2 3\n0 4 6 1\n1 6 9 2\n2 7 5 3\n6 4 5 7\n4 0 3 5", "6\n0 0 0 0\n0 0 1 0\n0 0 0 0\n0 0 0 0\n0 0 0 1\n0 1 0 0", "6\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n1 0 0 0", "6\n0 1 2 3\n0 4 6 1\n1 6 9 2\n3 7 5 3\n6 4 5 7\n4 0 3 5", "6\n0 1 2 3\n0 4 6 1\n1 6 9 2\n3 7 5 3\n6 4 6 7\n4 0 3 5", "6\n0 1 2 3\n0 4 6 1\n1 6 7 2\n2 7 5 3\n6 4 5 7\n4 0 3 5", "6\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0", "6\n0 1 2 3\n0 4 6 1\n1 6 7 2\n2 7 5 3\n6 4 5 7\n4 0 3 5\n"], "outputs": ["0", "768", "0", "0", "0", "1", "122880", "1\n"]}
| 568
| 447
|
coding
|
Solve the programming task below in a Python markdown code block.
In this Kata, you will be given an array of strings and your task is to remove all consecutive duplicate letters from each string in the array.
For example:
* `dup(["abracadabra","allottee","assessee"]) = ["abracadabra","alote","asese"]`.
* `dup(["kelless","keenness"]) = ["keles","kenes"]`.
Strings will be lowercase only, no spaces. See test cases for more examples.
~~~if:rust
For the sake of simplicity you can use the macro 'vec_of_string' to create a Vec with an array of string literals.
~~~
Good luck!
If you like this Kata, please try:
[Alternate capitalization](https://www.codewars.com/kata/59cfc000aeb2844d16000075)
[Vowel consonant lexicon](https://www.codewars.com/kata/59cf8bed1a68b75ffb000026)
Also feel free to reuse/extend the following starter code:
```python
def dup(arry):
```
|
{"functional": "_inputs = [[['ccooddddddewwwaaaaarrrrsssss', 'piccaninny', 'hubbubbubboo']], [['abracadabra', 'allottee', 'assessee']], [['kelless', 'keenness']], [['Woolloomooloo', 'flooddoorroommoonlighters', 'chuchchi']], [['adanac', 'soonness', 'toolless', 'ppellee']], [['callalloo', 'feelless', 'heelless']], [['putteellinen', 'keenness']], [['kelless', 'voorraaddoosspullen', 'achcha']]]\n_outputs = [[['codewars', 'picaniny', 'hubububo']], [['abracadabra', 'alote', 'asese']], [['keles', 'kenes']], [['Wolomolo', 'flodoromonlighters', 'chuchchi']], [['adanac', 'sones', 'toles', 'pele']], [['calalo', 'feles', 'heles']], [['putelinen', 'kenes']], [['keles', 'voradospulen', 'achcha']]]\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(dup(*i), o[0])"}
| 257
| 387
|
coding
|
Solve the programming task below in a Python markdown code block.
Write a function that will check whether the permutation of an input string is a palindrome. Bonus points for a solution that is efficient and/or that uses _only_ built-in language functions. Deem yourself **brilliant** if you can come up with a version that does not use _any_ function whatsoever.
# Example
`madam` -> True
`adamm` -> True
`junk` -> False
## Hint
The brute force approach would be to generate _all_ the permutations of the string and check each one of them whether it is a palindrome. However, an optimized approach will not require this at all.
Also feel free to reuse/extend the following starter code:
```python
def permute_a_palindrome(stg):
```
|
{"functional": "_inputs = [['a'], ['aa'], ['baa'], ['aab'], ['baabcd'], ['racecars'], ['abcdefghba'], ['']]\n_outputs = [[True], [True], [True], [True], [False], [False], [False], [True]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(permute_a_palindrome(*i), o[0])"}
| 172
| 203
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a string $s$ of length $n$ consisting only of the characters 0 and 1.
You perform the following operation until the string becomes empty: choose some consecutive substring of equal characters, erase it from the string and glue the remaining two parts together (any of them can be empty) in the same order. For example, if you erase the substring 111 from the string 111110, you will get the string 110. When you delete a substring of length $l$, you get $a \cdot l + b$ points.
Your task is to calculate the maximum number of points that you can score in total, if you have to make the given string empty.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 2000$) — the number of testcases.
The first line of each testcase contains three integers $n$, $a$ and $b$ ($1 \le n \le 100; -100 \le a, b \le 100$) — the length of the string $s$ and the parameters $a$ and $b$.
The second line contains the string $s$. The string $s$ consists only of the characters 0 and 1.
-----Output-----
For each testcase, print a single integer — the maximum number of points that you can score.
-----Examples-----
Input
3
3 2 0
000
5 -2 5
11001
6 1 -4
100111
Output
6
15
-2
-----Note-----
In the first example, it is enough to delete the entire string, then we will get $2 \cdot 3 + 0 = 6$ points.
In the second example, if we delete characters one by one, then for each deleted character we will get $(-2) \cdot 1 + 5 = 3$ points, i. e. $15$ points in total.
In the third example, we can delete the substring 00 from the string 100111, we get $1 \cdot 2 + (-4) = -2$ points, and the string will be equal to 1111, removing it entirely we get $1 \cdot 4 + (-4) = 0$ points. In total, we got $-2$ points for $2$ operations.
|
{"inputs": ["3\n3 2 0\n000\n5 -2 5\n11001\n6 1 0\n100111\n", "3\n3 2 0\n000\n5 -1 5\n11001\n6 1 0\n100111\n", "3\n3 2 1\n000\n5 -2 5\n11001\n6 1 0\n110111\n", "3\n3 2 1\n010\n5 -2 4\n11000\n6 2 0\n100111\n", "3\n3 2 0\n000\n5 -2 5\n11001\n6 1 -4\n100111\n", "3\n3 2 0\n000\n5 -2 5\n11001\n6 1 -4\n110111\n", "3\n3 2 0\n000\n5 -2 5\n11001\n6 1 -7\n110111\n", "3\n3 2 0\n000\n5 -4 5\n11001\n6 1 -4\n110111\n"], "outputs": ["6\n15\n6\n", "6\n20\n6\n", "9\n15\n6\n", "9\n10\n12\n", "6\n15\n-2\n", "6\n15\n-2\n", "6\n15\n-8\n", "6\n5\n-2\n"]}
| 542
| 394
|
coding
|
Solve the programming task below in a Python markdown code block.
Sum all the numbers of the array (in F# and Haskell you get a list) except the highest and the lowest element (the value, not the index!).
(The highest/lowest element is respectively only one element at each edge, even if there are more than one with the same value!)
Example:
```
{ 6, 2, 1, 8, 10 } => 16
{ 1, 1, 11, 2, 3 } => 6
```
If array is empty, null or None, or if only 1 Element exists, return 0.
Note:In C++ instead null an empty vector is used. In C there is no null. ;-)
Have fun coding it and please don't forget to vote and rank this kata! :-)
I have created other katas. Have a look if you like coding and challenges.
Also feel free to reuse/extend the following starter code:
```python
def sum_array(arr):
```
|
{"functional": "_inputs = [[None], [[]], [[3]], [[-3]], [[3, 5]], [[-3, -5]], [[6, 2, 1, 8, 10]], [[6, 0, 1, 10, 10]], [[-6, -20, -1, -10, -12]], [[-6, 20, -1, 10, -12]]]\n_outputs = [[0], [0], [0], [0], [0], [0], [16], [17], [-28], [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(sum_array(*i), o[0])"}
| 221
| 277
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.
The *greatest common divisor* (GCD) of a sequence is the greatest positive integer which divides each element of this sequence.
You are given a sequence $A$ of positive integers with size $N$. You are allowed to delete up to $N-1$ elements from this sequence. (I.e., you may delete any number of elements, including zero, as long as the resulting sequence is non-empty.)
Please find the minimum number of elements which have to be deleted so that the GCD of the resulting sequence would be equal to $1$, or determine that it is impossible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \dots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer — the minimum number of elements to delete, or $-1$ if it is impossible to make the GCD equal to $1$.
------ Constraints ------
$1 ≤ T ≤ 100$
$2 ≤ N ≤ 1,000$
$1 ≤ A_{i} ≤ 50,000$ for each valid $i$
------ Subtasks ------
Subtask #1 (20 points): $2 ≤ N ≤ 3$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
2
2
2 3
2
2 4
----- Sample Output 1 ------
0
-1
----- explanation 1 ------
Example case 1: The GCD is already equal to $1$, so the answer is $0$.
Example case 2: It is impossible to make the GCD equal to $1$, so the answer is $-1$.
|
{"inputs": ["2\n2\n2 3\n2\n2 4"], "outputs": ["0\n-1"]}
| 445
| 27
|
coding
|
Solve the programming task below in a Python markdown code block.
There are a total of W x H squares, with H rows vertically and W columns horizontally. Some squares are marked. Create a program that reads the marked state of each square and outputs the maximum rectangular area consisting of only the unmarked squares.
The input data consists of one line of W characters, given H lines. For example, the following data is given.
.. *.... **.
..........
** .... ***.
.... * .....
.. * .......
... ** .....
. *. * ......
..........
.. **......
. * .. * .....
One line of input data represents the square of one line. Of the character strings in the input data,. (Period) indicates unmarked squares, and * (asterisk) indicates marked squares. The input data string does not contain any characters other than periods, asterisks, and line breaks.
In the above example, the rectangle indicated by 0 in the figure below is the largest.
.. *.... **.
..........
** .... ***.
.... * 00000
.. * ..00000
... ** 00000
. *. *. 00000
..... 00000
.. **. 00000
. * .. * 00000
Therefore, if you output 35, the answer will be correct. If all the squares are marked, output 0.
Input
Given multiple datasets. Each dataset starts with a line of H and W separated by spaces, followed by an H x W rectangle. Both H and W shall be 500 or less.
The input ends with a line containing two 0s. The number of datasets does not exceed 20.
Output
For each dataset, output the area of the largest rectangle on one line.
Example
Input
10 10
...*....**
..........
**....**..
........*.
..*.......
**........
.*........
..........
....*..***
.*....*...
10 10
..*....*..
.*.*...*..
*****..*..
*...*..*..
*...*..*..
..........
****.*...*
..*..*...*
.*...*...*
****..***.
2 3
...
...
0 0
Output
28
12
6
|
{"inputs": ["10 7\n...*....**\n..........\n**....**..\n........*.\n..*.......\n**........\n.*........\n..........\n....*..***\n...*....*.\n10 4\n..*....*..\n.*.*...*..\n..*..*****\n*...*..*..\n..*..*...*\n..........\n.***.*..**\n..*..*...*\n..*..*...*\n****...***\n2 2\n...\n...\n0 0", "10 6\n...*....**\n..........\n**....**..\n........*.\n..*.......\n**........\n.*........\n..........\n....*..***\n.*....*...\n10 5\n..*....*..\n.*.*...*..\n*****..*..\n*.*.*.....\n*...*..*..\n..........\n.**..**.**\n..*..*...*\n.*...*...*\n****..***.\n2 3\n...\n...\n0 0", "10 4\n**....*...\n..........\n**....**..\n........*.\n..*.......\n**........\n.*........\n..........\n....*..***\n...*....*.\n10 5\n..*....*..\n.*.*...*..\n*****..*..\n*.*.*.....\n*...*..*..\n..........\n.***.*..**\n..*..*...*\n*...*...*.\n****..***.\n2 3\n...\n...\n0 0", "10 6\n...*....**\n..........\n**....**..\n........*.\n..*.......\n**........\n.*........\n..........\n....*..***\n.*....*...\n10 10\n..*....*..\n.*.*...*..\n*****..*..\n*.*.*.....\n*...*..*..\n..........\n.**..**.**\n..*..*...*\n.*...*...*\n****..***.\n2 3\n...\n...\n0 0", "10 10\n...*....**\n..........\n..**....**\n........*.\n..*.......\n**........\n.*........\n..........\n....*..***\n.*....*...\n10 5\n....*..*..\n.*.*...*..\n*****..*..\n*.*.*.....\n*...*..*..\n..........\n.***.*..**\n..*..*...*\n.*...*...*\n****..***.\n2 3\n...\n...\n0 0", "10 10\n...*....**\n..........\n**.*...*..\n........*.\n.......*..\n**........\n.*........\n..........\n....*..***\n.*....*...\n10 3\n..*....*..\n.*.*...*..\n*****..*..\n*...*..*..\n*...*..*..\n..........\n****.*...*\n..*..*...*\n.*...*...*\n**.*..****\n2 3\n...\n...\n0 0", "10 10\n...*....**\n..........\n**....**..\n........*.\n..*.......\n**........\n.*........\n..........\n....*..***\n.*....*...\n10 4\n..*....*..\n.*.*...*..\n..*..*****\n*...*..*..\n..*..*...*\n..........\n.***.*..**\n..*..*...*\n.*...*...*\n****...***\n2 3\n...\n...\n0 0", "10 4\n**....*...\n..........\n**....**..\n........*.\n..*.......\n**........\n.*........\n..........\n....*..***\n...*....*.\n10 10\n..*....*..\n.*.*...*..\n*****..*..\n*.*.*.....\n*...*..*..\n..........\n.***.*..**\n..*..*...*\n*...*...*.\n****..***.\n2 3\n...\n...\n0 0"], "outputs": ["21\n5\n4\n", "21\n8\n6\n", "9\n6\n6\n", "21\n12\n6\n", "32\n6\n6\n", "25\n6\n6\n", "28\n5\n6\n", "9\n12\n6\n"]}
| 504
| 1,102
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
With respect to a given puzzle string, a word is valid if both the following conditions are satisfied:
word contains the first letter of puzzle.
For each letter in word, that letter is in puzzle.
For example, if the puzzle is "abcdefg", then valid words are "faced", "cabbage", and "baggage", while
invalid words are "beefed" (does not include 'a') and "based" (includes 's' which is not in the puzzle).
Return an array answer, where answer[i] is the number of words in the given word list words that is valid with respect to the puzzle puzzles[i].
Please complete the following python code precisely:
```python
class Solution:
def findNumOfValidWords(self, words: List[str], puzzles: List[str]) -> List[int]:
```
|
{"functional": "def check(candidate):\n assert candidate(\nwords = [\"aaaa\",\"asas\",\"able\",\"ability\",\"actt\",\"actor\",\"access\"], \npuzzles = [\"aboveyz\",\"abrodyz\",\"abslute\",\"absoryz\",\"actresz\",\"gaswxyz\"]) == [1,1,3,2,4,0]\n\n\ncheck(Solution().findNumOfValidWords)"}
| 190
| 95
|
coding
|
Solve the programming task below in a Python markdown code block.
Sereja has a bracket sequence s_1, s_2, ..., s_{n}, or, in other words, a string s of length n, consisting of characters "(" and ")".
Sereja needs to answer m queries, each of them is described by two integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). The answer to the i-th query is the length of the maximum correct bracket subsequence of sequence s_{l}_{i}, s_{l}_{i} + 1, ..., s_{r}_{i}. Help Sereja answer all queries.
You can find the definitions for a subsequence and a correct bracket sequence in the notes.
-----Input-----
The first line contains a sequence of characters s_1, s_2, ..., s_{n} (1 ≤ n ≤ 10^6) without any spaces. Each character is either a "(" or a ")". The second line contains integer m (1 ≤ m ≤ 10^5) — the number of queries. Each of the next m lines contains a pair of integers. The i-th line contains integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the description of the i-th query.
-----Output-----
Print the answer to each question on a single line. Print the answers in the order they go in the input.
-----Examples-----
Input
())(())(())(
7
1 1
2 3
1 2
1 12
8 12
5 11
2 10
Output
0
0
2
10
4
6
6
-----Note-----
A subsequence of length |x| of string s = s_1s_2... s_{|}s| (where |s| is the length of string s) is string x = s_{k}_1s_{k}_2... s_{k}_{|}x| (1 ≤ k_1 < k_2 < ... < k_{|}x| ≤ |s|).
A correct bracket sequence is a bracket sequence that can be transformed into a correct aryphmetic expression by inserting characters "1" and "+" between the characters of the string. For example, bracket sequences "()()", "(())" are correct (the resulting expressions "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not.
For the third query required sequence will be «()».
For the fourth query required sequence will be «()(())(())».
|
{"inputs": ["(\n1\n1 1\n", ")\n1\n1 1\n", "(\n1\n1 1\n", ")\n1\n1 1\n", "()\n1\n1 2\n", ")(\n1\n1 2\n", ")(\n1\n1 2\n", "()\n1\n1 2\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "2\n", "0\n", "0\n", "2\n"]}
| 556
| 120
|
coding
|
Solve the programming task below in a Python markdown code block.
Alice and Bonnie are sisters, but they don't like each other very much. So when some old family photos were found in the attic, they started to argue about who should receive which photos. In the end, they decided that they would take turns picking photos. Alice goes first.
There are n stacks of photos. Each stack contains exactly two photos. In each turn, a player may take only a photo from the top of one of the stacks.
Each photo is described by two non-negative integers a and b, indicating that it is worth a units of happiness to Alice and b units of happiness to Bonnie. Values of a and b might differ for different photos.
It's allowed to pass instead of taking a photo. The game ends when all photos are taken or both players pass consecutively.
The players don't act to maximize their own happiness. Instead, each player acts to maximize the amount by which her happiness exceeds her sister's. Assuming both players play optimal, find the difference between Alice's and Bonnie's happiness. That is, if there's a perfectly-played game such that Alice has x happiness and Bonnie has y happiness at the end, you should print x - y.
Input
The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the number of two-photo stacks. Then follow n lines, each describing one of the stacks. A stack is described by four space-separated non-negative integers a1, b1, a2 and b2, each not exceeding 109. a1 and b1 describe the top photo in the stack, while a2 and b2 describe the bottom photo in the stack.
Output
Output a single integer: the difference between Alice's and Bonnie's happiness if both play optimally.
Examples
Input
2
12 3 4 7
1 15 9 1
Output
1
Input
2
5 4 8 8
4 12 14 0
Output
4
Input
1
0 10 0 10
Output
-10
|
{"inputs": ["1\n0 0 0 0\n", "1\n0 0 -1 0\n", "1\n0 10 0 19\n", "1\n0 10 1 11\n", "1\n0 -1 -1 0\n", "1\n0 10 0 11\n", "1\n0 -1 -1 1\n", "1\n0 10 0 10\n"], "outputs": ["0", "0\n", "-10\n", "-9\n", "0\n", "-10\n", "0\n", "-10"]}
| 452
| 143
|
coding
|
Solve the programming task below in a Python markdown code block.
Snuke has N dogs and M monkeys. He wants them to line up in a row.
As a Japanese saying goes, these dogs and monkeys are on bad terms. ("ken'en no naka", literally "the relationship of dogs and monkeys", means a relationship of mutual hatred.) Snuke is trying to reconsile them, by arranging the animals so that there are neither two adjacent dogs nor two adjacent monkeys.
How many such arrangements there are? Find the count modulo 10^9+7 (since animals cannot understand numbers larger than that).
Here, dogs and monkeys are both distinguishable. Also, two arrangements that result from reversing each other are distinguished.
-----Constraints-----
- 1 ≤ N,M ≤ 10^5
-----Input-----
Input is given from Standard Input in the following format:
N M
-----Output-----
Print the number of possible arrangements, modulo 10^9+7.
-----Sample Input-----
2 2
-----Sample Output-----
8
We will denote the dogs by A and B, and the monkeys by C and D. There are eight possible arrangements: ACBD, ADBC, BCAD, BDAC, CADB, CBDA, DACB and DBCA.
|
{"inputs": ["2 2\n", "3 2\n", "1 8\n", "100000 100000\n"], "outputs": ["8\n", "12\n", "0\n", "530123477\n"]}
| 265
| 65
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given a binary array nums and an integer goal, return the number of non-empty subarrays with a sum goal.
A subarray is a contiguous part of the array.
Please complete the following python code precisely:
```python
class Solution:
def numSubarraysWithSum(self, nums: List[int], goal: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(nums = [1,0,1,0,1], goal = 2) == 4\n assert candidate(nums = [0,0,0,0,0], goal = 0) == 15\n\n\ncheck(Solution().numSubarraysWithSum)"}
| 89
| 73
|
coding
|
Solve the programming task below in a Python markdown code block.
A positive integer is called a 2-3-integer, if it is equal to 2^{x}·3^{y} for some non-negative integers x and y. In other words, these integers are such integers that only have 2 and 3 among their prime divisors. For example, integers 1, 6, 9, 16 and 108 — are 2-3 integers, while 5, 10, 21 and 120 are not.
Print the number of 2-3-integers on the given segment [l, r], i. e. the number of sich 2-3-integers t that l ≤ t ≤ r.
-----Input-----
The only line contains two integers l and r (1 ≤ l ≤ r ≤ 2·10^9).
-----Output-----
Print a single integer the number of 2-3-integers on the segment [l, r].
-----Examples-----
Input
1 10
Output
7
Input
100 200
Output
5
Input
1 2000000000
Output
326
-----Note-----
In the first example the 2-3-integers are 1, 2, 3, 4, 6, 8 and 9.
In the second example the 2-3-integers are 108, 128, 144, 162 and 192.
|
{"inputs": ["1 1\n", "2 2\n", "2 2\n", "1 1\n", "1 2\n", "1 10\n", "2 10\n", "1 10\n"], "outputs": ["1\n", "1\n", "1\n", "1\n", "2\n", "7\n", "6\n", "7\n"]}
| 337
| 89
|
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 with unique values and the values of two different nodes of the tree x and y, return true if the nodes corresponding to the values x and y in the tree are cousins, or false otherwise.
Two nodes of a binary tree are cousins if they have the same depth with different parents.
Note that in a binary tree, the root node is at the depth 0, and children of each depth k node are at the depth k + 1.
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 isCousins(self, root: Optional[TreeNode], x: int, y: int) -> bool:
```
|
{"functional": "def check(candidate):\n assert candidate(root = tree_node([1,2,3,4]), x = 4, y = 3) == False\n assert candidate(root = tree_node([1,2,3,None,4,None,5]), x = 5, y = 4) == True\n assert candidate(root = tree_node([1,2,3,None,4]), x = 2, y = 3) == False\n\n\ncheck(Solution().isCousins)"}
| 207
| 113
|
coding
|
Solve the programming task below in a Python markdown code block.
Create a function that takes in the sum and age difference of two people, calculates their individual ages, and returns a pair of values (oldest age first) if those exist or `null/None` if:
* `sum < 0`
* `difference < 0`
* Either of the calculated ages come out to be negative
Also feel free to reuse/extend the following starter code:
```python
def get_ages(sum_, difference):
```
|
{"functional": "_inputs = [[24, 4], [30, 6], [70, 10], [18, 4], [63, 14], [80, 80], [63, -14], [-22, 15]]\n_outputs = [[[14, 10]], [[18, 12]], [[40, 30]], [[11, 7]], [[38.5, 24.5]], [[80, 0]], [None], [None]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(get_ages(*i), o[0])"}
| 106
| 266
|
coding
|
Solve the programming task below in a Python markdown code block.
A binary string is a string that consists of characters $0$ and $1$.
Let $\operatorname{MEX}$ of a binary string be the smallest digit among $0$, $1$, or $2$ that does not occur in the string. For example, $\operatorname{MEX}$ of $001011$ is $2$, because $0$ and $1$ occur in the string at least once, $\operatorname{MEX}$ of $1111$ is $0$, because $0$ and $2$ do not occur in the string and $0 < 2$.
A binary string $s$ is given. You should cut it into any number of substrings such that each character is in exactly one substring. It is possible to cut the string into a single substring — the whole string.
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
What is the minimal sum of $\operatorname{MEX}$ of all substrings pieces can be?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Description of the test cases follows.
Each test case contains a single binary string $s$ ($1 \le |s| \le 10^5$).
It's guaranteed that the sum of lengths of $s$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print a single integer — the minimal sum of $\operatorname{MEX}$ of all substrings that it is possible to get by cutting $s$ optimally.
-----Examples-----
Input
6
01
1111
01100
101
0000
01010
Output
1
0
2
1
1
2
-----Note-----
In the first test case the minimal sum is $\operatorname{MEX}(0) + \operatorname{MEX}(1) = 1 + 0 = 1$.
In the second test case the minimal sum is $\operatorname{MEX}(1111) = 0$.
In the third test case the minimal sum is $\operatorname{MEX}(01100) = 2$.
|
{"inputs": ["6\n1\n1001\n01100\n111\n0000\n01010\n", "6\n1\n1001\n01100\n101\n0000\n01010\n", "6\n1\n1111\n01100\n101\n1000\n11010\n", "6\n1\n1010\n11100\n100\n0000\n01010\n", "6\n0\n1011\n01100\n010\n0000\n01010\n", "6\n0\n1001\n01100\n110\n0010\n01010\n", "6\n0\n1001\n01100\n111\n0010\n01010\n", "6\n1\n0010\n01100\n100\n0000\n01010\n"], "outputs": ["0\n1\n2\n0\n1\n2\n", "0\n1\n2\n1\n1\n2\n", "0\n0\n2\n1\n1\n2\n", "0\n2\n1\n1\n1\n2\n", "1\n1\n2\n2\n1\n2\n", "1\n1\n2\n1\n2\n2\n", "1\n1\n2\n0\n2\n2\n", "0\n2\n2\n1\n1\n2\n"]}
| 552
| 374
|
coding
|
Solve the programming task below in a Python markdown code block.
Given two non-negative decimal integers $a$ and $b$, calculate their AND (logical conjunction), OR (logical disjunction) and XOR (exclusive disjunction) and print them in binary representation of 32 bits.
Constraints
* $0 \leq a, b \leq 2^{32} - 1$
Input
The input is given in the following format.
$a \; b$
Output
Print results of AND, OR and XOR in a line respectively.
Example
Input
8 10
Output
00000000000000000000000000001000
00000000000000000000000000001010
00000000000000000000000000000010
|
{"inputs": ["1 1", "2 1", "2 0", "1 0", "0 0", "1 3", "2 3", "4 0"], "outputs": ["00000000000000000000000000000001\n00000000000000000000000000000001\n00000000000000000000000000000000\n", "00000000000000000000000000000000\n00000000000000000000000000000011\n00000000000000000000000000000011\n", "00000000000000000000000000000000\n00000000000000000000000000000010\n00000000000000000000000000000010\n", "00000000000000000000000000000000\n00000000000000000000000000000001\n00000000000000000000000000000001\n", "00000000000000000000000000000000\n00000000000000000000000000000000\n00000000000000000000000000000000\n", "00000000000000000000000000000001\n00000000000000000000000000000011\n00000000000000000000000000000010\n", "00000000000000000000000000000010\n00000000000000000000000000000011\n00000000000000000000000000000001\n", "00000000000000000000000000000000\n00000000000000000000000000000100\n00000000000000000000000000000100\n"]}
| 223
| 854
|
coding
|
Solve the programming task below in a Python markdown code block.
You will be given a string and you task is to check if it is possible to convert that string into a palindrome by removing a single character. If the string is already a palindrome, return `"OK"`. If it is not, and we can convert it to a palindrome by removing one character, then return `"remove one"`, otherwise return `"not possible"`. The order of the characters should not be changed.
For example:
```Haskell
solve("abba") = "OK". -- This is a palindrome
solve("abbaa") = "remove one". -- remove the 'a' at the extreme right.
solve("abbaab") = "not possible".
```
More examples in the test cases.
Good luck!
If you like this Kata, please try [Single Character Palindromes II](https://www.codewars.com/kata/5a66ea69e6be38219f000110)
Also feel free to reuse/extend the following starter code:
```python
def solve(s):
```
|
{"functional": "_inputs = [['abba'], ['abbaa'], ['abbaab'], ['madmam'], ['raydarm'], ['hannah'], ['baba'], ['babab'], ['bababa'], ['abcbad'], ['abcdba'], ['dabcba'], ['abededba'], ['abdcdeba'], ['abcdedba'], ['abbcdedba']]\n_outputs = [['OK'], ['remove one'], ['not possible'], ['remove one'], ['not possible'], ['OK'], ['remove one'], ['OK'], ['remove one'], ['remove one'], ['remove one'], ['remove one'], ['remove one'], ['remove one'], ['remove one'], ['not possible']]\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])"}
| 237
| 285
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given an integer array nums and two integers left and right, return the number of contiguous non-empty subarrays such that the value of the maximum array element in that subarray is in the range [left, right].
The test cases are generated so that the answer will fit in a 32-bit integer.
Please complete the following python code precisely:
```python
class Solution:
def numSubarrayBoundedMax(self, nums: List[int], left: int, right: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(nums = [2,1,4,3], left = 2, right = 3) == 3\n assert candidate(nums = [2,9,2,5,6], left = 2, right = 8) == 7\n\n\ncheck(Solution().numSubarrayBoundedMax)"}
| 122
| 81
|
coding
|
Solve the programming task below in a Python markdown code block.
Kuznecov likes art, poetry, and music. And strings consisting of lowercase English letters.
Recently, Kuznecov has found two strings, $a$ and $b$, of lengths $n$ and $m$ respectively. They consist of lowercase English letters and no character is contained in both strings.
Let another string $c$ be initially empty. Kuznecov can do the following two types of operations:
Choose any character from the string $a$, remove it from $a$, and add it to the end of $c$.
Choose any character from the string $b$, remove it from $b$, and add it to the end of $c$.
But, he can not do more than $k$ operations of the same type in a row. He must perform operations until either $a$ or $b$ becomes empty. What is the lexicographically smallest possible value of $c$ after he finishes?
A string $x$ is lexicographically smaller than a string $y$ if and only if one of the following holds:
$x$ is a prefix of $y$, but $x \neq y$;
in the first position where $x$ and $y$ differ, the string $x$ has a letter that appears earlier in the alphabet than the corresponding letter in $y$.
-----Input-----
There are several test cases in the input data. The first line contains a single integer $t$ ($1 \leq t \leq 100$) — the number of test cases. This is followed by the test cases description.
The first line of each test case contains three integers $n$, $m$, and $k$ ($1\leq n,m,k \leq 100$) — parameters from the statement.
The second line of each test case contains the string $a$ of length $n$.
The third line of each test case contains the string $b$ of length $m$.
The strings contain only lowercase English letters. It is guaranteed that no symbol appears in $a$ and $b$ simultaneously.
-----Output-----
In each test case, output a single string $c$ — the answer to the problem.
-----Examples-----
Input
3
6 4 2
aaaaaa
bbbb
5 9 3
caaca
bedededeb
7 7 1
noskill
wxhtzdy
Output
aabaabaa
aaabbcc
dihktlwlxnyoz
-----Note-----
In the first test case, it is optimal to take two 'a's from the string $a$ and add them to the string $c$. Then it is forbidden to take more characters from $a$, hence one character 'b' from the string $b$ has to be taken. Following that logic, we end up with $c$ being 'aabaabaa' when string $a$ is emptied.
In the second test case it is optimal to take as many 'a's from string $a$ as possible, then take as many 'b's as possible from string $b$. In the end, we take two 'c's from the string $a$ emptying it.
|
{"inputs": ["1\n5 1 5\naaaaa\nx\n", "3\n6 4 2\naaaaaa\nbbbb\n5 9 3\ncaaca\nbedededeb\n7 7 1\nnoskill\nwxhtzdy\n"], "outputs": ["aaaaa\n", "aabaabaa\naaabbcc\ndihktlwlxnyoz\n"]}
| 681
| 88
|
coding
|
Solve the programming task below in a Python markdown code block.
We have a grid of squares with N rows and M columns. Let (i, j) denote the square at the i-th row from the top and j-th column from the left. We will choose K of the squares and put a piece on each of them.
If we place the K pieces on squares (x_1, y_1), (x_2, y_2), ..., and (x_K, y_K), the cost of this arrangement is computed as:
\sum_{i=1}^{K-1} \sum_{j=i+1}^K (|x_i - x_j| + |y_i - y_j|)
Find the sum of the costs of all possible arrangements of the pieces. Since this value can be tremendous, print it modulo 10^9+7.
We consider two arrangements of the pieces different if and only if there is a square that contains a piece in one of the arrangements but not in the other.
-----Constraints-----
- 2 \leq N \times M \leq 2 \times 10^5
- 2 \leq K \leq N \times M
- All values in input are integers.
-----Input-----
Input is given from Standard Input in the following format:
N M K
-----Output-----
Print the sum of the costs of all possible arrangements of the pieces, modulo 10^9+7.
-----Sample Input-----
2 2 2
-----Sample Output-----
8
There are six possible arrangements of the pieces, as follows:
- ((1,1),(1,2)), with the cost |1-1|+|1-2| = 1
- ((1,1),(2,1)), with the cost |1-2|+|1-1| = 1
- ((1,1),(2,2)), with the cost |1-2|+|1-2| = 2
- ((1,2),(2,1)), with the cost |1-2|+|2-1| = 2
- ((1,2),(2,2)), with the cost |1-2|+|2-2| = 1
- ((2,1),(2,2)), with the cost |2-2|+|1-2| = 1
The sum of these costs is 8.
|
{"inputs": ["4 2 2", "4 5 7", "2 3 2", "4 6 7", "2 3 3", "2 4 3", "1 4 3", "1 4 4"], "outputs": ["56\n", "4883760\n", "25\n", "24227280\n", "100\n", "336\n", "20\n", "10\n"]}
| 510
| 115
|
coding
|
Solve the programming task below in a Python markdown code block.
# Scenario
*You're saying good-bye your best friend* , **_See you next happy year_** .
**_Happy Year_** *is the year with only distinct digits* , (e.g) **_2018_**
___
# Task
**_Given_** a year, **_Find_** **_The next happy year_** or **_The closest year You'll see your best friend_**  
___
# Notes
* **_Year_** Of Course always **_Positive_** .
* **_Have no fear_** , *It is guaranteed that the answer exists* .
* **_It's not necessary_** *that the year passed to the function is Happy one* .
* **_Input Year with in range_** *(1000 ≤ y ≤ 9000)*
____
# Input >> Output Examples:
```
nextHappyYear (7712) ==> return (7801)
```
## **_Explanation_**:
As the **_Next closest year with only distinct digits is_** *7801* .
___
```
nextHappyYear (8989) ==> return (9012)
```
## **_Explanation_**:
As the **_Next closest year with only distinct digits is_** *9012* .
___
```
nextHappyYear (1001) ==> return (1023)
```
## **_Explanation_**:
As the **_Next closest year with only distinct digits is_** *1023* .
___
___
___
# [Playing with Numbers Series](https://www.codewars.com/collections/playing-with-numbers)
# [Playing With Lists/Arrays Series](https://www.codewars.com/collections/playing-with-lists-slash-arrays)
# [For More Enjoyable Katas](http://www.codewars.com/users/MrZizoScream/authored)
___
## ALL translations are welcomed
## Enjoy Learning !!
# Zizou
Also feel free to reuse/extend the following starter code:
```python
def next_happy_year(year):
```
|
{"functional": "_inputs = [[1001], [1123], [2001], [2334], [3331], [1987], [5555], [7712], [8088], [8999]]\n_outputs = [[1023], [1203], [2013], [2340], [3401], [2013], [5601], [7801], [8091], [9012]]\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(next_happy_year(*i), o[0])"}
| 519
| 270
|
coding
|
Solve the programming task below in a Python markdown code block.
There is a card game called "Durak", which means "Fool" in Russian. The game is quite popular in the countries that used to form USSR. The problem does not state all the game's rules explicitly — you can find them later yourselves if you want.
To play durak you need a pack of 36 cards. Each card has a suit ("S", "H", "D" and "C") and a rank (in the increasing order "6", "7", "8", "9", "T", "J", "Q", "K" and "A"). At the beginning of the game one suit is arbitrarily chosen as trump.
The players move like that: one player puts one or several of his cards on the table and the other one should beat each of them with his cards.
A card beats another one if both cards have similar suits and the first card has a higher rank then the second one. Besides, a trump card can beat any non-trump card whatever the cards’ ranks are. In all other cases you can not beat the second card with the first one.
You are given the trump suit and two different cards. Determine whether the first one beats the second one or not.
Input
The first line contains the tramp suit. It is "S", "H", "D" or "C".
The second line contains the description of the two different cards. Each card is described by one word consisting of two symbols. The first symbol stands for the rank ("6", "7", "8", "9", "T", "J", "Q", "K" and "A"), and the second one stands for the suit ("S", "H", "D" and "C").
Output
Print "YES" (without the quotes) if the first cards beats the second one. Otherwise, print "NO" (also without the quotes).
Examples
Input
H
QH 9S
Output
YES
Input
S
8D 6D
Output
YES
Input
C
7H AS
Output
NO
|
{"inputs": ["H\nQD KD\n", "D\n9S 8S\n", "H\nTH 8H\n", "D\nAC KS\n", "H\nAH KH\n", "H\nAD KD\n", "D\n8S 6C\n", "H\nAC QC\n"], "outputs": ["NO\n", "YES\n", "YES\n", "NO\n", "YES\n", "YES\n", "NO\n", "YES\n"]}
| 443
| 103
|
coding
|
Solve the programming task below in a Python markdown code block.
Given a string containing a list of integers separated by commas, write the function string_to_int_list(s) that takes said string and returns a new list containing all integers present in the string, preserving the order.
For example, give the string "-1,2,3,4,5", the function string_to_int_list() should return [-1,2,3,4,5]
Please note that there can be one or more consecutive commas whithout numbers, like so: "-1,-2,,,,,,3,4,5,,6"
Also feel free to reuse/extend the following starter code:
```python
def string_to_int_list(s):
```
|
{"functional": "_inputs = [['1,2,3,4,5'], ['21,12,23,34,45'], ['-1,-2,3,-4,-5'], ['1,2,3,,,4,,5,,,'], [',,,,,1,2,3,,,4,,5,,,'], [''], [',,,,,,,,']]\n_outputs = [[[1, 2, 3, 4, 5]], [[21, 12, 23, 34, 45]], [[-1, -2, 3, -4, -5]], [[1, 2, 3, 4, 5]], [[1, 2, 3, 4, 5]], [[]], [[]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(string_to_int_list(*i), o[0])"}
| 148
| 306
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given N integers; the i-th of them is A_i. Find the maximum possible sum of the absolute differences between the adjacent elements after arranging these integers in a row in any order you like.
Constraints
* 2 \leq N \leq 10^5
* 1 \leq A_i \leq 10^9
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1
:
A_N
Output
Print the maximum possible sum of the absolute differences between the adjacent elements after arranging the given integers in a row in any order you like.
Examples
Input
5
6
8
1
2
3
Output
21
Input
6
3
1
4
1
5
9
Output
25
Input
3
5
5
1
Output
8
|
{"inputs": ["3\n5\n6\n1", "3\n5\n3\n1", "3\n0\n3\n1", "3\n0\n0\n1", "3\n0\n0\n0", "3\n5\n5\n1", "3\n-1\n0\n1", "3\n-1\n1\n1"], "outputs": ["9\n", "6\n", "5\n", "2\n", "0\n", "8", "3\n", "4\n"]}
| 204
| 111
|
coding
|
Solve the programming task below in a Python markdown code block.
Your task is to write a function named `do_math` that receives a single argument.
This argument is a string that contains multiple whitespace delimited numbers. Each number has a single alphabet letter somewhere within it.
```
Example : "24z6 1x23 y369 89a 900b"
```
As shown above, this alphabet letter can appear anywhere within the number. You have to extract the letters and sort the numbers according to their corresponding letters.
```
Example : "24z6 1x23 y369 89a 900b" will become 89 900 123 369 246 (ordered according to the alphabet letter)
```
Here comes the difficult part, now you have to do a series of computations on the numbers you have extracted.
* The sequence of computations are `+ - * /`. Basic math rules do **NOT** apply, you have to do each computation in exactly this order.
* This has to work for any size of numbers sent in (after division, go back to addition, etc).
* In the case of duplicate alphabet letters, you have to arrange them according to the number that appeared first in the input string.
* Remember to also round the final answer to the nearest integer.
```
Examples :
"24z6 1x23 y369 89a 900b" = 89 + 900 - 123 * 369 / 246 = 1299
"24z6 1z23 y369 89z 900b" = 900 + 369 - 246 * 123 / 89 = 1414
"10a 90x 14b 78u 45a 7b 34y" = 10 + 45 - 14 * 7 / 78 + 90 - 34 = 60
```
Good luck and may the CODE be with you!
Also feel free to reuse/extend the following starter code:
```python
def do_math(s) :
```
|
{"functional": "_inputs = [['24z6 1z23 y369 89z 900b'], ['24z6 1x23 y369 89a 900b'], ['10a 90x 14b 78u 45a 7b 34y'], ['111a 222c 444y 777u 999a 888p'], ['1z 2t 3q 5x 6u 8a 7b']]\n_outputs = [[1414], [1299], [60], [1459], [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(do_math(*i), o[0])"}
| 499
| 302
|
coding
|
Solve the programming task below in a Python markdown code block.
**DESCRIPTION:**
Your strict math teacher is teaching you about right triangles, and the Pythagorean Theorem --> a^2 + b^2 = c^2 whereas a and b are the legs of the right triangle and c is the hypotenuse of the right triangle. On the test however, the question asks: What are the possible integer lengths for the other side of the triangle, but since you never learned anything about that in class, you realize she meant What are the possible integer lengths for the other side of the right triangle. Because you want to address the fact that she asked the wrong question and the fact that you're smart at math, you've decided to answer all the possible values for the third side EXCLUDING the possibilities for a right triangle in increasing order.
**EXAMPLES:**
```
side_len(1, 1) --> [1]
side_len(3, 4) --> [2, 3, 4, 6]
side_len(4, 6) --> [3, 4, 5, 6, 7, 8, 9]
```
**RETURN:**
Return your answer as a list of all the possible third side lengths of the triangle without the right triangles in increasing order.
By the way, after finishing this kata, please try some of my other katas: [Here](https://www.codewars.com/collections/tonylicodings-authored-katas)
NOTE:
When given side_len(x, y), y will always be greater than or equal to x. Also, if a right triangle's legs are passed in, exclude the hypotenuse. If a right triangle's leg and hypotenuse are passed in, exclude the other leg.
Also feel free to reuse/extend the following starter code:
```python
def side_len(x, y):
```
|
{"functional": "_inputs = [[1, 1], [3, 4], [4, 6], [5, 12], [8, 10]]\n_outputs = [[[1]], [[2, 3, 4, 6]], [[3, 4, 5, 6, 7, 8, 9]], [[8, 9, 10, 11, 12, 14, 15, 16]], [[3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]]]\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(side_len(*i), o[0])"}
| 397
| 296
|
coding
|
Solve the programming task below in a Python markdown code block.
You are the top spy of AtCoder Kingdom. To prevent the stolen secret from being handed to AlDebaran Kingdom, you have sneaked into the party where the transaction happens.
There are N attendees in the party, and they are given attendee numbers from 1 through N. The height of Attendee i is A_i.
According to an examination beforehand, you know that a pair of attendees satisfying the condition below will make the transaction.
- The absolute difference of their attendee numbers is equal to the sum of their heights.
There are \frac{N(N-1)}{2} ways to choose two from the N attendees and make a pair. Among them, how many satisfy the condition above?
P.S.: We cannot let you know the secret.
-----Constraints-----
- All values in input are integers.
- 2 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9\ (1 \leq i \leq N)
-----Input-----
Input is given from Standard Input in the following format:
N
A_1 A_2 \dots A_N
-----Output-----
Print the number of pairs satisfying the condition.
-----Sample Input-----
6
2 3 3 1 3 1
-----Sample Output-----
3
- A_1 + A_4 = 3, so the pair of Attendee 1 and 4 satisfy the condition.
- A_2 + A_6 = 4, so the pair of Attendee 2 and 6 satisfy the condition.
- A_4 + A_6 = 2, so the pair of Attendee 4 and 6 satisfy the condition.
No other pair satisfies the condition, so you should print 3.
|
{"inputs": ["6\n2 3 6 1 3 1", "6\n5 2 2 2 8 8", "6\n2 5 3 1 3 1", "6\n3 6 3 1 6 1", "6\n2 6 6 1 3 1", "6\n2 5 3 2 3 1", "6\n2 6 3 1 3 1", "6\n2 5 3 2 3 2"], "outputs": ["3\n", "0\n", "2\n", "1\n", "2\n", "0\n", "2\n", "0\n"]}
| 383
| 158
|
coding
|
Solve the programming task below in a Python markdown code block.
Let's define the permutation of length n as an array p = [p1, p2, ..., pn] consisting of n distinct integers from range from 1 to n. We say that this permutation maps value 1 into the value p1, value 2 into the value p2 and so on.
Kyota Ootori has just learned about cyclic representation of a permutation. A cycle is a sequence of numbers such that each element of this sequence is being mapped into the next element of this sequence (and the last element of the cycle is being mapped into the first element of the cycle). The cyclic representation is a representation of p as a collection of cycles forming p. For example, permutation p = [4, 1, 6, 2, 5, 3] has a cyclic representation that looks like (142)(36)(5) because 1 is replaced by 4, 4 is replaced by 2, 2 is replaced by 1, 3 and 6 are swapped, and 5 remains in place.
Permutation may have several cyclic representations, so Kyoya defines the standard cyclic representation of a permutation as follows. First, reorder the elements within each cycle so the largest element is first. Then, reorder all of the cycles so they are sorted by their first element. For our example above, the standard cyclic representation of [4, 1, 6, 2, 5, 3] is (421)(5)(63).
Now, Kyoya notices that if we drop the parenthesis in the standard cyclic representation, we get another permutation! For instance, [4, 1, 6, 2, 5, 3] will become [4, 2, 1, 5, 6, 3].
Kyoya notices that some permutations don't change after applying operation described above at all. He wrote all permutations of length n that do not change in a list in lexicographic order. Unfortunately, his friend Tamaki Suoh lost this list. Kyoya wishes to reproduce the list and he needs your help. Given the integers n and k, print the permutation that was k-th on Kyoya's list.
Input
The first line will contain two integers n, k (1 ≤ n ≤ 50, 1 ≤ k ≤ min{1018, l} where l is the length of the Kyoya's list).
Output
Print n space-separated integers, representing the permutation that is the answer for the question.
Examples
Input
4 3
Output
1 3 2 4
Input
10 1
Output
1 2 3 4 5 6 7 8 9 10
Note
The standard cycle representation is (1)(32)(4), which after removing parenthesis gives us the original permutation. The first permutation on the list would be [1, 2, 3, 4], while the second permutation would be [1, 2, 4, 3].
|
{"inputs": ["1 1\n", "5 8\n", "5 1\n", "2 2\n", "3 3\n", "9 1\n", "4 2\n", "2 1\n"], "outputs": ["1 ", "2 1 4 3 5 ", "1 2 3 4 5 ", "2 1 ", "2 1 3 ", "1 2 3 4 5 6 7 8 9 ", "1 2 4 3 ", "1 2\n"]}
| 647
| 125
|
coding
|
Solve the programming task below in a Python markdown code block.
The only difference between this problem and D2 is that you don't have to provide the way to construct the answer in this problem, but you have to do it in D2.
There's a table of $n \times m$ cells ($n$ rows and $m$ columns). The value of $n \cdot m$ is even.
A domino is a figure that consists of two cells having a common side. It may be horizontal (one of the cells is to the right of the other) or vertical (one of the cells is above the other).
You need to find out whether it is possible to place $\frac{nm}{2}$ dominoes on the table so that exactly $k$ of them are horizontal and all the other dominoes are vertical. The dominoes cannot overlap and must fill the whole table.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10$) — the number of test cases. Then $t$ test cases follow.
Each test case consists of a single line. The line contains three integers $n$, $m$, $k$ ($1 \le n,m \le 100$, $0 \le k \le \frac{nm}{2}$, $n \cdot m$ is even) — the number of rows, columns and horizontal dominoes, respectively.
-----Output-----
For each test case output "YES", if it is possible to place dominoes in the desired way, or "NO" otherwise.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
8
4 4 2
2 3 0
3 2 3
1 2 0
2 4 2
5 2 2
2 17 16
2 1 1
Output
YES
YES
YES
NO
YES
NO
YES
NO
-----Note-----
None
|
{"inputs": ["1\n6 1 0\n", "1\n1 2 1\n", "1\n2 1 0\n", "1\n6 3 8\n", "1\n4 1 0\n", "1\n1 4 2\n", "1\n7 4 2\n", "1\n8 1 0\n"], "outputs": ["YES\n", "YES\n", "YES\n", "NO\n", "YES\n", "YES\n", "YES\n", "YES\n"]}
| 446
| 118
|
coding
|
Solve the programming task below in a Python markdown code block.
Given an array, return the reversed version of the array (a different kind of reverse though), you reverse portions of the array, you'll be given a length argument which represents the length of each portion you are to reverse.
E.g
if after reversing some portions of the array and the length of the remaining portion in the array is not up to the length argument, just reverse them.
`selReverse(array, length)`
- array - array to reverse
- length - length of each portion to reverse
Note : if the length argument exceeds the array length, reverse all of them, if the length argument is zero do not reverse at all.
Also feel free to reuse/extend the following starter code:
```python
def sel_reverse(arr,l):
```
|
{"functional": "_inputs = [[[2, 4, 6, 8, 10, 12, 14, 16], 3], [[2, 4, 6, 8, 10, 12, 14, 16], 2], [[1, 2, 3, 4, 5, 6], 2], [[1, 2, 3, 4, 5, 6], 0], [[1, 2, 3, 4, 5, 6], 10]]\n_outputs = [[[6, 4, 2, 12, 10, 8, 16, 14]], [[4, 2, 8, 6, 12, 10, 16, 14]], [[2, 1, 4, 3, 6, 5]], [[1, 2, 3, 4, 5, 6]], [[6, 5, 4, 3, 2, 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(sel_reverse(*i), o[0])"}
| 168
| 384
|
coding
|
Solve the programming task below in a Python markdown code block.
In recreational mathematics, a [Keith number](https://en.wikipedia.org/wiki/Keith_number) or repfigit number (short for repetitive Fibonacci-like digit) is a number in the following integer sequence:
`14, 19, 28, 47, 61, 75, 197, 742, 1104, 1537, 2208, 2580, 3684, 4788, 7385, 7647, 7909, ...` (sequence A007629 in the OEIS)
Keith numbers were introduced by Mike Keith in 1987. They are computationally very challenging to find, with only about 100 known.
Implement the code to check if the given number is a Keith number. Return the number number of iteration needed to confirm it; otherwise return `false`.
**Note:** 1-digit numbers are **not** Keith numbers by definition
## Examples
```
n = 197 # --> [1, 9, 7]
# calculation iteration
1 + 9 + 7 = 17 # 1
9 + 7 + 17 = 33 # 2
7 + 17 + 33 = 57 # 3
17 + 33 + 57 = 107 # 4
33 + 57 + 107 = 197 # 5
```
As `197` is the same as the initial number, so it's a Keith number: return `5`
Another example:
```
n = 196
# calculation iteration
1 + 9 + 6 = 16 # 1
...
```
`196` is not a Keith number, so return `false`
Also feel free to reuse/extend the following starter code:
```python
def is_keith_number(n):
```
|
{"functional": "_inputs = [[14], [10], [4], [28], [23], [0], [19], [47], [34], [61], [58], [75], [197], [742], [1104], [1537], [2208], [2697], [7385], [31331], [120284], [1084051], [44121607], [251133297], [96189170155], [11812665388886672], [855191324330802397989], [18354972585225358067718266], [41796205765147426974704791528]]\n_outputs = [[3], [False], [False], [3], [False], [False], [2], [4], [False], [6], [False], [5], [5], [8], [9], [8], [9], [False], [10], [13], [14], [17], [22], [24], [32], [48], [64], [78], [89]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(is_keith_number(*i), o[0])"}
| 465
| 493
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given a string s. We want to partition the string into as many parts as possible so that each letter appears in at most one part.
Note that the partition is done so that after concatenating all the parts in order, the resultant string should be s.
Return a list of integers representing the size of these parts.
Please complete the following python code precisely:
```python
class Solution:
def partitionLabels(self, s: str) -> List[int]:
```
|
{"functional": "def check(candidate):\n assert candidate(s = \"ababcbacadefegdehijhklij\") == [9,7,8]\n assert candidate(s = \"eccbbbbdec\") == [10]\n\n\ncheck(Solution().partitionLabels)"}
| 114
| 62
|
coding
|
Solve the programming task below in a Python markdown code block.
Vasya is studying in the last class of school and soon he will take exams. He decided to study polynomials. Polynomial is a function P(x) = a_0 + a_1x^1 + ... + a_{n}x^{n}. Numbers a_{i} are called coefficients of a polynomial, non-negative integer n is called a degree of a polynomial.
Vasya has made a bet with his friends that he can solve any problem with polynomials. They suggested him the problem: "Determine how many polynomials P(x) exist with integer non-negative coefficients so that $P(t) = a$, and $P(P(t)) = b$, where $t, a$ and b are given positive integers"?
Vasya does not like losing bets, but he has no idea how to solve this task, so please help him to solve the problem.
-----Input-----
The input contains three integer positive numbers $t, a, b$ no greater than 10^18.
-----Output-----
If there is an infinite number of such polynomials, then print "inf" without quotes, otherwise print the reminder of an answer modulo 10^9 + 7.
-----Examples-----
Input
2 2 2
Output
2
Input
2 3 3
Output
1
|
{"inputs": ["2 2 2\n", "2 3 3\n", "1 1 1\n", "7 8 9\n", "3 3 3\n", "1 5 5\n", "1 2 2\n", "1 2 5\n"], "outputs": ["2\n", "1\n", "inf\n", "1\n", "2\n", "1\n", "1\n", "1\n"]}
| 286
| 102
|
coding
|
Solve the programming task below in a Python markdown code block.
A group of $n$ dancers rehearses a performance for the closing ceremony. The dancers are arranged in a row, they've studied their dancing moves and can't change positions. For some of them, a white dancing suit is already bought, for some of them — a black one, and for the rest the suit will be bought in the future.
On the day when the suits were to be bought, the director was told that the participants of the olympiad will be happy if the colors of the suits on the scene will form a palindrome. A palindrome is a sequence that is the same when read from left to right and when read from right to left. The director liked the idea, and she wants to buy suits so that the color of the leftmost dancer's suit is the same as the color of the rightmost dancer's suit, the 2nd left is the same as 2nd right, and so on.
The director knows how many burls it costs to buy a white suit, and how many burls to buy a black suit. You need to find out whether it is possible to buy suits to form a palindrome, and if it's possible, what's the minimal cost of doing so. Remember that dancers can not change positions, and due to bureaucratic reasons it is not allowed to buy new suits for the dancers who already have suits, even if it reduces the overall spending.
-----Input-----
The first line contains three integers $n$, $a$, and $b$ ($1 \leq n \leq 20$, $1 \leq a, b \leq 100$) — the number of dancers, the cost of a white suit, and the cost of a black suit.
The next line contains $n$ numbers $c_i$, $i$-th of which denotes the color of the suit of the $i$-th dancer. Number $0$ denotes the white color, $1$ — the black color, and $2$ denotes that a suit for this dancer is still to be bought.
-----Output-----
If it is not possible to form a palindrome without swapping dancers and buying new suits for those who have one, then output -1. Otherwise, output the minimal price to get the desired visual effect.
-----Examples-----
Input
5 100 1
0 1 2 1 2
Output
101
Input
3 10 12
1 2 0
Output
-1
Input
3 12 1
0 1 0
Output
0
-----Note-----
In the first sample, the cheapest way to obtain palindromic colors is to buy a black suit for the third from left dancer and a white suit for the rightmost dancer.
In the second sample, the leftmost dancer's suit already differs from the rightmost dancer's suit so there is no way to obtain the desired coloring.
In the third sample, all suits are already bought and their colors form a palindrome.
|
{"inputs": ["1 100 1\n0\n", "1 1 100\n2\n", "1 100 1\n2\n", "2 9 6\n2 2\n", "1 1 100\n1\n", "1 100 1\n1\n", "1 1 100\n0\n", "2 1 1\n2 2\n"], "outputs": ["0\n", "1\n", "1\n", "12\n", "0\n", "0\n", "0\n", "2\n"]}
| 637
| 135
|
coding
|
Solve the programming task below in a Python markdown code block.
There are $n$ computers in a row, all originally off, and Phoenix wants to turn all of them on. He will manually turn on computers one at a time. At any point, if computer $i-1$ and computer $i+1$ are both on, computer $i$ $(2 \le i \le n-1)$ will turn on automatically if it is not already on. Note that Phoenix cannot manually turn on a computer that already turned on automatically.
If we only consider the sequence of computers that Phoenix turns on manually, how many ways can he turn on all the computers? Two sequences are distinct if either the set of computers turned on manually is distinct, or the order of computers turned on manually is distinct. Since this number may be large, please print it modulo $M$.
-----Input-----
The first line contains two integers $n$ and $M$ ($3 \le n \le 400$; $10^8 \le M \le 10^9$) — the number of computers and the modulo. It is guaranteed that $M$ is prime.
-----Output-----
Print one integer — the number of ways to turn on the computers modulo $M$.
-----Examples-----
Input
3 100000007
Output
6
Input
4 100000007
Output
20
Input
400 234567899
Output
20914007
-----Note-----
In the first example, these are the $6$ orders in which Phoenix can turn on all computers:
$[1,3]$. Turn on computer $1$, then $3$. Note that computer $2$ turns on automatically after computer $3$ is turned on manually, but we only consider the sequence of computers that are turned on manually.
$[3,1]$. Turn on computer $3$, then $1$.
$[1,2,3]$. Turn on computer $1$, $2$, then $3$.
$[2,1,3]$
$[2,3,1]$
$[3,2,1]$
|
{"inputs": ["3 100000007\n", "4 100000007\n", "5 999999937\n", "6 100000007\n", "7 100000007\n", "6 100000007\n", "5 999999937\n", "7 100000007\n"], "outputs": ["6\n", "20\n", "78\n", "344\n", "1680\n", "344\n", "78\n", "1680\n"]}
| 472
| 163
|
coding
|
Solve the programming task below in a Python markdown code block.
Sophia has discovered several alien languages. Suprisingly, all of these languages have an alphabet, and each of them may contain thousands of characters! Also, all the words in a language have the same number of characters in it.
However, the aliens like their words to be aesthetically pleasing, which for them means that for the $i^{th}$ letter of an $n$-letter alphabet (letters are indexed $1\ldots n$):
if $2i>n$, then the $i^{th}$ letter may be the last letter of a word, or it may be immediately followed by any letter, including itself.
if $2i\leq n$, then the $i^{th}$ letter can not be the last letter of a word and also can only be immediately followed by $j^{th}$ letter if and only if $j\geq2i$.
Sophia wants to know how many different words exist in this language. Since the result may be large, she wants to know this number, modulo $100000007(10^8+7)$.
Input Format
The first line contains $\boldsymbol{\boldsymbol{t}}$, the number of test cases. The first line is followed by $\boldsymbol{\boldsymbol{t}}$ lines, each line denoting a test case. Each test case will have two space-separated integers $n$, $m$ which denote the number of letters in the language and the length of words in this language respectively.
Constraints
$1\leq t\leq5$
$1\leq n\leq10^5$
$1\leq m\leq5\cdot10^5$
Output Format
For each test case, output the number of possible words modulo $100000007(10^8+7)$.
Sample Input
3
1 3
2 3
3 2
Sample Output
1
3
6
Explanation
For the first test case, there's one letter ('a') and all the words consist of $3$ letters. There's only one possibility which is "aaa".
For the second test case, there are two letters ('a' and 'b') and all the words are of $3$ letters. The possible strings are "abb", "bab", & "bbb". The words can end only with 'b' because $2\cdot\text{index}(b)=2\cdot2>2$ and for 'a', it's $2\cdot\text{index}(a)=2\cdot1\leq2$. "aab" is not allowed because 'a' can not be followed immediately by 'a'. For a word of length 4 and alphabet of size 2, "abab" would be allowed.
For the third test case, there are three letters ('a', 'b' and 'c') and all of the words are $2$ letters. The words can only end with 'b' or 'c'. The possible words are "ab", "ac", "bb", "cc", "bc", "cb".
|
{"inputs": ["3\n1 3\n2 3\n3 2\n"], "outputs": ["1\n3\n6\n"]}
| 675
| 30
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a positive integer $N$. Print a numerical triangle of height $N-1$ like the one below:
1
22
333
4444
55555
......
Can you do it using only arithmetic operations, a single for loop and print statement?
Use no more than two lines. The first line (the for statement) is already written for you. You have to complete the print statement.
Note: Using anything related to strings will give a score of $\mbox{0}$.
Input Format
A single line containing integer, $N$.
Constraints
$1\leq N\leq9$
Output Format
Print $N-1$ lines as explained above.
Sample Input
5
Sample Output
1
22
333
4444
|
{"inputs": ["5\n"], "outputs": ["1\n22\n333\n4444\n"]}
| 191
| 26
|
coding
|
Solve the programming task below in a Python markdown code block.
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis!
The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options.
You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once.
You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy.
-----Input-----
The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5) — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively.
The next line contains one integer m (0 ≤ m ≤ 3·10^5) — the number of mouses in the price list.
The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9) — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in.
-----Output-----
Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy.
-----Example-----
Input
2 1 1
4
5 USB
6 PS/2
3 PS/2
7 PS/2
Output
3 14
-----Note-----
In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
|
{"inputs": ["3 0 3\n0\n", "1 1 1\n0\n", "1 1 1\n0\n", "3 0 3\n0\n", "1 2 1\n0\n", "2 0 3\n0\n", "1 2 0\n0\n", "2 1 3\n0\n"], "outputs": ["0 0\n", "0 0\n", "0 0\n", "0 0\n", "0 0\n", "0 0\n", "0 0\n", "0 0\n"]}
| 519
| 134
|
coding
|
Solve the programming task below in a Python markdown code block.
Write a function that accepts two square (`NxN`) matrices (two dimensional arrays), and returns the product of the two. Only square matrices will be given.
How to multiply two square matrices:
We are given two matrices, A and B, of size 2x2 (note: tests are not limited to 2x2). Matrix C, the solution, will be equal to the product of A and B. To fill in cell `[0][0]` of matrix C, you need to compute: `A[0][0] * B[0][0] + A[0][1] * B[1][0]`.
More general: To fill in cell `[n][m]` of matrix C, you need to first multiply the elements in the nth row of matrix A by the elements in the mth column of matrix B, then take the sum of all those products. This will give you the value for cell `[m][n]` in matrix C.
## Example
```
A B C
|1 2| x |3 2| = | 5 4|
|3 2| |1 1| |11 8|
```
Detailed calculation:
```
C[0][0] = A[0][0] * B[0][0] + A[0][1] * B[1][0] = 1*3 + 2*1 = 5
C[0][1] = A[0][0] * B[0][1] + A[0][1] * B[1][1] = 1*2 + 2*1 = 4
C[1][0] = A[1][0] * B[0][0] + A[1][1] * B[1][0] = 3*3 + 2*1 = 11
C[1][1] = A[1][0] * B[0][1] + A[1][1] * B[1][1] = 3*2 + 2*1 = 8
```
Link to Wikipedia explaining matrix multiplication (look at the square matrix example):
http://en.wikipedia.org/wiki/Matrix_multiplication
A more visual explanation of matrix multiplication: http://matrixmultiplication.xyz
~~~if:c
**Note:** In **C**, the dimensions of both square matrices `n` will be passed into your function. However, since the dimensions of your returned "matrix" is expected to be the same as that of the inputs, you will not need to keep track of the dimensions of your matrix in another variable.
~~~
Also feel free to reuse/extend the following starter code:
```python
def matrix_mult(a, b):
```
|
{"functional": "_inputs = [[[[1, 2], [3, 2]], [[3, 2], [1, 1]]], [[[9, 7], [0, 1]], [[1, 1], [4, 12]]], [[[1, 2, 3], [3, 2, 1], [2, 1, 3]], [[4, 5, 6], [6, 5, 4], [4, 6, 5]]]]\n_outputs = [[[[5, 4], [11, 8]]], [[[37, 93], [4, 12]]], [[[28, 33, 29], [28, 31, 31], [26, 33, 31]]]]\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(matrix_mult(*i), o[0])"}
| 610
| 323
|
coding
|
Solve the programming task below in a Python markdown code block.
Dima loves Inna very much. He decided to write a song for her. Dima has a magic guitar with n strings and m frets. Dima makes the guitar produce sounds like that: to play a note, he needs to hold one of the strings on one of the frets and then pull the string. When Dima pulls the i-th string holding it on the j-th fret the guitar produces a note, let's denote it as a_{ij}. We know that Dima's guitar can produce k distinct notes. It is possible that some notes can be produced in multiple ways. In other words, it is possible that a_{ij} = a_{pq} at (i, j) ≠ (p, q).
Dima has already written a song — a sequence of s notes. In order to play the song, you need to consecutively produce the notes from the song on the guitar. You can produce each note in any available way. Dima understood that there are many ways to play a song and he wants to play it so as to make the song look as complicated as possible (try to act like Cobein).
We'll represent a way to play a song as a sequence of pairs (x_{i}, y_{i}) (1 ≤ i ≤ s), such that the x_{i}-th string on the y_{i}-th fret produces the i-th note from the song. The complexity of moving between pairs (x_1, y_1) and (x_2, y_2) equals $|x_{1} - x_{2}|$ + $|y_{1} - y_{2}|$. The complexity of a way to play a song is the maximum of complexities of moving between adjacent pairs.
Help Dima determine the maximum complexity of the way to play his song! The guy's gotta look cool!
-----Input-----
The first line of the input contains four integers n, m, k and s (1 ≤ n, m ≤ 2000, 1 ≤ k ≤ 9, 2 ≤ s ≤ 10^5).
Then follow n lines, each containing m integers a_{ij} (1 ≤ a_{ij} ≤ k). The number in the i-th row and the j-th column (a_{ij}) means a note that the guitar produces on the i-th string and the j-th fret.
The last line of the input contains s integers q_{i} (1 ≤ q_{i} ≤ k) — the sequence of notes of the song.
-----Output-----
In a single line print a single number — the maximum possible complexity of the song.
-----Examples-----
Input
4 6 5 7
3 1 2 2 3 1
3 2 2 2 5 5
4 2 2 2 5 3
3 2 2 1 4 3
2 3 1 4 1 5 1
Output
8
Input
4 4 9 5
4 7 9 5
1 2 1 7
8 3 4 9
5 7 7 2
7 1 9 2 5
Output
4
|
{"inputs": ["1 10 9 5\n1 2 3 4 5 6 7 8 9 1\n1 1 9 2 3\n", "10 1 9 5\n1\n2\n3\n4\n5\n6\n7\n8\n9\n1\n1 1 9 2 3\n", "10 1 9 5\n1\n2\n3\n4\n5\n6\n7\n8\n9\n1\n1 1 9 2 3\n", "1 10 9 5\n1 2 3 4 5 6 7 8 9 1\n1 1 9 2 3\n", "1 10 9 5\n1 2 3 3 5 6 7 8 9 1\n1 1 9 2 3\n", "1 10 9 5\n1 2 3 3 5 6 7 6 9 1\n1 1 9 2 3\n", "10 1 9 5\n1\n2\n3\n4\n5\n6\n7\n8\n9\n1\n1 1 9 2 2\n", "1 10 9 5\n1 2 3 4 5 6 7 8 9 1\n1 1 9 1 3\n"], "outputs": ["9\n", "9\n", "9\n", "9\n", "9\n", "9\n", "9\n", "9\n"]}
| 694
| 366
|
coding
|
Solve the programming task below in a Python markdown code block.
# Task
A range-collapse representation of an array of integers looks like this: `"1,3-6,8"`, where `3-6` denotes the range from `3-6`, i.e. `[3,4,5,6]`.
Hence `"1,3-6,8"` = `[1,3,4,5,6,8]`. Some other range-collapse representations of `[1,3,4,5,6,8]` include `"1,3-5,6,8", "1,3,4,5,6,8", etc`.
Each range is written in the following format `"a-b"`, where `a < b`, and the whole range must belong to the array in an increasing order.
You are given an array `arr`. Your task is to find the number of different range-collapse representations of the given array.
# Example
For `arr = [1,3,4,5,6,8]`, the result should be `8`.
```
"1,3-4,5,6,8"
"1,3-4,5-6,8"
"1,3-5,6,8"
"1,3-6,8"
"1,3,4-5,6,8"
"1,3,4-6,8"
"1,3,4,5-6,8"
"1,3,4,5,6,8"```
# Input/OutPut
- `[input]` integer array `arr`
sorted array of different positive integers.
- `[output]` an integer
the number of different range-collapse representations of the given array.
Also feel free to reuse/extend the following starter code:
```python
def descriptions(arr):
```
|
{"functional": "_inputs = [[[1, 3, 4, 5, 6, 8]], [[1, 2, 3]], [[11, 43, 66, 123]], [[3, 4, 5, 8, 9, 10, 11, 23, 43, 66, 67]]]\n_outputs = [[8], [4], [1], [64]]\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(descriptions(*i), o[0])"}
| 390
| 244
|
coding
|
Solve the programming task below in a Python markdown code block.
A permutation of length N is an array of N integers A = [A_{1}, A_{2}, \dots, A_{N}] such that every integer from 1 to N appears in it exactly once.
We define a function over a permutation as follows:
F(A) = (A_{1} * A_{2}) + (A_{2} * A_{3}) + \cdots + (A_{N-2} * A_{N-1}) + (A_{N-1}*A_{N})
You are given an integer N. What is the expected value of the function F over all possible permutations of length N?
The expected value of the function can be represented as a fraction of the form \frac{P}{Q}. You are required to print P \cdot Q^{-1} \pmod{1 \, 000 \, 000 \, 007}.
------ Input Format ------
- The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first and only line of each test case contains a single integer N.
------ Output Format ------
For each test case, output on a single line the expected value of the function modulo 1 \, 000 \, 000 \, 007.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$2 ≤ N ≤ 10^{9}$
----- Sample Input 1 ------
2
2
3
----- Sample Output 1 ------
2
333333343
----- explanation 1 ------
- Test Case $1$: There are $2$ possible permutations: $A = \{1, 2\}$ with $F(A) = 2$ and $A = \{2, 1\}$ with $F(A) = 2$. Hence the expected value of the function is $F(A) = \frac{1}{2} * 2 + \frac{1}{2} * 2 = 2$.
- Test Case $2$: There are $6$ possible permutations, with the value of function as $\{5, 5, 8, 8, 9, 9 \}$. Hence the expected value of the function is $F(A) = \frac{5+5+8+8+9+9}{6} = \frac{22}{3}$. The required output will be $333333343$, since $333333343 \cdot 3 \equiv 22 \pmod {1 \, 000 \, 000 \, 007}$.
|
{"inputs": ["2\n2\n3"], "outputs": ["2\n333333343\n"]}
| 600
| 27
|
coding
|
Solve the programming task below in a Python markdown code block.
Write a function that returns a sequence (index begins with 1) of all the even characters from a string. If the string is smaller than two characters or longer than 100 characters, the function should return "invalid string".
For example:
`````
"abcdefghijklm" --> ["b", "d", "f", "h", "j", "l"]
"a" --> "invalid string"
`````
Also feel free to reuse/extend the following starter code:
```python
def even_chars(st):
```
|
{"functional": "_inputs = [['a'], ['abcdefghijklm'], ['aBc_e9g*i-k$m'], [''], ['ab'], ['aiqbuwbjqwbckjdwbwkqbefhglqhfjbwqejbcadn.bcaw.jbhwefjbwqkvbweevkj.bwvwbhvjk.dsvbajdv.hwuvghwuvfhgw.vjhwncv.wecnaw.ecnvw.kejvhnw.evjkhweqv.kjhwqeev.kjbhdjk.vbaewkjva']]\n_outputs = [['invalid string'], [['b', 'd', 'f', 'h', 'j', 'l']], [['B', '_', '9', '*', '-', '$']], ['invalid string'], [['b']], ['invalid string']]\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(even_chars(*i), o[0])"}
| 123
| 309
|
coding
|
Solve the programming task below in a Python markdown code block.
Find the number of palindromic numbers among the integers between A and B (inclusive).
Here, a palindromic number is a positive integer whose string representation in base 10 (without leading zeros) reads the same forward and backward.
-----Constraints-----
- 10000 \leq A \leq B \leq 99999
- All input values are integers.
-----Input-----
Input is given from Standard Input in the following format:
A B
-----Output-----
Print the number of palindromic numbers among the integers between A and B (inclusive).
-----Sample Input-----
11009 11332
-----Sample Output-----
4
There are four integers that satisfy the conditions: 11011, 11111, 11211 and 11311.
|
{"inputs": ["7055 945", "7055 699", "5344 1543", "7055 1543", "10323 699", "10323 293", "10426 293", "15714 1543"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
| 197
| 125
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given an array of strings of the same length words.
In one move, you can swap any two even indexed characters or any two odd indexed characters of a string words[i].
Two strings words[i] and words[j] are special-equivalent if after any number of moves, words[i] == words[j].
For example, words[i] = "zzxy" and words[j] = "xyzz" are special-equivalent because we may make the moves "zzxy" -> "xzzy" -> "xyzz".
A group of special-equivalent strings from words is a non-empty subset of words such that:
Every pair of strings in the group are special equivalent, and
The group is the largest size possible (i.e., there is not a string words[i] not in the group such that words[i] is special-equivalent to every string in the group).
Return the number of groups of special-equivalent strings from words.
Please complete the following python code precisely:
```python
class Solution:
def numSpecialEquivGroups(self, words: List[str]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(words = [\"abcd\",\"cdab\",\"cbad\",\"xyzz\",\"zzxy\",\"zzyx\"]) == 3\n assert candidate(words = [\"abc\",\"acb\",\"bac\",\"bca\",\"cab\",\"cba\"]) == 3\n\n\ncheck(Solution().numSpecialEquivGroups)"}
| 243
| 77
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given an integer n, return true if n has exactly three positive divisors. Otherwise, return false.
An integer m is a divisor of n if there exists an integer k such that n = k * m.
Please complete the following python code precisely:
```python
class Solution:
def isThree(self, n: int) -> bool:
```
|
{"functional": "def check(candidate):\n assert candidate(n = 2) == False\n assert candidate(n = 4) == True\n\n\ncheck(Solution().isThree)"}
| 90
| 41
|
coding
|
Solve the programming task below in a Python markdown code block.
God's Blessing on This PermutationForces!
A Random Pebble
You are given a permutation $p_1,p_2,\ldots,p_n$ of length $n$ and a positive integer $k \le n$.
In one operation you can choose two indices $i$ and $j$ ($1 \le i < j \le n$) and swap $p_i$ with $p_j$.
Find the minimum number of operations needed to make the sum $p_1 + p_2 + \ldots + p_k$ as small as possible.
A permutation is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). Description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k \le n \le 100$).
The second line of each test case contains $n$ integers $p_1,p_2,\ldots,p_n$ ($1 \le p_i \le n$). It is guaranteed that the given numbers form a permutation of length $n$.
-----Output-----
For each test case print one integer — the minimum number of operations needed to make the sum $p_1 + p_2 + \ldots + p_k$ as small as possible.
-----Examples-----
Input
4
3 1
2 3 1
3 3
1 2 3
4 2
3 4 1 2
1 1
1
Output
1
0
2
0
-----Note-----
In the first test case, the value of $p_1 + p_2 + \ldots + p_k$ is initially equal to $2$, but the smallest possible value is $1$. You can achieve it by swapping $p_1$ with $p_3$, resulting in the permutation $[1, 3, 2]$.
In the second test case, the sum is already as small as possible, so the answer is $0$.
|
{"inputs": ["4\n3 1\n2 3 1\n3 3\n1 2 3\n4 2\n3 4 1 2\n1 1\n1\n"], "outputs": ["1\n0\n2\n0\n"]}
| 550
| 58
|
coding
|
Solve the programming task below in a Python markdown code block.
Today you are going to lead a group of elven archers to defend the castle that is attacked by an army of angry orcs. Three sides of the castle are protected by impassable mountains and the remaining side is occupied by a long wall that is split into n sections. At this moment there are exactly ai archers located at the i-th section of this wall. You know that archer who stands at section i can shoot orcs that attack section located at distance not exceeding r, that is all such sections j that |i - j| ≤ r. In particular, r = 0 means that archers are only capable of shooting at orcs who attack section i.
Denote as defense level of section i the total number of archers who can shoot at the orcs attacking this section. Reliability of the defense plan is the minimum value of defense level of individual wall section.
There is a little time left till the attack so you can't redistribute archers that are already located at the wall. However, there is a reserve of k archers that you can distribute among wall sections in arbitrary way. You would like to achieve maximum possible reliability of the defence plan.
Input
The first line of the input contains three integers n, r and k (1 ≤ n ≤ 500 000, 0 ≤ r ≤ n, 0 ≤ k ≤ 1018) — the number of sections of the wall, the maximum distance to other section archers can still shoot and the number of archers yet to be distributed along the wall. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109) — the current number of archers at each section.
Output
Print one integer — the maximum possible value of defense plan reliability, i.e. the maximum possible value of minimum defense level if we distribute k additional archers optimally.
Examples
Input
5 0 6
5 4 3 4 9
Output
5
Input
4 2 0
1 2 3 4
Output
6
Input
5 1 1
2 1 2 1 2
Output
3
|
{"inputs": ["1 0 0\n1\n", "1 0 0\n0\n", "1 1 0\n0\n", "1 1 10\n23\n", "2 0 1\n1 1\n", "1 1 10\n20\n", "2 0 1\n1 0\n", "2 0 100\n98 2\n"], "outputs": ["1\n", "0\n", "0", "33\n", "1\n", "30", "1", "100\n"]}
| 470
| 132
|
coding
|
Solve the programming task below in a Python markdown code block.
Make a function that receives a value, ```val``` and outputs the smallest higher number than the given value, and this number belong to a set of positive integers that have the following properties:
- their digits occur only once
- they are odd
- they are multiple of three
```python
next_numb(12) == 15
next_numb(13) == 15
next_numb(99) == 105
next_numb(999999) == 1023459
next_number(9999999999) == "There is no possible number that
fulfills those requirements"
```
Enjoy the kata!!
Also feel free to reuse/extend the following starter code:
```python
def next_numb(val):
```
|
{"functional": "_inputs = [[12], [13], [99], [999999], [9999999999]]\n_outputs = [[15], [15], [105], [1023459], ['There is no possible number that fulfills those requirements']]\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(next_numb(*i), o[0])"}
| 187
| 215
|
coding
|
Solve the programming task below in a Python markdown code block.
Leo has developed a new programming language C+=. In C+=, integer variables can only be changed with a "+=" operation that adds the right-hand side value to the left-hand side variable. For example, performing "a += b" when a = $2$, b = $3$ changes the value of a to $5$ (the value of b does not change).
In a prototype program Leo has two integer variables a and b, initialized with some positive values. He can perform any number of operations "a += b" or "b += a". Leo wants to test handling large integers, so he wants to make the value of either a or b strictly greater than a given value $n$. What is the smallest number of operations he has to perform?
-----Input-----
The first line contains a single integer $T$ ($1 \leq T \leq 100$) — the number of test cases.
Each of the following $T$ lines describes a single test case, and contains three integers $a, b, n$ ($1 \leq a, b \leq n \leq 10^9$) — initial values of a and b, and the value one of the variables has to exceed, respectively.
-----Output-----
For each test case print a single integer — the smallest number of operations needed. Separate answers with line breaks.
-----Example-----
Input
2
1 2 3
5 4 100
Output
2
7
-----Note-----
In the first case we cannot make a variable exceed $3$ in one operation. One way of achieving this in two operations is to perform "b += a" twice.
|
{"inputs": ["2\n1 2 3\n5 4 100\n", "2\n1 2 3\n7 4 100\n", "2\n1 1 3\n5 4 100\n", "2\n1 1 3\n5 4 101\n", "2\n1 1 3\n5 4 111\n", "2\n1 2 3\n5 4 100\n", "16\n1 1 1\n3 4 7\n4 5 13\n456 123 7890123\n1 1 1000000000\n45 12 782595420\n1 1000000000 1000000000\n1 999999999 1000000000\n1 99999 676497416\n5 6 930234861\n8 9 881919225\n500000000 500000000 1000000000\n1000000000 1000000000 1000000000\n999999999 1000000000 1000000000\n666 999999 987405273\n5378 5378 652851553\n", "16\n1 1 1\n3 4 7\n4 5 13\n456 123 7890123\n1 1 1000000000\n45 12 782595420\n1 1000000000 1000000000\n1 999999999 1000000000\n1 99999 676497416\n5 6 930234861\n8 9 881919225\n500000000 500000000 1000000000\n1000000000 1000000000 1000000000\n999999999 1000000000 1000000000\n666 999999 987405273\n5378 5378 652851553\n"], "outputs": ["2\n7\n", "2\n6\n", "3\n7\n", "3\n7\n", "3\n7\n", "2\n7\n", "1\n2\n2\n21\n43\n36\n1\n2\n20\n40\n38\n2\n1\n1\n16\n24\n", "1\n2\n2\n21\n43\n36\n1\n2\n20\n40\n38\n2\n1\n1\n16\n24\n"]}
| 364
| 836
|
coding
|
Solve the programming task below in a Python markdown code block.
Little girl Susie went shopping with her mom and she wondered how to improve service quality.
There are n people in the queue. For each person we know time t_{i} needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed.
Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue.
-----Input-----
The first line contains integer n (1 ≤ n ≤ 10^5).
The next line contains n integers t_{i} (1 ≤ t_{i} ≤ 10^9), separated by spaces.
-----Output-----
Print a single number — the maximum number of not disappointed people in the queue.
-----Examples-----
Input
5
15 2 1 5 3
Output
4
-----Note-----
Value 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5.
|
{"inputs": ["5\n15 2 1 5 3\n", "1\n1000000000\n", "1\n1000000000\n", "5\n15 2 2 5 3\n", "5\n15 2 1 8 3\n", "5\n15 2 2 4 3\n", "5\n15 2 1 5 3\n", "10\n13 2 5 55 21 34 1 8 1 3\n"], "outputs": ["4\n", "1\n", "1\n", "4\n", "5\n", "4\n", "4\n", "6\n"]}
| 289
| 172
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.