task_type
stringclasses
1 value
problem
stringlengths
209
3.39k
answer
stringlengths
35
6.15k
problem_tokens
int64
60
774
answer_tokens
int64
12
2.04k
coding
Solve the programming task below in a Python markdown code block. One day little Vasya found mom's pocket book. The book had n names of her friends and unusually enough, each name was exactly m letters long. Let's number the names from 1 to n in the order in which they are written. As mom wasn't home, Vasya decided to play with names: he chose three integers i, j, k (1 ≤ i < j ≤ n, 1 ≤ k ≤ m), then he took names number i and j and swapped their prefixes of length k. For example, if we take names "CBDAD" and "AABRD" and swap their prefixes with the length of 3, the result will be names "AABAD" and "CBDRD". You wonder how many different names Vasya can write instead of name number 1, if Vasya is allowed to perform any number of the described actions. As Vasya performs each action, he chooses numbers i, j, k independently from the previous moves and his choice is based entirely on his will. The sought number can be very large, so you should only find it modulo 1000000007 (109 + 7). Input The first input line contains two integers n and m (1 ≤ n, m ≤ 100) — the number of names and the length of each name, correspondingly. Then n lines contain names, each name consists of exactly m uppercase Latin letters. Output Print the single number — the number of different names that could end up in position number 1 in the pocket book after the applying the procedures described above. Print the number modulo 1000000007 (109 + 7). Examples Input 2 3 AAB BAA Output 4 Input 4 5 ABABA BCGDG AAAAA YABSA Output 216 Note In the first sample Vasya can get the following names in the position number 1: "AAB", "AAA", "BAA" and "BAB".
{"inputs": ["1 1\nE\n", "1 1\nD\n", "1 1\nF\n", "2 2\nAA\nBB\n", "2 2\nNS\nPD\n", "2 2\nNS\nDP\n", "2 2\nSN\nDP\n", "2 2\nSN\nCP\n"], "outputs": ["1\n", "1\n", "1\n", "4\n", "4\n", "4\n", "4\n", "4\n"]}
453
112
coding
Solve the programming task below in a Python markdown code block. In Berland, $n$ different types of banknotes are used. Banknotes of the $i$-th type have denomination $10^{a_i}$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $1$. Let's denote $f(s)$ as the minimum number of banknotes required to represent exactly $s$ burles. For example, if the denominations of banknotes used in Berland are $1$, $10$ and $100$, then $f(59) = 14$: $9$ banknotes with denomination of $1$ burle and $5$ banknotes with denomination of $10$ burles can be used to represent exactly $9 \cdot 1 + 5 \cdot 10 = 59$ burles, and there's no way to do it with fewer banknotes. For a given integer $k$, find the minimum positive number of burles $s$ that cannot be represented with $k$ or fewer banknotes (that is, $f(s) > k$). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 10; 1 \le k \le 10^9$). The next line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 = a_1 < a_2 < \dots < a_n \le 9$). -----Output----- For each test case, print one integer — the minimum positive number of burles $s$ that cannot be represented with $k$ or fewer banknotes. -----Examples----- Input 4 3 13 0 1 2 2 777 0 4 3 255 0 1 3 10 1000000000 0 1 2 3 4 5 6 7 8 9 Output 59 778 148999 999999920999999999 -----Note----- None
{"inputs": ["4\n3 1\n0 1 2\n2 777\n0 2\n3 60\n0 1 3\n10 0000000010\n0 1 2 3 4 5 6 7 8 9\n", "4\n3 1\n0 1 2\n2 777\n0 2\n3 60\n0 1 3\n10 0000000011\n0 1 2 3 4 5 6 7 8 9\n", "4\n3 2\n0 1 2\n2 777\n0 2\n3 60\n0 1 3\n10 0000000010\n0 1 2 3 4 5 6 7 8 9\n", "4\n3 2\n0 1 2\n2 983\n0 2\n3 60\n0 1 3\n10 0000000010\n0 1 2 3 4 5 6 7 8 9\n", "4\n3 2\n0 1 2\n2 235\n0 2\n3 60\n0 1 3\n10 0000000010\n0 1 2 3 4 5 6 7 8 9\n", "4\n3 1\n0 1 2\n2 777\n0 2\n3 60\n0 1 3\n10 0000000110\n0 1 2 3 4 5 6 7 8 9\n", "4\n3 19\n0 1 2\n2 777\n0 3\n3 31\n0 1 2\n3 0000000000\n0 1 2 3 4 5 6 7 8 9\n", "4\n3 1\n0 1 2\n2 641\n0 2\n3 60\n0 1 3\n10 0000000110\n0 1 2 3 4 5 6 7 8 9\n"], "outputs": ["2\n67999\n529\n29\n", "2\n67999\n529\n39\n", "3\n67999\n529\n29\n", "3\n88599\n529\n29\n", "3\n13799\n529\n29\n", "2\n67999\n529\n30999999999\n", "299\n778\n1499\n1\n", "2\n54399\n529\n30999999999\n"]}
518
712
coding
Solve the programming task below in a Python markdown code block. You are given a string $s$ consisting of lowercase Latin letters "a", "b" and "c" and question marks "?". Let the number of question marks in the string $s$ be $k$. Let's replace each question mark with one of the letters "a", "b" and "c". Here we can obtain all $3^{k}$ possible strings consisting only of letters "a", "b" and "c". For example, if $s = $"ac?b?c" then we can obtain the following strings: $[$"acabac", "acabbc", "acabcc", "acbbac", "acbbbc", "acbbcc", "accbac", "accbbc", "accbcc"$]$. Your task is to count the total number of subsequences "abc" in all resulting strings. Since the answer can be very large, print it modulo $10^{9} + 7$. A subsequence of the string $t$ is such a sequence that can be derived from the string $t$ after removing some (possibly, zero) number of letters without changing the order of remaining letters. For example, the string "baacbc" contains two subsequences "abc" — a subsequence consisting of letters at positions $(2, 5, 6)$ and a subsequence consisting of letters at positions $(3, 5, 6)$. -----Input----- The first line of the input contains one integer $n$ $(3 \le n \le 200\,000)$ — the length of $s$. The second line of the input contains the string $s$ of length $n$ consisting of lowercase Latin letters "a", "b" and "c" and question marks"?". -----Output----- Print the total number of subsequences "abc" in all strings you can obtain if you replace all question marks with letters "a", "b" and "c", modulo $10^{9} + 7$. -----Examples----- Input 6 ac?b?c Output 24 Input 7 ??????? Output 2835 Input 9 cccbbbaaa Output 0 Input 5 a???c Output 46 -----Note----- In the first example, we can obtain $9$ strings: "acabac" — there are $2$ subsequences "abc", "acabbc" — there are $4$ subsequences "abc", "acabcc" — there are $4$ subsequences "abc", "acbbac" — there are $2$ subsequences "abc", "acbbbc" — there are $3$ subsequences "abc", "acbbcc" — there are $4$ subsequences "abc", "accbac" — there is $1$ subsequence "abc", "accbbc" — there are $2$ subsequences "abc", "accbcc" — there are $2$ subsequences "abc". So, there are $2 + 4 + 4 + 2 + 3 + 4 + 1 + 2 + 2 = 24$ subsequences "abc" in total.
{"inputs": ["3\n???\n", "3\nabc\n", "3\nabc\n", "3\n???\n", "3\ncba\n", "3\nbac\n", "3\naac\n", "3\nabb\n"], "outputs": ["1\n", "1\n", "1\n", "1\n", "0\n", "0\n", "0\n", "0\n"]}
703
89
coding
Solve the programming task below in a Python markdown code block. Vasya plays the LionAge II. He was bored of playing with a stupid computer, so he installed this popular MMORPG, to fight with his friends. Vasya came up with the name of his character — non-empty string s, consisting of a lowercase Latin letters. However, in order not to put up a front of friends, Vasya has decided to change no more than k letters of the character name so that the new name sounded as good as possible. Euphony of the line is defined as follows: for each pair of adjacent letters x and y (x immediately precedes y) the bonus c(x, y) is added to the result. Your task is to determine what the greatest Euphony can be obtained by changing at most k letters in the name of the Vasya's character. Input The first line contains character's name s and an integer number k (0 ≤ k ≤ 100). The length of the nonempty string s does not exceed 100. The second line contains an integer number n (0 ≤ n ≤ 676) — amount of pairs of letters, giving bonus to the euphony. The next n lines contain description of these pairs «x y c», which means that sequence xy gives bonus c (x, y — lowercase Latin letters, - 1000 ≤ c ≤ 1000). It is guaranteed that no pair x y mentioned twice in the input data. Output Output the only number — maximum possible euphony оf the new character's name. Examples Input winner 4 4 s e 7 o s 8 l o 13 o o 8 Output 36 Input abcdef 1 5 a b -10 b c 5 c d 5 d e 5 e f 5 Output 20 Note In the first example the most euphony name will be looser. It is easy to calculate that its euphony is 36.
{"inputs": ["xd 2\n0\n", "xd 3\n0\n", "dx 3\n0\n", "jmiqoyqf 4\n0\n", "jmiqoyqf 7\n0\n", "jmiqoyqf 2\n0\n", "jmiqoyqf 0\n0\n", "xmltrs 3\n2\ne a -27\nc v -932\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
447
136
coding
Solve the programming task below in a Python markdown code block. Masha has three sticks of length $a$, $b$ and $c$ centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. -----Input----- The only line contains tree integers $a$, $b$ and $c$ ($1 \leq a, b, c \leq 100$) — the lengths of sticks Masha possesses. -----Output----- Print a single integer — the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. -----Examples----- Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 -----Note----- In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length $2$ centimeter stick by one and after that form a triangle with sides $3$, $3$ and $5$ centimeters. In the third example, Masha can take $33$ minutes to increase one of the $10$ centimeters sticks by $33$ centimeters, and after that take $48$ minutes to increase another $10$ centimeters stick by $48$ centimeters. This way she can form a triangle with lengths $43$, $58$ and $100$ centimeters in $81$ minutes. One can show that it is impossible to get a valid triangle faster.
{"inputs": ["3 4 5\n", "2 5 3\n", "1 1 1\n", "3 1 1\n", "2 3 6\n", "4 5 6\n", "4 2 7\n", "2 5 2\n"], "outputs": ["0\n", "1\n", "0\n", "2\n", "2\n", "0\n", "2\n", "2\n"]}
441
102
coding
Solve the programming task below in a Python markdown code block. We have the number ```12385```. We want to know the value of the closest cube but higher than 12385. The answer will be ```13824```. Now, another case. We have the number ```1245678```. We want to know the 5th power, closest and higher than that number. The value will be ```1419857```. We need a function ```find_next_power``` ( ```findNextPower``` in JavaScript, CoffeeScript and Haskell), that receives two arguments, a value ```val```, and the exponent of the power,``` pow_```, and outputs the value that we want to find. Let'see some cases: ```python find_next_power(12385, 3) == 13824 find_next_power(1245678, 5) == 1419857 ``` The value, ```val``` will be always a positive integer. The power, ```pow_```, always higher than ```1```. Happy coding!! Also feel free to reuse/extend the following starter code: ```python def find_next_power(val, pow_): ```
{"functional": "_inputs = [[12385, 3], [1245678, 5], [1245678, 6]]\n_outputs = [[13824], [1419857], [1771561]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(find_next_power(*i), o[0])"}
276
208
coding
Solve the programming task below in a Python markdown code block. We have N squares assigned the numbers 1,2,3,\ldots,N. Each square has an integer written on it, and the integer written on Square i is a_i. How many squares i satisfy both of the following conditions? * The assigned number, i, is odd. * The written integer is odd. Constraints * All values in input are integers. * 1 \leq N, a_i \leq 100 Input Input is given from Standard Input in the following format: N a_1 a_2 \cdots a_N Output Print the number of squares that satisfy both of the conditions. Examples Input 5 1 3 4 5 7 Output 2 Input 15 13 76 46 15 50 98 93 77 31 43 84 90 6 24 14 Output 3
{"inputs": ["5\n1 5 4 5 7", "5\n0 4 4 5 7", "5\n0 4 4 5 0", "5\n1 4 4 5 7", "5\n0 5 4 5 7", "5\n1 3 4 6 7", "5\n2 5 4 5 7", "5\n1 4 4 7 7"], "outputs": ["2\n", "1\n", "0\n", "2\n", "1\n", "2\n", "1\n", "2\n"]}
220
142
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given the array of integers nums, you will choose two different indices i and j of that array. Return the maximum value of (nums[i]-1)*(nums[j]-1).   Please complete the following python code precisely: ```python class Solution: def maxProduct(self, nums: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [3,4,5,2]) == 12 \n assert candidate(nums = [1,5,4,5]) == 16\n assert candidate(nums = [3,7]) == 12\n\n\ncheck(Solution().maxProduct)"}
84
73
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You have an inventory of different colored balls, and there is a customer that wants orders balls of any color. The customer weirdly values the colored balls. Each colored ball's value is the number of balls of that color you currently have in your inventory. For example, if you own 6 yellow balls, the customer would pay 6 for the first yellow ball. After the transaction, there are only 5 yellow balls left, so the next yellow ball is then valued at 5 (i.e., the value of the balls decreases as you sell more to the customer). You are given an integer array, inventory, where inventory[i] represents the number of balls of the ith color that you initially own. You are also given an integer orders, which represents the total number of balls that the customer wants. You can sell the balls in any order. Return the maximum total value that you can attain after selling orders colored balls. As the answer may be too large, return it modulo 109 + 7.   Please complete the following python code precisely: ```python class Solution: def maxProfit(self, inventory: List[int], orders: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(inventory = [2,5], orders = 4) == 14\n assert candidate(inventory = [3,5], orders = 6) == 19\n assert candidate(inventory = [2,8,4,10,6], orders = 20) == 110\n assert candidate(inventory = [1000000000], orders = 1000000000) == 21\n\n\ncheck(Solution().maxProfit)"}
259
128
coding
Solve the programming task below in a Python markdown code block. One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want? Input The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play. Output In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds. 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 3 3 2 2 Output 4 Input 4 2 2 2 2 Output 3 Note You don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times: http://en.wikipedia.org/wiki/Mafia_(party_game).
{"inputs": ["3\n1 1 1\n", "3\n2 1 1\n", "3\n1 2 1\n", "3\n1 1 2\n", "3\n1 1 0\n", "3\n3 4 2\n", "3\n4 5 5\n", "3\n3 4 1\n"], "outputs": ["2\n", "2\n", "2\n", "2\n", "1\n", "5\n", "7\n", "4\n"]}
329
118
coding
Solve the programming task below in a Python markdown code block. ATM machines allow 4 or 6 digit PIN codes and PIN codes cannot contain anything but **exactly** 4 digits or exactly 6 digits. If the function is passed a valid PIN string, return `true`, else return `false`. ## Examples ``` "1234" --> true "12345" --> false "a234" --> false ``` Also feel free to reuse/extend the following starter code: ```python def validate_pin(pin): ```
{"functional": "_inputs = [['1'], ['12'], ['123'], ['12345'], ['1234567'], ['-1234'], ['-12345'], ['1.234'], ['00000000'], ['a234'], ['.234'], ['1234'], ['0000'], ['1111'], ['123456'], ['098765'], ['000000'], ['090909']]\n_outputs = [[False], [False], [False], [False], [False], [False], [False], [False], [False], [False], [False], [True], [True], [True], [True], [True], [True], [True]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(validate_pin(*i), o[0])"}
126
321
coding
Solve the programming task below in a Python markdown code block. It is known that there are k fish species in the polar ocean, numbered from 1 to k. They are sorted by non-decreasing order of their weight, which is a positive number. Let the weight of the i-th type of fish be wi, then 0 < w1 ≤ w2 ≤ ... ≤ wk holds. Polar bears Alice and Bob each have caught some fish, and they are guessing who has the larger sum of weight of the fish he/she's caught. Given the type of the fish they've caught, determine whether it is possible that the fish caught by Alice has a strictly larger total weight than Bob's. In other words, does there exist a sequence of weights wi (not necessary integers), such that the fish caught by Alice has a strictly larger total weight? Input The first line contains three integers n, m, k (1 ≤ n, m ≤ 105, 1 ≤ k ≤ 109) — the number of fish caught by Alice and Bob respectively, and the number of fish species. The second line contains n integers each from 1 to k, the list of fish type caught by Alice. The third line contains m integers each from 1 to k, the list of fish type caught by Bob. Note that one may have caught more than one fish for a same species. Output Output "YES" (without quotes) if it is possible, and "NO" (without quotes) otherwise. Examples Input 3 3 3 2 2 2 1 1 3 Output YES Input 4 7 9 5 2 7 3 3 5 2 7 3 8 7 Output NO Note In the first sample, if w1 = 1, w2 = 2, w3 = 2.5, then Alice has a total of 2 + 2 + 2 = 6 weight units, while Bob only has 1 + 1 + 2.5 = 4.5. In the second sample, the fish that Alice caught is a subset of Bob's. Therefore, the total weight of Bob’s fish is always not less than the total weight of Alice’s fish.
{"inputs": ["1 1 1\n1\n1\n", "3 3 3\n2 2 4\n1 1 3\n", "3 3 3\n2 2 4\n1 1 1\n", "3 3 3\n2 2 4\n1 1 2\n", "3 3 3\n2 2 2\n1 1 3\n", "5 4 5\n1 2 2 3 4\n1 3 4 5\n", "5 5 10\n8 2 8 5 9\n9 1 7 5 1\n", "5 5 10\n8 2 8 5 1\n9 1 7 5 1\n"], "outputs": ["NO\n", "YES\n", "YES\n", "YES\n", "YES\n", "YES\n", "YES\n", "YES\n"]}
478
214
coding
Solve the programming task below in a Python markdown code block. An array is called `centered-N` if some `consecutive sequence` of elements of the array sum to `N` and this sequence is preceded and followed by the same number of elements. Example: ``` [3,2,10,4,1,6,9] is centered-15 because the sequence 10,4,1 sums to 15 and the sequence is preceded by two elements [3,2] and followed by two elements [6,9] ``` Write a method called `isCenteredN` that returns : - `true` if its array argument is `not empty` and `centered-N` or empty and centered-0 - otherwise returns `false`. Also feel free to reuse/extend the following starter code: ```python def is_centered(arr,n): ```
{"functional": "_inputs = [[[0, 0, 0], 0], [[3, 2, 10, 4, 1, 6, 9], 15], [[2, 10, 4, 1, 6, 9], 15], [[3, 2, 10, 4, 1, 6], 15], [[1, 1, 8, 3, 1, 1], 15], [[1, 1, 8, 3, 1, 1], 11], [[1, 1, 8, 3, 1, 1], 13], [[9, 0, 6], 0], [[], 25], [[25], 25], [[25, 26, 23, 24], 0], [[2, 1, 2], 0], [[1, 1, 15, -1, -1], 15], [[1, 1, 15, -1, -1], 6], [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 0], [[0, 0, 0, -1, 1, 1, -1, 0, 0, 0], 0]]\n_outputs = [[True], [True], [False], [False], [True], [True], [True], [True], [False], [True], [True], [False], [True], [False], [True], [True]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(is_centered(*i), o[0])"}
189
509
coding
Solve the programming task below in a Python markdown code block. There is a vampire family of N members. Vampires are also known as extreme gourmets. Of course vampires' foods are human blood. However, not all kinds of blood is acceptable for them. Vampires drink blood that K blood types of ones are mixed, and each vampire has his/her favorite amount for each blood type. You, cook of the family, are looking inside a fridge to prepare dinner. Your first task is to write a program that judges if all family members' dinner can be prepared using blood in the fridge. Constraints * Judge data includes at most 100 data sets. * 1 ≤ N ≤ 100 * 1 ≤ K ≤ 100 * 0 ≤ Si ≤ 100000 * 0 ≤ Bij ≤ 1000 Input Input file consists of a number of data sets. One data set is given in following format: N K S1 S2 ... SK B11 B12 ... B1K B21 B22 ... B2K : BN1 BN2 ... BNK N and K indicate the number of family members and the number of blood types respectively. Si is an integer that indicates the amount of blood of the i-th blood type that is in a fridge. Bij is an integer that indicates the amount of blood of the j-th blood type that the i-th vampire uses. The end of input is indicated by a case where N = K = 0. You should print nothing for this data set. Output For each set, print "Yes" if everyone's dinner can be prepared using blood in a fridge, "No" otherwise (without quotes). Example Input 2 3 5 4 5 1 2 3 3 2 1 3 5 1 2 3 4 5 0 1 0 1 2 0 1 1 2 2 1 0 3 1 1 0 0 Output Yes No
{"inputs": ["2 3\n9 3 6\n1 1 3\n3 2 0\n3 5\n0 2 2 6 5\n0 1 0 1 2\n1 0 1 2 4\n1 0 3 0 1\n0 0", "2 3\n9 2 6\n1 1 3\n3 2 0\n3 5\n0 2 2 6 5\n0 1 0 1 2\n1 0 1 2 4\n1 0 3 0 1\n0 0", "2 3\n9 2 6\n0 1 3\n3 2 0\n3 5\n0 2 2 6 5\n0 1 0 1 2\n1 0 1 2 4\n1 0 3 0 1\n0 0", "2 3\n9 2 6\n0 1 3\n3 2 0\n3 5\n0 2 2 6 5\n0 1 0 1 2\n1 0 2 2 4\n1 0 3 0 1\n0 0", "2 3\n9 2 6\n0 1 2\n3 2 0\n3 5\n0 2 2 6 5\n0 1 0 1 2\n1 0 2 2 4\n1 0 3 0 1\n0 0", "2 3\n9 2 6\n0 1 2\n3 2 0\n3 5\n0 2 2 6 5\n0 1 0 1 2\n1 0 2 2 4\n1 0 3 1 1\n0 0", "2 3\n5 4 5\n1 2 3\n3 2 1\n3 5\n1 2 3 4 5\n0 1 0 1 2\n0 1 1 2 2\n1 0 3 1 1\n0 0", "2 3\n5 4 5\n1 2 3\n3 2 1\n3 5\n1 2 3 4 5\n-1 1 0 1 2\n0 1 1 2 2\n1 0 3 1 1\n0 0"], "outputs": ["Yes\nNo\n", "No\nNo\n", "No\nNo\n", "No\nNo\n", "No\nNo\n", "No\nNo\n", "Yes\nNo", "Yes\nNo\n"]}
448
622
coding
Solve the programming task below in a Python markdown code block. Taro's Shopping Mammy decided to give Taro his first shopping experience. Mammy tells him to choose any two items he wants from those listed in the shopping catalogue, but Taro cannot decide which two, as all the items look attractive. Thus he plans to buy the pair of two items with the highest price sum, not exceeding the amount Mammy allows. As getting two of the same item is boring, he wants two different items. You are asked to help Taro select the two items. The price list for all of the items is given. Among pairs of two items in the list, find the pair with the highest price sum not exceeding the allowed amount, and report the sum. Taro is buying two items, not one, nor three, nor more. Note that, two or more items in the list may be priced equally. Input The input consists of multiple datasets, each in the following format. n m a1 a2 ... an A dataset consists of two lines. In the first line, the number of items n and the maximum payment allowed m are given. n is an integer satisfying 2 ≤ n ≤ 1000. m is an integer satisfying 2 ≤ m ≤ 2,000,000. In the second line, prices of n items are given. ai (1 ≤ i ≤ n) is the price of the i-th item. This value is an integer greater than or equal to 1 and less than or equal to 1,000,000. The end of the input is indicated by a line containing two zeros. The sum of n's of all the datasets does not exceed 50,000. Output For each dataset, find the pair with the highest price sum not exceeding the allowed amount m and output the sum in a line. If the price sum of every pair of items exceeds m, output `NONE` instead. Sample Input 3 45 10 20 30 6 10 1 2 5 8 9 11 7 100 11 34 83 47 59 29 70 4 100 80 70 60 50 4 20 10 5 10 16 0 0 Output for the Sample Input 40 10 99 NONE 20 Example Input 3 45 10 20 30 6 10 1 2 5 8 9 11 7 100 11 34 83 47 59 29 70 4 100 80 70 60 50 4 20 10 5 10 16 0 0 Output 40 10 99 NONE 20
{"inputs": ["3 45\n20 20 30\n6 10\n1 2 5 8 4 11\n7 000\n11 34 8 47 59 29 70\n4 100\n80 70 62 50\n4 8\n0 5 10 16\n0 0", "3 45\n0 20 30\n6 10\n2 2 5 8 1 11\n7 100\n22 34 33 94 59 29 70\n4 100\n8 70 80 18\n4 20\n10 5 10 16\n0 0", "3 45\n2 20 30\n6 10\n1 2 5 8 4 11\n7 100\n11 34 83 47 59 29 70\n4 100\n80 70 62 50\n4 20\n1 5 10 16\n0 0", "3 45\n17 20 30\n6 10\n1 2 5 8 4 6\n7 100\n11 34 2 47 115 45 70\n4 100\n80 29 62 50\n4 20\n10 5 6 16\n0 0", "3 45\n0 20 30\n6 10\n2 2 5 8 1 11\n7 100\n22 34 33 94 59 56 70\n4 100\n8 70 80 18\n4 20\n10 5 10 29\n0 0", "3 45\n17 20 30\n6 10\n1 2 5 8 3 6\n7 100\n11 34 2 47 115 50 70\n4 100\n80 29 62 50\n4 20\n10 5 10 3\n0 0", "3 45\n20 20 30\n6 10\n1 2 5 8 4 11\n7 000\n11 34 8 47 59 29 70\n4 100\n80 70 62 50\n4 20\n0 5 10 16\n0 0", "3 45\n17 20 30\n6 10\n1 2 5 8 1 11\n7 100\n22 34 33 94 59 29 70\n4 100\n6 75 80 50\n4 20\n9 5 10 16\n0 0"], "outputs": ["40\n10\nNONE\nNONE\n5\n", "30\n10\n99\n98\n20\n", "32\n10\n99\nNONE\n17\n", "37\n10\n92\n91\n16\n", "30\n10\n93\n98\n20\n", "37\n10\n97\n91\n20\n", "40\n10\nNONE\nNONE\n16\n", "37\n10\n99\n86\n19\n"]}
649
903
coding
Solve the programming task below in a Python markdown code block. Young Teodor enjoys drawing. His favourite hobby is drawing segments with integer borders inside his huge [1;m] segment. One day Teodor noticed that picture he just drawn has one interesting feature: there doesn't exist an integer point, that belongs each of segments in the picture. Having discovered this fact, Teodor decided to share it with Sasha. Sasha knows that Teodor likes to show off so he never trusts him. Teodor wants to prove that he can be trusted sometimes, so he decided to convince Sasha that there is no such integer point in his picture, which belongs to each segment. However Teodor is lazy person and neither wills to tell Sasha all coordinates of segments' ends nor wills to tell him their amount, so he suggested Sasha to ask him series of questions 'Given the integer point xi, how many segments in Fedya's picture contain that point?', promising to tell correct answers for this questions. Both boys are very busy studying and don't have much time, so they ask you to find out how many questions can Sasha ask Teodor, that having only answers on his questions, Sasha can't be sure that Teodor isn't lying to him. Note that Sasha doesn't know amount of segments in Teodor's picture. Sure, Sasha is smart person and never asks about same point twice. Input First line of input contains two integer numbers: n and m (1 ≤ n, m ≤ 100 000) — amount of segments of Teodor's picture and maximal coordinate of point that Sasha can ask about. ith of next n lines contains two integer numbers li and ri (1 ≤ li ≤ ri ≤ m) — left and right ends of ith segment in the picture. Note that that left and right ends of segment can be the same point. It is guaranteed that there is no integer point, that belongs to all segments. Output Single line of output should contain one integer number k – size of largest set (xi, cnt(xi)) where all xi are different, 1 ≤ xi ≤ m, and cnt(xi) is amount of segments, containing point with coordinate xi, such that one can't be sure that there doesn't exist point, belonging to all of segments in initial picture, if he knows only this set(and doesn't know n). Examples Input 2 4 1 2 3 4 Output 4 Input 4 6 1 3 2 3 4 6 5 6 Output 5 Note First example shows situation where Sasha can never be sure that Teodor isn't lying to him, because even if one knows cnt(xi) for each point in segment [1;4], he can't distinguish this case from situation Teodor has drawn whole [1;4] segment. In second example Sasha can ask about 5 points e.g. 1, 2, 3, 5, 6, still not being sure if Teodor haven't lied to him. But once he knows information about all points in [1;6] segment, Sasha can be sure that Teodor haven't lied to him.
{"inputs": ["2 7\n1 2\n3 4\n", "2 4\n1 2\n3 4\n", "4 6\n1 3\n2 5\n4 6\n5 6\n", "4 6\n1 3\n2 3\n4 6\n5 6\n", "11 3\n1 1\n1 1\n1 1\n1 1\n2 2\n2 2\n2 2\n3 3\n3 3\n3 3\n3 3\n", "31 1600\n51 1483\n8 475\n14 472\n49 81\n300 1485\n627 682\n44 443\n1191 1541\n478 732\n1112 1202\n741 1341\n475 1187\n1218 1463\n523 1513\n274 477\n1259 1559\n384 928\n487 766\n227 1146\n1102 1268\n833 1240\n872 1342\n235 1075\n825 874\n32 880\n1411 1536\n520 778\n179 1003\n51 313\n1148 1288\n1467 1509\n", "31 1600\n643 1483\n8 475\n15 472\n49 81\n300 1485\n627 682\n44 443\n1191 1541\n478 732\n1112 1202\n741 1341\n475 1187\n1218 1463\n523 1513\n355 477\n1259 1559\n384 928\n487 766\n227 1224\n1102 1268\n833 1240\n872 1342\n666 1075\n734 874\n32 880\n1411 1536\n520 778\n179 1003\n51 313\n1148 1288\n1467 1509\n", "31 1600\n643 1483\n8 475\n15 472\n49 81\n300 1485\n627 682\n44 443\n1191 1541\n478 732\n1112 1202\n741 1341\n475 1187\n1218 1463\n523 1513\n355 477\n1259 1559\n384 928\n487 766\n227 1146\n1102 1268\n833 1240\n872 1342\n666 1075\n734 874\n32 880\n1411 1536\n520 778\n179 1003\n51 313\n1148 1288\n1467 1509\n"], "outputs": ["7\n", "4", "6\n", "5", "2", "1198\n", "1181", "1209\n"]}
655
993
coding
Solve the programming task below in a Python markdown code block. The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (x_{i}, y_{i}), a maximum brightness c, equal for all stars, and an initial brightness s_{i} (0 ≤ s_{i} ≤ c). Over time the stars twinkle. At moment 0 the i-th star has brightness s_{i}. Let at moment t some star has brightness x. Then at moment (t + 1) this star will have brightness x + 1, if x + 1 ≤ c, and 0, otherwise. You want to look at the sky q times. In the i-th time you will look at the moment t_{i} and you will see a rectangle with sides parallel to the coordinate axes, the lower left corner has coordinates (x_1i, y_1i) and the upper right — (x_2i, y_2i). For each view, you want to know the total brightness of the stars lying in the viewed rectangle. A star lies in a rectangle if it lies on its border or lies strictly inside it. -----Input----- The first line contains three integers n, q, c (1 ≤ n, q ≤ 10^5, 1 ≤ c ≤ 10) — the number of the stars, the number of the views and the maximum brightness of the stars. The next n lines contain the stars description. The i-th from these lines contains three integers x_{i}, y_{i}, s_{i} (1 ≤ x_{i}, y_{i} ≤ 100, 0 ≤ s_{i} ≤ c ≤ 10) — the coordinates of i-th star and its initial brightness. The next q lines contain the views description. The i-th from these lines contains five integers t_{i}, x_1i, y_1i, x_2i, y_2i (0 ≤ t_{i} ≤ 10^9, 1 ≤ x_1i < x_2i ≤ 100, 1 ≤ y_1i < y_2i ≤ 100) — the moment of the i-th view and the coordinates of the viewed rectangle. -----Output----- For each view print the total brightness of the viewed stars. -----Examples----- Input 2 3 3 1 1 1 3 2 0 2 1 1 2 2 0 2 1 4 5 5 1 1 5 5 Output 3 0 3 Input 3 4 5 1 1 2 2 3 0 3 3 1 0 1 1 100 100 1 2 2 4 4 2 2 1 4 7 1 50 50 51 51 Output 3 3 5 0 -----Note----- Let's consider the first example. At the first view, you can see only the first star. At moment 2 its brightness is 3, so the answer is 3. At the second view, you can see only the second star. At moment 0 its brightness is 0, so the answer is 0. At the third view, you can see both stars. At moment 5 brightness of the first is 2, and brightness of the second is 1, so the answer is 3.
{"inputs": ["2 3 3\n1 1 1\n3 2 0\n2 1 1 2 2\n0 2 1 4 5\n5 1 1 5 5\n", "2 3 3\n1 1 1\n3 2 0\n2 1 1 1 2\n0 2 1 4 5\n5 1 1 5 5\n", "2 3 3\n1 2 1\n3 2 0\n2 1 1 2 2\n0 2 1 7 5\n5 1 1 5 1\n", "2 3 3\n1 2 1\n3 2 0\n0 1 1 2 2\n0 2 1 7 5\n5 1 1 5 5\n", "2 3 3\n1 2 1\n3 2 0\n0 1 1 2 2\n1 2 1 7 5\n5 1 1 5 5\n", "2 3 3\n1 1 1\n3 2 1\n2 1 1 1 2\n0 2 1 7 5\n7 1 1 5 5\n", "2 3 3\n1 2 1\n3 2 0\n0 1 1 1 2\n1 2 1 7 2\n8 1 1 5 5\n", "2 3 6\n1 1 1\n3 2 0\n2 1 1 2 2\n0 2 1 4 5\n5 1 1 5 5\n"], "outputs": ["3\n0\n3\n", "3\n0\n3\n", "3\n0\n0\n", "1\n0\n3\n", "1\n1\n3\n", "3\n1\n0\n", "1\n1\n1\n", "3\n0\n11\n"]}
755
471
coding
Solve the programming task below in a Python markdown code block. Vova has won $n$ trophies in different competitions. Each trophy is either golden or silver. The trophies are arranged in a row. The beauty of the arrangement is the length of the longest subsegment consisting of golden trophies. Vova wants to swap two trophies (not necessarily adjacent ones) to make the arrangement as beautiful as possible — that means, to maximize the length of the longest such subsegment. Help Vova! Tell him the maximum possible beauty of the arrangement if he is allowed to do at most one swap. -----Input----- The first line contains one integer $n$ ($2 \le n \le 10^5$) — the number of trophies. The second line contains $n$ characters, each of them is either G or S. If the $i$-th character is G, then the $i$-th trophy is a golden one, otherwise it's a silver trophy. -----Output----- Print the maximum possible length of a subsegment of golden trophies, if Vova is allowed to do at most one swap. -----Examples----- Input 10 GGGSGGGSGG Output 7 Input 4 GGGG Output 4 Input 3 SSS Output 0 -----Note----- In the first example Vova has to swap trophies with indices $4$ and $10$. Thus he will obtain the sequence "GGGGGGGSGS", the length of the longest subsegment of golden trophies is $7$. In the second example Vova can make no swaps at all. The length of the longest subsegment of golden trophies in the sequence is $4$. In the third example Vova cannot do anything to make the length of the longest subsegment of golden trophies in the sequence greater than $0$.
{"inputs": ["2\nSS\n", "2\nSG\n", "2\nGS\n", "2\nGG\n", "2\nSG\n", "2\nGG\n", "2\nGS\n", "2\nSS\n"], "outputs": ["0\n", "1\n", "1\n", "2\n", "1\n", "2", "1\n", "0"]}
382
84
coding
Solve the programming task below in a Python markdown code block. You are given an array a of length n. We define f_{a} the following way: Initially f_{a} = 0, M = 1; for every 2 ≤ i ≤ n if a_{M} < a_{i} then we set f_{a} = f_{a} + a_{M} and then set M = i. Calculate the sum of f_{a} over all n! permutations of the array a modulo 10^9 + 7. Note: two elements are considered different if their indices differ, so for every array a there are exactly n! permutations. -----Input----- The first line contains integer n (1 ≤ n ≤ 1 000 000) — the size of array a. Second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9). -----Output----- Print the only integer, the sum of f_{a} over all n! permutations of the array a modulo 10^9 + 7. -----Examples----- Input 2 1 3 Output 1 Input 3 1 1 2 Output 4 -----Note----- For the second example all the permutations are: p = [1, 2, 3] : f_{a} is equal to 1; p = [1, 3, 2] : f_{a} is equal to 1; p = [2, 1, 3] : f_{a} is equal to 1; p = [2, 3, 1] : f_{a} is equal to 1; p = [3, 1, 2] : f_{a} is equal to 0; p = [3, 2, 1] : f_{a} is equal to 0. Where p is the array of the indices of initial array a. The sum of f_{a} is equal to 4.
{"inputs": ["2\n1 3\n", "2\n1 6\n", "2\n1 9\n", "2\n1 3\n", "3\n1 1 2\n", "3\n1 2 2\n", "3\n1 2 4\n", "3\n1 1 4\n"], "outputs": ["1", "1\n", "1\n", "1\n", "4", "2\n", "8\n", "4\n"]}
450
108
coding
Solve the programming task below in a Python markdown code block. Ever since you started work at the grocer, you have been faithfully logging down each item and its category that passes through. One day, your boss walks in and asks, "Why are we just randomly placing the items everywhere? It's too difficult to find anything in this place!" Now's your chance to improve the system, impress your boss, and get that raise! The input is a comma-separated list with category as the prefix in the form `"fruit_banana"`. Your task is to group each item into the 4 categories `Fruit, Meat, Other, Vegetable` and output a string with a category on each line followed by a sorted comma-separated list of items. For example, given: ``` "fruit_banana,vegetable_carrot,fruit_apple,canned_sardines,drink_juice,fruit_orange" ``` output: ``` "fruit:apple,banana,orange\nmeat:\nother:juice,sardines\nvegetable:carrot" ``` Assume that: 1. Only strings of the format `category_item` will be passed in 2. Strings will always be in lower case 3. Input will not be empty 4. Category and/or item will not be empty 5. There will be no duplicate items 6. All categories may not have items Also feel free to reuse/extend the following starter code: ```python def group_groceries(groceries): ```
{"functional": "_inputs = [['fruit_banana,vegetable_carrot,meat_chicken,drink_juice'], ['fruit_banana,vegetable_carrot,fruit_apple,canned_sardines,drink_juice,fruit_orange'], ['vegetable_celery,meat_chicken,meat_beef,fruit_banana,vegetable_carrot,canned_sardines,drink_juice,frozen_fries,fruit_lemon'], ['fruit_orange']]\n_outputs = [['fruit:banana\\nmeat:chicken\\nother:juice\\nvegetable:carrot'], ['fruit:apple,banana,orange\\nmeat:\\nother:juice,sardines\\nvegetable:carrot'], ['fruit:banana,lemon\\nmeat:beef,chicken\\nother:fries,juice,sardines\\nvegetable:carrot,celery'], ['fruit:orange\\nmeat:\\nother:\\nvegetable:']]\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(group_groceries(*i), o[0])"}
315
359
coding
Solve the programming task below in a Python markdown code block. Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
{"inputs": ["3 2\n0 0\n1 0\n2 0", "1 4\n0 0\n2 1\n2 0", "3 8\n0 0\n0 0\n2 0", "3 4\n0 0\n1 0\n2 0", "3 4\n0 0\n1 1\n2 0", "3 4\n0 0\n2 1\n2 0", "1 4\n0 0\n2 1\n2 1", "3 4\n0 0\n0 0\n2 0"], "outputs": ["2\n", "0\n", "8\n", "2\n", "2\n", "2\n", "0\n", "4\n"]}
583
174
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given the array orders, which represents the orders that customers have done in a restaurant. More specifically orders[i]=[customerNamei,tableNumberi,foodItemi] where customerNamei is the name of the customer, tableNumberi is the table customer sit at, and foodItemi is the item customer orders. Return the restaurant's “display table”. The “display table” is a table whose row entries denote how many of each food item each table ordered. The first column is the table number and the remaining columns correspond to each food item in alphabetical order. The first row should be a header whose first column is “Table”, followed by the names of the food items. Note that the customer names are not part of the table. Additionally, the rows should be sorted in numerically increasing order.   Please complete the following python code precisely: ```python class Solution: def displayTable(self, orders: List[List[str]]) -> List[List[str]]: ```
{"functional": "def check(candidate):\n assert candidate(orders = [[\"David\",\"3\",\"Ceviche\"],[\"Corina\",\"10\",\"Beef Burrito\"],[\"David\",\"3\",\"Fried Chicken\"],[\"Carla\",\"5\",\"Water\"],[\"Carla\",\"5\",\"Ceviche\"],[\"Rous\",\"3\",\"Ceviche\"]]) == [[\"Table\",\"Beef Burrito\",\"Ceviche\",\"Fried Chicken\",\"Water\"],[\"3\",\"0\",\"2\",\"1\",\"0\"],[\"5\",\"0\",\"1\",\"0\",\"1\"],[\"10\",\"1\",\"0\",\"0\",\"0\"]] \n assert candidate(orders = [[\"James\",\"12\",\"Fried Chicken\"],[\"Ratesh\",\"12\",\"Fried Chicken\"],[\"Amadeus\",\"12\",\"Fried Chicken\"],[\"Adam\",\"1\",\"Canadian Waffles\"],[\"Brianna\",\"1\",\"Canadian Waffles\"]]) == [[\"Table\",\"Canadian Waffles\",\"Fried Chicken\"],[\"1\",\"2\",\"0\"],[\"12\",\"0\",\"3\"]] \n assert candidate(orders = [[\"Laura\",\"2\",\"Bean Burrito\"],[\"Jhon\",\"2\",\"Beef Burrito\"],[\"Melissa\",\"2\",\"Soda\"]]) == [[\"Table\",\"Bean Burrito\",\"Beef Burrito\",\"Soda\"],[\"2\",\"1\",\"1\",\"1\"]]\n\n\ncheck(Solution().displayTable)"}
218
316
coding
Solve the programming task below in a Python markdown code block. Chef has a natural number N. Cheffina challenges chef to check whether the given number is divisible by the sum of its digits or not. If the given number is divisible then print "Yes" else "No". -----Input:----- - First-line will contain $T$, the number of test cases. Then the test cases follow. - Each test case contains a single line of input, $N$. -----Output:----- For each test case, output in a single line answer. -----Constraints----- - $1 \leq T \leq 10^6$ - $1 \leq N \leq 10^6$ -----Sample Input:----- 2 16 27 -----Sample Output:----- No Yes
{"inputs": ["2\n16\n27"], "outputs": ["No\nYes"]}
169
20
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese , Russian and Vietnamese as well. You have been appointed as the designer for your school's computer network. In total, there are N computers in the class, and M computer-to-computer connections need to be made. Also, there are three mandatory conditions the design should fulfill. The first requirement is that any computer in the network should be able to communicate with any other computer through the connections, possibly, through some other computers. Network attacks are possible, so the second requirement is that even if any one computer from the network gets disabled so that the rest of the computers are unable to communicate with it, the rest of the computers can still communicate with each other. In other words, the first requirement still holds for any subset of (N-1) computers. The third requirement is that there shouldn't be any irrelevant connections in the network. We will call a connection irrelevant if and only if after its' removal, the above two requirements are still held. Given N, M, please build a network with N computers and M connections, or state that it is impossible. ------ Input ------ The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first and only line of each test case contains a pair of space-separated integers N and M denoting the number of computers and the number of connections. ------ Output ------ Output T blocks. If it is impossible to construct a network with the given parameters for the corresponding test case, output just -1 -1. Otherwise, output M lines, each of which contains a space-separated pairs of integers denoting the IDs of the computers that should be connected. Note that multiple connections between any pair of computers and connections connecting a computer to itself are implicitly not allowed due to the third requirement. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ M ≤ N * (N - 1) / 2$ $1 ≤ Sum of all N ≤ 1000$ $Subtask 1 (21 point): 1 ≤ N ≤ 4$ $Subtask 2 (79 points): 1 ≤ N ≤ 100$ ------ Example ------ Input: 2 10 1 5 5 Output: -1 -1 1 2 2 3 3 4 4 5 5 1 ------ Explanation ------ Example case 1. There are not enough connections even to satisfy the first requirement. Example case 2. The obtained network satisfies all the requirements.
{"inputs": ["2\n10 1\n5 5"], "outputs": ["-1 -1\n1 2\n2 3\n3 4\n4 5\n5 1"]}
545
44
coding
Solve the programming task below in a Python markdown code block. This time Baby Ehab will only cut and not stick. He starts with a piece of paper with an array a of length n written on it, and then he does the following: * he picks a range (l, r) and cuts the subsegment a_l, a_{l + 1}, …, a_r out, removing the rest of the array. * he then cuts this range into multiple subranges. * to add a number theory spice to it, he requires that the elements of every subrange must have their product equal to their [least common multiple (LCM)](https://en.wikipedia.org/wiki/Least_common_multiple). Formally, he partitions the elements of a_l, a_{l + 1}, …, a_r into contiguous subarrays such that the product of every subarray is equal to its LCM. Now, for q independent ranges (l, r), tell Baby Ehab the minimum number of subarrays he needs. Input The first line contains 2 integers n and q (1 ≤ n,q ≤ 10^5) — the length of the array a and the number of queries. The next line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^5) — the elements of the array a. Each of the next q lines contains 2 integers l and r (1 ≤ l ≤ r ≤ n) — the endpoints of this query's interval. Output For each query, print its answer on a new line. Example Input 6 3 2 3 10 7 5 14 1 6 2 4 3 5 Output 3 1 2 Note The first query asks about the whole array. You can partition it into [2], [3,10,7], and [5,14]. The first subrange has product and LCM equal to 2. The second has product and LCM equal to 210. And the third has product and LCM equal to 70. Another possible partitioning is [2,3], [10,7], and [5,14]. The second query asks about the range (2,4). Its product is equal to its LCM, so you don't need to partition it further. The last query asks about the range (3,5). You can partition it into [10,7] and [5].
{"inputs": ["1 1\n100000\n1 1\n", "6 3\n1 4 5 5 4 3\n1 6\n4 4\n3 3\n", "6 3\n1 2 3 5 4 2\n1 2\n4 4\n3 6\n", "6 3\n1 6 3 4 4 1\n1 6\n2 6\n3 6\n", "6 3\n1 2 3 5 2 2\n1 6\n4 4\n3 6\n", "6 3\n1 2 3 5 4 2\n1 6\n4 4\n3 6\n", "6 3\n1 2 3 5 4 2\n1 6\n4 4\n3 5\n", "6 3\n1 4 3 5 4 2\n1 6\n4 4\n3 5\n"], "outputs": ["1\n", "2\n1\n1\n", "1\n1\n2\n", "3\n3\n2\n", "3\n1\n2\n", "3\n1\n2\n", "3\n1\n1\n", "3\n1\n1\n"]}
533
293
coding
Solve the programming task below in a Python markdown code block. To become the king of Codeforces, Kuroni has to solve the following problem. He is given $n$ numbers $a_1, a_2, \dots, a_n$. Help Kuroni to calculate $\prod_{1\le i<j\le n} |a_i - a_j|$. As result can be very big, output it modulo $m$. If you are not familiar with short notation, $\prod_{1\le i<j\le n} |a_i - a_j|$ is equal to $|a_1 - a_2|\cdot|a_1 - a_3|\cdot$ $\dots$ $\cdot|a_1 - a_n|\cdot|a_2 - a_3|\cdot|a_2 - a_4|\cdot$ $\dots$ $\cdot|a_2 - a_n| \cdot$ $\dots$ $\cdot |a_{n-1} - a_n|$. In other words, this is the product of $|a_i - a_j|$ for all $1\le i < j \le n$. -----Input----- The first line contains two integers $n$, $m$ ($2\le n \le 2\cdot 10^5$, $1\le m \le 1000$) — number of numbers and modulo. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$). -----Output----- Output the single number — $\prod_{1\le i<j\le n} |a_i - a_j| \bmod m$. -----Examples----- Input 2 10 8 5 Output 3 Input 3 12 1 4 5 Output 0 Input 3 7 1 4 9 Output 1 -----Note----- In the first sample, $|8 - 5| = 3 \equiv 3 \bmod 10$. In the second sample, $|1 - 4|\cdot|1 - 5|\cdot|4 - 5| = 3\cdot 4 \cdot 1 = 12 \equiv 0 \bmod 12$. In the third sample, $|1 - 4|\cdot|1 - 9|\cdot|4 - 9| = 3 \cdot 8 \cdot 5 = 120 \equiv 1 \bmod 7$.
{"inputs": ["2 2\n1 2\n", "2 2\n9 6\n", "2 2\n0 1\n", "2 5\n3 0\n", "2 5\n3 0\n", "2 2\n9 6\n", "2 2\n1 2\n", "2 2\n0 1\n"], "outputs": ["1", "1", "1", "3", "3\n", "1\n", "1\n", "1\n"]}
555
114
coding
Solve the programming task below in a Python markdown code block. This time the Berland Team Olympiad in Informatics is held in a remote city that can only be reached by one small bus. Bus has n passenger seats, seat i can be occupied only by a participant from the city a_{i}. Today the bus has completed m trips, each time bringing n participants. The participants were then aligned in one line in the order they arrived, with people from the same bus standing in the order of their seats (i. e. if we write down the cities where the participants came from, we get the sequence a_1, a_2, ..., a_{n} repeated m times). After that some teams were formed, each consisting of k participants form the same city standing next to each other in the line. Once formed, teams left the line. The teams were formed until there were no k neighboring participants from the same city. Help the organizers determine how many participants have left in the line after that process ended. We can prove that answer doesn't depend on the order in which teams were selected. -----Input----- The first line contains three integers n, k and m (1 ≤ n ≤ 10^5, 2 ≤ k ≤ 10^9, 1 ≤ m ≤ 10^9). The second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5), where a_{i} is the number of city, person from which must take seat i in the bus. -----Output----- Output the number of remaining participants in the line. -----Examples----- Input 4 2 5 1 2 3 1 Output 12 Input 1 9 10 1 Output 1 Input 3 2 10 1 2 1 Output 0 -----Note----- In the second example, the line consists of ten participants from the same city. Nine of them will form a team. At the end, only one participant will stay in the line.
{"inputs": ["1 5 8\n1\n", "1 9 10\n1\n", "1 9 13\n1\n", "1 2 13\n1\n", "1 5 13\n1\n", "1 3 13\n1\n", "1 8 10\n1\n", "1 9 10\n2\n"], "outputs": ["3\n", "1\n", "4\n", "1\n", "3\n", "1\n", "2\n", "1\n"]}
440
125
coding
Solve the programming task below in a Python markdown code block. Write the following function: ```python def area_of_polygon_inside_circle(circle_radius, number_of_sides): ``` It should calculate the area of a regular polygon of `numberOfSides`, `number-of-sides`, or `number_of_sides` sides inside a circle of radius `circleRadius`, `circle-radius`, or `circle_radius` which passes through all the vertices of the polygon (such circle is called [**circumscribed circle** or **circumcircle**](https://en.wikipedia.org/wiki/Circumscribed_circle)). The answer should be a number rounded to 3 decimal places. Input :: Output Examples ```python area_of_polygon_inside_circle(3, 3) # returns 11.691 area_of_polygon_inside_circle(5.8, 7) # returns 92.053 area_of_polygon_inside_circle(4, 5) # returns 38.042 ``` Also feel free to reuse/extend the following starter code: ```python def area_of_polygon_inside_circle(r, n): ```
{"functional": "_inputs = [[3, 3], [2, 4], [2.5, 5]]\n_outputs = [[11.691], [8], [14.86]]\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(area_of_polygon_inside_circle(*i), o[0])"}
239
189
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given the roots of two binary trees root and subRoot, return true if there is a subtree of root with the same structure and node values of subRoot and false otherwise. A subtree of a binary tree tree is a tree that consists of a node in tree and all of this node's descendants. The tree tree could also be considered as a subtree of itself.   Please complete the following python code precisely: ```python # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(root = tree_node([3,4,5,1,2]), subRoot = tree_node([4,1,2])) == True\n assert candidate(root = tree_node([3,4,5,1,2,None,None,None,None,0]), subRoot = tree_node([4,1,2])) == False\n\n\ncheck(Solution().isSubtree)"}
181
92
coding
Solve the programming task below in a Python markdown code block. A number K$K$ is said to be magical if it can be represented as a power of 2 only.That is K$K$=2x$2^{x}$ for some natural number x$x$. Given a string of digits S$S$ of length N$N$, Let P be a valid arrangement of S. By valid arrangement we mean that it should not have any leading zeroes. Find the sum of all such distinct Pi's, which, when treated as a number is magical. Two arrangements are said to be distinct, if there is atleast one index where they differ. As the sum can be very large, print the sum 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. -Only line of each test case contains a string S$S$ of length N$N$, consisting only of digits between 0 to 9. -----Output:----- For each test case, print a single integer denoting the sum of all such magical Pi′s$Pi's$ modulo 109+7$10^{9}+7$. If no such Pi$Pi$ exists print "-1". -----Constraints----- - 1≤T≤1000$1 \leq T \leq 1000$ - 2≤N≤1000$2 \leq N \leq 1000$ - String only consists of digits between 0 to 9, both inclusive. -----Subtasks----- - 10 points : 1≤N≤5$1 \leq N \leq 5$ - 40 points : 1≤N≤100$1 \leq N \leq 100$ - 50 points : Original Constraints -----Sample Input:----- 2 35566 31 -----Sample Output:----- 65536 -1 -----EXPLANATION:----- Only arrangement is 65536. No arrangement of 31 gives us a power of two.
{"inputs": ["2\n35566\n31"], "outputs": ["65536\n-1"]}
471
28
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Two strings $A$ and $B$ are *equivalent* (denoted by $A \sim B$) if they have the same lengths and $A$ can be transformed into $B$ by performing the following operation zero or more times: choose a substring of $A$ which contains '1' an even number of times and reverse this substring. You are given a binary string $S$. Find the number of different equivalence classes of the substrings of this string. In other words, find the smallest possible size of a set $\mathcal{C}$ of binary strings with the following property: for each non-empty string $R$ which is a substring of $S$ (including $S$ itself), there is a string $X \in \mathcal{C}$ such that $R \sim X$. ------ 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$ with length $N$. ------ Output ------ For each test case, print a single line containing one integer ― the number of equivalence classes among the substrings of the given string. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 1,000$ $S$ contains only characters '0' and '1' ------ Subtasks ------ Subtask #1 (10 points): $N ≤ 10$ Subtask #2 (90 points): original constraints ----- Sample Input 1 ------ 10 00000 10001 10101 01111 11001 01101 10110 10010 10111 11001 ----- Sample Output 1 ------ 5 11 8 9 12 10 10 11 11 12
{"inputs": ["10\n00000\n10001\n10101\n01111\n11001\n01101\n10110\n10010\n10111\n11001"], "outputs": ["5\n11\n8\n9\n12\n10\n10\n11\n11\n12"]}
480
98
coding
Solve the programming task below in a Python markdown code block. Among Johnny's numerous hobbies, there are two seemingly harmless ones: applying bitwise operations and sneaking into his dad's office. As it is usually the case with small children, Johnny is unaware that combining these two activities can get him in a lot of trouble. There is a set $S$ containing very important numbers on his dad's desk. The minute Johnny heard about it, he decided that it's a good idea to choose a positive integer $k$ and replace each element $s$ of the set $S$ with $s \oplus k$ ($\oplus$ denotes the exclusive or operation). Help him choose such $k$ that Johnny's dad will not see any difference after his son is done playing (i.e. Johnny will get the same set as before playing). It is possible that no such number exists. It is also possible that there are many of them. In such a case, output the smallest one. Note that the order of elements in a set doesn't matter, i.e. set $\{1, 2, 3\}$ equals to set $\{2, 1, 3\}$. Formally, find the smallest positive integer $k$ such that $\{s \oplus k | s \in S\} = S$ or report that there is no such number. For example, if $S = \{1, 3, 4\}$ and $k = 2$, new set will be equal to $\{3, 1, 6\}$. If $S = \{0, 1, 2, 3\}$ and $k = 1$, after playing set will stay the same. -----Input----- In the first line of input, there is a single integer $t$ ($1 \leq t \leq 1024$), the number of test cases. In the next lines, $t$ test cases follow. Each of them consists of two lines. In the first line there is a single integer $n$ ($1 \leq n \leq 1024$) denoting the number of elements in set $S$. Second line consists of $n$ distinct integers $s_i$ ($0 \leq s_i < 1024$), elements of $S$. It is guaranteed that the sum of $n$ over all test cases will not exceed $1024$. -----Output----- Print $t$ lines; $i$-th line should contain the answer to the $i$-th test case, the minimal positive integer $k$ satisfying the conditions or $-1$ if no such $k$ exists. -----Example----- Input 6 4 1 0 2 3 6 10 7 14 8 3 12 2 0 2 3 1 2 3 6 1 4 6 10 11 12 2 0 1023 Output 1 4 2 -1 -1 1023 -----Note----- In the first test case, the answer is $1$ because it is a minimum positive integer and it satisfies all the conditions.
{"inputs": ["6\n4\n1 0 2 6\n6\n10 7 14 8 0 6\n2\n0 2\n3\n1 2 3\n6\n1 0 3 10 5 12\n2\n0 1023\n", "6\n4\n1 0 2 3\n6\n10 7 14 8 0 12\n2\n0 2\n3\n1 2 3\n6\n1 4 6 10 11 12\n2\n0 302\n", "6\n4\n1 0 2 3\n6\n10 7 14 8 0 12\n2\n0 2\n3\n1 2 3\n6\n1 4 9 10 18 12\n2\n1 302\n", "6\n4\n1 0 2 3\n6\n10 7 14 8 0 12\n2\n0 2\n3\n1 2 3\n6\n1 0 6 10 11 12\n2\n0 302\n", "6\n4\n1 0 2 3\n6\n10 7 14 8 0 12\n2\n0 3\n3\n1 2 3\n6\n1 4 9 10 11 12\n2\n0 302\n", "6\n4\n1 0 4 3\n6\n10 7 14 8 0 12\n2\n0 2\n3\n1 2 3\n6\n1 4 9 10 18 12\n2\n1 302\n", "6\n4\n2 0 4 7\n6\n5 7 14 8 0 12\n2\n1 2\n3\n1 2 3\n6\n1 0 6 10 16 12\n2\n0 1023\n", "6\n4\n1 0 2 3\n6\n10 7 14 8 3 6\n2\n0 2\n3\n1 2 3\n6\n1 4 6 10 11 12\n2\n0 1023\n"], "outputs": ["-1\n-1\n2\n-1\n-1\n1023\n", "1\n-1\n2\n-1\n-1\n302\n", "1\n-1\n2\n-1\n-1\n303\n", "1\n-1\n2\n-1\n10\n302\n", "1\n-1\n3\n-1\n-1\n302\n", "-1\n-1\n2\n-1\n-1\n303\n", "-1\n-1\n3\n-1\n-1\n1023\n", "1\n-1\n2\n-1\n-1\n1023\n"]}
694
721
coding
Solve the programming task below in a Python markdown code block. You are given an axis-aligned rectangle in a 2D Cartesian plane. The bottom left corner of this rectangle has coordinates (0,0)$(0, 0)$ and the top right corner has coordinates (N−1,N−1)$(N-1, N-1)$. You are also given K$K$ light sources; each light source is a point inside or on the perimeter of the rectangle. For each light source, let's divide the plane into four quadrants by a horizontal and a vertical line passing through this light source. The light source can only illuminate one of these quadrants (including its border, i.e. the point containing the light source and two half-lines), but the quadrants illuminated by different light sources may be different. You want to assign a quadrant to each light source in such a way that when they illuminate their respective quadrants, the entire rectangle (including its perimeter) is illuminated. Find out whether it is possible to assign quadrants to light sources in such a way. -----Input----- - The first line of the input contains an integer T$T$ denoting the number of test cases. The description of the test cases follows. - The first line of each test case contains two space-separated integers K$K$ and N$N$. - Each of the next K$K$ lines contains two space-separated integers x$x$ and y$y$ denoting a light source with coordinates (x,y)$(x, y)$. -----Output----- For each test case, print a single line containing the string "yes" if it is possible to illuminate the whole rectangle or "no" if it is impossible. -----Constraints----- - 1≤T≤5,000$1 \le T \le 5,000$ - 1≤K≤100$1 \le K \le 100$ - 1≤N≤109$1 \le N \le 10^9$ - 0≤x,y≤N−1$0 \le x, y \le N-1$ - no two light sources coincide -----Example Input----- 2 2 10 0 0 1 0 2 10 1 2 1 1 -----Example Output----- yes no
{"inputs": ["2\n2 10\n0 0\n1 0\n2 10\n1 2\n1 1"], "outputs": ["yes\nno"]}
493
40
coding
Solve the programming task below in a Python markdown code block. Complete the code which should return `true` if the given object is a single ASCII letter (lower or upper case), `false` otherwise. Also feel free to reuse/extend the following starter code: ```python def is_letter(s): ```
{"functional": "_inputs = [[''], ['a'], ['X'], ['7'], ['_'], ['ab'], ['a\\n']]\n_outputs = [[False], [True], [True], [False], [False], [False], [False]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(is_letter(*i), o[0])"}
63
191
coding
Solve the programming task below in a Python markdown code block. Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. One day Petya encountered a tree with n vertexes. Besides, the tree was weighted, i. e. each edge of the tree has weight (a positive integer). An edge is lucky if its weight is a lucky number. Note that a tree with n vertexes is an undirected connected graph that has exactly n - 1 edges. Petya wondered how many vertex triples (i, j, k) exists that on the way from i to j, as well as on the way from i to k there must be at least one lucky edge (all three vertexes are pairwise distinct). The order of numbers in the triple matters, that is, the triple (1, 2, 3) is not equal to the triple (2, 1, 3) and is not equal to the triple (1, 3, 2). Find how many such triples of vertexes exist. Input The first line contains the single integer n (1 ≤ n ≤ 105) — the number of tree vertexes. Next n - 1 lines contain three integers each: ui vi wi (1 ≤ ui, vi ≤ n, 1 ≤ wi ≤ 109) — the pair of vertexes connected by the edge and the edge's weight. Output On the single line print the single number — the answer. Please do not use the %lld specificator to read or write 64-bit numbers in С++. It is recommended to use the cin, cout streams or the %I64d specificator. Examples Input 4 1 2 4 3 1 2 1 4 7 Output 16 Input 4 1 2 4 1 3 47 1 4 7447 Output 24 Note The 16 triples of vertexes from the first sample are: (1, 2, 4), (1, 4, 2), (2, 1, 3), (2, 1, 4), (2, 3, 1), (2, 3, 4), (2, 4, 1), (2, 4, 3), (3, 2, 4), (3, 4, 2), (4, 1, 2), (4, 1, 3), (4, 2, 1), (4, 2, 3), (4, 3, 1), (4, 3, 2). In the second sample all the triples should be counted: 4·3·2 = 24.
{"inputs": ["2\n1 2 7\n", "2\n1 2 3\n", "2\n1 2 10\n", "2\n2 1 1000000000\n", "2\n2 1 1010000000\n", "2\n2 1 1000000001\n", "4\n1 2 4\n3 1 2\n1 4 7\n", "4\n1 2 4\n1 3 47\n1 4 7447\n"], "outputs": ["0", "0", "0", "0", "0", "0", "16", "24"]}
638
168
coding
Solve the programming task below in a Python markdown code block. A group of people played a game. All players had distinct scores, which are positive integers. Takahashi knows N facts on the players' scores. The i-th fact is as follows: the A_i-th highest score among the players is B_i. Find the maximum possible number of players in the game. Constraints * 1 \leq N \leq 10^5 * 1 \leq A_i \leq 10^9(1\leq i\leq N) * 0 \leq B_i \leq 10^9(1\leq i\leq N) * If i ≠ j, A_i ≠ A_j. * There exists a possible outcome of the game that are consistent with the facts. * All input values are integers. Inputs Input is given from Standard Input in the following format: N A_1 B_1 : A_N B_N Outputs Print the maximum possible number of players in the game. Examples Input 3 4 7 2 9 6 2 Output 8 Input 5 1 10 3 6 5 2 4 4 2 8 Output 7 Input 2 1 1000000000 1000000000 1 Output 1000000001
{"inputs": ["3\n4 5\n2 9\n6 2", "3\n4 5\n2 9\n6 3", "3\n4 7\n2 9\n6 2", "3\n8 3\n2 14\n3 3", "5\n1 10\n3 6\n5 2\n4 4\n2 8", "2\n1 1000100000\n1000000000 1", "2\n1 1000100000\n1000000010 1", "2\n0 1000110010\n1000001010 1"], "outputs": ["8\n", "9\n", "8", "11\n", "7", "1000000001\n", "1000000011\n", "1000001011\n"]}
317
236
coding
Solve the programming task below in a Python markdown code block. In this challenge, the task is to debug the existing code to successfully execute all provided test files. A number is called a smart number if it has an odd number of factors. Given some numbers, find whether they are smart numbers or not. Debug the given function is_smart_number to correctly check if a given number is a smart number. Note: You can modify only one line in the given code and you cannot add or remove any new lines. To restore the original code, click on the icon to the right of the language selector. Input Format The first line of the input contains $\boldsymbol{\boldsymbol{t}}$, the number of test cases. The next $\boldsymbol{\boldsymbol{t}}$ lines contain one integer each. Constraints $1\leq t\leq10^3$ $1\leq n_i\leq10^4$, where $n_i$ is the $i^{\mbox{th}}$ integer. Output Format The output should consist of $\boldsymbol{\boldsymbol{t}}$ lines. In the $i^{\mbox{th}}$ line print YES if the $i^{\mbox{th}}$ integer has an odd number of factors, else print NO. Sample Input 4 1 2 7 169 Sample Output YES NO NO YES Explanation The factors of 1 are just 1 itself.So the answer is YES. The factors of 2 are 1 and 2.It has even number of factors.The answer is NO. The factors of 7 are 1 and 7.It has even number of factors.The answer is NO. The factors of 169 are 1,13 and 169.It has odd number of factors.The answer is YES.
{"inputs": ["4\n1\n2\n7\n169\n"], "outputs": ["YES\nNO\nNO\nYES\n"]}
395
30
coding
Solve the programming task below in a Python markdown code block. ## Task Generate a sorted list of all possible IP addresses in a network. For a subnet that is not a valid IPv4 network return `None`. ## Examples ``` ipsubnet2list("192.168.1.0/31") == ["192.168.1.0", "192.168.1.1"] ipsubnet2list("213.256.46.160/28") == None ``` Also feel free to reuse/extend the following starter code: ```python def ipsubnet2list(subnet): ```
{"functional": "_inputs = [['192.168.1.0/31'], ['195.20.15.0/28'], ['174.0.153.152/29'], ['213.192.46.160/28'], ['213.256.46.160/28']]\n_outputs = [[['192.168.1.0', '192.168.1.1']], [['195.20.15.1', '195.20.15.2', '195.20.15.3', '195.20.15.4', '195.20.15.5', '195.20.15.6', '195.20.15.7', '195.20.15.8', '195.20.15.9', '195.20.15.10', '195.20.15.11', '195.20.15.12', '195.20.15.13', '195.20.15.14']], [['174.0.153.153', '174.0.153.154', '174.0.153.155', '174.0.153.156', '174.0.153.157', '174.0.153.158']], [['213.192.46.161', '213.192.46.162', '213.192.46.163', '213.192.46.164', '213.192.46.165', '213.192.46.166', '213.192.46.167', '213.192.46.168', '213.192.46.169', '213.192.46.170', '213.192.46.171', '213.192.46.172', '213.192.46.173', '213.192.46.174']], [None]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(ipsubnet2list(*i), o[0])"}
148
769
coding
Solve the programming task below in a Python markdown code block. Your website is divided vertically in sections, and each can be of different size (height). You need to establish the section index (starting at `0`) you are at, given the `scrollY` and `sizes` of all sections. Sections start with `0`, so if first section is `200` high, it takes `0-199` "pixels" and second starts at `200`. ### Example: `getSectionIdFromScroll( 300, [300,200,400,600,100] )` will output number `1` as it's the second section. `getSectionIdFromScroll( 1600, [300,200,400,600,100] )` will output number `-1` as it's past last section. Given the `scrollY` integer (always non-negative) and an array of non-negative integers (with at least one element), calculate the index (starting at `0`) or `-1` if `scrollY` falls beyond last section (indication of an error). Also feel free to reuse/extend the following starter code: ```python def get_section_id(scroll, sizes): ```
{"functional": "_inputs = [[1, [300, 200, 400, 600, 100]], [299, [300, 200, 400, 600, 100]], [300, [300, 200, 400, 600, 100]], [1599, [300, 200, 400, 600, 100]], [1600, [300, 200, 400, 600, 100]]]\n_outputs = [[0], [0], [1], [4], [-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(get_section_id(*i), o[0])"}
282
314
coding
Solve the programming task below in a Python markdown code block. As you know, majority of students and teachers of Summer Informatics School live in Berland for the most part of the year. Since corruption there is quite widespread, the following story is not uncommon. Elections are coming. You know the number of voters and the number of parties — n and m respectively. For each voter you know the party he is going to vote for. However, he can easily change his vote given a certain amount of money. In particular, if you give i-th voter c_i bytecoins you can ask him to vote for any other party you choose. The United Party of Berland has decided to perform a statistical study — you need to calculate the minimum number of bytecoins the Party needs to spend to ensure its victory. In order for a party to win the elections, it needs to receive strictly more votes than any other party. Input The first line of input contains two integers n and m (1 ≤ n, m ≤ 3000) — the number of voters and the number of parties respectively. Each of the following n lines contains two integers p_i and c_i (1 ≤ p_i ≤ m, 1 ≤ c_i ≤ 10^9) — the index of this voter's preferred party and the number of bytecoins needed for him to reconsider his decision. The United Party of Berland has the index 1. Output Print a single number — the minimum number of bytecoins needed for The United Party of Berland to win the elections. Examples Input 1 2 1 100 Output 0 Input 5 5 2 100 3 200 4 300 5 400 5 900 Output 500 Input 5 5 2 100 3 200 4 300 5 800 5 900 Output 600 Note In the first sample, The United Party wins the elections even without buying extra votes. In the second sample, The United Party can buy the votes of the first and the fourth voter. This way The Party gets two votes, while parties 3, 4 and 5 get one vote and party number 2 gets no votes. In the third sample, The United Party can buy the votes of the first three voters and win, getting three votes against two votes of the fifth party.
{"inputs": ["1 1\n1 100\n", "1 1\n1 110\n", "1 2\n1 100\n", "1 259\n55 226621946\n", "1 654\n408 226621946\n", "1 3000\n61 226621946\n", "1 3000\n55 226621946\n", "1 522\n408 226621946\n"], "outputs": ["0\n", "0\n", "0\n", "226621946\n", "226621946\n", "226621946\n", "226621946\n", "226621946\n"]}
521
223
coding
Solve the programming task below in a Python markdown code block. The wide mouth frog is particularly interested in the eating habits of other creatures. He just can't stop asking the creatures he encounters what they like to eat. But then he meet the alligator who just LOVES to eat wide-mouthed frogs! When he meets the alligator, it then makes a tiny mouth. Your goal in this kata is to create complete the `mouth_size` method this method take one argument `animal` which corresponds to the animal encountered by frog. If this one is an `alligator` (case insensitive) return `small` otherwise return `wide`. Also feel free to reuse/extend the following starter code: ```python def mouth_size(animal): ```
{"functional": "_inputs = [['toucan'], ['ant bear'], ['alligator']]\n_outputs = [['wide'], ['wide'], ['small']]\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(mouth_size(*i), o[0])"}
152
171
coding
Solve the programming task below in a Python markdown code block. You are given $\textit{q}$ queries where each query consists of a set of $n$ points on a two-dimensional plane (i.e., $(x,y)$). For each set of points, print YES on a new line if all the points fall on the edges (i.e., sides and/or corners) of a non-degenerate rectangle which is axis parallel; otherwise, print NO instead. Input Format The first line contains a single positive integer, $\textit{q}$, denoting the number of queries. The subsequent lines describe each query in the following format: The first line contains a single positive integer, $n$, denoting the number of points in the query. Each line $\boldsymbol{i}$ of the $n$ subsequent lines contains two space-separated integers describing the respective values of $x_i$ and $y_i$ for the point at coordinate $(x_i,y_i)$. Constraints $1\leq q\leq10$ $1\leq n\leq10$ $-10^4\leq x,y\leq10^4$ Output Format For each query, print YES on a new line if all $n$ points lie on the edges of some non-degenerate rectangle which is axis parallel; otherwise, print NO instead. Sample Input 2 3 0 0 0 1 1 0 4 0 0 0 2 2 0 1 1 Sample Output YES NO Explanation We perform the following $q=2$ queries: In the first query, all points lie on the edges of a non-degenerate rectangle with corners at $(0,0)$, $(0,1)$, $(1,0)$, and $(1,1)$. Thus, we print YES on a new line. In the second query, points $(0,0)$, $(0,2)$, and $(2,0)$ could all lie along the edge of some non-degenerate rectangle, but point $(1,1)$ would have to fall inside that rectangle. Thus, we print NO on a new line.
{"inputs": ["2\n3\n0 0\n0 1\n1 0\n4\n0 0\n0 2\n2 0\n1 1\n"], "outputs": ["YES\nNO\n"]}
461
48
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.   Please complete the following python code precisely: ```python class Solution: def maxProfit(self, prices: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate([7,1,5,3,6,4]) == 5\n assert candidate(prices = [7,6,4,3,1]) == 0\n\n\ncheck(Solution().maxProfit)"}
120
60
coding
Solve the programming task below in a Python markdown code block. The government has invited bids from contractors to run canteens at all railway stations. Contractors will be allowed to bid for the catering contract at more than one station. However, to avoid monopolistic price-fixing, the government has declared that no contractor may bid for a pair of neighbouring stations. The railway network has exactly one route between any pair of stations. Each station is directly connected by a railway line to at most $50$ neighbouring stations. To help contractors plan their bids, the government has provided data on the number of passengers who pass through each station each year. Contractors would like to bid for stations with a higher volume of passenger traffic to increase their turnover. For instance, suppose the railway network is as follows, where the volume of passenger traffic is indicated by the side of each station. In this network, the best option for the contractor is to bid for stations $1, 2, 5$ and $6$, for a total passenger volume of $90$. Your task is to choose a set of stations that the contractor should bid for so that the total volume of traffic across all the stations in the bid is maximized. -----Input:----- The first line of the input contains one integer $N$ indicating the number of railways stations in the network. The stations are numbered $1,2,...,N$. This is followed by $N$ lines of input, lines $2, 3,..., N+1$, indicating the volume of traffic at each station. The volume of traffic at station $i, 1 \leq i \leq N$, is given by a single integer in line $i+1$. The next $N-1$ lines of input, lines $N+2, N+3, ..., 2N$, describe the railway network. Each of these lines contains two integers, denoting a pair of stations that are neighbours. -----Output:----- The output should be a single integer, corresponding to the total volume of traffic across the set of stations in the optimal bid made by the contractor. -----Constraints:----- - $1 \leq N \leq 100000$. - Each railway station has at most $50$ neighbours. -----Sample Input----- 6 10 20 25 40 30 30 4 5 1 3 3 4 2 3 6 4 -----Sample Output----- 90
{"inputs": ["6\n10\n20\n25\n40\n30\n30\n4 5\n1 3\n3 4\n2 3\n6 4"], "outputs": ["90"]}
517
51
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a valid (IPv4) IP address, return a defanged version of that IP address. A defanged IP address replaces every period "." with "[.]".   Please complete the following python code precisely: ```python class Solution: def defangIPaddr(self, address: str) -> str: ```
{"functional": "def check(candidate):\n assert candidate(address = \"1.1.1.1\") == \"1[.]1[.]1[.]1\"\n assert candidate(address = \"255.100.50.0\") == \"255[.]100[.]50[.]0\"\n\n\ncheck(Solution().defangIPaddr)"}
86
87
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. In some array arr, the values were in arithmetic progression: the values arr[i + 1] - arr[i] are all equal for every 0 <= i < arr.length - 1. A value from arr was removed that was not the first or last value in the array. Given arr, return the removed value.   Please complete the following python code precisely: ```python class Solution: def missingNumber(self, arr: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(arr = [5,7,11,13]) == 9\n assert candidate(arr = [15,13,12]) == 14\n\n\ncheck(Solution().missingNumber)"}
114
59
coding
Solve the programming task below in a Python markdown code block. # Task You are given a car odometer which displays the miles traveled as an integer. The odometer has a defect, however: it proceeds from digit `3` to digit `5` always skipping the digit `4`. This defect shows up in all positions (ones, tens, hundreds, etc). For example, if the odometer displays `15339` and the car travels another mile, the odometer changes to `15350` (instead of `15340`). Your task is to calculate the real distance, according The number the odometer shows. # Example For `n = 13` the output should be `12`(4 skiped). For `n = 15` the output should be `13`(4 and 14 skiped). For `n = 2003` the output should be `1461`. # Input/Output - `[input]` integer `n` The number the odometer shows. `1 <= n <= 999999999` - `[output]` an integer The real distance. Also feel free to reuse/extend the following starter code: ```python def faulty_odometer(n): ```
{"functional": "_inputs = [[13], [15], [55], [2005], [1500], [999999], [165826622]]\n_outputs = [[12], [13], [40], [1462], [1053], [531440], [69517865]]\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(faulty_odometer(*i), o[0])"}
285
236
coding
Solve the programming task below in a Python markdown code block. Chef and his friends are playing the game AMONG US. They all have chosen their names as numbers. There are N people in Chef’s group including him, and each swears that he is not the imposter. However, it turns out there were N+1 people in the game. Now all that Chef needs to know is the name of the imposter, which is a number. Also remember that numbers can be duplicate. Can you help out Chef in finding the imposter? Input : First line contains the value of N. Second line contains the N numbers that Chef’s friends used as their names. Third line contains the N+1 numbers that people in the game have used as their names. Output : Print the extra number in new line. Constraints : 1 ≤ Numbers used as names ≤ 1,000 1 ≤ N ≤ 1,000,000 Sample Input : 3 4 2 5 4 2 3 5 Sample Output : 3
{"inputs": ["3\n4 2 5\n4 2 3 5"], "outputs": ["3"]}
224
26
coding
Solve the programming task below in a Python markdown code block. Brief ===== Sometimes we need information about the list/arrays we're dealing with. You'll have to write such a function in this kata. Your function must provide the following informations: * Length of the array * Number of integer items in the array * Number of float items in the array * Number of string character items in the array * Number of whitespace items in the array The informations will be supplied in arrays that are items of another array. Like below: `Output array = [[array length],[no of integer items],[no of float items],[no of string chars items],[no of whitespace items]]` Added Difficulty ---------------- If any item count in the array is zero, you'll have to replace it with a **None/nil/null** value (according to the language). And of course, if the array is empty then return **'Nothing in the array!**. For the sake of simplicity, let's just suppose that there are no nested structures. Output ====== If you're head is spinning (just kidding!) then these examples will help you out- ``` array_info([1,2,3.33,4,5.01,'bass','kick',' '])--------->[[8],[3],[2],[2],[1]] array_info([0.001,2,' '])------------------------------>[[3],[1],[1],[None],[1]] array_info([])----------------------------------------->'Nothing in the array!' array_info([' '])-------------------------------------->[[1],[None],[None],[None],[1]] ``` Remarks ------- The input will always be arrays/lists. So no need to check the inputs. Hint ==== See the tags!!! Now let's get going ! Also feel free to reuse/extend the following starter code: ```python def array_info(x): ```
{"functional": "_inputs = [[[1, 2, 3.33, 4, 5.01, 'bass', 'kick', ' ']], [[0.001, 2, ' ']], [[]], [[' ']], [[' ', ' ']], [['jazz']], [[4]], [[3.1416]], [[11, 22, 33.33, 44.44, 'hasan', 'ahmad']], [['a', 'b', 'c', 'd', ' ']], [[1, 2, 3, 4, 5, 6, 7, 8, 9]], [[1, 2.23, 'string', ' ']]]\n_outputs = [[[[8], [3], [2], [2], [1]]], [[[3], [1], [1], [None], [1]]], ['Nothing in the array!'], [[[1], [None], [None], [None], [1]]], [[[2], [None], [None], [None], [2]]], [[[1], [None], [None], [1], [None]]], [[[1], [1], [None], [None], [None]]], [[[1], [None], [1], [None], [None]]], [[[6], [2], [2], [2], [None]]], [[[5], [None], [None], [4], [1]]], [[[9], [9], [None], [None], [None]]], [[[4], [1], [1], [1], [1]]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(array_info(*i), o[0])"}
398
488
coding
Solve the programming task below in a Python markdown code block. # Task You are given a string `s`. Let's call its substring a group, if all letters in it are adjacent and the same(such as `"aa","bbb","cccc"`..). Let's call the substiring with 2 or more adjacent group a big group(such as `"aabb","bbccc"`...). Your task is to count the number of `big groups` in the given string. # Example For `s = "ccccoodeffffiiighhhhhhhhhhttttttts"`, the result should be `3`. ``` The groups are "cccc", "oo", "ffff", "iii", "hhhhhhhhhh", "ttttttt" The big groups are "ccccoo", "ffffiii", "hhhhhhhhhhttttttt", 3 substrings altogether.``` For `s = "gztxxxxxggggggggggggsssssssbbbbbeeeeeeehhhmmmmmmmitttttttlllllhkppppp"`, the result should be `2`. ``` The big groups are : "xxxxxggggggggggggsssssssbbbbbeeeeeeehhhmmmmmmm" and "tttttttlllll" ``` For `s = "soooooldieeeeeer"`, the result should be `0`. There is no `big group` exist. # Input/Output - `[input]` string `s` A string of lowercase Latin letters. - `[output]` an integer The number of big groups. Also feel free to reuse/extend the following starter code: ```python def repeat_adjacent(string): ```
{"functional": "_inputs = [['ccccoodeffffiiighhhhhhhhhhttttttts'], ['soooooldieeeeeer'], ['ccccoooooooooooooooooooooooddee'], ['wwwwaaaarrioooorrrrr'], ['gztxxxxxggggggggggggsssssssbbbbbeeeeeeehhhmmmmmmmitttttttlllllhkppppp']]\n_outputs = [[3], [0], [1], [2], [2]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(repeat_adjacent(*i), o[0])"}
366
243
coding
Solve the programming task below in a Python markdown code block. Little Elephant loves magic squares very much. A magic square is a 3 × 3 table, each cell contains some positive integer. At that the sums of integers in all rows, columns and diagonals of the table are equal. The figure below shows the magic square, the sum of integers in all its rows, columns and diagonals equals 15. $\left. \begin{array}{|c|c|c|} \hline 4 & {9} & {2} \\ \hline 3 & {5} & {7} \\ \hline 8 & {1} & {6} \\ \hline \end{array} \right.$ The Little Elephant remembered one magic square. He started writing this square on a piece of paper, but as he wrote, he forgot all three elements of the main diagonal of the magic square. Fortunately, the Little Elephant clearly remembered that all elements of the magic square did not exceed 10^5. Help the Little Elephant, restore the original magic square, given the Elephant's notes. -----Input----- The first three lines of the input contain the Little Elephant's notes. The first line contains elements of the first row of the magic square. The second line contains the elements of the second row, the third line is for the third row. The main diagonal elements that have been forgotten by the Elephant are represented by zeroes. It is guaranteed that the notes contain exactly three zeroes and they are all located on the main diagonal. It is guaranteed that all positive numbers in the table do not exceed 10^5. -----Output----- Print three lines, in each line print three integers — the Little Elephant's magic square. If there are multiple magic squares, you are allowed to print any of them. Note that all numbers you print must be positive and not exceed 10^5. It is guaranteed that there exists at least one magic square that meets the conditions. -----Examples----- Input 0 1 1 1 0 1 1 1 0 Output 1 1 1 1 1 1 1 1 1 Input 0 3 6 5 0 5 4 7 0 Output 6 3 6 5 5 5 4 7 4
{"inputs": ["0 1 1\n1 0 1\n1 1 0\n", "0 3 6\n5 0 5\n4 7 0\n", "0 4 4\n4 0 4\n4 4 0\n", "0 7 6\n9 0 1\n4 3 0\n", "0 9 2\n3 0 7\n8 1 0\n", "0 4 4\n4 0 4\n4 4 0\n", "0 9 2\n3 0 7\n8 1 0\n", "0 7 6\n9 0 1\n4 3 0\n"], "outputs": ["1 1 1\n1 1 1\n1 1 1\n", "6 3 6\n5 5 5\n4 7 4\n", "4 4 4\n4 4 4\n4 4 4\n", "2 7 6\n9 5 1\n4 3 8\n", "4 9 2\n3 5 7\n8 1 6\n", "4 4 4\n4 4 4\n4 4 4\n", "4 9 2\n3 5 7\n8 1 6\n", "2 7 6\n9 5 1\n4 3 8\n"]}
494
326
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an n x n grid where you have placed some 1 x 1 x 1 cubes. Each value v = grid[i][j] represents a tower of v cubes placed on top of cell (i, j). After placing these cubes, you have decided to glue any directly adjacent cubes to each other, forming several irregular 3D shapes. Return the total surface area of the resulting shapes. Note: The bottom face of each shape counts toward its surface area.   Please complete the following python code precisely: ```python class Solution: def surfaceArea(self, grid: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(grid = [[1,2],[3,4]]) == 34\n assert candidate(grid = [[1,1,1],[1,0,1],[1,1,1]]) == 32\n assert candidate(grid = [[2,2,2],[2,1,2],[2,2,2]]) == 46\n\n\ncheck(Solution().surfaceArea)"}
147
96
coding
Solve the programming task below in a Python markdown code block. Write a program which reads $n$ dices constructed in the same way as Dice I, and determines whether they are all different. For the determination, use the same way as Dice III. Constraints * $2 \leq n \leq 100$ * $0 \leq $ the integer assigned to a face $ \leq 100$ Input In the first line, the number of dices $n$ is given. In the following $n$ lines, six integers assigned to the dice faces are given respectively in the same way as Dice III. Output Print "Yes" if given dices are all different, otherwise "No" in a line. Examples Input 3 1 2 3 4 5 6 6 2 4 3 5 1 6 5 4 3 2 1 Output No Input 3 1 2 3 4 5 6 6 5 4 3 2 1 5 4 3 2 1 6 Output Yes
{"inputs": ["3\n1 2 3 4 5 6\n6 5 4 3 2 1\n5 4 0 2 1 6", "3\n1 2 3 4 5 6\n6 2 5 3 5 1\n6 5 4 3 2 1", "3\n1 2 3 4 5 6\n5 5 4 3 2 1\n5 4 0 2 1 6", "3\n1 2 3 4 5 6\n6 2 5 3 0 1\n6 5 4 3 2 1", "3\n1 2 3 3 5 6\n5 5 4 3 2 1\n5 4 0 2 1 6", "3\n1 4 3 4 5 6\n6 2 5 3 0 1\n6 5 4 3 2 1", "3\n1 2 3 3 5 6\n5 2 4 3 2 1\n5 4 0 2 1 6", "3\n1 6 3 4 5 6\n6 2 5 3 0 1\n6 5 4 3 2 1"], "outputs": ["Yes\n", "Yes\n", "Yes\n", "Yes\n", "Yes\n", "Yes\n", "Yes\n", "Yes\n"]}
246
350
coding
Solve the programming task below in a Python markdown code block. Arcady is a copywriter. His today's task is to type up an already well-designed story using his favorite text editor. Arcady types words, punctuation signs and spaces one after another. Each letter and each sign (including line feed) requires one keyboard click in order to be printed. Moreover, when Arcady has a non-empty prefix of some word on the screen, the editor proposes a possible autocompletion for this word, more precisely one of the already printed words such that its prefix matches the currently printed prefix if this word is unique. For example, if Arcady has already printed «codeforces», «coding» and «codeforces» once again, then there will be no autocompletion attempt for «cod», but if he proceeds with «code», the editor will propose «codeforces». With a single click Arcady can follow the editor's proposal, i.e. to transform the current prefix to it. Note that no additional symbols are printed after the autocompletion (no spaces, line feeds, etc). What is the minimum number of keyboard clicks Arcady has to perform to print the entire text, if he is not allowed to move the cursor or erase the already printed symbols? A word here is a contiguous sequence of latin letters bordered by spaces, punctuation signs and line/text beginnings/ends. Arcady uses only lowercase letters. For example, there are 20 words in «it's well-known that tic-tac-toe is a paper-and-pencil game for two players, x and o.». -----Input----- The only line contains Arcady's text, consisting only of lowercase latin letters, spaces, line feeds and the following punctuation signs: «.», «,», «?», «!», «'» and «-». The total amount of symbols doesn't exceed 3·10^5. It's guaranteed that all lines are non-empty. -----Output----- Print a single integer — the minimum number of clicks. -----Examples----- Input snow affects sports such as skiing, snowboarding, and snowmachine travel. snowboarding is a recreational activity and olympic and paralympic sport. Output 141 Input 'co-co-co, codeforces?!' Output 25 Input thun-thun-thunder, thunder, thunder thunder, thun-, thunder thun-thun-thunder, thunder thunder, feel the thunder lightning then the thunder thunder, feel the thunder lightning then the thunder thunder, thunder Output 183 -----Note----- In sample case one it's optimal to use autocompletion for the first instance of «snowboarding» after typing up «sn» and for the second instance of «snowboarding» after typing up «snowb». This will save 7 clicks. In sample case two it doesn't matter whether to use autocompletion or not.
{"inputs": ["aaa aaaaaa\n", "aaa aaaaaa\n", "aaa aabaaa\n", "aaa aaabaa\n", "aaa aaabab\n", "aaa baaaab\n", "'co-co-co, codeforces?!'\n", "'co-co-co, '!?secrofedoc\n"], "outputs": ["10\n", "10\n", "11\n", "10\n", "10\n", "11\n", "25\n", "25\n"]}
610
112
coding
Solve the programming task below in a Python markdown code block. Generate and return **all** possible increasing arithmetic progressions of six primes `[a, b, c, d, e, f]` between the given limits. Note: the upper and lower limits are inclusive. An arithmetic progression is a sequence where the difference between consecutive numbers is the same, such as: 2, 4, 6, 8. A prime number is a number that is divisible only by itself and 1 (e.g. 2, 3, 5, 7, 11) Your solutions should be returned as lists inside a list in ascending order of the first item (if there are multiple lists with same first item, return in ascending order for the second item etc) are the e.g: `[ [a, b, c, d, e, f], [g, h, i, j, k, l] ]` where `a < g`. If there are no solutions, return an empty list: `[]` ## Examples Also feel free to reuse/extend the following starter code: ```python def primes_a_p(lower_limit, upper_limit): ```
{"functional": "_inputs = [[0, 200], [90, 600], [770, 1000], [30, 305], [1000, 4000]]\n_outputs = [[[[7, 37, 67, 97, 127, 157]]], [[[107, 137, 167, 197, 227, 257], [359, 389, 419, 449, 479, 509]]], [[]], [[[107, 137, 167, 197, 227, 257]]], [[[1013, 1193, 1373, 1553, 1733, 1913], [1039, 1249, 1459, 1669, 1879, 2089], [1039, 1609, 2179, 2749, 3319, 3889], [1091, 1301, 1511, 1721, 1931, 2141], [1201, 1471, 1741, 2011, 2281, 2551], [1289, 1709, 2129, 2549, 2969, 3389], [1301, 1511, 1721, 1931, 2141, 2351], [1307, 1487, 1667, 1847, 2027, 2207], [1321, 1531, 1741, 1951, 2161, 2371], [1381, 1831, 2281, 2731, 3181, 3631], [1451, 1901, 2351, 2801, 3251, 3701], [1453, 1663, 1873, 2083, 2293, 2503], [1511, 1811, 2111, 2411, 2711, 3011], [1619, 2039, 2459, 2879, 3299, 3719], [1663, 1873, 2083, 2293, 2503, 2713], [1669, 2029, 2389, 2749, 3109, 3469], [1973, 2063, 2153, 2243, 2333, 2423], [2129, 2459, 2789, 3119, 3449, 3779], [2207, 2447, 2687, 2927, 3167, 3407], [2221, 2251, 2281, 2311, 2341, 2371], [2339, 2459, 2579, 2699, 2819, 2939], [2351, 2441, 2531, 2621, 2711, 2801], [2377, 2647, 2917, 3187, 3457, 3727], [2437, 2557, 2677, 2797, 2917, 3037], [2467, 2617, 2767, 2917, 3067, 3217]]]]\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(primes_a_p(*i), o[0])"}
243
1,221
coding
Solve the programming task below in a Python markdown code block. Snuke has N hats. The i-th hat has an integer a_i written on it. There are N camels standing in a circle. Snuke will put one of his hats on each of these camels. If there exists a way to distribute the hats to the camels such that the following condition is satisfied for every camel, print `Yes`; otherwise, print `No`. * The bitwise XOR of the numbers written on the hats on both adjacent camels is equal to the number on the hat on itself. What is XOR? The bitwise XOR x_1 \oplus x_2 \oplus \ldots \oplus x_n of n non-negative integers x_1, x_2, \ldots, x_n is defined as follows: - When x_1 \oplus x_2 \oplus \ldots \oplus x_n is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if the number of integers among x_1, x_2, \ldots, x_n whose binary representations have 1 in the 2^k's place is odd, and 0 if that count is even. For example, 3 \oplus 5 = 6. Constraints * All values in input are integers. * 3 \leq N \leq 10^{5} * 0 \leq a_i \leq 10^{9} Input Input is given from Standard Input in the following format: N a_1 a_2 \ldots a_{N} Output Print the answer. Examples Input 3 1 2 3 Output Yes Input 4 1 2 4 8 Output No
{"inputs": ["3\n1 2 0", "3\n3 2 1", "3\n0 2 0", "3\n0 1 0", "3\n1 2 1", "3\n2 2 1", "3\n3 4 1", "3\n3 7 1"], "outputs": ["No\n", "Yes\n", "No\n", "No\n", "No\n", "No\n", "No\n", "No\n"]}
388
110
coding
Solve the programming task below in a Python markdown code block. Given an array of integers. Return an array, where the first element is the count of positives numbers and the second element is sum of negative numbers. If the input array is empty or null, return an empty array. # Example For input `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14, -15]`, you should return `[10, -65]`. Also feel free to reuse/extend the following starter code: ```python def count_positives_sum_negatives(arr): ```
{"functional": "_inputs = [[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14, -15]], [[0, 2, 3, 0, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14]], [[1]], [[-1]], [[0, 0, 0, 0, 0, 0, 0, 0, 0]], [[]]]\n_outputs = [[[10, -65]], [[8, -50]], [[1, 0]], [[0, -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(count_positives_sum_negatives(*i), o[0])"}
151
321
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.   Please complete the following python code precisely: ```python class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(nums = [2,7,11,15], target = 9) == [0,1]\n assert candidate(nums = [3,2,4], target = 6) == [1,2]\n assert candidate(nums = [3,3], target = 6) == [0,1]\n\n\ncheck(Solution().twoSum)"}
110
93
coding
Solve the programming task below in a Python markdown code block. Chef has two integers X and Y. Chef wants to perform some operations to make X and Y equal. In one operation, Chef can either: set X := X + 1 or set Y := Y + 2 Find the minimum number of operations required to make X and Y equal. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first and only line of each test case contains two space separated integers X and Y. ------ Output Format ------ For each test case, print the minimum number of operations required to make X and Y equal. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ X, Y ≤ 10^{9}$ ----- Sample Input 1 ------ 5 3 4 5 5 7 3 5 2 7 12 ----- Sample Output 1 ------ 1 0 2 3 5 ----- explanation 1 ------ Let $(x, y)$ denote the values of $X$ and $Y$ respectively. Test case $1$: Only one operation is required: $(3, 4) \xrightarrow{X := X + 1}{} (4, 4)$ Test case $2$: No operations are required since $X$ and $Y$ are already equal. Test case $3$: Two operations are required: $(7, 3) \xrightarrow{Y := Y + 2}{} (7, 5) \xrightarrow{Y := Y + 2}{} (7, 7)$ Test case $4$: Three operations are required. One such sequence of operations is: $(5, 2) \xrightarrow{Y := Y + 2}{} (5, 4) \xrightarrow{X := X + 1}{} (6, 4) \xrightarrow{Y := Y + 2}{} (6, 6)$
{"inputs": ["5\n3 4\n5 5\n7 3\n5 2\n7 12\n"], "outputs": ["1\n0\n2\n3\n5\n\n"]}
432
44
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 containing distinct numbers, and you can perform the following operations until the array is empty: If the first element has the smallest value, remove it Otherwise, put the first element at the end of the array. Return an integer denoting the number of operations it takes to make nums empty.   Please complete the following python code precisely: ```python class Solution: def countOperationsToEmptyArray(self, nums: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [3,4,-1]) == 5\n assert candidate(nums = [1,2,4,3]) == 5\n assert candidate(nums = [1,2,3]) == 3\n\n\ncheck(Solution().countOperationsToEmptyArray)"}
116
72
coding
Solve the programming task below in a Python markdown code block. There are N strings of lowercase alphabet only. The i-th string is S_i. Every string is unique. Provide answers for the Q queries below. The i-th query has the following format: Query: An integer k_i and a string p_{i,1}p_{i,2}...p_{i,26} that results from permuting {`a`,`b`,...,`z`} are given. Output the sequence of the string S_{k_i} among the N strings in lexicographical order when the literal sequence is p_{i,1}<p_{i,2}<...<p_{i,26}. Constraints * 1 ≦ N,Q ≦ 100000 * 1 ≦ |S_i| (1 ≦ i ≦ N) * S_i (1 ≦ i ≦ N) is a string of lowercase alphabet. * The sum of |S_i| is no more than 400000. * Every S_i is unique. * 1 ≦ k_i ≦ N (1 ≦ i ≦ Q) * For all 1 ≦ i ≦ Q, p_{i,1}p_{i,2}...p_{i,26} is a permutation of `abcd...z`. Input Inputs are provided from standard inputs in the following form. N S_1 : S_N Q k_1 p_{1,1}p_{1,2}...p_{1,26} : k_Q p_{Q,1}p_{Q,2}...p_{Q,26} Output Output Q lines. On line i, for the i-th query, output an integer indicating the sequence of the string S_{k_i} among the N strings in lexicographical order. Examples Input 5 aa abbaa abbba aaab aaaaaba 5 1 abcdefghijklmnopqrstuvwxyz 2 bacdefghijklmnopqrstuvwxyz 3 abcdefghijklmnopqrstuvwxyz 4 bacdefghijklmnopqrstuvwxyz 5 abcdefghijklmnopqrstuvwxyz Output 1 2 5 4 2 Input 8 abrakatabra abadaba abracadabra atcoder grand contest ababa a 6 3 abcdefghijklmnopqrstuvwxyz 6 qwertyuiopasdfghjklzxcvbnm 8 poiuytrewqlkjhgfdsamnbvcxz 2 qazwsxedcrfvtgbyhnujmikolp 1 plokmijnuhbygvtfcrdxeszwaq 4 mnbvcxzasdfghjklpoiuytrewq Output 4 8 2 3 4 7
{"inputs": ["5\naa\nabbaa\nabbba\naaab\naaaaaba\n5\n1 abcdefghijklmnopqrstuvwxyz\n2 bacdefghijklmnopqrstuvwxyz\n3 abcdefghijklmnopqrstuvwxyz\n4 bacdefghijklmnopqrsttvwxyz\n5 abcdefghijklmnopqrstuvwxyz", "5\naa\nabbaa\nabbba\naaab\naaaaaba\n5\n1 abcdefghijklmnopqrstuvwxyz\n2 bacdefghijklmnopqrstuvwxyz\n3 abcdefghijklmnopqrstuvwxyz\n4 bacdefghijklmnopqrsttvwxyz\n1 abcdefghijklmnopqrstuvwxyz", "5\naa\nabbaa\nabbba\naaab\nabaaaaa\n5\n1 abcdefghijklmnopqrstuvwxyz\n2 bacdefghijklmnopqrstuvwxyz\n3 abcdefghijklmnopqrstuvwxyz\n4 bacdefghijklmnopqrsttvwxyz\n1 abcdefghijklmnopqrstuvwxyz", "5\naa\nbbbaa\nabbba\naaab\naaaaaba\n5\n1 abcdefghmjklinopqrstuvwxyz\n2 bacdefghijklmnopqrstuxwvyz\n3 abcdefghijklmnopqrstuvwxyz\n4 bacdefghijklmnopqrsttvwxyz\n5 abcdefghijkllnopqrstuvwxyz", "5\naa\naabba\nabbba\naaab\naaaaaba\n5\n1 abcdefghijklmnopqrstuvwxyz\n2 bacdefghijklmnopqrstuvwxyz\n3 abcdefghijklmnopqrstuvwxyz\n4 bacdefghijklmnopqrsttvwxyz\n1 abcdefghijklmnopqrstuvwxyz", "5\naa\nabbaa\nabbba\naaab\naaaaaba\n5\n1 abcdefghmjklinopqrstuvwxyz\n2 bacdefghijklmnopqrstuxwvyz\n3 abcdefghijklmnopqrstuvwxyz\n4 zyxwvttsrqponmlkjihgfedcab\n5 abcdefghijklmnopqrstuvwxyz", "5\naa\nabbaa\nabbba\naaab\naabaaba\n5\n1 abcdefghmjklinopqrstuvwxyz\n2 bacdefghijklmnopqrstuxwvyz\n3 abcdefghijklmnopqrstuvwxyz\n4 bacdefghijklmnopqrsttvwxyz\n5 abcdefghijkllnopqrstuvwxyz", "5\naa\nbbbaa\nabbba\naaab\nabaaaaa\n5\n1 abcdefghmjklinopqrstuvwxyz\n2 bacdefghijklmnopqrstuxwvyz\n3 abcdefghijklmnopqrstuvwxyz\n4 bacdefghijklmnopqrsttvwxyz\n5 abcdefghijkllnopqrstuvwxyz"], "outputs": ["1\n2\n5\n4\n2\n", "1\n2\n5\n4\n1\n", "1\n2\n5\n5\n1\n", "1\n1\n4\n4\n2\n", "1\n3\n5\n4\n1\n", "1\n2\n5\n3\n2\n", "1\n2\n5\n5\n3\n", "1\n1\n4\n5\n3\n"]}
607
639
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given the root of a binary tree, return the postorder traversal of its nodes' values.   Please complete the following python code precisely: ```python # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(root = tree_node([1,None,2,3])) == [3,2,1]\n assert candidate(root = tree_node([])) == []\n assert candidate(root = tree_node([1])) == [1]\n\n\ncheck(Solution().postorderTraversal)"}
122
72
coding
Solve the programming task below in a Python markdown code block. For all x in the range of integers [0, 2 ** n), let y[x] be the binary exclusive-or of x and x // 2. Find the sum of all numbers in y. Write a function sum_them that, given n, will return the value of the above sum. This can be implemented a simple loop as shown in the initial code. But once n starts getting to higher numbers, such as 2000 (which will be tested), the loop is too slow. There is a simple solution that can quickly find the sum. Find it! Assume that n is a nonnegative integer. Hint: The complete solution can be written in two lines. Also feel free to reuse/extend the following starter code: ```python def sum_them(n): ```
{"functional": "_inputs = [[0], [1], [2], [3], [4]]\n_outputs = [[0], [1], [6], [28], [120]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(sum_them(*i), o[0])"}
176
182
coding
Solve the programming task below in a Python markdown code block. Vasya owns three big integers — $a, l, r$. Let's define a partition of $x$ such a sequence of strings $s_1, s_2, \dots, s_k$ that $s_1 + s_2 + \dots + s_k = x$, where $+$ is a concatanation of strings. $s_i$ is the $i$-th element of the partition. For example, number $12345$ has the following partitions: ["1", "2", "3", "4", "5"], ["123", "4", "5"], ["1", "2345"], ["12345"] and lots of others. Let's call some partition of $a$ beautiful if each of its elements contains no leading zeros. Vasya want to know the number of beautiful partitions of number $a$, which has each of $s_i$ satisfy the condition $l \le s_i \le r$. Note that the comparison is the integer comparison, not the string one. Help Vasya to count the amount of partitions of number $a$ such that they match all the given requirements. The result can be rather big, so print it modulo $998244353$. -----Input----- The first line contains a single integer $a~(1 \le a \le 10^{1000000})$. The second line contains a single integer $l~(0 \le l \le 10^{1000000})$. The third line contains a single integer $r~(0 \le r \le 10^{1000000})$. It is guaranteed that $l \le r$. It is also guaranteed that numbers $a, l, r$ contain no leading zeros. -----Output----- Print a single integer — the amount of partitions of number $a$ such that they match all the given requirements modulo $998244353$. -----Examples----- Input 135 1 15 Output 2 Input 10000 0 9 Output 1 -----Note----- In the first test case, there are two good partitions $13+5$ and $1+3+5$. In the second test case, there is one good partition $1+0+0+0+0$.
{"inputs": ["123\n0\n0\n", "123\n0\n0\n", "123\n0\n1\n", "135\n2\n4\n", "135\n4\n4\n", "135\n6\n4\n", "135\n6\n2\n", "135\n6\n5\n"], "outputs": ["0\n", "0", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
531
117
coding
Solve the programming task below in a Python markdown code block. Given two positive numbers B and C, what is the minimum positive value of A, such that A \cdot B is divisible by C. Here, A \cdot B denotes the value obtained when A is multiplied by B. ------ Input Format ------ - The first line will contain an integer T - number of test cases. Then the test cases follow. - The first and only line of each test case contains two integers B and C. ------ Output Format ------ For each test case, output the minimum value of A such that A \cdot B is divisible by C. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ B, C ≤ 10^{9}$ ----- Sample Input 1 ------ 2 2 4 8 12 ----- Sample Output 1 ------ 2 3 ----- explanation 1 ------ Test case $1$: We have $B = 2$ and $C = 4$. If we choose $A = 2$, then $A \cdot B = 4$, which is divisible by $4$. Note that this is minimum positive value of $A$, such that $A \cdot B$ is divisible by $C$. Test case $2$: We have $B = 8$ and $C = 12$. If we choose $A = 3$, then $A \cdot B = 24$, which is divisible by $12$. Note that this is minimum positive value of $A$, such that $A \cdot B$ is divisible by $C$.
{"inputs": ["2\n2 4\n8 12"], "outputs": ["2\n3"]}
341
23
coding
Solve the programming task below in a Python markdown code block. The chef is trying to solve some pattern problems, Chef wants your help to code it. Chef has one number K to form a new pattern. Help the chef to code this pattern problem. -----Input:----- - First-line will contain $T$, the number of test cases. Then the test cases follow. - Each test case contains a single line of input, one integer $K$. -----Output:----- For each test case, output as the pattern. -----Constraints----- - $1 \leq T \leq 100$ - $1 \leq K \leq 100$ -----Sample Input:----- 4 1 2 3 4 -----Sample Output:----- 1 21 *1 321 *21 **1 4321 *321 **21 ***1 -----EXPLANATION:----- No need, else pattern can be decode easily.
{"inputs": ["4\n1\n2\n3\n4"], "outputs": ["1\n21\n*1\n321\n*21\n**1\n4321\n*321\n**21\n***1"]}
212
54
coding
Solve the programming task below in a Python markdown code block. HELP! Jason can't find his textbook! It is two days before the test date, and Jason's textbooks are all out of order! Help him sort a list (ArrayList in java) full of textbooks by subject, so he can study before the test. The sorting should **NOT** be case sensitive Also feel free to reuse/extend the following starter code: ```python def sorter(textbooks): ```
{"functional": "_inputs = [[['Algebra', 'History', 'Geometry', 'English']], [['Algebra', 'history', 'Geometry', 'english']], [['Alg#bra', '$istory', 'Geom^try', '**english']]]\n_outputs = [[['Algebra', 'English', 'Geometry', 'History']], [['Algebra', 'english', 'Geometry', 'history']], [['$istory', '**english', 'Alg#bra', 'Geom^try']]]\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(sorter(*i), o[0])"}
96
237
coding
Solve the programming task below in a Python markdown code block. Vasya became interested in bioinformatics. He's going to write an article about similar cyclic DNA sequences, so he invented a new method for determining the similarity of cyclic sequences. Let's assume that strings s and t have the same length n, then the function h(s, t) is defined as the number of positions in which the respective symbols of s and t are the same. Function h(s, t) can be used to define the function of Vasya distance ρ(s, t): $\rho(s, t) = \sum_{i = 0}^{n - 1} \sum_{j = 0}^{n - 1} h(\operatorname{shift}(s, i), \operatorname{shift}(t, j))$ where $\operatorname{shift}(s, i)$ is obtained from string s, by applying left circular shift i times. For example, ρ("AGC", "CGT") = h("AGC", "CGT") + h("AGC", "GTC") + h("AGC", "TCG") + h("GCA", "CGT") + h("GCA", "GTC") + h("GCA", "TCG") + h("CAG", "CGT") + h("CAG", "GTC") + h("CAG", "TCG") = 1 + 1 + 0 + 0 + 1 + 1 + 1 + 0 + 1 = 6 Vasya found a string s of length n on the Internet. Now he wants to count how many strings t there are such that the Vasya distance from the string s attains maximum possible value. Formally speaking, t must satisfy the equation: $\rho(s, t) = \operatorname{max}_{u :|u|=|s|} \rho(s, u)$. Vasya could not try all possible strings to find an answer, so he needs your help. As the answer may be very large, count the number of such strings modulo 10^9 + 7. -----Input----- The first line of the input contains a single integer n (1 ≤ n ≤ 10^5). The second line of the input contains a single string of length n, consisting of characters "ACGT". -----Output----- Print a single number — the answer modulo 10^9 + 7. -----Examples----- Input 1 C Output 1 Input 2 AG Output 4 Input 3 TTT Output 1 -----Note----- Please note that if for two distinct strings t_1 and t_2 values ρ(s, t_1) и ρ(s, t_2) are maximum among all possible t, then both strings must be taken into account in the answer even if one of them can be obtained by a circular shift of another one. In the first sample, there is ρ("C", "C") = 1, for the remaining strings t of length 1 the value of ρ(s, t) is 0. In the second sample, ρ("AG", "AG") = ρ("AG", "GA") = ρ("AG", "AA") = ρ("AG", "GG") = 4. In the third sample, ρ("TTT", "TTT") = 27
{"inputs": ["1\nC\n", "1\nT\n", "1\nT\n", "1\nA\n", "1\nG\n", "1\nC\n", "2\nAG\n", "2\nAG\n"], "outputs": ["1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "4\n", "4\n"]}
736
86
coding
Solve the programming task below in a Python markdown code block. Zurikela is creating a graph with a special graph maker. At the begining, it is empty and has no nodes or edges. He can perform $3$ types of operations: $\mbox{A}$ $\boldsymbol{x}$: Create a set of $\boldsymbol{x}$ new nodes and name it $\boldsymbol{set}$-$\mbox{K}$. $\mbox{B}$ $\boldsymbol{x}$ $y$: Create edges between nodes of $\boldsymbol{set}$-$\boldsymbol{x}$ and $\boldsymbol{set}$-$y$. $\mbox{C}$ $\boldsymbol{x}$: Create a set composed of nodes from $\boldsymbol{set}$-$\boldsymbol{x}$ and its directly and indirectly connected nodes, called $\boldsymbol{set}$-$\mbox{K}$. Note that each node can only exist in one set, so other sets become empty. The first $\boldsymbol{set}$'s name will be $\boldsymbol{set}$-$\mbox{1}$. In first and third operation $\mbox{K}$ is referring to the index of new set: K = [index of last created set] + 1 Create the graph by completing the $\mbox{Q}$ operations specified during input. Then calculate the maximum number of independent nodes (i.e.:how many nodes in the final graph which don't have direct edge between them). Input Format The first line contains $\mbox{Q}$. The $\mbox{Q}$ subsequent lines each contain an operation to be performed. Constraints $1\leq Q\leq10^5$. For the first operation, $1\leq x\leq10^4$. For the second operation, $x<y$ and all $y$s are distinct. For the second and third operation, it's guaranteed that $\boldsymbol{set}$-$\boldsymbol{x}$ and $\boldsymbol{set}$-$y$ exist. Output Format Print maximum number of independent nodes in the final graph (i.e.: nodes which have no direct connection to one another). Sample Input 8 A 1 A 2 B 1 2 C 1 A 2 A 3 B 3 4 B 4 5 Sample Output 5 Explanation There are $8$ operations. After first operation$\left({A\mbox{1}}\right)$: After second operation$\left({A\mbox{2}}\right)$: After third operation$(B\:1\:2)$: After fourth operation$\left({C1}\right)$: After fifth and sixth operation $\left({A\mbox{2}}\right)$ and $(A3)$: After seventh operation$\left({B\:\:3\:\:4}\right)$: After eigth operation$\left(B\:4\:5\right)$: There are $2$ independent nodes in $\boldsymbol{set}$-$3$ and $3$ independent nodes in $\boldsymbol{set}$-$5$, so we print their sum ($5$) as our answer.
{"inputs": ["8\nA 1\nA 2\nB 1 2\nC 1\nA 2\nA 3\nB 3 4\nB 4 5\n"], "outputs": ["5\n"]}
680
52
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are playing a simplified PAC-MAN game on an infinite 2-D grid. You start at the point [0, 0], and you are given a destination point target = [xtarget, ytarget] that you are trying to get to. There are several ghosts on the map with their starting positions given as a 2D array ghosts, where ghosts[i] = [xi, yi] represents the starting position of the ith ghost. All inputs are integral coordinates. Each turn, you and all the ghosts may independently choose to either move 1 unit in any of the four cardinal directions: north, east, south, or west, or stay still. All actions happen simultaneously. You escape if and only if you can reach the target before any ghost reaches you. If you reach any square (including the target) at the same time as a ghost, it does not count as an escape. Return true if it is possible to escape regardless of how the ghosts move, otherwise return false.   Please complete the following python code precisely: ```python class Solution: def escapeGhosts(self, ghosts: List[List[int]], target: List[int]) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(ghosts = [[1,0],[0,3]], target = [0,1]) == True\n assert candidate(ghosts = [[1,0]], target = [2,0]) == False\n assert candidate(ghosts = [[2,0]], target = [1,0]) == False\n\n\ncheck(Solution().escapeGhosts)"}
257
90
coding
Solve the programming task below in a Python markdown code block. The chef is trying to decode some pattern problems, Chef wants your help to code it. Chef has one number K(odd) to form a new pattern. Help the chef to code this pattern problem. -----Input:----- - First-line will contain $T$, the number of test cases. Then the test cases follow. - Each test case contains a single line of input, one integer $K$. -----Output:----- For each test case, output as the pattern. -----Constraints----- - $1 \leq T \leq 100$ - $1 \leq K \leq 100$ -----Sample Input:----- 4 1 3 5 7 -----Sample Output:----- 1 111 111 111 11111 11 11 1 1 1 11 11 11111 1111111 11 11 1 1 1 1 1 1 1 1 1 1 1 11 11 1111111 -----EXPLANATION:----- No need, else pattern can be decode easily.
{"inputs": ["4\n1\n3\n5\n7"], "outputs": ["1\n111\n111\n111\n11111\n11 11\n1 1 1\n11 11\n11111\n1111111\n11 11\n1 1 1 1\n1 1 1\n1 1 1 1\n11 11\n1111111"]}
277
116
coding
Solve the programming task below in a Python markdown code block. One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a. Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins. Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible. More formally, you need to find such integer a (1 ≤ a ≤ n), that the probability that $|c - a|<|c - m|$ is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive). -----Input----- The first line contains two integers n and m (1 ≤ m ≤ n ≤ 10^9) — the range of numbers in the game, and the number selected by Misha respectively. -----Output----- Print a single number — such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them. -----Examples----- Input 3 1 Output 2 Input 4 3 Output 2 -----Note----- In the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0. In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less.
{"inputs": ["3 1\n", "4 3\n", "5 5\n", "1 1\n", "1 1\n", "2 1\n", "2 2\n", "3 1\n"], "outputs": ["2", "2", "4", "1", "1", "2", "1", "2"]}
446
78
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. The k-beauty of an integer num is defined as the number of substrings of num when it is read as a string that meet the following conditions: It has a length of k. It is a divisor of num. Given integers num and k, return the k-beauty of num. Note: Leading zeros are allowed. 0 is not a divisor of any value. A substring is a contiguous sequence of characters in a string.   Please complete the following python code precisely: ```python class Solution: def divisorSubstrings(self, num: int, k: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(num = 240, k = 2) == 2\n assert candidate(num = 430043, k = 2) == 2\n\n\ncheck(Solution().divisorSubstrings)"}
139
62
coding
Solve the programming task below in a Python markdown code block. You are given two positive integers $A$ and $B$. Find the number of pairs of positive integers $(X, Y)$ such that $1 \le X \le A$, $1 \le Y \le B$ and $X + Y$ is even. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $A$ and $B$. -----Output----- For each test case, print a single line containing one integer ― the number of valid pairs. -----Constraints----- - $1 \le T \le 1,000$ - $1 \le A, B \le 10^9$ -----Subtasks----- Subtask #1 (10 points): $A, B \le 10$ Subtask #2 (10 points): $A, B \le 1,000$ Subtask #3 (80 points): original constraints -----Example Input----- 4 1 1 2 3 4 6 8 9 -----Example Output----- 1 3 12 36
{"inputs": ["4\n1 1\n2 3\n4 6\n8 9"], "outputs": ["1\n3\n12\n36"]}
274
36
coding
Solve the programming task below in a Python markdown code block. problem I want to put as many rectangular tiles as possible on a rectangular wall with a size of $ h $ in height and $ w $ in width, and a size of $ a $ in height and $ b $ in width. The following conditions must be met when attaching tiles. * Do not stack tiles. * Do not apply tiles diagonally, that is, any edge of the tile is parallel or perpendicular to any edge of the wall. * Do not change the orientation of the tiles, that is, do not swap the vertical and horizontal directions. When as many tiles as possible are pasted, find the sum of the areas not covered by the tiles. output Output the total area of ​​the part not covered by the tile. Also, output a line break at the end. Example Input 5 8 2 2 Output 8
{"inputs": ["5 8\n3 2", "5 8\n6 2", "6 8\n6 3", "5 8\n1 3", "1 7\n2 6", "5 8\n2 2", "12 8\n6 3", "12 8\n9 3"], "outputs": ["16\n", "40\n", "12\n", "10\n", "7\n", "8", "24\n", "42\n"]}
189
117
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. A subsequence of nums is called a square streak if: The length of the subsequence is at least 2, and after sorting the subsequence, each element (except the first element) is the square of the previous number. Return the length of the longest square streak in nums, or return -1 if there is no square streak. A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.   Please complete the following python code precisely: ```python class Solution: def longestSquareStreak(self, nums: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [4,3,6,16,8,2]) == 3\n assert candidate(nums = [2,3,5,6,7]) == -1\n\n\ncheck(Solution().longestSquareStreak)"}
156
65
coding
Solve the programming task below in a Python markdown code block. Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge. Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him. Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps. Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps. Alice's *angriness* is defined to be the number of pairs (i, j) such that 1 ≤ i < j ≤ N and A_{i} > A_{j}. Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum *angriness* he can achieve? ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make. ------ Output Format ------ - For each test case, output on a new line a single integer: Alice's maximum possible angriness. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{9}$ $1 ≤ K ≤ 10^{9}$ ----- Sample Input 1 ------ 4 7 3 1 4 8 2 5 3 ----- Sample Output 1 ------ 21 0 22 10 ----- explanation 1 ------ Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows: - First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$ - Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$ - Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$ The angriness of this sequence is $21$, which is the maximum possible. Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.
{"inputs": ["4\n7 3\n1 4\n8 2\n5 3\n"], "outputs": ["21\n0\n22\n10\n"]}
606
39
coding
Solve the programming task below in a Python markdown code block. On Bertown's main street n trees are growing, the tree number i has the height of ai meters (1 ≤ i ≤ n). By the arrival of the President of Berland these trees were decided to be changed so that their heights formed a beautiful sequence. This means that the heights of trees on ends (the 1st one and the n-th one) should be equal to each other, the heights of the 2-nd and the (n - 1)-th tree must also be equal to each other, at that the height of the 2-nd tree should be larger than the height of the first tree by 1, and so on. In other words, the heights of the trees, standing at equal distance from the edge (of one end of the sequence) must be equal to each other, and with the increasing of the distance from the edge by 1 the tree height must also increase by 1. For example, the sequences "2 3 4 5 5 4 3 2" and "1 2 3 2 1" are beautiful, and '1 3 3 1" and "1 2 3 1" are not. Changing the height of a tree is a very expensive operation, using advanced technologies invented by Berland scientists. In one operation you can choose any tree and change its height to any number, either increase or decrease. Note that even after the change the height should remain a positive integer, i. e, it can't be less than or equal to zero. Identify the smallest number of changes of the trees' height needed for the sequence of their heights to become beautiful. Input The first line contains integer n (1 ≤ n ≤ 105) which is the number of trees. The second line contains integers ai (1 ≤ ai ≤ 105) which are the heights of the trees. Output Print a single number which is the minimal number of trees whose heights will have to be changed for the sequence to become beautiful. Examples Input 3 2 2 2 Output 1 Input 4 1 2 2 1 Output 0
{"inputs": ["1\n511\n", "1\n909\n", "1\n587\n", "1\n645\n", "1\n637\n", "1\n731\n", "1\n211\n", "1\n2727\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
465
103
coding
Solve the programming task below in a Python markdown code block. Complete the function that takes 3 numbers `x, y and k` (where `x ≤ y`), and returns the number of integers within the range `[x..y]` (both ends included) that are divisible by `k`. More scientifically: `{ i : x ≤ i ≤ y, i mod k = 0 }` ## Example Given ```x = 6, y = 11, k = 2``` the function should return `3`, because there are three numbers divisible by `2` between `6` and `11`: `6, 8, 10` - **Note**: The test cases are very large. You will need a O(log n) solution or better to pass. (A constant time solution is possible.) Also feel free to reuse/extend the following starter code: ```python def divisible_count(x,y,k): ```
{"functional": "_inputs = [[6, 11, 2], [11, 345, 17], [0, 1, 7], [20, 20, 2], [20, 20, 8], [19, 20, 2], [0, 10, 1], [11, 14, 2], [101, 9999999999999999999999999999999999999999999, 11], [1005, 9999999999999999999999999999999999999999999, 109]]\n_outputs = [[3], [20], [1], [1], [0], [1], [11], [2], [909090909090909090909090909090909090909081], [91743119266055045871559633027522935779807]]\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(divisible_count(*i), o[0])"}
198
458
coding
Solve the programming task below in a Python markdown code block. An array of integers $p_1, p_2, \dots, p_n$ is called a permutation if it contains each number from $1$ to $n$ exactly once. For example, the following arrays are permutations: $[3, 1, 2]$, $[1]$, $[1, 2, 3, 4, 5]$ and $[4, 3, 1, 2]$. The following arrays are not permutations: $[2]$, $[1, 1]$, $[2, 3, 4]$. Polycarp invented a really cool permutation $p_1, p_2, \dots, p_n$ of length $n$. It is very disappointing, but he forgot this permutation. He only remembers the array $q_1, q_2, \dots, q_{n-1}$ of length $n-1$, where $q_i=p_{i+1}-p_i$. Given $n$ and $q=q_1, q_2, \dots, q_{n-1}$, help Polycarp restore the invented permutation. -----Input----- The first line contains the integer $n$ ($2 \le n \le 2\cdot10^5$) — the length of the permutation to restore. The second line contains $n-1$ integers $q_1, q_2, \dots, q_{n-1}$ ($-n < q_i < n$). -----Output----- Print the integer -1 if there is no such permutation of length $n$ which corresponds to the given array $q$. Otherwise, if it exists, print $p_1, p_2, \dots, p_n$. Print any such permutation if there are many of them. -----Examples----- Input 3 -2 1 Output 3 1 2 Input 5 1 1 1 1 Output 1 2 3 4 5 Input 4 -1 2 2 Output -1
{"inputs": ["2\n0\n", "2\n1\n", "2\n0\n", "2\n1\n", "2\n2\n", "2\n4\n", "2\n3\n", "2\n7\n"], "outputs": ["-1\n", "1 2 ", "-1\n", "1 2 ", "-1\n", "-1\n", "-1\n", "-1\n"]}
452
89
coding
Solve the programming task below in a Python markdown code block. You're hanging out with your friends in a bar, when suddenly one of them is so drunk, that he can't speak, and when he wants to say something, he writes it down on a paper. However, none of the words he writes make sense to you. He wants to help you, so he points at a beer and writes "yvvi". You start to understand what he's trying to say, and you write a script, that decodes his words. Keep in mind that numbers, as well as other characters, can be part of the input, and you should keep them like they are. You should also test if the input is a string. If it is not, return "Input is not a string". Also feel free to reuse/extend the following starter code: ```python def decode(string_): ```
{"functional": "_inputs = [['yvvi'], ['Blf zoivzwb szw 10 yvvih'], [\"Ovg'h hdrn rm gsv ulfmgzrm!\"], [{'brand': 'Starobrno'}], [\"Tl slnv, blf'iv wifmp\"], [\"Hfiv r xzm wzmxv lm xlk'h xzi, slow nb yvvi\"], [True], [\"Hvv? R'n mlg gszg wifmp, r xzm hgroo gzpv nb xolgsvh luu\"], [123], [['Beer']]]\n_outputs = [['beer'], ['You already had 10 beers'], [\"Let's swim in the fountain!\"], ['Input is not a string'], [\"Go home, you're drunk\"], [\"Sure i can dance on cop's car, hold my beer\"], ['Input is not a string'], [\"See? I'm not that drunk, i can still take my clothes off\"], ['Input is not a string'], ['Input is not a 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(decode(*i), o[0])"}
183
379
coding
Solve the programming task below in a Python markdown code block. You are given a binary string $s$. Find the number of distinct cyclical binary strings of length $n$ which contain $s$ as a substring. The cyclical string $t$ contains $s$ as a substring if there is some cyclical shift of string $t$, such that $s$ is a substring of this cyclical shift of $t$. For example, the cyclical string "000111" contains substrings "001", "01110" and "10", but doesn't contain "0110" and "10110". Two cyclical strings are called different if they differ from each other as strings. For example, two different strings, which differ from each other by a cyclical shift, are still considered different cyclical strings. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 40$) — the length of the target string $t$. The next line contains the string $s$ ($1 \le |s| \le n$) — the string which must be a substring of cyclical string $t$. String $s$ contains only characters '0' and '1'. -----Output----- Print the only integer — the number of distinct cyclical binary strings $t$, which contain $s$ as a substring. -----Examples----- Input 2 0 Output 3 Input 4 1010 Output 2 Input 20 10101010101010 Output 962 -----Note----- In the first example, there are three cyclical strings, which contain "0" — "00", "01" and "10". In the second example, there are only two such strings — "1010", "0101".
{"inputs": ["2\n0\n", "1\n1\n", "5\n1\n", "1\n0\n", "5\n1\n", "1\n0\n", "1\n1\n", "2\n0\n"], "outputs": ["3", "1", "31", "1", "31\n", "1\n", "1\n", "3\n"]}
415
84
coding
Solve the programming task below in a Python markdown code block. You are given a positive integer $n$. Your task is to find any three integers $a$, $b$ and $c$ ($0 \le a, b, c \le 10^9$) for which $(a\oplus b)+(b\oplus c)+(a\oplus c)=n$, or determine that there are no such integers. Here $a \oplus b$ denotes the bitwise XOR of $a$ and $b$. For example, $2 \oplus 4 = 6$ and $3 \oplus 1=2$. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The following lines contain the descriptions of the test cases. The only line of each test case contains a single integer $n$ ($1 \le n \le 10^9$). -----Output----- For each test case, print any three integers $a$, $b$ and $c$ ($0 \le a, b, c \le 10^9$) for which $(a\oplus b)+(b\oplus c)+(a\oplus c)=n$. If no such integers exist, print $-1$. -----Examples----- Input 5 4 1 12 2046 194723326 Output 3 3 1 -1 2 4 6 69 420 666 12345678 87654321 100000000 -----Note----- In the first test case, $a=3$, $b=3$, $c=1$, so $(3 \oplus 3)+(3 \oplus 1) + (3 \oplus 1)=0+2+2=4$. In the second test case, there are no solutions. In the third test case, $(2 \oplus 4)+(4 \oplus 6) + (2 \oplus 6)=6+2+4=12$.
{"inputs": ["5\n4\n1\n12\n2046\n194723326\n"], "outputs": ["0 2 2\n-1\n0 6 6\n0 1023 1023\n0 97361663 97361663\n"]}
484
81
coding
Solve the programming task below in a Python markdown code block. A function receives a certain numbers of integers ```n1, n2, n3 ..., np```(all positive and different from 0) and a factor ```k, k > 0``` The function rearranges the numbers ```n1, n2, ..., np``` in such order that generates the minimum number concatenating the digits and this number should be divisible by ```k```. The order that the function receives their arguments is: ```python rearranger(k, n1, n2, n3,....,np) ``` ## Examples ```python rearranger(4, 32, 3, 34, 7, 12) == "Rearrangement: 12, 3, 34, 7, 32 generates: 12334732 divisible by 4" rearranger(10, 32, 3, 34, 7, 12) == "There is no possible rearrangement" ``` If there are more than one possible arrengement for the same minimum number, your code should be able to handle those cases: ```python rearranger(6, 19, 32, 2, 124, 20, 22) == "Rearrangements: 124, 19, 20, 2, 22, 32 and 124, 19, 20, 22, 2, 32 generates: 124192022232 divisible by 6" ``` The arrangements should be in sorted order, as you see: `124, 19, 20, 2, 22, 32` comes first than `124, 19, 20, 22, 2, 32`. Have an enjoyable time! (Thanks to `ChristianE.Cooper` for his contribution to this kata) Also feel free to reuse/extend the following starter code: ```python def rearranger(k, *args): ```
{"functional": "_inputs = [[4, 32, 3, 34, 7, 12], [10, 32, 3, 34, 7, 12]]\n_outputs = [['Rearrangement: 12, 3, 34, 7, 32 generates: 12334732 divisible by 4'], ['There is no possible rearrangement']]\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(rearranger(*i), o[0])"}
477
239
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin chinese, Russian and Vietnamese as well. There are N minions who are competing in an election of the president of the ACM (Association of Cute Minions). They are standing in a line in the order from minion 1 to minion N. For each i (1 ≤ i ≤ N), the i-th minion has an influence level of S_{i}. A single minion may cast any number of votes. Minion j will vote for minion i (i ≠ j) if and only if the influence level of the j-th minion is greater than or equal to the sum of influence levels of all the minions standing between them (excluding the i-th and j-th minion). Your task is to find the number of votes received by each minion. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains a single integer N denoting the number of minions. The second line contains N space-separated integers S_{1}, S_{2}, ..., S_{N}. ------ Output ------ For each test case, print a single line containing N space-separated integers. For each valid i, the i-th of these integers should denote the number of votes the i-th minion will get. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ S_{i} ≤ 10^{9} for each valid i$ $sum of N over all test cases won't exceed 10^{6}$ ------ Subtasks ------ Subtask #1 (30 points): $1 ≤ N ≤ 500$ $sum of N over all test cases won't exceed 10,000$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 2 4 4 3 2 1 5 1 2 2 3 1 ----- Sample Output 1 ------ 1 2 3 2 2 3 2 3 1 ----- explanation 1 ------ Example case 1: The first minion will get only a vote of the second minion. The second minion will get votes of the first and third minion. The third minion will get votes of the first, second and fourth minion. The fourth minion will get votes of the second and third minion.
{"inputs": ["2\n4\n4 3 2 1\n5\n1 2 2 3 1"], "outputs": ["1 2 3 2\n2 3 2 3 1"]}
526
50
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Seven different symbols represent Roman numerals with the following values: Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 Roman numerals are formed by appending the conversions of decimal place values from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules: If the value does not start with 4 or 9, select the symbol of the maximal value that can be subtracted from the input, append that symbol to the result, subtract its value, and convert the remainder to a Roman numeral. If the value starts with 4 or 9 use the subtractive form representing one symbol subtracted from the following symbol, for example, 4 is 1 (I) less than 5 (V): IV and 9 is 1 (I) less than 10 (X): IX. Only the following subtractive forms are used: 4 (IV), 9 (IX), 40 (XL), 90 (XC), 400 (CD) and 900 (CM). Only powers of 10 (I, X, C, M) can be appended consecutively at most 3 times to represent multiples of 10. You cannot append 5 (V), 50 (L), or 500 (D) multiple times. If you need to append a symbol 4 times use the subtractive form. Given an integer, convert it to a Roman numeral.   Please complete the following python code precisely: ```python class Solution: def intToRoman(self, num: int) -> str: ```
{"functional": "def check(candidate):\n assert candidate(num = 3) == \"III\"\n assert candidate(num = 4) == \"IV\"\n assert candidate(num = 9) == \"IX\"\n assert candidate(num = 58) == \"LVIII\"\n assert candidate(num = 1994) == \"MCMXCIV\"\n\n\ncheck(Solution().intToRoman)"}
388
93
coding
Solve the programming task below in a Python markdown code block. You are given two binary strings $a$ and $b$ of the same length. You can perform the following two operations on the string $a$: Swap any two bits at indices $i$ and $j$ respectively ($1 \le i, j \le n$), the cost of this operation is $|i - j|$, that is, the absolute difference between $i$ and $j$. Select any arbitrary index $i$ ($1 \le i \le n$) and flip (change $0$ to $1$ or $1$ to $0$) the bit at this index. The cost of this operation is $1$. Find the minimum cost to make the string $a$ equal to $b$. It is not allowed to modify string $b$. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^6$) — the length of the strings $a$ and $b$. The second and third lines contain strings $a$ and $b$ respectively. Both strings $a$ and $b$ have length $n$ and contain only '0' and '1'. -----Output----- Output the minimum cost to make the string $a$ equal to $b$. -----Examples----- Input 3 100 001 Output 2 Input 4 0101 0011 Output 1 -----Note----- In the first example, one of the optimal solutions is to flip index $1$ and index $3$, the string $a$ changes in the following way: "100" $\to$ "000" $\to$ "001". The cost is $1 + 1 = 2$. The other optimal solution is to swap bits and indices $1$ and $3$, the string $a$ changes then "100" $\to$ "001", the cost is also $|1 - 3| = 2$. In the second example, the optimal solution is to swap bits at indices $2$ and $3$, the string $a$ changes as "0101" $\to$ "0011". The cost is $|2 - 3| = 1$.
{"inputs": ["1\n0\n1\n", "1\n1\n1\n", "1\n1\n1\n", "1\n0\n1\n", "2\n10\n01\n", "2\n10\n00\n", "2\n10\n01\n", "2\n10\n00\n"], "outputs": ["1\n", "0\n", "0\n", "1\n", "1\n", "1\n", "1\n", "1\n"]}
500
110
coding
Solve the programming task below in a Python markdown code block. In a bizarre game of chess ,knight was so drunk, that instead of his usual move he started walking straight. In every move Knight jumps on 2n steps forward (n is number of block that he had travelled so far from starting) but after that he has to take either 1 step forward or backward. Now the Knight needs to get to position X so King (i.e. You) needs to decide the order of his backward or forward step in such a way that he can reach its destination in minimum number of steps. Remember he always travels in a straight line and the length of the board is infinite. -----Input----- The first line of the input contains an integer T denoting the number of test cases, for each test case enter value X ( i.e. destination) Note : initially knight is at n = 1. -----Output----- For each test case the output should be string of numbers 1 & 2 where 1 denotes backward step and 2 denote the forward step Note : for no solution print 0. -----Constraints----- - 1 ≤ T ≤ 100 - 1 ≤ X ≤ 10^10 -----Example----- Input: 2 17 10 Output: 2111 0 -----Explanation----- Case 1 : starting from n = 1 , knight moves to n = 3 ('2') , 5 ('1') , 9 ('1') , 17 ('1') i.e. string printed is 2 1 1 1 Case 2 : no solution is possible
{"inputs": ["2\n17\n10"], "outputs": ["2111\n0"]}
342
23
coding
Solve the programming task below in a Python markdown code block. Chef likes prime numbers. However, there is one thing he loves even more. Of course, it's semi-primes! A semi-prime number is an integer which can be expressed as a product of two distinct primes. For example, $15 = 3 \cdot 5$ is a semi-prime number, but $1$, $9 = 3 \cdot 3$ and $5$ are not. Chef is wondering how to check if an integer can be expressed as a sum of two (not necessarily distinct) semi-primes. Help Chef with this tough task! -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains a single integer $N$. -----Output----- For each test case, print a single line containing the string "YES" if it is possible to express $N$ as a sum of two semi-primes or "NO" otherwise. -----Constraints----- - $1 \le T \le 200$ - $1 \le N \le 200$ -----Example Input----- 3 30 45 62 -----Example Output----- YES YES NO -----Explanation----- Example case 1: $N=30$ can be expressed as $15 + 15 = (3 \cdot 5) + (3 \cdot 5)$. Example case 2: $45$ can be expressed as $35 + 10 = (5 \cdot 7) + (2 \cdot 5)$. Example case 3: $62$ cannot be expressed as a sum of two semi-primes.
{"inputs": ["3\n30\n45\n62\n"], "outputs": ["YES\nYES\nNO"]}
381
26
coding
Solve the programming task below in a Python markdown code block. Little Artem has invented a time machine! He could go anywhere in time, but all his thoughts of course are with computer science. He wants to apply this time machine to a well-known data structure: multiset. Artem wants to create a basic multiset of integers. He wants these structure to support operations of three types: Add integer to the multiset. Note that the difference between set and multiset is that multiset may store several instances of one integer. Remove integer from the multiset. Only one instance of this integer is removed. Artem doesn't want to handle any exceptions, so he assumes that every time remove operation is called, that integer is presented in the multiset. Count the number of instances of the given integer that are stored in the multiset. But what about time machine? Artem doesn't simply apply operations to the multiset one by one, he now travels to different moments of time and apply his operation there. Consider the following example. First Artem adds integer 5 to the multiset at the 1-st moment of time. Then Artem adds integer 3 to the multiset at the moment 5. Then Artem asks how many 5 are there in the multiset at moment 6. The answer is 1. Then Artem returns back in time and asks how many integers 3 are there in the set at moment 4. Since 3 was added only at moment 5, the number of integers 3 at moment 4 equals to 0. Then Artem goes back in time again and removes 5 from the multiset at moment 3. Finally Artyom asks at moment 7 how many integers 5 are there in the set. The result is 0, since we have removed 5 at the moment 3. Note that Artem dislikes exceptions so much that he assures that after each change he makes all delete operations are applied only to element that is present in the multiset. The answer to the query of the third type is computed at the moment Artem makes the corresponding query and are not affected in any way by future changes he makes. Help Artem implement time travellers multiset. -----Input----- The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of Artem's queries. Then follow n lines with queries descriptions. Each of them contains three integers a_{i}, t_{i} and x_{i} (1 ≤ a_{i} ≤ 3, 1 ≤ t_{i}, x_{i} ≤ 10^9) — type of the query, moment of time Artem travels to in order to execute this query and the value of the query itself, respectively. It's guaranteed that all moments of time are distinct and that after each operation is applied all operations of the first and second types are consistent. -----Output----- For each ask operation output the number of instances of integer being queried at the given moment of time. -----Examples----- Input 6 1 1 5 3 5 5 1 2 5 3 6 5 2 3 5 3 7 5 Output 1 2 1 Input 3 1 1 1 2 2 1 3 3 1 Output 0
{"inputs": ["3\n1 1 1\n2 2 1\n3 3 1\n", "3\n1 1 1\n2 2 1\n3 3 1\n", "6\n1 1 5\n3 5 5\n1 2 5\n3 6 5\n2 3 5\n3 7 5\n", "6\n1 1 5\n3 5 5\n1 2 0\n3 6 5\n2 3 5\n3 7 5\n", "6\n1 1 5\n3 5 5\n1 2 0\n3 6 2\n2 3 5\n3 7 5\n", "6\n1 1 5\n3 5 5\n1 2 0\n2 6 2\n2 3 5\n3 7 5\n", "6\n1 1 5\n3 4 5\n1 2 0\n2 6 2\n2 3 6\n3 7 5\n", "6\n1 1 5\n3 5 5\n1 2 5\n3 6 5\n2 3 4\n3 7 5\n"], "outputs": ["0\n", "0\n", "1\n2\n1\n", "1\n1\n0\n", "1\n0\n0\n", "1\n0\n", "1\n1\n", "1\n2\n2\n"]}
706
342
coding
Solve the programming task below in a Python markdown code block. You are given n positive integers a_1, a_2, ..., a_{n}. For every a_{i} you need to find a positive integer k_{i} such that the decimal notation of 2^{k}_{i} contains the decimal notation of a_{i} as a substring among its last min(100, length(2^{k}_{i})) digits. Here length(m) is the length of the decimal notation of m. Note that you don't have to minimize k_{i}. The decimal notations in this problem do not contain leading zeros. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2 000) — the number of integers a_{i}. Each of the next n lines contains a positive integer a_{i} (1 ≤ a_{i} < 10^11). -----Output----- Print n lines. The i-th of them should contain a positive integer k_{i} such that the last min(100, length(2^{k}_{i})) digits of 2^{k}_{i} contain the decimal notation of a_{i} as a substring. Integers k_{i} must satisfy 1 ≤ k_{i} ≤ 10^50. It can be shown that the answer always exists under the given constraints. If there are multiple answers, print any of them. -----Examples----- Input 2 8 2 Output 3 1 Input 2 3 4857 Output 5 20
{"inputs": ["2\n8\n2\n", "2\n1\n2\n", "2\n8\n2\n", "2\n14\n2\n", "2\n14\n3\n", "2\n3\n4857\n", "2\n1\n4857\n", "2\n3\n4857\n"], "outputs": ["3\n1\n", "9\n1\n", "3\n1\n", "18\n1\n", "18\n5\n", "5\n20\n", "9\n20\n", "5\n20\n"]}
339
134
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Mandarin Chinese] and [Vietnamese] as well. Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls. Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour. Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum possible difference between the maximum and minimum number of balls in the chosen boxes. ------ Constraints ------ $1 ≤ T ≤ 5$ $2 ≤ M ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$, for each valid $i$ ------ Subtasks ------ Subtask #1 (30 points): $N ≤ 10^{3}$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 1 4 2 10 20 15 28 ----- Sample Output 1 ------ 5 ----- explanation 1 ------ Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR". There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are: - $(10, 28)$ with difference $28-10=18$ - $(10, 20)$ with difference $20-10=10$ - $(15, 28)$ with difference $28-15=13$ - $(15, 20)$ with difference $20-15=5$, which is the smallest possible
{"inputs": ["1 \n4 2 \n10 20 15 28"], "outputs": ["5"]}
576
32
coding
Solve the programming task below in a Python markdown code block. Mr. Pr and Ms. Ad are at $a$ and $b$ respectively on an infinite number line. Mr. Pr wants to meet Ms. Ad. Mr. Pr can choose to move $c$ or $d$ units in 1 second. If Mr. Pr moves $c$ units then Ms. Ad will move $d$ units and vice versa. (Both of them always moved in positive x-direction) You have to determine if Mr. Pr can meet with Ms. Ad after some integral amount of time, given that Mr. Pr chooses optimally. Note that meeting after a fractional amount of time does not count. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains four space separated integers, $a$, $b$, $c$, and $d$. -----Output:----- - For each test case, output a single line containing "YES" if Mr. Pr meets with Ms. Ad, otherwise "NO". -----Constraints----- - $1 \leq T \leq 10^5$ - $1 \leq a,b,c,d \leq 10^9$ -----Sample Input:----- 2 3 4 1 2 10 20 3 7 -----Sample Output:----- YES NO -----Explanation:----- In the first test case, Mr. Pr will move 2 units in the first second and Ms. Ad moves 1 unit simultaneously and they meet. In the second test case, it is impossible to meet (fractional time is not allowed).
{"inputs": ["2\n3 4 1 2\n10 20 3 7"], "outputs": ["YES\nNO"]}
350
32
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given two integer arrays inorder and postorder where inorder is the inorder traversal of a binary tree and postorder is the postorder traversal of the same tree, construct and return the binary tree.   Please complete the following python code precisely: ```python # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]: ```
{"functional": "def check(candidate):\n assert is_same_tree(candidate(inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]), tree_node([3,9,20,None,None,15,7]))\n assert is_same_tree(candidate(inorder = [-1], postorder = [-1]), tree_node([-1]))\n\n\ncheck(Solution().buildTree)"}
145
101
coding
Solve the programming task below in a Python markdown code block. Team RCB has earned X points in the games it has played so far in this year's IPL. To qualify for the playoffs they must earn at least a total of Y points. They currently have Z games left, in each game they earn 2 points for a win, 1 point for a draw, and no points for a loss. Is it possible for RCB to qualify for the playoffs this year? ------ Input Format ------ - First line will contain T, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, three integers X , Y, Z. ------ Output Format ------ For each test case, output in one line YES if it is possible for RCB to qualify for the playoffs, or NO if it is not possible to do so. Output is case insensitive, which means that "yes", "Yes", "YEs", "no", "nO" - all such strings will be acceptable. ------ Constraints ------ $1 ≤ T ≤ 5000$ $0 ≤ X,Y,Z ≤ 1000$ ----- Sample Input 1 ------ 3 4 10 8 3 6 1 4 8 2 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Test Case $1$: There are $8$ games remaining. Out of these $8$ games, if RCB wins $2$ games, loses $4$ games and draws the remaining $2$ games they will have a total of 10 points, this shows that it is possible for RCB to qualify for the playoffs. Note: There can be many other combinations which will lead to RCB qualifying for the playoffs. Test Case $2$: There is no way such that RCB can qualify for the playoffs. Test Case $3$: If RCB wins all their remaining games, they will have end up with $8$ points. ($4 + 2*2 = 8$), hence it is possible for them to qualify for the playoffs.
{"inputs": ["3\n4 10 8\n3 6 1 \n4 8 2 "], "outputs": ["YES\nNO\nYES\n"]}
444
38
coding
Solve the programming task below in a Python markdown code block. Hey CodeWarrior, we've got a lot to code today! I hope you know the basic string manipulation methods, because this kata will be all about them. Here we go... ## Background We've got a very long string, containing a bunch of User IDs. This string is a listing, which seperates each user ID with a comma and a whitespace ("' "). Sometimes there are more than only one whitespace. Keep this in mind! Futhermore, some user Ids are written only in lowercase, others are mixed lowercase and uppercase characters. Each user ID starts with the same 3 letter "uid", e.g. "uid345edj". But that's not all! Some stupid student edited the string and added some hashtags (#). User IDs containing hashtags are invalid, so these hashtags should be removed! ## Task 1. Remove all hashtags 2. Remove the leading "uid" from each user ID 3. Return an array of strings --> split the string 4. Each user ID should be written in only lowercase characters 5. Remove leading and trailing whitespaces --- ## Note Even if this kata can be solved by using Regex or Linq, please try to find a solution by using only C#'s string class. Some references for C#: - [Microsoft MDSN: Trim](https://msdn.microsoft.com/de-de/library/t97s7bs3%28v=vs.110%29.aspx) - [Microsoft MSDN: Split](https://msdn.microsoft.com/de-de/library/tabh47cf%28v=vs.110%29.aspx) - [Microsoft MSDN: ToLower](https://msdn.microsoft.com/en-us/library/system.string.tolower%28v=vs.110%29.aspx) - [Microsoft MSDN: Replace](https://msdn.microsoft.com/de-de/library/fk49wtc1%28v=vs.110%29.aspx) - [Microsoft MSDN: Substring](https://msdn.microsoft.com/de-de/library/aka44szs%28v=vs.110%29.aspx) Also feel free to reuse/extend the following starter code: ```python def get_users_ids(s): ```
{"functional": "_inputs = [['uid12345'], [' uidabc '], ['#uidswagger'], ['uidone, uidtwo'], ['uidCAPSLOCK'], ['uid##doublehashtag'], [' uidin name whitespace'], ['uidMultipleuid'], ['uid12 ab, uid#, uidMiXeDcHaRs'], [' uidT#e#S#t# ']]\n_outputs = [[['12345']], [['abc']], [['swagger']], [['one', 'two']], [['capslock']], [['doublehashtag']], [['in name whitespace']], [['multipleuid']], [['12 ab', '', 'mixedchars']], [['test']]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(get_users_ids(*i), o[0])"}
497
281
coding
Solve the programming task below in a Python markdown code block. Hacker Zhorik wants to decipher two secret messages he intercepted yesterday. Yeah message is a sequence of encrypted blocks, each of them consists of several bytes of information. Zhorik knows that each of the messages is an archive containing one or more files. Zhorik knows how each of these archives was transferred through the network: if an archive consists of k files of sizes l_1, l_2, ..., l_{k} bytes, then the i-th file is split to one or more blocks b_{i}, 1, b_{i}, 2, ..., b_{i}, m_{i} (here the total length of the blocks b_{i}, 1 + b_{i}, 2 + ... + b_{i}, m_{i} is equal to the length of the file l_{i}), and after that all blocks are transferred through the network, maintaining the order of files in the archive. Zhorik thinks that the two messages contain the same archive, because their total lengths are equal. However, each file can be split in blocks in different ways in the two messages. You are given the lengths of blocks in each of the two messages. Help Zhorik to determine what is the maximum number of files could be in the archive, if the Zhorik's assumption is correct. -----Input----- The first line contains two integers n, m (1 ≤ n, m ≤ 10^5) — the number of blocks in the first and in the second messages. The second line contains n integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 10^6) — the length of the blocks that form the first message. The third line contains m integers y_1, y_2, ..., y_{m} (1 ≤ y_{i} ≤ 10^6) — the length of the blocks that form the second message. It is guaranteed that x_1 + ... + x_{n} = y_1 + ... + y_{m}. Also, it is guaranteed that x_1 + ... + x_{n} ≤ 10^6. -----Output----- Print the maximum number of files the intercepted array could consist of. -----Examples----- Input 7 6 2 5 3 1 11 4 4 7 8 2 4 1 8 Output 3 Input 3 3 1 10 100 1 100 10 Output 2 Input 1 4 4 1 1 1 1 Output 1 -----Note----- In the first example the maximum number of files in the archive is 3. For example, it is possible that in the archive are three files of sizes 2 + 5 = 7, 15 = 3 + 1 + 11 = 8 + 2 + 4 + 1 and 4 + 4 = 8. In the second example it is possible that the archive contains two files of sizes 1 and 110 = 10 + 100 = 100 + 10. Note that the order of files is kept while transferring archives through the network, so we can't say that there are three files of sizes 1, 10 and 100. In the third example the only possibility is that the archive contains a single file of size 4.
{"inputs": ["1 1\n1\n1\n", "1 1\n1\n1\n", "1 1\n2\n2\n", "1 2\n2\n1 1\n", "1 2\n2\n1 1\n", "2 2\n1 1\n1 1\n", "2 2\n2 3\n4 1\n", "2 2\n2 3\n3 2\n"], "outputs": ["1\n", "1\n", "1\n", "1\n", "1\n", "2\n", "1\n", "1\n"]}
742
134
coding
Solve the programming task below in a Python markdown code block. Alice and Bob have invented a new game. They have a rooted tree having N nodes (rooted at node 1), and the i^{th} node has the value A_{i} associated with it (1-indexed). The value associated with the root node is 0. They start with a marker at the root node, and a score of 0. Alice and Bob moves alternatively, with Alice moving first. In one move, they can move the pointer from a node to one of its child node, and add the value associated with that child node to the score. If the marker reaches a leaf node, the game ends. Alice wants to minimize the score of the game, whereas Bob wants to maximize the score. They were about to start the game when the Charlie arrives. He looked at the game, and declared the game to be boring. However, Charlie asked them - "What will be the score at the end of the game, if \textbf{each} one of you can skip \textbf{at most} K moves?" Can you help Alice and Bob to answer Charlie? ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of testcases. The description of the T testcases follows. - The first line of each test case contains two space separated integers N and K. - The second line of each test case contains N space separated integers A_{1}, A_{2},..., A_{N}. It is guaranteed that A_{1} = 0. - The following N-1 lines contains two space separated integers x and y, which denotes that there is an edge between nodes x and y. ------ Output Format ------ For each testcase, output in a single line the answer to the Charlie's problem. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $0 ≤ K ≤ 200$ $-10^{9} ≤ A_{i} ≤ 10^{9}$ $1 ≤ x, y ≤ N$ - The sum of $N$ over all test cases does not exceed $10^{5}$ ----- Sample Input 1 ------ 3 3 1 0 1 2 1 2 1 3 4 0 0 1 2 3 1 2 1 3 2 4 7 0 0 2 1 4 2 7 3 1 2 1 3 2 4 2 5 3 6 3 7 ----- Sample Output 1 ------ 1 2 6 ----- explanation 1 ------ Test Case $1$: Alice can move the marker to node $2$, and hence making score $= 1$. If Alice skips her chance, then Bob can move the marker to node $3$, and hence making score = $2$. Therefore, Alice will not skip her chance. Test Case $2$: If Alice moves the marker to node $2$, then Bob will move the marker to node $4$, making score = $4$. If Alice moves the marker to node $3$, the game will end, and the score will be $2$.
{"inputs": ["3\n3 1\n0 1 2\n1 2\n1 3\n4 0\n0 1 2 3\n1 2\n1 3\n2 4\n7 0\n0 2 1 4 2 7 3\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n"], "outputs": ["1\n2\n6\n"]}
693
102
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a 0-indexed array of strings words. Each string consists of lowercase English letters only. No letter occurs more than once in any string of words. Two strings s1 and s2 are said to be connected if the set of letters of s2 can be obtained from the set of letters of s1 by any one of the following operations: Adding exactly one letter to the set of the letters of s1. Deleting exactly one letter from the set of the letters of s1. Replacing exactly one letter from the set of the letters of s1 with any letter, including itself. The array words can be divided into one or more non-intersecting groups. A string belongs to a group if any one of the following is true: It is connected to at least one other string of the group. It is the only string present in the group. Note that the strings in words should be grouped in such a manner that a string belonging to a group cannot be connected to a string present in any other group. It can be proved that such an arrangement is always unique. Return an array ans of size 2 where: ans[0] is the maximum number of groups words can be divided into, and ans[1] is the size of the largest group.   Please complete the following python code precisely: ```python class Solution: def groupStrings(self, words: List[str]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(words = [\"a\",\"b\",\"ab\",\"cde\"]) == [2,3]\n assert candidate(words = [\"a\",\"ab\",\"abc\"]) == [1,3]\n\n\ncheck(Solution().groupStrings)"}
308
64
coding
Solve the programming task below in a Python markdown code block. You are given an integer $n$ and an integer $k$. In one step you can do one of the following moves: decrease $n$ by $1$; divide $n$ by $k$ if $n$ is divisible by $k$. For example, if $n = 27$ and $k = 3$ you can do the following steps: $27 \rightarrow 26 \rightarrow 25 \rightarrow 24 \rightarrow 8 \rightarrow 7 \rightarrow 6 \rightarrow 2 \rightarrow 1 \rightarrow 0$. You are asked to calculate the minimum number of steps to reach $0$ from $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 100$) — the number of queries. The only line of each query contains two integers $n$ and $k$ ($1 \le n \le 10^{18}$, $2 \le k \le 10^{18}$). -----Output----- For each query print the minimum number of steps to reach $0$ from $n$ in single line. -----Example----- Input 2 59 3 1000000000000000000 10 Output 8 19 -----Note----- Steps for the first test case are: $59 \rightarrow 58 \rightarrow 57 \rightarrow 19 \rightarrow 18 \rightarrow 6 \rightarrow 2 \rightarrow 1 \rightarrow 0$. In the second test case you have to divide $n$ by $k$ $18$ times and then decrease $n$ by $1$.
{"inputs": ["1\n2 99\n", "1\n2 99\n", "1\n1 75\n", "1\n2 75\n", "1\n2 52\n", "1\n1 14\n", "1\n2 60\n", "1\n99 99\n"], "outputs": ["2\n", "2\n", "1\n", "2\n", "2\n", "1\n", "2\n", "2\n"]}
387
111