question
large_stringlengths
265
13.2k
Solve the programming task below in a Python markdown code block. You are given a permutation $p$ of $n$ integers $1$, $2$, ..., $n$ (a permutation is an array where each element from $1$ to $n$ occurs exactly once). Let's call some subsegment $p[l, r]$ of this permutation special if $p_l + p_r = \max \limits_{i = l}^{r} p_i$. Please calculate the number of special subsegments. -----Input----- The first line contains one integer $n$ ($3 \le n \le 2 \cdot 10^5$). The second line contains $n$ integers $p_1$, $p_2$, ..., $p_n$ ($1 \le p_i \le n$). All these integers are pairwise distinct. -----Output----- Print the number of special subsegments of the given permutation. -----Examples----- Input 5 3 4 1 5 2 Output 2 Input 3 1 3 2 Output 1 -----Note----- Special subsegments in the first example are $[1, 5]$ and $[1, 3]$. The only special subsegment in the second example is $[1, 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. # Fourier transformations are hard. Fouriest transformations are harder. This Kata is based on the SMBC Comic on fourier transformations. A fourier transformation on a number is one that converts the number to a base in which it has more `4`s ( `10` in base `6` is `14`, which has `1` four as opposed to none, hence, fourier in base `6` ). A number's fouriest transformation converts it to the base in which it has the most `4`s. For example: `35353` is the fouriest in base `6`: `431401`. This kata requires you to create a method `fouriest` that takes a number and makes it the fouriest, telling us in which base this happened, as follows: ```python fouriest(number) -> "{number} is the fouriest ({fouriest_representation}) in base {base}" ``` ## Important notes * For this kata we don't care about digits greater than `9` ( only `0` to `9` ), so we will represent all digits greater than `9` as `'x'`: `10` in base `11` is `'x'`, `119` in base `20` is `'5x'`, `118` in base `20` is also `'5x'` * When a number has several fouriest representations, we want the one with the LOWEST base ```if:haskell,javascript * Numbers below `9` will not be tested ``` ```if:javascript * A `BigNumber` library has been provided; documentation is [here](https://mikemcl.github.io/bignumber.js/) ``` ## Examples ```python "30 is the fouriest (42) in base 7" "15 is the fouriest (14) in base 11" ``` Write your solution by modifying this code: ```python def fouriest(i): ``` Your solution should implemented in the function "fouriest". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Taro came to a square to look for treasure. There are many treasures buried in this square, but Taro has the latest machines, so he knows everything about where the treasures are buried. Since the square is very wide Taro decided to look for the treasure to decide the area, but the treasure is what treasure does not know immediately whether or not there in the area for a lot. So Taro decided to count the number of treasures in that area. Constraints > 1 ≤ n ≤ 5000 > 1 ≤ m ≤ 5 × 105 > | xi |, | yi | ≤ 109 (1 ≤ i ≤ n) > | xi1 |, | yi1 |, | xi2 |, | yi2 | ≤ 109 (1 ≤ i ≤ m) > xi1 ≤ xi2, yi1 ≤ yi2 (1 ≤ i ≤ m) > * All inputs are given as integers Input > n m > x1 y1 > x2 y2 > ... > xn yn > x11 y11 x12 y12 > x21 y21 x22 y22 > ... > xm1 ym1 xm2 ym2 > * n represents the number of treasures buried in the square * m represents the number of regions to examine * The 2nd to n + 1 lines represent the coordinates where each treasure is buried. * The n + 2nd to n + m + 1 lines represent each area to be examined. * The positive direction of the x-axis represents the east and the positive direction of the y-axis represents the north. * Each region is a rectangle, xi1 and yi1 represent the coordinates of the southwestern apex of the rectangle, and xi2 and yi2 represent the coordinates of the northeastern apex of the rectangle. Output > C1 > C2 > ... > Cm > * Output the number of treasures contained in each area to each line Examples Input 3 1 1 1 2 4 5 3 0 0 5 5 Output 3 Input 4 2 -1 1 0 3 4 0 2 1 -3 1 5 1 4 0 4 0 Output 2 1 Input 2 3 0 0 0 0 -1 -1 1 1 0 0 2 2 1 1 4 4 Output 2 2 0 Input 5 5 10 5 -3 -8 2 11 6 0 -1 3 -3 1 3 13 -1 -1 9 5 -3 -8 10 11 0 0 5 5 -10 -9 15 10 Output 2 2 5 0 4 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Passer ratings are the generally accepted standard for evaluating NFL quarterbacks. I knew a rating of 100 is pretty good, but never knew what makes up the rating. So out of curiosity I took a look at the wikipedia page and had an idea or my first kata: https://en.wikipedia.org/wiki/Passer_rating ## Formula There are four parts to the NFL formula: ```python A = ((Completions / Attempts) - .3) * 5 B = ((Yards / Attempts) - 3) * .25 C = (Touchdowns / Attempt) * 20 D = 2.375 - ((Interceptions / Attempts) * 25) ``` However, if the result of any calculation is greater than `2.375`, it is set to `2.375`. If the result is a negative number, it is set to zero. Finally the passer rating is: `((A + B + C + D) / 6) * 100` Return the rating rounded to the nearest tenth. ## Example Last year Tom Brady had 432 attempts, 3554 yards, 291 completions, 28 touchdowns, and 2 interceptions. His passer rating was 112.2 Happy coding! Write your solution by modifying this code: ```python def passer_rating(att, yds, comp, td, ints): ``` Your solution should implemented in the function "passer_rating". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. In Berland recently a new collection of toys went on sale. This collection consists of 10^9 types of toys, numbered with integers from 1 to 10^9. A toy from the new collection of the i-th type costs i bourles. Tania has managed to collect n different types of toys a_1, a_2, ..., a_{n} from the new collection. Today is Tanya's birthday, and her mother decided to spend no more than m bourles on the gift to the daughter. Tanya will choose several different types of toys from the new collection as a gift. Of course, she does not want to get a type of toy which she already has. Tanya wants to have as many distinct types of toys in her collection as possible as the result. The new collection is too diverse, and Tanya is too little, so she asks you to help her in this. -----Input----- The first line contains two integers n (1 ≤ n ≤ 100 000) and m (1 ≤ m ≤ 10^9) — the number of types of toys that Tanya already has and the number of bourles that her mom is willing to spend on buying new toys. The next line contains n distinct integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9) — the types of toys that Tanya already has. -----Output----- In the first line print a single integer k — the number of different types of toys that Tanya should choose so that the number of different types of toys in her collection is maximum possible. Of course, the total cost of the selected toys should not exceed m. In the second line print k distinct space-separated integers t_1, t_2, ..., t_{k} (1 ≤ t_{i} ≤ 10^9) — the types of toys that Tanya should choose. If there are multiple answers, you may print any of them. Values of t_{i} can be printed in any order. -----Examples----- Input 3 7 1 3 4 Output 2 2 5 Input 4 14 4 6 12 8 Output 4 7 2 3 1 -----Note----- In the first sample mom should buy two toys: one toy of the 2-nd type and one toy of the 5-th type. At any other purchase for 7 bourles (assuming that the toys of types 1, 3 and 4 have already been bought), it is impossible to buy two and more toys. 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. Decimal numbers are a common notation system currently in use and use ten symbols 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 to represent all numbers. Binary numbers are a popular notation in the computer world and use two symbols, 0 and 1, to represent all numbers. Only the four numbers 0, 1, 2, and 3 are used in quaternary numbers. In quaternary numbers, when the number is incremented from 0, it will be carried to the next digit when it reaches 4. Therefore, the decimal number 4 is carried to the expression "10". Decimal | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | ... --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |- --- Binary | 0 | 1 | 10 | 11 | 100 | 101 | 110 | 111 | 1000 | 101 | 1010 | ... Quadrant | 0 | 1 | 2 | 3 | 10 | 11 | 12 | 13 | 20 | 21 | 22 | ... In Hawaii, fish and taro were counted between fingers in the old days, so it seems that they used quaternary numbers instead of decimal numbers. Create a program that converts the integer n input in decimal to decimal and outputs it. Input A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of -1. One integer n (0 ≤ n ≤ 1000000) is given on one row for each dataset. The number of datasets does not exceed 2000. Output The result of conversion to quaternary number for each input data set is output on one line. Example Input 7 4 0 12 10 10000 -1 Output 13 10 0 30 22 2130100 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Your colleagues have been good enough(?) to buy you a birthday gift. Even though it is your birthday and not theirs, they have decided to play pass the parcel with it so that everyone has an even chance of winning. There are multiple presents, and you will receive one, but not all are nice... One even explodes and covers you in soil... strange office. To make up for this one present is a dog! Happy days! (do not buy dogs as presents, and if you do, never wrap them). Depending on the number of passes in the game (y), and the present you unwrap (x), return as follows: x == goodpresent --> return x with num of passes added to each charCode (turn to charCode, add y to each, turn back) x == crap || x == empty --> return string sorted alphabetically x == bang --> return string turned to char codes, each code reduced by number of passes and summed to a single figure x == badpresent --> return 'Take this back!' x == dog, return 'pass out from excitement y times' (where y is the value given for y). Write your solution by modifying this code: ```python def present(x,y): ``` Your solution should implemented in the function "present". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Find places where a string P is found within a text T. Print all indices of T where P found. The indices of T start with 0. Constraints * 1 ≤ length of T ≤ 1000000 * 1 ≤ length of P ≤ 10000 * The input consists of alphabetical characters and digits Input In the first line, a text T is given. In the second line, a string P is given. Output Print an index of T where P found in a line. Print the indices in ascending order. Examples Input aabaaa aa Output 0 3 4 Input xyzz yz Output 1 Input abc xyz Output 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 Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below. The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1, a2, ..., ak (i.e. some prefix of the sequence) equal to zero for some fixed k (k < n), and this should be done in the smallest possible number of moves. One move is choosing an integer i (1 ≤ i ≤ n) such that ai > 0 and an integer t (t ≥ 0) such that i + 2t ≤ n. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of ai + 2t is increased by 1. For example, let n = 4 and a = (1, 0, 1, 2), then it is possible to make move i = 3, t = 0 and get a = (1, 0, 0, 3) or to make move i = 1, t = 1 and get a = (0, 0, 2, 2) (the only possible other move is i = 1, t = 0). You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1 ≤ k < n). Input The first input line contains a single integer n. The second line contains n integers ai (0 ≤ ai ≤ 104), separated by single spaces. The input limitations for getting 20 points are: * 1 ≤ n ≤ 300 The input limitations for getting 50 points are: * 1 ≤ n ≤ 2000 The input limitations for getting 100 points are: * 1 ≤ n ≤ 105 Output Print exactly n - 1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams, or the %I64d specifier. Examples Input 4 1 0 1 2 Output 1 1 3 Input 8 1 2 3 4 5 6 7 8 Output 1 3 6 10 16 24 40 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. Sakuzyo - Imprinting A.R.C. Markland-N is a tall building with $n$ floors numbered from $1$ to $n$. Between each two adjacent floors in the building, there is a staircase connecting them. It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal. ConneR's office is at floor $s$ of the building. On each floor (including floor $s$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $k$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there. CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant. Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way! -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases in the test. Then the descriptions of $t$ test cases follow. The first line of a test case contains three integers $n$, $s$ and $k$ ($2 \le n \le 10^9$, $1 \le s \le n$, $1 \le k \le \min(n-1, 1000)$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $k$ distinct integers $a_1, a_2, \ldots, a_k$ ($1 \le a_i \le n$) — the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $k$ over all test cases does not exceed $1000$. -----Output----- For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $s$ to a floor with an open restaurant. -----Example----- Input 5 5 2 3 1 2 3 4 3 3 4 1 2 10 2 6 1 2 3 4 5 7 2 1 1 2 100 76 8 76 75 36 67 41 74 10 77 Output 2 0 4 0 2 -----Note----- In the first example test case, the nearest floor with an open restaurant would be the floor $4$. In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere. In the third example test case, the closest open restaurant is on the $6$-th floor. 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 Yes, your eyes are no problem, this is toLoverCase (), not toLowerCase (), we want to make the world full of love. ### What do we need to do? You need to add a prototype function to the String, the name is toLoverCase. Function can convert the letters in the string, converted to "L", "O", "V", "E", if not the letter, don't change it. ### How to convert? : ``` "love!".toLoverCase()="EVOL!" ``` more example see the testcases. ### Series: - [Bug in Apple](http://www.codewars.com/kata/56fe97b3cc08ca00e4000dc9) - [Father and Son](http://www.codewars.com/kata/56fe9a0c11086cd842000008) - [Jumping Dutch act](http://www.codewars.com/kata/570bcd9715944a2c8e000009) - [Planting Trees](http://www.codewars.com/kata/5710443187a36a9cee0005a1) - [Give me the equation](http://www.codewars.com/kata/56fe9b65cc08cafbc5000de3) - [Find the murderer](http://www.codewars.com/kata/570f3fc5b29c702c5500043e) - [Reading a Book](http://www.codewars.com/kata/570ca6a520c69f39dd0016d4) - [Eat watermelon](http://www.codewars.com/kata/570df12ce6e9282a7d000947) - [Special factor](http://www.codewars.com/kata/570e5d0b93214b1a950015b1) - [Guess the Hat](http://www.codewars.com/kata/570ef7a834e61306da00035b) - [Symmetric Sort](http://www.codewars.com/kata/5705aeb041e5befba20010ba) - [Are they symmetrical?](http://www.codewars.com/kata/5705cc3161944b10fd0004ba) - [Max Value](http://www.codewars.com/kata/570771871df89cf59b000742) - [Trypophobia](http://www.codewars.com/kata/56fe9ffbc25bf33fff000f7c) - [Virus in Apple](http://www.codewars.com/kata/5700af83d1acef83fd000048) - [Balance Attraction](http://www.codewars.com/kata/57033601e55d30d3e0000633) - [Remove screws I](http://www.codewars.com/kata/5710a50d336aed828100055a) - [Remove screws II](http://www.codewars.com/kata/5710a8fd336aed00d9000594) - [Regular expression compression](http://www.codewars.com/kata/570bae4b0237999e940016e9) - [Collatz Array(Split or merge)](http://www.codewars.com/kata/56fe9d579b7bb6b027000001) - [Tidy up the room](http://www.codewars.com/kata/5703ace6e55d30d3e0001029) - [Waiting for a Bus](http://www.codewars.com/kata/57070eff924f343280000015) Write your solution by modifying this code: ```python def to_lover_case(string): ``` Your solution should implemented in the function "to_lover_case". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Jon Snow is on the lookout for some orbs required to defeat the white walkers. There are k different types of orbs and he needs at least one of each. One orb spawns daily at the base of a Weirwood tree north of the wall. The probability of this orb being of any kind is equal. As the north of wall is full of dangers, he wants to know the minimum number of days he should wait before sending a ranger to collect the orbs such that the probability of him getting at least one of each kind of orb is at least $\frac{p_{i} - \epsilon}{2000}$, where ε < 10^{ - 7}. To better prepare himself, he wants to know the answer for q different values of p_{i}. Since he is busy designing the battle strategy with Sam, he asks you for your help. -----Input----- First line consists of two space separated integers k, q (1 ≤ k, q ≤ 1000) — number of different kinds of orbs and number of queries respectively. Each of the next q lines contain a single integer p_{i} (1 ≤ p_{i} ≤ 1000) — i-th query. -----Output----- Output q lines. On i-th of them output single integer — answer for i-th query. -----Examples----- Input 1 1 1 Output 1 Input 2 2 1 2 Output 2 2 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You have to create a method "compoundArray" which should take as input two int arrays of different length and return one int array with numbers of both arrays shuffled one by one. ```Example: Input - {1,2,3,4,5,6} and {9,8,7,6} Output - {1,9,2,8,3,7,4,6,5,6} ``` Write your solution by modifying this code: ```python def compound_array(a, b): ``` Your solution should implemented in the function "compound_array". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Given a sequence of integers a_1, ..., a_{n} and q queries x_1, ..., x_{q} on it. For each query x_{i} you have to count the number of pairs (l, r) such that 1 ≤ l ≤ r ≤ n and gcd(a_{l}, a_{l} + 1, ..., a_{r}) = x_{i}. $\operatorname{gcd}(v_{1}, v_{2}, \ldots, v_{n})$ is a greatest common divisor of v_1, v_2, ..., v_{n}, that is equal to a largest positive integer that divides all v_{i}. -----Input----- The first line of the input contains integer n, (1 ≤ n ≤ 10^5), denoting the length of the sequence. The next line contains n space separated integers a_1, ..., a_{n}, (1 ≤ a_{i} ≤ 10^9). The third line of the input contains integer q, (1 ≤ q ≤ 3 × 10^5), denoting the number of queries. Then follows q lines, each contain an integer x_{i}, (1 ≤ x_{i} ≤ 10^9). -----Output----- For each query print the result in a separate line. -----Examples----- Input 3 2 6 3 5 1 2 3 4 6 Output 1 2 2 0 1 Input 7 10 20 3 15 1000 60 16 10 1 2 3 4 5 6 10 20 60 1000 Output 14 0 2 2 2 0 2 2 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. Read problems statements in Mandarin Chinese and Russian as well. As every other little boy, Mike has a favorite toy to play with. Mike's favorite toy is a set of N disks. The boy likes to compose his disks in stacks, but there's one very important rule: the disks in a single stack must be ordered by their radiuses in a strictly increasing order such that the top-most disk will have the smallest radius. For example, a stack of disks with radii (5, 2, 1) is valid, while a stack of disks with radii (3, 4, 1) is not. Little Mike has recently come up with the following algorithm after the order of disks are given: First, Mike initiates an empty set of disk stacks. Then, Mike processes the disks in the chosen order using the following pattern: If there is at least one stack such that Mike can put the current disk on the top of the stack without making it invalid, then he chooses the stack with the smallest top disk radius strictly greater than the radius of the current disk, and puts the current disk on top of that stack. Otherwise, Mike makes a new stack containing only the current disk. For example, let's assume that the order of the disk radii is (3, 4, 5, 1, 1, 2). Here's how the set of the top stack disks will appear during the algorithm's run: In the beginning of the algorithm, the set of disk stacks is empty. After processing the first disk, the set of top stack disks is {3}. We cannot put the second disk on the only stack that we have after processing the first disk, so we make a new stack. After processing the second disk, the set of top stack disks is {3, 4}. We cannot put the third disk on any of the available stacks, so we make a new stack. After processing the third disk, the set of top stack disks is {3, 4, 5}. The fourth disk has radius 1, so it can be easily put on any of the available stacks. According to the algorithm, we choose the stack with the top disk radius equal to 3. After processing the fourth disk, the set of top stack disks is {1, 4, 5}. The fifth disk has radius 1, so there are two stacks we can put it on. According to the algorithm, we choose the stack with the top disk radius equal to 4. After processing the fifth disk, the set of top stack disks is {1, 1, 5}. The sixth disk has radius 2, so there is only one stack we can put it on. The final set of top stack disks is {1, 1, 2}. Mike is really excited about his new algorithm, but he has so many disks that it seems impossible to simulate the algorithm manually. You are given an array A of N integers denoting the radii of Mike's disks. The disks are already ordered by Mike. Your task is to find the set of the stack top disk radii after the algorithm is done. ------ Input ------ The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of a test description contains a single integer N. The second line of the description contains N integers denoting A_{1}, ... , A_{N}. ------ Output ------ For each test case, output a single line. The line should start with a positive integer S denoting the number of stacks after the algorithm is done. This should be followed by S integers on the same line denoting the stacks' top disk radii in non-decreasing order. If there are multiple correct answers, you are allowed to output any of them. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 6 3 4 5 1 1 2 10 3 2 9 5 2 9 4 14 7 10 8 14 5 13 19 17 10 18 12 ----- Sample Output 1 ------ 3 1 1 2 5 2 2 4 7 10 4 5 10 12 18 ----- explanation 1 ------ Test case $1$: This case is already explained in the problem statement. Test case $2$: After processing the first disk, the set of top stack disks are $[3]$. The second disk can be placed on the first stack. Thus, top stack disks are $[2]$. The next $3$ disks can be stacked together. Thus, the top stack disks are $[2, 2]$. The next two disks can be stacked together to get $[2, 2, 4]$. The next two disks can be stacked together to get $[2, 2, 4, 7]$. The last disk can be put on top of the fifth stack. The final stack top are $[2, 2, 4, 7, 10]$. Thus, $5$ stacks are needed. Test case $3$: After processing the first disk, the set of top stack disks are $[14]$. The second disk can be placed on the first stack. Thus, top stack disks are $[5]$. The next disk can be placed on second stack to get $[5, 13]$. The next $2$ disks can be stacked together. Thus, the top stack disks are $[5, 13, 17]$. The next disk can be placed on second stack to get $[5, 10, 17]$. The next disk can be placed on fourth stack to get $[5, 10, 17, 18]$. The last disk can be put on top of the third stack. The final stack top are $[5, 10, 12, 18]$. Thus, $4$ stacks are 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. Problem Given the integer n, output the smallest m such that nCm (the number of combinations that choose m out of n different ones) is even. Constraints The input satisfies the following conditions. * 1 ≤ n ≤ 1018 Input The input is given in the following format. n Output Output the minimum m such that nCm is an even number on one line. Examples Input 2 Output 1 Input 111 Output 16 Input 3 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.
Solve the programming task below in a Python markdown code block. Write a function that accepts two parameters (a and b) and says whether a is smaller than, bigger than, or equal to b. Here is an example code: var noIfsNoButs = function (a,b) { if(a > b) return a + " is greater than " + b else if(a < b) return a + " is smaller than " + b else if(a == b) return a + " is equal to " + b } There's only one problem... You can't use if statements, and you can't use shorthands like (a < b)?true:false; in fact the word "if" and the character "?" are not allowed in the code. Inputs are guarenteed to be numbers You are always welcome to check out some of my other katas: Very Easy (Kyu 8) Add Numbers Easy (Kyu 7-6) Convert Color image to greyscale Array Transformations Basic Compression Find Primes in Range No Ifs No Buts Medium (Kyu 5-4) Identify Frames In An Image Photoshop Like - Magic Wand Scientific Notation Vending Machine - FSA Find Matching Parenthesis Hard (Kyu 3-2) Ascii Art Generator Write your solution by modifying this code: ```python def no_ifs_no_buts(a, b): ``` Your solution should implemented in the function "no_ifs_no_buts". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. # Task You are a lonely frog. You live on an integer array. The meaning of your life is to jump and jump.. Now, here comes your new task. You are given an integer array `arr` and a positive integer `n`. You will jump following the rules below: - Your initial position at `arr[0]`. arr[0] will always be `0`. - You will jump according to the number of current position(arr[i]) - That is, if the number is a positive integer, let say 3, you will jump forward 3 steps; If the number is a negative integer, let say -3, you will jump backward 3 steps; If the number is 0, you will stay here for a little rest. - The number of current position(arr[i]) will increase decrease by 1 after each turn (jump or stay). - That is, if the number of current position(arr[i]) is greater than or equal to `n`, the number will decrease 1; if the number at current position(arr[i]) is less than `n`, the number will increase 1. You will stop jumping when you reach the exit (your position greater than or equal to arr.length). Please tell me, at the moment, how many elements in `arr` are equal to `n`? # Note - `3 <= arr.length <= 1000`, `1 <= n <= 9` - Frog will never jump backward out of `arr`. # Example For `arr = [0,3,0,1,-3], n = 3`, the output should be `2`. ``` Let us jump softly: [0,3,0,1,-3] ^--You are here, current number is 0, you stay here. currect number 0 < n, so 0 -> 1 [1,3,0,1,-3] ^--You are here, current number is 1, you will jump forward to position 1. current number 1 < n, so 1 -> 2 [2,3,0,1,-3] ^--You are here, current number is 3, you will jump forward to position 4. current number 3 >= n, so 3 -> 2 [2,2,0,1,-3] ^--You are here, current number is -3, you will jump backward to position 1. current number -3 < n, so -3 -> -2 [2,2,0,1,-2] ^--You are here, current number is 2, you will jump forward to position 3. current number 2 < n, so 2 -> 3 [2,3,0,1,-2] ^--You are here, current number is 1, you will jump forward to position 3. current number 1 < n, so 1 -> 2 [2,3,0,2,-2] ^--You are here, current number is -2, you will jump backward to position 2. current number -2 < n, so -2 -> -1 [2,3,0,2,-1] ^--You are here, current number is 0, you stay here. current number 0 < n, so 0 -> 1 [2,3,1,2,-1] ^--You are here, current number is 1, you will jump forward to position 3. current number 1 < n, so 1 -> 2 [2,3,2,2,-1] ^--You are here, current number is 2, you will jump forward to position 5. current number 2 < n, so 2 -> 3 [2,3,2,3,-1] exit ^--You are here, you reach to the exit. At the moment, arr[1] and arr[3] are equal to n. So, the output should be 2. ``` For `arr = [0,-1,-2,-3,-4], n = 4`, the output should be `2`. ``` Let's us jump fast ;-) [0,-1,-2,-3,-4] ^ [1,-1,-2,-3,-4] ^ [2,-1,-2,-3,-4] ^ [2, 0,-2,-3,-4] ^ [3, 0,-2,-3,-4] ^ [3, 0,-1,-3,-4] ^ [4, 0,-1,-3,-4] ^ [4, 0,-1,-2,-4] ^ [3, 0,-1,-2,-4] ^ [3, 0,-1,-2,-3] ^ [4, 0,-1,-2,-3] ^ [4, 0,-1,-1,-3] ^ [4, 1,-1,-1,-3] ^ [4, 2,-1,-1,-3] ^ [4, 2, 0,-1,-3] ^ [4, 3, 0,-1,-3] ^ [4, 3, 0, 0,-3] ^ [4, 3, 1, 0,-3] ^ [4, 3, 2, 0,-3] ^ [4, 3, 2, 1,-3] ^ [4, 3, 2, 2,-3] ^ [4, 3, 2, 2,-2] ^ [4, 4, 2, 2,-2] ^ [4, 4, 2, 2,-1] ^ [4, 4, 3, 2,-1] ^ [4, 4, 3, 2, 0] ^ [4, 4, 3, 3, 0] exit ^ At the moment, arr[0] and arr[1] are equal to n. So, the output should be 2. ``` For `arr = [0,-1,-2,-3,-4], n = 3`, the output should be `0`. ``` Let's jump fast ;-) [0,-1,-2,-3,-4] ^ [1,-1,-2,-3,-4] ^ [2,-1,-2,-3,-4] ^ [2, 0,-2,-3,-4] ^ [3, 0,-2,-3,-4] ^ [3, 0,-1,-3,-4] ^ [2, 0,-1,-3,-4] ^ [2, 0,-1,-2,-4] ^ [3, 0,-1,-2,-4] ^ [3, 0, 0,-2,-4] ^ [3, 1, 0,-2,-4] ^ [3, 2, 0,-2,-4] ^ [3, 2, 1,-2,-4] ^ [3, 2, 2,-2,-4] ^ [3, 2, 2,-1,-4] ^ [3, 3, 2,-1,-4] ^ [3, 3, 2, 0,-4] ^ [3, 3, 3, 0,-4] ^ [3, 3, 3, 0,-3] ^ [2, 3, 3, 0,-3] ^ [2, 3, 3, 1,-3] ^ [2, 3, 3, 2,-3] ^ [2, 3, 3, 2,-2] ^ [2, 2, 3, 2,-2] ^ [2, 2, 3, 2,-1] ^ [2, 2, 2, 2,-1] exit ^ At the moment, there is no element equal to n. So, the output should be 0. ``` [Base idea taken from [here](https://adventofcode.com/2017/day/5)] Write your solution by modifying this code: ```python def jumping(arr, n): ``` Your solution should implemented in the function "jumping". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. We're giving away nice huge bags containing number tiles! A bag we want to present to you contains n tiles. Each of them has a single number written on it — either 1 or 2. However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequence, in any order you wish. We will then compute the sums of all prefixes in the sequence, and then count how many of these sums are prime numbers. If you want to keep the prize, you will need to maximize the number of primes you get. Can you win the prize? Hurry up, the bags are waiting! Input The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of number tiles in the bag. The following line contains n space-separated integers a_1, a_2, ..., a_n (a_i ∈ \{1, 2\}) — the values written on the tiles. Output Output a permutation b_1, b_2, ..., b_n of the input sequence (a_1, a_2, ..., a_n) maximizing the number of the prefix sums being prime numbers. If there are multiple optimal permutations, output any. Examples Input 5 1 2 1 2 1 Output 1 1 1 2 2 Input 9 1 1 2 1 1 1 2 1 1 Output 1 1 1 2 1 1 1 2 1 Note The first solution produces the prefix sums 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, \mathbf{\color{blue}{7}} (four primes constructed), while the prefix sums in the second solution are 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, 6, \mathbf{\color{blue}{7}}, 8, 10, \mathbf{\color{blue}{11}} (five primes). Primes are marked bold and blue. In each of these cases, the number of produced primes is maximum possible. 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 the year 30XX, an expedition team reached a planet and found a warp machine suggesting the existence of a mysterious supercivilization. When you go through one of its entrance gates, you can instantaneously move to the exit irrespective of how far away it is. You can move even to the end of the universe at will with this technology! The scientist team started examining the machine and successfully identified all the planets on which the entrances to the machine were located. Each of these N planets (identified by an index from $1$ to $N$) has an entrance to, and an exit from the warp machine. Each of the entrances and exits has a letter inscribed on it. The mechanism of spatial mobility through the warp machine is as follows: * If you go into an entrance gate labeled with c, then you can exit from any gate with label c. * If you go into an entrance located on the $i$-th planet, then you can exit from any gate located on the $j$-th planet where $i < j$. Once you have reached an exit of the warp machine on a planet, you can continue your journey by entering into the warp machine on the same planet. In this way, you can reach a faraway planet. Our human race has decided to dispatch an expedition to the star $N$, starting from Star $1$ and using the warp machine until it reaches Star $N$. To evaluate the possibility of successfully reaching the destination. it is highly desirable for us to know how many different routes are available for the expedition team to track. Given information regarding the stars, make a program to enumerate the passages from Star $1$ to Star $N$. Input The input is given in the following format. $N$ $s$ $t$ The first line provides the number of the stars on which the warp machine is located $N$ ($2 \leq N \leq 100,000$). The second line provides a string $s$ of length $N$, each component of which represents the letter inscribed on the entrance of the machine on the star. By the same token, the third line provides a string $t$ of length $N$ consisting of the letters inscribed on the exit of the machine. Two strings $s$ and $t$ consist all of lower-case alphabetical letters, and the $i$-th letter of these strings corresponds respectively to the entrance and exit of Star $i$ machine. Output Divide the number of possible routes from Star $1$ to Star $N$ obtained above by 1,000,000,007, and output the remainder. Examples Input 6 abbaba baabab Output 5 Input 25 neihsokcpuziafoytisrevinu universityofaizupckoshien 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.
Solve the programming task below in a Python markdown code block. Nowadays all circuses in Berland have a round arena with diameter 13 meters, but in the past things were different. In Ancient Berland arenas in circuses were shaped as a regular (equiangular) polygon, the size and the number of angles could vary from one circus to another. In each corner of the arena there was a special pillar, and the rope strung between the pillars marked the arena edges. Recently the scientists from Berland have discovered the remains of the ancient circus arena. They found only three pillars, the others were destroyed by the time. You are given the coordinates of these three pillars. Find out what is the smallest area that the arena could have. Input The input file consists of three lines, each of them contains a pair of numbers –– coordinates of the pillar. Any coordinate doesn't exceed 1000 by absolute value, and is given with at most six digits after decimal point. Output Output the smallest possible area of the ancient arena. This number should be accurate to at least 6 digits after the decimal point. It's guaranteed that the number of angles in the optimal polygon is not larger than 100. Examples Input 0.000000 0.000000 1.000000 1.000000 0.000000 1.000000 Output 1.00000000 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$ numbers $a_1, a_2, \dots, a_n$. With a cost of one coin you can perform the following operation: Choose one of these numbers and add or subtract $1$ from it. In particular, we can apply this operation to the same number several times. We want to make the product of all these numbers equal to $1$, in other words, we want $a_1 \cdot a_2$ $\dots$ $\cdot a_n = 1$. For example, for $n = 3$ and numbers $[1, -3, 0]$ we can make product equal to $1$ in $3$ coins: add $1$ to second element, add $1$ to second element again, subtract $1$ from third element, so that array becomes $[1, -1, -1]$. And $1\cdot (-1) \cdot (-1) = 1$. What is the minimum cost we will have to pay to do that? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the number of numbers. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) — the numbers. -----Output----- Output a single number — the minimal number of coins you need to pay to make the product equal to $1$. -----Examples----- Input 2 -1 1 Output 2 Input 4 0 0 0 0 Output 4 Input 5 -5 -3 5 3 0 Output 13 -----Note----- In the first example, you can change $1$ to $-1$ or $-1$ to $1$ in $2$ coins. In the second example, you have to apply at least $4$ operations for the product not to be $0$. In the third example, you can change $-5$ to $-1$ in $4$ coins, $-3$ to $-1$ in $2$ coins, $5$ to $1$ in $4$ coins, $3$ to $1$ in $2$ coins, $0$ to $1$ in $1$ coin. 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 times you recall a good old friend and everything you've come through together. Luckily there are social networks — they store all your message history making it easy to know what you argued over 10 years ago. More formal, your message history is a sequence of messages ordered by time sent numbered from 1 to n where n is the total number of messages in the chat. Each message might contain a link to an earlier message which it is a reply to. When opening a message x or getting a link to it, the dialogue is shown in such a way that k previous messages, message x and k next messages are visible (with respect to message x). In case there are less than k messages somewhere, they are yet all shown. Digging deep into your message history, you always read all visible messages and then go by the link in the current message x (if there is one) and continue reading in the same manner. Determine the number of messages you'll read if your start from message number t for all t from 1 to n. Calculate these numbers independently. If you start with message x, the initial configuration is x itself, k previous and k next messages. Messages read multiple times are considered as one. -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 10^5, 0 ≤ k ≤ n) — the total amount of messages and the number of previous and next messages visible. The second line features a sequence of integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} < i), where a_{i} denotes the i-th message link destination or zero, if there's no link from i. All messages are listed in chronological order. It's guaranteed that the link from message x goes to message with number strictly less than x. -----Output----- Print n integers with i-th denoting the number of distinct messages you can read starting from message i and traversing the links while possible. -----Examples----- Input 6 0 0 1 1 2 3 2 Output 1 2 2 3 3 3 Input 10 1 0 1 0 3 4 5 2 3 7 0 Output 2 3 3 4 5 6 6 6 8 2 Input 2 2 0 1 Output 2 2 -----Note----- Consider i = 6 in sample case one. You will read message 6, then 2, then 1 and then there will be no link to go. In the second sample case i = 6 gives you messages 5, 6, 7 since k = 1, then 4, 5, 6, then 2, 3, 4 and then the link sequence breaks. The number of distinct messages here is equal to 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. Vanya has a table consisting of 100 rows, each row contains 100 cells. The rows are numbered by integers from 1 to 100 from bottom to top, the columns are numbered from 1 to 100 from left to right. In this table, Vanya chose n rectangles with sides that go along borders of squares (some rectangles probably occur multiple times). After that for each cell of the table he counted the number of rectangles it belongs to and wrote this number into it. Now he wants to find the sum of values in all cells of the table and as the table is too large, he asks you to help him find the result. -----Input----- The first line contains integer n (1 ≤ n ≤ 100) — the number of rectangles. Each of the following n lines contains four integers x_1, y_1, x_2, y_2 (1 ≤ x_1 ≤ x_2 ≤ 100, 1 ≤ y_1 ≤ y_2 ≤ 100), where x_1 and y_1 are the number of the column and row of the lower left cell and x_2 and y_2 are the number of the column and row of the upper right cell of a rectangle. -----Output----- In a single line print the sum of all values in the cells of the table. -----Examples----- Input 2 1 1 2 3 2 2 3 3 Output 10 Input 2 1 1 3 3 1 1 3 3 Output 18 -----Note----- Note to the first sample test: Values of the table in the first three rows and columns will be as follows: 121 121 110 So, the sum of values will be equal to 10. Note to the second sample test: Values of the table in the first three rows and columns will be as follows: 222 222 222 So, the sum of values will be equal to 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. A string of length 6 consisting of lowercase English letters is said to be coffee-like if and only if its 3-rd and 4-th characters are equal and its 5-th and 6-th characters are also equal. Given a string S, determine whether it is coffee-like. -----Constraints----- - S is a string of length 6 consisting of lowercase English letters. -----Input----- Input is given from Standard Input in the following format: S -----Output----- If S is coffee-like, print Yes; otherwise, print No. -----Sample Input----- sippuu -----Sample Output----- Yes In sippuu, the 3-rd and 4-th characters are equal, and the 5-th and 6-th characters are also equal. 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 Floral Clock has been standing by the side of Mirror Lake for years. Though unable to keep time, it reminds people of the passage of time and the good old days. On the rim of the Floral Clock are 2n flowers, numbered from 1 to 2n clockwise, each of which has a colour among all n possible ones. For each colour, there are exactly two flowers with it, the distance between which either is less than or equal to 2, or equals n. Additionally, if flowers u and v are of the same colour, then flowers opposite to u and opposite to v should be of the same colour as well — symmetry is beautiful! Formally, the distance between two flowers is 1 plus the number of flowers on the minor arc (or semicircle) between them. Below is a possible arrangement with n = 6 that cover all possibilities. <image> The beauty of an arrangement is defined to be the product of the lengths of flower segments separated by all opposite flowers of the same colour. In other words, in order to compute the beauty, we remove from the circle all flowers that have the same colour as flowers opposite to them. Then, the beauty is the product of lengths of all remaining segments. Note that we include segments of length 0 in this product. If there are no flowers that have the same colour as flower opposite to them, the beauty equals 0. For instance, the beauty of the above arrangement equals 1 × 3 × 1 × 3 = 9 — the segments are {2}, {4, 5, 6}, {8} and {10, 11, 12}. While keeping the constraints satisfied, there may be lots of different arrangements. Find out the sum of beauty over all possible arrangements, modulo 998 244 353. Two arrangements are considered different, if a pair (u, v) (1 ≤ u, v ≤ 2n) exists such that flowers u and v are of the same colour in one of them, but not in the other. Input The first and only line of input contains a lonely positive integer n (3 ≤ n ≤ 50 000) — the number of colours present on the Floral Clock. Output Output one integer — the sum of beauty over all possible arrangements of flowers, modulo 998 244 353. Examples Input 3 Output 24 Input 4 Output 4 Input 7 Output 1316 Input 15 Output 3436404 Note With n = 3, the following six arrangements each have a beauty of 2 × 2 = 4. <image> While many others, such as the left one in the figure below, have a beauty of 0. The right one is invalid, since it's asymmetric. <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. In telecomunications we use information coding to detect and prevent errors while sending data. A parity bit is a bit added to a string of binary code that indicates whether the number of 1-bits in the string is even or odd. Parity bits are used as the simplest form of error detecting code, and can detect a 1 bit error. In this case we are using even parity: the parity bit is set to `0` if the number of `1`-bits is even, and is set to `1` if odd. We are using them for the transfer of ASCII characters in binary (7-bit strings): the parity is added to the end of the 7-bit string, forming the 8th bit. In this Kata you are to test for 1-bit errors and return a new string consisting of all of the correct ASCII caracters **in 7 bit format** (removing the parity bit), or `"error"` in place of ASCII characters in which errors were detected. For more information on parity bits: https://en.wikipedia.org/wiki/Parity_bit ## Examples Correct 7 bit string with an even parity bit as the 8th bit: ``` "01011001" <-- The "1" on the right is the parity bit. ``` In this example, there are three 1-bits. Three is an odd number, and the parity bit is set to `1`. No errors are detected, so return `"0101100"` (7 bits). Example of a string of ASCII characters: ``` "01011001 01101110 01100000 01010110 10001111 01100011" ``` This should return: ``` "0101100 error 0110000 0101011 error 0110001" ``` Write your solution by modifying this code: ```python def parity_bit(binary): ``` Your solution should implemented in the function "parity_bit". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Little Artem found a grasshopper. He brought it to his house and constructed a jumping area for him. The area looks like a strip of cells 1 × n. Each cell contains the direction for the next jump and the length of that jump. Grasshopper starts in the first cell and follows the instructions written on the cells. Grasshopper stops immediately if it jumps out of the strip. Now Artem wants to find out if this will ever happen. -----Input----- The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — length of the strip. Next line contains a string of length n which consists of characters "<" and ">" only, that provide the direction of the jump from the corresponding cell. Next line contains n integers d_{i} (1 ≤ d_{i} ≤ 10^9) — the length of the jump from the i-th cell. -----Output----- Print "INFINITE" (without quotes) if grasshopper will continue his jumps forever. Otherwise print "FINITE" (without quotes). -----Examples----- Input 2 >< 1 2 Output FINITE Input 3 >>< 2 1 1 Output INFINITE -----Note----- In the first sample grasshopper starts from the first cell and jumps to the right on the next cell. When he is in the second cell he needs to jump two cells left so he will jump out of the strip. Second sample grasshopper path is 1 - 3 - 2 - 3 - 2 - 3 and so on. The path is infinite. 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 positive number n > 1 find the prime factor decomposition of n. The result will be a string with the following form : ``` "(p1**n1)(p2**n2)...(pk**nk)" ``` where ```a ** b``` means ```a``` to the power of ```b``` with the p(i) in increasing order and n(i) empty if n(i) is 1. ``` Example: n = 86240 should return "(2**5)(5)(7**2)(11)" ``` Write your solution by modifying this code: ```python def primeFactors(n): ``` Your solution should implemented in the function "primeFactors". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. problem AOR Ika made a set $ S = \\ {a_1, ..., a_N \\} $ and a map $ f: S → S $. $ f (a_i) = b_i $. For any element $ x $ in the set $ S $, all maps $ g, h: S → S $ satisfying $ g (f (x)) = h (f (x)) $ are $ g (x). ) = Determine if h (x) $ is satisfied, and if not, configure one counterexample. Example Input 5 1 2 3 4 5 3 4 2 5 1 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. Given a string containing a list of integers separated by commas, write the function string_to_int_list(s) that takes said string and returns a new list containing all integers present in the string, preserving the order. For example, give the string "-1,2,3,4,5", the function string_to_int_list() should return [-1,2,3,4,5] Please note that there can be one or more consecutive commas whithout numbers, like so: "-1,-2,,,,,,3,4,5,,6" Write your solution by modifying this code: ```python def string_to_int_list(s): ``` Your solution should implemented in the function "string_to_int_list". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. There is a chain consisting of multiple circles on a plane. The first (last) circle of the chain only intersects with the next (previous) circle, and each intermediate circle intersects only with the two neighboring circles. Your task is to find the shortest path that satisfies the following conditions. * The path connects the centers of the first circle and the last circle. * The path is confined in the chain, that is, all the points on the path are located within or on at least one of the circles. Figure E-1 shows an example of such a chain and the corresponding shortest path. <image> Figure E-1: An example chain and the corresponding shortest path Input The input consists of multiple datasets. Each dataset represents the shape of a chain in the following format. > n > x1 y1 r1 > x2 y2 r2 > ... > xn yn rn > The first line of a dataset contains an integer n (3 ≤ n ≤ 100) representing the number of the circles. Each of the following n lines contains three integers separated by a single space. (xi, yi) and ri represent the center position and the radius of the i-th circle Ci. You can assume that 0 ≤ xi ≤ 1000, 0 ≤ yi ≤ 1000, and 1 ≤ ri ≤ 25. You can assume that Ci and Ci+1 (1 ≤ i ≤ n−1) intersect at two separate points. When j ≥ i+2, Ci and Cj are apart and either of them does not contain the other. In addition, you can assume that any circle does not contain the center of any other circle. The end of the input is indicated by a line containing a zero. Figure E-1 corresponds to the first dataset of Sample Input below. Figure E-2 shows the shortest paths for the subsequent datasets of Sample Input. <image> Figure E-2: Example chains and the corresponding shortest paths Output For each dataset, output a single line containing the length of the shortest chain-confined path between the centers of the first circle and the last circle. The value should not have an error greater than 0.001. No extra characters should appear in the output. Sample Input 10 802 0 10 814 0 4 820 1 4 826 1 4 832 3 5 838 5 5 845 7 3 849 10 3 853 14 4 857 18 3 3 0 0 5 8 0 5 8 8 5 3 0 0 5 7 3 6 16 0 5 9 0 3 5 8 0 8 19 2 8 23 14 6 23 21 6 23 28 6 19 40 8 8 42 8 0 39 5 11 0 0 5 8 0 5 18 8 10 8 16 5 0 16 5 0 24 5 3 32 5 10 32 5 17 28 8 27 25 3 30 18 5 0 Output for the Sample Input 58.953437 11.414214 16.0 61.874812 63.195179 Example Input 10 802 0 10 814 0 4 820 1 4 826 1 4 832 3 5 838 5 5 845 7 3 849 10 3 853 14 4 857 18 3 3 0 0 5 8 0 5 8 8 5 3 0 0 5 7 3 6 16 0 5 9 0 3 5 8 0 8 19 2 8 23 14 6 23 21 6 23 28 6 19 40 8 8 42 8 0 39 5 11 0 0 5 8 0 5 18 8 10 8 16 5 0 16 5 0 24 5 3 32 5 10 32 5 17 28 8 27 25 3 30 18 5 0 Output 58.953437 11.414214 16.0 61.874812 63.195179 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 $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys. 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. Bandits appeared in the city! One of them is trying to catch as many citizens as he can. The city consists of $n$ squares connected by $n-1$ roads in such a way that it is possible to reach any square from any other square. The square number $1$ is the main square. After Sunday walk all the roads were changed to one-way roads in such a way that it is possible to reach any square from the main square. At the moment when the bandit appeared on the main square there were $a_i$ citizens on the $i$-th square. Now the following process will begin. First, each citizen that is currently on a square with some outgoing one-way roads chooses one of such roads and moves along it to another square. Then the bandit chooses one of the one-way roads outgoing from the square he is located and moves along it. The process is repeated until the bandit is located on a square with no outgoing roads. The bandit catches all the citizens on that square. The bandit wants to catch as many citizens as possible; the citizens want to minimize the number of caught people. The bandit and the citizens know positions of all citizens at any time, the citizens can cooperate. If both sides act optimally, how many citizens will be caught? -----Input----- The first line contains a single integer $n$ — the number of squares in the city ($2 \le n \le 2\cdot10^5$). The second line contains $n-1$ integers $p_2, p_3 \dots p_n$ meaning that there is a one-way road from the square $p_i$ to the square $i$ ($1 \le p_i < i$). The third line contains $n$ integers $a_1, a_2, \dots, a_n$ — the number of citizens on each square initially ($0 \le a_i \le 10^9$). -----Output----- Print a single integer — the number of citizens the bandit will catch if both sides act optimally. -----Examples----- Input 3 1 1 3 1 2 Output 3 Input 3 1 1 3 1 3 Output 4 -----Note----- In the first example the citizens on the square $1$ can split into two groups $2 + 1$, so that the second and on the third squares will have $3$ citizens each. In the second example no matter how citizens act the bandit can catch at least $4$ citizens. 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. Student Andrey has been skipping physical education lessons for the whole term, and now he must somehow get a passing grade on this subject. Obviously, it is impossible to do this by legal means, but Andrey doesn't give up. Having obtained an empty certificate from a local hospital, he is going to use his knowledge of local doctor's handwriting to make a counterfeit certificate of illness. However, after writing most of the certificate, Andrey suddenly discovered that doctor's signature is impossible to forge. Or is it? For simplicity, the signature is represented as an $n\times m$ grid, where every cell is either filled with ink or empty. Andrey's pen can fill a $3\times3$ square without its central cell if it is completely contained inside the grid, as shown below. xxx x.x xxx Determine whether is it possible to forge the signature on an empty $n\times m$ grid. -----Input----- The first line of input contains two integers $n$ and $m$ ($3 \le n, m \le 1000$). Then $n$ lines follow, each contains $m$ characters. Each of the characters is either '.', representing an empty cell, or '#', representing an ink filled cell. -----Output----- If Andrey can forge the signature, output "YES". Otherwise output "NO". You can print each letter in any case (upper or lower). -----Examples----- Input 3 3 ### #.# ### Output YES Input 3 3 ### ### ### Output NO Input 4 3 ### ### ### ### Output YES Input 5 7 ....... .#####. .#.#.#. .#####. ....... Output YES -----Note----- In the first sample Andrey can paint the border of the square with the center in $(2, 2)$. In the second sample the signature is impossible to forge. In the third sample Andrey can paint the borders of the squares with the centers in $(2, 2)$ and $(3, 2)$: we have a clear paper: ... ... ... ... use the pen with center at $(2, 2)$. ### #.# ### ... use the pen with center at $(3, 2)$. ### ### ### ### In the fourth sample Andrey can paint the borders of the squares with the centers in $(3, 3)$ and $(3, 5)$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem! You are given $n$ nonnegative integers $a_1, a_2, \dots, a_n$ arranged in a circle, where $n$ must be odd (ie. $n-1$ is divisible by $2$). Formally, for all $i$ such that $2 \leq i \leq n$, the elements $a_{i - 1}$ and $a_i$ are considered to be adjacent, and $a_n$ and $a_1$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value. Help Danny find the maximum possible circular value after some sequences of operations. -----Input----- The first line contains one odd integer $n$ ($1 \leq n < 2 \cdot 10^5$, $n$ is odd)  — the initial size of the circle. The second line contains $n$ integers $a_{1},a_{2},\dots,a_{n}$ ($0 \leq a_{i} \leq 10^9$)  — the initial numbers in the circle. -----Output----- Output the maximum possible circular value after applying some sequence of operations to the given circle. -----Examples----- Input 3 7 10 2 Output 17 Input 1 4 Output 4 -----Note----- For the first test case, here's how a circular value of $17$ is obtained: Pick the number at index $3$. The sum of adjacent elements equals $17$. Delete $7$ and $10$ from the circle and replace $2$ with $17$. Note that the answer may not fit in a $32$-bit integer. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Recently, Tokitsukaze found an interesting game. Tokitsukaze had $n$ items at the beginning of this game. However, she thought there were too many items, so now she wants to discard $m$ ($1 \le m \le n$) special items of them. These $n$ items are marked with indices from $1$ to $n$. In the beginning, the item with index $i$ is placed on the $i$-th position. Items are divided into several pages orderly, such that each page contains exactly $k$ positions and the last positions on the last page may be left empty. Tokitsukaze would do the following operation: focus on the first special page that contains at least one special item, and at one time, Tokitsukaze would discard all special items on this page. After an item is discarded or moved, its old position would be empty, and then the item below it, if exists, would move up to this empty position. The movement may bring many items forward and even into previous pages, so Tokitsukaze would keep waiting until all the items stop moving, and then do the operation (i.e. check the special page and discard the special items) repeatedly until there is no item need to be discarded. [Image] Consider the first example from the statement: $n=10$, $m=4$, $k=5$, $p=[3, 5, 7, 10]$. The are two pages. Initially, the first page is special (since it is the first page containing a special item). So Tokitsukaze discards the special items with indices $3$ and $5$. After, the first page remains to be special. It contains $[1, 2, 4, 6, 7]$, Tokitsukaze discards the special item with index $7$. After, the second page is special (since it is the first page containing a special item). It contains $[9, 10]$, Tokitsukaze discards the special item with index $10$. Tokitsukaze wants to know the number of operations she would do in total. -----Input----- The first line contains three integers $n$, $m$ and $k$ ($1 \le n \le 10^{18}$, $1 \le m \le 10^5$, $1 \le m, k \le n$) — the number of items, the number of special items to be discarded and the number of positions in each page. The second line contains $m$ distinct integers $p_1, p_2, \ldots, p_m$ ($1 \le p_1 < p_2 < \ldots < p_m \le n$) — the indices of special items which should be discarded. -----Output----- Print a single integer — the number of operations that Tokitsukaze would do in total. -----Examples----- Input 10 4 5 3 5 7 10 Output 3 Input 13 4 5 7 8 9 10 Output 1 -----Note----- For the first example: In the first operation, Tokitsukaze would focus on the first page $[1, 2, 3, 4, 5]$ and discard items with indices $3$ and $5$; In the second operation, Tokitsukaze would focus on the first page $[1, 2, 4, 6, 7]$ and discard item with index $7$; In the third operation, Tokitsukaze would focus on the second page $[9, 10]$ and discard item with index $10$. For the second example, Tokitsukaze would focus on the second page $[6, 7, 8, 9, 10]$ and discard all special items at once. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Create a __moreZeros__ function which will __receive a string__ for input, and __return an array__ (or null terminated string in C) containing only the characters from that string whose __binary representation of its ASCII value__ consists of _more zeros than ones_. You should __remove any duplicate characters__, keeping the __first occurence__ of any such duplicates, so they are in the __same order__ in the final array as they first appeared in the input string. Examples All input will be valid strings of length > 0. Leading zeros in binary should not be counted. Write your solution by modifying this code: ```python def more_zeros(s): ``` Your solution should implemented in the function "more_zeros". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are a secret agent from the Intelligence Center of Peacemaking Committee. You've just sneaked into a secret laboratory of an evil company, Automated Crime Machines. Your mission is to get a confidential document kept in the laboratory. To reach the document, you need to unlock the door to the safe where it is kept. You have to unlock the door in a correct way and with a great care; otherwise an alarm would ring and you would be caught by the secret police. The lock has a circular dial with N lights around it and a hand pointing to one of them. The lock also has M buttons to control the hand. Each button has a number Li printed on it. Initially, all the lights around the dial are turned off. When the i-th button is pressed, the hand revolves clockwise by Li lights, and the pointed light is turned on. You are allowed to press the buttons exactly N times. The lock opens only when you make all the lights turned on. <image> For example, in the case with N = 6, M = 2, L1 = 2 and L2 = 5, you can unlock the door by pressing buttons 2, 2, 2, 5, 2 and 2 in this order. There are a number of doors in the laboratory, and some of them don’t seem to be unlockable. Figure out which lock can be opened, given the values N, M, and Li's. Input The input starts with a line containing two integers, which represent N and M respectively. M lines follow, each of which contains an integer representing Li. It is guaranteed that 1 ≤ N ≤ 109, 1 ≤ M ≤ 105, and 1 ≤ Li ≤ N for each i = 1, 2, ... N. Output Output a line with "Yes" (without quotes) if the lock can be opened, and "No" otherwise. Examples Input 6 2 2 5 Output Yes Input 3 1 1 Output Yes Input 4 2 2 4 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. Nowadays, most of the internet advertisements are not statically linked to a web page. Instead, what will be shown to the person opening a web page is determined within 100 milliseconds after the web page is opened. Usually, multiple companies compete for each ad slot on the web page in an auction. Each of them receives a request with details about the user, web page and ad slot and they have to respond within those 100 milliseconds with a bid they would pay for putting an advertisement on that ad slot. The company that suggests the highest bid wins the auction and gets to place its advertisement. If there are several companies tied for the highest bid, the winner gets picked at random. However, the company that won the auction does not have to pay the exact amount of its bid. In most of the cases, a second-price auction is used. This means that the amount paid by the company is equal to the maximum of all the other bids placed for this ad slot. Let's consider one such bidding. There are n companies competing for placing an ad. The i-th of these companies will bid an integer number of microdollars equiprobably randomly chosen from the range between L_{i} and R_{i}, inclusive. In the other words, the value of the i-th company bid can be any integer from the range [L_{i}, R_{i}] with the same probability. Determine the expected value that the winner will have to pay in a second-price auction. -----Input----- The first line of input contains an integer number n (2 ≤ n ≤ 5). n lines follow, the i-th of them containing two numbers L_{i} and R_{i} (1 ≤ L_{i} ≤ R_{i} ≤ 10000) describing the i-th company's bid preferences. This problem doesn't have subproblems. You will get 8 points for the correct submission. -----Output----- Output the answer with absolute or relative error no more than 1e - 9. -----Examples----- Input 3 4 7 8 10 5 5 Output 5.7500000000 Input 3 2 5 3 4 1 6 Output 3.5000000000 -----Note----- Consider the first example. The first company bids a random integer number of microdollars in range [4, 7]; the second company bids between 8 and 10, and the third company bids 5 microdollars. The second company will win regardless of the exact value it bids, however the price it will pay depends on the value of first company's bid. With probability 0.5 the first company will bid at most 5 microdollars, and the second-highest price of the whole auction will be 5. With probability 0.25 it will bid 6 microdollars, and with probability 0.25 it will bid 7 microdollars. Thus, the expected value the second company will have to pay is 0.5·5 + 0.25·6 + 0.25·7 = 5.75. 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. Valera loves his garden, where n fruit trees grow. This year he will enjoy a great harvest! On the i-th tree b_{i} fruit grow, they will ripen on a day number a_{i}. Unfortunately, the fruit on the tree get withered, so they can only be collected on day a_{i} and day a_{i} + 1 (all fruits that are not collected in these two days, become unfit to eat). Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? -----Input----- The first line contains two space-separated integers n and v (1 ≤ n, v ≤ 3000) — the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers a_{i} and b_{i} (1 ≤ a_{i}, b_{i} ≤ 3000) — the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. -----Output----- Print a single integer — the maximum number of fruit that Valera can collect. -----Examples----- Input 2 3 1 5 2 3 Output 8 Input 5 10 3 20 2 20 1 20 4 20 5 20 Output 60 -----Note----- In the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. You are given a complete undirected graph. For each pair of vertices you are given the length of the edge that connects them. Find the shortest paths between each pair of vertices in the graph and return the length of the longest of them. -----Input----- The first line of the input contains a single integer N (3 ≤ N ≤ 10). The following N lines each contain N space-separated integers. jth integer in ith line a_{ij} is the length of the edge that connects vertices i and j. a_{ij} = a_{ji}, a_{ii} = 0, 1 ≤ a_{ij} ≤ 100 for i ≠ j. -----Output----- Output the maximum length of the shortest path between any pair of vertices in the graph. -----Examples----- Input 3 0 1 1 1 0 4 1 4 0 Output 2 Input 4 0 1 2 3 1 0 4 5 2 4 0 6 3 5 6 0 Output 5 -----Note----- You're running short of keywords, so you can't use some of them:define do for foreach while repeat until if then else elif elsif elseif case switch 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 a clear night, a boy, Campanella looked up at the sky, there were many stars which have different colors such as red, yellow, green, blue, purple, etc. He was watching the stars and just lost track of time. Presently, he wondered, | <image> ---|--- "There are many stars in the space. What color is the universe if I look up it from outside?" Until he found the answer to this question, he couldn't sleep. After a moment's thought, he proposed a hypothesis, "When I observe the stars in the sky, if more than half of the stars have the same color, that is the color of the universe." He has collected data of stars in the universe after consistently observing them. However, when he try to count the number of star, he was in bed sick with a cold. You decided to help him by developing a program to identify the color of the universe. You are given an array A. Let |A| be the number of element in A, and let Nm be the number of m in A. For example, if A = {3, 1, 2, 3, 3, 1, 5, 3}, |A| = 8, N3 = 4, N1 = 2, N5 = 1. Your program have to find m such that: Nm > (|A| / 2 ) If there is no such m, you also have to report the fact. There is no such m for the above example, but for a array A = {5, 2, 5, 3, 4, 5, 5}, 5 is the answer. Input There are several test cases. For each test case, in the first line |A| is given. In the next line, there will be |A| integers. The integers are less than 231 and |A| < 1,000,000. The input terminate with a line which contains single 0. Output For each test case, output m in a line. If there is no answer, output "NO COLOR" in a line. Example Input 8 3 1 2 3 3 1 5 3 7 5 2 5 3 4 5 5 0 Output NO COLOR 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. Santa has to send presents to the kids. He has a large stack of $n$ presents, numbered from $1$ to $n$; the topmost present has number $a_1$, the next present is $a_2$, and so on; the bottom present has number $a_n$. All numbers are distinct. Santa has a list of $m$ distinct presents he has to send: $b_1$, $b_2$, ..., $b_m$. He will send them in the order they appear in the list. To send a present, Santa has to find it in the stack by removing all presents above it, taking this present and returning all removed presents on top of the stack. So, if there are $k$ presents above the present Santa wants to send, it takes him $2k + 1$ seconds to do it. Fortunately, Santa can speed the whole process up — when he returns the presents to the stack, he may reorder them as he wishes (only those which were above the present he wanted to take; the presents below cannot be affected in any way). What is the minimum time required to send all of the presents, provided that Santa knows the whole list of presents he has to send and reorders the presents optimally? Santa cannot change the order of presents or interact with the stack of presents in any other way. Your program has to answer $t$ different test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 100$) — the number of test cases. Then the test cases follow, each represented by three lines. The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 10^5$) — the number of presents in the stack and the number of presents Santa wants to send, respectively. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le n$, all $a_i$ are unique) — the order of presents in the stack. The third line contains $m$ integers $b_1$, $b_2$, ..., $b_m$ ($1 \le b_i \le n$, all $b_i$ are unique) — the ordered list of presents Santa has to send. The sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer — the minimum number of seconds which Santa has to spend sending presents, if he reorders the presents optimally each time he returns them into the stack. -----Example----- Input 2 3 3 3 1 2 3 2 1 7 2 2 1 7 3 4 5 6 3 1 Output 5 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. Today is the ticket release date for Aizu Entertainment's recommended idol group "Akabeko & Koboushi". There are four types of tickets: S seat 6000 yen A seat 4000 yen B seat 3000 yen C seat 2000 yen You, the sales manager, are excitedly waiting for the launch. Finally on sale. It's selling very well! Shortly after the launch, I received a table summarizing the orders up to that point. Each row of the table shows the type and number of tickets sold so far. However, the ticket types do not always appear in the order of S, A, B, C. Create a program to find the sales amount for each row in this table. input Input data is given in the following format. t1 n1 t2 n2 t3 n3 t4 n4 The input consists of 4 lines. Line i is given the integer ti (1 ≤ ti ≤ 4) for the ticket type and the integer ni (0 ≤ ni ≤ 10000) for the number of tickets. The integers 1, 2, 3, and 4 representing the ticket types represent S seats, A seats, B seats, and C seats, respectively. Numbers from 1 to 4 always appear once as values ​​for t1, t2, t3, and t4, but they are not always given in the order of 1, 2, 3, 4. output Output the sales amount for each line. Example Input 3 10 1 4 4 1 2 5 Output 30000 24000 2000 20000 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 the speed skating badge test, grades are awarded when the time specified for two distances is exceeded. For example, to reach Class A, 500 M requires less than 40.0 seconds and 1000 M requires less than 1 minute and 23 seconds. Create a program that takes the time recorded in the speed skating competitions (500 M and 1000 M) as input and outputs what grade it corresponds to in the speed skating badge test. The table below shows the default badge test times for the 500 M and 1000 M. If it is less than class E, output NA. | 500 M | 1000 M --- | --- | --- AAA class | 35 seconds 50 | 1 minute 11 seconds 00 AA class | 37 seconds 50 | 1 minute 17 seconds 00 Class A | 40 seconds 00 | 1 minute 23 seconds 00 Class B | 43 seconds 00 | 1 minute 29 seconds 00 Class C | 50 seconds 00 | 1 minute 45 seconds 00 Class D | 55 seconds 00 | 1 minute 56 seconds 00 Class E | 1 minute 10 seconds 00 | 2 minutes 28 seconds 00 Input Given multiple datasets. For each dataset, real numbers t1, t2 (8.0 ≤ t1, t2 ≤ 360.0) representing 500 M time and 1000 M time, respectively, are given, separated by blanks. t1 and t2 are given in seconds as real numbers, including up to two digits after the decimal point. The number of datasets does not exceed 100. Output For each data set, output the judgment result AAA ~ E or NA on one line. Example Input 40.0 70.0 72.5 140.51 Output B NA 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. Imp is in a magic forest, where xorangles grow (wut?) [Image] A xorangle of order n is such a non-degenerate triangle, that lengths of its sides are integers not exceeding n, and the xor-sum of the lengths is equal to zero. Imp has to count the number of distinct xorangles of order n to get out of the forest. Formally, for a given integer n you have to find the number of such triples (a, b, c), that: 1 ≤ a ≤ b ≤ c ≤ n; $a \oplus b \oplus c = 0$, where $x \oplus y$ denotes the bitwise xor of integers x and y. (a, b, c) form a non-degenerate (with strictly positive area) triangle. -----Input----- The only line contains a single integer n (1 ≤ n ≤ 2500). -----Output----- Print the number of xorangles of order n. -----Examples----- Input 6 Output 1 Input 10 Output 2 -----Note----- The only xorangle in the first sample is (3, 5, 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. You are given array $a_1, a_2, \ldots, a_n$, consisting of non-negative integers. Let's define operation of "elimination" with integer parameter $k$ ($1 \leq k \leq n$) as follows: Choose $k$ distinct array indices $1 \leq i_1 < i_2 < \ldots < i_k \le n$. Calculate $x = a_{i_1} ~ \& ~ a_{i_2} ~ \& ~ \ldots ~ \& ~ a_{i_k}$, where $\&$ denotes the bitwise AND operation (notes section contains formal definition). Subtract $x$ from each of $a_{i_1}, a_{i_2}, \ldots, a_{i_k}$; all other elements remain untouched. Find all possible values of $k$, such that it's possible to make all elements of array $a$ equal to $0$ using a finite number of elimination operations with parameter $k$. It can be proven that exists at least one possible $k$ for any array $a$. Note that you firstly choose $k$ and only after that perform elimination operations with value $k$ you've chosen initially. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 10^4$). Description of the test cases follows. The first line of each test case contains one integer $n$ ($1 \leq n \leq 200000$) — the length of array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < 2^{30}$) — array $a$ itself. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case, print all values $k$, such that it's possible to make all elements of $a$ equal to $0$ in a finite number of elimination operations with the given parameter $k$. Print them in increasing order. -----Examples----- Input 5 4 4 4 4 4 4 13 7 25 19 6 3 5 3 1 7 1 1 1 5 0 0 0 0 0 Output 1 2 4 1 2 1 1 1 2 3 4 5 -----Note----- In the first test case: If $k = 1$, we can make four elimination operations with sets of indices $\{1\}$, $\{2\}$, $\{3\}$, $\{4\}$. Since $\&$ of one element is equal to the element itself, then for each operation $x = a_i$, so $a_i - x = a_i - a_i = 0$. If $k = 2$, we can make two elimination operations with, for example, sets of indices $\{1, 3\}$ and $\{2, 4\}$: $x = a_1 ~ \& ~ a_3$ $=$ $a_2 ~ \& ~ a_4$ $=$ $4 ~ \& ~ 4 = 4$. For both operations $x = 4$, so after the first operation $a_1 - x = 0$ and $a_3 - x = 0$, and after the second operation — $a_2 - x = 0$ and $a_4 - x = 0$. If $k = 3$, it's impossible to make all $a_i$ equal to $0$. After performing the first operation, we'll get three elements equal to $0$ and one equal to $4$. After that, all elimination operations won't change anything, since at least one chosen element will always be equal to $0$. If $k = 4$, we can make one operation with set $\{1, 2, 3, 4\}$, because $x = a_1 ~ \& ~ a_2 ~ \& ~ a_3 ~ \& ~ a_4$ $= 4$. In the second test case, if $k = 2$ then we can make the following elimination operations: Operation with indices $\{1, 3\}$: $x = a_1 ~ \& ~ a_3$ $=$ $13 ~ \& ~ 25 = 9$. $a_1 - x = 13 - 9 = 4$ and $a_3 - x = 25 - 9 = 16$. Array $a$ will become equal to $[4, 7, 16, 19]$. Operation with indices $\{3, 4\}$: $x = a_3 ~ \& ~ a_4$ $=$ $16 ~ \& ~ 19 = 16$. $a_3 - x = 16 - 16 = 0$ and $a_4 - x = 19 - 16 = 3$. Array $a$ will become equal to $[4, 7, 0, 3]$. Operation with indices $\{2, 4\}$: $x = a_2 ~ \& ~ a_4$ $=$ $7 ~ \& ~ 3 = 3$. $a_2 - x = 7 - 3 = 4$ and $a_4 - x = 3 - 3 = 0$. Array $a$ will become equal to $[4, 4, 0, 0]$. Operation with indices $\{1, 2\}$: $x = a_1 ~ \& ~ a_2$ $=$ $4 ~ \& ~ 4 = 4$. $a_1 - x = 4 - 4 = 0$ and $a_2 - x = 4 - 4 = 0$. Array $a$ will become equal to $[0, 0, 0, 0]$. Formal definition of bitwise AND: Let's define bitwise AND ($\&$) as follows. Suppose we have two non-negative integers $x$ and $y$, let's look at their binary representations (possibly, with leading zeroes): $x_k \dots x_2 x_1 x_0$ and $y_k \dots y_2 y_1 y_0$. Here, $x_i$ is the $i$-th bit of number $x$, and $y_i$ is the $i$-th bit of number $y$. Let $r = x ~ \& ~ y$ is a result of operation $\&$ on number $x$ and $y$. Then binary representation of $r$ will be $r_k \dots r_2 r_1 r_0$, where: $$ r_i = \begin{cases} 1, ~ \text{if} ~ x_i = 1 ~ \text{and} ~ y_i = 1 \\ 0, ~ \text{if} ~ x_i = 0 ~ \text{or} ~ y_i = 0 \end{cases} $$ 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 only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one — from $1$ to $2$, the third one — from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) — the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^{18})$ — the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ — the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999999999999 1000000000000000000 Output 8 2 4 1 -----Note----- Answers on queries from the first example are described 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. Create a program of the square picking method, which is one of the classical random number generation methods. The square harvesting method was proposed by von Neumann in the mid-1940s. In the square picking method, when the number of digits of the generated random number is n, the square of the initial value s is calculated, and the value is regarded as a 2n digit number (the number of squared digits as shown in the example below). If is not enough, make up for 0.) Let the n numbers in the center be the first random number. Then square this random number and take the n numbers in the center in the same way to get the next random number. For example, if 123 is the initial value 1232 = 00015129 → 0151 1512 = 00022801 → 0228 2282 = 00051984 → 0519 5192 = 00269361 → 2693 26932 = 07252249 → 2522 It will be like. Use this method to create a program that takes the initial value s (a positive integer less than 10000) as an input and generates and outputs 10 random numbers when n = 4. Input Given multiple datasets. The first line gives the number of datasets d (d ≤ 10). For each dataset, one row is given the initial value s (integer, 1 ≤ s <10000). Output For each dataset Case x: (x is a dataset number starting with 1) The first generated random number (integer) The second generated random number (integer) : : 10th generated random number (integer) Please output. Example Input 2 123 567 Output Case 1: 151 228 519 2693 2522 3604 9888 7725 6756 6435 Case 2: 3214 3297 8702 7248 5335 4622 3628 1623 6341 2082 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. Inaka has a disc, the circumference of which is n units. The circumference is equally divided by n points numbered clockwise from 1 to n, such that points i and i + 1 (1 ≤ i < n) are adjacent, and so are points n and 1. There are m straight segments on the disc, the endpoints of which are all among the aforementioned n points. Inaka wants to know if her image is rotationally symmetrical, i.e. if there is an integer k (1 ≤ k < n), such that if all segments are rotated clockwise around the center of the circle by k units, the new image will be the same as the original one. Input The first line contains two space-separated integers n and m (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 200 000) — the number of points and the number of segments, respectively. The i-th of the following m lines contains two space-separated integers a_i and b_i (1 ≤ a_i, b_i ≤ n, a_i ≠ b_i) that describe a segment connecting points a_i and b_i. It is guaranteed that no segments coincide. Output Output one line — "Yes" if the image is rotationally symmetrical, and "No" otherwise (both excluding quotation marks). You can output each letter in any case (upper or lower). Examples Input 12 6 1 3 3 7 5 7 7 11 9 11 11 3 Output Yes Input 9 6 4 5 5 6 7 8 8 9 1 2 2 3 Output Yes Input 10 3 1 2 3 2 7 2 Output No Input 10 2 1 6 2 7 Output Yes Note The first two examples are illustrated below. Both images become the same as their respective original ones after a clockwise rotation of 120 degrees around the center. <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. Vova has taken his summer practice this year and now he should write a report on how it went. Vova has already drawn all the tables and wrote down all the formulas. Moreover, he has already decided that the report will consist of exactly $n$ pages and the $i$-th page will include $x_i$ tables and $y_i$ formulas. The pages are numbered from $1$ to $n$. Vova fills the pages one after another, he can't go filling page $i + 1$ before finishing page $i$ and he can't skip pages. However, if he draws strictly more than $k$ tables in a row or writes strictly more than $k$ formulas in a row then he will get bored. Vova wants to rearrange tables and formulas in each page in such a way that he doesn't get bored in the process. Vova can't move some table or some formula to another page. Note that the count doesn't reset on the start of the new page. For example, if the page ends with $3$ tables and the next page starts with $5$ tables, then it's counted as $8$ tables in a row. Help Vova to determine if he can rearrange tables and formulas on each page in such a way that there is no more than $k$ tables in a row and no more than $k$ formulas in a row. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 3 \cdot 10^5$, $1 \le k \le 10^6$). The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^6$) — the number of tables on the $i$-th page. The third line contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^6$) — the number of formulas on the $i$-th page. -----Output----- Print "YES" if Vova can rearrange tables and formulas on each page in such a way that there is no more than $k$ tables in a row and no more than $k$ formulas in a row. Otherwise print "NO". -----Examples----- Input 2 2 5 5 2 2 Output YES Input 2 2 5 6 2 2 Output NO Input 4 1 4 1 10 1 3 2 10 1 Output YES -----Note----- In the first example the only option to rearrange everything is the following (let table be 'T' and formula be 'F'): page $1$: "TTFTTFT" page $2$: "TFTTFTT" That way all blocks of tables have length $2$. In the second example there is no way to fit everything in such a way that there are no more than $2$ tables in a row and $2$ formulas in a 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. In this kata, your task is to identify the pattern underlying a sequence of numbers. For example, if the sequence is [1, 2, 3, 4, 5], then the pattern is [1], since each number in the sequence is equal to the number preceding it, plus 1. See the test cases for more examples. A few more rules : pattern may contain negative numbers. sequence will always be made of a whole number of repetitions of the pattern. Your answer must correspond to the shortest form of the pattern, e.g. if the pattern is [1], then [1, 1, 1, 1] will not be considered a correct answer. Write your solution by modifying this code: ```python def find_pattern(seq): ``` Your solution should implemented in the function "find_pattern". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. It seems that Borya is seriously sick. He is going visit n doctors to find out the exact diagnosis. Each of the doctors needs the information about all previous visits, so Borya has to visit them in the prescribed order (i.e. Borya should first visit doctor 1, then doctor 2, then doctor 3 and so on). Borya will get the information about his health from the last doctor. Doctors have a strange working schedule. The doctor i goes to work on the s_{i}-th day and works every d_{i} day. So, he works on days s_{i}, s_{i} + d_{i}, s_{i} + 2d_{i}, .... The doctor's appointment takes quite a long time, so Borya can not see more than one doctor per day. What is the minimum time he needs to visit all doctors? -----Input----- First line contains an integer n — number of doctors (1 ≤ n ≤ 1000). Next n lines contain two numbers s_{i} and d_{i} (1 ≤ s_{i}, d_{i} ≤ 1000). -----Output----- Output a single integer — the minimum day at which Borya can visit the last doctor. -----Examples----- Input 3 2 2 1 2 2 2 Output 4 Input 2 10 1 6 5 Output 11 -----Note----- In the first sample case, Borya can visit all doctors on days 2, 3 and 4. In the second sample case, Borya can visit all doctors on days 10 and 11. 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. Converting a normal (12-hour) time like "8:30 am" or "8:30 pm" to 24-hour time (like "0830" or "2030") sounds easy enough, right? Well, let's see if you can do it! You will have to define a function named "to24hourtime", and you will be given an hour (always in the range of 1 to 12, inclusive), a minute (always in the range of 0 to 59, inclusive), and a period (either "am" or "pm") as input. Your task is to return a four-digit string that encodes that time in 24-hour time. Write your solution by modifying this code: ```python def to24hourtime(hour, minute, period): ``` Your solution should implemented in the function "to24hourtime". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Dima has a birthday soon! It's a big day! Saryozha's present to Dima is that Seryozha won't be in the room and won't disturb Dima and Inna as they celebrate the birthday. Inna's present to Dima is a stack, a queue and a deck. Inna wants her present to show Dima how great a programmer he is. For that, she is going to give Dima commands one by one. There are two types of commands: 1. Add a given number into one of containers. For the queue and the stack, you can add elements only to the end. For the deck, you can add elements to the beginning and to the end. 2. Extract a number from each of at most three distinct containers. Tell all extracted numbers to Inna and then empty all containers. In the queue container you can extract numbers only from the beginning. In the stack container you can extract numbers only from the end. In the deck number you can extract numbers from the beginning and from the end. You cannot extract numbers from empty containers. Every time Dima makes a command of the second type, Inna kisses Dima some (possibly zero) number of times. Dima knows Inna perfectly well, he is sure that this number equals the sum of numbers he extracts from containers during this operation. As we've said before, Dima knows Inna perfectly well and he knows which commands Inna will give to Dima and the order of the commands. Help Dima find the strategy that lets him give as more kisses as possible for his birthday! Input The first line contains integer n (1 ≤ n ≤ 105) — the number of Inna's commands. Then n lines follow, describing Inna's commands. Each line consists an integer: 1. Integer a (1 ≤ a ≤ 105) means that Inna gives Dima a command to add number a into one of containers. 2. Integer 0 shows that Inna asks Dima to make at most three extractions from different containers. Output Each command of the input must correspond to one line of the output — Dima's action. For the command of the first type (adding) print one word that corresponds to Dima's choice: * pushStack — add to the end of the stack; * pushQueue — add to the end of the queue; * pushFront — add to the beginning of the deck; * pushBack — add to the end of the deck. For a command of the second type first print an integer k (0 ≤ k ≤ 3), that shows the number of extract operations, then print k words separated by space. The words can be: * popStack — extract from the end of the stack; * popQueue — extract from the beginning of the line; * popFront — extract from the beginning from the deck; * popBack — extract from the end of the deck. The printed operations mustn't extract numbers from empty containers. Also, they must extract numbers from distinct containers. The printed sequence of actions must lead to the maximum number of kisses. If there are multiple sequences of actions leading to the maximum number of kisses, you are allowed to print any of them. Examples Input 10 0 1 0 1 2 0 1 2 3 0 Output 0 pushStack 1 popStack pushStack pushQueue 2 popStack popQueue pushStack pushQueue pushFront 3 popStack popQueue popFront Input 4 1 2 3 0 Output pushStack pushQueue pushFront 3 popStack popQueue popFront 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. "Night gathers, and now my watch begins. It shall not end until my death. I shall take no wife, hold no lands, father no children. I shall wear no crowns and win no glory. I shall live and die at my post. I am the sword in the darkness. I am the watcher on the walls. I am the shield that guards the realms of men. I pledge my life and honor to the Night's Watch, for this night and all the nights to come." — The Night's Watch oath. With that begins the watch of Jon Snow. He is assigned the task to support the stewards. This time he has n stewards with him whom he has to provide support. Each steward has his own strength. Jon Snow likes to support a steward only if there exists at least one steward who has strength strictly less than him and at least one steward who has strength strictly greater than him. Can you find how many stewards will Jon support? -----Input----- First line consists of a single integer n (1 ≤ n ≤ 10^5) — the number of stewards with Jon Snow. Second line consists of n space separated integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9) representing the values assigned to the stewards. -----Output----- Output a single integer representing the number of stewards which Jon will feed. -----Examples----- Input 2 1 5 Output 0 Input 3 1 2 5 Output 1 -----Note----- In the first sample, Jon Snow cannot support steward with strength 1 because there is no steward with strength less than 1 and he cannot support steward with strength 5 because there is no steward with strength greater than 5. In the second sample, Jon Snow can support steward with strength 2 because there are stewards with strength less than 2 and greater than 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. You can obtain profits from foreign exchange margin transactions. For example, if you buy 1000 dollar at a rate of 100 yen per dollar, and sell them at a rate of 108 yen per dollar, you can obtain (108 - 100) × 1000 = 8000 yen. Write a program which reads values of a currency $R_t$ at a certain time $t$ ($t = 0, 1, 2, ... n-1$), and reports the maximum value of $R_j - R_i$ where $j > i$ . Constraints * $2 \leq n \leq 200,000$ * $1 \leq R_t \leq 10^9$ Input The first line contains an integer $n$. In the following $n$ lines, $R_t$ ($t = 0, 1, 2, ... n-1$) are given in order. Output Print the maximum value in a line. Examples Input 6 5 3 1 3 4 3 Output 3 Input 3 4 3 2 Output -1 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Sonya was unable to think of a story for this problem, so here comes the formal description. You are given the array containing n positive integers. At one turn you can pick any element and increase or decrease it by 1. The goal is the make the array strictly increasing by making the minimum possible number of operations. You are allowed to change elements in any way, they can become negative or equal to 0. Input The first line of the input contains a single integer n (1 ≤ n ≤ 3000) — the length of the array. Next line contains n integer ai (1 ≤ ai ≤ 109). Output Print the minimum number of operation required to make the array strictly increasing. Examples Input 7 2 1 5 11 5 9 11 Output 9 Input 5 5 4 3 2 1 Output 12 Note In the first sample, the array is going to look as follows: 2 3 5 6 7 9 11 |2 - 2| + |1 - 3| + |5 - 5| + |11 - 6| + |5 - 7| + |9 - 9| + |11 - 11| = 9 And for the second sample: 1 2 3 4 5 |5 - 1| + |4 - 2| + |3 - 3| + |2 - 4| + |1 - 5| = 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. Consider the following arithmetic progression with n terms: * x, x + d, x + 2d, \ldots, x + (n-1)d What is the product of all terms in this sequence? Compute the answer modulo 1\ 000\ 003. You are given Q queries of this form. In the i-th query, compute the answer in case x = x_i, d = d_i, n = n_i. Constraints * 1 \leq Q \leq 10^5 * 0 \leq x_i, d_i \leq 1\ 000\ 002 * 1 \leq n_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: Q x_1 d_1 n_1 : x_Q d_Q n_Q Output Print Q lines. In the i-th line, print the answer for the i-th query. Example Input 2 7 2 4 12345 67890 2019 Output 9009 916936 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 vector of strings. You must sort it alphabetically (case-sensitive, and based on the ASCII values of the chars) and then return the first value. The returned value must be a string, and have `"***"` between each of its letters. You should not remove or add elements from/to the array. Write your solution by modifying this code: ```python def two_sort(array): ``` Your solution should implemented in the function "two_sort". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. A revolution took place on the Buka Island. New government replaced the old one. The new government includes n parties and each of them is entitled to some part of the island according to their contribution to the revolution. However, they can't divide the island. The island can be conventionally represented as two rectangles a × b and c × d unit squares in size correspondingly. The rectangles are located close to each other. At that, one of the sides with the length of a and one of the sides with the length of c lie on one line. You can see this in more details on the picture. <image> The i-th party is entitled to a part of the island equal to xi unit squares. Every such part should fully cover several squares of the island (it is not allowed to cover the squares partially) and be a connected figure. A "connected figure" presupposes that from any square of this party one can move to any other square of the same party moving through edge-adjacent squares also belonging to that party. Your task is to divide the island between parties. Input The first line contains 5 space-separated integers — a, b, c, d and n (1 ≤ a, b, c, d ≤ 50, b ≠ d, 1 ≤ n ≤ 26). The second line contains n space-separated numbers. The i-th of them is equal to number xi (1 ≤ xi ≤ a × b + c × d). It is guaranteed that <image>. Output If dividing the island between parties in the required manner is impossible, print "NO" (without the quotes). Otherwise, print "YES" (also without the quotes) and, starting from the next line, print max(b, d) lines each containing a + c characters. To mark what square should belong to what party, use lowercase Latin letters. For the party that is first in order in the input data, use "a", for the second one use "b" and so on. Use "." for the squares that belong to the sea. The first symbol of the second line of the output data should correspond to the square that belongs to the rectangle a × b. The last symbol of the second line should correspond to the square that belongs to the rectangle c × d. If there are several solutions output any. Examples Input 3 4 2 2 3 5 8 3 Output YES aaabb aabbb cbb.. ccb.. Input 3 2 1 4 4 1 2 3 4 Output YES abbd cccd ...d ...d 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. Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches. The current state of the wall can be respresented by a sequence $a$ of $n$ integers, with $a_i$ being the height of the $i$-th part of the wall. Vova can only use $2 \times 1$ bricks to put in the wall (he has infinite supply of them, however). Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $i$ the current height of part $i$ is the same as for part $i + 1$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $1$ of the wall or to the right of part $n$ of it). The next paragraph is specific to the version 1 of the problem. Vova can also put bricks vertically. That means increasing height of any part of the wall by 2. Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of parts in the wall. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the initial heights of the parts of the wall. -----Output----- Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise. -----Examples----- Input 5 2 1 1 2 5 Output YES Input 3 4 5 3 Output YES Input 2 10 10 Output YES Input 3 1 2 3 Output NO -----Note----- In the first example Vova can put a brick on parts 2 and 3 to make the wall $[2, 2, 2, 2, 5]$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $[5, 5, 5, 5, 5]$. In the second example Vova can put a brick vertically on part 3 to make the wall $[4, 5, 5]$, then horizontally on parts 2 and 3 to make it $[4, 6, 6]$ and then vertically on part 1 to make it $[6, 6, 6]$. In the third example the wall is already complete. 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. Denis, after buying flowers and sweets (you will learn about this story in the next task), went to a date with Nastya to ask her to become a couple. Now, they are sitting in the cafe and finally... Denis asks her to be together, but ... Nastya doesn't give any answer. The poor boy was very upset because of that. He was so sad that he punched some kind of scoreboard with numbers. The numbers are displayed in the same way as on an electronic clock: each digit position consists of $7$ segments, which can be turned on or off to display different numbers. The picture shows how all $10$ decimal digits are displayed: [Image] After the punch, some segments stopped working, that is, some segments might stop glowing if they glowed earlier. But Denis remembered how many sticks were glowing and how many are glowing now. Denis broke exactly $k$ segments and he knows which sticks are working now. Denis came up with the question: what is the maximum possible number that can appear on the board if you turn on exactly $k$ sticks (which are off now)? It is allowed that the number includes leading zeros. -----Input----- The first line contains integer $n$ $(1 \leq n \leq 2000)$  — the number of digits on scoreboard and $k$ $(0 \leq k \leq 2000)$  — the number of segments that stopped working. The next $n$ lines contain one binary string of length $7$, the $i$-th of which encodes the $i$-th digit of the scoreboard. Each digit on the scoreboard consists of $7$ segments. We number them, as in the picture below, and let the $i$-th place of the binary string be $0$ if the $i$-th stick is not glowing and $1$ if it is glowing. Then a binary string of length $7$ will specify which segments are glowing now. [Image] Thus, the sequences "1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011" encode in sequence all digits from $0$ to $9$ inclusive. -----Output----- Output a single number consisting of $n$ digits  — the maximum number that can be obtained if you turn on exactly $k$ sticks or $-1$, if it is impossible to turn on exactly $k$ sticks so that a correct number appears on the scoreboard digits. -----Examples----- Input 1 7 0000000 Output 8 Input 2 5 0010010 0010010 Output 97 Input 3 5 0100001 1001001 1010011 Output -1 -----Note----- In the first test, we are obliged to include all $7$ sticks and get one $8$ digit on the scoreboard. In the second test, we have sticks turned on so that units are formed. For $5$ of additionally included sticks, you can get the numbers $07$, $18$, $34$, $43$, $70$, $79$, $81$ and $97$, of which we choose the maximum  — $97$. In the third test, it is impossible to turn on exactly $5$ sticks so that a sequence of numbers appears on the scoreboard. 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 programing contest named SnakeUp, 2n people want to compete for it. In order to attend this contest, people need to form teams of exactly two people. You are given the strength of each possible combination of two people. All the values of the strengths are distinct. Every contestant hopes that he can find a teammate so that their team’s strength is as high as possible. That is, a contestant will form a team with highest strength possible by choosing a teammate from ones who are willing to be a teammate with him/her. More formally, two people A and B may form a team if each of them is the best possible teammate (among the contestants that remain unpaired) for the other one. Can you determine who will be each person’s teammate? -----Input----- There are 2n lines in the input. The first line contains an integer n (1 ≤ n ≤ 400) — the number of teams to be formed. The i-th line (i > 1) contains i - 1 numbers a_{i}1, a_{i}2, ... , a_{i}(i - 1). Here a_{ij} (1 ≤ a_{ij} ≤ 10^6, all a_{ij} are distinct) denotes the strength of a team consisting of person i and person j (people are numbered starting from 1.) -----Output----- Output a line containing 2n numbers. The i-th number should represent the number of teammate of i-th person. -----Examples----- Input 2 6 1 2 3 4 5 Output 2 1 4 3 Input 3 487060 3831 161856 845957 794650 976977 83847 50566 691206 498447 698377 156232 59015 382455 626960 Output 6 5 4 3 2 1 -----Note----- In the first sample, contestant 1 and 2 will be teammates and so do contestant 3 and 4, so the teammate of contestant 1, 2, 3, 4 will be 2, 1, 4, 3 respectively. 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. Alex, Bob and Carl will soon participate in a team chess tournament. Since they are all in the same team, they have decided to practise really hard before the tournament. But it's a bit difficult for them because chess is a game for two players, not three. So they play with each other according to following rules: Alex and Bob play the first game, and Carl is spectating; When the game ends, the one who lost the game becomes the spectator in the next game, and the one who was spectating plays against the winner. Alex, Bob and Carl play in such a way that there are no draws. Today they have played n games, and for each of these games they remember who was the winner. They decided to make up a log of games describing who won each game. But now they doubt if the information in the log is correct, and they want to know if the situation described in the log they made up was possible (that is, no game is won by someone who is spectating if Alex, Bob and Carl play according to the rules). Help them to check it! -----Input----- The first line contains one integer n (1 ≤ n ≤ 100) — the number of games Alex, Bob and Carl played. Then n lines follow, describing the game log. i-th line contains one integer a_{i} (1 ≤ a_{i} ≤ 3) which is equal to 1 if Alex won i-th game, to 2 if Bob won i-th game and 3 if Carl won i-th game. -----Output----- Print YES if the situation described in the log was possible. Otherwise print NO. -----Examples----- Input 3 1 1 2 Output YES Input 2 1 2 Output NO -----Note----- In the first example the possible situation is: Alex wins, Carl starts playing instead of Bob; Alex wins, Bob replaces Carl; Bob wins. The situation in the second example is impossible because Bob loses the first game, so he cannot win the second one. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Given an integer $x$. Your task is to find out how many positive integers $n$ ($1 \leq n \leq x$) satisfy $$n \cdot a^n \equiv b \quad (\textrm{mod}\;p),$$ where $a, b, p$ are all known constants. -----Input----- The only line contains four integers $a,b,p,x$ ($2 \leq p \leq 10^6+3$, $1 \leq a,b < p$, $1 \leq x \leq 10^{12}$). It is guaranteed that $p$ is a prime. -----Output----- Print a single integer: the number of possible answers $n$. -----Examples----- Input 2 3 5 8 Output 2 Input 4 6 7 13 Output 1 Input 233 233 10007 1 Output 1 -----Note----- In the first sample, we can see that $n=2$ and $n=8$ are possible answers. 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. Implement `String#ipv4_address?`, which should return true if given object is an IPv4 address - four numbers (0-255) separated by dots. It should only accept addresses in canonical representation, so no leading `0`s, spaces etc. Write your solution by modifying this code: ```python def ipv4_address(address): ``` Your solution should implemented in the function "ipv4_address". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. After rejecting 10^{100} data structure problems, Errorgorn is very angry at Anton and decided to kill him. Anton's DNA can be represented as a string a which only contains the characters "ANTON" (there are only 4 distinct characters). Errorgorn can change Anton's DNA into string b which must be a permutation of a. However, Anton's body can defend against this attack. In 1 second, his body can swap 2 adjacent characters of his DNA to transform it back to a. Anton's body is smart and will use the minimum number of moves. To maximize the chance of Anton dying, Errorgorn wants to change Anton's DNA the string that maximizes the time for Anton's body to revert his DNA. But since Errorgorn is busy making more data structure problems, he needs your help to find the best string B. Can you help him? Input The first line of input contains a single integer t (1 ≤ t ≤ 100000) — the number of testcases. The first and only line of each testcase contains 1 string a (1 ≤ |a| ≤ 100000). a consists of only the characters "A", "N", "O" and "T". It is guaranteed that the sum of |a| over all testcases does not exceed 100000. Output For each testcase, print a single string, b. If there are multiple answers, you can output any one of them. b must be a permutation of the string a. Example Input 4 ANTON NAAN AAAAAA OAANTTON Output NNOTA AANN AAAAAA TNNTAOOA Note For the first testcase, it takes 7 seconds for Anton's body to transform NNOTA to ANTON: NNOTA → NNOAT → NNAOT → NANOT → NANTO → ANNTO → ANTNO → ANTON. Note that you cannot output strings such as AANTON, ANTONTRYGUB, AAAAA and anton as it is not a permutation of ANTON. For the second testcase, it takes 2 seconds for Anton's body to transform AANN to NAAN. Note that other strings such as NNAA and ANNA will also be accepted. 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. As a token of his gratitude, Takahashi has decided to give his mother an integer sequence. The sequence A needs to satisfy the conditions below: - A consists of integers between X and Y (inclusive). - For each 1\leq i \leq |A|-1, A_{i+1} is a multiple of A_i and strictly greater than A_i. Find the maximum possible length of the sequence. -----Constraints----- - 1 \leq X \leq Y \leq 10^{18} - All input values are integers. -----Input----- Input is given from Standard Input in the following format: X Y -----Output----- Print the maximum possible length of the sequence. -----Sample Input----- 3 20 -----Sample Output----- 3 The sequence 3,6,18 satisfies the conditions. 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. Frugal bought a new house. He feels deeply in love with his new house because it has a comfortable living room in which he can put himself completely at ease. He thinks his new house is a really good buy. But, to his disappointment, the floor of its living room has some scratches on it. The floor has a rectangle shape, covered with square panels. He wants to replace all the scratched panels with flawless panels, but he cannot afford to do so. Then, he decides to cover all the scratched panels with carpets. The features of the carpets he can use are as follows. 1. Carpets are square-shaped. 2. Carpets may overlap each other. 3. Carpets cannot be folded. 4. Different sizes of carpets are available. Lengths of sides of carpets are multiples of that of the panels. The carpets must cover all the scratched panels, but must not cover any of the flawless ones. For example, if the scratched panels are as shown in Figure 1, at least 6 carpets are needed. <image> --- Figure 1: Example Covering As carpets cost the same irrespective of their sizes, Mr. Frugal would like to use as few number of carpets as possible. Your job is to write a program which tells the minimum number of the carpets to cover all the scratched panels. Input The input consists of multiple data sets. As in the following, the end of the input is indicated by a line containing two zeros. > DataSet1 > DataSet2 > ... > DataSetn > `0` `0` Each data set (DataSeti) represents the state of a floor. The format of a data set is as follows. > W H > P11 P12 P13 ... P1W > P21 P22 P23 ... P2W > ... > PH1 PH2 PH3 ... PHW > The positive integers W and H are the numbers of panels on the living room in the x- and y- direction, respectively. The values of W and H are no more than 10. The integer Pyx represents the state of the panel. The value of Pyx means, > `0`: flawless panel (must not be covered), > `1`: scratched panel (must be covered). Output For each data set, your program should output a line containing one integer which represents the minimum number of the carpets to cover all of the scratched panels. Example Input 4 3 0 1 1 1 1 1 1 1 1 1 1 1 8 5 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 0 1 1 1 0 1 1 1 8 8 0 1 1 0 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 0 1 1 0 10 10 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 0 0 Output 2 6 14 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. The purpose of this series is developing understanding of stastical problems in AS and A level maths. Let's get started with a simple concept in statistics: Mutually exclusive events. The probability of an OR event is calculated by the following rule: `P(A || B) = P(A) + P(B) - P(A && B)` The probability of event A or event B happening is equal to the probability of event A plus the probability of event B minus the probability of event A and event B happening simultaneously. Mutually exclusive events are events that cannot happen at the same time. For example, the head and tail results of a toin coss are mutually exclusive because they can't both happen at once. Thus, the above example for a coin toss would look like this: `P(H || T) = P(H) + P(T) - P(H && T)` Note that the probaility of tossing a coin and the result being both head and tails is 0. `P(H || T) = (0.5) + (0.5) - (0)` `P(H || T) = 1` Thus the probability of a coin toss result being a heads or a tails is 1, in other words: certain. Your task: You are going to have to work out the probability of one roll of a die returning two given outcomes, or rolls. Given that dice rolls are mutually exclusive, you will have to implement the above forumala. To make this interesting (this is a coding challenge after all), these dice are not fair and thus the probabilites of receiving each roll is different. You will be given a two-dimensional array containing the number each of the results (1-6) of the die and the probability of that roll for example `[1 , 0.23]` as well as the two rolls for example `1` and `5`. Given the two roll probabilities to calculate, return the probability of a single roll of the die returning either. If the total probability of the six rolls doesn't add up to one, there is a problem with the die; in this case, return null. Return your result as a string to two decimal places. Example below: `1 : 1/6` `2 : 1/6` `3 : 1/6` `4 : 1/6` `5 : 1/6` `6 : 1/6` If asked for the rolls `1` and `2` then you would need to sum the probabilities, both `1/6` therefore `2/6` and return this. As above, you will need to return it as a decimal and not a fraction. Write your solution by modifying this code: ```python def mutually_exclusive(dice, call1, call2): ``` Your solution should implemented in the function "mutually_exclusive". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. A Narcissistic Number is a number of length n in which the sum of its digits to the power of n is equal to the original number. If this seems confusing, refer to the example below. Ex: 153, where n = 3 (number of digits in 153) 1^(3) + 5^(3) + 3^(3) = 153 Write a method is_narcissistic(i) (in Haskell: isNarcissistic :: Integer -> Bool) which returns whether or not i is a Narcissistic Number. Write your solution by modifying this code: ```python def is_narcissistic(i): ``` Your solution should implemented in the function "is_narcissistic". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Dima and his friends have been playing hide and seek at Dima's place all night. As a result, Dima's place got messy. In the morning they decided that they need to clean the place. To decide who exactly would clean the apartment, the friends want to play a counting-out game. First, all the guys stand in a circle, and then each of them shows some number of fingers on one hand (one to five), and then the boys count in a circle, starting from Dima, the number of people, respective to the total number of fingers shown. The person on who the countdown stops will clean the apartment. For example, if Dima and one of his friends played hide and seek, and 7 fingers were shown during the counting-out, then Dima would clean the place. If there were 2 or say, 8 fingers shown, then his friend would clean the place. Dima knows how many fingers each of his friends will show during the counting-out. Now he is interested in the number of ways to show some number of fingers on one hand (one to five), so that he did not have to clean the place. Help Dima. -----Input----- The first line contains integer n (1 ≤ n ≤ 100) — the number of Dima's friends. Dima himself isn't considered to be his own friend. The second line contains n positive integers, not exceeding 5, representing, how many fingers the Dima's friends will show. The numbers in the lines are separated by a single space. -----Output----- In a single line print the answer to the problem. -----Examples----- Input 1 1 Output 3 Input 1 2 Output 2 Input 2 3 5 Output 3 -----Note----- In the first sample Dima can show 1, 3 or 5 fingers. If Dima shows 3 fingers, then the counting-out will go like that: Dima, his friend, Dima, his friend. In the second sample Dima can show 2 or 4 fingers. 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 all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to steal the money from rich, and return it to the poor. There are n citizens in Kekoland, each person has ci coins. Each day, Robin Hood will take exactly 1 coin from the richest person in the city and he will give it to the poorest person (poorest person right after taking richest's 1 coin). In case the choice is not unique, he will select one among them at random. Sadly, Robin Hood is old and want to retire in k days. He decided to spend these last days with helping poor people. After taking his money are taken by Robin Hood richest person may become poorest person as well, and it might even happen that Robin Hood will give his money back. For example if all people have same number of coins, then next day they will have same number of coins too. Your task is to find the difference between richest and poorest persons wealth after k days. Note that the choosing at random among richest and poorest doesn't affect the answer. Input The first line of the input contains two integers n and k (1 ≤ n ≤ 500 000, 0 ≤ k ≤ 109) — the number of citizens in Kekoland and the number of days left till Robin Hood's retirement. The second line contains n integers, the i-th of them is ci (1 ≤ ci ≤ 109) — initial wealth of the i-th person. Output Print a single line containing the difference between richest and poorest peoples wealth. Examples Input 4 1 1 1 4 2 Output 2 Input 3 1 2 2 2 Output 0 Note Lets look at how wealth changes through day in the first sample. 1. [1, 1, 4, 2] 2. [2, 1, 3, 2] or [1, 2, 3, 2] So the answer is 3 - 1 = 2 In second sample wealth will remain the same for each person. 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` (`a < b <= 20000`). Complete the function which returns a list of all those numbers in the interval `[a, b)` whose digits are made up of prime numbers (`2, 3, 5, 7`) but which are not primes themselves. Be careful about your timing! Good luck :) Write your solution by modifying this code: ```python def not_primes(a, b): ``` Your solution should implemented in the function "not_primes". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. In Berland each high school student is characterized by academic performance — integer value between 1 and 5. In high school 0xFF there are two groups of pupils: the group A and the group B. Each group consists of exactly n students. An academic performance of each student is known — integer value between 1 and 5. The school director wants to redistribute students between groups so that each of the two groups has the same number of students whose academic performance is equal to 1, the same number of students whose academic performance is 2 and so on. In other words, the purpose of the school director is to change the composition of groups, so that for each value of academic performance the numbers of students in both groups are equal. To achieve this, there is a plan to produce a series of exchanges of students between groups. During the single exchange the director selects one student from the class A and one student of class B. After that, they both change their groups. Print the least number of exchanges, in order to achieve the desired equal numbers of students for each academic performance. -----Input----- The first line of the input contains integer number n (1 ≤ n ≤ 100) — number of students in both groups. The second line contains sequence of integer numbers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 5), where a_{i} is academic performance of the i-th student of the group A. The third line contains sequence of integer numbers b_1, b_2, ..., b_{n} (1 ≤ b_{i} ≤ 5), where b_{i} is academic performance of the i-th student of the group B. -----Output----- Print the required minimum number of exchanges or -1, if the desired distribution of students can not be obtained. -----Examples----- Input 4 5 4 4 4 5 5 4 5 Output 1 Input 6 1 1 1 1 1 1 5 5 5 5 5 5 Output 3 Input 1 5 3 Output -1 Input 9 3 2 5 5 2 3 3 3 2 4 1 4 1 1 2 4 4 1 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.
Solve the programming task below in a Python markdown code block. A star is a figure of the following type: an asterisk character '*' in the center of the figure and four rays (to the left, right, top, bottom) of the same positive length. The size of a star is the length of its rays. The size of a star must be a positive number (i.e. rays of length $0$ are not allowed). Let's consider empty cells are denoted by '.', then the following figures are stars: [Image] The leftmost figure is a star of size $1$, the middle figure is a star of size $2$ and the rightmost figure is a star of size $3$. You are given a rectangular grid of size $n \times m$ consisting only of asterisks '*' and periods (dots) '.'. Rows are numbered from $1$ to $n$, columns are numbered from $1$ to $m$. Your task is to draw this grid using any number of stars or find out that it is impossible. Stars can intersect, overlap or even coincide with each other. The number of stars in the output can't exceed $n \cdot m$. Each star should be completely inside the grid. You can use stars of same and arbitrary sizes. In this problem, you do not need to minimize the number of stars. Just find any way to draw the given grid with at most $n \cdot m$ stars. -----Input----- The first line of the input contains two integers $n$ and $m$ ($3 \le n, m \le 1000$) — the sizes of the given grid. The next $n$ lines contains $m$ characters each, the $i$-th line describes the $i$-th row of the grid. It is guaranteed that grid consists of characters '*' and '.' only. -----Output----- If it is impossible to draw the given grid using stars only, print "-1". Otherwise in the first line print one integer $k$ ($0 \le k \le n \cdot m$) — the number of stars needed to draw the given grid. The next $k$ lines should contain three integers each — $x_j$, $y_j$ and $s_j$, where $x_j$ is the row index of the central star character, $y_j$ is the column index of the central star character and $s_j$ is the size of the star. Each star should be completely inside the grid. -----Examples----- Input 6 8 ....*... ...**... ..*****. ...**... ....*... ........ Output 3 3 4 1 3 5 2 3 5 1 Input 5 5 .*... ****. .**** ..**. ..... Output 3 2 2 1 3 3 1 3 4 1 Input 5 5 .*... ***.. .*... .*... ..... Output -1 Input 3 3 *.* .*. *.* Output -1 -----Note----- In the first example the output 2 3 4 1 3 5 2 is also correct. 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. Mothers arranged a dance party for the children in school. At that party, there are only mothers and their children. All are having great fun on the dance floor when suddenly all the lights went out. It's a dark night and no one can see each other. But you were flying nearby and you can see in the dark and have ability to teleport people anywhere you want. Legend: -Uppercase letters stands for mothers, lowercase stand for their children, i.e. "A" mother's children are "aaaa". -Function input: String contains only letters, uppercase letters are unique. Task: Place all people in alphabetical order where Mothers are followed by their children, i.e. "aAbaBb" => "AaaBbb". Write your solution by modifying this code: ```python def find_children(dancing_brigade): ``` Your solution should implemented in the function "find_children". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Do you know the story about the three musketeers? Anyway, you must help them now. Richelimakieu is a cardinal in the city of Bearis. He found three brave warriors and called them the three musketeers. Athos has strength a, Borthos strength b, and Caramis has strength c. The year 2015 is almost over and there are still n criminals to be defeated. The i-th criminal has strength ti. It's hard to defeat strong criminals — maybe musketeers will have to fight together to achieve it. Richelimakieu will coordinate musketeers' actions. In each hour each musketeer can either do nothing or be assigned to one criminal. Two or three musketeers can be assigned to the same criminal and then their strengths are summed up. A criminal can be defeated in exactly one hour (also if two or three musketeers fight him). Richelimakieu can't allow the situation where a criminal has strength bigger than the sum of strengths of musketeers fighting him — a criminal would win then! In other words, there are three ways to defeat a criminal. * A musketeer of the strength x in one hour can defeat a criminal of the strength not greater than x. So, for example Athos in one hour can defeat criminal i only if ti ≤ a. * Two musketeers can fight together and in one hour defeat a criminal of the strength not greater than the sum of strengths of these two musketeers. So, for example Athos and Caramis in one hour can defeat criminal i only if ti ≤ a + c. Note that the third remaining musketeer can either do nothing or fight some other criminal. * Similarly, all three musketeers can fight together and in one hour defeat a criminal of the strength not greater than the sum of musketeers' strengths, i.e. ti ≤ a + b + c. Richelimakieu doesn't want musketeers to fight during the New Year's Eve. Thus, he must coordinate their actions in order to minimize the number of hours till all criminals will be defeated. Find the minimum number of hours to defeat all criminals. If musketeers can't defeat them all then print "-1" (without the quotes) instead. Input The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of criminals. The second line contains three integers a, b and c (1 ≤ a, b, c ≤ 108) — strengths of musketeers. The third line contains n integers t1, t2, ..., tn (1 ≤ ti ≤ 108) — strengths of criminals. Output Print one line with the answer. If it's impossible to defeat all criminals, print "-1" (without the quotes). Otherwise, print the minimum number of hours the three musketeers will spend on defeating all criminals. Examples Input 5 10 20 30 1 1 1 1 50 Output 2 Input 5 10 20 30 1 1 1 1 51 Output 3 Input 7 30 20 10 34 19 50 33 88 15 20 Output -1 Input 6 10 5 10 10 9 5 25 20 5 Output 3 Note In the first sample Athos has strength 10, Borthos 20, and Caramis 30. They can defeat all criminals in two hours: * Borthos and Caramis should together fight a criminal with strength 50. In the same hour Athos can fight one of four criminals with strength 1. * There are three criminals left, each with strength 1. Each musketeer can fight one criminal in the second hour. In the second sample all three musketeers must together fight a criminal with strength 51. It takes one hour. In the second hour they can fight separately, each with one criminal. In the third hour one criminal is left and any of musketeers can fight him. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Consider the following series: `0,1,2,3,4,5,6,7,8,9,10,22,11,20,13,24...`There is nothing special between numbers `0` and `10`. Let's start with the number `10` and derive the sequence. `10` has digits `1` and `0`. The next possible number that does not have a `1` or a `0` is `22`. All other numbers between `10` and `22` have a `1` or a `0`. From `22`, the next number that does not have a `2` is `11`. Note that `30` is also a possibility because it is the next *higher* number that does not have a `2`, but we must select the *lowest* number that fits and **is not already in the sequence**. From `11`, the next lowest number that does not have a `1` is `20`. From `20`, the next lowest number that does not have a `2` or a `0` is `13`, then `24` , then `15` and so on. Once a number appers in the series, it cannot appear again. You will be given an index number and your task will be return the element at that position. See test cases for more examples. Note that the test range is `n <= 500`. Good luck! If you like this Kata, please try: [Sequence convergence](https://www.codewars.com/kata/59971e64bfccc70748000068) [https://www.codewars.com/kata/unique-digit-sequence-ii-optimization-problem](https://www.codewars.com/kata/unique-digit-sequence-ii-optimization-problem) Write your solution by modifying this code: ```python def find_num(n): ``` Your solution should implemented in the function "find_num". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Kostya likes Codeforces contests very much. However, he is very disappointed that his solutions are frequently hacked. That's why he decided to obfuscate (intentionally make less readable) his code before upcoming contest. To obfuscate the code, Kostya first looks at the first variable name used in his program and replaces all its occurrences with a single symbol a, then he looks at the second variable name that has not been replaced yet, and replaces all its occurrences with b, and so on. Kostya is well-mannered, so he doesn't use any one-letter names before obfuscation. Moreover, there are at most 26 unique identifiers in his programs. You are given a list of identifiers of some program with removed spaces and line breaks. Check if this program can be a result of Kostya's obfuscation. -----Input----- In the only line of input there is a string S of lowercase English letters (1 ≤ |S| ≤ 500) — the identifiers of a program with removed whitespace characters. -----Output----- If this program can be a result of Kostya's obfuscation, print "YES" (without quotes), otherwise print "NO". -----Examples----- Input abacaba Output YES Input jinotega Output NO -----Note----- In the first sample case, one possible list of identifiers would be "number string number character number string number". Here how Kostya would obfuscate the program: replace all occurences of number with a, the result would be "a string a character a string a", replace all occurences of string with b, the result would be "a b a character a b a", replace all occurences of character with c, the result would be "a b a c a b a", all identifiers have been replaced, thus the obfuscation is finished. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Polycarpus got an internship in one well-known social network. His test task is to count the number of unique users who have visited a social network during the day. Polycarpus was provided with information on all user requests for this time period. For each query, we know its time... and nothing else, because Polycarpus has already accidentally removed the user IDs corresponding to the requests from the database. Thus, it is now impossible to determine whether any two requests are made by the same person or by different people. But wait, something is still known, because that day a record was achieved — M simultaneous users online! In addition, Polycarpus believes that if a user made a request at second s, then he was online for T seconds after that, that is, at seconds s, s + 1, s + 2, ..., s + T - 1. So, the user's time online can be calculated as the union of time intervals of the form [s, s + T - 1] over all times s of requests from him. Guided by these thoughts, Polycarpus wants to assign a user ID to each request so that: the number of different users online did not exceed M at any moment, at some second the number of distinct users online reached value M, the total number of users (the number of distinct identifiers) was as much as possible. Help Polycarpus cope with the test. -----Input----- The first line contains three integers n, M and T (1 ≤ n, M ≤ 20 000, 1 ≤ T ≤ 86400) — the number of queries, the record number of online users and the time when the user was online after a query was sent. Next n lines contain the times of the queries in the format "hh:mm:ss", where hh are hours, mm are minutes, ss are seconds. The times of the queries follow in the non-decreasing order, some of them can coincide. It is guaranteed that all the times and even all the segments of type [s, s + T - 1] are within one 24-hour range (from 00:00:00 to 23:59:59). -----Output----- In the first line print number R — the largest possible number of distinct users. The following n lines should contain the user IDs for requests in the same order in which the requests are given in the input. User IDs must be integers from 1 to R. The requests of the same user must correspond to the same identifiers, the requests of distinct users must correspond to distinct identifiers. If there are multiple solutions, print any of them. If there is no solution, print "No solution" (without the quotes). -----Examples----- Input 4 2 10 17:05:53 17:05:58 17:06:01 22:39:47 Output 3 1 2 2 3 Input 1 2 86400 00:00:00 Output No solution -----Note----- Consider the first sample. The user who sent the first request was online from 17:05:53 to 17:06:02, the user who sent the second request was online from 17:05:58 to 17:06:07, the user who sent the third request, was online from 17:06:01 to 17:06:10. Thus, these IDs cannot belong to three distinct users, because in that case all these users would be online, for example, at 17:06:01. That is impossible, because M = 2. That means that some two of these queries belonged to the same user. One of the correct variants is given in the answer to the sample. For it user 1 was online from 17:05:53 to 17:06:02, user 2 — from 17:05:58 to 17:06:10 (he sent the second and third queries), user 3 — from 22:39:47 to 22:39:56. In the second sample there is only one query. So, only one user visited the network within the 24-hour period and there couldn't be two users online on the network simultaneously. (The time the user spent online is the union of time intervals for requests, so users who didn't send requests could not be online in the network.) 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 digits N_1, N_2, N_3 and N_4. Determine if these can be arranged into the sequence of digits "1974". Constraints * 0 \leq N_1, N_2, N_3, N_4 \leq 9 * N_1, N_2, N_3 and N_4 are integers. Input Input is given from Standard Input in the following format: N_1 N_2 N_3 N_4 Output If N_1, N_2, N_3 and N_4 can be arranged into the sequence of digits "1974", print `YES`; if they cannot, print `NO`. Examples Input 1 7 9 4 Output YES Input 1 9 7 4 Output YES Input 1 2 9 1 Output NO Input 4 9 0 8 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. You are given two strings $s$ and $t$, both consisting only of lowercase Latin letters. The substring $s[l..r]$ is the string which is obtained by taking characters $s_l, s_{l + 1}, \dots, s_r$ without changing the order. Each of the occurrences of string $a$ in a string $b$ is a position $i$ ($1 \le i \le |b| - |a| + 1$) such that $b[i..i + |a| - 1] = a$ ($|a|$ is the length of string $a$). You are asked $q$ queries: for the $i$-th query you are required to calculate the number of occurrences of string $t$ in a substring $s[l_i..r_i]$. -----Input----- The first line contains three integer numbers $n$, $m$ and $q$ ($1 \le n, m \le 10^3$, $1 \le q \le 10^5$) — the length of string $s$, the length of string $t$ and the number of queries, respectively. The second line is a string $s$ ($|s| = n$), consisting only of lowercase Latin letters. The third line is a string $t$ ($|t| = m$), consisting only of lowercase Latin letters. Each of the next $q$ lines contains two integer numbers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) — the arguments for the $i$-th query. -----Output----- Print $q$ lines — the $i$-th line should contain the answer to the $i$-th query, that is the number of occurrences of string $t$ in a substring $s[l_i..r_i]$. -----Examples----- Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 -----Note----- In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. While Vasya finished eating his piece of pizza, the lesson has already started. For being late for the lesson, the teacher suggested Vasya to solve one interesting problem. Vasya has an array a and integer x. He should find the number of different ordered pairs of indexes (i, j) such that a_{i} ≤ a_{j} and there are exactly k integers y such that a_{i} ≤ y ≤ a_{j} and y is divisible by x. In this problem it is meant that pair (i, j) is equal to (j, i) only if i is equal to j. For example pair (1, 2) is not the same as (2, 1). -----Input----- The first line contains 3 integers n, x, k (1 ≤ n ≤ 10^5, 1 ≤ x ≤ 10^9, 0 ≤ k ≤ 10^9), where n is the size of the array a and x and k are numbers from the statement. The second line contains n integers a_{i} (1 ≤ a_{i} ≤ 10^9) — the elements of the array a. -----Output----- Print one integer — the answer to the problem. -----Examples----- Input 4 2 1 1 3 5 7 Output 3 Input 4 2 0 5 3 1 7 Output 4 Input 5 3 1 3 3 3 3 3 Output 25 -----Note----- In first sample there are only three suitable pairs of indexes — (1, 2), (2, 3), (3, 4). In second sample there are four suitable pairs of indexes(1, 1), (2, 2), (3, 3), (4, 4). In third sample every pair (i, j) is suitable, so the answer is 5 * 5 = 25. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Little Petya very much likes gifts. Recently he has received a new laptop as a New Year gift from his mother. He immediately decided to give it to somebody else as what can be more pleasant than giving somebody gifts. And on this occasion he organized a New Year party at his place and invited n his friends there. If there's one thing Petya likes more that receiving gifts, that's watching others giving gifts to somebody else. Thus, he safely hid the laptop until the next New Year and made up his mind to watch his friends exchanging gifts while he does not participate in the process. He numbered all his friends with integers from 1 to n. Petya remembered that a friend number i gave a gift to a friend number pi. He also remembered that each of his friends received exactly one gift. Now Petya wants to know for each friend i the number of a friend who has given him a gift. Input The first line contains one integer n (1 ≤ n ≤ 100) — the quantity of friends Petya invited to the party. The second line contains n space-separated integers: the i-th number is pi — the number of a friend who gave a gift to friend number i. It is guaranteed that each friend received exactly one gift. It is possible that some friends do not share Petya's ideas of giving gifts to somebody else. Those friends gave the gifts to themselves. Output Print n space-separated integers: the i-th number should equal the number of the friend who gave a gift to friend number i. Examples Input 4 2 3 4 1 Output 4 1 2 3 Input 3 1 3 2 Output 1 3 2 Input 2 1 2 Output 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. E869120 found a chest which is likely to contain treasure. However, the chest is locked. In order to open it, he needs to enter a string S consisting of lowercase English letters. He also found a string S', which turns out to be the string S with some of its letters (possibly all or none) replaced with ?. One more thing he found is a sheet of paper with the following facts written on it: - Condition 1: The string S contains a string T as a contiguous substring. - Condition 2: S is the lexicographically smallest string among the ones that satisfy Condition 1. Print the string S. If such a string does not exist, print UNRESTORABLE. -----Constraints----- - 1 \leq |S'|, |T| \leq 50 - S' consists of lowercase English letters and ?. - T consists of lowercase English letters. -----Input----- Input is given from Standard Input in the following format: S T' -----Output----- Print the string S. If such a string does not exist, print UNRESTORABLE instead. -----Sample Input----- ?tc???? coder -----Sample Output----- atcoder There are 26 strings that satisfy Condition 1: atcoder, btcoder, ctcoder,..., ztcoder. Among them, the lexicographically smallest is atcoder, so we can say S = atcoder. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. ## Introduction Each chemical element in its neutral state has a specific number of electrons associated with it. This is represented by the **atomic number** which is noted by an integer number next to or above each element of the periodic table (as highlighted in the image above). As we move from left to right, starting from the top row of the periodic table, each element differs from its predecessor by 1 unit (electron). Electrons fill in different orbitals sets according to a specific order. Each set of orbitals, when full, contains an even number of electrons. The orbital sets are: * The _**s** orbital_ - a single orbital that can hold a maximum of 2 electrons. * The _**p** orbital set_ - can hold 6 electrons. * The _**d** orbital set_ - can hold 10 electrons. * The _**f** orbital set_ - can hold 14 electrons. The order in which electrons are filling the different set of orbitals is shown in the picture above. First electrons will occupy the **1s** orbital, then the **2s**, then the **2p** set, **3s** and so on. Electron configurations show how the number of electrons of an element is distributed across each orbital set. Each orbital is written as a sequence that follows the order in the picture, joined by the number of electrons contained in that orbital set. The final electron configuration is a single string of orbital names and number of electrons per orbital set where the first 2 digits of each substring represent the orbital name followed by a number that states the number of electrons that the orbital set contains. For example, a string that demonstrates an electron configuration of a chemical element that contains 10 electrons is: `1s2 2s2 2p6`. This configuration shows that there are two electrons in the `1s` orbital set, two electrons in the `2s` orbital set, and six electrons in the `2p` orbital set. `2 + 2 + 6 = 10` electrons total. ___ # Task Your task is to write a function that displays the electron configuration built according to the Madelung rule of all chemical elements of the periodic table. The argument will be the symbol of a chemical element, as displayed in the periodic table. **Note**: There will be a preloaded array called `ELEMENTS` with chemical elements sorted by their atomic number. For example, when the element "O" is fed into the function the output should look like: `"O -> 1s2 2s2 2p4"` However, there are some exceptions! The electron configurations of the elements below should end as: ``` Cr -> ...3d5 4s1 Cu -> ...3d10 4s1 Nb -> ...4d4 5s1 Mo -> ...4d5 5s1 Ru -> ...4d7 5s1 Rh -> ...4d8 5s1 Pd -> ...4d10 5s0 Ag -> ...4d10 5s1 La -> ...4f0 5d1 Ce -> ...4f1 5d1 Gd -> ...4f7 5d1 6s2 Pt -> ...4f14 5d9 6s1 Au -> ...4f14 5d10 6s1 Ac -> ...5f0 6d1 7s2 Th -> ...5f0 6d2 7s2 Pa -> ...5f2 6d1 7s2 U -> ...5f3 6d1 7s2 Np -> ...5f4 6d1 7s2 Cm -> ...5f7 6d1 7s2 ``` **Note**: for `Ni` the electron configuration should be `3d8 4s2` instead of `3d9 4s1`. Write your solution by modifying this code: ```python ELEMENTS = ['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne', 'Na', 'Mg', 'Al', 'Si', 'P', 'S', 'Cl', 'Ar', 'K', 'Ca', 'Sc', 'Ti', 'V', 'Cr', 'Mn', 'Fe', 'Co', 'Ni', 'Cu', 'Zn', 'Ga', 'Ge', 'As', 'Se', 'Br', 'Kr', 'Rb', 'Sr', 'Y', 'Zr', 'Nb', 'Mo', 'Tc', 'Ru', 'Rh', 'Pd', 'Ag', 'Cd', 'In', 'Sn', 'Sb', 'Te', 'I', 'Xe', 'Cs', 'Ba', 'La', 'Ce', 'Pr', 'Nd', 'Pm', 'Sm', 'Eu', 'Gd', 'Tb', 'Dy', 'Ho', 'Er', 'Tm', 'Yb', 'Lu', 'Hf', 'Ta', 'W', 'Re', 'Os', 'Ir', 'Pt', 'Au', 'Hg', 'Tl', 'Pb', 'Bi', 'Po', 'At', 'Rn', 'Fr', 'Ra', 'Ac', 'Th', 'Pa', 'U', 'Np', 'Pu', 'Am', 'Cm', 'Bk', 'Cf', 'Es', 'Fm', 'Md', 'No', 'Lr', 'Rf', 'Db', 'Sg', 'Bh', 'Hs', 'Mt', 'Ds', 'Rg', 'Cn', 'Nh', 'Fl', 'Mc', 'Lv', 'Ts', 'Og'] def get_electron_configuration(element): ``` Your solution should implemented in the function "get_electron_configuration". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Today we will be playing a red and white colouring game (no, this is not the Russian Civil War; these are just the colours of the Canadian flag). You are given an $n \times m$ grid of "R", "W", and "." characters. "R" is red, "W" is white and "." is blank. The neighbours of a cell are those that share an edge with it (those that only share a corner do not count). Your job is to colour the blank cells red or white so that every red cell only has white neighbours (and no red ones) and every white cell only has red neighbours (and no white ones). You are not allowed to recolour already coloured cells. -----Input----- The first line contains $t$ ($1 \le t \le 100$), the number of test cases. In each test case, the first line will contain $n$ ($1 \le n \le 50$) and $m$ ($1 \le m \le 50$), the height and width of the grid respectively. The next $n$ lines will contain the grid. Each character of the grid is either 'R', 'W', or '.'. -----Output----- For each test case, output "YES" if there is a valid grid or "NO" if there is not. If there is, output the grid on the next $n$ lines. If there are multiple answers, print any. In the output, the "YES"s and "NO"s are case-insensitive, meaning that outputs such as "yEs" and "nO" are valid. However, the grid is case-sensitive. -----Examples----- Input 3 4 6 .R.... ...... ...... .W.... 4 4 .R.W .... .... .... 5 1 R W R W R Output YES WRWRWR RWRWRW WRWRWR RWRWRW NO YES R W R W R -----Note----- The answer for the first example case is given in the example output, and it can be proven that no grid exists that satisfies the requirements of the second example case. In the third example all cells are initially coloured, and the colouring is valid. 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. Good evening, contestants. If a and d are relatively prime positive integers, the arithmetic sequence beginning with a and increasing by d, i.e., a, a + d, a + 2d, a + 3d, a + 4d, ..., contains infinitely many prime numbers. This fact is known as Dirichlet's Theorem on Arithmetic Progressions, which had been conjectured by Johann Carl Friedrich Gauss (1777 - 1855) and was proved by Johann Peter Gustav Lejeune Dirichlet (1805 - 1859) in 1837. For example, the arithmetic sequence beginning with 2 and increasing by 3, i.e., > 2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 41, 44, 47, 50, 53, 56, 59, 62, 65, 68, 71, 74, 77, 80, 83, 86, 89, 92, 95, 98, ... , contains infinitely many prime numbers > 2, 5, 11, 17, 23, 29, 41, 47, 53, 59, 71, 83, 89, ... . Your mission, should you decide to accept it, is to write a program to find the nth prime number in this arithmetic sequence for given positive integers a, d, and n. As always, should you or any of your team be tired or confused, the secretary disavow any knowledge of your actions. This judge system will self-terminate in three hours. Good luck! Input The input is a sequence of datasets. A dataset is a line containing three positive integers a, d, and n separated by a space. a and d are relatively prime. You may assume a <= 9307, d <= 346, and n <= 210. The end of the input is indicated by a line containing three zeros separated by a space. It is not a dataset. Output The output should be composed of as many lines as the number of the input datasets. Each line should contain a single integer and should never contain extra characters. The output integer corresponding to a dataset a, d, n should be the nth prime number among those contained in the arithmetic sequence beginning with a and increasing by d. FYI, it is known that the result is always less than 106 (one million) under this input condition. Example Input 367 186 151 179 10 203 271 37 39 103 230 1 27 104 185 253 50 85 1 1 1 9075 337 210 307 24 79 331 221 177 259 170 40 269 58 102 0 0 0 Output 92809 6709 12037 103 93523 14503 2 899429 5107 412717 22699 25673 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 There are $ N $ Amidakuji with 3 vertical lines. No matter which line you start from, the Amidakuji that ends at the starting line is considered a good Amidakuji. You can select one or more Amidakuji and connect them vertically in any order. Output "yes" if you can make a good Amidakuji, otherwise output "no". There are $ w_i $ horizontal lines in the $ i $ th Amidakuji. $ a_ {i, j} $ indicates whether the $ j $ th horizontal bar from the top of Amidakuji $ i $ extends from the central vertical line to the left or right. If $ a_ {i, j} $ is 0, it means that it extends to the left, and if it is 1, it means that it extends to the right. Constraints The input satisfies the following conditions. * $ 1 \ le N \ le 50 $ * $ 0 \ le w_i \ le 100 $ * $ a_ {i, j} $ is 0 or 1 Input The input is given in the following format. $ N $ $ w_1 $ $ a_ {1,1} $ $ a_ {1,2} $ ... $ a_ {1, w_1} $ $ w_2 $ $ a_ {2,1} $ $ a_ {2,2} $ ... $ a_ {2, w_2} $ ... $ w_N $ $ a_ {N, 1} $ $ a_ {N, 2} $ ... $ a_ {N, w_N} $ All inputs are given as integers. $ N $ is given on the first line. $ W_i $ and $ w_i $ $ a_ {i, j} $ are given on the following $ N $ line, separated by blanks. Output If you can make a good Amidakuji, it outputs "yes", otherwise it outputs "no". Examples Input 3 2 0 0 1 1 5 1 0 0 1 0 Output yes Input 2 1 1 1 0 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. Array inversion indicates how far the array is from being sorted. Inversions are pairs of elements in array that are out of order. ## Examples ``` [1, 2, 3, 4] => 0 inversions [1, 3, 2, 4] => 1 inversion: 2 and 3 [4, 1, 2, 3] => 3 inversions: 4 and 1, 4 and 2, 4 and 3 [4, 3, 2, 1] => 6 inversions: 4 and 3, 4 and 2, 4 and 1, 3 and 2, 3 and 1, 2 and 1 ``` ## Goal The goal is to come up with a function that can calculate inversions for any arbitrary array Write your solution by modifying this code: ```python def count_inversions(array): ``` Your solution should implemented in the function "count_inversions". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Problem statement N first-year students of the Faculty of Information Science and Technology of R University take the final exam of the lecture called Programming Exercise 1. The test is a perfect score of m. In other words, the score that one student can get is an integer between 0 and m. Since the teacher in charge is nasty, I am concerned about the score distribution that maximizes the difference between the average value and the median value. Output one such way of scoring. Note: The average value is the sum of the points divided by n, and the median is the (n + 1) / second (1 − indexed) from the front if n is an odd number when the points are arranged in ascending order. If it is an even number, it is the score obtained by adding the n / 2nd and n / 2 + 1th scores from the front and dividing by 2. input n \ m Constraint * An integer * 1 ≤ n ≤ 100 * 1 ≤ m ≤ 100 output Output the n-point column of the answer on a single line separated by spaces, and finally output a line break. If there are multiple types, any one can be output. sample Sample input 1 3 100 Sample output 1 0 0 100 100 \ 100 \ 0 etc. are also correct answers. Sample input 2 1 100 Sample output 2 50 Example Input 3 100 Output 0 0 100 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. A palindrome is a string $t$ which reads the same backward as forward (formally, $t[i] = t[|t| + 1 - i]$ for all $i \in [1, |t|]$). Here $|t|$ denotes the length of a string $t$. For example, the strings 010, 1001 and 0 are palindromes. You have $n$ binary strings $s_1, s_2, \dots, s_n$ (each $s_i$ consists of zeroes and/or ones). You can swap any pair of characters any number of times (possibly, zero). Characters can be either from the same string or from different strings — there are no restrictions. Formally, in one move you: choose four integer numbers $x, a, y, b$ such that $1 \le x, y \le n$ and $1 \le a \le |s_x|$ and $1 \le b \le |s_y|$ (where $x$ and $y$ are string indices and $a$ and $b$ are positions in strings $s_x$ and $s_y$ respectively), swap (exchange) the characters $s_x[a]$ and $s_y[b]$. What is the maximum number of strings you can make palindromic simultaneously? -----Input----- The first line contains single integer $Q$ ($1 \le Q \le 50$) — the number of test cases. The first line on each test case contains single integer $n$ ($1 \le n \le 50$) — the number of binary strings you have. Next $n$ lines contains binary strings $s_1, s_2, \dots, s_n$ — one per line. It's guaranteed that $1 \le |s_i| \le 50$ and all strings constist of zeroes and/or ones. -----Output----- Print $Q$ integers — one per test case. The $i$-th integer should be the maximum number of palindromic strings you can achieve simultaneously performing zero or more swaps on strings from the $i$-th test case. -----Example----- Input 4 1 0 3 1110 100110 010101 2 11111 000001 2 001 11100111 Output 1 2 2 2 -----Note----- In the first test case, $s_1$ is palindrome, so the answer is $1$. In the second test case you can't make all three strings palindromic at the same time, but you can make any pair of strings palindromic. For example, let's make $s_1 = \text{0110}$, $s_2 = \text{111111}$ and $s_3 = \text{010000}$. In the third test case we can make both strings palindromic. For example, $s_1 = \text{11011}$ and $s_2 = \text{100001}$. In the last test case $s_2$ is palindrome and you can make $s_1$ palindrome, for example, by swapping $s_1[2]$ and $s_1[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. Search trees are data structures that support dynamic set operations including insert, search, delete and so on. Thus a search tree can be used both as a dictionary and as a priority queue. Binary search tree is one of fundamental search trees. The keys in a binary search tree are always stored in such a way as to satisfy the following binary search tree property: * Let $x$ be a node in a binary search tree. If $y$ is a node in the left subtree of $x$, then $y.key \leq x.key$. If $y$ is a node in the right subtree of $x$, then $x.key \leq y.key$. The following figure shows an example of the binary search tree. <image> For example, keys of nodes which belong to the left sub-tree of the node containing 80 are less than or equal to 80, and keys of nodes which belong to the right sub-tree are more than or equal to 80. The binary search tree property allows us to print out all the keys in the tree in sorted order by an inorder tree walk. A binary search tree should be implemented in such a way that the binary search tree property continues to hold after modifications by insertions and deletions. A binary search tree can be represented by a linked data structure in which each node is an object. In addition to a key field and satellite data, each node contains fields left, right, and p that point to the nodes corresponding to its left child, its right child, and its parent, respectively. To insert a new value $v$ into a binary search tree $T$, we can use the procedure insert as shown in the following pseudo code. The insert procedure is passed a node $z$ for which $z.key = v$, $z.left = NIL$, and $z.right = NIL$. The procedure modifies $T$ and some of the fields of $z$ in such a way that $z$ is inserted into an appropriate position in the tree. 1 insert(T, z) 2 y = NIL // parent of x 3 x = 'the root of T' 4 while x ≠ NIL 5 y = x // set the parent 6 if z.key < x.key 7 x = x.left // move to the left child 8 else 9 x = x.right // move to the right child 10 z.p = y 11 12 if y == NIL // T is empty 13 'the root of T' = z 14 else if z.key < y.key 15 y.left = z // z is the left child of y 16 else 17 y.right = z // z is the right child of y Write a program which performs the following operations to a binary search tree $T$. * insert $k$: Insert a node containing $k$ as key into $T$. * print: Print the keys of the binary search tree by inorder tree walk and preorder tree walk respectively. You should use the above pseudo code to implement the insert operation. $T$ is empty at the initial state. Constraints * The number of operations $\leq 500,000$ * The number of print operations $\leq 10$. * $-2,000,000,000 \leq key \leq 2,000,000,000$ * The height of the binary tree does not exceed 100 if you employ the above pseudo code. * The keys in the binary search tree are all different. Input In the first line, the number of operations $m$ is given. In the following $m$ lines, operations represented by insert $k$ or print are given. Output For each print operation, print a list of keys obtained by inorder tree walk and preorder tree walk in a line respectively. Put a space character before each key. Example Input 8 insert 30 insert 88 insert 12 insert 1 insert 20 insert 17 insert 25 print Output 1 12 17 20 25 30 88 30 12 1 20 17 25 88 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. Taro is going to play a card game. However, now he has only n cards, even though there should be 52 cards (he has no Jokers). The 52 cards include 13 ranks of each of the four suits: spade, heart, club and diamond. Note 解説 Input In the first line, the number of cards n (n ≤ 52) is given. In the following n lines, data of the n cards are given. Each card is given by a pair of a character and an integer which represent its suit and rank respectively. A suit is represented by 'S', 'H', 'C' and 'D' for spades, hearts, clubs and diamonds respectively. A rank is represented by an integer from 1 to 13. Output Print the missing cards. The same as the input format, each card should be printed with a character and an integer separated by a space character in a line. Arrange the missing cards in the following priorities: * Print cards of spades, hearts, clubs and diamonds in this order. * If the suits are equal, print cards with lower ranks first. Example Input 47 S 10 S 11 S 12 S 13 H 1 H 2 S 6 S 7 S 8 S 9 H 6 H 8 H 9 H 10 H 11 H 4 H 5 S 2 S 3 S 4 S 5 H 12 H 13 C 1 C 2 D 1 D 2 D 3 D 4 D 5 D 6 D 7 C 3 C 4 C 5 C 6 C 7 C 8 C 9 C 10 C 11 C 13 D 9 D 10 D 11 D 12 D 13 Output S 1 H 3 H 7 C 12 D 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. Chaneka has a hobby of playing with animal toys. Every toy has a different fun value, a real number. Chaneka has four boxes to store the toys with specification: The first box stores toys with fun values in range of $(-\infty,-1]$. The second box stores toys with fun values in range of $(-1, 0)$. The third box stores toys with fun values in range of $(0, 1)$. The fourth box stores toys with fun value in range of $[1, \infty)$. Chaneka has $A$, $B$, $C$, $D$ toys in the first, second, third, and fourth box, respectively. One day she decides that she only wants one toy, a super toy. So she begins to create this super toy by sewing all the toys she has. While the number of toys Chaneka has is more than 1, she takes two different toys randomly and then sews them together, creating a new toy. The fun value of this new toy is equal to the multiplication of fun values of the sewn toys. She then puts this new toy in the appropriate box. She repeats this process until she only has one toy. This last toy is the super toy, and the box that stores this toy is the special box. As an observer, you only know the number of toys in each box initially but do not know their fun values. You also don't see the sequence of Chaneka's sewing. Determine which boxes can be the special box after Chaneka found her super toy. -----Input----- The first line has an integer $T$ $(1 \le T \le 5 \cdot 10^4)$, the number of test cases. Every case contains a line with four space-separated integers $A$ $B$ $C$ $D$ $(0 \le A, B, C, D \le 10^6, A + B + C + D > 0)$, which denotes the number of toys in the first, second, third, and fourth box, respectively. -----Output----- For each case, print four space-separated strings. Each string represents the possibility that the first, second, third, and fourth box can be the special box from left to right. For each box, print "Ya" (Without quotes, Indonesian for yes) if that box can be the special box. Print "Tidak" (Without quotes, Indonesian for No) otherwise. -----Example----- Input 2 1 2 0 1 0 1 0 0 Output Ya Ya Tidak Tidak Tidak Ya Tidak Tidak -----Note----- For the first case, here is a scenario where the first box is the special box: The first box had toys with fun values $\{-3\}$. The second box had toys with fun values $\{ -0.5, -0.5 \}$ The fourth box had toys with fun values $\{ 3 \}$ The sewing sequence: Chaneka sews the toy with fun $-0.5$ and $-0.5$ to a toy with fun $0.25$ and then put it in the third box. Chaneka sews the toy with fun $-3$ and $0.25$ to a toy with fun $-0.75$ and then put it in the second box. Chaneka sews the toy with fun $-0.75$ and $3$ to a toy with fun $-1.25$ and then put it in the first box, which then became the special box. Here is a scenario where the second box ends up being the special box: The first box had toys with fun values $\{-3\}$ The second box had toys with fun values $\{ -0.33, -0.25 \}$. The fourth box had toys with fun values $\{ 3 \}$. The sewing sequence: Chaneka sews the toy with fun $-3$ and $-0.33$ to a toy with fun $0.99$ and then put it in the third box. Chaneka sews the toy with fun $0.99$ and $3$ to a toy with fun $2.97$ and then put in it the fourth box. Chaneka sews the toy with fun $2.97$ and $-0.25$ to a toy with fun $-0.7425$ and then put it in the second box, which then became the special box. There is only one toy for the second case, so Chaneka does not have to sew anything because that toy, by definition, is the super toy. 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. Just in case somebody missed it: this winter is totally cold in Nvodsk! It is so cold that one gets funny thoughts. For example, let's say there are strings with the length exactly n, based on the alphabet of size m. Any its substring with length equal to k is a palindrome. How many such strings exist? Your task is to find their quantity modulo 1000000007 (109 + 7). Be careful and don't miss a string or two! Let us remind you that a string is a palindrome if it can be read the same way in either direction, from the left to the right and from the right to the left. Input The first and only line contains three integers: n, m and k (1 ≤ n, m, k ≤ 2000). Output Print a single integer — the number of strings of the described type modulo 1000000007 (109 + 7). Examples Input 1 1 1 Output 1 Input 5 2 4 Output 2 Note In the first sample only one string is valid: "a" (let's denote the only letter of our alphabet as "a"). In the second sample (if we denote the alphabet letters as "a" and "b") the following strings are valid: "aaaaa" and "bbbbb". 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 that takes a string and returns an array of the repeated characters (letters, numbers, whitespace) in the string. If a charater is repeated more than once, only show it once in the result array. Characters should be shown **by the order of their first repetition**. Note that this may be different from the order of first appearance of the character. Characters are case sensitive. For F# return a "char list" ## Examples: ```python remember("apple") => returns ["p"] remember("apPle") => returns [] # no repeats, "p" != "P" remember("pippi") => returns ["p","i"] # show "p" only once remember('Pippi') => returns ["p","i"] # "p" is repeated first ``` Write your solution by modifying this code: ```python def remember(str_): ``` Your solution should implemented in the function "remember". The i Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.