task_type
stringclasses 1
value | problem
stringlengths 261
3.34k
| answer
stringlengths 35
6.15k
| problem_tokens
int64 62
774
| answer_tokens
int64 12
2.04k
|
|---|---|---|---|---|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given an array routes representing bus routes where routes[i] is a bus route that the ith bus repeats forever.
For example, if routes[0] = [1, 5, 7], this means that the 0th bus travels in the sequence 1 -> 5 -> 7 -> 1 -> 5 -> 7 -> 1 -> ... forever.
You will start at the bus stop source (You are not on any bus initially), and you want to go to the bus stop target. You can travel between bus stops by buses only.
Return the least number of buses you must take to travel from source to target. Return -1 if it is not possible.
Please complete the following python code precisely:
```python
class Solution:
def numBusesToDestination(self, routes: List[List[int]], source: int, target: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(routes = [[1,2,7],[3,6,7]], source = 1, target = 6) == 2\n assert candidate(routes = [[7,12],[4,5,15],[6],[15,19],[9,12,13]], source = 15, target = 12) == -1\n\n\ncheck(Solution().numBusesToDestination)"}
| 201
| 104
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.
Please complete the following python code precisely:
```python
class Solution:
def firstUniqChar(self, s: str) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(s = \"leetcode\") == 0\n assert candidate(s = \"loveleetcode\") == 2\n assert candidate(s = \"aabb\") == -1\n\n\ncheck(Solution().firstUniqChar)"}
| 78
| 59
|
coding
|
Solve the programming task below in a Python markdown code block.
Chang's new maths teacher is very enthusiastic about making sure that students understand the concept rather than remembering it. On her first day at teaching, she gives an assignment to all the students to test their potential. Chang wants to demonstrate that he understands the concept rather than rote learning. Help Chang in doing this by solving the hardest problem in the assignment. The problem is written as follows.
A perfect function is defined in the following manner.
- F(x, y) = x2 + y for 1 ≤ x ≤ A, 1 ≤ y ≤ B
- F(x, y) = 2 otherwise
Find the number of integral pairs (x, y) such that F(x, y) is a perfect square.
-----Input-----
First and the only line of the input contains two single space separated integers A and B.
-----Output-----
Output a single integer indicating the output to the only test case.
-----Constraints-----
- 1 ≤ A, B ≤ 106
-----Example-----
Input:
4 4
Output:
1
-----Explanation-----
Example case 1.
(1, 3) is the only pair such that x = 1, y = 3 satisfying x <= 4 and y <= 4. 1^2 + 3 = 4 which is a perfect square.
|
{"inputs": ["4 4"], "outputs": ["1"]}
| 284
| 14
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problems statements in [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Suppose the population is divided into the age groups $[1, 10], [11, 20], [21, 30], \ldots, [91, \infty)$. The age groups are numbered from $1$ to $10$ and there are $X_{i}$ people in age group $i$.
The COVID vaccine drive has started and people will be vaccinated in the decreasing order of their age groups. Suppose $P$ people are vaccinated per day and if less than $P$ people are left in an age group, then the remaining doses for that day are given to the people of immediate lower age group and the process for that day continues until the vaccines for that day are finished or the entire population has been vaccinated. The selection of a person from a given age group is done randomly.
Given $X$, $P$, and Chef’s age group $G$, tell the minimum and the maximum number of days will it take for Chef to get vaccinated.
------ Input ------
The first line contains an integer $T$, the number of test cases. Then the test cases follow.
Each test case contains a single line of input, twelve integers $G$, $P$, $X_{1}, X_{2}, \ldots, X_{10}$.
------ Output ------
For each test case, output in a single line two space-separated integers, the minimum and maximum time required for Chef to get vaccinated.
------ Constraints ------
$1 ≤ T ≤ 10^{4}$
$1 ≤ G ≤ 10$
$1 ≤ P, X_{i} ≤ 10^{5}$
----- Sample Input 1 ------
3
5 2 2 2 2 2 2 2 2 2 2 2
5 2 2 2 2 2 2 3 2 2 2 2
9 4 2 2 2 2 3 2 2 2 2 2
----- Sample Output 1 ------
6 6
6 7
1 1
----- explanation 1 ------
Test Case $1$: It takes one day for every age group to get vaccinated.
Test Case $2$: Age groups $10 - 7$ get vaccinated in a total of $4$ days. On the $5$-th day, $2$ out of $3$ people of age group $6$ get vaccinated. On the $6$-th day, the last person of the $6$-th age group and one random person of the $5$-th age group gets vaccinated. So if the person is Chef, the minimum time required for him to get vaccinated will be $6$ days, and if not, Chef will get vaccinated on the $7$-th day.
Test Case $3$: The last $2$ age groups get vaccinated on the same day.
|
{"inputs": ["3\n5 2 2 2 2 2 2 2 2 2 2 2\n5 2 2 2 2 2 2 3 2 2 2 2\n9 4 2 2 2 2 3 2 2 2 2 2"], "outputs": ["6 6\n6 7\n1 1"]}
| 656
| 94
|
coding
|
Solve the programming task below in a Python markdown code block.
# Fourier transformations are hard. Fouriest transformations are harder.
This Kata is based on the SMBC Comic on fourier transformations.
A fourier transformation on a number is one that converts the number to a base in which it has more `4`s ( `10` in base `6` is `14`, which has `1` four as opposed to none, hence, fourier in base `6` ).
A number's fouriest transformation converts it to the base in which it has the most `4`s.
For example: `35353` is the fouriest in base `6`: `431401`.
This kata requires you to create a method `fouriest` that takes a number and makes it the fouriest, telling us in which base this happened, as follows:
```python
fouriest(number) -> "{number} is the fouriest ({fouriest_representation}) in base {base}"
```
## Important notes
* For this kata we don't care about digits greater than `9` ( only `0` to `9` ), so we will represent all digits greater than `9` as `'x'`: `10` in base `11` is `'x'`, `119` in base `20` is `'5x'`, `118` in base `20` is also `'5x'`
* When a number has several fouriest representations, we want the one with the LOWEST base
```if:haskell,javascript
* Numbers below `9` will not be tested
```
```if:javascript
* A `BigNumber` library has been provided; documentation is [here](https://mikemcl.github.io/bignumber.js/)
```
## Examples
```python
"30 is the fouriest (42) in base 7"
"15 is the fouriest (14) in base 11"
```
Also feel free to reuse/extend the following starter code:
```python
def fouriest(i):
```
|
{"functional": "_inputs = [[30], [15], [9999], [35353], [100], [1000000243], [142042158218941532125212890], [2679388715912901287113185885289513476], [640614569414659959863091616350016384446719891887887380], [2579111107964987025047536361483312385374008248282655401675211033926782006920415224913494809688581314878892733564]]\n_outputs = [['30 is the fouriest (42) in base 7'], ['15 is the fouriest (14) in base 11'], ['9999 is the fouriest (304444) in base 5'], ['35353 is the fouriest (431401) in base 6'], ['100 is the fouriest (244) in base 6'], ['1000000243 is the fouriest (24x44) in base 149'], ['142042158218941532125212890 is the fouriest (14340031300334233041101030243023303030) in base 5'], ['2679388715912901287113185885289513476 is the fouriest (444444444444444444) in base 128'], ['640614569414659959863091616350016384446719891887887380 is the fouriest (44444444444444444444444444444444) in base 52'], ['2579111107964987025047536361483312385374008248282655401675211033926782006920415224913494809688581314878892733564 is the fouriest (4444444444444444444444444444444444444444444444) in base 290']]\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(fouriest(*i), o[0])"}
| 442
| 967
|
coding
|
Solve the programming task below in a Python markdown code block.
A pair of strings $(α, β)$ is called a subpair of a string $x$ if $x$ = $x_1+$$α+$$x_2+$$β+$$x_3$ (where a+b means concatenation of strings a and b) for some (possibly empty) strings $x1, x2$ and $x3$. We are given two strings and we need to find one subpair from each string such that :
Let $(a,b) , (c,d) $be subpair of $string1$ and $string2$ respectively and $X$ $=$ $a$ + $b$ + $c$ + $d$
- $X$ is a palindrome
- $|a| = |d|$
- $|b| = |c|$
- $|X|$ is maximum
-----Input Format:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- Each testcase contains of a single line of input, two strings str1, str2.
-----Output Format:-----
- For each testcase, output in a single line representing the length of palindrome |X|.
-----Constraints-----
- $1 \leq T \leq 5$
- $2 \leq |str1| \leq 10^3$
- $2 \leq |str2| \leq 10^3$
-----Sample Input 1:-----
1
abgcd dchbag
-----Sample Output 1:-----
8
-----Sample Input 2:-----
4
aaa aaa
zaaax yaaaw
zax yaw
zx yw
-----Sample Output 2:-----
6
6
2
0
-----EXPLANATION:-----
Sample Testcase 1: The subpairs are ("ab","cd") and ("dc","ba"). When the subpairs are concatenated string is "abcddcba" which is a pallindrome , |"ab"| = |"ba"|, |"cd"| = |"dc"| and has the maximum length equal to 8.
|
{"inputs": ["1\nabgcd dchbag", "4\naaa aaa\nzaaax yaaaw\nzax yaw\nzx yw"], "outputs": ["8", "6\n6\n2\n0"]}
| 457
| 48
|
coding
|
Solve the programming task below in a Python markdown code block.
Katie, Kuro and Shiro are best friends. They have known each other since kindergarten. That's why they often share everything with each other and work together on some very hard problems.
Today is Shiro's birthday. She really loves pizza so she wants to invite her friends to the pizza restaurant near her house to celebrate her birthday, including her best friends Katie and Kuro.
She has ordered a very big round pizza, in order to serve her many friends. Exactly $n$ of Shiro's friends are here. That's why she has to divide the pizza into $n + 1$ slices (Shiro also needs to eat). She wants the slices to be exactly the same size and shape. If not, some of her friends will get mad and go home early, and the party will be over.
Shiro is now hungry. She wants to cut the pizza with minimum of straight cuts. A cut is a straight segment, it might have ends inside or outside the pizza. But she is too lazy to pick up the calculator.
As usual, she will ask Katie and Kuro for help. But they haven't come yet. Could you help Shiro with this problem?
-----Input-----
A single line contains one non-negative integer $n$ ($0 \le n \leq 10^{18}$) — the number of Shiro's friends. The circular pizza has to be sliced into $n + 1$ pieces.
-----Output-----
A single integer — the number of straight cuts Shiro needs.
-----Examples-----
Input
3
Output
2
Input
4
Output
5
-----Note-----
To cut the round pizza into quarters one has to make two cuts through the center with angle $90^{\circ}$ between them.
To cut the round pizza into five equal parts one has to make five cuts.
|
{"inputs": ["3\n", "4\n", "0\n", "1\n", "2\n", "7\n", "8\n", "2\n"], "outputs": ["2", "5", "0", "1", "3", "4", "9", "3\n"]}
| 394
| 63
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a sequence of integers $A_1, A_2, \ldots, A_N$. This sequence is circular ― for each valid $i$, the element $A_{i+1}$ follows after $A_i$, and the element $A_1$ follows after $A_N$.
You may insert any positive integers at any positions you choose in this sequence; let's denote the resulting sequence by $B$. This sequence is also circular. For each pair of its elements $B_s$ and $B_f$, let's denote the (non-circular) sequence created by starting at $B_s$ and moving from each element to the one that follows after it, until we reach $B_f$, by $B(s, f)$. This sequence includes the elements $B_s$ and $B_f$.
For each $K$ from $2$ to $N$ inclusive, find the smallest possible number of elements that need to be inserted into $A$ to form a sequence $B$ for which there is no subsequence $B(p, q)$ such that:
- The size of $B(p, q)$ is at least $K$.
- There is no pair of consecutive elements in $B(p, q)$ such that their GCD is equal to $1$.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single integer $N$.
- The second line contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$.
-----Output-----
For each test case, print a single line containing $N-1$ space-separated integers. For each $i$ ($1 \le i \le N-1$), the $i$-th of these integers should be the smallest number of inserted elements in a valid sequence $B$ for $K = i+1$.
-----Constraints-----
- $1 \le T \le 2,000$
- $2 \le N \le 10^5$
- $1 \le A_i \le 10^9$ for each valid $i$
- the sum of $N$ over all test cases does not exceed $2 \cdot 10^6$
-----Example Input-----
1
5
3 6 4 5 9
-----Example Output-----
3 1 1 0
|
{"inputs": ["1\n5\n3 6 4 5 9"], "outputs": ["3 1 1 0"]}
| 539
| 30
|
coding
|
Solve the programming task below in a Python markdown code block.
Adam and Martha are planning to leave the city after their retirement and build a house in a huge land belonging to their family. To keep everyone happy, they want to build the house at a location having distance _a*d1_ from aunt Kimberly's house, where a is some ratio and d1 is the distance of that location to uncle Bob's house. Also, the house should be at a distance _b*d2_ from uncle Jack's house where b is some ratio and d2 is the distance of the location to aunt Janet's house.
You need to help them find the location of their house.
Input Format
The first line of input contains two integers a and b (the ratios above). In the next four lines, there are 4 pairs of integers that indicate the coordinates of Kimberly's, Bob's, Jack's, and Janet's houses, respectively.
Output Format
You must output the coordinate of house with exactly two points after decimal point (rounded to closest one hundredth). If there is no location satisfying the above constraints, output Impossible! If there are more than one possible locations, output a location with minimum x-coordinate and among the ones having the minimum x-coordinate, a location with minimum y-coordinate.
Constraints
1 < a, b <= 1000
-1000 <= all input coordinates <= 1000
Sample Input
3 4
4 0
0 0
-2 -4
-2 -1
Sample Output
-2.00 0.00
Explanation
As required, the point (-2.00, 0.00) has distance 2 from Bob's house and distance 3*2=6 from Kimberly's house. It also has distance 1 from Janet's house and distance 4*1=4 from Jack's house.
|
{"inputs": ["3 4\n4 0\n0 0\n-2 -4\n-2 -1\n"], "outputs": ["-2.00 0.00\n"]}
| 408
| 43
|
coding
|
Solve the programming task below in a Python markdown code block.
A first-year student, came to your college. Being a good senior, you must tell him if it is possible to go from College Main Gate to Hostel for him.
The college can be visualized on a 2D-plane. Suppose the College Main Gate is situated at origin i.e. at the coordinates (0, 0) and the Hostel is situated at the coordinates (x,y).
As the first-year student wants to explore the college campus further, in one move, he will increase or decrease any coordinate (either the x-coordinate or the y-coordinate) by a value of exactly K.
Is it possible for the first-year student to reach the Hostel?
------ Input Format ------
- First line of input contains a single integer T, denoting the number of test cases. Then the description of the T test case follows.
- Each test case contains one line of input.
- The first line contains three integers x, y, K.
------ Output Format ------
For each test case, output a single line answer containing a "YES" or "NO" (without quotes).
You may print each character of each string in uppercase or lowercase (for example, the strings without quotes "yEs", "yes", "Yes" and "YES" will all be treated as identical).
------ Constraints ------
$1 ≤ T ≤ 600$
$-1000 ≤ x, y ≤ 1000$
$1 ≤ K ≤ 1000$
----- Sample Input 1 ------
4
1 2 1
3 -5 2
-9 -6 3
-18 12 5
----- Sample Output 1 ------
YES
NO
YES
NO
----- explanation 1 ------
Test case 1:
Here $K = 1$, One of the paths that the first-year student can follow is described as: $(0, 0)$ to $(1, 0)$ to $(1, 1)$ to $(1, 2)$. Hence, he can reach the hostel coordinate at $(x, y) = (1, 2)$ with the $K = 1$ constraint.
Test case 2:
As here $K = 2$, it can be proved that there is no path to move from $(0, 0)$ to $(3, -5$) by only increasing or decreasing any coordinate by exactly $K = 2$.
Test case 3:
Here $K = 3$, One of the paths that the first-year student can follow is described as: $(0, 0)$ to $(-3, 0)$ to $(-3, -3)$ to $(-3, -6)$ to $(-6, -6)$ to $(-9, -6)$. Hence, he can reach the hostel coordinate at $(x, y) = (-9, -6)$ with the $K = 3$ constraint.
|
{"inputs": ["4\n1 2 1\n3 -5 2\n-9 -6 3\n-18 12 5\n"], "outputs": ["YES\nNO\nYES\nNO"]}
| 622
| 47
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given a 0-indexed array of positive integers nums. Find the number of triplets (i, j, k) that meet the following conditions:
0 <= i < j < k < nums.length
nums[i], nums[j], and nums[k] are pairwise distinct.
In other words, nums[i] != nums[j], nums[i] != nums[k], and nums[j] != nums[k].
Return the number of triplets that meet the conditions.
Please complete the following python code precisely:
```python
class Solution:
def unequalTriplets(self, nums: List[int]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(nums = [4,4,2,4,3]) == 3\n assert candidate(nums = [1,1,1,1,1]) == 0\n\n\ncheck(Solution().unequalTriplets)"}
| 146
| 61
|
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.
For a sequence of positive integers $A_{1}, A_{2}, \ldots, A_{K}$, let's define the number of inversions in it as the number of pairs of integers $(i, j)$ such that $1 ≤ i < j ≤ K$ and $A_{i} > A_{j}$.
You are given a permutation $P_{1}, P_{2}, \ldots, P_{N}$ of the integers $1$ through $N$. Determine if it is possible to partition this permutation into two subsequences (possibly empty or non-contiguous) such that:
Each element of $P$ appears in exactly one of these subsequences.
The numbers of inversions in one subsequence is equal to the number of inversions in the other subsequence.
------ 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 $P_{1}, P_{2}, \ldots, P_{N}$.
------ Output ------
For each test case, print a single line containing the string "YES" if it is possible to partition the permutation in a given way or "NO" if it is impossible.
------ Constraints ------
$1 ≤ N ≤ 100$
$1 ≤ P_{i} ≤ N$ for each valid $i$
$P_{1}, P_{2}, \ldots, P_{N}$ are pairwise distinct
the sum of $N$ over all test cases does not exceed $200$
------ Subtasks ------
Subtask #1 (20 points): $N ≤ 16$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
4
1
1
3
3 2 1
4
4 3 2 1
5
1 4 3 2 5
----- Sample Output 1 ------
YES
NO
YES
NO
----- explanation 1 ------
Example case 1: We can split $(1)$ into $(1)$ and $()$. There are $0$ inversions in each of these sequences.
Example case 3: We can split $(4, 3, 2, 1)$ into $(4, 3)$ and $(2, 1)$. There is $1$ inversion in each of them. Note that this is not the only solution ? we could also split the permutation into sequences $(4, 1)$ and $(3, 2)$.
|
{"inputs": ["4\n1\n1\n3\n3 2 1\n4\n4 3 2 1\n5 \n1 4 3 2 5"], "outputs": ["YES\nNO\nYES\nNO"]}
| 603
| 53
|
coding
|
Solve the programming task below in a Python markdown code block.
As technologies develop, manufacturers are making the process of unlocking a phone as user-friendly as possible. To unlock its new phone, Arkady's pet dog Mu-mu has to bark the password once. The phone represents a password as a string of two lowercase English letters.
Mu-mu's enemy Kashtanka wants to unlock Mu-mu's phone to steal some sensible information, but it can only bark n distinct words, each of which can be represented as a string of two lowercase English letters. Kashtanka wants to bark several words (not necessarily distinct) one after another to pronounce a string containing the password as a substring. Tell if it's possible to unlock the phone in this way, or not.
-----Input-----
The first line contains two lowercase English letters — the password on the phone.
The second line contains single integer n (1 ≤ n ≤ 100) — the number of words Kashtanka knows.
The next n lines contain two lowercase English letters each, representing the words Kashtanka knows. The words are guaranteed to be distinct.
-----Output-----
Print "YES" if Kashtanka can bark several words in a line forming a string containing the password, and "NO" otherwise.
You can print each letter in arbitrary case (upper or lower).
-----Examples-----
Input
ya
4
ah
oy
to
ha
Output
YES
Input
hp
2
ht
tp
Output
NO
Input
ah
1
ha
Output
YES
-----Note-----
In the first example the password is "ya", and Kashtanka can bark "oy" and then "ah", and then "ha" to form the string "oyahha" which contains the password. So, the answer is "YES".
In the second example Kashtanka can't produce a string containing password as a substring. Note that it can bark "ht" and then "tp" producing "http", but it doesn't contain the password "hp" as a substring.
In the third example the string "hahahaha" contains "ah" as a substring.
|
{"inputs": ["ah\n1\nha\n", "bb\n1\naa\n", "aa\n1\naa\n", "bb\n1\nbb\n", "ba\n1\ncc\n", "ha\n1\nha\n", "aa\n1\naa\n", "ez\n1\njl\n"], "outputs": ["YES\n", "NO\n", "YES\n", "YES\n", "NO\n", "YES\n", "YES\n", "NO\n"]}
| 443
| 102
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given a 0-indexed binary string s and two integers minJump and maxJump. In the beginning, you are standing at index 0, which is equal to '0'. You can move from index i to index j if the following conditions are fulfilled:
i + minJump <= j <= min(i + maxJump, s.length - 1), and
s[j] == '0'.
Return true if you can reach index s.length - 1 in s, or false otherwise.
Please complete the following python code precisely:
```python
class Solution:
def canReach(self, s: str, minJump: int, maxJump: int) -> bool:
```
|
{"functional": "def check(candidate):\n assert candidate(s = \"011010\", minJump = 2, maxJump = 3) == True\n assert candidate(s = \"01101110\", minJump = 2, maxJump = 3) == False\n\n\ncheck(Solution().canReach)"}
| 159
| 77
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given an alphabet consisting of n letters, your task is to make a string of the maximum possible length so that the following conditions are satisfied: the i-th letter occurs in the string no more than a_{i} times; the number of occurrences of each letter in the string must be distinct for all the letters that occurred in the string at least once.
-----Input-----
The first line of the input contains a single integer n (2 ≤ n ≤ 26) — the number of letters in the alphabet.
The next line contains n integers a_{i} (1 ≤ a_{i} ≤ 10^9) — i-th of these integers gives the limitation on the number of occurrences of the i-th character in the string.
-----Output-----
Print a single integer — the maximum length of the string that meets all the requirements.
-----Examples-----
Input
3
2 5 5
Output
11
Input
3
1 1 2
Output
3
-----Note-----
For convenience let's consider an alphabet consisting of three letters: "a", "b", "c". In the first sample, some of the optimal strings are: "cccaabbccbb", "aabcbcbcbcb". In the second sample some of the optimal strings are: "acc", "cbc".
|
{"inputs": ["2\n1 1\n", "2\n1 1\n", "2\n1 2\n", "2\n1 0\n", "3\n2 5 5\n", "3\n1 1 2\n", "3\n1 1 1\n", "3\n1 1 1\n"], "outputs": ["1\n", "1\n", "3\n", "1\n", "11\n", "3\n", "1\n", "1\n"]}
| 293
| 111
|
coding
|
Solve the programming task below in a Python markdown code block.
Due to the COVID pandemic, there has been an increase in the number of cases if a hospital. The management has decided to clear a large square area for the patients and arrange for beds. But the beds can't be too near to each other.
The area is of dimension $N$ x $N$
The whole area is already divided into blocks. $1$ means there's a bed in the block, $0$ denotes there isn't. Note, beds placed on consecutive diagonal blocks are safe.
This is a SAFE example:
1 0
0 1
This is an UNSAFE example:
0 1 1
0 0 0
1 0 0
To avoid spreading the virus even further, you have to make sure no two adjacent blocks have beds in them. This is done to maintain distance between beds.
Return an output of "SAFE" if you find the workers have arranged the beds with due consideration to the distance needed. Return "UNSAFE" otherwise.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- Next line contains $N$.
- Next $N$ lines will contain $N$ number of space-separated integers $Ai$ which make denote the beds in the area.
-----Output:-----
For each test case, output in a single line whether the total arrangement is "SAFE" or "UNSAFE". Even if there's a single unsafe bed in the whole area, report the whole area as "UNSAFE".
-----Constraints-----
- $1 \leq T \leq 100$
- $0 \leq Ai \leq 1$
- $2 \leq N \leq 100$
-----Subtasks-----
- 30 points : $1 \leq N \leq 7$
- 70 points : Original constraints
-----Sample Input:-----
2
4
1 0 1 0
0 0 0 1
0 1 0 0
1 0 0 1
4
1 0 1 0
0 0 0 0
1 0 1 1
0 1 0 0
-----Sample Output:-----
SAFE
UNSAFE
-----EXPLANATION:-----
Beds placed on blocks diagonally are not a problem.
|
{"inputs": ["2\n4\n1 0 1 0\n0 0 0 1\n0 1 0 0\n1 0 0 1\n4\n1 0 1 0\n0 0 0 0\n1 0 1 1\n0 1 0 0"], "outputs": ["SAFE\nUNSAFE"]}
| 505
| 83
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Run-length encoding is a compression algorithm that allows for an integer array nums with many segments of consecutive repeated numbers to be represented by a (generally smaller) 2D array encoded. Each encoded[i] = [vali, freqi] describes the ith segment of repeated numbers in nums where vali is the value that is repeated freqi times.
For example, nums = [1,1,1,2,2,2,2,2] is represented by the run-length encoded array encoded = [[1,3],[2,5]]. Another way to read this is "three 1's followed by five 2's".
The product of two run-length encoded arrays encoded1 and encoded2 can be calculated using the following steps:
Expand both encoded1 and encoded2 into the full arrays nums1 and nums2 respectively.
Create a new array prodNums of length nums1.length and set prodNums[i] = nums1[i] * nums2[i].
Compress prodNums into a run-length encoded array and return it.
You are given two run-length encoded arrays encoded1 and encoded2 representing full arrays nums1 and nums2 respectively. Both nums1 and nums2 have the same length. Each encoded1[i] = [vali, freqi] describes the ith segment of nums1, and each encoded2[j] = [valj, freqj] describes the jth segment of nums2.
Return the product of encoded1 and encoded2.
Note: Compression should be done such that the run-length encoded array has the minimum possible length.
Please complete the following python code precisely:
```python
class Solution:
def findRLEArray(self, encoded1: List[List[int]], encoded2: List[List[int]]) -> List[List[int]]:
```
|
{"functional": "def check(candidate):\n assert candidate(encoded1 = [[1,3],[2,3]], encoded2 = [[6,3],[3,3]]) == [[6,6]]\n assert candidate(encoded1 = [[1,3],[2,1],[3,2]], encoded2 = [[2,3],[3,3]]) == [[2,3],[6,1],[9,2]]\n\n\ncheck(Solution().findRLEArray)"}
| 381
| 103
|
coding
|
Solve the programming task below in a Python markdown code block.
## Number pyramid
Number pyramid is a recursive structure where each next row is constructed by adding adjacent values of the current row. For example:
```
Row 1 [1 2 3 4]
Row 2 [3 5 7]
Row 3 [8 12]
Row 4 [20]
```
___
## Task
Given the first row of the number pyramid, find the value stored in its last row.
___
## Examples
```python
reduce_pyramid([1]) == 1
reduce_pyramid([3, 5]) == 8
reduce_pyramid([3, 9, 4]) == 25
```
___
## Performance tests
```python
Number of tests: 10
List size: 10,000
```
Also feel free to reuse/extend the following starter code:
```python
def reduce_pyramid(base):
```
|
{"functional": "_inputs = [[[1]], [[3, 5]], [[3, 9, 4]], [[5, 6, 7, 8]], [[13, 1, 21, 9]], [[13, 76, 21, 42, 63]]]\n_outputs = [[1], [8], [25], [52], [88], [674]]\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(reduce_pyramid(*i), o[0])"}
| 221
| 237
|
coding
|
Solve the programming task below in a Python markdown code block.
Chef has a sequence $A_1, A_2, \ldots, A_N$. For a positive integer $M$, sequence $B$ is defined as $B = A*M$ that is, appending $A$ exactly $M$ times. For example, If $A = [1, 2]$ and $M = 3$, then $B = A*M = [1, 2, 1, 2, 1, 2]$
You have to help him to find out the minimum value of $M$ such that the length of the longest strictly increasing subsequence is maximum possible.
-----Input:-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single integer $N$.
- The second line contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$.
-----Output:-----
For each test case, print a single line containing one integer ― the minimum value of $M$.
-----Constraints-----
- $1 \le T \le 500$
- $1 \le N \le 2*10^5$
- $1 \le A_i \le 10^9$
- It's guaranteed that the total length of the sequence $A$ in one test file doesn't exceed $2*10^6$
-----Sample Input:-----
3
2
2 1
2
1 2
5
1 3 2 1 2
-----Sample Output:-----
2
1
2
-----Explanation:-----
In the first test case, Choosing $M = 2$ gives $B = [2, 1, 2, 1]$ which has a longest strictly increasing sequence of length $2$ which is the maximum possible.
In the second test case, Choosing $M = 1$ gives $B = [1, 2]$ which has a longest strictly increasing sequence of length $2$ which is the maximum possible.
|
{"inputs": ["3\n2\n2 1\n2\n1 2\n5\n1 3 2 1 2"], "outputs": ["2\n1\n2"]}
| 458
| 40
|
coding
|
Solve the programming task below in a Python markdown code block.
There are $n$ points on a coordinate axis $OX$. The $i$-th point is located at the integer point $x_i$ and has a speed $v_i$. It is guaranteed that no two points occupy the same coordinate. All $n$ points move with the constant speed, the coordinate of the $i$-th point at the moment $t$ ($t$ can be non-integer) is calculated as $x_i + t \cdot v_i$.
Consider two points $i$ and $j$. Let $d(i, j)$ be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points $i$ and $j$ coincide at some moment, the value $d(i, j)$ will be $0$.
Your task is to calculate the value $\sum\limits_{1 \le i < j \le n}$ $d(i, j)$ (the sum of minimum distances over all pairs of points).
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of points.
The second line of the input contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^8$), where $x_i$ is the initial coordinate of the $i$-th point. It is guaranteed that all $x_i$ are distinct.
The third line of the input contains $n$ integers $v_1, v_2, \dots, v_n$ ($-10^8 \le v_i \le 10^8$), where $v_i$ is the speed of the $i$-th point.
-----Output-----
Print one integer — the value $\sum\limits_{1 \le i < j \le n}$ $d(i, j)$ (the sum of minimum distances over all pairs of points).
-----Examples-----
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{"inputs": ["2\n2 1\n-3 0\n", "2\n2 1\n-4 0\n", "2\n0 1\n-4 0\n", "2\n0 2\n-4 0\n", "2\n2 1\n-5 0\n", "2\n2 1\n-4 1\n", "2\n0 2\n-7 0\n", "2\n0 1\n-4 1\n"], "outputs": ["0\n", "0\n", "1\n", "2\n", "0\n", "0\n", "2\n", "1\n"]}
| 508
| 142
|
coding
|
Solve the programming task below in a Python markdown code block.
# Task
Given string `s`, which contains only letters from `a to z` in lowercase.
A set of alphabet is given by `abcdefghijklmnopqrstuvwxyz`.
2 sets of alphabets mean 2 or more alphabets.
Your task is to find the missing letter(s). You may need to output them by the order a-z. It is possible that there is more than one missing letter from more than one set of alphabet.
If the string contains all of the letters in the alphabet, return an empty string `""`
# Example
For `s='abcdefghijklmnopqrstuvwxy'`
The result should be `'z'`
For `s='aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyy'`
The result should be `'zz'`
For `s='abbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxy'`
The result should be `'ayzz'`
For `s='codewars'`
The result should be `'bfghijklmnpqtuvxyz'`
# Input/Output
- `[input]` string `s`
Given string(s) contains one or more set of alphabets in lowercase.
- `[output]` a string
Find the letters contained in each alphabet but not in the string(s). Output them by the order `a-z`. If missing alphabet is repeated, please repeat them like `"bbccdd"`, not `"bcdbcd"`
Also feel free to reuse/extend the following starter code:
```python
def missing_alphabets(s):
```
|
{"functional": "_inputs = [['abcdefghijklmnopqrstuvwxy'], ['abcdefghijklmnopqrstuvwxyz'], ['aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyy'], ['abbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxy'], ['codewars']]\n_outputs = [['z'], [''], ['zz'], ['ayzz'], ['bfghijklmnpqtuvxyz']]\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(missing_alphabets(*i), o[0])"}
| 358
| 242
|
coding
|
Solve the programming task below in a Python markdown code block.
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya wonders eagerly what minimum lucky number has the sum of digits equal to n. Help him cope with the task.
Input
The single line contains an integer n (1 ≤ n ≤ 106) — the sum of digits of the required lucky number.
Output
Print on the single line the result — the minimum lucky number, whose sum of digits equals n. If such number does not exist, print -1.
Examples
Input
11
Output
47
Input
10
Output
-1
|
{"inputs": ["8\n", "4\n", "2\n", "1\n", "5\n", "6\n", "3\n", "9\n"], "outputs": ["44\n", "4\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n"]}
| 185
| 71
|
coding
|
Solve the programming task below in a Python markdown code block.
Thumbelina has had an accident. She has found herself on a little island in the middle of a swamp and wants to get to the shore very much.
One can get to the shore only by hills that are situated along a straight line that connects the little island with the shore. Let us assume that the hills are numbered from 1 to n and the number of a hill is equal to the distance in meters between it and the island. The distance between the n-th hill and the shore is also 1 meter.
Thumbelina is too small to make such jumps. Fortunately, a family of frogs living in the swamp suggests to help her. Each frog agrees to give Thumbelina a ride but Thumbelina should choose only one frog. Each frog has a certain jump length. If Thumbelina agrees to accept help from a frog whose jump length is d, the frog will jump from the island on the hill d, then — on the hill 2d, then 3d and so on until they get to the shore (i.e. find itself beyond the hill n).
However, there is one more problem: mosquitoes also live in the swamp. At the moment they have a siesta, and they are having a nap on some hills. If the frog jumps on a hill with a mosquito the frog will smash it. The frogs Thumbelina has met are pacifists, so they will find the death of each mosquito very much sad. Help Thumbelina choose a frog that will bring her to the shore and smash as small number of mosquitoes as possible.
Input
The first line contains three integers n, m and k (1 ≤ n ≤ 109, 1 ≤ m, k ≤ 100) — the number of hills, frogs and mosquitoes respectively. The second line contains m integers di (1 ≤ di ≤ 109) — the lengths of the frogs’ jumps. The third line contains k integers — the numbers of the hills on which each mosquito is sleeping. No more than one mosquito can sleep on each hill. The numbers in the lines are separated by single spaces.
Output
In the first line output the number of frogs that smash the minimal number of mosquitoes, in the second line — their numbers in increasing order separated by spaces. The frogs are numbered from 1 to m in the order of the jump length given in the input data.
Examples
Input
5 3 5
2 3 4
1 2 3 4 5
Output
2
2 3
Input
1000000000 2 3
2 5
999999995 999999998 999999996
Output
1
2
|
{"inputs": ["1 1 1\n1\n1\n", "2 1 1\n1\n1\n", "2 1 1\n1\n0\n", "2 1 1\n2\n0\n", "2 2 1\n2 1\n1\n", "3 2 2\n2 4\n3 2\n", "3 2 2\n2 4\n3 3\n", "5 3 5\n2 3 4\n1 1 3 4 5\n"], "outputs": ["1\n1 ", "1\n1 ", "1\n1 ", "1\n1 ", "1\n1 ", "1\n2 ", "2\n1 2 ", "3\n1 2 3 "]}
| 591
| 171
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given an integer array nums of size n, return the minimum number of moves required to make all array elements equal.
In one move, you can increment n - 1 elements of the array by 1.
Please complete the following python code precisely:
```python
class Solution:
def minMoves(self, nums: List[int]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(nums = [1,2,3]) == 3\n assert candidate(nums = [1,1,1]) == 0\n\n\ncheck(Solution().minMoves)"}
| 90
| 51
|
coding
|
Solve the programming task below in a Python markdown code block.
Paprika loves permutations. She has an array $a_1, a_2, \dots, a_n$. She wants to make the array a permutation of integers $1$ to $n$.
In order to achieve this goal, she can perform operations on the array. In each operation she can choose two integers $i$ ($1 \le i \le n$) and $x$ ($x > 0$), then perform $a_i := a_i mod x$ (that is, replace $a_i$ by the remainder of $a_i$ divided by $x$). In different operations, the chosen $i$ and $x$ can be different.
Determine the minimum number of operations needed to make the array a permutation of integers $1$ to $n$. If it is impossible, output $-1$.
A permutation is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
-----Input-----
Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Description of the test cases follows.
The first line of each test case contains an integer $n$ ($1 \le n \le 10^5$).
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$. ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum number of operations needed to make the array a permutation of integers $1$ to $n$, or $-1$ if it is impossible.
-----Examples-----
Input
4
2
1 7
3
1 5 4
4
12345678 87654321 20211218 23571113
9
1 2 3 4 18 19 5 6 7
Output
1
-1
4
2
-----Note-----
For the first test, the only possible sequence of operations which minimizes the number of operations is:
Choose $i=2$, $x=5$. Perform $a_2 := a_2 mod 5 = 2$.
For the second test, it is impossible to obtain a permutation of integers from $1$ to $n$.
|
{"inputs": ["8\n2\n4 3\n3\n3 2 3\n1\n1\n1\n2\n1\n6\n1\n4\n1\n751699\n5\n1 8 9 9 11\n", "8\n2\n1 3\n3\n3 2 3\n1\n1\n1\n2\n1\n6\n1\n1\n1\n751699\n5\n1 8 9 9 11\n", "8\n2\n2 3\n3\n3 4 5\n1\n1\n1\n2\n1\n3\n1\n4\n1\n606626\n5\n7 5 5 9 14\n", "8\n2\n2 3\n3\n3 2 3\n1\n1\n1\n2\n1\n6\n1\n4\n1\n751699\n5\n1 8 9 9 11\n", "8\n2\n2 3\n3\n3 2 3\n1\n1\n1\n2\n1\n6\n1\n5\n1\n751699\n5\n1 8 9 9 11\n", "8\n2\n2 3\n3\n3 4 3\n1\n1\n1\n2\n1\n6\n1\n8\n1\n6800033\n5\n1 1 9 9 14\n", "8\n2\n1 3\n3\n3 1 9\n1\n1\n1\n2\n1\n3\n1\n4\n1\n12071207\n5\n1 3 9 9 2\n", "8\n2\n2 3\n3\n3 4 4\n1\n1\n1\n2\n1\n3\n1\n4\n1\n4555863\n5\n7 5 5 9 14\n"], "outputs": ["-1\n1\n0\n-1\n1\n1\n1\n4\n", "-1\n1\n0\n-1\n1\n0\n1\n4\n", "1\n2\n0\n-1\n1\n1\n1\n4\n", "1\n1\n0\n-1\n1\n1\n1\n4\n", "1\n1\n0\n-1\n1\n1\n1\n4\n", "1\n-1\n0\n-1\n1\n1\n1\n-1\n", "-1\n1\n0\n-1\n1\n1\n1\n-1\n", "1\n-1\n0\n-1\n1\n1\n1\n4\n"]}
| 632
| 614
|
coding
|
Solve the programming task below in a Python markdown code block.
Roughly speaking, this problem asks you to create the checker of [*the problem used in December CookOff*].
You are given a tree with N vertices and a string C of length N, consisting of the characters 'R', 'G' and 'B'.
For each integer i\ (1≤ i≤ N), C_{i} is the color of vertex i — 'R' is for Red, 'G' is for Green and 'B' is for Blue.
The input is called valid if for all pairs of Blue vertices (u,\ v),\ u\neq v, there is at least one Red vertex and one Green vertex on the [simple path] between u and v.
Your task is to validate the input for given T test cases. For each test case, print Yes if the input is valid, otherwise No.
------ Input Format ------
- The first line of input contains one positive integer T, the number of test cases. The description of T test cases follows.
- The first line of each test case contains one positive integer N, the number of vertices of the tree.
- The second line of each test case contains a string C, denoting the colors of the vertices.
- Each of the following N - 1 lines contains two space-separated integers u and v, denoting an edge between vertices u and v in the tree.
------ Output Format ------
For each test case, output a single line containing Yes if the input is valid, and No otherwise.
You may print each character of the string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical).
------ Constraints ------
$1 ≤ T ≤ 10^{4} $
$1 ≤ N ≤ 2\cdot 10^{5}$
- The sum of $N$ for each test case is at most $2\cdot 10^{5}$
$|C|=N$
$C$ consists of three types of characters, 'R', 'G' and 'B'
$1 ≤ u, v ≤ N, u \neq v$
- The input graph forms a tree
----- Sample Input 1 ------
5
4
BRGB
3 2
1 2
4 3
3
RGB
1 2
2 3
1
B
2
BB
1 2
4
BRGB
3 2
1 2
4 2
----- Sample Output 1 ------
Yes
Yes
Yes
No
No
----- explanation 1 ------
- Test case 1:
- The only pair is $(1, 4)$.
- Test case 3:
- There are no pairs.
- Test case 5:
- The only pair is $(1, 4)$. There are no Green vertices on the path between $1$ and $4$.
|
{"inputs": ["5\n4\nBRGB\n3 2\n1 2\n4 3\n3\nRGB\n1 2\n2 3\n1\nB\n2\nBB\n1 2\n4\nBRGB\n3 2\n1 2\n4 2"], "outputs": ["Yes\nYes\nYes\nNo\nNo"]}
| 617
| 78
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You have intercepted a secret message encoded as a string of numbers. The message is decoded via the following mapping:
"1" -> 'A'
"2" -> 'B'
...
"25" -> 'Y'
"26" -> 'Z'
However, while decoding the message, you realize that there are many different ways you can decode the message because some codes are contained in other codes ("2" and "5" vs "25").
For example, "11106" can be decoded into:
"AAJF" with the grouping (1, 1, 10, 6)
"KJF" with the grouping (11, 10, 6)
The grouping (1, 11, 06) is invalid because "06" is not a valid code (only "6" is valid).
Note: there may be strings that are impossible to decode.
Given a string s containing only digits, return the number of ways to decode it. If the entire string cannot be decoded in any valid way, return 0.
The test cases are generated so that the answer fits in a 32-bit integer.
Please complete the following python code precisely:
```python
class Solution:
def numDecodings(self, s: str) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(s = \"12\") == 2\n assert candidate(s = \"226\") == 3\n assert candidate(s = \"06\") == 0\n\n\ncheck(Solution().numDecodings)"}
| 292
| 60
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given an integer array arr and an integer k, modify the array by repeating it k times.
For example, if arr = [1, 2] and k = 3 then the modified array will be [1, 2, 1, 2, 1, 2].
Return the maximum sub-array sum in the modified array. Note that the length of the sub-array can be 0 and its sum in that case is 0.
As the answer can be very large, return the answer modulo 109 + 7.
Please complete the following python code precisely:
```python
class Solution:
def kConcatenationMaxSum(self, arr: List[int], k: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(arr = [1,2], k = 3) == 9\n assert candidate(arr = [1,-2,1], k = 5) == 2\n assert candidate(arr = [-1,-2], k = 7) == 0\n\n\ncheck(Solution().kConcatenationMaxSum)"}
| 169
| 82
|
coding
|
Solve the programming task below in a Python markdown code block.
We consider a positive integer perfect, if and only if the sum of its digits is exactly $10$. Given a positive integer $k$, your task is to find the $k$-th smallest perfect positive integer.
-----Input-----
A single line with a positive integer $k$ ($1 \leq k \leq 10\,000$).
-----Output-----
A single number, denoting the $k$-th smallest perfect integer.
-----Examples-----
Input
1
Output
19
Input
2
Output
28
-----Note-----
The first perfect integer is $19$ and the second one is $28$.
|
{"inputs": ["1\n", "2\n", "3\n", "4\n", "5\n", "7\n", "9\n", "7\n"], "outputs": ["19\n", "28\n", "37\n", "46\n", "55\n", "73\n", "91\n", "73"]}
| 151
| 77
|
coding
|
Solve the programming task below in a Python markdown code block.
Chef has N subordinates. In order to complete a very important order he will choose exactly K of them. He can't choose less than K since it will be not enough to complete the order in time. On the other hand if he chooses more than K subordinates he can't control them during the operation. Help him to find the number of ways he can choose the team to complete this very important order.
------ Input ------
The first line contains a single positive integer T ≤ 100, the number of test cases. T test cases follow. The only line of each test case contains two integers N and K, where 0 ≤ N, K < 2^{64}. It is guaranteed that the answer will be less than 2^{64}.
------ Output ------
For each test case, output a single line containing the number of ways to choose the required team.
----- Sample Input 1 ------
3
2 1
3 3
10 5
----- Sample Output 1 ------
2
1
252
|
{"inputs": ["3\n5 2\n5 4\n9 3", "3\n5 2\n2 5\n9 3", "3\n5 1\n2 5\n9 3", "3\n9 1\n2 5\n9 3", "3\n9 1\n2 5\n8 3", "3\n9 1\n2 5\n7 3", "3\n9 0\n2 5\n7 3", "3\n9 0\n2 3\n5 4"], "outputs": ["10\n5\n84\n", "10\n0\n84\n", "5\n0\n84\n", "9\n0\n84\n", "9\n0\n56\n", "9\n0\n35\n", "1\n0\n35\n", "1\n0\n5\n"]}
| 231
| 199
|
coding
|
Solve the programming task below in a Python markdown code block.
Difference of Big Integers
Given two integers $A$ and $B$, compute the difference, $A - B$.
Input
Two integers $A$ and $B$ separated by a space character are given in a line.
Output
Print the difference in a line.
Constraints
* $-1 \times 10^{100000} \leq A, B \leq 10^{100000}$
Sample Input 1
5 8
Sample Output 1
-3
Sample Input 2
100 25
Sample Output 2
75
Sample Input 3
-1 -1
Sample Output 3
0
Sample Input 4
12 -3
Sample Output 4
15
Example
Input
5 8
Output
-3
|
{"inputs": ["5 4", "5 5", "5 1", "4 1", "2 0", "8 0", "8 1", "0 1"], "outputs": ["1\n", "0\n", "4\n", "3\n", "2\n", "8\n", "7\n", "-1\n"]}
| 194
| 78
|
coding
|
Solve the programming task below in a Python markdown code block.
Anmol gained a lot of weight last semester. So this semester, he decided to run everyday. There is a very long straight road starting at his hostel. There are N poles on the road - P1, P2, P3,..., PN on the road. All the poles lie on the same side of his hostel. The distance between Pi and his hostel is Di.
For 1 ≤ i, j ≤ N, i < j implies Di < Dj
Everyday, Anmol chooses a pole Pi to start running from. He keeps on running until he reaches Pi+K. Whenever he reaches a pole (other than the starting pole), he records the distance traveled since the last pole.
You are given the distances recorded by him today. Your task is to find the number of distinct values of i such that i + K ≤ N and if he starts at Pi and end at Pi+K, he would end up having exactly the same record of distances (in the same order).
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains two space separated integers N and K.
- The next line contains N space separated integers D1, D2,..., DN.
- The next line contains K space separated integers representing the distances recorded by Anmol in the same order.
.
-----Output-----
- For each test case, output a single line containing the answer for that test case.
-----Constraints-----
- 1 ≤ T ≤ 10
- 2 ≤ N ≤ 5 x 104
- 1 ≤ K < N
- 1 ≤ Di ≤ 106
-----Subtasks-----
-----Subtask #1 (20 points)-----
- 1 ≤ N ≤ 1000
-----Subtask #2 (80 points)-----
- Original constraints
-----Example-----
Input:
3
5 1
1 5 10 12 14
5
5 2
5 8 13 16 21
3 5
5 3
2 6 8 11 16
2 3 5
Output:
1
2
1
-----Explanation-----
Example case 1. If he runs from P2 to P3, he will record (5)
Example case 2. He can start at P1 or P3
Example case 3. He can start at P2
|
{"inputs": ["3\n5 1\n1 5 10 12 14\n5\n5 2\n5 8 13 16 21\n3 5\n5 3\n2 6 8 11 16\n2 3 5"], "outputs": ["1\n2\n1"]}
| 529
| 78
|
coding
|
Solve the programming task below in a Python markdown code block.
Some days ago, I learned the concept of LCM (least common multiple). I've played with it for several times and I want to make a big number with it.
But I also don't want to use many numbers, so I'll choose three positive integers (they don't have to be distinct) which are not greater than n. Can you help me to find the maximum possible least common multiple of these three integers?
Input
The first line contains an integer n (1 ≤ n ≤ 106) — the n mentioned in the statement.
Output
Print a single integer — the maximum possible LCM of three not necessarily distinct positive integers that are not greater than n.
Examples
Input
9
Output
504
Input
7
Output
210
Note
The least common multiple of some positive integers is the least positive integer which is multiple for each of them.
The result may become very large, 32-bit integer won't be enough. So using 64-bit integers is recommended.
For the last example, we can chose numbers 7, 6, 5 and the LCM of them is 7·6·5 = 210. It is the maximum value we can get.
|
{"inputs": ["1\n", "8\n", "6\n", "3\n", "4\n", "5\n", "2\n", "9\n"], "outputs": ["1\n", "280\n", "60\n", "6\n", "12\n", "60\n", "2\n", "504\n"]}
| 269
| 77
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given an array of equal-length strings words. Assume that the length of each string is n.
Each string words[i] can be converted into a difference integer array difference[i] of length n - 1 where difference[i][j] = words[i][j+1] - words[i][j] where 0 <= j <= n - 2. Note that the difference between two letters is the difference between their positions in the alphabet i.e. the position of 'a' is 0, 'b' is 1, and 'z' is 25.
For example, for the string "acb", the difference integer array is [2 - 0, 1 - 2] = [2, -1].
All the strings in words have the same difference integer array, except one. You should find that string.
Return the string in words that has different difference integer array.
Please complete the following python code precisely:
```python
class Solution:
def oddString(self, words: List[str]) -> str:
```
|
{"functional": "def check(candidate):\n assert candidate(words = [\"adc\",\"wzy\",\"abc\"]) == \"abc\"\n assert candidate(words = [\"aaa\",\"bob\",\"ccc\",\"ddd\"]) == \"bob\"\n\n\ncheck(Solution().oddString)"}
| 234
| 60
|
coding
|
Solve the programming task below in a Python markdown code block.
There is a string $s$ of length $3$, consisting of uppercase and lowercase English letters. Check if it is equal to "YES" (without quotes), where each letter can be in any case. For example, "yES", "Yes", "yes" are all allowable.
-----Input-----
The first line of the input contains an integer $t$ ($1 \leq t \leq 10^3$) — the number of testcases.
The description of each test consists of one line containing one string $s$ consisting of three characters. Each character of $s$ is either an uppercase or lowercase English letter.
-----Output-----
For each test case, output "YES" (without quotes) if $s$ satisfies the condition, and "NO" (without quotes) otherwise.
You can output "YES" and "NO" in any case (for example, strings "yES", "yes" and "Yes" will be recognized as a positive response).
-----Examples-----
Input
10
YES
yES
yes
Yes
YeS
Noo
orZ
yEz
Yas
XES
Output
YES
YES
YES
YES
YES
NO
NO
NO
NO
NO
-----Note-----
The first five test cases contain the strings "YES", "yES", "yes", "Yes", "YeS". All of these are equal to "YES", where each character is either uppercase or lowercase.
|
{"inputs": ["1\nvop\n", "1\nhac\n", "1\nNNy\n", "1\nHAC\n", "1\nyeQ\n", "1\nHAm\n", "1\nxxx\n", "1\nxxy\n"], "outputs": ["NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n"]}
| 320
| 93
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given a positive integer array skill of even length n where skill[i] denotes the skill of the ith player. Divide the players into n / 2 teams of size 2 such that the total skill of each team is equal.
The chemistry of a team is equal to the product of the skills of the players on that team.
Return the sum of the chemistry of all the teams, or return -1 if there is no way to divide the players into teams such that the total skill of each team is equal.
Please complete the following python code precisely:
```python
class Solution:
def dividePlayers(self, skill: List[int]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(skill = [3,2,5,1,3,4]) == 22\n assert candidate(skill = [3,4]) == 12\n assert candidate(skill = [1,1,2,3]) == -1\n\n\ncheck(Solution().dividePlayers)"}
| 153
| 75
|
coding
|
Solve the programming task below in a Python markdown code block.
The palindrome is a string that can be read the same way from left to right and from right to left. For example, strings "hanah", "2332" are palindromes, however the string "rahul" is not a palindrome.
We define "Easy String" if it has an even length and the first half of this string is equal to the second half of this string. Like "dada" is an easy string because the first half "da" is equal to the second half "da", however the string "adda" is not an easy string because the first half "ad" is not equal to the second half "ad".
If you are given a palindrome string of a particular length N, what is the maximal possible number of characters/symbols in an "easy string" that can be obtained by removing and changing the order of characters/symbols in the palindrome?
Input
The first line of the sample input contains an integer T - the number of test cases.
Then, T lines follow.
Each line consists of a single integer N - the length of a palindrome.
Output
For each test case output a single integer - answer to the problem.
Constraints
• 1 ≤ T ≤ 10000
• 1 ≤ N ≤ 1000000000
SAMPLE INPUT
2
2
4
SAMPLE OUTPUT
2
4
|
{"inputs": ["2\n2\n4\n"], "outputs": ["2\n4\n"]}
| 308
| 20
|
coding
|
Solve the programming task below in a Python markdown code block.
Rng is baking cookies.
Initially, he can bake one cookie per second.
He can also eat the cookies baked by himself. When there are x cookies not yet eaten, he can choose to eat all those cookies. After he finishes eating those cookies, the number of cookies he can bake per second becomes x. Note that a cookie always needs to be baked for 1 second, that is, he cannot bake a cookie in 1/x seconds when x > 1. When he choose to eat the cookies, he must eat all of them; he cannot choose to eat only part of them. It takes him A seconds to eat the cookies regardless of how many, during which no cookies can be baked.
He wants to give N cookies to Grandma. Find the shortest time needed to produce at least N cookies not yet eaten.
Constraints
* 1≦N≦10^{12}
* 0≦A≦10^{12}
* A is an integer.
Input
The input is given from Standard Input in the following format:
N A
Output
Print the shortest time needed to produce at least N cookies not yet eaten.
Examples
Input
8 1
Output
7
Input
1000000000000 1000000000000
Output
1000000000000
|
{"inputs": ["9 1", "7 0", "3 0", "4 1", "1 1", "8 1", "13 1", "13 0"], "outputs": ["7\n", "6\n", "3\n", "4\n", "1\n", "7", "9\n", "8\n"]}
| 303
| 79
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given an array A containing N positive integers. Find the number of pairs (i,j) such that 1 ≤ i < j ≤ N and:
A_{i} + A_{j} ≥ A_{i} \cdot A_{j}
------ Input Format ------
- The first line contains an integer T denoting the number of test cases. The T test cases then follow.
- The first line of each test case contains an integer N.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, output on a new line the required answer.
------ Constraints ------
$1 ≤T ≤1000$
$2 ≤N ≤2 \cdot 10^{5}$
$1 ≤A_{i} ≤10^{6}$
- Sum of $N$ over all cases won't exceed $2 \cdot 10^{5}$
----- Sample Input 1 ------
2
3
1 3 4
5
2 3 4 5 6
----- Sample Output 1 ------
2
0
----- explanation 1 ------
Test case $1$: The two pairs are $(1, 2)$ and $(1, 3)$, because:
- $A_{1} + A_{2} = 4 > 3 = A_{1} \cdot A_{2}$
- $A_{1} + A_{3} = 5 > 4 = A_{1} \cdot A_{3}$
Test case $2$: No pairs satisfy the condition.
|
{"inputs": ["2\n3\n1 3 4\n5\n2 3 4 5 6"], "outputs": ["2\n0"]}
| 361
| 34
|
coding
|
Solve the programming task below in a Python markdown code block.
A variation of determining leap years, assuming only integers are used and years can be negative and positive.
Write a function which will return the days in the year and the year entered in a string. For example 2000, entered as an integer, will return as a string 2000 has 366 days
There are a few assumptions we will accept the year 0, even though there is no year 0 in the Gregorian Calendar.
Also the basic rule for validating a leap year are as follows
Most years that can be divided evenly by 4 are leap years.
Exception: Century years are NOT leap years UNLESS they can be evenly divided by 400.
So the years 0, -64 and 2016 will return 366 days.
Whilst 1974, -10 and 666 will return 365 days.
Also feel free to reuse/extend the following starter code:
```python
def year_days(year):
```
|
{"functional": "_inputs = [[0], [-64], [2016], [1974], [-10], [666], [1857], [2000], [-300], [-1]]\n_outputs = [['0 has 366 days'], ['-64 has 366 days'], ['2016 has 366 days'], ['1974 has 365 days'], ['-10 has 365 days'], ['666 has 365 days'], ['1857 has 365 days'], ['2000 has 366 days'], ['-300 has 365 days'], ['-1 has 365 days']]\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(year_days(*i), o[0])"}
| 226
| 304
|
coding
|
Solve the programming task below in a Python markdown code block.
Diana is planning to make a very long journey. Her journey consists of $N$ bus routes, numbered from $1 to N$ in the order she must take them. The buses themselves are very fast but do not run often. The $i-th$ bus route only runs every $Xi$ days.
More specifically, she can only take the $i-th$ bus on day $Xi, 2Xi, 3Xi$, and so on. Since the buses are very fast, she can take multiple buses on the same day.
Diana must finish her journey by day D, but she would like to start the journey as late as possible. What is the latest day she could take the first bus, and still finish her journey by day $D$?
It is guaranteed that it is possible for Diana to finish her journey by day $D$.
-----Input:-----
The first line of the input gives the number of test cases, $T$. $T$ test cases follow. Each test case begins with a line containing the two integers N and D. Then, another line follows containing $N$ integers, the $i-th$ one is $Xi$.
-----Output:-----
For each test case, output one line containing an integer $Y$, where $Y$ is the latest day she could take the first bus, and still finish her journey by day $D$.
-----Constraints-----
- $1 \leq T \leq 100$
- $1 \leq Xi \leq D.$
- $1 \leq N \leq 1000.$
It is guaranteed that it is possible for Diana to finish her journey by day $D$.
-----Sample Input:-----
3
3 10
3 7 2
4 100
11 10 5 50
1 1
1
-----Sample Output:-----
6
99
1
-----EXPLANATION:-----
In Sample $Case 1$, there are $N = 3$ bus routes and Bucket must arrive by day $D = 10$. She could:
- Take the 1st bus on day 6 $(X1 = 3)$,
- Take the 2nd bus on day 7 $(X2 = 7)$ and
- Take the 3rd bus on day 8 $(X3 = 2)$.
In Sample $Case 2$, there are $N = 4$ bus routes and Bucket must arrive by day $D = 100$. She could:
- Take the 1st bus on day 99 $(X1 = 11)$,
- Take the 2nd bus on day 100$ (X2 = 10)$,
- Take the 3rd bus on day 100 $(X3 = 5)$ and
- Take the 4th bus on day 100 $(X4 = 50)$,
In Sample Case 3, there is $N = 1$ bus route, and Bucket must arrive by day $D = 1$. She could:
- Take the 1st bus on day 1 $(X1 = 1)$.
|
{"inputs": ["3\n3 10\n3 7 2\n4 100\n11 10 5 50\n1 1\n1"], "outputs": ["6\n99\n1"]}
| 688
| 51
|
coding
|
Solve the programming task below in a Python markdown code block.
Meliodas and Ban are fighting over chocolates. Meliodas has $X$ chocolates, while Ban has $Y$. Whoever has lesser number of chocolates eats as many chocolates as he has from the other's collection. This eatfest war continues till either they have the same number of chocolates, or atleast one of them is left with no chocolates.
Can you help Elizabeth predict the total no of chocolates they'll be left with at the end of their war?
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- Each testcase contains of a single line of input, which contains two integers $X, Y$, the no of chocolates Meliodas and Ban have, respectively.
-----Output:-----
For each testcase, output in a single line the no of chocolates that remain after Ban and Meliodas stop fighting.
-----Constraints-----
- $1 \leq T \leq 100000$
- $0 \leq X,Y \leq 10^9$
-----Sample Input:-----
3
5 3
10 10
4 8
-----Sample Output:-----
2
20
8
-----EXPLANATION:-----
Denoting Meliodas as $M$, Ban as $B$.
Testcase 1:
$M$=5, $B$=3
Ban eates 3 chocolates of Meliodas.
$M$=2, $B$=3
Meliodas eats 2 chocolates of Ban.
$M$=2, $B$=1
Ban eates 1 chocolate of Meliodas.
$M$=1, $B$=1
Since they have the same no of candies, they stop quarreling.
Total candies left: 2
Testcase 2:
$M$=10, $B$=10
Since both of them had the same candies to begin with, there was no point in fighting.
Total candies left: 20
Testcase 3:
$M$=4, $B$=8
Meliodas eats 4 chocolates of Ban.
$M$=4, $B$=4
Since they have the same no of candies, they stop quarreling.
Total candies left: 8
|
{"inputs": ["3\n5 3\n10 10\n4 8"], "outputs": ["2\n20\n8"]}
| 496
| 31
|
coding
|
Solve the programming task below in a Python markdown code block.
You wrote down all integers from $0$ to $10^n - 1$, padding them with leading zeroes so their lengths are exactly $n$. For example, if $n = 3$ then you wrote out 000, 001, ..., 998, 999.
A block in an integer $x$ is a consecutive segment of equal digits that cannot be extended to the left or to the right.
For example, in the integer $00027734000$ there are three blocks of length $1$, one block of length $2$ and two blocks of length $3$.
For all integers $i$ from $1$ to $n$ count the number of blocks of length $i$ among the written down integers.
Since these integers may be too large, print them modulo $998244353$.
-----Input-----
The only line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$).
-----Output-----
In the only line print $n$ integers. The $i$-th integer is equal to the number of blocks of length $i$.
Since these integers may be too large, print them modulo $998244353$.
-----Examples-----
Input
1
Output
10
Input
2
Output
180 10
|
{"inputs": ["1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n"], "outputs": ["10\n", "180 10\n", "2610 180 10\n", "34200 2610 180 10\n", "423000 34200 2610 180 10\n", "5040000 423000 34200 2610 180 10\n", "58500000 5040000 423000 34200 2610 180 10\n", "666000000 58500000 5040000 423000 34200 2610 180 10\n"]}
| 313
| 246
|
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.
Chef Ada is training to defend her title of World Chess Champion.
To train her calculation skills, Ada placed a king on a chessboard. Remember that a chessboard has $8$ rows and $8$ columns (for the purposes of this problem, both the rows and the columns are numbered $1$ through $8$); let's denote the square in row $r$ and column $c$ by $(r, c)$. A king on a square $(r, c)$ can move to another square $(r', c')$ if and only if $(r'-r)^2+(c'-c)^2 ≤ 2$.
Ada placed her king on the square $(R, C)$. Now, she is counting the number of squares that can be visited (reached) by the king in at most $K$ moves. Help Ada verify her answers.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains three space-separated integers $R$, $C$ and $K$.
------ Output ------
For each test case, print a single line containing one integer — the number of squares the king can visit.
------ Constraints ------
$1 ≤ T ≤ 512$
$1 ≤ R, C, K ≤ 8$
----- Sample Input 1 ------
1
1 3 1
----- Sample Output 1 ------
6
----- explanation 1 ------
Example case 1: The king can stay on its original square or move to one of the squares circled in the following figure.
|
{"inputs": ["1\n1 3 1"], "outputs": ["6"]}
| 394
| 18
|
coding
|
Solve the programming task below in a Python markdown code block.
One company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received n resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the number of variants of group composition to evaluate.
-----Input-----
The only line of the input contains one integer n (7 ≤ n ≤ 777) — the number of potential employees that sent resumes.
-----Output-----
Output one integer — the number of different variants of group composition.
-----Examples-----
Input
7
Output
29
|
{"inputs": ["7\n", "8\n", "9\n", "8\n", "9\n", "7\n", "10\n", "10\n"], "outputs": ["29", "92", "246", "92\n", "246\n", "29\n", "582", "582\n"]}
| 144
| 80
|
coding
|
Solve the programming task below in a Python markdown code block.
Allen is hosting a formal dinner party. $2n$ people come to the event in $n$ pairs (couples). After a night of fun, Allen wants to line everyone up for a final picture. The $2n$ people line up, but Allen doesn't like the ordering. Allen prefers if each pair occupies adjacent positions in the line, as this makes the picture more aesthetic.
Help Allen find the minimum number of swaps of adjacent positions he must perform to make it so that each couple occupies adjacent positions in the line.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 100$), the number of pairs of people.
The second line contains $2n$ integers $a_1, a_2, \dots, a_{2n}$. For each $i$ with $1 \le i \le n$, $i$ appears exactly twice. If $a_j = a_k = i$, that means that the $j$-th and $k$-th people in the line form a couple.
-----Output-----
Output a single integer, representing the minimum number of adjacent swaps needed to line the people up so that each pair occupies adjacent positions.
-----Examples-----
Input
4
1 1 2 3 3 2 4 4
Output
2
Input
3
1 1 2 2 3 3
Output
0
Input
3
3 1 2 3 1 2
Output
3
-----Note-----
In the first sample case, we can transform $1 1 2 3 3 2 4 4 \rightarrow 1 1 2 3 2 3 4 4 \rightarrow 1 1 2 2 3 3 4 4$ in two steps. Note that the sequence $1 1 2 3 3 2 4 4 \rightarrow 1 1 3 2 3 2 4 4 \rightarrow 1 1 3 3 2 2 4 4$ also works in the same number of steps.
The second sample case already satisfies the constraints; therefore we need $0$ swaps.
|
{"inputs": ["1\n1 1\n", "1\n1 1\n", "1\n0 0\n", "2\n1 2 1 2\n", "2\n2 1 1 2\n", "2\n2 1 1 2\n", "2\n1 2 1 2\n", "2\n1 1 2 2\n"], "outputs": ["0\n", "0\n", "0\n", "1\n", "2\n", "2\n", "1\n", "0\n"]}
| 484
| 122
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given an array points, an integer angle, and your location, where location = [posx, posy] and points[i] = [xi, yi] both denote integral coordinates on the X-Y plane.
Initially, you are facing directly east from your position. You cannot move from your position, but you can rotate. In other words, posx and posy cannot be changed. Your field of view in degrees is represented by angle, determining how wide you can see from any given view direction. Let d be the amount in degrees that you rotate counterclockwise. Then, your field of view is the inclusive range of angles [d - angle/2, d + angle/2].
Your browser does not support the video tag or this video format.
You can see some set of points if, for each point, the angle formed by the point, your position, and the immediate east direction from your position is in your field of view.
There can be multiple points at one coordinate. There may be points at your location, and you can always see these points regardless of your rotation. Points do not obstruct your vision to other points.
Return the maximum number of points you can see.
Please complete the following python code precisely:
```python
class Solution:
def visiblePoints(self, points: List[List[int]], angle: int, location: List[int]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(points = [[2,1],[2,2],[3,3]], angle = 90, location = [1,1]) == 3\n assert candidate(points = [[2,1],[2,2],[3,4],[1,1]], angle = 90, location = [1,1]) == 4\n assert candidate(points = [[1,0],[2,1]], angle = 13, location = [1,1]) == 1\n\n\ncheck(Solution().visiblePoints)"}
| 302
| 124
|
coding
|
Solve the programming task below in a Python markdown code block.
There are an integer sequence A_1,...,A_N consisting of N terms, and N buttons. When the i-th (1 ≦ i ≦ N) button is pressed, the values of the i terms from the first through the i-th are all incremented by 1.
There is also another integer sequence B_1,...,B_N. Takahashi will push the buttons some number of times so that for every i, A_i will be a multiple of B_i.
Find the minimum number of times Takahashi will press the buttons.
Constraints
* All input values are integers.
* 1 ≦ N ≦ 10^5
* 0 ≦ A_i ≦ 10^9(1 ≦ i ≦ N)
* 1 ≦ B_i ≦ 10^9(1 ≦ i ≦ N)
Input
The input is given from Standard Input in the following format:
N
A_1 B_1
:
A_N B_N
Output
Print an integer representing the minimum number of times Takahashi will press the buttons.
Examples
Input
3
3 5
2 7
9 4
Output
7
Input
7
3 1
4 1
5 9
2 6
5 3
5 8
9 7
Output
22
|
{"inputs": ["3\n3 5\n2 2\n9 4", "3\n3 4\n2 2\n9 4", "3\n2 4\n2 1\n9 4", "3\n2 1\n0 1\n8 4", "3\n3 4\n2 3\n9 4", "3\n3 4\n2 1\n9 4", "3\n3 5\n2 7\n9 2", "3\n3 4\n3 3\n9 4"], "outputs": ["7\n", "5\n", "6\n", "0\n", "5\n", "5\n", "7\n", "5\n"]}
| 301
| 158
|
coding
|
Solve the programming task below in a Python markdown code block.
Snuke is having a barbeque party.
At the party, he will make N servings of Skewer Meal.
Example of a serving of Skewer Meal
He has a stock of 2N skewers, all of which will be used in Skewer Meal. The length of the i-th skewer is L_i.
Also, he has an infinite supply of ingredients.
To make a serving of Skewer Meal, he picks 2 skewers and threads ingredients onto those skewers.
Let the length of the shorter skewer be x, then the serving can hold the maximum of x ingredients.
What is the maximum total number of ingredients that his N servings of Skewer Meal can hold, if he uses the skewers optimally?
-----Constraints-----
- 1≦N≦100
- 1≦L_i≦100
- For each i, L_i is an integer.
-----Input-----
The input is given from Standard Input in the following format:
N
L_1 L_2 ... L_{2N}
-----Output-----
Print the maximum total number of ingredients that Snuke's N servings of Skewer Meal can hold.
-----Sample Input-----
2
1 3 1 2
-----Sample Output-----
3
If he makes a serving using the first and third skewers, and another using the second and fourth skewers, each serving will hold 1 and 2 ingredients, for the total of 3.
|
{"inputs": ["1\n1 1\n", "2\n1 3 1 1", "2\n1 2 1 2", "2\n1 2 0 1", "2\n2 0 0 0", "2\n1 3 1 2", "2\n1 3 1 2\n", "2\n2 -1 0 0"], "outputs": ["1\n", "2\n", "3\n", "1\n", "0\n", "3", "3\n", "-1\n"]}
| 319
| 123
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a square grid with $n$ rows and $n$ columns. Each cell contains either $0$ or $1$.
In an operation, you can select a cell of the grid and flip it (from $0 \to 1$ or $1 \to 0$). Find the minimum number of operations you need to obtain a square that remains the same when rotated $0^{\circ}$, $90^{\circ}$, $180^{\circ}$ and $270^{\circ}$.
The picture below shows an example of all rotations of a grid.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 100$) — the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 100$) — the size of the grid.
Then $n$ lines follow, each with $n$ characters $a_{i,j}$ ($0 \leq a_{i,j} \leq 1$) — the number written in each cell.
-----Output-----
For each test case output a single integer — the minimum number of operations needed to make the square look the same rotated $0^{\circ}$, $90^{\circ}$, $180^{\circ}$ and $270^{\circ}$.
-----Examples-----
Input
5
3
010
110
010
1
0
5
11100
11011
01011
10011
11000
5
01000
10101
01010
00010
01001
5
11001
00000
11111
10110
01111
Output
1
0
9
7
6
-----Note-----
In the first test case, we can perform one operations to make the grid $\begin{matrix}0 & 1 & 0\\ 1 & 1 & {1}\\ 0 & 1 & 0\end{matrix}$. Now, all rotations of the square are the same.
In the second test case, all rotations of the square are already the same, so we don't need any flips.
|
{"inputs": ["5\n3\n010\n110\n010\n1\n0\n5\n11100\n11011\n01011\n10011\n11000\n5\n01000\n10101\n01010\n00010\n01001\n5\n11001\n00000\n11111\n10110\n01111\n"], "outputs": ["1\n0\n9\n7\n6\n"]}
| 536
| 136
|
coding
|
Solve the programming task below in a Python markdown code block.
During a recent research Berland scientists found out that there were n cities in Ancient Berland, joined by two-way paths. Any two cities are joined by no more than one path. No path joins a city with itself. According to a well-known tradition, the road network was built so that it would be impossible to choose three cities from each of which one can get to any other one directly. That is, there was no cycle exactly as long as 3. Unfortunately, the road map has not been preserved till nowadays. Now the scientists are interested how much developed a country Ancient Berland was. Help them - find, what maximal number of roads could be in the country. You also have to restore any of the possible road maps.
Input
The first line contains an integer n (1 ≤ n ≤ 100) — the number of cities in Berland.
Output
On the first line must be printed number m — the maximal number of roads in Berland. Then print m lines containing two numbers each — the numbers of cities that the given road joins. The cities are numbered with integers from 1 to n. If there are several variants of solving the problem, print any of them.
Examples
Input
3
Output
2
1 2
2 3
Input
4
Output
4
1 2
2 3
3 4
4 1
|
{"inputs": ["3\n", "1\n", "9\n", "6\n", "5\n", "7\n", "2\n", "8\n"], "outputs": ["2\n1 2\n1 3\n", "0\n", "20\n1 5\n1 6\n1 7\n1 8\n1 9\n2 5\n2 6\n2 7\n2 8\n2 9\n3 5\n3 6\n3 7\n3 8\n3 9\n4 5\n4 6\n4 7\n4 8\n4 9\n", "9\n1 4\n1 5\n1 6\n2 4\n2 5\n2 6\n3 4\n3 5\n3 6\n", "6\n1 3\n1 4\n1 5\n2 3\n2 4\n2 5\n", "12\n1 4\n1 5\n1 6\n1 7\n2 4\n2 5\n2 6\n2 7\n3 4\n3 5\n3 6\n3 7\n", "1\n1 2\n", "16\n1 5\n1 6\n1 7\n1 8\n2 5\n2 6\n2 7\n2 8\n3 5\n3 6\n3 7\n3 8\n4 5\n4 6\n4 7\n4 8\n"]}
| 298
| 337
|
coding
|
Solve the programming task below in a Python markdown code block.
This is yet another problem on regular bracket sequences.
A bracket sequence is called regular, if by inserting "+" and "1" into it we get a correct mathematical expression. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. You have a pattern of a bracket sequence that consists of characters "(", ")" and "?". You have to replace each character "?" with a bracket so, that you get a regular bracket sequence.
For each character "?" the cost of its replacement with "(" and ")" is given. Among all the possible variants your should choose the cheapest.
Input
The first line contains a non-empty pattern of even length, consisting of characters "(", ")" and "?". Its length doesn't exceed 5·104. Then there follow m lines, where m is the number of characters "?" in the pattern. Each line contains two integer numbers ai and bi (1 ≤ ai, bi ≤ 106), where ai is the cost of replacing the i-th character "?" with an opening bracket, and bi — with a closing one.
Output
Print the cost of the optimal regular bracket sequence in the first line, and the required sequence in the second.
Print -1, if there is no answer. If the answer is not unique, print any of them.
Examples
Input
(??)
1 2
2 8
Output
4
()()
|
{"inputs": ["((()))\n", "()()()\n", "))))))\n", "((((((\n", "(())()\n", ")))(((\n", ")())))\n", ")())((\n"], "outputs": ["0\n((()))", "0\n()()()", "-1", "-1", "0\n(())()", "-1\n", "-1\n", "-1\n"]}
| 311
| 87
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Special binary strings are binary strings with the following two properties:
The number of 0's is equal to the number of 1's.
Every prefix of the binary string has at least as many 1's as 0's.
You are given a special binary string s.
A move consists of choosing two consecutive, non-empty, special substrings of s, and swapping them. Two strings are consecutive if the last character of the first string is exactly one index before the first character of the second string.
Return the lexicographically largest resulting string possible after applying the mentioned operations on the string.
Please complete the following python code precisely:
```python
class Solution:
def makeLargestSpecial(self, s: str) -> str:
```
|
{"functional": "def check(candidate):\n assert candidate(s = \"11011000\") == \"11100100\"\n assert candidate(s = \"10\") == \"10\"\n\n\ncheck(Solution().makeLargestSpecial)"}
| 169
| 63
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given a 0-indexed string s of even length n. The string consists of exactly n / 2 opening brackets '[' and n / 2 closing brackets ']'.
A string is called balanced if and only if:
It is the empty string, or
It can be written as AB, where both A and B are balanced strings, or
It can be written as [C], where C is a balanced string.
You may swap the brackets at any two indices any number of times.
Return the minimum number of swaps to make s balanced.
Please complete the following python code precisely:
```python
class Solution:
def minSwaps(self, s: str) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(s = \"][][\") == 1\n assert candidate(s = \"]]][[[\") == 2\n assert candidate(s = \"[]\") == 0\n\n\ncheck(Solution().minSwaps)"}
| 160
| 60
|
coding
|
Solve the programming task below in a Python markdown code block.
A spoonerism is a spoken phrase in which the first letters of two of the words are swapped around, often with amusing results.
In its most basic form a spoonerism is a two word phrase in which only the first letters of each word are swapped:
```"not picking" --> "pot nicking"```
Your task is to create a function that takes a string of two words, separated by a space: ```words``` and returns a spoonerism of those words in a string, as in the above example.
NOTE: All input strings will contain only two words. Spoonerisms can be more complex. For example, three-word phrases in which the first letters of the first and last words are swapped: ```"pack of lies" --> "lack of pies"``` or more than one letter from a word is swapped: ```"flat battery --> "bat flattery"```
You are NOT expected to account for these, or any other nuances involved in spoonerisms.
Once you have completed this kata, a slightly more challenging take on the idea can be found here: http://www.codewars.com/kata/56dbed3a13c2f61ae3000bcd
Also feel free to reuse/extend the following starter code:
```python
def spoonerize(words):
```
|
{"functional": "_inputs = [['not picking'], ['wedding bells'], ['jelly beans'], ['pop corn']]\n_outputs = [['pot nicking'], ['bedding wells'], ['belly jeans'], ['cop porn']]\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(spoonerize(*i), o[0])"}
| 290
| 187
|
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.
Two players (let's call them A and B) are playing a game on a row of cells. A cell may contain the character 'A', contain the character 'B' or be empty. Each character may only be moved from a cell $c_{i}$ to a cell $c_{f}$ if cell $c_{f}$ and all cells between $c_{i}$ and $c_{f}$ are currently empty. The leftmost (first) character may only be moved to the right, the second character may only be moved to the left, the next character to the right, and so on. Each character may be moved any number of times, including zero.
The players alternate turns; player A starts. On each turn, the current player must choose a cell containing their own character ('A' for player A, 'B' for player B) and move it to a different cell. This cell may be chosen arbitrarily as long as all rules described above are satisfied. The first player that cannot move a character loses.
Note that each character is always moved in a fixed direction, so the game is finite. Determine the winner of the game if both players play optimally.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains a single string $s$ describing the initial state of the row of cells. Each character of $s$ is either 'A', 'B' or '.', denoting a cell containing 'A', a cell containing 'B' or an empty cell respectively.
------ Output ------
For each test case, print a single line containing the string "A" if player A wins or "B" if player B wins.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ |s| ≤ 10^{5}$
the sum of $|s|$ over all test cases does not exceed $10^{6}$
----- Sample Input 1 ------
3
A.B
A..B
A..B.A..B
----- Sample Output 1 ------
A
A
B
----- explanation 1 ------
Example case 1: Player A can move the first character 'A' one cell to the right. Now, player B cannot move and loses.
Example case 2: Player A can move the first character two cells to the right (to the cell adjacent to 'B'). Again, player B cannot move and loses.
|
{"inputs": ["3\nA.B\nA..B\nA..B.A..B"], "outputs": ["A\nA\nB"]}
| 574
| 30
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problems statements [Mandarin] , [Bengali] , [Hindi] , [Russian] and [Vietnamese] as well.
Chef is participating in a pogo stick race. In this competition, there are $N$ squares (numbered $1$ through $N$) in a row. Chef must choose a starting square, enter this square and start jumping on his pogo stick. In each jump, if Chef is at a square $s$, then he can only jump to the square $s+K$. If square $s+K$ does not exist, Chef jumps out of the row of squares and the race ends for him. It is not allowed to stop jumping any earlier.
Each square has a value; let's denote the value of the $i$-th square by $A_{i}$. Initially, Chef has $0$ points. When he jumps in some square (including the initial square), he is awarded a number of points equal to the value of this square, exactly once. Note that if this value is negative, the number of Chef's points is decreased.
Determine the maximum possible total number of points Chef can get if he selects the initial cell optimally.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $K$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer ― the maximum number of points.
------ Constraints ------
$1 ≤ T ≤ 1,000$
$1 ≤ N ≤ 10^{5}$
$1 ≤ K ≤ 10^{5}$
$|A_{i}| ≤ 10,000$ for each valid $i$
the sum of $N$ over all test cases does not exceed $10^{6}$
------ Subtasks ------
Subtask #1 (30 points):
$N ≤ 1,000$
the sum of $N$ over all test cases does not exceed $10,000$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
2
5 2
3 6 4 7 2
5 3
3 -5 6 3 10
----- Sample Output 1 ------
13
10
|
{"inputs": ["2\n5 2\n3 6 4 7 2\n5 3\n3 -5 6 3 10"], "outputs": ["13\n10"]}
| 562
| 45
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You have k lists of sorted integers in non-decreasing order. Find the smallest range that includes at least one number from each of the k lists.
We define the range [a, b] is smaller than range [c, d] if b - a < d - c or a < c if b - a == d - c.
Please complete the following python code precisely:
```python
class Solution:
def smallestRange(self, nums: List[List[int]]) -> List[int]:
```
|
{"functional": "def check(candidate):\n assert candidate(nums = [[4,10,15,24,26], [0,9,12,20], [5,18,22,30]]) == [20,24]\n assert candidate(nums = [[1,2,3],[1,2,3],[1,2,3]]) == [1,1]\n\n\ncheck(Solution().smallestRange)"}
| 120
| 103
|
coding
|
Solve the programming task below in a Python markdown code block.
Tokitsukaze is one of the characters in the game "Kantai Collection". In this game, every character has a common attribute — health points, shortened to HP.
In general, different values of HP are grouped into $4$ categories: Category $A$ if HP is in the form of $(4 n + 1)$, that is, when divided by $4$, the remainder is $1$; Category $B$ if HP is in the form of $(4 n + 3)$, that is, when divided by $4$, the remainder is $3$; Category $C$ if HP is in the form of $(4 n + 2)$, that is, when divided by $4$, the remainder is $2$; Category $D$ if HP is in the form of $4 n$, that is, when divided by $4$, the remainder is $0$.
The above-mentioned $n$ can be any integer.
These $4$ categories ordered from highest to lowest as $A > B > C > D$, which means category $A$ is the highest and category $D$ is the lowest.
While playing the game, players can increase the HP of the character. Now, Tokitsukaze wants you to increase her HP by at most $2$ (that is, either by $0$, $1$ or $2$). How much should she increase her HP so that it has the highest possible category?
-----Input-----
The only line contains a single integer $x$ ($30 \leq x \leq 100$) — the value Tokitsukaze's HP currently.
-----Output-----
Print an integer $a$ ($0 \leq a \leq 2$) and an uppercase letter $b$ ($b \in \lbrace A, B, C, D \rbrace$), representing that the best way is to increase her HP by $a$, and then the category becomes $b$.
Note that the output characters are case-sensitive.
-----Examples-----
Input
33
Output
0 A
Input
98
Output
1 B
-----Note-----
For the first example, the category of Tokitsukaze's HP is already $A$, so you don't need to enhance her ability.
For the second example: If you don't increase her HP, its value is still $98$, which equals to $(4 \times 24 + 2)$, and its category is $C$. If you increase her HP by $1$, its value becomes $99$, which equals to $(4 \times 24 + 3)$, and its category becomes $B$. If you increase her HP by $2$, its value becomes $100$, which equals to $(4 \times 25)$, and its category becomes $D$.
Therefore, the best way is to increase her HP by $1$ so that the category of her HP becomes $B$.
|
{"inputs": ["6\n", "4\n", "5\n", "8\n", "3\n", "9\n", "0\n", "2\n"], "outputs": ["1 B\n", "1 A\n", "0 A\n", "1 A\n", "2 A\n", "0 A\n", "1 A\n", "1 B\n"]}
| 640
| 78
|
coding
|
Solve the programming task below in a Python markdown code block.
Write a program of the Selection Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode:
SelectionSort(A)
1 for i = 0 to A.length-1
2 mini = i
3 for j = i to A.length-1
4 if A[j] < A[mini]
5 mini = j
6 swap A[i] and A[mini]
Note that, indices for array elements are based on 0-origin.
Your program should also print the number of swap operations defined in line 6 of the pseudocode in the case where i ≠ mini.
Constraints
1 ≤ N ≤ 100
Input
The first line of the input includes an integer N, the number of elements in the sequence.
In the second line, N elements of the sequence are given separated by space characters.
Output
The output consists of 2 lines.
In the first line, please print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character.
In the second line, please print the number of swap operations.
Examples
Input
6
5 6 4 2 1 3
Output
1 2 3 4 5 6
4
Input
6
5 2 4 6 1 3
Output
1 2 3 4 5 6
3
|
{"inputs": ["6\n5 2 4 6 1 0", "6\n5 6 4 4 1 3", "6\n6 2 4 6 1 0", "6\n5 6 4 4 1 2", "6\n6 2 4 6 0 0", "6\n5 6 4 3 1 2", "6\n6 2 8 6 0 0", "6\n5 6 4 2 1 2"], "outputs": ["0 1 2 4 5 6\n5\n", "1 3 4 4 5 6\n2\n", "0 1 2 4 6 6\n4\n", "1 2 4 4 5 6\n2\n", "0 0 2 4 6 6\n4\n", "1 2 3 4 5 6\n3\n", "0 0 2 6 6 8\n3\n", "1 2 2 4 5 6\n4\n"]}
| 306
| 254
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given two 0-indexed integer arrays nums1 and nums2 of equal length. Every second, for all indices 0 <= i < nums1.length, value of nums1[i] is incremented by nums2[i]. After this is done, you can do the following operation:
Choose an index 0 <= i < nums1.length and make nums1[i] = 0.
You are also given an integer x.
Return the minimum time in which you can make the sum of all elements of nums1 to be less than or equal to x, or -1 if this is not possible.
Please complete the following python code precisely:
```python
class Solution:
def minimumTime(self, nums1: List[int], nums2: List[int], x: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(nums1 = [1,2,3], nums2 = [1,2,3], x = 4) == 3\n assert candidate(nums1 = [1,2,3], nums2 = [3,3,3], x = 4) == -1\n\n\ncheck(Solution().minimumTime)"}
| 182
| 83
|
coding
|
Solve the programming task below in a Python markdown code block.
Chef recently started selling a special fruit.
He has been selling the fruit for X rupees (X is a multiple of 100). He earns a profit of Y rupees on selling the fruit currently.
Chef decided to increase the selling price by 10\%. Please help him calculate his new profit after the increase in selling price.
Note that only the selling price has been increased and the buying price is same.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of a single line of input containing two space-separated integers X and Y denoting the initial selling price and the profit respectively.
------ Output Format ------
For each test case, output a single integer, denoting the new profit.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ X ≤ 1000$
$1 ≤ Y ≤ 100$
$X$ is a multiple of $100$.
----- Sample Input 1 ------
4
100 10
200 5
500 10
100 7
----- Sample Output 1 ------
20
25
60
17
----- explanation 1 ------
Test case $1$: The buying price of the item is the difference of selling price and profit, which is $90$. The new selling price is $10\%$ more than the initial selling price. Thus, the new profit is $110-90 = 20$.
Test case $2$: The buying price of the item is the difference of selling price and profit, which is $195$. The new selling price is $10\%$ more than the initial selling price. Thus, the new profit is $220-195 = 25$.
Test case $3$: The buying price of the item is the difference of selling price and profit, which is $490$. The new selling price is $10\%$ more than the initial selling price. Thus, the new profit is $550-490 = 60$.
Test case $4$: The buying price of the item is the difference of selling price and profit, which is $93$. The new selling price is $10\%$ more than the initial selling price. Thus, the new profit is $110-93 = 17$.
|
{"inputs": ["4\n100 10\n200 5\n500 10\n100 7\n"], "outputs": ["20\n25\n60\n17\n"]}
| 543
| 50
|
coding
|
Solve the programming task below in a Python markdown code block.
2N players are running a competitive table tennis training on N tables numbered from 1 to N.
The training consists of rounds. In each round, the players form N pairs, one pair per table. In each pair, competitors play a match against each other. As a result, one of them wins and the other one loses.
The winner of the match on table X plays on table X-1 in the next round, except for the winner of the match on table 1 who stays at table 1.
Similarly, the loser of the match on table X plays on table X+1 in the next round, except for the loser of the match on table N who stays at table N.
Two friends are playing their first round matches on distinct tables A and B. Let's assume that the friends are strong enough to win or lose any match at will. What is the smallest number of rounds after which the friends can get to play a match against each other?
Constraints
* 2 \leq N \leq 10^{18}
* 1 \leq A < B \leq N
* All input values are integers.
Input
Input is given from Standard Input in the following format:
N A B
Output
Print the smallest number of rounds after which the friends can get to play a match against each other.
Examples
Input
5 2 4
Output
1
Input
5 2 3
Output
2
|
{"inputs": ["4 2 3", "5 2 8", "6 3 3", "6 3 5", "1 2 5", "0 3 4", "1 3 8", "5 2 3"], "outputs": ["2\n", "3\n", "0\n", "1\n", "-2\n", "-3\n", "-4\n", "2"]}
| 312
| 93
|
coding
|
Solve the programming task below in a Python markdown code block.
## Task
To charge your mobile phone battery, do you know how much time it takes from 0% to 100%? It depends on your cell phone battery capacity and the power of the charger. A rough calculation method is:
```
0% --> 85% (fast charge)
(battery capacity(mAh) * 85%) / power of the charger(mA)
85% --> 95% (decreasing charge)
(battery capacity(mAh) * 10%) / (power of the charger(mA) * 50%)
95% --> 100% (trickle charge)
(battery capacity(mAh) * 5%) / (power of the charger(mA) * 20%)
```
For example: Your battery capacity is 1000 mAh and you use a charger 500 mA, to charge your mobile phone battery from 0% to 100% needs time:
```
0% --> 85% (fast charge) 1.7 (hour)
85% --> 95% (decreasing charge) 0.4 (hour)
95% --> 100% (trickle charge) 0.5 (hour)
total times = 1.7 + 0.4 + 0.5 = 2.6 (hour)
```
Complete function `calculateTime` that accepts two arguments `battery` and `charger`, return how many hours can charge the battery from 0% to 100%. The result should be a number, round to 2 decimal places (In Haskell, no need to round it).
Also feel free to reuse/extend the following starter code:
```python
def calculate_time(battery,charger):
```
|
{"functional": "_inputs = [[1000, 500], [1500, 500], [2000, 1000], [5000, 1000], [1000, 5000], [3050, 2600]]\n_outputs = [[2.6], [3.9], [2.6], [6.5], [0.26], [1.53]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(calculate_time(*i), o[0])"}
| 391
| 251
|
coding
|
Solve the programming task below in a Python markdown code block.
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not.
Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase).
You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase).
You have to answer $m$ queries — calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive.
-----Input-----
The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of the string $s$ and the number of queries.
The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters.
The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) — parameters of the $i$-th query.
-----Output-----
For each query, print a single integer — the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive.
-----Examples-----
Input
5 4
baacb
1 3
1 5
4 5
2 3
Output
1
2
0
1
-----Note-----
Consider the queries of the example test.
in the first query, the substring is baa, which can be changed to bac in one operation;
in the second query, the substring is baacb, which can be changed to cbacb in two operations;
in the third query, the substring is cb, which can be left unchanged;
in the fourth query, the substring is aa, which can be changed to ba in one operation.
|
{"inputs": ["1 1\na\n1 1\n", "1 1\na\n1 1\n", "5 4\nbaacb\n1 3\n1 5\n4 5\n2 3\n", "5 4\nbaacb\n1 3\n1 5\n4 5\n2 4\n", "5 4\nbaacb\n1 3\n2 5\n4 5\n2 4\n", "5 4\nbaacb\n1 3\n1 1\n4 5\n2 3\n", "5 4\nbaacb\n3 3\n1 3\n4 5\n2 3\n", "5 4\nbabcb\n2 3\n1 3\n3 5\n2 3\n"], "outputs": ["0\n", "0\n", "1\n2\n0\n1\n", "1\n2\n0\n1\n", "1\n1\n0\n1\n", "1\n0\n0\n1\n", "0\n1\n0\n1\n", "0\n1\n1\n0\n"]}
| 520
| 248
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given a positive integer num represented as a string, return the integer num without trailing zeros as a string.
Please complete the following python code precisely:
```python
class Solution:
def removeTrailingZeros(self, num: str) -> str:
```
|
{"functional": "def check(candidate):\n assert candidate(num = \"51230100\") == \"512301\"\n assert candidate(num = \"123\") == \"123\"\n\n\ncheck(Solution().removeTrailingZeros)"}
| 71
| 64
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's.
Increment the large integer by one and return the resulting array of digits.
Please complete the following python code precisely:
```python
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
```
|
{"functional": "def check(candidate):\n assert candidate(digits = [1,2,3]) == [1,2,4]\n assert candidate(digits = [4,3,2,1]) == [4,3,2,2]\n assert candidate(digits = [0]) == [1]\n\n\ncheck(Solution().plusOne)"}
| 117
| 78
|
coding
|
Solve the programming task below in a Python markdown code block.
The king Copa often has been reported about the Codeforces site, which is rapidly getting more and more popular among the brightest minds of the humanity, who are using it for training and competing. Recently Copa understood that to conquer the world he needs to organize the world Codeforces tournament. He hopes that after it the brightest minds will become his subordinates, and the toughest part of conquering the world will be completed.
The final round of the Codeforces World Finals 20YY is scheduled for DD.MM.YY, where DD is the day of the round, MM is the month and YY are the last two digits of the year. Bob is lucky to be the first finalist form Berland. But there is one problem: according to the rules of the competition, all participants must be at least 18 years old at the moment of the finals. Bob was born on BD.BM.BY. This date is recorded in his passport, the copy of which he has already mailed to the organizers. But Bob learned that in different countries the way, in which the dates are written, differs. For example, in the US the month is written first, then the day and finally the year. Bob wonders if it is possible to rearrange the numbers in his date of birth so that he will be at least 18 years old on the day DD.MM.YY. He can always tell that in his motherland dates are written differently. Help him.
According to another strange rule, eligible participant must be born in the same century as the date of the finals. If the day of the finals is participant's 18-th birthday, he is allowed to participate.
As we are considering only the years from 2001 to 2099 for the year of the finals, use the following rule: the year is leap if it's number is divisible by four.
Input
The first line contains the date DD.MM.YY, the second line contains the date BD.BM.BY. It is guaranteed that both dates are correct, and YY and BY are always in [01;99].
It could be that by passport Bob was born after the finals. In this case, he can still change the order of numbers in date.
Output
If it is possible to rearrange the numbers in the date of birth so that Bob will be at least 18 years old on the DD.MM.YY, output YES. In the other case, output NO.
Each number contains exactly two digits and stands for day, month or year in a date. Note that it is permitted to rearrange only numbers, not digits.
Examples
Input
01.01.98
01.01.80
Output
YES
Input
20.10.20
10.02.30
Output
NO
Input
28.02.74
28.02.64
Output
NO
|
{"inputs": ["02.05.90\n08.03.50\n", "29.02.80\n29.02.60\n", "15.12.62\n17.12.21\n", "08.07.20\n27.01.01\n", "09.05.55\n25.09.42\n", "28.02.20\n11.01.29\n", "06.08.91\n05.12.73\n", "31.05.20\n02.12.04\n"], "outputs": ["YES\n", "YES\n", "YES\n", "YES\n", "NO\n", "YES\n", "YES\n", "YES\n"]}
| 623
| 198
|
coding
|
Solve the programming task below in a Python markdown code block.
Find the sum of the weights of edges of the Minimum-Cost Arborescence with the root r for a given weighted directed graph G = (V, E).
Constraints
* 1 ≤ |V| ≤ 100
* 0 ≤ |E| ≤ 1,000
* 0 ≤ wi ≤ 10,000
* G has arborescence(s) with the root r
Input
|V| |E| r
s0 t0 w0
s1 t1 w1
:
s|E|-1 t|E|-1 w|E|-1
, where |V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively. r is the root of the Minimum-Cost Arborescence.
si and ti represent source and target verticess of i-th directed edge. wi represents the weight of the i-th directed edge.
Output
Print the sum of the weights the Minimum-Cost Arborescence.
Examples
Input
4 6 0
0 1 3
0 2 2
2 0 1
2 3 1
3 0 1
3 1 5
Output
6
Input
6 10 0
0 2 7
0 1 1
0 3 5
1 4 9
2 1 6
1 3 2
3 4 3
4 2 2
2 5 8
3 5 3
Output
11
|
{"inputs": ["4 6 0\n0 1 3\n0 2 2\n2 0 1\n2 3 2\n3 0 1\n3 1 5", "4 6 0\n0 1 1\n0 2 2\n2 0 1\n2 3 2\n3 0 1\n3 1 5", "4 6 0\n0 1 1\n0 2 2\n3 1 1\n2 3 3\n3 0 0\n0 1 7", "4 6 0\n0 1 1\n0 2 2\n2 0 1\n2 3 0\n3 1 1\n3 1 5", "4 5 0\n0 1 4\n0 2 2\n3 1 1\n1 3 3\n3 0 0\n0 1 9", "4 5 0\n0 1 2\n0 2 3\n3 1 1\n1 3 3\n3 0 0\n0 1 9", "4 6 0\n0 0 1\n0 2 4\n3 0 1\n2 3 2\n3 0 0\n0 1 7", "4 6 0\n0 1 1\n0 2 0\n3 0 1\n2 3 2\n3 0 0\n0 1 0"], "outputs": ["7\n", "5\n", "6\n", "3\n", "9\n", "8\n", "13\n", "2\n"]}
| 371
| 383
|
coding
|
Solve the programming task below in a Python markdown code block.
Given is a string S consisting of L and R.
Let N be the length of S. There are N squares arranged from left to right, and the i-th character of S from the left is written on the i-th square from the left.
The character written on the leftmost square is always R, and the character written on the rightmost square is always L.
Initially, one child is standing on each square.
Each child will perform the move below 10^{100} times:
- Move one square in the direction specified by the character written in the square on which the child is standing. L denotes left, and R denotes right.
Find the number of children standing on each square after the children performed the moves.
-----Constraints-----
- S is a string of length between 2 and 10^5 (inclusive).
- Each character of S is L or R.
- The first and last characters of S are R and L, respectively.
-----Input-----
Input is given from Standard Input in the following format:
S
-----Output-----
Print the number of children standing on each square after the children performed the moves, in order from left to right.
-----Sample Input-----
RRLRL
-----Sample Output-----
0 1 2 1 1
- After each child performed one move, the number of children standing on each square is 0, 2, 1, 1, 1 from left to right.
- After each child performed two moves, the number of children standing on each square is 0, 1, 2, 1, 1 from left to right.
- After each child performed 10^{100} moves, the number of children standing on each square is 0, 1, 2, 1, 1 from left to right.
|
{"inputs": ["RL\n", "RRLRL", "RRLRL\n", "RRLLRLRLLRLL", "RRLLLLRLRLRL", "RRLRLLRLLLRL", "RRLRLLRLLRLL", "RRLLLLRRLRLL"], "outputs": ["1 1\n", "0 1 2 1 1", "0 1 2 1 1\n", "0 2 2 0 1 1 2 1 0 2 1 0\n", "0 3 3 0 0 0 1 1 1 1 1 1\n", "0 1 2 2 1 0 2 2 0 0 1 1\n", "0 1 2 2 1 0 2 1 0 2 1 0\n", "0 3 3 0 0 0 0 1 2 2 1 0\n"]}
| 388
| 223
|
coding
|
Solve the programming task below in a Python markdown code block.
In this Kata, you will be given an array of integers whose elements have both a negative and a positive value, except for one integer that is either only negative or only positive. Your task will be to find that integer.
Examples:
`[1, -1, 2, -2, 3] => 3`
`3` has no matching negative appearance
`[-3, 1, 2, 3, -1, -4, -2] => -4`
`-4` has no matching positive appearance
`[1, -1, 2, -2, 3, 3] => 3`
(the only-positive or only-negative integer may appear more than once)
Good luck!
Also feel free to reuse/extend the following starter code:
```python
def solve(arr):
```
|
{"functional": "_inputs = [[[1, -1, 2, -2, 3]], [[-3, 1, 2, 3, -1, -4, -2]], [[1, -1, 2, -2, 3, 3]], [[-110, 110, -38, -38, -62, 62, -38, -38, -38]], [[-9, -105, -9, -9, -9, -9, 105]]]\n_outputs = [[3], [-4], [3], [-38], [-9]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(solve(*i), o[0])"}
| 185
| 284
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a string $s$, consisting only of characters '0' and '1'.
You have to choose a contiguous substring of $s$ and remove all occurrences of the character, which is a strict minority in it, from the substring.
That is, if the amount of '0's in the substring is strictly smaller than the amount of '1's, remove all occurrences of '0' from the substring. If the amount of '1's is strictly smaller than the amount of '0's, remove all occurrences of '1'. If the amounts are the same, do nothing.
You have to apply the operation exactly once. What is the maximum amount of characters that can be removed?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of testcases.
The only line of each testcase contains a non-empty string $s$, consisting only of characters '0' and '1'. The length of $s$ doesn't exceed $2 \cdot 10^5$.
The total length of strings $s$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print a single integer — the maximum amount of characters that can be removed after applying the operation exactly once.
-----Examples-----
Input
4
01
1010101010111
00110001000
1
Output
0
5
3
0
-----Note-----
In the first testcase, you can choose substrings "0", "1" or "01". In "0" the amount of '0' is $1$, the amount of '1' is $0$. '1' is a strict minority, thus all occurrences of it are removed from the substring. However, since there were $0$ of them, nothing changes. Same for "1". And in "01" neither of '0' or '1' is a strict minority. Thus, nothing changes. So there is no way to remove any characters.
In the second testcase, you can choose substring "10101010101". It contains $5$ characters '0' and $6$ characters '1'. '0' is a strict minority. Thus, you can remove all its occurrences. There exist other substrings that produce the same answer.
In the third testcase, you can choose substring "011000100". It contains $6$ characters '0' and $3$ characters '1'. '1' is a strict minority. Thus, you can remove all its occurrences.
|
{"inputs": ["1\n00000000000000\n", "4\n01\n1010101010111\n00110001000\n1\n", "1\n00000000000000000011111111111111111111\n", "1\n1101010101111010110101010101001010111010\n", "1\n10101010101010101010101010101001010101010101010101010101010101010101010000\n", "1\n101010101010101010101010101010010101010101010101010101010101010101010100000\n"], "outputs": ["0\n", "0\n5\n3\n0\n", "18\n", "17\n", "35\n", "35\n"]}
| 577
| 341
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Our hero Teemo is attacking an enemy Ashe with poison attacks! When Teemo attacks Ashe, Ashe gets poisoned for a exactly duration seconds. More formally, an attack at second t will mean Ashe is poisoned during the inclusive time interval [t, t + duration - 1]. If Teemo attacks again before the poison effect ends, the timer for it is reset, and the poison effect will end duration seconds after the new attack.
You are given a non-decreasing integer array timeSeries, where timeSeries[i] denotes that Teemo attacks Ashe at second timeSeries[i], and an integer duration.
Return the total number of seconds that Ashe is poisoned.
Please complete the following python code precisely:
```python
class Solution:
def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(timeSeries = [1,4], duration = 2) == 4\n assert candidate(timeSeries = [1,2], duration = 2) == 3\n\n\ncheck(Solution().findPoisonedDuration)"}
| 188
| 62
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given a binary array nums, return the maximum length of a contiguous subarray with an equal number of 0 and 1.
Please complete the following python code precisely:
```python
class Solution:
def findMaxLength(self, nums: List[int]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(nums = [0,1]) == 2\n assert candidate(nums = [0,1,0]) == 2\n\n\ncheck(Solution().findMaxLength)"}
| 74
| 49
|
coding
|
Solve the programming task below in a Python markdown code block.
Every great chef knows that lucky numbers are positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Our chef has recently returned from the Lucky country. He observed that every restaurant in the Lucky country had a lucky number as its name.
He believes that having a lucky number as a restaurant name can indeed turn out to be very lucky.
Our chef believes that it is possible to make a lucky number having N digits even luckier. Any number following the rules below is called Lucky lucky number -
1. The number contains only digits 4 and 7.
2. Count of digit 4 in the number should be divisible by 7.
3. Count of digit 7 in the number should be divisible by 4.
Help our chef to compute the count of digit 4 in the smallest Lucky lucky number having N digits.
-----Input-----
First line contains T, number of test cases. Each of the next T lines contains a number N, the number of digits in the Lucky lucky number to be formed.
1<=T<=1000
1<=N<=1000000000 (10^9)
-----Output-----
If it is not possible to form a Lucky lucky number having N digits, output -1.
Otherwise, output the count of digit 4 in the smallest Lucky lucky number having N digits.
-----Example-----
Input:
5
7
4
11
1
15
Output:
7
0
7
-1
7
Explanation
For the last test case, N = 15, the smallest lucky lucky number is
444444477777777. The count of digit 4 is 7.
|
{"inputs": ["5\n7\n4\n11\n1\n15\n\n"], "outputs": ["7\n0\n7\n-1\n7"]}
| 411
| 35
|
coding
|
Solve the programming task below in a Python markdown code block.
Everybody knows that opposites attract. That is the key principle of the "Perfect Matching" dating agency. The "Perfect Matching" matchmakers have classified each registered customer by his interests and assigned to the i-th client number ti ( - 10 ≤ ti ≤ 10). Of course, one number can be assigned to any number of customers.
"Perfect Matching" wants to advertise its services and publish the number of opposite couples, that is, the couples who have opposite values of t. Each couple consists of exactly two clients. The customer can be included in a couple an arbitrary number of times. Help the agency and write the program that will find the sought number by the given sequence t1, t2, ..., tn. For example, if t = (1, - 1, 1, - 1), then any two elements ti and tj form a couple if i and j have different parity. Consequently, in this case the sought number equals 4.
Of course, a client can't form a couple with him/herself.
Input
The first line of the input data contains an integer n (1 ≤ n ≤ 105) which represents the number of registered clients of the "Couple Matching". The second line contains a sequence of integers t1, t2, ..., tn ( - 10 ≤ ti ≤ 10), ti — is the parameter of the i-th customer that has been assigned to the customer by the result of the analysis of his interests.
Output
Print the number of couples of customs with opposite t. The opposite number for x is number - x (0 is opposite to itself). Couples that only differ in the clients' order are considered the same.
Note that the answer to the problem can be large enough, so you must use the 64-bit integer type for calculations. Please, do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator.
Examples
Input
5
-3 3 0 0 3
Output
3
Input
3
0 0 0
Output
3
Note
In the first sample the couples of opposite clients are: (1,2), (1,5) и (3,4).
In the second sample any couple of clients is opposite.
|
{"inputs": ["1\n0\n", "1\n5\n", "1\n9\n", "1\n1\n", "1\n-1\n", "1\n-2\n", "1\n-4\n", "1\n-7\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
| 505
| 90
|
coding
|
Solve the programming task below in a Python markdown code block.
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya has a number consisting of n digits without leading zeroes. He represented it as an array of digits without leading zeroes. Let's call it d. The numeration starts with 1, starting from the most significant digit. Petya wants to perform the following operation k times: find the minimum x (1 ≤ x < n) such that dx = 4 and dx + 1 = 7, if x is odd, then to assign dx = dx + 1 = 4, otherwise to assign dx = dx + 1 = 7. Note that if no x was found, then the operation counts as completed and the array doesn't change at all.
You are given the initial number as an array of digits and the number k. Help Petya find the result of completing k operations.
Input
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 109) — the number of digits in the number and the number of completed operations. The second line contains n digits without spaces representing the array of digits d, starting with d1. It is guaranteed that the first digit of the number does not equal zero.
Output
In the single line print the result without spaces — the number after the k operations are fulfilled.
Examples
Input
7 4
4727447
Output
4427477
Input
4 2
4478
Output
4478
Note
In the first sample the number changes in the following sequence: 4727447 → 4427447 → 4427477 → 4427447 → 4427477.
In the second sample: 4478 → 4778 → 4478.
|
{"inputs": ["2 0\n47\n", "2 1\n47\n", "3 99\n447\n", "3 74\n447\n", "3 99\n609\n", "4 2\n4478\n", "5 0\n12473\n", "3 100\n447\n"], "outputs": ["47\n", "44\n", "477\n", "447\n", "609\n", "4478\n", "12473\n", "447\n"]}
| 469
| 141
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
There are $N$ circles in a plane. Circle $i$ and circle $j$ form a *good pair* if it is possible to choose a point $P_{1}$ on the perimeter of circle $i$ and a point $P_{2}$ on the perimeter of circle $j$ such that the Euclidean distance between $P_{1}$ and $P_{2}$ is exactly $K$. (Note that $P_{1}$ and $P_{2}$ do not need to have integer coordinates.)
You should answer $Q$ queries. In each query, you are given the required distance $K$. Chef is interested in finding the number of good pairs of distinct circles for each query. Please help him.
Note: Circle $i$ and circle $j$ are distinct when $i \neq j$. There may be any number of circles that coincide (have identical centers and radii).
------ Input ------
The first line of the input contains two space-separated integers $N$ and $Q$ denoting the number of circles and the number of queries respectively.
Each of the following $N$ lines contains three space-separated integers $X$, $Y$ and $R$ describing a circle with radius $R$ and center $(X, Y)$.
Each of the next $Q$ lines contains one integer $K$ describing a query.
------ Output ------
For each query, print a single line containing one integer — the number of good pairs of circles.
------ Constraints ------
$2 ≤ N ≤ 10^{3}$
$1 ≤ Q ≤ 5\cdot 10^{5}$
$X, Y ≤ |2\cdot 10^{5}|$
$1 ≤ R ≤ 2\cdot 10^{5}$
$0 ≤ K ≤ 10^{6}$
------ Subtasks ------
Subtask #1 (30 points):
$2 ≤ N ≤ 10^{2}$
$1 ≤ Q ≤ 5\cdot 10^{2}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
2 3
0 0 5
8 3 2
0
10
20
----- Sample Output 1 ------
0
1
0
----- explanation 1 ------
The distance between the point $(0.00, -5.00)$ on circle $1$ and the point $(8.00, 1.00)$ on circle $2$ is $10$. There is no pair of points on these two circles with distance $0$ or $20$.
|
{"inputs": ["2 3\n0 0 5\n8 3 2\n0\n10\n20"], "outputs": ["0\n1\n0"]}
| 601
| 38
|
coding
|
Solve the programming task below in a Python markdown code block.
A dragon symbolizes wisdom, power and wealth. On Lunar New Year's Day, people model a dragon with bamboo strips and clothes, raise them with rods, and hold the rods high and low to resemble a flying dragon.
A performer holding the rod low is represented by a 1, while one holding it high is represented by a 2. Thus, the line of performers can be represented by a sequence a_1, a_2, ..., a_{n}.
Little Tommy is among them. He would like to choose an interval [l, r] (1 ≤ l ≤ r ≤ n), then reverse a_{l}, a_{l} + 1, ..., a_{r} so that the length of the longest non-decreasing subsequence of the new sequence is maximum.
A non-decreasing subsequence is a sequence of indices p_1, p_2, ..., p_{k}, such that p_1 < p_2 < ... < p_{k} and a_{p}_1 ≤ a_{p}_2 ≤ ... ≤ a_{p}_{k}. The length of the subsequence is k.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 2000), denoting the length of the original sequence.
The second line contains n space-separated integers, describing the original sequence a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 2, i = 1, 2, ..., n).
-----Output-----
Print a single integer, which means the maximum possible length of the longest non-decreasing subsequence of the new sequence.
-----Examples-----
Input
4
1 2 1 2
Output
4
Input
10
1 1 2 2 2 1 1 2 2 1
Output
9
-----Note-----
In the first example, after reversing [2, 3], the array will become [1, 1, 2, 2], where the length of the longest non-decreasing subsequence is 4.
In the second example, after reversing [3, 7], the array will become [1, 1, 1, 1, 2, 2, 2, 2, 2, 1], where the length of the longest non-decreasing subsequence is 9.
|
{"inputs": ["1\n2\n", "1\n2\n", "1\n1\n", "2\n1 2\n", "2\n2 1\n", "2\n1 2\n", "2\n2 1\n", "2\n1 1\n"], "outputs": ["1\n", "1\n", "1\n", "2\n", "2\n", "2\n", "2\n", "2\n"]}
| 506
| 96
|
coding
|
Solve the programming task below in a Python markdown code block.
Create a function which answers the question "Are you playing banjo?".
If your name starts with the letter "R" or lower case "r", you are playing banjo!
The function takes a name as its only argument, and returns one of the following strings:
```
name + " plays banjo"
name + " does not play banjo"
```
Names given are always valid strings.
Also feel free to reuse/extend the following starter code:
```python
def areYouPlayingBanjo(name):
```
|
{"functional": "_inputs = [['Adam'], ['Paul'], ['Ringo'], ['bravo'], ['rolf']]\n_outputs = [['Adam does not play banjo'], ['Paul does not play banjo'], ['Ringo plays banjo'], ['bravo does not play banjo'], ['rolf plays banjo']]\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(areYouPlayingBanjo(*i), o[0])"}
| 121
| 209
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given two strings current and correct representing two 24-hour times.
24-hour times are formatted as "HH:MM", where HH is between 00 and 23, and MM is between 00 and 59. The earliest 24-hour time is 00:00, and the latest is 23:59.
In one operation you can increase the time current by 1, 5, 15, or 60 minutes. You can perform this operation any number of times.
Return the minimum number of operations needed to convert current to correct.
Please complete the following python code precisely:
```python
class Solution:
def convertTime(self, current: str, correct: str) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(current = \"02:30\", correct = \"04:35\") == 3\n assert candidate(current = \"11:00\", correct = \"11:01\") == 1\n\n\ncheck(Solution().convertTime)"}
| 178
| 69
|
coding
|
Solve the programming task below in a Python markdown code block.
*It seemed a good idea at the time...*
# Why I did it?
After a year on **Codewars** I really needed a holiday...
But not wanting to drift backwards in the honour rankings while I was away, I hatched a cunning plan!
# The Cunning Plan
So I borrowed my friend's "Clone Machine" and cloned myself :-)
Now my clone can do my Kata solutions for me and I can relax!
Brilliant!!
Furthermore, at the end of the day my clone can re-clone herself...
Double brilliant!!
I wonder why I didn't think to do this earlier?
So as I left for the airport I gave my clone instructions to:
* do my Kata solutions for me
* feed the cat
* try to keep the house tidy and not eat too much
* sleep
* clone yourself
* repeat same next day
# The Flaw
Well, how was I supposed to know that cloned DNA is faulty?
:-(
Every time they sleep they wake up with decreased ability - they get slower... they get dumber... they are only able to solve 1 less Kata than they could the previous day.
For example, if they can solve 10 Kata today, then tomorrow they can solve only 9 Kata, then 8, 7, 6... Eventually they can't do much more than sit around all day playing video games.
And (unlike me), when the clone cannot solve any more Kata they are no longer clever enough to operate the clone machine either!
# The Return Home
I suspected something was wrong when I noticed my **Codewars** honour had stopped rising.
I made a hasty return home...
...and found 100s of clones scattered through the house. Mostly they sit harmlessly mumbling to themselves. The largest group have made a kind of nest in my loungeroom where they sit catatonic in front of the PlayStation.
The whole place needs fumigating.
The fridge and pantry are empty.
And I can't find the cat.
# Kata Task
Write a method to predict the final outcome where:
Input:
* `kata-per-day` is the number of Kata I can solve per day
Output:
* ```[number-of-clones, number-of-kata-solved-by-clones]```
Also feel free to reuse/extend the following starter code:
```python
def clonewars(n):
```
|
{"functional": "_inputs = [[0], [1], [5], [10]]\n_outputs = [[[1, 0]], [[1, 1]], [[16, 57]], [[512, 2036]]]\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(clonewars(*i), o[0])"}
| 513
| 194
|
coding
|
Solve the programming task below in a Python markdown code block.
A little weird green frog speaks in a very strange variation of English: it reverses sentence, omitting all puntuation marks `, ; ( ) - ` except the final exclamation, question or period. We urgently need help with building a proper translator.
To simplify the task, we always use lower-case letters. Apostrophes are forbidden as well.
Translator should be able to process multiple sentences in one go. Sentences are separated by arbitrary amount of spaces.
**Examples**
`you should use python.` -> `python use should you.`
`look, a fly!` -> `fly a look!`
`multisentence is good. is not it?` -> `good is multisentence. it not is?`
Also feel free to reuse/extend the following starter code:
```python
def frogify(s):
```
|
{"functional": "_inputs = [['i am a frog.'], ['can you do it?'], ['seems like you understand!'], ['multisentence is good. is not it?'], ['green, red or orange - we all just frogs, do not you think so?']]\n_outputs = [['frog a am i.'], ['it do you can?'], ['understand you like seems!'], ['good is multisentence. it not is?'], ['so think you not do frogs just all we orange or red green?']]\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(frogify(*i), o[0])"}
| 183
| 251
|
coding
|
Solve the programming task below in a Python markdown code block.
A rabbit who came to the fair found that the prize for a game at a store was a carrot cake. The rules for this game are as follows.
There is a grid-like field of vertical h squares x horizontal w squares, and each block has at most one block. Each block is a color represented by one of the uppercase letters ('A'-'Z'). When n or more blocks of the same color are lined up in a straight line vertically or horizontally, those blocks disappear.
Participants can select two adjacent cells next to each other and switch their states to each other. When blocks are exchanged, disappeared, or dropped and there are no blocks in the cell below the cell with the block, this block At this time, it disappears when n or more blocks of the same color are lined up again. However, the disappearance of the blocks does not occur while the falling blocks exist, and occurs at the same time when all the blocks have finished falling.
If you eliminate all the blocks on the field with one operation, this game will be successful and you will get a free gift cake. Rabbit wants to get the cake with one entry fee, can not do it If you don't want to participate. From the state of the field at the beginning of the game, answer if the rabbit should participate in this game.
Input
The first line of input is given h, w, n separated by spaces.
2 ≤ h, w, n ≤ 30
In the following h lines, the state of the field is given in order from the top. Uppercase letters represent blocks, and'.' Represents an empty space. The state of the given field is n or more consecutive blocks of the same color in the vertical or horizontal direction. No, no blocks are in a falling state. There is one or more blocks.
Output
Output "YES" if the rabbit should participate in this game, otherwise output "NO" on one line.
Examples
Input
4 6 3
......
...Y..
...Y..
RRYRYY
Output
YES
Input
4 6 3
......
...Y..
...Y..
RRYRY.
Output
NO
|
{"inputs": ["4 6 3\n......\n..Y...\n...Y..\nRRYRYY", "4 6 1\n......\n...Y..\n/..Z..\nYRRRYY", "4 6 3\n......\n...Y..\n...Y..\nRRYRZ.", "4 6 3\n......\n...Y..\n/..Y..\nRRYRYY", "4 6 3\n......\n...Y..\n...Y..\n.RYRYR", "4 6 3\n......\n...Y..\n...Z..\nRRYRZ.", "4 6 3\n......\n...Y..\n/..Z..\nRRYRYY", "4 6 3\n....-.\n...Y..\n...Y..\n.RYRYR"], "outputs": ["NO\n", "YES\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n"]}
| 456
| 226
|
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. A string swap is an operation where you choose two indices in a string (not necessarily different) and swap the characters at these indices.
Return true if it is possible to make both strings equal by performing at most one string swap on exactly one of the strings. Otherwise, return false.
Please complete the following python code precisely:
```python
class Solution:
def areAlmostEqual(self, s1: str, s2: str) -> bool:
```
|
{"functional": "def check(candidate):\n assert candidate(s1 = \"bank\", s2 = \"kanb\") == True\n assert candidate(s1 = \"attack\", s2 = \"defend\") == False\n assert candidate(s1 = \"kelb\", s2 = \"kelb\") == True\n assert candidate(s1 = \"abcd\", s2 = \"dcba\") == False\n\n\ncheck(Solution().areAlmostEqual)"}
| 124
| 96
|
coding
|
Solve the programming task below in a Python markdown code block.
In this kata you are given an array to sort but you're expected to start sorting from a specific position of the array (in ascending order) and optionally you're given the number of items to sort.
#### Examples:
```python
sect_sort([1, 2, 5, 7, 4, 6, 3, 9, 8], 2) //=> [1, 2, 3, 4, 5, 6, 7, 8, 9]
sect_sort([9, 7, 4, 2, 5, 3, 1, 8, 6], 2, 5) //=> [9, 7, 1, 2, 3, 4, 5, 8, 6]
```
#### Documentation:
```python
sect_sort(array, start, length);
```
- array - array to sort
- start - position to begin sorting
- length - number of items to sort (optional)
if the **length** argument is not passed or is zero, you sort all items to the right of the start postiton in the array
Also feel free to reuse/extend the following starter code:
```python
def sect_sort(lst, start, length=0):
```
|
{"functional": "_inputs = [[[1, 2, 5, 7, 4, 6, 3, 9, 8], 2], [[1, 2, 5, 7, 4, 6, 3, 9, 8], 8], [[9, 7, 4, 2, 5, 3, 1, 8, 6], 2, 5], [[1, 2, 5, 7, 4, 6, 3, 9, 8], 8, 3], [[], 0]]\n_outputs = [[[1, 2, 3, 4, 5, 6, 7, 8, 9]], [[1, 2, 5, 7, 4, 6, 3, 9, 8]], [[9, 7, 1, 2, 3, 4, 5, 8, 6]], [[1, 2, 5, 7, 4, 6, 3, 9, 8]], [[]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(sect_sort(*i), o[0])"}
| 285
| 390
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
A truck has two fuel tanks. You are given two integers, mainTank representing the fuel present in the main tank in liters and additionalTank representing the fuel present in the additional tank in liters.
The truck has a mileage of 10 km per liter. Whenever 5 liters of fuel get used up in the main tank, if the additional tank has at least 1 liters of fuel, 1 liters of fuel will be transferred from the additional tank to the main tank.
Return the maximum distance which can be traveled.
Note: Injection from the additional tank is not continuous. It happens suddenly and immediately for every 5 liters consumed.
Please complete the following python code precisely:
```python
class Solution:
def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(mainTank = 5, additionalTank = 10) == 60\n assert candidate(mainTank = 1, additionalTank = 2) == 10\n\n\ncheck(Solution().distanceTraveled)"}
| 184
| 61
|
coding
|
Solve the programming task below in a Python markdown code block.
Japan achieved the second straight victory in the national baseball competition WBC !! A baseball tournament was held at Aizu Gakuen High School as baseball became more popular. In this tournament, a round-robin league match will be held and the ranking will be decided in the following ways.
1. The team with the most wins is ranked high
2. If the number of wins is the same, the team with the fewest losses will be ranked higher.
Create a program that inputs the results of each team and outputs the team names in order from the top team. If there are teams with the same rank, output them in the order of input. However, the number of teams n is an integer from 2 to 10 and the team name t is a single-byte alphabetic character. , 2 for a draw. Also, the team name shall be unique.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
score1
score2
::
scoren
The number of teams n (2 ≤ n ≤ 10) is given on the first line, and the scorei of the i-th team is given on the following n lines. Each grade is given in the following format.
t r1 r2 ... rn−1
The team name t (one-character half-width alphabetic character) and the result ri (0, 1, or 2) for each match of t are given separated by blanks.
The number of datasets does not exceed 50.
Output
For each dataset, the team name is output in order from the top team.
Example
Input
6
A 1 0 0 2 0
B 0 0 1 1 0
C 1 1 1 1 1
D 1 0 0 1 2
E 2 0 0 0 0
F 1 1 0 2 1
4
g 1 1 1
h 0 1 2
w 0 0 0
b 0 2 1
0
Output
E
A
B
D
F
C
w
h
b
g
|
{"inputs": ["6\nA 1 0 0 2 0\nB 0 0 1 1 0\nC 1 1 1 1 1\nD 1 0 0 1 2\nE 2 0 0 0 0\nF 1 1 0 2 1\n4\ng 1 1 1\nh 0 1 2\nx 0 0 0\nb 0 2 1\n0", "6\nA 1 0 0 2 0\nB 0 0 1 1 0\nC 1 1 1 1 1\nD 1 0 0 1 2\nE 2 0 0 0 0\nF 1 1 0 2 1\n4\ng 1 1 1\ng 0 1 2\nx 0 0 0\nb 0 2 1\n0", "6\nA 1 0 0 2 0\nC 0 0 1 1 0\nC 1 1 1 1 1\nD 1 0 0 1 2\nE 2 0 0 0 0\nF 1 1 0 2 1\n4\ng 1 1 1\ng 0 1 2\nx 0 0 0\nb 0 2 1\n0", "6\nA 1 0 0 2 0\nB 0 0 1 1 0\nC 1 1 1 1 1\nD 1 0 0 1 2\nE 2 0 0 1 0\nF 1 1 0 2 1\n4\ng 1 1 1\nh 0 1 2\nw 0 0 0\nb 0 2 1\n0", "6\nA 1 0 1 2 0\nB 0 0 1 1 0\nC 1 1 1 1 2\nD 1 0 1 0 1\nE 2 0 0 1 0\nF 1 1 0 2 1\n4\nh 1 1 1\ng 0 0 2\nw 0 0 0\nb 0 3 2\n0", "6\nA 1 0 0 2 0\nB 0 0 1 1 0\nC 1 1 1 1 1\nD 1 1 0 1 2\nE 2 0 0 0 0\nF 2 1 0 2 1\n4\ng 1 1 1\nh 0 1 2\nx 0 0 0\nb 0 2 1\n0", "6\nA 1 0 0 2 0\nB 0 0 1 1 0\nC 1 1 1 0 1\nD 1 0 0 1 2\nE 2 0 0 1 0\nF 1 1 0 2 1\n4\ng 1 1 1\nh 0 1 1\nw 0 0 0\nb 0 2 1\n0", "6\nA 1 0 0 2 0\nB 0 0 1 1 0\nC 1 1 1 1 1\nD 1 0 1 1 2\nE 2 0 0 0 0\nF 1 1 0 2 1\n4\ng 1 1 1\nh 0 1 2\ny 0 0 0\nb 0 2 1\n0"], "outputs": ["E\nA\nB\nD\nF\nC\nx\nh\nb\ng\n", "E\nA\nB\nD\nF\nC\nx\ng\nb\ng\n", "E\nA\nC\nD\nF\nC\nx\ng\nb\ng\n", "A\nE\nB\nD\nF\nC\nw\nh\nb\ng\n", "E\nB\nA\nD\nF\nC\nw\ng\nb\nh\n", "E\nA\nB\nF\nD\nC\nx\nh\nb\ng\n", "A\nE\nB\nD\nF\nC\nw\nb\nh\ng\n", "E\nA\nB\nD\nF\nC\ny\nh\nb\ng\n"]}
| 496
| 1,070
|
coding
|
Solve the programming task below in a Python markdown code block.
A border of a string is a proper prefix of it that is also a suffix. For example:
a and abra are borders of abracadabra,
kan and kankan are borders of kankankan.
de is a border of decode.
Note that decode is not a border of decode because it's not proper.
A palindromic border is a border that is palindromic. For example,
a and ana are palindromic borders of anabanana,
l, lol and lolol are palindromic borders of lololol.
Let's define $P(s)$ as the number of palindromic borders of string $\boldsymbol{\mathrm{~s~}}$. For example, if $\boldsymbol{s}=$ lololol, then $P(s)=3$.
Now, a string of length $N$ has exactly $N(N+1)/2$ non-empty substrings (we count substrings as distinct if they are of different lengths or are in different positions, even if they are the same string). Given a string $\boldsymbol{\mathrm{~s~}}$, consisting only of the first 8 lowercase letters of the English alphabet, your task is to find the sum of $P(s^{\prime})$ for all the non-empty substrings ${s'}$ of $\boldsymbol{\mathrm{~s~}}$. In other words, you need to find:
$\sum\limits_{1\leq i\leq j\leq N}P\left(s_{i\ldots j}\right)$
where $s_{i\ldots j}$ is the substring of $\boldsymbol{\mathrm{~s~}}$ starting at position $\boldsymbol{i}$ and ending at position $j$.
Since the answer can be very large, output the answer modulo $10^9+7$.
Input Format
The first line contains a string consisting of $N$ characters.
Output Format
Print a single integer: the remainder of the division of the resulting number by $10^9+7$.
Constraints
$1\leq N\leq10^5$
All characters in the string can be any of the first 8 lowercase letters of the English alphabet (abcdefgh).
Sample Input 1
ababa
Sample Output 1
5
Sample Input 2
aaaa
Sample Output 2
10
Sample Input 3
abcacb
Sample Output 3
3
Explanation
$\boldsymbol{s}=$ ababa has 15 substrings but only 4 substrings have palindromic borders.
$s_{1\ldots3}=$ aba $\longrightarrow P(s_{1\ldots3})=1$
$s_{1\ldots5}=$ ababa $\longrightarrow P(s_{1...5})=2$
$s_{2\ldots4}=$ bab $\longrightarrow P(s_{2\ldots4})=1$
$s_{3\ldots5}=$ aba $\rightarrow P(s_{3\ldots5})=1$
|
{"inputs": ["aaaa\n", "ababa\n", "abcacb\n"], "outputs": ["10\n", "5\n", "3\n"]}
| 663
| 33
|
coding
|
Solve the programming task below in a Python markdown code block.
A Little Elephant from the Zoo of Lviv likes lucky strings, i.e., the strings that consist only of the lucky digits 4 and 7.
The Little Elephant calls some string T of the length M balanced if there exists at least one integer X (1 ≤ X ≤ M) such that the number of digits 4 in the substring T[1, X - 1] is equal to the number of digits 7 in the substring T[X, M]. For example, the string S = 7477447 is balanced since S[1, 4] = 7477 has 1 digit 4 and S[5, 7] = 447 has 1 digit 7. On the other hand, one can verify that the string S = 7 is not balanced.
The Little Elephant has the string S of the length N. He wants to know the number of such pairs of integers (L; R) that 1 ≤ L ≤ R ≤ N and the substring S[L, R] is balanced. Help him to find this number.
Notes.
Let S be some lucky string. Then
- |S| denotes the length of the string S;
- S[i] (1 ≤ i ≤ |S|) denotes the ith character of S (the numeration of characters starts from 1);
- S[L, R] (1 ≤ L ≤ R ≤ |S|) denotes the string with the following sequence of characters: S[L], S[L + 1], ..., S[R], and is called a substring of S. For L > R we mean by S[L, R] an empty string.
-----Input-----
The first line of the input file contains a single integer T, the number of test cases. Each of the following T lines contains one string, the string S for the corresponding test case. The input file does not contain any whitespaces.
-----Output-----
For each test case output a single line containing the answer for this test case.
-----Constraints-----
1 ≤ T ≤ 10
1 ≤ |S| ≤ 100000
S consists only of the lucky digits 4 and 7.
-----Example-----
Input:
4
47
74
477
4747477
Output:
2
2
3
23
-----Explanation-----
In the first test case balance substrings are S[1, 1] = 4 and S[1, 2] = 47.
In the second test case balance substrings are S[2, 2] = 4 and S[1, 2] = 74.
Unfortunately, we can't provide you with the explanations of the third and the fourth test cases. You should figure it out by yourself. Please, don't ask about this in comments.
|
{"inputs": ["4\n47\n74\n477\n4747477\n\n"], "outputs": ["2\n2\n3\n23"]}
| 606
| 39
|
coding
|
Solve the programming task below in a Python markdown code block.
Given a triplet of integers (X , Y , Z), such that X ≤ Y and Y ≥ Z, we define f(X , Y , Z) to be (X + Y) * (Y + Z). If either X > Y or Y < Z, or both, then f(X , Y , Z) is defined to be 0.
You are provided three arrays A , B and C of any length (their lengths may or may not be equal).
Your task is to find the sum of f(X , Y , Z) over all triplets (X, Y , Z) where X, Y and Z belong to A, B and C respectively.
Output your sum for each test case modulo 1000000007.
-----Input-----
- The first line contains a single integer, T, which is the number of test cases. The description of each testcase follows:
- The first line of each testcase contains 3 integers: p, q and r. These denote the lengths of A,B and C respectively.
- The second line contains p integers, which are the elements of A
- The third line contains q integers, which are the elements of B
- The fourth line contains r integers, which are the elements of C
-----Output-----
Output the required sum modulo 1000000007 for each test case in a new line.
-----Constraints-----
- 1 ≤ T ≤ 10
- 1 ≤ p, q, r ≤ 100000
- 1 ≤ every array element ≤ 1000000000
-----Subtasks -----
- Subtask #1 (30 points): 1 ≤ p,q,r ≤ 100
- Subtask #2 (70 points): 1 ≤ p,q,r ≤ 100000
-----Example : -----
Input:
1
3 1 3
1 2 3
5
4 5 6
Output:
399
-----Explanation: -----
As there is only one choice for Y which equals to 5, to get a non-zero function value,we can choose any element for X from the set { 1 , 2 , 3 } and for Z from the set { 4 , 5 }
So triplets which give non-zero function values are:
{ 1 , 5 , 4 } : ( 1 + 5 ) * ( 5 + 4 ) = 54
{ 1 , 5 , 5 } : ( 1 + 5 ) * ( 5 + 5 ) = 60
{ 2 , 5 , 4 } : ( 2 + 5 ) * ( 5 + 4 ) = 63
{ 2 , 5 , 5 } : ( 2 + 5 ) * ( 5 + 5 ) = 70
{ 3 , 5 , 4 } : ( 3 + 5 ) * ( 5 + 4 ) = 72
{ 3 , 5 , 5 } : ( 3 + 5 ) * ( 5 + 5 ) = 80
Final answer : 54 + 60 + 63 + 70 + 72 + 80 = 399
|
{"inputs": ["1 \n3 1 3\n1 2 3\n5\n4 5 6\n\n"], "outputs": ["399"]}
| 757
| 37
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given a string s consisting of lowercase English letters, and an integer k. Your task is to convert the string into an integer by a special process, and then transform it by summing its digits repeatedly k times. More specifically, perform the following steps:
Convert s into an integer by replacing each letter with its position in the alphabet (i.e. replace 'a' with 1, 'b' with 2, ..., 'z' with 26).
Transform the integer by replacing it with the sum of its digits.
Repeat the transform operation (step 2) k times in total.
For example, if s = "zbax" and k = 2, then the resulting integer would be 8 by the following operations:
Convert: "zbax" ➝ "(26)(2)(1)(24)" ➝ "262124" ➝ 262124
Transform #1: 262124 ➝ 2 + 6 + 2 + 1 + 2 + 4 ➝ 17
Transform #2: 17 ➝ 1 + 7 ➝ 8
Return the resulting integer after performing the operations described above.
Please complete the following python code precisely:
```python
class Solution:
def getLucky(self, s: str, k: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(s = \"iiii\", k = 1) == 36\n assert candidate(s = \"leetcode\", k = 2) == 6\n\n\ncheck(Solution().getLucky)"}
| 310
| 56
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given two arithmetic progressions: a_1k + b_1 and a_2l + b_2. Find the number of integers x such that L ≤ x ≤ R and x = a_1k' + b_1 = a_2l' + b_2, for some integers k', l' ≥ 0.
-----Input-----
The only line contains six integers a_1, b_1, a_2, b_2, L, R (0 < a_1, a_2 ≤ 2·10^9, - 2·10^9 ≤ b_1, b_2, L, R ≤ 2·10^9, L ≤ R).
-----Output-----
Print the desired number of integers x.
-----Examples-----
Input
2 0 3 3 5 21
Output
3
Input
2 4 3 0 6 17
Output
2
|
{"inputs": ["2 0 3 3 5 21\n", "2 4 3 0 6 17\n", "2 0 3 3 5 21\n", "2 4 3 0 6 17\n", "4 0 4 1 5 100\n", "4 0 4 1 0 100\n", "4 0 4 1 0 100\n", "4 0 4 1 5 100\n"], "outputs": ["3\n", "2\n", "3\n", "2\n", "0\n", "0\n", "0\n", "0\n"]}
| 218
| 162
|
coding
|
Solve the programming task below in a Python markdown code block.
A boy Petya loves chess very much. He even came up with a chess piece of his own, a semiknight. The semiknight can move in any of these four directions: 2 squares forward and 2 squares to the right, 2 squares forward and 2 squares to the left, 2 squares backward and 2 to the right and 2 squares backward and 2 to the left. Naturally, the semiknight cannot move beyond the limits of the chessboard.
Petya put two semiknights on a standard chessboard. Petya simultaneously moves with both semiknights. The squares are rather large, so after some move the semiknights can meet, that is, they can end up in the same square. After the meeting the semiknights can move on, so it is possible that they meet again. Petya wonders if there is such sequence of moves when the semiknights meet. Petya considers some squares bad. That is, they do not suit for the meeting. The semiknights can move through these squares but their meetings in these squares don't count.
Petya prepared multiple chess boards. Help Petya find out whether the semiknights can meet on some good square for each board.
Please see the test case analysis.
-----Input-----
The first line contains number t (1 ≤ t ≤ 50) — the number of boards. Each board is described by a matrix of characters, consisting of 8 rows and 8 columns. The matrix consists of characters ".", "#", "K", representing an empty good square, a bad square and the semiknight's position, correspondingly. It is guaranteed that matrix contains exactly 2 semiknights. The semiknight's squares are considered good for the meeting. The tests are separated by empty line.
-----Output-----
For each test, print on a single line the answer to the problem: "YES", if the semiknights can meet and "NO" otherwise.
-----Examples-----
Input
2
........
........
......#.
K..##..#
.......#
...##..#
......#.
K.......
........
........
..#.....
..#..#..
..####..
...##...
........
....K#K#
Output
YES
NO
-----Note-----
Consider the first board from the sample. We will assume the rows and columns of the matrix to be numbered 1 through 8 from top to bottom and from left to right, correspondingly. The knights can meet, for example, in square (2, 7). The semiknight from square (4, 1) goes to square (2, 3) and the semiknight goes from square (8, 1) to square (6, 3). Then both semiknights go to (4, 5) but this square is bad, so they move together to square (2, 7).
On the second board the semiknights will never meet.
|
{"inputs": ["1\n...#...#\n........\n.#...K..\n........\n...#...#\n........\n.K...#..\n........\n", "1\nK.#....#\n...#..#.\n..#.....\n..#.###.\n..#.....\n...#....\n.#.....#\n.#...##K\n", "1\n...#...#\n........\n.#...K..\n........\n...#...#\n........\n.K...#..\n........\n", "1\nK.#....#\n...#..#.\n..#.....\n..#.###.\n..#.....\n...#....\n.#.....#\n.#...##K\n", "1\nK.#....#\n.#..#...\n..#.....\n..#.###.\n..#.....\n...#....\n.#.....#\n.#...##K\n", "1\n#...#...\n........\n.#...K..\n........\n...#...#\n........\n.K...#..\n........\n", "1\n#.K....#\n...#..#.\n..#.....\n..#.###.\n..#.....\n...#....\n.#.....#\n.#...##K\n", "1\n#.K....#\n...#..#.\n..#.....\n..#.###.\n.....#..\n...#....\n.#.....#\n.#...##K\n"], "outputs": ["YES\n", "NO\n", "YES\n", "NO\n", "NO\n", "YES\n", "NO\n", "NO\n"]}
| 642
| 364
|
coding
|
Solve the programming task below in a Python markdown code block.
Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).
Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
Examples
Input
4
Output
2
Input
11
Output
10
Input
5
Output
0
Input
33
Output
21
Note
In the first sample 4 = 2 + 2, a = 2 is the only possibility.
In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.
It's easy to check that there is no suitable a in the third sample.
In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer.
|
{"inputs": ["1\n", "5\n", "4\n", "99\n", "91\n", "67\n", "11\n", "33\n"], "outputs": ["0\n", "0\n", "2\n", "54", "0", "0", "10\n", "21"]}
| 535
| 74
|
coding
|
Solve the programming task below in a Python markdown code block.
This problem is a simplified version of D2, but it has significant differences, so read the whole statement.
Polycarp has an array of $n$ ($n$ is even) integers $a_1, a_2, \dots, a_n$. Polycarp conceived of a positive integer $k$. After that, Polycarp began performing the following operations on the array: take an index $i$ ($1 \le i \le n$) and reduce the number $a_i$ by $k$.
After Polycarp performed some (possibly zero) number of such operations, it turned out that all numbers in the array became the same. Find the maximum $k$ at which such a situation is possible, or print $-1$ if such a number can be arbitrarily large.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10$) — the number of test cases. Then $t$ test cases follow.
Each test case consists of two lines. The first line contains an even integer $n$ ($4 \le n \le 40$) ($n$ is even). The second line contains $n$ integers $a_1, a_2, \dots a_n$ ($-10^6 \le a_i \le 10^6$).
It is guaranteed that the sum of all $n$ specified in the given test cases does not exceed $100$.
-----Output-----
For each test case output on a separate line an integer $k$ ($k \ge 1$) — the maximum possible number that Polycarp used in operations on the array, or $-1$, if such a number can be arbitrarily large.
-----Examples-----
Input
3
6
1 5 3 1 1 5
8
-1 0 1 -1 0 1 -1 0
4
100 -1000 -1000 -1000
Output
2
1
1100
-----Note-----
None
|
{"inputs": ["1\n4\n0 0 0 4\n", "1\n4\n1 2 3 4\n", "1\n4\n1 1 1 1\n", "1\n4\n-10 -10 -5 -5\n", "1\n4\n1000000 0 0 0\n", "1\n6\n8 128 -38 -39 19 119\n", "1\n4\n572894 617066 -274175 841676\n", "1\n4\n210452 -553585 -303337 -97565\n"], "outputs": ["4\n", "1\n", "-1\n", "5\n", "1000000\n", "1\n", "1\n", "1\n"]}
| 452
| 215
|
coding
|
Solve the programming task below in a Python markdown code block.
In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$.
A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel.
You are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor.
-----Input-----
The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel.
The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer.
Each of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \le x, y \le n$, $x \ne y)$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input.
-----Output-----
Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input.
-----Examples-----
Input
4 2
10 4 10 15
1 2
4 3
Output
0 0 1 2
Input
10 4
5 4 1 5 4 3 7 1 2 5
4 6
2 1
10 8
3 5
Output
5 4 0 5 3 3 9 0 2 5
-----Note-----
In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
|
{"inputs": ["2 0\n3 1\n", "2 0\n1 1\n", "2 0\n3 1\n", "2 0\n1 1\n", "2 0\n1 2\n", "2 0\n3 2\n", "2 0\n3 3\n", "2 0\n1 3\n"], "outputs": ["1 0 \n", "0 0 \n", "1 0 ", "0 0 ", "0 1\n", "1 0\n", "0 0\n", "0 1\n"]}
| 673
| 134
|
coding
|
Solve the programming task below in a Python markdown code block.
There is a beautiful garden of stones in Innopolis.
Its most beautiful place is the $n$ piles with stones numbered from $1$ to $n$.
EJOI participants have visited this place twice.
When they first visited it, the number of stones in piles was $x_1, x_2, \ldots, x_n$, correspondingly. One of the participants wrote down this sequence in a notebook.
They visited it again the following day, and the number of stones in piles was equal to $y_1, y_2, \ldots, y_n$. One of the participants also wrote it down in a notebook.
It is well known that every member of the EJOI jury during the night either sits in the room $108$ or comes to the place with stones. Each jury member who comes there either takes one stone for himself or moves one stone from one pile to another. We can assume that there is an unlimited number of jury members. No one except the jury goes to the place with stones at night.
Participants want to know whether their notes can be correct or they are sure to have made a mistake.
-----Input-----
The first line of the input file contains a single integer $n$, the number of piles with stones in the garden ($1 \leq n \leq 50$).
The second line contains $n$ integers separated by spaces $x_1, x_2, \ldots, x_n$, the number of stones in piles recorded in the notebook when the participants came to the place with stones for the first time ($0 \leq x_i \leq 1000$).
The third line contains $n$ integers separated by spaces $y_1, y_2, \ldots, y_n$, the number of stones in piles recorded in the notebook when the participants came to the place with stones for the second time ($0 \leq y_i \leq 1000$).
-----Output-----
If the records can be consistent output "Yes", otherwise output "No" (quotes for clarity).
-----Examples-----
Input
5
1 2 3 4 5
2 1 4 3 5
Output
Yes
Input
5
1 1 1 1 1
1 0 1 0 1
Output
Yes
Input
3
2 3 9
1 7 9
Output
No
-----Note-----
In the first example, the following could have happened during the night: one of the jury members moved one stone from the second pile to the first pile, and the other jury member moved one stone from the fourth pile to the third pile.
In the second example, the jury took stones from the second and fourth piles.
It can be proved that it is impossible for the jury members to move and took stones to convert the first array into the second array.
|
{"inputs": ["1\n3\n2\n", "1\n1\n5\n", "1\n6\n1\n", "1\n1\n1\n", "1\n0\n0\n", "1\n7\n3\n", "1\n2\n1\n", "1\n0\n0\n"], "outputs": ["Yes\n", "No\n", "Yes\n", "Yes\n", "Yes\n", "Yes\n", "Yes\n", "Yes\n"]}
| 622
| 102
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a metal dial called the "Freedom Trail Ring" and use the dial to spell a specific keyword to open the door.
Given a string ring that represents the code engraved on the outer ring and another string key that represents the keyword that needs to be spelled, return the minimum number of steps to spell all the characters in the keyword.
Initially, the first character of the ring is aligned at the "12:00" direction. You should spell all the characters in key one by one by rotating ring clockwise or anticlockwise to make each character of the string key aligned at the "12:00" direction and then by pressing the center button.
At the stage of rotating the ring to spell the key character key[i]:
You can rotate the ring clockwise or anticlockwise by one place, which counts as one step. The final purpose of the rotation is to align one of ring's characters at the "12:00" direction, where this character must equal key[i].
If the character key[i] has been aligned at the "12:00" direction, press the center button to spell, which also counts as one step. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.
Please complete the following python code precisely:
```python
class Solution:
def findRotateSteps(self, ring: str, key: str) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(ring = \"godding\", key = \"gd\") == 4\n assert candidate(ring = \"godding\", key = \"godding\") == 13\n\n\ncheck(Solution().findRotateSteps)"}
| 334
| 60
|
coding
|
Solve the programming task below in a Python markdown code block.
Vasya wants to climb up a stair of certain amount of steps (Input parameter 1). There are 2 simple rules that he has to stick to.
1. Vasya can climb 1 or 2 steps at each move.
2. Vasya wants the number of moves to be a multiple of a certain integer. (Input parameter 2).
### Task:
What is the `MINIMAL` number of moves making him climb to the top of the stairs that satisfies his conditions?
### Input
1. Number of stairs: `0 < N ≤ 10000` ;
2. Integer to be multiplied : `1 < M ≤ 10 `;
### Output
1. Return a single integer - the minimal number of moves being a multiple of M;
2. If there is no way he can climb satisfying condition return - 1 instead. (`Nothing` in Haskell)
### Examples
```python
numberOfSteps(10, 2) => 6 # Sequence of steps : {2, 2, 2, 2, 1, 1}
numberOfSteps(3, 5) => -1 # !Possible sequences of steps : {2, 1}, {1, 2}, {1, 1, 1}
```
``` haskell
numberOfSteps 10 2 == Just 6 -- Sequence of steps : {2, 2, 2, 2, 1, 1}
numberOfSteps 3 5 == Nothing -- Not possible, since the sequences of steps would be {2, 1}, {1, 2} or {1, 1, 1}
```
Also feel free to reuse/extend the following starter code:
```python
def numberOfSteps(steps, m):
```
|
{"functional": "_inputs = [[10, 2], [3, 5], [29, 7], [2, 2], [1, 2], [10000, 2], [10000, 3], [10000, 10], [9999, 3], [9999, 2], [9999, 10], [9999, 9], [18, 10], [19, 10], [20, 10], [7688, 5], [4608, 5], [3979, 2], [5, 2]]\n_outputs = [[6], [-1], [21], [2], [-1], [5000], [5001], [5000], [5001], [5000], [5000], [5004], [10], [10], [10], [3845], [2305], [1990], [4]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(numberOfSteps(*i), o[0])"}
| 395
| 397
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.