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,537 | B | 1537B | B. Bad Boy | 900 | constructive algorithms; greedy; math | Riley is a very bad boy, but at the same time, he is a yo-yo master. So, he decided to use his yo-yo skills to annoy his friend Anton.Anton's room can be represented as a grid with \(n\) rows and \(m\) columns. Let \((i, j)\) denote the cell in row \(i\) and column \(j\). Anton is currently standing at position \((i, j... | The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. Then \(t\) test cases follow.The only line of each test case contains four integers \(n\), \(m\), \(i\), \(j\) (\(1 \leq n, m \leq 10^9\), \(1\le i\le n\), \(1\le j\le m\)) β the dimensions of the room, and the cell at w... | For each test case, print four integers \(x_1\), \(y_1\), \(x_2\), \(y_2\) (\(1 \leq x_1, x_2 \leq n\), \(1\le y_1, y_2\le m\)) β the coordinates of where the two yo-yos should be thrown. They will be thrown at coordinates \((x_1,y_1)\) and \((x_2,y_2)\).If there are multiple answers, you may print any. | Here is a visualization of the first test case. | Input: 7 2 3 1 1 4 4 1 2 3 5 2 2 5 1 2 1 3 1 3 1 1 1 1 1 1000000000 1000000000 1000000000 50 | Output: 1 2 2 3 4 1 4 4 3 1 1 5 5 1 1 1 1 1 2 1 1 1 1 1 50 1 1 1000000000 | Beginner | 3 | 1,106 | 353 | 304 | 15 |
1,746 | D | 1746D | D. Paths on the Tree | 1,900 | dfs and similar; dp; greedy; sortings; trees | You are given a rooted tree consisting of \(n\) vertices. The vertices are numbered from \(1\) to \(n\), and the root is the vertex \(1\). You are also given a score array \(s_1, s_2, \ldots, s_n\).A multiset of \(k\) simple paths is called valid if the following two conditions are both true. Each path starts from \(1\... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains two space-separated integers \(n\) (\(2 \le n \le 2 \cdot 10^5\)) and \(k\) (\(1 \le k \le 10^9\... | For each test case, print a single integer β the maximum value of a path multiset. | In the first test case, one of optimal solutions is four paths \(1 \to 2 \to 3 \to 5\), \(1 \to 2 \to 3 \to 5\), \(1 \to 4\), \(1 \to 4\), here \(c=[4,2,2,2,2]\). The value equals to \(4\cdot 6+ 2\cdot 2+2\cdot 1+2\cdot 5+2\cdot 7=54\).In the second test case, one of optimal solution is three paths \(1 \to 2 \to 3 \to ... | Input: 25 41 2 1 36 2 1 5 75 31 2 1 36 6 1 4 10 | Output: 54 56 | Hard | 5 | 696 | 832 | 82 | 17 |
292 | A | 292A | A. SMSC | 1,100 | implementation | Some large corporation where Polycarpus works has its own short message service center (SMSC). The center's task is to send all sorts of crucial information. Polycarpus decided to check the efficiency of the SMSC. For that, he asked to give him the statistics of the performance of the SMSC for some period of time. In t... | The first line contains a single integer n (1 β€ n β€ 103) β the number of tasks of the SMSC. Next n lines contain the tasks' descriptions: the i-th line contains two space-separated integers ti and ci (1 β€ ti, ci β€ 106) β the time (the second) when the i-th task was received and the number of messages to send, correspon... | In a single line print two space-separated integers β the time when the last text message was sent and the maximum queue size at a certain moment of time. | In the first test sample: second 1: the first message has appeared in the queue, the queue's size is 1; second 2: the first message is sent, the second message has been received, the queue's size is 1; second 3: the second message is sent, the queue's size is 0, Thus, the maximum size of the queue is 1, the last messag... | Input: 21 12 1 | Output: 3 1 | Easy | 1 | 1,659 | 525 | 154 | 2 |
689 | A | 689A | A. Mike and Cellphone | 1,400 | brute force; constructive algorithms; implementation | While swimming at the beach, Mike has accidentally dropped his cellphone into the water. There was no worry as he bought a cheap replacement phone with an old-fashioned keyboard. The keyboard has only ten digital equal-sized keys, located in the following way: Together with his old phone, he lost all his contacts and n... | The first line of the input contains the only integer n (1 β€ n β€ 9) β the number of digits in the phone number that Mike put in.The second line contains the string consisting of n digits (characters from '0' to '9') representing the number that Mike put in. | If there is no other phone number with the same finger movements and Mike can be sure he is calling the correct number, print ""YES"" (without quotes) in the only line.Otherwise print ""NO"" (without quotes) in the first line. | You can find the picture clarifying the first sample case in the statement above. | Input: 3586 | Output: NO | Easy | 3 | 878 | 257 | 226 | 6 |
913 | D | 913D | D. Too Easy Problems | 1,800 | binary search; brute force; data structures; greedy; sortings | You are preparing for an exam on scheduling theory. The exam will last for exactly T milliseconds and will consist of n problems. You can either solve problem i in exactly ti milliseconds or ignore it and spend no time. You don't need time to rest after solving a problem, either.Unfortunately, your teacher considers so... | The first line contains two integers n and T (1 β€ n β€ 2Β·105; 1 β€ T β€ 109) β the number of problems in the exam and the length of the exam in milliseconds, respectively.Each of the next n lines contains two integers ai and ti (1 β€ ai β€ n; 1 β€ ti β€ 104). The problems are numbered from 1 to n. | In the first line, output a single integer s β your maximum possible final score.In the second line, output a single integer k (0 β€ k β€ n) β the number of problems you should solve.In the third line, output k distinct integers p1, p2, ..., pk (1 β€ pi β€ n) β the indexes of problems you should solve, in any order.If ther... | In the first example, you should solve problems 3, 1, and 4. In this case you'll spend 80 + 100 + 90 = 270 milliseconds, falling within the length of the exam, 300 milliseconds (and even leaving yourself 30 milliseconds to have a rest). Problems 3 and 1 will bring you a point each, while problem 4 won't. You'll score t... | Input: 5 3003 1004 1504 802 902 300 | Output: 233 1 4 | Medium | 5 | 1,135 | 291 | 387 | 9 |
2,131 | D | 2131D | D. Arboris Contractio | 0 | data structures; graphs; greedy; trees | Kagari is preparing to archive a tree, and she knows the cost of doing so will depend on its diameter\(^{\text{β}}\). To keep the expense down, her goal is to shrink the diameter as much as possible first. She can perform the following operation on the tree: Choose two vertices \(s\) and \(t\). Let the sequence of vert... | 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 one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the number of the vertices in the tree.The following \(n-1\) line... | For each test case, output one integer β the minimum number of operations to minimize the diameter. | In the first test case, the diameter of the original tree is \(3\). Kagari can perform an operation on \(s = 3\) and \(t = 4\). As the figure depicts, the operations includes the following steps: Remove edges \((3, 1)\), \((1, 2)\) and \((2, 4)\). Add edges \((3, 1)\), \((3, 2)\) and \((3, 4)\). After the operation, th... | Input: 441 21 32 422 141 22 32 4111 21 32 43 53 85 65 77 97 105 11 | Output: 1 0 0 4 | Beginner | 4 | 1,246 | 644 | 99 | 21 |
1,417 | A | 1417A | A. Copy-paste | 800 | greedy; math | β Hey folks, how do you like this problem?β That'll do it. BThero is a powerful magician. He has got \(n\) piles of candies, the \(i\)-th pile initially contains \(a_i\) candies. BThero can cast a copy-paste spell as follows: He chooses two piles \((i, j)\) such that \(1 \le i, j \le n\) and \(i \ne j\). All candies fr... | The first line contains one integer \(T\) (\(1 \le T \le 500\)) β the number of test cases.Each test case consists of two lines: the first line contains two integers \(n\) and \(k\) (\(2 \le n \le 1000\), \(2 \le k \le 10^4\)); the second line contains \(n\) integers \(a_1\), \(a_2\), ..., \(a_n\) (\(1 \le a_i \le k\))... | For each test case, print one integer β the maximum number of times BThero can cast the spell without losing his magic power. | In the first test case we get either \(a = [1, 2]\) or \(a = [2, 1]\) after casting the spell for the first time, and it is impossible to cast it again. | Input: 3 2 2 1 1 3 5 1 2 3 3 7 3 2 2 | Output: 1 5 4 | Beginner | 2 | 670 | 473 | 125 | 14 |
1,618 | C | 1618C | C. Paint the Array | 1,100 | math | You are given an array \(a\) consisting of \(n\) positive integers. You have to choose a positive integer \(d\) and paint all elements into two colors. All elements which are divisible by \(d\) will be painted red, and all other elements will be painted blue.The coloring is called beautiful if there are no pairs of adj... | The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of testcases.The first line of each testcase contains one integer \(n\) (\(2 \le n \le 100\)) β the number of elements of the array.The second line of each testcase contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^{18... | For each testcase print a single integer. If there is no such value of \(d\) that yields a beautiful coloring, print \(0\). Otherwise, print any suitable value of \(d\) (\(1 \le d \le 10^{18}\)). | Input: 5 5 1 2 3 4 5 3 10 5 15 3 100 10 200 10 9 8 2 6 6 2 8 6 5 4 2 1 3 | Output: 2 0 100 0 3 | Easy | 1 | 476 | 325 | 195 | 16 | |
2,034 | H | 2034H | H. Rayan vs. Rayaneh | 3,300 | brute force; dfs and similar; dp; number theory | Rayan makes his final efforts to win Reyhaneh's heart by claiming he is stronger than Rayaneh (i.e., computer in Persian). To test this, Reyhaneh asks Khwarizmi for help. Khwarizmi explains that a set is integer linearly independent if no element in the set can be written as an integer linear combination of the others.... | The first line contains an integer \(t\) (\(1 \leq t \leq 100\)), the number of test cases.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 10^5\)), the size of the set. The second line contains \(n\) distinct integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^5\)).The sum of \(n\) over... | In the first line of each test case print the size of the largest integer linearly independent subset.In the next line, print one such subset in any order. If there are multiple valid subsets, print any one of them. | In example 1, \(\{4, 6\}\) is an integer linearly independent subset. It can be proven that there is no integer linearly independent subset with at least \(3\) elements.In example 2, \(\{35, 21, 30\}\) is an integer linearly independent subset because no integer linear combination of any two elements can create the thi... | Input: 352 4 6 8 10512 15 21 30 3532 3 6 | Output: 2 4 6 3 35 21 30 2 2 3 | Master | 4 | 760 | 369 | 215 | 20 |
2,128 | D | 2128D | D. Sum of LDS | 1,600 | brute force; combinatorics; dp; greedy; math | You're given a permutation\(^{\text{β}}\) \(p_1, \ldots, p_n\) such that \(\max(p_i, p_{i+1}) > p_{i+2}\) for all \(1 \leq i \leq n-2\).Compute the sum of the length of the longest decreasing subsequence\(^{\text{β }}\) of the subarray \([p_l, p_{l+1}, \ldots, p_r]\) over all pairs \(1 \leq l \leq r \leq n\).\(^{\text{β... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10\,000\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(3 \leq n \leq 500\,000\)).The second line of each test case contains \(n\) integers \(p... | For each test case, output the sum over all subarrays of the length of its longest decreasing subsequence. | For any array \(a\), we define \(\text{LDS}(a)\) as the length of the longest decreasing subsequence of \(a\).In the first test case, all subarrays are decreasing. In the second one, we have \(\text{LDS}([4]) = \text{LDS}([3]) = \text{LDS}([1]) = \text{LDS}([2]) = 1\) \(\text{LDS}([4,3]) = \text{LDS}([3,1]) = 2, \text{... | Input: 433 2 144 3 1 266 1 5 2 4 332 3 1 | Output: 10 17 40 8 | Medium | 5 | 885 | 547 | 106 | 21 |
1,282 | B2 | 1282B2 | B2. K for the Price of One (Hard Version) | 1,600 | dp; greedy; sortings | This is the hard version of this problem. The only difference is the constraint on \(k\) β the number of gifts in the offer. In this version: \(2 \le k \le n\).Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky β today the offer ""\(k\) of goods for the price of ... | The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases in the test.The next lines contain a description of \(t\) test cases. The first line of each test case contains three integers \(n, p, k\) (\(2 \le n \le 2 \cdot 10^5\), \(1 \le p \le 2\cdot10^9\), \(2 \le k \le n\)) β the numbe... | For each test case in a separate line print one integer \(m\) β the maximum number of goods that Vasya can buy. | Input: 8 5 6 2 2 4 3 5 7 5 11 2 2 4 3 5 7 3 2 3 4 2 6 5 2 3 10 1 3 9 2 2 10000 2 10000 10000 2 9999 2 10000 10000 4 6 4 3 2 3 2 5 5 3 1 2 2 1 2 | Output: 3 4 1 1 2 0 4 5 | Medium | 3 | 1,883 | 665 | 111 | 12 | |
1,748 | E | 1748E | E. Yet Another Array Counting Problem | 2,300 | binary search; data structures; divide and conquer; dp; flows; math; trees | The position of the leftmost maximum on the segment \([l; r]\) of array \(x = [x_1, x_2, \ldots, x_n]\) is the smallest integer \(i\) such that \(l \le i \le r\) and \(x_i = \max(x_l, x_{l+1}, \ldots, x_r)\).You are given an array \(a = [a_1, a_2, \ldots, a_n]\) of length \(n\). Find the number of integer arrays \(b = ... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^3\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(m\) (\(2 \le n,m \le 2 \cdot 10^5\), \(n \cdot m \le 10^6\)).The second line of each test case contains \(n\) intege... | For each test case print one integer β the number of arrays \(b\) that satisfy the conditions from the statement, modulo \(10^9+7\). | In the first test case, the following \(8\) arrays satisfy the conditions from the statement: \([1,2,1]\); \([1,2,2]\); \([1,3,1]\); \([1,3,2]\); \([1,3,3]\); \([2,3,1]\); \([2,3,2]\); \([2,3,3]\). In the second test case, the following \(5\) arrays satisfy the conditions from the statement: \([1,1,1,1]\); \([2,1,1,1]\... | Input: 43 31 3 24 22 2 2 26 96 9 6 9 6 99 10010 40 20 20 100 60 80 60 60 | Output: 8 5 11880 351025663 | Expert | 7 | 752 | 477 | 132 | 17 |
1,278 | F | 1278F | F. Cards | 2,600 | combinatorics; dp; math; number theory; probabilities | Consider the following experiment. You have a deck of \(m\) cards, and exactly one card is a joker. \(n\) times, you do the following: shuffle the deck, take the top card of the deck, look at it and return it into the deck.Let \(x\) be the number of times you have taken the joker out of the deck during this experiment.... | The only line contains three integers \(n\), \(m\) and \(k\) (\(1 \le n, m < 998244353\), \(1 \le k \le 5000\)). | Print one integer β the expected value of \(x^k\), taken modulo \(998244353\) (the answer can always be represented as an irreducible fraction \(\frac{a}{b}\), where \(b \mod 998244353 \ne 0\); you have to print \(a \cdot b^{-1} \mod 998244353\)). | Input: 1 1 1 | Output: 1 | Expert | 5 | 505 | 112 | 247 | 12 | |
1,303 | G | 1303G | G. Sum of Prefix Sums | 2,700 | data structures; divide and conquer; geometry; trees | We define the sum of prefix sums of an array \([s_1, s_2, \dots, s_k]\) as \(s_1 + (s_1 + s_2) + (s_1 + s_2 + s_3) + \dots + (s_1 + s_2 + \dots + s_k)\).You are given a tree consisting of \(n\) vertices. Each vertex \(i\) has an integer \(a_i\) written on it. We define the value of the simple path from vertex \(u\) to ... | The first line contains one integer \(n\) (\(2 \le n \le 150000\)) β the number of vertices in the tree.Then \(n - 1\) lines follow, representing the edges of the tree. Each line contains two integers \(u_i\) and \(v_i\) (\(1 \le u_i, v_i \le n\), \(u_i \ne v_i\)), denoting an edge between vertices \(u_i\) and \(v_i\).... | Print one integer β the maximum value over all paths in the tree. | The best path in the first example is from vertex \(3\) to vertex \(1\). It gives the sequence \([3, 3, 7, 1]\), and the sum of prefix sums is \(36\). | Input: 4 4 2 3 2 4 1 1 3 3 7 | Output: 36 | Master | 4 | 633 | 461 | 65 | 13 |
1,604 | A | 1604A | A. Era | 800 | greedy | Shohag has an integer sequence \(a_1, a_2, \ldots, a_n\). He can perform the following operation any number of times (possibly, zero): Select any positive integer \(k\) (it can be different in different operations). Choose any position in the sequence (possibly the beginning or end of the sequence, or in between any tw... | The first line contains a single integer \(t\) (\(1 \le t \le 200\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 100\)) β the initial length of the sequence.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le... | For each test case, print a single integer β the minimum number of operations needed to perform to achieve the goal mentioned in the statement. | In the first test case, we have to perform at least one operation, as \(a_2=3>2\). We can perform the operation \([1, 3, 4] \rightarrow [1, \underline{2}, 3, 4]\) (the newly inserted element is underlined), now the condition is satisfied.In the second test case, Shohag can perform the following operations:\([1, 2, 5, 7... | Input: 4 3 1 3 4 5 1 2 5 7 4 1 1 3 69 6969 696969 | Output: 1 3 0 696966 | Beginner | 1 | 1,083 | 360 | 143 | 16 |
2,045 | E | 2045E | E. Narrower Passageway | 2,700 | combinatorics; data structures | You are a strategist of The ICPC Kingdom. You received an intel that there will be monster attacks on a narrow passageway near the kingdom. The narrow passageway can be represented as a grid with \(2\) rows (numbered from \(1\) to \(2\)) and \(N\) columns (numbered from \(1\) to \(N\)). Denote \((r, c)\) as the cell in... | The first line consists of an integer \(N\) (\(1 \leq N \leq 100\,000\)).Each of the next two lines consists of \(N\) integers \(P_{r, c}\) (\(1 \leq P_{r, c} \leq 200\,000\)). | Let \(M = 998\,244\,353\). It can be shown that the expected total strength can be expressed as an irreducible fraction \(\frac{x}{y}\) such that \(x\) and \(y\) are integers and \(y \not\equiv 0 \pmod{M}\). Output an integer \(k\) in a single line such that \(0 \leq k < M\) and \(k \cdot y \equiv x \pmod{M}\). | Explanation for the sample input/output #1There are \(8\) possible scenarios for the passageway. Each scenario is equally likely to happen. Therefore, the expected total strength is \((0 + 5 + 10 + 5 + 5 + 0 + 5 + 0) / 8 = \frac{15}{4}\). Since \(249\,561\,092 \cdot 4 \equiv 15 \pmod{998\,244\,353}\), the output of thi... | Input: 3 8 4 5 5 4 8 | Output: 249561092 | Master | 2 | 1,645 | 176 | 312 | 20 |
784 | F | 784F | F. Crunching Numbers Just for You | 1,900 | *special; implementation | You are developing a new feature for the website which sells airline tickets: being able to sort tickets by price! You have already extracted the tickets' prices, so there's just the last step to be done...You are given an array of integers. Sort it in non-descending order. | The input consists of a single line of space-separated integers. The first number is n (1 β€ n β€ 10) β the size of the array. The following n numbers are the elements of the array (1 β€ ai β€ 100). | Output space-separated elements of the sorted array. | Remember, this is a very important feature, and you have to make sure the customers appreciate it! | Input: 3 3 1 2 | Output: 1 2 3 | Hard | 2 | 274 | 194 | 52 | 7 |
1,878 | A | 1878A | A. How Much Does Daytona Cost? | 800 | greedy | We define an integer to be the most common on a subsegment, if its number of occurrences on that subsegment is larger than the number of occurrences of any other integer in that subsegment. A subsegment of an array is a consecutive segment of elements in the array \(a\).Given an array \(a\) of size \(n\), and an intege... | Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. The description of test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 100\), \(1 \le k \le 100\)) β the number of elements in... | For each test case output ""YES"" if there exists a subsegment in which \(k\) is the most common element, and ""NO"" otherwise.You can output the answer in any case (for example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as a positive answer). | In the first test case we need to check if there is a subsegment where the most common element is \(4\).On the subsegment \([2,5]\) the elements are \(4, \ 3, \ 4, \ 1\). \(4\) appears \(2\) times; \(1\) appears \(1\) time; \(3\) appears \(1\) time.This means that \(4\) is the most common element on the subsegment \([2... | Input: 75 41 4 3 4 14 12 3 4 45 643 5 60 4 22 51 54 15 3 3 11 335 33 4 1 5 5 | Output: YES NO NO YES YES YES YES | Beginner | 1 | 426 | 525 | 271 | 18 |
1,549 | A | 1549A | A. Gregor and Cryptography | 800 | math; number theory | Gregor is learning about RSA cryptography, and although he doesn't understand how RSA works, he is now fascinated with prime numbers and factoring them.Gregor's favorite prime number is \(P\). Gregor wants to find two bases of \(P\). Formally, Gregor is looking for two integers \(a\) and \(b\) which satisfy both of the... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)).Each subsequent line contains the integer \(P\) (\(5 \le P \le {10}^9\)), with \(P\) guaranteed to be prime. | Your output should consist of \(t\) lines. Each line should consist of two integers \(a\) and \(b\) (\(2 \le a < b \le P\)). If there are multiple possible solutions, print any. | The first query is \(P=17\). \(a=3\) and \(b=5\) are valid bases in this case, because \(17 \bmod 3 = 17 \bmod 5 = 2\). There are other pairs which work as well.In the second query, with \(P=5\), the only solution is \(a=2\) and \(b=4\). | Input: 2 17 5 | Output: 3 5 2 4 | Beginner | 2 | 527 | 226 | 177 | 15 |
1,986 | G2 | 1986G2 | G2. Permutation Problem (Hard Version) | 2,500 | brute force; data structures; hashing; math; number theory | This is the hard version of the problem. The only difference is that in this version \(n \leq 5 \cdot 10^5\) and the sum of \(n\) for all sets of input data does not exceed \(5 \cdot 10^5\).You are given a permutation \(p\) of length \(n\). Calculate the number of index pairs \(1 \leq i < j \leq n\) such that \(p_i \cd... | Each test consists of several 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. Then follows their description.The first line of each set of input data contains a single integer \(n\) (\(1 \leq n \leq 5 \cdot 10^5\)) β the length of the permu... | For each set of input data, output the number of index pairs \(1 \leq i < j \leq n\) such that \(p_i \cdot p_j\) is divisible by \(i \cdot j\) without remainder. | In the first set of input data, there are no index pairs, as the size of the permutation is \(1\).In the second set of input data, there is one index pair \((1, 2)\) and it is valid.In the third set of input data, the index pair \((1, 2)\) is valid.In the fourth set of input data, the index pairs \((1, 2)\), \((1, 5)\)... | Input: 61121 232 3 152 4 1 3 5128 9 7 12 1 10 6 3 2 4 11 5151 2 4 6 8 10 12 14 3 9 15 5 7 11 13 | Output: 0 1 1 3 9 3 | Expert | 5 | 604 | 581 | 161 | 19 |
35 | B | 35B | B. Warehouse | 1,700 | implementation | Once upon a time, when the world was more beautiful, the sun shone brighter, the grass was greener and the sausages tasted better Arlandia was the most powerful country. And its capital was the place where our hero DravDe worked. He couldnβt program or make up problems (in fact, few people saw a computer those days) bu... | The first input line contains integers n, m and k (1 β€ n, m β€ 30, 1 β€ k β€ 2000) β the height, the width of shelving and the amount of the operations in the warehouse that you need to analyze. In the following k lines the queries are given in the order of appearance in the format described above. | For each query of the Β«-1 idΒ» type output two numbers in a separate line β index of the shelf and index of the section where the box with this identifier lay. If there was no such box in the warehouse when the query was made, output Β«-1 -1Β» without quotes. | Input: 2 2 9+1 1 1 cola+1 1 1 fanta+1 1 1 sevenup+1 1 1 whitekey-1 cola-1 fanta-1 sevenup-1 whitekey-1 cola | Output: 1 11 22 12 2-1 -1 | Medium | 1 | 2,521 | 296 | 256 | 0 | |
1,431 | J | 1431J | J. Zero-XOR Array | 3,400 | *special; dp | You are given an array of integers \(a\) of size \(n\). This array is non-decreasing, i. e. \(a_1 \le a_2 \le \dots \le a_n\).You have to find arrays of integers \(b\) of size \(2n - 1\), such that: \(b_{2i-1} = a_i\) (\(1 \le i \le n\)); array \(b\) is non-decreasing; \(b_1 \oplus b_2 \oplus \dots \oplus b_{2n-1} = 0\... | The first line contains a single integer \(n\) (\(2 \le n \le 17\)) β the size of the array \(a\).The second line contains \(n\) integers (\(0 \le a_i \le 2^{60} - 1; a_i \le a_{i+1}\)) β elements of the array \(a\). | Print a single integer β the number of arrays that meet all the above conditions, modulo \(998244353\). | Input: 3 0 1 3 | Output: 2 | Master | 2 | 529 | 216 | 103 | 14 | |
796 | F | 796F | F. Sequence Recovery | 2,800 | bitmasks; data structures; greedy | Zane once had a good sequence a consisting of n integers a1, a2, ..., an β but he has lost it.A sequence is said to be good if and only if all of its integers are non-negative and do not exceed 109 in value. However, Zane remembers having played around with his sequence by applying m operations to it.There are two type... | The first line contains two integers n and m (1 β€ n, m β€ 3Β·105) β the number of integers in Zane's original sequence and the number of operations that have been applied to the sequence, respectively.The i-th of the following m lines starts with one integer ti () β the type of the i-th operation.If the operation is type... | If there does not exist a valid good sequence, print ""NO"" (without quotation marks) in the first line.Otherwise, print ""YES"" (without quotation marks) in the first line, and print n space-separated integers b1, b2, ..., bn (0 β€ bi β€ 109) in the second line.If there are multiple answers, print any of them. | In the first sample, it is easy to verify that this good sequence is valid. In particular, its cuteness is 19 OR 0 OR 0 OR 0 OR 1 = 19.In the second sample, the two operations clearly contradict, so there is no such good sequence. | Input: 5 41 1 5 191 2 5 12 5 1001 1 5 100 | Output: YES19 0 0 0 1 | Master | 3 | 1,720 | 1,032 | 310 | 7 |
1,101 | A | 1101A | A. Minimum Integer | 1,000 | math | You are given \(q\) queries in the following form:Given three integers \(l_i\), \(r_i\) and \(d_i\), find minimum positive integer \(x_i\) such that it is divisible by \(d_i\) and it does not belong to the segment \([l_i, r_i]\).Can you answer all the queries?Recall that a number \(x\) belongs to segment \([l, r]\) if ... | The first line contains one integer \(q\) (\(1 \le q \le 500\)) β the number of queries.Then \(q\) lines follow, each containing a query given in the format \(l_i\) \(r_i\) \(d_i\) (\(1 \le l_i \le r_i \le 10^9\), \(1 \le d_i \le 10^9\)). \(l_i\), \(r_i\) and \(d_i\) are integers. | For each query print one integer: the answer to this query. | Input: 5 2 4 2 5 10 4 3 10 1 1 2 3 4 6 5 | Output: 6 4 1 3 10 | Beginner | 1 | 338 | 281 | 59 | 11 | |
82 | E | 82E | E. Corridor | 2,600 | geometry | Consider a house plan. Let the house be represented by an infinite horizontal strip defined by the inequality - h β€ y β€ h. Strictly outside the house there are two light sources at the points (0, f) and (0, - f). Windows are located in the walls, the windows are represented by segments on the lines y = h and y = - h. A... | The first line of the input file contains three integers n, h and f (1 β€ n β€ 500, 1 β€ h β€ 10, h < f β€ 1000). Next, n lines contain two integers each li, ri ( - 5000 β€ li < ri β€ 5000), each entry indicates two segments. Endpoints of the first segment are (li, h)-(ri, h), and endpoints of the second segment are (li, - h)... | Print the single real number β the area of the illuminated part of the floor with an absolute or relative error of no more than 10 - 4. | The second sample test is shown on the figure. Green area is the desired area of the illuminated part of the floor. Violet segments indicate windows. | Input: 1 1 2-1 1 | Output: 10.0000000000 | Expert | 1 | 487 | 485 | 135 | 0 |
1,370 | E | 1370E | E. Binary Subsequence Rotation | 2,100 | binary search; constructive algorithms; data structures; greedy | Naman has two binary strings \(s\) and \(t\) of length \(n\) (a binary string is a string which only consists of the characters ""0"" and ""1""). He wants to convert \(s\) into \(t\) using the following operation as few times as possible.In one operation, he can choose any subsequence of \(s\) and rotate it clockwise o... | The first line contains a single integer \(n\) \((1 \le n \le 10^6)\) β the length of the strings.The second line contains the binary string \(s\) of length \(n\).The third line contains the binary string \(t\) of length \(n\). | If it is impossible to convert \(s\) to \(t\) after any number of operations, print \(-1\).Otherwise, print the minimum number of operations required. | In the first test, Naman can choose the subsequence corresponding to indices \(\{2, 6\}\) and rotate it once to convert \(s\) into \(t\).In the second test, he can rotate the subsequence corresponding to all indices \(5\) times. It can be proved, that it is the minimum required number of operations.In the last test, it... | Input: 6 010000 000001 | Output: 1 | Hard | 4 | 1,031 | 227 | 150 | 13 |
1,516 | E | 1516E | E. Baby Ehab Plays with Permutations | 2,500 | combinatorics; dp; math | This time around, Baby Ehab will play with permutations. He has \(n\) cubes arranged in a row, with numbers from \(1\) to \(n\) written on them. He'll make exactly \(j\) operations. In each operation, he'll pick up \(2\) cubes and switch their positions.He's wondering: how many different sequences of cubes can I have a... | The only line contains \(2\) integers \(n\) and \(k\) (\(2 \le n \le 10^9\), \(1 \le k \le 200\)) β the number of cubes Baby Ehab has, and the parameter \(k\) from the statement. | Print \(k\) space-separated integers. The \(i\)-th of them is the number of possible sequences you can end up with if you do exactly \(i\) operations. Since this number can be very large, print the remainder when it's divided by \(10^9+7\). | In the second example, there are \(3\) sequences he can get after \(1\) swap, because there are \(3\) pairs of cubes he can swap. Also, there are \(3\) sequences he can get after \(2\) swaps: \([1,2,3]\), \([3,1,2]\), \([2,3,1]\). | Input: 2 3 | Output: 1 1 1 | Expert | 3 | 490 | 178 | 240 | 15 |
1,662 | B | 1662B | B. Toys | 0 | greedy; strings | Vittorio has three favorite toys: a teddy bear, an owl, and a raccoon. Each of them has a name. Vittorio takes several sheets of paper and writes a letter on each side of every sheet so that it is possible to spell any of the three names by arranging some of the sheets in a row (sheets can be reordered and flipped as n... | The first line contains a string \(t\) consisting of uppercase letters of the English alphabet (\(1\le |t| \le 1000\)) β the name of the teddy bear.The second line contains a string \(o\) consisting of uppercase letters of the English alphabet (\(1\le |o| \le 1000\)) β the name of the owl.The third line contains a stri... | The first line of the output contains a single integer \(m\) β the minimum number of sheets required.Then \(m\) lines follow: the \(j\)-th of these lines contains a string of two uppercase letters of the English alphabet β the letters appearing on the two sides of the \(j\)-th sheet.Note that you can print the sheets a... | In the first sample, the solution uses two sheets: the first sheet has A on one side and G on the other side; the second sheet has A on one side and M on the other side.The name AA can be spelled using the A side of both sheets. The name GA can be spelled using the G side of the first sheet and the A side of the second... | Input: AA GA MA | Output: 2 AG AM | Beginner | 2 | 729 | 529 | 366 | 16 |
70 | E | 70E | E. Information Reform | 2,700 | dp; implementation; trees | Thought it is already the XXI century, the Mass Media isn't very popular in Walrusland. The cities get news from messengers who can only travel along roads. The network of roads in Walrusland is built so that it is possible to get to any city from any other one in exactly one way, and the roads' lengths are equal.The N... | The first line contains two given numbers n and k (1 β€ n β€ 180, 1 β€ k β€ 105).The second line contains n - 1 integers di, numbered starting with 1 (di β€ di + 1, 0 β€ di β€ 105).Next n - 1 lines contain the pairs of cities connected by a road. | On the first line print the minimum number of fishlars needed for a year's maintenance. On the second line print n numbers, where the i-th number will represent the number of the regional center, appointed to the i-th city. If the i-th city is a regional center itself, then you should print number i.If there are severa... | Input: 8 102 5 9 11 15 19 201 41 31 74 62 82 33 5 | Output: 383 3 3 4 3 4 3 3 | Master | 3 | 1,033 | 239 | 367 | 0 | |
1,562 | F | 1562F | F. Tubular Bells | 2,900 | interactive; math; number theory; probabilities | Do you know what tubular bells are? They are a musical instrument made up of cylindrical metal tubes. In an orchestra, tubular bells are used to mimic the ringing of bells.Mike has tubular bells, too! They consist of \(n\) tubes, and each of the tubes has a length that can be expressed by a integer from \(l\) to \(r\) ... | Each test contains multiple test cases.The first line contains one positive integer \(t\) (\(1 \le t \le 20\)), denoting the number of test cases. Description of the test cases follows.The single line of each test case contains one positive integer \(n\) (\(3 \le n \le 10^5\)) β number of tubes in Mike's tubular bells.... | Input: 3 5 8 10 7 6 9 5 24 25 28 27 26 7 1 2 3 4 5 6 7 | Output: ? 1 2 40 ? 2 5 90 ? 3 1 56 ? 4 5 18 ! 8 10 7 6 9 ? 1 5 312 ? 2 4 675 ! 24 25 28 27 26 ? 1 4 4 ? 2 5 10 ? 3 7 21 ? 6 2 6 ? 2 5 10 ? 1 2 2 ? 1 2 2 ? 1 2 2 ? 1 2 2 ? 1 2 2 ! 1 2 3 4 5 6 7 | Master | 4 | 1,227 | 629 | 0 | 15 | ||
696 | D | 696D | D. Legen... | 2,500 | data structures; dp; matrices; strings | Barney was hanging out with Nora for a while and now he thinks he may have feelings for her. Barney wants to send her a cheesy text message and wants to make her as happy as possible. Initially, happiness level of Nora is 0. Nora loves some pickup lines like ""I'm falling for you"" and stuff. Totally, she knows n picku... | The first line of input contains two integers n and l (1 β€ n β€ 200, 1 β€ l β€ 1014) β the number of pickup lines and the maximum length of Barney's text.The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 100), meaning that Nora's happiness level increases by ai after every time seeing i-th pickup line.The next... | Print the only integer β the maximum possible value of Nora's happiness level after reading Barney's text. | An optimal answer for the first sample case is hearth containing each pickup line exactly once.An optimal answer for the second sample case is artart. | Input: 3 63 2 1heartearthart | Output: 6 | Expert | 4 | 890 | 522 | 106 | 6 |
2,094 | H | 2094H | H. La Vaca Saturno Saturnita | 1,900 | binary search; brute force; math; number theory | Saturnita's mood depends on an array \(a\) of length \(n\), which only he knows the meaning of, and a function \(f(k, a, l, r)\), which only he knows how to compute. Shown below is the pseudocode for his function \(f(k, a, l, r)\). function f(k, a, l, r): ans := 0 for i from l to r (inclusive): while k is divisible by ... | The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(q\) (\(1 \leq n \leq 10^5, 1 \leq q \leq 5\cdot 10^4\)).The following line contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(2 \leq a_i \leq 10^5\)).The followi... | For each query, output the answer on a new line. | Input: 25 32 3 5 7 112 1 52 2 42310 1 54 318 12 8 9216 1 248 2 482944 1 4 | Output: 5 6 1629 13 12 520 | Hard | 4 | 487 | 594 | 48 | 20 | |
161 | E | 161E | E. Polycarpus the Safecracker | 2,500 | brute force; dp | Polycarpus has t safes. The password for each safe is a square matrix consisting of decimal digits '0' ... '9' (the sizes of passwords to the safes may vary). Alas, Polycarpus has forgotten all passwords, so now he has to restore them.Polycarpus enjoys prime numbers, so when he chose the matrix passwords, he wrote a pr... | The first line of the input contains an integer t (1 β€ t β€ 30) β the number of safes. Next t lines contain integers pi (10 β€ pi β€ 99999), pi is a prime number written in the first row of the password matrix for the i-th safe. All pi's are written without leading zeros. | Print t numbers, the i-th of them should be the number of matrices that can be a password to the i-th safe. Print the numbers on separate lines. | Here is a possible password matrix for the second safe: 239307977Here is a possible password matrix for the fourth safe: 9001000200021223 | Input: 4112394019001 | Output: 428612834 | Expert | 2 | 982 | 269 | 144 | 1 |
1,969 | D | 1969D | D. Shop Game | 1,900 | data structures; greedy; math; sortings | Alice and Bob are playing a game in the shop. There are \(n\) items in the shop; each item has two parameters: \(a_i\) (item price for Alice) and \(b_i\) (item price for Bob).Alice wants to choose a subset (possibly empty) of items and buy them. After that, Bob does the following: if Alice bought less than \(k\) items,... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 2 \cdot 10^5\); \(0 \le k \le n\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)).The third li... | For each test case, print a single integer β Alice's profit if both Alice and Bob act optimally. | In the first test case, Alice should buy the \(2\)-nd item and sell it to Bob, so her profit is \(2 - 1 = 1\).In the second test case, Alice should buy the \(1\)-st, the \(2\)-nd and the \(3\)-rd item; then Bob takes the \(1\)-st item for free and pays for the \(2\)-nd and the \(3\)-rd item. Alice's profit is \((3+2) -... | Input: 42 02 11 24 11 2 1 43 3 2 34 22 1 1 14 2 3 26 21 3 4 9 1 37 6 8 10 6 8 | Output: 1 1 0 7 | Hard | 4 | 1,039 | 502 | 96 | 19 |
940 | E | 940E | E. Cashback | 2,000 | data structures; dp; greedy; math | Since you are the best Wraith King, Nizhniy Magazin Β«MirΒ» at the centre of Vinnytsia is offering you a discount.You are given an array a of length n and an integer c. The value of some array b of length k is the sum of its elements except for the smallest. For example, the value of the array [3, 1, 6, 5, 2] with c = 2 ... | The first line contains integers n and c (1 β€ n, c β€ 100 000).The second line contains n integers ai (1 β€ ai β€ 109) β elements of a. | Output a single integer β the smallest possible sum of values of these subarrays of some partition of a. | In the first example any partition yields 6 as the sum.In the second example one of the optimal partitions is [1, 1], [10, 10, 10, 10, 10, 10, 9, 10, 10, 10] with the values 2 and 90 respectively.In the third example one of the optimal partitions is [2, 3], [6, 4, 5, 7], [1] with the values 3, 13 and 1 respectively.In ... | Input: 3 51 2 3 | Output: 6 | Hard | 4 | 465 | 132 | 104 | 9 |
361 | A | 361A | A. Levko and Table | 800 | constructive algorithms; implementation | Levko loves tables that consist of n rows and n columns very much. He especially loves beautiful tables. A table is beautiful to Levko if the sum of elements in each row and column of the table equals k.Unfortunately, he doesn't know any such table. Your task is to help him to find at least one of them. | The single line contains two integers, n and k (1 β€ n β€ 100, 1 β€ k β€ 1000). | Print any beautiful table. Levko doesn't like too big numbers, so all elements of the table mustn't exceed 1000 in their absolute value.If there are multiple suitable tables, you are allowed to print any of them. | In the first sample the sum in the first row is 1 + 3 = 4, in the second row β 3 + 1 = 4, in the first column β 1 + 3 = 4 and in the second column β 3 + 1 = 4. There are other beautiful tables for this sample.In the second sample the sum of elements in each row and each column equals 7. Besides, there are other tables ... | Input: 2 4 | Output: 1 33 1 | Beginner | 2 | 304 | 75 | 212 | 3 |
1,851 | A | 1851A | A. Escalator Conversations | 800 | brute force; constructive algorithms; math | One day, Vlad became curious about who he can have a conversation with on the escalator in the subway. There are a total of \(n\) passengers. The escalator has a total of \(m\) steps, all steps indexed from \(1\) to \(m\) and \(i\)-th step has height \(i \cdot k\).Vlad's height is \(H\) centimeters. Two people with hei... | The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.Then the descriptions of the test cases follow.The first line of each test case contains integers: \(n, m, k, H\) (\(1 \le n,m \le 50\), \(1 \le k,H \le 10^6\)). Here, \(n\) is the number of people, \(m\) is the number of s... | For each test case, output a single integer β the number of people Vlad can have a conversation with on the escalator individually. | The first example is explained in the problem statement.In the second example, Vlad can have a conversation with the person with height \(11\).In the third example, Vlad can have a conversation with people with heights: \(44, 74, 98, 62\). Therefore, the answer is \(4\).In the fourth example, Vlad can have a conversati... | Input: 75 3 3 115 4 14 18 22 9 5 611 910 50 3 1143 44 74 98 62 60 99 4 11 734 8 8 4968 58 82 737 1 4 6618 66 39 83 48 99 799 1 1 1326 23 84 6 60 87 40 41 256 13 3 2830 70 85 13 1 55 | Output: 2 1 4 1 0 0 3 | Beginner | 3 | 1,731 | 563 | 131 | 18 |
912 | B | 912B | B. New Year's Eve | 1,300 | bitmasks; constructive algorithms; number theory | Since Grisha behaved well last year, at New Year's Eve he was visited by Ded Moroz who brought an enormous bag of gifts with him! The bag contains n sweet candies from the good ol' bakery, each labeled from 1 to n corresponding to its tastiness. No two candies have the same tastiness.The choice of candies has a direct ... | The sole string contains two integers n and k (1 β€ k β€ n β€ 1018). | Output one number β the largest possible xor-sum. | In the first sample case, one optimal answer is 1, 2 and 4, giving the xor-sum of 7.In the second sample case, one can, for example, take all six candies and obtain the xor-sum of 7. | Input: 4 3 | Output: 7 | Easy | 3 | 919 | 65 | 49 | 9 |
250 | B | 250B | B. Restoring IPv6 | 1,500 | implementation; strings | An IPv6-address is a 128-bit number. For convenience, this number is recorded in blocks of 16 bits in hexadecimal record, the blocks are separated by colons β 8 blocks in total, each block has four hexadecimal digits. Here is an example of the correct record of a IPv6 address: ""0124:5678:90ab:cdef:0124:5678:90ab:cdef"... | The first line contains a single integer n β the number of records to restore (1 β€ n β€ 100).Each of the following n lines contains a string β the short IPv6 addresses. Each string only consists of string characters ""0123456789abcdef:"".It is guaranteed that each short address is obtained by the way that is described i... | For each short IPv6 address from the input print its full record on a separate line. Print the full records for the short IPv6 addresses in the order, in which the short records follow in the input. | Input: 6a56f:d3:0:0124:01:f19a:1000:00a56f:00d3:0000:0124:0001::a56f::0124:0001:0000:1234:0ff0a56f:0000::0000:0001:0000:1234:0ff0::0ea::4d:f4:6:0 | Output: a56f:00d3:0000:0124:0001:f19a:1000:0000a56f:00d3:0000:0124:0001:0000:0000:0000a56f:0000:0000:0124:0001:0000:1234:0ff0a56f:0000:0000:0000:0001:0000:1234:0ff00000:000... | Medium | 2 | 1,990 | 364 | 198 | 2 | |
46 | D | 46D | D. Parking Lot | 1,800 | data structures; implementation | Nowadays it is becoming increasingly difficult to park a car in cities successfully. Let's imagine a segment of a street as long as L meters along which a parking lot is located. Drivers should park their cars strictly parallel to the pavement on the right side of the street (remember that in the country the authors of... | The first line contains three integers L, b ΠΈ f (10 β€ L β€ 100000, 1 β€ b, f β€ 100). The second line contains an integer n (1 β€ n β€ 100) that indicates the number of requests the program has got. Every request is described on a single line and is given by two numbers. The first number represents the request type. If the ... | For every request of the 1 type print number -1 on the single line if the corresponding car couldn't find place to park along the street. Otherwise, print a single number equal to the distance between the back of the car in its parked position and the beginning of the parking lot zone. | Input: 30 1 261 51 41 52 21 51 4 | Output: 06111723 | Medium | 2 | 1,567 | 853 | 286 | 0 | |
191 | E | 191E | E. Thwarting Demonstrations | 2,200 | binary search; data structures; trees | It is dark times in Berland. Berlyand opposition, funded from a neighboring state, has organized a demonstration in Berland capital Bertown. Through the work of intelligence we know that the demonstrations are planned to last for k days.Fortunately, Berland has a special police unit, which can save the country. It has ... | The first line contains two integers n and k β the number of soldiers in the detachment and the number of times somebody goes on duty.The second line contains n space-separated integers ai, their absolute value doesn't exceed 109 β the soldiers' reliabilities.Please do not use the %lld specifier to read or write 64-bit... | Print a single number β the sought minimum reliability of the groups that go on duty during these k days. | Input: 3 41 4 2 | Output: 4 | Hard | 3 | 1,672 | 402 | 105 | 1 | |
1,763 | C | 1763C | C. Another Array Problem | 2,000 | brute force; constructive algorithms; greedy | You are given an array \(a\) of \(n\) integers. You are allowed to perform the following operation on it as many times as you want (0 or more times): Choose \(2\) indices \(i\),\(j\) where \(1 \le i < j \le n\) and replace \(a_k\) for all \(i \leq k \leq j\) with \(|a_i - a_j|\) Print the maximum sum of all the element... | The first line contains a single integer \(t\) (\(1 \le t \le 10^5\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the length of the array \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_... | For each test case, print the sum of the final array. | In the first test case, it is not possible to achieve a sum \(> 3\) by using these operations, therefore the maximum sum is \(3\).In the second test case, it can be shown that the maximum sum achievable is \(16\). By using operation \((1,2)\) we transform the array from \([9,1]\) into \([8,8]\), thus the sum of the fin... | Input: 331 1 129 134 9 5 | Output: 3 16 18 | Hard | 3 | 375 | 455 | 53 | 17 |
656 | E | 656E | E. Out of Controls | 2,000 | *special | You are given a complete undirected graph. For each pair of vertices you are given the length of the edge that connects them. Find the shortest paths between each pair of vertices in the graph and return the length of the longest of them. | The first line of the input contains a single integer N (3 β€ N β€ 10).The following N lines each contain N space-separated integers. jth integer in ith line aij is the length of the edge that connects vertices i and j. aij = aji, aii = 0, 1 β€ aij β€ 100 for i β j. | Output the maximum length of the shortest path between any pair of vertices in the graph. | You're running short of keywords, so you can't use some of them:definedoforforeachwhilerepeatuntilifthenelseelifelsifelseifcaseswitch | Input: 30 1 11 0 41 4 0 | Output: 2 | Hard | 1 | 238 | 262 | 89 | 6 |
1,354 | F | 1354F | F. Summoning Minions | 2,500 | constructive algorithms; dp; flows; graph matchings; greedy; sortings | Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other.Polycarp can summon \(n\) different minions. The initial power level of the \(i\)-th minion is \(a_i\), and when it is summoned, all previously summoned minions' power levels are increased by \(b_i\).... | The first line contains one integer \(T\) (\(1 \le T \le 75\)) β the number of test cases.Each test case begins with a line containing two integers \(n\) and \(k\) (\(1 \le k \le n \le 75\)) β the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respective... | For each test case print the optimal sequence of actions as follows:Firstly, print \(m\) β the number of actions which Polycarp has to perform (\(0 \le m \le 2n\)). Then print \(m\) integers \(o_1\), \(o_2\), ..., \(o_m\), where \(o_i\) denotes the \(i\)-th action as follows: if the \(i\)-th action is to summon the min... | Consider the example test.In the first test case, Polycarp can summon the minion \(2\) with power level \(7\), then summon the minion \(1\), which will increase the power level of the previous minion by \(3\), then destroy the minion \(1\), and finally, summon the minion \(5\). After this, Polycarp will have two minion... | Input: 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 | Output: 4 2 1 -1 5 1 2 5 5 4 3 2 1 | Expert | 6 | 841 | 499 | 720 | 13 |
2,121 | B | 2121B | B. Above the Clouds | 800 | constructive algorithms; greedy; strings | You are given a string \(s\) of length \(n\), consisting of lowercase letters of the Latin alphabet. Determine whether there exist three non-empty strings \(a\), \(b\), and \(c\) such that: \(a + b + c = s\), meaning the concatenation\(^{\text{β}}\) of strings \(a\), \(b\), and \(c\) equals \(s\). The string \(b\) is a... | Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(3 \leq n \leq 10^5\)) β the length of the string \(s\). The second ... | For each test case, output ""Yes"" if there exist three non-empty strings \(a\), \(b\), and \(c\) that satisfy the conditions, and ""No"" otherwise. You may output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive answers. | In the first test case, there exist unique non-empty strings \(a\), \(b\), and \(c\) such that \(a + b + c = s\). These are the strings \(a =\) ""a"", \(b =\) ""a"", and \(c =\) ""a"". The concatenation of strings \(a\) and \(c\) equals \(a + c = \) ""aa"". The string \(b\) is a substring of this string. In the sixth t... | Input: 123aaa3aba3aab4abca4abba4aabb5abaca5abcda5abcba6abcbbf6abcdaa3abb | Output: Yes No Yes No Yes Yes Yes No Yes Yes Yes Yes | Beginner | 3 | 964 | 535 | 308 | 21 |
1,851 | C | 1851C | C. Tiles Comeback | 1,000 | greedy | Vlad remembered that he had a series of \(n\) tiles and a number \(k\). The tiles were numbered from left to right, and the \(i\)-th tile had colour \(c_i\).If you stand on the first tile and start jumping any number of tiles right, you can get a path of length \(p\). The length of the path is the number of tiles you s... | The first line of input data contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 2 \cdot 10^5\))βthe number of tiles in the series and the length of the blo... | For each test case, output on a separate line: YES if you can get a path that satisfies these conditions; NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as positive response). | In the first test case, you can jump from the first tile to the last tile;The second test case is explained in the problem statement. | Input: 104 21 1 1 114 31 2 1 1 7 5 3 3 1 3 4 4 2 43 33 1 310 41 2 1 2 1 2 1 2 1 26 21 3 4 1 6 62 21 14 22 1 1 12 11 23 22 2 24 11 1 2 2 | Output: YES YES NO NO YES YES NO YES YES YES | Beginner | 1 | 1,687 | 552 | 243 | 18 |
1,468 | M | 1468M | M. Similar Sets | 2,300 | data structures; graphs; implementation | You are given \(n\) sets of integers. The \(i\)-th set contains \(k_i\) integers.Two sets are called similar if they share at least two common elements, i. e. there exist two integers \(x\) and \(y\) such that \(x \ne y\), and they both belong to each of the two sets.Your task is to find two similar sets among the give... | The first line contains a single integer \(t\) (\(1 \le t \le 50000\)) β the number of test cases. Then \(t\) test cases follow.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 10^5\)) the number of given sets. The following \(n\) lines describe the sets. The \(i\)-th line starts with an ... | For each test case, print the answer on a single line. If there is no pair of similar sets, print -1. Otherwise, print two different integers β the indices of the similar sets. The sets are numbered from \(1\) to \(n\) in the order they are given in the input. If there are multiple answers, print any of them. | Input: 342 1 103 1 3 55 5 4 3 2 13 10 20 3034 1 2 3 44 2 3 4 54 3 4 5 623 1 3 53 4 3 2 | Output: 2 3 1 2 -1 | Expert | 3 | 373 | 702 | 310 | 14 | |
1,599 | H | 1599H | H. Hidden Fortress | 2,100 | interactive; math | This is an interactive problem!As part of your contribution in the Great Bubble War, you have been tasked with finding the newly built enemy fortress. The world you live in is a giant \(10^9 \times 10^9\) grid, with squares having both coordinates between \(1\) and \(10^9\). You know that the enemy base has the shape o... | The input contains the answers to your queries. | Input: 1 1 2 1 | Output: ? 2 2 ? 5 5 ? 4 7 ? 1 5 ! 2 3 4 5 | Hard | 2 | 1,207 | 47 | 0 | 15 | ||
1,431 | H | 1431H | H. Rogue-like Game | 2,600 | *special; brute force; greedy; two pointers | Marina plays a new rogue-like game. In this game, there are \(n\) different character species and \(m\) different classes. The game is played in runs; for each run, Marina has to select a species and a class for her character. If she selects the \(i\)-th species and the \(j\)-th class, she will get \(c_{i, j}\) points ... | The first line contains three integers \(n\), \(m\) and \(k\) (\(1 \le n, m \le 1500\); \(0 \le k \le 10^9\)).The second line contains \(n\) integers \(a_1\), \(a_2\), ..., \(a_n\) (\(0 = a_1 \le a_2 \le \dots \le a_n \le 10^{12}\)), where \(a_i\) is the number of points required to unlock the \(i\)-th species (or \(0\... | Print one integer β the minimum number of runs Marina has to play to unlock all species and all classes if she can read exactly one guide before playing the game. | The explanation for the first test: Marina reads a guide on the combination of the \(1\)-st species and the \(2\)-nd class. Thus, \(c_{1, 2}\) becomes \(7\). Initially, only the \(1\)-st species and the \(1\)-st class are unlocked. Marina plays a run with the \(1\)-st species and the \(1\)-st class. Her score becomes \... | Input: 3 4 2 0 5 7 0 2 6 10 2 5 5 2 5 3 4 4 3 4 2 4 | Output: 3 | Expert | 4 | 1,349 | 945 | 162 | 14 |
1,998 | B | 1998B | B. Minimize Equal Sum Subarrays | 1,000 | constructive algorithms; math; number theory | It is known that Farmer John likes Permutations, but I like them too!β Sun Tzu, The Art of Constructing PermutationsYou are given a permutation\(^{\text{β}}\) \(p\) of length \(n\).Find a permutation \(q\) of length \(n\) that minimizes the number of pairs (\(i, j\)) (\(1 \leq i \leq j \leq n\)) such that \(p_i + p_{i+... | The first line contains \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)).The following line contains \(n\) space-separated integers \(p_1, p_2, \ldots, p_n\) (\(1 \leq p_i \leq n\)) β denoting the permutation \(p\) of length \(n\)... | For each test case, output one line containing any permutation of length \(n\) (the permutation \(q\)) such that \(q\) minimizes the number of pairs. | For the first test, there exists only one pair (\(i, j\)) (\(1 \leq i \leq j \leq n\)) such that \(p_i + p_{i+1} + \ldots + p_j = q_i + q_{i+1} + \ldots + q_j\), which is (\(1, 2\)). It can be proven that no such \(q\) exists for which there are no pairs. | Input: 321 251 2 3 4 574 7 5 1 2 6 3 | Output: 2 1 3 5 4 2 1 6 2 1 4 7 3 5 | Beginner | 3 | 710 | 413 | 149 | 19 |
1,167 | C | 1167C | C. News Distribution | 1,400 | dfs and similar; dsu; graphs | In some social network, there are \(n\) users communicating with each other in \(m\) groups of friends. Let's analyze the process of distributing some news between users.Initially, some user \(x\) receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is ... | The first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 5 \cdot 10^5\)) β the number of users and the number of groups of friends, respectively.Then \(m\) lines follow, each describing a group of friends. The \(i\)-th line begins with integer \(k_i\) (\(0 \le k_i \le n\)) β the number of users in the \(i... | Print \(n\) integers. The \(i\)-th integer should be equal to the number of users that will know the news if user \(i\) starts distributing it. | Input: 7 5 3 2 5 4 0 2 1 2 1 1 2 6 7 | Output: 4 4 1 4 4 2 2 | Easy | 3 | 711 | 495 | 143 | 11 | |
345 | F | 345F | F. Superstitions Inspection | 2,700 | *special | You read scientific research regarding popularity of most famous superstitions across various countries, and you want to analyze their data. More specifically, you want to know which superstitions are popular in most countries.The data is given as a single file in the following format: country name on a separate line, ... | The input contains between 2 and 50 lines. Every line of input will contain between 1 and 50 characters, inclusive.No line has leading or trailing spaces. | Output the list of superstitions which are observed in the greatest number of countries in alphabetical order. Each superstition must be converted to lowercase (one superstition can be written with varying capitalization in different countries).The alphabetical order of superstitions means the lexicographical order of ... | Input: Ukraine* Friday the 13th* black cat* knock the woodUSA* wishing well* friday the 13thHollandFrance* Wishing Well | Output: friday the 13th wishing well | Master | 1 | 1,342 | 154 | 353 | 3 | |
1,931 | D | 1931D | D. Divisible Pairs | 1,300 | combinatorics; math; number theory | Polycarp has two favorite integers \(x\) and \(y\) (they can be equal), and he has found an array \(a\) of length \(n\).Polycarp considers a pair of indices \(\langle i, j \rangle\) (\(1 \le i < j \le n\)) beautiful if: \(a_i + a_j\) is divisible by \(x\); \(a_i - a_j\) is divisible by \(y\). For example, if \(x=5\), \... | The first line of the input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Then the descriptions of the test cases follow.The first line of each test case contains three integers \(n\), \(x\), and \(y\) (\(2 \le n \le 2 \cdot 10^5\), \(1 \le x, y \le 10^9\)) β the size of the array a... | For each test case, output a single integer β the number of beautiful pairs in the array \(a\). | Input: 76 5 21 2 7 4 9 67 9 51 10 15 3 8 12 159 4 1014 10 2 2 11 11 13 5 69 5 610 7 6 7 9 7 7 10 109 6 24 9 7 1 2 2 13 3 159 2 314 6 1 15 12 15 8 2 1510 5 713 3 3 2 12 11 3 7 13 14 | Output: 2 0 1 3 5 7 0 | Easy | 3 | 750 | 580 | 95 | 19 | |
1,479 | E | 1479E | E. School Clubs | 3,500 | dp; fft; math; number theory; probabilities | In Homer's school, there are \(n\) students who love clubs. Initially, there are \(m\) clubs, and each of the \(n\) students is in exactly one club. In other words, there are \(a_i\) students in the \(i\)-th club for \(1 \leq i \leq m\) and \(a_1+a_2+\dots+a_m = n\).The \(n\) students are so unfriendly that every day o... | The first line contains an integer \(m\) (\(1 \leq m \leq 1000\)) β the number of clubs initially.The second line contains \(m\) integers \(a_1, a_2, \dots, a_m\) (\(1 \leq a_i \leq 4 \cdot 10^8\)) with \(1 \leq a_1+a_2+\dots+a_m \leq 4 \cdot 10^8\), where \(a_i\) denotes the number of students in the \(i\)-th club ini... | Print one integer β the expected number of days until every student is in the same club for the first time, modulo \(998\,244\,353\). | In the first example, no matter which student gets angry, the two students will become in the same club with probability \(\frac 1 4\). So the expected number of days until every student is in the same club should be \(4\).In the second example, we note that in the first day: The only student in the first club will get... | Input: 2 1 1 | Output: 4 | Master | 5 | 1,669 | 327 | 133 | 14 |
853 | C | 853C | C. Boredom | 2,100 | data structures | Ilya is sitting in a waiting area of Metropolis airport and is bored of looking at time table that shows again and again that his plane is delayed. So he took out a sheet of paper and decided to solve some problems.First Ilya has drawn a grid of size n Γ n and marked n squares on it, such that no two marked squares sha... | The first line of input contains two integers n and q (2 β€ n β€ 200 000, 1 β€ q β€ 200 000) β the size of the grid and the number of query rectangles.The second line contains n integers p1, p2, ..., pn, separated by spaces (1 β€ pi β€ n, all pi are different), they specify grid squares marked by Ilya: in column i he has mar... | For each query rectangle output its beauty degree on a separate line. | The first sample test has one beautiful rectangle that occupies the whole grid, therefore the answer to any query is 1.In the second sample test the first query rectangle intersects 3 beautiful rectangles, as shown on the picture below: There are 5 beautiful rectangles that intersect the second query rectangle, as show... | Input: 2 31 21 1 1 11 1 1 21 1 2 2 | Output: 111 | Hard | 1 | 1,075 | 711 | 69 | 8 |
770 | C | 770C | C. Online Courses In BSU | 1,500 | *special; dfs and similar; graphs; implementation | Now you can take online courses in the Berland State University! Polycarp needs to pass k main online courses of his specialty to get a diploma. In total n courses are availiable for the passage.The situation is complicated by the dependence of online courses, for each course there is a list of those that must be passe... | The first line contains n and k (1 β€ k β€ n β€ 105) β the number of online-courses and the number of main courses of Polycarp's specialty. The second line contains k distinct integers from 1 to n β numbers of main online-courses of Polycarp's specialty. Then n lines follow, each of them describes the next course: the i-t... | Print -1, if there is no the way to get a specialty. Otherwise, in the first line print the integer m β the minimum number of online-courses which it is necessary to pass to get a specialty. In the second line print m distinct integers β numbers of courses which it is necessary to pass in the chronological order of the... | In the first test firstly you can take courses number 1 and 2, after that you can take the course number 4, then you can take the course number 5, which is the main. After that you have to take only the course number 3, which is the last not passed main course. | Input: 6 25 30002 2 11 41 5 | Output: 51 2 3 4 5 | Medium | 4 | 743 | 717 | 396 | 7 |
1,957 | B | 1957B | B. A BIT of a Construction | 1,100 | bitmasks; constructive algorithms; greedy; implementation | Given integers \(n\) and \(k\), construct a sequence of \(n\) non-negative (i.e. \(\geq 0\)) integers \(a_1, a_2, \ldots, a_n\) such that \(\sum\limits_{i = 1}^n a_i = k\) The number of \(1\)s in the binary representation of \(a_1 | a_2 | \ldots | a_n\) is maximized, where \(|\) denotes the bitwise OR operation. | The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The only line of each test case contains two integers \(n\) and \(k\) (\(1 \leq n \leq 2 \cdot 10^5\), \(1 \leq k \leq 10^9\)) β the number of non-negative integers to be printed and the sum respectively.It is guaranteed ... | For each test case, output a sequence \(a_1, a_2, \ldots, a_n\) on a new line that satisfies the conditions given above.If there are multiple solutions, print any of them. | In the first test case, we have to print exactly one integer, hence we can only output \(5\) as the answer.In the second test case, we output \(1, 2\) which sum up to \(3\), and \(1 | 2 = (11)_2\) has two \(1\)s in its binary representation, which is the maximum we can achieve in these constraints.In the fourth test ca... | Input: 41 52 32 56 51 | Output: 5 1 2 5 0 3 1 1 32 2 12 | Easy | 4 | 313 | 395 | 171 | 19 |
802 | F | 802F | F. Marmots (hard) | 2,800 | math; probabilities | Your task is the exact same as for the easy version. But this time, the marmots subtract the village's population P from their random number before responding to Heidi's request.Also, there are now villages with as few as a single inhabitant, meaning that .Can you help Heidi find out whether a village follows a Poisson... | Same as for the easy and medium versions. But remember that now 1 β€ P β€ 1000 and that the marmots may provide positive as well as negative integers. | Output one line per village, in the same order as provided in the input. The village's line shall state poisson if the village's distribution is of the Poisson type, and uniform if the answers came from a uniform distribution. | Master | 2 | 347 | 148 | 226 | 8 | ||
1,808 | D | 1808D | D. Petya, Petya, Petr, and Palindromes | 2,100 | binary search; brute force; data structures; two pointers | Petya and his friend, the robot Petya++, have a common friend β the cyborg Petr#. Sometimes Petr# comes to the friends for a cup of tea and tells them interesting problems.Today, Petr# told them the following problem.A palindrome is a sequence that reads the same from left to right as from right to left. For example, \... | The first line of the input contains two integers \(n\) and \(k\) (\(1 \le n \le 2 \cdot 10^5\), \(1 \le k \le n\), \(k\) is odd) β the length of the sequence and the length of subarrays for which it is necessary to determine whether they are palindromes.The second line of the input contains \(n\) integers \(a_1, a_2, ... | Output a single integer β the total palindromicity of all subarrays of length \(k\). | In the first example, the palindromicity of the subarray \([1, 2, 8, 2, 5]\) is \(1\), the palindromicity of the string \([2, 8, 2, 5, 2]\) is also \(1\), the palindromicity of the string \([8, 2, 5, 2, 8]\) is \(0\), and the palindromicity of the string \([2, 5, 2, 8, 6]\) is \(2\). The total palindromicity is \(1+1+0... | Input: 8 5 1 2 8 2 5 2 8 6 | Output: 4 | Hard | 4 | 1,251 | 389 | 84 | 18 |
2,090 | B | 2090B | B. Pushing Balls | 1,000 | brute force; dp; implementation | Ecrade has an \(n \times m\) grid, originally empty, and he has pushed several (possibly, zero) balls in it.Each time, he can push one ball into the grid either from the leftmost edge of a particular row or the topmost edge of a particular column of the grid.When a ball moves towards a position: If there is no ball ori... | The first line contains an integer \(t\) (\(1 \le t \le 10\,000\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n, m \le 50\)).This is followed by \(n\) lines, each containing exactly \(m\) characters and consisting... | For each test case, output ""Yes"" (without quotes) if it is possible for Ecrade to push the balls to reach the final state, and ""No"" (without quotes) otherwise.You can output ""Yes"" and ""No"" in any case (for example, strings ""YES"", ""yEs"" and ""yes"" will be recognized as a positive response). | For simplicity, if Ecrade pushes a ball from the leftmost edge of the \(i\)-th row, we call the operation \(\text{ROW}\ i\); if he pushes a ball from the topmost edge of the \(i\)-th column, we call the operation \(\text{COL}\ i\).For intuitive understanding, a non-zero number \(x\) in the matrix given below represents... | Input: 53 30010011103 30101110103 31111111113 30000000003 3000000001 | Output: YES YES YES YES NO | Beginner | 3 | 903 | 594 | 303 | 20 |
792 | E | 792E | E. Colored Balls | 2,500 | greedy; math; number theory | There are n boxes with colored balls on the table. Colors are numbered from 1 to n. i-th box contains ai balls, all of which have color i. You have to write a program that will divide all balls into sets such that: each ball belongs to exactly one of the sets, there are no empty sets, there is no set containing two (or... | The first line contains one integer number n (1 β€ n β€ 500).The second line contains n integer numbers a1, a2, ... , an (1 β€ ai β€ 109). | Print one integer number β the minimum possible number of sets. | In the first example the balls can be divided into sets like that: one set with 4 balls of the first color, two sets with 3 and 4 balls, respectively, of the second color, and two sets with 4 balls of the third color. | Input: 34 7 8 | Output: 5 | Expert | 3 | 526 | 134 | 63 | 7 |
1,221 | E | 1221E | E. Game With String | 2,500 | games | Alice and Bob play a game. Initially they have a string \(s_1, s_2, \dots, s_n\), consisting of only characters . and X. They take alternating turns, and Alice is moving first. During each turn, the player has to select a contiguous substring consisting only of characters . and replaces each of them with X. Alice must ... | The first line contains one integer \(q\) (\(1 \le q \le 3 \cdot 10^5\)) β the number of queries.The first line of each query contains two integers \(a\) and \(b\) (\(1 \le b < a \le 3 \cdot 10^5\)).The second line of each query contains the string \(s\) (\(1 \le |s| \le 3 \cdot 10^5\)), consisting of only characters .... | For each test case print YES if Alice can win and NO otherwise.You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). | In the first query Alice can select substring \(s_3 \dots s_5\). After that \(s\) turns into XXXXX...XX...X. After that, no matter what move Bob makes, Alice can make the move (this will be her second move), but Bob can't make his second move.In the second query Alice can not win because she cannot even make one move.I... | Input: 3 3 2 XX......XX...X 4 2 X...X.X..X 5 3 .......X..X | Output: YES NO YES | Expert | 1 | 822 | 413 | 206 | 12 |
255 | B | 255B | B. Code Parsing | 1,200 | implementation | Little Vitaly loves different algorithms. Today he has invented a new algorithm just for you. Vitaly's algorithm works with string s, consisting of characters ""x"" and ""y"", and uses two following operations at runtime: Find two consecutive characters in the string, such that the first of them equals ""y"", and the s... | The first line contains a non-empty string s. It is guaranteed that the string only consists of characters ""x"" and ""y"". It is guaranteed that the string consists of at most 106 characters. It is guaranteed that as the result of the algorithm's execution won't be an empty string. | In the only line print the string that is printed as the result of the algorithm's work, if the input of the algorithm input receives string s. | In the first test the algorithm will end after the first step of the algorithm, as it is impossible to apply any operation. Thus, the string won't change.In the second test the transformation will be like this: string ""yxyxy"" transforms into string ""xyyxy""; string ""xyyxy"" transforms into string ""xyxyy""; string ... | Input: x | Output: x | Easy | 1 | 1,308 | 283 | 143 | 2 |
585 | B | 585B | B. Phillip and Trains | 1,700 | dfs and similar; graphs; shortest paths | The mobile application store has a new game called ""Subway Roller"".The protagonist of the game Philip is located in one end of the tunnel and wants to get out of the other one. The tunnel is a rectangular field consisting of three rows and n columns. At the beginning of the game the hero is in some cell of the leftmo... | Each test contains from one to ten sets of the input data. The first line of the test contains a single integer t (1 β€ t β€ 10 for pretests and tests or t = 1 for hacks; see the Notes section for details) β the number of sets.Then follows the description of t sets of the input data. The first line of the description of ... | For each set of the input data print on a single line word YES, if it is possible to win the game and word NO otherwise. | In the first set of the input of the first sample Philip must first go forward and go down to the third row of the field, then go only forward, then go forward and climb to the second row, go forward again and go up to the first row. After that way no train blocks Philip's path, so he can go straight to the end of the ... | Input: 216 4...AAAAA........s.BBB......CCCCC........DDDDD...16 4...AAAAA........s.BBB....CCCCC.........DDDDD.... | Output: YESNO | Medium | 3 | 1,241 | 932 | 120 | 5 |
1,356 | A3 | 1356A3 | A3. Distinguish Z from S | 0 | *special | You are given an operation that implements a single-qubit unitary transformation: either the Z gate or the S gate. The operation will have Adjoint and Controlled variants defined.Your task is to perform necessary operations and measurements to figure out which unitary it was and to return 0 if it was the Z gate or 1 if... | Beginner | 1 | 727 | 0 | 0 | 13 | ||||
898 | D | 898D | D. Alarm Clock | 1,600 | greedy | Every evening Vitalya sets n alarm clocks to wake up tomorrow. Every alarm clock rings during exactly one minute and is characterized by one integer ai β number of minute after midnight in which it rings. Every alarm clock begins ringing at the beginning of the minute and rings during whole minute. Vitalya will definit... | First line contains three integers n, m and k (1 β€ k β€ n β€ 2Β·105, 1 β€ m β€ 106) β number of alarm clocks, and conditions of Vitalya's waking up. Second line contains sequence of distinct integers a1, a2, ..., an (1 β€ ai β€ 106) in which ai equals minute on which i-th alarm clock will ring. Numbers are given in arbitrary ... | Output minimal number of alarm clocks that Vitalya should turn off to sleep all next day long. | In first example Vitalya should turn off first alarm clock which rings at minute 3.In second example Vitalya shouldn't turn off any alarm clock because there are no interval of 10 consequence minutes in which 3 alarm clocks will ring.In third example Vitalya should turn off any 6 alarm clocks. | Input: 3 3 23 5 1 | Output: 1 | Medium | 1 | 848 | 389 | 94 | 8 |
38 | A | 38A | A. Army | 800 | implementation | The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.Vasya has ju... | The first input line contains an integer n (2 β€ n β€ 100). The second line contains n - 1 integers di (1 β€ di β€ 100). The third input line contains two integers a and b (1 β€ a < b β€ n). The numbers on the lines are space-separated. | Print the single number which is the number of years that Vasya needs to rise from rank a to rank b. | Input: 35 61 2 | Output: 5 | Beginner | 1 | 487 | 230 | 100 | 0 | |
335 | F | 335F | F. Buy One, Get One Free | 3,000 | dp; greedy | A nearby pie shop is having a special sale. For each pie you pay full price for, you may select one pie of a strictly lesser value to get for free. Given the prices of all the pies you wish to acquire, determine the minimum total amount you must pay for all of the pies. | Input will begin with an integer n (1 β€ n β€ 500000), the number of pies you wish to acquire. Following this is a line with n integers, each indicating the cost of a pie. All costs are positive integers not exceeding 109. | Print the minimum cost to acquire all the pies.Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. | In the first test case you can pay for a pie with cost 5 and get a pie with cost 4 for free, then pay for a pie with cost 5 and get a pie with cost 3 for free, then pay for a pie with cost 4 and get a pie with cost 3 for free.In the second test case you have to pay full price for every pie. | Input: 63 4 5 3 4 5 | Output: 14 | Master | 2 | 270 | 220 | 194 | 3 |
222 | D | 222D | D. Olympiad | 1,900 | binary search; greedy; sortings; two pointers | A boy named Vasya has taken part in an Olympiad. His teacher knows that in total Vasya got at least x points for both tours of the Olympiad. The teacher has the results of the first and the second tour of the Olympiad but the problem is, the results have only points, no names. The teacher has to know Vasya's chances.He... | The first line contains two space-separated integers n, x (1 β€ n β€ 105; 0 β€ x β€ 2Β·105) β the number of Olympiad participants and the minimum number of points Vasya earned.The second line contains n space-separated integers: a1, a2, ..., an (0 β€ ai β€ 105) β the participants' points in the first tour.The third line conta... | Print two space-separated integers β the best and the worst place Vasya could have got on the Olympiad. | In the first text sample all 5 participants earn 2 points each in any case. Depending on the jury's decision, Vasya can get the first (the best) as well as the last (the worst) fifth place.In the second test sample in the best case scenario Vasya wins again: he can win 12 points and become the absolute winner if the to... | Input: 5 21 1 1 1 11 1 1 1 1 | Output: 1 5 | Hard | 4 | 805 | 612 | 103 | 2 |
911 | A | 911A | A. Nearest Minimums | 1,100 | implementation | You are given an array of n integer numbers a0, a1, ..., an - 1. Find the distance between two closest (nearest) minimums in it. It is guaranteed that in the array a minimum occurs at least two times. | The first line contains positive integer n (2 β€ n β€ 105) β size of the given array. The second line contains n integers a0, a1, ..., an - 1 (1 β€ ai β€ 109) β elements of the array. It is guaranteed that in the array a minimum occurs at least two times. | Print the only number β distance between two nearest minimums in the array. | Input: 23 3 | Output: 1 | Easy | 1 | 200 | 251 | 75 | 9 | |
402 | E | 402E | E. Strictly Positive Matrix | 2,200 | graphs; math | You have matrix a of size n Γ n. Let's number the rows of the matrix from 1 to n from top to bottom, let's number the columns from 1 to n from left to right. Let's use aij to represent the element on the intersection of the i-th row and the j-th column. Matrix a meets the following two conditions: for any numbers i, j ... | The first line contains integer n (2 β€ n β€ 2000) β the number of rows and columns in matrix a.The next n lines contain the description of the rows of matrix a. The i-th line contains n non-negative integers ai1, ai2, ..., ain (0 β€ aij β€ 50). It is guaranteed that . | If there is a positive integer k β₯ 1, such that matrix ak is strictly positive, print ""YES"" (without the quotes). Otherwise, print ""NO"" (without the quotes). | Input: 21 00 1 | Output: NO | Hard | 2 | 571 | 265 | 161 | 4 | |
1,741 | B | 1741B | B. Funny Permutation | 800 | constructive algorithms; math | A sequence of \(n\) numbers is called permutation if it contains all numbers from \(1\) to \(n\) exactly once. For example, the sequences \([3, 1, 4, 2]\), [\(1\)] and \([2,1]\) are permutations, but \([1,2,1]\), \([0,1]\) and \([1,3,4]\) are not.For a given number \(n\) you need to make a permutation \(p\) such that t... | The first line of input data contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The description of the test cases follows.Each test case consists of f single line containing one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)).It is guaranteed that the sum of \(n\) over all test cases does no... | For each test case, print on a separate line: any funny permutation \(p\) of length \(n\); or the number -1 if the permutation you are looking for does not exist. | The first test case is explained in the problem statement.In the second test case, it is not possible to make the required permutation: permutations \([1, 2, 3]\), \([1, 3, 2]\), \([2, 1, 3]\), \([3, 2, 1]\) have fixed points, and in \([2, 3, 1]\) and \([3, 1, 2]\) the first condition is met not for all positions. | Input: 543752 | Output: 3 4 2 1 -1 6 7 4 5 3 2 1 5 4 1 2 3 2 1 | Beginner | 2 | 1,307 | 346 | 162 | 17 |
778 | A | 778A | A. String Game | 1,700 | binary search; greedy; strings | Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her.Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain or... | The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 β€ |p| < |t| β€ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t.Next line contains a permutation a1, a2, ..., a|t| of letter indi... | Print a single integer number, the maximum number of letters that Nastya can remove. | In the first sample test sequence of removing made by Nastya looks like this:""ababcba"" ""ababcba"" ""ababcba"" ""ababcba"" Nastya can not continue, because it is impossible to get word ""abb"" from word ""ababcba"".So, Nastya will remove only three letters. | Input: ababcbaabb5 3 4 1 7 6 2 | Output: 3 | Medium | 3 | 1,147 | 422 | 84 | 7 |
1,187 | F | 1187F | F. Expected Square Beauty | 2,500 | dp; math; probabilities | Let \(x\) be an array of integers \(x = [x_1, x_2, \dots, x_n]\). Let's define \(B(x)\) as a minimal size of a partition of \(x\) into subsegments such that all elements in each subsegment are equal. For example, \(B([3, 3, 6, 1, 6, 6, 6]) = 4\) using next partition: \([3, 3\ |\ 6\ |\ 1\ |\ 6, 6, 6]\).Now you don't hav... | The first line contains the single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the size of the array \(x\).The second line contains \(n\) integers \(l_1, l_2, \dots, l_n\) (\(1 \le l_i \le 10^9\)).The third line contains \(n\) integers \(r_1, r_2, \dots, r_n\) (\(l_i \le r_i \le 10^9\)). | Print the single integer β \(E((B(x))^2)\) as \(P \cdot Q^{-1} \mod 10^9 + 7\). | Let's describe all possible values of \(x\) for the first sample: \([1, 1, 1]\): \(B(x) = 1\), \(B^2(x) = 1\); \([1, 1, 2]\): \(B(x) = 2\), \(B^2(x) = 4\); \([1, 1, 3]\): \(B(x) = 2\), \(B^2(x) = 4\); \([1, 2, 1]\): \(B(x) = 3\), \(B^2(x) = 9\); \([1, 2, 2]\): \(B(x) = 2\), \(B^2(x) = 4\); \([1, 2, 3]\): \(B(x) = 3\), ... | Input: 3 1 1 1 1 2 3 | Output: 166666673 | Expert | 3 | 719 | 291 | 79 | 11 |
1,044 | E | 1044E | E. Grid Sort | 3,100 | implementation | You are given an \(n \times m\) grid. Each grid cell is filled with a unique integer from \(1\) to \(nm\) so that each integer appears exactly once.In one operation, you can choose an arbitrary cycle of the grid and move all integers along that cycle one space over. Here, a cycle is any sequence that satisfies the foll... | The first line contains two integers \(n\) and \(m\) (\(3 \leq n,m \leq 20\)) β the dimensions of the grid.Each of the next \(n\) lines contains \(m\) integers \(x_{i,1}, x_{i,2}, \ldots, x_{i, m}\) (\(1 \leq x_{i,j} \leq nm\)), denoting the values of the block in row \(i\) and column \(j\). It is guaranteed that all \... | First, print a single integer \(k\), the number of operations (\(k \geq 0\)).On each of the next \(k\) lines, print a cycle as follows:$$$\(s\ y_1\ y_2\ \ldots\ y_s\)\(Here, \)s\( is the number of blocks to move (\)s \geq 4\(). Here we have block \)y_1\( moving to where block \)y_2\( is, block \)y_2\( moving to where b... | The first sample is the case in the statement. Here, we can use the cycle in reverse order to sort the grid. | Input: 3 34 1 27 6 38 5 9 | Output: 18 1 4 7 8 5 6 3 2 | Master | 1 | 1,255 | 344 | 462 | 10 |
1,945 | G | 1945G | G. Cook and Porridge | 2,500 | binary search; constructive algorithms; data structures; implementation | Finally, lunchtime!\(n\) schoolchildren have lined up in a long queue at the cook's tent for porridge. The cook will be serving porridge for \(D\) minutes. The schoolchild standing in the \(i\)-th position in the queue has a priority of \(k_i\) and eats one portion of porridge in \(s_i\) minutes.At the beginning of eac... | 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. This is followed by a description of the test cases.The first line of each test case contains two integers \(n\) and \(D\) (\(1 \le n \le 2 \cdot 10^5\), \(1 \le D \le 3\cdot 10^5\... | For each test case, output the minimum number of minutes after which each schoolchild will receive porridge at least once. If this does not happen within the break time, output \(-1\). | Input: 73 32 23 12 35 1010 37 111 35 16 15 204 27 28 51 53 15 171 38 28 32 21 15 148 24 21 38 36 41 114 55 148 24 21 38 36 4 | Output: 3 -1 12 6 6 1 6 | Expert | 4 | 2,082 | 908 | 184 | 19 | |
29 | A | 29A | A. Spit Problem | 1,000 | brute force | In a Berland's zoo there is an enclosure with camels. It is known that camels like to spit. Bob watched these interesting animals for the whole day and registered in his notepad where each animal spitted. Now he wants to know if in the zoo there are two camels, which spitted at each other. Help him to solve this task.T... | The first line contains integer n (1 β€ n β€ 100) β the amount of camels in the zoo. Each of the following n lines contains two integers xi and di ( - 104 β€ xi β€ 104, 1 β€ |di| β€ 2Β·104) β records in Bob's notepad. xi is a position of the i-th camel, and di is a distance at which the i-th camel spitted. Positive values of ... | If there are two camels, which spitted at each other, output YES. Otherwise, output NO. | Input: 20 11 -1 | Output: YES | Beginner | 1 | 485 | 445 | 87 | 0 | |
1,734 | C | 1734C | C. Removing Smallest Multiples | 1,200 | greedy; math | You are given a set \(S\), which contains the first \(n\) positive integers: \(1, 2, \ldots, n\).You can perform the following operation on \(S\) any number of times (possibly zero): Choose a positive integer \(k\) where \(1 \le k \le n\), such that there exists a multiple of \(k\) in \(S\). Then, delete the smallest m... | The first line of the input contains a single integer \(t\) (\(1 \le t \le 10\,000\)) β the number of test cases. The description of the test cases follows.The first line contains a single positive integer \(n\) (\(1 \le n \le 10^6\)).The second line of each test case contains a binary string of length \(n\), describin... | For each test case, output one non-negative integer β the minimum possible total cost of operations such that \(S\) would be transformed into \(T\). | In the first test case, we shall not perform any operations as \(S\) is already equal to \(T\), which is the set \(\{1, 2, 3, 4, 5, 6\}\).In the second test case, initially, \(S = \{1, 2, 3, 4, 5, 6, 7\}\), and \(T = \{1, 2, 4, 7\}\). We shall perform the following operations: Choose \(k=3\), then delete \(3\) from \(S... | Input: 6611111171101001400004001081001010115110011100101100 | Output: 0 11 4 4 17 60 | Easy | 2 | 603 | 528 | 148 | 17 |
1,694 | A | 1694A | A. Creep | 800 | greedy; implementation | Define the score of some binary string \(T\) as the absolute difference between the number of zeroes and ones in it. (for example, \(T=\) 010001 contains \(4\) zeroes and \(2\) ones, so the score of \(T\) is \(|4-2| = 2\)).Define the creepiness of some binary string \(S\) as the maximum score among all of its prefixes ... | The first line contains a single integer \(t\) \((1\le t\le 1000)\) β the number of test cases. The description of the test cases follows.The only line of each test case contains two integers \(a\) and \(b\) (\( 1 \le a, b \le 100\)) β the numbers of zeroes and ones correspondingly. | For each test case, print a binary string consisting of \(a\) zeroes and \(b\) ones with the minimum possible creepiness. If there are multiple answers, print any of them. | In the first test case, the score of \(S[1 \ldots 1]\) is \(1\), and the score of \(S[1 \ldots 2]\) is \(0\).In the second test case, the minimum possible creepiness is \(1\) and one of the other answers is 101.In the third test case, the minimum possible creepiness is \(3\) and one of the other answers is 0001100. | Input: 5 1 1 1 2 5 2 4 5 3 7 | Output: 10 011 0011000 101010101 0001111111 | Beginner | 2 | 643 | 283 | 171 | 16 |
2,039 | C1 | 2039C1 | C1. Shohag Loves XOR (Easy Version) | 1,200 | bitmasks; brute force; math; number theory | This is the easy version of the problem. The differences between the two versions are highlighted in bold. You can only make hacks if both versions of the problem are solved.Shohag has two integers \(x\) and \(m\). Help him count the number of integers \(1 \le y \le m\) such that \(\mathbf{x \neq y}\) and \(x \oplus y\... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first and only line of each test case contains two space-separated integers \(x\) and \(m\) (\(1 \le x \le 10^6\), \(1 \le m \le 10^{18}\)).It is guaranteed that the sum of \(x\) over all test cases does not exceed \(10... | For each test case, print a single integer β the number of suitable \(y\). | In the first test case, for \(x = 6\), there are \(3\) valid values for \(y\) among the integers from \(1\) to \(m = 9\), and they are \(4\), \(5\), and \(7\). \(y = 4\) is valid because \(x \oplus y = 6 \oplus 4 = 2\) and \(2\) is a divisor of both \(x = 6\) and \(y = 4\). \(y = 5\) is valid because \(x \oplus y = 6 \... | Input: 56 95 72 36 44 1 | Output: 3 2 1 1 0 | Easy | 4 | 553 | 325 | 74 | 20 |
1,523 | D | 1523D | D. Love-Hate | 2,400 | bitmasks; brute force; dp; probabilities | William is hosting a party for \(n\) of his trader friends. They started a discussion on various currencies they trade, but there's an issue: not all of his trader friends like every currency. They like some currencies, but not others.For each William's friend \(i\) it is known whether he likes currency \(j\). There ar... | The first line contains three integers \(n, m\) and \(p\) \((1 \le n \le 2 \cdot 10^5, 1 \le p \le m \le 60, 1 \le p \le 15)\), which is the number of trader friends, the number of currencies, the maximum number of currencies each friend can like.Each of the next \(n\) lines contain \(m\) characters. The \(j\)-th chara... | Print a string of length \(m\), which defines the subset of currencies of the maximum size, which are liked by at least half of all friends. Currencies belonging to this subset must be signified by the character \(1\).If there are multiple answers, print any. | In the first sample test case only the first currency is liked by at least \(\lceil \frac{3}{2} \rceil = 2\) friends, therefore it's easy to demonstrate that a better answer cannot be found.In the second sample test case the answer includes \(2\) currencies and will be liked by friends \(1\), \(2\), and \(5\). For this... | Input: 3 4 3 1000 0110 1001 | Output: 1000 | Expert | 4 | 685 | 489 | 259 | 15 |
135 | E | 135E | E. Weak Subsequence | 3,000 | combinatorics | Little Petya very much likes strings. Recently he has received a voucher to purchase a string as a gift from his mother. The string can be bought in the local shop. One can consider that the shop has all sorts of strings over the alphabet of fixed size. The size of the alphabet is equal to k. However, the voucher has a... | The first line contains two integers k (1 β€ k β€ 106) and w (2 β€ w β€ 109) β the alphabet size and the required length of the maximum substring that also is the weak subsequence, correspondingly. | Print a single number β the number of strings Petya can buy using the voucher, modulo 1000000007 (109 + 7). If there are infinitely many such strings, print ""-1"" (without the quotes). | In the first sample Petya can buy the following strings: aaa, aab, abab, abb, abba, baa, baab, baba, bba, bbb. | Input: 2 2 | Output: 10 | Master | 1 | 1,076 | 193 | 185 | 1 |
1,523 | H | 1523H | H. Hopping Around the Array | 3,500 | data structures; dp | William really wants to get a pet. Since his childhood he dreamt about getting a pet grasshopper. William is being very responsible about choosing his pet, so he wants to set up a trial for the grasshopper!The trial takes place on an array \(a\) of length \(n\), which defines lengths of hops for each of \(n\) cells. A ... | The first line contains two integers \(n\) and \(q\) (\(1 \le n, q \le 20000\)), the length of the array and the number of queries respectively.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le n\)) β the elements of the array.The following \(q\) lines contain queries: each line contain... | For each query print a single number in a new line β the response to a query. | For the second query the process occurs like this: For the third query the process occurs like this: | Input: 9 5 1 1 2 1 3 1 2 1 1 1 1 0 2 5 1 5 9 1 2 8 2 1 9 4 | Output: 0 2 1 2 2 | Master | 2 | 1,167 | 506 | 77 | 15 |
233 | B | 233B | B. Non-square Equation | 1,400 | binary search; brute force; math | Let's consider equation:x2 + s(x)Β·x - n = 0, where x, n are positive integers, s(x) is the function, equal to the sum of digits of number x in the decimal number system.You are given an integer n, find the smallest positive integer root of equation x, or else determine that there are no such roots. | A single line contains integer n (1 β€ n β€ 1018) β the equation parameter.Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier. | Print -1, if the equation doesn't have integer positive roots. Otherwise print such smallest integer x (x > 0), that the equation given in the statement holds. | In the first test case x = 1 is the minimum root. As s(1) = 1 and 12 + 1Β·1 - 2 = 0.In the second test case x = 10 is the minimum root. As s(10) = 1 + 0 = 1 and 102 + 1Β·10 - 110 = 0.In the third test case the equation has no roots. | Input: 2 | Output: 1 | Easy | 3 | 299 | 216 | 159 | 2 |
894 | B | 894B | B. Ralph And His Magic Field | 1,800 | combinatorics; constructive algorithms; math; number theory | Ralph has a magic field which is divided into n Γ m blocks. That is to say, there are n rows and m columns on the field. Ralph can put an integer in each block. However, the magic field doesn't always work properly. It works only if the product of integers in each row and each column equals to k, where k is either 1 or... | The only line contains three integers n, m and k (1 β€ n, m β€ 1018, k is either 1 or -1). | Print a single number denoting the answer modulo 1000000007. | In the first example the only way is to put -1 into the only block.In the second example the only way is to put 1 into every block. | Input: 1 1 -1 | Output: 1 | Medium | 4 | 787 | 88 | 60 | 8 |
888 | F | 888F | F. Connecting Vertices | 2,500 | dp; graphs | There are n points marked on the plane. The points are situated in such a way that they form a regular polygon (marked points are its vertices, and they are numbered in counter-clockwise order). You can draw n - 1 segments, each connecting any two marked points, in such a way that all points have to be connected with e... | The first line contains one number n (3 β€ n β€ 500) β the number of marked points.Then n lines follow, each containing n elements. ai, j (j-th element of line i) is equal to 1 iff you can connect points i and j directly (otherwise ai, j = 0). It is guaranteed that for any pair of points ai, j = aj, i, and for any point ... | Print the number of ways to connect points modulo 109 + 7. | Input: 30 0 10 0 11 1 0 | Output: 1 | Expert | 2 | 1,038 | 330 | 58 | 8 | |
1,218 | C | 1218C | C. Jumping Transformers | 2,600 | dp | You, the mighty Blackout, are standing in the upper-left \((0,0)\) corner of \(N\)x\(M\) matrix. You must move either right or down each second. There are \(K\) transformers jumping around the matrix in the following way. Each transformer starts jumping from position \((x,y)\), at time \(t\), and jumps to the next posi... | In the first line, integers \(N\),\(M\) (\(1 \leq N, M \leq 500\)), representing size of the matrix, and \(K\) (\(0 \leq K \leq 5*10^5\)) , the number of jumping transformers.In next \(K\) lines, for each transformer, numbers \(x\), \(y\), \(d\) (\(d \geq 1\)), \(t\) (\(0 \leq t \leq N+M-2\)), and \(e\) (\(0 \leq e \le... | Print single integer, the minimum possible amount of energy wasted, for Blackout to arrive at bottom-right corner. | If Blackout takes the path from (0, 0) to (2, 0), and then from (2, 0) to (2, 2) he will need to kill the first and third transformer for a total energy cost of 9. There exists no path with less energy value. | Input: 3 3 5 0 1 1 0 7 1 1 1 0 10 1 1 1 1 2 1 1 1 2 2 0 1 1 2 3 | Output: 9 | Expert | 1 | 1,022 | 600 | 114 | 12 |
1,991 | E | 1991E | E. Coloring Game | 1,900 | constructive algorithms; dfs and similar; games; graphs; greedy; interactive | This is an interactive problem.Consider an undirected connected graph consisting of \(n\) vertices and \(m\) edges. Each vertex can be colored with one of three colors: \(1\), \(2\), or \(3\). Initially, all vertices are uncolored.Alice and Bob are playing a game consisting of \(n\) rounds. In each round, the following... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. The description of test cases follows.The first line of each test case contains two integers \(n\), \(m\) (\(1 \le n \le 10^4\), \(n - 1 \le m \le \min(\frac{n \cdot (n - 1)}{2}, 10^... | Note that the sample test cases are example games and do not necessarily represent the optimal strategy for both players.In the first test case, you choose to play as Alice. Alice chooses two colors: \(3\) and \(1\). Bob chooses vertex \(3\) and colors it with color \(1\). Alice chooses two colors: \(1\) and \(2\). Bob... | Input: 2 3 3 1 2 2 3 3 1 3 1 2 2 1 1 4 4 1 2 2 3 3 4 4 1 2 3 1 2 2 1 3 1 | Output: Alice 3 1 1 2 2 1 Bob 1 2 2 1 4 1 3 3 | Hard | 6 | 669 | 737 | 0 | 19 | |
1,772 | A | 1772A | A. A+B? | 800 | implementation | You are given an expression of the form \(a{+}b\), where \(a\) and \(b\) are integers from \(0\) to \(9\). You have to evaluate it and print the result. | The first line contains one integer \(t\) (\(1 \le t \le 100\)) β the number of test cases.Each test case consists of one line containing an expression of the form \(a{+}b\) (\(0 \le a, b \le 9\), both \(a\) and \(b\) are integers). The integers are not separated from the \(+\) sign. | For each test case, print one integer β the result of the expression. | Input: 44+20+03+78+9 | Output: 6 0 10 17 | Beginner | 1 | 152 | 284 | 69 | 17 | |
186 | B | 186B | B. Growing Mushrooms | 1,200 | greedy; sortings | Each year in the castle of Dwarven King there is a competition in growing mushrooms among the dwarves. The competition is one of the most prestigious ones, and the winner gets a wooden salad bowl. This year's event brought together the best mushroom growers from around the world, so we had to slightly change the rules ... | The first input line contains four integer numbers n, t1, t2, k (1 β€ n, t1, t2 β€ 1000; 1 β€ k β€ 100) β the number of participants, the time before the break, the time after the break and the percentage, by which the mushroom growth drops during the break, correspondingly.Each of the following n lines contains two intege... | Print the final results' table: n lines, each line should contain the number of the corresponding dwarf and the final maximum height of his mushroom with exactly two digits after the decimal point. The answer will be considered correct if it is absolutely accurate. | First example: for each contestant it is optimal to use firstly speed 2 and afterwards speed 4, because 2Β·3Β·0.5 + 4Β·3 > 4Β·3Β·0.5 + 2Β·3. | Input: 2 3 3 502 44 2 | Output: 1 15.002 15.00 | Easy | 2 | 2,183 | 461 | 265 | 1 |
715 | E | 715E | E. Complete the Permutations | 3,400 | combinatorics; fft; graphs; math | ZS the Coder is given two permutations p and q of {1, 2, ..., n}, but some of their elements are replaced with 0. The distance between two permutations p and q is defined as the minimum number of moves required to turn p into q. A move consists of swapping exactly 2 elements of p.ZS the Coder wants to determine the num... | The first line of the input contains a single integer n (1 β€ n β€ 250) β the number of elements in the permutations.The second line contains n integers, p1, p2, ..., pn (0 β€ pi β€ n) β the permutation p. It is guaranteed that there is at least one way to replace zeros such that p is a permutation of {1, 2, ..., n}.The th... | Print n integers, i-th of them should denote the answer for k = i - 1. Since the answer may be quite large, and ZS the Coder loves weird primes, print them modulo 998244353 = 223Β·7Β·17 + 1, which is a prime. | In the first sample case, there is the only way to replace zeros so that it takes 0 swaps to convert p into q, namely p = (1, 2, 3), q = (1, 2, 3).There are two ways to replace zeros so that it takes 1 swap to turn p into q. One of these ways is p = (1, 2, 3), q = (3, 2, 1), then swapping 1 and 3 from p transform it in... | Input: 31 0 00 2 0 | Output: 1 2 1 | Master | 4 | 581 | 512 | 206 | 7 |
1,485 | C | 1485C | C. Floor and Mod | 1,700 | binary search; brute force; math; number theory | A pair of positive integers \((a,b)\) is called special if \(\lfloor \frac{a}{b} \rfloor = a \bmod b\). Here, \(\lfloor \frac{a}{b} \rfloor\) is the result of the integer division between \(a\) and \(b\), while \(a \bmod b\) is its remainder.You are given two integers \(x\) and \(y\). Find the number of special pairs \... | The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases.The only line of the description of each test case contains two integers \(x\), \(y\) (\(1 \le x,y \le 10^9\)). | For each test case print the answer on a single line. | In the first test case, the only special pair is \((3, 2)\).In the second test case, there are no special pairs.In the third test case, there are two special pairs: \((3, 2)\) and \((4, 3)\). | Input: 9 3 4 2 100 4 3 50 3 12 4 69 420 12345 6789 123456 789 12345678 9 | Output: 1 0 2 3 5 141 53384 160909 36 | Medium | 4 | 382 | 207 | 53 | 14 |
107 | A | 107A | A. Dorm Water Supply | 1,400 | dfs and similar; graphs | The German University in Cairo (GUC) dorm houses are numbered from 1 to n. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only in this direction and not vice versa), and diameter (which characterizes the maximal amount of water it can handle).For each house, there... | The first line contains two space-separated integers n and p (1 β€ n β€ 1000, 0 β€ p β€ n) β the number of houses and the number of pipes correspondingly. Then p lines follow β the description of p pipes. The i-th line contains three integers ai bi di, indicating a pipe of diameter di going from house ai to house bi (1 β€ a... | Print integer t in the first line β the number of tank-tap pairs of houses.For the next t lines, print 3 integers per line, separated by spaces: tanki, tapi, and diameteri, where tanki β tapi (1 β€ i β€ t). Here tanki and tapi are indexes of tank and tap houses respectively, and diameteri is the maximum amount of water t... | Input: 3 21 2 102 3 20 | Output: 11 3 10 | Easy | 2 | 1,325 | 468 | 399 | 1 | |
204 | D | 204D | D. Little Elephant and Retro Strings | 2,400 | dp | The Little Elephant has found a ragged old black-and-white string s on the attic.The characters of string s are numbered from the left to the right from 1 to |s|, where |s| is the length of the string. Let's denote the i-th character of string s as si. As the string is black-and-white, each character of the string is e... | The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 106). The second line contains string s. String s has length n and only consists of characters ""W"", ""B"" and ""X"". | On a single line print an integer β the answer to the problem modulo 1000000007 (109 + 7). | Input: 3 2XXX | Output: 0 | Expert | 1 | 1,510 | 192 | 90 | 2 | |
1,720 | D2 | 1720D2 | D2. Xor-Subsequence (hard version) | 2,400 | bitmasks; data structures; dp; strings; trees | It is the hard version of the problem. The only difference is that in this version \(a_i \le 10^9\).You are given an array of \(n\) integers \(a_0, a_1, a_2, \ldots a_{n - 1}\). Bryap wants to find the longest beautiful subsequence in the array.An array \(b = [b_0, b_1, \ldots, b_{m-1}]\), where \(0 \le b_0 < b_1 < \ld... | The first line contains a single integer \(t\) (\(1 \leq t \leq 10^5\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \leq n \leq 3 \cdot 10^5\)) β the length of the array.The second line of each test case contains \(n\) integ... | For each test case print a single integer β the length of the longest beautiful subsequence. | In the first test case, we can pick the whole array as a beautiful subsequence because \(1 \oplus 1 < 2 \oplus 0\).In the second test case, we can pick elements with indexes \(1\), \(2\) and \(4\) (in \(0\) indexation). For this elements holds: \(2 \oplus 2 < 4 \oplus 1\) and \(4 \oplus 4 < 1 \oplus 2\). | Input: 321 255 2 4 3 1103 8 8 2 9 1 6 2 8 3 | Output: 2 3 6 | Expert | 5 | 868 | 495 | 92 | 17 |
1,684 | B | 1684B | B. Z mod X = C | 800 | constructive algorithms; math | You are given three positive integers \(a\), \(b\), \(c\) (\(a < b < c\)). You have to find three positive integers \(x\), \(y\), \(z\) such that:$$$\(x \bmod y = a,\)\( \)\(y \bmod z = b,\)\( \)\(z \bmod x = c.\)\(Here \)p \bmod q\( denotes the remainder from dividing \)p\( by \)q$$$. It is possible to show that for s... | The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10\,000\)) β the number of test cases. Description of the test cases follows.Each test case contains a single line with three integers \(a\), \(b\), \(c\) (\(1 \le a < b < c \le 10^8\)). | For each test case output three positive integers \(x\), \(y\), \(z\) (\(1 \le x, y, z \le 10^{18}\)) such that \(x \bmod y = a\), \(y \bmod z = b\), \(z \bmod x = c\).You can output any correct answer. | In the first test case:$$$\(x \bmod y = 12 \bmod 11 = 1;\)\[\(y \bmod z = 11 \bmod 4 = 3;\)\]\(z \bmod x = 4 \bmod 12 = 4.\)$$$ | Input: 4 1 3 4 127 234 421 2 7 8 59 94 388 | Output: 12 11 4 1063 234 1484 25 23 8 2221 94 2609 | Beginner | 2 | 361 | 291 | 202 | 16 |
1,425 | F | 1425F | F. Flamingoes of Mystery | 1,400 | interactive | This is an interactive problem. You have to use a flush operation right after printing each line. For example, in C++ you should use the function fflush(stdout), in Java β System.out.flush(), in Pascal β flush(output) and in Python β sys.stdout.flush().Mr. Chanek wants to buy a flamingo to accompany his chickens on his... | Use standard input to read the responses of your questions.Initially, the judge will give an integer \(N\) \((3 \le N \le 10^3)\), the number of cages, and the number of coins Mr. Chanek has.For each of your questions, the jury will give an integer that denotes the number of flamingoes from cage \(L\) to \(R\) inclusiv... | To ask questions, your program must use standard output.Then, you can ask at most \(N\) questions. Questions are asked in the format ""? L R"", (\(1 \le L < R \le N\)). To guess the flamingoes, print a line that starts with ""!"" followed by \(N\) integers where the \(i\)-th integer denotes the number of flamingo in ca... | In the sample input, the correct flamingoes amount is \([1, 4, 4, 6, 7, 8]\). | Input: 6 5 15 10 | Output: ? 1 2 ? 5 6 ? 3 4 ! 1 4 4 6 7 8 | Easy | 1 | 972 | 534 | 467 | 14 |
1,547 | B | 1547B | B. Alphabetical Strings | 800 | greedy; implementation; strings | A string \(s\) of length \(n\) (\(1 \le n \le 26\)) is called alphabetical if it can be obtained using the following algorithm: first, write an empty string to \(s\) (i.e. perform the assignment \(s\) := """"); then perform the next step \(n\) times; at the \(i\)-th step take \(i\)-th lowercase letter of the Latin alph... | The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Then \(t\) test cases follow.Each test case is written on a separate line that contains one string \(s\). String \(s\) consists of lowercase letters of the Latin alphabet and has a length between \(1\) and \(26\), inclusive. | Output \(t\) lines, each of them must contain the answer to the corresponding test case. Output YES if the given string \(s\) is alphabetical and NO otherwise.You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive answer). | The example contains test cases from the main part of the condition. | Input: 11 a ba ab bac ihfcbadeg z aa ca acb xyz ddcba | Output: YES YES YES YES YES NO NO NO NO NO NO | Beginner | 3 | 1,070 | 317 | 282 | 15 |
2,080 | A | 2080A | 3,200 | *special; constructive algorithms; graphs | Master | 3 | 0 | 0 | 0 | 20 | ||||||
769 | B | 769B | B. News About Credit | 1,200 | *special; greedy; two pointers | Polycarp studies at the university in the group which consists of n students (including himself). All they are registrated in the social net ""TheContacnt!"".Not all students are equally sociable. About each student you know the value ai β the maximum number of messages which the i-th student is agree to send per day. ... | The first line contains the positive integer n (2 β€ n β€ 100) β the number of students. The second line contains the sequence a1, a2, ..., an (0 β€ ai β€ 100), where ai equals to the maximum number of messages which can the i-th student agree to send. Consider that Polycarp always has the number 1. | Print -1 to the first line if it is impossible to inform all students about credit. Otherwise, in the first line print the integer k β the number of messages which will be sent. In each of the next k lines print two distinct integers f and t, meaning that the student number f sent the message with news to the student n... | In the first test Polycarp (the student number 1) can send the message to the student number 2, who after that can send the message to students number 3 and 4. Thus, all students knew about the credit. | Input: 41 2 1 0 | Output: 31 22 42 3 | Easy | 3 | 1,171 | 296 | 619 | 7 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.