question
large_stringlengths
265
13.2k
Solve the programming task below in a Python markdown code block. Having learned the multiplication table, Takahashi can multiply two integers between 1 and 9 (inclusive) together. Given an integer N, determine whether N can be represented as the product of two integers between 1 and 9. If it can, print Yes; if it cannot, print No. -----Constraints----- - 1 \leq N \leq 100 - N is an integer. -----Input----- Input is given from Standard Input in the following format: N -----Output----- If N can be represented as the product of two integers between 1 and 9 (inclusive), print Yes; if it cannot, print No. -----Sample Input----- 10 -----Sample Output----- Yes 10 can be represented as, for example, 2 \times 5. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Example Input 2 3 1 3 1 0 Output 2 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Polycarp doesn't like integers that are divisible by $3$ or end with the digit $3$ in their decimal representation. Integers that meet both conditions are disliked by Polycarp, too. Polycarp starts to write out the positive (greater than $0$) integers which he likes: $1, 2, 4, 5, 7, 8, 10, 11, 14, 16, \dots$. Output the $k$-th element of this sequence (the elements are numbered from $1$). -----Input----- The first line contains one integer $t$ ($1 \le t \le 100$) — the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing one integer $k$ ($1 \le k \le 1000$). -----Output----- For each test case, output in a separate line one integer $x$ — the $k$-th element of the sequence that was written out by Polycarp. -----Examples----- Input 10 1 2 3 4 5 6 7 8 9 1000 Output 1 2 4 5 7 8 10 11 14 1666 -----Note----- None Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. A: Flick input A college student who loves 2D, commonly known as 2D, replaced his long-used Galapagos mobile phone with a smartphone. 2D, who operated the smartphone for the first time, noticed that the character input method was a little different from that of old mobile phones. On smartphones, a method called flick input was adopted by taking advantage of the fact that the screen can be touch-flicked (flick means "the operation of sliding and flipping a finger on the screen"). Flick input is a character input method as shown below. In flick input, as shown in Fig. A (a), input is performed using 10 buttons 0-9 (# and * are omitted this time). Each button is assigned one row-row row as shown in the figure. <image> --- Figure A: Smartphone button details One button can be operated by the method shown in Fig. A (b). In other words * If you just "touch" a button, the character "Adan" corresponding to that button will be output. * Press the button "flick to the left" to output "Idan" * When you press the button "flick upward", "Udan" is output. * Press the button "flick to the right" to output "Edan" * Press the button "flick down" to output "step" That's what it means. Figure A (c) shows how to input flicks for all buttons. Note that you flick button 0 upwards to output "n". Your job is to find the string that will be output as a result of a flick input operation, given a string that indicates that operation. Input A character string representing a flick input operation is given on one line (2 to 1000 characters). This character string consists of the numbers '0'-'9', which represent the buttons to be operated, and the five types of characters,'T',' L',' U',' R', and'D', which represent the flick direction. It consists of a combination. The characters that represent the flick direction have the following meanings. *'T': Just touch *'L': Flick left *'U': Flick up *'R': Flick right *'D': Flick down For example, "2D" represents the operation of "flicking the button of 2 downward", so "ko" can be output. In the given character string, one number and one character indicating the flick direction appear alternately. Also, it always starts with one number and ends with one letter indicating the flick direction. Furthermore, in Fig. A (c), flicking is not performed toward a place without characters (there is no input such as "8L"). Output Output the character string representing the result of the flick input operation in Roman characters. For romaji consonants, * Or line:'k' * Sayuki:'s' * Ta line:'t' * Line:'n' * Is the line:'h' * Ma line:'m' * Ya line:'y' * Line:'r' * Wa line:'w' to use. Romaji, which represents one hiragana character, should be output as a set of the consonants and vowels ('a','i','u','e','o') represented above. "shi" and "fu" are wrong because they violate this condition. However, the character "A line" should output only one vowel character, and "n" should output "n" twice in a row as "nn". Output a line break at the end of the character string. Sample Input 1 5R2D Sample Output 1 neko Sample Input 2 8U9U6U0T Sample Output 2 yuruhuwa Sample Input 3 9L4U7R1L2D0U4R3U4D Sample Output 3 ritumeikonntesuto Example Input 5R2D Output neko Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Dr. Moriarty is about to send a message to Sherlock Holmes. He has a string s. String p is called a substring of string s if you can read it starting from some position in the string s. For example, string "aba" has six substrings: "a", "b", "a", "ab", "ba", "aba". Dr. Moriarty plans to take string s and cut out some substring from it, let's call it t. Then he needs to change the substring t zero or more times. As a result, he should obtain a fixed string u (which is the string that should be sent to Sherlock Holmes). One change is defined as making one of the following actions: * Insert one letter to any end of the string. * Delete one letter from any end of the string. * Change one letter into any other one. Moriarty is very smart and after he chooses some substring t, he always makes the minimal number of changes to obtain u. Help Moriarty choose the best substring t from all substrings of the string s. The substring t should minimize the number of changes Moriarty should make to obtain the string u from it. Input The first line contains a non-empty string s, consisting of lowercase Latin letters. The second line contains a non-empty string u, consisting of lowercase Latin letters. The lengths of both strings are in the range from 1 to 2000, inclusive. Output Print the only integer — the minimum number of changes that Dr. Moriarty has to make with the string that you choose. Examples Input aaaaa aaa Output 0 Input abcabc bcd Output 1 Input abcdef klmnopq Output 7 Note In the first sample Moriarty can take any substring of length 3, and it will be equal to the required message u, so Moriarty won't have to make any changes. In the second sample you should take a substring consisting of characters from second to fourth ("bca") or from fifth to sixth ("bc"). Then you will only have to make one change: to change or to add the last character. In the third sample the initial string s doesn't contain any character that the message should contain, so, whatever string you choose, you will have to make at least 7 changes to obtain the required message. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Masha really loves algebra. On the last lesson, her strict teacher Dvastan gave she new exercise. You are given geometric progression b defined by two integers b_1 and q. Remind that a geometric progression is a sequence of integers b_1, b_2, b_3, ..., where for each i > 1 the respective term satisfies the condition b_{i} = b_{i} - 1·q, where q is called the common ratio of the progression. Progressions in Uzhlyandia are unusual: both b_1 and q can equal 0. Also, Dvastan gave Masha m "bad" integers a_1, a_2, ..., a_{m}, and an integer l. Masha writes all progression terms one by one onto the board (including repetitive) while condition |b_{i}| ≤ l is satisfied (|x| means absolute value of x). There is an exception: if a term equals one of the "bad" integers, Masha skips it (doesn't write onto the board) and moves forward to the next term. But the lesson is going to end soon, so Masha has to calculate how many integers will be written on the board. In order not to get into depression, Masha asked you for help: help her calculate how many numbers she will write, or print "inf" in case she needs to write infinitely many integers. -----Input----- The first line of input contains four integers b_1, q, l, m (-10^9 ≤ b_1, q ≤ 10^9, 1 ≤ l ≤ 10^9, 1 ≤ m ≤ 10^5) — the initial term and the common ratio of progression, absolute value of maximal number that can be written on the board and the number of "bad" integers, respectively. The second line contains m distinct integers a_1, a_2, ..., a_{m} (-10^9 ≤ a_{i} ≤ 10^9) — numbers that will never be written on the board. -----Output----- Print the only integer, meaning the number of progression terms that will be written on the board if it is finite, or "inf" (without quotes) otherwise. -----Examples----- Input 3 2 30 4 6 14 25 48 Output 3 Input 123 1 2143435 4 123 11 -5453 141245 Output 0 Input 123 1 2143435 4 54343 -13 6 124 Output inf -----Note----- In the first sample case, Masha will write integers 3, 12, 24. Progression term 6 will be skipped because it is a "bad" integer. Terms bigger than 24 won't be written because they exceed l by absolute value. In the second case, Masha won't write any number because all terms are equal 123 and this is a "bad" integer. In the third case, Masha will write infinitely integers 123. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Tanya is learning how to add numbers, but so far she is not doing it correctly. She is adding two numbers $a$ and $b$ using the following algorithm: If one of the numbers is shorter than the other, Tanya adds leading zeros so that the numbers are the same length. The numbers are processed from right to left (that is, from the least significant digits to the most significant). In the first step, she adds the last digit of $a$ to the last digit of $b$ and writes their sum in the answer. At each next step, she performs the same operation on each pair of digits in the same place and writes the result to the left side of the answer. For example, the numbers $a = 17236$ and $b = 3465$ Tanya adds up as follows: $$ \large{ \begin{array}{r} + \begin{array}{r} 17236\\ 03465\\ \end{array} \\ \hline \begin{array}{r} 1106911 \end{array} \end{array}} $$ calculates the sum of $6 + 5 = 11$ and writes $11$ in the answer. calculates the sum of $3 + 6 = 9$ and writes the result to the left side of the answer to get $911$. calculates the sum of $2 + 4 = 6$ and writes the result to the left side of the answer to get $6911$. calculates the sum of $7 + 3 = 10$, and writes the result to the left side of the answer to get $106911$. calculates the sum of $1 + 0 = 1$ and writes the result to the left side of the answer and get $1106911$. As a result, she gets $1106911$. You are given two positive integers $a$ and $s$. Find the number $b$ such that by adding $a$ and $b$ as described above, Tanya will get $s$. Or determine that no suitable $b$ exists. -----Input----- The first line of input data contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Each test case consists of a single line containing two positive integers $a$ and $s$ ($1 \le a \lt s \le 10^{18}$) separated by a space. -----Output----- For each test case print the answer on a separate line. If the solution exists, print a single positive integer $b$. The answer must be written without leading zeros. If multiple answers exist, print any of them. If no suitable number $b$ exists, output -1. -----Examples----- Input 6 17236 1106911 1 5 108 112 12345 1023412 1 11 1 20 Output 3465 4 -1 90007 10 -1 -----Note----- The first test case is explained in the main part of the statement. In the third test case, we cannot choose $b$ that satisfies the problem statement. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Your task is to determine how many files of the copy queue you will be able to save into your Hard Disk Drive. The files must be saved in the order they appear in the queue. ### Input: * Array of file sizes `(0 <= s <= 100)` * Capacity of the HD `(0 <= c <= 500)` ### Output: * Number of files that can be fully saved in the HD. ### Examples: ``` save([4,4,4,3,3], 12) -> 3 # 4+4+4 <= 12, but 4+4+4+3 > 12 ``` ``` save([4,4,4,3,3], 11) -> 2 # 4+4 <= 11, but 4+4+4 > 11 ``` Do not expect any negative or invalid inputs. Write your solution by modifying this code: ```python def save(sizes, hd): ``` Your solution should implemented in the function "save". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Given is an integer sequence of length N+1: A_0, A_1, A_2, \ldots, A_N. Is there a binary tree of depth N such that, for each d = 0, 1, \ldots, N, there are exactly A_d leaves at depth d? If such a tree exists, print the maximum possible number of vertices in such a tree; otherwise, print -1. Constraints * 0 \leq N \leq 10^5 * 0 \leq A_i \leq 10^{8} (0 \leq i \leq N) * A_N \geq 1 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_0 A_1 A_2 \cdots A_N Output Print the answer as an integer. Examples Input 3 0 1 1 2 Output 7 Input 4 0 0 1 0 2 Output 10 Input 2 0 3 1 Output -1 Input 1 1 1 Output -1 Input 10 0 0 1 1 2 3 5 8 13 21 34 Output 264 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. In mathematics, a matrix (plural matrices) is a rectangular array of numbers. Matrices have many applications in programming, from performing transformations in 2D space to machine learning. One of the most useful operations to perform on matrices is matrix multiplication, which takes a pair of matrices and produces another matrix – known as the dot product. Multiplying matrices is very different to multiplying real numbers, and follows its own set of rules. Unlike multiplying real numbers, multiplying matrices is non-commutative: in other words, multiplying matrix ```a``` by matrix ```b``` will not give the same result as multiplying matrix ```b``` by matrix ```a```. Additionally, not all pairs of matrix can be multiplied. For two matrices to be multipliable, the number of columns in matrix ```a``` must match the number of rows in matrix ```b```. There are many introductions to matrix multiplication online, including at Khan Academy, and in the classic MIT lecture series by Herbert Gross. To complete this kata, write a function that takes two matrices - ```a``` and ```b``` - and returns the dot product of those matrices. If the matrices cannot be multiplied, return ```-1``` for JS/Python, or `null` for Java. Each matrix will be represented by a two-dimensional list (a list of lists). Each inner list will contain one or more numbers, representing a row in the matrix. For example, the following matrix: ```|1 2|``````|3 4|``` Would be represented as: ```[[1, 2], [3, 4]]``` It can be assumed that all lists will be valid matrices, composed of lists with equal numbers of elements, and which contain only numbers. The numbers may include integers and/or decimal points. Write your solution by modifying this code: ```python def getMatrixProduct(a, b): ``` Your solution should implemented in the function "getMatrixProduct". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Vasya and Petya take part in a Codeforces round. The round lasts for two hours and contains five problems. For this round the dynamic problem scoring is used. If you were lucky not to participate in any Codeforces round with dynamic problem scoring, here is what it means. The maximum point value of the problem depends on the ratio of the number of participants who solved the problem to the total number of round participants. Everyone who made at least one submission is considered to be participating in the round. <image> Pay attention to the range bounds. For example, if 40 people are taking part in the round, and 10 of them solve a particular problem, then the solvers fraction is equal to 1 / 4, and the problem's maximum point value is equal to 1500. If the problem's maximum point value is equal to x, then for each whole minute passed from the beginning of the contest to the moment of the participant's correct submission, the participant loses x / 250 points. For example, if the problem's maximum point value is 2000, and the participant submits a correct solution to it 40 minutes into the round, this participant will be awarded with 2000·(1 - 40 / 250) = 1680 points for this problem. There are n participants in the round, including Vasya and Petya. For each participant and each problem, the number of minutes which passed between the beginning of the contest and the submission of this participant to this problem is known. It's also possible that this participant made no submissions to this problem. With two seconds until the end of the round, all participants' submissions have passed pretests, and not a single hack attempt has been made. Vasya believes that no more submissions or hack attempts will be made in the remaining two seconds, and every submission will pass the system testing. Unfortunately, Vasya is a cheater. He has registered 109 + 7 new accounts for the round. Now Vasya can submit any of his solutions from these new accounts in order to change the maximum point values of the problems. Vasya can also submit any wrong solutions to any problems. Note that Vasya can not submit correct solutions to the problems he hasn't solved. Vasya seeks to score strictly more points than Petya in the current round. Vasya has already prepared the scripts which allow to obfuscate his solutions and submit them into the system from any of the new accounts in just fractions of seconds. However, Vasya doesn't want to make his cheating too obvious, so he wants to achieve his goal while making submissions from the smallest possible number of new accounts. Find the smallest number of new accounts Vasya needs in order to beat Petya (provided that Vasya's assumptions are correct), or report that Vasya can't achieve his goal. Input The first line contains a single integer n (2 ≤ n ≤ 120) — the number of round participants, including Vasya and Petya. Each of the next n lines contains five integers ai, 1, ai, 2..., ai, 5 ( - 1 ≤ ai, j ≤ 119) — the number of minutes passed between the beginning of the round and the submission of problem j by participant i, or -1 if participant i hasn't solved problem j. It is guaranteed that each participant has made at least one successful submission. Vasya is listed as participant number 1, Petya is listed as participant number 2, all the other participants are listed in no particular order. Output Output a single integer — the number of new accounts Vasya needs to beat Petya, or -1 if Vasya can't achieve his goal. Examples Input 2 5 15 40 70 115 50 45 40 30 15 Output 2 Input 3 55 80 10 -1 -1 15 -1 79 60 -1 42 -1 13 -1 -1 Output 3 Input 5 119 119 119 119 119 0 0 0 0 -1 20 65 12 73 77 78 112 22 23 11 1 78 60 111 62 Output 27 Input 4 -1 20 40 77 119 30 10 73 50 107 21 29 -1 64 98 117 65 -1 -1 -1 Output -1 Note In the first example, Vasya's optimal strategy is to submit the solutions to the last three problems from two new accounts. In this case the first two problems will have the maximum point value of 1000, while the last three problems will have the maximum point value of 500. Vasya's score will be equal to 980 + 940 + 420 + 360 + 270 = 2970 points, while Petya will score just 800 + 820 + 420 + 440 + 470 = 2950 points. In the second example, Vasya has to make a single unsuccessful submission to any problem from two new accounts, and a single successful submission to the first problem from the third new account. In this case, the maximum point values of the problems will be equal to 500, 1500, 1000, 1500, 3000. Vasya will score 2370 points, while Petya will score just 2294 points. In the third example, Vasya can achieve his goal by submitting the solutions to the first four problems from 27 new accounts. The maximum point values of the problems will be equal to 500, 500, 500, 500, 2000. Thanks to the high cost of the fifth problem, Vasya will manage to beat Petya who solved the first four problems very quickly, but couldn't solve the fifth one. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are an experienced Codeforces user. Today you found out that during your activity on Codeforces you have made y submissions, out of which x have been successful. Thus, your current success rate on Codeforces is equal to x / y. Your favorite rational number in the [0;1] range is p / q. Now you wonder: what is the smallest number of submissions you have to make if you want your success rate to be p / q? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. Each of the next t lines contains four integers x, y, p and q (0 ≤ x ≤ y ≤ 109; 0 ≤ p ≤ q ≤ 109; y > 0; q > 0). It is guaranteed that p / q is an irreducible fraction. Hacks. For hacks, an additional constraint of t ≤ 5 must be met. Output For each test case, output a single integer equal to the smallest number of submissions you have to make if you want your success rate to be equal to your favorite rational number, or -1 if this is impossible to achieve. Example Input 4 3 10 1 2 7 14 3 8 20 70 2 7 5 6 1 1 Output 4 10 0 -1 Note In the first example, you have to make 4 successful submissions. Your success rate will be equal to 7 / 14, or 1 / 2. In the second example, you have to make 2 successful and 8 unsuccessful submissions. Your success rate will be equal to 9 / 24, or 3 / 8. In the third example, there is no need to make any new submissions. Your success rate is already equal to 20 / 70, or 2 / 7. In the fourth example, the only unsuccessful submission breaks your hopes of having the success rate equal to 1. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You might know some pretty large perfect squares. But what about the NEXT one? Complete the `findNextSquare` method that finds the next integral perfect square after the one passed as a parameter. Recall that an integral perfect square is an integer n such that sqrt(n) is also an integer. If the parameter is itself not a perfect square then `-1` should be returned. You may assume the parameter is positive. **Examples:** ``` findNextSquare(121) --> returns 144 findNextSquare(625) --> returns 676 findNextSquare(114) --> returns -1 since 114 is not a perfect ``` Write your solution by modifying this code: ```python def find_next_square(sq): ``` Your solution should implemented in the function "find_next_square". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Tsuruga Castle, a symbol of Aizuwakamatsu City, was named "Tsuruga Castle" after Gamo Ujisato built a full-scale castle tower. You can overlook the Aizu basin from the castle tower. On a clear day, you can see Tsuruga Castle from the summit of Mt. Iimori, which is famous for Byakkotai. <image> We decided to conduct a dating survey of visitors to Tsuruga Castle to use as a reference for future public relations activities in Aizuwakamatsu City. Please create a program that inputs the age of visitors and outputs the number of people by age group below. Category | Age --- | --- Under 10 years old | 0 ~ 9 Teens | 10 ~ 19 20s | 20 ~ 29 30s | 30 ~ 39 40s | 40 ~ 49 50s | 50 ~ 59 Over 60 years old | 60 ~ Input A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format: n a1 a2 :: an The first line gives the number of visitors n (1 ≤ n ≤ 1000000), and the following n lines give the age of the i-th visitor ai (0 ≤ ai ≤ 120). Output The number of people is output in the following format for each data set. Line 1: Number of people under 10 Line 2: Number of teens Line 3: Number of people in their 20s Line 4: Number of people in their 30s Line 5: Number of people in their 40s Line 6: Number of people in their 50s Line 7: Number of people over 60 Example Input 8 71 34 65 11 41 39 6 5 4 67 81 78 65 0 Output 2 1 0 2 1 0 2 0 0 0 0 0 0 4 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Recently, Tokitsukaze found an interesting game. Tokitsukaze had n items at the beginning of this game. However, she thought there were too many items, so now she wants to discard m (1 ≤ m ≤ n) special items of them. These n items are marked with indices from 1 to n. In the beginning, the item with index i is placed on the i-th position. Items are divided into several pages orderly, such that each page contains exactly k positions and the last positions on the last page may be left empty. Tokitsukaze would do the following operation: focus on the first special page that contains at least one special item, and at one time, Tokitsukaze would discard all special items on this page. After an item is discarded or moved, its old position would be empty, and then the item below it, if exists, would move up to this empty position. The movement may bring many items forward and even into previous pages, so Tokitsukaze would keep waiting until all the items stop moving, and then do the operation (i.e. check the special page and discard the special items) repeatedly until there is no item need to be discarded. <image> Consider the first example from the statement: n=10, m=4, k=5, p=[3, 5, 7, 10]. The are two pages. Initially, the first page is special (since it is the first page containing a special item). So Tokitsukaze discards the special items with indices 3 and 5. After, the first page remains to be special. It contains [1, 2, 4, 6, 7], Tokitsukaze discards the special item with index 7. After, the second page is special (since it is the first page containing a special item). It contains [9, 10], Tokitsukaze discards the special item with index 10. Tokitsukaze wants to know the number of operations she would do in total. Input The first line contains three integers n, m and k (1 ≤ n ≤ 10^{18}, 1 ≤ m ≤ 10^5, 1 ≤ m, k ≤ n) — the number of items, the number of special items to be discarded and the number of positions in each page. The second line contains m distinct integers p_1, p_2, …, p_m (1 ≤ p_1 < p_2 < … < p_m ≤ n) — the indices of special items which should be discarded. Output Print a single integer — the number of operations that Tokitsukaze would do in total. Examples Input 10 4 5 3 5 7 10 Output 3 Input 13 4 5 7 8 9 10 Output 1 Note For the first example: * In the first operation, Tokitsukaze would focus on the first page [1, 2, 3, 4, 5] and discard items with indices 3 and 5; * In the second operation, Tokitsukaze would focus on the first page [1, 2, 4, 6, 7] and discard item with index 7; * In the third operation, Tokitsukaze would focus on the second page [9, 10] and discard item with index 10. For the second example, Tokitsukaze would focus on the second page [6, 7, 8, 9, 10] and discard all special items at once. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Omkar's most recent follower, Ajit, has entered the Holy Forest. Ajit realizes that Omkar's forest is an n by m grid (1 ≤ n, m ≤ 2000) of some non-negative integers. Since the forest is blessed by Omkar, it satisfies some special conditions: 1. For any two adjacent (sharing a side) cells, the absolute value of the difference of numbers in them is at most 1. 2. If the number in some cell is strictly larger than 0, it should be strictly greater than the number in at least one of the cells adjacent to it. Unfortunately, Ajit is not fully worthy of Omkar's powers yet. He sees each cell as a "0" or a "#". If a cell is labeled as "0", then the number in it must equal 0. Otherwise, the number in it can be any nonnegative integer. Determine how many different assignments of elements exist such that these special conditions are satisfied. Two assignments are considered different if there exists at least one cell such that the numbers written in it in these assignments are different. Since the answer may be enormous, find the answer modulo 10^9+7. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 100). Description of the test cases follows. The first line of each test case contains two integers n and m (1 ≤ n, m ≤ 2000, nm ≥ 2) – the dimensions of the forest. n lines follow, each consisting of one string of m characters. Each of these characters is either a "0" or a "#". It is guaranteed that the sum of n over all test cases does not exceed 2000 and the sum of m over all test cases does not exceed 2000. Output For each test case, print one integer: the number of valid configurations modulo 10^9+7. Example Input 4 3 4 0000 00#0 0000 2 1 # # 1 2 ## 6 29 ############################# #000##0###0##0#0####0####000# #0#0##00#00##00####0#0###0#0# #0#0##0#0#0##00###00000##00## #000##0###0##0#0##0###0##0#0# ############################# Output 2 3 3 319908071 Note For the first test case, the two valid assignments are 0000\\\ 0000\\\ 0000 and 0000\\\ 0010\\\ 0000 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Monocarp plays "Rage of Empires II: Definitive Edition" — a strategic computer game. Right now he's planning to attack his opponent in the game, but Monocarp's forces cannot enter the opponent's territory since the opponent has built a wall. The wall consists of $n$ sections, aligned in a row. The $i$-th section initially has durability $a_i$. If durability of some section becomes $0$ or less, this section is considered broken. To attack the opponent, Monocarp needs to break at least two sections of the wall (any two sections: possibly adjacent, possibly not). To do this, he plans to use an onager — a special siege weapon. The onager can be used to shoot any section of the wall; the shot deals $2$ damage to the target section and $1$ damage to adjacent sections. In other words, if the onager shoots at the section $x$, then the durability of the section $x$ decreases by $2$, and the durability of the sections $x - 1$ and $x + 1$ (if they exist) decreases by $1$ each. Monocarp can shoot at any sections any number of times, he can even shoot at broken sections. Monocarp wants to calculate the minimum number of onager shots needed to break at least two sections. Help him! -----Input----- The first line contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of sections. The second line contains the sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^6$), where $a_i$ is the initial durability of the $i$-th section. -----Output----- Print one integer — the minimum number of onager shots needed to break at least two sections of the wall. -----Examples----- Input 5 20 10 30 10 20 Output 10 Input 3 1 8 1 Output 1 Input 6 7 6 6 8 5 8 Output 4 Input 6 14 3 8 10 15 4 Output 4 Input 4 1 100 100 1 Output 2 Input 3 40 10 10 Output 7 -----Note----- In the first example, it is possible to break the $2$-nd and the $4$-th section in $10$ shots, for example, by shooting the third section $10$ times. After that, the durabilities become $[20, 0, 10, 0, 20]$. Another way of doing it is firing $5$ shots at the $2$-nd section, and another $5$ shots at the $4$-th section. After that, the durabilities become $[15, 0, 20, 0, 15]$. In the second example, it is enough to shoot the $2$-nd section once. Then the $1$-st and the $3$-rd section will be broken. In the third example, it is enough to shoot the $2$-nd section twice (then the durabilities become $[5, 2, 4, 8, 5, 8]$), and then shoot the $3$-rd section twice (then the durabilities become $[5, 0, 0, 6, 5, 8]$). So, four shots are enough to break the $2$-nd and the $3$-rd section. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. There is a right triangle with legs of length a and b. Your task is to determine whether it is possible to locate the triangle on the plane in such a way that none of its sides is parallel to the coordinate axes. All the vertices must have integer coordinates. If there exists such a location, you have to output the appropriate coordinates of vertices. Input The first line contains two integers a, b (1 ≤ a, b ≤ 1000), separated by a single space. Output In the first line print either "YES" or "NO" (without the quotes) depending on whether the required location exists. If it does, print in the next three lines three pairs of integers — the coordinates of the triangle vertices, one pair per line. The coordinates must be integers, not exceeding 109 in their absolute value. Examples Input 1 1 Output NO Input 5 5 Output YES 2 1 5 5 -2 4 Input 5 10 Output YES -10 4 -2 -2 1 2 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Polycarp found on the street an array $a$ of $n$ elements. Polycarp invented his criterion for the beauty of an array. He calls an array $a$ beautiful if at least one of the following conditions must be met for each different pair of indices $i \ne j$: $a_i$ is divisible by $a_j$; or $a_j$ is divisible by $a_i$. For example, if: $n=5$ and $a=[7, 9, 3, 14, 63]$, then the $a$ array is not beautiful (for $i=4$ and $j=2$, none of the conditions above is met); $n=3$ and $a=[2, 14, 42]$, then the $a$ array is beautiful; $n=4$ and $a=[45, 9, 3, 18]$, then the $a$ array is not beautiful (for $i=1$ and $j=4$ none of the conditions above is met); Ugly arrays upset Polycarp, so he wants to remove some elements from the array $a$ so that it becomes beautiful. Help Polycarp determine the smallest number of elements to remove to make the array $a$ beautiful. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ numbers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$) — elements of the array $a$. -----Output----- For each test case output one integer — the minimum number of elements that must be removed to make the array $a$ beautiful. -----Examples----- Input 4 5 7 9 3 14 63 3 2 14 42 4 45 9 3 18 3 2 2 8 Output 2 0 1 0 -----Note----- In the first test case, removing $7$ and $14$ will make array $a$ beautiful. In the second test case, the array $a$ is already beautiful. In the third test case, removing one of the elements $45$ or $18$ will make the array $a$ beautiful. In the fourth test case, the array $a$ is beautiful. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Petya loves football very much. One day, as he was watching a football match, he was writing the players' current positions on a piece of paper. To simplify the situation he depicted it as a string consisting of zeroes and ones. A zero corresponds to players of one team; a one corresponds to players of another team. If there are at least 7 players of some team standing one after another, then the situation is considered dangerous. For example, the situation 00100110111111101 is dangerous and 11110111011101 is not. You are given the current situation. Determine whether it is dangerous or not. Input The first input line contains a non-empty string consisting of characters "0" and "1", which represents players. The length of the string does not exceed 100 characters. There's at least one player from each team present on the field. Output Print "YES" if the situation is dangerous. Otherwise, print "NO". Examples Input 001001 Output NO Input 1000000001 Output YES Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. # Task Given string `s`, which contains only letters from `a to z` in lowercase. A set of alphabet is given by `abcdefghijklmnopqrstuvwxyz`. 2 sets of alphabets mean 2 or more alphabets. Your task is to find the missing letter(s). You may need to output them by the order a-z. It is possible that there is more than one missing letter from more than one set of alphabet. If the string contains all of the letters in the alphabet, return an empty string `""` # Example For `s='abcdefghijklmnopqrstuvwxy'` The result should be `'z'` For `s='aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyy'` The result should be `'zz'` For `s='abbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxy'` The result should be `'ayzz'` For `s='codewars'` The result should be `'bfghijklmnpqtuvxyz'` # Input/Output - `[input]` string `s` Given string(s) contains one or more set of alphabets in lowercase. - `[output]` a string Find the letters contained in each alphabet but not in the string(s). Output them by the order `a-z`. If missing alphabet is repeated, please repeat them like `"bbccdd"`, not `"bcdbcd"` Write your solution by modifying this code: ```python def missing_alphabets(s): ``` Your solution should implemented in the function "missing_alphabets". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ — a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) — the number of test cases. Next $T$ lines contain test cases — one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) — the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Ashish has $n$ elements arranged in a line. These elements are represented by two integers $a_i$ — the value of the element and $b_i$ — the type of the element (there are only two possible types: $0$ and $1$). He wants to sort the elements in non-decreasing values of $a_i$. He can perform the following operation any number of times: Select any two elements $i$ and $j$ such that $b_i \ne b_j$ and swap them. That is, he can only swap two elements of different types in one move. Tell him if he can sort the elements in non-decreasing values of $a_i$ after performing any number of operations. -----Input----- The first line contains one integer $t$ $(1 \le t \le 100)$ — the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line contains $n$ integers $a_i$ $(1 \le a_i \le 10^5)$  — the value of the $i$-th element. The third line containts $n$ integers $b_i$ $(b_i \in \{0, 1\})$  — the type of the $i$-th element. -----Output----- For each test case, print "Yes" or "No" (without quotes) depending on whether it is possible to sort elements in non-decreasing order of their value. You may print each letter in any case (upper or lower). -----Example----- Input 5 4 10 20 20 30 0 1 0 1 3 3 1 2 0 1 1 4 2 2 4 8 1 1 1 1 3 5 15 4 0 0 0 4 20 10 100 50 1 0 0 1 Output Yes Yes Yes No Yes -----Note----- For the first case: The elements are already in sorted order. For the second case: Ashish may first swap elements at positions $1$ and $2$, then swap elements at positions $2$ and $3$. For the third case: The elements are already in sorted order. For the fourth case: No swap operations may be performed as there is no pair of elements $i$ and $j$ such that $b_i \ne b_j$. The elements cannot be sorted. For the fifth case: Ashish may swap elements at positions $3$ and $4$, then elements at positions $1$ and $2$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$) — the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$) — the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$) — the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$) — the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$) — the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Congratulations! That Special Someone has given you their phone number. But WAIT, is it a valid number? Your task is to write a function that verifies whether a given string contains a valid British mobile (cell) phone number or not. If valid, return 'In with a chance'. If invalid, or if you're given an empty string, return 'Plenty more fish in the sea'. A number can be valid in the following ways: Here in the UK mobile numbers begin with '07' followed by 9 other digits, e.g. '07454876120'. Sometimes the number is preceded by the country code, the prefix '+44', which replaces the '0' in ‘07’, e.g. '+447454876120'. And sometimes you will find numbers with dashes in-between digits or on either side, e.g. '+44--745---487-6120' or '-074-54-87-61-20-'. As you can see, dashes may be consecutive. Good Luck Romeo/Juliette! Write your solution by modifying this code: ```python def validate_number(s): ``` Your solution should implemented in the function "validate_number". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018. Input The first line of the input contains integer n (1 ≤ n ≤ 1000). Output Output the smallest positive integer with exactly n divisors. Examples Input 4 Output 6 Input 6 Output 12 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. **DESCRIPTION:** Your strict math teacher is teaching you about right triangles, and the Pythagorean Theorem --> a^2 + b^2 = c^2 whereas a and b are the legs of the right triangle and c is the hypotenuse of the right triangle. On the test however, the question asks: What are the possible integer lengths for the other side of the triangle, but since you never learned anything about that in class, you realize she meant What are the possible integer lengths for the other side of the right triangle. Because you want to address the fact that she asked the wrong question and the fact that you're smart at math, you've decided to answer all the possible values for the third side EXCLUDING the possibilities for a right triangle in increasing order. **EXAMPLES:** ``` side_len(1, 1) --> [1] side_len(3, 4) --> [2, 3, 4, 6] side_len(4, 6) --> [3, 4, 5, 6, 7, 8, 9] ``` **RETURN:** Return your answer as a list of all the possible third side lengths of the triangle without the right triangles in increasing order. By the way, after finishing this kata, please try some of my other katas: [Here](https://www.codewars.com/collections/tonylicodings-authored-katas) NOTE: When given side_len(x, y), y will always be greater than or equal to x. Also, if a right triangle's legs are passed in, exclude the hypotenuse. If a right triangle's leg and hypotenuse are passed in, exclude the other leg. Write your solution by modifying this code: ```python def side_len(x, y): ``` Your solution should implemented in the function "side_len". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. In this Kata you are a builder and you are assigned a job of building a wall with a specific size (God knows why...). Create a function called `build_a_wall` (or `buildAWall` in JavaScript) that takes `x` and `y` as integer arguments (which represent the number of rows of bricks for the wall and the number of bricks in each row respectively) and outputs the wall as a string with the following symbols: `■■` => One full brick (2 of `■`) `■` => Half a brick `|` => Mortar (between bricks on same row) There has to be a `|` between every two bricks on the same row. For more stability each row's bricks cannot be aligned vertically with the bricks of the row above it or below it meaning at every 2 rows you will have to make the furthest brick on both sides of the row at the size of half a brick (`■`) while the first row from the bottom will only consist of full bricks. Starting from the top, every new row is to be represented with `\n` except from the bottom row. See the examples for a better understanding. If one or both of the arguments aren't valid (less than 1, isn't integer, isn't given...etc) return `None` in Python. If the number of bricks required to build the wall is greater than 10000 return `"Naah, too much...here's my resignation."` Examples, based on Python: ``` build_a_wall(5,5) => '■■|■■|■■|■■|■■\n■|■■|■■|■■|■■|■\n■■|■■|■■|■■|■■\n■|■■|■■|■■|■■|■\n■■|■■|■■|■■|■■' build_a_wall(10,7) => '■|■■|■■|■■|■■|■■|■■|■\n■■|■■|■■|■■|■■|■■|■■\n■|■■|■■|■■|■■|■■|■■|■\n■■|■■|■■|■■|■■|■■|■■\n■|■■|■■|■■|■■|■■|■■|■\n■■|■■|■■|■■|■■|■■|■■\n■|■■|■■|■■|■■|■■|■■|■\n■■|■■|■■|■■|■■|■■|■■\n■|■■|■■|■■|■■|■■|■■|■\n■■|■■|■■|■■|■■|■■|■■' build_a_wall("eight",[3]) => None } }> Invalid input build_a_wall(12,-4) => None } build_a_wall(123,987) => "Naah, too much...here's my resignation." 123 * 987 = 121401 > 10000 ``` Write your solution by modifying this code: ```python def build_a_wall(x=0, y=0): ``` Your solution should implemented in the function "build_a_wall". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. # Task: We define the "self reversed power sequence" as one shown below: Implement a function that takes 2 arguments (`ord max` and `num dig`), and finds the smallest term of the sequence whose index is less than or equal to `ord max`, and has exactly `num dig` number of digits. If there is a number with correct amount of digits, the result should be an array in the form: ```python [True, smallest found term] [False, -1] ``` ## Input range: ```python ord_max <= 1000 ``` ___ ## Examples: ```python min_length_num(5, 10) == [True, 10] # 10th term has 5 digits min_length_num(7, 11) == [False, -1] # no terms before the 13th one have 7 digits min_length_num(7, 14) == [True, 13] # 13th term is the first one which has 7 digits ``` Which you can see in the table below: ``` n-th Term Term Value 1 0 2 1 3 3 4 8 5 22 6 65 7 209 8 732 9 2780 10 11377 11 49863 12 232768 13 1151914 14 6018785 ``` ___ Enjoy it and happy coding!! Write your solution by modifying this code: ```python def min_length_num(num_dig, ord_max): ``` Your solution should implemented in the function "min_length_num". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Athenaeus has just finished creating his latest musical composition and will present it tomorrow to the people of Athens. Unfortunately, the melody is rather dull and highly likely won't be met with a warm reception. His song consists of $n$ notes, which we will treat as positive integers. The diversity of a song is the number of different notes it contains. As a patron of music, Euterpe watches over composers and guides them throughout the process of creating new melodies. She decided to help Athenaeus by changing his song to make it more diverse. Being a minor goddess, she cannot arbitrarily change the song. Instead, for each of the $n$ notes in the song, she can either leave it as it is or increase it by $1$. Given the song as a sequence of integers describing the notes, find out the maximal, achievable diversity. -----Input----- The input consists of multiple test cases. The first line contains an integer $t$ ($1 \leq t \leq 10000$) — the number of test cases. Then $t$ test cases follow, each one is described in two lines. In the first line of each test case there is a single integer $n$ ($1 \leq n \leq 10^5$) denoting the length of the song. The next line contains a sequence of $n$ integers $x_1, x_2, \ldots, x_n$ $(1 \leq x_1 \leq x_2 \leq \ldots \leq x_n \leq 2 \cdot n)$, describing the song. The sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case, you should output a single line containing precisely one integer, the maximal diversity of the song, i.e. the maximal possible number of different elements in the final sequence. -----Examples----- Input 5 6 1 2 2 2 5 6 2 4 4 6 1 1 3 4 4 5 1 1 6 1 1 1 2 2 2 Output 5 2 6 1 3 -----Note----- In the first test case, Euterpe can increase the second, fifth and sixth element to obtain the sequence $1, \underline{3}, 2, 2, \underline{6}, \underline{7}$, which has $5$ different elements (increased elements are underlined). In the second test case, Euterpe can increase the first element to obtain the sequence $\underline{5}, 4$, which has $2$ different elements. In the third test case, Euterpe can increase the second, fifth and sixth element to obtain the sequence $1, \underline{2}, 3, 4, \underline{5}, \underline{6}$, which has $6$ different elements. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You're given an ancient book that unfortunately has a few pages in the wrong position, fortunately your computer has a list of every page number in order from ``1`` to ``n``. You're supplied with an array of numbers, and should return an array with each page number that is out of place. Incorrect page numbers will not appear next to each other. Duplicate incorrect page numbers are possible. Example: ```Given: list = [1,2,10,3,4,5,8,6,7] ``` ``Return: [10,8] `` Your returning list should have the incorrect page numbers in the order they were found. Write your solution by modifying this code: ```python def find_page_number(pages): ``` Your solution should implemented in the function "find_page_number". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. A magic island Geraldion, where Gerald lives, has its own currency system. It uses banknotes of several values. But the problem is, the system is not perfect and sometimes it happens that Geraldionians cannot express a certain sum of money with any set of banknotes. Of course, they can use any number of banknotes of each value. Such sum is called unfortunate. Gerald wondered: what is the minimum unfortunate sum? -----Input----- The first line contains number n (1 ≤ n ≤ 1000) — the number of values of the banknotes that used in Geraldion. The second line contains n distinct space-separated numbers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^6) — the values of the banknotes. -----Output----- Print a single line — the minimum unfortunate sum. If there are no unfortunate sums, print - 1. -----Examples----- Input 5 1 2 3 4 5 Output -1 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You can find anything whatsoever in our Galaxy! A cubical planet goes round an icosahedral star. Let us introduce a system of axes so that the edges of the cubical planet are parallel to the coordinate axes and two opposite vertices lay in the points (0, 0, 0) and (1, 1, 1). Two flies live on the planet. At the moment they are sitting on two different vertices of the cubical planet. Your task is to determine whether they see each other or not. The flies see each other when the vertices they occupy lie on the same face of the cube. Input The first line contains three space-separated integers (0 or 1) — the coordinates of the first fly, the second line analogously contains the coordinates of the second fly. Output Output "YES" (without quotes) if the flies see each other. Otherwise, output "NO". Examples Input 0 0 0 0 1 0 Output YES Input 1 1 0 0 1 0 Output YES Input 0 0 0 1 1 1 Output NO Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. For the given integer $n$ ($n > 2$) let's write down all the strings of length $n$ which contain $n-2$ letters 'a' and two letters 'b' in lexicographical (alphabetical) order. Recall that the string $s$ of length $n$ is lexicographically less than string $t$ of length $n$, if there exists such $i$ ($1 \le i \le n$), that $s_i < t_i$, and for any $j$ ($1 \le j < i$) $s_j = t_j$. The lexicographic comparison of strings is implemented by the operator < in modern programming languages. For example, if $n=5$ the strings are (the order does matter): aaabb aabab aabba abaab ababa abbaa baaab baaba babaa bbaaa It is easy to show that such a list of strings will contain exactly $\frac{n \cdot (n-1)}{2}$ strings. You are given $n$ ($n > 2$) and $k$ ($1 \le k \le \frac{n \cdot (n-1)}{2}$). Print the $k$-th string from the list. -----Input----- The input contains one or more test cases. The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test. Then $t$ test cases follow. Each test case is written on the the separate line containing two integers $n$ and $k$ ($3 \le n \le 10^5, 1 \le k \le \min(2\cdot10^9, \frac{n \cdot (n-1)}{2})$. The sum of values $n$ over all test cases in the test doesn't exceed $10^5$. -----Output----- For each test case print the $k$-th string from the list of all described above strings of length $n$. Strings in the list are sorted lexicographically (alphabetically). -----Example----- Input 7 5 1 5 2 5 8 5 10 3 1 3 2 20 100 Output aaabb aabab baaba bbaaa abb bab aaaaabaaaaabaaaaaaaa Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Petya started to attend programming lessons. On the first lesson his task was to write a simple program. The program was supposed to do the following: in the given string, consisting if uppercase and lowercase Latin letters, it: * deletes all the vowels, * inserts a character "." before each consonant, * replaces all uppercase consonants with corresponding lowercase ones. Vowels are letters "A", "O", "Y", "E", "U", "I", and the rest are consonants. The program's input is exactly one string, it should return the output as a single string, resulting after the program's processing the initial string. Help Petya cope with this easy task. Input The first line represents input string of Petya's program. This string only consists of uppercase and lowercase Latin letters and its length is from 1 to 100, inclusive. Output Print the resulting string. It is guaranteed that this string is not empty. Examples Input tour Output .t.r Input Codeforces Output .c.d.f.r.c.s Input aBAcAba Output .b.c.b Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Soon a school Olympiad in Informatics will be held in Berland, n schoolchildren will participate there. At a meeting of the jury of the Olympiad it was decided that each of the n participants, depending on the results, will get a diploma of the first, second or third degree. Thus, each student will receive exactly one diploma. They also decided that there must be given at least min_1 and at most max_1 diplomas of the first degree, at least min_2 and at most max_2 diplomas of the second degree, and at least min_3 and at most max_3 diplomas of the third degree. After some discussion it was decided to choose from all the options of distributing diplomas satisfying these limitations the one that maximizes the number of participants who receive diplomas of the first degree. Of all these options they select the one which maximizes the number of the participants who receive diplomas of the second degree. If there are multiple of these options, they select the option that maximizes the number of diplomas of the third degree. Choosing the best option of distributing certificates was entrusted to Ilya, one of the best programmers of Berland. However, he found more important things to do, so it is your task now to choose the best option of distributing of diplomas, based on the described limitations. It is guaranteed that the described limitations are such that there is a way to choose such an option of distributing diplomas that all n participants of the Olympiad will receive a diploma of some degree. -----Input----- The first line of the input contains a single integer n (3 ≤ n ≤ 3·10^6) — the number of schoolchildren who will participate in the Olympiad. The next line of the input contains two integers min_1 and max_1 (1 ≤ min_1 ≤ max_1 ≤ 10^6) — the minimum and maximum limits on the number of diplomas of the first degree that can be distributed. The third line of the input contains two integers min_2 and max_2 (1 ≤ min_2 ≤ max_2 ≤ 10^6) — the minimum and maximum limits on the number of diplomas of the second degree that can be distributed. The next line of the input contains two integers min_3 and max_3 (1 ≤ min_3 ≤ max_3 ≤ 10^6) — the minimum and maximum limits on the number of diplomas of the third degree that can be distributed. It is guaranteed that min_1 + min_2 + min_3 ≤ n ≤ max_1 + max_2 + max_3. -----Output----- In the first line of the output print three numbers, showing how many diplomas of the first, second and third degree will be given to students in the optimal variant of distributing diplomas. The optimal variant of distributing diplomas is the one that maximizes the number of students who receive diplomas of the first degree. Of all the suitable options, the best one is the one which maximizes the number of participants who receive diplomas of the second degree. If there are several of these options, the best one is the one that maximizes the number of diplomas of the third degree. -----Examples----- Input 6 1 5 2 6 3 7 Output 1 2 3 Input 10 1 2 1 3 1 5 Output 2 3 5 Input 6 1 3 2 2 2 2 Output 2 2 2 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. "We've tried solitary confinement, waterboarding and listening to Just In Beaver, to no avail. We need something extreme." "Little Alena got an array as a birthday present..." The array b of length n is obtained from the array a of length n and two integers l and r (l ≤ r) using the following procedure: b_1 = b_2 = b_3 = b_4 = 0. For all 5 ≤ i ≤ n: b_{i} = 0 if a_{i}, a_{i} - 1, a_{i} - 2, a_{i} - 3, a_{i} - 4 > r and b_{i} - 1 = b_{i} - 2 = b_{i} - 3 = b_{i} - 4 = 1 b_{i} = 1 if a_{i}, a_{i} - 1, a_{i} - 2, a_{i} - 3, a_{i} - 4 < l and b_{i} - 1 = b_{i} - 2 = b_{i} - 3 = b_{i} - 4 = 0 b_{i} = b_{i} - 1 otherwise You are given arrays a and b' of the same length. Find two integers l and r (l ≤ r), such that applying the algorithm described above will yield an array b equal to b'. It's guaranteed that the answer exists. -----Input----- The first line of input contains a single integer n (5 ≤ n ≤ 10^5) — the length of a and b'. The second line of input contains n space separated integers a_1, ..., a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9) — the elements of a. The third line of input contains a string of n characters, consisting of 0 and 1 — the elements of b'. Note that they are not separated by spaces. -----Output----- Output two integers l and r ( - 10^9 ≤ l ≤ r ≤ 10^9), conforming to the requirements described above. If there are multiple solutions, output any of them. It's guaranteed that the answer exists. -----Examples----- Input 5 1 2 3 4 5 00001 Output 6 15 Input 10 -10 -9 -8 -7 -6 6 7 8 9 10 0000111110 Output -5 5 -----Note----- In the first test case any pair of l and r pair is valid, if 6 ≤ l ≤ r ≤ 10^9, in that case b_5 = 1, because a_1, ..., a_5 < l. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given a tube which is reflective inside represented as two non-coinciding, but parallel to $Ox$ lines. Each line has some special integer points — positions of sensors on sides of the tube. You are going to emit a laser ray in the tube. To do so, you have to choose two integer points $A$ and $B$ on the first and the second line respectively (coordinates can be negative): the point $A$ is responsible for the position of the laser, and the point $B$ — for the direction of the laser ray. The laser ray is a ray starting at $A$ and directed at $B$ which will reflect from the sides of the tube (it doesn't matter if there are any sensors at a reflection point or not). A sensor will only register the ray if the ray hits exactly at the position of the sensor. [Image] Examples of laser rays. Note that image contains two examples. The $3$ sensors (denoted by black bold points on the tube sides) will register the blue ray but only $2$ will register the red. Calculate the maximum number of sensors which can register your ray if you choose points $A$ and $B$ on the first and the second lines respectively. -----Input----- The first line contains two integers $n$ and $y_1$ ($1 \le n \le 10^5$, $0 \le y_1 \le 10^9$) — number of sensors on the first line and its $y$ coordinate. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$) — $x$ coordinates of the sensors on the first line in the ascending order. The third line contains two integers $m$ and $y_2$ ($1 \le m \le 10^5$, $y_1 < y_2 \le 10^9$) — number of sensors on the second line and its $y$ coordinate. The fourth line contains $m$ integers $b_1, b_2, \ldots, b_m$ ($0 \le b_i \le 10^9$) — $x$ coordinates of the sensors on the second line in the ascending order. -----Output----- Print the only integer — the maximum number of sensors which can register the ray. -----Example----- Input 3 1 1 5 6 1 3 3 Output 3 -----Note----- One of the solutions illustrated on the image by pair $A_2$ and $B_2$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. An n × n table a is defined as follows: The first row and the first column contain ones, that is: a_{i}, 1 = a_{1, }i = 1 for all i = 1, 2, ..., n. Each of the remaining numbers in the table is equal to the sum of the number above it and the number to the left of it. In other words, the remaining elements are defined by the formula a_{i}, j = a_{i} - 1, j + a_{i}, j - 1. These conditions define all the values in the table. You are given a number n. You need to determine the maximum value in the n × n table defined by the rules above. -----Input----- The only line of input contains a positive integer n (1 ≤ n ≤ 10) — the number of rows and columns of the table. -----Output----- Print a single line containing a positive integer m — the maximum value in the table. -----Examples----- Input 1 Output 1 Input 5 Output 70 -----Note----- In the second test the rows of the table look as follows: {1, 1, 1, 1, 1}, {1, 2, 3, 4, 5}, {1, 3, 6, 10, 15}, {1, 4, 10, 20, 35}, {1, 5, 15, 35, 70}. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively. He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$. Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$. Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible. Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal. -----Input----- The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ — the length of the first array. The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ — elements of the array $A$. The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ — the length of the second array. The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$. -----Output----- Print a single integer — the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal. If there is no way to make array equal, print "-1". -----Examples----- Input 5 11 2 3 5 7 4 11 7 3 7 Output 3 Input 2 1 2 1 100 Output -1 Input 3 1 2 3 3 1 2 3 Output 3 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
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. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Write a program which reads a sequence and prints it in the reverse order. Note 解説 Constraints * n ≤ 100 * 0 ≤ ai < 1000 Input The input is given in the following format: n a1 a2 . . . an n is the size of the sequence and ai is the ith element of the sequence. Output Print the reversed sequence in a line. Print a single space character between adjacent elements (Note that your program should not put a space character after the last element). Examples Input 5 1 2 3 4 5 Output 5 4 3 2 1 Input 8 3 3 4 4 5 8 7 9 Output 9 7 8 5 4 4 3 3 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese , Russian and Vietnamese as well. Triangle classification is an important problem in modern mathematics. Mathematicians have developed many criteria according to which a triangle can be classified. In this problem, you will be asked to classify some triangles according to their sides and angles. According to their measure, angles may be: Acute — an angle that is less than 90 degrees Right — a 90-degrees angle Obtuse — an angle that is greater than 90 degrees According to their sides, triangles may be: Scalene — all sides are different Isosceles — exactly two sides are equal According to their angles, triangles may be: Acute — all angles are acute Right — one angle is right Obtuse — one angle is obtuse Triangles with three equal sides (equilateral triangles) will not appear in the test data. The triangles formed by three collinear points are not considered in this problem. In order to classify a triangle, you should use only the adjactives from the statement. There is no triangle which could be described in two different ways according to the classification characteristics considered above. ------ Input ------ The first line of input contains an integer SUBTASK_{ID} denoting the subtask id this input belongs to. The second line of input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains six integers x_{1}, y_{1}, x_{2}, y_{2}, x_{3} and y_{3} denoting Cartesian coordinates of points, that form the triangle to be classified. It is guaranteed that the points are non-collinear. ------ Output ------ For each test case, output a single line containing the classification of the given triangle. If SUBTASK_{ID} equals 1, then the classification should follow the "Side classification starting with a capital letter> triangle" format. If SUBTASK_{ID} equals 2, then the classification should follow the "Side classification starting with a capital letter> angle classification> triangle" format. Please, check out the samples section to better understand the format of the output. ------ Constraints ------ $1 ≤ T ≤ 60$ $|x_{i}|, |y_{i}| ≤ 100$ $Subtask 1 (50 points): no additional constraints$ $Subtask 2 (50 points): no additional constraints$ ------ Note ------ The first test of the first subtask and the first test of the second subtask are the example tests (each in the corresponding subtask). It's made for you to make sure that your solution produces the same verdict both on your machine and our server. ------ Tip ------ Consider using the following condition in order to check whether two floats or doubles A and B are equal instead of traditional A == B: |A - B| < 10^{-6}. ----- Sample Input 1 ------ 1 2 0 0 1 1 1 2 3 0 0 4 4 7 ----- Sample Output 1 ------ Scalene triangle Isosceles triangle ----- explanation 1 ------ ----- Sample Input 2 ------ 2 6 0 0 4 1 1 3 0 0 1 0 1 2 0 0 1 1 1 2 0 0 2 1 1 2 3 0 0 4 4 7 0 0 2 1 4 0 ----- Sample Output 2 ------ Scalene acute triangle Scalene right triangle Scalene obtuse triangle Isosceles acute triangle Isosceles right triangle Isosceles obtuse triangle ----- explanation 2 ------ Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Sereja has an array a, consisting of n integers a_1, a_2, ..., a_{n}. The boy cannot sit and do nothing, he decided to study an array. Sereja took a piece of paper and wrote out m integers l_1, l_2, ..., l_{m} (1 ≤ l_{i} ≤ n). For each number l_{i} he wants to know how many distinct numbers are staying on the positions l_{i}, l_{i} + 1, ..., n. Formally, he want to find the number of distinct numbers among a_{l}_{i}, a_{l}_{i} + 1, ..., a_{n}.? Sereja wrote out the necessary array elements but the array was so large and the boy was so pressed for time. Help him, find the answer for the described question for each l_{i}. -----Input----- The first line contains two integers n and m (1 ≤ n, m ≤ 10^5). The second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — the array elements. Next m lines contain integers l_1, l_2, ..., l_{m}. The i-th line contains integer l_{i} (1 ≤ l_{i} ≤ n). -----Output----- Print m lines — on the i-th line print the answer to the number l_{i}. -----Examples----- Input 10 10 1 2 3 4 1 2 3 4 100000 99999 1 2 3 4 5 6 7 8 9 10 Output 6 6 6 6 6 5 4 3 2 1 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Vanya got an important task — he should enumerate books in the library and label each book with its number. Each of the n books should be assigned with a number from 1 to n. Naturally, distinct books should be assigned distinct numbers. Vanya wants to know how many digits he will have to write down as he labels the books. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^9) — the number of books in the library. -----Output----- Print the number of digits needed to number all the books. -----Examples----- Input 13 Output 17 Input 4 Output 4 -----Note----- Note to the first test. The books get numbers 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, which totals to 17 digits. Note to the second sample. The books get numbers 1, 2, 3, 4, which totals to 4 digits. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Given an integer, take the (mean) average of each pair of consecutive digits. Repeat this process until you have a single integer, then return that integer. e.g. Note: if the average of two digits is not an integer, round the result **up** (e.g. the average of 8 and 9 will be 9) ## Examples ``` digitsAverage(246) ==> 4 original: 2 4 6 \ / \ / 1st iter: 3 5 \ / 2nd iter: 4 digitsAverage(89) ==> 9 original: 8 9 \ / 1st iter: 9 ``` p.s. for a bigger challenge, check out the [one line version](https://www.codewars.com/kata/one-line-task-digits-average) of this kata by myjinxin2015! Write your solution by modifying this code: ```python def digits_average(input): ``` Your solution should implemented in the function "digits_average". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Dmitry has an array of $n$ non-negative integers $a_1, a_2, \dots, a_n$. In one operation, Dmitry can choose any index $j$ ($1 \le j \le n$) and increase the value of the element $a_j$ by $1$. He can choose the same index $j$ multiple times. For each $i$ from $0$ to $n$, determine whether Dmitry can make the $\mathrm{MEX}$ of the array equal to exactly $i$. If it is possible, then determine the minimum number of operations to do it. The $\mathrm{MEX}$ of the array is equal to the minimum non-negative integer that is not in the array. For example, the $\mathrm{MEX}$ of the array $[3, 1, 0]$ is equal to $2$, and the array $[3, 3, 1, 4]$ is equal to $0$. -----Input----- The first line of input data contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. The descriptions of the test cases follow. The first line of the description of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the array $a$. The second line of the description of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le n$) — elements of the array $a$. It is guaranteed that the sum of the values $n$ over all test cases in the test does not exceed $2\cdot10^5$. -----Output----- For each test case, output $n + 1$ integer — $i$-th number is equal to the minimum number of operations for which you can make the array $\mathrm{MEX}$ equal to $i$ ($0 \le i \le n$), or -1 if this cannot be done. -----Examples----- Input 5 3 0 1 3 7 0 1 2 3 4 3 2 4 3 0 0 0 7 4 6 2 3 5 0 5 5 4 0 1 0 4 Output 1 1 0 -1 1 1 2 2 1 0 2 6 3 0 1 4 3 1 0 -1 -1 -1 -1 -1 -1 2 1 0 2 -1 -1 -----Note----- In the first set of example inputs, $n=3$: to get $\mathrm{MEX}=0$, it is enough to perform one increment: $a_1$++; to get $\mathrm{MEX}=1$, it is enough to perform one increment: $a_2$++; $\mathrm{MEX}=2$ for a given array, so there is no need to perform increments; it is impossible to get $\mathrm{MEX}=3$ by performing increments. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. We have a tree with N vertices. Vertex 1 is the root of the tree, and the parent of Vertex i (2 \leq i \leq N) is Vertex P_i. To each vertex in the tree, Snuke will allocate a color, either black or white, and a non-negative integer weight. Snuke has a favorite integer sequence, X_1, X_2, ..., X_N, so he wants to allocate colors and weights so that the following condition is satisfied for all v. - The total weight of the vertices with the same color as v among the vertices contained in the subtree whose root is v, is X_v. Here, the subtree whose root is v is the tree consisting of Vertex v and all of its descendants. Determine whether it is possible to allocate colors and weights in this way. -----Constraints----- - 1 \leq N \leq 1 000 - 1 \leq P_i \leq i - 1 - 0 \leq X_i \leq 5 000 -----Inputs----- Input is given from Standard Input in the following format: N P_2 P_3 ... P_N X_1 X_2 ... X_N -----Outputs----- If it is possible to allocate colors and weights to the vertices so that the condition is satisfied, print POSSIBLE; otherwise, print IMPOSSIBLE. -----Sample Input----- 3 1 1 4 3 2 -----Sample Output----- POSSIBLE For example, the following allocation satisfies the condition: - Set the color of Vertex 1 to white and its weight to 2. - Set the color of Vertex 2 to black and its weight to 3. - Set the color of Vertex 3 to white and its weight to 2. There are also other possible allocations. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. It's another Start[c]up finals, and that means there is pizza to order for the onsite contestants. There are only 2 types of pizza (obviously not, but let's just pretend for the sake of the problem), and all pizzas contain exactly S slices. It is known that the i-th contestant will eat si slices of pizza, and gain ai happiness for each slice of type 1 pizza they eat, and bi happiness for each slice of type 2 pizza they eat. We can order any number of type 1 and type 2 pizzas, but we want to buy the minimum possible number of pizzas for all of the contestants to be able to eat their required number of slices. Given that restriction, what is the maximum possible total happiness that can be achieved? Input The first line of input will contain integers N and S (1 ≤ N ≤ 105, 1 ≤ S ≤ 105), the number of contestants and the number of slices per pizza, respectively. N lines follow. The i-th such line contains integers si, ai, and bi (1 ≤ si ≤ 105, 1 ≤ ai ≤ 105, 1 ≤ bi ≤ 105), the number of slices the i-th contestant will eat, the happiness they will gain from each type 1 slice they eat, and the happiness they will gain from each type 2 slice they eat, respectively. Output Print the maximum total happiness that can be achieved. Examples Input 3 12 3 5 7 4 6 7 5 9 5 Output 84 Input 6 10 7 4 7 5 8 8 12 5 8 6 11 6 3 3 7 5 9 6 Output 314 Note In the first example, you only need to buy one pizza. If you buy a type 1 pizza, the total happiness will be 3·5 + 4·6 + 5·9 = 84, and if you buy a type 2 pizza, the total happiness will be 3·7 + 4·7 + 5·5 = 74. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. -----Input----- The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. -----Output----- Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. -----Examples----- Input 8 10000011 Output 5 Input 2 01 Output 2 -----Note----- In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Recently, Vladimir got bad mark in algebra again. To avoid such unpleasant events in future he decided to train his arithmetic skills. He wrote four integer numbers a, b, c, d on the blackboard. During each of the next three minutes he took two numbers from the blackboard (not necessarily adjacent) and replaced them with their sum or their product. In the end he got one number. Unfortunately, due to the awful memory he forgot that number, but he remembers four original numbers, sequence of the operations and his surprise because of the very small result. Help Vladimir remember the forgotten number: find the smallest number that can be obtained from the original numbers by the given sequence of operations. Input First line contains four integers separated by space: 0 ≤ a, b, c, d ≤ 1000 — the original numbers. Second line contains three signs ('+' or '*' each) separated by space — the sequence of the operations in the order of performing. ('+' stands for addition, '*' — multiplication) Output Output one integer number — the minimal result which can be obtained. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d). Examples Input 1 1 1 1 + + * Output 3 Input 2 2 2 2 * * + Output 8 Input 1 2 3 4 * + + Output 9 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Takahashi loves numbers divisible by 2. You are given a positive integer N. Among the integers between 1 and N (inclusive), find the one that can be divisible by 2 for the most number of times. The solution is always unique. Here, the number of times an integer can be divisible by 2, is how many times the integer can be divided by 2 without remainder. For example, - 6 can be divided by 2 once: 6 -> 3. - 8 can be divided by 2 three times: 8 -> 4 -> 2 -> 1. - 3 can be divided by 2 zero times. -----Constraints----- - 1 ≤ N ≤ 100 -----Input----- Input is given from Standard Input in the following format: N -----Output----- Print the answer. -----Sample Input----- 7 -----Sample Output----- 4 4 can be divided by 2 twice, which is the most number of times among 1, 2, ..., 7. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. The Resistance is trying to take control over all planets in a particular solar system. This solar system is shaped like a tree. More precisely, some planets are connected by bidirectional hyperspace tunnels in such a way that there is a path between every pair of the planets, but removing any tunnel would disconnect some of them. The Resistance already has measures in place that will, when the time is right, enable them to control every planet that is not remote. A planet is considered to be remote if it is connected to the rest of the planets only via a single hyperspace tunnel. How much work is there left to be done: that is, how many remote planets are there? -----Input----- The first line of the input contains an integer N (2 ≤ N ≤ 1000) – the number of planets in the galaxy. The next N - 1 lines describe the hyperspace tunnels between the planets. Each of the N - 1 lines contains two space-separated integers u and v (1 ≤ u, v ≤ N) indicating that there is a bidirectional hyperspace tunnel between the planets u and v. It is guaranteed that every two planets are connected by a path of tunnels, and that each tunnel connects a different pair of planets. -----Output----- A single integer denoting the number of remote planets. -----Examples----- Input 5 4 1 4 2 1 3 1 5 Output 3 Input 4 1 2 4 3 1 4 Output 2 -----Note----- In the first example, only planets 2, 3 and 5 are connected by a single tunnel. In the second example, the remote planets are 2 and 3. Note that this problem has only two versions – easy and medium. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Given a string of words (x), you need to return an array of the words, sorted alphabetically by the final character in each. If two words have the same last letter, they returned array should show them in the order they appeared in the given string. All inputs will be valid. Write your solution by modifying this code: ```python def last(s): ``` Your solution should implemented in the function "last". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Recently, the bear started studying data structures and faced the following problem. You are given a sequence of integers x1, x2, ..., xn of length n and m queries, each of them is characterized by two integers li, ri. Let's introduce f(p) to represent the number of such indexes k, that xk is divisible by p. The answer to the query li, ri is the sum: <image>, where S(li, ri) is a set of prime numbers from segment [li, ri] (both borders are included in the segment). Help the bear cope with the problem. Input The first line contains integer n (1 ≤ n ≤ 106). The second line contains n integers x1, x2, ..., xn (2 ≤ xi ≤ 107). The numbers are not necessarily distinct. The third line contains integer m (1 ≤ m ≤ 50000). Each of the following m lines contains a pair of space-separated integers, li and ri (2 ≤ li ≤ ri ≤ 2·109) — the numbers that characterize the current query. Output Print m integers — the answers to the queries on the order the queries appear in the input. Examples Input 6 5 5 7 10 14 15 3 2 11 3 12 4 4 Output 9 7 0 Input 7 2 3 5 7 11 4 8 2 8 10 2 123 Output 0 7 Note Consider the first sample. Overall, the first sample has 3 queries. 1. The first query l = 2, r = 11 comes. You need to count f(2) + f(3) + f(5) + f(7) + f(11) = 2 + 1 + 4 + 2 + 0 = 9. 2. The second query comes l = 3, r = 12. You need to count f(3) + f(5) + f(7) + f(11) = 1 + 4 + 2 + 0 = 7. 3. The third query comes l = 4, r = 4. As this interval has no prime numbers, then the sum equals 0. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Polycarpus develops an interesting theory about the interrelation of arithmetic progressions with just everything in the world. His current idea is that the population of the capital of Berland changes over time like an arithmetic progression. Well, or like multiple arithmetic progressions. Polycarpus believes that if he writes out the population of the capital for several consecutive years in the sequence a_1, a_2, ..., a_{n}, then it is convenient to consider the array as several arithmetic progressions, written one after the other. For example, sequence (8, 6, 4, 2, 1, 4, 7, 10, 2) can be considered as a sequence of three arithmetic progressions (8, 6, 4, 2), (1, 4, 7, 10) and (2), which are written one after another. Unfortunately, Polycarpus may not have all the data for the n consecutive years (a census of the population doesn't occur every year, after all). For this reason, some values of a_{i} ​​may be unknown. Such values are represented by number -1. For a given sequence a = (a_1, a_2, ..., a_{n}), which consists of positive integers and values ​​-1, find the minimum number of arithmetic progressions Polycarpus needs to get a. To get a, the progressions need to be written down one after the other. Values ​​-1 may correspond to an arbitrary positive integer and the values a_{i} > 0 must be equal to the corresponding elements of sought consecutive record of the progressions. Let us remind you that a finite sequence c is called an arithmetic progression if the difference c_{i} + 1 - c_{i} of any two consecutive elements in it is constant. By definition, any sequence of length 1 is an arithmetic progression. -----Input----- The first line of the input contains integer n (1 ≤ n ≤ 2·10^5) — the number of elements in the sequence. The second line contains integer values a_1, a_2, ..., a_{n} separated by a space (1 ≤ a_{i} ≤ 10^9 or a_{i} = - 1). -----Output----- Print the minimum number of arithmetic progressions that you need to write one after another to get sequence a. The positions marked as -1 in a can be represented by any positive integers. -----Examples----- Input 9 8 6 4 2 1 4 7 10 2 Output 3 Input 9 -1 6 -1 2 -1 4 7 -1 2 Output 3 Input 5 -1 -1 -1 -1 -1 Output 1 Input 7 -1 -1 4 5 1 2 3 Output 2 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Dreamoon loves summing up something for no reason. One day he obtains two integers a and b occasionally. He wants to calculate the sum of all nice integers. Positive integer x is called nice if $\operatorname{mod}(x, b) \neq 0$ and $\frac{\operatorname{div}(x, b)}{\operatorname{mod}(x, b)} = k$, where k is some integer number in range [1, a]. By $\operatorname{div}(x, y)$ we denote the quotient of integer division of x and y. By $\operatorname{mod}(x, y)$ we denote the remainder of integer division of x and y. You can read more about these operations here: http://goo.gl/AcsXhT. The answer may be large, so please print its remainder modulo 1 000 000 007 (10^9 + 7). Can you compute it faster than Dreamoon? -----Input----- The single line of the input contains two integers a, b (1 ≤ a, b ≤ 10^7). -----Output----- Print a single integer representing the answer modulo 1 000 000 007 (10^9 + 7). -----Examples----- Input 1 1 Output 0 Input 2 2 Output 8 -----Note----- For the first sample, there are no nice integers because $\operatorname{mod}(x, 1)$ is always zero. For the second sample, the set of nice integers is {3, 5}. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Monocarp and Polycarp are learning new programming techniques. Now they decided to try pair programming. It's known that they have worked together on the same file for $n + m$ minutes. Every minute exactly one of them made one change to the file. Before they started, there were already $k$ lines written in the file. Every minute exactly one of them does one of two actions: adds a new line to the end of the file or changes one of its lines. Monocarp worked in total for $n$ minutes and performed the sequence of actions $[a_1, a_2, \dots, a_n]$. If $a_i = 0$, then he adds a new line to the end of the file. If $a_i > 0$, then he changes the line with the number $a_i$. Monocarp performed actions strictly in this order: $a_1$, then $a_2$, ..., $a_n$. Polycarp worked in total for $m$ minutes and performed the sequence of actions $[b_1, b_2, \dots, b_m]$. If $b_j = 0$, then he adds a new line to the end of the file. If $b_j > 0$, then he changes the line with the number $b_j$. Polycarp performed actions strictly in this order: $b_1$, then $b_2$, ..., $b_m$. Restore their common sequence of actions of length $n + m$ such that all actions would be correct — there should be no changes to lines that do not yet exist. Keep in mind that in the common sequence Monocarp's actions should form the subsequence $[a_1, a_2, \dots, a_n]$ and Polycarp's — subsequence $[b_1, b_2, \dots, b_m]$. They can replace each other at the computer any number of times. Let's look at an example. Suppose $k = 3$. Monocarp first changed the line with the number $2$ and then added a new line (thus, $n = 2, \: a = [2, 0]$). Polycarp first added a new line and then changed the line with the number $5$ (thus, $m = 2, \: b = [0, 5]$). Since the initial length of the file was $3$, in order for Polycarp to change line number $5$ two new lines must be added beforehand. Examples of correct sequences of changes, in this case, would be $[0, 2, 0, 5]$ and $[2, 0, 0, 5]$. Changes $[0, 0, 5, 2]$ (wrong order of actions) and $[0, 5, 2, 0]$ (line $5$ cannot be edited yet) are not correct. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$). Then $t$ test cases follow. Before each test case, there is an empty line. Each test case contains three lines. The first line contains three integers $k$, $n$, $m$ ($0 \le k \le 100$, $1 \le n, m \le 100$) — the initial number of lines in file and lengths of Monocarp's and Polycarp's sequences of changes respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 300$). The third line contains $m$ integers $b_1, b_2, \dots, b_m$ ($0 \le b_j \le 300$). -----Output----- For each test case print any correct common sequence of Monocarp's and Polycarp's actions of length $n + m$ or -1 if such sequence doesn't exist. -----Examples----- Input 5 3 2 2 2 0 0 5 4 3 2 2 0 5 0 6 0 2 2 1 0 2 3 5 4 4 6 0 8 0 0 7 0 9 5 4 1 8 7 8 0 0 Output 2 0 0 5 0 2 0 6 5 -1 0 6 0 7 0 8 0 9 -1 -----Note----- None Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Flatland has recently introduced a new type of an eye check for the driver's licence. The check goes like that: there is a plane with mannequins standing on it. You should tell the value of the minimum angle with the vertex at the origin of coordinates and with all mannequins standing inside or on the boarder of this angle. As you spend lots of time "glued to the screen", your vision is impaired. So you have to write a program that will pass the check for you. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of mannequins. Next n lines contain two space-separated integers each: x_{i}, y_{i} (|x_{i}|, |y_{i}| ≤ 1000) — the coordinates of the i-th mannequin. It is guaranteed that the origin of the coordinates has no mannequin. It is guaranteed that no two mannequins are located in the same point on the plane. -----Output----- Print a single real number — the value of the sought angle in degrees. The answer will be considered valid if the relative or absolute error doesn't exceed 10^{ - 6}. -----Examples----- Input 2 2 0 0 2 Output 90.0000000000 Input 3 2 0 0 2 -2 2 Output 135.0000000000 Input 4 2 0 0 2 -2 0 0 -2 Output 270.0000000000 Input 2 2 1 1 2 Output 36.8698976458 -----Note----- Solution for the first sample test is shown below: [Image] Solution for the second sample test is shown below: [Image] Solution for the third sample test is shown below: [Image] Solution for the fourth sample test is shown below: $\#$ Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward. Examples of numerical palindromes are: 2332 110011 54322345 For a given number ```num```, return its closest numerical palindrome which can either be smaller or larger than ```num```. If there are 2 possible values, the larger value should be returned. If ```num``` is a numerical palindrome itself, return it. For this kata, single digit numbers will NOT be considered numerical palindromes. Also, you know the drill - be sure to return "Not valid" if the input is not an integer or is less than 0. ``` palindrome(8) => 11 palindrome(281) => 282 palindrome(1029) => 1001 palindrome(1221) => 1221 palindrome("1221") => "Not valid" ``` ```Haskell In Haskell the function should return a Maybe Int with Nothing for cases where the argument is less than zero. ``` Other Kata in this Series: Numerical Palindrome #1 Numerical Palindrome #1.5 Numerical Palindrome #2 Numerical Palindrome #3 Numerical Palindrome #3.5 Numerical Palindrome #4 Numerical Palindrome #5 Write your solution by modifying this code: ```python def palindrome(num): ``` Your solution should implemented in the function "palindrome". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Have you heard about Megamind? Megamind and Metro Man are two aliens who came to earth. Megamind wanted to destroy the earth, while Metro Man wanted to stop him and protect mankind. After a lot of fighting, Megamind finally threw Metro Man up into the sky. Metro Man was defeated and was never seen again. Megamind wanted to be a super villain. He believed that the difference between a villain and a super villain is nothing but presentation. Megamind became bored, as he had nobody or nothing to fight against since Metro Man was gone. So, he wanted to create another hero against whom he would fight for recreation. But accidentally, another villain named Hal Stewart was created in the process, who also wanted to destroy the earth. Also, at some point Megamind had fallen in love with a pretty girl named Roxanne Ritchi. This changed him into a new man. Now he wants to stop Hal Stewart for the sake of his love. So, the ultimate fight starts now. * Megamind has unlimited supply of guns named Magic-48. Each of these guns has `shots` rounds of magic spells. * Megamind has perfect aim. If he shoots a magic spell it will definitely hit Hal Stewart. Once hit, it decreases the energy level of Hal Stewart by `dps` units. * However, since there are exactly `shots` rounds of magic spells in each of these guns, he may need to swap an old gun with a fully loaded one. This takes some time. Let’s call it swapping period. * Since Hal Stewart is a mutant, he has regeneration power. His energy level increases by `regen` unit during a swapping period. * Hal Stewart will be defeated immediately once his energy level becomes zero or negative. * Hal Stewart initially has the energy level of `hp` and Megamind has a fully loaded gun in his hand. * Given the values of `hp`, `dps`, `shots` and `regen`, find the minimum number of times Megamind needs to shoot to defeat Hal Stewart. If it is not possible to defeat him, return `-1` instead. # Example Suppose, `hp` = 13, `dps` = 4, `shots` = 3 and `regen` = 1. There are 3 rounds of spells in the gun. Megamind shoots all of them. Hal Stewart’s energy level decreases by 12 units, and thus his energy level becomes 1. Since Megamind’s gun is now empty, he will get a new gun and thus it’s a swapping period. At this time, Hal Stewart’s energy level will increase by 1 unit and will become 2. However, when Megamind shoots the next spell, Hal’s energy level will drop by 4 units and will become −2, thus defeating him. So it takes 4 shoots in total to defeat Hal Stewart. However, in this same example if Hal’s regeneration power was 50 instead of 1, it would have been impossible to defeat Hal. Write your solution by modifying this code: ```python def mega_mind(hp, dps, shots, regen): ``` Your solution should implemented in the function "mega_mind". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. In some other world, today is Christmas Eve. There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters. He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible. More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}? -----Constraints----- - 2 \leq K < N \leq 10^5 - 1 \leq h_i \leq 10^9 - h_i is an integer. -----Input----- Input is given from Standard Input in the following format: N K h_1 h_2 : h_N -----Output----- Print the minimum possible value of h_{max} - h_{min}. -----Sample Input----- 5 3 10 15 11 14 12 -----Sample Output----- 2 If we decorate the first, third and fifth trees, h_{max} = 12, h_{min} = 10 so h_{max} - h_{min} = 2. This is optimal. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Write a program which computes the area of a shape represented by the following three lines: $y = x^2$ $y = 0$ $x = 600$ It is clear that the area is $72000000$, if you use an integral you learn in high school. On the other hand, we can obtain an approximative area of the shape by adding up areas of many rectangles in the shape as shown in the following figure: <image> $f(x) = x^2$ The approximative area $s$ where the width of the rectangles is $d$ is: area of rectangle where its width is $d$ and height is $f(d)$ $+$ area of rectangle where its width is $d$ and height is $f(2d)$ $+$ area of rectangle where its width is $d$ and height is $f(3d)$ $+$ ... area of rectangle where its width is $d$ and height is $f(600 - d)$ The more we decrease $d$, the higer-precision value which is close to $72000000$ we could obtain. Your program should read the integer $d$ which is a divisor of $600$, and print the area $s$. Input The input consists of several datasets. Each dataset consists of an integer $d$ in a line. The number of datasets is less than or equal to 20. Output For each dataset, print the area $s$ in a line. Example Input 20 10 Output 68440000 70210000 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence. When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample). We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously. Help Vasya count to which girlfriend he will go more often. Input The first line contains two integers a and b (a ≠ b, 1 ≤ a, b ≤ 106). Output Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency. Examples Input 3 7 Output Dasha Input 5 3 Output Masha Input 2 3 Output Equal Note Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often. If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute. If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute. If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute. If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction. In sum Masha and Dasha get equal time — three minutes for each one, thus, Vasya will go to both girlfriends equally often. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. The screen that displays digital numbers that you often see on calculators is called a "7-segment display" because the digital numbers consist of seven parts (segments). The new product to be launched by Wakamatsu will incorporate a 7-segment display into the product, and as an employee, you will create a program to display the given number on the 7-segment display. This 7-segment display will not change until the next switch instruction is sent. By sending a signal consisting of 7 bits, the display information of each corresponding segment can be switched. Bits have a value of 1 or 0, where 1 stands for "switch" and 0 stands for "as is". The correspondence between each bit and segment is shown in the figure below. The signal sends 7 bits in the order of "gfedcba". For example, in order to display "0" from the hidden state, "0111111" must be sent to the display as a signal. To change from "0" to "5", send "1010010". If you want to change "5" to "1" in succession, send "1101011". <image> Create a program that takes n (1 ≤ n ≤ 100) numbers that you want to display and outputs the signal sequence required to correctly display those numbers di (0 ≤ di ≤ 9) on the 7-segment display. please. It is assumed that the initial state of the 7-segment display is all hidden. Input A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of -1. Each dataset is given in the following format: n d1 d2 :: dn The number of datasets does not exceed 120. Output For each input dataset, output the sequence of signals needed to properly output the numbers to the display. Example Input 3 0 5 1 1 0 -1 Output 0111111 1010010 1101011 0111111 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Snuke has decided to play a game, where the player runs a railway company. There are M+1 stations on Snuke Line, numbered 0 through M. A train on Snuke Line stops at station 0 and every d-th station thereafter, where d is a predetermined constant for each train. For example, if d = 3, the train stops at station 0, 3, 6, 9, and so forth. There are N kinds of souvenirs sold in areas around Snuke Line. The i-th kind of souvenirs can be purchased when the train stops at one of the following stations: stations l_i, l_i+1, l_i+2, ..., r_i. There are M values of d, the interval between two stops, for trains on Snuke Line: 1, 2, 3, ..., M. For each of these M values, find the number of the kinds of souvenirs that can be purchased if one takes a train with that value of d at station 0. Here, assume that it is not allowed to change trains. -----Constraints----- - 1 ≦ N ≦ 3 × 10^{5} - 1 ≦ M ≦ 10^{5} - 1 ≦ l_i ≦ r_i ≦ M -----Input----- The input is given from Standard Input in the following format: N M l_1 r_1 : l_{N} r_{N} -----Output----- Print the answer in M lines. The i-th line should contain the maximum number of the kinds of souvenirs that can be purchased if one takes a train stopping every i-th station. -----Sample Input----- 3 3 1 2 2 3 3 3 -----Sample Output----- 3 2 2 - If one takes a train stopping every station, three kinds of souvenirs can be purchased: kind 1, 2 and 3. - If one takes a train stopping every second station, two kinds of souvenirs can be purchased: kind 1 and 2. - If one takes a train stopping every third station, two kinds of souvenirs can be purchased: kind 2 and 3. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. The new operating system BerOS has a nice feature. It is possible to use any number of characters '/' as a delimiter in path instead of one traditional '/'. For example, strings //usr///local//nginx/sbin// and /usr/local/nginx///sbin are equivalent. The character '/' (or some sequence of such characters) at the end of the path is required only in case of the path to the root directory, which can be represented as single character '/'. A path called normalized if it contains the smallest possible number of characters '/'. Your task is to transform a given path to the normalized form. Input The first line of the input contains only lowercase Latin letters and character '/' — the path to some directory. All paths start with at least one character '/'. The length of the given line is no more than 100 characters, it is not empty. Output The path in normalized form. Examples Input //usr///local//nginx/sbin Output /usr/local/nginx/sbin Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≤ n ≤ 10^5, 0 ≤ q ≤ 3 ⋅ 10^5) — the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≤ a_i ≤ 10^9) — the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≤ m_j ≤ 10^{18}). Output For each teacher's query, output two numbers A and B — the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] — on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 — to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] ⇒ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] ⇒ A = 5, B = 2. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. While working at DTL, Ela is very aware of her physical and mental health. She started to practice various sports, such as Archery, Yoga, and Football. Since she started engaging in sports activities, Ela switches to trying a new sport on days she considers being "Luxury" days. She counts the days since she started these activities, in which the day she starts is numbered as day $1$. A "Luxury" day is the day in which the number of this day is a luxurious number. An integer $x$ is called a luxurious number if it is divisible by ${\lfloor \sqrt{x} \rfloor}$. Here $\lfloor r \rfloor$ denotes the "floor" of a real number $r$. In other words, it's the largest integer not greater than $r$. For example: $8$, $56$, $100$ are luxurious numbers, since $8$ is divisible by $\lfloor \sqrt{8} \rfloor = \lfloor 2.8284 \rfloor = 2$, $56$ is divisible $\lfloor \sqrt{56} \rfloor = \lfloor 7.4833 \rfloor = 7$, and $100$ is divisible by $\lfloor \sqrt{100} \rfloor = \lfloor 10 \rfloor = 10$, respectively. On the other hand $5$, $40$ are not, since $5$ are not divisible by $\lfloor \sqrt{5} \rfloor = \lfloor 2.2361 \rfloor = 2$, and $40$ are not divisible by $\lfloor \sqrt{40} \rfloor = \lfloor 6.3246 \rfloor = 6$. Being a friend of Ela, you want to engage in these fitness activities with her to keep her and yourself accompanied (and have fun together, of course). Between day $l$ and day $r$, you want to know how many times she changes the activities. -----Input----- Each test contains multiple test cases. The first line has the number of test cases $t$ ($1 \le t \le 10\ 000$). The description of the test cases follows. The only line of each test case contains two integers $l$ and $r$ ($1 \le l \le r \le 10^{18}$) — the intervals at which you want to know how many times Ela changes her sports. -----Output----- For each test case, output an integer that denotes the answer. -----Examples----- Input 5 8 19 8 20 119 121 1 100000000000000000 1234567891011 1000000000000000000 Output 5 6 2 948683296 2996666667 -----Note----- In the first test case, $5$ luxury numbers in range $[8, 19]$ are: $8, 9, 12, 15, 16$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Consider a sequence of integers $a_1, a_2, \ldots, a_n$. In one move, you can select any element of the sequence and delete it. After an element is deleted, all elements to the right are shifted to the left by $1$ position, so there are no empty spaces in the sequence. So after you make a move, the sequence's length decreases by $1$. The indices of the elements after the move are recalculated. E. g. let the sequence be $a=[3, 2, 2, 1, 5]$. Let's select the element $a_3=2$ in a move. Then after the move the sequence will be equal to $a=[3, 2, 1, 5]$, so the $3$-rd element of the new sequence will be $a_3=1$ and the $4$-th element will be $a_4=5$. You are given a sequence $a_1, a_2, \ldots, a_n$ and a number $k$. You need to find the minimum number of moves you have to make so that in the resulting sequence there will be at least $k$ elements that are equal to their indices, i. e. the resulting sequence $b_1, b_2, \ldots, b_m$ will contain at least $k$ indices $i$ such that $b_i = i$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 100$) — the number of test cases. Then $t$ test cases follow. Each test case consists of two consecutive lines. The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 2000$). The second line contains a sequence of integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). The numbers in the sequence are not necessarily different. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2000$. -----Output----- For each test case output in a single line: $-1$ if there's no desired move sequence; otherwise, the integer $x$ ($0 \le x \le n$) — the minimum number of the moves to be made so that the resulting sequence will contain at least $k$ elements that are equal to their indices. -----Examples----- Input 4 7 6 1 1 2 3 4 5 6 5 2 5 1 3 2 3 5 2 5 5 5 5 4 8 4 1 2 3 3 2 2 5 5 Output 1 2 -1 2 -----Note----- In the first test case the sequence doesn't satisfy the desired condition, but it can be provided by deleting the first element, hence the sequence will be $[1, 2, 3, 4, 5, 6]$ and $6$ elements will be equal to their indices. In the second test case there are two ways to get the desired result in $2$ moves: the first one is to delete the $1$-st and the $3$-rd elements so that the sequence will be $[1, 2, 3]$ and have $3$ elements equal to their indices; the second way is to delete the $2$-nd and the $3$-rd elements to get the sequence $[5, 2, 3]$ with $2$ desired elements. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. We held two competitions: Coding Contest and Robot Maneuver. In each competition, the contestants taking the 3-rd, 2-nd, and 1-st places receive 100000, 200000, and 300000 yen (the currency of Japan), respectively. Furthermore, a contestant taking the first place in both competitions receives an additional 400000 yen. DISCO-Kun took the X-th place in Coding Contest and the Y-th place in Robot Maneuver. Find the total amount of money he earned. Constraints * 1 \leq X \leq 205 * 1 \leq Y \leq 205 * X and Y are integers. Input Input is given from Standard Input in the following format: X Y Output Print the amount of money DISCO-Kun earned, as an integer. Examples Input 1 1 Output 1000000 Input 3 101 Output 100000 Input 4 4 Output 0 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. We have an integer sequence A, whose length is N. Find the number of the non-empty contiguous subsequences of A whose sums are 0. Note that we are counting the ways to take out subsequences. That is, even if the contents of some two subsequences are the same, they are counted individually if they are taken from different positions. Constraints * 1 \leq N \leq 2 \times 10^5 * -10^9 \leq A_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output Find the number of the non-empty contiguous subsequences of A whose sum is 0. Examples Input 6 1 3 -4 2 2 -2 Output 3 Input 7 1 -1 1 -1 1 -1 1 Output 12 Input 5 1 -2 3 -4 5 Output 0 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Little Petya very much likes playing with little Masha. Recently he has received a game called "Zero-One" as a gift from his mother. Petya immediately offered Masha to play the game with him. Before the very beginning of the game several cards are lain out on a table in one line from the left to the right. Each card contains a digit: 0 or 1. Players move in turns and Masha moves first. During each move a player should remove a card from the table and shift all other cards so as to close the gap left by the removed card. For example, if before somebody's move the cards on the table formed a sequence 01010101, then after the fourth card is removed (the cards are numbered starting from 1), the sequence will look like that: 0100101. The game ends when exactly two cards are left on the table. The digits on these cards determine the number in binary notation: the most significant bit is located to the left. Masha's aim is to minimize the number and Petya's aim is to maximize it. An unpleasant accident occurred before the game started. The kids spilled juice on some of the cards and the digits on the cards got blurred. Each one of the spoiled cards could have either 0 or 1 written on it. Consider all possible variants of initial arrangement of the digits (before the juice spilling). For each variant, let's find which two cards are left by the end of the game, assuming that both Petya and Masha play optimally. An ordered pair of digits written on those two cards is called an outcome. Your task is to find the set of outcomes for all variants of initial digits arrangement. Input The first line contains a sequence of characters each of which can either be a "0", a "1" or a "?". This sequence determines the initial arrangement of cards on the table from the left to the right. The characters "?" mean that the given card was spoiled before the game. The sequence's length ranges from 2 to 105, inclusive. Output Print the set of outcomes for all possible initial digits arrangements. Print each possible outcome on a single line. Each outcome should be represented by two characters: the digits written on the cards that were left by the end of the game. The outcomes should be sorted lexicographically in ascending order (see the first sample). Examples Input ???? Output 00 01 10 11 Input 1010 Output 10 Input 1?1 Output 01 11 Note In the first sample all 16 variants of numbers arrangement are possible. For the variant 0000 the outcome is 00. For the variant 1111 the outcome is 11. For the variant 0011 the outcome is 01. For the variant 1100 the outcome is 10. Regardless of outcomes for all other variants the set which we are looking for will contain all 4 possible outcomes. In the third sample only 2 variants of numbers arrangement are possible: 111 and 101. For the variant 111 the outcome is 11. For the variant 101 the outcome is 01, because on the first turn Masha can remove the first card from the left after which the game will end. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Given an integer N not less than 3, find the sum of the interior angles of a regular polygon with N sides. Print the answer in degrees, but do not print units. Constraints * 3 \leq N \leq 100 Input Input is given from Standard Input in the following format: N Output Print an integer representing the sum of the interior angles of a regular polygon with N sides. Examples Input 3 Output 180 Input 100 Output 17640 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. --- # Story The Pied Piper has been enlisted to play his magical tune and coax all the rats out of town. But some of the rats are deaf and are going the wrong way! # Kata Task How many deaf rats are there? # Legend * ```P``` = The Pied Piper * ```O~``` = Rat going left * ```~O``` = Rat going right # Example * ex1 ```~O~O~O~O P``` has 0 deaf rats * ex2 ```P O~ O~ ~O O~``` has 1 deaf rat * ex3 ```~O~O~O~OP~O~OO~``` has 2 deaf rats --- # Series * [The deaf rats of Hamelin (2D)](https://www.codewars.com/kata/the-deaf-rats-of-hamelin-2d) Write your solution by modifying this code: ```python def count_deaf_rats(town): ``` Your solution should implemented in the function "count_deaf_rats". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Your task is to make two functions, ```max``` and ```min``` (`maximum` and `minimum` in PHP and Python) that take a(n) array/vector of integers ```list``` as input and outputs, respectively, the largest and lowest number in that array/vector. #Examples ```python maximun([4,6,2,1,9,63,-134,566]) returns 566 minimun([-52, 56, 30, 29, -54, 0, -110]) returns -110 maximun([5]) returns 5 minimun([42, 54, 65, 87, 0]) returns 0 ``` #Notes - You may consider that there will not be any empty arrays/vectors. Write your solution by modifying this code: ```python def minimum(arr): ``` Your solution should implemented in the function "minimum". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. The math faculty of Berland State University has suffered the sudden drop in the math skills of enrolling students. This year the highest grade on the entrance math test was 8. Out of 100! Thus, the decision was made to make the test easier. Future students will be asked just a single question. They are given a sequence of integer numbers $a_1, a_2, \dots, a_n$, each number is from $1$ to $3$ and $a_i \ne a_{i + 1}$ for each valid $i$. The $i$-th number represents a type of the $i$-th figure: circle; isosceles triangle with the length of height equal to the length of base; square. The figures of the given sequence are placed somewhere on a Cartesian plane in such a way that: $(i + 1)$-th figure is inscribed into the $i$-th one; each triangle base is parallel to OX; the triangle is oriented in such a way that the vertex opposite to its base is at the top; each square sides are parallel to the axes; for each $i$ from $2$ to $n$ figure $i$ has the maximum possible length of side for triangle and square and maximum radius for circle. Note that the construction is unique for some fixed position and size of just the first figure. The task is to calculate the number of distinct points (not necessarily with integer coordinates) where figures touch. The trick is, however, that the number is sometimes infinite. But that won't make the task difficult for you, will it? So can you pass the math test and enroll into Berland State University? -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$) — the number of figures. The second line contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3$, $a_i \ne a_{i + 1}$) — types of the figures. -----Output----- The first line should contain either the word "Infinite" if the number of distinct points where figures touch is infinite or "Finite" otherwise. If the number is finite than print it in the second line. It's guaranteed that the number fits into 32-bit integer type. -----Examples----- Input 3 2 1 3 Output Finite 7 Input 3 1 2 3 Output Infinite -----Note----- Here are the glorious pictures for the examples. Note that the triangle is not equilateral but just isosceles with the length of height equal to the length of base. Thus it fits into a square in a unique way. The distinct points where figures touch are marked red. In the second example the triangle and the square touch each other for the whole segment, it contains infinite number of points. [Image] Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. ABC Gene There is a gene sequence represented by the string `ABC`. You can rewrite this gene sequence by performing the following operations several times. * Choose one of the letters `A`,` B`, `C`. Let this be x. Replace all x in the gene sequence with `ABC` at the same time. Given a string S consisting only of `A`,` B`, and `C`. Determine if the gene sequence can be matched to S. Constraints * 1 ≤ | S | ≤ 5,000 * S consists only of `A`,` B`, and `C`. Input Format Input is given from standard input in the following format. S Output Format Output `Yes` if the gene sequence can be matched to S, and` No` if it cannot be matched. Sample Input 1 ABC Sample Output 1 Yes The gene sequence is `ABC` from the beginning. Sample Input 2 AABCC Sample Output 2 Yes If you select `B` and perform the operation, it becomes` ABC` → `AABCC`. Sample Input 3 AABCABC Sample Output 3 No For example, even if you select `C` and perform an operation, it does not change from` AABCC` to `AABCABC`. Since all `C`s are replaced with` ABC` at the same time, the actual result is `AABCC` →` AABABCABC`. Example Input ABC Output Yes Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Vasya plays The Elder Trolls IV: Oblivon. Oh, those creators of computer games! What they do not come up with! Absolutely unique monsters have been added to the The Elder Trolls IV: Oblivon. One of these monsters is Unkillable Slug. Why it is "Unkillable"? Firstly, because it can be killed with cutting weapon only, so lovers of two-handed amber hammers should find suitable knife themselves. Secondly, it is necessary to make so many cutting strokes to Unkillable Slug. Extremely many. Too many! Vasya has already promoted his character to 80-th level and in order to gain level 81 he was asked to kill Unkillable Slug. The monster has a very interesting shape. It looks like a rectangular parallelepiped with size x × y × z, consisting of undestructable cells 1 × 1 × 1. At one stroke Vasya can cut the Slug along an imaginary grid, i.e. cut with a plane parallel to one of the parallelepiped side. Monster dies when amount of parts it is divided reaches some critical value. All parts of monster do not fall after each cut, they remains exactly on its places. I. e. Vasya can cut several parts with one cut. Vasya wants to know what the maximum number of pieces he can cut the Unkillable Slug into striking him at most k times. Vasya's character uses absolutely thin sword with infinite length. Input The first line of input contains four integer numbers x, y, z, k (1 ≤ x, y, z ≤ 106, 0 ≤ k ≤ 109). Output Output the only number — the answer for the problem. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). Examples Input 2 2 2 3 Output 8 Input 2 2 2 1 Output 2 Note In the first sample Vasya make 3 pairwise perpendicular cuts. He cuts monster on two parts with the first cut, then he divides each part on two with the second cut, and finally he divides each of the 4 parts on two. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Dr .: Peter, I've finally done it. Peter: What's wrong, Dr. David? Is it a silly invention again? Dr .: This table, this table. | Character | Sign --- | --- (Blank) | 101 '| 000000 , | 000011 -| 10010001 . | 010001 ? | 000001 A | 100101 B | 10011010 | Character | Sign --- | --- C | 0101 D | 0001 E | 110 F | 01001 G | 10011011 H | 010000 I | 0111 J | 10011000 | Character | Sign --- | --- K | 0110 L | 00100 M | 10011001 N | 10011110 O | 00101 P | 111 Q | 10011111 R | 1000 | Character | Sign --- | --- S | 00110 T | 00111 U | 10011100 V | 10011101 W | 000010 X | 10010010 Y | 10010011 Z | 10010000 Peter: What? This table is. Dr .: Okay, just do what you say. First, write your name on a piece of paper. Peter: Yes, "PETER POTTER". Dr .: Then, replace each character with the "code" in this table. Peter: Well, change "P" to "111" and "E" to "110" ... It's quite annoying. 111 110 00111 110 1000 101 111 00101 00111 00111 110 1000 became. It looks like a barcode. Dr .: All right. Then connect all the replaced strings and separate them by 5 characters. Peter: Yes, if you connect and separate. 11111 00011 11101 00010 11110 01010 01110 01111 10100 0 It turned out to be something like this. But what about the last "0" guy? Dr .: Add 0 to make it 5 letters. Peter: Well, there is only one 0 at the end, so I should add four more 0s. I was able to do it. 11111 00011 11101 00010 11110 01010 01110 01111 10100 00000 Dr .: Next, use this table. | Sign | Character --- | --- 00000 | A 00001 | B 00010 | C 00011 | D 00100 | E 00101 | F 00110 | G 00111 | H | Sign | Character --- | --- 01000 | I 01001 | J 01010 | K 01011 | L 01100 | M 01101 | N 01110 | O 01111 | P | Sign | Character --- | --- 10000 | Q 10001 | R 10010 | S 10011 | T 10100 | U 10101 | V 10110 | W 10111 | X | Sign | Character --- | --- 11000 | Y 11001 | Z 11010 | (blank) 11011 | .. 11100 |, 11101 |- 11110 |' 11111 |? Peter: How do you use this ... yeah! Now you're going to replace the code with a letter! Dr .: That's right. If it is "11111", go to "?", If it is "00011", go to "D". Peter: This is simple ... well, it became "? D-C'KOPUA". But it doesn't make sense. Dr .: Count the number of characters. Peter: It's 10 characters. Oh, "PETER POTTER" was 12 characters, but 2 characters are reduced. Dr .: Yes, you can use this table to reduce the number of letters. Now do the same thing in this sentence. PETER PIPER PICKED A PECK OF PICKLED PEPPERS. A PECK OF PICKLED PEPPERS PETER PIPER PICKED. IF PETER PIPER PICKED A PECK OF PICKLED PEPPERS, WHERE'S THE PECK OF PICKLED PEPPERS PETER PIPER PICKED? Peter: Gyogyo, the lines are separated, what do you do? Dr .: Due to space limitations, there are only 3 lines, but think of it as a long line with a space instead of a newline character. Peter: Yes, yes. There is a blank space between the lines. But it's a hassle ... Dr .: Then why not let the program do it? So, instead of Peter, create a program that converts the read character string into a code and outputs it. input Given multiple datasets. Each dataset is given a line of strings (consisting of the characters contained in the table). The string contains at least 1 and no more than 100 characters. The number of datasets does not exceed 200. output For each data set, output the converted character string on one line. Example Input PETER POTTER Output ?D-C'KOPUA Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given two strings A and B representing essays of two students who are suspected cheaters. For any two strings C, D we define their similarity score S(C,D) as 4⋅ LCS(C,D) - |C| - |D|, where LCS(C,D) denotes the length of the Longest Common Subsequence of strings C and D. You believe that only some part of the essays could have been copied, therefore you're interested in their substrings. Calculate the maximal similarity score over all pairs of substrings. More formally, output maximal S(C, D) over all pairs (C, D), where C is some substring of A, and D is some substring of B. If X is a string, |X| denotes its length. A string a is a substring of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters. Pay attention to the difference between the substring and subsequence, as they both appear in the problem statement. You may wish to read the [Wikipedia page about the Longest Common Subsequence problem](https://en.wikipedia.org/wiki/Longest_common_subsequence_problem). Input The first line contains two positive integers n and m (1 ≤ n, m ≤ 5000) — lengths of the two strings A and B. The second line contains a string consisting of n lowercase Latin letters — string A. The third line contains a string consisting of m lowercase Latin letters — string B. Output Output maximal S(C, D) over all pairs (C, D), where C is some substring of A, and D is some substring of B. Examples Input 4 5 abba babab Output 5 Input 8 10 bbbbabab bbbabaaaaa Output 12 Input 7 7 uiibwws qhtkxcn Output 0 Note For the first case: abb from the first string and abab from the second string have LCS equal to abb. The result is S(abb, abab) = (4 ⋅ |abb|) - |abb| - |abab| = 4 ⋅ 3 - 3 - 4 = 5. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Of course you have heard the famous task about Hanoi Towers, but did you know that there is a special factory producing the rings for this wonderful game? Once upon a time, the ruler of the ancient Egypt ordered the workers of Hanoi Factory to create as high tower as possible. They were not ready to serve such a strange order so they had to create this new tower using already produced rings. There are n rings in factory's stock. The i-th ring has inner radius a_{i}, outer radius b_{i} and height h_{i}. The goal is to select some subset of rings and arrange them such that the following conditions are satisfied: Outer radiuses form a non-increasing sequence, i.e. one can put the j-th ring on the i-th ring only if b_{j} ≤ b_{i}. Rings should not fall one into the the other. That means one can place ring j on the ring i only if b_{j} > a_{i}. The total height of all rings used should be maximum possible. -----Input----- The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of rings in factory's stock. The i-th of the next n lines contains three integers a_{i}, b_{i} and h_{i} (1 ≤ a_{i}, b_{i}, h_{i} ≤ 10^9, b_{i} > a_{i}) — inner radius, outer radius and the height of the i-th ring respectively. -----Output----- Print one integer — the maximum height of the tower that can be obtained. -----Examples----- Input 3 1 5 1 2 6 2 3 7 3 Output 6 Input 4 1 2 1 1 3 3 4 6 2 5 7 1 Output 4 -----Note----- In the first sample, the optimal solution is to take all the rings and put them on each other in order 3, 2, 1. In the second sample, one can put the ring 3 on the ring 4 and get the tower of height 3, or put the ring 1 on the ring 2 and get the tower of height 4. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. # Introduction Hamsters are rodents belonging to the subfamily Cricetinae. The subfamily contains about 25 species, classified in six or seven genera. They have become established as popular small house pets, and, partly because they are easy to breed in captivity, hamsters are often used as laboratory animals. # Task Write a function that accepts two inputs: `code` and `message` and returns an encrypted string from `message` using the `code`. The `code` is a string that generates the key in the way shown below: ``` 1 | h a m s t e r 2 | i b n u f 3 | j c o v g 4 | k d p w 5 | l q x 6 | y 7 | z ``` All letters from `code` get number `1`. All letters which directly follow letters from `code` get number `2` (unless they already have a smaller number assigned), etc. It's difficult to describe but it should be easy to understand from the example below: ``` 1 | a e h m r s t 2 | b f i n u 3 | c g j o v 4 | d k p w 5 | l q x 6 | y 7 | z ``` How does the encoding work using the `hamster` code? ``` a => a1 b => a2 c => a3 d => a4 e => e1 f => e2 ... ``` And applying it to strings : ``` hamsterMe('hamster', 'hamster') => h1a1m1s1t1e1r1 hamsterMe('hamster', 'helpme') => h1e1h5m4m1e1 ``` And you probably started wondering what will happen if there is no `a` in the `code`. Just add these letters after the last available letter (in alphabetic order) in the `code`. The key for code `hmster` is: ``` 1 | e h m r s t 2 | f i n u 3 | g j o v 4 | k p w 5 | l q x 6 | y 7 | z 8 | a 9 | b 10 | c 11 | d ``` # Additional notes The `code` will have at least 1 letter. Duplication of letters in `code` is possible and should be handled. The `code` and `message` consist of only lowercase letters. Write your solution by modifying this code: ```python def hamster_me(code, message): ``` Your solution should implemented in the function "hamster_me". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese , Russian and Vietnamese A number is called palindromic if its decimal representation is a palindrome. You are given a range, described by a pair of integers L and R. Find the sum of all palindromic numbers lying in the range [L, R], inclusive of both the extrema. ------ Input ------ The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains a pair of space separated integers L and R denoting the range for which you are required to find the sum of the palindromic numbers. ------ Output ------ For each test case, output a single line containing the sum of all the palindromic numbers in the given range. ------ Constraints ------ $1 ≤ T ≤ 100$ $Subtask 1 (34 points) : 1 ≤ L ≤ R ≤ 10^{3}$ $Subtask 2 (66 points) : 1 ≤ L ≤ R ≤ 10^{5}$ ----- Sample Input 1 ------ 2 1 10 123 150 ----- Sample Output 1 ------ 45 272 ----- explanation 1 ------ Example case 1. The palindromic numbers between 1 and 10 are all numbers except the number 10. Their sum is 45. Example case 2. The palindromic numbers between 123 and 150 are 131 and 141 and their sum is 272. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given a tree consisting of $n$ vertices. A number is written on each vertex; the number on vertex $i$ is equal to $a_i$. Recall that a simple path is a path that visits each vertex at most once. Let the weight of the path be the bitwise XOR of the values written on vertices it consists of. Let's say that a tree is good if no simple path has weight $0$. You can apply the following operation any number of times (possibly, zero): select a vertex of the tree and replace the value written on it with an arbitrary positive integer. What is the minimum number of times you have to apply this operation in order to make the tree good? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of vertices. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i < 2^{30}$) — the numbers written on vertices. Then $n - 1$ lines follow, each containing two integers $x$ and $y$ ($1 \le x, y \le n; x \ne y$) denoting an edge connecting vertex $x$ with vertex $y$. It is guaranteed that these edges form a tree. -----Output----- Print a single integer — the minimum number of times you have to apply the operation in order to make the tree good. -----Examples----- Input 6 3 2 1 3 2 1 4 5 3 4 1 4 2 1 6 1 Output 2 Input 4 2 1 1 1 1 2 1 3 1 4 Output 0 Input 5 2 2 2 2 2 1 2 2 3 3 4 4 5 Output 2 -----Note----- In the first example, it is enough to replace the value on the vertex $1$ with $13$, and the value on the vertex $4$ with $42$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Once upon a time a child got a test consisting of multiple-choice questions as homework. A multiple-choice question consists of four choices: A, B, C and D. Each choice has a description, and the child should find out the only one that is correct. Fortunately the child knows how to solve such complicated test. The child will follow the algorithm: If there is some choice whose description at least twice shorter than all other descriptions, or at least twice longer than all other descriptions, then the child thinks the choice is great. If there is exactly one great choice then the child chooses it. Otherwise the child chooses C (the child think it is the luckiest choice). You are given a multiple-choice questions, can you predict child's choose? -----Input----- The first line starts with "A." (without quotes), then followed the description of choice A. The next three lines contains the descriptions of the other choices in the same format. They are given in order: B, C, D. Please note, that the description goes after prefix "X.", so the prefix mustn't be counted in description's length. Each description is non-empty and consists of at most 100 characters. Each character can be either uppercase English letter or lowercase English letter, or "_". -----Output----- Print a single line with the child's choice: "A", "B", "C" or "D" (without quotes). -----Examples----- Input A.VFleaKing_is_the_author_of_this_problem B.Picks_is_the_author_of_this_problem C.Picking_is_the_author_of_this_problem D.Ftiasch_is_cute Output D Input A.ab B.abcde C.ab D.abc Output C Input A.c B.cc C.c D.c Output B -----Note----- In the first sample, the first choice has length 39, the second one has length 35, the third one has length 37, and the last one has length 15. The choice D (length 15) is twice shorter than all other choices', so it is great choice. There is no other great choices so the child will choose D. In the second sample, no choice is great, so the child will choose the luckiest choice C. In the third sample, the choice B (length 2) is twice longer than all other choices', so it is great choice. There is no other great choices so the child will choose B. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Sensation, sensation in the two-dimensional kingdom! The police have caught a highly dangerous outlaw, member of the notorious "Pihters" gang. The law department states that the outlaw was driving from the gang's headquarters in his car when he crashed into an ice cream stall. The stall, the car, and the headquarters each occupies exactly one point on the two-dimensional kingdom. The outlaw's car was equipped with a GPS transmitter. The transmitter showed that the car made exactly n movements on its way from the headquarters to the stall. A movement can move the car from point (x, y) to one of these four points: to point (x - 1, y) which we will mark by letter "L", to point (x + 1, y) — "R", to point (x, y - 1) — "D", to point (x, y + 1) — "U". The GPS transmitter is very inaccurate and it doesn't preserve the exact sequence of the car's movements. Instead, it keeps records of the car's possible movements. Each record is a string of one of these types: "UL", "UR", "DL", "DR" or "ULDR". Each such string means that the car made a single movement corresponding to one of the characters of the string. For example, string "UL" means that the car moved either "U", or "L". You've received the journal with the outlaw's possible movements from the headquarters to the stall. The journal records are given in a chronological order. Given that the ice-cream stall is located at point (0, 0), your task is to print the number of different points that can contain the gang headquarters (that is, the number of different possible locations of the car's origin). Input The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of the car's movements from the headquarters to the stall. Each of the following n lines describes the car's possible movements. It is guaranteed that each possible movement is one of the following strings: "UL", "UR", "DL", "DR" or "ULDR". All movements are given in chronological order. Please do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin and cout stream or the %I64d specifier. Output Print a single integer — the number of different possible locations of the gang's headquarters. Examples Input 3 UR UL ULDR Output 9 Input 2 DR DL Output 4 Note The figure below shows the nine possible positions of the gang headquarters from the first sample: <image> For example, the following movements can get the car from point (1, 0) to point (0, 0): <image> Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Find the greatest common divisor of two positive integers. The integers can be large, so you need to find a clever solution. The inputs `x` and `y` are always greater or equal to 1, so the greatest common divisor will always be an integer that is also greater or equal to 1. Write your solution by modifying this code: ```python def mygcd(x,y): ``` Your solution should implemented in the function "mygcd". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Being a nonconformist, Volodya is displeased with the current state of things, particularly with the order of natural numbers (natural number is positive integer number). He is determined to rearrange them. But there are too many natural numbers, so Volodya decided to start with the first n. He writes down the following sequence of numbers: firstly all odd integers from 1 to n (in ascending order), then all even integers from 1 to n (also in ascending order). Help our hero to find out which number will stand at the position number k. -----Input----- The only line of input contains integers n and k (1 ≤ k ≤ n ≤ 10^12). Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Output----- Print the number that will stand at the position number k after Volodya's manipulations. -----Examples----- Input 10 3 Output 5 Input 7 7 Output 6 -----Note----- In the first sample Volodya's sequence will look like this: {1, 3, 5, 7, 9, 2, 4, 6, 8, 10}. The third place in the sequence is therefore occupied by the number 5. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Today, Mezo is playing a game. Zoma, a character in that game, is initially at position $x = 0$. Mezo starts sending $n$ commands to Zoma. There are two possible commands: 'L' (Left) sets the position $x: =x - 1$; 'R' (Right) sets the position $x: =x + 1$. Unfortunately, Mezo's controller malfunctions sometimes. Some commands are sent successfully and some are ignored. If the command is ignored then the position $x$ doesn't change and Mezo simply proceeds to the next command. For example, if Mezo sends commands "LRLR", then here are some possible outcomes (underlined commands are sent successfully): "LRLR" — Zoma moves to the left, to the right, to the left again and to the right for the final time, ending up at position $0$; "LRLR" — Zoma recieves no commands, doesn't move at all and ends up at position $0$ as well; "LRLR" — Zoma moves to the left, then to the left again and ends up in position $-2$. Mezo doesn't know which commands will be sent successfully beforehand. Thus, he wants to know how many different positions may Zoma end up at. -----Input----- The first line contains $n$ $(1 \le n \le 10^5)$ — the number of commands Mezo sends. The second line contains a string $s$ of $n$ commands, each either 'L' (Left) or 'R' (Right). -----Output----- Print one integer — the number of different positions Zoma may end up at. -----Example----- Input 4 LRLR Output 5 -----Note----- In the example, Zoma may end up anywhere between $-2$ and $2$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. # Task John and Alice have an appointment today. In the morning, John starts from (`0,0`) and goes to the place (`a,b`) where he is dating. Unfortunately, John had no sense of direction at all, so he moved 1 step in a random direction(up, down, left or right) each time. For example, if John at (x,y), next step he may move to `(x+1,y), (x-1,y),(x,y+1) or (x,y-1)`. Obviously, when he arrived at the destination, it was already too late and Alice had already left. It's a sadly story :( The second day, Alice asked John why he didn't go to the dating place. John said he took `s` steps to his date yesterday. Alice wants to know whether John is lying. Please help Alice to judge. Given two coordinates `a, b` and the step `s`, return `true` if John tells the truth, `false` otherwise. # Input/Output `[input]` integer `a` The x-coordinates of the dating site. `-10^7 <= a <= 10^7` `[input]` integer `b` The y-coordinates of the dating site. `-10^7 <= b <= 10^7` `[input]` integer `s` A positive integer. The steps John using. `0 < s <= 10^9` `[output]` a boolean value return `true` if John tells the truth, `false` otherwise. # Example For `a = 3, b = 3, s = 6`, the output should be `true`. A possible path is: `(0,0) -> (0,1) -> (0,2) -> (0,3) -> (1,3) -> (2,3) -> (3,3)` For `a = 3, b = 3, s = 8`, the output should be `true`. A possible path is: `(0,0) -> (0,1) -> (1,1) -> (1,2) -> (2,2) -> (2,1) -> (3,1) -> (3,2) -> (3,3)` For `a = 4, b = 5, s = 10`, the output should be `false`. John can't reach coordinates (a, b) using 10 steps, he's lying ;-) Write your solution by modifying this code: ```python def is_john_lying(a,b,s): ``` Your solution should implemented in the function "is_john_lying". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Taro made a sugoroku so that everyone can play at the children's association event. In order to make the game interesting, I wrote instructions such as "advance 6" and "back 5" in some of the sugoroku squares other than "Furidashi" and "Agari". Turn the roulette wheel to advance as many times as you can, and if an instruction is written on the stopped square, move according to that instruction. However, it does not follow the instructions of the squares that proceeded according to the instructions. Roulette shall be able to give a number between 1 and a certain number with equal probability. Also, if you get a larger number than you reach "Agari", or if you follow the instructions and you go beyond "Agari", you will move to "Agari". If you follow the instructions and return before "Furidashi", you will return to "Furidashi". <image> However, depending on the instructions of the roulette wheel and the square, it may not be possible to reach the "rise". For example, let's say you made a sugoroku like the one shown in the figure. If you use a roulette wheel that only gives 1 and 2, you can go to "Agari" if you come out in the order of 1 and 2, but if you get 2 first, you will not be able to reach "Agari" forever no matter what comes out. Taro didn't know that and wrote instructions in various squares. Therefore, on behalf of Taro, please create a program to determine whether or not you may not be able to reach the "rise" depending on the instructions of the roulette and the square. input The input consists of multiple datasets. The end of the input is indicated by a single zero line. Each dataset is given in the following format. max n d1 d2 .. .. .. dn The first line gives the maximum number of roulette wheels max (2 ≤ max ≤ 250), and the second line gives the number of cells other than "starting" and "rising" n (2 ≤ n ≤ 250). .. The next n lines are given the number di (-n ≤ di ≤ n) that represents the indication for each cell. When di is zero, it means that no instruction is written, when it is a positive number, it means | di | forward instruction, and when it is negative, it means | di | back instruction (where | x | is x). Represents an absolute value). All values ​​entered are integers. The number of datasets does not exceed 100. output The judgment result is output to one line for each data set. Depending on the instructions of the roulette and the square, if it may not be possible to reach the "go up", "NG" is output, otherwise "OK" is output. Example Input 3 3 -2 1 0 2 4 2 0 -1 -2 2 2 -2 -2 0 Output OK NG NG Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given a binary array A=(A_1,A_2,\cdots,A_N) of length N. Process Q queries of the following types. The i-th query is represented by three integers T_i,L_i,R_i. * T_i=1: Replace the value of A_j with 1-A_j for each L_i \leq j \leq R_i. * T_i=2: Calculate the inversion(*) of the array A_{L_i},A_{L_i+1},\cdots,A_{R_i}. Note:The inversion of the array x_1,x_2,\cdots,x_k is the number of the pair of integers i,j with 1 \leq i < j \leq k, x_i > x_j. Constraints * 1 \leq N \leq 2 \times 10^5 * 0 \leq A_i \leq 1 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq T_i \leq 2 * 1 \leq L_i \leq R_i \leq N * All values in Input are integer. Input Input is given from Standard Input in the following format: N Q A_1 A_2 \cdots A_N T_1 L_1 R_1 T_2 L_2 R_2 \vdots T_Q L_Q R_Q Output For each query with T_i=2, print the answer. Example Input 5 5 0 1 0 0 1 2 1 5 1 3 4 2 2 5 1 1 3 2 1 2 Output 2 0 1 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. One day Vasya painted a Cartesian coordinate system on a piece of paper and marked some set of points (x1, y1), (x2, y2), ..., (xn, yn). Let's define neighbors for some fixed point from the given set (x, y): * point (x', y') is (x, y)'s right neighbor, if x' > x and y' = y * point (x', y') is (x, y)'s left neighbor, if x' < x and y' = y * point (x', y') is (x, y)'s lower neighbor, if x' = x and y' < y * point (x', y') is (x, y)'s upper neighbor, if x' = x and y' > y We'll consider point (x, y) from the given set supercentral, if it has at least one upper, at least one lower, at least one left and at least one right neighbor among this set's points. Vasya marked quite many points on the paper. Analyzing the picture manually is rather a challenge, so Vasya asked you to help him. Your task is to find the number of supercentral points in the given set. Input The first input line contains the only integer n (1 ≤ n ≤ 200) — the number of points in the given set. Next n lines contain the coordinates of the points written as "x y" (without the quotes) (|x|, |y| ≤ 1000), all coordinates are integers. The numbers in the line are separated by exactly one space. It is guaranteed that all points are different. Output Print the only number — the number of supercentral points of the given set. Examples Input 8 1 1 4 2 3 1 1 2 0 2 0 1 1 0 1 3 Output 2 Input 5 0 0 0 1 1 0 0 -1 -1 0 Output 1 Note In the first sample the supercentral points are only points (1, 1) and (1, 2). In the second sample there is one supercental point — point (0, 0). Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. <image> My grandmother uses a balance. The balance will balance if you place the same size on both of the two dishes, otherwise it will tilt to the heavier side. The weights of the 10 weights are 1g, 2g, 4g, 8g, 16g, 32g, 64g, 128g, 256g, 512g in order of lightness. My grandmother says, "Weigh up to about 1 kg in grams." "Then, try to weigh the juice here," and my grandmother put the juice on the left plate and the 8g, 64g, and 128g weights on the right plate to balance. Then he answered, "The total weight is 200g, so the juice is 200g. How is it correct?" Since the weight of the item to be placed on the left plate is given, create a program that outputs the weight to be placed on the right plate in order of lightness when balancing with the item of the weight given by the balance. However, the weight of the item to be weighed shall be less than or equal to the total weight of all weights (= 1023g). Hint The weight of the weight is 2 to the nth power (n = 0, 1, .... 9) g. Input Given multiple datasets. For each dataset, the weight of the item to be placed on the left plate is given in one line. Please process until the end of the input. The number of datasets does not exceed 50. Output For each data set, separate the weights (ascending order) to be placed on the right plate with one blank and output them on one line. Example Input 5 7 127 Output 1 4 1 2 4 1 2 4 8 16 32 64 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Pasha has a positive integer a without leading zeroes. Today he decided that the number is too small and he should make it larger. Unfortunately, the only operation Pasha can do is to swap two adjacent decimal digits of the integer. Help Pasha count the maximum number he can get if he has the time to make at most k swaps. -----Input----- The single line contains two integers a and k (1 ≤ a ≤ 10^18; 0 ≤ k ≤ 100). -----Output----- Print the maximum number that Pasha can get if he makes at most k swaps. -----Examples----- Input 1990 1 Output 9190 Input 300 0 Output 300 Input 1034 2 Output 3104 Input 9090000078001234 6 Output 9907000008001234 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. # Problem In China,there is an ancient mathematical book, called "The Mathematical Classic of Sun Zi"(《孙子算经》). In the book, there is a classic math problem: “今有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二,问物几何?” Ahh, Sorry. I forgot that you don't know Chinese. Let's translate it to English: There is a unkown positive integer `n`. We know: `n % 3 = 2`, and `n % 5 = 3`, and `n % 7 = 2`. What's the minimum possible positive integer `n`? The correct answer is `23`. # Task You are given three non-negative integers `x,y,z`. They represent the remainders of the unknown positive integer `n` divided by 3,5,7. That is: `n % 3 = x, n % 5 = y, n % 7 = z` Your task is to find the minimum possible positive integer `n` and return it. # Example For `x = 2, y = 3, z = 2`, the output should be `23` `23 % 3 = 2, 23 % 5 = 3, 23 % 7 = 2` For `x = 1, y = 2, z = 3`, the output should be `52` `52 % 3 = 1, 52 % 5 = 2, 52 % 7 = 3` For `x = 1, y = 3, z = 5`, the output should be `103` `103 % 3 = 1, 103 % 5 = 3, 103 % 7 = 5` For `x = 0, y = 0, z = 0`, the output should be `105` For `x = 1, y = 1, z = 1`, the output should be `1` # Note - `0 <= x < 3, 0 <= y < 5, 0 <= z < 7` - Happy Coding `^_^` Write your solution by modifying this code: ```python def find_unknown_number(x,y,z): ``` Your solution should implemented in the function "find_unknown_number". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence (or, shortly, an RBS) is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example: * bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"); * bracket sequences ")(", "(" and ")" are not. Let's denote the concatenation of two strings x and y as x+y. For example, "()()" + ")(" = "()())(". You are given n bracket sequences s_1, s_2, ..., s_n. You can rearrange them in any order (you can rearrange only the strings themselves, but not the characters in them). Your task is to rearrange the strings in such a way that the string s_1 + s_2 + ... + s_n has as many non-empty prefixes that are RBS as possible. Input The first line contains a single integer n (1 ≤ n ≤ 20). Then n lines follow, the i-th of them contains s_i — a bracket sequence (a string consisting of characters "(" and/or ")". All sequences s_i are non-empty, their total length does not exceed 4 ⋅ 10^5. Output Print one integer — the maximum number of non-empty prefixes that are RBS for the string s_1 + s_2 + ... + s_n, if the strings s_1, s_2, ..., s_n can be rearranged arbitrarily. Examples Input 2 ( ) Output 1 Input 4 ()()()) ( ( ) Output 4 Input 1 (()) Output 1 Input 1 )(() Output 0 Note In the first example, you can concatenate the strings as follows: "(" + ")" = "()", the resulting string will have one prefix, that is an RBS: "()". In the second example, you can concatenate the strings as follows: "(" + ")" + "()()())" + "(" = "()()()())(", the resulting string will have four prefixes that are RBS: "()", "()()", "()()()", "()()()()". The third and the fourth examples contain only one string each, so the order is fixed. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Aiz Onsen has a bathhouse and a pool. To use the bathhouse, you need to buy a bathing ticket, and to use the pool, you need to buy a pool ticket. Prices for these tickets may vary from day to day. In addition, Aiz Onsen has the following rules. * Tickets are valid only once on the day of purchase. * If you buy 5 or more bathing tickets and 2 or more pool tickets at once, you will get a 20% discount on all tickets. Sadayoshi, who loves hot springs, and his friends go to Aiz Onsen almost every day. They are all capricious and use different numbers from day to day. Aiz Onsen has discount rules, so if you work together as a group and buy well, you may be able to reduce the total price. Create a program that outputs the cheapest total price when the bathing ticket and pool ticket charges, the number of bathing tickets to be used and the number of pool tickets are given as inputs. However, if buying more tickets than you use will reduce the total price, you do not have to use all the tickets you bought. input The input is given in the following format. N x1 y1 b1 p1 x2 y2 b2 p2 :: xN yN bN pN N (1 ≤ N ≤ 365) on the first line is the number of days for which you want to calculate the charge. In the next N lines, the bathing ticket fee xi (100 ≤ xi ≤ 1000) for the i-day day, the pool ticket fee yi (100 ≤ yi ≤ 1000), the number of bathing tickets used bi (0 ≤ bi ≤ 6), The number of pool tickets to use pi (0 ≤ pi ≤ 6) is given. Both bathing tickets and pool tickets are charged in increments of 50 yen. output Print the cheapest total price on one line for each day. Example Input 2 100 100 1 1 1000 500 5 2 Output 200 4800 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.