contest_id
int32 1
2.13k
| index
stringclasses 62
values | problem_id
stringlengths 2
6
| title
stringlengths 0
67
| rating
int32 0
3.5k
| tags
stringlengths 0
139
| statement
stringlengths 0
6.96k
| input_spec
stringlengths 0
2.32k
| output_spec
stringlengths 0
1.52k
| note
stringlengths 0
5.06k
| sample_tests
stringlengths 0
1.02k
| difficulty_category
stringclasses 6
values | tag_count
int8 0
11
| statement_length
int32 0
6.96k
| input_spec_length
int16 0
2.32k
| output_spec_length
int16 0
1.52k
| contest_year
int16 0
21
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1,737 |
B
|
1737B
|
B. Ela's Fitness and the Luxury Number
| 1,300 |
binary search; implementation; math
|
While working at DTL, Ela is very aware of her physical and mental health. She started to practice various sports, such as Archery, Yoga, and Football.Since she started engaging in sports activities, Ela switches to trying a new sport on days she considers being ""Luxury"" days. She counts the days since she started these activities, in which the day she starts is numbered as day \(1\). A ""Luxury"" day is the day in which the number of this day is a luxurious number. An integer \(x\) is called a luxurious number if it is divisible by \({\lfloor \sqrt{x} \rfloor}\).Here \(\lfloor r \rfloor\) denotes the ""floor"" of a real number \(r\). In other words, it's the largest integer not greater than \(r\).For example: \(8\), \(56\), \(100\) are luxurious numbers, since \(8\) is divisible by \(\lfloor \sqrt{8} \rfloor = \lfloor 2.8284 \rfloor = 2\), \(56\) is divisible \(\lfloor \sqrt{56} \rfloor = \lfloor 7.4833 \rfloor = 7\), and \(100\) is divisible by \(\lfloor \sqrt{100} \rfloor = \lfloor 10 \rfloor = 10\), respectively. On the other hand \(5\), \(40\) are not, since \(5\) are not divisible by \(\lfloor \sqrt{5} \rfloor = \lfloor 2.2361 \rfloor = 2\), and \(40\) are not divisible by \(\lfloor \sqrt{40} \rfloor = \lfloor 6.3246 \rfloor = 6\).Being a friend of Ela, you want to engage in these fitness activities with her to keep her and yourself accompanied (and have fun together, of course). Between day \(l\) and day \(r\), you want to know how many times she changes the activities.
|
Each test contains multiple test cases. The first line has the number of test cases \(t\) (\(1 \le t \le 10\ 000\)). The description of the test cases follows.The only line of each test case contains two integers \(l\) and \(r\) (\(1 \le l \le r \le 10^{18}\)) β the intervals at which you want to know how many times Ela changes her sports.
|
For each test case, output an integer that denotes the answer.
|
In the first test case, \(5\) luxury numbers in range \([8, 19]\) are: \(8, 9, 12, 15, 16\).
|
Input: 58 198 20119 1211 1000000000000000001234567891011 1000000000000000000 | Output: 5 6 2 948683296 2996666667
|
Easy
| 3 | 1,503 | 341 | 62 | 17 |
1,152 |
A
|
1152A
|
A. Neko Finds Grapes
| 800 |
greedy; implementation; math
|
On a random day, Neko found \(n\) treasure chests and \(m\) keys. The \(i\)-th chest has an integer \(a_i\) written on it and the \(j\)-th key has an integer \(b_j\) on it. Neko knows those chests contain the powerful mysterious green Grapes, thus Neko wants to open as many treasure chests as possible.The \(j\)-th key can be used to unlock the \(i\)-th chest if and only if the sum of the key number and the chest number is an odd number. Formally, \(a_i + b_j \equiv 1 \pmod{2}\). One key can be used to open at most one chest, and one chest can be opened at most once.Find the maximum number of chests Neko can open.
|
The first line contains integers \(n\) and \(m\) (\(1 \leq n, m \leq 10^5\)) β the number of chests and the number of keys.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^9\)) β the numbers written on the treasure chests.The third line contains \(m\) integers \(b_1, b_2, \ldots, b_m\) (\(1 \leq b_i \leq 10^9\)) β the numbers written on the keys.
|
Print the maximum number of chests you can open.
|
In the first example, one possible way to unlock \(3\) chests is as follows: Use first key to unlock the fifth chest, Use third key to unlock the second chest, Use fourth key to unlock the first chest. In the second example, you can use the only key to unlock any single chest (note that one key can't be used twice).In the third example, no key can unlock the given chest.
|
Input: 5 4 9 14 6 2 11 8 4 7 20 | Output: 3
|
Beginner
| 3 | 620 | 387 | 48 | 11 |
2,101 |
A
|
2101A
|
A. Mex in the Grid
| 1,300 |
constructive algorithms; implementation
|
You are given \(n^2\) cards with values from \(0\) to \(n^2-1\). You are to arrange them in a \(n\) by \(n\) grid such that there is exactly one card in each cell.The MEX (minimum excluded value) of a subgrid\(^{\text{β}}\) is defined as the smallest non-negative integer that does not appear in the subgrid.Your task is to arrange the cards such that the sum of MEX values over all \(\left(\frac{n(n+1)}{2}\right)^2\) subgrids is maximized.\(^{\text{β}}\)A subgrid of a \(n\) by \(n\) grid is specified by four numbers \(l_1, r_1, l_2, r_2\) satisfying \(1\le l_1\le r_1\le n\) and \(1\le l_2\le r_2\le n\). The element in the \(i\)-th row and the \(j\)-th column of the grid is part of the subgrid if and only if \(l_1\le i\le r_1\) and \(l_2\le j\le r_2\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(1\le n\le 500\)) β the side length of the grid.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(1000\).
|
For each test case, output \(n\) lines, each containing \(n\) integers representing the elements of the grid. If there are multiple answers, you can output any of them.
|
In the first test case, one valid arrangement is: 0123 There are \(9\) subgrids in total, and the \(4\) of them with non-zero MEX are shown below:0 values:\([0]\) β MEX: \(1\)01 values:\([0, 1]\) β MEX: \(2\)02 values:\([0, 2]\) β MEX: \(1\)0123 values:\([0, 1, 2, 3]\) β MEX: \(4\)The sum of MEX over all subgrids would be \(1+2+1+4 = 8\). It can be proven that no other arrangements have a larger sum of MEX values.
|
Input: 223 | Output: 0 1 2 3 8 4 5 6 0 1 7 2 3
|
Easy
| 2 | 759 | 360 | 168 | 21 |
755 |
F
|
755F
|
F. PolandBall and Gifts
| 2,600 |
bitmasks; dp; greedy
|
It's Christmas time! PolandBall and his friends will be giving themselves gifts. There are n Balls overall. Each Ball has someone for whom he should bring a present according to some permutation p, pi β i for all i.Unfortunately, Balls are quite clumsy. We know earlier that exactly k of them will forget to bring their gift. A Ball number i will get his present if the following two constraints will hold: Ball number i will bring the present he should give. Ball x such that px = i will bring his present. What is minimum and maximum possible number of kids who will not get their present if exactly k Balls will forget theirs?
|
The first line of input contains two integers n and k (2 β€ n β€ 106, 0 β€ k β€ n), representing the number of Balls and the number of Balls who will forget to bring their presents. The second line contains the permutation p of integers from 1 to n, where pi is the index of Ball who should get a gift from the i-th Ball. For all i, pi β i holds.
|
You should output two values β minimum and maximum possible number of Balls who will not get their presents, in that order.
|
In the first sample, if the third and the first balls will forget to bring their presents, they will be th only balls not getting a present. Thus the minimum answer is 2. However, if the first ans the second balls will forget to bring their presents, then only the fifth ball will get a present. So, the maximum answer is 4.
|
Input: 5 23 4 1 5 2 | Output: 2 4
|
Expert
| 3 | 629 | 342 | 123 | 7 |
1,603 |
C
|
1603C
|
C. Extreme Extension
| 2,300 |
dp; greedy; math; number theory
|
For an array \(b\) of \(n\) integers, the extreme value of this array is the minimum number of times (possibly, zero) the following operation has to be performed to make \(b\) non-decreasing: Select an index \(i\) such that \(1 \le i \le |b|\), where \(|b|\) is the current length of \(b\). Replace \(b_i\) with two elements \(x\) and \(y\) such that \(x\) and \(y\) both are positive integers and \(x + y = b_i\). This way, the array \(b\) changes and the next operation is performed on this modified array. For example, if \(b = [2, 4, 3]\) and index \(2\) gets selected, then the possible arrays after this operation are \([2, \underline{1}, \underline{3}, 3]\), \([2, \underline{2}, \underline{2}, 3]\), or \([2, \underline{3}, \underline{1}, 3]\). And consequently, for this array, this single operation is enough to make it non-decreasing: \([2, 4, 3] \rightarrow [2, \underline{2}, \underline{2}, 3]\).It's easy to see that every array of positive integers can be made non-decreasing this way.YouKn0wWho has an array \(a\) of \(n\) integers. Help him find the sum of extreme values of all nonempty subarrays of \(a\) modulo \(998\,244\,353\). If a subarray appears in \(a\) multiple times, its extreme value should be counted the number of times it appears.An array \(d\) is a subarray of an array \(c\) if \(d\) can be obtained from \(c\) by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10\,000\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^5\)).It is guaranteed that the sum of \(n\) over all test cases doesn't exceed \(10^5\).
|
For each test case, print a single integer β the sum of extreme values of all subarrays of \(a\) modulo \(998\,244\,353\).
|
Let \(f(l, r)\) denote the extreme value of \([a_l, a_{l+1}, \ldots, a_r]\).In the first test case, \(f(1, 3) = 3\), because YouKn0wWho can perform the following operations on the subarray \([5, 4, 3]\) (the newly inserted elements are underlined):\([5, 4, 3] \rightarrow [\underline{3}, \underline{2}, 4, 3] \rightarrow [3, 2, \underline{2}, \underline{2}, 3] \rightarrow [\underline{1}, \underline{2}, 2, 2, 2, 3]\); \(f(1, 2) = 1\), because \([5, 4] \rightarrow [\underline{2}, \underline{3}, 4]\); \(f(2, 3) = 1\), because \([4, 3] \rightarrow [\underline{1}, \underline{3}, 3]\); \(f(1, 1) = f(2, 2) = f(3, 3) = 0\), because they are already non-decreasing. So the total sum of extreme values of all subarrays of \(a = 3 + 1 + 1 + 0 + 0 + 0 = 5\).
|
Input: 4 3 5 4 3 4 3 2 1 4 1 69 8 7264 40515 28226 92776 35285 21709 75124 48163 | Output: 5 9 0 117
|
Expert
| 4 | 1,480 | 380 | 122 | 16 |
1,826 |
F
|
1826F
|
F. Fading into Fog
| 2,800 |
geometry; interactive; math; probabilities
|
This is an interactive problem.There are \(n\) distinct hidden points with real coordinates on a two-dimensional Euclidean plane. In one query, you can ask some line \(ax + by + c = 0\) and get the projections of all \(n\) points to this line in some order. The given projections are not exact, please read the interaction section for more clarity.Using the minimum number of queries, guess all \(n\) points and output them in some order. Here minimality means the minimum number of queries required to solve any possible test case with \(n\) points.The hidden points are fixed in advance and do not change throughout the interaction. In other words, the interactor is not adaptive.A projection of point \(A\) to line \(ax + by + c = 0\) is the point on the line closest to \(A\).
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 50\)) β the number of test cases.The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \leq n \leq 25\)) β the number of hidden points.For each test case, it is guaranteed that for any pair of hidden points, their \(x\) coordinates differ by at least \(1\). Analogously, \(y\) coordinates of any pair also differ by at least \(1\).Coordinates \(x\) and \(y\) of all hidden points do not exceed \(100\) by absolute value.
|
In the sample the hidden points are \((1, 3)\) and \((2.5, 0.5)\)A picture, which describes the first query:A picture, which describes the second query:
|
Input: 1 2 1 1 2.5 1 1.500000001 1.500000000 2 2 | Output: ? 0 1 -1 ? 0.2 -0.2 0 ! 1 3 2.5 0.500000001
|
Master
| 4 | 780 | 542 | 0 | 18 |
|
935 |
C
|
935C
|
C. Fifa and Fafa
| 1,600 |
geometry
|
Fifa and Fafa are sharing a flat. Fifa loves video games and wants to download a new soccer game. Unfortunately, Fafa heavily uses the internet which consumes the quota. Fifa can access the internet through his Wi-Fi access point. This access point can be accessed within a range of r meters (this range can be chosen by Fifa) from its position. Fifa must put the access point inside the flat which has a circular shape of radius R. Fifa wants to minimize the area that is not covered by the access point inside the flat without letting Fafa or anyone outside the flat to get access to the internet.The world is represented as an infinite 2D plane. The flat is centered at (x1, y1) and has radius R and Fafa's laptop is located at (x2, y2), not necessarily inside the flat. Find the position and the radius chosen by Fifa for his access point which minimizes the uncovered area.
|
The single line of the input contains 5 space-separated integers R, x1, y1, x2, y2 (1 β€ R β€ 105, |x1|, |y1|, |x2|, |y2| β€ 105).
|
Print three space-separated numbers xap, yap, r where (xap, yap) is the position which Fifa chose for the access point and r is the radius of its range. Your answer will be considered correct if the radius does not differ from optimal more than 10 - 6 absolutely or relatively, and also the radius you printed can be changed by no more than 10 - 6 (absolutely or relatively) in such a way that all points outside the flat and Fafa's laptop position are outside circle of the access point range.
|
Input: 5 3 3 1 1 | Output: 3.7677669529663684 3.7677669529663684 3.914213562373095
|
Medium
| 1 | 878 | 127 | 494 | 9 |
|
205 |
A
|
205A
|
A. Little Elephant and Rozdil
| 900 |
brute force; implementation
|
The Little Elephant loves Ukraine very much. Most of all he loves town Rozdol (ukr. ""Rozdil"").However, Rozdil is dangerous to settle, so the Little Elephant wants to go to some other town. The Little Elephant doesn't like to spend much time on travelling, so for his journey he will choose a town that needs minimum time to travel to. If there are multiple such cities, then the Little Elephant won't go anywhere.For each town except for Rozdil you know the time needed to travel to this town. Find the town the Little Elephant will go to or print ""Still Rozdil"", if he stays in Rozdil.
|
The first line contains a single integer n (1 β€ n β€ 105) β the number of cities. The next line contains n integers, separated by single spaces: the i-th integer represents the time needed to go from town Rozdil to the i-th town. The time values are positive integers, not exceeding 109.You can consider the cities numbered from 1 to n, inclusive. Rozdil is not among the numbered cities.
|
Print the answer on a single line β the number of the town the Little Elephant will go to. If there are multiple cities with minimum travel time, print ""Still Rozdil"" (without the quotes).
|
In the first sample there are only two cities where the Little Elephant can go. The travel time for the first town equals 7, to the second one β 4. The town which is closest to Rodzil (the only one) is the second one, so the answer is 2.In the second sample the closest cities are cities two and five, the travelling time to both of them equals 4, so the answer is ""Still Rozdil"".
|
Input: 27 4 | Output: 2
|
Beginner
| 2 | 590 | 387 | 190 | 2 |
1,327 |
C
|
1327C
|
C. Game with Chips
| 1,600 |
constructive algorithms; implementation
|
Petya has a rectangular Board of size \(n \times m\). Initially, \(k\) chips are placed on the board, \(i\)-th chip is located in the cell at the intersection of \(sx_i\)-th row and \(sy_i\)-th column.In one action, Petya can move all the chips to the left, right, down or up by \(1\) cell.If the chip was in the \((x, y)\) cell, then after the operation: left, its coordinates will be \((x, y - 1)\); right, its coordinates will be \((x, y + 1)\); down, its coordinates will be \((x + 1, y)\); up, its coordinates will be \((x - 1, y)\). If the chip is located by the wall of the board, and the action chosen by Petya moves it towards the wall, then the chip remains in its current position.Note that several chips can be located in the same cell.For each chip, Petya chose the position which it should visit. Note that it's not necessary for a chip to end up in this position.Since Petya does not have a lot of free time, he is ready to do no more than \(2nm\) actions.You have to find out what actions Petya should do so that each chip visits the position that Petya selected for it at least once. Or determine that it is not possible to do this in \(2nm\) actions.
|
The first line contains three integers \(n, m, k\) (\(1 \le n, m, k \le 200\)) β the number of rows and columns of the board and the number of chips, respectively.The next \(k\) lines contains two integers each \(sx_i, sy_i\) (\( 1 \le sx_i \le n, 1 \le sy_i \le m\)) β the starting position of the \(i\)-th chip.The next \(k\) lines contains two integers each \(fx_i, fy_i\) (\( 1 \le fx_i \le n, 1 \le fy_i \le m\)) β the position that the \(i\)-chip should visit at least once.
|
In the first line print the number of operations so that each chip visits the position that Petya selected for it at least once.In the second line output the sequence of operations. To indicate operations left, right, down, and up, use the characters \(L, R, D, U\) respectively.If the required sequence does not exist, print -1 in the single line.
|
Input: 3 3 2 1 2 2 1 3 3 3 2 | Output: 3 DRD
|
Medium
| 2 | 1,168 | 480 | 348 | 13 |
|
20 |
A
|
20A
|
A. BerOS file system
| 1,700 |
implementation
|
The new operating system BerOS has a nice feature. It is possible to use any number of characters '/' as a delimiter in path instead of one traditional '/'. For example, strings //usr///local//nginx/sbin// and /usr/local/nginx///sbin are equivalent. The character '/' (or some sequence of such characters) at the end of the path is required only in case of the path to the root directory, which can be represented as single character '/'.A path called normalized if it contains the smallest possible number of characters '/'.Your task is to transform a given path to the normalized form.
|
The first line of the input contains only lowercase Latin letters and character '/' β the path to some directory. All paths start with at least one character '/'. The length of the given line is no more than 100 characters, it is not empty.
|
The path in normalized form.
|
Input: //usr///local//nginx/sbin | Output: /usr/local/nginx/sbin
|
Medium
| 1 | 587 | 240 | 28 | 0 |
|
809 |
C
|
809C
|
C. Find a car
| 2,600 |
combinatorics; divide and conquer; dp
|
After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y. The upper left fragment 5 Γ 5 of the parking Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
|
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
|
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
|
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
|
Input: 41 1 1 1 13 2 5 4 51 1 5 5 100001 4 2 5 2 | Output: 113930
|
Expert
| 3 | 1,588 | 227 | 140 | 8 |
25 |
C
|
25C
|
C. Roads in Berland
| 1,900 |
graphs; shortest paths
|
There are n cities numbered from 1 to n in Berland. Some of them are connected by two-way roads. Each road has its own length β an integer number from 1 to 1000. It is known that from each city it is possible to get to any other city by existing roads. Also for each pair of cities it is known the shortest distance between them. Berland Government plans to build k new roads. For each of the planned road it is known its length, and what cities it will connect. To control the correctness of the construction of new roads, after the opening of another road Berland government wants to check the sum of the shortest distances between all pairs of cities. Help them β for a given matrix of shortest distances on the old roads and plans of all new roads, find out how the sum of the shortest distances between all pairs of cities changes after construction of each road.
|
The first line contains integer n (2 β€ n β€ 300) β amount of cities in Berland. Then there follow n lines with n integer numbers each β the matrix of shortest distances. j-th integer in the i-th row β di, j, the shortest distance between cities i and j. It is guaranteed that di, i = 0, di, j = dj, i, and a given matrix is a matrix of shortest distances for some set of two-way roads with integer lengths from 1 to 1000, such that from each city it is possible to get to any other city using these roads.Next line contains integer k (1 β€ k β€ 300) β amount of planned roads. Following k lines contain the description of the planned roads. Each road is described by three space-separated integers ai, bi, ci (1 β€ ai, bi β€ n, ai β bi, 1 β€ ci β€ 1000) β ai and bi β pair of cities, which the road connects, ci β the length of the road. It can be several roads between a pair of cities, but no road connects the city with itself.
|
Output k space-separated integers qi (1 β€ i β€ k). qi should be equal to the sum of shortest distances between all pairs of cities after the construction of roads with indexes from 1 to i. Roads are numbered from 1 in the input order. Each pair of cities should be taken into account in the sum exactly once, i. e. we count unordered pairs.
|
Input: 20 55 011 2 3 | Output: 3
|
Hard
| 2 | 868 | 923 | 339 | 0 |
|
1,400 |
B
|
1400B
|
B. RPG Protagonist
| 1,700 |
brute force; greedy; math
|
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.You decided to rob a town's blacksmith and you take a follower with you. You can carry at most \(p\) units and your follower β at most \(f\) units.In the blacksmith shop, you found \(cnt_s\) swords and \(cnt_w\) war axes. Each sword weights \(s\) units and each war axe β \(w\) units. You don't care what to take, since each of them will melt into one steel ingot.What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains two integers \(p\) and \(f\) (\(1 \le p, f \le 10^9\)) β yours and your follower's capacities.The second line of each test case contains two integers \(cnt_s\) and \(cnt_w\) (\(1 \le cnt_s, cnt_w \le 2 \cdot 10^5\)) β the number of swords and war axes in the shop.The third line of each test case contains two integers \(s\) and \(w\) (\(1 \le s, w \le 10^9\)) β the weights of each sword and each war axe.It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed \(2 \cdot 10^5\).
|
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
|
In the first test case: you should take \(3\) swords and \(3\) war axes: \(3 \cdot 5 + 3 \cdot 6 = 33 \le 33\) and your follower β \(3\) swords and \(2\) war axes: \(3 \cdot 5 + 2 \cdot 6 = 27 \le 27\). \(3 + 3 + 3 + 2 = 11\) weapons in total.In the second test case, you can take all available weapons even without your follower's help, since \(5 \cdot 10 + 5 \cdot 10 \le 100\).In the third test case, you can't take anything, but your follower can take \(3\) war axes: \(3 \cdot 5 \le 19\).
|
Input: 3 33 27 6 10 5 6 100 200 10 10 5 5 1 19 1 3 19 5 | Output: 11 20 3
|
Medium
| 3 | 657 | 674 | 115 | 14 |
1,121 |
A
|
1121A
|
A. Technogoblet of Fire
| 1,100 |
implementation; sortings
|
Everybody knows that the \(m\)-coder Tournament will happen soon. \(m\) schools participate in the tournament, and only one student from each school participates.There are a total of \(n\) students in those schools. Before the tournament, all students put their names and the names of their schools into the Technogoblet of Fire. After that, Technogoblet selects the strongest student from each school to participate. Arkady is a hacker who wants to have \(k\) Chosen Ones selected by the Technogoblet. Unfortunately, not all of them are the strongest in their schools, but Arkady can make up some new school names and replace some names from Technogoblet with those. You can't use each made-up name more than once. In that case, Technogoblet would select the strongest student in those made-up schools too.You know the power of each student and schools they study in. Calculate the minimal number of schools Arkady has to make up so that \(k\) Chosen Ones would be selected by the Technogoblet.
|
The first line contains three integers \(n\), \(m\) and \(k\) (\(1 \le n \le 100\), \(1 \le m, k \le n\)) β the total number of students, the number of schools and the number of the Chosen Ones.The second line contains \(n\) different integers \(p_1, p_2, \ldots, p_n\) (\(1 \le p_i \le n\)), where \(p_i\) denotes the power of \(i\)-th student. The bigger the power, the stronger the student.The third line contains \(n\) integers \(s_1, s_2, \ldots, s_n\) (\(1 \le s_i \le m\)), where \(s_i\) denotes the school the \(i\)-th student goes to. At least one student studies in each of the schools. The fourth line contains \(k\) different integers \(c_1, c_2, \ldots, c_k\) (\(1 \le c_i \le n\)) β the id's of the Chosen Ones.
|
Output a single integer β the minimal number of schools to be made up by Arkady so that \(k\) Chosen Ones would be selected by the Technogoblet.
|
In the first example there's just a single Chosen One with id \(3\). His power is equal to \(3\), but in the same school \(1\), there's a student with id \(5\) and power \(6\), and that means inaction would not lead to the latter being chosen. If we, however, make up a new school (let its id be \(4\)) for the Chosen One, Technogoblet would select students with ids \(2\) (strongest in \(3\)), \(5\) (strongest in \(1\)), \(6\) (strongest in \(2\)) and \(3\) (strongest in \(4\)).In the second example, you can change the school of student \(3\) to the made-up \(5\) and the school of student \(4\) to the made-up \(6\). It will cause the Technogoblet to choose students \(8\), \(7\), \(6\), \(5\), \(3\) and \(4\).
|
Input: 7 3 1 1 5 3 4 6 7 2 1 3 1 2 1 2 3 3 | Output: 1
|
Easy
| 2 | 995 | 725 | 144 | 11 |
461 |
C
|
461C
|
C. Appleman and a Sheet of Paper
| 2,200 |
data structures; implementation
|
Appleman has a very big sheet of paper. This sheet has a form of rectangle with dimensions 1 Γ n. Your task is help Appleman with folding of such a sheet. Actually, you need to perform q queries. Each query will have one of the following types: Fold the sheet of paper at position pi. After this query the leftmost part of the paper with dimensions 1 Γ pi must be above the rightmost part of the paper with dimensions 1 Γ ([current width of sheet] - pi). Count what is the total width of the paper pieces, if we will make two described later cuts and consider only the pieces between the cuts. We will make one cut at distance li from the left border of the current sheet of paper and the other at distance ri from the left border of the current sheet of paper. Please look at the explanation of the first test example for better understanding of the problem.
|
The first line contains two integers: n and q (1 β€ n β€ 105; 1 β€ q β€ 105) β the width of the paper and the number of queries.Each of the following q lines contains one of the described queries in the following format: ""1 pi"" (1 β€ pi < [current width of sheet]) β the first type query. ""2 li ri"" (0 β€ li < ri β€ [current width of sheet]) β the second type query.
|
For each query of the second type, output the answer.
|
The pictures below show the shapes of the paper during the queries of the first example: After the first fold operation the sheet has width equal to 4, after the second one the width of the sheet equals to 2.
|
Input: 7 41 31 22 0 12 1 2 | Output: 43
|
Hard
| 2 | 859 | 363 | 53 | 4 |
78 |
A
|
78A
|
A. Haiku
| 800 |
implementation; strings
|
Haiku is a genre of Japanese traditional poetry.A haiku poem consists of 17 syllables split into three phrases, containing 5, 7 and 5 syllables correspondingly (the first phrase should contain exactly 5 syllables, the second phrase should contain exactly 7 syllables, and the third phrase should contain exactly 5 syllables). A haiku masterpiece contains a description of a moment in those three phrases. Every word is important in a small poem, which is why haiku are rich with symbols. Each word has a special meaning, a special role. The main principle of haiku is to say much using a few words.To simplify the matter, in the given problem we will consider that the number of syllable in the phrase is equal to the number of vowel letters there. Only the following letters are regarded as vowel letters: ""a"", ""e"", ""i"", ""o"" and ""u"".Three phases from a certain poem are given. Determine whether it is haiku or not.
|
The input data consists of three lines. The length of each line is between 1 and 100, inclusive. The i-th line contains the i-th phrase of the poem. Each phrase consists of one or more words, which are separated by one or more spaces. A word is a non-empty sequence of lowercase Latin letters. Leading and/or trailing spaces in phrases are allowed. Every phrase has at least one non-space character. See the example for clarification.
|
Print ""YES"" (without the quotes) if the poem is a haiku. Otherwise, print ""NO"" (also without the quotes).
|
Input: on codeforces beta round is running a rustling of keys | Output: YES
|
Beginner
| 2 | 925 | 434 | 109 | 0 |
|
138 |
A
|
138A
|
A. Literature Lesson
| 1,600 |
implementation
|
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes.Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters ""a"", ""e"", ""i"", ""o"", ""u"" are considered vowels.Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit β ermit).Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): Clerihew (aabb); Alternating (abab); Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa).If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme.Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task.
|
The first line contains two integers n and k (1 β€ n β€ 2500, 1 β€ k β€ 5) β the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104.If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on.
|
Print the rhyme scheme of the poem as ""aabb"", ""abab"", ""abba"", ""aaaa""; or ""NO"" if the poem does not belong to any of the above mentioned schemes.
|
In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is ""NO"".
|
Input: 1 1daymaysunfun | Output: aabb
|
Medium
| 1 | 1,510 | 472 | 154 | 1 |
638 |
B
|
638B
|
B. Making Genome in Berland
| 1,500 |
*special; dfs and similar; strings
|
Berland scientists face a very important task - given the parts of short DNA fragments, restore the dinosaur DNA! The genome of a berland dinosaur has noting in common with the genome that we've used to: it can have 26 distinct nucleotide types, a nucleotide of each type can occur at most once. If we assign distinct English letters to all nucleotides, then the genome of a Berland dinosaur will represent a non-empty string consisting of small English letters, such that each letter occurs in it at most once.Scientists have n genome fragments that are represented as substrings (non-empty sequences of consecutive nucleotides) of the sought genome.You face the following problem: help scientists restore the dinosaur genome. It is guaranteed that the input is not contradictory and at least one suitable line always exists. When the scientists found out that you are a strong programmer, they asked you in addition to choose the one with the minimum length. If there are multiple such strings, choose any string.
|
The first line of the input contains a positive integer n (1 β€ n β€ 100) β the number of genome fragments.Each of the next lines contains one descriptions of a fragment. Each fragment is a non-empty string consisting of distinct small letters of the English alphabet. It is not guaranteed that the given fragments are distinct. Fragments could arbitrarily overlap and one fragment could be a substring of another one.It is guaranteed that there is such string of distinct letters that contains all the given fragments as substrings.
|
In the single line of the output print the genome of the minimum length that contains all the given parts. All the nucleotides in the genome must be distinct. If there are multiple suitable strings, print the string of the minimum length. If there also are multiple suitable strings, you can print any of them.
|
Input: 3bcdabcdef | Output: abcdef
|
Medium
| 3 | 1,015 | 531 | 310 | 6 |
|
1,408 |
I
|
1408I
|
I. Bitwise Magic
| 3,200 |
dp; math
|
You are given a positive integer \(k\) and an array \(a_1, a_2, \ldots, a_n\) of non-negative distinct integers not smaller than \(k\) and not greater than \(2^c-1\).In each of the next \(k\) seconds, one element is chosen randomly equiprobably out of all \(n\) elements and decreased by \(1\).For each integer \(x\), \(0 \leq x \leq 2^c - 1\), you need to find the probability that in the end the bitwise XOR of all elements of the array is equal to \(x\). Each of these values can be represented as an irreducible fraction \(\frac{p}{q}\), and you need to find the value of \(p \cdot q^{-1}\) modulo \(998\,244\,353\).
|
The first line of input contains three integers \(n, k, c\) (\(1 \leq n \leq (2^c - k)\), \(1 \leq k \leq 16\), \(1 \leq c \leq 16\)).The second line contains \(n\) distinct integers \(a_1, a_2, \ldots, a_n\) (\(k \leq a_i \leq 2^c-1\)).
|
Print \(2^c\) integers: the probability that the bitwise XOR is equal to \(x\) in the end for \(x\) in \(\{0, 1, \ldots, 2^c-1\}\) modulo \(998\,244\,353\).
|
Input: 4 1 3 1 2 3 4 | Output: 0 0 0 748683265 0 499122177 0 748683265
|
Master
| 2 | 620 | 237 | 156 | 14 |
|
858 |
A
|
858A
|
A. k-rounding
| 1,100 |
brute force; math; number theory
|
For a given positive integer n denote its k-rounding as the minimum positive integer x, such that x ends with k or more zeros in base 10 and is divisible by n.For example, 4-rounding of 375 is 375Β·80 = 30000. 30000 is the minimum integer such that it ends with 4 or more zeros and is divisible by 375.Write a program that will perform the k-rounding of n.
|
The only line contains two integers n and k (1 β€ n β€ 109, 0 β€ k β€ 8).
|
Print the k-rounding of n.
|
Input: 375 4 | Output: 30000
|
Easy
| 3 | 355 | 69 | 26 | 8 |
|
778 |
B
|
778B
|
B. Bitwise Formula
| 1,800 |
bitmasks; brute force; dfs and similar; expression parsing; implementation
|
Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied their properties and invented a new game.Initially, Bob chooses integer m, bit depth of the game, which means that all numbers in the game will consist of m bits. Then he asks Peter to choose some m-bit number. After that, Bob computes the values of n variables. Each variable is assigned either a constant m-bit number or result of bitwise operation. Operands of the operation may be either variables defined before, or the number, chosen by Peter. After that, Peter's score equals to the sum of all variable values.Bob wants to know, what number Peter needs to choose to get the minimum possible score, and what number he needs to choose to get the maximum possible score. In both cases, if there are several ways to get the same score, find the minimum number, which he can choose.
|
The first line contains two integers n and m, the number of variables and bit depth, respectively (1 β€ n β€ 5000; 1 β€ m β€ 1000). The following n lines contain descriptions of the variables. Each line describes exactly one variable. Description has the following format: name of a new variable, space, sign "":="", space, followed by one of: Binary number of exactly m bits. The first operand, space, bitwise operation (""AND"", ""OR"" or ""XOR""), space, the second operand. Each operand is either the name of variable defined before or symbol '?', indicating the number chosen by Peter. Variable names are strings consisting of lowercase Latin letters with length at most 10. All variable names are different.
|
In the first line output the minimum number that should be chosen by Peter, to make the sum of all variable values minimum possible, in the second line output the minimum number that should be chosen by Peter, to make the sum of all variable values maximum possible. Both numbers should be printed as m-bit binary numbers.
|
In the first sample if Peter chooses a number 0112, then a = 1012, b = 0112, c = 0002, the sum of their values is 8. If he chooses the number 1002, then a = 1012, b = 0112, c = 1112, the sum of their values is 15.For the second test, the minimum and maximum sum of variables a, bb, cx, d and e is 2, and this sum doesn't depend on the number chosen by Peter, so the minimum Peter can choose is 0.
|
Input: 3 3a := 101b := 011c := ? XOR b | Output: 011100
|
Medium
| 5 | 879 | 709 | 322 | 7 |
1,090 |
K
|
1090K
| 2,000 |
math
|
Hard
| 1 | 0 | 0 | 0 | 10 |
||||||
846 |
D
|
846D
|
D. Monitor
| 1,900 |
binary search; data structures
|
Recently Luba bought a monitor. Monitor is a rectangular matrix of size n Γ m. But then she started to notice that some pixels cease to work properly. Luba thinks that the monitor will become broken the first moment when it contains a square k Γ k consisting entirely of broken pixels. She knows that q pixels are already broken, and for each of them she knows the moment when it stopped working. Help Luba to determine when the monitor became broken (or tell that it's still not broken even after all q pixels stopped working).
|
The first line contains four integer numbers n, m, k, q (1 β€ n, m β€ 500, 1 β€ k β€ min(n, m), 0 β€ q β€ nΒ·m) β the length and width of the monitor, the size of a rectangle such that the monitor is broken if there is a broken rectangle with this size, and the number of broken pixels.Each of next q lines contain three integer numbers xi, yi, ti (1 β€ xi β€ n, 1 β€ yi β€ m, 0 β€ t β€ 109) β coordinates of i-th broken pixel (its row and column in matrix) and the moment it stopped working. Each pixel is listed at most once.We consider that pixel is already broken at moment ti.
|
Print one number β the minimum moment the monitor became broken, or ""-1"" if it's still not broken after these q pixels stopped working.
|
Input: 2 3 2 52 1 82 2 81 2 11 3 42 3 2 | Output: 8
|
Hard
| 2 | 528 | 568 | 137 | 8 |
|
1,774 |
F2
|
1774F2
|
F2. Magician and Pigs (Hard Version)
| 2,700 |
binary search; brute force; data structures; implementation
|
This is the hard version of the problem. The only difference between the two versions is the constraint on \(n\) and \(x\). You can make hacks only if both versions of the problem are solved.Little09 has been interested in magic for a long time, and it's so lucky that he meets a magician! The magician will perform \(n\) operations, each of them is one of the following three: \(1\ x\): Create a pig with \(x\) Health Points. \(2\ x\): Reduce the Health Point of all living pigs by \(x\). \(3\): Repeat all previous operations. Formally, assuming that this is the \(i\)-th operation in the operation sequence, perform the first \(i-1\) operations (including ""Repeat"" operations involved) in turn.A pig will die when its Health Point is less than or equal to \(0\). Little09 wants to know how many living pigs there are after all the operations. Please, print the answer modulo \(998\,244\,353\).
|
The first line contains a single integer \(n\) (\(1\leq n\leq 8\cdot 10^5\)) β the number of operations.Each of the following \(n\) lines contains an operation given in the form described in the problem statement. It's guaranteed that \(1\leq x\leq 10^9\) in operations of the first two types.
|
Print a single integer β the number of living pigs after all the operations, modulo \(998\,244\,353\).
|
In the first example, the operations are equivalent to repeating four times: create a pig with \(8\) Health Points and then reduce the Health Points of all living pigs by \(3\). It is easy to find that there are two living pigs in the end with \(2\) and \(5\) Health Points.
|
Input: 4 1 8 2 3 3 3 | Output: 2
|
Master
| 4 | 898 | 293 | 102 | 17 |
2,011 |
F
|
2011F
|
F. Good Subarray
| 0 |
*special; data structures; greedy
|
You are given an integer array \(a\) of size \(n\).Let's call an array good if it can be obtained using the following algorithm: create an array consisting of any single integer; and then perform the following operation an arbitrary number of times: choose an element from the already existing array (let's call it \(x\)) and add \(x\), \((x-1)\), or \((x+1)\) to the end of the array.For example, the arrays \([1, 2, 1]\), \([5]\) and \([3, 2, 1, 4]\) are good, while \([2, 4]\) and \([3, 1, 2]\) are not.Your task is to count the number of good contiguous subarrays of the array \(a\). Two subarrays that have the same elements but are in different positions of the array \(a\) are considered different.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 3 \cdot 10^5\)).The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\( 1 \le a_i \le n)\).Additional constraint on the input: the sum of \(n\) over all test cases does not exceed \(3 \cdot 10^5\).
|
For each test case, print a single integer β the number of good contiguous subarrays of the array \(a\).
|
In the first example, the following four subarrays are good: from the \(1\)-st to the \(1\)-st element; from the \(1\)-st to the \(2\)-nd element; from the \(2\)-nd to the \(2\)-nd element; from the \(3\)-rd to the \(3\)-rd element. In the second example, the only subarray which is not good is the subarray from the \(3\)-rd element to the \(4\)-th element.
|
Input: 431 1 343 2 3 11184 5 6 5 3 2 3 1 | Output: 4 9 1 23
|
Beginner
| 3 | 705 | 405 | 104 | 20 |
981 |
G
|
981G
|
G. Magic multisets
| 2,500 |
data structures
|
In the School of Magic in Dirtpolis a lot of interesting objects are studied on Computer Science lessons.Consider, for example, the magic multiset. If you try to add an integer to it that is already presented in the multiset, each element in the multiset duplicates. For example, if you try to add the integer \(2\) to the multiset \(\{1, 2, 3, 3\}\), you will get \(\{1, 1, 2, 2, 3, 3, 3, 3\}\). If you try to add an integer that is not presented in the multiset, it is simply added to it. For example, if you try to add the integer \(4\) to the multiset \(\{1, 2, 3, 3\}\), you will get \(\{1, 2, 3, 3, 4\}\).Also consider an array of \(n\) initially empty magic multisets, enumerated from \(1\) to \(n\).You are to answer \(q\) queries of the form ""add an integer \(x\) to all multisets with indices \(l, l + 1, \ldots, r\)"" and ""compute the sum of sizes of multisets with indices \(l, l + 1, \ldots, r\)"". The answers for the second type queries can be large, so print the answers modulo \(998244353\).
|
The first line contains two integers \(n\) and \(q\) (\(1 \leq n, q \leq 2 \cdot 10^{5}\)) β the number of magic multisets in the array and the number of queries, respectively.The next \(q\) lines describe queries, one per line. Each line starts with an integer \(t\) (\(1 \leq t \leq 2\)) β the type of the query. If \(t\) equals \(1\), it is followed by three integers \(l\), \(r\), \(x\) (\(1 \leq l \leq r \leq n\), \(1 \leq x \leq n\)) meaning that you should add \(x\) to all multisets with indices from \(l\) to \(r\) inclusive. If \(t\) equals \(2\), it is followed by two integers \(l\), \(r\) (\(1 \leq l \leq r \leq n\)) meaning that you should compute the sum of sizes of all multisets with indices from \(l\) to \(r\) inclusive.
|
For each query of the second type print the sum of sizes of multisets on the given segment.The answers can be large, so print them modulo \(998244353\).
|
In the first example after the first two queries the multisets are equal to \([\{1, 2\},\{1, 2\},\{\},\{\}]\), after the third query they are equal to \([\{1, 1, 2, 2\},\{1, 1, 2, 2\},\{1\},\{1\}]\).In the second example the first multiset evolves as follows: \(\{\} \to \{3\} \to \{3, 3\} \to \{2, 3, 3\} \to \{1, 2, 3, 3\} \to \{1, 1, 2, 2, 3, 3, 3, 3\}\).
|
Input: 4 41 1 2 11 1 2 21 1 4 12 1 4 | Output: 10
|
Expert
| 1 | 1,010 | 741 | 152 | 9 |
1,950 |
G
|
1950G
|
G. Shuffling Songs
| 1,900 |
bitmasks; dfs and similar; dp; graphs; hashing; implementation; strings
|
Vladislav has a playlist consisting of \(n\) songs, numbered from \(1\) to \(n\). Song \(i\) has genre \(g_i\) and writer \(w_i\). He wants to make a playlist in such a way that every pair of adjacent songs either have the same writer or are from the same genre (or both). He calls such a playlist exciting. Both \(g_i\) and \(w_i\) are strings of length no more than \(10^4\).It might not always be possible to make an exciting playlist using all the songs, so the shuffling process occurs in two steps. First, some amount (possibly zero) of the songs are removed, and then the remaining songs in the playlist are rearranged to make it exciting.Since Vladislav doesn't like when songs get removed from his playlist, he wants the making playlist to perform as few removals as possible. Help him find the minimum number of removals that need to be performed in order to be able to rearrange the rest of the songs to make the playlist exciting.
|
The first line of the input contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 16\)) β the number of songs in the original playlist.Then \(n\) lines follow, the \(i\)-th of which contains two strings of lowercase letters \(g_i\) and \(w_i\) (\(1 \leq |g_i|, |w_i| \leq 10^4\)) β the genre and the writer of the \(i\)-th song. Where \(|g_i|\) and \(|w_i|\) are lengths of the strings.The sum of \(2^n\) over all test cases does not exceed \(2^{16}\).The sum of \(|g_i| + |w_i|\) over all test cases does not exceed \(4 \cdot 10^5\).
|
For each test case, output a single integer β the minimum number of removals necessary so that the resulting playlist can be made exciting.
|
In the first test case, the playlist is already exciting.In the second test case, if you have the songs in the order \(4, 3, 1, 2\), it is exciting, so you don't need to remove any songs.In the third test case, you can remove songs \(4, 5, 6, 7\). Then the playlist with songs in the order \(1, 2, 3\) is exciting.
|
Input: 41pop taylorswift4electronic themotanselectronic carlasdreamspop themotanspop irinarimes7rap eminemrap drdrerap kanyewestpop taylorswiftindierock arcticmonkeysindierock arcticmonkeyspunkrock theoffspring4a bc de fg h | Output: 0 0 4 3
|
Hard
| 7 | 942 | 681 | 139 | 19 |
325 |
B
|
325B
|
B. Stadium and Games
| 1,800 |
binary search; math
|
Daniel is organizing a football tournament. He has come up with the following tournament format: In the first several (possibly zero) stages, while the number of teams is even, they split in pairs and play one game for each pair. At each stage the loser of each pair is eliminated (there are no draws). Such stages are held while the number of teams is even. Eventually there will be an odd number of teams remaining. If there is one team remaining, it will be declared the winner, and the tournament ends. Otherwise each of the remaining teams will play with each other remaining team once in round robin tournament (if there are x teams, there will be games), and the tournament ends. For example, if there were 20 teams initially, they would begin by playing 10 games. So, 10 teams would be eliminated, and the remaining 10 would play 5 games. Then the remaining 5 teams would play 10 games in a round robin tournament. In total there would be 10+5+10=25 games.Daniel has already booked the stadium for n games. Help him to determine how many teams he should invite so that the tournament needs exactly n games. You should print all possible numbers of teams that will yield exactly n games in ascending order, or -1 if there are no such numbers.
|
The first line contains a single integer n (1 β€ n β€ 1018), the number of games that should be played.Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
|
Print all possible numbers of invited teams in ascending order, one per line. If exactly n games cannot be played, output one number: -1.
|
Input: 3 | Output: 34
|
Medium
| 2 | 1,249 | 248 | 137 | 3 |
|
1,533 |
B
|
1533B
|
B. Nearest Point Function
| 0 |
*special; implementation
|
Your friend has created a nearest point function. For a given array of integer points \(x\) (sorted in ascending order, without any duplicates) and a point \(y\) it can find the nearest point from \(x\) to the point \(y\). In other words, it will find such a point \(x_i\) that \(|y - x_i|\) is the minimum possible, where \(|a|\) is the absolute value of \(a\).For example, if \(x = [1, 2, 5, 7, 9, 11]\), the answer for \(y=3\) will be \(2\), the answer for \(y=5\) will be \(5\) and the answer for \(y=100\) will be \(11\).This function is a bit buggy, though. If there are several nearest points to the given one, the function crashes. For example, if \(x = [1, 2, 5, 7, 9, 11]\) (as above) and \(y=6\), the function will crash, since points \(5\) and \(7\) are both the nearest points for \(6\).Your task is, for a given array of integer points \(x\) (sorted in ascending order, without any duplicates), determine if it is possible to cause the function to crash by choosing some integer point \(y\).You have to answer \(t\) independent test cases.
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of the test case contains one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the number of points in the array \(x\).The second line of the test case contains \(n\) integers \(x_1, x_2, \ldots, x_n\) (\(1 \le x_i \le 10^9\)), where \(x_i\) is the \(i\)-th point in the array \(x\).All points in the array \(x\) are distinct, and the array \(x\) is sorted in ascending order (in other words, \(x_1 < x_2 < \ldots < x_n\)).The sum of \(n\) over the test cases in the input does not exceed \(2 \cdot 10^5\) (\(\sum n \le 2 \cdot 10^5\)).
|
For each test case, print YES if it is possible to find some integer point \(y\) that will crash the function and NO otherwise.
|
In the first test case of the example, the function crashes if \(y = 2\) is chosen.In the fifth test case of the example, the function crashes if \(y = 500000000\) is chosen.In the sixth test case of the example, the function crashes if \(y = 10\) is chosen.
|
Input: 7 2 1 3 2 1 100 3 1 50 101 2 1 1000000000 2 1 999999999 6 1 2 5 7 9 11 6 1 2 5 8 9 12 | Output: YES NO NO NO YES YES NO
|
Beginner
| 2 | 1,053 | 654 | 127 | 15 |
57 |
D
|
57D
|
D. Journey
| 2,500 |
dp; math
|
Stewie the Rabbit explores a new parallel universe. This two dimensional universe has the shape of a rectangular grid, containing n lines and m columns. The universe is very small: one cell of the grid can only contain one particle. Each particle in this universe is either static or dynamic. Each static particle always remains in one and the same position. Due to unintelligible gravitation laws no two static particles in the parallel universe can be present in one column or row, and they also can't be present in the diagonally adjacent cells. A dynamic particle appears in a random empty cell, randomly chooses the destination cell (destination cell may coincide with the start cell, see the samples) and moves there along the shortest path through the cells, unoccupied by the static particles. All empty cells have the same probability of being selected as the beginning or end of the path. Having reached the destination cell, the particle disappears. Only one dynamic particle can exist at one moment of time. This particle can move from a cell to a cell if they have an adjacent side, and this transition takes exactly one galactic second. Stewie got interested in what is the average lifespan of one particle in the given universe.
|
The first line contains two space-separated integers: n, m (2 β€ n, m β€ 1000) which represent the sizes of the universe. The next n lines containing m symbols each describe the universe without dynamic particles β the j-th symbol of the i-th line equals to 'X' if the cell is occupied by a static particle, and to '.' if it is empty. It is guaranteed that the described universe satisfies the properties described above, that is no two static particles can be in one column or in one row, besides, they can't be positioned in the diagonally adjacent cells.
|
You have to print on a single line a single number which is the average life span of a particle with an accuracy of at least 6 decimal places.The answer will be accepted if it is within 10 - 6 of absolute or relative error from the correct answer.
|
Input: 2 2...X | Output: 0.888888888889
|
Expert
| 2 | 1,243 | 555 | 247 | 0 |
|
593 |
B
|
593B
|
B. Anton and Lines
| 1,600 |
geometry; sortings
|
The teacher gave Anton a large geometry homework, but he didn't do it (as usual) as he participated in a regular round on Codeforces. In the task he was given a set of n lines defined by the equations y = kiΒ·x + bi. It was necessary to determine whether there is at least one point of intersection of two of these lines, that lays strictly inside the strip between x1 < x2. In other words, is it true that there are 1 β€ i < j β€ n and x', y', such that: y' = ki * x' + bi, that is, point (x', y') belongs to the line number i; y' = kj * x' + bj, that is, point (x', y') belongs to the line number j; x1 < x' < x2, that is, point (x', y') lies inside the strip bounded by x1 < x2. You can't leave Anton in trouble, can you? Write a program that solves the given task.
|
The first line of the input contains an integer n (2 β€ n β€ 100 000) β the number of lines in the task given to Anton. The second line contains integers x1 and x2 ( - 1 000 000 β€ x1 < x2 β€ 1 000 000) defining the strip inside which you need to find a point of intersection of at least two lines.The following n lines contain integers ki, bi ( - 1 000 000 β€ ki, bi β€ 1 000 000) β the descriptions of the lines. It is guaranteed that all lines are pairwise distinct, that is, for any two i β j it is true that either ki β kj, or bi β bj.
|
Print ""Yes"" (without quotes), if there is at least one intersection of two distinct lines, located strictly inside the strip. Otherwise print ""No"" (without quotes).
|
In the first sample there are intersections located on the border of the strip, but there are no intersections located strictly inside it.
|
Input: 41 21 21 00 10 2 | Output: NO
|
Medium
| 2 | 765 | 534 | 168 | 5 |
449 |
C
|
449C
|
C. Jzzhu and Apples
| 2,500 |
constructive algorithms; number theory
|
Jzzhu has picked n apples from his big apple tree. All the apples are numbered from 1 to n. Now he wants to sell them to an apple store. Jzzhu will pack his apples into groups and then sell them. Each group must contain two apples, and the greatest common divisor of numbers of the apples in each group must be greater than 1. Of course, each apple can be part of at most one group.Jzzhu wonders how to get the maximum possible number of groups. Can you help him?
|
A single integer n (1 β€ n β€ 105), the number of the apples.
|
The first line must contain a single integer m, representing the maximum number of groups he can get. Each of the next m lines must contain two integers β the numbers of apples in the current group.If there are several optimal answers you can print any of them.
|
Input: 6 | Output: 26 32 4
|
Expert
| 2 | 463 | 59 | 261 | 4 |
|
1,149 |
B
|
1149B
|
B. Three Religions
| 2,200 |
dp; implementation; strings
|
During the archaeological research in the Middle East you found the traces of three ancient religions: First religion, Second religion and Third religion. You compiled the information on the evolution of each of these beliefs, and you now wonder if the followers of each religion could coexist in peace.The Word of Universe is a long word containing the lowercase English characters only. At each moment of time, each of the religion beliefs could be described by a word consisting of lowercase English characters.The three religions can coexist in peace if their descriptions form disjoint subsequences of the Word of Universe. More formally, one can paint some of the characters of the Word of Universe in three colors: \(1\), \(2\), \(3\), so that each character is painted in at most one color, and the description of the \(i\)-th religion can be constructed from the Word of Universe by removing all characters that aren't painted in color \(i\).The religions however evolve. In the beginning, each religion description is empty. Every once in a while, either a character is appended to the end of the description of a single religion, or the last character is dropped from the description. After each change, determine if the religions could coexist in peace.
|
The first line of the input contains two integers \(n, q\) (\(1 \leq n \leq 100\,000\), \(1 \leq q \leq 1000\)) β the length of the Word of Universe and the number of religion evolutions, respectively. The following line contains the Word of Universe β a string of length \(n\) consisting of lowercase English characters.Each of the following line describes a single evolution and is in one of the following formats: + \(i\) \(c\) (\(i \in \{1, 2, 3\}\), \(c \in \{\mathtt{a}, \mathtt{b}, \dots, \mathtt{z}\}\): append the character \(c\) to the end of \(i\)-th religion description. - \(i\) (\(i \in \{1, 2, 3\}\)) β remove the last character from the \(i\)-th religion description. You can assume that the pattern is non-empty. You can assume that no religion will have description longer than \(250\) characters.
|
Write \(q\) lines. The \(i\)-th of them should be YES if the religions could coexist in peace after the \(i\)-th evolution, or NO otherwise.You can print each character in any case (either upper or lower).
|
In the first example, after the 6th evolution the religion descriptions are: ad, bc, and ab. The following figure shows how these descriptions form three disjoint subsequences of the Word of Universe:
|
Input: 6 8 abdabc + 1 a + 1 d + 2 b + 2 c + 3 a + 3 b + 1 c - 2 | Output: YES YES YES YES YES YES NO YES
|
Hard
| 3 | 1,265 | 815 | 205 | 11 |
196 |
C
|
196C
|
C. Paint Tree
| 2,200 |
constructive algorithms; divide and conquer; geometry; sortings; trees
|
You are given a tree with n vertexes and n points on a plane, no three points lie on one straight line.Your task is to paint the given tree on a plane, using the given points as vertexes. That is, you should correspond each vertex of the tree to exactly one point and each point should correspond to a vertex. If two vertexes of the tree are connected by an edge, then the corresponding points should have a segment painted between them. The segments that correspond to non-adjacent edges, should not have common points. The segments that correspond to adjacent edges should have exactly one common point.
|
The first line contains an integer n (1 β€ n β€ 1500) β the number of vertexes on a tree (as well as the number of chosen points on the plane).Each of the next n - 1 lines contains two space-separated integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the numbers of tree vertexes connected by the i-th edge.Each of the next n lines contain two space-separated integers xi and yi ( - 109 β€ xi, yi β€ 109) β the coordinates of the i-th point on the plane. No three points lie on one straight line.It is guaranteed that under given constraints problem has a solution.
|
Print n distinct space-separated integers from 1 to n: the i-th number must equal the number of the vertex to place at the i-th point (the points are numbered in the order, in which they are listed in the input).If there are several solutions, print any of them.
|
The possible solutions for the sample are given below.
|
Input: 31 32 30 01 12 0 | Output: 1 3 2
|
Hard
| 5 | 605 | 558 | 262 | 1 |
30 |
B
|
30B
|
B. Codeforces World Finals
| 1,700 |
implementation
|
The king Copa often has been reported about the Codeforces site, which is rapidly getting more and more popular among the brightest minds of the humanity, who are using it for training and competing. Recently Copa understood that to conquer the world he needs to organize the world Codeforces tournament. He hopes that after it the brightest minds will become his subordinates, and the toughest part of conquering the world will be completed.The final round of the Codeforces World Finals 20YY is scheduled for DD.MM.YY, where DD is the day of the round, MM is the month and YY are the last two digits of the year. Bob is lucky to be the first finalist form Berland. But there is one problem: according to the rules of the competition, all participants must be at least 18 years old at the moment of the finals. Bob was born on BD.BM.BY. This date is recorded in his passport, the copy of which he has already mailed to the organizers. But Bob learned that in different countries the way, in which the dates are written, differs. For example, in the US the month is written first, then the day and finally the year. Bob wonders if it is possible to rearrange the numbers in his date of birth so that he will be at least 18 years old on the day DD.MM.YY. He can always tell that in his motherland dates are written differently. Help him.According to another strange rule, eligible participant must be born in the same century as the date of the finals. If the day of the finals is participant's 18-th birthday, he is allowed to participate. As we are considering only the years from 2001 to 2099 for the year of the finals, use the following rule: the year is leap if it's number is divisible by four.
|
The first line contains the date DD.MM.YY, the second line contains the date BD.BM.BY. It is guaranteed that both dates are correct, and YY and BY are always in [01;99].It could be that by passport Bob was born after the finals. In this case, he can still change the order of numbers in date.
|
If it is possible to rearrange the numbers in the date of birth so that Bob will be at least 18 years old on the DD.MM.YY, output YES. In the other case, output NO. Each number contains exactly two digits and stands for day, month or year in a date. Note that it is permitted to rearrange only numbers, not digits.
|
Input: 01.01.9801.01.80 | Output: YES
|
Medium
| 1 | 1,700 | 292 | 314 | 0 |
|
1,798 |
A
|
1798A
|
A. Showstopper
| 800 |
greedy; implementation; sortings
|
You are given two arrays \(a_1, a_2, \ldots, a_n\) and \(b_1, b_2, \ldots, b_n\).In one operation, you can choose any integer \(i\) from \(1\) to \(n\) and swap the numbers \(a_i\) and \(b_i\).Determine whether, after using any (possibly zero) number of operations, the following two conditions can be satisfied simultaneously: \(a_n = \max(a_1, a_2, \ldots, a_n)\), \(b_n = \max(b_1, b_2, \ldots, b_n)\). Here \(\max(c_1, c_2, \ldots, c_k)\) denotes the maximum number among \(c_1, c_2, \ldots, c_k\). For example, \(\max(3, 5, 4) = 5\), \(\max(1, 7, 7) = 7\), \(\max(6, 2) = 6\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 200\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 100\)) β the length of the arrays.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 100\)) β elements of the first array.The third line of each test case contains \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(1 \le b_i \le 100\)) β elements of the second array.
|
For each test case, print ""Yes"" if after using any (possibly zero) number of operations the conditions described above are satisfied. Otherwise, print ""No"".You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses.
|
In the first test case, you can swap the numbers \(a_3\) and \(b_3\), after which the array \(a\) becomes equal to \([7, 9, 9]\), and the array \(b\) becomes equal to \([7, 6, 7]\), and both conditions are met.In the second test case, it can be proved that it is impossible to satisfy both conditions.In the third test case, you can swap the numbers \(a_1\) and \(b_1\), after which the array \(a\) becomes equal to \([99, 99]\), and the array \(b\) becomes equal to \([100, 100]\), and both conditions are satisfied.In fifth test case, you can swap \(a_7\) and \(b_7\), \(a_8\) and \(b_8\), \(a_9\) and \(b_9\), after which the array \(a\) becomes equal to \([1, 2, 3, 4, 5, 6, 6, 6, 6]\), and the array \(b\) becomes equal to \([9, 9, 9, 9, 9, 9, 7, 8, 9]\), and both conditions are satisfied.
|
Input: 737 9 77 6 9410 10 15 1510 16 15 152100 9999 10011191 2 3 4 5 6 7 8 99 9 9 9 9 9 6 6 671 1 2 2 1 1 21 2 1 2 1 2 1230 45 30 | Output: Yes No Yes Yes Yes No No
|
Beginner
| 3 | 581 | 550 | 321 | 17 |
1,667 |
E
|
1667E
|
E. Centroid Probabilities
| 3,000 |
combinatorics; dp; fft; math
|
Consider every tree (connected undirected acyclic graph) with \(n\) vertices (\(n\) is odd, vertices numbered from \(1\) to \(n\)), and for each \(2 \le i \le n\) the \(i\)-th vertex is adjacent to exactly one vertex with a smaller index.For each \(i\) (\(1 \le i \le n\)) calculate the number of trees for which the \(i\)-th vertex will be the centroid. The answer can be huge, output it modulo \(998\,244\,353\).A vertex is called a centroid if its removal splits the tree into subtrees with at most \((n-1)/2\) vertices each.
|
The first line contains an odd integer \(n\) (\(3 \le n < 2 \cdot 10^5\), \(n\) is odd) β the number of the vertices in the tree.
|
Print \(n\) integers in a single line, the \(i\)-th integer is the answer for the \(i\)-th vertex (modulo \(998\,244\,353\)).
|
Example \(1\): there are two possible trees: with edges \((1-2)\), and \((1-3)\) β here the centroid is \(1\); and with edges \((1-2)\), and \((2-3)\) β here the centroid is \(2\). So the answer is \(1, 1, 0\).Example \(2\): there are \(24\) possible trees, for example with edges \((1-2)\), \((2-3)\), \((3-4)\), and \((4-5)\). Here the centroid is \(3\).
|
Input: 3 | Output: 1 1 0
|
Master
| 4 | 528 | 129 | 125 | 16 |
2,072 |
G
|
2072G
|
G. I've Been Flipping Numbers for 300 Years and Calculated the Sum
| 2,200 |
binary search; brute force; combinatorics; divide and conquer; math; number theory
|
After three hundred years of slime farming, Akito finally obtained the magical number \(n\). Upon reaching the merchant, he wanted to exchange the number for gold, but the merchant gave the hero a quest.The merchant said that for the quest, the skill \(\text{rev}(n, p)\) would be required, which Akito, by happy coincidence, had recently learned. \(\text{rev}(n, p)\) represents the following procedure: Write the number \(n\) in base \(p\), let this representation be \(n = \overline{n_{\ell - 1} \ldots n_1 n_0}\), where \(\ell\) is the length of the base \(p\) representation of the number \(n\). Reverse the base \(p\) representation, let this be \(m = \overline{n_0 n_1 \ldots n_{\ell - 1}}\). Convert the number \(m\) back to decimal and return it as the result. The merchant's quest was to calculate the sum \(x = \sum\limits_{p = 2}^{k} \text{rev}(n, p)\). Since this number can be quite large, only the remainder of \(x\) when divided by \(10^9 + 7\) is required. The merchant also mentioned that the previous traveler had been calculating this sum for three hundred years and had not finished it. But you will help Akito finish it faster, right?
|
The first line contains the number \(t\) (\(1 \le t \le 5000\)) β the number of test cases.In the only line of each test case, two numbers \(n\) and \(k\) are given (\(1 \le n \le 3 \cdot 10^5, 2 \le k \le 10^{18}\)) β the magical number and the upper limit for summation.Note that the sum of \(n\) across all test cases is not bounded.
|
For each test case, you need to output a single number β the remainder of \(x = \sum\limits_{p = 2}^{k} \text{rev}(n, p)\) when divided by \(10^9 + 7\).
|
In the third test case, \(n = 1\). The number one in any numeral system is represented by a single digit, which means \(\text{rev}(1, p) = 1\) for any \(p \ge 2\). Thus, \(x = \sum\limits_{p = 2}^{k} 1 = \sum\limits_{p = 2}^{10} 1 = 10 - 2 + 1 = 9\).In the fourth test case, \(x = \text{rev}(4, 2) + \text{rev}(4, 3) + \text{rev}(4, 4)\). Let's calculate each term: \(4 = 100_2 \rightarrow \text{rev}(4, 2) = 001_2 = 1\) \(4 = 11_3 \rightarrow \text{rev}(4, 3) = 11_3 = 4\) \(4 = 10_4 \rightarrow \text{rev}(4, 4) = 01_4 = 1\) Thus, \(x = 1 + 4 + 1 = 6\).In the seventh test case, \(x = \text{rev}(9, 2) + \text{rev}(9, 3)\). Let's calculate each term: \(9 = 1001_2 \rightarrow \text{rev}(9, 2) = 1001_2 = 9\) \(9 = 100_3 \rightarrow \text{rev}(9, 3) = 001_3 = 1\) Thus, \(x = 9 + 1 = 10\).
|
Input: 123 242 521 104 416 269 699 319 849982 44353100000 100000000717 30777 1000000000000000000 | Output: 3 7594 9 6 1 33471 10 2006 120792461 584502117 775 46058362
|
Hard
| 6 | 1,156 | 336 | 152 | 20 |
1,106 |
C
|
1106C
|
C. Lunar New Year and Number Division
| 900 |
greedy; implementation; math; sortings
|
Lunar New Year is approaching, and Bob is struggling with his homework β a number division problem.There are \(n\) positive integers \(a_1, a_2, \ldots, a_n\) on Bob's homework paper, where \(n\) is always an even number. Bob is asked to divide those numbers into groups, where each group must contain at least \(2\) numbers. Suppose the numbers are divided into \(m\) groups, and the sum of the numbers in the \(j\)-th group is \(s_j\). Bob's aim is to minimize the sum of the square of \(s_j\), that is $$$\(\sum_{j = 1}^{m} s_j^2.\)$$$Bob is puzzled by this hard problem. Could you please help him solve it?
|
The first line contains an even integer \(n\) (\(2 \leq n \leq 3 \cdot 10^5\)), denoting that there are \(n\) integers on Bob's homework paper.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^4\)), describing the numbers you need to deal with.
|
A single line containing one integer, denoting the minimum of the sum of the square of \(s_j\), which is $$$\(\sum_{i = j}^{m} s_j^2,\)\( where \)m$$$ is the number of groups.
|
In the first sample, one of the optimal solutions is to divide those \(4\) numbers into \(2\) groups \(\{2, 8\}, \{5, 3\}\). Thus the answer is \((2 + 8)^2 + (5 + 3)^2 = 164\).In the second sample, one of the optimal solutions is to divide those \(6\) numbers into \(3\) groups \(\{1, 2\}, \{1, 2\}, \{1, 2\}\). Thus the answer is \((1 + 2)^2 + (1 + 2)^2 + (1 + 2)^2 = 27\).
|
Input: 4 8 5 2 3 | Output: 164
|
Beginner
| 4 | 610 | 282 | 175 | 11 |
623 |
A
|
623A
|
A. Graph and String
| 1,800 |
constructive algorithms; graphs
|
One day student Vasya was sitting on a lecture and mentioned a string s1s2... sn, consisting of letters ""a"", ""b"" and ""c"" that was written on his desk. As the lecture was boring, Vasya decided to complete the picture by composing a graph G with the following properties: G has exactly n vertices, numbered from 1 to n. For all pairs of vertices i and j, where i β j, there is an edge connecting them if and only if characters si and sj are either equal or neighbouring in the alphabet. That is, letters in pairs ""a""-""b"" and ""b""-""c"" are neighbouring, while letters ""a""-""c"" are not. Vasya painted the resulting graph near the string and then erased the string. Next day Vasya's friend Petya came to a lecture and found some graph at his desk. He had heard of Vasya's adventure and now he wants to find out whether it could be the original graph G, painted by Vasya. In order to verify this, Petya needs to know whether there exists a string s, such that if Vasya used this s he would produce the given graph G.
|
The first line of the input contains two integers n and m β the number of vertices and edges in the graph found by Petya, respectively.Each of the next m lines contains two integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the edges of the graph G. It is guaranteed, that there are no multiple edges, that is any pair of vertexes appear in this list no more than once.
|
In the first line print ""Yes"" (without the quotes), if the string s Petya is interested in really exists and ""No"" (without the quotes) otherwise.If the string s exists, then print it on the second line of the output. The length of s must be exactly n, it must consist of only letters ""a"", ""b"" and ""c"" only, and the graph built using this string must coincide with G. If there are multiple possible answers, you may print any of them.
|
In the first sample you are given a graph made of two vertices with an edge between them. So, these vertices can correspond to both the same and adjacent letters. Any of the following strings ""aa"", ""ab"", ""ba"", ""bb"", ""bc"", ""cb"", ""cc"" meets the graph's conditions. In the second sample the first vertex is connected to all three other vertices, but these three vertices are not connected with each other. That means that they must correspond to distinct letters that are not adjacent, but that is impossible as there are only two such letters: a and c.
|
Input: 2 11 2 | Output: Yesaa
|
Medium
| 2 | 1,025 | 365 | 443 | 6 |
1,384 |
A
|
1384A
|
A. Common Prefixes
| 1,200 |
constructive algorithms; greedy; strings
|
The length of the longest common prefix of two strings \(s = s_1 s_2 \ldots s_n\) and \(t = t_1 t_2 \ldots t_m\) is defined as the maximum integer \(k\) (\(0 \le k \le min(n,m)\)) such that \(s_1 s_2 \ldots s_k\) equals \(t_1 t_2 \ldots t_k\).Koa the Koala initially has \(n+1\) strings \(s_1, s_2, \dots, s_{n+1}\).For each \(i\) (\(1 \le i \le n\)) she calculated \(a_i\) β the length of the longest common prefix of \(s_i\) and \(s_{i+1}\).Several days later Koa found these numbers, but she couldn't remember the strings.So Koa would like to find some strings \(s_1, s_2, \dots, s_{n+1}\) which would have generated numbers \(a_1, a_2, \dots, a_n\). Can you help her?If there are many answers print any. We can show that answer always exists for the given constraints.
|
Each test contains multiple test cases. The first line contains \(t\) (\(1 \le t \le 100\)) β the number of test cases. Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 100\)) β the number of elements in the list \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 50\)) β the elements of \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(100\).
|
For each test case:Output \(n+1\) lines. In the \(i\)-th line print string \(s_i\) (\(1 \le |s_i| \le 200\)), consisting of lowercase Latin letters. Length of the longest common prefix of strings \(s_i\) and \(s_{i+1}\) has to be equal to \(a_i\).If there are many answers print any. We can show that answer always exists for the given constraints.
|
In the \(1\)-st test case one of the possible answers is \(s = [aeren, ari, arousal, around, ari]\).Lengths of longest common prefixes are: Between \(\color{red}{a}eren\) and \(\color{red}{a}ri\) \(\rightarrow 1\) Between \(\color{red}{ar}i\) and \(\color{red}{ar}ousal\) \(\rightarrow 2\) Between \(\color{red}{arou}sal\) and \(\color{red}{arou}nd\) \(\rightarrow 4\) Between \(\color{red}{ar}ound\) and \(\color{red}{ar}i\) \(\rightarrow 2\)
|
Input: 4 4 1 2 4 2 2 5 3 3 1 3 1 3 0 0 0 | Output: aeren ari arousal around ari monogon monogamy monthly kevinvu kuroni kurioni korone anton loves adhoc problems
|
Easy
| 3 | 772 | 502 | 348 | 13 |
325 |
D
|
325D
|
D. Reclamation
| 2,900 |
dsu
|
In a far away land, there exists a planet shaped like a cylinder. There are three regions in this planet: top, bottom, and side as shown in the following picture. Both the top and the bottom areas consist of big cities. The side area consists entirely of the sea.One day, a city decides that it has too little space and would like to reclamate some of the side area into land. The side area can be represented by a grid with r rows and c columns β each cell represents a rectangular area in the side area. The rows are numbered 1 through r from top to bottom, while the columns are numbered 1 through c from left to right. Two cells are adjacent if they share a side. In addition, two cells located on the same row β one in the leftmost column, and the other in the rightmost column β are also adjacent.Initially, all of the cells are occupied by the sea. The plan is to turn some of those cells into land one by one in a particular order that will be given to you.However, the sea on the side area is also used as a major trade route. More formally, it is not allowed to reclamate the sea cells into land in such way that there does not exist a sequence of cells with the following property: All cells in the sequence are occupied by the sea (i.e., they are not reclamated). The first cell in the sequence is in the top row. The last cell in the sequence is in the bottom row. Consecutive cells in the sequence are adjacent. Thus, the plan is revised. Each time a cell is going to be turned from sea to land, the city first needs to check whether or not it would violate the above condition by doing that. If it would, then the cell is not turned into land and the plan proceeds into the next cell. Otherwise, the cell is turned into land.Your job is to simulate this and output the number of cells that were successfully turned into land.
|
The first line consists of three integers r, c, and n (1 β€ r, c β€ 3000, 1 β€ n β€ 3Β·105). Then, n lines follow, describing the cells in the order you will reclamate them. Each line will consists of two integers: ri and ci (1 β€ ri β€ r, 1 β€ ci β€ c), which represents the cell located at row ri and column ci. All of the lines describing the cells will be distinct.
|
You should output a single number representing the number of cells that were successfully turned to land.
|
The pictures below show the sequence of reclamations that are performed in the example input. Blue cells represent the cells occupied by sea, while other colored cells represent land. The latest cell that are reclamated is colored either yellow or red, depending on whether the addition violates the condition in the statement. The dashed red line represents a possible trade route, if it exists.No route exists, so this reclamation is not performed.No route exists, skipped.Remember that the leftmost and rightmost cells in the same row are adjacent.No route exists, skipped.Hence the result is:There are 6 successful reclamation and 3 failed ones.
|
Input: 3 4 92 23 22 33 43 11 32 11 11 4 | Output: 6
|
Master
| 1 | 1,840 | 360 | 105 | 3 |
802 |
G
|
802G
|
G. Fake News (easy)
| 800 |
implementation; strings
|
As it's the first of April, Heidi is suspecting that the news she reads today are fake, and she does not want to look silly in front of all the contestants. She knows that a newspiece is fake if it contains heidi as a subsequence. Help Heidi assess whether the given piece is true, but please be discreet about it...
|
The first and only line of input contains a single nonempty string s of length at most 1000 composed of lowercase letters (a-z).
|
Output YES if the string s contains heidi as a subsequence and NO otherwise.
|
A string s contains another string p as a subsequence if it is possible to delete some characters from s and obtain p.
|
Input: abcheaibcdi | Output: YES
|
Beginner
| 2 | 316 | 128 | 76 | 8 |
1,666 |
C
|
1666C
|
C. Connect the Points
| 1,800 |
brute force; constructive algorithms; geometry
|
You are given three points on a plane. You should choose some segments on the plane that are parallel to coordinate axes, so that all three points become connected. The total length of the chosen segments should be the minimal possible.Two points \(a\) and \(b\) are considered connected if there is a sequence of points \(p_0 = a, p_1, \ldots, p_k = b\) such that points \(p_i\) and \(p_{i+1}\) lie on the same segment.
|
The input consists of three lines describing three points. Each line contains two integers \(x\) and \(y\) separated by a space β the coordinates of the point (\(-10^9 \le x, y \le 10^9\)). The points are pairwise distinct.
|
On the first line output \(n\) β the number of segments, at most 100.The next \(n\) lines should contain descriptions of segments. Output four integers \(x_1\), \(y_1\), \(x_2\), \(y_2\) on a line β the coordinates of the endpoints of the corresponding segment (\(-10^9 \le x_1, y_1, x_2, y_2 \le 10^9\)). Each segment should be either horizontal or vertical.It is guaranteed that the solution with the given constraints exists.
|
The points and the segments from the example are shown below.
|
Input: 1 1 3 5 8 6 | Output: 3 1 1 1 5 1 5 8 5 8 5 8 6
|
Medium
| 3 | 420 | 223 | 428 | 16 |
618 |
F
|
618F
|
F. Double Knapsack
| 3,000 |
constructive algorithms; two pointers
|
You are given two multisets A and B. Each multiset has exactly n integers each between 1 and n inclusive. Multisets may contain multiple copies of the same number.You would like to find a nonempty subset of A and a nonempty subset of B such that the sum of elements in these subsets are equal. Subsets are also multisets, i.e. they can contain elements with equal values.If no solution exists, print - 1. Otherwise, print the indices of elements in any such subsets of A and B that have the same sum.
|
The first line of the input contains a single integer n (1 β€ n β€ 1 000 000) β the size of both multisets.The second line contains n integers, denoting the elements of A. Each element will be between 1 and n inclusive.The third line contains n integers, denoting the elements of B. Each element will be between 1 and n inclusive.
|
If there is no solution, print a single integer - 1. Otherwise, your solution should be printed on four lines.The first line should contain a single integer ka, the size of the corresponding subset of A. The second line should contain ka distinct integers, the indices of the subset of A.The third line should contain a single integer kb, the size of the corresponding subset of B. The fourth line should contain kb distinct integers, the indices of the subset of B.Elements in both sets are numbered from 1 to n. If there are multiple possible solutions, print any of them.
|
Input: 1010 10 10 10 10 10 10 10 10 1010 9 8 7 6 5 4 3 2 1 | Output: 1235 8 10
|
Master
| 2 | 500 | 328 | 574 | 6 |
|
760 |
A
|
760A
|
A. Petr and a calendar
| 800 |
implementation; math
|
Petr wants to make a calendar for current month. For this purpose he draws a table in which columns correspond to weeks (a week is seven consequent days from Monday to Sunday), rows correspond to weekdays, and cells contain dates. For example, a calendar for January 2017 should look like on the picture: Petr wants to know how many columns his table should have given the month and the weekday of the first date of that month? Assume that the year is non-leap.
|
The only line contain two integers m and d (1 β€ m β€ 12, 1 β€ d β€ 7) β the number of month (January is the first month, December is the twelfth) and the weekday of the first date of this month (1 is Monday, 7 is Sunday).
|
Print single integer: the number of columns the table should have.
|
The first example corresponds to the January 2017 shown on the picture in the statements.In the second example 1-st January is Monday, so the whole month fits into 5 columns.In the third example 1-st November is Saturday and 5 columns is enough.
|
Input: 1 7 | Output: 6
|
Beginner
| 2 | 461 | 218 | 66 | 7 |
141 |
C
|
141C
|
C. Queue
| 1,800 |
constructive algorithms; greedy; sortings
|
In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai β how many people who are taller than him/her and stand in queue in front of him.After a while the cashier has a lunch break and the people in the queue seat on the chairs in the waiting room in a random order.When the lunch break was over, it turned out that nobody can remember the exact order of the people in the queue, but everyone remembers his number ai.Your task is to restore the order in which the people stood in the queue if it is possible. There may be several acceptable orders, but you need to find any of them. Also, you need to print a possible set of numbers hi β the heights of people in the queue, so that the numbers ai are correct.
|
The first input line contains integer n β the number of people in the queue (1 β€ n β€ 3000). Then n lines contain descriptions of the people as ""namei ai"" (one description on one line), where namei is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the i-th person's name), ai is an integer (0 β€ ai β€ n - 1), that represents the number of people who are higher and stand in the queue in front of person i. It is guaranteed that all names are different.
|
If there's no acceptable order of the people in the queue, print the single line containing ""-1"" without the quotes. Otherwise, print in n lines the people as ""namei hi"", where hi is the integer from 1 to 109 (inclusive), the possible height of a man whose name is namei. Print the people in the order in which they stand in the queue, starting from the head of the queue and moving to its tail. Numbers hi are not necessarily unique.
|
Input: 4a 0b 2c 0d 0 | Output: a 150c 170d 180b 160
|
Medium
| 3 | 841 | 506 | 438 | 1 |
|
417 |
D
|
417D
|
D. Cunning Gena
| 1,900 |
bitmasks; dp; greedy; sortings
|
A boy named Gena really wants to get to the ""Russian Code Cup"" finals, or at least get a t-shirt. But the offered problems are too complex, so he made an arrangement with his n friends that they will solve the problems for him.The participants are offered m problems on the contest. For each friend, Gena knows what problems he can solve. But Gena's friends won't agree to help Gena for nothing: the i-th friend asks Gena xi rubles for his help in solving all the problems he can. Also, the friend agreed to write a code for Gena only if Gena's computer is connected to at least ki monitors, each monitor costs b rubles.Gena is careful with money, so he wants to spend as little money as possible to solve all the problems. Help Gena, tell him how to spend the smallest possible amount of money. Initially, there's no monitors connected to Gena's computer.
|
The first line contains three integers n, m and b (1 β€ n β€ 100; 1 β€ m β€ 20; 1 β€ b β€ 109) β the number of Gena's friends, the number of problems and the cost of a single monitor.The following 2n lines describe the friends. Lines number 2i and (2i + 1) contain the information about the i-th friend. The 2i-th line contains three integers xi, ki and mi (1 β€ xi β€ 109; 1 β€ ki β€ 109; 1 β€ mi β€ m) β the desired amount of money, monitors and the number of problems the friend can solve. The (2i + 1)-th line contains mi distinct positive integers β the numbers of problems that the i-th friend can solve. The problems are numbered from 1 to m.
|
Print the minimum amount of money Gena needs to spend to solve all the problems. Or print -1, if this cannot be achieved.
|
Input: 2 2 1100 1 12100 2 11 | Output: 202
|
Hard
| 4 | 858 | 637 | 121 | 4 |
|
2,014 |
B
|
2014B
|
B. Robin Hood and the Major Oak
| 800 |
math
|
In Sherwood, the trees are our shelter, and we are all children of the forest.The Major Oak in Sherwood is known for its majestic foliage, which provided shelter to Robin Hood and his band of merry men and women.The Major Oak grows \(i^i\) new leaves in the \(i\)-th year. It starts with \(1\) leaf in year \(1\).Leaves last for \(k\) years on the tree. In other words, leaves grown in year \(i\) last between years \(i\) and \(i+k-1\) inclusive.Robin considers even numbers lucky. Help Robin determine whether the Major Oak will have an even number of leaves in year \(n\).
|
The first line of the input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Each test case consists of two integers \(n\), \(k\) (\(1 \le n \le 10^9\), \(1 \le k \le n\)) β the requested year and the number of years during which the leaves remain.
|
For each test case, output one line, ""YES"" if in year \(n\) the Major Oak will have an even number of leaves and ""NO"" otherwise.You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses.
|
In the first test case, there is only \(1\) leaf.In the second test case, \(k=1\), so in the \(2\)-nd year there will be \(2^2=4\) leaves.In the third test case, \(k=2\), so in the \(2\)-nd year there will be \(1+2^2=5\) leaves.In the fourth test case, \(k=2\), so in the \(3\)-rd year there will be \(2^2 + 3^3 = 4 + 27 = 31\) leaves.
|
Input: 51 12 12 23 24 4 | Output: NO YES NO NO YES
|
Beginner
| 1 | 574 | 281 | 293 | 20 |
1,763 |
B
|
1763B
|
B. Incinerate
| 1,200 |
binary search; brute force; data structures; implementation; math; sortings
|
To destroy humanity, The Monster Association sent \(n\) monsters to Earth's surface. The \(i\)-th monster has health \(h_i\) and power \(p_i\).With his last resort attack, True Spiral Incineration Cannon, Genos can deal \(k\) damage to all monsters alive. In other words, Genos can reduce the health of all monsters by \(k\) (if \(k > 0\)) with a single attack. However, after every attack Genos makes, the monsters advance. With their combined efforts, they reduce Genos' attack damage by the power of the \(^\dagger\)weakest monster \(^\ddagger\)alive. In other words, the minimum \(p_i\) among all currently living monsters is subtracted from the value of \(k\) after each attack.\(^\dagger\)The Weakest monster is the one with the least power.\(^\ddagger\)A monster is alive if its health is strictly greater than \(0\).Will Genos be successful in killing all the monsters?
|
The first line of the input contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. The description of test cases follows.The first line of each test case contains two integers, \(n\) and \(k\) (\(1 \le n, k \le 10^5\)) β the number of monsters and Genos' initial attack damage. Then two lines follow, each containing \(n\) integers describing the arrays \(h\) and \(p\) (\(1 \le h_i, p_i \le 10^9\)).It's guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, print the answer β ""YES"" (without quotes) if Genos could kill all monsters and ""NO"" otherwise.
|
In the first example, after Genos' first attack, \(h\) and \(k\) will update to: \(h: [11,0,6,2,3,0]\) \(k: 7-1 = 6\) After second attack: \(h: [5,0,0,0,0,0]\) \(k: 6-2 = 4\) After third attack: \(h: [1,0,0,0,0,0]\) \(k: 4-2 = 2\) After fourth attack: \(h: [0,0,0,0,0,0]\) As Genos could kill all monsters, the answer is YES.
|
Input: 36 718 5 13 9 10 12 7 2 1 2 63 45 5 54 4 43 22 1 31 1 1 | Output: YES NO YES
|
Easy
| 6 | 877 | 518 | 118 | 17 |
1,795 |
E
|
1795E
|
E. Explosions?
| 2,200 |
binary search; data structures; dp; greedy; math
|
You are playing yet another game where you kill monsters using magic spells. There are \(n\) cells in the row, numbered from \(1\) to \(n\). Initially, the \(i\)-th cell contains the \(i\)-th monster with \(h_i\) health.You have a basic spell that costs \(1\) MP and deals \(1\) damage to the monster you choose. You can cast it any number of times. Also, you have a special scroll with ""Explosion"" spell you can use only once. You want to finish killing monsters with explosion, that's why you, firstly, cast the basic spell several times (possibly, zero), and then after that, you cast one ""Explosion"".How does ""Explosion"" spell work? Firstly, you choose the power of the spell: if you pour \(x\) MP into it, ""Explosion"" will deal \(x\) damage. Secondly, you choose some monster \(i\), which will be targeted by the spell. That's what happens next: if its current health \(h_i > x\), then he stays alive with health decreased by \(x\); if \(h_i \le x\), the \(i\)-th monster dies with an explosion that deals \(h_i - 1\) damage to monsters in the neighboring cells \(i - 1\) and \(i + 1\), if these cells exist and monsters inside are still alive; if the damage dealt by the explosion is enough to kill the monster \(i - 1\) (or \(i + 1\)), i. e. the current \(h_{i - 1} \le h_i - 1\) (or \(h_{i + 1} \le h_i - 1\)), then that monster also dies creating a secondary explosion of power \(h_{i-1} - 1\) (or \(h_{i+1} - 1\)) that may deals damage to their neighbors, and so on, until the explosions end. Your goal is to kill all the remaining monsters with those ""chaining"" explosions, that's why you need a basic spell to decrease \(h_i\) of some monsters or even kill them beforehand (monsters die when their current health \(h_i\) becomes less or equal to zero). Note that monsters don't move between cells, so, for example, monsters \(i\) and \(i + 2\) will never become neighbors.What is the minimum total MP you need to kill all monsters in the way you want? The total MP is counted as the sum of the number of basic spells you cast and the power \(x\) of explosion scroll you've chosen.
|
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains the single integer \(n\) (\(1 \le n \le 3 \cdot 10^5\)) β the number of cells in the row, i. e. the number of monsters.The second line of each test case contains \(n\) integers \(h_1, h_2, \dots, h_n\) (\(1 \le h_i \le 10^6\)) β the initial health of the monsters.It's guaranteed that the sum of \(n\) over all test cases doesn't exceed \(3 \cdot 10^5\).
|
For each test case, print one integer β the minimum total MP you need to kill all monsters by finishing them with explosion.
|
In the first test case, you can, for example, use basic spell on monsters \(1\) and \(2\) (once per monster) to kill them. After that, you cast ""Explosion"" of power \(x = 1\) on monster \(3\) to kill it. The total MP you need is \(2 + 1 = 3\).In the second test case, it's optimal to cast basic spell \(4\) times onto monster \(1\) to kill it. After that, you can cast ""Explosion"" of power \(x = 2\) onto monster \(3\). It dies, creating an explosion of power \(1\) that kills monsters \(2\) and \(4\). The total MP you need is \(4 + 2 = 6\).In the third test case, you cast ""Explosion"" of power \(15\) onto monster \(3\). Explosion of the \(3\)-rd monster (of power \(14\)) kills monsters \(2\) and \(4\). Secondary explosion of monster \(2\) (of power \(9\)) kills monster \(1\).
|
Input: 531 1 144 1 2 145 10 15 1014291 2 3 2 2 2 3 2 1 | Output: 3 6 15 42 12
|
Hard
| 5 | 2,102 | 488 | 124 | 17 |
1,152 |
F1
|
1152F1
|
F1. Neko Rules the Catniverse (Small Version)
| 2,800 |
bitmasks; dp; matrices
|
This problem is same as the next one, but has smaller constraints.Aki is playing a new video game. In the video game, he will control Neko, the giant cat, to fly between planets in the Catniverse.There are \(n\) planets in the Catniverse, numbered from \(1\) to \(n\). At the beginning of the game, Aki chooses the planet where Neko is initially located. Then Aki performs \(k - 1\) moves, where in each move Neko is moved from the current planet \(x\) to some other planet \(y\) such that: Planet \(y\) is not visited yet. \(1 \leq y \leq x + m\) (where \(m\) is a fixed constant given in the input) This way, Neko will visit exactly \(k\) different planets. Two ways of visiting planets are called different if there is some index \(i\) such that the \(i\)-th planet visited in the first way is different from the \(i\)-th planet visited in the second way.What is the total number of ways to visit \(k\) planets this way? Since the answer can be quite large, print it modulo \(10^9 + 7\).
|
The only line contains three integers \(n\), \(k\) and \(m\) (\(1 \le n \le 10^5\), \(1 \le k \le \min(n, 12)\), \(1 \le m \le 4\)) β the number of planets in the Catniverse, the number of planets Neko needs to visit and the said constant \(m\).
|
Print exactly one integer β the number of different ways Neko can visit exactly \(k\) planets. Since the answer can be quite large, print it modulo \(10^9 + 7\).
|
In the first example, there are \(4\) ways Neko can visit all the planets: \(1 \rightarrow 2 \rightarrow 3\) \(2 \rightarrow 3 \rightarrow 1\) \(3 \rightarrow 1 \rightarrow 2\) \(3 \rightarrow 2 \rightarrow 1\) In the second example, there are \(9\) ways Neko can visit exactly \(2\) planets: \(1 \rightarrow 2\) \(2 \rightarrow 1\) \(2 \rightarrow 3\) \(3 \rightarrow 1\) \(3 \rightarrow 2\) \(3 \rightarrow 4\) \(4 \rightarrow 1\) \(4 \rightarrow 2\) \(4 \rightarrow 3\) In the third example, with \(m = 4\), Neko can visit all the planets in any order, so there are \(5! = 120\) ways Neko can visit all the planets.In the fourth example, Neko only visit exactly \(1\) planet (which is also the planet he initially located), and there are \(100\) ways to choose the starting planet for Neko.
|
Input: 3 3 1 | Output: 4
|
Master
| 3 | 990 | 245 | 161 | 11 |
1,619 |
F
|
1619F
|
F. Let's Play the Hat?
| 2,000 |
brute force; constructive algorithms; greedy; math
|
The Hat is a game of speedy explanation/guessing words (similar to Alias). It's fun. Try it! In this problem, we are talking about a variant of the game when the players are sitting at the table and everyone plays individually (i.e. not teams, but individual gamers play).\(n\) people gathered in a room with \(m\) tables (\(n \ge 2m\)). They want to play the Hat \(k\) times. Thus, \(k\) games will be played at each table. Each player will play in \(k\) games.To do this, they are distributed among the tables for each game. During each game, one player plays at exactly one table. A player can play at different tables.Players want to have the most ""fair"" schedule of games. For this reason, they are looking for a schedule (table distribution for each game) such that: At any table in each game there are either \(\lfloor\frac{n}{m}\rfloor\) people or \(\lceil\frac{n}{m}\rceil\) people (that is, either \(n/m\) rounded down, or \(n/m\) rounded up). Different numbers of people can play different games at the same table. Let's calculate for each player the value \(b_i\) β the number of times the \(i\)-th player played at a table with \(\lceil\frac{n}{m}\rceil\) persons (\(n/m\) rounded up). Any two values of \(b_i\)must differ by no more than \(1\). In other words, for any two players \(i\) and \(j\), it must be true \(|b_i - b_j| \le 1\). For example, if \(n=5\), \(m=2\) and \(k=2\), then at the request of the first item either two players or three players should play at each table. Consider the following schedules: First game: \(1, 2, 3\) are played at the first table, and \(4, 5\) at the second one. The second game: at the first table they play \(5, 1\), and at the second β \(2, 3, 4\). This schedule is not ""fair"" since \(b_2=2\) (the second player played twice at a big table) and \(b_5=0\) (the fifth player did not play at a big table). First game: \(1, 2, 3\) are played at the first table, and \(4, 5\) at the second one. The second game: at the first table they play \(4, 5, 2\), and at the second one β \(1, 3\). This schedule is ""fair"": \(b=[1,2,1,1,1]\) (any two values of \(b_i\) differ by no more than \(1\)). Find any ""fair"" game schedule for \(n\) people if they play on the \(m\) tables of \(k\) games.
|
The first line of the input contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases in the test.Each test case consists of one line that contains three integers \(n\), \(m\) and \(k\) (\(2 \le n \le 2\cdot10^5\), \(1 \le m \le \lfloor\frac{n}{2}\rfloor\), \(1 \le k \le 10^5\)) β the number of people, tables and games, respectively.It is guaranteed that the sum of \(nk\) (\(n\) multiplied by \(k\)) over all test cases does not exceed \(2\cdot10^5\).
|
For each test case print a required schedule β a sequence of \(k\) blocks of \(m\) lines. Each block corresponds to one game, a line in a block corresponds to one table. In each line print the number of players at the table and the indices of the players (numbers from \(1\) to \(n\)) who should play at this table.If there are several required schedules, then output any of them. We can show that a valid solution always exists.You can output additional blank lines to separate responses to different sets of inputs.
|
Input: 3 5 2 2 8 3 1 2 1 3 | Output: 3 1 2 3 2 4 5 3 4 5 2 2 1 3 2 6 2 3 3 5 1 3 4 7 8 2 2 1 2 2 1 2 2 1
|
Hard
| 4 | 2,246 | 472 | 517 | 16 |
|
1,898 |
B
|
1898B
|
B. Milena and Admirer
| 1,500 |
greedy; math
|
Milena has received an array of integers \(a_1, a_2, \ldots, a_n\) of length \(n\) from a secret admirer. She thinks that making it non-decreasing should help her identify the secret admirer.She can use the following operation to make this array non-decreasing: Select an element \(a_i\) of array \(a\) and an integer \(x\) such that \(1 \le x < a_i\). Then, replace \(a_i\) by two elements \(x\) and \(a_i - x\) in array \(a\). New elements (\(x\) and \(a_i - x\)) are placed in the array \(a\) in this order instead of \(a_i\).More formally, let \(a_1, a_2, \ldots, a_i, \ldots, a_k\) be an array \(a\) before the operation. After the operation, it becomes equal to \(a_1, a_2, \ldots, a_{i-1}, x, a_i - x, a_{i+1}, \ldots, a_k\). Note that the length of \(a\) increases by \(1\) on each operation. Milena can perform this operation multiple times (possibly zero). She wants you to determine the minimum number of times she should perform this operation to make array \(a\) non-decreasing.An array \(x_1, x_2, \ldots, x_k\) of length \(k\) is called non-decreasing if \(x_i \le x_{i+1}\) for all \(1 \le i < k\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 10\,000\)). The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(1\leq n\leq 2\cdot 10^5\)) β the length of the array \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1\leq a_i\leq 10^9\)) β the array \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2\cdot 10^5\).
|
For each test case, output one integer β the minimum number of operations required to make the array non-decreasing.It can be shown that it is always possible to make the array \(a\) non-decreasing in the finite number of operations.
|
In the first test case, Milena can replace the second element of array \(a\) by integers \(1\) and \(2\), so the array would become \([\, 1, \, \underline{1}, \, \underline{2}, \, 2 \,]\). Only \(1\) operation is required.In the second test case, the array \(a\) is already non-decreasing, so the answer is \(0\).In the third test case, Milena can make array \(a\) non-decreasing in \(3\) operations as follows. Select \(i=1\) and \(x=2\) and replace \(a_1\) by \(2\) and \(1\). The array \(a\) becomes equal to \([\, \underline{2}, \, \underline{1}, \, 2, \, 1 \, ]\). Select \(i=3\) and \(x=1\) and replace \(a_3\) by \(1\) and \(1\). The array \(a\) becomes equal to \([\, 2, \, 1, \, \underline{1}, \, \underline{1}, \, 1 \,]\). Select \(i=1\) and \(x=1\) and replace \(a_1\) by \(2\) and \(1\). The array \(a\) becomes equal to \([\, \underline{1}, \, \underline{1}, \, 1, \, 1, \, 1, \, 1 \,]\). It can be shown that it is impossible to make it non-decreasing in \(2\) or less operations, so the answer is \(3\).
|
Input: 431 3 241 2 3 433 2 171 4 4 3 5 7 6 | Output: 1 0 3 9
|
Medium
| 2 | 1,114 | 507 | 233 | 18 |
1,370 |
A
|
1370A
|
A. Maximum GCD
| 800 |
greedy; implementation; math; number theory
|
Let's consider all integers in the range from \(1\) to \(n\) (inclusive).Among all pairs of distinct integers in this range, find the maximum possible greatest common divisor of integers in pair. Formally, find the maximum value of \(\mathrm{gcd}(a, b)\), where \(1 \leq a < b \leq n\).The greatest common divisor, \(\mathrm{gcd}(a, b)\), of two positive integers \(a\) and \(b\) is the biggest integer that is a divisor of both \(a\) and \(b\).
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 100\)) β the number of test cases. The description of the test cases follows.The only line of each test case contains a single integer \(n\) (\(2 \leq n \leq 10^6\)).
|
For each test case, output the maximum value of \(\mathrm{gcd}(a, b)\) among all \(1 \leq a < b \leq n\).
|
In the first test case, \(\mathrm{gcd}(1, 2) = \mathrm{gcd}(2, 3) = \mathrm{gcd}(1, 3) = 1\).In the second test case, \(2\) is the maximum possible value, corresponding to \(\mathrm{gcd}(2, 4)\).
|
Input: 2 3 5 | Output: 1 2
|
Beginner
| 4 | 445 | 230 | 105 | 13 |
1,331 |
F
|
1331F
|
F. Elementary!
| 0 |
*special; brute force; dp; strings
|
The input consists of a single string of uppercase letters A-Z. The length of the string is between 1 and 10 characters, inclusive.
|
Output ""YES"" or ""NO"".
|
Input: GENIUS | Output: YES
|
Beginner
| 4 | 0 | 131 | 25 | 13 |
||
1,329 |
D
|
1329D
|
D. Dreamoon Likes Strings
| 3,100 |
constructive algorithms; data structures
|
Dreamoon likes strings. Today he created a game about strings:String \(s_1, s_2, \ldots, s_n\) is beautiful if and only if for each \(1 \le i < n, s_i \ne s_{i+1}\).Initially, Dreamoon has a string \(a\). In each step Dreamoon can choose a beautiful substring of \(a\) and remove it. Then he should concatenate the remaining characters (in the same order).Dreamoon wants to use the smallest number of steps to make \(a\) empty. Please help Dreamoon, and print any sequence of the smallest number of steps to make \(a\) empty.
|
The first line contains an integer \(t\) (\(1 \leq t \leq 200\,000\)), denoting the number of test cases in the input.For each test case, there's one line with a non-empty string of lowercase Latin letters \(a\).The total sum of lengths of strings in all test cases is at most \(200\,000\).
|
For each test case, in the first line, you should print \(m\): the smallest number of steps to make \(a\) empty. Each of the following \(m\) lines should contain two integers \(l_i, r_i\) (\(1 \leq l_i \leq r_i \leq |a|\)), denoting, that the \(i\)-th step is removing the characters from index \(l_i\) to \(r_i\) in the current string. (indices are numbered starting from \(1\)). Note that after the deletion of the substring, indices of remaining characters may change, and \(r_i\) should be at most the current length of \(a\).If there are several possible solutions, you can print any.
|
Input: 4 aabbcc aaabbb aaa abacad | Output: 3 3 3 2 4 1 2 3 3 4 2 3 1 2 3 1 1 1 1 1 1 1 1 6
|
Master
| 2 | 525 | 290 | 589 | 13 |
|
1,989 |
A
|
1989A
|
A. Catch the Coin
| 800 |
implementation
|
Monocarp visited a retro arcade club with arcade cabinets. There got curious about the ""Catch the Coin"" cabinet.The game is pretty simple. The screen represents a coordinate grid such that: the X-axis is directed from left to right; the Y-axis is directed from bottom to top; the center of the screen has coordinates \((0, 0)\). At the beginning of the game, the character is located in the center, and \(n\) coins appear on the screen β the \(i\)-th coin is at coordinates \((x_i, y_i)\). The coordinates of all coins are different and not equal to \((0, 0)\).In one second, Monocarp can move the character in one of eight directions. If the character is at coordinates \((x, y)\), then it can end up at any of the coordinates \((x, y + 1)\), \((x + 1, y + 1)\), \((x + 1, y)\), \((x + 1, y - 1)\), \((x, y - 1)\), \((x - 1, y - 1)\), \((x - 1, y)\), \((x - 1, y + 1)\).If the character ends up at the coordinates with a coin, then Monocarp collects that coin.After Monocarp makes a move, all coins fall down by \(1\), that is, they move from \((x, y)\) to \((x, y - 1)\). You can assume that the game field is infinite in all directions.Monocarp wants to collect at least one coin, but cannot decide which coin to go for. Help him determine, for each coin, whether he can collect it.
|
The first line contains a single integer \(n\) (\(1 \le n \le 500\)) β the number of coins.In the \(i\)-th of the next \(n\) lines, two integers \(x_i\) and \(y_i\) (\(-50 \le x_i, y_i \le 50\)) are written β the coordinates of the \(i\)-th coin. The coordinates of all coins are different. No coin is located at \((0, 0)\).
|
For each coin, print ""YES"" if Monocarp can collect it. Otherwise, print ""NO"".
|
Pay attention to the second coin in the example. Monocarp can first move from \((0, 0)\) to \((-1, -1)\). Then the coin falls \(1\) down and ends up at \((-2, -2)\). Finally, Monocarp moves to \((-2, -2)\) and collects the coin.
|
Input: 524 42-2 -1-1 -20 -5015 0 | Output: YES YES NO NO YES
|
Beginner
| 1 | 1,287 | 324 | 81 | 19 |
1,581 |
A
|
1581A
|
A. CQXYM Count Permutations
| 800 |
combinatorics; math; number theory
|
CQXYM is counting permutations length of \(2n\).A permutation is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array) and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the array).A permutation \(p\)(length of \(2n\)) will be counted only if the number of \(i\) satisfying \(p_i<p_{i+1}\) is no less than \(n\). For example: Permutation \([1, 2, 3, 4]\) will count, because the number of such \(i\) that \(p_i<p_{i+1}\) equals \(3\) (\(i = 1\), \(i = 2\), \(i = 3\)). Permutation \([3, 2, 1, 4]\) won't count, because the number of such \(i\) that \(p_i<p_{i+1}\) equals \(1\) (\(i = 3\)). CQXYM wants you to help him to count the number of such permutations modulo \(1000000007\) (\(10^9+7\)).In addition, modulo operation is to get the remainder. For example: \(7 \mod 3=1\), because \(7 = 3 \cdot 2 + 1\), \(15 \mod 4=3\), because \(15 = 4 \cdot 3 + 3\).
|
The input consists of multiple test cases. The first line contains an integer \(t (t \geq 1)\) β the number of test cases. The description of the test cases follows.Only one line of each test case contains an integer \(n(1 \leq n \leq 10^5)\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\)
|
For each test case, print the answer in a single line.
|
\(n=1\), there is only one permutation that satisfies the condition: \([1,2].\)In permutation \([1,2]\), \(p_1<p_2\), and there is one \(i=1\) satisfy the condition. Since \(1 \geq n\), this permutation should be counted. In permutation \([2,1]\), \(p_1>p_2\). Because \(0<n\), this permutation should not be counted.\(n=2\), there are \(12\) permutations: \([1,2,3,4],[1,2,4,3],[1,3,2,4],[1,3,4,2],[1,4,2,3],[2,1,3,4],[2,3,1,4],[2,3,4,1],[2,4,1,3],[3,1,2,4],[3,4,1,2],[4,1,2,3].\)
|
Input: 4 1 2 9 91234 | Output: 1 12 830455698 890287984
|
Beginner
| 3 | 1,032 | 326 | 54 | 15 |
1,772 |
B
|
1772B
|
B. Matrix Rotation
| 800 |
brute force; implementation
|
You have a matrix \(2 \times 2\) filled with distinct integers. You want your matrix to become beautiful. The matrix is beautiful if the following two conditions are satisfied: in each row, the first element is smaller than the second element; in each column, the first element is smaller than the second element. You can perform the following operation on the matrix any number of times: rotate it clockwise by \(90\) degrees, so the top left element shifts to the top right cell, the top right element shifts to the bottom right cell, and so on: Determine if it is possible to make the matrix beautiful by applying zero or more operations.
|
The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.Each test case consists of two lines. Each of those lines contains two integers β the elements of the corresponding row of the matrix. In each matrix, all four elements are distinct integers from \(1\) to \(100\).
|
For each test case, print YES if the matrix can become beautiful, or NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
Input: 61 35 78 103 48 104 36 19 27 54 21 24 3 | Output: YES YES NO YES YES NO
|
Beginner
| 2 | 641 | 305 | 236 | 17 |
|
557 |
A
|
557A
|
A. Ilya and Diplomas
| 1,100 |
greedy; implementation; math
|
Soon a school Olympiad in Informatics will be held in Berland, n schoolchildren will participate there.At a meeting of the jury of the Olympiad it was decided that each of the n participants, depending on the results, will get a diploma of the first, second or third degree. Thus, each student will receive exactly one diploma.They also decided that there must be given at least min1 and at most max1 diplomas of the first degree, at least min2 and at most max2 diplomas of the second degree, and at least min3 and at most max3 diplomas of the third degree.After some discussion it was decided to choose from all the options of distributing diplomas satisfying these limitations the one that maximizes the number of participants who receive diplomas of the first degree. Of all these options they select the one which maximizes the number of the participants who receive diplomas of the second degree. If there are multiple of these options, they select the option that maximizes the number of diplomas of the third degree.Choosing the best option of distributing certificates was entrusted to Ilya, one of the best programmers of Berland. However, he found more important things to do, so it is your task now to choose the best option of distributing of diplomas, based on the described limitations.It is guaranteed that the described limitations are such that there is a way to choose such an option of distributing diplomas that all n participants of the Olympiad will receive a diploma of some degree.
|
The first line of the input contains a single integer n (3 β€ n β€ 3Β·106) β the number of schoolchildren who will participate in the Olympiad.The next line of the input contains two integers min1 and max1 (1 β€ min1 β€ max1 β€ 106) β the minimum and maximum limits on the number of diplomas of the first degree that can be distributed.The third line of the input contains two integers min2 and max2 (1 β€ min2 β€ max2 β€ 106) β the minimum and maximum limits on the number of diplomas of the second degree that can be distributed. The next line of the input contains two integers min3 and max3 (1 β€ min3 β€ max3 β€ 106) β the minimum and maximum limits on the number of diplomas of the third degree that can be distributed. It is guaranteed that min1 + min2 + min3 β€ n β€ max1 + max2 + max3.
|
In the first line of the output print three numbers, showing how many diplomas of the first, second and third degree will be given to students in the optimal variant of distributing diplomas.The optimal variant of distributing diplomas is the one that maximizes the number of students who receive diplomas of the first degree. Of all the suitable options, the best one is the one which maximizes the number of participants who receive diplomas of the second degree. If there are several of these options, the best one is the one that maximizes the number of diplomas of the third degree.
|
Input: 61 52 63 7 | Output: 1 2 3
|
Easy
| 3 | 1,505 | 780 | 587 | 5 |
|
1,556 |
A
|
1556A
|
A. A Variety of Operations
| 800 |
math
|
William has two numbers \(a\) and \(b\) initially both equal to zero. William mastered performing three different operations with them quickly. Before performing each operation some positive integer \(k\) is picked, which is then used to perform one of the following operations: (note, that for each operation you can choose a new positive integer \(k\)) add number \(k\) to both \(a\) and \(b\), or add number \(k\) to \(a\) and subtract \(k\) from \(b\), or add number \(k\) to \(b\) and subtract \(k\) from \(a\). Note that after performing operations, numbers \(a\) and \(b\) may become negative as well.William wants to find out the minimal number of operations he would have to perform to make \(a\) equal to his favorite number \(c\) and \(b\) equal to his second favorite number \(d\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). Description of the test cases follows.The only line of each test case contains two integers \(c\) and \(d\) \((0 \le c, d \le 10^9)\), which are William's favorite numbers and which he wants \(a\) and \(b\) to be transformed into.
|
For each test case output a single number, which is the minimal number of operations which William would have to perform to make \(a\) equal to \(c\) and \(b\) equal to \(d\), or \(-1\) if it is impossible to achieve this using the described operations.
|
Let us demonstrate one of the suboptimal ways of getting a pair \((3, 5)\): Using an operation of the first type with \(k=1\), the current pair would be equal to \((1, 1)\). Using an operation of the third type with \(k=8\), the current pair would be equal to \((-7, 9)\). Using an operation of the second type with \(k=7\), the current pair would be equal to \((0, 2)\). Using an operation of the first type with \(k=3\), the current pair would be equal to \((3, 5)\).
|
Input: 6 1 2 3 5 5 3 6 6 8 0 0 0 | Output: -1 2 2 1 2 0
|
Beginner
| 1 | 793 | 349 | 253 | 15 |
1,059 |
D
|
1059D
|
D. Nature Reserve
| 2,200 |
binary search; geometry; ternary search
|
There is a forest that we model as a plane and live \(n\) rare animals. Animal number \(i\) has its lair in the point \((x_{i}, y_{i})\). In order to protect them, a decision to build a nature reserve has been made.The reserve must have a form of a circle containing all lairs. There is also a straight river flowing through the forest. All animals drink from this river, therefore it must have at least one common point with the reserve. On the other hand, ships constantly sail along the river, so the reserve must not have more than one common point with the river.For convenience, scientists have made a transformation of coordinates so that the river is defined by \(y = 0\). Check whether it is possible to build a reserve, and if possible, find the minimum possible radius of such a reserve.
|
The first line contains one integer \(n\) (\(1 \le n \le 10^5\)) β the number of animals. Each of the next \(n\) lines contains two integers \(x_{i}\), \(y_{i}\) (\(-10^7 \le x_{i}, y_{i} \le 10^7\)) β the coordinates of the \(i\)-th animal's lair. It is guaranteed that \(y_{i} \neq 0\). No two lairs coincide.
|
If the reserve cannot be built, print \(-1\). Otherwise print the minimum radius. Your answer will be accepted if absolute or relative error does not exceed \(10^{-6}\).Formally, let your answer be \(a\), and the jury's answer be \(b\). Your answer is considered correct if \(\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}\).
|
In the first sample it is optimal to build the reserve with the radius equal to \(0.5\) and the center in \((0,\ 0.5)\).In the second sample it is impossible to build a reserve.In the third sample it is optimal to build the reserve with the radius equal to \(\frac{5}{8}\) and the center in \((\frac{1}{2},\ \frac{5}{8})\).
|
Input: 10 1 | Output: 0.5
|
Hard
| 3 | 798 | 311 | 321 | 10 |
987 |
A
|
987A
|
A. Infinity Gauntlet
| 800 |
implementation
|
You took a peek on Thanos wearing Infinity Gauntlet. In the Gauntlet there is a place for six Infinity Gems: the Power Gem of purple color, the Time Gem of green color, the Space Gem of blue color, the Soul Gem of orange color, the Reality Gem of red color, the Mind Gem of yellow color. Using colors of Gems you saw in the Gauntlet determine the names of absent Gems.
|
In the first line of input there is one integer \(n\) (\(0 \le n \le 6\)) β the number of Gems in Infinity Gauntlet.In next \(n\) lines there are colors of Gems you saw. Words used for colors are: purple, green, blue, orange, red, yellow. It is guaranteed that all the colors are distinct. All colors are given in lowercase English letters.
|
In the first line output one integer \(m\) (\(0 \le m \le 6\)) β the number of absent Gems.Then in \(m\) lines print the names of absent Gems, each on its own line. Words used for names are: Power, Time, Space, Soul, Reality, Mind. Names can be printed in any order. Keep the first letter uppercase, others lowercase.
|
In the first sample Thanos already has Reality, Power, Mind and Soul Gems, so he needs two more: Time and Space.In the second sample Thanos doesn't have any Gems, so he needs all six.
|
Input: 4redpurpleyelloworange | Output: 2SpaceTime
|
Beginner
| 1 | 368 | 340 | 317 | 9 |
1,639 |
K
|
1639K
|
K. Treasure Hunt
| 0 |
graphs; interactive
|
All problems in this contest share the same statement, the only difference is the test your solution runs on. For further information on scoring please refer to ""Scoring"" section of the statement.This is an interactive problem.Imagine you are a treasure hunter, a very skillful one. One day you came across an ancient map which could help you to become rich. The map shows multiple forestry roads, and at each junction there is a treasure. So, you start your journey hoping to retrieve all the hidden treasures, but you don't know yet that there is a wicked wizard standing against you and craving to tangle up these roads and impede your achievements.The treasure map is represented as an undirected graph in which vertices correspond to junctions and edges correspond to roads. Your path begins at a certain fixed vertex with a label known to you. Every time you come to a vertex that you have not been to before, you dig up a treasure chest and put a flag in this vertex. At the initial vertex you'll find a treasure chest immediately and, consequently, you'll put a flag there immediately as well.When you are standing at the junction you can see for each of the adjacent vertices its degree and if there is a flag there. There are no other things you can see from there. Besides, the power of the wicked wizard is so great that he is able to change the location of the roads and junctions on the map without changing the graph structure. Therefore, the sequence of the roads coming from the junction \(v\) might be different each time you come in the junction \(v\). However, keep in mind that the set of adjacent crossroads does not change, and you are well aware of previously dug treasures at each adjacent to \(v\) vertex.Your goal is to collect treasures from all vertices of the graph as fast as you can. Good luck in hunting!
|
Beginner
| 2 | 1,839 | 0 | 0 | 16 |
||||
1,109 |
B
|
1109B
|
B. Sasha and One More Name
| 1,800 |
constructive algorithms; hashing; strings
|
Reading books is one of Sasha's passions. Once while he was reading one book, he became acquainted with an unusual character. The character told about himself like that: ""Many are my names in many countries. Mithrandir among the Elves, TharkΓ»n to the Dwarves, OlΓ³rin I was in my youth in the West that is forgotten, in the South IncΓ‘nus, in the North Gandalf; to the East I go not.""And at that moment Sasha thought, how would that character be called in the East? In the East all names are palindromes. A string is a palindrome if it reads the same backward as forward. For example, such strings as ""kazak"", ""oo"" and ""r"" are palindromes, but strings ""abb"" and ""ij"" are not. Sasha believed that the hero would be named after one of the gods of the East. As long as there couldn't be two equal names, so in the East people did the following: they wrote the original name as a string on a piece of paper, then cut the paper minimum number of times \(k\), so they got \(k+1\) pieces of paper with substrings of the initial string, and then unite those pieces together to get a new string. Pieces couldn't be turned over, they could be shuffled.In this way, it's possible to achive a string abcdefg from the string f|de|abc|g using \(3\) cuts (by swapping papers with substrings f and abc). The string cbadefg can't be received using the same cuts.More formally, Sasha wants for the given palindrome \(s\) find such minimum \(k\), that you can cut this string into \(k + 1\) parts, and then unite them in such a way that the final string will be a palindrome and it won't be equal to the initial string \(s\). It there is no answer, then print ""Impossible"" (without quotes).
|
The first line contains one string \(s\) (\(1 \le |s| \le 5\,000\)) β the initial name, which consists only of lowercase Latin letters. It is guaranteed that \(s\) is a palindrome.
|
Print one integer \(k\) β the minimum number of cuts needed to get a new name, or ""Impossible"" (without quotes).
|
In the first example, you can cut the string in those positions: no|l|on, and then unite them as follows on|l|no. It can be shown that there is no solution with one cut.In the second example, you can cut the string right in the middle, and swap peaces, so you get toot.In the third example, you can't make a string, that won't be equal to the initial one.In the fourth example, you can cut the suffix nik and add it to the beginning, so you get nikkinnikkin.
|
Input: nolon | Output: 2
|
Medium
| 3 | 1,683 | 180 | 114 | 11 |
2,075 |
D
|
2075D
|
D. Equalization
| 2,000 |
bitmasks; brute force; dp; graphs; math
|
You are given two non-negative integers \(x\) and \(y\).You can perform the following operation any number of times (possibly zero): choose a positive integer \(k\) and divide either \(x\) or \(y\) by \(2^k\) rounding down. The cost of this operation is \(2^k\). However, there is an additional constraint: you cannot select the same value of \(k\) more than once.Your task is to calculate the minimum possible cost in order to make \(x\) equal to \(y\).
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^5\)) β the number of test cases.The only line of each test case contains two integers \(x\) and \(y\) (\(0 \le x, y \le 10^{17}\)).
|
For each test case, print a single integer β the minimum possible cost in order to make \(x\) equal to \(y\).
|
In the first example, you can proceed as follows: choose \(k=1\) and divide \(y\) by \(2\). After that, \(x\) and \(y\) are equal to \(0\).In the second example, you can proceed as follows: choose \(k=2\) and divide \(x\) by \(4\); choose \(k=1\) and divide \(y\) by \(2\). After that, \(x\) and \(y\) are equal to \(1\).In the third example, numbers already are equal.
|
Input: 50 16 23 313 374238659325782394 12983091057341925 | Output: 2 6 0 26 32764
|
Hard
| 5 | 454 | 196 | 109 | 20 |
1,948 |
E
|
1948E
|
E. Clique Partition
| 2,100 |
brute force; constructive algorithms; graphs; greedy; implementation
|
You are given two integers, \(n\) and \(k\). There is a graph on \(n\) vertices, numbered from \(1\) to \(n\), which initially has no edges.You have to assign each vertex an integer; let \(a_i\) be the integer on the vertex \(i\). All \(a_i\) should be distinct integers from \(1\) to \(n\).After assigning integers, for every pair of vertices \((i, j)\), you add an edge between them if \(|i - j| + |a_i - a_j| \le k\).Your goal is to create a graph which can be partitioned into the minimum possible (for the given values of \(n\) and \(k\)) number of cliques. Each vertex of the graph should belong to exactly one clique. Recall that a clique is a set of vertices such that every pair of vertices in it are connected with an edge.Since BledDest hasn't really brushed his programming skills up, he can't solve the problem ""given a graph, partition it into the minimum number of cliques"". So we also ask you to print the partition itself.
|
The first line contains one integer \(t\) (\(1 \le t \le 1600\)) β the number of test cases.Each test case consists of one line containing two integers \(n\) and \(k\) (\(2 \le n \le 40\); \(1 \le k \le 2n\)).
|
For each test case, print three lines: the first line should contain \(n\) distinct integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le n\)) β the values you assign to the vertices; the second line should contain one integer \(q\) (\(1 \le q \le n\)) β the number of cliques you partition the graph into; the third line should contain \(n\) integers \(c_1, c_2, \dots, c_n\) (\(1 \le c_i \le q\)) β the partition of the graph into cliques. Where two vertices \(u\) and \(v\) are in the same clique if \(c_u = c_v\). If there are multiple answers, print any of them.
|
Input: 32 35 48 16 | Output: 2 1 1 1 1 3 1 5 2 4 2 1 1 2 1 2 1 2 3 4 5 6 7 8 1 1 1 1 1 1 1 1 1
|
Hard
| 5 | 941 | 209 | 564 | 19 |
|
1,083 |
F
|
1083F
|
F. The Fair Nut and Amusing Xor
| 3,300 |
data structures
|
The Fair Nut has two arrays \(a\) and \(b\), consisting of \(n\) numbers. He found them so long ago that no one knows when they came to him.The Fair Nut often changes numbers in his arrays. He also is interested in how similar \(a\) and \(b\) are after every modification.Let's denote similarity of two arrays as the minimum number of operations to apply to make arrays equal (every operation can be applied for both arrays). If it is impossible, similarity will be equal \(-1\).Per one operation you can choose a subarray with length \(k\) (\(k\) is fixed), and change every element \(a_i\), which belongs to the chosen subarray, to \(a_i \oplus x\) (\(x\) can be chosen), where \(\oplus\) denotes the bitwise XOR operation. Nut has already calculated the similarity of the arrays after every modification. Can you do it?Note that you just need to calculate those values, that is you do not need to apply any operations.
|
The first line contains three numbers \(n\), \(k\) and \(q\) (\(1 \le k \le n \le 2 \cdot 10^5\), \(0 \le q \le 2 \cdot 10^5\)) β the length of the arrays, the length of the subarrays, to which the operations are applied, and the number of queries.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i < 2^{14}\)) β elements of array \(a\).The third line contains \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(0 \le b_i < 2^{14}\)) β elements of array \(b\).Each of the next \(q\) lines describes query and contains string \(s\) and two integers \(p\) and \(v\) (\(1 \le p \le n\), \(0 \le v < 2^{14}\)) β array, which this query changes (Β«aΒ» or Β«bΒ» without quotes), index of changing element and its new value.
|
On the first line print initial similarity of arrays \(a\) and \(b\).On the \(i\)-th of following \(q\) lines print similarity of \(a\) and \(b\) after applying first \(i\) modifications.
|
In the first sample making arrays \([0, 4, 2]\) and \([1, 2, 3]\) is impossible with \(k=3\). After the modification, you can apply the operation with \(x=1\) to the whole first array (its length is equal to \(k\)), and it will be equal to the second array.In order to make arrays equal in the second sample before changes, you can apply operations with \(x=1\) on subarray \([1, 2]\) of \(a\) and with \(x=2\) on subarray \([2, 3]\) of \(b\).After all queries arrays will be equal \([0, 3, 2]\) and \([1, 0, 0]\). The same operations make them equal \([1, 2, 2]\).
|
Input: 3 3 10 4 21 2 3b 2 5 | Output: -11
|
Master
| 1 | 921 | 734 | 187 | 10 |
215 |
C
|
215C
|
C. Crosses
| 2,100 |
brute force; implementation
|
There is a board with a grid consisting of n rows and m columns, the rows are numbered from 1 from top to bottom and the columns are numbered from 1 from left to right. In this grid we will denote the cell that lies on row number i and column number j as (i, j).A group of six numbers (a, b, c, d, x0, y0), where 0 β€ a, b, c, d, is a cross, and there is a set of cells that are assigned to it. Cell (x, y) belongs to this set if at least one of two conditions are fulfilled: |x0 - x| β€ a and |y0 - y| β€ b |x0 - x| β€ c and |y0 - y| β€ d The picture shows the cross (0, 1, 1, 0, 2, 3) on the grid 3 Γ 4. Your task is to find the number of different groups of six numbers, (a, b, c, d, x0, y0) that determine the crosses of an area equal to s, which are placed entirely on the grid. The cross is placed entirely on the grid, if any of its cells is in the range of the grid (that is for each cell (x, y) of the cross 1 β€ x β€ n; 1 β€ y β€ m holds). The area of the cross is the number of cells it has.Note that two crosses are considered distinct if the ordered groups of six numbers that denote them are distinct, even if these crosses coincide as sets of points.
|
The input consists of a single line containing three integers n, m and s (1 β€ n, m β€ 500, 1 β€ s β€ nΒ·m). The integers are separated by a space.
|
Print a single integer β the number of distinct groups of six integers that denote crosses with area s and that are fully placed on the n Γ m grid.Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
|
In the first sample the sought groups of six numbers are: (0, 0, 0, 0, 1, 1), (0, 0, 0, 0, 1, 2), (0, 0, 0, 0, 2, 1), (0, 0, 0, 0, 2, 2).In the second sample the sought groups of six numbers are: (0, 1, 1, 0, 2, 2), (0, 1, 1, 0, 2, 3), (1, 0, 0, 1, 2, 2), (1, 0, 0, 1, 2, 3).
|
Input: 2 2 1 | Output: 4
|
Hard
| 2 | 1,156 | 142 | 294 | 2 |
1,814 |
F
|
1814F
|
F. Communication Towers
| 2,700 |
brute force; divide and conquer; dsu
|
There are \(n\) communication towers, numbered from \(1\) to \(n\), and \(m\) bidirectional wires between them. Each tower has a certain set of frequencies that it accepts, the \(i\)-th of them accepts frequencies from \(l_i\) to \(r_i\).Let's say that a tower \(b\) is accessible from a tower \(a\), if there exists a frequency \(x\) and a sequence of towers \(a=v_1, v_2, \dots, v_k=b\), where consecutive towers in the sequence are directly connected by a wire, and each of them accepts frequency \(x\). Note that accessibility is not transitive, i. e if \(b\) is accessible from \(a\) and \(c\) is accessible from \(b\), then \(c\) may not be accessible from \(a\).Your task is to determine the towers that are accessible from the \(1\)-st tower.
|
The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 2 \cdot 10^5\); \(0 \le m \le 4 \cdot 10^5\)) β the number of communication towers and the number of wires, respectively.Then \(n\) lines follows, the \(i\)-th of them contains two integers \(l_i\) and \(r_i\) (\(1 \le l_i \le r_i \le 2 \cdot 10^5\)) β the boundaries of the acceptable frequencies for the \(i\)-th tower.Then \(m\) lines follows, the \(i\)-th of them contains two integers \(v_i\) and \(u_i\) (\(1 \le v_i, u_i \le n\); \(v_i \ne u_i\)) β the \(i\)-th wire that connects towers \(v_i\) and \(u_i\). There are no two wires connecting the same pair of towers.
|
In a single line, print distinct integers from \(1\) to \(n\) in ascending order β the indices of the communication towers that are accessible from the \(1\)-st tower.
|
Input: 6 5 3 5 1 2 2 4 2 3 3 3 4 6 1 3 6 1 3 5 3 6 2 3 | Output: 1 3 5 6
|
Master
| 3 | 750 | 642 | 167 | 18 |
|
626 |
G
|
626G
|
G. Raffles
| 3,100 |
data structures; dp; greedy; math
|
Johnny is at a carnival which has n raffles. Raffle i has a prize with value pi. Each participant can put tickets in whichever raffles they choose (they may have more than one ticket in a single raffle). At the end of the carnival, one ticket is selected at random from each raffle, and the owner of the ticket wins the associated prize. A single person can win multiple prizes from different raffles. However, county rules prevent any one participant from owning more than half the tickets in a single raffle, i.e. putting more tickets in the raffle than all the other participants combined. To help combat this (and possibly win some prizes), the organizers started by placing a single ticket in each raffle, which they will never remove.Johnny bought t tickets and is wondering where to place them. Currently, there are a total of li tickets in the i-th raffle. He watches as other participants place tickets and modify their decisions and, at every moment in time, wants to know how much he can possibly earn. Find the maximum possible expected value of Johnny's winnings at each moment if he distributes his tickets optimally. Johnny may redistribute all of his tickets arbitrarily between each update, but he may not place more than t tickets total or have more tickets in a single raffle than all other participants combined.
|
The first line contains two integers n, t, and q (1 β€ n, t, q β€ 200 000) β the number of raffles, the number of tickets Johnny has, and the total number of updates, respectively.The second line contains n space-separated integers pi (1 β€ pi β€ 1000) β the value of the i-th prize.The third line contains n space-separated integers li (1 β€ li β€ 1000) β the number of tickets initially in the i-th raffle.The last q lines contain the descriptions of the updates. Each description contains two integers tk, rk (1 β€ tk β€ 2, 1 β€ rk β€ n) β the type of the update and the raffle number. An update of type 1 represents another participant adding a ticket to raffle rk. An update of type 2 represents another participant removing a ticket from raffle rk.It is guaranteed that, after each update, each raffle has at least 1 ticket (not including Johnny's) in it.
|
Print q lines, each containing a single real number β the maximum expected value of Johnny's winnings after the k-th update. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .
|
In the first case, Johnny only has one ticket to distribute. The prizes are worth 4 and 5, and the raffles initially have 1 and 2 tickets, respectively. After the first update, each raffle has 2 tickets, so Johnny has expected value of winning by placing his ticket into the second raffle. The second update adds a ticket to the second raffle, so Johnny can win in the first raffle. After the final update, Johnny keeps his ticket in the first raffle and wins .In the second case, Johnny has more tickets than he is allowed to spend. In particular, after the first update, there are 7, 6, and 6 tickets in each raffle, respectively, so Johnny can only put in 19 tickets, winning each prize with probability . Also, note that after the last two updates, Johnny must remove a ticket from the last raffle in order to stay under the tickets in the third raffle.
|
Input: 2 1 34 51 21 11 22 1 | Output: 1.6666666671.3333333332.000000000
|
Master
| 4 | 1,332 | 851 | 358 | 6 |
24 |
E
|
24E
|
E. Berland collider
| 2,300 |
binary search
|
Recently the construction of Berland collider has been completed. Collider can be represented as a long narrow tunnel that contains n particles. We associate with collider 1-dimensional coordinate system, going from left to right. For each particle we know its coordinate and velocity at the moment of start of the collider. The velocities of the particles don't change after the launch of the collider. Berland scientists think that the big bang will happen at the first collision of particles, whose velocities differs in directions. Help them to determine how much time elapses after the launch of the collider before the big bang happens.
|
The first line contains single integer n (1 β€ n β€ 5Β·105) β amount of particles in the collider. Next n lines contain description of particles. Each particle is described by two integers xi, vi ( - 109 β€ xi, vi β€ 109, vi β 0) β coordinate and velocity respectively. All the coordinates are distinct. The particles are listed in order of increasing of coordinates. All the coordinates are in meters, and all the velocities β in meters per second. The negative velocity means that after the start of collider the particle will move to the left, and the positive β that the particle will move to the right.
|
If there will be no big bang, output -1. Otherwise output one number β how much time in seconds elapses after the launch of the collider before the big bang happens. Your answer must have a relative or absolute error less than 10 - 9.
|
Input: 3-5 90 15 -1 | Output: 1.00000000000000000000
|
Expert
| 1 | 642 | 602 | 234 | 0 |
|
1,108 |
E1
|
1108E1
|
E1. Array and Segments (Easy version)
| 1,800 |
brute force; greedy; implementation
|
The only difference between easy and hard versions is a number of elements in the array.You are given an array \(a\) consisting of \(n\) integers. The value of the \(i\)-th element of the array is \(a_i\).You are also given a set of \(m\) segments. The \(j\)-th segment is \([l_j; r_j]\), where \(1 \le l_j \le r_j \le n\).You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array \(a = [0, 0, 0, 0, 0]\) and the given segments are \([1; 3]\) and \([2; 4]\) then you can choose both of them and the array will become \(b = [-1, -2, -2, -1, 0]\).You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array \(a\) and obtain the array \(b\) then the value \(\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i\) will be maximum possible.Note that you can choose the empty set.If there are multiple answers, you can print any.If you are Python programmer, consider using PyPy instead of Python when you submit your code.
|
The first line of the input contains two integers \(n\) and \(m\) (\(1 \le n \le 300, 0 \le m \le 300\)) β the length of the array \(a\) and the number of segments, respectively.The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(-10^6 \le a_i \le 10^6\)), where \(a_i\) is the value of the \(i\)-th element of the array \(a\).The next \(m\) lines are contain two integers each. The \(j\)-th of them contains two integers \(l_j\) and \(r_j\) (\(1 \le l_j \le r_j \le n\)), where \(l_j\) and \(r_j\) are the ends of the \(j\)-th segment.
|
In the first line of the output print one integer \(d\) β the maximum possible value \(\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i\) if \(b\) is the array obtained by applying some subset of the given segments to the array \(a\).In the second line of the output print one integer \(q\) (\(0 \le q \le m\)) β the number of segments you apply.In the third line print \(q\) distinct integers \(c_1, c_2, \dots, c_q\) in any order (\(1 \le c_k \le m\)) β indices of segments you apply to the array \(a\) in such a way that the value \(\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i\) of the obtained array \(b\) is maximum possible.If there are multiple answers, you can print any.
|
In the first example the obtained array \(b\) will be \([0, -4, 1, 1, 2]\) so the answer is \(6\).In the second example the obtained array \(b\) will be \([2, -3, 1, -1, 4]\) so the answer is \(7\).In the third example you cannot do anything so the answer is \(0\).
|
Input: 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 | Output: 6 2 1 4
|
Medium
| 3 | 1,127 | 565 | 690 | 11 |
2,025 |
G
|
2025G
|
G. Variable Damage
| 3,000 |
data structures; flows
|
Monocarp is gathering an army to fight a dragon in a videogame.The army consists of two parts: the heroes and the defensive artifacts. Each hero has one parameter β his health. Each defensive artifact also has one parameter β its durability.Before the battle begins, Monocarp distributes artifacts to the heroes so that each hero receives at most one artifact.The battle consists of rounds that proceed as follows: first, the dragon deals damage equal to \(\frac{1}{a + b}\) (a real number without rounding) to each hero, where \(a\) is the number of heroes alive and \(b\) is the number of active artifacts; after that, all heroes with health \(0\) or less die; finally, some artifacts are deactivated. An artifact with durability \(x\) is deactivated when one of the following occurs: the hero holding the artifact either dies or receives \(x\) total damage (from the start of the battle). If an artifact is not held by any hero, it is inactive from the beginning of the battle. The battle ends when there are no heroes left alive.Initially, the army is empty. There are \(q\) queries: add a hero with health \(x\) or an artifact with durability \(y\) to the army. After each query, determine the maximum number of rounds that Monocarp can survive if he distributes the artifacts optimally.
|
The first line contains one integer \(q\) (\(1 \le q \le 3 \cdot 10^5\)) β the number of queries.In the \(i\)-th of the following \(q\) lines, there are two integers \(t_i\) and \(v_i\) (\(t_i \in \{1, 2\}\); \(1 \le v_i \le 10^9\)) β the type of the query and the value of the query parameter. If the type is \(1\), a hero with health \(v_i\) is added. If the type is \(2\), an artifact with durability \(v_i\) is added.
|
Print \(q\) integers. After each query, output the maximum number of rounds that Monocarp can survive if he distributes the artifacts optimally.
|
Let's consider the first example. An artifact with durability \(5\) is added. Since there are no heroes yet, the battle ends immediately. A hero with health \(4\) is added. Monocarp can give him an artifact with durability \(5\). First, there are rounds in which the hero takes \(\frac{1}{1 + 1} = \frac{1}{2}\) damage. After \(8\) such rounds, a total of \(4\) damage will have been dealt, and the hero will die, while the artifact will deactivate. There are no more heroes alive, so the battle ends after \(8\) rounds. A hero with health \(10\) is added. Now let the artifact with durability \(5\) be with this hero. Then, in the first \(12\) rounds, the heroes will take \(12 \cdot \frac{1}{2 + 1} = 4\) damage, and the first hero will die. The second hero has \(6\) health left, and the artifact has \(1\) durability. Now the damage is \(\frac{1}{2}\), so after another \(2\) rounds, the artifact will deactivate. The second hero has \(5\) health left. After another \(5\) rounds, the second hero will die. Therefore, the answer is \(12 + 2 + 5 = 19\).
|
Input: 32 51 41 10 | Output: 0 8 19
|
Master
| 2 | 1,292 | 421 | 144 | 20 |
2,129 |
F1
|
2129F1
|
F1. Top-K Tracker (Easy Version)
| 3,500 |
interactive
|
This is an interactive problem.This is the easy version of the problem. The only difference is that \(n \le 845\) in this version. You can make hacks only if all versions of the problem are solved.There is a hidden permutation \(p\), which is a permutation of \(\{1,2,\ldots,n\}\). Let \(pos_i\) denote the position of the value \(i\) in \(p\), i.e., \(pos_{p_i} = i\).To find this permutation, you can ask four types of queries: ""\(1\ m\ i_1\ i_2\ \ldots\ i_m\)"" (\(1 \leq m \leq n\), \(i_1,\ i_2,\ \ldots,\ i_m\) must be distinct). Let \(k = \min(60, m)\). The jury will return the top \(k\) largest number(s) in \([p_{i_1}, p_{i_2}, \ldots, p_{i_m}]\) in increasing order. ""\(2\ m\ i_1\ i_2\ \ldots\ i_m\)"" (\(1 \leq m \leq n\), \(i_1,\ i_2,\ \ldots,\ i_m\) must be distinct). Let \(k = \min(60, m)\). The jury will return the top \(k\) largest number(s) in \([pos_{i_1}, pos_{i_2}, \ldots, pos_{i_m}]\) in increasing order. ""\(3\ m\ i_1\ i_2\ ...\ i_m\)"" (\(1 \leq m \leq n\), \(i_1,\ i_2,\ ...,\ i_m\) must be distinct). Let \(k = \min(300, m)\). The jury will return the top \(k\) largest number(s) in \([p_{i_1}, p_{i_2}, ..., p_{i_m}]\) in increasing order. ""\(4\ m\ i_1\ i_2\ \ldots\ i_m\)"" (\(1 \leq m \leq n\), \(i_1,\ i_2,\ \ldots,\ i_m\) must be distinct). Let \(k = \min(300, m)\). The jury will return the top \(k\) largest number(s) in \([pos_{i_1}, pos_{i_2}, \ldots, pos_{i_m}]\) in increasing order. Let \(c_i\) be the usage count of queries of type \(i\) \((1 \le i \le 4)\). Your query count must satisfy the following constraints: \(c_1+c_2+c_3+c_4 \le 30.\) \(c_3+c_4 \le 1.\)
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 40\)). The description of the test cases follows.
|
In the first test case, the hidden permutation is \(p = [3, 1, 2]\), thus \(pos = [2, 3, 1]\).For the query ""3 2 3 1"", the jury returns \(2\) and \(3\) because \(2\) and \(3\) are the top \(k\) largest number(s) in \([p_3, p_1]\), where \(k = \min(300, m) = \min(300, 2) = 2\).For the query ""2 1 2"", the jury returns \(3\) because \(3\) is the top \(k\) largest number in \([pos_2]\), where \(k = \min(60, m) = \min(60, 1) = 1\).Note that the example is only for understanding the statement and does not guarantee finding the unique permutation \(p\).
|
Input: 2 3 2 3 3 2 2 | Output: 3 2 3 1 2 1 2 ! 3 1 2 4 1 1 ! 2 1
|
Master
| 1 | 1,607 | 159 | 0 | 21 |
|
683 |
J
|
683J
|
J. The Hero with Bombs
| 3,000 |
*special
|
In a new computer game you need to help the hero to get out of the maze, which is a rectangular field of size n Γ m. The hero is located in one of the cells of this field. He knows where the exit of the maze is, and he wants to reach it.In one move, the hero can either move to the next cell (i.e. the cell which has a common side with the current cell) if it is free, or plant a bomb on the cell where he is, or skip the move and do nothing. A planted bomb explodes after three moves, that is, after the hero makes 3 more actions but does not have time to make the fourth (all three types of moves described above are considered as actions).The explosion destroys the obstacles in all the cells which have at least one common point with this cell (i.e. in all the cells sharing with the bomb cell a corner or a side). The explosion must not hurt the cell with the exit or the cell with the hero. The hero can not go beyond the boundaries of the maze.Your task is to determine the sequence of hero's actions to reach the exit. Note that you haven't to minimize the length of the sequence. The only restriction β the length of the resulting sequence should not exceed 100,000 symbols.
|
The first line contains two integers n and m (1 β€ n, m β€ 100, nΒ·m > 1) β sizes of the maze.Each of the following n lines contains m characters β description of the maze. The character ""."" means a free cell, ""E"" β the hero, ""T"" β the exit, ""X"" β the obstacle.It is guaranteed that there is exactly one hero and exactly one exit in the maze.
|
Print the hero's actions which will help him get out of the maze (""M"" β to plant a bomb, ""T"" β to skip the move, ""S"" β to go down, ""W"" β to go left, ""N"" β to go up, ""E"" β to go right). If the hero can not reach the exit, print ""No solution"" (without quotes).The length of the resulting sequence should not exceed 100,000 symbols. If there are several solutions it is allowed to print any of them.
|
Input: 3 5XEX.XX.XXTX.X.X | Output: SSMNNTSSNEMWWTEEEE
|
Master
| 1 | 1,183 | 347 | 410 | 6 |
|
2,126 |
B
|
2126B
|
B. No Casino in the Mountains
| 800 |
dp; greedy
|
You are given an array \(a\) of \(n\) numbers and a number \(k\). The value \(a_i\) describes the weather on the \(i\)-th day: if it rains on the \(i\)-th day, then \(a_i = 1\); otherwise, if the weather is good on the \(i\)-th day, then \(a_i = 0\).Jean wants to visit as many peaks as possible. One hike to a peak takes exactly \(k\) days, and during each of these days, the weather must be good (\(a_i = 0\)). That is, formally, he can start a hike on day \(i\) only if all \(a_j = 0\) for all \(j\) \((i \leq j \leq i + k - 1)\).After each hike, before starting the next one, Jean must take a break of at least one day, meaning that on the day following a hike, he cannot go on another hike.Find the maximum number of peaks that Jean can visit.
|
Each test consists of several test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of the test cases follows. The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 10^5\), \(1 \le k \le n\)). The second line contains \(n\) numbers \(a_i\) (\(a_i \in \{0, 1\}\)), where \(a_i\) denotes the weather on the \(i\)-th day.It is guaranteed that the total value of \(n\) across all test cases does not exceed \(10^5\).
|
For each test case, output a single integer: the maximum number of hikes that Jean can make.
|
In the first sample: Day \(1\) β good weather, Jean goes on a hike. (\(a_1 = 0\)) Day \(2\) β mandatory break. Day \(3\) β again good weather, Jean goes on the second hike. (\(a_3 = 0\)) Day \(4\) β break. Day \(5\) β good weather, third hike. (\(a_5 = 0\)) Thus, Jean can make 3 hikes, alternating each with a mandatory day of rest. In the second sample: From day \(1\) to day \(3\) β three days of good weather, Jean goes on a hike. (\(a_1 = a_2 = a_3 = 0\)) Day \(4\) β mandatory break. From day \(5\) to day \(7\) β again three days of good weather, Jean goes on the second hike. (\(a_5 = a_6 = a_7 = 0\)) In total, Jean makes 2 hikes. In the third sample: There are no days with good weather (\(a_i = 1\) for all \(i\)) Jean cannot make any hikes. Answer: 0
|
Input: 55 10 1 0 0 07 30 0 0 0 0 0 03 11 1 14 20 1 0 16 20 0 1 0 0 0 | Output: 3 2 0 0 2
|
Beginner
| 2 | 748 | 516 | 92 | 21 |
79 |
E
|
79E
|
E. Security System
| 2,900 |
math
|
Fox Ciel safely returned to her castle, but there was something wrong with the security system of the castle: sensors attached in the castle were covering her.Ciel is at point (1, 1) of the castle now, and wants to move to point (n, n), which is the position of her room. By one step, Ciel can move from point (x, y) to either (x + 1, y) (rightward) or (x, y + 1) (upward).In her castle, c2 sensors are set at points (a + i, b + j) (for every integer i and j such that: 0 β€ i < c, 0 β€ j < c).Each sensor has a count value and decreases its count value every time Ciel moves. Initially, the count value of each sensor is t. Every time Ciel moves to point (x, y), the count value of a sensor at point (u, v) decreases by (|u - x| + |v - y|). When the count value of some sensor becomes strictly less than 0, the sensor will catch Ciel as a suspicious individual!Determine whether Ciel can move from (1, 1) to (n, n) without being caught by a sensor, and if it is possible, output her steps. Assume that Ciel can move to every point even if there is a censor on the point.
|
In the first line there are five integers n, t, a, b, c (2 β€ n β€ 2Β·105, 0 β€ t β€ 1014, 1 β€ a β€ n - c + 1, 1 β€ b β€ n - c + 1, 1 β€ c β€ n).Please do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin stream (also you may use the %I64d specificator).
|
If Ciel's objective is possible, output in first line 2n - 2 characters that represent her feasible steps, where i-th character is R if i-th step is moving rightward, or U if moving upward. If there are several solution, output lexicographically first one. Character R is lexicographically earlier than the character U.If her objective is impossible, output Impossible.
|
The answers for the first sample and the second sample are shown on the picture: Here, a red point represents a point that contains a sensor.
|
Input: 5 25 2 4 1 | Output: RRUURURU
|
Master
| 1 | 1,069 | 296 | 369 | 0 |
1,520 |
C
|
1520C
|
C. Not Adjacent Matrix
| 1,000 |
constructive algorithms
|
We will consider the numbers \(a\) and \(b\) as adjacent if they differ by exactly one, that is, \(|a-b|=1\).We will consider cells of a square matrix \(n \times n\) as adjacent if they have a common side, that is, for cell \((r, c)\) cells \((r, c-1)\), \((r, c+1)\), \((r-1, c)\) and \((r+1, c)\) are adjacent to it.For a given number \(n\), construct a square matrix \(n \times n\) such that: Each integer from \(1\) to \(n^2\) occurs in this matrix exactly once; If \((r_1, c_1)\) and \((r_2, c_2)\) are adjacent cells, then the numbers written in them must not be adjacent.
|
The first line contains one integer \(t\) (\(1 \le t \le 100\)). Then \(t\) test cases follow.Each test case is characterized by one integer \(n\) (\(1 \le n \le 100\)).
|
For each test case, output: -1, if the required matrix does not exist; the required matrix, otherwise (any such matrix if many of them exist). The matrix should be outputted as \(n\) lines, where each line contains \(n\) integers.
|
Input: 3 1 2 3 | Output: 1 -1 2 9 7 4 6 3 1 8 5
|
Beginner
| 1 | 578 | 169 | 230 | 15 |
|
678 |
B
|
678B
|
B. The Same Calendar
| 1,600 |
implementation
|
The girl Taylor has a beautiful calendar for the year y. In the calendar all days are given with their days of week: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday.The calendar is so beautiful that she wants to know what is the next year after y when the calendar will be exactly the same. Help Taylor to find that year.Note that leap years has 366 days. The year is leap if it is divisible by 400 or it is divisible by 4, but not by 100 (https://en.wikipedia.org/wiki/Leap_year).
|
The only line contains integer y (1000 β€ y < 100'000) β the year of the calendar.
|
Print the only integer y' β the next year after y when the calendar will be the same. Note that you should find the first year after y with the same calendar.
|
Today is Monday, the 13th of June, 2016.
|
Input: 2016 | Output: 2044
|
Medium
| 1 | 499 | 81 | 158 | 6 |
190 |
A
|
190A
|
A. Vasya and the Bus
| 1,100 |
greedy; math
|
One day Vasya heard a story: ""In the city of High Bertown a bus number 62 left from the bus station. It had n grown-ups and m kids...""The latter events happen to be of no importance to us. Vasya is an accountant and he loves counting money. So he wondered what maximum and minimum sum of money these passengers could have paid for the ride.The bus fare equals one berland ruble in High Bertown. However, not everything is that easy β no more than one child can ride for free with each grown-up passenger. That means that a grown-up passenger who rides with his k (k > 0) children, pays overall k rubles: a ticket for himself and (k - 1) tickets for his children. Also, a grown-up can ride without children, in this case he only pays one ruble.We know that in High Bertown children can't ride in a bus unaccompanied by grown-ups.Help Vasya count the minimum and the maximum sum in Berland rubles, that all passengers of this bus could have paid in total.
|
The input file consists of a single line containing two space-separated numbers n and m (0 β€ n, m β€ 105) β the number of the grown-ups and the number of the children in the bus, correspondingly.
|
If n grown-ups and m children could have ridden in the bus, then print on a single line two space-separated integers β the minimum and the maximum possible total bus fare, correspondingly. Otherwise, print ""Impossible"" (without the quotes).
|
In the first sample a grown-up rides with two children and pays two rubles.In the second sample there are only children in the bus, so the situation is impossible. In the third sample there are two cases: Each of the two grown-ups rides with one children and pays one ruble for the tickets. In this case the passengers pay two rubles in total. One of the grown-ups ride with two children's and pays two rubles, the another one rides alone and pays one ruble for himself. So, they pay three rubles in total.
|
Input: 1 2 | Output: 2 2
|
Easy
| 2 | 955 | 194 | 242 | 1 |
950 |
B
|
950B
|
B. Intercepted Message
| 1,100 |
greedy; implementation
|
Hacker Zhorik wants to decipher two secret messages he intercepted yesterday. Yeah message is a sequence of encrypted blocks, each of them consists of several bytes of information.Zhorik knows that each of the messages is an archive containing one or more files. Zhorik knows how each of these archives was transferred through the network: if an archive consists of k files of sizes l1, l2, ..., lk bytes, then the i-th file is split to one or more blocks bi, 1, bi, 2, ..., bi, mi (here the total length of the blocks bi, 1 + bi, 2 + ... + bi, mi is equal to the length of the file li), and after that all blocks are transferred through the network, maintaining the order of files in the archive.Zhorik thinks that the two messages contain the same archive, because their total lengths are equal. However, each file can be split in blocks in different ways in the two messages.You are given the lengths of blocks in each of the two messages. Help Zhorik to determine what is the maximum number of files could be in the archive, if the Zhorik's assumption is correct.
|
The first line contains two integers n, m (1 β€ n, m β€ 105) β the number of blocks in the first and in the second messages.The second line contains n integers x1, x2, ..., xn (1 β€ xi β€ 106) β the length of the blocks that form the first message.The third line contains m integers y1, y2, ..., ym (1 β€ yi β€ 106) β the length of the blocks that form the second message.It is guaranteed that x1 + ... + xn = y1 + ... + ym. Also, it is guaranteed that x1 + ... + xn β€ 106.
|
Print the maximum number of files the intercepted array could consist of.
|
In the first example the maximum number of files in the archive is 3. For example, it is possible that in the archive are three files of sizes 2 + 5 = 7, 15 = 3 + 1 + 11 = 8 + 2 + 4 + 1 and 4 + 4 = 8.In the second example it is possible that the archive contains two files of sizes 1 and 110 = 10 + 100 = 100 + 10. Note that the order of files is kept while transferring archives through the network, so we can't say that there are three files of sizes 1, 10 and 100.In the third example the only possibility is that the archive contains a single file of size 4.
|
Input: 7 62 5 3 1 11 4 47 8 2 4 1 8 | Output: 3
|
Easy
| 2 | 1,067 | 467 | 73 | 9 |
1,788 |
B
|
1788B
|
B. Sum of Two Numbers
| 1,100 |
constructive algorithms; greedy; implementation; math; probabilities
|
The sum of digits of a non-negative integer \(a\) is the result of summing up its digits together when written in the decimal system. For example, the sum of digits of \(123\) is \(6\) and the sum of digits of \(10\) is \(1\). In a formal way, the sum of digits of \(\displaystyle a=\sum_{i=0}^{\infty} a_i \cdot 10^i\), where \(0 \leq a_i \leq 9\), is defined as \(\displaystyle\sum_{i=0}^{\infty}{a_i}\).Given an integer \(n\), find two non-negative integers \(x\) and \(y\) which satisfy the following conditions. \(x+y=n\), and the sum of digits of \(x\) and the sum of digits of \(y\) differ by at most \(1\). It can be shown that such \(x\) and \(y\) always exist.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10\,000\)). Each test case consists of a single integer \(n\) (\(1 \leq n \leq 10^9\))
|
For each test case, print two integers \(x\) and \(y\).If there are multiple answers, print any.
|
In the second test case, the sum of digits of \(67\) and the sum of digits of \(94\) are both \(13\).In the third test case, the sum of digits of \(60\) is \(6\), and the sum of digits of \(7\) is \(7\).
|
Input: 5 1 161 67 1206 19 | Output: 1 0 67 94 60 7 1138 68 14 5
|
Easy
| 5 | 670 | 196 | 96 | 17 |
2,036 |
G
|
2036G
|
G. Library of Magic
| 2,200 |
binary search; constructive algorithms; divide and conquer; interactive; math; number theory
|
This is an interactive problem.The Department of Supernatural Phenomena at the Oxenfurt Academy has opened the Library of Magic, which contains the works of the greatest sorcerers of Redania β \(n\) (\(3 \leq n \leq 10^{18}\)) types of books, numbered from \(1\) to \(n\). Each book's type number is indicated on its spine. Moreover, each type of book is stored in the library in exactly two copies! And you have been appointed as the librarian.One night, you wake up to a strange noise and see a creature leaving the building through a window. Three thick tomes of different colors were sticking out of the mysterious thief's backpack. Before you start searching for them, you decide to compute the numbers \(a\), \(b\), and \(c\) written on the spines of these books. All three numbers are distinct.So, you have an unordered set of tomes, which includes one tome with each of the pairwise distinct numbers \(a\), \(b\), and \(c\), and two tomes for all numbers from \(1\) to \(n\), except for \(a\), \(b\), and \(c\). You want to find these values \(a\), \(b\), and \(c\).Since you are not working in a simple library, but in the Library of Magic, you can only use one spell in the form of a query to check the presence of books in their place: ""xor l r"" β Bitwise XOR query with parameters \(l\) and \(r\). Let \(k\) be the number of such tomes in the library whose numbers are greater than or equal to \(l\) and less than or equal to \(r\). You will receive the result of the computation \(v_1 \oplus v_2 \oplus ... \oplus v_k\), where \(v_1 ... v_k\) are the numbers on the spines of these tomes, and \(\oplus\) denotes the operation of bitwise exclusive OR. Since your magical abilities as a librarian are severely limited, you can make no more than \(150\) queries.
|
The first line of input contains an integer \(t\) (\(1 \le t \le 300\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(3 \leq n \leq 10^{18}\)) β the number of types of tomes.
|
In the first test case, the books in the library after the theft look like this:Now consider the answers to the queries: For the query ""xor 1 1"", you receive the result \(1 \oplus 1 = 0\). Two tomes satisfy the condition specified in the query β both with the number \(1\). For the query ""xor 2 2"", you receive the result \(2\), as only one tome satisfies the specified condition. For the query ""xor 3 3"", you receive the result \(3\). For the query ""xor 4 6"", you receive the result \(4 \oplus 6 \oplus 4 \oplus 5 \oplus 6 = 5\). In the second test case, there are only \(3\) types of books, and it is easy to guess that the missing ones have the numbers \(1\), \(2\), and \(3\).
|
Input: 2 6 0 2 3 5 3 | Output: xor 1 1 xor 2 2 xor 3 3 xor 4 6 ans 2 3 5 ans 1 2 3
|
Hard
| 6 | 1,774 | 223 | 0 | 20 |
|
1,728 |
C
|
1728C
|
C. Digital Logarithm
| 1,400 |
data structures; greedy; sortings
|
Let's define \(f(x)\) for a positive integer \(x\) as the length of the base-10 representation of \(x\) without leading zeros. I like to call it a digital logarithm. Similar to a digital root, if you are familiar with that.You are given two arrays \(a\) and \(b\), each containing \(n\) positive integers. In one operation, you do the following: pick some integer \(i\) from \(1\) to \(n\); assign either \(f(a_i)\) to \(a_i\) or \(f(b_i)\) to \(b_i\). Two arrays are considered similar to each other if you can rearrange the elements in both of them, so that they are equal (e. g. \(a_i = b_i\) for all \(i\) from \(1\) to \(n\)).What's the smallest number of operations required to make \(a\) and \(b\) similar to each other?
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of testcases.The first line of the testcase contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of elements in each of the arrays.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i < 10^9\)).The third line contains \(n\) integers \(b_1, b_2, \dots, b_n\) (\(1 \le b_j < 10^9\)).The sum of \(n\) over all testcases doesn't exceed \(2 \cdot 10^5\).
|
For each testcase, print the smallest number of operations required to make \(a\) and \(b\) similar to each other.
|
In the first testcase, you can apply the digital logarithm to \(b_1\) twice.In the second testcase, the arrays are already similar to each other.In the third testcase, you can first apply the digital logarithm to \(a_1\), then to \(b_2\).
|
Input: 411100041 2 3 43 1 4 232 9 31 100 91075019 709259 5 611271314 9024533 81871864 9 3 6 48659503 2 371245467 6 7 37376159 8 364036498 52295554 169 | Output: 2 0 2 18
|
Easy
| 3 | 727 | 480 | 114 | 17 |
45 |
F
|
45F
|
F. Goats and Wolves
| 2,500 |
greedy
|
Once Vasya needed to transport m goats and m wolves from riverbank to the other as quickly as possible. The boat can hold n animals and Vasya, in addition, he is permitted to put less than n animals in the boat. If in one place (on one of the banks or in the boat) the wolves happen to strictly outnumber the goats, then the wolves eat the goats and Vasya gets upset. When Vasya swims on the boat from one shore to the other, he must take at least one animal to accompany him, otherwise he will get bored and he will, yet again, feel upset. When the boat reaches the bank, first all the animals get off simultaneously, and then the animals chosen by Vasya simultaneously get on the boat. That means that at the moment when the animals that have just arrived have already got off and the animals that are going to leave haven't yet got on, somebody might eat someone. Vasya needs to transport all the animals from one river bank to the other so that nobody eats anyone and Vasya doesn't get upset. What is the minimal number of times he will have to cross the river?
|
The first line contains two space-separated numbers m and n (1 β€ m, n β€ 105) β the number of animals and the boat's capacity.
|
If it is impossible to transport all the animals so that no one got upset, and all the goats survived, print -1. Otherwise print the single number β how many times Vasya will have to cross the river.
|
The first sample match to well-known problem for children.
|
Input: 3 2 | Output: 11
|
Expert
| 1 | 1,065 | 125 | 199 | 0 |
1,996 |
A
|
1996A
|
A. Legs
| 800 |
binary search; math; ternary search
|
It's another beautiful day on Farmer John's farm.After Farmer John arrived at his farm, he counted \(n\) legs. It is known only chickens and cows live on the farm, and a chicken has \(2\) legs while a cow has \(4\). What is the minimum number of animals Farmer John can have on his farm assuming he counted the legs of all animals?
|
The first line contains single integer \(t\) (\(1 \leq t \leq 10^3\)) β the number of test cases.Each test case contains an integer \(n\) (\(2 \leq n \leq 2 \cdot 10^3\), \(n\) is even).
|
For each test case, output an integer, the minimum number of animals Farmer John can have on his farm.
|
Input: 3268 | Output: 1 2 2
|
Beginner
| 3 | 331 | 186 | 102 | 19 |
|
1,551 |
D1
|
1551D1
|
D1. Domino (easy version)
| 1,700 |
constructive algorithms; math
|
The only difference between this problem and D2 is that you don't have to provide the way to construct the answer in this problem, but you have to do it in D2.There's a table of \(n \times m\) cells (\(n\) rows and \(m\) columns). The value of \(n \cdot m\) is even.A domino is a figure that consists of two cells having a common side. It may be horizontal (one of the cells is to the right of the other) or vertical (one of the cells is above the other).You need to find out whether it is possible to place \(\frac{nm}{2}\) dominoes on the table so that exactly \(k\) of them are horizontal and all the other dominoes are vertical. The dominoes cannot overlap and must fill the whole table.
|
The first line contains one integer \(t\) (\(1 \le t \le 10\)) β the number of test cases. Then \(t\) test cases follow.Each test case consists of a single line. The line contains three integers \(n\), \(m\), \(k\) (\(1 \le n,m \le 100\), \(0 \le k \le \frac{nm}{2}\), \(n \cdot m\) is even) β the number of rows, columns and horizontal dominoes, respectively.
|
For each test case output ""YES"", if it is possible to place dominoes in the desired way, or ""NO"" otherwise.You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
Input: 8 4 4 2 2 3 0 3 2 3 1 2 0 2 4 2 5 2 2 2 17 16 2 1 1 | Output: YES YES YES NO YES NO YES NO
|
Medium
| 2 | 691 | 360 | 264 | 15 |
|
1,852 |
A
|
1852A
|
A. Ntarsis' Set
| 1,800 |
binary search; math; number theory
|
Ntarsis has been given a set \(S\), initially containing integers \(1, 2, 3, \ldots, 10^{1000}\) in sorted order. Every day, he will remove the \(a_1\)-th, \(a_2\)-th, \(\ldots\), \(a_n\)-th smallest numbers in \(S\) simultaneously.What is the smallest element in \(S\) after \(k\) days?
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^5\)). The description of the test cases follows.The first line of each test case consists of two integers \(n\) and \(k\) (\(1 \leq n,k \leq 2 \cdot 10^5\)) β the length of \(a\) and the number of days.The following line of each test case consists of \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^9\)) β the elements of array \(a\).It is guaranteed that: The sum of \(n\) over all test cases won't exceed \(2 \cdot 10^5\); The sum of \(k\) over all test cases won't exceed \(2 \cdot 10^5\); \(a_1 < a_2 < \cdots < a_n\) for all test cases.
|
For each test case, print an integer that is the smallest element in \(S\) after \(k\) days.
|
For the first test case, each day the \(1\)-st, \(2\)-nd, \(4\)-th, \(5\)-th, and \(6\)-th smallest elements need to be removed from \(S\). So after the first day, \(S\) will become \(\require{cancel}\) \(\{\cancel 1, \cancel 2, 3, \cancel 4, \cancel 5, \cancel 6, 7, 8, 9, \ldots\} = \{3, 7, 8, 9, \ldots\}\). The smallest element is \(3\).For the second case, each day the \(1\)-st, \(3\)-rd, \(5\)-th, \(6\)-th and \(7\)-th smallest elements need to be removed from \(S\). \(S\) will be changed as follows: Day\(S\) before\(S\) after1\(\{\cancel 1, 2, \cancel 3, 4, \cancel 5, \cancel 6, \cancel 7, 8, 9, 10, \ldots \}\)\(\to\)\(\{2, 4, 8, 9, 10, \ldots\}\)2\(\{\cancel 2, 4, \cancel 8, 9, \cancel{10}, \cancel{11}, \cancel{12}, 13, 14, 15, \ldots\}\)\(\to\)\(\{4, 9, 13, 14, 15, \ldots\}\)3\(\{\cancel 4, 9, \cancel{13}, 14, \cancel{15}, \cancel{16}, \cancel{17}, 18, 19, 20, \ldots\}\)\(\to\)\(\{9, 14, 18, 19, 20, \ldots\}\) The smallest element left after \(k = 3\) days is \(9\).
|
Input: 75 11 2 4 5 65 31 3 5 6 74 10002 3 4 59 14341 4 7 9 12 15 17 18 2010 41 3 5 7 9 11 13 15 17 1910 61 4 7 10 13 16 19 22 25 2810 1500001 3 4 5 10 11 12 13 14 15 | Output: 3 9 1 12874 16 18 1499986
|
Medium
| 3 | 287 | 669 | 92 | 18 |
665 |
D
|
665D
|
D. Simple Subset
| 1,800 |
constructive algorithms; greedy; number theory
|
A tuple of positive integers {x1, x2, ..., xk} is called simple if for all pairs of positive integers (i, j) (1 β€ i < j β€ k), xi + xj is a prime.You are given an array a with n positive integers a1, a2, ..., an (not necessary distinct). You want to find a simple subset of the array a with the maximum size.A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.Let's define a subset of the array a as a tuple that can be obtained from a by removing some (possibly all) elements of it.
|
The first line contains integer n (1 β€ n β€ 1000) β the number of integers in the array a.The second line contains n integers ai (1 β€ ai β€ 106) β the elements of the array a.
|
On the first line print integer m β the maximum possible size of simple subset of a.On the second line print m integers bl β the elements of the simple subset of the array a with the maximum size.If there is more than one solution you can print any of them. You can print the elements of the subset in any order.
|
Input: 22 3 | Output: 23 2
|
Medium
| 3 | 547 | 173 | 312 | 6 |
|
1,185 |
F
|
1185F
|
F. Two Pizzas
| 2,100 |
bitmasks; brute force
|
A company of \(n\) friends wants to order exactly two pizzas. It is known that in total there are \(9\) pizza ingredients in nature, which are denoted by integers from \(1\) to \(9\).Each of the \(n\) friends has one or more favorite ingredients: the \(i\)-th of friends has the number of favorite ingredients equal to \(f_i\) (\(1 \le f_i \le 9\)) and your favorite ingredients form the sequence \(b_{i1}, b_{i2}, \dots, b_{if_i}\) (\(1 \le b_{it} \le 9\)).The website of CodePizza restaurant has exactly \(m\) (\(m \ge 2\)) pizzas. Each pizza is characterized by a set of \(r_j\) ingredients \(a_{j1}, a_{j2}, \dots, a_{jr_j}\) (\(1 \le r_j \le 9\), \(1 \le a_{jt} \le 9\)) , which are included in it, and its price is \(c_j\).Help your friends choose exactly two pizzas in such a way as to please the maximum number of people in the company. It is known that a person is pleased with the choice if each of his/her favorite ingredients is in at least one ordered pizza. If there are several ways to choose two pizzas so as to please the maximum number of friends, then choose the one that minimizes the total price of two pizzas.
|
The first line of the input contains two integers \(n\) and \(m\) (\(1 \le n \le 10^5, 2 \le m \le 10^5\)) β the number of friends in the company and the number of pizzas, respectively.Next, the \(n\) lines contain descriptions of favorite ingredients of the friends: the \(i\)-th of them contains the number of favorite ingredients \(f_i\) (\(1 \le f_i \le 9\)) and a sequence of distinct integers \(b_{i1}, b_{i2}, \dots, b_{if_i}\) (\(1 \le b_{it} \le 9\)).Next, the \(m\) lines contain pizza descriptions: the \(j\)-th of them contains the integer price of the pizza \(c_j\) (\(1 \le c_j \le 10^9\)), the number of ingredients \(r_j\) (\(1 \le r_j \le 9\)) and the ingredients themselves as a sequence of distinct integers \(a_{j1}, a_{j2}, \dots, a_{jr_j}\) (\(1 \le a_{jt} \le 9\)).
|
Output two integers \(j_1\) and \(j_2\) (\(1 \le j_1,j_2 \le m\), \(j_1 \ne j_2\)) denoting the indices of two pizzas in the required set. If there are several solutions, output any of them. Pizza indices can be printed in any order.
|
Input: 3 4 2 6 7 4 2 3 9 5 3 2 3 9 100 1 7 400 3 3 2 5 100 2 9 2 500 3 2 9 5 | Output: 2 3
|
Hard
| 2 | 1,131 | 788 | 233 | 11 |
|
570 |
B
|
570B
|
B. Simple Game
| 1,300 |
constructive algorithms; games; greedy; implementation; math
|
One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a.Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins.Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible.More formally, you need to find such integer a (1 β€ a β€ n), that the probability that is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive).
|
The first line contains two integers n and m (1 β€ m β€ n β€ 109) β the range of numbers in the game, and the number selected by Misha respectively.
|
Print a single number β such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them.
|
In the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0.In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less.
|
Input: 3 1 | Output: 2
|
Easy
| 5 | 896 | 145 | 149 | 5 |
594 |
C
|
594C
|
C. Edo and Magnets
| 2,300 |
brute force; greedy; implementation; two pointers
|
Edo has got a collection of n refrigerator magnets!He decided to buy a refrigerator and hang the magnets on the door. The shop can make the refrigerator with any size of the door that meets the following restrictions: the refrigerator door must be rectangle, and both the length and the width of the door must be positive integers.Edo figured out how he wants to place the magnets on the refrigerator. He introduced a system of coordinates on the plane, where each magnet is represented as a rectangle with sides parallel to the coordinate axes.Now he wants to remove no more than k magnets (he may choose to keep all of them) and attach all remaining magnets to the refrigerator door, and the area of ββthe door should be as small as possible. A magnet is considered to be attached to the refrigerator door if its center lies on the door or on its boundary. The relative positions of all the remaining magnets must correspond to the plan.Let us explain the last two sentences. Let's suppose we want to hang two magnets on the refrigerator. If the magnet in the plan has coordinates of the lower left corner (x1, y1) and the upper right corner (x2, y2), then its center is located at (, ) (may not be integers). By saying the relative position should correspond to the plan we mean that the only available operation is translation, i.e. the vector connecting the centers of two magnets in the original plan, must be equal to the vector connecting the centers of these two magnets on the refrigerator.The sides of the refrigerator door must also be parallel to coordinate axes.
|
The first line contains two integers n and k (1 β€ n β€ 100 000, 0 β€ k β€ min(10, n - 1)) β the number of magnets that Edo has and the maximum number of magnets Edo may not place on the refrigerator.Next n lines describe the initial plan of placing magnets. Each line contains four integers x1, y1, x2, y2 (1 β€ x1 < x2 β€ 109, 1 β€ y1 < y2 β€ 109) β the coordinates of the lower left and upper right corners of the current magnet. The magnets can partially overlap or even fully coincide.
|
Print a single integer β the minimum area of the door of refrigerator, which can be used to place at least n - k magnets, preserving the relative positions.
|
In the first test sample it is optimal to remove either the first or the third magnet. If we remove the first magnet, the centers of two others will lie at points (2.5, 2.5) and (3.5, 3.5). Thus, it is enough to buy a fridge with door width 1 and door height 1, the area of the door also equals one, correspondingly.In the second test sample it doesn't matter which magnet to remove, the answer will not change β we need a fridge with door width 8 and door height 8.In the third sample you cannot remove anything as k = 0.
|
Input: 3 11 1 2 22 2 3 33 3 4 4 | Output: 1
|
Expert
| 4 | 1,576 | 482 | 156 | 5 |
1,129 |
A1
|
1129A1
|
A1. Toy Train (Simplified)
| 1,700 |
brute force; greedy
|
This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled.Alice received a set of Toy Trainβ’ from Bob. It consists of one train and a connected railway network of \(n\) stations, enumerated from \(1\) through \(n\). The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station \(i\) is station \(i+1\) if \(1 \leq i < n\) or station \(1\) if \(i = n\). It takes the train \(1\) second to travel to its next station as described.Bob gave Alice a fun task before he left: to deliver \(m\) candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from \(1\) through \(m\). Candy \(i\) (\(1 \leq i \leq m\)), now at station \(a_i\), should be delivered to station \(b_i\) (\(a_i \neq b_i\)). The blue numbers on the candies correspond to \(b_i\) values. The image corresponds to the \(1\)-st example. The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible.Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there.
|
The first line contains two space-separated integers \(n\) and \(m\) (\(2 \leq n \leq 100\); \(1 \leq m \leq 200\)) β the number of stations and the number of candies, respectively.The \(i\)-th of the following \(m\) lines contains two space-separated integers \(a_i\) and \(b_i\) (\(1 \leq a_i, b_i \leq n\); \(a_i \neq b_i\)) β the station that initially contains candy \(i\) and the destination station of the candy, respectively.
|
In the first and only line, print \(n\) space-separated integers, the \(i\)-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station \(i\).
|
Consider the second sample.If the train started at station \(1\), the optimal strategy is as follows. Load the first candy onto the train. Proceed to station \(2\). This step takes \(1\) second. Deliver the first candy. Proceed to station \(1\). This step takes \(1\) second. Load the second candy onto the train. Proceed to station \(2\). This step takes \(1\) second. Deliver the second candy. Proceed to station \(1\). This step takes \(1\) second. Load the third candy onto the train. Proceed to station \(2\). This step takes \(1\) second. Deliver the third candy. Hence, the train needs \(5\) seconds to complete the tasks.If the train were to start at station \(2\), however, it would need to move to station \(1\) before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is \(5+1 = 6\) seconds.
|
Input: 5 7 2 4 5 1 2 3 3 4 4 1 5 3 3 5 | Output: 10 9 10 10 9
|
Medium
| 2 | 1,580 | 433 | 205 | 11 |
1,906 |
M
|
1906M
|
M. Triangle Construction
| 1,700 |
greedy; math
|
You are given a regular \(N\)-sided polygon. Label one arbitrary side as side \(1\), then label the next sides in clockwise order as side \(2\), \(3\), \(\dots\), \(N\). There are \(A_i\) special points on side \(i\). These points are positioned such that side \(i\) is divided into \(A_i + 1\) segments with equal length.For instance, suppose that you have a regular \(4\)-sided polygon, i.e., a square. The following illustration shows how the special points are located within each side when \(A = [3, 1, 4, 6]\). The uppermost side is labelled as side \(1\). You want to create as many non-degenerate triangles as possible while satisfying the following requirements. Each triangle consists of \(3\) distinct special points (not necessarily from different sides) as its corners. Each special point can only become the corner of at most \(1\) triangle. All triangles must not intersect with each other.Determine the maximum number of non-degenerate triangles that you can create.A triangle is non-degenerate if it has a positive area.
|
The first line consists of an integer \(N\) (\(3 \leq N \leq 200\,000\)).The following line consists of \(N\) integers \(A_i\) (\(1 \leq A_i \leq 2 \cdot 10^9\)).
|
Output a single integer representing the maximum number of non-degenerate triangles that you can create.
|
Explanation for the sample input/output #1One possible construction which achieves maximum number of non-degenerate triangles can be seen in the following illustration. Explanation for the sample input/output #2One possible construction which achieves maximum number of non-degenerate triangles can be seen in the following illustration.
|
Input: 4 3 1 4 6 | Output: 4
|
Medium
| 2 | 1,037 | 162 | 104 | 19 |
717 |
C
|
717C
|
C. Potions Homework
| 1,200 |
implementation; sortings
|
Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen each other after a long time. The sun is shining, birds are singing, flowers are blooming, and their Potions class teacher, professor Snipe is sulky as usual. Due to his angst fueled by disappointment in his own life, he has given them a lot of homework in Potions class. Each of the n students has been assigned a single task. Some students do certain tasks faster than others. Thus, they want to redistribute the tasks so that each student still does exactly one task, and that all tasks are finished. Each student has their own laziness level, and each task has its own difficulty level. Professor Snipe is trying hard to improve their work ethics, so each studentβs laziness level is equal to their taskβs difficulty level. Both sets of values are given by the sequence a, where ai represents both the laziness level of the i-th student and the difficulty of his task. The time a student needs to finish a task is equal to the product of their laziness level and the taskβs difficulty. They are wondering, what is the minimum possible total time they must spend to finish all tasks if they distribute them in the optimal way. Each person should receive one task and each task should be given to one person. Print the answer modulo 10 007.
|
The first line of input contains integer n (1 β€ n β€ 100 000) β the number of tasks. The next n lines contain exactly one integer number ai (1 β€ ai β€ 100 000) β both the difficulty of the initial task and the laziness of the i-th students.
|
Print the minimum total time to finish all tasks modulo 10 007.
|
In the first sample, if the students switch their tasks, they will be able to finish them in 3 + 3 = 6 time units.
|
Input: 213 | Output: 6
|
Easy
| 2 | 1,422 | 238 | 63 | 7 |
162 |
I
|
162I
|
I. Truncatable primes
| 2,000 |
*special
|
A truncatable prime is a prime number which contains no zeros in decimal notation and all its suffixes are primes. 1 is considered to be not a prime.You are given a positive integer n. Figure out whether it is a truncatable prime.
|
The only line of input contains an integer n (2 β€ n β€ 107).
|
Output ""YES"" if n is a truncatable prime. Output ""NO"" otherwise. Quotes for clarity only.
|
In the first sample 19 is a prime but its suffix 9 is not.In the second sample 9137, 137, 37 and 7 are all primes, so 9137 is a truncatable prime.
|
Input: 19 | Output: NO
|
Hard
| 1 | 230 | 59 | 93 | 1 |
1,858 |
D
|
1858D
|
D. Trees and Segments
| 2,200 |
brute force; data structures; dp; greedy; two pointers
|
The teachers of the Summer Informatics School decided to plant \(n\) trees in a row, and it was decided to plant only oaks and firs. To do this, they made a plan, which can be represented as a binary string \(s\) of length \(n\). If \(s_i = 0\), then the \(i\)-th tree in the row should be an oak, and if \(s_i = 1\), then the \(i\)-th tree in the row should be a fir.The day of tree planting is tomorrow, and the day after tomorrow an inspector will come to the School. The inspector loves nature very much, and he will evaluate the beauty of the row as follows: First, he will calculate \(l_0\) as the maximum number of consecutive oaks in the row (the maximum substring consisting of zeros in the plan \(s\)). If there are no oaks in the row, then \(l_0 = 0\). Then, he will calculate \(l_1\) as the maximum number of consecutive firs in the row (the maximum substring consisting of ones in the plan \(s\)). If there are no firs in the row, then \(l_1 = 0\). Finally, he will calculate the beauty of the row as \(a \cdot l_0 + l_1\) for some \(a\) β the inspector's favourite number. The teachers know the value of the parameter \(a\), but for security reasons they cannot tell it to you. They only told you that \(a\) is an integer from \(1\) to \(n\).Since the trees have not yet been planted, the teachers decided to change the type of no more than \(k\) trees to the opposite (i.e., change \(s_i\) from \(0\) to \(1\) or from \(1\) to \(0\) in the plan) in order to maximize the beauty of the row of trees according to the inspector.For each integer \(j\) from \(1\) to \(n\) answer the following question independently: What is the maximum beauty of the row of trees that the teachers can achieve by changing the type of no more than \(k\) trees if the inspector's favourite number \(a\) is equal to \(j\)?
|
The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 3000\), \(0 \le k \le n\)) β the number of trees in the row and the maximum number of changes.The second line contains a string \(s\) of length \(n\), consisting of zeros and ones β the plan description.It is guaranteed that the sum of all \(n\) values for all test cases does not exceed \(3000\).
|
For each test case, print \(n\) integers, the \(j\)-th (\(1 \le j \le n\)) of which is the maximum beauty of the row of trees after no more than \(k\) changes if \(a = j\) is used to calculate the beauty.
|
In the first test case no changes are allowed, so \(l_0 = 0\) and \(l_1 = 3\) always hold. Thus, regardless of the value of \(a\), the beauty of the row of trees will be \(3\).In the second test case for \(a \in \{1, 2\}\) the teachers can, for example, change the plan \(s\) to \(0111\) (by changing \(s_4\)), and for \(a \in \{3, 4\}\) β to \(0010\) (by changing \(s_2\)). In this case, the beauty of the row for each \(a\) is calculated as follows: For \(a = 1\): \(l_0 = 1\), \(l_1 = 3\). The beauty of the row is \(1\cdot 1 + 3 = 4\). For \(a = 2\): \(l_0 = 1\), \(l_1 = 3\). The beauty of the row is \(2\cdot 1 + 3 = 5\). For \(a = 3\): \(l_0 = 2\), \(l_1 = 1\). The beauty of the row is \(3\cdot 2 + 1 = 7\). For \(a = 4\): \(l_0 = 2\), \(l_1 = 1\). The beauty of the row is \(4\cdot 2 + 1 = 9\). It can be shown that the changes described above are optimal for all \(a\) from \(1\) to \(4\).
|
Input: 53 01114 101105 0100006 21011017 10001101 | Output: 3 3 3 4 5 7 9 5 9 13 17 21 6 9 13 17 21 25 7 10 13 17 21 25 29
|
Hard
| 5 | 1,814 | 480 | 204 | 18 |
623 |
B
|
623B
|
B. Array GCD
| 2,300 |
dp; greedy; number theory
|
You are given array ai of length n. You may consecutively apply two operations to this array: remove some subsegment (continuous subsequence) of length m < n and pay for it mΒ·a coins; change some elements of the array by at most 1, and pay b coins for each change. Please note that each of operations may be applied at most once (and may be not applied at all) so you can remove only one segment and each number may be changed (increased or decreased) by at most 1. Also note, that you are not allowed to delete the whole array.Your goal is to calculate the minimum number of coins that you need to spend in order to make the greatest common divisor of the elements of the resulting array be greater than 1.
|
The first line of the input contains integers n, a and b (1 β€ n β€ 1 000 000, 0 β€ a, b β€ 109) β the length of the array, the cost of removing a single element in the first operation and the cost of changing an element, respectively.The second line contains n integers ai (2 β€ ai β€ 109) β elements of the array.
|
Print a single number β the minimum cost of changes needed to obtain an array, such that the greatest common divisor of all its elements is greater than 1.
|
In the first sample the optimal way is to remove number 3 and pay 1 coin for it.In the second sample you need to remove a segment [17, 13] and then decrease number 6. The cost of these changes is equal to 2Β·3 + 2 = 8 coins.
|
Input: 3 1 44 2 3 | Output: 1
|
Expert
| 3 | 707 | 309 | 155 | 6 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.