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,054 | G | 1054G | G. New Road Network | 3,300 | constructive algorithms; greedy; math | The king of some country \(N\) decided to completely rebuild the road network. There are \(n\) people living in the country, they are enumerated from \(1\) to \(n\). It is possible to construct a road between the house of any citizen \(a\) to the house of any other citizen \(b\). There should not be more than one road ... | Each test consists of one or more test cases.The first line contains a single integer \(t\) (\(1 \leq t \leq 2000\)) β the number of test cases.The following lines describe the test cases, each in the following format.The first line contains two integers \(n\) and \(m\) (\(1 \leq n, m \leq 2000\)) β the number of citiz... | Print the answer for all test cases in the order they are given in the input, each in the following format.If there is no way to build the desired road network, print ""NO"" (without quotes).Otherwise in the first line print ""YES"" (without quotes).In the next \(n-1\) lines print the description of the road network: e... | In the first example you can build the following network: It is easy to see that for each community all the houses of its members form a connected subtree. For example, the \(2\)-nd community unites the citizens \(1\), \(2\), \(3\). They form a connected subtree, because if we delete everything except the houses \(1\),... | Input: 24 30011111001113 3011101110 | Output: YES1 32 33 4NO | Master | 3 | 1,230 | 891 | 576 | 10 |
2,036 | A | 2036A | A. Quintomania | 800 | implementation | Boris Notkin composes melodies. He represents them as a sequence of notes, where each note is encoded as an integer from \(0\) to \(127\) inclusive. The interval between two notes \(a\) and \(b\) is equal to \(|a - b|\) semitones.Boris considers a melody perfect if the interval between each two adjacent notes is either... | The first line contains an integer \(t\) (\(1 \leq t \leq 1000\)) β the number of melodies.Each melody is described by two lines. The first line contains an integer \(n\) (\(2 \leq n \leq 50\)) β the number of notes in the melody.The second line contains \(n\) integers \(a_{1}, a_{2}, \dots, a_{n}\) (\(0 \leq a_{i} \le... | For each melody, output ""YES"", if it is perfect; 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. | Input: 82114 109217 10376 83 88838 45 38 80 85 92 99 106563 58 65 58 658117 124 48 53 48 43 54 49595 102 107 114 1211072 77 82 75 70 75 68 75 68 75 | Output: YES YES YES NO YES NO YES YES | Beginner | 1 | 510 | 355 | 237 | 20 | |
1,501 | B | 1501B | B. Napoleon Cake | 900 | dp; implementation; sortings | This week Arkady wanted to cook some pancakes (to follow ancient traditions) and make a problem about that. But then he remembered that one can't make a problem about stacking pancakes without working at a specific IT company, so he decided to bake the Napoleon cake instead.To bake a Napoleon cake, one has to bake \(n\... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 20\,000\)). Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of layers in the cake.The second line of each test ... | For each test case, print a single line with \(n\) integers. The \(i\)-th of the integers should be equal to \(1\) if the \(i\)-th layer from the bottom gets drenched, and \(0\) otherwise. | Input: 3 6 0 3 0 0 1 3 10 0 0 0 1 0 5 0 0 0 2 3 0 0 0 | Output: 1 1 0 1 1 1 0 1 1 1 1 1 0 0 1 1 0 0 0 | Beginner | 3 | 1,047 | 554 | 188 | 15 | |
613 | E | 613E | E. Puzzle Lover | 3,200 | dp; hashing; strings | Oleg Petrov loves crossword puzzles and every Thursday he buys his favorite magazine with crosswords and other word puzzles. In the last magazine Oleg found a curious puzzle, and the magazine promised a valuable prize for it's solution. We give a formal description of the problem below.The puzzle field consists of two ... | The first two lines contain the state of the field for the puzzle. Each of these non-empty lines contains exactly n small English letters.The next line is left empty.The next line is non-empty and contains word w, consisting of small English letters.The length of each line doesn't exceed 2 000. | Print a single integer β the number of distinct solutions for the puzzle modulo 109 + 7. | Input: codeedoccode | Output: 4 | Master | 3 | 1,139 | 295 | 88 | 6 | |
2,122 | A | 2122A | A. Greedy Grid | 800 | constructive algorithms; greedy | A path in a grid is called greedy if it starts at the top-left cell and moves only to the right or downward, always moving to its neighbor with the greater value (or either if the values are equal).The value of a path is the sum of the values of the cells it visits, including the start and end.Does there exist an \(n \... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 5000\)). The description of the test cases follows. The only line of each test case contains two integers \(n\), \(m\) (\(1 \leq n, m \leq 100\)) β the number of rows and columns in the grid, respectively. | For each test case, on a separate line output ""YES"" if the required grid exists, and ""NO"" otherwise.You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses. | In the first test case, an example of a grid in which no greedy path achieves the maximum value out of all down/right paths is: $$$\( \begin{bmatrix} 3 & 5 & 1 \\ 2 & 1 & 2 \\ 5 & 4 & 3 \\ \end{bmatrix} \)\( Let \)a_{i, j}\( denote the value of the cell in the \)i\(-th row and \)j\(-th column. The maximum value of a do... | Input: 23 31 2 | Output: YES NO | Beginner | 2 | 438 | 315 | 265 | 21 |
1,283 | F | 1283F | F. DIY Garland | 2,200 | constructive algorithms; greedy; trees | Polycarp has decided to decorate his room because the New Year is soon. One of the main decorations that Polycarp will install is the garland he is going to solder himself.Simple garlands consisting of several lamps connected by one wire are too boring for Polycarp. He is going to solder a garland consisting of \(n\) l... | The first line contains one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the number of lamps.The second line contains \(n - 1\) integers \(a_1\), \(a_2\), ..., \(a_{n - 1}\) (\(1 \le a_i \le n\)), where \(a_i\) is the index of the main lamp for the \(i\)-th wire (wires are numbered in descending order of importance). | If it is impossible to restore the original scheme, print one integer \(-1\).Otherwise print the scheme as follows. In the first line, print one integer \(k\) (\(1 \le k \le n\)) β the index of the lamp that is connected to the power grid. Then print \(n - 1\) lines, each containing two integers \(x_i\) and \(y_i\) (\(... | The scheme for the first example (R denotes the lamp connected to the grid, the numbers on wires are their importance values): | Input: 6 3 6 3 1 5 | Output: 3 6 3 6 5 1 3 1 4 5 2 | Hard | 3 | 1,946 | 320 | 720 | 12 |
1,934 | B | 1934B | B. Yet Another Coin Problem | 1,200 | brute force; dp; greedy; math | You have \(5\) different types of coins, each with a value equal to one of the first \(5\) triangular numbers: \(1\), \(3\), \(6\), \(10\), and \(15\). These coin types are available in abundance. Your goal is to find the minimum number of these coins required such that their total value sums up to exactly \(n\).We can... | The first line contains one 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 an integer \(n\) (\(1 \leq n \leq 10^9\)) β the target value. | For each test case, output a single number β the minimum number of coins required. | In the first test case, for \(n = 1\), the answer is \(1\) since only one \(1\) value coin is sufficient. \(1 = 1 \cdot 1\).In the fourth test case, for \(n = 5\), the answer is \(3\), which can be achieved using two \(1\) value coins and one \(3\) value coin. \(5 = 2 \cdot 1 + 1 \cdot 3\).In the seventh test case, for... | Input: 14123571112141617182098402931328 | Output: 1 2 1 3 2 2 2 3 2 3 2 2 8 26862090 | Easy | 4 | 356 | 238 | 82 | 19 |
1,423 | A | 1423A | A. Wakanda Forever | 3,500 | In the Kingdom of Wakanda, the 2020 economic crisis has made a great impact on each city and its surrounding area. Cities have made a plan to build a fast train rail between them to boost the economy, but because of the insufficient funds, each city can only build a rail with one other city, and they want to do it toge... | First line contains one integer \(N \;(2 \leq N \leq 10^3)\, \) β the number of cities.Each of the next \(N\) lines contains \(N-1\) integers \(A_{i,1}, A_{i,2}, ..., A_{i,i-1}, A_{i,i+1}, ..., A_{i,N-1}\; (1 \leq A_{i,j} \leq 10^9)\, \) β where \(A_{i,j}\) represents the cost for city \(i\) to build the rail to city \... | Output should contain \(N\) integers \(O_{1}, O_{2}, ..., O_N\), where \(O_i\) represents the city with which city \(i\) should build the rail with, or \(-1\) if it is not possible to find the stable pairing. | Input: 4 35 19 20 76 14 75 23 43 78 14 76 98 | Output: 3 4 1 2 | Master | 0 | 1,013 | 372 | 208 | 14 | ||
1,517 | F | 1517F | F. Reunion | 3,200 | combinatorics; dp; trees | It is reported that the 2050 Conference will be held in Yunqi Town in Hangzhou from April 23 to 25, including theme forums, morning jogging, camping and so on.The relationship between the \(n\) volunteers of the 2050 Conference can be represented by a tree (a connected undirected graph with \(n\) vertices and \(n-1\) e... | The first line contains a single integer \(n\) (\(2\le n\le 300\)) denoting the number of volunteers.Each of the next \(n-1\) lines contains two integers \(a\) and \(b\) denoting an edge between vertex \(a\) and vertex \(b\). | Output the expected level modulo \(998\,244\,353\).Formally, let \(M = 998\,244\,353\). It can be shown that the answer can be expressed as an irreducible fraction \(\frac{p}{q}\), where \(p\) and \(q\) are integers and \(q \not \equiv 0 \pmod{M}\). Output the integer equal to \(p \cdot q^{-1} \bmod M\). In other words... | For the first example, the following table shows all possible outcomes. \(yes\) means the volunteer can attend the on-site reunion and \(no\) means he cannot attend. $$$\(\begin{array}{cccc} 1 & 2 & 3 & level\\ yes & yes & yes & 3\\ yes & yes & no & 1\\ yes & no & yes & 0\\ yes & no & no & 0\\ no & yes & yes & 1\\ no &... | Input: 3 1 2 2 3 | Output: 499122177 | Master | 3 | 1,307 | 225 | 408 | 15 |
1,061 | D | 1061D | D. TV Shows | 2,000 | data structures; greedy; implementation; sortings | There are \(n\) TV shows you want to watch. Suppose the whole time is split into equal parts called ""minutes"". The \(i\)-th of the shows is going from \(l_i\)-th to \(r_i\)-th minute, both ends inclusive.You need a TV to watch a TV show and you can't watch two TV shows which air at the same time on the same TV, so it... | The first line contains integers \(n\), \(x\) and \(y\) (\(1 \le n \le 10^5\), \(1 \le y < x \le 10^9\)) β the number of TV shows, the cost to rent a TV for the first minute and the cost to rent a TV for every subsequent minute.Each of the next \(n\) lines contains two integers \(l_i\) and \(r_i\) (\(1 \le l_i \le r_i ... | Print exactly one integer β the minimum cost to view all the shows taken modulo \(10^9 + 7\). | In the first example, the optimal strategy would be to rent \(3\) TVs to watch: Show \([1, 2]\) on the first TV, Show \([4, 10]\) on the second TV, Shows \([2, 4], [5, 9], [10, 11]\) on the third TV. This way the cost for the first TV is \(4 + 3 \cdot (2 - 1) = 7\), for the second is \(4 + 3 \cdot (10 - 4) = 22\) and f... | Input: 5 4 31 24 102 410 115 9 | Output: 60 | Hard | 4 | 1,184 | 394 | 93 | 10 |
1,408 | D | 1408D | D. Searchlights | 2,000 | binary search; brute force; data structures; dp; implementation; sortings; two pointers | There are \(n\) robbers at coordinates \((a_1, b_1)\), \((a_2, b_2)\), ..., \((a_n, b_n)\) and \(m\) searchlight at coordinates \((c_1, d_1)\), \((c_2, d_2)\), ..., \((c_m, d_m)\). In one move you can move each robber to the right (increase \(a_i\) of each robber by one) or move each robber up (increase \(b_i\) of each... | The first line of input contains two integers \(n\) and \(m\) (\(1 \leq n, m \leq 2000\)): the number of robbers and the number of searchlight.Each of the next \(n\) lines contains two integers \(a_i\), \(b_i\) (\(0 \leq a_i, b_i \leq 10^6\)), coordinates of robbers.Each of the next \(m\) lines contains two integers \(... | Print one integer: the minimum number of moves you need to perform to reach a safe configuration. | In the first test, you can move each robber to the right three times. After that there will be one robber in the coordinates \((3, 0)\).The configuration of the robbers is safe, because the only searchlight can't see the robber, because it is in the coordinates \((2, 3)\) and \(3 > 2\).In the second test, you can move ... | Input: 1 1 0 0 2 3 | Output: 3 | Hard | 7 | 804 | 396 | 97 | 14 |
2,035 | C | 2035C | C. Alya and Permutation | 1,400 | bitmasks; constructive algorithms; math | Alya has been given a hard problem. Unfortunately, she is too busy running for student council. Please solve this problem for her.Given an integer \(n\), construct a permutation \(p\) of integers \(1, 2, \ldots, n\) that maximizes the value of \(k\) (which is initially \(0\)) after the following process.Perform \(n\) o... | 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\) (\(5\le n\le 2 \cdot 10^5\)) β the length of the permutation.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\). | For each test case, output the maximum value of \(k\) in the first line and output the permutation \(p_1, p_2,\ldots, p_n\) in the second line.If there are multiple such permutations, output any. | For the first test case, the value of \(k\) is determined as follows:\(k = 0\) initially. On the \(1\)st operation, \(1\) is odd, so Alya sets \(k\) to be \(k\&p_1 = 0\&2 = 0\). On the \(2\)nd operation, \(2\) is even, so Alya sets \(k\) to be \(k|p_2 = 0|1 = 1\). On the \(3\)rd operation, \(3\) is odd, so Alya sets \(... | Input: 65678910 | Output: 5 2 1 3 4 5 7 1 2 4 6 5 3 7 2 4 5 1 3 6 7 15 2 4 5 1 3 6 7 8 9 2 4 5 6 7 1 3 8 9 15 1 2 3 4 5 6 8 10 9 7 | Easy | 3 | 544 | 311 | 195 | 20 |
985 | F | 985F | F. Isomorphic Strings | 2,300 | hashing; strings | You are given a string s of length n consisting of lowercase English letters.For two given strings s and t, say S is the set of distinct characters of s and T is the set of distinct characters of t. The strings s and t are isomorphic if their lengths are equal and there is a one-to-one mapping (bijection) f between S a... | The first line contains two space-separated integers n and m (1 β€ n β€ 2Β·105, 1 β€ m β€ 2Β·105) β the length of the string s and the number of queries.The second line contains string s consisting of n lowercase English letters.The following m lines contain a single query on each line: xi, yi and leni (1 β€ xi, yi β€ n, 1 β€ l... | For each query in a separate line print ""YES"" if substrings s[xi... xi + leni - 1] and s[yi... yi + leni - 1] are isomorphic and ""NO"" otherwise. | The queries in the example are following: substrings ""a"" and ""a"" are isomorphic: f(a) = a; substrings ""ab"" and ""ca"" are isomorphic: f(a) = c, f(b) = a; substrings ""bac"" and ""aba"" are not isomorphic since f(b) and f(c) must be equal to a at same time; substrings ""bac"" and ""cab"" are isomorphic: f(b) = c, ... | Input: 7 4abacaba1 1 11 4 22 1 32 4 3 | Output: YESYESNOYES | Expert | 2 | 926 | 404 | 148 | 9 |
1,326 | E | 1326E | E. Bombs | 2,400 | data structures; two pointers | You are given a permutation, \(p_1, p_2, \ldots, p_n\).Imagine that some positions of the permutation contain bombs, such that there exists at least one position without a bomb.For some fixed configuration of bombs, consider the following process. Initially, there is an empty set, \(A\).For each \(i\) from \(1\) to \(n... | The first line contains a single integer, \(n\) (\(2 \leq n \leq 300\,000\)).The second line contains \(n\) distinct integers \(p_1, p_2, \ldots, p_n\) (\(1 \leq p_i \leq n)\).The third line contains \(n\) distinct integers \(q_1, q_2, \ldots, q_n\) (\(1 \leq q_i \leq n)\). | Print \(n\) space-separated integers, such that the \(i\)-th of them equals the cost of a configuration of bombs in positions \(q_1, q_2, \ldots, q_{i-1}\). | In the first test: If there are no bombs, \(A\) is equal to \(\{1, 2, 3\}\) at the end of the process, so the cost of the configuration is \(3\). If there is one bomb in position \(1\), \(A\) is equal to \(\{1, 2\}\) at the end of the process, so the cost of the configuration is \(2\); If there are two bombs in positio... | Input: 3 3 2 1 1 2 3 | Output: 3 2 1 | Expert | 2 | 965 | 274 | 156 | 13 |
1,952 | J | 1952J | J. Help, what does it mean to be ""Based"" | 0 | *special; brute force; constructive algorithms; expression parsing; implementation; sortings | Sup, gimme a single integer \(x\) (\(1 \le x \le 4\)) homie uwu :3 | If \(x = 1\), spit out some based code that, like when ya feed it two integers \(a\) and \(b\), it hands over their sum.If \(x = 2\), slap some based code that takes an integer \(a\) and returns its absolute value like magic, no cap.If \(x = 3\), check this out: blaze some based code that grabs an integer \(n\) (\(1 \l... | Slide into the ""My Submissions"" page and vibe check ur code by clicking on its ID. You'll see just how based or cringe it is. | Input: 1 | Output: yoink a yoink b *slaps a on top of b* yeet b go touch some grass | Beginner | 6 | 0 | 66 | 667 | 19 | |
1,245 | D | 1245D | D. Shichikuji and Power Grid | 1,900 | dsu; graphs; greedy; shortest paths; trees | Shichikuji is the new resident deity of the South Black Snail Temple. Her first job is as follows:There are \(n\) new cities located in Prefecture X. Cities are numbered from \(1\) to \(n\). City \(i\) is located \(x_i\) km North of the shrine and \(y_i\) km East of the shrine. It is possible that \((x_i, y_i) = (x_j, ... | First line of input contains a single integer \(n\) (\(1 \leq n \leq 2000\)) β the number of cities.Then, \(n\) lines follow. The \(i\)-th line contains two space-separated integers \(x_i\) (\(1 \leq x_i \leq 10^6\)) and \(y_i\) (\(1 \leq y_i \leq 10^6\)) β the coordinates of the \(i\)-th city.The next line contains \(... | In the first line print a single integer, denoting the minimum amount of yen needed.Then, print an integer \(v\) β the number of power stations to be built.Next, print \(v\) space-separated integers, denoting the indices of cities in which a power station will be built. Each number should be from \(1\) to \(n\) and all... | For the answers given in the samples, refer to the following diagrams (cities with power stations are colored green, other cities are colored blue, and wires are colored red):For the first example, the cost of building power stations in all cities is \(3 + 2 + 3 = 8\). It can be shown that no configuration costs less t... | Input: 3 2 3 1 1 3 2 3 2 3 3 2 3 | Output: 8 3 1 2 3 0 | Hard | 5 | 1,941 | 567 | 963 | 12 |
1,505 | E | 1505E | E. Cakewalk | 1,800 | *special; greedy; implementation; shortest paths | A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't.The mouse is in a bit of a hurry, though, so once she enters the cake from its northw... | The first line of input contains two integers \(H\) and \(W\) (\(1 \le H, W \le 5\)), separated by a space, β the height and the width of the cake.The next \(H\) lines contain a string of \(W\) characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square... | Output the number of berries the mouse will eat following her strategy. | Input: 4 3 *.. .*. ..* ... | Output: 3 | Medium | 4 | 756 | 334 | 71 | 15 | |
1,662 | M | 1662M | M. Bottle Arrangements | 0 | constructive algorithms | Gabriella has been instructed to organize a renowned wine tasting event which will be attended by \(m\) critics. On display, there will be \(n\) different varieties of wine, each of which can either be a red wine or a white wine.The wines will come in \(n\) bottles arranged in a line on the table, and, for convenience,... | Each test contains multiple test cases. The first line contains an integer \(t\) (\(1\le t\le 100\)) β the number of test cases. The descriptions of the \(t\) test cases follow.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n \le 100\), \(1 \le m \le 100\)) β the number of bottles of wi... | For each test case, if at least one solution exists, print a string of length \(n\) made up of the characters R and W, where the \(j\)-th character (\(1 \le j \le n\)) denotes the type of the wine in the \(j\)-th bottle of the arrangement (R for red and W for white). If there are multiple solutions, print any.If no sol... | In the first test case, there are \(n = 5\) bottles of wine to be arranged and \(m = 3\) critics. The arrangement RWRRW satisfies the requests of all three critics. Indeed: the first critic can choose the interval \([3, \, 3]\), which contains exactly one bottle of red wine (note that \([1, \, 1]\) and \([4, \, 4]\) ar... | Input: 35 31 03 22 24 32 11 10 33 20 20 3 | Output: RWRRW IMPOSSIBLE WWW | Beginner | 1 | 1,053 | 549 | 362 | 16 |
178 | F3 | 178F3 | F3. Representative Sampling | 2,500 | The Smart Beaver from ABBYY has a long history of cooperating with the ""Institute of Cytology and Genetics"". Recently, the Institute staff challenged the Beaver with a new problem. The problem is as follows.There is a collection of n proteins (not necessarily distinct). Each protein is a string consisting of lowercas... | The first input line contains two integers n and k (1 β€ k β€ n), separated by a single space. The following n lines contain the descriptions of proteins, one per line. Each protein is a non-empty string of no more than 500 characters consisting of only lowercase Latin letters (a...z). Some of the strings may be equal.Th... | Print a single number denoting the largest possible value of representativity that a subcollection of size k of the given collection of proteins can have. | Input: 3 2ababzdabq | Output: 2 | Expert | 0 | 1,476 | 501 | 154 | 1 | ||
900 | D | 900D | D. Unusual Sequences | 2,000 | bitmasks; combinatorics; dp; math; number theory | Count the number of distinct sequences a1, a2, ..., an (1 β€ ai) consisting of positive integers such that gcd(a1, a2, ..., an) = x and . As this number could be large, print the answer modulo 109 + 7.gcd here means the greatest common divisor. | The only line contains two positive integers x and y (1 β€ x, y β€ 109). | Print the number of such sequences modulo 109 + 7. | There are three suitable sequences in the first test: (3, 3, 3), (3, 6), (6, 3).There are no suitable sequences in the second test. | Input: 3 9 | Output: 3 | Hard | 5 | 243 | 70 | 50 | 9 |
725 | B | 725B | B. Food on the Plane | 1,200 | implementation; math | A new airplane SuperPuperJet has an infinite number of rows, numbered with positive integers starting with 1 from cockpit to tail. There are six seats in each row, denoted with letters from 'a' to 'f'. Seats 'a', 'b' and 'c' are located to the left of an aisle (if one looks in the direction of the cockpit), while seats... | The only line of input contains a description of Vasya's seat in the format ns, where n (1 β€ n β€ 1018) is the index of the row and s is the seat in this row, denoted as letter from 'a' to 'f'. The index of the row and the seat are not separated by a space. | Print one integer β the number of seconds Vasya has to wait until he gets his lunch. | In the first sample, the first flight attendant serves Vasya first, so Vasya gets his lunch after 1 second.In the second sample, the flight attendants will spend 6 seconds to serve everyone in the rows 1 and 3, then they will move one row forward in 1 second. As they first serve seats located to the right of the aisle ... | Input: 1f | Output: 1 | Easy | 2 | 1,695 | 256 | 84 | 7 |
958 | E1 | 958E1 | E1. Guard Duty (easy) | 1,600 | brute force; geometry; greedy; math | The Rebel fleet is afraid that the Empire might want to strike back again. Princess Heidi needs to know if it is possible to assign R Rebel spaceships to guard B bases so that every base has exactly one guardian and each spaceship has exactly one assigned base (in other words, the assignment is a perfect matching). Sin... | The first line contains two space-separated integers R, B(1 β€ R, B β€ 10). For 1 β€ i β€ R, the i + 1-th line contains two space-separated integers xi and yi (|xi|, |yi| β€ 10000) denoting the coordinates of the i-th Rebel spaceship. The following B lines have the same format, denoting the position of bases. It is guarante... | If it is possible to connect Rebel spaceships and bases so as satisfy the constraint, output Yes, otherwise output No (without quote). | For the first example, one possible way is to connect the Rebels and bases in order.For the second example, there is no perfect matching between Rebels and bases. | Input: 3 30 02 03 1-2 10 32 2 | Output: Yes | Medium | 4 | 547 | 397 | 134 | 9 |
570 | C | 570C | C. Replacement | 1,600 | constructive algorithms; data structures; implementation | Daniel has a string s, consisting of lowercase English letters and period signs (characters '.'). Let's define the operation of replacement as the following sequence of steps: find a substring "".."" (two consecutive periods) in string s, of all occurrences of the substring let's choose the first one, and replace this ... | The first line contains two integers n and m (1 β€ n, m β€ 300 000) the length of the string and the number of queries.The second line contains string s, consisting of n lowercase English letters and period signs.The following m lines contain the descriptions of queries. The i-th line contains integer xi and ci (1 β€ xi β€... | Print m numbers, one per line, the i-th of these numbers must be equal to the value of f(s) after performing the i-th assignment. | Note to the first sample test (replaced periods are enclosed in square brackets).The original string is "".b..bz...."". after the first query f(hb..bz....) = 4 (""hb[..]bz...."" β ""hb.bz[..].."" β ""hb.bz[..]."" β ""hb.bz[..]"" β ""hb.bz."") after the second query f(hbΡ.bz....) = 3 (""hbΡ.bz[..].."" β ""hbΡ.bz[..]."" ... | Input: 10 3.b..bz....1 h3 c9 f | Output: 431 | Medium | 3 | 910 | 433 | 129 | 5 |
1,836 | A | 1836A | A. Destroyer | 800 | implementation; sortings | John is a lead programmer on a destroyer belonging to the space navy of the Confederacy of Independent Operating Systems. One of his tasks is checking if the electronic brains of robots were damaged during battles.A standard test is to order the robots to form one or several lines, in each line the robots should stand ... | The first line contains a single integer \(t\) (\(1 \leq t \leq 100\)), denoting the number of test cases.The first line in each test case contains a single integer \(n\) (\(1 \le n \le 100\)) β the number of robots.The second line in each test case contains \(n\) integers \(l_1, l_2, \ldots, l_n\) (\(0 \leq l_i < 100\... | For each test case, output ""YES"", if there exists a robot arrangement consistent with robots' reports. 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. | Example arrangement consistent with robot statements from the first example test case: Example arrangement consistent with robot statements from the second example is shown in the statement.In the third test case, the third robot claims that there are two machines in front of it. In such a case, the robot directly in f... | Input: 560 1 2 0 1 090 0 0 0 1 1 1 2 230 0 219950 1 2 3 4 | Output: YES YES NO NO YES | Beginner | 2 | 802 | 466 | 291 | 18 |
741 | B | 741B | B. Arpa's weak amphitheater and Mehrdad's valuable Hoses | 1,600 | dfs and similar; dp; dsu | Just to remind, girls in Arpa's land are really nice.Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi. Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are in the same friendship group if and only if t... | The first line contains integers n, m and w (1 β€ n β€ 1000, , 1 β€ w β€ 1000) β the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited.The second line contains n integers w1, w2, ..., wn (1 β€ wi β€ 1000) β the weights of the Hoses.The third line contains n integers b1, b2, ... | Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed w. | In the first sample there are two friendship groups: Hoses {1, 2} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and sum of their beauty is 6.In the second sample there are two friendship groups: Hoses {1, 2, 3} and Hos {4}. Mehrdad can't invite all the Hoses ... | Input: 3 1 53 2 52 4 21 2 | Output: 6 | Medium | 3 | 1,000 | 603 | 131 | 7 |
1,552 | A | 1552A | A. Subsequence Permutation | 800 | sortings; strings | A string \(s\) of length \(n\), consisting of lowercase letters of the English alphabet, is given.You must choose some number \(k\) between \(0\) and \(n\). Then, you select \(k\) characters of \(s\) and permute them however you want. In this process, the positions of the other \(n-k\) characters remain unchanged. You ... | The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. Then \(t\) test cases follow.The first line of each test case contains one integer \(n\) (\(1 \le n \le 40\)) β the length of the string.The second line of each test case contains the string \(s\). It is guaranteed that \(... | For each test case, output the minimum \(k\) that allows you to obtain a string sorted alphabetically, through the operation described above. | In the first test case, we can choose the \(k=2\) characters \(\texttt{""_ol""}\) and rearrange them as \(\texttt{""_lo""}\) (so the resulting string is \(\texttt{""llo""}\)). It is not possible to sort the string choosing strictly less than \(2\) characters.In the second test case, one possible way to sort \(s\) is to... | Input: 4 3 lol 10 codeforces 5 aaaaa 4 dcba | Output: 2 6 0 4 | Beginner | 2 | 741 | 380 | 141 | 15 |
1,569 | C | 1569C | C. Jury Meeting | 1,500 | combinatorics; math | \(n\) people gathered to hold a jury meeting of the upcoming competition, the \(i\)-th member of the jury came up with \(a_i\) tasks, which they want to share with each other.First, the jury decides on the order which they will follow while describing the tasks. Let that be a permutation \(p\) of numbers from \(1\) to ... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of the test case contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β number of jury members.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) β the number ... | For each test case, print one integer β the number of nice permutations, taken modulo \(998\,244\,353\). | Explanation of the first test case from the example:There are two possible permutations, \(p = [1, 2]\) and \(p = [2, 1]\). For \(p = [1, 2]\), the process is the following: the first jury member tells a task; the second jury member tells a task; the first jury member doesn't have any tasks left to tell, so they are sk... | Input: 4 2 1 2 3 5 5 5 4 1 3 3 7 6 3 4 2 1 3 3 | Output: 1 6 0 540 | Medium | 2 | 1,132 | 452 | 104 | 15 |
1,015 | F | 1015F | F. Bracket Substring | 2,300 | dp; strings | You are given a bracket sequence \(s\) (not necessarily a regular one). A bracket sequence is a string containing only characters '(' and ')'.A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters ... | The first line of the input contains one integer \(n\) (\(1 \le n \le 100\)) β the half-length of the resulting regular bracket sequences (the resulting sequences must have length equal to \(2n\)).The second line of the input contains one string \(s\) (\(1 \le |s| \le 200\)) β the string \(s\) that should be a substrin... | Print only one integer β the number of regular bracket sequences containing the given bracket sequence \(s\) as a substring. Since this number can be huge, print it modulo \(10^9+7\) (\(1000000007\)). | All regular bracket sequences satisfying the conditions above for the first example: ""(((()))())""; ""((()()))()""; ""((()))()()""; ""(()(()))()""; ""()((()))()"". All regular bracket sequences satisfying the conditions above for the second example: ""((()))""; ""(()())""; ""(())()""; ""()(())"". And there is no regul... | Input: 5()))() | Output: 5 | Expert | 2 | 719 | 406 | 200 | 10 |
1,539 | C | 1539C | C. Stable Groups | 1,200 | greedy; sortings | There are \(n\) students numerated from \(1\) to \(n\). The level of the \(i\)-th student is \(a_i\). You need to split the students into stable groups. A group of students is called stable, if in the sorted array of their levels no two neighboring elements differ by more than \(x\).For example, if \(x = 4\), then the ... | The first line contains three integers \(n\), \(k\), \(x\) (\(1 \le n \le 200\,000\), \(0 \le k \le 10^{18}\), \(1 \le x \le 10^{18}\)) β the initial number of students, the number of students you can additionally invite, and the maximum allowed level difference.The second line contains \(n\) integers \(a_1, a_2, \dots... | In the only line print a single integer: the minimum number of stable groups you can split the students into. | In the first example you can invite two students with levels \(2\) and \(11\). Then you can split the students into two stable groups: \([1, 1, 2, 5, 8, 11, 12, 13]\), \([20, 22]\). In the second example you are not allowed to invite new students, so you need \(3\) groups: \([1, 1, 5, 5, 20, 20]\) \([60, 70, 70, 70, 80... | Input: 8 2 3 1 1 5 8 12 13 20 22 | Output: 2 | Easy | 2 | 964 | 378 | 109 | 15 |
1,070 | M | 1070M | M. Algoland and Berland | 3,000 | constructive algorithms; divide and conquer; geometry | Once upon a time Algoland and Berland were a single country, but those times are long gone. Now they are two different countries, but their cities are scattered on a common territory.All cities are represented as points on the Cartesian plane. Algoland consists of \(a\) cities numbered from \(1\) to \(a\). The coordina... | Input contains one or several test cases. The first input line contains a single integer number \(t\) (\(1 \le t \le 3000\)) β number of test cases. Then, \(t\) test cases follow. Solve test cases separately, test cases are completely independent and do not affect each other.Each test case starts with a line containing... | For each of the \(t\) test cases, first print ""YES"" if there is an answer or ""NO"" otherwise.If there is an answer, print the freeway building plan in the next \(a+b-1\) lines. Each line of the plan should contain two space-separated integers \(j\) and \(i\) which means that a freeway from the \(j\)-th Berland city ... | Input: 22 31 1 20 01 11 23 24 01 110 00 1 | Output: YES2 21 23 23 1YES1 1 | Master | 3 | 1,978 | 1,246 | 410 | 10 | |
1,373 | G | 1373G | G. Pawns | 2,600 | data structures; divide and conquer; greedy | You are given a chessboard consisting of \(n\) rows and \(n\) columns. Rows are numbered from bottom to top from \(1\) to \(n\). Columns are numbered from left to right from \(1\) to \(n\). The cell at the intersection of the \(x\)-th column and the \(y\)-th row is denoted as \((x, y)\). Furthermore, the \(k\)-th colum... | The first line contains three integers \(n\), \(k\) and \(m\) (\(1 \le n, m \le 2 \cdot 10^5; 1 \le k \le n\)) β the size of the board, the index of the special column and the number of changes respectively.Then \(m\) lines follow. The \(i\)-th line contains two integers \(x\) and \(y\) (\(1 \le x, y \le n\)) β the ind... | After each change print one integer β the minimum number of rows which you have to add to make the board good. | Input: 5 3 5 4 4 3 5 2 4 3 4 3 5 | Output: 0 1 2 2 1 | Expert | 3 | 1,125 | 501 | 110 | 13 | |
2,112 | C | 2112C | C. Coloring Game | 1,300 | binary search; brute force; greedy; two pointers | Alice and Bob are playing a game using an integer array \(a\) of size \(n\).Initially, all elements of the array are colorless. First, Alice chooses \(3\) elements and colors them red. Then Bob chooses any element and colors it blue (if it was red β recolor it). Alice wins if the sum of the red elements is strictly gre... | The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(3 \le n \le 5000\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_1 \le a_2 \le \cdots \le a_n \le 10^5\)).Additional constra... | For each test case, print a single integer β the number of ways that Alice can choose \(3\) elements in order to win regardless of Bob's actions. | In the first two test cases, no matter which three elements Alice chooses, Bob will be able to paint one element blue so that Alice does not win.In the third test case, Alice can choose any three elements. If Bob colors one of the red elements, the sum of red elements will be \(14\), and the sum of blue elements will b... | Input: 631 2 341 1 2 457 7 7 7 751 1 2 2 462 3 3 4 5 551 1 1 1 3 | Output: 0 0 10 2 16 0 | Easy | 4 | 486 | 399 | 145 | 21 |
1,332 | C | 1332C | C. K-Complete Word | 1,500 | dfs and similar; dsu; greedy; implementation; strings | Word \(s\) of length \(n\) is called \(k\)-complete if \(s\) is a palindrome, i.e. \(s_i=s_{n+1-i}\) for all \(1 \le i \le n\); \(s\) has a period of \(k\), i.e. \(s_i=s_{k+i}\) for all \(1 \le i \le n-k\). For example, ""abaaba"" is a \(3\)-complete word, while ""abccba"" is not.Bob is given a word \(s\) of length \(n... | The first line contains a single integer \(t\) (\(1 \le t\le 10^5\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le k < n \le 2 \cdot 10^5\), \(n\) is divisible by \(k\)).The second line of each test case contains a word \(s\) of length \(n\).It is guaranteed ... | For each test case, output one integer, representing the minimum number of characters he has to replace to convert \(s\) to any \(k\)-complete word. | In the first test case, one optimal solution is aaaaaa.In the second test case, the given word itself is \(k\)-complete. | Input: 4 6 2 abaaba 6 3 abaaba 36 9 hippopotomonstrosesquippedaliophobia 21 7 wudixiaoxingxingheclp | Output: 2 0 23 16 | Medium | 5 | 870 | 471 | 148 | 13 |
1,139 | C | 1139C | C. Edgy Trees | 1,500 | dfs and similar; dsu; graphs; math; trees | You are given a tree (a connected undirected graph without cycles) of \(n\) vertices. Each of the \(n - 1\) edges of the tree is colored in either black or red.You are also given an integer \(k\). Consider sequences of \(k\) vertices. Let's call a sequence \([a_1, a_2, \ldots, a_k]\) good if it satisfies the following ... | The first line contains two integers \(n\) and \(k\) (\(2 \le n \le 10^5\), \(2 \le k \le 100\)), the size of the tree and the length of the vertex sequence.Each of the next \(n - 1\) lines contains three integers \(u_i\), \(v_i\) and \(x_i\) (\(1 \le u_i, v_i \le n\), \(x_i \in \{0, 1\}\)), where \(u_i\) and \(v_i\) d... | Print the number of good sequences modulo \(10^9 + 7\). | In the first example, all sequences (\(4^4\)) of length \(4\) except the following are good: \([1, 1, 1, 1]\) \([2, 2, 2, 2]\) \([3, 3, 3, 3]\) \([4, 4, 4, 4]\) In the second example, all edges are red, hence there aren't any good sequences. | Input: 4 4 1 2 1 2 3 1 3 4 1 | Output: 252 | Medium | 5 | 1,111 | 458 | 55 | 11 |
460 | B | 460B | B. Little Dima and Equation | 1,500 | brute force; implementation; math; number theory | Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 109) of the equation:x = bΒ·s(x)a + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the d... | The first line contains three space-separated integers: a, b, c (1 β€ a β€ 5; 1 β€ b β€ 10000; - 10000 β€ c β€ 10000). | Print integer n β the number of the solutions that you've found. Next print n integers in the increasing order β the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 109. | Input: 3 2 8 | Output: 310 2008 13726 | Medium | 4 | 561 | 112 | 232 | 4 | |
35 | D | 35D | D. Animals | 1,700 | dp; greedy | Once upon a time DravDe, an outstanding person famous for his professional achievements (as you must remember, he works in a warehouse storing Ogudar-Olok, a magical but non-alcoholic drink) came home after a hard day. That day he had to drink 9875 boxes of the drink and, having come home, he went to bed at once.DravDe... | The first input line contains integers n and X (1 β€ n β€ 100, 1 β€ X β€ 104) β amount of days in DravDeβs dream and the total amount of food (in tons) that was there initially. The second line contains integers ci (1 β€ ci β€ 300). Numbers in the second line are divided by a space. | Output the only number β the maximum possible amount of animals on the farm by the end of the n-th day given that the food was enough for everybody. | Note to the first example: DravDe leaves the second and the third animal on the farm. The second animal will eat one ton of food on the second day and one ton on the third day. The third animal will eat one ton of food on the third day. | Input: 3 41 1 1 | Output: 2 | Medium | 2 | 1,442 | 277 | 148 | 0 |
827 | E | 827E | E. Rusty String | 2,700 | fft; math; strings | Grigory loves strings. Recently he found a metal strip on a loft. The strip had length n and consisted of letters ""V"" and ""K"". Unfortunately, rust has eaten some of the letters so that it's now impossible to understand which letter was written.Grigory couldn't understand for a long time what these letters remind hi... | There are several (at least one) test cases in the input. The first line contains single integer β the number of test cases.There is an empty line before each test case. Each test case is described in two lines: the first line contains single integer n (1 β€ n β€ 5Β·105) β the length of the string, the second line contain... | For each test case print two lines. In the first line print the number of possible periods after we replace each unreadable letter with ""V"" or ""K"". In the next line print all these values in increasing order. | In the first test case from example we can obtain, for example, ""VKKVK"", which has periods 3 and 5.In the second test case we can obtain ""VVVVVV"" which has all periods from 1 to 6.In the third test case string ""KVKV"" has periods 2 and 4, and string ""KVKK"" has periods 3 and 4. | Input: 3 5V??VK 6?????? 4?VK? | Output: 23 561 2 3 4 5 632 3 4 | Master | 3 | 739 | 596 | 212 | 8 |
2,045 | D | 2045D | D. Aquatic Dragon | 3,500 | You live in an archipelago consisting of \(N\) islands (numbered from \(1\) to \(N\)) laid out in a single line. Island \(i\) is adjacent to island \(i+1\), for \(1 \leq i < N\). Between adjacent islands \(i\) and \(i+1\), there is a pair of one-directional underwater tunnels: one that allows you to walk from island \(... | The first line consists of five integers \(N\) \(D\) \(T_s\) \(T_f\) \(T_w\) (\(2 \leq N \leq 200\,000; 1 \leq D, T_s, T_f, T_w \leq 200\,000\)).The second line consists of \(N\) integers \(P_i\) (\(1 \leq P_i \leq 200\,000)\). | Output an integer in a single line representing the minimum possible time to go to island \(N\) with your dragon. | Explanation for the sample input/output #1The following sequence of events will complete your mission in the minimum time. The shrine on island \(1\) increases your dragon's stamina to \(1\). Fly with your dragon to island \(2\). The shrine on island \(2\) increases your dragon's stamina to \(2\). Walk alone to island ... | Input: 5 4 2 9 1 1 2 4 2 1 | Output: 28 | Master | 0 | 1,866 | 227 | 113 | 20 | |
599 | E | 599E | E. Sandy and Nuts | 2,600 | bitmasks; dp; trees | Rooted tree is a connected graph without any simple cycles with one vertex selected as a root. In this problem the vertex number 1 will always serve as a root.Lowest common ancestor of two vertices u and v is the farthest from the root vertex that lies on both the path from u to the root and on path from v to the root.... | The first line of the input contains three integers n, m and q (1 β€ n β€ 13, 0 β€ m < n, 0 β€ q β€ 100) β the number of vertices, the number of edges and LCA triples remembered by Sandy respectively.Each of the next m lines contains two integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the numbers of vertices connected by the... | Print a single integer β the number of trees of size n that satisfy all the conditions. | In the second sample correct answer looks like this: In the third sample there are two possible trees: In the fourth sample the answer is 0 because the information about LCA is inconsistent. | Input: 4 0 0 | Output: 16 | Expert | 3 | 963 | 621 | 87 | 5 |
838 | A | 838A | A. Binary Blocks | 1,400 | brute force | You are given an image, that can be represented with a 2-d n by m grid of pixels. Each pixel of the image is either on or off, denoted by the characters ""0"" or ""1"", respectively. You would like to compress this image. You want to choose an integer k > 1 and split the image into k by k blocks. If n and m are not div... | The first line of input will contain two integers n, m (2 β€ n, m β€ 2 500), the dimensions of the image.The next n lines of input will contain a binary string with exactly m characters, representing the image. | Print a single integer, the minimum number of pixels needed to toggle to make the image compressible. | We first choose k = 2.The image is padded as follows: 001000101100110010000000We can toggle the image to look as follows: 001100001100000000000000We can see that this image is compressible for k = 2. | Input: 3 5001001011011001 | Output: 5 | Easy | 1 | 872 | 208 | 101 | 8 |
1,023 | E | 1023E | E. Down or Right | 2,100 | constructive algorithms; interactive; matrices | This is an interactive problem.Bob lives in a square grid of size \(n \times n\), with rows numbered \(1\) through \(n\) from top to bottom, and columns numbered \(1\) through \(n\) from left to right. Every cell is either allowed or blocked, but you don't know the exact description of the grid. You are given only an i... | The only line of the input contains an integer \(n\) (\(2 \le n \le 500\)) β the size of the grid. | When you are ready to print the answer, print a single line containing ""! S"" where where \(S\) is a string of length \(2 \cdot n - 2\) consisting of characters 'D' and 'R', denoting moves down and right respectively. The path should be a valid path going from the cell \((1, 1)\) to the cell \((n, n)\) passing only th... | The first example is shown on the picture below. To hack, use the following input format:The first line should contain a single integer \(n\) (\(2 \le n \le 500\)) β the size of the grid.Each of the next \(n\) lines should contain a string of \(n\) characters '#' or '.', where '#' denotes a blocked cell, and '.' denote... | Input: 4 YES NO YES YES | Output: ? 1 1 4 4 ? 1 2 4 3 ? 4 1 4 4 ? 1 4 4 4 ! RDRRDD | Hard | 3 | 1,662 | 98 | 340 | 10 |
1,812 | C | 1812C | C. Digits | 0 | *special | The first line contains a single integer \(t\) (\(1 \le t \le 32\)) β the number of test cases.Each test case contains \(n\) lines. The \(i\)-th line contains an integer \(a_i\) (\(1 \le a_i \le 9\)).The sum of \(n\) over all test cases doesn't exceed \(155\). | For each test case, print one integer β the answer. | Input: 3 2 1 4 7 1 2 3 5 | Output: 8 7 30 | Beginner | 1 | 0 | 260 | 51 | 18 | ||
2,071 | C | 2071C | C. Trapmigiano Reggiano | 1,700 | constructive algorithms; data structures; dfs and similar; dp; greedy; sortings; trees | In an Italian village, a hungry mouse starts at vertex \(\textrm{st}\) on a given tree\(^{\text{β}}\) with \(n\) vertices.Given a permutation \(p\) of length \(n\)\(^{\text{β }}\), there are \(n\) steps. For the \(i\)-th step: A tempting piece of Parmesan cheese appears at vertex \(p_i\). If the mouse is currently at ve... | 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 three integers \(n\), \(\textrm{st}\), and \(\textrm{en}\) (\(1 \le n \le 10^5\); \(1 \le \textrm{st}, \textrm{en} \... | For each test case: If no such permutation exists, output \(-1\). Otherwise, output \(n\) integers \(p_1, p_2, \ldots, p_n\), representing the order in which the cheese will appear at the vertices, ensuring the mouse will eventually be caught at vertex \(\textrm{en}\). If there are multiple solutions, print any of them... | In the first test case, there is only one permutation with length \(n = 1\) that is \(p = [1]\), which successfully catches the mouse:$$$\(\textrm{st} = 1 \overset{p_1 = 1}{\xrightarrow{\hspace{1.3cm}}} 1 = \textrm{en}.\)\(In the second test case, one possible permutation with length \)n = 2\( is \)p = [1, 2]\(:\)\(\te... | Input: 41 1 12 1 21 23 2 21 22 36 1 41 21 34 55 61 4 | Output: 1 1 2 3 1 2 1 4 3 2 6 5 | Medium | 7 | 1,142 | 700 | 321 | 20 |
1,310 | B | 1310B | B. Double Elimination | 2,500 | dp; implementation | The biggest event of the year β Cota 2 world championship ""The Innernational"" is right around the corner. \(2^n\) teams will compete in a double-elimination format (please, carefully read problem statement even if you know what is it) to identify the champion. Teams are numbered from \(1\) to \(2^n\) and will play ga... | First input line has two integers \(n, k\) β \(2^n\) teams are competing in the championship. You are a fan of \(k\) teams (\(2 \le n \le 17; 0 \le k \le 2^n\)).Second input line has \(k\) distinct integers \(a_1, \ldots, a_k\) β numbers of teams you're a fan of (\(1 \le a_i \le 2^n\)). | Output single integer β maximal possible number of championship games that include teams you're fan of. | On the image, each game of the championship is denoted with an English letter (\(a\) to \(n\)). Winner of game \(i\) is denoted as \(Wi\), loser is denoted as \(Li\). Teams you're a fan of are highlighted with red background.In the first example, team \(6\) will play in 6 games if it looses the first upper bracket game... | Input: 3 1 6 | Output: 6 | Expert | 2 | 1,654 | 287 | 103 | 13 |
815 | B | 815B | B. Karen and Test | 2,200 | brute force; combinatorics; constructive algorithms; math | Karen has just arrived at school, and she has a math test today! The test is about basic addition and subtraction. Unfortunately, the teachers were too busy writing tasks for Codeforces rounds, and had no time to make an actual test. So, they just put one question in the test that is worth all the points.There are n in... | The first line of input contains a single integer n (1 β€ n β€ 200000), the number of numbers written on the first row.The next line contains n integers. Specifically, the i-th one among these is ai (1 β€ ai β€ 109), the i-th number on the first row. | Output a single integer on a line by itself, the number on the final row after performing the process above.Since this number can be quite large, print only the non-negative remainder after dividing it by 109 + 7. | In the first test case, the numbers written on the first row are 3, 6, 9, 12 and 15.Karen performs the operations as follows: The non-negative remainder after dividing the final number by 109 + 7 is still 36, so this is the correct output.In the second test case, the numbers written on the first row are 3, 7, 5 and 2.K... | Input: 53 6 9 12 15 | Output: 36 | Hard | 4 | 1,222 | 246 | 213 | 8 |
442 | D | 442D | D. Adam and Tree | 2,600 | data structures; trees | When Adam gets a rooted tree (connected non-directed graph without cycles), he immediately starts coloring it. More formally, he assigns a color to each edge of the tree so that it meets the following two conditions: There is no vertex that has more than two incident edges painted the same color. For any two vertexes t... | The first line contains integer n (1 β€ n β€ 106) β the number of times a new vertex is added. The second line contains n numbers pi (1 β€ pi β€ i) β the numbers of the vertexes to which we add another vertex. | Print n integers β the minimum costs of the tree painting after each addition. | The figure below shows one of the possible variants to paint a tree from the sample at the last moment. The cost of the vertexes with numbers 11 and 12 equals 3. | Input: 111 1 1 3 4 4 7 3 7 6 6 | Output: 1 1 1 1 1 2 2 2 2 2 3 | Expert | 2 | 1,091 | 205 | 78 | 4 |
1,651 | B | 1651B | B. Prove Him Wrong | 800 | constructive algorithms; greedy | Recently, your friend discovered one special operation on an integer array \(a\): Choose two indices \(i\) and \(j\) (\(i \neq j\)); Set \(a_i = a_j = |a_i - a_j|\). After playing with this operation for a while, he came to the next conclusion: For every array \(a\) of \(n\) integers, where \(1 \le a_i \le 10^9\), you ... | The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. Then \(t\) test cases follow.The first and only line of each test case contains a single integer \(n\) (\(2 \le n \le 1000\)) β the length of array \(a\). | For each test case, if there is no counterexample array \(a\) of size \(n\), print NO.Otherwise, print YES followed by the array \(a\) itself (\(1 \le a_i \le 10^9\)). If there are multiple counterexamples, print any. | In the first test case, the only possible pairs of indices are \((1, 2)\) and \((2, 1)\).If you perform the operation on indices \((1, 2)\) (or \((2, 1)\)), you'll get \(a_1 = a_2 = |1 - 337| = 336\), or array \([336, 336]\). In both cases, the total sum increases, so this array \(a\) is a counterexample. | Input: 325123 | Output: YES 1 337 NO YES 31 4 159 | Beginner | 2 | 846 | 251 | 217 | 16 |
1,051 | A | 1051A | A. Vasya And Password | 1,200 | greedy; implementation; strings | Vasya came up with a password to register for EatForces β a string \(s\). The password in EatForces should be a string, consisting of lowercase and uppercase Latin letters and digits.But since EatForces takes care of the security of its users, user passwords must contain at least one digit, at least one uppercase Latin... | The first line contains a single integer \(T\) (\(1 \le T \le 100\)) β the number of testcases.Each of the next \(T\) lines contains the initial password \(s~(3 \le |s| \le 100)\), consisting of lowercase and uppercase Latin letters and digits.Only \(T = 1\) is allowed for hacks. | For each testcase print a renewed password, which corresponds to given conditions. The length of the replaced substring is calculated as following: write down all the changed positions. If there are none, then the length is \(0\). Otherwise the length is the difference between the first and the last changed position pl... | In the first example Vasya's password lacks a digit, he replaces substring ""C"" with ""4"" and gets password ""abcD4E"". That means, he changed the substring of length 1.In the second example Vasya's password is ok from the beginning, and nothing has to be changed. That is the same as replacing the empty substring wit... | Input: 2abcDCEhtQw27 | Output: abcD4EhtQw27 | Easy | 3 | 1,299 | 280 | 636 | 10 |
1,692 | E | 1692E | E. Binary Deque | 1,200 | binary search; implementation; two pointers | Slavic has an array of length \(n\) consisting only of zeroes and ones. In one operation, he removes either the first or the last element of the array. What is the minimum number of operations Slavic has to perform such that the total sum of the array is equal to \(s\) after performing all the operations? In case the s... | The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(s\) (\(1 \leq n, s \leq 2 \cdot 10^5\)) β the length of the array and the needed sum of elements.The second line of each test case contains \(n\) integers... | For each test case, output a single integer β the minimum amount of operations required to have the total sum of the array equal to \(s\), or -1 if obtaining an array with sum \(s\) isn't possible. | In the first test case, the sum of the whole array is \(1\) from the beginning, so we don't have to make any operations.In the second test case, the sum of the array is \(2\) and we want it to be equal to \(1\), so we should remove the first element. The array turns into \([1, 0]\), which has a sum equal to \(1\).In th... | Input: 73 11 0 03 11 1 09 30 1 0 1 1 1 0 0 16 41 1 1 1 1 15 10 0 1 1 016 21 1 0 0 1 0 0 1 1 0 0 0 0 0 1 16 31 0 1 0 0 0 | Output: 0 1 3 2 2 7 -1 | Easy | 3 | 400 | 472 | 197 | 16 |
1,910 | H | 1910H | H. Sum of Digits of Sums | 2,400 | *special; binary search; data structures | You are given an array \([a_1, a_2, \dots, a_n]\), consisting of positive integers.For every \(i\) from \(1\) to \(n\), calculate \(\sum \limits_{j=1}^{n} F(a_i + a_j)\), where \(F(x)\) is the sum of digits of \(x\). | The first line contains one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i < 10^9\)). | Print \(n\) integers. The \(i\)-th of them should be equal to \(\sum \limits_{j=1}^{n} F(a_i + a_j)\). | Input: 4 1 3 3 7 | Output: 18 17 17 15 | Expert | 3 | 216 | 161 | 102 | 19 | |
611 | B | 611B | B. New Year and Old Property | 1,300 | bitmasks; brute force; implementation | The year 2015 is almost over.Limak is a little polar bear. He has recently learnt about the binary system. He noticed that the passing year has exactly one zero in its representation in the binary system β 201510 = 111110111112. Note that he doesn't care about the number of zeros in the decimal representation.Limak cho... | The only line of the input contains two integers a and b (1 β€ a β€ b β€ 1018) β the first year and the last year in Limak's interval respectively. | Print one integer β the number of years Limak will count in his chosen interval. | In the first sample Limak's interval contains numbers 510 = 1012, 610 = 1102, 710 = 1112, 810 = 10002, 910 = 10012 and 1010 = 10102. Two of them (1012 and 1102) have the described property. | Input: 5 10 | Output: 2 | Easy | 3 | 550 | 144 | 80 | 6 |
1,955 | H | 1955H | H. The Most Reckless Defense | 2,300 | bitmasks; brute force; constructive algorithms; dp; flows; graph matchings; shortest paths | You are playing a very popular Tower Defense game called ""Runnerfield 2"". In this game, the player sets up defensive towers that attack enemies moving from a certain starting point to the player's base.You are given a grid of size \(n \times m\), on which \(k\) towers are already placed and a path is laid out through... | The first line contains an integer \(t\) (\(1 \le t \le 100\)) β the number of test cases.The first line of each test case contains three integers \(n\), \(m\), and \(k\) (\(2 \le n, m \le 50, 1 \le k < n \cdot m\)) β the dimensions of the field and the number of towers on it.The next \(n\) lines each contain \(m\) cha... | For each test case, output the maximum amount of base health \(h\) on a separate line, for which it is possible to set the ranges so that the player does not lose when an enemy with health \(h\) passes through (without considering the additions for tower ranges).If it is impossible to choose ranges even for an enemy wi... | In the first example, there is no point in increasing the tower range, as it will not be able to deal enough damage to the monster even with \(1\) unit of health.In the second example, the tower has a range of \(1\), and it deals damage to the monster in cells \((1, 1)\) and \((2, 2)\).In the third example, the tower h... | Input: 62 2 1#.##1 2 12 2 1#.##1 2 22 2 1#.##1 2 5003 3 2#..##..##1 2 43 1 33 5 2#.####.#.####.#2 2 22 4 25 5 4#....#....#....#....#####3 2 1424 5 92 5 791 3 50 | Output: 0 1 1491 11 8 1797 | Expert | 7 | 1,917 | 954 | 363 | 19 |
671 | D | 671D | D. Roads in Yusland | 2,900 | data structures; dp; greedy | Mayor of Yusland just won the lottery and decided to spent money on something good for town. For example, repair all the roads in the town.Yusland consists of n intersections connected by n - 1 bidirectional roads. One can travel from any intersection to any other intersection using only these roads.There is only one r... | The first line of the input contains two integers n and m (1 β€ n, m β€ 300 000) β the number of cities in Yusland and the number of workers respectively.Then follow nβ1 line, each of them contains two integers xi and yi (1 β€ xi, yi β€ n) β indices of intersections connected by the i-th road.Last m lines provide the descr... | If it's impossible to repair all roads then print - 1. Otherwise print a single integer β minimum cost required to repair all roads using ""RC company"" workers. | In the first sample, we should choose workers with indices 1, 3, 4 and 5, some roads will be repaired more than once but it is OK. The cost will be equal to 2 + 3 + 1 + 2 = 8 coins. | Input: 6 51 21 33 44 54 62 1 23 1 44 1 35 3 16 3 2 | Output: 8 | Master | 3 | 935 | 602 | 161 | 6 |
812 | D | 812D | D. Sagheer and Kindergarten | 2,700 | dfs and similar; graphs; implementation; trees | Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing if and only if ... | The first line contains four integers n, m, k, q (1 β€ n, m, k, q β€ 105) β the number of children, toys, scenario requests and queries.Each of the next k lines contains two integers a, b (1 β€ a β€ n and 1 β€ b β€ m) β a scenario request meaning child a requests toy b. The requests are given in the order they are made by ch... | For each query, print on a single line the number of children who will start crying when child x makes his last request for toy y. Please answer all queries independent of each other. | In the first example, child 1 is waiting for toy 2, which child 2 has, while child 2 is waiting for top 3, which child 3 has. When child 3 makes his last request, the toy he requests is held by child 1. Each of the three children is waiting for a toy held by another child and no one is playing, so all the three will st... | Input: 3 3 5 11 12 23 31 22 33 1 | Output: 3 | Master | 4 | 2,253 | 715 | 183 | 8 |
1,571 | B | 1571B | B. Epic Novel | 1,300 | *special; math | Alex has bought a new novel that was published in \(n\) volumes. He has read these volumes one by one, and each volume has taken him several (maybe one) full days to read. So, on the first day, he was reading the first volume, and on each of the following days, he was reading either the same volume he had been reading ... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 100\)) β the number of volumes the novel consists of.The second line of each test ca... | For each test case, print the possible index of volume Alex could read at day \(b\). If there are multiple answers, print any. | In the first test case, since Alex was reading volume \(1\) both at day \(1\) and at day \(100\) then he was reading volume \(1\) at any day between them.In the second test case, Alex could read any volume from \(1\) to \(4\) at day \(16\). For example, he could read volume \(1\) from day \(1\) to day \(15\), volume \(... | Input: 4 1 1 1 100 1 99 4 10 1 20 4 16 100 1 1 100 100 42 100 1 1 100 2 99 | Output: 1 2 42 1 | Easy | 2 | 1,616 | 899 | 126 | 15 |
1,608 | E | 1608E | E. The Cells on the Paper | 2,800 | binary search; implementation; sortings | On an endless checkered sheet of paper, \(n\) cells are chosen and colored in three colors, where \(n\) is divisible by \(3\). It turns out that there are exactly \(\frac{n}{3}\) marked cells of each of three colors! Find the largest such \(k\) that it's possible to choose \(\frac{k}{3}\) cells of each color, remove al... | The first line of the input contains a single integer \(n\) β the number of the marked cells (\(3 \leq n \le 10^5\), \(n\) is divisible by 3).The \(i\)-th of the following \(n\) lines contains three integers \(x_i\), \(y_i\), \(c_i\) (\(|x_i|,|y_i| \leq 10^9\); \(1 \leq c_i \leq 3\)), where \((x_i, y_i)\) are the coord... | Output a single integer \(k\) β the largest number of cells you can leave. | In the first sample, it's possible to leave \(6\) cells with indexes \(1, 5, 6, 7, 8, 9\).In the second sample, it's possible to leave \(3\) cells with indexes \(1, 2, 3\). | Input: 9 2 3 1 4 1 2 2 1 3 3 4 1 5 3 2 4 4 3 2 4 1 5 2 2 3 5 3 | Output: 6 | Master | 3 | 748 | 516 | 74 | 16 |
730 | D | 730D | D. Running Over The Bridges | 2,200 | greedy; implementation; math | Polycarp is playing a game called ""Running Over The Bridges"". In this game he has to run over n bridges from the left to the right. Bridges are arranged one after the other, so the i-th bridge begins where the (i - 1)-th bridge ends.You have the following data about bridges: li and ti β the length of the i-th bridge ... | The first line contains two integers n and r (1 β€ n β€ 2Β·105, 1 β€ r β€ 1012) β the number of bridges and the duration of the effect of a magical drink.The second line contains a sequence of integers l1, l2, ..., ln (1 β€ li β€ 5Β·106), where li is equal to the length of the i-th bridge.The third line contains a sequence of ... | The first line of the output should contain k β the minimal number of drinks which Polycarp has to use, or -1 if there is no solution.If the solution exists and the value of k is not greater than 105 then output k integers on the next line β moments of time from beginning of the game when Polycarp has to use drinks. Pr... | In the first case, there is only one bridge and it is clear that Polycarp cannot run over it without magical drinks. So, if he will use one magical drink on start (moment of time 0), and the second one β three seconds later (moment of time 3), he will be able to reach the end of the bridge in time. Please note, in this... | Input: 1 3710 | Output: 20 3 | Hard | 3 | 1,333 | 461 | 427 | 7 |
182 | B | 182B | B. Vasya's Calendar | 1,000 | implementation | Vasya lives in a strange world. The year has n months and the i-th month has ai days. Vasya got a New Year present β the clock that shows not only the time, but also the date.The clock's face can display any number from 1 to d. It is guaranteed that ai β€ d for all i from 1 to n. The clock does not keep information abou... | The first line contains the single number d β the maximum number of the day that Vasya's clock can show (1 β€ d β€ 106).The second line contains a single integer n β the number of months in the year (1 β€ n β€ 2000).The third line contains n space-separated integers: ai (1 β€ ai β€ d) β the number of days in each month in th... | Print a single number β the number of times Vasya manually increased the day number by one throughout the last year. | In the first sample the situation is like this: Day 1. Month 1. The clock shows 1. Vasya changes nothing. Day 2. Month 1. The clock shows 2. Vasya changes nothing. Day 1. Month 2. The clock shows 3. Vasya manually increases the day number by 1. After that the clock shows 4. Vasya increases the day number by 1 manually.... | Input: 422 2 | Output: 2 | Beginner | 1 | 1,271 | 378 | 116 | 1 |
229 | C | 229C | C. Triangles | 1,900 | combinatorics; graphs; math | Alice and Bob don't play games anymore. Now they study properties of all sorts of graphs together. Alice invented the following task: she takes a complete undirected graph with n vertices, chooses some m edges and keeps them. Bob gets the remaining edges.Alice and Bob are fond of ""triangles"" in graphs, that is, cycle... | The first line contains two space-separated integers n and m (1 β€ n β€ 106, 0 β€ m β€ 106) β the number of vertices in the initial complete graph and the number of edges in Alice's graph, correspondingly. Then m lines follow: the i-th line contains two space-separated integers ai, bi (1 β€ ai, bi β€ n, ai β bi), β the numbe... | Print a single number β the total number of cycles of length 3 in Alice and Bob's graphs together.Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is advised to use the cin, cout streams or the %I64d specifier. | In the first sample Alice has 2 triangles: (1, 2, 3) and (2, 3, 4). Bob's graph has only 1 triangle : (1, 4, 5). That's why the two graphs in total contain 3 triangles.In the second sample Alice's graph has only one triangle: (1, 2, 3). Bob's graph has three triangles: (1, 4, 5), (2, 4, 5) and (3, 4, 5). In this case t... | Input: 5 51 21 32 32 43 4 | Output: 3 | Hard | 3 | 466 | 629 | 243 | 2 |
1,263 | D | 1263D | D. Secret Passwords | 1,500 | dfs and similar; dsu; graphs | One unknown hacker wants to get the admin's password of AtForces testing system, to get problems from the next contest. To achieve that, he sneaked into the administrator's office and stole a piece of paper with a list of \(n\) passwords β strings, consists of small Latin letters.Hacker went home and started preparing ... | The first line contain integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β number of passwords in the list. Next \(n\) lines contains passwords from the list β non-empty strings \(s_i\), with length at most \(50\) letters. Some of the passwords may be equal.It is guaranteed that the total length of all passwords does not ex... | In a single line print the minimal number of passwords, the use of which will allow guaranteed to access the system. | In the second example hacker need to use any of the passwords to access the system. | Input: 4 a b ab d | Output: 2 | Medium | 3 | 1,554 | 395 | 116 | 12 |
2,002 | E | 2002E | E. Cosmic Rays | 2,300 | brute force; data structures; dp | Given an array of integers \(s_1, s_2, \ldots, s_l\), every second, cosmic rays will cause all \(s_i\) such that \(i=1\) or \(s_i\neq s_{i-1}\) to be deleted simultaneously, and the remaining parts will be concatenated together in order to form the new array \(s_1, s_2, \ldots, s_{l'}\).Define the strength of an array ... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1\le t\le10^4\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(1\le n\le3\cdot10^5\)) β the length of sequence \(a\).The next \(n\) lines contain two intege... | For each test case, print one line containing \(n\) integers β the answer for each prefix of pairs. | In the first test case, for the prefix of length \(4\), the changes will be \([0,0,1,0,0,0,1,1,1,1,1]\rightarrow[0,0,0,1,1,1,1]\rightarrow[0,0,1,1,1]\rightarrow[0,1,1]\rightarrow[1]\rightarrow[]\), so the array becomes empty after \(5\) seconds.In the second test case, for the prefix of length \(4\), the changes will b... | Input: 442 01 13 05 164 61 34 64 07 66 379 07 15 07 19 01 12 01010 74 92 27 92 88 511 715 512 74 0 | Output: 2 2 4 5 4 4 7 7 10 10 9 9 9 9 9 9 10 10 10 10 10 10 10 12 15 15 15 | Expert | 3 | 710 | 566 | 99 | 20 |
999 | C | 999C | C. Alphabetic Removals | 1,200 | implementation | You are given a string \(s\) consisting of \(n\) lowercase Latin letters. Polycarp wants to remove exactly \(k\) characters (\(k \le n\)) from the string \(s\). Polycarp uses the following algorithm \(k\) times: if there is at least one letter 'a', remove the leftmost occurrence and stop the algorithm, otherwise go to ... | The first line of input contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 4 \cdot 10^5\)) β the length of the string and the number of letters Polycarp will remove.The second line contains the string \(s\) consisting of \(n\) lowercase Latin letters. | Print the string that will be obtained from \(s\) after Polycarp removes exactly \(k\) letters using the above algorithm \(k\) times.If the resulting string is empty, print nothing. It is allowed to print nothing or an empty line (line break). | Input: 15 3cccaabababaccbc | Output: cccbbabaccbc | Easy | 1 | 716 | 259 | 243 | 9 | |
2,030 | A | 2030A | A. A Gift From Orangutan | 800 | constructive algorithms; greedy; math; sortings | While exploring the jungle, you have bumped into a rare orangutan with a bow tie! You shake hands with the orangutan and offer him some food and water. In return...The orangutan has gifted you an array \(a\) of length \(n\). Using \(a\), you will construct two arrays \(b\) and \(c\), both containing \(n\) elements, in ... | The first line contains \(t\) (\(1 \leq t \leq 100\)) β the number of test cases.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 1000\)) β the number of elements in \(a\).The following line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 1000\)) β the elements of the arr... | For each test case, output the maximum score that you can get. | In the first test case, there is no other way to rearrange \(a\). So, \(b = [69]\) and \(c = [69]\). The only possible score is \(69 - 69 = 0\).In the second test case, you can rearrange \(a\) as \([7, 5, 6]\). Here, \(b = [7, 5, 5]\) and \(c = [7, 7, 7]\). The score in this case is \((7 - 7) + (7 - 5) + (7 - 5) = 4\).... | Input: 316937 6 551 1 1 2 2 | Output: 0 4 4 | Beginner | 4 | 770 | 413 | 62 | 20 |
1,511 | B | 1511B | B. GCD Length | 1,100 | constructive algorithms; math; number theory | You are given three integers \(a\), \(b\) and \(c\).Find two positive integers \(x\) and \(y\) (\(x > 0\), \(y > 0\)) such that: the decimal representation of \(x\) without leading zeroes consists of \(a\) digits; the decimal representation of \(y\) without leading zeroes consists of \(b\) digits; the decimal represent... | The first line contains a single integer \(t\) (\(1 \le t \le 285\)) β the number of testcases.Each of the next \(t\) lines contains three integers \(a\), \(b\) and \(c\) (\(1 \le a, b \le 9\), \(1 \le c \le min(a, b)\)) β the required lengths of the numbers.It can be shown that the answer exists for all testcases unde... | For each testcase print two positive integers β \(x\) and \(y\) (\(x > 0\), \(y > 0\)) such that the decimal representation of \(x\) without leading zeroes consists of \(a\) digits; the decimal representation of \(y\) without leading zeroes consists of \(b\) digits; the decimal representation of \(gcd(x, y)\) without l... | In the example: \(gcd(11, 492) = 1\) \(gcd(13, 26) = 13\) \(gcd(140133, 160776) = 21\) \(gcd(1, 1) = 1\) | Input: 4 2 3 1 2 2 2 6 6 2 1 1 1 | Output: 11 492 13 26 140133 160776 1 1 | Easy | 3 | 550 | 408 | 359 | 15 |
1,614 | C | 1614C | C. Divan and bitwise operations | 1,500 | bitmasks; combinatorics; constructive algorithms; dp; math | Once Divan analyzed a sequence \(a_1, a_2, \ldots, a_n\) consisting of \(n\) non-negative integers as follows. He considered each non-empty subsequence of the sequence \(a\), computed the bitwise XOR of its elements and added up all the XORs, obtaining the coziness of the sequence \(a\).A sequence \(c\) is a subsequenc... | The first line contains one integer number \(t\) (\(1 \le t \le 10^3\)) β the number of test cases.The first line of each test case contains two integer numbers \(n\) and \(m\) (\(1 \le n, m \le 2 \cdot 10^5\)) β the length of the sequence and the number of contiguous segments whose bitwise OR values Divan remembers, r... | For each test case print the coziness any suitable sequence \(a\) modulo \(10^9 + 7\). | In first example, one of the sequences that fits the constraints is \([0, 2]\). Consider all its non-empty subsequences: \([0]\): the bitwise XOR of this subsequence is \(0\); \([2]\): the bitwise XOR of this subsequence is \(2\); \([0, 2]\): the bitwise XOR of this subsequence is \(2\). The sum of all results is \(4\)... | Input: 3 2 1 1 2 2 3 2 1 3 5 2 3 5 5 4 1 2 7 3 3 7 4 4 0 4 5 2 | Output: 4 20 112 | Medium | 5 | 1,122 | 916 | 86 | 16 |
1,773 | H | 1773H | H. Hot and Cold | 2,600 | binary search; interactive | This is an interactive problem.After emigrating to another country, little Hanna came across the fact that playing ""Hot and cold"" became more difficult: she does not understand the hint phrases! You are to write a helping program for her.The playground where the game happens is a rectangle, and the treasure is hidden... | Input: Tabilmadi Daha yakin Daha yakin Sama distanco Dalej Znaydeno! | Output: 500 200 560 230 566 240 566 238 30 239 566 239 | Expert | 2 | 1,165 | 0 | 0 | 17 | |||
72 | C | 72C | C. Extraordinarily Nice Numbers | 1,200 | *special; math | The positive integer a is a divisor of the positive integer b if and only if there exists a positive integer c such that a Γ c = b. King Astyages thinks a positive integer x is extraordinarily nice if the number of its even divisors is equal to the number of its odd divisors.For example 3 has two positive divisors 3 an... | The input contains only a single integer x (1 β€ x β€ 103). | Write a single yes or no. Write yes if the number is extraordinarily nice and no otherwise.You don't need to care about capital or small letters. The output is case-insensitive. | Input: 2 | Output: yes | Easy | 2 | 579 | 57 | 177 | 0 | |
2,038 | G | 2038G | G. Guess One Character | 1,900 | constructive algorithms; implementation; interactive | This is an interactive problem. You have to use flush operation right after printing each line. For example, in C++ you should use the function fflush(stdout) or cout.flush(), in Java or Kotlin β System.out.flush(), and in Python β sys.stdout.flush().The jury has a string \(s\) consisting of characters 0 and/or 1. The ... | In the example, there are \(3\) test cases: 101, 11 and 10. Note that everything after the // sign is a comment that explains which line means what in the interaction. The jury program won't print these comments in the actual problem, and you shouldn't print them. The empty lines are also added for your convenience, th... | Input: 3 // 3 test cases 3 // the length of the string is 3 1 // 101 occurs once 1 // guessed correctly 2 // the length of the string is 2 0 // 00 occurs zero times 0 // 0 occurs zero times 1 // guessed correctly 2 // the length of the string is 2 1 // 1 occurs once 0 // 01 occurs zero times 1 // guessed correctly | Ou... | Hard | 3 | 932 | 0 | 0 | 20 | ||
2,096 | A | 2096A | A. Wonderful Sticks | 800 | constructive algorithms; greedy | You are the proud owner of \(n\) sticks. Each stick has an integer length from \(1\) to \(n\). The lengths of the sticks are distinct.You want to arrange the sticks in a row. There is a string \(s\) of length \(n - 1\) that describes the requirements of the arrangement.Specifically, for each \(i\) from \(1\) to \(n - 1... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 500\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(2 \le n \le 100\)) β the number of sticks.The second line of each test case contains a sing... | For each test case, output \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq n\), the \(a_i\) are distinct) β the lengths of the sticks in order. If there are multiple solutions, print any of them. | For the first test case, the requirements of the arrangement are as follows: \(s_1 = \texttt{<}\), which means \(a_2\) is smaller than \(a_1\).Thus, one possible arrangement is \([2, 1]\).For the second test case, the requirements of the arrangement are as follows: \(s_1 = \texttt{<}\), which means \(a_2\) is smaller t... | Input: 52<5<<><2>3<>7><>>>< | Output: 2 1 4 3 2 5 1 1 2 2 1 3 3 4 2 5 6 7 1 | Beginner | 2 | 639 | 464 | 207 | 20 |
1,848 | C | 1848C | C. Vika and Price Tags | 1,800 | math; number theory | Vika came to her favorite cosmetics store ""Golden Pear"". She noticed that the prices of \(n\) items have changed since her last visit.She decided to analyze how much the prices have changed and calculated the difference between the old and new prices for each of the \(n\) items.Vika enjoyed calculating the price diff... | 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\) (\(1 \le n \le 10^5\)) β the number of items whose prices have changed.T... | For each test case, output ""YES"" if the pair of price arrays is dull, and ""NO"" otherwise.You can output each letter in any case (lowercase or uppercase). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be accepted as a positive answer. | In the first test case, the array \(a\) is initially zero.In the second test case, after the first operation \(a = [1, 2, 3], b = [0, 0, 0]\). After the second operation \(a = [0, 0, 0], b = [1, 2, 3]\).In the third test case, it can be shown that the array \(a\) will never become zero. | Input: 940 0 0 01 2 3 431 2 31 2 321 22 16100 23 53 11 56 321245 31 12 6 6 671 2 3 4 5 6 77 6 5 4 3 2 134 0 24 0 232 5 21 3 426 14 220 00 3 | Output: YES YES NO NO YES YES NO YES YES | Medium | 2 | 1,204 | 644 | 260 | 18 |
1,311 | E | 1311E | E. Construct the Binary Tree | 2,200 | brute force; constructive algorithms; trees | You are given two integers \(n\) and \(d\). You need to construct a rooted binary tree consisting of \(n\) vertices with a root at the vertex \(1\) and the sum of depths of all vertices equals to \(d\).A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a vertex \... | The first line of the input contains one integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.The only line of each test case contains two integers \(n\) and \(d\) (\(2 \le n, d \le 5000\)) β the number of vertices in the tree and the required sum of depths of all vertices.It is guaranteed that the sum of \(... | For each test case, print the answer.If it is impossible to construct such a tree, print ""NO"" (without quotes) in the first line. Otherwise, print ""{YES}"" in the first line. Then print \(n-1\) integers \(p_2, p_3, \dots, p_n\) in the second line, where \(p_i\) is the parent of the vertex \(i\). Note that the sequen... | Pictures corresponding to the first and the second test cases of the example: | Input: 3 5 7 10 19 10 18 | Output: YES 1 2 1 3 YES 1 2 3 3 9 9 2 1 6 NO | Hard | 3 | 699 | 414 | 377 | 13 |
1,077 | B | 1077B | B. Disturbed People | 1,000 | greedy | There is a house with \(n\) flats situated on the main street of Berlatov. Vova is watching this house every night. The house can be represented as an array of \(n\) integer numbers \(a_1, a_2, \dots, a_n\), where \(a_i = 1\) if in the \(i\)-th flat the light is on and \(a_i = 0\) otherwise.Vova thinks that people in t... | The first line of the input contains one integer \(n\) (\(3 \le n \le 100\)) β the number of flats in the house.The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(a_i \in \{0, 1\}\)), where \(a_i\) is the state of light in the \(i\)-th flat. | Print only one integer β the minimum number \(k\) such that if people from exactly \(k\) pairwise distinct flats will turn off the light then nobody will be disturbed. | In the first example people from flats \(2\) and \(7\) or \(4\) and \(7\) can turn off the light and nobody will be disturbed. It can be shown that there is no better answer in this example.There are no disturbed people in second and third examples. | Input: 10 1 1 0 1 1 0 1 0 1 0 | Output: 2 | Beginner | 1 | 684 | 271 | 167 | 10 |
1,114 | E | 1114E | E. Arithmetic Progression | 2,200 | binary search; interactive; number theory; probabilities | This is an interactive problem!An arithmetic progression or arithmetic sequence is a sequence of integers such that the subtraction of element with its previous element (\(x_i - x_{i - 1}\), where \(i \ge 2\)) is constant β such difference is called a common difference of the sequence.That is, an arithmetic progression... | Note that the example interaction contains extra empty lines so that it's easier to read. The real interaction doesn't contain any empty lines and you shouldn't print any extra empty lines as well.The list in the example test is \([14, 24, 9, 19]\). | Input: 4 0 1 14 24 9 19 | Output: > 25 > 15 ? 1 ? 2 ? 3 ? 4 ! 9 5 | Hard | 4 | 1,592 | 0 | 0 | 11 | ||
630 | Q | 630Q | Q. Pyramids | 1,700 | geometry; math | IT City administration has no rest because of the fame of the Pyramids in Egypt. There is a project of construction of pyramid complex near the city in the place called Emerald Walley. The distinction of the complex is that its pyramids will be not only quadrangular as in Egypt but also triangular and pentagonal. Of co... | The only line of the input contains three integers l3, l4, l5 (1 β€ l3, l4, l5 β€ 1000) β the edge lengths of triangular, quadrangular and pentagonal pyramids correspondingly. | Output one number β the total volume of the pyramids. Absolute or relative error should not be greater than 10 - 9. | Input: 2 5 3 | Output: 38.546168065709 | Medium | 2 | 859 | 173 | 115 | 6 | |
908 | C | 908C | C. New Year and Curling | 1,500 | brute force; geometry; implementation; math | Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then... | The first line will contain two integers n and r (1 β€ n, r β€ 1 000), the number of disks, and the radius of the disks, respectively.The next line will contain n integers x1, x2, ..., xn (1 β€ xi β€ 1 000) β the x-coordinates of the disks. | Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6.Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker prog... | The final positions of the disks will look as follows: In particular, note the position of the last disk. | Input: 6 25 5 6 8 3 12 | Output: 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 | Medium | 4 | 668 | 236 | 381 | 9 |
173 | D | 173D | D. Deputies | 2,500 | constructive algorithms; graphs; greedy; implementation | The Trinitarian kingdom has exactly n = 3k cities. All of them are located on the shores of river Trissisipi, which flows through the whole kingdom. Some of the cities are located on one side of the river, and all the rest are on the other side.Some cities are connected by bridges built between them. Each bridge connec... | The first line contains two integers n and m β the number of cities and bridges (3 β€ n < 105, n = 3k, 0 β€ m β€ 105). Next m lines describe the bridges. The i-th line contains two integers ai and bi β the numbers of cities that are connected by the i-th bridge (1 β€ ai, bi β€ n, ai β bi, 1 β€ i β€ m).It is guaranteed that no... | If distributing the deputies in the required manner is impossible, print in a single line ""NO"" (without the quotes).Otherwise, in the first line print ""YES"" (without the quotes), and in the second line print which deputy should be put in charge of each city. The i-th number should represent the number of the deputy... | Input: 6 61 24 13 56 52 64 6 | Output: YES1 2 1 2 2 1 | Expert | 4 | 960 | 423 | 482 | 1 | |
28 | C | 28C | C. Bath Queue | 2,200 | combinatorics; dp; probabilities | There are n students living in the campus. Every morning all students wake up at the same time and go to wash. There are m rooms with wash basins. The i-th of these rooms contains ai wash basins. Every student independently select one the rooms with equal probability and goes to it. After all students selected their ro... | The first line contains two positive integers n and m (1 β€ n, m β€ 50) β the amount of students and the amount of rooms. The second line contains m integers a1, a2, ... , am (1 β€ ai β€ 50). ai means the amount of wash basins in the i-th room. | Output single number: the expected value of the size of the largest queue. Your answer must have an absolute or relative error less than 10 - 9. | Input: 1 12 | Output: 1.00000000000000000000 | Hard | 3 | 534 | 240 | 144 | 0 | |
1,530 | E | 1530E | E. Minimax | 2,100 | constructive algorithms; greedy; strings | Prefix function of string \(t = t_1 t_2 \ldots t_n\) and position \(i\) in it is defined as the length \(k\) of the longest proper (not equal to the whole substring) prefix of substring \(t_1 t_2 \ldots t_i\) which is also a suffix of the same substring.For example, for string \(t = \) abacaba the values of the prefix ... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^5\)). Description of the test cases follows.The only line of each test case contains string \(s\) (\(1 \le |s| \le 10^5\)) consisting of lowercase English letters.It is guaranteed that the sum of lengths of ... | For each test case print a single string \(t\).The multisets of letters in strings \(s\) and \(t\) must be equal. The value of \(f(t)\), the maximum of prefix functions in string \(t\), must be as small as possible. String \(t\) must be the lexicographically smallest string out of all strings satisfying the previous co... | A string \(a\) is lexicographically smaller than a string \(b\) if and only if one of the following holds: \(a\) is a prefix of \(b\), but \(a \ne b\); in the first position where \(a\) and \(b\) differ, the string \(a\) has a letter that appears earlier in the alphabet than the corresponding letter in \(b\).In the fir... | Input: 3 vkcup abababa zzzzzz | Output: ckpuv aababab zzzzzz | Hard | 3 | 867 | 371 | 329 | 15 |
1,032 | C | 1032C | C. Playing Piano | 1,700 | constructive algorithms; dp | Little Paul wants to learn how to play piano. He already has a melody he wants to start with. For simplicity he represented this melody as a sequence \(a_1, a_2, \ldots, a_n\) of key numbers: the more a number is, the closer it is to the right end of the piano keyboard.Paul is very clever and knows that the essential t... | The first line contains a single integer \(n\) (\(1 \le n \le 10^5\)) denoting the number of notes.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 2\cdot10^5\)) denoting the positions of notes on the keyboard. | If there is no convenient fingering, print \(-1\). Otherwise, print \(n\) numbers \(b_1, b_2, \ldots, b_n\), each from \(1\) to \(5\), denoting a convenient fingering, separated by spaces. | The third sample test is kinda ""Non stop"" song by Reflex. | Input: 5 1 1 4 2 2 | Output: 1 4 5 4 5 | Medium | 2 | 1,223 | 244 | 188 | 10 |
1,093 | D | 1093D | D. Beautiful Graph | 1,700 | dfs and similar; graphs | You are given an undirected unweighted graph consisting of \(n\) vertices and \(m\) edges.You have to write a number on each vertex of the graph. Each number should be \(1\), \(2\) or \(3\). The graph becomes beautiful if for each edge the sum of numbers on vertices connected by this edge is odd.Calculate the number of... | The first line contains one integer \(t\) (\(1 \le t \le 3 \cdot 10^5\)) β the number of tests in the input.The first line of each test contains two integers \(n\) and \(m\) (\(1 \le n \le 3 \cdot 10^5, 0 \le m \le 3 \cdot 10^5\)) β the number of vertices and the number of edges, respectively. Next \(m\) lines describe... | For each test print one line, containing one integer β the number of possible ways to write numbers \(1\), \(2\), \(3\) on the vertices of given graph so it becomes beautiful. Since answers may be large, print them modulo \(998244353\). | Possible ways to distribute numbers in the first test: the vertex \(1\) should contain \(1\), and \(2\) should contain \(2\); the vertex \(1\) should contain \(3\), and \(2\) should contain \(2\); the vertex \(1\) should contain \(2\), and \(2\) should contain \(1\); the vertex \(1\) should contain \(2\), and \(2\) sho... | Input: 2 2 1 1 2 4 6 1 2 1 3 1 4 2 3 2 4 3 4 | Output: 4 0 | Medium | 2 | 600 | 588 | 236 | 10 |
2,009 | C | 2009C | C. The Legend of Freya the Frog | 1,100 | implementation; math | Freya the Frog is traveling on the 2D coordinate plane. She is currently at point \((0,0)\) and wants to go to point \((x,y)\). In one move, she chooses an integer \(d\) such that \(0 \leq d \leq k\) and jumps \(d\) spots forward in the direction she is facing. Initially, she is facing the positive \(x\) direction. Aft... | The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.Each test case contains three integers \(x\), \(y\), and \(k\) (\(0 \leq x, y \leq 10^9, 1 \leq k \leq 10^9\)). | For each test case, output the number of jumps Freya needs to make on a new line. | In the first sample, one optimal set of moves is if Freya jumps in the following way: (\(0,0\)) \(\rightarrow\) (\(2,0\)) \(\rightarrow\) (\(2,2\)) \(\rightarrow\) (\(3,2\)) \(\rightarrow\) (\(3,5\)) \(\rightarrow\) (\(6,5\)) \(\rightarrow\) (\(6,8\)) \(\rightarrow\) (\(9,8\)) \(\rightarrow\) (\(9,11\)). This takes 8 j... | Input: 39 11 30 10 81000000 100000 10 | Output: 8 4 199999 | Easy | 2 | 641 | 204 | 81 | 20 |
97 | D | 97D | D. Robot in Basement | 2,700 | bitmasks; brute force; implementation | The Professor has lost his home robot yet again. After some thinking Professor understood that he had left the robot in the basement.The basement in Professor's house is represented by a rectangle n Γ m, split into 1 Γ 1 squares. Some squares are walls which are impassable; other squares are passable. You can get from ... | The first line contains three integers n, m and k (3 β€ n, m β€ 150, 1 β€ k β€ 105). Next n lines contain m characters each β that is the Professor's basement's description: ""#"" stands for a wall, ""."" stands for a passable square and ""E"" stands for the exit from the basement (this square also is passable). It is poss... | Print in the output file the length of the smallest possible prefix that will lead the robot to the exit square. In other words, wherever the robot had been positioned initially, it should be positioned in the exit square after all the commands from the prefix are fulfilled (during doing commands the robot can come and... | Input: 5 5 7######...##...##E..######UULLDDR | Output: 6 | Master | 3 | 1,736 | 711 | 636 | 0 | |
1,943 | F | 1943F | F. Minimum Hamming Distance | 3,500 | dp | You are given a binary string\(^\dagger\) \(s\) of length \(n\).A binary string \(p\) of the same length \(n\) is called good if for every \(i\) (\(1 \leq i \leq n\)), there exist indices \(l\) and \(r\) such that: \(1 \leq l \leq i \leq r \leq n\) \(s_i\) is a mode\(^\ddagger\) of the string \(p_lp_{l+1}\ldots p_r\) Y... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^5\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^4\)) β the length of the binary string \(s\).The second line of each test ca... | For each test case, print the minimum Hamming distance between \(t\) and any good string \(g\). | In the first test case, \(g=\mathtt{000}\) is a good string which has Hamming distance \(0\) from \(t\).In the second test case, \(g=\mathtt{0011}\) is a good string which has Hamming distance \(2\) from \(t\). It can be proven that there are no good strings with Hamming distance less than \(2\) from \(t\).In the third... | Input: 330000004000011116111111000100 | Output: 0 2 1 | Master | 1 | 1,068 | 699 | 95 | 19 |
662 | C | 662C | C. Binary Table | 2,600 | bitmasks; brute force; divide and conquer; dp; fft; math | You are given a table consisting of n rows and m columns. Each cell of the table contains either 0 or 1. In one move, you are allowed to pick any row or any column and invert all values, that is, replace 0 by 1 and vice versa.What is the minimum number of cells with value 1 you can get after applying some number of ope... | The first line of the input contains two integers n and m (1 β€ n β€ 20, 1 β€ m β€ 100 000) β the number of rows and the number of columns, respectively.Then n lines follows with the descriptions of the rows. Each line has length m and contains only digits '0' and '1'. | Output a single integer β the minimum possible number of ones you can get after applying some sequence of operations. | Input: 3 4011010100111 | Output: 2 | Expert | 6 | 328 | 265 | 117 | 6 | |
1,703 | B | 1703B | B. ICPC Balloons | 800 | data structures; implementation | In an ICPC contest, balloons are distributed as follows: Whenever a team solves a problem, that team gets a balloon. The first team to solve a problem gets an additional balloon. A contest has 26 problems, labelled \(\textsf{A}\), \(\textsf{B}\), \(\textsf{C}\), ..., \(\textsf{Z}\). You are given the order of solved pr... | The first line of the input contains an integer \(t\) (\(1 \leq t \leq 100\)) β the number of testcases.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 50\)) β the length of the string.The second line of each test case contains a string \(s\) of length \(n\) consisting of uppercase English l... | For each test case, output a single integer β the total number of balloons that the teams received. | In the first test case, \(5\) balloons are given out: Problem \(\textsf{A}\) is solved. That team receives \(2\) balloons: one because they solved the problem, an an additional one because they are the first team to solve problem \(\textsf{A}\). Problem \(\textsf{B}\) is solved. That team receives \(2\) balloons: one b... | Input: 63ABA1A3ORZ5BAAAA4BKPT10CODEFORCES | Output: 5 2 6 7 8 17 | Beginner | 2 | 629 | 366 | 99 | 17 |
1,562 | D1 | 1562D1 | D1. Two Hundred Twenty One (easy version) | 1,700 | data structures; dp; math | This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.Stitch likes experimenting with different machines with his friend Sparky. Today t... | Each test contains multiple test cases.The first line contains one positive integer \(t\) (\(1 \le t \le 10^3\)), denoting the number of test cases. Description of the test cases follows.The first line of each test case contains two positive integers \(n\) and \(q\) (\(1 \le n, q \le 3 \cdot 10^5\)) β the number of rod... | For each test case, print a single integer β the minimal number of rods that can be removed. | In the first test case for the first query you can remove the rods numbered \(5\) and \(8\), then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.In the second test case: For the first query, we can remove the rods numbered \(1\) and \(11\), then the follow... | Input: 3 14 1 +--++---++-++- 1 14 14 3 +--++---+++--- 1 14 6 12 3 10 4 10 +-+- 1 1 1 2 1 3 1 4 2 2 2 3 2 4 3 3 3 4 4 4 | Output: 2 2 1 0 1 2 1 2 1 2 1 1 2 1 | Medium | 3 | 1,820 | 884 | 92 | 15 |
2,084 | A | 2084A | A. Max and Mod | 800 | constructive algorithms; math | You are given an integer \(n\). Find any permutation \(p\) of length \(n\)\(^{\text{β}}\) such that: For all \(2 \le i \le n\), \(\max(p_{i - 1}, p_i) \bmod i\) \(^{\text{β }}\) \(= i - 1\) is satisfied. If it is impossible to find such a permutation \(p\), output \(-1\).\(^{\text{β}}\)A permutation of length \(n\) is a... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 99\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(2 \le n \le 100\)). | For each test case: If such a permutation \(p\) doesn't exist, output a single integer \(-1\). Otherwise, output \(n\) integers \(p_1, p_2, \ldots, p_n\) β the permutation \(p\) you've found. If there are multiple answers, output any of them. | In the first test case, it is impossible to find such a permutation \(p\), so you should output \(-1\).In the fourth test case, \(p = [1, 5, 2, 3, 4]\) satisfies the condition: For \(i = 2\), \(\max(p_1, p_2) = 5\) and \(5 \bmod 2 = 1\). For \(i = 3\), \(\max(p_2, p_3) = 5\) and \(5 \bmod 3 = 2\). For \(i = 4\), \(\max... | Input: 42345 | Output: -1 3 2 1 -1 1 5 2 3 4 | Beginner | 2 | 691 | 247 | 242 | 20 |
1,916 | F | 1916F | F. Group Division | 2,900 | constructive algorithms; dfs and similar; graphs; greedy | In the \(31\)st lyceum, there were two groups of olympiad participants: computer science and mathematics. The number of computer scientists was \(n_1\), and the number of mathematicians was \(n_2\). It is not known for certain who belonged to which group, but it is known that there were friendly connections between som... | Each test consists of several test cases. The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. Then follows the description of the test cases.The first line of each test case contains three integers \(n_1\), \(n_2\), and \(m\) (\(1 \le n_1, n_2 \le 2000\), \(1 \le m \le 5000... | For each test case, output two lines.In the first line, output \(n_1\) distinct numbers \(a_i\) (\(1 \le a_i \le n_1 + n_2\)) β the people belonging to the first group.In the second line, output \(n_2\) distinct numbers \(b_i\) (\(1 \le b_i \le n_1 + n_2\)) β the people belonging to the second group.All numbers must be... | Consider the third test case. The division into groups looks as follows: The students selected as computer scientists are colored in green, and those selected as mathematicians are colored in blue.Consider all pairs of computer scientists and how they are acquainted:Pairs \((4, 5), (4, 6)\) are directly acquainted.Pair... | Input: 31 2 32 31 31 21 4 72 53 42 41 23 54 51 53 3 71 21 62 32 53 44 54 6 | Output: 3 1 2 5 1 2 3 4 4 5 6 1 2 3 | Master | 4 | 1,710 | 1,058 | 383 | 19 |
1,227 | E | 1227E | E. Arson In Berland Forest | 2,200 | binary search; graphs; graphs; shortest paths | The Berland Forest can be represented as an infinite cell plane. Every cell contains a tree. That is, contained before the recent events.A destructive fire raged through the Forest, and several trees were damaged by it. Precisely speaking, you have a \(n \times m\) rectangle map which represents the damaged part of the... | The first line contains two integer \(n\) and \(m\) (\(1 \le n, m \le 10^6\), \(1 \le n \cdot m \le 10^6\)) β the sizes of the map.Next \(n\) lines contain the map. The \(i\)-th line corresponds to the \(i\)-th row of the map and contains \(m\)-character string. The \(j\)-th character of the \(i\)-th string is ""X"" if... | In the first line print the single integer \(T\) β the maximum time the Forest was on fire. In the next \(n\) lines print the certificate: the map (\(n \times m\) rectangle) where the trees that were set on fire are marked as ""X"" and all other trees are marked as ""."". | Input: 3 6 XXXXXX XXXXXX XXXXXX | Output: 1 ...... .X.XX. ...... | Hard | 4 | 1,449 | 430 | 272 | 12 | |
1,178 | F1 | 1178F1 | F1. Short Colorful Strip | 2,200 | combinatorics; dfs and similar; dp | This is the first subtask of problem F. The only differences between this and the second subtask are the constraints on the value of \(m\) and the time limit. You need to solve both subtasks in order to hack this one.There are \(n+1\) distinct colours in the universe, numbered \(0\) through \(n\). There is a strip of p... | The first line contains a two integers \(n\), \(m\) (\(1 \leq n \leq 500\), \(n = m\)) β the number of colours excluding the colour \(0\) and the length of the paper, respectively.The second line contains \(m\) space separated integers \(c_1, c_2, \ldots, c_m\) (\(1 \leq c_i \leq n\)) β the colour visible on the segmen... | Output a single integer β the number of ways Alice can perform the painting, modulo \(998244353\). | In the first example, there are \(5\) ways, all depicted in the figure below. Here, \(0\) is white, \(1\) is red, \(2\) is green and \(3\) is blue.Below is an example of a painting process that is not valid, as in the second step the segment 1 3 is not single colour, and thus may not be repainted with colour \(2\). | Input: 3 3 1 2 3 | Output: 5 | Hard | 3 | 1,107 | 579 | 98 | 11 |
1,214 | E | 1214E | E. Petya and Construction Set | 2,000 | constructive algorithms; graphs; math; sortings; trees | It's Petya's birthday party and his friends have presented him a brand new ""Electrician-\(n\)"" construction set, which they are sure he will enjoy as he always does with weird puzzles they give him.Construction set ""Electrician-\(n\)"" consists of \(2n - 1\) wires and \(2n\) light bulbs. Each bulb has its own unique... | The first line of the input contains a single integer \(n\) (\(1 \leq n \leq 100\,000\)) β the parameter of a construction set that defines the number of bulbs and the number of wires.Next line contains \(n\) integers \(d_1, d_2, \ldots, d_n\) (\(1 \leq d_i \leq n\)), where \(d_i\) stands for the number of wires the ch... | Print \(2n - 1\) lines. The \(i\)-th of them should contain two distinct integers \(a_i\) and \(b_i\) (\(1 \leq a_i, b_i \leq 2n\), \(a_i \ne b_i\)) β indices of bulbs connected by a wire.If there are several possible valid answer you can print any of them. | Answer for the first sample test. Answer for the second sample test. | Input: 3 2 2 2 | Output: 1 6 2 6 3 5 3 6 4 5 | Hard | 5 | 1,545 | 378 | 257 | 12 |
2,097 | F | 2097F | F. Lost Luggage | 3,500 | dp; flows | As is known, the airline ""Trouble"" often loses luggage, and concerned journalists decided to calculate the maximum number of luggage pieces that may not return to travelers.The airline ""Trouble"" operates flights between \(n\) airports, numbered from \(1\) to \(n\). The journalists' experiment will last for \(m\) da... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). The description of the test cases follows. The first line of each test case contains two integers \(n\) and \(m\) (\(3 \le n \le 12\), \(1 \le m \le 2000\)) β the number of airports and the number of da... | For each test case, output \(m\) integers β the maximum number of unfound pieces of luggage for each number of days from \(1\) to \(m\). | In the first test case: On the first day, all \(5\) pieces of luggage may not be found, as the lost luggage can be sent on flights from each airport. In the morning of the second day, there may be no more than \(3\) pieces of luggage in the \(2\)-nd airport, no more than \(2\) pieces in the \(5\)-th airport, and no lug... | Input: 25 31 1 1 1 10 0 1 0 00 1 0 0 11 0 0 1 00 1 0 0 09 0 9 9 90 1 0 0 00 0 0 0 09 0 9 0 00 0 0 0 03 10 100000000 50 100000000 50 100000000 50 100000000 5 | Output: 5 4 2 100000005 | Master | 2 | 1,758 | 1,406 | 136 | 20 |
1,276 | E | 1276E | E. Four Stones | 3,500 | constructive algorithms | There are four stones on an infinite line in integer coordinates \(a_1, a_2, a_3, a_4\). The goal is to have the stones in coordinates \(b_1, b_2, b_3, b_4\). The order of the stones does not matter, that is, a stone from any position \(a_i\) can end up in at any position \(b_j\), provided there is a required number of... | The first line contains four integers \(a_1, \ldots, a_4\) (\(-10^9 \leq a_i \leq 10^9\)) β initial coordinates of the stones. There may be multiple stones sharing the same coordinate.The second line contains four integers \(b_1, \ldots, b_4\) (\(-10^9 \leq b_i \leq 10^9\)) β target coordinates of the stones. There may... | If there is no sequence of operations that achieves the goal, print a single integer \(-1\). Otherwise, on the first line print a single integer \(k\) (\(0 \leq k \leq 1000\)) β the number of operations in your sequence. On the next \(k\) lines, describe the operations. The \(i\)-th of these lines should contain two in... | Input: 0 1 2 3 3 5 6 8 | Output: 3 1 3 2 5 0 3 | Master | 1 | 1,040 | 369 | 870 | 12 | |
2,037 | F | 2037F | F. Ardent Flames | 2,100 | binary search; data structures; math; sortings; two pointers | You have obtained the new limited event character Xilonen. You decide to use her in combat.There are \(n\) enemies in a line. The \(i\)'th enemy from the left has health \(h_i\) and is currently at position \(x_i\). Xilonen has an attack damage of \(m\), and you are ready to defeat the enemies with her.Xilonen has a po... | The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains three integers \(n\), \(m\), and \(k\) (\(1 \leq k \leq n \leq 10^5\), \(1 \leq m \leq 10^9\)).The following line contains \(n\) integers \(h_1, h_2, ..., h_n\) (\(1 \leq h_i \leq 10^9\... | For each test case, output an integer on a new line, the minimum number of attacks that must be performed to defeat at least \(k\) enemies. If it is impossible to find a \(p\) such that eventually at least \(k\) enemies will be defeated, output \(-1\) instead. | In the first testcase, it is optimal to select \(p=2\). Each attack, the first enemy takes \(5-|2-1|=4\) damage, the second enemy takes \(5\) damage, the third enemy takes \(4\) damage, the fourth enemy takes \(3\) damage, and the fifth enemy takes \(2\) damage. After \(2\) attacks, the first three enemies will be defe... | Input: 65 5 37 7 7 7 71 2 3 4 59 5 92 4 6 8 10 8 6 4 21 2 3 4 5 6 7 8 92 10 21 11 202 10 169696969 4204204201 202 10 210 151 192 2 21000000000 11 3 | Output: 2 2 -1 6969697 15 1000000000 | Hard | 5 | 1,359 | 554 | 260 | 20 |
1,965 | E | 1965E | E. Connected Cubes | 3,100 | constructive algorithms; games | There are \(n \cdot m\) unit cubes currently in positions \((1, 1, 1)\) through \((n, m, 1)\). Each of these cubes is one of \(k\) colors. You want to add additional cubes at any integer coordinates such that the subset of cubes of each color is connected, where two cubes are considered connected if they share a face.I... | The first line of the input contains three integers \(n\), \(m\), and \(k\) (\(2 \le n, m, k \le 50\)) β the number of rows and columns of cubes, and the number of colors, respectively.The \(i\)-th of the next \(n\) lines contains \(m\) integers. The \(j\)-th of these is \(a_{ij}\) (\(1 \le a_{ij} \le k\)) β the color ... | If there is no solution, print a single integer \(-1\).Otherwise, the first line of output should contain a single integer \(p\) (\(0 \le p \le 4 \cdot 10^5\)) β the number of additional cubes you will add.The next \(p\) lines should contain four integers \(x\), \(y\), \(z\) and \(c\) (\(1 \le x, y, z \le 10^6\), \(1 \... | The image in the statement corresponds to the first example case, with \(\text{red} = 1\), \(\text{blue} = 2\), \(\text{green} = 3\). | Input: 3 4 33 2 3 11 1 1 11 3 3 2 | Output: 13 1 1 2 3 1 3 2 3 2 1 2 3 2 2 2 3 2 3 2 3 3 3 2 3 1 2 2 2 1 2 3 2 1 3 3 2 1 4 3 2 2 4 3 2 3 4 3 2 3 4 2 2 | Master | 2 | 1,029 | 472 | 605 | 19 |
73 | C | 73C | C. LionAge II | 1,800 | dp | Vasya plays the LionAge II. He was bored of playing with a stupid computer, so he installed this popular MMORPG, to fight with his friends. Vasya came up with the name of his character β non-empty string s, consisting of a lowercase Latin letters. However, in order not to put up a front of friends, Vasya has decided to... | The first line contains character's name s and an integer number k (0 β€ k β€ 100). The length of the nonempty string s does not exceed 100. The second line contains an integer number n (0 β€ n β€ 676) β amount of pairs of letters, giving bonus to the euphony. The next n lines contain description of these pairs Β«x y cΒ», wh... | Output the only number β maximum possible euphony ΠΎf the new character's name. | In the first example the most euphony name will be looser. It is easy to calculate that its euphony is 36. | Input: winner 44s e 7o s 8l o 13o o 8 | Output: 36 | Medium | 1 | 712 | 482 | 78 | 0 |
661 | H | 661H | H. Rotate Matrix | 2,400 | *special | You are given a square matrix of integer numbers. Rotate it 90 degrees clockwise (see examples for clarification of rotation). | The input consists of n lines (1 β€ n β€ 10, n is not given explicitly). Each of the lines contains n space-separated integers; j-th integer in i-th line corresponds to matrix element mij (1 β€ mij β€ 100). | Output the rotated matrix in the same format as the input. | Input: 1 2 34 5 67 8 9 | Output: 7 4 18 5 29 6 3 | Expert | 1 | 126 | 202 | 58 | 6 | |
171 | B | 171B | B. Star | 1,300 | *special; combinatorics | The input contains a single integer a (1 β€ a β€ 18257). | Print a single integer output (1 β€ output β€ 2Β·109). | Input: 2 | Output: 13 | Easy | 2 | 0 | 54 | 51 | 1 | ||
613 | C | 613C | C. Necklace | 2,500 | constructive algorithms; math | Ivan wants to make a necklace as a present to his beloved girl. A necklace is a cyclic sequence of beads of different colors. Ivan says that necklace is beautiful relative to the cut point between two adjacent beads, if the chain of beads remaining after this cut is a palindrome (reads the same forward and backward). I... | The first line of the input contains a single number n (1 β€ n β€ 26) β the number of colors of beads. The second line contains after n positive integers ai β the quantity of beads of i-th color. It is guaranteed that the sum of ai is at least 2 and does not exceed 100 000. | In the first line print a single number β the maximum number of beautiful cuts that a necklace composed from given beads may have. In the second line print any example of such necklace.Each color of the beads should be represented by the corresponding lowercase English letter (starting with a). As the necklace is cycli... | In the first sample a necklace can have at most one beautiful cut. The example of such a necklace is shown on the picture.In the second sample there is only one way to compose a necklace. | Input: 34 2 1 | Output: 1abacaba | Expert | 2 | 525 | 272 | 356 | 6 |
1,512 | G | 1512G | G. Short Task | 1,700 | brute force; dp; math; number theory | Let us denote by \(d(n)\) the sum of all divisors of the number \(n\), i.e. \(d(n) = \sum\limits_{k | n} k\).For example, \(d(1) = 1\), \(d(4) = 1+2+4=7\), \(d(6) = 1+2+3+6=12\).For a given number \(c\), find the minimum \(n\) such that \(d(n) = c\). | The first line contains one integer \(t\) (\(1 \le t \le 10^4\)). Then \(t\) test cases follow.Each test case is characterized by one integer \(c\) (\(1 \le c \le 10^7\)). | For each test case, output: ""-1"" if there is no such \(n\) that \(d(n) = c\); \(n\), otherwise. | Input: 12 1 2 3 4 5 6 7 8 9 10 39 691 | Output: 1 -1 2 3 -1 5 4 7 -1 -1 18 -1 | Medium | 4 | 250 | 171 | 97 | 15 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.