contestId
int64
0
1.01k
name
stringlengths
2
58
tags
listlengths
0
11
title
stringclasses
523 values
time-limit
stringclasses
8 values
memory-limit
stringclasses
8 values
problem-description
stringlengths
0
7.15k
input-specification
stringlengths
0
2.05k
output-specification
stringlengths
0
1.5k
demo-input
listlengths
0
7
demo-output
listlengths
0
7
note
stringlengths
0
5.24k
test_cases
listlengths
0
402
timeConsumedMillis
int64
0
8k
memoryConsumedBytes
int64
0
537M
score
float64
-1
3.99
__index_level_0__
int64
0
621k
0
none
[ "none" ]
null
null
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. One day Petya dreamt of a lexicographically *k*-th permutation of integers from 1 to *n*. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
The first line contains two integers *n* and *k* (1<=≀<=*n*,<=*k*<=≀<=109) β€” the number of elements in the permutation and the lexicographical number of the permutation.
If the *k*-th permutation of numbers from 1 to *n* does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes *i*, that *i* and *a**i* are both lucky numbers.
[ "7 4\n", "4 7\n" ]
[ "1\n", "1\n" ]
A permutation is an ordered set of *n* elements, where each integer from 1 to *n* occurs exactly once. The element of permutation in position with index *i* is denoted as *a*<sub class="lower-index">*i*</sub> (1 ≀ *i* ≀ *n*). Permutation *a* is lexicographically smaller that permutation *b* if there is such a *i* (1 ≀ *i* ≀ *n*), that *a*<sub class="lower-index">*i*</sub> &lt; *b*<sub class="lower-index">*i*</sub>, and for any *j* (1 ≀ *j* &lt; *i*) *a*<sub class="lower-index">*j*</sub> = *b*<sub class="lower-index">*j*</sub>. Let's make a list of all possible permutations of *n* elements and sort it in the order of lexicographical increasing. Then the lexicographically *k*-th permutation is the *k*-th element of this list of permutations. In the first sample the permutation looks like that: 1 2 3 4 6 7 5 The only suitable position is 4. In the second sample the permutation looks like that: 2 1 3 4 The only suitable position is 4.
[ { "input": "7 4", "output": "1" }, { "input": "4 7", "output": "1" }, { "input": "7 1", "output": "2" }, { "input": "7 5040", "output": "1" }, { "input": "10 1023", "output": "0" }, { "input": "7 7477", "output": "-1" }, { "input": "10 1000...
60
0
0
5,743
416
Art Union
[ "brute force", "dp", "implementation" ]
null
null
A well-known art union called "Kalevich is Alive!" manufactures objects d'art (pictures). The union consists of *n* painters who decided to organize their work as follows. Each painter uses only the color that was assigned to him. The colors are distinct for all painters. Let's assume that the first painter uses color 1, the second one uses color 2, and so on. Each picture will contain all these *n* colors. Adding the *j*-th color to the *i*-th picture takes the *j*-th painter *t**ij* units of time. Order is important everywhere, so the painters' work is ordered by the following rules: - Each picture is first painted by the first painter, then by the second one, and so on. That is, after the *j*-th painter finishes working on the picture, it must go to the (*j*<=+<=1)-th painter (if *j*<=&lt;<=*n*); - each painter works on the pictures in some order: first, he paints the first picture, then he paints the second picture and so on; - each painter can simultaneously work on at most one picture. However, the painters don't need any time to have a rest; - as soon as the *j*-th painter finishes his part of working on the picture, the picture immediately becomes available to the next painter. Given that the painters start working at time 0, find for each picture the time when it is ready for sale.
The first line of the input contains integers *m*,<=*n* (1<=≀<=*m*<=≀<=50000,<=1<=≀<=*n*<=≀<=5), where *m* is the number of pictures and *n* is the number of painters. Then follow the descriptions of the pictures, one per line. Each line contains *n* integers *t**i*1,<=*t**i*2,<=...,<=*t**in* (1<=≀<=*t**ij*<=≀<=1000), where *t**ij* is the time the *j*-th painter needs to work on the *i*-th picture.
Print the sequence of *m* integers *r*1,<=*r*2,<=...,<=*r**m*, where *r**i* is the moment when the *n*-th painter stopped working on the *i*-th picture.
[ "5 1\n1\n2\n3\n4\n5\n", "4 2\n2 5\n3 1\n5 3\n10 1\n" ]
[ "1 3 6 10 15 ", "7 8 13 21 " ]
none
[ { "input": "5 1\n1\n2\n3\n4\n5", "output": "1 3 6 10 15 " }, { "input": "4 2\n2 5\n3 1\n5 3\n10 1", "output": "7 8 13 21 " }, { "input": "1 1\n66", "output": "66 " }, { "input": "2 2\n1 1\n1 1", "output": "2 3 " }, { "input": "2 2\n10 1\n10 1", "output": "11 2...
732
9,625,600
0
5,746
660
Seating On Bus
[ "implementation" ]
null
null
Consider 2*n* rows of the seats in a bus. *n* rows of the seats on the left and *n* rows of the seats on the right. Each row can be filled by two people. So the total capacity of the bus is 4*n*. Consider that *m* (*m*<=≀<=4*n*) people occupy the seats in the bus. The passengers entering the bus are numbered from 1 to *m* (in the order of their entering the bus). The pattern of the seat occupation is as below: 1-st row left window seat, 1-st row right window seat, 2-nd row left window seat, 2-nd row right window seat, ... , *n*-th row left window seat, *n*-th row right window seat. After occupying all the window seats (for *m*<=&gt;<=2*n*) the non-window seats are occupied: 1-st row left non-window seat, 1-st row right non-window seat, ... , *n*-th row left non-window seat, *n*-th row right non-window seat. All the passengers go to a single final destination. In the final destination, the passengers get off in the given order. 1-st row left non-window seat, 1-st row left window seat, 1-st row right non-window seat, 1-st row right window seat, ... , *n*-th row left non-window seat, *n*-th row left window seat, *n*-th row right non-window seat, *n*-th row right window seat. You are given the values *n* and *m*. Output *m* numbers from 1 to *m*, the order in which the passengers will get off the bus.
The only line contains two integers, *n* and *m* (1<=≀<=*n*<=≀<=100,<=1<=≀<=*m*<=≀<=4*n*) β€” the number of pairs of rows and the number of passengers.
Print *m* distinct integers from 1 to *m* β€” the order in which the passengers will get off the bus.
[ "2 7\n", "9 36\n" ]
[ "5 1 6 2 7 3 4\n", "19 1 20 2 21 3 22 4 23 5 24 6 25 7 26 8 27 9 28 10 29 11 30 12 31 13 32 14 33 15 34 16 35 17 36 18\n" ]
none
[ { "input": "2 7", "output": "5 1 6 2 7 3 4" }, { "input": "9 36", "output": "19 1 20 2 21 3 22 4 23 5 24 6 25 7 26 8 27 9 28 10 29 11 30 12 31 13 32 14 33 15 34 16 35 17 36 18" }, { "input": "1 1", "output": "1" }, { "input": "1 4", "output": "3 1 4 2" }, { "input...
109
0
0
5,753
864
Bus
[ "greedy", "implementation", "math" ]
null
null
A bus moves along the coordinate line *Ox* from the point *x*<==<=0 to the point *x*<==<=*a*. After starting from the point *x*<==<=0, it reaches the point *x*<==<=*a*, immediately turns back and then moves to the point *x*<==<=0. After returning to the point *x*<==<=0 it immediately goes back to the point *x*<==<=*a* and so on. Thus, the bus moves from *x*<==<=0 to *x*<==<=*a* and back. Moving from the point *x*<==<=0 to *x*<==<=*a* or from the point *x*<==<=*a* to *x*<==<=0 is called a bus journey. In total, the bus must make *k* journeys. The petrol tank of the bus can hold *b* liters of gasoline. To pass a single unit of distance the bus needs to spend exactly one liter of gasoline. The bus starts its first journey with a full petrol tank. There is a gas station in point *x*<==<=*f*. This point is between points *x*<==<=0 and *x*<==<=*a*. There are no other gas stations on the bus route. While passing by a gas station in either direction the bus can stop and completely refuel its tank. Thus, after stopping to refuel the tank will contain *b* liters of gasoline. What is the minimum number of times the bus needs to refuel at the point *x*<==<=*f* to make *k* journeys? The first journey starts in the point *x*<==<=0.
The first line contains four integers *a*, *b*, *f*, *k* (0<=&lt;<=*f*<=&lt;<=*a*<=≀<=106, 1<=≀<=*b*<=≀<=109, 1<=≀<=*k*<=≀<=104) β€” the endpoint of the first bus journey, the capacity of the fuel tank of the bus, the point where the gas station is located, and the required number of journeys.
Print the minimum number of times the bus needs to refuel to make *k* journeys. If it is impossible for the bus to make *k* journeys, print -1.
[ "6 9 2 4\n", "6 10 2 4\n", "6 5 4 3\n" ]
[ "4\n", "2\n", "-1\n" ]
In the first example the bus needs to refuel during each journey. In the second example the bus can pass 10 units of distance without refueling. So the bus makes the whole first journey, passes 4 units of the distance of the second journey and arrives at the point with the gas station. Then it can refuel its tank, finish the second journey and pass 2 units of distance from the third journey. In this case, it will again arrive at the point with the gas station. Further, he can refill the tank up to 10 liters to finish the third journey and ride all the way of the fourth journey. At the end of the journey the tank will be empty. In the third example the bus can not make all 3 journeys because if it refuels during the second journey, the tanks will contain only 5 liters of gasoline, but the bus needs to pass 8 units of distance until next refueling.
[ { "input": "6 9 2 4", "output": "4" }, { "input": "6 10 2 4", "output": "2" }, { "input": "6 5 4 3", "output": "-1" }, { "input": "2 2 1 1", "output": "0" }, { "input": "10 4 6 10", "output": "-1" }, { "input": "3 1 1 1", "output": "-1" }, { ...
109
0
0
5,776
262
Roma and Changing Signs
[ "greedy" ]
null
null
Roma works in a company that sells TVs. Now he has to prepare a report for the last year. Roma has got a list of the company's incomes. The list is a sequence that consists of *n* integers. The total income of the company is the sum of all integers in sequence. Roma decided to perform exactly *k* changes of signs of several numbers in the sequence. He can also change the sign of a number one, two or more times. The operation of changing a number's sign is the operation of multiplying this number by -1. Help Roma perform the changes so as to make the total income of the company (the sum of numbers in the resulting sequence) maximum. Note that Roma should perform exactly *k* changes.
The first line contains two integers *n* and *k* (1<=≀<=*n*,<=*k*<=≀<=105), showing, how many numbers are in the sequence and how many swaps are to be made. The second line contains a non-decreasing sequence, consisting of *n* integers *a**i* (|*a**i*|<=≀<=104). The numbers in the lines are separated by single spaces. Please note that the given sequence is sorted in non-decreasing order.
In the single line print the answer to the problem β€” the maximum total income that we can obtain after exactly *k* changes.
[ "3 2\n-1 -1 1\n", "3 1\n-1 -1 1\n" ]
[ "3\n", "1\n" ]
In the first sample we can get sequence [1, 1, 1], thus the total income equals 3. In the second test, the optimal strategy is to get sequence [-1, 1, 1], thus the total income equals 1.
[ { "input": "3 2\n-1 -1 1", "output": "3" }, { "input": "3 1\n-1 -1 1", "output": "1" }, { "input": "17 27\n257 320 676 1136 2068 2505 2639 4225 4951 5786 7677 7697 7851 8337 8429 8469 9343", "output": "81852" }, { "input": "69 28\n-9822 -9264 -9253 -9221 -9139 -9126 -9096 -89...
280
7,372,800
0
5,796
995
Tesla
[ "constructive algorithms", "implementation" ]
null
null
Allen dreams of one day owning a enormous fleet of electric cars, the car of the future! He knows that this will give him a big status boost. As Allen is planning out all of the different types of cars he will own and how he will arrange them, he realizes that he has a problem. Allen's future parking lot can be represented as a rectangle with $4$ rows and $n$ ($n \le 50$) columns of rectangular spaces, each of which can contain at most one car at any time. He imagines having $k$ ($k \le 2n$) cars in the grid, and all the cars are initially in the second and third rows. Each of the cars also has a different designated parking space in the first or fourth row. Allen has to put the cars into corresponding parking places. However, since Allen would never entrust his cars to anyone else, only one car can be moved at a time. He can drive a car from a space in any of the four cardinal directions to a neighboring empty space. Furthermore, Allen can only move one of his cars into a space on the first or fourth rows if it is the car's designated parking space. Allen knows he will be a very busy man, and will only have time to move cars at most $20000$ times before he realizes that moving cars is not worth his time. Help Allen determine if he should bother parking his cars or leave it to someone less important.
The first line of the input contains two space-separated integers $n$ and $k$ ($1 \le n \le 50$, $1 \le k \le 2n$), representing the number of columns and the number of cars, respectively. The next four lines will contain $n$ integers each between $0$ and $k$ inclusive, representing the initial state of the parking lot. The rows are numbered $1$ to $4$ from top to bottom and the columns are numbered $1$ to $n$ from left to right. In the first and last line, an integer $1 \le x \le k$ represents a parking spot assigned to car $x$ (you can only move this car to this place), while the integer $0$ represents a empty space (you can't move any car to this place). In the second and third line, an integer $1 \le x \le k$ represents initial position of car $x$, while the integer $0$ represents an empty space (you can move any car to this place). Each $x$ between $1$ and $k$ appears exactly once in the second and third line, and exactly once in the first and fourth line.
If there is a sequence of moves that brings all of the cars to their parking spaces, with at most $20000$ car moves, then print $m$, the number of moves, on the first line. On the following $m$ lines, print the moves (one move per line) in the format $i$ $r$ $c$, which corresponds to Allen moving car $i$ to the neighboring space at row $r$ and column $c$. If it is not possible for Allen to move all the cars to the correct spaces with at most $20000$ car moves, print a single line with the integer $-1$.
[ "4 5\n1 2 0 4\n1 2 0 4\n5 0 0 3\n0 5 0 3\n", "1 2\n1\n2\n1\n2\n", "1 2\n1\n1\n2\n2\n" ]
[ "6\n1 1 1\n2 1 2\n4 1 4\n3 4 4\n5 3 2\n5 4 2\n", "-1\n", "2\n1 1 1\n2 4 1\n" ]
In the first sample test case, all cars are in front of their spots except car $5$, which is in front of the parking spot adjacent. The example shows the shortest possible sequence of moves, but any sequence of length at most $20000$ will be accepted. In the second sample test case, there is only one column, and the cars are in the wrong order, so no cars can move and the task is impossible.
[ { "input": "4 5\n1 2 0 4\n1 2 0 4\n5 0 0 3\n0 5 0 3", "output": "6\n1 1 1\n2 1 2\n4 1 4\n3 4 4\n5 3 2\n5 4 2" }, { "input": "1 2\n1\n2\n1\n2", "output": "-1" }, { "input": "1 2\n1\n1\n2\n2", "output": "2\n1 1 1\n2 4 1" }, { "input": "2 2\n1 0\n0 2\n0 1\n0 2", "output": "7...
109
307,200
-1
5,810
863
Almost Permutation
[ "flows" ]
null
null
Recently Ivan noticed an array *a* while debugging his code. Now Ivan can't remember this array, but the bug he was trying to fix didn't go away, so Ivan thinks that the data from this array might help him to reproduce the bug. Ivan clearly remembers that there were *n* elements in the array, and each element was not less than 1 and not greater than *n*. Also he remembers *q* facts about the array. There are two types of facts that Ivan remembers: - 1 *l**i* *r**i* *v**i* β€” for each *x* such that *l**i*<=≀<=*x*<=≀<=*r**i* *a**x*<=β‰₯<=*v**i*; - 2 *l**i* *r**i* *v**i* β€” for each *x* such that *l**i*<=≀<=*x*<=≀<=*r**i* *a**x*<=≀<=*v**i*. Also Ivan thinks that this array was a permutation, but he is not so sure about it. He wants to restore some array that corresponds to the *q* facts that he remembers and is very similar to permutation. Formally, Ivan has denoted the *cost* of array as follows: , where *cnt*(*i*) is the number of occurences of *i* in the array. Help Ivan to determine minimum possible *cost* of the array that corresponds to the facts!
The first line contains two integer numbers *n* and *q* (1<=≀<=*n*<=≀<=50, 0<=≀<=*q*<=≀<=100). Then *q* lines follow, each representing a fact about the array. *i*-th line contains the numbers *t**i*, *l**i*, *r**i* and *v**i* for *i*-th fact (1<=≀<=*t**i*<=≀<=2, 1<=≀<=*l**i*<=≀<=*r**i*<=≀<=*n*, 1<=≀<=*v**i*<=≀<=*n*, *t**i* denotes the type of the fact).
If the facts are controversial and there is no array that corresponds to them, print -1. Otherwise, print minimum possible *cost* of the array.
[ "3 0\n", "3 1\n1 1 3 2\n", "3 2\n1 1 3 2\n2 1 3 2\n", "3 2\n1 1 3 2\n2 1 3 1\n" ]
[ "3\n", "5\n", "9\n", "-1\n" ]
none
[ { "input": "3 0", "output": "3" }, { "input": "3 1\n1 1 3 2", "output": "5" }, { "input": "3 2\n1 1 3 2\n2 1 3 2", "output": "9" }, { "input": "3 2\n1 1 3 2\n2 1 3 1", "output": "-1" }, { "input": "50 0", "output": "50" }, { "input": "50 1\n2 31 38 25"...
46
0
0
5,814
56
Domino Principle
[ "binary search", "data structures", "sortings" ]
E. Domino Principle
2
256
Vasya is interested in arranging dominoes. He is fed up with common dominoes and he uses the dominoes of different heights. He put *n* dominoes on the table along one axis, going from left to right. Every domino stands perpendicular to that axis so that the axis passes through the center of its base. The *i*-th domino has the coordinate *x**i* and the height *h**i*. Now Vasya wants to learn for every domino, how many dominoes will fall if he pushes it to the right. Help him do that. Consider that a domino falls if it is touched strictly above the base. In other words, the fall of the domino with the initial coordinate *x* and height *h* leads to the fall of all dominoes on the segment [*x*<=+<=1,<=*x*<=+<=*h*<=-<=1].
The first line contains integer *n* (1<=≀<=*n*<=≀<=105) which is the number of dominoes. Then follow *n* lines containing two integers *x**i* and *h**i* (<=-<=108<=≀<=*x**i*<=≀<=108,<=2<=≀<=*h**i*<=≀<=108) each, which are the coordinate and height of every domino. No two dominoes stand on one point.
Print *n* space-separated numbers *z**i* β€” the number of dominoes that will fall if Vasya pushes the *i*-th domino to the right (including the domino itself).
[ "4\n16 5\n20 5\n10 10\n18 2\n", "4\n0 10\n1 5\n9 10\n15 10\n" ]
[ "3 1 4 1 ", "4 1 2 1 " ]
none
[ { "input": "4\n16 5\n20 5\n10 10\n18 2", "output": "3 1 4 1 " }, { "input": "4\n0 10\n1 5\n9 10\n15 10", "output": "4 1 2 1 " }, { "input": "3\n6 7\n2 9\n-6 10", "output": "1 2 3 " }, { "input": "4\n-6 9\n9 5\n-7 4\n0 9", "output": "2 1 3 1 " }, { "input": "5\n-47...
2,000
21,606,400
0
5,817
143
Help Kingdom of Far Far Away 2
[ "implementation", "strings" ]
null
null
For some time the program of rounding numbers that had been developed by the Codeforces participants during one of the previous rounds, helped the citizens of Far Far Away to convert numbers into a more easily readable format. However, as time went by, the economy of the Far Far Away developed and the scale of operations grew. So the King ordered to found the Bank of Far Far Away and very soon even the rounding didn't help to quickly determine even the order of the numbers involved in operations. Besides, rounding a number to an integer wasn't very convenient as a bank needed to operate with all numbers with accuracy of up to 0.01, and not up to an integer. The King issued yet another order: to introduce financial format to represent numbers denoting amounts of money. The formal rules of storing a number in the financial format are as follows: - A number contains the integer part and the fractional part. The two parts are separated with a character "." (decimal point). - To make digits in the integer part of a number easier to read, they are split into groups of three digits, starting from the least significant ones. The groups are separated with the character "," (comma). For example, if the integer part of a number equals 12345678, then it will be stored in the financial format as 12,345,678 - In the financial format a number's fractional part should contain exactly two digits. So, if the initial number (the number that is converted into the financial format) contains less than two digits in the fractional part (or contains no digits at all), it is complemented with zeros until its length equals 2. If the fractional part contains more than two digits, the extra digits are simply discarded (they are not rounded: see sample tests). - When a number is stored in the financial format, the minus sign is not written. Instead, if the initial number had the minus sign, the result is written in round brackets. - Please keep in mind that the bank of Far Far Away operates using an exotic foreign currency β€” snakes ($), that's why right before the number in the financial format we should put the sign "$". If the number should be written in the brackets, then the snake sign should also be inside the brackets. For example, by the above given rules number 2012 will be stored in the financial format as "$2,012.00" and number -12345678.9 will be stored as "($12,345,678.90)". The merchants of Far Far Away visited you again and expressed much hope that you supply them with the program that can convert arbitrary numbers to the financial format. Can you help them?
The input contains a number that needs to be converted into financial format. The number's notation length does not exceed 100 characters, including (possible) signs "-" (minus) and "." (decimal point). The number's notation is correct, that is: - The number's notation only contains characters from the set {"0" – "9", "-", "."}. - The decimal point (if it is present) is unique and is preceded and followed by a non-zero quantity on decimal digits - A number cannot start with digit 0, except for a case when its whole integer part equals zero (in this case the integer parts is guaranteed to be a single zero: "0"). - The minus sign (if it is present) is unique and stands in the very beginning of the number's notation - If a number is identically equal to 0 (that is, if it is written as, for example, "0" or "0.000"), than it is not preceded by the minus sign. - The input data contains no spaces. - The number's notation contains at least one decimal digit.
Print the number given in the input in the financial format by the rules described in the problem statement.
[ "2012\n", "0.000\n", "-0.00987654321\n", "-12345678.9\n" ]
[ "$2,012.00", "$0.00", "($0.00)", "($12,345,678.90)" ]
Pay attention to the second and third sample tests. They show that the sign of a number in the financial format (and consequently, the presence or absence of brackets) is determined solely by the sign of the initial number. It does not depend on the sign of the number you got after translating the number to the financial format.
[ { "input": "2012", "output": "$2,012.00" }, { "input": "0.000", "output": "$0.00" }, { "input": "-0.00987654321", "output": "($0.00)" }, { "input": "-12345678.9", "output": "($12,345,678.90)" }, { "input": "0.99999999999999999999", "output": "$0.99" }, { ...
92
512,000
0
5,824
172
Phone Code
[ "*special", "brute force", "implementation" ]
null
null
Polycarpus has *n* friends in Tarasov city. Polycarpus knows phone numbers of all his friends: they are strings *s*1,<=*s*2,<=...,<=*s**n*. All these strings consist only of digits and have the same length. Once Polycarpus needed to figure out Tarasov city phone code. He assumed that the phone code of the city is the longest common prefix of all phone numbers of his friends. In other words, it is the longest string *c* which is a prefix (the beginning) of each *s**i* for all *i* (1<=≀<=*i*<=≀<=*n*). Help Polycarpus determine the length of the city phone code.
The first line of the input contains an integer *n* (2<=≀<=*n*<=≀<=3Β·104) β€” the number of Polycarpus's friends. The following *n* lines contain strings *s*1,<=*s*2,<=...,<=*s**n* β€” the phone numbers of Polycarpus's friends. It is guaranteed that all strings consist only of digits and have the same length from 1 to 20, inclusive. It is also guaranteed that all strings are different.
Print the number of digits in the city phone code.
[ "4\n00209\n00219\n00999\n00909\n", "2\n1\n2\n", "3\n77012345678999999999\n77012345678901234567\n77012345678998765432\n" ]
[ "2\n", "0\n", "12\n" ]
A prefix of string *t* is a string that is obtained by deleting zero or more digits from the end of string *t*. For example, string "00209" has 6 prefixes: "" (an empty prefix), "0", "00", "002", "0020", "00209". In the first sample the city phone code is string "00". In the second sample the city phone code is an empty string. In the third sample the city phone code is string "770123456789".
[ { "input": "4\n00209\n00219\n00999\n00909", "output": "2" }, { "input": "2\n1\n2", "output": "0" }, { "input": "3\n77012345678999999999\n77012345678901234567\n77012345678998765432", "output": "12" }, { "input": "5\n4491183345\n4491184811\n4491162340\n4491233399\n4491449214", ...
248
2,969,600
3
5,833
888
Almost Identity Permutations
[ "combinatorics", "dp", "math" ]
null
null
A permutation *p* of size *n* is an array such that every integer from 1 to *n* occurs exactly once in this array. Let's call a permutation an almost identity permutation iff there exist at least *n*<=-<=*k* indices *i* (1<=≀<=*i*<=≀<=*n*) such that *p**i*<==<=*i*. Your task is to count the number of almost identity permutations for given numbers *n* and *k*.
The first line contains two integers *n* and *k* (4<=≀<=*n*<=≀<=1000, 1<=≀<=*k*<=≀<=4).
Print the number of almost identity permutations for given *n* and *k*.
[ "4 1\n", "4 2\n", "5 3\n", "5 4\n" ]
[ "1\n", "7\n", "31\n", "76\n" ]
none
[ { "input": "4 1", "output": "1" }, { "input": "4 2", "output": "7" }, { "input": "5 3", "output": "31" }, { "input": "5 4", "output": "76" }, { "input": "200 1", "output": "1" }, { "input": "200 2", "output": "19901" }, { "input": "200 3", ...
93
2,355,200
3
5,837
48
Permutations
[ "greedy" ]
D. Permutations
1
256
A permutation is a sequence of integers from 1 to *n* of length *n* containing each number exactly once. For example, (1), (4,<=3,<=5,<=1,<=2), (3,<=2,<=1) are permutations, and (1,<=1), (4,<=3,<=1), (2,<=3,<=4) are not. There are many tasks on permutations. Today you are going to solve one of them. Let’s imagine that somebody took several permutations (perhaps, with a different number of elements), wrote them down consecutively as one array and then shuffled the resulting array. The task is to restore the initial permutations if it is possible.
The first line contains an integer *n* (1<=≀<=*n*<=≀<=105). The next line contains the mixed array of *n* integers, divided with a single space. The numbers in the array are from 1 to 105.
If this array can be split into several permutations so that every element of the array belongs to exactly one permutation, print in the first line the number of permutations. The second line should contain *n* numbers, corresponding to the elements of the given array. If the *i*-th element belongs to the first permutation, the *i*-th number should be 1, if it belongs to the second one, then its number should be 2 and so on. The order of the permutations’ numbering is free. If several solutions are possible, print any one of them. If there’s no solution, print in the first line <=-<=1.
[ "9\n1 2 3 1 2 1 4 2 5\n", "4\n4 3 2 1\n", "4\n1 2 2 3\n" ]
[ "3\n3 1 2 1 2 2 2 3 2\n", "1\n1 1 1 1 ", "-1\n" ]
In the first sample test the array is split into three permutations: (2, 1), (3, 2, 1, 4, 5), (1, 2). The first permutation is formed by the second and the fourth elements of the array, the second one β€” by the third, the fifth, the sixth, the seventh and the ninth elements, the third one β€” by the first and the eigth elements. Clearly, there are other splitting variants possible.
[ { "input": "9\n1 2 3 1 2 1 4 2 5", "output": "3\n1 1 1 2 2 3 1 3 1 " }, { "input": "4\n4 3 2 1", "output": "1\n1 1 1 1 " }, { "input": "4\n1 2 2 3", "output": "-1" }, { "input": "1\n1", "output": "1\n1 " }, { "input": "1\n2", "output": "-1" }, { "input...
93
0
0
5,838
923
Producing Snow
[ "binary search", "data structures" ]
null
null
Alice likes snow a lot! Unfortunately, this year's winter is already over, and she can't expect to have any more of it. Bob has thus bought her a giftΒ β€” a large snow maker. He plans to make some amount of snow every day. On day *i* he will make a pile of snow of volume *V**i* and put it in her garden. Each day, every pile will shrink a little due to melting. More precisely, when the temperature on a given day is *T**i*, each pile will reduce its volume by *T**i*. If this would reduce the volume of a pile to or below zero, it disappears forever. All snow piles are independent of each other. Note that the pile made on day *i* already loses part of its volume on the same day. In an extreme case, this may mean that there are no piles left at the end of a particular day. You are given the initial pile sizes and the temperature on each day. Determine the total volume of snow melted on each day.
The first line contains a single integer *N* (1<=≀<=*N*<=≀<=105)Β β€” the number of days. The second line contains *N* integers *V*1,<=*V*2,<=...,<=*V**N* (0<=≀<=*V**i*<=≀<=109), where *V**i* is the initial size of a snow pile made on the day *i*. The third line contains *N* integers *T*1,<=*T*2,<=...,<=*T**N* (0<=≀<=*T**i*<=≀<=109), where *T**i* is the temperature on the day *i*.
Output a single line with *N* integers, where the *i*-th integer represents the total volume of snow melted on day *i*.
[ "3\n10 10 5\n5 7 2\n", "5\n30 25 20 15 10\n9 10 12 4 13\n" ]
[ "5 12 4\n", "9 20 35 11 25\n" ]
In the first sample, Bob first makes a snow pile of volume 10, which melts to the size of 5 on the same day. On the second day, he makes another pile of size 10. Since it is a bit warmer than the day before, the first pile disappears completely while the second pile shrinks to 3. At the end of the second day, he has only a single pile of size 3. On the third day he makes a smaller pile than usual, but as the temperature dropped too, both piles survive till the end of the day.
[ { "input": "3\n10 10 5\n5 7 2", "output": "5 12 4" }, { "input": "5\n30 25 20 15 10\n9 10 12 4 13", "output": "9 20 35 11 25" }, { "input": "4\n0 0 0 0\n1 2 3 4", "output": "0 0 0 0" }, { "input": "10\n11 39 16 34 25 3 12 11 31 16\n10 0 4 9 8 9 7 8 9 2", "output": "10 0 9...
686
37,068,800
3
5,839
1,003
Intense Heat
[ "brute force", "implementation", "math" ]
null
null
The heat during the last few days has been really intense. Scientists from all over the Berland study how the temperatures and weather change, and they claim that this summer is abnormally hot. But any scientific claim sounds a lot more reasonable if there are some numbers involved, so they have decided to actually calculate some value which would represent how high the temperatures are. Mathematicians of Berland State University came up with a special heat intensity value. This value is calculated as follows: Suppose we want to analyze the segment of $n$ consecutive days. We have measured the temperatures during these $n$ days; the temperature during $i$-th day equals $a_i$. We denote the average temperature of a segment of some consecutive days as the arithmetic mean of the temperature measures during this segment of days. So, if we want to analyze the average temperature from day $x$ to day $y$, we calculate it as $\frac{\sum \limits_{i = x}^{y} a_i}{y - x + 1}$ (note that division is performed without any rounding). The heat intensity value is the maximum of average temperatures over all segments of not less than $k$ consecutive days. For example, if analyzing the measures $[3, 4, 1, 2]$ and $k = 3$, we are interested in segments $[3, 4, 1]$, $[4, 1, 2]$ and $[3, 4, 1, 2]$ (we want to find the maximum value of average temperature over these segments). You have been hired by Berland State University to write a program that would compute the heat intensity value of a given period of days. Are you up to this task?
The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 5000$) β€” the number of days in the given period, and the minimum number of days in a segment we consider when calculating heat intensity value, respectively. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 5000$) β€” the temperature measures during given $n$ days.
Print one real number β€” the heat intensity value, i. e., the maximum of average temperatures over all segments of not less than $k$ consecutive days. Your answer will be considered correct if the following condition holds: $|res - res_0| &lt; 10^{-6}$, where $res$ is your answer, and $res_0$ is the answer given by the jury's solution.
[ "4 3\n3 4 1 2\n" ]
[ "2.666666666666667\n" ]
none
[ { "input": "4 3\n3 4 1 2", "output": "2.666666666666667" }, { "input": "5 1\n3 10 9 10 6", "output": "10.000000000000000" }, { "input": "5 2\n7 3 3 1 8", "output": "5.000000000000000" }, { "input": "5 3\n1 7 6 9 1", "output": "7.333333333333333" }, { "input": "5 4...
4,000
0
0
5,868
850
Five Dimensional Points
[ "brute force", "geometry", "math" ]
null
null
You are given set of *n* points in 5-dimensional space. The points are labeled from 1 to *n*. No two points coincide. We will call point *a* bad if there are different points *b* and *c*, not equal to *a*, from the given set such that angle between vectors and is acute (i.e. strictly less than ). Otherwise, the point is called good. The angle between vectors and in 5-dimensional space is defined as , where is the scalar product and is length of . Given the list of points, print the indices of the good points in ascending order.
The first line of input contains a single integer *n* (1<=≀<=*n*<=≀<=103)Β β€” the number of points. The next *n* lines of input contain five integers *a**i*,<=*b**i*,<=*c**i*,<=*d**i*,<=*e**i* (|*a**i*|,<=|*b**i*|,<=|*c**i*|,<=|*d**i*|,<=|*e**i*|<=≀<=103) Β β€” the coordinates of the i-th point. All points are distinct.
First, print a single integer *k*Β β€” the number of good points. Then, print *k* integers, each on their own lineΒ β€” the indices of the good points in ascending order.
[ "6\n0 0 0 0 0\n1 0 0 0 0\n0 1 0 0 0\n0 0 1 0 0\n0 0 0 1 0\n0 0 0 0 1\n", "3\n0 0 1 2 0\n0 0 9 2 0\n0 0 5 9 0\n" ]
[ "1\n1\n", "0\n" ]
In the first sample, the first point forms exactly a <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/362ac8c7a7496dffc06cd0c843287cd822be63c3.png" style="max-width: 100.0%;max-height: 100.0%;"/> angle with all other pairs of points, so it is good. In the second sample, along the cd plane, we can see the points look as follows: <img class="tex-graphics" src="https://espresso.codeforces.com/982219f7eb73ea120de10dd91baa59317fe7af64.png" style="max-width: 100.0%;max-height: 100.0%;"/> We can see that all angles here are acute, so no points are good.
[ { "input": "6\n0 0 0 0 0\n1 0 0 0 0\n0 1 0 0 0\n0 0 1 0 0\n0 0 0 1 0\n0 0 0 0 1", "output": "1\n1" }, { "input": "3\n0 0 1 2 0\n0 0 9 2 0\n0 0 5 9 0", "output": "0" }, { "input": "1\n0 0 0 0 0", "output": "1\n1" }, { "input": "2\n0 1 2 3 4\n5 6 7 8 9", "output": "2\n1\n2"...
46
0
0
5,871
863
Yet Another Array Queries Problem
[ "data structures", "implementation" ]
null
null
You are given an array *a* of size *n*, and *q* queries to it. There are queries of two types: - 1 *l**i* *r**i* β€” perform a cyclic shift of the segment [*l**i*,<=*r**i*] to the right. That is, for every *x* such that *l**i*<=≀<=*x*<=&lt;<=*r**i* new value of *a**x*<=+<=1 becomes equal to old value of *a**x*, and new value of *a**l**i* becomes equal to old value of *a**r**i*; - 2 *l**i* *r**i* β€” reverse the segment [*l**i*,<=*r**i*]. There are *m* important indices in the array *b*1, *b*2, ..., *b**m*. For each *i* such that 1<=≀<=*i*<=≀<=*m* you have to output the number that will have index *b**i* in the array after all queries are performed.
The first line contains three integer numbers *n*, *q* and *m* (1<=≀<=*n*,<=*q*<=≀<=2Β·105, 1<=≀<=*m*<=≀<=100). The second line contains *n* integer numbers *a*1, *a*2, ..., *a**n* (1<=≀<=*a**i*<=≀<=109). Then *q* lines follow. *i*-th of them contains three integer numbers *t**i*, *l**i*, *r**i*, where *t**i* is the type of *i*-th query, and [*l**i*,<=*r**i*] is the segment where this query is performed (1<=≀<=*t**i*<=≀<=2, 1<=≀<=*l**i*<=≀<=*r**i*<=≀<=*n*). The last line contains *m* integer numbers *b*1, *b*2, ..., *b**m* (1<=≀<=*b**i*<=≀<=*n*) β€” important indices of the array.
Print *m* numbers, *i*-th of which is equal to the number at index *b**i* after all queries are done.
[ "6 3 5\n1 2 3 4 5 6\n2 1 3\n2 3 6\n1 1 6\n2 2 1 5 3\n" ]
[ "3 3 1 5 2 \n" ]
none
[ { "input": "6 3 5\n1 2 3 4 5 6\n2 1 3\n2 3 6\n1 1 6\n2 2 1 5 3", "output": "3 3 1 5 2 " }, { "input": "5 2 5\n64 3 4 665 2\n1 1 3\n2 1 5\n1 2 3 4 5", "output": "2 665 3 64 4 " }, { "input": "1 1 1\n474812122\n2 1 1\n1", "output": "474812122 " } ]
156
102,400
0
5,882
151
Phone Numbers
[ "implementation", "strings" ]
null
null
Winters are just damn freezing cold in Nvodsk! That's why a group of *n* friends prefers to take a taxi, order a pizza and call girls. The phone numbers in the city consist of three pairs of digits (for example, 12-34-56). Each friend has a phonebook of size *s**i* (that's the number of phone numbers). We know that taxi numbers consist of six identical digits (for example, 22-22-22), the numbers of pizza deliveries should necessarily be decreasing sequences of six different digits (for example, 98-73-21), all other numbers are the girls' numbers. You are given your friends' phone books. Calculate which friend is best to go to when you are interested in each of those things (who has maximal number of phone numbers of each type). If the phone book of one person contains some number two times, you should count it twice. That is, each number should be taken into consideration the number of times it occurs in the phone book.
The first line contains an integer *n* (1<=≀<=*n*<=≀<=100) β€” the number of friends. Then follow *n* data blocks that describe each friend's phone books. Each block is presented in the following form: first goes the line that contains integer *s**i* and string *name**i* (0<=≀<=*s**i*<=≀<=100) β€” the number of phone numbers in the phone book of the *i*-th friend and the name of the *i*-th friend. The name is a non-empty sequence of uppercase and lowercase Latin letters, containing no more than 20 characters. Next *s**i* lines contain numbers as "XX-XX-XX", where X is arbitrary digits from 0 to 9.
In the first line print the phrase "If you want to call a taxi, you should call: ". Then print names of all friends whose phone books contain maximal number of taxi phone numbers. In the second line print the phrase "If you want to order a pizza, you should call: ". Then print names of all friends who have maximal number of pizza phone numbers. In the third line print the phrase "If you want to go to a cafe with a wonderful girl, you should call: ". Then print names of all friends who have maximal number of girls' phone numbers. Print the names in the order in which they are given in the input data. Separate two consecutive names with a comma and a space. Each line should end with exactly one point. For clarifications concerning the output form, see sample tests. It is necessary that you follow the output form strictly. Extra spaces are not allowed.
[ "4\n2 Fedorov\n22-22-22\n98-76-54\n3 Melnikov\n75-19-09\n23-45-67\n99-99-98\n7 Rogulenko\n22-22-22\n11-11-11\n33-33-33\n44-44-44\n55-55-55\n66-66-66\n95-43-21\n3 Kaluzhin\n11-11-11\n99-99-99\n98-65-32\n", "3\n5 Gleb\n66-66-66\n55-55-55\n01-01-01\n65-43-21\n12-34-56\n3 Serega\n55-55-55\n87-65-43\n65-55-21\n5 Melni...
[ "If you want to call a taxi, you should call: Rogulenko.\nIf you want to order a pizza, you should call: Fedorov, Rogulenko, Kaluzhin.\nIf you want to go to a cafe with a wonderful girl, you should call: Melnikov.\n", "If you want to call a taxi, you should call: Gleb.\nIf you want to order a pizza, you should ca...
In the first sample you are given four friends. Fedorov's phone book contains one taxi number and one pizza delivery number, Melnikov's phone book only has 3 numbers of girls, Rogulenko's one has 6 taxi numbers and one pizza delivery number, Kaluzhin's one contains 2 taxi numbers and one pizza delivery number. Thus, if you need to order a taxi, you should obviously call Rogulenko, if you need to order a pizza you should call anybody of the following: Rogulenko, Fedorov, Kaluzhin (each of them has one number). Melnikov has maximal number of phone numbers of girls.
[ { "input": "4\n2 Fedorov\n22-22-22\n98-76-54\n3 Melnikov\n75-19-09\n23-45-67\n99-99-98\n7 Rogulenko\n22-22-22\n11-11-11\n33-33-33\n44-44-44\n55-55-55\n66-66-66\n95-43-21\n3 Kaluzhin\n11-11-11\n99-99-99\n98-65-32", "output": "If you want to call a taxi, you should call: Rogulenko.\nIf you want to order a piz...
46
0
0
5,883
567
Berland National Library
[ "implementation" ]
null
null
Berland National Library has recently been built in the capital of Berland. In addition, in the library you can take any of the collected works of Berland leaders, the library has a reading room. Today was the pilot launch of an automated reading room visitors' accounting system! The scanner of the system is installed at the entrance to the reading room. It records the events of the form "reader entered room", "reader left room". Every reader is assigned a registration number during the registration procedure at the library β€” it's a unique integer from 1 to 106. Thus, the system logs events of two forms: - "+ *r**i*" β€” the reader with registration number *r**i* entered the room; - "- *r**i*" β€” the reader with registration number *r**i* left the room. The first launch of the system was a success, it functioned for some period of time, and, at the time of its launch and at the time of its shutdown, the reading room may already have visitors. Significant funds of the budget of Berland have been spent on the design and installation of the system. Therefore, some of the citizens of the capital now demand to explain the need for this system and the benefits that its implementation will bring. Now, the developers of the system need to urgently come up with reasons for its existence. Help the system developers to find the minimum possible capacity of the reading room (in visitors) using the log of the system available to you.
The first line contains a positive integer *n* (1<=≀<=*n*<=≀<=100) β€” the number of records in the system log. Next follow *n* events from the system journal in the order in which the were made. Each event was written on a single line and looks as "+ *r**i*" or "- *r**i*", where *r**i* is an integer from 1 to 106, the registration number of the visitor (that is, distinct visitors always have distinct registration numbers). It is guaranteed that the log is not contradictory, that is, for every visitor the types of any of his two consecutive events are distinct. Before starting the system, and after stopping the room may possibly contain visitors.
Print a single integer β€” the minimum possible capacity of the reading room.
[ "6\n+ 12001\n- 12001\n- 1\n- 1200\n+ 1\n+ 7\n", "2\n- 1\n- 2\n", "2\n+ 1\n- 1\n" ]
[ "3", "2", "1" ]
In the first sample test, the system log will ensure that at some point in the reading room were visitors with registration numbers 1, 1200 and 12001. More people were not in the room at the same time based on the log. Therefore, the answer to the test is 3.
[ { "input": "6\n+ 12001\n- 12001\n- 1\n- 1200\n+ 1\n+ 7", "output": "3" }, { "input": "2\n- 1\n- 2", "output": "2" }, { "input": "2\n+ 1\n- 1", "output": "1" }, { "input": "5\n+ 1\n- 1\n+ 2\n+ 3\n- 4", "output": "3" }, { "input": "3\n- 1\n- 2\n- 3", "output": "...
78
0
0
5,903
552
Vanya and Scales
[ "brute force", "dp", "greedy", "math", "meet-in-the-middle", "number theory" ]
null
null
Vanya has a scales for weighing loads and weights of masses *w*0,<=*w*1,<=*w*2,<=...,<=*w*100 grams where *w* is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass *m* using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass *m* and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
The first line contains two integers *w*,<=*m* (2<=≀<=*w*<=≀<=109, 1<=≀<=*m*<=≀<=109) β€” the number defining the masses of the weights and the mass of the item.
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
[ "3 7\n", "100 99\n", "100 50\n" ]
[ "YES\n", "YES\n", "NO\n" ]
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1. Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100. Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input.
[ { "input": "3 7", "output": "YES" }, { "input": "100 99", "output": "YES" }, { "input": "100 50", "output": "NO" }, { "input": "1000000000 1", "output": "YES" }, { "input": "100 10002", "output": "NO" }, { "input": "4 7", "output": "NO" }, { ...
108
0
0
5,924
0
none
[ "none" ]
null
null
From beginning till end, this message has been waiting to be conveyed. For a given unordered multiset of *n* lowercase English letters ("multi" means that a letter may appear more than once), we treat all letters as strings of length 1, and repeat the following operation *n*<=-<=1 times: - Remove any two elements *s* and *t* from the set, and add their concatenation *s*<=+<=*t* to the set. The cost of such operation is defined to be , where *f*(*s*,<=*c*) denotes the number of times character *c* appears in string *s*. Given a non-negative integer *k*, construct any valid non-empty set of no more than 100<=000 letters, such that the minimum accumulative cost of the whole process is exactly *k*. It can be shown that a solution always exists.
The first and only line of input contains a non-negative integer *k* (0<=≀<=*k*<=≀<=100<=000) β€” the required minimum cost.
Output a non-empty string of no more than 100<=000 lowercase English letters β€” any multiset satisfying the requirements, concatenated to be a string. Note that the printed string doesn't need to be the final concatenated string. It only needs to represent an unordered multiset of letters.
[ "12\n", "3\n" ]
[ "abababab\n", "codeforces\n" ]
For the multiset {'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b'}, one of the ways to complete the process is as follows: - {"ab", "a", "b", "a", "b", "a", "b"}, with a cost of 0; - {"aba", "b", "a", "b", "a", "b"}, with a cost of 1; - {"abab", "a", "b", "a", "b"}, with a cost of 1; - {"abab", "ab", "a", "b"}, with a cost of 0; - {"abab", "aba", "b"}, with a cost of 1; - {"abab", "abab"}, with a cost of 1; - {"abababab"}, with a cost of 8. The total cost is 12, and it can be proved to be the minimum cost of the process.
[ { "input": "12", "output": "abababab" }, { "input": "3", "output": "codeforces" }, { "input": "0", "output": "o" }, { "input": "2", "output": "aabb" }, { "input": "5", "output": "aaabbcc" }, { "input": "10", "output": "aaaaa" }, { "input": ...
46
0
0
5,935
677
Vanya and Balloons
[ "binary search", "brute force", "dp", "implementation" ]
null
null
Vanya plays a game of balloons on the field of size *n*<=Γ—<=*n*, where each cell contains a balloon with one of the values 0, 1, 2 or 3. The goal is to destroy a cross, such that the product of all values of balloons in the cross is maximum possible. There are two types of crosses: normal and rotated. For example: or Formally, the cross is given by three integers *r*, *c* and *d*, such that *d*<=≀<=*r*,<=*c*<=≀<=*n*<=-<=*d*<=+<=1. The normal cross consists of balloons located in cells (*x*,<=*y*) (where *x* stay for the number of the row and *y* for the number of the column), such that |*x*<=-<=*r*|Β·|*y*<=-<=*c*|<==<=0 and |*x*<=-<=*r*|<=+<=|*y*<=-<=*c*|<=&lt;<=*d*. Rotated cross consists of balloons located in cells (*x*,<=*y*), such that |*x*<=-<=*r*|<==<=|*y*<=-<=*c*| and |*x*<=-<=*r*|<=&lt;<=*d*. Vanya wants to know the maximum possible product of the values of balls forming one cross. As this value can be large, output it modulo 109<=+<=7.
The first line of the input contains a single integer *n* (1<=≀<=*n*<=≀<=1000)Β β€” the number of rows and columns in the table with balloons. The each of the following *n* lines contains *n* characters '0', '1', '2' or '3'Β β€” the description of the values in balloons.
Print the maximum possible product modulo 109<=+<=7. Note, that you are not asked to maximize the remainder modulo 109<=+<=7, but to find the maximum value and print it this modulo.
[ "4\n1233\n0213\n2020\n0303\n", "5\n00300\n00300\n33333\n00300\n00300\n", "5\n00003\n02030\n00300\n03020\n30000\n", "5\n21312\n10003\n10002\n10003\n23231\n", "5\n12131\n12111\n12112\n21311\n21212\n" ]
[ "108\n", "19683\n", "108\n", "3\n", "24\n" ]
In the first sample, the maximum product is achieved for a rotated cross with a center in the cell (3, 3) and radius 1: 2Β·2Β·3Β·3Β·3 = 108.
[]
30
0
0
5,947
596
Wilbur and Swimming Pool
[ "geometry", "implementation" ]
null
null
After making bad dives into swimming pools, Wilbur wants to build a swimming pool in the shape of a rectangle in his backyard. He has set up coordinate axes, and he wants the sides of the rectangle to be parallel to them. Of course, the area of the rectangle must be positive. Wilbur had all four vertices of the planned pool written on a paper, until his friend came along and erased some of the vertices. Now Wilbur is wondering, if the remaining *n* vertices of the initial rectangle give enough information to restore the area of the planned swimming pool.
The first line of the input contains a single integer *n* (1<=≀<=*n*<=≀<=4)Β β€” the number of vertices that were not erased by Wilbur's friend. Each of the following *n* lines contains two integers *x**i* and *y**i* (<=-<=1000<=≀<=*x**i*,<=*y**i*<=≀<=1000)Β β€”the coordinates of the *i*-th vertex that remains. Vertices are given in an arbitrary order. It's guaranteed that these points are distinct vertices of some rectangle, that has positive area and which sides are parallel to the coordinate axes.
Print the area of the initial rectangle if it could be uniquely determined by the points remaining. Otherwise, print <=-<=1.
[ "2\n0 0\n1 1\n", "1\n1 1\n" ]
[ "1\n", "-1\n" ]
In the first sample, two opposite corners of the initial rectangle are given, and that gives enough information to say that the rectangle is actually a unit square. In the second sample there is only one vertex left and this is definitely not enough to uniquely define the area.
[ { "input": "2\n0 0\n1 1", "output": "1" }, { "input": "1\n1 1", "output": "-1" }, { "input": "1\n-188 17", "output": "-1" }, { "input": "1\n71 -740", "output": "-1" }, { "input": "4\n-56 -858\n-56 -174\n778 -858\n778 -174", "output": "570456" }, { "inp...
62
0
3
5,976
386
Fly, freebies, fly!
[ "binary search", "brute force", "implementation" ]
null
null
Everyone loves a freebie. Especially students. It is well-known that if in the night before exam a student opens window, opens the student's record-book and shouts loudly three times "Fly, freebie, fly!" β€” then flown freebie helps him to pass the upcoming exam. In the night before the exam on mathematical analysis *n* students living in dormitory shouted treasured words. The *i*-th student made a sacrament at the time *t**i*, where *t**i* is the number of seconds elapsed since the beginning of the night. It is known that the freebie is a capricious and willful lady. That night the freebie was near dormitory only for *T* seconds. Therefore, if for two students their sacrament times differ for more than *T*, then the freebie didn't visit at least one of them. Since all students are optimists, they really want to know what is the maximal number of students visited by the freebie can be.
The first line of the input contains integer *n* (1<=≀<=*n*<=≀<=100), where *n* β€” the number of students shouted "Fly, freebie, fly!" The second line contains *n* positive integers *t**i* (1<=≀<=*t**i*<=≀<=1000). The last line contains integer *T* (1<=≀<=*T*<=≀<=1000) β€” the time interval during which the freebie was near the dormitory.
Print a single integer β€” the largest number of people who will pass exam tomorrow because of the freebie visit.
[ "6\n4 1 7 8 3 8\n1\n" ]
[ "3\n" ]
none
[ { "input": "6\n4 1 7 8 3 8\n1", "output": "3" }, { "input": "4\n4 2 1 5\n2", "output": "2" }, { "input": "10\n4 7 1 3 8 5 2 1 8 4\n3", "output": "6" }, { "input": "8\n39 49 37 28 40 17 50 2\n10", "output": "3" }, { "input": "2\n1 1\n1", "output": "2" }, { ...
31
0
0
5,988
257
Playing Cubes
[ "games", "greedy", "implementation" ]
null
null
Petya and Vasya decided to play a little. They found *n* red cubes and *m* blue cubes. The game goes like that: the players take turns to choose a cube of some color (red or blue) and put it in a line from left to right (overall the line will have *n*<=+<=*m* cubes). Petya moves first. Petya's task is to get as many pairs of neighbouring cubes of the same color as possible. Vasya's task is to get as many pairs of neighbouring cubes of different colors as possible. The number of Petya's points in the game is the number of pairs of neighboring cubes of the same color in the line, the number of Vasya's points in the game is the number of neighbouring cubes of the different color in the line. Your task is to calculate the score at the end of the game (Petya's and Vasya's points, correspondingly), if both boys are playing optimally well. To "play optimally well" first of all means to maximize the number of one's points, and second β€” to minimize the number of the opponent's points.
The only line contains two space-separated integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=105) β€” the number of red and blue cubes, correspondingly.
On a single line print two space-separated integers β€” the number of Petya's and Vasya's points correspondingly provided that both players play optimally well.
[ "3 1\n", "2 4\n" ]
[ "2 1\n", "3 2\n" ]
In the first test sample the optimal strategy for Petya is to put the blue cube in the line. After that there will be only red cubes left, so by the end of the game the line of cubes from left to right will look as [blue, red, red, red]. So, Petya gets 2 points and Vasya gets 1 point. If Petya would choose the red cube during his first move, then, provided that both boys play optimally well, Petya would get 1 point and Vasya would get 2 points.
[ { "input": "3 1", "output": "2 1" }, { "input": "2 4", "output": "3 2" }, { "input": "1 1", "output": "0 1" }, { "input": "2 1", "output": "1 1" }, { "input": "4 4", "output": "3 4" }, { "input": "10 7", "output": "9 7" }, { "input": "5 13"...
156
0
-1
5,994
60
Where Are My Flakes?
[ "implementation", "two pointers" ]
A. Where Are My Flakes?
2
256
One morning the Cereal Guy found out that all his cereal flakes were gone. He found a note instead of them. It turned out that his smart roommate hid the flakes in one of *n* boxes. The boxes stand in one row, they are numbered from 1 to *n* from the left to the right. The roommate left hints like "Hidden to the left of the *i*-th box" ("To the left of *i*"), "Hidden to the right of the *i*-th box" ("To the right of *i*"). Such hints mean that there are no flakes in the *i*-th box as well. The Cereal Guy wants to know the minimal number of boxes he necessarily needs to check to find the flakes considering all the hints. Or he wants to find out that the hints are contradictory and the roommate lied to him, that is, no box has the flakes.
The first line contains two integers *n* and *m* (1<=≀<=*n*<=≀<=1000,<=0<=≀<=*m*<=≀<=1000) which represent the number of boxes and the number of hints correspondingly. Next *m* lines contain hints like "To the left of *i*" and "To the right of *i*", where *i* is integer (1<=≀<=*i*<=≀<=*n*). The hints may coincide.
The answer should contain exactly one integer β€” the number of boxes that should necessarily be checked or "-1" if the hints are contradictory.
[ "2 1\nTo the left of 2\n", "3 2\nTo the right of 1\nTo the right of 2\n", "3 1\nTo the left of 3\n", "3 2\nTo the left of 2\nTo the right of 1\n" ]
[ "1\n", "1\n", "2\n", "-1\n" ]
none
[ { "input": "2 1\nTo the left of 2", "output": "1" }, { "input": "3 2\nTo the right of 1\nTo the right of 2", "output": "1" }, { "input": "3 1\nTo the left of 3", "output": "2" }, { "input": "3 2\nTo the left of 2\nTo the right of 1", "output": "-1" }, { "input": "...
374
1,433,600
3.90383
5,995
221
Little Elephant and Numbers
[ "implementation" ]
null
null
The Little Elephant loves numbers. He has a positive integer *x*. The Little Elephant wants to find the number of positive integers *d*, such that *d* is the divisor of *x*, and *x* and *d* have at least one common (the same) digit in their decimal representations. Help the Little Elephant to find the described number.
A single line contains a single integer *x* (1<=≀<=*x*<=≀<=109).
In a single line print an integer β€” the answer to the problem.
[ "1\n", "10\n" ]
[ "1\n", "2\n" ]
none
[ { "input": "1", "output": "1" }, { "input": "10", "output": "2" }, { "input": "47", "output": "1" }, { "input": "100", "output": "5" }, { "input": "128", "output": "6" }, { "input": "2", "output": "1" }, { "input": "17", "output": "2" ...
124
0
0
6,003
177
Rectangular Game
[ "number theory" ]
null
null
The Smart Beaver from ABBYY decided to have a day off. But doing nothing the whole day turned out to be too boring, and he decided to play a game with pebbles. Initially, the Beaver has *n* pebbles. He arranges them in *a* equal rows, each row has *b* pebbles (*a*<=&gt;<=1). Note that the Beaver must use all the pebbles he has, i. e. *n*<==<=*a*Β·*b*. Once the Smart Beaver has arranged the pebbles, he takes back any of the resulting rows (that is, *b* pebbles) and discards all other pebbles. Then he arranges all his pebbles again (possibly choosing other values of *a* and *b*) and takes back one row, and so on. The game continues until at some point the Beaver ends up with exactly one pebble. The game process can be represented as a finite sequence of integers *c*1,<=...,<=*c**k*, where: - *c*1<==<=*n* - *c**i*<=+<=1 is the number of pebbles that the Beaver ends up with after the *i*-th move, that is, the number of pebbles in a row after some arrangement of *c**i* pebbles (1<=≀<=*i*<=&lt;<=*k*). Note that *c**i*<=&gt;<=*c**i*<=+<=1. - *c**k*<==<=1 The result of the game is the sum of numbers *c**i*. You are given *n*. Find the maximum possible result of the game.
The single line of the input contains a single integer *n* β€” the initial number of pebbles the Smart Beaver has. The input limitations for getting 30 points are: - 2<=≀<=*n*<=≀<=50 The input limitations for getting 100 points are: - 2<=≀<=*n*<=≀<=109
Print a single number β€” the maximum possible result of the game.
[ "10\n", "8\n" ]
[ "16\n", "15\n" ]
Consider the first example (*c*<sub class="lower-index">1</sub> = 10). The possible options for the game development are: - Arrange the pebbles in 10 rows, one pebble per row. Then *c*<sub class="lower-index">2</sub> = 1, and the game ends after the first move with the result of 11. - Arrange the pebbles in 5 rows, two pebbles per row. Then *c*<sub class="lower-index">2</sub> = 2, and the game continues. During the second move we have two pebbles which can be arranged in a unique way (remember that you are not allowed to put all the pebbles in the same row!) β€” 2 rows, one pebble per row. *c*<sub class="lower-index">3</sub> = 1, and the game ends with the result of 13. - Finally, arrange the pebbles in two rows, five pebbles per row. The same logic leads us to *c*<sub class="lower-index">2</sub> = 5, *c*<sub class="lower-index">3</sub> = 1, and the game ends with the result of 16 β€” the maximum possible result.
[ { "input": "10", "output": "16" }, { "input": "8", "output": "15" }, { "input": "4", "output": "7" }, { "input": "36", "output": "67" }, { "input": "32", "output": "63" }, { "input": "46", "output": "70" }, { "input": "6", "output": "10...
186
20,172,800
3
6,005
79
Colorful Field
[ "implementation", "sortings" ]
B. Colorful Field
2
256
Fox Ciel saw a large field while she was on a bus. The field was a *n*<=Γ—<=*m* rectangle divided into 1<=Γ—<=1 cells. Some cells were wasteland, and other each cell contained crop plants: either carrots or kiwis or grapes. After seeing the field carefully, Ciel found that the crop plants of each cell were planted in following procedure: - Assume that the rows are numbered 1 to *n* from top to bottom and the columns are numbered 1 to *m* from left to right, and a cell in row *i* and column *j* is represented as (*i*,<=*j*). - First, each field is either cultivated or waste. Crop plants will be planted in the cultivated cells in the order of (1,<=1)<=β†’<=...<=β†’<=(1,<=*m*)<=β†’<=(2,<=1)<=β†’<=...<=β†’<=(2,<=*m*)<=β†’<=...<=β†’<=(*n*,<=1)<=β†’<=...<=β†’<=(*n*,<=*m*). Waste cells will be ignored. - Crop plants (either carrots or kiwis or grapes) will be planted in each cell one after another cyclically. Carrots will be planted in the first cell, then kiwis in the second one, grapes in the third one, carrots in the forth one, kiwis in the fifth one, and so on. The following figure will show you the example of this procedure. Here, a white square represents a cultivated cell, and a black square represents a waste cell. Now she is wondering how to determine the crop plants in some certain cells.
In the first line there are four positive integers *n*,<=*m*,<=*k*,<=*t* (1<=≀<=*n*<=≀<=4Β·104,<=1<=≀<=*m*<=≀<=4Β·104,<=1<=≀<=*k*<=≀<=103,<=1<=≀<=*t*<=≀<=103), each of which represents the height of the field, the width of the field, the number of waste cells and the number of queries that ask the kind of crop plants in a certain cell. Following each *k* lines contains two integers *a*,<=*b* (1<=≀<=*a*<=≀<=*n*,<=1<=≀<=*b*<=≀<=*m*), which denotes a cell (*a*,<=*b*) is waste. It is guaranteed that the same cell will not appear twice in this section. Following each *t* lines contains two integers *i*,<=*j* (1<=≀<=*i*<=≀<=*n*,<=1<=≀<=*j*<=≀<=*m*), which is a query that asks you the kind of crop plants of a cell (*i*,<=*j*).
For each query, if the cell is waste, print Waste. Otherwise, print the name of crop plants in the cell: either Carrots or Kiwis or Grapes.
[ "4 5 5 6\n4 3\n1 3\n3 3\n2 5\n3 2\n1 3\n1 4\n2 3\n2 4\n1 1\n1 1\n" ]
[ "Waste\nGrapes\nCarrots\nKiwis\nCarrots\nCarrots\n" ]
The sample corresponds to the figure in the statement.
[ { "input": "4 5 5 6\n4 3\n1 3\n3 3\n2 5\n3 2\n1 3\n1 4\n2 3\n2 4\n1 1\n1 1", "output": "Waste\nGrapes\nCarrots\nKiwis\nCarrots\nCarrots" }, { "input": "2 3 2 2\n1 1\n2 2\n2 1\n2 2", "output": "Grapes\nWaste" }, { "input": "31 31 31 4\n4 9\n16 27\n11 29\n8 28\n11 2\n10 7\n22 6\n1 25\n14 8...
186
0
0
6,007
0
none
[ "none" ]
null
null
It is nighttime and Joe the Elusive got into the country's main bank's safe. The safe has *n* cells positioned in a row, each of them contains some amount of diamonds. Let's make the problem more comfortable to work with and mark the cells with positive numbers from 1 to *n* from the left to the right. Unfortunately, Joe didn't switch the last security system off. On the plus side, he knows the way it works. Every minute the security system calculates the total amount of diamonds for each two adjacent cells (for the cells between whose numbers difference equals 1). As a result of this check we get an *n*<=-<=1 sums. If at least one of the sums differs from the corresponding sum received during the previous check, then the security system is triggered. Joe can move the diamonds from one cell to another between the security system's checks. He manages to move them no more than *m* times between two checks. One of the three following operations is regarded as moving a diamond: moving a diamond from any cell to any other one, moving a diamond from any cell to Joe's pocket, moving a diamond from Joe's pocket to any cell. Initially Joe's pocket is empty, and it can carry an unlimited amount of diamonds. It is considered that before all Joe's actions the system performs at least one check. In the morning the bank employees will come, which is why Joe has to leave the bank before that moment. Joe has only *k* minutes left before morning, and on each of these *k* minutes he can perform no more than *m* operations. All that remains in Joe's pocket, is considered his loot. Calculate the largest amount of diamonds Joe can carry with him. Don't forget that the security system shouldn't be triggered (even after Joe leaves the bank) and Joe should leave before morning.
The first line contains integers *n*, *m* and *k* (1<=≀<=*n*<=≀<=104, 1<=≀<=*m*,<=*k*<=≀<=109). The next line contains *n* numbers. The *i*-th number is equal to the amount of diamonds in the *i*-th cell β€” it is an integer from 0 to 105.
Print a single number β€” the maximum number of diamonds Joe can steal.
[ "2 3 1\n2 3\n", "3 2 2\n4 1 3\n" ]
[ "0", "2" ]
In the second sample Joe can act like this: The diamonds' initial positions are 4 1 3. During the first period of time Joe moves a diamond from the 1-th cell to the 2-th one and a diamond from the 3-th cell to his pocket. By the end of the first period the diamonds' positions are 3 2 2. The check finds no difference and the security system doesn't go off. During the second period Joe moves a diamond from the 3-rd cell to the 2-nd one and puts a diamond from the 1-st cell to his pocket. By the end of the second period the diamonds' positions are 2 3 1. The check finds no difference again and the security system doesn't go off. Now Joe leaves with 2 diamonds in his pocket.
[ { "input": "2 3 1\n2 3", "output": "0" }, { "input": "3 2 2\n4 1 3", "output": "2" }, { "input": "5 10 10\n7 0 7 0 7", "output": "7" }, { "input": "6 10 4\n1 2 3 4 5 6", "output": "0" }, { "input": "7 5 2\n1 2 3 4 5 6 7", "output": "1" }, { "input": "1...
122
0
0
6,008
816
Karen and Morning
[ "brute force", "implementation" ]
null
null
Karen is getting ready for a new school day! It is currently hh:mm, given in a 24-hour format. As you know, Karen loves palindromes, and she believes that it is good luck to wake up when the time is a palindrome. What is the minimum number of minutes she should sleep, such that, when she wakes up, the time is a palindrome? Remember that a palindrome is a string that reads the same forwards and backwards. For instance, 05:39 is not a palindrome, because 05:39 backwards is 93:50. On the other hand, 05:50 is a palindrome, because 05:50 backwards is 05:50.
The first and only line of input contains a single string in the format hh:mm (00<=≀<= hh <=≀<=23, 00<=≀<= mm <=≀<=59).
Output a single integer on a line by itself, the minimum number of minutes she should sleep, such that, when she wakes up, the time is a palindrome.
[ "05:39\n", "13:31\n", "23:59\n" ]
[ "11\n", "0\n", "1\n" ]
In the first test case, the minimum number of minutes Karen should sleep for is 11. She can wake up at 05:50, when the time is a palindrome. In the second test case, Karen can wake up immediately, as the current time, 13:31, is already a palindrome. In the third test case, the minimum number of minutes Karen should sleep for is 1 minute. She can wake up at 00:00, when the time is a palindrome.
[ { "input": "05:39", "output": "11" }, { "input": "13:31", "output": "0" }, { "input": "23:59", "output": "1" }, { "input": "13:32", "output": "69" }, { "input": "14:40", "output": "1" }, { "input": "14:00", "output": "41" }, { "input": "05:...
61
6,963,200
0
6,024
879
Table Tennis
[ "data structures", "implementation" ]
null
null
*n* people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins *k* games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
The first line contains two integers: *n* and *k* (2<=≀<=*n*<=≀<=500, 2<=≀<=*k*<=≀<=1012)Β β€” the number of people and the number of wins. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=*n*) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all *a**i* are distinct.
Output a single integer β€” power of the winner.
[ "2 2\n1 2\n", "4 2\n3 1 2 4\n", "6 2\n6 5 3 1 2 4\n", "2 10000000000\n2 1\n" ]
[ "2 ", "3 ", "6 ", "2\n" ]
Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
[ { "input": "2 2\n1 2", "output": "2 " }, { "input": "4 2\n3 1 2 4", "output": "3 " }, { "input": "6 2\n6 5 3 1 2 4", "output": "6 " }, { "input": "2 10000000000\n2 1", "output": "2" }, { "input": "4 4\n1 3 4 2", "output": "4 " }, { "input": "2 21474836...
61
0
0
6,045
761
Dasha and Password
[ "brute force", "dp", "implementation" ]
null
null
After overcoming the stairs Dasha came to classes. She needed to write a password to begin her classes. The password is a string of length *n* which satisfies the following requirements: - There is at least one digit in the string, - There is at least one lowercase (small) letter of the Latin alphabet in the string, - There is at least one of three listed symbols in the string: '#', '*', '&amp;'. Considering that these are programming classes it is not easy to write the password. For each character of the password we have a fixed string of length *m*, on each of these *n* strings there is a pointer on some character. The *i*-th character displayed on the screen is the pointed character in the *i*-th string. Initially, all pointers are on characters with indexes 1 in the corresponding strings (all positions are numbered starting from one). During one operation Dasha can move a pointer in one string one character to the left or to the right. Strings are cyclic, it means that when we move the pointer which is on the character with index 1 to the left, it moves to the character with the index *m*, and when we move it to the right from the position *m* it moves to the position 1. You need to determine the minimum number of operations necessary to make the string displayed on the screen a valid password.
The first line contains two integers *n*, *m* (3<=≀<=*n*<=≀<=50,<=1<=≀<=*m*<=≀<=50) β€” the length of the password and the length of strings which are assigned to password symbols. Each of the next *n* lines contains the string which is assigned to the *i*-th symbol of the password string. Its length is *m*, it consists of digits, lowercase English letters, and characters '#', '*' or '&amp;'. You have such input data that you can always get a valid password.
Print one integer β€” the minimum number of operations which is necessary to make the string, which is displayed on the screen, a valid password.
[ "3 4\n1**2\na3*0\nc4**\n", "5 5\n#*&amp;#*\n*a1c&amp;\n&amp;q2w*\n#a3c#\n*&amp;#*&amp;\n" ]
[ "1\n", "3\n" ]
In the first test it is necessary to move the pointer of the third string to one left to get the optimal answer. In the second test one of possible algorithms will be: - to move the pointer of the second symbol once to the right. - to move the pointer of the third symbol twice to the right.
[ { "input": "3 4\n1**2\na3*0\nc4**", "output": "1" }, { "input": "5 5\n#*&#*\n*a1c&\n&q2w*\n#a3c#\n*&#*&", "output": "3" }, { "input": "5 2\n&l\n*0\n*9\n*#\n#o", "output": "2" }, { "input": "25 16\nvza**ooxkmd#*ywa\ndip#*#&ef&z&&&pv\nwggob&&72#*&&nku\nrsb##*&jm&#u**te\nzif#lu#...
685
30,003,200
3
6,049
57
Square Earth?
[ "dfs and similar", "greedy", "implementation" ]
A. Square Earth?
2
256
Meg the Rabbit decided to do something nice, specifically β€” to determine the shortest distance between two points on the surface of our planet. But Meg... what can you say, she wants everything simple. So, she already regards our planet as a two-dimensional circle. No, wait, it's even worse β€” as a square of side *n*. Thus, the task has been reduced to finding the shortest path between two dots on a square (the path should go through the square sides). To simplify the task let us consider the vertices of the square to lie at points whose coordinates are: (0,<=0), (*n*,<=0), (0,<=*n*) and (*n*,<=*n*).
The single line contains 5 space-separated integers: *n*,<=*x*1,<=*y*1,<=*x*2,<=*y*2 (1<=≀<=*n*<=≀<=1000,<=0<=≀<=*x*1,<=*y*1,<=*x*2,<=*y*2<=≀<=*n*) which correspondingly represent a side of the square, the coordinates of the first point and the coordinates of the second point. It is guaranteed that the points lie on the sides of the square.
You must print on a single line the shortest distance between the points.
[ "2 0 0 1 0\n", "2 0 1 2 1\n", "100 0 0 100 100\n" ]
[ "1\n", "4\n", "200\n" ]
none
[ { "input": "2 0 0 1 0", "output": "1" }, { "input": "2 0 1 2 1", "output": "4" }, { "input": "100 0 0 100 100", "output": "200" }, { "input": "4 0 3 1 4", "output": "2" }, { "input": "10 8 10 10 0", "output": "12" }, { "input": "26 21 0 26 14", "ou...
218
3,379,200
-1
6,062
427
Checkposts
[ "dfs and similar", "graphs", "two pointers" ]
null
null
Your city has *n* junctions. There are *m* one-way roads between the junctions. As a mayor of the city, you have to ensure the security of all the junctions. To ensure the security, you have to build some police checkposts. Checkposts can only be built in a junction. A checkpost at junction *i* can protect junction *j* if either *i*<==<=*j* or the police patrol car can go to *j* from *i* and then come back to *i*. Building checkposts costs some money. As some areas of the city are more expensive than others, building checkpost at some junctions might cost more money than other junctions. You have to determine the minimum possible money needed to ensure the security of all the junctions. Also you have to find the number of ways to ensure the security in minimum price and in addition in minimum number of checkposts. Two ways are different if any of the junctions contains a checkpost in one of them and do not contain in the other.
In the first line, you will be given an integer *n*, number of junctions (1<=≀<=*n*<=≀<=105). In the next line, *n* space-separated integers will be given. The *i**th* integer is the cost of building checkpost at the *i**th* junction (costs will be non-negative and will not exceed 109). The next line will contain an integer *m*Β (0<=≀<=*m*<=≀<=3Β·105). And each of the next *m* lines contains two integers *u**i* and *v**i*Β (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*;Β *u*<=β‰ <=*v*). A pair *u**i*,<=*v**i* means, that there is a one-way road which goes from *u**i* to *v**i*. There will not be more than one road between two nodes in the same direction.
Print two integers separated by spaces. The first one is the minimum possible money needed to ensure the security of all the junctions. And the second one is the number of ways you can ensure the security modulo 1000000007 (109<=+<=7).
[ "3\n1 2 3\n3\n1 2\n2 3\n3 2\n", "5\n2 8 0 6 0\n6\n1 4\n1 3\n2 4\n3 4\n4 5\n5 1\n", "10\n1 3 2 2 1 3 1 4 10 10\n12\n1 2\n2 3\n3 1\n3 4\n4 5\n5 6\n5 7\n6 4\n7 3\n8 9\n9 10\n10 9\n", "2\n7 91\n2\n1 2\n2 1\n" ]
[ "3 1\n", "8 2\n", "15 6\n", "7 1\n" ]
none
[ { "input": "3\n1 2 3\n3\n1 2\n2 3\n3 2", "output": "3 1" }, { "input": "5\n2 8 0 6 0\n6\n1 4\n1 3\n2 4\n3 4\n4 5\n5 1", "output": "8 2" }, { "input": "10\n1 3 2 2 1 3 1 4 10 10\n12\n1 2\n2 3\n3 1\n3 4\n4 5\n5 6\n5 7\n6 4\n7 3\n8 9\n9 10\n10 9", "output": "15 6" }, { "input": ...
810
22,630,400
-1
6,098
261
Maxim and Restaurant
[ "dp", "math", "probabilities" ]
null
null
Maxim has opened his own restaurant! The restaurant has got a huge table, the table's length is *p* meters. Maxim has got a dinner party tonight, *n* guests will come to him. Let's index the guests of Maxim's restaurant from 1 to *n*. Maxim knows the sizes of all guests that are going to come to him. The *i*-th guest's size (*a**i*) represents the number of meters the guest is going to take up if he sits at the restaurant table. Long before the dinner, the guests line up in a queue in front of the restaurant in some order. Then Maxim lets the guests in, one by one. Maxim stops letting the guests in when there is no place at the restaurant table for another guest in the queue. There is no place at the restaurant table for another guest in the queue, if the sum of sizes of all guests in the restaurant plus the size of this guest from the queue is larger than *p*. In this case, not to offend the guest who has no place at the table, Maxim doesn't let any other guest in the restaurant, even if one of the following guests in the queue would have fit in at the table. Maxim is now wondering, what is the average number of visitors who have come to the restaurant for all possible *n*! orders of guests in the queue. Help Maxim, calculate this number.
The first line contains integer *n* (1<=≀<=*n*<=≀<=50) β€” the number of guests in the restaurant. The next line contains integers *a*1, *a*2, ..., *a**n* (1<=≀<=*a**i*<=≀<=50) β€” the guests' sizes in meters. The third line contains integer *p* (1<=≀<=*p*<=≀<=50) β€” the table's length in meters. The numbers in the lines are separated by single spaces.
In a single line print a real number β€” the answer to the problem. The answer will be considered correct, if the absolute or relative error doesn't exceed 10<=-<=4.
[ "3\n1 2 3\n3\n" ]
[ "1.3333333333\n" ]
In the first sample the people will come in the following orders: - (1, 2, 3) β€” there will be two people in the restaurant; - (1, 3, 2) β€” there will be one person in the restaurant; - (2, 1, 3) β€” there will be two people in the restaurant; - (2, 3, 1) β€” there will be one person in the restaurant; - (3, 1, 2) β€” there will be one person in the restaurant; - (3, 2, 1) β€” there will be one person in the restaurant. In total we get (2 + 1 + 2 + 1 + 1 + 1) / 6 = 8 / 6 = 1.(3).
[ { "input": "3\n1 2 3\n3", "output": "1.3333333333" }, { "input": "9\n2 2 2 2 2 2 2 1 2\n9", "output": "4.5555555556" }, { "input": "7\n2 1 1 2 1 1 2\n2", "output": "1.2857142857" }, { "input": "8\n3 1 5 6 1 5 4 4\n7", "output": "1.6250000000" }, { "input": "2\n1 3...
248
716,800
-1
6,117
981
Bookshelves
[ "bitmasks", "dp", "greedy" ]
null
null
Mr Keks is a typical white-collar in Byteland. He has a bookshelf in his office with some books on it, each book has an integer positive price. Mr Keks defines the value of a shelf as the sum of books prices on it. Miraculously, Mr Keks was promoted and now he is moving into a new office. He learned that in the new office he will have not a single bookshelf, but exactly $k$ bookshelves. He decided that the beauty of the $k$ shelves is the [bitwise AND](https://en.wikipedia.org/wiki/Bitwise_operation#AND) of the values of all the shelves. He also decided that he won't spend time on reordering the books, so he will place several first books on the first shelf, several next books on the next shelf and so on. Of course, he will place at least one book on each shelf. This way he will put all his books on $k$ shelves in such a way that the beauty of the shelves is as large as possible. Compute this maximum possible beauty.
The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 50$)Β β€” the number of books and the number of shelves in the new office. The second line contains $n$ integers $a_1, a_2, \ldots a_n$, ($0 &lt; a_i &lt; 2^{50}$)Β β€” the prices of the books in the order they stand on the old shelf.
Print the maximum possible beauty of $k$ shelves in the new office.
[ "10 4\n9 14 28 1 7 13 15 29 2 31\n", "7 3\n3 14 15 92 65 35 89\n" ]
[ "24\n", "64\n" ]
In the first example you can split the books as follows: $$(9 + 14 + 28 + 1 + 7) \&amp; (13 + 15) \&amp; (29 + 2) \&amp; (31) = 24.$$ In the second example you can split the books as follows: $$(3 + 14 + 15 + 92) \&amp; (65) \&amp; (35 + 89) = 64.$$
[ { "input": "10 4\n9 14 28 1 7 13 15 29 2 31", "output": "24" }, { "input": "7 3\n3 14 15 92 65 35 89", "output": "64" }, { "input": "40 5\n6 18 24 5 14 16 31 9 15 5 25 2 18 12 19 27 10 23 23 18 22 14 1 14 6 14 17 28 11 21 8 23 10 30 21 5 17 11 26 16", "output": "80" }, { "inp...
655
50,995,200
3
6,122
40
Find Color
[ "constructive algorithms", "geometry", "implementation", "math" ]
A. Find Color
2
256
Not so long ago as a result of combat operations the main Berland place of interest β€” the magic clock β€” was damaged. The cannon's balls made several holes in the clock, that's why the residents are concerned about the repair. The magic clock can be represented as an infinite Cartesian plane, where the origin corresponds to the clock center. The clock was painted two colors as is shown in the picture: The picture shows only the central part of the clock. This coloring naturally extends to infinity. The balls can be taken to be points on the plane. Your task is to find the color of the area, damaged by the given ball. All the points located on the border of one of the areas have to be considered painted black.
The first and single line contains two integers *x* and *y* β€” the coordinates of the hole made in the clock by the ball. Each of the numbers *x* and *y* has an absolute value that does not exceed 1000.
Find the required color. All the points between which and the origin of coordinates the distance is integral-value are painted black.
[ "-2 1\n", "2 1\n", "4 3\n" ]
[ "white\n", "black\n", "black\n" ]
none
[ { "input": "-2 1", "output": "white" }, { "input": "2 1", "output": "black" }, { "input": "4 3", "output": "black" }, { "input": "3 3", "output": "black" }, { "input": "4 4", "output": "white" }, { "input": "-4 4", "output": "black" }, { "i...
154
6,758,400
3.948911
6,126
126
Pills
[ "brute force", "flows" ]
null
null
Doctor prescribed medicine to his patient. The medicine is represented by pills. Each pill consists of a shell and healing powder. The shell consists of two halves; each half has one of four colors β€” blue, red, white or yellow. The doctor wants to put 28 pills in a rectangular box 7<=Γ—<=8 in size. Besides, each pill occupies exactly two neighboring cells and any cell contains exactly one half of a pill. Thus, the result is a four colored picture 7<=Γ—<=8 in size. The doctor thinks that a patient will recover sooner if the picture made by the pills will be special. Unfortunately, putting the pills in the box so as to get the required picture is not a very easy task. That's why doctor asks you to help. Doctor has some amount of pills of each of 10 painting types. They all contain the same medicine, that's why it doesn't matter which 28 of them will be stored inside the box. Place the pills in the box so that the required picture was formed. If it is impossible to place the pills in the required manner, then place them so that the number of matching colors in all 56 cells in the final arrangement and the doctor's picture were maximum.
First 7 lines contain the doctor's picture. Each line contains 8 characters, each character can be "B", "R", "W" and "Y" that stands for blue, red, white and yellow colors correspondingly. Next four lines contain 10 numbers that stand for, correspondingly, the number of pills painted: "BY" "BW" "BR" "BB" "RY" "RW" "RR" "WY" "WW" "YY" Those numbers lie within range from 0 to 28 inclusively. It is guaranteed that the total number of pills in no less than 28.
Print on the first line the maximal number cells for which the colors match. Then print 13 lines each containing 15 characters β€” the pills' position in the optimal arrangement. The intersections of odd lines and odd columns should contain characters "B", "R", "W" and "Y". All other positions should contain characters ".", "-" and "|". Use "-" and "|" to show which halves belong to one pill. See the samples for more clarification. If there are several possible solutions, print any of them.
[ "WWWBBWWW\nWWWBBWWW\nYYWBBWWW\nYYWBBWRR\nYYWBBWRR\nYYWBBWRR\nYYWBBWRR\n0 0 0 8\n0 1 5\n1 10\n5\n", "WWWWWWWW\nWBYWRBBY\nBRYRBWYY\nWWBRYWBB\nBWWRWBYW\nRBWRBWYY\nWWWWWWWW\n0 0 0 1\n0 0 1\n0 1\n25\n" ]
[ "53\nW.W.W.B.B.W.W.W\n|.|.|.|.|.|.|.|\nW.W.W.B.B.W.W.W\n...............\nY.Y.W.B.B.W.W-W\n|.|.|.|.|.|....\nY.Y.W.B.B.W.R.R\n............|.|\nY.Y.W.B.B.R.R.R\n|.|.|.|.|.|....\nY.Y.W.B.B.W.R.R\n............|.|\nY-Y.B-B.B-B.R.R\n", "15\nW.Y.Y-Y.Y-Y.Y-Y\n|.|............\nW.Y.Y.Y.Y.B-B.Y\n....|.|.|.....|\nY-Y.Y.Y.Y.Y-...
none
[]
46
0
0
6,142
229
Planets
[ "binary search", "data structures", "graphs", "shortest paths" ]
null
null
Goa'uld Apophis captured Jack O'Neill's team again! Jack himself was able to escape, but by that time Apophis's ship had already jumped to hyperspace. But Jack knows on what planet will Apophis land. In order to save his friends, Jack must repeatedly go through stargates to get to this planet. Overall the galaxy has *n* planets, indexed with numbers from 1 to *n*. Jack is on the planet with index 1, and Apophis will land on the planet with index *n*. Jack can move between some pairs of planets through stargates (he can move in both directions); the transfer takes a positive, and, perhaps, for different pairs of planets unequal number of seconds. Jack begins his journey at time 0. It can be that other travellers are arriving to the planet where Jack is currently located. In this case, Jack has to wait for exactly 1 second before he can use the stargate. That is, if at time *t* another traveller arrives to the planet, Jack can only pass through the stargate at time *t*<=+<=1, unless there are more travellers arriving at time *t*<=+<=1 to the same planet. Knowing the information about travel times between the planets, and the times when Jack would not be able to use the stargate on particular planets, determine the minimum time in which he can get to the planet with index *n*.
The first line contains two space-separated integers: *n* (2<=≀<=*n*<=≀<=105), the number of planets in the galaxy, and *m* (0<=≀<=*m*<=≀<=105) β€” the number of pairs of planets between which Jack can travel using stargates. Then *m* lines follow, containing three integers each: the *i*-th line contains numbers of planets *a**i* and *b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*, *a**i*<=β‰ <=*b**i*), which are connected through stargates, and the integer transfer time (in seconds) *c**i* (1<=≀<=*c**i*<=≀<=104) between these planets. It is guaranteed that between any pair of planets there is at most one stargate connection. Then *n* lines follow: the *i*-th line contains an integer *k**i* (0<=≀<=*k**i*<=≀<=105) that denotes the number of moments of time when other travellers arrive to the planet with index *i*. Then *k**i* distinct space-separated integers *t**ij* (0<=≀<=*t**ij*<=&lt;<=109) follow, sorted in ascending order. An integer *t**ij* means that at time *t**ij* (in seconds) another traveller arrives to the planet *i*. It is guaranteed that the sum of all *k**i* does not exceed 105.
Print a single number β€” the least amount of time Jack needs to get from planet 1 to planet *n*. If Jack can't get to planet *n* in any amount of time, print number -1.
[ "4 6\n1 2 2\n1 3 3\n1 4 8\n2 3 4\n2 4 5\n3 4 3\n0\n1 3\n2 3 4\n0\n", "3 1\n1 2 3\n0\n1 3\n0\n" ]
[ "7\n", "-1\n" ]
In the first sample Jack has three ways to go from planet 1. If he moves to planet 4 at once, he spends 8 seconds. If he transfers to planet 3, he spends 3 seconds, but as other travellers arrive to planet 3 at time 3 and 4, he can travel to planet 4 only at time 5, thus spending 8 seconds in total. But if Jack moves to planet 2, and then β€” to planet 4, then he spends a total of only 2 + 5 = 7 seconds. In the second sample one can't get from planet 1 to planet 3 by moving through stargates.
[ { "input": "4 6\n1 2 2\n1 3 3\n1 4 8\n2 3 4\n2 4 5\n3 4 3\n0\n1 3\n2 3 4\n0", "output": "7" }, { "input": "3 1\n1 2 3\n0\n1 3\n0", "output": "-1" }, { "input": "2 1\n1 2 3\n0\n1 3", "output": "3" }, { "input": "2 1\n1 2 3\n1 0\n0", "output": "4" }, { "input": "3 3...
92
0
0
6,151
722
Generating Sets
[ "binary search", "data structures", "dfs and similar", "greedy", "strings", "trees" ]
null
null
You are given a set *Y* of *n* distinct positive integers *y*1,<=*y*2,<=...,<=*y**n*. Set *X* of *n* distinct positive integers *x*1,<=*x*2,<=...,<=*x**n* is said to generate set *Y* if one can transform *X* to *Y* by applying some number of the following two operation to integers in *X*: 1. Take any integer *x**i* and multiply it by two, i.e. replace *x**i* with 2Β·*x**i*. 1. Take any integer *x**i*, multiply it by two and add one, i.e. replace *x**i* with 2Β·*x**i*<=+<=1. Note that integers in *X* are not required to be distinct after each operation. Two sets of distinct integers *X* and *Y* are equal if they are equal as sets. In other words, if we write elements of the sets in the array in the increasing order, these arrays would be equal. Note, that any set of integers (or its permutation) generates itself. You are given a set *Y* and have to find a set *X* that generates *Y* and the maximum element of *X* is mininum possible.
The first line of the input contains a single integer *n* (1<=≀<=*n*<=≀<=50<=000)Β β€” the number of elements in *Y*. The second line contains *n* integers *y*1,<=...,<=*y**n* (1<=≀<=*y**i*<=≀<=109), that are guaranteed to be distinct.
Print *n* integersΒ β€” set of distinct integers that generate *Y* and the maximum element of which is minimum possible. If there are several such sets, print any of them.
[ "5\n1 2 3 4 5\n", "6\n15 14 3 13 1 12\n", "6\n9 7 13 17 5 11\n" ]
[ "4 5 2 3 1 \n", "12 13 14 7 3 1 \n", "4 5 2 6 3 1 \n" ]
none
[ { "input": "5\n1 2 3 4 5", "output": "4 5 2 3 1 " }, { "input": "6\n15 14 3 13 1 12", "output": "12 13 14 7 3 1 " }, { "input": "6\n9 7 13 17 5 11", "output": "4 5 2 6 3 1 " }, { "input": "10\n18 14 19 17 11 7 20 10 4 12", "output": "8 9 4 10 5 2 6 7 3 1 " }, { "i...
46
0
0
6,178
770
Online Courses In BSU
[ "*special", "dfs and similar", "graphs", "implementation" ]
null
null
Now you can take online courses in the Berland State University! Polycarp needs to pass *k* main online courses of his specialty to get a diploma. In total *n* courses are availiable for the passage. The situation is complicated by the dependence of online courses, for each course there is a list of those that must be passed before starting this online course (the list can be empty, it means that there is no limitation). Help Polycarp to pass the least number of courses in total to get the specialty (it means to pass all main and necessary courses). Write a program which prints the order of courses. Polycarp passes courses consistently, he starts the next course when he finishes the previous one. Each course can't be passed more than once.
The first line contains *n* and *k* (1<=≀<=*k*<=≀<=*n*<=≀<=105) β€” the number of online-courses and the number of main courses of Polycarp's specialty. The second line contains *k* distinct integers from 1 to *n* β€” numbers of main online-courses of Polycarp's specialty. Then *n* lines follow, each of them describes the next course: the *i*-th of them corresponds to the course *i*. Each line starts from the integer *t**i* (0<=≀<=*t**i*<=≀<=*n*<=-<=1) β€” the number of courses on which the *i*-th depends. Then there follows the sequence of *t**i* distinct integers from 1 to *n* β€” numbers of courses in random order, on which the *i*-th depends. It is guaranteed that no course can depend on itself. It is guaranteed that the sum of all values *t**i* doesn't exceed 105.
Print -1, if there is no the way to get a specialty. Otherwise, in the first line print the integer *m* β€” the minimum number of online-courses which it is necessary to pass to get a specialty. In the second line print *m* distinct integers β€” numbers of courses which it is necessary to pass in the chronological order of their passage. If there are several answers it is allowed to print any of them.
[ "6 2\n5 3\n0\n0\n0\n2 2 1\n1 4\n1 5\n", "9 3\n3 9 5\n0\n0\n3 9 4 5\n0\n0\n1 8\n1 6\n1 2\n2 1 2\n", "3 3\n1 2 3\n1 2\n1 3\n1 1\n" ]
[ "5\n1 2 3 4 5 \n", "6\n1 2 9 4 5 3 \n", "-1\n" ]
In the first test firstly you can take courses number 1 and 2, after that you can take the course number 4, then you can take the course number 5, which is the main. After that you have to take only the course number 3, which is the last not passed main course.
[ { "input": "6 2\n5 3\n0\n0\n0\n2 2 1\n1 4\n1 5", "output": "5\n1 2 3 4 5 " }, { "input": "9 3\n3 9 5\n0\n0\n3 9 4 5\n0\n0\n1 8\n1 6\n1 2\n2 1 2", "output": "6\n1 2 9 4 5 3 " }, { "input": "3 3\n1 2 3\n1 2\n1 3\n1 1", "output": "-1" }, { "input": "5 3\n2 1 4\n0\n0\n1 5\n0\n0",...
46
307,200
0
6,185
744
Hongcow Masters the Cyclic Shift
[ "strings", "two pointers" ]
null
null
Hongcow's teacher heard that Hongcow had learned about the cyclic shift, and decided to set the following problem for him. You are given a list of *n* strings *s*1,<=*s*2,<=...,<=*s**n* contained in the list *A*. A list *X* of strings is called stable if the following condition holds. First, a message is defined as a concatenation of some elements of the list *X*. You can use an arbitrary element as many times as you want, and you may concatenate these elements in any arbitrary order. Let *S**X* denote the set of of all messages you can construct from the list. Of course, this set has infinite size if your list is nonempty. Call a single message good if the following conditions hold: - Suppose the message is the concatenation of *k* strings *w*1,<=*w*2,<=...,<=*w**k*, where each *w**i* is an element of *X*. - Consider the |*w*1|<=+<=|*w*2|<=+<=...<=+<=|*w**k*| cyclic shifts of the string. Let *m* be the number of these cyclic shifts of the string that are elements of *S**X*. - A message is good if and only if *m* is exactly equal to *k*. The list *X* is called stable if and only if every element of *S**X* is good. Let *f*(*L*) be 1 if *L* is a stable list, and 0 otherwise. Find the sum of *f*(*L*) where *L* is a nonempty contiguous sublist of *A* (there are contiguous sublists in total).
The first line of input will contain a single integer *n* (1<=≀<=*n*<=≀<=30), denoting the number of strings in the list. The next *n* lines will each contain a string *s**i* ().
Print a single integer, the number of nonempty contiguous sublists that are stable.
[ "4\na\nab\nb\nbba\n", "5\nhh\nee\nll\nll\noo\n", "6\naab\nab\nbba\nb\nab\nc\n" ]
[ "7\n", "0\n", "13\n" ]
For the first sample, there are 10 sublists to consider. Sublists ["a", "ab", "b"], ["ab", "b", "bba"], and ["a", "ab", "b", "bba"] are not stable. The other seven sublists are stable. For example, *X* = ["a", "ab", "b"] is not stable, since the message "ab" + "ab" = "abab" has four cyclic shifts ["abab", "baba", "abab", "baba"], which are all elements of *S*<sub class="lower-index">*X*</sub>.
[]
46
4,608,000
-1
6,221
338
GCD Table
[ "chinese remainder theorem", "math", "number theory" ]
null
null
Consider a table *G* of size *n*<=Γ—<=*m* such that *G*(*i*,<=*j*)<==<=*GCD*(*i*,<=*j*) for all 1<=≀<=*i*<=≀<=*n*,<=1<=≀<=*j*<=≀<=*m*. *GCD*(*a*,<=*b*) is the greatest common divisor of numbers *a* and *b*. You have a sequence of positive integer numbers *a*1,<=*a*2,<=...,<=*a**k*. We say that this sequence occurs in table *G* if it coincides with consecutive elements in some row, starting from some position. More formally, such numbers 1<=≀<=*i*<=≀<=*n* and 1<=≀<=*j*<=≀<=*m*<=-<=*k*<=+<=1 should exist that *G*(*i*,<=*j*<=+<=*l*<=-<=1)<==<=*a**l* for all 1<=≀<=*l*<=≀<=*k*. Determine if the sequence *a* occurs in table *G*.
The first line contains three space-separated integers *n*, *m* and *k* (1<=≀<=*n*,<=*m*<=≀<=1012; 1<=≀<=*k*<=≀<=10000). The second line contains *k* space-separated integers *a*1,<=*a*2,<=...,<=*a**k* (1<=≀<=*a**i*<=≀<=1012).
Print a single word "YES", if the given sequence occurs in table *G*, otherwise print "NO".
[ "100 100 5\n5 2 1 2 1\n", "100 8 5\n5 2 1 2 1\n", "100 100 7\n1 2 3 4 5 6 7\n" ]
[ "YES\n", "NO\n", "NO\n" ]
Sample 1. The tenth row of table *G* starts from sequence {1, 2, 1, 2, 5, 2, 1, 2, 1, 10}. As you can see, elements from fifth to ninth coincide with sequence *a*. Sample 2. This time the width of table *G* equals 8. Sequence *a* doesn't occur there.
[ { "input": "100 100 5\n5 2 1 2 1", "output": "YES" }, { "input": "100 8 5\n5 2 1 2 1", "output": "NO" }, { "input": "100 100 7\n1 2 3 4 5 6 7", "output": "NO" }, { "input": "5 5 5\n1 1 1 1 1", "output": "YES" }, { "input": "11 10 1\n11", "output": "NO" }, ...
186
921,600
3
6,250
476
Dreamoon and Sets
[ "constructive algorithms", "greedy", "math" ]
null
null
Dreamoon likes to play with sets, integers and . is defined as the largest positive integer that divides both *a* and *b*. Let *S* be a set of exactly four distinct integers greater than 0. Define *S* to be of rank *k* if and only if for all pairs of distinct elements *s**i*, *s**j* from *S*, . Given *k* and *n*, Dreamoon wants to make up *n* sets of rank *k* using integers from 1 to *m* such that no integer is used in two different sets (of course you can leave some integers without use). Calculate the minimum *m* that makes it possible and print one possible solution.
The single line of the input contains two space separated integers *n*, *k* (1<=≀<=*n*<=≀<=10<=000,<=1<=≀<=*k*<=≀<=100).
On the first line print a single integer β€” the minimal possible *m*. On each of the next *n* lines print four space separated integers representing the *i*-th set. Neither the order of the sets nor the order of integers within a set is important. If there are multiple possible solutions with minimal *m*, print any one of them.
[ "1 1\n", "2 2\n" ]
[ "5\n1 2 3 5\n", "22\n2 4 6 22\n14 18 10 16\n" ]
For the first example it's easy to see that set {1, 2, 3, 4} isn't a valid set of rank 1 since <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e2af04e5e60e1fe79a4d74bf22dfa575f0b0f7bb.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
[ { "input": "1 1", "output": "5\n1 3 4 5" }, { "input": "2 2", "output": "22\n2 6 8 10\n14 18 20 22" }, { "input": "7 7", "output": "287\n7 21 28 35\n49 63 70 77\n91 105 112 119\n133 147 154 161\n175 189 196 203\n217 231 238 245\n259 273 280 287" }, { "input": "13 7", "out...
31
0
-1
6,270
859
Declined Finalists
[ "greedy", "implementation" ]
null
null
This year, as in previous years, MemSQL is inviting the top 25 competitors from the Start[c]up qualification round to compete onsite for the final round. Not everyone who is eligible to compete onsite can afford to travel to the office, though. Initially the top 25 contestants are invited to come onsite. Each eligible contestant must either accept or decline the invitation. Whenever a contestant declines, the highest ranked contestant not yet invited is invited to take the place of the one that declined. This continues until 25 contestants have accepted invitations. After the qualifying round completes, you know *K* of the onsite finalists, as well as their qualifying ranks (which start at 1, there are no ties). Determine the minimum possible number of contestants that declined the invitation to compete onsite in the final round.
The first line of input contains *K* (1<=≀<=*K*<=≀<=25), the number of onsite finalists you know. The second line of input contains *r*1,<=*r*2,<=...,<=*r**K* (1<=≀<=*r**i*<=≀<=106), the qualifying ranks of the finalists you know. All these ranks are distinct.
Print the minimum possible number of contestants that declined the invitation to compete onsite.
[ "25\n2 3 4 5 6 7 8 9 10 11 12 14 15 16 17 18 19 20 21 22 23 24 25 26 28\n", "5\n16 23 8 15 4\n", "3\n14 15 92\n" ]
[ "3\n", "0\n", "67\n" ]
In the first example, you know all 25 onsite finalists. The contestants who ranked 1-st, 13-th, and 27-th must have declined, so the answer is 3.
[ { "input": "25\n2 3 4 5 6 7 8 9 10 11 12 14 15 16 17 18 19 20 21 22 23 24 25 26 28", "output": "3" }, { "input": "5\n16 23 8 15 4", "output": "0" }, { "input": "3\n14 15 92", "output": "67" }, { "input": "1\n1000000", "output": "999975" }, { "input": "25\n1000000 ...
46
0
3
6,278
3
Least Cost Bracket Sequence
[ "greedy" ]
D. Least Cost Bracket Sequence
1
64
This is yet another problem on regular bracket sequences. A bracket sequence is called regular, if by inserting "+" and "1" into it we get a correct mathematical expression. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. You have a pattern of a bracket sequence that consists of characters "(", ")" and "?". You have to replace each character "?" with a bracket so, that you get a regular bracket sequence. For each character "?" the cost of its replacement with "(" and ")" is given. Among all the possible variants your should choose the cheapest.
The first line contains a non-empty pattern of even length, consisting of characters "(", ")" and "?". Its length doesn't exceed 5Β·104. Then there follow *m* lines, where *m* is the number of characters "?" in the pattern. Each line contains two integer numbers *a**i* and *b**i* (1<=≀<=*a**i*,<=<=*b**i*<=≀<=106), where *a**i* is the cost of replacing the *i*-th character "?" with an opening bracket, and *b**i* β€” with a closing one.
Print the cost of the optimal regular bracket sequence in the first line, and the required sequence in the second. Print -1, if there is no answer. If the answer is not unique, print any of them.
[ "(??)\n1 2\n2 8\n" ]
[ "4\n()()\n" ]
none
[ { "input": "(??)\n1 2\n2 8", "output": "4\n()()" }, { "input": "??\n1 1\n1 1", "output": "2\n()" }, { "input": "(???\n1 1\n1 1\n1 1", "output": "3\n(())" }, { "input": "(??)\n2 1\n1 1", "output": "2\n()()" }, { "input": "(???)?\n3 3\n3 1\n3 3\n2 3", "output": ...
404
2,048,000
3.782741
6,284
9
Hexadecimal's Numbers
[ "brute force", "implementation", "math" ]
C. Hexadecimal's Numbers
1
64
One beautiful July morning a terrible thing happened in Mainframe: a mean virus Megabyte somehow got access to the memory of his not less mean sister Hexadecimal. He loaded there a huge amount of *n* different natural numbers from 1 to *n* to obtain total control over her energy. But his plan failed. The reason for this was very simple: Hexadecimal didn't perceive any information, apart from numbers written in binary format. This means that if a number in a decimal representation contained characters apart from 0 and 1, it was not stored in the memory. Now Megabyte wants to know, how many numbers were loaded successfully.
Input data contains the only number *n* (1<=≀<=*n*<=≀<=109).
Output the only number β€” answer to the problem.
[ "10\n" ]
[ "2" ]
For *n* = 10 the answer includes numbers 1 and 10.
[ { "input": "10", "output": "2" }, { "input": "20", "output": "3" }, { "input": "72", "output": "3" }, { "input": "99", "output": "3" }, { "input": "100", "output": "4" }, { "input": "101", "output": "5" }, { "input": "102", "output": "5...
1,090
67,072,000
0
6,304
0
none
[ "none" ]
null
null
There are two small spaceship, surrounded by two groups of enemy larger spaceships. The space is a two-dimensional plane, and one group of the enemy spaceships is positioned in such a way that they all have integer $y$-coordinates, and their $x$-coordinate is equal to $-100$, while the second group is positioned in such a way that they all have integer $y$-coordinates, and their $x$-coordinate is equal to $100$. Each spaceship in both groups will simultaneously shoot two laser shots (infinite ray that destroys any spaceship it touches), one towards each of the small spaceships, all at the same time. The small spaceships will be able to avoid all the laser shots, and now want to position themselves at some locations with $x=0$ (with not necessarily integer $y$-coordinates), such that the rays shot at them would destroy as many of the enemy spaceships as possible. Find the largest numbers of spaceships that can be destroyed this way, assuming that the enemy spaceships can't avoid laser shots.
The first line contains two integers $n$ and $m$ ($1 \le n, m \le 60$), the number of enemy spaceships with $x = -100$ and the number of enemy spaceships with $x = 100$, respectively. The second line contains $n$ integers $y_{1,1}, y_{1,2}, \ldots, y_{1,n}$ ($|y_{1,i}| \le 10\,000$) β€” the $y$-coordinates of the spaceships in the first group. The third line contains $m$ integers $y_{2,1}, y_{2,2}, \ldots, y_{2,m}$ ($|y_{2,i}| \le 10\,000$) β€” the $y$-coordinates of the spaceships in the second group. The $y$ coordinates are not guaranteed to be unique, even within a group.
Print a single integer – the largest number of enemy spaceships that can be destroyed.
[ "3 9\n1 2 3\n1 2 3 7 8 9 11 12 13\n", "5 5\n1 2 3 4 5\n1 2 3 4 5\n" ]
[ "9\n", "10\n" ]
In the first example the first spaceship can be positioned at $(0, 2)$, and the second – at $(0, 7)$. This way all the enemy spaceships in the first group and $6$ out of $9$ spaceships in the second group will be destroyed. In the second example the first spaceship can be positioned at $(0, 3)$, and the second can be positioned anywhere, it will be sufficient to destroy all the enemy spaceships.
[ { "input": "3 9\n1 2 3\n1 2 3 7 8 9 11 12 13", "output": "9" }, { "input": "5 5\n1 2 3 4 5\n1 2 3 4 5", "output": "10" }, { "input": "50 50\n744 333 562 657 680 467 357 376 759 311 371 327 369 172 286 577 446 922 16 69 350 92 627 852 878 733 148 857 663 969 131 250 563 665 67 169 178 625...
30
0
0
6,314
802
Heidi and Library (easy)
[ "greedy" ]
null
null
Your search for Heidi is over – you finally found her at a library, dressed up as a human. In fact, she has spent so much time there that she now runs the place! Her job is to buy books and keep them at the library so that people can borrow and read them. There are *n* different books, numbered 1 through *n*. We will look at the library's operation during *n* consecutive days. Heidi knows in advance that on the *i*-th day (1<=≀<=*i*<=≀<=*n*) precisely one person will come to the library, request to borrow the book *a**i*, read it in a few hours, and return the book later on the same day. Heidi desperately wants to please all her guests, so she will make sure to always have the book *a**i* available in the library on the *i*-th day. During the night before the *i*-th day, she has the option of going to the bookstore (which operates at nights to avoid competition with the library) and buying any book for the price of 1 CHF. Of course, if she already has a book at the library, she does not need to buy it again. Initially, the library contains no books. There is a problem, though. The capacity of the library is *k* – this means that at any time, there can be at most *k* books at the library. If buying a new book would cause Heidi to have more than *k* books, she must first get rid of some book that she already has, in order to make room for the new book. If she later needs a book that she got rid of, she will need to buy that book again. You are given *k* and the sequence of requests for books *a*1,<=*a*2,<=...,<=*a**n*. What is the minimum cost (in CHF) of buying new books to satisfy all the requests?
The first line of input will contain two integers *n* and *k* (1<=≀<=*n*,<=*k*<=≀<=80). The second line will contain *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=*n*) – the sequence of book requests.
On a single line print the minimum cost of buying books at the store so as to satisfy all requests.
[ "4 80\n1 2 2 1\n", "4 1\n1 2 2 1\n", "4 2\n1 2 3 1\n" ]
[ "2\n", "3\n", "3\n" ]
In the first test case, Heidi is able to keep all books forever. Therefore, she only needs to buy the book 1 before the first day and the book 2 before the second day. In the second test case, she can only keep one book at a time. Therefore she will need to buy new books on the first, second and fourth day. In the third test case, before buying book 3 on the third day, she must decide which of the books 1 and 2 she should get rid of. Of course, she should keep the book 1, which will be requested on the fourth day.
[ { "input": "4 80\n1 2 2 1", "output": "2" }, { "input": "4 1\n1 2 2 1", "output": "3" }, { "input": "4 2\n1 2 3 1", "output": "3" }, { "input": "11 1\n1 2 3 5 1 10 10 1 1 3 5", "output": "9" }, { "input": "5 2\n1 2 3 1 2", "output": "4" }, { "input": "...
62
0
0
6,322
66
Petya and Post
[ "data structures", "dp" ]
E. Petya and Post
2
256
Little Vasya's uncle is a postman. The post offices are located on one circular road. Besides, each post office has its own gas station located next to it. Petya's uncle works as follows: in the morning he should leave the house and go to some post office. In the office he receives a portion of letters and a car. Then he must drive in the given car exactly one round along the circular road and return to the starting post office (the uncle can drive along the circle in any direction, counterclockwise or clockwise). Besides, since the car belongs to the city post, it should also be fuelled with gasoline only at the Post Office stations. The total number of stations equals to *n*. One can fuel the car at the *i*-th station with no more than *a**i* liters of gasoline. Besides, one can fuel the car no more than once at each station. Also, the distance between the 1-st and the 2-nd station is *b*1 kilometers, the distance between the 2-nd and the 3-rd one is *b*2 kilometers, ..., between the (*n*<=-<=1)-th and the *n*-th ones the distance is *b**n*<=-<=1 kilometers and between the *n*-th and the 1-st one the distance is *b**n* kilometers. Petya's uncle's high-tech car uses only one liter of gasoline per kilometer. It is known that the stations are located so that the sum of all *a**i* is equal to the sum of all *b**i*. The *i*-th gas station and *i*-th post office are very close, so the distance between them is 0 kilometers. Thus, it becomes clear that if we start from some post offices, then it is not always possible to drive one round along a circular road. The uncle faces the following problem: to what stations can he go in the morning to be able to ride exactly one circle along the circular road and visit all the post offices that are on it? Petya, who used to attend programming classes, has volunteered to help his uncle, but his knowledge turned out to be not enough, so he asks you to help him write the program that will solve the posed problem.
The first line contains integer *n* (1<=≀<=*n*<=≀<=105). The second line contains *n* integers *a**i* β€” amount of gasoline on the *i*-th station. The third line contains *n* integers *b*1,<=*b*2,<=...,<=*b**n*. They are the distances between the 1-st and the 2-nd gas stations, between the 2-nd and the 3-rd ones, ..., between the *n*-th and the 1-st ones, respectively. The sum of all *b**i* equals to the sum of all *a**i* and is no more than 109. Each of the numbers *a**i*, *b**i* is no less than 1 and no more than 109.
Print on the first line the number *k* β€” the number of possible post offices, from which the car can drive one circle along a circular road. Print on the second line *k* numbers in the ascending order β€” the numbers of offices, from which the car can start.
[ "4\n1 7 2 3\n8 1 1 3\n", "8\n1 2 1 2 1 2 1 2\n2 1 2 1 2 1 2 1\n" ]
[ "2\n2 4\n", "8\n1 2 3 4 5 6 7 8\n" ]
none
[ { "input": "4\n1 7 2 3\n8 1 1 3", "output": "2\n2 4" }, { "input": "8\n1 2 1 2 1 2 1 2\n2 1 2 1 2 1 2 1", "output": "8\n1 2 3 4 5 6 7 8" }, { "input": "20\n31 16 20 30 19 35 8 11 20 45 10 26 21 39 29 52 8 10 37 49\n16 33 41 32 43 24 35 48 19 37 28 26 7 10 23 48 18 2 1 25", "output": ...
92
0
0
6,330
327
Hungry Sequence
[ "math" ]
null
null
Iahub and Iahubina went to a date at a luxury restaurant. Everything went fine until paying for the food. Instead of money, the waiter wants Iahub to write a Hungry sequence consisting of *n* integers. A sequence *a*1, *a*2, ..., *a**n*, consisting of *n* integers, is Hungry if and only if: - Its elements are in increasing order. That is an inequality *a**i*<=&lt;<=*a**j* holds for any two indices *i*,<=*j* (*i*<=&lt;<=*j*). - For any two indices *i* and *j* (*i*<=&lt;<=*j*), *a**j* must not be divisible by *a**i*. Iahub is in trouble, so he asks you for help. Find a Hungry sequence with *n* elements.
The input contains a single integer: *n* (1<=≀<=*n*<=≀<=105).
Output a line that contains *n* space-separated integers *a*1 *a*2, ..., *a**n* (1<=≀<=*a**i*<=≀<=107), representing a possible Hungry sequence. Note, that each *a**i* must not be greater than 10000000 (107) and less than 1. If there are multiple solutions you can output any one.
[ "3\n", "5\n" ]
[ "2 9 15\n", "11 14 20 27 31\n" ]
none
[ { "input": "3", "output": "2 9 15" }, { "input": "5", "output": "11 14 20 27 31" }, { "input": "1", "output": "3" }, { "input": "1000", "output": "3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 ...
92
3,379,200
-1
6,333
932
Permutation Cycle
[ "brute force", "constructive algorithms" ]
null
null
For a permutation *P*[1... *N*] of integers from 1 to *N*, function *f* is defined as follows: Let *g*(*i*) be the minimum positive integer *j* such that *f*(*i*,<=*j*)<==<=*i*. We can show such *j* always exists. For given *N*,<=*A*,<=*B*, find a permutation *P* of integers from 1 to *N* such that for 1<=≀<=*i*<=≀<=*N*, *g*(*i*) equals either *A* or *B*.
The only line contains three integers *N*,<=*A*,<=*B* (1<=≀<=*N*<=≀<=106,<=1<=≀<=*A*,<=*B*<=≀<=*N*).
If no such permutation exists, output -1. Otherwise, output a permutation of integers from 1 to *N*.
[ "9 2 5\n", "3 2 1\n" ]
[ "6 5 8 3 4 1 9 2 7", "1 2 3 " ]
In the first example, *g*(1) = *g*(6) = *g*(7) = *g*(9) = 2 and *g*(2) = *g*(3) = *g*(4) = *g*(5) = *g*(8) = 5 In the second example, *g*(1) = *g*(2) = *g*(3) = 1
[ { "input": "9 2 5", "output": "2 1 4 3 6 7 8 9 5 " }, { "input": "3 2 1", "output": "1 2 3 " }, { "input": "7 4 4", "output": "-1" }, { "input": "1000000 999998 3", "output": "-1" }, { "input": "1 1 1", "output": "1 " }, { "input": "993012 997 1001", ...
2,000
30,105,600
0
6,340
0
none
[ "none" ]
null
null
Vasya should paint a fence in front of his own cottage. The fence is a sequence of *n* wooden boards arranged in a single row. Each board is a 1 centimeter wide rectangle. Let's number the board fence using numbers 1,<=2,<=...,<=*n* from left to right. The height of the *i*-th board is *h**i* centimeters. Vasya has a 1 centimeter wide brush and the paint of two colors, red and green. Of course, the amount of the paint is limited. Vasya counted the area he can paint each of the colors. It turned out that he can not paint over *a* square centimeters of the fence red, and he can not paint over *b* square centimeters green. Each board of the fence should be painted exactly one of the two colors. Perhaps Vasya won't need one of the colors. In addition, Vasya wants his fence to look smart. To do this, he should paint the fence so as to minimize the value that Vasya called the fence unattractiveness value. Vasya believes that two consecutive fence boards, painted different colors, look unattractive. The unattractiveness value of a fence is the total length of contact between the neighboring boards of various colors. To make the fence look nice, you need to minimize the value as low as possible. Your task is to find what is the minimum unattractiveness Vasya can get, if he paints his fence completely. The picture shows the fence, where the heights of boards (from left to right) are 2,3,2,4,3,1. The first and the fifth boards are painted red, the others are painted green. The first and the second boards have contact length 2, the fourth and fifth boards have contact length 3, the fifth and the sixth have contact length 1. Therefore, the unattractiveness of the given painted fence is 2+3+1=6.
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=200) β€” the number of boards in Vasya's fence. The second line contains two integers *a* and *b* (0<=≀<=*a*,<=*b*<=≀<=4Β·104) β€” the area that can be painted red and the area that can be painted green, correspondingly. The third line contains a sequence of *n* integers *h*1,<=*h*2,<=...,<=*h**n* (1<=≀<=*h**i*<=≀<=200) β€” the heights of the fence boards. All numbers in the lines are separated by single spaces.
Print a single number β€” the minimum unattractiveness value Vasya can get if he paints his fence completely. If it is impossible to do, print <=-<=1.
[ "4\n5 7\n3 3 4 1\n", "3\n2 3\n1 3 1\n", "3\n3 3\n2 2 2\n" ]
[ "3\n", "2\n", "-1\n" ]
none
[]
92
0
0
6,343
213
Numbers
[ "combinatorics", "dp" ]
null
null
Furik loves writing all sorts of problems, especially such that he can't solve himself. You've got one of his problems, the one Furik gave to Rubik. And Rubik asks you to solve it. There is integer *n* and array *a*, consisting of ten integers, indexed by numbers from 0 to 9. Your task is to count the number of positive integers with the following properties: - the number's length does not exceed *n*; - the number doesn't have leading zeroes; - digit *i* (0<=≀<=*i*<=≀<=9) occurs in the number at least *a*[*i*] times.
The first line contains integer *n* (1<=≀<=*n*<=≀<=100). The next line contains 10 integers *a*[0], *a*[1], ..., *a*[9] (0<=≀<=*a*[*i*]<=≀<=100) β€” elements of array *a*. The numbers are separated by spaces.
On a single line print the remainder of dividing the answer to the problem by 1000000007 (109<=+<=7).
[ "1\n0 0 0 0 0 0 0 0 0 1\n", "2\n1 1 0 0 0 0 0 0 0 0\n", "3\n1 1 0 0 0 0 0 0 0 0\n" ]
[ "1\n", "1\n", "36\n" ]
In the first sample number 9 meets the requirements. In the second sample number 10 meets the requirements. In the third sample numbers 10, 110, 210, 120, 103 meet the requirements. There are other suitable numbers, 36 in total.
[ { "input": "1\n0 0 0 0 0 0 0 0 0 1", "output": "1" }, { "input": "2\n1 1 0 0 0 0 0 0 0 0", "output": "1" }, { "input": "3\n1 1 0 0 0 0 0 0 0 0", "output": "36" }, { "input": "4\n0 1 0 1 2 0 0 0 0 0", "output": "12" }, { "input": "5\n2 1 2 0 0 0 0 0 0 0", "outp...
92
0
0
6,350
75
Facetook Priority Wall
[ "expression parsing", "implementation", "strings" ]
B. Facetook Priority Wall
2
256
Facetook is a well known social network website, and it will launch a new feature called Facetook Priority Wall. This feature will sort all posts from your friends according to the priority factor (it will be described). This priority factor will be affected by three types of actions: - 1. "*X* posted on *Y*'s wall" (15 points), - 2. "*X* commented on *Y*'s post" (10 points), - 3. "*X* likes *Y*'s post" (5 points). *X* and *Y* will be two distinct names. And each action will increase the priority factor between *X* and *Y* (and vice versa) by the above value of points (the priority factor between *X* and *Y* is the same as the priority factor between *Y* and *X*). You will be given *n* actions with the above format (without the action number and the number of points), and you have to print all the distinct names in these actions sorted according to the priority factor with you.
The first line contains your name. The second line contains an integer *n*, which is the number of actions (1<=≀<=*n*<=≀<=100). Then *n* lines follow, it is guaranteed that each one contains exactly 1 action in the format given above. There is exactly one space between each two words in a line, and there are no extra spaces. All the letters are lowercase. All names in the input will consist of at least 1 letter and at most 10 small Latin letters.
Print *m* lines, where *m* is the number of distinct names in the input (excluding yourself). Each line should contain just 1 name. The names should be sorted according to the priority factor with you in the descending order (the highest priority factor should come first). If two or more names have the same priority factor, print them in the alphabetical (lexicographical) order. Note, that you should output all the names that are present in the input data (excluding yourself), even if that person has a zero priority factor. The lexicographical comparison is performed by the standard "&lt;" operator in modern programming languages. The line *a* is lexicographically smaller than the line *b*, if either *a* is the prefix of *b*, or if exists such an *i* (1<=≀<=*i*<=≀<=*min*(|*a*|,<=|*b*|)), that *a**i*<=&lt;<=*b**i*, and for any *j* (1<=≀<=*j*<=&lt;<=*i*) *a**j*<==<=*b**j*, where |*a*| and |*b*| stand for the lengths of strings *a* and *b* correspondently.
[ "ahmed\n3\nahmed posted on fatma's wall\nfatma commented on ahmed's post\nmona likes ahmed's post\n", "aba\n1\nlikes likes posted's post\n" ]
[ "fatma\nmona\n", "likes\nposted\n" ]
none
[ { "input": "ahmed\n3\nahmed posted on fatma's wall\nfatma commented on ahmed's post\nmona likes ahmed's post", "output": "fatma\nmona" }, { "input": "aba\n1\nlikes likes posted's post", "output": "likes\nposted" }, { "input": "nu\n5\ng commented on pwyndmh's post\nqv posted on g's wall\n...
62
102,400
0
6,353
182
Common Divisors
[ "brute force", "hashing", "implementation", "math", "strings" ]
null
null
Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with. String *a* is the divisor of string *b* if and only if there exists a positive integer *x* such that if we write out string *a* consecutively *x* times, we get string *b*. For example, string "abab" has two divisors β€” "ab" and "abab". Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
The first input line contains a non-empty string *s*1. The second input line contains a non-empty string *s*2. Lengths of strings *s*1 and *s*2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Print the number of common divisors of strings *s*1 and *s*2.
[ "abcdabcd\nabcdabcdabcdabcd\n", "aaa\naa\n" ]
[ "2\n", "1\n" ]
In first sample the common divisors are strings "abcd" and "abcdabcd". In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa".
[ { "input": "abcdabcd\nabcdabcdabcdabcd", "output": "2" }, { "input": "aaa\naa", "output": "1" }, { "input": "aaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaa", "output": "3" }, { "input": "aaaaaaaaaaaaaa\naaaaaaaaaaaaaa", "output": "4" }, { "input": "a\nb", ...
62
204,800
0
6,360
514
R2D2 and Droid Army
[ "binary search", "data structures", "two pointers" ]
null
null
An army of *n* droids is lined up in one row. Each droid is described by *m* integers *a*1,<=*a*2,<=...,<=*a**m*, where *a**i* is the number of details of the *i*-th type in this droid's mechanism. R2-D2 wants to destroy the sequence of consecutive droids of maximum length. He has *m* weapons, the *i*-th weapon can affect all the droids in the army by destroying one detail of the *i*-th type (if the droid doesn't have details of this type, nothing happens to it). A droid is considered to be destroyed when all of its details are destroyed. R2-D2 can make at most *k* shots. How many shots from the weapon of what type should R2-D2 make to destroy the sequence of consecutive droids of maximum length?
The first line contains three integers *n*,<=*m*,<=*k* (1<=≀<=*n*<=≀<=105, 1<=≀<=*m*<=≀<=5, 0<=≀<=*k*<=≀<=109) β€” the number of droids, the number of detail types and the number of available shots, respectively. Next *n* lines follow describing the droids. Each line contains *m* integers *a*1,<=*a*2,<=...,<=*a**m* (0<=≀<=*a**i*<=≀<=108), where *a**i* is the number of details of the *i*-th type for the respective robot.
Print *m* space-separated integers, where the *i*-th number is the number of shots from the weapon of the *i*-th type that the robot should make to destroy the subsequence of consecutive droids of the maximum length. If there are multiple optimal solutions, print any of them. It is not necessary to make exactly *k* shots, the number of shots can be less.
[ "5 2 4\n4 0\n1 2\n2 1\n0 2\n1 3\n", "3 2 4\n1 2\n1 3\n2 2\n" ]
[ "2 2\n", "1 3\n" ]
In the first test the second, third and fourth droids will be destroyed. In the second test the first and second droids will be destroyed.
[ { "input": "5 2 4\n4 0\n1 2\n2 1\n0 2\n1 3", "output": "2 2" }, { "input": "3 2 4\n1 2\n1 3\n2 2", "output": "1 3" }, { "input": "1 1 0\n0", "output": "0" }, { "input": "1 1 0\n1", "output": "0" }, { "input": "1 1 1\n0", "output": "0" }, { "input": "4 ...
46
0
0
6,386
353
Queue
[ "constructive algorithms", "dp" ]
null
null
There are *n* schoolchildren, boys and girls, lined up in the school canteen in front of the bun stall. The buns aren't ready yet and the line is undergoing some changes. Each second all boys that stand right in front of girls, simultaneously swap places with the girls (so that the girls could go closer to the beginning of the line). In other words, if at some time the *i*-th position has a boy and the (*i*<=+<=1)-th position has a girl, then in a second, the *i*-th position will have a girl and the (*i*<=+<=1)-th one will have a boy. Let's take an example of a line of four people: a boy, a boy, a girl, a girl (from the beginning to the end of the line). Next second the line will look like that: a boy, a girl, a boy, a girl. Next second it will be a girl, a boy, a girl, a boy. Next second it will be a girl, a girl, a boy, a boy. The line won't change any more. Your task is: given the arrangement of the children in the line to determine the time needed to move all girls in front of boys (in the example above it takes 3 seconds). Baking buns takes a lot of time, so no one leaves the line until the line stops changing.
The first line contains a sequence of letters without spaces *s*1*s*2... *s**n* (1<=≀<=*n*<=≀<=106), consisting of capital English letters M and F. If letter *s**i* equals M, that means that initially, the line had a boy on the *i*-th position. If letter *s**i* equals F, then initially the line had a girl on the *i*-th position.
Print a single integer β€” the number of seconds needed to move all the girls in the line in front of the boys. If the line has only boys or only girls, print 0.
[ "MFM\n", "MMFF\n", "FFMMM\n" ]
[ "1\n", "3\n", "0\n" ]
In the first test case the sequence of changes looks as follows: MFM  →  FMM. The second test sample corresponds to the sample from the statement. The sequence of changes is: MMFF  →  MFMF  →  FMFM  →  FFMM.
[ { "input": "MFM", "output": "1" }, { "input": "MMFF", "output": "3" }, { "input": "FFMMM", "output": "0" }, { "input": "MMFMMFFFFM", "output": "7" }, { "input": "MFFFMMFMFMFMFFFMMMFFMMMMMMFMMFFMMMFMMFMFFFMMFMMMFFMMFFFFFMFMFFFMMMFFFMFMFMFMFFFMMMMFMMFMMFFMMMMMMFFM",...
0
0
-1
6,398
986
Perfect Encoding
[ "fft", "math" ]
null
null
You are working as an analyst in a company working on a new system for big data storage. This system will store $n$ different objects. Each object should have a unique ID. To create the system, you choose the parameters of the systemΒ β€” integers $m \ge 1$ and $b_{1}, b_{2}, \ldots, b_{m}$. With these parameters an ID of some object in the system is an array of integers $[a_{1}, a_{2}, \ldots, a_{m}]$ where $1 \le a_{i} \le b_{i}$ holds for every $1 \le i \le m$. Developers say that production costs are proportional to $\sum_{i=1}^{m} b_{i}$. You are asked to choose parameters $m$ and $b_{i}$ so that the system will be able to assign unique IDs to $n$ different objects and production costs are minimized. Note that you don't have to use all available IDs.
In the only line of input there is one positive integer $n$. The length of the decimal representation of $n$ is no greater than $1.5 \cdot 10^{6}$. The integer does not contain leading zeros.
Print one numberΒ β€” minimal value of $\sum_{i=1}^{m} b_{i}$.
[ "36\n", "37\n", "12345678901234567890123456789\n" ]
[ "10\n", "11\n", "177\n" ]
none
[ { "input": "36", "output": "10" }, { "input": "37", "output": "11" }, { "input": "12345678901234567890123456789", "output": "177" }, { "input": "1", "output": "1" }, { "input": "2", "output": "2" }, { "input": "3", "output": "3" }, { "input...
249
5,939,200
3
6,400
343
Rational Resistance
[ "math", "number theory" ]
null
null
Mad scientist Mike is building a time machine in his spare time. To finish the work, he needs a resistor with a certain resistance value. However, all Mike has is lots of identical resistors with unit resistance *R*0<==<=1. Elements with other resistance can be constructed from these resistors. In this problem, we will consider the following as elements: 1. one resistor; 1. an element and one resistor plugged in sequence; 1. an element and one resistor plugged in parallel. With the consecutive connection the resistance of the new element equals *R*<==<=*R**e*<=+<=*R*0. With the parallel connection the resistance of the new element equals . In this case *R**e* equals the resistance of the element being connected. Mike needs to assemble an element with a resistance equal to the fraction . Determine the smallest possible number of resistors he needs to make such an element.
The single input line contains two space-separated integers *a* and *b* (1<=≀<=*a*,<=*b*<=≀<=1018). It is guaranteed that the fraction is irreducible. It is guaranteed that a solution always exists.
Print a single number β€” the answer to the problem. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is recommended to use the cin, cout streams or the %I64d specifier.
[ "1 1\n", "3 2\n", "199 200\n" ]
[ "1\n", "3\n", "200\n" ]
In the first sample, one resistor is enough. In the second sample one can connect the resistors in parallel, take the resulting element and connect it to a third resistor consecutively. Then, we get an element with resistance <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/5305da389756aab6423d918a08ced468f05604df.png" style="max-width: 100.0%;max-height: 100.0%;"/>. We cannot make this element using two resistors.
[ { "input": "1 1", "output": "1" }, { "input": "3 2", "output": "3" }, { "input": "199 200", "output": "200" }, { "input": "1 1000000000000000000", "output": "1000000000000000000" }, { "input": "3 1", "output": "3" }, { "input": "21 8", "output": "7...
122
0
0
6,403
45
Codecraft III
[ "implementation" ]
A. Codecraft III
2
256
Today Vasya visited a widely known site and learned that the continuation of his favourite game Codecraft II will appear after exactly *k* months. He looked at the calendar and learned that at the moment is the month number *s*. Vasya immediately got interested in what month Codecraft III will appear. Help him understand that. All the twelve months in Vasya's calendar are named using their usual English names: January, February, March, April, May, June, July, August, September, October, November, December.
The first input line contains the name of the current month. It is guaranteed that it is a proper English name of one of twelve months. The first letter is uppercase, the rest are lowercase. The second line contains integer *k* (0<=≀<=*k*<=≀<=100) β€” the number of months left till the appearance of Codecraft III.
Print starting from an uppercase letter the name of the month in which the continuation of Codeforces II will appear. The printed name must be contained in the list January, February, March, April, May, June, July, August, September, October, November, December.
[ "November\n3\n", "May\n24\n" ]
[ "February\n", "May\n" ]
none
[ { "input": "November\n3", "output": "February" }, { "input": "May\n24", "output": "May" }, { "input": "April\n0", "output": "April" }, { "input": "September\n0", "output": "September" }, { "input": "August\n0", "output": "August" }, { "input": "June\n1...
186
0
0
6,424
57
Martian Architecture
[ "implementation" ]
B. Martian Architecture
2
256
Chris the Rabbit found the traces of an ancient Martian civilization. The brave astronomer managed to see through a small telescope an architecture masterpiece β€” "A Road to the Sun". The building stands on cubical stones of the same size. The foundation divides the entire "road" into cells, into which the cubical stones are fit tightly. Thus, to any cell of the foundation a coordinate can be assigned. To become the leader of the tribe, a Martian should build a Road to the Sun, that is to build from those cubical stones on a given foundation a stairway. The stairway should be described by the number of stones in the initial coordinate and the coordinates of the stairway's beginning and end. Each following cell in the coordinate's increasing order should contain one cubical stone more than the previous one. At that if the cell has already got stones, they do not count in this building process, the stairways were simply built on them. In other words, let us assume that a stairway is built with the initial coordinate of *l*, the final coordinate of *r* and the number of stones in the initial coordinate *x*. That means that *x* stones will be added in the cell *l*, *x*<=+<=1 stones will be added in the cell *l*<=+<=1, ..., *x*<=+<=*r*<=-<=*l* stones will be added in the cell *r*. Chris managed to find an ancient manuscript, containing the descriptions of all the stairways. Now he wants to compare the data to be sure that he has really found "A Road to the Sun". For that he chose some road cells and counted the total number of cubical stones that has been accumulated throughout the Martian history and then asked you to count using the manuscript to what the sum should ideally total.
The first line contains three space-separated integers: *n*,<=*m*,<=*k* (1<=≀<=*n*,<=*m*<=≀<=105,<=1<=≀<=*k*<=≀<=*min*(*n*,<=100)) which is the number of cells, the number of "Roads to the Sun" and the number of cells in the query correspondingly. Each of the following *m* roads contain three space-separated integers: *a**i*,<=*b**i*,<=*c**i* (1<=≀<=*a**i*<=≀<=*b**i*<=≀<=*n*,<=1<=≀<=*c**i*<=≀<=1000) which are the stairway's description, its beginning, end and the initial cell's height. Then follow a line, containing *k* different space-separated integers *b**i*. All these numbers ranging from 1 to *n* are cells, the number of stones in which interests Chris.
You have to print a single number on a single line which is the sum of stones in all the cells Chris is interested in. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d).
[ "5 2 1\n1 5 1\n2 4 1\n3\n", "3 2 1\n1 3 1\n1 3 1\n2\n", "3 2 1\n1 3 1\n1 3 1\n3\n" ]
[ "5\n", "4\n", "6\n" ]
none
[ { "input": "5 2 1\n1 5 1\n2 4 1\n3", "output": "5" }, { "input": "3 2 1\n1 3 1\n1 3 1\n2", "output": "4" }, { "input": "3 2 1\n1 3 1\n1 3 1\n3", "output": "6" }, { "input": "10 10 3\n6 10 74\n7 9 35\n3 6 63\n2 4 80\n2 10 78\n10 10 13\n4 10 16\n1 2 13\n3 7 17\n4 6 67\n9 8 10",...
2,000
8,806,400
0
6,466
958
Hyperspace Jump (easy)
[ "expression parsing", "math" ]
null
null
The Rebel fleet is on the run. It consists of *m* ships currently gathered around a single planet. Just a few seconds ago, the vastly more powerful Empire fleet has appeared in the same solar system, and the Rebels will need to escape into hyperspace. In order to spread the fleet, the captain of each ship has independently come up with the coordinate to which that ship will jump. In the obsolete navigation system used by the Rebels, this coordinate is given as the value of an arithmetic expression of the form . To plan the future of the resistance movement, Princess Heidi needs to know, for each ship, how many ships are going to end up at the same coordinate after the jump. You are her only hope!
The first line of the input contains a single integer *m* (1<=≀<=*m*<=≀<=200<=000) – the number of ships. The next *m* lines describe one jump coordinate each, given as an arithmetic expression. An expression has the form (a+b)/c. Namely, it consists of: an opening parenthesis (, a positive integer *a* of up to two decimal digits, a plus sign +, a positive integer *b* of up to two decimal digits, a closing parenthesis ), a slash /, and a positive integer *c* of up to two decimal digits.
Print a single line consisting of *m* space-separated integers. The *i*-th integer should be equal to the number of ships whose coordinate is equal to that of the *i*-th ship (including the *i*-th ship itself).
[ "4\n(99+98)/97\n(26+4)/10\n(12+33)/15\n(5+1)/7\n" ]
[ "1 2 2 1 " ]
In the sample testcase, the second and the third ship will both end up at the coordinate 3. Note that this problem has only two versions – easy and hard.
[ { "input": "4\n(99+98)/97\n(26+4)/10\n(12+33)/15\n(5+1)/7", "output": "1 2 2 1 " }, { "input": "10\n(44+98)/19\n(36+58)/47\n(62+74)/68\n(69+95)/82\n(26+32)/29\n(32+46)/39\n(32+24)/28\n(47+61)/54\n(39+13)/26\n(98+98)/98", "output": "1 9 9 9 9 9 9 9 9 9 " }, { "input": "30\n(89+76)/87\n(81...
608
19,763,200
3
6,477
94
Friends
[ "graphs", "implementation", "math" ]
B. Friends
1
256
One day Igor K. stopped programming and took up math. One late autumn evening he was sitting at a table reading a book and thinking about something. The following statement caught his attention: "Among any six people there are either three pairwise acquainted people or three pairwise unacquainted people" Igor just couldn't get why the required minimum is 6 people. "Well, that's the same for five people, too!" β€” he kept on repeating in his mind. β€” "Let's take, say, Max, Ilya, Vova β€” here, they all know each other! And now let's add Dima and Oleg to Vova β€” none of them is acquainted with each other! Now, that math is just rubbish!" Igor K. took 5 friends of his and wrote down who of them is friends with whom. Now he wants to check whether it is true for the five people that among them there are either three pairwise acquainted or three pairwise not acquainted people.
The first line contains an integer *m* (0<=≀<=*m*<=≀<=10), which is the number of relations of acquaintances among the five friends of Igor's. Each of the following *m* lines contains two integers *a**i* and *b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=5;*a**i*<=β‰ <=*b**i*), where (*a**i*,<=*b**i*) is a pair of acquainted people. It is guaranteed that each pair of the acquaintances is described exactly once. The acquaintance relation is symmetrical, i.e. if *x* is acquainted with *y*, then *y* is also acquainted with *x*.
Print "FAIL", if among those five people there are no either three pairwise acquainted or three pairwise unacquainted people. Otherwise print "WIN".
[ "4\n1 3\n2 3\n1 4\n5 3\n", "5\n1 2\n2 3\n3 4\n4 5\n5 1\n" ]
[ "WIN\n", "FAIL\n" ]
none
[ { "input": "4\n1 3\n2 3\n1 4\n5 3", "output": "WIN" }, { "input": "5\n1 2\n2 3\n3 4\n4 5\n5 1", "output": "FAIL" }, { "input": "1\n4 3", "output": "WIN" }, { "input": "6\n1 3\n2 3\n1 2\n5 3\n4 2\n4 5", "output": "WIN" }, { "input": "2\n1 3\n2 5", "output": "WI...
61
0
0
6,507
735
Urbanization
[ "greedy", "number theory", "sortings" ]
null
null
Local authorities have heard a lot about combinatorial abilities of Ostap Bender so they decided to ask his help in the question of urbanization. There are *n* people who plan to move to the cities. The wealth of the *i* of them is equal to *a**i*. Authorities plan to build two cities, first for *n*1 people and second for *n*2 people. Of course, each of *n* candidates can settle in only one of the cities. Thus, first some subset of candidates of size *n*1 settle in the first city and then some subset of size *n*2 is chosen among the remaining candidates and the move to the second city. All other candidates receive an official refuse and go back home. To make the statistic of local region look better in the eyes of their bosses, local authorities decided to pick subsets of candidates in such a way that the sum of arithmetic mean of wealth of people in each of the cities is as large as possible. Arithmetic mean of wealth in one city is the sum of wealth *a**i* among all its residents divided by the number of them (*n*1 or *n*2 depending on the city). The division should be done in real numbers without any rounding. Please, help authorities find the optimal way to pick residents for two cities.
The first line of the input contains three integers *n*, *n*1 and *n*2 (1<=≀<=*n*,<=*n*1,<=*n*2<=≀<=100<=000, *n*1<=+<=*n*2<=≀<=*n*)Β β€” the number of candidates who want to move to the cities, the planned number of residents of the first city and the planned number of residents of the second city. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=100<=000), the *i*-th of them is equal to the wealth of the *i*-th candidate.
Print one real valueΒ β€” the maximum possible sum of arithmetic means of wealth of cities' residents. You answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6. Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program will consider your answer correct, if .
[ "2 1 1\n1 5\n", "4 2 1\n1 4 2 3\n" ]
[ "6.00000000\n", "6.50000000\n" ]
In the first sample, one of the optimal solutions is to move candidate 1 to the first city and candidate 2 to the second. In the second sample, the optimal solution is to pick candidates 3 and 4 for the first city, and candidate 2 for the second one. Thus we obtain (*a*<sub class="lower-index">3</sub> + *a*<sub class="lower-index">4</sub>) / 2 + *a*<sub class="lower-index">2</sub> = (3 + 2) / 2 + 4 = 6.5
[ { "input": "2 1 1\n1 5", "output": "6.00000000" }, { "input": "4 2 1\n1 4 2 3", "output": "6.50000000" }, { "input": "3 1 2\n1 2 3", "output": "4.50000000" }, { "input": "10 4 6\n3 5 7 9 12 25 67 69 83 96", "output": "88.91666667" }, { "input": "19 7 12\n1 2 4 8 1...
202
30,720,000
3
6,519
797
Broken BST
[ "data structures", "dfs and similar" ]
null
null
Let *T* be arbitrary binary tree β€” tree, every vertex of which has no more than two children. Given tree is rooted, so there exists only one vertex which doesn't have a parent β€” it's the root of a tree. Every vertex has an integer number written on it. Following algorithm is run on every value from the tree *T*: 1. Set pointer to the root of a tree. 1. Return success if the value in the current vertex is equal to the number you are looking for 1. Go to the left child of the vertex if the value in the current vertex is greater than the number you are looking for 1. Go to the right child of the vertex if the value in the current vertex is less than the number you are looking for 1. Return fail if you try to go to the vertex that doesn't exist Here is the pseudo-code of the described algorithm: The described algorithm works correctly if the tree is binary search tree (i.e. for each node the values of left subtree are less than the value in the node, the values of right subtree are greater than the value in the node). But it can return invalid result if tree is not a binary search tree. Since the given tree is not necessarily a binary search tree, not all numbers can be found this way. Your task is to calculate, how many times the search will fail being running on every value from the tree. If the tree has multiple vertices with the same values on them then you should run algorithm on every one of them separately.
First line contains integer number *n* (1<=≀<=*n*<=≀<=105) β€” number of vertices in the tree. Each of the next *n* lines contains 3 numbers *v*, *l*, *r* (0<=≀<=*v*<=≀<=109) β€” value on current vertex, index of the left child of the vertex and index of the right child of the vertex, respectively. If some child doesn't exist then number <=-<=1 is set instead. Note that different vertices of the tree may contain the same values.
Print number of times when search algorithm will fail.
[ "3\n15 -1 -1\n10 1 3\n5 -1 -1\n", "8\n6 2 3\n3 4 5\n12 6 7\n1 -1 8\n4 -1 -1\n5 -1 -1\n14 -1 -1\n2 -1 -1\n" ]
[ "2\n", "1\n" ]
In the example the root of the tree in vertex 2. Search of numbers 5 and 15 will return fail because on the first step algorithm will choose the subtree which doesn't contain numbers you are looking for.
[ { "input": "3\n15 -1 -1\n10 1 3\n5 -1 -1", "output": "2" }, { "input": "8\n6 2 3\n3 4 5\n12 6 7\n1 -1 8\n4 -1 -1\n5 -1 -1\n14 -1 -1\n2 -1 -1", "output": "1" }, { "input": "1\n493041212 -1 -1", "output": "0" }, { "input": "10\n921294733 5 9\n341281094 -1 -1\n35060484 10 -1\n36...
888
31,232,000
-1
6,520
886
Maximum Element
[ "combinatorics", "dp", "math" ]
null
null
One day Petya was solving a very interesting problem. But although he used many optimization techniques, his solution still got Time limit exceeded verdict. Petya conducted a thorough analysis of his program and found out that his function for finding maximum element in an array of *n* positive integers was too slow. Desperate, Petya decided to use a somewhat unexpected optimization using parameter *k*, so now his function contains the following code: That way the function iteratively checks array elements, storing the intermediate maximum, and if after *k* consecutive iterations that maximum has not changed, it is returned as the answer. Now Petya is interested in fault rate of his function. He asked you to find the number of permutations of integers from 1 to *n* such that the return value of his function on those permutations is not equal to *n*. Since this number could be very big, output the answer modulo 109<=+<=7.
The only line contains two integers *n* and *k* (1<=≀<=*n*,<=*k*<=≀<=106), separated by a spaceΒ β€” the length of the permutations and the parameter *k*.
Output the answer to the problem modulo 109<=+<=7.
[ "5 2\n", "5 3\n", "6 3\n" ]
[ "22\n", "6\n", "84\n" ]
Permutations from second example: [4, 1, 2, 3, 5], [4, 1, 3, 2, 5], [4, 2, 1, 3, 5], [4, 2, 3, 1, 5], [4, 3, 1, 2, 5], [4, 3, 2, 1, 5].
[ { "input": "5 2", "output": "22" }, { "input": "5 3", "output": "6" }, { "input": "6 3", "output": "84" }, { "input": "5 10", "output": "0" }, { "input": "1 1", "output": "0" }, { "input": "2 1", "output": "0" }, { "input": "2 2", "outp...
108
15,052,800
0
6,522
903
Almost Difference
[ "data structures", "math" ]
null
null
Let's denote a function You are given an array *a* consisting of *n* integers. You have to calculate the sum of *d*(*a**i*,<=*a**j*) over all pairs (*i*,<=*j*) such that 1<=≀<=*i*<=≀<=*j*<=≀<=*n*.
The first line contains one integer *n* (1<=≀<=*n*<=≀<=200000) β€” the number of elements in *a*. The second line contains *n* integers *a*1, *a*2, ..., *a**n* (1<=≀<=*a**i*<=≀<=109) β€” elements of the array.
Print one integer β€” the sum of *d*(*a**i*,<=*a**j*) over all pairs (*i*,<=*j*) such that 1<=≀<=*i*<=≀<=*j*<=≀<=*n*.
[ "5\n1 2 3 1 3\n", "4\n6 6 5 5\n", "4\n6 6 4 4\n" ]
[ "4\n", "0\n", "-8\n" ]
In the first example: 1. *d*(*a*<sub class="lower-index">1</sub>, *a*<sub class="lower-index">2</sub>) = 0; 1. *d*(*a*<sub class="lower-index">1</sub>, *a*<sub class="lower-index">3</sub>) = 2; 1. *d*(*a*<sub class="lower-index">1</sub>, *a*<sub class="lower-index">4</sub>) = 0; 1. *d*(*a*<sub class="lower-index">1</sub>, *a*<sub class="lower-index">5</sub>) = 2; 1. *d*(*a*<sub class="lower-index">2</sub>, *a*<sub class="lower-index">3</sub>) = 0; 1. *d*(*a*<sub class="lower-index">2</sub>, *a*<sub class="lower-index">4</sub>) = 0; 1. *d*(*a*<sub class="lower-index">2</sub>, *a*<sub class="lower-index">5</sub>) = 0; 1. *d*(*a*<sub class="lower-index">3</sub>, *a*<sub class="lower-index">4</sub>) =  - 2; 1. *d*(*a*<sub class="lower-index">3</sub>, *a*<sub class="lower-index">5</sub>) = 0; 1. *d*(*a*<sub class="lower-index">4</sub>, *a*<sub class="lower-index">5</sub>) = 2.
[ { "input": "5\n1 2 3 1 3", "output": "4" }, { "input": "4\n6 6 5 5", "output": "0" }, { "input": "4\n6 6 4 4", "output": "-8" }, { "input": "1\n1", "output": "0" }, { "input": "1\n1000000000", "output": "0" }, { "input": "2\n1 1000000000", "output"...
296
35,328,000
3
6,534
627
XOR Equation
[ "dp", "math" ]
null
null
Two positive integers *a* and *b* have a sum of *s* and a bitwise XOR of *x*. How many possible values are there for the ordered pair (*a*,<=*b*)?
The first line of the input contains two integers *s* and *x* (2<=≀<=*s*<=≀<=1012, 0<=≀<=*x*<=≀<=1012), the sum and bitwise xor of the pair of positive integers, respectively.
Print a single integer, the number of solutions to the given conditions. If no solutions exist, print 0.
[ "9 5\n", "3 3\n", "5 2\n" ]
[ "4\n", "2\n", "0\n" ]
In the first sample, we have the following solutions: (2, 7), (3, 6), (6, 3), (7, 2). In the second sample, the only solutions are (1, 2) and (2, 1).
[ { "input": "9 5", "output": "4" }, { "input": "3 3", "output": "2" }, { "input": "5 2", "output": "0" }, { "input": "6 0", "output": "1" }, { "input": "549755813887 549755813887", "output": "549755813886" }, { "input": "2 0", "output": "1" }, {...
46
0
0
6,538
706
Beru-taxi
[ "brute force", "geometry", "implementation" ]
null
null
Vasiliy lives at point (*a*,<=*b*) of the coordinate plane. He is hurrying up to work so he wants to get out of his house as soon as possible. New app suggested *n* available Beru-taxi nearby. The *i*-th taxi is located at point (*x**i*,<=*y**i*) and moves with a speed *v**i*. Consider that each of *n* drivers will move directly to Vasiliy and with a maximum possible speed. Compute the minimum time when Vasiliy will get in any of Beru-taxi cars.
The first line of the input contains two integers *a* and *b* (<=-<=100<=≀<=*a*,<=*b*<=≀<=100)Β β€” coordinates of Vasiliy's home. The second line contains a single integer *n* (1<=≀<=*n*<=≀<=1000)Β β€” the number of available Beru-taxi cars nearby. The *i*-th of the following *n* lines contains three integers *x**i*, *y**i* and *v**i* (<=-<=100<=≀<=*x**i*,<=*y**i*<=≀<=100, 1<=≀<=*v**i*<=≀<=100)Β β€” the coordinates of the *i*-th car and its speed. It's allowed that several cars are located at the same point. Also, cars may be located at exactly the same point where Vasiliy lives.
Print a single real valueΒ β€” the minimum time Vasiliy needs to get in any of the Beru-taxi cars. You answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6. Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program will consider your answer correct, if .
[ "0 0\n2\n2 0 1\n0 2 2\n", "1 3\n3\n3 3 2\n-2 3 6\n-2 7 10\n" ]
[ "1.00000000000000000000", "0.50000000000000000000" ]
In the first sample, first taxi will get to Vasiliy in time 2, and second will do this in time 1, therefore 1 is the answer. In the second sample, cars 2 and 3 will arrive simultaneously.
[ { "input": "0 0\n2\n2 0 1\n0 2 2", "output": "1.00000000000000000000" }, { "input": "1 3\n3\n3 3 2\n-2 3 6\n-2 7 10", "output": "0.50000000000000000000" }, { "input": "2 2\n10\n8 10 1\n14 18 5\n2 2 1\n4 2 2\n5 2 1\n0 2 1\n2 10 4\n10 2 4\n14 18 20\n14 18 10", "output": "0.000000000000...
77
1,638,400
3
6,540
922
Magic Forest
[ "brute force" ]
null
null
Imp is in a magic forest, where xorangles grow (wut?) 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*; - , where denotes the [bitwise xor](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of integers *x* and *y*. - (*a*,<=*b*,<=*c*) form a non-degenerate (with strictly positive area) triangle.
The only line contains a single integer *n* (1<=≀<=*n*<=≀<=2500).
Print the number of xorangles of order *n*.
[ "6\n", "10\n" ]
[ "1\n", "2\n" ]
The only xorangle in the first sample is (3, 5, 6).
[ { "input": "6", "output": "1" }, { "input": "10", "output": "2" }, { "input": "3", "output": "0" }, { "input": "4", "output": "0" }, { "input": "5", "output": "0" }, { "input": "2500", "output": "700393" }, { "input": "952", "output": "...
93
1,945,600
3
6,550
483
Friends and Presents
[ "binary search", "math" ]
null
null
You have two friends. You want to present each of them several positive integers. You want to present *cnt*1 numbers to the first friend and *cnt*2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends. In addition, the first friend does not like the numbers that are divisible without remainder by prime number *x*. The second one does not like the numbers that are divisible without remainder by prime number *y*. Of course, you're not going to present your friends numbers they don't like. Your task is to find such minimum number *v*, that you can form presents using numbers from a set 1,<=2,<=...,<=*v*. Of course you may choose not to present some numbers at all. A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
The only line contains four positive integers *cnt*1, *cnt*2, *x*, *y* (1<=≀<=*cnt*1,<=*cnt*2<=&lt;<=109; *cnt*1<=+<=*cnt*2<=≀<=109; 2<=≀<=*x*<=&lt;<=*y*<=≀<=3Β·104)Β β€” the numbers that are described in the statement. It is guaranteed that numbers *x*, *y* are prime.
Print a single integer β€” the answer to the problem.
[ "3 1 2 3\n", "1 3 2 3\n" ]
[ "5\n", "4\n" ]
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend. In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
[ { "input": "3 1 2 3", "output": "5" }, { "input": "1 3 2 3", "output": "4" }, { "input": "916200 69682 2 3", "output": "1832399" }, { "input": "808351 17767 433 509", "output": "826121" }, { "input": "8851 901 20897 26183", "output": "9752" }, { "input...
62
0
3
6,566
244
Undoubtedly Lucky Numbers
[ "bitmasks", "brute force", "dfs and similar" ]
null
null
Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose decimal representation (without leading zeroes) contain only the lucky digits *x* and *y*. For example, if *x*<==<=4, and *y*<==<=7, then numbers 47, 744, 4 are lucky. Let's call a positive integer *a* undoubtedly lucky, if there are such digits *x* and *y* (0<=≀<=*x*,<=*y*<=≀<=9), that the decimal representation of number *a* (without leading zeroes) contains only digits *x* and *y*. Polycarpus has integer *n*. He wants to know how many positive integers that do not exceed *n*, are undoubtedly lucky. Help him, count this number.
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=109) β€” Polycarpus's number.
Print a single integer that says, how many positive integers that do not exceed *n* are undoubtedly lucky.
[ "10\n", "123\n" ]
[ "10\n", "113\n" ]
In the first test sample all numbers that do not exceed 10 are undoubtedly lucky. In the second sample numbers 102, 103, 104, 105, 106, 107, 108, 109, 120, 123 are not undoubtedly lucky.
[ { "input": "10", "output": "10" }, { "input": "123", "output": "113" }, { "input": "1", "output": "1" }, { "input": "2", "output": "2" }, { "input": "1000", "output": "352" }, { "input": "1000000000", "output": "40744" }, { "input": "999999...
2,000
0
0
6,596
49
Sum
[ "math" ]
B. Sum
2
256
Vasya studies positional numeral systems. Unfortunately, he often forgets to write the base of notation in which the expression is written. Once he saw a note in his notebook saying *a*<=+<=*b*<==<=?, and that the base of the positional notation wasn’t written anywhere. Now Vasya has to choose a base *p* and regard the expression as written in the base *p* positional notation. Vasya understood that he can get different results with different bases, and some bases are even invalid. For example, expression 78<=+<=87 in the base 16 positional notation is equal to *FF*16, in the base 15 positional notation it is equal to 11015, in the base 10 one β€” to 16510, in the base 9 one β€” to 1769, and in the base 8 or lesser-based positional notations the expression is invalid as all the numbers should be strictly less than the positional notation base. Vasya got interested in what is the length of the longest possible expression value. Help him to find this length. The length of a number should be understood as the number of numeric characters in it. For example, the length of the longest answer for 78<=+<=87<==<=? is 3. It is calculated like that in the base 15 (11015), base 10 (16510), base 9 (1769) positional notations, for example, and in some other ones.
The first letter contains two space-separated numbers *a* and *b* (1<=≀<=*a*,<=*b*<=≀<=1000) which represent the given summands.
Print a single number β€” the length of the longest answer.
[ "78 87\n", "1 1\n" ]
[ "3\n", "2\n" ]
none
[ { "input": "78 87", "output": "3" }, { "input": "1 1", "output": "2" }, { "input": "9 7", "output": "2" }, { "input": "11 11", "output": "3" }, { "input": "43 21", "output": "3" }, { "input": "84 89", "output": "3" }, { "input": "12 34", ...
248
0
0
6,610
168
Wizards and Demonstration
[ "implementation", "math" ]
null
null
Some country is populated by wizards. They want to organize a demonstration. There are *n* people living in the city, *x* of them are the wizards who will surely go to the demonstration. Other city people (*n*<=-<=*x* people) do not support the wizards and aren't going to go to the demonstration. We know that the city administration will react only to the demonstration involving at least *y* percent of the city people. Having considered the matter, the wizards decided to create clone puppets which can substitute the city people on the demonstration. So all in all, the demonstration will involve only the wizards and their puppets. The city administration cannot tell the difference between a puppet and a person, so, as they calculate the percentage, the administration will consider the city to be consisting of only *n* people and not containing any clone puppets. Help the wizards and find the minimum number of clones to create to that the demonstration had no less than *y* percent of the city people.
The first line contains three space-separated integers, *n*, *x*, *y* (1<=≀<=*n*,<=*x*,<=*y*<=≀<=104,<=*x*<=≀<=*n*) β€” the number of citizens in the city, the number of wizards and the percentage the administration needs, correspondingly. Please note that *y* can exceed 100 percent, that is, the administration wants to see on a demonstration more people that actually live in the city (<=&gt;<=*n*).
Print a single integer β€” the answer to the problem, the minimum number of clones to create, so that the demonstration involved no less than *y* percent of *n* (the real total city population).
[ "10 1 14\n", "20 10 50\n", "1000 352 146\n" ]
[ "1\n", "0\n", "1108\n" ]
In the first sample it is necessary that at least 14% of 10 people came to the demonstration. As the number of people should be integer, then at least two people should come. There is only one wizard living in the city and he is going to come. That isn't enough, so he needs to create one clone. In the second sample 10 people should come to the demonstration. The city has 10 wizards. They will all come to the demonstration, so nobody has to create any clones.
[ { "input": "10 1 14", "output": "1" }, { "input": "20 10 50", "output": "0" }, { "input": "1000 352 146", "output": "1108" }, { "input": "68 65 20", "output": "0" }, { "input": "78 28 27", "output": "0" }, { "input": "78 73 58", "output": "0" }, ...
154
0
3
6,611
526
Om Nom and Dark Park
[ "dfs and similar", "greedy", "implementation" ]
null
null
Om Nom is the main character of a game "Cut the Rope". He is a bright little monster who likes visiting friends living at the other side of the park. However the dark old parks can scare even somebody as fearless as Om Nom, so he asks you to help him. The park consists of 2*n*<=+<=1<=-<=1 squares connected by roads so that the scheme of the park is a full binary tree of depth *n*. More formally, the entrance to the park is located at the square 1. The exits out of the park are located at squares 2*n*,<=2*n*<=+<=1,<=...,<=2*n*<=+<=1<=-<=1 and these exits lead straight to the Om Nom friends' houses. From each square *i* (2<=≀<=*i*<=&lt;<=2*n*<=+<=1) there is a road to the square . Thus, it is possible to go from the park entrance to each of the exits by walking along exactly *n* roads. Om Nom loves counting lights on the way to his friend. Om Nom is afraid of spiders who live in the park, so he doesn't like to walk along roads that are not enough lit. What he wants is that the way to any of his friends should have in total the same number of lights. That will make him feel safe. He asked you to help him install additional lights. Determine what minimum number of lights it is needed to additionally place on the park roads so that a path from the entrance to any exit of the park contains the same number of street lights. You may add an arbitrary number of street lights to each of the roads.
The first line contains integer *n* (1<=≀<=*n*<=≀<=10) β€” the number of roads on the path from the entrance to any exit. The next line contains 2*n*<=+<=1<=-<=2 numbers *a*2,<=*a*3,<=... *a*2*n*<=+<=1<=-<=1 β€” the initial numbers of street lights on each road of the park. Here *a**i* is the number of street lights on the road between squares *i* and . All numbers *a**i* are positive integers, not exceeding 100.
Print the minimum number of street lights that we should add to the roads of the park to make Om Nom feel safe.
[ "2\n1 2 3 4 5 6\n" ]
[ "5\n" ]
Picture for the sample test. Green color denotes the additional street lights.
[ { "input": "2\n1 2 3 4 5 6", "output": "5" }, { "input": "2\n1 2 3 3 2 2", "output": "0" }, { "input": "1\n39 52", "output": "13" }, { "input": "2\n59 96 34 48 8 72", "output": "139" }, { "input": "3\n87 37 91 29 58 45 51 74 70 71 47 38 91 89", "output": "210"...
31
0
0
6,627
14
Four Segments
[ "brute force", "constructive algorithms", "geometry", "implementation", "math" ]
C. Four Segments
2
64
Several months later Alex finally got his brother Bob's creation by post. And now, in his turn, Alex wants to boast about something to his brother. He thought for a while, and came to the conclusion that he has no ready creations, and decided to write a program for rectangles detection. According to his plan, the program detects if the four given segments form a rectangle of a positive area and with sides parallel to coordinate axes. As Alex does badly at school and can't write this program by himself, he asks you to help him.
The input data contain four lines. Each of these lines contains four integers *x*1, *y*1, *x*2, *y*2 (<=-<=109<=≀<=*x*1,<=*y*1,<=*x*2,<=*y*2<=≀<=109) β€” coordinates of segment's beginning and end positions. The given segments can degenerate into points.
Output the word Β«YESΒ», if the given four segments form the required rectangle, otherwise output Β«NOΒ».
[ "1 1 6 1\n1 0 6 0\n6 0 6 1\n1 1 1 0\n", "0 0 0 3\n2 0 0 0\n2 2 2 0\n0 2 2 2\n" ]
[ "YES\n", "NO\n" ]
none
[ { "input": "1 1 6 1\n1 0 6 0\n6 0 6 1\n1 1 1 0", "output": "YES" }, { "input": "0 0 0 3\n2 0 0 0\n2 2 2 0\n0 2 2 2", "output": "NO" }, { "input": "0 0 0 2\n2 0 0 0\n2 2 2 0\n0 2 2 2", "output": "YES" }, { "input": "0 0 10 0\n0 0 10 0\n0 0 0 5\n0 0 0 -5", "output": "NO" ...
62
0
0
6,628
868
Huge Strings
[ "bitmasks", "brute force", "dp", "implementation", "strings" ]
null
null
You are given *n* strings *s*1,<=*s*2,<=...,<=*s**n* consisting of characters 0 and 1. *m* operations are performed, on each of them you concatenate two existing strings into a new one. On the *i*-th operation the concatenation *s**a**i**s**b**i* is saved into a new string *s**n*<=+<=*i* (the operations are numbered starting from 1). After each operation you need to find the maximum positive integer *k* such that all possible strings consisting of 0 and 1 of length *k* (there are 2*k* such strings) are substrings of the new string. If there is no such *k*, print 0.
The first line contains single integer *n* (1<=≀<=*n*<=≀<=100)Β β€” the number of strings. The next *n* lines contain strings *s*1,<=*s*2,<=...,<=*s**n* (1<=≀<=|*s**i*|<=≀<=100), one per line. The total length of strings is not greater than 100. The next line contains single integer *m* (1<=≀<=*m*<=≀<=100)Β β€” the number of operations. *m* lines follow, each of them contains two integers *a**i* abd *b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*<=+<=*i*<=-<=1)Β β€” the number of strings that are concatenated to form *s**n*<=+<=*i*.
Print *m* lines, each should contain one integerΒ β€” the answer to the question after the corresponding operation.
[ "5\n01\n10\n101\n11111\n0\n3\n1 2\n6 5\n4 4\n" ]
[ "1\n2\n0\n" ]
On the first operation, a new string "0110" is created. For *k* = 1 the two possible binary strings of length *k* are "0" and "1", they are substrings of the new string. For *k* = 2 and greater there exist strings of length *k* that do not appear in this string (for *k* = 2 such string is "00"). So the answer is 1. On the second operation the string "01100" is created. Now all strings of length *k* = 2 are present. On the third operation the string "1111111111" is created. There is no zero, so the answer is 0.
[ { "input": "5\n01\n10\n101\n11111\n0\n3\n1 2\n6 5\n4 4", "output": "1\n2\n0" }, { "input": "5\n01\n1\n0011\n0\n01\n6\n5 5\n3 2\n4 2\n6 7\n5 1\n9 7", "output": "1\n1\n1\n2\n1\n2" }, { "input": "5\n111101000111100011100110000100\n000111001\n01101000\n000011010010001001100100000001010010011...
810
268,390,400
0
6,630
999
Cards and Joy
[ "dp" ]
null
null
There are $n$ players sitting at the card table. Each player has a favorite number. The favorite number of the $j$-th player is $f_j$. There are $k \cdot n$ cards on the table. Each card contains a single integer: the $i$-th card contains number $c_i$. Also, you are given a sequence $h_1, h_2, \dots, h_k$. Its meaning will be explained below. The players have to distribute all the cards in such a way that each of them will hold exactly $k$ cards. After all the cards are distributed, each player counts the number of cards he has that contains his favorite number. The joy level of a player equals $h_t$ if the player holds $t$ cards containing his favorite number. If a player gets no cards with his favorite number (i.e., $t=0$), his joy level is $0$. Print the maximum possible total joy levels of the players after the cards are distributed. Note that the sequence $h_1, \dots, h_k$ is the same for all the players.
The first line of input contains two integers $n$ and $k$ ($1 \le n \le 500, 1 \le k \le 10$) β€” the number of players and the number of cards each player will get. The second line contains $k \cdot n$ integers $c_1, c_2, \dots, c_{k \cdot n}$ ($1 \le c_i \le 10^5$) β€” the numbers written on the cards. The third line contains $n$ integers $f_1, f_2, \dots, f_n$ ($1 \le f_j \le 10^5$) β€” the favorite numbers of the players. The fourth line contains $k$ integers $h_1, h_2, \dots, h_k$ ($1 \le h_t \le 10^5$), where $h_t$ is the joy level of a player if he gets exactly $t$ cards with his favorite number written on them. It is guaranteed that the condition $h_{t - 1} &lt; h_t$ holds for each $t \in [2..k]$.
Print one integer β€” the maximum possible total joy levels of the players among all possible card distributions.
[ "4 3\n1 3 2 8 5 5 8 2 2 8 5 2\n1 2 2 5\n2 6 7\n", "3 3\n9 9 9 9 9 9 9 9 9\n1 2 3\n1 2 3\n" ]
[ "21\n", "0\n" ]
In the first example, one possible optimal card distribution is the following: - Player $1$ gets cards with numbers $[1, 3, 8]$; - Player $2$ gets cards with numbers $[2, 2, 8]$; - Player $3$ gets cards with numbers $[2, 2, 8]$; - Player $4$ gets cards with numbers $[5, 5, 5]$. Thus, the answer is $2 + 6 + 6 + 7 = 21$. In the second example, no player can get a card with his favorite number. Thus, the answer is $0$.
[ { "input": "4 3\n1 3 2 8 5 5 8 2 2 8 5 2\n1 2 2 5\n2 6 7", "output": "21" }, { "input": "3 3\n9 9 9 9 9 9 9 9 9\n1 2 3\n1 2 3", "output": "0" }, { "input": "1 1\n1\n2\n1", "output": "0" }, { "input": "1 1\n1\n1\n1", "output": "1" }, { "input": "1 1\n1\n1\n100000",...
2,000
63,385,600
0
6,635
374
Inna and Nine
[ "combinatorics", "greedy" ]
null
null
Inna loves digit 9 very much. That's why she asked Dima to write a small number consisting of nines. But Dima must have misunderstood her and he wrote a very large number *a*, consisting of digits from 1 to 9. Inna wants to slightly alter the number Dima wrote so that in the end the number contained as many digits nine as possible. In one move, Inna can choose two adjacent digits in a number which sum equals 9 and replace them by a single digit 9. For instance, Inna can alter number 14545181 like this: 14545181<=β†’<=1945181<=β†’<=194519<=β†’<=19919. Also, she can use this method to transform number 14545181 into number 19991. Inna will not transform it into 149591 as she can get numbers 19919 and 19991 which contain more digits nine. Dima is a programmer so he wants to find out how many distinct numbers containing as many digits nine as possible Inna can get from the written number. Help him with this challenging task.
The first line of the input contains integer *a* (1<=≀<=*a*<=≀<=10100000). Number *a* doesn't have any zeroes.
In a single line print a single integer β€” the answer to the problem. It is guaranteed that the answer to the problem doesn't exceed 263<=-<=1. 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.
[ "369727\n", "123456789987654321\n", "1\n" ]
[ "2\n", "1\n", "1\n" ]
Notes to the samples In the first sample Inna can get the following numbers: 369727 → 99727 → 9997, 369727 → 99727 → 9979. In the second sample, Inna can act like this: 123456789987654321 → 12396789987654321 → 1239678998769321.
[ { "input": "369727", "output": "2" }, { "input": "123456789987654321", "output": "1" }, { "input": "1", "output": "1" }, { "input": "3636363636363454545454543636363636454545452727272727218181818181999111777", "output": "1512" }, { "input": "1188", "output": "1...
655
11,264,000
3
6,637
877
Slava and tanks
[ "constructive algorithms" ]
null
null
Slava plays his favorite game "Peace Lightning". Now he is flying a bomber on a very specific map. Formally, map is a checkered field of size 1<=Γ—<=*n*, the cells of which are numbered from 1 to *n*, in each cell there can be one or several tanks. Slava doesn't know the number of tanks and their positions, because he flies very high, but he can drop a bomb in any cell. All tanks in this cell will be damaged. If a tank takes damage for the first time, it instantly moves to one of the neighboring cells (a tank in the cell *n* can only move to the cell *n*<=-<=1, a tank in the cell 1 can only move to the cell 2). If a tank takes damage for the second time, it's counted as destroyed and never moves again. The tanks move only when they are damaged for the first time, they do not move by themselves. Help Slava to destroy all tanks using as few bombs as possible.
The first line contains a single integer *n* (2<=≀<=*n*<=≀<=100<=000) β€” the size of the map.
In the first line print *m* β€” the minimum number of bombs Slava needs to destroy all tanks. In the second line print *m* integers *k*1,<=*k*2,<=...,<=*k**m*. The number *k**i* means that the *i*-th bomb should be dropped at the cell *k**i*. If there are multiple answers, you can print any of them.
[ "2\n", "3\n" ]
[ "3\n2 1 2 ", "4\n2 1 3 2 " ]
none
[ { "input": "2", "output": "3\n2 1 2 " }, { "input": "3", "output": "4\n2 1 3 2 " }, { "input": "4", "output": "6\n2 4 1 3 2 4 " }, { "input": "6", "output": "9\n2 4 6 1 3 5 2 4 6 " }, { "input": "10", "output": "15\n2 4 6 8 10 1 3 5 7 9 2 4 6 8 10 " }, { ...
62
0
0
6,638
23
Tree
[ "dp" ]
E. Tree
2
256
Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree.
The first input line contains integer number *n* (1<=≀<=*n*<=≀<=700) β€” amount of vertices in the tree. The following *n*<=-<=1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, *a**i*, *b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*). It's guaranteed that the graph described in the input is a tree.
Output the only number β€” the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges.
[ "5\n1 2\n2 3\n3 4\n4 5\n", "8\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n6 8\n", "3\n1 2\n1 3\n" ]
[ "6", "18", "3" ]
none
[ { "input": "5\n1 2\n2 3\n3 4\n4 5", "output": "6" }, { "input": "8\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n6 8", "output": "18" }, { "input": "3\n1 2\n1 3", "output": "3" }, { "input": "5\n3 2\n1 5\n4 5\n5 3", "output": "6" }, { "input": "5\n2 1\n3 4\n3 5\n5 2", "outpu...
0
0
-1
6,651
746
Green and Black Tea
[ "constructive algorithms", "greedy", "math" ]
null
null
Innokentiy likes tea very much and today he wants to drink exactly *n* cups of tea. He would be happy to drink more but he had exactly *n* tea bags, *a* of them are green and *b* are black. Innokentiy doesn't like to drink the same tea (green or black) more than *k* times in a row. Your task is to determine the order of brewing tea bags so that Innokentiy will be able to drink *n* cups of tea, without drinking the same tea more than *k* times in a row, or to inform that it is impossible. Each tea bag has to be used exactly once.
The first line contains four integers *n*, *k*, *a* and *b* (1<=≀<=*k*<=≀<=*n*<=≀<=105, 0<=≀<=*a*,<=*b*<=≀<=*n*)Β β€” the number of cups of tea Innokentiy wants to drink, the maximum number of cups of same tea he can drink in a row, the number of tea bags of green and black tea. It is guaranteed that *a*<=+<=*b*<==<=*n*.
If it is impossible to drink *n* cups of tea, print "NO" (without quotes). Otherwise, print the string of the length *n*, which consists of characters 'G' and 'B'. If some character equals 'G', then the corresponding cup of tea should be green. If some character equals 'B', then the corresponding cup of tea should be black. If there are multiple answers, print any of them.
[ "5 1 3 2\n", "7 2 2 5\n", "4 3 4 0\n" ]
[ "GBGBG\n", "BBGBGBB", "NO\n" ]
none
[ { "input": "5 1 3 2", "output": "GBGBG" }, { "input": "7 2 2 5", "output": "BBGBBGB" }, { "input": "4 3 4 0", "output": "NO" }, { "input": "2 2 0 2", "output": "BB" }, { "input": "3 2 0 3", "output": "NO" }, { "input": "1 1 0 1", "output": "B" },...
92
2,764,800
0
6,657
228
Zigzag
[ "data structures" ]
null
null
The court wizard Zigzag wants to become a famous mathematician. For that, he needs his own theorem, like the Cauchy theorem, or his sum, like the Minkowski sum. But most of all he wants to have his sequence, like the Fibonacci sequence, and his function, like the Euler's totient function. The Zigag's sequence with the zigzag factor z is an infinite sequence *S**i**z* (*i*<=β‰₯<=1;Β *z*<=β‰₯<=2), that is determined as follows: - *S**i**z*<==<=2, when ; - , when ; - , when . Operation means taking the remainder from dividing number *x* by number *y*. For example, the beginning of sequence *S**i*3 (zigzag factor 3) looks as follows: 1, 2, 3, 2, 1, 2, 3, 2, 1. Let's assume that we are given an array *a*, consisting of *n* integers. Let's define element number *i* (1<=≀<=*i*<=≀<=*n*) of the array as *a**i*. The Zigzag function is function , where *l*,<=*r*,<=*z* satisfy the inequalities 1<=≀<=*l*<=≀<=*r*<=≀<=*n*, *z*<=β‰₯<=2. To become better acquainted with the Zigzag sequence and the Zigzag function, the wizard offers you to implement the following operations on the given array *a*. 1. The assignment operation. The operation parameters are (*p*,<=*v*). The operation denotes assigning value *v* to the *p*-th array element. After the operation is applied, the value of the array element *a**p* equals *v*. 1. The Zigzag operation. The operation parameters are (*l*,<=*r*,<=*z*). The operation denotes calculating the Zigzag function *Z*(*l*,<=*r*,<=*z*). Explore the magical powers of zigzags, implement the described operations.
The first line contains integer *n* (1<=≀<=*n*<=≀<=105) β€” The number of elements in array *a*. The second line contains *n* space-separated integers: *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109) β€” the elements of the array. The third line contains integer *m* (1<=≀<=*m*<=≀<=105) β€” the number of operations. Next *m* lines contain the operations' descriptions. An operation's description starts with integer *t**i* (1<=≀<=*t**i*<=≀<=2) β€” the operation type. - If *t**i*<==<=1 (assignment operation), then on the line follow two space-separated integers: *p**i*,<=*v**i* (1<=≀<=*p**i*<=≀<=*n*;Β 1<=≀<=*v**i*<=≀<=109) β€” the parameters of the assigning operation. - If *t**i*<==<=2 (Zigzag operation), then on the line follow three space-separated integers: *l**i*,<=*r**i*,<=*z**i* (1<=≀<=*l**i*<=≀<=*r**i*<=≀<=*n*;Β 2<=≀<=*z**i*<=≀<=6) β€” the parameters of the Zigzag operation. You should execute the operations in the order, in which they are given in the input.
For each Zigzag operation print the calculated value of the Zigzag function on a single line. Print the values for Zigzag functions in the order, in which they are given in the input. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
[ "5\n2 3 1 5 5\n4\n2 2 3 2\n2 1 5 3\n1 3 5\n2 1 5 3\n" ]
[ "5\n26\n38\n" ]
Explanation of the sample test: - Result of the first operation is *Z*(2, 3, 2) = 3Β·1 + 1Β·2 = 5. - Result of the second operation is *Z*(1, 5, 3) = 2Β·1 + 3Β·2 + 1Β·3 + 5Β·2 + 5Β·1 = 26. - After the third operation array *a* is equal to 2, 3, 5, 5, 5. - Result of the forth operation is *Z*(1, 5, 3) = 2Β·1 + 3Β·2 + 5Β·3 + 5Β·2 + 5Β·1 = 38.
[]
92
0
0
6,660
761
Dasha and Puzzle
[ "constructive algorithms", "dfs and similar", "graphs", "greedy", "trees" ]
null
null
Dasha decided to have a rest after solving the problem. She had been ready to start her favourite activity β€” origami, but remembered the puzzle that she could not solve. The tree is a non-oriented connected graph without cycles. In particular, there always are *n*<=-<=1 edges in a tree with *n* vertices. The puzzle is to position the vertices at the points of the Cartesian plane with integral coordinates, so that the segments between the vertices connected by edges are parallel to the coordinate axes. Also, the intersection of segments is allowed only at their ends. Distinct vertices should be placed at different points. Help Dasha to find any suitable way to position the tree vertices on the plane. It is guaranteed that if it is possible to position the tree vertices on the plane without violating the condition which is given above, then you can do it by using points with integral coordinates which don't exceed 1018 in absolute value.
The first line contains single integer *n* (1<=≀<=*n*<=≀<=30) β€” the number of vertices in the tree. Each of next *n*<=-<=1 lines contains two integers *u**i*, *v**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*) that mean that the *i*-th edge of the tree connects vertices *u**i* and *v**i*. It is guaranteed that the described graph is a tree.
If the puzzle doesn't have a solution then in the only line print "NO". Otherwise, the first line should contain "YES". The next *n* lines should contain the pair of integers *x**i*, *y**i* (|*x**i*|,<=|*y**i*|<=≀<=1018) β€” the coordinates of the point which corresponds to the *i*-th vertex of the tree. If there are several solutions, print any of them.
[ "7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7", "6\n1 2\n2 3\n2 4\n2 5\n2 6\n", "4\n1 2\n2 3\n3 4" ]
[ "YES\n0 0\n1 0\n0 1\n2 0\n1 -1\n-1 1\n0 2", "NO\n", "YES\n3 3\n4 3\n5 3\n6 3" ]
In the first sample one of the possible positions of tree is: <img class="tex-graphics" src="https://espresso.codeforces.com/360c9c903e5177970a5a3b2711f6718c2163ffd8.png" style="max-width: 100.0%;max-height: 100.0%;"/>
[ { "input": "7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7", "output": "YES\n0 0\n0 1073741824\n1073741824 0\n0 1610612736\n536870912 1073741824\n1073741824 536870912\n1610612736 0" }, { "input": "6\n1 2\n2 3\n2 4\n2 5\n2 6", "output": "NO" }, { "input": "4\n1 2\n2 3\n3 4", "output": "YES\n0 0\n0 1...
155
409,600
0
6,664
79
Bus Game
[ "greedy" ]
A. Bus Game
2
256
After Fox Ciel won an onsite round of a programming contest, she took a bus to return to her castle. The fee of the bus was 220 yen. She met Rabbit Hanako in the bus. They decided to play the following game because they got bored in the bus. - Initially, there is a pile that contains *x* 100-yen coins and *y* 10-yen coins. - They take turns alternatively. Ciel takes the first turn. - In each turn, they must take exactly 220 yen from the pile. In Ciel's turn, if there are multiple ways to take 220 yen, she will choose the way that contains the maximal number of 100-yen coins. In Hanako's turn, if there are multiple ways to take 220 yen, she will choose the way that contains the maximal number of 10-yen coins. - If Ciel or Hanako can't take exactly 220 yen from the pile, she loses. Determine the winner of the game.
The first line contains two integers *x* (0<=≀<=*x*<=≀<=106) and *y* (0<=≀<=*y*<=≀<=106), separated by a single space.
If Ciel wins, print "Ciel". Otherwise, print "Hanako".
[ "2 2\n", "3 22\n" ]
[ "Ciel\n", "Hanako\n" ]
In the first turn (Ciel's turn), she will choose 2 100-yen coins and 2 10-yen coins. In the second turn (Hanako's turn), she will choose 1 100-yen coin and 12 10-yen coins. In the third turn (Ciel's turn), she can't pay exactly 220 yen, so Ciel will lose.
[ { "input": "2 2", "output": "Ciel" }, { "input": "3 22", "output": "Hanako" }, { "input": "0 22", "output": "Ciel" }, { "input": "1000 1000", "output": "Ciel" }, { "input": "0 0", "output": "Hanako" }, { "input": "0 21", "output": "Hanako" }, {...
280
2,867,200
3.924659
6,668
981
Businessmen Problems
[ "sortings" ]
null
null
Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies. In order to avoid this representatives of both companies decided to make an agreement on the sets the companies should present. The sets should be chosen in the way that maximizes the total income of the companies. All elements are enumerated with integers. The ChemForces company has discovered $n$ distinct chemical elements with indices $a_1, a_2, \ldots, a_n$, and will get an income of $x_i$ Berland rubles if the $i$-th element from this list is in the set of this company. The TopChemist company discovered $m$ distinct chemical elements with indices $b_1, b_2, \ldots, b_m$, and it will get an income of $y_j$ Berland rubles for including the $j$-th element from this list to its set. In other words, the first company can present any subset of elements from $\{a_1, a_2, \ldots, a_n\}$ (possibly empty subset), the second company can present any subset of elements from $\{b_1, b_2, \ldots, b_m\}$ (possibly empty subset). There shouldn't be equal elements in the subsets. Help the representatives select the sets in such a way that no element is presented in both sets and the total income is the maximum possible.
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) Β β€” the number of elements discovered by ChemForces. The $i$-th of the next $n$ lines contains two integers $a_i$ and $x_i$ ($1 \leq a_i \leq 10^9$, $1 \leq x_i \leq 10^9$) Β β€” the index of the $i$-th element and the income of its usage on the exhibition. It is guaranteed that all $a_i$ are distinct. The next line contains a single integer $m$ ($1 \leq m \leq 10^5$) Β β€” the number of chemicals invented by TopChemist. The $j$-th of the next $m$ lines contains two integers $b_j$ and $y_j$, ($1 \leq b_j \leq 10^9$, $1 \leq y_j \leq 10^9$) Β β€” the index of the $j$-th element and the income of its usage on the exhibition. It is guaranteed that all $b_j$ are distinct.
Print the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets.
[ "3\n1 2\n7 2\n3 10\n4\n1 4\n2 4\n3 4\n4 4\n", "1\n1000000000 239\n3\n14 15\n92 65\n35 89\n" ]
[ "24\n", "408\n" ]
In the first example ChemForces can choose the set ($3, 7$), while TopChemist can choose ($1, 2, 4$). This way the total income is $(10 + 2) + (4 + 4 + 4) = 24$. In the second example ChemForces can choose the only element $10^9$, while TopChemist can choose ($14, 92, 35$). This way the total income is $(239) + (15 + 65 + 89) = 408$.
[ { "input": "3\n1 2\n7 2\n3 10\n4\n1 4\n2 4\n3 4\n4 4", "output": "24" }, { "input": "1\n1000000000 239\n3\n14 15\n92 65\n35 89", "output": "408" }, { "input": "10\n598654597 488228616\n544064902 21923894\n329635457 980089248\n988262691 654502493\n967529230 543358150\n835120075 128123793\...
1,263
22,016,000
3
6,675
931
Laboratory Work
[ "implementation", "math" ]
null
null
Anya and Kirill are doing a physics laboratory work. In one of the tasks they have to measure some value *n* times, and then compute the average value to lower the error. Kirill has already made his measurements, and has got the following integer values: *x*1, *x*2, ..., *x**n*. It is important that the values are close to each other, namely, the difference between the maximum value and the minimum value is at most 2. Anya does not want to make the measurements, however, she can't just copy the values from Kirill's work, because the error of each measurement is a random value, and this coincidence will be noted by the teacher. Anya wants to write such integer values *y*1, *y*2, ..., *y**n* in her work, that the following conditions are met: - the average value of *x*1,<=*x*2,<=...,<=*x**n* is equal to the average value of *y*1,<=*y*2,<=...,<=*y**n*;- all Anya's measurements are in the same bounds as all Kirill's measurements, that is, the maximum value among Anya's values is not greater than the maximum value among Kirill's values, and the minimum value among Anya's values is not less than the minimum value among Kirill's values;- the number of equal measurements in Anya's work and Kirill's work is as small as possible among options with the previous conditions met. Formally, the teacher goes through all Anya's values one by one, if there is equal value in Kirill's work and it is not strike off yet, he strikes off this Anya's value and one of equal values in Kirill's work. The number of equal measurements is then the total number of strike off values in Anya's work. Help Anya to write such a set of measurements that the conditions above are met.
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=100<=000) β€” the numeber of measurements made by Kirill. The second line contains a sequence of integers *x*1,<=*x*2,<=...,<=*x**n* (<=-<=100<=000<=≀<=*x**i*<=≀<=100<=000) β€” the measurements made by Kirill. It is guaranteed that the difference between the maximum and minimum values among values *x*1,<=*x*2,<=...,<=*x**n* does not exceed 2.
In the first line print the minimum possible number of equal measurements. In the second line print *n* integers *y*1,<=*y*2,<=...,<=*y**n* β€” the values Anya should write. You can print the integers in arbitrary order. Keep in mind that the minimum value among Anya's values should be not less that the minimum among Kirill's values, and the maximum among Anya's values should be not greater than the maximum among Kirill's values. If there are multiple answers, print any of them.
[ "6\n-1 1 1 0 0 -1\n", "3\n100 100 101\n", "7\n-10 -9 -10 -8 -10 -9 -9\n" ]
[ "2\n0 0 0 0 0 0 \n", "3\n101 100 100 \n", "5\n-10 -10 -9 -9 -9 -9 -9 \n" ]
In the first example Anya can write zeros as here measurements results. The average value is then equal to the average value of Kirill's values, and there are only two equal measurements. In the second example Anya should write two values 100 and one value 101 (in any order), because it is the only possibility to make the average be the equal to the average of Kirill's values. Thus, all three measurements are equal. In the third example the number of equal measurements is 5.
[ { "input": "6\n-1 1 1 0 0 -1", "output": "2\n0 0 0 0 0 0 " }, { "input": "3\n100 100 101", "output": "3\n101 100 100 " }, { "input": "7\n-10 -9 -10 -8 -10 -9 -9", "output": "5\n-10 -10 -9 -9 -9 -9 -9 " }, { "input": "60\n-8536 -8536 -8536 -8535 -8536 -8536 -8536 -8536 -8536 -...
295
12,697,600
0
6,681
83
Magical Array
[ "math" ]
A. Magical Array
2
256
Valery is very interested in magic. Magic attracts him so much that he sees it everywhere. He explains any strange and weird phenomenon through intervention of supernatural forces. But who would have thought that even in a regular array of numbers Valera manages to see something beautiful and magical. Valera absolutely accidentally got a piece of ancient parchment on which an array of numbers was written. He immediately thought that the numbers in this array were not random. As a result of extensive research Valera worked out a wonderful property that a magical array should have: an array is defined as magic if its minimum and maximum coincide. He decided to share this outstanding discovery with you, but he asks you for help in return. Despite the tremendous intelligence and wit, Valera counts very badly and so you will have to complete his work. All you have to do is count the number of magical subarrays of the original array of numbers, written on the parchment. Subarray is defined as non-empty sequence of consecutive elements.
The first line of the input data contains an integer *n* (1<=≀<=*n*<=≀<=105). The second line contains an array of original integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=109<=≀<=*a**i*<=≀<=109).
Print on the single line the answer to the problem: the amount of subarrays, which are magical. Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator).
[ "4\n2 1 1 4\n", "5\n-2 -2 -2 0 1\n" ]
[ "5\n", "8\n" ]
Notes to sample tests: Magical subarrays are shown with pairs of indices [a;b] of the beginning and the end. In the first sample: [1;1], [2;2], [3;3], [4;4], [2;3]. In the second sample: [1;1], [2;2], [3;3], [4;4], [5;5], [1;2], [2;3], [1;3].
[ { "input": "4\n2 1 1 4", "output": "5" }, { "input": "5\n-2 -2 -2 0 1", "output": "8" }, { "input": "1\n10", "output": "1" }, { "input": "2\n5 6", "output": "2" }, { "input": "5\n5 5 4 5 5", "output": "7" }, { "input": "8\n1 2 0 0 0 0 3 3", "output...
92
0
0
6,683
0
none
[ "none" ]
null
null
Bear Limak prepares problems for a programming competition. Of course, it would be unprofessional to mention the sponsor name in the statement. Limak takes it seriously and he is going to change some words. To make it still possible to read, he will try to modify each word as little as possible. Limak has a string *s* that consists of uppercase English letters. In one move he can swap two adjacent letters of the string. For example, he can transform a string "ABBC" into "BABC" or "ABCB" in one move. Limak wants to obtain a string without a substring "VK" (i.e. there should be no letter 'V' immediately followed by letter 'K'). It can be easily proved that it's possible for any initial string *s*. What is the minimum possible number of moves Limak can do?
The first line of the input contains an integer *n* (1<=≀<=*n*<=≀<=75)Β β€” the length of the string. The second line contains a string *s*, consisting of uppercase English letters. The length of the string is equal to *n*.
Print one integer, denoting the minimum possible number of moves Limak can do, in order to obtain a string without a substring "VK".
[ "4\nVKVK\n", "5\nBVVKV\n", "7\nVVKEVKK\n", "20\nVKVKVVVKVOVKVQKKKVVK\n", "5\nLIMAK\n" ]
[ "3\n", "2\n", "3\n", "8\n", "0\n" ]
In the first sample, the initial string is "VKVK". The minimum possible number of moves is 3. One optimal sequence of moves is: 1. Swap two last letters. The string becomes "VKKV".1. Swap first two letters. The string becomes "KVKV".1. Swap the second and the third letter. The string becomes "KKVV". Indeed, this string doesn't have a substring "VK". In the second sample, there are two optimal sequences of moves. One is "BVVKV"  →  "VBVKV"  →  "VVBKV". The other is "BVVKV"  →  "BVKVV"  →  "BKVVV". In the fifth sample, no swaps are necessary.
[]
46
0
0
6,691
159
Matchmaker
[ "*special", "greedy", "sortings" ]
null
null
Polycarpus has *n* markers and *m* marker caps. Each marker is described by two numbers: *x**i* is the color and *y**i* is the diameter. Correspondingly, each cap is described by two numbers: *a**j* is the color and *b**j* is the diameter. Cap (*a**j*,<=*b**j*) can close marker (*x**i*,<=*y**i*) only if their diameters match, that is, *b**j*<==<=*y**i*. Besides, a marker is considered to be beautifully closed, if the cap color and the marker color match, that is, *a**j*<==<=*x**i*. Find the way to close the maximum number of markers. If there are several such ways, then choose the one that has the maximum number of beautifully closed markers.
The first input line contains two space-separated integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=105) β€” the number of markers and the number of caps, correspondingly. Next *n* lines describe the markers. The *i*-th line contains two space-separated integers *x**i*, *y**i* (1<=≀<=*x**i*,<=*y**i*<=≀<=1000) β€” the *i*-th marker's color and diameter, correspondingly. Next *m* lines describe the caps. The *j*-th line contains two space-separated integers *a**j*, *b**j* (1<=≀<=*a**j*,<=*b**j*<=≀<=1000) β€” the color and diameter of the *j*-th cap, correspondingly.
Print two space-separated integers *u*,<=*v*, where *u* is the number of closed markers and *v* is the number of beautifully closed markers in the sought optimal way. Remember that you have to find the way to close the maximum number of markers, and if there are several such ways, you should choose the one where the number of beautifully closed markers is maximum.
[ "3 4\n1 2\n3 4\n2 4\n5 4\n2 4\n1 1\n1 2\n", "2 2\n1 2\n2 1\n3 4\n5 1\n" ]
[ "3 2\n", "1 0\n" ]
In the first test sample the first marker should be closed by the fourth cap, the second marker should be closed by the first cap and the third marker should be closed by the second cap. Thus, three markers will be closed, and two of them will be beautifully closed β€” the first and the third markers.
[ { "input": "3 4\n1 2\n3 4\n2 4\n5 4\n2 4\n1 1\n1 2", "output": "3 2" }, { "input": "2 2\n1 2\n2 1\n3 4\n5 1", "output": "1 0" }, { "input": "6 7\n2 1\n2 2\n2 1\n1 1\n2 1\n1 2\n2 2\n2 2\n2 2\n1 2\n2 2\n1 1\n1 2", "output": "3 3" }, { "input": "6 7\n2 1\n1 1\n2 2\n1 2\n1 1\n1 2...
3,000
10,444,800
0
6,696
699
One Bomb
[ "implementation" ]
null
null
You are given a description of a depot. It is a rectangular checkered field of *n*<=Γ—<=*m* size. Each cell in a field can be empty (".") or it can be occupied by a wall ("*"). You have one bomb. If you lay the bomb at the cell (*x*,<=*y*), then after triggering it will wipe out all walls in the row *x* and all walls in the column *y*. You are to determine if it is possible to wipe out all walls in the depot by placing and triggering exactly one bomb. The bomb can be laid both in an empty cell or in a cell occupied by a wall.
The first line contains two positive integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=1000)Β β€” the number of rows and columns in the depot field. The next *n* lines contain *m* symbols "." and "*" eachΒ β€” the description of the field. *j*-th symbol in *i*-th of them stands for cell (*i*,<=*j*). If the symbol is equal to ".", then the corresponding cell is empty, otherwise it equals "*" and the corresponding cell is occupied by a wall.
If it is impossible to wipe out all walls by placing and triggering exactly one bomb, then print "NO" in the first line (without quotes). Otherwise print "YES" (without quotes) in the first line and two integers in the second lineΒ β€” the coordinates of the cell at which the bomb should be laid. If there are multiple answers, print any of them.
[ "3 4\n.*..\n....\n.*..\n", "3 3\n..*\n.*.\n*..\n", "6 5\n..*..\n..*..\n*****\n..*..\n..*..\n..*..\n" ]
[ "YES\n1 2\n", "NO\n", "YES\n3 3\n" ]
none
[ { "input": "3 4\n.*..\n....\n.*..", "output": "YES\n1 2" }, { "input": "3 3\n..*\n.*.\n*..", "output": "NO" }, { "input": "6 5\n..*..\n..*..\n*****\n..*..\n..*..\n..*..", "output": "YES\n3 3" }, { "input": "1 10\n**********", "output": "YES\n1 1" }, { "input": "10...
46
0
0
6,701
519
A and B and Lecture Rooms
[ "binary search", "data structures", "dfs and similar", "dp", "trees" ]
null
null
A and B are preparing themselves for programming contests. The University where A and B study is a set of rooms connected by corridors. Overall, the University has *n* rooms connected by *n*<=-<=1 corridors so that you can get from any room to any other one by moving along the corridors. The rooms are numbered from 1 to *n*. Every day А and B write contests in some rooms of their university, and after each contest they gather together in the same room and discuss problems. A and B want the distance from the rooms where problems are discussed to the rooms where contests are written to be equal. The distance between two rooms is the number of edges on the shortest path between them. As they write contests in new rooms every day, they asked you to help them find the number of possible rooms to discuss problems for each of the following *m* days.
The first line contains integer *n* (1<=≀<=*n*<=≀<=105) β€” the number of rooms in the University. The next *n*<=-<=1 lines describe the corridors. The *i*-th of these lines (1<=≀<=*i*<=≀<=*n*<=-<=1) contains two integers *a**i* and *b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*), showing that the *i*-th corridor connects rooms *a**i* and *b**i*. The next line contains integer *m* (1<=≀<=*m*<=≀<=105) β€” the number of queries. Next *m* lines describe the queries. The *j*-th of these lines (1<=≀<=*j*<=≀<=*m*) contains two integers *x**j* and *y**j* (1<=≀<=*x**j*,<=*y**j*<=≀<=*n*) that means that on the *j*-th day A will write the contest in the room *x**j*, B will write in the room *y**j*.
In the *i*-th (1<=≀<=*i*<=≀<=*m*) line print the number of rooms that are equidistant from the rooms where A and B write contest on the *i*-th day.
[ "4\n1 2\n1 3\n2 4\n1\n2 3\n", "4\n1 2\n2 3\n2 4\n2\n1 2\n1 3\n" ]
[ "1\n", "0\n2\n" ]
in the first sample there is only one room at the same distance from rooms number 2 and 3 β€” room number 1.
[ { "input": "4\n1 2\n1 3\n2 4\n1\n2 3", "output": "1" }, { "input": "4\n1 2\n2 3\n2 4\n2\n1 2\n1 3", "output": "0\n2" }, { "input": "15\n1 2\n1 3\n1 4\n2 5\n2 6\n2 7\n5 8\n6 9\n9 14\n14 15\n7 10\n4 13\n3 11\n3 12\n6\n10 15\n13 12\n2 15\n8 4\n15 12\n6 13", "output": "1\n10\n1\n7\n0\n4"...
2,000
14,336,000
0
6,735
272
Dima and Two Sequences
[ "combinatorics", "math", "sortings" ]
null
null
Little Dima has two sequences of points with integer coordinates: sequence (*a*1,<=1),<=(*a*2,<=2),<=...,<=(*a**n*,<=*n*) and sequence (*b*1,<=1),<=(*b*2,<=2),<=...,<=(*b**n*,<=*n*). Now Dima wants to count the number of distinct sequences of points of length 2Β·*n* that can be assembled from these sequences, such that the *x*-coordinates of points in the assembled sequence will not decrease. Help him with that. Note that each element of the initial sequences should be used exactly once in the assembled sequence. Dima considers two assembled sequences (*p*1,<=*q*1),<=(*p*2,<=*q*2),<=...,<=(*p*2Β·*n*,<=*q*2Β·*n*) and (*x*1,<=*y*1),<=(*x*2,<=*y*2),<=...,<=(*x*2Β·*n*,<=*y*2Β·*n*) distinct, if there is such *i* (1<=≀<=*i*<=≀<=2Β·*n*), that (*p**i*,<=*q**i*)<=β‰ <=(*x**i*,<=*y**i*). As the answer can be rather large, print the remainder from dividing the answer by number *m*.
The first line contains integer *n* (1<=≀<=*n*<=≀<=105). The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109). The third line contains *n* integers *b*1,<=*b*2,<=...,<=*b**n* (1<=≀<=*b**i*<=≀<=109). The numbers in the lines are separated by spaces. The last line contains integer *m* (2<=≀<=*m*<=≀<=109<=+<=7).
In the single line print the remainder after dividing the answer to the problem by number *m*.
[ "1\n1\n2\n7\n", "2\n1 2\n2 3\n11\n" ]
[ "1\n", "2\n" ]
In the first sample you can get only one sequence: (1, 1), (2, 1). In the second sample you can get such sequences : (1, 1), (2, 2), (2, 1), (3, 2); (1, 1), (2, 1), (2, 2), (3, 2). Thus, the answer is 2.
[ { "input": "1\n1\n2\n7", "output": "1" }, { "input": "2\n1 2\n2 3\n11", "output": "2" }, { "input": "100\n1 8 10 6 5 3 2 3 4 2 3 7 1 1 5 1 4 1 8 1 5 5 6 5 3 7 4 5 5 3 8 7 8 6 8 9 10 7 8 5 8 9 1 3 7 2 6 1 7 7 2 8 1 5 4 2 10 4 9 8 1 10 1 5 9 8 1 9 5 1 5 7 1 6 7 8 8 2 2 3 3 7 2 10 6 3 6 3 5...
778
47,718,400
3
6,749
229
Shifts
[ "brute force", "two pointers" ]
null
null
You are given a table consisting of *n* rows and *m* columns. Each cell of the table contains a number, 0 or 1. In one move we can choose some row of the table and cyclically shift its values either one cell to the left, or one cell to the right. To cyclically shift a table row one cell to the right means to move the value of each cell, except for the last one, to the right neighboring cell, and to move the value of the last cell to the first cell. A cyclical shift of a row to the left is performed similarly, but in the other direction. For example, if we cyclically shift a row "00110" one cell to the right, we get a row "00011", but if we shift a row "00110" one cell to the left, we get a row "01100". Determine the minimum number of moves needed to make some table column consist only of numbers 1.
The first line contains two space-separated integers: *n* (1<=≀<=*n*<=≀<=100)Β β€” the number of rows in the table and *m* (1<=≀<=*m*<=≀<=104)Β β€” the number of columns in the table. Then *n* lines follow, each of them contains *m* characters "0" or "1": the *j*-th character of the *i*-th line describes the contents of the cell in the *i*-th row and in the *j*-th column of the table. It is guaranteed that the description of the table contains no other characters besides "0" and "1".
Print a single number: the minimum number of moves needed to get only numbers 1 in some column of the table. If this is impossible, print -1.
[ "3 6\n101010\n000100\n100000\n", "2 3\n111\n000\n" ]
[ "3\n", "-1\n" ]
In the first sample one way to achieve the goal with the least number of moves is as follows: cyclically shift the second row to the right once, then shift the third row to the left twice. Then the table column before the last one will contain only 1s. In the second sample one can't shift the rows to get a column containing only 1s.
[ { "input": "3 6\n101010\n000100\n100000", "output": "3" }, { "input": "2 3\n111\n000", "output": "-1" }, { "input": "1 1\n1", "output": "0" }, { "input": "1 1\n0", "output": "-1" }, { "input": "3 1\n1\n1\n0", "output": "-1" }, { "input": "6 2\n10\n11\n...
218
0
0
6,754
573
Bear and Drawing
[ "constructive algorithms", "dfs and similar", "trees" ]
null
null
Limak is a little bear who learns to draw. People usually start with houses, fences and flowers but why would bears do it? Limak lives in the forest and he decides to draw a tree. Recall that tree is a connected graph consisting of *n* vertices and *n*<=-<=1 edges. Limak chose a tree with *n* vertices. He has infinite strip of paper with two parallel rows of dots. Little bear wants to assign vertices of a tree to some *n* distinct dots on a paper so that edges would intersect only at their endpoints β€” drawn tree must be planar. Below you can see one of correct drawings for the first sample test. Is it possible for Limak to draw chosen tree?
The first line contains single integer *n* (1<=≀<=*n*<=≀<=105). Next *n*<=-<=1 lines contain description of a tree. *i*-th of them contains two space-separated integers *a**i* and *b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*,<=*a**i*<=β‰ <=*b**i*) denoting an edge between vertices *a**i* and *b**i*. It's guaranteed that given description forms a tree.
Print "Yes" (without the quotes) if Limak can draw chosen tree. Otherwise, print "No" (without the quotes).
[ "8\n1 2\n1 3\n1 6\n6 4\n6 7\n6 5\n7 8\n", "13\n1 2\n1 3\n1 4\n2 5\n2 6\n2 7\n3 8\n3 9\n3 10\n4 11\n4 12\n4 13\n" ]
[ "Yes\n", "No\n" ]
none
[]
30
0
0
6,755
616
Sum of Remainders
[ "implementation", "math", "number theory" ]
null
null
Calculate the value of the sum: *n* mod 1 + *n* mod 2 + *n* mod 3 + ... + *n* mod *m*. As the result can be very large, you should print the value modulo 109<=+<=7 (the remainder when divided by 109<=+<=7). The modulo operator *a* mod *b* stands for the remainder after dividing *a* by *b*. For example 10 mod 3 = 1.
The only line contains two integers *n*,<=*m* (1<=≀<=*n*,<=*m*<=≀<=1013) β€” the parameters of the sum.
Print integer *s* β€” the value of the required sum modulo 109<=+<=7.
[ "3 4\n", "4 4\n", "1 1\n" ]
[ "4\n", "1\n", "0\n" ]
none
[ { "input": "3 4", "output": "4" }, { "input": "4 4", "output": "1" }, { "input": "1 1", "output": "0" }, { "input": "20000000 10000000", "output": "176305083" }, { "input": "10000000000000 10000000000000", "output": "869957328" }, { "input": "1 1000000...
2,000
0
0
6,759
797
Minimal string
[ "data structures", "greedy", "strings" ]
null
null
Petya recieved a gift of a string *s* with length up to 105 characters for his birthday. He took two more empty strings *t* and *u* and decided to play a game. This game has two possible moves: - Extract the first character of *s* and append *t* with this character. - Extract the last character of *t* and append *u* with this character. Petya wants to get strings *s* and *t* empty and string *u* lexicographically minimal. You should write a program that will help Petya win the game.
First line contains non-empty string *s* (1<=≀<=|*s*|<=≀<=105), consisting of lowercase English letters.
Print resulting string *u*.
[ "cab\n", "acdb\n" ]
[ "abc\n", "abdc\n" ]
none
[ { "input": "cab", "output": "abc" }, { "input": "acdb", "output": "abdc" }, { "input": "a", "output": "a" }, { "input": "ab", "output": "ab" }, { "input": "ba", "output": "ab" }, { "input": "dijee", "output": "deeji" }, { "input": "bhrmc", ...
108
307,200
0
6,760
397
On Segment's Own Points
[ "implementation" ]
null
null
Our old friend Alexey has finally entered the University of City N β€” the Berland capital. Alexey expected his father to get him a place to live in but his father said it was high time for Alexey to practice some financial independence. So, Alexey is living in a dorm. The dorm has exactly one straight dryer β€” a 100 centimeter long rope to hang clothes on. The dryer has got a coordinate system installed: the leftmost end of the dryer has coordinate 0, and the opposite end has coordinate 100. Overall, the university has *n* students. Dean's office allows *i*-th student to use the segment (*l**i*,<=*r**i*) of the dryer. However, the dean's office actions are contradictory and now one part of the dryer can belong to multiple students! Alexey don't like when someone touch his clothes. That's why he want make it impossible to someone clothes touch his ones. So Alexey wonders: what is the total length of the parts of the dryer that he may use in a such way that clothes of the others (*n*<=-<=1) students aren't drying there. Help him! Note that Alexey, as the most respected student, has number 1.
The first line contains a positive integer *n* (1<=≀<=*n*<=≀<=100). The (*i*<=+<=1)-th line contains integers *l**i* and *r**i* (0<=≀<=*l**i*<=&lt;<=*r**i*<=≀<=100) β€”Β the endpoints of the corresponding segment for the *i*-th student.
On a single line print a single number *k*, equal to the sum of lengths of the parts of the dryer which are inside Alexey's segment and are outside all other segments.
[ "3\n0 5\n2 8\n1 6\n", "3\n0 10\n1 5\n7 15\n" ]
[ "1\n", "3\n" ]
Note that it's not important are clothes drying on the touching segments (e.g. (0, 1) and (1, 2)) considered to be touching or not because you need to find the length of segments. In the first test sample Alexey may use the only segment (0, 1). In such case his clothes will not touch clothes on the segments (1, 6) and (2, 8). The length of segment (0, 1) is 1. In the second test sample Alexey may dry his clothes on segments (0, 1) and (5, 7). Overall length of these segments is 3.
[ { "input": "3\n0 5\n2 8\n1 6", "output": "1" }, { "input": "3\n0 10\n1 5\n7 15", "output": "3" }, { "input": "1\n0 100", "output": "100" }, { "input": "2\n1 9\n1 9", "output": "0" }, { "input": "2\n1 9\n5 10", "output": "4" }, { "input": "2\n1 9\n3 5",...
93
6,144,000
0
6,764
484
Bits
[ "bitmasks", "constructive algorithms" ]
null
null
Let's denote as the number of bits set ('1' bits) in the binary representation of the non-negative integer *x*. You are given multiple queries consisting of pairs of integers *l* and *r*. For each query, find the *x*, such that *l*<=≀<=*x*<=≀<=*r*, and is maximum possible. If there are multiple such numbers find the smallest of them.
The first line contains integer *n*Β β€” the number of queries (1<=≀<=*n*<=≀<=10000). Each of the following *n* lines contain two integers *l**i*,<=*r**i*Β β€” the arguments for the corresponding query (0<=≀<=*l**i*<=≀<=*r**i*<=≀<=1018).
For each query print the answer in a separate line.
[ "3\n1 2\n2 4\n1 10\n" ]
[ "1\n3\n7\n" ]
The binary representations of numbers from 1 to 10 are listed below: 1<sub class="lower-index">10</sub> = 1<sub class="lower-index">2</sub> 2<sub class="lower-index">10</sub> = 10<sub class="lower-index">2</sub> 3<sub class="lower-index">10</sub> = 11<sub class="lower-index">2</sub> 4<sub class="lower-index">10</sub> = 100<sub class="lower-index">2</sub> 5<sub class="lower-index">10</sub> = 101<sub class="lower-index">2</sub> 6<sub class="lower-index">10</sub> = 110<sub class="lower-index">2</sub> 7<sub class="lower-index">10</sub> = 111<sub class="lower-index">2</sub> 8<sub class="lower-index">10</sub> = 1000<sub class="lower-index">2</sub> 9<sub class="lower-index">10</sub> = 1001<sub class="lower-index">2</sub> 10<sub class="lower-index">10</sub> = 1010<sub class="lower-index">2</sub>
[ { "input": "3\n1 2\n2 4\n1 10", "output": "1\n3\n7" }, { "input": "55\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n2 2\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n3 3\n3 4\n3 5\n3 6\n3 7\n3 8\n3 9\n3 10\n4 4\n4 5\n4 6\n4 7\n4 8\n4 9\n4 10\n5 5\n5 6\n5 7\n5 8\n5 9\n5 10\n6 6\n6 7\n6 8\n6 9\n6 10...
109
0
0
6,774