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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
17 | C | 17C | C. Balance | 2,500 | dp | Nick likes strings very much, he likes to rotate them, sort them, rearrange characters within a string... Once he wrote a random string of characters a, b, c on a piece of paper and began to perform the following operations: to take two adjacent characters and replace the second character with the first one, to take tw... | The first line contains integer n (1 ≤ n ≤ 150) — the length of the given string s. Next line contains the given string s. The initial string can be balanced as well, in this case it should be counted too. The given string s consists only of characters a, b and c. | Output the only number — the number of different balanced strings that can be obtained by performing the described operations, perhaps multiple times, on the given string s, modulo 51123987. | In the first sample it is possible to get 51 different strings through the described operations, but only 7 of them are balanced: «abca», «bbca», «bcca», «bcaa», «abcc», «abbc», «aabc». In the second sample: «abbc», «aabc», «abcc». In the third sample there is only one balanced string — «ab» itself. | Input: 4abca | Output: 7 | Expert | 1 | 1,314 | 264 | 190 | 0 |
1,142 | C | 1142C | C. U2 | 2,400 | geometry | Recently Vasya learned that, given two points with different \(x\) coordinates, you can draw through them exactly one parabola with equation of type \(y = x^2 + bx + c\), where \(b\) and \(c\) are reals. Let's call such a parabola an \(U\)-shaped one.Vasya drew several distinct points with integer coordinates on a plan... | The first line contains a single integer \(n\) (\(1 \le n \le 100\,000\)) — the number of points.The next \(n\) lines describe the points, the \(i\)-th of them contains two integers \(x_i\) and \(y_i\) — the coordinates of the \(i\)-th point. It is guaranteed that all points are distinct and that the coordinates do not... | In the only line print a single integer — the number of \(U\)-shaped parabolas that pass through at least two of the given points and do not contain any of the given points inside their internal area (excluding the parabola itself). | On the pictures below all \(U\)-shaped parabolas that pass through at least two given points are drawn for each of the examples. The \(U\)-shaped parabolas that do not have any given point inside their internal area are drawn in red. The first example. The second example. | Input: 3 -1 0 0 2 1 0 | Output: 2 | Expert | 1 | 745 | 355 | 232 | 11 |
1,946 | F | 1946F | F. Nobody is needed | 2,500 | 2-sat; data structures; dfs and similar; dp | Oleg received a permutation \(a\) of length \(n\) as a birthday present.Oleg's friend Nechipor asks Oleg \(q\) questions, each question is characterized by two numbers \(l\) and \(r\), in response to the question Oleg must say the number of sets of indices \((t_1, t_2, \ldots, t_k)\) of any length \(k \ge 1\) such that... | Each test consists of several sets of input data. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of sets of input data. The description of the sets of input data follows.The first line of each set of input data contains two integers \(n\) and \(q\) (\(1 \le n, q \le 10^6\)) — the len... | For each set of input data, output the answers to all of Nechipor's questions. | All suitable arrays in the first set of input data: (\(1\)), (\(2\)), (\(3\)), (\(4\)), (\(5\)), (\(6\)), (\(7\)), (\(8\)), (\(1, 3\)), (\(1, 6\)), (\(1, 7\)), (\(1, 6, 7\)), (\(2, 3\)), (\(2, 4\)), (\(2, 5\)), (\(2, 6\)), (\(2, 7\)), (\(2, 8\)), (\(2, 6, 7\)), (\(6, 7\)).All suitable arrays in the fourth set of input ... | Input: 48 82 1 6 3 5 4 8 71 82 81 71 61 35 84 42 31 111 13 33 2 11 21 32 38 11 2 3 4 5 6 7 81 8 | Output: 20 15 18 12 5 5 1 3 1 2 3 2 27 | Expert | 4 | 565 | 816 | 78 | 19 |
1,910 | D | 1910D | D. Remove and Add | 1,800 | *special; greedy | You are given an array \(a_1, a_2, \dots, a_n\), consisting of \(n\) integers.You goal is to make is strictly increasing. To achieve that, you perform each of the following operations exactly once: first, remove any element; second, select any number of elements (possibly, none or all \(n-1\)) and add \(1\) to them. No... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of testcases.The first line of each testcase contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) — the number of elements of the array.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^6\)).T... | For each testcase, print YES if it's possible to remove one element and add \(1\) to some elements (possibly, none or all), so that the array becomes strictly increasing. Otherwise, print NO. | In the first testcase, you can remove the third element and add \(1\) to the second and the last element. \(a'\) will become \([4, 5, 6]\), which is strictly increasing.In the second testcase, there is no way to perform the operations, so that the result is strictly increasing.In the third testcase, you can remove eith... | Input: 844 4 1 554 4 1 5 5210 531 2 332 1 141 1 1 141 3 1 251 1 3 3 1 | Output: YES NO YES YES YES NO YES YES | Medium | 2 | 510 | 387 | 191 | 19 |
180 | E | 180E | E. Cubes | 1,800 | binary search; dp; two pointers | Let's imagine that you're playing the following simple computer game. The screen displays n lined-up cubes. Each cube is painted one of m colors. You are allowed to delete not more than k cubes (that do not necessarily go one after another). After that, the remaining cubes join together (so that the gaps are closed) an... | The first line contains three integers n, m and k (1 ≤ n ≤ 2·105, 1 ≤ m ≤ 105, 0 ≤ k < n). The second line contains n integers from 1 to m — the numbers of cube colors. The numbers of colors are separated by single spaces. | Print the maximum possible number of points you can score. | In the first sample you should delete the fifth and the sixth cubes.In the second sample you should delete the fourth and the seventh cubes.In the third sample you shouldn't delete any cubes. | Input: 10 3 21 2 1 1 3 2 1 1 2 2 | Output: 4 | Medium | 3 | 657 | 222 | 58 | 1 |
534 | C | 534C | C. Polycarpus' Dice | 1,600 | math | Polycarp has n dice d1, d2, ..., dn. The i-th dice shows numbers from 1 to di. Polycarp rolled all the dice and the sum of numbers they showed is A. Agrippina didn't see which dice showed what number, she knows only the sum A and the values d1, d2, ..., dn. However, she finds it enough to make a series of statements of... | The first line contains two integers n, A (1 ≤ n ≤ 2·105, n ≤ A ≤ s) — the number of dice and the sum of shown values where s = d1 + d2 + ... + dn.The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 106), where di is the maximum value that the i-th dice can show. | Print n integers b1, b2, ..., bn, where bi is the number of values for which it is guaranteed that the i-th dice couldn't show them. | In the first sample from the statement A equal to 8 could be obtained in the only case when both the first and the second dice show 4. Correspondingly, both dice couldn't show values 1, 2 or 3.In the second sample from the statement A equal to 3 could be obtained when the single dice shows 3. Correspondingly, it couldn... | Input: 2 84 4 | Output: 3 3 | Medium | 1 | 774 | 273 | 132 | 5 |
1,687 | B | 1687B | B. Railway System | 1,700 | constructive algorithms; graphs; greedy; interactive; sortings | As for the technology in the outside world, it is really too advanced for Gensokyo to even look up to.—Yasaka Kanako, Symposium of Post-mysticismThis is an interactive problem.Under the direct supervision of Kanako and the Moriya Shrine, the railway system of Gensokyo is finally finished. GSKR (Gensokyo Railways) consi... | The first and only line of input contains two integers \(n,m\) (\(2 \leq n \leq 200\), \(1\le m \le 500\)) — the number of stations and tracks. | Here is the graph of the example, satisfying \(l_i=i\). | Input: 5 4 0 5 9 7 | Output: ? 0000 ? 1110 ? 1111 ? 1101 ! 7 | Medium | 5 | 1,489 | 143 | 0 | 16 | |
1,201 | E2 | 1201E2 | E2. Knightmare (hard) | 3,000 | graphs; interactive; shortest paths | This is an interactive problem.Alice and Bob are playing a game on the chessboard of size \(n \times m\) where \(n\) and \(m\) are even. The rows are numbered from \(1\) to \(n\) and the columns are numbered from \(1\) to \(m\). There are two knights on the chessboard. A white one initially is on the position \((x_1, y... | In the first example, the white knight can reach it's target square in one move.In the second example black knight wins, no matter what white knight moves. | Input: 8 8 2 3 1 8 | Output: WHITE 4 4 | Master | 3 | 2,017 | 0 | 0 | 12 | ||
1,082 | F | 1082F | F. Speed Dial | 2,800 | dp; strings; trees | Polycarp's phone book contains \(n\) phone numbers, each of them is described by \(s_i\) — the number itself and \(m_i\) — the number of times Polycarp dials it in daily.Polycarp has just bought a brand new phone with an amazing speed dial feature! More precisely, \(k\) buttons on it can have a number assigned to it (n... | The first line contains two integers \(n\) and \(k\) (\(1 \le n \le 500\), \(1 \le k \le 10\)) — the amount of numbers in Polycarp's phone book and the number of speed dial buttons his new phone has.The \(i\)-th of the next \(n\) lines contain a string \(s_i\) and an integer \(m_i\) \((1 \le m_i \le 500)\), where \(s_i... | Print a single integer — the minimal total number of digit number presses Polycarp can achieve after he assigns numbers to speed dial buttons and enters each of the numbers from his phone book the given number of times in an optimal way. | The only speed dial button in the first example should have ""0001"" on it. The total number of digit button presses will be \(0 \cdot 5\) for the first number + \(3 \cdot 4\) for the second + \(2 \cdot 1\) for the third. \(14\) in total.The only speed dial button in the second example should have ""00"" on it. The tot... | Input: 3 1 0001 5 001 4 01 1 | Output: 14 | Master | 3 | 860 | 559 | 237 | 10 |
1,149 | A | 1149A | A. Prefix Sum Primes | 1,200 | constructive algorithms; greedy; math; number theory | We're giving away nice huge bags containing number tiles! A bag we want to present to you contains \(n\) tiles. Each of them has a single number written on it — either \(1\) or \(2\).However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequ... | The first line of the input contains a single integer \(n\) (\(1 \leq n \leq 200\,000\)) — the number of number tiles in the bag. The following line contains \(n\) space-separated integers \(a_1, a_2, \dots, a_n\) (\(a_i \in \{1, 2\}\)) — the values written on the tiles. | Output a permutation \(b_1, b_2, \dots, b_n\) of the input sequence \((a_1, a_2, \dots, a_n)\) maximizing the number of the prefix sums being prime numbers. If there are multiple optimal permutations, output any. | The first solution produces the prefix sums \(1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, \mathbf{\color{blue}{7}}\) (four primes constructed), while the prefix sums in the second solution are \(1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, 6, \mat... | Input: 5 1 2 1 2 1 | Output: 1 1 1 2 2 | Easy | 4 | 609 | 271 | 212 | 11 |
1,342 | B | 1342B | B. Binary Period | 1,100 | constructive algorithms; strings | Let's say string \(s\) has period \(k\) if \(s_i = s_{i + k}\) for all \(i\) from \(1\) to \(|s| - k\) (\(|s|\) means length of string \(s\)) and \(k\) is the minimum positive integer with this property.Some examples of a period: for \(s\)=""0101"" the period is \(k=2\), for \(s\)=""0000"" the period is \(k=1\), for \(... | The first line contains single integer \(T\) (\(1 \le T \le 100\)) — the number of test cases.Next \(T\) lines contain test cases — one per line. Each line contains string \(t\) (\(1 \le |t| \le 100\)) consisting only of 0's and 1's. | Print one string for each test case — string \(s\) you needed to find. If there are multiple solutions print any one of them. | In the first and second test cases, \(s = t\) since it's already one of the optimal solutions. Answers have periods equal to \(1\) and \(2\), respectively.In the third test case, there are shorter optimal solutions, but it's okay since we don't need to minimize the string \(s\). String \(s\) has period equal to \(1\). | Input: 4 00 01 111 110 | Output: 00 01 11111 1010 | Easy | 2 | 970 | 233 | 125 | 13 |
11 | E | 11E | E. Forward, march! | 2,800 | binary search; dp; greedy | Jack has become a soldier now. Unfortunately, he has trouble with the drill. Instead of marching beginning with the left foot and then changing legs with each step, as ordered, he keeps repeating a sequence of steps, in which he sometimes makes the wrong steps or — horror of horrors! — stops for a while. For example, i... | The first line of input contains a sequence consisting only of characters 'L', 'R' and 'X', where 'L' corresponds to a step with the left foot, 'R' — with the right foot, and 'X' — to a break. The length of the sequence will not exceed 106. | Output the maximum percentage of time that Jack can spend marching correctly, rounded down to exactly six digits after the decimal point. | In the second example, if we add two breaks to receive LXXRXR, Jack will march: LXXRXRLXXRXRL... instead of LRLRLRLRLRLRL... and will make the correct step in half the cases. If we didn't add any breaks, the sequence would be incorrect — Jack can't step on his right foot twice in a row. | Input: X | Output: 0.000000 | Master | 3 | 1,652 | 240 | 137 | 0 |
1,669 | D | 1669D | D. Colorful Stamp | 1,100 | implementation | A row of \(n\) cells is given, all initially white. Using a stamp, you can stamp any two neighboring cells such that one becomes red and the other becomes blue. A stamp can be rotated, i.e. it can be used in both ways: as \(\color{blue}{\texttt{B}}\color{red}{\texttt{R}}\) and as \(\color{red}{\texttt{R}}\color{blue}{\... | The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) — the number of test cases.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 10^5\)) — the length of the picture.The second line of each test case contains a string \(s\) — the picture you need to make. It is guaranteed that the... | Output \(t\) lines, each of which contains the answer to the corresponding test case. As an answer, output ""YES"" if it possible to make the picture using the stamp zero or more times, and ""NO"" otherwise.You can output the answer in any case (for example, the strings ""yEs"", ""yes"", ""Yes"" and ""YES"" will be rec... | The first test case is explained in the statement.For the second, third, and fourth test cases, it is not possible to stamp a single cell, so the answer is ""NO"".For the fifth test case, you can use the stamp as follows: \(\texttt{WWW} \to \texttt{W}\color{brown}{\underline{\color{red}{\texttt{R}}\color{blue}{\texttt{... | Input: 125BRBBW1B2WB2RW3BRB3RBB7WWWWWWW9RBWBWRRBW10BRBRBRBRRB12BBBRWWRRRWBR10BRBRBRBRBW5RBWBW | Output: YES NO NO NO YES YES YES NO YES NO YES NO | Easy | 1 | 1,436 | 583 | 350 | 16 |
54 | E | 54E | E. Vacuum Сleaner | 2,700 | geometry | One winter evening the Hedgehog was relaxing at home in his cozy armchair and clicking through the TV channels. Stumbled on an issue of «TopShop», the Hedgehog was about to change the channel when all of a sudden he was stopped by an advertisement of a new wondrous invention.Actually, a vacuum cleaner was advertised th... | The first line contains an integer N which represents the number of vertices of the vacuum cleaner's polygon (3 ≤ N ≤ 4·104). Then follow N lines each containing two numbers — the coordinates of a vertex of the polygon. All the coordinates are integer and their absolute values do not exceed 106.It is guaranteed that th... | Print the minimum possible uncovered area. The answer will be accepted if it is within 10 - 6 of absolute or relative error from the correct answer. | Input: 40 01 01 10 1 | Output: 0.00000000000000000000 | Master | 1 | 1,518 | 481 | 148 | 0 | |
7 | A | 7A | A. Kalevitch and Chess | 1,100 | brute force; constructive algorithms | A famous Berland's painter Kalevitch likes to shock the public. One of his last obsessions is chess. For more than a thousand years people have been playing this old game on uninteresting, monotonous boards. Kalevitch decided to put an end to this tradition and to introduce a new attitude to chessboards.As before, the ... | The input file contains 8 lines, each of the lines contains 8 characters. The given matrix describes the client's requirements, W character stands for a white square, and B character — for a square painted black.It is guaranteed that client's requirments can be fulfilled with a sequence of allowed strokes (vertical/col... | Output the only number — the minimum amount of rows and columns that Kalevitch has to paint on the white chessboard to meet the client's requirements. | Input: WWWBWWBWBBBBBBBBWWWBWWBWWWWBWWBWWWWBWWBWWWWBWWBWWWWBWWBWWWWBWWBW | Output: 3 | Easy | 2 | 1,568 | 343 | 150 | 0 | |
207 | B2 | 207B2 | B2. Military Trainings | 1,900 | The Smart Beaver from ABBYY started cooperating with the Ministry of Defence. Now they train soldiers to move armoured columns. The training involves testing a new type of tanks that can transmit information. To test the new type of tanks, the training has a special exercise, its essence is as follows.Initially, the co... | The first line contains integer n — the number of tanks in the column. Each of the next n lines contains one integer ai (1 ≤ ai ≤ 250000, 1 ≤ i ≤ n) — the message receiving radii of the tanks in the order from tank 1 to tank n (let us remind you that initially the tanks are located in the column in ascending order of t... | Print a single integer — the minimum possible total time of transmitting the messages.Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. | In the first sample the original order of tanks is 1, 2, 3. The first tank sends a message to the second one, then the second tank sends it to the third one — it takes two seconds. The third tank moves to the beginning of the column and the order of tanks now is 3, 1, 2. The third tank sends a message to the first one,... | Input: 3211 | Output: 5 | Hard | 0 | 2,856 | 661 | 233 | 2 | |
1,534 | H | 1534H | H. Lost Nodes | 3,500 | constructive algorithms; dp; graphs; interactive; sortings; trees | This is an interactive problem.As he qualified for IOI this year, Little Ericyi was given a gift from all his friends: a tree of \(n\) nodes!On the flight to IOI Little Ericyi was very bored, so he decided to play a game with Little Yvonne with his new tree. First, Little Yvonne selects two (not necessarily different) ... | Here is the the tree from the first sample interaction. Nodes \(a\) and \(b\) are highlighted. Notice that \(a\) and \(b\) can be output in any order.Additionally, here are the answers to querying every single node \(1,2,\ldots,n\) for your convenience: \(1\): \(1\) \(2\): \(2\) \(3\): \(2\) \(4\): \(4\)_______________... | Input: 4 3 2 2 1 2 4 1 1 2 2 | Output: 3 ? 1 ? 2 ? 3 ! 4 1 | Master | 6 | 1,396 | 0 | 0 | 15 | ||
1,672 | H | 1672H | H. Zigu Zagu | 2,700 | constructive algorithms; data structures; greedy | You have a binary string \(a\) of length \(n\) consisting only of digits \(0\) and \(1\). You are given \(q\) queries. In the \(i\)-th query, you are given two indices \(l\) and \(r\) such that \(1 \le l \le r \le n\). Let \(s=a[l,r]\). You are allowed to do the following operation on \(s\): Choose two indices \(x\) an... | The first line contains two integers \(n\) and \(q\) (\(1 \le n, q \le 2 \cdot 10 ^ 5\)) — the length of the binary string \(a\) and the number of queries respectively.The second line contains a binary string \(a\) of length \(n\) (\(a_i \in \{0, 1\}\)).Each of the next \(q\) lines contains two integers \(l\) and \(r\)... | Print \(q\) lines, the \(i\)-th line representing the minimum number of operations needed for the \(i\)-th query. | In the first test case, The substring is \(\texttt{101}\), so we can do one operation to make the substring empty. The substring is \(\texttt{11011}\), so we can do one operation on \(s[2, 4]\) to make \(\texttt{11}\), then use two more operations to make the substring empty. The substring is \(\texttt{011}\), so we ca... | Input: 5 3 11011 2 4 1 5 3 5 | Output: 1 3 2 | Master | 3 | 786 | 390 | 113 | 16 |
1,076 | G | 1076G | G. Array Game | 3,000 | data structures; games | Consider a following game between two players:There is an array \(b_1\), \(b_2\), ..., \(b_k\), consisting of positive integers. Initially a chip is placed into the first cell of the array, and \(b_1\) is decreased by \(1\). Players move in turns. Each turn the current player has to do the following: if the index of th... | The first line contains three integers \(n\), \(m\) and \(q\) (\(1 \le n, q \le 2 \cdot 10^5\), \(1 \le m \le 5\)) — the number of elements in \(a\), the parameter described in the game and the number of queries, respectively.The second line contains \(n\) integers \(a_1\), \(a_2\), ..., \(a_n\) (\(1 \le a_i \le 10^{12... | For each query of type \(2\) print \(1\) if the first player wins in the corresponding game, or \(2\) if the second player wins. | Input: 5 2 4 1 2 3 4 5 1 3 5 6 2 2 5 1 1 2 3 2 1 5 | Output: 1 1 | Master | 2 | 1,018 | 921 | 128 | 10 | |
1,044 | A | 1044A | A. The Tower is Going Home | 1,700 | binary search; two pointers | On a chessboard with a width of \(10^9\) and a height of \(10^9\), the rows are numbered from bottom to top from \(1\) to \(10^9\), and the columns are numbered from left to right from \(1\) to \(10^9\). Therefore, for each cell of the chessboard you can assign the coordinates \((x,y)\), where \(x\) is the column numbe... | The first line contains two integers \(n\) and \(m\) (\(0 \le n,m \le 10^5\)) — the number of vertical and horizontal spells.Each of the following \(n\) lines contains one integer \(x\) (\(1 \le x < 10^9\)) — the description of the vertical spell. It will create a blocking line between the columns of \(x\) and \(x+1\).... | In a single line print one integer — the minimum number of spells the rook needs to remove so it can get from the cell \((1,1)\) to at least one cell in the row with the number \(10^9\) | In the first sample, in order for the rook return home, it is enough to remove the second horizontal spell. Illustration for the first sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the second horizontal spell. It also shows the path,... | Input: 2 3681 5 61 9 42 4 2 | Output: 1 | Medium | 2 | 2,209 | 863 | 185 | 10 |
697 | B | 697B | B. Barnicle | 1,400 | brute force; implementation; math; strings | Barney is standing in a bar and starring at a pretty girl. He wants to shoot her with his heart arrow but he needs to know the distance between him and the girl to make his shot accurate. Barney asked the bar tender Carl about this distance value, but Carl was so busy talking to the customers so he wrote the distance v... | The first and only line of input contains a single string of form a.deb where a, d and b are integers and e is usual character 'e' (0 ≤ a ≤ 9, 0 ≤ d < 10100, 0 ≤ b ≤ 100) — the scientific notation of the desired distance value.a and b contain no leading zeros and d contains no trailing zeros (but may be equal to 0). Al... | Print the only real number x (the desired distance value) in the only line in its decimal notation. Thus if x is an integer, print it's integer value without decimal part and decimal point and without leading zeroes. Otherwise print x in a form of p.q such that p is an integer that have no leading zeroes (but may be eq... | Input: 8.549e2 | Output: 854.9 | Easy | 4 | 935 | 359 | 414 | 6 | |
354 | D | 354D | D. Transferring Pyramid | 2,900 | dp | Vasya and Petya are using an interesting data storing structure: a pyramid.The pyramid consists of n rows, the i-th row contains i cells. Each row is shifted half a cell to the left relative to the previous row. The cells are numbered by integers from 1 to as shown on the picture below.An example of a pyramid at n = 5 ... | The first line contains two integers n and k (1 ≤ n, k ≤ 105).The next k lines contain the coordinates of the modified cells ri and ci (1 ≤ ci ≤ ri ≤ n) — the row and the cell's number in the row. All cells are distinct. | Print a single number showing how many numbers the final sequence has. | One of the possible solutions of the first sample consists of two operations:2 4 v4 v7 v82 6 v6 v9 v10The picture shows the changed cells color-highlighted. The subpyramid used by the first operation is highlighted blue and the subpyramid used by the first operation is highlighted yellow: | Input: 4 53 13 34 14 34 4 | Output: 10 | Master | 1 | 1,752 | 220 | 70 | 3 |
2,041 | K | 2041K | K. Trophic Balance Species | 3,100 | binary search; brute force; dfs and similar; graphs | Image generated by ChatGPT 4o. In an interdisciplinary collaboration, an ecosystem scientist and a computer scientist join forces to analyze the structure of a complex ecosystem using computational methods. The ecosystem scientist models the ecosystem as a directed graph \(D = (V, A)\), where each species is represente... | The first line contains exactly two integers \(n\) and \(m\), where \(n\) (resp. \(m\)) denotes the number of nodes (resp. edges) in the directed graph \(D\) induced by the investigated ecosystem. The nodes are numbered as \(1, 2, \ldots, n\). Then, \(m\) lines follow. The \(i\)-th line contains two integers \(x_i\) an... | Output on a single line the node identidiers of all trophic balance species in ascending order. For any two consecutive node identifiers, separate them by a space. | Input: 4 31 22 32 4 | Output: 2 | Master | 4 | 2,937 | 720 | 163 | 20 | |
1,630 | F | 1630F | F. Making It Bipartite | 3,400 | flows; graph matchings; graphs; number theory | You are given an undirected graph of \(n\) vertices indexed from \(1\) to \(n\), where vertex \(i\) has a value \(a_i\) assigned to it and all values \(a_i\) are different. There is an edge between two vertices \(u\) and \(v\) if either \(a_u\) divides \(a_v\) or \(a_v\) divides \(a_u\).Find the minimum number of verti... | The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 5\cdot10^4\)) — the number of vertices in the graph.The secon... | For each test case print a single integer — the minimum number of vertices to remove such that the remaining graph is bipartite. | In the first test case if we remove the vertices with values \(1\) and \(2\) we will obtain a bipartite graph, so the answer is \(2\), it is impossible to remove less than \(2\) vertices and still obtain a bipartite graph. BeforeAfter test case #1 In the second test case we do not have to remove any vertex because the ... | Input: 448 4 2 1430 2 3 5512 4 6 2 31085 195 5 39 3 13 266 154 14 2 | Output: 2 0 1 2 | Master | 4 | 443 | 599 | 128 | 16 |
1,792 | F2 | 1792F2 | F2. Graph Coloring (hard version) | 2,900 | brute force; combinatorics; divide and conquer; dp; fft; graphs | The only difference between the easy and the hard version is the constraint on \(n\).You are given an undirected complete graph on \(n\) vertices. A complete graph is a graph where each pair of vertices is connected by an edge. You have to paint the edges of the graph into two colors, red and blue (each edge will have ... | The first (and only) line contains one integer \(n\) (\(3 \le n \le 5 \cdot 10^4\)). | Print one integer — the number of ways to paint the graph, taken modulo \(998244353\). | Input: 3 | Output: 6 | Master | 6 | 1,122 | 84 | 86 | 17 | |
1,922 | A | 1922A | A. Tricky Template | 800 | constructive algorithms; implementation; strings | You are given an integer \(n\) and three strings \(a, b, c\), each consisting of \(n\) lowercase Latin letters.Let a template be a string \(t\) consisting of \(n\) lowercase and/or uppercase Latin letters. The string \(s\) matches the template \(t\) if the following conditions hold for all \(i\) from \(1\) to \(n\): if... | The first line contains an integer \(t\) (\(1 \le t \le 1000\)) — the number of test cases.The first line of each test case contains an integer \(n\) (\(1 \le n \le 20\)) — the length of the given strings.The next three lines contain the strings \(a, b\) and \(c\). Each string consists of exactly \(n\) lowercase Latin ... | For each testcase, print ""YES"" if there exists a template \(t\) such that the strings \(a\) and \(b\) match it, while the string \(c\) does not. Otherwise, print ""NO"". | In the first test case, you can use the template ""C"". The first letter of strings \(a\) and \(b\) differ from 'c', so they match the template. The first letter of string \(c\) equals 'c', so it doesn't match.In the third test case, you can use the template ""CODEforces"". | Input: 41abc2aabbaa10mathforcesluckforcesadhoccoder3accabdabc | Output: YES NO YES NO | Beginner | 3 | 898 | 328 | 171 | 19 |
886 | E | 886E | E. Maximum Element | 2,400 | combinatorics; dp; math | One day Petya was solving a very interesting problem. But although he used many optimization techniques, his solution still got Time limit exceeded verdict. Petya conducted a thorough analysis of his program and found out that his function for finding maximum element in an array of n positive integers was too slow. Des... | The only line contains two integers n and k (1 ≤ n, k ≤ 106), separated by a space — the length of the permutations and the parameter k. | Output the answer to the problem modulo 109 + 7. | Permutations from second example: [4, 1, 2, 3, 5], [4, 1, 3, 2, 5], [4, 2, 1, 3, 5], [4, 2, 3, 1, 5], [4, 3, 1, 2, 5], [4, 3, 2, 1, 5]. | Input: 5 2 | Output: 22 | Expert | 3 | 1,123 | 136 | 48 | 8 |
1,620 | G | 1620G | G. Subsequences Galore | 2,400 | bitmasks; combinatorics; dp | For a sequence of strings \([t_1, t_2, \dots, t_m]\), let's define the function \(f([t_1, t_2, \dots, t_m])\) as the number of different strings (including the empty string) that are subsequences of at least one string \(t_i\). \(f([]) = 0\) (i. e. the number of such strings for an empty sequence is \(0\)).You are give... | The first line contains one integer \(n\) (\(1 \le n \le 23\)) — the number of strings.Then \(n\) lines follow. The \(i\)-th line contains the string \(s_i\) (\(1 \le |s_i| \le 2 \cdot 10^4\)), consisting of lowercase Latin letters. Each string \(s_i\) is sorted. | Since printing up to \(2^{23}\) integers would be really slow, you should do the following:For each of the \(2^n\) subsequences (which we denote as \([s_{i_1}, s_{i_2}, \dots, s_{i_k}]\)), calculate \(f([s_{i_1}, s_{i_2}, \dots, s_{i_k}])\), take it modulo \(998244353\), then multiply it by \(k \cdot (i_1 + i_2 + \dots... | Input: 3 a b c | Output: 92 | Expert | 3 | 736 | 263 | 504 | 16 | |
2,090 | C | 2090C | C. Dining Hall | 1,700 | data structures; greedy; implementation; sortings | Inside the large kingdom, there is an infinite dining hall. It can be represented as a set of cells (\(x, y\)), where \(x\) and \(y\) are non-negative integers. There are an infinite number of tables in the hall. Each table occupies four cells (\(3x + 1, 3y + 1\)), (\(3x + 1, 3y + 2\)), (\(3x + 2, 3y + 1\)), (\(3x + 2,... | The first line contains a single integer \(q\) (\(1 \leq q \leq 5000\)) — the number of test cases. Then follows their description.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 50\,000\)) — the number of guests.The second line of each test case contains \(n\) integers \(t_1, t_2, \ld... | For each test case, output \(n\) lines — for each guest, the cell where they sit. | Consider the first test case:The distance from the first guest to the cell (\(1, 1\)) is \(2\), so he sits there.The distance from the second guest to the cell (\(1, 2\)) is \(3\), as is the distance to the cell (\(2, 1\)), but since the first coordinate is smaller for the first option, he will choose it.The distance f... | Input: 260 1 1 0 0 151 0 0 1 1 | Output: 1 1 1 2 2 1 1 4 4 1 1 5 1 1 1 4 4 1 1 2 2 1 | Medium | 4 | 1,526 | 476 | 81 | 20 |
1,822 | G1 | 1822G1 | G1. Magic Triples (Easy Version) | 1,700 | brute force; data structures; math; number theory | This is the easy version of the problem. The only difference is that in this version, \(a_i \le 10^6\).For a given sequence of \(n\) integers \(a\), a triple \((i, j, k)\) is called magic if: \(1 \le i, j, k \le n\). \(i\), \(j\), \(k\) are pairwise distinct. there exists a positive integer \(b\) such that \(a_i \cdot ... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. The description of the test cases follows.The first line of the test case contains a single integer \(n\) (\(3 \le n \le 2 \cdot 10^5\)) — the length of the sequence.The second line of the test contains \(n\) integers \(a_... | For each test case, output a single integer — the number of magic triples for the sequence \(a\). | In the first example, there are \(6\) magic triples for the sequence \(a\) — \((2, 3, 5)\), \((2, 5, 3)\), \((3, 2, 5)\), \((3, 5, 2)\), \((5, 2, 3)\), \((5, 3, 2)\).In the second example, there is a single magic triple for the sequence \(a\) — \((2, 1, 3)\). | Input: 751 7 7 2 736 2 1891 2 3 4 5 6 7 8 941000 993 986 17971 10 100 1000 10000 100000 100000081 1 2 2 4 4 8 891 1 1 2 2 2 4 4 4 | Output: 6 1 3 0 9 16 45 | Medium | 4 | 577 | 478 | 97 | 18 |
1,913 | F | 1913F | F. Palindromic Problem | 2,800 | binary search; data structures; hashing; string suffix structures; strings | You are given a string \(s\) of length \(n\), consisting of lowercase Latin letters.You are allowed to replace at most one character in the string with an arbitrary lowercase Latin letter.Print the lexicographically minimal string that can be obtained from the original string and contains the maximum number of palindro... | The first line contains one integer \(n\) (\(1 \leq n \leq 3 \cdot 10^5\)) — the number of characters in the string.The second line contains the string \(s\) itself, consisting of exactly \(n\) lowercase Latin letters. | In the first line, print one integer — the maximum number of palindromic substrings that can be obtained using the operation described in the statement at most once.In the second line, print the string that can be obtained from \(s\) and has the maximum possible number of palindromic substrings. If there are multiple a... | Input: 5aabaa | Output: 15 aaaaa | Master | 5 | 780 | 218 | 369 | 19 | |
1,472 | C | 1472C | C. Long Jumps | 1,100 | dp; graphs | Polycarp found under the Christmas tree an array \(a\) of \(n\) elements and instructions for playing with it: At first, choose index \(i\) (\(1 \leq i \leq n\)) — starting position in the array. Put the chip at the index \(i\) (on the value \(a_i\)). While \(i \leq n\), add \(a_i\) to your score and move the chip \(a_... | The first line contains one integer \(t\) (\(1 \leq t \leq 10^4\)) — the number of test cases. Then \(t\) test cases follow.The first line of each test case contains one integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) — the length of the array \(a\).The next line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le... | For each test case, output on a separate line one number — the maximum score that Polycarp can get by playing the game on the corresponding array according to the instruction from the statement. Note that Polycarp chooses any starting position from \(1\) to \(n\) in such a way as to maximize his result. | The first test case is explained in the statement.In the second test case, the maximum score can be achieved by choosing \(i = 1\).In the third test case, the maximum score can be achieved by choosing \(i = 2\).In the fourth test case, the maximum score can be achieved by choosing \(i = 1\). | Input: 4 5 7 3 1 2 3 3 2 1 4 6 2 1000 2 3 995 1 5 1 1 1 1 1 | Output: 7 6 1000 5 | Easy | 2 | 1,332 | 461 | 304 | 14 |
1,090 | E | 1090E | 2,300 | flows; graphs | Expert | 2 | 0 | 0 | 0 | 10 | ||||||
331 | D1 | 331D1 | D1. Escaping on Beaveractor | 2,400 | dfs and similar; implementation | Don't put up with what you're sick of! The Smart Beaver decided to escape from the campus of Beaver Science Academy (BSA). BSA is a b × b square on a plane. Each point x, y (0 ≤ x, y ≤ b) belongs to BSA. To make the path quick and funny, the Beaver constructed a Beaveractor, an effective and comfortable types of transp... | The first line contains two integers: the number of traffic rules n and the size of the campus b, 0 ≤ n, 1 ≤ b. Next n lines contain the rules. Each line of the rules contains four space-separated integers x0, y0, x1, y1 — the beginning and the end of the arrow. It is guaranteed that all arrows are parallel to the coor... | Print q lines. Each line should contain two integers — the Beaveractor's coordinates at the final moment of time for each plan. If the Smart Beaver manages to leave the campus in time ti, print the coordinates of the last point in the campus he visited. | Input: 3 30 0 0 10 2 2 23 3 2 3120 0 L 00 0 L 10 0 L 20 0 L 30 0 L 40 0 L 50 0 L 62 0 U 22 0 U 33 0 U 51 3 D 21 3 R 2 | Output: 0 00 10 21 22 23 23 22 23 21 32 21 3 | Expert | 2 | 1,205 | 1,162 | 253 | 3 | |
1,043 | E | 1043E | E. Train Hard, Win Easy | 1,900 | constructive algorithms; greedy; math; sortings | Zibi is a competitive programming coach. There are \(n\) competitors who want to be prepared well. The training contests are quite unusual – there are two people in a team, two problems, and each competitor will code exactly one of them. Of course, people in one team will code different problems.Rules of scoring also a... | The first line contains two integers \(n\) and \(m\) (\(2 \le n \le 300\,000\), \(0 \le m \le 300\,000\)) — the number of participants and the number of pairs of people who will not write a contest together.Each of the next \(n\) lines contains two integers \(x_i\) and \(y_i\) (\(-10^9 \le x_i, y_i \le 10^9\)) — the sc... | Output \(n\) integers — the sum of scores for all participants in the same order as they appear in the input. | In the first example, there will be only one team consisting of persons \(1\) and \(3\). The optimal strategy for them is to assign the first task to the \(3\)-rd person and the second task to the \(1\)-st person, this will lead to score equal to \(1 + 2 = 3\).In the second example, nobody likes anyone, so there won't ... | Input: 3 21 22 31 31 22 3 | Output: 3 0 3 | Hard | 4 | 1,608 | 732 | 109 | 10 |
698 | F | 698F | F. Coprime Permutation | 3,000 | combinatorics; number theory | Two positive integers are coprime if and only if they don't have a common divisor greater than 1.Some bear doesn't want to tell Radewoosh how to solve some algorithmic problem. So, Radewoosh is going to break into that bear's safe with solutions. To pass through the door, he must enter a permutation of numbers 1 throug... | The first line of the input contains one integer n (2 ≤ n ≤ 1 000 000).The second line contains n integers p1, p2, ..., pn (0 ≤ pi ≤ n) where pi = 0 means a gap to fill, and pi ≥ 1 means a fixed number.It's guaranteed that if i ≠ j and pi, pj ≥ 1 then pi ≠ pj. | Print the number of ways to fill the gaps modulo 109 + 7 (i.e. modulo 1000000007). | In the first sample test, none of four element is fixed. There are four permutations satisfying the given conditions: (1,2,3,4), (1,4,3,2), (3,2,1,4), (3,4,1,2).In the second sample test, there must be p3 = 1 and p4 = 2. The two permutations satisfying the conditions are: (3,4,1,2,5), (5,4,1,2,3). | Input: 40 0 0 0 | Output: 4 | Master | 2 | 665 | 260 | 82 | 6 |
1,991 | C | 1991C | C. Absolute Zero | 1,300 | constructive algorithms; greedy; math | You are given an array \(a\) of \(n\) integers.In one operation, you will perform the following two-step move: Choose an integer \(x\) (\(0 \le x \le 10^{9}\)). Replace each \(a_i\) with \(|a_i - x|\), where \(|v|\) denotes the absolute value of \(v\). For example, by choosing \(x = 8\), the array \([5, 7, 10]\) will b... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) — the length of the array \(a\).The second line ... | For each test case, output a single integer \(-1\) if it is impossible to make all array elements equal to \(0\) in at most \(40\) operations.Otherwise, output two lines. The first line of output should contain a single integer \(k\) (\(0 \le k \le 40\)) — the number of operations. The second line of output should cont... | In the first test case, we can perform only one operation by choosing \(x = 5\), changing the array from \([5]\) to \([0]\).In the second test case, no operations are needed because all elements of the array are already \(0\).In the third test case, we can choose \(x = 6\) to change the array from \([4, 6, 8]\) to \([2... | Input: 51520 034 6 8480 40 20 1051 2 3 4 5 | Output: 1 5 0 3 6 1 1 7 60 40 20 10 30 25 5 -1 | Easy | 3 | 570 | 539 | 588 | 19 |
1,322 | E | 1322E | E. Median Mountain Range | 3,300 | data structures | Berland — is a huge country with diverse geography. One of the most famous natural attractions of Berland is the ""Median mountain range"". This mountain range is \(n\) mountain peaks, located on one straight line and numbered in order of \(1\) to \(n\). The height of the \(i\)-th mountain top is \(a_i\). ""Median moun... | The first line contains integers \(n\) (\(1 \le n \le 500\,000\)) — the number of mountains.The second line contains integers \(a_1, a_2, a_3, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) — current heights of the mountains. | In the first line print \(c\) — the number of alignments, which change the height of at least one mountain.In the second line print \(n\) integers — the final heights of the mountains after \(c\) alignments. | In the first example, the heights of the mountains at index \(1\) and \(5\) never change. Since the median of \(1\), \(2\), \(1\) is \(1\), the second and the fourth mountains will have height 1 after the first alignment, and since the median of \(2\), \(1\), \(2\) is \(2\), the third mountain will have height 2 after ... | Input: 5 1 2 1 2 1 | Output: 2 1 1 1 1 1 | Master | 1 | 1,632 | 217 | 207 | 13 |
56 | A | 56A | A. Bar | 1,000 | implementation | According to Berland laws it is only allowed to sell alcohol to people not younger than 18 years. Vasya's job is to monitor the law's enforcement. Tonight he entered a bar and saw n people sitting there. For every one of them Vasya happened to determine either the age or the drink the person is having. Vasya can check ... | The first line contains an integer n (1 ≤ n ≤ 100) which is the number of the bar's clients. Then follow n lines, each describing one visitor. A line either contains his age (an integer from 0 to 1000) or his drink (a string of capital Latin letters from 1 to 100 in length). It is guaranteed that the input data does no... | Print a single number which is the number of people Vasya should check to guarantee the law enforcement. | In the sample test the second and fifth clients should be checked. | Input: 518VODKACOKE1917 | Output: 2 | Beginner | 1 | 653 | 441 | 104 | 0 |
1,789 | C | 1789C | C. Serval and Toxel's Arrays | 1,500 | combinatorics; dp; implementation; math | Toxel likes arrays. Before traveling to the Paldea region, Serval gave him an array \(a\) as a gift. This array has \(n\) pairwise distinct elements.In order to get more arrays, Toxel performed \(m\) operations with the initial array. In the \(i\)-th operation, he modified the \(p_{i}\)-th element of the \((i-1)\)-th a... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1\le t\le10^{4}\)). The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1\le n,m\le2\cdot10^{5}\)) — the length of the array and the number of operations.The ... | For each test case, print a single integer — the sum of the values of all pairs of arrays. | In the first test case, the arrays change as follows: \([1,2,3]\to[\underline{4},2,3]\to[4,\underline{5},3]\).The concatenation of the \(0\)-th array and the \(1\)-st array is \(\require{cancel}[1,2,3,4,\cancel{2},\cancel{3}]\). There are \(4\) distinct elements.The concatenation of the \(0\)-th array and the \(2\)-nd ... | Input: 33 21 2 31 42 51 111 110 104 6 9 12 16 20 2 10 19 71 35 42 172 186 117 18 175 55 52 2 | Output: 13 1 705 | Medium | 4 | 882 | 891 | 90 | 17 |
314 | D | 314D | D. Sereja and Straight Lines | 2,500 | binary search; data structures; geometry; sortings; two pointers | Sereja placed n points on a plane. Now Sereja wants to place on the plane two straight lines, intersecting at a right angle, so that one of the straight lines intersect the Ox axis at an angle of 45 degrees and the maximum distance from the points to the straight lines were minimum. In this problem we consider the dist... | The first line contains integer n (1 ≤ n ≤ 105). Next n lines contain the coordinates of the lines. The i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109). | In a single line print a real number — the answer to the problem. Your answer will be considered correct iff its absolute or relative error doesn't exceed 10 - 6. | Input: 40 02 00 22 2 | Output: 0.000000000000000 | Expert | 5 | 623 | 162 | 162 | 3 | |
1,988 | F | 1988F | F. Heartbeat | 3,000 | combinatorics; dp; fft; math | For an array \(u_1, u_2, \ldots, u_n\), define a prefix maximum as an index \(i\) such that \(u_i>u_j\) for all \(j<i\); a suffix maximum as an index \(i\) such that \(u_i>u_j\) for all \(j>i\); an ascent as an index \(i\) (\(i>1\)) such that \(u_i>u_{i-1}\). You are given three cost arrays: \([a_1, a_2, \ldots, a_n]\)... | The first line contains an integer \(n\) (\(1\le n\le 700\)).The second line contains \(n\) integers \(a_1,\ldots,a_n\) (\(0\le a_i<998\,244\,353\)).The third line contains \(n\) integers \(b_1,\ldots,b_n\) (\(0\le b_i<998\,244\,353\)).The fourth line contains \(n\) integers \(c_0,\ldots,c_{n-1}\) (\(0\le c_i<998\,244\... | Print \(n\) integers: the \(i\)-th one is \(f(i)\) modulo \(998\,244\,353\). | In the second example: Consider permutation \([1,2,3]\). Indices \(1,2,3\) are prefix maximums. Index \(3\) is the only suffix maximum. Indices \(2,3\) are ascents. In conclusion, it has \(3\) prefix maximums, \(1\) suffix maximums, and \(2\) ascents. Therefore, its cost is \(a_3b_1c_2=12\). Permutation \([1,3,2]\) has... | Input: 31 1 11 1 11 1 1 | Output: 1 2 6 | Master | 4 | 655 | 328 | 76 | 19 |
1,765 | L | 1765L | L. Project Manager | 2,400 | brute force; data structures; implementation | There are \(n\) employees at Bersoft company, numbered from \(1\) to \(n\). Each employee works on some days of the week and rests on the other days. You are given the lists of working days of the week for each employee.There are regular days and holidays. On regular days, only those employees work that have the curren... | The first line contains three integers \(n, m\) and \(k\) (\(1 \le n, m, k \le 2 \cdot 10^5\)) — the number of employees, the number of holidays and the number of projects.The \(i\)-th of the next \(n\) lines contains the list of working days of the \(i\)-th employee. First, a single integer \(t\) (\(1 \le t \le 7\)) —... | Print \(k\) integers — the \(j\)-th value should be equal to the day the \(j\)-th project is completed on. | Input: 3 5 4 2 Saturday Sunday 2 Tuesday Thursday 4 Monday Wednesday Friday Saturday 4 7 13 14 15 5 1 1 3 3 2 3 2 3 2 5 3 3 3 1 1 8 3 3 3 3 3 3 3 3 | Output: 25 9 27 27 | Expert | 3 | 1,275 | 1,062 | 106 | 17 | |
383 | B | 383B | B. Volcanoes | 2,500 | binary search; implementation; sortings; two pointers | Iahub got lost in a very big desert. The desert can be represented as a n × n square matrix, where each cell is a zone of the desert. The cell (i, j) represents the cell at row i and column j (1 ≤ i, j ≤ n). Iahub can go from one cell (i, j) only down or right, that is to cells (i + 1, j) or (i, j + 1). Also, there are... | The first line contains two integers n (1 ≤ n ≤ 109) and m (1 ≤ m ≤ 105). Each of the next m lines contains a pair of integers, x and y (1 ≤ x, y ≤ n), representing the coordinates of the volcanoes.Consider matrix rows are numbered from 1 to n from top to bottom, and matrix columns are numbered from 1 to n from left to... | Print one integer, the minimum time in which Iahub can arrive at cell (n, n). If no solution exists (there is no path to the final cell), print -1. | Consider the first sample. A possible road is: (1, 1) → (1, 2) → (2, 2) → (2, 3) → (3, 3) → (3, 4) → (4, 4). | Input: 4 21 31 4 | Output: 6 | Expert | 4 | 590 | 406 | 147 | 3 |
20 | B | 20B | B. Equation | 2,000 | math | You are given an equation: Ax2 + Bx + C = 0. Your task is to find the number of distinct roots of the equation and print all of them in ascending order. | The first line contains three integer numbers A, B and C ( - 105 ≤ A, B, C ≤ 105). Any coefficient may be equal to 0. | In case of infinite root count print the only integer -1. In case of no roots print the only integer 0. In other cases print the number of root on the first line and the roots on the following lines in the ascending order. Print roots with at least 5 digits after the decimal point. | Input: 1 -5 6 | Output: 22.00000000003.0000000000 | Hard | 1 | 152 | 117 | 282 | 0 | |
1,841 | B | 1841B | B. Keep it Beautiful | 1,000 | implementation | The array \([a_1, a_2, \dots, a_k]\) is called beautiful if it is possible to remove several (maybe zero) elements from the beginning of the array and insert all these elements to the back of the array in the same order in such a way that the resulting array is sorted in non-descending order.In other words, the array \... | The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.Each test case consists of two lines. The first line contains one integer \(q\) (\(1 \le q \le 2 \cdot 10^5\)) — the number of queries. The second line contains \(q\) integers \(x_1, x_2, \dots, x_q\) (\(0 \le x_i \le 10^9\)).Ad... | For each test case, print one string consisting of exactly \(q\) characters. The \(i\)-th character of the string should be 1 if you appended the integer during the \(i\)-th query; otherwise, it should be 0. | Consider the first test case of the example. Initially, the array is \([]\). trying to append an integer \(3\). The array \([3]\) is beautiful, so we append \(3\); trying to append an integer \(7\). The array \([3, 7]\) is beautiful, so we append \(7\); trying to append an integer \(7\). The array \([3, 7, 7]\) is beau... | Input: 393 7 7 9 2 4 6 3 451 1 1 1 153 2 1 2 3 | Output: 111110010 11111 11011 | Beginner | 1 | 1,466 | 425 | 207 | 18 |
1,515 | D | 1515D | D. Phoenix and Socks | 1,500 | greedy; sortings; two pointers | To satisfy his love of matching socks, Phoenix has brought his \(n\) socks (\(n\) is even) to the sock store. Each of his socks has a color \(c_i\) and is either a left sock or right sock. Phoenix can pay one dollar to the sock store to either: recolor a sock to any color \(c'\) \((1 \le c' \le n)\) turn a left sock in... | The input consists of multiple test cases. The first line contains an integer \(t\) (\(1 \le t \le 1000\)) — the number of test cases.The first line of each test case contains three integers \(n\), \(l\), and \(r\) (\(2 \le n \le 2 \cdot 10^5\); \(n\) is even; \(0 \le l, r \le n\); \(l+r=n\)) — the total number of sock... | For each test case, print one integer — the minimum cost for Phoenix to make \(n/2\) matching pairs. Each sock must be included in exactly one matching pair. | In the first test case, Phoenix can pay \(2\) dollars to: recolor sock \(1\) to color \(2\) recolor sock \(3\) to color \(2\) There are now \(3\) matching pairs. For example, pairs \((1, 4)\), \((2, 5)\), and \((3, 6)\) are matching.In the second test case, Phoenix can pay \(3\) dollars to: turn sock \(6\) from a right... | Input: 4 6 3 3 1 2 3 2 2 2 6 2 4 1 1 2 2 2 2 6 5 1 6 5 4 3 2 1 4 0 4 4 4 4 3 | Output: 2 3 5 3 | Medium | 3 | 736 | 650 | 157 | 15 |
1,778 | F | 1778F | F. Maximizing Root | 2,600 | dfs and similar; dp; graphs; math; number theory; trees | You are given a rooted tree consisting of \(n\) vertices numbered from \(1\) to \(n\). Vertex \(1\) is the root of the tree. Each vertex has an integer value. The value of \(i\)-th vertex is \(a_i\). You can do the following operation at most \(k\) times. Choose a vertex \(v\) that has not been chosen before and an int... | The first line contains an integer \(t\) (\(1 \leq t \leq 50\,000\)) — 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\) (\(2 \leq n \leq 10^5\), \(0 \leq k \leq n\)) — the number of vertices in the tree and the number of operation... | For each test case, output the maximum value of the root after performing at most \(k\) operations. | Both examples have the same tree: For the first test case, you can do two operations as follows: Choose the subtree of vertex \(4\) and \(x = 2\). After this operation, the node values become \(\{24, 12, 24, 12, 12\}.\) Choose the subtree of vertex \(1\) and \(x = 12\). After this operation, the node values become \(\{... | Input: 25 224 12 24 6 121 21 32 42 55 324 12 24 6 121 21 32 42 5 | Output: 288 576 | Expert | 6 | 933 | 797 | 99 | 17 |
1,423 | C | 1423C | C. Dušan's Railway | 3,500 | divide and conquer; graphs; trees | As you may already know, Dušan is keen on playing with railway models. He has a big map with cities that are connected with railways. His map can be seen as a graph where vertices are cities and the railways connecting them are the edges. So far, the graph corresponding to his map is a tree. As you already know, a tree... | The first line in the standard input contains an integer \(n\) (\(1 \le n \le 10^4\)), representing the number of the cities in Dušan's railway map, and an integer k (\(3 \le k \le n\)) representing the shortcutting diameter that he wants to achieve.Each of the following \(n - 1\) lines will contain two integers \(u_i\... | The first line of the output should contain a number \(t\) representing the number of the shortcuts that were added.Each of the following \(t\) lines should contain two integers \(u_i\) and \(v_i\), signifying that a shortcut is added between cities \(u_i\) and \(v_i\). | Notice that adding a shortcut between all cities and city 1 will make a graph theoretic diameter become 2. On the other hand, the paths obtained that way might not be good, since some of the edges might get duplicated. In the example, adding a shortcut between all cities and city 1 doesn't create a valid solution, beca... | Input: 10 3 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 | Output: 8 3 7 3 5 3 6 3 1 7 9 7 10 7 4 7 5 | Master | 3 | 1,531 | 443 | 270 | 14 |
1,450 | D | 1450D | D. Rating Compression | 1,800 | binary search; data structures; greedy; implementation; two pointers | On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers \(a\) of length \(n\). You are now updating the infrastructure, so you've created a program to compress these graphs.The program works as follows. Given an integer parameter \(k\), the program takes the m... | The first line contains a single integer \(t\) (\(1\leq t\leq 10^4\)) — the number of test cases.The first line of the description of each test case contains a single integer \(n\) (\(1\leq n\leq 3\cdot 10^5\)) — the length of the array.The second line of the description of each test case contains \(n\) integers \(a_1,... | For each test case, print a binary string of length \(n\). The \(k\)-th character of the string should be \(1\) if CodeCook users will be happy after a \(k\)-compression of the array \(a\), and \(0\) otherwise. | In the first test case, \(a=[1, 5, 3, 4, 2]\). The \(1\)-compression of \(a\) is \([1, 5, 3, 4, 2]\) and it is a permutation. The \(2\)-compression of \(a\) is \([1, 3, 3, 2]\) and it is not a permutation, since \(3\) appears twice. The \(3\)-compression of \(a\) is \([1, 3, 2]\) and it is a permutation. The \(4\)-comp... | Input: 5 5 1 5 3 4 2 4 1 3 2 1 5 1 3 3 3 2 10 1 2 3 4 5 6 7 8 9 10 3 3 3 2 | Output: 10111 0001 00111 1111111111 000 | Medium | 5 | 1,264 | 474 | 210 | 14 |
1,065 | D | 1065D | D. Three Pieces | 2,200 | dfs and similar; dp; shortest paths | You stumbled upon a new kind of chess puzzles. The chessboard you are given is not necesserily \(8 \times 8\), but it still is \(N \times N\). Each square has some number written on it, all the numbers are from \(1\) to \(N^2\) and all the numbers are pairwise distinct. The \(j\)-th square in the \(i\)-th row has a num... | The first line contains a single integer \(N\) (\(3 \le N \le 10\)) — the size of the chessboard.Each of the next \(N\) lines contains \(N\) integers \(A_{i1}, A_{i2}, \dots, A_{iN}\) (\(1 \le A_{ij} \le N^2\)) — the numbers written on the squares of the \(i\)-th row of the board.It is guaranteed that all \(A_{ij}\) ar... | The only line should contain two integers — the number of steps in the best answer and the number of replacement moves in it. | Here are the steps for the first example (the starting piece is a knight): Move to \((3, 2)\) Move to \((1, 3)\) Move to \((3, 2)\) Replace the knight with a rook Move to \((3, 1)\) Move to \((3, 3)\) Move to \((3, 2)\) Move to \((2, 2)\) Move to \((2, 3)\) Move to \((2, 1)\) Move to \((1, 1)\) Move to \((1, 2)\) | Input: 31 9 38 6 74 2 5 | Output: 12 1 | Hard | 3 | 1,407 | 340 | 125 | 10 |
1,172 | C1 | 1172C1 | C1. Nauuo and Pictures (easy version) | 2,300 | dp; probabilities | The only difference between easy and hard versions is constraints.Nauuo is a girl who loves random picture websites.One day she made a random picture website by herself which includes \(n\) pictures.When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probabi... | The first line contains two integers \(n\) and \(m\) (\(1\le n\le 50\), \(1\le m\le 50\)) — the number of pictures and the number of visits to the website.The second line contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(a_i\) is either \(0\) or \(1\)) — if \(a_i=0\) , Nauuo does not like the \(i\)-th picture; otherwis... | The output contains \(n\) integers \(r_1,r_2,\ldots,r_n\) — the expected weights modulo \(998244353\). | In the first example, if the only visit shows the first picture with a probability of \(\frac 2 3\), the final weights are \((1,1)\); if the only visit shows the second picture with a probability of \(\frac1 3\), the final weights are \((2,2)\).So, both expected weights are \(\frac2 3\cdot 1+\frac 1 3\cdot 2=\frac4 3\)... | Input: 2 1 0 1 2 1 | Output: 332748119 332748119 | Expert | 2 | 1,281 | 546 | 102 | 11 |
1,968 | G2 | 1968G2 | G2. Division + LCP (hard version) | 2,200 | binary search; brute force; data structures; dp; hashing; math; string suffix structures; strings | This is the hard version of the problem. In this version \(l\le r\).You are given a string \(s\). For a fixed \(k\), consider a division of \(s\) into exactly \(k\) continuous substrings \(w_1,\dots,w_k\). Let \(f_k\) be the maximal possible \(LCP(w_1,\dots,w_k)\) among all divisions.\(LCP(w_1,\dots,w_m)\) is the lengt... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.The first line of each test case contains two integers \(n\), \(l\), \(r\) (\(1 \le l \le r \le n \le 2 \cdot 10^5\)) — the length of the string and the given range.The second line of each test case contains string \(s\) of... | For each test case, output \(r-l+1\) values: \(f_l,\dots,f_r\). | Input: 73 1 3aba3 2 3aaa7 1 5abacaba9 1 6abababcab10 1 10aaaaaaawac9 1 9abafababa7 2 7vvzvvvv | Output: 3 1 0 1 1 7 3 1 1 0 9 2 2 2 0 0 10 3 2 1 1 1 1 1 0 0 9 3 2 1 1 0 0 0 0 2 2 1 1 1 0 | Hard | 8 | 855 | 471 | 63 | 19 | |
1,583 | H | 1583H | H. Omkar and Tours | 3,300 | data structures; divide and conquer; sortings; trees | Omkar is hosting tours of his country, Omkarland! There are \(n\) cities in Omkarland, and, rather curiously, there are exactly \(n-1\) bidirectional roads connecting the cities to each other. It is guaranteed that you can reach any city from any other city through the road network.Every city has an enjoyment value \(e... | The first line contains two integers \(n\) and \(q\) (\(2 \leq n \leq 2 \cdot 10^5\), \(1 \leq q \leq 2 \cdot 10^5\)), representing the number of cities and the number of groups, respectively.The next line contains \(n\) integers \(e_1, e_2, \ldots, e_n\) (\(1 \leq e_i \leq 10^9\)), where \(e_i\) represents the enjoyme... | Output \(q\) lines. The \(i\)-th line should contain two integers: the highest possible enjoyment value of a city reachable by the \(i\)-th tour group, and the amount of money per vehicle Omkar needs to guarantee that he can reimburse the \(i\)-th tour group. | A map of the first sample is shown below. For the nodes, unbolded numbers represent indices and bolded numbers represent enjoyment values. For the edges, unbolded numbers represent capacities and bolded numbers represent tolls. For the first query, a tour group of size \(1\) starting at city \(3\) can reach cities \(1\... | Input: 5 3 2 2 3 3 3 1 2 4 7 1 3 2 8 2 4 8 2 2 5 1 1 1 3 9 5 6 2 | Output: 3 8 3 0 3 2 | Master | 4 | 1,912 | 788 | 259 | 15 |
1,003 | F | 1003F | F. Abbreviation | 2,200 | dp; hashing; strings | You are given a text consisting of \(n\) space-separated words. There is exactly one space character between any pair of adjacent words. There are no spaces before the first word and no spaces after the last word. The length of text is the number of letters and spaces in it. \(w_i\) is the \(i\)-th word of text. All wo... | The first line of the input contains one integer \(n\) (\(1 \le n \le 300\)) — the number of words in the text.The next line contains \(n\) space-separated words of the text \(w_1, w_2, \dots, w_n\). Each word consists only of lowercase Latin letters.It is guaranteed that the length of text does not exceed \(10^5\). | Print one integer — the minimum length of the text after at most one abbreviation. | In the first example you can obtain the text ""TB or not TB"".In the second example you can obtain the text ""a AAAB AAAB c"".In the third example you can obtain the text ""AB aa AB bb"". | Input: 6to be or not to be | Output: 12 | Hard | 3 | 1,541 | 317 | 82 | 10 |
1,034 | A | 1034A | A. Enlarge GCD | 1,800 | number theory | Mr. F has \(n\) positive integers, \(a_1, a_2, \ldots, a_n\).He thinks the greatest common divisor of these integers is too small. So he wants to enlarge it by removing some of the integers.But this problem is too simple for him, so he does not want to do it by himself. If you help him, he will give you some scores in ... | The first line contains an integer \(n\) (\(2 \leq n \leq 3 \cdot 10^5\)) — the number of integers Mr. F has.The second line contains \(n\) integers, \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 1.5 \cdot 10^7\)). | Print an integer — the minimum number of integers you need to remove so that the greatest common divisor of the remaining integers is bigger than that of all integers.You should not remove all of the integers.If there is no solution, print «-1» (without quotes). | In the first example, the greatest common divisor is \(1\) in the beginning. You can remove \(1\) so that the greatest common divisor is enlarged to \(2\). The answer is \(1\).In the second example, the greatest common divisor is \(3\) in the beginning. You can remove \(6\) and \(9\) so that the greatest common divisor... | Input: 31 2 4 | Output: 1 | Medium | 1 | 501 | 213 | 262 | 10 |
111 | A | 111A | A. Petya and Inequiations | 1,400 | greedy | Little Petya loves inequations. Help him find n positive integers a1, a2, ..., an, such that the following two conditions are satisfied: a12 + a22 + ... + an2 ≥ x a1 + a2 + ... + an ≤ y | The first line contains three space-separated integers n, x and y (1 ≤ n ≤ 105, 1 ≤ x ≤ 1012, 1 ≤ y ≤ 106).Please do not use the %lld specificator to read or write 64-bit integers in С++. It is recommended to use cin, cout streams or the %I64d specificator. | Print n positive integers that satisfy the conditions, one integer per line. If such numbers do not exist, print a single number ""-1"". If there are several solutions, print any of them. | Input: 5 15 15 | Output: 44112 | Easy | 1 | 185 | 257 | 187 | 1 | |
1,574 | C | 1574C | C. Slay the Dragon | 1,300 | binary search; greedy; sortings; ternary search | Recently, Petya learned about a new game ""Slay the Dragon"". As the name suggests, the player will have to fight with dragons. To defeat a dragon, you have to kill it and defend your castle. To do this, the player has a squad of \(n\) heroes, the strength of the \(i\)-th hero is equal to \(a_i\).According to the rules... | The first line contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) — number of heroes.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^{12}\)), where \(a_i\) is the strength of the \(i\)-th hero.The third line contains a single integer \(m\) (\(1 \le m \le 2 \cdot 10^5\)) ... | Print \(m\) lines, \(i\)-th of which contains a single integer — the minimum number of coins that should be spent to defeat the \(i\)-th dragon. | To defeat the first dragon, you can increase the strength of the third hero by \(1\), then the strength of the heroes will be equal to \([3, 6, 3, 3]\). To kill the dragon, you can choose the first hero.To defeat the second dragon, you can increase the forces of the second and third heroes by \(1\), then the strength o... | Input: 4 3 6 2 3 5 3 12 7 9 4 14 1 10 8 7 | Output: 1 2 4 0 2 | Easy | 4 | 1,096 | 513 | 144 | 15 |
876 | A | 876A | A. Trip For Meal | 900 | math | Winnie-the-Pooh likes honey very much! That is why he decided to visit his friends. Winnie has got three best friends: Rabbit, Owl and Eeyore, each of them lives in his own house. There are winding paths between each pair of houses. The length of a path between Rabbit's and Owl's houses is a meters, between Rabbit's an... | First line contains an integer n (1 ≤ n ≤ 100) — number of visits.Second line contains an integer a (1 ≤ a ≤ 100) — distance between Rabbit's and Owl's houses.Third line contains an integer b (1 ≤ b ≤ 100) — distance between Rabbit's and Eeyore's houses.Fourth line contains an integer c (1 ≤ c ≤ 100) — distance between... | Output one number — minimum distance in meters Winnie must go through to have a meal n times. | In the first test case the optimal path for Winnie is the following: first have a meal in Rabbit's house, then in Owl's house, then in Eeyore's house. Thus he will pass the distance 2 + 1 = 3.In the second test case Winnie has a meal in Rabbit's house and that is for him. So he doesn't have to walk anywhere at all. | Input: 3231 | Output: 3 | Beginner | 1 | 1,238 | 347 | 93 | 8 |
1,804 | H | 1804H | H. Code Lock | 3,300 | bitmasks; dp | Lara has a safe that is locked with a circle-shaped code lock that consists of a rotating arrow, a static circumference around the arrow, an input screen, and an input button.The circumference of the lock is split into \(k\) equal sections numbered from \(1\) to \(k\) in clockwise order. Arrow always points to one of t... | The first line of the input contains two integers \(k\) and \(n\) (\(2 \leq k \leq 16\), \(2 \leq n \leq 100\,000\)) — the number of sectors on the lock's circumference and the length of Lara's password, respectively.The second line of the input contains a string of length \(n\) that consists of the first \(k\) lowerca... | On the first line print minimum possible number of seconds it can take Lara to enter the password and open the safe if she assigns letters to sectors optimally.On the second line print the number of ways to assign letters optimally. | The initial states of optimal arrangements for the first example are shown in the figure below. The initial states of optimal arrangements for the second example are shown in the figure below. The initial states of optimal arrangements for the third example are shown in the figure below. | Input: 3 10 abcabcabca | Output: 19 2 | Master | 2 | 2,115 | 389 | 232 | 18 |
1,371 | E2 | 1371E2 | E2. Asterism (Hard Version) | 2,300 | binary search; combinatorics; dp; math; number theory; sortings | This is the hard version of the problem. The difference between versions is the constraints on \(n\) and \(a_i\). You can make hacks only if all versions of the problem are solved.First, Aoi came up with the following idea for the competitive programming problem:Yuzu is a girl who collecting candies. Originally, she ha... | The first line contains two integers \(n\), \(p\) \((2 \le p \le n \le 10^5)\). It is guaranteed, that the number \(p\) is prime (it has exactly two divisors \(1\) and \(p\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) \((1 \le a_i \le 10^9)\). | In the first line, print the number of good integers \(x\).In the second line, output all good integers \(x\) in the ascending order.It is guaranteed that the number of good integers \(x\) does not exceed \(10^5\). | In the first test, \(p=2\). If \(x \le 2\), there are no valid permutations for Yuzu. So \(f(x)=0\) for all \(x \le 2\). The number \(0\) is divisible by \(2\), so all integers \(x \leq 2\) are not good. If \(x = 3\), \(\{1,2,3\}\) is the only valid permutation for Yuzu. So \(f(3)=1\), so the number \(3\) is good. If \... | Input: 3 2 3 4 5 | Output: 1 3 | Expert | 6 | 1,625 | 266 | 214 | 13 |
1,531 | E1 | 1531E1 | E1. Сортировка слиянием | 0 | *special | Рассмотрим следующий код сортировки слиянием на языке Python: def sort(a): n = len(a) b = [0 for i in range(n)] log = [] def mergeSort(l, r): if r - l <= 1: return m = (l + r) >> 1 mergeSort(l, m) mergeSort(m, r) i, j, k = l, m, l while i < m and j < r: if a[i] < a[j]: log.append('0') b[k] = a[i] i += 1 else: log.appen... | Ввод содержит непустую строку \(s\), состоящую из символов 0 и 1. В этой версии задачи для любого теста существует перестановка длины \(16\), удовлетворяющая условию. Тем не менее, ваш ответ может иметь любую длину, в том числе отличную от \(16\). | В первой строке выведите целое число \(n\) — длину перестановки.Во второй строке выведите \(n\) различных целых чисел \(a_0, a_1, \ldots, a_{n-1}\) (\(1 \le a_i \le n\)) — элементы перестановки.Если существует несколько вариантов ответа, выведите любой из них. | Input: 00000000000000000000000000000000 | Output: 16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | Beginner | 1 | 976 | 247 | 260 | 15 | |
1,775 | D | 1775D | D. Friendly Spiders | 1,800 | dfs and similar; graphs; math; number theory; shortest paths | Mars is home to an unusual species of spiders — Binary spiders.Right now, Martian scientists are observing a colony of \(n\) spiders, the \(i\)-th of which has \(a_i\) legs.Some of the spiders are friends with each other. Namely, the \(i\)-th and \(j\)-th spiders are friends if \(\gcd(a_i, a_j) \ne 1\), i. e., there is... | The first line of input contains an integer \(n\) (\(2 \le n \le 3\cdot10^5\)) — the number of spiders in the colony.The second line of input contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 3\cdot10^5\)) — the number of legs the spiders have.The third line of input contains two integers \(s\) and \(t... | If it is impossible to transmit a message between the given pair of spiders, print \(-1\).Otherwise, in the first line of the output print the integer \(t\) (\(t \ge 1\)) — the number of spiders that participate in the message transmission (i. e. the minimum time of message delivery in seconds plus one). In the second ... | The first example is shown above. It shows that the message from the \(5\)-th spider (with eight legs) to the \(6\)-th spider (with \(15\) legs) is optimal to pass through the \(4\)-th spider (with six legs).In the second example, the spider number \(7\) (with \(11\) legs) is not friends with anyone, so it is impossibl... | Input: 7 2 14 9 6 8 15 11 5 6 | Output: 3 5 4 6 | Medium | 5 | 1,485 | 398 | 576 | 17 |
1,864 | E | 1864E | E. Guess Game | 2,100 | bitmasks; data structures; games; math; probabilities; sortings; strings; trees | Carol has a sequence \(s\) of \(n\) non-negative integers. She wants to play the ""Guess Game"" with Alice and Bob.To play the game, Carol will randomly select two integer indices \(i_a\) and \(i_b\) within the range \([1, n]\), and set \(a=s_{i_a}\), \(b=s_{i_b}\). Please note that \(i_a\) and \(i_b\) may coincide.Car... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^5\)). The description of the test cases follows.The first line of each testcase contains a single integer \(n\) (\(1 \le n \le 2\cdot 10^5\)).The second line of each testcase contains \(n\) integers \(s_1,s_... | For each test case, print a single integer — the answer to the problem modulo \(998\,244\,353\). | In the first test case, there are only \(4\) possible situations: \(i_a=1\), \(i_b=1\), \(a=2\), \(b=2\), the number of turns is \(2\); \(i_a=1\), \(i_b=2\), \(a=2\), \(b=3\), the number of turns is \(3\); \(i_a=2\), \(i_b=1\), \(a=3\), \(b=2\), the number of turns is \(2\); \(i_a=2\), \(i_b=2\), \(a=3\), \(b=3\), the ... | Input: 422 330 0 039 9 6834124838 0 113193378 8 321939321 113193378 9463828 99 | Output: 499122179 1 332748120 77987843 | Hard | 8 | 1,679 | 453 | 96 | 18 |
598 | C | 598C | C. Nearest vectors | 2,300 | geometry; sortings | You are given the set of vectors on the plane, each of them starting at the origin. Your task is to find a pair of vectors with the minimal non-oriented angle between them.Non-oriented angle is non-negative value, minimal between clockwise and counterclockwise direction angles. Non-oriented angle is always between 0 an... | First line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of vectors.The i-th of the following n lines contains two integers xi and yi (|x|, |y| ≤ 10 000, x2 + y2 > 0) — the coordinates of the i-th vector. Vectors are numbered from 1 to n in order of appearing in the input. It is guaranteed tha... | Print two integer numbers a and b (a ≠ b) — a pair of indices of vectors with the minimal non-oriented angle. You can print the numbers in any order. If there are many possible answers, print any. | Input: 4-1 00 -11 01 1 | Output: 3 4 | Expert | 2 | 389 | 421 | 196 | 5 | |
1,250 | D | 1250D | D. Conference Problem | 3,000 | dp | A large-scale conference on unnatural sciences is going to be held soon in Berland! In total, \(n\) scientists from all around the world have applied. All of them have indicated a time segment when they will attend the conference: two integers \(l_i\), \(r_i\) — day of arrival and day of departure.Also, some of the sci... | The first line of the input contains integer \(t\) (\(1 \le t \le 100\)) — number of test cases. Then the test cases follow.The first line of each test case contains integer \(n\) (\(1 \le n \le 500\)) — the number of registered conference participants.Next \(n\) lines follow, each containing three integers \(l_i\), \(... | Output \(t\) integers — maximum number of upset scientists for each test case. | Input: 2 4 1 10 30 5 6 30 6 12 0 1 1 0 4 1 2 1 2 3 0 3 4 0 4 5 2 | Output: 4 2 | Master | 1 | 1,205 | 594 | 78 | 12 | |
1,451 | E1 | 1451E1 | E1. Bitwise Queries (Easy Version) | 2,000 | bitmasks; constructive algorithms; interactive; math | The only difference between the easy and hard versions is the constraints on the number of queries.This is an interactive problem.Ridbit has a hidden array \(a\) of \(n\) integers which he wants Ashish to guess. Note that \(n\) is a power of two. Ashish is allowed to ask three different types of queries. They are of th... | The first line of input contains one integer \(n\) \((4 \le n \le 2^{16})\) — the length of the array. It is guaranteed that \(n\) is a power of two. | The array \(a\) in the example is \([0, 0, 2, 3]\). | Input: 4 0 2 3 | Output: OR 1 2 OR 2 3 XOR 2 4 ! 0 0 2 3 | Hard | 4 | 840 | 149 | 0 | 14 | |
1,699 | A | 1699A | A. The Third Three Number Problem | 800 | constructive algorithms; math | You are given a positive integer \(n\). Your task is to find any three integers \(a\), \(b\) and \(c\) (\(0 \le a, b, c \le 10^9\)) for which \((a\oplus b)+(b\oplus c)+(a\oplus c)=n\), or determine that there are no such integers.Here \(a \oplus b\) denotes the bitwise XOR of \(a\) and \(b\). For example, \(2 \oplus 4 ... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. The following lines contain the descriptions of the test cases.The only line of each test case contains a single integer \(n\) (\(1 \le n \le 10^9\)). | For each test case, print any three integers \(a\), \(b\) and \(c\) (\(0 \le a, b, c \le 10^9\)) for which \((a\oplus b)+(b\oplus c)+(a\oplus c)=n\). If no such integers exist, print \(-1\). | In the first test case, \(a=3\), \(b=3\), \(c=1\), so \((3 \oplus 3)+(3 \oplus 1) + (3 \oplus 1)=0+2+2=4\).In the second test case, there are no solutions.In the third test case, \((2 \oplus 4)+(4 \oplus 6) + (2 \oplus 6)=6+2+4=12\). | Input: 541122046194723326 | Output: 3 3 1 -1 2 4 6 69 420 666 12345678 87654321 100000000 | Beginner | 2 | 347 | 288 | 190 | 16 |
1,136 | D | 1136D | D. Nastya Is Buying Lunch | 1,800 | greedy | At the big break Nastya came to the school dining room. There are \(n\) pupils in the school, numbered from \(1\) to \(n\). Unfortunately, Nastya came pretty late, so that all pupils had already stood in the queue, i.e. Nastya took the last place in the queue. Of course, it's a little bit sad for Nastya, but she is not... | The first line contains two integers \(n\) and \(m\) (\(1 \leq n \leq 3 \cdot 10^{5}\), \(0 \leq m \leq 5 \cdot 10^{5}\)) — the number of pupils in the queue and number of pairs of pupils such that the first one agrees to change places with the second one if the first is directly in front of the second.The second line ... | Print a single integer — the number of places in queue she can move forward. | In the first example Nastya can just change places with the first pupil in the queue.Optimal sequence of changes in the second example is change places for pupils with numbers \(1\) and \(3\). change places for pupils with numbers \(3\) and \(2\). change places for pupils with numbers \(1\) and \(2\). The queue looks l... | Input: 2 11 21 2 | Output: 1 | Medium | 1 | 695 | 1,162 | 76 | 11 |
1,925 | D | 1925D | D. Good Trip | 1,900 | combinatorics; dp; math; probabilities | There are \(n\) children in a class, \(m\) pairs among them are friends. The \(i\)-th pair who are friends have a friendship value of \(f_i\). The teacher has to go for \(k\) excursions, and for each of the excursions she chooses a pair of children randomly, equiprobably and independently. If a pair of children who are... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 5 \cdot 10^4\)). Description of the test cases follows.The first line of each test case contains \(3\) integers \(n\), \(m\) and \(k\) (\(2 \le n \le 10^5\), \(0 \le m \le \min \Big(10^5\), \( \frac{n(n-1)}{2} ... | For each test case, print one integer — the answer to the problem. | For the first test case, there are no pairs of friends, so the friendship value of all pairs is \(0\) and stays \(0\) for subsequent rounds, hence the friendship value for all excursions is \(0\).For the second test case, there is only one pair possible \((1, 2)\) and its friendship value is initially \(1\), so each tu... | Input: 4100 0 242 1 101 2 13 1 22 1 15 2 41 2 253 2 24 | Output: 0 55 777777784 40000020 | Hard | 4 | 901 | 886 | 66 | 19 |
1,399 | F | 1399F | F. Yet Another Segments Subset | 2,300 | data structures; dp; graphs; sortings | You are given \(n\) segments on a coordinate axis \(OX\). The \(i\)-th segment has borders \([l_i; r_i]\). All points \(x\), for which \(l_i \le x \le r_i\) holds, belong to the \(i\)-th segment.Your task is to choose the maximum by size (the number of segments) subset of the given set of segments such that each pair o... | The first line of the input contains one integer \(t\) (\(1 \le t \le 1000\)) — the number of test cases. Then \(t\) test cases follow.The first line of the test case contains one integer \(n\) (\(1 \le n \le 3000\)) — the number of segments. The next \(n\) lines describe segments. The \(i\)-th segment is given as two ... | For each test case, print the answer: the maximum possible size of the subset of the given set of segments such that each pair of segments in this subset either non-intersecting or one of them lies inside the other one. | Input: 4 4 1 5 2 4 2 3 3 4 5 1 5 2 3 2 5 3 5 2 2 3 1 3 2 4 2 3 7 1 10 2 8 2 5 3 4 4 4 6 8 7 7 | Output: 3 4 2 7 | Expert | 4 | 993 | 674 | 219 | 13 | |
362 | C | 362C | C. Insertion Sort | 1,900 | data structures; dp; implementation; math | Petya is a beginner programmer. He has already mastered the basics of the C++ language and moved on to learning algorithms. The first algorithm he encountered was insertion sort. Petya has already written the code that implements this algorithm and sorts the given integer zero-indexed array a of size n in the non-decre... | The first line contains a single integer n (2 ≤ n ≤ 5000) — the length of the permutation. The second line contains n different integers from 0 to n - 1, inclusive — the actual permutation. | Print two integers: the minimum number of times the swap function is executed and the number of such pairs (i, j) that swapping the elements of the input permutation with indexes i and j leads to the minimum number of the executions. | In the first sample the appropriate pairs are (0, 3) and (0, 4). In the second sample the appropriate pairs are (0, 4), (1, 4), (2, 4) and (3, 4). | Input: 54 0 3 1 2 | Output: 3 2 | Hard | 4 | 1,072 | 189 | 233 | 3 |
1,032 | G | 1032G | G. Chattering | 2,900 | There are \(n\) parrots standing in a circle. Each parrot has a certain level of respect among other parrots, namely \(r_i\). When a parrot with respect level \(x\) starts chattering, \(x\) neighbours to the right and to the left of it start repeating the same words in 1 second. Their neighbours then start repeating as... | In the first line of input there is a single integer \(n\), the number of parrots (\(1 \leq n \leq 10^5\)).In the next line of input there are \(n\) integers \(r_1\), ..., \(r_n\), the respect levels of parrots in order they stand in the circle (\(1 \leq r_i \leq n\)). | Print \(n\) integers. \(i\)-th of them should equal the number of seconds that is needed for all parrots to start chattering if the \(i\)-th parrot is the first to start. | Input: 4 1 1 4 1 | Output: 2 2 1 2 | Master | 0 | 574 | 269 | 170 | 10 | ||
1,085 | G | 1085G | G. Beautiful Matrix | 2,900 | combinatorics; data structures; dp | Petya collects beautiful matrix.A matrix of size \(n \times n\) is beautiful if: All elements of the matrix are integers between \(1\) and \(n\); For every row of the matrix, all elements of this row are different; For every pair of vertically adjacent elements, these elements are different. Today Petya bought a beauti... | The first line contains one integer \(n\) (\(1 \le n \le 2000\)) — the number of rows and columns in \(a\).Each of the next \(n\) lines contains \(n\) integers \(a_{i,j}\) (\(1 \le a_{i,j} \le n\)) — the elements of \(a\).It is guaranteed that \(a\) is a beautiful matrix. | Print one integer — the rarity of matrix \(a\), taken modulo \(998\,244\,353\). | There are only \(2\) beautiful matrices of size \(2 \times 2\): There are the first \(5\) beautiful matrices of size \(3 \times 3\) in lexicographical order: | Input: 2 2 1 1 2 | Output: 1 | Master | 3 | 763 | 272 | 79 | 10 |
1,288 | D | 1288D | D. Minimax Problem | 2,000 | binary search; bitmasks; dp | You are given \(n\) arrays \(a_1\), \(a_2\), ..., \(a_n\); each array consists of exactly \(m\) integers. We denote the \(y\)-th element of the \(x\)-th array as \(a_{x, y}\).You have to choose two arrays \(a_i\) and \(a_j\) (\(1 \le i, j \le n\), it is possible that \(i = j\)). After that, you will obtain a new array ... | The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 3 \cdot 10^5\), \(1 \le m \le 8\)) — the number of arrays and the number of elements in each array, respectively.Then \(n\) lines follow, the \(x\)-th line contains the array \(a_x\) represented by \(m\) integers \(a_{x, 1}\), \(a_{x, 2}\), ..., \(a_{x... | Print two integers \(i\) and \(j\) (\(1 \le i, j \le n\), it is possible that \(i = j\)) — the indices of the two arrays you have to choose so that the value of \(\min \limits_{k = 1}^{m} b_k\) is maximum possible. If there are multiple answers, print any of them. | Input: 6 5 5 0 3 1 2 1 8 9 1 3 1 2 3 4 5 9 1 0 3 7 2 3 0 6 3 6 4 1 7 0 | Output: 1 5 | Hard | 3 | 541 | 357 | 264 | 12 | |
1,336 | B | 1336B | B. Xenia and Colorful Gems | 1,700 | binary search; greedy; math; sortings; two pointers | Xenia is a girl being born a noble. Due to the inflexibility and harshness of her family, Xenia has to find some ways to amuse herself. Recently Xenia has bought \(n_r\) red gems, \(n_g\) green gems and \(n_b\) blue gems. Each of the gems has a weight.Now, she is going to pick three gems.Xenia loves colorful things, so... | The first line contains a single integer \(t\) (\(1\le t \le 100\)) — the number of test cases. Then \(t\) test cases follow.The first line of each test case contains three integers \(n_r,n_g,n_b\) (\(1\le n_r,n_g,n_b\le 10^5\)) — the number of red gems, green gems and blue gems respectively.The second line of each tes... | For each test case, print a line contains one integer — the minimum value which Xenia wants to find. | In the first test case, Xenia has the following gems:If she picks the red gem with weight \(7\), the green gem with weight \(6\), and the blue gem with weight \(4\), she will achieve the most balanced selection with \((x-y)^2+(y-z)^2+(z-x)^2=(7-6)^2+(6-4)^2+(4-7)^2=14\). | Input: 5 2 2 3 7 8 6 3 3 1 4 1 1 1 1 1 1000000000 2 2 2 1 2 5 4 6 7 2 2 2 1 2 3 4 6 7 3 4 1 3 2 1 7 3 3 4 6 | Output: 14 1999999996000000002 24 24 14 | Medium | 5 | 638 | 891 | 100 | 13 |
628 | E | 628E | E. Zbazi in Zeydabad | 2,300 | data structures; implementation | A tourist wants to visit country Zeydabad for Zbazi (a local game in Zeydabad).The country Zeydabad is a rectangular table consisting of n rows and m columns. Each cell on the country is either 'z' or '.'.The tourist knows this country is named Zeydabad because there are lots of ''Z-pattern""s in the country. A ''Z-pat... | The first line contains two integers n, m (1 ≤ n, m ≤ 3000) — the number of rows and columns respectively.Each of the next n lines contains m characters 'z' or '.' — the description of Zeydabad. | Print the only integer a — the number of ''Z-pattern""s in Zeydabad. | Input: 4 4zzzzzzz..z..zzzz | Output: 16 | Expert | 2 | 990 | 194 | 68 | 6 | |
1,805 | B | 1805B | B. The String Has a Target | 800 | greedy; strings | You are given a string \(s\). You can apply this operation to the string exactly once: choose index \(i\) and move character \(s_i\) to the beginning of the string (removing it at the old position). For example, if you apply the operation with index \(i=4\) to the string ""abaacd"" with numbering from \(1\), you get th... | 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 a single integer \(n\) (\(1 \le n \le 10 ^ 5\)) — the length of the string.The second line of each test case contains... | For each test case, on a separate line print the lexicographically smallest string that can be obtained after applying the operation to the original string exactly once. | In the first test case, you need to move the last character to the beginning.In the second case, you need to move the second letter ""a"".In the third set you need to apply the operation with \(i=1\), then the string will not change. | Input: 43cba4acac5abbcb4aaba | Output: acb aacc abbcb aaab | Beginner | 2 | 724 | 481 | 169 | 18 |
1,861 | A | 1861A | A. Prime Deletion | 800 | constructive algorithms; math | A prime number is a positive integer that has exactly two different positive divisors: \(1\) and the integer itself. For example, \(2\), \(3\), \(13\) and \(101\) are prime numbers; \(1\), \(4\), \(6\) and \(42\) are not.You are given a sequence of digits from \(1\) to \(9\), in which every digit from \(1\) to \(9\) ap... | The first line contains one integer \(t\) (\(1 \le t \le 5000\)) — the number of test cases.Each test case consists of one line containing a string of \(9\) digits (without any characters between them). Each digit from \(1\) to \(9\) appears in this string exactly once. | For each test case, print the answer on a separate line as follows: if it is impossible to perform the described operations, print \(-1\); otherwise, print any sequence that represents a prime number, which can be obtained from the given string performing several (maybe zero) operations described in the statement. If t... | Input: 4123456789987654321243567918576318429 | Output: 167 53 3571 57638429 | Beginner | 2 | 810 | 270 | 384 | 18 | |
1,540 | D | 1540D | D. Inverse Inversions | 3,200 | binary search; brute force; data structures | You were playing with permutation \(p\) of length \(n\), but you lost it in Blair, Alabama!Luckily, you remember some information about the permutation. More specifically, you remember an array \(b\) of length \(n\), where \(b_i\) is the number of indices \(j\) such that \(j < i\) and \(p_j > p_i\).You have the array \... | The first line contains a single integer \(n\) (\(1 \leq n \leq 10^5\)) — the size of permutation.The second line contains \(n\) integers \(b_1, b_2 \ldots, b_n\) (\(0 \leq b_i < i\)) — your initial memory of the array \(b\).The third line contains a single integer \(q\) (\(1 \leq q \leq 10^5\)) — the number of queries... | For each query of type \(2\), print one integer — the answer to the query. | For the first sample, there's initially only one possible permutation that satisfies the constraints: \([1, 2, 3]\), as it must have \(0\) inversions.After the query of type \(1\), the array \(b\) is \([0, 1, 0]\). The only permutation \(p\) that produces this array is \([2, 1, 3]\). With this permutation, \(b_2\) is e... | Input: 3 0 0 0 7 2 1 2 2 2 3 1 2 1 2 1 2 2 2 3 | Output: 1 2 3 2 1 3 | Master | 3 | 850 | 618 | 74 | 15 |
1,431 | F | 1431F | F. Neural Network Problem | 2,100 | *special; binary search; greedy | You want to train a neural network model for your graduation work. There are \(n\) images in the dataset, the \(i\)-th image's size is \(a_i\) bytes.You don't have any powerful remote servers to train this model so you have to do it on your local machine. But there is a problem: the total size of the dataset is too big... | The first line of the input contains three integers \(n\), \(k\) and \(x\) (\(1 \le n \le 10^5\); \(1 \le k, x \le n\)) — the number of images in the dataset, the maximum number of images you can remove and the length of each block (except maybe for the last one), respectively.The second line of the input contains \(n\... | Print one integer: the minimum possible value of the metric described in the problem statement after removing no more than \(k\) images from the dataset. | In the first example, you can remove the whole array so the answer is \(0\).In the second example, you can remove the first and the last elements of \(a\) and obtain \(b = [1, 5, 5]\). The size of the first (and the only) block is \(11\). So the answer is \(11\).In the third example, you can remove the second element o... | Input: 5 5 4 1 1 5 4 5 | Output: 0 | Hard | 3 | 2,084 | 431 | 153 | 14 |
1,970 | F3 | 1970F3 | F3. Playing Quidditch (Hard) | 2,300 | implementation | This afternoon, you decided to enjoy the first days of Spring by taking a walk outside. As you come near the Quidditch field, you hear screams. Once again, there is a conflict about the score: the two teams are convinced that they won the game! To prevent this problem from happening one more time, you decide to get inv... | On the first line, the integers \(N\) and \(M\).The description of the field follows: \(N\) lines of \(M\) pairs of characters separated by spaces. Each pair of characters represents a position on the field. It can be either: .. to represent an empty cell R0, ..., R9, B0, ..., B9 to represent a player. The first charac... | You must output the description of the main events of the game, one event per line. More precisely: Each time a team scores, you must print t RED GOAL or t BLUE GOAL, depending on the team who scored, where t is the current time (the position of the action in the list of actions, starting from 0). In the case where a p... | In the first example, the red player takes the Quaffle, move it and throw it. The blue player catches the ball, goes to the red goal and scores.In the second example, the red player takes the ball and scores in the goal of their own team: the blue team wins a point.In the third example, the Bludger goes at the position... | Input: 3 5.. .. R0 .. ..RG .. .Q .. BG.. .. B0 .. ..12R0 DR0 C .QR0 RR0 TR0 DB0 RB0 UB0 C .QB0 LB0 LB0 LB0 T | Output: 11 BLUE GOAL FINAL SCORE: 0 1 | Expert | 1 | 2,483 | 1,843 | 1,218 | 19 |
1,041 | D | 1041D | D. Glider | 1,700 | binary search; data structures; two pointers | A plane is flying at a constant height of \(h\) meters above the ground surface. Let's consider that it is flying from the point \((-10^9, h)\) to the point \((10^9, h)\) parallel with \(Ox\) axis.A glider is inside the plane, ready to start his flight at any moment (for the sake of simplicity let's consider that he ma... | The first line contains two integers \(n\) and \(h\) \((1 \le n \le 2\cdot10^{5}, 1 \le h \le 10^{9})\) — the number of ascending air flow segments and the altitude at which the plane is flying, respectively.Each of the next \(n\) lines contains two integers \(x_{i1}\) and \(x_{i2}\) \((1 \le x_{i1} < x_{i2} \le 10^{9}... | Print one integer — the maximum distance along \(Ox\) axis that the glider can fly from the point where he jumps off the plane to the point where he lands if he can start his flight at any integer coordinate. | In the first example if the glider can jump out at \((2, 4)\), then the landing point is \((12, 0)\), so the distance is \(12-2 = 10\).In the second example the glider can fly from \((16,10)\) to \((34,0)\), and the distance is \(34-16=18\).In the third example the glider can fly from \((-100,1000000000)\) to \((199999... | Input: 3 42 57 910 11 | Output: 10 | Medium | 3 | 1,523 | 449 | 208 | 10 |
62 | B | 62B | B. Tyndex.Brome | 1,800 | binary search; implementation | Tyndex is again well ahead of the rivals! The reaction to the release of Zoozle Chrome browser was the release of a new browser Tyndex.Brome!The popularity of the new browser is growing daily. And the secret is not even the Tyndex.Bar installed (the Tyndex.Bar automatically fills the glass with the finest 1664 cognac a... | The first line contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ 105). They are the number of potential addresses and the length of the address entered by the user. The next line contains k lowercase Latin letters. They are the address entered by the user (s). Each next i-th (1 ≤ i ≤ n) line contains a non-empty sequ... | On each n line of the output file print a single number: the value of the error function when the current potential address is chosen.Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). | Input: 2 10codeforcescodeforcescodehorses | Output: 012 | Medium | 2 | 1,685 | 463 | 265 | 0 | |
501 | D | 501D | D. Misha and Permutations Summation | 2,000 | data structures | Let's define the sum of two permutations p and q of numbers 0, 1, ..., (n - 1) as permutation , where Perm(x) is the x-th lexicographically permutation of numbers 0, 1, ..., (n - 1) (counting from zero), and Ord(p) is the number of permutation p in the lexicographical order.For example, Perm(0) = (0, 1, ..., n - 2, n -... | The first line contains an integer n (1 ≤ n ≤ 200 000).The second line contains n distinct integers from 0 to n - 1, separated by a space, forming permutation p.The third line contains n distinct integers from 0 to n - 1, separated by spaces, forming permutation q. | Print n distinct integers from 0 to n - 1, forming the sum of the given permutations. Separate the numbers by spaces. | Permutations of numbers from 0 to 1 in the lexicographical order: (0, 1), (1, 0).In the first sample Ord(p) = 0 and Ord(q) = 0, so the answer is .In the second sample Ord(p) = 0 and Ord(q) = 1, so the answer is .Permutations of numbers from 0 to 2 in the lexicographical order: (0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0)... | Input: 20 10 1 | Output: 0 1 | Hard | 1 | 646 | 265 | 117 | 5 |
2,039 | H2 | 2039H2 | H2. Cool Swap Walk (Hard Version) | 3,500 | constructive algorithms; constructive algorithms; constructive algorithms; implementation; sortings | This is the hard version of the problem. The only difference is the maximum number of operations you can perform. You can only make hacks if both versions are solved.You are given an array \(a\) of size \(n\).A cool swap walk is the following process: In an \(n \times n\) grid, we note the cells in row \(i\) and column... | 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 an integer \(n\) (\(2 \leq n \leq 500\)) — the size of the array.The second line of each test case contains \(n\) integers \(a_1,a_2,\ldots ,a_n\) (\(1 \le a_i \le n\)) — the elements of ... | For each test case, your output should consist of several lines: The first line contains an integer \(k\) (\(0 \leq k \leq n+4\)), representing the number of cool swap walks you perform. Each of the next \(k\) lines contains a string \(s\) of length \(2n-2\) consisting only of R and D, representing the path (letters ar... | In the first test case, the array \(a\) is already non-decreasing, so you don't need to perform any walk.In the second test case, \(a=[2,1,3]\) initially.In the first walk: In the \(1\)-st step, you step right to \((1,2)\). Then, \(a=[1,2,3]\). Note that although the array \(a\) is already non-decreasing, you can not s... | Input: 321 232 1 343 2 3 4 | Output: 0 2 RRDD DRDR 3 RRDRDD DRDDRR DDRRRD | Master | 5 | 822 | 426 | 466 | 20 |
774 | C | 774C | C. Maximum Number | 1,200 | *special; constructive algorithms; greedy; implementation | Stepan has the newest electronic device with a display. Different digits can be shown on it. Each digit is shown on a seven-section indicator like it is shown on the picture below. So, for example, to show the digit 3 on the display, 5 sections must be highlighted; and for the digit 6, 6 sections must be highlighted. T... | The first line contains the integer n (2 ≤ n ≤ 100 000) — the maximum number of sections which can be highlighted on the display. | Print the maximum integer which can be shown on the display of Stepan's newest device. | Input: 2 | Output: 1 | Easy | 4 | 679 | 129 | 86 | 7 | |
1,742 | D | 1742D | D. Coprime | 1,100 | brute force; greedy; number theory | Given an array of \(n\) positive integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 1000\)). Find the maximum value of \(i + j\) such that \(a_i\) and \(a_j\) are coprime,\(^{\dagger}\) or \(-1\) if no such \(i\), \(j\) exist.For example consider the array \([1, 3, 5, 2, 4, 7, 7]\). The maximum value of \(i + j\) that ... | The input consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 10\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains an integer \(n\) (\(2 \leq n \leq 2\cdot10^5\)) — the length of the array.The following line contains... | For each test case, output a single integer — the maximum value of \(i + j\) such that \(i\) and \(j\) satisfy the condition that \(a_i\) and \(a_j\) are coprime, or output \(-1\) in case no \(i\), \(j\) satisfy the condition. | For the first test case, we can choose \(i = j = 3\), with sum of indices equal to \(6\), since \(1\) and \(1\) are coprime.For the second test case, we can choose \(i = 7\) and \(j = 5\), with sum of indices equal to \(7 + 5 = 12\), since \(7\) and \(4\) are coprime. | Input: 633 2 171 3 5 2 4 7 751 2 3 4 532 2 465 4 3 15 12 1651 2 2 3 6 | Output: 6 12 9 -1 10 7 | Easy | 3 | 574 | 536 | 226 | 17 |
1,444 | C | 1444C | C. Team-Building | 2,500 | data structures; dfs and similar; dsu; graphs | The new academic year has started, and Berland's university has \(n\) first-year students. They are divided into \(k\) academic groups, however, some of the groups might be empty. Among the students, there are \(m\) pairs of acquaintances, and each acquaintance pair might be both in a common group or be in two differen... | The first line contains three integers \(n\), \(m\) and \(k\) (\(1 \le n \le 500\,000\); \(0 \le m \le 500\,000\); \(2 \le k \le 500\,000\)) — the number of students, the number of pairs of acquaintances and the number of groups respectively.The second line contains \(n\) integers \(c_1, c_2, \dots, c_n\) (\(1 \le c_i ... | Print a single integer — the number of ways to choose two different groups such that it's possible to select two teams to play the game. | The acquaintances graph for the first example is shown in the picture below (next to each student there is their group number written).In that test we can select the following groups: Select the first and the second groups. For instance, one team can be formed from students \(1\) and \(4\), while other team can be form... | Input: 6 8 3 1 1 2 2 3 3 1 3 1 5 1 6 2 5 2 6 3 4 3 5 5 6 | Output: 2 | Expert | 4 | 994 | 670 | 136 | 14 |
1,178 | H | 1178H | H. Stock Exchange | 3,500 | binary search; flows; graphs | Warning: This problem has an unusual memory limit!Bob decided that he will not waste his prime years implementing GUI forms for a large corporation and instead will earn his supper on the Stock Exchange Reykjavik. The Stock Exchange Reykjavik is the only actual stock exchange in the world. The only type of transaction ... | The first line contains a single integer \(n\) (\(1 \leq n \leq 2200\)) — the number stocks currently owned by Bob.Each of the next \(2n\) lines contains integers \(a_i\) and \(b_i\) (\(0 \leq a_i, b_i \leq 10^9\)), representing the stock price of stock \(i\). | If it is impossible for Bob to achieve his goal, output a single integer \(-1\).Otherwise, output two integers \(T\) and \(E\), where \(T\) is the minimum time in which he can achieve his goal, and \(E\) is the minimum number of exchanges in which he can achieve his goal at time \(T\). | In the first example, Bob simply waits until time \(t = 3\), when both stocks cost exactly the same amount.In the second example, the optimum strategy is to exchange stock \(2\) for stock \(1\) at time \(t = 1\), then exchange one share of stock \(1\) for stock \(3\) at time \(t = 5\) (where both cost \(15\)) and then ... | Input: 1 3 10 1 16 | Output: 3 1 | Master | 3 | 1,216 | 260 | 286 | 11 |
295 | C | 295C | C. Greg and Friends | 2,100 | combinatorics; dp; graphs; shortest paths | One day Greg and his friends were walking in the forest. Overall there were n people walking, including Greg. Soon he found himself in front of a river. The guys immediately decided to get across the river. Luckily, there was a boat by the river bank, just where the guys were standing. We know that the boat can hold pe... | The first line contains two integers n, k (1 ≤ n ≤ 50, 1 ≤ k ≤ 5000) — the number of people, including Greg, and the boat's weight limit. The next line contains n integers — the people's weights. A person's weight is either 50 kilos or 100 kilos.You can consider Greg and his friends indexed in some way. | In the first line print an integer — the minimum number of rides. If transporting everyone to the other bank is impossible, print an integer -1.In the second line print the remainder after dividing the number of ways to transport the people in the minimum number of rides by number 1000000007 (109 + 7). If transporting ... | In the first test Greg walks alone and consequently, he needs only one ride across the river.In the second test you should follow the plan: transport two 50 kg. people; transport one 50 kg. person back; transport one 100 kg. person; transport one 50 kg. person back; transport two 50 kg. people. That totals to 5 rides. ... | Input: 1 5050 | Output: 11 | Hard | 4 | 1,142 | 304 | 378 | 2 |
14 | D | 14D | D. Two Paths | 1,900 | dfs and similar; dp; graphs; shortest paths; trees; two pointers | As you know, Bob's brother lives in Flatland. In Flatland there are n cities, connected by n - 1 two-way roads. The cities are numbered from 1 to n. You can get from one city to another moving along the roads.The «Two Paths» company, where Bob's brother works, has won a tender to repair two paths in Flatland. A path is... | The first line contains an integer n (2 ≤ n ≤ 200), where n is the amount of cities in the country. The following n - 1 lines contain the information about the roads. Each line contains a pair of numbers of the cities, connected by the road ai, bi (1 ≤ ai, bi ≤ n). | Output the maximum possible profit. | Input: 41 22 33 4 | Output: 1 | Hard | 6 | 833 | 265 | 35 | 0 | |
995 | A | 995A | A. Tesla | 2,100 | constructive algorithms; implementation | Allen dreams of one day owning a enormous fleet of electric cars, the car of the future! He knows that this will give him a big status boost. As Allen is planning out all of the different types of cars he will own and how he will arrange them, he realizes that he has a problem. Allen's future parking lot can be represe... | The first line of the input contains two space-separated integers \(n\) and \(k\) (\(1 \le n \le 50\), \(1 \le k \le 2n\)), representing the number of columns and the number of cars, respectively.The next four lines will contain \(n\) integers each between \(0\) and \(k\) inclusive, representing the initial state of th... | If there is a sequence of moves that brings all of the cars to their parking spaces, with at most \(20000\) car moves, then print \(m\), the number of moves, on the first line. On the following \(m\) lines, print the moves (one move per line) in the format \(i\) \(r\) \(c\), which corresponds to Allen moving car \(i\) ... | In the first sample test case, all cars are in front of their spots except car \(5\), which is in front of the parking spot adjacent. The example shows the shortest possible sequence of moves, but any sequence of length at most \(20000\) will be accepted.In the second sample test case, there is only one column, and the... | Input: 4 51 2 0 41 2 0 45 0 0 30 5 0 3 | Output: 61 1 12 1 24 1 43 4 45 3 25 4 2 | Hard | 2 | 1,368 | 1,011 | 528 | 9 |
981 | H | 981H | H. K Paths | 3,100 | combinatorics; data structures; dp; fft; math | You are given a tree of \(n\) vertices. You are to select \(k\) (not necessarily distinct) simple paths in such a way that it is possible to split all edges of the tree into three sets: edges not contained in any path, edges that are a part of exactly one of these paths, and edges that are parts of all selected paths, ... | The first line contains two integers \(n\) and \(k\) (\(1 \leq n, k \leq 10^{5}\)) — the number of vertices in the tree and the desired number of paths.The next \(n - 1\) lines describe edges of the tree. Each line contains two integers \(a\) and \(b\) (\(1 \le a, b \le n\), \(a \ne b\)) — the endpoints of an edge. It ... | Print the number of ways to select \(k\) enumerated not necessarily distinct simple paths in such a way that for each edge either it is not contained in any path, or it is contained in exactly one path, or it is contained in all \(k\) paths, and the intersection of all paths is non-empty. As the answer can be large, pr... | In the first example the following ways are valid: \(((1,2), (1,2))\), \(((1,2), (1,3))\), \(((1,3), (1,2))\), \(((1,3), (1,3))\), \(((1,3), (2,3))\), \(((2,3), (1,3))\), \(((2,3), (2,3))\). In the second example \(k=1\), so all \(n \cdot (n - 1) / 2 = 5 \cdot 4 / 2 = 10\) paths are valid.In the third example, the answ... | Input: 3 21 22 3 | Output: 7 | Master | 5 | 651 | 367 | 348 | 9 |
2,046 | E1 | 2046E1 | E1. Cheops and a Contest (Easy Version) | 2,900 | constructive algorithms; greedy | This is the easy version of the problem. The difference between the versions is that in this version, \(m\) equals \(2\). You can hack only if you solved all versions of this problem. There is a problem-solving competition in Ancient Egypt with \(n\) participants, numbered from \(1\) to \(n\). Each participant comes fr... | 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\) (\(2 \mathbf{=} m \le n \le 3 \cdot {10}^5\)) — the number of participants and the number ... | For each test case, if there exists a set of problems that satisfies Cheops' conditions, then in the first line output a single integer \(p\) (\(1 \le p \le 5n\)) — the number of problems in your solution.Then output \(p\) lines, each containing two integers \(d\) and \(t\) (\(0 \le d, t \le {10}^9\)) — the difficulty ... | Input: 25 25 7 16 7 23 9 25 10 34 4 12 1 23 3 4 52 21 2 11 2 11 21 1 | Output: 7 6 4 6 5 5 6 5 7 4 8 4 9 7 1 -1 | Master | 2 | 1,255 | 1,100 | 464 | 20 | |
1,576 | A | 1576A | A. Communication Routing Challenge | 0 | *special | In optical communication networks, appropriate path planning can improve the utilization of communication resources and bring a smooth communication experience to users. The following figure shows an inter-satellite optical communication network. User messages are sent from one terrestrial base station (nodes \(4\) to ... | The first line contains four integers separated by space: \(\mathit{NodeCount}\), \(\mathit{EdgeCount}\), \(\mathit{ConstrainedCount}\), and \(\mathit{FlowCount}\). \(8 \le \mathit{NodeCount} \le 1400\), \(15 \le \mathit{EdgeCount} \le 15000\), \(3 \le \mathit{ConstrainedCount} \le 3600\), \(1 \le \mathit{FlowCount} \l... | In the first line, output the number of your success flows.Next, each line output edge information about the path that a flow passes through. The format is as follows: \(\mathit{FlowID}\ \mathit{EdgeID}_1\ \mathit{EdgeID}_2\ \mathit{EdgeID}_3\ \dots\ \mathit{EdgeID}_n\).There is no requirement on the output sequence be... | The total distance of a flow path is \(620\) (\(120 + 100 + 100 + 300 = 620\)). (Note: 0 9 10 12 13 is also a valid output result.) | Input: 8 15 3 1 0 0 0 1 100 1050 1 1 0 1 200 2200 2 1 0 1 200 99400 3 2 0 3 100 450 4 3 0 3 500 1120 5 4 1 2 1000 40000 6 5 2 3 600 10000 7 5 2 3 600 10000 8 6 1 4 120 2500 9 6 1 4 120 450 10 7 1 5 170 1250 11 8 2 5 200 2500 12 9 3 5 100 1250 13 10 3 6 300 1150 14 11 3 7 300 1100 2 5 7 2 6 7 2 6 11 0 4 6 100 | Output: ... | Beginner | 1 | 3,397 | 2,097 | 602 | 15 |
1,337 | B | 1337B | B. Kana and Dragon Quest game | 900 | greedy; implementation; math | Kana was just an ordinary high school girl before a talent scout discovered her. Then, she became an idol. But different from the stereotype, she is also a gameholic. One day Kana gets interested in a new adventure game called Dragon Quest. In this game, her quest is to beat a dragon. The dragon has a hit point of \(x\... | The first line contains a single integer \(t\) (\(1 \leq t \leq 1000\)) — the number of test cases.The next \(t\) lines describe test cases. For each test case the only line contains three integers \(x\), \(n\), \(m\) (\(1\le x \le 10^5\), \(0\le n,m\le30\)) — the dragon's intitial hit point, the maximum number of Void... | If it is possible to defeat the dragon, print ""YES"" (without quotes). Otherwise, print ""NO"" (without quotes).You can print each letter in any case (upper or lower). | One possible casting sequence of the first test case is shown below: Void Absorption \(\left\lfloor \frac{100}{2} \right\rfloor + 10=60\). Lightning Strike \(60-10=50\). Void Absorption \(\left\lfloor \frac{50}{2} \right\rfloor + 10=35\). Void Absorption \(\left\lfloor \frac{35}{2} \right\rfloor + 10=27\). Lightning St... | Input: 7 100 3 4 189 3 4 64 2 3 63 2 3 30 27 7 10 9 1 69117 21 2 | Output: YES NO NO YES YES YES YES | Beginner | 3 | 1,232 | 382 | 168 | 13 |
1,442 | C | 1442C | C. Graph Transpositions | 2,400 | dfs and similar; graphs; greedy; shortest paths | You are given a directed graph of \(n\) vertices and \(m\) edges. Vertices are numbered from \(1\) to \(n\). There is a token in vertex \(1\).The following actions are allowed: Token movement. To move the token from vertex \(u\) to vertex \(v\) if there is an edge \(u \to v\) in the graph. This action takes \(1\) secon... | The first line of input contains two integers \(n, m\) (\(1 \le n, m \le 200\,000\)).The next \(m\) lines contain two integers each: \(u, v\) (\(1 \le u, v \le n; u \ne v\)), which represent the edges of the graph. It is guaranteed that all ordered pairs \((u, v)\) are distinct.It is guaranteed that it is possible to m... | Print one integer: the minimum required time modulo \(998\,244\,353\). | The first example can be solved by transposing the graph and moving the token to vertex \(4\), taking \(2\) seconds.The best way to solve the second example is the following: transpose the graph, move the token to vertex \(2\), transpose the graph again, move the token to vertex \(3\), transpose the graph once more and... | Input: 4 4 1 2 2 3 3 4 4 1 | Output: 2 | Expert | 4 | 798 | 392 | 70 | 14 |
1,779 | B | 1779B | B. MKnez's ConstructiveForces Task | 900 | constructive algorithms; math | MKnez wants to construct an array \(s_1,s_2, \ldots , s_n\) satisfying the following conditions: Each element is an integer number different from \(0\); For each pair of adjacent elements their sum is equal to the sum of the whole array. More formally, \(s_i \neq 0\) must hold for each \(1 \leq i \leq n\). Moreover, it... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 100\)). The description of the test cases follows.The only line of each test case contains a single integer \(n\) (\(2 \leq n \leq 1000\)) — the length of the array. | For each test case, print ""YES"" if an array of length \(n\) satisfying the conditions exists. Otherwise, print ""NO"". If the answer is ""YES"", on the next line print a sequence \(s_1,s_2, \ldots, s_n\) satisfying the conditions. Each element should be a non-zero integer in the range \([-5000,5000]\), i. e. \(-5000 ... | In the first test case, \([9,5]\) is a valid answer since \(9+5\) (the sum of the two adjacent elements \(s_1+s_2\)) is equal to \(9+5\) (the sum of all elements). Other solutions include \([6,-9], [-1,-2], [-5000,5000], \ldots\)For the second test case, let us show why some arrays do not satisfy the constraints: \([1,... | Input: 2 2 3 | Output: YES 9 5 NO | Beginner | 2 | 498 | 277 | 583 | 17 |
862 | F | 862F | F. Mahmoud and Ehab and the final stage | 2,900 | data structures; strings | Mahmoud and Ehab solved Dr. Evil's questions so he gave them the password of the door of the evil land. When they tried to open the door using it, the door gave them a final question to solve before they leave (yes, the door is digital, Dr. Evil is modern). If they don't solve it, all the work will be useless and they ... | The first line of input contains 2 integers n and q (1 ≤ n ≤ 105, 1 ≤ q ≤ 105) – The number of strings and the number of queries, respectively.The second line contains n strings stri consisting of lowercase English letters.The next q lines describe the queries and may have one of the 2 forms: 1 a b (1 ≤ a ≤ b ≤ n). 2 x... | For each query of first type output its answer in a new line. | Input: 5 9mahmoud mahmoudbadawy drmahmoud drevil mahmoud1 1 51 1 21 2 32 3 mahmoud2 4 mahmoud2 2 mahmouu1 1 51 2 31 1 1 | Output: 14141330127 | Master | 2 | 903 | 453 | 61 | 8 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.