question
large_stringlengths
265
13.2k
Solve the programming task below in a Python markdown code block. Positive integer $x$ is called divisor of positive integer $y$, if $y$ is divisible by $x$ without remainder. For example, $1$ is a divisor of $7$ and $3$ is not divisor of $8$. We gave you an integer $d$ and asked you to find the smallest positive integer $a$, such that $a$ has at least $4$ divisors; difference between any two divisors of $a$ is at least $d$. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 3000$) β€” the number of test cases. The first line of each test case contains a single integer $d$ ($1 \leq d \leq 10000$). -----Output----- For each test case print one integer $a$ β€” the answer for this test case. -----Examples----- Input 2 1 2 Output 6 15 -----Note----- In the first test case, integer $6$ have following divisors: $[1, 2, 3, 6]$. There are $4$ of them and the difference between any two of them is at least $1$. There is no smaller integer with at least $4$ divisors. In the second test case, integer $15$ have following divisors: $[1, 3, 5, 15]$. There are $4$ of them and the difference between any two of them is at least $2$. The answer $12$ is INVALID because divisors are $[1, 2, 3, 4, 6, 12]$. And the difference between, for example, divisors $2$ and $3$ is less than $d=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. For the New Year, Polycarp decided to send postcards to all his $n$ friends. He wants to make postcards with his own hands. For this purpose, he has a sheet of paper of size $w \times h$, which can be cut into pieces. Polycarp can cut any sheet of paper $w \times h$ that he has in only two cases: If $w$ is even, then he can cut the sheet in half and get two sheets of size $\frac{w}{2} \times h$; If $h$ is even, then he can cut the sheet in half and get two sheets of size $w \times \frac{h}{2}$; If $w$ and $h$ are even at the same time, then Polycarp can cut the sheet according to any of the rules above. After cutting a sheet of paper, the total number of sheets of paper is increased by $1$. Help Polycarp to find out if he can cut his sheet of size $w \times h$ at into $n$ or more pieces, using only the rules described above. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing three integers $w$, $h$, $n$ ($1 \le w, h \le 10^4, 1 \le n \le 10^9$) β€” the width and height of the sheet Polycarp has and the number of friends he needs to send a postcard to. -----Output----- For each test case, output on a separate line: "YES", if it is possible to cut a sheet of size $w \times h$ into at least $n$ pieces; "NO" otherwise. You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive). -----Examples----- Input 5 2 2 3 3 3 2 5 10 2 11 13 1 1 4 4 Output YES NO YES YES YES -----Note----- In the first test case, you can first cut the $2 \times 2$ sheet into two $2 \times 1$ sheets, and then cut each of them into two more sheets. As a result, we get four sheets $1 \times 1$. We can choose any three of them and send them to our friends. In the second test case, a $3 \times 3$ sheet cannot be cut, so it is impossible to get two sheets. In the third test case, you can cut a $5 \times 10$ sheet into two $5 \times 5$ sheets. In the fourth test case, there is no need to cut the sheet, since we only need one sheet. In the fifth test case, you can first cut the $1 \times 4$ sheet into two $1 \times 2$ sheets, and then cut each of them into two more sheets. As a result, we get four sheets $1 \times 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. There is a grid with infinitely many rows and columns. In this grid, there is a rectangular region with consecutive N rows and M columns, and a card is placed in each square in this region. The front and back sides of these cards can be distinguished, and initially every card faces up. We will perform the following operation once for each square contains a card: * For each of the following nine squares, flip the card in it if it exists: the target square itself and the eight squares that shares a corner or a side with the target square. It can be proved that, whether each card faces up or down after all the operations does not depend on the order the operations are performed. Find the number of cards that face down after all the operations. Constraints * 1 \leq N,M \leq 10^9 * All input values are integers. Input Input is given from Standard Input in the following format: N M Output Print the number of cards that face down after all the operations. Examples Input 2 2 Output 0 Input 1 7 Output 5 Input 314 1592 Output 496080 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 "Ritsumeikan University Competitive Programming Camp" will be held this year as well. I am very much looking forward to this annual training camp. However, I couldn't stand my desires and splurged before the training camp, so I couldn't afford it. So I decided to use the cheapest Seishun 18 Ticket to get to Minami Kusatsu, the nearest station to Ritsumeikan University. This ticket is cheap, but there are many transfers, and I have to spend the whole day to move, which makes me very tired. It was after I left Aizu-Wakamatsu Station that I realized that the day of such a move was Friday the 13th. I was swayed by the train for 12 hours, feeling anxious. Today is the second day of the training camp, Sunday, March 15, 2015. I arrived in Minami-Kusatsu on the 13th without any problems, but I didn't want to feel this kind of anxiety anymore. So, as a judge on the second day, I asked him to ask for the number of Friday the 13th that existed within the specified period, and asked him to create a program. The definition of the year of the stagnation is as follows. * A year in which the year is divisible by 4 is a leap year. * However, a year divisible by 100 is not a leap year. * However, a year divisible by 400 is a leap year. Constraints The input satisfies the following constraints. * 1 ≀ Y1 ≀ Y2 ≀ 1018 * 1 ≀ Mi ≀ 12 * 1 ≀ Di ≀ 31 (Mi = 1, 3, 5, 7, 8, 10, 12) * 1 ≀ Di ≀ 30 (Mi = 4, 6, 9, 11) * 1 ≀ Di ≀ 28 (Mi = 2 and Yi year is not a leap year) * 1 ≀ Di ≀ 29 (Mi = 2 and Yi year is a leap year) * Y1 year M January D1 is a date more than 0 days before Y2 year M February D2. Input Six integers Y1, M1, D1, Y2, M2, D2 separated by blanks are given in one line. Output Output the number of Friday the 13th that exists between M1 January D1 of Y1 and D2 M2 of Y2 on one line. Examples Input 2015 3 13 2015 3 13 Output 1 Input 2015 2 14 2015 3 15 Output 1 Input 1234 5 6 789012345678901234 5 6 Output 1357101234567708000 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 two sequences a and b and number p. Sequence a consists of n integers a1, a2, ..., an. Similarly, sequence b consists of m integers b1, b2, ..., bm. As usual, Sereja studies the sequences he has. Today he wants to find the number of positions q (q + (m - 1)Β·p ≀ n; q β‰₯ 1), such that sequence b can be obtained from sequence aq, aq + p, aq + 2p, ..., aq + (m - 1)p by rearranging elements. Sereja needs to rush to the gym, so he asked to find all the described positions of q. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 2Β·105, 1 ≀ p ≀ 2Β·105). The next line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109). The next line contains m integers b1, b2, ..., bm (1 ≀ bi ≀ 109). Output In the first line print the number of valid qs. In the second line, print the valid values in the increasing order. Examples Input 5 3 1 1 2 3 2 1 1 2 3 Output 2 1 3 Input 6 3 2 1 3 2 2 3 1 1 2 3 Output 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. Little Vlad is fond of popular computer game Bota-2. Recently, the developers announced the new add-on named Bota-3. Of course, Vlad immediately bought only to find out his computer is too old for the new game and needs to be updated. There are n video cards in the shop, the power of the i-th video card is equal to integer value a_{i}. As Vlad wants to be sure the new game will work he wants to buy not one, but several video cards and unite their powers using the cutting-edge technology. To use this technology one of the cards is chosen as the leading one and other video cards are attached to it as secondary. For this new technology to work it's required that the power of each of the secondary video cards is divisible by the power of the leading video card. In order to achieve that the power of any secondary video card can be reduced to any integer value less or equal than the current power. However, the power of the leading video card should remain unchanged, i.e. it can't be reduced. Vlad has an infinite amount of money so he can buy any set of video cards. Help him determine which video cards he should buy such that after picking the leading video card and may be reducing some powers of others to make them work together he will get the maximum total value of video power. -----Input----- The first line of the input contains a single integer n (1 ≀ n ≀ 200 000)Β β€” the number of video cards in the shop. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 200 000)Β β€” powers of video cards. -----Output----- The only line of the output should contain one integer valueΒ β€” the maximum possible total power of video cards working together. -----Examples----- Input 4 3 2 15 9 Output 27 Input 4 8 2 2 7 Output 18 -----Note----- In the first sample, it would be optimal to buy video cards with powers 3, 15 and 9. The video card with power 3 should be chosen as the leading one and all other video cards will be compatible with it. Thus, the total power would be 3 + 15 + 9 = 27. If he buys all the video cards and pick the one with the power 2 as the leading, the powers of all other video cards should be reduced by 1, thus the total power would be 2 + 2 + 14 + 8 = 26, that is less than 27. Please note, that it's not allowed to reduce the power of the leading video card, i.e. one can't get the total power 3 + 1 + 15 + 9 = 28. In the second sample, the optimal answer is to buy all video cards and pick the one with the power 2 as the leading. The video card with the power 7 needs it power to be reduced down to 6. The total power would be 8 + 2 + 2 + 6 = 18. 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. Gregor is learning about RSA cryptography, and although he doesn't understand how RSA works, he is now fascinated with prime numbers and factoring them. Gregor's favorite prime number is $P$. Gregor wants to find two bases of $P$. Formally, Gregor is looking for two integers $a$ and $b$ which satisfy both of the following properties. $P mod a = P mod b$, where $x mod y$ denotes the remainder when $x$ is divided by $y$, and $2 \le a < b \le P$. Help Gregor find two bases of his favorite prime number! -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). Each subsequent line contains the integer $P$ ($5 \le P \le {10}^9$), with $P$ guaranteed to be prime. -----Output----- Your output should consist of $t$ lines. Each line should consist of two integers $a$ and $b$ ($2 \le a < b \le P$). If there are multiple possible solutions, print any. -----Examples----- Input 2 17 5 Output 3 5 2 4 -----Note----- The first query is $P=17$. $a=3$ and $b=5$ are valid bases in this case, because $17 mod 3 = 17 mod 5 = 2$. There are other pairs which work as well. In the second query, with $P=5$, the only solution is $a=2$ and $b=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. This is a hard version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $s$ consisting of $n$ lowercase Latin letters. You have to color all its characters the minimum number of colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to find the minimum number of colors which you have to color the given string in so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the length of $s$. The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters. -----Output----- In the first line print one integer $res$ ($1 \le res \le n$) β€” the minimum number of colors in which you have to color the given string so that after coloring it can become sorted by some sequence of swaps. In the second line print any possible coloring that can be used to sort the string using some sequence of swaps described in the problem statement. The coloring is the array $c$ of length $n$, where $1 \le c_i \le res$ and $c_i$ means the color of the $i$-th character. -----Examples----- Input 9 abacbecfd Output 2 1 1 2 1 2 1 2 1 2 Input 8 aaabbcbb Output 2 1 2 1 2 1 2 1 1 Input 7 abcdedc Output 3 1 1 1 1 1 2 3 Input 5 abcde Output 1 1 1 1 1 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. Bleatrix Trotter the sheep has devised a strategy that helps her fall asleep faster. First, she picks a number N. Then she starts naming N, 2 Γ— N, 3 Γ— N, and so on. Whenever she names a number, she thinks about all of the digits in that number. She keeps track of which digits (0, 1, 2, 3, 4, 5, 6, 7, 8, and 9) she has seen at least once so far as part of any number she has named. Once she has seen each of the ten digits at least once, she will fall asleep. Bleatrix must start with N and must always name (i + 1) Γ— N directly after i Γ— N. For example, suppose that Bleatrix picks N = 1692. She would count as follows: N = 1692. Now she has seen the digits 1, 2, 6, and 9. 2N = 3384. Now she has seen the digits 1, 2, 3, 4, 6, 8, and 9. 3N = 5076. Now she has seen all ten digits, and falls asleep. The purpose of this kata is to return the last number Bleatrix Trotter sees before falling asleep. Input Will always be positive integer or zero Output The last number Bleatrix Trotter sees or "INSOMNIA" (-1 in Rust and C++) if she will count forever Please note, this challenge is not my idea. It's from Google Code Jam 2016 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. Let's define a split of $n$ as a nonincreasing sequence of positive integers, the sum of which is $n$. For example, the following sequences are splits of $8$: $[4, 4]$, $[3, 3, 2]$, $[2, 2, 1, 1, 1, 1]$, $[5, 2, 1]$. The following sequences aren't splits of $8$: $[1, 7]$, $[5, 4]$, $[11, -3]$, $[1, 1, 4, 1, 1]$. The weight of a split is the number of elements in the split that are equal to the first element. For example, the weight of the split $[1, 1, 1, 1, 1]$ is $5$, the weight of the split $[5, 5, 3, 3, 3]$ is $2$ and the weight of the split $[9]$ equals $1$. For a given $n$, find out the number of different weights of its splits. -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^9$). -----Output----- Output one integerΒ β€” the answer to the problem. -----Examples----- Input 7 Output 4 Input 8 Output 5 Input 9 Output 5 -----Note----- In the first sample, there are following possible weights of splits of $7$: Weight 1: [$\textbf 7$] Weight 2: [$\textbf 3$, $\textbf 3$, 1] Weight 3: [$\textbf 2$, $\textbf 2$, $\textbf 2$, 1] Weight 7: [$\textbf 1$, $\textbf 1$, $\textbf 1$, $\textbf 1$, $\textbf 1$, $\textbf 1$, $\textbf 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. In this Kata, you will be given two numbers, n and k and your task will be to return the k-digit array that sums to n and has the maximum possible GCD. For example, given `n = 12, k = 3`, there are a number of possible `3-digit` arrays that sum to `12`, such as `[1,2,9], [2,3,7], [2,4,6], ...` and so on. Of all the possibilities, the one with the highest GCD is `[2,4,6]`. Therefore, `solve(12,3) = [2,4,6]`. Note also that digits cannot be repeated within the sub-array, so `[1,1,10]` is not a possibility. Lastly, if there is no such array, return an empty array. More examples in the test cases. Good luck! 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 two indistinguishable pieces placed on a number line. Both pieces are initially at coordinate 0. (They can occupy the same position.) We can do the following two kinds of operations: * Choose a piece and move it to the right (the positive direction) by 1. * Move the piece with the smaller coordinate to the position of the piece with the greater coordinate. If two pieces already occupy the same position, nothing happens, but it still counts as doing one operation. We want to do a total of N operations of these kinds in some order so that one of the pieces will be at coordinate A and the other at coordinate B. Find the number of ways to move the pieces to achieve it. The answer can be enormous, so compute the count modulo 998244353. Two ways to move the pieces are considered different if and only if there exists an integer i (1 \leq i \leq N) such that the set of the coordinates occupied by the pieces after the i-th operation is different in those two ways. Constraints * 1 \leq N \leq 10^7 * 0 \leq A \leq B \leq N * All values in input are integers. Input Input is given from Standard Input in the following format: N A B Output Print the number of ways to move the pieces to achieve our objective, modulo 998244353. Examples Input 5 1 3 Output 4 Input 10 0 0 Output 1 Input 10 4 6 Output 197 Input 1000000 100000 200000 Output 758840509 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 Anton found a box with digits in his room. There are k_2 digits 2, k_3 digits 3, k_5 digits 5 and k_6 digits 6. Anton's favorite integers are 32 and 256. He decided to compose this integers from digits he has. He wants to make the sum of these integers as large as possible. Help him solve this task! Each digit can be used no more than once, i.e. the composed integers should contain no more than k_2 digits 2, k_3 digits 3 and so on. Of course, unused digits are not counted in the sum. -----Input----- The only line of the input contains four integers k_2, k_3, k_5 and k_6Β β€” the number of digits 2, 3, 5 and 6 respectively (0 ≀ k_2, k_3, k_5, k_6 ≀ 5Β·10^6). -----Output----- Print one integerΒ β€” maximum possible sum of Anton's favorite integers that can be composed using digits from the box. -----Examples----- Input 5 1 3 4 Output 800 Input 1 1 1 1 Output 256 -----Note----- In the first sample, there are five digits 2, one digit 3, three digits 5 and four digits 6. Anton can compose three integers 256 and one integer 32 to achieve the value 256 + 256 + 256 + 32 = 800. Note, that there is one unused integer 2 and one unused integer 6. They are not counted in the answer. In the second sample, the optimal answer is to create on integer 256, thus the answer is 256. 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 has interviewed Oleg and has written the interview down without punctuation marks and spaces to save time. Thus, the interview is now a string s consisting of n lowercase English letters. There is a filler word ogo in Oleg's speech. All words that can be obtained from ogo by adding go several times to the end of it are also considered to be fillers. For example, the words ogo, ogogo, ogogogo are fillers, but the words go, og, ogog, ogogog and oggo are not fillers. The fillers have maximal size, for example, for ogogoo speech we can't consider ogo a filler and goo as a normal phrase. We should consider ogogo as a filler here. To print the interview, Polycarp has to replace each of the fillers with three asterisks. Note that a filler word is replaced with exactly three asterisks regardless of its length. Polycarp has dealt with this problem in no time. Can you do the same? The clock is ticking! -----Input----- The first line contains a positive integer n (1 ≀ n ≀ 100)Β β€” the length of the interview. The second line contains the string s of length n, consisting of lowercase English letters. -----Output----- Print the interview text after the replacement of each of the fillers with "***". It is allowed for the substring "***" to have several consecutive occurences. -----Examples----- Input 7 aogogob Output a***b Input 13 ogogmgogogogo Output ***gmg*** Input 9 ogoogoogo Output ********* -----Note----- The first sample contains one filler word ogogo, so the interview for printing is "a***b". The second sample contains two fillers ogo and ogogogo. Thus, the interview is transformed to "***gmg***". 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 Takahashi Kingdom, there is a east-west railroad and N cities along it, numbered 1, 2, 3, ..., N from west to east. A company called AtCoder Express possesses M trains, and the train i runs from City L_i to City R_i (it is possible that L_i = R_i). Takahashi the king is interested in the following Q matters: - The number of the trains that runs strictly within the section from City p_i to City q_i, that is, the number of trains j such that p_i \leq L_j and R_j \leq q_i. Although he is genius, this is too much data to process by himself. Find the answer for each of these Q queries to help him. -----Constraints----- - N is an integer between 1 and 500 (inclusive). - M is an integer between 1 and 200 \ 000 (inclusive). - Q is an integer between 1 and 100 \ 000 (inclusive). - 1 \leq L_i \leq R_i \leq N (1 \leq i \leq M) - 1 \leq p_i \leq q_i \leq N (1 \leq i \leq Q) -----Input----- Input is given from Standard Input in the following format: N M Q L_1 R_1 L_2 R_2 : L_M R_M p_1 q_1 p_2 q_2 : p_Q q_Q -----Output----- Print Q lines. The i-th line should contain the number of the trains that runs strictly within the section from City p_i to City q_i. -----Sample Input----- 2 3 1 1 1 1 2 2 2 1 2 -----Sample Output----- 3 As all the trains runs within the section from City 1 to City 2, the answer to the only query is 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. International Abbreviation Olympiad takes place annually starting from 1989. Each year the competition receives an abbreviation of form IAO'y, where y stands for some number of consequent last digits of the current year. Organizers always pick an abbreviation with non-empty string y that has never been used before. Among all such valid abbreviations they choose the shortest one and announce it to be the abbreviation of this year's competition. For example, the first three Olympiads (years 1989, 1990 and 1991, respectively) received the abbreviations IAO'9, IAO'0 and IAO'1, while the competition in 2015 received an abbreviation IAO'15, as IAO'5 has been already used in 1995. You are given a list of abbreviations. For each of them determine the year it stands for. -----Input----- The first line of the input contains a single integer n (1 ≀ n ≀ 1000)Β β€” the number of abbreviations to process. Then n lines follow, each containing a single abbreviation. It's guaranteed that each abbreviation contains at most nine digits. -----Output----- For each abbreviation given in the input, find the year of the corresponding Olympiad. -----Examples----- Input 5 IAO'15 IAO'2015 IAO'1 IAO'9 IAO'0 Output 2015 12015 1991 1989 1990 Input 4 IAO'9 IAO'99 IAO'999 IAO'9999 Output 1989 1999 2999 9999 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. Andrew skipped lessons on the subject 'Algorithms and Data Structures' for the entire term. When he came to the final test, the teacher decided to give him a difficult task as a punishment. The teacher gave Andrew an array of n numbers a_1, ..., a_{n}. After that he asked Andrew for each k from 1 to n - 1 to build a k-ary heap on the array and count the number of elements for which the property of the minimum-rooted heap is violated, i.e. the value of an element is less than the value of its parent. Andrew looked up on the Wikipedia that a k-ary heap is a rooted tree with vertices in elements of the array. If the elements of the array are indexed from 1 to n, then the children of element v are elements with indices k(v - 1) + 2, ..., kv + 1 (if some of these elements lie outside the borders of the array, the corresponding children are absent). In any k-ary heap every element except for the first one has exactly one parent; for the element 1 the parent is absent (this element is the root of the heap). Denote p(v) as the number of the parent of the element with the number v. Let's say that for a non-root element v the property of the heap is violated if a_{v} < a_{p}(v). Help Andrew cope with the task! -----Input----- The first line contains a single integer n (2 ≀ n ≀ 2Β·10^5). The second line contains n space-separated integers a_1, ..., a_{n} ( - 10^9 ≀ a_{i} ≀ 10^9). -----Output----- in a single line print n - 1 integers, separate the consecutive numbers with a single space β€” the number of elements for which the property of the k-ary heap is violated, for k = 1, 2, ..., n - 1. -----Examples----- Input 5 1 5 4 3 2 Output 3 2 1 0 Input 6 2 2 2 2 2 2 Output 0 0 0 0 0 -----Note----- Pictures with the heaps for the first sample are given below; elements for which the property of the heap is violated are marked with red. [Image] [Image] $\therefore$ [Image] In the second sample all elements are equal, so the property holds for all pairs. 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 two boards, each divided into a grid with N rows and N columns. For both of these boards, the square at the i-th row from the top and the j-th column from the left is called Square (i,j). There is a lowercase English letter written in each square on the first board. The letter written in Square (i,j) is S_{i,j}. On the second board, nothing is written yet. Snuke will write letters on the second board, as follows: * First, choose two integers A and B ( 0 \leq A, B < N ). * Write one letter in each square on the second board. Specifically, write the letter written in Square ( i+A, j+B ) on the first board into Square (i,j) on the second board. Here, the k-th row is also represented as the (N+k)-th row, and the k-th column is also represented as the (N+k)-th column. After this operation, the second board is called a good board when, for every i and j ( 1 \leq i, j \leq N ), the letter in Square (i,j) and the letter in Square (j,i) are equal. Find the number of the ways to choose integers A and B ( 0 \leq A, B < N ) such that the second board is a good board. Constraints * 1 \leq N \leq 300 * S_{i,j} ( 1 \leq i, j \leq N ) is a lowercase English letter. Input Input is given from Standard Input in the following format: N S_{1,1}S_{1,2}..S_{1,N} S_{2,1}S_{2,2}..S_{2,N} : S_{N,1}S_{N,2}..S_{N,N} Output Print the number of the ways to choose integers A and B ( 0 \leq A, B < N ) such that the second board is a good board. Examples Input 2 ab ca Output 2 Input 4 aaaa aaaa aaaa aaaa Output 16 Input 5 abcde fghij klmno pqrst uvwxy 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. In mathematics, a **pandigital number** is a number that in a given base has among its significant digits each digit used in the base at least once. For example, 1234567890 is a pandigital number in base 10. For simplification, in this kata, we will consider pandigital numbers in *base 10* and with all digits used *exactly once*. The challenge is to calculate a sorted sequence of pandigital numbers, starting at a certain `offset` and with a specified `size`. Example: ```python > get_sequence(0, 5) [1023456789, 1023456798, 1023456879, 1023456897, 1023456978] ``` Rules: - We are looking for positive pandigital numbers in base 10. - Each digit should occur `exactly once`. - A pandigital number can't start with digit zero. - The offset is an integer (negative, zero or positive number) (long in Java) - The size is a positive integer number (int in Java) - Return the `size` pandigital numbers which are not smaller than the `offset`. If there is not enough `size` pandigital numbers, just return all of them. - Return an empty array if nothing is found. 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 card game called "Durak", which means "Fool" in Russian. The game is quite popular in the countries that used to form USSR. The problem does not state all the game's rules explicitly β€” you can find them later yourselves if you want. To play durak you need a pack of 36 cards. Each card has a suit ("S", "H", "D" and "C") and a rank (in the increasing order "6", "7", "8", "9", "T", "J", "Q", "K" and "A"). At the beginning of the game one suit is arbitrarily chosen as trump. The players move like that: one player puts one or several of his cards on the table and the other one should beat each of them with his cards. A card beats another one if both cards have similar suits and the first card has a higher rank then the second one. Besides, a trump card can beat any non-trump card whatever the cards’ ranks are. In all other cases you can not beat the second card with the first one. You are given the trump suit and two different cards. Determine whether the first one beats the second one or not. Input The first line contains the tramp suit. It is "S", "H", "D" or "C". The second line contains the description of the two different cards. Each card is described by one word consisting of two symbols. The first symbol stands for the rank ("6", "7", "8", "9", "T", "J", "Q", "K" and "A"), and the second one stands for the suit ("S", "H", "D" and "C"). Output Print "YES" (without the quotes) if the first cards beats the second one. Otherwise, print "NO" (also without the quotes). Examples Input H QH 9S Output YES Input S 8D 6D Output YES Input C 7H AS 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. Write a program to check whether a triangle is valid or not, when the three angles of the triangle are the inputs. A triangle is valid if the sum of all the three angles is equal to 180 degrees. -----Input----- The first line contains an integer T, the total number of testcases. Then T lines follow, each line contains three angles A, B and C, of the triangle separated by space. -----Output----- For each test case, display 'YES' if the triangle is valid, and 'NO', if it is not, in a new line. -----Constraints----- - 1 ≀ T ≀ 1000 - 1 ≀ A,B,C ≀ 180 -----Example----- Input 3 40 40 100 45 45 90 180 1 1 Output YES YES 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. Boboniu likes bit operations. He wants to play a game with you. Boboniu gives you two sequences of non-negative integers $a_1,a_2,\ldots,a_n$ and $b_1,b_2,\ldots,b_m$. For each $i$ ($1\le i\le n$), you're asked to choose a $j$ ($1\le j\le m$) and let $c_i=a_i\& b_j$, where $\&$ denotes the bitwise AND operation. Note that you can pick the same $j$ for different $i$'s. Find the minimum possible $c_1 | c_2 | \ldots | c_n$, where $|$ denotes the bitwise OR operation. -----Input----- The first line contains two integers $n$ and $m$ ($1\le n,m\le 200$). The next line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($0\le a_i < 2^9$). The next line contains $m$ integers $b_1,b_2,\ldots,b_m$ ($0\le b_i < 2^9$). -----Output----- Print one integer: the minimum possible $c_1 | c_2 | \ldots | c_n$. -----Examples----- Input 4 2 2 6 4 0 2 4 Output 2 Input 7 6 1 9 1 9 8 1 0 1 1 4 5 1 4 Output 0 Input 8 5 179 261 432 162 82 43 10 38 379 357 202 184 197 Output 147 -----Note----- For the first example, we have $c_1=a_1\& b_2=0$, $c_2=a_2\& b_1=2$, $c_3=a_3\& b_1=0$, $c_4 = a_4\& b_1=0$.Thus $c_1 | c_2 | c_3 |c_4 =2$, and this is the minimal answer we can get. 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 four integers A, B, C, and D. Find the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D. -----Constraints----- - 1\leq A\leq B\leq 10^{18} - 1\leq C,D\leq 10^9 - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: A B C D -----Output----- Print the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D. -----Sample Input----- 4 9 2 3 -----Sample Output----- 2 5 and 7 satisfy the condition. 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. Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't). q events are about to happen (in chronological order). They are of three types: 1. Application x generates a notification (this new notification is unread). 2. Thor reads all notifications generated so far by application x (he may re-read some notifications). 3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation. Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone. Input The first line of input contains two integers n and q (1 ≀ n, q ≀ 300 000) β€” the number of applications and the number of events to happen. The next q lines contain the events. The i-th of these lines starts with an integer typei β€” type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≀ typei ≀ 3, 1 ≀ xi ≀ n, 1 ≀ ti ≀ q). Output Print the number of unread notifications after each event. Examples Input 3 4 1 3 1 1 1 2 2 3 Output 1 2 3 2 Input 4 6 1 2 1 4 1 2 3 3 1 3 1 3 Output 1 2 3 0 1 2 Note In the first sample: 1. Application 3 generates a notification (there is 1 unread notification). 2. Application 1 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads the notification generated by application 3, there are 2 unread notifications left. In the second sample test: 1. Application 2 generates a notification (there is 1 unread notification). 2. Application 4 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left. 5. Application 3 generates a notification (there is 1 unread notification). 6. Application 3 generates a notification (there are 2 unread notifications). 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. Berland State University has received a new update for the operating system. Initially it is installed only on the $1$-st computer. Update files should be copied to all $n$ computers. The computers are not connected to the internet, so the only way to transfer update files from one computer to another is to copy them using a patch cable (a cable connecting two computers directly). Only one patch cable can be connected to a computer at a time. Thus, from any computer where the update files are installed, they can be copied to some other computer in exactly one hour. Your task is to find the minimum number of hours required to copy the update files to all $n$ computers if there are only $k$ patch cables in Berland State University. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^5$) β€” the number of test cases. Each test case consists of a single line that contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$) β€” the number of computers and the number of patch cables. -----Output----- For each test case print one integer β€” the minimum number of hours required to copy the update files to all $n$ computers. -----Examples----- Input 4 8 3 6 6 7 1 1 1 Output 4 3 6 0 -----Note----- Let's consider the test cases of the example: $n=8$, $k=3$: during the first hour, we copy the update files from the computer $1$ to the computer $2$; during the second hour, we copy the update files from the computer $1$ to the computer $3$, and from the computer $2$ to the computer $4$; during the third hour, we copy the update files from the computer $1$ to the computer $5$, from the computer $2$ to the computer $6$, and from the computer $3$ to the computer $7$; during the fourth hour, we copy the update files from the computer $2$ to the computer $8$. $n=6$, $k=6$: during the first hour, we copy the update files from the computer $1$ to the computer $2$; during the second hour, we copy the update files from the computer $1$ to the computer $3$, and from the computer $2$ to the computer $4$; during the third hour, we copy the update files from the computer $1$ to the computer $5$, and from the computer $2$ to the computer $6$. $n=7$, $k=1$: during the first hour, we copy the update files from the computer $1$ to the computer $2$; during the second hour, we copy the update files from the computer $1$ to the computer $3$; during the third hour, we copy the update files from the computer $1$ to the computer $4$; during the fourth hour, we copy the update files from the computer $4$ to the computer $5$; during the fifth hour, we copy the update files from the computer $4$ to the computer $6$; during the sixth hour, we copy the update files from the computer $3$ to the computer $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. Long long ago, there was a thief. Looking for treasures, he was running about all over the world. One day, he heard a rumor that there were islands that had large amount of treasures, so he decided to head for there. Finally he found n islands that had treasures and one island that had nothing. Most of islands had seashore and he can land only on an island which had nothing. He walked around the island and found that there was an old bridge between this island and each of all other n islands. He tries to visit all islands one by one and pick all the treasures up. Since he is afraid to be stolen, he visits with bringing all treasures that he has picked up. He is a strong man and can bring all the treasures at a time, but the old bridges will break if he cross it with taking certain or more amount of treasures. Please write a program that judges if he can collect all the treasures and can be back to the island where he land on by properly selecting an order of his visit. Constraints * 1 ≀ n ≀ 25 Input Input consists of several datasets. The first line of each dataset contains an integer n. Next n lines represents information of the islands. Each line has two integers, which means the amount of treasures of the island and the maximal amount that he can take when he crosses the bridge to the islands, respectively. The end of input is represented by a case with n = 0. Output For each dataset, if he can collect all the treasures and can be back, print "Yes" Otherwise print "No" Example Input 3 2 3 3 6 1 2 3 2 3 3 5 1 2 0 Output Yes 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. You are given two arrays $a$ and $b$ of positive integers, with length $n$ and $m$ respectively. Let $c$ be an $n \times m$ matrix, where $c_{i,j} = a_i \cdot b_j$. You need to find a subrectangle of the matrix $c$ such that the sum of its elements is at most $x$, and its area (the total number of elements) is the largest possible. Formally, you need to find the largest number $s$ such that it is possible to choose integers $x_1, x_2, y_1, y_2$ subject to $1 \leq x_1 \leq x_2 \leq n$, $1 \leq y_1 \leq y_2 \leq m$, $(x_2 - x_1 + 1) \times (y_2 - y_1 + 1) = s$, and $$\sum_{i=x_1}^{x_2}{\sum_{j=y_1}^{y_2}{c_{i,j}}} \leq x.$$ -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 2000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 2000$). The third line contains $m$ integers $b_1, b_2, \ldots, b_m$ ($1 \leq b_i \leq 2000$). The fourth line contains a single integer $x$ ($1 \leq x \leq 2 \cdot 10^{9}$). -----Output----- If it is possible to choose four integers $x_1, x_2, y_1, y_2$ such that $1 \leq x_1 \leq x_2 \leq n$, $1 \leq y_1 \leq y_2 \leq m$, and $\sum_{i=x_1}^{x_2}{\sum_{j=y_1}^{y_2}{c_{i,j}}} \leq x$, output the largest value of $(x_2 - x_1 + 1) \times (y_2 - y_1 + 1)$ among all such quadruplets, otherwise output $0$. -----Examples----- Input 3 3 1 2 3 1 2 3 9 Output 4 Input 5 1 5 4 2 4 5 2 5 Output 1 -----Note----- Matrix from the first sample and the chosen subrectangle (of blue color): [Image] Matrix from the second sample and the chosen subrectangle (of blue color): $\left. \begin{array}{l l l l l}{10} & {8} & {4} & {8} & {10} \end{array} \right.$ 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 stardate is 1977 and the science and art of detecting Death Stars is in its infancy. Princess Heidi has received information about the stars in the nearby solar system from the Rebel spies and now, to help her identify the exact location of the Death Star, she needs to know whether this information is correct. Two rebel spies have provided her with the maps of the solar system. Each map is an N Γ— N grid, where each cell is either occupied by a star or empty. To see whether the information is correct, Heidi needs to know whether the two maps are of the same solar system, or if possibly one of the spies is actually an Empire double agent, feeding her false information. Unfortunately, spies may have accidentally rotated a map by 90, 180, or 270 degrees, or flipped it along the vertical or the horizontal axis, before delivering it to Heidi. If Heidi can rotate or flip the maps so that two of them become identical, then those maps are of the same solar system. Otherwise, there are traitors in the Rebel ranks! Help Heidi find out. -----Input----- The first line of the input contains one number N (1 ≀ N ≀ 10) – the dimension of each map. Next N lines each contain N characters, depicting the first map: 'X' indicates a star, while 'O' indicates an empty quadrant of space. Next N lines each contain N characters, depicting the second map in the same format. -----Output----- The only line of output should contain the word Yes if the maps are identical, or No if it is impossible to match them by performing rotations and translations. -----Examples----- Input 4 XOOO XXOO OOOO XXXX XOOO XOOO XOXO XOXX Output Yes Input 2 XX OO XO OX Output No -----Note----- In the first test, you can match the first map to the second map by first flipping the first map along the vertical axis, and then by rotating it 90 degrees clockwise. 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 lives in a round building, whose entrances are numbered sequentially by integers from 1 to n. Entrance n and entrance 1 are adjacent. Today Vasya got bored and decided to take a walk in the yard. Vasya lives in entrance a and he decided that during his walk he will move around the house b entrances in the direction of increasing numbers (in this order entrance n should be followed by entrance 1). The negative value of b corresponds to moving |b| entrances in the order of decreasing numbers (in this order entrance 1 is followed by entrance n). If b = 0, then Vasya prefers to walk beside his entrance. [Image] Illustration for n = 6, a = 2, b = - 5. Help Vasya to determine the number of the entrance, near which he will be at the end of his walk. -----Input----- The single line of the input contains three space-separated integers n, a and b (1 ≀ n ≀ 100, 1 ≀ a ≀ n, - 100 ≀ b ≀ 100)Β β€” the number of entrances at Vasya's place, the number of his entrance and the length of his walk, respectively. -----Output----- Print a single integer k (1 ≀ k ≀ n)Β β€” the number of the entrance where Vasya will be at the end of his walk. -----Examples----- Input 6 2 -5 Output 3 Input 5 1 3 Output 4 Input 3 2 7 Output 3 -----Note----- The first example is illustrated by the picture in the statements. 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. Two deer, AtCoDeer and TopCoDeer, are playing a game called Honest or Dishonest. In this game, an honest player always tells the truth, and an dishonest player always tell lies. You are given two characters a and b as the input. Each of them is either H or D, and carries the following information: If a=H, AtCoDeer is honest; if a=D, AtCoDeer is dishonest. If b=H, AtCoDeer is saying that TopCoDeer is honest; if b=D, AtCoDeer is saying that TopCoDeer is dishonest. Given this information, determine whether TopCoDeer is honest. -----Constraints----- - a=H or a=D. - b=H or b=D. -----Input----- The input is given from Standard Input in the following format: a b -----Output----- If TopCoDeer is honest, print H. If he is dishonest, print D. -----Sample Input----- H H -----Sample Output----- H In this input, AtCoDeer is honest. Hence, as he says, TopCoDeer is honest. 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. If we write out the digits of "60" as English words we get "sixzero"; the number of letters in "sixzero" is seven. The number of letters in "seven" is five. The number of letters in "five" is four. The number of letters in "four" is four: we have reached a stable equilibrium. Note: for integers larger than 9, write out the names of each digit in a single word (instead of the proper name of the number in English). For example, write 12 as "onetwo" (instead of twelve), and 999 as "nineninenine" (instead of nine hundred and ninety-nine). For any integer between 0 and 999, return an array showing the path from that integer to a stable equilibrium: 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 N non-negative integers A_1, A_2, ..., A_N and another non-negative integer K. For a integer X between 0 and K (inclusive), let f(X) = (X XOR A_1) + (X XOR A_2) + ... + (X XOR A_N). Here, for non-negative integers a and b, a XOR b denotes the bitwise exclusive OR of a and b. Find the maximum value of f. What is XOR? The bitwise exclusive OR of a and b, X, is defined as follows: - When X is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if, when written in base two, exactly one of A and B has 1 in the 2^k's place, and 0 otherwise. For example, 3 XOR 5 = 6. (When written in base two: 011 XOR 101 = 110.) -----Constraints----- - All values in input are integers. - 1 \leq N \leq 10^5 - 0 \leq K \leq 10^{12} - 0 \leq A_i \leq 10^{12} -----Input----- Input is given from Standard Input in the following format: N K A_1 A_2 ... A_N -----Output----- Print the maximum value of f. -----Sample Input----- 3 7 1 6 3 -----Sample Output----- 14 The maximum value is: f(4) = (4 XOR 1) + (4 XOR 6) + (4 XOR 3) = 5 + 2 + 7 = 14. 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 an array $a$ consisting of $n$ integers $a_1, a_2, \dots , a_n$. In one operation you can choose two elements of the array and replace them with the element equal to their sum (it does not matter where you insert the new element). For example, from the array $[2, 1, 4]$ you can obtain the following arrays: $[3, 4]$, $[1, 6]$ and $[2, 5]$. Your task is to find the maximum possible number of elements divisible by $3$ that are in the array after performing this operation an arbitrary (possibly, zero) number of times. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The first line of each query contains one integer $n$ ($1 \le n \le 100$). The second line of each query contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$). -----Output----- For each query print one integer in a single line β€” the maximum possible number of elements divisible by $3$ that are in the array after performing described operation an arbitrary (possibly, zero) number of times. -----Example----- Input 2 5 3 1 2 3 1 7 1 1 1 1 1 2 2 Output 3 3 -----Note----- In the first query of the example you can apply the following sequence of operations to obtain $3$ elements divisible by $3$: $[3, 1, 2, 3, 1] \rightarrow [3, 3, 3, 1]$. In the second query you can obtain $3$ elements divisible by $3$ with the following sequence of operations: $[1, 1, 1, 1, 1, 2, 2] \rightarrow [1, 1, 1, 1, 2, 3] \rightarrow [1, 1, 1, 3, 3] \rightarrow [2, 1, 3, 3] \rightarrow [3, 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. Two players play a simple game. Each player is provided with a box with balls. First player's box contains exactly n_1 balls and second player's box contains exactly n_2 balls. In one move first player can take from 1 to k_1 balls from his box and throw them away. Similarly, the second player can take from 1 to k_2 balls from his box in his move. Players alternate turns and the first player starts the game. The one who can't make a move loses. Your task is to determine who wins if both players play optimally. -----Input----- The first line contains four integers n_1, n_2, k_1, k_2. All numbers in the input are from 1 to 50. This problem doesn't have subproblems. You will get 3 points for the correct submission. -----Output----- Output "First" if the first player wins and "Second" otherwise. -----Examples----- Input 2 2 1 2 Output Second Input 2 1 1 1 Output First -----Note----- Consider the first sample test. Each player has a box with 2 balls. The first player draws a single ball from his box in one move and the second player can either take 1 or 2 balls from his box in one move. No matter how the first player acts, the second player can always win if he plays wisely. 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 robot in a warehouse and $n$ packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point $(0, 0)$. The $i$-th package is at the point $(x_i, y_i)$. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $(0, 0)$ doesn't contain a package. The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point $(x, y)$ to the point ($x + 1, y$) or to the point $(x, y + 1)$. As we say above, the robot wants to collect all $n$ packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path. The string $s$ of length $n$ is lexicographically less than the string $t$ of length $n$ if there is some index $1 \le j \le n$ that for all $i$ from $1$ to $j-1$ $s_i = t_i$ and $s_j < t_j$. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 100$) β€” the number of test cases. Then test cases follow. The first line of a test case contains one integer $n$ ($1 \le n \le 1000$) β€” the number of packages. The next $n$ lines contain descriptions of packages. The $i$-th package is given as two integers $x_i$ and $y_i$ ($0 \le x_i, y_i \le 1000$) β€” the $x$-coordinate of the package and the $y$-coordinate of the package. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $(0, 0)$ doesn't contain a package. The sum of all values $n$ over test cases in the test doesn't exceed $1000$. -----Output----- Print the answer for each test case. If it is impossible to collect all $n$ packages in some order starting from ($0,0$), print "NO" on the first line. Otherwise, print "YES" in the first line. Then print the shortest path β€” a string consisting of characters 'R' and 'U'. Among all such paths choose the lexicographically smallest path. Note that in this problem "YES" and "NO" can be only uppercase words, i.e. "Yes", "no" and "YeS" are not acceptable. -----Example----- Input 3 5 1 3 1 2 3 3 5 5 4 3 2 1 0 0 1 1 4 3 Output YES RUUURRRRUU NO YES RRRRUUU -----Note----- For the first test case in the example the optimal path RUUURRRRUU is shown below: [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. On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs. The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$. More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$ For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$ A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array). A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not. -----Input----- The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β€” the number of test cases. The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β€” the length of the array. The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β€” the elements of the array. It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$. -----Output----- For each test case, print a binary string of length $n$. The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise. -----Examples----- Input 5 5 1 5 3 4 2 4 1 3 2 1 5 1 3 3 3 2 10 1 2 3 4 5 6 7 8 9 10 3 3 3 2 Output 10111 0001 00111 1111111111 000 -----Note----- In the first test case, $a=[1, 5, 3, 4, 2]$. The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation. The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice. The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation. The $4$-compression of $a$ is $[1, 2]$ and it is a permutation. The $5$-compression of $a$ is $[1]$ and it is a permutation. 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. ATMs of a well-known bank of a small country are arranged so that they can not give any amount of money requested by the user. Due to the limited size of the bill dispenser (the device that is directly giving money from an ATM) and some peculiarities of the ATM structure, you can get at most k bills from it, and the bills may be of at most two distinct denominations. For example, if a country uses bills with denominations 10, 50, 100, 500, 1000 and 5000 burles, then at k = 20 such ATM can give sums 100 000 burles and 96 000 burles, but it cannot give sums 99 000 and 101 000 burles. Let's suppose that the country uses bills of n distinct denominations, and the ATM that you are using has an unlimited number of bills of each type. You know that during the day you will need to withdraw a certain amount of cash q times. You know that when the ATM has multiple ways to give money, it chooses the one which requires the minimum number of bills, or displays an error message if it cannot be done. Determine the result of each of the q of requests for cash withdrawal. Input The first line contains two integers n, k (1 ≀ n ≀ 5000, 1 ≀ k ≀ 20). The next line contains n space-separated integers ai (1 ≀ ai ≀ 107) β€” the denominations of the bills that are used in the country. Numbers ai follow in the strictly increasing order. The next line contains integer q (1 ≀ q ≀ 20) β€” the number of requests for cash withdrawal that you will make. The next q lines contain numbers xi (1 ≀ xi ≀ 2Β·108) β€” the sums of money in burles that you are going to withdraw from the ATM. Output For each request for cash withdrawal print on a single line the minimum number of bills it can be done, or print - 1, if it is impossible to get the corresponding sum. Examples Input 6 20 10 50 100 500 1000 5000 8 4200 100000 95000 96000 99000 10100 2015 9950 Output 6 20 19 20 -1 3 -1 -1 Input 5 2 1 2 3 5 8 8 1 3 5 7 9 11 13 15 Output 1 1 1 2 2 2 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. Is it rated? Here it is. The Ultimate Question of Competitive Programming, Codeforces, and Everything. And you are here to answer it. Another Codeforces round has been conducted. No two participants have the same number of points. For each participant, from the top to the bottom of the standings, their rating before and after the round is known. It's known that if at least one participant's rating has changed, then the round was rated for sure. It's also known that if the round was rated and a participant with lower rating took a better place in the standings than a participant with higher rating, then at least one round participant's rating has changed. In this problem, you should not make any other assumptions about the rating system. Determine if the current round is rated, unrated, or it's impossible to determine whether it is rated of not. -----Input----- The first line contains a single integer n (2 ≀ n ≀ 1000)Β β€” the number of round participants. Each of the next n lines contains two integers a_{i} and b_{i} (1 ≀ a_{i}, b_{i} ≀ 4126)Β β€” the rating of the i-th participant before and after the round, respectively. The participants are listed in order from the top to the bottom of the standings. -----Output----- If the round is rated for sure, print "rated". If the round is unrated for sure, print "unrated". If it's impossible to determine whether the round is rated or not, print "maybe". -----Examples----- Input 6 3060 3060 2194 2194 2876 2903 2624 2624 3007 2991 2884 2884 Output rated Input 4 1500 1500 1300 1300 1200 1200 1400 1400 Output unrated Input 5 3123 3123 2777 2777 2246 2246 2246 2246 1699 1699 Output maybe -----Note----- In the first example, the ratings of the participants in the third and fifth places have changed, therefore, the round was rated. In the second example, no one's rating has changed, but the participant in the second place has lower rating than the participant in the fourth place. Therefore, if the round was rated, someone's rating would've changed for sure. In the third example, no one's rating has changed, and the participants took places in non-increasing order of their rating. Therefore, it's impossible to determine whether the round is rated or not. 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. ##Overview Write a helper function that takes in a Time object and converts it to a more human-readable format. You need only go up to '_ weeks ago'. ```python to_pretty(0) => "just now" to_pretty(40000) => "11 hours ago" ``` ##Specifics - The output will be an amount of time, t, included in one of the following phrases: "just now", "[t] seconds ago", "[t] minutes ago", "[t] hours ago", "[t] days ago", "[t] weeks ago". - You will have to handle the singular cases. That is, when t = 1, the phrasing will be one of "a second ago", "a minute ago", "an hour ago", "a day ago", "a week ago". - The amount of time is always rounded down to the nearest integer. For example, if the amount of time is actually 11.73 hours ago, the return value will be "11 hours ago". - Only times in the past will be given, with the range "just now" to "52 weeks ago" 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. Mikhail walks on a Cartesian plane. He starts at the point $(0, 0)$, and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point $(0, 0)$, he can go to any of the following points in one move: $(1, 0)$; $(1, 1)$; $(0, 1)$; $(-1, 1)$; $(-1, 0)$; $(-1, -1)$; $(0, -1)$; $(1, -1)$. If Mikhail goes from the point $(x1, y1)$ to the point $(x2, y2)$ in one move, and $x1 \ne x2$ and $y1 \ne y2$, then such a move is called a diagonal move. Mikhail has $q$ queries. For the $i$-th query Mikhail's target is to go to the point $(n_i, m_i)$ from the point $(0, 0)$ in exactly $k_i$ moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point $(0, 0)$ to the point $(n_i, m_i)$ in $k_i$ moves. Note that Mikhail can visit any point any number of times (even the destination point!). -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 10^4$) β€” the number of queries. Then $q$ lines follow. The $i$-th of these $q$ lines contains three integers $n_i$, $m_i$ and $k_i$ ($1 \le n_i, m_i, k_i \le 10^{18}$) β€” $x$-coordinate of the destination point of the query, $y$-coordinate of the destination point of the query and the number of moves in the query, correspondingly. -----Output----- Print $q$ integers. The $i$-th integer should be equal to -1 if Mikhail cannot go from the point $(0, 0)$ to the point $(n_i, m_i)$ in exactly $k_i$ moves described above. Otherwise the $i$-th integer should be equal to the the maximum number of diagonal moves among all possible movements. -----Example----- Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 -----Note----- One of the possible answers to the first test case: $(0, 0) \to (1, 0) \to (1, 1) \to (2, 2)$. One of the possible answers to the second test case: $(0, 0) \to (0, 1) \to (1, 2) \to (0, 3) \to (1, 4) \to (2, 3) \to (3, 2) \to (4, 3)$. In the third test case Mikhail cannot reach the point $(10, 1)$ in 9 moves. 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. Let $s$ be some string consisting of symbols "0" or "1". Let's call a string $t$ a substring of string $s$, if there exists such number $1 \leq l \leq |s| - |t| + 1$ that $t = s_l s_{l+1} \ldots s_{l + |t| - 1}$. Let's call a substring $t$ of string $s$ unique, if there exist only one such $l$. For example, let $s = $"1010111". A string $t = $"010" is an unique substring of $s$, because $l = 2$ is the only one suitable number. But, for example $t = $"10" isn't a unique substring of $s$, because $l = 1$ and $l = 3$ are suitable. And for example $t =$"00" at all isn't a substring of $s$, because there is no suitable $l$. Today Vasya solved the following problem at the informatics lesson: given a string consisting of symbols "0" and "1", the task is to find the length of its minimal unique substring. He has written a solution to this problem and wants to test it. He is asking you to help him. You are given $2$ positive integers $n$ and $k$, such that $(n \bmod 2) = (k \bmod 2)$, where $(x \bmod 2)$ is operation of taking remainder of $x$ by dividing on $2$. Find any string $s$ consisting of $n$ symbols "0" or "1", such that the length of its minimal unique substring is equal to $k$. -----Input----- The first line contains two integers $n$ and $k$, separated by spaces ($1 \leq k \leq n \leq 100\,000$, $(k \bmod 2) = (n \bmod 2)$). -----Output----- Print a string $s$ of length $n$, consisting of symbols "0" and "1". Minimal length of the unique substring of $s$ should be equal to $k$. You can find any suitable string. It is guaranteed, that there exists at least one such string. -----Examples----- Input 4 4 Output 1111 Input 5 3 Output 01010 Input 7 3 Output 1011011 -----Note----- In the first test, it's easy to see, that the only unique substring of string $s = $"1111" is all string $s$, which has length $4$. In the second test a string $s = $"01010" has minimal unique substring $t =$"101", which has length $3$. In the third test a string $s = $"1011011" has minimal unique substring $t =$"110", which has length $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. You are creating a level for a video game. The level consists of $n$ rooms placed in a circle. The rooms are numbered $1$ through $n$. Each room contains exactly one exit: completing the $j$-th room allows you to go the $(j+1)$-th room (and completing the $n$-th room allows you to go the $1$-st room). You are given the description of the multiset of $n$ chests: the $i$-th chest has treasure value $c_i$. Each chest can be of one of two types: regular chestΒ β€” when a player enters a room with this chest, he grabs the treasure and proceeds to the next room; mimic chestΒ β€” when a player enters a room with this chest, the chest eats him alive, and he loses. The player starts in a random room with each room having an equal probability of being chosen. The players earnings is equal to the total value of treasure chests he'd collected before he lost. You are allowed to choose the order the chests go into the rooms. For each $k$ from $1$ to $n$ place the chests into the rooms in such a way that: each room contains exactly one chest; exactly $k$ chests are mimics; the expected value of players earnings is minimum possible. Please note that for each $k$ the placement is chosen independently. It can be shown that it is in the form of $\frac{P}{Q}$ where $P$ and $Q$ are non-negative integers and $Q \ne 0$. Report the values of $P \cdot Q^{-1} \pmod {998244353}$. -----Input----- The first contains a single integer $n$ ($2 \le n \le 3 \cdot 10^5$)Β β€” the number of rooms and the number of chests. The second line contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^6$)Β β€” the treasure values of each chest. -----Output----- Print $n$ integersΒ β€” the $k$Β -th value should be equal to the minimum possible expected value of players earnings if the chests are placed into the rooms in some order and exactly $k$ of the chests are mimics. It can be shown that it is in the form of $\frac{P}{Q}$ where $P$ and $Q$ are non-negative integers and $Q \ne 0$. Report the values of $P \cdot Q^{-1} \pmod {998244353}$. -----Examples----- Input 2 1 2 Output 499122177 0 Input 8 10 4 3 6 5 10 7 5 Output 499122193 249561095 249561092 873463811 499122178 124780545 623902721 0 -----Note----- In the first example the exact values of minimum expected values are: $\frac 1 2$, $\frac 0 2$. In the second example the exact values of minimum expected values are: $\frac{132} 8$, $\frac{54} 8$, $\frac{30} 8$, $\frac{17} 8$, $\frac{12} 8$, $\frac 7 8$, $\frac 3 8$, $\frac 0 8$. 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. Ayoub thinks that he is a very smart person, so he created a function $f(s)$, where $s$ is a binary string (a string which contains only symbols "0" and "1"). The function $f(s)$ is equal to the number of substrings in the string $s$ that contains at least one symbol, that is equal to "1". More formally, $f(s)$ is equal to the number of pairs of integers $(l, r)$, such that $1 \leq l \leq r \leq |s|$ (where $|s|$ is equal to the length of string $s$), such that at least one of the symbols $s_l, s_{l+1}, \ldots, s_r$ is equal to "1". For example, if $s = $"01010" then $f(s) = 12$, because there are $12$ such pairs $(l, r)$: $(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$. Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $n$ and $m$ and asked him this problem. For all binary strings $s$ of length $n$ which contains exactly $m$ symbols equal to "1", find the maximum value of $f(s)$. Mahmoud couldn't solve the problem so he asked you for help. Can you help him? -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) Β β€” the number of test cases. The description of the test cases follows. The only line for each test case contains two integers $n$, $m$ ($1 \leq n \leq 10^{9}$, $0 \leq m \leq n$)Β β€” the length of the string and the number of symbols equal to "1" in it. -----Output----- For every test case print one integer numberΒ β€” the maximum value of $f(s)$ over all strings $s$ of length $n$, which has exactly $m$ symbols, equal to "1". -----Example----- Input 5 3 1 3 2 3 3 4 0 5 2 Output 4 5 6 0 12 -----Note----- In the first test case, there exists only $3$ strings of length $3$, which has exactly $1$ symbol, equal to "1". These strings are: $s_1 = $"100", $s_2 = $"010", $s_3 = $"001". The values of $f$ for them are: $f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$, so the maximum value is $4$ and the answer is $4$. In the second test case, the string $s$ with the maximum value is "101". In the third test case, the string $s$ with the maximum value is "111". In the fourth test case, the only string $s$ of length $4$, which has exactly $0$ symbols, equal to "1" is "0000" and the value of $f$ for that string is $0$, so the answer is $0$. In the fifth test case, the string $s$ with the maximum value is "01010" and it is described as an example in 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. A and B are preparing themselves for programming contests. To train their logical thinking and solve problems better, A and B decided to play chess. During the game A wondered whose position is now stronger. For each chess piece we know its weight: the queen's weight is 9, the rook's weight is 5, the bishop's weight is 3, the knight's weight is 3, the pawn's weight is 1, the king's weight isn't considered in evaluating position. The player's weight equals to the sum of weights of all his pieces on the board. As A doesn't like counting, he asked you to help him determine which player has the larger position weight. -----Input----- The input contains eight lines, eight characters each β€” the board's description. The white pieces on the board are marked with uppercase letters, the black pieces are marked with lowercase letters. The white pieces are denoted as follows: the queen is represented is 'Q', the rook β€” as 'R', the bishop β€” as'B', the knight β€” as 'N', the pawn β€” as 'P', the king β€” as 'K'. The black pieces are denoted as 'q', 'r', 'b', 'n', 'p', 'k', respectively. An empty square of the board is marked as '.' (a dot). It is not guaranteed that the given chess position can be achieved in a real game. Specifically, there can be an arbitrary (possibly zero) number pieces of each type, the king may be under attack and so on. -----Output----- Print "White" (without quotes) if the weight of the position of the white pieces is more than the weight of the position of the black pieces, print "Black" if the weight of the black pieces is more than the weight of the white pieces and print "Draw" if the weights of the white and black pieces are equal. -----Examples----- Input ...QK... ........ ........ ........ ........ ........ ........ ...rk... Output White Input rnbqkbnr pppppppp ........ ........ ........ ........ PPPPPPPP RNBQKBNR Output Draw Input rppppppr ...k.... ........ ........ ........ ........ K...Q... ........ Output Black -----Note----- In the first test sample the weight of the position of the white pieces equals to 9, the weight of the position of the black pieces equals 5. In the second test sample the weights of the positions of the black and the white pieces are equal to 39. In the third test sample the weight of the position of the white pieces equals to 9, the weight of the position of the black pieces equals to 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. We have a permutation of the integers from 1 through N, p_1, p_2, .., p_N. We also have M pairs of two integers between 1 and N (inclusive), represented as (x_1,y_1), (x_2,y_2), .., (x_M,y_M). AtCoDeer the deer is going to perform the following operation on p as many times as desired so that the number of i (1 ≀ i ≀ N) such that p_i = i is maximized: * Choose j such that 1 ≀ j ≀ M, and swap p_{x_j} and p_{y_j}. Find the maximum possible number of i such that p_i = i after operations. Constraints * 2 ≀ N ≀ 10^5 * 1 ≀ M ≀ 10^5 * p is a permutation of integers from 1 through N. * 1 ≀ x_j,y_j ≀ N * x_j β‰  y_j * If i β‰  j, \\{x_i,y_i\\} β‰  \\{x_j,y_j\\}. * All values in input are integers. Input Input is given from Standard Input in the following format: N M p_1 p_2 .. p_N x_1 y_1 x_2 y_2 : x_M y_M Output Print the maximum possible number of i such that p_i = i after operations. Examples Input 5 2 5 3 1 4 2 1 3 5 4 Output 2 Input 3 2 3 2 1 1 2 2 3 Output 3 Input 10 8 5 3 6 8 7 10 9 1 2 4 3 1 4 1 5 9 2 5 6 5 3 5 8 9 7 9 Output 8 Input 5 1 1 2 3 4 5 1 5 Output 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. Theater stage is a rectangular field of size n Γ— m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not. You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above)Β β€” left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines. A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ. -----Input----- The first line contains two positive integers n and m (1 ≀ n, m ≀ 1000)Β β€” the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 eachΒ β€” the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan. -----Output----- Print one integerΒ β€” the number of good positions for placing the spotlight. -----Examples----- Input 2 4 0 1 0 0 1 0 1 0 Output 9 Input 4 4 0 0 0 0 1 0 0 1 0 1 1 0 0 1 0 0 Output 20 -----Note----- In the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example. 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, little Vasya found himself in a maze consisting of (n + 1) rooms, numbered from 1 to (n + 1). Initially, Vasya is at the first room and to get out of the maze, he needs to get to the (n + 1)-th one. The maze is organized as follows. Each room of the maze has two one-way portals. Let's consider room number i (1 ≀ i ≀ n), someone can use the first portal to move from it to room number (i + 1), also someone can use the second portal to move from it to room number pi, where 1 ≀ pi ≀ i. In order not to get lost, Vasya decided to act as follows. * Each time Vasya enters some room, he paints a cross on its ceiling. Initially, Vasya paints a cross at the ceiling of room 1. * Let's assume that Vasya is in room i and has already painted a cross on its ceiling. Then, if the ceiling now contains an odd number of crosses, Vasya uses the second portal (it leads to room pi), otherwise Vasya uses the first portal. Help Vasya determine the number of times he needs to use portals to get to room (n + 1) in the end. Input The first line contains integer n (1 ≀ n ≀ 103) β€” the number of rooms. The second line contains n integers pi (1 ≀ pi ≀ i). Each pi denotes the number of the room, that someone can reach, if he will use the second portal in the i-th room. Output Print a single number β€” the number of portal moves the boy needs to go out of the maze. As the number can be rather large, print it modulo 1000000007 (109 + 7). Examples Input 2 1 2 Output 4 Input 4 1 1 2 3 Output 20 Input 5 1 1 1 1 1 Output 62 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. Tarly has two different type of items, food boxes and wine barrels. There are f food boxes and w wine barrels. Tarly stores them in various stacks and each stack can consist of either food boxes or wine barrels but not both. The stacks are placed in a line such that no two stacks of food boxes are together and no two stacks of wine barrels are together. The height of a stack is defined as the number of items in the stack. Two stacks are considered different if either their heights are different or one of them contains food and other contains wine. Jon Snow doesn't like an arrangement if any stack of wine barrels has height less than or equal to h. What is the probability that Jon Snow will like the arrangement if all arrangement are equiprobably? Two arrangement of stacks are considered different if exists such i, that i-th stack of one arrangement is different from the i-th stack of the other arrangement. -----Input----- The first line of input contains three integers f, w, h (0 ≀ f, w, h ≀ 10^5) β€” number of food boxes, number of wine barrels and h is as described above. It is guaranteed that he has at least one food box or at least one wine barrel. -----Output----- Output the probability that Jon Snow will like the arrangement. The probability is of the form [Image], then you need to output a single integer pΒ·q^{ - 1} mod (10^9 + 7). -----Examples----- Input 1 1 1 Output 0 Input 1 2 1 Output 666666672 -----Note----- In the first example f = 1, w = 1 and h = 1, there are only two possible arrangement of stacks and Jon Snow doesn't like any of them. In the second example f = 1, w = 2 and h = 1, there are three arrangements. Jon Snow likes the (1) and (3) arrangement. So the probabilty is $\frac{2}{3}$. [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. Read problems statements in Russian here Chef has a special affection for sets of binary strings of equal length which have same numbers of 1's. Given three integers n, k and m, your task is to find the the lexicographically m^{th} smallest string among strings which have length n and have k 1's. If no such string exists output -1. ------ Tips: ------ To see what lexicographic order means . See http://en.wikipedia.org/wiki/Lexicographical_{order} ------ Input ------ Input description. The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows: The first and only line of each test case contains three space separated integers N , K and M ------ Output ------ For each test case output the answer on a separate line . ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 350$ $1 ≀ K ≀ N$ ------ Example ------ Input: 1 3 2 2 Output: 101 ------ Explanation ------ Example case 1. The set of strings in lexicographic order is "011", "101", and "110" ------ Scoring ------ Subtask 1 (41 point): $1 ≀ N ≀ 20$ Subtask 2 (59 points): $1 ≀ N ≀ 350$ 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 and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process. You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded. Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$? Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β€” the number of test cases. The description of the test cases follows. The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β€” the number of elements in the array, your position in line and the number of people whose choices you can fix. The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β€” elements of the array. It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$. -----Output----- For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$. -----Example----- Input 4 6 4 2 2 9 2 3 8 5 4 4 1 2 13 60 4 4 1 3 1 2 2 1 2 2 0 1 2 Output 8 4 1 1 -----Note----- In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element). Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$. In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $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. Consider an infinite sequence a_1, a_2, … Initially, the values of all the terms are 0, and from this state we will sequentially perform Q operations. The i-th operation (1 ≀ i ≀ Q) is as follows: * For every positive integer j, add x_i to the value of a_{j Γ— m_i}. Find the value of the largest term after these Q operations. Constraints * 1 ≀ Q ≀ 299 * 2 ≀ m_i ≀ 300 * -10^6 ≀ x_i ≀ 10^6 * All m_i are distinct. * All input values are integers. Input Input is given from Standard Input in the following format: Q m_1 x_1 : m_Q x_Q Output Print the value of the largest term after the Q operations. Examples Input 3 2 10 3 -20 6 15 Output 10 Input 3 10 -3 50 4 100 -5 Output 1 Input 5 56 114834 72 -149861 100 190757 192 -132693 240 133108 Output 438699 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 store sells $n$ items, the price of the $i$-th item is $p_i$. The store's management is going to hold a promotion: if a customer purchases at least $x$ items, $y$ cheapest of them are free. The management has not yet decided on the exact values of $x$ and $y$. Therefore, they ask you to process $q$ queries: for the given values of $x$ and $y$, determine the maximum total value of items received for free, if a customer makes one purchase. Note that all queries are independent; they don't affect the store's stock. -----Input----- The first line contains two integers $n$ and $q$ ($1 \le n, q \le 2 \cdot 10^5$) β€” the number of items in the store and the number of queries, respectively. The second line contains $n$ integers $p_1, p_2, \dots, p_n$ ($1 \le p_i \le 10^6$), where $p_i$ β€” the price of the $i$-th item. The following $q$ lines contain two integers $x_i$ and $y_i$ each ($1 \le y_i \le x_i \le n$) β€” the values of the parameters $x$ and $y$ in the $i$-th query. -----Output----- For each query, print a single integer β€” the maximum total value of items received for free for one purchase. -----Examples----- Input 5 3 5 3 1 5 2 3 2 1 1 5 3 Output 8 5 6 -----Note----- In the first query, a customer can buy three items worth $5, 3, 5$, the two cheapest of them are $3 + 5 = 8$. In the second query, a customer can buy two items worth $5$ and $5$, the cheapest of them is $5$. In the third query, a customer has to buy all the items to receive the three cheapest of them for free; their total price is $1 + 2 + 3 = 6$. 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. Alena has successfully passed the entrance exams to the university and is now looking forward to start studying. One two-hour lesson at the Russian university is traditionally called a pair, it lasts for two academic hours (an academic hour is equal to 45 minutes). The University works in such a way that every day it holds exactly n lessons. Depending on the schedule of a particular group of students, on a given day, some pairs may actually contain classes, but some may be empty (such pairs are called breaks). The official website of the university has already published the schedule for tomorrow for Alena's group. Thus, for each of the n pairs she knows if there will be a class at that time or not. Alena's House is far from the university, so if there are breaks, she doesn't always go home. Alena has time to go home only if the break consists of at least two free pairs in a row, otherwise she waits for the next pair at the university. Of course, Alena does not want to be sleepy during pairs, so she will sleep as long as possible, and will only come to the first pair that is presented in her schedule. Similarly, if there are no more pairs, then Alena immediately goes home. Alena appreciates the time spent at home, so she always goes home when it is possible, and returns to the university only at the beginning of the next pair. Help Alena determine for how many pairs she will stay at the university. Note that during some pairs Alena may be at the university waiting for the upcoming pair. -----Input----- The first line of the input contains a positive integer n (1 ≀ n ≀ 100) β€” the number of lessons at the university. The second line contains n numbers a_{i} (0 ≀ a_{i} ≀ 1). Number a_{i} equals 0, if Alena doesn't have the i-th pairs, otherwise it is equal to 1. Numbers a_1, a_2, ..., a_{n} are separated by spaces. -----Output----- Print a single number β€” the number of pairs during which Alena stays at the university. -----Examples----- Input 5 0 1 0 1 1 Output 4 Input 7 1 0 1 0 0 1 0 Output 4 Input 1 0 Output 0 -----Note----- In the first sample Alena stays at the university from the second to the fifth pair, inclusive, during the third pair she will be it the university waiting for the next pair. In the last sample Alena doesn't have a single pair, so she spends all the time at home. 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 A_1, ..., A_N of length N. We will choose exactly \left\lfloor \frac{N}{2} \right\rfloor elements from this sequence so that no two adjacent elements are chosen. Find the maximum possible sum of the chosen elements. Here \lfloor x \rfloor denotes the greatest integer not greater than x. -----Constraints----- - 2 \leq N \leq 2\times 10^5 - |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_N -----Output----- Print the maximum possible sum of the chosen elements. -----Sample Input----- 6 1 2 3 4 5 6 -----Sample Output----- 12 Choosing 2, 4, and 6 makes the sum 12, which is the maximum possible value. 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 are n cities numbered from 1 to n in Berland. Some of them are connected by two-way roads. Each road has its own length β€” an integer number from 1 to 1000. It is known that from each city it is possible to get to any other city by existing roads. Also for each pair of cities it is known the shortest distance between them. Berland Government plans to build k new roads. For each of the planned road it is known its length, and what cities it will connect. To control the correctness of the construction of new roads, after the opening of another road Berland government wants to check the sum of the shortest distances between all pairs of cities. Help them β€” for a given matrix of shortest distances on the old roads and plans of all new roads, find out how the sum of the shortest distances between all pairs of cities changes after construction of each road. Input The first line contains integer n (2 ≀ n ≀ 300) β€” amount of cities in Berland. Then there follow n lines with n integer numbers each β€” the matrix of shortest distances. j-th integer in the i-th row β€” di, j, the shortest distance between cities i and j. It is guaranteed that di, i = 0, di, j = dj, i, and a given matrix is a matrix of shortest distances for some set of two-way roads with integer lengths from 1 to 1000, such that from each city it is possible to get to any other city using these roads. Next line contains integer k (1 ≀ k ≀ 300) β€” amount of planned roads. Following k lines contain the description of the planned roads. Each road is described by three space-separated integers ai, bi, ci (1 ≀ ai, bi ≀ n, ai β‰  bi, 1 ≀ ci ≀ 1000) β€” ai and bi β€” pair of cities, which the road connects, ci β€” the length of the road. It can be several roads between a pair of cities, but no road connects the city with itself. Output Output k space-separated integers qi (1 ≀ i ≀ k). qi should be equal to the sum of shortest distances between all pairs of cities after the construction of roads with indexes from 1 to i. Roads are numbered from 1 in the input order. Each pair of cities should be taken into account in the sum exactly once, i. e. we count unordered pairs. Examples Input 2 0 5 5 0 1 1 2 3 Output 3 Input 3 0 4 5 4 0 9 5 9 0 2 2 3 8 1 2 1 Output 17 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. Emuskald is an avid horticulturist and owns the world's longest greenhouse β€” it is effectively infinite in length. Over the years Emuskald has cultivated n plants in his greenhouse, of m different plant species numbered from 1 to m. His greenhouse is very narrow and can be viewed as an infinite line, with each plant occupying a single point on that line. Emuskald has discovered that each species thrives at a different temperature, so he wants to arrange m - 1 borders that would divide the greenhouse into m sections numbered from 1 to m from left to right with each section housing a single species. He is free to place the borders, but in the end all of the i-th species plants must reside in i-th section from the left. Of course, it is not always possible to place the borders in such way, so Emuskald needs to replant some of his plants. He can remove each plant from its position and place it anywhere in the greenhouse (at any real coordinate) with no plant already in it. Since replanting is a lot of stress for the plants, help Emuskald find the minimum number of plants he has to replant to be able to place the borders. Input The first line of input contains two space-separated integers n and m (1 ≀ n, m ≀ 5000, n β‰₯ m), the number of plants and the number of different species. Each of the following n lines contain two space-separated numbers: one integer number si (1 ≀ si ≀ m), and one real number xi (0 ≀ xi ≀ 109), the species and position of the i-th plant. Each xi will contain no more than 6 digits after the decimal point. It is guaranteed that all xi are different; there is at least one plant of each species; the plants are given in order "from left to the right", that is in the ascending order of their xi coordinates (xi < xi + 1, 1 ≀ i < n). Output Output a single integer β€” the minimum number of plants to be replanted. Examples Input 3 2 2 1 1 2.0 1 3.100 Output 1 Input 3 3 1 5.0 2 5.5 3 6.0 Output 0 Input 6 3 1 14.284235 2 17.921382 1 20.328172 3 20.842331 1 25.790145 1 27.204125 Output 2 Note In the first test case, Emuskald can replant the first plant to the right of the last plant, so the answer is 1. In the second test case, the species are already in the correct order, so no replanting is needed. 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. Little Egor is a huge movie fan. He likes watching different kinds of movies: from drama movies to comedy movies, from teen movies to horror movies. He is planning to visit cinema this weekend, but he's not sure which movie he should watch. There are n movies to watch during this weekend. Each movie can be characterized by two integers L_{i} and R_{i}, denoting the length and the rating of the corresponding movie. Egor wants to watch exactly one movie with the maximal value of L_{i} Γ— R_{i}. If there are several such movies, he would pick a one with the maximal R_{i} among them. If there is still a tie, he would pick the one with the minimal index among them. Your task is to help Egor to pick a movie to watch during this weekend. ------ Input ------ The first line of the input contains an integer T denoting the number of test cases. The first line of the test case description contains an integer n. The second line of the test case description contains n integers L_{1}, L_{2}, ...,L_{n}. The following line contains n integers R_{1}, R_{2}, ..., R_{n}. ------ Output ------ For each test case, output a single integer i denoting the index of the movie that Egor should watch during this weekend. Note that we follow 1-based indexing. ------ Constraints ------ $1 ≀ T ≀ 5$ $1 ≀ n ≀ 100$ $1 ≀ L_{i}, R_{i} ≀ 100$ ----- Sample Input 1 ------ 2 2 1 2 2 1 4 2 1 4 1 2 4 1 4 ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ In the first example case, both films have the same value of L Γ— R, but the first film has a better rating. In the second example case, the second and the fourth movies are equally good, but the second movie has a smaller index. 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. Yukiya, the owner of Aizuyama Ski Resort, has prepared a course for advanced skiers with obstacles and jumping hills. There are various ways to slide on the course, and users who can slide in all patterns during the season will be given a gift. Let's create a program for the oil tree shop that outputs the number of patterns of how to slide based on the floor plan of the course. <image> The course is represented by a grid of X x Y squares as shown above. It is assumed that the origin is at the upper left, the x coordinate increases as it goes to the right, and the y coordinate increases as it goes down. Each gliding pattern starts at the highest point (y = 1, but without obstacles) and progresses towards the goal (y = Y). A runner in a grid square (x, y) can move to either (x βˆ’ 1, y + 1), (x, y + 1), or (x + 1, y + 1). I will. There are obstacles and jumping platforms in the squares, and you cannot enter the squares with obstacles, and when you enter the squares with jumping platforms, you will move to (x, y + 2). However, there is no jumping platform in the highest cell (the cell with y = 1), and when entering a cell with a jumping platform, you can only enter from the cell with the same x coordinate. Starting from the top of the course (y = 1) and crossing the bottom without deviating from the course (y β‰₯ Y), it is considered as one way of sliding and ends. Create a program that takes the course information as input and outputs the total number of slips. Input A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format: X Y c11 c21 ... cX1 c12 c22 ... cX2 :: c1Y c2Y ... cXY The first line gives the course size X, Y (1 ≀ X, Y ≀ 15). Course information is given in the Y line that follows. cij (one of 0, 1, or 2) is an integer that represents the information of the squares of x = i, y = j, where 0 is a movable square, 1 is a square with an obstacle, and 2 is a square with a jumping platform. Represents. The number of datasets does not exceed 50. Output For each input dataset, the number of patterns of how to slide the course is output on one line. Example Input 5 5 0 0 0 0 1 2 1 0 2 0 1 0 0 1 1 0 2 1 2 0 0 1 0 0 0 5 5 0 0 1 0 0 2 1 0 2 0 1 0 0 1 1 0 2 1 2 0 0 1 0 0 0 15 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Output 8 6 52694573 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 are N one-off jobs available. If you take the i-th job and complete it, you will earn the reward of B_i after A_i days from the day you do it. You can take and complete at most one of these jobs in a day. However, you cannot retake a job that you have already done. Find the maximum total reward that you can earn no later than M days from today. You can already start working today. -----Constraints----- - All values in input are integers. - 1 \leq N \leq 10^5 - 1 \leq M \leq 10^5 - 1 \leq A_i \leq 10^5 - 1 \leq B_i \leq 10^4 -----Input----- Input is given from Standard Input in the following format: N M A_1 B_1 A_2 B_2 \vdots A_N B_N -----Output----- Print the maximum total reward that you can earn no later than M days from today. -----Sample Input----- 3 4 4 3 4 1 2 2 -----Sample Output----- 5 You can earn the total reward of 5 by taking the jobs as follows: - Take and complete the first job today. You will earn the reward of 3 after four days from today. - Take and complete the third job tomorrow. You will earn the reward of 2 after two days from tomorrow, that is, after three days from today. 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 positive integers $a$ and $b$. In one move you can increase $a$ by $1$ (replace $a$ with $a+1$). Your task is to find the minimum number of moves you need to do in order to make $a$ divisible by $b$. It is possible, that you have to make $0$ moves, as $a$ is already divisible by $b$. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The only line of the test case contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$). -----Output----- For each test case print the answer β€” the minimum number of moves you need to do in order to make $a$ divisible by $b$. -----Example----- Input 5 10 4 13 9 100 13 123 456 92 46 Output 2 5 4 333 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. Dr. Hedro is astonished. According to his theory, we can make sludge that can dissolve almost everything on the earth. Now he's trying to produce the sludge to verify his theory. The sludge is produced in a rectangular solid shaped tank whose size is N Γ— N Γ— 2. Let coordinate of two corner points of tank be (-N/2, -N/2, -1), (N/2, N/2, 1) as shown in Figure 1. <image> Figure 1 Doctor pours liquid that is ingredient of the sludge until height of the liquid becomes 1. Next, he get a lid on a tank and rotates slowly, without ruffling, with respect to z axis (Figure 2). After rotating enough long time, sludge is produced. Volume of liquid never changes through this operation. <image> Figure 2 Needless to say, ordinary materials cannot be the tank. According to Doctor's theory, there is only one material that is not dissolved by the sludge on the earth. Doctor named it finaldefenceproblem (for short, FDP). To attach FDP tiles inside the tank allows to produce the sludge. Since to produce FDP is very difficult, the size of FDP tiles are limited to 1 * 1. At first, doctor tried to cover entire the entire inside of the tank. However, it turned out that to make enough number FDP tiles that can cover completely is impossible because it takes too long time. Therefore, he decided to replace FDP tiles where an area the sludge touches is zero with those of ordinary materials. All tiles are very thin, so never affects height of the sludge. How many number of FDP tiles does doctor need? He has imposed this tough problem on you, his helper. Constraints * Judge data consists of at most 150 datasets. * 2 ≀ N ≀ 1012 * N is even number Input Input file contains several data sets. Each data set has one integer that describes N. Input ends when N = 0. You should output nothing for this case. Output For each data set, print the number of tiles in a line. Example Input 2 4 0 Output 24 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. Ujan has a lot of numbers in his boxes. He likes order and balance, so he decided to reorder the numbers. There are $k$ boxes numbered from $1$ to $k$. The $i$-th box contains $n_i$ integer numbers. The integers can be negative. All of the integers are distinct. Ujan is lazy, so he will do the following reordering of the numbers exactly once. He will pick a single integer from each of the boxes, $k$ integers in total. Then he will insert the chosen numbersΒ β€” one integer in each of the boxes, so that the number of integers in each box is the same as in the beginning. Note that he may also insert an integer he picked from a box back into the same box. Ujan will be happy if the sum of the integers in each box is the same. Can he achieve this and make the boxes perfectly balanced, like all things should be? -----Input----- The first line contains a single integer $k$ ($1 \leq k \leq 15$), the number of boxes. The $i$-th of the next $k$ lines first contains a single integer $n_i$ ($1 \leq n_i \leq 5\,000$), the number of integers in box $i$. Then the same line contains $n_i$ integers $a_{i,1}, \ldots, a_{i,n_i}$ ($|a_{i,j}| \leq 10^9$), the integers in the $i$-th box. It is guaranteed that all $a_{i,j}$ are distinct. -----Output----- If Ujan cannot achieve his goal, output "No" in a single line. Otherwise in the first line output "Yes", and then output $k$ lines. The $i$-th of these lines should contain two integers $c_i$ and $p_i$. This means that Ujan should pick the integer $c_i$ from the $i$-th box and place it in the $p_i$-th box afterwards. If there are multiple solutions, output any of those. You can print each letter in any case (upper or lower). -----Examples----- Input 4 3 1 7 4 2 3 2 2 8 5 1 10 Output Yes 7 2 2 3 5 1 10 4 Input 2 2 3 -2 2 -1 5 Output No Input 2 2 -10 10 2 0 -20 Output Yes -10 2 -20 1 -----Note----- In the first sample, Ujan can put the number $7$ in the $2$nd box, the number $2$ in the $3$rd box, the number $5$ in the $1$st box and keep the number $10$ in the same $4$th box. Then the boxes will contain numbers $\{1,5,4\}$, $\{3, 7\}$, $\{8,2\}$ and $\{10\}$. The sum in each box then is equal to $10$. In the second sample, it is not possible to pick and redistribute the numbers in the required way. In the third sample, one can swap the numbers $-20$ and $-10$, making the sum in each box equal to $-10$. 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. Kamil likes streaming the competitive programming videos. His MeTube channel has recently reached $100$ million subscribers. In order to celebrate this, he posted a video with an interesting problem he couldn't solve yet. Can you help him? You're given a tree β€” a connected undirected graph consisting of $n$ vertices connected by $n - 1$ edges. The tree is rooted at vertex $1$. A vertex $u$ is called an ancestor of $v$ if it lies on the shortest path between the root and $v$. In particular, a vertex is an ancestor of itself. Each vertex $v$ is assigned its beauty $x_v$ β€” a non-negative integer not larger than $10^{12}$. This allows us to define the beauty of a path. Let $u$ be an ancestor of $v$. Then we define the beauty $f(u, v)$ as the greatest common divisor of the beauties of all vertices on the shortest path between $u$ and $v$. Formally, if $u=t_1, t_2, t_3, \dots, t_k=v$ are the vertices on the shortest path between $u$ and $v$, then $f(u, v) = \gcd(x_{t_1}, x_{t_2}, \dots, x_{t_k})$. Here, $\gcd$ denotes the greatest common divisor of a set of numbers. In particular, $f(u, u) = \gcd(x_u) = x_u$. Your task is to find the sum $$ \sum_{u\text{ is an ancestor of }v} f(u, v). $$ As the result might be too large, please output it modulo $10^9 + 7$. Note that for each $y$, $\gcd(0, y) = \gcd(y, 0) = y$. In particular, $\gcd(0, 0) = 0$. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100\,000$) β€” the number of vertices in the tree. The following line contains $n$ integers $x_1, x_2, \dots, x_n$ ($0 \le x_i \le 10^{12}$). The value $x_v$ denotes the beauty of vertex $v$. The following $n - 1$ lines describe the edges of the tree. Each of them contains two integers $a, b$ ($1 \le a, b \le n$, $a \neq b$) β€” the vertices connected by a single edge. -----Output----- Output the sum of the beauties on all paths $(u, v)$ such that $u$ is ancestor of $v$. This sum should be printed modulo $10^9 + 7$. -----Examples----- Input 5 4 5 6 0 8 1 2 1 3 1 4 4 5 Output 42 Input 7 0 2 3 0 0 0 0 1 2 1 3 2 4 2 5 3 6 3 7 Output 30 -----Note----- The following figure shows all $10$ possible paths for which one endpoint is an ancestor of another endpoint. The sum of beauties of all these paths is equal to $42$: [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. Anastasia loves going for a walk in Central Uzhlyandian Park. But she became uninterested in simple walking, so she began to collect Uzhlyandian pebbles. At first, she decided to collect all the pebbles she could find in the park. She has only two pockets. She can put at most k pebbles in each pocket at the same time. There are n different pebble types in the park, and there are w_{i} pebbles of the i-th type. Anastasia is very responsible, so she never mixes pebbles of different types in same pocket. However, she can put different kinds of pebbles in different pockets at the same time. Unfortunately, she can't spend all her time collecting pebbles, so she can collect pebbles from the park only once a day. Help her to find the minimum number of days needed to collect all the pebbles of Uzhlyandian Central Park, taking into consideration that Anastasia can't place pebbles of different types in same pocket. -----Input----- The first line contains two integers n and k (1 ≀ n ≀ 10^5, 1 ≀ k ≀ 10^9)Β β€” the number of different pebble types and number of pebbles Anastasia can place in one pocket. The second line contains n integers w_1, w_2, ..., w_{n} (1 ≀ w_{i} ≀ 10^4)Β β€” number of pebbles of each type. -----Output----- The only line of output contains one integerΒ β€” the minimum number of days Anastasia needs to collect all the pebbles. -----Examples----- Input 3 2 2 3 4 Output 3 Input 5 4 3 1 8 9 7 Output 5 -----Note----- In the first sample case, Anastasia can collect all pebbles of the first type on the first day, of second typeΒ β€” on the second day, and of third typeΒ β€” on the third day. Optimal sequence of actions in the second sample case: In the first day Anastasia collects 8 pebbles of the third type. In the second day she collects 8 pebbles of the fourth type. In the third day she collects 3 pebbles of the first type and 1 pebble of the fourth type. In the fourth day she collects 7 pebbles of the fifth type. In the fifth day she collects 1 pebble of the second type. 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 are N blocks arranged in a row. Let us paint these blocks. We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways. Find the number of ways to paint the blocks under the following conditions: - For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors. - There may be at most K pairs of adjacent blocks that are painted in the same color. Since the count may be enormous, print it modulo 998244353. -----Constraints----- - All values in input are integers. - 1 \leq N, M \leq 2 \times 10^5 - 0 \leq K \leq N - 1 -----Input----- Input is given from Standard Input in the following format: N M K -----Output----- Print the answer. -----Sample Input----- 3 2 1 -----Sample Output----- 6 The following ways to paint the blocks satisfy the conditions: 112, 121, 122, 211, 212, and 221. Here, digits represent the colors of the blocks. 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 a computer game with ninjas. At this stage Vasya's ninja should get out of a deep canyon. The canyon consists of two vertical parallel walls, their height is n meters. Let's imagine that we split these walls into 1 meter-long areas and number them with positive integers from 1 to n from bottom to top. Some areas are safe and the ninja can climb them. Others are spiky and ninja can't be there. Let's call such areas dangerous. Initially the ninja is on the lower area of the left wall. He can use each second to perform one of the following actions: * climb one area up; * climb one area down; * jump to the opposite wall. That gets the ninja to the area that is exactly k meters higher than the area he jumped from. More formally, if before the jump the ninja is located at area x of one wall, then after the jump he is located at area x + k of the other wall. If at some point of time the ninja tries to get to an area with a number larger than n, then we can assume that the ninja got out of the canyon. The canyon gets flooded and each second the water level raises one meter. Initially the water level is at the lower border of the first area. Ninja cannot be on the area covered by water. We can assume that the ninja and the water "move in turns" β€” first the ninja performs some action, then the water raises for one meter, then the ninja performs one more action and so on. The level is considered completed if the ninja manages to get out of the canyon. After several failed attempts Vasya started to doubt whether it is possible to complete the level at all. Help him answer the question. Input The first line contains two integers n and k (1 ≀ n, k ≀ 105) β€” the height of the canyon and the height of ninja's jump, correspondingly. The second line contains the description of the left wall β€” a string with the length of n characters. The i-th character represents the state of the i-th wall area: character "X" represents a dangerous area and character "-" represents a safe area. The third line describes the right wall in the same format. It is guaranteed that the first area of the left wall is not dangerous. Output Print "YES" (without the quotes) if the ninja can get out from the canyon, otherwise, print "NO" (without the quotes). Examples Input 7 3 ---X--X -X--XX- Output YES Input 6 2 --X-X- X--XX- Output NO Note In the first sample the ninja should first jump to the right wall, then go one meter down along the right wall, then jump to the left wall. The next jump can get the ninja from the canyon. In the second sample there's no way the ninja can get out of the canyon. 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 will be given a string S of length 3 representing the weather forecast for three days in the past. The i-th character (1 \leq i \leq 3) of S represents the forecast for the i-th day. S, C, and R stand for sunny, cloudy, and rainy, respectively. You will also be given a string T of length 3 representing the actual weather on those three days. The i-th character (1 \leq i \leq 3) of S represents the actual weather on the i-th day. S, C, and R stand for sunny, cloudy, and rainy, respectively. Print the number of days for which the forecast was correct. -----Constraints----- - S and T are strings of length 3 each. - S and T consist of S, C, and R. -----Input----- Input is given from Standard Input in the following format: S T -----Output----- Print the number of days for which the forecast was correct. -----Sample Input----- CSS CSR -----Sample Output----- 2 - For the first day, it was forecast to be cloudy, and it was indeed cloudy. - For the second day, it was forecast to be sunny, and it was indeed sunny. - For the third day, it was forecast to be sunny, but it was rainy. Thus, the forecast was correct for two days in this case. 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. This is the easy version of this problem. The only difference is the constraint on $k$ β€” the number of gifts in the offer. In this version: $k=2$. Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very luckyΒ β€” today the offer "$k$ of goods for the price of one" is held in store. Remember, that in this problem $k=2$. Using this offer, Vasya can buy exactly $k$ of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has. More formally, for each good, its price is determined by $a_i$Β β€” the number of coins it costs. Initially, Vasya has $p$ coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary: Vasya can buy one good with the index $i$ if he currently has enough coins (i.e $p \ge a_i$). After buying this good, the number of Vasya's coins will decrease by $a_i$, (i.e it becomes $p := p - a_i$). Vasya can buy a good with the index $i$, and also choose exactly $k-1$ goods, the price of which does not exceed $a_i$, if he currently has enough coins (i.e $p \ge a_i$). Thus, he buys all these $k$ goods, and his number of coins decreases by $a_i$ (i.e it becomes $p := p - a_i$). Please note that each good can be bought no more than once. For example, if the store now has $n=5$ goods worth $a_1=2, a_2=4, a_3=3, a_4=5, a_5=7$, respectively, $k=2$, and Vasya has $6$ coins, then he can buy $3$ goods. A good with the index $1$ will be bought by Vasya without using the offer and he will pay $2$ coins. Goods with the indices $2$ and $3$ Vasya will buy using the offer and he will pay $4$ coins. It can be proved that Vasya can not buy more goods with six coins. Help Vasya to find out the maximum number of goods he can buy. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$)Β β€” the number of test cases in the test. The next lines contain a description of $t$ test cases. The first line of each test case contains three integers $n, p, k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le p \le 2\cdot10^9$, $k=2$)Β β€” the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them. The second line of each test case contains $n$ integers $a_i$ ($1 \le a_i \le 10^4$)Β β€” the prices of goods. It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$. It is guaranteed that in this version of the problem $k=2$ for all test cases. -----Output----- For each test case in a separate line print one integer $m$Β β€” the maximum number of goods that Vasya can buy. -----Example----- Input 6 5 6 2 2 4 3 5 7 5 11 2 2 4 3 5 7 2 10000 2 10000 10000 2 9999 2 10000 10000 5 13 2 8 2 8 2 5 3 18 2 1 2 3 Output 3 4 2 0 4 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. Polycarp is a beginner programmer. He is studying how to use a command line. Polycarp faced the following problem. There are n files in a directory and he needs to delete some of them. Polycarp wants to run a single delete command with filename pattern as an argument. All the files to be deleted should match the pattern and all other files shouldn't match the pattern. Polycarp doesn't know about an asterisk '*', the only special character he knows is a question mark '?' which matches any single character. All other characters in the pattern match themselves only. Formally, a pattern matches a filename if and only if they have equal lengths and all characters in the corresponding positions are equal except when the character in the pattern is '?', in which case the corresponding filename character does not matter. For example, the filename pattern "a?ba?": matches filenames "aabaa", "abba.", "a.ba9" and "a.ba."; does not match filenames "aaba", "abaab", "aabaaa" and "aabaa.". Help Polycarp find a pattern which matches files to be deleted and only them or report if there is no such pattern. -----Input----- The first line of the input contains two integers n and m (1 ≀ m ≀ n ≀ 100) β€” the total number of files and the number of files to be deleted. The following n lines contain filenames, single filename per line. All filenames are non-empty strings containing only lowercase English letters, digits and dots ('.'). The length of each filename doesn't exceed 100. It is guaranteed that all filenames are distinct. The last line of the input contains m distinct integer numbers in ascending order a_1, a_2, ..., a_{m} (1 ≀ a_{i} ≀ n) β€” indices of files to be deleted. All files are indexed from 1 to n in order of their appearance in the input. -----Output----- If the required pattern exists, print "Yes" in the first line of the output. The second line should contain the required pattern. If there are multiple solutions, print any of them. If the required pattern doesn't exist, print the only line containing "No". -----Examples----- Input 3 2 ab ac cd 1 2 Output Yes a? Input 5 3 test tezt test. .est tes. 1 4 5 Output Yes ?es? Input 4 4 a b c dd 1 2 3 4 Output No Input 6 3 .svn .git .... ... .. . 1 2 3 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. You are given a string S of length 3 consisting of a, b and c. Determine if S can be obtained by permuting abc. -----Constraints----- - |S|=3 - S consists of a, b and c. -----Input----- Input is given from Standard Input in the following format: S -----Output----- If S can be obtained by permuting abc, print Yes; otherwise, print No. -----Sample Input----- bac -----Sample Output----- Yes Swapping the first and second characters in bac results in abc. 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. Statistics claims that students sleep no more than three hours a day. But even in the world of their dreams, while they are snoring peacefully, the sense of impending doom is still upon them. A poor student is dreaming that he is sitting the mathematical analysis exam. And he is examined by the most formidable professor of all times, a three times Soviet Union Hero, a Noble Prize laureate in student expulsion, venerable Petr Palych. The poor student couldn't answer a single question. Thus, instead of a large spacious office he is going to apply for a job to thorium mines. But wait a minute! Petr Palych decided to give the student the last chance! Yes, that is possible only in dreams. So the professor began: "Once a Venusian girl and a Marsian boy met on the Earth and decided to take a walk holding hands. But the problem is the girl has al fingers on her left hand and ar fingers on the right one. The boy correspondingly has bl and br fingers. They can only feel comfortable when holding hands, when no pair of the girl's fingers will touch each other. That is, they are comfortable when between any two girl's fingers there is a boy's finger. And in addition, no three fingers of the boy should touch each other. Determine if they can hold hands so that the both were comfortable." The boy any the girl don't care who goes to the left and who goes to the right. The difference is only that if the boy goes to the left of the girl, he will take her left hand with his right one, and if he goes to the right of the girl, then it is vice versa. Input The first line contains two positive integers not exceeding 100. They are the number of fingers on the Venusian girl's left and right hand correspondingly. The second line contains two integers not exceeding 100. They are the number of fingers on the Marsian boy's left and right hands correspondingly. Output Print YES or NO, that is, the answer to Petr Palych's question. Examples Input 5 1 10 5 Output YES Input 4 5 3 3 Output YES Input 1 2 11 6 Output NO Note The boy and the girl don't really care who goes to the left. 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. Kate and Michael want to buy a pizza and share it. Depending on the price of the pizza, they are going to divide the costs: * If the pizza is less than €5,- Michael invites Kate, so Michael pays the full price. * Otherwise Kate will contribute 1/3 of the price, but no more than €10 (she's broke :-) and Michael pays the rest. How much is Michael going to pay? Calculate the amount with two decimals, if necessary. 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. Numbers $1, 2, 3, \dots n$ (each integer from $1$ to $n$ once) are written on a board. In one operation you can erase any two numbers $a$ and $b$ from the board and write one integer $\frac{a + b}{2}$ rounded up instead. You should perform the given operation $n - 1$ times and make the resulting number that will be left on the board as small as possible. For example, if $n = 4$, the following course of action is optimal: choose $a = 4$ and $b = 2$, so the new number is $3$, and the whiteboard contains $[1, 3, 3]$; choose $a = 3$ and $b = 3$, so the new number is $3$, and the whiteboard contains $[1, 3]$; choose $a = 1$ and $b = 3$, so the new number is $2$, and the whiteboard contains $[2]$. It's easy to see that after $n - 1$ operations, there will be left only one number. Your goal is to minimize it. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. The only line of each test case contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$)Β β€” the number of integers written on the board initially. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, in the first line, print the minimum possible number left on the board after $n - 1$ operations. Each of the next $n - 1$ lines should contain two integersΒ β€” numbers $a$ and $b$ chosen and erased in each operation. -----Example----- Input 1 4 Output 2 2 4 3 3 3 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 are given a rectangular field of n Γ— m cells. Each cell is either empty or impassable (contains an obstacle). Empty cells are marked with '.', impassable cells are marked with '*'. Let's call two empty cells adjacent if they share a side. Let's call a connected component any non-extendible set of cells such that any two of them are connected by the path of adjacent cells. It is a typical well-known definition of a connected component. For each impassable cell (x, y) imagine that it is an empty cell (all other cells remain unchanged) and find the size (the number of cells) of the connected component which contains (x, y). You should do it for each impassable cell independently. The answer should be printed as a matrix with n rows and m columns. The j-th symbol of the i-th row should be "." if the cell is empty at the start. Otherwise the j-th symbol of the i-th row should contain the only digit β€”- the answer modulo 10. The matrix should be printed without any spaces. To make your output faster it is recommended to build the output as an array of n strings having length m and print it as a sequence of lines. It will be much faster than writing character-by-character. As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. -----Input----- The first line contains two integers n, m (1 ≀ n, m ≀ 1000) β€” the number of rows and columns in the field. Each of the next n lines contains m symbols: "." for empty cells, "*" for impassable cells. -----Output----- Print the answer as a matrix as described above. See the examples to precise the format of the output. -----Examples----- Input 3 3 *.* .*. *.* Output 3.3 .5. 3.3 Input 4 5 **..* ..*** .*.*. *.*.* Output 46..3 ..732 .6.4. 5.4.3 -----Note----- In first example, if we imagine that the central cell is empty then it will be included to component of size 5 (cross). If any of the corner cell will be empty then it will be included to component of size 3 (corner). 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. Input The only line of input contains three integers a1, a2, a3 (1 ≀ a1, a2, a3 ≀ 20), separated by spaces. Output Output a single integer. Examples Input 2 3 2 Output 5 Input 13 14 1 Output 14 Input 14 5 9 Output 464 Input 17 18 3 Output 53 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. Leha somehow found an array consisting of n integers. Looking at it, he came up with a task. Two players play the game on the array. Players move one by one. The first player can choose for his move a subsegment of non-zero length with an odd sum of numbers and remove it from the array, after that the remaining parts are glued together into one array and the game continues. The second player can choose a subsegment of non-zero length with an even sum and remove it. Loses the one who can not make a move. Who will win if both play optimally? -----Input----- First line of input data contains single integer n (1 ≀ n ≀ 10^6) β€” length of the array. Next line contains n integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 10^9). -----Output----- Output answer in single line. "First", if first player wins, and "Second" otherwise (without quotes). -----Examples----- Input 4 1 3 2 3 Output First Input 2 2 2 Output Second -----Note----- In first sample first player remove whole array in one move and win. In second sample first player can't make a move and lose. 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 are $n$ consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger. The university team for the Olympiad consists of $a$ student-programmers and $b$ student-athletes. Determine the largest number of students from all $a+b$ students, which you can put in the railway carriage so that: no student-programmer is sitting next to the student-programmer; and no student-athlete is sitting next to the student-athlete. In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting. Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all). -----Input----- The first line contain three integers $n$, $a$ and $b$ ($1 \le n \le 2\cdot10^{5}$, $0 \le a, b \le 2\cdot10^{5}$, $a + b > 0$) β€” total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes. The second line contains a string with length $n$, consisting of characters "." and "*". The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member. -----Output----- Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete. -----Examples----- Input 5 1 1 *...* Output 2 Input 6 2 3 *...*. Output 4 Input 11 3 10 .*....**.*. Output 7 Input 3 2 3 *** Output 0 -----Note----- In the first example you can put all student, for example, in the following way: *.AB* In the second example you can put four students, for example, in the following way: *BAB*B In the third example you can put seven students, for example, in the following way: B*ABAB**A*B The letter A means a student-programmer, and the letter B β€” student-athlete. 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 dragon symbolizes wisdom, power and wealth. On Lunar New Year's Day, people model a dragon with bamboo strips and clothes, raise them with rods, and hold the rods high and low to resemble a flying dragon. A performer holding the rod low is represented by a 1, while one holding it high is represented by a 2. Thus, the line of performers can be represented by a sequence a1, a2, ..., an. Little Tommy is among them. He would like to choose an interval [l, r] (1 ≀ l ≀ r ≀ n), then reverse al, al + 1, ..., ar so that the length of the longest non-decreasing subsequence of the new sequence is maximum. A non-decreasing subsequence is a sequence of indices p1, p2, ..., pk, such that p1 < p2 < ... < pk and ap1 ≀ ap2 ≀ ... ≀ apk. The length of the subsequence is k. Input The first line contains an integer n (1 ≀ n ≀ 2000), denoting the length of the original sequence. The second line contains n space-separated integers, describing the original sequence a1, a2, ..., an (1 ≀ ai ≀ 2, i = 1, 2, ..., n). Output Print a single integer, which means the maximum possible length of the longest non-decreasing subsequence of the new sequence. Examples Input 4 1 2 1 2 Output 4 Input 10 1 1 2 2 2 1 1 2 2 1 Output 9 Note In the first example, after reversing [2, 3], the array will become [1, 1, 2, 2], where the length of the longest non-decreasing subsequence is 4. In the second example, after reversing [3, 7], the array will become [1, 1, 1, 1, 2, 2, 2, 2, 2, 1], where the length of the longest non-decreasing subsequence is 9. 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've got string s, consisting of only lowercase English letters. Find its lexicographically maximum subsequence. We'll call a non-empty string s[p1p2... pk] = sp1sp2... spk(1 ≀ p1 < p2 < ... < pk ≀ |s|) a subsequence of string s = s1s2... s|s|. String x = x1x2... x|x| is lexicographically larger than string y = y1y2... y|y|, if either |x| > |y| and x1 = y1, x2 = y2, ... , x|y| = y|y|, or exists such number r (r < |x|, r < |y|), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 > yr + 1. Characters in lines are compared like their ASCII codes. Input The single line contains a non-empty string s, consisting only of lowercase English letters. The string's length doesn't exceed 105. Output Print the lexicographically maximum subsequence of string s. Examples Input ababba Output bbba Input abbcbccacbbcbaaba Output cccccbba Note Let's look at samples and see what the sought subsequences look like (they are marked with uppercase bold letters). The first sample: aBaBBA The second sample: abbCbCCaCbbCBaaBA 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 string $s$ of length $n$ ($1 \le n \le 26$) is called alphabetical if it can be obtained using the following algorithm: first, write an empty string to $s$ (i.e. perform the assignment $s$ := ""); then perform the next step $n$ times; at the $i$-th step take $i$-th lowercase letter of the Latin alphabet and write it either to the left of the string $s$ or to the right of the string $s$ (i.e. perform the assignment $s$ := $c+s$ or $s$ := $s+c$, where $c$ is the $i$-th letter of the Latin alphabet). In other words, iterate over the $n$ first letters of the Latin alphabet starting from 'a' and etc. Each time we prepend a letter to the left of the string $s$ or append a letter to the right of the string $s$. Strings that can be obtained in that way are alphabetical. For example, the following strings are alphabetical: "a", "ba", "ab", "bac" and "ihfcbadeg". The following strings are not alphabetical: "z", "aa", "ca", "acb", "xyz" and "ddcba". From the given string, determine if it is alphabetical. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case is written on a separate line that contains one string $s$. String $s$ consists of lowercase letters of the Latin alphabet and has a length between $1$ and $26$, inclusive. -----Output----- Output $t$ lines, each of them must contain the answer to the corresponding test case. Output YES if the given string $s$ is alphabetical and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive answer). -----Examples----- Input 11 a ba ab bac ihfcbadeg z aa ca acb xyz ddcba Output YES YES YES YES YES NO NO NO NO NO NO -----Note----- The example contains test cases from the main part of the condition. 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 function ```javascript tripledouble(num1,num2) ``` ```python triple_double(num1, num2) ``` which takes numbers `num1` and `num2` and returns `1` if there is a straight triple of a number at any place in `num1` and also a straight double of the **same** number in `num2`. If this isn't the case, return `0` ## Examples ```python triple_double(451999277, 41177722899) == 1 # num1 has straight triple 999s and num2 has straight double 99s triple_double(1222345, 12345) == 0 # num1 has straight triple 2s but num2 has only a single 2 triple_double(12345, 12345) == 0 triple_double(666789, 12345667) == 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. There are many anime that are about "love triangles": Alice loves Bob, and Charlie loves Bob as well, but Alice hates Charlie. You are thinking about an anime which has n characters. The characters are labeled from 1 to n. Every pair of two characters can either mutually love each other or mutually hate each other (there is no neutral state). You hate love triangles (A-B are in love and B-C are in love, but A-C hate each other), and you also hate it when nobody is in love. So, considering any three characters, you will be happy if exactly one pair is in love (A and B love each other, and C hates both A and B), or if all three pairs are in love (A loves B, B loves C, C loves A). You are given a list of m known relationships in the anime. You know for sure that certain pairs love each other, and certain pairs hate each other. You're wondering how many ways you can fill in the remaining relationships so you are happy with every triangle. Two ways are considered different if two characters are in love in one way but hate each other in the other. Print this count modulo 1 000 000 007. -----Input----- The first line of input will contain two integers n, m (3 ≀ n ≀ 100 000, 0 ≀ m ≀ 100 000). The next m lines will contain the description of the known relationships. The i-th line will contain three integers a_{i}, b_{i}, c_{i}. If c_{i} is 1, then a_{i} and b_{i} are in love, otherwise, they hate each other (1 ≀ a_{i}, b_{i} ≀ n, a_{i} β‰  b_{i}, $c_{i} \in \{0,1 \}$). Each pair of people will be described no more than once. -----Output----- Print a single integer equal to the number of ways to fill in the remaining pairs so that you are happy with every triangle modulo 1 000 000 007. -----Examples----- Input 3 0 Output 4 Input 4 4 1 2 1 2 3 1 3 4 0 4 1 0 Output 1 Input 4 4 1 2 1 2 3 1 3 4 0 4 1 1 Output 0 -----Note----- In the first sample, the four ways are to: Make everyone love each other Make 1 and 2 love each other, and 3 hate 1 and 2 (symmetrically, we get 3 ways from this). In the second sample, the only possible solution is to make 1 and 3 love each other and 2 and 4 hate each other. 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 sequence of integers $a_1, a_2, \dots, a_n$. You need to paint elements in colors, so that: If we consider any color, all elements of this color must be divisible by the minimal element of this color. The number of used colors must be minimized. For example, it's fine to paint elements $[40, 10, 60]$ in a single color, because they are all divisible by $10$. You can use any color an arbitrary amount of times (in particular, it is allowed to use a color only once). The elements painted in one color do not need to be consecutive. For example, if $a=[6, 2, 3, 4, 12]$ then two colors are required: let's paint $6$, $3$ and $12$ in the first color ($6$, $3$ and $12$ are divisible by $3$) and paint $2$ and $4$ in the second color ($2$ and $4$ are divisible by $2$). For example, if $a=[10, 7, 15]$ then $3$ colors are required (we can simply paint each element in an unique color). -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$), where $n$ is the length of the given sequence. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 100$). These numbers can contain duplicates. -----Output----- Print the minimal number of colors to paint all the given numbers in a valid way. -----Examples----- Input 6 10 2 3 5 4 2 Output 3 Input 4 100 100 100 100 Output 1 Input 8 7 6 5 4 3 2 2 3 Output 4 -----Note----- In the first example, one possible way to paint the elements in $3$ colors is: paint in the first color the elements: $a_1=10$ and $a_4=5$, paint in the second color the element $a_3=3$, paint in the third color the elements: $a_2=2$, $a_5=4$ and $a_6=2$. In the second example, you can use one color to paint all the elements. In the third example, one possible way to paint the elements in $4$ colors is: paint in the first color the elements: $a_4=4$, $a_6=2$ and $a_7=2$, paint in the second color the elements: $a_2=6$, $a_5=3$ and $a_8=3$, paint in the third color the element $a_3=5$, paint in the fourth color the element $a_1=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. International Abbreviation Olympiad takes place annually starting from 1989. Each year the competition receives an abbreviation of form IAO'y, where y stands for some number of consequent last digits of the current year. Organizers always pick an abbreviation with non-empty string y that has never been used before. Among all such valid abbreviations they choose the shortest one and announce it to be the abbreviation of this year's competition. For example, the first three Olympiads (years 1989, 1990 and 1991, respectively) received the abbreviations IAO'9, IAO'0 and IAO'1, while the competition in 2015 received an abbreviation IAO'15, as IAO'5 has been already used in 1995. You are given a list of abbreviations. For each of them determine the year it stands for. Input The first line of the input contains a single integer n (1 ≀ n ≀ 1000) β€” the number of abbreviations to process. Then n lines follow, each containing a single abbreviation. It's guaranteed that each abbreviation contains at most nine digits. Output For each abbreviation given in the input, find the year of the corresponding Olympiad. Examples Input 5 IAO'15 IAO'2015 IAO'1 IAO'9 IAO'0 Output 2015 12015 1991 1989 1990 Input 4 IAO'9 IAO'99 IAO'999 IAO'9999 Output 1989 1999 2999 9999 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 garland consisting of $n$ lamps. States of the lamps are represented by the string $s$ of length $n$. The $i$-th character of the string $s_i$ equals '0' if the $i$-th lamp is turned off or '1' if the $i$-th lamp is turned on. You are also given a positive integer $k$. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called $k$-periodic if the distance between each pair of adjacent turned on lamps is exactly $k$. Consider the case $k=3$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 25~ 000$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 10^6; 1 \le k \le n$) β€” the length of $s$ and the required period. The second line of the test case contains the string $s$ consisting of $n$ characters '0' and '1'. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ ($\sum n \le 10^6$). -----Output----- For each test case, print the answer β€” the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. -----Example----- Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 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. Kolya loves putting gnomes at the circle table and giving them coins, and Tanya loves studying triplets of gnomes, sitting in the vertexes of an equilateral triangle. More formally, there are 3n gnomes sitting in a circle. Each gnome can have from 1 to 3 coins. Let's number the places in the order they occur in the circle by numbers from 0 to 3n - 1, let the gnome sitting on the i-th place have a_{i} coins. If there is an integer i (0 ≀ i < n) such that a_{i} + a_{i} + n + a_{i} + 2n β‰  6, then Tanya is satisfied. Count the number of ways to choose a_{i} so that Tanya is satisfied. As there can be many ways of distributing coins, print the remainder of this number modulo 10^9 + 7. Two ways, a and b, are considered distinct if there is index i (0 ≀ i < 3n), such that a_{i} β‰  b_{i} (that is, some gnome got different number of coins in these two ways). -----Input----- A single line contains number n (1 ≀ n ≀ 10^5) β€” the number of the gnomes divided by three. -----Output----- Print a single number β€” the remainder of the number of variants of distributing coins that satisfy Tanya modulo 10^9 + 7. -----Examples----- Input 1 Output 20 Input 2 Output 680 -----Note----- 20 ways for n = 1 (gnome with index 0 sits on the top of the triangle, gnome 1 on the right vertex, gnome 2 on the left vertex): [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. You are given a problemset consisting of $n$ problems. The difficulty of the $i$-th problem is $a_i$. It is guaranteed that all difficulties are distinct and are given in the increasing order. You have to assemble the contest which consists of some problems of the given problemset. In other words, the contest you have to assemble should be a subset of problems (not necessary consecutive) of the given problemset. There is only one condition that should be satisfied: for each problem but the hardest one (the problem with the maximum difficulty) there should be a problem with the difficulty greater than the difficulty of this problem but not greater than twice the difficulty of this problem. In other words, let $a_{i_1}, a_{i_2}, \dots, a_{i_p}$ be the difficulties of the selected problems in increasing order. Then for each $j$ from $1$ to $p-1$ $a_{i_{j + 1}} \le a_{i_j} \cdot 2$ should hold. It means that the contest consisting of only one problem is always valid. Among all contests satisfying the condition above you have to assemble one with the maximum number of problems. Your task is to find this number of problems. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of problems in the problemset. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β€” difficulties of the problems. It is guaranteed that difficulties of the problems are distinct and are given in the increasing order. -----Output----- Print a single integer β€” maximum number of problems in the contest satisfying the condition in the problem statement. -----Examples----- Input 10 1 2 5 6 7 10 21 23 24 49 Output 4 Input 5 2 10 50 110 250 Output 1 Input 6 4 7 12 100 150 199 Output 3 -----Note----- Description of the first example: there are $10$ valid contests consisting of $1$ problem, $10$ valid contests consisting of $2$ problems ($[1, 2], [5, 6], [5, 7], [5, 10], [6, 7], [6, 10], [7, 10], [21, 23], [21, 24], [23, 24]$), $5$ valid contests consisting of $3$ problems ($[5, 6, 7], [5, 6, 10], [5, 7, 10], [6, 7, 10], [21, 23, 24]$) and a single valid contest consisting of $4$ problems ($[5, 6, 7, 10]$). In the second example all the valid contests consist of $1$ problem. In the third example are two contests consisting of $3$ problems: $[4, 7, 12]$ and $[100, 150, 199]$. 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 company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received n resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the number of variants of group composition to evaluate. -----Input----- The only line of the input contains one integer n (7 ≀ n ≀ 777) β€” the number of potential employees that sent resumes. -----Output----- Output one integer β€” the number of different variants of group composition. -----Examples----- Input 7 Output 29 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 an integer $n$ and an integer $k$. In one step you can do one of the following moves: decrease $n$ by $1$; divide $n$ by $k$ if $n$ is divisible by $k$. For example, if $n = 27$ and $k = 3$ you can do the following steps: $27 \rightarrow 26 \rightarrow 25 \rightarrow 24 \rightarrow 8 \rightarrow 7 \rightarrow 6 \rightarrow 2 \rightarrow 1 \rightarrow 0$. You are asked to calculate the minimum number of steps to reach $0$ from $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 100$) β€” the number of queries. The only line of each query contains two integers $n$ and $k$ ($1 \le n \le 10^{18}$, $2 \le k \le 10^{18}$). -----Output----- For each query print the minimum number of steps to reach $0$ from $n$ in single line. -----Example----- Input 2 59 3 1000000000000000000 10 Output 8 19 -----Note----- Steps for the first test case are: $59 \rightarrow 58 \rightarrow 57 \rightarrow 19 \rightarrow 18 \rightarrow 6 \rightarrow 2 \rightarrow 1 \rightarrow 0$. In the second test case you have to divide $n$ by $k$ $18$ times and then decrease $n$ by $1$. 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 N lamps are placed in a line, some are switched on and some are off. What is the smallest number of lamps that need to be switched so that on and off lamps will alternate with each other? You are given an array `a` of zeros and ones - `1` mean switched-on lamp and `0` means switched-off. Your task is to find the smallest number of lamps that need to be switched. # Example For `a = [1, 0, 0, 1, 1, 1, 0]`, the result should be `3`. ``` a --> 1 0 0 1 1 1 0 swith --> 0 1 0 became--> 0 1 0 1 0 1 0 ``` # Input/Output - `[input]` integer array `a` array of zeros and ones - initial lamp setup, 1 mean switched-on lamp and 0 means switched-off. `2 < a.length <= 1000` - `[output]` an integer minimum number of switches. 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, capitalize the letters that occupy even indexes and odd indexes separately, and return as shown below. Index `0` will be considered even. For example, `capitalize("abcdef") = ['AbCdEf', 'aBcDeF']`. See test cases for more examples. The input will be a lowercase string with no spaces. Good luck! If you like this Kata, please try: [Indexed capitalization](https://www.codewars.com/kata/59cfc09a86a6fdf6df0000f1) [Even-odd disparity](https://www.codewars.com/kata/59c62f1bdcc40560a2000060) 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. Sato, a botanist, invented a number of special fertilizers for seedlings. When you give the fertilizer to the seedlings, the size of the seedlings changes in a blink of an eye. However, it was found that fertilizer has the following side effects. * The size of the seedlings does not change with the fertilizer given the first time. * From the second time onward, the seedlings will be affected by the combination of the fertilizer given at that time and the fertilizer given immediately before. Saplings may grow if they have a positive effect, and shrink if they have a negative effect. As a test, Dr. Sato said that for three types of fertilizers (fertilizers 1, 2, and 3), seedling growth was achieved by combining the fertilizer given at a certain point (this fertilizer) and the fertilizer given immediately before (previous fertilizer). We investigated the degree and created the following "growth table". The first row of the table on the right is the number of the fertilizer given this time, and the first column is the number of the fertilizer given immediately before. Other numbers indicate the growth rate (ratio of post-growth to pre-growth size) of seedlings due to the combination of the fertilizer given immediately before and the fertilizer given this time. A growth rate of> 1.0 indicates that the sapling grows, and a growth rate of <1.0 indicates that the sapling shrinks. For example, fertilizer 1 followed by fertilizer 2 triples the size of the sapling, but fertilizer 1 followed by fertilizer 3 reduces the size of the sapling in half. | <image> --- | --- If the number of fertilizers given to the seedlings is limited, which fertilizer should be given and in what order to grow the seedlings as large as possible? The "Growth Table" will tell you the answer. As an example, if you give the fertilizers shown in the table above only three times, the seedlings will grow the most if you give them in the order of fertilizer 3 β†’ fertilizer 1 β†’ fertilizer 2 as shown below. <image> * The size of the sapling does not change with the first fertilizer (fertilizer 3). * In the second fertilizer (fertilizer 1), the growth rate of fertilizer 1 after fertilizer 3 is 3.0 from the table, so the size of the seedling is 3.0 times that of the previous time. * In the third fertilizer (fertilizer 2), the growth rate of fertilizer 2 after fertilizer 1 is 3.0 from the table, so the size of the seedlings is 3.0 times the previous time and 9.0 times the initial 3.0 x 3.0. .. This time, Dr. Sato examined all the n types of fertilizers he invented and created a "growth table" like the one above, but it turned out to be a very large table, and he decided on the types of fertilizers and the order in which they were given. I am having a hard time. Therefore, instead of the doctor, I created a program to find the maximum size of the seedling after applying fertilizer m times by inputting the growth value part in the "growth table" of the seedling by combining n kinds of fertilizer. Please give me. However, the size of the first seedling is 1, and the growth rate of the fertilizer given the first time is 1.0 for all fertilizers. Fertilizers are numbered from 1 to n. Input A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format: n m g11 g12 ... g1n g21 g22 ... g2n :: gn1 gn2 ... gnn The first line gives the number of fertilizer types n (2 ≀ n ≀ 100) and the number of fertilizer applications m (1 ≀ m ≀ 100). The next n lines give the seedling growth gij (0.0 ≀ gij ≀ 10.0, real number) when fertilizer i is followed by fertilizer j. The number of datasets does not exceed 20. Output Outputs the maximum sapling size on one line for each dataset. For the size of the sapling to be output, round off to the second decimal place. Example Input 3 3 1.3 3.0 0.5 2.4 2.1 1.0 3.0 0.8 1.2 2 2 1.0 1.0 1.0 1.0 0 0 Output 9.00 1.00 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. Ayush is a cashier at the shopping center. Recently his department has started a ''click and collect" service which allows users to shop online. The store contains k items. n customers have already used the above service. Each user paid for m items. Let a_{ij} denote the j-th item in the i-th person's order. Due to the space limitations all the items are arranged in one single row. When Ayush receives the i-th order he will find one by one all the items a_{ij} (1 ≀ j ≀ m) in the row. Let pos(x) denote the position of the item x in the row at the moment of its collection. Then Ayush takes time equal to pos(a_{i}1) + pos(a_{i}2) + ... + pos(a_{im}) for the i-th customer. When Ayush accesses the x-th element he keeps a new stock in the front of the row and takes away the x-th element. Thus the values are updating. Your task is to calculate the total time it takes for Ayush to process all the orders. You can assume that the market has endless stock. -----Input----- The first line contains three integers n, m and k (1 ≀ n, k ≀ 100, 1 ≀ m ≀ k) β€” the number of users, the number of items each user wants to buy and the total number of items at the market. The next line contains k distinct integers p_{l} (1 ≀ p_{l} ≀ k) denoting the initial positions of the items in the store. The items are numbered with integers from 1 to k. Each of the next n lines contains m distinct integers a_{ij} (1 ≀ a_{ij} ≀ k) β€” the order of the i-th person. -----Output----- Print the only integer t β€” the total time needed for Ayush to process all the orders. -----Example----- Input 2 2 5 3 4 1 2 5 1 5 3 1 Output 14 -----Note----- Customer 1 wants the items 1 and 5. pos(1) = 3, so the new positions are: [1, 3, 4, 2, 5]. pos(5) = 5, so the new positions are: [5, 1, 3, 4, 2]. Time taken for the first customer is 3 + 5 = 8. Customer 2 wants the items 3 and 1. pos(3) = 3, so the new positions are: [3, 5, 1, 4, 2]. pos(1) = 3, so the new positions are: [1, 3, 5, 4, 2]. Time taken for the second customer is 3 + 3 = 6. Total time is 8 + 6 = 14. Formally pos(x) is the index of x in the current row. 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 an array containing cats and dogs. Each dog can catch only one cat, but cannot catch a cat that is more than `n` elements away. Your task will be to return the maximum number of cats that can be caught. For example: ```Haskell solve(['D','C','C','D','C'], 2) = 2, because the dog at index 0 (D0) catches C1 and D3 catches C4. solve(['C','C','D','D','C','D'], 2) = 3, because D2 catches C0, D3 catches C1 and D5 catches C4. solve(['C','C','D','D','C','D'], 1) = 2, because D2 catches C1, D3 catches C4. C0 cannot be caught because n == 1. solve(['D','C','D','D','C'], 1) = 2, too many dogs, so all cats get caught! ``` Do not modify the input array. More examples in the test cases. Good luck! 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 are n animals in the queue to Dr. Dolittle. When an animal comes into the office, the doctor examines him, gives prescriptions, appoints tests and may appoint extra examination. Doc knows all the forest animals perfectly well and therefore knows exactly that the animal number i in the queue will have to visit his office exactly ai times. We will assume that an examination takes much more time than making tests and other extra procedures, and therefore we will assume that once an animal leaves the room, it immediately gets to the end of the queue to the doctor. Of course, if the animal has visited the doctor as many times as necessary, then it doesn't have to stand at the end of the queue and it immediately goes home. Doctor plans to go home after receiving k animals, and therefore what the queue will look like at that moment is important for him. Since the doctor works long hours and she can't get distracted like that after all, she asked you to figure it out. Input The first line of input data contains two space-separated integers n and k (1 ≀ n ≀ 105, 0 ≀ k ≀ 1014). In the second line are given space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 109). Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator). Output If the doctor will overall carry out less than k examinations, print a single number "-1" (without quotes). Otherwise, print the sequence of numbers β€” number of animals in the order in which they stand in the queue. Note that this sequence may be empty. This case is present in pretests. You can just print nothing or print one "End of line"-character. Both will be accepted. Examples Input 3 3 1 2 1 Output 2 Input 4 10 3 3 2 1 Output -1 Input 7 10 1 3 3 1 2 3 1 Output 6 2 3 Note In the first sample test: * Before examination: {1, 2, 3} * After the first examination: {2, 3} * After the second examination: {3, 2} * After the third examination: {2} In the second sample test: * Before examination: {1, 2, 3, 4, 5, 6, 7} * After the first examination: {2, 3, 4, 5, 6, 7} * After the second examination: {3, 4, 5, 6, 7, 2} * After the third examination: {4, 5, 6, 7, 2, 3} * After the fourth examination: {5, 6, 7, 2, 3} * After the fifth examination: {6, 7, 2, 3, 5} * After the sixth examination: {7, 2, 3, 5, 6} * After the seventh examination: {2, 3, 5, 6} * After the eighth examination: {3, 5, 6, 2} * After the ninth examination: {5, 6, 2, 3} * After the tenth examination: {6, 2, 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. You are given n segments on the coordinate axis Ox and the number k. The point is satisfied if it belongs to at least k segments. Find the smallest (by the number of segments) set of segments on the coordinate axis Ox which contains all satisfied points and no others. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 106) β€” the number of segments and the value of k. The next n lines contain two integers li, ri ( - 109 ≀ li ≀ ri ≀ 109) each β€” the endpoints of the i-th segment. The segments can degenerate and intersect each other. The segments are given in arbitrary order. Output First line contains integer m β€” the smallest number of segments. Next m lines contain two integers aj, bj (aj ≀ bj) β€” the ends of j-th segment in the answer. The segments should be listed in the order from left to right. Examples Input 3 2 0 5 -3 2 3 8 Output 2 0 2 3 5 Input 3 2 0 5 -3 3 3 8 Output 1 0 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. There are a number of ways to shuffle a deck of cards. Riffle shuffle is one such example. The following is how to perform riffle shuffle. There is a deck of n cards. First, we divide it into two decks; deck A which consists of the top half of it and deck B of the bottom half. Deck A will have one more card when n is odd. Next, c cards are pulled from bottom of deck A and are stacked on deck C, which is empty initially. Then c cards are pulled from bottom of deck B and stacked on deck C, likewise. This operation is repeated until deck A and B become empty. When the number of cards of deck A(B) is less than c, all cards are pulled. Finally we obtain shuffled deck C. See an example below: - A single riffle operation where n = 9, c = 3 for given deck [0 1 2 3 4 5 6 7 8] (right is top) - Step 0 deck A [4 5 6 7 8] deck B [0 1 2 3] deck C [] - Step 1 deck A [7 8] deck B [0 1 2 3] deck C [4 5 6] - Step 2 deck A [7 8] deck B [3] deck C [4 5 6 0 1 2] - Step 3 deck A [] deck B [3] deck C [4 5 6 0 1 2 7 8] - Step 4 deck A [] deck B [] deck C [4 5 6 0 1 2 7 8 3] shuffled deck [4 5 6 0 1 2 7 8 3] This operation, called riffle operation, is repeated several times. Write a program that simulates Riffle shuffle and answer which card will be finally placed on the top of the deck. Input The input consists of multiple data sets. Each data set starts with a line containing two positive integers n(1 ≀ n ≀ 50) and r(1 ≀ r ≀ 50); n and r are the number of cards in the deck and the number of riffle operations, respectively. r more positive integers follow, each of which represents a riffle operation. These riffle operations are performed in the listed order. Each integer represents c, which is explained above. The end of the input is indicated by EOF. The number of data sets is less than 100. Output For each data set in the input, your program should print the number of the top card after the shuffle. Assume that at the beginning the cards are numbered from 0 to n-1, from the bottom to the top. Each number should be written in a sparate line without any superfluous characters such as leading or following spaces. Example Input 9 1 3 9 4 1 2 3 4 Output 3 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. There is enough money available on ATM in nominal value 10, 20, 50, 100, 200 and 500 dollars. You are given money in nominal value of `n` with `1<=n<=1500`. Try to find minimal number of notes that must be used to repay in dollars, or output -1 if it is impossible. Good Luck!!! 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. Mr. Bender has a digital table of size n Γ— n, each cell can be switched on or off. He wants the field to have at least c switched on squares. When this condition is fulfilled, Mr Bender will be happy. We'll consider the table rows numbered from top to bottom from 1 to n, and the columns β€” numbered from left to right from 1 to n. Initially there is exactly one switched on cell with coordinates (x, y) (x is the row number, y is the column number), and all other cells are switched off. Then each second we switch on the cells that are off but have the side-adjacent cells that are on. For a cell with coordinates (x, y) the side-adjacent cells are cells with coordinates (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1). In how many seconds will Mr. Bender get happy? Input The first line contains four space-separated integers n, x, y, c (1 ≀ n, c ≀ 109; 1 ≀ x, y ≀ n; c ≀ n2). Output In a single line print a single integer β€” the answer to the problem. Examples Input 6 4 3 1 Output 0 Input 9 3 8 10 Output 2 Note Initially the first test has one painted cell, so the answer is 0. In the second test all events will go as is shown on the figure. <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. You are given an array $a$ consisting of $n$ integers. Let's denote monotonic renumeration of array $a$ as an array $b$ consisting of $n$ integers such that all of the following conditions are met: $b_1 = 0$; for every pair of indices $i$ and $j$ such that $1 \le i, j \le n$, if $a_i = a_j$, then $b_i = b_j$ (note that if $a_i \ne a_j$, it is still possible that $b_i = b_j$); for every index $i \in [1, n - 1]$ either $b_i = b_{i + 1}$ or $b_i + 1 = b_{i + 1}$. For example, if $a = [1, 2, 1, 2, 3]$, then two possible monotonic renumerations of $a$ are $b = [0, 0, 0, 0, 0]$ and $b = [0, 0, 0, 0, 1]$. Your task is to calculate the number of different monotonic renumerations of $a$. The answer may be large, so print it modulo $998244353$. -----Input----- The first line contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β€” the number of elements in $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print one integer β€” the number of different monotonic renumerations of $a$, taken modulo $998244353$. -----Examples----- Input 5 1 2 1 2 3 Output 2 Input 2 100 1 Output 2 Input 4 1 3 3 7 Output 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.