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,136
B
1136B
B. Nastya Is Playing Computer Games
1,000
constructive algorithms; math
Finished her homework, Nastya decided to play computer games. Passing levels one by one, Nastya eventually faced a problem. Her mission is to leave a room, where a lot of monsters live, as quickly as possible.There are \(n\) manholes in the room which are situated on one line, but, unfortunately, all the manholes are closed, and there is one stone on every manhole. There is exactly one coin under every manhole, and to win the game Nastya should pick all the coins. Initially Nastya stands near the \(k\)-th manhole from the left. She is thinking what to do.In one turn, Nastya can do one of the following: if there is at least one stone on the manhole Nastya stands near, throw exactly one stone from it onto any other manhole (yes, Nastya is strong). go to a neighboring manhole; if there are no stones on the manhole Nastya stays near, she can open it and pick the coin from it. After it she must close the manhole immediately (it doesn't require additional moves). The figure shows the intermediate state of the game. At the current position Nastya can throw the stone to any other manhole or move left or right to the neighboring manholes. If she were near the leftmost manhole, she could open it (since there are no stones on it). Nastya can leave the room when she picks all the coins. Monsters are everywhere, so you need to compute the minimum number of moves Nastya has to make to pick all the coins.Note one time more that Nastya can open a manhole only when there are no stones onto it.
The first and only line contains two integers \(n\) and \(k\), separated by space (\(2 \leq n \leq 5000\), \(1 \leq k \leq n\)) β€” the number of manholes and the index of manhole from the left, near which Nastya stays initially. Initially there is exactly one stone near each of the \(n\) manholes.
Print a single integer β€” minimum number of moves which lead Nastya to pick all the coins.
Let's consider the example where \(n = 2\), \(k = 2\). Nastya should play as follows: At first she throws the stone from the second manhole to the first. Now there are two stones on the first manhole. Then she opens the second manhole and pick the coin from it. Then she goes to the first manhole, throws two stones by two moves to the second manhole and then opens the manhole and picks the coin from it. So, \(6\) moves are required to win.
Input: 2 2 | Output: 6
Beginner
2
1,501
297
89
11
1,207
G
1207G
G. Indie Album
2,700
data structures; dfs and similar; hashing; string suffix structures; strings; trees
Mishka's favourite experimental indie band has recently dropped a new album! Songs of that album share one gimmick. Each name \(s_i\) is one of the following types: \(1~c\) β€” a single lowercase Latin letter; \(2~j~c\) β€” name \(s_j\) (\(1 \le j < i\)) with a single lowercase Latin letter appended to its end. Songs are numbered from \(1\) to \(n\). It's guaranteed that the first song is always of type \(1\).Vova is rather interested in the new album but he really doesn't have the time to listen to it entirely. Thus he asks Mishka some questions about it to determine if some song is worth listening to. Questions have the following format: \(i~t\) β€” count the number of occurrences of string \(t\) in \(s_i\) (the name of the \(i\)-th song of the album) as a continuous substring, \(t\) consists only of lowercase Latin letters. Mishka doesn't question the purpose of that information, yet he struggles to provide it. Can you please help Mishka answer all Vova's questions?
The first line contains a single integer \(n\) (\(1 \le n \le 4 \cdot 10^5\)) β€” the number of songs in the album.Each of the next \(n\) lines contains the desciption of the \(i\)-th song of the album in the following format: \(1~c\) β€” \(s_i\) is a single lowercase Latin letter; \(2~j~c\) β€” \(s_i\) is the name \(s_j\) (\(1 \le j < i\)) with a single lowercase Latin letter appended to its end. The next line contains a single integer \(m\) (\(1 \le m \le 4 \cdot 10^5\)) β€” the number of Vova's questions.Each of the next \(m\) lines contains the desciption of the \(j\)-th Vova's question in the following format: \(i~t\) (\(1 \le i \le n\), \(1 \le |t| \le 4 \cdot 10^5\)) β€” count the number of occurrences of string \(t\) in \(s_i\) (the name of the \(i\)-th song of the album) as a continuous substring, \(t\) consists only of lowercase Latin letters. It's guaranteed that the total length of question strings \(t\) doesn't exceed \(4 \cdot 10^5\).
For each question print a single integer β€” the number of occurrences of the question string \(t\) in the name of the \(i\)-th song of the album as a continuous substring.
Song names of the first example: d da dad dada dadad dadada dadadad dadadada d do dok doki dokid dokido dokidok dokidoki do dok doki dokidoki Thus the occurrences for each question string are: string ""da"" starts in positions \([1, 3, 5, 7]\) in the name ""dadadada""; string ""dada"" starts in positions \([1, 3, 5]\) in the name ""dadadada""; string ""ada"" starts in positions \([2, 4, 6]\) in the name ""dadadada""; string ""dada"" starts in positions \([1, 3]\) in the name ""dadada""; no occurrences of string ""dada"" in the name ""dad""; string ""doki"" starts in position \([1]\) in the name ""doki""; string ""ok"" starts in position \([2]\) in the name ""doki""; string ""doki"" starts in positions \([1, 5]\) in the name ""dokidoki""; string ""doki"" starts in position \([1]\) in the name ""dokidok""; string ""d"" starts in position \([1]\) in the name ""d""; no occurrences of string ""a"" in the name ""d""; string ""doki"" starts in positions \([1, 5]\) in the name ""dokidoki"".
Input: 20 1 d 2 1 a 2 2 d 2 3 a 2 4 d 2 5 a 2 6 d 2 7 a 1 d 2 9 o 2 10 k 2 11 i 2 12 d 2 13 o 2 14 k 2 15 i 2 1 o 2 17 k 2 18 i 2 15 i 12 8 da 8 dada 8 ada 6 dada 3 dada 19 doki 19 ok 16 doki 15 doki 9 d 1 a 20 doki | Output: 4 3 3 2 0 1 1 2 1 1 0 2
Master
6
977
952
170
12
353
E
353E
E. Antichain
2,200
dp; graph matchings; greedy
You have a directed acyclic graph G, consisting of n vertexes, numbered from 0 to n - 1. The graph contains n edges numbered from 0 to n - 1. An edge with number i connects vertexes i and (i + 1) mod n, and it can be directed in either direction (from i to (i + 1) mod n, or vise versa).Operation x mod y means taking the remainder after dividing number x by number y.Let's call two vertexes u and v in graph G comparable if the graph contains a path either from u to v or from v to u. We'll assume that an antichain is a set of vertexes of graph G, where any two distinct vertexes are not comparable. The size of an antichain is the number of vertexes in the corresponding set. An antichain is maximum if the graph doesn't have antichains of a larger size.Your task is to find the size of the maximum antichain in graph G.
The first line contains the sequence of characters s0s1... sn - 1 (2 ≀ n ≀ 106), consisting of numbers zero and one. The length of the line (number n) corresponds to the number of vertexes and edges in graph G. If character si (i β‰₯ 0) equals 0, then the edge between vertexes i and (i + 1) mod n is directed from the i-th vertex to the (i + 1) mod n-th one, otherwise β€” to the opposite point.It is guaranteed that the given graph is acyclic.
Print a single integer β€” the size of the maximum antichain of graph G.
Consider the first test sample. The graph's G edges are: 0 β†’ 1, 1 β†’ 2, 0 β†’ 2. We can choose the set of vertexes [0] as the maximum antichain. We cannot choose an antichain of larger size.
Input: 001 | Output: 1
Hard
3
823
441
70
3
341
D
341D
D. Iahub and Xors
2,500
data structures
Iahub does not like background stories, so he'll tell you exactly what this problem asks you for.You are given a matrix a with n rows and n columns. Initially, all values of the matrix are zeros. Both rows and columns are 1-based, that is rows are numbered 1, 2, ..., n and columns are numbered 1, 2, ..., n. Let's denote an element on the i-th row and j-th column as ai, j.We will call a submatrix (x0, y0, x1, y1) such elements ai, j for which two inequalities hold: x0 ≀ i ≀ x1, y0 ≀ j ≀ y1.Write a program to perform two following operations: Query(x0, y0, x1, y1): print the xor sum of the elements of the submatrix (x0, y0, x1, y1). Update(x0, y0, x1, y1, v): each element from submatrix (x0, y0, x1, y1) gets xor-ed by value v.
The first line contains two integers: n (1 ≀ n ≀ 1000) and m (1 ≀ m ≀ 105). The number m represents the number of operations you need to perform. Each of the next m lines contains five or six integers, depending on operation type. If the i-th operation from the input is a query, the first number from i-th line will be 1. It will be followed by four integers x0, y0, x1, y1. If the i-th operation is an update, the first number from the i-th line will be 2. It will be followed by five integers x0, y0, x1, y1, v. It is guaranteed that for each update operation, the following inequality holds: 0 ≀ v < 262. It is guaranteed that for each operation, the following inequalities hold: 1 ≀ x0 ≀ x1 ≀ n, 1 ≀ y0 ≀ y1 ≀ n.
For each query operation, output on a new line the result.
After the first 3 operations, the matrix will look like this: 1 1 21 1 23 3 3The fourth operation asks us to compute 1 xor 2 xor 3 xor 3 = 3.The fifth operation asks us to compute 1 xor 3 = 2.
Input: 3 52 1 1 2 2 12 1 3 2 3 22 3 1 3 3 31 2 2 3 31 2 2 3 2 | Output: 32
Expert
1
734
717
58
3
1,452
E
1452E
E. Two Editorials
2,500
brute force; dp; greedy; sortings; two pointers
Berland regional ICPC contest has just ended. There were \(m\) participants numbered from \(1\) to \(m\), who competed on a problemset of \(n\) problems numbered from \(1\) to \(n\).Now the editorial is about to take place. There are two problem authors, each of them is going to tell the tutorial to exactly \(k\) consecutive tasks of the problemset. The authors choose the segment of \(k\) consecutive tasks for themselves independently of each other. The segments can coincide, intersect or not intersect at all.The \(i\)-th participant is interested in listening to the tutorial of all consecutive tasks from \(l_i\) to \(r_i\). Each participant always chooses to listen to only the problem author that tells the tutorials to the maximum number of tasks he is interested in. Let this maximum number be \(a_i\). No participant can listen to both of the authors, even if their segments don't intersect.The authors want to choose the segments of \(k\) consecutive tasks for themselves in such a way that the sum of \(a_i\) over all participants is maximized.
The first line contains three integers \(n, m\) and \(k\) (\(1 \le n, m \le 2000\), \(1 \le k \le n\)) β€” the number of problems, the number of participants and the length of the segment of tasks each of the problem authors plans to tell the tutorial to.The \(i\)-th of the next \(m\) lines contains two integers \(l_i\) and \(r_i\) (\(1 \le l_i \le r_i \le n\)) β€” the segment of tasks the \(i\)-th participant is interested in listening to the tutorial to.
Print a single integer β€” the maximum sum of \(a_i\) over all participants.
In the first example the first author can tell the tutorial to problems from \(1\) to \(3\) and the second one β€” from \(6\) to \(8\). That way the sequence of \(a_i\) will be \([3, 2, 3, 3, 3]\). Notice that the last participant can't listen to both author, he only chooses the one that tells the maximum number of problems he's interested in.In the second example the first one can tell problems \(2\) to \(4\), the second one β€” \(4\) to \(6\).In the third example the first one can tell problems \(1\) to \(1\), the second one β€” \(2\) to \(2\). Or \(4\) to \(4\) and \(3\) to \(3\). Every pair of different problems will get the same sum of \(2\).In the fourth example the first one can tell problems \(1\) to \(5\), the second one β€” \(1\) to \(5\) as well.
Input: 10 5 3 1 3 2 4 6 9 6 9 1 8 | Output: 14
Expert
5
1,059
456
74
14
1,553
D
1553D
D. Backspace
1,500
dp; greedy; strings; two pointers
You are given two strings \(s\) and \(t\), both consisting of lowercase English letters. You are going to type the string \(s\) character by character, from the first character to the last one.When typing a character, instead of pressing the button corresponding to it, you can press the ""Backspace"" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if \(s\) is ""abcbd"" and you press Backspace instead of typing the first and the fourth characters, you will get the string ""bd"" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if \(s\) is ""abcaa"" and you press Backspace instead of the last two letters, then the resulting text is ""a"".Your task is to determine whether you can obtain the string \(t\), if you type the string \(s\) and press ""Backspace"" instead of typing several (maybe zero) characters of \(s\).
The first line contains a single integer \(q\) (\(1 \le q \le 10^5\)) β€” the number of test cases.The first line of each test case contains the string \(s\) (\(1 \le |s| \le 10^5\)). Each character of \(s\) is a lowercase English letter.The second line of each test case contains the string \(t\) (\(1 \le |t| \le 10^5\)). Each character of \(t\) is a lowercase English letter.It is guaranteed that the total number of characters in the strings over all test cases does not exceed \(2 \cdot 10^5\).
For each test case, print ""YES"" if you can obtain the string \(t\) by typing the string \(s\) and replacing some characters with presses of ""Backspace"" button, or ""NO"" if you cannot.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).
Consider the example test from the statement.In order to obtain ""ba"" from ""ababa"", you may press Backspace instead of typing the first and the fourth characters.There's no way to obtain ""bb"" while typing ""ababa"".There's no way to obtain ""aaaa"" while typing ""aaa"".In order to obtain ""ababa"" while typing ""aababa"", you have to press Backspace instead of typing the first character, then type all the remaining characters.
Input: 4 ababa ba ababa bb aaa aaaa aababa ababa | Output: YES NO NO YES
Medium
4
1,013
497
341
15
207
A3
207A3
A3. Beaver's Calculator 1.0
2,000
greedy
The Smart Beaver from ABBYY has once again surprised us! He has developed a new calculating device, which he called the ""Beaver's Calculator 1.0"". It is very peculiar and it is planned to be used in a variety of scientific problems.To test it, the Smart Beaver invited n scientists, numbered from 1 to n. The i-th scientist brought ki calculating problems for the device developed by the Smart Beaver from ABBYY. The problems of the i-th scientist are numbered from 1 to ki, and they must be calculated sequentially in the described order, since calculating each problem heavily depends on the results of calculating of the previous ones.Each problem of each of the n scientists is described by one integer ai, j, where i (1 ≀ i ≀ n) is the number of the scientist, j (1 ≀ j ≀ ki) is the number of the problem, and ai, j is the number of resource units the calculating device needs to solve this problem.The calculating device that is developed by the Smart Beaver is pretty unusual. It solves problems sequentially, one after another. After some problem is solved and before the next one is considered, the calculating device allocates or frees resources.The most expensive operation for the calculating device is freeing resources, which works much slower than allocating them. It is therefore desirable that each next problem for the calculating device requires no less resources than the previous one.You are given the information about the problems the scientists offered for the testing. You need to arrange these problems in such an order that the number of adjacent ""bad"" pairs of problems in this list is minimum possible. We will call two consecutive problems in this list a ""bad pair"" if the problem that is performed first requires more resources than the one that goes after it. Do not forget that the problems of the same scientist must be solved in a fixed order.
The first line contains integer n β€” the number of scientists. To lessen the size of the input, each of the next n lines contains five integers ki, ai, 1, xi, yi, mi (0 ≀ ai, 1 < mi ≀ 109, 1 ≀ xi, yi ≀ 109) β€” the number of problems of the i-th scientist, the resources the first problem requires and three parameters that generate the subsequent values of ai, j. For all j from 2 to ki, inclusive, you should calculate value ai, j by formula ai, j = (ai, j - 1 * xi + yi) mod mi, where a mod b is the operation of taking the remainder of division of number a by number b.To get the full points for the first group of tests it is sufficient to solve the problem with n = 2, 1 ≀ ki ≀ 2000.To get the full points for the second group of tests it is sufficient to solve the problem with n = 2, 1 ≀ ki ≀ 200000.To get the full points for the third group of tests it is sufficient to solve the problem with 1 ≀ n ≀ 5000, 1 ≀ ki ≀ 5000.
On the first line print a single number β€” the number of ""bad"" pairs in the optimal order.If the total number of problems does not exceed 200000, also print lines β€” the optimal order of the problems. On each of these lines print two integers separated by a single space β€” the required number of resources for the problem and the number of the scientist who offered this problem, respectively. The scientists are numbered from 1 to n in the order of input.
In the first sample n = 2, k1 = 2, a1, 1 = 1, a1, 2 = 2, k2 = 2, a2, 1 = 3, a2, 2 = 4. We've got two scientists, each of them has two calculating problems. The problems of the first scientist require 1 and 2 resource units, the problems of the second one require 3 and 4 resource units. Let's list all possible variants of the calculating order (each problem is characterized only by the number of resource units it requires): (1, 2, 3, 4), (1, 3, 2, 4), (3, 1, 2, 4), (1, 3, 4, 2), (3, 4, 1, 2), (3, 1, 4, 2).Sequence of problems (1, 3, 2, 4) has one ""bad"" pair (3 and 2), (3, 1, 4, 2) has two ""bad"" pairs (3 and 1, 4 and 2), and (1, 2, 3, 4) has no ""bad"" pairs.
Input: 22 1 1 1 102 3 1 1 10 | Output: 01 12 13 24 2
Hard
1
1,884
928
456
2
701
A
701A
A. Cards
800
greedy; implementation
There are n cards (n is even) in the deck. Each card has a positive integer written on it. n / 2 people will play new card game. At the beginning of the game each player gets two cards, each card is given to exactly one player. Find the way to distribute cards such that the sum of values written of the cards will be equal for each player. It is guaranteed that it is always possible.
The first line of the input contains integer n (2 ≀ n ≀ 100) β€” the number of cards in the deck. It is guaranteed that n is even.The second line contains the sequence of n positive integers a1, a2, ..., an (1 ≀ ai ≀ 100), where ai is equal to the number written on the i-th card.
Print n / 2 pairs of integers, the i-th pair denote the cards that should be given to the i-th player. Each card should be given to exactly one player. Cards are numbered in the order they appear in the input.It is guaranteed that solution exists. If there are several correct answers, you are allowed to print any of them.
In the first sample, cards are distributed in such a way that each player has the sum of numbers written on his cards equal to 8. In the second sample, all values ai are equal. Thus, any distribution is acceptable.
Input: 61 5 7 4 4 3 | Output: 1 36 24 5
Beginner
2
385
278
323
7
73
E
73E
E. Morrowindows
2,400
math; number theory
Vasya plays The Elder Trolls III: Morrowindows. He has a huge list of items in the inventory, however, there is no limits on the size of things. Vasya does not know the total amount of items but he is sure that are not more than x and not less than 2 items in his inventory. A new patch for the game appeared to view inventory in n different modes. Displaying in mode i is a partition of all inventory items on pages, each of which (except for maybe the last one) shows exactly ai items. In addition, each mode shows how many pages bi is in a complete list. Great! Perhaps this information will be enough for Vasya to find the required number. Moreover, it is very interesting, what is the fewest number of modes in which Vasya can see inventory to determine the number of items in it?Vasya cannot use the information that was received while looking on inventory in some mode for selection of next actions. I. e. Vasya chooses some set of modes first, and then sees all the results and determines the size.Knowing the number of ai, x and assuming that Vasya is very smart, check whether he can uniquely determine the number of items in his inventory, and how many modes he will need to do that if he knows numbers ai, x and he is able to know number bi after viewing items in mode i.
The first line contains two integers n and x (0 ≀ n ≀ 105, 2 ≀ x ≀ 109). The second line contains integers ai (1 ≀ ai ≀ 109). Some numbers among all ai may be equal.
Output the fewest amount of modes required to uniquely determine amount of items in the inventory. If there is no solution output - 1.
In the second example Vasya is not able to determine items count uniquely because 3 items, as well as 4 items, can be displayed on two pages.
Input: 2 42 3 | Output: 2
Expert
2
1,283
165
134
0
269
C
269C
C. Flawed Flow
2,100
constructive algorithms; flows; graphs; greedy
Emuskald considers himself a master of flow algorithms. Now he has completed his most ingenious program yet β€” it calculates the maximum flow in an undirected graph. The graph consists of n vertices and m edges. Vertices are numbered from 1 to n. Vertices 1 and n being the source and the sink respectively.However, his max-flow algorithm seems to have a little flaw β€” it only finds the flow volume for each edge, but not its direction. Help him find for each edge the direction of the flow through this edges. Note, that the resulting flow should be correct maximum flow.More formally. You are given an undirected graph. For each it's undirected edge (ai, bi) you are given the flow volume ci. You should direct all edges in such way that the following conditions hold: for each vertex v (1 < v < n), sum of ci of incoming edges is equal to the sum of ci of outcoming edges; vertex with number 1 has no incoming edges; the obtained directed graph does not have cycles.
The first line of input contains two space-separated integers n and m (2 ≀ n ≀ 2Β·105, n - 1 ≀ m ≀ 2Β·105), the number of vertices and edges in the graph. The following m lines contain three space-separated integers ai, bi and ci (1 ≀ ai, bi ≀ n, ai β‰  bi, 1 ≀ ci ≀ 104), which means that there is an undirected edge from ai to bi with flow volume ci.It is guaranteed that there are no two edges connecting the same vertices; the given graph is connected; a solution always exists.
Output m lines, each containing one integer di, which should be 0 if the direction of the i-th edge is ai β†’ bi (the flow goes from vertex ai to vertex bi) and should be 1 otherwise. The edges are numbered from 1 to m in the order they are given in the input.If there are several solutions you can print any of them.
In the first test case, 10 flow units pass through path , and 5 flow units pass directly from source to sink: .
Input: 3 33 2 101 2 103 1 5 | Output: 101
Hard
4
968
478
315
2
248
A
248A
A. Cupboards
800
implementation
One foggy Stockholm morning, Karlsson decided to snack on some jam in his friend Lillebror Svantenson's house. Fortunately for Karlsson, there wasn't anybody in his friend's house. Karlsson was not going to be hungry any longer, so he decided to get some food in the house.Karlsson's gaze immediately fell on n wooden cupboards, standing in the kitchen. He immediately realized that these cupboards have hidden jam stocks. Karlsson began to fly greedily around the kitchen, opening and closing the cupboards' doors, grab and empty all the jars of jam that he could find.And now all jars of jam are empty, Karlsson has had enough and does not want to leave traces of his stay, so as not to let down his friend. Each of the cupboards has two doors: the left one and the right one. Karlsson remembers that when he rushed to the kitchen, all the cupboards' left doors were in the same position (open or closed), similarly, all the cupboards' right doors were in the same position (open or closed). Karlsson wants the doors to meet this condition as well by the time the family returns. Karlsson does not remember the position of all the left doors, also, he cannot remember the position of all the right doors. Therefore, it does not matter to him in what position will be all left or right doors. It is important to leave all the left doors in the same position, and all the right doors in the same position. For example, all the left doors may be closed, and all the right ones may be open.Karlsson needs one second to open or close a door of a cupboard. He understands that he has very little time before the family returns, so he wants to know the minimum number of seconds t, in which he is able to bring all the cupboard doors in the required position.Your task is to write a program that will determine the required number of seconds t.
The first input line contains a single integer n β€” the number of cupboards in the kitchen (2 ≀ n ≀ 104). Then follow n lines, each containing two integers li and ri (0 ≀ li, ri ≀ 1). Number li equals one, if the left door of the i-th cupboard is opened, otherwise number li equals zero. Similarly, number ri equals one, if the right door of the i-th cupboard is opened, otherwise number ri equals zero.The numbers in the lines are separated by single spaces.
In the only output line print a single integer t β€” the minimum number of seconds Karlsson needs to change the doors of all cupboards to the position he needs.
Input: 50 11 00 11 10 1 | Output: 3
Beginner
1
1,839
458
158
2
959
E
959E
E. Mahmoud and Ehab and the xor-MST
1,900
bitmasks; dp; graphs; implementation; math
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≀ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight (where is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph?You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graphYou can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_treeThe weight of the minimum spanning tree is the sum of the weights on the edges included in it.
The only line contains an integer n (2 ≀ n ≀ 1012), the number of vertices in the graph.
The only line contains an integer x, the weight of the graph's minimum spanning tree.
In the first sample: The weight of the minimum spanning tree is 1+2+1=4.
Input: 4 | Output: 4
Hard
5
673
88
85
9
230
B
230B
B. T-primes
1,300
binary search; implementation; math; number theory
We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Π’-prime, if t has exactly three distinct positive divisors.You are given an array of n positive integers. For each of them determine whether it is Π’-prime or not.
The first line contains a single positive integer, n (1 ≀ n ≀ 105), showing how many numbers are in the array. The next line contains n space-separated integers xi (1 ≀ xi ≀ 1012).Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is advised to use the cin, cout streams or the %I64d specifier.
Print n lines: the i-th line should contain ""YES"" (without the quotes), if number xi is Π’-prime, and ""NO"" (without the quotes), if it isn't.
The given test has three numbers. The first number 4 has exactly three divisors β€” 1, 2 and 4, thus the answer for this number is ""YES"". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is ""NO"".
Input: 34 5 6 | Output: YESNONO
Easy
4
304
325
144
2
1,943
A
1943A
A. MEX Game 1
1,300
games; greedy
Alice and Bob play yet another game on an array \(a\) of size \(n\). Alice starts with an empty array \(c\). Both players take turns playing, with Alice starting first.On Alice's turn, she picks one element from \(a\), appends that element to \(c\), and then deletes it from \(a\).On Bob's turn, he picks one element from \(a\), and then deletes it from \(a\). The game ends when the array \(a\) is empty. Game's score is defined to be the MEX\(^\dagger\) of \(c\). Alice wants to maximize the score while Bob wants to minimize it. Find game's final score if both players play optimally.\(^\dagger\) The \(\operatorname{MEX}\) (minimum excludant) of an array of integers is defined as the smallest non-negative integer which does not occur in the array. For example: The MEX of \([2,2,1]\) is \(0\), because \(0\) does not belong to the array. The MEX of \([3,1,0,1]\) is \(2\), because \(0\) and \(1\) belong to the array, but \(2\) does not. The MEX of \([0,3,1,2]\) is \(4\), because \(0\), \(1\), \(2\) and \(3\) belong to the array, but \(4\) does not.
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 2 \cdot 10^4\)) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i < n\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
For each test case, find game's score if both players play optimally.
In the first test case, a possible game with a score of \(2\) is as follows: Alice chooses the element \(1\). After this move, \(a=[0,0,1]\) and \(c=[1]\). Bob chooses the element \(0\). After this move, \(a=[0,1]\) and \(c=[1]\). Alice chooses the element \(0\). After this move, \(a=[1]\) and \(c=[1,0]\). Bob chooses the element \(1\). After this move, \(a=[\,]\) and \(c=[1,0]\). At the end, \(c=[1,0]\), which has a MEX of \(2\). Note that this is an example game and does not necessarily represent the optimal strategy for both players.
Input: 340 0 1 140 1 2 321 1 | Output: 2 1 0
Easy
2
1,057
482
69
19
103
A
103A
A. Testing Pants for Sadness
1,100
greedy; implementation; math
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called ""Testing Pants for Sadness"".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
The first line contains a positive integer n (1 ≀ n ≀ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≀ ai ≀ 109), the number of answer variants to question i.
Print a single number β€” the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator.
Note to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished.
Input: 21 1 | Output: 2
Easy
3
1,058
227
257
1
1,607
E
1607E
E. Robot on the Board 1
1,600
implementation
The robot is located on a checkered rectangular board of size \(n \times m\) (\(n\) rows, \(m\) columns). The rows in the board are numbered from \(1\) to \(n\) from top to bottom, and the columns β€” from \(1\) to \(m\) from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.The sequence of commands \(s\) executed by the robot is given. Each command is denoted by one of the symbols 'L', 'R', 'D' or 'U', and triggers the movement to left, right, down or up, respectively.The robot can start its movement in any cell. The robot executes the commands starting from the first one, strictly in the order in which they are listed in \(s\). If the robot moves beyond the edge of the board, it falls and breaks. A command that causes the robot to break is not considered successfully executed.The robot's task is to execute as many commands as possible without falling off the board. For example, on board \(3 \times 3\), if the robot starts a sequence of actions \(s=\)""RRDLUU"" (""right"", ""right"", ""down"", ""left"", ""up"", ""up"") from the central cell, the robot will perform one command, then the next command will force him to cross the edge. If the robot starts moving from the cell \((2, 1)\) (second row, first column) then all commands will be executed successfully and the robot will stop at the cell \((1, 2)\) (first row, second column). The robot starts from cell \((2, 1)\) (second row, first column). It moves right, right, down, left, up, and up. In this case it ends in the cell \((1, 2)\) (first row, second column). Determine the cell from which the robot should start its movement in order to execute as many commands as possible.
The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β€” the number of test cases.The next \(2t\) lines contain descriptions of the test cases.In the description of each test case, the first line contains two integers \(n\) and \(m\) (\(1 \leq n, m \leq 10^6\)) β€” the height and width of the field that the robot is located on. The second line of the description is a string \(s\) consisting solely of characters 'L', 'R', 'D' and 'U' β€” the sequence of commands the robot executes. The string has a length from \(1\) to \(10^6\) commands.It is guaranteed that the total length of \(s\) over all test cases does not exceed \(10^6\).
Print \(t\) lines, each of which contains the answer to the corresponding test case. The answer to the test case are two integers \(r\) (\(1 \leq r \leq n\)) and \(c\) (\(1 \leq c \leq m\)), separated by a space β€” the coordinates of the cell (row number and column number) from which the robot should start moving to perform as many commands as possible.If there are several such cells, you may output any of them.
Input: 4 1 1 L 1 2 L 3 3 RRDLUU 4 3 LUURRDDLLLUU | Output: 1 1 1 2 2 1 3 2
Medium
1
1,705
642
414
16
2,043
G
2043G
G. Problem with Queries
3,000
brute force; data structures; implementation
You are given an array \(a\), consisting of \(n\) integers. Your task is to process \(q\) queries of two types: \(1~p~x\) β€” set the value of the element at index \(p\) equal to \(x\); \(2~l~r\) β€” count the number of pairs of indices \((i, j)\) such that \(l \le i < j \le r\) and \(a_i \ne a_j\). Note that the queries in this task are encoded; each subsequent query can only be decoded after calculating the answer to the preceding query of the second type.
The first line contains a single integer \(n\) (\(1 \le n \le 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le n\)).The third line contains a single integer \(q\) (\(1 \le q \le 3 \cdot 10^5\)) β€” the number of queries.The next \(q\) lines describe the queries in one of the following formats: \(1~p'~x'\) (\(0 \le p', x' \le n-1\)); \(2~l'~r'\) (\(0 \le l', r' \le n-1\)). The queries are encoded as follows: let \(\mathit{last}\) be the answer to the latest processed query of the second type (initially, \(\mathit{last} = 0\)). if the type of the query is \(1\), then \(p = ((p' + \mathit{last}) \bmod n) + 1\), \(x = ((x' + \mathit{last}) \bmod n) + 1\). if the type of the query is \(2\), \(l = ((l' + \mathit{last}) \bmod n) + 1\), \(r = ((r' + \mathit{last}) \bmod n) + 1\). If \(l > r\), swap their values. Don't forget to update the value of \(\mathit{last}\) after answering each query of the second type.Additional constraint on the input: there is at least one query of the second type.
For each query of the second type, print the answer β€” the number of pairs of indices \((i, j)\) such that \(l \le i < j \le r\) and \(a_i \ne a_j\).
In the first example, the actual queries (after decoding) are: 2 1 3 1 1 3 2 1 3 1 2 3 2 1 3
Input: 31 2 352 0 21 0 22 0 21 2 02 1 0 | Output: 3 2 0
Master
3
458
1,038
148
20
229
A
229A
A. Shifts
1,500
brute force; two pointers
You are given a table consisting of n rows and m columns. Each cell of the table contains a number, 0 or 1. In one move we can choose some row of the table and cyclically shift its values either one cell to the left, or one cell to the right.To cyclically shift a table row one cell to the right means to move the value of each cell, except for the last one, to the right neighboring cell, and to move the value of the last cell to the first cell. A cyclical shift of a row to the left is performed similarly, but in the other direction. For example, if we cyclically shift a row ""00110"" one cell to the right, we get a row ""00011"", but if we shift a row ""00110"" one cell to the left, we get a row ""01100"".Determine the minimum number of moves needed to make some table column consist only of numbers 1.
The first line contains two space-separated integers: n (1 ≀ n ≀ 100) β€” the number of rows in the table and m (1 ≀ m ≀ 104) β€” the number of columns in the table. Then n lines follow, each of them contains m characters ""0"" or ""1"": the j-th character of the i-th line describes the contents of the cell in the i-th row and in the j-th column of the table.It is guaranteed that the description of the table contains no other characters besides ""0"" and ""1"".
Print a single number: the minimum number of moves needed to get only numbers 1 in some column of the table. If this is impossible, print -1.
In the first sample one way to achieve the goal with the least number of moves is as follows: cyclically shift the second row to the right once, then shift the third row to the left twice. Then the table column before the last one will contain only 1s.In the second sample one can't shift the rows to get a column containing only 1s.
Input: 3 6101010000100100000 | Output: 3
Medium
2
811
461
141
2
1,301
A
1301A
A. Three Strings
800
implementation; strings
You are given three strings \(a\), \(b\) and \(c\) of the same length \(n\). The strings consist of lowercase English letters only. The \(i\)-th letter of \(a\) is \(a_i\), the \(i\)-th letter of \(b\) is \(b_i\), the \(i\)-th letter of \(c\) is \(c_i\).For every \(i\) (\(1 \leq i \leq n\)) you must swap (i.e. exchange) \(c_i\) with either \(a_i\) or \(b_i\). So in total you'll perform exactly \(n\) swap operations, each of them either \(c_i \leftrightarrow a_i\) or \(c_i \leftrightarrow b_i\) (\(i\) iterates over all integers between \(1\) and \(n\), inclusive).For example, if \(a\) is ""code"", \(b\) is ""true"", and \(c\) is ""help"", you can make \(c\) equal to ""crue"" taking the \(1\)-st and the \(4\)-th letters from \(a\) and the others from \(b\). In this way \(a\) becomes ""hodp"" and \(b\) becomes ""tele"".Is it possible that after these swaps the string \(a\) becomes exactly the same as the string \(b\)?
The input consists of multiple test cases. 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 first line of each test case contains a string of lowercase English letters \(a\).The second line of each test case contains a string of lowercase English letters \(b\).The third line of each test case contains a string of lowercase English letters \(c\).It is guaranteed that in each test case these three strings are non-empty and have the same length, which is not exceeding \(100\).
Print \(t\) lines with answers for all test cases. For each test case:If it is possible to make string \(a\) equal to string \(b\) print ""YES"" (without quotes), otherwise print ""NO"" (without quotes).You can print either lowercase or uppercase letters in the answers.
In the first test case, it is impossible to do the swaps so that string \(a\) becomes exactly the same as string \(b\).In the second test case, you should swap \(c_i\) with \(a_i\) for all possible \(i\). After the swaps \(a\) becomes ""bca"", \(b\) becomes ""bca"" and \(c\) becomes ""abc"". Here the strings \(a\) and \(b\) are equal.In the third test case, you should swap \(c_1\) with \(a_1\), \(c_2\) with \(b_2\), \(c_3\) with \(b_3\) and \(c_4\) with \(a_4\). Then string \(a\) becomes ""baba"", string \(b\) becomes ""baba"" and string \(c\) becomes ""abab"". Here the strings \(a\) and \(b\) are equal.In the fourth test case, it is impossible to do the swaps so that string \(a\) becomes exactly the same as string \(b\).
Input: 4 aaa bbb ccc abc bca bca aabb bbaa baba imi mii iim | Output: NO YES YES NO
Beginner
2
928
574
270
13
1,290
B
1290B
B. Irreducible Anagrams
1,800
binary search; constructive algorithms; data structures; strings; two pointers
Let's call two strings \(s\) and \(t\) anagrams of each other if it is possible to rearrange symbols in the string \(s\) to get a string, equal to \(t\).Let's consider two strings \(s\) and \(t\) which are anagrams of each other. We say that \(t\) is a reducible anagram of \(s\) if there exists an integer \(k \ge 2\) and \(2k\) non-empty strings \(s_1, t_1, s_2, t_2, \dots, s_k, t_k\) that satisfy the following conditions: If we write the strings \(s_1, s_2, \dots, s_k\) in order, the resulting string will be equal to \(s\); If we write the strings \(t_1, t_2, \dots, t_k\) in order, the resulting string will be equal to \(t\); For all integers \(i\) between \(1\) and \(k\) inclusive, \(s_i\) and \(t_i\) are anagrams of each other. If such strings don't exist, then \(t\) is said to be an irreducible anagram of \(s\). Note that these notions are only defined when \(s\) and \(t\) are anagrams of each other.For example, consider the string \(s = \) ""gamegame"". Then the string \(t = \) ""megamage"" is a reducible anagram of \(s\), we may choose for example \(s_1 = \) ""game"", \(s_2 = \) ""gam"", \(s_3 = \) ""e"" and \(t_1 = \) ""mega"", \(t_2 = \) ""mag"", \(t_3 = \) ""e"": On the other hand, we can prove that \(t = \) ""memegaga"" is an irreducible anagram of \(s\).You will be given a string \(s\) and \(q\) queries, represented by two integers \(1 \le l \le r \le |s|\) (where \(|s|\) is equal to the length of the string \(s\)). For each query, you should find if the substring of \(s\) formed by characters from the \(l\)-th to the \(r\)-th has at least one irreducible anagram.
The first line contains a string \(s\), consisting of lowercase English characters (\(1 \le |s| \le 2 \cdot 10^5\)).The second line contains a single integer \(q\) (\(1 \le q \le 10^5\)) β€” the number of queries.Each of the following \(q\) lines contain two integers \(l\) and \(r\) (\(1 \le l \le r \le |s|\)), representing a query for the substring of \(s\) formed by characters from the \(l\)-th to the \(r\)-th.
For each query, print a single line containing ""Yes"" (without quotes) if the corresponding substring has at least one irreducible anagram, and a single line containing ""No"" (without quotes) otherwise.
In the first sample, in the first and third queries, the substring is ""a"", which has itself as an irreducible anagram since two or more non-empty strings cannot be put together to obtain ""a"". On the other hand, in the second query, the substring is ""aaa"", which has no irreducible anagrams: its only anagram is itself, and we may choose \(s_1 = \) ""a"", \(s_2 = \) ""aa"", \(t_1 = \) ""a"", \(t_2 = \) ""aa"" to show that it is a reducible anagram.In the second query of the second sample, the substring is ""abb"", which has, for example, ""bba"" as an irreducible anagram.
Input: aaaaa 3 1 1 2 4 5 5 | Output: Yes No Yes
Medium
5
1,601
414
204
12
1,242
C
1242C
C. Sum Balance
2,400
bitmasks; dfs and similar; dp; graphs
Ujan has a lot of numbers in his boxes. He likes order and balance, so he decided to reorder the numbers.There are \(k\) boxes numbered from \(1\) to \(k\). The \(i\)-th box contains \(n_i\) integer numbers. The integers can be negative. All of the integers are distinct. Ujan is lazy, so he will do the following reordering of the numbers exactly once. He will pick a single integer from each of the boxes, \(k\) integers in total. Then he will insert the chosen numbers β€” one integer in each of the boxes, so that the number of integers in each box is the same as in the beginning. Note that he may also insert an integer he picked from a box back into the same box.Ujan will be happy if the sum of the integers in each box is the same. Can he achieve this and make the boxes perfectly balanced, like all things should be?
The first line contains a single integer \(k\) (\(1 \leq k \leq 15\)), the number of boxes. The \(i\)-th of the next \(k\) lines first contains a single integer \(n_i\) (\(1 \leq n_i \leq 5\,000\)), the number of integers in box \(i\). Then the same line contains \(n_i\) integers \(a_{i,1}, \ldots, a_{i,n_i}\) (\(|a_{i,j}| \leq 10^9\)), the integers in the \(i\)-th box. It is guaranteed that all \(a_{i,j}\) are distinct.
If Ujan cannot achieve his goal, output ""No"" in a single line. Otherwise in the first line output ""Yes"", and then output \(k\) lines. The \(i\)-th of these lines should contain two integers \(c_i\) and \(p_i\). This means that Ujan should pick the integer \(c_i\) from the \(i\)-th box and place it in the \(p_i\)-th box afterwards.If there are multiple solutions, output any of those.You can print each letter in any case (upper or lower).
In the first sample, Ujan can put the number \(7\) in the \(2\)nd box, the number \(2\) in the \(3\)rd box, the number \(5\) in the \(1\)st box and keep the number \(10\) in the same \(4\)th box. Then the boxes will contain numbers \(\{1,5,4\}\), \(\{3, 7\}\), \(\{8,2\}\) and \(\{10\}\). The sum in each box then is equal to \(10\).In the second sample, it is not possible to pick and redistribute the numbers in the required way.In the third sample, one can swap the numbers \(-20\) and \(-10\), making the sum in each box equal to \(-10\).
Input: 4 3 1 7 4 2 3 2 2 8 5 1 10 | Output: Yes 7 2 2 3 5 1 10 4
Expert
4
824
424
444
12
1,594
A
1594A
A. Consecutive Sum Riddle
800
math
Theofanis has a riddle for you and if you manage to solve it, he will give you a Cypriot snack halloumi for free (Cypriot cheese).You are given an integer \(n\). You need to find two integers \(l\) and \(r\) such that \(-10^{18} \le l < r \le 10^{18}\) and \(l + (l + 1) + \ldots + (r - 1) + r = n\).
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The first and only line of each test case contains a single integer \(n\) (\(1 \le n \le 10^{18}\)).
For each test case, print the two integers \(l\) and \(r\) such that \(-10^{18} \le l < r \le 10^{18}\) and \(l + (l + 1) + \ldots + (r - 1) + r = n\). It can be proven that an answer always exists. If there are multiple answers, print any.
In the first test case, \(0 + 1 = 1\).In the second test case, \((-1) + 0 + 1 + 2 = 2\).In the fourth test case, \(1 + 2 + 3 = 6\).In the fifth test case, \(18 + 19 + 20 + 21 + 22 = 100\).In the sixth test case, \((-2) + (-1) + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 = 25\).
Input: 7 1 2 3 6 100 25 3000000000000 | Output: 0 1 -1 2 1 2 1 3 18 22 -2 7 999999999999 1000000000001
Beginner
1
300
197
240
15
864
B
864B
B. Polycarp and Letters
1,000
brute force; implementation; strings
Polycarp loves lowercase letters and dislikes uppercase ones. Once he got a string s consisting only of lowercase and uppercase Latin letters.Let A be a set of positions in the string. Let's call it pretty if following conditions are met: letters on positions from A in the string are all distinct and lowercase; there are no uppercase letters in the string which are situated between positions from A (i.e. there is no such j that s[j] is an uppercase letter, and a1 < j < a2 for some a1 and a2 from A). Write a program that will determine the maximum number of elements in a pretty set of positions.
The first line contains a single integer n (1 ≀ n ≀ 200) β€” length of string s.The second line contains a string s consisting of lowercase and uppercase Latin letters.
Print maximum number of elements in pretty set of positions for string s.
In the first example the desired positions might be 6 and 8 or 7 and 8. Positions 6 and 7 contain letters 'a', position 8 contains letter 'b'. The pair of positions 1 and 8 is not suitable because there is an uppercase letter 'B' between these position.In the second example desired positions can be 7, 8 and 11. There are other ways to choose pretty set consisting of three elements.In the third example the given string s does not contain any lowercase letters, so the answer is 0.
Input: 11aaaaBaabAbA | Output: 2
Beginner
3
601
166
73
8
44
C
44C
C. Holidays
1,300
implementation
School holidays come in Berland. The holidays are going to continue for n days. The students of school β„–N are having the time of their lives and the IT teacher Marina Sergeyevna, who has spent all the summer busy checking the BSE (Berland State Examination) results, has finally taken a vacation break! Some people are in charge of the daily watering of flowers in shifts according to the schedule. However when Marina Sergeyevna was making the schedule, she was so tired from work and so lost in dreams of the oncoming vacation that she perhaps made several mistakes. In fact, it is possible that according to the schedule, on some days during the holidays the flowers will not be watered or will be watered multiple times. Help Marina Sergeyevna to find a mistake.
The first input line contains two numbers n and m (1 ≀ n, m ≀ 100) β€” the number of days in Berland holidays and the number of people in charge of the watering respectively. The next m lines contain the description of the duty schedule. Each line contains two integers ai and bi (1 ≀ ai ≀ bi ≀ n), meaning that the i-th person in charge should water the flowers from the ai-th to the bi-th day inclusively, once a day. The duty shifts are described sequentially, i.e. bi ≀ ai + 1 for all i from 1 to n - 1 inclusively.
Print ""OK"" (without quotes), if the schedule does not contain mistakes. Otherwise you have to find the minimal number of a day when the flowers will not be watered or will be watered multiple times, and output two integers β€” the day number and the number of times the flowers will be watered that day.
Keep in mind that in the second sample the mistake occurs not only on the second day, but also on the sixth day, when nobody waters the flowers. However, you have to print the second day, i.e. the day with the minimal number.
Input: 10 51 23 34 67 78 10 | Output: OK
Easy
1
766
517
303
0
35
C
35C
C. Fire Again
1,500
brute force; dfs and similar; shortest paths
After a terrifying forest fire in Berland a forest rebirth program was carried out. Due to it N rows with M trees each were planted and the rows were so neat that one could map it on a system of coordinates so that the j-th tree in the i-th row would have the coordinates of (i, j). However a terrible thing happened and the young forest caught fire. Now we must find the coordinates of the tree that will catch fire last to plan evacuation.The burning began in K points simultaneously, which means that initially K trees started to burn. Every minute the fire gets from the burning trees to the ones that aren’t burning and that the distance from them to the nearest burning tree equals to 1.Find the tree that will be the last to start burning. If there are several such trees, output any.
The first input line contains two integers N, M (1 ≀ N, M ≀ 2000) β€” the size of the forest. The trees were planted in all points of the (x, y) (1 ≀ x ≀ N, 1 ≀ y ≀ M) type, x and y are integers.The second line contains an integer K (1 ≀ K ≀ 10) β€” amount of trees, burning in the beginning. The third line contains K pairs of integers: x1, y1, x2, y2, ..., xk, yk (1 ≀ xi ≀ N, 1 ≀ yi ≀ M) β€” coordinates of the points from which the fire started. It is guaranteed that no two points coincide.
Output a line with two space-separated integers x and y β€” coordinates of the tree that will be the last one to start burning. If there are several such trees, output any.
Input: 3 312 2 | Output: 1 1
Medium
3
791
489
170
0
702
C
702C
C. Cellular Network
1,500
binary search; implementation; two pointers
You are given n points on the straight line β€” the positions (x-coordinates) of the cities and m points on the same line β€” the positions (x-coordinates) of the cellular towers. All towers work in the same way β€” they provide cellular network for all cities, which are located at the distance which is no more than r from this tower.Your task is to find minimal r that each city has been provided by cellular network, i.e. for each city there is at least one cellular tower at the distance which is no more than r.If r = 0 then a tower provides cellular network only for the point where it is located. One tower can provide cellular network for any number of cities, but all these cities must be at the distance which is no more than r from this tower.
The first line contains two positive integers n and m (1 ≀ n, m ≀ 105) β€” the number of cities and the number of cellular towers.The second line contains a sequence of n integers a1, a2, ..., an ( - 109 ≀ ai ≀ 109) β€” the coordinates of cities. It is allowed that there are any number of cities in the same point. All coordinates ai are given in non-decreasing order.The third line contains a sequence of m integers b1, b2, ..., bm ( - 109 ≀ bj ≀ 109) β€” the coordinates of cellular towers. It is allowed that there are any number of towers in the same point. All coordinates bj are given in non-decreasing order.
Print minimal r so that each city will be covered by cellular network.
Input: 3 2-2 2 4-3 0 | Output: 4
Medium
3
749
610
70
7
2,074
C
2074C
C. XOR and Triangle
1,100
bitmasks; brute force; geometry; greedy; probabilities
This time, the pink soldiers have given you an integer \(x\) (\(x \ge 2\)).Please determine if there exists a positive integer \(y\) that satisfies the following conditions. \(y\) is strictly less than \(x\). There exists a non-degenerate triangle\(^{\text{βˆ—}}\) with side lengths \(x\), \(y\), \(x \oplus y\). Here, \(\oplus\) denotes the bitwise XOR operation. Additionally, if there exists such an integer \(y\), output any.\(^{\text{βˆ—}}\)A triangle with side lengths \(a\), \(b\), \(c\) is non-degenerate when \(a+b > c\), \(a+c > b\), \(b+c > a\).
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 2000\)). The description of the test cases follows. The only line of each test case contains a single integer \(x\) (\(2 \le x \le 10^9\)).
For each test case, print one integer on a separate line. The integer you must output is as follows: If there exists an integer \(y\) satisfying the conditions, output the value of \(y\) (\(1 \le y < x\)); Otherwise, output \(-1\). If there exist multiple integers that satisfy the conditions, you may output any.
In the first test case, there exists a non-degenerate triangle with side lengths \(3\), \(5\), and \(3 \oplus 5 = 6\). Therefore, \(y=3\) is a valid answer.In the second test case, \(1\) is the only possible candidate for \(y\), but it cannot make a non-degenerate triangle. Therefore, the answer is \(-1\).
Input: 75263694420 | Output: 3 -1 5 -1 66 -1 320
Easy
5
552
249
313
20
1,044
D
1044D
D. Deduction Queries
2,400
data structures; dsu
There is an array \(a\) of \(2^{30}\) integers, indexed from \(0\) to \(2^{30}-1\). Initially, you know that \(0 \leq a_i < 2^{30}\) (\(0 \leq i < 2^{30}\)), but you do not know any of the values. Your task is to process queries of two types: 1 l r x: You are informed that the bitwise xor of the subarray \([l, r]\) (ends inclusive) is equal to \(x\). That is, \(a_l \oplus a_{l+1} \oplus \ldots \oplus a_{r-1} \oplus a_r = x\), where \(\oplus\) is the bitwise xor operator. In some cases, the received update contradicts past updates. In this case, you should ignore the contradicting update (the current update). 2 l r: You are asked to output the bitwise xor of the subarray \([l, r]\) (ends inclusive). If it is still impossible to know this value, considering all past updates, then output \(-1\).Note that the queries are encoded. That is, you need to write an online solution.
The first line contains a single integer \(q\) (\(1 \leq q \leq 2 \cdot 10^5\)) β€” the number of queries.Each of the next \(q\) lines describes a query. It contains one integer \(t\) (\(1 \leq t \leq 2\)) β€” the type of query.The given queries will be encoded in the following way: let \(last\) be the answer to the last query of the second type that you have answered (initially, \(last = 0\)). If the last answer was \(-1\), set \(last = 1\). If \(t = 1\), three integers follow, \(l'\), \(r'\), and \(x'\) (\(0 \leq l', r', x' < 2^{30}\)), meaning that you got an update. First, do the following: \(l = l' \oplus last\), \(r = r' \oplus last\), \(x = x' \oplus last\) and, if \(l > r\), swap \(l\) and \(r\).This means you got an update that the bitwise xor of the subarray \([l, r]\) is equal to \(x\) (notice that you need to ignore updates that contradict previous updates). If \(t = 2\), two integers follow, \(l'\) and \(r'\) (\(0 \leq l', r' < 2^{30}\)), meaning that you got a query. First, do the following: \(l = l' \oplus last\), \(r = r' \oplus last\) and, if \(l > r\), swap \(l\) and \(r\).For the given query, you need to print the bitwise xor of the subarray \([l, r]\). If it is impossible to know, print \(-1\). Don't forget to change the value of \(last\).It is guaranteed there will be at least one query of the second type.
After every query of the second type, output the bitwise xor of the given subarray or \(-1\) if it is still impossible to know.
In the first example, the real queries (without being encoded) are: 12 2 1 2 2 0 1073741823 1 1 2 5 2 1 1 2 2 2 2 1 2 1 2 3 6 2 1 1 1 1 3 0 2 1 1 2 2 2 2 3 3 The answers for the first two queries are \(-1\) because we don't have any such information on the array initially. The first update tells us \(a_1 \oplus a_2 = 5\). Note that we still can't be certain about the values \(a_1\) or \(a_2\) independently (for example, it could be that \(a_1 = 1, a_2 = 4\), and also \(a_1 = 3, a_2 = 6\)). After we receive all three updates, we have enough information to deduce \(a_1, a_2, a_3\) independently. In the second example, notice that after the first two updates we already know that \(a_5 \oplus a_6 = 12\), so the third update is contradicting, and we ignore it.
Input: 122 1 22 1 10737418221 0 3 42 0 02 3 32 0 31 6 7 32 4 41 0 2 12 0 02 4 42 0 0 | Output: -1-1-1-15-1635
Expert
2
884
1,344
127
10
1,398
B
1398B
B. Substring Removal Game
800
games; greedy; sortings
Alice and Bob play a game. They have a binary string \(s\) (a string such that each character in it is either \(0\) or \(1\)). Alice moves first, then Bob, then Alice again, and so on.During their move, the player can choose any number (not less than one) of consecutive equal characters in \(s\) and delete them.For example, if the string is \(10110\), there are \(6\) possible moves (deleted characters are bold): \(\textbf{1}0110 \to 0110\); \(1\textbf{0}110 \to 1110\); \(10\textbf{1}10 \to 1010\); \(101\textbf{1}0 \to 1010\); \(10\textbf{11}0 \to 100\); \(1011\textbf{0} \to 1011\). After the characters are removed, the characters to the left and to the right of the removed block become adjacent. I. e. the following sequence of moves is valid: \(10\textbf{11}0 \to 1\textbf{00} \to 1\).The game ends when the string becomes empty, and the score of each player is the number of \(1\)-characters deleted by them.Each player wants to maximize their score. Calculate the resulting score of Alice.
The first line contains one integer \(T\) (\(1 \le T \le 500\)) β€” the number of test cases.Each test case contains exactly one line containing a binary string \(s\) (\(1 \le |s| \le 100\)).
For each test case, print one integer β€” the resulting score of Alice (the number of \(1\)-characters deleted by her).
Questions about the optimal strategy will be ignored.
Input: 5 01111001 0000 111111 101010101 011011110111 | Output: 4 0 6 3 6
Beginner
3
1,001
189
117
13
934
A
934A
A. A Compatible Pair
1,400
brute force; games
Nian is a monster which lives deep in the oceans. Once a year, it shows up on the land, devouring livestock and even people. In order to keep the monster away, people fill their villages with red colour, light, and cracking noise, all of which frighten the monster out of coming.Little Tommy has n lanterns and Big Banban has m lanterns. Tommy's lanterns have brightness a1, a2, ..., an, and Banban's have brightness b1, b2, ..., bm respectively.Tommy intends to hide one of his lanterns, then Banban picks one of Tommy's non-hidden lanterns and one of his own lanterns to form a pair. The pair's brightness will be the product of the brightness of two lanterns.Tommy wants to make the product as small as possible, while Banban tries to make it as large as possible.You are asked to find the brightness of the chosen pair if both of them choose optimally.
The first line contains two space-separated integers n and m (2 ≀ n, m ≀ 50).The second line contains n space-separated integers a1, a2, ..., an.The third line contains m space-separated integers b1, b2, ..., bm.All the integers range from - 109 to 109.
Print a single integer β€” the brightness of the chosen pair.
In the first example, Tommy will hide 20 and Banban will choose 18 from Tommy and 14 from himself.In the second example, Tommy will hide 3 and Banban will choose 2 from Tommy and 1 from himself.
Input: 2 220 182 14 | Output: 252
Easy
2
856
253
59
9
567
C
567C
C. Geometric Progression
1,700
binary search; data structures; dp
Polycarp loves geometric progressions very much. Since he was only three years old, he loves only the progressions of length three. He also has a favorite integer k and a sequence a, consisting of n integers.He wants to know how many subsequences of length three can be selected from a, so that they form a geometric progression with common ratio k.A subsequence of length three is a combination of three such indexes i1, i2, i3, that 1 ≀ i1 < i2 < i3 ≀ n. That is, a subsequence of length three are such groups of three elements that are not necessarily consecutive in the sequence, but their indexes are strictly increasing.A geometric progression with common ratio k is a sequence of numbers of the form bΒ·k0, bΒ·k1, ..., bΒ·kr - 1.Polycarp is only three years old, so he can not calculate this number himself. Help him to do it.
The first line of the input contains two integers, n and k (1 ≀ n, k ≀ 2Β·105), showing how many numbers Polycarp's sequence has and his favorite number.The second line contains n integers a1, a2, ..., an ( - 109 ≀ ai ≀ 109) β€” elements of the sequence.
Output a single number β€” the number of ways to choose a subsequence of length three, such that it forms a geometric progression with a common ratio k.
In the first sample test the answer is four, as any of the two 1s can be chosen as the first element, the second element can be any of the 2s, and the third element of the subsequence must be equal to 4.
Input: 5 21 1 2 2 4 | Output: 4
Medium
3
830
251
150
5
1,505
H
1505H
H. L BREAK into program
2,500
*special
Hack the program and get the password hidden in it.
This program has only one test, and it's empty (it doesn't give your program anything to read).
Output the password recovered from the program. The password is case sensitive.
Expert
1
51
95
79
15
962
B
962B
B. Students in Railway Carriage
1,300
constructive algorithms; greedy; implementation
There are \(n\) consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger.The university team for the Olympiad consists of \(a\) student-programmers and \(b\) student-athletes. Determine the largest number of students from all \(a+b\) students, which you can put in the railway carriage so that: no student-programmer is sitting next to the student-programmer; and no student-athlete is sitting next to the student-athlete. In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting.Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all).
The first line contain three integers \(n\), \(a\) and \(b\) (\(1 \le n \le 2\cdot10^{5}\), \(0 \le a, b \le 2\cdot10^{5}\), \(a + b > 0\)) β€” total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes.The second line contains a string with length \(n\), consisting of characters ""."" and ""*"". The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member.
Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete.
In the first example you can put all student, for example, in the following way: *.AB*In the second example you can put four students, for example, in the following way: *BAB*BIn the third example you can put seven students, for example, in the following way: B*ABAB**A*BThe letter A means a student-programmer, and the letter B β€” student-athlete.
Input: 5 1 1*...* | Output: 2
Easy
3
721
491
210
9
1,868
A
1868A
A. Fill in the Matrix
1,300
constructive algorithms; implementation
There is an empty matrix \(M\) of size \(n\times m\).Zhongkao examination is over, and Daniel would like to do some puzzle games. He is going to fill in the matrix \(M\) using permutations of length \(m\). That is, each row of \(M\) must be a permutation of length \(m^\dagger\).Define the value of the \(i\)-th column in \(M\) as \(v_i=\operatorname{MEX}(M_{1,i},M_{2,i},\ldots,M_{n,i})^\ddagger\). Since Daniel likes diversity, the beauty of \(M\) is \(s=\operatorname{MEX}(v_1,v_2,\cdots,v_m)\).You have to help Daniel fill in the matrix \(M\) and maximize its beauty.\(^\dagger\) A permutation of length \(m\) is an array consisting of \(m\) distinct integers from \(0\) to \(m-1\) in arbitrary order. For example, \([1,2,0,4,3]\) is a permutation, but \([0,1,1]\) is not a permutation (\(1\) appears twice in the array), and \([0,1,3]\) is also not a permutation (\(m-1=2\) but there is \(3\) in the array).\(^\ddagger\) The \(\operatorname{MEX}\) of an array is the smallest non-negative integer that does not belong to the array. For example, \(\operatorname{MEX}(2,2,1)=0\) because \(0\) does not belong to the array, and \(\operatorname{MEX}(0,3,1,2)=4\) because \(0\), \(1\), \(2\) and \(3\) appear in the array, but \(4\) does not.
The first line of input contains a single integer \(t\) (\(1\le t\le 1000\)) β€” the number of test cases. The description of test cases follows.The only line of each test case contains two integers \(n\) and \(m\) (\(1\le n,m\le 2\cdot 10^5\)) β€” the size of the matrix.It is guaranteed that the sum of \(n\cdot m\) over all test cases does not exceed \(2\cdot 10^5\).
For each test case, in the first line output a single integer β€” the maximum beauty of \(M\).Then output the matrix \(M\) of size \(n\times m\) β€” the matrix you find.If there are multiple solutions, you may output any of them.
In the first test case: \(v_1=\operatorname{MEX}(1,0,1,0)=2\); \(v_2=\operatorname{MEX}(0,2,0,2)=1\); \(v_3=\operatorname{MEX}(2,1,2,1)=0\). Therefore, \(s=\operatorname{MEX}(2,1,0)=3\). It can be shown that \(3\) is the maximum possible beauty of \(M\).In the second test case, any permutation will make \(s=2\).In the third test case: \(v_1=\operatorname{MEX}(3,5,1,4,4,2)=0\); \(v_2=\operatorname{MEX}(0,2,3,1,2,4)=5\); \(v_3=\operatorname{MEX}(1,1,2,3,5,0)=4\); \(v_4=\operatorname{MEX}(4,0,4,2,3,5)=1\); \(v_5=\operatorname{MEX}(2,4,5,5,0,1)=3\); \(v_6=\operatorname{MEX}(5,3,0,0,1,3)=2\). Therefore, \(s=\operatorname{MEX}(0,5,4,1,3,2)=6\).
Input: 44 31 166 62 1 | Output: 3 1 0 2 0 2 1 1 0 2 0 2 1 2 14 7 15 4 10 0 8 6 1 2 3 5 9 11 12 13 6 3 0 1 4 2 5 5 2 1 0 4 3 1 3 2 4 5 0 4 1 3 2 5 0 4 2 5 3 0 1 2 4 0 5 1 3 0 0 0
Easy
2
1,242
366
225
18
2,128
B
2128B
B. Deque Process
1,100
constructive algorithms; greedy; sortings; two pointers
We say that an array \(a\) of size \(n\) is bad if and only if there exists \(1 \leq i \leq n - 4\) such that one of the following conditions holds: \(a_i < a_{i+1} < a_{i+2} < a_{i+3} < a_{i+4}\) \(a_i > a_{i+1} > a_{i+2} > a_{i+3} > a_{i+4}\) An array is good if and only if it's not bad. For example: \(a = [3, \color{red}{1, 2, 4, 5, 6}]\) is bad because \(a_2 < a_3 < a_4 < a_5 < a_6\). \(a = [\color{red}{7, 6, 5, 4, 1}, 2, 3]\) is bad because \(a_1 > a_2 > a_3 > a_4 > a_5\). \(a = [7, 6, 5, 1, 2, 3, 4]\) is good. You're given a permutation\(^{\text{βˆ—}}\) \(p_1, p_2, \ldots, p_n\).You must perform \(n\) turns. At each turn, you must remove either the leftmost or the rightmost remaining element in \(p\). Let \(q_i\) be the element removed at the \(i\)-th turn.Choose which element to remove at each turn so that the resulting array \(q\) is good. We can show that under the given constraints, it's always possible.\(^{\text{βˆ—}}\)A permutation of length \(n\) 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).
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10\,000\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(5 \leq n \leq 100\,000\)) β€” the length of the array.The second line of each test case contains \(n\) integers \(p_1, p_2, \ldots, p_n\) (\(1 \leq p_i \leq n\), \(p_i\) are pairwise distinct) β€” elements of the permutation.It is guaranteed that the sum of \(n\) over all test cases doesn't exceed \(200\,000\).
For each test case, you must output a string \(s\) of length \(n\). For every \(1 \leq i \leq n\), at the \(i\)-th turn: \(s_i = \texttt{L}\) means that you removed the leftmost element of \(p\) \(s_i = \texttt{R}\) means that you removed the rightmost element of \(p\) We can show that an answer always exists. If there are multiple solutions, print any of them.
In the first test case, the sequence \(\color{blue}{\texttt{RRR}}\color{red}{\texttt{LLLL}}\) results in \(q = [\color{blue}{7}, \color{blue}{6}, \color{blue}{5}, \color{red}{1}, \color{red}{2}, \color{red}{3}, \color{red}{4}]\).In the second test case, the sequence \(\color{red}{\texttt{LL}}\color{blue}{\texttt{RR}}\color{red}{\texttt{LL}}\color{blue}{\texttt{RR}}\color{red}{\texttt{L}}\) results in \(q = [\color{red}{1}, \color{red}{3}, \color{blue}{2}, \color{blue}{4}, \color{red}{6}, \color{red}{8}, \color{blue}{5}, \color{blue}{7}, \color{red}{9}]\).
Input: 671 2 3 4 5 6 791 3 6 8 9 7 5 4 2121 2 11 3 6 4 7 8 12 5 10 964 1 2 5 6 351 2 3 5 495 1 8 6 2 7 9 4 3 | Output: RRRLLLL LLRRLLRRL LLLLLLLLLLLL LLLLLL LLLLL LLLLLLLLL
Easy
4
1,264
541
363
21
30
D
30D
D. King's Problem?
2,600
geometry; greedy
Every true king during his life must conquer the world, hold the Codeforces world finals, win pink panda in the shooting gallery and travel all over his kingdom.King Copa has already done the first three things. Now he just needs to travel all over the kingdom. The kingdom is an infinite plane with Cartesian coordinate system on it. Every city is a point on this plane. There are n cities in the kingdom at points with coordinates (x1, 0), (x2, 0), ..., (xn, 0), and there is one city at point (xn + 1, yn + 1). King starts his journey in the city number k. Your task is to find such route for the king, which visits all cities (in any order) and has minimum possible length. It is allowed to visit a city twice. The king can end his journey in any city. Between any pair of cities there is a direct road with length equal to the distance between the corresponding points. No two cities may be located at the same point.
The first line contains two integers n and k (1 ≀ n ≀ 105, 1 ≀ k ≀ n + 1) β€” amount of cities and index of the starting city. The second line contains n + 1 numbers xi. The third line contains yn + 1. All coordinates are integers and do not exceed 106 by absolute value. No two cities coincide.
Output the minimum possible length of the journey. Your answer must have relative or absolute error less than 10 - 6.
Input: 3 10 1 2 11 | Output: 3.41421356237309490000
Expert
2
922
293
117
0
1,388
C
1388C
C. Uncle Bogdan and Country Happiness
1,800
dfs and similar; greedy; math; trees
Uncle Bogdan is in captain Flint's crew for a long time and sometimes gets nostalgic for his homeland. Today he told you how his country introduced a happiness index.There are \(n\) cities and \(nβˆ’1\) undirected roads connecting pairs of cities. Citizens of any city can reach any other city traveling by these roads. Cities are numbered from \(1\) to \(n\) and the city \(1\) is a capital. In other words, the country has a tree structure.There are \(m\) citizens living in the country. A \(p_i\) people live in the \(i\)-th city but all of them are working in the capital. At evening all citizens return to their home cities using the shortest paths. Every person has its own mood: somebody leaves his workplace in good mood but somebody are already in bad mood. Moreover any person can ruin his mood on the way to the hometown. If person is in bad mood he won't improve it.Happiness detectors are installed in each city to monitor the happiness of each person who visits the city. The detector in the \(i\)-th city calculates a happiness index \(h_i\) as the number of people in good mood minus the number of people in bad mood. Let's say for the simplicity that mood of a person doesn't change inside the city.Happiness detector is still in development, so there is a probability of a mistake in judging a person's happiness. One late evening, when all citizens successfully returned home, the government asked uncle Bogdan (the best programmer of the country) to check the correctness of the collected happiness indexes.Uncle Bogdan successfully solved the problem. Can you do the same?More formally, You need to check: ""Is it possible that, after all people return home, for each city \(i\) the happiness index will be equal exactly to \(h_i\)"".
The first line contains a single integer \(t\) (\(1 \le t \le 10000\)) β€” the number of test cases.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n \le 10^5\); \(0 \le m \le 10^9\)) β€” the number of cities and citizens.The second line of each test case contains \(n\) integers \(p_1, p_2, \ldots, p_{n}\) (\(0 \le p_i \le m\); \(p_1 + p_2 + \ldots + p_{n} = m\)), where \(p_i\) is the number of people living in the \(i\)-th city.The third line contains \(n\) integers \(h_1, h_2, \ldots, h_{n}\) (\(-10^9 \le h_i \le 10^9\)), where \(h_i\) is the calculated happiness index of the \(i\)-th city.Next \(n βˆ’ 1\) lines contain description of the roads, one per line. Each line contains two integers \(x_i\) and \(y_i\) (\(1 \le x_i, y_i \le n\); \(x_i \neq y_i\)), where \(x_i\) and \(y_i\) are cities connected by the \(i\)-th road.It's guaranteed that the sum of \(n\) from all test cases doesn't exceed \(2 \cdot 10^5\).
For each test case, print YES, if the collected data is correct, or NO β€” otherwise. You can print characters in YES or NO in any case.
Let's look at the first test case of the first sample: At first, all citizens are in the capital. Let's describe one of possible scenarios: a person from city \(1\): he lives in the capital and is in good mood; a person from city \(4\): he visited cities \(1\) and \(4\), his mood was ruined between cities \(1\) and \(4\); a person from city \(3\): he visited cities \(1\) and \(3\) in good mood; a person from city \(6\): he visited cities \(1\), \(3\) and \(6\), his mood was ruined between cities \(1\) and \(3\); In total, \(h_1 = 4 - 0 = 4\), \(h_2 = 0\), \(h_3 = 1 - 1 = 0\), \(h_4 = 0 - 1 = -1\), \(h_5 = 0\), \(h_6 = 0 - 1 = -1\), \(h_7 = 0\). The second case of the first test: All people have already started in bad mood in the capital β€” this is the only possible scenario.The first case of the second test: The second case of the second test: It can be proven that there is no way to achieve given happiness indexes in both cases of the second test.
Input: 2 7 4 1 0 1 1 0 1 0 4 0 0 -1 0 -1 0 1 2 1 3 1 4 3 5 3 6 3 7 5 11 1 2 5 2 1 -11 -2 -6 -2 -1 1 2 1 3 1 4 3 5 | Output: YES YES
Medium
4
1,753
952
134
13
1,245
F
1245F
F. Daniel and Spring Cleaning
2,300
bitmasks; brute force; combinatorics; dp
While doing some spring cleaning, Daniel found an old calculator that he loves so much. However, it seems like it is broken. When he tries to compute \(1 + 3\) using the calculator, he gets \(2\) instead of \(4\). But when he tries computing \(1 + 4\), he gets the correct answer, \(5\). Puzzled by this mystery, he opened up his calculator and found the answer to the riddle: the full adders became half adders! So, when he tries to compute the sum \(a + b\) using the calculator, he instead gets the xorsum \(a \oplus b\) (read the definition by the link: https://en.wikipedia.org/wiki/Exclusive_or).As he saw earlier, the calculator sometimes gives the correct answer. And so, he wonders, given integers \(l\) and \(r\), how many pairs of integers \((a, b)\) satisfy the following conditions: $$$\(a + b = a \oplus b\)\( \)\(l \leq a \leq r\)\( \)\(l \leq b \leq r\)$$$However, Daniel the Barman is going to the bar and will return in two hours. He tells you to solve the problem before he returns, or else you will have to enjoy being blocked.
The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β€” the number of testcases.Then, \(t\) lines follow, each containing two space-separated integers \(l\) and \(r\) (\(0 \le l \le r \le 10^9\)).
Print \(t\) integers, the \(i\)-th integer should be the answer to the \(i\)-th testcase.
\(a \oplus b\) denotes the bitwise XOR of \(a\) and \(b\).For the first testcase, the pairs are: \((1, 2)\), \((1, 4)\), \((2, 1)\), \((2, 4)\), \((3, 4)\), \((4, 1)\), \((4, 2)\), and \((4, 3)\).
Input: 3 1 4 323 323 1 1000000 | Output: 8 0 3439863766
Expert
4
1,047
211
89
12
1,710
B
1710B
B. Rain
2,100
binary search; brute force; data structures; geometry; greedy; implementation; math
You are the owner of a harvesting field which can be modeled as an infinite line, whose positions are identified by integers.It will rain for the next \(n\) days. On the \(i\)-th day, the rain will be centered at position \(x_i\) and it will have intensity \(p_i\). Due to these rains, some rainfall will accumulate; let \(a_j\) be the amount of rainfall accumulated at integer position \(j\). Initially \(a_j\) is \(0\), and it will increase by \(\max(0,p_i-|x_i-j|)\) after the \(i\)-th day's rain.A flood will hit your field if, at any moment, there is a position \(j\) with accumulated rainfall \(a_j>m\).You can use a magical spell to erase exactly one day's rain, i.e., setting \(p_i=0\). For each \(i\) from \(1\) to \(n\), check whether in case of erasing the \(i\)-th day's rain there is no flood.
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 10^4\)). The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \leq n \leq 2 \cdot 10^5\), \(1 \leq m \leq 10^9\)) β€” the number of rainy days and the maximal accumulated rainfall with no flood occurring.Then \(n\) lines follow. The \(i\)-th of these lines contains two integers \(x_i\) and \(p_i\) (\(1 \leq x_i,p_i \leq 10^9\)) β€” the position and intensity of the \(i\)-th day's rain.The sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
For each test case, output a binary string \(s\) length of \(n\). The \(i\)-th character of \(s\) is 1 if after erasing the \(i\)-th day's rain there is no flood, while it is 0, if after erasing the \(i\)-th day's rain the flood still happens.
In the first test case, if we do not use the spell, the accumulated rainfall distribution will be like this: If we erase the third day's rain, the flood is avoided and the accumulated rainfall distribution looks like this: In the second test case, since initially the flood will not happen, we can erase any day's rain.In the third test case, there is no way to avoid the flood.
Input: 43 61 55 53 42 31 35 22 51 610 66 124 51 612 55 59 78 3 | Output: 001 11 00 100110
Hard
7
806
631
243
17
2,106
F
2106F
F. Goblin
1,900
dfs and similar; dp; dsu; greedy; math
Dr. TC has a new patient called Goblin. He wants to test Goblin's intelligence, but he has gotten bored of his standard test. So, he decided to make it a bit harder.First, he creates a binary string\(^{\text{βˆ—}}\) \(s\) having \(n\) characters. Then, he creates \(n\) binary strings \(a_1, a_2, \ldots, a_n\). It is known that \(a_i\) is created by first copying \(s\), then flipping the \(i\)-th character (\(\texttt{1}\) becomes \(\texttt{0}\) and vice versa). After creating all \(n\) strings, he arranges them into an \(n \times n\) grid \(g\) where \(g_{i, j} = a_{i_j}\). A set \(S\) of size \(k\) containing distinct integer pairs \(\{(x_1, y_1), (x_2, y_2), \ldots, (x_k, y_k)\}\) is considered good if: \(1 \leq x_i, y_i \leq n\) for all \(1 \leq i \leq k\). \(g_{x_i, y_i} = \texttt{0}\) for all \(1 \leq i \leq k\). For any two integers \(i\) and \(j\) (\(1 \leq i, j \leq k\)), coordinate \((x_i, y_i)\) is reachable from coordinate \((x_j, y_j)\) by traveling through a sequence of adjacent cells (which share a side) that all have a value of \(\texttt{0}\). Goblin's task is to find the maximum possible size of a good set \(S\). Because Dr. TC is generous, this time he gave him two seconds to find the answer instead of one. Goblin is not known for his honesty, so he has asked you to help him cheat.\(^{\text{βˆ—}}\)A binary string is a string that only consists of characters \(\texttt{1}\) and \(\texttt{0}\).
The first line of the input consists of a single integer \(t\) \((1 \le t \le 10^3)\) β€” the number of test cases.The first line of each test contains a single integer \(n\) \((1 \le n \le 2 \cdot 10^5)\) β€” the length of the binary string \(s\).The second line of each test contains a single binary string \(s\) of length \(n\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
For each test case, output a single number, the maximum possible size of a good set of cells from the grid.
In the first example, the following grid has been written on the board:$$$\( 1 0 0 \)\( \)\( 0 1 0 \)\( \)\( 0 0 1 \)\(The set that consists of cells \)(1, 2)\( and \)(1, 3)\( is good. The set that consists of cells \)(1, 1)\( and \)(1, 2)\( is not good, since the value of cell \)(1, 1)\( is not \)0\(. The set of cells \)(1, 2)\(, \)(1, 3)\(, \)(2, 3)\( is good and has a maximum size of \)3\(. Note that the set of cells \)(2, 1)\(, \)(3, 1)\(, and \)(3, 2)\( is also good with a maximum size of \)3\(.In the second example, the following grid is written on the board:\)\( 1 0 1 0 \)\( \)\( 0 1 1 0 \)\( \)\( 0 0 0 0 \)\( \)\( 0 0 1 1 \)\(And the maximum possible size of a good set is \)9$$$.
Input: 6300040010710110014000121110 | Output: 3 9 10 7 1 0
Hard
5
1,426
419
107
21
1,575
J
1575J
J. Jeopardy of Dropped Balls
1,500
binary search; brute force; dsu; implementation
Mr. Chanek has a new game called Dropping Balls. Initially, Mr. Chanek has a grid \(a\) of size \(n \times m\)Each cell \((x,y)\) contains an integer \(a_{x,y}\) denoting the direction of how the ball will move. \(a_{x,y}=1\) β€” the ball will move to the right (the next cell is \((x, y + 1)\)); \(a_{x,y}=2\) β€” the ball will move to the bottom (the next cell is \((x + 1, y)\)); \(a_{x,y}=3\) β€” the ball will move to the left (the next cell is \((x, y - 1)\)). Every time a ball leaves a cell \((x,y)\), the integer \(a_{x,y}\) will change to \(2\). Mr. Chanek will drop \(k\) balls sequentially, each starting from the first row, and on the \(c_1, c_2, \dots, c_k\)-th (\(1 \leq c_i \leq m\)) columns.Determine in which column each ball will end up in (position of the ball after leaving the grid).
The first line contains three integers \(n\), \(m\), and \(k\) (\(1 \leq n, m \leq 1000\), \(1 \leq k \leq 10^5\)) β€” the size of the grid and the number of balls dropped by Mr. Chanek.The \(i\)-th of the next \(n\) lines contains \(m\) integers \(a_{i,1},a_{i,2},\ldots,a_{i,m}\) (\(1 \leq a_{i,j} \leq 3\)). It will satisfy \(a_{i, 1} \ne 3\) and \(a_{i, m} \ne 1\).The next line contains \(k\) integers \(c_1, c_2, \ldots, c_k\) (\(1 \leq c_i \leq m\)) β€” the balls' column positions dropped by Mr. Chanek sequentially.
Output \(k\) integers β€” the \(i\)-th integer denoting the column where the \(i\)-th ball will end.
In the first example, the first ball will drop as follows. Note that the cell \((1, 1)\) will change direction to the bottom direction. The second and third balls will drop as follows. All balls will be dropped from the first row and on the \(c_1, c_2, \dots, c_k\)-th columns respectively. A ball will stop dropping once it leaves the grid.
Input: 5 5 3 1 2 3 3 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 1 | Output: 2 2 1
Medium
4
799
520
98
15
676
C
676C
C. Vasya and String
1,500
binary search; dp; strings; two pointers
High school student Vasya got a string of length n as a birthday present. This string consists of letters 'a' and 'b' only. Vasya denotes beauty of the string as the maximum length of a substring (consecutive subsequence) consisting of equal letters.Vasya can change no more than k characters of the original string. What is the maximum beauty of the string he can achieve?
The first line of the input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n) β€” the length of the string and the maximum number of characters to change.The second line contains the string, consisting of letters 'a' and 'b' only.
Print the only integer β€” the maximum beauty of the string Vasya can achieve by changing no more than k characters.
In the first sample, Vasya can obtain both strings ""aaaa"" and ""bbbb"".In the second sample, the optimal answer is obtained with the string ""aaaaabaa"" or with the string ""aabaaaaa"".
Input: 4 2abba | Output: 4
Medium
4
373
237
114
6
1,983
E
1983E
E. I Love Balls
2,300
combinatorics; math; probabilities
Alice and Bob are playing a game. There are \(n\) balls, out of which \(k\) are special. Each ball has a value associated with it. The players play turn by turn. In each turn, the player randomly picks a ball and adds the value of the ball to their score, which is \(0\) at the beginning of the game. The selected ball is removed from the game. If the ball was special, the same player takes the next turn if at least one ball is remaining. If the ball picked was not special, the next player plays their turn.They play this game until no balls are remaining in the game. Alice plays first.Find the expected score that both the players have at the end of the game modulo \(10^9+7\). Formally, let \(M = 10^9+7\). It can be shown that the answer can be expressed as an irreducible fraction \(\frac{p}{q}\), where \(p\) and \(q\) are integers and \(q \not \equiv 0 \pmod{M}\). Output the integer equal to \(p \cdot q^{-1} \bmod M\). In other words, output such an integer \(x\) that \(0 \le x < M\) and \(x \cdot q \equiv p \pmod{M}\).
There are multiple test cases. The first line of the input contains an integer \(t\), the number of test cases (\(1 \le t \le 2 \cdot 10^5\)).Each test case description is on a new line. The first line of the test case contains two integers \(n\) and \(k\) in the respective order separated by a space (\(1 \le k \le n \le 4 \cdot 10^5\)).The second line of the test case contains \(n\) integers: \(v_1, v_2, \ldots, v_n\), the value for each ball separated by spaces. The first \(k\) balls are special (\(1 \le v_i \le 10^7\)).The sum of \(n\) over all test cases does not exceed \(5 \cdot 10^5\).
Output two integers per test case in a new line, the expected score of Alice and the expected score of Bob modulo \(10^9+7\).
In the first test case, Alice's expected score is \(45\), and Bob's is \(30\) at the end of the game.
Input: 15 210 20 5 15 25 | Output: 45 30
Expert
3
1,033
598
125
19
1,695
B
1695B
B. Circle Game
1,000
games; greedy
Mike and Joe are playing a game with some stones. Specifically, they have \(n\) piles of stones of sizes \(a_1, a_2, \ldots, a_n\). These piles are arranged in a circle.The game goes as follows. Players take turns removing some positive number of stones from a pile in clockwise order starting from pile \(1\). Formally, if a player removed stones from pile \(i\) on a turn, the other player removes stones from pile \(((i\bmod n) + 1)\) on the next turn.If a player cannot remove any stones on their turn (because the pile is empty), they lose. Mike goes first.If Mike and Joe play optimally, who will win?
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)). Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 50\)) β€” the number of piles.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) β€” the size of the piles.
For each test case print the winner of the game, either ""Mike"" or ""Joe"" on its own line (without quotes).
In the first test case, Mike just takes all \(37\) stones on his first turn.In the second test case, Joe can just copy Mike's moves every time. Since Mike went first, he will hit \(0\) on the first pile one move before Joe does so on the second pile.
Input: 21372100 100 | Output: Mike Joe
Beginner
2
607
380
109
16
2,043
B
2043B
B. Digits
1,100
math; number theory
Artem wrote the digit \(d\) on the board exactly \(n!\) times in a row. So, he got the number \(dddddd \dots ddd\) (exactly \(n!\) digits).Now he is curious about which odd digits from \(1\) to \(9\) divide the number written on the board.
The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β€” the number of test cases. The next \(t\) test cases follow.Each test case consists of a single line containing two integers \(n\) and \(d\) (\(2 \le n \le 10^9\), \(1 \le d \le 9\)).
For each test case, output the odd digits in ascending order that divide the number written on the board.
The factorial of a positive integer \(n\) (\(n!\)) is the product of all integers from \(1\) to \(n\). For example, the factorial of \(5\) is \(1 \cdot 2 \cdot 3 \cdot 4 \cdot 5 = 120\).
Input: 32 67 18 5 | Output: 1 3 1 3 7 9 1 3 5 7 9
Easy
2
239
253
105
20
1,031
F
1031F
F. Familiar Operations
2,800
brute force; graphs; math
You are given two positive integers \(a\) and \(b\). There are two possible operations: multiply one of the numbers by some prime \(p\); divide one of the numbers on its prime factor \(p\). What is the minimum number of operations required to obtain two integers having the same number of divisors? You are given several such pairs, you need to find the answer for each of them.
The first line contains a single integer \(t\) (\(1 \le t \le 10^5\)) β€” the number of pairs of integers for which you are to find the answer.Each of the next \(t\) lines contain two integers \(a_i\) and \(b_i\) (\(1 \le a_i, b_i \le 10^6\)).
Output \(t\) lines β€” the \(i\)-th of them should contain the answer for the pair \(a_i\), \(b_i\).
These are the numbers with equal number of divisors, which are optimal to obtain in the sample test case: \((27, 10)\), 4 divisors \((100, 1156)\), 9 divisors \((220, 140)\), 12 divisors \((17, 19)\), 2 divisors \((12, 18)\), 6 divisors \((50, 32)\), 6 divisors \((224, 1925)\), 12 divisors Note that there can be several optimal pairs of numbers.
Input: 89 10100 17220 7017 194 1832 20100 32224 385 | Output: 13101011
Master
3
378
241
98
10
1,760
F
1760F
F. Quests
1,500
binary search; greedy; sortings
There are \(n\) quests. If you complete the \(i\)-th quest, you will gain \(a_i\) coins. You can only complete at most one quest per day. However, once you complete a quest, you cannot do the same quest again for \(k\) days. (For example, if \(k=2\) and you do quest \(1\) on day \(1\), then you cannot do it on day \(2\) or \(3\), but you can do it again on day \(4\).)You are given two integers \(c\) and \(d\). Find the maximum value of \(k\) such that you can gain at least \(c\) coins over \(d\) days. If no such \(k\) exists, output Impossible. If \(k\) can be arbitrarily large, output Infinity.
The input consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains three integers \(n,c,d\) (\(2 \leq n \leq 2\cdot10^5\); \(1 \leq c \leq 10^{16}\); \(1 \leq d \leq 2\cdot10^5\)) β€” the number of quests, the number of coins you need, and the number of days.The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \leq a_i \leq 10^9\)) β€” the rewards for the quests.The sum of \(n\) over all test cases does not exceed \(2\cdot10^5\), and the sum of \(d\) over all test cases does not exceed \(2\cdot10^5\).
For each test case, output one of the following. If no such \(k\) exists, output Impossible. If \(k\) can be arbitrarily large, output Infinity. Otherwise, output a single integer β€” the maximum value of \(k\) such that you can gain at least \(c\) coins over \(d\) days. Please note, the checker is case-sensitive, and you should output strings exactly as they are given.
In the first test case, one way to earn \(5\) coins over \(4\) days with \(k=2\) is as follows: Day 1: do quest 2, and earn \(2\) coins. Day 2: do quest 1, and earn \(1\) coin. Day 3: do nothing. Day 4: do quest 2, and earn \(2\) coins. In total, we earned \(2+1+2=5\) coins.In the second test case, we can make over \(20\) coins on the first day itself by doing the first quest to earn \(100\) coins, so the value of \(k\) can be arbitrarily large, since we never need to do another quest.In the third test case, no matter what we do, we can't earn \(100\) coins over \(3\) days.
Input: 62 5 41 22 20 10100 103 100 37 2 64 20 34 5 6 74 100000000000 20228217734 927368 26389746 6278969742 20 45 1 | Output: 2 Infinity Impossible 1 12 0
Medium
3
602
691
370
17
1,789
D
1789D
D. Serval and Shift-Shift-Shift
2,200
bitmasks; brute force; constructive algorithms; implementation
Serval has two \(n\)-bit binary integer numbers \(a\) and \(b\). He wants to share those numbers with Toxel.Since Toxel likes the number \(b\) more, Serval decides to change \(a\) into \(b\) by some (possibly zero) operations. In an operation, Serval can choose any positive integer \(k\) between \(1\) and \(n\), and change \(a\) into one of the following number: \(a\oplus(a\ll k)\) \(a\oplus(a\gg k)\) In other words, the operation moves every bit of \(a\) left or right by \(k\) positions, where the overflowed bits are removed, and the missing bits are padded with \(0\). The bitwise XOR of the shift result and the original \(a\) is assigned back to \(a\).Serval does not have much time. He wants to perform no more than \(n\) operations to change \(a\) into \(b\). Please help him to find out an operation sequence, or determine that it is impossible to change \(a\) into \(b\) in at most \(n\) operations. You do not need to minimize the number of operations. In this problem, \(x\oplus y\) denotes the bitwise XOR operation of \(x\) and \(y\). \(a\ll k\) and \(a\gg k\) denote the logical left shift and logical right shift.
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1\le t\le2\cdot10^{3}\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1\le n\le2\cdot10^{3}\)) β€” the number of bits in numbers \(a\) and \(b\).The second and the third line of each test case contain a binary string of length \(n\), representing \(a\) and \(b\), respectively. The strings contain only characters 0 and 1.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2\cdot10^{3}\).
For each test case, if it is impossible to change \(a\) into \(b\) in at most \(n\) operations, print a single integer \(-1\).Otherwise, in the first line, print the number of operations \(m\) (\(0\le m\le n\)).If \(m>0\), in the second line, print \(m\) integers \(k_{1},k_{2},\dots,k_{m}\) representing the operations. If \(1\le k_{i}\le n\), it means logical left shift \(a\) by \(k_{i}\) positions. If \(-n\le k_{i}\le-1\), it means logical right shift \(a\) by \(-k_{i}\) positions.If there are multiple solutions, print any of them.
In the first test case:The first operation changes \(a\) into \(\require{cancel}00111\oplus\cancel{001}11\underline{000}=11111\).The second operation changes \(a\) into \(\require{cancel}11111\oplus\underline{00}111\cancel{11}=11000\).The bits with strikethroughs are overflowed bits that are removed. The bits with underline are padded bits.In the second test case, \(a\) is already equal to \(b\), so no operations are needed.In the third test case, it can be shown that \(a\) cannot be changed into \(b\).
Input: 3500111110001113001000 | Output: 2 3 -2 0 -1
Hard
4
1,133
576
538
17
258
E
258E
E. Little Elephant and Tree
2,400
data structures; dfs and similar; trees
The Little Elephant loves trees very much, he especially loves root trees.He's got a tree consisting of n nodes (the nodes are numbered from 1 to n), with root at node number 1. Each node of the tree contains some list of numbers which initially is empty. The Little Elephant wants to apply m operations. On the i-th operation (1 ≀ i ≀ m) he first adds number i to lists of all nodes of a subtree with the root in node number ai, and then he adds number i to lists of all nodes of the subtree with root in node bi.After applying all operations the Little Elephant wants to count for each node i number ci β€” the number of integers j (1 ≀ j ≀ n; j β‰  i), such that the lists of the i-th and the j-th nodes contain at least one common number.Help the Little Elephant, count numbers ci for him.
The first line contains two integers n and m (1 ≀ n, m ≀ 105) β€” the number of the tree nodes and the number of operations. Each of the following n - 1 lines contains two space-separated integers, ui and vi (1 ≀ ui, vi ≀ n, ui β‰  vi), that mean that there is an edge between nodes number ui and vi. Each of the following m lines contains two space-separated integers, ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi), that stand for the indexes of the nodes in the i-th operation.It is guaranteed that the given graph is an undirected tree.
In a single line print n space-separated integers β€” c1, c2, ..., cn.
Input: 5 11 21 33 53 42 3 | Output: 0 3 3 3 3
Expert
3
789
525
68
2
598
B
598B
B. Queries on a String
1,300
implementation; strings
You are given a string s and should process m queries. Each query is described by two 1-based indices li, ri and integer ki. It means that you should cyclically shift the substring s[li... ri] ki times. The queries should be processed one after another in the order they are given.One operation of a cyclic shift (rotation) is equivalent to moving the last character to the position of the first character and shifting all other characters one position to the right.For example, if the string s is abacaba and the query is l1 = 3, r1 = 6, k1 = 1 then the answer is abbacaa. If after that we would process the query l2 = 1, r2 = 4, k2 = 2 then we would get the string baabcaa.
The first line of the input contains the string s (1 ≀ |s| ≀ 10 000) in its initial state, where |s| stands for the length of s. It contains only lowercase English letters.Second line contains a single integer m (1 ≀ m ≀ 300) β€” the number of queries.The i-th of the next m lines contains three integers li, ri and ki (1 ≀ li ≀ ri ≀ |s|, 1 ≀ ki ≀ 1 000 000) β€” the description of the i-th query.
Print the resulting string s after processing all m queries.
The sample is described in problem statement.
Input: abacaba23 6 11 4 2 | Output: baabcaa
Easy
2
675
393
60
5
173
C
173C
C. Spiral Maximum
1,900
brute force; dp
Let's consider a k Γ— k square, divided into unit squares. Please note that k β‰₯ 3 and is odd. We'll paint squares starting from the upper left square in the following order: first we move to the right, then down, then to the left, then up, then to the right again and so on. We finish moving in some direction in one of two cases: either we've reached the square's border or the square following after the next square is already painted. We finish painting at the moment when we cannot move in any direction and paint a square. The figure that consists of the painted squares is a spiral. The figure shows examples of spirals for k = 3, 5, 7, 9. You have an n Γ— m table, each of its cells contains a number. Let's consider all possible spirals, formed by the table cells. It means that we consider all spirals of any size that don't go beyond the borders of the table. Let's find the sum of the numbers of the cells that form the spiral. You have to find the maximum of those values among all spirals.
The first line contains two integers n and m (3 ≀ n, m ≀ 500) β€” the sizes of the table.Each of the next n lines contains m space-separated integers: the j-th number in the i-th line aij ( - 1000 ≀ aij ≀ 1000) is the number recorded in the j-th cell of the i-th row of the table.
Print a single number β€” the maximum sum of numbers among all spirals.
In the first sample the spiral with maximum sum will cover all 1's of the table.In the second sample the spiral may cover only six 1's.
Input: 6 50 0 0 0 01 1 1 1 10 0 0 0 11 1 1 0 11 0 0 0 11 1 1 1 1 | Output: 17
Hard
2
1,000
278
69
1
1,025
D
1025D
D. Recovering BST
2,100
brute force; dp; math; number theory; trees
Dima the hamster enjoys nibbling different things: cages, sticks, bad problemsetters and even trees!Recently he found a binary search tree and instinctively nibbled all of its edges, hence messing up the vertices. Dima knows that if Andrew, who has been thoroughly assembling the tree for a long time, comes home and sees his creation demolished, he'll get extremely upset. To not let that happen, Dima has to recover the binary search tree. Luckily, he noticed that any two vertices connected by a direct edge had their greatest common divisor value exceed \(1\).Help Dima construct such a binary search tree or determine that it's impossible. The definition and properties of a binary search tree can be found here.
The first line contains the number of vertices \(n\) (\(2 \le n \le 700\)).The second line features \(n\) distinct integers \(a_i\) (\(2 \le a_i \le 10^9\)) β€” the values of vertices in ascending order.
If it is possible to reassemble the binary search tree, such that the greatest common divisor of any two vertices connected by the edge is greater than \(1\), print ""Yes"" (quotes for clarity).Otherwise, print ""No"" (quotes for clarity).
The picture below illustrates one of the possible trees for the first example. The picture below illustrates one of the possible trees for the third example.
Input: 63 6 9 18 36 108 | Output: Yes
Hard
5
717
201
239
10
455
D
455D
D. Serega and Fun
2,700
data structures
Serega loves fun. However, everyone has fun in the unique manner. Serega has fun by solving query problems. One day Fedor came up with such a problem.You are given an array a consisting of n positive integers and queries to it. The queries can be of two types: Make a unit cyclic shift to the right on the segment from l to r (both borders inclusive). That is rearrange elements of the array in the following manner:a[l], a[l + 1], ..., a[r - 1], a[r] β†’ a[r], a[l], a[l + 1], ..., a[r - 1]. Count how many numbers equal to k are on the segment from l to r (both borders inclusive). Fedor hurried to see Serega enjoy the problem and Serega solved it really quickly. Let's see, can you solve it?
The first line contains integer n (1 ≀ n ≀ 105) β€” the number of elements of the array. The second line contains n integers a[1], a[2], ..., a[n] (1 ≀ a[i] ≀ n).The third line contains a single integer q (1 ≀ q ≀ 105) β€” the number of queries. The next q lines contain the queries.As you need to respond to the queries online, the queries will be encoded. A query of the first type will be given in format: 1 l'i r'i. A query of the second type will be given in format: 2 l'i r'i k'i. All the number in input are integer. They satisfy the constraints: 1 ≀ l'i, r'i, k'i ≀ n.To decode the queries from the data given in input, you need to perform the following transformations:li = ((l'i + lastans - 1) mod n) + 1; ri = ((r'i + lastans - 1) mod n) + 1; ki = ((k'i + lastans - 1) mod n) + 1.Where lastans is the last reply to the query of the 2-nd type (initially, lastans = 0). If after transformation li is greater than ri, you must swap these values.
For each query of the 2-nd type print the answer on a single line.
Input: 76 6 2 7 4 2 571 3 62 2 4 22 2 4 72 2 2 51 2 61 1 42 1 7 3 | Output: 2100
Master
1
693
949
66
4
958
A3
958A3
A3. Death Stars (hard)
3,100
The stardate is 2015, and Death Stars are bigger than ever! This time, two rebel spies have yet again given Heidi two maps with the possible locations of the Death Stars.Heidi has now received two maps with possible locations of N Death Stars. She knows that each of the maps is possibly corrupted, and may contain some stars that are not Death Stars. Furthermore, each of the maps was created from a different point of view. Hence, stars that are shown in one of the maps are rotated and translated with respect to the other map. Now Heidi wants to find out which of the stars shown in both maps are actually Death Stars, and the correspondence between the Death Stars on the two maps.
The first line of the input contains an integer N (1000 ≀ N ≀ 50000) – the number of Death Stars. The second line of the input contains an integer N1 (N ≀ N1 ≀ 1.5Β·N) – the number of stars in the first map. The next N1 lines specify the coordinates of the stars in the first map. The i-th line contains two space-separated floating-point numbers xi and yi with two decimal digits of precision each, representing the coordinates of the i-th star in the first map.The next line of the input contains an integer N2 (N ≀ N2 ≀ 1.5Β·N) – the number of stars in the second map. The next N2 lines contain locations of the stars in the second map, given in the same format as for the first map.
You should output exactly N lines, each containing a space-separated pair of integers i1 and i2. Each such line should indicate that the star numbered i1 in the first map corresponds to the star numbered i2 in the second map. Your answer will be considered correct if over 90% of the distinct pairs listed in your output are indeed correct.
The tests are generated in the following way: The number of Death Stars N is pre-selected in some way. The numbers of stars on the first and on the second map, N1 and N2, are selected uniformly at random between 1.0 Γ— N and 1.5 Γ— N. N Death Stars are generated at random, with coordinates between - 10000 and 10000. Additional N1 - N and N2 - N stars for the first and for the second map respectively are generated in the same way. A translation vector (dx, dy) is generated, with dx and dy selected uniformly at random between - 10000 and 10000. Each point in the first map is translated by (dx, dy). A rotation angle ΞΈ is generated, with ΞΈ selected uniformly at random between 0 and 2Ο€. Each point in the first map is rotated by an angle of ΞΈ around the origin. Translations and rotations for the second map are generated and applied in the same way. The order of points is randomly permuted for both maps. The test case is saved, with each point written with two decimal digits of precision.
Master
0
686
684
340
9
1,954
B
1954B
B. Make It Ugly
1,200
implementation; math
Let's call an array \(a\) beautiful if you can make all its elements the same by using the following operation an arbitrary number of times (possibly, zero): choose an index \(i\) (\(2 \le i \le |a| - 1\)) such that \(a_{i - 1} = a_{i + 1}\), and replace \(a_i\) with \(a_{i - 1}\). You are given a beautiful array \(a_1, a_2, \dots, a_n\). What is the minimum number of elements you have to remove from it in order for it to stop being beautiful? Swapping elements is prohibited. If it is impossible to do so, then output -1.
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 contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le n\)).Additional constraints on the input: in every test case, the given array \(a\) is beautiful; the sum of \(n\) over all test cases does not exceed \(3 \cdot 10^5\).
For each test case, output a single integer β€” the minimum number of elements you have to remove from the array \(a\) in order for it to stop being beautiful. If it is impossible, then output -1.
In the first testcase, it is impossible to modify the array in such a way that it stops being beautiful. An array consisting of identical numbers will remain beautiful no matter how many numbers we remove from it.In the second testcase, you can remove the number at the index \(5\), for example.The resulting array will be \([1, 2, 1, 2]\). Let's check if it is beautiful. Two operations are available: Choose \(i = 2\): the array becomes \([1, 1, 1, 2]\). No more operations can be applied to it, and the numbers are not all the same. Choose \(i = 3\) instead: the array becomes \([1, 2, 2, 2]\). No more operations can be applied to it either, and the numbers are still not all the same. Thus, the array \([1, 2, 1, 2]\) is not beautiful.In the fourth testcase, you can remove the first three elements, for example. The resulting array \([5, 3, 3, 3]\) is not beautiful.
Input: 432 2 251 2 1 2 11173 3 3 5 3 3 3 | Output: -1 1 -1 3
Easy
2
526
443
194
19
1,468
B
1468B
B. Bakery
2,900
data structures; dsu
Monocarp would like to open a bakery in his local area. But, at first, he should figure out whether he can compete with other shops.Monocarp plans that the bakery will work for \(n\) days. On the \(i\)-th day, \(a_i\) loaves of bread will be baked in the morning before the opening. At the end of the \(n\)-th day, Monocarp will sell all the remaining bread that wasn't sold earlier with a huge discount.Because of how bread is stored, the bakery seller sells the bread in the following order: firstly, he sells the loaves that were baked that morning; secondly, he sells the loaves that were baked the day before and weren't sold yet; then the loaves that were baked two days before and weren't sold yet, and so on. That's why some customers may buy a rather stale bread and will definitely spread negative rumors.Let's define loaf spoilage as the difference between the day it was baked and the day it was sold. Then the unattractiveness of the bakery will be equal to the maximum spoilage among all loaves of bread baked at the bakery.Suppose Monocarp's local area has consumer demand equal to \(k\), it means that each day \(k\) customers will come to the bakery and each of them will ask for one loaf of bread (the loaves are sold according to the aforementioned order). If there is no bread left, then the person just doesn't buy anything. During the last day sale, all the remaining loaves will be sold (and they will still count in the calculation of the unattractiveness).Monocarp analyzed his competitors' data and came up with \(m\) possible consumer demand values \(k_1, k_2, \dots, k_m\), and now he'd like to calculate the unattractiveness of the bakery for each value of demand. Can you help him?
The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 2 \cdot 10^5\); \(1 \le m \le 2 \cdot 10^5\)) β€” the number of days the bakery is open and the number of possible values of consumer demand.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) β€” the number of bread loaves that will be baked each day.The third line contains \(m\) integers \(k_1, k_2, \dots, k_m\) (\(1 \le k_1 < k_2 < \dots < k_m \le 10^9\)) β€” the possible consumer demand values in the ascending order.
Print \(m\) integers: for each consumer demand, print the unattractiveness of the bakery.
In the first example, let's describe what happens for couple consumer demands:If consumer demand is equal to \(1\): at day \(1\): \(5\) loaves are baked and only \(1\) is sold with spoilage equal to \(1 - 1 = 0\); at day \(2\): \(4\) loaves are left and \(2\) more are baked. Only \(1\) loaf was sold and it was the loaf baked today with spoilage \(2 - 2 = 0\); at day \(3\): \(4\) loaves from the first day and \(1\) loaf from the second day left. One more loaf was baked and was sold this day with spoilage \(3 - 3 = 0\); at day \(4\): \(4\) loaves from the first day and \(1\) loaf from the second day left. \(3\) more loaves were baked and one of them was sold this day with spoilage \(4 - 4 = 0\); at day \(5\): \(4\) loaves from the first day, \(1\) loaf from the second day and \(2\) loaves from the fourth day left. \(7\) more loaves were baked and, since it's the last day, all \(14\) loaves were sold. \(4\) loaves from the first day have the maximum spoilage equal to \(5 - 1 = 4\). In total, the unattractiveness of the bakery will be equal to \(4\).If consumer demand is equal to \(10\) then all baked bread will be sold in the day it was baked and will have spoilage equal to \(0\).
Input: 5 4 5 2 1 3 7 1 3 4 10 | Output: 4 2 1 0
Master
2
1,711
524
89
14
248
C
248C
C. Robo-Footballer
2,000
binary search; geometry
It's a beautiful April day and Wallace is playing football with his friends. But his friends do not know that Wallace actually stayed home with Gromit and sent them his robotic self instead. Robo-Wallace has several advantages over the other guys. For example, he can hit the ball directly to the specified point. And yet, the notion of a giveaway is foreign to him. The combination of these features makes the Robo-Wallace the perfect footballer β€” as soon as the ball gets to him, he can just aim and hit the goal. He followed this tactics in the first half of the match, but he hit the goal rarely. The opposing team has a very good goalkeeper who catches most of the balls that fly directly into the goal. But Robo-Wallace is a quick thinker, he realized that he can cheat the goalkeeper. After all, they are playing in a football box with solid walls. Robo-Wallace can kick the ball to the other side, then the goalkeeper will not try to catch the ball. Then, if the ball bounces off the wall and flies into the goal, the goal will at last be scored.Your task is to help Robo-Wallace to detect a spot on the wall of the football box, to which the robot should kick the ball, so that the ball bounces once and only once off this wall and goes straight to the goal. In the first half of the match Robo-Wallace got a ball in the head and was severely hit. As a result, some of the schemes have been damaged. Because of the damage, Robo-Wallace can only aim to his right wall (Robo-Wallace is standing with his face to the opposing team's goal).The football box is rectangular. Let's introduce a two-dimensional coordinate system so that point (0, 0) lies in the lower left corner of the field, if you look at the box above. Robo-Wallace is playing for the team, whose goal is to the right. It is an improvised football field, so the gate of Robo-Wallace's rivals may be not in the middle of the left wall. In the given coordinate system you are given: y1, y2 β€” the y-coordinates of the side pillars of the goalposts of robo-Wallace's opponents; yw β€” the y-coordinate of the wall to which Robo-Wallace is aiming; xb, yb β€” the coordinates of the ball's position when it is hit; r β€” the radius of the ball. A goal is scored when the center of the ball crosses the OY axis in the given coordinate system between (0, y1) and (0, y2). The ball moves along a straight line. The ball's hit on the wall is perfectly elastic (the ball does not shrink from the hit), the angle of incidence equals the angle of reflection. If the ball bounces off the wall not to the goal, that is, if it hits the other wall or the goal post, then the opposing team catches the ball and Robo-Wallace starts looking for miscalculation and gets dysfunctional. Such an outcome, if possible, should be avoided. We assume that the ball touches an object, if the distance from the center of the ball to the object is no greater than the ball radius r.
The first and the single line contains integers y1, y2, yw, xb, yb, r (1 ≀ y1, y2, yw, xb, yb ≀ 106; y1 < y2 < yw; yb + r < yw; 2Β·r < y2 - y1).It is guaranteed that the ball is positioned correctly in the field, doesn't cross any wall, doesn't touch the wall that Robo-Wallace is aiming at. The goal posts can't be located in the field corners.
If Robo-Wallace can't score a goal in the described manner, print ""-1"" (without the quotes). Otherwise, print a single number xw β€” the abscissa of his point of aiming. If there are multiple points of aiming, print the abscissa of any of them. When checking the correctness of the answer, all comparisons are made with the permissible absolute error, equal to 10 - 8. It is recommended to print as many characters after the decimal point as possible.
Note that in the first and third samples other correct values of abscissa xw are also possible.
Input: 4 10 13 10 3 1 | Output: 4.3750000000
Hard
2
2,917
344
451
2
1,535
E
1535E
E. Gold Transfer
2,200
binary search; data structures; dp; greedy; interactive; trees
You are given a rooted tree. Each vertex contains \(a_i\) tons of gold, which costs \(c_i\) per one ton. Initially, the tree consists only a root numbered \(0\) with \(a_0\) tons of gold and price \(c_0\) per ton.There are \(q\) queries. Each query has one of two types: Add vertex \(i\) (where \(i\) is an index of query) as a son to some vertex \(p_i\); vertex \(i\) will have \(a_i\) tons of gold with \(c_i\) per ton. It's guaranteed that \(c_i > c_{p_i}\). For a given vertex \(v_i\) consider the simple path from \(v_i\) to the root. We need to purchase \(w_i\) tons of gold from vertices on this path, spending the minimum amount of money. If there isn't enough gold on the path, we buy all we can. If we buy \(x\) tons of gold in some vertex \(v\) the remaining amount of gold in it decreases by \(x\) (of course, we can't buy more gold that vertex has at the moment). For each query of the second type, calculate the resulting amount of gold we bought and the amount of money we should spend.Note that you should solve the problem in online mode. It means that you can't read the whole input at once. You can read each query only after writing the answer for the last query, so don't forget to flush output after printing answers. You can use functions like fflush(stdout) in C++ and BufferedWriter.flush in Java or similar after each writing in your program. In standard (if you don't tweak I/O), endl flushes cout in C++ and System.out.println in Java (or println in Kotlin) makes automatic flush as well.
The first line contains three integers \(q\), \(a_0\) and \(c_0\) (\(1 \le q \le 3 \cdot 10^5\); \(1 \le a_0, c_0 < 10^6\)) β€” the number of queries, the amount of gold in the root and its price.Next \(q\) lines contain descriptions of queries; The \(i\)-th query has one of two types: ""\(1\) \(p_i\) \(a_i\) \(c_i\)"" (\(0 \le p_i < i\); \(1 \le a_i, c_i < 10^6\)): add vertex \(i\) as a son to vertex \(p_i\). The vertex \(i\) will have \(a_i\) tons of gold with price \(c_i\) per one ton. It's guaranteed that \(p_i\) exists and \(c_i > c_{p_i}\). ""\(2\) \(v_i\) \(w_i\)"" (\(0 \le v_i < i\); \(1 \le w_i < 10^6\)): buy \(w_i\) tons of gold from vertices on path from \(v_i\) to \(0\) spending the minimum amount of money. If there isn't enough gold, we buy as much as we can. It's guaranteed that vertex \(v_i\) exist. It's guaranteed that there is at least one query of the second type.
For each query of the second type, print the resulting amount of gold we bought and the minimum amount of money we should spend.
Explanation of the sample:At the first query, the tree consist of root, so we purchase \(2\) tons of gold and pay \(2 \cdot 2 = 4\). \(3\) tons remain in the root.At the second query, we add vertex \(2\) as a son of vertex \(0\). Vertex \(2\) now has \(3\) tons of gold with price \(4\) per one ton.At the third query, a path from \(2\) to \(0\) consists of only vertices \(0\) and \(2\) and since \(c_0 < c_2\) we buy \(3\) remaining tons of gold in vertex \(0\) and \(1\) ton in vertex \(2\). So we bought \(3 + 1 = 4\) tons and paid \(3 \cdot 2 + 1 \cdot 4 = 10\). Now, in vertex \(0\) no gold left and \(2\) tons of gold remain in vertex \(2\).At the fourth query, we add vertex \(4\) as a son of vertex \(0\). Vertex \(4\) now has \(1\) ton of gold with price \(3\).At the fifth query, a path from \(4\) to \(0\) consists of only vertices \(0\) and \(4\). But since no gold left in vertex \(0\) and only \(1\) ton is in vertex \(4\), we buy \(1\) ton of gold in vertex \(4\) and spend \(1 \cdot 3 = 3\). Now, in vertex \(4\) no gold left.
Input: 5 5 2 2 0 2 1 0 3 4 2 2 4 1 0 1 3 2 4 2 | Output: 2 4 4 10 1 3
Hard
6
1,516
892
128
15
1,391
C
1391C
C. Cyclic Permutations
1,500
combinatorics; dp; graphs; math
A permutation of length \(n\) 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).Consider a permutation \(p\) of length \(n\), we build a graph of size \(n\) using it as follows: For every \(1 \leq i \leq n\), find the largest \(j\) such that \(1 \leq j < i\) and \(p_j > p_i\), and add an undirected edge between node \(i\) and node \(j\) For every \(1 \leq i \leq n\), find the smallest \(j\) such that \(i < j \leq n\) and \(p_j > p_i\), and add an undirected edge between node \(i\) and node \(j\) In cases where no such \(j\) exists, we make no edges. Also, note that we make edges between the corresponding indices, not the values at those indices.For clarity, consider as an example \(n = 4\), and \(p = [3,1,4,2]\); here, the edges of the graph are \((1,3),(2,1),(2,3),(4,3)\).A permutation \(p\) is cyclic if the graph built using \(p\) has at least one simple cycle. Given \(n\), find the number of cyclic permutations of length \(n\). Since the number may be very large, output it modulo \(10^9+7\).Please refer to the Notes section for the formal definition of a simple cycle
The first and only line contains a single integer \(n\) (\(3 \le n \le 10^6\)).
Output a single integer \(0 \leq x < 10^9+7\), the number of cyclic permutations of length \(n\) modulo \(10^9+7\).
There are \(16\) cyclic permutations for \(n = 4\). \([4,2,1,3]\) is one such permutation, having a cycle of length four: \(4 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 4\).Nodes \(v_1\), \(v_2\), \(\ldots\), \(v_k\) form a simple cycle if the following conditions hold: \(k \geq 3\). \(v_i \neq v_j\) for any pair of indices \(i\) and \(j\). (\(1 \leq i < j \leq k\)) \(v_i\) and \(v_{i+1}\) share an edge for all \(i\) (\(1 \leq i < k\)), and \(v_1\) and \(v_k\) share an edge.
Input: 4 | Output: 16
Medium
4
1,329
79
115
13
1,535
B
1535B
B. Array Reodering
900
brute force; greedy; math; number theory; sortings
You are given an array \(a\) consisting of \(n\) integers.Let's call a pair of indices \(i\), \(j\) good if \(1 \le i < j \le n\) and \(\gcd(a_i, 2a_j) > 1\) (where \(\gcd(x, y)\) is the greatest common divisor of \(x\) and \(y\)).Find the maximum number of good index pairs if you can reorder the array \(a\) in an arbitrary way.
The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases.The first line of the test case contains a single integer \(n\) (\(2 \le n \le 2000\)) β€” the number of elements in the array.The second line of the test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^5\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2000\).
For each test case, output a single integer β€” the maximum number of good index pairs if you can reorder the array \(a\) in an arbitrary way.
In the first example, the array elements can be rearranged as follows: \([6, 3, 5, 3]\).In the third example, the array elements can be rearranged as follows: \([4, 4, 2, 1, 1]\).
Input: 3 4 3 6 5 3 2 1 7 5 1 4 2 4 1 | Output: 4 0 9
Beginner
5
330
413
140
15
2,040
A
2040A
A. Game of Division
800
games; math
You are given an array of integers \(a_1, a_2, \ldots, a_n\) of length \(n\) and an integer \(k\).Two players are playing a game. The first player chooses an index \(1 \le i \le n\). Then the second player chooses a different index \(1 \le j \le n, i \neq j\). The first player wins if \(|a_i - a_j|\) is not divisible by \(k\). Otherwise, the second player wins.We play as the first player. Determine whether it is possible to win, and if so, which index \(i\) should be chosen.The absolute value of a number \(x\) is denoted by \(|x|\) and is equal to \(x\) if \(x \ge 0\), and \(-x\) otherwise.
Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 100\)) β€” 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 100\); \(1 \le k \le 100\)) β€” the length of the array and the number \(k\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 100\)) β€” the elements of the array \(a\).
For each test case, if it is impossible for the first player to win, print ""NO"" (without quotes).Otherwise, print ""YES"" (without quotes) and on the next line the appropriate index \(1 \le i \le n\). If there are multiple solutions, print any of them.You can output each letter in any case (lowercase or uppercase). For example, the strings ""yEs"", ""yes"", ""Yes"" and ""YES"" will be recognized as a positive answer.
In the first test case, the first player can choose \(a_2 = 2\). Then: If the second player chooses \(a_1 = 1\), the resulting difference is \(|2 - 1| = 1\) which is not divisible by \(k = 2\). If the second player chooses \(a_3 = 3\), the resulting difference is \(|2 - 3| = 1\) which is not divisible by \(k = 2\). In the second test case: If the first player chooses \(a_1 = 1\) and then the second player chooses \(a_4 = 5\), the resulting difference is \(|1 - 5| = 4\) which is divisible by \(k = 2\). If the first player chooses \(a_2 = 2\) and then the second player chooses \(a_3 = 4\), the resulting difference is \(|2 - 4| = 2\) which is divisible by \(k = 2\). If the first player chooses \(a_3 = 4\) and then the second player chooses \(a_2 = 2\), the resulting difference is \(|4 - 2| = 2\) which is divisible by \(k = 2\). If the first player chooses \(a_4 = 5\) and then the second player chooses \(a_1 = 1\), the resulting difference is \(|5 - 1| = 4\) which is divisible by \(k = 2\). In any case, the second player wins.
Input: 73 21 2 34 21 2 4 55 310 7 3 4 55 31 31 15 55 362 117 172 217 181 36 | Output: YES 2 NO YES 3 NO NO YES 2 YES 1
Beginner
2
597
491
422
20
1,003
B
1003B
B. Binary String Constructing
1,300
constructive algorithms
You are given three integers \(a\), \(b\) and \(x\). Your task is to construct a binary string \(s\) of length \(n = a + b\) such that there are exactly \(a\) zeroes, exactly \(b\) ones and exactly \(x\) indices \(i\) (where \(1 \le i < n\)) such that \(s_i \ne s_{i + 1}\). It is guaranteed that the answer always exists.For example, for the string ""01010"" there are four indices \(i\) such that \(1 \le i < n\) and \(s_i \ne s_{i + 1}\) (\(i = 1, 2, 3, 4\)). For the string ""111001"" there are two such indices \(i\) (\(i = 3, 5\)).Recall that binary string is a non-empty sequence of characters where each character is either 0 or 1.
The first line of the input contains three integers \(a\), \(b\) and \(x\) (\(1 \le a, b \le 100, 1 \le x < a + b)\).
Print only one string \(s\), where \(s\) is any binary string satisfying conditions described above. It is guaranteed that the answer always exists.
All possible answers for the first example: 1100; 0011. All possible answers for the second example: 110100; 101100; 110010; 100110; 011001; 001101; 010011; 001011.
Input: 2 2 1 | Output: 1100
Easy
1
639
117
148
10
1,799
G
1799G
G. Count Voting
2,600
combinatorics; dp; math
There are \(n\) people that will participate in voting. Each person has exactly one vote.\(i\)-th person has a team \(t_i\) (\(1 \leq t_i \leq n\)) where \(t_i = t_j\) means \(i\), \(j\) are in the same team. By the rules each person should vote for the person from the different team. Note that it automatically means that each person can't vote for himself.Each person knows the number of votes \(c_i\) he wants to get. How many possible votings exists, such that each person will get the desired number of votes? Due to this number can be big, find it by modulo \(998\,244\,353\).
The first line contains a single integer \(n\) (\(1 \leq n \leq 200\)) β€” the number of people.The second line contains \(n\) integers \(c_1, c_2, \ldots, c_n\) (\(0 \leq c_i \leq n\)) β€” desired number of votes. It is guaranteed, that \(\sum\limits_{i=1}^{n} c_i = n\).The third line contains \(n\) integers \(t_1, t_2, \ldots, t_n\) (\(1 \leq t_i \leq n\)) β€” team numbers.
Print a single integer β€” the number of possible votings by modulo \(998\,244\,353\).
In the first test there are two possible votings: \((2, 3, 1)\), \((3, 1, 2)\).In the third test there are five possible votings: \((3, 3, 2, 2, 1)\), \((2, 3, 2, 3, 1)\), \((3, 3, 1, 2, 2)\), \((3, 1, 2, 3, 2)\), \((2, 3, 1, 3, 2)\).
Input: 3 1 1 1 1 2 3 | Output: 2
Expert
3
583
372
84
17
461
A
461A
A. Appleman and Toastman
1,200
greedy; sortings
Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks: Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman. Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman. After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?
The first line contains a single integer n (1 ≀ n ≀ 3Β·105). The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 106) β€” the initial group that is given to Toastman.
Print a single integer β€” the largest possible score.
Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions.
Input: 33 1 5 | Output: 26
Easy
2
679
173
52
4
2,073
K
2073K
2,900
Master
0
0
0
0
20
215
E
215E
E. Periodical Numbers
2,100
combinatorics; dp; number theory
A non-empty string s is called binary, if it consists only of characters ""0"" and ""1"". Let's number the characters of binary string s from 1 to the string's length and let's denote the i-th character in string s as si.Binary string s with length n is periodical, if there is an integer 1 ≀ k < n such that: k is a divisor of number n for all 1 ≀ i ≀ n - k, the following condition fulfills: si = si + k For example, binary strings ""101010"" and ""11"" are periodical and ""10"" and ""10010"" are not.A positive integer x is periodical, if its binary representation (without leading zeroes) is a periodic string.Your task is to calculate, how many periodic numbers are in the interval from l to r (both ends are included).
The single input line contains two integers l and r (1 ≀ l ≀ r ≀ 1018). The numbers are separated by a space.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 a single integer, showing how many periodic numbers are in the interval from l to r (both ends are included).
In the first sample periodic numbers are 3, 7 and 10.In the second sample periodic numbers are 31 and 36.
Input: 1 10 | Output: 3
Hard
3
725
256
115
2
2,003
D2
2003D2
D2. Turtle and a MEX Problem (Hard Version)
2,100
dfs and similar; dp; graphs; greedy; implementation; math
The two versions are different problems. In this version of the problem, you can't choose the same integer twice or more. You can make hacks only if both versions are solved.One day, Turtle was playing with \(n\) sequences. Let the length of the \(i\)-th sequence be \(l_i\). Then the \(i\)-th sequence was \(a_{i, 1}, a_{i, 2}, \ldots, a_{i, l_i}\).Piggy gave Turtle a problem to solve when Turtle was playing. The statement of the problem was: There was a non-negative integer \(x\) at first. Turtle would perform an arbitrary number (possibly zero) of operations on the integer. In each operation, Turtle could choose an integer \(i\) such that \(1 \le i \le n\) and \(i\) wasn't chosen before, and set \(x\) to \(\text{mex}^{\dagger}(x, a_{i, 1}, a_{i, 2}, \ldots, a_{i, l_i})\). Turtle was asked to find the answer, which was the maximum value of \(x\) after performing an arbitrary number of operations. Turtle solved the above problem without difficulty. He defined \(f(k)\) as the answer to the above problem when the initial value of \(x\) was \(k\).Then Piggy gave Turtle a non-negative integer \(m\) and asked Turtle to find the value of \(\sum\limits_{i = 0}^m f(i)\) (i.e., the value of \(f(0) + f(1) + \ldots + f(m)\)). Unfortunately, he couldn't solve this problem. Please help him!\(^{\dagger}\text{mex}(c_1, c_2, \ldots, c_k)\) is defined as the smallest non-negative integer \(x\) which does not occur in the sequence \(c\). For example, \(\text{mex}(2, 2, 0, 3)\) is \(1\), \(\text{mex}(1, 2)\) is \(0\).
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The first line of each test case contains two integers \(n, m\) (\(1 \le n \le 2 \cdot 10^5, 0 \le m \le 10^9\)).Each of the following \(n\) lines contains several integers. The first integer \(l_i\) (\(1 \le l_i \le 2 \cdot 10^5\)) represents the length of the \(i\)-th sequence, and the following \(l_i\) integers \(a_{i, 1}, a_{i, 2}, \ldots, a_{i, l_i}\) (\(0 \le a_{i, j} \le 10^9\)) represent the elements of the \(i\)-th sequence.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\) and the sum of \(\sum l_i\) over all test cases does not exceed \(2 \cdot 10^5\).
For each test case, output a single integer β€” the value of \(\sum\limits_{i = 0}^m f(i)\).
In the first test case, when \(x\) is initially \(2\), Turtle can choose \(i = 3\) and set \(x\) to \(\text{mex}(x, a_{3, 1}, a_{3, 2}, a_{3, 3}, a_{3, 4}) = \text{mex}(2, 7, 0, 1, 5) = 3\). It can be proved that Turtle can't make the value of \(x\) greater than \(3\), so \(f(2) = 3\).It can be seen that \(f(0) = 3\), \(f(1) = 3\), \(f(2) = 3\), \(f(3) = 3\), and \(f(4) = 4\). So \(f(0) + f(1) + f(2) + f(3) + f(4) = 3 + 3 + 3 + 3 + 4 = 16\).In the second test case, when \(x\) is initially \(1\), Turtle can choose \(i = 1\) and set \(x\) to \(\text{mex}(x, a_{1, 1}, a_{1, 2}, a_{1, 3}, a_{1, 4}, a_{1, 5}) = \text{mex}(1, 0, 2, 0, 4, 11) = 3\). It can be proved that Turtle can't make the value of \(x\) greater than \(3\), so \(f(1) = 3\).It can be seen that \(f(0) = 4\), \(f(1) = 3\), \(f(2) = 4\), \(f(3) = 3\), and \(f(4) = 4\). So \(f(0) + f(1) + f(2) + f(3) + f(4) = 4 + 3 + 4 + 3 + 4 = 18\).In the fourth test case, it can be seen that \(f(0) = 3\) and \(f(1) = 1\). So \(f(0) + f(1) = 3 + 1 = 4\).
Input: 63 42 0 23 2 3 34 7 0 1 53 45 0 2 0 4 111 15 1 3 0 3 32 502 1 22 1 21 17 1 2 4 1 4 9 54 1145142 2 25 7 3 6 0 33 0 1 15 0 9 2 1 55 19198101 22 324003 03 1416324 2 14607284 1312631 2 0 14151955 1223554 192248 2 1492515 725556 | Output: 16 18 1281 4 6556785365 1842836177961
Hard
6
1,523
771
90
20
1,030
A
1030A
A. In Search of an Easy Problem
800
implementation
When preparing a tournament, Codeforces coordinators try treir best to make the first problem as easy as possible. This time the coordinator had chosen some problem and asked \(n\) people about their opinions. Each person answered whether this problem is easy or hard.If at least one of these \(n\) people has answered that the problem is hard, the coordinator decides to change the problem. For the given responses, check if the problem is easy enough.
The first line contains a single integer \(n\) (\(1 \le n \le 100\)) β€” the number of people who were asked to give their opinions.The second line contains \(n\) integers, each integer is either \(0\) or \(1\). If \(i\)-th integer is \(0\), then \(i\)-th person thinks that the problem is easy; if it is \(1\), then \(i\)-th person thinks that the problem is hard.
Print one word: ""EASY"" if the problem is easy according to all responses, or ""HARD"" if there is at least one person who thinks the problem is hard. You may print every letter in any register: ""EASY"", ""easy"", ""EaSY"" and ""eAsY"" all will be processed correctly.
In the first example the third person says it's a hard problem, so it should be replaced.In the second example the problem easy for the only person, so it doesn't have to be replaced.
Input: 30 0 1 | Output: HARD
Beginner
1
453
363
270
10
401
E
401E
E. Olympic Games
2,500
math
This problem was deleted from the contest, because it was used previously at another competition.
Input: 1 11 2 100 | Output: 6
Expert
1
97
0
0
4
1,427
C
1427C
C. The Hard Work of Paparazzi
2,000
dp
You are a paparazzi working in Manhattan.Manhattan has \(r\) south-to-north streets, denoted by numbers \(1, 2,\ldots, r\) in order from west to east, and \(r\) west-to-east streets, denoted by numbers \(1,2,\ldots,r\) in order from south to north. Each of the \(r\) south-to-north streets intersects each of the \(r\) west-to-east streets; the intersection between the \(x\)-th south-to-north street and the \(y\)-th west-to-east street is denoted by \((x, y)\). In order to move from the intersection \((x,y)\) to the intersection \((x', y')\) you need \(|x-x'|+|y-y'|\) minutes.You know about the presence of \(n\) celebrities in the city and you want to take photos of as many of them as possible. More precisely, for each \(i=1,\dots, n\), you know that the \(i\)-th celebrity will be at the intersection \((x_i, y_i)\) in exactly \(t_i\) minutes from now (and he will stay there for a very short time, so you may take a photo of him only if at the \(t_i\)-th minute from now you are at the intersection \((x_i, y_i)\)). You are very good at your job, so you are able to take photos instantaneously. You know that \(t_i < t_{i+1}\) for any \(i=1,2,\ldots, n-1\).Currently you are at your office, which is located at the intersection \((1, 1)\). If you plan your working day optimally, what is the maximum number of celebrities you can take a photo of?
The first line of the input contains two positive integers \(r, n\) (\(1\le r\le 500\), \(1\le n\le 100,000\)) – the number of south-to-north/west-to-east streets and the number of celebrities.Then \(n\) lines follow, each describing the appearance of a celebrity. The \(i\)-th of these lines contains \(3\) positive integers \(t_i, x_i, y_i\) (\(1\le t_i\le 1,000,000\), \(1\le x_i, y_i\le r\)) β€” denoting that the \(i\)-th celebrity will appear at the intersection \((x_i, y_i)\) in \(t_i\) minutes from now.It is guaranteed that \(t_i<t_{i+1}\) for any \(i=1,2,\ldots, n-1\).
Print a single integer, the maximum number of celebrities you can take a photo of.
Explanation of the first testcase: There is only one celebrity in the city, and he will be at intersection \((6,8)\) exactly \(11\) minutes after the beginning of the working day. Since you are initially at \((1,1)\) and you need \(|1-6|+|1-8|=5+7=12\) minutes to reach \((6,8)\) you cannot take a photo of the celebrity. Thus you cannot get any photo and the answer is \(0\).Explanation of the second testcase: One way to take \(4\) photos (which is the maximum possible) is to take photos of celebrities with indexes \(3, 5, 7, 9\) (see the image for a visualization of the strategy): To move from the office at \((1,1)\) to the intersection \((5,5)\) you need \(|1-5|+|1-5|=4+4=8\) minutes, so you arrive at minute \(8\) and you are just in time to take a photo of celebrity \(3\). Then, just after you have taken a photo of celebrity \(3\), you move toward the intersection \((4,4)\). You need \(|5-4|+|5-4|=1+1=2\) minutes to go there, so you arrive at minute \(8+2=10\) and you wait until minute \(12\), when celebrity \(5\) appears. Then, just after you have taken a photo of celebrity \(5\), you go to the intersection \((6,6)\). You need \(|4-6|+|4-6|=2+2=4\) minutes to go there, so you arrive at minute \(12+4=16\) and you wait until minute \(17\), when celebrity \(7\) appears. Then, just after you have taken a photo of celebrity \(7\), you go to the intersection \((5,4)\). You need \(|6-5|+|6-4|=1+2=3\) minutes to go there, so you arrive at minute \(17+3=20\) and you wait until minute \(21\) to take a photo of celebrity \(9\). Explanation of the third testcase: The only way to take \(1\) photo (which is the maximum possible) is to take a photo of the celebrity with index \(1\) (since \(|2-1|+|1-1|=1\), you can be at intersection \((2,1)\) after exactly one minute, hence you are just in time to take a photo of celebrity \(1\)).Explanation of the fourth testcase: One way to take \(3\) photos (which is the maximum possible) is to take photos of celebrities with indexes \(3, 8, 10\): To move from the office at \((1,1)\) to the intersection \((101,145)\) you need \(|1-101|+|1-145|=100+144=244\) minutes, so you can manage to be there when the celebrity \(3\) appears (at minute \(341\)). Then, just after you have taken a photo of celebrity \(3\), you move toward the intersection \((149,379)\). You need \(|101-149|+|145-379|=282\) minutes to go there, so you arrive at minute \(341+282=623\) and you wait until minute \(682\), when celebrity \(8\) appears. Then, just after you have taken a photo of celebrity \(8\), you go to the intersection \((157,386)\). You need \(|149-157|+|379-386|=8+7=15\) minutes to go there, so you arrive at minute \(682+15=697\) and you wait until minute \(855\) to take a photo of celebrity \(10\).
Input: 10 1 11 6 8 | Output: 0
Hard
1
1,356
578
82
14
1,607
H
1607H
H. Banquet Preparations 2
2,200
greedy; sortings; two pointers
The chef has cooked \(n\) dishes yet again: the \(i\)-th dish consists of \(a_i\) grams of fish and \(b_i\) grams of meat. Banquet organizers consider two dishes \(i\) and \(j\) equal if \(a_i=a_j\) and \(b_i=b_j\) at the same time.The banquet organizers estimate the variety of \(n\) dishes as follows. The variety of a set of dishes is equal to the number of different dishes in it. The less variety is, the better.In order to reduce the variety, a taster was invited. He will eat exactly \(m_i\) grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he will eat exactly \(m_i\) grams of the \(i\)-th dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of variety is the minimum possible. If there are several correct answers, you may output any of them.
The first line of input data contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β€” the number of test cases.Each test case's description is preceded by a blank line. Next comes a line that contains an integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β€” the number of dishes. Then follows \(n\) lines, \(i\)-th of which contains three integers \(a_i\), \(b_i\) and \(m_i\) (\(0 \leq a_i, b_i \le 10^6\); \(0 \le m_i \le a_i+b_i\)) β€” the mass of fish in \(i\)-th dish, the mass of meat in \(i\)-th dish and how many grams in total the taster should eat in \(i\)-th dish.The sum of all \(n\) values for all input data sets in the test does not exceed \(2 \cdot 10^5\).
For each test case, print on the first line the minimum value of variety that can be achieved by eating exactly \(m_i\) grams of food (for all \(i\) from \(1\) to \(n\)) from a dish \(i\).Then print \(n\) lines that describe a way to do this: the \(i\)-th line should contain two integers \(x_i\) and \(y_i\) (\(0 \leq x_i \leq a_i\); \(0 \leq y_i \leq b_i\); \(x_i+y_i=m_i\)), where \(x_i\) is how many grams of fish the taster should eat from \(i\)-th dish, and \(y_i\) is how many grams of meat.If there are several ways to achieve a minimum balance, print any of them.
Input: 5 3 10 10 2 9 9 0 10 9 1 2 3 4 1 5 1 2 3 7 2 5 6 5 4 5 5 6 1 13 42 50 5 5 7 12 3 1 4 7 3 7 0 0 0 4 1 5 | Output: 1 1 1 0 0 1 0 2 0 1 1 1 2 3 2 0 4 1 5 1 8 42 2 5 7 3 1 4 3 0 0 4 1
Hard
3
909
661
572
16
1,188
C
1188C
C. Array Beauty
2,500
dp
Let's call beauty of an array \(b_1, b_2, \ldots, b_n\) (\(n > 1\)) β€” \(\min\limits_{1 \leq i < j \leq n} |b_i - b_j|\).You're given an array \(a_1, a_2, \ldots a_n\) and a number \(k\). Calculate the sum of beauty over all subsequences of the array of length exactly \(k\). As this number can be very large, output it modulo \(998244353\).A sequence \(a\) is a subsequence of an array \(b\) if \(a\) can be obtained from \(b\) by deletion of several (possibly, zero or all) elements.
The first line contains integers \(n, k\) (\(2 \le k \le n \le 1000\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 10^5\)).
Output one integer β€” the sum of beauty over all subsequences of the array of length exactly \(k\). As this number can be very large, output it modulo \(998244353\).
In the first example, there are \(4\) subsequences of length \(3\) β€” \([1, 7, 3]\), \([1, 3, 5]\), \([7, 3, 5]\), \([1, 7, 5]\), each of which has beauty \(2\), so answer is \(8\).In the second example, there is only one subsequence of length \(5\) β€” the whole array, which has the beauty equal to \(|10-1| = 9\).
Input: 4 3 1 7 3 5 | Output: 8
Expert
1
484
162
164
11
1,305
G
1305G
G. Kuroni and Antihype
3,500
bitmasks; brute force; dp; dsu; graphs
Kuroni isn't good at economics. So he decided to found a new financial pyramid called Antihype. It has the following rules: You can join the pyramid for free and get \(0\) coins. If you are already a member of Antihype, you can invite your friend who is currently not a member of Antihype, and get a number of coins equal to your age (for each friend you invite). \(n\) people have heard about Antihype recently, the \(i\)-th person's age is \(a_i\). Some of them are friends, but friendship is a weird thing now: the \(i\)-th person is a friend of the \(j\)-th person if and only if \(a_i \text{ AND } a_j = 0\), where \(\text{AND}\) denotes the bitwise AND operation.Nobody among the \(n\) people is a member of Antihype at the moment. They want to cooperate to join and invite each other to Antihype in a way that maximizes their combined gainings. Could you help them?
The first line contains a single integer \(n\) (\(1\le n \le 2\cdot 10^5\)) β€” the number of people.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0\le a_i \le 2\cdot 10^5\)) β€” the ages of the people.
Output exactly one integer β€” the maximum possible combined gainings of all \(n\) people.
Only the first and second persons are friends. The second can join Antihype and invite the first one, getting \(2\) for it.
Input: 3 1 2 3 | Output: 2
Master
5
872
220
88
13
86
E
86E
E. Long sequence
2,700
brute force; math; matrices
A sequence a0, a1, ... is called a recurrent binary sequence, if each term ai (i = 0, 1, ...) is equal to 0 or 1 and there exist coefficients such that an = c1Β·an - 1 + c2Β·an - 2 + ... + ckΒ·an - k (mod 2), for all n β‰₯ k. Assume that not all of ci are zeros.Note that such a sequence can be uniquely recovered from any k-tuple {as, as + 1, ..., as + k - 1} and so it is periodic. Moreover, if a k-tuple contains only zeros, then the sequence contains only zeros, so this case is not very interesting. Otherwise the minimal period of the sequence is not greater than 2k - 1, as k-tuple determines next element, and there are 2k - 1 non-zero k-tuples. Let us call a sequence long if its minimal period is exactly 2k - 1. Your task is to find a long sequence for a given k, if there is any.
Input contains a single integer k (2 ≀ k ≀ 50).
If there is no long sequence for a given k, output ""-1"" (without quotes). Otherwise the first line of the output should contain k integer numbers: c1, c2, ..., ck (coefficients). The second line should contain first k elements of the sequence: a0, a1, ..., ak - 1. All of them (elements and coefficients) should be equal to 0 or 1, and at least one ci has to be equal to 1.If there are several solutions, output any.
1. In the first sample: c1 = 1, c2 = 1, so an = an - 1 + an - 2 (mod 2). Thus the sequence will be:so its period equals 3 = 22 - 1.2. In the second sample: c1 = 0, c2 = 1, c3 = 1, so an = an - 2 + an - 3 (mod 2). Thus our sequence is:and its period equals 7 = 23 - 1.Periods are colored.
Input: 2 | Output: 1 11 0
Master
3
786
47
418
0
1,208
A
1208A
A. XORinacci
900
math
Cengiz recently learned Fibonacci numbers and now he is studying different algorithms to find them. After getting bored of reading them, he came with his own new type of numbers that he named XORinacci numbers. He defined them as follows: \(f(0) = a\); \(f(1) = b\); \(f(n) = f(n-1) \oplus f(n-2)\) when \(n > 1\), where \(\oplus\) denotes the bitwise XOR operation. You are given three integers \(a\), \(b\), and \(n\), calculate \(f(n)\).You have to answer for \(T\) independent test cases.
The input contains one or more independent test cases.The first line of input contains a single integer \(T\) (\(1 \le T \le 10^3\)), the number of test cases.Each of the \(T\) following lines contains three space-separated integers \(a\), \(b\), and \(n\) (\(0 \le a, b, n \le 10^9\)) respectively.
For each test case, output \(f(n)\).
In the first example, \(f(2) = f(0) \oplus f(1) = 3 \oplus 4 = 7\).
Input: 3 3 4 2 4 5 0 325 265 1231232 | Output: 7 4 76
Beginner
1
492
299
36
12
2,070
B
2070B
B. Robot Program
1,100
brute force; implementation; math
There is a robot on the coordinate line. Initially, the robot is located at the point \(x\) (\(x \ne 0\)). The robot has a sequence of commands of length \(n\) consisting of characters, where L represents a move to the left by one unit (from point \(p\) to point \((p-1)\)) and R represents a move to the right by one unit (from point \(p\) to point \((p+1)\)).The robot starts executing this sequence of commands (one command per second, in the order they are presented). However, whenever the robot reaches the point \(0\), the counter of executed commands is reset (i. e. it starts executing the entire sequence of commands from the very beginning). If the robot has completed all commands and is not at \(0\), it stops.Your task is to calculate how many times the robot will enter the point \(0\) during the next \(k\) seconds.
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The first line of a test case contains three integers \(n\), \(x\) and \(k\) (\(1 \le n \le 2 \cdot 10^5\); \(-n \le x \le n\); \(n \le k \le 10^{18}\)).The second line of a test case contains a string \(s\) consisting of \(n\) characters L and/or R.Additional constraint on the input: the sum of \(n\) over all test cases doesn't exceed \(2 \cdot 10^5\).
For each test case, print a single integer β€” the number of times the robot will enter the point \(0\) during the next \(k\) seconds.
In the first example, the robot moves as follows: \(2 \rightarrow 1 \rightarrow \underline{0} \rightarrow -1 \rightarrow -2 \rightarrow -1\). The robot has completed all instructions from the sequence and is not at \(0\). So it stops after \(5\) seconds and the point \(0\) is entered once.In the second example, the robot moves as follows: \(-1 \rightarrow \underline{0} \rightarrow 1 \rightarrow \underline{0} \rightarrow 1 \rightarrow \underline{0} \rightarrow 1 \rightarrow \underline{0} \rightarrow 1\). The robot worked \(8\) seconds and the point \(0\) is entered \(4\) times.In the third example, the robot moves as follows: \(-2 \rightarrow -3 \rightarrow -2 \rightarrow -1 \rightarrow \underline{0} \rightarrow -1\). The robot worked \(5\) seconds and the point \(0\) is entered once. In the fourth example, the robot moves as follows: \(3 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 3 \rightarrow 2\). The robot has completed all instructions from the sequence and is not at \(0\). So it stops after \(5\) seconds, without reaching the point \(0\).
Input: 63 2 6LLR2 -1 8RL4 -2 5LRRR5 3 7LRRLL1 1 1L3 -1 4846549234412827RLR | Output: 1 4 1 0 1 2423274617206414
Easy
3
831
452
132
20
755
E
755E
E. PolandBall and White-Red graph
2,400
constructive algorithms; graphs; shortest paths
PolandBall has an undirected simple graph consisting of n vertices. Unfortunately, it has no edges. The graph is very sad because of that. PolandBall wanted to make it happier, adding some red edges. Then, he will add white edges in every remaining place. Therefore, the final graph will be a clique in two colors: white and red. Colorfulness of the graph is a value min(dr, dw), where dr is the diameter of the red subgraph and dw is the diameter of white subgraph. The diameter of a graph is a largest value d such that shortest path between some pair of vertices in it is equal to d. If the graph is not connected, we consider its diameter to be -1.PolandBall wants the final graph to be as neat as possible. He wants the final colorfulness to be equal to k. Can you help him and find any graph which satisfies PolandBall's requests?
The only one input line contains two integers n and k (2 ≀ n ≀ 1000, 1 ≀ k ≀ 1000), representing graph's size and sought colorfulness.
If it's impossible to find a suitable graph, print -1.Otherwise, you can output any graph which fulfills PolandBall's requirements. First, output m β€” the number of red edges in your graph. Then, you should output m lines, each containing two integers ai and bi, (1 ≀ ai, bi ≀ n, ai β‰  bi) which means that there is an undirected red edge between vertices ai and bi. Every red edge should be printed exactly once, you can print the edges and the vertices of every edge in arbitrary order.Remember that PolandBall's graph should remain simple, so no loops or multiple edges are allowed.
In the first sample case, no graph can fulfill PolandBall's requirements.In the second sample case, red graph is a path from 1 to 5. Its diameter is 4. However, white graph has diameter 2, because it consists of edges 1-3, 1-4, 1-5, 2-4, 2-5, 3-5.
Input: 4 1 | Output: -1
Expert
3
836
134
583
7
1,251
F
1251F
F. Red-White Fence
2,500
combinatorics; fft
Polycarp wants to build a fence near his house. He has \(n\) white boards and \(k\) red boards he can use to build it. Each board is characterised by its length, which is an integer.A good fence should consist of exactly one red board and several (possibly zero) white boards. The red board should be the longest one in the fence (every white board used in the fence should be strictly shorter), and the sequence of lengths of boards should be ascending before the red board and descending after it. Formally, if \(m\) boards are used, and their lengths are \(l_1\), \(l_2\), ..., \(l_m\) in the order they are placed in the fence, from left to right (let's call this array \([l_1, l_2, \dots, l_m]\) the array of lengths), the following conditions should hold: there should be exactly one red board in the fence (let its index be \(j\)); for every \(i \in [1, j - 1]\) \(l_i < l_{i + 1}\); for every \(i \in [j, m - 1]\) \(l_i > l_{i + 1}\). When Polycarp will build his fence, he will place all boards from left to right on the same height of \(0\), without any gaps, so these boards compose a polygon: Example: a fence with \([3, 5, 4, 2, 1]\) as the array of lengths. The second board is red. The perimeter of the fence is \(20\). Polycarp is interested in fences of some special perimeters. He has \(q\) even integers he really likes (these integers are \(Q_1\), \(Q_2\), ..., \(Q_q\)), and for every such integer \(Q_i\), he wants to calculate the number of different fences with perimeter \(Q_i\) he can build (two fences are considered different if their arrays of lengths are different). Can you help him calculate these values?
The first line contains two integers \(n\) and \(k\) (\(1 \le n \le 3 \cdot 10^5\), \(1 \le k \le 5\)) β€” the number of white and red boards Polycarp has.The second line contains \(n\) integers \(a_1\), \(a_2\), ..., \(a_n\) (\(1 \le a_i \le 3 \cdot 10^5\)) β€” the lengths of white boards Polycarp has.The third line contains \(k\) integers \(b_1\), \(b_2\), ..., \(b_k\) (\(1 \le b_i \le 3 \cdot 10^5\)) β€” the lengths of red boards Polycarp has. All \(b_i\) are distinct.The fourth line contains one integer \(q\) (\(1 \le q \le 3 \cdot 10^5\)) β€” the number of special integers.The fifth line contains \(q\) integers \(Q_1\), \(Q_2\), ..., \(Q_q\) (\(4 \le Q_i \le 12 \cdot 10^5\), every \(Q_i\) is even) β€” the special integers Polycarp likes.
For each \(Q_i\), print one integer β€” the number of good fences with perimeter \(Q_i\) Polycarp can build, taken modulo \(998244353\).
Possible fences in the first example denoted by their arrays of lengths (the length of the red board is highlighted): with perimeter \(6\): \([\textbf{2}]\); with perimeter \(8\): \([1, \textbf{2}]\), \([\textbf{2}, 1]\); with perimeter \(10\): \([1, \textbf{2}, 1]\), \([\textbf{4}]\); with perimeter \(12\): \([1, \textbf{4}]\), \([3, \textbf{4}]\), \([\textbf{4}, 1]\), \([\textbf{4}, 3]\); with perimeter \(14\): \([1, \textbf{4}, 1]\), \([1, \textbf{4}, 3]\), \([3, \textbf{4}, 1]\), \([3, \textbf{4}, 3]\), \([1, 3, \textbf{4}]\), \([\textbf{4}, 3, 1]\); with perimeter \(16\): \([1, \textbf{4}, 3, 1]\), \([3, \textbf{4}, 3, 1]\), \([1, 3, \textbf{4}, 1]\), \([1, 3, \textbf{4}, 3]\); with perimeter \(18\): \([1, 3, \textbf{4}, 3, 1]\).
Input: 5 2 3 3 1 1 1 2 4 7 6 8 10 12 14 16 18 | Output: 1 2 2 4 6 4 1
Expert
2
1,637
742
134
12
1,191
A
1191A
A. Tokitsukaze and Enhancement
800
brute force
Tokitsukaze is one of the characters in the game ""Kantai Collection"". In this game, every character has a common attribute β€” health points, shortened to HP.In general, different values of HP are grouped into \(4\) categories: Category \(A\) if HP is in the form of \((4 n + 1)\), that is, when divided by \(4\), the remainder is \(1\); Category \(B\) if HP is in the form of \((4 n + 3)\), that is, when divided by \(4\), the remainder is \(3\); Category \(C\) if HP is in the form of \((4 n + 2)\), that is, when divided by \(4\), the remainder is \(2\); Category \(D\) if HP is in the form of \(4 n\), that is, when divided by \(4\), the remainder is \(0\). The above-mentioned \(n\) can be any integer.These \(4\) categories ordered from highest to lowest as \(A > B > C > D\), which means category \(A\) is the highest and category \(D\) is the lowest.While playing the game, players can increase the HP of the character. Now, Tokitsukaze wants you to increase her HP by at most \(2\) (that is, either by \(0\), \(1\) or \(2\)). How much should she increase her HP so that it has the highest possible category?
The only line contains a single integer \(x\) (\(30 \leq x \leq 100\)) β€” the value Tokitsukaze's HP currently.
Print an integer \(a\) (\(0 \leq a \leq 2\)) and an uppercase letter \(b\) (\(b \in \lbrace A, B, C, D \rbrace\)), representing that the best way is to increase her HP by \(a\), and then the category becomes \(b\).Note that the output characters are case-sensitive.
For the first example, the category of Tokitsukaze's HP is already \(A\), so you don't need to enhance her ability.For the second example: If you don't increase her HP, its value is still \(98\), which equals to \((4 \times 24 + 2)\), and its category is \(C\). If you increase her HP by \(1\), its value becomes \(99\), which equals to \((4 \times 24 + 3)\), and its category becomes \(B\). If you increase her HP by \(2\), its value becomes \(100\), which equals to \((4 \times 25)\), and its category becomes \(D\). Therefore, the best way is to increase her HP by \(1\) so that the category of her HP becomes \(B\).
Input: 33 | Output: 0 A
Beginner
1
1,116
110
265
11
549
A
549A
A. Face Detection
900
implementation; strings
The developers of Looksery have to write an efficient algorithm that detects faces on a picture. Unfortunately, they are currently busy preparing a contest for you, so you will have to do it for them. In this problem an image is a rectangular table that consists of lowercase Latin letters. A face on the image is a 2 Γ— 2 square, such that from the four letters of this square you can make word ""face"". You need to write a program that determines the number of faces on the image. The squares that correspond to the faces can overlap.
The first line contains two space-separated integers, n and m (1 ≀ n, m ≀ 50) β€” the height and the width of the image, respectively.Next n lines define the image. Each line contains m lowercase Latin letters.
In the single line print the number of faces on the image.
In the first sample the image contains a single face, located in a square with the upper left corner at the second line and the second column: In the second sample the image also contains exactly one face, its upper left corner is at the second row and the first column.In the third sample two faces are shown: In the fourth sample the image has no faces on it.
Input: 4 4xxxxxfaxxcexxxxx | Output: 1
Beginner
2
536
208
58
5
546
D
546D
D. Soldier and Number Game
1,700
constructive algorithms; dp; math; number theory
Two soldiers are playing a game. At the beginning first of them chooses a positive integer n and gives it to the second soldier. Then the second one tries to make maximum possible number of rounds. Each round consists of choosing a positive integer x > 1, such that n is divisible by x and replacing n with n / x. When n becomes equal to 1 and there is no more possible valid moves the game is over and the score of the second soldier is equal to the number of rounds he performed.To make the game more interesting, first soldier chooses n of form a! / b! for some positive integer a and b (a β‰₯ b). Here by k! we denote the factorial of k that is defined as a product of all positive integers not large than k.What is the maximum possible score of the second soldier?
First line of input consists of single integer t (1 ≀ t ≀ 1 000 000) denoting number of games soldiers play.Then follow t lines, each contains pair of integers a and b (1 ≀ b ≀ a ≀ 5 000 000) defining the value of n for a game.
For each game output a maximum score that the second soldier can get.
Input: 23 16 3 | Output: 25
Medium
4
767
227
69
5
164
C
164C
C. Machine Programming
2,400
flows; graphs
One remarkable day company ""X"" received k machines. And they were not simple machines, they were mechanical programmers! This was the last unsuccessful step before switching to android programmers, but that's another story.The company has now n tasks, for each of them we know the start time of its execution si, the duration of its execution ti, and the company profit from its completion ci. Any machine can perform any task, exactly one at a time. If a machine has started to perform the task, it is busy at all moments of time from si to si + ti - 1, inclusive, and it cannot switch to another task.You are required to select a set of tasks which can be done with these k machines, and which will bring the maximum total profit.
The first line contains two integer numbers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ 50) β€” the numbers of tasks and machines, correspondingly.The next n lines contain space-separated groups of three integers si, ti, ci (1 ≀ si, ti ≀ 109, 1 ≀ ci ≀ 106), si is the time where they start executing the i-th task, ti is the duration of the i-th task and ci is the profit of its execution.
Print n integers x1, x2, ..., xn. Number xi should equal 1, if task i should be completed and otherwise it should equal 0.If there are several optimal solutions, print any of them.
In the first sample the tasks need to be executed at moments of time 2 ... 8, 1 ... 3 and 4 ... 4, correspondingly. The first task overlaps with the second and the third ones, so we can execute either task one (profit 5) or tasks two and three (profit 6).
Input: 3 12 7 51 3 34 1 3 | Output: 0 1 1
Expert
2
734
374
180
1
1,725
D
1725D
D. Deducing Sortability
2,900
binary search; bitmasks; math
Let's say Pak Chanek has an array \(A\) consisting of \(N\) positive integers. Pak Chanek will do a number of operations. In each operation, Pak Chanek will do the following: Choose an index \(p\) (\(1 \leq p \leq N\)). Let \(c\) be the number of operations that have been done on index \(p\) before this operation. Decrease the value of \(A_p\) by \(2^c\). Multiply the value of \(A_p\) by \(2\). After each operation, all elements of \(A\) must be positive integers.An array \(A\) is said to be sortable if and only if Pak Chanek can do zero or more operations so that \(A_1 < A_2 < A_3 < A_4 < \ldots < A_N\).Pak Chanek must find an array \(A\) that is sortable with length \(N\) such that \(A_1 + A_2 + A_3 + A_4 + \ldots + A_N\) is the minimum possible. If there are more than one possibilities, Pak Chanek must choose the array that is lexicographically minimum among them.Pak Chanek must solve the following things: Pak Chanek must print the value of \(A_1 + A_2 + A_3 + A_4 + \ldots + A_N\) for that array. \(Q\) questions will be given. For the \(i\)-th question, an integer \(P_i\) is given. Pak Chanek must print the value of \(A_{P_i}\). Help Pak Chanek solve the problem.Note: an array \(B\) of size \(N\) is said to be lexicographically smaller than an array \(C\) that is also of size \(N\) if and only if there exists an index \(i\) such that \(B_i < C_i\) and for each \(j < i\), \(B_j = C_j\).
The first line contains two integers \(N\) and \(Q\) (\(1 \leq N \leq 10^9\), \(0 \leq Q \leq \min(N, 10^5)\)) β€” the required length of array \(A\) and the number of questions.The \(i\)-th of the next \(Q\) lines contains a single integer \(P_i\) (\(1 \leq P_1 < P_2 < \ldots < P_Q \leq N\)) β€” the index asked in the \(i\)-th question.
Print \(Q+1\) lines. The \(1\)-st line contains an integer representing \(A_1 + A_2 + A_3 + A_4 + \ldots + A_N\). For each \(1 \leq i \leq Q\), the \((i+1)\)-th line contains an integer representing \(A_{P_i}\).
In the first example, the array \(A\) obtained is \([1, 2, 3, 3, 4, 4]\). We can see that the array is sortable by doing the following operations: Choose index \(5\), then \(A = [1, 2, 3, 3, 6, 4]\). Choose index \(6\), then \(A = [1, 2, 3, 3, 6, 6]\). Choose index \(4\), then \(A = [1, 2, 3, 4, 6, 6]\). Choose index \(6\), then \(A = [1, 2, 3, 4, 6, 8]\).
Input: 6 3 1 4 5 | Output: 17 1 3 4
Master
3
1,411
335
211
17
2,002
H
2002H
H. Counting 101
3,500
greedy
It's been a long summer's day, with the constant chirping of cicadas and the heat which never seemed to end. Finally, it has drawn to a close. The showdown has passed, the gates are open, and only a gentle breeze is left behind.Your predecessors had taken their final bow; it's your turn to take the stage.Sorting through some notes that were left behind, you found a curious statement named Problem 101: Given a positive integer sequence \(a_1,a_2,\ldots,a_n\), you can operate on it any number of times. In an operation, you choose three consecutive elements \(a_i,a_{i+1},a_{i+2}\), and merge them into one element \(\max(a_i+1,a_{i+1},a_{i+2}+1)\). Please calculate the maximum number of operations you can do without creating an element greater than \(m\). After some thought, you decided to propose the following problem, named Counting 101: Given \(n\) and \(m\). For each \(k=0,1,\ldots,\left\lfloor\frac{n-1}{2}\right\rfloor\), please find the number of integer sequences \(a_1,a_2,\ldots,a_n\) with elements in \([1, m]\), such that when used as input for Problem 101, the answer is \(k\). As the answer can be very large, please print it modulo \(10^9+7\).
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1\le t\le10^3\)). The description of the test cases follows. The only line of each test case contains two integers \(n\), \(m\) (\(1\le n\le 130\), \(1\le m\le 30\)).
For each test case, output \(\left\lfloor\frac{n+1}{2}\right\rfloor\) numbers. The \(i\)-th number is the number of valid sequences such that when used as input for Problem 101, the answer is \(i-1\), modulo \(10^9+7\).
In the first test case, there are \(2^3=8\) candidate sequences. Among them, you can operate on \([1,2,1]\) and \([1,1,1]\) once; you cannot operate on the other \(6\) sequences.
Input: 23 210 10 | Output: 6 2 1590121 23399118 382293180 213020758 379696760
Master
1
1,167
264
219
20
1,639
J
1639J
J. 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
386
C
386C
C. Diverse Substrings
2,000
dp; strings; two pointers
String diversity is the number of symbols that occur in the string at least once. Diversity of s will be denoted by d(s). For example , d(""aaa"")=1, d(""abacaba"")=3.Given a string s, consisting of lowercase Latin letters. Consider all its substrings. Obviously, any substring diversity is a number from 1 to d(s). Find statistics about substrings diversity: for each k from 1 to d(s), find how many substrings of s has a diversity of exactly k.
The input consists of a single line containing s. It contains only lowercase Latin letters, the length of s is from 1 to 3Β·105.
Print to the first line the value d(s). Print sequence t1, t2, ..., td(s) to the following lines, where ti is the number of substrings of s having diversity of exactly i.
Consider the first example.We denote by s(i, j) a substring of ""abca"" with the indices in the segment [i, j]. s(1, 1) = ""a"", d(""a"") = 1 s(2, 2) = ""b"", d(""b"") = 1 s(3, 3) = ""c"", d(""c"") = 1 s(4, 4) = ""a"", d(""a"") = 1 s(1, 2) = ""ab"", d(""ab"") = 2 s(2, 3) = ""bc"", d(""bc"") = 2 s(3, 4) = ""ca"", d(""ca"") = 2 s(1, 3) = ""abc"", d(""abc"") = 3 s(2, 4) = ""bca"", d(""bca"") = 3 s(1, 4) = ""abca"", d(""abca"") = 3 Total number of substring with diversity 1 is 4, with diversity 2 equals 3, 3 diversity is 3.
Input: abca | Output: 3433
Hard
3
446
127
170
3
377
E
377E
E. Cookie Clicker
2,800
dp; geometry
Kostya is playing the computer game Cookie Clicker. The goal of this game is to gather cookies. You can get cookies using different buildings: you can just click a special field on the screen and get the cookies for the clicks, you can buy a cookie factory, an alchemy lab, a time machine and it all will bring lots and lots of cookies.At the beginning of the game (time 0), Kostya has 0 cookies and no buildings. He has n available buildings to choose from: the i-th building is worth ci cookies and when it's built it brings vi cookies at the end of each second. Also, to make the game more interesting to play, Kostya decided to add a limit: at each moment of time, he can use only one building. Of course, he can change the active building each second at his discretion.It's important that Kostya is playing a version of the game where he can buy new buildings and change active building only at time moments that are multiples of one second. Kostya can buy new building and use it at the same time. If Kostya starts to use a building at the time moment t, he can get the first profit from it only at the time moment t + 1.Kostya wants to earn at least s cookies as quickly as possible. Determine the number of seconds he needs to do that.
The first line contains two integers n and s (1 ≀ n ≀ 2Β·105, 1 ≀ s ≀ 1016) β€” the number of buildings in the game and the number of cookies Kostya wants to earn.Each of the next n lines contains two integers vi and ci (1 ≀ vi ≀ 108, 0 ≀ ci ≀ 108) β€” the number of cookies the i-th building brings per second and the building's price.
Output the only integer β€” the minimum number of seconds Kostya needs to earn at least s cookies. It is guaranteed that he can do it.
Input: 3 91 02 35 4 | Output: 6
Master
2
1,243
331
132
3
1,545
E1
1545E1
E1. AquaMoon and Time Stop (easy version)
3,500
data structures; dp
Note that the differences between easy and hard versions are the constraints on \(n\) and the time limit. You can make hacks only if both versions are solved.AquaMoon knew through foresight that some ghosts wanted to curse tourists on a pedestrian street. But unfortunately, this time, these ghosts were hiding in a barrier, and she couldn't enter this barrier in a short time and destroy them. Therefore, all that can be done is to save any unfortunate person on the street from the ghosts.The pedestrian street can be represented as a one-dimensional coordinate system. There is one person hanging out on the pedestrian street. At the time \(0\) he is at coordinate \(x\), moving with a speed of \(1\) unit per second. In particular, at time \(i\) the person will be at coordinate \(x+i\).The ghosts are going to cast \(n\) curses on the street. The \(i\)-th curse will last from time \(tl_i-1+10^{-18}\) to time \(tr_i+1-10^{-18}\) (exclusively) and will kill people with coordinates from \(l_i-1+10^{-18}\) to \(r_i+1-10^{-18}\) (exclusively). Formally that means, that the person, whose coordinate is between \((l_i-1+10^{-18},r_i+1-10^{-18})\) in the time range \((tl_i-1+10^{-18},tr_i+1-10^{-18})\) will die.To save the person on the street, AquaMoon can stop time at any moment \(t\), and then move the person from his current coordinate \(x\) to any coordinate \(y\) (\(t\), \(x\) and \(y\) are not necessarily integers). The movement costs AquaMoon \(|x-y|\) energy. The movement is continuous, so if there exists some cursed area between points \(x\) and \(y\) at time \(t\), the person will die too.AquaMoon wants to know what is the minimum amount of energy she needs to spend in order to save the person on the street from all \(n\) curses. But she is not good at programming. As her friend, can you help her?
The first line contains a single integer \(n\) (\(1\le n\le 2000\)) β€” the number of curses.The next line contains a single integer \(x\) (\(1\le x\le 10^6\)) β€” the initial coordinate of the person.The following \(n\) lines contain four integers \(tl_i\), \(tr_i\), \(l_i\), \(r_i\) each (\(1\le tl_i\le tr_i\le 10^6\), \(1\le l_i\le r_i\le 10^6\)).
Print a single integer β€” the minimum energy which AquaMoon needs to spent, rounded up to the nearest integer (in case there are two nearest integers you should round the answer to the highest of them).
Input: 2 1 1 2 1 2 2 3 2 3 | Output: 2
Master
2
1,823
348
201
15
2,126
F
2126F
F. 1-1-1, Free Tree!
2,000
brute force; data structures; dfs and similar; graphs; implementation; trees
Given a tree\(^{\text{βˆ—}}\) with \(n\) vertices numbered from \(1\) to \(n\). Each vertex has an initial color \(a_i\).Each edge of the tree is defined by three numbers: \(u_i\), \(v_i\), and \(c_i\), where \(u_i\) and \(v_i\) are the endpoints of the edge, and \(c_i\) is the edge parameter. The cost of the edge is defined as follows: if the colors of vertices \(u_i\) and \(v_i\) are the same, the cost is \(0\); otherwise, the cost is \(c_i\).You are also given \(q\) queries. Each query has the form: repaint vertex \(v\) to color \(x\). The queries depend on each other (after each query, the color change is preserved). After each query, you need to output the sum of the costs of all edges in the tree.\(^{\text{βˆ—}}\)A tree is a connected graph without cycles.
The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The first line of each test case contains two integers \(n\) and \(q\) (\(1 \le n, q \le 2\cdot10^5\)) β€” the number of vertices and the number of queries, respectively.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le n\)), where the \(i\)-th number specifies the initial color of vertex \(i\).The next \(n-1\) lines describe the edges of the tree. Each line contains three integers \(u\), \(v\), and \(c\), denoting an edge between vertices \(u\) and \(v\) with parameter \(c\) (\(1 \le u, v \le n\), \(1 \le c \le 10^9\)).The following \(q\) lines contain the queries. Each query contains two integers \(v\) and \(x\) β€” repaint vertex \(v\) to color \(x\) (\(1 \le v,x \le n\)).It is guaranteed that the sum of \(n\) and the sum of \(q\) across all test cases do not exceed \(2\cdot10^5\).
For each query, output a single integer on a separate line β€” the sum of the costs of all edges in the tree after applying the corresponding query.
First test: \(n =1\), one vertex β€” no edges. Query: repaint \(a_1\) to \(1\), the sum of costs is \(0\).Second test: \(n=2\), edge \(1 - 2\) (\(c=10\)). Queries: \(a_1 = 2\): colors [\(2, 1\)], cost is \(10\); \(a_2 = 2\): colors [\(2, 2\)], cost \(0\); \(a_1 = 1\): colors [\(1, 2\)], cost \(10\). Third test: \(n=5\), edges: \(1 - 2\ (c=5)\), \(2 - 3\ (c=3)\), \(2 - 4\ (c=4)\), \(4 - 5\ (c=7)\). Initial colors \([1,2,1,2,3]\). Queries:\(a_3 = 2 \rightarrow [1,2,2,2,3]\): edges \(1 - 2\ (c = 5)\) and \(4 - 5\ (c=7)\) give \(12\);\(a_5 = 2 \rightarrow [1,2,2,2,2]\): edge \(1 - 2\ (c=5)\), cost \(5\);\(a_1 = 2 \rightarrow [2,2,2,2,2]\): cost is \(0\);\(a_2 = 3 \rightarrow [2,3,2,2,2]\): edges \(1-2\ (5)\), \(2-3\ (3)\), \(2-4\ (4)\) give \(12\).
Input: 41 111 12 31 11 2 101 22 21 15 41 2 1 2 31 2 52 3 32 4 44 5 73 25 21 22 34 31 1 2 21 2 22 3 62 4 83 14 12 2 | Output: 0 10 0 10 12 5 0 12 8 0 16
Hard
6
768
914
146
21
987
C
987C
C. Three displays
1,400
brute force; dp; implementation
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.There are \(n\) displays placed along a road, and the \(i\)-th of them can display a text with font size \(s_i\) only. Maria Stepanovna wants to rent such three displays with indices \(i < j < k\) that the font size increases if you move along the road in a particular direction. Namely, the condition \(s_i < s_j < s_k\) should be held.The rent cost is for the \(i\)-th display is \(c_i\). Please determine the smallest cost Maria Stepanovna should pay.
The first line contains a single integer \(n\) (\(3 \le n \le 3\,000\)) β€” the number of displays.The second line contains \(n\) integers \(s_1, s_2, \ldots, s_n\) (\(1 \le s_i \le 10^9\)) β€” the font sizes on the displays in the order they stand along the road.The third line contains \(n\) integers \(c_1, c_2, \ldots, c_n\) (\(1 \le c_i \le 10^8\)) β€” the rent costs for each display.
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer β€” the minimum total rent cost of three displays with indices \(i < j < k\) such that \(s_i < s_j < s_k\).
In the first example you can, for example, choose displays \(1\), \(4\) and \(5\), because \(s_1 < s_4 < s_5\) (\(2 < 4 < 10\)), and the rent cost is \(40 + 10 + 40 = 90\).In the second example you can't select a valid triple of indices, so the answer is -1.
Input: 52 4 5 4 1040 30 20 10 40 | Output: 90
Easy
3
628
384
206
9
1,992
A
1992A
A. Only Pluses
800
brute force; constructive algorithms; greedy; math; sortings
Kmes has written three integers \(a\), \(b\) and \(c\) in order to remember that he has to give Noobish_Monk \(a \times b \times c\) bananas.Noobish_Monk has found these integers and decided to do the following at most \(5\) times: pick one of these integers; increase it by \(1\). For example, if \(a = 2\), \(b = 3\) and \(c = 4\), then one can increase \(a\) three times by one and increase \(b\) two times. After that \(a = 5\), \(b = 5\), \(c = 4\). Then the total number of bananas will be \(5 \times 5 \times 4 = 100\).What is the maximum value of \(a \times b \times c\) Noobish_Monk can achieve with these operations?
Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases. The description of the test cases follows.The first and only line of each test case contains three integers \(a\), \(b\) and \(c\) (\(1 \le a, b, c \le 10\)) β€” Kmes's integers.
For each test case, output a single integer β€” the maximum amount of bananas Noobish_Monk can get.
Input: 22 3 410 1 10 | Output: 100 600
Beginner
5
626
323
97
19
1,325
F
1325F
F. Ehab's Last Theorem
2,500
constructive algorithms; dfs and similar; graphs; greedy
It's the year 5555. You have a graph, and you want to find a long cycle and a huge independent set, just because you can. But for now, let's just stick with finding either.Given a connected graph with \(n\) vertices, you can choose to either: find an independent set that has exactly \(\lceil\sqrt{n}\rceil\) vertices. find a simple cycle of length at least \(\lceil\sqrt{n}\rceil\). An independent set is a set of vertices such that no two of them are connected by an edge. A simple cycle is a cycle that doesn't contain any vertex twice. I have a proof you can always solve one of these problems, but it's too long to fit this margin.
The first line contains two integers \(n\) and \(m\) (\(5 \le n \le 10^5\), \(n-1 \le m \le 2 \cdot 10^5\)) β€” the number of vertices and edges in the graph.Each of the next \(m\) lines contains two space-separated integers \(u\) and \(v\) (\(1 \le u,v \le n\)) that mean there's an edge between vertices \(u\) and \(v\). It's guaranteed that the graph is connected and doesn't contain any self-loops or multiple edges.
If you choose to solve the first problem, then on the first line print ""1"", followed by a line containing \(\lceil\sqrt{n}\rceil\) distinct integers not exceeding \(n\), the vertices in the desired independent set.If you, however, choose to solve the second problem, then on the first line print ""2"", followed by a line containing one integer, \(c\), representing the length of the found cycle, followed by a line containing \(c\) distinct integers integers not exceeding \(n\), the vertices in the desired cycle, in the order they appear in the cycle.
In the first sample:Notice that you can solve either problem, so printing the cycle \(2-4-3-1-5-6\) is also acceptable.In the second sample:Notice that if there are multiple answers you can print any, so printing the cycle \(2-5-6\), for example, is acceptable.In the third sample:
Input: 6 6 1 3 3 4 4 2 2 6 5 6 5 1 | Output: 1 1 6 4
Expert
4
636
418
556
13
1,216
E1
1216E1
E1. Numerical Sequence (easy version)
1,900
binary search; brute force; math
The only difference between the easy and the hard versions is the maximum value of \(k\).You are given an infinite sequence of form ""112123123412345\(\dots\)"" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from \(1\) to \(1\), the second one β€” from \(1\) to \(2\), the third one β€” from \(1\) to \(3\), \(\dots\), the \(i\)-th block consists of all numbers from \(1\) to \(i\). So the first \(56\) elements of the sequence are ""11212312341234512345612345671234567812345678912345678910"". Elements of the sequence are numbered from one. For example, the \(1\)-st element of the sequence is \(1\), the \(3\)-rd element of the sequence is \(2\), the \(20\)-th element of the sequence is \(5\), the \(38\)-th element is \(2\), the \(56\)-th element of the sequence is \(0\).Your task is to answer \(q\) independent queries. In the \(i\)-th query you are given one integer \(k_i\). Calculate the digit at the position \(k_i\) of the sequence.
The first line of the input contains one integer \(q\) (\(1 \le q \le 500\)) β€” the number of queries.The \(i\)-th of the following \(q\) lines contains one integer \(k_i\) \((1 \le k_i \le 10^9)\) β€” the description of the corresponding query.
Print \(q\) lines. In the \(i\)-th line print one digit \(x_i\) \((0 \le x_i \le 9)\) β€” the answer to the query \(i\), i.e. \(x_i\) should be equal to the element at the position \(k_i\) of the sequence.
Answers on queries from the first example are described in the problem statement.
Input: 5 1 3 20 38 56 | Output: 1 2 5 2 0
Hard
3
1,020
242
203
12
448
B
448B
B. Suffix Structures
1,400
implementation; strings
Bizon the Champion isn't just a bison. He also is a favorite of the ""Bizons"" team.At a competition the ""Bizons"" got the following problem: ""You are given two distinct words (strings of English letters), s and t. You need to transform word s into word t"". The task looked simple to the guys because they know the suffix data structures well. Bizon Senior loves suffix automaton. By applying it once to a string, he can remove from this string any single character. Bizon Middle knows suffix array well. By applying it once to a string, he can swap any two characters of this string. The guys do not know anything about the suffix tree, but it can help them do much more. Bizon the Champion wonders whether the ""Bizons"" can solve the problem. Perhaps, the solution do not require both data structures. Find out whether the guys can solve the problem and if they can, how do they do it? Can they solve it either only with use of suffix automaton or only with use of suffix array or they need both structures? Note that any structure may be used an unlimited number of times, the structures may be used in any order.
The first line contains a non-empty word s. The second line contains a non-empty word t. Words s and t are different. Each word consists only of lowercase English letters. Each word contains at most 100 letters.
In the single line print the answer to the problem. Print ""need tree"" (without the quotes) if word s cannot be transformed into word t even with use of both suffix array and suffix automaton. Print ""automaton"" (without the quotes) if you need only the suffix automaton to solve the problem. Print ""array"" (without the quotes) if you need only the suffix array to solve the problem. Print ""both"" (without the quotes), if you need both data structures to solve the problem.It's guaranteed that if you can solve the problem only with use of suffix array, then it is impossible to solve it only with use of suffix automaton. This is also true for suffix automaton.
In the third sample you can act like that: first transform ""both"" into ""oth"" by removing the first character using the suffix automaton and then make two swaps of the string using the suffix array and get ""hot"".
Input: automatontomat | Output: automaton
Easy
2
1,120
211
668
4
207
C1
207C1
C1. Game with Two Trees
2,100
The Smart Beaver from ABBYY has come up with a new developing game for children. The Beaver thinks that this game will help children to understand programming better.The main object of the game is finite rooted trees, each of their edges contains some lowercase English letter. Vertices on any tree are always numbered sequentially from 1 to m, where m is the number of vertices in the tree. Before describing the actual game, let's introduce some definitions.We'll assume that the sequence of vertices with numbers v1, v2, ..., vk (k β‰₯ 1) is a forward path, if for any integer i from 1 to k - 1 vertex vi is a direct ancestor of vertex vi + 1. If we sequentially write out all letters from the the edges of the given path from v1 to vk, we get some string (k = 1 gives us an empty string). We'll say that such string corresponds to forward path v1, v2, ..., vk.We'll assume that the sequence of tree vertices with numbers v1, v2, ..., vk (k β‰₯ 1) is a backward path if for any integer i from 1 to k - 1 vertex vi is the direct descendant of vertex vi + 1. If we sequentially write out all the letters from the edges of the given path from v1 to vk, we get some string (k = 1 gives us an empty string). We'll say that such string corresponds to backward path v1, v2, ..., vk.Now let's describe the game that the Smart Beaver from ABBYY has come up with. The game uses two rooted trees, each of which initially consists of one vertex with number 1. The player is given some sequence of operations. Each operation is characterized by three values (t, v, c) where: t is the number of the tree on which the operation is executed (1 or 2); v is the vertex index in this tree (it is guaranteed that the tree contains a vertex with this index); c is a lowercase English letter. The actual operation is as follows: vertex v of tree t gets a new descendant with number m + 1 (where m is the current number of vertices in tree t), and there should be letter c put on the new edge from vertex v to vertex m + 1.We'll say that an ordered group of three integers (i, j, q) is a good combination if: 1 ≀ i ≀ m1, where m1 is the number of vertices in the first tree; 1 ≀ j, q ≀ m2, where m2 is the number of vertices in the second tree; there exists a forward path v1, v2, ..., vk such that v1 = j and vk = q in the second tree; the string that corresponds to the forward path in the second tree from vertex j to vertex q equals the string that corresponds to the backward path in the first tree from vertex i to vertex 1 (note that both paths are determined uniquely). Your task is to calculate the number of existing good combinations after each operation on the trees.
The first line contains integer n β€” the number of operations on the trees. Next n lines specify the operations in the order of their execution. Each line has form ""t v c"", where t is the number of the tree, v is the vertex index in this tree, and c is a lowercase English letter.To get the full points for the first group of tests it is sufficient to solve the problem with 1 ≀ n ≀ 700.To get the full points for the second group of tests it is sufficient to solve the problem with 1 ≀ n ≀ 7000.To get the full points for the third group of tests it is sufficient to solve the problem with 1 ≀ n ≀ 100000.
Print exactly n lines, each containing one integer β€” the number of existing good combinations after the corresponding operation from the input.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.
After the first operation the only good combination was (1, 1, 1). After the second operation new good combinations appeared, (2, 1, 2) and (1, 2, 2). The third operation didn't bring any good combinations. The fourth operation added good combination (1, 3, 3). Finally, the fifth operation resulted in as much as three new good combinations β€” (1, 4, 4), (2, 3, 4) and (3, 1, 4).
Input: 51 1 a2 1 a1 2 b2 1 b2 3 a | Output: 13347
Hard
0
2,655
607
290
2
226
D
226D
D. The table
2,100
constructive algorithms; greedy
Harry Potter has a difficult homework. Given a rectangular table, consisting of n Γ— m cells. Each cell of the table contains the integer. Harry knows how to use two spells: the first spell change the sign of the integers in the selected row, the second β€” in the selected column. Harry's task is to make non-negative the sum of the numbers in each row and each column using these spells.Alone, the boy can not cope. Help the young magician!
The first line contains two integers n and m (1 ≀ n, m ≀ 100) β€” the number of rows and the number of columns. Next n lines follow, each contains m integers: j-th integer in the i-th line is ai, j (|ai, j| ≀ 100), the number in the i-th row and j-th column of the table.The rows of the table numbered from 1 to n. The columns of the table numbered from 1 to m.
In the first line print the number a β€” the number of required applications of the first spell. Next print a space-separated integers β€” the row numbers, you want to apply a spell. These row numbers must be distinct!In the second line print the number b β€” the number of required applications of the second spell. Next print b space-separated integers β€” the column numbers, you want to apply a spell. These column numbers must be distinct!If there are several solutions are allowed to print any of them.
Input: 4 1-1-1-1-1 | Output: 4 1 2 3 4 0
Hard
2
439
359
500
2
575
H
575H
H. Bots
1,800
combinatorics; dp; math; number theory
Sasha and Ira are two best friends. But they aren’t just friends, they are software engineers and experts in artificial intelligence. They are developing an algorithm for two bots playing a two-player game. The game is cooperative and turn based. In each turn, one of the players makes a move (it doesn’t matter which player, it's possible that players turns do not alternate). Algorithm for bots that Sasha and Ira are developing works by keeping track of the state the game is in. Each time either bot makes a move, the state changes. And, since the game is very dynamic, it will never go back to the state it was already in at any point in the past.Sasha and Ira are perfectionists and want their algorithm to have an optimal winning strategy. They have noticed that in the optimal winning strategy, both bots make exactly N moves each. But, in order to find the optimal strategy, their algorithm needs to analyze all possible states of the game (they haven’t learned about alpha-beta pruning yet) and pick the best sequence of moves.They are worried about the efficiency of their algorithm and are wondering what is the total number of states of the game that need to be analyzed?
The first and only line contains integer N. 1 ≀ N ≀ 106
Output should contain a single integer – number of possible states modulo 109 + 7.
Start: Game is in state A. Turn 1: Either bot can make a move (first bot is red and second bot is blue), so there are two possible states after the first turn – B and C. Turn 2: In both states B and C, either bot can again make a turn, so the list of possible states is expanded to include D, E, F and G. Turn 3: Red bot already did N=2 moves when in state D, so it cannot make any more moves there. It can make moves when in state E, F and G, so states I, K and M are added to the list. Similarly, blue bot cannot make a move when in state G, but can when in D, E and F, so states H, J and L are added. Turn 4: Red bot already did N=2 moves when in states H, I and K, so it can only make moves when in J, L and M, so states P, R and S are added. Blue bot cannot make a move when in states J, L and M, but only when in H, I and K, so states N, O and Q are added. Overall, there are 19 possible states of the game their algorithm needs to analyze.
Input: 2 | Output: 19
Medium
4
1,184
55
82
5
2,042
E
2042E
E. Vertex Pairs
2,900
binary search; brute force; data structures; dfs and similar; divide and conquer; greedy; implementation; trees
You are given a tree consisting of \(2n\) vertices. Recall that a tree is a connected undirected graph with no cycles. Each vertex has an integer from \(1\) to \(n\) written on it. Each value from \(1\) to \(n\) is written on exactly two different vertices. Each vertex also has a cost β€”vertex \(i\) costs \(2^i\).You need to choose a subset of vertices of the tree such that: the subset is connected; that is, from each vertex in the subset, you can reach every other vertex in the subset by passing only through the vertices in the subset; each value from \(1\) to \(n\) is written on at least one vertex in the subset. Among all such subsets, you need to find the one with the smallest total cost of the vertices in it. Note that you are not required to minimize the number of vertices in the subset.
The first line contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)).The second line contains \(2n\) integers \(a_1, a_2, \dots, a_{2n}\) (\(1 \le a_i \le n\)). Each value from \(1\) to \(n\) appears exactly twice.Each of the next \(2n-1\) lines contains two integers \(v\) and \(u\) (\(1 \le v, u \le 2n\)) β€” the edges of the tree. These edges form a valid tree.
In the first line, print a single integer \(k\) β€” the number of vertices in the subset.In the second line, print \(k\) distinct integers from \(1\) to \(2n\) β€” the indices of the vertices in the chosen subset. The vertices can be printed in an arbitrary order.
The images show the answers to the first two examples. The numbers in parentheses are the values written on the vertices.In the first example, there are valid subsets such as: \([2, 4, 5]\) (with a cost of \(2^2 + 2^4 + 2^5 = 52\)), \([2, 4, 5, 6]\) (with a cost of \(116\)), \([1, 6, 3]\) (with a cost of \(74\)), \([2, 6, 3]\) (with a cost of \(76\)), and many others.In the second example, the cost of the subset \([4, 6, 3, 1]\) is \(90\).
Input: 31 1 3 2 3 24 21 66 26 32 5 | Output: 3 2 4 5
Master
8
803
372
260
20
147
A
147A
A. Punctuation
1,300
implementation; strings
You are given a text that consists of lowercase Latin letters, spaces and punctuation marks (dot, comma, exclamation mark and question mark). A word is defined as a sequence of consecutive Latin letters.Your task is to add spaces to the text by the following rules: if there is no punctuation mark between two words, then they should be separated by exactly one space there should be no spaces before each punctuation mark there should be exactly one space after each punctuation mark It is guaranteed that there is at least one word between any two punctuation marks. The text begins and ends with a Latin letter.
The input data contains of a single non-empty line β€” the text whose length is no more than 10000 characters.
Print the text, edited according to the rules. In this problem you should follow the output format very strictly. For example, extra space at the end of the output line is considered as wrong answer. Note that a newline character at the end of the line doesn't matter.
Input: galileo galilei was an italian physicist ,mathematician,astronomer | Output: galileo galilei was an italian physicist, mathematician, astronomer
Easy
2
614
108
268
1
1,371
A
1371A
A. Magical Sticks
800
math
A penguin Rocher has \(n\) sticks. He has exactly one stick with length \(i\) for all \(1 \le i \le n\).He can connect some sticks. If he connects two sticks that have lengths \(a\) and \(b\), he gets one stick with length \(a + b\). Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections.He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create?
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases. Next \(t\) lines contain descriptions of test cases.For each test case, the only line contains a single integer \(n\) (\(1 \le n \le 10^{9}\)).
For each test case, print a single integer β€” the answer to the problem.
In the third case, he can connect two sticks with lengths \(1\) and \(2\) and he will get one stick with length \(3\). So, he will have two sticks with lengths \(3\).In the fourth case, he can connect two sticks with lengths \(1\) and \(3\) and he will get one stick with length \(4\). After that, he will have three sticks with lengths \(\{2, 4, 4\}\), so two sticks have the same length, and one stick has the other length.
Input: 4 1 2 3 4 | Output: 1 1 2 2
Beginner
1
616
284
71
13