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,567 | F | 1567F | F. One-Four Overload | 2,700 | 2-sat; constructive algorithms; dfs and similar; dsu; graphs; implementation | Alice has an empty grid with \(n\) rows and \(m\) columns. Some of the cells are marked, and no marked cells are adjacent to the edge of the grid. (Two squares are adjacent if they share a side.) Alice wants to fill each cell with a number such that the following statements are true: every unmarked cell contains either... | The first line of input contains two integers \(n\) and \(m\) (\(1 \leq n, m \leq 500\)) β the number of rows and the number of columns in the grid, respectively.Then \(n\) lines follow, each containing \(m\) characters. Each of these characters is either '.' or 'X' β an unmarked and a marked cell, respectively. No mar... | Output ""'NO"" if no suitable grid exists. Otherwise, output ""'YES""'. Then output \(n\) lines of \(m\) space-separated integers β the integers in the grid. | It can be shown that no such grid exists for the second test. | Input: 5 5 ..... .XXX. .X.X. .XXX. ..... | Output: YES 4 1 4 4 1 4 5 5 5 1 4 5 1 5 4 1 5 5 5 4 1 4 4 1 4 | Master | 6 | 681 | 367 | 157 | 15 |
1,930 | D1 | 1930D1 | D1. Sum over all Substrings (Easy Version) | 1,800 | brute force; dp; greedy; strings | This is the easy version of the problem. The only difference between the two versions is the constraint on \(t\) and \(n\). You can make hacks only if both versions of the problem are solved.For a binary\(^\dagger\) pattern \(p\) and a binary string \(q\), both of length \(m\), \(q\) is called \(p\)-good if for every \... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 500\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 100\)) β the length of the binary string \(s\).The se... | For each test case, output the sum of values of \(f\) over all substrings of \(s\). | In the first test case, the only \(\mathtt{1}\)-good string is \(\mathtt{1}\). Thus, \(f(\mathtt{1})=1\).In the second test case, \(f(\mathtt{10})=1\) because \(\mathtt{01}\) is \(\mathtt{10}\)-good, and \(\mathtt{00}\) is not \(\mathtt{10}\)-good. Thus, the answer is \(f(\mathtt{1})+f(\mathtt{10})+f(\mathtt{0}) = 1 + ... | Input: 4112105000002011110110000000111111 | Output: 1 2 0 346 | Medium | 4 | 1,340 | 545 | 83 | 19 |
1,967 | B2 | 1967B2 | B2. Reverse Card (Hard Version) | 2,200 | brute force; math; number theory | The two versions are different problems. You may want to read both versions. You can make hacks only if both versions are solved.You are given two positive integers \(n\), \(m\).Calculate the number of ordered pairs \((a, b)\) satisfying the following conditions: \(1\le a\le n\), \(1\le b\le m\); \(b \cdot \gcd(a,b)\) ... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1\le t\le 10^4\)). The description of the test cases follows.The first line of each test case contains two integers \(n\), \(m\) (\(1\le n,m\le 2 \cdot 10^6\)).It is guaranteed that neither the sum of \(n\) nor the sum of ... | For each test case, print a single integer: the number of valid pairs. | In the first test case, no pair satisfies the conditions.In the fourth test case, \((2,2),(3,6),(4,4),(6,3),(6,6),(8,8)\) satisfy the conditions. | Input: 61 12 33 510 8100 12331000000 1145141 | Output: 0 1 1 6 423 5933961 | Hard | 3 | 345 | 371 | 70 | 19 |
1,042 | B | 1042B | B. Vitamins | 1,200 | bitmasks; brute force; dp; implementation | Berland shop sells \(n\) kinds of juices. Each juice has its price \(c_i\). Each juice includes some set of vitamins in it. There are three types of vitamins: vitamin ""A"", vitamin ""B"" and vitamin ""C"". Each juice can contain one, two or all three types of vitamins in it.Petya knows that he needs all three types of... | The first line contains a single integer \(n\) \((1 \le n \le 1\,000)\) β the number of juices.Each of the next \(n\) lines contains an integer \(c_i\) \((1 \le c_i \le 100\,000)\) and a string \(s_i\) β the price of the \(i\)-th juice and the vitamins it contains. String \(s_i\) contains from \(1\) to \(3\) characters... | Print -1 if there is no way to obtain all three vitamins. Otherwise print the minimum total price of juices that Petya has to buy to obtain all three vitamins. | In the first example Petya buys the first, the second and the fourth juice. He spends \(5 + 6 + 4 = 15\) and obtains all three vitamins. He can also buy just the third juice and obtain three vitamins, but its cost is \(16\), which isn't optimal.In the second example Petya can't obtain all three vitamins, as no juice co... | Input: 45 C6 B16 BAC4 A | Output: 15 | Easy | 4 | 526 | 520 | 159 | 10 |
1,360 | D | 1360D | D. Buying Shovels | 1,300 | math; number theory | Polycarp wants to buy exactly \(n\) shovels. The shop sells packages with shovels. The store has \(k\) types of packages: the package of the \(i\)-th type consists of exactly \(i\) shovels (\(1 \le i \le k\)). The store has an infinite number of packages of each type.Polycarp wants to choose one type of packages and th... | The first line contains an integer \(t\) (\(1 \le t \le 100\)) β the number of test cases in the input. Then, \(t\) test cases follow, one per line.Each test case consists of two positive integers \(n\) (\(1 \le n \le 10^9\)) and \(k\) (\(1 \le k \le 10^9\)) β the number of shovels and the number of types of packages. | Print \(t\) answers to the test cases. Each answer is a positive integer β the minimum number of packages. | The answer to the first test case was explained in the statement.In the second test case, there is only one way to buy \(8\) shovels β \(8\) packages of one shovel.In the third test case, you need to buy a \(1\) package of \(6\) shovels. | Input: 5 8 7 8 1 6 10 999999733 999999732 999999733 999999733 | Output: 2 8 1 999999733 1 | Easy | 2 | 829 | 319 | 106 | 13 |
2,084 | B | 2084B | B. MIN = GCD | 1,100 | greedy; math; number theory | You are given a positive integer sequence \(a\) of length \(n\). Determine if it is possible to rearrange \(a\) such that there exists an integer \(i\) (\(1 \le i<n\)) satisfying $$$\( \min([a_1,a_2,\ldots,a_i])=\gcd([a_{i+1},a_{i+2},\ldots,a_n]). \)\(Here \)\gcd(c)\( denotes the greatest common divisor of \)c\(, which... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(2 \le n \le 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1... | For each test case, output ""Yes"" if it is possible, and ""No"" otherwise.You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses. | In the first test case, rearrange \(a\) to \([1,1]\) and let \(i=1\), then \(\min([1])=\gcd([1])\).In the second test case, it can be shown that it is impossible.In the third test case, rearrange \(a\) to \([3,2,2]\) and let \(i=2\), then \(\min([3,2])=\gcd([2])\).In the fifth test case, rearrange \(a\) to \([3,4,5,6,9... | Input: 721 121 232 2 332 3 454 5 6 9 33998244359987710471 99824435698771045 100000000761 1 4 5 1 4 | Output: Yes No Yes No Yes Yes Yes | Easy | 3 | 389 | 428 | 236 | 20 |
638 | D | 638D | D. Three-dimensional Turtle Super Computer | 1,800 | brute force; dfs and similar; graphs | A super computer has been built in the Turtle Academy of Sciences. The computer consists of nΒ·mΒ·k CPUs. The architecture was the paralellepiped of size n Γ m Γ k, split into 1 Γ 1 Γ 1 cells, each cell contains exactly one CPU. Thus, each CPU can be simultaneously identified as a group of three numbers from the layer nu... | The first line contains three integers n, m and k (1 β€ n, m, k β€ 100) β the dimensions of the Super Computer. Then n blocks follow, describing the current state of the processes. The blocks correspond to the layers of the Super Computer in the order from 1 to n. Each block consists of m lines, k characters in each β th... | Print a single integer β the number of critical CPUs, that is, such that turning only this CPU off will disrupt some control. | In the first sample the whole first layer of CPUs is malfunctional. In the second layer when CPU (2, 1, 2) turns off, it disrupts the control by CPU (2, 1, 3) over CPU (2, 1, 1), and when CPU (2, 2, 2) is turned off, it disrupts the control over CPU (2, 2, 3) by CPU (2, 2, 1).In the second sample all processors except ... | Input: 2 2 3000000111111 | Output: 2 | Medium | 3 | 1,630 | 646 | 125 | 6 |
171 | D | 171D | D. Broken checker | 1,300 | *special; brute force | ""This problem is rubbish! There is not statement, and there are only 5 test cases. The problemsetter took liberties with this problem!"" β people complained in the comments to one round on Codeforces. And even more... No, wait, the checker for the problem was alright, that's a mercy. | The only line of the input contains an integer between 1 and 5, inclusive. All tests for this problem are different. The contents of the test case doesn't need to be equal to its index. | The only line of the output contains an integer between 1 and 3, inclusive. | This problem has no samples, since there so few test cases. | Easy | 2 | 285 | 185 | 75 | 1 | |
534 | B | 534B | B. Covered Path | 1,400 | dp; greedy; math | The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v1 meters per second, and in the end it is v2 meters per second. We know that this section of the route took exactly t seconds to pass.Assuming that at each of the seconds the speed is constant, and b... | The first line contains two integers v1 and v2 (1 β€ v1, v2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum v... | Print the maximum possible length of the path segment in meters. | In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters. | Input: 5 64 2 | Output: 26 | Easy | 3 | 569 | 614 | 64 | 5 |
1,490 | C | 1490C | C. Sum of Cubes | 1,100 | binary search; brute force; brute force; math | You are given a positive integer \(x\). Check whether the number \(x\) is representable as the sum of the cubes of two positive integers.Formally, you need to check if there are two integers \(a\) and \(b\) (\(1 \le a, b\)) such that \(a^3+b^3=x\).For example, if \(x = 35\), then the numbers \(a=2\) and \(b=3\) are sui... | The first line contains one integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. Then \(t\) test cases follow.Each test case contains one integer \(x\) (\(1 \le x \le 10^{12}\)).Please note, that the input for some test cases won't fit into \(32\)-bit integer type, so you should use at least \(64\)-bit integ... | For each test case, output on a separate line: ""YES"" if \(x\) is representable as the sum of the cubes of two positive integers. ""NO"" otherwise. You can output ""YES"" and ""NO"" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive). | The number \(1\) is not representable as the sum of two cubes.The number \(2\) is represented as \(1^3+1^3\).The number \(4\) is not representable as the sum of two cubes.The number \(34\) is not representable as the sum of two cubes.The number \(35\) is represented as \(2^3+3^3\).The number \(16\) is represented as \(... | Input: 7 1 2 4 34 35 16 703657519796 | Output: NO YES NO NO YES YES YES | Easy | 4 | 413 | 357 | 275 | 14 |
1,252 | L | 1252L | L. Road Construction | 2,300 | flows; graphs | There are \(N\) cities in the country of Numbata, numbered from \(1\) to \(N\). Currently, there is no road connecting them. Therefore, each of these \(N\) cities proposes a road candidate to be constructed.City \(i\) likes to connect with city \(A_i\), so city \(i\) proposes to add a direct bidirectional road connecti... | Input begins with a line containing two integers: \(N\) \(K\) (\(3 \le N \le 2000\); \(1 \le K \le 2000\)) representing the number of cities and the number of workers, respectively. The next \(N\) lines each contains several integers: \(A_i\) \(M_i\) \((B_i)_1\), \((B_i)_2\), \(\cdots\), \((B_i)_{M_i}\) (\(1 \le A_i \l... | If it is not possible to assign each worker to construct a road such that any pair of cities are connected by a sequence of constructed road, simply output -1 in a line. Otherwise, for each worker in the same order as input, output in a line two integers (separated by a single space): \(u\) and \(v\) in any order. This... | Explanation for the sample input/output #1We can assign the workers to construct the following roads: The first worker constructs a road connecting city \(1\) and city \(2\). The second worker constructs a road connecting city \(2\) and city \(3\). The third worker constructs a road connecting city \(3\) and city \(4\)... | Input: 4 5 2 2 1 2 3 2 2 3 4 2 3 4 2 2 4 5 1 2 3 4 5 | Output: 1 2 2 3 3 4 0 0 4 2 | Expert | 2 | 1,646 | 848 | 671 | 12 |
359 | B | 359B | B. Permutation | 1,400 | constructive algorithms; dp; math | A permutation p is an ordered group of numbers p1, p2, ..., pn, consisting of n distinct positive integers, each is no more than n. We'll define number n as the length of permutation p1, p2, ..., pn.Simon has a positive integer n and a non-negative integer k, such that 2k β€ n. Help him find permutation a of length 2n, ... | The first line contains two integers n and k (1 β€ n β€ 50000, 0 β€ 2k β€ n). | Print 2n integers a1, a2, ..., a2n β the required permutation a. It is guaranteed that the solution exists. If there are multiple solutions, you can print any of them. | Record |x| represents the absolute value of number x. In the first sample |1 - 2| - |1 - 2| = 0.In the second sample |3 - 2| + |1 - 4| - |3 - 2 + 1 - 4| = 1 + 3 - 2 = 2.In the third sample |2 - 7| + |4 - 6| + |1 - 3| + |5 - 8| - |2 - 7 + 4 - 6 + 1 - 3 + 5 - 8| = 12 - 12 = 0. | Input: 1 0 | Output: 1 2 | Easy | 3 | 355 | 73 | 167 | 3 |
884 | A | 884A | A. Book Reading | 800 | implementation | Recently Luba bought a very interesting book. She knows that it will take t seconds to read the book. Luba wants to finish reading as fast as she can.But she has some work to do in each of n next days. The number of seconds that Luba has to spend working during i-th day is ai. If some free time remains, she can spend i... | The first line contains two integers n and t (1 β€ n β€ 100, 1 β€ t β€ 106) β the number of days and the time required to read the book.The second line contains n integers ai (0 β€ ai β€ 86400) β the time Luba has to spend on her work during i-th day. | Print the minimum day Luba can finish reading the book.It is guaranteed that answer doesn't exceed n. | Input: 2 286400 86398 | Output: 2 | Beginner | 1 | 505 | 245 | 101 | 8 | |
766 | D | 766D | D. Mahmoud and a Dictionary | 2,000 | data structures; dfs and similar; dp; dsu; graphs | Mahmoud wants to write a new dictionary that contains n words and relations between them. There are two types of relations: synonymy (i. e. the two words mean the same) and antonymy (i. e. the two words mean the opposite). From time to time he discovers a new relation between two words.He know that if two words have a ... | The first line of input contains three integers n, m and q (2 β€ n β€ 105, 1 β€ m, q β€ 105) where n is the number of words in the dictionary, m is the number of relations Mahmoud figured out and q is the number of questions Mahmoud asked after telling all relations.The second line contains n distinct words a1, a2, ..., an... | First, print m lines, one per each relation. If some relation is wrong (makes two words opposite and have the same meaning at the same time) you should print ""NO"" (without quotes) and ignore it, otherwise print ""YES"" (without quotes).After that print q lines, one per each question. If the two words have the same me... | Input: 3 3 4hate love like1 love like2 love hate1 hate likelove likelove hatelike hatehate like | Output: YESYESNO1222 | Hard | 5 | 1,619 | 1,066 | 458 | 7 | |
477 | D | 477D | D. Dreamoon and Binary | 2,700 | dp; strings | Dreamoon saw a large integer x written on the ground and wants to print its binary form out. Dreamoon has accomplished the part of turning x into its binary format. Now he is going to print it in the following manner.He has an integer n = 0 and can only perform the following two operations in any order for unlimited ti... | The single line of the input contains a binary integer representing x (1 β€ x < 25000) without leading zeros. | The first line of the output should contain an integer representing the number of different ideal sequences modulo 1000000007 (109 + 7).The second line of the output contains an integer representing the minimal length of an ideal sequence modulo 1000000007 (109 + 7). | For the first sample, the shortest and the only ideal sequence is Β«222221Β» of length 6.For the second sample, there are three ideal sequences Β«21211Β», Β«212222222221Β», Β«222222222222222222222222221Β». Among them the shortest one has length 5. | Input: 101 | Output: 16 | Master | 2 | 1,094 | 108 | 267 | 4 |
1,873 | D | 1873D | D. 1D Eraser | 800 | greedy; implementation; two pointers | You are given a strip of paper \(s\) that is \(n\) cells long. Each cell is either black or white. In an operation you can take any \(k\) consecutive cells and make them all white.Find the minimum number of operations needed to remove all black cells. | The first line contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \leq k \leq n \leq 2 \cdot 10^5\)) β the length of the paper and the integer used in the operation.The second line of each test case contains a s... | For each test case, output a single integer β the minimum number of operations needed to remove all black cells. | In the first test case you can perform the following operations: $$$\(\color{red}{\texttt{WBW}}\texttt{WWB} \to \texttt{WWW}\color{red}{\texttt{WWB}} \to \texttt{WWWWWW}\)\(In the second test case you can perform the following operations: \)\(\texttt{WW}\color{red}{\texttt{BWB}}\texttt{WW} \to \texttt{WWWWWWW}\)\(In th... | Input: 86 3WBWWWB7 3WWBWBWW5 4BWBWB5 5BBBBB8 2BWBWBBBB10 2WBBWBBWBBW4 1BBBB3 2WWW | Output: 2 1 2 1 4 3 4 0 | Beginner | 3 | 251 | 532 | 112 | 18 |
1,213 | C | 1213C | C. Book Reading | 1,200 | math | Polycarp is reading a book consisting of \(n\) pages numbered from \(1\) to \(n\). Every time he finishes the page with the number divisible by \(m\), he writes down the last digit of this page number. For example, if \(n=15\) and \(m=5\), pages divisible by \(m\) are \(5, 10, 15\). Their last digits are \(5, 0, 5\) co... | The first line of the input contains one integer \(q\) (\(1 \le q \le 1000\)) β the number of queries.The following \(q\) lines contain queries, one per line. Each query is given as two integers \(n\) and \(m\) (\(1 \le n, m \le 10^{16}\)) β the number of pages in the book and required divisor, respectively. | For each query print the answer for it β the sum of digits written down by Polycarp. | Input: 7 1 1 10 1 100 3 1024 14 998244353 1337 123 144 1234312817382646 13 | Output: 1 45 153 294 3359835 0 427262129093995 | Easy | 1 | 474 | 309 | 84 | 12 | |
1,976 | D | 1976D | D. Invertible Bracket Sequences | 2,000 | binary search; combinatorics; data structures; divide and conquer; implementation; two pointers | A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example: bracket sequences ""()()"" and ""(())"" are regular (the resulting expressions are: ""(1)+(1)"" and ""((1+1)+1)"... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The only line of each test case contains a non-empty regular bracket sequence; it consists only of characters '(' and/or ')'.Additional constraint on the input: the total length of the regular bracket sequences over all tes... | For each test case, print a single integer β the number of pairs \((l,r)\) meeting the conditions from the statement. | In the first example, there is only one pair: \((2, 3)\): (()) \(\rightarrow\) ()(). In the second example, there are no pairs.In the third example, there are three pairs: \((2, 3)\): ()()() \(\rightarrow\) (())(); \((4, 5)\): ()()() \(\rightarrow\) ()(()); \((2, 5)\): ()()() \(\rightarrow\) (()()); | Input: 4(())()()()()(()())(()) | Output: 1 0 3 13 | Hard | 6 | 892 | 360 | 117 | 19 |
1,983 | F | 1983F | F. array-value | 2,500 | binary search; bitmasks; data structures; greedy; two pointers | You have an array of non-negative integers \(a_1, a_2, \ldots, a_n\).The value of a sub-array of length \(\ge 2\), \(a[l, r] = [a_l, a_{l+1}, \ldots, a_r]\) is the minimum value of \(a_i \oplus a_j\) such that \(l \le i < j \le r\), where \(\oplus\) is the xor (exclusive-or) operator.You have to find the \(k\)-th small... | The first line of the input contains multiple test cases \(t\) (\(1 \le t \le 2 \cdot 10^4\)).The first line of each test case contains integer numbers \(n\) and \(k\) (\(2 \le n \le 10^5\), \(1 \le k \le \frac{n\cdot(n-1)}{2}\)).The second line of the input contains \(n\) non-negative integer numbers \(a_1, a_2, \ldot... | Print the \(k\)-th smallest value obtained over all subarrays of length at least \(2\). | In the first testcase, we have subarrays with their smallest exclusive-or pair as:\([1,2]: 3\)\([2,3]: 1\)\([3,4]: 7\)\([4,5]: 1\)\([1,2,3]: 1\)\([2,3,4]: 1\)\([3,4,5]: 1\)\([1,2,3,4]: 1\)\([2,3,4,5]: 1\)\([1,2,3,4,5]: 1\)The sorted order would be: \(1, 1, 1, 1, 1, 1, 1, 1, 3, 7\). Therefore, the second smallest elemen... | Input: 45 21 2 3 4 52 14 34 61 2 4 85 91 2 3 4 5 | Output: 1 7 12 3 | Expert | 5 | 370 | 457 | 87 | 19 |
2,084 | G1 | 2084G1 | G1. Wish Upon a Satellite (Easy Version) | 2,600 | dp; games | This is the easy version of the problem. The difference between the versions is that in this version, \(t \le 1000\), \(n \le 5000\) and the sum of \(n\) does not exceed \(5000\). You can hack only if you solved all versions of this problem. For a non-empty sequence \(c\) of length \(k\), define \(f(c)\) as follows: Tu... | 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 a single integer \(n\) (\(1 \le n \le 5000\)).The second line of each test case contains \(n\) integers \(a_1, a_2, ... | For each test case, output a single integer β the maximum beauty of the permutation \(b\). | In the first test case, the permutation \(b\) with the maximum beauty is \([1, 2]\). The beauty of \([1, 2]\) is \(4\) since \(f([1]) + f([2]) + f([1, 2]) = 1 + 2 + 1 = 4\). If \(c = [1, 2]\), then \(f(c) = 1\) since Turtle can only choose \(i = 1\) and he will set \(c_1\) to \(\min(c_1, c_2) = 1\).In the second test c... | Input: 821 030 0 030 1 053 2 4 5 170 3 2 5 0 0 0101 2 6 5 8 9 0 0 0 050 4 1 0 050 1 5 2 3 | Output: 4 12 11 44 110 300 45 40 | Expert | 2 | 2,563 | 517 | 90 | 20 |
1,575 | I | 1575I | I. Illusions of the Desert | 2,300 | data structures; trees | Chanek Jones is back, helping his long-lost relative Indiana Jones, to find a secret treasure in a maze buried below a desert full of illusions.The map of the labyrinth forms a tree with \(n\) rooms numbered from \(1\) to \(n\) and \(n - 1\) tunnels connecting them such that it is possible to travel between each pair o... | The first line contains two integers \(n\) and \(q\) (\(2 \leq n \leq 10^5\), \(1 \leq q \leq 10^5\)) β the number of rooms in the maze and the number of queries.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq |a_i| \leq 10^9\)) β inital illusion rate of each room.The \(i\)-th of the next \(... | For each type \(2\) query, output a line containing an integer β the minimum sum of energy needed for Chanek and Indiana to take the secret treasure. | In the first query, their movement from the \(1\)-st to the \(2\)-nd room is as follows. \(1 \rightarrow 5\) β takes \(\max(|10 + 4|, |10 - 4|) = 14\) energy. \(5 \rightarrow 6\) β takes \(\max(|4 + (-6)|, |4 - (-6)|) = 10\) energy. \(6 \rightarrow 2\) β takes \(\max(|-6 + (-9)|, |-6 - (-9)|) = 15\) energy. In total, i... | Input: 6 4 10 -9 2 -1 4 -6 1 5 5 4 5 6 6 2 6 3 2 1 2 1 1 -3 2 1 2 2 3 3 | Output: 39 32 0 | Expert | 2 | 1,142 | 587 | 149 | 15 |
1,666 | F | 1666F | F. Fancy Stack | 2,200 | combinatorics; dp; implementation | Little Fiona has a collection of \(n\) blocks of various sizes \(a_1, a_2, \ldots, a_n\), where \(n\) is even. Some of the blocks can be equal in size. She would like to put all these blocks one onto another to form a fancy stack.Let \(b_1, b_2, \ldots, b_n\) be the sizes of blocks in the stack from top to bottom. Sinc... | Each input contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 2500\)). Description of the test cases follows.The first line of each test case contains a single integer \(n\) β the number of blocks at Fiona's disposal (\(2 \le n \le 5000\); \(n\) is even). The second line... | For each test case, print the number of ways to build a fancy stack, modulo \(998\,244\,353\). | Input: 241 2 3 481 1 2 3 4 4 6 7 | Output: 2 4 | Hard | 3 | 1,161 | 554 | 94 | 16 | |
214 | B | 214B | B. Hometask | 1,600 | brute force; constructive algorithms; greedy; math | 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 set of digits, your task is to find the maximum integer that you can make ... | A single line contains a single integer n (1 β€ n β€ 100000) β the number of digits in the set. The second line contains n digits, the digits are separated by a single space. | On a single line print the answer to the problem. If such number does not exist, then you should print -1. | In the first sample there is only one number you can make β 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number. | Input: 10 | Output: 0 | Medium | 4 | 584 | 172 | 106 | 2 |
1,582 | B | 1582B | B. Luntik and Subsequences | 900 | combinatorics; math | Luntik came out for a morning stroll and found an array \(a\) of length \(n\). He calculated the sum \(s\) of the elements of the array (\(s= \sum_{i=1}^{n} a_i\)). Luntik calls a subsequence of the array \(a\) nearly full if the sum of the numbers in that subsequence is equal to \(s-1\).Luntik really wants to know the... | The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. The next \(2 \cdot t\) lines contain descriptions of test cases. The description of each test case consists of two lines.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 60\)) β the length of... | For each test case print the number of nearly full subsequences of the array. | In the first test case, \(s=1+2+3+4+5=15\), only \((2,3,4,5)\) is a nearly full subsequence among all subsequences, the sum in it is equal to \(2+3+4+5=14=15-1\).In the second test case, there are no nearly full subsequences.In the third test case, \(s=1+0=1\), the nearly full subsequences are \((0)\) and \(()\) (the s... | Input: 5 5 1 2 3 4 5 2 1000 1000 2 1 0 5 3 0 2 1 1 5 2 1 0 3 0 | Output: 1 0 2 4 4 | Beginner | 2 | 585 | 456 | 77 | 15 |
330 | A | 330A | A. Cakeminator | 800 | brute force; implementation | You are given a rectangular cake, represented as an r Γ c grid. Each cell either has an evil strawberry, or is empty. For example, a 3 Γ 4 cake may look as follows: The cakeminator is going to eat the cake! Each time he eats, he chooses a row or a column that does not contain any evil strawberries and contains at least... | The first line contains two integers r and c (2 β€ r, c β€ 10), denoting the number of rows and the number of columns of the cake. The next r lines each contains c characters β the j-th character of the i-th line denotes the content of the cell at row i and column j, and is either one of these: '.' character denotes a ca... | Output the maximum number of cake cells that the cakeminator can eat. | For the first example, one possible way to eat the maximum number of cake cells is as follows (perform 3 eats). | Input: 3 4S.........S. | Output: 8 | Beginner | 2 | 519 | 411 | 69 | 3 |
638 | C | 638C | C. Road Improvement | 1,800 | *special; dfs and similar; graphs; greedy; trees | In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultane... | The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi). | First print number k β the minimum number of days needed to repair all the roads in Berland.In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated ... | In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day. | Input: 41 23 43 2 | Output: 22 2 11 3 | Medium | 5 | 628 | 250 | 533 | 6 |
955 | E | 955E | E. Icicles | 2,900 | Andrew's favourite Krakozyabra has recenly fled away and now he's eager to bring it back!At the moment the refugee is inside an icy cave with n icicles dangling from the ceiling located in integer coordinates numbered from 1 to n. The distance between floor and the i-th icicle is equal to ai.Andrew is free to choose an... | The first line contains the number of icicles n (2 β€ n β€ 105).The next line contains n space-separated numbers ai (1 β€ ai β€ 105) β the distances from floor to icicles. | Print an only integer β the minimum time it takes to entrap Krakozyabra between two fallen icicles. If it is impossible, print - 1. | In sample case one it's optimal to launch the sound wave from point 3. Then in two seconds icicles 1 and 5 will start falling, and in one more seconds they will block the paths. Krakozyabra will be located at at that time. Note that icicle number 3 will also be fallen, so there will actually be two icicles blocking the... | Input: 51 4 3 5 1 | Output: 3 | Master | 0 | 1,736 | 167 | 131 | 9 | |
1,260 | C | 1260C | C. Infinite Fence | 1,700 | greedy; math; number theory | You are a rebel leader and you are planning to start a revolution in your country. But the evil Government found out about your plans and set your punishment in the form of correctional labor.You must paint a fence which consists of \(10^{100}\) planks in two colors in the following way (suppose planks are numbered fro... | The first line contains single integer \(T\) (\(1 \le T \le 1000\)) β the number of test cases.The next \(T\) lines contain descriptions of test cases β one per line. Each test case contains three integers \(r\), \(b\), \(k\) (\(1 \le r, b \le 10^9\), \(2 \le k \le 10^9\)) β the corresponding coefficients. | Print \(T\) words β one per line. For each test case print REBEL (case insensitive) if the execution is unavoidable or OBEY (case insensitive) otherwise. | Input: 4 1 1 2 2 10 4 5 2 3 3 2 2 | Output: OBEY REBEL OBEY OBEY | Medium | 3 | 1,380 | 307 | 153 | 12 | |
1,374 | B | 1374B | B. Multiply by 2, divide by 6 | 900 | math | You are given an integer \(n\). In one move, you can either multiply \(n\) by two or divide \(n\) by \(6\) (if it is divisible by \(6\) without the remainder).Your task is to find the minimum number of moves needed to obtain \(1\) from \(n\) or determine if it's impossible to do that.You have to answer \(t\) independen... | 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 one integer \(n\) (\(1 \le n \le 10^9\)). | For each test case, print the answer β the minimum number of moves needed to obtain \(1\) from \(n\) if it's possible to do that or -1 if it's impossible to obtain \(1\) from \(n\). | Consider the sixth test case of the example. The answer can be obtained by the following sequence of moves from the given integer \(15116544\): Divide by \(6\) and get \(2519424\); divide by \(6\) and get \(419904\); divide by \(6\) and get \(69984\); divide by \(6\) and get \(11664\); multiply by \(2\) and get \(23328... | Input: 7 1 2 3 12 12345 15116544 387420489 | Output: 0 -1 2 -1 -1 12 36 | Beginner | 1 | 333 | 225 | 181 | 13 |
1,146 | B | 1146B | B. Hate ""A"" | 1,100 | implementation; strings | Bob has a string \(s\) consisting of lowercase English letters. He defines \(s'\) to be the string after removing all ""a"" characters from \(s\) (keeping all other characters in the same order). He then generates a new string \(t\) by concatenating \(s\) and \(s'\). In other words, \(t=s+s'\) (look at notes for an exa... | The first line of input contains a string \(t\) (\(1 \leq |t| \leq 10^5\)) consisting of lowercase English letters. | Print a string \(s\) that could have generated \(t\). It can be shown if an answer exists, it is unique. If no string exists, print "":("" (without double quotes, there is no space between the characters). | In the first example, we have \(s = \) ""aaaaa"", and \(s' = \) """".In the second example, no such \(s\) can work that will generate the given \(t\).In the third example, we have \(s = \) ""ababacac"", and \(s' = \) ""bbcc"", and \(t = s + s' = \) ""ababacacbbcc"". | Input: aaaaa | Output: aaaaa | Easy | 2 | 492 | 115 | 205 | 11 |
884 | E | 884E | E. Binary Matrix | 2,500 | dsu | You are given a matrix of size n Γ m. Each element of the matrix is either 1 or 0. You have to determine the number of connected components consisting of 1's. Two cells belong to the same component if they have a common border, and both elements in these cells are 1's.Note that the memory limit is unusual! | The first line contains two numbers n and m (1 β€ n β€ 212, 4 β€ m β€ 214) β the number of rows and columns, respectively. It is guaranteed that m is divisible by 4.Then the representation of matrix follows. Each of n next lines contains one-digit hexadecimal numbers (that is, these numbers can be represented either as dig... | Print the number of connected components consisting of 1's. | In the first example the matrix is: 000110101000It is clear that it has three components.The second example: 0101111111100011It is clear that the number of components is 2.There are no 1's in the third example, so the answer is 0. | Input: 3 41A8 | Output: 3 | Expert | 1 | 307 | 679 | 59 | 8 |
1,592 | F2 | 1592F2 | F2. Alice and Recoloring 2 | 2,800 | constructive algorithms; flows; graph matchings; greedy | The difference between the versions is in the costs of operations. Solution for one version won't work for another!Alice has a grid of size \(n \times m\), initially all its cells are colored white. The cell on the intersection of \(i\)-th row and \(j\)-th column is denoted as \((i, j)\). Alice can do the following ope... | The first line of the input contains \(2\) integers \(n, m\) (\(1 \le n, m \le 500\)) β the dimensions of the grid.The \(i\)-th of the next \(n\) lines contains a string \(s_i\) of length \(m\), consisting of letters W and B. The \(j\)-th character of string \(s_i\) is W if the cell \((i, j)\) is colored white in the f... | Output the smallest number of coins Alice would have to spend to achieve her favorite coloring. | In the first sample, it's optimal to just apply the fourth operation once to the rectangle containing cells \((2, 2), (2, 3), (3, 2), (3, 3)\). This would cost \(2\) coins. | Input: 3 3 WWW WBB WBB | Output: 2 | Master | 4 | 1,312 | 375 | 95 | 15 |
1,579 | E2 | 1579E2 | E2. Array Optimization by Deque | 1,700 | data structures; greedy | In fact, the problems E1 and E2 do not have much in common. You should probably think of them as two separate problems.You are given an integer array \(a[1 \ldots n] = [a_1, a_2, \ldots, a_n]\).Let us consider an empty deque (double-ended queue). A deque is a data structure that supports adding elements to both the beg... | The first line contains an integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.The next \(2t\) lines contain descriptions of the test cases. The first line of each test case description contains an integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β array size. The second line of the description contains \(n\)... | Print \(t\) lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer β the minimal possible number of inversions in the deque after executing the described algorithm. | One of the ways to get the sequence \([5, 3, 7, 5]\) in the deque, containing only two inversions, from the initial array \([3, 7, 5, 5]\) (the first sample test case) is described in the problem statement. Also, in this example, you could get the answer of two inversions by simply putting each element of the original ... | Input: 6 4 3 7 5 5 3 3 2 1 3 3 1 2 4 -1 2 2 -1 4 4 5 1 3 5 1 3 1 3 2 | Output: 2 0 1 0 1 2 | Medium | 2 | 1,693 | 499 | 233 | 15 |
1,551 | E | 1551E | E. Fixed Points | 2,000 | binary search; brute force; dp | Consider a sequence of integers \(a_1, a_2, \ldots, a_n\). In one move, you can select any element of the sequence and delete it. After an element is deleted, all elements to the right are shifted to the left by \(1\) position, so there are no empty spaces in the sequence. So after you make a move, the sequence's lengt... | The first line contains one integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. Then \(t\) test cases follow.Each test case consists of two consecutive lines. The first line contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 2000\)). The second line contains a sequence of integers \(a_1, a_2, \ldots... | For each test case output in a single line: \(-1\) if there's no desired move sequence; otherwise, the integer \(x\) (\(0 \le x \le n\)) β the minimum number of the moves to be made so that the resulting sequence will contain at least \(k\) elements that are equal to their indices. | In the first test case the sequence doesn't satisfy the desired condition, but it can be provided by deleting the first element, hence the sequence will be \([1, 2, 3, 4, 5, 6]\) and \(6\) elements will be equal to their indices.In the second test case there are two ways to get the desired result in \(2\) moves: the fi... | Input: 4 7 6 1 1 2 3 4 5 6 5 2 5 1 3 2 3 5 2 5 5 5 5 4 8 4 1 2 3 3 2 2 5 5 | Output: 1 2 -1 2 | Hard | 3 | 1,029 | 492 | 282 | 15 |
2,063 | E | 2063E | E. Triangle Tree | 2,300 | data structures; dfs and similar; dp; greedy; trees | One day, a giant tree grew in the countryside. Little John, with his childhood eagle, decided to make it his home. Little John will build a structure on the tree with galvanized square steel. However, little did he know, he could not build what is physically impossible. You are given a rooted tree\(^{\text{β}}\) contai... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(1 \le n \le 3 \cdot 10^5\)).Each of the next \(n-1\) lines contains two integers \(u_i\) a... | For each test case, output the answer on a separate line. | On the first test case, the only good pair \((i,j)\) satisfying \(i<j\) is \((2,3)\). Here, \(\text{lca}(2,3)\) is \(1\), and the two distances are \(1\) and \(1\).There is only one value of \(x\) for two side lengths \(1\) and \(1\), which is \(1\). Therefore, the answer for the first test case is \(1\).On the second ... | Input: 431 21 331 23 252 31 54 21 2112 12 32 44 56 55 74 88 97 1010 11 | Output: 1 0 4 29 | Expert | 5 | 1,551 | 565 | 57 | 20 |
855 | E | 855E | E. Salazar Slytherin's Locket | 2,200 | bitmasks; dp | Harry came to know from Dumbledore that Salazar Slytherin's locket is a horcrux. This locket was present earlier at 12 Grimmauld Place, the home of Sirius Black's mother. It was stolen from there and is now present in the Ministry of Magic in the office of Dolorous Umbridge, Harry's former Defense Against the Dark Arts... | First line of input contains q (1 β€ q β€ 105) β number of queries.Each of the next q lines contain three space separated integers bi, li, ri (2 β€ bi β€ 10, 1 β€ li β€ ri β€ 1018). | You have to output q lines, each containing a single integer, the answer to the corresponding query. | In sample test case 1, for first query, when we convert numbers 4 to 9 into base 2, we get: 4 = 1002, 5 = 1012, 6 = 1102, 7 = 1112, 8 = 10002, 9 = 10012. Out of these, only base 2 representation of 9 has even number of 1 and 0. Thus, the answer is 1. | Input: 22 4 93 1 10 | Output: 12 | Hard | 2 | 972 | 174 | 100 | 8 |
574 | A | 574A | A. Bear and Elections | 1,200 | greedy; implementation | Limak is a grizzly bear who desires power and adoration. He wants to win in upcoming elections and rule over the Bearland.There are n candidates, including Limak. We know how many citizens are going to vote for each candidate. Now i-th candidate would get ai votes. Limak is candidate number 1. To win in elections, he m... | The first line contains single integer n (2 β€ n β€ 100) - number of candidates.The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ 1000) - number of votes for each candidate. Limak is candidate number 1.Note that after bribing number of votes for some candidate might be zero or might be greater... | Print the minimum number of citizens Limak must bribe to have strictly more votes than any other candidate. | In the first sample Limak has 5 votes. One of the ways to achieve victory is to bribe 4 citizens who want to vote for the third candidate. Then numbers of votes would be 9, 1, 7, 2, 8 (Limak would have 9 votes). Alternatively, Limak could steal only 3 votes from the third candidate and 1 vote from the second candidate ... | Input: 55 1 11 2 8 | Output: 4 | Easy | 2 | 700 | 331 | 107 | 5 |
611 | G | 611G | G. New Year and Cake | 2,900 | geometry; two pointers | Limak is a little polar bear. According to some old traditions, his bear family prepared a New Year cake. And Limak likes cakes.As you may know, a New Year cake is a strictly convex polygon with n vertices.Parents won't allow Limak to eat more than half of a cake because he would get sick. After some thinking they deci... | The first line of the input contains a single integer n (4 β€ n β€ 500 000) β the number of vertices in the polygon denoting the cake.Each of the next n lines contains two integers xi and yi (|xi|, |yi| β€ 109) β coordinates of the i-th point.It's guaranteed that all points are distinct, polygon is strictly convex and poi... | Print the sum of values of Limak's disappointment over all possible scenarios modulo 109 + 7. | In the first sample possible values of Limak's disappointment are 0, 18, 18, 24, 30. | Input: 52 42 75 75 43 -2 | Output: 90 | Master | 2 | 804 | 357 | 93 | 6 |
1,650 | E | 1650E | E. Rescheduling the Exam | 1,900 | binary search; data structures; greedy; implementation; math; sortings | Now Dmitry has a session, and he has to pass \(n\) exams. The session starts on day \(1\) and lasts \(d\) days. The \(i\)th exam will take place on the day of \(a_i\) (\(1 \le a_i \le d\)), all \(a_i\) β are different. Sample, where \(n=3\), \(d=12\), \(a=[3,5,9]\). Orange β exam days. Before the first exam Dmitry will... | The first line of input data contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of input test cases. The descriptions of test cases follow.An empty line is written in the test before each case.The first line of each test case contains two integers \(n\) and \(d\) (\(2 \le n \le 2 \cdot 10^5, 1 \le d \le 10^9... | For each test case, output the maximum possible value of \(\mu\) if Dmitry can move any one exam to an arbitrary day. All values of \(a_i\) should remain distinct. | The first sample is parsed in statement.One of the optimal schedule changes for the second sample: Initial schedule. New schedule.In the third sample, we need to move the exam from day \(1\) to any day from \(4\) to \(100\).In the fourth sample, any change in the schedule will only reduce \(\mu\), so the schedule shoul... | Input: 93 123 5 92 51 52 1001 25 153 6 9 12 153 10000000001 400000000 5000000002 103 42 21 24 156 11 12 132 2017 20 | Output: 2 1 1 2 99999999 3 0 1 9 | Hard | 6 | 1,499 | 647 | 163 | 16 |
1,872 | F | 1872F | F. Selling a Menagerie | 1,800 | dfs and similar; dsu; graphs; implementation; math | You are the owner of a menagerie consisting of \(n\) animals numbered from \(1\) to \(n\). However, maintaining the menagerie is quite expensive, so you have decided to sell it!It is known that each animal is afraid of exactly one other animal. More precisely, animal \(i\) is afraid of animal \(a_i\) (\(a_i \neq i\)). ... | The first line of the input contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Then follow the descriptions of the test cases.The first line of each test case description contains an integer \(n\) (\(2 \le n \le 10^5\)) β the number of animals.The second line of the test case description contai... | Output \(t\) lines, each containing the answer to the corresponding test case. The answer should be \(n\) integers β the permutation \(p_1, p_2, \ldots, p_n\), indicating in which order to sell the animals in order to maximize the profit. If there are multiple possible answers, you can output any of them. | Input: 832 3 26 6 182 1 4 3 6 5 8 71 2 1 2 2 1 2 152 1 1 1 19 8 1 1 122 11000000000 99999999972 3 2 6 4 4 31 2 3 4 5 6 753 4 4 1 33 4 5 6 732 1 11 2 242 1 4 11 1 1 1 | Output: 1 2 3 2 4 5 1 6 3 7 8 3 4 5 1 2 1 2 7 5 1 3 2 6 4 5 3 2 4 1 3 2 1 3 4 1 2 | Medium | 5 | 2,372 | 701 | 306 | 18 | |
605 | C | 605C | C. Freelancer's Dreams | 2,400 | geometry | Mikhail the Freelancer dreams of two things: to become a cool programmer and to buy a flat in Moscow. To become a cool programmer, he needs at least p experience points, and a desired flat in Moscow costs q dollars. Mikhail is determined to follow his dreams and registered at a freelance site.He has suggestions to work... | The first line of the input contains three integers n, p and q (1 β€ n β€ 100 000, 1 β€ p, q β€ 1 000 000) β the number of projects and the required number of experience and money.Each of the next n lines contains two integers ai and bi (1 β€ ai, bi β€ 1 000 000) β the daily increase in experience and daily income for workin... | Print a real value β the minimum number of days Mikhail needs to get the required amount of experience and money. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will conside... | First sample corresponds to the example in the problem statement. | Input: 3 20 206 21 32 6 | Output: 5.000000000000000 | Expert | 1 | 1,274 | 342 | 347 | 6 |
2,069 | F | 2069F | F. Graph Inclusion | 2,800 | data structures; dfs and similar; divide and conquer; dsu; graphs | A connected component of an undirected graph is defined as a set of vertices \(S\) of this graph such that: for every pair of vertices \((u, v)\) in \(S\), there exists a path between vertices \(u\) and \(v\); there is no vertex outside \(S\) that has a path to a vertex within \(S\). For example, the graph in the pictu... | The first line contains two integers \(n\) and \(q\) (\(2 \le n \le 4 \cdot 10^5\); \(1 \le q \le 4 \cdot 10^5\)) β the number of vertices and queries, respectively.Next, there are \(q\) lines, where the \(i\)-th line describes the \(i\)-th query. The description of the query begins with the character \(c_i\) (either A... | For each query, print one integer β the minimum number of edges that you have to add to the graph \(A\) so that it includes \(B\). | Input: 6 9A 2 3B 1 3A 2 1A 3 2B 5 6A 6 5A 3 4A 4 2A 4 3 | Output: 0 1 0 1 2 1 1 0 1 | Master | 5 | 1,008 | 597 | 130 | 20 | |
1,572 | E | 1572E | E. Polygon | 3,000 | binary search; dp; geometry | You are given a strictly convex polygon with \(n\) vertices.You will make \(k\) cuts that meet the following conditions: each cut is a segment that connects two different nonadjacent vertices; two cuts can intersect only at vertices of the polygon. Your task is to maximize the area of the smallest region that will be f... | The first line contains two integers \(n\), \(k\) (\(3 \le n \le 200\), \(0 \le k \le n-3\)).The following \(n\) lines describe vertices of the polygon in anticlockwise direction. The \(i\)-th line contains two integers \(x_i\), \(y_i\) (\(|x_i|, |y_i| \le 10^8\)) β the coordinates of the \(i\)-th vertex.It is guarante... | Print one integer: the maximum possible area of the smallest region after making \(k\) cuts multiplied by \(2\). | In the first example, it's optimal to make cuts between the following pairs of vertices: \((-2, -4)\) and \((4, 2)\), \((-2, -4)\) and \((1, 5)\), \((-5, -1)\) and \((1, 5)\), \((-5, 0)\) and \((0, 5)\). Points \((-5, -1)\), \((1, 5)\), \((0, 5)\), \((-5, 0)\) determine the smallest region with double area of \(11\). I... | Input: 8 4 -2 -4 2 -2 4 2 1 5 0 5 -4 4 -5 0 -5 -1 | Output: 11 | Master | 3 | 362 | 394 | 112 | 15 |
1,167 | E | 1167E | E. Range Deleting | 2,100 | binary search; combinatorics; data structures; two pointers | You are given an array consisting of \(n\) integers \(a_1, a_2, \dots , a_n\) and an integer \(x\). It is guaranteed that for every \(i\), \(1 \le a_i \le x\).Let's denote a function \(f(l, r)\) which erases all values such that \(l \le a_i \le r\) from the array \(a\) and returns the resulting array. For example, if \... | The first line contains two integers \(n\) and \(x\) (\(1 \le n, x \le 10^6\)) β the length of array \(a\) and the upper limit for its elements, respectively.The second line contains \(n\) integers \(a_1, a_2, \dots a_n\) (\(1 \le a_i \le x\)). | Print the number of pairs \(1 \le l \le r \le x\) such that \(f(l, r)\) is sorted in non-descending order. | In the first test case correct pairs are \((1, 1)\), \((1, 2)\), \((1, 3)\) and \((2, 3)\).In the second test case correct pairs are \((1, 3)\), \((1, 4)\), \((2, 3)\), \((2, 4)\), \((3, 3)\) and \((3, 4)\). | Input: 3 3 2 3 1 | Output: 4 | Hard | 4 | 576 | 244 | 106 | 11 |
1,067 | B | 1067B | B. Multihedgehog | 1,800 | dfs and similar; graphs; shortest paths | Someone give a strange birthday present to Ivan. It is hedgehog β connected undirected graph in which one vertex has degree at least \(3\) (we will call it center) and all other vertices has degree 1. Ivan thought that hedgehog is too boring and decided to make himself \(k\)-multihedgehog.Let us define \(k\)-multihedge... | First line of input contains \(2\) integers \(n\), \(k\) (\(1 \le n \le 10^{5}\), \(1 \le k \le 10^{9}\)) β number of vertices and hedgehog parameter.Next \(n-1\) lines contains two integers \(u\) \(v\) (\(1 \le u, \,\, v \le n; \,\, u \ne v\)) β indices of vertices connected by edge.It is guaranteed that given graph i... | Print ""Yes"" (without quotes), if given graph is \(k\)-multihedgehog, and ""No"" (without quotes) otherwise. | 2-multihedgehog from the first example looks like this:Its center is vertex \(13\). Hedgehogs created on last step are: [4 (center), 1, 2, 3], [6 (center), 7, 8, 9], [5 (center), 10, 11, 12, 13].Tree from second example is not a hedgehog because degree of center should be at least \(3\). | Input: 14 21 42 43 44 1310 511 512 514 55 136 78 613 69 6 | Output: Yes | Medium | 3 | 1,009 | 329 | 109 | 10 |
1,572 | F | 1572F | F. Stations | 3,400 | data structures | There are \(n\) cities in a row numbered from \(1\) to \(n\).The cities will be building broadcasting stations. The station in the \(i\)-th city has height \(h_i\) and range \(w_i\). It can broadcast information to city \(j\) if the following constraints are met: \(i \le j \le w_i\), and for each \(k\) such that \(i < ... | The first line contains two integers \(n\) and \(q\) (\(1 \le n, q \le 2\cdot10^5\)) β number of cities and number of events.Then \(q\) lines follow. The \(i\)-th line begins with an integer \(p_i\) (\(p_i = 1\) or \(p_i = 2\)).If \(p_i = 1\) a station will be rebuilt. Then two integers \(c_i\) and \(g_i\) (\(1 \le c_i... | For each query, print in a single line the sum of \(b_j\) over the given interval. | In the first test case, only station \(1\) reaches city \(1\) before and after it is rebuilt.In the second test case, after each rebuild, the array \(b\) looks as follows: \([1, 1, 1, 2, 1]\); \([1, 2, 2, 3, 2]\); \([1, 2, 2, 1, 2]\); \([1, 1, 2, 1, 2]\); \([1, 1, 2, 1, 1]\). | Input: 1 3 2 1 1 1 1 1 2 1 1 | Output: 1 1 | Master | 1 | 1,131 | 567 | 82 | 15 |
1,454 | A | 1454A | A. Special Permutation | 800 | constructive algorithms; probabilities | You are given one integer \(n\) (\(n > 1\)).Recall that a permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2, 3, 1, 5, 4]\) is a permutation of length \(5\), but \([1, 2, 2]\) is not a permutation (\(2\) appears twice in the array) and... | The first line of the input contains one integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. Then \(t\) test cases follow.The only line of the test case contains one integer \(n\) (\(2 \le n \le 100\)) β the length of the permutation you have to find. | For each test case, print \(n\) distinct integers \(p_1, p_2, \ldots, p_n\) β a permutation that there is no index \(i\) (\(1 \le i \le n\)) such that \(p_i = i\) (so, for all \(i\) from \(1\) to \(n\) the condition \(p_i \ne i\) should be satisfied).If there are several answers, you can print any. It can be proven tha... | Input: 2 2 5 | Output: 2 1 2 1 5 3 4 | Beginner | 2 | 777 | 263 | 359 | 14 | |
986 | F | 986F | F. Oppa Funcan Style Remastered | 3,300 | graphs; math; number theory; shortest paths | Surely you have seen insane videos by South Korean rapper PSY, such as ""Gangnam Style"", ""Gentleman"" and ""Daddy"". You might also hear that PSY has been recording video ""Oppa Funcan Style"" two years ago (unfortunately we couldn't find it on the internet). We will remind you what this hit looked like (you can find... | In the first line of input there is one integer \(t\) (\(1 \le t \le 10^{4}\)) β the number of options for \(n\) and \(k\) to check.In the next \(t\) lines the options are given: each option is described with two integers \(n\) and \(k\) (\(1 \le n \le 10^{18}\), \(1 \le k \le 10^{15}\)) β the number of dancers and the... | Print \(t\) lines. If the \(i\)-th option of the video is feasible, print ""YES"" (without quotes) in \(i\)-th line, otherwise print ""NO"" (without quotes). | Input: 37 73 85 6 | Output: YESNOYES | Master | 4 | 1,813 | 442 | 157 | 9 | |
2,040 | B | 2040B | B. Paint a Strip | 1,000 | constructive algorithms; greedy; math | You have an array of zeros \(a_1, a_2, \ldots, a_n\) of length \(n\).You can perform two types of operations on it: Choose an index \(i\) such that \(1 \le i \le n\) and \(a_i = 0\), and assign \(1\) to \(a_i\); Choose a pair of indices \(l\) and \(r\) such that \(1 \le l \le r \le n\), \(a_l = 1\), \(a_r = 1\), \(a_l ... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The only line of each test case contains one integer \(n\) (\(1 \le n \le 10^5\)) β the length of the array.Note that there is no limit on the sum of \(n\) ove... | For each test case, print one integer β the minimum number of needed operations of first type. | In the first test case, you can perform an operation of the \(1\)st type with \(i = 1\).In the second test case, you can perform the following sequence of operations: Operation of \(1\)st type, \(i = 1\). After performing this operation, the array will look like this: \([1, 0]\). Operation of \(1\)st type, \(i = 2\). A... | Input: 412420 | Output: 1 2 2 4 | Beginner | 3 | 542 | 337 | 94 | 20 |
977 | D | 977D | D. Divide by three, multiply by two | 1,400 | dfs and similar; math; sortings | Polycarp likes to play with numbers. He takes some integer number \(x\), writes it down on the board, and then performs with it \(n - 1\) operations of the two kinds: divide the number \(x\) by \(3\) (\(x\) must be divisible by \(3\)); multiply the number \(x\) by \(2\). After each operation, Polycarp writes down the r... | The first line of the input contatins an integer number \(n\) (\(2 \le n \le 100\)) β the number of the elements in the sequence. The second line of the input contains \(n\) integer numbers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 3 \cdot 10^{18}\)) β rearranged (reordered) sequence that Polycarp can wrote down on the... | Print \(n\) integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.It is guaranteed that the answer exists. | In the first example the given sequence can be rearranged in the following way: \([9, 3, 6, 12, 4, 8]\). It can match possible Polycarp's game which started with \(x = 9\). | Input: 64 8 6 3 12 9 | Output: 9 3 6 12 4 8 | Easy | 3 | 964 | 327 | 177 | 9 |
843 | B | 843B | B. Interactive LowerBound | 2,000 | brute force; interactive; probabilities | This is an interactive problem.You are given a sorted in increasing order singly linked list. You should find the minimum integer in the list which is greater than or equal to x.More formally, there is a singly liked list built on an array of n elements. Element with index i contains two integers: valuei is the integer... | The first line contains three integers n, start, x (1 β€ n β€ 50000, 1 β€ start β€ n, 0 β€ x β€ 109) β the number of elements in the list, the index of the first element and the integer x. | To print the answer for the problem, print ! ans, where ans is the minimum integer in the list greater than or equal to x, or -1, if there is no such integer. | You can read more about singly linked list by the following link: https://en.wikipedia.org/wiki/Linked_list#Singly_linked_list The illustration for the first sample case. Start and finish elements are marked dark. | Input: 5 3 8097 -158 516 281 179 4 | Output: ? 1? 2? 3? 4? 5! 81 | Hard | 3 | 967 | 182 | 158 | 8 |
1,986 | B | 1986B | B. Matrix Stabilization | 1,000 | brute force; data structures; greedy; sortings | You are given a matrix of size \(n \times m\), where the rows are numbered from \(1\) to \(n\) from top to bottom, and the columns are numbered from \(1\) to \(m\) from left to right. The element at the intersection of the \(i\)-th row and the \(j\)-th column is denoted by \(a_{ij}\).Consider the algorithm for stabiliz... | Each test consists of multiple sets of input data. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of sets of input data. This is followed by their description.The first line of each set of input data contains two integers \(n\) and \(m\) (\(1 \leq n, m \leq 100, n \cdot m > 1\)) β ... | For each set of input data, output \(n\) lines with \(m\) numbers in each line β the values of the cells of matrix \(a\) after the stabilization algorithm. | In the first set of input data, the algorithm will select the cell \((1, 1)\) twice in a row and then terminate. In the second set of input data, there is no cell whose value is strictly greater than the values of all neighboring cells.In the third set of input data, the algorithm will select the cell \((2, 2)\) and th... | Input: 61 23 12 1112 21 23 42 37 4 51 8 105 492 74 31 7474 92 17 731 17 92 374 7 3 927 31 1 13 31000000000 1 10000000001 1000000000 11000000000 1 1000000000 | Output: 1 1 1 1 1 2 3 3 4 4 5 1 8 8 74 74 31 31 74 74 17 7 31 17 17 3 31 7 3 3 7 7 1 1 1 1 1 1 1 1 1 1 1 | Beginner | 4 | 1,031 | 651 | 155 | 19 |
1,034 | E | 1034E | E. Little C Loves 3 III | 3,200 | bitmasks; dp; math | Little C loves number Β«3Β» very much. He loves all things about it.Now he is interested in the following problem:There are two arrays of \(2^n\) intergers \(a_0,a_1,...,a_{2^n-1}\) and \(b_0,b_1,...,b_{2^n-1}\).The task is for each \(i (0 \leq i \leq 2^n-1)\), to calculate \(c_i=\sum a_j \cdot b_k\) (\(j|k=i\) and \(j\&... | The first line contains one integer \(n (0 \leq n \leq 21)\).The second line contains \(2^n\) integers in \([0,3]\) without spaces β the \(i\)-th of them is \(a_{i-1}\).The third line contains \(2^n\) integers in \([0,3]\) without spaces β the \(i\)-th of them is \(b_{i-1}\). | Print one line contains \(2^n\) integers in \([0,3]\) without spaces β the \(i\)-th of them is \(c_{i-1}\&3\). (It's obvious that \(c_{i}\&3\) is in \([0,3]\)). | Input: 11111 | Output: 12 | Master | 3 | 775 | 276 | 160 | 10 | |
804 | E | 804E | E. The same permutation | 3,100 | constructive algorithms | Seyyed and MoJaK are friends of Sajjad. Sajjad likes a permutation. Seyyed wants to change the permutation in a way that Sajjad won't like it. Seyyed thinks more swaps yield more probability to do that, so he makes MoJaK to perform a swap between every pair of positions (i, j), where i < j, exactly once. MoJaK doesn't ... | The first line contains single integer n (1 β€ n β€ 1000) β the size of the permutation.As the permutation is not important, you can consider ai = i, where the permutation is a1, a2, ..., an. | If it is not possible to swap all pairs of positions so that the permutation stays the same, print ""NO"",Otherwise print ""YES"", then print lines: the i-th of these lines should contain two integers a and b (a < b) β the positions where the i-th swap is performed. | Input: 3 | Output: NO | Master | 1 | 506 | 189 | 266 | 8 | |
2,131 | G | 2131G | G. Wafu! | 0 | bitmasks; brute force; data structures; dfs and similar; dp; math | To help improve her math, Kudryavka is given a set \(S\) that consists of \(n\) distinct positive integers. Initially, her score is \(1\). She can perform an arbitrary number of the following operations on the set if it is not empty: Let the minimum value of \(S\) be \(m\). Multiply her score by \(m\). Remove \(m\) fro... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 2 \cdot 10^5\), \(1 \le k \le 10^9\)). The second line of each test case... | For each test case, output an integer indicating the answer modulo \(10^9+7\). | Let us simulate the process in the first test case:$$$\( \{1,3\} \xrightarrow{\text{remove}\ 1} \{3\} \xrightarrow[\text{add}\ 1,2]{\text{remove}\ 3} \{1,2\} \xrightarrow{\text{remove}\ 1} \{2\} \)\(The removed values are \)1\(, \)3\( and \)1\( respectively, so her score is \)1\times 3\times 1 = 3\(.In the second test ... | Input: 42 31 33 65 1 42 1002 1005 151 2 3 4 5 | Output: 3 24 118143737 576 | Beginner | 6 | 631 | 644 | 78 | 21 |
547 | B | 547B | B. Mike and Feet | 1,900 | binary search; data structures; dp; dsu | Mike is the president of country What-The-Fatherland. There are n bears living in this country besides Mike. All of them are standing in a line and they are numbered from 1 to n from left to right. i-th bear is exactly ai feet high. A group of bears is a non-empty contiguous segment of the line. The size of a group is ... | The first line of input contains integer n (1 β€ n β€ 2 Γ 105), the number of bears.The second line contains n integers separated by space, a1, a2, ..., an (1 β€ ai β€ 109), heights of bears. | Print n integers in one line. For each x from 1 to n, print the maximum strength among all groups of size x. | Input: 101 2 3 4 5 4 3 2 1 6 | Output: 6 4 4 3 3 2 2 1 1 1 | Hard | 4 | 532 | 187 | 108 | 5 | |
1,196 | A | 1196A | A. Three Piles of Candies | 800 | brute force; constructive algorithms; math | Alice and Bob have received three big piles of candies as a gift. Now they want to divide these candies as fair as possible. To do this, Alice takes one pile of candies, then Bob takes one of the other two piles. The last pile is split between Alice and Bob as they want: for example, it is possible that Alice takes the... | The first line of the input contains one integer \(q\) (\(1 \le q \le 1000\)) β the number of queries. Then \(q\) queries follow.The only line of the query contains three integers \(a, b\) and \(c\) (\(1 \le a, b, c \le 10^{16}\)) β the number of candies in the first, second and third piles correspondingly. | Print \(q\) lines. The \(i\)-th line should contain the answer for the \(i\)-th query β the maximum number of candies Alice can have after the division, if both Alice and Bob act optimally (of course, Bob will have the same number of candies). | Input: 4 1 3 4 1 10 100 10000000000000000 10000000000000000 10000000000000000 23 34 45 | Output: 4 55 15000000000000000 51 | Beginner | 3 | 1,498 | 308 | 243 | 11 | |
1,157 | C2 | 1157C2 | C2. Increasing Subsequence (hard version) | 1,700 | greedy | The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).You are given a sequence \(a\) consisting of \(n\) integers.You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or... | The first line of the input contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of elements in \(a\).The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 2 \cdot 10^5\)), where \(a_i\) is the \(i\)-th element of \(a\). | In the first line of the output print \(k\) β the maximum number of elements in a strictly increasing sequence you can obtain.In the second line print a string \(s\) of length \(k\), where the \(j\)-th character of this string \(s_j\) should be 'L' if you take the leftmost element during the \(j\)-th move and 'R' other... | The first example is described in the problem statement. | Input: 5 1 2 4 3 2 | Output: 4 LRRR | Medium | 1 | 963 | 279 | 375 | 11 |
2,084 | D | 2084D | D. Arcology On Permafrost | 1,600 | binary search; brute force; constructive algorithms; greedy; math | You are given three integers \(n\), \(m\), and \(k\), where \(m \cdot k < n\).For a sequence \(b\) consisting of non-negative integers, define \(f(b)\) as follows: You may perform the following operation on \(b\): Let \(l\) denote the current length of \(b\). Choose a positive integer \(1 \leq i \leq l - k + 1\), remov... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains three integers \(n\), \(m\), and \(k\) (\(2 \le n \le 2 \cdot 10^5\), \(1 \le m < n\), \(1 \le k < n\), \(1 \le m \c... | For each test case, output \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 10^9\)).If there are multiple answers, print any of them. | In the first test case, it can be shown that \(f(a) = 1\), which is maximized.In the second test case, it can be shown that \(f(a) = 1\), which is maximized. \(f(a) = 1\) since you can perform the following operations: Choose \(i = 3\), remove the subarray from index \(3\) to \(4\) and concatenate the remaining parts. ... | Input: 82 1 15 2 26 1 48 2 28 1 511 3 322 6 317 2 2 | Output: 0 0 0 1 0 0 0 0 1 2 2 0 1 0 2 1 0 1 0 8 1 0 1 2 1000000000 1 0 1 2 1 0 0 1 0 2 1 0 2 1 0 0 2 1 0 2 1 0 3 2 1 0 2 1 0 2 1 0 2 1 0 2 1 4 0 2 1 3 4 0 2 1 0 3 4 0 1 2 1 3 | Medium | 5 | 1,092 | 425 | 142 | 20 |
967 | B | 967B | B. Watering System | 1,000 | math; sortings | Arkady wants to water his only flower. Unfortunately, he has a very poor watering system that was designed for \(n\) flowers and so it looks like a pipe with \(n\) holes. Arkady can only use the water that flows from the first hole.Arkady can block some of the holes, and then pour \(A\) liters of water into the pipe. A... | The first line contains three integers \(n\), \(A\), \(B\) (\(1 \le n \le 100\,000\), \(1 \le B \le A \le 10^4\)) β the number of holes, the volume of water Arkady will pour into the system, and the volume he wants to get out of the first hole.The second line contains \(n\) integers \(s_1, s_2, \ldots, s_n\) (\(1 \le s... | Print a single integer β the number of holes Arkady should block. | In the first example Arkady should block at least one hole. After that, \(\frac{10 \cdot 2}{6} \approx 3.333\) liters of water will flow out of the first hole, and that suits Arkady.In the second example even without blocking any hole, \(\frac{80 \cdot 3}{10} = 24\) liters will flow out of the first hole, that is not l... | Input: 4 10 32 2 2 2 | Output: 1 | Beginner | 2 | 728 | 360 | 65 | 9 |
999 | F | 999F | F. Cards and Joy | 2,000 | dp | There are \(n\) players sitting at the card table. Each player has a favorite number. The favorite number of the \(j\)-th player is \(f_j\).There are \(k \cdot n\) cards on the table. Each card contains a single integer: the \(i\)-th card contains number \(c_i\). Also, you are given a sequence \(h_1, h_2, \dots, h_k\).... | The first line of input contains two integers \(n\) and \(k\) (\(1 \le n \le 500, 1 \le k \le 10\)) β the number of players and the number of cards each player will get.The second line contains \(k \cdot n\) integers \(c_1, c_2, \dots, c_{k \cdot n}\) (\(1 \le c_i \le 10^5\)) β the numbers written on the cards.The thir... | Print one integer β the maximum possible total joy levels of the players among all possible card distributions. | In the first example, one possible optimal card distribution is the following: Player \(1\) gets cards with numbers \([1, 3, 8]\); Player \(2\) gets cards with numbers \([2, 2, 8]\); Player \(3\) gets cards with numbers \([2, 2, 8]\); Player \(4\) gets cards with numbers \([5, 5, 5]\). Thus, the answer is \(2 + 6 + 6 +... | Input: 4 31 3 2 8 5 5 8 2 2 8 5 21 2 2 52 6 7 | Output: 21 | Hard | 1 | 946 | 734 | 111 | 9 |
394 | E | 394E | E. Lightbulb for Minister | 0 | geometry | The Minister for education is coming! Naturally, nobody wants to perform poorly in front of such a honored guest. However, two hours before the arrival it turned out that one of the classes has a malfunctioning lightbulb β for some reason it doesn't get enough energy. The solution was found quickly: all we've got to do... | The first line contains integer n (2 β€ n β€ 105) β the number of generators. Each of the next n lines contains a pair of integers xi, yi, representing the coordinates of the i-th generator in the plane of the class ceiling. It's guaranteed that no two generators have the same location.The next line contains integer m (3... | Print a single real number β the minimum value of the sum of squares of distances from the generators to the point of the lightbulb's optimal position. The answer will be considered valid if its absolute or relative error doesn't exceed 10 - 4. | We'll define a strictly convex polygon as a convex polygon with the following property: no three vertices of the polygon lie on the same line. | Input: 43 23 45 45 243 34 45 34 2 | Output: 8.00000000 | Beginner | 1 | 1,072 | 683 | 244 | 3 |
765 | G | 765G | G. Math, math everywhere | 3,200 | brute force; dp; math; meet-in-the-middle; number theory | If you have gone that far, you'll probably skip unnecessary legends anyway...You are given a binary string and an integer . Find the number of integers k, 0 β€ k < N, such that for all i = 0, 1, ..., m - 1 Print the answer modulo 109 + 7. | In the first line of input there is a string s consisting of 0's and 1's (1 β€ |s| β€ 40).In the next line of input there is an integer n (1 β€ n β€ 5Β·105).Each of the next n lines contains two space-separated integers pi, Ξ±i (1 β€ pi, Ξ±i β€ 109, pi is prime). All pi are distinct. | A single integer β the answer to the problem. | Input: 122 13 1 | Output: 2 | Master | 5 | 237 | 275 | 45 | 7 | |
1,740 | A | 1740A | A. Factorise N+M | 800 | constructive algorithms; number theory | Pak Chanek has a prime number\(^\dagger\) \(n\). Find a prime number \(m\) such that \(n + m\) is not prime.\(^\dagger\) A prime number is a number with exactly \(2\) factors. The first few prime numbers are \(2,3,5,7,11,13,\ldots\). In particular, \(1\) is not a prime number. | Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. The following lines contain the description of each test case.The only line of each test case contains a prime number \(n\) (\(2 \leq n \leq 10^5\)). | For each test case, output a line containing a prime number \(m\) (\(2 \leq m \leq 10^5\)) such that \(n + m\) is not prime. It can be proven that under the constraints of the problem, such \(m\) always exists.If there are multiple solutions, you can output any of them. | In the first test case, \(m = 2\), which is prime, and \(n + m = 7 + 2 = 9\), which is not prime.In the second test case, \(m = 7\), which is prime, and \(n + m = 2 + 7 = 9\), which is not prime.In the third test case, \(m = 47837\), which is prime, and \(n + m = 75619 + 47837 = 123456\), which is not prime. | Input: 37275619 | Output: 2 7 47837 | Beginner | 2 | 277 | 283 | 270 | 17 |
1,933 | C | 1933C | C. Turtle Fingers: Count the Values of k | 1,100 | brute force; implementation; math; number theory | You are given three positive integers \(a\), \(b\) and \(l\) (\(a,b,l>0\)). It can be shown that there always exists a way to choose non-negative (i.e. \(\ge 0\)) integers \(k\), \(x\), and \(y\) such that \(l = k \cdot a^x \cdot b^y\). Your task is to find the number of distinct possible values of \(k\) across all suc... | The first line contains the integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The following \(t\) lines contain three integers, \(a\), \(b\) and \(l\) (\(2 \le a, b \le 100\), \(1 \le l \le 10^6\)) β description of a test case. | Output \(t\) lines, with the \(i\)-th (\(1 \le i \le t\)) line containing an integer, the answer to the \(i\)-th test case. | In the first test case, \(a=2, b=5, l=20\). The possible values of \(k\) (and corresponding \(x,y\)) are as follows: Choose \(k = 1, x = 2, y = 1\). Then \(k \cdot a^x \cdot b^y = 1 \cdot 2^2 \cdot 5^1 = 20 = l\). Choose \(k = 2, x = 1, y = 1\). Then \(k \cdot a^x \cdot b^y = 2 \cdot 2^1 \cdot 5^1 = 20 = l\). Choose \(... | Input: 112 5 202 5 214 6 482 3 723 5 752 2 10243 7 83349100 100 10000007 3 22 6 617 3 632043 | Output: 6 1 5 12 6 11 24 4 1 3 24 | Easy | 4 | 327 | 241 | 123 | 19 |
2,064 | E | 2064E | E. Mycraft Sand Sort | 2,400 | combinatorics; data structures; dsu; greedy; math; sortings | Steve has a permutation\(^{\text{β}}\) \(p\) and an array \(c\), both of length \(n\). Steve wishes to sort the permutation \(p\). Steve has an infinite supply of coloured sand blocks, and using them he discovered a physics-based way to sort an array of numbers called gravity sort. Namely, to perform gravity sort on \(... | The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each testcase contains an integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the lengths of the arrays.The second line of each testcase contains \(n\) distinct integers \(p_1,p_2,\ldots,p_n\) (\(1 \le p_i \le n\)) β... | For each testcase, output the number of pairs of arrays \((p', c')\) Steve could have started with, modulo \(998\,244\,353\). | The second test case is shown below. Gravity sort of the second testcase. It can be shown that permutations of \(p\) will yield the same result, and that \(c\) must equal \([1,1,1,1,1]\) (since all sand must be the same color), so the answer is \(5! = 120\).The third test case is shown in the statement above. It can be... | Input: 411155 3 4 1 21 1 1 1 154 2 3 1 52 1 4 1 54029 15 20 35 37 31 27 1 32 36 38 25 22 8 16 7 3 28 11 12 23 4 14 9 39 13 10 30 6 2 24 17 19 5 34 18 33 26 40 213 1 2 2 1 2 3 1 1 1 1 2 1 3 1 1 3 1 1 1 2 2 1 3 3 3 2 3 2 2 2 2 1 3 2 1 1 2 2 2 | Output: 1 120 1 143654893 | Expert | 6 | 1,501 | 526 | 125 | 20 |
70 | C | 70C | C. Lucky Tickets | 2,200 | binary search; data structures; sortings; two pointers | In Walrusland public transport tickets are characterized by two integers: by the number of the series and by the number of the ticket in the series. Let the series number be represented by a and the ticket number β by b, then a ticket is described by the ordered pair of numbers (a, b). The walruses believe that a ticke... | The first line contains three integers maxx, maxy, w (1 β€ maxx, maxy β€ 105, 1 β€ w β€ 107). | Print on a single line two space-separated numbers, the x and the y. If there are several possible variants, print any of them. If such x and y do not exist, print a single number - 1. | Input: 2 2 1 | Output: 1 1 | Hard | 4 | 932 | 89 | 184 | 0 | |
332 | B | 332B | B. Maximum Absurdity | 1,500 | data structures; dp; implementation | Reforms continue entering Berland. For example, during yesterday sitting the Berland Parliament approved as much as n laws (each law has been assigned a unique number from 1 to n). Today all these laws were put on the table of the President of Berland, G.W. Boosch, to be signed.This time mr. Boosch plans to sign 2k law... | The first line contains two integers n and k (2 β€ n β€ 2Β·105, 0 < 2k β€ n) β the number of laws accepted by the parliament and the length of one segment in the law list, correspondingly. The next line contains n integers x1, x2, ..., xn β the absurdity of each law (1 β€ xi β€ 109). | Print two integers a, b β the beginning of segments that mr. Boosch should choose. That means that the president signs laws with numbers from segments [a; a + k - 1] and [b; b + k - 1]. If there are multiple solutions, print the one with the minimum number a. If there still are multiple solutions, print the one with th... | In the first sample mr. Boosch signs laws with numbers from segments [1;2] and [4;5]. The total absurdity of the signed laws equals 3 + 6 + 1 + 6 = 16.In the second sample mr. Boosch signs laws with numbers from segments [1;2] and [3;4]. The total absurdity of the signed laws equals 1 + 1 + 1 + 1 = 4. | Input: 5 23 6 1 1 6 | Output: 1 4 | Medium | 3 | 1,109 | 278 | 332 | 3 |
1,918 | F | 1918F | F. Caterpillar on a Tree | 2,500 | dfs and similar; graphs; greedy; implementation; sortings; trees | The caterpillar decided to visit every node of the tree. Initially, it is sitting at the root.The tree is represented as a rooted tree with the root at the node \(1\). Each crawl to a neighboring node takes \(1\) minute for the caterpillar. And there is a trampoline under the tree. If the caterpillar detaches from the ... | The first line of the input contains two integers: \(n\) (\(2 \le n \le 2\cdot 10^5\)) β the number of nodes in the tree, and \(k\) (\(0 \le k \le 10^9\)) β the maximum number of teleports to the root.The second line contains \(n-1\) integers \(p_2\), \(p_3\), ..., \(p_n\) (\(1 \le p_i \le n\)) β the ancestors in the t... | Print a single number in a single line β the minimum number of minutes required to visit all the nodes of the tree. | The figure shows one of the ways to traverse the tree from the first test in 9 minutes. | Input: 8 1 1 1 2 2 4 3 3 | Output: 9 | Expert | 6 | 1,011 | 378 | 115 | 19 |
388 | A | 388A | A. Fox and Box Accumulation | 1,400 | greedy; sortings | Fox Ciel has n boxes in her room. They have the same size and weight, but they might have different strength. The i-th box can hold at most xi boxes on its top (we'll call xi the strength of the box). Since all the boxes have the same size, Ciel cannot put more than one box directly on the top of some box. For example,... | The first line contains an integer n (1 β€ n β€ 100). The next line contains n integers x1, x2, ..., xn (0 β€ xi β€ 100). | Output a single integer β the minimal possible number of piles. | In example 1, one optimal way is to build 2 piles: the first pile contains boxes 1 and 3 (from top to bottom), the second pile contains only box 2.In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). | Input: 30 0 10 | Output: 2 | Easy | 2 | 940 | 117 | 63 | 3 |
1,446 | F | 1446F | F. Line Distance | 3,200 | binary search; data structures; geometry | You are given an integer \(k\) and \(n\) distinct points with integer coordinates on the Euclidean plane, the \(i\)-th point has coordinates \((x_i, y_i)\).Consider a list of all the \(\frac{n(n - 1)}{2}\) pairs of points \(((x_i, y_i), (x_j, y_j))\) (\(1 \le i < j \le n\)). For every such pair, write out the distance ... | The first line contains two integers \(n\), \(k\) (\(2 \le n \le 10^5\), \(1 \le k \le \frac{n(n - 1)}{2}\)).The \(i\)-th of the next \(n\) lines contains two integers \(x_i\) and \(y_i\) (\(-10^4 \le x_i, y_i \le 10^4\)) β the coordinates of the \(i\)-th point. It is guaranteed that all given points are pairwise disti... | You should output one number β the \(k\)-th smallest distance from the origin. Your answer is considered correct if its absolute or relative error does not exceed \(10^{-6}\).Formally, let your answer be \(a\), and the jury's answer be \(b\). Your answer is accepted if and only if \(\frac{|a - b|}{\max{(1, |b|)}} \le 1... | There are \(6\) pairs of points: Line \(1-2\) : distance \(0\) from the origin Line \(1-3\) : distance \(\frac{\sqrt{2}}{2} \approx 0.707106781\) from the origin Line \(1-4\) : distance \(2\) from the origin Line \(2-3\) : distance \(1\) from the origin Line \(2-4\) : distance \(2\) from the origin Line \(3-4\) : dista... | Input: 4 3 2 1 -2 -1 0 -1 -2 4 | Output: 0.707106780737 | Master | 3 | 461 | 324 | 329 | 14 |
1,809 | C | 1809C | C. Sum on Subarrays | 1,500 | constructive algorithms; greedy; math | For an array \(a = [a_1, a_2, \dots, a_n]\), let's denote its subarray \(a[l, r]\) as the array \([a_l, a_{l+1}, \dots, a_r]\).For example, the array \(a = [1, -3, 1]\) has \(6\) non-empty subarrays: \(a[1,1] = [1]\); \(a[1,2] = [1,-3]\); \(a[1,3] = [1,-3,1]\); \(a[2,2] = [-3]\); \(a[2,3] = [-3,1]\); \(a[3,3] = [1]\). ... | The first line contains one integer \(t\) (\(1 \le t \le 5000\)) β the number of test cases.Each test case consists of one line containing two integers \(n\) and \(k\) (\(2 \le n \le 30\); \(0 \le k \le \dfrac{(n+1) \cdot n}{2}\)). | For each test case, print \(n\) integers β the elements of the array meeting the constraints. It can be shown that the answer always exists. If there are multiple answers, print any of them. | Input: 43 22 02 24 6 | Output: 1 -3 1 -13 -42 -13 42 -3 -4 10 -2 | Medium | 3 | 615 | 231 | 190 | 18 | |
404 | C | 404C | C. Restore Graph | 1,800 | dfs and similar; graphs; sortings | Valera had an undirected connected graph without self-loops and multiple edges consisting of n vertices. The graph had an interesting property: there were at most k edges adjacent to each of its vertices. For convenience, we will assume that the graph vertices were indexed by integers from 1 to n.One day Valera counted... | The first line contains two space-separated integers n and k (1 β€ k < n β€ 105). Number n shows the number of vertices in the original graph. Number k shows that at most k edges were adjacent to each vertex in the original graph.The second line contains space-separated integers d[1], d[2], ..., d[n] (0 β€ d[i] < n). Numb... | If Valera made a mistake in his notes and the required graph doesn't exist, print in the first line number -1. Otherwise, in the first line print integer m (0 β€ m β€ 106) β the number of edges in the found graph.In each of the next m lines print two space-separated integers ai and bi (1 β€ ai, bi β€ n; ai β bi), denoting ... | Input: 3 20 1 1 | Output: 31 21 33 2 | Medium | 3 | 676 | 408 | 493 | 4 | |
1,201 | A | 1201A | A. Important Exam | 900 | implementation; strings | A class of students wrote a multiple-choice test.There are \(n\) students in the class. The test had \(m\) questions, each of them had \(5\) possible answers (A, B, C, D or E). There is exactly one correct answer for each question. The correct answer for question \(i\) worth \(a_i\) points. Incorrect answers are graded... | The first line contains integers \(n\) and \(m\) (\(1 \le n, m \le 1000\)) β the number of students in the class and the number of questions in the test.Each of the next \(n\) lines contains string \(s_i\) (\(|s_i| = m\)), describing an answer of the \(i\)-th student. The \(j\)-th character represents the student answe... | Print a single integer β the maximum possible total score of the class. | In the first example, one of the most optimal test answers is ""ABCD"", this way the total number of points will be \(16\).In the second example, one of the most optimal test answers is ""CCC"", this way each question will be answered by exactly one student and the total number of points is \(5 + 4 + 12 = 21\). | Input: 2 4 ABCD ABCE 1 2 3 4 | Output: 16 | Beginner | 2 | 563 | 519 | 71 | 12 |
965 | B | 965B | B. Battleship | 1,300 | implementation | Arkady is playing Battleship. The rules of this game aren't really important.There is a field of \(n \times n\) cells. There should be exactly one \(k\)-decker on the field, i. e. a ship that is \(k\) cells long oriented either horizontally or vertically. However, Arkady doesn't know where it is located. For each cell ... | The first line contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 100\)) β the size of the field and the size of the ship.The next \(n\) lines contain the field. Each line contains \(n\) characters, each of which is either '#' (denotes a definitely empty cell) or '.' (denotes a cell that can belong to the ship)... | Output two integers β the row and the column of a cell that belongs to the maximum possible number of different locations of the ship.If there are multiple answers, output any of them. In particular, if no ship can be placed on the field, you can output any cell. | The picture below shows the three possible locations of the ship that contain the cell \((3, 2)\) in the first sample. | Input: 4 3#..##.#......### | Output: 3 2 | Easy | 1 | 534 | 321 | 263 | 9 |
706 | C | 706C | C. Hard problem | 1,600 | dp; strings | Vasiliy is fond of solving different tasks. Today he found one he wasn't able to solve himself, so he asks you to help.Vasiliy is given n strings consisting of lowercase English letters. He wants them to be sorted in lexicographical order (as in the dictionary), but he is not allowed to swap any of them. The only opera... | The first line of the input contains a single integer n (2 β€ n β€ 100 000) β the number of strings.The second line contains n integers ci (0 β€ ci β€ 109), the i-th of them is equal to the amount of energy Vasiliy has to spent in order to reverse the i-th string. Then follow n lines, each containing a string consisting of... | If it is impossible to reverse some of the strings such that they will be located in lexicographical order, print - 1. Otherwise, print the minimum total amount of energy Vasiliy has to spent. | In the second sample one has to reverse string 2 or string 3. To amount of energy required to reverse the string 3 is smaller.In the third sample, both strings do not change after reverse and they go in the wrong order, so the answer is - 1.In the fourth sample, both strings consists of characters 'a' only, but in the ... | Input: 21 2baac | Output: 1 | Medium | 2 | 1,007 | 405 | 192 | 7 |
1,909 | B | 1909B | B. Make Almost Equal With Mod | 1,200 | bitmasks; constructive algorithms; math; number theory | xi - Solar Stormβ You are given an array \(a_1, a_2, \dots, a_n\) of distinct positive integers. You have to do the following operation exactly once: choose a positive integer \(k\); for each \(i\) from \(1\) to \(n\), replace \(a_i\) with \(a_i \text{ mod } k^\dagger\). Find a value of \(k\) such that \(1 \leq k \leq 1... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 500\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 100\)) β the length of the array \(a\).The second line of each test case contain... | For each test case, output a single integer: a value of \(k\) (\(1 \leq k \leq 10^{18}\)) such that the array \(a_1, a_2, \dots, a_n\) contains exactly \(2\) distinct values at the end of the operation. | In the first test case, you can choose \(k = 7\). The array becomes \([8 \text{ mod } 7, 15 \text{ mod } 7, 22 \text{ mod } 7, 30 \text{ mod } 7] = [1, 1, 1, 2]\), which contains exactly \(2\) distinct values (\(\{1, 2\}\)).In the second test case, you can choose \(k = 30\). The array becomes \([0, 0, 8, 0, 8]\), which... | Input: 548 15 22 30560 90 98 120 3086328 769 541 986 215 73451000 2000 7000 11000 1600022 1 | Output: 7 30 3 5000 1000000000000000000 | Easy | 4 | 851 | 551 | 202 | 19 |
507 | E | 507E | E. Breaking Good | 2,100 | dfs and similar; dp; graphs; shortest paths | Breaking Good is a new video game which a lot of gamers want to have. There is a certain level in the game that is really difficult even for experienced gamers.Walter William, the main character of the game, wants to join a gang called Los Hermanos (The Brothers). The gang controls the whole country which consists of n... | The first line of input contains two integers n, m (2 β€ n β€ 105, ), the number of cities and number of roads respectively.In following m lines there are descriptions of roads. Each description consists of three integers x, y, z (1 β€ x, y β€ n, ) meaning that there is a road connecting cities number x and y. If z = 1, th... | In the first line output one integer k, the minimum possible number of roads affected by gang.In the following k lines output three integers describing roads that should be affected. Each line should contain three integers x, y, z (1 β€ x, y β€ n, ), cities connected by a road and the new state of a road. z = 1 indicates... | In the first test the only path is 1 - 2In the second test the only shortest path is 1 - 3 - 4In the third test there are multiple shortest paths but the optimal is 1 - 4 - 6 - 8 | Input: 2 11 2 0 | Output: 11 2 1 | Hard | 4 | 1,868 | 360 | 838 | 5 |
1,505 | A | 1505A | A. Is it rated - 2 | 900 | *special; implementation; interactive | InteractionThis is an interactive problem. You need to read participants' queries from standard input and print your responses to standard output. You don't know the number of queries upfront, so you'll need to process them as you get them; you'll know you're done once you reach the end of the file.In each query, you w... | Input: Is it rated? Is it rated? Is it rated? | Output: NO NO NO | Beginner | 3 | 613 | 0 | 0 | 15 | |||
1,728 | F | 1728F | F. Fishermen | 3,100 | flows; graph matchings; greedy | There are \(n\) fishermen who have just returned from a fishing trip. The \(i\)-th fisherman has caught a fish of size \(a_i\).The fishermen will choose some order in which they are going to tell the size of the fish they caught (the order is just a permutation of size \(n\)). However, they are not entirely honest, and... | The first line contains one integer \(n\) (\(1 \le n \le 1000\)) β the number of fishermen.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^6\)). | Print one integer β the minimum possible value of \(\sum\limits_{i=1}^{n} b_i\) you can obtain by choosing the order of fishermen optimally. | Input: 7 1 8 2 3 2 2 3 | Output: 33 | Master | 3 | 1,848 | 181 | 140 | 17 | |
1,155 | B | 1155B | B. Game with Telephone Numbers | 1,200 | games; greedy; implementation | A telephone number is a sequence of exactly \(11\) digits such that its first digit is 8.Vasya and Petya are playing a game. Initially they have a string \(s\) of length \(n\) (\(n\) is odd) consisting of digits. Vasya makes the first move, then players alternate turns. In one move the player must choose a character an... | The first line contains one integer \(n\) (\(13 \le n < 10^5\), \(n\) is odd) β the length of string \(s\).The second line contains the string \(s\) (\(|s| = n\)) consisting only of decimal digits. | If Vasya has a strategy that guarantees him victory, print YES.Otherwise print NO. | In the first example Vasya needs to erase the second character. Then Petya cannot erase a character from the remaining string 880011223344 so that it does not become a telephone number.In the second example after Vasya's turn Petya can erase one character character 8. The resulting string can't be a telephone number, b... | Input: 13 8380011223344 | Output: YES | Easy | 3 | 735 | 197 | 82 | 11 |
1,500 | A | 1500A | A. Going Home | 1,800 | brute force; hashing; implementation; math | It was the third month of remote learning, Nastya got sick of staying at dormitory, so she decided to return to her hometown. In order to make her trip more entertaining, one of Nastya's friend presented her an integer array \(a\). Several hours after starting her journey home Nastya remembered about the present. To en... | The first line contains the single integer \(n\) (\(4 \leq n \leq 200\,000\)) β the size of the array.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 2.5 \cdot 10^6\)). | Print ""YES"" if there are such four indices, and ""NO"" otherwise.If such indices exist, print these indices \(x\), \(y\), \(z\) and \(w\) (\(1 \le x, y, z, w \le n\)).If there are multiple answers, print any of them. | In the first example \(a_2 + a_3 = 1 + 5 = 2 + 4 = a_1 + a_6\). Note that there are other answer, for example, 2 3 4 6.In the second example, we can't choose four indices. The answer 1 2 2 3 is wrong, because indices should be different, despite that \(a_1 + a_2 = 1 + 3 = 3 + 1 = a_2 + a_3\) | Input: 6 2 1 5 2 7 4 | Output: YES 2 3 1 6 | Medium | 4 | 565 | 205 | 218 | 15 |
1,921 | G | 1921G | G. Mischievous Shooter | 2,200 | brute force; data structures; divide and conquer; dp; implementation | Once the mischievous and wayward shooter named Shel found himself on a rectangular field of size \(n \times m\), divided into unit squares. Each cell either contains a target or not.Shel only had a lucky shotgun with him, with which he can shoot in one of the four directions: right-down, left-down, left-up, or right-up... | Each test consists of several test cases. The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. Then follows the description of the test cases.The first line of each test case contains field dimensions \(n\), \(m\), and the constant for the shotgun's power \(k\) (\(1 \le n, m... | For each test case, output a single integer on a separate line, which is equal to the maximum possible number of hit targets with one shot. | Possible optimal shots for the examples in the statement: | Input: 43 3 1.#.###.#.2 5 3###.....##4 4 2..#####.#..#####2 1 3## | Output: 3 4 5 2 | Hard | 5 | 696 | 657 | 139 | 19 |
1,729 | D | 1729D | D. Friends and the Restaurant | 1,200 | greedy; sortings; two pointers | A group of \(n\) friends decide to go to a restaurant. Each of the friends plans to order meals for \(x_i\) burles and has a total of \(y_i\) burles (\(1 \le i \le n\)). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each... | The first line of the input contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases in the test.The descriptions of the test cases follow.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 10^5\)) β the number of friends.The second line of each test case contains exactly... | For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0. | The first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends (\(1+3+10 \ge 2+3+7\)). Note that they do not have the option of spl... | Input: 668 3 9 2 4 55 3 1 4 5 1041 2 3 41 1 2 232 3 71 3 1062 3 6 9 5 73 2 7 10 6 1065 4 2 1 8 1001 1 1 1 1 20061 4 1 2 4 21 3 3 2 3 4 | Output: 2 0 1 3 1 3 | Easy | 3 | 1,753 | 804 | 149 | 17 |
1,428 | A | 1428A | A. Box is Pull | 800 | math | Wabbit is trying to move a box containing food for the rest of the zoo in the coordinate plane from the point \((x_1,y_1)\) to the point \((x_2,y_2)\).He has a rope, which he can use to pull the box. He can only pull the box if he stands exactly \(1\) unit away from the box in the direction of one of two coordinate axe... | Each test contains multiple test cases. The first line contains a single integer \(t\) \((1 \leq t \leq 1000)\): the number of test cases. The description of the test cases follows.Each of the next \(t\) lines contains four space-separated integers \(x_1, y_1, x_2, y_2\) \((1 \leq x_1, y_1, x_2, y_2 \leq 10^9)\), descr... | For each test case, print a single integer: the minimum time in seconds Wabbit needs to bring the box from \((x_1,y_1)\) to \((x_2,y_2)\). | In the first test case, the starting and the ending points of the box are \((1,2)\) and \((2,2)\) respectively. This is the same as the picture in the statement. Wabbit needs only \(1\) second to move as shown in the picture in the statement.In the second test case, Wabbit can start at the point \((2,1)\). He pulls the... | Input: 2 1 2 2 2 1 1 2 2 | Output: 1 4 | Beginner | 1 | 1,289 | 345 | 138 | 14 |
935 | F | 935F | F. Fafa and Array | 2,600 | data structures; greedy | Fafa has an array A of n positive integers, the function f(A) is defined as . He wants to do q queries of two types: 1 l r x β find the maximum possible value of f(A), if x is to be added to one element in the range [l, r]. You can choose to which element to add x. 2 l r x β increase all the elements in the range [l, r... | The first line contains one integer n (3 β€ n β€ 105) β the length of the array.The second line contains n positive integers a1, a2, ..., an (0 < ai β€ 109) β the array elements.The third line contains an integer q (1 β€ q β€ 105) β the number of queries. Then q lines follow, line i describes the i-th query and contains fou... | For each query of type 1, print the answer to the query. | Input: 51 1 1 1 151 2 4 12 2 3 12 4 4 22 3 4 11 3 3 2 | Output: 28 | Expert | 2 | 394 | 408 | 56 | 9 | |
342 | A | 342A | A. Xenia and Divisors | 1,200 | greedy; implementation | Xenia the mathematician has a sequence consisting of n (n is divisible by 3) positive integers, each of them is at most 7. She wants to split the sequence into groups of three so that for each group of three a, b, c the following conditions held: a < b < c; a divides b, b divides c. Naturally, Xenia wants each element ... | The first line contains integer n (3 β€ n β€ 99999) β the number of elements in the sequence. The next line contains n positive integers, each of them is at most 7.It is guaranteed that n is divisible by 3. | If the required partition exists, print groups of three. Print each group as values of the elements it contains. You should print values in increasing order. Separate the groups and integers in groups by whitespaces. If there are multiple solutions, you can print any of them.If there is no solution, print -1. | Input: 61 1 1 2 2 2 | Output: -1 | Easy | 2 | 519 | 204 | 310 | 3 | |
746 | A | 746A | A. Compote | 800 | implementation; math | Nikolay has a lemons, b apples and c pears. He decided to cook a compote. According to the recipe the fruits should be in the ratio 1: 2: 4. It means that for each lemon in the compote should be exactly 2 apples and exactly 4 pears. You can't crumble up, break up or cut these fruits into pieces. These fruits β lemons, ... | The first line contains the positive integer a (1 β€ a β€ 1000) β the number of lemons Nikolay has. The second line contains the positive integer b (1 β€ b β€ 1000) β the number of apples Nikolay has. The third line contains the positive integer c (1 β€ c β€ 1000) β the number of pears Nikolay has. | Print the maximum total number of lemons, apples and pears from which Nikolay can cook the compote. | In the first example Nikolay can use 1 lemon, 2 apples and 4 pears, so the answer is 1 + 2 + 4 = 7.In the second example Nikolay can use 3 lemons, 6 apples and 12 pears, so the answer is 3 + 6 + 12 = 21.In the third example Nikolay don't have enough pears to cook any compote, so the answer is 0. | Input: 257 | Output: 7 | Beginner | 2 | 575 | 293 | 99 | 7 |
203 | E | 203E | E. Transportation | 2,300 | greedy; sortings; two pointers | Valera came to Japan and bought many robots for his research. He's already at the airport, the plane will fly very soon and Valera urgently needs to bring all robots to the luggage compartment.The robots are self-propelled (they can potentially move on their own), some of them even have compartments to carry other robo... | The first line contains three space-separated integers n, d, S (1 β€ n β€ 105, 1 β€ d, S β€ 109). The first number represents the number of robots, the second one β the distance to the luggage compartment and the third one β the amount of available fuel.Next n lines specify the robots. The i-th line contains three space-se... | Print two space-separated integers β the maximum number of robots Valera can transport to the luggage compartment and the minimum amount of fuel he will need for that. If Valera won't manage to get any robots to the luggage compartment, print two zeroes. | Input: 3 10 100 12 101 6 100 1 1 | Output: 2 6 | Expert | 3 | 1,988 | 608 | 254 | 2 | |
1,977 | B | 1977B | B. Binary Colouring | 1,100 | bitmasks; constructive algorithms; greedy; math | You are given a positive integer \(x\). Find any array of integers \(a_0, a_1, \ldots, a_{n-1}\) for which the following holds: \(1 \le n \le 32\), \(a_i\) is \(1\), \(0\), or \(-1\) for all \(0 \le i \le n - 1\), \(x = \displaystyle{\sum_{i=0}^{n - 1}{a_i \cdot 2^i}}\), There does not exist an index \(0 \le i \le n - ... | Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of the test cases follows.The only line of each test case contains a single positive integer \(x\) (\(1 \le x < 2^{30}\)). | For each test case, output two lines.On the first line, output an integer \(n\) (\(1 \le n \le 32\)) β the length of the array \(a_0, a_1, \ldots, a_{n-1}\).On the second line, output the array \(a_0, a_1, \ldots, a_{n-1}\).If there are multiple valid arrays, you can output any of them. | In the first test case, one valid array is \([1]\), since \((1) \cdot 2^0 = 1\).In the second test case, one possible valid array is \([0,-1,0,0,1]\), since \((0) \cdot 2^0 + (-1) \cdot 2^1 + (0) \cdot 2^2 + (0) \cdot 2^3 + (1) \cdot 2^4 = -2 + 16 = 14\). | Input: 71142415271119 | Output: 1 1 5 0 -1 0 0 1 6 0 0 0 -1 0 1 5 -1 0 0 0 1 6 -1 0 -1 0 0 1 5 -1 0 -1 0 1 5 -1 0 1 0 1 | Easy | 4 | 470 | 285 | 287 | 19 |
1,867 | B | 1867B | B. XOR Palindromes | 1,100 | bitmasks; constructive algorithms; strings | You are given a binary string \(s\) of length \(n\) (a string that consists only of \(0\) and \(1\)). A number \(x\) is good if there exists a binary string \(l\) of length \(n\), containing \(x\) ones, such that if each symbol \(s_i\) is replaced by \(s_i \oplus l_i\) (where \(\oplus\) denotes the bitwise XOR operatio... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^5\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)).The second line of each test case contains a binary string \(s\) of len... | For each test case, output a single line containing a binary string \(t\) of length \(n+1\) - the answer to the problem. | Consider the first example. \(t_2 = 1\) because we can choose \(l = \) 010100, then the string \(s\) becomes 111111, which is a palindrome. \(t_4 = 1\) because we can choose \(l = \) 101011. It can be shown that for all other \(i\), there is no answer, so the remaining symbols are \(0\). | Input: 561010115000009100100011310011 | Output: 0010100 111111 0011111100 0110 11 | Easy | 3 | 660 | 414 | 120 | 18 |
979 | E | 979E | E. Kuro and Topological Parity | 2,400 | dp | Kuro has recently won the ""Most intelligent cat ever"" contest. The three friends then decided to go to Katie's home to celebrate Kuro's winning. After a big meal, they took a small break then started playing games.Kuro challenged Katie to create a game with only a white paper, a pencil, a pair of scissors and a lot o... | The first line contains two integers \(n\) and \(p\) (\(1 \leq n \leq 50\), \(0 \leq p \leq 1\)) β the number of pieces and Kuro's wanted parity.The second line contains \(n\) integers \(c_{1}, c_{2}, ..., c_{n}\) (\(-1 \leq c_{i} \leq 1\)) β the colors of the pieces. | Print a single integer β the number of ways to put the arrows and choose colors so the number of valid paths of alternating colors has the parity of \(p\). | In the first example, there are \(6\) ways to color the pieces and add the arrows, as are shown in the figure below. The scores are \(3, 3, 5\) for the first row and \(5, 3, 3\) for the second row, both from left to right. | Input: 3 1-1 0 1 | Output: 6 | Expert | 1 | 1,965 | 268 | 155 | 9 |
282 | C | 282C | C. XOR and OR | 1,500 | constructive algorithms; implementation; math | The Bitlandians are quite weird people. They do everything differently. They have a different alphabet so they have a different definition for a string.A Bitlandish string is a string made only of characters ""0"" and ""1"".BitHaval (the mayor of Bitland) loves to play with Bitlandish strings. He takes some Bitlandish ... | The first line contains Bitlandish string a, the second line contains Bitlandish string b. The strings can have different lengths.It is guaranteed that the given strings only consist of characters ""0"" and ""1"". The strings are not empty, their length doesn't exceed 106. | Print ""YES"" if a can be transformed into b, otherwise print ""NO"". Please do not print the quotes. | Input: 1110 | Output: YES | Medium | 3 | 1,069 | 273 | 101 | 2 | |
1,819 | F | 1819F | F. Willy-nilly, Crack, Into Release! | 3,500 | data structures; dp | You have long dreamed of working in a large IT company and finally got a job there. You have studied all existing modern technologies for a long time and are ready to apply all your knowledge in practice. But then you sit down at your desk and see a sheet of paper with the company's motto printed in large letters: abcd... | The first line contains two integers \(n\) and \(q\) (\(1 \le n \le 20\), \(1 \le q \le 100\,000\)) β the length of the strings under consideration and the number of queries to modify the set of strings.Each of the next \(q\) lines contains a string \(t_i\) (\(\lvert t_i \rvert = n\)). All strings consist of characters... | For each of the \(q\) queries, output two integers: the minimum and maximum size of a correct sequence of operations in which each word from the set appears at least once.If there is no sequence of operations that satisfies the condition of the problem, output a single number \(-1\). | Let's consider the first test example. After the first query, the set of important words is equal to \(\{\)aa\(\}\), the minimum sequence of actions has the following form: aa, ab, aa. The maximum sequence of actions that fits is aa, ab, ba, bb, bc, cb, cc, cd, dc, dd, da, ad, aa. After the second query, the set of imp... | Input: 2 4 aa ac dd ac | Output: 2 12 4 4 -1 12 12 | Master | 2 | 2,083 | 472 | 284 | 18 |
1,165 | A | 1165A | A. Remainder | 1,100 | implementation; math | You are given a huge decimal number consisting of \(n\) digits. It is guaranteed that this number has no leading zeros. Each digit of this number is either 0 or 1.You may perform several (possibly zero) operations with this number. During each operation you are allowed to change any digit of your number; you may change... | The first line of the input contains three integers \(n, x, y\) (\(0 \le y < x < n \le 2 \cdot 10^5\)) β the length of the number and the integers \(x\) and \(y\), respectively.The second line of the input contains one decimal number consisting of \(n\) digits, each digit of this number is either 0 or 1. It is guarante... | Print one integer β the minimum number of operations you should perform to obtain the number having remainder \(10^y\) modulo \(10^x\). In other words, the obtained number should have remainder \(10^y\) when divided by \(10^x\). | In the first example the number will be \(11010100100\) after performing one operation. It has remainder \(100\) modulo \(100000\).In the second example the number will be \(11010100010\) after performing three operations. It has remainder \(10\) modulo \(100000\). | Input: 11 5 2 11010100101 | Output: 1 | Easy | 2 | 754 | 363 | 228 | 11 |
1,089 | L | 1089L | L. Lazyland | 900 | The kingdom of Lazyland is the home to \(n\) idlers. These idlers are incredibly lazy and create many problems to their ruler, the mighty King of Lazyland. Today \(k\) important jobs for the kingdom (\(k \le n\)) should be performed. Every job should be done by one person and every person can do at most one job. The Ki... | The first line of the input contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 10^5\)) β the number of idlers and the number of jobs.The second line of the input contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le k\)) β the jobs chosen by each idler.The third line of the input contains \(n\) int... | The only line of the output should contain one number β the minimum total time the King needs to spend persuading the idlers to get all the jobs done. | In the first example the optimal plan is to persuade idlers 1, 6, and 8 to do jobs 2, 4, and 6.In the second example each job was chosen by some idler, so there is no need to persuade anyone. | Input: 8 7 1 1 3 1 5 3 7 1 5 7 4 8 1 3 5 2 | Output: 10 | Beginner | 0 | 775 | 443 | 150 | 10 | |
1,475 | F | 1475F | F. Unusual Matrix | 1,900 | 2-sat; brute force; constructive algorithms | You are given two binary square matrices \(a\) and \(b\) of size \(n \times n\). A matrix is called binary if each of its elements is equal to \(0\) or \(1\). You can do the following operations on the matrix \(a\) arbitrary number of times (0 or more): vertical xor. You choose the number \(j\) (\(1 \le j \le n\)) and ... | The first line contains one integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases. Then \(t\) test cases follow.The first line of each test case contains one integer \(n\) (\(1 \leq n \leq 1000\)) β the size of the matrices.The following \(n\) lines contain strings of length \(n\), consisting of the charact... | For each test case, output on a separate line: ""YES"", there is such a sequence of operations that the matrix \(a\) becomes equal to the matrix \(b\); ""NO"" otherwise. You can output ""YES"" and ""NO"" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive). | The first test case is explained in the statements.In the second test case, the following sequence of operations is suitable: horizontal xor, \(i=1\); horizontal xor, \(i=2\); horizontal xor, \(i=3\); It can be proved that there is no sequence of operations in the third test case so that the matrix \(a\) becomes equal ... | Input: 3 3 110 001 110 000 000 000 3 101 010 101 010 101 010 2 01 11 10 10 | Output: YES YES NO | Hard | 3 | 1,302 | 618 | 296 | 14 |
174 | A | 174A | A. Problem About Equation | 1,100 | math | A group of n merry programmers celebrate Robert Floyd's birthday. Polucarpus has got an honourable task of pouring Ber-Cola to everybody. Pouring the same amount of Ber-Cola to everybody is really important. In other words, the drink's volume in each of the n mugs must be the same.Polycarpus has already began the proce... | The first line contains a pair of integers n, b (2 β€ n β€ 100, 1 β€ b β€ 100), where n is the total number of friends in the group and b is the current volume of drink in the bottle. The second line contains a sequence of integers a1, a2, ..., an (0 β€ ai β€ 100), where ai is the current volume of drink in the i-th mug. | Print a single number ""-1"" (without the quotes), if there is no solution. Otherwise, print n float numbers c1, c2, ..., cn, where ci is the volume of the drink to add in the i-th mug. Print the numbers with no less than 6 digits after the decimal point, print each ci on a single line. Polycarpus proved that if a solu... | Input: 5 501 2 3 4 5 | Output: 12.00000011.00000010.0000009.0000008.000000 | Easy | 1 | 924 | 316 | 533 | 1 | |
362 | D | 362D | D. Fools and Foolproof Roads | 2,100 | data structures; dfs and similar; dsu; graphs; greedy | You must have heard all about the Foolland on your Geography lessons. Specifically, you must know that federal structure of this country has been the same for many centuries. The country consists of n cities, some pairs of cities are connected by bidirectional roads, each road is described by its length li.The fools li... | The first line contains four integers n (1 β€ n β€ 105), m (0 β€ m β€ 105), p (0 β€ p β€ 105), q (1 β€ q β€ n) β the number of cities in the Foolland, the number of existing roads, the number of roads that are planned to construct and the required number of regions.Next m lines describe the roads that exist by the moment upgra... | If constructing the roads in the required way is impossible, print a single string ""NO"" (without the quotes). Otherwise, in the first line print word ""YES"" (without the quotes), and in the next p lines print the road construction plan. Each line of the plan must consist of two distinct integers, giving the numbers ... | Consider the first sample. Before the reform the Foolland consists of four regions. The first region includes cities 1, 2, 3, the second region has cities 4 and 6, the third region has cities 5, 7, 8, the fourth region has city 9. The total length of the roads in these cities is 11, 20, 5 and 0, correspondingly. Accord... | Input: 9 6 2 21 2 23 2 14 6 201 3 87 8 35 7 2 | Output: YES9 51 9 | Hard | 5 | 1,599 | 592 | 496 | 3 |
391 | C2 | 391C2 | C2. The Tournament | 0 | greedy | This problem consists of three subproblems: for solving subproblem C1 you will receive 4 points, for solving subproblem C2 you will receive 4 points, and for solving subproblem C3 you will receive 8 points.Manao decided to pursue a fighter's career. He decided to begin with an ongoing tournament. Before Manao joined, t... | The first line contains a pair of integers n and k (1 β€ k β€ n + 1). The i-th of the following n lines contains two integers separated by a single space β pi and ei (0 β€ pi, ei β€ 200000).The problem consists of three subproblems. The subproblems have different constraints on the input. You will get some score for the co... | Print a single number in a single line β the minimum amount of effort Manao needs to use to rank in the top k. If no amount of effort can earn Manao such a rank, output number -1. | Consider the first test case. At the time when Manao joins the tournament, there are three fighters. The first of them has 1 tournament point and the victory against him requires 1 unit of effort. The second contestant also has 1 tournament point, but Manao needs 4 units of effort to defeat him. The third contestant ha... | Input: 3 21 11 42 2 | Output: 3 | Beginner | 1 | 1,413 | 602 | 179 | 3 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.