task_type
stringclasses
1 value
problem
stringlengths
261
3.34k
answer
stringlengths
35
6.15k
problem_tokens
int64
62
774
answer_tokens
int64
12
2.04k
coding
Solve the programming task below in a Python markdown code block. You have N items that you want to put them into a knapsack. Item i has value vi and weight wi. You want to find a subset of items to put such that: * The total value of the items is as large as possible. * The items have combined weight at most W, that is capacity of the knapsack. Find the maximum total value of items in the knapsack. Constraints * 1 ≤ N ≤ 40 * 1 ≤ vi ≤ 1015 * 1 ≤ wi ≤ 1015 * 1 ≤ W ≤ 1015 Input N W v1 w1 v2 w2 : vN wN The first line consists of the integers N and W. In the following N lines, the value and weight of the i-th item are given. Output Print the maximum total values of the items in a line. Examples Input 4 5 4 2 5 2 2 1 8 3 Output 13 Input 2 20 5 9 4 10 Output 9
{"inputs": ["2 20\n5 9\n4 0", "2 20\n5 28\n4 8", "2 20\n5 14\n4 8", "2 20\n5 14\n5 8", "2 20\n5 28\n1 8", "2 20\n5 9\n4 10", "2 20\n5 14\n4 10", "2 20\n5 28\n1 10"], "outputs": ["9\n", "4\n", "5\n", "5\n", "1\n", "9", "5\n", "1\n"]}
256
158
coding
Solve the programming task below in a Python markdown code block. Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018. Input The first line of the input contains integer n (1 ≤ n ≤ 1000). Output Output the smallest positive integer with exactly n divisors. Examples Input 4 Output 6 Input 6 Output 12
{"inputs": ["8\n", "7\n", "1\n", "9\n", "2\n", "3\n", "5\n", "6\n"], "outputs": ["24", "64", "1", "36", "2\n", "4\n", "16\n", "12"]}
102
70
coding
Solve the programming task below in a Python markdown code block. Two integer sequences existed initially, one of them was strictly increasing, and another one — strictly decreasing. Strictly increasing sequence is a sequence of integers $[x_1 < x_2 < \dots < x_k]$. And strictly decreasing sequence is a sequence of integers $[y_1 > y_2 > \dots > y_l]$. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing. Elements of increasing sequence were inserted between elements of the decreasing one (and, possibly, before its first element and after its last element) without changing the order. For example, sequences $[1, 3, 4]$ and $[10, 4, 2]$ can produce the following resulting sequences: $[10, \textbf{1}, \textbf{3}, 4, 2, \textbf{4}]$, $[\textbf{1}, \textbf{3}, \textbf{4}, 10, 4, 2]$. The following sequence cannot be the result of these insertions: $[\textbf{1}, 10, \textbf{4}, 4, \textbf{3}, 2]$ because the order of elements in the increasing sequence was changed. Let the obtained sequence be $a$. This sequence $a$ is given in the input. Your task is to find any two suitable initial sequences. One of them should be strictly increasing, and another one — strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing. If there is a contradiction in the input and it is impossible to split the given sequence $a$ into one increasing sequence and one decreasing sequence, print "NO". -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of elements in $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- If there is a contradiction in the input and it is impossible to split the given sequence $a$ into one increasing sequence and one decreasing sequence, print "NO" in the first line. Otherwise print "YES" in the first line. In the second line, print a sequence of $n$ integers $res_1, res_2, \dots, res_n$, where $res_i$ should be either $0$ or $1$ for each $i$ from $1$ to $n$. The $i$-th element of this sequence should be $0$ if the $i$-th element of $a$ belongs to the increasing sequence, and $1$ otherwise. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing. -----Examples----- Input 9 5 1 3 6 8 2 9 0 10 Output YES 1 0 0 0 0 1 0 1 0 Input 5 1 2 4 0 2 Output NO
{"inputs": ["1\n89\n", "1\n34\n", "1\n63\n", "1\n507\n", "1\n783\n", "1\n165\n", "1\n238\n", "1\n1337\n"], "outputs": ["YES\n1\n", "YES\n1\n", "YES\n1\n", "YES\n1\n", "YES\n1\n", "YES\n1\n", "YES\n1\n", "YES\n1 \n"]}
719
117
coding
Solve the programming task below in a Python markdown code block. A binary string is a string consisting only of the characters 0 and 1. You are given a binary string $s$. For some non-empty substring$^\dagger$ $t$ of string $s$ containing $x$ characters 0 and $y$ characters 1, define its cost as: $x \cdot y$, if $x > 0$ and $y > 0$; $x^2$, if $x > 0$ and $y = 0$; $y^2$, if $x = 0$ and $y > 0$. Given a binary string $s$ of length $n$, find the maximum cost across all its non-empty substrings. $^\dagger$ A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. -----Input----- Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line of each test case contains a binary string $s$ of length $n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum cost across all substrings. -----Examples----- Input 6 5 11100 7 1100110 6 011110 7 1001010 4 1000 1 0 Output 9 12 16 12 9 1 -----Note----- In the first test case, we can take a substring $111$. It contains $3$ characters 1 and $0$ characters 0. So $a = 3$, $b = 0$ and its cost is $3^2 = 9$. In the second test case, we can take the whole string. It contains $4$ characters 1 and $3$ characters 0. So $a = 4$, $b = 3$ and its cost is $4 \cdot 3 = 12$. In the third test case, we can can take a substring $1111$ and its cost is $4^2 = 16$. In the fourth test case, we can take the whole string and cost is $4 \cdot 3 = 12$. In the fifth test case, we can take a substring $000$ and its cost is $3 \cdot 3 = 9$. In the sixth test case, we can only take the substring $0$ and its cost is $1 \cdot 1 = 1$.
{"inputs": ["1\n7\n0111111\n", "6\n5\n11100\n7\n1100110\n6\n011110\n7\n1001010\n4\n1000\n1\n0\n"], "outputs": ["36\n", "9\n12\n16\n12\n9\n1\n"]}
692
94
coding
Solve the programming task below in a Python markdown code block. ## Story Before we dive into the exercise, I would like to show you why these numbers are so important in computer programming today. It all goes back to the time of 19th century. Where computers we know today were non-existing. The first ever **computer program** was for the Analytical Engine to compute **Bernoulli numbers**. A woman named Ada Lovelace wrote the very first program. The sad part is the engine was never fully build so her code was never tested. She also predicted the start of **AI** (artificial intelligence). Computers will be able to compose music by themselves, solve problems (not only numbers) ... So in her honor reproduce what was done back in 1842. The Bernoulli numbers are a sequence of rational numbers with deep connections to number theory. The Swiss mathematician Jakob Bernoulli and the Japanese mathematician Seki Kowa discovered the numbers around the same time at the start of the 18th Century. If you want to read more about her or Bernoulli numbers follow these links: https://en.wikipedia.org/wiki/Ada_Lovelace https://en.wikipedia.org/wiki/Bernoulli_number http://mathworld.wolfram.com/BernoulliNumber.html ## Exercise Your job is to write a function `bernoulli_number(n)` which outputs the n-th Bernoulli number. The input will always be a non-negative integer so you do not need to worry about exceptions. How you will solve the problem is none of my business but here are some guidelines. You can make pascal triangle and then with the basic formula generate all Bernoulli numbers. Look example below. For the sake of floating numbers, just use `Fractions` so there will be no problems with rounding. 0 = 1 + 2b1 ............................................................... b1 = - 1/2 0 = 1 + 3b1 + 3b2 ................................................... b2 = 1/6 0 = 1 + 4b1 + 6b2 + 4b3 ....................................... b3 = 0 0 = 1 + 5b1 + 10b2 + 10b3 + 5b4 ...................... b4 = - 1/30 ... and so on. ``` bernoulli_number(0) # return 1 bernoulli_number(1) # return Fraction(-1,2) or Rational(-1,2) or "1/2" bernoulli_number(6) # return Fraction(1,42) or ... bernoulli_number(42) # return Fraction(1520097643918070802691,1806) or ... bernoulli_number(22) # return Fraction(854513,138) or ... "854513/138" ``` ## Note See "Sample Tests" to see the return type for each language. Good luck and happy coding! PS: be careful some numbers might exceed `1000`. If this kata is too hard for you try to solve pascal triangle and something similar to this and then come back :). Also feel free to reuse/extend the following starter code: ```python def bernoulli_number(n): ```
{"functional": "_inputs = [[0], [3], [1337]]\n_outputs = [[1], [0], [0]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(bernoulli_number(*i), o[0])"}
731
172
coding
Solve the programming task below in a Python markdown code block. A sequence a_1,a_2,... ,a_n is said to be /\/\/\/ when the following conditions are satisfied: * For each i = 1,2,..., n-2, a_i = a_{i+2}. * Exactly two different numbers appear in the sequence. You are given a sequence v_1,v_2,...,v_n whose length is even. We would like to make this sequence /\/\/\/ by replacing some of its elements. Find the minimum number of elements that needs to be replaced. Constraints * 2 \leq n \leq 10^5 * n is even. * 1 \leq v_i \leq 10^5 * v_i is an integer. Input Input is given from Standard Input in the following format: n v_1 v_2 ... v_n Output Print the minimum number of elements that needs to be replaced. Examples Input 4 3 1 3 2 Output 1 Input 6 105 119 105 119 105 119 Output 0 Input 4 1 1 1 1 Output 2
{"inputs": ["4\n3 2 3 2", "4\n4 1 3 2", "4\n4 1 3 0", "4\n4 1 3 1", "4\n4 1 6 1", "4\n4 0 6 1", "4\n4 0 9 1", "4\n8 0 9 1"], "outputs": ["0\n", "2\n", "2\n", "1\n", "1\n", "2\n", "2\n", "2\n"]}
273
126
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an array of intervals intervals where intervals[i] = [starti, endi], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Note that intervals which only touch at a point are non-overlapping. For example, [1, 2] and [2, 3] are non-overlapping.   Please complete the following python code precisely: ```python class Solution: def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(intervals = [[1,2],[2,3],[3,4],[1,3]]) == 1\n assert candidate(intervals = [[1,2],[1,2],[1,2]]) == 2\n assert candidate(intervals = [[1,2],[2,3]]) == 0\n\n\ncheck(Solution().eraseOverlapIntervals)"}
125
90
coding
Solve the programming task below in a Python markdown code block. Return a new array consisting of elements which are multiple of their own index in input array (length > 1). Some cases: ``` [22, -6, 32, 82, 9, 25] => [-6, 32, 25] [68, -1, 1, -7, 10, 10] => [-1, 10] [-56,-85,72,-26,-14,76,-27,72,35,-21,-67,87,0,21,59,27,-92,68] => [-85, 72, 0, 68] ``` Also feel free to reuse/extend the following starter code: ```python def multiple_of_index(arr): ```
{"functional": "_inputs = [[[22, -6, 32, 82, 9, 25]], [[68, -1, 1, -7, 10, 10]], [[11, -11]], [[-56, -85, 72, -26, -14, 76, -27, 72, 35, -21, -67, 87, 0, 21, 59, 27, -92, 68]], [[28, 38, -44, -99, -13, -54, 77, -51]], [[-1, -49, -1, 67, 8, -60, 39, 35]]]\n_outputs = [[[-6, 32, 25]], [[-1, 10]], [[-11]], [[-85, 72, 0, 68]], [[38, -44, -99]], [[-49, 8, -60, 35]]]\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(multiple_of_index(*i), o[0])"}
200
404
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given two integer arrays nums and divisors. The divisibility score of divisors[i] is the number of indices j such that nums[j] is divisible by divisors[i]. Return the integer divisors[i] with the maximum divisibility score. If multiple integers have the maximum score, return the smallest one.   Please complete the following python code precisely: ```python class Solution: def maxDivScore(self, nums: List[int], divisors: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [4,7,9,3,9], divisors = [5,2,3]) == 3\n assert candidate(nums = [20,14,21,10], divisors = [5,7,5]) == 5\n assert candidate(nums = [12], divisors = [10,16]) == 10\n\n\ncheck(Solution().maxDivScore)"}
120
106
coding
Solve the programming task below in a Python markdown code block. In some other world, today is Christmas Eve. There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters. He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible. More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}? -----Constraints----- - 2 \leq K < N \leq 10^5 - 1 \leq h_i \leq 10^9 - h_i is an integer. -----Input----- Input is given from Standard Input in the following format: N K h_1 h_2 : h_N -----Output----- Print the minimum possible value of h_{max} - h_{min}. -----Sample Input----- 5 3 10 15 11 14 12 -----Sample Output----- 2 If we decorate the first, third and fifth trees, h_{max} = 12, h_{min} = 10 so h_{max} - h_{min} = 2. This is optimal.
{"inputs": ["3 3\n5\n7\n0\n1\n7", "3 3\n5\n7\n5\n7\n7", "3 3\n5\n7\n5\n1\n7", "3 3\n5\n7\n5\n1\n5", "5 3\n5\n7\n5\n9\n7", "3 3\n5\n7\n5\n7\n6", "5 3\n5\n7\n2\n0\n6", "5 3\n5\n7\n5\n1\n5"], "outputs": ["7\n", "2\n", "2\n", "2\n", "2\n", "2\n", "2\n", "0\n"]}
334
158
coding
Solve the programming task below in a Python markdown code block. Mrs. Smith is trying to contact her husband, John Smith, but she forgot the secret phone number! The only thing Mrs. Smith remembered was that any permutation of $n$ can be a secret phone number. Only those permutations that minimize secret value might be the phone of her husband. The sequence of $n$ integers is called a permutation if it contains all integers from $1$ to $n$ exactly once. The secret value of a phone number is defined as the sum of the length of the longest increasing subsequence (LIS) and length of the longest decreasing subsequence (LDS). A subsequence $a_{i_1}, a_{i_2}, \ldots, a_{i_k}$ where $1\leq i_1 < i_2 < \ldots < i_k\leq n$ is called increasing if $a_{i_1} < a_{i_2} < a_{i_3} < \ldots < a_{i_k}$. If $a_{i_1} > a_{i_2} > a_{i_3} > \ldots > a_{i_k}$, a subsequence is called decreasing. An increasing/decreasing subsequence is called longest if it has maximum length among all increasing/decreasing subsequences. For example, if there is a permutation $[6, 4, 1, 7, 2, 3, 5]$, LIS of this permutation will be $[1, 2, 3, 5]$, so the length of LIS is equal to $4$. LDS can be $[6, 4, 1]$, $[6, 4, 2]$, or $[6, 4, 3]$, so the length of LDS is $3$. Note, the lengths of LIS and LDS can be different. So please help Mrs. Smith to find a permutation that gives a minimum sum of lengths of LIS and LDS. -----Input----- The only line contains one integer $n$ ($1 \le n \le 10^5$) — the length of permutation that you need to build. -----Output----- Print a permutation that gives a minimum sum of lengths of LIS and LDS. If there are multiple answers, print any. -----Examples----- Input 4 Output 3 4 1 2 Input 2 Output 2 1 -----Note----- In the first sample, you can build a permutation $[3, 4, 1, 2]$. LIS is $[3, 4]$ (or $[1, 2]$), so the length of LIS is equal to $2$. LDS can be ony of $[3, 1]$, $[4, 2]$, $[3, 2]$, or $[4, 1]$. The length of LDS is also equal to $2$. The sum is equal to $4$. Note that $[3, 4, 1, 2]$ is not the only permutation that is valid. In the second sample, you can build a permutation $[2, 1]$. LIS is $[1]$ (or $[2]$), so the length of LIS is equal to $1$. LDS is $[2, 1]$, so the length of LDS is equal to $2$. The sum is equal to $3$. Note that permutation $[1, 2]$ is also valid.
{"inputs": ["4\n", "2\n", "1\n", "3\n", "5\n", "6\n", "7\n", "8\n"], "outputs": ["3 4 1 2\n", "2 1\n", "1\n", "3 2 1\n", "4 5 2 3 1\n", "5 6 3 4 1 2\n", "6 7 4 5 2 3 1\n", "7 8 5 6 3 4 1 2\n"]}
744
126
coding
Solve the programming task below in a Python markdown code block. An array is called beautiful if all the elements in the array are equal. You can transform an array using the following steps any number of times: 1. Choose two indices i and j (1 ≤ i,j ≤ n), and an integer x (1 ≤ x ≤ a_i). Let i be the source index and j be the sink index. 2. Decrease the i-th element by x, and increase the j-th element by x. The resulting values at i-th and j-th index are a_i-x and a_j+x respectively. 3. The cost of this operation is x ⋅ |j-i| . 4. Now the i-th index can no longer be the sink and the j-th index can no longer be the source. The total cost of a transformation is the sum of all the costs in step 3. For example, array [0, 2, 3, 3] can be transformed into a beautiful array [2, 2, 2, 2] with total cost 1 ⋅ |1-3| + 1 ⋅ |1-4| = 5. An array is called balanced, if it can be transformed into a beautiful array, and the cost of such transformation is uniquely defined. In other words, the minimum cost of transformation into a beautiful array equals the maximum cost. You are given an array a_1, a_2, …, a_n of length n, consisting of non-negative integers. Your task is to find the number of balanced arrays which are permutations of the given array. Two arrays are considered different, if elements at some position differ. Since the answer can be large, output it modulo 10^9 + 7. Input The first line contains a single integer n (1 ≤ n ≤ 10^5) — the size of the array. The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9). Output Output a single integer — the number of balanced permutations modulo 10^9+7. Examples Input 3 1 2 3 Output 6 Input 4 0 4 0 4 Output 2 Input 5 0 11 12 13 14 Output 120 Note In the first example, [1, 2, 3] is a valid permutation as we can consider the index with value 3 as the source and index with value 1 as the sink. Thus, after conversion we get a beautiful array [2, 2, 2], and the total cost would be 2. We can show that this is the only transformation of this array that leads to a beautiful array. Similarly, we can check for other permutations too. In the second example, [0, 0, 4, 4] and [4, 4, 0, 0] are balanced permutations. In the third example, all permutations are balanced.
{"inputs": ["3\n0 0 3\n", "3\n0 3 3\n", "3\n0 1 2\n", "3\n0 2 3\n", "3\n1 3 3\n", "3\n0 3 1\n", "3\n0 3 2\n", "3\n0 1 1\n"], "outputs": ["3", "3\n", "6\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
659
117
coding
Solve the programming task below in a Python markdown code block. Given is a string S. Let T be the concatenation of K copies of S. We can repeatedly perform the following operation: choose a character in T and replace it with a different character. Find the minimum number of operations required to satisfy the following condition: any two adjacent characters in T are different. Constraints * 1 \leq |S| \leq 100 * S consists of lowercase English letters. * 1 \leq K \leq 10^9 * K is an integer. Input Input is given from Standard Input in the following format: S K Output Print the minimum number of operations required. Examples Input issii 2 Output 4 Input qq 81 Output 81 Input cooooooooonteeeeeeeeeest 999993333 Output 8999939997
{"inputs": ["oo\n4", "oo\n7", "rq\n81", "oo\n13", "qq\n81", "rr\n552", "rr\n826", "rr\n757"], "outputs": ["4\n", "7\n", "0\n", "13\n", "81", "552\n", "826\n", "757\n"]}
208
94
coding
Solve the programming task below in a Python markdown code block. You are asked to ensure that the first and last names of people begin with a capital letter in their passports. For example, alison heck should be capitalised correctly as Alison Heck. $\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\$ Given a full name, your task is to capitalize the name appropriately. Input Format A single line of input containing the full name, $\mbox{S}$. Constraints $0<len(S)<1000$ The string consists of alphanumeric characters and spaces. Note: in a word only the first character is capitalized. Example 12abc when capitalized remains 12abc. Output Format Print the capitalized string, $\mbox{S}$. Sample Input chris alan Sample Output Chris Alan
{"inputs": ["chris alan\n"], "outputs": ["Chris Alan\n"]}
641
17
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given the head of a singly linked list, reverse the list, and return the reversed list.   Please complete the following python code precisely: ```python # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: ```
{"functional": "def check(candidate):\n assert is_same_list(candidate(head = list_node([1,2,3,4,5])), list_node([5,4,3,2,1]))\n assert is_same_list(candidate(head = list_node([1,2])), list_node([2,1]))\n assert is_same_list(candidate(head = list_node([])), list_node([]))\n\n\ncheck(Solution().reverseList)"}
110
97
coding
Solve the programming task below in a Python markdown code block. You've got an array, consisting of n integers a_1, a_2, ..., a_{n}. Also, you've got m queries, the i-th query is described by two integers l_{i}, r_{i}. Numbers l_{i}, r_{i} define a subsegment of the original array, that is, the sequence of numbers a_{l}_{i}, a_{l}_{i} + 1, a_{l}_{i} + 2, ..., a_{r}_{i}. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b_1, b_2, ..., b_{k}, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1 ≤ x ≤ k), that the following inequation fulfills: b_1 ≤ b_2 ≤ ... ≤ b_{x} ≥ b_{x} + 1 ≥ b_{x} + 2... ≥ b_{k}. Note that the non-decreasing and the non-increasing sequences are also considered ladders. -----Input----- The first line contains two integers n and m (1 ≤ n, m ≤ 10^5) — the number of array elements and the number of queries. The second line contains the sequence of integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9), where number a_{i} stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces. -----Output----- Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise. -----Examples----- Input 8 6 1 2 1 3 3 5 2 1 1 3 2 3 2 4 8 8 1 4 5 8 Output Yes Yes No Yes No Yes
{"inputs": ["1 1\n6\n1 1\n", "1 1\n6\n1 1\n", "1 1\n7\n1 1\n", "1 1\n5\n1 1\n", "1 1\n2\n1 1\n", "1 1\n3\n1 1\n", "2 5\n1 1\n1 2\n2 2\n2 2\n1 2\n1 2\n", "2 5\n1 1\n1 2\n2 2\n2 2\n1 2\n1 2\n"], "outputs": ["Yes\n", "Yes\n", "Yes\n", "Yes\n", "Yes\n", "Yes\n", "Yes\nYes\nYes\nYes\nYes\n", "Yes\nYes\nYes\nYes\nYes\n"]}
518
186
coding
Solve the programming task below in a Python markdown code block. Little Egor is a huge movie fan. He likes watching different kinds of movies: from drama movies to comedy movies, from teen movies to horror movies. He is planning to visit cinema this weekend, but he's not sure which movie he should watch. There are n movies to watch during this weekend. Each movie can be characterized by two integers Li and Ri, denoting the length and the rating of the corresponding movie. Egor wants to watch exactly one movie with the maximal value of Li × Ri. If there are several such movies, he would pick a one with the maximal Ri among them. If there is still a tie, he would pick the one with the minimal index among them. Your task is to help Egor to pick a movie to watch during this weekend. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The first line of the test case description contains an integer n. The second line of the test case description contains n integers L1, L2, ...,Ln. The following line contains n integers R1, R2, ..., Rn. -----Output----- For each test case, output a single integer i denoting the index of the movie that Egor should watch during this weekend. Note that we follow 1-based indexing. -----Constraints----- - 1 ≤ T ≤ 5 - 1 ≤ n ≤ 100 - 1 ≤ Li, Ri ≤ 100 -----Example----- Input: 2 2 1 2 2 1 4 2 1 4 1 2 4 1 4 Output: 1 2 -----Explanation----- In the first example case, both films have the same value of L × R, but the first film has a better rating. In the second example case, the second and the fourth movies are equally good, but the second movie has a smaller index.
{"inputs": ["2\n2\n1 2\n2 1\n4\n2 1 4 1\n2 4 1 4"], "outputs": ["1\n2"]}
409
42
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. An array A is larger than some array B if for the first index i where A[i] != B[i], A[i] > B[i]. For example, consider 0-indexing: [1,3,2,4] > [1,2,2,4], since at index 1, 3 > 2. [1,4,4,4] < [2,1,1,1], since at index 0, 1 < 2. A subarray is a contiguous subsequence of the array. Given an integer array nums of distinct integers, return the largest subarray of nums of length k.   Please complete the following python code precisely: ```python class Solution: def largestSubarray(self, nums: List[int], k: int) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(nums = [1,4,5,2,3], k = 3) == [5,2,3]\n assert candidate(nums = [1,4,5,2,3], k = 4) == [4,5,2,3]\n assert candidate(nums = [1,4,5,2,3], k = 1) == [5]\n\n\ncheck(Solution().largestSubarray)"}
187
109
coding
Solve the programming task below in a Python markdown code block. Dreamoon loves summing up something for no reason. One day he obtains two integers a and b occasionally. He wants to calculate the sum of all nice integers. Positive integer x is called nice if $\operatorname{mod}(x, b) \neq 0$ and $\frac{\operatorname{div}(x, b)}{\operatorname{mod}(x, b)} = k$, where k is some integer number in range [1, a]. By $\operatorname{div}(x, y)$ we denote the quotient of integer division of x and y. By $\operatorname{mod}(x, y)$ we denote the remainder of integer division of x and y. You can read more about these operations here: http://goo.gl/AcsXhT. The answer may be large, so please print its remainder modulo 1 000 000 007 (10^9 + 7). Can you compute it faster than Dreamoon? -----Input----- The single line of the input contains two integers a, b (1 ≤ a, b ≤ 10^7). -----Output----- Print a single integer representing the answer modulo 1 000 000 007 (10^9 + 7). -----Examples----- Input 1 1 Output 0 Input 2 2 Output 8 -----Note----- For the first sample, there are no nice integers because $\operatorname{mod}(x, 1)$ is always zero. For the second sample, the set of nice integers is {3, 5}.
{"inputs": ["1 1\n", "2 2\n", "4 1\n", "4 2\n", "4 3\n", "4 4\n", "3 4\n", "2 4\n"], "outputs": ["0\n", "8\n", "0\n", "24\n", "102\n", "264\n", "162\n", "84\n"]}
348
94
coding
Solve the programming task below in a Python markdown code block. There are n wizards. They are numbered from 1 to n, and the i-th wizard has the magical power ri (1 ≤ i ≤ n). Now they are confronting a powerful wizard, whose enemy's magical power is S. n Wizards are good at fighting together, especially two people. When two wizards cooperate, the magical power is simply the sum, and you can use powerful magic to beat enemies that are strong to some extent. Your job is to output the total number of wizard pairs (i, j) (i ≠ j and 1 ≤ i ≤ n and 1 ≤ j ≤ n) that can win against enemies with magical power S. However, (i, j) and (j, i) are counted as the same pair. When one magical power is greater than the other, the side with the greater magical power wins. If they are equal, it does not mean that they have won by trade-off. Constraints * All inputs are integers * 1 ≤ n ≤ 20,000 * 1 ≤ ri ≤ 100 (1 ≤ i ≤ n) * 1 ≤ S ≤ 100 * The number of test cases does not exceed 100. Input The input consists of multiple test cases. Each test case follows the format below. n S r1 r2 ... rn The meaning of each variable is as in the problem statement. The end of the input is indicated by a line where two 0s are separated by a single space. Output Output the total number of pairs (i, j) (i ≠ j) that satisfy the conditions on one line for each case. Example Input 3 7 1 3 10 0 0 Output 2
{"inputs": ["3 7\n1\n0\n5\n0 0", "3 7\n1\n3\n9\n0 0", "3 7\n1\n0\n9\n0 0", "3 7\n5\n4\n2\n0 0", "3 7\n4\n4\n2\n0 0", "3 7\n4\n4\n1\n0 0", "3 7\n0\n3\n3\n0 0", "3 5\n1\n0\n9\n0 0"], "outputs": ["0\n", "2\n", "2\n", "1\n", "1\n", "1\n", "0\n", "2\n"]}
381
158
coding
Solve the programming task below in a Python markdown code block. You have N bamboos. The lengths (in centimeters) of these are l_1, l_2, ..., l_N, respectively. Your objective is to use some of these bamboos (possibly all) to obtain three bamboos of length A, B, C. For that, you can use the following three kinds of magics any number: - Extension Magic: Consumes 1 MP (magic point). Choose one bamboo and increase its length by 1. - Shortening Magic: Consumes 1 MP. Choose one bamboo of length at least 2 and decrease its length by 1. - Composition Magic: Consumes 10 MP. Choose two bamboos and combine them into one bamboo. The length of this new bamboo is equal to the sum of the lengths of the two bamboos combined. (Afterwards, further magics can be used on this bamboo.) At least how much MP is needed to achieve the objective? -----Constraints----- - 3 \leq N \leq 8 - 1 \leq C < B < A \leq 1000 - 1 \leq l_i \leq 1000 - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: N A B C l_1 l_2 : l_N -----Output----- Print the minimum amount of MP needed to achieve the objective. -----Sample Input----- 5 100 90 80 98 40 30 21 80 -----Sample Output----- 23 We are obtaining three bamboos of lengths 100, 90, 80 from five bamboos 98, 40, 30, 21, 80. We already have a bamboo of length 80, and we can obtain bamboos of lengths 100, 90 by using the magics as follows at the total cost of 23 MP, which is optimal. - Use Extension Magic twice on the bamboo of length 98 to obtain a bamboo of length 100. (MP consumed: 2) - Use Composition Magic on the bamboos of lengths 40, 30 to obtain a bamboo of length 70. (MP consumed: 10) - Use Shortening Magic once on the bamboo of length 21 to obtain a bamboo of length 20. (MP consumed: 1) - Use Composition Magic on the bamboo of length 70 obtained in step 2 and the bamboo of length 20 obtained in step 3 to obtain a bamboo of length 90. (MP consumed: 10)
{"inputs": ["3 3 2 1\n1\n2\n3\n", "4 1000 2 1\n663\n663\n2\n1\n", "5 100 20 80\n79\n50\n9\n9\n80", "5 100 20 80\n79\n50\n9\n9\n157", "5 100 12 80\n80\n40\n42\n21\n62", "5 100 12 22\n80\n40\n42\n21\n62", "5 100 171 80\n68\n40\n9\n21\n80", "5 100 12 22\n80\n41\n42\n21\n62"], "outputs": ["0\n", "336\n", "32\n", "62\n", "21\n", "41\n", "152\n", "42\n"]}
598
253
coding
Solve the programming task below in a Python markdown code block. ```if-not:sql Implement a function that receives two IPv4 addresses, and returns the number of addresses between them (including the first one, excluding the last one). ``` ```if:sql Given a database of first and last IPv4 addresses, calculate the number of addresses between them (including the first one, excluding the last one). ## Input ~~~ --------------------------------- | Table | Column | Type | |--------------+--------+-------| | ip_addresses | id | int | | | first | text | | | last | text | --------------------------------- ~~~ ## Output ~~~ ---------------------- | Column | Type | |-------------+------| | id | int | | ips_between | int | ---------------------- ~~~ ``` All inputs will be valid IPv4 addresses in the form of strings. The last address will always be greater than the first one. ___ ## Examples ```python ips_between("10.0.0.0", "10.0.0.50") == 50 ips_between("10.0.0.0", "10.0.1.0") == 256 ips_between("20.0.0.10", "20.0.1.0") == 246 ``` Also feel free to reuse/extend the following starter code: ```python def ips_between(start, end): ```
{"functional": "_inputs = [['150.0.0.0', '150.0.0.1'], ['10.0.0.0', '10.0.0.50'], ['20.0.0.10', '20.0.1.0'], ['10.11.12.13', '10.11.13.0'], ['160.0.0.0', '160.0.1.0'], ['170.0.0.0', '170.1.0.0'], ['50.0.0.0', '50.1.1.1'], ['180.0.0.0', '181.0.0.0'], ['1.2.3.4', '5.6.7.8']]\n_outputs = [[1], [50], [246], [243], [256], [65536], [65793], [16777216], [67372036]]\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(ips_between(*i), o[0])"}
328
398
coding
Solve the programming task below in a Python markdown code block. Create a program that will take in a string as input and, if there are duplicates of more than two alphabetical characters in the string, returns the string with all the extra characters in a bracket. For example, the input "aaaabbcdefffffffg" should return "aa[aa]bbcdeff[fffff]g" Please also ensure that the input is a string, and return "Please enter a valid string" if it is not. Also feel free to reuse/extend the following starter code: ```python def string_parse(string): ```
{"functional": "_inputs = [['aaaabbcdefffffffg'], [3], ['boopdedoop'], ['helloookat'], [True], [''], ['aAAabbcdeffFfFffg'], ['aAAabbcdeFFFffffg'], [{}], [[5.3]]]\n_outputs = [['aa[aa]bbcdeff[fffff]g'], ['Please enter a valid string'], ['boopdedoop'], ['helloo[o]kat'], ['Please enter a valid string'], [''], ['aAAabbcdeffFfFffg'], ['aAAabbcdeFF[F]ff[ff]g'], ['Please enter a valid string'], ['Please enter a valid string']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(string_parse(*i), o[0])"}
128
291
coding
Solve the programming task below in a Python markdown code block. ------Read problems statements in Mandarin chinese , Russian and Vietnamese as well. ------ Tumut, the best programmer in his village Applidz, invented a problem and decided to share it with you: You are given two integer sequences $S_{1}, S_{2}, \dots, S_{N}$ and $T_{1}, T_{2}, \dots, T_{M}$ and an integer $x$. You are allowed to perform the following operation any number of times: choose an element of $S$ and an element of $T$ (let's denote them by $S_{i}$ and $T_{j}$ respectively) decrease both $S_{i}$ and $T_{j}$ by $x$, i.e. replace $S_{i}$ by $S_{i}-x$ and $T_{j}$ by $T_{j}-x$ Let's denote the minimum and maximum value in the sequence $S$ after performing the chosen operations (possibly none) by $minS$ and $maxS$ respectively. Similarly, let's denote the minimum and maximum value in $T$ after performing the chosen operations by $minT$ and $maxT$ respectively. The goal is minimizing the expression $(maxS+maxT) - (minS+minT)$. Compute the minimum value of this expression. ------ Input ------ The first line of the input contains three space-separated integers $N$, $M$ and $x$. The second line contains $N$ space-separated integers $S_{1}, S_{2} \dots S_{N}$. The third line contains $M$ space-separated integers $T_{1}, T_{2} \dots T_{M}$. ------ Output ------ Print a single line containing one integer — the minimum possible value of the expression $(maxS+maxT) - (minS+minT)$. ------ Constraints ------ $1 ≤ N, M ≤ 5\cdot 10^{5}$ $1 ≤ S_{i} ≤ 10^{9}$ for each valid $i$ $1 ≤ T_{i} ≤ 10^{9}$ for each valid $i$ $1 ≤ x ≤ 10^{9}$ ------ Subtasks ------ Subtask #1 (20 points): $N, M ≤ 20$ $S_{i} ≤ 20$ for each valid $i$ $T_{i} ≤ 20$ for each valid $i$ Subtask #2 (30 points): $N, M ≤ 1,000$ $S_{i} ≤ 1,000$ for each valid $i$ $T_{i} ≤ 1,000$ for each valid $i$ Subtask #3 (50 points): original constraints ----- Sample Input 1 ------ 2 2 3 1 8 2 3 ----- Sample Output 1 ------ 2 ----- explanation 1 ------ We can perform these two operations: 1. decrease $S_{2}$ and $T_{1}$ by $x$ 2. decrease $S_{2}$ and $T_{2}$ by $x$ Afterwards, the sequence $S$ will be $[1, 2]$ and the sequence $T$ will be $[-1, 0]$. The resulting value of the given expression is $(2+0)-(1+(-1)) = 2$. It is impossible to obtain a smaller value no matter how many operations are performed.
{"inputs": ["2 2 3\n1 8\n2 3"], "outputs": ["2"]}
759
24
coding
Solve the programming task below in a Python markdown code block. Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division). After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$: function add(A, B): while B is greater than 0: U = A XOR B V = A AND B A = U B = V * 2 return A Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated. -----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 string $A$. - The second line contains a single string $B$. -----Output----- For each test case, print a single line containing one integer ― the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le |A|, |B| \le 10^5$ - $A$ and $B$ contain only characters '0' and '1' - the sum of $|A| + |B|$ over all test cases does not exceed $10^6$ -----Subtasks----- Subtask #1 (20 points): $|A|, |B| \le 30$ Subtask #2 (30 points): - $|A|, |B| \le 500$ - the sum of $|A| + |B|$ over all test cases does not exceed $10^5$ Subtask #3 (50 points): original constraints -----Example Input----- 3 100010 0 0 100010 11100 1010 -----Example Output----- 0 1 3 -----Explanation----- Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all. Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have: - $U = 34$ - $V = 0$ - $A$ changes to $34$ - $B$ changes to $2 \cdot 0 = 0$ The while-loop terminates immediately afterwards, so it is executed only once. Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$.
{"inputs": ["3\n100010\n0\n0\n100010\n11100\n1010"], "outputs": ["0\n1\n3"]}
732
45
coding
Solve the programming task below in a Python markdown code block. My friend John likes to go to the cinema. He can choose between system A and system B. ``` System A : he buys a ticket (15 dollars) every time System B : he buys a card (500 dollars) and a first ticket for 0.90 times the ticket price, then for each additional ticket he pays 0.90 times the price paid for the previous ticket. ``` #Example: If John goes to the cinema 3 times: ``` System A : 15 * 3 = 45 System B : 500 + 15 * 0.90 + (15 * 0.90) * 0.90 + (15 * 0.90 * 0.90) * 0.90 ( = 536.5849999999999, no rounding for each ticket) ``` John wants to know how many times he must go to the cinema so that the *final result* of System B, when rounded *up* to the next dollar, will be cheaper than System A. The function `movie` has 3 parameters: `card` (price of the card), `ticket` (normal price of a ticket), `perc` (fraction of what he paid for the previous ticket) and returns the first `n` such that ``` ceil(price of System B) < price of System A. ``` More examples: ``` movie(500, 15, 0.9) should return 43 (with card the total price is 634, with tickets 645) movie(100, 10, 0.95) should return 24 (with card the total price is 235, with tickets 240) ``` Also feel free to reuse/extend the following starter code: ```python def movie(card, ticket, perc): ```
{"functional": "_inputs = [[500, 15, 0.9], [100, 10, 0.95], [0, 10, 0.95], [250, 20, 0.9], [500, 20, 0.9], [2500, 20, 0.9]]\n_outputs = [[43], [24], [2], [21], [34], [135]]\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(movie(*i), o[0])"}
443
256
coding
Solve the programming task below in a Python markdown code block. Read problems statements in [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. You have three shoes of the same size lying around. Each shoe is either a left shoe (represented using 0) or a right shoe (represented using 1). Given A, B, C, representing the information for each shoe, find out whether you can go out now, wearing one left shoe and one right shoe. ------ Input Format ------ - 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, three integers A, B, C. ------ Output Format ------ For each test case, output in a single line the answer: 1 if it's possible to go out with a pair of shoes and 0 if not. ------ Constraints ------ $1 ≤ T ≤ 8$ $0 ≤ A, B, C ≤ 1$ ----- Sample Input 1 ------ 3 0 0 0 0 1 1 1 0 1 ----- Sample Output 1 ------ 0 1 1 ----- explanation 1 ------ Test Case $1$: Since there's no right shoe, it's not possible to go out with a pair of shoes. Test Case $2$: It's possible to go out with a pair of shoes wearing the first and last shoes. Test Case $3$: It's possible to go out with a pair of shoes wearing the first and second shoes.
{"inputs": ["3\n0 0 0\n0 1 1\n1 0 1"], "outputs": ["0\n1\n1"]}
330
34
coding
Solve the programming task below in a Python markdown code block. You are going out for a walk, when you suddenly encounter a monster. Fortunately, you have N katana (swords), Katana 1, Katana 2, …, Katana N, and can perform the following two kinds of attacks in any order: - Wield one of the katana you have. When you wield Katana i (1 ≤ i ≤ N), the monster receives a_i points of damage. The same katana can be wielded any number of times. - Throw one of the katana you have. When you throw Katana i (1 ≤ i ≤ N) at the monster, it receives b_i points of damage, and you lose the katana. That is, you can no longer wield or throw that katana. The monster will vanish when the total damage it has received is H points or more. At least how many attacks do you need in order to vanish it in total? -----Constraints----- - 1 ≤ N ≤ 10^5 - 1 ≤ H ≤ 10^9 - 1 ≤ a_i ≤ b_i ≤ 10^9 - All input values are integers. -----Input----- Input is given from Standard Input in the following format: N H a_1 b_1 : a_N b_N -----Output----- Print the minimum total number of attacks required to vanish the monster. -----Sample Input----- 1 10 3 5 -----Sample Output----- 3 You have one katana. Wielding it deals 3 points of damage, and throwing it deals 5 points of damage. By wielding it twice and then throwing it, you will deal 3 + 3 + 5 = 11 points of damage in a total of three attacks, vanishing the monster.
{"inputs": ["1 10\n4 5", "1 10\n3 5", "1 10\n3 5\n", "2 10\n2 5\n2 6", "2 19\n2 5\n2 6", "2 19\n4 5\n2 6", "2 19\n4 5\n2 4", "2 2\n1 5\n2 10"], "outputs": ["3\n", "3", "3\n", "2\n", "6\n", "4\n", "5\n", "1\n"]}
383
138
coding
Solve the programming task below in a Python markdown code block. Three numbers A, B and C are the inputs. Write a program to find second largest among them. -----Input----- The first line contains an integer T, the total number of testcases. Then T lines follow, each line contains three integers A, B and C. -----Output----- For each test case, display the second largest among A, B and C, in a new line. -----Constraints----- - 1 ≤ T ≤ 1000 - 1 ≤ A,B,C ≤ 1000000 -----Example----- Input 3 120 11 400 10213 312 10 10 3 450 Output 120 312 10
{"inputs": ["3 \n0 7 27\n11308 32 23\n0 1 39", "3 \n0 5 766\n5757 312 12\n6 0 40", "3 \n0 5 766\n5757 317 12\n6 0 40", "3 \n0 7 766\n5757 317 12\n6 0 40", "3 \n0 7 766\n5757 596 16\n6 0 40", "3 \n1 5 766\n5352 312 19\n1 0 40", "3 \n9 10 68\n10213 56 13\n10 0 3", "3 \n0 7 27\n11308 317 23\n0 1 39"], "outputs": ["7\n32\n1\n", "5\n312\n6\n", "5\n317\n6\n", "7\n317\n6\n", "7\n596\n6\n", "5\n312\n1\n", "10\n56\n3\n", "7\n317\n1\n"]}
177
332
coding
Solve the programming task below in a Python markdown code block. Those days, many boys use beautiful girls' photos as avatars in forums. So it is pretty hard to tell the gender of a user at the first glance. Last year, our hero went to a forum and had a nice chat with a beauty (he thought so). After that they talked very often and eventually they became a couple in the network. But yesterday, he came to see "her" in the real world and found out "she" is actually a very strong man! Our hero is very sad and he is too tired to love again now. So he came up with a way to recognize users' genders by their user names. This is his method: if the number of distinct characters in one's user name is odd, then he is a male, otherwise she is a female. You are given the string that denotes the user name, please help our hero to determine the gender of this user by his method. -----Input----- The first line contains a non-empty string, that contains only lowercase English letters — the user name. This string contains at most 100 letters. -----Output----- If it is a female by our hero's method, print "CHAT WITH HER!" (without the quotes), otherwise, print "IGNORE HIM!" (without the quotes). -----Examples----- Input wjmzbmr Output CHAT WITH HER! Input xiaodao Output IGNORE HIM! Input sevenkplus Output CHAT WITH HER! -----Note----- For the first example. There are 6 distinct characters in "wjmzbmr". These characters are: "w", "j", "m", "z", "b", "r". So wjmzbmr is a female and you should print "CHAT WITH HER!".
{"inputs": ["oh\n", "oh\n", "nh\n", "rhh\n", "rhh\n", "hhr\n", "pezu\n", "kmsk\n"], "outputs": ["CHAT WITH HER!\n", "CHAT WITH HER!\n", "CHAT WITH HER!\n", "CHAT WITH HER!\n", "CHAT WITH HER!\n", "CHAT WITH HER!\n", "CHAT WITH HER!\n", "IGNORE HIM!\n"]}
371
99
coding
Solve the programming task below in a Python markdown code block. Here your task is to Create a (nice) Christmas Tree. You don't have to check errors or incorrect input values, every thing is ok without bad tricks, only one int parameter as input and a string to return;-)... So what to do?First three easy examples: ```` Input: 3 and Output: * *** ***** ### Input 9 and Output: * *** ***** *** ***** ******* ***** ******* ********* ### Input 17 and Output: * *** ***** *** ***** ******* ***** ******* ********* ******* ********* *********** ********* *********** ************* ### Really nice trees, or what???! So merry Christmas;-) ```` You can see, always a root, always steps of hight 3, tree never smaller than 3 (return "") and no difference for input values like 15 or 17 (because (int) 15/3 = (int) 17/3). That's valid for every input and every tree. I think there's nothing more to say - perhaps look at the testcases too;-)! There are some static tests at the beginning and many random tests if you submit your solution.  Hope you have fun:-)! Also feel free to reuse/extend the following starter code: ```python def christmas_tree(height): ```
{"functional": "_inputs = [[5], [10], [8], [2]]\n_outputs = [[' *\\r\\n ***\\r\\n*****\\r\\n ###'], [' *\\r\\n ***\\r\\n *****\\r\\n ***\\r\\n *****\\r\\n *******\\r\\n *****\\r\\n *******\\r\\n*********\\r\\n ###'], [' *\\r\\n ***\\r\\n *****\\r\\n ***\\r\\n *****\\r\\n*******\\r\\n ###'], ['']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(christmas_tree(*i), o[0])"}
314
279
coding
Solve the programming task below in a Python markdown code block. What adds up =========== Given three arrays of integers your task is to create an algorithm that finds the numbers in the first two arrays whose sum is equal to any number in the third. The return value should be an array containing the values from the argument arrays that adds up. The sort order of the resulting array is not important. If no combination of numbers adds up return a empty array. ### Example A small example: Given the three input arrays `a1 = [1, 2]; a2 = [4,3]; a3 = [6,5,8]`, we need to find the number pairs from `a1` and `a2` that sum up to a number in `a3` and return those three numbers in an array. In this example, the result from the function would be `[[1, 4, 5] , [2, 4, 6], [2, 3, 5]]`. ``` Given three arrays a1 a2 a3 1 4 6 (a1 a2 a3) (a1 a2 a3) (a1 a2 a3) 2 3 5 => [[1, 4, 5] , [2, 4, 6], [2, 3, 5]] 8 each value in the result array contains one part from each of the arguments. ``` ### Testing A function `compare_array` is given. This function takes two arrays and compares them invariant of sort order. ```python test.expect(compare_arrays(addsup([1,2], [3,1], [5,4]), [[1,3,4], [2,3,5]])) ``` ### Greater goal For extra honor try and make it as effective as possible. Discuss whats the most effective way of doing this. The fastest way i can do this is in *O(n^2)*. Can you do it quicker? Also feel free to reuse/extend the following starter code: ```python def addsup(a1, a2, a3): ```
{"functional": "_inputs = [[[], [1, 2, 3], [5, 2, 3]], [[1, 3, 4], [], [4, 6, 5]], [[1, 2, 3], [4, 5, 6], []]]\n_outputs = [[[]], [[]], [[]]]\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(addsup(*i), o[0])"}
466
214
coding
Solve the programming task below in a Python markdown code block. Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white. Consider a set consisting of k (0 ≤ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices. Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (10^9 + 7). -----Input----- The first line contains an integer n (2 ≤ n ≤ 10^5) — the number of tree vertices. The second line contains the description of the tree: n - 1 integers p_0, p_1, ..., p_{n} - 2 (0 ≤ p_{i} ≤ i). Where p_{i} means that there is an edge connecting vertex (i + 1) of the tree and vertex p_{i}. Consider tree vertices are numbered from 0 to n - 1. The third line contains the description of the colors of the vertices: n integers x_0, x_1, ..., x_{n} - 1 (x_{i} is either 0 or 1). If x_{i} is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white. -----Output----- Output a single integer — the number of ways to split the tree modulo 1000000007 (10^9 + 7). -----Examples----- Input 3 0 0 0 1 1 Output 2 Input 6 0 1 1 0 4 1 1 0 0 1 0 Output 1 Input 10 0 1 2 1 4 4 4 0 8 0 0 0 1 0 1 1 0 0 1 Output 27
{"inputs": ["2\n0\n1 0\n", "2\n0\n1 0\n", "3\n0 0\n0 1 1\n", "3\n0 0\n1 1 1\n", "3\n0 1\n0 1 1\n", "3\n0 0\n0 1 1\n", "5\n0 1 1 3\n0 0 0 1 1\n", "5\n0 1 1 3\n0 0 0 1 1\n"], "outputs": ["1\n", "1", "2\n", "1\n", "1\n", "2", "1\n", "1"]}
467
155
coding
Solve the programming task below in a Python markdown code block. Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. -----Input----- The first line of the input contains one integer n (1 ≤ n ≤ 100) — the length of the sequence. The second line contains the sequence consisting of n characters U and R. -----Output----- Print the minimum possible length of the sequence of moves after all replacements are done. -----Examples----- Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 -----Note----- In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
{"inputs": ["1\nR\n", "1\nU\n", "1\nR\n", "1\nU\n", "2\nUR\n", "2\nUR\n", "2\nRU\n", "3\nRUR\n"], "outputs": ["1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "2\n"]}
295
87
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an array nums of n integers where nums[i] is in the range [1, n], return an array of all the integers in the range [1, n] that do not appear in nums.   Please complete the following python code precisely: ```python class Solution: def findDisappearedNumbers(self, nums: List[int]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(nums = [4,3,2,7,8,2,3,1]) == [5,6]\n assert candidate(nums = [1,1]) == [2]\n\n\ncheck(Solution().findDisappearedNumbers)"}
93
65
coding
Solve the programming task below in a Python markdown code block. Chef has a sequence of N$N$ integers A1,A2,...,AN$A_1, A_2, ..., A_N$. Chef thinks that a triplet of integers (i,j,k)$(i,j,k)$ is good if 1≤i<j<k≤N$1 \leq i < j < k \leq N$ and P$P$ in the following expression contains an odd number of ones in its binary representation: P=[Ai<<(⌊log2(Aj)⌋+⌊log2(Ak)⌋+2)]+[Aj<<(⌊log2(Ak)⌋+1)]+Ak$P = [ A_i<< ( \lfloor \log_2(A_j) \rfloor + \lfloor \log_2(A_k) \rfloor + 2 ) ] + [A_j << ( \lfloor \log_2(A_k) \rfloor + 1) ] + A_k$ The <<$<<$ operator is called left shift, x<<y$x << y$ is defined as x⋅2y$x \cdot 2^y$. Help the Chef finding the total number of good triplets modulo 109+7$10^9 + 7$. -----Input:----- - The first line of the input contains a single integer T$T$ denoting the number of test cases. The description of T$T$ test cases follows. - The first line of each test case contains a single integer N$N$. - The second line of each test case contains N$N$ space-separated integers A1,A2,...,AN$A_1, A_2, ..., A_N$. -----Output:----- For each test case, print a single line containing one integer, the number of good triplets modulo 109+7$10^9+7$. -----Constraints:----- - 1≤T≤103$1 \leq T \leq 10^3$ - 1≤N≤105$1\leq N \leq 10^5$ - 1≤Ai≤109$1 \leq A_i \leq 10^9$ - The sum of N$N$ over all testcases is less than 106$10^6$ -----Sample Input:----- 1 4 1 1 2 3 -----Sample Output:----- 1
{"inputs": ["1\n4\n1 1 2 3"], "outputs": ["1"]}
532
22
coding
Solve the programming task below in a Python markdown code block. My little sister came back home from school with the following task: given a squared sheet of paper she has to cut it in pieces which, when assembled, give squares the sides of which form an increasing sequence of numbers. At the beginning it was lot of fun but little by little we were tired of seeing the pile of torn paper. So we decided to write a program that could help us and protects trees. ## Task Given a positive integral number n, return a **strictly increasing** sequence (list/array/string depending on the language) of numbers, so that the sum of the squares is equal to n². If there are multiple solutions (and there will be), return as far as possible the result with the largest possible values: ## Examples `decompose(11)` must return `[1,2,4,10]`. Note that there are actually two ways to decompose 11², 11² = 121 = 1 + 4 + 16 + 100 = 1² + 2² + 4² + 10² but don't return `[2,6,9]`, since 9 is smaller than 10. For `decompose(50)` don't return `[1, 1, 4, 9, 49]` but `[1, 3, 5, 8, 49]` since `[1, 1, 4, 9, 49]` doesn't form a strictly increasing sequence. ## Note Neither `[n]` nor `[1,1,1,…,1]` are valid solutions. If no valid solution exists, return `nil`, `null`, `Nothing`, `None` (depending on the language) or `"[]"` (C) ,`{}` (C++), `[]` (Swift, Go). The function "decompose" will take a positive integer n and return the decomposition of N = n² as: - [x1 ... xk] or - "x1 ... xk" or - Just [x1 ... xk] or - Some [x1 ... xk] or - {x1 ... xk} or - "[x1,x2, ... ,xk]" depending on the language (see "Sample tests") # Note for Bash ``` decompose 50 returns "1,3,5,8,49" decompose 4 returns "Nothing" ``` # Hint Very often `xk` will be `n-1`. Also feel free to reuse/extend the following starter code: ```python def decompose(n): ```
{"functional": "_inputs = [[12], [6], [50], [44], [625], [5], [7100], [123456], [1234567], [7654321], [4], [7654322]]\n_outputs = [[[1, 2, 3, 7, 9]], [None], [[1, 3, 5, 8, 49]], [[2, 3, 5, 7, 43]], [[2, 5, 8, 34, 624]], [[3, 4]], [[2, 3, 5, 119, 7099]], [[1, 2, 7, 29, 496, 123455]], [[2, 8, 32, 1571, 1234566]], [[6, 10, 69, 3912, 7654320]], [None], [[1, 4, 11, 69, 3912, 7654321]]]\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(decompose(*i), o[0])"}
580
418
coding
Solve the programming task below in a Python markdown code block. You are given $n$ strings. Each string consists of lowercase English letters. Rearrange (reorder) the given strings in such a way that for every string, all strings that are placed before it are its substrings. String $a$ is a substring of string $b$ if it is possible to choose several consecutive letters in $b$ in such a way that they form $a$. For example, string "for" is contained as a substring in strings "codeforces", "for" and "therefore", but is not contained as a substring in strings "four", "fofo" and "rof". -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) — the number of strings. The next $n$ lines contain the given strings. The number of letters in each string is from $1$ to $100$, inclusive. Each string consists of lowercase English letters. Some strings might be equal. -----Output----- If it is impossible to reorder $n$ given strings in required order, print "NO" (without quotes). Otherwise print "YES" (without quotes) and $n$ given strings in required order. -----Examples----- Input 5 a aba abacaba ba aba Output YES a ba aba aba abacaba Input 5 a abacaba ba aba abab Output NO Input 3 qwerty qwerty qwerty Output YES qwerty qwerty qwerty -----Note----- In the second example you cannot reorder the strings because the string "abab" is not a substring of the string "abacaba".
{"inputs": ["2\na\nb\n", "2\na\nb\n", "2\na\nc\n", "2\n`\nc\n", "2\nq\nqq\n", "2\nq\nqq\n", "2\nq\npq\n", "2\nr\npq\n"], "outputs": ["NO\n", "NO\n", "NO\n", "NO\n", "YES\nq\nqq\n", "YES\nq\nqq\n", "YES\nq\npq\n", "NO\n"]}
376
113
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an integer array nums consisting of n elements, and an integer k. Find a contiguous subarray whose length is equal to k that has the maximum average value and return this value. Any answer with a calculation error less than 10-5 will be accepted.   Please complete the following python code precisely: ```python class Solution: def findMaxAverage(self, nums: List[int], k: int) -> float: ```
{"functional": "def check(candidate):\n assert candidate(nums = [1,12,-5,-6,50,3], k = 4) == 12.75\n assert candidate(nums = [5], k = 1) == 5.00000\n\n\ncheck(Solution().findMaxAverage)"}
109
76
coding
Solve the programming task below in a Python markdown code block. Once Bob saw a string. It contained so many different letters, that the letters were marked by numbers, but at the same time each letter could be met in the string at most 10 times. Bob didn't like that string, because it contained repeats: a repeat of length x is such a substring of length 2x, that its first half coincides character by character with its second half. Bob started deleting all the repeats from the string. He does it as follows: while it's possible, Bob takes the shortest repeat, if it is not unique, he takes the leftmost one, and deletes its left half and everything that is to the left of this repeat. You're given the string seen by Bob. Find out, what it will look like after Bob deletes all the repeats in the way described above. Input The first input line contains integer n (1 ≤ n ≤ 105) — length of the string. The following line contains n space-separated integer numbers from 0 to 109 inclusive — numbers that stand for the letters of the string. It's guaranteed that each letter can be met in the string at most 10 times. Output In the first line output the length of the string's part, left after Bob's deletions. In the second line output all the letters (separated by a space) of the string, left after Bob deleted all the repeats in the described way. Examples Input 6 1 2 3 1 2 3 Output 3 1 2 3 Input 7 4 5 6 5 6 7 7 Output 1 7
{"inputs": ["1\n0\n", "2\n1 2\n", "2\n4 2\n", "2\n2 2\n", "6\n1 2 3 1 2 3\n", "7\n1 2 3 1 2 3 1\n", "7\n4 5 6 5 6 5 7\n", "7\n4 5 6 5 9 5 7\n"], "outputs": ["1\n0 ", "2\n1 2 ", "2\n4 2\n", "1\n2\n", "3\n1 2 3 ", "4\n1 2 3 1 ", "4\n5 6 5 7\n", "7\n4 5 6 5 9 5 7\n"]}
357
182
coding
Solve the programming task below in a Python markdown code block. Given an amount A, we want you to compute the number of ways in which you can gather A rupees if you have an infinite supply of each of C = {1, 3, 5} valued rupee coins. Input: First line contains T, the number of test-cases. This is followed by T lines, where each line consists of the amount A. Output: For each test case, print the number of ways in which A can be formed using an infinite supply of 1, 3 and 5 rupee coins. Constraints T < 100 A < 101 Explanation : A = 5 Ways this amount can be achieved: {1,1,1,1,1}, {1,1,3}, {5} Hence, the answer is 3. SAMPLE INPUT 2 5 10 SAMPLE OUTPUT 3 7
{"inputs": ["100\n0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40\n41\n42\n43\n44\n45\n46\n47\n48\n49\n50\n51\n52\n53\n54\n55\n56\n57\n58\n59\n60\n61\n62\n63\n64\n65\n66\n67\n68\n69\n70\n71\n72\n73\n74\n75\n76\n77\n78\n79\n80\n81\n82\n83\n84\n85\n86\n87\n88\n89\n90\n91\n92\n93\n94\n95\n96\n97\n98\n99", "100\n84\n87\n78\n16\n94\n36\n87\n93\n50\n22\n63\n28\n91\n60\n64\n27\n41\n27\n73\n37\n12\n69\n68\n30\n83\n31\n63\n24\n68\n36\n30\n3\n23\n59\n70\n68\n94\n57\n12\n43\n30\n74\n22\n20\n85\n38\n99\n25\n16\n71\n14\n27\n92\n81\n57\n74\n63\n71\n97\n82\n6\n26\n85\n28\n37\n6\n47\n30\n14\n58\n25\n96\n83\n46\n15\n68\n35\n65\n44\n51\n88\n9\n77\n79\n89\n85\n4\n52\n55\n100\n33\n61\n77\n69\n40\n13\n27\n87\n95\n40"], "outputs": ["1\n1\n1\n2\n2\n3\n4\n4\n5\n6\n7\n8\n9\n10\n11\n13\n14\n15\n17\n18\n20\n22\n23\n25\n27\n29\n31\n33\n35\n37\n40\n42\n44\n47\n49\n52\n55\n57\n60\n63\n66\n69\n72\n75\n78\n82\n85\n88\n92\n95\n99\n103\n106\n110\n114\n118\n122\n126\n130\n134\n139\n143\n147\n152\n156\n161\n166\n170\n175\n180\n185\n190\n195\n200\n205\n211\n216\n221\n227\n232\n238\n244\n249\n255\n261\n267\n273\n279\n285\n291\n298\n304\n310\n317\n323\n330\n337\n343\n350\n357", "261\n279\n227\n14\n323\n55\n279\n317\n99\n23\n152\n35\n304\n139\n156\n33\n69\n33\n200\n57\n9\n180\n175\n40\n255\n42\n152\n27\n175\n55\n40\n2\n25\n134\n185\n175\n323\n126\n9\n75\n40\n205\n23\n20\n267\n60\n357\n29\n14\n190\n11\n33\n310\n244\n126\n205\n152\n190\n343\n249\n4\n31\n267\n35\n57\n4\n88\n40\n11\n130\n29\n337\n255\n85\n13\n175\n52\n161\n78\n103\n285\n6\n221\n232\n291\n267\n2\n106\n118\n364\n47\n143\n221\n180\n66\n10\n33\n279\n330\n66"]}
204
1,286
coding
Solve the programming task below in a Python markdown code block. FizzBuzz is a game in which integers of 1 or more are spoken in order according to the following rules. * "Fizz" when divisible by 3 * "Buzz" when divisible by 5 * "FizzBuzz" when divisible by both 3 and 5 * At other times, that number An example of the progress of the game is shown below. 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, 16,… The character string obtained by combining the obtained remarks into one character string is called FizzBuzz String. Since the index s is given, output 20 characters from the s character of the FizzBuzz String. However, the index may start from 1, and the length of the obtained character string may be sufficiently large (s + 20 or more). Constraints * s is an integer * 1 ≤ s ≤ 1018 Input Input is given in the following format > s > Output Output 20 characters from the s character of FizzBuzz String on one line Examples Input 1 Output 12Fizz4BuzzFizz78Fiz Input 20 Output zzBuzz11Fizz1314Fizz Input 10000000000 Output 93FizzBuzz1418650796
{"inputs": ["8", "2", "4", "3", "6", "5", "7", "9"], "outputs": ["BuzzFizz78FizzBuzz11\n", "2Fizz4BuzzFizz78Fizz\n", "izz4BuzzFizz78FizzBu\n", "Fizz4BuzzFizz78FizzB\n", "z4BuzzFizz78FizzBuzz\n", "zz4BuzzFizz78FizzBuz\n", "4BuzzFizz78FizzBuzz1\n", "uzzFizz78FizzBuzz11F\n"]}
336
120
coding
Solve the programming task below in a Python markdown code block. Chef has planned that he will drink exactly X liters of tea daily. He has an empty jar having a capacity of Y liters. Chef can visit the tea shop to refill the jar. In each refill, the jar is completely filled to the brim and Chef is charged Z rupees. Chef wonders what is the minimum amount of money he has to pay for drinking exactly X liters of tea daily. ------ Input Format ------ - First line will contain T, number of test cases. Then the test cases follow. - Each test case contains of a single line of input, three space-separated integers X, Y, and Z denoting the amount of tea Chef has to drink daily, the capacity of his jar and the cost of refilling his jar. ------ Output Format ------ For each test case, output in a single line, the minimum amount Chef has to pay. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ X, Y, Z ≤ 100$ ----- Sample Input 1 ------ 4 3 3 3 6 3 3 5 7 2 9 4 5 ----- Sample Output 1 ------ 3 6 2 15 ----- explanation 1 ------ Test case $1$: Chef can get his jar refilled once and drink $3$ liters of tea, which will cost him $3$ rupees in total. Test case $2$: Chef needs to refill his jar twice to get $3\cdot 2 = 6$ liters of tea, which will cost him $3\cdot 2 = 6$ rupees in total. Test case $3$: Chef can refill his jar once and drink exactly $5$ liters out of the $7$ liters. This will cost him $2$ rupees. Test case $4$: Chef can refill his jar twice and drink $4\cdot 2 = 8$ liters of tea. He needs to have one more refill, but this time he would drink only $1$ liter tea from the jar. Thus the total tea he drinks is $4+4+1 = 9$ liters. He made $3$ refills in total costing him $5\cdot 3 = 15$ rupees.
{"inputs": ["4\n3 3 3\n6 3 3\n5 7 2\n9 4 5\n"], "outputs": ["3\n6\n2\n15\n"]}
485
45
coding
Solve the programming task below in a Python markdown code block. # Task "AL-AHLY" and "Zamalek" are the best teams in Egypt, but "AL-AHLY" always wins the matches between them. "Zamalek" managers want to know what is the best match they've played so far. The best match is the match they lost with the minimum goal difference. If there is more than one match with the same difference, choose the one "Zamalek" scored more goals in. Given the information about all matches they played, return the `index` of the best match (`0-based`). If more than one valid result, return the smallest index. # Example For `ALAHLYGoals = [6,4] and zamalekGoals = [1,2]`, the output should be 1. Because `4 - 2` is less than `6 - 1` For `ALAHLYGoals = [1,2,3,4,5] and zamalekGoals = [0,1,2,3,4]`, the output should be 4. The goal difference of all matches are 1, but at 4th match "Zamalek" scored more goals in. So the result is `4`. # Input/Output - `[input]` integer array `ALAHLYGoals` The number of goals "AL-AHLY" scored in each match. - `[input]` integer array `zamalekGoals` The number of goals "Zamalek" scored in each match. It is guaranteed that zamalekGoals[i] < ALAHLYGoals[i] for each element. - `[output]` an integer Index of the best match. Also feel free to reuse/extend the following starter code: ```python def best_match(goals1, goals2): ```
{"functional": "_inputs = [[[6, 4], [1, 2]], [[1], [0]], [[1, 2, 3, 4, 5], [0, 1, 2, 3, 4]], [[3, 4, 3], [1, 1, 2]], [[4, 3, 4], [1, 1, 1]]]\n_outputs = [[1], [0], [4], [2], [1]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(best_match(*i), o[0])"}
405
247
coding
Solve the programming task below in a Python markdown code block. Yash likes playing with numbers. He has a challenge for you. Yash gives you a number that he made by multiplying two numbers. He claims that the possible sum of the two numbers he multiplied is as minimum as possible. He challenges you to find that minimum sum. Formally, for a given value of N, you are required to find the minimum sum of two distinct numbers x and y such that x, y > 0 and xy = N. -----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. Each testcase contains single integer N. -----Output:----- For each testcase, output minimum x + y such that x*y = N and x,y >0. -----Constraints :----- $1 \leq T \leq 100$ $1 < N \leq 10^{12}$ -----Sample Input:----- 1 852140 -----Sample Output:----- 1929
{"inputs": ["1\n852140"], "outputs": ["1929"]}
230
22
coding
Solve the programming task below in a Python markdown code block. Luke Skywalker gave Chewbacca an integer number x. Chewbacca isn't good at numbers but he loves inverting digits in them. Inverting digit t means replacing it with digit 9 - t. Help Chewbacca to transform the initial number x to the minimum possible positive number by inverting some (possibly, zero) digits. The decimal representation of the final number shouldn't start with a zero. -----Input----- The first line contains a single integer x (1 ≤ x ≤ 10^18) — the number that Luke Skywalker gave to Chewbacca. -----Output----- Print the minimum possible positive number that Chewbacca can obtain after inverting some digits. The number shouldn't contain leading zeroes. -----Examples----- Input 27 Output 22 Input 4545 Output 4444
{"inputs": ["1\n", "9\n", "9\n", "1\n", "5\n", "8\n", "27\n", "81\n"], "outputs": ["1\n", "9\n", "9\n", "1\n", "4\n", "1\n", "22\n", "11\n"]}
187
74
coding
Solve the programming task below in a Python markdown code block. You are given an array a_1, a_2, ..., a_{n} consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums. What is the maximum possible integer you can get? Definitions of subsegment and array splitting are given in notes. -----Input----- The first line contains two integers n and k (1 ≤ k ≤ n ≤ 10^5) — the size of the array a and the number of subsegments you have to split the array to. The second line contains n integers a_1, a_2, ..., a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Print single integer — the maximum possible integer you can get if you split the array into k non-empty subsegments and take maximum of minimums on the subsegments. -----Examples----- Input 5 2 1 2 3 4 5 Output 5 Input 5 1 -4 -5 -3 -2 -1 Output -5 -----Note----- A subsegment [l, r] (l ≤ r) of array a is the sequence a_{l}, a_{l} + 1, ..., a_{r}. Splitting of array a of n elements into k subsegments [l_1, r_1], [l_2, r_2], ..., [l_{k}, r_{k}] (l_1 = 1, r_{k} = n, l_{i} = r_{i} - 1 + 1 for all i > 1) is k sequences (a_{l}_1, ..., a_{r}_1), ..., (a_{l}_{k}, ..., a_{r}_{k}). In the first example you should split the array into subsegments [1, 4] and [5, 5] that results in sequences (1, 2, 3, 4) and (5). The minimums are min(1, 2, 3, 4) = 1 and min(5) = 5. The resulting maximum is max(1, 5) = 5. It is obvious that you can't reach greater result. In the second example the only option you have is to split the array into one subsegment [1, 5], that results in one sequence ( - 4, - 5, - 3, - 2, - 1). The only minimum is min( - 4, - 5, - 3, - 2, - 1) = - 5. The resulting maximum is - 5.
{"inputs": ["2 1\n1 2\n", "2 2\n5 2\n", "2 2\n5 2\n", "2 1\n1 2\n", "2 3\n5 2\n", "2 6\n5 2\n", "2 6\n5 0\n", "2 6\n1 0\n"], "outputs": ["1\n", "5\n", "5\n", "1\n", "5\n", "5\n", "5\n", "1\n"]}
626
118
coding
Solve the programming task below in a Python markdown code block. Chef visited a grocery store for fresh supplies. There are N items in the store where the i^{th} item has a freshness value A_{i} and cost B_{i}. Chef has decided to purchase all the items having a freshness value greater than equal to X. Find the total cost of the groceries Chef buys. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and X — the number of items and the minimum freshness value an item should have. - The second line contains N space-separated integers, the array A, denoting the freshness value of each item. - The third line contains N space-separated integers, the array B, denoting the cost of each item. ------ Output Format ------ For each test case, output on a new line, the total cost of the groceries Chef buys. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, X ≤ 100$ $1 ≤ A_{i}, B_{i} ≤ 100$ ----- Sample Input 1 ------ 4 2 20 15 67 10 90 3 1 1 2 3 1 2 3 3 100 10 90 50 30 7 93 4 50 12 78 50 40 40 30 20 10 ----- Sample Output 1 ------ 90 6 0 50 ----- explanation 1 ------ Test case $1$: Item $2$ has freshness value greater than equal to $X = 20$. Thus, Chef buys item $2$. The total cost is $90$. Test case $2$: Items $1, 2,$ and $3$ have freshness value greater than equal to $X = 1$. Thus, Chef buys all $3$ items. The total cost is $1+2+3 = 6$. Test case $3$: No item has freshness value greater than equal to $X = 100$. Thus, Chef buys no items. Test case $4$: Items $2$ and $3$ have freshness value greater than equal to $X = 50$. Thus, Chef buys items $2$ and $3$. The total cost is $30+20 = 50$.
{"inputs": ["4\n2 20\n15 67\n10 90\n3 1\n1 2 3\n1 2 3\n3 100\n10 90 50\n30 7 93\n4 50\n12 78 50 40\n40 30 20 10\n"], "outputs": ["90\n6\n0\n50\n"]}
554
107
coding
Solve the programming task below in a Python markdown code block. Gotham city is in danger. All the people from Gotham were airlifted and they are going to be dropped at different districts of a new safe city. There are $N$ districts located at position $(1, 2, \ldots, N)$ in a straight line in this newly built safe city. An array(P) is given which tells us about the maximum capacity of people that can be accommodated in the districts. There is a condition that people can move only in the forward direction(i.e from city $i$ to $i+1$ for $1 ≤ i < N$) to get shelter. People want to travel as little as possible to get shelter. The sad part is that in case any person who does not get shelter is going to die. Initially, all the districts in the new city are empty. You are asked $Q$ queries. Each query consists of two Integers $X$ and $K$, where $K$ denotes the number of people who are going to be dropped at district $X$. You are required to print the total sum of distance traveled by the people in the current query(that got shelter or did not die) in a new line. Note:- Queries are not independent(they depend on the previous queries). ------ Input ------ First Line consists of one Integer $N$, where $N$ is the number of districts. Second line consists of $N$ integers of (array P) separated by a space. Third Line contains an integer $Q$, which denotes the number of queries. Next, $Q$ lines consist of two integers $X$ and $K$ separated by space. ------ Output ------ For each query, you have to print the total sum of distance traveled by the people in the current query(that got shelter or did not die ) in a new line. ------ Constraints ------ $1 ≤ N ≤ 10^{5}$ $1 ≤ Q ≤ 10^{5}$ $1 ≤ X ≤ N$ $1 ≤ P[i] ≤ 10^{9}$ $1 ≤ K ≤ 10^{9}$ ------ Example Input ------ 4 5 5 6 1 2 2 11 2 3 ------ Example Output ------ 6 2 ------ Explanation ------ Query $1$: $5$ people will get shelter in city $2$ and the remaining $6$ will get shelter in city $3$. So, the total distance covered in this query is $6$. Query $2$: Only $1$ person can get shelter in city $4$ and the rest will die. So, the total distance traveled is $2$.
{"inputs": ["4\n5 5 6 1\n2\n2 11\n2 3\n"], "outputs": ["6\n2\n"]}
568
35
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become: [4,5,6,7,0,1,2] if it was rotated 4 times. [0,1,2,4,5,6,7] if it was rotated 7 times. Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]]. Given the sorted rotated array nums of unique elements, return the minimum element of this array. You must write an algorithm that runs in O(log n) time.   Please complete the following python code precisely: ```python class Solution: def findMin(self, nums: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [3,4,5,1,2]) == 1\n assert candidate(nums = [4,5,6,7,0,1,2]) == 0\n assert candidate(nums = [11,13,15,17]) == 11\n\n\ncheck(Solution().findMin)"}
228
86
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. There is an undirected graph with n nodes, where each node is numbered between 0 and n - 1. You are given a 2D array graph, where graph[u] is an array of nodes that node u is adjacent to. More formally, for each v in graph[u], there is an undirected edge between node u and node v. The graph has the following properties: There are no self-edges (graph[u] does not contain u). There are no parallel edges (graph[u] does not contain duplicate values). If v is in graph[u], then u is in graph[v] (the graph is undirected). The graph may not be connected, meaning there may be two nodes u and v such that there is no path between them. A graph is bipartite if the nodes can be partitioned into two independent sets A and B such that every edge in the graph connects a node in set A and a node in set B. Return true if and only if it is bipartite.   Please complete the following python code precisely: ```python class Solution: def isBipartite(self, graph: List[List[int]]) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(graph = [[1,2,3],[0,2],[0,1,3],[0,2]]) == False\n assert candidate(graph = [[1,3],[0,2],[1,3],[0,2]]) == True\n\n\ncheck(Solution().isBipartite)"}
260
76
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are playing a game that contains multiple characters, and each of the characters has two main properties: attack and defense. You are given a 2D integer array properties where properties[i] = [attacki, defensei] represents the properties of the ith character in the game. A character is said to be weak if any other character has both attack and defense levels strictly greater than this character's attack and defense levels. More formally, a character i is said to be weak if there exists another character j where attackj > attacki and defensej > defensei. Return the number of weak characters.   Please complete the following python code precisely: ```python class Solution: def numberOfWeakCharacters(self, properties: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(properties = [[5,5],[6,3],[3,6]]) == 0\n assert candidate(properties = [[2,2],[3,3]]) == 1\n assert candidate(properties = [[1,5],[10,4],[4,3]]) == 1\n\n\ncheck(Solution().numberOfWeakCharacters)"}
172
83
coding
Solve the programming task below in a Python markdown code block. In Berland a money reform is being prepared. New coins are being introduced. After long economic calculations was decided that the most expensive coin should possess the denomination of exactly n Berland dollars. Also the following restriction has been introduced for comfort: the denomination of each coin should be divisible by the denomination of any cheaper coin. It is known that among all the possible variants the variant with the largest number of new coins will be chosen. Find this variant. Print in the order of decreasing of the coins' denominations. Input The first and only line contains an integer n (1 ≤ n ≤ 106) which represents the denomination of the most expensive coin. Output Print the denominations of all the coins in the order of decreasing. The number of coins must be the largest possible (with the given denomination n of the most expensive coin). Also, the denomination of every coin must be divisible by the denomination of any cheaper coin. Naturally, the denominations of all the coins should be different. If there are several solutins to that problem, print any of them. Examples Input 10 Output 10 5 1 Input 4 Output 4 2 1 Input 3 Output 3 1
{"inputs": ["8\n", "7\n", "9\n", "6\n", "1\n", "2\n", "5\n", "3\n"], "outputs": ["8 4 2 1\n", "7 1\n", "9 3 1\n", "6 3 1\n", "1\n", "2 1\n", "5 1\n", "3 1\n"]}
269
92
coding
Solve the programming task below in a Python markdown code block. Students are taught to add multi-digit numbers from right-to-left one digit at a time. Many find the "carry" operation - in which 1 is carried from one digit position to be added to the next - to be a significant challenge. Your job is to count the number of carry operations for each of addition problem so that educators may assess their difficulty. For the input first line contains n number of records which is less then 1000. And then each line contains two numbers with spacing between them as shown in sample. SAMPLE INPUT 3 123 456 555 555 123 594 SAMPLE OUTPUT No carry operation 3 carry operations 1 carry operation Explanation EXAMPLE 1:- when the digits are added then there is no carry found as 3+6=9,2+5=7 and 1+4=5. EXAMPLE 2:- when the digits are added then carry is generated as 5+5 =10 so carry is found 3 times. EXAMPLE 3:- when the digits 9 and 2 are added only then carry is found so 1 time only.
{"inputs": ["3\n123 456\n555 555\n123 594", "3\n5555555555 5555555555\n1111111111 1111111111\n5111111111 5111111111"], "outputs": ["No carry operation\n3 carry operations\n1 carry operation", "10 carry operations\nNo carry operation\n1 carry operation"]}
264
129
coding
Solve the programming task below in a Python markdown code block. Read problems statements in [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. Lately, Chef has been inspired by the "pawri" meme. Therefore, he decided to take a string $S$ and change each of its substrings that spells "party" to "pawri". Find the resulting string. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing the string after it is modified by Chef. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ |S| ≤ 10^{5}$ $S$ contains only lowercase English letters ----- Sample Input 1 ------ 3 part partypartiparty yemaihuyemericarhaiauryahapartyhorahihai ----- Sample Output 1 ------ part pawripartipawri yemaihuyemericarhaiauryahapawrihorahihai ----- explanation 1 ------ Example case 1: There is no substring "party" in the original string. Example case 2: The original string has $2$ substrings "party". Example case 3: There is only a single substring "party" in the original string.
{"inputs": ["3\npart\npartypartiparty\nyemaihuyemericarhaiauryahapartyhorahihai"], "outputs": ["part\npawripartipawri\nyemaihuyemericarhaiauryahapawrihorahihai"]}
335
67
coding
Solve the programming task below in a Python markdown code block. You are given three strings A, B and C. Each of these is a string of length N consisting of lowercase English letters. Our objective is to make all these three strings equal. For that, you can repeatedly perform the following operation: * Operation: Choose one of the strings A, B and C, and specify an integer i between 1 and N (inclusive). Change the i-th character from the beginning of the chosen string to some other lowercase English letter. What is the minimum number of operations required to achieve the objective? Constraints * 1 \leq N \leq 100 * Each of the strings A, B and C is a string of length N. * Each character in each of the strings A, B and C is a lowercase English letter. Input Input is given from Standard Input in the following format: N A B C Output Print the minimum number of operations required. Examples Input 4 west east wait Output 3 Input 9 different different different Output 0 Input 7 zenkoku touitsu program Output 13
{"inputs": ["4\nwest\neast\nw`it", "4\nwesu\neast\nw`it", "4\nwetu\neast\nw`it", "4\nwetv\n`est\ntiaw", "4\ntewv\n`ftt\nsaiw", "4\nwetu\ne`st\nw`it", "4\nwetu\n`est\nw`it", "4\nwetu\n`est\nwait"], "outputs": ["4\n", "5\n", "6\n", "7\n", "8\n", "5\n", "5\n", "5\n"]}
252
144
coding
Solve the programming task below in a Python markdown code block. The courier charges for a courier company are set according to size and weight as shown in the table below. A size | B size | C size | D size | E size | F size --- | --- | --- | --- | --- | --- | --- Size | 60 cm or less | 80 cm or less | 100 cm or less | 120 cm or less | 140 cm or less | 160 cm or less Weight | 2kg or less | 5kg or less | 10kg or less | 15kg or less | 20kg or less | 25kg or less Price | 600 yen | 800 yen | 1000 yen | 1200 yen | 1400 yen | 1600 yen The size is the sum of the three sides (length, width, height). For example, a baggage that is 120 cm in size and weighs less than 15 kg will be D size (1,200 yen). Even if the size is 120 cm or less, if the weight exceeds 15 kg and 20 kg or less, it will be E size. Please create a program that outputs the total charge by inputting the information of the luggage brought in in one day. Luggage that exceeds F size is excluded and is not included in the total price. 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 x1 y1 h1 w1 x2 y2 h2 w2 :: xn yn hn wn The number of packages n (1 ≤ n ≤ 10000) on the first line, followed by the i-th package on the n lines: vertical length xi, horizontal length yi, height hi, weight wi (1 ≤ xi, yi , hi, wi ≤ 200) are given on one line, separated by blanks. The number of datasets does not exceed 20. Output Outputs the total package charge for each dataset on one line. Example Input 2 50 25 5 5 80 60 10 30 3 10 15 25 24 5 8 12 5 30 30 30 18 0 Output 800 3800
{"inputs": ["2\n26 1 8 2\n80 82 0 0\n3\n18 0 0 24\n4 0 4 3\n30 30 1 1\n0", "2\n51 1 5 5\n80 82 3 0\n3\n7 2 0 24\n2 0 12 3\n30 30 0 11\n0", "2\n51 2 5 5\n80 82 3 0\n3\n7 2 0 24\n2 0 12 3\n30 30 0 11\n0", "2\n51 1 5 5\n80 82 3 0\n3\n10 2 0 24\n2 0 12 3\n30 30 0 11\n0", "2\n51 2 5 5\n80 82 3 0\n3\n7 2 0 24\n2 0 12 3\n30 30 -1 11\n0", "2\n74 2 5 5\n80 82 1 0\n0\n7 2 0 24\n2 -1 12 3\n30 30 -1 11\n0", "2\n74 2 5 5\n80 47 0 0\n0\n7 2 0 24\n2 -1 12 3\n30 30 -2 11\n0", "2\n74 2 5 5\n31 82 3 0\n3\n7 2 0 24\n2 -1 12 3\n27 30 -1 11\n0"], "outputs": ["600\n3200\n", "800\n3600\n", "800\n3600\n", "800\n3600\n", "800\n3600\n", "1000\n", "2400\n", "2200\n3600\n"]}
555
527
coding
Solve the programming task below in a Python markdown code block. As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation: $f(a, l, r) = \sum_{i = l}^{r} a [ i ] ; m(a) = \operatorname{max}_{1 \leq l \leq r \leq n} f(a, l, r)$ A swap operation is the following sequence of actions: choose two indexes i, j (i ≠ j); perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp. What maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations? -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 200; 1 ≤ k ≤ 10). The next line contains n integers a[1], a[2], ..., a[n] ( - 1000 ≤ a[i] ≤ 1000). -----Output----- In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations. -----Examples----- Input 10 2 10 -1 2 2 2 2 2 2 -1 10 Output 32 Input 5 10 -1 -1 -1 -1 -1 Output -1
{"inputs": ["1 1\n1\n", "1 1\n1\n", "1 1\n-1\n", "1 10\n1\n", "1 10\n1\n", "1 1\n-1\n", "1 16\n1\n", "1 2\n-2\n"], "outputs": ["1\n", "1\n", "-1\n", "1\n", "1\n", "-1\n", "1\n", "-2\n"]}
328
108
coding
Solve the programming task below in a Python markdown code block. You are given two positive integers $n$ and $s$. Find the maximum possible median of an array of $n$ non-negative integers (not necessarily distinct), such that the sum of its elements is equal to $s$. A median of an array of integers of length $m$ is the number standing on the $\lceil {\frac{m}{2}} \rceil$-th (rounding up) position in the non-decreasing ordering of its elements. Positions are numbered starting from $1$. For example, a median of the array $[20,40,20,50,50,30]$ is the $\lceil \frac{m}{2} \rceil$-th element of $[20,20,30,40,50,50]$, so it is $30$. There exist other definitions of the median, but in this problem we use the described definition. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Description of the test cases follows. Each test case contains a single line with two integers $n$ and $s$ ($1 \le n, s \le 10^9$) — the length of the array and the required sum of the elements. -----Output----- For each test case print a single integer — the maximum possible median. -----Examples----- Input 8 1 5 2 5 3 5 2 1 7 17 4 14 1 1000000000 1000000000 1 Output 5 2 2 0 4 4 1000000000 0 -----Note----- Possible arrays for the first three test cases (in each array the median is underlined): In the first test case $[\underline{5}]$ In the second test case $[\underline{2}, 3]$ In the third test case $[1, \underline{2}, 2]$
{"inputs": ["8\n1 5\n1 9\n3 5\n2 1\n14 0\n4 2\n1 1010010000\n1000100000 1\n", "8\n1 9\n1 9\n3 5\n2 1\n14 0\n4 2\n1 1010010000\n1000100000 1\n", "8\n1 9\n1 9\n3 7\n2 1\n14 0\n6 2\n1 1010010000\n1000100000 1\n", "8\n1 9\n1 9\n3 7\n3 2\n14 0\n6 2\n1 1010010000\n1000100000 1\n", "8\n1 5\n1 9\n3 5\n2 1\n14 0\n4 2\n2 1010010000\n1000100000 1\n", "8\n1 9\n1 9\n3 5\n1 1\n14 0\n6 2\n1 1010010000\n1000100000 1\n", "8\n1 9\n1 9\n3 7\n2 1\n14 0\n6 2\n1 1010010100\n1000100000 1\n", "8\n1 6\n1 1\n3 5\n2 1\n14 7\n4 2\n1 1000000000\n1000000000 2\n"], "outputs": ["5\n9\n2\n0\n0\n0\n1010010000\n0\n", "9\n9\n2\n0\n0\n0\n1010010000\n0\n", "9\n9\n3\n0\n0\n0\n1010010000\n0\n", "9\n9\n3\n1\n0\n0\n1010010000\n0\n", "5\n9\n2\n0\n0\n0\n505005000\n0\n", "9\n9\n2\n1\n0\n0\n1010010000\n0\n", "9\n9\n3\n0\n0\n0\n1010010100\n0\n", "6\n1\n2\n0\n0\n0\n1000000000\n0\n"]}
476
661
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an integer n, you must transform it into 0 using the following operations any number of times: Change the rightmost (0th) bit in the binary representation of n. Change the ith bit in the binary representation of n if the (i-1)th bit is set to 1 and the (i-2)th through 0th bits are set to 0. Return the minimum number of operations to transform n into 0.   Please complete the following python code precisely: ```python class Solution: def minimumOneBitOperations(self, n: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(n = 3) == 2\n assert candidate(n = 6) == 4\n\n\ncheck(Solution().minimumOneBitOperations)"}
142
45
coding
Solve the programming task below in a Python markdown code block. In this kata, your task is to write a function `to_bytes(n)` (or `toBytes(n)` depending on language) that produces a list of bytes that represent a given non-negative integer `n`. Each byte in the list is represented by a string of `'0'` and `'1'` of length 8. The most significant byte is first in the list. The example test cases should provide you with all the details. You may assume that the argument `n` is valid. Also feel free to reuse/extend the following starter code: ```python def to_bytes(n): ```
{"functional": "_inputs = [[0], [1]]\n_outputs = [[['00000000']], [['00000001']]]\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(to_bytes(*i), o[0])"}
136
176
coding
Solve the programming task below in a Python markdown code block. The only difference between easy and hard versions is constraints. Ivan plays a computer game that contains some microtransactions to make characters look cooler. Since Ivan wants his character to be really cool, he wants to use some of these microtransactions — and he won't start playing until he gets all of them. Each day (during the morning) Ivan earns exactly one burle. There are $n$ types of microtransactions in the game. Each microtransaction costs $2$ burles usually and $1$ burle if it is on sale. Ivan has to order exactly $k_i$ microtransactions of the $i$-th type (he orders microtransactions during the evening). Ivan can order any (possibly zero) number of microtransactions of any types during any day (of course, if he has enough money to do it). If the microtransaction he wants to order is on sale then he can buy it for $1$ burle and otherwise he can buy it for $2$ burles. There are also $m$ special offers in the game shop. The $j$-th offer $(d_j, t_j)$ means that microtransactions of the $t_j$-th type are on sale during the $d_j$-th day. Ivan wants to order all microtransactions as soon as possible. Your task is to calculate the minimum day when he can buy all microtransactions he want and actually start playing. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n, m \le 1000$) — the number of types of microtransactions and the number of special offers in the game shop. The second line of the input contains $n$ integers $k_1, k_2, \dots, k_n$ ($0 \le k_i \le 1000$), where $k_i$ is the number of copies of microtransaction of the $i$-th type Ivan has to order. It is guaranteed that sum of all $k_i$ is not less than $1$ and not greater than $1000$. The next $m$ lines contain special offers. The $j$-th of these lines contains the $j$-th special offer. It is given as a pair of integers $(d_j, t_j)$ ($1 \le d_j \le 1000, 1 \le t_j \le n$) and means that microtransactions of the $t_j$-th type are on sale during the $d_j$-th day. -----Output----- Print one integer — the minimum day when Ivan can order all microtransactions he wants and actually start playing. -----Examples----- Input 5 6 1 2 0 2 0 2 4 3 3 1 5 1 2 1 5 2 3 Output 8 Input 5 3 4 2 1 3 2 3 5 4 2 2 5 Output 20
{"inputs": ["5 3\n4 2 1 3 2\n3 5\n4 2\n2 5\n", "5 3\n4 2 1 3 0\n3 5\n4 2\n2 5\n", "5 3\n2 2 1 3 2\n3 5\n4 2\n2 5\n", "5 3\n4 2 1 5 0\n3 5\n4 2\n2 5\n", "5 3\n2 2 1 3 2\n3 5\n4 3\n2 5\n", "5 3\n4 2 1 0 2\n3 5\n4 2\n2 5\n", "5 3\n4 2 2 5 0\n3 5\n4 2\n2 5\n", "5 3\n1 2 1 3 2\n3 5\n4 3\n2 5\n"], "outputs": ["20\n", "18\n", "16\n", "22\n", "17\n", "14\n", "24\n", "15\n"]}
655
270
coding
Solve the programming task below in a Python markdown code block. Sultan, the freestyle wrestler, you all know him. He broke multiple records in the history of all wrestling leagues. Now 20 years have passed, Sultan has grown old. He has two sons, he wants them to be like him. Sultan being orthodox goes to his astrologer, where he is told that his sons shall be invincible like him. Sultan starts to train them. After training, his son Multan & Fultan, having strengths are M and F respectively, decide to fight. They can defeat if the strength of challengers Si is a positive integer multiple of their strength else they lose. Multan fights first, then Fultan. A challenger once knocked out cannot challenge them again. Sultan's sons are still not very good wrestlers. Sultan considers them wrestlers if they both combined are able to win at least 70% of the all the fights. Also, he wants to know who is a better wrestler of the two. Your task is to help Sultan in this venture. Print "Yes" (without quotes) if they are able to win, else print "No" (without quotes). If yes, also name whether, "Multan" or "Fultan" is a better wrestler, if both win equally print “Both”. -----Input----- - First line contains single integer T denoting test cases. - Second Line contains single integer N for number of challengers. - Third Line contains space separated two integer denoting strength M & F - Next Line contains strength space separated N integer ith of which denoting Si of N challengers respectively. -----Output----- - Yes or No corresponding the result. - Also, if Yes, print, Multan, Fultan, Both accordingly. -----Constraints----- - 1 ≤ T ≤ 100 - 1 ≤ N ≤ 1000 - 1 ≤ M, F ≤ 109 - 0 ≤ Si ≤ 109 -----Example----- Input: 2 7 2 3 4 5 7 8 9 10 14 6 5 7 1 2 8 9 10 11 Output: Yes Multan No -----Explanation----- Example case 1. Multan (M) defeats total 4 challengers with strengths 4, 8, 10, 14 and Fultan (F) defeats 1 challenger with strength 9. Their total wins are 5 out of 7 and win accuracy of 71.4%. Hence, 'Yes' and since Multan is better wrestler so 'Multan' Example case 2. Multan defeats 1 and Fultan defeat 0 challengers. Total wins 1 out of 6 with accuracy 16.67% Hence, No.
{"inputs": ["2\n7\n2 3\n4 5 7 8 9 10 14\n6\n5 7\n1 2 8 9 10 11"], "outputs": ["Yes\nMultan\nNo"]}
599
59
coding
Solve the programming task below in a Python markdown code block. You are given $n$ integers $a_1, a_2, \ldots, a_n$. Find the maximum value of $max(a_l, a_{l + 1}, \ldots, a_r) \cdot min(a_l, a_{l + 1}, \ldots, a_r)$ over all pairs $(l, r)$ of integers for which $1 \le l < r \le n$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^6$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $3 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum possible value of the product from the statement. -----Examples----- Input 4 3 2 4 3 4 3 2 3 1 2 69 69 6 719313 273225 402638 473783 804745 323328 Output 12 6 4761 381274500335 -----Note----- Let $f(l, r) = max(a_l, a_{l + 1}, \ldots, a_r) \cdot min(a_l, a_{l + 1}, \ldots, a_r)$. In the first test case, $f(1, 2) = max(a_1, a_2) \cdot min(a_1, a_2) = max(2, 4) \cdot min(2, 4) = 4 \cdot 2 = 8$. $f(1, 3) = max(a_1, a_2, a_3) \cdot min(a_1, a_2, a_3) = max(2, 4, 3) \cdot min(2, 4, 3) = 4 \cdot 2 = 8$. $f(2, 3) = max(a_2, a_3) \cdot min(a_2, a_3) = max(4, 3) \cdot min(4, 3) = 4 \cdot 3 = 12$. So the maximum is $f(2, 3) = 12$. In the second test case, the maximum is $f(1, 2) = f(1, 3) = f(2, 3) = 6$.
{"inputs": ["1\n2\n44841 417\n", "1\n2\n44841 2653\n", "1\n2\n44841 66241\n", "1\n2\n44841 83433\n", "1\n2\n29981 18424\n", "1\n2\n23338 18424\n", "1\n2\n23338 34273\n", "1\n2\n11672 34273\n"], "outputs": ["18698697\n", "118963173\n", "2970312681\n", "3741219153\n", "552369944\n", "429979312\n", "799863274\n", "400034456\n"]}
663
244
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese , Russian and Vietnamese as well. Chef belongs to a very rich family which owns many gold mines. Today, he brought N gold coins and decided to form a triangle using these coins. Isn't it strange? Chef has a unusual way of forming a triangle using gold coins, which is described as follows: He puts 1 coin in the 1^{st} row. then puts 2 coins in the 2^{nd} row. then puts 3 coins in the 3^{rd} row. and so on as shown in the given figure. Chef is interested in forming a triangle with maximum possible height using at most N coins. Can you tell him the maximum possible height of the triangle? ------ Input ------ The first line of input contains a single integer T denoting the number of test cases. The first and the only line of each test case contains an integer N denoting the number of gold coins Chef has. ------ Output ------ For each test case, output a single line containing an integer corresponding to the maximum possible height of the triangle that Chef can get. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 10^{9}$ ------ Subtasks ------ $Subtask 1 (48 points) : 1 ≤ N ≤ 10^{5}$ $Subtask 2 (52 points) : 1 ≤ N ≤ 10^{9}$ ----- Sample Input 1 ------ 3 3 5 7 ----- Sample Output 1 ------ 2 2 3 ----- explanation 1 ------ Test 1: Chef can't form a triangle with height > 2 as it requires atleast 6 gold coins. Test 2: Chef can't form a triangle with height > 2 as it requires atleast 6 gold coins. Test 3: Chef can't form a triangle with height > 3 as it requires atleast 10 gold coins.
{"inputs": ["3\n3\n5\n7", "3\n5\n9\n2", "3\n5\n5\n2", "3\n2\n5\n2", "3\n5\n5\n7", "3\n5\n5\n3", "3\n5\n7\n7", "3\n5\n7\n5"], "outputs": ["2\n2\n3", "2\n3\n1\n", "2\n2\n1\n", "1\n2\n1\n", "2\n2\n3\n", "2\n2\n2\n", "2\n3\n3\n", "2\n3\n2\n"]}
423
141
coding
Solve the programming task below in a Python markdown code block. You are given a board of size $n \times n$, where $n$ is odd (not divisible by $2$). Initially, each cell of the board contains one figure. In one move, you can select exactly one figure presented in some cell and move it to one of the cells sharing a side or a corner with the current cell, i.e. from the cell $(i, j)$ you can move the figure to cells: $(i - 1, j - 1)$; $(i - 1, j)$; $(i - 1, j + 1)$; $(i, j - 1)$; $(i, j + 1)$; $(i + 1, j - 1)$; $(i + 1, j)$; $(i + 1, j + 1)$; Of course, you can not move figures to cells out of the board. It is allowed that after a move there will be several figures in one cell. Your task is to find the minimum number of moves needed to get all the figures into one cell (i.e. $n^2-1$ cells should contain $0$ figures and one cell should contain $n^2$ figures). You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 200$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n < 5 \cdot 10^5$) — the size of the board. It is guaranteed that $n$ is odd (not divisible by $2$). It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^5$ ($\sum n \le 5 \cdot 10^5$). -----Output----- For each test case print the answer — the minimum number of moves needed to get all the figures into one cell. -----Example----- Input 3 1 5 499993 Output 0 40 41664916690999888
{"inputs": ["1\n2119\n", "1\n9693\n", "1\n2569\n", "1\n69791\n", "1\n69791\n", "1\n54111\n", "1\n39575\n", "1\n47045\n"], "outputs": ["3171549680\n", "303566175288\n", "5651594480\n", "113312287936960\n", "113312287936960\n", "52812341771840\n", "20660532731600\n", "34707166856360\n"]}
496
211
coding
Solve the programming task below in a Python markdown code block. After the hundred years of war started by the Fire Nation, its time for the Water Tribes to bring it to an end. Avatar asked Sokka to go to the Southern WarZone from The Northern WarZone and gave him some money in a bag for his journey. Sokka has the World Map to guide him during his journey . The World Map is described as a $N$x$N$ grid where the Northern WarZone is denoted by $(0,0)$ and the Southern WarZone is denoted by $(N-1,N-1)$. Each location on the world map is denoted in a similar way by two integers r and c. For each location having: - $r = c$ denotes neutral land ( $(0,0)$ and $(N-1,N-1)$ also come under this category) - $r < c$ denotes the land under the rule of Water Tribes - $r > c$ denotes the land under the rule of Fire Kingdom Being wise Sokka travels only from one location to any other valid location (by valid location we mean a location which exists on the world map grid i.e. for that location $0 \leq r < N$ and $0 \leq c < N$ ) just to the right $(r,c+1)$ or below $(r+1,c)$ the current location randomly. Due to the times of war , Sokka has to pay one coin each time he transitions from one nation to another. Here a transition is counted when Sokka is in Water Tribe land and moves to Fire Nation Land crossing the neutral land or vice versa .The first move is obviously never counted as a transition. Moreover , the coin is to be payed exactly once for one such transition (eg. if he makes this transition k times he has to pay k coins). The initial number of coins Sokka has is $2*N$. The probability that the coins he has when he reaches his destination is lesser than the number of coins he started his journey with can be expressed as a fraction $P/Q$, where P and Q are integers $(P \geq 0, Q > 0)$ and Q is co-prime with $(10^9)+7$. You should compute $P/Q$ modulo $(10^9)+7$ for $T$ values of $N$. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, one integer $N$, the size of the world map grid. -----Output:----- For each testcase, output in a single line , the Probability modulo (10^9)+7. -----Constraints----- - $1 \leq T \leq 100000$ - $2 \leq N \leq 10^9$ -----Sample Input:----- 2 5 2 -----Sample Output:----- 200000002 0 -----EXPLANATION:----- For N=2, there is no possible path in which Sokka has to spend money, so the final number of coins in the bag is not lesser than the initial amount.
{"inputs": ["2\n5\n2"], "outputs": ["200000002\n0"]}
679
26
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin chinese, Russian and Vietnamese as well. Gritukan likes to take cyclic trips around the world. There are $N$ countries numbered $1$ through $N$ in the world. Before starting his travels, Gritukan chooses a permutation $P$ of all $N$ countries. Then, he visits all countries (some countries may be visited multiple times) using the following algorithm: for each $v$ from $1$ to $N$ inclusive: - travel to country $v$ - travel from country $v$ to country $P_{v}$, then from country $P_{v}$ to country $P_{P_{v}}$, and so on until he reaches country $v$ again; let's call this sequence of visited countries a *cyclic route* (Each country appears at most once in a cyclic route. It is also possible for a route to contain only one country if $v=P_{v}$, in which case Gritukan's travel along this route consists of staying in country $v$.) Unfortunately, Gritukan does not remember the routes he took. He only remembers a sequence $A_{1..N}$ with the following meaning: for each country $v$, the number of cyclic routes which contained this country is $A_{v}$. Given the sequence $A$, Gritukan asks you to calculate the number of permutations $P$ consistent with this sequence, modulo $998244353$ (it's prime). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \dots, A_{N}$. ------ Output ------ For each test case, print a single line containing one integer — the number of possible permutations modulo $998244353$ (it's prime). ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 10^{6}$ $1 ≤ A_{i} ≤ N$ for each valid $i$ the sum of $N$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (40 points): $1 ≤ N ≤ 5000$ Subtask #2 (60 points): original constraints ----- Sample Input 1 ------ 2 6 1 1 1 1 1 1 6 6 6 6 6 6 6 ----- Sample Output 1 ------ 1 120
{"inputs": ["2\n6\n1 1 1 1 1 1\n6\n6 6 6 6 6 6"], "outputs": ["1\n120"]}
595
44
coding
Solve the programming task below in a Python markdown code block. As a New Year's gift, Dolphin received a string s of length 19. The string s has the following format: [five lowercase English letters],[seven lowercase English letters],[five lowercase English letters]. Dolphin wants to convert the comma-separated string s into a space-separated string. Write a program to perform the conversion for him. -----Constraints----- - The length of s is 19. - The sixth and fourteenth characters in s are ,. - The other characters in s are lowercase English letters. -----Input----- The input is given from Standard Input in the following format: s -----Output----- Print the string after the conversion. -----Sample Input----- happy,newyear,enjoy -----Sample Output----- happy newyear enjoy Replace all the commas in happy,newyear,enjoy with spaces to obtain happy newyear enjoy.
{"inputs": ["happy,newyear,enjoy\n", "haiku,atcoder,tasks\n", "abcde,fghihgf,edcba\n", "mzbmw,yydiadt,coueg\n", "dbyfw,rpwbpuv,ifnua\n", "wyndm,tqvkgkb,tytsz\n", "twfle,sjzzszf,tzfpn\n", "cguem,rczqxyc,vdqnk\n"], "outputs": ["happy newyear enjoy\n", "haiku atcoder tasks\n", "abcde fghihgf edcba\n", "mzbmw yydiadt coueg\n", "dbyfw rpwbpuv ifnua\n", "wyndm tqvkgkb tytsz\n", "twfle sjzzszf tzfpn\n", "cguem rczqxyc vdqnk\n"]}
184
196
coding
Solve the programming task below in a Python markdown code block. Two children, Lily and Ron, want to share a chocolate bar. Each of the squares has an integer on it. Lily decides to share a contiguous segment of the bar selected such that: The length of the segment matches Ron's birth month, and, The sum of the integers on the squares is equal to his birth day. Determine how many ways she can divide the chocolate. Example $s=[2,2,1,3,2]$ $\boldsymbol{d}=4$ $m=2$ Lily wants to find segments summing to Ron's birth day, $\boldsymbol{d}=4$ with a length equalling his birth month, $m=2$. In this case, there are two segments meeting her criteria: $[2,2]$ and $[1,3]$. Function Description Complete the birthday function in the editor below. birthday has the following parameter(s): int s[n]: the numbers on each of the squares of chocolate int d: Ron's birth day int m: Ron's birth month Returns int: the number of ways the bar can be divided Input Format The first line contains an integer $n$, the number of squares in the chocolate bar. The second line contains $n$ space-separated integers $s[i]$, the numbers on the chocolate squares where $0\leq i<n$. The third line contains two space-separated integers, $\boldsymbol{d}$ and $m$, Ron's birth day and his birth month. Constraints $1\leq n\leq100$ $1\leq s[i]\leq5$, where ($0\leq i<n$) $1\leq d\leq31$ $1\leq m\leq12$ Sample Input 0 5 1 2 1 3 2 3 2 Sample Output 0 2 Explanation 0 Lily wants to give Ron $m=2$ squares summing to $\boldsymbol{d}=3$. The following two segments meet the criteria: Sample Input 1 6 1 1 1 1 1 1 3 2 Sample Output 1 0 Explanation 1 Lily only wants to give Ron $m=2$ consecutive squares of chocolate whose integers sum to $\boldsymbol{d}=3$. There are no possible pieces satisfying these constraints: Thus, we print $\mbox{o}$ as our answer. Sample Input 2 1 4 4 1 Sample Output 2 1 Explanation 2 Lily only wants to give Ron $m=1$ square of chocolate with an integer value of $\boldsymbol{d}=4$. Because the only square of chocolate in the bar satisfies this constraint, we print $\mbox{1}$ as our answer.
{"inputs": ["1\n4\n4 1\n", "5\n1 2 1 3 2\n3 2\n", "6\n1 1 1 1 1 1\n3 2\n"], "outputs": ["1\n", "2\n", "0\n"]}
620
66
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an integer array queries and a positive integer intLength, return an array answer where answer[i] is either the queries[i]th smallest positive palindrome of length intLength or -1 if no such palindrome exists. A palindrome is a number that reads the same backwards and forwards. Palindromes cannot have leading zeros.   Please complete the following python code precisely: ```python class Solution: def kthPalindrome(self, queries: List[int], intLength: int) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(queries = [1,2,3,4,5,90], intLength = 3) == [101,111,121,131,141,999]\n assert candidate(queries = [2,4,6], intLength = 4) == [1111,1331,1551]\n\n\ncheck(Solution().kthPalindrome)"}
120
110
coding
Solve the programming task below in a Python markdown code block. Let N be a positive integer. You are given a string s of length N - 1, consisting of `<` and `>`. Find the number of permutations (p_1, p_2, \ldots, p_N) of (1, 2, \ldots, N) that satisfy the following condition, modulo 10^9 + 7: * For each i (1 \leq i \leq N - 1), p_i < p_{i + 1} if the i-th character in s is `<`, and p_i > p_{i + 1} if the i-th character in s is `>`. Constraints * N is an integer. * 2 \leq N \leq 3000 * s is a string of length N - 1. * s consists of `<` and `>`. Input Input is given from Standard Input in the following format: N s Output Print the number of permutations that satisfy the condition, modulo 10^9 + 7. Examples Input 4 <>< Output 5 Input 5 <<<< Output 1 Input 20 >>>><>>><>><>>><<>> Output 217136290
{"inputs": ["4\n<<>", "4\n<><", "5\n<<><", "5\n><<<", "5\n<<<<", "20\n>><<>>><>><>>><>>>>", "20\n>><<>>><>>>>>><>>><", "20\n>>>><>>>>><<>>><<>>"], "outputs": ["3\n", "5", "9\n", "4\n", "1", "217136290\n", "415929071\n", "784400335\n"]}
285
132
coding
Solve the programming task below in a Python markdown code block. It's hard times now. Today Petya needs to score 100 points on Informatics exam. The tasks seem easy to Petya, but he thinks he lacks time to finish them all, so he asks you to help with one.. There is a glob pattern in the statements (a string consisting of lowercase English letters, characters "?" and "*"). It is known that character "*" occurs no more than once in the pattern. Also, n query strings are given, it is required to determine for each of them if the pattern matches it or not. Everything seemed easy to Petya, but then he discovered that the special pattern characters differ from their usual meaning. A pattern matches a string if it is possible to replace each character "?" with one good lowercase English letter, and the character "*" (if there is one) with any, including empty, string of bad lowercase English letters, so that the resulting string is the same as the given string. The good letters are given to Petya. All the others are bad. -----Input----- The first line contains a string with length from 1 to 26 consisting of distinct lowercase English letters. These letters are good letters, all the others are bad. The second line contains the pattern — a string s of lowercase English letters, characters "?" and "*" (1 ≤ |s| ≤ 10^5). It is guaranteed that character "*" occurs in s no more than once. The third line contains integer n (1 ≤ n ≤ 10^5) — the number of query strings. n lines follow, each of them contains single non-empty string consisting of lowercase English letters — a query string. It is guaranteed that the total length of all query strings is not greater than 10^5. -----Output----- Print n lines: in the i-th of them print "YES" if the pattern matches the i-th query string, and "NO" otherwise. You can choose the case (lower or upper) for each letter arbitrary. -----Examples----- Input ab a?a 2 aaa aab Output YES NO Input abc a?a?a* 4 abacaba abaca apapa aaaaax Output NO YES NO YES -----Note----- In the first example we can replace "?" with good letters "a" and "b", so we can see that the answer for the first query is "YES", and the answer for the second query is "NO", because we can't match the third letter. Explanation of the second example. The first query: "NO", because character "*" can be replaced with a string of bad letters only, but the only way to match the query string is to replace it with the string "ba", in which both letters are good. The second query: "YES", because characters "?" can be replaced with corresponding good letters, and character "*" can be replaced with empty string, and the strings will coincide. The third query: "NO", because characters "?" can't be replaced with bad letters. The fourth query: "YES", because characters "?" can be replaced with good letters "a", and character "*" can be replaced with a string of bad letters "x".
{"inputs": ["a\nab\n1\na\n", "a\na\n1\nab\n", "b\naa\n1\na\n", "c\na\n1\nab\n", "a\na\n1\nab\n", "b\naa\n1\na\n", "c\na\n1\nab\n", "a\nab\n1\na\n"], "outputs": ["NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n", "NO\n"]}
678
118
coding
Solve the programming task below in a Python markdown code block. Chef has been working in a restaurant which has N floors. He wants to minimize the time it takes him to go from the N-th floor to ground floor. He can either take the elevator or the stairs. The stairs are at an angle of 45 degrees and Chef's velocity is V1 m/s when taking the stairs down. The elevator on the other hand moves with a velocity V2 m/s. Whenever an elevator is called, it always starts from ground floor and goes to N-th floor where it collects Chef (collecting takes no time), it then makes its way down to the ground floor with Chef in it. The elevator cross a total distance equal to N meters when going from N-th floor to ground floor or vice versa, while the length of the stairs is sqrt(2) * N because the stairs is at angle 45 degrees. Chef has enlisted your help to decide whether he should use stairs or the elevator to minimize his travel time. Can you help him out? -----Input----- The first line contains a single integer T, the number of test cases. Each test case is described by a single line containing three space-separated integers N, V1, V2. -----Output----- For each test case, output a single line with string Elevator or Stairs, denoting the answer to the problem. -----Constraints----- - 1 ≤ T ≤ 1000 - 1 ≤ N, V1, V2 ≤ 100 -----Example----- Input: 3 5 10 15 2 10 14 7 14 10 Output: Elevator Stairs Stairs
{"inputs": ["3\n5 10 15\n2 10 14\n7 14 10"], "outputs": ["Elevator\nStairs\nStairs"]}
362
43
coding
Solve the programming task below in a Python markdown code block. You are given a prime number p, n integers a_1, a_2, …, a_n, and an integer k. Find the number of pairs of indexes (i, j) (1 ≤ i < j ≤ n) for which (a_i + a_j)(a_i^2 + a_j^2) ≡ k mod p. Input The first line contains integers n, p, k (2 ≤ n ≤ 3 ⋅ 10^5, 2 ≤ p ≤ 10^9, 0 ≤ k ≤ p-1). p is guaranteed to be prime. The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ p-1). It is guaranteed that all elements are different. Output Output a single integer — answer to the problem. Examples Input 3 3 0 0 1 2 Output 1 Input 6 7 2 1 2 3 4 5 6 Output 3 Note In the first example: (0+1)(0^2 + 1^2) = 1 ≡ 1 mod 3. (0+2)(0^2 + 2^2) = 8 ≡ 2 mod 3. (1+2)(1^2 + 2^2) = 15 ≡ 0 mod 3. So only 1 pair satisfies the condition. In the second example, there are 3 such pairs: (1, 5), (2, 3), (4, 6).
{"inputs": ["2 2 1\n1 0\n", "2 2 0\n1 0\n", "2 2 2\n1 0\n", "2 3 2\n1 0\n", "2 4 0\n1 0\n", "2 37 0\n3 1\n", "2 56 0\n3 1\n", "2 56 0\n6 1\n"], "outputs": ["1", "0", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
358
135
coding
Solve the programming task below in a Python markdown code block. # Summation Of Primes The sum of the primes below or equal to 10 is **2 + 3 + 5 + 7 = 17**. Find the sum of all the primes **_below or equal to the number passed in_**. From Project Euler's [Problem #10](https://projecteuler.net/problem=10 "Project Euler - Problem 10"). Also feel free to reuse/extend the following starter code: ```python def summationOfPrimes(primes): ```
{"functional": "_inputs = [[5], [6], [7], [8], [9], [10], [20], [30], [40], [50], [100], [200], [300], [400], [500], [1000], [2000], [3000], [4000], [5000], [25000]]\n_outputs = [[10], [10], [17], [17], [17], [17], [77], [129], [197], [328], [1060], [4227], [8275], [13887], [21536], [76127], [277050], [593823], [1013507], [1548136], [32405717]]\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(summationOfPrimes(*i), o[0])"}
125
375
coding
Solve the programming task below in a Python markdown code block. Consider an array A of length N. You know that for all 1 ≤ i ≤ N, 0 ≤ A_{i} ≤ 10^{5}. We construct an array B of length N-1 such that, for all 1 ≤ i ≤ N - 1, B_{i} = min(A_{i}, A_{i + 1}). You are given the array B, you need to find out the total number of distinct arrays A which we could have used to construct the given array B. The answer may be large, so you need to find it modulo 10^{9} + 7. ------ Input Format ------ - First line will contain T, number of testcases. Then the testcases follow. - The first line of each test case contains a single integer N - The second line of each test case contains N-1 space separated integers - the i^{th} of which is B_{i} ------ Output Format ------ For each testcase(in a new line), output the count of valid possibilities of array A modulo 10^{9} + 7. ------ Constraints ------ $1 ≤ T ≤ 10$ $2 ≤ N ≤ 10^{5}$ $0 ≤ B_{i} ≤ 10^{5}$ ----- Sample Input 1 ------ 3 2 100 5 3 9 8 4 3 10 12 ----- Sample Output 1 ------ 199801 199983 199977 ----- explanation 1 ------ Test Case $1$: All valid arrays $A$ are of the form $[100, x]$ and $[x, 100]$ where $100 ≤ x ≤ 10^{5}$. Therefore, the answer is $(10^{5} - 100) + (10^{5} - 100 + 1) = 199801$. Test Case $3$: All valid arrays $A$ are of the form $[10, 12, x]$ and $[10, x, 12]$ where $12 ≤ x ≤ 10^{5}$. Therefore, the answer is $(10^{5} - 12) + (10^{5} - 12 + 1) = 199977$.
{"inputs": ["3\n2\n100\n5\n3 9 8 4\n3\n10 12"], "outputs": ["199801\n199983\n199977"]}
532
55
coding
Solve the programming task below in a Python markdown code block. There are 2000001 stones placed on a number line. The coordinates of these stones are -1000000, -999999, -999998, \ldots, 999999, 1000000. Among them, some K consecutive stones are painted black, and the others are painted white. Additionally, we know that the stone at coordinate X is painted black. Print all coordinates that potentially contain a stone painted black, in ascending order. -----Constraints----- - 1 \leq K \leq 100 - 0 \leq X \leq 100 - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: K X -----Output----- Print all coordinates that potentially contain a stone painted black, in ascending order, with spaces in between. -----Sample Input----- 3 7 -----Sample Output----- 5 6 7 8 9 We know that there are three stones painted black, and the stone at coordinate 7 is painted black. There are three possible cases: - The three stones painted black are placed at coordinates 5, 6, and 7. - The three stones painted black are placed at coordinates 6, 7, and 8. - The three stones painted black are placed at coordinates 7, 8, and 9. Thus, five coordinates potentially contain a stone painted black: 5, 6, 7, 8, and 9.
{"inputs": ["9 7", "5 5", "9 3", "2 4", "2 6", "1 4", "4 7", "6 5"], "outputs": ["-1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15\n", "1 2 3 4 5 6 7 8 9\n", "-5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11\n", "3 4 5\n", "5 6 7\n", "4\n", "4 5 6 7 8 9 10\n", "0 1 2 3 4 5 6 7 8 9 10\n"]}
346
209
coding
Solve the programming task below in a Python markdown code block. The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below. The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1, a2, ..., ak (i.e. some prefix of the sequence) equal to zero for some fixed k (k < n), and this should be done in the smallest possible number of moves. One move is choosing an integer i (1 ≤ i ≤ n) such that ai > 0 and an integer t (t ≥ 0) such that i + 2t ≤ n. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of ai + 2t is increased by 1. For example, let n = 4 and a = (1, 0, 1, 2), then it is possible to make move i = 3, t = 0 and get a = (1, 0, 0, 3) or to make move i = 1, t = 1 and get a = (0, 0, 2, 2) (the only possible other move is i = 1, t = 0). You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1 ≤ k < n). Input The first input line contains a single integer n. The second line contains n integers ai (0 ≤ ai ≤ 104), separated by single spaces. The input limitations for getting 20 points are: * 1 ≤ n ≤ 300 The input limitations for getting 50 points are: * 1 ≤ n ≤ 2000 The input limitations for getting 100 points are: * 1 ≤ n ≤ 105 Output Print exactly n - 1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams, or the %I64d specifier. Examples Input 4 1 0 1 2 Output 1 1 3 Input 8 1 2 3 4 5 6 7 8 Output 1 3 6 10 16 24 40
{"inputs": ["4\n0 0 1 2\n", "4\n1 0 1 2\n", "5\n4 1 4 7 6\n", "5\n4 1 1 7 6\n", "5\n4 1 0 1 2\n", "5\n4 2 0 1 2\n", "5\n8 2 0 1 2\n", "5\n4 1 1 7 2\n"], "outputs": ["0\n0\n1\n", "1\n1\n3\n", "4\n5\n9\n17\n", "4\n5\n6\n14\n", "4\n5\n5\n7\n", "4\n6\n6\n9\n", "8\n10\n10\n13\n", "4\n5\n6\n14\n"]}
583
196
coding
Solve the programming task below in a Python markdown code block. There is a tree with N vertices numbered 1 through N. The i-th edge connects Vertex x_i and y_i. Each vertex is painted white or black. The initial color of Vertex i is represented by a letter c_i. c_i = W represents the vertex is white; c_i = B represents the vertex is black. A cat will walk along this tree. More specifically, she performs one of the following in one second repeatedly: - Choose a vertex that is adjacent to the vertex where she is currently, and move to that vertex. Then, invert the color of the destination vertex. - Invert the color of the vertex where she is currently. The cat's objective is to paint all the vertices black. She may start and end performing actions at any vertex. At least how many seconds does it takes for the cat to achieve her objective? -----Constraints----- - 1 ≤ N ≤ 10^5 - 1 ≤ x_i,y_i ≤ N (1 ≤ i ≤ N-1) - The given graph is a tree. - c_i = W or c_i = B. -----Input----- Input is given from Standard Input in the following format: N x_1 y_1 x_2 y_2 : x_{N-1} y_{N-1} c_1c_2..c_N -----Output----- Print the minimum number of seconds required to achieve the objective. -----Sample Input----- 5 1 2 2 3 2 4 4 5 WBBWW -----Sample Output----- 5 The objective can be achieved in five seconds, for example, as follows: - Start at Vertex 1. Change the color of Vertex 1 to black. - Move to Vertex 2, then change the color of Vertex 2 to white. - Change the color of Vertex 2 to black. - Move to Vertex 4, then change the color of Vertex 4 to black. - Move to Vertex 5, then change the color of Vertex 5 to black.
{"inputs": ["1\nB", "1\nB\n", "5\n1 2\n0 3\n2 4\n4 5\nWBBWW", "5\n1 2\n2 3\n2 5\n4 5\nWBBWW", "5\n1 2\n1 3\n2 4\n4 5\nWBBWW", "5\n1 3\n2 3\n2 5\n4 5\nWBBWW", "5\n1 2\n1 3\n3 4\n4 5\nWBBWW", "5\n1 2\n2 3\n2 4\n4 5\nWBBWW"], "outputs": ["0", "0\n", "5\n", "5\n", "5\n", "7\n", "5\n", "5"]}
435
185
coding
Solve the programming task below in a Python markdown code block. If you have completed the Tribonacci sequence kata, you would know by now that mister Fibonacci has at least a bigger brother. If not, give it a quick look to get how things work. Well, time to expand the family a little more: think of a Quadribonacci starting with a signature of 4 elements and each following element is the sum of the 4 previous, a Pentabonacci (well *Cinquebonacci* would probably sound a bit more italian, but it would also sound really awful) with a signature of 5 elements and each following element is the sum of the 5 previous, and so on. Well, guess what? You have to build a Xbonacci function that takes a **signature** of X elements *- and remember each next element is the sum of the last X elements -* and returns the first **n** elements of the so seeded sequence. ``` xbonacci {1,1,1,1} 10 = {1,1,1,1,4,7,13,25,49,94} xbonacci {0,0,0,0,1} 10 = {0,0,0,0,1,1,2,4,8,16} xbonacci {1,0,0,0,0,0,1} 10 = {1,0,0,0,0,0,1,2,3,6} xbonacci {1,1} produces the Fibonacci sequence ``` Also feel free to reuse/extend the following starter code: ```python def Xbonacci(signature,n): ```
{"functional": "_inputs = [[[0, 1], 10], [[1, 1], 10], [[0, 0, 0, 0, 1], 10], [[1, 0, 0, 0, 0, 0, 1], 10], [[1, 0, 0, 0, 0, 0, 0, 0, 0, 0], 20], [[0.5, 0, 0, 0, 0, 0, 0, 0, 0, 0], 10], [[0.5, 0, 0, 0, 0, 0, 0, 0, 0, 0], 20], [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 20], [[1, 2, 3, 4, 5, 6, 7, 8, 9, 0], 9], [[1, 2, 3, 4, 5, 6, 7, 8, 9, 0], 0]]\n_outputs = [[[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]], [[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]], [[0, 0, 0, 0, 1, 1, 2, 4, 8, 16]], [[1, 0, 0, 0, 0, 0, 1, 2, 3, 6]], [[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 32, 64, 128, 256]], [[0.5, 0, 0, 0, 0, 0, 0, 0, 0, 0]], [[0.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5, 0.5, 1, 2, 4, 8, 16, 32, 64, 128]], [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], [[1, 2, 3, 4, 5, 6, 7, 8, 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(Xbonacci(*i), o[0])"}
361
806
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. Little Egor is a huge movie fan. He likes watching different kinds of movies: from drama movies to comedy movies, from teen movies to horror movies. He is planning to visit cinema this weekend, but he's not sure which movie he should watch. There are n movies to watch during this weekend. Each movie can be characterized by two integers L_{i} and R_{i}, denoting the length and the rating of the corresponding movie. Egor wants to watch exactly one movie with the maximal value of L_{i} × R_{i}. If there are several such movies, he would pick a one with the maximal R_{i} among them. If there is still a tie, he would pick the one with the minimal index among them. Your task is to help Egor to pick a movie to watch during this weekend. ------ Input ------ The first line of the input contains an integer T denoting the number of test cases. The first line of the test case description contains an integer n. The second line of the test case description contains n integers L_{1}, L_{2}, ...,L_{n}. The following line contains n integers R_{1}, R_{2}, ..., R_{n}. ------ Output ------ For each test case, output a single integer i denoting the index of the movie that Egor should watch during this weekend. Note that we follow 1-based indexing. ------ Constraints ------ $1 ≤ T ≤ 5$ $1 ≤ n ≤ 100$ $1 ≤ L_{i}, R_{i} ≤ 100$ ----- Sample Input 1 ------ 2 2 1 2 2 1 4 2 1 4 1 2 4 1 4 ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ In the first example case, both films have the same value of L × R, but the first film has a better rating. In the second example case, the second and the fourth movies are equally good, but the second movie has a smaller index.
{"inputs": ["2\n2\n1 2\n2 1\n4\n2 1 4 1\n2 4 1 4", "2\n2\n1 2\n2 1\n4\n2 1 4 1\n2 5 1 4", "2\n2\n2 2\n2 1\n4\n2 1 4 1\n4 4 1 4", "2\n2\n1 2\n2 1\n4\n1 1 4 1\n2 5 1 4", "2\n2\n2 2\n2 1\n4\n2 1 4 1\n2 4 1 4", "2\n2\n2 2\n2 1\n4\n2 1 4 1\n1 4 1 4", "2\n2\n1 2\n2 1\n4\n1 1 1 1\n2 5 1 4", "2\n2\n2 2\n2 1\n3\n2 1 4 1\n1 4 1 4"], "outputs": ["1\n2", "1\n2\n", "1\n1\n", "1\n2\n", "1\n2\n", "1\n2\n", "1\n2\n", "1\n2\n"]}
459
301
coding
Solve the programming task below in a Python markdown code block. You are taking a computer-based examination. The examination consists of N questions, and the score allocated to the i-th question is s_i. Your answer to each question will be judged as either "correct" or "incorrect", and your grade will be the sum of the points allocated to questions that are answered correctly. When you finish answering the questions, your answers will be immediately judged and your grade will be displayed... if everything goes well. However, the examination system is actually flawed, and if your grade is a multiple of 10, the system displays 0 as your grade. Otherwise, your grade is displayed correctly. In this situation, what is the maximum value that can be displayed as your grade? -----Constraints----- - All input values are integers. - 1 ≤ N ≤ 100 - 1 ≤ s_i ≤ 100 -----Input----- Input is given from Standard Input in the following format: N s_1 s_2 : s_N -----Output----- Print the maximum value that can be displayed as your grade. -----Sample Input----- 3 5 10 15 -----Sample Output----- 25 Your grade will be 25 if the 10-point and 15-point questions are answered correctly and the 5-point question is not, and this grade will be displayed correctly. Your grade will become 30 if the 5-point question is also answered correctly, but this grade will be incorrectly displayed as 0.
{"inputs": ["3\n2\n0\n2", "3\n2\n1\n2", "3\n0\n0\n3", "3\n1\n8\n1", "3\n2\n8\n0", "3\n2\n3\n1", "3\n2\n3\n2", "3\n2\n8\n2"], "outputs": ["4\n", "5\n", "3\n", "9\n", "8\n", "6\n", "7\n", "12\n"]}
321
111
coding
Solve the programming task below in a Python markdown code block. # Write Number in Expanded Form - Part 2 This is version 2 of my ['Write Number in Exanded Form' Kata](https://www.codewars.com/kata/write-number-in-expanded-form). You will be given a number and you will need to return it as a string in [Expanded Form](https://www.mathplacementreview.com/arithmetic/decimals.php#writing-a-decimal-in-expanded-form). For example: ```python expanded_form(1.24) # Should return '1 + 2/10 + 4/100' expanded_form(7.304) # Should return '7 + 3/10 + 4/1000' expanded_form(0.04) # Should return '4/100' ``` Also feel free to reuse/extend the following starter code: ```python def expanded_form(num): ```
{"functional": "_inputs = [[1.24], [7.304], [0.04], [1.04], [7.3004], [0.004], [693.230459]]\n_outputs = [['1 + 2/10 + 4/100'], ['7 + 3/10 + 4/1000'], ['4/100'], ['1 + 4/100'], ['7 + 3/10 + 4/10000'], ['4/1000'], ['600 + 90 + 3 + 2/10 + 3/100 + 4/10000 + 5/100000 + 9/1000000']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(expanded_form(*i), o[0])"}
202
332
coding
Solve the programming task below in a Python markdown code block. Two integers A and B are the inputs. Write a program to find GCD and LCM of A and B. -----Input----- The first line contains an integer T, total number of testcases. Then follow T lines, each line contains an integer A and B. -----Output----- Display the GCD and LCM of A and B separated by space respectively. The answer for each test case must be displayed in a new line. -----Constraints----- - 1 ≤ T ≤ 1000 - 1 ≤ A,B ≤ 1000000 -----Example----- Input 3 120 140 10213 312 10 30 Output 20 840 1 3186456 10 30
{"inputs": ["3\n120 140\n10213 312\n10 30"], "outputs": ["20 840\n1 3186456\n10 30"]}
188
57
coding
Solve the programming task below in a Python markdown code block. Problem Statement We can describe detailed direction by repeating the directional names: north, south, east and west. For example, northwest is the direction halfway between north and west, and northnorthwest is between north and northwest. In this problem, we describe more detailed direction between north and west as follows. * "north" means $0$ degrees. * "west" means $90$ degrees. * If the direction $dir$ means $a$ degrees and the sum of the occurrences of "north" and "west" in $dir$ is $n$ ($\geq$ 1), "north"$dir$ (the concatenation of "north" and $dir$) means $a - \frac{90}{2^n}$ degrees and "west"$dir$ means $a + \frac{90}{2^n}$ degrees. Your task is to calculate the angle in degrees described by the given direction. * * * Input The input contains several datasets. The number of datasets does not exceed $100$. Each dataset is described by a single line that contains a string denoting a direction. You may assume the given string can be obtained by concatenating some "north" and "west", the sum of the occurrences of "north" and "west" in the given string is between $1$ and $20$, inclusive, and the angle denoted by the given direction is between $0$ and $90$, inclusive. The final dataset is followed by a single line containing only a single "#". Output For each dataset, print an integer if the angle described by the given direction can be represented as an integer, otherwise print it as an irreducible fraction. Follow the format of the sample output. * * * Sample Input north west northwest northnorthwest westwestwestnorth Output for the Sample Input 0 90 45 45/2 315/4 Example Input north west northwest northnorthwest westwestwestnorth # Output 0 90 45 45/2 315/4
{"inputs": ["north\nwest\nnorthwest\nnorthnorthwest\nwestwestwestnorth\n#"], "outputs": ["0\n90\n45\n45/2\n315/4"]}
461
45
coding
Solve the programming task below in a Python markdown code block. We have a country containing _N _cities. Each day we choose 2 cities such that there is no road between them and build a road between them. We choose each pair of nonadjacent cities with equal probability. Let X be the number of days before we obtain a connected country. What is the expected value of X? Output the integer part of answer. Input Format First line of input as an integer N. Constraints $N<=30$ Output Format Print an integer being the result of the test. Sample Input 0 3 Sample Output 0 2 Explanation 0 In the first example, first two roads are sufficient for connecting the cities so the answer would be 2. Sample Input 1 4 Sample Output 1 3 Explanation 1 In the second example if the first three roads of the country are edges of a triangle, then we need a fourth road to make the country connected, otherwise the country would be connected with first three roads. The probability of the former situation is 4/20 (number of triple of roads that make a triangle divided by number of ways we can choose 3 different roads), and the probability of later situation is 16/20. So the result would be 4/20*4 + 16/20*3 = 3.2 and since you have to print only the integer part as output, print 3
{"inputs": ["3\n", "4\n"], "outputs": ["2\n", "3\n"]}
311
22
coding
Solve the programming task below in a Python markdown code block. Gematria is an Assyro-Babylonian-Greek system of code and numerology later adopted into Jewish culture. The system assigns numerical value to a word or a phrase in the belief that words or phrases with identical numerical values bear some relation to each other or bear some relation to the number itself. While more commonly used on Hebrew words, there is also an English version. Each letter has a value and the gematrian value of a word or a phrase is the sum of those values. The code takes a word or an expression and returns the gematrian value of it. The calculation is case insensitive and counts no spaces. Example: The gematrian value of "love" is 20+50+700+5 = 775 ‎These are the values of the different letters: a=1, b=2, c=3, d=4, e=5, f=6, g=7, h=8, i=9, k=10, l=20, m=30, n=40, o=50, p=60, q=70, r=80, s=90, t=100, u=200, x=300, y=400, z=500, j=600, v=700, w=900 Also feel free to reuse/extend the following starter code: ```python def gematria(string): ```
{"functional": "_inputs = [['love'], ['jaels'], ['JAELS'], ['Devil'], ['Coding is fun']]\n_outputs = [[775], [716], [716], [738], [458]]\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(gematria(*i), o[0])"}
334
195
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well. Let's define a function F(X) as follows: F(X) = \frac{X}{Y} where Y is the largest perfect square that divides X. For example, The largest perfect square that divides 12 is 4. Hence F(12) = \frac{12}{4} = 3. The largest perfect square that divides 36 is 36. Hence F(36) = \frac{36}{36} = 1. The largest perfect square that divides 7 is 1. Hence F(7) = \frac{7}{1} = 7. You are given an array A consisting of N integers. A pair of integer (i, j) is called *Good* if 1 ≤ i < j ≤ N and F(A_{i} * A_{j}) > 1. Find the number of *Good* pairs. ------ Input Format ------ - The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains an integer N. - The second line of each testcase contains N space-separated integers A_{1}, A_{2}, \dots , A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the number of *Good* pairs. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{6}$ - The sum of $N$ over all test cases does not exceed $10^{6}$. ----- Sample Input 1 ------ 3 3 2 3 12 4 1 2 3 4 3 2 3 7 ----- Sample Output 1 ------ 2 5 3 ----- explanation 1 ------ Test case $1$: - $(i = 1, j = 2)$: $F(A_{1} * A_{2}) = F(2 * 3) = F(6) = \frac{6}{1} = 6 > 1$. - $(i = 1, j = 3)$: $F(A_{1} * A_{3}) = F(2 *12) = F(24) = \frac{24}{4} = 6 > 1$. - $(i = 2, j = 3)$: $F(A_{2} * A_{3}) = F(3 *12) = F(36) = \frac{36}{36} = 1 \ngtr 1$. So there are 2 Good pairs. Test case $2$: All pairs except ($1, 4$) are Good pairs as $F(A_{1} * A_{4}) = F(1 * 4) = F(4) = \frac{4}{4} = 1 \ngtr 1$. Test case $3$: All pairs are Good pairs.
{"inputs": ["3\n3\n2 3 12\n4 \n1 2 3 4\n3\n2 3 7"], "outputs": ["2\n5\n3"]}
707
44
coding
Solve the programming task below in a Python markdown code block. Consider a number X on which K Mag-Inc operations are to be performed. In a Mag-Inc operation, the number X undergoes an increment of A/B times of X where A and B are two integers. There is a numerator and a denominator array of size K which contain the ith values of A and B. After K Mag-Inc operations, the number X turns to M. Now your job is to find what percentage of M is to be decremented from M if it has to be converted back to X. Let this percentage be denoted by Z. Print the integral part of Z. -----Input:----- First line contains an integer T denoting the number of test cases. First line of every test case contains two space separated integers X and K. The second and third line of every test case will contain K space separated integers denoting the Numerator and Denominator array. -----Output:----- For each test case, print the required result in a single line. -----Constraints:----- 1 ≤ T ≤ 100 1 ≤ K, A, B ≤ 40000 1≤X≤10^100 -----Example:-----Input: 2 100 1 1 4 100 2 1 1 2 3Output: 20 50 -----Explanation:----- Case 2: 100 undergoes an increment of (1/2)*100. Therefore M = 100 + 50. Now M = 150. Now again, M undergoes an increment of (1/3)*150. Therefore, M = 150 + 50. Now as we want to revert back M = 200 to X i.e. 100, we need to decrement it by a value of 100 and we know that 100 is 50% of 200. Hence, we print 50.
{"inputs": ["2\n100 1\n1\n4\n100 2\n1 1\n2 3"], "outputs": ["20\n50"]}
434
40
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a positive integer n representing n cities numbered from 1 to n. You are also given a 2D array roads where roads[i] = [ai, bi, distancei] indicates that there is a bidirectional road between cities ai and bi with a distance equal to distancei. The cities graph is not necessarily connected. The score of a path between two cities is defined as the minimum distance of a road in this path. Return the minimum possible score of a path between cities 1 and n. Note: A path is a sequence of roads between two cities. It is allowed for a path to contain the same road multiple times, and you can visit cities 1 and n multiple times along the path. The test cases are generated such that there is at least one path between 1 and n.   Please complete the following python code precisely: ```python class Solution: def minScore(self, n: int, roads: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(n = 4, roads = [[1,2,9],[2,3,6],[2,4,5],[1,4,7]]) == 5\n assert candidate(n = 4, roads = [[1,2,2],[1,3,4],[3,4,7]]) == 2\n\n\ncheck(Solution().minScore)"}
219
91
coding
Solve the programming task below in a Python markdown code block. You are given an array $a$ consisting of $n$ integers. Each $a_i$ is one of the six following numbers: $4, 8, 15, 16, 23, 42$. Your task is to remove the minimum number of elements to make this array good. An array of length $k$ is called good if $k$ is divisible by $6$ and it is possible to split it into $\frac{k}{6}$ subsequences $4, 8, 15, 16, 23, 42$. Examples of good arrays: $[4, 8, 15, 16, 23, 42]$ (the whole array is a required sequence); $[4, 8, 4, 15, 16, 8, 23, 15, 16, 42, 23, 42]$ (the first sequence is formed from first, second, fourth, fifth, seventh and tenth elements and the second one is formed from remaining elements); $[]$ (the empty array is good). Examples of bad arrays: $[4, 8, 15, 16, 42, 23]$ (the order of elements should be exactly $4, 8, 15, 16, 23, 42$); $[4, 8, 15, 16, 23, 42, 4]$ (the length of the array is not divisible by $6$); $[4, 8, 15, 16, 23, 42, 4, 8, 15, 16, 23, 23]$ (the first sequence can be formed from first six elements but the remaining array cannot form the required sequence). -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 5 \cdot 10^5$) — the number of elements in $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ (each $a_i$ is one of the following numbers: $4, 8, 15, 16, 23, 42$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer — the minimum number of elements you have to remove to obtain a good array. -----Examples----- Input 5 4 8 15 16 23 Output 5 Input 12 4 8 4 15 16 8 23 15 16 42 23 42 Output 0 Input 15 4 8 4 8 15 16 8 16 23 15 16 4 42 23 42 Output 3
{"inputs": ["1\n4\n", "1\n4\n", "1\n8\n", "1\n42\n", "1\n42\n", "1\n15\n", "1\n16\n", "1\n23\n"], "outputs": ["1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n"]}
690
91
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given two strings word1 and word2, return the minimum number of steps required to make word1 and word2 the same. In one step, you can delete exactly one character in either string.   Please complete the following python code precisely: ```python class Solution: def minDistance(self, word1: str, word2: str) -> int: ```
{"functional": "def check(candidate):\n assert candidate(word1 = \"leetcode\", word2 = \"etco\") == 4\n\n\ncheck(Solution().minDistance)"}
93
39
coding
Solve the programming task below in a Python markdown code block. Vasya used to be an accountant before the war began and he is one of the few who knows how to operate a computer, so he was assigned as the programmer. We all know that programs often store sets of integers. For example, if we have a problem about a weighted directed graph, its edge can be represented by three integers: the number of the starting vertex, the number of the final vertex and the edge's weight. So, as Vasya was trying to represent characteristics of a recently invented robot in his program, he faced the following problem. Vasya is not a programmer, so he asked his friend Gena, what the convenient way to store n integers is. Gena used to code in language X-- and so he can use only the types that occur in this language. Let's define, what a "type" is in language X--: * First, a type is a string "int". * Second, a type is a string that starts with "pair", then followed by angle brackets listing exactly two comma-separated other types of language X--. This record contains no spaces. * No other strings can be regarded as types. More formally: type := int | pair<type,type>. For example, Gena uses the following type for graph edges: pair<int,pair<int,int>>. Gena was pleased to help Vasya, he dictated to Vasya a type of language X--, that stores n integers. Unfortunately, Gena was in a hurry, so he omitted the punctuation. Now Gena has already left and Vasya can't find the correct punctuation, resulting in a type of language X--, however hard he tries. Help Vasya and add the punctuation marks so as to receive the valid type of language X--. Otherwise say that the task is impossible to perform. Input The first line contains a single integer n (1 ≤ n ≤ 105), showing how many numbers the type dictated by Gena contains. The second line contains space-separated words, said by Gena. Each of them is either "pair" or "int" (without the quotes). It is guaranteed that the total number of words does not exceed 105 and that among all the words that Gena said, there are exactly n words "int". Output If it is possible to add the punctuation marks so as to get a correct type of language X-- as a result, print a single line that represents the resulting type. Otherwise, print "Error occurred" (without the quotes). Inside the record of a type should not be any extra spaces and other characters. It is guaranteed that if such type exists, then it is unique. Note that you should print the type dictated by Gena (if such type exists) and not any type that can contain n values. Examples Input 3 pair pair int int int Output pair&lt;pair&lt;int,int&gt;,int&gt; Input 1 pair int Output Error occurred
{"inputs": ["1\nint\n", "2\nint int\n", "1\npair iot\n", "1\npair ins\n", "1\npair int\n", "2\npair int int\n", "2\nint pair int\n", "1\nint pair pair\n"], "outputs": ["int\n", "Error occurred\n", "Error occurred\n", "Error occurred\n", "Error occurred\n", "pair<int,int>\n", "Error occurred\n", "Error occurred\n"]}
632
106
coding
Solve the programming task below in a Python markdown code block. Subodh is having N branches, where each branches have positive integral student. A minimize operation is performed on the branch such that all of them are reduced by the minimum number student in a branch. Suppose we have 5 branches and all of them have below students in a branch. 5 2 4 2 6 Then in one minimize operation we reduce all the branch by 2 students. For next minimize operation 3 branch are left (have non-zero student), whose value are 3 2 4 Above step is repeated till no students are left in all branches. Given students of N branches, print the number of student that are reduce in subsequent minimize operation. Input Format The first line contains a single integer N. The next line contains N integers : X(0), X(1), X(2),..... X(N-1) separated by space, where X(i) represents the student of ith branch. Output Format For each minimize operation, print the number of students that are reduced in branches in separate line. Constraints 1 ≤ N ≤ 1000 1 ≤ X(i) ≤ 1000 SAMPLE INPUT 6 5 4 4 2 2 8 SAMPLE OUTPUT 6 4 2 1
{"inputs": ["6\n5 4 4 2 2 8"], "outputs": ["6\n4\n2\n1"]}
278
30
coding
Solve the programming task below in a Python markdown code block. You are a skier (marked below by the `X`). You have made it to the Olympics! Well done. ``` \_\_\_X\_ \*\*\*\*\*\ \*\*\*\*\*\*\ \*\*\*\*\*\*\*\ \*\*\*\*\*\*\*\*\ \*\*\*\*\*\*\*\*\*\\.\_\_\_\_/ ``` Your job in this kata is to calculate the maximum speed you will achieve during your downhill run. The speed is dictated by the height of the mountain. Each element of the array is a layer of the mountain as indicated by the diagram above (and further below). So for this example the mountain has a height of 5 (5 rows of stars). `Speed` is `mountain height * 1.5`. The jump length is calculated by `(mountain height * speed * 9) / 10`. Jump length should be rounded to 2 decimal places. You must return the length of the resulting jump as a string in the following format: * when less than 10 m: `"X metres: He's crap!"` * between 10 and 25 m: `"X metres: He's ok!"` * between 25 and 50 m: `"X metres: He's flying!"` * when more than 50 m: `"X metres: Gold!!"` So in the example case above, the right answer would be `"33.75 metres: He's flying!"` Sadly, it takes a lot of time to make arrays look like mountains, so the tests wont all look so nice. To give an example, the above mountain would look as follows in most cases: ``` [*****, ******, *******, ********, *********] ``` Not as much fun, eh? *p.s. if you think "metre" is incorrect, please [read this](https://en.wikipedia.org/wiki/Metre#Spelling)* Also feel free to reuse/extend the following starter code: ```python def ski_jump(mountain): ```
{"functional": "_inputs = [[['*']], [['*', '**', '***']], [['*', '**', '***', '****', '*****', '******']], [['*', '**', '***', '****', '*****', '******', '*******', '********']]]\n_outputs = [[\"1.35 metres: He's crap!\"], [\"12.15 metres: He's ok!\"], [\"48.60 metres: He's flying!\"], ['86.40 metres: Gold!!']]\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(ski_jump(*i), o[0])"}
449
254
coding
Solve the programming task below in a Python markdown code block. Given a string of words, you need to find the highest scoring word. Each letter of a word scores points according to its position in the alphabet: `a = 1, b = 2, c = 3` etc. You need to return the highest scoring word as a string. If two words score the same, return the word that appears earliest in the original string. All letters will be lowercase and all inputs will be valid. Also feel free to reuse/extend the following starter code: ```python def high(x): ```
{"functional": "_inputs = [['man i need a taxi up to ubud'], ['what time are we climbing up the volcano'], ['take me to semynak'], ['massage yes massage yes massage'], ['take two bintang and a dance please']]\n_outputs = [['taxi'], ['volcano'], ['semynak'], ['massage'], ['bintang']]\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(high(*i), o[0])"}
123
215
coding
Solve the programming task below in a Python markdown code block. Maxim wants to buy an apartment in a new house at Line Avenue of Metropolis. The house has n apartments that are numbered from 1 to n and are arranged in a row. Two apartments are adjacent if their indices differ by 1. Some of the apartments can already be inhabited, others are available for sale. Maxim often visits his neighbors, so apartment is good for him if it is available for sale and there is at least one already inhabited apartment adjacent to it. Maxim knows that there are exactly k already inhabited apartments, but he doesn't know their indices yet. Find out what could be the minimum possible and the maximum possible number of apartments that are good for Maxim. -----Input----- The only line of the input contains two integers: n and k (1 ≤ n ≤ 10^9, 0 ≤ k ≤ n). -----Output----- Print the minimum possible and the maximum possible number of apartments good for Maxim. -----Example----- Input 6 3 Output 1 3 -----Note----- In the sample test, the number of good apartments could be minimum possible if, for example, apartments with indices 1, 2 and 3 were inhabited. In this case only apartment 4 is good. The maximum possible number could be, for example, if apartments with indices 1, 3 and 5 were inhabited. In this case all other apartments: 2, 4 and 6 are good.
{"inputs": ["6 3\n", "8 0\n", "8 8\n", "1 0\n", "1 1\n", "2 0\n", "2 1\n", "2 2\n"], "outputs": ["1 3\n", "0 0\n", "0 0\n", "0 0\n", "0 0\n", "0 0\n", "1 1\n", "0 0\n"]}
307
102
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. A tuple $(a, b, c)$ is considered good if it consists of three prime numbers $a$, $b$ and $c$ such that $a < b < c ≤ N$ and $a + b = c$. Two tuples are considered different if they differ in at least one position. Given $N$, find the number of good tuples that can be formed by integers from $1$ to $N$. ------ 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 following $T$ lines contain a single integer $N$. ------ Output ------ For each test case, print a single line containing one integer — the number of good tuples which can be formed using integers from $1$ to $N$. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{6}$ ----- Sample Input 1 ------ 2 3 6 ----- Sample Output 1 ------ 0 1 ----- explanation 1 ------ Example case 1: There are no good tuples. $(1, 2, 3)$ is not a good tuple since $1$ is not prime. Example case 2: $(2, 3, 5)$ is a good tuple since $2$, $3$ and $5$ are prime and $2 + 3 = 5$.
{"inputs": ["2\n3\n6"], "outputs": ["0\n1"]}
352
18
coding
Solve the programming task below in a Python markdown code block. Kiyora has $n$ whiteboards numbered from $1$ to $n$. Initially, the $i$-th whiteboard has the integer $a_i$ written on it. Koxia performs $m$ operations. The $j$-th operation is to choose one of the whiteboards and change the integer written on it to $b_j$. Find the maximum possible sum of integers written on the whiteboards after performing all $m$ operations. -----Input----- Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. The description of test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n,m \le 100$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). The third line of each test case contains $m$ integers $b_1, b_2, \ldots, b_m$ ($1 \le b_i \le 10^9$). -----Output----- For each test case, output a single integer — the maximum possible sum of integers written on whiteboards after performing all $m$ operations. -----Examples----- Input 4 3 2 1 2 3 4 5 2 3 1 2 3 4 5 1 1 100 1 5 3 1 1 1 1 1 1000000000 1000000000 1000000000 Output 12 9 1 3000000002 -----Note----- In the first test case, Koxia can perform the operations as follows: Choose the $1$-st whiteboard and rewrite the integer written on it to $b_1=4$. Choose the $2$-nd whiteboard and rewrite to $b_2=5$. After performing all operations, the numbers on the three whiteboards are $4$, $5$ and $3$ respectively, and their sum is $12$. It can be proven that this is the maximum possible sum achievable. In the second test case, Koxia can perform the operations as follows: Choose the $2$-nd whiteboard and rewrite to $b_1=3$. Choose the $1$-st whiteboard and rewrite to $b_2=4$. Choose the $2$-nd whiteboard and rewrite to $b_3=5$. The sum is $4 + 5 = 9$. It can be proven that this is the maximum possible sum achievable.
{"inputs": ["4\n3 2\n1 2 3\n4 5\n2 3\n1 2\n3 4 5\n1 1\n100\n1\n5 3\n1 1 1 1 1\n1000000000 1000000000 1000000000\n"], "outputs": ["12\n9\n1\n3000000002\n"]}
631
115