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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
496 | D | 496D | D. Tennis Game | 1,900 | binary search | Petya and Gena love playing table tennis. A single match is played according to the following rules: a match consists of multiple sets, each set consists of multiple serves. Each serve is won by one of the players, this player scores one point. As soon as one of the players scores t points, he wins the set; then the ne... | The first line contains a single integer n β the length of the sequence of games (1 β€ n β€ 105).The second line contains n space-separated integers ai. If ai = 1, then the i-th serve was won by Petya, if ai = 2, then the i-th serve was won by Gena.It is not guaranteed that at least one option for numbers s and t corresp... | In the first line print a single number k β the number of options for numbers s and t.In each of the following k lines print two integers si and ti β the option for numbers s and t. Print the options in the order of increasing si, and for equal si β in the order of increasing ti. | Input: 51 2 1 2 1 | Output: 21 33 1 | Hard | 1 | 1,197 | 345 | 280 | 4 | |
869 | E | 869E | E. The Untended Antiquity | 2,400 | data structures; hashing | Adieu l'ami.Koyomi is helping Oshino, an acquaintance of his, to take care of an open space around the abandoned Eikou Cram School building, Oshino's makeshift residence.The space is represented by a rectangular grid of n Γ m cells, arranged into n rows and m columns. The c-th cell in the r-th row is denoted by (r, c).... | The first line of input contains three space-separated integers n, m and q (1 β€ n, m β€ 2 500, 1 β€ q β€ 100 000) β the number of rows and columns in the grid, and the total number of Oshino and Koyomi's actions, respectively.The following q lines each describes an action, containing five space-separated integers t, r1, c... | For each of Koyomi's attempts (actions with t = 3), output one line β containing ""Yes"" (without quotes) if it's feasible, and ""No"" (without quotes) otherwise. | For the first example, the situations of Koyomi's actions are illustrated below. | Input: 5 6 51 2 2 4 51 3 3 3 33 4 4 1 12 2 2 4 53 1 1 4 4 | Output: NoYes | Expert | 2 | 1,122 | 690 | 162 | 8 |
833 | D | 833D | D. Red-Black Cobweb | 2,800 | data structures; divide and conquer; implementation; trees | Slastyona likes to watch life of nearby grove's dwellers. This time she watches a strange red-black spider sitting at the center of a huge cobweb.The cobweb is a set of n nodes connected by threads, each of the treads is either red of black. Using these threads, the spider can move between nodes. No thread connects a n... | The first line contains the number of nodes n (2 β€ n β€ 105).The next n - 1 lines contain four integers each, denoting the i-th thread of the cobweb: the nodes it connects ui, vi (1 β€ ui β€ n, 1 β€ vi β€ n), the clamminess of the thread xi (1 β€ x β€ 109 + 6), and the color of the thread ci (). The red color is denoted by 0,... | Print single integer the jelliness of the cobweb modulo 109 + 7. If there are no paths such that the numbers of red and black threads differ at most twice, print 1. | In the first example there are 4 pairs of nodes such that the numbers of threads of both colors on them differ at most twice. There pairs are (1, 3) with product of clamminess equal to 45, (1, 5) with product of clamminess equal to 45, (3, 4) with product of clamminess equal to 25 and (4, 5) with product of clamminess ... | Input: 51 2 9 02 3 5 12 4 5 02 5 5 1 | Output: 1265625 | Master | 4 | 1,111 | 357 | 164 | 8 |
291 | E | 291E | E. Tree-String Problem | 2,000 | *special; dfs and similar; hashing; strings | A rooted tree is a non-directed connected graph without any cycles with a distinguished vertex, which is called the tree root. Consider the vertices of a rooted tree, that consists of n vertices, numbered from 1 to n. In this problem the tree root is the vertex number 1.Let's represent the length of the shortest by the... | The first line contains integer n (2 β€ n β€ 105) β the number of vertices of Polycarpus's tree. Next n - 1 lines contain the tree edges. The i-th of them contains number pi + 1 and string si + 1 (1 β€ pi + 1 β€ n; pi + 1 β (i + 1)). String si + 1 is non-empty and consists of lowercase English letters. The last line contai... | Print a single integer β the required number.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 test case string ""aba"" is determined by the pairs of positions: (2, 0) and (5, 0); (5, 2) and (6, 1); (5, 2) and (3, 1); (4, 0) and (4, 2); (4, 4) and (4, 6); (3, 3) and (3, 5).Note that the string is not defined by the pair of positions (7, 1) and (5, 0), as the way between them doesn't always go down. | Input: 71 ab5 bacaba1 abacaba2 aca5 ba2 baaba | Output: 6 | Hard | 4 | 2,231 | 477 | 192 | 2 |
567 | B | 567B | B. Berland National Library | 1,300 | implementation | Berland National Library has recently been built in the capital of Berland. In addition, in the library you can take any of the collected works of Berland leaders, the library has a reading room.Today was the pilot launch of an automated reading room visitors' accounting system! The scanner of the system is installed a... | The first line contains a positive integer n (1 β€ n β€ 100) β the number of records in the system log. Next follow n events from the system journal in the order in which the were made. Each event was written on a single line and looks as ""+ ri"" or ""- ri"", where ri is an integer from 1 to 106, the registration number... | Print a single integer β the minimum possible capacity of the reading room. | In the first sample test, the system log will ensure that at some point in the reading room were visitors with registration numbers 1, 1200 and 12001. More people were not in the room at the same time based on the log. Therefore, the answer to the test is 3. | Input: 6+ 12001- 12001- 1- 1200+ 1+ 7 | Output: 3 | Easy | 1 | 1,426 | 633 | 75 | 5 |
2,051 | D | 2051D | D. Counting Pairs | 1,200 | binary search; sortings; two pointers | You are given a sequence \(a\), consisting of \(n\) integers, where the \(i\)-th element of the sequence is equal to \(a_i\). You are also given two integers \(x\) and \(y\) (\(x \le y\)).A pair of integers \((i, j)\) is considered interesting if the following conditions are met: \(1 \le i < j \le n\); if you simultane... | 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 three integers \(n, x, y\) (\(3 \le n \le 2 \cdot 10^5\), \(1 \le x \le y \le 2 \cdot 10^{14}\)); The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(... | For each test case, output one integer β the number of interesting pairs of integers for the given sequence \(a\). | In the first example, there are \(4\) interesting pairs of integers: \((1, 2)\); \((1, 4)\); \((2, 3)\); \((3, 4)\). | Input: 74 8 104 6 3 66 22 274 9 6 3 4 53 8 103 2 13 1 12 3 43 3 63 2 14 4 123 3 2 16 8 81 1 2 2 2 3 | Output: 4 7 0 0 1 5 6 | Easy | 3 | 570 | 453 | 114 | 20 |
1,320 | B | 1320B | B. Navigation System | 1,700 | dfs and similar; graphs; shortest paths | The map of Bertown can be represented as a set of \(n\) intersections, numbered from \(1\) to \(n\) and connected by \(m\) one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has t... | The first line contains two integers \(n\) and \(m\) (\(2 \le n \le m \le 2 \cdot 10^5\)) β the number of intersections and one-way roads in Bertown, respectively.Then \(m\) lines follow, each describing a road. Each line contains two integers \(u\) and \(v\) (\(1 \le u, v \le n\), \(u \ne v\)) denoting a road from int... | Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey. | Input: 6 9 1 5 5 4 1 2 2 3 3 4 4 1 2 6 6 4 4 2 4 1 2 3 4 | Output: 1 2 | Medium | 3 | 3,681 | 1,208 | 111 | 13 | |
717 | F | 717F | F. Heroes of Making Magic III | 2,600 | data structures | Iβm strolling on sunshine, yeah-ah! And doesnβt it feel good! Well, it certainly feels good for our Heroes of Making Magic, who are casually walking on a one-directional road, fighting imps. Imps are weak and feeble creatures and they are not good at much. However, Heroes enjoy fighting them. For fun, if nothing else. ... | The first line contains a single integer n (1 β€ n β€ 200 000), the length of the array a. The following line contains n integers a1, a2, ..., an (0 β€ ai β€ 5 000), the initial number of imps in each cell. The third line contains a single integer q (1 β€ q β€ 300 000), the number of queries. The remaining q lines contain on... | For each second type of query output 1 if it is possible to clear the segment, and 0 if it is not. | For the first query, one can easily check that it is indeed impossible to get from the first to the last cell while clearing everything. After we add 1 to the second position, we can clear the segment, for example by moving in the following way: . | Input: 32 2 232 0 21 1 1 12 0 2 | Output: 01 | Expert | 1 | 1,640 | 422 | 98 | 7 |
305 | D | 305D | D. Olya and Graph | 2,200 | combinatorics; math | Olya has got a directed non-weighted graph, consisting of n vertexes and m edges. We will consider that the graph vertexes are indexed from 1 to n in some manner. Then for any graph edge that goes from vertex v to vertex u the following inequation holds: v < u.Now Olya wonders, how many ways there are to add an arbitra... | The first line contains three space-separated integers n, m, k (2 β€ n β€ 106, 0 β€ m β€ 105, 1 β€ k β€ 106).The next m lines contain the description of the edges of the initial graph. The i-th line contains a pair of space-separated integers ui, vi (1 β€ ui < vi β€ n) β the numbers of vertexes that have a directed edge from u... | Print a single integer β the answer to the problem modulo 1000000007 (109 + 7). | In the first sample there are two ways: the first way is not to add anything, the second way is to add a single edge from vertex 2 to vertex 5. | Input: 7 8 21 22 33 43 64 54 75 66 7 | Output: 2 | Hard | 2 | 1,157 | 644 | 79 | 3 |
847 | B | 847B | B. Preparing for Merge Sort | 1,600 | binary search; data structures | Ivan has an array consisting of n different integers. He decided to reorder all elements in increasing order. Ivan loves merge sort so he decided to represent his array with one or several increasing sequences which he then plans to merge into one sorted array.Ivan represent his array with increasing sequences with hel... | The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of elements in Ivan's array.The second line contains a sequence consisting of distinct integers a1, a2, ..., an (1 β€ ai β€ 109) β Ivan's array. | Print representation of the given array in the form of one or more increasing sequences in accordance with the algorithm described above. Each sequence must be printed on a new line. | Input: 51 3 2 5 4 | Output: 1 3 5 2 4 | Medium | 2 | 1,083 | 214 | 182 | 8 | |
1,203 | F2 | 1203F2 | F2. Complete the Projects (hard version) | 2,300 | dp; greedy | The only difference between easy and hard versions is that you should complete all the projects in easy version but this is not necessary in hard version.Polycarp is a very famous freelancer. His current rating is \(r\) units.Some very rich customers asked him to complete some projects for their companies. To complete ... | The first line of the input contains two integers \(n\) and \(r\) (\(1 \le n \le 100, 1 \le r \le 30000\)) β the number of projects and the initial rating of Polycarp, respectively.The next \(n\) lines contain projects, one per line. The \(i\)-th project is represented as a pair of integers \(a_i\) and \(b_i\) (\(1 \le... | Print one integer β the size of the maximum possible subset (possibly, empty) of projects Polycarp can choose. | Input: 3 4 4 6 10 -2 8 -1 | Output: 3 | Expert | 2 | 1,138 | 470 | 110 | 12 | |
746 | E | 746E | E. Numbers Exchange | 1,900 | greedy; implementation; math | Eugeny has n cards, each of them has exactly one integer written on it. Eugeny wants to exchange some cards with Nikolay so that the number of even integers on his cards would equal the number of odd integers, and that all these numbers would be distinct. Nikolay has m cards, distinct numbers from 1 to m are written on... | The first line contains two integers n and m (2 β€ n β€ 2Β·105, 1 β€ m β€ 109) β the number of cards Eugeny has and the number of cards Nikolay has. It is guaranteed that n is even.The second line contains a sequence of n positive integers a1, a2, ..., an (1 β€ ai β€ 109) β the numbers on Eugeny's cards. | If there is no answer, print -1.Otherwise, in the first line print the minimum number of exchanges. In the second line print n integers β Eugeny's cards after all the exchanges with Nikolay. The order of cards should coincide with the card's order in the input data. If the i-th card wasn't exchanged then the i-th numbe... | Input: 6 25 6 7 9 4 5 | Output: 15 6 7 9 4 2 | Hard | 3 | 667 | 298 | 581 | 7 | |
1,451 | B | 1451B | B. Non-Substring Subsequence | 900 | dp; greedy; implementation; strings | Hr0d1y has \(q\) queries on a binary string \(s\) of length \(n\). A binary string is a string containing only characters '0' and '1'.A query is described by a pair of integers \(l_i\), \(r_i\) \((1 \leq l_i \lt r_i \leq n)\). For each query, he has to determine whether there exists a good subsequence in \(s\) that is ... | The first line of the input contains a single integer \(t\) (\(1\leq t \leq 100\)) β the number of test cases. The description of each test case is as follows.The first line contains two integers \(n\) (\(2 \leq n \leq 100\)) and \(q\) (\(1\leq q \leq 100\)) β the length of the string and the number of queries. The sec... | For each test case, output \(q\) lines. The \(i\)-th line of the output of each test case should contain ""YES"" if there exists a good subsequence equal to the substring \(s[l_i...r_i]\), and ""NO"" otherwise.You may print each letter in any case (upper or lower). | In the first test case, \(s[2\ldots 4] = \) ""010"". In this case \(s_1s_3s_5\) (""001000"") and \(s_2s_3s_6\) (""001000"") are good suitable subsequences, while \(s_2s_3s_4\) (""001000"") is not good. \(s[1\ldots 3] = \) ""001"". No suitable good subsequence exists. \(s[3\ldots 5] = \) ""100"". Here \(s_3s_5s_6\) (""0... | Input: 2 6 3 001000 2 4 1 3 3 5 4 2 1111 1 4 2 3 | Output: YES NO YES NO YES | Beginner | 4 | 950 | 466 | 265 | 14 |
1,487 | C | 1487C | C. Minimum Ties | 1,500 | brute force; constructive algorithms; dfs and similar; graphs; greedy; implementation; math | A big football championship will occur soon! \(n\) teams will compete in it, and each pair of teams will play exactly one game against each other.There are two possible outcomes of a game: the game may result in a tie, then both teams get \(1\) point; one team might win in a game, then the winning team gets \(3\) point... | The first line contains one integer \(t\) (\(1 \le t \le 100\)) β the number of test cases.Then the test cases follow. Each test case is described by one line containing one integer \(n\) (\(2 \le n \le 100\)) β the number of teams. | For each test case, print \(\frac{n(n - 1)}{2}\) integers describing the results of the games in the following order: the first integer should correspond to the match between team \(1\) and team \(2\), the second β between team \(1\) and team \(3\), then \(1\) and \(4\), ..., \(1\) and \(n\), \(2\) and \(3\), \(2\) and... | In the first test case of the example, both teams get \(1\) point since the game between them is a tie.In the second test case of the example, team \(1\) defeats team \(2\) (team \(1\) gets \(3\) points), team \(1\) loses to team \(3\) (team \(3\) gets \(3\) points), and team \(2\) wins against team \(3\) (team \(2\) g... | Input: 2 2 3 | Output: 0 1 -1 1 | Medium | 7 | 836 | 232 | 828 | 14 |
2,056 | D | 2056D | D. Unique Median | 2,200 | binary search; brute force; combinatorics; data structures; divide and conquer; dp | An array \(b\) of \(m\) integers is called good if, when it is sorted, \(b_{\left\lfloor \frac{m + 1}{2} \right\rfloor} = b_{\left\lceil \frac{m + 1}{2} \right\rceil}\). In other words, \(b\) is good if both of its medians are equal. In particular, \(\left\lfloor \frac{m + 1}{2} \right\rfloor = \left\lceil \frac{m + 1}... | 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 array.The second line of each test case contains \... | For each test case, output a single integer representing the number of good subarrays in \(a\). | In the first case, every subarray is good since all its elements are equal to \(1\).In the second case, an example of a good subarray is \(b = [10, 2, 3, 3]\). When it is sorted, \(b = [2, 3, 3, 10]\), so \(b_{\left\lfloor \frac{4 + 1}{2} \right\rfloor} = b_{\left\lceil \frac{4 + 1}{2} \right\rceil} = b_2 = b_3 = 3\). ... | Input: 341 1 1 151 10 2 3 3106 3 2 3 5 3 4 2 3 5 | Output: 10 11 42 | Hard | 6 | 762 | 507 | 95 | 20 |
1,508 | D | 1508D | D. Swap Pass | 3,000 | constructive algorithms; geometry; sortings | Based on a peculiar incident at basketball practice, Akari came up with the following competitive programming problem!You are given \(n\) points on the plane, no three of which are collinear. The \(i\)-th point initially has a label \(a_i\), in such a way that the labels \(a_1, a_2, \dots, a_n\) form a permutation of \... | The first line contains an integer \(n\) (\(3 \le n \le 2000\)) β the number of points.The \(i\)-th of the following \(n\) lines contains three integers \(x_i\), \(y_i\), \(a_i\) (\(-10^6 \le x_i, y_i \le 10^6\), \(1 \le a_i \le n\)), representing that the \(i\)-th point has coordinates \((x_i, y_i)\) and initially has... | If it is impossible to perform a valid sequence of operations, print \(-1\).Otherwise, print an integer \(k\) (\(0 \le k \le \frac{n(n - 1)}{2}\)) β the number of operations to perform, followed by \(k\) lines, each containing two integers \(i\) and \(j\) (\(1 \le i, j \le n\), \(i\neq j\)) β the indices of the points ... | The following animation showcases the first sample test case. The black numbers represent the indices of the points, while the boxed orange numbers represent their labels. In the second test case, all labels are already in their correct positions, so no operations are necessary. | Input: 5 -1 -2 2 3 0 5 1 3 4 4 -3 3 5 2 1 | Output: 5 1 2 5 3 4 5 1 5 1 3 | Master | 3 | 1,059 | 494 | 485 | 15 |
560 | B | 560B | B. Gerald is into Art | 1,200 | constructive algorithms; implementation | Gerald bought two very rare paintings at the Sotheby's auction and he now wants to hang them on the wall. For that he bought a special board to attach it to the wall and place the paintings on the board. The board has shape of an a1 Γ b1 rectangle, the paintings have shape of a a2 Γ b2 and a3 Γ b3 rectangles.Since the ... | The first line contains two space-separated numbers a1 and b1 β the sides of the board. Next two lines contain numbers a2, b2, a3 and b3 β the sides of the paintings. All numbers ai, bi in the input are integers and fit into the range from 1 to 1000. | If the paintings can be placed on the wall, print ""YES"" (without the quotes), and if they cannot, print ""NO"" (without the quotes). | That's how we can place the pictures in the first test:And that's how we can do it in the third one. | Input: 3 21 32 1 | Output: YES | Easy | 2 | 755 | 250 | 134 | 5 |
1,915 | A | 1915A | A. Odd One Out | 800 | bitmasks; implementation | You are given three digits \(a\), \(b\), \(c\). Two of them are equal, but the third one is different from the other two. Find the value that occurs exactly once. | The first line contains a single integer \(t\) (\(1 \leq t \leq 270\)) β the number of test cases.The only line of each test case contains three digits \(a\), \(b\), \(c\) (\(0 \leq a\), \(b\), \(c \leq 9\)). Two of the digits are equal, but the third one is different from the other two. | For each test case, output the value that occurs exactly once. | Input: 101 2 24 3 45 5 67 8 89 0 93 6 32 8 25 7 77 7 55 7 5 | Output: 1 3 6 7 0 6 8 5 5 7 | Beginner | 2 | 162 | 288 | 62 | 19 | |
1,787 | H | 1787H | H. Codeforces Scoreboard | 3,300 | binary search; data structures; dp; geometry | You are participating in a Codeforces Round with \(n\) problems. You spend exactly one minute to solve each problem, the time it takes to submit a problem can be ignored. You can only solve at most one problem at any time. The contest starts at time \(0\), so you can make your first submission at any time \(t \ge 1\) m... | Each test contains multiple test cases. 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 one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of problems.The \(n\) lines follow, the \(i\)-th of them contains three integers \(k... | For each test case, print a line containing a single integer β the maximum score you can get. | In the second test case, the points for all problems at each minute are listed below. Time\(1\)\(2\)\(3\)\(4\)\(5\)\(6\)Problem \(1\)\(7\)\(6\)\(5\)\(\color{red}{4}\)\(3\)\(2\)Problem \(2\)\(\color{red}{20}\)\(11\)\(4\)\(4\)\(4\)\(4\)Problem \(3\)\(12\)\(10\)\(\color{red}{8}\)\(6\)\(4\)\(3\)Problem \(4\)\(9\)\(5\)\(1\)... | Input: 4410000 1000000000 200610000 1000000000 99992 999991010 10101000000000 1000000000 99999999961 8 19 29 42 14 34 13 12 19 510 12 584 10 14 19 81 14 34 15 62 9 61 11 102 19 124 19 14105 12 75 39 122 39 113 23 155 30 113 17 135 29 143 17 113 36 183 9 8 | Output: 3999961003 53 78 180 | Master | 4 | 736 | 581 | 93 | 17 |
2,096 | D | 2096D | D. Wonderful Lightbulbs | 2,000 | combinatorics; constructive algorithms; math | You are the proud owner of an infinitely large grid of lightbulbs, represented by a Cartesian coordinate system. Initially, all of the lightbulbs are turned off, except for one lightbulb, where you buried your proudest treasure.In order to hide your treasure's position, you perform the following operation an arbitrary ... | 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 2 \cdot 10^5\)) β the number of lightbulbs that are on.The \(i\)-th of the nex... | For each test case, output two integers \(s\) and \(t\) (\(-10^9 \le s, t \le 10^9\)) β one possible position of the buried treasure. If there are multiple solutions, print any of them.For this problem, hacks are disabled. | For the first test case, one possible scenario is that you hid your treasure at position \((2, 3)\). Then, you did not perform any operations.In the end, only the lightbulb at \((2, 3)\) is turned on.For the second test case, one possible scenario is that you hid your treasure at position \((-2, -2)\). Then, you perfor... | Input: 412 33-2 -1-1 -2-1 -377 266 276 287 278 268 277 281170 969 869 073 570 -170 571 770 473 471 372 3 | Output: 2 3 -2 -2 7 27 72 7 | Hard | 3 | 925 | 947 | 222 | 20 |
68 | A | 68A | A. Irrational problem | 1,100 | implementation; number theory | Little Petya was given this problem for homework:You are given function (here represents the operation of taking the remainder). His task is to count the number of integers x in range [a;b] with property f(x) = x.It is a pity that Petya forgot the order in which the remainders should be taken and wrote down only 4 numb... | First line of the input will contain 6 integers, separated by spaces: p1, p2, p3, p4, a, b (1 β€ p1, p2, p3, p4 β€ 1000, 0 β€ a β€ b β€ 31415). It is guaranteed that numbers p1, p2, p3, p4 will be pairwise distinct. | Output the number of integers in the given range that have the given property. | Input: 2 7 1 8 2 8 | Output: 0 | Easy | 2 | 1,092 | 210 | 78 | 0 | |
1,744 | D | 1744D | D. Divisibility by 2^n | 1,200 | greedy; math; sortings | You are given an array of positive integers \(a_1, a_2, \ldots, a_n\).Make the product of all the numbers in the array (that is, \(a_1 \cdot a_2 \cdot \ldots \cdot a_n\)) divisible by \(2^n\).You can perform the following operation as many times as you like: select an arbitrary index \(i\) (\(1 \leq i \leq n\)) and rep... | The first line of the input contains a single integer \(t\) \((1 \leq t \leq 10^4\)) β the number test cases.Then the descriptions of the input data sets follow.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β the length of array \(a\).The second line of each test case... | For each test case, print the least number of operations to make the product of all numbers in the array divisible by \(2^n\). If the answer does not exist, print -1. | In the first test case, the product of all elements is initially \(2\), so no operations needed.In the second test case, the product of elements initially equals \(6\). We can apply the operation for \(i = 2\), and then \(a_2\) becomes \(2\cdot2=4\), and the product of numbers becomes \(3\cdot4=12\), and this product o... | Input: 61223 2310 6 11413 17 1 151 1 12 1 1620 7 14 18 3 5 | Output: 0 1 1 -1 2 1 | Easy | 3 | 684 | 516 | 166 | 17 |
1,614 | B | 1614B | B. Divan and a New Project | 1,000 | constructive algorithms; sortings | The company ""Divan's Sofas"" is planning to build \(n + 1\) different buildings on a coordinate line so that: the coordinate of each building is an integer number; no two buildings stand at the same point. Let \(x_i\) be the coordinate of the \(i\)-th building. To get from the building \(i\) to the building \(j\), Div... | Each test contains several test cases. The first line contains one integer number \(t\) (\(1 \le t \le 10^3\)) β the number of test cases.The first line of each case contains an integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of buildings that ""Divan's Sofas"" is going to build, apart from the headquarters.T... | For each test case, on the first line print the number \(T\) β the minimum time Divan will spend walking. On the second line print the sequence \(x_0, x_1, \ldots, x_n\) of \(n + 1\) integers, where \(x_i\) (\(-10^6 \le x_i \le 10^6\)) is the selected coordinate of the \(i\)-th building. It can be shown that an optimal... | Let's look at the first example.Divan will visit the first building \(a_1 = 1\) times, the second \(a_2 = 2\) times and the third \(a_3 = 3\) times. Then one of the optimal solution will be as follows: the headquarters is located in \(x_0 = 2\); \(x_1 = 4\): Divan will spend \(2 \cdot |x_0-x_1| \cdot a_1 = 2 \cdot |2-4... | Input: 4 3 1 2 3 5 3 8 10 6 1 5 1 1 1 1 1 1 0 | Output: 14 2 4 1 3 78 1 -1 0 2 3 4 18 3 6 1 5 2 4 0 1 2 | Beginner | 2 | 896 | 564 | 424 | 16 |
1,543 | A | 1543A | A. Exciting Bets | 900 | greedy; math; number theory | Welcome to Rockport City!It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet \(a\) dollars and Ronnie has bet \(b\) dollars. But the fans seem to be disappointed. The excitement of the fans is given by \(gcd(a,b)\), where \(gcd(x, y)\) denotes the greatest common d... | The first line of input contains a single integer \(t\) (\(1\leq t\leq 5\cdot 10^3\)) β the number of test cases.The first and the only line of each test case contains two integers \(a\) and \(b\) (\(0\leq a, b\leq 10^{18}\)). | For each test case, print a single line containing two integers. If the fans can get infinite excitement, print 0 0.Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement. | For the first test case, you can apply the first operation \(1\) time to get \(a=9\) and \(b=6\). It can be shown that \(3\) is the maximum excitement possible.For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to \(1\). Since the initial excitement is also ... | Input: 4 8 5 1 2 4 4 3 9 | Output: 3 1 1 0 0 0 6 3 | Beginner | 3 | 867 | 226 | 289 | 15 |
391 | B | 391B | B. Word Folding | 0 | brute force | You will receive 5 points for solving this problem.Manao has invented a new operation on strings that is called folding. Each fold happens between a pair of consecutive letters and places the second part of the string above first part, running in the opposite direction and aligned to the position of the fold. Using thi... | The input will consist of one line containing a single string of n characters with 1 β€ n β€ 1000 and no spaces. All characters of the string will be uppercase letters.This problem doesn't have subproblems. You will get 5 points for the correct submission. | Print a single integer β the size of the largest pile composed of identical characters that can be seen in a valid result of folding operations on the given string. | Consider the first example. Manao can create a pile of three 'A's using the folding ""AB|RACAD|ABRA"", which results in the following structure: ABRADACAR ABIn the second example, Manao can create a pile of three 'B's using the following folding: ""AB|BB|CBDB"". CBDBBBABAnother way for Manao to create a pile of three '... | Input: ABRACADABRA | Output: 3 | Beginner | 1 | 1,655 | 254 | 164 | 3 |
940 | F | 940F | F. Machine Learning | 2,600 | brute force; data structures | You come home and fell some unpleasant smell. Where is it coming from?You are given an array a. You have to answer the following queries: You are given two integers l and r. Let ci be the number of occurrences of i in al: r, where al: r is the subarray of a from l-th element to r-th inclusive. Find the Mex of {c0, c1, ... | The first line of input contains two integers n and q (1 β€ n, q β€ 100 000) β the length of the array and the number of queries respectively.The second line of input contains n integers β a1, a2, ..., an (1 β€ ai β€ 109).Each of the next q lines describes a single query.The first type of query is described by three intege... | For each query of the first type output a single integer β the Mex of {c0, c1, ..., c109}. | The subarray of the first query consists of the single element β 1. The subarray of the second query consists of four 2s, one 3 and two 1s.The subarray of the fourth query consists of three 1s, three 2s and one 3. | Input: 10 41 2 3 1 1 2 2 2 9 91 1 11 2 82 7 11 2 8 | Output: 232 | Expert | 2 | 608 | 564 | 90 | 9 |
1,900 | A | 1900A | A. Cover in Water | 800 | constructive algorithms; greedy; implementation; strings | Filip has a row of cells, some of which are blocked, and some are empty. He wants all empty cells to have water in them. He has two actions at his disposal: \(1\) β place water in an empty cell. \(2\) β remove water from a cell and place it in any other empty cell. If at some moment cell \(i\) (\(2 \le i \le n-1\)) is ... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 100\)) β the number of cells. The next line contains a string \(s\) of length \(... | For each test case, output a single number β the minimal amount of actions \(1\) needed to fill all empty cells with water. | Test Case 1In the first test case, Filip can put water in cells \(1\) and \(3\). As cell \(2\) is between \(2\) cells with water, it gets filled with water too.Test Case 2In the second case, he can put water sources in cells \(3\) and \(5\). That results in cell \(4\) getting filled with water. Then he will remove wate... | Input: 53...7##....#7..#.#..4####10#...#..#.# | Output: 2 2 5 0 2 | Beginner | 4 | 664 | 424 | 123 | 19 |
238 | E | 238E | E. Meeting Her | 2,600 | dp; graphs; shortest paths | Urpal lives in a big city. He has planned to meet his lover tonight. The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation t... | The first line of the input contains four integers n, m, a, b (2 β€ n β€ 100; 0 β€ m β€ nΒ·(n - 1); 1 β€ a, b β€ n; a β b). The next m lines contain two integers each ui and vi (1 β€ ui, vi β€ n; ui β vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct. The next line contains ... | In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1. | Input: 7 8 1 71 21 32 43 44 64 56 75 732 71 45 7 | Output: 2 | Expert | 3 | 1,294 | 611 | 184 | 2 | |
982 | E | 982E | E. Billiard | 2,600 | geometry; number theory | Consider a billiard table of rectangular size \(n \times m\) with four pockets. Let's introduce a coordinate system with the origin at the lower left corner (see the picture). There is one ball at the point \((x, y)\) currently. Max comes to the table and strikes the ball. The ball starts moving along a line that is pa... | The only line contains \(6\) integers \(n\), \(m\), \(x\), \(y\), \(v_x\), \(v_y\) (\(1 \leq n, m \leq 10^9\), \(0 \leq x \leq n\); \(0 \leq y \leq m\); \(-1 \leq v_x, v_y \leq 1\); \((v_x, v_y) \neq (0, 0)\)) β the width of the table, the length of the table, the \(x\)-coordinate of the initial position of the ball, t... | Print the coordinates of the pocket the ball will fall into, or \(-1\) if the ball will move indefinitely. | The first sample: The second sample: In the third sample the ball will never change its \(y\) coordinate, so the ball will never fall into a pocket. | Input: 4 3 2 2 -1 1 | Output: 0 0 | Expert | 2 | 1,005 | 538 | 106 | 9 |
1,991 | I | 1991I | I. Grid Game | 3,500 | constructive algorithms; games; graph matchings; greedy; interactive | This is an interactive problem.You are given a grid with \(n\) rows and \(m\) columns. You need to fill each cell with a unique integer from \(1\) to \(n \cdot m\).After filling the grid, you will play a game on this grid against the interactor. Players take turns selecting one of the previously unselected cells from t... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. The description of test cases follows.The only line of each test case contains two integers \(n\) and \(m\) (\(4 \le n, m \le 10\)) β the number of rows and columns in the grid. | Note that this is an example game and does not necessarily represent the optimal strategy for both players.First, we fill a \(4 \times 4\) grid with unique integers from \(1\) to \(16\) in the following way: \(2\)\(3\)\(4\)\(10\)\(12\)\(6\)\(11\)\(15\)\(5\)\(13\)\(16\)\(8\)\(9\)\(7\)\(1\)\(14\) Next, the game begins. T... | Input: 1 4 4 3 4 4 4 4 2 4 1 1 4 1 2 2 2 2 1 | Output: 2 3 4 10 12 6 11 15 5 13 16 8 9 7 1 14 2 4 4 3 3 3 3 1 1 3 1 1 2 3 3 2 | Master | 5 | 803 | 314 | 0 | 19 | |
1,987 | G2 | 1987G2 | G2. Spinning Round (Hard Version) | 3,500 | divide and conquer; dp; trees | This is the hard version of the problem. The only difference between the two versions are the allowed characters in \(s\). You can make hacks only if both versions of the problem are solved.You are given a permutation \(p\) of length \(n\). You are also given a string \(s\) of length \(n\), where each character is eith... | Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 2 \cdot 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 4 \cdot 10^5\)) β the length of the permuta... | For each test case, output the maximum possible diameter over all connected graphs that you form, or \(-1\) if it is not possible to form any connected graphs. | In the first test case, there are two connected graphs (the labels are indices): The graph on the left has a diameter of \(2\), while the graph on the right has a diameter of \(3\), so the answer is \(3\).In the second test case, there are no connected graphs, so the answer is \(-1\). | Input: 852 1 4 3 5R?RL?21 2LR33 1 2L?R75 3 1 6 4 2 7?R?R?R?55 2 1 3 4?????66 2 3 4 5 1?LLRLL81 7 5 6 2 8 4 3?R??????126 10 7 1 8 5 12 2 11 3 4 9???????????? | Output: 3 -1 -1 4 4 3 5 8 | Master | 3 | 1,364 | 739 | 159 | 19 |
1,730 | A | 1730A | A. Planets | 800 | data structures; greedy; sortings | One day, Vogons wanted to build a new hyperspace highway through a distant system with \(n\) planets. The \(i\)-th planet is on the orbit \(a_i\), there could be multiple planets on the same orbit. It's a pity that all the planets are on the way and need to be destructed.Vogons have two machines to do that. The first m... | The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. Then the test cases follow.Each test case consists of two lines.The first line contains two integers \(n\) and \(c\) (\(1 \le n, c \le 100\)) β the number of planets and the cost of the second machine usage.The second line ... | For each test case print a single integer β the minimum cost of destroying all planets. | In the first test case, the cost of using both machines is the same, so you can always use the second one and destroy all planets in orbit \(1\), all planets in orbit \(2\), all planets in orbit \(4\), all planets in orbit \(5\).In the second test case, it is advantageous to use the second machine for \(2\) Triganic Pu... | Input: 410 12 1 4 5 2 4 5 5 1 25 23 2 1 2 22 21 12 21 2 | Output: 4 4 2 2 | Beginner | 3 | 735 | 444 | 87 | 17 |
1,064 | A | 1064A | A. Make a triangle! | 800 | brute force; geometry; math | Masha has three sticks of length \(a\), \(b\) and \(c\) centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks.What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to a... | The only line contains tree integers \(a\), \(b\) and \(c\) (\(1 \leq a, b, c \leq 100\)) β the lengths of sticks Masha possesses. | Print a single integer β the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. | In the first example, Masha can make a triangle from the sticks without increasing the length of any of them.In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length \(2\) centimeter stick by one and after that form... | Input: 3 4 5 | Output: 0 | Beginner | 3 | 485 | 130 | 155 | 10 |
1,611 | A | 1611A | A. Make Even | 800 | constructive algorithms; math | Polycarp has an integer \(n\) that doesn't contain the digit 0. He can do the following operation with his number several (possibly zero) times: Reverse the prefix of length \(l\) (in other words, \(l\) leftmost digits) of \(n\). So, the leftmost digit is swapped with the \(l\)-th digit from the left, the second digit ... | The first line contains the number \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Each of the following \(t\) lines contains one integer \(n\) (\(1 \le n < 10^9\)). It is guaranteed that the given number doesn't contain the digit 0. | Print \(t\) lines. On each line print one integer β the answer to the corresponding test case. If it is impossible to make an even number, print -1. | In the first test case, \(n=3876\), which is already an even number. Polycarp doesn't need to do anything, so the answer is \(0\).In the second test case, \(n=387\). Polycarp needs to do \(2\) operations: Select \(l=2\) and reverse the prefix \(\underline{38}7\). The number \(n\) becomes \(837\). This number is odd. Se... | Input: 4 3876 387 4489 3 | Output: 0 2 1 -1 | Beginner | 2 | 1,021 | 241 | 148 | 16 |
1,707 | A | 1707A | A. Doremy's IQ | 1,600 | binary search; constructive algorithms; greedy; implementation | Doremy is asked to test \(n\) contests. Contest \(i\) can only be tested on day \(i\). The difficulty of contest \(i\) is \(a_i\). Initially, Doremy's IQ is \(q\). On day \(i\) Doremy will choose whether to test contest \(i\) or not. She can only test a contest if her current IQ is strictly greater than \(0\).If Doremy... | 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. The description of the test cases follows.The first line contains two integers \(n\) and \(q\) (\(1 \le n \le 10^5\), \(1 \le q \le 10^9\)) β the number of contests and Doremy's IQ ... | For each test case, you need to output a binary string \(s\), where \(s_i=1\) if Doremy should choose to test contest \(i\), and \(s_i=0\) otherwise. The number of ones in the string should be maximum possible, and she should never test a contest when her IQ is zero or less.If there are multiple solutions, you may outp... | In the first test case, Doremy tests the only contest. Her IQ doesn't decrease.In the second test case, Doremy tests both contests. Her IQ decreases by \(1\) after testing contest \(2\).In the third test case, Doremy tests contest \(1\) and \(2\). Her IQ decreases to \(0\) after testing contest \(2\), so she can't test... | Input: 51 112 11 23 11 2 14 21 4 3 15 25 1 2 4 3 | Output: 1 11 110 1110 01111 | Medium | 4 | 635 | 542 | 327 | 17 |
1,843 | D | 1843D | D. Apple Tree | 1,200 | combinatorics; dfs and similar; dp; math; trees | Timofey has an apple tree growing in his garden; it is a rooted tree of \(n\) vertices with the root in vertex \(1\) (the vertices are numbered from \(1\) to \(n\)). A tree is a connected graph without loops and multiple edges.This tree is very unusual β it grows with its root upwards. However, it's quite normal for pr... | The first line contains integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains integer \(n\) (\(2 \leq n \leq 2 \cdot 10^5\)) β the number of vertices in the tree.Then there are \(n - 1\) lines describing the tree. In line \(i\) there are two integers \(u_i\) and \(v... | For each Timofey's assumption output the number of ordered pairs of vertices from which apples can fall from the tree if the assumption is true on a separate line. | In the first example: For the first assumption, there are two possible pairs of vertices from which apples can fall from the tree: \((4, 4), (5, 4)\). For the second assumption there are also two pairs: \((5, 4), (5, 5)\). For the third assumption there is only one pair: \((4, 4)\). For the fourth assumption, there are... | Input: 251 23 45 33 243 45 14 41 331 21 331 12 33 1 | Output: 2 2 1 4 4 1 2 | Easy | 5 | 1,320 | 838 | 163 | 18 |
1,002 | D2 | 1002D2 | D2. Oracle for f(x) = b * x + (1 - b) * (1 - x) mod 2 | 1,300 | *special | Implement a quantum oracle on N qubits which implements the following function: Here (a vector of N integers, each of which can be 0 or 1), and is a vector of N 1s.For an explanation on how this type of quantum oracles works, see Introduction to quantum oracles.You have to implement an operation which takes the followi... | Easy | 1 | 847 | 0 | 0 | 10 | ||||
1,732 | C2 | 1732C2 | C2. Sheikh (Hard Version) | 2,100 | binary search; bitmasks; brute force; greedy; implementation; two pointers | This is the hard version of the problem. The only difference is that in this version \(q = n\).You are given an array of integers \(a_1, a_2, \ldots, a_n\).The cost of a subsegment of the array \([l, r]\), \(1 \leq l \leq r \leq n\), is the value \(f(l, r) = \operatorname{sum}(l, r) - \operatorname{xor}(l, r)\), where ... | Each test consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. The description of test cases follows.The first line of each test case contains two integers \(n\) and \(q\) (\(1 \leq n \leq 10^5\), \(q = n\)) β the length of the array and the numb... | For each test case print \(q\) pairs of numbers \(L_i \leq l \leq r \leq R_i\) such that the value \(f(l, r)\) is maximum and among such the length \(r - l + 1\) is minimum. If there are several correct answers, print any of them. | In all test cases, the first query is considered.In the first test case, \(f(1, 1) = 0 - 0 = 0\).In the second test case, \(f(1, 1) = 5 - 5 = 0\), \(f(2, 2) = 10 - 10 = 0\). Note that \(f(1, 2) = (10 + 5) - (10 \oplus 5) = 0\), but we need to find a subsegment with the minimum length among the maximum values of \(f(l, ... | Input: 61 101 12 25 101 22 23 30 2 41 31 22 34 40 12 8 31 41 32 42 35 521 32 32 32 101 51 41 32 53 57 70 1 0 1 0 1 01 73 62 51 44 72 62 7 | Output: 1 1 1 1 2 2 1 1 1 1 2 2 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 3 4 2 4 4 6 2 4 2 4 4 6 2 4 2 4 | Hard | 6 | 875 | 784 | 230 | 17 |
385 | C | 385C | C. Bear and Prime Numbers | 1,700 | binary search; brute force; data structures; dp; implementation; math; number theory | Recently, the bear started studying data structures and faced the following problem.You are given a sequence of integers x1, x2, ..., xn of length n and m queries, each of them is characterized by two integers li, ri. Let's introduce f(p) to represent the number of such indexes k, that xk is divisible by p. The answer ... | The first line contains integer n (1 β€ n β€ 106). The second line contains n integers x1, x2, ..., xn (2 β€ xi β€ 107). The numbers are not necessarily distinct.The third line contains integer m (1 β€ m β€ 50000). Each of the following m lines contains a pair of space-separated integers, li and ri (2 β€ li β€ ri β€ 2Β·109) β th... | Print m integers β the answers to the queries on the order the queries appear in the input. | Consider the first sample. Overall, the first sample has 3 queries. The first query l = 2, r = 11 comes. You need to count f(2) + f(3) + f(5) + f(7) + f(11) = 2 + 1 + 4 + 2 + 0 = 9. The second query comes l = 3, r = 12. You need to count f(3) + f(5) + f(7) + f(11) = 1 + 4 + 2 + 0 = 7. The third query comes l = 4, r = 4... | Input: 65 5 7 10 14 1532 113 124 4 | Output: 970 | Medium | 7 | 497 | 366 | 91 | 3 |
922 | A | 922A | A. Cloning Toys | 1,300 | implementation | Imp likes his plush toy a lot. Recently, he found a machine that can clone plush toys. Imp knows that if he applies the machine to an original toy, he additionally gets one more original toy and one copy, and if he applies the machine to a copied toy, he gets two additional copies.Initially, Imp has only one original t... | The only line contains two integers x and y (0 β€ x, y β€ 109) β the number of copies and the number of original toys Imp wants to get (including the initial one). | Print ""Yes"", if the desired configuration is possible, and ""No"" otherwise.You can print each letter in arbitrary case (upper or lower). | In the first example, Imp has to apply the machine twice to original toys and then twice to copies. | Input: 6 3 | Output: Yes | Easy | 1 | 531 | 161 | 139 | 9 |
1,095 | D | 1095D | D. Circular Dance | 1,600 | implementation | There are \(n\) kids, numbered from \(1\) to \(n\), dancing in a circle around the Christmas tree. Let's enumerate them in a clockwise direction as \(p_1\), \(p_2\), ..., \(p_n\) (all these numbers are from \(1\) to \(n\) and are distinct, so \(p\) is a permutation). Let the next kid for a kid \(p_i\) be kid \(p_{i + 1... | The first line of the input contains one integer \(n\) (\(3 \le n \le 2 \cdot 10^5\)) β the number of the kids.The next \(n\) lines contain \(2\) integers each. The \(i\)-th line contains two integers \(a_{i, 1}\) and \(a_{i, 2}\) (\(1 \le a_{i, 1}, a_{i, 2} \le n, a_{i, 1} \ne a_{i, 2}\)) β the kids the \(i\)-th kid r... | Print \(n\) integers \(p_1\), \(p_2\), ..., \(p_n\) β permutation of integers from \(1\) to \(n\), which corresponds to the order of kids in the circle. If there are several answers, you may print any (for example, it doesn't matter which kid is the first in the circle). It is guaranteed that at least one solution exis... | Input: 5 3 5 1 4 2 4 1 5 2 3 | Output: 3 2 4 1 5 | Medium | 1 | 1,209 | 356 | 323 | 10 | |
1,714 | F | 1714F | F. Build a Tree and That Is It | 1,900 | constructive algorithms; implementation; trees | A tree is a connected undirected graph without cycles. Note that in this problem, we are talking about not rooted trees.You are given four positive integers \(n, d_{12}, d_{23}\) and \(d_{31}\). Construct a tree such that: it contains \(n\) vertices numbered from \(1\) to \(n\), the distance (length of the shortest pat... | The first line of the input contains an integer \(t\) (\(1 \le t \le 10^4\)) βthe number of test cases in the test.This is followed by \(t\) test cases, each written on a separate line.Each test case consists of four positive integers \(n, d_{12}, d_{23}\) and \(d_{31}\) (\(3 \le n \le 2\cdot10^5; 1 \le d_{12}, d_{23},... | For each test case, print YES if the suitable tree exists, and NO otherwise. If the answer is positive, print another \(n-1\) line each containing a description of an edge of the tree β a pair of positive integers \(x_i, y_i\), which means that the \(i\)th edge connects vertices \(x_i\) and \(y_i\). The edges and verti... | Input: 95 1 2 15 2 2 25 2 2 35 2 2 45 3 2 34 2 1 14 3 1 14 1 2 37 1 4 1 | Output: YES 1 2 4 1 3 1 2 5 YES 4 3 2 5 1 5 5 3 NO YES 2 4 4 1 2 5 5 3 YES 5 4 4 1 2 5 3 5 YES 2 3 3 4 1 3 NO YES 4 3 1 2 2 4 NO | Hard | 3 | 589 | 435 | 422 | 17 | |
825 | D | 825D | D. Suitable Replacement | 1,500 | binary search; greedy; implementation | You are given two strings s and t consisting of small Latin letters, string s can also contain '?' characters. Suitability of string s is calculated by following metric:Any two letters can be swapped positions, these operations can be performed arbitrary number of times over any pair of positions. Among all resulting s... | The first line contains string s (1 β€ |s| β€ 106).The second line contains string t (1 β€ |t| β€ 106). | Print string s with '?' replaced with small Latin letters in such a way that suitability of that string is maximal.If there are multiple strings with maximal suitability then print any of them. | In the first example string ""baab"" can be transformed to ""abab"" with swaps, this one has suitability of 2. That means that string ""baab"" also has suitability of 2.In the second example maximal suitability you can achieve is 1 and there are several dozens of such strings, ""azbz"" is just one of them.In the third ... | Input: ?aa?ab | Output: baab | Medium | 3 | 581 | 99 | 193 | 8 |
1,930 | B | 1930B | B. Permutation Printing | 1,000 | brute force; constructive algorithms; math | You are given a positive integer \(n\).Find a permutation\(^\dagger\) \(p\) of length \(n\) such that there do not exist two distinct indices \(i\) and \(j\) (\(1 \leq i, j < n\); \(i \neq j\)) such that \(p_i\) divides \(p_j\) and \(p_{i+1}\) divides \(p_{j+1}\).Refer to the Notes section for some examples.Under the c... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^3\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(3 \leq n \leq 10^5\)) β the length of the permutation \(p\).It is guar... | For each test case, output \(p_1, p_2, \ldots, p_n\).If there are multiple solutions, you may output any one of them. | In the first test case, \(p=[4,1,2,3]\) is a valid permutation. However, the permutation \(p=[1,2,3,4]\) is not a valid permutation as we can choose \(i=1\) and \(j=3\). Then \(p_1=1\) divides \(p_3=3\) and \(p_2=2\) divides \(p_4=4\). Note that the permutation \(p=[3, 4, 2, 1]\) is also not a valid permutation as we c... | Input: 243 | Output: 4 1 2 3 1 2 3 | Beginner | 3 | 733 | 394 | 117 | 19 |
192 | A | 192A | A. Funky Numbers | 1,300 | binary search; brute force; implementation | As you very well know, this year's funkiest numbers are so called triangular numbers (that is, integers that are representable as , where k is some positive integer), and the coolest numbers are those that are representable as a sum of two triangular numbers.A well-known hipster Andrew adores everything funky and cool ... | The first input line contains an integer n (1 β€ n β€ 109). | Print ""YES"" (without the quotes), if n can be represented as a sum of two triangular numbers, otherwise print ""NO"" (without the quotes). | In the first sample number .In the second sample number 512 can not be represented as a sum of two triangular numbers. | Input: 256 | Output: YES | Easy | 3 | 497 | 57 | 140 | 1 |
297 | C | 297C | C. Splitting the Uniqueness | 2,400 | constructive algorithms | Polar bears like unique arrays β that is, arrays without repeated elements. You have got a unique array s with length n containing non-negative integers. Since you are good friends with Alice and Bob, you decide to split the array in two. Precisely, you need to construct two arrays a and b that are also of length n, wi... | The first line of the input contains integer n (1 β€ n β€ 105).The second line contains n distinct integers s1, s2, ... sn (0 β€ si β€ 109). | If it is possible to make Alice and Bob happy (if you can split the given array), print ""YES"" (without quotes) in the first line. In the second line, print the array a. In the third line, print the array b. There may be more than one solution. Any of them will be accepted.If it is impossible to split s into almost un... | In the sample, we can remove the first two entries from a and the second entry from b to make them both unique. | Input: 612 5 8 3 11 9 | Output: YES6 2 6 0 2 46 3 2 3 9 5 | Expert | 1 | 1,090 | 136 | 389 | 2 |
1,760 | B | 1760B | B. Atilla's Favorite Problem | 800 | greedy; implementation; strings | In order to write a string, Atilla needs to first learn all letters that are contained in the string.Atilla needs to write a message which can be represented as a string \(s\). He asks you what is the minimum alphabet size required so that one can write this message.The alphabet of size \(x\) (\(1 \leq x \leq 26\)) con... | The first line contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 100\)) β the length of the string.The second line of each test case contains a string \(s\) of length \(n\), consisting of lowercase Latin ... | For each test case, output a single integer β the minimum alphabet size required to so that Atilla can write his message \(s\). | For the first test case, Atilla needs to know only the character \(\texttt{a}\), so the alphabet of size \(1\) which only contains \(\texttt{a}\) is enough.For the second test case, Atilla needs to know the characters \(\texttt{d}\), \(\texttt{o}\), \(\texttt{w}\), \(\texttt{n}\). The smallest alphabet size that contai... | Input: 51a4down10codeforces3bcf5zzzzz | Output: 1 23 19 6 26 | Beginner | 3 | 495 | 328 | 127 | 17 |
1,685 | B | 1685B | B. Linguistics | 2,000 | greedy; implementation; sortings; strings | Alina has discovered a weird language, which contains only \(4\) words: \(\texttt{A}\), \(\texttt{B}\), \(\texttt{AB}\), \(\texttt{BA}\). It also turned out that there are no spaces in this language: a sentence is written by just concatenating its words into a single string.Alina has found one such sentence \(s\) and s... | The first line of the input contains a single integer \(t\) (\(1 \le t \le 10^5\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains four integers \(a\), \(b\), \(c\), \(d\) (\(0\le a,b,c,d\le 2\cdot 10^5\)) β the number of times that words \(\texttt{A}\), \... | For each test case output \(\texttt{YES}\) if it is possible that the sentence \(s\) consists of precisely \(a\) words \(\texttt{A}\), \(b\) words \(\texttt{B}\), \(c\) words \(\texttt{AB}\), and \(d\) words \(\texttt{BA}\), and \(\texttt{NO}\) otherwise. You can output each letter in any case. | In the first test case, the sentence \(s\) is \(\texttt{B}\). Clearly, it can't consist of a single word \(\texttt{A}\), so the answer is \(\texttt{NO}\).In the second test case, the sentence \(s\) is \(\texttt{AB}\), and it's possible that it consists of a single word \(\texttt{AB}\), so the answer is \(\texttt{YES}\)... | Input: 8 1 0 0 0 B 0 0 1 0 AB 1 1 0 1 ABAB 1 0 1 1 ABAAB 1 1 2 2 BAABBABBAA 1 1 2 3 ABABABBAABAB 2 3 5 4 AABAABBABAAABABBABBBABB 1 3 3 10 BBABABABABBBABABABABABABAABABA | Output: NO YES YES YES YES YES NO YES | Hard | 4 | 771 | 866 | 295 | 16 |
350 | E | 350E | E. Wrong Floyd | 2,200 | brute force; constructive algorithms; dfs and similar; graphs | Valera conducts experiments with algorithms that search for shortest paths. He has recently studied the Floyd's algorithm, so it's time to work with it.Valera's already written the code that counts the shortest distance between any pair of vertexes in a non-directed connected graph from n vertexes and m edges, containi... | The first line of the input contains three integers n, m, k (3 β€ n β€ 300, 2 β€ k β€ n , ) β the number of vertexes, the number of edges and the number of marked vertexes. The second line of the input contains k space-separated integers a1, a2, ... ak (1 β€ ai β€ n) β the numbers of the marked vertexes. It is guaranteed tha... | If the graph doesn't exist, print -1 on a single line. Otherwise, print m lines, each containing two integers u, v β the description of the edges of the graph Valera's been looking for. | Input: 3 2 21 2 | Output: 1 32 3 | Hard | 4 | 1,370 | 350 | 185 | 3 | |
1,249 | A | 1249A | A. Yet Another Dividing into Teams | 800 | math | You are a coach of a group consisting of \(n\) students. The \(i\)-th student has programming skill \(a_i\). All students have distinct programming skills. You want to divide them into teams in such a way that: No two students \(i\) and \(j\) such that \(|a_i - a_j| = 1\) belong to the same team (i.e. skills of each pa... | The first line of the input contains one integer \(q\) (\(1 \le q \le 100\)) β the number of queries. Then \(q\) queries follow.The first line of the query contains one integer \(n\) (\(1 \le n \le 100\)) β the number of students in the query. The second line of the query contains \(n\) integers \(a_1, a_2, \dots, a_n\... | For each query, print the answer on it β the minimum number of teams you can form if no two students \(i\) and \(j\) such that \(|a_i - a_j| = 1\) may belong to the same team (i.e. skills of each pair of students in the same team has the difference strictly greater than \(1\)) | In the first query of the example, there are \(n=4\) students with the skills \(a=[2, 10, 1, 20]\). There is only one restriction here: the \(1\)-st and the \(3\)-th students can't be in the same team (because of \(|a_1 - a_3|=|2-1|=1\)). It is possible to divide them into \(2\) teams: for example, students \(1\), \(2\... | Input: 4 4 2 10 1 20 2 3 6 5 2 3 4 99 100 1 42 | Output: 2 1 2 1 | Beginner | 1 | 492 | 436 | 277 | 12 |
2,039 | C2 | 2039C2 | C2. Shohag Loves XOR (Hard Version) | 1,800 | bitmasks; brute force; math; number theory | This is the hard version of the problem. The differences between the two versions are highlighted in bold. You can only make hacks if both versions of the problem are solved.Shohag has two integers \(x\) and \(m\). Help him count the number of integers \(1 \le y \le m\) such that \(x \oplus y\) is divisible\(^{\text{β}... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first and only line of each test case contains two space-separated integers \(x\) and \(m\) (\(1 \le x \le 10^6\), \(1 \le m \le 10^{18}\)).It is guaranteed that the sum of \(x\) over all test cases does not exceed \(10... | For each test case, print a single integer β the number of suitable \(y\). | In the first test case, for \(x = 7\), there are \(3\) valid values for \(y\) among the integers from \(1\) to \(m = 10\), and they are \(1\), \(7\), and \(9\). \(y = 1\) is valid because \(x \oplus y = 7 \oplus 1 = 6\) and \(6\) is divisible by \(y = 1\). \(y = 7\) is valid because \(x \oplus y = 7 \oplus 7 = 0\) and ... | Input: 57 102 36 41 64 1 | Output: 3 2 2 6 1 | Medium | 4 | 527 | 325 | 74 | 20 |
1,725 | G | 1725G | G. Garage | 1,500 | binary search; geometry; math | Pak Chanek plans to build a garage. He wants the garage to consist of a square and a right triangle that are arranged like the following illustration. Define \(a\) and \(b\) as the lengths of two of the sides in the right triangle as shown in the illustration. An integer \(x\) is suitable if and only if we can construc... | The only line contains a single integer \(N\) (\(1 \leq N \leq 10^9\)). | An integer that represents the \(N\)-th smallest suitable number. | The \(3\)-rd smallest suitable number is \(7\). A square area of \(7\) can be obtained by assigning \(a=3\) and \(b=4\). | Input: 3 | Output: 7 | Medium | 3 | 576 | 71 | 65 | 17 |
2,045 | A | 2045A | A. Scrambled Scrabble | 1,700 | brute force; greedy | You are playing a word game using a standard set of \(26\) uppercase English letters: A β Z. In this game, you can form vowels and consonants as follows. The letters A, E, I, O, and U can only form a vowel. The letter Y can form either a vowel or a consonant. Each of the remaining letters other than A, E, I, O, U, and ... | A single line consisting of a string \(S\) (\(1 \leq |S| \leq 5000\)). The string \(S\) consists of only uppercase English letters. | If a word cannot be created, output 0. Otherwise, output a single integer representing the length of longest word that can be created. | Explanation for the sample input/output #1A possible longest word is JAKCARTAP, consisting of the syllables JAK, CAR, and TAP.Explanation for the sample input/output #2The whole string \(S\) is a word consisting of one syllable which is the concatenation of the consonant NG, the vowel E, and the consonant NG.Explanatio... | Input: ICPCJAKARTA | Output: 9 | Medium | 2 | 840 | 131 | 134 | 20 |
954 | C | 954C | C. Matrix Walk | 1,700 | implementation | There is a matrix A of size x Γ y filled with integers. For every , Ai, j = y(i - 1) + j. Obviously, every integer from [1..xy] occurs exactly once in this matrix. You have traversed some path in this matrix. Your path can be described as a sequence of visited cells a1, a2, ..., an denoting that you started in the cell... | The first line contains one integer number n (1 β€ n β€ 200000) β the number of cells you visited on your path (if some cell is visited twice, then it's listed twice).The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109) β the integers in the cells on your path. | If all possible values of x and y such that 1 β€ x, y β€ 109 contradict with the information about your path, print NO.Otherwise, print YES in the first line, and in the second line print the values x and y such that your path was possible with such number of lines and columns in the matrix. Remember that they must be po... | The matrix and the path on it in the first test looks like this: Also there exist multiple correct answers for both the first and the third examples. | Input: 81 2 3 6 9 8 5 2 | Output: YES3 3 | Medium | 1 | 1,104 | 273 | 354 | 9 |
685 | C | 685C | C. Optimal Point | 2,900 | binary search; math | When the river brought Gerda to the house of the Old Lady who Knew Magic, this lady decided to make Gerda her daughter. She wants Gerda to forget about Kay, so she puts all the roses from the garden underground.Mole, who lives in this garden, now can watch the roses without going up to the surface. Typical mole is blin... | The first line of the input contains an integer t t (1 β€ t β€ 100 000) β the number of test cases. Then follow exactly t blocks, each containing the description of exactly one test.The first line of each block contains an integer ni (1 β€ ni β€ 100 000) β the number of roses in the test. Then follow ni lines, containing t... | For each of t test cases print three integers β the coordinates of the optimal point to watch roses. If there are many optimal answers, print any of them.The coordinates of the optimal point may coincide with the coordinates of any rose. | In the first sample, the maximum Manhattan distance from the point to the rose is equal to 4.In the second sample, the maximum possible distance is 0. Note that the positions of the roses may coincide with each other and with the position of the optimal point. | Input: 150 0 40 0 -40 4 04 0 01 1 1 | Output: 0 0 0 | Master | 2 | 885 | 572 | 237 | 6 |
298 | B | 298B | B. Sail | 1,200 | brute force; greedy; implementation | The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y). If the wind blows to the east, the boat will move to (x + 1, y). If the ... | The first line contains five integers t, sx, sy, ex, ey (1 β€ t β€ 105, - 109 β€ sx, sy, ex, ey β€ 109). The starting location and the ending location will be different.The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: ""E""... | If they can reach (ex, ey) within t seconds, print the earliest time they can achieve it. Otherwise, print ""-1"" (without quotes). | In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4.In the second sample, they cannot sail to the destination. | Input: 5 0 0 1 1SESNW | Output: 4 | Easy | 3 | 691 | 375 | 131 | 2 |
402 | C | 402C | C. Searching for Graph | 1,500 | brute force; constructive algorithms; graphs | Let's call an undirected graph of n vertices p-interesting, if the following conditions fulfill: the graph contains exactly 2n + p edges; the graph doesn't contain self-loops and multiple edges; for any integer k (1 β€ k β€ n), any subgraph consisting of k vertices contains at most 2k + p edges. A subgraph of a graph is ... | The first line contains a single integer t (1 β€ t β€ 5) β the number of tests in the input. Next t lines each contains two space-separated integers: n, p (5 β€ n β€ 24; p β₯ 0; ) β the number of vertices in the graph and the interest value for the appropriate test. It is guaranteed that the required graph exists. | For each of the t tests print 2n + p lines containing the description of the edges of a p-interesting graph: the i-th line must contain two space-separated integers ai, bi (1 β€ ai, bi β€ n; ai β bi) β two vertices, connected by an edge in the resulting graph. Consider the graph vertices numbered with integers from 1 to ... | Input: 16 0 | Output: 1 21 31 41 51 62 32 42 52 63 43 53 6 | Medium | 3 | 582 | 310 | 456 | 4 | |
1,833 | G | 1833G | G. Ksyusha and Chinchilla | 1,800 | constructive algorithms; dfs and similar; dp; dsu; greedy; implementation; trees | Ksyusha has a pet chinchilla, a tree on \(n\) vertices and huge scissors. A tree is a connected graph without cycles. During a boring physics lesson Ksyusha thought about how to entertain her pet.Chinchillas like to play with branches. A branch is a tree of \(3\) vertices. The branch looks like this. A cut is the remov... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β 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 vertices in the tree.The next \(n - 1\) rows of each testcase contain integers \(v_i\) and \(u_i\) (\(1 \le v_i, u_i ... | Print the answer for each testcase.If the desired way to cut the tree does not exist, print \(-1\).Otherwise, print an integer \(k\) β the number of edges to be cut. In the next line, print \(k\) different integers \(e_i\) (\(1 \le e_i < n\)) β numbers of the edges to be cut. If \(k = 0\), print an empty string instead... | The first testcase in first test. | Input: 491 24 37 95 44 63 28 71 761 21 34 31 56 161 23 23 44 56 551 35 35 23 4 | Output: 2 2 8 -1 1 3 -1 | Medium | 7 | 641 | 537 | 371 | 18 |
1,493 | D | 1493D | D. GCD of an Array | 2,100 | brute force; data structures; hashing; implementation; math; number theory; sortings; two pointers | You are given an array \(a\) of length \(n\). You are asked to process \(q\) queries of the following format: given integers \(i\) and \(x\), multiply \(a_i\) by \(x\).After processing each query you need to output the greatest common divisor (GCD) of all elements of the array \(a\).Since the answer can be too large, y... | The first line contains two integers β \(n\) and \(q\) (\(1 \le n, q \le 2 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 2 \cdot 10^5\)) β the elements of the array \(a\) before the changes.The next \(q\) lines contain queries in the following format: each line contain... | Print \(q\) lines: after processing each query output the GCD of all elements modulo \(10^9+7\) on a separate line. | After the first query the array is \([12, 6, 8, 12]\), \(\operatorname{gcd}(12, 6, 8, 12) = 2\).After the second query β \([12, 18, 8, 12]\), \(\operatorname{gcd}(12, 18, 8, 12) = 2\).After the third query β \([12, 18, 24, 12]\), \(\operatorname{gcd}(12, 18, 24, 12) = 6\).Here the \(\operatorname{gcd}\) function denote... | Input: 4 3 1 6 8 12 1 12 2 3 3 3 | Output: 2 2 6 | Hard | 8 | 364 | 401 | 115 | 14 |
643 | G | 643G | G. Choosing Ads | 3,200 | data structures | One social network developer recently suggested a new algorithm of choosing ads for users.There are n slots which advertisers can buy. It is possible to buy a segment of consecutive slots at once. The more slots you own, the bigger are the chances your ad will be shown to users.Every time it is needed to choose ads to ... | The first line of the input contains three integers n, m and p (1 β€ n, m β€ 150 000, 20 β€ p β€ 100) β the number of slots, the number of queries to your system and threshold for which display of the ad is guaranteed.Next line contains n integers ai (1 β€ ai β€ 150 000), where the i-th number means id of advertiser who curr... | For each query of the second type answer should be printed in a separate line. First integer of the answer should be the number of advertisements that will be shown . Next cnt integers should be advertisers' ids. It is allowed to print one advertiser more than once, but each advertiser that owns at least slots of the s... | Samples demonstrate that you actually have quite a lot of freedom in choosing advertisers. | Input: 5 9 331 2 1 3 32 1 52 1 52 1 32 3 31 2 4 52 1 52 3 51 4 5 12 1 5 | Output: 3 1 2 32 1 32 2 13 1 1000 10001 52 5 32 1 5 | Master | 1 | 781 | 619 | 364 | 6 |
1,696 | B | 1696B | B. NIT Destroys the Universe | 900 | greedy | For a collection of integers \(S\), define \(\operatorname{mex}(S)\) as the smallest non-negative integer that does not appear in \(S\).NIT, the cleaver, decides to destroy the universe. He is not so powerful as Thanos, so he can only destroy the universe by snapping his fingers several times.The universe can be repres... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). Description of the test cases follows.The first line of each test case contains one integer \(n\) (\(1\le n\le 10^5\)).The second line of each test case contains \(n\) integers \(a_1\), \(a_2\), \(\ldo... | For each test case, print one integer β the answer to the problem. | In the first test case, we do \(0\) operations and all elements in the array are already equal to \(0\).In the second test case, one optimal way is doing the operation with \(l=2\), \(r=5\).In the third test case, one optimal way is doing the operation twice, respectively with \(l=4\), \(r=4\) and \(l=2\), \(r=6\).In t... | Input: 440 0 0 050 1 2 3 470 2 3 0 1 2 011000000000 | Output: 0 1 2 1 | Beginner | 1 | 925 | 448 | 66 | 16 |
2,117 | A | 2117A | A. False Alarm | 800 | greedy; implementation | Yousef is at the entrance of a long hallway with \(n\) doors in a row, numbered from \(1\) to \(n\). He needs to pass through all the doors from \(1\) to \(n\) in order of numbering and reach the exit (past door \(n\)). Each door can be open or closed. If a door is open, Yousef passes through it in \(1\) second. If the... | The first line of the input contains an integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.The first line of each test case contains two integers \(n, x\) (\(1 \le n, x \le 10\)) β the number of doors and the number of seconds of the button, respectively.The second line of each test case contains \(n\) int... | For each test case, output ""YES"" if Yousef can reach the exit, and ""NO"" otherwise.You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses. | In the first test case, the optimal way is as follows: At time \(0\), the door is open, so Yousef passes. At time \(1\), the door is closed, Yousef can use the button now and pass through the door. At time \(2\), the button's effect is still on, so Yousef can still pass. At time \(3\), the button's effect has finished,... | Input: 74 20 1 1 06 31 0 1 1 0 08 81 1 1 0 0 1 1 11 215 11 0 1 0 17 40 0 0 1 1 0 110 30 1 0 0 1 0 0 1 0 0 | Output: YES NO YES YES NO YES NO | Beginner | 2 | 618 | 546 | 247 | 21 |
164 | A | 164A | A. Variable, or There and Back Again | 1,700 | dfs and similar; graphs | Life is not easy for the perfectly common variable named Vasya. Wherever it goes, it is either assigned a value, or simply ignored, or is being used!Vasya's life goes in states of a program. In each state, Vasya can either be used (for example, to calculate the value of another variable), or be assigned a value, or ign... | The first line contains two space-separated integers n and m (1 β€ n, m β€ 105) β the numbers of states and transitions, correspondingly.The second line contains space-separated n integers f1, f2, ..., fn (0 β€ fi β€ 2), fi described actions performed upon Vasya in state i: 0 represents ignoring, 1 β assigning a value, 2 β... | Print n integers r1, r2, ..., rn, separated by spaces or new lines. Number ri should equal 1, if Vasya's value in state i is interesting to the world and otherwise, it should equal 0. The states are numbered from 1 to n in the order, in which they are described in the input. | In the first sample the program states can be used to make the only path in which the value of Vasya interests the world, 1 2 3 4; it includes all the states, so in all of them Vasya's value is interesting to the world.The second sample the only path in which Vasya's value is interesting to the world is , β 1 3; state ... | Input: 4 31 0 0 21 22 33 4 | Output: 1111 | Medium | 2 | 840 | 553 | 275 | 1 |
438 | D | 438D | D. The Child and Sequence | 2,300 | data structures; math | At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite sequence of Picks.Fortunately, Picks remembers how to repair the sequence. Initially he should create an integer array a[1], a[2], ..., a[n]. Then he s... | The first line of input contains two integer: n, m (1 β€ n, m β€ 105). The second line contains n integers, separated by space: a[1], a[2], ..., a[n] (1 β€ a[i] β€ 109) β initial value of array elements.Each of the next m lines begins with a number type . If type = 1, there will be two integers more in the line: l, r (1 β€ ... | For each operation 1, please print a line containing the answer. Notice that the answer may exceed the 32-bit integer. | Consider the first testcase: At first, a = {1, 2, 3, 4, 5}. After operation 1, a = {1, 2, 3, 0, 1}. After operation 2, a = {1, 2, 5, 0, 1}. At operation 3, 2 + 5 + 0 + 1 = 8. After operation 4, a = {1, 2, 2, 0, 1}. At operation 5, 1 + 2 + 2 = 5. | Input: 5 51 2 3 4 52 3 5 43 3 51 2 52 1 3 31 1 3 | Output: 85 | Expert | 2 | 737 | 620 | 118 | 4 |
1,133 | F2 | 1133F2 | F2. Spanning Tree with One Fixed Degree | 1,900 | constructive algorithms; dfs and similar; dsu; graphs; greedy | You are given an undirected unweighted connected graph consisting of \(n\) vertices and \(m\) edges. It is guaranteed that there are no self-loops or multiple edges in the given graph.Your task is to find any spanning tree of this graph such that the degree of the first vertex (vertex with label \(1\) on it) is equal t... | The first line contains three integers \(n\), \(m\) and \(D\) (\(2 \le n \le 2 \cdot 10^5\), \(n - 1 \le m \le min(2 \cdot 10^5, \frac{n(n-1)}{2}), 1 \le D < n\)) β the number of vertices, the number of edges and required degree of the first vertex, respectively.The following \(m\) lines denote edges: edge \(i\) is rep... | If there is no spanning tree satisfying the condition from the problem statement, print ""NO"" in the first line.Otherwise print ""YES"" in the first line and then print \(n-1\) lines describing the edges of a spanning tree such that the degree of the first vertex (vertex with label \(1\) on it) is equal to \(D\). Make... | The picture corresponding to the first and second examples: The picture corresponding to the third example: | Input: 4 5 1 1 2 1 3 1 4 2 3 3 4 | Output: YES 2 1 2 3 3 4 | Hard | 5 | 449 | 718 | 553 | 11 |
1,334 | B | 1334B | B. Middle Class | 1,100 | greedy; sortings | Many years ago Berland was a small country where only \(n\) people lived. Each person had some savings: the \(i\)-th one had \(a_i\) burles.The government considered a person as wealthy if he had at least \(x\) burles. To increase the number of wealthy people Berland decided to carry out several reforms. Each reform lo... | The first line contains single integer \(T\) (\(1 \le T \le 1000\)) β the number of test cases.Next \(2T\) lines contain the test cases β two lines per test case. The first line contains two integers \(n\) and \(x\) (\(1 \le n \le 10^5\), \(1 \le x \le 10^9\)) β the number of people and the minimum amount of money to b... | Print \(T\) integers β one per test case. For each test case print the maximum possible number of wealthy people after several (maybe zero) reforms. | The first test case is described in the statement.In the second test case, the government, for example, could carry out two reforms: \([\underline{11}, \underline{9}, 11, 9] \rightarrow [10, 10, \underline{11}, \underline{9}] \rightarrow [10, 10, 10, 10]\).In the third test case, the government couldn't make even one p... | Input: 4 4 3 5 1 2 1 4 10 11 9 11 9 2 5 4 3 3 7 9 4 9 | Output: 2 4 0 3 | Easy | 2 | 1,041 | 539 | 148 | 13 |
1,056 | E | 1056E | E. Check Transcription | 2,100 | brute force; data structures; hashing; strings | One of Arkady's friends works at a huge radio telescope. A few decades ago the telescope has sent a signal \(s\) towards a faraway galaxy. Recently they've received a response \(t\) which they believe to be a response from aliens! The scientists now want to check if the signal \(t\) is similar to \(s\).The original sig... | The first line contains a string \(s\) (\(2 \le |s| \le 10^5\)) consisting of zeros and ones β the original signal.The second line contains a string \(t\) (\(1 \le |t| \le 10^6\)) consisting of lowercase English letters only β the received signal.It is guaranteed, that the string \(s\) contains at least one '0' and at ... | Print a single integer β the number of pairs of strings \(r_0\) and \(r_1\) that transform \(s\) to \(t\).In case there are no such pairs, print \(0\). | In the first example, the possible pairs \((r_0, r_1)\) are as follows: ""a"", ""aaaaa"" ""aa"", ""aaaa"" ""aaaa"", ""aa"" ""aaaaa"", ""a"" The pair ""aaa"", ""aaa"" is not allowed, since \(r_0\) and \(r_1\) must be different.In the second example, the following pairs are possible: ""ko"", ""kokotlin"" ""koko"", ""tlin... | Input: 01 aaaaaa | Output: 4 | Hard | 4 | 998 | 334 | 151 | 10 |
1,042 | F | 1042F | F. Leaf Sets | 2,400 | data structures; dfs and similar; dsu; graphs; greedy; sortings; trees | You are given an undirected tree, consisting of \(n\) vertices.The vertex is called a leaf if it has exactly one vertex adjacent to it.The distance between some pair of vertices is the number of edges in the shortest path between them.Let's call some set of leaves beautiful if the maximum distance between any pair of l... | The first line contains two integers \(n\) and \(k\) (\(3 \le n \le 10^6\), \(1 \le k \le 10^6\)) β the number of vertices in the tree and the maximum distance between any pair of leaves in each beautiful set.Each of the next \(n - 1\) lines contains two integers \(v_i\) and \(u_i\) (\(1 \le v_i, u_i \le n\)) β the des... | Print a single integer β the minimal number of beautiful sets the split can have. | Here is the graph for the first example: | Input: 9 31 21 32 42 53 66 76 83 9 | Output: 2 | Expert | 7 | 476 | 401 | 81 | 10 |
83 | D | 83D | D. Numbers | 2,400 | dp; math; number theory | One quite ordinary day Valera went to school (there's nowhere else he should go on a week day). In a maths lesson his favorite teacher Ms. Evans told students about divisors. Despite the fact that Valera loved math, he didn't find this particular topic interesting. Even more, it seemed so boring that he fell asleep in ... | The first and only line contains three positive integers a, b, k (1 β€ a β€ b β€ 2Β·109, 2 β€ k β€ 2Β·109). | Print on a single line the answer to the given problem. | Comments to the samples from the statement: In the first sample the answer is numbers 2, 4, 6, 8, 10.In the second one β 15, 21In the third one there are no such numbers. | Input: 1 10 2 | Output: 5 | Expert | 3 | 1,293 | 100 | 55 | 0 |
648 | D | 648D | D. Π‘ΠΎΠ±Π°ΡΠΊΠΈ ΠΈ ΠΌΠΈΡΠΊΠΈ | 1,900 | data structures; greedy; sortings | ΠΠ° ΠΊΠΎΠΎΡΠ΄ΠΈΠ½Π°ΡΠ½ΠΎΠΉ ΠΏΡΡΠΌΠΎΠΉ ΡΠΈΠ΄ΠΈΡ n ΡΠΎΠ±Π°ΡΠ΅ΠΊ, i-Ρ ΡΠΎΠ±Π°ΡΠΊΠ° Π½Π°Ρ
ΠΎΠ΄ΠΈΡΡΡ Π² ΡΠΎΡΠΊΠ΅ xi. ΠΡΠΎΠΌΠ΅ ΡΠΎΠ³ΠΎ, Π½Π° ΠΏΡΡΠΌΠΎΠΉ Π΅ΡΡΡ m ΠΌΠΈΡΠΎΠΊ Ρ Π΅Π΄ΠΎΠΉ, Π΄Π»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΠΈΠ·Π²Π΅ΡΡΠ½Π° Π΅Ρ ΠΊΠΎΠΎΡΠ΄ΠΈΠ½Π°ΡΠ° Π½Π° ΠΏΡΡΠΌΠΎΠΉ uj ΠΈ Π²ΡΠ΅ΠΌΡ tj, ΡΠ΅ΡΠ΅Π· ΠΊΠΎΡΠΎΡΠΎΠ΅ Π΅Π΄Π° Π² ΠΌΠΈΡΠΊΠ΅ ΠΎΡΡΡΠ½Π΅Ρ ΠΈ ΡΡΠ°Π½Π΅Ρ Π½Π΅Π²ΠΊΡΡΠ½ΠΎΠΉ. ΠΡΠΎ Π·Π½Π°ΡΠΈΡ, ΡΡΠΎ Π΅ΡΠ»ΠΈ ΡΠΎΠ±Π°ΡΠΊΠ° ΠΏΡΠΈΠ±Π΅ΠΆΠΈΡ ΠΊ ΠΌΠΈΡΠΊΠ΅ Π² ΠΌΠΎΠΌΠ΅Π½Ρ Π²ΡΠ΅ΠΌΠ΅Π½ΠΈ, ΡΡΡΠΎΠ³ΠΎ Π±ΠΎΠ»ΡΡΠΈΠΉ tj, ΡΠΎ Π΅Π΄Π° ... | Π ΠΏΠ΅ΡΠ²ΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ Π½Π°Ρ
ΠΎΠ΄ΠΈΡΡΡ ΠΏΠ°ΡΠ° ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ΅Π» n ΠΈ m (1 β€ n, m β€ 200 000) β ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΠΎΠ±Π°ΡΠ΅ΠΊ ΠΈ ΠΌΠΈΡΠΎΠΊ ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²Π΅Π½Π½ΠΎ.ΠΠΎ Π²ΡΠΎΡΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ Π½Π°Ρ
ΠΎΠ΄ΡΡΡΡ n ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ΅Π» xi ( - 109 β€ xi β€ 109) β ΠΊΠΎΠΎΡΠ΄ΠΈΠ½Π°ΡΠ° i-ΠΉ ΡΠΎΠ±Π°ΡΠΊΠΈ.Π ΡΠ»Π΅Π΄ΡΡΡΠΈΡ
m ΡΡΡΠΎΠΊΠ°Ρ
Π½Π°Ρ
ΠΎΠ΄ΡΡΡΡ ΠΏΠ°ΡΡ ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ΅Π» uj ΠΈ tj ( - 109 β€ uj β€ 109, 1 β€ tj β€ 109) β ΠΊΠΎΠΎΡΠ΄ΠΈΠ½Π°ΡΠ° j-ΠΉ ΠΌΠΈΡΠΊΠΈ ΠΈ Π²... | ΠΡΠ²Π΅Π΄ΠΈΡΠ΅ ΠΎΠ΄Π½ΠΎ ΡΠ΅Π»ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ a β ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΠΎΠ±Π°ΡΠ΅ΠΊ, ΠΊΠΎΡΠΎΡΡΠ΅ ΡΠΌΠΎΠ³ΡΡ ΠΏΠΎΠΊΡΡΠ°ΡΡ. | Π ΠΏΠ΅ΡΠ²ΠΎΠΌ ΠΏΡΠΈΠΌΠ΅ΡΠ΅ ΠΏΠ΅ΡΠ²Π°Ρ ΡΠΎΠ±Π°ΡΠΊΠ° ΠΏΠΎΠ±Π΅ΠΆΠΈΡ Π½Π°ΠΏΡΠ°Π²ΠΎ ΠΊ ΠΏΠ΅ΡΠ²ΠΎΠΉ ΠΌΠΈΡΠΊΠ΅, ΡΡΠ΅ΡΡΡ ΡΠΎΠ±Π°ΡΠΊΠ° ΡΡΠ°Π·Ρ Π½Π°ΡΠ½ΡΡ Π΅ΡΡΡ ΠΈΠ· Π²ΡΠΎΡΠΎΠΉ ΠΌΠΈΡΠΊΠΈ, ΡΠ΅ΡΠ²ΡΡΡΠ°Ρ ΡΠΎΠ±Π°ΡΠΊΠ° ΠΏΠΎΠ±Π΅ΠΆΠΈΡ Π²Π»Π΅Π²ΠΎ ΠΊ ΡΡΠ΅ΡΡΠ΅ΠΉ ΠΌΠΈΡΠΊΠ΅, Π° ΠΏΡΡΠ°Ρ ΡΠΎΠ±Π°ΡΠΊΠ° ΠΏΠΎΠ±Π΅ΠΆΠΈΡ Π²Π»Π΅Π²ΠΎ ΠΊ ΡΠ΅ΡΠ²ΡΡΡΠΎΠΉ ΠΌΠΈΡΠΊΠ΅. | Input: 5 4-2 0 4 8 13-1 14 36 311 2 | Output: 4 | Hard | 3 | 743 | 491 | 87 | 6 |
853 | E | 853E | E. Lada Malina | 3,400 | data structures; geometry | After long-term research and lots of experiments leading Megapolian automobile manufacturer Β«AutoVozΒ» released a brand new car model named Β«Lada MalinaΒ». One of the most impressive features of Β«Lada MalinaΒ» is its highly efficient environment-friendly engines.Consider car as a point in Oxy plane. Car is equipped with k... | The first line of input contains three integers k, n, q (2 β€ k β€ 10, 1 β€ n β€ 105, 1 β€ q β€ 105), the number of engines of Β«Lada MalinaΒ», number of factories producing Β«Lada MalinaΒ» and number of options of an exposition time and location respectively.The following k lines contain the descriptions of Β«Lada MalinaΒ» engine... | For each possible option of the exposition output the number of cars that will be able to get to the exposition location by the moment of its beginning. | Images describing sample tests are given below. Exposition options are denoted with crosses, factories are denoted with points. Each factory is labeled with a number of cars that it has.First sample test explanation: Car from the first factory is not able to get to the exposition location in time. Car from the second f... | Input: 2 4 11 1-1 12 3 12 -2 1-2 1 1-2 -2 10 0 2 | Output: 3 | Master | 2 | 2,147 | 1,133 | 152 | 8 |
1,545 | C | 1545C | C. AquaMoon and Permutations | 2,800 | 2-sat; brute force; combinatorics; constructive algorithms; graph matchings; graphs | Cirno has prepared \(n\) arrays of length \(n\) each. Each array is a permutation of \(n\) integers from \(1\) to \(n\). These arrays are special: for all \(1 \leq i \leq n\), if we take the \(i\)-th element of each array and form another array of length \(n\) with these elements, the resultant array is also a permutat... | The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 100\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(5 \leq n \leq 500\)).Then \(2n\) lines followed. The \(i\)-th of these lines contains \(n\) integers, represent... | For each test case print two lines.In the first line, print the number of good subsets by modulo \(998\,244\,353\).In the second line, print \(n\) indices from \(1\) to \(2n\) β indices of the \(n\) arrays that form a good subset (you can print them in any order). If there are several possible answers β print any of th... | In the first test case, the number of good subsets is \(1\). The only such subset is the set of arrays with indices \(1\), \(2\), \(3\), \(4\), \(5\), \(6\), \(7\).In the second test case, the number of good subsets is \(2\). They are \(1\), \(3\), \(5\), \(6\), \(10\) or \(2\), \(4\), \(7\), \(8\), \(9\). | Input: 3 7 1 2 3 4 5 6 7 2 3 4 5 6 7 1 3 4 5 6 7 1 2 4 5 6 7 1 2 3 5 6 7 1 2 3 4 6 7 1 2 3 4 5 7 1 2 3 4 5 6 1 2 3 4 5 7 6 1 3 4 5 6 7 2 1 4 5 6 7 3 2 1 5 6 7 4 2 3 1 6 7 5 2 3 4 1 7 6 2 3 4 5 1 7 2 3 4 5 6 5 4 5 1 2 3 3 5 2 4 1 1 2 3 4 5 5 2 4 1 3 3 4 5 1 2 2 3 4 5 1 1 3 5 2 4 4 1 3 5 2 2 4 1 3 5 5 1 2 3 4 6 2 3 4 5 6... | Master | 6 | 1,548 | 427 | 323 | 15 |
269 | A | 269A | A. Magical Boxes | 1,600 | greedy; math | Emuskald is a well-known illusionist. One of his trademark tricks involves a set of magical boxes. The essence of the trick is in packing the boxes inside other boxes.From the top view each magical box looks like a square with side length equal to 2k (k is an integer, k β₯ 0) units. A magical box v can be put inside a m... | The first line of input contains an integer n (1 β€ n β€ 105), the number of different sizes of boxes Emuskald has. Each of following n lines contains two integers ki and ai (0 β€ ki β€ 109, 1 β€ ai β€ 109), which means that Emuskald has ai boxes with side length 2ki. It is guaranteed that all of ki are distinct. | Output a single integer p, such that the smallest magical box that can contain all of Emuskaldβs boxes has side length 2p. | Picture explanation. If we have 3 boxes with side length 2 and 5 boxes with side length 1, then we can put all these boxes inside a box with side length 4, for example, as shown in the picture.In the second test case, we can put all four small boxes into a box with side length 2. | Input: 20 31 5 | Output: 3 | Medium | 2 | 830 | 308 | 122 | 2 |
1,202 | D | 1202D | D. Print a 1337-string... | 1,900 | combinatorics; constructive algorithms; math; strings | The subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.You are given an integer \(n\). You have to find a sequence \(s\) consisting of digits \(\{1, 3, 7\}\) such that it has exactly \(n\) subsequences equal to \(1337\).For ... | The first line contains one integer \(t\) (\(1 \le t \le 10\)) β the number of queries. Next \(t\) lines contains a description of queries: the \(i\)-th line contains one integer \(n_i\) (\(1 \le n_i \le 10^9\)). | For the \(i\)-th query print one string \(s_i\) (\(1 \le |s_i| \le 10^5\)) consisting of digits \(\{1, 3, 7\}\). String \(s_i\) must have exactly \(n_i\) subsequences \(1337\). If there are multiple such strings, print any of them. | Input: 2 6 1 | Output: 113337 1337 | Hard | 4 | 1,177 | 212 | 231 | 12 | |
333 | A | 333A | A. Secrets | 1,600 | greedy | Gerald has been selling state secrets at leisure. All the secrets cost the same: n marks. The state which secrets Gerald is selling, has no paper money, only coins. But there are coins of all positive integer denominations that are powers of three: 1 mark, 3 marks, 9 marks, 27 marks and so on. There are no coins of oth... | The single line contains a single integer n (1 β€ n β€ 1017).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 a single line print an integer: the maximum number of coins the unlucky buyer could have paid with. | In the first test case, if a buyer has exactly one coin of at least 3 marks, then, to give Gerald one mark, he will have to give this coin. In this sample, the customer can not have a coin of one mark, as in this case, he will be able to give the money to Gerald without any change.In the second test case, if the buyer ... | Input: 1 | Output: 1 | Medium | 1 | 1,151 | 206 | 102 | 3 |
1,936 | B | 1936B | B. Pinball | 2,000 | binary search; data structures; implementation; math; two pointers | There is a one-dimensional grid of length \(n\). The \(i\)-th cell of the grid contains a character \(s_i\), which is either '<' or '>'.When a pinball is placed on one of the cells, it moves according to the following rules: If the pinball is on the \(i\)-th cell and \(s_i\) is '<', the pinball moves one cell to the le... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^5\)). The description of the test cases follows.The first line of each test case contains an integer \(n\) (\(1 \le n \le 5 \cdot 10^5\)).The second line of each test case contains a string \(s_1s_2 \ldots s... | For each test case, for each \(i\) (\(1 \le i \le n\)) output the answer if a pinball is initially placed on the \(i\)-th cell. | In the first test case, the movement of the pinball for \(i=1\) is shown in the following pictures. It takes the pinball \(3\) seconds to leave the grid. The movement of the pinball for \(i=2\) is shown in the following pictures. It takes the pinball \(6\) seconds to leave the grid. | Input: 33><<4<<<<6<><<<> | Output: 3 6 5 1 2 3 4 1 4 7 10 8 1 | Hard | 5 | 972 | 472 | 127 | 19 |
1,476 | F | 1476F | F. Lanterns | 3,000 | binary search; data structures; dp | There are \(n\) lanterns in a row. The lantern \(i\) is placed in position \(i\) and has power equal to \(p_i\).Each lantern can be directed to illuminate either some lanterns to the left or some lanterns to the right. If the \(i\)-th lantern is turned to the left, it illuminates all such lanterns \(j\) that \(j \in [i... | The first line contains one integer \(t\) (\(1 \le t \le 10000\)) β the number of test cases.Each test case consists of two lines. The first line contains one integer \(n\) (\(2 \le n \le 3 \cdot 10^5\)) β the number of lanterns.The second line contains \(n\) integers \(p_1, p_2, \dots, p_n\) (\(0 \le p_i \le n\)) β th... | For each test case, print the answer as follows:If it is possible to direct all lanterns so that each lantern is illuminated, print YES in the first line and a string of \(n\) characters L and/or R (the \(i\)-th character is L if the \(i\)-th lantern is turned to the left, otherwise this character is R) in the second l... | Input: 4 8 0 0 3 1 1 1 1 2 2 1 1 2 2 2 2 0 1 | Output: YES RRLLLLRL YES RL YES RL NO | Master | 3 | 596 | 422 | 440 | 14 | |
1,558 | E | 1558E | E. Down Below | 3,000 | binary search; dfs and similar; graphs; greedy; meet-in-the-middle; shortest paths | In a certain video game, the player controls a hero characterized by a single integer value: power.On the current level, the hero got into a system of \(n\) caves numbered from \(1\) to \(n\), and \(m\) tunnels between them. Each tunnel connects two distinct caves. Any two caves are connected with at most one tunnel. A... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). Description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(3 \le n \le 1000\); \(n \le m \le min(\frac{n(n-1)}{2}, 2000)\)) β the number of caves and... | For each test case print a single integer β the smallest possible power the hero must start the level with to be able to beat all the monsters and pass the level. | In the first test case, the hero can pass the level with initial power \(15\) as follows: move from cave \(1\) to cave \(2\): since \(15 > 11\), the hero beats the monster, and his power increases to \(15 + 8 = 23\); move from cave \(2\) to cave \(3\): since \(23 > 22\), the hero beats the monster, and his power increa... | Input: 3 4 4 11 22 13 8 7 5 1 2 2 3 3 4 4 1 4 4 11 22 13 5 7 8 1 2 2 3 3 4 4 1 5 7 10 40 20 30 7 2 10 5 1 2 1 5 2 3 2 4 2 5 3 4 4 5 | Output: 15 15 19 | Master | 6 | 1,846 | 1,177 | 162 | 15 |
557 | D | 557D | D. Vitaly and Cycle | 2,000 | combinatorics; dfs and similar; graphs; math | After Vitaly was expelled from the university, he became interested in the graph theory.Vitaly especially liked the cycles of an odd length in which each vertex occurs at most once.Vitaly was wondering how to solve the following problem. You are given an undirected graph consisting of n vertices and m edges, not necess... | The first line of the input contains two integers n and m ( β the number of vertices in the graph and the number of edges in the graph.Next m lines contain the descriptions of the edges of the graph, one edge per line. Each edge is given by a pair of integers ai, bi (1 β€ ai, bi β€ n) β the vertices that are connected by... | Print in the first line of the output two space-separated integers t and w β the minimum number of edges that should be added to the graph to form a simple cycle of an odd length consisting of more than one vertex where each vertex occurs at most once, and the number of ways to do this. | The simple cycle is a cycle that doesn't contain any vertex twice. | Input: 4 41 21 34 24 3 | Output: 1 2 | Hard | 4 | 918 | 515 | 287 | 5 |
311 | B | 311B | B. Cats Transport | 2,400 | data structures; dp | Zxr960115 is owner of a large farm. He feeds m cute cats and employs p feeders. There's a straight road across the farm and n hills along the road, numbered from 1 to n from left to right. The distance between hill i and (i - 1) is di meters. The feeders live in hill 1.One day, the cats went out to play. Cat i went on ... | The first line of the input contains three integers n, m, p (2 β€ n β€ 105, 1 β€ m β€ 105, 1 β€ p β€ 100).The second line contains n - 1 positive integers d2, d3, ..., dn (1 β€ di < 104).Each of the next m lines contains two integers hi and ti (1 β€ hi β€ n, 0 β€ ti β€ 109). | Output an integer, the minimum sum of waiting time of all cats.Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. | Input: 4 6 21 3 51 02 14 91 102 103 12 | Output: 3 | Expert | 2 | 1,196 | 264 | 212 | 3 | |
1,000 | G | 1000G | G. Two-Paths | 2,700 | data structures; dp; trees | You are given a weighted tree (undirected connected graph with no cycles, loops or multiple edges) with \(n\) vertices. The edge \(\{u_j, v_j\}\) has weight \(w_j\). Also each vertex \(i\) has its own value \(a_i\) assigned to it.Let's call a path starting in vertex \(u\) and ending in vertex \(v\), where each edge can... | The first line contains two integers \(n\) and \(q\) (\(2 \le n \le 3 \cdot 10^5\), \(1 \le q \le 4 \cdot 10^5\)) β the number of vertices in the tree and the number of queries.The second line contains \(n\) space-separated integers \(a_1, a_2, \dots, a_n\) \((1 \le a_i \le 10^9)\) β the values of the vertices.Next \(n... | For each query print one integer per line β maximal profit \(\text{Pr}(p)\) of the some 2-path \(p\) with the corresponding endpoints. | Explanation of queries: \((1, 1)\) β one of the optimal 2-paths is the following: \(1 \rightarrow 2 \rightarrow 4 \rightarrow 5 \rightarrow 4 \rightarrow 2 \rightarrow 3 \rightarrow 2 \rightarrow 1\). \(\text{Pr}(p) = (a_1 + a_2 + a_3 + a_4 + a_5) - (2 \cdot w(1,2) + 2 \cdot w(2,3) + 2 \cdot w(2,4) + 2 \cdot w(4,5)) = ... | Input: 7 66 5 5 3 2 1 21 2 22 3 22 4 14 5 16 4 27 3 251 14 45 66 43 43 7 | Output: 999812-14 | Master | 3 | 976 | 753 | 134 | 10 |
626 | F | 626F | F. Group Projects | 2,400 | dp | There are n students in a class working on group projects. The students will divide into groups (some students may be in groups alone), work on their independent pieces, and then discuss the results together. It takes the i-th student ai minutes to finish his/her independent piece.If students work at different paces, i... | The first line contains two space-separated integers n and k (1 β€ n β€ 200, 0 β€ k β€ 1000) β the number of students and the maximum total imbalance allowed, respectively.The second line contains n space-separated integers ai (1 β€ ai β€ 500) β the time it takes the i-th student to complete his/her independent piece of work... | Print a single integer, the number of ways the students can form groups. As the answer may be large, print its value modulo 109 + 7. | In the first sample, we have three options: The first and second students form a group, and the third student forms a group. Total imbalance is 2 + 0 = 2. The first student forms a group, and the second and third students form a group. Total imbalance is 0 + 1 = 1. All three students form their own groups. Total imbala... | Input: 3 22 4 5 | Output: 3 | Expert | 1 | 854 | 321 | 132 | 6 |
1,997 | B | 1997B | B. Make Three Regions | 1,100 | constructive algorithms; two pointers | There is a grid, consisting of \(2\) rows and \(n\) columns. Each cell of the grid is either free or blocked.A free cell \(y\) is reachable from a free cell \(x\) if at least one of these conditions holds: \(x\) and \(y\) share a side; there exists a free cell \(z\) such that \(z\) is reachable from \(x\) and \(y\) is ... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of columns.The \(i\)-th of the next two lines contains a description of the \(i\)-th row of the grid β the string ... | For each test case, print a single integer β the number of cells such that the number of connected regions becomes \(3\) if this cell is blocked. | In the first test case, if the cell \((1, 3)\) is blocked, the number of connected regions becomes \(3\) (as shown in the picture from the statement). | Input: 48.......x.x.xx...2....3xxxxxx9..x.x.x.xx.......x | Output: 1 0 0 2 | Easy | 2 | 924 | 604 | 145 | 19 |
1,264 | E | 1264E | E. Beautiful League | 2,700 | constructive algorithms; flows; graph matchings | A football league has recently begun in Beautiful land. There are \(n\) teams participating in the league. Let's enumerate them with integers from \(1\) to \(n\).There will be played exactly \(\frac{n(n-1)}{2}\) matches: each team will play against all other teams exactly once. In each match, there is always a winner a... | The first line contains two integers \(n, m\) (\(3 \leq n \leq 50, 0 \leq m \leq \frac{n(n-1)}{2}\)) β the number of teams in the football league and the number of matches that were played.Each of \(m\) following lines contains two integers \(u\) and \(v\) (\(1 \leq u, v \leq n\), \(u \neq v\)) denoting that the \(u\)-... | Print \(n\) lines, each line having a string of exactly \(n\) characters. Each character must be either \(0\) or \(1\).Let \(a_{ij}\) be the \(j\)-th number in the \(i\)-th line. For all \(1 \leq i \leq n\) it should be true, that \(a_{ii} = 0\). For all pairs of teams \(i \neq j\) the number \(a_{ij}\) indicates the r... | The beauty of league in the first test case is equal to \(3\) because there exists three beautiful triples: \((1, 2, 3)\), \((2, 3, 1)\), \((3, 1, 2)\).The beauty of league in the second test is equal to \(6\) because there exists six beautiful triples: \((1, 2, 4)\), \((2, 4, 1)\), \((4, 1, 2)\), \((2, 4, 3)\), \((4, ... | Input: 3 1 1 2 | Output: 010 001 100 | Master | 3 | 1,060 | 431 | 836 | 12 |
508 | E | 508E | E. Arthur and Brackets | 2,200 | dp; greedy | Notice that the memory limit is non-standard.Recently Arthur and Sasha have studied correct bracket sequences. Arthur understood this topic perfectly and become so amazed about correct bracket sequences, so he even got himself a favorite correct bracket sequence of length 2n. Unlike Arthur, Sasha understood the topic v... | The first line contains integer n (1 β€ n β€ 600), the number of opening brackets in Arthur's favorite correct bracket sequence. Next n lines contain numbers li and ri (1 β€ li β€ ri < 2n), representing the segment where lies the distance from the i-th opening bracket and the corresponding closing one. The descriptions of ... | If it is possible to restore the correct bracket sequence by the given data, print any possible choice.If Arthur got something wrong, and there are no sequences corresponding to the given information, print a single line ""IMPOSSIBLE"" (without the quotes). | Input: 41 11 11 11 1 | Output: ()()()() | Hard | 2 | 945 | 457 | 257 | 5 | |
408 | B | 408B | B. Garland | 1,200 | implementation | Once little Vasya read an article in a magazine on how to make beautiful handmade garland from colored paper. Vasya immediately went to the store and bought n colored sheets of paper, the area of each sheet is 1 square meter.The garland must consist of exactly m pieces of colored paper of arbitrary area, each piece sho... | The first line contains a non-empty sequence of n (1 β€ n β€ 1000) small English letters (""a""...""z""). Each letter means that Vasya has a sheet of paper of the corresponding color.The second line contains a non-empty sequence of m (1 β€ m β€ 1000) small English letters that correspond to the colors of the pieces of pape... | Print an integer that is the maximum possible total area of the pieces of paper in the garland Vasya wants to get or -1, if it is impossible to make the garland from the sheets he's got. It is guaranteed that the answer is always an integer. | In the first test sample Vasya can make an garland of area 6: he can use both sheets of color b, three (but not four) sheets of color a and cut a single sheet of color c in three, for example, equal pieces. Vasya can use the resulting pieces to make a garland of area 6.In the second test sample Vasya cannot make a garl... | Input: aaabbacaabbccac | Output: 6 | Easy | 1 | 725 | 362 | 241 | 4 |
1,001 | D | 1001D | D. Distinguish plus state and minus state | 1,400 | *special | You are given a qubit which is guaranteed to be either in or in state. Your task is to perform necessary operations and measurements to figure out which state it was and to return 1 if it was a state or -1 if it was state. The state of the qubit after the operations does not matter. | You have to implement an operation which takes a qubit as an input and returns an integer. Your code should have the following signature:namespace Solution { open Microsoft.Quantum.Primitive; open Microsoft.Quantum.Canon; operation Solve (q : Qubit) : Int { body { // your code here } }} | Easy | 1 | 283 | 287 | 0 | 10 | |||
263 | A | 263A | A. Beautiful Matrix | 800 | implementation | You've got a 5 Γ 5 matrix, consisting of 24 zeroes and a single number one. Let's index the matrix rows by numbers from 1 to 5 from top to bottom, let's index the matrix columns by numbers from 1 to 5 from left to right. In one move, you are allowed to apply one of the two following transformations to the matrix: Swap ... | The input consists of five lines, each line contains five integers: the j-th integer in the i-th line of the input represents the element of the matrix that is located on the intersection of the i-th row and the j-th column. It is guaranteed that the matrix consists of 24 zeroes and a single number one. | Print a single integer β the minimum number of moves needed to make the matrix beautiful. | Input: 0 0 0 0 00 0 0 0 10 0 0 0 00 0 0 0 00 0 0 0 0 | Output: 3 | Beginner | 1 | 787 | 304 | 89 | 2 | |
1,452 | G | 1452G | G. Game On Tree | 2,700 | data structures; dfs and similar; greedy; trees | Alice and Bob are playing a game. They have a tree consisting of \(n\) vertices. Initially, Bob has \(k\) chips, the \(i\)-th chip is located in the vertex \(a_i\) (all these vertices are unique). Before the game starts, Alice will place a chip into one of the vertices of the tree.The game consists of turns. Each turn,... | The first line contains one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the number of vertices in the tree.Then \(n - 1\) lines follow, each line contains two integers \(u_i\), \(v_i\) (\(1 \le u_i, v_i \le n\); \(u_i \ne v_i\)) that denote the endpoints of an edge. These edges form a tree.The next line contains one... | Print \(n\) integers. The \(i\)-th of them should be equal to the number of turns the game will last if Alice initially places her chip in the vertex \(i\). If one of Bob's chips is already placed in vertex \(i\), then the answer for vertex \(i\) is \(0\). | Input: 5 2 4 3 1 3 4 3 5 2 4 5 | Output: 2 1 2 0 0 | Master | 4 | 1,143 | 568 | 256 | 14 | |
597 | A | 597A | A. Divisibility | 1,600 | math | Find the number of k-divisible numbers on the segment [a, b]. In other words you need to find the number of such integer values x that a β€ x β€ b and x is divisible by k. | The only line contains three space-separated integers k, a and b (1 β€ k β€ 1018; - 1018 β€ a β€ b β€ 1018). | Print the required number. | Input: 1 1 10 | Output: 10 | Medium | 1 | 169 | 103 | 26 | 5 | |
120 | D | 120D | D. Three Sons | 1,400 | brute force | Three sons inherited from their father a rectangular corn fiend divided into n Γ m squares. For each square we know how many tons of corn grows on it. The father, an old farmer did not love all three sons equally, which is why he bequeathed to divide his field into three parts containing A, B and C tons of corn.The fie... | The first line contains space-separated integers n and m β the sizes of the original (1 β€ n, m β€ 50, max(n, m) β₯ 3). Then the field's description follows: n lines, each containing m space-separated integers cij, (0 β€ cij β€ 100) β the number of tons of corn each square contains. The last line contains space-separated in... | Print the answer to the problem: the number of ways to divide the father's field so that one of the resulting parts contained A tons of corn, another one contained B tons, and the remaining one contained C tons. If no such way exists, print 0. | The lines dividing the field can be horizontal or vertical, but they should be parallel to each other. | Input: 3 31 1 11 1 11 1 13 3 3 | Output: 2 | Easy | 1 | 820 | 355 | 243 | 1 |
1,379 | D | 1379D | D. New Passenger Trams | 2,300 | binary search; brute force; data structures; sortings; two pointers | There are many freight trains departing from Kirnes planet every day. One day on that planet consists of \(h\) hours, and each hour consists of \(m\) minutes, where \(m\) is an even number. Currently, there are \(n\) freight trains, and they depart every day at the same time: \(i\)-th train departs at \(h_i\) hours and... | The first line of input contains four integers \(n\), \(h\), \(m\), \(k\) (\(1 \le n \le 100\,000\), \(1 \le h \le 10^9\), \(2 \le m \le 10^9\), \(m\) is even, \(1 \le k \le {m \over 2}\)) β the number of freight trains per day, the number of hours and minutes on the planet, and the boarding time for each passenger tra... | The first line of output should contain two integers: the minimum number of trains that need to be canceled, and the optimal starting time \(t\). Second line of output should contain freight trains that need to be canceled. | In the first test case of the example the first tram can depart at 0 hours and 0 minutes. Then the freight train at 16 hours and 0 minutes can depart at the same time as the passenger tram, and the freight train at 17 hours and 15 minutes can depart at the same time as the boarding starts for the upcoming passenger tra... | Input: 2 24 60 15 16 0 17 15 | Output: 0 0 | Expert | 5 | 2,003 | 540 | 223 | 13 |
2,061 | F2 | 2061F2 | F2. Kevin and Binary String (Hard Version) | 3,500 | data structures; dp | This is the hard version of the problem. The difference between the versions is that in this version, string \(t\) consists of '0', '1' and '?'. You can hack only if you solved all versions of this problem. Kevin has a binary string \(s\) of length \(n\). Kevin can perform the following operation: Choose two adjacent b... | 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 string \(s\) consisting of '0' and '1'.The second line of each test case contains a string \(t\) consisting of '0'... | For each test case, output one integer β the minimum number of operations required. If it is impossible, output \(-1\). | In the first test case of the first example, the possible way is shown in the statement.In the second test case of the first example, one possible way could be: Swap blocks \([2, 2], [3, 3]\), \(s\) will become \(\mathtt{001101}\). Swap blocks \([3, 4], [5, 5]\), \(s\) will become \(\mathtt{000111}\). Swap blocks \([1,... | Input: 600011001110000011111010101111000010101100101101001100100111001 | Output: 1 3 1 -1 -1 -1 | Master | 2 | 1,455 | 505 | 119 | 20 |
1,763 | A | 1763A | A. Absolute Maximization | 800 | bitmasks; constructive algorithms; greedy; math | You are given an array \(a\) of length \(n\). You can perform the following operation several (possibly, zero) times: Choose \(i\), \(j\), \(b\): Swap the \(b\)-th digit in the binary representation of \(a_i\) and \(a_j\). Find the maximum possible value of \(\max(a) - \min(a)\).In a binary representation, bits are num... | The first line contains a single integer \(t\) (\(1 \le t \le 128\)) β the number of testcases.The first line of each test case contains a single integer \(n\) (\(3 \le n \le 512\)) β the length of array \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i < 1024\)) β t... | For each testcase, print one integer β the maximum possible value of \(\max(a) - \min(a)\). | In the first example, it can be shown that we do not need to perform any operations β the maximum value of \(\max(a) - \min(a)\) is \(1 - 0 = 1\).In the second example, no operation can change the array β the maximum value of \(\max(a) - \min(a)\) is \(5 - 5 = 0\).In the third example, initially \(a = [1, 2, 3, 4, 5]\)... | Input: 431 0 145 5 5 551 2 3 4 5720 85 100 41 76 49 36 | Output: 1 0 7 125 | Beginner | 4 | 976 | 428 | 91 | 17 |
793 | A | 793A | A. Oleg and shares | 900 | implementation; math | Oleg the bank client checks share prices every day. There are n share prices he is interested in. Today he observed that each second exactly one of these prices decreases by k rubles (note that each second exactly one price changes, but at different seconds different prices can change). Prices can become negative. Oleg... | The first line contains two integers n and k (1 β€ n β€ 105, 1 β€ k β€ 109) β the number of share prices, and the amount of rubles some price decreases each second.The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109) β the initial prices. | Print the only line containing the minimum number of seconds needed for prices to become equal, of Β«-1Β» if it is impossible. | Consider the first example. Suppose the third price decreases in the first second and become equal 12 rubles, then the first price decreases and becomes equal 9 rubles, and in the third second the third price decreases again and becomes equal 9 rubles. In this case all prices become equal 9 rubles in 3 seconds.There co... | Input: 3 312 9 15 | Output: 3 | Beginner | 2 | 571 | 248 | 124 | 7 |
1,991 | D | 1991D | D. Prime XOR Coloring | 1,900 | bitmasks; constructive algorithms; graphs; greedy; math; number theory | You are given an undirected graph with \(n\) vertices, numbered from \(1\) to \(n\). There is an edge between vertices \(u\) and \(v\) if and only if \(u \oplus v\) is a prime number, where \(\oplus\) denotes the bitwise XOR operator.Color all vertices of the graph using the minimum number of colors, such that no two v... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 500\)). The description of test cases follows.The only line contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of vertices in the graph.It is guaranteed that the sum of \(n\) over all t... | For each test case, output two lines.The first line should contain a single integer \(k\) (\(1 \le k \le n\)) β the minimum number of colors required.The second line should contain \(n\) integers \(c_1, c_2, \ldots, c_n\) (\(1 \le c_i \le k\)) β the color of each vertex.If there are multiple solutions, output any of th... | In the first test case, the minimum number of colors is \(1\), because there is only one vertex.In the second test case, the minimum number of colors is \(2\), because there is an edge connecting \(1\) and \(2\) (\(1 \oplus 2 = 3\), which is a prime number).In the third test case, the minimum number of colors is still ... | Input: 6123456 | Output: 1 1 2 1 2 2 1 2 2 3 1 2 2 3 3 1 2 2 3 3 4 1 2 2 3 3 4 | Hard | 6 | 378 | 363 | 323 | 19 |
570 | E | 570E | E. Pig and Palindromes | 2,300 | combinatorics; dp | Peppa the Pig was walking and walked into the forest. What a strange coincidence! The forest has the shape of a rectangle, consisting of n rows and m columns. We enumerate the rows of the rectangle from top to bottom with numbers from 1 to n, and the columns β from left to right with numbers from 1 to m. Let's denote t... | The first line contains two integers n, m (1 β€ n, m β€ 500) β the height and width of the field.Each of the following n lines contains m lowercase English letters identifying the types of cells of the forest. Identical cells are represented by identical letters, different cells are represented by different letters. | Print a single integer β the number of beautiful paths modulo 109 + 7. | Picture illustrating possibilities for the sample test. | Input: 3 4aaabbaaaabba | Output: 3 | Expert | 2 | 1,350 | 315 | 70 | 5 |
1,039 | E | 1039E | E. Summer Oenothera Exhibition | 3,400 | data structures | While some people enjoy spending their time solving programming contests, Dina prefers taking beautiful pictures. As soon as Byteland Botanical Garden announced Summer Oenothera Exhibition she decided to test her new camera there.The exhibition consists of \(l = 10^{100}\) Oenothera species arranged in a row and consec... | The first line contains three positive integer \(n\), \(w\), \(q\) (\(1 \leq n, q \leq 100\,000\), \(1 \leq w \leq 10^9\)) β the number of taken photos, the number of flowers on a single photo and the number of queries.Next line contains \(n\) non-negative integers \(x_i\) (\(0 \le x_i \le 10^9\)) β the indices of the ... | Print \(q\) integers β for each width of the truncated photo \(k_i\), the minimum number of cuts that is possible. | Input: 3 6 52 4 01 2 3 4 5 | Output: 00112 | Master | 1 | 1,898 | 540 | 114 | 10 | |
1,559 | C | 1559C | C. Mocha and Hiking | 1,200 | constructive algorithms; graphs | The city where Mocha lives in is called Zhijiang. There are \(n+1\) villages and \(2n-1\) directed roads in this city. There are two kinds of roads: \(n-1\) roads are from village \(i\) to village \(i+1\), for all \(1\leq i \leq n-1\). \(n\) roads can be described by a sequence \(a_1,\ldots,a_n\). If \(a_i=0\), the \(i... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 20\)) β the number of test cases. Each test case consists of two lines.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^4\)) β indicates that the number of villages is \(n+1\).The seco... | For each test case, print a line with \(n+1\) integers, where the \(i\)-th number is the \(i\)-th village they will go through. If the answer doesn't exist, print \(-1\).If there are multiple correct answers, you can print any one of them. | In the first test case, the city looks like the following graph:So all possible answers are \((1 \to 4 \to 2 \to 3)\), \((1 \to 2 \to 3 \to 4)\).In the second test case, the city looks like the following graph:So all possible answers are \((4 \to 1 \to 2 \to 3)\), \((1 \to 2 \to 3 \to 4)\), \((3 \to 4 \to 1 \to 2)\), \... | Input: 2 3 0 1 0 3 1 1 0 | Output: 1 4 2 3 4 1 2 3 | Easy | 2 | 681 | 668 | 239 | 15 |
1,111 | D | 1111D | D. Destroy the Colony | 2,600 | combinatorics; dp; math | There is a colony of villains with several holes aligned in a row, where each hole contains exactly one villain.Each colony arrangement can be expressed as a string of even length, where the \(i\)-th character of the string represents the type of villain in the \(i\)-th hole. Iron Man can destroy a colony only if the c... | The first line contains a string \(s\) (\(2 \le |s| \le 10^{5}\)), representing the initial colony arrangement. String \(s\) can have both lowercase and uppercase English letters and its length is even.The second line contains a single integer \(q\) (\(1 \le q \le 10^{5}\)) β the number of questions.The \(i\)-th of the... | For each question output the number of arrangements possible modulo \(10^9+7\). | Consider the first example. For the first question, the possible arrangements are ""aabb"" and ""bbaa"", and for the second question, index \(1\) contains 'a' and index \(2\) contains 'b' and there is no valid arrangement in which all 'a' and 'b' are in the same half. | Input: abba 2 1 4 1 2 | Output: 2 0 | Expert | 3 | 1,202 | 490 | 79 | 11 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.