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,916 | H2 | 1916H2 | H2. Matrix Rank (Hard Version) | 2,700 | combinatorics; dp; math; matrices; string suffix structures | This is the hard version of the problem. The only differences between the two versions of this problem are the constraints on \(k\). You can make hacks only if all versions of the problem are solved.You are given integers \(n\), \(p\) and \(k\). \(p\) is guaranteed to be a prime number. For each \(r\) from \(0\) to \(k... | The first line of input contains three integers \(n\), \(p\) and \(k\) (\(1 \leq n \leq 10^{18}\), \(2 \leq p < 998\,244\,353\), \(0 \leq k \leq 5 \cdot 10^5\)).It is guaranteed that \(p\) is a prime number. | Output \(k+1\) integers, the answers for each \(r\) from \(0\) to \(k\). | Input: 3 2 3 | Output: 1 49 294 168 | Master | 5 | 699 | 207 | 72 | 19 | |
1,189 | B | 1189B | B. Number Circle | 1,100 | greedy; math; sortings | You are given \(n\) numbers \(a_1, a_2, \ldots, a_n\). Is it possible to arrange them in a circle in such a way that every number is strictly less than the sum of its neighbors?For example, for the array \([1, 4, 5, 6, 7, 8]\), the arrangement on the left is valid, while arrangement on the right is not, as \(5\ge 4 + 1... | The first line contains a single integer \(n\) (\(3\le n \le 10^5\)) β the number of numbers.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \le 10^9\)) β the numbers. The given numbers are not necessarily distinct (i.e. duplicates are allowed). | If there is no solution, output ""NO"" in the first line. If there is a solution, output ""YES"" in the first line. In the second line output \(n\) numbers β elements of the array in the order they will stay in the circle. The first and the last element you output are considered neighbors in the circle. If there are mu... | One of the possible arrangements is shown in the first example: \(4< 2 + 3\);\(2 < 4 + 3\);\(3< 4 + 2\).One of the possible arrangements is shown in the second example.No matter how we arrange \(13, 8, 5\) in a circle in the third example, \(13\) will have \(8\) and \(5\) as neighbors, but \(13\ge 8 + 5\). There is no ... | Input: 3 2 4 3 | Output: YES 4 2 3 | Easy | 3 | 340 | 277 | 409 | 11 |
1,934 | D2 | 1934D2 | D2. XOR Break β Game Version | 2,400 | bitmasks; games; greedy; interactive | This is an interactive problem.This is the game version of the problem. Note that the solution of this problem may or may not share ideas with the solution of the solo version. You can solve and get points for both versions independently.Alice and Bob are playing a game. The game starts with a positive integer \(n\), w... | Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.The only line of each test case contains a single integer \(n\) (\(1 \leq n \leq 10^{18}\)) β the number the game starts with. | Explanation for the interaction.Interactor / BobAliceExplanation4\(t\)1\(n\) for the first test casesecondAlice chooses to go second0 0Bob says he cannot break \(p = 1\)3\(n\) for the second test casefirstAlice chooses to go first1 2Alice breaks \(p = 3\) into \(p_1 = 1\) and \(p_2 = 2\)0 0Bob says he cannot break \(p ... | Input: 4 1 0 0 3 0 0 13 3 4 0 0 777777770001 0 0 | Output: second first 2 1 first 10 7 1 2 first 777777770000 1 | Expert | 4 | 1,015 | 274 | 0 | 19 | |
1,558 | B | 1558B | B. Up the Strip | 1,900 | brute force; dp; math; number theory; two pointers | Note that the memory limit in this problem is lower than in others.You have a vertical strip with \(n\) cells, numbered consecutively from \(1\) to \(n\) from top to bottom.You also have a token that is initially placed in cell \(n\). You will move the token up until it arrives at cell \(1\).Let the token be in cell \(... | The only line contains two integers \(n\) and \(m\) (\(2 \le n \le 4 \cdot 10^6\); \(10^8 < m < 10^9\); \(m\) is a prime number) β the length of the strip and the modulo. | Print the number of ways to move the token from cell \(n\) to cell \(1\), modulo \(m\). | In the first test, there are three ways to move the token from cell \(3\) to cell \(1\) in one shift: using subtraction of \(y = 2\), or using division by \(z = 2\) or \(z = 3\).There are also two ways to move the token from cell \(3\) to cell \(1\) via cell \(2\): first subtract \(y = 1\), and then either subtract \(y... | Input: 3 998244353 | Output: 5 | Hard | 5 | 1,048 | 170 | 87 | 15 |
1,002 | C1 | 1002C1 | C1. Distinguish zero state and plus state with minimum error | 1,700 | *special | You are given a qubit which is guaranteed to be either in state or in state. Your task is to perform necessary operations and measurements to figure out which state it was and to return 0 if it was a state or 1 if it was state. The state of the qubit after the operations does not matter.Note that these states are not o... | Medium | 1 | 847 | 0 | 0 | 10 | ||||
1,381 | B | 1381B | B. Unmerge | 1,800 | dp | Let \(a\) and \(b\) be two arrays of lengths \(n\) and \(m\), respectively, with no elements in common. We can define a new array \(\mathrm{merge}(a,b)\) of length \(n+m\) recursively as follows: If one of the arrays is empty, the result is the other array. That is, \(\mathrm{merge}(\emptyset,b)=b\) and \(\mathrm{merge... | The first line contains a single integer \(t\) (\(1\le t\le 1000\)) β the number of test cases. Next \(2t\) lines contain descriptions of test cases. The first line of each test case contains a single integer \(n\) (\(1\le n\le 2000\)).The second line of each test case contains \(2n\) integers \(p_1,\ldots,p_{2n}\) (\(... | For each test case, output ""YES"" if there exist arrays \(a\), \(b\), each of length \(n\) and with no common elements, so that \(p=\mathrm{merge}(a,b)\). Otherwise, output ""NO"". | In the first test case, \([2,3,1,4]=\mathrm{merge}([3,1],[2,4])\).In the second test case, we can show that \([3,1,2,4]\) is not the merge of two arrays of length \(2\).In the third test case, \([3,2,6,1,5,7,8,4]=\mathrm{merge}([3,2,8,4],[6,1,5,7])\).In the fourth test case, \([1,2,3,4,5,6]=\mathrm{merge}([1,3,6],[2,4,... | Input: 6 2 2 3 1 4 2 3 1 2 4 4 3 2 6 1 5 7 8 4 3 1 2 3 4 5 6 4 6 1 3 7 4 5 8 2 6 4 3 2 5 1 11 9 12 8 6 10 7 | Output: YES NO YES YES NO NO | Medium | 1 | 1,762 | 470 | 181 | 13 |
803 | F | 803F | F. Coprime Subsequences | 2,000 | bitmasks; combinatorics; number theory | Let's call a non-empty sequence of positive integers a1, a2... ak coprime if the greatest common divisor of all elements of this sequence is equal to 1.Given an array a consisting of n positive integers, find the number of its coprime subsequences. Since the answer may be very large, print it modulo 109 + 7.Note that t... | The first line contains one integer number n (1 β€ n β€ 100000).The second line contains n integer numbers a1, a2... an (1 β€ ai β€ 100000). | Print the number of coprime subsequences of a modulo 109 + 7. | In the first example coprime subsequences are: 1 1, 2 1, 3 1, 2, 3 2, 3 In the second example all subsequences are coprime. | Input: 31 2 3 | Output: 5 | Hard | 3 | 483 | 136 | 61 | 8 |
320 | A | 320A | A. Magic Numbers | 900 | brute force; greedy | A magic number is a number formed by concatenation of numbers 1, 14 and 144. We can use each of these numbers any number of times. Therefore 14144, 141414 and 1411 are magic numbers but 1444, 514 and 414 are not.You're given a number. Determine if it is a magic number or not. | The first line of input contains an integer n, (1 β€ n β€ 109). This number doesn't contain leading zeros. | Print ""YES"" if n is a magic number or print ""NO"" if it's not. | Input: 114114 | Output: YES | Beginner | 2 | 276 | 104 | 65 | 3 | |
2,024 | A | 2024A | A. Profitable Interest Rate | 800 | greedy; math | Alice has \(a\) coins. She can open a bank deposit called ""Profitable"", but the minimum amount required to open this deposit is \(b\) coins.There is also a deposit called ""Unprofitable"", which can be opened with any amount of coins. Alice noticed that if she opens the ""Unprofitable"" deposit with \(x\) coins, the ... | 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.A single line of each test case contains two integers \(a\) and \(b\) (\(1 \le a, b \le 10^9\)) β the number of coins Alice has and the i... | For each test case, output a single integer β the maximum number of coins that Alice can deposit into the ""Profitable"" deposit. If Alice can never open the ""Profitable"" deposit, output \(0\). | In the first test case, \(a \ge b\), so Alice can immediately open the ""Profitable"" deposit with all \(10\) coins.In the second test case, Alice can open the ""Unprofitable"" deposit with \(2\) coins. Then she will have \(5\) coins left, but the minimum amount required to open the ""Profitable"" deposit will decrease... | Input: 510 57 95 1001 11 2 | Output: 10 5 0 1 0 | Beginner | 2 | 742 | 386 | 195 | 20 |
1,469 | E | 1469E | E. A Bit Similar | 2,400 | bitmasks; brute force; hashing; string suffix structures; strings; two pointers | Let's call two strings \(a\) and \(b\) (both of length \(k\)) a bit similar if they have the same character in some position, i. e. there exists at least one \(i \in [1, k]\) such that \(a_i = b_i\).You are given a binary string \(s\) of length \(n\) (a string of \(n\) characters 0 and/or 1) and an integer \(k\). Let's... | The first line contains one integer \(q\) (\(1 \le q \le 10000\)) β the number of test cases. Each test case consists of two lines.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 10^6\)). The second line contains the string \(s\), consisting of \(n\) characters (each characte... | For each test case, print the answer as follows: if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example); otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the seco... | Input: 7 4 2 0110 4 2 1001 9 3 010001110 9 3 101110001 10 3 0101110001 10 10 1111111111 11 10 11111111110 | Output: YES 11 YES 00 YES 010 YES 101 NO YES 0000000001 YES 0000000010 | Expert | 6 | 1,101 | 424 | 421 | 14 | |
690 | E1 | 690E1 | E1. Photographs (I) | 1,800 | The Human-Cow Confederation (HC2), led by Heidi, has built a base where people and cows can hide, guarded from zombie attacks. The entrance to the base is protected by an automated gate which performs a kind of a Turing test: it shows the entering creature a photograph and asks them whether the top and bottom halves of... | The first line of the input contains the number q of questions (1 β€ q β€ 220). After that, q questions follow, each of which in the format described below.The first line of every question contains two space-separated integers h and w (1 β€ h, w β€ 600) β the height (number of rows) and width (number of columns) of the pho... | Your program should print q lines. The i-th line should contain your answer for the i-th question: YES if the photograph has been rearranged and NO otherwise. Your answers will be accepted if they all conform to this format and if at least 75% of them are correct.Because the input is rather huge, feel free to process i... | The link to download all necessary files is http://assets.codeforces.com/files/690/easy_contestant_package.zip | Medium | 0 | 980 | 1,492 | 445 | 6 | ||
2,027 | D1 | 2027D1 | D1. The Endspeaker (Easy Version) | 1,700 | binary search; dp; graphs; greedy; implementation; two pointers | This is the easy version of this problem. The only difference is that you only need to output the minimum total cost of operations in this version. You must solve both versions to be able to hack.You're given an array \(a\) of length \(n\), and an array \(b\) of length \(m\) (\(b_i > b_{i+1}\) for all \(1 \le i < m\)).... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)). The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n, m \le 3 \cdot 10^5\), \(\boldsymbol{1 \le n \cdot m \le 3 \cdot 10^5}\)).The... | For each test case, if it's possible to make \(a\) empty, then output the minimum total cost of the operations.If there is no possible sequence of operations which makes \(a\) empty, then output a single integer \(-1\). | In the first test case, one optimal sequence of operations which yields a total cost of \(1\) is as follows: Perform an operation of type \(2\). Choose the prefix to be \([9]\). This incurs a cost of \(1\). Perform an operation of type \(1\). The value of \(k\) is now \(2\). This incurs no cost. Perform an operation of... | Input: 54 29 3 4 311 71 22019 1810 22 5 2 1 10 3 2 9 9 617 910 112 2 2 2 2 2 2 2 2 220 18 16 14 12 10 8 6 4 2 11 61032 16 8 4 2 1 | Output: 1 -1 2 10 4 | Medium | 6 | 977 | 716 | 219 | 20 |
1,165 | F1 | 1165F1 | F1. Microtransactions (easy version) | 2,000 | binary search; greedy | The only difference between easy and hard versions is constraints.Ivan plays a computer game that contains some microtransactions to make characters look cooler. Since Ivan wants his character to be really cool, he wants to use some of these microtransactions β and he won't start playing until he gets all of them.Each ... | The first line of the input contains two integers \(n\) and \(m\) (\(1 \le n, m \le 1000\)) β the number of types of microtransactions and the number of special offers in the game shop.The second line of the input contains \(n\) integers \(k_1, k_2, \dots, k_n\) (\(0 \le k_i \le 1000\)), where \(k_i\) is the number of ... | Print one integer β the minimum day when Ivan can order all microtransactions he wants and actually start playing. | Input: 5 6 1 2 0 2 0 2 4 3 3 1 5 1 2 1 5 2 3 | Output: 8 | Hard | 2 | 1,261 | 776 | 114 | 11 | |
17 | B | 17B | B. Hierarchy | 1,500 | dfs and similar; dsu; greedy; shortest paths | Nick's company employed n people. Now Nick needs to build a tree hierarchy of Β«supervisor-surbodinateΒ» relations in the company (this is to say that each employee, except one, has exactly one supervisor). There are m applications written in the following form: Β«employee ai is ready to become a supervisor of employee bi... | The first input line contains integer n (1 β€ n β€ 1000) β amount of employees in the company. The following line contains n space-separated numbers qj (0 β€ qj β€ 106)β the employees' qualifications. The following line contains number m (0 β€ m β€ 10000) β amount of received applications. The following m lines contain the a... | Output the only line β the minimum cost of building such a hierarchy, or -1 if it is impossible to build it. | In the first sample one of the possible ways for building a hierarchy is to take applications with indexes 1, 2 and 4, which give 11 as the minimum total cost. In the second sample it is impossible to build the required hierarchy, so the answer is -1. | Input: 47 2 3 141 2 52 4 13 4 11 3 5 | Output: 11 | Medium | 4 | 561 | 648 | 108 | 0 |
1,361 | B | 1361B | B. Johnny and Grandmaster | 1,900 | greedy; implementation; math; sortings | Johnny has just found the new, great tutorial: ""How to become a grandmaster?"". The tutorial tells many strange and unexpected for Johnny things, such as you have to be patient or that very important is solving many harder and harder problems. The boy has found an online judge with tasks divided by topics they cover. ... | Input consists of multiple test cases. The first line contains one integer \(t\) \((1 \leq t \leq 10^5)\) β the number of test cases. Each test case is described as follows:The first line contains two integers \(n\) and \(p\) \((1 \leq n, p \leq 10^6)\). The second line contains \(n\) integers \(k_i\) \((0 \leq k_i \le... | Output one integer β the reminder of division the answer by \(1\,000\,000\,007\). | You have to minimize the difference, not it's remainder. For example, if the minimum difference is equal to \(2\), but there is also a distribution where the difference is \(10^9 + 8\), then the answer is \(2\), not \(1\).In the first test case of the example, there're the following numbers: \(4\), \(8\), \(16\), \(16\... | Input: 4 5 2 2 3 4 4 3 3 1 2 10 1000 4 5 0 1 1 100 1 8 89 | Output: 4 1 146981438 747093407 | Hard | 4 | 985 | 391 | 81 | 13 |
1,486 | F | 1486F | F. Pairs of Paths | 2,600 | combinatorics; data structures; dfs and similar; dp; trees | You are given a tree consisting of \(n\) vertices, and \(m\) simple vertex paths. Your task is to find how many pairs of those paths intersect at exactly one vertex. More formally you have to find the number of pairs \((i, j)\) \((1 \leq i < j \leq m)\) such that \(path_i\) and \(path_j\) have exactly one vertex in com... | First line contains a single integer \(n\) \((1 \leq n \leq 3 \cdot 10^5)\).Next \(n - 1\) lines describe the tree. Each line contains two integers \(u\) and \(v\) \((1 \leq u, v \leq n)\) describing an edge between vertices \(u\) and \(v\).Next line contains a single integer \(m\) \((1 \leq m \leq 3 \cdot 10^5)\).Next... | Output a single integer β the number of pairs of paths that intersect at exactly one vertex. | The tree in the first example and paths look like this. Pairs \((1,4)\) and \((3,4)\) intersect at one vertex.In the second example all three paths contain the same single vertex, so all pairs \((1, 2)\), \((1, 3)\) and \((2, 3)\) intersect at one vertex.The third example is the same as the first example with two addit... | Input: 5 1 2 1 3 1 4 3 5 4 2 3 2 4 3 4 3 5 | Output: 2 | Expert | 5 | 324 | 544 | 92 | 14 |
1,611 | C | 1611C | C. Polycarp Recovers the Permutation | 1,000 | constructive algorithms | Polycarp wrote on a whiteboard an array \(p\) of length \(n\), which is a permutation of numbers from \(1\) to \(n\). In other words, in \(p\) each number from \(1\) to \(n\) occurs exactly once.He also prepared a resulting array \(a\), which is initially empty (that is, it has a length of \(0\)).After that, he did exa... | The first line of the input contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases in the test.Each test case consists of two lines. The first of them contains an integer \(n\) (\(1 \le n \le 2\cdot10^5\)) β the length of the array \(a\). The second line contains \(n\) integers \(a_1, a_2, \dots, a... | Print \(t\) lines, each of the lines must contain the answer to the corresponding set of input data: numbers \(p_1, p_2, \dots, p_n\) β any of the possible initial values of the array \(p\), which will lead to the given array \(a\). All elements of \(p\) are distinct integers from \(1\) to \(n\). Thus, if there are sev... | The first test case in the example is clarified in the main section of the problem statement. There may be other correct answers for this test set.In the second test case, \(n=1\). Thus, there is only one permutation that can be the answer: \(p=[1]\). Indeed, this is the answer to this test case.In the third test case ... | Input: 4 4 1 3 2 4 1 1 5 1 3 5 4 2 3 3 2 1 | Output: 3 1 4 2 1 -1 2 3 1 | Beginner | 1 | 1,998 | 548 | 398 | 16 |
140 | F | 140F | F. New Year Snowflake | 2,600 | geometry; sortings | As Gerald ..., in other words, on a New Year Eve Constantine prepared an unusual present for the Beautiful Lady. The present is the magic New Year snowflake that can make any dream come true.The New Year snowflake consists of tiny ice crystals, which can be approximately regarded as points on the plane. The beauty of t... | The first line contains two integers n and k (1 β€ n β€ 200 000, 0 β€ k β€ 10) β the number of the surviving snowflake crystals and the maximum number of melted crystals, correspondingly. Next n lines contain the coordinates of the crystals that are left in the following form: ""xi yi"". The coordinates are integers and do... | The first line contains an integer c β the number of possible symmetry centers. Next c lines should contain the centers' descriptions. Each symmetry center is described by a couple of coordinates ""x y"", separated by a space. Print the coordinates with absolute error not exceeding 10 - 6. You are allowed to print the ... | Input: 4 00 00 11 01 1 | Output: 10.5 0.5 | Expert | 2 | 1,355 | 388 | 486 | 1 | |
1,674 | E | 1674E | E. Breaking the Wall | 2,000 | binary search; brute force; constructive algorithms; greedy; math | Monocarp plays ""Rage of Empires II: Definitive Edition"" β a strategic computer game. Right now he's planning to attack his opponent in the game, but Monocarp's forces cannot enter the opponent's territory since the opponent has built a wall.The wall consists of \(n\) sections, aligned in a row. The \(i\)-th section i... | The first line contains one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the number of sections.The second line contains the sequence of integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^6\)), where \(a_i\) is the initial durability of the \(i\)-th section. | Print one integer β the minimum number of onager shots needed to break at least two sections of the wall. | In the first example, it is possible to break the \(2\)-nd and the \(4\)-th section in \(10\) shots, for example, by shooting the third section \(10\) times. After that, the durabilities become \([20, 0, 10, 0, 20]\). Another way of doing it is firing \(5\) shots at the \(2\)-nd section, and another \(5\) shots at the ... | Input: 5 20 10 30 10 20 | Output: 10 | Hard | 5 | 1,209 | 263 | 105 | 16 |
939 | A | 939A | A. Love Triangle | 800 | graphs | As you could know there are no male planes nor female planes. However, each plane on Earth likes some other plane. There are n planes on Earth, numbered from 1 to n, and the plane with number i likes the plane with number fi, where 1 β€ fi β€ n and fi β i.We call a love triangle a situation in which plane A likes plane B... | The first line contains a single integer n (2 β€ n β€ 5000) β the number of planes.The second line contains n integers f1, f2, ..., fn (1 β€ fi β€ n, fi β i), meaning that the i-th plane likes the fi-th. | Output Β«YESΒ» if there is a love triangle consisting of planes on Earth. Otherwise, output Β«NOΒ».You can output any letter in lower case or in upper case. | In first example plane 2 likes plane 4, plane 4 likes plane 1, plane 1 likes plane 2 and that is a love triangle.In second example there are no love triangles. | Input: 52 4 5 1 3 | Output: YES | Beginner | 1 | 419 | 199 | 152 | 9 |
1,630 | A | 1630A | A. And Matching | 1,500 | bitmasks; constructive algorithms | You are given a set of \(n\) (\(n\) is always a power of \(2\)) elements containing all integers \(0, 1, 2, \ldots, n-1\) exactly once.Find \(\frac{n}{2}\) pairs of elements such that: Each element in the set is in exactly one pair. The sum over all pairs of the bitwise AND of its elements must be exactly equal to \(k\... | The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 400\)) β the number of test cases. Description of the test cases follows.Each test case consists of a single line with two integers \(n\) and \(k\) (\(4 \leq n \leq 2^{16}\), \(n\) is a power of \(2\), \(0 \leq k ... | For each test case, if there is no solution, print a single line with the integer \(-1\).Otherwise, print \(\frac{n}{2}\) lines, the \(i\)-th of them must contain \(a_i\) and \(b_i\), the elements in the \(i\)-th pair. If there are many solutions, print any of them. Print the pairs and the elements in the pairs in any ... | In the first test, \((0\&3)+(1\&2) = 0\).In the second test, \((0\&2)+(1\&3) = 1\).In the third test, \((0\&1)+(2\&3) = 2\).In the fourth test, there is no solution. | Input: 44 04 14 24 3 | Output: 0 3 1 2 0 2 1 3 0 1 2 3 -1 | Medium | 2 | 611 | 464 | 326 | 16 |
1,730 | C | 1730C | C. Minimum Notation | 1,200 | data structures; greedy; math; sortings | You have a string \(s\) consisting of digits from \(0\) to \(9\) inclusive. You can perform the following operation any (possibly zero) number of times: You can choose a position \(i\) and delete a digit \(d\) on the \(i\)-th position. Then insert the digit \(\min{(d + 1, 9)}\) on any position (at the beginning, at the... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Then the test cases follow.Each test case consists of a single line that contains one string \(s\) (\(1 \le |s| \le 2 \cdot 10^5\)) β the string consisting of digits. Please note that \(s\) is just a string consisting of d... | Print a single string β the minimum string that is possible to obtain. | In the first test case: Delete \(8\) and insert \(9\) at the end of the notation. The resulting notation is \(04299\). Delete \(4\) and insert \(5\) in the \(3\)-rd position of the notation. The resulting notation is \(02599\). Nothing needs to be done in the second and third test cases. | Input: 404829901314752277691991 | Output: 02599 9 01 111334567888999 | Easy | 4 | 700 | 459 | 70 | 17 |
356 | C | 356C | C. Compartments | 2,100 | combinatorics; constructive algorithms; greedy; implementation | A team of students from the city S is sent to the All-Berland Olympiad in Informatics. Traditionally, they go on the train. All students have bought tickets in one carriage, consisting of n compartments (each compartment has exactly four people). We know that if one compartment contain one or two students, then they ge... | The first line contains integer n (1 β€ n β€ 106) β the number of compartments in the carriage. The second line contains n integers a1, a2, ..., an showing how many students ride in each compartment (0 β€ ai β€ 4). It is guaranteed that at least one student is riding in the train. | If no sequence of swapping seats with other people leads to the desired result, print number ""-1"" (without the quotes). In another case, print the smallest number of people you need to persuade to swap places. | Input: 51 2 2 4 3 | Output: 2 | Hard | 4 | 1,198 | 277 | 211 | 3 | |
645 | D | 645D | D. Robot Rapping Results Report | 1,800 | binary search; dp; graphs | While Farmer John rebuilds his farm in an unfamiliar portion of Bovinia, Bessie is out trying some alternative jobs. In her new gig as a reporter, Bessie needs to know about programming competition results as quickly as possible. When she covers the 2016 Robot Rap Battle Tournament, she notices that all of the robots o... | The first line of the input consists of two integers, the number of robots n (2 β€ n β€ 100 000) and the number of rap battles m ().The next m lines describe the results of the rap battles in the order they took place. Each consists of two integers ui and vi (1 β€ ui, vi β€ n, ui β vi), indicating that robot ui beat robot ... | Print the minimum k such that the ordering of the robots by skill level is uniquely defined by the first k rap battles. If there exists more than one ordering that satisfies all m relations, output -1. | In the first sample, the robots from strongest to weakest must be (4, 2, 1, 3), which Bessie can deduce after knowing the results of the first four rap battles.In the second sample, both (1, 3, 2) and (3, 1, 2) are possible orderings of the robots from strongest to weakest after both rap battles. | Input: 4 52 11 32 34 24 3 | Output: 4 | Medium | 3 | 845 | 482 | 201 | 6 |
603 | E | 603E | E. Pastoral Oddities | 3,000 | data structures; divide and conquer; dsu; math; trees | In the land of Bovinia there are n pastures, but no paths connecting the pastures. Of course, this is a terrible situation, so Kevin Sun is planning to rectify it by constructing m undirected paths connecting pairs of distinct pastures. To make transportation more efficient, he also plans to pave some of these new path... | The first line contains two integers n (2 β€ n β€ 100 000) and m (1 β€ m β€ 300 000), denoting the number of pastures and paths, respectively. The next m lines each contain three integers ai, bi and li, describing the i-th path. The i-th path connects pastures ai and bi (1 β€ ai, bi β€ n; ai β bi) and has length li (1 β€ li β€... | Output m lines. The i-th line should contain a single integer denoting the minimum possible length of the longest path (maximum-weight edge) in a sunny paving using only the first i paths. If Kevin cannot pave a set of paths so that each pasture is incident to an odd number of paved paths, output - 1.Note that the pavi... | For the first sample, these are the paths that Kevin should pave after building the i-th path: No set of paths works. Paths 1 (length 4) and 2 (length 8). Paths 1 (length 4) and 2 (length 8). Paths 3 (length 2) and 4 (length 3). In the second sample, there never exists a paving that makes Kevin happy. | Input: 4 41 3 42 4 81 2 23 4 3 | Output: -1883 | Master | 5 | 940 | 386 | 438 | 6 |
100 | C | 100C | C. A+B | 1,400 | *special; implementation | Bijan is new to programming. He learned recently that programmers do not code every bit of their apps from scratch.For example they never write a code to sum two integers, because their languages have the ability to do the sum. But can they use it? Can an experienced coder who has attended more than 100 contests, sum t... | You are given two integers a and b, one per line (1 β€ a, b < 10500). These numbers will not have any leading zeros. | Write sum of the two integers. Do not put any leading zeros. | Input: 23 | Output: 5 | Easy | 2 | 382 | 115 | 60 | 1 | |
214 | A | 214A | A. System of Equations | 800 | brute force | Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?You are given a system of equations: You should count, how many there are pairs of integer... | A single line contains two integers n, m (1 β€ n, m β€ 1000) β the parameters of the system. The numbers on the line are separated by a space. | On a single line print the answer to the problem. | In the first sample the suitable pair is integers (3, 0). In the second sample the suitable pair is integers (3, 5). In the third sample there is no suitable pair. | Input: 9 3 | Output: 1 | Beginner | 1 | 365 | 140 | 49 | 2 |
1,942 | H | 1942H | H. Farmer John's Favorite Intern | 3,500 | data structures; dp; flows; trees | Peaches...β Ruby just won an internship position at Farmer John's farm by winning a coding competition! As the newly recruited intern, Ruby is tasked with maintaining Farmer John's peach tree, a tree consisting of \(n\) nodes rooted at node \(1\). Each node initially contains \(a_i = 0\) peaches, and there are two types... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(q\) (\(1 \le n, q \le 2 \cdot 10^5\)) β the size of the tree and the number of operations.The second line contains \(n - 1\) integers \(p_2, p_3, \ldots, p_... | For each test case, output \(q\) lines. The \(i\)-th line should contain ""YES"" if Ruby can make the peach tree healthy after performing operations \(1, 2, \ldots, i\) in some order. Otherwise, it should contain ""NO"".You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", "... | For the prefix containing operations \(1, 2, \ldots, 5\) in the first test case, Ruby may perform the operations in the following order: Ruby performs operation \(2\) and chooses to increase \(a_4\) by \(9\) and \(a_5\) by \(8\). Ruby performs operation \(1\) and chooses to increase \(a_1\) by \(5\), \(a_3\) by \(2\), ... | Input: 28 81 1 1 4 3 6 65 6 2 9 8 4 1 31 3 141 4 171 2 72 2 11 6 12 1 10000001 4 9999991 3 110 201 1 1 2 5 2 4 7 2311353 270334 74853 385085 315501 183346 234819 417314 103862 4294371 1 8375411 10 9338761 1 5659581 4 7914552 3 850542 3 4409781 4 9810401 5 685222 1 8583052 4 1843081 4 9050812 8 5196262 2 2690901 1 43016... | Master | 4 | 1,661 | 1,031 | 381 | 19 |
1,657 | C | 1657C | C. Bracket Sequence Deletion | 1,200 | greedy; implementation | You are given a bracket sequence consisting of \(n\) characters '(' and/or )'. You perform several operations with it.During one operation, you choose the shortest prefix of this string (some amount of first characters of the string) that is good and remove it from the string.The prefix is considered good if one of the... | The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The next \(2t\) lines describe test cases.The first line of the test case contains one integer \(n\) (\(1 \le n \le 5 \cdot 10^5\)) β the length of the bracket sequence.The second line of the test case contains \(n... | For each test case, print two integers \(c\) and \(r\) β the number of operations you will perform on the given bracket sequence and the number of characters that remain in the string after performing all operations. | Input: 52()3())4((((5)((()6)((()( | Output: 1 0 1 1 2 0 1 0 1 1 | Easy | 2 | 1,172 | 499 | 216 | 16 | |
327 | C | 327C | C. Magic Five | 1,700 | combinatorics; math | There is a long plate s containing n digits. Iahub wants to delete some digits (possibly none, but he is not allowed to delete all the digits) to form his ""magic number"" on the plate, a number that is divisible by 5. Note that, the resulting number may contain leading zeros.Now Iahub wants to count the number of ways... | In the first line you're given a string a (1 β€ |a| β€ 105), containing digits only. In the second line you're given an integer k (1 β€ k β€ 109). The plate s is formed by concatenating k copies of a together. That is n = |a|Β·k. | Print a single integer β the required number of ways modulo 1000000007 (109 + 7). | In the first case, there are four possible ways to make a number that is divisible by 5: 5, 15, 25 and 125.In the second case, remember to concatenate the copies of a. The actual plate is 1399013990.In the third case, except deleting all digits, any choice will do. Therefore there are 26 - 1 = 63 possible ways to delet... | Input: 12561 | Output: 4 | Medium | 2 | 517 | 224 | 81 | 3 |
31 | A | 31A | A. Worms Evolution | 1,200 | implementation | Professor Vasechkin is studying evolution of worms. Recently he put forward hypotheses that all worms evolve by division. There are n forms of worms. Worms of these forms have lengths a1, a2, ..., an. To prove his theory, professor needs to find 3 different forms that the length of the first form is equal to sum of len... | The first line contains integer n (3 β€ n β€ 100) β amount of worm's forms. The second line contains n space-separated integers ai (1 β€ ai β€ 1000) β lengths of worms of each form. | Output 3 distinct integers i j k (1 β€ i, j, k β€ n) β such indexes of worm's forms that ai = aj + ak. If there is no such triple, output -1. If there are several solutions, output any of them. It possible that aj = ak. | Input: 51 2 3 5 7 | Output: 3 2 1 | Easy | 1 | 369 | 177 | 217 | 0 | |
1,804 | B | 1804B | B. Vaccination | 1,000 | greedy; implementation | Ethan runs a vaccination station to help people combat the seasonal flu. He analyses the historical data in order to develop an optimal strategy for vaccine usage.Consider there are \(n\) patients coming to the station on a particular day. The \(i\)-th patient comes at the moment \(t_i\). We know that each of these pat... | The first line of the input contains the number of test cases \(t\) (\(1 \leq t \leq 10^4\)). Then follow \(t\) descriptions of the test cases.The first line of each test case contains four integers \(n\), \(k\), \(d\) and \(w\) (\(1 \leq n, k \leq 2 \cdot 10^5\), \(0 \leq d, w \leq 10^6\)). They are the number of pati... | Output one integer, the minimum number of vaccine packs required to vaccinate all \(n\) patients. | In the first example, the first pack can be opened at moment \(1\) to vaccinate patient \(1\). The vaccine is durable enough to be used at moments \(2\) and \(3\) for patients \(2\) and \(3\), respectively. Then the staff needs to ask patients \(4\) and \(5\) to wait for moment \(13\). At moment \(13\) the staff opens ... | Input: 56 3 5 31 2 3 10 11 186 4 0 03 3 3 3 3 49 10 2 20 1 2 3 4 5 6 7 83 10 3 610 20 305 5 4 40 2 4 6 8 | Output: 2 3 2 3 1 | Beginner | 2 | 1,216 | 866 | 97 | 18 |
1,218 | D | 1218D | D. Xor Spanning Tree | 2,400 | divide and conquer; fft; graphs | In the galaxy far far away is the ancient interplanetary republic of Bubbleland, consisting of \(N\) planets. Between them, there are \(M\) bidirectional wormholes, each connecting a pair of planets. Bubbleland is a very centralized republic, having a capital planet Whiteplanet, from which any another planet can be rea... | First line of input contains two numbers \(N (1 \leq N \leq 100.000)\), the number of planets and \(M (1 \leq M \leq 100.041)\), the number of wormholes. Following \(M\) lines contain three numbers \(U, V (1 \leq U \neq V \leq N)\) and \(W (1 \leq W \leq 100.000)\), meaning that there exists a wormhole connecting plane... | Output two numbers, the smallest possible cost of entire reparation and the number of different valid reparations with that cost modulo \(1000000007\). | We can repair wormholes \(1\),\(2\),\(3\),\(5\) and \(6\), paying \(5 \oplus 1\oplus 2 \oplus 3 \oplus 4=1\), one can check that this is the cheapest repair in which all of the planets are connected and the only valid repair with that cost. | Input: 6 6 4 1 5 5 2 1 6 3 2 1 2 6 1 3 3 2 3 4 | Output: 1 1 | Expert | 3 | 1,570 | 366 | 151 | 12 |
1,656 | C | 1656C | C. Make Equal With Mod | 1,200 | constructive algorithms; math; number theory; sortings | You are given an array of \(n\) non-negative integers \(a_1, a_2, \ldots, a_n\). You can make the following operation: choose an integer \(x \geq 2\) and replace each number of the array by the remainder when dividing that number by \(x\), that is, for all \(1 \leq i \leq n\) set \(a_i\) to \(a_i \bmod x\).Determine if... | The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. Description of the test cases follows.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 10^5\)) β the length of the array.The second line of each test ... | For each test case, print a line with YES if you can make all elements of the list equal by applying the operation. Otherwise, print NO.You may print each letter in any case (for example, ""YES"", ""Yes"", ""yes"", ""yEs"" will all be recognized as a positive answer). | In the first test case, one can apply the operation with \(x = 3\) to obtain the array \([2, 2, 0, 2]\), and then apply the operation with \(x = 2\) to obtain \([0, 0, 0, 0]\).In the second test case, all numbers are already equal.In the fourth test case, applying the operation with \(x = 4\) results in the array \([1,... | Input: 442 5 6 831 1 154 1 7 0 845 9 17 5 | Output: YES YES NO YES | Easy | 4 | 425 | 517 | 268 | 16 |
1,529 | B | 1529B | B. Sifid and Strange Subsequences | 1,100 | greedy; math; sortings | A sequence \((b_1, b_2, \ldots, b_k)\) is called strange, if the absolute difference between any pair of its elements is greater than or equal to the maximum element in the sequence. Formally speaking, it's strange if for every pair \((i, j)\) with \(1 \le i<j \le k\), we have \(|a_i-a_j|\geq MAX\), where \(MAX\) is th... | The first line contains an 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\le n\le 10^5)\) β the length of the array \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots... | For each test case output a single integer β the length of the longest strange subsequence of \(a\). | In the first test case, one of the longest strange subsequences is \((a_1, a_2, a_3, a_4)\)In the second test case, one of the longest strange subsequences is \((a_1, a_3, a_4, a_5, a_7)\).In the third test case, one of the longest strange subsequences is \((a_1, a_3, a_4, a_5)\).In the fourth test case, one of the lon... | Input: 6 4 -1 -2 0 0 7 -3 4 -2 0 -4 6 1 5 0 5 -3 2 -5 3 2 3 1 4 -3 0 2 0 6 -3 -2 -1 1 1 1 | Output: 4 5 4 1 3 4 | Easy | 3 | 880 | 473 | 100 | 15 |
277 | C | 277C | C. Game | 2,400 | games; implementation | Two players play the following game. Initially, the players have a knife and a rectangular sheet of paper, divided into equal square grid cells of unit size. The players make moves in turn, the player who can't make a move loses. In one move, a player can take the knife and cut the paper along any segment of the grid l... | The first line contains three integers n, m, k (1 β€ n, m β€ 109, 0 β€ k β€ 105) β the sizes of the piece of paper and the number of cuts. Then follow k lines, each containing 4 integers xbi, ybi, xei, yei (0 β€ xbi, xei β€ n, 0 β€ ybi, yei β€ m) β the coordinates of the ends of the existing cuts. It is guaranteed that each cu... | If the second player wins, print ""SECOND"". Otherwise, in the first line print ""FIRST"", and in the second line print any winning move of the first player (the coordinates of the cut ends, follow input format to print them). | Input: 2 1 0 | Output: FIRST1 0 1 1 | Expert | 2 | 1,149 | 552 | 226 | 2 | |
1,250 | B | 1250B | B. The Feast and the Bus | 1,800 | brute force; constructive algorithms; greedy; math | Employees of JebTrains are on their way to celebrate the 256-th day of the year! There are \(n\) employees and \(k\) teams in JebTrains. Each employee is a member of some (exactly one) team. All teams are numbered from \(1\) to \(k\). You are given an array of numbers \(t_1, t_2, \dots, t_n\) where \(t_i\) is the \(i\)... | The first line contains two integers \(n\) and \(k\) (\(1 \le n \le 5\cdot10^5, 1 \le k \le 8000\)) β the number of employees and the number of teams in JebTrains. The second line contains a sequence of integers \(t_1, t_2, \dots, t_n\), where \(t_i\) (\(1 \le t_i \le k\)) is the \(i\)-th employee's team number. Every ... | Print the minimum cost of the rent. | Input: 6 3 3 1 2 3 2 3 | Output: 6 | Medium | 4 | 1,095 | 356 | 35 | 12 | |
1,385 | A | 1385A | A. Three Pairwise Maximums | 800 | math | You are given three positive (i.e. strictly greater than zero) integers \(x\), \(y\) and \(z\).Your task is to find positive integers \(a\), \(b\) and \(c\) such that \(x = \max(a, b)\), \(y = \max(a, c)\) and \(z = \max(b, c)\), or determine that it is impossible to find such \(a\), \(b\) and \(c\).You have to answer ... | The first line of the input contains one integer \(t\) (\(1 \le t \le 2 \cdot 10^4\)) β the number of test cases. Then \(t\) test cases follow.The only line of the test case contains three integers \(x\), \(y\), and \(z\) (\(1 \le x, y, z \le 10^9\)). | For each test case, print the answer: ""NO"" in the only line of the output if a solution doesn't exist; or ""YES"" in the first line and any valid triple of positive integers \(a\), \(b\) and \(c\) (\(1 \le a, b, c \le 10^9\)) in the second line. You can print \(a\), \(b\) and \(c\) in any order. | Input: 5 3 2 3 100 100 100 50 49 49 10 30 20 1 1000000000 1000000000 | Output: YES 3 2 1 YES 100 100 100 NO NO YES 1 1 1000000000 | Beginner | 1 | 413 | 251 | 298 | 13 | |
1,396 | B | 1396B | B. Stoned Game | 1,800 | brute force; constructive algorithms; games; greedy | T is playing a game with his friend, HL.There are \(n\) piles of stones, the \(i\)-th pile initially has \(a_i\) stones. T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chos... | The first line of the input contains a single integer \(t\) \((1 \le t \le 100)\) β the number of games. The description of the games follows. Each description contains two lines:The first line contains a single integer \(n\) \((1 \le n \le 100)\) β the number of piles.The second line contains \(n\) integers \(a_1, a_2... | For each game, print on a single line the name of the winner, ""T"" or ""HL"" (without quotes) | In the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains \(1\) stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner. | Input: 2 1 2 2 1 1 | Output: T HL | Medium | 4 | 674 | 359 | 94 | 13 |
1,734 | D | 1734D | D. Slime Escape | 1,800 | data structures; dp; greedy; two pointers | You are playing a game called Slime Escape. The game takes place on a number line. Initially, there are \(n\) slimes. For all positive integers \(i\) where \(1 \le i \le n\), the \(i\)-th slime is located at position \(i\) and has health \(a_i\). You are controlling the slime at position \(k\). There are two escapes lo... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 20\,000\)) β the number of test cases. Description of the test cases follows.The first line of each test case contains two positive integers \(n\), \(k\) (\(3 \leq n \leq 200\,000\), \(1 \leq k \leq n\)) β the number... | For each test case, print ""YES"" (without quotes) if you can escape without losing, and ""NO"" (without quotes) otherwise. | In the first test case, you control the slime at position \(4\) with health \(6\). One way to escape is to absorb the slimes at positions \(5\), \(6\), and \(7\). Your slime escapes with \(0\) health at position \(8\).In the second test case, you control the slime with \(232\) health at position \(1\). Since your slime... | Input: 67 4-1 -2 -3 6 -2 -3 -13 1232 -500 -7007 4-1 -2 -4 6 -2 -4 -18 4-100 10 -7 6 -2 -3 6 -108 2-999 0 -2 3 4 5 6 77 37 3 3 4 2 1 1 | Output: YES YES NO YES NO YES | Medium | 4 | 1,062 | 744 | 123 | 17 |
1,070 | C | 1070C | C. Cloud Computing | 2,000 | data structures; greedy | Buber is a Berland technology company that specializes in waste of investor's money. Recently Buber decided to transfer its infrastructure to a cloud. The company decided to rent CPU cores in the cloud for \(n\) consecutive days, which are numbered from \(1\) to \(n\). Buber requires \(k\) CPU cores each day.The cloud ... | The first line of the input contains three integers \(n\), \(k\) and \(m\) (\(1 \le n,k \le 10^6, 1 \le m \le 2\cdot10^5\)) β the number of days to analyze, the desired daily number of cores, the number of tariff plans.The following \(m\) lines contain descriptions of tariff plans, one description per line. Each line c... | Print a single integer number β the minimal amount of money that Buber will pay. | Input: 5 7 31 4 5 31 3 5 22 5 10 1 | Output: 44 | Hard | 2 | 1,309 | 627 | 80 | 10 | |
255 | D | 255D | D. Mr. Bender and Square | 1,800 | binary search; implementation; math | Mr. Bender has a digital table of size n Γ n, each cell can be switched on or off. He wants the field to have at least c switched on squares. When this condition is fulfilled, Mr Bender will be happy.We'll consider the table rows numbered from top to bottom from 1 to n, and the columns β numbered from left to right fro... | The first line contains four space-separated integers n, x, y, c (1 β€ n, c β€ 109; 1 β€ x, y β€ n; c β€ n2). | In a single line print a single integer β the answer to the problem. | Initially the first test has one painted cell, so the answer is 0. In the second test all events will go as is shown on the figure. . | Input: 6 4 3 1 | Output: 0 | Medium | 3 | 764 | 104 | 68 | 2 |
223 | B | 223B | B. Two Strings | 1,900 | data structures; dp; strings | A subsequence of length |x| of string s = s1s2... s|s| (where |s| is the length of string s) is a string x = sk1sk2... sk|x| (1 β€ k1 < k2 < ... < k|x| β€ |s|).You've got two strings β s and t. Let's consider all subsequences of string s, coinciding with string t. Is it true that each character of string s occurs in at l... | The first line contains string s, the second line contains string t. Each line consists only of lowercase English letters. The given strings are non-empty, the length of each string does not exceed 2Β·105. | Print ""Yes"" (without the quotes), if each character of the string s occurs in at least one of the described subsequences, or ""No"" (without the quotes) otherwise. | In the first sample string t can occur in the string s as a subsequence in three ways: abab, abab and abab. In these occurrences each character of string s occurs at least once.In the second sample the 4-th character of the string s doesn't occur in any occurrence of string t.In the third sample there is no occurrence ... | Input: ababab | Output: Yes | Hard | 3 | 515 | 204 | 165 | 2 |
149 | D | 149D | D. Coloring Brackets | 1,900 | dp | Once Petya read a problem about a bracket sequence. He gave it much thought but didn't find a solution. Today you will face it.You are given string s. It represents a correct bracket sequence. A correct bracket sequence is the sequence of opening (""("") and closing ("")"") brackets, such that it is possible to obtain ... | The first line contains the single string s (2 β€ |s| β€ 700) which represents a correct bracket sequence. | Print the only number β the number of ways to color the bracket sequence that meet the above given conditions modulo 1000000007 (109 + 7). | Let's consider the first sample test. The bracket sequence from the sample can be colored, for example, as is shown on two figures below. The two ways of coloring shown below are incorrect. | Input: (()) | Output: 12 | Hard | 1 | 1,602 | 104 | 138 | 1 |
1,419 | B | 1419B | B. Stairs | 1,200 | brute force; constructive algorithms; greedy; implementation; math | Jett is tired after destroying the town and she wants to have a rest. She likes high places, that's why for having a rest she wants to get high and she decided to craft staircases.A staircase is a squared figure that consists of square cells. Each staircase consists of an arbitrary number of stairs. If a staircase has ... | The first line contains a single integer \(t\) \((1 \le t \le 1000)\) β the number of test cases.The description of each test case contains a single integer \(x\) \((1 \le x \le 10^{18})\) β the number of cells for building staircases. | For each test case output a single integer β the number of different nice staircases, that can be built, using not more than \(x\) cells, in total. | In the first test case, it is possible to build only one staircase, that consists of \(1\) stair. It's nice. That's why the answer is \(1\).In the second test case, it is possible to build two different nice staircases: one consists of \(1\) stair, and another consists of \(3\) stairs. This will cost \(7\) cells. In th... | Input: 4 1 8 6 1000000000000000000 | Output: 1 2 1 30 | Easy | 5 | 939 | 235 | 147 | 14 |
446 | E | 446E | E. DZY Loves Bridges | 3,100 | math; matrices | DZY owns 2m islands near his home, numbered from 1 to 2m. He loves building bridges to connect the islands. Every bridge he builds takes one day's time to walk across.DZY has a strange rule of building the bridges. For every pair of islands u, v (u β v), he has built 2k different bridges connecting them, where (a|b mea... | To avoid huge input, we use the following way to generate the array a. You are given the first s elements of array: a1, a2, ..., as. All the other elements should be calculated by formula: ai = (101Β·ai - s + 10007) mod 1051131 (s < i β€ 2m).The first line contains three integers m, t, s (1 β€ m β€ 25; 1 β€ t β€ 1018; 1 β€ s ... | To avoid huge output, you only need to output xor-sum of all the answers for all i modulo 1051131 (1 β€ i β€ 2m), i.e. (ans[1] mod 1051131) xor (ans[2] mod 1051131) xor... xor (ans[n] mod 1051131). | In the first sample, ans = [6, 7, 6, 6].If he wants to be at island 1 after one day, he has 6 different ways: home β> 1 -(stay)-> 1 home β> 2 β> 1 home β> 3 β> 1 home β> 3 β> 1 (note that there are two different bridges between 1 and 3) home β> 4 β> 1 home β> 4 β> 1 (note that there are two different bridges from home ... | Input: 2 1 41 1 1 2 | Output: 1 | Master | 2 | 1,243 | 403 | 195 | 4 |
513 | B2 | 513B2 | B2. Permutations | 1,800 | bitmasks; divide and conquer; math | You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum:Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p). | The single line of input contains two integers n and m (1 β€ m β€ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p).The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subp... | Output n number forming the required permutation. | In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order. | Input: 2 2 | Output: 2 1 | Medium | 3 | 219 | 502 | 49 | 5 |
1,468 | E | 1468E | E. Four Segments | 800 | greedy | Monocarp wants to draw four line segments on a sheet of paper. He wants the \(i\)-th segment to have its length equal to \(a_i\) (\(1 \le i \le 4\)). These segments can intersect with each other, and each segment should be either horizontal or vertical.Monocarp wants to draw the segments in such a way that they enclose... | The first line contains one integer \(t\) (\(1 \le t \le 3 \cdot 10^4\)) β the number of test cases.Each test case consists of a single line containing four integers \(a_1\), \(a_2\), \(a_3\), \(a_4\) (\(1 \le a_i \le 10^4\)) β the lengths of the segments Monocarp wants to draw. | For each test case, print one integer β the maximum area of a rectangle Monocarp can enclose with four segments (it can be shown that the answer is always an integer). | The first test case of the example is described in the statement.For the second test case, Monocarp can draw the segments \(AB\), \(BC\), \(CD\) and \(DA\) as follows: Here, Monocarp has drawn segments \(AB\) (with length \(5\)), \(BC\) (with length \(5\)), \(CD\) (with length \(5\)) and \(DA\) (with length \(5\)). He ... | Input: 4 1 2 3 4 5 5 5 5 3 1 4 1 100 20 20 100 | Output: 3 25 3 2000 | Beginner | 1 | 855 | 279 | 167 | 14 |
14 | B | 14B | B. Young Photographer | 1,000 | implementation | Among other things, Bob is keen on photography. Especially he likes to take pictures of sportsmen. That was the reason why he placed himself in position x0 of a long straight racetrack and got ready to take pictures. But the problem was that not all the runners passed him. The total amount of sportsmen, training at tha... | The first line of the input file contains integers n and x0 (1 β€ n β€ 100; 0 β€ x0 β€ 1000). The following n lines contain pairs of integers ai, bi (0 β€ ai, bi β€ 1000; ai β bi). | Output the required minimum distance in the same units as the positions on the racetrack. If there is no such a position, output -1. | Input: 3 30 714 24 6 | Output: 1 | Beginner | 1 | 784 | 174 | 132 | 0 | |
1,271 | E | 1271E | E. Common Number | 2,100 | binary search; combinatorics; dp; math | At first, let's define function \(f(x)\) as follows: $$$\( \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} \)\(We can see that if we choose some value \)v\( and will apply function \)f\( to it, then apply \)f\( to... | The first line contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 10^{18}\)). | Print the only integer β the maximum value that is contained in at least \(k\) paths. | In the first example, the answer is \(5\), since \(5\) occurs in \(path(5)\), \(path(10)\) and \(path(11)\).In the second example, the answer is \(4\), since \(4\) occurs in \(path(4)\), \(path(5)\), \(path(8)\), \(path(9)\), \(path(10)\) and \(path(11)\).In the third example \(n = k\), so the answer is \(1\), since \(... | Input: 11 3 | Output: 5 | Hard | 4 | 903 | 85 | 85 | 12 |
213 | C | 213C | C. Relay Race | 2,000 | dp | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with... | The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j). | On a single line print a single number β the answer to the problem. | Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1). Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample: Furik's pa... | Input: 15 | Output: 5 | Hard | 1 | 1,112 | 220 | 67 | 2 |
1,949 | H | 1949H | H. Division Avoidance | 3,100 | greedy; math | A newly discovered organism can be represented as a set of cells on an infinite grid. There is a coordinate system on the grid such that each cell has two integer coordinates \(x\) and \(y\). A cell with coordinates \(x=a\) and \(y=b\) will be denoted as \((a, b)\).Initially, the organism consists of a single cell \((0... | Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \le t \le 10\,000\)) β the number of test cases. The descriptions of the \(t\) test cases follow.The first line of each test case contains an integer \(n\) (\(1 \le n \le 10^6\)) β the number of forbidden cells.The next \(n\) lines co... | For each test case, print \(\texttt{YES}\) if it is possible for the organism to contain no forbidden cells after zero or more divisions. Otherwise, print \(\texttt{NO}\). | In the first test case, dividing the following cells in the following order creates an organism without any forbidden cells: \((0, 0)\), \((1, 0)\), \((1, 1)\), \((0, 1)\), \((2, 1)\), \((2, 2)\), \((1, 2)\), \((1, 1)\). The following picture demonstrates how the organism changes during this process:In the second test ... | Input: 240 01 00 11 1160 00 10 20 31 01 11 21 32 02 12 22 33 03 13 23 3 | Output: YES NO | Master | 2 | 1,256 | 630 | 171 | 19 |
1,514 | A | 1514A | A. Perfectly Imperfect Array | 800 | math; number theory | Given an array \(a\) of length \(n\), tell us whether it has a non-empty subsequence such that the product of its elements is not a perfect square.A sequence \(b\) is a subsequence of an array \(a\) if \(b\) can be obtained from \(a\) by deleting some (possibly zero) elements. | The first line contains an integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains an integer \(n\) (\(1 \le n \le 100\)) β the length of the array \(a\).The second line of each test case contains \(n\) integers \(a_1\), \(a_2\)... | If there's a subsequence of \(a\) whose product isn't a perfect square, print ""YES"". Otherwise, print ""NO"". | In the first example, the product of the whole array (\(20\)) isn't a perfect square.In the second example, all subsequences have a perfect square product. | Input: 2 3 1 5 4 2 100 10000 | Output: YES NO | Beginner | 2 | 277 | 403 | 111 | 15 |
908 | E | 908E | E. New Year and Entity Enumeration | 2,500 | bitmasks; combinatorics; dp; math | You are given an integer m.Let M = 2m - 1.You are also given a set of n integers denoted as the set T. The integers will be provided in base 2 as n binary strings of length m.A set of integers S is called ""good"" if the following hold. If , then . If , then All elements of S are less than or equal to M. Here, and refe... | The first line will contain two integers m and n (1 β€ m β€ 1 000, 1 β€ n β€ min(2m, 50)).The next n lines will contain the elements of T. Each line will contain exactly m zeros and ones. Elements of T will be distinct. | Print a single integer, the number of good sets modulo 109 + 7. | An example of a valid set S is {00000, 00101, 00010, 00111, 11000, 11010, 11101, 11111}. | Input: 5 3110100010111000 | Output: 4 | Expert | 4 | 429 | 215 | 63 | 9 |
1,442 | B | 1442B | B. Identify the Operations | 1,800 | combinatorics; data structures; dsu; greedy; implementation | We start with a permutation \(a_1, a_2, \ldots, a_n\) and with an empty array \(b\). We apply the following operation \(k\) times.On the \(i\)-th iteration, we select an index \(t_i\) (\(1 \le t_i \le n-i+1\)), remove \(a_{t_i}\) from the array, and append one of the numbers \(a_{t_i-1}\) or \(a_{t_i+1}\) (if \(t_i-1\)... | Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \le t \le 100\,000\)), denoting the number of test cases, followed by a description of the test cases.The first line of each test case contains two integers \(n, k\) (\(1 \le k < n \le 200\,000\)): sizes of arrays \(a\) and \(b\).The ... | For each test case print one integer: the number of possible sequences modulo \(998\,244\,353\). | \(\require{cancel}\)Let's denote as \(a_1 a_2 \ldots \cancel{a_i} \underline{a_{i+1}} \ldots a_n \rightarrow a_1 a_2 \ldots a_{i-1} a_{i+1} \ldots a_{n-1}\) an operation over an element with index \(i\): removal of element \(a_i\) from array \(a\) and appending element \(a_{i+1}\) to array \(b\).In the first example te... | Input: 3 5 3 1 2 3 4 5 3 2 5 4 3 4 3 2 1 4 3 1 7 4 1 4 7 3 6 2 5 3 2 4 5 | Output: 2 0 4 | Medium | 5 | 762 | 720 | 96 | 14 |
1,276 | A | 1276A | A. As Simple as One and Two | 1,400 | dp; greedy | You are given a non-empty string \(s=s_1s_2\dots s_n\), which consists only of lowercase Latin letters. Polycarp does not like a string if it contains at least one string ""one"" or at least one string ""two"" (or both at the same time) as a substring. In other words, Polycarp does not like the string \(s\) if there is... | The first line of the input contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases in the input. Next, the test cases are given.Each test case consists of one non-empty string \(s\). Its length does not exceed \(1.5\cdot10^5\). The string \(s\) consists only of lowercase Latin letters.It is guarant... | Print an answer for each test case in the input in order of their appearance.The first line of each answer should contain \(r\) (\(0 \le r \le |s|\)) β the required minimum number of positions to be removed, where \(|s|\) is the length of the given line. The second line of each answer should contain \(r\) different int... | In the first example, answers are: ""onetwone"", ""testme"" β Polycarp likes it, there is nothing to remove, ""oneoneone"", ""twotwo"". In the second example, answers are: ""onetwonetwooneooonetwooo"", ""two"", ""one"", ""twooooo"", ""ttttwo"", ""ttwwoo"" β Polycarp likes it, there is nothing to remove, ""ooone"", ""on... | Input: 4 onetwone testme oneoneone twotwo | Output: 2 6 3 0 3 4 1 7 2 1 4 | Easy | 2 | 1,150 | 425 | 579 | 12 |
1,282 | D | 1282D | D. Enchanted Artifact | 2,300 | constructive algorithms; interactive; strings | This is an interactive problem.After completing the last level of the enchanted temple, you received a powerful artifact of the 255th level. Do not rush to celebrate, because this artifact has a powerful rune that can be destroyed with a single spell \(s\), which you are going to find. We define the spell as some non-e... | Input: 2 2 1 2 3 0 | Output: aaa aaab abba bba abaaa aabba | Expert | 3 | 1,541 | 0 | 0 | 12 | |||
89 | E | 89E | E. Fire and Ice | 2,900 | greedy | The Fire Lord attacked the Frost Kingdom. He has already got to the Ice Fortress, where the Snow Queen dwells. He arranged his army on a segment n in length not far from the city walls. And only the frost magician Solomon can save the Frost Kingdom. The n-long segment is located at a distance equal exactly to 1 from th... | The first line contains an integer n (1 β€ n β€ 1000). The next line contains n numbers, the i-th of them represents the strength of the fire demon standing of the i-th position, an integer from 1 to 100. If there's no demon on the i-th position, then the i-th number equals to 0. It is guaranteed that the input data have... | Print a string of minimum length, containing characters ""L"", ""R"" and ""A"" β the succession of actions leading to the required result.If there are several possible answers, print any of them. | Input: 31 0 1 | Output: ARARARALLLA | Master | 1 | 1,794 | 345 | 195 | 0 | |
1,976 | F | 1976F | F. Remove Bridges | 2,800 | data structures; dfs and similar; dp; greedy; sortings; trees | You are given a rooted tree, consisting of \(n\) vertices, numbered from \(1\) to \(n\). Vertex \(1\) is the root. Additionally, the root only has one child.You are asked to add exactly \(k\) edges to the tree (possibly, multiple edges and/or edges already existing in the tree).Recall that a bridge is such an edge that... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of testcases.The first line of each testcase contains a single integer \(n\) (\(2 \le n \le 3 \cdot 10^5\)) β the number of vertices of the tree.Each of the next \(n - 1\) lines contain two integers \(v\) and \(u\) (\(1 \le v, u \le n\))... | For each testcase, print \(n - 1\) integers. For each \(k\) from \(1\) to \(n - 1\) print the smallest number of bridges that can be left after you add \(k\) edges to the tree. | Input: 421 2124 105 1212 113 69 61 612 711 62 1110 910 881 22 32 43 53 64 74 851 22 33 44 5 | Output: 0 7 3 1 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0 | Master | 6 | 851 | 570 | 176 | 19 | |
1,450 | G | 1450G | G. Communism | 3,500 | bitmasks; dp; trees | Please pay attention to the unusual memory limit in this problem.In a parallel universe, Satan is called ""Trygub"". For that reason, the letters of his namesake were deleted from the alphabet in ancient times.The government has \(n\) workers standing in a row and numbered with integers from \(1\) to \(n\) from left to... | The first line contains three integers \(n, a, b\) (\(1 \le n \le 5000\), \(1\le a\le b\le 10^5\)) β the number of workers and the numerator and denominator of the parameter \(k\), respectively.The second line contains a string \(s\) of length \(n\), consisting of lowercase English characters β the job categories of ea... | Print an integer \(c\) equal to the number of obtainable job categories followed by \(c\) space-separated characters β the obtainable job categories sorted in the lexicographical order. | The first operation must select the job category 'i' because all other job categories cannot satisfy the condition, therefore 'i' is not obtainable.Below is showed how to obtain 'c', 'm', and 'o'. The square brackets denote the segment containing all workers of the selected category, the red color denotes this category... | Input: 7 1 2 comicom | Output: 3 c m o | Master | 3 | 1,469 | 413 | 185 | 14 |
1,055 | A | 1055A | A. Metro | 900 | graphs | Alice has a birthday today, so she invited home her best friend Bob. Now Bob needs to find a way to commute to the Alice's home.In the city in which Alice and Bob live, the first metro line is being built. This metro line contains \(n\) stations numbered from \(1\) to \(n\). Bob lives near the station with number \(1\)... | The first line contains two integers \(n\) and \(s\) (\(2 \le s \le n \le 1000\)) β the number of stations in the metro and the number of the station where Alice's home is located. Bob lives at station \(1\).Next lines describe information about closed and open stations.The second line contains \(n\) integers \(a_1, a_... | Print ""YES"" (quotes for clarity) if Bob will be able to commute to the Alice's home by metro and ""NO"" (quotes for clarity) otherwise.You can print each letter in any case (upper or lower). | In the first example, all stations are opened, so Bob can simply travel to the station with number \(3\).In the second example, Bob should travel to the station \(5\) first, switch to the second track and travel to the station \(4\) then.In the third example, Bob simply can't enter the train going in the direction of A... | Input: 5 31 1 1 1 11 1 1 1 1 | Output: YES | Beginner | 1 | 1,212 | 822 | 192 | 10 |
603 | A | 603A | A. Alternative Thinking | 1,600 | dp; greedy; math | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiadβ'1' for a correctly identified cow and '0' otherwise.However, all is not l... | The first line contains the number of questions on the olympiad n (1 β€ n β€ 100 000).The following line contains a binary string of length n representing Kevin's results on the USAICO. | Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. | In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.In the second sample, Kevin can flip the entire string and still have the same score. | Input: 810000011 | Output: 5 | Medium | 3 | 1,234 | 183 | 154 | 6 |
290 | B | 290B | B. QR code | 1,500 | *special; implementation | The input contains two integers a1, a2 (0 β€ ai β€ 32), separated by a single space. | Output a single integer. | Input: 1 1 | Output: 0 | Medium | 2 | 0 | 82 | 24 | 2 | ||
2,081 | G1 | 2081G1 | G1. Hard Formula | 3,100 | math; number theory | This is the easy version of the problem. The difference between the versions is that in this version, the limits on \(n\) and the time limit are smaller. You can hack only if you solved all versions of this problem. You are given an integer \(n\), and you need to compute \((\sum_{k=1}^n k\bmod\varphi(k))\bmod 2^{32}\),... | The only line contains a single integer \(n\) (\(1 \le n \le 10^{10})\). | Print a single integer, representing \((\sum_{k=1}^n k\bmod\varphi(k))\bmod 2^{32}\). | Input: 5 | Output: 2 | Master | 2 | 431 | 72 | 85 | 20 | |
1,906 | J | 1906J | J. Count BFS Graph | 2,100 | combinatorics; dp | You are currently researching a graph traversal algorithm called the Breadth First Search (BFS). Suppose you have an input graph of \(N\) nodes (numbered from \(1\) to \(N\)). The graph is represented by an adjacency matrix \(M\), for which node \(u\) can traverse to node \(v\) if \(M_{u, v}\) is \(1\), otherwise it is... | The first line consists of an integer \(N\) (\(2 \leq N \leq 5000\)).The second line consists of \(N\) integers \(A_i\). The array \(A\) is a permutation of \(1\) to \(N\) and \(A_1 = 1\). | Output an integer representing the number of simple undirected graphs with \(N\) nodes and adjacency matrix \(M\) such that \(\text{BFS}(M) = A\). Since the answer can be very large, output the answer modulo \(998\,244\,353\). | Explanation for the sample input/output #1The following illustration shows all graphs that satisfy the requirements. Explanation for the sample input/output #2The only graph that satisfies the requirements is a graph with two edges: one that connects nodes \(1\) and \(3\), and another one that connects nodes \(3\) and ... | Input: 3 1 2 3 | Output: 3 | Hard | 2 | 1,544 | 188 | 226 | 19 |
452 | C | 452C | C. Magic Trick | 2,100 | combinatorics; math; probabilities | Alex enjoys performing magic tricks. He has a trick that requires a deck of n cards. He has m identical decks of n different cards each, which have been mixed together. When Alex wishes to perform the trick, he grabs n cards at random and performs the trick with those. The resulting deck looks like a normal deck, but m... | First line of the input consists of two integers n and m (1 β€ n, m β€ 1000), separated by space β number of cards in each deck, and number of decks. | On the only line of the output print one floating point number β probability of Alex successfully performing the trick. Relative or absolute error of your answer should not be higher than 10 - 6. | In the first sample, with probability Alex will perform the trick with two cards with the same value from two different decks. In this case the trick is guaranteed to succeed.With the remaining probability he took two different cards, and the probability of pulling off the trick is .The resulting probability is | Input: 2 2 | Output: 0.6666666666666666 | Hard | 3 | 801 | 147 | 195 | 4 |
175 | A | 175A | A. Robot Bicorn Attack | 1,400 | brute force; implementation | Vasya plays Robot Bicorn Attack.The game consists of three rounds. For each one a non-negative integer amount of points is given. The result of the game is the sum of obtained points. Vasya has already played three rounds and wrote obtained points one by one (without leading zeros) into the string s. Vasya decided to b... | The only line of input contains non-empty string s obtained by Vasya. The string consists of digits only. The string length does not exceed 30 characters. | Print the only number β the maximum amount of points Vasya could get. If Vasya is wrong and the string could not be obtained according to the rules then output number -1. | In the first example the string must be split into numbers 1, 2 and 34.In the second example the string must be split into numbers 90, 0 and 0. In the third example the string is incorrect, because after splitting the string into 3 numbers number 00 or 09 will be obtained, but numbers cannot have leading zeroes. | Input: 1234 | Output: 37 | Easy | 2 | 686 | 154 | 170 | 1 |
903 | D | 903D | D. Almost Difference | 2,200 | data structures; math | Let's denote a function You are given an array a consisting of n integers. You have to calculate the sum of d(ai, aj) over all pairs (i, j) such that 1 β€ i β€ j β€ n. | The first line contains one integer n (1 β€ n β€ 200000) β the number of elements in a.The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109) β elements of the array. | Print one integer β the sum of d(ai, aj) over all pairs (i, j) such that 1 β€ i β€ j β€ n. | In the first example: d(a1, a2) = 0; d(a1, a3) = 2; d(a1, a4) = 0; d(a1, a5) = 2; d(a2, a3) = 0; d(a2, a4) = 0; d(a2, a5) = 0; d(a3, a4) = - 2; d(a3, a5) = 0; d(a4, a5) = 2. | Input: 51 2 3 1 3 | Output: 4 | Hard | 2 | 164 | 176 | 87 | 9 |
660 | B | 660B | B. Seating On Bus | 1,000 | implementation | Consider 2n rows of the seats in a bus. n rows of the seats on the left and n rows of the seats on the right. Each row can be filled by two people. So the total capacity of the bus is 4n.Consider that m (m β€ 4n) people occupy the seats in the bus. The passengers entering the bus are numbered from 1 to m (in the order o... | The only line contains two integers, n and m (1 β€ n β€ 100, 1 β€ m β€ 4n) β the number of pairs of rows and the number of passengers. | Print m distinct integers from 1 to m β the order in which the passengers will get off the bus. | Input: 2 7 | Output: 5 1 6 2 7 3 4 | Beginner | 1 | 1,295 | 130 | 95 | 6 | |
30 | A | 30A | A. Accounting | 1,400 | brute force; math | A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself.The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative β it means that there was a los... | The input contains three integers A, B, n (|A|, |B| β€ 1000, 1 β€ n β€ 10). | Output the required integer coefficient X, or Β«No solutionΒ», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. | Input: 2 18 2 | Output: 3 | Easy | 2 | 842 | 72 | 179 | 0 | |
1,519 | D | 1519D | D. Maximum Sum of Products | 1,600 | brute force; dp; implementation; math; two pointers | You are given two integer arrays \(a\) and \(b\) of length \(n\).You can reverse at most one subarray (continuous subsegment) of the array \(a\). Your task is to reverse such a subarray that the sum \(\sum\limits_{i=1}^n a_i \cdot b_i\) is maximized. | The first line contains one integer \(n\) (\(1 \le n \le 5000\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^7\)).The third line contains \(n\) integers \(b_1, b_2, \dots, b_n\) (\(1 \le b_i \le 10^7\)). | Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of \(a\). | In the first example, you can reverse the subarray \([4, 5]\). Then \(a = [2, 3, 2, 3, 1]\) and \(2 \cdot 1 + 3 \cdot 3 + 2 \cdot 2 + 3 \cdot 4 + 1 \cdot 2 = 29\).In the second example, you don't need to use the reverse operation. \(13 \cdot 2 + 37 \cdot 4 = 174\).In the third example, you can reverse the subarray \([3... | Input: 5 2 3 2 1 3 1 3 2 4 2 | Output: 29 | Medium | 5 | 250 | 244 | 114 | 15 |
696 | E | 696E | E. ...Wait for it... | 3,000 | data structures; dsu; trees | Barney is searching for his dream girl. He lives in NYC. NYC has n junctions numbered from 1 to n and n - 1 roads connecting them. We will consider the NYC as a rooted tree with root being junction 1. m girls live in NYC, i-th of them lives along junction ci and her weight initially equals i pounds. Barney consider a g... | The first line of input contains three integers n, m and q (1 β€ n, m, q β€ 105) β the number of junctions in NYC, the number of girls living in NYC and the number of events respectively.The next n - 1 lines describes the roads. Each line contains two integers v and u (1 β€ v, u β€ n, v β u) meaning that there is a road co... | For each event of the first type, print number t and then t integers g1, g2, ..., gt in one line, meaning that in this event Barney will invite t girls whose indices are g1, ..., gt in the order from the best to the worst according to Barney's considerations. | For the first sample case: Description of events: Weights of girls in subtree of junction 4 increase by 3. These girls have IDs: 1, 3, 5, 4, 7. Barney goes from junction 2 to 1. Girls on his way have IDs 1, 2, 3, 5, 6, 7 with weights 4, 2, 6, 8, 6, 10 respectively. So, he invites girls 2 and 1. Barney goes from junctio... | Input: 5 7 113 52 34 31 44 1 4 5 4 1 42 4 31 2 1 21 4 2 12 2 102 1 101 2 4 11 2 3 42 5 22 4 91 3 5 21 1 2 3 | Output: 2 2 1 1 3 1 5 0 1 4 2 6 7 | Master | 3 | 1,257 | 965 | 259 | 6 |
1,263 | C | 1263C | C. Everyone is a Winner! | 1,400 | binary search; math; meet-in-the-middle; number theory | On the well-known testing system MathForces, a draw of \(n\) rating units is arranged. The rating will be distributed according to the following algorithm: if \(k\) participants take part in this event, then the \(n\) rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the ... | The first line contains integer number \(t\) (\(1 \le t \le 10\)) β the number of test cases in the input. Then \(t\) test cases follow.Each line contains an integer \(n\) (\(1 \le n \le 10^9\)) β the total number of the rating units being drawn. | Output the answers for each of \(t\) test cases. Each answer should be contained in two lines.In the first line print a single integer \(m\) β the number of different rating increment values that Vasya can get.In the following line print \(m\) integers in ascending order β the values of possible rating increments. | Input: 4 5 11 1 3 | Output: 4 0 1 2 5 6 0 1 2 3 5 11 2 0 1 3 0 1 3 | Easy | 4 | 1,369 | 246 | 315 | 12 | |
1,356 | A1 | 1356A1 | A1. Distinguish I from X | 0 | *special | You are given an operation that implements a single-qubit unitary transformation: either the identity (I gate) or the X gate. The operation will have Adjoint and Controlled variants defined.Your task is to perform necessary operations and measurements to figure out which unitary it was and to return 0 if it was the I g... | Beginner | 1 | 737 | 0 | 0 | 13 | ||||
501 | C | 501C | C. Misha and Forest | 1,500 | constructive algorithms; data structures; greedy; sortings; trees | Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of n vertices. For each vertex v from 0 to n - 1 he wrote down two integers, degreev and sv, were the first integer is the number of vertices adjacent to vertex v, and the secon... | The first line contains integer n (1 β€ n β€ 216), the number of vertices in the graph.The i-th of the next lines contains numbers degreei and si (0 β€ degreei β€ n - 1, 0 β€ si < 216), separated by a space. | In the first line print number m, the number of edges of the graph.Next print m lines, each containing two distinct numbers, a and b (0 β€ a β€ n - 1, 0 β€ b β€ n - 1), corresponding to edge (a, b).Edges can be printed in any order; vertices of the edge can also be printed in any order. | The XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C++, Java and Python it is represented as ""^"", and in Pascal β as ""xor"". | Input: 32 31 01 0 | Output: 21 02 0 | Medium | 5 | 712 | 202 | 283 | 5 |
2,038 | J | 2038J | J. Waiting for... | 800 | greedy; implementation | Monocarp is waiting for a bus at the bus stop. Unfortunately, there are many people who want to ride a bus too.You are given a list of events of two types: B \(b_i\) β a bus with \(b_i\) free seats arrives at the stop; P \(p_i\) β \(p_i\) people arrive at the stop. These events are listed in a chronological order. When... | The first line contains one integer \(n\) \((1 \le n \le 10^3)\) β the number of events.Then, \(n\) lines follow. The \(i\)-th of them contains the description of the \(i\)-th event in one of the two following formats: B \(b_i\) (\(1 \le b_i \le 10^6\)) β a bus with \(b_i\) free seats arrives at the stop; P \(p_i\) (\(... | For each event of type B, print YES if it is possible for Monocarp to take the corresponding bus, or NO otherwise (case-insensitive). | Input: 10P 2P 5B 8P 14B 5B 9B 3P 2B 1B 2 | Output: YES NO NO YES NO YES | Beginner | 2 | 908 | 453 | 133 | 20 | |
550 | C | 550C | C. Divisibility by Eight | 1,500 | brute force; dp; math | You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-ne... | The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits. | Print ""NO"" (without quotes), if there is no such way to remove some digits from number n. Otherwise, print ""YES"" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.If there are multiple possible answers, you may print any of t... | Input: 3454 | Output: YES344 | Medium | 3 | 489 | 171 | 324 | 5 | |
1,096 | G | 1096G | G. Lucky Tickets | 2,400 | divide and conquer; dp; fft | All bus tickets in Berland have their numbers. A number consists of \(n\) digits (\(n\) is even). Only \(k\) decimal digits \(d_1, d_2, \dots, d_k\) can be used to form ticket numbers. If \(0\) is among these digits, then numbers may have leading zeroes. For example, if \(n = 4\) and only digits \(0\) and \(4\) can be ... | The first line contains two integers \(n\) and \(k\) \((2 \le n \le 2 \cdot 10^5, 1 \le k \le 10)\) β the number of digits in each ticket number, and the number of different decimal digits that may be used. \(n\) is even.The second line contains a sequence of pairwise distinct integers \(d_1, d_2, \dots, d_k\) \((0 \le... | Print the number of lucky ticket numbers, taken modulo \(998244353\). | In the first example there are \(6\) lucky ticket numbers: \(1111\), \(1818\), \(1881\), \(8118\), \(8181\) and \(8888\).There is only one ticket number in the second example, it consists of \(20\) digits \(6\). This ticket number is lucky, so the answer is \(1\). | Input: 4 21 8 | Output: 6 | Expert | 3 | 651 | 423 | 69 | 10 |
335 | B | 335B | B. Palindrome | 1,900 | constructive algorithms; dp | Given a string s, determine if it contains any palindrome of length exactly 100 as a subsequence. If it has any, print any one of them. If it doesn't have any, print a palindrome that is a subsequence of s and is as long as possible. | The only line of the input contains one string s of length n (1 β€ n β€ 5Β·104) containing only lowercase English letters. | If s contains a palindrome of length exactly 100 as a subsequence, print any palindrome of length 100 which is a subsequence of s. If s doesn't contain any palindromes of length exactly 100, print a palindrome that is a subsequence of s and is as long as possible.If there exists multiple answers, you are allowed to pri... | A subsequence of a string is a string that can be derived from it by deleting some characters without changing the order of the remaining characters. A palindrome is a string that reads the same forward or backward. | Input: bbbabcbbb | Output: bbbcbbb | Hard | 2 | 233 | 119 | 335 | 3 |
126 | E | 126E | E. Pills | 2,900 | brute force; flows | Doctor prescribed medicine to his patient. The medicine is represented by pills. Each pill consists of a shell and healing powder. The shell consists of two halves; each half has one of four colors β blue, red, white or yellow.The doctor wants to put 28 pills in a rectangular box 7 Γ 8 in size. Besides, each pill occup... | First 7 lines contain the doctor's picture. Each line contains 8 characters, each character can be ""B"", ""R"", ""W"" and ""Y"" that stands for blue, red, white and yellow colors correspondingly.Next four lines contain 10 numbers that stand for, correspondingly, the number of pills painted:""BY"" ""BW"" ""BR"" ""BB"""... | Print on the first line the maximal number cells for which the colors match.Then print 13 lines each containing 15 characters β the pills' position in the optimal arrangement. The intersections of odd lines and odd columns should contain characters ""B"", ""R"", ""W"" and ""Y"". All other positions should contain chara... | Input: WWWBBWWWWWWBBWWWYYWBBWWWYYWBBWRRYYWBBWRRYYWBBWRRYYWBBWRR0 0 0 80 1 51 105 | Output: 53W.W.W.B.B.W.W.W|.|.|.|.|.|.|.|W.W.W.B.B.W.W.W...............Y.Y.W.B.B.W.W-W|.|.|.|.|.|....Y.Y.W.B.B.W.R.R............|.|Y.Y.W.B.B.R.R.R|.|.|.|.|.|....Y.Y.W.B.B.W.R.R............|.|Y-Y.B-B.B-B.R.R | Master | 2 | 1,141 | 482 | 509 | 1 | |
1,737 | A | 1737A | A. Ela Sorting Books | 900 | greedy; implementation; strings | Ela loves reading a lot, just like her new co-workers in DTL! On her first day after becoming an engineer in DTL, she is challenged by a co-worker to sort a heap of books into different compartments on the shelf.\(n\) books must be split into \(k\) compartments on the bookshelf (\(n\) is divisible by \(k\)). Each book ... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). Description of the test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 200\); \(1 \le k \le n\)). It is guaranteed that \(n\) is divisible by \(k\).T... | For each test case, output a string of \(k\) letters which is the most optimal string that Ela can find. | In the first test case, the books can be divided into \(3\) compartments as below: the first compartment contains the books with indices \(1, 2, 3, 7\): \(multiset_1 = \{\)'c', 'a', 'b', 'd'\(\}\) \(\rightarrow\) \(MEX(multiset_1) =\) 'e' the second compartment contains the books with indices \(4, 5, 6, 9\) : \(multise... | Input: 512 3cabccadabaac12 6cabccadabaac12 12cabccadabaac25 1abcdefghijklmnopqrstuvwxy10 5bcdxedbcfg | Output: edb ccbbba bbbbbaaaaaaa z aaaaa | Beginner | 3 | 2,030 | 603 | 104 | 17 |
883 | H | 883H | H. Palindromic Cut | 1,800 | brute force; implementation; strings | Kolya has a string s of length n consisting of lowercase and uppercase Latin letters and digits.He wants to rearrange the symbols in s and cut it into the minimum number of parts so that each part is a palindrome and all parts have the same lengths. A palindrome is a string which reads the same backward as forward, suc... | The first line contains an integer n (1 β€ n β€ 4Β·105) β the length of string s.The second line contains a string s of length n consisting of lowercase and uppercase Latin letters and digits. | Print to the first line an integer k β minimum number of palindromes into which you can cut a given string.Print to the second line k strings β the palindromes themselves. Separate them by a space. You are allowed to print palindromes in arbitrary order. All of them should have the same length. | Input: 6aabaac | Output: 2aba aca | Medium | 3 | 508 | 189 | 295 | 8 | |
792 | F | 792F | F. Mages and Monsters | 3,100 | data structures; geometry | Vova plays a computer game known as Mages and Monsters. Vova's character is a mage. Though as he has just started, his character knows no spells.Vova's character can learn new spells during the game. Every spell is characterized by two values xi and yi β damage per second and mana cost per second, respectively. Vova do... | The first line contains two integer numbers q and m (2 β€ q β€ 105, 1 β€ m β€ 1012) β the number of queries and the amount of mana at the beginning of every fight.i-th of each next q lines contains three numbers ki, ai and bi (1 β€ ki β€ 2, 1 β€ ai, bi β€ 106). Using them you can restore queries this way: let j be the index of... | For every query of second type print YES if Vova is able to win the fight with corresponding monster and NO otherwise. | In first example Vova's character at first learns the spell with 5 damage and 10 mana cost per second. Next query is a fight with monster which can kill character in 20 seconds and has 50 health points. Vova kills it in 10 seconds (spending 100 mana). Next monster has 52 health, so Vova can't deal that much damage with... | Input: 3 1001 4 92 19 492 19 49 | Output: YESNO | Master | 2 | 1,950 | 648 | 118 | 7 |
2,014 | A | 2014A | A. Robin Helps | 800 | greedy; implementation | There is a little bit of the outlaw in everyone, and a little bit of the hero too.The heroic outlaw Robin Hood is famous for taking from the rich and giving to the poor.Robin encounters \(n\) people starting from the \(1\)-st and ending with the \(n\)-th. The \(i\)-th person has \(a_i\) gold. If \(a_i \ge k\), Robin wi... | The first line of the input 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\), \(k\) (\(1 \le n \le 50, 1 \le k \le 100\)) β the number of people and the threshold at which Robin Hood takes the gold.The second line of each tes... | For each test case, output a single integer, the number of people that will get gold from Robin Hood. | In the first test case, Robin takes \(2\) gold from the first person and gives a gold to the second person.In the second test case, Robin takes \(3\) gold and gives \(1\) gold to each of the next \(2\) people.In the third test case, Robin takes \(3\) gold and so only gives gold to \(3\) other people. | Input: 42 22 03 23 0 06 20 3 0 0 0 02 55 4 | Output: 1 2 3 0 | Beginner | 2 | 480 | 427 | 101 | 20 |
234 | C | 234C | C. Weather | 1,300 | dp; implementation | Scientists say a lot about the problems of global warming and cooling of the Earth. Indeed, such natural phenomena strongly influence all life on our planet.Our hero Vasya is quite concerned about the problems. He decided to try a little experiment and observe how outside daily temperature changes. He hung out a thermo... | The first line contains a single integer n (2 β€ n β€ 105) β the number of days for which Vasya has been measuring the temperature. The second line contains a sequence of n integers t1, t2, ..., tn (|ti| β€ 109) β the sequence of temperature values. Numbers ti are separated by single spaces. | Print a single integer β the answer to the given task. | Note to the first sample: there are two ways to change exactly one number so that the sequence met Vasya's condition. You can either replace the first number 1 by any negative number or replace the number -2 by any positive number. | Input: 4-1 1 -2 1 | Output: 1 | Easy | 2 | 1,362 | 289 | 54 | 2 |
1,324 | D | 1324D | D. Pair of Topics | 1,400 | binary search; data structures; sortings; two pointers | The next lecture in a high school requires two topics to be discussed. The \(i\)-th topic is interesting by \(a_i\) units for the teacher and by \(b_i\) units for the students.The pair of topics \(i\) and \(j\) (\(i < j\)) is called good if \(a_i + a_j > b_i + b_j\) (i.e. it is more interesting for the teacher).Your ta... | The first line of the input contains one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the number of topics.The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)), where \(a_i\) is the interestingness of the \(i\)-th topic for the teacher.The third line of the input cont... | Print one integer β the number of good pairs of topic. | Input: 5 4 8 2 6 2 4 5 4 1 3 | Output: 7 | Easy | 4 | 369 | 467 | 54 | 13 | |
1,249 | D2 | 1249D2 | D2. Too Many Segments (hard version) | 1,800 | data structures; greedy; sortings | The only difference between easy and hard versions is constraints.You are given \(n\) segments on the coordinate axis \(OX\). Segments can intersect, lie inside each other and even coincide. The \(i\)-th segment is \([l_i; r_i]\) (\(l_i \le r_i\)) and it covers all integer points \(j\) such that \(l_i \le j \le r_i\).T... | The first line of the input contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 2 \cdot 10^5\)) β the number of segments and the maximum number of segments by which each integer point can be covered.The next \(n\) lines contain segments. The \(i\)-th line contains two integers \(l_i\) and \(r_i\) (\(1 \le l_i \l... | In the first line print one integer \(m\) (\(0 \le m \le n\)) β the minimum number of segments you need to remove so that there are no bad points.In the second line print \(m\) distinct integers \(p_1, p_2, \dots, p_m\) (\(1 \le p_i \le n\)) β indices of segments you remove in any order. If there are multiple answers, ... | Input: 7 2 11 11 9 11 7 8 8 9 7 8 9 11 7 9 | Output: 3 4 6 7 | Medium | 3 | 498 | 386 | 346 | 12 | |
316 | D2 | 316D2 | D2. PE Lesson | 2,300 | dp | Smart Beaver decided to be not only smart, but also a healthy beaver! And so he began to attend physical education classes at school X. In this school, physical education has a very creative teacher. One of his favorite warm-up exercises is throwing balls. Students line up. Each one gets a single ball in the beginning.... | The first line contains a single number n β the number of students in the line and the number of balls. The next line contains exactly n space-separated integers. Each number corresponds to a student in the line (the i-th number corresponds to the i-th from the left student in the line) and shows the number of throws h... | The output should contain a single integer β the number of variants of ball orders after the warm up exercise is complete. As the number can be rather large, print it modulo 1000000007 (109 + 7). | Input: 51 2 2 1 2 | Output: 120 | Expert | 1 | 1,956 | 574 | 195 | 3 | |
691 | A | 691A | A. Fashion in Berland | 1,000 | implementation | According to rules of the Berland fashion, a jacket should be fastened by all the buttons except only one, but not necessarily it should be the last one. Also if the jacket has only one button, it should be fastened, so the jacket will not swinging open.You are given a jacket with n buttons. Determine if it is fastened... | The first line contains integer n (1 β€ n β€ 1000) β the number of buttons on the jacket.The second line contains n integers ai (0 β€ ai β€ 1). The number ai = 0 if the i-th button is not fastened. Otherwise ai = 1. | In the only line print the word ""YES"" if the jacket is fastened in a right way. Otherwise print the word ""NO"". | Input: 31 0 1 | Output: YES | Beginner | 1 | 336 | 211 | 114 | 6 | |
2,075 | E | 2075E | E. XOR Matrix | 2,500 | bitmasks; combinatorics; dp; implementation; math | For two arrays \(a = [a_1, a_2, \dots, a_n]\) and \(b = [b_1, b_2, \dots, b_m]\), we define the XOR matrix \(X\) of size \(n \times m\), where for each pair \((i,j)\) (\(1 \le i \le n\); \(1 \le j \le m\)) it holds that \(X_{i,j} = a_i \oplus b_j\). The symbol \(\oplus\) denotes the bitwise XOR operation.You are given ... | The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Each test case consists of one line containing four integers \(n, m, A, B\) (\(2 \le n, m, A, B \le 2^{29} - 1\)). | For each test case, output one integer β the number of pairs of arrays \((a, b)\) that satisfy all three conditions. Since this number can be very large, output it modulo \(998244353\). | Input: 62 2 2 22 3 4 55 7 4 31337 42 1337 424 2 13 37536870902 536370902 536390912 466128231 | Output: 57 864 50360 439988899 112000 732195491 | Expert | 5 | 644 | 206 | 185 | 20 | |
98 | D | 98D | D. Help Monks | 2,500 | constructive algorithms | In a far away kingdom is the famous Lio Shan monastery. Gods constructed three diamond pillars on the monastery's lawn long ago. Gods also placed on one pillar n golden disks of different diameters (in the order of the diameters' decreasing from the bottom to the top). Besides, gods commanded to carry all the disks fro... | The first line contains an integer n β the number of disks (1 β€ n β€ 20). The second line contains n integers di β the disks' diameters after Ku Sean Sun cut their edges. The diameters are given from the bottom to the top (1 β€ di β€ 20, besides, di β₯ di + 1 for any 1 β€ i < n). | Print on the first line number m β the smallest number of moves to solve the gods' problem. Print on the next m lines the description of moves: two space-separated positive integers si and ti that determine the number of the pillar from which the disk is moved and the number of pillar where the disk is moved, correspon... | Pay attention to the third test demonstrating that the order of disks should remain the same in the end, even despite the disks' same radius. If this condition was not necessary to fulfill, the gods' task could have been solved within a smaller number of moves (three β simply moving the three disks from the first pilla... | Input: 33 2 1 | Output: 71 31 23 21 32 12 31 3 | Expert | 1 | 2,541 | 275 | 353 | 0 |
1,045 | H | 1045H | H. Self-exploration | 2,400 | math | Being bored of exploring the Moon over and over again Wall-B decided to explore something he is made of β binary numbers. He took a binary number and decided to count how many times different substrings of length two appeared. He stored those values in \(c_{00}\), \(c_{01}\), \(c_{10}\) and \(c_{11}\), representing how... | First two lines contain two positive binary numbers \(A\) and \(B\) (\(1 \leq A \leq B < 2^{100\,000}\)), representing the start and the end of the interval respectively. Binary numbers \(A\) and \(B\) have no leading zeroes.Next four lines contain decimal numbers \(c_{00}\), \(c_{01}\), \(c_{10}\) and \(c_{11}\) (\(0 ... | Output one integer number representing how many binary numbers in the interval \([A, B]\) satisfy the constraints mod \(10^9 + 7\). | Example 1: The binary numbers in the interval \([10,1001]\) are \(10,11,100,101,110,111,1000,1001\). Only number 110 satisfies the constraints: \(c_{00} = 0, c_{01} = 0, c_{10} = 1, c_{11} = 1\).Example 2: No number in the interval satisfies the constraints | Input: 1010010011 | Output: 1 | Expert | 1 | 1,147 | 451 | 131 | 10 |
2,129 | C3 | 2129C3 | C3. Interactive RBS (Hard Version) | 2,300 | binary search; bitmasks; constructive algorithms; dp; interactive | This is an interactive problem.This is the hard version of the problem. The only difference is the limit on the number of queries. You can make hacks only if all versions of the problem are solved.There is a hidden bracket sequence \(s\) of length \(n\), where \(s\) only contains \(\texttt{'('}\) and \(\texttt{')'}\). ... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 20\)). The description of the test cases follows. | In the first test case, the hidden bracket sequence is \(s=\texttt{"")((""}\).For the query ""? 4 1 2 3 3"", the jury returns \(0\) because \(f(s_{1}s_{2}s_{3}s_{3}) = f(\texttt{"")(((""}) = 0\).For the query ""? 2 2 1"", the jury returns \(1\) because \(f(s_{2}s_{1}) = f(\texttt{""()""}) = 1\).For the query ""? 2 3 1"... | Input: 2 3 0 1 1 2 3 | Output: ? 4 1 2 3 3 ? 2 2 1 ? 2 3 1 ! )(( ? 4 1 2 1 2 ! () | Expert | 5 | 1,478 | 159 | 0 | 21 | |
1,299 | E | 1299E | E. So Mean | 3,400 | interactive; math | This problem is interactive.We have hidden a permutation \(p_1, p_2, \dots, p_n\) of numbers from \(1\) to \(n\) from you, where \(n\) is even. You can try to guess it using the following queries:\(?\) \(k\) \(a_1\) \(a_2\) \(\dots\) \(a_k\).In response, you will learn if the average of elements with indexes \(a_1, a_2... | The first line contains a single integer \(n\) (\(2 \le n \le 800\), \(n\) is even). | Input: 2 1 2 | Output: ? 1 2 ? 1 1 ! 1 2 | Master | 2 | 954 | 84 | 0 | 12 | ||
1,773 | E | 1773E | E. Easy Assembly | 1,400 | greedy; sortings | Emma loves playing with blocks. She has several cubic blocks of the same size that are numbered with distinct integers written on them. She assembles towers from those blocks by stacking them vertically. A configuration of her game is a set of towers that she has assembled from the blocks. Emma can perform two kinds of... | The first line of the input file contains an integer \(n\) (\(1 \le n \le 10\,000\)) β the number of towers in the initial configuration. Next \(n\) lines describe towers. Each tower \(i\) is described by a line that starts with the number \(k_i\) (\(k_i \ge 1\); \(\sum_1^n{k_i} \le 10\,000\)) β the number of blocks in... | Output a line with two integers \(s\) and \(c\) β the number of split and combine operations Emma should make to get a single tower with blocks sorted by their numbers, so that the total number of operations is minimized. | The example needs the following operations (1 split and 2 combines).InitialSplit lastCombined 2nd onto 1stCombined 1st onto 2nd | Input: 2 3 3 5 8 2 9 2 | Output: 1 2 | Easy | 2 | 1,249 | 535 | 221 | 17 |
571 | E | 571E | E. Geometric Progressions | 3,200 | math | Geometric progression with the first element a and common ratio b is a sequence of numbers a, ab, ab2, ab3, ....You are given n integer geometric progressions. Your task is to find the smallest integer x, that is the element of all the given progressions, or else state that such integer does not exist. | The first line contains integer (1 β€ n β€ 100) β the number of geometric progressions. Next n lines contain pairs of integers a, b (1 β€ a, b β€ 109), that are the first element and the common ratio of the corresponding geometric progression. | If the intersection of all progressions is empty, then print - 1, otherwise print the remainder of the minimal positive integer number belonging to all progressions modulo 1000000007 (109 + 7). | In the second sample test one of the progressions contains only powers of two, the other one contains only powers of three. | Input: 22 24 1 | Output: 4 | Master | 1 | 303 | 239 | 193 | 5 |
612 | C | 612C | C. Replace To Make Regular Bracket Sequence | 1,400 | data structures; expression parsing; math | You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.The following definition of a r... | The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 106. | If it's impossible to get RBS from s print Impossible.Otherwise print the least number of replaces needed to get RBS from s. | Input: [<}){} | Output: 2 | Easy | 3 | 700 | 144 | 124 | 6 | |
1,357 | C2 | 1357C2 | C2. Prepare superposition of basis states with the same parity | 0 | *special | You are given \(N\) qubits in the state \(|0...0 \rangle\), and an integer \(parity \in \{0, 1\}\). Your task is to prepare an equal superposition of all basis states that have the given parity of the number of \(1\)s in their binary notation, i.e., the basis states that have an even number of \(1\)s if \(parity = 0\) ... | Beginner | 1 | 1,233 | 0 | 0 | 13 | ||||
1,773 | G | 1773G | G. Game of Questions | 2,800 | bitmasks; combinatorics; dp; fft; math; probabilities | Genie is taking part in an intellectual game. The game consists of \(n\) questions, and there are \(m\) participants numbered from \(1\) to \(m\). Genie is the participant number \(1\).For each question \(i\) and participant \(j\), it is known whether the participant will answer the question correctly or not.The goal o... | The first line contains two integers \(n\) and \(m\) β the number of questions and the number of participants (\(1 \le n \le 2 \cdot 10^5\); \(2 \le m \le 17\)).The \(i\)-th of the next \(n\) lines contains \(m\) characters \(s_{i, 1}, s_{i, 2}, \ldots, s_{i, m}\). Character \(s_{i, j}\) is '1' if participant \(j\) ans... | Print the probability that Genie will win the game. Your answer will be considered correct if its absolute or relative error does not exceed \(10^{-9}\). | In the first example, there is a single question and Genie will answer it correctly, thus winning the game (along with participants \(2\) and \(4\)).In the second example, one participant will leave after the first asked question, and another participant will leave after the second asked question. Each participant will... | Input: 1 5 11010 | Output: 1.0000000000000000 | Master | 6 | 993 | 367 | 153 | 17 |
1,532 | D | 1532D | D. Teams Forming | 0 | *special; sortings | There are \(n\) students in a university. The number of students is even. The \(i\)-th student has programming skill equal to \(a_i\). The coach wants to form \(\frac{n}{2}\) teams. Each team should consist of exactly two students, and each student should belong to exactly one team. Two students can form a team only if... | The first line of the input contains one integer \(n\) (\(2 \le n \le 100\)) β the number of students. It is guaranteed that \(n\) is even.The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 100\)), where \(a_i\) is the skill of the \(i\)-th student. | Print one number β the minimum total number of problems students should solve to form exactly \(\frac{n}{2}\) teams. | In the first example the optimal teams will be: \((3, 4)\), \((1, 6)\) and \((2, 5)\), where numbers in brackets are indices of students. Then, to form the first team the third student should solve \(1\) problem, to form the second team nobody needs to solve problems and to form the third team the second student should... | Input: 6 5 10 2 3 14 5 | Output: 5 | Beginner | 2 | 715 | 293 | 116 | 15 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.