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'...
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 Mah...
[ { "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 col...
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 cl...
[]
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...
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 ...
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 ...
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 o...
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 fr...
[ { "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 g...
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...
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 th...
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 ...
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...
[ { "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 arbitr...
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 ...
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 ente...
[ { "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 clo...
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 maxi...
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 Ki...
[ "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...
[ { "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 complexit...
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* ...
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 visi...
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 apartment...
[ { "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 ...
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...
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 "Purificati...
[ "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 se...
[ { "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 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*)...
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 ...
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....
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. Y...
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) β€”...
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...
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 lexicographica...
[ { "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 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 pr...
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* ...
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...
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**...
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]$. Th...
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...
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. T...
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...
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 ma...
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 inte...
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 i...
[ { "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 ';'. ...
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 ...
[ "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 o...
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 subsequenc...
[ "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 re...
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 ...
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...
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 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 se...
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)...
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 t...
[ { "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". ...
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*,<=*...
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...
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 storm...
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...
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...
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. ...
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 volum...
[ { "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 mu...
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 *...
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 ...
[ { "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 ...
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 Wint...
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) ...
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 shou...
[ { "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<...
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*<=&lt;<=*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 roa...
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 wh...
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 th...
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 ea...
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*, c...
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 f...
[ "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 p...
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 pile...
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 eq...
[ "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 "()[]", "([...
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 ...
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 choo...
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...
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 nu...
[ "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 surpr...
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 intereste...
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 fr...
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 th...
[ { "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."...
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 "&lt;&gt;" to denote horizontal tiles and characters "^v" to denote vertical tiles. Refer to the sample test for the output format ...
[ "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", "&lt;&gt;**\n*^&lt;&gt;\n*v**\n&lt;&gt;&lt;&gt;\n", "*&lt;&gt;*\n&lt;&gt;&lt;&gt;\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...
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 tw...
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 th...
[ { "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 ...
The single line contains two space-separated integers: *n*,<=*k* (1<=≀<=*n*<=≀<=105,<=0<=≀<=*k*<=&lt;<=*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 e...
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 cho...
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 ar...
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*. T...
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...
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...
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*<=&lt;<=*x*<=≀<=*y*<=≀<=|*s*| and substrings *s*[*a*....
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**...
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],<=...,...
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 solutio...
[ "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...
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...
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*<=≀<=...
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 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 sav...
[ { "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, numbe...
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 ...
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...
[ "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 a...
[ { "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 s...
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 th...
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...
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, ...
[ "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 β€” c...
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 w...
[ "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 beau...
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 ...
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...
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...
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*<=&lt;<=*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*<=&lt;<=*n*). Vasya wants to add either a "+" or a "-" before...
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*...
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 betwe...
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 Robo...
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/119a8ba68772b2c2bf76f2acdc58027f6c5cde1...
[ { "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 exactl...
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* ...
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 word...
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...
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 i...
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 d...
[ { "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...
The only line contains four positive integers *cnt*1, *cnt*2, *x*, *y* (1<=≀<=*cnt*1,<=*cnt*2<=&lt;<=109; *cnt*1<=+<=*cnt*2<=≀<=109; 2<=≀<=*x*<=&lt;<=*y*<=≀<=3Β·104)Β β€” the numbers that are described in the statement. It is guaranteed that numbers *x*, *y* are prime.
Print a single integer β€” the answer to the problem.
[ "3 1 2 3\n", "1 3 2 3\n" ]
[ "5\n", "4\n" ]
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend. In the second sample you give the set of numbers {3} to the first...
[ { "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. Acco...
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 th...
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,...
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 bott...
[ "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 bot...
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 ...
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 ...
[ "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*<=&gt;<=1. No bank is a neighbour of itself. Vasya has an account in each bank. Its balance may be negative...
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...
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 ...
[ { "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 integer...
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 ...
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...
[ "3\npair pair int int int\n", "1\npair int\n" ]
[ "pair&lt;pair&lt;int,int&gt;,int&gt;", "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 station...
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*<=&lt;<=*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">...
[ { "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 ...
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 en...
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...
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 d...
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 que...
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 ...
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 (...
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, Teodo...
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 ...
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 ...
[ "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...
[ { "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 algor...
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 o...
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 wi...
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...
[ { "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 o...
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 gram...
[ { "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 onc...
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,...
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 secon...
[ { "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$ ...
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 wit...
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 ...
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*<=&lt;<=*c*), separated by single spaces β€” the original message. The third input line contains *m* integers *b**i* (0<=≀<=*b**i*<=&lt;<=*c*), separated by single...
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....
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 a...
[ { "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...
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.cod...
[ { "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