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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
959 | Mahmoud and Ehab and the even-odd game | [
"games",
"math"
] | null | null | Mahmoud and Ehab play a game called the even-odd game. Ehab chooses his favorite integer *n* and then they take turns, starting from Mahmoud. In each player's turn, he has to choose an integer *a* and subtract it from *n* such that:
- 1<=β€<=*a*<=β€<=*n*. - If it's Mahmoud's turn, *a* has to be even, but if it's Ehab's turn, *a* has to be odd.
If the current player can't choose any number satisfying the conditions, he loses. Can you determine the winner if they both play optimally? | The only line contains an integer *n* (1<=β€<=*n*<=β€<=109), the number at the beginning of the game. | Output "Mahmoud" (without quotes) if Mahmoud wins and "Ehab" (without quotes) otherwise. | [
"1\n",
"2\n"
] | [
"Ehab",
"Mahmoud"
] | In the first sample, Mahmoud can't choose any integer *a* initially because there is no positive even integer less than or equal to 1 so Ehab wins.
In the second sample, Mahmoud has to choose *a*β=β2 and subtract it from *n*. It's Ehab's turn and *n*β=β0. There is no positive odd integer less than or equal to 0 so Mahmoud wins. | [
{
"input": "1",
"output": "Ehab"
},
{
"input": "2",
"output": "Mahmoud"
},
{
"input": "10000",
"output": "Mahmoud"
},
{
"input": "33333",
"output": "Ehab"
},
{
"input": "5",
"output": "Ehab"
},
{
"input": "1000000000",
"output": "Mahmoud"
},
{
... | 62 | 0 | 3 | 3,996 | |
0 | none | [
"none"
] | null | null | Piet is one of the most known visual esoteric programming languages. The programs in Piet are constructed from colorful blocks of pixels and interpreted using pretty complicated rules. In this problem we will use a subset of Piet language with simplified rules.
The program will be a rectangular image consisting of colored and black pixels. The color of each pixel will be given by an integer number between 0 and 9, inclusive, with 0 denoting black. A block of pixels is defined as a rectangle of pixels of the same color (not black). It is guaranteed that all connected groups of colored pixels of the same color will form rectangular blocks. Groups of black pixels can form arbitrary shapes.
The program is interpreted using movement of instruction pointer (IP) which consists of three parts:
- current block pointer (BP); note that there is no concept of current pixel within the block;- direction pointer (DP) which can point left, right, up or down;- block chooser (CP) which can point to the left or to the right from the direction given by DP; in absolute values CP can differ from DP by 90 degrees counterclockwise or clockwise, respectively.
Initially BP points to the block which contains the top-left corner of the program, DP points to the right, and CP points to the left (see the orange square on the image below).
One step of program interpretation changes the state of IP in a following way. The interpreter finds the furthest edge of the current color block in the direction of the DP. From all pixels that form this edge, the interpreter selects the furthest one in the direction of CP. After this, BP attempts to move from this pixel into the next one in the direction of DP. If the next pixel belongs to a colored block, this block becomes the current one, and two other parts of IP stay the same. It the next pixel is black or outside of the program, BP stays the same but two other parts of IP change. If CP was pointing to the left, now it points to the right, and DP stays the same. If CP was pointing to the right, now it points to the left, and DP is rotated 90 degrees clockwise.
This way BP will never point to a black block (it is guaranteed that top-left pixel of the program will not be black).
You are given a Piet program. You have to figure out which block of the program will be current after *n* steps. | The first line of the input contains two integer numbers *m* (1<=β€<=*m*<=β€<=50) and *n* (1<=β€<=*n*<=β€<=5Β·107). Next *m* lines contain the rows of the program. All the lines have the same length between 1 and 50 pixels, and consist of characters 0-9. The first character of the first line will not be equal to 0. | Output the color of the block which will be current after *n* steps of program interpretation. | [
"2 10\n12\n43\n",
"3 12\n1423\n6624\n6625\n",
"5 9\n10345\n23456\n34567\n45678\n56789\n"
] | [
"1\n",
"6\n",
"5\n"
] | In the first example IP changes in the following way. After step 1 block 2 becomes current one and stays it after two more steps. After step 4 BP moves to block 3, after step 7 β to block 4, and finally after step 10 BP returns to block 1.
The sequence of states of IP is shown on the image: the arrows are traversed clockwise, the main arrow shows direction of DP, the side one β the direction of CP. | [] | 92 | 0 | 0 | 4,000 | |
765 | Souvenirs | [
"data structures"
] | null | null | Artsem is on vacation and wants to buy souvenirs for his two teammates. There are *n* souvenir shops along the street. In *i*-th shop Artsem can buy one souvenir for *a**i* dollars, and he cannot buy more than one souvenir in one shop. He doesn't want to introduce envy in his team, so he wants to buy two souvenirs with least possible difference in price.
Artsem has visited the shopping street *m* times. For some strange reason on the *i*-th day only shops with numbers from *l**i* to *r**i* were operating (weird? yes it is, but have you ever tried to come up with a reasonable legend for a range query problem?). For each visit, Artsem wants to know the minimum possible difference in prices of two different souvenirs he can buy in the opened shops.
In other words, for each Artsem's visit you should find the minimum possible value of |*a**s*<=-<=*a**t*| where *l**i*<=β€<=*s*,<=*t*<=β€<=*r**i*, *s*<=β <=*t*. | The first line contains an integer *n* (2<=β€<=*n*<=β€<=105).
The second line contains *n* space-separated integers *a*1, ..., *a**n* (0<=β€<=*a**i*<=β€<=109).
The third line contains the number of queries *m* (1<=β€<=*m*<=β€<=3Β·105).
Next *m* lines describe the queries. *i*-th of these lines contains two space-separated integers *l**i* and *r**i* denoting the range of shops working on *i*-th day (1<=β€<=*l**i*<=<<=*r**i*<=β€<=*n*). | Print the answer to each query in a separate line. | [
"8\n3 1 4 1 5 9 2 6\n4\n1 8\n1 3\n4 8\n5 7\n"
] | [
"0\n1\n1\n3\n"
] | none | [] | 30 | 0 | 0 | 4,004 | |
551 | GukiZ hates Boxes | [
"binary search",
"greedy"
] | null | null | Professor GukiZ is concerned about making his way to school, because massive piles of boxes are blocking his way.
In total there are *n* piles of boxes, arranged in a line, from left to right, *i*-th pile (1<=β€<=*i*<=β€<=*n*) containing *a**i* boxes. Luckily, *m* students are willing to help GukiZ by removing all the boxes from his way. Students are working simultaneously. At time 0, all students are located left of the first pile. It takes one second for every student to move from this position to the first pile, and after that, every student must start performing sequence of two possible operations, each taking one second to complete. Possible operations are:
1. If *i*<=β <=*n*, move from pile *i* to pile *i*<=+<=1;1. If pile located at the position of student is not empty, remove one box from it.
GukiZ's students aren't smart at all, so they need you to tell them how to remove boxes before professor comes (he is very impatient man, and doesn't want to wait). They ask you to calculate minumum time *t* in seconds for which they can remove all the boxes from GukiZ's way. Note that students can be positioned in any manner after *t* seconds, but all the boxes must be removed. | The first line contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=105), the number of piles of boxes and the number of GukiZ's students.
The second line contains *n* integers *a*1,<=*a*2,<=... *a**n* (0<=β€<=*a**i*<=β€<=109) where *a**i* represents the number of boxes on *i*-th pile. It's guaranteed that at least one pile of is non-empty. | In a single line, print one number, minimum time needed to remove all the boxes in seconds. | [
"2 1\n1 1\n",
"3 2\n1 0 2\n",
"4 100\n3 4 5 4\n"
] | [
"4\n",
"5\n",
"5\n"
] | First sample: Student will first move to the first pile (1 second), then remove box from first pile (1 second), then move to the second pile (1 second) and finally remove the box from second pile (1 second).
Second sample: One of optimal solutions is to send one student to remove a box from the first pile and a box from the third pile, and send another student to remove a box from the third pile. Overall, 5 seconds.
Third sample: With a lot of available students, send three of them to remove boxes from the first pile, four of them to remove boxes from the second pile, five of them to remove boxes from the third pile, and four of them to remove boxes from the fourth pile. Process will be over in 5 seconds, when removing the boxes from the last pile is finished. | [
{
"input": "2 1\n1 1",
"output": "4"
},
{
"input": "3 2\n1 0 2",
"output": "5"
},
{
"input": "4 100\n3 4 5 4",
"output": "5"
},
{
"input": "5 8\n121351 0 13513 0 165454",
"output": "37544"
},
{
"input": "6 6\n0 10 0 0 10 0",
"output": "8"
},
{
"input":... | 62 | 1,945,600 | 0 | 4,014 | |
432 | Football Kit | [
"brute force",
"greedy",
"implementation"
] | null | null | Consider a football tournament where *n* teams participate. Each team has two football kits: for home games, and for away games. The kit for home games of the *i*-th team has color *x**i* and the kit for away games of this team has color *y**i* (*x**i*<=β <=*y**i*).
In the tournament, each team plays exactly one home game and exactly one away game with each other team (*n*(*n*<=-<=1) games in total). The team, that plays the home game, traditionally plays in its home kit. The team that plays an away game plays in its away kit. However, if two teams has the kits of the same color, they cannot be distinguished. In this case the away team plays in its home kit.
Calculate how many games in the described tournament each team plays in its home kit and how many games it plays in its away kit. | The first line contains a single integer *n* (2<=β€<=*n*<=β€<=105) β the number of teams. Next *n* lines contain the description of the teams. The *i*-th line contains two space-separated numbers *x**i*, *y**i* (1<=β€<=*x**i*,<=*y**i*<=β€<=105;Β *x**i*<=β <=*y**i*) β the color numbers for the home and away kits of the *i*-th team. | For each team, print on a single line two space-separated integers β the number of games this team is going to play in home and away kits, correspondingly. Print the answers for the teams in the order they appeared in the input. | [
"2\n1 2\n2 1\n",
"3\n1 2\n2 1\n1 3\n"
] | [
"2 0\n2 0\n",
"3 1\n4 0\n2 2\n"
] | none | [
{
"input": "2\n1 2\n2 1",
"output": "2 0\n2 0"
},
{
"input": "3\n1 2\n2 1\n1 3",
"output": "3 1\n4 0\n2 2"
},
{
"input": "2\n1 2\n1 2",
"output": "1 1\n1 1"
},
{
"input": "2\n1 2\n3 4",
"output": "1 1\n1 1"
},
{
"input": "3\n1 100000\n1 100000\n100000 2",
"out... | 264 | 31,232,000 | 3 | 4,015 | |
830 | Office Keys | [
"binary search",
"brute force",
"dp",
"greedy",
"sortings"
] | null | null | There are *n* people and *k* keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else.
You are to determine the minimum time needed for all *n* people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it. | The first line contains three integers *n*, *k* and *p* (1<=β€<=*n*<=β€<=1<=000, *n*<=β€<=*k*<=β€<=2<=000, 1<=β€<=*p*<=β€<=109) β the number of people, the number of keys and the office location.
The second line contains *n* distinct integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109) β positions in which people are located initially. The positions are given in arbitrary order.
The third line contains *k* distinct integers *b*1,<=*b*2,<=...,<=*b**k* (1<=β€<=*b**j*<=β€<=109) β positions of the keys. The positions are given in arbitrary order.
Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point. | Print the minimum time (in seconds) needed for all *n* to reach the office with keys. | [
"2 4 50\n20 100\n60 10 40 80\n",
"1 2 10\n11\n15 7\n"
] | [
"50\n",
"7\n"
] | In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50 seconds. Thus, after 50 seconds everybody is in office with keys. | [
{
"input": "2 4 50\n20 100\n60 10 40 80",
"output": "50"
},
{
"input": "1 2 10\n11\n15 7",
"output": "7"
},
{
"input": "2 5 15\n10 4\n29 23 21 22 26",
"output": "23"
},
{
"input": "3 10 1500\n106 160 129\n1333 1532 1181 1091 1656 1698 1291 1741 1242 1163",
"output": "1394... | 77 | 4,300,800 | 3 | 4,018 | |
721 | Passwords | [
"implementation",
"math",
"sortings",
"strings"
] | null | null | Vanya is managed to enter his favourite site Codehorses. Vanya uses *n* distinct passwords for sites at all, however he can't remember which one exactly he specified during Codehorses registration.
Vanya will enter passwords in order of non-decreasing their lengths, and he will enter passwords of same length in arbitrary order. Just when Vanya will have entered the correct password, he is immediately authorized on the site. Vanya will not enter any password twice.
Entering any passwords takes one second for Vanya. But if Vanya will enter wrong password *k* times, then he is able to make the next try only 5 seconds after that. Vanya makes each try immediately, that is, at each moment when Vanya is able to enter password, he is doing that.
Determine how many seconds will Vanya need to enter Codehorses in the best case for him (if he spends minimum possible number of second) and in the worst case (if he spends maximum possible amount of seconds). | The first line of the input contains two integers *n* and *k* (1<=β€<=*n*,<=*k*<=β€<=100)Β β the number of Vanya's passwords and the number of failed tries, after which the access to the site is blocked for 5 seconds.
The next *n* lines contains passwords, one per lineΒ β pairwise distinct non-empty strings consisting of latin letters and digits. Each password length does not exceed 100 characters.
The last line of the input contains the Vanya's Codehorses password. It is guaranteed that the Vanya's Codehorses password is equal to some of his *n* passwords. | Print two integersΒ β time (in seconds), Vanya needs to be authorized to Codehorses in the best case for him and in the worst case respectively. | [
"5 2\ncba\nabc\nbb1\nabC\nABC\nabc\n",
"4 100\n11\n22\n1\n2\n22\n"
] | [
"1 15\n",
"3 4\n"
] | Consider the first sample case. As soon as all passwords have the same length, Vanya can enter the right password at the first try as well as at the last try. If he enters it at the first try, he spends exactly 1 second. Thus in the best case the answer is 1. If, at the other hand, he enters it at the last try, he enters another 4 passwords before. He spends 2 seconds to enter first 2 passwords, then he waits 5 seconds as soon as he made 2 wrong tries. Then he spends 2 more seconds to enter 2 wrong passwords, again waits 5 seconds and, finally, enters the correct password spending 1 more second. In summary in the worst case he is able to be authorized in 15 seconds.
Consider the second sample case. There is no way of entering passwords and get the access to the site blocked. As soon as the required password has length of 2, Vanya enters all passwords of length 1 anyway, spending 2 seconds for that. Then, in the best case, he immediately enters the correct password and the answer for the best case is 3, but in the worst case he enters wrong password of length 2 and only then the right one, spending 4 seconds at all. | [
{
"input": "5 2\ncba\nabc\nbb1\nabC\nABC\nabc",
"output": "1 15"
},
{
"input": "4 100\n11\n22\n1\n2\n22",
"output": "3 4"
},
{
"input": "1 1\na1\na1",
"output": "1 1"
},
{
"input": "1 100\na1\na1",
"output": "1 1"
},
{
"input": "2 1\nabc\nAbc\nAbc",
"output": ... | 93 | 6,963,200 | 0 | 4,021 | |
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 -... | 358 | 13,209,600 | 3 | 4,028 | |
169 | Chores | [
"sortings"
] | null | null | Petya and Vasya are brothers. Today is a special day for them as their parents left them home alone and commissioned them to do *n* chores. Each chore is characterized by a single parameter β its complexity. The complexity of the *i*-th chore equals *h**i*.
As Petya is older, he wants to take the chores with complexity larger than some value *x* (*h**i*<=><=*x*) to leave to Vasya the chores with complexity less than or equal to *x* (*h**i*<=β€<=*x*). The brothers have already decided that Petya will do exactly *a* chores and Vasya will do exactly *b* chores (*a*<=+<=*b*<==<=*n*).
In how many ways can they choose an integer *x* so that Petya got exactly *a* chores and Vasya got exactly *b* chores? | The first input line contains three integers *n*,<=*a* and *b* (2<=β€<=*n*<=β€<=2000; *a*,<=*b*<=β₯<=1; *a*<=+<=*b*<==<=*n*) β the total number of chores, the number of Petya's chores and the number of Vasya's chores.
The next line contains a sequence of integers *h*1,<=*h*2,<=...,<=*h**n* (1<=β€<=*h**i*<=β€<=109), *h**i* is the complexity of the *i*-th chore. The numbers in the given sequence are not necessarily different.
All numbers on the lines are separated by single spaces. | Print the required number of ways to choose an integer value of *x*. If there are no such ways, print 0. | [
"5 2 3\n6 2 3 100 1\n",
"7 3 4\n1 1 9 1 1 1 1\n"
] | [
"3\n",
"0\n"
] | In the first sample the possible values of *x* are 3, 4 or 5.
In the second sample it is impossible to find such *x*, that Petya got 3 chores and Vasya got 4. | [
{
"input": "5 2 3\n6 2 3 100 1",
"output": "3"
},
{
"input": "7 3 4\n1 1 9 1 1 1 1",
"output": "0"
},
{
"input": "2 1 1\n10 2",
"output": "8"
},
{
"input": "2 1 1\n7 7",
"output": "0"
},
{
"input": "2 1 1\n1 1000000000",
"output": "999999999"
},
{
"inp... | 46 | 0 | 3 | 4,036 | |
854 | Maxim Buys an Apartment | [
"constructive algorithms",
"math"
] | null | null | Maxim wants to buy an apartment in a new house at Line Avenue of Metropolis. The house has *n* apartments that are numbered from 1 to *n* and are arranged in a row. Two apartments are adjacent if their indices differ by 1. Some of the apartments can already be inhabited, others are available for sale.
Maxim often visits his neighbors, so apartment is good for him if it is available for sale and there is at least one already inhabited apartment adjacent to it. Maxim knows that there are exactly *k* already inhabited apartments, but he doesn't know their indices yet.
Find out what could be the minimum possible and the maximum possible number of apartments that are good for Maxim. | The only line of the input contains two integers: *n* and *k* (1<=β€<=*n*<=β€<=109, 0<=β€<=*k*<=β€<=*n*). | Print the minimum possible and the maximum possible number of apartments good for Maxim. | [
"6 3\n"
] | [
"1 3\n"
] | In the sample test, the number of good apartments could be minimum possible if, for example, apartments with indices 1, 2 and 3 were inhabited. In this case only apartment 4 is good. The maximum possible number could be, for example, if apartments with indices 1, 3 and 5 were inhabited. In this case all other apartments: 2, 4 and 6 are good. | [
{
"input": "6 3",
"output": "1 3"
},
{
"input": "10 1",
"output": "1 2"
},
{
"input": "10 9",
"output": "1 1"
},
{
"input": "8 0",
"output": "0 0"
},
{
"input": "8 8",
"output": "0 0"
},
{
"input": "966871928 890926970",
"output": "1 75944958"
},... | 61 | 0 | 0 | 4,044 | |
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 "
},
{
... | 124 | 0 | 0 | 4,045 | |
329 | Purification | [
"constructive algorithms",
"greedy"
] | null | null | You are an adventurer currently journeying inside an evil temple. After defeating a couple of weak zombies, you arrived at a square room consisting of tiles forming an *n*<=Γ<=*n* grid. The rows are numbered 1 through *n* from top to bottom, and the columns are numbered 1 through *n* from left to right. At the far side of the room lies a door locked with evil magical forces. The following inscriptions are written on the door:
Being a very senior adventurer, you immediately realize what this means. You notice that every single cell in the grid are initially evil. You should purify all of these cells.
The only method of tile purification known to you is by casting the "Purification" spell. You cast this spell on a single tile β then, all cells that are located in the same row and all cells that are located in the same column as the selected tile become purified (including the selected tile)! It is allowed to purify a cell more than once.
You would like to purify all *n*<=Γ<=*n* cells while minimizing the number of times you cast the "Purification" spell. This sounds very easy, but you just noticed that some tiles are particularly more evil than the other tiles. You cannot cast the "Purification" spell on those particularly more evil tiles, not even after they have been purified. They can still be purified if a cell sharing the same row or the same column gets selected by the "Purification" spell.
Please find some way to purify all the cells with the minimum number of spells cast. Print -1 if there is no such way. | The first line will contain a single integer *n* (1<=β€<=*n*<=β€<=100). Then, *n* lines follows, each contains *n* characters. The *j*-th character in the *i*-th row represents the cell located at row *i* and column *j*. It will be the character 'E' if it is a particularly more evil cell, and '.' otherwise. | If there exists no way to purify all the cells, output -1. Otherwise, if your solution casts *x* "Purification" spells (where *x* is the minimum possible number of spells), output *x* lines. Each line should consist of two integers denoting the row and column numbers of the cell on which you should cast the "Purification" spell. | [
"3\n.E.\nE.E\n.E.\n",
"3\nEEE\nE..\nE.E\n",
"5\nEE.EE\nE.EE.\nE...E\n.EE.E\nEE.EE\n"
] | [
"1 1\n2 2\n3 3\n",
"-1\n",
"3 3\n1 3\n2 2\n4 4\n5 3"
] | The first example is illustrated as follows. Purple tiles are evil tiles that have not yet been purified. Red tile is the tile on which "Purification" is cast. Yellow tiles are the tiles being purified as a result of the current "Purification" spell. Green tiles are tiles that have been purified previously.
In the second example, it is impossible to purify the cell located at row 1 and column 1.
For the third example: | [
{
"input": "3\n.E.\nE.E\n.E.",
"output": "1 1\n2 2\n3 1"
},
{
"input": "3\nEEE\nE..\nE.E",
"output": "-1"
},
{
"input": "5\nEE.EE\nE.EE.\nE...E\n.EE.E\nEE.EE",
"output": "1 3\n2 2\n3 2\n4 1\n5 3"
},
{
"input": "3\n.EE\n.EE\n.EE",
"output": "1 1\n2 1\n3 1"
},
{
"in... | 156 | 307,200 | 0 | 4,052 | |
0 | none | [
"none"
] | null | null | All Berland residents are waiting for an unprecedented tour of wizard in his Blue Helicopter over the cities of Berland!
It is well-known that there are *n* cities in Berland, some pairs of which are connected by bidirectional roads. Each pair of cities is connected by no more than one road. It is not guaranteed that the road network is connected, i.e. it is possible that you can't reach some city from some other.
The tour will contain several episodes. In each of the episodes:
- the wizard will disembark at some city *x* from the Helicopter; - he will give a performance and show a movie for free at the city *x*; - he will drive to some neighboring city *y* using a road; - he will give a performance and show a movie for free at the city *y*; - he will drive to some neighboring to *y* city *z*; - he will give a performance and show a movie for free at the city *z*; - he will embark the Helicopter and fly away from the city *z*.
It is known that the wizard doesn't like to use roads, so he agrees to use each road at most once (regardless of direction). In other words, for road between *a* and *b* he only can drive once from *a* to *b*, or drive once from *b* to *a*, or do not use this road at all.
The wizards wants to plan as many episodes as possible without violation the above rules. Help the wizard!
Please note that the wizard can visit the same city multiple times, the restriction is on roads only. | The first line contains two integers *n*, *m* (1<=β€<=*n*<=β€<=2Β·105, 0<=β€<=*m*<=β€<=2Β·105) β the number of cities and the number of roads in Berland, respectively.
The roads description follow, one in each line. Each description is a pair of two integers *a**i*,<=*b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*, *a**i*<=β <=*b**i*), where *a**i* and *b**i* are the ids of the cities connected by the *i*-th road. It is guaranteed that there are no two roads connecting the same pair of cities. Every road is bidirectional. The cities are numbered from 1 to *n*.
It is possible that the road network in Berland is not connected. | In the first line print *w* β the maximum possible number of episodes. The next *w* lines should contain the episodes in format *x*, *y*, *z* β the three integers denoting the ids of the cities in the order of the wizard's visits. | [
"4 5\n1 2\n3 2\n2 4\n3 4\n4 1\n",
"5 8\n5 3\n1 2\n4 5\n5 1\n2 5\n4 3\n1 4\n3 2\n"
] | [
"2\n1 4 2\n4 3 2\n",
"4\n1 4 5\n2 3 4\n1 5 3\n5 2 1\n"
] | none | [
{
"input": "4 5\n1 2\n3 2\n2 4\n3 4\n4 1",
"output": "2\n1 4 2\n4 3 2"
},
{
"input": "5 8\n5 3\n1 2\n4 5\n5 1\n2 5\n4 3\n1 4\n3 2",
"output": "4\n1 4 5\n2 3 4\n1 5 3\n5 2 1"
},
{
"input": "1 0",
"output": "0"
},
{
"input": "2 1\n1 2",
"output": "0"
},
{
"input": "... | 46 | 0 | 0 | 4,058 | |
690 | Brain Network (medium) | [
"dfs and similar",
"graphs",
"trees"
] | null | null | Further research on zombie thought processes yielded interesting results. As we know from the previous problem, the nervous system of a zombie consists of *n* brains and *m* brain connectors joining some pairs of brains together. It was observed that the intellectual abilities of a zombie depend mainly on the topology of its nervous system. More precisely, we define the distance between two brains *u* and *v* (1<=β€<=*u*,<=*v*<=β€<=*n*) as the minimum number of brain connectors used when transmitting a thought between these two brains. The brain latency of a zombie is defined to be the maximum distance between any two of its brains. Researchers conjecture that the brain latency is the crucial parameter which determines how smart a given zombie is. Help them test this conjecture by writing a program to compute brain latencies of nervous systems.
In this problem you may assume that any nervous system given in the input is valid, i.e., it satisfies conditions (1) and (2) from the easy version. | The first line of the input contains two space-separated integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=100000) denoting the number of brains (which are conveniently numbered from 1 to *n*) and the number of brain connectors in the nervous system, respectively. In the next *m* lines, descriptions of brain connectors follow. Every connector is given as a pair of brains *a*β*b* it connects (1<=β€<=*a*,<=*b*<=β€<=*n* and *a*<=β <=*b*). | Print one number β the brain latency. | [
"4 3\n1 2\n1 3\n1 4\n",
"5 4\n1 2\n2 3\n3 4\n3 5\n"
] | [
"2",
"3"
] | none | [
{
"input": "2 1\n1 2",
"output": "1"
},
{
"input": "3 2\n2 1\n3 2",
"output": "2"
},
{
"input": "10 9\n5 1\n1 2\n9 3\n10 5\n6 3\n8 5\n2 7\n2 3\n9 4",
"output": "6"
},
{
"input": "4 3\n1 2\n1 3\n1 4",
"output": "2"
},
{
"input": "5 4\n1 2\n2 3\n3 4\n3 5",
"outp... | 62 | 0 | 0 | 4,083 | |
822 | Hacker, pack your bags! | [
"binary search",
"greedy",
"implementation",
"sortings"
] | null | null | It's well known that the best way to distract from something is to do one's favourite thing. Job is such a thing for Leha.
So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world. For such zeal boss gave the hacker a vacation of exactly *x* days. You know the majority of people prefer to go somewhere for a vacation, so Leha immediately went to the travel agency. There he found out that *n* vouchers left. *i*-th voucher is characterized by three integers *l**i*, *r**i*, *cost**i* β day of departure from ViΔkopolis, day of arriving back in ViΔkopolis and cost of the voucher correspondingly. The duration of the *i*-th voucher is a value *r**i*<=-<=*l**i*<=+<=1.
At the same time Leha wants to split his own vocation into two parts. Besides he wants to spend as little money as possible. Formally Leha wants to choose exactly two vouchers *i* and *j* (*i*<=β <=*j*) so that they don't intersect, sum of their durations is exactly *x* and their total cost is as minimal as possible. Two vouchers *i* and *j* don't intersect if only at least one of the following conditions is fulfilled: *r**i*<=<<=*l**j* or *r**j*<=<<=*l**i*.
Help Leha to choose the necessary vouchers! | The first line contains two integers *n* and *x* (2<=β€<=*n*,<=*x*<=β€<=2Β·105) β the number of vouchers in the travel agency and the duration of Leha's vacation correspondingly.
Each of the next *n* lines contains three integers *l**i*, *r**i* and *cost**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=2Β·105,<=1<=β€<=*cost**i*<=β€<=109) β description of the voucher. | Print a single integer β a minimal amount of money that Leha will spend, or print <=-<=1 if it's impossible to choose two disjoint vouchers with the total duration exactly *x*. | [
"4 5\n1 3 4\n1 2 5\n5 6 1\n1 2 4\n",
"3 2\n4 6 3\n2 4 1\n3 5 4\n"
] | [
"5\n",
"-1\n"
] | In the first sample Leha should choose first and third vouchers. Hereupon the total duration will be equal to (3β-β1β+β1)β+β(6β-β5β+β1)β=β5 and the total cost will be 4β+β1β=β5.
In the second sample the duration of each voucher is 3 therefore it's impossible to choose two vouchers with the total duration equal to 2. | [
{
"input": "4 5\n1 3 4\n1 2 5\n5 6 1\n1 2 4",
"output": "5"
},
{
"input": "3 2\n4 6 3\n2 4 1\n3 5 4",
"output": "-1"
},
{
"input": "2 1855\n159106 161198 437057705\n149039 158409 889963913",
"output": "-1"
},
{
"input": "15 17\n1 10 8\n5 19 1\n12 14 6\n9 19 8\n6 7 3\n5 11 9\n... | 46 | 0 | 0 | 4,087 | |
496 | Removing Columns | [
"brute force",
"constructive algorithms",
"implementation"
] | null | null | You are given an *n*<=Γ<=*m* rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the table
Β
we obtain the table:
Β
A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good. | The first line contains two integers Β β *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=100).
Next *n* lines contain *m* small English letters eachΒ β the characters of the table. | Print a single numberΒ β the minimum number of columns that you need to remove in order to make the table good. | [
"1 10\ncodeforces\n",
"4 4\ncase\ncare\ntest\ncode\n",
"5 4\ncode\nforc\nesco\ndefo\nrces\n"
] | [
"0\n",
"2\n",
"4\n"
] | In the first sample the table is already good.
In the second sample you may remove the first and third column.
In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).
Let strings *s* and *t* have equal length. Then, *s* is lexicographically larger than *t* if they are not equal and the character following the largest common prefix of *s* and *t* (the prefix may be empty) in *s* is alphabetically larger than the corresponding character of *t*. | [
{
"input": "1 10\ncodeforces",
"output": "0"
},
{
"input": "4 4\ncase\ncare\ntest\ncode",
"output": "2"
},
{
"input": "5 4\ncode\nforc\nesco\ndefo\nrces",
"output": "4"
},
{
"input": "2 2\nfb\nye",
"output": "0"
},
{
"input": "5 5\nrzrzh\nrzrzh\nrzrzh\nrzrzh\nrzrz... | 62 | 4,608,000 | 0 | 4,089 | |
216 | Tiling with Hexagons | [
"implementation",
"math"
] | null | null | Several ages ago Berland was a kingdom. The King of Berland adored math. That's why, when he first visited one of his many palaces, he first of all paid attention to the floor in one hall. The floor was tiled with hexagonal tiles.
The hall also turned out hexagonal in its shape. The King walked along the perimeter of the hall and concluded that each of the six sides has *a*, *b*, *c*, *a*, *b* and *c* adjacent tiles, correspondingly.
To better visualize the situation, look at the picture showing a similar hexagon for *a*<==<=2, *b*<==<=3 and *c*<==<=4.
According to the legend, as the King of Berland obtained the values *a*, *b* and *c*, he almost immediately calculated the total number of tiles on the hall floor. Can you do the same? | The first line contains three integers: *a*, *b* and *c* (2<=β€<=*a*,<=*b*,<=*c*<=β€<=1000). | Print a single number β the total number of tiles on the hall floor. | [
"2 3 4\n"
] | [
"18"
] | none | [
{
"input": "2 3 4",
"output": "18"
},
{
"input": "2 2 2",
"output": "7"
},
{
"input": "7 8 13",
"output": "224"
},
{
"input": "14 7 75",
"output": "1578"
},
{
"input": "201 108 304",
"output": "115032"
},
{
"input": "999 998 996",
"output": "298302... | 62 | 0 | 0 | 4,093 | |
631 | Print Check | [
"constructive algorithms",
"implementation"
] | null | null | Kris works in a large company "Blake Technologies". As a best engineer of the company he was assigned a task to develop a printer that will be able to print horizontal and vertical strips. First prototype is already built and Kris wants to tests it. He wants you to implement the program that checks the result of the printing.
Printer works with a rectangular sheet of paper of size *n*<=Γ<=*m*. Consider the list as a table consisting of *n* rows and *m* columns. Rows are numbered from top to bottom with integers from 1 to *n*, while columns are numbered from left to right with integers from 1 to *m*. Initially, all cells are painted in color 0.
Your program has to support two operations:
1. Paint all cells in row *r**i* in color *a**i*; 1. Paint all cells in column *c**i* in color *a**i*.
If during some operation *i* there is a cell that have already been painted, the color of this cell also changes to *a**i*.
Your program has to print the resulting table after *k* operation. | The first line of the input contains three integers *n*, *m* and *k* (1<=<=β€<=<=*n*,<=<=*m*<=<=β€<=5000, *n*Β·*m*<=β€<=100<=000, 1<=β€<=*k*<=β€<=100<=000)Β β the dimensions of the sheet and the number of operations, respectively.
Each of the next *k* lines contains the description of exactly one query:
- 1Β *r**i*Β *a**i* (1<=β€<=*r**i*<=β€<=*n*, 1<=β€<=*a**i*<=β€<=109), means that row *r**i* is painted in color *a**i*; - 2Β *c**i*Β *a**i* (1<=β€<=*c**i*<=β€<=*m*, 1<=β€<=*a**i*<=β€<=109), means that column *c**i* is painted in color *a**i*. | Print *n* lines containing *m* integers eachΒ β the resulting table after all operations are applied. | [
"3 3 3\n1 1 3\n2 2 1\n1 2 2\n",
"5 3 5\n1 1 1\n1 3 1\n1 5 1\n2 1 1\n2 3 1\n"
] | [
"3 1 3 \n2 2 2 \n0 1 0 \n",
"1 1 1 \n1 0 1 \n1 1 1 \n1 0 1 \n1 1 1 \n"
] | The figure below shows all three operations for the first sample step by step. The cells that were painted on the corresponding step are marked gray. | [
{
"input": "3 3 3\n1 1 3\n2 2 1\n1 2 2",
"output": "3 1 3 \n2 2 2 \n0 1 0 "
},
{
"input": "5 3 5\n1 1 1\n1 3 1\n1 5 1\n2 1 1\n2 3 1",
"output": "1 1 1 \n1 0 1 \n1 1 1 \n1 0 1 \n1 1 1 "
},
{
"input": "5 5 4\n1 2 1\n1 4 1\n2 2 1\n2 4 1",
"output": "0 1 0 1 0 \n1 1 1 1 1 \n0 1 0 1 0 \n1... | 1,000 | 10,547,200 | 0 | 4,100 | |
512 | Fox And Polygon | [
"constructive algorithms",
"divide and conquer"
] | null | null | Fox Ciel just designed a puzzle game called "Polygon"! It is played using triangulations of a regular *n*-edge polygon. The goal is to transform one triangulation to another by some tricky rules.
Triangulation of an *n*-edge poylgon is a set of *n*<=-<=3 diagonals satisfying the condition that no two diagonals share a common internal point.
For example, the initial state of the game may look like (a) in above figure. And your goal may look like (c). In each step you can choose a diagonal inside the polygon (but not the one of edges of the polygon) and flip this diagonal.
Suppose you are going to flip a diagonal *a*<=β<=*b*. There always exist two triangles sharing *a*<=β<=*b* as a side, let's denote them as *a*<=β<=*b*<=β<=*c* and *a*<=β<=*b*<=β<=*d*. As a result of this operation, the diagonal *a*<=β<=*b* is replaced by a diagonal *c*<=β<=*d*. It can be easily proven that after flip operation resulting set of diagonals is still a triangulation of the polygon.
So in order to solve above case, you may first flip diagonal 6<=β<=3, it will be replaced by diagonal 2<=β<=4. Then you flip diagonal 6<=β<=4 and get figure (c) as result.
Ciel just proved that for any starting and destination triangulations this game has a solution. She wants you to solve it in no more than 20<=000 steps for any puzzle satisfying *n*<=β€<=1000. | The first line contain an integer *n* (4<=β€<=*n*<=β€<=1000), number of edges of the regular polygon.
Then follows two groups of (*n*<=-<=3) lines describing the original triangulation and goal triangulation.
Description of each triangulation consists of (*n*<=-<=3) lines. Each line contains 2 integers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*), describing a diagonal *a**i*<=β<=*b**i*.
It is guaranteed that both original and goal triangulations are correct (i. e. no two diagonals share a common internal point in both of these triangulations). | First, output an integer *k* (0<=β€<=*k*<=β€<=20,<=000): number of steps.
Then output *k* lines, each containing 2 integers *a**i* and *b**i*: the endpoints of a diagonal you are going to flip at step *i*. You may output *a**i* and *b**i* in any order.
If there are several possible solutions, output any of them. | [
"4\n1 3\n2 4\n",
"6\n2 6\n3 6\n4 6\n6 2\n5 2\n4 2\n",
"8\n7 1\n2 7\n7 3\n6 3\n4 6\n6 1\n6 2\n6 3\n6 4\n6 8\n"
] | [
"1\n1 3\n",
"2\n6 3\n6 4\n",
"3\n7 3\n7 2\n7 1"
] | Sample test 2 is discussed above and shown on the picture. | [
{
"input": "4\n1 3\n2 4",
"output": "1\n3 1"
},
{
"input": "6\n2 6\n3 6\n4 6\n6 2\n5 2\n4 2",
"output": "5\n6 2\n6 4\n3 1\n6 3\n5 3"
},
{
"input": "8\n7 1\n2 7\n7 3\n6 3\n4 6\n6 1\n6 2\n6 3\n6 4\n6 8",
"output": "8\n6 3\n7 3\n7 2\n7 1\n7 4\n8 4\n4 1\n4 2"
},
{
"input": "5\n5 ... | 30 | 0 | 0 | 4,104 | |
964 | Splits | [
"math"
] | null | null | Let's define a split of $n$ as a nonincreasing sequence of positive integers, the sum of which is $n$.
For example, the following sequences are splits of $8$: $[4, 4]$, $[3, 3, 2]$, $[2, 2, 1, 1, 1, 1]$, $[5, 2, 1]$.
The following sequences aren't splits of $8$: $[1, 7]$, $[5, 4]$, $[11, -3]$, $[1, 1, 4, 1, 1]$.
The weight of a split is the number of elements in the split that are equal to the first element. For example, the weight of the split $[1, 1, 1, 1, 1]$ is $5$, the weight of the split $[5, 5, 3, 3, 3]$ is $2$ and the weight of the split $[9]$ equals $1$.
For a given $n$, find out the number of different weights of its splits. | The first line contains one integer $n$ ($1 \leq n \leq 10^9$). | Output one integerΒ β the answer to the problem. | [
"7\n",
"8\n",
"9\n"
] | [
"4\n",
"5\n",
"5\n"
] | In the first sample, there are following possible weights of splits of $7$:
Weight 1: [$\textbf 7$]
Weight 2: [$\textbf 3$, $\textbf 3$, 1]
Weight 3: [$\textbf 2$, $\textbf 2$, $\textbf 2$, 1]
Weight 7: [$\textbf 1$, $\textbf 1$, $\textbf 1$, $\textbf 1$, $\textbf 1$, $\textbf 1$, $\textbf 1$] | [
{
"input": "7",
"output": "4"
},
{
"input": "8",
"output": "5"
},
{
"input": "9",
"output": "5"
},
{
"input": "1",
"output": "1"
},
{
"input": "286",
"output": "144"
},
{
"input": "48",
"output": "25"
},
{
"input": "941",
"output": "471... | 124 | 307,200 | 3 | 4,105 | |
220 | Little Elephant and Array | [
"constructive algorithms",
"data structures"
] | null | null | The Little Elephant loves playing with arrays. He has array *a*, consisting of *n* positive integers, indexed from 1 to *n*. Let's denote the number with index *i* as *a**i*.
Additionally the Little Elephant has *m* queries to the array, each query is characterised by a pair of integers *l**j* and *r**j* (1<=β€<=*l**j*<=β€<=*r**j*<=β€<=*n*). For each query *l**j*,<=*r**j* the Little Elephant has to count, how many numbers *x* exist, such that number *x* occurs exactly *x* times among numbers *a**l**j*,<=*a**l**j*<=+<=1,<=...,<=*a**r**j*.
Help the Little Elephant to count the answers to all queries. | The first line contains two space-separated integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=105) β the size of array *a* and the number of queries to it. The next line contains *n* space-separated positive integers *a*1, *a*2, ..., *a**n* (1<=β€<=*a**i*<=β€<=109). Next *m* lines contain descriptions of queries, one per line. The *j*-th of these lines contains the description of the *j*-th query as two space-separated integers *l**j* and *r**j* (1<=β€<=*l**j*<=β€<=*r**j*<=β€<=*n*). | In *m* lines print *m* integers β the answers to the queries. The *j*-th line should contain the answer to the *j*-th query. | [
"7 2\n3 1 2 2 3 3 7\n1 7\n3 4\n"
] | [
"3\n1\n"
] | none | [
{
"input": "7 2\n3 1 2 2 3 3 7\n1 7\n3 4",
"output": "3\n1"
},
{
"input": "6 6\n1 2 2 3 3 3\n1 2\n2 2\n1 3\n2 4\n4 6\n1 6",
"output": "1\n0\n2\n1\n1\n3"
},
{
"input": "1 2\n1\n1 1\n1 1",
"output": "1\n1"
},
{
"input": "1 1\n1000000000\n1 1",
"output": "0"
}
] | 46 | 7,168,000 | 0 | 4,111 | |
16 | Monitor | [
"binary search",
"number theory"
] | C. Monitor | 0 | 64 | Reca company makes monitors, the most popular of their models is AB999 with the screen size *a*<=Γ<=*b* centimeters. Because of some production peculiarities a screen parameters are integer numbers. Recently the screen sides ratio *x*:<=*y* became popular with users. That's why the company wants to reduce monitor AB999 size so that its screen sides ratio becomes *x*:<=*y*, at the same time they want its total area to be maximal of all possible variants. Your task is to find the screen parameters of the reduced size model, or find out that such a reduction can't be performed. | The first line of the input contains 4 integers β *a*, *b*, *x* and *y* (1<=β€<=*a*,<=*b*,<=*x*,<=*y*<=β€<=2Β·109). | If the answer exists, output 2 positive integers β screen parameters of the reduced size model. Output 0 0 otherwise. | [
"800 600 4 3\n",
"1920 1200 16 9\n",
"1 1 1 2\n"
] | [
"800 600\n",
"1920 1080\n",
"0 0\n"
] | none | [
{
"input": "800 600 4 3",
"output": "800 600"
},
{
"input": "1920 1200 16 9",
"output": "1920 1080"
},
{
"input": "1 1 1 2",
"output": "0 0"
},
{
"input": "1002105126 227379125 179460772 1295256518",
"output": "0 0"
},
{
"input": "625166755 843062051 1463070160 19... | 500 | 0 | 0 | 4,119 |
171 | Broken checker | [
"*special",
"brute force"
] | null | null | "This problem is rubbish! There is not statement, and there are only 5 test cases. The problemsetter took liberties with this problem!" β people complained in the comments to one round on Codeforces. And even more... No, wait, the checker for the problem was alright, that's a mercy. | The only line of the input contains an integer between 1 and 5, inclusive. All tests for this problem are different. The contents of the test case doesn't need to be equal to its index. | The only line of the output contains an integer between 1 and 3, inclusive. | [] | [] | This problem has no samples, since there so few test cases. | [
{
"input": "3",
"output": "1"
},
{
"input": "1",
"output": "2"
},
{
"input": "4",
"output": "2"
},
{
"input": "2",
"output": "3"
},
{
"input": "5",
"output": "1"
}
] | 186 | 409,600 | 0 | 4,137 | |
583 | Asphalting Roads | [
"implementation"
] | null | null | City X consists of *n* vertical and *n* horizontal infinite roads, forming *n*<=Γ<=*n* intersections. Roads (both vertical and horizontal) are numbered from 1 to *n*, and the intersections are indicated by the numbers of the roads that form them.
Sand roads have long been recognized out of date, so the decision was made to asphalt them. To do this, a team of workers was hired and a schedule of work was made, according to which the intersections should be asphalted.
Road repairs are planned for *n*2 days. On the *i*-th day of the team arrives at the *i*-th intersection in the list and if none of the two roads that form the intersection were already asphalted they asphalt both roads. Otherwise, the team leaves the intersection, without doing anything with the roads.
According to the schedule of road works tell in which days at least one road will be asphalted. | The first line contains integer *n* (1<=β€<=*n*<=β€<=50) β the number of vertical and horizontal roads in the city.
Next *n*2 lines contain the order of intersections in the schedule. The *i*-th of them contains two numbers *h**i*,<=*v**i* (1<=β€<=*h**i*,<=*v**i*<=β€<=*n*), separated by a space, and meaning that the intersection that goes *i*-th in the timetable is at the intersection of the *h**i*-th horizontal and *v**i*-th vertical roads. It is guaranteed that all the intersections in the timetable are distinct. | In the single line print the numbers of the days when road works will be in progress in ascending order. The days are numbered starting from 1. | [
"2\n1 1\n1 2\n2 1\n2 2\n",
"1\n1 1\n"
] | [
"1 4 \n",
"1 \n"
] | In the sample the brigade acts like that:
1. On the first day the brigade comes to the intersection of the 1-st horizontal and the 1-st vertical road. As none of them has been asphalted, the workers asphalt the 1-st vertical and the 1-st horizontal road; 1. On the second day the brigade of the workers comes to the intersection of the 1-st horizontal and the 2-nd vertical road. The 2-nd vertical road hasn't been asphalted, but as the 1-st horizontal road has been asphalted on the first day, the workers leave and do not asphalt anything; 1. On the third day the brigade of the workers come to the intersection of the 2-nd horizontal and the 1-st vertical road. The 2-nd horizontal road hasn't been asphalted but as the 1-st vertical road has been asphalted on the first day, the workers leave and do not asphalt anything; 1. On the fourth day the brigade come to the intersection formed by the intersection of the 2-nd horizontal and 2-nd vertical road. As none of them has been asphalted, the workers asphalt the 2-nd vertical and the 2-nd horizontal road. | [
{
"input": "2\n1 1\n1 2\n2 1\n2 2",
"output": "1 4 "
},
{
"input": "1\n1 1",
"output": "1 "
},
{
"input": "2\n1 1\n2 2\n1 2\n2 1",
"output": "1 2 "
},
{
"input": "2\n1 2\n2 2\n2 1\n1 1",
"output": "1 3 "
},
{
"input": "3\n2 2\n1 2\n3 2\n3 3\n1 1\n2 3\n1 3\n3 1\n2 ... | 77 | 5,120,000 | 3 | 4,142 | |
0 | none | [
"none"
] | 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"
},
{... | 62 | 0 | 0 | 4,149 | |
600 | Extract Numbers | [
"implementation",
"strings"
] | null | null | You are given string *s*. Let's call word any largest sequence of consecutive symbols without symbols ',' (comma) and ';' (semicolon). For example, there are four words in string "aba,123;1a;0": "aba", "123", "1a", "0". A word can be empty: for example, the string *s*=";;" contains three empty words separated by ';'.
You should find all words in the given string that are nonnegative INTEGER numbers without leading zeroes and build by them new string *a*. String *a* should contain all words that are numbers separating them by ',' (the order of numbers should remain the same as in the string *s*). By all other words you should build string *b* in the same way (the order of numbers should remain the same as in the string *s*).
Here strings "101", "0" are INTEGER numbers, but "01" and "1.0" are not.
For example, for the string aba,123;1a;0 the string *a* would be equal to "123,0" and string *b* would be equal to "aba,1a". | The only line of input contains the string *s* (1<=β€<=|*s*|<=β€<=105). The string contains only symbols '.' (ASCII 46), ',' (ASCII 44), ';' (ASCII 59), digits, lowercase and uppercase latin letters. | Print the string *a* to the first line and string *b* to the second line. Each string should be surrounded by quotes (ASCII 34).
If there are no words that are numbers print dash (ASCII 45) on the first line. If all words are numbers print dash on the second line. | [
"aba,123;1a;0\n",
"1;;01,a0,\n",
"1\n",
"a\n"
] | [
"\"123,0\"\n\"aba,1a\"\n",
"\"1\"\n\",01,a0,\"\n",
"\"1\"\n-\n",
"-\n\"a\"\n"
] | In the second example the string *s* contains five words: "1", "", "01", "a0", "". | [
{
"input": "aba,123;1a;0",
"output": "\"123,0\"\n\"aba,1a\""
},
{
"input": "1;;01,a0,",
"output": "\"1\"\n\",01,a0,\""
},
{
"input": "1",
"output": "\"1\"\n-"
},
{
"input": "a",
"output": "-\n\"a\""
},
{
"input": ",;,,;",
"output": "-\n\",,,,,\""
},
{
... | 93 | 1,126,400 | 3 | 4,150 | |
134 | Average Numbers | [
"brute force",
"implementation"
] | null | null | You are given a sequence of positive integers *a*1,<=*a*2,<=...,<=*a**n*. Find all such indices *i*, that the *i*-th element equals the arithmetic mean of all other elements (that is all elements except for this one). | The first line contains the integer *n* (2<=β€<=*n*<=β€<=2Β·105). The second line contains elements of the sequence *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=1000). All the elements are positive integers. | Print on the first line the number of the sought indices. Print on the second line the sought indices in the increasing order. All indices are integers from 1 to *n*.
If the sought elements do not exist, then the first output line should contain number 0. In this case you may either not print the second line or print an empty line. | [
"5\n1 2 3 4 5\n",
"4\n50 50 50 50\n"
] | [
"1\n3 ",
"4\n1 2 3 4 "
] | none | [
{
"input": "5\n1 2 3 4 5",
"output": "1\n3 "
},
{
"input": "4\n50 50 50 50",
"output": "4\n1 2 3 4 "
},
{
"input": "3\n2 3 1",
"output": "1\n1 "
},
{
"input": "2\n4 2",
"output": "0"
},
{
"input": "2\n1 1",
"output": "2\n1 2 "
},
{
"input": "10\n3 3 3 ... | 1,000 | 5,529,600 | 0 | 4,152 | |
949 | Zebras | [
"greedy"
] | null | null | Oleg writes down the history of the days he lived. For each day he decides if it was good or bad. Oleg calls a non-empty sequence of days a zebra, if it starts with a bad day, ends with a bad day, and good and bad days are alternating in it. Let us denote bad days as 0 and good days as 1. Then, for example, sequences of days 0, 010, 01010 are zebras, while sequences 1, 0110, 0101 are not.
Oleg tells you the story of days he lived in chronological order in form of string consisting of 0 and 1. Now you are interested if it is possible to divide Oleg's life history into several subsequences, each of which is a zebra, and the way it can be done. Each day must belong to exactly one of the subsequences. For each of the subsequences, days forming it must be ordered chronologically. Note that subsequence does not have to be a group of consecutive days. | In the only line of input data there is a non-empty string *s* consisting of characters 0 and 1, which describes the history of Oleg's life. Its length (denoted as |*s*|) does not exceed 200<=000 characters. | If there is a way to divide history into zebra subsequences, in the first line of output you should print an integer *k* (1<=β€<=*k*<=β€<=|*s*|), the resulting number of subsequences. In the *i*-th of following *k* lines first print the integer *l**i* (1<=β€<=*l**i*<=β€<=|*s*|), which is the length of the *i*-th subsequence, and then *l**i* indices of days forming the subsequence. Indices must follow in ascending order. Days are numbered starting from 1. Each index from 1 to *n* must belong to exactly one subsequence. If there is no way to divide day history into zebra subsequences, print -1.
Subsequences may be printed in any order. If there are several solutions, you may print any of them. You do not have to minimize nor maximize the value of *k*. | [
"0010100\n",
"111\n"
] | [
"3\n3 1 3 4\n3 2 5 6\n1 7\n",
"-1\n"
] | none | [
{
"input": "0010100",
"output": "3\n1 1\n5 2 3 4 5 6\n1 7"
},
{
"input": "111",
"output": "-1"
},
{
"input": "0",
"output": "1\n1 1"
},
{
"input": "1",
"output": "-1"
},
{
"input": "0101010101",
"output": "-1"
},
{
"input": "010100001",
"output": "... | 374 | 30,208,000 | 0 | 4,159 | |
884 | Binary Matrix | [
"dsu"
] | null | null | You are given a matrix of size *n*<=Γ<=*m*. Each element of the matrix is either 1 or 0. You have to determine the number of connected components consisting of 1's. Two cells belong to the same component if they have a common border, and both elements in these cells are 1's.
Note that the memory limit is unusual! | The first line contains two numbers *n* and *m* (1<=β€<=*n*<=β€<=212, 4<=β€<=*m*<=β€<=214) β the number of rows and columns, respectively. It is guaranteed that *m* is divisible by 4.
Then the representation of matrix follows. Each of *n* next lines contains one-digit hexadecimal numbers (that is, these numbers can be represented either as digits from 0 to 9 or as uppercase Latin letters from *A* to *F*). Binary representation of each of these numbers denotes next 4 elements of the matrix in the corresponding row. For example, if the number *B* is given, then the corresponding elements are 1011, and if the number is 5, then the corresponding elements are 0101.
Elements are not separated by whitespaces. | Print the number of connected components consisting of 1's. | [
"3 4\n1\nA\n8\n",
"2 8\n5F\nE3\n",
"1 4\n0\n"
] | [
"3\n",
"2\n",
"0\n"
] | In the first example the matrix is:
It is clear that it has three components.
The second example:
It is clear that the number of components is 2.
There are no 1's in the third example, so the answer is 0. | [
{
"input": "3 4\n1\nA\n8",
"output": "3"
},
{
"input": "2 8\n5F\nE3",
"output": "2"
},
{
"input": "1 4\n0",
"output": "0"
},
{
"input": "1 4\nD",
"output": "2"
},
{
"input": "10 120\n4100B3BC23752433106B89343D9BA9\nD0412141283A93738E2805121044D9\nB111606365A975606... | 139 | 409,600 | -1 | 4,161 | |
762 | USB vs. PS/2 | [
"greedy",
"implementation",
"sortings",
"two pointers"
] | null | null | Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis!
The computers bought for the room were different. Some of them had only USB ports, someΒ β only PS/2 ports, and some had both options.
You have found a price list of a certain computer shop. In it, for *m* mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once.
You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. | The first line contains three integers *a*, *b* and *c* (0<=β€<=*a*,<=*b*,<=*c*<=β€<=105) Β β the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively.
The next line contains one integer *m* (0<=β€<=*m*<=β€<=3Β·105) Β β the number of mouses in the price list.
The next *m* lines each describe another mouse. The *i*-th line contains first integer *val**i* (1<=β€<=*val**i*<=β€<=109) Β β the cost of the *i*-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. | Output two integers separated by spaceΒ β the number of equipped computers and the total cost of the mouses you will buy. | [
"2 1 1\n4\n5 USB\n6 PS/2\n3 PS/2\n7 PS/2\n"
] | [
"3 14\n"
] | In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports. | [
{
"input": "2 1 1\n4\n5 USB\n6 PS/2\n3 PS/2\n7 PS/2",
"output": "3 14"
},
{
"input": "1 4 4\n12\n36949214 USB\n683538043 USB\n595594834 PS/2\n24951774 PS/2\n131512123 USB\n327575645 USB\n30947411 USB\n916758386 PS/2\n474310330 USB\n350512489 USB\n281054887 USB\n875326145 USB",
"output": "8 23453... | 2,000 | 70,860,800 | 0 | 4,169 | |
448 | Multiplication Table | [
"binary search",
"brute force"
] | null | null | Bizon the Champion isn't just charming, he also is very smart.
While some of us were learning the multiplication table, Bizon the Champion had fun in his own manner. Bizon the Champion painted an *n*<=Γ<=*m* multiplication table, where the element on the intersection of the *i*-th row and *j*-th column equals *i*Β·*j* (the rows and columns of the table are numbered starting from 1). Then he was asked: what number in the table is the *k*-th largest number? Bizon the Champion always answered correctly and immediately. Can you repeat his success?
Consider the given multiplication table. If you write out all *n*Β·*m* numbers from the table in the non-decreasing order, then the *k*-th number you write out is called the *k*-th largest number. | The single line contains integers *n*, *m* and *k* (1<=β€<=*n*,<=*m*<=β€<=5Β·105;Β 1<=β€<=*k*<=β€<=*n*Β·*m*). | Print the *k*-th largest number in a *n*<=Γ<=*m* multiplication table. | [
"2 2 2\n",
"2 3 4\n",
"1 10 5\n"
] | [
"2\n",
"3\n",
"5\n"
] | A 2βΓβ3 multiplication table looks like this: | [
{
"input": "2 2 2",
"output": "2"
},
{
"input": "2 3 4",
"output": "3"
},
{
"input": "1 10 5",
"output": "5"
},
{
"input": "1 1 1",
"output": "1"
},
{
"input": "10 1 7",
"output": "7"
},
{
"input": "10 10 33",
"output": "14"
},
{
"input": "... | 1,000 | 0 | 0 | 4,170 | |
747 | Servers | [
"implementation"
] | null | null | There are *n* servers in a laboratory, each of them can perform tasks. Each server has a unique idΒ β integer from 1 to *n*.
It is known that during the day *q* tasks will come, the *i*-th of them is characterized with three integers: *t**i*Β β the moment in seconds in which the task will come, *k**i*Β β the number of servers needed to perform it, and *d**i*Β β the time needed to perform this task in seconds. All *t**i* are distinct.
To perform the *i*-th task you need *k**i* servers which are unoccupied in the second *t**i*. After the servers begin to perform the task, each of them will be busy over the next *d**i* seconds. Thus, they will be busy in seconds *t**i*,<=*t**i*<=+<=1,<=...,<=*t**i*<=+<=*d**i*<=-<=1. For performing the task, *k**i* servers with the smallest ids will be chosen from all the unoccupied servers. If in the second *t**i* there are not enough unoccupied servers, the task is ignored.
Write the program that determines which tasks will be performed and which will be ignored. | The first line contains two positive integers *n* and *q* (1<=β€<=*n*<=β€<=100, 1<=β€<=*q*<=β€<=105) β the number of servers and the number of tasks.
Next *q* lines contains three integers each, the *i*-th line contains integers *t**i*, *k**i* and *d**i* (1<=β€<=*t**i*<=β€<=106, 1<=β€<=*k**i*<=β€<=*n*, 1<=β€<=*d**i*<=β€<=1000)Β β the moment in seconds in which the *i*-th task will come, the number of servers needed to perform it, and the time needed to perform this task in seconds. The tasks are given in a chronological order and they will come in distinct seconds. | Print *q* lines. If the *i*-th task will be performed by the servers, print in the *i*-th line the sum of servers' ids on which this task will be performed. Otherwise, print -1. | [
"4 3\n1 3 2\n2 2 1\n3 4 3\n",
"3 2\n3 2 3\n5 1 2\n",
"8 6\n1 3 20\n4 2 1\n6 5 5\n10 1 1\n15 3 6\n21 8 8\n"
] | [
"6\n-1\n10\n",
"3\n3\n",
"6\n9\n30\n-1\n15\n36\n"
] | In the first example in the second 1 the first task will come, it will be performed on the servers with ids 1, 2 and 3 (the sum of the ids equals 6) during two seconds. In the second 2 the second task will come, it will be ignored, because only the server 4 will be unoccupied at that second. In the second 3 the third task will come. By this time, servers with the ids 1, 2 and 3 will be unoccupied again, so the third task will be done on all the servers with the ids 1, 2, 3 and 4 (the sum of the ids is 10).
In the second example in the second 3 the first task will come, it will be performed on the servers with ids 1 and 2 (the sum of the ids is 3) during three seconds. In the second 5 the second task will come, it will be performed on the server 3, because the first two servers will be busy performing the first task. | [
{
"input": "4 3\n1 3 2\n2 2 1\n3 4 3",
"output": "6\n-1\n10"
},
{
"input": "3 2\n3 2 3\n5 1 2",
"output": "3\n3"
},
{
"input": "8 6\n1 3 20\n4 2 1\n6 5 5\n10 1 1\n15 3 6\n21 8 8",
"output": "6\n9\n30\n-1\n15\n36"
},
{
"input": "4 1\n6 1 1",
"output": "1"
},
{
"inp... | 997 | 8,601,600 | 0 | 4,174 | |
863 | 1-2-3 | [
"graphs",
"implementation"
] | null | null | Ilya is working for the company that constructs robots. Ilya writes programs for entertainment robots, and his current project is "Bob", a new-generation game robot. Ilya's boss wants to know his progress so far. Especially he is interested if Bob is better at playing different games than the previous model, "Alice".
So now Ilya wants to compare his robots' performance in a simple game called "1-2-3". This game is similar to the "Rock-Paper-Scissors" game: both robots secretly choose a number from the set {1,<=2,<=3} and say it at the same moment. If both robots choose the same number, then it's a draw and noone gets any points. But if chosen numbers are different, then one of the robots gets a point: 3 beats 2, 2 beats 1 and 1 beats 3.
Both robots' programs make them choose their numbers in such a way that their choice in (*i*<=+<=1)-th game depends only on the numbers chosen by them in *i*-th game.
Ilya knows that the robots will play *k* games, Alice will choose number *a* in the first game, and Bob will choose *b* in the first game. He also knows both robots' programs and can tell what each robot will choose depending on their choices in previous game. Ilya doesn't want to wait until robots play all *k* games, so he asks you to predict the number of points they will have after the final game. | The first line contains three numbers *k*, *a*, *b* (1<=β€<=*k*<=β€<=1018, 1<=β€<=*a*,<=*b*<=β€<=3).
Then 3 lines follow, *i*-th of them containing 3 numbers *A**i*,<=1, *A**i*,<=2, *A**i*,<=3, where *A**i*,<=*j* represents Alice's choice in the game if Alice chose *i* in previous game and Bob chose *j* (1<=β€<=*A**i*,<=*j*<=β€<=3).
Then 3 lines follow, *i*-th of them containing 3 numbers *B**i*,<=1, *B**i*,<=2, *B**i*,<=3, where *B**i*,<=*j* represents Bob's choice in the game if Alice chose *i* in previous game and Bob chose *j* (1<=β€<=*B**i*,<=*j*<=β€<=3). | Print two numbers. First of them has to be equal to the number of points Alice will have, and second of them must be Bob's score after *k* games. | [
"10 2 1\n1 1 1\n1 1 1\n1 1 1\n2 2 2\n2 2 2\n2 2 2\n",
"8 1 1\n2 2 1\n3 3 1\n3 1 3\n1 1 1\n2 1 1\n1 2 3\n",
"5 1 1\n1 2 2\n2 2 2\n2 2 2\n1 2 2\n2 2 2\n2 2 2\n"
] | [
"1 9\n",
"5 2\n",
"0 0\n"
] | In the second example game goes like this:
<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/1e21b6e200707470571d69c9946ace6b56f5279b.png" style="max-width: 100.0%;max-height: 100.0%;"/>
The fourth and the seventh game are won by Bob, the first game is draw and the rest are won by Alice. | [
{
"input": "10 2 1\n1 1 1\n1 1 1\n1 1 1\n2 2 2\n2 2 2\n2 2 2",
"output": "1 9"
},
{
"input": "8 1 1\n2 2 1\n3 3 1\n3 1 3\n1 1 1\n2 1 1\n1 2 3",
"output": "5 2"
},
{
"input": "5 1 1\n1 2 2\n2 2 2\n2 2 2\n1 2 2\n2 2 2\n2 2 2",
"output": "0 0"
},
{
"input": "1 1 1\n3 3 1\n1 1 1\... | 1,000 | 5,529,600 | 0 | 4,177 | |
514 | Han Solo and Lazer Gun | [
"brute force",
"data structures",
"geometry",
"implementation",
"math"
] | null | null | There are *n* Imperial stormtroopers on the field. The battle field is a plane with Cartesian coordinate system. Each stormtrooper is associated with his coordinates (*x*,<=*y*) on this plane.
Han Solo has the newest duplex lazer gun to fight these stormtroopers. It is situated at the point (*x*0,<=*y*0). In one shot it can can destroy all the stormtroopers, situated on some line that crosses point (*x*0,<=*y*0).
Your task is to determine what minimum number of shots Han Solo needs to defeat all the stormtroopers.
The gun is the newest invention, it shoots very quickly and even after a very large number of shots the stormtroopers don't have enough time to realize what's happening and change their location. | The first line contains three integers *n*, *x*0 ΠΈ *y*0 (1<=β€<=*n*<=β€<=1000, <=-<=104<=β€<=*x*0,<=*y*0<=β€<=104) β the number of stormtroopers on the battle field and the coordinates of your gun.
Next *n* lines contain two integers each *x**i*, *y**i* (<=-<=104<=β€<=*x**i*,<=*y**i*<=β€<=104) β the coordinates of the stormtroopers on the battlefield. It is guaranteed that no stormtrooper stands at the same point with the gun. Multiple stormtroopers can stand at the same point. | Print a single integer β the minimum number of shots Han Solo needs to destroy all the stormtroopers. | [
"4 0 0\n1 1\n2 2\n2 0\n-1 -1\n",
"2 1 2\n1 1\n1 0\n"
] | [
"2\n",
"1\n"
] | Explanation to the first and second samples from the statement, respectively: | [
{
"input": "4 0 0\n1 1\n2 2\n2 0\n-1 -1",
"output": "2"
},
{
"input": "2 1 2\n1 1\n1 0",
"output": "1"
},
{
"input": "1 1 1\n0 0",
"output": "1"
},
{
"input": "2 0 0\n10000 -10000\n-10000 10000",
"output": "1"
},
{
"input": "2 0 0\n10000 -10000\n10000 10000",
... | 140 | 3,788,800 | 3 | 4,182 | |
895 | XK Segments | [
"binary search",
"math",
"sortings",
"two pointers"
] | null | null | While Vasya finished eating his piece of pizza, the lesson has already started. For being late for the lesson, the teacher suggested Vasya to solve one interesting problem. Vasya has an array *a* and integer *x*. He should find the number of different ordered pairs of indexes (*i*,<=*j*) such that *a**i*<=β€<=*a**j* and there are exactly *k* integers *y* such that *a**i*<=β€<=*y*<=β€<=*a**j* and *y* is divisible by *x*.
In this problem it is meant that pair (*i*,<=*j*) is equal to (*j*,<=*i*) only if *i* is equal to *j*. For example pair (1,<=2) is not the same as (2,<=1). | The first line contains 3 integers *n*,<=*x*,<=*k* (1<=β€<=*n*<=β€<=105,<=1<=β€<=*x*<=β€<=109,<=0<=β€<=*k*<=β€<=109), where *n* is the size of the array *a* and *x* and *k* are numbers from the statement.
The second line contains *n* integers *a**i* (1<=β€<=*a**i*<=β€<=109)Β β the elements of the array *a*. | Print one integerΒ β the answer to the problem. | [
"4 2 1\n1 3 5 7\n",
"4 2 0\n5 3 1 7\n",
"5 3 1\n3 3 3 3 3\n"
] | [
"3\n",
"4\n",
"25\n"
] | In first sample there are only three suitable pairs of indexesΒ β (1,β2),β(2,β3),β(3,β4).
In second sample there are four suitable pairs of indexes(1,β1),β(2,β2),β(3,β3),β(4,β4).
In third sample every pair (*i*,β*j*) is suitable, so the answer is 5β*β5β=β25. | [
{
"input": "4 2 1\n1 3 5 7",
"output": "3"
},
{
"input": "4 2 0\n5 3 1 7",
"output": "4"
},
{
"input": "5 3 1\n3 3 3 3 3",
"output": "25"
},
{
"input": "5 3 4\n24 13 1 24 24",
"output": "4"
},
{
"input": "4 2 2\n1 3 5 7",
"output": "2"
},
{
"input": "5... | 358 | 14,438,400 | 3 | 4,189 | |
17 | Balance | [
"dp"
] | C. Balance | 3 | 128 | Nick likes strings very much, he likes to rotate them, sort them, rearrange characters within a string... Once he wrote a random string of characters a, b, c on a piece of paper and began to perform the following operations:
- to take two adjacent characters and replace the second character with the first one, - to take two adjacent characters and replace the first character with the second one
To understand these actions better, let's take a look at a string Β«abcΒ». All of the following strings can be obtained by performing one of the described operations on Β«abcΒ»: Β«bbcΒ», Β«abbΒ», Β«accΒ». Let's denote the frequency of a character for each of the characters a, b and c as the number of occurrences of this character in the string. For example, for string Β«abcΒ»: |*a*| = 1, |*b*| = 1, |*c*| = 1, and for string Β«bbcΒ»: |*a*| = 0, |*b*| = 2, |*c*| = 1.
While performing the described operations, Nick sometimes got balanced strings. Let's say that a string is balanced, if the frequencies of each character differ by at most 1. That is <=-<=1<=β€<=|*a*|<=-<=|*b*|<=β€<=1, <=-<=1<=β€<=|*a*|<=-<=|*c*|<=β€<=1 ΠΈ <=-<=1<=β€<=|*b*|<=-<=|*c*|<=β€<=1.
Would you help Nick find the number of different balanced strings that can be obtained by performing the operations described above, perhaps multiple times, on the given string *s*. This number should be calculated modulo 51123987. | The first line contains integer *n* (1<=β€<=*n*<=β€<=150) β the length of the given string *s*. Next line contains the given string *s*. The initial string can be balanced as well, in this case it should be counted too. The given string *s* consists only of characters a, b and c. | Output the only number β the number of different balanced strings that can be obtained by performing the described operations, perhaps multiple times, on the given string *s*, modulo 51123987. | [
"4\nabca\n",
"4\nabbc\n",
"2\nab\n"
] | [
"7\n",
"3\n",
"1\n"
] | In the first sample it is possible to get 51 different strings through the described operations, but only 7 of them are balanced: Β«abcaΒ», Β«bbcaΒ», Β«bccaΒ», Β«bcaaΒ», Β«abccΒ», Β«abbcΒ», Β«aabcΒ». In the second sample: Β«abbcΒ», Β«aabcΒ», Β«abccΒ». In the third sample there is only one balanced string β Β«abΒ» itself. | [] | 184 | 2,150,400 | -1 | 4,200 |
558 | Amr and Chemistry | [
"brute force",
"graphs",
"greedy",
"math",
"shortest paths"
] | null | null | Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experiment.
Amr has *n* different types of chemicals. Each chemical *i* has an initial volume of *a**i* liters. For this experiment, Amr has to mix all the chemicals together, but all the chemicals volumes must be equal first. So his task is to make all the chemicals volumes equal.
To do this, Amr can do two different kind of operations.
- Choose some chemical *i* and double its current volume so the new volume will be 2*a**i* - Choose some chemical *i* and divide its volume by two (integer division) so the new volume will be
Suppose that each chemical is contained in a vessel of infinite volume. Now Amr wonders what is the minimum number of operations required to make all the chemicals volumes equal? | The first line contains one number *n* (1<=β€<=*n*<=β€<=105), the number of chemicals.
The second line contains *n* space separated integers *a**i* (1<=β€<=*a**i*<=β€<=105), representing the initial volume of the *i*-th chemical in liters. | Output one integer the minimum number of operations required to make all the chemicals volumes equal. | [
"3\n4 8 2\n",
"3\n3 5 6\n"
] | [
"2",
"5"
] | In the first sample test, the optimal solution is to divide the second chemical volume by two, and multiply the third chemical volume by two to make all the volumes equal 4.
In the second sample test, the optimal solution is to divide the first chemical volume by two, and divide the second and the third chemical volumes by two twice to make all the volumes equal 1. | [
{
"input": "3\n4 8 2",
"output": "2"
},
{
"input": "3\n3 5 6",
"output": "5"
},
{
"input": "2\n50000 100000",
"output": "1"
},
{
"input": "2\n99999 99998",
"output": "2"
},
{
"input": "17\n1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536",
"o... | 93 | 0 | 0 | 4,212 | |
552 | Vanya and Table | [
"implementation",
"math"
] | null | null | Vanya has a table consisting of 100 rows, each row contains 100 cells. The rows are numbered by integers from 1 to 100 from bottom to top, the columns are numbered from 1 to 100 from left to right.
In this table, Vanya chose *n* rectangles with sides that go along borders of squares (some rectangles probably occur multiple times). After that for each cell of the table he counted the number of rectangles it belongs to and wrote this number into it. Now he wants to find the sum of values in all cells of the table and as the table is too large, he asks you to help him find the result. | The first line contains integer *n* (1<=β€<=*n*<=β€<=100) β the number of rectangles.
Each of the following *n* lines contains four integers *x*1,<=*y*1,<=*x*2,<=*y*2 (1<=β€<=*x*1<=β€<=*x*2<=β€<=100, 1<=β€<=*y*1<=β€<=*y*2<=β€<=100), where *x*1 and *y*1 are the number of the column and row of the lower left cell and *x*2 and *y*2 are the number of the column and row of the upper right cell of a rectangle. | In a single line print the sum of all values in the cells of the table. | [
"2\n1 1 2 3\n2 2 3 3\n",
"2\n1 1 3 3\n1 1 3 3\n"
] | [
"10\n",
"18\n"
] | Note to the first sample test:
Values of the table in the first three rows and columns will be as follows:
121
121
110
So, the sum of values will be equal to 10.
Note to the second sample test:
Values of the table in the first three rows and columns will be as follows:
222
222
222
So, the sum of values will be equal to 18. | [
{
"input": "2\n1 1 2 3\n2 2 3 3",
"output": "10"
},
{
"input": "2\n1 1 3 3\n1 1 3 3",
"output": "18"
},
{
"input": "5\n4 11 20 15\n7 5 12 20\n10 8 16 12\n7 5 12 15\n2 2 20 13",
"output": "510"
},
{
"input": "5\n4 11 20 20\n6 11 20 16\n5 2 19 15\n11 3 18 15\n3 2 14 11",
"o... | 155 | 2,355,200 | 3 | 4,222 | |
584 | Kolya and Tanya | [
"combinatorics"
] | null | null | Kolya loves putting gnomes at the circle table and giving them coins, and Tanya loves studying triplets of gnomes, sitting in the vertexes of an equilateral triangle.
More formally, there are 3*n* gnomes sitting in a circle. Each gnome can have from 1 to 3 coins. Let's number the places in the order they occur in the circle by numbers from 0 to 3*n*<=-<=1, let the gnome sitting on the *i*-th place have *a**i* coins. If there is an integer *i* (0<=β€<=*i*<=<<=*n*) such that *a**i*<=+<=*a**i*<=+<=*n*<=+<=*a**i*<=+<=2*n*<=β <=6, then Tanya is satisfied.
Count the number of ways to choose *a**i* so that Tanya is satisfied. As there can be many ways of distributing coins, print the remainder of this number modulo 109<=+<=7. Two ways, *a* and *b*, are considered distinct if there is index *i* (0<=β€<=*i*<=<<=3*n*), such that *a**i*<=β <=*b**i* (that is, some gnome got different number of coins in these two ways). | A single line contains number *n* (1<=β€<=*n*<=β€<=105) β the number of the gnomes divided by three. | Print a single number β the remainder of the number of variants of distributing coins that satisfy Tanya modulo 109<=+<=7. | [
"1\n",
"2\n"
] | [
"20",
"680"
] | 20 ways for *n*β=β1 (gnome with index 0 sits on the top of the triangle, gnome 1 on the right vertex, gnome 2 on the left vertex): <img class="tex-graphics" src="https://espresso.codeforces.com/64df38b85ccb482cf88d02dc52e348e33313f9da.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "1",
"output": "20"
},
{
"input": "2",
"output": "680"
},
{
"input": "3",
"output": "19340"
},
{
"input": "4",
"output": "529040"
},
{
"input": "5",
"output": "14332100"
},
{
"input": "7",
"output": "459529590"
},
{
"input": "10"... | 46 | 0 | 0 | 4,223 | |
0 | none | [
"none"
] | null | null | Limak is a little polar bear. He loves connecting with other bears via social networks. He has *n* friends and his relation with the *i*-th of them is described by a unique integer *t**i*. The bigger this value is, the better the friendship is. No two friends have the same value *t**i*.
Spring is starting and the Winter sleep is over for bears. Limak has just woken up and logged in. All his friends still sleep and thus none of them is online. Some (maybe all) of them will appear online in the next hours, one at a time.
The system displays friends who are online. On the screen there is space to display at most *k* friends. If there are more than *k* friends online then the system displays only *k* best of themΒ β those with biggest *t**i*.
Your task is to handle queries of two types:
- "1 id"Β β Friend *id* becomes online. It's guaranteed that he wasn't online before. - "2 id"Β β Check whether friend *id* is displayed by the system. Print "YES" or "NO" in a separate line.
Are you able to help Limak and answer all queries of the second type? | The first line contains three integers *n*, *k* and *q* (1<=β€<=*n*,<=*q*<=β€<=150<=000,<=1<=β€<=*k*<=β€<=*min*(6,<=*n*))Β β the number of friends, the maximum number of displayed online friends and the number of queries, respectively.
The second line contains *n* integers *t*1,<=*t*2,<=...,<=*t**n* (1<=β€<=*t**i*<=β€<=109) where *t**i* describes how good is Limak's relation with the *i*-th friend.
The *i*-th of the following *q* lines contains two integers *type**i* and *id**i* (1<=β€<=*type**i*<=β€<=2,<=1<=β€<=*id**i*<=β€<=*n*)Β β the *i*-th query. If *type**i*<==<=1 then a friend *id**i* becomes online. If *type**i*<==<=2 then you should check whether a friend *id**i* is displayed.
It's guaranteed that no two queries of the first type will have the same *id**i* becuase one friend can't become online twice. Also, it's guaranteed that at least one query will be of the second type (*type**i*<==<=2) so the output won't be empty. | For each query of the second type print one line with the answerΒ β "YES" (without quotes) if the given friend is displayed and "NO" (without quotes) otherwise. | [
"4 2 8\n300 950 500 200\n1 3\n2 4\n2 3\n1 1\n1 2\n2 1\n2 2\n2 3\n",
"6 3 9\n50 20 51 17 99 24\n1 3\n1 4\n1 5\n1 2\n2 4\n2 2\n1 1\n2 4\n2 3\n"
] | [
"NO\nYES\nNO\nYES\nYES\n",
"NO\nYES\nNO\nYES\n"
] | In the first sample, Limak has 4 friends who all sleep initially. At first, the system displays nobody because nobody is online. There are the following 8 queries:
1. "1 3"Β β Friend 3 becomes online. 1. "2 4"Β β We should check if friend 4 is displayed. He isn't even online and thus we print "NO". 1. "2 3"Β β We should check if friend 3 is displayed. Right now he is the only friend online and the system displays him. We should print "YES". 1. "1 1"Β β Friend 1 becomes online. The system now displays both friend 1 and friend 3. 1. "1 2"Β β Friend 2 becomes online. There are 3 friends online now but we were given *k*β=β2 so only two friends can be displayed. Limak has worse relation with friend 1 than with other two online friends (*t*<sub class="lower-index">1</sub>β<β*t*<sub class="lower-index">2</sub>,β*t*<sub class="lower-index">3</sub>) so friend 1 won't be displayed 1. "2 1"Β β Print "NO". 1. "2 2"Β β Print "YES". 1. "2 3"Β β Print "YES". | [
{
"input": "4 2 8\n300 950 500 200\n1 3\n2 4\n2 3\n1 1\n1 2\n2 1\n2 2\n2 3",
"output": "NO\nYES\nNO\nYES\nYES"
},
{
"input": "6 3 9\n50 20 51 17 99 24\n1 3\n1 4\n1 5\n1 2\n2 4\n2 2\n1 1\n2 4\n2 3",
"output": "NO\nYES\nNO\nYES"
},
{
"input": "6 3 10\n62417580 78150524 410053501 582708235 ... | 2,000 | 18,022,400 | 0 | 4,231 | |
803 | Coprime Subsequences | [
"bitmasks",
"combinatorics",
"number theory"
] | null | null | Let's call a non-empty sequence of positive integers *a*1,<=*a*2... *a**k* coprime if the greatest common divisor of all elements of this sequence is equal to 1.
Given an array *a* consisting of *n* positive integers, find the number of its coprime subsequences. Since the answer may be very large, print it modulo 109<=+<=7.
Note that two subsequences are considered different if chosen indices are different. For example, in the array [1,<=1] there are 3 different subsequences: [1], [1] and [1,<=1]. | The first line contains one integer number *n* (1<=β€<=*n*<=β€<=100000).
The second line contains *n* integer numbers *a*1,<=*a*2... *a**n* (1<=β€<=*a**i*<=β€<=100000). | Print the number of coprime subsequences of *a* modulo 109<=+<=7. | [
"3\n1 2 3\n",
"4\n1 1 1 1\n",
"7\n1 3 5 15 3 105 35\n"
] | [
"5\n",
"15\n",
"100\n"
] | In the first example coprime subsequences are:
1. 1 1. 1,β2 1. 1,β3 1. 1,β2,β3 1. 2,β3
In the second example all subsequences are coprime. | [
{
"input": "3\n1 2 3",
"output": "5"
},
{
"input": "4\n1 1 1 1",
"output": "15"
},
{
"input": "7\n1 3 5 15 3 105 35",
"output": "100"
},
{
"input": "1\n1",
"output": "1"
},
{
"input": "1\n100000",
"output": "0"
},
{
"input": "5\n10 8 6 4 6",
"outpu... | 187 | 1,536,000 | 0 | 4,234 | |
27 | Ring Road 2 | [
"2-sat",
"dfs and similar",
"dsu",
"graphs"
] | D. Ring Road 2 | 2 | 256 | It is well known that Berland has *n* cities, which form the Silver ring β cities *i* and *i*<=+<=1 (1<=β€<=*i*<=<<=*n*) are connected by a road, as well as the cities *n* and 1. The goverment have decided to build *m* new roads. The list of the roads to build was prepared. Each road will connect two cities. Each road should be a curve which lies inside or outside the ring. New roads will have no common points with the ring (except the endpoints of the road).
Now the designers of the constructing plan wonder if it is possible to build the roads in such a way that no two roads intersect (note that the roads may intersect at their endpoints). If it is possible to do, which roads should be inside the ring, and which should be outside? | The first line contains two integers *n* and *m* (4<=β€<=*n*<=β€<=100,<=1<=β€<=*m*<=β€<=100). Each of the following *m* lines contains two integers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*,<=*a**i*<=β <=*b**i*). No two cities will be connected by more than one road in the list. The list will not contain the roads which exist in the Silver ring. | If it is impossible to build the roads in such a way that no two roads intersect, output Impossible. Otherwise print *m* characters. *i*-th character should be i, if the road should be inside the ring, and o if the road should be outside the ring. If there are several solutions, output any of them. | [
"4 2\n1 3\n2 4\n",
"6 3\n1 3\n3 5\n5 1\n"
] | [
"io\n",
"ooo\n"
] | none | [
{
"input": "4 1\n4 2",
"output": "o"
},
{
"input": "4 2\n1 3\n2 4",
"output": "io"
},
{
"input": "5 1\n3 5",
"output": "o"
},
{
"input": "5 2\n2 4\n4 1",
"output": "oo"
},
{
"input": "5 3\n4 2\n1 3\n5 2",
"output": "oio"
},
{
"input": "5 4\n1 3\n3 5\n1... | 124 | 102,400 | 0 | 4,236 |
340 | The Wall | [
"math"
] | null | null | Iahub and his friend Floyd have started painting a wall. Iahub is painting the wall red and Floyd is painting it pink. You can consider the wall being made of a very large number of bricks, numbered 1, 2, 3 and so on.
Iahub has the following scheme of painting: he skips *x*<=-<=1 consecutive bricks, then he paints the *x*-th one. That is, he'll paint bricks *x*, 2Β·*x*, 3Β·*x* and so on red. Similarly, Floyd skips *y*<=-<=1 consecutive bricks, then he paints the *y*-th one. Hence he'll paint bricks *y*, 2Β·*y*, 3Β·*y* and so on pink.
After painting the wall all day, the boys observed that some bricks are painted both red and pink. Iahub has a lucky number *a* and Floyd has a lucky number *b*. Boys wonder how many bricks numbered no less than *a* and no greater than *b* are painted both red and pink. This is exactly your task: compute and print the answer to the question. | The input will have a single line containing four integers in this order: *x*, *y*, *a*, *b*. (1<=β€<=*x*,<=*y*<=β€<=1000, 1<=β€<=*a*,<=*b*<=β€<=2Β·109, *a*<=β€<=*b*). | Output a single integer β the number of bricks numbered no less than *a* and no greater than *b* that are painted both red and pink. | [
"2 3 6 18\n"
] | [
"3"
] | Let's look at the bricks from *a* to *b* (*a*β=β6,β*b*β=β18). The bricks colored in red are numbered 6, 8, 10, 12, 14, 16, 18. The bricks colored in pink are numbered 6, 9, 12, 15, 18. The bricks colored in both red and pink are numbered with 6, 12 and 18. | [
{
"input": "2 3 6 18",
"output": "3"
},
{
"input": "4 6 20 201",
"output": "15"
},
{
"input": "15 27 100 10000",
"output": "74"
},
{
"input": "105 60 3456 78910",
"output": "179"
},
{
"input": "1 1 1000 100000",
"output": "99001"
},
{
"input": "3 2 5 5... | 92 | 6,963,200 | 0 | 4,240 | |
0 | none | [
"none"
] | null | null | Galya is playing one-dimensional Sea Battle on a 1<=Γ<=*n* grid. In this game *a* ships are placed on the grid. Each of the ships consists of *b* consecutive cells. No cell can be part of two ships, however, the ships can touch each other.
Galya doesn't know the ships location. She can shoot to some cells and after each shot she is told if that cell was a part of some ship (this case is called "hit") or not (this case is called "miss").
Galya has already made *k* shots, all of them were misses.
Your task is to calculate the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship.
It is guaranteed that there is at least one valid ships placement. | The first line contains four positive integers *n*, *a*, *b*, *k* (1<=β€<=*n*<=β€<=2Β·105, 1<=β€<=*a*,<=*b*<=β€<=*n*, 0<=β€<=*k*<=β€<=*n*<=-<=1)Β β the length of the grid, the number of ships on the grid, the length of each ship and the number of shots Galya has already made.
The second line contains a string of length *n*, consisting of zeros and ones. If the *i*-th character is one, Galya has already made a shot to this cell. Otherwise, she hasn't. It is guaranteed that there are exactly *k* ones in this string. | In the first line print the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship.
In the second line print the cells Galya should shoot at.
Each cell should be printed exactly once. You can print the cells in arbitrary order. The cells are numbered from 1 to *n*, starting from the left.
If there are multiple answers, you can print any of them. | [
"5 1 2 1\n00100\n",
"13 3 2 3\n1000000010001\n"
] | [
"2\n4 2\n",
"2\n7 11\n"
] | There is one ship in the first sample. It can be either to the left or to the right from the shot Galya has already made (the "1" character). So, it is necessary to make two shots: one at the left part, and one at the right part. | [
{
"input": "5 1 2 1\n00100",
"output": "2\n2 5 "
},
{
"input": "13 3 2 3\n1000000010001",
"output": "2\n3 5 "
},
{
"input": "1 1 1 0\n0",
"output": "1\n1 "
},
{
"input": "2 2 1 0\n00",
"output": "1\n1 "
},
{
"input": "5 4 1 0\n00000",
"output": "2\n1 2 "
},
... | 264 | 14,438,400 | 3 | 4,241 | |
509 | Painting Pebbles | [
"constructive algorithms",
"greedy",
"implementation"
] | null | null | There are *n* piles of pebbles on the table, the *i*-th pile contains *a**i* pebbles. Your task is to paint each pebble using one of the *k* given colors so that for each color *c* and any two piles *i* and *j* the difference between the number of pebbles of color *c* in pile *i* and number of pebbles of color *c* in pile *j* is at most one.
In other words, let's say that *b**i*,<=*c* is the number of pebbles of color *c* in the *i*-th pile. Then for any 1<=β€<=*c*<=β€<=*k*, 1<=β€<=*i*,<=*j*<=β€<=*n* the following condition must be satisfied |*b**i*,<=*c*<=-<=*b**j*,<=*c*|<=β€<=1. It isn't necessary to use all *k* colors: if color *c* hasn't been used in pile *i*, then *b**i*,<=*c* is considered to be zero. | The first line of the input contains positive integers *n* and *k* (1<=β€<=*n*,<=*k*<=β€<=100), separated by a space β the number of piles and the number of colors respectively.
The second line contains *n* positive integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=100) denoting number of pebbles in each of the piles. | If there is no way to paint the pebbles satisfying the given condition, output "NO" (without quotes) .
Otherwise in the first line output "YES" (without quotes). Then *n* lines should follow, the *i*-th of them should contain *a**i* space-separated integers. *j*-th (1<=β€<=*j*<=β€<=*a**i*) of these integers should be equal to the color of the *j*-th pebble in the *i*-th pile. If there are several possible answers, you may output any of them. | [
"4 4\n1 2 3 4\n",
"5 2\n3 2 4 1 3\n",
"5 4\n3 2 4 3 5\n"
] | [
"YES\n1\n1 4\n1 2 4\n1 2 3 4\n",
"NO\n",
"YES\n1 2 3\n1 3\n1 2 3 4\n1 3 4\n1 1 2 3 4\n"
] | none | [
{
"input": "4 4\n1 2 3 4",
"output": "YES\n1 \n1 1 \n1 1 2 \n1 1 2 3 "
},
{
"input": "5 2\n3 2 4 1 3",
"output": "NO"
},
{
"input": "5 4\n3 2 4 3 5",
"output": "YES\n1 1 1 \n1 1 \n1 1 1 2 \n1 1 1 \n1 1 1 2 3 "
},
{
"input": "4 3\n5 6 7 8",
"output": "YES\n1 1 1 1 1 \n1 1 ... | 92 | 307,200 | 3 | 4,246 | |
223 | Bracket Sequence | [
"data structures",
"expression parsing",
"implementation"
] | null | null | A bracket sequence is a string, containing only characters "(", ")", "[" and "]".
A correct bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()[]", "([])" are correct (the resulting expressions are: "(1)+[1]", "([1+1]+1)"), and "](" and "[" are not. The empty string is a correct bracket sequence by definition.
A substring *s*[*l*... *r*] (1<=β€<=*l*<=β€<=*r*<=β€<=|*s*|) of string *s*<==<=*s*1*s*2... *s*|*s*| (where |*s*| is the length of string *s*) is the string *s**l**s**l*<=+<=1... *s**r*. The empty string is a substring of any string by definition.
You are given a bracket sequence, not necessarily correct. Find its substring which is a correct bracket sequence and contains as many opening square brackets Β«[Β» as possible. | The first and the only line contains the bracket sequence as a string, consisting only of characters "(", ")", "[" and "]". It is guaranteed that the string is non-empty and its length doesn't exceed 105 characters. | In the first line print a single integer β the number of brackets Β«[Β» in the required bracket sequence. In the second line print the optimal sequence. If there are more than one optimal solutions print any of them. | [
"([])\n",
"(((\n"
] | [
"1\n([])\n",
"0\n\n"
] | none | [
{
"input": "([])",
"output": "1\n([])"
},
{
"input": "(((",
"output": "0"
},
{
"input": "(][)",
"output": "0"
},
{
"input": "(()[))()[]",
"output": "1\n()[]"
},
{
"input": "(][](](][[(][",
"output": "1\n[]"
},
{
"input": "((])(]]))(](((()[[()[[[)([]()]... | 92 | 6,963,200 | 0 | 4,248 | |
743 | Vladik and fractions | [
"brute force",
"constructive algorithms",
"math",
"number theory"
] | null | null | Vladik and Chloe decided to determine who of them is better at math. Vladik claimed that for any positive integer *n* he can represent fraction as a sum of three distinct positive fractions in form .
Help Vladik with that, i.e for a given *n* find three distinct positive integers *x*, *y* and *z* such that . Because Chloe can't check Vladik's answer if the numbers are large, he asks you to print numbers not exceeding 109.
If there is no such answer, print -1. | The single line contains single integer *n* (1<=β€<=*n*<=β€<=104). | If the answer exists, print 3 distinct numbers *x*, *y* and *z* (1<=β€<=*x*,<=*y*,<=*z*<=β€<=109, *x*<=β <=*y*, *x*<=β <=*z*, *y*<=β <=*z*). Otherwise print -1.
If there are multiple answers, print any of them. | [
"3\n",
"7\n"
] | [
"2 7 42\n",
"7 8 56\n"
] | none | [
{
"input": "3",
"output": "2 7 42"
},
{
"input": "7",
"output": "7 8 56"
},
{
"input": "2",
"output": "2 3 6"
},
{
"input": "5",
"output": "5 6 30"
},
{
"input": "4",
"output": "4 5 20"
},
{
"input": "7",
"output": "7 8 56"
},
{
"input": "8... | 78 | 7,065,600 | 3 | 4,257 | |
442 | Andrey and Problem | [
"greedy",
"math",
"probabilities"
] | null | null | Andrey needs one more problem to conduct a programming contest. He has *n* friends who are always willing to help. He can ask some of them to come up with a contest problem. Andrey knows one value for each of his fiends β the probability that this friend will come up with a problem if Andrey asks him.
Help Andrey choose people to ask. As he needs only one problem, Andrey is going to be really upset if no one comes up with a problem or if he gets more than one problem from his friends. You need to choose such a set of people that maximizes the chances of Andrey not getting upset. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=100) β the number of Andrey's friends. The second line contains *n* real numbers *p**i* (0.0<=β€<=*p**i*<=β€<=1.0) β the probability that the *i*-th friend can come up with a problem. The probabilities are given with at most 6 digits after decimal point. | Print a single real number β the probability that Andrey won't get upset at the optimal choice of friends. The answer will be considered valid if it differs from the correct one by at most 10<=-<=9. | [
"4\n0.1 0.2 0.3 0.8\n",
"2\n0.1 0.2\n"
] | [
"0.800000000000\n",
"0.260000000000\n"
] | In the first sample the best strategy for Andrey is to ask only one of his friends, the most reliable one.
In the second sample the best strategy for Andrey is to ask all of his friends to come up with a problem. Then the probability that he will get exactly one problem is 0.1Β·0.8β+β0.9Β·0.2β=β0.26. | [
{
"input": "4\n0.1 0.2 0.3 0.8",
"output": "0.800000000000"
},
{
"input": "2\n0.1 0.2",
"output": "0.260000000000"
},
{
"input": "1\n0.217266",
"output": "0.217266000000"
},
{
"input": "2\n0.608183 0.375030",
"output": "0.608183000000"
},
{
"input": "3\n0.388818 0... | 109 | 409,600 | 0 | 4,258 | |
39 | Multiplication Table | [
"implementation"
] | H. Multiplication Table | 2 | 64 | Petya studies positional notations. He has already learned to add and subtract numbers in the systems of notations with different radices and has moved on to a more complicated action β multiplication. To multiply large numbers one has to learn the multiplication table. Unfortunately, in the second grade students learn only the multiplication table of decimals (and some students even learn it in the first grade). Help Petya make a multiplication table for numbers in the system of notations with the radix *k*. | The first line contains a single integer *k* (2<=β€<=*k*<=β€<=10) β the radix of the system. | Output the multiplication table for the system of notations with the radix *k*. The table must contain *k*<=-<=1 rows and *k*<=-<=1 columns. The element on the crossing of the *i*-th row and the *j*-th column is equal to the product of *i* and *j* in the system of notations with the radix *k*. Each line may have any number of spaces between the numbers (the extra spaces in the samples are put for clarity). | [
"10\n",
"3\n"
] | [
"1 2 3 4 5 6 7 8 9\n2 4 6 8 10 12 14 16 18\n3 6 9 12 15 18 21 24 27\n4 8 12 16 20 24 28 32 36\n5 10 15 20 25 30 35 40 45\n6 12 18 24 30 36 42 48 54\n7 14 21 28 35 42 49 56 63\n8 16 24 32 40 48 56 64 72\n9 18 27 36 45 54 63 72 81\n",
"1 2\n2 11"
] | none | [
{
"input": "10",
"output": "1 2 3 4 5 6 7 8 9 \n2 4 6 8 10 12 14 16 18 \n3 6 9 12 15 18 21 24 27 \n4 8 12 16 20 24 28 32 36 \n5 10 15 20 25 30 35 40 45 \n6 12 18 24 30 36 42 48 54 \n7 14 21 28 35 42 49 56 63 \n8 16 24 32 40 48 56 64 72 \n9 18 27 36 45 54 63 72 81 "
},
{
"input": "3",
"output": "... | 218 | 0 | 3.9455 | 4,268 |
717 | Festival Organization | [
"combinatorics",
"math",
"number theory"
] | null | null | The Prodiggers are quite a cool band and for this reason, they have been the surprise guest at the ENTER festival for the past 80 years. At the beginning of their careers, they werenβt so successful, so they had to spend time digging channels to earn money; hence the name. Anyway, they like to tour a lot and have surprising amounts of energy to do extremely long tours. However, they hate spending two consecutive days without having a concert, so they would like to avoid it.
A tour is defined by a sequence of concerts and days-off. You need to count in how many ways The Prodiggers can select *k* different tours of the same length between *l* and *r*.
For example if *k*<==<=2, *l*<==<=1 and *r*<==<=2, if we define concert day as {1} and day-off as {0}, here are all possible tours: {0}, {1}, {00}, {01}, {10}, {11}. But tour 00 can not be selected because it has 2 days-off in a row. Now, we need to count in how many ways we can select *k*<==<=2 tours of the same length in range [1;2]. Here they are: {0,1}; {01,10}; {01,11}; {10,11}.
Since their schedule is quite busy, they want you to tell them in how many ways can do that, modulo 1<=000<=000<=007 (109<=+<=7). | The first line of the input contains three integers *k*, *l* and *r* (1<=β€<=*k*<=β€<=200, 1<=β€<=*l*<=β€<=*r*<=β€<=1018). | Output a single number: the number of ways to select *k* different tours of the same length, modulo 1<=000<=000<=007. | [
"1 1 2\n"
] | [
"5\n"
] | none | [] | 1,000 | 307,200 | 0 | 4,279 | |
685 | Kay and Snowflake | [
"data structures",
"dfs and similar",
"dp",
"trees"
] | null | null | After the piece of a devilish mirror hit the Kay's eye, he is no longer interested in the beauty of the roses. Now he likes to watch snowflakes.
Once upon a time, he found a huge snowflake that has a form of the tree (connected acyclic graph) consisting of *n* nodes. The root of tree has index 1. Kay is very interested in the structure of this tree.
After doing some research he formed *q* queries he is interested in. The *i*-th query asks to find a centroid of the subtree of the node *v**i*. Your goal is to answer all queries.
Subtree of a node is a part of tree consisting of this node and all it's descendants (direct or not). In other words, subtree of node *v* is formed by nodes *u*, such that node *v* is present on the path from *u* to root.
Centroid of a tree (or a subtree) is a node, such that if we erase it from the tree, the maximum size of the connected component will be at least two times smaller than the size of the initial tree (or a subtree). | The first line of the input contains two integers *n* and *q* (2<=β€<=*n*<=β€<=300<=000, 1<=β€<=*q*<=β€<=300<=000)Β β the size of the initial tree and the number of queries respectively.
The second line contains *n*<=-<=1 integer *p*2,<=*p*3,<=...,<=*p**n* (1<=β€<=*p**i*<=β€<=*n*)Β β the indices of the parents of the nodes from 2 to *n*. Node 1 is a root of the tree. It's guaranteed that *p**i* define a correct tree.
Each of the following *q* lines contain a single integer *v**i* (1<=β€<=*v**i*<=β€<=*n*)Β β the index of the node, that define the subtree, for which we want to find a centroid. | For each query print the index of a centroid of the corresponding subtree. If there are many suitable nodes, print any of them. It's guaranteed, that each subtree has at least one centroid. | [
"7 4\n1 1 3 3 5 3\n1\n2\n3\n5\n"
] | [
"3\n2\n3\n6\n"
] | The first query asks for a centroid of the whole treeΒ β this is node 3. If we delete node 3 the tree will split in four components, two of size 1 and two of size 2.
The subtree of the second node consists of this node only, so the answer is 2.
Node 3 is centroid of its own subtree.
The centroids of the subtree of the node 5 are nodes 5 and 6Β β both answers are considered correct. | [
{
"input": "7 4\n1 1 3 3 5 3\n1\n2\n3\n5",
"output": "3\n2\n3\n6"
},
{
"input": "2 2\n1\n1\n2",
"output": "2\n2"
}
] | 1,263 | 59,904,000 | -1 | 4,287 | |
0 | none | [
"none"
] | null | null | Drazil created a following problem about putting 1<=Γ<=2 tiles into an *n*<=Γ<=*m* grid:
"There is a grid with some cells that are empty and some cells that are occupied. You should use 1<=Γ<=2 tiles to cover all empty cells and no two tiles should cover each other. And you should print a solution about how to do it."
But Drazil doesn't like to write special checking program for this task. His friend, Varda advised him: "how about asking contestant only to print the solution when it exists and it is unique? Otherwise contestant may print 'Not unique' ".
Drazil found that the constraints for this task may be much larger than for the original task!
Can you solve this new problem?
Note that you should print 'Not unique' either when there exists no solution or when there exists several different solutions for the original task. | The first line contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=2000).
The following *n* lines describe the grid rows. Character '.' denotes an empty cell, and the character '*' denotes a cell that is occupied. | If there is no solution or the solution is not unique, you should print the string "Not unique".
Otherwise you should print how to cover all empty cells with 1<=Γ<=2 tiles. Use characters "<>" to denote horizontal tiles and characters "^v" to denote vertical tiles. Refer to the sample test for the output format example. | [
"3 3\n...\n.*.\n...\n",
"4 4\n..**\n*...\n*.**\n....\n",
"2 4\n*..*\n....\n",
"1 1\n.\n",
"1 1\n*\n"
] | [
"Not unique\n",
"<>**\n*^<>\n*v**\n<><>\n",
"*<>*\n<><>\n",
"Not unique\n",
"*\n"
] | In the first case, there are indeed two solutions:
and
so the answer is "Not unique". | [] | 0 | 0 | -1 | 4,300 | |
499 | Watching a movie | [
"greedy",
"implementation"
] | null | null | You have decided to watch the best moments of some movie. There are two buttons on your player:
1. Watch the current minute of the movie. By pressing this button, you watch the current minute of the movie and the player automatically proceeds to the next minute of the movie. 1. Skip exactly *x* minutes of the movie (*x* is some fixed positive integer). If the player is now at the *t*-th minute of the movie, then as a result of pressing this button, it proceeds to the minute (*t*<=+<=*x*).
Initially the movie is turned on in the player on the first minute, and you want to watch exactly *n* best moments of the movie, the *i*-th best moment starts at the *l**i*-th minute and ends at the *r**i*-th minute (more formally, the *i*-th best moment consists of minutes: *l**i*,<=*l**i*<=+<=1,<=...,<=*r**i*).
Determine, what is the minimum number of minutes of the movie you have to watch if you want to watch all the best moments? | The first line contains two space-separated integers *n*, *x* (1<=β€<=*n*<=β€<=50, 1<=β€<=*x*<=β€<=105) β the number of the best moments of the movie and the value of *x* for the second button.
The following *n* lines contain the descriptions of the best moments of the movie, the *i*-th line of the description contains two integers separated by a space *l**i*, *r**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=105).
It is guaranteed that for all integers *i* from 2 to *n* the following condition holds: *r**i*<=-<=1<=<<=*l**i*. | Output a single number β the answer to the problem. | [
"2 3\n5 6\n10 12\n",
"1 1\n1 100000\n"
] | [
"6\n",
"100000\n"
] | In the first sample, the player was initially standing on the first minute. As the minutes from the 1-st to the 4-th one don't contain interesting moments, we press the second button. Now we can not press the second button and skip 3 more minutes, because some of them contain interesting moments. Therefore, we watch the movie from the 4-th to the 6-th minute, after that the current time is 7. Similarly, we again skip 3 minutes and then watch from the 10-th to the 12-th minute of the movie. In total, we watch 6 minutes of the movie.
In the second sample, the movie is very interesting, so you'll have to watch all 100000 minutes of the movie. | [
{
"input": "2 3\n5 6\n10 12",
"output": "6"
},
{
"input": "1 1\n1 100000",
"output": "100000"
},
{
"input": "10 1\n2156 3497\n4784 7775\n14575 31932\n33447 35902\n36426 47202\n48772 60522\n63982 68417\n78537 79445\n90081 90629\n94325 95728",
"output": "53974"
},
{
"input": "1... | 77 | 307,200 | 0 | 4,301 | |
285 | Slightly Decreasing Permutations | [
"greedy",
"implementation"
] | null | null | Permutation *p* is an ordered set of integers *p*1,<=<=*p*2,<=<=...,<=<=*p**n*, consisting of *n* distinct positive integers, each of them doesn't exceed *n*. We'll denote the *i*-th element of permutation *p* as *p**i*. We'll call number *n* the size or the length of permutation *p*1,<=<=*p*2,<=<=...,<=<=*p**n*.
The decreasing coefficient of permutation *p*1,<=*p*2,<=...,<=*p**n* is the number of such *i* (1<=β€<=*i*<=<<=*n*), that *p**i*<=><=*p**i*<=+<=1.
You have numbers *n* and *k*. Your task is to print the permutation of length *n* with decreasing coefficient *k*. | The single line contains two space-separated integers: *n*,<=*k* (1<=β€<=*n*<=β€<=105,<=0<=β€<=*k*<=<<=*n*) β the permutation length and the decreasing coefficient. | In a single line print *n* space-separated integers: *p*1,<=*p*2,<=...,<=*p**n* β the permutation of length *n* with decreasing coefficient *k*.
If there are several permutations that meet this condition, print any of them. It is guaranteed that the permutation with the sought parameters exists. | [
"5 2\n",
"3 0\n",
"3 2\n"
] | [
"1 5 2 4 3\n",
"1 2 3\n",
"3 2 1\n"
] | none | [
{
"input": "5 2",
"output": "1 5 2 4 3"
},
{
"input": "3 0",
"output": "1 2 3"
},
{
"input": "3 2",
"output": "3 2 1"
},
{
"input": "1 0",
"output": "1"
},
{
"input": "2 0",
"output": "1 2"
},
{
"input": "2 1",
"output": "2 1"
},
{
"input":... | 154 | 7,065,600 | 3 | 4,304 | |
0 | none | [
"none"
] | null | null | Felix the Robot is preparing for a probability theory exam. Unfortunately, during the semester, he took a course of the belles-lettres instead of studying the subject, so now he does not know the answer to any of the upcoming exam's questions. One thing is for sure: Felix needs help!
The exam for robots is an online event. It consists of *n*<==<=5000 questions each of which has only two possible answers: "yes" and "no". A robot can attempt to pass the exam at most *x*<==<=100 times. The questions, their order and right answers don't change from one attempt to another. Once the exam starts, Felix will get just a few seconds for all his attempts, and he can't learn the right answers so fast.
The robot will try to pass the exam in the following way. First, Felix fixes the answers for all questions. The result is a string of *n* bits: answers for the first, second, ..., *n*-th questions. In this string, 0 means that the answer is "no", and 1 is for "yes". Then he answers the questions according to this string, spending one attempt. After that, Felix can fix another string of *n* bits and make another attempt, and so on until there are no more attempts.
In the online system for exam, the following optimization is implemented: if at some moment of time, the examinee already got *k*<==<=2000 answers wrong, the attempt is immediately terminated with the corresponding message. For Felix, this means that the remaining bits in the string he fixed are ignored. If there were strictly less than *k* wrong answers for all *n* questions, the exam is considered passed.
The result of an attempt is a number from *k* to *n* inclusive: the number of questions after which the attempt was terminated. If the exam is passed, this number is considered to be *n*<=+<=1. The exam result is the highest result among all attempts. If there were no attempts, the exam result is zero.
Your task is to write a program which will determine the bit strings for all attempts Felix makes. After each attempt, your program will get its result immediately. Help Felix get the highest exam result you can!
Interaction Protocol
Your solution can make from 0 to *x* attempts inclusive. To make an attempt, print a string to the standard output. The string must consist of exactly *n* binary digits without spaces and end with a newline character.
To prevent output buffering, after printing a string, insert a command to flush the buffer: for example, it can be fflushΒ (stdout) in C or C++, System.out.flushΒ () in Java, flushΒ (output) in Pascal or sys.stdout.flushΒ () in Python.
After each attempt you make, you can immediately read its result from the standard input. The result is an integer from *k* to *n*<=+<=1 inclusive, followed by a newline character.
Scoring System
A test is defined by a string of *n* binary digits: the right answers to *n* questions. This string is kept secret from the solution. Each test is evaluated separately.
If a solution followed the interaction protocol and terminated correctly on a test, it gets a score of *max* (0,<=*S*<=-<=4000) where *S* is the exam result. Otherwise, the solution gets zero score for the test.
Testing
Your solution will be checked on sets of tests generated in advance. Each test is created using a pseudo-random number generator. You can consider that the answers are uniformly distributed (the probabilities of digits 0 and 1 are the same) and mutually independent (the probabilities of all 2*n* possible strings are the same). A solution gets the score which is the sum of its score on all the tests.
During the main phase of the contest, there are two ways to send a solution for checking.
- The first one is to check on examples. There are 10 example tests which are also available for local testing. As soon as the solution is checked, you can see reports for all examples by clicking on the submission result.- The second way is to check on preliminary tests. There are 100 preliminary tests which are generated in advance but kept secret. The score for preliminary tests (but not for example tests) is used in the preliminary scoreboard. This score does not affect the final results, but nevertheless allows to roughly compare a solution with others.
After the main phase ends, for each participant, the system chooses the final solution:
- consider all solutions sent for preliminary testing; - choose the ones which got a total score strictly greater than zero; - define the final solution as the one of chosen solutions which has the latest submission time.
Note that the solutions sent only to be checked on examples are not considered when choosing the final solution.
During the final testing, all final solutions will be checked on the same large set of a large number (<=β<=1000) of final tests. The score for final tests determines the final scoreboard. The winner is the contestant whose solution gets the highest total score. In case two or more participants have equal total score, the contestants with such score tie for the same place.
A package for local development is available on GitHub at the following address: [https://github.com/GassaFM/online-exam](https://github.com/GassaFM/online-exam). You can download sources or the latest release: [https://github.com/GassaFM/online-exam/releases](https://github.com/GassaFM/online-exam/releases).
Example
To have an example which fits into the problem statement, let *n*<==<=10, *k*<==<=2, and *x*<==<=3 (recall that in the real problem, *n*<==<=5000, *k*<==<=2000, and *x*<==<=100, so this example is not a correct test for the problem). Let the right answers be defined by the string 1010001111. Before any attempts are made, the exam result is zero.
Consider a solution making three attempts.
Let the first attempt be defined by the string 0100100100. The result of this attempt is the number 2: the first wrong answer is the answer to the first question, and the second is to the second question. The exam result at this moment is 2.
Let the second attempt be defined by the string 1010101010. The result of this attempt is the number 8: the first wrong answer is the answer to the fifth question, and the second is to the eighth question. The exam result at this moment is 8.
Let the second attempt be defined by the string 1001011001. The result of this attempt is the number 4: the first wrong answer is the answer to the third question, and the second is to the fourth question. The exam result at this moment is still 8.
As *x*<==<=3 in our example, further attempts are impossible, so if the solution terminates correctly, the exam result is 8.
Now consider another solution making two attempts.
Let the first attempt be defined by the string 1010001110. Its result is the number 11: the first and only wrong answer is the answer to the tenth question, *k*<==<=2, so the exam is considered passed.
Let the first attempt be defined by the string 0000011111. Its result is the number 3: the first wrong answer is the answer to the first question, and the second one is to the third question.
If the solution terminates correctly after the above two attempts, the exam result is 11. | none | none | [] | [] | none | [] | 280 | 5,836,800 | -1 | 4,313 | |
2 | Commentator problem | [
"geometry"
] | C. Commentator problem | 1 | 64 | The Olympic Games in Bercouver are in full swing now. Here everyone has their own objectives: sportsmen compete for medals, and sport commentators compete for more convenient positions to give a running commentary. Today the main sport events take place at three round stadiums, and the commentator's objective is to choose the best point of observation, that is to say the point from where all the three stadiums can be observed. As all the sport competitions are of the same importance, the stadiums should be observed at the same angle. If the number of points meeting the conditions is more than one, the point with the maximum angle of observation is prefered.
Would you, please, help the famous Berland commentator G. Berniev to find the best point of observation. It should be noted, that the stadiums do not hide each other, the commentator can easily see one stadium through the other. | The input data consists of three lines, each of them describes the position of one stadium. The lines have the format *x*,<=<=*y*,<=<=*r*, where (*x*,<=*y*) are the coordinates of the stadium's center (<=-<=<=103<=β€<=*x*,<=<=*y*<=β€<=103), and *r* (1<=β€<=*r*<=<=β€<=103) is its radius. All the numbers in the input data are integer, stadiums do not have common points, and their centers are not on the same line. | Print the coordinates of the required point with five digits after the decimal point. If there is no answer meeting the conditions, the program shouldn't print anything. The output data should be left blank. | [
"0 0 10\n60 0 10\n30 30 10\n"
] | [
"30.00000 0.00000\n"
] | none | [
{
"input": "0 0 10\n60 0 10\n30 30 10",
"output": "30.00000 0.00000"
},
{
"input": "0 0 10\n100 100 10\n200 0 20",
"output": "60.76252 39.23748"
},
{
"input": "0 0 10\n300 300 11\n500 -500 12",
"output": "348.52046 -94.13524"
},
{
"input": "0 0 10\n300 300 12\n500 -500 14",
... | 61 | 0 | 0 | 4,324 |
0 | none | [
"none"
] | null | null | Little Artem likes electronics. He can spend lots of time making different schemas and looking for novelties in the nearest electronics store. The new control element was delivered to the store recently and Artem immediately bought it.
That element can store information about the matrix of integers size *n*<=Γ<=*m*. There are *n*<=+<=*m* inputs in that element, i.e. each row and each column can get the signal. When signal comes to the input corresponding to some row, this row cyclically shifts to the left, that is the first element of the row becomes last element, second element becomes first and so on. When signal comes to the input corresponding to some column, that column shifts cyclically to the top, that is first element of the column becomes last element, second element becomes first and so on. Rows are numbered with integers from 1 to *n* from top to bottom, while columns are numbered with integers from 1 to *m* from left to right.
Artem wants to carefully study this element before using it. For that purpose he is going to set up an experiment consisting of *q* turns. On each turn he either sends the signal to some input or checks what number is stored at some position of the matrix.
Artem has completed his experiment and has written down the results, but he has lost the chip! Help Artem find any initial matrix that will match the experiment results. It is guaranteed that experiment data is consistent, which means at least one valid matrix exists. | The first line of the input contains three integers *n*, *m* and *q* (1<=β€<=*n*,<=*m*<=β€<=100,<=1<=β€<=*q*<=β€<=10<=000)Β β dimensions of the matrix and the number of turns in the experiment, respectively.
Next *q* lines contain turns descriptions, one per line. Each description starts with an integer *t**i* (1<=β€<=*t**i*<=β€<=3) that defines the type of the operation. For the operation of first and second type integer *r**i* (1<=β€<=*r**i*<=β€<=*n*) or *c**i* (1<=β€<=*c**i*<=β€<=*m*) follows, while for the operations of the third type three integers *r**i*, *c**i* and *x**i* (1<=β€<=*r**i*<=β€<=*n*, 1<=β€<=*c**i*<=β€<=*m*, <=-<=109<=β€<=*x**i*<=β€<=109) are given.
Operation of the first type (*t**i*<==<=1) means that signal comes to the input corresponding to row *r**i*, that is it will shift cyclically. Operation of the second type (*t**i*<==<=2) means that column *c**i* will shift cyclically. Finally, operation of the third type means that at this moment of time cell located in the row *r**i* and column *c**i* stores value *x**i*. | Print the description of any valid initial matrix as *n* lines containing *m* integers each. All output integers should not exceed 109 by their absolute value.
If there are multiple valid solutions, output any of them. | [
"2 2 6\n2 1\n2 2\n3 1 1 1\n3 2 2 2\n3 1 2 8\n3 2 1 8\n",
"3 3 2\n1 2\n3 2 2 5\n"
] | [
"8 2 \n1 8 \n",
"0 0 0 \n0 0 5 \n0 0 0 \n"
] | none | [
{
"input": "2 2 6\n2 1\n2 2\n3 1 1 1\n3 2 2 2\n3 1 2 8\n3 2 1 8",
"output": "8 2 \n1 8 "
},
{
"input": "3 3 2\n1 2\n3 2 2 5",
"output": "0 0 0 \n0 0 5 \n0 0 0 "
},
{
"input": "5 5 1\n1 5",
"output": "0 0 0 0 0 \n0 0 0 0 0 \n0 0 0 0 0 \n0 0 0 0 0 \n0 0 0 0 0 "
},
{
"input": "1... | 468 | 6,041,600 | -1 | 4,328 | |
349 | Color the Fence | [
"data structures",
"dp",
"greedy",
"implementation"
] | null | null | Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has.
Unfortunately, Igor could only get *v* liters of paint. He did the math and concluded that digit *d* requires *a**d* liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number.
Help Igor find the maximum number he can write on the fence. | The first line contains a positive integer *v* (0<=β€<=*v*<=β€<=106). The second line contains nine positive integers *a*1,<=*a*2,<=...,<=*a*9 (1<=β€<=*a**i*<=β€<=105). | Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. | [
"5\n5 4 3 2 1 2 3 4 5\n",
"2\n9 11 1 12 5 8 9 10 6\n",
"0\n1 1 1 1 1 1 1 1 1\n"
] | [
"55555\n",
"33\n",
"-1\n"
] | none | [
{
"input": "5\n5 4 3 2 1 2 3 4 5",
"output": "55555"
},
{
"input": "2\n9 11 1 12 5 8 9 10 6",
"output": "33"
},
{
"input": "0\n1 1 1 1 1 1 1 1 1",
"output": "-1"
},
{
"input": "50\n5 3 10 2 2 4 3 6 5",
"output": "5555555555555555555555555"
},
{
"input": "22\n405 3... | 0 | 0 | -1 | 4,331 | |
159 | Palindrome pairs | [
"*special",
"brute force",
"dp",
"strings"
] | null | null | You are given a non-empty string *s* consisting of lowercase letters. Find the number of pairs of non-overlapping palindromic substrings of this string.
In a more formal way, you have to find the quantity of tuples (*a*,<=*b*,<=*x*,<=*y*) such that 1<=β€<=*a*<=β€<=*b*<=<<=*x*<=β€<=*y*<=β€<=|*s*| and substrings *s*[*a*... *b*], *s*[*x*... *y*] are palindromes.
A palindrome is a string that can be read the same way from left to right and from right to left. For example, "abacaba", "z", "abba" are palindromes.
A substring *s*[*i*... *j*] (1<=β€<=*i*<=β€<=*j*<=β€<=|*s*|) of string *s* = *s*1*s*2... *s*|*s*| is a string *s**i**s**i*<=+<=1... *s**j*. For example, substring *s*[2...4] of string *s* = "abacaba" equals "bac". | The first line of input contains a non-empty string *s* which consists of lowercase letters ('a'...'z'), *s* contains at most 2000 characters. | Output a single number β the quantity of pairs of non-overlapping palindromic substrings of *s*.
Please do not use the %lld format specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d format specifier. | [
"aa\n",
"aaa\n",
"abacaba\n"
] | [
"1\n",
"5\n",
"36\n"
] | none | [
{
"input": "aa",
"output": "1"
},
{
"input": "aaa",
"output": "5"
},
{
"input": "abacaba",
"output": "36"
},
{
"input": "aaaaaaaaaa",
"output": "495"
},
{
"input": "aabbb",
"output": "24"
},
{
"input": "abbaa",
"output": "18"
},
{
"input": ... | 2,930 | 157,593,600 | 3 | 4,334 | |
739 | Alyona and mex | [
"constructive algorithms",
"greedy"
] | null | null | Alyona's mother wants to present an array of *n* non-negative integers to Alyona. The array should be special.
Alyona is a capricious girl so after she gets the array, she inspects *m* of its subarrays. Subarray is a set of some subsequent elements of the array. The *i*-th subarray is described with two integers *l**i* and *r**i*, and its elements are *a*[*l**i*],<=*a*[*l**i*<=+<=1],<=...,<=*a*[*r**i*].
Alyona is going to find mex for each of the chosen subarrays. Among these *m* mexes the girl is going to find the smallest. She wants this minimum mex to be as large as possible.
You are to find an array *a* of *n* elements so that the minimum mex among those chosen by Alyona subarrays is as large as possible.
The mex of a set *S* is a minimum possible non-negative integer that is not in *S*. | The first line contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=105).
The next *m* lines contain information about the subarrays chosen by Alyona. The *i*-th of these lines contains two integers *l**i* and *r**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=*n*), that describe the subarray *a*[*l**i*],<=*a*[*l**i*<=+<=1],<=...,<=*a*[*r**i*]. | In the first line print single integerΒ β the maximum possible minimum mex.
In the second line print *n* integersΒ β the array *a*. All the elements in *a* should be between 0 and 109.
It is guaranteed that there is an optimal answer in which all the elements in *a* are between 0 and 109.
If there are multiple solutions, print any of them. | [
"5 3\n1 3\n2 5\n4 5\n",
"4 2\n1 4\n2 4\n"
] | [
"2\n1 0 2 1 0\n",
"3\n5 2 0 1"
] | The first example: the mex of the subarray (1,β3) is equal to 3, the mex of the subarray (2,β5) is equal to 3, the mex of the subarray (4,β5) is equal to 2 as well, thus the minumal mex among the subarrays chosen by Alyona is equal to 2. | [
{
"input": "5 3\n1 3\n2 5\n4 5",
"output": "2\n0 1 0 1 0"
},
{
"input": "4 2\n1 4\n2 4",
"output": "3\n0 1 2 0"
},
{
"input": "1 1\n1 1",
"output": "1\n0"
},
{
"input": "2 1\n2 2",
"output": "1\n0 0"
},
{
"input": "5 6\n2 4\n2 3\n1 4\n3 4\n2 5\n1 3",
"output":... | 748 | 101,273,600 | 3 | 4,336 | |
371 | Fox Dividing Cheese | [
"math",
"number theory"
] | null | null | Two little greedy bears have found two pieces of cheese in the forest of weight *a* and *b* grams, correspondingly. The bears are so greedy that they are ready to fight for the larger piece. That's where the fox comes in and starts the dialog: "Little bears, wait a little, I want to make your pieces equal" "Come off it fox, how are you going to do that?", the curious bears asked. "It's easy", said the fox. "If the mass of a certain piece is divisible by two, then I can eat exactly a half of the piece. If the mass of a certain piece is divisible by three, then I can eat exactly two-thirds, and if the mass is divisible by five, then I can eat four-fifths. I'll eat a little here and there and make the pieces equal".
The little bears realize that the fox's proposal contains a catch. But at the same time they realize that they can not make the two pieces equal themselves. So they agreed to her proposal, but on one condition: the fox should make the pieces equal as quickly as possible. Find the minimum number of operations the fox needs to make pieces equal. | The first line contains two space-separated integers *a* and *b* (1<=β€<=*a*,<=*b*<=β€<=109). | If the fox is lying to the little bears and it is impossible to make the pieces equal, print -1. Otherwise, print the required minimum number of operations. If the pieces of the cheese are initially equal, the required number is 0. | [
"15 20\n",
"14 8\n",
"6 6\n"
] | [
"3\n",
"-1\n",
"0\n"
] | none | [
{
"input": "15 20",
"output": "3"
},
{
"input": "14 8",
"output": "-1"
},
{
"input": "6 6",
"output": "0"
},
{
"input": "1 1",
"output": "0"
},
{
"input": "1 1024",
"output": "10"
},
{
"input": "1024 729",
"output": "16"
},
{
"input": "1024... | 93 | 20,172,800 | 0 | 4,349 | |
616 | Longest k-Good Segment | [
"binary search",
"data structures",
"two pointers"
] | null | null | The array *a* with *n* integers is given. Let's call the sequence of one or more consecutive elements in *a* segment. Also let's call the segment k-good if it contains no more than *k* different values.
Find any longest k-good segment.
As the input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. | The first line contains two integers *n*,<=*k* (1<=β€<=*k*<=β€<=*n*<=β€<=5Β·105) β the number of elements in *a* and the parameter *k*.
The second line contains *n* integers *a**i* (0<=β€<=*a**i*<=β€<=106) β the elements of the array *a*. | Print two integers *l*,<=*r* (1<=β€<=*l*<=β€<=*r*<=β€<=*n*) β the index of the left and the index of the right ends of some k-good longest segment. If there are several longest segments you can print any of them. The elements in *a* are numbered from 1 to *n* from left to right. | [
"5 5\n1 2 3 4 5\n",
"9 3\n6 5 1 2 3 2 1 4 5\n",
"3 1\n1 2 3\n"
] | [
"1 5\n",
"3 7\n",
"1 1\n"
] | none | [
{
"input": "5 5\n1 2 3 4 5",
"output": "1 5"
},
{
"input": "9 3\n6 5 1 2 3 2 1 4 5",
"output": "3 7"
},
{
"input": "3 1\n1 2 3",
"output": "1 1"
},
{
"input": "1 1\n747391",
"output": "1 1"
},
{
"input": "5 2\n171230 171230 171230 171230 171230",
"output": "1 ... | 62 | 2,867,200 | -1 | 4,350 | |
466 | Number of Ways | [
"binary search",
"brute force",
"data structures",
"dp",
"two pointers"
] | null | null | You've got array *a*[1],<=*a*[2],<=...,<=*a*[*n*], consisting of *n* integers. Count the number of ways to split all the elements of the array into three contiguous parts so that the sum of elements in each part is the same.
More formally, you need to find the number of such pairs of indices *i*,<=*j* (2<=β€<=*i*<=β€<=*j*<=β€<=*n*<=-<=1), that . | The first line contains integer *n* (1<=β€<=*n*<=β€<=5Β·105), showing how many numbers are in the array. The second line contains *n* integers *a*[1], *a*[2], ..., *a*[*n*] (|*a*[*i*]|<=β€<=<=109) β the elements of array *a*. | Print a single integer β the number of ways to split the array into three parts with the same sum. | [
"5\n1 2 3 0 3\n",
"4\n0 1 -1 0\n",
"2\n4 1\n"
] | [
"2\n",
"1\n",
"0\n"
] | none | [
{
"input": "5\n1 2 3 0 3",
"output": "2"
},
{
"input": "4\n0 1 -1 0",
"output": "1"
},
{
"input": "2\n4 1",
"output": "0"
},
{
"input": "9\n0 0 0 0 0 0 0 0 0",
"output": "28"
},
{
"input": "10\n2 5 -2 2 -3 -2 3 5 -5 -2",
"output": "0"
},
{
"input": "1\... | 249 | 56,729,600 | 3 | 4,355 | |
611 | New Year and Days | [
"implementation"
] | null | null | Today is Wednesday, the third day of the week. What's more interesting is that tomorrow is the last day of the year 2015.
Limak is a little polar bear. He enjoyed this year a lot. Now, he is so eager to the coming year 2016.
Limak wants to prove how responsible a bear he is. He is going to regularly save candies for the entire year 2016! He considers various saving plans. He can save one candy either on some fixed day of the week or on some fixed day of the month.
Limak chose one particular plan. He isn't sure how many candies he will save in the 2016 with his plan. Please, calculate it and tell him. | The only line of the input is in one of the following two formats:
- "*x* of week" where *x* (1<=β€<=*x*<=β€<=7) denotes the day of the week. The 1-st day is Monday and the 7-th one is Sunday. - "*x* of month" where *x* (1<=β€<=*x*<=β€<=31) denotes the day of the month. | Print one integerΒ β the number of candies Limak will save in the year 2016. | [
"4 of week\n",
"30 of month\n"
] | [
"52\n",
"11\n"
] | Polar bears use the Gregorian calendar. It is the most common calendar and you likely use it too. You can read about it on Wikipedia if you want to β [https://en.wikipedia.org/wiki/Gregorian_calendar](https://en.wikipedia.org/wiki/Gregorian_calendar). The week starts with Monday.
In the first sample Limak wants to save one candy on each Thursday (the 4-th day of the week). There are 52 Thursdays in the 2016. Thus, he will save 52 candies in total.
In the second sample Limak wants to save one candy on the 30-th day of each month. There is the 30-th day in exactly 11 months in the 2016Β β all months but February. It means that Limak will save 11 candies in total. | [
{
"input": "4 of week",
"output": "52"
},
{
"input": "30 of month",
"output": "11"
},
{
"input": "17 of month",
"output": "12"
},
{
"input": "31 of month",
"output": "7"
},
{
"input": "6 of week",
"output": "53"
},
{
"input": "1 of week",
"output":... | 109 | 307,200 | 3 | 4,362 | |
623 | Graph and String | [
"constructive algorithms",
"graphs"
] | null | null | One day student Vasya was sitting on a lecture and mentioned a string *s*1*s*2... *s**n*, consisting of letters "a", "b" and "c" that was written on his desk. As the lecture was boring, Vasya decided to complete the picture by composing a graph *G* with the following properties:
- *G* has exactly *n* vertices, numbered from 1 to *n*. - For all pairs of vertices *i* and *j*, where *i*<=β <=*j*, there is an edge connecting them if and only if characters *s**i* and *s**j* are either equal or neighbouring in the alphabet. That is, letters in pairs "a"-"b" and "b"-"c" are neighbouring, while letters "a"-"c" are not.
Vasya painted the resulting graph near the string and then erased the string. Next day Vasya's friend Petya came to a lecture and found some graph at his desk. He had heard of Vasya's adventure and now he wants to find out whether it could be the original graph *G*, painted by Vasya. In order to verify this, Petya needs to know whether there exists a string *s*, such that if Vasya used this *s* he would produce the given graph *G*. | The first line of the input contains two integers *n* and *m* Β β the number of vertices and edges in the graph found by Petya, respectively.
Each of the next *m* lines contains two integers *u**i* and *v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*,<=*u**i*<=β <=*v**i*)Β β the edges of the graph *G*. It is guaranteed, that there are no multiple edges, that is any pair of vertexes appear in this list no more than once. | In the first line print "Yes" (without the quotes), if the string *s* Petya is interested in really exists and "No" (without the quotes) otherwise.
If the string *s* exists, then print it on the second line of the output. The length of *s* must be exactly *n*, it must consist of only letters "a", "b" and "c" only, and the graph built using this string must coincide with *G*. If there are multiple possible answers, you may print any of them. | [
"2 1\n1 2\n",
"4 3\n1 2\n1 3\n1 4\n"
] | [
"Yes\naa\n",
"No\n"
] | In the first sample you are given a graph made of two vertices with an edge between them. So, these vertices can correspond to both the same and adjacent letters. Any of the following strings "aa", "ab", "ba", "bb", "bc", "cb", "cc" meets the graph's conditions.
In the second sample the first vertex is connected to all three other vertices, but these three vertices are not connected with each other. That means that they must correspond to distinct letters that are not adjacent, but that is impossible as there are only two such letters: a and c. | [
{
"input": "2 1\n1 2",
"output": "Yes\naa"
},
{
"input": "4 3\n1 2\n1 3\n1 4",
"output": "No"
},
{
"input": "4 4\n1 2\n1 3\n1 4\n3 4",
"output": "Yes\nbacc"
},
{
"input": "1 0",
"output": "Yes\na"
},
{
"input": "8 28\n3 2\n4 2\n7 4\n6 3\n3 7\n8 1\n3 4\n5 1\n6 5\n5... | 62 | 5,632,000 | 0 | 4,372 | |
762 | Radio stations | [
"binary search",
"data structures"
] | null | null | In the lattice points of the coordinate line there are *n* radio stations, the *i*-th of which is described by three integers:
- *x**i* β the coordinate of the *i*-th station on the line, - *r**i* β the broadcasting range of the *i*-th station, - *f**i* β the broadcasting frequency of the *i*-th station.
We will say that two radio stations with numbers *i* and *j* reach each other, if the broadcasting range of each of them is more or equal to the distance between them. In other words *min*(*r**i*,<=*r**j*)<=β₯<=|*x**i*<=-<=*x**j*|.
Let's call a pair of radio stations (*i*,<=*j*) bad if *i*<=<<=*j*, stations *i* and *j* reach each other and they are close in frequency, that is, |*f**i*<=-<=*f**j*|<=β€<=*k*.
Find the number of bad pairs of radio stations. | The first line contains two integers *n* and *k* (1<=β€<=*n*<=β€<=105, 0<=β€<=*k*<=β€<=10) β the number of radio stations and the maximum difference in the frequencies for the pair of stations that reach each other to be considered bad.
In the next *n* lines follow the descriptions of radio stations. Each line contains three integers *x**i*, *r**i* and *f**i* (1<=β€<=*x**i*,<=*r**i*<=β€<=109, 1<=β€<=*f**i*<=β€<=104) β the coordinate of the *i*-th radio station, it's broadcasting range and it's broadcasting frequency. No two radio stations will share a coordinate. | Output the number of bad pairs of radio stations. | [
"3 2\n1 3 10\n3 2 5\n4 10 8\n",
"3 3\n1 3 10\n3 2 5\n4 10 8\n",
"5 1\n1 3 2\n2 2 4\n3 2 1\n4 2 1\n5 3 3\n",
"5 1\n1 5 2\n2 5 4\n3 5 1\n4 5 1\n5 5 3\n"
] | [
"1\n",
"2\n",
"2\n",
"5\n"
] | none | [
{
"input": "3 2\n1 3 10\n3 2 5\n4 10 8",
"output": "1"
},
{
"input": "3 3\n1 3 10\n3 2 5\n4 10 8",
"output": "2"
},
{
"input": "5 1\n1 3 2\n2 2 4\n3 2 1\n4 2 1\n5 3 3",
"output": "2"
},
{
"input": "5 1\n1 5 2\n2 5 4\n3 5 1\n4 5 1\n5 5 3",
"output": "5"
},
{
"input... | 30 | 0 | 0 | 4,375 | |
1,009 | Relatively Prime Graph | [
"brute force",
"constructive algorithms",
"graphs",
"greedy",
"math"
] | null | null | Let's call an undirected graph $G = (V, E)$ relatively prime if and only if for each edge $(v, u) \in E$ Β $GCD(v, u) = 1$ (the greatest common divisor of $v$ and $u$ is $1$). If there is no edge between some pair of vertices $v$ and $u$ then the value of $GCD(v, u)$ doesn't matter. The vertices are numbered from $1$ to $|V|$.
Construct a relatively prime graph with $n$ vertices and $m$ edges such that it is connected and it contains neither self-loops nor multiple edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
If there are multiple answers then print any of them. | The only line contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) β the number of vertices and the number of edges. | If there exists no valid graph with the given number of vertices and edges then output "Impossible".
Otherwise print the answer in the following format:
The first line should contain the word "Possible".
The $i$-th of the next $m$ lines should contain the $i$-th edge $(v_i, u_i)$ of the resulting graph ($1 \le v_i, u_i \le n, v_i \neq u_i$). For each pair $(v, u)$ there can be no more pairs $(v, u)$ or $(u, v)$. The vertices are numbered from $1$ to $n$.
If there are multiple answers then print any of them. | [
"5 6\n",
"6 12\n"
] | [
"Possible\n2 5\n3 2\n5 1\n3 4\n4 1\n5 4\n",
"Impossible\n"
] | Here is the representation of the graph from the first example: <img class="tex-graphics" src="https://espresso.codeforces.com/7a1353a992545456c007e3071fa0a06fe46fc64e.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "5 6",
"output": "Possible\n2 1\n3 1\n4 1\n5 1\n3 2\n5 2"
},
{
"input": "6 12",
"output": "Impossible"
},
{
"input": "572 99643",
"output": "Possible\n2 1\n3 1\n4 1\n5 1\n6 1\n7 1\n8 1\n9 1\n10 1\n11 1\n12 1\n13 1\n14 1\n15 1\n16 1\n17 1\n18 1\n19 1\n20 1\n21 1\n22 1\n23 1... | 139 | 9,830,400 | 0 | 4,377 | |
464 | Restore Cube | [
"brute force",
"geometry"
] | null | null | Peter had a cube with non-zero length of a side. He put the cube into three-dimensional space in such a way that its vertices lay at integer points (it is possible that the cube's sides are not parallel to the coordinate axes). Then he took a piece of paper and wrote down eight lines, each containing three integers β coordinates of cube's vertex (a single line contains coordinates of a single vertex, each vertex is written exactly once), put the paper on the table and left. While Peter was away, his little brother Nick decided to play with the numbers on the paper. In one operation Nick could swap some numbers inside a single line (Nick didn't swap numbers from distinct lines). Nick could have performed any number of such operations.
When Peter returned and found out about Nick's mischief, he started recollecting the original coordinates. Help Peter restore the original position of the points or else state that this is impossible and the numbers were initially recorded incorrectly. | Each of the eight lines contains three space-separated integers β the numbers written on the piece of paper after Nick's mischief. All numbers do not exceed 106 in their absolute value. | If there is a way to restore the cube, then print in the first line "YES". In each of the next eight lines print three integers β the restored coordinates of the points. The numbers in the *i*-th output line must be a permutation of the numbers in *i*-th input line. The numbers should represent the vertices of a cube with non-zero length of a side. If there are multiple possible ways, print any of them.
If there is no valid way, print "NO" (without the quotes) in the first line. Do not print anything else. | [
"0 0 0\n0 0 1\n0 0 1\n0 0 1\n0 1 1\n0 1 1\n0 1 1\n1 1 1\n",
"0 0 0\n0 0 0\n0 0 0\n0 0 0\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n"
] | [
"YES\n0 0 0\n0 0 1\n0 1 0\n1 0 0\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n",
"NO\n"
] | none | [
{
"input": "0 0 0\n0 0 1\n0 0 1\n0 0 1\n0 1 1\n0 1 1\n0 1 1\n1 1 1",
"output": "YES\n0 0 0\n0 0 1\n0 1 0\n1 0 0\n0 1 1\n1 0 1\n1 1 0\n1 1 1"
},
{
"input": "0 0 0\n0 0 0\n0 0 0\n0 0 0\n1 1 1\n1 1 1\n1 1 1\n1 1 1",
"output": "NO"
},
{
"input": "0 0 0\n1 0 0\n0 1 0\n1 1 0\n0 0 1\n1 0 1\n0 1... | 46 | 0 | -1 | 4,384 | |
394 | Very Beautiful Number | [
"math"
] | null | null | Teacher thinks that we make a lot of progress. Now we are even allowed to use decimal notation instead of counting sticks. After the test the teacher promised to show us a "very beautiful number". But the problem is, he's left his paper with the number in the teachers' office.
The teacher remembers that the "very beautiful number" was strictly positive, didn't contain any leading zeroes, had the length of exactly *p* decimal digits, and if we move the last digit of the number to the beginning, it grows exactly *x* times. Besides, the teacher is sure that among all such numbers the "very beautiful number" is minimal possible.
The teachers' office isn't near and the teacher isn't young. But we've passed the test and we deserved the right to see the "very beautiful number". Help to restore the justice, find the "very beautiful number" for us! | The single line contains integers *p*, *x* (1<=β€<=*p*<=β€<=106,<=1<=β€<=*x*<=β€<=9). | If the teacher's made a mistake and such number doesn't exist, then print on a single line "Impossible" (without the quotes). Otherwise, print the "very beautiful number" without leading zeroes. | [
"6 5\n",
"1 2\n",
"6 4\n"
] | [
"142857",
"Impossible\n",
"102564"
] | Sample 1: 142857Β·5β=β714285.
Sample 2: The number that consists of a single digit cannot stay what it is when multiplied by 2, thus, the answer to the test sample is "Impossible". | [
{
"input": "6 5",
"output": "142857"
},
{
"input": "1 2",
"output": "Impossible"
},
{
"input": "6 4",
"output": "102564"
},
{
"input": "11 1",
"output": "11111111111"
},
{
"input": "42 5",
"output": "102040816326530612244897959183673469387755"
},
{
"in... | 1,000 | 2,355,200 | 0 | 4,397 | |
73 | The Elder Trolls IV: Oblivon | [
"greedy",
"math"
] | A. The Elder Trolls IV: Oblivon | 2 | 256 | Vasya plays The Elder Trolls IV: Oblivon. Oh, those creators of computer games! What they do not come up with! Absolutely unique monsters have been added to the The Elder Trolls IV: Oblivon. One of these monsters is Unkillable Slug. Why it is "Unkillable"? Firstly, because it can be killed with cutting weapon only, so lovers of two-handed amber hammers should find suitable knife themselves. Secondly, it is necessary to make so many cutting strokes to Unkillable Slug. Extremely many. Too many!
Vasya has already promoted his character to 80-th level and in order to gain level 81 he was asked to kill Unkillable Slug. The monster has a very interesting shape. It looks like a rectangular parallelepiped with size *x*<=Γ<=*y*<=Γ<=*z*, consisting of undestructable cells 1<=Γ<=1<=Γ<=1. At one stroke Vasya can cut the Slug along an imaginary grid, i.e. cut with a plane parallel to one of the parallelepiped side. Monster dies when amount of parts it is divided reaches some critical value.
All parts of monster do not fall after each cut, they remains exactly on its places. I. e. Vasya can cut several parts with one cut.
Vasya wants to know what the maximum number of pieces he can cut the Unkillable Slug into striking him at most *k* times.
Vasya's character uses absolutely thin sword with infinite length. | The first line of input contains four integer numbers *x*,<=*y*,<=*z*,<=*k* (1<=β€<=*x*,<=*y*,<=*z*<=β€<=106,<=0<=β€<=*k*<=β€<=109). | Output the only number β the answer for the problem.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). | [
"2 2 2 3\n",
"2 2 2 1\n"
] | [
"8",
"2"
] | In the first sample Vasya make 3 pairwise perpendicular cuts. He cuts monster on two parts with the first cut, then he divides each part on two with the second cut, and finally he divides each of the 4 parts on two. | [
{
"input": "2 2 2 3",
"output": "8"
},
{
"input": "2 2 2 1",
"output": "2"
},
{
"input": "1 1 1 1",
"output": "1"
},
{
"input": "1 2 3 3",
"output": "6"
},
{
"input": "20 4 5 12",
"output": "120"
},
{
"input": "100 500 100500 1000000000",
"output":... | 216 | 0 | 0 | 4,413 |
56 | Spoilt Permutation | [
"implementation"
] | B. Spoilt Permutation | 2 | 256 | Vasya collects coins: he has exactly one coin for every year from 1 to *n*. Naturally, Vasya keeps all the coins in his collection in the order in which they were released. Once Vasya's younger brother made a change β he took all the coins whose release year dated from *l* to *r* inclusively and put them in the reverse order. That is, he took a certain segment [*l*,<=*r*] and reversed it. At that the segment's endpoints did not coincide. For example, if *n*<==<=8, then initially Vasya's coins were kept in the order 1 2 3 4 5 6 7 8. If Vasya's younger brother chose the segment [2,<=6], then after the reversal the coin order will change to 1 6 5 4 3 2 7 8. Vasya suspects that someone else could have spoilt the permutation after his brother. Help him to find that out. Check if the given permutation can be obtained from the permutation 1 2 ... *n* using exactly one segment reversal. If it is possible, find the segment itself. | The first line contains an integer *n* (1<=β€<=*n*<=β€<=1000) which is the number of coins in Vasya's collection. The second line contains space-separated *n* integers which are the spoilt sequence of coins. It is guaranteed that the given sequence is a permutation, i.e. it contains only integers from 1 to *n*, and every number is used exactly 1 time. | If it is impossible to obtain the given permutation from the original one in exactly one action, print 0 0. Otherwise, print two numbers *l* *r* (1<=β€<=*l*<=<<=*r*<=β€<=*n*) which are the endpoints of the segment that needs to be reversed to obtain from permutation 1 2 ... *n* the given one. | [
"8\n1 6 5 4 3 2 7 8\n",
"4\n2 3 4 1\n",
"4\n1 2 3 4\n"
] | [
"2 6\n",
"0 0\n",
"0 0\n"
] | none | [
{
"input": "8\n1 6 5 4 3 2 7 8",
"output": "2 6"
},
{
"input": "4\n2 3 4 1",
"output": "0 0"
},
{
"input": "4\n1 2 3 4",
"output": "0 0"
},
{
"input": "8\n1 3 2 4 6 5 7 8",
"output": "0 0"
},
{
"input": "8\n1 3 4 2 6 5 7 8",
"output": "0 0"
},
{
"input... | 124 | 307,200 | 0 | 4,416 |
257 | Sum | [
"greedy",
"math"
] | null | null | Vasya has found a piece of paper with an array written on it. The array consists of *n* integers *a*1,<=*a*2,<=...,<=*a**n*. Vasya noticed that the following condition holds for the array *a**i*<=β€<=*a**i*<=+<=1<=β€<=2Β·*a**i* for any positive integer *i* (*i*<=<<=*n*).
Vasya wants to add either a "+" or a "-" before each number of array. Thus, Vasya will get an expression consisting of *n* summands. The value of the resulting expression is the sum of all its elements. The task is to add signs "+" and "-" before each number so that the value of expression *s* meets the limits 0<=β€<=*s*<=β€<=*a*1. Print a sequence of signs "+" and "-", satisfying the given limits. It is guaranteed that the solution for the problem exists. | The first line contains integer *n* (1<=β€<=*n*<=β€<=105) β the size of the array. The second line contains space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=109) β the original array.
It is guaranteed that the condition *a**i*<=β€<=*a**i*<=+<=1<=β€<=2Β·*a**i* fulfills for any positive integer *i* (*i*<=<<=*n*). | In a single line print the sequence of *n* characters "+" and "-", where the *i*-th character is the sign that is placed in front of number *a**i*. The value of the resulting expression *s* must fit into the limits 0<=β€<=*s*<=β€<=*a*1. If there are multiple solutions, you are allowed to print any of them. | [
"4\n1 2 3 5\n",
"3\n3 3 5\n"
] | [
"+++-",
"++-"
] | none | [
{
"input": "4\n1 2 3 5",
"output": "+++-"
},
{
"input": "3\n3 3 5",
"output": "++-"
},
{
"input": "4\n2 4 5 6",
"output": "-++-"
},
{
"input": "6\n3 5 10 11 12 20",
"output": "++-++-"
},
{
"input": "10\n10 14 17 22 43 72 74 84 88 93",
"output": "++---++--+"
... | 2,000 | 21,913,600 | 0 | 4,419 | |
748 | Santa Claus and Robot | [
"constructive algorithms",
"math"
] | null | null | Santa Claus has Robot which lives on the infinite grid and can move along its lines. He can also, having a sequence of *m* points *p*1,<=*p*2,<=...,<=*p**m* with integer coordinates, do the following: denote its initial location by *p*0. First, the robot will move from *p*0 to *p*1 along one of the shortest paths between them (please notice that since the robot moves only along the grid lines, there can be several shortest paths). Then, after it reaches *p*1, it'll move to *p*2, again, choosing one of the shortest ways, then to *p*3, and so on, until he has visited all points in the given order. Some of the points in the sequence may coincide, in that case Robot will visit that point several times according to the sequence order.
While Santa was away, someone gave a sequence of points to Robot. This sequence is now lost, but Robot saved the protocol of its unit movements. Please, find the minimum possible length of the sequence. | The first line of input contains the only positive integer *n* (1<=β€<=*n*<=β€<=2Β·105) which equals the number of unit segments the robot traveled. The second line contains the movements protocol, which consists of *n* letters, each being equal either L, or R, or U, or D. *k*-th letter stands for the direction which Robot traveled the *k*-th unit segment in: L means that it moved to the left, RΒ β to the right, UΒ β to the top and DΒ β to the bottom. Have a look at the illustrations for better explanation. | The only line of input should contain the minimum possible length of the sequence. | [
"4\nRURD\n",
"6\nRRULDD\n",
"26\nRRRULURURUULULLLDLDDRDRDLD\n",
"3\nRLL\n",
"4\nLRLR\n"
] | [
"2\n",
"2\n",
"7\n",
"2\n",
"4\n"
] | The illustrations to the first three tests are given below.
<img class="tex-graphics" src="https://espresso.codeforces.com/832fb8f97a482be815e0f87edde26c9791a0d330.png" style="max-width: 100.0%;max-height: 100.0%;"/> <img class="tex-graphics" src="https://espresso.codeforces.com/119a8ba68772b2c2bf76f2acdc58027f6c5cde1f.png" style="max-width: 100.0%;max-height: 100.0%;"/> <img class="tex-graphics" src="https://espresso.codeforces.com/c7b4534f24cbad48148bcba24bc44f37bf7a2dbf.png" style="max-width: 100.0%;max-height: 100.0%;"/>
The last example illustrates that each point in the sequence should be counted as many times as it is presented in the sequence. | [
{
"input": "4\nRURD",
"output": "2"
},
{
"input": "6\nRRULDD",
"output": "2"
},
{
"input": "26\nRRRULURURUULULLLDLDDRDRDLD",
"output": "7"
},
{
"input": "3\nRLL",
"output": "2"
},
{
"input": "4\nLRLR",
"output": "4"
},
{
"input": "5\nLRDLR",
"outpu... | 93 | 0 | 0 | 4,422 | |
16 | Burglar and Matches | [
"greedy",
"implementation",
"sortings"
] | B. Burglar and Matches | 0 | 64 | A burglar got into a matches warehouse and wants to steal as many matches as possible. In the warehouse there are *m* containers, in the *i*-th container there are *a**i* matchboxes, and each matchbox contains *b**i* matches. All the matchboxes are of the same size. The burglar's rucksack can hold *n* matchboxes exactly. Your task is to find out the maximum amount of matches that a burglar can carry away. He has no time to rearrange matches in the matchboxes, that's why he just chooses not more than *n* matchboxes so that the total amount of matches in them is maximal. | The first line of the input contains integer *n* (1<=β€<=*n*<=β€<=2Β·108) and integer *m* (1<=β€<=*m*<=β€<=20). The *i*<=+<=1-th line contains a pair of numbers *a**i* and *b**i* (1<=β€<=*a**i*<=β€<=108,<=1<=β€<=*b**i*<=β€<=10). All the input numbers are integer. | Output the only number β answer to the problem. | [
"7 3\n5 10\n2 5\n3 6\n",
"3 3\n1 3\n2 2\n3 1\n"
] | [
"62\n",
"7\n"
] | none | [
{
"input": "7 3\n5 10\n2 5\n3 6",
"output": "62"
},
{
"input": "3 3\n1 3\n2 2\n3 1",
"output": "7"
},
{
"input": "1 1\n1 2",
"output": "2"
},
{
"input": "1 2\n1 9\n1 6",
"output": "9"
},
{
"input": "1 10\n1 1\n1 9\n1 3\n1 9\n1 7\n1 10\n1 4\n1 7\n1 3\n1 1",
"ou... | 186 | 7,065,600 | -1 | 4,432 |
894 | Ralph And His Magic Field | [
"combinatorics",
"constructive algorithms",
"math",
"number theory"
] | null | null | Ralph has a magic field which is divided into *n*<=Γ<=*m* blocks. That is to say, there are *n* rows and *m* columns on the field. Ralph can put an integer in each block. However, the magic field doesn't always work properly. It works only if the product of integers in each row and each column equals to *k*, where *k* is either 1 or -1.
Now Ralph wants you to figure out the number of ways to put numbers in each block in such a way that the magic field works properly. Two ways are considered different if and only if there exists at least one block where the numbers in the first way and in the second way are different. You are asked to output the answer modulo 1000000007<==<=109<=+<=7.
Note that there is no range of the numbers to put in the blocks, but we can prove that the answer is not infinity. | The only line contains three integers *n*, *m* and *k* (1<=β€<=*n*,<=*m*<=β€<=1018, *k* is either 1 or -1). | Print a single number denoting the answer modulo 1000000007. | [
"1 1 -1\n",
"1 3 1\n",
"3 3 -1\n"
] | [
"1\n",
"1\n",
"16\n"
] | In the first example the only way is to put -1 into the only block.
In the second example the only way is to put 1 into every block. | [
{
"input": "1 1 -1",
"output": "1"
},
{
"input": "1 3 1",
"output": "1"
},
{
"input": "3 3 -1",
"output": "16"
},
{
"input": "2 7 1",
"output": "64"
},
{
"input": "1 1 1",
"output": "1"
},
{
"input": "2 4 -1",
"output": "8"
},
{
"input": "1... | 1,000 | 43,110,400 | 0 | 4,435 | |
147 | Punctuation | [
"implementation",
"strings"
] | null | null | You are given a text that consists of lowercase Latin letters, spaces and punctuation marks (dot, comma, exclamation mark and question mark). A word is defined as a sequence of consecutive Latin letters.
Your task is to add spaces to the text by the following rules:
- if there is no punctuation mark between two words, then they should be separated by exactly one space - there should be no spaces before each punctuation mark - there should be exactly one space after each punctuation mark
It is guaranteed that there is at least one word between any two punctuation marks. The text begins and ends with a Latin letter. | The input data contains of a single non-empty line β the text whose length is no more than 10000 characters. | Print the text, edited according to the rules. In this problem you should follow the output format very strictly. For example, extra space at the end of the output line is considered as wrong answer. Note that a newline character at the end of the line doesn't matter. | [
"galileo galilei was an italian physicist ,mathematician,astronomer\n",
"galileo was born in pisa\n"
] | [
"galileo galilei was an italian physicist, mathematician, astronomer\n",
"galileo was born in pisa\n"
] | none | [
{
"input": "galileo galilei was an italian physicist ,mathematician,astronomer",
"output": "galileo galilei was an italian physicist, mathematician, astronomer"
},
{
"input": "galileo was born in pisa",
"output": "galileo was born in pisa"
},
{
"input": "jkhksdfhsdfsf",
"outpu... | 436 | 2,048,000 | 0 | 4,440 | |
774 | Big Number and Remainder | [
"*special",
"math",
"number theory"
] | null | null | Stepan has a very big positive integer.
Let's consider all cyclic shifts of Stepan's integer (if we look at his integer like at a string) which are also integers (i.e. they do not have leading zeros). Let's call such shifts as good shifts. For example, for the integer 10203 the good shifts are the integer itself 10203 and integers 20310 and 31020.
Stepan wants to know the minimum remainder of the division by the given number *m* among all good shifts. Your task is to determine the minimum remainder of the division by *m*. | The first line contains the integer which Stepan has. The length of Stepan's integer is between 2 and 200<=000 digits, inclusive. It is guaranteed that Stepan's integer does not contain leading zeros.
The second line contains the integer *m* (2<=β€<=*m*<=β€<=108) β the number by which Stepan divides good shifts of his integer. | Print the minimum remainder which Stepan can get if he divides all good shifts of his integer by the given number *m*. | [
"521\n3\n",
"1001\n5\n",
"5678901234567890123456789\n10000\n"
] | [
"2\n",
"0\n",
"123\n"
] | In the first example all good shifts of the integer 521 (good shifts are equal to 521, 215 and 152) has same remainder 2 when dividing by 3.
In the second example there are only two good shifts: the Stepan's integer itself and the shift by one position to the right. The integer itself is 1001 and the remainder after dividing it by 5 equals 1. The shift by one position to the right equals to 1100 and the remainder after dividing it by 5 equals 0, which is the minimum possible remainder. | [
{
"input": "521\n3",
"output": "2"
},
{
"input": "1001\n5",
"output": "0"
},
{
"input": "5678901234567890123456789\n10000",
"output": "123"
},
{
"input": "552352155\n13",
"output": "2"
},
{
"input": "11533077525260\n193983",
"output": "22331"
},
{
"inp... | 1,794 | 30,310,400 | 3 | 4,445 | |
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<=<<=109; *cnt*1<=+<=*cnt*2<=β€<=109; 2<=β€<=*x*<=<<=*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... | 31 | 4,300,800 | 0 | 4,448 | |
41 | Martian Dollar | [
"brute force"
] | B. Martian Dollar | 2 | 256 | One day Vasya got hold of information on the Martian dollar course in bourles for the next *n* days. The buying prices and the selling prices for one dollar on day *i* are the same and are equal to *a**i*. Vasya has *b* bourles. He can buy a certain number of dollars and then sell it no more than once in *n* days. According to Martian laws, one can buy only an integer number of dollars. Which maximal sum of money in bourles can Vasya get by the end of day *n*? | The first line contains two integers *n* and *b* (1<=β€<=*n*,<=*b*<=β€<=2000) β the number of days and the initial number of money in bourles. The next line contains *n* integers *a**i* (1<=β€<=*a**i*<=β€<=2000) β the prices of Martian dollars. | Print the single number β which maximal sum of money in bourles can Vasya get by the end of day *n*. | [
"2 4\n3 7\n",
"4 10\n4 3 2 1\n",
"4 10\n4 2 3 1\n"
] | [
"8\n",
"10\n",
"15\n"
] | none | [
{
"input": "2 4\n3 7",
"output": "8"
},
{
"input": "4 10\n4 3 2 1",
"output": "10"
},
{
"input": "4 10\n4 2 3 1",
"output": "15"
},
{
"input": "2 755\n51 160",
"output": "2281"
},
{
"input": "3 385\n978 1604 1888",
"output": "385"
},
{
"input": "4 1663... | 62 | 4,608,000 | 0 | 4,449 |
254 | Rats | [
"brute force",
"dfs and similar",
"graphs",
"implementation",
"shortest paths"
] | null | null | Rats have bred to hundreds and hundreds in the basement of the store, owned by Vasily Petrovich. Vasily Petrovich may have not noticed their presence, but they got into the habit of sneaking into the warehouse and stealing food from there. Vasily Petrovich cannot put up with it anymore, he has to destroy the rats in the basement. Since mousetraps are outdated and do not help, and rat poison can poison inattentive people as well as rats, he chose a radical way: to blow up two grenades in the basement (he does not have more).
In this problem, we will present the shop basement as a rectangular table of *n*<=Γ<=*m* cells. Some of the cells are occupied by walls, and the rest of them are empty. Vasily has been watching the rats and he found out that at a certain time they go to sleep, and all the time they sleep in the same places. He wants to blow up a grenade when this convenient time comes. On the plan of his basement, he marked cells with sleeping rats in them. Naturally, these cells are not occupied by walls.
Grenades can only blow up in a cell that is not occupied by a wall. The blast wave from a grenade distributes as follows. We assume that the grenade blast occurs at time 0. During this initial time only the cell where the grenade blew up gets 'clear'. If at time *t* some cell is clear, then at time *t*<=+<=1 those side-neighbouring cells which are not occupied by the walls get clear too (some of them could have been cleared before). The blast wave distributes for exactly *d* seconds, then it dies immediately.
Vasily Petrovich wonders, whether he can choose two cells to blast the grenades so as to clear all cells with sleeping rats. Write the program that finds it out. | The first line contains three integers *n*, *m* and *d*, separated by single spaces (4<=β€<=*n*,<=*m*<=β€<=1000,<=1<=β€<=*d*<=β€<=8). Next *n* lines contain the table that represents the basement plan. Each row of the table consists of *m* characters. Character "X" means that the corresponding cell is occupied by the wall, character "." represents a empty cell, character "R" represents a empty cell with sleeping rats.
It is guaranteed that the first and the last row, as well as the first and the last column consist of characters "X". The plan has at least two empty cells. There is at least one cell with sleeping rats. | If it is impossible to blow up all cells with sleeping rats, print a single integer -1. Otherwise, print four space-separated integers *r*1,<=*c*1,<=*r*2,<=*c*2, that mean that one grenade should go off in cell (*r*1,<=*c*1), and the other one β in cell (*r*2,<=*c*2).
Consider the table rows numbered from top to bottom from 1 to *n* and the table columns β from left to right from 1 to *m*. As *r*1 and *r*2 represent the row numbers, and *c*1 and *c*2 represent the column numbers in the table, they should fit the limits: 1<=β€<=*r*1,<=*r*2<=β€<=*n*,<=1<=β€<=*c*1,<=*c*2<=β€<=*m*. It is forbidden to blow a grenade twice in the same cell. The blast waves of the grenades can intersect. It is possible that one grenade blast destroys no rats, and the other one destroys all of them. | [
"4 4 1\nXXXX\nXR.X\nX.RX\nXXXX\n",
"9 14 5\nXXXXXXXXXXXXXX\nX....R...R...X\nX..R.........X\nX....RXR..R..X\nX..R...X.....X\nXR.R...X.....X\nX....XXR.....X\nX....R..R.R..X\nXXXXXXXXXXXXXX\n",
"7 7 1\nXXXXXXX\nX.R.R.X\nX.....X\nX..X..X\nX..R..X\nX....RX\nXXXXXXX\n"
] | [
"2 2 2 3\n",
"2 3 6 9\n",
"-1\n"
] | none | [
{
"input": "4 4 1\nXXXX\nXR.X\nX.RX\nXXXX",
"output": "2 2 2 3"
},
{
"input": "9 14 5\nXXXXXXXXXXXXXX\nX....R...R...X\nX..R.........X\nX....RXR..R..X\nX..R...X.....X\nXR.R...X.....X\nX....XXR.....X\nX....R..R.R..X\nXXXXXXXXXXXXXX",
"output": "2 3 6 9"
},
{
"input": "7 7 1\nXXXXXXX\nX.R.R... | 46 | 0 | 0 | 4,455 | |
687 | NP-Hard Problem | [
"dfs and similar",
"graphs"
] | null | null | Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting.
Suppose the graph *G* is given. Subset *A* of its vertices is called a vertex cover of this graph, if for each edge *uv* there is at least one endpoint of it in this set, i.e. or (or both).
Pari and Arya have won a great undirected graph as an award in a team contest. Now they have to split it in two parts, but both of them want their parts of the graph to be a vertex cover.
They have agreed to give you their graph and you need to find two disjoint subsets of its vertices *A* and *B*, such that both *A* and *B* are vertex cover or claim it's impossible. Each vertex should be given to no more than one of the friends (or you can even keep it for yourself). | The first line of the input contains two integers *n* and *m* (2<=β€<=*n*<=β€<=100<=000, 1<=β€<=*m*<=β€<=100<=000)Β β the number of vertices and the number of edges in the prize graph, respectively.
Each of the next *m* lines contains a pair of integers *u**i* and *v**i* (1<=<=β€<=<=*u**i*,<=<=*v**i*<=<=β€<=<=*n*), denoting an undirected edge between *u**i* and *v**i*. It's guaranteed the graph won't contain any self-loops or multiple edges. | If it's impossible to split the graph between Pari and Arya as they expect, print "-1" (without quotes).
If there are two disjoint sets of vertices, such that both sets are vertex cover, print their descriptions. Each description must contain two lines. The first line contains a single integer *k* denoting the number of vertices in that vertex cover, and the second line contains *k* integersΒ β the indices of vertices. Note that because of *m*<=β₯<=1, vertex cover cannot be empty. | [
"4 2\n1 2\n2 3\n",
"3 3\n1 2\n2 3\n1 3\n"
] | [
"1\n2 \n2\n1 3 \n",
"-1\n"
] | In the first sample, you can give the vertex number 2 to Arya and vertices numbered 1 and 3 to Pari and keep vertex number 4 for yourself (or give it someone, if you wish).
In the second sample, there is no way to satisfy both Pari and Arya. | [
{
"input": "4 2\n1 2\n2 3",
"output": "1\n2 \n2\n1 3 "
},
{
"input": "3 3\n1 2\n2 3\n1 3",
"output": "-1"
},
{
"input": "5 7\n3 2\n5 4\n3 4\n1 3\n1 5\n1 4\n2 5",
"output": "-1"
},
{
"input": "10 11\n4 10\n8 10\n2 3\n2 4\n7 1\n8 5\n2 8\n7 2\n1 2\n2 9\n6 8",
"output": "-1"
... | 779 | 268,390,400 | 0 | 4,459 | |
675 | Money Transfers | [
"constructive algorithms",
"data structures",
"greedy",
"sortings"
] | null | null | There are *n* banks in the city where Vasya lives, they are located in a circle, such that any two banks are neighbouring if their indices differ by no more than 1. Also, bank 1 and bank *n* are neighbours if *n*<=><=1. No bank is a neighbour of itself.
Vasya has an account in each bank. Its balance may be negative, meaning Vasya owes some money to this bank.
There is only one type of operations available: transfer some amount of money from any bank to account in any neighbouring bank. There are no restrictions on the size of the sum being transferred or balance requirements to perform this operation.
Vasya doesn't like to deal with large numbers, so he asks you to determine the minimum number of operations required to change the balance of each bank account to zero. It's guaranteed, that this is possible to achieve, that is, the total balance of Vasya in all banks is equal to zero. | The first line of the input contains a single integer *n* (1<=β€<=*n*<=β€<=100<=000)Β β the number of banks.
The second line contains *n* integers *a**i* (<=-<=109<=β€<=*a**i*<=β€<=109), the *i*-th of them is equal to the initial balance of the account in the *i*-th bank. It's guaranteed that the sum of all *a**i* is equal to 0. | Print the minimum number of operations required to change balance in each bank to zero. | [
"3\n5 0 -5\n",
"4\n-1 0 1 0\n",
"4\n1 2 3 -6\n"
] | [
"1\n",
"2\n",
"3\n"
] | In the first sample, Vasya may transfer 5 from the first bank to the third.
In the second sample, Vasya may first transfer 1 from the third bank to the second, and then 1 from the second to the first.
In the third sample, the following sequence provides the optimal answer:
1. transfer 1 from the first bank to the second bank; 1. transfer 3 from the second bank to the third; 1. transfer 6 from the third bank to the fourth. | [
{
"input": "3\n5 0 -5",
"output": "1"
},
{
"input": "4\n-1 0 1 0",
"output": "2"
},
{
"input": "4\n1 2 3 -6",
"output": "3"
},
{
"input": "1\n0",
"output": "0"
},
{
"input": "50\n108431864 128274949 -554057370 -384620666 -202862975 -803855410 -482167063 -55139054 ... | 217 | 17,920,000 | 3 | 4,465 | |
190 | STL | [
"dfs and similar"
] | null | null | Vasya used to be an accountant before the war began and he is one of the few who knows how to operate a computer, so he was assigned as the programmer.
We all know that programs often store sets of integers. For example, if we have a problem about a weighted directed graph, its edge can be represented by three integers: the number of the starting vertex, the number of the final vertex and the edge's weight. So, as Vasya was trying to represent characteristics of a recently invented robot in his program, he faced the following problem.
Vasya is not a programmer, so he asked his friend Gena, what the convenient way to store *n* integers is. Gena used to code in language X-- and so he can use only the types that occur in this language. Let's define, what a "type" is in language X--:
- First, a type is a string "int". - Second, a type is a string that starts with "pair", then followed by angle brackets listing exactly two comma-separated other types of language X--. This record contains no spaces. - No other strings can be regarded as types.
More formally: type := int | pair<type,type>. For example, Gena uses the following type for graph edges: pair<int,pair<int,int>>.
Gena was pleased to help Vasya, he dictated to Vasya a type of language X--, that stores *n* integers. Unfortunately, Gena was in a hurry, so he omitted the punctuation. Now Gena has already left and Vasya can't find the correct punctuation, resulting in a type of language X--, however hard he tries.
Help Vasya and add the punctuation marks so as to receive the valid type of language X--. Otherwise say that the task is impossible to perform. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=105), showing how many numbers the type dictated by Gena contains.
The second line contains space-separated words, said by Gena. Each of them is either "pair" or "int" (without the quotes).
It is guaranteed that the total number of words does not exceed 105 and that among all the words that Gena said, there are exactly *n* words "int". | If it is possible to add the punctuation marks so as to get a correct type of language X-- as a result, print a single line that represents the resulting type. Otherwise, print "Error occurred" (without the quotes). Inside the record of a type should not be any extra spaces and other characters.
It is guaranteed that if such type exists, then it is unique.
Note that you should print the type dictated by Gena (if such type exists) and not any type that can contain *n* values. | [
"3\npair pair int int int\n",
"1\npair int\n"
] | [
"pair<pair<int,int>,int>",
"Error occurred"
] | none | [
{
"input": "3\npair pair int int int",
"output": "pair<pair<int,int>,int>"
},
{
"input": "1\npair int",
"output": "Error occurred"
},
{
"input": "4\npair pair int int pair int int",
"output": "pair<pair<int,int>,pair<int,int>>"
},
{
"input": "4\npair pair pair int int int int... | 2,000 | 11,264,000 | 0 | 4,504 | |
675 | Trains and Statistic | [
"data structures",
"dp",
"greedy"
] | null | null | Vasya commutes by train every day. There are *n* train stations in the city, and at the *i*-th station it's possible to buy only tickets to stations from *i*<=+<=1 to *a**i* inclusive. No tickets are sold at the last station.
Let Ο*i*,<=*j* be the minimum number of tickets one needs to buy in order to get from stations *i* to station *j*. As Vasya is fond of different useless statistic he asks you to compute the sum of all values Ο*i*,<=*j* among all pairs 1<=β€<=*i*<=<<=*j*<=β€<=*n*. | The first line of the input contains a single integer *n* (2<=β€<=*n*<=β€<=100<=000)Β β the number of stations.
The second line contains *n*<=-<=1 integer *a**i* (*i*<=+<=1<=β€<=*a**i*<=β€<=*n*), the *i*-th of them means that at the *i*-th station one may buy tickets to each station from *i*<=+<=1 to *a**i* inclusive. | Print the sum of Ο*i*,<=*j* among all pairs of 1<=β€<=*i*<=<<=*j*<=β€<=*n*. | [
"4\n4 4 4\n",
"5\n2 3 5 5\n"
] | [
"6\n",
"17\n"
] | In the first sample it's possible to get from any station to any other (with greater index) using only one ticket. The total number of pairs is 6, so the answer is also 6.
Consider the second sample:
- Ο<sub class="lower-index">1,β2</sub>β=β1 - Ο<sub class="lower-index">1,β3</sub>β=β2 - Ο<sub class="lower-index">1,β4</sub>β=β3 - Ο<sub class="lower-index">1,β5</sub>β=β3 - Ο<sub class="lower-index">2,β3</sub>β=β1 - Ο<sub class="lower-index">2,β4</sub>β=β2 - Ο<sub class="lower-index">2,β5</sub>β=β2 - Ο<sub class="lower-index">3,β4</sub>β=β1 - Ο<sub class="lower-index">3,β5</sub>β=β1 - Ο<sub class="lower-index">4,β5</sub>β=β1
Thus the answer equals 1β+β2β+β3β+β3β+β1β+β2β+β2β+β1β+β1β+β1β=β17. | [
{
"input": "4\n4 4 4",
"output": "6"
},
{
"input": "5\n2 3 5 5",
"output": "17"
},
{
"input": "2\n2",
"output": "1"
},
{
"input": "10\n2 10 8 7 8 8 10 9 10",
"output": "63"
},
{
"input": "3\n3 3",
"output": "3"
},
{
"input": "4\n3 3 4",
"output": "... | 46 | 0 | 0 | 4,507 | |
111 | Petya and Spiders | [
"bitmasks",
"dp",
"dsu"
] | C. Petya and Spiders | 2 | 256 | Little Petya loves training spiders. Petya has a board *n*<=Γ<=*m* in size. Each cell of the board initially has a spider sitting on it. After one second Petya chooses a certain action for each spider, and all of them humbly perform its commands. There are 5 possible commands: to stay idle or to move from current cell to some of the four side-neighboring cells (that is, one command for each of the four possible directions). Petya gives the commands so that no spider leaves the field. It is allowed for spiders to pass through each other when they crawl towards each other in opposite directions. All spiders crawl simultaneously and several spiders may end up in one cell. Petya wants to know the maximum possible number of spider-free cells after one second. | The first line contains two space-separated integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=40,<=*n*Β·*m*<=β€<=40) β the board sizes. | In the first line print the maximum number of cells without spiders. | [
"1 1\n",
"2 3\n"
] | [
"0\n",
"4\n"
] | In the first sample the only possible answer is:
s
In the second sample one of the possible solutions is:
s denotes command "stay idle", l, r, d, u denote commands "crawl left", "crawl right", "crawl down", "crawl up", correspondingly. | [
{
"input": "1 1",
"output": "0"
},
{
"input": "2 3",
"output": "4"
},
{
"input": "4 1",
"output": "2"
},
{
"input": "4 2",
"output": "5"
},
{
"input": "4 3",
"output": "8"
},
{
"input": "4 4",
"output": "12"
},
{
"input": "1 40",
"outpu... | 46 | 0 | 0 | 4,515 |
518 | Ilya and Escalator | [
"combinatorics",
"dp",
"math",
"probabilities"
] | null | null | Ilya got tired of sports programming, left university and got a job in the subway. He was given the task to determine the escalator load factor.
Let's assume that *n* people stand in the queue for the escalator. At each second one of the two following possibilities takes place: either the first person in the queue enters the escalator with probability *p*, or the first person in the queue doesn't move with probability (1<=-<=*p*), paralyzed by his fear of escalators and making the whole queue wait behind him.
Formally speaking, the *i*-th person in the queue cannot enter the escalator until people with indices from 1 to *i*<=-<=1 inclusive enter it. In one second only one person can enter the escalator. The escalator is infinite, so if a person enters it, he never leaves it, that is he will be standing on the escalator at any following second. Ilya needs to count the expected value of the number of people standing on the escalator after *t* seconds.
Your task is to help him solve this complicated task. | The first line of the input contains three numbers *n*,<=*p*,<=*t* (1<=β€<=*n*,<=*t*<=β€<=2000, 0<=β€<=*p*<=β€<=1). Numbers *n* and *t* are integers, number *p* is real, given with exactly two digits after the decimal point. | Print a single real number β the expected number of people who will be standing on the escalator after *t* seconds. The absolute or relative error mustn't exceed 10<=-<=6. | [
"1 0.50 1\n",
"1 0.50 4\n",
"4 0.20 2\n"
] | [
"0.5\n",
"0.9375\n",
"0.4\n"
] | none | [
{
"input": "1 0.50 1",
"output": "0.500000000000000"
},
{
"input": "1 0.50 4",
"output": "0.937500000000000"
},
{
"input": "4 0.20 2",
"output": "0.400000000000000"
},
{
"input": "2000 0.61 2000",
"output": "1219.999999999999545"
},
{
"input": "100 1.00 200",
... | 77 | 2,764,800 | -1 | 4,518 | |
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... | 280 | 5,529,600 | 3.9197 | 4,519 |
446 | DZY Loves Fibonacci Numbers | [
"data structures",
"math",
"number theory"
] | null | null | In mathematical terms, the sequence *F**n* of Fibonacci numbers is defined by the recurrence relation
DZY loves Fibonacci numbers very much. Today DZY gives you an array consisting of *n* integers: *a*1,<=*a*2,<=...,<=*a**n*. Moreover, there are *m* queries, each query has one of the two types:
1. Format of the query "1 *l* *r*". In reply to the query, you need to add *F**i*<=-<=*l*<=+<=1 to each element *a**i*, where *l*<=β€<=*i*<=β€<=*r*. 1. Format of the query "2 *l* *r*". In reply to the query you should output the value of modulo 1000000009Β (109<=+<=9).
Help DZY reply to all the queries. | The first line of the input contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=300000). The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n*Β (1<=β€<=*a**i*<=β€<=109) β initial array *a*.
Then, *m* lines follow. A single line describes a single query in the format given in the statement. It is guaranteed that for each query inequality 1<=β€<=*l*<=β€<=*r*<=β€<=*n* holds. | For each query of the second type, print the value of the sum on a single line. | [
"4 4\n1 2 3 4\n1 1 4\n2 1 4\n1 2 4\n2 1 3\n"
] | [
"17\n12\n"
] | After the first query, *a*β=β[2,β3,β5,β7].
For the second query, *sum*β=β2β+β3β+β5β+β7β=β17.
After the third query, *a*β=β[2,β4,β6,β9].
For the fourth query, *sum*β=β2β+β4β+β6β=β12. | [
{
"input": "4 4\n1 2 3 4\n1 1 4\n2 1 4\n1 2 4\n2 1 3",
"output": "17\n12"
},
{
"input": "2 2\n1 2\n2 1 2\n2 1 2",
"output": "3\n3"
},
{
"input": "10 20\n56 12 68 23 41 29 97 69 79 76\n1 3 5\n1 8 9\n2 3 10\n1 7 10\n2 1 10\n1 5 10\n2 2 4\n1 2 4\n1 5 6\n2 1 2\n2 4 5\n1 1 5\n1 8 9\n2 5 8\n2 ... | 31 | 0 | 0 | 4,523 | |
935 | Fafa and the Gates | [
"implementation"
] | null | null | Two neighboring kingdoms decided to build a wall between them with some gates to enable the citizens to go from one kingdom to another. Each time a citizen passes through a gate, he has to pay one silver coin.
The world can be represented by the first quadrant of a plane and the wall is built along the identity line (i.e. the line with the equation *x*<==<=*y*). Any point below the wall belongs to the first kingdom while any point above the wall belongs to the second kingdom. There is a gate at any integer point on the line (i.e. at points (0,<=0), (1,<=1), (2,<=2), ...). The wall and the gates do not belong to any of the kingdoms.
Fafa is at the gate at position (0,<=0) and he wants to walk around in the two kingdoms. He knows the sequence *S* of moves he will do. This sequence is a string where each character represents a move. The two possible moves Fafa will do are 'U' (move one step up, from (*x*,<=*y*) to (*x*,<=*y*<=+<=1)) and 'R' (move one step right, from (*x*,<=*y*) to (*x*<=+<=1,<=*y*)).
Fafa wants to know the number of silver coins he needs to pay to walk around the two kingdoms following the sequence *S*. Note that if Fafa visits a gate without moving from one kingdom to another, he pays no silver coins. Also assume that he doesn't pay at the gate at point (0,<=0), i.Β e. he is initially on the side he needs. | The first line of the input contains single integer *n* (1<=β€<=*n*<=β€<=105) β the number of moves in the walking sequence.
The second line contains a string *S* of length *n* consisting of the characters 'U' and 'R' describing the required moves. Fafa will follow the sequence *S* in order from left to right. | On a single line, print one integer representing the number of silver coins Fafa needs to pay at the gates to follow the sequence *S*. | [
"1\nU\n",
"6\nRURUUR\n",
"7\nURRRUUU\n"
] | [
"0\n",
"1\n",
"2\n"
] | The figure below describes the third sample. The red arrows represent the sequence of moves Fafa will follow. The green gates represent the gates at which Fafa have to pay silver coins. | [
{
"input": "1\nU",
"output": "0"
},
{
"input": "6\nRURUUR",
"output": "1"
},
{
"input": "7\nURRRUUU",
"output": "2"
},
{
"input": "100\nRUURUURRUURUUUUURRUUURRRRUURRURRURRRRUUUUUURRUURRRRURUUURUURURRRRRURUURRUURUURRUUURUUUUUURRUUUURUUUR",
"output": "3"
},
{
"input... | 77 | 102,400 | 3 | 4,532 | |
0 | none | [
"none"
] | null | null | Young Teodor enjoys drawing. His favourite hobby is drawing segments with integer borders inside his huge [1;*m*] segment. One day Teodor noticed that picture he just drawn has one interesting feature: there doesn't exist an integer point, that belongs each of segments in the picture. Having discovered this fact, Teodor decided to share it with Sasha.
Sasha knows that Teodor likes to show off so he never trusts him. Teodor wants to prove that he can be trusted sometimes, so he decided to convince Sasha that there is no such integer point in his picture, which belongs to each segment. However Teodor is lazy person and neither wills to tell Sasha all coordinates of segments' ends nor wills to tell him their amount, so he suggested Sasha to ask him series of questions 'Given the integer point *x**i*, how many segments in Fedya's picture contain that point?', promising to tell correct answers for this questions.
Both boys are very busy studying and don't have much time, so they ask you to find out how many questions can Sasha ask Teodor, that having only answers on his questions, Sasha can't be sure that Teodor isn't lying to him. Note that Sasha doesn't know amount of segments in Teodor's picture. Sure, Sasha is smart person and never asks about same point twice. | First line of input contains two integer numbers: *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=100<=000)Β β amount of segments of Teodor's picture and maximal coordinate of point that Sasha can ask about.
*i*th of next *n* lines contains two integer numbers *l**i* and *r**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=*m*)Β β left and right ends of *i*th segment in the picture. Note that that left and right ends of segment can be the same point.
It is guaranteed that there is no integer point, that belongs to all segments. | Single line of output should contain one integer number *k* β size of largest set (*x**i*,<=*cnt*(*x**i*)) where all *x**i* are different, 1<=β€<=*x**i*<=β€<=*m*, and *cnt*(*x**i*) is amount of segments, containing point with coordinate *x**i*, such that one can't be sure that there doesn't exist point, belonging to all of segments in initial picture, if he knows only this set(and doesn't know *n*). | [
"2 4\n1 2\n3 4\n",
"4 6\n1 3\n2 3\n4 6\n5 6\n"
] | [
"4\n",
"5\n"
] | First example shows situation where Sasha can never be sure that Teodor isn't lying to him, because even if one knows *cnt*(*x*<sub class="lower-index">*i*</sub>) for each point in segment [1;4], he can't distinguish this case from situation Teodor has drawn whole [1;4] segment.
In second example Sasha can ask about 5 points e.g. 1,β2,β3,β5,β6, still not being sure if Teodor haven't lied to him. But once he knows information about all points in [1;6] segment, Sasha can be sure that Teodor haven't lied to him. | [
{
"input": "2 4\n1 2\n3 4",
"output": "4"
},
{
"input": "4 6\n1 3\n2 3\n4 6\n5 6",
"output": "5"
},
{
"input": "43 1319\n750 1030\n857 946\n941 1203\n407 1034\n947 1290\n546 585\n630 1201\n72 342\n693 1315\n34 719\n176 1097\n36 931\n198 973\n5 1025\n892 1054\n461 1287\n195 1273\n832 1039... | 109 | 139,264,000 | 0 | 4,538 | |
999 | Reversing Encryption | [
"implementation"
] | null | null | A string $s$ of length $n$ can be encrypted by the following algorithm:
- iterate over all divisors of $n$ in decreasing order (i.e. from $n$ to $1$), - for each divisor $d$, reverse the substring $s[1 \dots d]$ (i.e. the substring which starts at position $1$ and ends at position $d$).
For example, the above algorithm applied to the string $s$="codeforces" leads to the following changes: "codeforces" $\to$ "secrofedoc" $\to$ "orcesfedoc" $\to$ "rocesfedoc" $\to$ "rocesfedoc" (obviously, the last reverse operation doesn't change the string because $d=1$).
You are given the encrypted string $t$. Your task is to decrypt this string, i.e., to find a string $s$ such that the above algorithm results in string $t$. It can be proven that this string $s$ always exists and is unique. | The first line of input consists of a single integer $n$ ($1 \le n \le 100$) β the length of the string $t$. The second line of input consists of the string $t$. The length of $t$ is $n$, and it consists only of lowercase Latin letters. | Print a string $s$ such that the above algorithm results in $t$. | [
"10\nrocesfedoc\n",
"16\nplmaetwoxesisiht\n",
"1\nz\n"
] | [
"codeforces\n",
"thisisexampletwo\n",
"z\n"
] | The first example is described in the problem statement. | [
{
"input": "10\nrocesfedoc",
"output": "codeforces"
},
{
"input": "16\nplmaetwoxesisiht",
"output": "thisisexampletwo"
},
{
"input": "1\nz",
"output": "z"
},
{
"input": "2\nir",
"output": "ri"
},
{
"input": "3\nilj",
"output": "jli"
},
{
"input": "4\nj... | 62 | 307,200 | 3 | 4,543 | |
977 | Divide by three, multiply by two | [
"dfs and similar",
"math",
"sortings"
] | null | null | Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds:
- divide the number $x$ by $3$ ($x$ must be divisible by $3$); - multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.
You are given a sequence of length $n$ β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists. | The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board. | Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists. | [
"6\n4 8 6 3 12 9\n",
"4\n42 28 84 126\n",
"2\n1000000000000000000 3000000000000000000\n"
] | [
"9 3 6 12 4 8 \n",
"126 42 84 28 \n",
"3000000000000000000 1000000000000000000 \n"
] | In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$. | [
{
"input": "6\n4 8 6 3 12 9",
"output": "9 3 6 12 4 8 "
},
{
"input": "4\n42 28 84 126",
"output": "126 42 84 28 "
},
{
"input": "2\n1000000000000000000 3000000000000000000",
"output": "3000000000000000000 1000000000000000000 "
},
{
"input": "19\n46875000000000000 73242187500... | 62 | 1,433,600 | 3 | 4,544 | |
680 | Bear and Five Cards | [
"constructive algorithms",
"implementation"
] | null | null | A little bear Limak plays a game. He has five cards. There is one number written on each card. Each number is a positive integer.
Limak can discard (throw out) some cards. His goal is to minimize the sum of numbers written on remaining (not discarded) cards.
He is allowed to at most once discard two or three cards with the same number. Of course, he won't discard cards if it's impossible to choose two or three cards with the same number.
Given five numbers written on cards, cay you find the minimum sum of numbers on remaining cards? | The only line of the input contains five integers *t*1, *t*2, *t*3, *t*4 and *t*5 (1<=β€<=*t**i*<=β€<=100)Β β numbers written on cards. | Print the minimum possible sum of numbers written on remaining cards. | [
"7 3 7 3 20\n",
"7 9 3 1 8\n",
"10 10 10 10 10\n"
] | [
"26\n",
"28\n",
"20\n"
] | In the first sample, Limak has cards with numbers 7, 3, 7, 3 and 20. Limak can do one of the following.
- Do nothing and the sum would be 7β+β3β+β7β+β3β+β20β=β40. - Remove two cards with a number 7. The remaining sum would be 3β+β3β+β20β=β26. - Remove two cards with a number 3. The remaining sum would be 7β+β7β+β20β=β34.
You are asked to minimize the sum so the answer is 26.
In the second sample, it's impossible to find two or three cards with the same number. Hence, Limak does nothing and the sum is 7β+β9β+β1β+β3β+β8β=β28.
In the third sample, all cards have the same number. It's optimal to discard any three cards. The sum of two remaining numbers is 10β+β10β=β20. | [
{
"input": "7 3 7 3 20",
"output": "26"
},
{
"input": "7 9 3 1 8",
"output": "28"
},
{
"input": "10 10 10 10 10",
"output": "20"
},
{
"input": "8 7 1 8 7",
"output": "15"
},
{
"input": "7 7 7 8 8",
"output": "16"
},
{
"input": "8 8 8 2 2",
"output"... | 140 | 409,600 | 3 | 4,545 | |
65 | Harry Potter and Three Spells | [
"implementation",
"math"
] | A. Harry Potter and Three Spells | 2 | 256 | A long time ago (probably even in the first book), Nicholas Flamel, a great alchemist and the creator of the Philosopher's Stone, taught Harry Potter three useful spells. The first one allows you to convert *a* grams of sand into *b* grams of lead, the second one allows you to convert *c* grams of lead into *d* grams of gold and third one allows you to convert *e* grams of gold into *f* grams of sand. When Harry told his friends about these spells, Ron Weasley was amazed. After all, if they succeed in turning sand into lead, lead into gold, and then turning part of the gold into sand again and so on, then it will be possible to start with a small amount of sand and get huge amounts of gold! Even an infinite amount of gold! Hermione Granger, by contrast, was skeptical about that idea. She argues that according to the law of conservation of matter getting an infinite amount of matter, even using magic, is impossible. On the contrary, the amount of matter may even decrease during transformation, being converted to magical energy. Though Hermione's theory seems convincing, Ron won't believe her. As far as Ron is concerned, Hermione made up her law of conservation of matter to stop Harry and Ron wasting their time with this nonsense, and to make them go and do homework instead. That's why Ron has already collected a certain amount of sand for the experiments. A quarrel between the friends seems unavoidable...
Help Harry to determine which one of his friends is right, and avoid the quarrel after all. To do this you have to figure out whether it is possible to get the amount of gold greater than any preassigned number from some finite amount of sand. | The first line contains 6 integers *a*, *b*, *c*, *d*, *e*, *f* (0<=β€<=*a*,<=*b*,<=*c*,<=*d*,<=*e*,<=*f*<=β€<=1000). | Print "Ron", if it is possible to get an infinitely large amount of gold having a certain finite amount of sand (and not having any gold and lead at all), i.e., Ron is right. Otherwise, print "Hermione". | [
"100 200 250 150 200 250\n",
"100 50 50 200 200 100\n",
"100 10 200 20 300 30\n",
"0 0 0 0 0 0\n",
"1 1 0 1 1 1\n",
"1 0 1 2 1 2\n",
"100 1 100 1 0 1\n"
] | [
"Ron\n",
"Hermione\n",
"Hermione\n",
"Hermione\n",
"Ron\n",
"Hermione\n",
"Ron\n"
] | Consider the first sample. Let's start with the 500 grams of sand. Apply the first spell 5 times and turn the sand into 1000 grams of lead. Then apply the second spell 4 times to get 600 grams of gold. Letβs take 400 grams from the resulting amount of gold turn them back into sand. We get 500 grams of sand and 200 grams of gold. If we apply the same operations to 500 grams of sand again, we can get extra 200 grams of gold every time. Thus, you can get 200, 400, 600 etc. grams of gold, i.e., starting with a finite amount of sand (500 grams), you can get the amount of gold which is greater than any preassigned number.
In the forth sample it is impossible to get sand, or lead, or gold, applying the spells.
In the fifth sample an infinitely large amount of gold can be obtained by using only the second spell, which allows you to receive 1 gram of gold out of nothing. Note that if such a second spell is available, then the first and the third one do not affect the answer at all.
The seventh sample is more interesting. We can also start with a zero amount of sand there. With the aid of the third spell you can get sand out of nothing. We get 10000 grams of sand in this manner. Let's get 100 grams of lead using the first spell 100 times. Then make 1 gram of gold from them. We managed to receive 1 gram of gold, starting with a zero amount of sand! Clearly, in this manner you can get an infinitely large amount of gold. | [
{
"input": "100 200 250 150 200 250",
"output": "Ron"
},
{
"input": "100 50 50 200 200 100",
"output": "Hermione"
},
{
"input": "100 10 200 20 300 30",
"output": "Hermione"
},
{
"input": "0 0 0 0 0 0",
"output": "Hermione"
},
{
"input": "1 1 0 1 1 1",
"output"... | 124 | 0 | 0 | 4,547 |
714 | Filya and Homework | [
"implementation",
"sortings"
] | null | null | Today, hedgehog Filya went to school for the very first time! Teacher gave him a homework which Filya was unable to complete without your help.
Filya is given an array of non-negative integers *a*1,<=*a*2,<=...,<=*a**n*. First, he pick an integer *x* and then he adds *x* to some elements of the array (no more than once), subtract *x* from some other elements (also, no more than once) and do no change other elements. He wants all elements of the array to be equal.
Now he wonders if it's possible to pick such integer *x* and change some elements of the array using this *x* in order to make all elements equal. | The first line of the input contains an integer *n* (1<=β€<=*n*<=β€<=100<=000)Β β the number of integers in the Filya's array. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=109)Β β elements of the array. | If it's impossible to make all elements of the array equal using the process given in the problem statement, then print "NO" (without quotes) in the only line of the output. Otherwise print "YES" (without quotes). | [
"5\n1 3 3 2 1\n",
"5\n1 2 3 4 5\n"
] | [
"YES\n",
"NO\n"
] | In the first sample Filya should select *x*β=β1, then add it to the first and the last elements of the array and subtract from the second and the third elements. | [
{
"input": "5\n1 3 3 2 1",
"output": "YES"
},
{
"input": "5\n1 2 3 4 5",
"output": "NO"
},
{
"input": "2\n1 2",
"output": "YES"
},
{
"input": "3\n1 2 3",
"output": "YES"
},
{
"input": "3\n1 1 1",
"output": "YES"
},
{
"input": "2\n1 1000000000",
"ou... | 124 | 0 | 0 | 4,548 | |
157 | Trace | [
"geometry",
"sortings"
] | null | null | One day, as Sherlock Holmes was tracking down one very important criminal, he found a wonderful painting on the wall. This wall could be represented as a plane. The painting had several concentric circles that divided the wall into several parts. Some parts were painted red and all the other were painted blue. Besides, any two neighboring parts were painted different colors, that is, the red and the blue color were alternating, i. e. followed one after the other. The outer area of the wall (the area that lied outside all circles) was painted blue. Help Sherlock Holmes determine the total area of red parts of the wall.
Let us remind you that two circles are called concentric if their centers coincide. Several circles are called concentric if any two of them are concentric. | The first line contains the single integer *n* (1<=β€<=*n*<=β€<=100). The second line contains *n* space-separated integers *r**i* (1<=β€<=*r**i*<=β€<=1000) β the circles' radii. It is guaranteed that all circles are different. | Print the single real number β total area of the part of the wall that is painted red. The answer is accepted if absolute or relative error doesn't exceed 10<=-<=4. | [
"1\n1\n",
"3\n1 4 2\n"
] | [
"3.1415926536\n",
"40.8407044967\n"
] | In the first sample the picture is just one circle of radius 1. Inner part of the circle is painted red. The area of the red part equals ΟβΓβ1<sup class="upper-index">2</sup>β=βΟ.
In the second sample there are three circles of radii 1, 4 and 2. Outside part of the second circle is painted blue. Part between the second and the third circles is painted red. Part between the first and the third is painted blue. And, finally, the inner part of the first circle is painted red. Overall there are two red parts: the ring between the second and the third circles and the inner part of the first circle. Total area of the red parts is equal (ΟβΓβ4<sup class="upper-index">2</sup>β-βΟβΓβ2<sup class="upper-index">2</sup>)β+βΟβΓβ1<sup class="upper-index">2</sup>β=βΟβΓβ12β+βΟβ=β13Ο | [
{
"input": "1\n1",
"output": "3.1415926536"
},
{
"input": "3\n1 4 2",
"output": "40.8407044967"
},
{
"input": "4\n4 1 3 2",
"output": "31.4159265359"
},
{
"input": "4\n100 10 2 1",
"output": "31111.1920484997"
},
{
"input": "10\n10 9 8 7 6 5 4 3 2 1",
"output"... | 186 | 0 | 0 | 4,549 | |
1,005 | Tanya and Stairways | [
"implementation"
] | null | null | Little girl Tanya climbs the stairs inside a multi-storey building. Every time Tanya climbs a stairway, she starts counting steps from $1$ to the number of steps in this stairway. She speaks every number aloud. For example, if she climbs two stairways, the first of which contains $3$ steps, and the second contains $4$ steps, she will pronounce the numbers $1, 2, 3, 1, 2, 3, 4$.
You are given all the numbers pronounced by Tanya. How many stairways did she climb? Also, output the number of steps in each stairway.
The given sequence will be a valid sequence that Tanya could have pronounced when climbing one or more stairways. | The first line contains $n$ ($1 \le n \le 1000$) β the total number of numbers pronounced by Tanya.
The second line contains integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 1000$) β all the numbers Tanya pronounced while climbing the stairs, in order from the first to the last pronounced number. Passing a stairway with $x$ steps, she will pronounce the numbers $1, 2, \dots, x$ in that order.
The given sequence will be a valid sequence that Tanya could have pronounced when climbing one or more stairways. | In the first line, output $t$ β the number of stairways that Tanya climbed. In the second line, output $t$ numbers β the number of steps in each stairway she climbed. Write the numbers in the correct order of passage of the stairways. | [
"7\n1 2 3 1 2 3 4\n",
"4\n1 1 1 1\n",
"5\n1 2 3 4 5\n",
"5\n1 2 1 2 1\n"
] | [
"2\n3 4 ",
"4\n1 1 1 1 ",
"1\n5 ",
"3\n2 2 1 "
] | none | [
{
"input": "7\n1 2 3 1 2 3 4",
"output": "2\n3 4 "
},
{
"input": "4\n1 1 1 1",
"output": "4\n1 1 1 1 "
},
{
"input": "5\n1 2 3 4 5",
"output": "1\n5 "
},
{
"input": "5\n1 2 1 2 1",
"output": "3\n2 2 1 "
},
{
"input": "1\n1",
"output": "1\n1 "
},
{
"inp... | 62 | 0 | 3 | 4,551 | |
177 | Encrypting Messages | [
"data structures"
] | null | null | The Smart Beaver from ABBYY invented a new message encryption method and now wants to check its performance. Checking it manually is long and tiresome, so he decided to ask the ABBYY Cup contestants for help.
A message is a sequence of *n* integers *a*1,<=*a*2,<=...,<=*a**n*. Encryption uses a key which is a sequence of *m* integers *b*1,<=*b*2,<=...,<=*b**m* (*m*<=β€<=*n*). All numbers from the message and from the key belong to the interval from 0 to *c*<=-<=1, inclusive, and all the calculations are performed modulo *c*.
Encryption is performed in *n*<=-<=*m*<=+<=1 steps. On the first step we add to each number *a*1,<=*a*2,<=...,<=*a**m* a corresponding number *b*1,<=*b*2,<=...,<=*b**m*. On the second step we add to each number *a*2,<=*a*3,<=...,<=*a**m*<=+<=1 (changed on the previous step) a corresponding number *b*1,<=*b*2,<=...,<=*b**m*. And so on: on step number *i* we add to each number *a**i*,<=*a**i*<=+<=1,<=...,<=*a**i*<=+<=*m*<=-<=1 a corresponding number *b*1,<=*b*2,<=...,<=*b**m*. The result of the encryption is the sequence *a*1,<=*a*2,<=...,<=*a**n* after *n*<=-<=*m*<=+<=1 steps.
Help the Beaver to write a program that will encrypt messages in the described manner. | The first input line contains three integers *n*, *m* and *c*, separated by single spaces.
The second input line contains *n* integers *a**i* (0<=β€<=*a**i*<=<<=*c*), separated by single spaces β the original message.
The third input line contains *m* integers *b**i* (0<=β€<=*b**i*<=<<=*c*), separated by single spaces β the encryption key.
The input limitations for getting 30 points are:
- 1<=β€<=*m*<=β€<=*n*<=β€<=103 - 1<=β€<=*c*<=β€<=103
The input limitations for getting 100 points are:
- 1<=β€<=*m*<=β€<=*n*<=β€<=105 - 1<=β€<=*c*<=β€<=103 | Print *n* space-separated integers β the result of encrypting the original message. | [
"4 3 2\n1 1 1 1\n1 1 1\n",
"3 1 5\n1 2 3\n4\n"
] | [
"0 1 1 0\n",
"0 1 2\n"
] | In the first sample the encryption is performed in two steps: after the first step *a*β=β(0,β0,β0,β1) (remember that the calculations are performed modulo 2), after the second step *a*β=β(0,β1,β1,β0), and that is the answer. | [
{
"input": "4 3 2\n1 1 1 1\n1 1 1",
"output": "0 1 1 0"
},
{
"input": "3 1 5\n1 2 3\n4",
"output": "0 1 2"
},
{
"input": "5 2 7\n0 0 1 2 4\n3 5",
"output": "3 1 2 3 2"
},
{
"input": "20 15 17\n4 9 14 11 15 16 15 4 0 10 7 12 10 1 8 6 7 14 1 13\n6 3 14 8 8 11 16 4 5 9 2 13 6 14... | 528 | 7,680,000 | 3 | 4,554 | |
158 | Taxi | [
"*special",
"greedy",
"implementation"
] | null | null | After the lessons *n* groups of schoolchildren went outside and decided to visit Polycarpus to celebrate his birthday. We know that the *i*-th group consists of *s**i* friends (1<=β€<=*s**i*<=β€<=4), and they want to go to Polycarpus together. They decided to get there by taxi. Each car can carry at most four passengers. What minimum number of cars will the children need if all members of each group should ride in the same taxi (but one taxi can take more than one group)? | The first line contains integer *n* (1<=β€<=*n*<=β€<=105) β the number of groups of schoolchildren. The second line contains a sequence of integers *s*1,<=*s*2,<=...,<=*s**n* (1<=β€<=*s**i*<=β€<=4). The integers are separated by a space, *s**i* is the number of children in the *i*-th group. | Print the single number β the minimum number of taxis necessary to drive all children to Polycarpus. | [
"5\n1 2 4 3 3\n",
"8\n2 3 4 4 2 1 3 1\n"
] | [
"4\n",
"5\n"
] | In the first test we can sort the children into four cars like this:
- the third group (consisting of four children), - the fourth group (consisting of three children), - the fifth group (consisting of three children), - the first and the second group (consisting of one and two children, correspondingly).
There are other ways to sort the groups into four cars. | [
{
"input": "5\n1 2 4 3 3",
"output": "4"
},
{
"input": "8\n2 3 4 4 2 1 3 1",
"output": "5"
},
{
"input": "5\n4 4 4 4 4",
"output": "5"
},
{
"input": "12\n1 1 1 1 1 1 1 1 1 1 1 1",
"output": "3"
},
{
"input": "2\n2 1",
"output": "1"
},
{
"input": "4\n3 ... | 60 | 0 | -1 | 4,568 | |
226 | Flying Saucer Segments | [
"math"
] | null | null | An expedition group flew from planet ACM-1 to Earth in order to study the bipedal species (its representatives don't even have antennas on their heads!).
The flying saucer, on which the brave pioneers set off, consists of three sections. These sections are connected by a chain: the 1-st section is adjacent only to the 2-nd one, the 2-nd one β to the 1-st and the 3-rd ones, the 3-rd one β only to the 2-nd one. The transitions are possible only between the adjacent sections.
The spacecraft team consists of *n* aliens. Each of them is given a rank β an integer from 1 to *n*. The ranks of all astronauts are distinct. The rules established on the Saucer, state that an alien may move from section *a* to section *b* only if it is senior in rank to all aliens who are in the segments *a* and *b* (besides, the segments *a* and *b* are of course required to be adjacent). Any alien requires exactly 1 minute to make a move. Besides, safety regulations require that no more than one alien moved at the same minute along the ship.
Alien *A* is senior in rank to alien *B*, if the number indicating rank *A*, is more than the corresponding number for *B*.
At the moment the whole saucer team is in the 3-rd segment. They all need to move to the 1-st segment. One member of the crew, the alien with the identification number CFR-140, decided to calculate the minimum time (in minutes) they will need to perform this task.
Help CFR-140, figure out the minimum time (in minutes) that all the astronauts will need to move from the 3-rd segment to the 1-st one. Since this number can be rather large, count it modulo *m*. | The first line contains two space-separated integers: *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=109) β the number of aliens on the saucer and the number, modulo which you should print the answer, correspondingly. | Print a single number β the answer to the problem modulo *m*. | [
"1 10\n",
"3 8\n"
] | [
"2\n",
"2\n"
] | In the first sample the only crew member moves from segment 3 to segment 2, and then from segment 2 to segment 1 without any problems. Thus, the whole moving will take two minutes.
To briefly describe the movements in the second sample we will use value <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4c7c8e716067e9c6251e8ca82a4ca7fde74fbacb.png" style="max-width: 100.0%;max-height: 100.0%;"/>, which would correspond to an alien with rank *i* moving from the segment in which it is at the moment, to the segment number *j*. Using these values, we will describe the movements between the segments in the second sample: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7668900e59f9bc3ccc78b0a77dca322fcb02fa04.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/1adb8798904e42944c35bd49feff02db6c3ea10b.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/954e2f634474269f53df1edbf2e7b214d8a2611c.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7668900e59f9bc3ccc78b0a77dca322fcb02fa04.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/be5f59dacfc6f7ded42f8f260c7b7aedf17ec1f0.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/d4fd9e68a9c6a277942eb188291d6d2744ea21d3.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7668900e59f9bc3ccc78b0a77dca322fcb02fa04.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/1adb8798904e42944c35bd49feff02db6c3ea10b.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/b73a9870e1b41a5e048c3ab3e3fd4b92c336c9ec.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7668900e59f9bc3ccc78b0a77dca322fcb02fa04.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/be5f59dacfc6f7ded42f8f260c7b7aedf17ec1f0.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/954e2f634474269f53df1edbf2e7b214d8a2611c.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7668900e59f9bc3ccc78b0a77dca322fcb02fa04.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/1adb8798904e42944c35bd49feff02db6c3ea10b.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/49b1ffd4dcd2e0da0acec04559e0c3efc7854b07.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7668900e59f9bc3ccc78b0a77dca322fcb02fa04.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/be5f59dacfc6f7ded42f8f260c7b7aedf17ec1f0.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/ef8b3f32ee76c86f57fa63f7251fa290642f17f8.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7668900e59f9bc3ccc78b0a77dca322fcb02fa04.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/1adb8798904e42944c35bd49feff02db6c3ea10b.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/954e2f634474269f53df1edbf2e7b214d8a2611c.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7668900e59f9bc3ccc78b0a77dca322fcb02fa04.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/be5f59dacfc6f7ded42f8f260c7b7aedf17ec1f0.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/d4fd9e68a9c6a277942eb188291d6d2744ea21d3.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7668900e59f9bc3ccc78b0a77dca322fcb02fa04.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/1adb8798904e42944c35bd49feff02db6c3ea10b.png" style="max-width: 100.0%;max-height: 100.0%;"/>; In total: the aliens need 26 moves. The remainder after dividing 26 by 8 equals 2, so the answer to this test is 2. | [
{
"input": "1 10",
"output": "2"
},
{
"input": "3 8",
"output": "2"
},
{
"input": "8 12",
"output": "8"
},
{
"input": "4 84",
"output": "80"
},
{
"input": "9 95",
"output": "17"
},
{
"input": "331358794 820674098",
"output": "2619146"
},
{
... | 124 | 0 | 0 | 4,570 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.