Dilation – is one of the simplest methods of morphological image processing. Its main purpose is to expand image objects by applying some kernel.
Suppose we have some initial image $$$A$$$ consisting only of black (objects) and white (background) pixels. If a $$$3 \times 3$$$ kernel is used for dilation, then in the target image $$$B$$$ the pixel $$$B_{i, j}$$$ will be black if it was black in the source image ($$$A_{i, j}$$$ is black) or at least one was black from neighboring pixels (some neighbors may not exist): $$$A_{i - 1, j - 1}$$$, $$$A_{i - 1, j}$$$, $$$A_{i - 1, j + 1}$$$, $$$A_{i, j - 1}$$$, $$$A_{i, j + 1}$$$, $$$A_{i + 1, j - 1}$$$, $$$A_{i + 1, j}$$$ or $$$A_{i + 1, j + 1}$$$.
Let's assume that you have a processed $$$B$$$ image. Can you restore the original one? Write a program that, based on the image $$$B$$$, builds the image from which $$$B$$$ came after dilation using a kernel of size $$$3 \times 3$$$, if possible.
The first line contains the integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 100$$$) – the number of rows and columns in the image, respectively.
This is followed by $$$n$$$ lines, each of which contains $$$m$$$ characters. Each symbol describes a pixel in the image. The white pixel is indicated by a dot «.», and the black one is a hash symbol «#». It is guaranteed that there are no other characters in the lines.
The first line should contain the word «Possible», if it is possible to restore the original image, or «Impossible» – if it is not.
If the word «Possible» was output, then print $$$n$$$ lines, each containing $$$m$$$ characters – the supposed source image, encoded in the same way as the input one.
If there are multiple solutions print any of them.
5 5......###..###..###......
Possible ..... ..... ..#.. ..... .....
3 3....#....
Impossible
The grandson of Professor R. Vanya downloaded the game 'Destroy them all!' and plays it all day long.
The game has a checkered field of size $$$n \times m$$$. It contains $$$k$$$ cells. The player can select any unmarked cell of the field and destroy all the marked cells exactly in one of the directions (left, right, above, below it). The grandson really wants to know the maximum number of cells he can destroy in one move at the current state of the field, but he is still too young to calculate this number. Help him – find the maximum number of cells that can be destroyed in one move if the coordinates of the $$$k$$$ marked cells are known.
The first line contains the numbers $$$n$$$, $$$m$$$ and $$$k$$$ $$$(1 \le n, m \le 10^9, 1 \le k \le min(n \cdot m, 2 \cdot 10^5))$$$ - the size of the playing field and the number of marked cells.
The next $$$k$$$ lines contain pairs of numbers $$$x_i$$$ and $$$y_i$$$ $$$(1 \le x_i \le n, 1 \le y_i \le m)$$$ – the coordinates of the $$$i$$$-th marked cell.
In the first line, print one number – the maximum number of cells that can be destroyed in one move. In the second line, print two numbers – the coordinates of an empty cell from which the maximum number of cells can be destroyed.
If there are no unmarked points, print «0» (without quotes) in the first line and «0 0» (without quotes) in the second line.
If there are multiple solutions print any of them.
12 12 4 3 7 3 11 6 4 12 4
2 3 4
From a point with coordinates $$$(3, 4)$$$, either two points above or two points to the right can be destroyed.
Vasya has been very interested in exploring other planets since childhood. Growing up, he was able to fulfill his dream and participate in the launch of a meteorological station to Mars. Unfortunately, an error was made during the assembly of the station. To even greater regret, it was discovered after the station started working on the surface of the planet.
The problem lies in the data from an ultra-sensitive temperature sensor capable of receiving readings with an accuracy of up to millionths of a degree. Vasya was instructed to try to solve the problem.
The sensor takes data every second and sends readings to Earth as a series of 32-bit integers. The series consists of $$$n$$$ sequential temperature measurements $$$t_1, t_2, ..., t_n$$$.
After watching the video of the satellite assembly, Vasya found out that some sensor connectors were connected backward ; as a result, some bits are transmitted in an inverted form ($$$0$$$ instead of $$$1$$$ and $$$1$$$ instead of $$$0$$$). Also, in all measurements, these bits have the same numbers (the numbering of bits traditionally starts with the least significant one, which has the number $$$0$$$). It is not clear from the video which bits are being transmitted incorrectly, but Vasya decided to try to determine them from the data received.
Vasya has a series of measurements consisting of $$$n$$$ obtained distorted numbers $$$t_1, t_2, ..., t_n$$$. From the readings of other devices, it is known that the temperature first rose and then fell during the measurement process. Vasya wants to try to select some value $$$k$$$ and apply $$$t_i$$$ to the elements using the xor operation (bitwise exclusive «OR»): $$$r_i = t_i \, xor \, k$$$. The resulting sequence $$$r_1, r_2, ..., r_n$$$ should behave like the Martian temperature. First, it does not decrease and then does not increase, and the intervals of non-decreasing and non-increasing must be of length at least 2 (the intervals themselves can intersect).
Write a program that helps Vasya find the number $$$k$$$.
The first line of the input data contains the number of measurements $$$n$$$ ($$$3 \leq n \leq 10\,000$$$).
The second line contains distorted measurements in the form of integers $$$t_i$$$ ($$$0 \leq t_i \leq 2\,147\,483\,647$$$), separated by spaces.
In a single line, print the integer $$$k$$$ ($$$0 \leq k \leq 2\,147\,483\,647$$$). It is guaranteed that the data is correct and at least one such number exists.
If there are multiple solutions print any of them.
32 1 2
2
31 2 1
0
The xor operation exists in all modern programming languages; for example, in the languages $$$C\!+\!+$$$, $$$Python$$$ and $$$Java$$$ it is designated as «$$$ ^\wedge $$$», in $$$Pascal$$$ – as «xor». An example of using the «xor» operation: $$$10101_2 \oplus 10001_2 = 00100_2$$$.
In nature, DNA molecules are responsible for the transmission of hereditary information. The structure of DNA is simple and complex at the same time – its molecules are two long polymer strands bonded together in a double helix. Each DNA strand is built based on only four nucleotides: Adenine (A – Adenine), Guanine (G – Guanine), Thymine (T – Thymine), and Cytosine (C – Cytosine). It is customary to encode nucleotides in capital Latin letters and write their sequence as a line. For example: «ACTG».
In the first strand, nucleotides are connected sequentially in any order. Still, the second strand of the helix is built strictly on the principle of complementarity: Adenine always forms a pair with Thymine (A–T or T–A) and Cytosine with Guanine (C–G or G–C). So, a nucleotide located at a particular place in the first strand uniquely defines a nucleotide situated at the same place in the second strand.
For example, if one strand consists of «ACTGTAC» nucleotides, then the second will have the form «TGACATG».
This method of recording information is redundant and, in some places, ambiguous. For example, it may happen that the same sequence of nucleotides occurs in the first strand in one direction, in the second strand – in a different place and in exactly the opposite direction.
Write a program that, based on the sequence of nucleotides in one strand, will determine the longest subsequence of consecutive nucleotides that also occurs in the complementary strand, but in the reverse sequence. The location of the searched subsequence is not important.
The input contains a single line with a length from $$$ 1 $$$ to $$$ 100\,000 $$$ characters. It is guaranteed that the string consists only of the letters «A», «C», «G» and «T».
If the searched sequence does not exist, print $$$0$$$ in a single line. If the sequence exists, indicate its length in the first line, and print the sequence itself as a chain of capital Latin letters in the second line.
If there are multiple solutions print any of them.
AGCT
4 AGCT
AACGTACGTG
8 ACGTACGT
Let's look at the second example. The complementary to the chain «AACGTACGTG» is «TTGCATGCAC». The «ACGTACGT» gene occurs both in the direct «A–ACGTACGT–G» and in the complementary chain «T–TGCATGCA–C», but in the latter it is written backwards.
Positive integers that are convenient for forming various measurement systems are called practical. A positive integer $$$x$$$ will be practical if any natural number less than $$$x$$$ can be represented as the sum of any different divisors of $$$x$$$.
For example, the number $$$6$$$ is practical since using the sum of its divisors $$$1, 2, 3, 6$$$, you can represent any number from $$$1$$$ to $$$6$$$: $$$1 = 1$$$, $$$2 = 2$$$, $$$3 = 3$$$, $$$4 = 1 + 3$$$, $$$5 = 2 + 3$$$, and $$$6 = 6$$$.
On the other hand, the number $$$9$$$ is not practical since, using its divisors $$$1, 3, 9$$$, it is impossible to represent the numbers $$$2, 5, 6, 7$$$ and $$$8$$$.
There are infinitely many practical numbers, and they have many useful properties. In particular:
Among the practical numbers, primitive practical numbers stand out - those that cannot be obtained from other practical numbers by multiplying them or multiplying a practical number by any of its divisors.
Write a program that, by the number $$$n$$$, returns the $$$n$$$-th largest primitive practical number.
The single line contains an integer $$$n$$$ ($$$1 \leq n \leq 10\,000$$$) – the number of the desired primitive practical number.
In a single line, print a positive integer – the $$$n$$$-th largest primitive practical number.
5
28
1
1
Alexander hosted tournaments in the sports version of the game "What? Where? When?" for a long time.
In sports version of "What? Where? When?" several teams of 6 people play simultaneously. The host asks a question, after which, within 60 seconds, the teams must come up with an answer to it and write it down on the answer form. After a minute to think, players have 10 seconds to hand over the forms to the moderator.
Sasha is collecting questions for a new game. The game consists of $$$R$$$ rounds, with $$$Q$$$ questions in each round. There is a database of $$$N$$$ questions; for each question, it is known how many teams tried to answer the question and how many teams answered it incorrectly. Based on this data, a question rating is calculated for each question: the proportion of incorrect answers to the question is counted, multiplied by 10000 and rounded down to an integer. The easiest question has the lowest rating. Sasha wants to form a pack of questions for the game according to the following rules:
Help Sasha: form a pack of questions for the game, that satisfies the rules above.
The first line of the input contains non-negative integers $$$N, R, Q, L, C, D, S$$$ in accordance with the statement ($$$1 \le N, R, Q \le 10^6$$$, $$$1 \le R \cdot Q \le 10^6$$$, $$$2 \le L, C \le 9999$$$, $$$L \lt C$$$, $$$1 \le D, S \le 10000$$$).
The following $$$N$$$ lines contain two non-negative integers $$$A_i, B_i$$$ — how many teams were asked the question and how many teams answered the question incorrectly ($$$0 \le B_i \le A_i \le 10^9$$$).
If it is possible to form a package without breaking the rules, then output $$$R \cdot Q$$$ integers — the numbers of questions from the database in the order in which they should be asked in the game.
If there are multiple solutions print any of them.
If it is impossible to form a package, output 0.
10 3 2 4000 6000 100 50001000 1001000 2001000 3001000 4001000 5001000 5501000 6001000 7001000 8501000 950
4 3 8 7 6 5
Veronica loves to play with her toys. Cubes are one of her favorite toys.
Veronica builds towers, pyramids, and many other forms from multi-colored cubes. Today, Veronica wants to build a tower of $$$n$$$ cubes in height. Veronica has unlimited quantities of red, green, and blue cubes. Veronica wants to build a beautiful multi-colored tower, so she wants the tower to have no more than $$$k_r$$$ in a row of red cubes, $$$k_g$$$ in a row of green cubes, and $$$k_b$$$ in a row of blue cubes.
Help Veronica to determine how many different towers with a height of $$$n$$$ cubes she can build. Since this number may be very large, print the answer modulo $$$10^9 + 7$$$.
A single line contains four integers $$$n$$$, $$$k_r$$$, $$$k_g$$$, $$$k_b$$$ $$$(1\leq n,k_r,k_g,k_b\leq 10^6)$$$ — tower height and restrictions on the number of consecutive red, green and blue cubes, respectively.
In a single line, print an integer – the number of different variants of towers, taken modulo $$$10^9 + 7$$$.
5 2 2 2
180
6 1 1 3
222
3 1 1 1
12
4 4 4 4
81
Veronika has a huge number of toys – as many as $$$n$$$ pieces. Veronica decided to determine which toys she liked the most. To do this, she put all $$$n$$$ toys in one row. She gave the first toy a value equal to $$$a_1$$$. She gave each next toy a value according to the following rule:
where $$$k$$$, $$$p$$$ — beauty parameters. Veronica wants to find out the five largest values from the whole set of her toys. Help her in this difficult task.
A single line contains four integers $$$n$$$, $$$a_1$$$, $$$k$$$, $$$p$$$ $$$(5\leq n\leq 2 \cdot 10^7, 1\leq a_1, k, p\leq 10^9)$$$ — the number of toys, the value of the first toy and the dependency parameters.
Print the five largest values among Veronica's toys, separated by space, in ascending order.
5 1 2 100
1 2 4 8 16
10 10 3 1000
430 610 810 830 870
Line pinball is played with $$$n$$$ metal balls having weights of $$$1, 2, 3, ..., n$$$ grams. Along a straight line, there are $$$n + 1$$$ holes equipped with plungers. The holes are numbered from left to right with numbers from $$$0$$$ to $$$n$$$.
Each plunger is inclined to the right and has its impact force $$$p_i$$$, where $$$i$$$ is the number of the hole. The design and inclination of the plunger are such that if it hits a ball with a mass of $$$x$$$ grams with force $$$p_i$$$, then the ball will move into the hole numbered $$$i + \lfloor p_i / x \rfloor$$$, where $$$\lfloor ... \rfloor$$$ means round down to a lower integer.
The game begins with one of the balls being placed in the starting hole numbered $$$0$$$. Next, the plungers of all the holes into which the ball falls are used sequentially. The game ends and is considered successful when the ball hits the hole number $$$n$$$. The game is considered lost if, at some point, the force of the plunger is too small to throw the ball into another hole or too strong and throws it further than the hole number $$$n$$$.
Write a program that selects the impact force $$$p_i$$$ for all $$$n$$$ plungers so that any ball with a mass from $$$1$$$ to $$$n$$$, starting from hole $$$0$$$, ends up in hole $$$n$$$.
The only line contains the integer $$$n$$$ ($$$1 \leq n \leq 50$$$).
In a single line print $$$n$$$ integers separated by spaces – the impact forces of the plungers $$$p_0, p_1, ..., p_{n-1}$$$ ($$$1 \leq p_i \leq 10^6$$$). The $$$n$$$-th hole plunger is never used, and its parameter must not be printed. It is guaranteed that a solution exists.
If there are multiple solutions print any of them.
2
2 2
3
3 5 3
Veronika really likes to visit shops because there are a lot of different colorful items. Arriving at the shop, Veronica first asks her dad to pick her up. As soon as Veronica is picked up, she immediately begins to command where to go. To do this, Veronica points her finger in the direction to go and says the word «there». Dad, of course, is happy to carry Veronica in his arms and goes in the direction shown by Veronica, but only until he meets an obstacle in the form of another customer, a shelf, or a wall.
For simplification, the shop can be represented as a rectangular matrix of size $$$n \times m$$$. Each cell of the shop can be empty and marked with the symbol «.», or occupied «#». Initially, Veronica is in the Dad's arms in the bottom left cell with coordinates $$$x$$$ equal to 1 and $$$y$$$ equal to 1. Veronica can show one of the four directions in letters, setting the movement: «U» — increasing the coordinates of $$$x$$$, «D» — decrease of the $$$x$$$ coordinate, «R» — increase of the $$$y$$$ coordinate, «L» — decrease of the $$$y$$$ coordinate.
Find the coordinates of Veronica and her dad after following all the instructions.
The first line contains two integers $$$n$$$, $$$m$$$$$$(1\leq n,m\leq 1000)$$$ — the size of the shop.
Next, $$$n$$$ lines are given, each has $$$m$$$ characters. All lines consist of the characters «.» — the cell is empty, «#» — the cell is occupied.
The last line of input contains a single line $$$s$$$, consisting of the characters «D», «U», «L», «R», denoting Veronica's commands. The length of the line $$$s$$$ does not exceed $$$2 \cdot 10^6$$$.
In a single line, print the final coordinates of the cell in which Veronica is located.
4 4................DRLU
1 4
4 4.......#........DRUL
1 2
5 6#..#..#..#........##..#.....#.RURD
6 1
Before the appearance of Veronica, Igor and Ira chose a stroller for a long time. The stroller should satisfy many parameters: it should be comfortable, warm, stable, and multifunctional. One of the essential criteria is its size when folded.
For transportation during the trip, the stroller must be located in the trunk of the car. The trunk is a rectangle with the size $$$a \times b$$$. The stroller must be transported only folded to not damage it. When folded, the stroller occupies a space of $$$c \times d$$$. Igor wants to find out if a folded stroller will fit in the trunk. Help him - write a program to determine whether it can be done.
A single line contains four integers $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$ $$$(1\leq a,b,c,d\leq 1000)$$$ — the dimensions of the trunk and the dimensions of the folded stroller.
In a single line print «YES» (without quotes), if the stroller can be placed in the trunk according to the rules, otherwise output «NO» (without quotes).
10 10 4 8
YES
8 11 10 12
NO
14 14 1 15
YES
Veronica is very good at drawing and coloring pictures. Igor and Ira bought her a new bee coloring book.
The coloring consists of $$$n$$$ lines, each contains exactly $$$m$$$ cells. Each cell is a regular hexagon, like a honeycomb in bees, and has no more than 6 neighbors. As an example, the $$$5 \times 5$$$ field looks like this:
Initially, some cells in the coloring are already colored. Veronica wants to get a continuous line of colored cells that connects the first row with the $$$n$$$-th row by coloring the minimum number of cells.
The first line contains two integers $$$n$$$, $$$m$$$ $$$(1 \leq n, m \leq 1000)$$$ — the number of lines in the coloring and the number of cells in each line.
Next, $$$n$$$ lines are given, each line contains exactly $$$m$$$ characters and each line consists only of the characters «.» and «#». The symbol «.» means that the cell is not colored, «#» — the cell is colored.
Print one integer – the minimum number of cells that need to be colored to connect the first row with the $$$n$$$-th row.
5 5....#.#....#......#..#...
2
A group of n students decided to go to the cinema. They bought a whole row of seats in the cinema for n places in advance. Of course, some seats for students are preferable to others. Therefore, each student knows the number of a seat he wants to sit in. Moreover, it is known that a student will be dissatisfied if he sits in the wrong place. You need to seat the students so that the total dissatisfaction is minimal.
In the first line, you are given a single number n – the number of students and seats (1 ≤ n ≤ 105). In the next line, you are given n numbers, each from 1 to n, i-th of them is the number of a seat the i-th student wants to sit on.
The following line contains n numbers – students' dissatisfaction. The dissatisfaction of any student is an integer not less than 0 and not exceeding 106.
In the first line, print a single number – the minimum total dissatisfaction of students.
In the following line, print a permutation of numbers from 1 to n, the ith of them should equal the seat number where the ith student should be seated. The total dissatisfaction with this should be the minimum possible.
If there are multiple solutions print any of them.
3
1 2 3
1 2 3
0
1 2 3
4
3 3 2 2
2 1 3 4
4
3 1 4 2
3
3 3 3
3 2 1
3
3 1 2
A keen entomologist, Vasya studied an ant population in the neighboring forest. He discovered a huge anthill and, after spending a lot of time, determined that $$$k$$$ ants lived in it.
Due to the imposed quarantine, Vasya could not continue his field research. However, Vasya knows that there are $$$n$$$ anthills in total in the forest, which means that one may try to apply Zipf's law to estimate their size.
Zipf's law states: if $$$k$$$ insects live in the largest anthill, then in the second largest will be approximately half as many ($$$k / 2$$$), in the third three times less ($$$k / 3$$$), and in the $$$i$$$-th largest will live $$$i$$$ times less than ($$$k / i$$$).
Vasya made a forecast – a series of integers $$$c_1, c_2, ..., c_n$$$, where $$$c_i = k / i$$$, rounded to the nearest integer (according to mathematical rules, that is, numbers like $$$1.5, 2.5, ... $$$ were rounded up).
Unfortunately, the ants living on the home ant farm did not appreciate the researcher's impulse and damaged his notes. The saddest thing is that the original number $$$k$$$ was also damaged. However, some of the $$$c_i$$$ numbers have been preserved. Help Vasya recover the lost data.
Write a program that uses the preserved records $$$c_i$$$ to find the minimum initial number $$$k$$$. If there are several such numbers, print the minimum of them.
The first line of input data contains the single integer $$$n$$$ ($$$3 \leq n \leq 1\,000$$$) – the number of anthills in the forest.
The second line contains $$$n$$$ entries separated by a space – the sequence $$$c_1, c_2, ..., c_n$$$. If the entry $$$c_i$$$ is not affected by ants, then the integer $$$c_i = k / i$$$ ($$$0 \leq c_i \leq 2\,147\,483\,647$$$) is indicated there, rounded to the nearest integer. If the record is damaged, then a question mark is indicated «?». It is guaranteed that the numbers $$$c_i$$$ are not contradictory and the desired number $$$k$$$ exists. It is also guaranteed that sequence always starts with «?».
In a single line, print an integer – the minimum value of $$$k$$$ from satisfying the conditions of the problem.
3? 3 2
5
3? 4 ?
7