task_type
stringclasses 1
value | problem
stringlengths 209
3.39k
| answer
stringlengths 35
6.15k
| problem_tokens
int64 60
774
| answer_tokens
int64 12
2.04k
|
|---|---|---|---|---|
coding
|
Solve the programming task below in a Python markdown code block.
Mario transforms each time he eats a mushroom as follows:
If he is currently small, he turns normal.
If he is currently normal, he turns huge.
If he is currently huge, he turns small.
Given that Mario was initially normal, find his size after eating X mushrooms.
------ Input Format ------
- The first line of input will contain one integer T, the number of test cases. Then the test cases follow.
- Each test case contains a single line of input, containing one integer X.
------ Output Format ------
For each test case, output in a single line Mario's size after eating X mushrooms.
Print:
- \texttt{NORMAL}, if his final size is normal.
- \texttt{HUGE}, if his final size is huge.
- \texttt{SMALL}, if his final size is small.
You may print each character of the answer in either uppercase or lowercase (for example, the strings \texttt{Huge}, \texttt{hUgE}, \texttt{huge} and \texttt{HUGE} will all be treated as identical).
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ X ≤ 100$
----- Sample Input 1 ------
3
2
4
12
----- Sample Output 1 ------
SMALL
HUGE
NORMAL
----- explanation 1 ------
Test case $1$: Mario's initial size is normal. On eating the first mushroom, he turns huge. On eating the second mushroom, he turns small.
Test case $2$: Mario's initial size is normal. On eating the first mushroom, he turns huge. On eating the second mushroom, he turns small. On eating the third mushroom, he turns normal. On eating the fourth mushroom, he turns huge.
|
{"inputs": ["3\n2\n4\n12"], "outputs": ["SMALL\nHUGE\nNORMAL"]}
| 384
| 25
|
coding
|
Solve the programming task below in a Python markdown code block.
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher.
Neko has two integers $a$ and $b$. His goal is to find a non-negative integer $k$ such that the least common multiple of $a+k$ and $b+k$ is the smallest possible. If there are multiple optimal integers $k$, he needs to choose the smallest one.
Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it?
-----Input-----
The only line contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$).
-----Output-----
Print the smallest non-negative integer $k$ ($k \ge 0$) such that the lowest common multiple of $a+k$ and $b+k$ is the smallest possible.
If there are many possible integers $k$ giving the same value of the least common multiple, print the smallest one.
-----Examples-----
Input
6 10
Output
2
Input
21 31
Output
9
Input
5 10
Output
0
-----Note-----
In the first test, one should choose $k = 2$, as the least common multiple of $6 + 2$ and $10 + 2$ is $24$, which is the smallest least common multiple possible.
|
{"inputs": ["1 1\n", "2 3\n", "2 2\n", "2 6\n", "6 9\n", "2 3\n", "2 2\n", "2 6\n"], "outputs": ["0", "0", "0", "0", "0", "0\n", "0\n", "0\n"]}
| 307
| 81
|
coding
|
Solve the programming task below in a Python markdown code block.
Chef has an array A of length N.
Chef forms a binary array B of length N using the parity of the sums of adjacent elements in A. Formally,
B_{i} = (A_{i} + A_{i+1}) \, \% \, 2 for 1 ≤ i ≤ N - 1
B_{N} = (A_{N} + A_{1}) \, \% \, 2
Here x \, \% \, y denotes the remainder obtained when x is divided by y.
Chef lost the array A and needs your help. Given array B, determine whether there exits any valid array A which could have formed B.
------ 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 N space-separated integers B_{1}, B_{2}, \dots, B_{N} denoting the array B.
------ Output Format ------
For each testcase, output YES if there exists a valid array A, NO otherwise.
You can print any character in any case. For example YES, Yes, yEs are all considered same.
------ Constraints ------
$1 ≤ T ≤ 1000$
$2 ≤ N ≤ 10^{5}$
$B_{i} \in \{0, 1\}$
- The sum of $N$ over all test cases do not exceed $3 \cdot 10^{5}$.
----- Sample Input 1 ------
4
2
0 0
2
1 0
4
1 0 1 0
3
1 0 0
----- Sample Output 1 ------
YES
NO
YES
NO
----- explanation 1 ------
Test case 1: One such valid array is $A = [3, 3]$.
Test case 2: It can be shown that no such arrays exist and are valid.
Test case 3: One such valid array is $A = [1, 2, 4, 5]$.
- $B_{1} = 1$ since $A_{1} + A_{2} = 1 + 2 = 3$ and $3 \, \% \, 2 = 1$
- $B_{2} = 0$ since $A_{2} + A_{3} = 2 + 4 = 6$ and $6 \, \% \, 2 = 0$
- $B_{3} = 1$ since $A_{3} + A_{4} = 4 + 5 = 9$ and $9 \, \% \, 2 = 1$
- $B_{4} = 0$ since $A_{4} + A_{1} = 5 + 1 = 6$ and $6 \, \% \, 2 = 0$
|
{"inputs": ["4\n2\n0 0\n2\n1 0\n4\n1 0 1 0\n3\n1 0 0\n"], "outputs": ["YES\nNO\nYES\nNO\n"]}
| 649
| 50
|
coding
|
Solve the programming task below in a Python markdown code block.
Chef is given two integers N, S.
Consider an array A of size N such that A[i]=i for every 1 ≤ i ≤ N.
You are required to find any position x(1≤ x ≤ N) such that :
(A[1]+A[2]+...A[x-1]) + (A[x+1]+A[x+2]+...A[N]) = S
If there are multiple such positions x, print any such position. If there is no such position, print -1.
------ Input Format ------
- First line will contain T, number of testcases. Then the testcases follow.
- Each testcase contains of a single line of input, two integers N, S.
------ Output Format ------
For each testcase, output the required position or -1 in case there is no such position.
------ Constraints ------
$1 ≤ T ≤ 1000$
$2 ≤ N ≤ 10^{9}$
$1 ≤ S ≤ 10^{18}$
----- Sample Input 1 ------
3
4 8
5 10
2 3
----- Sample Output 1 ------
2
5
-1
----- explanation 1 ------
Test Case $1$: $A[1]+A[3]+A[4]=1+3+4=8$, so $x=2$ is a suitable position.
Test Case $3$: There is no suitable position.
|
{"inputs": ["3\n4 8\n5 10\n2 3\n"], "outputs": ["2\n5\n-1\n"]}
| 313
| 32
|
coding
|
Solve the programming task below in a Python markdown code block.
There are N squares arranged in a row, numbered 1, 2, ..., N from left to right. You are given a string S of length N consisting of `.` and `#`. If the i-th character of S is `#`, Square i contains a rock; if the i-th character of S is `.`, Square i is empty.
In the beginning, Snuke stands on Square A, and Fnuke stands on Square B.
You can repeat the following operation any number of times:
* Choose Snuke or Fnuke, and make him jump one or two squares to the right. The destination must be one of the squares, and it must not contain a rock or the other person.
You want to repeat this operation so that Snuke will stand on Square C and Fnuke will stand on Square D.
Determine whether this is possible.
Constraints
* 4 \leq N \leq 200\ 000
* S is a string of length N consisting of `.` and `#`.
* 1 \leq A, B, C, D \leq N
* Square A, B, C and D do not contain a rock.
* A, B, C and D are all different.
* A < B
* A < C
* B < D
Input
Input is given from Standard Input in the following format:
N A B C D
S
Output
Print `Yes` if the objective is achievable, and `No` if it is not.
Examples
Input
7 1 3 6 7
.#..#..
Output
Yes
Input
7 1 3 7 6
.#..#..
Output
No
Input
15 1 3 15 13
...#.#...#.#...
Output
Yes
|
{"inputs": ["9 1 3 7 6\n.#..#..", "4 1 3 7 6\n.#..#..", "8 1 3 7 6\n.#..#..", "1 1 3 7 6\n.#..#..", "8 1 4 7 6\n.#..#..", "8 2 4 7 6\n.#..#..", "5 1 3 7 6\n.#..#..", "0 1 3 7 6\n.#..#.."], "outputs": ["No\n", "No\n", "No\n", "No\n", "No\n", "No\n", "No\n", "No\n"]}
| 400
| 166
|
coding
|
Solve the programming task below in a Python markdown code block.
Vasya has n burles. One bottle of Ber-Cola costs a burles and one Bars bar costs b burles. He can buy any non-negative integer number of bottles of Ber-Cola and any non-negative integer number of Bars bars.
Find out if it's possible to buy some amount of bottles of Ber-Cola and Bars bars and spend exactly n burles.
In other words, you should find two non-negative integers x and y such that Vasya can buy x bottles of Ber-Cola and y Bars bars and x·a + y·b = n or tell that it's impossible.
-----Input-----
First line contains single integer n (1 ≤ n ≤ 10 000 000) — amount of money, that Vasya has.
Second line contains single integer a (1 ≤ a ≤ 10 000 000) — cost of one bottle of Ber-Cola.
Third line contains single integer b (1 ≤ b ≤ 10 000 000) — cost of one Bars bar.
-----Output-----
If Vasya can't buy Bars and Ber-Cola in such a way to spend exactly n burles print «NO» (without quotes).
Otherwise in first line print «YES» (without quotes). In second line print two non-negative integers x and y — number of bottles of Ber-Cola and number of Bars bars Vasya should buy in order to spend exactly n burles, i.e. x·a + y·b = n. If there are multiple answers print any of them.
Any of numbers x and y can be equal 0.
-----Examples-----
Input
7
2
3
Output
YES
2 1
Input
100
25
10
Output
YES
0 10
Input
15
4
8
Output
NO
Input
9960594
2551
2557
Output
YES
1951 1949
-----Note-----
In first example Vasya can buy two bottles of Ber-Cola and one Bars bar. He will spend exactly 2·2 + 1·3 = 7 burles.
In second example Vasya can spend exactly n burles multiple ways: buy two bottles of Ber-Cola and five Bars bars; buy four bottles of Ber-Cola and don't buy Bars bars; don't buy Ber-Cola and buy 10 Bars bars.
In third example it's impossible to but Ber-Cola and Bars bars in order to spend exactly n burles.
|
{"inputs": ["7\n2\n3\n", "1\n7\n8\n", "4\n2\n2\n", "4\n1\n3\n", "4\n1\n2\n", "4\n3\n1\n", "4\n2\n1\n", "1\n1\n1\n"], "outputs": ["YES\n2 1\n", "NO\n", "YES\n0 2\n", "YES\n1 1\n", "YES\n0 2\n", "YES\n0 4\n", "YES\n0 4\n", "YES\n0 1\n"]}
| 558
| 130
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a string $s$ of length $n$ consisting of characters a and/or b.
Let $\operatorname{AB}(s)$ be the number of occurrences of string ab in $s$ as a substring. Analogically, $\operatorname{BA}(s)$ is the number of occurrences of ba in $s$ as a substring.
In one step, you can choose any index $i$ and replace $s_i$ with character a or b.
What is the minimum number of steps you need to make to achieve $\operatorname{AB}(s) = \operatorname{BA}(s)$?
Reminder:
The number of occurrences of string $d$ in $s$ as substring is the number of indices $i$ ($1 \le i \le |s| - |d| + 1$) such that substring $s_i s_{i + 1} \dots s_{i + |d| - 1}$ is equal to $d$. For example, $\operatorname{AB}($aabbbabaa$) = 2$ since there are two indices $i$: $i = 2$ where aabbbabaa and $i = 6$ where aabbbabaa.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). Description of the test cases follows.
The first and only line of each test case contains a single string $s$ ($1 \le |s| \le 100$, where $|s|$ is the length of the string $s$), consisting only of characters a and/or b.
-----Output-----
For each test case, print the resulting string $s$ with $\operatorname{AB}(s) = \operatorname{BA}(s)$ you'll get making the minimum number of steps.
If there are multiple answers, print any of them.
-----Examples-----
Input
4
b
aabbbabaa
abbb
abbaab
Output
b
aabbbabaa
bbbb
abbaaa
-----Note-----
In the first test case, both $\operatorname{AB}(s) = 0$ and $\operatorname{BA}(s) = 0$ (there are no occurrences of ab (ba) in b), so can leave $s$ untouched.
In the second test case, $\operatorname{AB}(s) = 2$ and $\operatorname{BA}(s) = 2$, so you can leave $s$ untouched.
In the third test case, $\operatorname{AB}(s) = 1$ and $\operatorname{BA}(s) = 0$. For example, we can change $s_1$ to b and make both values zero.
In the fourth test case, $\operatorname{AB}(s) = 2$ and $\operatorname{BA}(s) = 1$. For example, we can change $s_6$ to a and make both values equal to $1$.
|
{"inputs": ["4\nb\naabbbabaa\nabbb\nabbaab\n", "4\nb\naabbbabaa\nabbb\nabbaab\n", "4\na\naabbbabaa\nabbb\nabbaab\n", "4\nb\naabbbabaa\nabbb\nabbabb\n", "4\na\naabababaa\nabbb\nabbaab\n", "4\nb\naabbbacaa\nabbb\nabbabb\n", "4\nc\naabbbacaa\nabbb\nabbabb\n", "4\nc\naacabbbaa\nabbb\nabbabb\n"], "outputs": ["b\naabbbabaa\nbbbb\nbbbaab\n", "b\naabbbabaa\nbbbb\nbbbaab\n", "a\naabbbabaa\nbbbb\nbbbaab\n", "b\naabbbabaa\nbbbb\nbbbabb\n", "a\naabababaa\nbbbb\nbbbaab\n", "b\naabbbacaa\nbbbb\nbbbabb\n", "c\naabbbacaa\nbbbb\nbbbabb\n", "c\naacabbbaa\nbbbb\nbbbabb\n"]}
| 656
| 262
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Your music player contains n different songs. You want to listen to goal songs (not necessarily different) during your trip. To avoid boredom, you will create a playlist so that:
Every song is played at least once.
A song can only be played again only if k other songs have been played.
Given n, goal, and k, return the number of possible playlists that you can create. Since the answer can be very large, return it modulo 109 + 7.
Please complete the following python code precisely:
```python
class Solution:
def numMusicPlaylists(self, n: int, goal: int, k: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(n = 3, goal = 3, k = 1) == 6\n assert candidate(n = 2, goal = 3, k = 0) == 6\n assert candidate(n = 2, goal = 3, k = 1) == 2\n\n\ncheck(Solution().numMusicPlaylists)"}
| 155
| 87
|
coding
|
Solve the programming task below in a Python markdown code block.
Chef spent N days working really hard! He planned loads of tasks: as many as Ai tasks to do on the ith day! Chef's work was brutal, so he only managed to finish Bi tasks on the ith day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
-----Input-----
- The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
- The first line of each test case contains three integers — N, K, M — denoting the number of days, white and black buttons appropriately.
- The second line contains N space-separated integers A1, A2, … , AN, denoting the number of planned tasks.
- The third line contains N space-separated integers B1, B2, … , BN, denoting the number of completed tasks.
- The fourth line contains K space-separated integers C1, C2, … , CK, denoting the integers on white buttons.
- The fifth and last line contains M space-separated integers D1, D2, … , DM, denoting the integers on black buttons.
-----Output-----
- In a single line, output an integer — the minimum possible amount of uncompleted tasks.
-----Constraints-----
- 1 ≤ T ≤ 4
- 1 ≤ N, K, M ≤ 10^5
- 1 ≤ Bi ≤ Ai ≤ 10^5
- 1 ≤ Ci, Di ≤ 10^5
-----Subtasks-----
- Subtask N ≤ 10, K, M ≤ 5. Points: 30
- Subtask Original constraints. Points: 70
-----Example-----
Input:
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
Output:
3
-----Explanation-----
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks.
|
{"inputs": ["1\n4 2 2\n5 7 6 1\n3 3 1 1\n6 3\n1 4"], "outputs": ["3"]}
| 720
| 42
|
coding
|
Solve the programming task below in a Python markdown code block.
Killgrave wants to use his mind control powers to get money from the Justice League superheroes living in $N$ houses in Happy Harbor that are numbered sequentially from $1$ to $N$. There are $\mbox{M}$ roads, and each road $j$ connects two different houses, $A_j$ and $B_j$. Each superhero house $\boldsymbol{i}$ (where $1\leq i\leq N$) has $C_i$ dollars stashed away for a rainy day.
As long as a superhero is home at house $\boldsymbol{i}$, Killgrave knows they will hand over all of their saved money, $C_i$. Once he gets money from them, he moves on to the next house. However, the superheroes are cunning; when Killgrave comes to house $\mbox{X}$, every neighbor immediately connected to house $\mbox{X}$ by a single road skips town for a couple of days (making it impossible for Killgrave to get money from them). In other words, after Killgrave visits all the superheroes he wants, there will be no road in which he was able to get money from both houses on either end of the road.
What is the maximum amount of money Killgrave can collect from the superheroes, and how many different ways can Killgrave get that amount of money? Two ways are considered to be different if the sets of visited houses are different.
Note: Killgrave can start at an arbitrary house and doesn't have to only use the roads.
Input Format
The first line contains two space-separated integers, $N$ (the number of houses) and $\mbox{M}$ (the number of roads), respectively.
The second line contains $N$ space-separated integers, where each integer $\boldsymbol{i}$ describes the amount of money, $C_i$, at house $\boldsymbol{i}$.
Each line $j$ of the $\mbox{M}$ subsequent lines contains two space-separated integers defining a road connecting houses $A_j$ and $B_j$. Every road connects a different pair of houses.
Constraints
$1\leq N\leq34$
$0\leq M\leq N\cdot\frac{(N-1)}{2}$
$0\leq C_i\leq100$
$1\leq A_j,B_j\leq N$, where $A_j\neq B_j$
No unordered pair $(A_j,B_j)$ will appear more than once.
Output Format
Print two space-separated integers:
The first integer must denote the maximum amount of money Killgrave can get out of the Justice League.
The second integer must denote the number of different ways he can collect that amount of money.
Sample Input
3 2
6 8 2
1 2
3 2
Sample Output
8 2
Explanation
Killgrave has two possible courses of action:
Visit house $2$ and get $8$ dollars.
Visit houses $1$ and $3$ and get $2+6=8$ dollars.
Both of these options result in $8$ dollars, so we know that this is maximal. Thus, we print the maximum amount of money ($8$) followed by the number of ways he can get that amount of money ($2$) as two space-separated values on a single line.
|
{"inputs": ["3 2\n6 8 2\n1 2\n3 2\n"], "outputs": ["8 2\n"]}
| 708
| 32
|
coding
|
Solve the programming task below in a Python markdown code block.
Balph is learning to play a game called Buma. In this game, he is given a row of colored balls. He has to choose the color of one new ball and the place to insert it (between two balls, or to the left of all the balls, or to the right of all the balls).
When the ball is inserted the following happens repeatedly: if some segment of balls of the same color became longer as a result of a previous action and its length became at least 3, then all the balls of this segment are eliminated.
Consider, for example, a row of balls 'AAABBBWWBB'. Suppose Balph chooses a ball of color 'W' and the place to insert it after the sixth ball, i. e. to the left of the two 'W's. After Balph inserts this ball, the balls of color 'W' are eliminated, since this segment was made longer and has length 3 now, so the row becomes 'AAABBBBB'. The balls of color 'B' are eliminated now, because the segment of balls of color 'B' became longer and has length 5 now. Thus, the row becomes 'AAA'. However, none of the balls are eliminated now, because there is no elongated segment.
Help Balph count the number of possible ways to choose a color of a new ball and a place to insert it that leads to the elimination of all the balls.
Input
The only line contains a non-empty string of uppercase English letters of length at most 3 ⋅ 10^5. Each letter represents a ball with the corresponding color.
Output
Output the number of ways to choose a color and a position of a new ball in order to eliminate all the balls.
Examples
Input
BBWWBB
Output
3
Input
BWWB
Output
0
Input
BBWBB
Output
0
Input
OOOWWW
Output
0
Input
WWWOOOOOOWWW
Output
7
|
{"inputs": ["A\n", "B\n", "C\n", "D\n", "@\n", "E\n", "F\n", "G\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
| 424
| 70
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Chef decided to write an infinite sequence. Initially, he wrote $0$, and then he started repeating the following process:
Look at the last element written so far (the $l$-th element if the sequence has length $l$ so far); let's denote it by $x$.
If $x$ does not occur anywhere earlier in the sequence, the next element in the sequence is $0$.
Otherwise, look at the previous occurrence of $x$ in the sequence, i.e. the $k$-th element, where $k < l$, this element is equal to $x$ and all elements between the $k+1$-th and $l-1$-th are different from $x$. The next element is $l-k$, i.e. the distance between the last two occurrences of $x$.
The resulting sequence is $(0, 0, 1, 0, 2, 0, 2, 2, 1, \ldots)$: the second element is $0$ since $0$ occurs only once in the sequence $(0)$, the third element is $1$ since the distance between the two occurrences of $0$ in the sequence $(0, 0)$ is $1$, the fourth element is $0$ since $1$ occurs only once in the sequence $(0, 0, 1)$, and so on.
Chef has given you a task to perform. Consider the $N$-th element of the sequence (denoted by $x$) and the first $N$ elements of the sequence. Find the number of occurrences of $x$ among these $N$ elements.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains a single integer $N$.
------ Output ------
For each test case, print a single line containing one integer ― the number of occurrences of the $N$-th element.
------ Constraints ------
$1 ≤ T ≤ 128$
$1 ≤ N ≤ 128$
------ Subtasks ------
Subtask #1 (30 points): $1 ≤ N ≤ 16$
Subtask #2 (70 points): $1 ≤ N ≤ 128$
----- Sample Input 1 ------
1
2
----- Sample Output 1 ------
2
----- explanation 1 ------
Example case 1: The $2$-nd element is $0$. It occurs twice among the first two elements, since the first two elements are both $0$.
|
{"inputs": ["1\n2"], "outputs": ["2"]}
| 604
| 14
|
coding
|
Solve the programming task below in a Python markdown code block.
Our Chef is doing what he is best at, COOKING A BARBECUE for his guests. He has invited all of us, and taking the help of his apprentice to smoke the barbecues. The chef has got BBQ sticks, each can take N fillings, and he presents N distinctly filled sticks in front his guests forming a N*N matrix
But here is the problem, he has got only two type of fillings, meat and capsicum, but still wants the N sticks to look "presentable", he is very particular about it. As a solution he fills the main diagonal of the N*N matrix with the same type of filling (either meat or capsicum) forming a "presentable" set
The Chef's apprentice is a fool, so the Chef asks him to cook M distinctly filled sticks ,so that the Chef is sure that among M there exist N sticks forming a "presentable" set. Your job is to determine smallest possible value of M.
-----Input-----
T, the number of test cases, followed by T lines.
Each line containing the positive integer N >= 4
-----Output-----
T lines of output, each line contain the positive integer M
-----Example-----
Input:
1
4
Output:
5
|
{"inputs": ["1\n4"], "outputs": ["5"]}
| 272
| 14
|
coding
|
Solve the programming task below in a Python markdown code block.
You have n problems. You have estimated the difficulty of the i-th one as integer c_{i}. Now you want to prepare a problemset for a contest, using some of the problems you've made.
A problemset for the contest must consist of at least two problems. You think that the total difficulty of the problems of the contest must be at least l and at most r. Also, you think that the difference between difficulties of the easiest and the hardest of the chosen problems must be at least x.
Find the number of ways to choose a problemset for the contest.
-----Input-----
The first line contains four integers n, l, r, x (1 ≤ n ≤ 15, 1 ≤ l ≤ r ≤ 10^9, 1 ≤ x ≤ 10^6) — the number of problems you have, the minimum and maximum value of total difficulty of the problemset and the minimum difference in difficulty between the hardest problem in the pack and the easiest one, respectively.
The second line contains n integers c_1, c_2, ..., c_{n} (1 ≤ c_{i} ≤ 10^6) — the difficulty of each problem.
-----Output-----
Print the number of ways to choose a suitable problemset for the contest.
-----Examples-----
Input
3 5 6 1
1 2 3
Output
2
Input
4 40 50 10
10 20 30 25
Output
2
Input
5 25 35 10
10 10 20 10 20
Output
6
-----Note-----
In the first example two sets are suitable, one consisting of the second and third problem, another one consisting of all three problems.
In the second example, two sets of problems are suitable — the set of problems with difficulties 10 and 30 as well as the set of problems with difficulties 20 and 30.
In the third example any set consisting of one problem of difficulty 10 and one problem of difficulty 20 is suitable.
|
{"inputs": ["1 10 6 1\n15\n", "1 10 6 1\n17\n", "1 10 6 2\n17\n", "1 10 20 1\n15\n", "1 10 20 1\n15\n", "3 5 6 1\n1 2 3\n", "3 5 8 1\n1 2 3\n", "3 5 8 1\n1 2 0\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "2\n", "2\n", "0\n"]}
| 459
| 158
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given an array target that consists of distinct integers and another integer array arr that can have duplicates.
In one operation, you can insert any integer at any position in arr. For example, if arr = [1,4,1,2], you can add 3 in the middle and make it [1,4,3,1,2]. Note that you can insert the integer at the very beginning or end of the array.
Return the minimum number of operations needed to make target a subsequence of arr.
A subsequence of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the remaining elements' relative order. For example, [2,7,4] is a subsequence of [4,2,3,7,2,1,4] (the underlined elements), while [2,4,2] is not.
Please complete the following python code precisely:
```python
class Solution:
def minOperations(self, target: List[int], arr: List[int]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(target = [5,1,3], arr = [9,4,2,3,4]) == 2\n assert candidate(target = [6,4,8,1,3,2], arr = [4,7,6,2,3,8,6,1]) == 3\n\n\ncheck(Solution().minOperations)"}
| 238
| 89
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given two strings s1 and s2 of equal length consisting of letters "x" and "y" only. Your task is to make these two strings equal to each other. You can swap any two characters that belong to different strings, which means: swap s1[i] and s2[j].
Return the minimum number of swaps required to make s1 and s2 equal, or return -1 if it is impossible to do so.
Please complete the following python code precisely:
```python
class Solution:
def minimumSwap(self, s1: str, s2: str) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(s1 = \"xx\", s2 = \"yy\") == 1\n assert candidate(s1 = \"xy\", s2 = \"yx\") == 2\n assert candidate(s1 = \"xx\", s2 = \"xy\") == -1\n\n\ncheck(Solution().minimumSwap)"}
| 144
| 76
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given the root of a Binary Search Tree (BST), return the minimum absolute difference between the values of any two different nodes in the tree.
Please complete the following python code precisely:
```python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def getMinimumDifference(self, root: Optional[TreeNode]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(root = tree_node([4,2,6,1,3])) == 1\n assert candidate(root = tree_node([1,0,48,None,None,12,49])) == 1\n\n\ncheck(Solution().getMinimumDifference)"}
| 129
| 69
|
coding
|
Solve the programming task below in a Python markdown code block.
When a warrior wants to talk with another one about peace or war he uses a smartphone. In one distinct country warriors who spent all time in training kata not always have enough money. So if they call some number they want to know which operator serves this number.
Write a function which **accepts number and return name of operator or string "no info"**, if operator can't be defined. number always looks like 8yyyxxxxxxx, where yyy corresponds to operator.
Here is short list of operators:
* 039 xxx xx xx - Golden Telecom
* 050 xxx xx xx - MTS
* 063 xxx xx xx - Life:)
* 066 xxx xx xx - MTS
* 067 xxx xx xx - Kyivstar
* 068 xxx xx xx - Beeline
* 093 xxx xx xx - Life:)
* 095 xxx xx xx - MTS
* 096 xxx xx xx - Kyivstar
* 097 xxx xx xx - Kyivstar
* 098 xxx xx xx - Kyivstar
* 099 xxx xx xx - MTS Test [Just return "MTS"]
Also feel free to reuse/extend the following starter code:
```python
def detect_operator(num):
```
|
{"functional": "_inputs = [['80661111841'], ['80671991111'], ['80631551111'], ['80931551111'], ['80111551111']]\n_outputs = [['MTS'], ['Kyivstar'], ['Life:)'], ['Life:)'], ['no info']]\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(detect_operator(*i), o[0])"}
| 294
| 235
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given an n x n matrix where each of the rows and columns is sorted in ascending order, return the kth smallest element in the matrix.
Note that it is the kth smallest element in the sorted order, not the kth distinct element.
You must find a solution with a memory complexity better than O(n2).
Please complete the following python code precisely:
```python
class Solution:
def kthSmallest(self, matrix: List[List[int]], k: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8) == 13\n assert candidate(matrix = [[-5]], k = 1) == -5\n\n\ncheck(Solution().kthSmallest)"}
| 120
| 79
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a string $s$ consisting of $n$ lowercase Latin letters.
Let's define a substring as a contiguous subsegment of a string. For example, "acab" is a substring of "abacaba" (it starts in position $3$ and ends in position $6$), but "aa" or "d" aren't substrings of this string. So the substring of the string $s$ from position $l$ to position $r$ is $s[l; r] = s_l s_{l + 1} \dots s_r$.
You have to choose exactly one of the substrings of the given string and reverse it (i. e. make $s[l; r] = s_r s_{r - 1} \dots s_l$) to obtain a string that is less lexicographically. Note that it is not necessary to obtain the minimum possible string.
If it is impossible to reverse some substring of the given string to obtain a string that is less, print "NO". Otherwise print "YES" and any suitable substring.
String $x$ is lexicographically less than string $y$, if either $x$ is a prefix of $y$ (and $x \ne y$), or there exists such $i$ ($1 \le i \le min(|x|, |y|)$), that $x_i < y_i$, and for any $j$ ($1 \le j < i$) $x_j = y_j$. Here $|a|$ denotes the length of the string $a$. The lexicographic comparison of strings is implemented by operator < in modern programming languages.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 3 \cdot 10^5$) — the length of $s$.
The second line of the input contains the string $s$ of length $n$ consisting only of lowercase Latin letters.
-----Output-----
If it is impossible to reverse some substring of the given string to obtain a string which is lexicographically less, print "NO". Otherwise print "YES" and two indices $l$ and $r$ ($1 \le l < r \le n$) denoting the substring you have to reverse. If there are multiple answers, you can print any.
-----Examples-----
Input
7
abacaba
Output
YES
2 5
Input
6
aabcfg
Output
NO
-----Note-----
In the first testcase the resulting string is "aacabba".
|
{"inputs": ["2\nba\n", "2\naa\n", "2\nba\n", "2\naa\n", "2\nab\n", "2\nb`\n", "2\nb_\n", "2\nac\n"], "outputs": ["YES\n1 2\n", "NO\n", "YES\n1 2\n", "NO\n", "NO\n", "YES\n1 2\n", "YES\n1 2\n", "NO\n"]}
| 547
| 104
|
coding
|
Solve the programming task below in a Python markdown code block.
We have a tree with N vertices. Vertex 1 is the root of the tree, and the parent of Vertex i (2 \leq i \leq N) is Vertex P_i.
To each vertex in the tree, Snuke will allocate a color, either black or white, and a non-negative integer weight.
Snuke has a favorite integer sequence, X_1, X_2, ..., X_N, so he wants to allocate colors and weights so that the following condition is satisfied for all v.
- The total weight of the vertices with the same color as v among the vertices contained in the subtree whose root is v, is X_v.
Here, the subtree whose root is v is the tree consisting of Vertex v and all of its descendants.
Determine whether it is possible to allocate colors and weights in this way.
-----Constraints-----
- 1 \leq N \leq 1 000
- 1 \leq P_i \leq i - 1
- 0 \leq X_i \leq 5 000
-----Inputs-----
Input is given from Standard Input in the following format:
N
P_2 P_3 ... P_N
X_1 X_2 ... X_N
-----Outputs-----
If it is possible to allocate colors and weights to the vertices so that the condition is satisfied, print POSSIBLE; otherwise, print IMPOSSIBLE.
-----Sample Input-----
3
1 1
4 3 2
-----Sample Output-----
POSSIBLE
For example, the following allocation satisfies the condition:
- Set the color of Vertex 1 to white and its weight to 2.
- Set the color of Vertex 2 to black and its weight to 3.
- Set the color of Vertex 3 to white and its weight to 2.
There are also other possible allocations.
|
{"inputs": ["1\n\n1", "1\n\n2", "1\n\n3", "1\n\n6", "1\n\n9", "1\n\n4", "1\n\n5", "1\n\n8"], "outputs": ["POSSIBLE\n", "POSSIBLE\n", "POSSIBLE\n", "POSSIBLE\n", "POSSIBLE\n", "POSSIBLE\n", "POSSIBLE\n", "POSSIBLE\n"]}
| 401
| 102
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Two players play a turn based game on a binary tree. We are given the root of this binary tree, and the number of nodes n in the tree. n is odd, and each node has a distinct value from 1 to n.
Initially, the first player names a value x with 1 <= x <= n, and the second player names a value y with 1 <= y <= n and y != x. The first player colors the node with value x red, and the second player colors the node with value y blue.
Then, the players take turns starting with the first player. In each turn, that player chooses a node of their color (red if player 1, blue if player 2) and colors an uncolored neighbor of the chosen node (either the left child, right child, or parent of the chosen node.)
If (and only if) a player cannot choose such a node in this way, they must pass their turn. If both players pass their turn, the game ends, and the winner is the player that colored more nodes.
You are the second player. If it is possible to choose such a y to ensure you win the game, return true. If it is not possible, return false.
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 btreeGameWinningMove(self, root: Optional[TreeNode], n: int, x: int) -> bool:
```
|
{"functional": "def check(candidate):\n assert candidate(root = tree_node([1,2,3,4,5,6,7,8,9,10,11]), n = 11, x = 3) == True\n assert candidate(root = tree_node([1,2,3]), n = 3, x = 1) == False\n\n\ncheck(Solution().btreeGameWinningMove)"}
| 364
| 96
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problems statements in Mandarin Chinese and Russian as well.
Andrii is a great programmer. He recently managed to enter Ukrainian IOI team. His parents wanted to appreciate him and present him a DAG(Directed Acyclic Graph). His favourite TV show is "Pimp My Ride" so he'd like to customize his graph. He'd like to add as many edges as possible in order to still have a DAG. Please, help Andrii, find the maximum number of edges he can add to his graph without obtaining any cycle.
------ Input ------
The first line of an input contains single integer N — the number of vertices in the graph. Vertices are numerated from 1 to N. Next N lines contain N characters each — adjacency matrix of the graph. If there is '1' in j^{th} character of the i^{th} line then there is an edge from vertex i to vertex j in Andrii's graph. It's guaranteed that given graph does not contain cycles.
------ Output ------
Output the maximal number of edges one can add to the graph in the first line. Then output the edges themselves. Edges should be written in separate lines, one by one. Edge is defined by a pair of integers a and b which means that edge from a to b should be added.
The author is pretty lazy and he does not want to write checking program. So, if there are multiple solutions which lead to maximal number of edges that can be added, then you should output lexicographically smallest sequence of edges. A sequence of edges is smaller than another if the first edge that they differ in is lexicographically smaller. Edges are compared as pairs of integers, i.e. as sequence of two integers.
------ Constraints ------
$1 ≤ N ≤ 1500$
------ Example ------
Input:
3
010
000
000
Output:
2
1 3
2 3
|
{"inputs": ["3\n010\n000\n000"], "outputs": ["2\n1 3\n2 3"]}
| 420
| 32
|
coding
|
Solve the programming task below in a Python markdown code block.
The National Championships are starting soon. There are 4 race categories, numbered from 1 to 4, that Chef is interested in. Chef is participating in exactly 2 of these categories.
Chef has an arch-rival who is, unfortunately, the only person participating who is better than Chef, i.e, Chef can't defeat the arch-rival in any of the four race categories but can defeat anyone else. Chef's arch-rival is also participating in exactly 2 of the four categories.
Chef hopes to not fall into the same categories as that of the arch-rival.
Given X, Y, A, B where X, Y are the races that Chef participates in, and A, B are the races that Chef's arch-rival participates in, find the maximum number of gold medals (first place) that Chef can win.
------ Input Format ------
- The first line of input contains an integer T, denoting the number of testcases. The description of T testcases follows.
- Each testcase consists of a single line containing four space-separated integers — the values of X, Y, A, and B respectively.
------ Output Format ------
- For each testcase, print a single line containing one integer — the maximum number of gold medals that Chef can win.
------ Constraints ------
$1 ≤ T ≤ 144$
$1 ≤ X, Y, A, B ≤ 4$
$X \neq Y$
$A \neq B$
------ subtasks ------
Subtask #1 (100 points): Original constraints
----- Sample Input 1 ------
3
4 3 1 2
4 2 1 2
2 1 1 2
----- Sample Output 1 ------
2
1
0
----- explanation 1 ------
Test case $1$: Chef participates in the races $4, 3$, whereas Chef's rival participates in $1, 2$. As Chef's only rival does not participate in any of the races that Chef takes part in, Chef can win the gold medal in both of the races, thus the answer is $2$.
Test case $2$: Chef participates in the races $4, 2$, whereas Chef's rival participates in $1, 2$. Chef cannot win race $2$ as Chef will be beaten by the arch-rival, however Chef can win the gold medal for race $4$. Thus the answer is $1$.
Test case $3$: Chef participates in the races $2, 1$, whereas Chef's rival participates in $1, 2$. Chef will be beaten by the arch-rival in both races, thus the answer is $0$.
|
{"inputs": ["3\n4 3 1 2\n4 2 1 2\n2 1 1 2"], "outputs": ["2\n1\n0"]}
| 566
| 40
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given an integer n, return the count of all numbers with unique digits, x, where 0 <= x < 10n.
Please complete the following python code precisely:
```python
class Solution:
def countNumbersWithUniqueDigits(self, n: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(n = 2) == 91\n assert candidate(n = 0) == 1\n\n\ncheck(Solution().countNumbersWithUniqueDigits)"}
| 78
| 47
|
coding
|
Solve the programming task below in a Python markdown code block.
Given is a tree with N vertices numbered 1 to N, and N-1 edges numbered 1 to N-1.
Edge i connects Vertex a_i and b_i bidirectionally and has a length of 1.
Snuke will paint each vertex white or black.
The niceness of a way of painting the graph is \max(X, Y), where X is the maximum among the distances between white vertices, and Y is the maximum among the distances between black vertices.
Here, if there is no vertex of one color, we consider the maximum among the distances between vertices of that color to be 0.
There are 2^N ways of painting the graph. Compute the sum of the nicenesses of all those ways, modulo (10^{9}+7).
-----Constraints-----
- 2 \leq N \leq 2 \times 10^{5}
- 1 \leq a_i, b_i \leq N
- The given graph is a tree.
-----Input-----
Input is given from Standard Input in the following format:
N
a_1 b_1
\vdots
a_{N-1} b_{N-1}
-----Output-----
Print the sum of the nicenesses of the ways of painting the graph, modulo (10^{9}+7).
-----Sample Input-----
2
1 2
-----Sample Output-----
2
- If we paint Vertex 1 and 2 the same color, the niceness will be 1; if we paint them different colors, the niceness will be 0.
- The sum of those nicenesses is 2.
|
{"inputs": ["2\n1 2\n", "2\n2 1\n", "2\n2 1\n", "6\n1 2\n2 3\n3 4\n4 5\n3 6\n", "35\n25 4\n33 7\n11 26\n32 4\n12 7\n31 27\n19 6\n10 22\n17 12\n28 24\n28 1\n24 15\n30 24\n24 11\n23 18\n14 15\n4 29\n33 24\n15 34\n11 3\n4 35\n5 34\n34 2\n16 19\n7 18\n19 31\n22 8\n13 26\n20 6\n20 9\n4 33\n4 8\n29 19\n15 21\n"], "outputs": ["2\n", "2\n", "2\n", "224\n", "298219707\n"]}
| 354
| 275
|
coding
|
Solve the programming task below in a Python markdown code block.
A dial lock is a kind of lock which has some dials with printed numbers. It has a special sequence of numbers, namely an unlocking sequence, to be opened.
You are working at a manufacturer of dial locks. Your job is to verify that every manufactured lock is unlocked by its unlocking sequence. In other words, you have to rotate many dials of many many locks.
It’s a very hard and boring task. You want to reduce time to open the locks. It’s a good idea to rotate multiple dials at one time. It is, however, a difficult problem to find steps to open a given lock with the fewest rotations. So you decided to write a program to find such steps for given initial and unlocking sequences.
Your company’s dial locks are composed of vertically stacked k (1 ≤ k ≤ 10) cylindrical dials. Every dial has 10 numbers, from 0 to 9, along the side of the cylindrical shape from the left to the right in sequence. The neighbor on the right of 9 is 0.
A dial points one number at a certain position. If you rotate a dial to the left by i digits, the dial newly points the i-th right number. In contrast, if you rotate a dial to the right by i digits, it points the i-th left number. For example, if you rotate a dial pointing 8 to the left by 3 digits, the dial newly points 1.
You can rotate more than one adjacent dial at one time. For example, consider a lock with 5 dials. You can rotate just the 2nd dial. You can rotate the 3rd, 4th and 5th dials at the same time. But you cannot rotate the 1st and 3rd dials at one time without rotating the 2nd dial. When you rotate multiple dials, you have to rotate them to the same direction by the same digits.
Your program is to calculate the fewest number of rotations to unlock, for given initial and unlocking sequences. Rotating one or more adjacent dials to the same direction by the same digits is counted as one rotation.
Input
The input consists of multiple datasets. Each datset consists of two lines. The first line contains an integer k. The second lines contain two strings, separated by a space, which indicate the initial and unlocking sequences.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print the minimum number of rotations in one line.
Example
Input
4
1357 4680
6
777777 003330
0
Output
1
2
|
{"inputs": ["4\n1357 1908\n0\n509138 5249\n0", "4\n1357 1908\n0\n509138 1663\n0", "4\n1357 2529\n0\n509138 1663\n0", "4\n1155 2529\n0\n509138 1663\n0", "4\n1155 2529\n0\n463161 1663\n0", "4\n5501 3524\n0\n829953 5963\n0", "4\n1772 9224\n0\n637757 358\n-1", "4\n1772 7811\n0\n637757 358\n-1"], "outputs": ["3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n"]}
| 582
| 270
|
coding
|
Solve the programming task below in a Python markdown code block.
Bear Limak prepares problems for a programming competition. Of course, it would be unprofessional to mention the sponsor name in the statement. Limak takes it seriously and he is going to change some words. To make it still possible to read, he will try to modify each word as little as possible.
Limak has a string s that consists of uppercase English letters. In one move he can swap two adjacent letters of the string. For example, he can transform a string "ABBC" into "BABC" or "ABCB" in one move.
Limak wants to obtain a string without a substring "VK" (i.e. there should be no letter 'V' immediately followed by letter 'K'). It can be easily proved that it's possible for any initial string s.
What is the minimum possible number of moves Limak can do?
Input
The first line of the input contains an integer n (1 ≤ n ≤ 75) — the length of the string.
The second line contains a string s, consisting of uppercase English letters. The length of the string is equal to n.
Output
Print one integer, denoting the minimum possible number of moves Limak can do, in order to obtain a string without a substring "VK".
Examples
Input
4
VKVK
Output
3
Input
5
BVVKV
Output
2
Input
7
VVKEVKK
Output
3
Input
20
VKVKVVVKVOVKVQKKKVVK
Output
8
Input
5
LIMAK
Output
0
Note
In the first sample, the initial string is "VKVK". The minimum possible number of moves is 3. One optimal sequence of moves is:
1. Swap two last letters. The string becomes "VKKV".
2. Swap first two letters. The string becomes "KVKV".
3. Swap the second and the third letter. The string becomes "KKVV". Indeed, this string doesn't have a substring "VK".
In the second sample, there are two optimal sequences of moves. One is "BVVKV" → "VBVKV" → "VVBKV". The other is "BVVKV" → "BVKVV" → "BKVVV".
In the fifth sample, no swaps are necessary.
|
{"inputs": ["1\nZ\n", "1\nV\n", "1\nK\n", "1\nY\n", "1\nU\n", "2\nKV\n", "2\nVK\n", "2\nKU\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "1\n", "0\n"]}
| 499
| 87
|
coding
|
Solve the programming task below in a Python markdown code block.
Chef has two binary strings A and B, each of length N. He can perform the following operation on A any number of times:
Choose L and R (1 ≤ L ≤ R ≤ N), such that, in the [substring] A[L,R], the number of 1s is not equal to the number of 0s and reverse the substring A[L,R].
Find whether Chef can convert the string A into the string B by performing the above operation any (possibly zero) number of times on A.
------ Input Format ------
- The first line of the input contains a single integer T, the number of test cases. The descriptions of the test cases follow.
- The first line of each test case contains one integer N, the length of the binary strings.
- The second line of each test case contains the string A.
- The third line of each test case contains the string B.
------ Output Format ------
For each test case, print \texttt{YES} if it is possible to convert the string A into the string B by performing any (possibly zero) number of given operations on A. Otherwise, print \texttt{NO}.
You may print each character of the string in uppercase or lowercase (for example, the strings \texttt{YeS}, \texttt{yEs}, \texttt{yes} and \texttt{YES} will all be treated as identical).
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$1 ≤ N ≤ 10^{5}$
$|A|=|B|=N$
- The sum of $N$ over all test cases does not exceed $10^{5}$.
----- Sample Input 1 ------
3
2
11
00
5
10110
11001
2
10
01
----- Sample Output 1 ------
NO
YES
NO
----- explanation 1 ------
Test Case $1$: It is impossible to convert $11$ to $00$ by performing any number of operations.
Test Case $2$: We can convert the string $A$ into string $B$ as:
- Operation $1$: Choose $L = 2$ and $R = 4$. Hence, the chosen substring is $1{\underline{011}}0$. On reversing this substring, we get $11100$.
- Operation $2$: Choose $L = 3$ and $R = 5$. Hence, the chosen substring is $11{\underline{100}}$. On reversing it, we get $11001$.
Note that the number of $1$s is not equal to the number of $0$s in the chosen substrings.
Test Case $3$: It is impossible to convert $10$ to $01$ by performing any number of operations.
|
{"inputs": ["3\n2\n11\n00\n5\n10110\n11001\n2\n10\n01"], "outputs": ["NO\nYES\nNO"]}
| 616
| 46
|
coding
|
Solve the programming task below in a Python markdown code block.
Winter is here at the North and the White Walkers are close. John Snow has an army consisting of n soldiers. While the rest of the world is fighting for the Iron Throne, he is going to get ready for the attack of the White Walkers.
He has created a method to know how strong his army is. Let the i-th soldier’s strength be a_{i}. For some k he calls i_1, i_2, ..., i_{k} a clan if i_1 < i_2 < i_3 < ... < i_{k} and gcd(a_{i}_1, a_{i}_2, ..., a_{i}_{k}) > 1 . He calls the strength of that clan k·gcd(a_{i}_1, a_{i}_2, ..., a_{i}_{k}). Then he defines the strength of his army by the sum of strengths of all possible clans.
Your task is to find the strength of his army. As the number may be very large, you have to print it modulo 1000000007 (10^9 + 7).
Greatest common divisor (gcd) of a sequence of integers is the maximum possible integer so that each element of the sequence is divisible by it.
-----Input-----
The first line contains integer n (1 ≤ n ≤ 200000) — the size of the army.
The second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 1000000) — denoting the strengths of his soldiers.
-----Output-----
Print one integer — the strength of John Snow's army modulo 1000000007 (10^9 + 7).
-----Examples-----
Input
3
3 3 1
Output
12
Input
4
2 3 4 6
Output
39
-----Note-----
In the first sample the clans are {1}, {2}, {1, 2} so the answer will be 1·3 + 1·3 + 2·3 = 12
|
{"inputs": ["3\n3 3 1\n", "3\n3 4 1\n", "3\n6 4 1\n", "3\n6 6 1\n", "3\n2 4 1\n", "3\n4 3 2\n", "3\n2 6 1\n", "3\n6 9 1\n"], "outputs": ["12\n", "7\n", "14\n", "24\n", "10\n", "13\n", "12\n", "21\n"]}
| 467
| 125
|
coding
|
Solve the programming task below in a Python markdown code block.
```
*************************
* Create a frame! *
* __ __ *
* / \~~~/ \ *
* ,----( .. ) *
* / \__ __/ *
* /| (\ |( *
* ^ \ /___\ /\ | *
* |__| |__|-.. *
*************************
```
Given an array of strings and a character to be used as border, output the frame with the content inside.
Notes:
* Always keep a space between the input string and the left and right borders.
* The biggest string inside the array should always fit in the frame.
* The input array is never empty.
## Example
`frame(['Create', 'a', 'frame'], '+')`
Output:
```
++++++++++
+ Create +
+ a +
+ frame +
++++++++++
```
Also feel free to reuse/extend the following starter code:
```python
def frame(text, char):
```
|
{"functional": "_inputs = [[['Small', 'frame'], '~'], [['Create', 'this', 'kata'], '+'], [['This is a very long single frame'], '-']]\n_outputs = [['~~~~~~~~~\\n~ Small ~\\n~ frame ~\\n~~~~~~~~~'], ['++++++++++\\n+ Create +\\n+ this +\\n+ kata +\\n++++++++++'], ['------------------------------------\\n- This is a very long single frame -\\n------------------------------------']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(frame(*i), o[0])"}
| 226
| 241
|
coding
|
Solve the programming task below in a Python markdown code block.
There are $N$ cars (numbered $1$ through $N$) on a circular track with length $N$. For each $i$ ($2 \le i \le N$), the $i$-th of them is at a distance $i-1$ clockwise from car $1$, i.e. car $1$ needs to travel a distance $i-1$ clockwise to reach car $i$. Also, for each valid $i$, the $i$-th car has $f_i$ litres of gasoline in it initially.
You are driving car $1$ in the clockwise direction. To move one unit of distance in this direction, you need to spend $1$ litre of gasoline. When you pass another car (even if you'd run out of gasoline exactly at that point), you steal all its gasoline. Once you do not have any gasoline left, you stop.
What is the total clockwise distance travelled by your car?
-----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 $f_1, f_2, \ldots, f_N$.
-----Output-----
For each test case, print a single line containing one integer ― the total clockwise distance travelled.
-----Constraints-----
- $1 \le T \le 100$
- $1 \le N \le 100$
- $0 \le f_i \le 100$ for each valid $i$
-----Subtasks-----
Subtask #1 (100 points): original constraints
-----Example Input-----
3
5
3 0 0 0 0
5
1 1 1 1 1
5
5 4 3 2 1
-----Example Output-----
3
5
15
|
{"inputs": ["3\n5\n3 0 0 0 0\n5\n1 1 1 1 1\n5\n5 4 3 2 1"], "outputs": ["3\n5\n15"]}
| 429
| 53
|
coding
|
Solve the programming task below in a Python markdown code block.
Background
There is a message that is circulating via public media that claims a reader can easily read a message where the inner letters of each words is scrambled, as long as the first and last letters remain the same and the word contains all the letters.
Another example shows that it is quite difficult to read the text where all the letters are reversed rather than scrambled.
In this kata we will make a generator that generates text in a similar pattern, but instead of scrambled or reversed, ours will be sorted alphabetically
Requirement
return a string where:
1) the first and last characters remain in original place for each word
2) characters between the first and last characters must be sorted alphabetically
3) punctuation should remain at the same place as it started, for example: shan't -> sahn't
Assumptions
1) words are seperated by single spaces
2) only spaces separate words, special characters do not, for example: tik-tak -> tai-ktk
3) special characters do not take the position of the non special characters, for example: -dcba -> -dbca
4) for this kata puctuation is limited to 4 characters: hyphen(-), apostrophe('), comma(,) and period(.)
5) ignore capitalisation
for reference: http://en.wikipedia.org/wiki/Typoglycemia
Also feel free to reuse/extend the following starter code:
```python
def scramble_words(words):
```
|
{"functional": "_inputs = [['professionals'], ['i'], [''], ['me'], ['you'], ['card-carrying'], [\"shan't\"], ['-dcba'], ['dcba.'], [\"you've gotta dance like there's nobody watching, love like you'll never be hurt, sing like there's nobody listening, and live like it's heaven on earth.\"]]\n_outputs = [['paefilnoorsss'], ['i'], [''], ['me'], ['you'], ['caac-dinrrryg'], [\"sahn't\"], ['-dbca'], ['dbca.'], [\"you've gotta dacne like teehr's nbdooy wachintg, love like ylo'ul neevr be hrut, sing like teehr's nbdooy leiinnstg, and live like it's haeevn on earth.\"]]\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(scramble_words(*i), o[0])"}
| 316
| 324
|
coding
|
Solve the programming task below in a Python markdown code block.
In winter, the inhabitants of the Moscow Zoo are very bored, in particular, it concerns gorillas. You decided to entertain them and brought a permutation $p$ of length $n$ to the zoo.
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in any order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ occurs twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$, but $4$ is present in the array).
The gorillas had their own permutation $q$ of length $n$. They suggested that you count the number of pairs of integers $l, r$ ($1 \le l \le r \le n$) such that $\operatorname{MEX}([p_l, p_{l+1}, \ldots, p_r])=\operatorname{MEX}([q_l, q_{l+1}, \ldots, q_r])$.
The $\operatorname{MEX}$ of the sequence is the minimum integer positive number missing from this sequence. For example, $\operatorname{MEX}([1, 3]) = 2$, $\operatorname{MEX}([5]) = 1$, $\operatorname{MEX}([3, 1, 2, 6]) = 4$.
You do not want to risk your health, so you will not dare to refuse the gorillas.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the permutations length.
The second line contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$) — the elements of the permutation $p$.
The third line contains $n$ integers $q_1, q_2, \ldots, q_n$ ($1 \le q_i \le n$) — the elements of the permutation $q$.
-----Output-----
Print a single integer — the number of suitable pairs $l$ and $r$.
-----Examples-----
Input
3
1 3 2
2 1 3
Output
2
Input
7
7 3 6 2 1 5 4
6 7 2 5 3 1 4
Output
16
Input
6
1 2 3 4 5 6
6 5 4 3 2 1
Output
11
-----Note-----
In the first example, two segments are correct – $[1, 3]$ with $\operatorname{MEX}$ equal to $4$ in both arrays and $[3, 3]$ with $\operatorname{MEX}$ equal to $1$ in both of arrays.
In the second example, for example, the segment $[1, 4]$ is correct, and the segment $[6, 7]$ isn't correct, because $\operatorname{MEX}(5, 4) \neq \operatorname{MEX}(1, 4)$.
|
{"inputs": ["1\n1\n1\n", "3\n1 3 2\n2 1 3\n", "6\n1 2 3 4 5 6\n6 5 4 3 2 1\n", "7\n7 3 6 2 1 5 4\n6 7 2 5 3 1 4\n"], "outputs": ["1\n", "2\n", "11\n", "16\n"]}
| 715
| 108
|
coding
|
Solve the programming task below in a Python markdown code block.
Create a function that transforms any positive number to a string representing the number in words. The function should work for all numbers between 0 and 999999.
### Examples
```
number2words(0) ==> "zero"
number2words(1) ==> "one"
number2words(9) ==> "nine"
number2words(10) ==> "ten"
number2words(17) ==> "seventeen"
number2words(20) ==> "twenty"
number2words(21) ==> "twenty-one"
number2words(45) ==> "forty-five"
number2words(80) ==> "eighty"
number2words(99) ==> "ninety-nine"
number2words(100) ==> "one hundred"
number2words(301) ==> "three hundred one"
number2words(799) ==> "seven hundred ninety-nine"
number2words(800) ==> "eight hundred"
number2words(950) ==> "nine hundred fifty"
number2words(1000) ==> "one thousand"
number2words(1002) ==> "one thousand two"
number2words(3051) ==> "three thousand fifty-one"
number2words(7200) ==> "seven thousand two hundred"
number2words(7219) ==> "seven thousand two hundred nineteen"
number2words(8330) ==> "eight thousand three hundred thirty"
number2words(99999) ==> "ninety-nine thousand nine hundred ninety-nine"
number2words(888888) ==> "eight hundred eighty-eight thousand eight hundred eighty-eight"
```
Also feel free to reuse/extend the following starter code:
```python
def number2words(n):
```
|
{"functional": "_inputs = [[0], [1], [8], [5], [9], [10], [19], [20], [22], [54], [80], [98], [100], [301], [793], [800], [650], [1000], [1003], [3052], [7300], [7217], [8340], [99997], [888887]]\n_outputs = [['zero'], ['one'], ['eight'], ['five'], ['nine'], ['ten'], ['nineteen'], ['twenty'], ['twenty-two'], ['fifty-four'], ['eighty'], ['ninety-eight'], ['one hundred'], ['three hundred one'], ['seven hundred ninety-three'], ['eight hundred'], ['six hundred fifty'], ['one thousand'], ['one thousand three'], ['three thousand fifty-two'], ['seven thousand three hundred'], ['seven thousand two hundred seventeen'], ['eight thousand three hundred forty'], ['ninety-nine thousand nine hundred ninety-seven'], ['eight hundred eighty-eight thousand eight hundred eighty-seven']]\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(number2words(*i), o[0])"}
| 448
| 392
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given $n$ integer numbers $a_1, a_2, \dots, a_n$. Consider graph on $n$ nodes, in which nodes $i$, $j$ ($i\neq j$) are connected if and only if, $a_i$ AND $a_j\neq 0$, where AND denotes the bitwise AND operation.
Find the length of the shortest cycle in this graph or determine that it doesn't have cycles at all.
-----Input-----
The first line contains one integer $n$ $(1 \le n \le 10^5)$ — number of numbers.
The second line contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^{18}$).
-----Output-----
If the graph doesn't have any cycles, output $-1$. Else output the length of the shortest cycle.
-----Examples-----
Input
4
3 6 28 9
Output
4
Input
5
5 12 9 16 48
Output
3
Input
4
1 2 4 8
Output
-1
-----Note-----
In the first example, the shortest cycle is $(9, 3, 6, 28)$.
In the second example, the shortest cycle is $(5, 12, 9)$.
The graph has no cycles in the third example.
|
{"inputs": ["3\n1 1 3\n", "3\n5 5 5\n", "3\n1 1 3\n", "3\n5 5 5\n", "3\n2 1 3\n", "3\n4 5 5\n", "3\n2 2 3\n", "4\n1 2 4 8\n"], "outputs": ["3", "3", "3", "3", "-1\n", "3\n", "3\n", "-1"]}
| 322
| 115
|
coding
|
Solve the programming task below in a Python markdown code block.
Mole is hungry again. He found one ant colony, consisting of n ants, ordered in a row. Each ant i (1 ≤ i ≤ n) has a strength si.
In order to make his dinner more interesting, Mole organizes a version of «Hunger Games» for the ants. He chooses two numbers l and r (1 ≤ l ≤ r ≤ n) and each pair of ants with indices between l and r (inclusively) will fight. When two ants i and j fight, ant i gets one battle point only if si divides sj (also, ant j gets one battle point only if sj divides si).
After all fights have been finished, Mole makes the ranking. An ant i, with vi battle points obtained, is going to be freed only if vi = r - l, or in other words only if it took a point in every fight it participated. After that, Mole eats the rest of the ants. Note that there can be many ants freed or even none.
In order to choose the best sequence, Mole gives you t segments [li, ri] and asks for each of them how many ants is he going to eat if those ants fight.
Input
The first line contains one integer n (1 ≤ n ≤ 105), the size of the ant colony.
The second line contains n integers s1, s2, ..., sn (1 ≤ si ≤ 109), the strengths of the ants.
The third line contains one integer t (1 ≤ t ≤ 105), the number of test cases.
Each of the next t lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n), describing one query.
Output
Print to the standard output t lines. The i-th line contains number of ants that Mole eats from the segment [li, ri].
Examples
Input
5
1 3 2 4 2
4
1 5
2 5
3 5
4 5
Output
4
4
1
1
Note
In the first test battle points for each ant are v = [4, 0, 2, 0, 2], so ant number 1 is freed. Mole eats the ants 2, 3, 4, 5.
In the second test case battle points are v = [0, 2, 0, 2], so no ant is freed and all of them are eaten by Mole.
In the third test case battle points are v = [2, 0, 2], so ants number 3 and 5 are freed. Mole eats only the ant 4.
In the fourth test case battle points are v = [0, 1], so ant number 5 is freed. Mole eats the ant 4.
|
{"inputs": ["5\n1 5 2 4 2\n4\n1 5\n2 5\n3 5\n4 5\n", "5\n1 3 4 4 2\n4\n1 5\n2 5\n3 5\n4 5\n", "5\n1 3 4 4 2\n4\n1 5\n2 5\n3 5\n2 5\n", "5\n1 3 4 4 2\n4\n1 5\n2 5\n1 5\n4 5\n", "5\n1 3 4 4 2\n4\n1 5\n2 5\n1 5\n3 5\n", "5\n1 3 4 4 2\n4\n1 3\n2 5\n1 5\n3 5\n", "5\n1 3 4 2 2\n4\n1 5\n2 5\n3 5\n2 5\n", "5\n1 3 4 4 2\n4\n1 5\n3 5\n1 5\n4 5\n"], "outputs": ["4\n4\n1\n1\n", "4\n4\n2\n1\n", "4\n4\n2\n4\n", "4\n4\n4\n1\n", "4\n4\n4\n2\n", "2\n4\n4\n2\n", "4\n4\n1\n4\n", "4\n2\n4\n1\n"]}
| 602
| 342
|
coding
|
Solve the programming task below in a Python markdown code block.
Catherine received an array of integers as a gift for March 8. Eventually she grew bored with it, and she started calculated various useless characteristics for it. She succeeded to do it for each one she came up with. But when she came up with another one — xor of all pairwise sums of elements in the array, she realized that she couldn't compute it for a very large array, thus she asked for your help. Can you do it? Formally, you need to compute
$$ (a_1 + a_2) \oplus (a_1 + a_3) \oplus \ldots \oplus (a_1 + a_n) \\ \oplus (a_2 + a_3) \oplus \ldots \oplus (a_2 + a_n) \\ \ldots \\ \oplus (a_{n-1} + a_n) \\ $$
Here $x \oplus y$ is a bitwise XOR operation (i.e. $x$ ^ $y$ in many modern programming languages). You can read about it in Wikipedia: https://en.wikipedia.org/wiki/Exclusive_or#Bitwise_operation.
-----Input-----
The first line contains a single integer $n$ ($2 \leq n \leq 400\,000$) — the number of integers in the array.
The second line contains integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^7$).
-----Output-----
Print a single integer — xor of all pairwise sums of integers in the given array.
-----Examples-----
Input
2
1 2
Output
3
Input
3
1 2 3
Output
2
-----Note-----
In the first sample case there is only one sum $1 + 2 = 3$.
In the second sample case there are three sums: $1 + 2 = 3$, $1 + 3 = 4$, $2 + 3 = 5$. In binary they are represented as $011_2 \oplus 100_2 \oplus 101_2 = 010_2$, thus the answer is 2.
$\oplus$ is the bitwise xor operation. To define $x \oplus y$, consider binary representations of integers $x$ and $y$. We put the $i$-th bit of the result to be 1 when exactly one of the $i$-th bits of $x$ and $y$ is 1. Otherwise, the $i$-th bit of the result is put to be 0. For example, $0101_2 \, \oplus \, 0011_2 = 0110_2$.
|
{"inputs": ["2\n1 2\n", "2\n1 1\n", "2\n1 1\n", "2\n2 2\n", "2\n2 4\n", "2\n1 4\n", "2\n4 3\n", "2\n8 3\n"], "outputs": ["3", "2", "2\n", "4\n", "6\n", "5\n", "7\n", "11\n"]}
| 614
| 101
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problem statements in [Mandarin Chinese] and [Bengali].
There is an empty bus with M seats and a total of N people, numbered from 1 to N. Everyone is currently outside the bus. You are given a sequence of Q events of the following form.
- +\ i : It denotes that the person i enters the bus.
- -\ i : It denotes that the person i leaves the bus.
It is guaranteed in the input that each person from 1 to N enters *at most* once as well as leaves the bus *at most* once.
Determine whether the sequence of events is *consistent* or not (i.e. no person leaves the bus before entering and the number of passengers in the bus does not exceed M at any point of time).
------ 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.
- Each test case contains Q + 1 lines of input.
- The first line of each test case contains three space-separated integers N, M, Q.
- Q lines follow. For each valid j, j^{th} of these lines contains a character ch, followed by a space and an integer i. Here ch is either '+' or '-' and 1 ≤ i ≤ N.
- It is guaranteed that + i" and - i" appears at most once for every 1 ≤ i ≤ N
------ Output Format ------
For each test case, print a single line containing one string - "Consistent" (without quotes) if the sequence is consistent, "Inconsistent" (without quotes) otherwise.
------ Constraints ------
$1 ≤ T ≤ 20$
$1 ≤ N ≤ 10^{4}$
$1 ≤ M ≤ 10^{4}$
$1 ≤ Q ≤ 10^{4}$
----- Sample Input 1 ------
2
2 1 4
+ 1
+ 2
- 1
- 2
3 2 6
+ 2
+ 1
- 1
+ 3
- 3
- 2
----- Sample Output 1 ------
Inconsistent
Consistent
----- explanation 1 ------
- Test case $1$: After Person $2$ enters the bus, there are two people inside the bus while the capacity of the bus is $1$.
----- Sample Input 2 ------
2
100 10 5
+ 1
+ 2
- 3
+ 3
- 2
6 4 4
+ 3
+ 2
+ 1
+ 4
----- Sample Output 2 ------
Inconsistent
Consistent
----- explanation 2 ------
- Test case $1$: Person $3$ leaves the bus without entering and thus it is inconsistent.
|
{"inputs": ["2\n2 1 4\n+ 1\n+ 2\n- 1\n- 2\n3 2 6\n+ 2\n+ 1\n- 1\n+ 3\n- 3\n- 2", "2\n100 10 5\n+ 1\n+ 2\n- 3\n+ 3\n- 2\n6 4 4\n+ 3\n+ 2\n+ 1\n+ 4\n"], "outputs": ["Inconsistent\nConsistent", "Inconsistent\nConsistent\n"]}
| 616
| 131
|
coding
|
Solve the programming task below in a Python markdown code block.
Right now she actually isn't. But she will be, if you don't solve this problem.
You are given integers n, k, A and B. There is a number x, which is initially equal to n. You are allowed to perform two types of operations: Subtract 1 from x. This operation costs you A coins. Divide x by k. Can be performed only if x is divisible by k. This operation costs you B coins. What is the minimum amount of coins you have to pay to make x equal to 1?
-----Input-----
The first line contains a single integer n (1 ≤ n ≤ 2·10^9).
The second line contains a single integer k (1 ≤ k ≤ 2·10^9).
The third line contains a single integer A (1 ≤ A ≤ 2·10^9).
The fourth line contains a single integer B (1 ≤ B ≤ 2·10^9).
-----Output-----
Output a single integer — the minimum amount of coins you have to pay to make x equal to 1.
-----Examples-----
Input
9
2
3
1
Output
6
Input
5
5
2
20
Output
8
Input
19
3
4
2
Output
12
-----Note-----
In the first testcase, the optimal strategy is as follows: Subtract 1 from x (9 → 8) paying 3 coins. Divide x by 2 (8 → 4) paying 1 coin. Divide x by 2 (4 → 2) paying 1 coin. Divide x by 2 (2 → 1) paying 1 coin.
The total cost is 6 coins.
In the second test case the optimal strategy is to subtract 1 from x 4 times paying 8 coins in total.
|
{"inputs": ["9\n2\n3\n1\n", "7\n2\n3\n1\n", "7\n2\n3\n1\n", "7\n2\n1\n1\n", "9\n2\n3\n0\n", "9\n2\n3\n1\n", "5\n5\n2\n20\n", "19\n3\n4\n2\n"], "outputs": ["6\n", "8\n", "8\n", "4\n", "3\n", "6\n", "8\n", "12\n"]}
| 404
| 121
|
coding
|
Solve the programming task below in a Python markdown code block.
VK news recommendation system daily selects interesting publications of one of n disjoint categories for each user. Each publication belongs to exactly one category. For each category i batch algorithm selects a_i publications.
The latest A/B test suggests that users are reading recommended publications more actively if each category has a different number of publications within daily recommendations. The targeted algorithm can find a single interesting publication of i-th category within t_i seconds.
What is the minimum total time necessary to add publications to the result of batch algorithm execution, so all categories have a different number of publications? You can't remove publications recommended by the batch algorithm.
Input
The first line of input consists of single integer n — the number of news categories (1 ≤ n ≤ 200 000).
The second line of input consists of n integers a_i — the number of publications of i-th category selected by the batch algorithm (1 ≤ a_i ≤ 10^9).
The third line of input consists of n integers t_i — time it takes for targeted algorithm to find one new publication of category i (1 ≤ t_i ≤ 10^5).
Output
Print one integer — the minimal required time for the targeted algorithm to get rid of categories with the same size.
Examples
Input
5
3 7 9 7 8
5 2 5 7 5
Output
6
Input
5
1 2 3 4 5
1 1 1 1 1
Output
0
Note
In the first example, it is possible to find three publications of the second type, which will take 6 seconds.
In the second example, all news categories contain a different number of publications.
|
{"inputs": ["1\n1\n72\n", "1\n5\n16\n", "1\n3\n31\n", "1\n5\n12\n", "1\n3\n47\n", "1\n5\n15\n", "1\n1\n100\n", "1\n1\n101\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
| 365
| 112
|
coding
|
Solve the programming task below in a Python markdown code block.
A sorting algorithm A is said to have more time complexity than a sorting algorithm B if it uses more number of comparisons for sorting the same array than algorithm B.
Given that algorithm A uses X comparisons to sort an array and algorithm B uses Y comparisons to sort the same array, find whether algorithm A has more time complexity.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two space-separated integers X and Y — the number of comparisons used by algorithms A and B to sort the array respectively.
------ Output Format ------
For each test case, output on a new line, YES, if the algorithm A has more time complexity than B and NO otherwise.
You may print each character of the string in uppercase or lowercase (for example, the strings YES, yEs, yes, and yeS will all be treated as identical).
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ X, Y ≤ 100$
----- Sample Input 1 ------
4
9 9
15 7
10 19
21 20
----- Sample Output 1 ------
NO
YES
NO
YES
----- explanation 1 ------
Test case $1$: The number of comparisons used by algorithm $A$ is $9$ and that used by $B$ is also $9$. Since the number of comparisons used by $A$ is not more than that of $B$, $A$ does not have more time complexity than $B$.
Test case $2$: The number of comparisons used by algorithm $A$ is $15$ and that used by $B$ is $7$. Since the number of comparisons used by $A$ is more than that of $B$, $A$ does have more time complexity than $B$.
Test case $3$: The number of comparisons used by algorithm $A$ is $10$ and that used by $B$ is $19$. Since the number of comparisons used by $A$ is not more than that of $B$, $A$ does not have more time complexity than $B$.
Test case $4$: The number of comparisons used by algorithm $A$ is $21$ and that used by $B$ is $20$. Since the number of comparisons used by $A$ is more than that of $B$, $A$ does have more time complexity than $B$.
|
{"inputs": ["4\n9 9\n15 7\n10 19\n21 20\n"], "outputs": ["NO\nYES\nNO\nYES"]}
| 530
| 40
|
coding
|
Solve the programming task below in a Python markdown code block.
Lee was cleaning his house for the party when he found a messy string under the carpets. Now he'd like to make it clean accurately and in a stylish way...
The string $s$ he found is a binary string of length $n$ (i. e. string consists only of 0-s and 1-s).
In one move he can choose two consecutive characters $s_i$ and $s_{i+1}$, and if $s_i$ is 1 and $s_{i + 1}$ is 0, he can erase exactly one of them (he can choose which one to erase but he can't erase both characters simultaneously). The string shrinks after erasing.
Lee can make an arbitrary number of moves (possibly zero) and he'd like to make the string $s$ as clean as possible. He thinks for two different strings $x$ and $y$, the shorter string is cleaner, and if they are the same length, then the lexicographically smaller string is cleaner.
Now you should answer $t$ test cases: for the $i$-th test case, print the cleanest possible string that Lee can get by doing some number of moves.
Small reminder: if we have two strings $x$ and $y$ of the same length then $x$ is lexicographically smaller than $y$ if there is a position $i$ such that $x_1 = y_1$, $x_2 = y_2$,..., $x_{i - 1} = y_{i - 1}$ and $x_i < y_i$.
-----Input-----
The first line contains the integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
Next $2t$ lines contain test cases — one per two lines.
The first line of each test case contains the integer $n$ ($1 \le n \le 10^5$) — the length of the string $s$.
The second line contains the binary string $s$. The string $s$ is a string of length $n$ which consists only of zeroes and ones.
It's guaranteed that sum of $n$ over test cases doesn't exceed $10^5$.
-----Output-----
Print $t$ answers — one per test case.
The answer to the $i$-th test case is the cleanest string Lee can get after doing some number of moves (possibly zero).
-----Example-----
Input
5
10
0001111111
4
0101
8
11001101
10
1110000000
1
1
Output
0001111111
001
01
0
1
-----Note-----
In the first test case, Lee can't perform any moves.
In the second test case, Lee should erase $s_2$.
In the third test case, Lee can make moves, for example, in the following order: 11001101 $\rightarrow$ 1100101 $\rightarrow$ 110101 $\rightarrow$ 10101 $\rightarrow$ 1101 $\rightarrow$ 101 $\rightarrow$ 01.
|
{"inputs": ["5\n10\n0001111111\n4\n0101\n8\n11001101\n10\n1110000000\n1\n1\n", "5\n10\n0001111111\n4\n0101\n8\n11001101\n10\n1010000000\n1\n1\n", "5\n10\n0011111111\n4\n0101\n8\n11001101\n10\n1010000000\n1\n1\n", "5\n10\n0001111110\n4\n0101\n8\n11001101\n10\n1110000000\n1\n1\n", "5\n10\n0001111110\n4\n0101\n8\n11001101\n10\n1110000001\n1\n1\n", "5\n10\n0111111111\n4\n0101\n8\n11001101\n10\n1010000000\n1\n1\n", "5\n10\n0011111111\n4\n0101\n8\n11001111\n10\n1010000100\n1\n1\n", "5\n10\n0010111111\n4\n0101\n8\n11001111\n10\n1010000100\n1\n1\n"], "outputs": ["0001111111\n001\n01\n0\n1\n", "0001111111\n001\n01\n0\n1\n", "0011111111\n001\n01\n0\n1\n", "0000\n001\n01\n0\n1\n", "0000\n001\n01\n01\n1\n", "0111111111\n001\n01\n0\n1\n", "0011111111\n001\n01111\n0\n1\n", "000111111\n001\n01111\n0\n1\n"]}
| 728
| 624
|
coding
|
Solve the programming task below in a Python markdown code block.
Giga Tower is the tallest and deepest building in Cyberland. There are 17 777 777 777 floors, numbered from - 8 888 888 888 to 8 888 888 888. In particular, there is floor 0 between floor - 1 and floor 1. Every day, thousands of tourists come to this place to enjoy the wonderful view.
In Cyberland, it is believed that the number "8" is a lucky number (that's why Giga Tower has 8 888 888 888 floors above the ground), and, an integer is lucky, if and only if its decimal notation contains at least one digit "8". For example, 8, - 180, 808 are all lucky while 42, - 10 are not. In the Giga Tower, if you write code at a floor with lucky floor number, good luck will always be with you (Well, this round is #278, also lucky, huh?).
Tourist Henry goes to the tower to seek good luck. Now he is at the floor numbered a. He wants to find the minimum positive integer b, such that, if he walks b floors higher, he will arrive at a floor with a lucky number.
-----Input-----
The only line of input contains an integer a ( - 10^9 ≤ a ≤ 10^9).
-----Output-----
Print the minimum b in a line.
-----Examples-----
Input
179
Output
1
Input
-1
Output
9
Input
18
Output
10
-----Note-----
For the first sample, he has to arrive at the floor numbered 180.
For the second sample, he will arrive at 8.
Note that b should be positive, so the answer for the third sample is 10, not 0.
|
{"inputs": ["0\n", "2\n", "4\n", "6\n", "8\n", "6\n", "4\n", "2\n"], "outputs": ["8\n", "6\n", "4\n", "2\n", "10\n", "2\n", "4\n", "6\n"]}
| 436
| 71
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Write an efficient algorithm that searches for a value target in an m x n integer matrix matrix. This matrix has the following properties:
Integers in each row are sorted in ascending from left to right.
Integers in each column are sorted in ascending from top to bottom.
Please complete the following python code precisely:
```python
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
```
|
{"functional": "def check(candidate):\n assert candidate(matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5) == True\n assert candidate(matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20) == False\n\n\ncheck(Solution().searchMatrix)"}
| 108
| 180
|
coding
|
Solve the programming task below in a Python markdown code block.
You task is to implement an simple interpreter for the notorious esoteric language [HQ9+](https://esolangs.org/wiki/HQ9+) that will work for a single character input:
- If the input is `'H'`, return `'Hello World!'`
- If the input is `'Q'`, return the input
- If the input is `'9'`, return the full lyrics of [99 Bottles of Beer](http://www.99-bottles-of-beer.net/lyrics.html). It should be formatted like this:
```if:rust
__Note__: In Rust, return `Some` containing the appropriate value.
```
```
99 bottles of beer on the wall, 99 bottles of beer.
Take one down and pass it around, 98 bottles of beer on the wall.
98 bottles of beer on the wall, 98 bottles of beer.
Take one down and pass it around, 97 bottles of beer on the wall.
97 bottles of beer on the wall, 97 bottles of beer.
Take one down and pass it around, 96 bottles of beer on the wall.
...
...
...
2 bottles of beer on the wall, 2 bottles of beer.
Take one down and pass it around, 1 bottle of beer on the wall.
1 bottle of beer on the wall, 1 bottle of beer.
Take one down and pass it around, no more bottles of beer on the wall.
No more bottles of beer on the wall, no more bottles of beer.
Go to the store and buy some more, 99 bottles of beer on the wall.
```
- For everything else, don't return anything (return `null` in C#, `None` in Rust).
(`+` has no visible effects so we can safely ignore it.)
Also feel free to reuse/extend the following starter code:
```python
def HQ9(code):
```
|
{"functional": "_inputs = [['X'], ['H'], ['Q']]\n_outputs = [[None], ['Hello World!'], ['Q']]\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(HQ9(*i), o[0])"}
| 412
| 169
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given $n$ numbers $a_1, a_2, \dots, a_n$. With a cost of one coin you can perform the following operation:
Choose one of these numbers and add or subtract $1$ from it.
In particular, we can apply this operation to the same number several times.
We want to make the product of all these numbers equal to $1$, in other words, we want $a_1 \cdot a_2$ $\dots$ $\cdot a_n = 1$.
For example, for $n = 3$ and numbers $[1, -3, 0]$ we can make product equal to $1$ in $3$ coins: add $1$ to second element, add $1$ to second element again, subtract $1$ from third element, so that array becomes $[1, -1, -1]$. And $1\cdot (-1) \cdot (-1) = 1$.
What is the minimum cost we will have to pay to do that?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the number of numbers.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) — the numbers.
-----Output-----
Output a single number — the minimal number of coins you need to pay to make the product equal to $1$.
-----Examples-----
Input
2
-1 1
Output
2
Input
4
0 0 0 0
Output
4
Input
5
-5 -3 5 3 0
Output
13
-----Note-----
In the first example, you can change $1$ to $-1$ or $-1$ to $1$ in $2$ coins.
In the second example, you have to apply at least $4$ operations for the product not to be $0$.
In the third example, you can change $-5$ to $-1$ in $4$ coins, $-3$ to $-1$ in $2$ coins, $5$ to $1$ in $4$ coins, $3$ to $1$ in $2$ coins, $0$ to $1$ in $1$ coin.
|
{"inputs": ["2\n0 1\n", "2\n0 2\n", "2\n0 0\n", "2\n0 4\n", "2\n1 4\n", "2\n-1 1\n", "2\n-1 0\n", "2\n-1 2\n"], "outputs": ["1\n", "2\n", "2\n", "4\n", "3\n", "2", "1\n", "3\n"]}
| 527
| 104
|
coding
|
Solve the programming task below in a Python markdown code block.
In CODE FESTIVAL XXXX, there are N+1 participants from all over the world, including Takahashi.
Takahashi checked and found that the time gap (defined below) between the local times in his city and the i-th person's city was D_i hours. The time gap between two cities is defined as follows. For two cities A and B, if the local time in city B is d o'clock at the moment when the local time in city A is 0 o'clock, then the time gap between these two cities is defined to be min(d,24-d) hours. Here, we are using 24-hour notation. That is, the local time in the i-th person's city is either d o'clock or 24-d o'clock at the moment when the local time in Takahashi's city is 0 o'clock, for example.
Then, for each pair of two people chosen from the N+1 people, he wrote out the time gap between their cities. Let the smallest time gap among them be s hours.
Find the maximum possible value of s.
Constraints
* 1 \leq N \leq 50
* 0 \leq D_i \leq 12
* All input values are integers.
Input
Input is given from Standard Input in the following format:
N
D_1 D_2 ... D_N
Output
Print the maximum possible value of s.
Examples
Input
3
7 12 8
Output
4
Input
2
11 11
Output
2
Input
1
0
Output
0
|
{"inputs": ["1\n9", "1\n0", "1\n11", "1\n10", "2\n8 8", "2\n2 0", "2\n6 8", "2\n9 7"], "outputs": ["9\n", "0", "11\n", "10\n", "8\n", "0\n", "6\n", "7\n"]}
| 353
| 89
|
coding
|
Solve the programming task below in a Python markdown code block.
Given is a string S consisting of `0` and `1`. Find the number of strings, modulo 998244353, that can result from applying the following operation on S between 0 and K times (inclusive):
* Choose a pair of integers i, j (1\leq i < j\leq |S|) such that the i-th and j-th characters of S are `0` and `1`, respectively. Remove the j-th character from S and insert it to the immediate left of the i-th character.
Constraints
* 1 \leq |S| \leq 300
* 0 \leq K \leq 10^9
* S consists of `0` and `1`.
Input
Input is given from Standard Input in the following format:
S K
Output
Find the number of strings, modulo 998244353, that can result from applying the operation on S between 0 and K times (inclusive).
Examples
Input
0101 1
Output
4
Input
01100110 2
Output
14
Input
1101010010101101110111100011011111011000111101110101010010101010101 20
Output
113434815
|
{"inputs": ["0111 1", "0111 0", "0011 1", "0001 1", "0011 2", "0101 5", "0011 0", "0101 1"], "outputs": ["2\n", "1\n", "3\n", "4\n", "6\n", "5\n", "1\n", "4"]}
| 346
| 101
|
coding
|
Solve the programming task below in a Python markdown code block.
Problem
Given the strings $ s $, $ t $.
First, let the string set $ A = $ {$ s $}, $ B = \ phi $.
At this time, I want to perform the following operations as much as possible.
operation
step1
Perform the following processing for all $ u ∊ A $.
1. From all subsequences of $ u $ (not necessarily contiguous), choose any one that is equal to $ t $. Let the indexes corresponding to $ u $ of the selected subsequence be $ z_1 $, $ z_2 $,…, $ z_ {| t |} $ (however, 1 $ \ le $ $ z_1 $ $ \ lt $ $ z_2). $$ \ lt $… $ \ lt $ $ z_ {| t |} $ $ \ le $ $ | u | $). If there is no such subsequence, the operation ends.
2. Divide the original string with the selected substring characters $ u_ {z_1} $, $ u_ {z_2} $,…, $ u_ {z_ {| t |}} $ and convert them to $ B $ to add.
For example, in the case of $ u = $ "abcdcd" and $ t = $ "ac", the following two division methods can be considered.
If you choose the first and third characters of $ u $, it will be split into "", "b" and "dcd".
If you choose the first and fifth characters of $ u $, it will be split into "", "bcd" and "d".
step2
Replace $ A $ with $ B $ and empty $ B $.
Increase the number of operations by 1.
Subsequence example
The subsequences of "abac" are {"", "a", "b", "c", "aa", "ab", "ac", "ba", "bc", "aac", "aba" "," abc "," bac "," abac "}.
"a" is a subsequence created by extracting the first or third character of "abac".
"ac" is a subsequence created by extracting the 1st and 4th characters of "abac" or the 3rd and 4th characters.
Constraints
The input satisfies the following conditions.
* 1 $ \ le $ $ | t | $ $ \ le $ $ | s | $ $ \ le $ $ 10 ^ 5 $
* Characters contained in the strings $ s $ and $ t $ are uppercase or lowercase letters of the alphabet
Input
The input is given in the following format.
$ s $
$ t $
The string $ s $ is given on the first line, and the string $ t $ is given on the second line.
Output
Output the maximum number of operations.
Examples
Input
AABABCAC
A
Output
2
Input
abaabbabaabaabbabbabaabbab
ab
Output
3
Input
AbCdEfG
aBcDeFg
Output
0
|
{"inputs": ["AABABCAC\nB", "AACABCAC\nB", "AACABCAC\nC", "AACAACAC\nC", "@ACAACAC\nC", "AABABCAC\nA", "AbCdEfG\naBcDeFh", "AbGdEfC\naBcDeFh"], "outputs": ["1\n", "1\n", "2\n", "2\n", "2\n", "2", "0\n", "0\n"]}
| 642
| 109
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a string $S$. Find the number of ways to choose an unordered pair of non-overlapping non-empty substrings of this string (let's denote them by $s_1$ and $s_2$ in such a way that $s_2$ starts after $s_1$ ends) such that their concatenation $s_1 + s_2$ is a palindrome.
Two pairs $(s_1, s_2)$ and $(s_1', s_2')$ are different if $s_1$ is chosen at a different position from $s_1'$ or $s_2$ is chosen at a different position from $s_2'$.
-----Input-----
The first and only line of the input contains a single string $S$.
-----Output-----
Print a single line containing one integer — the number of ways to choose a valid pair of substrings.
-----Constraints-----
- $1 \le |S| \le 1,000$
- $S$ contains only lowercase English letters
-----Subtasks-----
Subtask #1 (25 points): $|S| \le 100$
Subtask #2 (75 points): original constraints
-----Example Input-----
abba
-----Example Output-----
7
-----Explanation-----
The following pairs of substrings can be chosen: ("a", "a"), ("a", "ba"), ("a", "bba"), ("ab", "a"), ("ab", "ba"), ("abb", "a"), ("b", "b").
|
{"inputs": ["abba"], "outputs": ["7"]}
| 335
| 13
|
coding
|
Solve the programming task below in a Python markdown code block.
The new "Avengers" movie has just been released! There are a lot of people at the cinema box office standing in a huge line. Each of them has a single `100`, `50` or `25` dollar bill. An "Avengers" ticket costs `25 dollars`.
Vasya is currently working as a clerk. He wants to sell a ticket to every single person in this line.
Can Vasya sell a ticket to every person and give change if he initially has no money and sells the tickets strictly in the order people queue?
Return `YES`, if Vasya can sell a ticket to every person and give change with the bills he has at hand at that moment. Otherwise return `NO`.
### Examples:
```csharp
Line.Tickets(new int[] {25, 25, 50}) // => YES
Line.Tickets(new int[] {25, 100}) // => NO. Vasya will not have enough money to give change to 100 dollars
Line.Tickets(new int[] {25, 25, 50, 50, 100}) // => NO. Vasya will not have the right bills to give 75 dollars of change (you can't make two bills of 25 from one of 50)
```
```python
tickets([25, 25, 50]) # => YES
tickets([25, 100]) # => NO. Vasya will not have enough money to give change to 100 dollars
tickets([25, 25, 50, 50, 100]) # => NO. Vasya will not have the right bills to give 75 dollars of change (you can't make two bills of 25 from one of 50)
```
```cpp
tickets({25, 25, 50}) // => YES
tickets({25, 100}) // => NO. Vasya will not have enough money to give change to 100 dollars
tickets({25, 25, 50, 50, 100}) // => NO. Vasya will not have the right bills to give 75 dollars of change (you can't make two bills of 25 from one of 50)
```
Also feel free to reuse/extend the following starter code:
```python
def tickets(people):
```
|
{"functional": "_inputs = [[[25, 25, 50]], [[25, 25, 50, 100]], [[25, 100]], [[25, 25, 25, 25, 25, 25, 25, 25, 25, 25]], [[50, 50, 50, 50, 50, 50, 50, 50, 50, 50]], [[100, 100, 100, 100, 100, 100, 100, 100, 100, 100]], [[25, 25, 25, 25, 50, 100, 50]], [[50, 100, 100]], [[25, 25, 100]], [[25, 25, 25, 25, 25, 25, 25, 50, 50, 50, 100, 100, 100, 100]], [[25, 25, 50, 50, 100]], [[25, 50, 50]], [[25, 25, 25, 100]], [[25, 50, 25, 100]], [[25, 25, 25, 25, 25, 100, 100]], [[25, 50, 100, 25, 25, 25, 50]], [[25, 50, 25, 50, 100, 25, 25, 50]], [[25, 50, 25, 100, 25, 25, 50, 100, 25, 25, 25, 100, 25, 25, 50, 100, 25, 50, 25, 100, 25, 50, 50, 50]], [[25, 25, 25, 100, 25, 25, 25, 100, 25, 25, 50, 100, 25, 25, 50, 100, 50, 50]], [[25, 50, 25, 100, 25, 25, 50, 100, 25, 50, 25, 100, 50, 25]]]\n_outputs = [['YES'], ['YES'], ['NO'], ['YES'], ['NO'], ['NO'], ['YES'], ['NO'], ['NO'], ['NO'], ['NO'], ['NO'], ['YES'], ['YES'], ['NO'], ['NO'], ['NO'], ['NO'], ['NO'], ['NO']]\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(tickets(*i), o[0])"}
| 551
| 887
|
coding
|
Solve the programming task below in a Python markdown code block.
There is an N-car train.
You are given an integer i. Find the value of j such that the following statement is true: "the i-th car from the front of the train is the j-th car from the back."
-----Constraints-----
- 1 \leq N \leq 100
- 1 \leq i \leq N
-----Input-----
Input is given from Standard Input in the following format:
N i
-----Output-----
Print the answer.
-----Sample Input-----
4 2
-----Sample Output-----
3
The second car from the front of a 4-car train is the third car from the back.
|
{"inputs": ["2 1", "2 2", "4 1", "2 0", "9 1", "1 1", "4 2", "4 2\n"], "outputs": ["2\n", "1\n", "4\n", "3\n", "9\n", "1", "3", "3\n"]}
| 148
| 77
|
coding
|
Solve the programming task below in a Python markdown code block.
Given an array of ones and zeroes, convert the equivalent binary value to an integer.
Eg: `[0, 0, 0, 1]` is treated as `0001` which is the binary representation of `1`.
Examples:
```
Testing: [0, 0, 0, 1] ==> 1
Testing: [0, 0, 1, 0] ==> 2
Testing: [0, 1, 0, 1] ==> 5
Testing: [1, 0, 0, 1] ==> 9
Testing: [0, 0, 1, 0] ==> 2
Testing: [0, 1, 1, 0] ==> 6
Testing: [1, 1, 1, 1] ==> 15
Testing: [1, 0, 1, 1] ==> 11
```
However, the arrays can have varying lengths, not just limited to `4`.
Also feel free to reuse/extend the following starter code:
```python
def binary_array_to_number(arr):
```
|
{"functional": "_inputs = [[[0, 0, 0, 1]], [[0, 0, 1, 0]], [[1, 1, 1, 1]], [[0, 1, 1, 0]]]\n_outputs = [[1], [2], [15], [6]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(binary_array_to_number(*i), o[0])"}
| 255
| 211
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0.
A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.
For example, "ace" is a subsequence of "abcde".
A common subsequence of two strings is a subsequence that is common to both strings.
Please complete the following python code precisely:
```python
class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(text1 = \"abcde\", text2 = \"ace\" ) == 3 \n assert candidate(text1 = \"abc\", text2 = \"abc\") == 3\n assert candidate(text1 = \"abc\", text2 = \"def\") == 0\n\n\ncheck(Solution().longestCommonSubsequence)"}
| 153
| 83
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given three integers, k, digit1, and digit2, you want to find the smallest integer that is:
Larger than k,
A multiple of k, and
Comprised of only the digits digit1 and/or digit2.
Return the smallest such integer. If no such integer exists or the integer exceeds the limit of a signed 32-bit integer (231 - 1), return -1.
Please complete the following python code precisely:
```python
class Solution:
def findInteger(self, k: int, digit1: int, digit2: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(k = 2, digit1 = 0, digit2 = 2) == 20\n assert candidate(k = 3, digit1 = 4, digit2 = 2) == 24\n assert candidate(k = 2, digit1 = 0, digit2 = 0) == -1\n\n\ncheck(Solution().findInteger)"}
| 143
| 93
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given an undirected graph consisting of N vertices and M edges. The vertices are numbered 1 to N, and the edges are numbered 1 to M. In addition, each vertex has a label, `A` or `B`. The label of Vertex i is s_i. Edge i bidirectionally connects vertex a_i and b_i.
The phantom thief Nusook likes to choose some vertex as the startpoint and traverse an edge zero or more times. Today, he will make a string after traveling as above, by placing the labels of the visited vertices in the order visited, beginning from the startpoint.
For example, in a graph where Vertex 1 has the label `A` and Vertex 2 has the label `B`, if Nusook travels along the path 1 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 2, the resulting string is `ABABB`.
Determine if Nusook can make all strings consisting of `A` and `B`.
Constraints
* 2 \leq N \leq 2 \times 10^{5}
* 1 \leq M \leq 2 \times 10^{5}
* |s| = N
* s_i is `A` or `B`.
* 1 \leq a_i, b_i \leq N
* The given graph may NOT be simple or connected.
Input
Input is given from Standard Input in the following format:
N M
s
a_1 b_1
:
a_{M} b_{M}
Output
If Nusook can make all strings consisting of `A` and `B`, print `Yes`; otherwise, print `No`.
Examples
Input
2 3
AB
1 1
1 2
2 2
Output
Yes
Input
4 3
ABAB
1 2
2 3
3 1
Output
No
Input
13 23
ABAAAABBBBAAB
7 1
10 6
1 11
2 10
2 8
2 11
11 12
8 3
7 12
11 2
13 13
11 9
4 1
9 7
9 6
8 13
8 6
4 10
8 7
4 3
2 1
8 12
6 9
Output
Yes
Input
13 17
BBABBBAABABBA
7 1
7 9
11 12
3 9
11 9
2 1
11 5
12 11
10 8
1 11
1 8
7 7
9 10
8 8
8 12
6 2
13 11
Output
No
|
{"inputs": ["2 3\nAA\n1 1\n1 2\n2 2", "2 3\nAA\n1 2\n1 2\n2 2", "2 3\nBA\n1 1\n1 2\n2 2", "2 3\nBA\n2 1\n1 2\n2 2", "2 3\nBA\n1 1\n1 2\n2 1", "2 3\nBA\n2 2\n1 2\n2 2", "2 3\nAB\n1 1\n1 2\n2 2", "4 3\nABAB\n1 1\n2 3\n3 1"], "outputs": ["No\n", "No\n", "Yes\n", "No\n", "No\n", "No\n", "Yes", "No\n"]}
| 644
| 190
|
coding
|
Solve the programming task below in a Python markdown code block.
An elephant decided to visit his friend. It turned out that the elephant's house is located at point 0 and his friend's house is located at point x(x > 0) of the coordinate line. In one step the elephant can move 1, 2, 3, 4 or 5 positions forward. Determine, what is the minimum number of steps he need to make in order to get to his friend's house.
-----Input-----
The first line of the input contains an integer x (1 ≤ x ≤ 1 000 000) — The coordinate of the friend's house.
-----Output-----
Print the minimum number of steps that elephant needs to make to get from point 0 to point x.
-----Examples-----
Input
5
Output
1
Input
12
Output
3
-----Note-----
In the first sample the elephant needs to make one step of length 5 to reach the point x.
In the second sample the elephant can get to point x if he moves by 3, 5 and 4. There are other ways to get the optimal answer but the elephant cannot reach x in less than three moves.
|
{"inputs": ["5\n", "1\n", "2\n", "3\n", "4\n", "3\n", "1\n", "4\n"], "outputs": ["1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n"]}
| 254
| 70
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given an integer num, return the number of steps to reduce it to zero.
In one step, if the current number is even, you have to divide it by 2, otherwise, you have to subtract 1 from it.
Please complete the following python code precisely:
```python
class Solution:
def numberOfSteps(self, num: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(num = 14) == 6\n assert candidate(num = 8) == 4\n assert candidate(num = 123) == 12\n\n\ncheck(Solution().numberOfSteps)"}
| 94
| 59
|
coding
|
Solve the programming task below in a Python markdown code block.
You can find anything whatsoever in our Galaxy! A cubical planet goes round an icosahedral star. Let us introduce a system of axes so that the edges of the cubical planet are parallel to the coordinate axes and two opposite vertices lay in the points (0, 0, 0) and (1, 1, 1). Two flies live on the planet. At the moment they are sitting on two different vertices of the cubical planet. Your task is to determine whether they see each other or not. The flies see each other when the vertices they occupy lie on the same face of the cube.
Input
The first line contains three space-separated integers (0 or 1) — the coordinates of the first fly, the second line analogously contains the coordinates of the second fly.
Output
Output "YES" (without quotes) if the flies see each other. Otherwise, output "NO".
Examples
Input
0 0 0
0 1 0
Output
YES
Input
1 1 0
0 1 0
Output
YES
Input
0 0 0
1 1 1
Output
NO
|
{"inputs": ["1 0 0\n0 0 0\n", "1 1 0\n0 0 1\n", "1 1 1\n0 1 1\n", "0 0 1\n1 0 0\n", "1 1 1\n0 0 0\n", "1 0 0\n0 1 0\n", "1 0 0\n0 1 1\n", "0 0 1\n1 0 1\n"], "outputs": ["YES\n", "NO\n", "YES\n", "YES\n", "NO\n", "YES\n", "NO\n", "YES\n"]}
| 254
| 150
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
There is a garland — a cyclic rope with red and green flowers on it in some order. The sequence of flower colours is described by a string $s$; since the rope is cyclic, each two consecutive flowers are adjacent and the first and last flower are also adjacent.
The garland is *beautiful* if there is no pair of adjacent flowers with identical colours.
You want the garland to be beautiful. To achieve that, you may perform the following operation at most once:
Make two cuts on the rope (not intersecting the flowers), splitting the garland into two parts.
Reverse one of these two parts.
Tie together corresponding endpoints of the two parts, creating one whole garland again.
Can you find out whether it is possible to make the garland beautiful?
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains a single string $s$ describing the garland. Each character of $s$ is either 'R' or 'G', denoting a red or green flower respectively.
------ Output ------
For each test case, print a single line containing the string "yes" if the garland can be made beautiful or "no" otherwise.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$2 ≤ |s| ≤ 10^{5}$
the sum of $|s|$ over all test cases does not exceed $10^{6}$
----- Sample Input 1 ------
3
RG
RRGG
RR
----- Sample Output 1 ------
yes
yes
no
----- explanation 1 ------
Example case 1: The garland is already beautiful.
Example case 2: We can cut the garland between flowers 1 and 2 and between flowers 3 and 4. After reversing the part containing flowers 2 and 3 and rejoining, we obtain "RGRG".
|
{"inputs": ["3\nRG\nRRGG\nRR"], "outputs": ["yes\nyes\nno"]}
| 464
| 23
|
coding
|
Solve the programming task below in a Python markdown code block.
The chef is trying to solve some pattern problems, Chef wants your help to code it. Chef has one number K (odd) to form a new pattern. Help the chef to code this pattern problem.
-----Input:-----
- First-line will contain $T$, the number of test cases. Then the test cases follow.
- Each test case contains a single line of input, one integer $K$.
-----Output:-----
For each test case, output as the pattern.
-----Constraints-----
- $1 \leq T \leq 100$
- $1 \leq K \leq 100$
-----Sample Input:-----
4
1
3
5
7
-----Sample Output:-----
*
*
**
*
*
**
* *
**
*
*
**
* *
* *
* *
**
*
-----EXPLANATION:-----
No need, else pattern can be decode easily.
|
{"inputs": ["4\n1\n3\n5\n7"], "outputs": ["*\n*\n**\n*\n*\n**\n* *\n**\n*\n*\n**\n* *\n* *\n* *\n**\n*"]}
| 200
| 60
|
coding
|
Solve the programming task below in a Python markdown code block.
There are $n$ rectangles in a row. You can either turn each rectangle by $90$ degrees or leave it as it is. If you turn a rectangle, its width will be height, and its height will be width. Notice that you can turn any number of rectangles, you also can turn all or none of them. You can not change the order of the rectangles.
Find out if there is a way to make the rectangles go in order of non-ascending height. In other words, after all the turns, a height of every rectangle has to be not greater than the height of the previous rectangle (if it is such).
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of rectangles.
Each of the next $n$ lines contains two integers $w_i$ and $h_i$ ($1 \leq w_i, h_i \leq 10^9$) — the width and the height of the $i$-th rectangle.
-----Output-----
Print "YES" (without quotes) if there is a way to make the rectangles go in order of non-ascending height, otherwise print "NO".
You can print each letter in any case (upper or lower).
-----Examples-----
Input
3
3 4
4 6
3 5
Output
YES
Input
2
3 4
5 5
Output
NO
-----Note-----
In the first test, you can rotate the second and the third rectangles so that the heights will be [4, 4, 3].
In the second test, there is no way the second rectangle will be not higher than the first one.
|
{"inputs": ["1\n1 1\n", "1\n1 1\n", "1\n1 2\n", "1\n2 2\n", "1\n3 2\n", "1\n4 2\n", "1\n0 2\n", "2\n3 4\n5 5\n"], "outputs": ["YES\n", "YES\n", "YES\n", "YES\n", "YES\n", "YES\n", "YES\n", "NO\n"]}
| 375
| 106
|
coding
|
Solve the programming task below in a Python markdown code block.
Hexadecimal likes drawing. She has drawn many graphs already, both directed and not. Recently she has started to work on a still-life «interesting graph and apples». An undirected graph is called interesting, if each of its vertices belongs to one cycle only — a funny ring — and does not belong to any other cycles. A funny ring is a cycle that goes through all the vertices just once. Moreover, loops are funny rings too.
She has already drawn the apples and some of the graph edges. But now it is not clear, how to connect the rest of the vertices to get an interesting graph as a result. The answer should contain the minimal amount of added edges. And furthermore, the answer should be the lexicographically smallest one. The set of edges (x1, y1), (x2, y2), ..., (xn, yn), where xi ≤ yi, is lexicographically smaller than the set (u1, v1), (u2, v2), ..., (un, vn), where ui ≤ vi, provided that the sequence of integers x1, y1, x2, y2, ..., xn, yn is lexicographically smaller than the sequence u1, v1, u2, v2, ..., un, vn. If you do not cope, Hexadecimal will eat you. ...eat you alive.
Input
The first line of the input data contains a pair of integers n and m (1 ≤ n ≤ 50, 0 ≤ m ≤ 2500) — the amount of vertices and edges respectively. The following lines contain pairs of numbers xi and yi (1 ≤ xi, yi ≤ n) — the vertices that are already connected by edges. The initial graph may contain multiple edges and loops.
Output
In the first line output «YES» or «NO»: if it is possible or not to construct an interesting graph. If the answer is «YES», in the second line output k — the amount of edges that should be added to the initial graph. Finally, output k lines: pairs of vertices xj and yj, between which edges should be drawn. The result may contain multiple edges and loops. k can be equal to zero.
Examples
Input
3 2
1 2
2 3
Output
YES
1
1 3
|
{"inputs": ["1 0\n", "2 0\n", "6 0\n", "50 0\n", "49 0\n", "49 0\n", "50 0\n", "6 1\n4 1\n"], "outputs": ["YES\n1\n1 1\n", "YES\n2\n1 2\n1 2\n", "YES\n6\n1 2\n1 3\n2 4\n3 5\n4 6\n5 6\n", "YES\n50\n1 2\n1 3\n2 4\n3 5\n4 6\n5 7\n6 8\n7 9\n8 10\n9 11\n10 12\n11 13\n12 14\n13 15\n14 16\n15 17\n16 18\n17 19\n18 20\n19 21\n20 22\n21 23\n22 24\n23 25\n24 26\n25 27\n26 28\n27 29\n28 30\n29 31\n30 32\n31 33\n32 34\n33 35\n34 36\n35 37\n36 38\n37 39\n38 40\n39 41\n40 42\n41 43\n42 44\n43 45\n44 46\n45 47\n46 48\n47 49\n48 50\n49 50\n", "YES\n49\n1 2\n1 3\n2 4\n3 5\n4 6\n5 7\n6 8\n7 9\n8 10\n9 11\n10 12\n11 13\n12 14\n13 15\n14 16\n15 17\n16 18\n17 19\n18 20\n19 21\n20 22\n21 23\n22 24\n23 25\n24 26\n25 27\n26 28\n27 29\n28 30\n29 31\n30 32\n31 33\n32 34\n33 35\n34 36\n35 37\n36 38\n37 39\n38 40\n39 41\n40 42\n41 43\n42 44\n43 45\n44 46\n45 47\n46 48\n47 49\n48 49\n", "YES\n49\n1 2\n1 3\n2 4\n3 5\n4 6\n5 7\n6 8\n7 9\n8 10\n9 11\n10 12\n11 13\n12 14\n13 15\n14 16\n15 17\n16 18\n17 19\n18 20\n19 21\n20 22\n21 23\n22 24\n23 25\n24 26\n25 27\n26 28\n27 29\n28 30\n29 31\n30 32\n31 33\n32 34\n33 35\n34 36\n35 37\n36 38\n37 39\n38 40\n39 41\n40 42\n41 43\n42 44\n43 45\n44 46\n45 47\n46 48\n47 49\n48 49\n", "YES\n50\n1 2\n1 3\n2 4\n3 5\n4 6\n5 7\n6 8\n7 9\n8 10\n9 11\n10 12\n11 13\n12 14\n13 15\n14 16\n15 17\n16 18\n17 19\n18 20\n19 21\n20 22\n21 23\n22 24\n23 25\n24 26\n25 27\n26 28\n27 29\n28 30\n29 31\n30 32\n31 33\n32 34\n33 35\n34 36\n35 37\n36 38\n37 39\n38 40\n39 41\n40 42\n41 43\n42 44\n43 45\n44 46\n45 47\n46 48\n47 49\n48 50\n49 50\n", "YES\n5\n1 2\n2 3\n3 5\n4 6\n5 6\n"]}
| 491
| 1,286
|
coding
|
Solve the programming task below in a Python markdown code block.
There are N cities. There are also K roads and L railways, extending between the cities.
The i-th road bidirectionally connects the p_i-th and q_i-th cities, and the i-th railway bidirectionally connects the r_i-th and s_i-th cities.
No two roads connect the same pair of cities. Similarly, no two railways connect the same pair of cities.
We will say city A and B are connected by roads if city B is reachable from city A by traversing some number of roads. Here, any city is considered to be connected to itself by roads.
We will also define connectivity by railways similarly.
For each city, find the number of the cities connected to that city by both roads and railways.
-----Constraints-----
- 2 ≦ N ≦ 2*10^5
- 1 ≦ K, L≦ 10^5
- 1 ≦ p_i, q_i, r_i, s_i ≦ N
- p_i < q_i
- r_i < s_i
- When i ≠ j, (p_i, q_i) ≠ (p_j, q_j)
- When i ≠ j, (r_i, s_i) ≠ (r_j, s_j)
-----Input-----
The input is given from Standard Input in the following format:
N K L
p_1 q_1
:
p_K q_K
r_1 s_1
:
r_L s_L
-----Output-----
Print N integers. The i-th of them should represent the number of the cities connected to the i-th city by both roads and railways.
-----Sample Input-----
4 3 1
1 2
2 3
3 4
2 3
-----Sample Output-----
1 2 2 1
All the four cities are connected to each other by roads.
By railways, only the second and third cities are connected. Thus, the answers for the cities are 1, 2, 2 and 1, respectively.
|
{"inputs": ["4 3 1\n1 2\n2 3\n3 4\n2 3\n", "4 2 2\n1 2\n2 3\n1 4\n2 3\n", "7 4 4\n1 2\n2 3\n2 5\n6 7\n3 5\n4 5\n3 4\n6 7\n"], "outputs": ["1 2 2 1\n", "1 2 2 1\n", "1 1 2 1 2 2 2\n"]}
| 431
| 130
|
coding
|
Solve the programming task below in a Python markdown code block.
Snuke loves flags.
Snuke is placing N flags on a line.
The i-th flag can be placed at either coordinate x_i or coordinate y_i.
Snuke thinks that the flags look nicer when the smallest distance between two of them, d, is larger. Find the maximum possible value of d.
Constraints
* 2 ≤ N ≤ 10^{4}
* 1 ≤ x_i, y_i ≤ 10^{9}
* x_i and y_i are integers.
Input
The input is given from Standard Input in the following format:
N
x_1 y_1
:
x_N y_N
Output
Print the answer.
Examples
Input
3
1 3
2 5
1 9
Output
4
Input
5
2 2
2 2
2 2
2 2
2 2
Output
0
Input
22
93 6440
78 6647
862 11
8306 9689
798 99
801 521
188 206
6079 971
4559 209
50 94
92 6270
5403 560
803 83
1855 99
42 504
75 484
629 11
92 122
3359 37
28 16
648 14
11 269
Output
17
|
{"inputs": ["3\n1 3\n2 5\n2 9", "3\n2 3\n2 5\n2 9", "3\n1 3\n2 5\n1 9", "3\n3 3\n2 5\n2 16", "3\n4 0\n5 0\n1 21", "3\n2 1\n8 0\n1 45", "3\n2 3\n2 5\n2 16", "3\n3 3\n2 5\n1 16"], "outputs": ["4\n", "3\n", "4", "2\n", "5\n", "7\n", "3\n", "2\n"]}
| 379
| 162
|
coding
|
Solve the programming task below in a Python markdown code block.
Phone number in Berland is a sequence of n digits. Often, to make it easier to memorize the number, it is divided into groups of two or three digits. For example, the phone number 1198733 is easier to remember as 11-987-33. Your task is to find for a given phone number any of its divisions into groups of two or three digits.
Input
The first line contains integer n (2 ≤ n ≤ 100) — amount of digits in the phone number. The second line contains n digits — the phone number to divide into groups.
Output
Output any of divisions of the given phone number into groups of two or three digits. Separate groups by single character -. If the answer is not unique, output any.
Examples
Input
6
549871
Output
54-98-71
Input
7
1198733
Output
11-987-33
|
{"inputs": ["2\n74\n", "2\n33\n", "2\n24\n", "2\n46\n", "2\n15\n", "2\n16\n", "2\n22\n", "2\n71\n"], "outputs": ["74\n", "33\n", "24\n", "46\n", "15\n", "16\n", "22\n", "71\n"]}
| 224
| 102
|
coding
|
Solve the programming task below in a Python markdown code block.
# Task
Some children are playing rope skipping game. Children skip the rope at roughly the same speed: `once per second`. If the child fails during the jump, he needs to tidy up the rope and continue. This will take `3 seconds`.
You are given an array `failedCount`, where each element is the jump count at the failed. ie. `[12,23,45]` means the child failed 3 times in the game process. The 1st mistake occurred when he jumped 12 times; The 2nd mistake occurred when he jumped 23 times; The 3rd mistake occurred when he jumped 45 times.
Your task is to calculate how many times the child jumped in 60 seconds.
Note: Each child persisted at least 60 jumps, which meant it could have been over 60 seconds, but the child continued to skip rope.
# Input/Output
`[input]` integer array `failedCount`
`0 ≤ failedCount.length ≤ 60`
`1 ≤ failedCount[i] ≤ 60`
`[output]` an integer
how many times the child jumped in 60 seconds.
# Example
For `failedCount = []`, the output should be `60`.
There is no mistake in the game process. So the child jumped 60 times in 60 seconds.
For `failedCount = [12, 23, 45]`, the output should be `51`.
```
The 1st mistake occurred when he jumped 12 times. --> 12 seconds past.
Tidy up the rope and continue. --> 15 seconds past.
The 2nd mistake occurred when he jumped 23 times. --> 26 seconds past.
Tidy up the rope and continue. --> 29 seconds past.
The 3rd mistake occurred when he jumped 45 times. --> 51 seconds past.
Tidy up the rope and continue. --> 54 seconds past.
When he jumped 51 times --> 60 seconds past.
```
Also feel free to reuse/extend the following starter code:
```python
def tiaosheng(failed_counter):
```
|
{"functional": "_inputs = [[[]], [[12, 23, 45]], [[17]], [[10, 20, 30, 40]], [[10, 20, 30, 40, 58]], [[10, 20, 30, 40, 47, 60]], [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]]\n_outputs = [[60], [51], [57], [48], [48], [47], [30]]\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(tiaosheng(*i), o[0])"}
| 481
| 288
|
coding
|
Solve the programming task below in a Python markdown code block.
Vasya is an active Internet user. One day he came across an Internet resource he liked, so he wrote its address in the notebook. We know that the address of the written resource has format: <protocol>://<domain>.ru[/<context>]
where: <protocol> can equal either "http" (without the quotes) or "ftp" (without the quotes), <domain> is a non-empty string, consisting of lowercase English letters, the /<context> part may not be present. If it is present, then <context> is a non-empty string, consisting of lowercase English letters.
If string <context> isn't present in the address, then the additional character "/" isn't written. Thus, the address has either two characters "/" (the ones that go before the domain), or three (an extra one in front of the context).
When the boy came home, he found out that the address he wrote in his notebook had no punctuation marks. Vasya must have been in a lot of hurry and didn't write characters ":", "/", ".".
Help Vasya to restore the possible address of the recorded Internet resource.
-----Input-----
The first line contains a non-empty string that Vasya wrote out in his notebook. This line consists of lowercase English letters only.
It is guaranteed that the given string contains at most 50 letters. It is guaranteed that the given string can be obtained from some correct Internet resource address, described above.
-----Output-----
Print a single line — the address of the Internet resource that Vasya liked. If there are several addresses that meet the problem limitations, you are allowed to print any of them.
-----Examples-----
Input
httpsunrux
Output
http://sun.ru/x
Input
ftphttprururu
Output
ftp://http.ru/ruru
-----Note-----
In the second sample there are two more possible answers: "ftp://httpruru.ru" and "ftp://httpru.ru/ru".
|
{"inputs": ["ftprru\n", "ftprru\n", "ftparua\n", "httpzru\n", "ftparua\n", "httpzru\n", "httprrur\n", "httprrur\n"], "outputs": ["ftp://r.ru\n", "ftp://r.ru", "ftp://a.ru/a\n", "http://z.ru\n", "ftp://a.ru/a\n", "http://z.ru", "http://r.ru/r\n", "http://r.ru/r\n"]}
| 426
| 112
|
coding
|
Solve the programming task below in a Python markdown code block.
Scientists working internationally use metric units almost exclusively. Unless that is, they wish to crash multimillion dollars worth of equipment on Mars.
Your task is to write a simple function that takes a number of meters, and outputs it using metric prefixes.
In practice, meters are only measured in "mm" (thousandths of a meter), "cm" (hundredths of a meter), "m" (meters) and "km" (kilometers, or clicks for the US military).
For this exercise we just want units bigger than a meter, from meters up to yottameters, excluding decameters and hectometers.
All values passed in will be positive integers.
e.g.
```python
meters(5);
// returns "5m"
meters(51500);
// returns "51.5km"
meters(5000000);
// returns "5Mm"
```
See http://en.wikipedia.org/wiki/SI_prefix for a full list of prefixes
Also feel free to reuse/extend the following starter code:
```python
def meters(x):
```
|
{"functional": "_inputs = [[1], [999], [123456], [12300000], [9000000000.0], [9000000000000.0], [9000000000000000.0], [9e+18], [9e+21], [9e+24]]\n_outputs = [['1m'], ['999m'], ['123.456km'], ['12.3Mm'], ['9Gm'], ['9Tm'], ['9Pm'], ['9Em'], ['9Zm'], ['9Ym']]\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(meters(*i), o[0])"}
| 243
| 303
|
coding
|
Solve the programming task below in a Python markdown code block.
Given a sequence of items and a specific item in that sequence, return the item immediately following the item specified. If the item occurs more than once in a sequence, return the item after the first occurence. This should work for a sequence of any type.
When the item isn't present or nothing follows it, the function should return nil in Clojure and Elixir, Nothing in Haskell, undefined in JavaScript, None in Python.
```python
next_item([1, 2, 3, 4, 5, 6, 7], 3) # => 4
next_item(['Joe', 'Bob', 'Sally'], 'Bob') # => "Sally"
```
Also feel free to reuse/extend the following starter code:
```python
def next_item(xs, item):
```
|
{"functional": "_inputs = [[[1, 2, 3, 4, 5, 6, 7, 8], 3], [['a', 'b', 'c'], 'd'], [['a', 'b', 'c'], 'c'], ['testing', 't'], ['Hello!', 'o'], ['Hello!', '!'], ['Hello!', 'x']]\n_outputs = [[4], [None], [None], ['e'], ['!'], [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(next_item(*i), o[0])"}
| 180
| 243
|
coding
|
Solve the programming task below in a Python markdown code block.
Mr. Vincent works in a door mat manufacturing company. One day, he designed a new door mat with the following specifications:
Mat size must be $N$X$\mbox{M}$. ($N$ is an odd natural number, and $\mbox{M}$ is $3$ times $N$.)
The design should have 'WELCOME' written in the center.
The design pattern should only use |, . and - characters.
Sample Designs
Size: 7 x 21
---------.|.---------
------.|..|..|.------
---.|..|..|..|..|.---
-------WELCOME-------
---.|..|..|..|..|.---
------.|..|..|.------
---------.|.---------
Size: 11 x 33
---------------.|.---------------
------------.|..|..|.------------
---------.|..|..|..|..|.---------
------.|..|..|..|..|..|..|.------
---.|..|..|..|..|..|..|..|..|.---
-------------WELCOME-------------
---.|..|..|..|..|..|..|..|..|.---
------.|..|..|..|..|..|..|.------
---------.|..|..|..|..|.---------
------------.|..|..|.------------
---------------.|.---------------
Input Format
A single line containing the space separated values of $N$ and $\mbox{M}$.
Constraints
$5<N<101$
$15<M<303$
Output Format
Output the design pattern.
Sample Input
9 27
Sample Output
------------.|.------------
---------.|..|..|.---------
------.|..|..|..|..|.------
---.|..|..|..|..|..|..|.---
----------WELCOME----------
---.|..|..|..|..|..|..|.---
------.|..|..|..|..|.------
---------.|..|..|.---------
------------.|.------------
|
{"inputs": ["9 27\n"], "outputs": ["------------.|.------------\n---------.|..|..|.---------\n------.|..|..|..|..|.------\n---.|..|..|..|..|..|..|.---\n----------WELCOME----------\n---.|..|..|..|..|..|..|.---\n------.|..|..|..|..|.------\n---------.|..|..|.---------\n------------.|.------------\n"]}
| 459
| 113
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e., the length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Please complete the following python code precisely:
```python
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(n = 5, ranges = [3,4,1,1,0,0]) == 1\n assert candidate(n = 3, ranges = [0,0,0,0]) == -1\n\n\ncheck(Solution().minTaps)"}
| 191
| 70
|
coding
|
Solve the programming task below in a Python markdown code block.
# Right in the Center
_This is inspired by one of Nick Parlante's exercises on the [CodingBat](https://codingbat.com/java) online code practice tool._
Given a sequence of characters, does `"abc"` appear in the CENTER of the sequence?
The sequence of characters could contain more than one `"abc"`.
To define CENTER, the number of characters in the sequence to the left and right of the "abc" (which is in the middle) must differ by at most one.
If it is in the CENTER, return `True`. Otherwise, return `False`.
Write a function as the solution for this problem. This kata looks simple, but it might not be easy.
## Example
is_in_middle("AAabcBB") -> True
is_in_middle("AabcBB") -> True
is_in_middle("AabcBBB") -> False
Also feel free to reuse/extend the following starter code:
```python
def is_in_middle(s):
```
|
{"functional": "_inputs = [['abc'], ['abcabcabc'], ['AAabcBBB'], ['AAAabcBB'], ['AAAAabcBB'], ['AAabcabcBB'], ['abcabcabcabc'], ['AabcBBB'], [''], ['ABC'], ['abcZ'], ['Yabc']]\n_outputs = [[True], [True], [True], [True], [False], [False], [False], [False], [False], [False], [True], [True]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(is_in_middle(*i), o[0])"}
| 222
| 240
|
coding
|
Solve the programming task below in a Python markdown code block.
One day Polycarpus got hold of two non-empty strings s and t, consisting of lowercase Latin letters. Polycarpus is quite good with strings, so he immediately wondered, how many different pairs of "x y" are there, such that x is a substring of string s, y is a subsequence of string t, and the content of x and y is the same. Two pairs are considered different, if they contain different substrings of string s or different subsequences of string t. Read the whole statement to understand the definition of different substrings and subsequences.
The length of string s is the number of characters in it. If we denote the length of the string s as |s|, we can write the string as s = s1s2... s|s|.
A substring of s is a non-empty string x = s[a... b] = sasa + 1... sb (1 ≤ a ≤ b ≤ |s|). For example, "code" and "force" are substrings or "codeforces", while "coders" is not. Two substrings s[a... b] and s[c... d] are considered to be different if a ≠ c or b ≠ d. For example, if s="codeforces", s[2...2] and s[6...6] are different, though their content is the same.
A subsequence of s is a non-empty string y = s[p1p2... p|y|] = sp1sp2... sp|y| (1 ≤ p1 < p2 < ... < p|y| ≤ |s|). For example, "coders" is a subsequence of "codeforces". Two subsequences u = s[p1p2... p|u|] and v = s[q1q2... q|v|] are considered different if the sequences p and q are different.
Input
The input consists of two lines. The first of them contains s (1 ≤ |s| ≤ 5000), and the second one contains t (1 ≤ |t| ≤ 5000). Both strings consist of lowercase Latin letters.
Output
Print a single number — the number of different pairs "x y" such that x is a substring of string s, y is a subsequence of string t, and the content of x and y is the same. As the answer can be rather large, print it modulo 1000000007 (109 + 7).
Examples
Input
aa
aa
Output
5
Input
codeforces
forceofcode
Output
60
Note
Let's write down all pairs "x y" that form the answer in the first sample: "s[1...1] t[1]", "s[2...2] t[1]", "s[1...1] t[2]","s[2...2] t[2]", "s[1...2] t[1 2]".
|
{"inputs": ["a\nb\n", "`\nb\n", "`\nc\n", "_\nb\n", "_\na\n", "b\nab\n", "ab\na\n", "b\nba\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "1\n", "1\n", "1\n"]}
| 645
| 82
|
coding
|
Solve the programming task below in a Python markdown code block.
Chef has a binary string S of length N.
In one operation, Chef can:
Select two indices i and j (1 ≤ i, j ≤ N, i \ne j) and flip S_{i} and S_{j}. (i.e. change 0 to 1 and 1 to 0)
For example, if S = 10010 and chef applys operation on i = 1 and j = 3 then: \underline{1}0\underline{0}10 \rightarrow 00110.
Find if it is possible to convert S to a palindrome by applying the above operation any (possibly zero) number of times.
Note: A string is called a palindrome if it reads the same backwards and forwards, for e.g. 10001 and 0110 are palindromic strings.
------ 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 length of the binary string S.
- The second line of each test case contains a binary string S of length N containing 0s and 1s only.
------ Output Format ------
For each test case, output YES if it is possible to convert S to a palindrome. Otherwise, output NO.
You can print each character of the string in uppercase or lowercase. For example, the strings YES, yes, Yes, and yEs are all considered the same.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$S$ contains $0$ and $1$ only.
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
6
101011
2
01
7
1110000
----- Sample Output 1 ------
YES
NO
YES
----- explanation 1 ------
Test case 1: We can perform the following operation:
- Select $i = 3$ and $j = 5$. Then $10\underline{1}0\underline{1}1 \rightarrow 100001$, which is a palindrome.
Test case 2: It can be proven that we can not make $S$ a palindrome using the given operation.
Test case 3: We can perform the following operations:
- Select $i = 4$ and $j = 5$. Then $111\underline{0}\underline{0}00 \rightarrow 1111100$
- Select $i = 6$ and $j = 7$. Then $11111\underline{0}\underline{0} \rightarrow 1111111$, which is a palindrome.
|
{"inputs": ["3\n6\n101011\n2\n01\n7\n1110000\n"], "outputs": ["YES\nNO\nYES\n"]}
| 637
| 42
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given an array of strings names of size n. You will create n folders in your file system such that, at the ith minute, you will create a folder with the name names[i].
Since two files cannot have the same name, if you enter a folder name that was previously used, the system will have a suffix addition to its name in the form of (k), where, k is the smallest positive integer such that the obtained name remains unique.
Return an array of strings of length n where ans[i] is the actual name the system will assign to the ith folder when you create it.
Please complete the following python code precisely:
```python
class Solution:
def getFolderNames(self, names: List[str]) -> List[str]:
```
|
{"functional": "def check(candidate):\n assert candidate(names = [\"pes\",\"fifa\",\"gta\",\"pes(2019)\"]) == [\"pes\",\"fifa\",\"gta\",\"pes(2019)\"]\n assert candidate(names = [\"gta\",\"gta(1)\",\"gta\",\"avalon\"]) == [\"gta\",\"gta(1)\",\"gta(2)\",\"avalon\"]\n assert candidate(names = [\"onepiece\",\"onepiece(1)\",\"onepiece(2)\",\"onepiece(3)\",\"onepiece\"]) == [\"onepiece\",\"onepiece(1)\",\"onepiece(2)\",\"onepiece(3)\",\"onepiece(4)\"]\n assert candidate(names = [\"wano\",\"wano\",\"wano\",\"wano\"]) == [\"wano\",\"wano(1)\",\"wano(2)\",\"wano(3)\"]\n assert candidate(names = [\"kaido\",\"kaido(1)\",\"kaido\",\"kaido(1)\"]) == [\"kaido\",\"kaido(1)\",\"kaido(2)\",\"kaido(1)(1)\"]\n\n\ncheck(Solution().getFolderNames)"}
| 170
| 295
|
coding
|
Solve the programming task below in a Python markdown code block.
Fangy the little walrus, as all the modern walruses, loves to communicate via text messaging. One day he faced the following problem: When he sends large texts, they are split into parts each containing n characters (which is the size of one text message). Thus, whole sentences and words get split!
Fangy did not like it, so he faced the task of breaking the text into minimal messages on his own so that no sentence were broken into pieces when it is sent and the number of text messages to be sent would be minimal. If two consecutive sentences are in different messages, the space between them can be ignored (Fangy does not write this space).
The little walrus's text looks in the following manner:
TEXT ::= SENTENCE | SENTENCE SPACE TEXT
SENTENCE ::= WORD SPACE SENTENCE | WORD END
END ::= {'.', '?', '!'}
WORD ::= LETTER | LETTER WORD
LETTER ::= {'a'..'z', 'A'..'Z'}
SPACE ::= ' '
SPACE stands for the symbol of a space.
So, how many messages did Fangy send?
Input
The first line contains an integer n, which is the size of one message (2 ≤ n ≤ 255). The second line contains the text. The length of the text does not exceed 104 characters. It is guaranteed that the text satisfies the above described format. Specifically, this implies that the text is not empty.
Output
On the first and only line print the number of text messages Fangy will need. If it is impossible to split the text, print "Impossible" without the quotes.
Examples
Input
25
Hello. I am a little walrus.
Output
2
Input
2
How are you?
Output
Impossible
Input
19
Hello! Do you like fish? Why?
Output
3
Note
Let's take a look at the third sample. The text will be split into three messages: "Hello!", "Do you like fish?" and "Why?".
|
{"inputs": ["2\na. b.\n", "4\na. A.\n", "2\na- b.\n", "8\nabc! ab.\n", "5\nabc. abcd.\n", "5\na. b. c. d.\n", "5\na. b. c- d.\n", "5\na. b/ c- d.\n"], "outputs": ["2\n", "2\n", "Impossible\n", "1\n", "2\n", "2\n", "2\n", "Impossible\n"]}
| 442
| 123
|
coding
|
Solve the programming task below in a Python markdown code block.
The stardate is 1983, and Princess Heidi is getting better at detecting the Death Stars. This time, two Rebel spies have yet again given Heidi two maps with the possible locations of the Death Star. Since she got rid of all double agents last time, she knows that both maps are correct, and indeed show the map of the solar system that contains the Death Star. However, this time the Empire has hidden the Death Star very well, and Heidi needs to find a place that appears on both maps in order to detect the Death Star.
The first map is an N × M grid, each cell of which shows some type of cosmic object that is present in the corresponding quadrant of space. The second map is an M × N grid. Heidi needs to align those two maps in such a way that they overlap over some M × M section in which all cosmic objects are identical. Help Heidi by identifying where such an M × M section lies within both maps.
-----Input-----
The first line of the input contains two space-separated integers N and M (1 ≤ N ≤ 2000, 1 ≤ M ≤ 200, M ≤ N). The next N lines each contain M lower-case Latin characters (a-z), denoting the first map. Different characters correspond to different cosmic object types. The next M lines each contain N characters, describing the second map in the same format.
-----Output-----
The only line of the output should contain two space-separated integers i and j, denoting that the section of size M × M in the first map that starts at the i-th row is equal to the section of the second map that starts at the j-th column. Rows and columns are numbered starting from 1.
If there are several possible ways to align the maps, Heidi will be satisfied with any of those. It is guaranteed that a solution exists.
-----Example-----
Input
10 5
somer
andom
noise
mayth
eforc
ebewi
thyou
hctwo
again
noise
somermayth
andomeforc
noiseebewi
againthyou
noisehctwo
Output
4 6
-----Note-----
The 5-by-5 grid for the first test case looks like this:
mayth
eforc
ebewi
thyou
hctwo
|
{"inputs": ["1 1\ng\ng\n", "1 1\ng\ng\n", "10 5\nsomer\nandom\nnoise\nmayth\neforc\nebewi\nthyou\nhctwo\nagain\nnoise\nsomermayth\nandomeforc\nnoiseebewi\nagainthyou\nnoisehctwo\n", "10 5\nsomer\nandom\nnoise\nmayth\neforc\nebewi\nthyou\nhctwo\nagain\nooise\nsomermayth\nandomeforc\nnoiseebewi\nagainthyou\nnoisehctwo\n", "10 5\nsomer\nmodna\nnoise\nmayth\neforc\nebewi\nthyou\nhctwo\nagain\nooise\nsomermayth\nandomeforc\nnoiseebewi\nagainthyou\nnoisehctwo\n", "10 5\nsomer\nmodna\nnoise\nmayth\neforc\nebewi\nthyou\nhctwo\nagain\nooise\nsomermayth\nandomeforc\nnoiseebewi\nagainthyou\nmoisehctwo\n", "10 5\nsomer\nandom\nnoise\nmayth\neforc\nebewi\nthyou\nhctwo\nagain\noiose\nsomermayth\nandomeforc\nnoiseebewi\nagainthyou\nnoisehctwo\n", "10 5\nsomer\nmodna\nnoise\nmayth\neforc\nebewi\nthyou\nhctwo\nagain\nooise\nsomermayth\naodomeforc\nnoiseebewi\nagainthyou\nnoisehctwo\n"], "outputs": ["1 1\n", "1 1", "4 6\n", "4 6\n", "4 6\n", "4 6\n", "4 6\n", "4 6\n"]}
| 499
| 432
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible.
We remind that in a sequence of numbers a_1, a_2, ..., a_{k} the element a_{i} is a record if for every integer j (1 ≤ j < i) the following holds: a_{j} < a_{i}.
-----Input-----
The first line contains the only integer n (1 ≤ n ≤ 10^5) — the length of the permutation.
The second line contains n integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the permutation. All the integers are distinct.
-----Output-----
Print the only integer — the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one.
-----Examples-----
Input
1
1
Output
1
Input
5
5 1 2 3 4
Output
5
-----Note-----
In the first example the only element can be removed.
|
{"inputs": ["1\n1\n", "1\n1\n", "3\n3 2 1\n", "3\n3 2 1\n", "5\n5 1 2 3 4\n", "5\n4 3 5 1 2\n", "5\n2 3 4 1 5\n", "5\n1 2 3 4 5\n"], "outputs": ["1\n", "1\n", "1\n", "1\n", "5\n", "1\n", "1\n", "1\n"]}
| 246
| 126
|
coding
|
Solve the programming task below in a Python markdown code block.
Artem has an array of n positive integers. Artem decided to play with it. The game consists of n moves. Each move goes like this. Artem chooses some element of the array and removes it. For that, he gets min(a, b) points, where a and b are numbers that were adjacent with the removed number. If the number doesn't have an adjacent number to the left or right, Artem doesn't get any points.
After the element is removed, the two parts of the array glue together resulting in the new array that Artem continues playing with. Borya wondered what maximum total number of points Artem can get as he plays this game.
-----Input-----
The first line contains a single integer n (1 ≤ n ≤ 5·10^5) — the number of elements in the array. The next line contains n integers a_{i} (1 ≤ a_{i} ≤ 10^6) — the values of the array elements.
-----Output-----
In a single line print a single integer — the maximum number of points Artem can get.
-----Examples-----
Input
5
3 1 5 2 6
Output
11
Input
5
1 2 3 4 5
Output
6
Input
5
1 100 101 100 1
Output
102
|
{"inputs": ["1\n4\n", "1\n4\n", "1\n1\n", "1\n87\n", "1\n87\n", "1\n79\n", "1\n41\n", "1\n52\n"], "outputs": ["0\n", "0", "0\n", "0\n", "0", "0\n", "0\n", "0\n"]}
| 298
| 89
|
coding
|
Solve the programming task below in a Python markdown code block.
We have a string S of length N consisting of A, B, and C.
You can do the following operation on S zero or more times:
- Choose i (1 \leq i \leq |S| - 1) such that S_i \neq S_{i + 1}. Replace S_i with the character (among A, B, and C) that is different from both S_i and S_{i + 1}, and remove S_{i + 1} from S.
Find the number of distinct strings that S can be after zero or more operations, and print the count modulo (10^9+7).
-----Constraints-----
- 1 \leq N \leq 10^6
- S is a string of length N consisting of A, B, and C.
-----Input-----
Input is given from Standard Input in the following format:
N
S
-----Output-----
Print the number of distinct strings that S can be after zero or more operations, modulo (10^9+7).
-----Sample Input-----
5
ABAAC
-----Sample Output-----
11
For example, the following sequence of operations turns S into ACB:
- First, choose i=2. We replace S_2 with C and remove S_3, turning S into ACAC.
- Then, choose i=3. We replace S_3 with B and remove S_4, turning S into ACB.
|
{"inputs": ["1\nA\n", "1\nB\n", "1\nC\n", "2\nAB\n", "2\nAC\n", "2\nBA\n", "2\nBC\n", "2\nCA\n"], "outputs": ["1\n", "1\n", "1\n", "2\n", "2\n", "2\n", "2\n", "2\n"]}
| 313
| 86
|
coding
|
Solve the programming task below in a Python markdown code block.
## Problem
Determine whether a positive integer number is **colorful** or not.
`263` is a colorful number because `[2, 6, 3, 2*6, 6*3, 2*6*3]` are all different; whereas `236` is not colorful, because `[2, 3, 6, 2*3, 3*6, 2*3*6]` have `6` twice.
So take all consecutive subsets of digits, take their product and ensure all the products are different.
## Examples
```pyhton
263 --> true
236 --> false
```
Also feel free to reuse/extend the following starter code:
```python
def colorful(number):
```
|
{"functional": "_inputs = [[5], [23], [263], [235789], [50], [13], [236], [2357893]]\n_outputs = [[True], [True], [True], [True], [False], [False], [False], [False]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(colorful(*i), o[0])"}
| 180
| 214
|
coding
|
Solve the programming task below in a Python markdown code block.
Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and unpredictable. A palindromic number is another matter. It is aesthetically pleasing, and it has a number of remarkable properties. Help Rikhail to convince the scientific community in this!
Let us remind you that a number is called prime if it is integer larger than one, and is not divisible by any positive integer other than itself and one.
Rikhail calls a number a palindromic if it is integer, positive, and its decimal representation without leading zeros is a palindrome, i.e. reads the same from left to right and right to left.
One problem with prime numbers is that there are too many of them. Let's introduce the following notation: π(n) — the number of primes no larger than n, rub(n) — the number of palindromic numbers no larger than n. Rikhail wants to prove that there are a lot more primes than palindromic ones.
He asked you to solve the following problem: for a given value of the coefficient A find the maximum n, such that π(n) ≤ A·rub(n).
-----Input-----
The input consists of two positive integers p, q, the numerator and denominator of the fraction that is the value of A ($A = \frac{p}{q}$, $p, q \leq 10^{4}, \frac{1}{42} \leq \frac{p}{q} \leq 42$).
-----Output-----
If such maximum number exists, then print it. Otherwise, print "Palindromic tree is better than splay tree" (without the quotes).
-----Examples-----
Input
1 1
Output
40
Input
1 42
Output
1
Input
6 4
Output
172
|
{"inputs": ["1 1\n", "6 4\n", "3 1\n", "5 8\n", "4 9\n", "5 8\n", "4 9\n", "3 1\n"], "outputs": ["40\n", "172\n", "2530\n", "16\n", "10\n", "16", "10", "2530"]}
| 402
| 96
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given a string s consisting only of lowercase English letters. We call a substring special if it contains no character which has occurred at least twice (in other words, it does not contain a repeating character). Your task is to count the number of special substrings. For example, in the string "pop", the substring "po" is a special substring, however, "pop" is not special (since 'p' has occurred twice).
Return the number of special substrings.
A substring is a contiguous sequence of characters within a string. For example, "abc" is a substring of "abcd", but "acd" is not.
Please complete the following python code precisely:
```python
class Solution:
def numberOfSpecialSubstrings(self, s: str) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(s = \"abcd\") == 10\n assert candidate(s = \"ooo\") == 3\n assert candidate(s = \"abab\") == 7\n\n\ncheck(Solution().numberOfSpecialSubstrings)"}
| 179
| 59
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You have k bags. You are given a 0-indexed integer array weights where weights[i] is the weight of the ith marble. You are also given the integer k.
Divide the marbles into the k bags according to the following rules:
No bag is empty.
If the ith marble and jth marble are in a bag, then all marbles with an index between the ith and jth indices should also be in that same bag.
If a bag consists of all the marbles with an index from i to j inclusively, then the cost of the bag is weights[i] + weights[j].
The score after distributing the marbles is the sum of the costs of all the k bags.
Return the difference between the maximum and minimum scores among marble distributions.
Please complete the following python code precisely:
```python
class Solution:
def putMarbles(self, weights: List[int], k: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(weights = [1,3,5,1], k = 2) == 4\n assert candidate(weights = [1, 3], k = 2) == 0\n\n\ncheck(Solution().putMarbles)"}
| 209
| 63
|
coding
|
Solve the programming task below in a Python markdown code block.
You have N cups and 1 ball.
The cups are arranged in a row, from left to right.
You turned down all the cups, then inserted the ball into the leftmost cup.
Then, you will perform the following Q operations:
* The i-th operation: swap the positions of the A_i-th and B_i-th cups from the left. If one of these cups contains the ball, the ball will also move.
Since you are a magician, you can cast a magic described below:
* Magic: When the ball is contained in the i-th cup from the left, teleport the ball into the adjacent cup (that is, the (i-1)-th or (i+1)-th cup, if they exist).
The magic can be cast before the first operation, between two operations, or after the last operation, but you are allowed to cast it at most once during the whole process.
Find the number of cups with a possibility of containing the ball after all the operations and possibly casting the magic.
Constraints
* 2 \leq N \leq 10^5
* 1 \leq Q \leq 10^5
* 1 \leq A_i < B_i \leq N
Input
The input is given from Standard Input in the following format:
N Q
A_1 B_1
A_2 B_2
:
A_Q B_Q
Output
Print the number of cups with a possibility of eventually containing the ball.
Examples
Input
10 3
1 3
2 4
4 5
Output
4
Input
20 3
1 7
8 20
1 19
Output
5
|
{"inputs": ["10 3\n1 3\n2 8\n4 5", "10 3\n1 3\n2 8\n4 2", "14 1\n1 3\n2 8\n5 5", "14 3\n2 1\n2 8\n5 7", "10 3\n1 3\n2 8\n5 5", "10 3\n1 3\n2 8\n4 1", "14 3\n1 3\n2 8\n5 5", "14 1\n1 3\n4 8\n5 5"], "outputs": ["5\n", "4\n", "3\n", "6\n", "4\n", "5\n", "4\n", "3\n"]}
| 371
| 182
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given an integer, $N$. Write a program to determine if $N$ is an element of the Fibonacci sequence.
The first few elements of the Fibonacci sequence are $0,1,1,2,3,5,8,13,\cdots$. A Fibonacci sequence is one where every element is a sum of the previous two elements in the sequence. The first two elements are ${0}$ and ${1}$.
Formally:
$\begin{aligned}&\textit{fib}_0=0\\ &\textit{fib}_1=1\\ &\vdots\\ &\textit{fib}_n=fib_{n-1}+fib_{n-2}\forall n>1\end{aligned}$
Function Description
Complete the isFibo function in the editor below.
isFibo has the following parameters:
- int n: the number to check
Returns
- string: either IsFibo or IsNotFibo
Input Format
The first line contains ${t}}$, number of test cases.
${t}}$ lines follow. Each line contains an integer $n$.
Constraints
$1\leq t\leq10^5$
$1\leq n\leq10^{10}$
Sample Input
STDIN Function
----- --------
3 t = 3
5 n = 5
7 n = 7
8 n = 8
Sample Output
IsFibo
IsNotFibo
IsFibo
Explanation
$5$ is a Fibonacci number given by ${fib}_5=3+2$
$7$ is not a Fibonacci number
$8$ is a Fibonacci number given by ${fib}_6=5+3$
Time Limit
The time limit for this challenge is given here.
|
{"inputs": ["3\n5\n7\n8\n"], "outputs": ["IsFibo\nIsNotFibo\nIsFibo\n"]}
| 401
| 31
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef has defined a new type of rooted tree - divisor tree. In this tree, every node has a positive integer written on it. It follows some rules:
The root can have any positive integer written on it.
Suppose a node has the integer A written on it, and suppose A has k proper divisors. [Note: Proper divisors of an integer are all its divisors except the integer itself. 1 has no proper divisor] Then this node will have exactly k child nodes, and each of A's proper divisors will be written on exactly one of the child nodes. For example, a node with number 12 written on it would have children with the numbers 1, 2, 3, 4, and 6 written on them.
You can observe that the nodes have 1 written on them, if and only if, they are leaves.
The score of a path in this tree is defined as the sum of degrees of all of the nodes in the path. The Score of the tree is defined as the maximum score of a path from the root to one of the leaves.
You are given two integers A, B. You want to find the sum of Scores of all the divisor trees which have n written on their root, where A ≤ n ≤ B.
------ Input ------
The only line of the input contains two space separated integers A and B respectively.
------ Output ------
Output a single integer corresponding to the answer of the problem.
------ Constraints ------
$1 ≤ A ≤ B ≤ 10^{12}$
$ B - A < 10^{5}$
------ Subtasks ------
Subtask #1 (10 points):
$1 ≤ A ≤ B ≤ 50$
Subtask #2 (25 points):
$1 ≤ A ≤ B ≤ 10^{6}$
$ B - A < 10^{5}$
Subtask #3 (25 points):
$ A = B $
Subtask #4 (40 points):
$Original constraints.$
----- Sample Input 1 ------
11 12
----- Sample Output 1 ------
14
----- explanation 1 ------
----- Sample Input 2 ------
932451 935212
----- Sample Output 2 ------
101245
----- explanation 2 ------
Input 1.
Here we have, A = 11 and B = 12.
The Score of the divisor tree which has 12 written on its root is 12. This because the path 12 -> 6 -> 3 -> 1 (look at the figure below) has sum of the degrees of all nodes in it = 5 + 4 + 2 + 1 = 12. This is the maximum score of a path among all paths from root to the leaves. Hence, the Score of this tree is 12.
Note that in the figure, the nodes are denoted by (value written on it, degree of node), and the leaves are marked green.
You can find that the score of divisor tree which has 11 written on its root is 2.
Hence, answer will be 12 + 2 = 14.
|
{"inputs": ["11 12", "932451 935212"], "outputs": ["14", "101245"]}
| 714
| 40
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given two integers $l$ and $r$, $l\le r$. Find the largest possible value of $a mod b$ over all pairs $(a, b)$ of integers for which $r\ge a \ge b \ge l$.
As a reminder, $a mod b$ is a remainder we get when dividing $a$ by $b$. For example, $26 mod 8 = 2$.
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ $(1\le t\le 10^4)$, denoting the number of test cases. Description of the test cases follows.
The only line of each test case contains two integers $l$, $r$ ($1\le l \le r \le 10^9$).
-----Output-----
For every test case, output the largest possible value of $a mod b$ over all pairs $(a, b)$ of integers for which $r\ge a \ge b \ge l$.
-----Examples-----
Input
4
1 1
999999999 1000000000
8 26
1 999999999
Output
0
1
12
499999999
-----Note-----
In the first test case, the only allowed pair is $(a, b) = (1, 1)$, for which $a mod b = 1 mod 1 = 0$.
In the second test case, the optimal choice is pair $(a, b) = (1000000000, 999999999)$, for which $a mod b = 1$.
|
{"inputs": ["4\n1 1\n9312924 1000001000\n6 51\n1 481905067\n", "4\n1 1\n7655209 1000001000\n6 85\n1 481905067\n", "4\n1 1\n9312924 1000001000\n6 70\n1 481905067\n", "4\n1 1\n7655209 1000001000\n6 56\n1 481905067\n", "4\n1 1\n2508889 1010100000\n9 26\n1 999999999\n", "4\n1 1\n2508889 1010100000\n9 42\n1 999999999\n", "4\n1 1\n13116759 1000001000\n6 70\n1 68132153\n", "4\n1 1\n47273429 1000000000\n9 26\n1 999999999\n"], "outputs": ["0\n500000499\n25\n240952533\n", "0\n500000499\n42\n240952533\n", "0\n500000499\n34\n240952533\n", "0\n500000499\n27\n240952533\n", "0\n505049999\n12\n499999999\n", "0\n505049999\n20\n499999999\n", "0\n500000499\n34\n34066076\n", "0\n499999999\n12\n499999999\n"]}
| 393
| 574
|
coding
|
Solve the programming task below in a Python markdown code block.
Cheesy Cheeseman just got a new monitor! He is happy with it, but he just discovered that his old desktop wallpaper no longer fits. He wants to find a new wallpaper, but does not know which size wallpaper he should be looking for, and alas, he just threw out the new monitor's box. Luckily he remembers the width and the aspect ratio of the monitor from when Bob Mortimer sold it to him. Can you help Cheesy out?
# The Challenge
Given an integer `width` and a string `ratio` written as `WIDTH:HEIGHT`, output the screen dimensions as a string written as `WIDTHxHEIGHT`.
Also feel free to reuse/extend the following starter code:
```python
def find_screen_height(width, ratio):
```
|
{"functional": "_inputs = [[1024, '4:3'], [1280, '16:9'], [3840, '32:9'], [1600, '4:3'], [1280, '5:4'], [2160, '3:2'], [1920, '16:9'], [5120, '32:9']]\n_outputs = [['1024x768'], ['1280x720'], ['3840x1080'], ['1600x1200'], ['1280x1024'], ['2160x1440'], ['1920x1080'], ['5120x1440']]\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(find_screen_height(*i), o[0])"}
| 167
| 327
|
coding
|
Solve the programming task below in a Python markdown code block.
Harish has decided to go to Arya's hotel this morning. We all know he is crazy for masala dosas. And as usual he is always hungry. He decided to order all the masala dosas at once. But then he realised that he did not have enough money to buy all of them. So he decided to share the amount with his friend Ozil. But both of them are fans of even numbers. Both of them says they want to eat even number of dosas. Ozil is ready to put the share if and only if , he is sure that he can get even number of dosas. So given N number of dosas can you please help Harish to decide, if he will be able to get all the dosas at once from the hotel.
-----Input-----
The first line of input contains an integer T which denotes the number of test files. Next T lines contains an integer N where N is the total number of dosas.
-----Output-----
Print "YES" if both can get even number of dosas. If it is not possible print "NO".
-----Constraints-----
- 1 ≤ T ≤ 10^6
- 1 ≤ N ≤ 10^18
-----Subtasks-----
Subtask #1 : (20 points)
- 1 ≤ T ≤ 10
- 1 ≤ N≤ 100
Subtask 2 : (80 points)
- 1 ≤ T ≤ 10^6
- 1 ≤ N≤ 10^18
-----Example-----
Input:
2
16
27
Output:
YES
NO
|
{"inputs": ["2\n16\n27"], "outputs": ["YES\nNO"]}
| 353
| 20
|
coding
|
Solve the programming task below in a Python markdown code block.
problem
AOR Ika made a set $ S = \\ {a_1, ..., a_N \\} $ and a map $ f: S → S $. $ f (a_i) = b_i $. For any element $ x $ in the set $ S $, all maps $ g, h: S → S $ satisfying $ g (f (x)) = h (f (x)) $ are $ g (x). ) = Determine if h (x) $ is satisfied, and if not, configure one counterexample.
Example
Input
5
1 2 3 4 5
3 4 2 5 1
Output
Yes
|
{"inputs": ["5\n2 1 3 4 5\n3 4 2 5 1", "5\n1 2 3 4 5\n2 4 3 5 1", "5\n1 3 2 4 5\n3 4 2 5 1", "5\n2 1 3 4 5\n2 4 3 5 1", "5\n1 4 3 2 5\n3 4 2 5 1", "5\n2 1 3 4 5\n3 4 2 5 1", "5\n1 2 3 4 5\n2 4 3 5 1", "5\n2 1 3 4 5\n3 4 1 5 2"], "outputs": ["Yes\n", "Yes\n", "Yes\n", "Yes\n", "Yes\n", "Yes\n", "Yes\n", "Yes\n"]}
| 153
| 222
|
coding
|
Solve the programming task below in a Python markdown code block.
Takahashi will take part in an eating contest. Teams of N members will compete in this contest, and Takahashi's team consists of N players numbered 1 through N from youngest to oldest. The consumption coefficient of Member i is A_i.
In the contest, N foods numbered 1 through N will be presented, and the difficulty of Food i is F_i. The details of the contest are as follows:
- A team should assign one member to each food, and should not assign the same member to multiple foods.
- It will take x \times y seconds for a member to finish the food, where x is the consumption coefficient of the member and y is the difficulty of the dish.
- The score of a team is the longest time it takes for an individual member to finish the food.
Before the contest, Takahashi's team decided to do some training. In one set of training, a member can reduce his/her consumption coefficient by 1, as long as it does not go below 0. However, for financial reasons, the N members can do at most K sets of training in total.
What is the minimum possible score of the team, achieved by choosing the amounts of members' training and allocating the dishes optimally?
-----Constraints-----
- All values in input are integers.
- 1 \leq N \leq 2 \times 10^5
- 0 \leq K \leq 10^{18}
- 1 \leq A_i \leq 10^6\ (1 \leq i \leq N)
- 1 \leq F_i \leq 10^6\ (1 \leq i \leq N)
-----Input-----
Input is given from Standard Input in the following format:
N K
A_1 A_2 ... A_N
F_1 F_2 ... F_N
-----Output-----
Print the minimum possible score of the team.
-----Sample Input-----
3 5
4 2 1
2 3 1
-----Sample Output-----
2
They can achieve the score of 2, as follows:
- Member 1 does 4 sets of training and eats Food 2 in (4-4) \times 3 = 0 seconds.
- Member 2 does 1 set of training and eats Food 3 in (2-1) \times 1 = 1 second.
- Member 3 does 0 sets of training and eats Food 1 in (1-0) \times 2 = 2 seconds.
They cannot achieve a score of less than 2, so the answer is 2.
|
{"inputs": ["3 1\n4 2 1\n2 3 1", "3 6\n4 2 1\n2 3 1", "3 1\n8 2 1\n2 3 1", "3 8\n4 1 1\n2 3 1", "3 1\n2 2 1\n2 3 1", "3 6\n4 4 1\n2 3 1", "3 2\n8 2 1\n2 3 1", "3 1\n8 2 1\n1 3 1"], "outputs": ["4\n", "1\n", "7\n", "0\n", "3\n", "2\n", "6\n", "7\n"]}
| 565
| 174
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
In the universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.
Rick stated that magnetic force between two different balls at positions x and y is |x - y|.
Given the integer array position and the integer m. Return the required force.
Please complete the following python code precisely:
```python
class Solution:
def maxDistance(self, position: List[int], m: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(position = [1,2,3,4,7], m = 3) == 3\n assert candidate(position = [5,4,3,2,1,1000000000], m = 2) == 999999999\n\n\ncheck(Solution().maxDistance)"}
| 159
| 88
|
coding
|
Solve the programming task below in a Python markdown code block.
British mathematician John Littlewood once said about Indian mathematician Srinivasa Ramanujan that "every positive integer was one of his personal friends."
It turns out that positive integers can also be friends with each other! You are given an array a of distinct positive integers.
Define a subarray a_i, a_{i+1}, …, a_j to be a friend group if and only if there exists an integer m ≥ 2 such that a_i mod m = a_{i+1} mod m = … = a_j mod m, where x mod y denotes the remainder when x is divided by y.
Your friend Gregor wants to know the size of the largest friend group in a.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 2⋅ 10^4).
Each test case begins with a line containing the integer n (1 ≤ n ≤ 2 ⋅ 10^5), the size of the array a.
The next line contains n positive integers a_1, a_2, …, a_n (1 ≤ a_i ≤ {10}^{18}), representing the contents of the array a. It is guaranteed that all the numbers in a are distinct.
It is guaranteed that the sum of n over all test cases is less than 2⋅ 10^5.
Output
Your output should consist of t lines. Each line should consist of a single integer, the size of the largest friend group in a.
Example
Input
4
5
1 5 2 4 6
4
8 2 5 10
2
1000 2000
8
465 55 3 54 234 12 45 78
Output
3
3
2
6
Note
In the first test case, the array is [1,5,2,4,6]. The largest friend group is [2,4,6], since all those numbers are congruent to 0 modulo 2, so m=2.
In the second test case, the array is [8,2,5,10]. The largest friend group is [8,2,5], since all those numbers are congruent to 2 modulo 3, so m=3.
In the third case, the largest friend group is [1000,2000]. There are clearly many possible values of m that work.
|
{"inputs": ["1\n3\n2 8943586220 45777194315\n", "1\n3\n2 7886764954 45777194315\n", "1\n3\n1 8943586220 276932169986\n", "1\n3\n1 8943586220 328707442366\n", "1\n3\n2 8943586220 328707442366\n", "1\n3\n2 8943586220 630301212521\n", "1\n3\n2 8943586220 844669376002\n", "1\n3\n2 8943586220 275585903615\n"], "outputs": ["3\n", "2\n", "2\n", "2\n", "3\n", "3\n", "3\n", "3\n"]}
| 546
| 292
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
A binary string is monotone increasing if it consists of some number of 0's (possibly none), followed by some number of 1's (also possibly none).
You are given a binary string s. You can flip s[i] changing it from 0 to 1 or from 1 to 0.
Return the minimum number of flips to make s monotone increasing.
Please complete the following python code precisely:
```python
class Solution:
def minFlipsMonoIncr(self, s: str) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(s = \"00110\") == 1\n assert candidate(s = \"010110\") == 2\n assert candidate(s = \"00011000\") == 2\n\n\ncheck(Solution().minFlipsMonoIncr)"}
| 128
| 75
|
coding
|
Solve the programming task below in a Python markdown code block.
-----Problem Statement-----
Chef studies combinatorics. He tries to group objects by their rang (a positive integer associated with each object). He also gives the formula for calculating the number of different objects with rang N as following:
the number of different objects with rang N = F(N) = A0 + A1 * N + A2 * N2 + A3 * N3.
Now Chef wants to know how many different multisets of these objects exist such that sum of rangs of the objects in the multiset equals to S. You are given the coefficients in F(N) and the target sum S. Please, find the number of different multisets modulo 1,000,000,007.
You should consider a multiset as an unordered sequence of integers. Two multisets are different if and only if there at least exists one element which occurs X times in the first multiset but Y times in the second one, where (X ≠ Y).
-----Input-----
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains four integers A0, A1, A2, A3. The second line contains an integer S.
-----Output-----
For each test case, output a single line containing a single integer - the answer to the test case modulo 1,000,000,007.
-----Constraints-----
- 1 ≤ T ≤ 500
- 1 ≤ S ≤ 100
- 0 ≤ Ai ≤ 1000
- Sum of all S for all test cases is not greater than 500. It's guaranteed that at least one Ai is non-zero.
-----Example-----
Input:
4
1 0 0 0
1
1 0 0 0
3
0 1 0 0
2
2 3 1 4
10
Output:
1
3
3
213986343
-----Explanation-----
Example case 2.
In the second example function looks as follows F(N) = 1. So for each rang there is a single object of the rang. To get multiset with sum of rangs equal to 3, you can pick: three objects of rang 1, or one object of rang 1 and one of rang 2, or only one object of rang 3.
Example case 3.
In the third example function looks as follows F(N) = N. So, you have one distinct object of rang 1, two distinct objects of rang 2, three distinct objects of rang 3 and so on. To get
multiset with sum of rangs equal to 2, you can pick: two objects of rang 1, one of objects of rang 2 (two ways).
|
{"inputs": ["4\n1 0 0 0\n1\n1 0 0 0\n3\n0 1 0 0\n2\n2 3 1 4\n10", "4\n1 0 0 0\n1\n1 0 0 0\n3\n0 1 0 0\n2\n2 3 1 4\n12", "4\n1 0 0 0\n1\n1 0 0 0\n3\n0 1 0 0\n2\n3 3 1 4\n10", "4\n2 0 0 0\n1\n1 0 0 0\n3\n0 1 0 0\n2\n2 3 1 4\n12", "4\n2 0 1 0\n1\n1 0 0 0\n3\n0 1 0 0\n2\n2 3 1 4\n12", "4\n2 0 1 0\n1\n1 0 0 0\n5\n0 1 0 0\n2\n2 3 1 4\n12", "4\n2 0 1 0\n1\n1 0 0 0\n5\n0 1 0 0\n3\n2 3 1 4\n12", "4\n2 0 1 0\n1\n1 0 0 0\n5\n0 1 0 0\n3\n2 3 1 7\n12"], "outputs": ["1\n3\n3\n213986343", "1\n3\n3\n261439560\n", "1\n3\n3\n276164583\n", "2\n3\n3\n261439560\n", "3\n3\n3\n261439560\n", "3\n7\n3\n261439560\n", "3\n7\n6\n261439560\n", "3\n7\n6\n511008122\n"]}
| 619
| 501
|
coding
|
Solve the programming task below in a Python markdown code block.
PolandBall is a young, clever Ball. He is interested in prime numbers. He has stated a following hypothesis: "There exists such a positive integer n that for each positive integer m number n·m + 1 is a prime number".
Unfortunately, PolandBall is not experienced yet and doesn't know that his hypothesis is incorrect. Could you prove it wrong? Write a program that finds a counterexample for any n.
-----Input-----
The only number in the input is n (1 ≤ n ≤ 1000) — number from the PolandBall's hypothesis.
-----Output-----
Output such m that n·m + 1 is not a prime number. Your answer will be considered correct if you output any suitable m such that 1 ≤ m ≤ 10^3. It is guaranteed the the answer exists.
-----Examples-----
Input
3
Output
1
Input
4
Output
2
-----Note-----
A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.
For the first sample testcase, 3·1 + 1 = 4. We can output 1.
In the second sample testcase, 4·1 + 1 = 5. We cannot output 1 because 5 is prime. However, m = 2 is okay since 4·2 + 1 = 9, which is not a prime number.
|
{"inputs": ["3\n", "4\n", "1\n", "2\n", "5\n", "6\n", "7\n", "8\n"], "outputs": ["1", "2", "3", "4", "1", "4", "1", "1"]}
| 313
| 62
|
coding
|
Solve the programming task below in a Python markdown code block.
There are only 2 type of denominations in Chefland:
Coins worth 1 rupee each
Notes worth 10 rupees each
Chef wants to pay his friend exactly X rupees.
What is the minimum number of coins Chef needs to pay exactly X rupees?
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of a single line of input containing a single integer X.
------ Output Format ------
For each test case, output on a new line the minimum number of coins Chef needs to pay exactly X rupees.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ X ≤ 1000$
----- Sample Input 1 ------
4
53
100
9
11
----- Sample Output 1 ------
3
0
9
1
----- explanation 1 ------
Test case $1$: Chef can use $5$ notes and $3$ coins in the optimal case.
Test case $2$: Chef can use $10$ notes and $0$ coins in the optimal case.
Test case $3$: Chef can only use $9$ coins.
Test case $4$: Chef can use $1$ note and $1$ coin in the optimal case.
|
{"inputs": ["4\n53\n100\n9\n11\n"], "outputs": ["3\n0\n9\n1\n"]}
| 293
| 32
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.