contest_id int32 1 2.13k | index stringclasses 62
values | problem_id stringlengths 2 6 | title stringlengths 0 67 | rating int32 0 3.5k | tags stringlengths 0 139 | statement stringlengths 0 6.96k | input_spec stringlengths 0 2.32k | output_spec stringlengths 0 1.52k | note stringlengths 0 5.06k | sample_tests stringlengths 0 1.02k | difficulty_category stringclasses 6
values | tag_count int8 0 11 | statement_length int32 0 6.96k | input_spec_length int16 0 2.32k | output_spec_length int16 0 1.52k | contest_year int16 0 21 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1,493 | C | 1493C | C. K-beautiful Strings | 2,000 | binary search; brute force; constructive algorithms; greedy; strings | You are given a string \(s\) consisting of lowercase English letters and a number \(k\). Let's call a string consisting of lowercase English letters beautiful if the number of occurrences of each letter in that string is divisible by \(k\). You are asked to find the lexicographically smallest beautiful string of length... | The first line contains a single integer \(T\) (\(1 \le T \le 10\,000\)) — the number of test cases.The next \(2 \cdot T\) lines contain the description of test cases. The description of each test case consists of two lines.The first line of the description contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 10^... | For each test case output in a separate line lexicographically smallest beautiful string of length \(n\), which is greater or equal to string \(s\), or \(-1\) if such a string does not exist. | In the first test case ""acac"" is greater than or equal to \(s\), and each letter appears \(2\) or \(0\) times in it, so it is beautiful.In the second test case each letter appears \(0\) or \(1\) times in \(s\), so \(s\) itself is the answer.We can show that there is no suitable string in the third test case.In the fo... | Input: 4 4 2 abcd 3 1 abc 4 3 aaaa 9 3 abaabaaaa | Output: acac abc -1 abaabaaab | Hard | 5 | 746 | 546 | 191 | 14 |
1,214 | B | 1214B | B. Badges | 1,100 | brute force; math | There are \(b\) boys and \(g\) girls participating in Olympiad of Metropolises. There will be a board games tournament in the evening and \(n\) participants have accepted the invitation. The organizers do not know how many boys and girls are among them.Organizers are preparing red badges for girls and blue ones for boy... | The first line contains an integer \(b\) (\(1 \le b \le 300\)), the number of boys. The second line contains an integer \(g\) (\(1 \le g \le 300\)), the number of girls. The third line contains an integer \(n\) (\(1 \le n \le b + g\)), the number of the board games tournament participants. | Output the only integer, the minimum number of badge decks that Vasya could take. | In the first example, each of 4 decks should be taken: (0 blue, 3 red), (1 blue, 2 red), (2 blue, 1 red), (3 blue, 0 red).In the second example, 4 decks should be taken: (2 blue, 3 red), (3 blue, 2 red), (4 blue, 1 red), (5 blue, 0 red). Piles (0 blue, 5 red) and (1 blue, 4 red) can not be used. | Input: 5 6 3 | Output: 4 | Easy | 2 | 742 | 290 | 81 | 12 |
19 | B | 19B | B. Checkout Assistant | 1,900 | dp | Bob came to a cash & carry store, put n items into his trolley, and went to the checkout counter to pay. Each item is described by its price ci and time ti in seconds that a checkout assistant spends on this item. While the checkout assistant is occupied with some item, Bob can steal some other items from his trolley. ... | The first input line contains number n (1 ≤ n ≤ 2000). In each of the following n lines each item is described by a pair of numbers ti, ci (0 ≤ ti ≤ 2000, 1 ≤ ci ≤ 109). If ti is 0, Bob won't be able to steal anything, while the checkout assistant is occupied with item i. | Output one number — answer to the problem: what is the minimum amount of money that Bob will have to pay. | Input: 42 100 201 51 3 | Output: 8 | Hard | 1 | 550 | 272 | 105 | 0 | |
404 | B | 404B | B. Marathon | 1,500 | implementation; math | Valera takes part in the Berland Marathon. The marathon race starts at the stadium that can be represented on the plane as a square whose lower left corner is located at point with coordinates (0, 0) and the length of the side equals a meters. The sides of the square are parallel to coordinate axes.As the length of the... | The first line contains two space-separated real numbers a and d (1 ≤ a, d ≤ 105), given with precision till 4 decimal digits after the decimal point. Number a denotes the length of the square's side that describes the stadium. Number d shows that after each d meters Valera gets an extra drink.The second line contains ... | Print n lines, each line should contain two real numbers xi and yi, separated by a space. Numbers xi and yi in the i-th line mean that Valera is at point with coordinates (xi, yi) after he covers i·d meters. Your solution will be considered correct if the absolute or relative error doesn't exceed 10 - 4.Note, that this... | Input: 2 52 | Output: 1.0000000000 2.00000000002.0000000000 0.0000000000 | Medium | 2 | 891 | 393 | 420 | 4 | |
78 | C | 78C | C. Beaver Game | 2,000 | dp; games; number theory | Two beavers, Timur and Marsel, play the following game.There are n logs, each of exactly m meters in length. The beavers move in turns. For each move a beaver chooses a log and gnaws it into some number (more than one) of equal parts, the length of each one is expressed by an integer and is no less than k meters. Each ... | The first line contains three integers n, m, k (1 ≤ n, m, k ≤ 109). | Print ""Timur"", if Timur wins, or ""Marsel"", if Marsel wins. You should print everything without the quotes. | In the first sample the beavers only have one log, of 15 meters in length. Timur moves first. The only move he can do is to split the log into 3 parts each 5 meters in length. Then Marsel moves but he can't split any of the resulting logs, as k = 4. Thus, the winner is Timur.In the second example the beavers have 4 log... | Input: 1 15 4 | Output: Timur | Hard | 3 | 549 | 67 | 110 | 0 |
213 | B | 213B | B. Numbers | 1,900 | combinatorics; dp | Furik loves writing all sorts of problems, especially such that he can't solve himself. You've got one of his problems, the one Furik gave to Rubik. And Rubik asks you to solve it.There is integer n and array a, consisting of ten integers, indexed by numbers from 0 to 9. Your task is to count the number of positive int... | The first line contains integer n (1 ≤ n ≤ 100). The next line contains 10 integers a[0], a[1], ..., a[9] (0 ≤ a[i] ≤ 100) — elements of array a. The numbers are separated by spaces. | On a single line print the remainder of dividing the answer to the problem by 1000000007 (109 + 7). | In the first sample number 9 meets the requirements.In the second sample number 10 meets the requirements.In the third sample numbers 10, 110, 210, 120, 103 meet the requirements. There are other suitable numbers, 36 in total. | Input: 10 0 0 0 0 0 0 0 0 1 | Output: 1 | Hard | 2 | 497 | 182 | 99 | 2 |
418 | E | 418E | E. Tricky Password | 3,100 | data structures | In order to ensure confidentiality, the access to the ""Russian Code Cup"" problems is password protected during the problem development process.To select a password, the jury can generate a special table that contains n columns and the infinite number of rows. To construct a table, the first row is fixed, and all the ... | The first line contains an integer n (1 ≤ n ≤ 100000) — the number of columns. The second line contains the description of the first row of the table, that is, n integers, which are not less than 1 and do not exceed 109.The third line of the input contains an integer m (1 ≤ m ≤ 100000) — the number of requests.Next, ea... | Print an answer for each request of the second type in the order you receive them. | Input: 61 2 2 2 3 132 2 31 3 32 3 4 | Output: 21 | Master | 1 | 841 | 736 | 82 | 4 | |
1,517 | H | 1517H | H. Fly Around the World | 3,500 | dp; geometry | After hearing the story of Dr. Zhang, Wowo decides to plan his own flight around the world. He already chose \(n\) checkpoints in the world map. Due to the landform and the clouds, he cannot fly too high or too low. Formally, let \(b_i\) be the height of Wowo's aircraft at checkpoint \(i\), \(x_i^-\le b_i\le x_i^+\) sh... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 66\,666\)). Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(3 \le n \le 100\,000\)).The \(i\)-th of the next \(n\) lines contains two integers \(x_i^-\)... | For each test case, output YES if a sequence \(b_1,\ldots, b_n\) satisfying the constraints exists and NO otherwise. The sequence \(b_1,\ldots, b_n\) is not required. | In the first test case, all \(b_i\)'s are in \([0,1]\). Because of the constraints \(1=y_2^-\le b_2-b_1\le y_2^+=1\), \(b_2-b_1\) must be \(1\). So \(b_2=1\) and \(b_1=0\) must hold. Then by \(1=y_3^-\le b_3-b_2\le y_3^+=1\), \(b_3\) equals \(2\). This contradicts the constraint of \(b_3\le 1\). So no solution exists.I... | Input: 4 3 0 1 0 1 0 1 1 1 1 1 -100 100 3 -967 541 -500 834 -724 669 -858 978 -964 962 -645 705 4 0 0 0 1 0 1 1 1 0 1 0 1 0 1 0 0 0 0 4 0 0 33 34 65 66 100 100 0 100 0 100 0 100 0 0 0 0 | Output: NO YES YES NO | Master | 2 | 1,252 | 1,107 | 166 | 15 |
14 | A | 14A | A. Letter | 800 | implementation | A boy Bob likes to draw. Not long ago he bought a rectangular graph (checked) sheet with n rows and m columns. Bob shaded some of the squares on the sheet. Having seen his masterpiece, he decided to share it with his elder brother, who lives in Flatland. Now Bob has to send his picture by post, but because of the world... | The first line of the input data contains numbers n and m (1 ≤ n, m ≤ 50), n — amount of lines, and m — amount of columns on Bob's sheet. The following n lines contain m characters each. Character «.» stands for a non-shaded square on the sheet, and «*» — for a shaded square. It is guaranteed that Bob has shaded at lea... | Output the required rectangle of the minimum cost. Study the output data in the sample tests to understand the output format better. | Input: 6 7.........***....*......***....*......***.. | Output: ****..****..*** | Beginner | 1 | 710 | 334 | 132 | 0 | |
2,122 | D | 2122D | D. Traffic Lights | 2,400 | brute force; data structures; divide and conquer; dp; graphs; greedy; shortest paths | You are given a simple undirected connected graph of \(n\) vertices and \(m\) edges.There is a token in vertex \(1\). We consider the initial time to be at \(0\) seconds. After \(t\) seconds, if the token is in vertex \(u\), you must do exactly one of the following: wait one second, move the token through the \((t \bmo... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)). The description of the test cases follows. The first line of each test case contains two integers \(n\), \(m\) (\(2 \leq n \leq 5000\), \(n - 1 \leq m \leq \frac{n(n - 1)}{2}\)) — the number of vertice... | For each test case, output a single line containing two integers — the minimum total time and the minimum waiting time that minimizes the total time, respectively. | In the first test case, an optimal strategy is to do the following: at time \(0\), wait one second, at time \(1\), move the token from vertex \(1\) to vertex \(5\), at time \(2\), wait one second, at time \(3\), move the token from vertex \(5\) to vertex \(6\). In the second test case, an optimal strategy is to do the ... | Input: 26 61 22 33 44 61 55 64 31 21 31 4 | Output: 4 2 3 0 | Expert | 7 | 733 | 725 | 163 | 21 |
1,929 | D | 1929D | D. Sasha and a Walk in the City | 1,900 | combinatorics; dp; math; trees | Sasha wants to take a walk with his girlfriend in the city. The city consists of \(n\) intersections, numbered from \(1\) to \(n\). Some of them are connected by roads, and from any intersection, there is exactly one simple path\(^{\dagger}\) to any other intersection. In other words, the intersections and the roads be... | Each test 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 of each test case contains a single integer \(n\) (\(2 \le n \leq 3 \cdot 10^5\)) — the number of intersections in the cit... | For each test case, output a single integer — the number of good sets of intersections modulo \(998\,244\,353\). | In the first test case, there are \(2^3 = 8\) sets of intersections. All of them are good, except for the set \(\{1, 2, 3\}\), because if intersections \(1, 2\), and \(3\) are dangerous, then the simple path \(1 - 2 - 3\) contains \(3\) dangerous intersections. Thus, there are \(7\) good sets.In the second test case, t... | Input: 431 33 243 42 33 151 23 45 12 341 22 33 4 | Output: 7 12 16 11 | Hard | 4 | 1,075 | 679 | 112 | 19 |
1,760 | C | 1760C | C. Advantage | 800 | data structures; implementation; sortings | There are \(n\) participants in a competition, participant \(i\) having a strength of \(s_i\). Every participant wonders how much of an advantage they have over the other best participant. In other words, each participant \(i\) wants to know the difference between \(s_i\) and \(s_j\), where \(j\) is the strongest parti... | The input consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 1000\)) — the number of test cases. The descriptions of the test cases follow.The first line of each test case contains an integer \(n\) (\(2 \leq n \leq 2\cdot10^5\)) — the length of the array.The following line contai... | For each test case, output \(n\) space-separated integers. For each \(i\) (\(1 \leq i \leq n\)) output the difference between \(s_i\) and the maximum strength of any other participant. | For the first test case: The first participant has a strength of \(4\) and the largest strength of a participant different from the first one is \(7\), so the answer for the first participant is \(4 - 7 = -3\). The second participant has a strength of \(7\) and the largest strength of a participant different from the s... | Input: 544 7 3 521 251 2 3 4 534 9 444 4 4 4 | Output: -3 2 -4 -2 -1 1 -4 -3 -2 -1 1 -5 5 -5 0 0 0 0 | Beginner | 3 | 577 | 547 | 184 | 17 |
699 | B | 699B | B. One Bomb | 1,400 | implementation | You are given a description of a depot. It is a rectangular checkered field of n × m size. Each cell in a field can be empty (""."") or it can be occupied by a wall (""*""). You have one bomb. If you lay the bomb at the cell (x, y), then after triggering it will wipe out all walls in the row x and all walls in the colu... | The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and columns in the depot field. The next n lines contain m symbols ""."" and ""*"" each — the description of the field. j-th symbol in i-th of them stands for cell (i, j). If the symbol is equal to ""."", then the corresponding... | If it is impossible to wipe out all walls by placing and triggering exactly one bomb, then print ""NO"" in the first line (without quotes).Otherwise print ""YES"" (without quotes) in the first line and two integers in the second line — the coordinates of the cell at which the bomb should be laid. If there are multiple ... | Input: 3 4.*.......*.. | Output: YES1 2 | Easy | 1 | 518 | 411 | 347 | 6 | |
842 | B | 842B | B. Gleb And Pizza | 1,100 | geometry | Gleb ordered pizza home. When the courier delivered the pizza, he was very upset, because several pieces of sausage lay on the crust, and he does not really like the crust.The pizza is a circle of radius r and center at the origin. Pizza consists of the main part — circle of radius r - d with center at the origin, and ... | First string contains two integer numbers r and d (0 ≤ d < r ≤ 500) — the radius of pizza and the width of crust.Next line contains one integer number n — the number of pieces of sausage (1 ≤ n ≤ 105).Each of next n lines contains three integer numbers xi, yi and ri ( - 500 ≤ xi, yi ≤ 500, 0 ≤ ri ≤ 500), where xi and y... | Output the number of pieces of sausage that lay on the crust. | Below is a picture explaining the first example. Circles of green color denote pieces of sausage lying on the crust. | Input: 8 477 8 1-7 3 20 2 10 -2 2-3 -3 10 6 25 3 1 | Output: 2 | Easy | 1 | 651 | 415 | 61 | 8 |
103 | C | 103C | C. Russian Roulette | 1,900 | constructive algorithms; greedy | After all the events in Orlando we all know, Sasha and Roma decided to find out who is still the team's biggest loser. Thankfully, Masha found somewhere a revolver with a rotating cylinder of n bullet slots able to contain exactly k bullets, now the boys have a chance to resolve the problem once and for all. Sasha sele... | The first line contains three integers n, k and p (1 ≤ n ≤ 1018, 0 ≤ k ≤ n, 1 ≤ p ≤ 1000) — the number of slots in the cylinder, the number of bullets and the number of queries. Then follow p lines; they are the queries. Each line contains one integer xi (1 ≤ xi ≤ n) the number of slot to describe.Please do not use the... | For each query print ""."" if the slot should be empty and ""X"" if the slot should be charged. | The lexicographical comparison of is performed by the < operator in modern programming languages. The a string is lexicographically less that the b string, if there exists such i (1 ≤ i ≤ n), that ai < bi, and for any j (1 ≤ j < i) aj = bj. | Input: 3 1 3123 | Output: ..X | Hard | 2 | 1,957 | 446 | 95 | 1 |
661 | G | 661G | G. Hungarian Notation | 2,200 | *special | In Hungarian notation, a variable name is prefixed with a letter or a group of letters which are mnemonics for the type of that variable. For the purposes of this problem we will consider only two data types: integer and real.You are given the meaningful part of variable name in lowercase and a sample value that it wil... | The first line of the input contains a string of lowercase letters of English alphabet. The length of the string will be between 1 and 10, inclusive.The second line of the input contains a string of digits and zero or one decimal point '.'. The length of the string will be between 1 and 11, inclusive. It's guaranteed t... | Output a single string — the name of the variable in Hungarian notation. | Input: count18 | Output: iCount | Hard | 1 | 797 | 391 | 72 | 6 | |
963 | A | 963A | A. Alternating Sum | 1,800 | math; number theory | You are given two integers \(a\) and \(b\). Moreover, you are given a sequence \(s_0, s_1, \dots, s_{n}\). All values in \(s\) are integers \(1\) or \(-1\). It's known that sequence is \(k\)-periodic and \(k\) divides \(n+1\). In other words, for each \(k \leq i \leq n\) it's satisfied that \(s_{i} = s_{i - k}\).Find o... | The first line contains four integers \(n, a, b\) and \(k\) \((1 \leq n \leq 10^{9}, 1 \leq a, b \leq 10^{9}, 1 \leq k \leq 10^{5})\).The second line contains a sequence of length \(k\) consisting of characters '+' and '-'. If the \(i\)-th character (0-indexed) is '+', then \(s_{i} = 1\), otherwise \(s_{i} = -1\).Note ... | Output a single integer — value of given expression modulo \(10^{9} + 9\). | In the first example:\((\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i})\) = \(2^{2} 3^{0} - 2^{1} 3^{1} + 2^{0} 3^{2}\) = 7In the second example:\((\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i}) = -1^{4} 5^{0} - 1^{3} 5^{1} - 1^{2} 5^{2} - 1^{1} 5^{3} - 1^{0} 5^{4} = -781 \equiv 999999228 \pmod{10^{9} + 9}\). | Input: 2 2 3 3+-+ | Output: 7 | Medium | 2 | 464 | 437 | 74 | 9 |
1,131 | E | 1131E | E. String Multiplication | 2,300 | dp; greedy; strings | Roman and Denis are on the trip to the programming competition. Since the trip was long, they soon got bored, and hence decided to came up with something. Roman invented a pizza's recipe, while Denis invented a string multiplication. According to Denis, the result of multiplication (product) of strings \(s\) of length ... | The first line contains a single integer \(n\) (\(2 \leq n \leq 100\,000\)) — the number of strings, wroted by Denis.Next \(n\) lines contain non-empty strings \(p_1, p_2, \ldots, p_n\), consisting of lowercase english letters.It's guaranteed, that the total length of the strings \(p_i\) is at most \(100\,000\), and th... | Print exactly one integer — the beauty of the product of the strings. | In the first example, the product of strings is equal to ""abaaaba"".In the second example, the product of strings is equal to ""abanana"". | Input: 3 a b a | Output: 3 | Expert | 3 | 1,798 | 381 | 69 | 11 |
2,110 | C | 2110C | C. Racing | 1,400 | constructive algorithms; greedy | In 2077, a sport called hobby-droning is gaining popularity among robots.You already have a drone, and you want to win. For this, your drone needs to fly through a course with \(n\) obstacles.The \(i\)-th obstacle is defined by two numbers \(l_i, r_i\). Let the height of your drone at the \(i\)-th obstacle be \(h_i\). ... | 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. In the first line of each test case, an integer \(n\) (\(1 \le n \le 2 \cdot 10^5)\) is given — the size of the array \(d\).In the second line of each test ca... | For each test case, output \(n\) integers \(d_1,d_2,\ldots,d_n\), if it is possible to correctly restore the array \(d\), or \(-1\) if it is not possible. | In the first test case, one possible answer is \(d=[0,1,1,1]\). The array \(h\) will be \([0,0+1,0+1+1,0+1+1+1]=[0,1,2,3]\). This array meets the conditions of the problem.In the second test case, it can be proven that there is no suitable array \(d\), so the answer is \(-1\). | Input: 540 -1 -1 10 41 22 41 430 -1 -10 12 20 32-1 -10 02 28-1 -1 1 -1 -1 0 0 -10 00 10 20 21 30 42 54 5101 1 | Output: 0 1 1 1 -1 -1 0 1 1 0 1 0 0 1 -1 | Easy | 2 | 948 | 711 | 154 | 21 |
1,067 | A | 1067A | A. Array Without Local Maximums | 1,900 | dp | Ivan unexpectedly saw a present from one of his previous birthdays. It is array of \(n\) numbers from \(1\) to \(200\). Array is old and some numbers are hard to read. Ivan remembers that for all elements at least one of its neighbours ls not less than it, more formally:\(a_{1} \le a_{2}\),\(a_{n} \le a_{n-1}\) and\(a_... | First line of input contains one integer \(n\) (\(2 \le n \le 10^{5}\)) — size of the array.Second line of input contains \(n\) integers \(a_{i}\) — elements of array. Either \(a_{i} = -1\) or \(1 \le a_{i} \le 200\). \(a_{i} = -1\) means that \(i\)-th element can't be read. | Print number of ways to restore the array modulo \(998244353\). | In the first example, only possible value of \(a_{2}\) is \(2\).In the second example, \(a_{1} = a_{2}\) so there are \(200\) different values because all restored elements should be integers between \(1\) and \(200\). | Input: 31 -1 2 | Output: 1 | Hard | 1 | 609 | 275 | 63 | 10 |
2,075 | C | 2075C | C. Two Colors | 1,500 | binary search; combinatorics; math | Monocarp has installed a new fence at his summer house. The fence consists of \(n\) planks of the same size arranged in a row.Monocarp decided that he would paint his fence according to the following rules: each plank of the fence will be painted in exactly one color; the number of different colors that the planks will... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.The first line of each test case contains two integers \(n\) and \(m\) (\(2 \le n, m \le 2 \cdot 10^5\)) — the number of planks in the fence and the number of different colors of paint that Monocarp has.The second line cont... | For each test case, output the number of different ways to paint the fence that satisfy all of Monocarp's described wishes. | In the first test case, there are \(4\) different ways to paint the fence (the sequences of color numbers in which the planks can be painted from left to right are listed below): \([1, 2, 2, 2, 2]\); \([1, 1, 2, 2, 2]\); \([2, 2, 2, 1, 1]\); \([2, 2, 2, 2, 1]\). In the second test case, there are \(6\) different ways t... | Input: 35 22 45 23 412 35 9 8 | Output: 4 6 22 | Medium | 3 | 1,000 | 626 | 123 | 20 |
774 | G | 774G | G. Perfectionist Arkadiy | 2,700 | *special; number theory | Arkadiy has lots square photos with size a × a. He wants to put some of them on a rectangular wall with size h × w. The photos which Arkadiy will put on the wall must form a rectangular grid and the distances between neighboring vertically and horizontally photos and also the distances between outside rows and columns ... | The first line contains three integers a, h and w (1 ≤ a, h, w ≤ 109) — the size of photos and the height and the width of the wall. | Print one non-negative real number — the minimum value of x which can be obtained after putting the photos on the wall. The absolute or the relative error of the answer must not exceed 10 - 6.Print -1 if there is no way to put positive number of photos and satisfy the constraints. | In the first example Arkadiy can put 7 rows of photos with 5 photos in each row, so the minimum value of x equals to 0.5.In the second example Arkadiy can put only 1 photo which will take the whole wall, so the minimum value of x equals to 0.In the third example there is no way to put positive number of photos and sati... | Input: 2 18 13 | Output: 0.5 | Master | 2 | 1,083 | 132 | 281 | 7 |
1,275 | E1 | 1275E1 | E1. Контрольная сумма | 0 | *special | Данные пользователей ВКонтакте хранятся на десятках тысяч серверов. Для того, чтобы можно было определять ошибки при записи данных на диск, на диск регулярно записываются текущие контрольные суммы CRC32 (Wiki, IEEE 802-3). Благодаря этому, при чтении данных можно заново вычислить контрольную сумму и проверить, что данн... | В первой строке дано два целых числа \(n\) и \(q\) — количество байт в файле и количество запросов, для которых нужно решить задачу (\(8 \le n \le 2 \cdot 10^5\); \(1 \le q \le 10^5\)).Во второй строке дано \(n\) чисел \(a_0, a_1, \ldots, a_{n-1}\) — содержимое файла в байтах (\(0 \le a_i \le 255\)).В следующих \(q\) с... | Для каждого запроса выведите четыре целых числа \(z_0, z_1, z_2, z_3\), на которые нужно заменить четыре байта с номерами \(j, j+1, j+2, j+3\), чтобы crc32 не изменился. Обратите внимание, что все запросы независимы, и на самом деле последовательность не изменяется.Если существует несколько решений, выведите любое, а е... | CRC32 байтовой последовательности из первого примера (1 2 3 4 5 6 7 8) равен 3fca88c5, CRC32 измененной последовательности (0 0 0 0 212 34 127 159) так же равен 3fca88c5. Стандартная утилита crc32 из большинства дистрибутивов линукса должна посчитать от них данную контрольную сумму.CRC32 последовательности из второго п... | Input: 8 1 1 2 3 4 5 6 7 8 0 4 0 0 0 0 | Output: 212 34 127 159 | Beginner | 1 | 1,641 | 673 | 388 | 12 |
1,371 | C | 1371C | C. A Cookie for You | 1,300 | greedy; implementation; math | Anna is a girl so brave that she is loved by everyone in the city and citizens love her cookies. She is planning to hold a party with cookies. Now she has \(a\) vanilla cookies and \(b\) chocolate cookies for the party.She invited \(n\) guests of the first type and \(m\) guests of the second type to the party. They wil... | The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) — the number of test cases. Next \(t\) lines contain descriptions of test cases.For each test case, the only line contains four integers \(a\), \(b\), \(n\), \(m\) (\(0 \le a,b,n,m \le 10^{18}, n+m \neq 0\))... | For each test case, print the answer in one line. If there exists at least one valid order, print ""Yes"". Otherwise, print ""No"".You can print each letter in any case (upper or lower). | In the first test case, let's consider the order \(\{1, 2, 2\}\) of types of guests. Then: The first guest eats a chocolate cookie. After that, there are \(2\) vanilla cookies and \(1\) chocolate cookie. The second guest eats a chocolate cookie. After that, there are \(2\) vanilla cookies and \(0\) chocolate cookies. T... | Input: 6 2 2 1 2 0 100 0 1 12 13 25 1 27 83 14 25 0 0 1 0 1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000 | Output: Yes No No Yes No Yes | Easy | 3 | 1,187 | 321 | 186 | 13 |
811 | D | 811D | D. Vladik and Favorite Game | 2,100 | constructive algorithms; dfs and similar; graphs; interactive | This is an interactive problem.Vladik has favorite game, in which he plays all his free time.Game field could be represented as n × m matrix which consists of cells of three types: «.» — normal cell, player can visit it. «F» — finish cell, player has to finish his way there to win. There is exactly one cell of this typ... | First line contains two space-separated integers n and m (1 ≤ n, m ≤ 100) — number of rows and columns respectively.Each of next n lines contains m characters describing corresponding row of field. Set of characters in field is described above.Guaranteed that cell with coordinates (1, 1) is normal and there is at least... | In first test case all four directions swapped with their opposite directions. Protocol of interaction In more convenient form:This test could be presenter for hack in following way: 4 3 1 1...**.F*.... | Input: 4 3...**.F*....1 11 21 31 32 33 34 34 24 13 1 | Output: RLLDUUURRD | Hard | 4 | 910 | 386 | 0 | 8 | |
2,125 | D | 2125D | D. Segments Covering | 1,600 | dp; math; probabilities | There is a linear strip divided into \(m\) cells, numbered from \(1\) to \(m\) from left to right.You are given \(n\) segments. Each segment is defined by four numbers: \(l\), \(r\), \(p\) and \(q\) — the segment covers cells from \(l\) to \(r\) inclusively and exists with probability \(\frac{p}{q}\) (independently).Yo... | The first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 2 \cdot 10^5\)).Then \(n\) lines follow. The \(i\)-th of them contains four integers \(l_i\), \(r_i\), \(p_i\) and \(q_i\) (\(1 \le l_i \le r_i \le m\); \(1 \le p_i < q_i < 998244353\)). | Print a single integer — the probability that each cell is covered by exactly one segment, taken modulo \(998244353\).Formally, the probability can be expressed as an irreducible fraction \(\frac{x}{y}\). You have to print the value of \(x \cdot y^{-1} \bmod 998244353\), where \(y^{-1}\) is an integer such that \(y \cd... | In the first example, the probability is equal to \(\frac{5}{18}\). | Input: 3 31 2 1 33 3 1 21 3 2 3 | Output: 610038216 | Medium | 3 | 409 | 257 | 352 | 21 |
49 | B | 49B | B. Sum | 1,500 | math | Vasya studies positional numeral systems. Unfortunately, he often forgets to write the base of notation in which the expression is written. Once he saw a note in his notebook saying a + b = ?, and that the base of the positional notation wasn’t written anywhere. Now Vasya has to choose a base p and regard the expressio... | The first letter contains two space-separated numbers a and b (1 ≤ a, b ≤ 1000) which represent the given summands. | Print a single number — the length of the longest answer. | Input: 78 87 | Output: 3 | Medium | 1 | 1,244 | 115 | 57 | 0 | |
2,103 | B | 2103B | B. Binary Typewriter | 1,100 | greedy; math | You are given a binary string \(s\) of length \(n\) and a typewriter with two buttons: 0 and 1. Initially, your finger is on the button 0. You can do the following two operations: Press the button your finger is currently on. This will type out the character that is on the button. Move your finger to the other button. ... | 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 length of the binary string \(s\).The second line of each t... | For each test case, output the minimum cost of string \(s\) after performing at most one substring reversal. | In the first test case, we can choose not to reverse any substrings. We can do operation \(1\) three times to type 000.In the second test case, we can choose not to reverse any substrings. We can do operation \(2\) to move our finger to button 1. Then, we do operation \(1\) three times to type 111.In the third test cas... | Input: 63000311130113100510101191101010010011011100 | Output: 3 4 4 4 8 29 | Easy | 2 | 1,145 | 558 | 108 | 21 |
1,202 | F | 1202F | F. You Are Given Some Letters... | 2,700 | binary search; implementation; math | You are given \(a\) uppercase Latin letters 'A' and \(b\) letters 'B'.The period of the string is the smallest such positive integer \(k\) that \(s_i = s_{i~mod~k}\) (\(0\)-indexed) for each \(i\). Note that this implies that \(k\) won't always divide \(a+b = |s|\).For example, the period of string ""ABAABAA"" is \(3\)... | The first line contains two integers \(a\) and \(b\) (\(1 \le a, b \le 10^9\)) — the number of letters 'A' and 'B', respectively. | Print the number of different periods over all possible strings with \(a\) letters 'A' and \(b\) letters 'B'. | All the possible periods for the first example: \(3\) ""BBABBA"" \(4\) ""BBAABB"" \(5\) ""BBBAAB"" \(6\) ""AABBBB"" All the possible periods for the second example: \(3\) ""BAABAABA"" \(5\) ""BAABABAA"" \(6\) ""BABAAABA"" \(7\) ""BAABAAAB"" \(8\) ""AAAAABBB"" Note that these are not the only possible strings for the gi... | Input: 2 4 | Output: 4 | Master | 3 | 500 | 129 | 109 | 12 |
231 | C | 231C | C. To Add or Not to Add | 1,600 | binary search; sortings; two pointers | A piece of paper contains an array of n integers a1, a2, ..., an. Your task is to find a number that occurs the maximum number of times in this array.However, before looking for such number, you are allowed to perform not more than k following operations — choose an arbitrary element from the array and add 1 to it. In ... | The first line contains two integers n and k (1 ≤ n ≤ 105; 0 ≤ k ≤ 109) — the number of elements in the array and the number of operations you are allowed to perform, correspondingly.The third line contains a sequence of n integers a1, a2, ..., an (|ai| ≤ 109) — the initial array. The numbers in the lines are separated... | In a single line print two numbers — the maximum number of occurrences of some number in the array after at most k allowed operations are performed, and the minimum number that reaches the given maximum. Separate the printed numbers by whitespaces. | In the first sample your task is to increase the second element of the array once and increase the fifth element of the array twice. Thus, we get sequence 6, 4, 4, 0, 4, where number 4 occurs 3 times.In the second sample you don't need to perform a single operation or increase each element by one. If we do nothing, we ... | Input: 5 36 3 4 0 2 | Output: 3 4 | Medium | 3 | 688 | 338 | 248 | 2 |
662 | A | 662A | A. Gambling Nim | 2,400 | bitmasks; math; matrices; probabilities | As you know, the game of ""Nim"" is played with n piles of stones, where the i-th pile initially contains ai stones. Two players alternate the turns. During a turn a player picks any non-empty pile and removes any positive number of stones from it. The one who is not able to make a move loses the game.Petya and Vasya a... | The first line of the input contains a single integer n (1 ≤ n ≤ 500 000) — the number of cards in the deck.Each of the following n lines contains the description of one card, consisting of two integers ai and bi (0 ≤ ai, bi ≤ 1018). | Output the answer as an irreducible fraction p / q. If the probability of Petya's victory is 0, print 0/1. | Input: 21 11 1 | Output: 0/1 | Expert | 4 | 1,030 | 233 | 106 | 6 | |
1,207 | C | 1207C | C. Gas Pipeline | 1,500 | dp; greedy | You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment \([0, n]\) on \(OX\) axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval \((x, x + 1)\) with integer \(x\). So we can represent the road as a binar... | The fist line contains one integer \(T\) (\(1 \le T \le 100\)) — the number of queries. Next \(2 \cdot T\) lines contain independent queries — one query per two lines.The first line contains three integers \(n\), \(a\), \(b\) (\(2 \le n \le 2 \cdot 10^5\), \(1 \le a \le 10^8\), \(1 \le b \le 10^8\)) — the length of the... | Print \(T\) integers — one per query. For each query print the minimum possible cost of the constructed pipeline. | The optimal pipeline for the first query is shown at the picture above.The optimal pipeline for the second query is pictured below: The optimal (and the only possible) pipeline for the third query is shown below: The optimal pipeline for the fourth query is shown below: | Input: 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 | Output: 94 25 2900000000 13 | Medium | 2 | 1,580 | 638 | 113 | 12 |
1,336 | C | 1336C | C. Kaavi and Magic Spell | 2,200 | dp; strings | Kaavi, the mysterious fortune teller, deeply believes that one's fate is inevitable and unavoidable. Of course, she makes her living by predicting others' future. While doing divination, Kaavi believes that magic spells can provide great power for her to see the future. Kaavi has a string \(T\) of length \(m\) and all ... | The first line contains a string \(S\) of length \(n\) (\(1 \leq n \leq 3000\)).The second line contains a string \(T\) of length \(m\) (\(1 \leq m \leq n\)).Both strings contain only lowercase Latin letters. | The output contains only one integer — the answer modulo \(998\,244\,353\). | The first test:The red ones are the magic spells. In the first operation, Kaavi can either add the first character ""a"" at the front or the back of \(A\), although the results are the same, they are considered as different operations. So the answer is \(6\times2=12\). | Input: abab ba | Output: 12 | Hard | 2 | 1,318 | 208 | 75 | 13 |
1,194 | B | 1194B | B. Yet Another Crosses Problem | 1,300 | implementation | You are given a picture consisting of \(n\) rows and \(m\) columns. Rows are numbered from \(1\) to \(n\) from the top to the bottom, columns are numbered from \(1\) to \(m\) from the left to the right. Each cell is painted either black or white. You think that this picture is not interesting enough. You consider a pic... | The first line contains an integer \(q\) (\(1 \le q \le 5 \cdot 10^4\)) — the number of queries.The first line of each query contains two integers \(n\) and \(m\) (\(1 \le n, m \le 5 \cdot 10^4\), \(n \cdot m \le 4 \cdot 10^5\)) — the number of rows and the number of columns in the picture.Each of the next \(n\) lines ... | Print \(q\) lines, the \(i\)-th line should contain a single integer — the answer to the \(i\)-th query, which is the minimum number of minutes you have to spend so the resulting picture contains at least one cross. | The example contains all the pictures from above in the same order.The first 5 pictures already contain a cross, thus you don't have to paint anything.You can paint \((1, 3)\), \((3, 1)\), \((5, 3)\) and \((3, 5)\) on the \(6\)-th picture to get a cross in \((3, 3)\). That'll take you \(4\) minutes.You can paint \((1, ... | Input: 9 5 5 ..*.. ..*.. ***** ..*.. ..*.. 3 4 **** .*.. .*.. 4 3 *** *.. *.. *.. 5 5 ***** *.*.* ***** ..*.* ..*** 1 4 **** 5 5 ..... ..*.. .***. ..*.. ..... 5 3 ... .*. .*. *** .*. 3 3 .*. *.* .*. 4 4 *.** .... *.** *.** | Output: 0 0 0 0 0 4 1 1 2 | Easy | 1 | 1,067 | 508 | 215 | 11 |
1,301 | E | 1301E | E. Nanosoft | 2,500 | binary search; data structures; dp; implementation | Warawreh created a great company called Nanosoft. The only thing that Warawreh still has to do is to place a large picture containing its logo on top of the company's building.The logo of Nanosoft can be described as four squares of the same size merged together into one large square. The top left square is colored wit... | The first line of input contains three integers \(n\), \(m\) and \(q\) \((1 \leq n , m \leq 500, 1 \leq q \leq 3 \cdot 10^{5})\) — the number of row, the number columns and the number of options.For the next \(n\) lines, every line will contain \(m\) characters. In the \(i\)-th line the \(j\)-th character will contain ... | For every option print the maximum area of sub-square inside the given sub-rectangle, which can be a NanoSoft Logo. If there are no such sub-squares, print \(0\). | Picture for the first test:The pictures from the left to the right corresponds to the options. The border of the sub-rectangle in the option is marked with black, the border of the sub-square with the maximal possible size, that can be cut is marked with gray. | Input: 5 5 5 RRGGB RRGGY YYBBG YYBBR RBBRG 1 1 5 5 2 2 5 5 2 2 3 3 1 1 3 5 4 4 5 5 | Output: 16 4 4 4 0 | Expert | 4 | 1,328 | 819 | 162 | 13 |
2,011 | G | 2011G | G. Removal of a Permutation | 0 | *special | You are given a permutation \(p\) of length \(n\).You can perform operations of two types: mark all positions \(i\) such that \(1 \le i < n\) and \(p_i < p_{i + 1}\), and simultaneously remove the elements at these positions; mark all positions \(i\) such that \(2 \le i \le n\) and \(p_{i - 1} > p_i\), and simultaneous... | 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\) (\(2 \le n \le 250\,000\)).The second line of each test case contains \(n\) integers \(p_1, p_2, \dots, p_n\) (\(1 \le p_i \le n\)). The array \(p\) is a perm... | For each test case, print \((n-1)\) integers. The \(i\)-th of them should be equal to the minimum number of operations required to remove \(i\) from the permutation. | Input: 544 2 1 321 265 4 1 3 2 675 6 1 3 7 2 453 1 2 5 4 | Output: 1 1 2 1 1 1 2 1 3 1 1 1 2 1 2 1 1 2 1 | Beginner | 1 | 501 | 431 | 165 | 20 | |
688 | B | 688B | B. Lovely Palindromes | 1,000 | constructive algorithms; math | Pari has a friend who loves palindrome numbers. A palindrome number is a number that reads the same forward or backward. For example 12321, 100001 and 1 are palindrome numbers, while 112 and 1021 are not.Pari is trying to love them too, but only very special and gifted people can understand the beauty behind palindrome... | The only line of the input contains a single integer n (1 ≤ n ≤ 10100 000). | Print the n-th even-length palindrome number. | The first 10 even-length palindrome numbers are 11, 22, 33, ... , 88, 99 and 1001. | Input: 1 | Output: 11 | Beginner | 2 | 708 | 75 | 45 | 6 |
1,368 | H2 | 1368H2 | H2. Breadboard Capacity (hard version) | 3,500 | This is a harder version of the problem H with modification queries.Lester and Delbert work at an electronics company. They are currently working on a microchip component serving to connect two independent parts of a large supercomputer.The component is built on top of a breadboard — a grid-like base for a microchip. T... | The first line contains three integers \(n, m, q\) (\(1 \leq n, m \leq 10^5\), \(0 \leq q \leq 10^5\)) — the number of rows and columns of the breadboard, and the number of modifications respectively.The next four lines describe initial coloring of the ports. Each character in these lines is either R or B, depending on... | Print \(q + 1\) integers, one per line — the breadboard capacity after \(0, \ldots, q\) modifications have been made to the initial coloring. | Input: 4 5 4 BBRR RBBR BBBBB RRRRR L 2 3 R 3 4 U 1 5 D 1 5 | Output: 7 7 9 4 9 | Master | 0 | 2,115 | 1,164 | 141 | 13 | ||
325 | A | 325A | A. Square and Rectangles | 1,500 | implementation | You are given n rectangles. The corners of rectangles have integer coordinates and their edges are parallel to the Ox and Oy axes. The rectangles may touch each other, but they do not overlap (that is, there are no points that belong to the interior of more than one rectangle). Your task is to determine if the rectangl... | The first line contains a single integer n (1 ≤ n ≤ 5). Next n lines contain four integers each, describing a single rectangle: x1, y1, x2, y2 (0 ≤ x1 < x2 ≤ 31400, 0 ≤ y1 < y2 ≤ 31400) — x1 and x2 are x-coordinates of the left and right edges of the rectangle, and y1 and y2 are y-coordinates of the bottom and top edge... | In a single line print ""YES"", if the given rectangles form a square, or ""NO"" otherwise. | Input: 50 0 2 30 3 3 52 0 5 23 2 5 52 2 3 3 | Output: YES | Medium | 1 | 514 | 452 | 91 | 3 | |
931 | A | 931A | A. Friends Meeting | 800 | brute force; greedy; implementation; math | Two friends are on the coordinate axis Ox in points with integer coordinates. One of them is in the point x1 = a, another one is in the point x2 = b. Each of the friends can move by one along the line in any direction unlimited number of times. When a friend moves, the tiredness of a friend changes according to the fol... | The first line contains a single integer a (1 ≤ a ≤ 1000) — the initial position of the first friend. The second line contains a single integer b (1 ≤ b ≤ 1000) — the initial position of the second friend.It is guaranteed that a ≠ b. | Print the minimum possible total tiredness if the friends meet in the same point. | In the first example the first friend should move by one to the right (then the meeting happens at point 4), or the second friend should move by one to the left (then the meeting happens at point 3). In both cases, the total tiredness becomes 1.In the second example the first friend should move by one to the left, and ... | Input: 34 | Output: 1 | Beginner | 4 | 756 | 233 | 81 | 9 |
1,143 | A | 1143A | A. The Doors | 800 | implementation | Three years have passes and nothing changed. It is still raining in London, and Mr. Black has to close all the doors in his home in order to not be flooded. Once, however, Mr. Black became so nervous that he opened one door, then another, then one more and so on until he opened all the doors in his house.There are exac... | The first line contains integer \(n\) (\(2 \le n \le 200\,000\)) — the number of doors.The next line contains \(n\) integers: the sequence in which Mr. Black opened the doors. The \(i\)-th of these integers is equal to \(0\) in case the \(i\)-th opened door is located in the left exit, and it is equal to \(1\) in case ... | Print the smallest integer \(k\) such that after Mr. Black opened the first \(k\) doors, he was able to exit the house. | In the first example the first two doors are from the left exit, so when Mr. Black opened both of them only, there were two more closed door in the left exit and one closed door in the right exit. So Mr. Black wasn't able to exit at that moment.When he opened the third door, all doors from the right exit became open, s... | Input: 5 0 0 1 0 0 | Output: 3 | Beginner | 1 | 966 | 475 | 119 | 11 |
1,744 | F | 1744F | F. MEX vs MED | 2,000 | math; two pointers | You are given a permutation \(p_1, p_2, \ldots, p_n\) of length \(n\) of numbers \(0, \ldots, n - 1\). Count the number of subsegments \(1 \leq l \leq r \leq n\) of this permutation such that \(mex(p_l, p_{l+1}, \ldots, p_r) > med(p_l, p_{l+1}, \ldots, p_r)\).\(mex\) of \(S\) is the smallest non-negative integer that d... | The first line of the input contains a single integer \(t\) \((1 \leq t \leq 10^4\)), the number of test cases.The descriptions of the test cases follow.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)), the length of the permutation \(p\).The second line of each test cas... | For each test case print the answer in a single line: the number of subsegments \(1 \leq l \leq r \leq n\) of this permutation such that \(mex(p_l, p_{l+1}, \ldots, p_r) > med(p_l, p_{l+1}, \ldots, p_r)\). | The first test case contains exactly one subsegment and \(mex({0}) = 1 > med({0}) = 0\) on it.In the third test case, on the following subsegments: \([1, 0]\), \([0]\), \([1, 0, 2]\) and \([0, 2]\), \(mex\) is greater than \(med\).In the fourth test case, on the following subsegments: \([0, 2]\), \([0]\), \([0, 2, 1]\)... | Input: 81021 031 0 240 2 1 353 1 0 2 462 0 4 1 3 583 7 2 6 0 1 5 442 0 1 3 | Output: 1 2 4 4 8 8 15 6 | Hard | 2 | 982 | 532 | 205 | 17 |
1,927 | E | 1927E | E. Klever Permutation | 1,400 | constructive algorithms; math; two pointers | You are given two integers \(n\) and \(k\) (\(k \le n\)), where \(k\) is even.A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in any order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (as \(2\) appears twice in the array) and \(... | The first line of the input contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. This is followed by the description of the test cases.The first and only line of each test case contains two integers \(n\) and \(k\) (\(2 \le k \le n \le 2 \cdot 10^5\), \(k\) is even), where \(n\) is the len... | For each test case, output any \(k\)-level permutation of length \(n\).It is guaranteed that such a permutation always exists given the constraints. | In the second test case of the example: \(p_1 + p_2 = 3 + 1 = 4\); \(p_2 + p_3 = 1 + 2 = 3\). The maximum among the sums is \(4\), and the minimum is \(3\). | Input: 52 23 210 413 47 4 | Output: 2 1 1 3 2 1 8 4 10 2 7 5 9 3 6 4 10 1 13 5 9 2 12 6 8 3 11 7 1 6 3 7 2 5 4 | Easy | 3 | 1,024 | 442 | 148 | 19 |
181 | A | 181A | A. Series of Crimes | 800 | brute force; geometry; implementation | The Berland capital is shaken with three bold crimes committed by the Pihsters, a notorious criminal gang.The Berland capital's map is represented by an n × m rectangular table. Each cell of the table on the map represents some districts of the capital. The capital's main detective Polycarpus took a map and marked ther... | The first line contains two space-separated integers n and m (2 ≤ n, m ≤ 100) — the number of rows and columns in the table, correspondingly.Each of the next n lines contains m characters — the description of the capital's map. Each character can either be a ""."" (dot), or an ""*"" (asterisk). A character equals ""*""... | Print two integers — the number of the row and the number of the column of the city district that is the fourth one to be robbed. The rows are numbered starting from one from top to bottom and the columns are numbered starting from one from left to right. | Input: 3 2.*..** | Output: 1 1 | Beginner | 3 | 734 | 539 | 255 | 1 | |
745 | A | 745A | A. Hongcow Learns the Cyclic Shift | 900 | implementation; strings | Hongcow is learning to spell! One day, his teacher gives him a word that he needs to learn to spell. Being a dutiful student, he immediately learns how to spell the word.Hongcow has decided to try to make new words from this one. He starts by taking the word he just learned how to spell, and moves the last character of... | The first line of input will be a single string s (1 ≤ |s| ≤ 50), the word Hongcow initially learns how to spell. The string s consists only of lowercase English letters ('a'–'z'). | Output a single integer equal to the number of distinct strings that Hongcow can obtain by applying the cyclic shift arbitrarily many times to the given string. | For the first sample, the strings Hongcow can generate are ""abcd"", ""dabc"", ""cdab"", and ""bcda"".For the second sample, no matter how many times Hongcow does the cyclic shift, Hongcow can only generate ""bbb"".For the third sample, the two strings Hongcow can generate are ""yzyz"" and ""zyzy"". | Input: abcd | Output: 4 | Beginner | 2 | 731 | 180 | 160 | 7 |
1,197 | F | 1197F | F. Coloring Game | 2,700 | dp; games; matrices | Alice and Bob want to play a game. They have \(n\) colored paper strips; the \(i\)-th strip is divided into \(a_i\) cells numbered from \(1\) to \(a_i\). Each cell can have one of \(3\) colors.In the beginning of the game, Alice and Bob put \(n\) chips, the \(i\)-th chip is put in the \(a_i\)-th cell of the \(i\)-th st... | The first line contains one integer \(n\) — the number of paper strips (\(1 \le n \le 1000\)).The second line contains \(n\) integers \(a_1\), \(a_2\), ..., \(a_n\) (\(1 \le a_i \le 10^9\)), where \(a_i\) is the number of cells in the \(i\)-th strip.The third line contains one integer \(m\) (\(1 \le m \le 1000\)) — the... | Print one integer: the number of good colorings, taken modulo \(998244353\). | Input: 3 3 4 5 2 1 1 1 2 2 2 1 1 1 1 0 0 0 1 1 | Output: 14346 | Master | 3 | 1,605 | 1,042 | 76 | 11 | |
1,437 | E | 1437E | E. Make It Increasing | 2,200 | binary search; constructive algorithms; data structures; dp; implementation | You are given an array of \(n\) integers \(a_1\), \(a_2\), ..., \(a_n\), and a set \(b\) of \(k\) distinct integers from \(1\) to \(n\).In one operation, you may choose two integers \(i\) and \(x\) (\(1 \le i \le n\), \(x\) can be any integer) and assign \(a_i := x\). This operation can be done only if \(i\) does not b... | The first line contains two integers \(n\) and \(k\) (\(1 \le n \le 5 \cdot 10^5\), \(0 \le k \le n\)) — the size of the array \(a\) and the set \(b\), respectively.The second line contains \(n\) integers \(a_1\), \(a_2\), ..., \(a_n\) (\(1 \le a_i \le 10^9\)).Then, if \(k \ne 0\), the third line follows, containing \(... | If it is impossible to make the array \(a\) increasing using the given operations, print \(-1\).Otherwise, print one integer — the minimum number of operations you have to perform. | Input: 7 2 1 2 1 1 3 5 1 3 5 | Output: 4 | Hard | 5 | 516 | 442 | 180 | 14 | |
1,482 | A | 1482A | A. Prison Break | 800 | math | Michael is accused of violating the social distancing rules and creating a risk of spreading coronavirus. He is now sent to prison. Luckily, Michael knows exactly what the prison looks like from the inside, especially since it's very simple.The prison can be represented as a rectangle \(a\times b\) which is divided int... | The first line contains a single integer \(t\) (\(1\leq t\leq 100\)) — the number of test cases.Each of the following \(t\) lines contains two integers \(a\) and \(b\) (\(1\leq a, b\leq 100\)), representing a corresponding test case. | For each test case print the single integer on a separate line — the answer to the problem. | Some possible escape plans for the example test cases are shown below. Broken walls are shown in gray, not broken walls are shown in black. | Input: 2 2 2 1 3 | Output: 4 3 | Beginner | 1 | 951 | 233 | 91 | 14 |
213 | E | 213E | E. Two Permutations | 2,700 | data structures; hashing; strings | Rubik is very keen on number permutations. A permutation a with length n is a sequence, consisting of n different numbers from 1 to n. Element number i (1 ≤ i ≤ n) of this permutation will be denoted as ai.Furik decided to make a present to Rubik and came up with a new problem on permutations. Furik tells Rubik two num... | The first line contains two integers n and m (1 ≤ n ≤ m ≤ 200000) — the sizes of the given permutations. The second line contains n distinct integers — permutation a, the third line contains m distinct integers — permutation b. Numbers on the lines are separated by spaces. | On a single line print the answer to the problem. | Input: 1 111 | Output: 1 | Master | 3 | 875 | 273 | 49 | 2 | |
1,280 | A | 1280A | A. Cut and Paste | 1,700 | implementation; math | We start with a string \(s\) consisting only of the digits \(1\), \(2\), or \(3\). The length of \(s\) is denoted by \(|s|\). For each \(i\) from \(1\) to \(|s|\), the \(i\)-th character of \(s\) is denoted by \(s_i\). There is one cursor. The cursor's location \(\ell\) is denoted by an integer in \(\{0, \ldots, |s|\}\... | The first line of input contains a single integer \(t\) (\(1 \le t \le 1000\)) denoting the number of test cases. The next lines contain descriptions of the test cases.The first line of each test case contains a single integer \(x\) (\(1 \le x \le 10^6\)). The second line of each test case consists of the initial strin... | For each test case, output a single line containing a single integer denoting the answer for that test case modulo \(10^9 + 7\). | Let's illustrate what happens with the first test case. Initially, we have \(s = \) 231. Initially, \(\ell = 0\) and \(c = \varepsilon\) (the empty string). The following things happen if we follow the procedure above: Step 1, Move once: we get \(\ell = 1\). Step 2, Cut once: we get \(s = \) 2 and \(c = \) 31. Step 3, ... | Input: 4 5 231 7 2323 6 333 24 133321333 | Output: 25 1438 1101 686531475 | Medium | 2 | 1,628 | 627 | 128 | 12 |
2,035 | B | 2035B | B. Everyone Loves Tres | 900 | constructive algorithms; greedy; math; number theory | There are 3 heroes and 3 villains, so 6 people in total.Given a positive integer \(n\). Find the smallest integer whose decimal representation has length \(n\) and consists only of \(3\)s and \(6\)s such that it is divisible by both \(33\) and \(66\). If no such integer exists, print \(-1\). | The first line contains a single integer \(t\) (\(1\le t\le 500\)) — the number of test cases.The only line of each test case contains a single integer \(n\) (\(1\le n\le 500\)) — the length of the decimal representation. | For each test case, output the smallest required integer if such an integer exists and \(-1\) otherwise. | For \(n=1\), no such integer exists as neither \(3\) nor \(6\) is divisible by \(33\).For \(n=2\), \(66\) consists only of \(6\)s and it is divisible by both \(33\) and \(66\).For \(n=3\), no such integer exists. Only \(363\) is divisible by \(33\), but it is not divisible by \(66\).For \(n=4\), \(3366\) and \(6666\) a... | Input: 6123457 | Output: -1 66 -1 3366 36366 3336366 | Beginner | 4 | 292 | 221 | 104 | 20 |
627 | F | 627F | F. Island Puzzle | 3,400 | dfs and similar; dsu; graphs; trees | A remote island chain contains n islands, with some bidirectional bridges between them. The current bridge network forms a tree. In other words, a total of n - 1 bridges connect pairs of islands in a way that it's possible to reach any island from any other island using the bridge network. The center of each island con... | The first line contains a single integer n (2 ≤ n ≤ 200 000) — the total number of islands.The second line contains n space-separated integers ai (0 ≤ ai ≤ n - 1) — the statue currently located on the i-th island. If ai = 0, then the island has no statue. It is guaranteed that the ai are distinct.The third line contain... | Print a single line of integers:If the rearrangement can be done in the existing network, output 0 t, where t is the number of moves necessary to perform the rearrangement.Otherwise, print u, v, and t (1 ≤ u < v ≤ n) — the two endpoints of the new bridge, and the minimum number of statue movements needed to perform the... | In the first sample, the islanders can build a bridge connecting islands 1 and 3 and then make the following sequence of moves: first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3 for a total of 3 moves.In the second sample, th... | Input: 31 0 22 0 11 22 3 | Output: 1 3 3 | Master | 4 | 1,188 | 733 | 445 | 6 |
1,081 | E | 1081E | E. Missing Numbers | 1,900 | binary search; constructive algorithms; greedy; math; number theory | Chouti is working on a strange math problem.There was a sequence of \(n\) positive integers \(x_1, x_2, \ldots, x_n\), where \(n\) is even. The sequence was very special, namely for every integer \(t\) from \(1\) to \(n\), \(x_1+x_2+...+x_t\) is a square of some integer number (that is, a perfect square).Somehow, the n... | The first line contains an even number \(n\) (\(2 \le n \le 10^5\)).The second line contains \(\frac{n}{2}\) positive integers \(x_2, x_4, \ldots, x_n\) (\(1 \le x_i \le 2 \cdot 10^5\)). | If there are no possible sequence, print ""No"".Otherwise, print ""Yes"" and then \(n\) positive integers \(x_1, x_2, \ldots, x_n\) (\(1 \le x_i \le 10^{13}\)), where \(x_2, x_4, \ldots, x_n\) should be same as in input data. If there are multiple answers, print any.Note, that the limit for \(x_i\) is larger than for i... | In the first example \(x_1=4\) \(x_1+x_2=9\) \(x_1+x_2+x_3=25\) \(x_1+x_2+x_3+x_4=36\) \(x_1+x_2+x_3+x_4+x_5=100\) \(x_1+x_2+x_3+x_4+x_5+x_6=144\) All these numbers are perfect squares.In the second example, \(x_1=100\), \(x_1+x_2=10000\). They are all perfect squares. There're other answers possible. For example, \(x_... | Input: 65 11 44 | Output: Yes4 5 16 11 64 44 | Hard | 5 | 686 | 186 | 452 | 10 |
2,025 | F | 2025F | F. Choose Your Queries | 2,700 | constructive algorithms; dfs and similar; dp; graphs; greedy; trees | You are given an array \(a\), consisting of \(n\) integers (numbered from \(1\) to \(n\)). Initially, they are all zeroes.You have to process \(q\) queries. The \(i\)-th query consists of two different integers \(x_i\) and \(y_i\). During the \(i\)-th query, you have to choose an integer \(p\) (which is either \(x_i\) ... | The first line contains two integers \(n\) and \(q\) (\(2 \le n \le 3 \cdot 10^5\); \(1 \le q \le 3 \cdot 10^5\)) — the number of elements in \(a\) and the number of queries, respectively.Then \(q\) lines follow. The \(i\)-th of these lines contains two integers \(x_i\) and \(y_i\) (\(1 \le x_i, y_i \le n\); \(x_i \ne ... | For each query, print a line containing two characters: the first character should be x if you choose \(p=x_i\), or y if you choose \(p=y_i\); the second character should be + if you choose \(d=1\), or - if you choose \(d=-1\). If there are multiple answers, print any of them. | Input: 3 41 23 23 11 2 | Output: y+ x+ x- y- | Master | 6 | 608 | 367 | 277 | 20 | |
140 | E | 140E | E. New Year Garland | 2,600 | combinatorics; dp | As Gerald, Alexander, Sergey and Gennady are already busy with the usual New Year chores, Edward hastily decorates the New Year Tree. And any decent New Year Tree must be decorated with a good garland. Edward has lamps of m colors and he wants to make a garland from them. That garland should represent a sequence whose ... | The first line contains three integers n, m and p (1 ≤ n, m ≤ 106, 2 ≤ p ≤ 109) which are the number of the tree's layers, the number of the lamps' colors and module correspondingly. The next line contains n integers li (1 ≤ li ≤ 5000, ). | Print the only integer — the number of garlands modulo p. | In the first sample the following variants are possible: 121|1|12, 121|1|21, 121|2|12, 121|2|21, 212|1|12, 212|1|21, 212|2|12, 212|2|21. In the second sample the following variants are possible: 12|13, 12|23, 12|31, 12|32 and so on. Figure for the first sample: | Input: 3 2 10003 1 2 | Output: 8 | Expert | 2 | 1,448 | 238 | 57 | 1 |
688 | A | 688A | A. Opponents | 800 | implementation | Arya has n opponents in the school. Each day he will fight with all opponents who are present this day. His opponents have some fighting plan that guarantees they will win, but implementing this plan requires presence of them all. That means if one day at least one of Arya's opponents is absent at the school, then Arya... | The first line of the input contains two integers n and d (1 ≤ n, d ≤ 100) — the number of opponents and the number of days, respectively.The i-th of the following d lines contains a string of length n consisting of characters '0' and '1'. The j-th character of this string is '0' if the j-th opponent is going to be abs... | Print the only integer — the maximum number of consecutive days that Arya will beat all present opponents. | In the first and the second samples, Arya will beat all present opponents each of the d days.In the third sample, Arya will beat his opponents on days 1, 3 and 4 and his opponents will beat him on days 2 and 5. Thus, the maximum number of consecutive winning days is 2, which happens on days 3 and 4. | Input: 2 21000 | Output: 2 | Beginner | 1 | 725 | 340 | 106 | 6 |
175 | F | 175F | F. Gnomes of Might and Magic | 3,000 | data structures; graphs; implementation; shortest paths | Vasya plays a popular game the Gnomes of Might and Magic.In this game Vasya manages the kingdom of gnomes, consisting of several castles, connected by bidirectional roads. The kingdom road network has a special form. The kingdom has m main castles a1, a2, ..., am, which form the Good Path. This path consists of roads b... | The first line contains two integers n and m (3 ≤ m ≤ n ≤ 100000) — the number of castles in the kingdom, and the number of castles on the Good Path, respectively.The second line contains m integers, which are numbers of Good Path castles (the castles are numbered from 1 to n) in the order of occurrence on the Path, st... | For each query ""?"" print a single number on a single line — the number of very bad gnomes destroyed by the corresponding Mission of Death. Print the answers to queries in the chronological order. | In the example after the first four requests there is only one path from castle 1 to castle 2, which does not contain roads with very bad gnomes: 1 6 3 5 2.After a gnome stood on the road (2, 5), the next Mission of Death moves along path 1 2, and destroys the gnome, who was on the road (1, 2). The next Mission of Deat... | Input: 6 31 2 33 1 4 23 2 5 33 3 6 110+ 1 2+ 4 2+ 1 3+ 2 3? 1 2+ 2 5? 1 2? 1 2+ 1 2? 1 2 | Output: 0101 | Master | 4 | 2,460 | 1,953 | 197 | 1 |
154 | C | 154C | C. Double Profiles | 2,300 | graphs; hashing; sortings | You have been offered a job in a company developing a large social network. Your first task is connected with searching profiles that most probably belong to the same user.The social network contains n registered profiles, numbered from 1 to n. Some pairs there are friends (the ""friendship"" relationship is mutual, th... | The first line contains two space-separated integers n and m (1 ≤ n ≤ 106, 0 ≤ m ≤ 106), — the number of profiles and the number of pairs of friends, correspondingly. Next m lines contains descriptions of pairs of friends in the format ""v u"", where v and u (1 ≤ v, u ≤ n, v ≠ u) are numbers of profiles that are friend... | Print the single integer — the number of unordered pairs of profiles that are doubles. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the %I64d specificator. | In the first and second sample any two profiles are doubles.In the third sample the doubles are pairs of profiles (1, 3) and (2, 4). | Input: 3 31 22 31 3 | Output: 3 | Expert | 3 | 835 | 455 | 214 | 1 |
1,672 | F2 | 1672F2 | F2. Checker for Array Shuffling | 2,800 | constructive algorithms; dfs and similar; graphs | oolimry has an array \(a\) of length \(n\) which he really likes. Today, you have changed his array to \(b\), a permutation of \(a\), to make him sad.Because oolimry is only a duck, he can only perform the following operation to restore his array: Choose two integers \(i,j\) such that \(1 \leq i,j \leq n\). Swap \(b_i\... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) — the length of the array.The second lin... | For each test case, print ""AC"" (without quotes) if \(b\) has the maximum sadness over all permutations of \(a\), and ""WA"" (without quotes) otherwise. | In the first test case, the array \([1,2]\) has sadness \(1\). We can transform \([1,2]\) into \([2,1]\) using one operation with \((i,j)=(1,2)\).In the second test case, the array \([3,3,2,1]\) has sadness \(2\). We can transform \([3,3,2,1]\) into \([1,2,3,3]\) with two operations with \((i,j)=(1,4)\) and \((i,j)=(2,... | Input: 422 11 241 2 3 33 3 2 122 12 141 2 3 33 2 3 1 | Output: AC AC WA WA | Master | 3 | 586 | 735 | 153 | 16 |
1,840 | C | 1840C | C. Ski Resort | 1,000 | combinatorics; math; two pointers | Dima Vatrushin is a math teacher at school. He was sent on vacation for \(n\) days for his good work. Dima has long dreamed of going to a ski resort, so he wants to allocate several consecutive days and go skiing. Since the vacation requires careful preparation, he will only go for at least \(k\) days.You are given an ... | The first line of the input contains an integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.Then follow the descriptions of the test cases.The first line of each test case contains three integers \(n\), \(k\), \(q\) (\(1 \le n \le 2 \cdot 10^5\), \(1 \le k \le n\), \(-10^9 \le q \le 10^9\)) — the length of ... | Output \(t\) integers, each of which is the answer to the corresponding test case — the number of ways for Dima to choose vacation dates at the resort. | In the first test case of the example, Dima can go on any day, so the suitable dates for him are [1], [2], [3], [1, 2], [2, 3], [1, 2, 3].In the second and fourth test cases of the example, Dima cannot go on any day due to the high temperature, so there are no suitable dates.In the third test case of the example, Dima ... | Input: 73 1 15-5 0 -105 3 -338 12 9 0 54 3 1212 12 10 154 1 -50 -1 2 55 5 03 -1 4 -5 -31 1 556 1 30 3 -2 5 -4 -4 | Output: 6 0 1 0 0 1 9 | Beginner | 3 | 759 | 662 | 151 | 18 |
1,834 | E | 1834E | E. MEX of LCM | 2,300 | binary search; data structures; implementation; math; number theory | You are given an array \(a\) of length \(n\). A positive integer \(x\) is called good if it is impossible to find a subsegment\(^{\dagger}\) of the array such that the least common multiple of all its elements is equal to \(x\).You need to find the smallest good integer.A subsegment\(^{\dagger}\) of the array \(a\) is ... | Each test consists of multiple test cases. The first line of each test case contains a single integer \(t\) (\(1 \le t \le 5 \cdot 10^4\)) — the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 3 \cdot 10^5\)) — the length of t... | For each test case, output a single integer — the smallest good integer. | In the first test case, \(4\) is a good integer, and it is the smallest one, since the integers \(1,2,3\) appear in the array, which means that there are subsegments of the array of length \(1\) with least common multiples of \(1,2,3\). However, it is impossible to find a subsegment of the array with a least common mul... | Input: 631 2 351 2 3 4 522 311000000000121 8 4 2 3 5 7 2 9 10 11 13127 2 5 4 2 1 1 2 3 11 8 9 | Output: 4 7 1 1 16 13 | Expert | 5 | 449 | 573 | 72 | 18 |
2,022 | D2 | 2022D2 | D2. Asesino (Hard Version) | 2,700 | constructive algorithms; dp; interactive | This is the hard version of the problem. In this version, you must use the minimum number of queries possible. You can make hacks only if both versions of the problem are solved.This is an interactive problem.It is a tradition in Mexico's national IOI trainings to play the game ""Asesino"", which is similar to ""Among ... | The first line of input 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 \le n \le 10^5\)) — the number of people playing the game.It is guaranteed that the sum of \(n\) ov... | Note that the example test cases do not represent an optimal strategy for asking questions and are only shown for the sake of demonstrating the interaction format. Specifically, we cannot determine which player is the Impostor from the questions asked in the examples.In the first test case of the example, players at in... | Input: 2 7 1 0 0 1 1 0 0 4 0 1 1 1 | Output: ? 1 3 ? 7 6 ? 2 5 ? 6 2 ? 4 5 ? 4 6 ? 1 4 ! 4 ? 1 2 ? 2 3 ? 3 4 ? 4 1 ! 3 | Master | 3 | 2,149 | 363 | 0 | 20 | |
349 | B | 349B | B. Color the Fence | 1,700 | data structures; dp; greedy; implementation | Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requ... | The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 105). | Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. | Input: 55 4 3 2 1 2 3 4 5 | Output: 55555 | Medium | 4 | 502 | 139 | 136 | 3 | |
901 | E | 901E | E. Cyclic Cipher | 3,300 | fft; math | Senor Vorpal Kickass'o invented an innovative method to encrypt integer sequences of length n. To encrypt a sequence, one has to choose a secret sequence , that acts as a key.Vorpal is very selective, so the key should be such a sequence bi, that its cyclic shifts are linearly independent, that is, there is no non-zero... | The first line contains a single integer n ().The second line contains n integers b0, b1, ..., bn - 1 ().The third line contains n integers c0, c1, ..., cn - 1 ().It is guaranteed that all cyclic shifts of sequence bi are linearly independent. | In the first line print a single integer k — the number of sequences ai, such that after encrypting them with key bi you get the sequence ci.After that in each of k next lines print n integers a0, a1, ..., an - 1. Print the sequences in lexicographical order.Note that k could be equal to 0. | Input: 110 | Output: 11 | Master | 2 | 847 | 243 | 291 | 9 | |
1,497 | C1 | 1497C1 | C1. k-LCM (easy version) | 1,200 | constructive algorithms; math | It is the easy version of the problem. The only difference is that in this version \(k = 3\).You are given a positive integer \(n\). Find \(k\) positive integers \(a_1, a_2, \ldots, a_k\), such that: \(a_1 + a_2 + \ldots + a_k = n\) \(LCM(a_1, a_2, \ldots, a_k) \le \frac{n}{2}\) Here \(LCM\) is the least common multipl... | The first line contains a single integer \(t\) \((1 \le t \le 10^4)\) — the number of test cases.The only line of each test case contains two integers \(n\), \(k\) (\(3 \le n \le 10^9\), \(k = 3\)). | For each test case print \(k\) positive integers \(a_1, a_2, \ldots, a_k\), for which all conditions are satisfied. | Input: 3 3 3 8 3 14 3 | Output: 1 1 1 4 2 2 2 6 6 | Easy | 2 | 423 | 198 | 115 | 14 | |
235 | D | 235D | D. Graph Game | 3,000 | graphs | In computer science, there is a method called ""Divide And Conquer By Node"" to solve some hard problems about paths on a tree. Let's desribe how this method works by function:solve(t) (t is a tree): Chose a node x (it's common to chose weight-center) in tree t. Let's call this step ""Line A"". Deal with all paths that... | The first line contains an integer n (3 ≤ n ≤ 3000) — the number of nodes and edges in the graph. Each of the next n lines contains two space-separated integers ai, bi (0 ≤ ai, bi ≤ n - 1) indicating an edge between nodes ai and bi.Consider that the graph nodes are numbered from 0 to (n - 1). It's guaranteed that there... | Print a single real number — the expectation of totalCost. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. | Consider the second example. No matter what we choose first, the totalCost will always be 3 + 2 + 1 = 6. | Input: 53 42 32 40 41 2 | Output: 13.166666666666666 | Master | 1 | 1,364 | 417 | 155 | 2 |
315 | B | 315B | B. Sereja and Array | 1,200 | implementation | Sereja has got an array, consisting of n integers, a1, a2, ..., an. Sereja is an active boy, so he is now going to complete m operations. Each operation will have one of the three forms: Make vi-th array element equal to xi. In other words, perform the assignment avi = xi. Increase each array element by yi. In other wo... | The first line contains integers n, m (1 ≤ n, m ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the original array.Next m lines describe operations, the i-th line describes the i-th operation. The first number in the i-th line is integer ti (1 ≤ ti ≤ 3) that represents the o... | For each third type operation print value aqi. Print the values in the order, in which the corresponding queries follow in the input. | Input: 10 111 2 3 4 5 6 7 8 9 103 23 92 103 13 101 1 102 102 103 13 103 9 | Output: 291120304039 | Easy | 1 | 501 | 544 | 133 | 3 | |
2,096 | F | 2096F | F. Wonderful Impostors | 3,100 | data structures; implementation; two pointers | You are a proud live streamer known as Gigi Murin. Today, you will play a game with \(n\) viewers numbered \(1\) to \(n\).In the game, each player is either a crewmate or an impostor. You don't know the role of each viewer.There are \(m\) statements numbered \(1\) to \(m\), which are either true or false. For each \(i\... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains two integers \(n\), \(m\) (\(1 \leq n, m \leq 2 \cdot 10^5\)) — the number of viewers, and the number of statements.... | For each question, output ""YES"" if it is possible that the requested statements are all true. Otherwise, output ""NO"".You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses. | In the first test case, there are \(4\) viewers and \(3\) statements. The statements are as follows: Statement \(1\): There is at least one impostor among viewers \(1\), \(2\), and \(3\); Statement \(2\): There is at least one impostor among viewers \(2\), \(3\), and \(4\); Statement \(3\): There are no impostors among... | Input: 44 31 1 31 2 40 2 311 35 20 1 51 1 531 12 21 21 20 1 11 1 121 12 27 91 2 21 4 50 5 61 2 21 1 10 4 70 3 70 2 70 6 651 52 63 74 85 9 | Output: YES YES YES NO YES YES YES NO YES NO YES | Master | 3 | 944 | 952 | 282 | 20 |
1,916 | G | 1916G | G. Optimizations From Chelsu | 3,500 | divide and conquer; dp; number theory; trees | You are given a tree with \(n\) vertices, whose vertices are numbered from \(1\) to \(n\). Each edge is labeled with some integer \(w_i\).Define \(len(u, v)\) as the number of edges in the simple path between vertices \(u\) and \(v\), and \(gcd(u, v)\) as the Greatest Common Divisor of all numbers written on the edges ... | Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) — the number of test cases. This is followed by their description.The first line of each test case contains the number \(n\) (\(2 \leq n \leq 10^5\)) — the number of vertices in the tree.The next \(n-1\) l... | For each test case, output a single number equal to the maximum value of \(len(u, v) \cdot gcd(u, v)\) over all pairs of vertices in the tree. | Input: 421 2 100000000000043 2 62 1 102 4 681 2 122 3 93 4 94 5 65 6 126 7 47 8 9121 2 122 3 122 4 62 5 95 6 61 7 44 8 128 9 48 10 122 11 97 12 9 | Output: 1000000000000 12 18 24 | Master | 4 | 561 | 513 | 142 | 19 | |
1,685 | D2 | 1685D2 | D2. Permutation Weight (Hard Version) | 3,500 | constructive algorithms; greedy | This is a hard version of the problem. The difference between the easy and hard versions is that in this version, you have to output the lexicographically smallest permutation with the smallest weight.You are given a permutation \(p_1, p_2, \ldots, p_n\) of integers from \(1\) to \(n\).Let's define the weight of the pe... | The first line of the input contains a single integer \(t\) (\(1 \le t \le 100\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 200\)) — the size of the permutation.The second line of each test case contains \(n\) in... | For each test case, output \(n\) integers \(q_1, q_2, \ldots, q_n\) (\(1 \le q_i \le n\), all \(q_i\) are distinct) — the lexicographically smallest permutation with the smallest weight. | In the first test case, there are two permutations of length \(2\): \((1, 2)\) and \((2, 1)\). Permutation \((1, 2)\) has weight \(|1 - p_2| + |2 - p_1| = 0\), and the permutation \((2, 1)\) has the same weight: \(|2 - p_1| + |1 - p_2| = 0\). In this version, you have to output the lexicographically smaller of them — \... | Input: 322 142 3 1 455 4 3 2 1 | Output: 1 2 1 3 4 2 1 3 4 2 5 | Master | 2 | 865 | 495 | 186 | 16 |
106 | E | 106E | E. Space Rescuers | 2,100 | geometry; ternary search | The Galaxy contains n planets, there are many different living creatures inhabiting each planet. And each creature can get into troubles! Space rescuers know it perfectly well and they are always ready to help anyone who really needs help. All you need to do is call for them. Now the space rescuers plan to build the la... | The first line of the input file contains integer n — the number of planets (1 ≤ N ≤ 100). Each of the following n lines contains information about the planets. The i-th line contains three integers xi, yi, zi — the coordinates of the i-th planet ( - 104 ≤ xi, yi, zi ≤ 104, 1 ≤ i ≤ n). No two planets coincide. | Print on the first line of the output file three space-separated real numbers x0, y0, z0 — the coordinates for the future base. If there are several solutions, you are allowed to print any of them. The answer will be accepted if the distance from this point to the remotest planet will differ from the juries' variant in... | Input: 55 0 0-5 0 00 3 44 -3 02 2 -2 | Output: 0.000 0.000 0.000 | Hard | 2 | 1,269 | 311 | 371 | 1 | |
773 | A | 773A | A. Success Rate | 1,700 | binary search; math | You are an experienced Codeforces user. Today you found out that during your activity on Codeforces you have made y submissions, out of which x have been successful. Thus, your current success rate on Codeforces is equal to x / y.Your favorite rational number in the [0;1] range is p / q. Now you wonder: what is the sma... | The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.Each of the next t lines contains four integers x, y, p and q (0 ≤ x ≤ y ≤ 109; 0 ≤ p ≤ q ≤ 109; y > 0; q > 0).It is guaranteed that p / q is an irreducible fraction.Hacks. For hacks, an additional constraint of t ≤ 5 must be met. | For each test case, output a single integer equal to the smallest number of submissions you have to make if you want your success rate to be equal to your favorite rational number, or -1 if this is impossible to achieve. | In the first example, you have to make 4 successful submissions. Your success rate will be equal to 7 / 14, or 1 / 2.In the second example, you have to make 2 successful and 8 unsuccessful submissions. Your success rate will be equal to 9 / 24, or 3 / 8.In the third example, there is no need to make any new submissions... | Input: 43 10 1 27 14 3 820 70 2 75 6 1 1 | Output: 4100-1 | Medium | 2 | 407 | 315 | 220 | 7 |
1,709 | C | 1709C | C. Recover an RBS | 1,800 | constructive algorithms; greedy; implementation; strings | A bracket sequence is a string containing only characters ""("" and "")"". A regular bracket sequence (or, shortly, an RBS) is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters ""1"" and ""+"" between the original characters of the sequence. For example: bracket seq... | The first line contains a single integer \(t\) (\(1 \le t \le 5 \cdot 10^4\)) — the number of testcases.The only line of each testcase contains an RBS with some brackets replaced with question marks. Each character is either '(', ')' or '?'. At least one RBS can be recovered from the given sequence.The total length of ... | For each testcase, print ""YES"" if the way to replace question marks with brackets, so that the resulting sequence is an RBS, is unique. If there is more than one way, then print ""NO"". | In the first testcase, the only possible original RBS is ""(())"".In the second testcase, there are multiple ways to recover an RBS.In the third and the fourth testcases, the only possible original RBS is ""()"".In the fifth testcase, the original RBS can be either ""((()()))"" or ""(())()()"". | Input: 5(?))??????()???(?)()?) | Output: YES NO YES YES NO | Medium | 4 | 668 | 385 | 187 | 17 |
44 | G | 44G | G. Shooting Gallery | 2,500 | data structures; implementation | Berland amusement park shooting gallery is rightly acknowledged as one of the best in the world. Every day the country's best shooters master their skills there and the many visitors compete in clay pigeon shooting to win decent prizes. And the head of the park has recently decided to make an online version of the shoo... | The first line contains an integer n (1 ≤ n ≤ 105) — the number of targets. Each of the subsequent n lines contains the description of a target. The target is described by five integers xl, xr, yl, yr, z, that determine it's location in space (0 ≤ xl < xr ≤ 107, 0 ≤ yl < yr ≤ 107, 0 < z ≤ 107). The next line contains a... | For every shot in the single line print the number of the target which the shot has hit, or 0, if the bullet did not hit any target. The targets are numbered starting from 1 in the order in which they were given in the input data. | Input: 21 4 1 4 12 5 2 6 240 03 34 53 5 | Output: 0120 | Expert | 2 | 1,883 | 804 | 230 | 0 | |
272 | C | 272C | C. Dima and Staircase | 1,500 | data structures; implementation | Dima's got a staircase that consists of n stairs. The first stair is at height a1, the second one is at a2, the last one is at an (1 ≤ a1 ≤ a2 ≤ ... ≤ an). Dima decided to play with the staircase, so he is throwing rectangular boxes at the staircase from above. The i-th box has width wi and height hi. Dima throws each ... | The first line contains integer n (1 ≤ n ≤ 105) — the number of stairs in the staircase. The second line contains a non-decreasing sequence, consisting of n integers, a1, a2, ..., an (1 ≤ ai ≤ 109; ai ≤ ai + 1).The next line contains integer m (1 ≤ m ≤ 105) — the number of boxes. Each of the following m lines contains ... | Print m integers — for each box the height, where the bottom of the box will be after landing. Print the answers for the boxes in the order, in which the boxes are given in the input.Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d ... | The first sample are shown on the picture. | Input: 51 2 3 6 641 13 11 14 3 | Output: 1346 | Medium | 2 | 1,104 | 456 | 330 | 2 |
1,092 | D2 | 1092D2 | D2. Great Vova Wall (Version 2) | 2,200 | data structures; implementation | Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.The current state of the wall can be respresented by a sequence \(a\) of \(n\) integers, with \(a_i\) being the height of th... | The first line contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) — the number of parts in the wall.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) — the initial heights of the parts of the wall. | Print ""YES"" if Vova can complete the wall using any amount of bricks (possibly zero).Print ""NO"" otherwise. | In the first example Vova can put a brick on parts 2 and 3 to make the wall \([2, 2, 2, 2, 5]\) and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it \([5, 5, 5, 5, 5]\).In the second example Vova can put no bricks in the wall.In the third example the wall is already complete. | Input: 5 2 1 1 2 5 | Output: YES | Hard | 2 | 1,142 | 249 | 110 | 10 |
1,930 | G | 1930G | G. Prefix Max Set Counting | 3,100 | data structures; dp; trees | Define a function \(f\) such that for an array \(b\), \(f(b)\) returns the array of prefix maxima of \(b\). In other words, \(f(b)\) is an array containing only those elements \(b_i\), for which \(b_i=\max(b_1,b_2,\ldots,b_i)\), without changing their order. For example, \(f([3,10,4,10,15,1])=[3,10,10,15]\).You are giv... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^5\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 10^6\)) — the number of vertices.The following next \(n-... | For each test case, output the number of distinct values of \(f(a)\) modulo \(998\,244\,353\) that you can get. | In the first test case, the only valid pre-order is \(a=[1]\). So the only possible value of \(f(a)\) is \([1]\).In the second test case, the only valid pre-order is \(a=[1,2]\). So the only possible value \(f(a)\) is \([1,2]\).In the third test case, the two valid pre-orders are \(a=[1,2,3]\) and \(a=[1,3,2]\). So the... | Input: 6121 231 21 331 22 351 21 31 41 5101 22 31 42 52 64 75 84 99 10 | Output: 1 1 2 1 8 6 | Master | 3 | 1,301 | 590 | 111 | 19 |
961 | A | 961A | A. Tetris | 900 | implementation | You are given a following process. There is a platform with \(n\) columns. \(1 \times 1\) squares are appearing one after another in some columns on this platform. If there are no squares in the column, a square will occupy the bottom row. Otherwise a square will appear at the top of the highest square of this column. ... | The first line of input contain 2 integer numbers \(n\) and \(m\) (\(1 \le n, m \le 1000\)) — the length of the platform and the number of the squares.The next line contain \(m\) integer numbers \(c_1, c_2, \dots, c_m\) (\(1 \le c_i \le n\)) — column in which \(i\)-th square will appear. | Print one integer — the amount of points you will receive. | In the sample case the answer will be equal to \(2\) because after the appearing of \(6\)-th square will be removed one row (counts of the squares on the platform will look like \([2~ 3~ 1]\), and after removing one row will be \([1~ 2~ 0]\)).After the appearing of \(9\)-th square counts will be \([2~ 3~ 1]\), and afte... | Input: 3 91 1 2 2 2 3 1 2 3 | Output: 2 | Beginner | 1 | 568 | 288 | 58 | 9 |
534 | A | 534A | A. Exam | 1,100 | constructive algorithms; implementation; math | An exam for n students will take place in a long and narrow room, so the students will sit in a line in some order. The teacher suspects that students with adjacent numbers (i and i + 1) always studied side by side and became friends and if they take an exam sitting next to each other, they will help each other for sur... | A single line contains integer n (1 ≤ n ≤ 5000) — the number of students at an exam. | In the first line print integer k — the maximum number of students who can be seated so that no two students with adjacent numbers sit next to each other.In the second line print k distinct integers a1, a2, ..., ak (1 ≤ ai ≤ n), where ai is the number of the student on the i-th position. The students on adjacent positi... | Input: 6 | Output: 61 5 3 6 2 4 | Easy | 3 | 489 | 84 | 497 | 5 | |
1,983 | B | 1983B | B. Corner Twist | 1,200 | constructive algorithms; greedy; implementation; math | You are given two grids of numbers \(a\) and \(b\), with \(n\) rows and \(m\) columns. All the values in the grid are \(0\), \(1\) or \(2\).You can perform the following operation on \(a\) any number of times: Pick any subrectangle in the grid with length and width \(\ge 2\). You are allowed to choose the entire grid a... | The first line contains an integer \(t\), the number of testcases (\(1 \le t \le 250\)).For each testcase:The first line contains two integers \(n\) and \(m\), the number of rows and columns in the grid (\(2 \le n,m \le 500\)).Each of the next n lines contain m characters — the \(j\)-th character of the \(i\)-th line r... | For each test case print ""YES"" (without quotes) if it is possible to convert grid \(a\) into grid \(b\) and ""NO"" (without quotes) 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 testcase, grid \(a\) can be converted into \(b\) in the following manner:\(\begin{matrix}\fbox{0} & 0 & \fbox{0}\\ 0 & 0 & 0\\ \fbox{0} & 0 & \fbox{0}\end{matrix} \Rightarrow \begin{matrix}1 & 0 & 2\\ 0 & \fbox{0} & \fbox{0}\\ 2 & \fbox{0} & \fbox{1}\end{matrix} \Rightarrow \begin{matrix}1 & 0 & 2\\ \fbox{... | Input: 73 30000000001111111114 4000000000000000021001200001200214 4102012001210000000001200220000003 30120120120101110118 8000000000000000000000000000000000000000000000000000000001000000000000000012000000201000000102000000201000000102000000210100000002 700000000000000222011101112222 70000000010001022201111210202 | Outp... | Easy | 4 | 780 | 614 | 305 | 19 |
2,119 | D | 2119D | D. Token Removing | 2,100 | combinatorics; dp; math | r-906 & Hatsune Miku - All I Can See Is You Define a sequence of integers \(a\) valid if and only if \(\forall 1 \le i \le n, 0 \le a_i \le i\).Define the weight \(f(a)\) of a valid sequence \(a\) of length \(n\): Initially, a token is placed at each integer point in the closed segment \([1, n]\) on the number axis. Pe... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)). The description of the test cases follows. The only line of each test case contains two integers \(n\) and \(m\) (\(1 \le n \le 5000, 10^8 \le m \le 1.01 \cdot 10^9\)) — the length of valid sequences, ... | For each test case, output an integer — the sum of the weights over all \((n + 1)!\) valid sequences of length \(n\), modulo \(m\). | In the first test case, valid sequences are \([0]\) and \([1]\), and the answer is \(f([0]) + f([1]) = 1 + 1 = 2\).In the second test case, valid sequences are \([0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2]\). The weight of \([0, 1]\) is \(2\), and the others are \(1\), so the answer is \(5 \cdot 1 + 1 \cdot 2 = 7\). | Input: 61 10000000072 10000000073 10000000074 10000000075 1000000007114 514191981 | Output: 2 7 37 273 2672 393775292 | Hard | 3 | 1,006 | 432 | 131 | 21 |
1,623 | A | 1623A | A. Robot Cleaner | 800 | brute force; implementation; math | A robot cleaner is placed on the floor of a rectangle room, surrounded by walls. The floor consists of \(n\) rows and \(m\) columns. The rows of the floor are numbered from \(1\) to \(n\) from top to bottom, and columns of the floor are numbered from \(1\) to \(m\) from left to right. The cell on the intersection of th... | 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.A test case consists of only one line, containing six integers \(n\), \(m\), \(r_b\), \(c_b\), \(r_d\), and \(c_d\) (\(1 \le n, m \le 100\), \(1 \le r_b, r_d \le n... | For each test case, print an integer — the time for the robot to clean the dirty cell. We can show that the robot always cleans the dirty cell eventually. | In the first example, the floor has the size of \(10\times 10\). The initial position of the robot is \((6, 1)\) and the position of the dirty cell is \((2, 8)\). See the illustration of this example in the problem statement.In the second example, the floor is the same, but the initial position of the robot is now \((9... | Input: 5 10 10 6 1 2 8 10 10 9 9 1 1 9 8 5 6 2 1 6 9 2 2 5 8 2 2 1 1 2 1 | Output: 7 10 9 3 0 | Beginner | 3 | 1,534 | 443 | 154 | 16 |
1,921 | D | 1921D | D. Very Different Array | 1,100 | data structures; greedy; sortings; two pointers | Petya has an array \(a_i\) of \(n\) integers. His brother Vasya became envious and decided to make his own array of \(n\) integers.To do this, he found \(m\) integers \(b_i\) (\(m\ge n\)), and now he wants to choose some \(n\) integers of them and arrange them in a certain order to obtain an array \(c_i\) of length \(n... | Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 100\)) — the number of test cases. This is followed by a description of the test cases.The first line of each test case contains two integers \(n\) and \(m\) (\(1\le n\le m\le 2 \cdot 10^5\)).The second line of each... | For each test case, output a single integer — the maximum total difference \(D\) that can be obtained. | In the first example, Vasya can, for example, create the array \((1, 5, 7, 2)\). Then the total difference will be \(D = |6-1|+|1-5|+|2-7|+|4-2| = 5+4+5+2 = 16\).In the second example, all the integers available to Vasya are equal to 1, so he can only create the array \((1, 1, 1)\), for which the difference \(D = 0\).I... | Input: 94 66 1 2 43 5 1 7 2 33 41 1 11 1 1 15 51 2 3 4 51 2 3 4 52 65 88 7 5 8 2 102 24 19 64 68 10 6 43 10 6 1 8 93 56 5 21 7 9 7 25 59 10 6 3 75 9 2 3 91 632 7 10 1 1 5 | Output: 16 0 12 11 10 23 15 25 7 | Easy | 4 | 602 | 578 | 102 | 19 |
1,965 | F | 1965F | F. Conference | 3,300 | data structures; flows | You have been asked to organize a very important art conference. The first step is to choose the dates.The conference must last for a certain number of consecutive days. Each day, one lecturer must perform, and the same lecturer cannot perform more than once.You asked \(n\) potential lecturers if they could participate... | The first line of input contains one integer \(n\) — the number of potential lecturers (\(1 \le n \le 2 \cdot 10^5\)).Each of the next \(n\) lines contains two integers \(l_i\) and \(r_i\) — the segment of available days for the \(i\)th lecturer (\(1 \le l_i \le r_i \le 2 \cdot 10^5\)). | Print \(n\) integers, where the \(k\)th number denotes the number of ways to select a segment of \(k\) consecutive days as conference dates. | In the first testcase, a one-day conference can be organized on any of the days from \(1\) to \(6\). A two-day conference can be organized from day \(2\) to day \(3\), as well as from day \(4\) to day \(5\).In the second testcase, five lecturers can perform only from day \(1\) to day \(3\), so it will not be possible t... | Input: 31 23 45 6 | Output: 6 2 0 | Master | 2 | 759 | 287 | 140 | 19 |
1,033 | F | 1033F | F. Boolean Computer | 2,800 | bitmasks; brute force; fft; math | Alice has a computer that operates on \(w\)-bit integers. The computer has \(n\) registers for values. The current content of the registers is given as an array \(a_1, a_2, \ldots, a_n\). The computer uses so-called ""number gates"" to manipulate this data. Each ""number gate"" takes two registers as inputs and calcula... | The first line contains three integers: \(w\), \(n\), and \(m~(1 \leq w \leq 12, 1 \leq n \leq 3\cdot 10^4, 1 \leq m \leq 5\cdot 10^4)\) — the word size, the number of variables, and the number of gates.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) \((0 \leq a_i < 2^w)\) — the value of variables sto... | Print \(m\) lines. The \(i\)-th line should contain the number of ordered pairs of variables for which the \(i\)-th gate returns zero. | In the first test case, the inputs in binary are \(1101\), \(1010\), \(0110\). The pairs that return \(0\) are \((13, 6)\), \((6, 13)\), and \((6, 6)\). As it was already mentioned in the problem statement, \(13 \oplus 10 = 10 \oplus 13 = 12\). The other pairs are \(13 \oplus 13 = 11\), \(10 \oplus 10 = 8\) and \(10 \o... | Input: 4 3 113 10 6AXoA | Output: 3 | Master | 4 | 1,974 | 522 | 134 | 10 |
834 | B | 834B | B. The Festive Evening | 1,100 | data structures; implementation | It's the end of July – the time when a festive evening is held at Jelly Castle! Guests from all over the kingdom gather here to discuss new trends in the world of confectionery. Yet some of the things discussed here are not supposed to be disclosed to the general public: the information can cause discord in the kingdom... | Two integers are given in the first string: the number of guests n and the number of guards k (1 ≤ n ≤ 106, 1 ≤ k ≤ 26).In the second string, n uppercase English letters s1s2... sn are given, where si is the entrance used by the i-th guest. | Output «YES» if at least one door was unguarded during some time, and «NO» otherwise.You can output each letter in arbitrary case (upper or lower). | In the first sample case, the door A is opened right before the first guest's arrival and closed when the second guest enters the castle. The door B is opened right before the arrival of the third guest, and closed after the fifth one arrives. One guard can handle both doors, as the first one is closed before the secon... | Input: 5 1AABBB | Output: NO | Easy | 2 | 1,420 | 240 | 147 | 8 |
2,087 | I | 2087I | I. Hamiltonian Partition | 0 | *special; *special | You are given a directed acyclic graph with \(n\) vertices and \(m\) edges. The graph contains no cycles or multiple edges.You need to partition all the edges of this graph into Hamiltonian cycles (cycles that visit each of the \(n\) vertices of the graph exactly once) such that each edge belongs to exactly one cycle. ... | The first line contains two integers \(n\) and \(m\) (\(2 \le n \le 100\); \(1 \le m \le \frac{n(n-1)}{2}\)).The next \(m\) lines contain two integers \(x_i\) and \(y_i\) in the \(i\)-th line (\(1 \le x_i, y_i \le n\); \(x_i \ne y_i\)), representing a directed edge from vertex \(x_i\) to vertex \(y_i\).Additional const... | In the first line, output a single integer \(k\) (\(1 \le k \le n \cdot m\)) — the number of edges you add.Then output \(k\) lines, in the \(i\)-th of which there should be two integers \(x_i\) and \(y_i\) (\(1 \le x_i, y_i \le n\); \(x_i \ne y_i\)), denoting the start and end of the next edge.Then in a single line, ou... | Input: 3 21 22 3 | Output: 1 3 1 1 1 1 1 | Beginner | 2 | 608 | 389 | 829 | 20 | |
1,741 | C | 1741C | C. Minimize the Thickness | 1,100 | brute force; greedy; math; two pointers | You are given a sequence \(a=[a_1,a_2,\dots,a_n]\) consisting of \(n\) positive integers.Let's call a group of consecutive elements a segment. Each segment is characterized by two indices: the index of its left end and the index of its right end. Denote by \(a[l,r]\) a segment of the sequence \(a\) with the left end in... | The first line contains a single integer \(t\) (\(1 \le t \le 100\)) — the number of test cases.Each test case is described by two lines.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2000\)) — the length of the sequence \(a\).The second line of each test case contains exactly \(n\) int... | For each test case, output one integer — the minimum possible thickness of a split of the sequence \(a\) into segments.Note that there always exist a split, you can always consider whole sequence as one segment. | The split in the first test case is explained in the statement, it can be shown that it is optimal.In the second test case, it is possible to split into segments only by leaving a single segment. Then the thickness of this split is equal to the length of the entire sequence, that is, \(4\).In the third test case, the o... | Input: 4655 45 30 30 40 100410 23 7 13510 55 35 30 6564 1 1 1 1 4 | Output: 3 4 2 3 | Easy | 4 | 1,190 | 493 | 211 | 17 |
1,468 | F | 1468F | F. Full Turn | 1,700 | geometry; hashing; number theory | There are \(n\) persons located on a plane. The \(i\)-th person is located at the point \((x_i, y_i)\) and initially looks at the point \((u_i, v_i)\).At the same moment of time, all persons will start to rotate clockwise synchronously with the same angular speed. They will rotate until they do one full \(360\)-degree ... | The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)) — the number of persons. The following \(n\) lines describe persons, each line containing four space-separated integers \(x_i, y_i, u_i, v_i... | For each test case, print one integer — the number of pairs of persons who will make eye contact at least once during the rotation, including the initial moment. | Input: 320 0 0 11 0 2 030 0 1 11 1 0 01 0 2 060 0 0 11 0 1 22 0 2 33 0 3 -54 0 4 -55 0 5 -5 | Output: 0 1 9 | Medium | 3 | 843 | 707 | 161 | 14 | |
455 | A | 455A | A. Boredom | 1,500 | dp | Alex doesn't like boredom. That's why whenever he gets bored, he comes up with games. One long winter evening he came up with a game and decided to play it.Given a sequence a consisting of n integers. The player can make several steps. In a single step he can choose an element of the sequence (let's denote it ak) and d... | The first line contains integer n (1 ≤ n ≤ 105) that shows how many numbers are in Alex's sequence. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 105). | Print a single integer — the maximum number of points that Alex can earn. | Consider the third test example. At first step we need to choose any element equal to 2. After that step our sequence looks like this [2, 2, 2, 2]. Then we do 4 steps, on each step we choose any element equals to 2. In total we earn 10 points. | Input: 21 2 | Output: 2 | Medium | 1 | 543 | 167 | 73 | 4 |
215 | D | 215D | D. Hot Days | 1,900 | greedy | The official capital and the cultural capital of Berland are connected by a single road running through n regions. Each region has a unique climate, so the i-th (1 ≤ i ≤ n) region has a stable temperature of ti degrees in summer.This summer a group of m schoolchildren wants to get from the official capital to the cultu... | The first input line contains two integers n and m (1 ≤ n ≤ 105; 1 ≤ m ≤ 106) — the number of regions on the way and the number of schoolchildren in the group, correspondingly. Next n lines contain four integers each: the i-th line contains ti, Ti, xi and costi (1 ≤ ti, Ti, xi, costi ≤ 106). The numbers in the lines ar... | Print the only integer — the minimum number of roubles the organizers will have to spend to transport all schoolchildren.Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier. | In the first sample the organizers will use only one bus to travel through the first region. However, the temperature in the bus will equal 30 + 10 = 40 degrees and each of 10 schoolchildren will ask for compensation. Only one bus will transport the group through the second region too, but the temperature inside won't ... | Input: 2 1030 35 1 10020 35 10 10 | Output: 120 | Hard | 1 | 1,479 | 349 | 264 | 2 |
19 | C | 19C | C. Deletion of Repeats | 2,200 | greedy; hashing; string suffix structures | Once Bob saw a string. It contained so many different letters, that the letters were marked by numbers, but at the same time each letter could be met in the string at most 10 times. Bob didn't like that string, because it contained repeats: a repeat of length x is such a substring of length 2x, that its first half coin... | The first input line contains integer n (1 ≤ n ≤ 105) — length of the string. The following line contains n space-separated integer numbers from 0 to 109 inclusive — numbers that stand for the letters of the string. It's guaranteed that each letter can be met in the string at most 10 times. | In the first line output the length of the string's part, left after Bob's deletions. In the second line output all the letters (separated by a space) of the string, left after Bob deleted all the repeats in the described way. | Input: 61 2 3 1 2 3 | Output: 31 2 3 | Hard | 3 | 755 | 291 | 226 | 0 | |
2,068 | B | 2068B | B. Urban Planning | 3,100 | constructive algorithms | You are responsible for planning a new city! The city will be represented by a rectangular grid, where each cell is either a park or a built-up area.The residents will naturally want to go for walks in the city parks. In particular, a rectangular walk is a rectangle consisting of the grid cells, which is at least 2 cel... | The input contains a single integer \(k\) (\(0 \le k \le 4.194\cdot 10^{12}\)). | On the first line, print two integers \(h\) and \(w\) (\(1 \le h, w \le 2025\)), the height and width of the grid. On the next \(h\) lines, print a string with \(w\) characters each, with each character being #, denoting a park, or ., denoting a built-up area.It is guaranteed that for any value of \(k\) within the give... | In the sample, here are the five possible rectangular walks: | Input: 5 | Output: 3 4 #### #.## #### | Master | 1 | 696 | 79 | 492 | 20 |
496 | C | 496C | C. Removing Columns | 1,500 | brute force; constructive algorithms; implementation | You are given an n × m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the tableabcdedfghijk we obtain the table:acdefghjk A table is... | The first line contains two integers — n and m (1 ≤ n, m ≤ 100).Next n lines contain m small English letters each — the characters of the table. | Print a single number — the minimum number of columns that you need to remove in order to make the table good. | In the first sample the table is already good.In the second sample you may remove the first and third column.In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).Let strings s and t have equal length. Then, s is lexicographically larger t... | Input: 1 10codeforces | Output: 0 | Medium | 3 | 562 | 144 | 110 | 4 |
1,582 | C | 1582C | C. Grandma Capa Knits a Scarf | 1,200 | brute force; data structures; greedy; strings; two pointers | Grandma Capa has decided to knit a scarf and asked Grandpa Sher to make a pattern for it, a pattern is a string consisting of lowercase English letters. Grandpa Sher wrote a string \(s\) of length \(n\).Grandma Capa wants to knit a beautiful scarf, and in her opinion, a beautiful scarf can only be knit from a string th... | The first line contains a single integer \(t\) (\(1 \le t \le 100\)) — the number of test cases. The next \(2 \cdot t\) lines contain the description of test cases. The description of each test case consists of two lines.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)) — the lengt... | For each test case print the minimum number of erased symbols required to make the string a palindrome, if it is possible, and \(-1\), if it is impossible. | In the first test case, you can choose a letter 'a' and erase its first and last occurrences, you will get a string 'bcaacb', which is a palindrome. You can also choose a letter 'b' and erase all its occurrences, you will get a string 'acaaca', which is a palindrome as well.In the second test case, it can be shown that... | Input: 5 8 abcaacab 6 xyzxyz 4 abba 8 rprarlap 10 khyyhhyhky | Output: 2 -1 0 3 2 | Easy | 5 | 1,074 | 534 | 155 | 15 |
1,942 | E | 1942E | E. Farm Game | 2,300 | combinatorics; games | Lunatic Princess - Touhou⠀Farmer Nhoj has brought his cows over to Farmer John's farm to play a game! FJ's farm can be modeled by a number line with walls at points \(0\) and \(l + 1\). On the farm, there are \(2n\) cows, with \(n\) of the cows belonging to FJ and the other \(n\) belonging to FN. They place each of the... | The first line contains \(t\) (\(1 \leq t \leq 10^4\)) — the number of test cases.Each test case contains two integers \(l\) and \(n\) (\(2 \leq l \leq 10^6, 1 \leq n \leq \lfloor \frac{l}{2} \rfloor\)) — the length of the number line and the number of cows each farmer will place.It is guaranteed the sum of \(l\) over ... | For each test case output an integer: the number of game configurations where Farmer John wins if both farmers play optimally, modulo \(998\,244\,353\). | Let J denote FJ's cow, N denote FN's cow, and _ denote an empty space.For the first test case, the two possible configurations are JN or NJ. In both cases, since FJ makes the first turn and cannot make any moves, he cannot win.For the second case there are two possible configurations for FJ to win: N_J and J_N. | Input: 32 13 1420 69 | Output: 0 2 870279412 | Expert | 2 | 1,486 | 360 | 152 | 19 |
819 | D | 819D | D. Mister B and Astronomers | 2,900 | number theory | After studying the beacons Mister B decided to visit alien's planet, because he learned that they live in a system of flickering star Moon. Moreover, Mister B learned that the star shines once in exactly T seconds. The problem is that the star is yet to be discovered by scientists.There are n astronomers numerated from... | The first line contains two integers T and n (1 ≤ T ≤ 109, 2 ≤ n ≤ 2·105).The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109). | Print n integers: for each astronomer print the number of time segments describer earlier. | In the first sample test the first astronomer will send requests at moments t1 = 0, 5, 10, ..., the second — at moments t2 = 3, 8, 13, .... That's why interval [0, 1) the first astronomer will discover first at moment t1 = 0, [1, 2) — the first astronomer at moment t1 = 5, [2, 3) — the first astronomer at moment t1 = 1... | Input: 4 22 3 | Output: 3 1 | Master | 1 | 1,433 | 141 | 90 | 8 |
1,098 | D | 1098D | D. Eels | 2,800 | data structures | Vasya is a big fish lover, and his parents gave him an aquarium for the New Year. Vasya does not have a degree in ichthyology, so he thinks that filling a new aquarium with eels is a good idea. Unfortunately, eels are predators, so Vasya decided to find out how dangerous this idea was.Getting into one aquarium, eels fi... | The first line of input contains a single integer \(q\) (\(1 \le q \le 500\,000\)), the number of operations that Vasya makes. The next \(q\) lines describe operations. Each operation has one of two types : + x describes the addition of one eel of weight \(x\) to the set (\(1 \le x \le 10^9\)). Note that in the set the... | For each operation, output single integer, the danger of the set of eels after this operation. | In the third example, after performing all the operations, the set of eels looks like \(\{1, 1, 4\}\). For this set of eels, there are several possible scenarios, if all of them are placed in one aquarium: The eel of weight 4 eats the eel of weight 1, and then the second eel of weight 1. In this case, none of the battl... | Input: 2 + 1 - 1 | Output: 0 0 | Master | 1 | 1,496 | 495 | 94 | 10 |
1,028 | B | 1028B | B. Unnatural Conditions | 1,200 | constructive algorithms; math | Let \(s(x)\) be sum of digits in decimal representation of positive integer \(x\). Given two integers \(n\) and \(m\), find some positive integers \(a\) and \(b\) such that \(s(a) \ge n\), \(s(b) \ge n\), \(s(a + b) \le m\). | The only line of input contain two integers \(n\) and \(m\) (\(1 \le n, m \le 1129\)). | Print two lines, one for decimal representation of \(a\) and one for decimal representation of \(b\). Both numbers must not contain leading zeros and must have length no more than \(2230\). | In the first sample, we have \(n = 6\) and \(m = 5\). One valid solution is \(a = 6\), \(b = 7\). Indeed, we have \(s(a) = 6 \ge n\) and \(s(b) = 7 \ge n\), and also \(s(a + b) = s(13) = 4 \le m\). | Input: 6 5 | Output: 6 7 | Easy | 2 | 224 | 86 | 189 | 10 |
508 | A | 508A | A. Pasha and Pixels | 1,100 | brute force | Pasha loves his phone and also putting his hair up... But the hair is now irrelevant.Pasha has installed a new game to his phone. The goal of the game is following. There is a rectangular field consisting of n row with m pixels in each row. Initially, all the pixels are colored white. In one move, Pasha can choose any ... | The first line of the input contains three integers n, m, k (1 ≤ n, m ≤ 1000, 1 ≤ k ≤ 105) — the number of rows, the number of columns and the number of moves that Pasha is going to perform. The next k lines contain Pasha's moves in the order he makes them. Each line contains two integers i and j (1 ≤ i ≤ n, 1 ≤ j ≤ m)... | If Pasha loses, print the number of the move when the 2 × 2 square consisting of black pixels is formed.If Pasha doesn't lose, that is, no 2 × 2 square consisting of black pixels is formed during the given k moves, print 0. | Input: 2 2 41 11 22 12 2 | Output: 4 | Easy | 1 | 954 | 412 | 223 | 5 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.