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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
122 | A | 122A | A. Lucky Division | 1,000 | brute force; number theory | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.Petya calls a number almost lucky if it could be evenly divided by some lucky number. Help him find ou... | The single line contains an integer n (1 ≤ n ≤ 1000) — the number that needs to be checked. | In the only line print ""YES"" (without the quotes), if number n is almost lucky. Otherwise, print ""NO"" (without the quotes). | Note that all lucky numbers are almost lucky as any number is evenly divisible by itself.In the first sample 47 is a lucky number. In the second sample 16 is divisible by 4. | Input: 47 | Output: YES | Beginner | 2 | 360 | 91 | 127 | 1 |
45 | G | 45G | G. Prime Problem | 2,200 | number theory | In Berland prime numbers are fashionable — the respectable citizens dwell only on the floors with numbers that are prime numbers. The numismatists value particularly high the coins with prime nominal values. All the prime days are announced holidays!Yet even this is not enough to make the Berland people happy. On the m... | The single input line contains an integer n (2 ≤ n ≤ 6000) — the number of houses on the main streets of the capital. | Print the sequence of n numbers, where the i-th number stands for the number of color for house number i. Number the colors consecutively starting from 1. Any painting order is allowed. If there are several solutions to that problem, print any of them. If there's no such way of painting print the single number -1. | Input: 8 | Output: 1 2 2 1 1 1 1 2 | Hard | 1 | 1,160 | 117 | 315 | 0 | |
808 | G | 808G | G. Anthem of Berland | 2,300 | dp; strings | Berland has a long and glorious history. To increase awareness about it among younger citizens, King of Berland decided to compose an anthem.Though there are lots and lots of victories in history of Berland, there is the one that stand out the most. King wants to mention it in the anthem as many times as possible.He ha... | The first line contains string of small Latin letters and question marks s (1 ≤ |s| ≤ 105).The second line contains string of small Latin letters t (1 ≤ |t| ≤ 105).Product of lengths of strings |s|·|t| won't exceed 107. | Output the maximum number of occurrences of string t you can achieve by replacing all the question marks in string s with small Latin letters. | In the first example the resulting string s is ""winlosewinwinlwinwin""In the second example the resulting string s is ""glorytoreorand"". The last letter of the string can be arbitrary.In the third example occurrences of string t are overlapping. String s with maximal number of occurrences of t is ""abcabcab"". | Input: winlose???winl???w??win | Output: 5 | Expert | 2 | 865 | 219 | 142 | 8 |
1,769 | B1 | 1769B1 | B1. Копирование файлов I | 1,000 | *special; brute force; implementation; math | В этой версии задачи размеры копируемых файлов не превышают \(1000\) байт.Вы копируете с одного сервера на другой \(n\) файлов размером \(a_1, a_2, \ldots, a_n\) байт. Файлы копируются последовательно в заданном порядке.При копировании вы видите два прогресс-бара: первый показывает процент скопированных данных в текуще... | В первой строке задано одно целое число \(n\) (\(1 \le n \le 100\)) — число копируемых файлов.Во второй строке заданы \(n\) целых чисел \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 1000\)) — размеры файлов в байтах в том порядке, в котором они будут копироваться. | Выведите в возрастающем порядке все числа от \(0\) до \(100\) включительно такие, что существует момент времени, в который на обоих прогресс-барах одновременно показывается это число. | В первом тесте копируется всего один файл, поэтому оба прогресс-бара всегда показывают одинаковые значения.Во втором тесте первый прогресс-бар сразу же уйдёт вперёд, потом сбросится в ноль и начнёт догонять второй прогресс-бар заново. В конце копирования прогресс-бары некоторое время будут показывать одно и то же число... | Input: 1 6 | Output: 0 16 33 50 66 83 100 | Beginner | 4 | 1,076 | 261 | 183 | 17 |
1,924 | E | 1924E | E. Paper Cutting Again | 3,100 | combinatorics; probabilities | There is a rectangular sheet of paper with initial height \(n\) and width \(m\). Let the current height and width be \(h\) and \(w\) respectively. We introduce a \(xy\)-coordinate system so that the four corners of the sheet are \((0, 0), (w, 0), (0, h)\), and \((w, h)\). The sheet can then be cut along the lines \(x =... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 57000\)). Description of the test cases follows.The first line of each test case contains 3 integers \(n\), \(m\), and \(k\) (\(1 \le n, m \le 10^6\), \(2 \le k \le 10^{12}\)).It is guaranteed that the sum of \... | For each test case, print one integer — the answer to the problem. | For the first test case, the area is already less than \(10\) so no cuts are required.For the second test case, the area is exactly \(8\) so any one of the \(4\) possible cuts would make the area strictly less than \(8\).For the third test case, the final answer is \(\frac{17}{6} = 833\,333\,342\bmod (10^9+7)\).For the... | Input: 42 4 102 4 82 4 22 4 6 | Output: 0 1 833333342 250000003 | Master | 2 | 842 | 389 | 66 | 19 |
1,005 | D | 1005D | D. Polycarp and Div 3 | 1,500 | dp; greedy; number theory | Polycarp likes numbers that are divisible by 3.He has a huge number \(s\). Polycarp wants to cut from it the maximum number of numbers that are divisible by \(3\). To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after \(m\) such cuts, there will be \(m+1\) parts ... | The first line of the input contains a positive integer \(s\). The number of digits of the number \(s\) is between \(1\) and \(2\cdot10^5\), inclusive. The first (leftmost) digit is not equal to 0. | Print the maximum number of numbers divisible by \(3\) that Polycarp can get by making vertical cuts in the given number \(s\). | In the first example, an example set of optimal cuts on the number is 3|1|21.In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by \(3\).In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and \(33\) ... | Input: 3121 | Output: 2 | Medium | 3 | 1,054 | 197 | 127 | 10 |
1,607 | B | 1607B | B. Odd Grasshopper | 900 | math | The grasshopper is located on the numeric axis at the point with coordinate \(x_0\).Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate \(x\) with a distance \(d\) to the left moves the grasshopper to a point with a coordinate \(x - d\), while jumpi... | The first line of input contains an integer \(t\) (\(1 \leq t \leq 10^4\)) — the number of test cases.Each of the following \(t\) lines contains two integers \(x_0\) (\(-10^{14} \leq x_0 \leq 10^{14}\)) and \(n\) (\(0 \leq n \leq 10^{14}\)) — the coordinate of the grasshopper's initial position and the number of jumps. | Print exactly \(t\) lines. On the \(i\)-th line print one integer — the answer to the \(i\)-th test case — the coordinate of the point the grasshopper will be at after making \(n\) jumps from the point \(x_0\). | The first two test cases in the example correspond to the first two jumps from the point \(x_0 = 0\). Since \(0\) is an even number, the first jump of length \(1\) is made to the left, and the grasshopper ends up at the point \(0 - 1 = -1\).Then, since \(-1\) is an odd number, a jump of length \(2\) is made to the righ... | Input: 9 0 1 0 2 10 10 10 99 177 13 10000000000 987654321 -433494437 87178291199 1 0 -1 1 | Output: -1 1 11 110 190 9012345679 -87611785637 1 0 | Beginner | 1 | 1,314 | 320 | 210 | 16 |
1,950 | D | 1950D | D. Product of Binary Decimals | 1,100 | brute force; dp; implementation; number theory | Let's call a number a binary decimal if it is a positive integer and all digits in its decimal notation are either \(0\) or \(1\). For example, \(1\,010\,111\) is a binary decimal, while \(10\,201\) and \(787\,788\) are not.Given a number \(n\), you are asked whether or not it is possible to represent \(n\) as a produc... | The first line contains a single integer \(t\) (\(1 \leq t \leq 5 \cdot 10^4\)) — the number of test cases.The only line of each test case contains a single integer \(n\) (\(1 \leq n \leq 10^5\)). | For each test case, output ""YES"" (without quotes) if \(n\) can be represented as a product of binary decimals, 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). | The first five test cases can be represented as a product of binary decimals as follows: \(121 = 11 \times 11\). \(1 = 1\) is already a binary decimal. \(14\,641 = 11 \times 11 \times 11 \times 11\). \(12\,221 = 11 \times 11 \times 101\). \(10\,110 = 10\,110\) is already a binary decimal. | Input: 111211146411222110110100000991122024124211001 | Output: YES YES YES YES YES YES NO NO NO NO YES | Easy | 4 | 373 | 196 | 292 | 19 |
530 | A | 530A | A. Quadratic equation | 1,500 | *special | You are given a quadratic equation with integer coefficients A * X2 + B * X + C = 0. It is guaranteed that A ≠ 0 and that the equation has at least one real root. Output the roots of the equation. | The only line of input contains integers A, B and C ( - 1000 ≤ A, B, C ≤ 1000, A ≠ 0), separated by spaces. | Output the roots of the equation in increasing order. If the equation has a single root of multiplicity 2, output it once. The root is considered to be correct if its absolute or relative error does not exceed 10 - 4. | Input: 1 -2 1 | Output: 1 | Medium | 1 | 196 | 107 | 217 | 5 | |
858 | E | 858E | E. Tests Renumeration | 2,200 | greedy; implementation | The All-Berland National Olympiad in Informatics has just ended! Now Vladimir wants to upload the contest from the Olympiad as a gym to a popular Codehorses website.Unfortunately, the archive with Olympiad's data is a mess. For example, the files with tests are named arbitrary without any logic.Vladimir wants to rename... | The first line contains single integer n (1 ≤ n ≤ 105) — the number of files with tests.n lines follow, each describing a file with test. Each line has a form of ""name_i type_i"", where ""name_i"" is the filename, and ""type_i"" equals ""1"", if the i-th file contains an example test, and ""0"" if it contains a regula... | In the first line print the minimum number of lines in Vladimir's script file.After that print the script file, each line should be ""move file_1 file_2"", where ""file_1"" is an existing at the moment of this line being run filename, and ""file_2"" — is a string of digits and small English letters with length from 1 t... | Input: 501 02 12extra 03 199 0 | Output: 4move 3 1move 01 5move 2extra 4move 99 3 | Hard | 2 | 1,687 | 479 | 324 | 8 | |
1,287 | A | 1287A | A. Angry Students | 800 | greedy; implementation | It's a walking tour day in SIS.Winter, so \(t\) groups of students are visiting Torzhok. Streets of Torzhok are so narrow that students have to go in a row one after another.Initially, some students are angry. Let's describe a group of students by a string of capital letters ""A"" and ""P"": ""A"" corresponds to an ang... | The first line contains a single integer \(t\) — the number of groups of students (\(1 \le t \le 100\)). The following \(2t\) lines contain descriptions of groups of students.The description of the group starts with an integer \(k_i\) (\(1 \le k_i \le 100\)) — the number of students in the group, followed by a string \... | For every group output single integer — the last moment a student becomes angry. | In the first test, after \(1\) minute the state of students becomes PPAA. After that, no new angry students will appear.In the second tets, state of students in the first group is: after \(1\) minute — AAPAAPPAAPPP after \(2\) minutes — AAAAAAPAAAPP after \(3\) minutes — AAAAAAAAAAAP after \(4\) minutes all \(12\) stud... | Input: 1 4 PPAP | Output: 1 | Beginner | 2 | 1,342 | 422 | 80 | 12 |
1,857 | F | 1857F | F. Sum and Product | 1,600 | binary search; data structures; math | You have an array \(a\) of length \(n\).Your task is to answer \(q\) queries: given \(x,y\), find the number of pairs \(i\) and \(j\) (\(1 \le i < j \le n\)) that both \(a_i + a_j = x\) and \(a_i \cdot a_j = y\).That is, for the array \([1,3,2]\) and asking for \(x=3,y=2\) the answer is \(1\): \(i=1\) and \(j=2\) fail ... | The first line contains one integer \(t\) (\(1\le t\le 10^4\)) — the number of test cases.The second line of each test case contains one integer \(n\) (\(1 \le n \le 2\cdot 10^5\)) — the length of the array \(a\).The third line of each test case contains \(n\) integers \(a_1,a_2,\dots,a_n\) (\(1 \le |a_i| \le 10^9\)) —... | For each test case print a line with \(q\) numbers — the answers to the queries. | For the first test case, let's analyze each pair of numbers separately: pair \((a_1,a_2)\): \(a_1 + a_2 = 4\), \(a_1 \cdot a_2 = 3\) pair \((a_1,a_3)\): \(a_1 + a_3 = 3\), \(a_1 \cdot a_3 = 2\) pair \((a_2,a_3)\): \(a_2 + a_3 = 5\), \(a_2 \cdot a_3 = 6\) From this, we can see that for the first query, the pair \((a_1,a... | Input: 331 3 243 25 63 15 541 1 1 112 161 4 -2 3 3 332 -8-1 -27 12 | Output: 1 1 0 0 6 1 1 3 | Medium | 3 | 537 | 716 | 80 | 18 |
764 | B | 764B | B. Timofey and cubes | 900 | constructive algorithms; implementation | Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his parents. Every cube has a number ai, which is written on it. Timofey put all the cubes in a row and went to unpack other presents.In this time, Timofey's elder brother, Dima reordered the cubes using the following rule. Suppose the... | The first line contains single integer n (1 ≤ n ≤ 2·105) — the number of cubes.The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109), where ai is the number written on the i-th cube after Dima has changed their order. | Print n integers, separated by spaces — the numbers written on the cubes in their initial order.It can be shown that the answer is unique. | Consider the first sample. At the begining row was [2, 3, 9, 6, 7, 1, 4]. After first operation row was [4, 1, 7, 6, 9, 3, 2]. After second operation row was [4, 3, 9, 6, 7, 1, 2]. After third operation row was [4, 3, 7, 6, 9, 1, 2]. At fourth operation we reverse just middle element, so nothing has changed. The final ... | Input: 74 3 7 6 9 1 2 | Output: 2 3 9 6 7 1 4 | Beginner | 2 | 797 | 235 | 138 | 7 |
1,726 | E | 1726E | E. Almost Perfect | 2,400 | combinatorics; fft; math | A permutation \(p\) of length \(n\) is called almost perfect if for all integer \(1 \leq i \leq n\), it holds that \(\lvert p_i - p^{-1}_i \rvert \le 1\), where \(p^{-1}\) is the inverse permutation of \(p\) (i.e. \(p^{-1}_{k_1} = k_2\) if and only if \(p_{k_2} = k_1\)).Count the number of almost perfect permutations o... | The first line contains a single integer \(t\) (\(1 \leq t \leq 1000\)) — the number of test cases. The description of each test case follows.The first and only line of each test case contains a single integer \(n\) (\(1 \leq n \leq 3 \cdot 10^5\)) — the length of the permutation.It is guaranteed that the sum of \(n\) ... | For each test case, output a single integer — the number of almost perfect permutations of length \(n\) modulo \(998244353\). | For \(n = 2\), both permutations \([1, 2]\), and \([2, 1]\) are almost perfect.For \(n = 3\), there are only \(6\) permutations. Having a look at all of them gives us: \([1, 2, 3]\) is an almost perfect permutation. \([1, 3, 2]\) is an almost perfect permutation. \([2, 1, 3]\) is an almost perfect permutation. \([2, 3,... | Input: 32350 | Output: 2 4 830690567 | Expert | 3 | 356 | 373 | 125 | 17 |
1,039 | B | 1039B | B. Subway Pursuit | 2,100 | binary search; interactive; probabilities | This is an interactive problem.In the Wonderful Metropolis of the Future, there is no need in subway train drivers. Due to the technological progress, they were replaced by the Artificial Intelligence (AI). Unfortunately, one day the predictions of sci-fi writers came true: the AI rebelled and now there is an uncontrol... | The first line contains two integers \(n\) and \(k\) (\(1 \leq n \leq 10^{18}\), \(0 \leq k \leq 10\)) — the number of stations and the maximum number of stations the train can move between two applications of the gadget. | In the first sample, the train was initially at the station \(5\), after the first application of the gadget it did not move, after the second application it moved to the station \(3\), and after the third application moved again to the station \(5\). | Input: 10 2YesNoYesYes | Output: 3 53 33 45 5 | Hard | 3 | 1,773 | 221 | 0 | 10 | |
164 | B | 164B | B. Ancient Berland Hieroglyphs | 2,000 | two pointers | Polycarpus enjoys studying Berland hieroglyphs. Once Polycarp got hold of two ancient Berland pictures, on each of which was drawn a circle of hieroglyphs. We know that no hieroglyph occurs twice in either the first or the second circle (but in can occur once in each of them).Polycarpus wants to save these pictures on ... | The first line contains two integers la and lb (1 ≤ la, lb ≤ 1000000) — the number of hieroglyphs in the first and second circles, respectively.Below, due to difficulties with encoding of Berland hieroglyphs, they are given as integers from 1 to 106.The second line contains la integers — the hieroglyphs in the first pi... | Print a single number — the maximum length of the common substring and subsequence. If at any way of breaking the circles it does not exist, print 0. | In the first test Polycarpus picks a string that consists of hieroglyphs 5 and 1, and in the second sample — from hieroglyphs 1, 3 and 5. | Input: 5 41 2 3 4 51 3 5 6 | Output: 2 | Hard | 1 | 1,478 | 634 | 149 | 1 |
702 | A | 702A | A. Maximum Increase | 800 | dp; greedy; implementation | You are given array consisting of n integers. Your task is to find the maximum length of an increasing subarray of the given array.A subarray is the sequence of consecutive elements of the array. Subarray is called increasing if each element of this subarray strictly greater than previous. | The first line contains single positive integer n (1 ≤ n ≤ 105) — the number of integers.The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 109). | Print the maximum length of an increasing subarray of the given array. | Input: 51 7 2 11 15 | Output: 3 | Beginner | 3 | 290 | 165 | 70 | 7 | |
1,978 | D | 1978D | D. Elections | 1,600 | data structures; greedy; implementation; math | Elections are taking place in Berland. There are \(n\) candidates participating in the elections, numbered from \(1\) to \(n\). The \(i\)-th candidate has \(a_i\) fans who will vote for him. Additionally, there are \(c\) people who are undecided about their favorite candidate, let's call them undecided. Undecided peopl... | Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 2 \cdot 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 \(c\) (\(1 \le n \le 2 \cdot 10^5\), \(0 \le c \le 10^9\)) — t... | For each test case, output \(n\) integers, the \(i\)-th of which should be equal to the minimum number of candidates that need to be excluded from the elections for candidate number \(i\) to win. | In the first test case: If all candidates are allowed, candidate number \(1\) will receive \(3\) votes (\(1\) undecided person will vote for him), candidate number \(2\) will receive \(0\) votes, and candidate number \(3\) will receive \(3\) votes. Therefore, candidate number \(1\) wins (he received the same number of ... | Input: 53 12 0 32 30 105 35 4 3 2 14 53 10 7 16 02 2 2 3 3 3 | Output: 0 1 2 1 0 0 1 2 3 4 1 0 2 3 1 1 2 0 4 5 | Medium | 4 | 1,041 | 637 | 195 | 19 |
1,485 | A | 1485A | A. Add and Divide | 1,000 | brute force; greedy; math; number theory | You have two positive integers \(a\) and \(b\).You can perform two kinds of operations: \(a = \lfloor \frac{a}{b} \rfloor\) (replace \(a\) with the integer part of the division between \(a\) and \(b\)) \(b=b+1\) (increase \(b\) by \(1\)) Find the minimum number of operations required to make \(a=0\). | 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 \(a\), \(b\) (\(1 \le a,b \le 10^9\)). | For each test case, print a single integer: the minimum number of operations required to make \(a=0\). | In the first test case, one of the optimal solutions is: Divide \(a\) by \(b\). After this operation \(a = 4\) and \(b = 2\). Divide \(a\) by \(b\). After this operation \(a = 2\) and \(b = 2\). Increase \(b\). After this operation \(a = 2\) and \(b = 3\). Divide \(a\) by \(b\). After this operation \(a = 0\) and \(b =... | Input: 6 9 2 1337 1 1 1 50000000 4 991026972 997 1234 5678 | Output: 4 9 2 12 3 1 | Beginner | 4 | 301 | 207 | 102 | 14 |
228 | A | 228A | A. Is your horseshoe on the other hoof? | 800 | implementation | Valera the Horse is going to the party with friends. He has been following the fashion trends for a while, and he knows that it is very popular to wear all horseshoes of different color. Valera has got four horseshoes left from the last year, but maybe some of them have the same color. In this case he needs to go to th... | The first line contains four space-separated integers s1, s2, s3, s4 (1 ≤ s1, s2, s3, s4 ≤ 109) — the colors of horseshoes Valera has.Consider all possible colors indexed with integers. | Print a single integer — the minimum number of horseshoes Valera needs to buy. | Input: 1 7 3 3 | Output: 1 | Beginner | 1 | 775 | 185 | 78 | 2 | |
883 | M | 883M | M. Quadcopter Competition | 1,100 | greedy; math | Polycarp takes part in a quadcopter competition. According to the rules a flying robot should: start the race from some point of a field, go around the flag, close cycle returning back to the starting point. Polycarp knows the coordinates of the starting point (x1, y1) and the coordinates of the point where the flag is... | The first line contains two integer numbers x1 and y1 ( - 100 ≤ x1, y1 ≤ 100) — coordinates of the quadcopter starting (and finishing) point.The second line contains two integer numbers x2 and y2 ( - 100 ≤ x2, y2 ≤ 100) — coordinates of the flag.It is guaranteed that the quadcopter starting point and the flag do not co... | Print the length of minimal path of the quadcopter to surround the flag and return back. | Input: 1 55 2 | Output: 18 | Easy | 2 | 901 | 327 | 88 | 8 | |
2,078 | D | 2078D | D. Scammy Game Ad | 1,800 | dp; greedy; implementation | Consider the following game. In this game, a level consists of \(n\) pairs of gates. Each pair contains one left gate and one right gate. Each gate performs one of two operations: Addition Operation (+ a): Increases the number of people in a lane by a constant amount \(a\). Multiplication Operation (x a): Multiplies th... | 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 one integer \(n\) (\(1 \leq n \le 30\)) — the number of pairs of gates.The next \(n\) lines of each test case provide the information for the left gate followed by the right gate of eac... | For each test case, output a single integer — the maximum total number of people at the end of the level. | In the first case, here is one possible way to play this game optimally.Initially, we have \(l=1\) person in the left lane and \(r=1\) person in the right lane.After passing through the first pair of gates, we gain \(4\) people from the left gate and \(1 \cdot (2-1) = 1\) person from the right gate, for a total of \(4+... | Input: 43+ 4 x 2x 3 x 3+ 7 + 44+ 9 x 2x 2 x 3+ 9 + 10x 2 + 14x 2 + 1+ 9 + 10x 2 x 3+ 9 x 25x 3 x 3x 2 x 2+ 21 + 2x 2 x 3+ 41 x 3 | Output: 32 98 144 351 | Medium | 3 | 773 | 469 | 105 | 20 |
939 | C | 939C | C. Convenient For Everybody | 1,600 | binary search; two pointers | In distant future on Earth day lasts for n hours and that's why there are n timezones. Local times in adjacent timezones differ by one hour. For describing local time, hours numbers from 1 to n are used, i.e. there is no time ""0 hours"", instead of it ""n hours"" is used. When local time in the 1-st timezone is 1 hour... | The first line contains a single integer n (2 ≤ n ≤ 100 000) — the number of hours in day.The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 10 000), where ai is the number of people in the i-th timezone who want to participate in the contest.The third line contains two space-separated intege... | Output a single integer — the time of the beginning of the contest (in the first timezone local time), such that the number of participants will be maximum possible. If there are many answers, output the smallest among them. | In the first example, it's optimal to start competition at 3 hours (in first timezone). In this case, it will be 1 hour in the second timezone and 2 hours in the third timezone. Only one person from the first timezone won't participate.In second example only people from the third and the fourth timezones will participa... | Input: 31 2 31 3 | Output: 3 | Medium | 2 | 1,061 | 347 | 224 | 9 |
2,045 | B | 2045B | B. ICPC Square | 2,000 | math; number theory | ICPC Square is a hotel provided by the ICPC Committee for the accommodation of the participants. It consists of \(N\) floors (numbered from \(1\) to \(N\)). This hotel has a very unique elevator. If a person is currently at floor \(x\), by riding the elevator once, they can go to floor \(y\) if and only if \(y\) is a m... | A single line consisting of three integers \(N\) \(D\) \(S\) (\(2 \leq N \leq 10^{12}; 1 \leq D \leq N - 1; 1 \leq S \leq N\)). | Output a single integer representing the highest floor you can reach by riding the elevator zero or more times. | Explanation for the sample input/output #1First, ride the elevator from floor \(3\) to floor \(15\). This is possible because \(15\) is a multiple of \(3\) and \(15 - 3 \leq 35\). Then, ride the elevator from floor \(15\) to floor \(30\). This is possible because \(30\) is a multiple of \(15\) and \(30 - 15 \leq 35\). ... | Input: 64 35 3 | Output: 60 | Hard | 2 | 522 | 127 | 111 | 20 |
19 | A | 19A | A. World Football Cup | 1,400 | implementation | Everyone knows that 2010 FIFA World Cup is being held in South Africa now. By the decision of BFA (Berland's Football Association) next World Cup will be held in Berland. BFA took the decision to change some World Cup regulations: the final tournament features n teams (n is always even) the first n / 2 teams (according... | The first input line contains the only integer n (1 ≤ n ≤ 50) — amount of the teams, taking part in the final tournament of World Cup. The following n lines contain the names of these teams, a name is a string of lower-case and upper-case Latin letters, its length doesn't exceed 30 characters. The following n·(n - 1) /... | Output n / 2 lines — names of the teams, which managed to get through to the knockout stage in lexicographical order. Output each name in a separate line. No odd characters (including spaces) are allowed. It's guaranteed that the described regulations help to order the teams without ambiguity. | Input: 4ABCDA-B 1:1A-C 2:2A-D 1:0B-C 1:0B-D 0:3C-D 0:3 | Output: AD | Easy | 1 | 1,060 | 738 | 294 | 0 | |
1,454 | D | 1454D | D. Number into Sequence | 1,300 | constructive algorithms; math; number theory | You are given an integer \(n\) (\(n > 1\)).Your task is to find a sequence of integers \(a_1, a_2, \ldots, a_k\) such that: each \(a_i\) is strictly greater than \(1\); \(a_1 \cdot a_2 \cdot \ldots \cdot a_k = n\) (i. e. the product of this sequence is \(n\)); \(a_{i + 1}\) is divisible by \(a_i\) for each \(i\) from \... | The first line of the input contains one integer \(t\) (\(1 \le t \le 5000\)) — the number of test cases. Then \(t\) test cases follow.The only line of the test case contains one integer \(n\) (\(2 \le n \le 10^{10}\)).It is guaranteed that the sum of \(n\) does not exceed \(10^{10}\) (\(\sum n \le 10^{10}\)). | For each test case, print the answer: in the first line, print one positive integer \(k\) — the maximum possible length of \(a\). In the second line, print \(k\) integers \(a_1, a_2, \ldots, a_k\) — the sequence of length \(k\) satisfying the conditions from the problem statement.If there are several answers, you can p... | Input: 4 2 360 4999999937 4998207083 | Output: 1 2 3 2 2 90 1 4999999937 1 4998207083 | Easy | 3 | 630 | 311 | 420 | 14 | |
1,278 | D | 1278D | D. Segment Tree | 2,100 | data structures; dsu; graphs; trees | As the name of the task implies, you are asked to do some work with segments and trees.Recall that a tree is a connected undirected graph such that there is exactly one simple path between every pair of its vertices.You are given \(n\) segments \([l_1, r_1], [l_2, r_2], \dots, [l_n, r_n]\), \(l_i < r_i\) for every \(i\... | The first line contains a single integer \(n\) (\(1 \le n \le 5 \cdot 10^5\)) — the number of segments.The \(i\)-th of the next \(n\) lines contain the description of the \(i\)-th segment — two integers \(l_i\) and \(r_i\) (\(1 \le l_i < r_i \le 2n\)).It is guaranteed that all segments borders are pairwise distinct. | Print ""YES"" if the resulting graph is a tree and ""NO"" otherwise. | The graph corresponding to the first example:The graph corresponding to the second example:The graph corresponding to the third example: | Input: 6 9 12 2 11 1 3 6 10 5 7 4 8 | Output: YES | Hard | 4 | 992 | 317 | 68 | 12 |
1,430 | B | 1430B | B. Barrels | 800 | greedy; implementation; sortings | You have \(n\) barrels lined up in a row, numbered from left to right from one. Initially, the \(i\)-th barrel contains \(a_i\) liters of water.You can pour water from one barrel to another. In one act of pouring, you can choose two different barrels \(x\) and \(y\) (the \(x\)-th barrel shouldn't be empty) and pour any... | The first line contains one integer \(t\) (\(1 \le t \le 1000\)) — the number of test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le k < n \le 2 \cdot 10^5\)) — the number of barrels and the number of pourings you can make.The second line contains \(n\) integers \(a_1, a_2, \dots,... | For each test case, print the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most \(k\) times. | Input: 2 4 1 5 5 5 5 3 2 0 0 0 | Output: 10 0 | Beginner | 3 | 1,081 | 516 | 168 | 14 | |
1,620 | B | 1620B | B. Triangles on a Rectangle | 1,000 | geometry; greedy; math | A rectangle with its opposite corners in \((0, 0)\) and \((w, h)\) and sides parallel to the axes is drawn on a plane.You are given a list of lattice points such that each point lies on a side of a rectangle but not in its corner. Also, there are at least two points on every side of a rectangle.Your task is to choose t... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of testcases.The first line of each testcase contains two integers \(w\) and \(h\) (\(3 \le w, h \le 10^6\)) — the coordinates of the corner of a rectangle.The next two lines contain the description of the points on two horizontal sides.... | For each testcase print a single integer — the doubled maximum area of a triangle formed by such three points that exactly two of them belong to the same side. | The points in the first testcase of the example: \((1, 0)\), \((2, 0)\); \((2, 8)\), \((3, 8)\), \((4, 8)\); \((0, 1)\), \((0, 4)\), \((0, 6)\); \((5, 4)\), \((5, 5)\). The largest triangle is formed by points \((0, 1)\), \((0, 6)\) and \((5, 4)\) — its area is \(\frac{25}{2}\). Thus, the doubled area is \(25\). Two po... | Input: 3 5 8 2 1 2 3 2 3 4 3 1 4 6 2 4 5 10 7 2 3 9 2 1 7 3 1 3 4 3 4 5 6 11 5 3 1 6 8 3 3 6 8 3 1 3 4 2 2 4 | Output: 25 42 35 | Beginner | 3 | 612 | 1,070 | 159 | 16 |
425 | E | 425E | E. Sereja and Sets | 2,500 | dp | Let's assume that set S consists of m distinct intervals [l1, r1], [l2, r2], ..., [lm, rm] (1 ≤ li ≤ ri ≤ n; li, ri are integers).Let's assume that f(S) is the maximum number of intervals that you can choose from the set S, such that every two of them do not intersect. We assume that two intervals, [l1, r1] and [l2, r2... | The first line contains integers n, k (1 ≤ n ≤ 500; 0 ≤ k ≤ 500). | In a single line, print the answer to the problem modulo 1000000007 (109 + 7). | Input: 3 1 | Output: 23 | Expert | 1 | 526 | 65 | 78 | 4 | |
891 | A | 891A | A. Pride | 1,500 | brute force; dp; greedy; math; number theory | You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the greatest common divisor.What is the minimum number of operations you need to make all of the elements equal to 1? | The first line of the input contains one integer n (1 ≤ n ≤ 2000) — the number of elements in the array.The second line contains n space separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the array. | Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1. | In the first sample you can turn all numbers to 1 using the following 5 moves: [2, 2, 3, 4, 6]. [2, 1, 3, 4, 6] [2, 1, 3, 1, 6] [2, 1, 1, 1, 6] [1, 1, 1, 1, 6] [1, 1, 1, 1, 1] We can prove that in this case it is not possible to make all numbers one using less than 5 moves. | Input: 52 2 3 4 6 | Output: 5 | Medium | 5 | 317 | 215 | 144 | 8 |
1,110 | H | 1110H | H. Modest Substrings | 3,500 | dp; strings | You are given two integers \(l\) and \(r\).Let's call an integer \(x\) modest, if \(l \le x \le r\).Find a string of length \(n\), consisting of digits, which has the largest possible number of substrings, which make a modest integer. Substring having leading zeros are not counted. If there are many answers, find lexic... | The first line contains one integer \(l\) (\(1 \le l \le 10^{800}\)).The second line contains one integer \(r\) (\(l \le r \le 10^{800}\)).The third line contains one integer \(n\) (\(1 \le n \le 2\,000\)). | In the first line, print the maximum possible number of modest substrings.In the second line, print a string of length \(n\) having exactly that number of modest substrings.If there are multiple such strings, print the lexicographically smallest of them. | In the first example, string «101» has modest substrings «1», «10», «1».In the second example, string «111» has modest substrings «1» (\(3\) times) and «11» (\(2\) times). | Input: 1 10 3 | Output: 3 101 | Master | 2 | 492 | 206 | 254 | 11 |
9 | D | 9D | D. How many trees? | 1,900 | combinatorics; divide and conquer; dp | In one very old text file there was written Great Wisdom. This Wisdom was so Great that nobody could decipher it, even Phong — the oldest among the inhabitants of Mainframe. But still he managed to get some information from there. For example, he managed to learn that User launches games for pleasure — and then terribl... | The input data contains two space-separated positive integer numbers n and h (n ≤ 35, h ≤ n). | Output one number — the answer to the problem. It is guaranteed that it does not exceed 9·1018. | Input: 3 2 | Output: 5 | Hard | 3 | 1,939 | 93 | 95 | 0 | |
1,938 | A | 1938A | 3,500 | Master | 0 | 0 | 0 | 0 | 19 | |||||||
1,773 | L | 1773L | L. Lisa's Sequences | 3,500 | dp | Lisa loves playing with the sequences of integers. When she gets a new integer sequence \(a_i\) of length \(n\), she starts looking for all monotone subsequences. A monotone subsequence \([l, r]\) is defined by two indices \(l\) and \(r\) (\(1 \le l < r \le n\)) such that \(\forall i = l, l+1, \ldots, r-1: a_i \le a_{i... | The first line of the input contains two integers \(n\) and \(k\) (\(3 \le k \le n \le 10^6\)) — the length of the sequence and Lisa's boredom threshold. The second line contains \(n\) integers \(b_i\) (\(1 \le b_i \le 99\,999\)) — the original sequence that Lucas has. | On the first line output an integer \(m\) — the minimal number of elements in \(b_i\) that needs to be changed to make the sequence not boring for Lisa. On the second line output \(n\) integers \(a_i\) (\(0 \le a_i \le 100\,000\)), so that the sequence of integers \(a_i\) is not boring for Lisa and is different from th... | Input: 5 3 1 2 3 4 5 | Output: 2 1 0 3 0 5 | Master | 1 | 921 | 269 | 375 | 17 | |
1,935 | C | 1935C | C. Messenger in MAC | 1,800 | binary search; brute force; constructive algorithms; data structures; dp; greedy; sortings | In the new messenger for the students of the Master's Assistance Center, Keftemerum, an update is planned, in which developers want to optimize the set of messages shown to the user. There are a total of \(n\) messages. Each message is characterized by two integers \(a_i\) and \(b_i\). The time spent reading the set of... | Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 5 \cdot 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 \(l\) (\(1 \leq n \leq 2000\), \(1 \leq l \leq 10^9\)) — the n... | For each test case, output a single integer — the maximum possible size of a set of messages, the reading time of which does not exceed \(l\). | In the first test case, you can take a set of three messages with numbers \(p_1 = 3\), \(p_2 = 2\), and \(p_3 = 5\). The time spent reading this set is equal to \(a_3 + a_2 + a_5 + |b_3 - b_2| + |b_2 - b_5| = 2 + 1 + 2 + |4 - 5| + |5 - 3| = 8\).In the second test case, you can take a set of one message with number \(p_... | Input: 55 84 31 52 44 32 31 64 103 124 82 12 125 2624 78 2830 223 817 175 1415 31000000000 998244353179 239228 1337993 1007 | Output: 3 1 2 1 0 | Medium | 7 | 1,128 | 642 | 142 | 19 |
2,008 | H | 2008H | H. Sakurako's Test | 2,100 | binary search; brute force; greedy; math; number theory | Sakurako will soon take a test. The test can be described as an array of integers \(n\) and a task on it:Given an integer \(x\), Sakurako can perform the following operation any number of times: Choose an integer \(i\) (\(1\le i\le n\)) such that \(a_i\ge x\); Change the value of \(a_i\) to \(a_i-x\). Using this operat... | The first line contains one integer \(t\) (\(1\le t\le 10^4\)) — the number of test cases.The first line of each test case contains two integers \(n\) and \(q\) (\(1\le n,q\le 10^5\)) — the number of elements in the array and the number of queries.The second line of each test case contains \(n\) integers \(a_1, a_2, \d... | For each test case, output \(q\) integers — the answer for each query. | Input: 25 51 2 3 4 5123456 31 2 6 4 1 3215 | Output: 0 1 1 1 2 1 0 2 | Hard | 5 | 841 | 610 | 70 | 20 | |
1,463 | C | 1463C | C. Busy Robot | 1,800 | implementation | You have a robot that can move along a number line. At time moment \(0\) it stands at point \(0\).You give \(n\) commands to the robot: at time \(t_i\) seconds you command the robot to go to point \(x_i\). Whenever the robot receives a command, it starts moving towards the point \(x_i\) with the speed of \(1\) unit per... | The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) — the number of test cases. The next lines describe the test cases.The first line of a test case contains a single integer \(n\) (\(1 \le n \le 10^5\)) — the number of commands.The next \(n\) lines describe the commands. The \(i\)-th of these lines c... | For each testcase output a single integer — the number of successful commands. | The movements of the robot in the first test case are described in the problem statement. Only the last command is successful.In the second test case the second command is successful: the robot passes through target point \(4\) at time \(5\). Also, the last command is eventually successful.In the third test case no com... | Input: 8 3 1 5 3 0 6 4 3 1 5 2 4 10 -5 5 2 -5 3 1 4 1 5 1 6 1 4 3 3 5 -3 9 2 12 0 8 1 1 2 -6 7 2 8 3 12 -9 14 2 18 -1 23 9 5 1 -4 4 -7 6 -1 7 -3 8 -7 2 1 2 2 -2 6 3 10 5 5 8 0 12 -4 14 -7 19 -5 | Output: 1 2 0 2 1 1 0 2 | Medium | 1 | 1,247 | 607 | 78 | 14 |
1,809 | A | 1809A | A. Garland | 800 | implementation | You have a garland consisting of \(4\) colored light bulbs, the color of the \(i\)-th light bulb is \(s_i\).Initially, all the light bulbs are turned off. Your task is to turn all the light bulbs on. You can perform the following operation any number of times: select a light bulb and switch its state (turn it on if it ... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.The single line of each test case contains \(s\) — a sequence of \(4\) characters, where each character is a decimal digit. The \(i\)-th character denotes the color of the \(i\)-th light bulb. | For each test case, print one integer — the minimum number of operations to turn all the light bulbs on. If it is impossible to turn all the bulbs on, print -1. | In the first example, all the colors are different, so you can just turn all the bulbs on in \(4\) operations.In the second example, it is impossible to turn all the bulbs on, because after you switch one light bulb, it is impossible to turn the others on.In the third example, you can proceed as follows: turn the first... | Input: 3954600003313 | Output: 4 -1 6 | Beginner | 1 | 700 | 289 | 160 | 18 |
501 | B | 501B | B. Misha and Changing Handles | 1,100 | data structures; dsu; strings | Misha hacked the Codeforces site. Then he decided to let all the users change their handles. A user can now change his handle any number of times. But each new handle must not be equal to any handle that is already used or that was used at some point.Misha has a list of handle change requests. After completing the requ... | The first line contains integer q (1 ≤ q ≤ 1000), the number of handle change requests.Next q lines contain the descriptions of the requests, one per line.Each query consists of two non-empty strings old and new, separated by a space. The strings consist of lowercase and uppercase Latin letters and digits. Strings old ... | In the first line output the integer n — the number of users that changed their handles at least once.In the next n lines print the mapping between the old and the new handles of the users. Each of them must contain two strings, old and new, separated by a space, meaning that before the user had handle old, and after a... | Input: 5Misha ILoveCodeforcesVasya PetrovPetrov VasyaPetrov123ILoveCodeforces MikeMirzayanovPetya Ivanov | Output: 3Petya IvanovMisha MikeMirzayanovVasya VasyaPetrov123 | Easy | 3 | 436 | 568 | 481 | 5 | |
633 | D | 633D | D. Fibonacci-ish | 2,000 | brute force; dp; hashing; implementation; math | Yash has recently learnt about the Fibonacci sequence and is very excited about it. He calls a sequence Fibonacci-ish if the sequence consists of at least two elements f0 and f1 are arbitrary fn + 2 = fn + 1 + fn for all n ≥ 0. You are given some sequence of integers a1, a2, ..., an. Your task is rearrange elements of ... | The first line of the input contains a single integer n (2 ≤ n ≤ 1000) — the length of the sequence ai.The second line contains n integers a1, a2, ..., an (|ai| ≤ 109). | Print the length of the longest possible Fibonacci-ish prefix of the given sequence after rearrangement. | In the first sample, if we rearrange elements of the sequence as - 1, 2, 1, the whole sequence ai would be Fibonacci-ish.In the second sample, the optimal way to rearrange elements is , , , , 28. | Input: 31 2 -1 | Output: 3 | Hard | 5 | 407 | 168 | 104 | 6 |
51 | A | 51A | A. Cheaterius's Problem | 1,300 | implementation | Cheaterius is a famous in all the Berland astrologist, magician and wizard, and he also is a liar and a cheater. One of his latest inventions is Cheaterius' amulets! They bring luck and wealth, but are rather expensive. Cheaterius makes them himself. The technology of their making is kept secret. But we know that throu... | The first line contains an integer n (1 ≤ n ≤ 1000), where n is the number of amulets. Then the amulet's descriptions are contained. Every description occupies two lines and contains two numbers (from 1 to 6) in each line. Between every pair of amulets the line ""**"" is located. | Print the required number of piles. | Input: 43123**3123**1332**3213 | Output: 1 | Easy | 1 | 1,031 | 280 | 35 | 0 | |
1,808 | E2 | 1808E2 | E2. Minibuses on Venus (medium version) | 2,500 | combinatorics; divide and conquer; dp; matrices | This is the medium version of the problem. The only difference between the three versions is the constraints on \(n\) and \(k\). You can make hacks only if all versions of the problem are solved.Maxim is a minibus driver on Venus.To ride on Maxim's minibus, you need a ticket. Each ticket has a number consisting of \(n\... | The only line of the input contains three integers \(n\), \(k\) and \(m\) (\(1 \le n \le 10^{18}\), \(1 \le k \le 100\), \(10^8 \le m \le 10^9 + 7\), \(m\) is a prime number) — the number of digits on the ticket, the base of the numeral system on Venus, and the module for answer calculation. | Print one integer — the number of lucky tickets modulo \(m\), i. e. the remainder after dividing the answer by \(m\). | In the first example, there are only four lucky tickets: \(000\), \(011\), \(101\), and \(110\). | Input: 3 2 1000000007 | Output: 4 | Expert | 4 | 1,129 | 292 | 117 | 18 |
109 | D | 109D | D. Lucky Sorting | 2,000 | constructive algorithms; sortings | Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.Petya got an array consisting of n numbers, it is the gift for his birthday. Now he wants to sort it i... | The first line contains an integer n (1 ≤ n ≤ 105) — the number of elements in the array. The second line contains n positive integers, not exceeding 109 — the array that needs to be sorted in the non-decreasing order. | On the first line print number k (0 ≤ k ≤ 2n) — the number of the swaps in the sorting. On the following k lines print one pair of distinct numbers (a pair per line) — the indexes of elements to swap. The numbers in the array are numbered starting from 1. If it is impossible to sort the given sequence, print the single... | Input: 24 7 | Output: 0 | Hard | 2 | 693 | 218 | 466 | 1 | |
40 | B | 40B | B. Repaintings | 1,600 | math | A chessboard n × m in size is given. During the zero minute we repaint all the black squares to the 0 color. During the i-th minute we repaint to the i color the initially black squares that have exactly four corner-adjacent squares painted i - 1 (all such squares are repainted simultaneously). This process continues a... | The first line contains integers n and m (1 ≤ n, m ≤ 5000). The second line contains integer x (1 ≤ x ≤ 109). | Print how many squares will be painted exactly x times. | Input: 3 31 | Output: 4 | Medium | 1 | 553 | 109 | 55 | 0 | |
629 | A | 629A | A. Far Relative’s Birthday Cake | 800 | brute force; combinatorics; constructive algorithms; implementation | Door's family is going celebrate Famil Doors's birthday party. They love Famil Door so they are planning to make his birthday cake weird!The cake is a n × n square consisting of equal squares with side length 1. Each square is either empty or consists of a single chocolate. They bought the cake and randomly started to ... | In the first line of the input, you are given a single integer n (1 ≤ n ≤ 100) — the length of the side of the cake.Then follow n lines, each containing n characters. Empty cells are denoted with '.', while cells that contain chocolates are denoted by 'C'. | Print the value of Famil Door's happiness, i.e. the number of pairs of chocolate pieces that share the same row or the same column. | If we number rows from top to bottom and columns from left to right, then, pieces that share the same row in the first sample are: (1, 2) and (1, 3) (3, 1) and (3, 3) Pieces that share the same column are: (2, 1) and (3, 1) (1, 3) and (3, 3) | Input: 3.CCC..C.C | Output: 4 | Beginner | 4 | 731 | 256 | 131 | 6 |
2,044 | H | 2044H | H. Hard Demon Problem | 2,100 | constructive algorithms; data structures; dp; implementation; math | Swing is opening a pancake factory! A good pancake factory must be good at flattening things, so Swing is going to test his new equipment on 2D matrices.Swing is given an \(n \times n\) matrix \(M\) containing positive integers. He has \(q\) queries to ask you. For each query, he gives you four integers \(x_1\), \(y_1\... | The first line contains an integer \(t\) (\(1 \leq t \leq 10^3\)) — the number of test cases.The first line of each test contains two integers \(n\) and \(q\) (\(1 \leq n \leq 2000, 1 \leq q \leq 10^6\)) — the length of \(M\) and the number of queries.The following \(n\) lines contain \(n\) integers each, the \(i\)'th ... | For each test case, output the results of the \(q\) queries on a new line. | In the second query of the first test case, \(A = [9, 5, 5, 2]\). Therefore, the sum is \(1 \cdot 9 + 2 \cdot 5 + 3 \cdot 5 + 4 \cdot 2 = 42\). | Input: 24 31 5 2 44 9 5 34 5 2 31 5 5 21 1 4 42 2 3 31 2 4 33 31 2 34 5 67 8 91 1 1 31 3 3 32 2 2 2 | Output: 500 42 168 14 42 5 | Hard | 5 | 927 | 740 | 74 | 20 |
126 | C | 126C | C. E-reader Display | 2,000 | constructive algorithms; greedy | After years of hard work scientists invented an absolutely new e-reader display. The new display has a larger resolution, consumes less energy and its production is cheaper. And besides, one can bend it. The only inconvenience is highly unusual management. For that very reason the developers decided to leave the e-read... | The first line contains number n (1 ≤ n ≤ 2000).Next n lines contain n characters each: the description of the picture that needs to be shown. ""0"" represents the white color and ""1"" represents the black color. | Print one integer z — the least number of commands needed to display the picture. | Input: 50111010010100011001111110 | Output: 4 | Hard | 2 | 1,298 | 213 | 81 | 1 | |
176 | C | 176C | C. Playing with Superglue | 2,000 | combinatorics; constructive algorithms | Two players play a game. The game is played on a rectangular board with n × m squares. At the beginning of the game two different squares of the board have two chips. The first player's goal is to shift the chips to the same square. The second player aims to stop the first one with a tube of superglue.We'll describe th... | The first line contains six integers n, m, x1, y1, x2, y2 — the board sizes and the coordinates of the first and second chips, correspondingly (1 ≤ n, m ≤ 100; 2 ≤ n × m; 1 ≤ x1, x2 ≤ n; 1 ≤ y1, y2 ≤ m). The numbers in the line are separated by single spaces.It is guaranteed that the chips are located in different squa... | If the first player wins, print ""First"" without the quotes. Otherwise, print ""Second"" without the quotes. | Input: 1 6 1 2 1 6 | Output: First | Hard | 2 | 1,827 | 324 | 109 | 1 | |
407 | C | 407C | C. Curious Array | 2,500 | brute force; combinatorics; implementation; math | You've got an array consisting of n integers: a[1], a[2], ..., a[n]. Moreover, there are m queries, each query can be described by three integers li, ri, ki. Query li, ri, ki means that we should add to each element a[j], where li ≤ j ≤ ri.Record means the binomial coefficient, or the number of combinations from y elem... | The first line contains integers n, m (1 ≤ n, m ≤ 105).The second line contains n integers a[1], a[2], ..., a[n] (0 ≤ ai ≤ 109) — the initial array.Next m lines contain queries in the format li, ri, ki — to all elements of the segment li... ri add number (1 ≤ li ≤ ri ≤ n; 0 ≤ k ≤ 100). | Print n integers: the i-th number is the value of element a[i] after all the queries. As the values can be rather large, print them modulo 1000000007 (109 + 7). | Input: 5 10 0 0 0 01 5 0 | Output: 1 1 1 1 1 | Expert | 4 | 427 | 286 | 160 | 4 | |
1,632 | E2 | 1632E2 | E2. Distance Tree (hard version) | 2,700 | binary search; dfs and similar; shortest paths; trees | This version of the problem differs from the previous one only in the constraint on \(n\).A tree is a connected undirected graph without cycles. A weighted tree has a weight assigned to each edge. The distance between two vertices is the minimum sum of weights on the path connecting them.You are given a weighted tree w... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 3 \cdot 10^5\)).Each of the next \(n−1\) lines contains two integers \(u\) and \(v\) (\(1 \le u,v \le n\)) indicating that there is an edge bet... | For each test case, print \(n\) integers in a single line, \(x\)-th of which is equal to \(f(x)\) for all \(x\) from \(1\) to \(n\). | In the first testcase: For \(x = 1\), we can an edge between vertices \(1\) and \(3\), then \(d(1) = 0\) and \(d(2) = d(3) = d(4) = 1\), so \(f(1) = 1\). For \(x \ge 2\), no matter which edge we add, \(d(1) = 0\), \(d(2) = d(4) = 1\) and \(d(3) = 2\), so \(f(x) = 2\). | Input: 341 22 31 421 271 21 33 43 53 65 7 | Output: 1 2 2 2 1 1 2 2 3 3 3 3 3 | Master | 4 | 770 | 492 | 132 | 16 |
1,195 | D2 | 1195D2 | D2. Submarine in the Rybinsk Sea (hard edition) | 1,800 | combinatorics; math; number theory | This problem differs from the previous one only in the absence of the constraint on the equal length of all numbers \(a_1, a_2, \dots, a_n\).A team of SIS students is going to make a trip on a submarine. Their target is an ancient treasure in a sunken ship lying on the bottom of the Great Rybinsk sea. Unfortunately, th... | The first line of the input contains a single integer \(n\) (\(1 \le n \le 100\,000\)) — the number of elements in the array. The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) — the elements of the array. | Print the answer modulo \(998\,244\,353\). | Input: 3 12 3 45 | Output: 12330 | Medium | 3 | 1,878 | 257 | 42 | 11 | |
550 | B | 550B | B. Preparing Olympiad | 1,400 | bitmasks; brute force | You have n problems. You have estimated the difficulty of the i-th one as integer ci. Now you want to prepare a problemset for a contest, using some of the problems you've made.A problemset for the contest must consist of at least two problems. You think that the total difficulty of the problems of the contest must be ... | The first line contains four integers n, l, r, x (1 ≤ n ≤ 15, 1 ≤ l ≤ r ≤ 109, 1 ≤ x ≤ 106) — the number of problems you have, the minimum and maximum value of total difficulty of the problemset and the minimum difference in difficulty between the hardest problem in the pack and the easiest one, respectively.The second... | Print the number of ways to choose a suitable problemset for the contest. | In the first example two sets are suitable, one consisting of the second and third problem, another one consisting of all three problems.In the second example, two sets of problems are suitable — the set of problems with difficulties 10 and 30 as well as the set of problems with difficulties 20 and 30.In the third exam... | Input: 3 5 6 11 2 3 | Output: 2 | Easy | 2 | 539 | 410 | 73 | 5 |
290 | E | 290E | E. HQ | 2,500 | *special; constructive algorithms | The famous joke programming language HQ9+ has only 4 commands. In this problem we will explore its subset — a language called HQ... | The only line of the input is a string between 1 and 106 characters long. | Output ""Yes"" or ""No"". | The rest of the problem statement was destroyed by a stray raccoon. We are terribly sorry for the inconvenience. | Input: HHHH | Output: Yes | Expert | 2 | 131 | 73 | 25 | 2 |
1,092 | C | 1092C | C. Prefixes and Suffixes | 1,700 | strings | Ivan wants to play a game with you. He picked some string \(s\) of length \(n\) consisting only of lowercase Latin letters. You don't know this string. Ivan has informed you about all its improper prefixes and suffixes (i.e. prefixes and suffixes of lengths from \(1\) to \(n-1\)), but he didn't tell you which strings a... | The first line of the input contains one integer number \(n\) (\(2 \le n \le 100\)) — the length of the guessed string \(s\).The next \(2n-2\) lines are contain prefixes and suffixes, one per line. Each of them is the string of length from \(1\) to \(n-1\) consisting only of lowercase Latin letters. They can be given i... | Print one string of length \(2n-2\) — the string consisting only of characters 'P' and 'S'. The number of characters 'P' should be equal to the number of characters 'S'. The \(i\)-th character of this string should be 'P' if the \(i\)-th of the input strings is the prefix and 'S' otherwise.If there are several possible... | The only string which Ivan can guess in the first example is ""ababa"".The only string which Ivan can guess in the second example is ""aaa"". Answers ""SPSP"", ""SSPP"" and ""PSPS"" are also acceptable.In the third example Ivan can guess the string ""ac"" or the string ""ca"". The answer ""SP"" is also acceptable. | Input: 5 ba a abab a aba baba ab aba | Output: SPPSPSPS | Medium | 1 | 710 | 537 | 348 | 10 |
1,302 | C | 1302C | C. Segment tree or Fenwick? | 0 | data structures | This is an unusual problem in an unusual contest, here is the announcement: http://codeforces.com/blog/entry/73543You are given an array \(A\) of length \(n\), initially filled with zeros. You need to process \(q\) queries to the array, each of one of the following types: 1 x y: you need to assign \(A_x=y\); 2 l r: you... | The first line contains an integer \(T\) (\(1 \leq T \leq 10^5\)) — the number of test cases.Each test case description starts with two integers \(n, q\) (\(1 \leq n, q \leq 10^5\)) — the length of the array and the number of queries. The following \(q\) lines contain the description of queries: \(1~x~y\) (\(1 \leq x \... | For each query of the second type print its result on a separate line. | Input: 2 6 5 2 1 6 1 3 2 2 2 4 1 6 3 2 1 6 5 3 1 3 7 1 1 4 2 1 5 | Output: 0 2 5 11 | Beginner | 1 | 431 | 550 | 70 | 13 | |
1,106 | D | 1106D | D. Lunar New Year and a Wander | 1,500 | data structures; dfs and similar; graphs; greedy; shortest paths | Lunar New Year is approaching, and Bob decides to take a wander in a nearby park.The park can be represented as a connected graph with \(n\) nodes and \(m\) bidirectional edges. Initially Bob is at the node \(1\) and he records \(1\) on his notebook. He can wander from one node to another through those bidirectional ed... | The first line contains two positive integers \(n\) and \(m\) (\(1 \leq n, m \leq 10^5\)), denoting the number of nodes and edges, respectively.The following \(m\) lines describe the bidirectional edges in the graph. The \(i\)-th of these lines contains two integers \(u_i\) and \(v_i\) (\(1 \leq u_i, v_i \leq n\)), rep... | Output a line containing the lexicographically smallest sequence \(a_1, a_2, \ldots, a_n\) Bob can record. | In the first sample, Bob's optimal wandering path could be \(1 \rightarrow 2 \rightarrow 1 \rightarrow 3\). Therefore, Bob will obtain the sequence \(\{1, 2, 3\}\), which is the lexicographically smallest one.In the second sample, Bob's optimal wandering path could be \(1 \rightarrow 4 \rightarrow 3 \rightarrow 2 \righ... | Input: 3 2 1 2 1 3 | Output: 1 2 3 | Medium | 5 | 1,135 | 502 | 106 | 11 |
581 | D | 581D | D. Three Logos | 1,700 | bitmasks; brute force; constructive algorithms; geometry; implementation; math | Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area. Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no emp... | The first line of the input contains six positive integers x1, y1, x2, y2, x3, y3 (1 ≤ x1, y1, x2, y2, x3, y3 ≤ 100), where xi and yi determine the length and width of the logo of the i-th company respectively. | If it is impossible to place all the three logos on a square shield, print a single integer ""-1"" (without the quotes).If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters ""A"", ""B"" o... | Input: 5 1 2 5 5 2 | Output: 5AAAAABBBBBBBBBBCCCCCCCCCC | Medium | 6 | 613 | 210 | 1,042 | 5 | |
316 | E1 | 316E1 | E1. Summer Homework | 1,500 | brute force; data structures | By the age of three Smart Beaver mastered all arithmetic operations and got this summer homework from the amazed teacher:You are given a sequence of integers a1, a2, ..., an. Your task is to perform on it m consecutive operations of the following type: For given numbers xi and vi assign value vi to element axi. For giv... | The first line contains two integers n and m (1 ≤ n, m ≤ 2·105) — the number of integers in the sequence and the number of operations, correspondingly. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 105). Then follow m lines, each describes an operation. Each line starts with an integer ti (1 ≤ ti ≤ 3) —... | For each query print the calculated sum modulo 1000000000 (109). | Input: 5 51 3 1 2 42 1 42 1 52 2 41 3 102 1 5 | Output: 1232850 | Medium | 2 | 632 | 972 | 64 | 3 | |
470 | H | 470H | H. Array Sorting | 2,300 | *special | Sorting arrays is traditionally associated with high-level languages. How hard can it be in FALSE? Sort the given array 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. | Input: 3 3 1 2 | Output: 1 2 3 | Expert | 1 | 144 | 194 | 52 | 4 | |
799 | F | 799F | F. Beautiful fountains rows | 3,500 | data structures | Butler Ostin wants to show Arkady that rows of odd number of fountains are beautiful, while rows of even number of fountains are not.The butler wants to show Arkady n gardens. Each garden is a row of m cells, the i-th garden has one fountain in each of the cells between li and ri inclusive, and there are no more founta... | The first line contains two integers n and m (1 ≤ n, m ≤ 2·105) — the number of gardens and the length of each garden.n lines follow. The i-th of these lines contains two integers li and ri (1 ≤ li ≤ ri ≤ m) — the bounds of the segment that contains fountains in the i-th garden. | Print one integer: the total length of all suitable segments. | In the first example the following pairs suit Ostin: (a, b): (1, 2), (1, 4), (1, 5), (2, 2), (2, 4), (2, 5), (3, 3), (4, 4), (4, 5).In the second example the following pairs suit Ostin: (a, b): (1, 2), (1, 5), (2, 2), (2, 5), (3, 3), (4, 4), (4, 6), (5, 5), (6, 6). | Input: 1 52 4 | Output: 23 | Master | 1 | 912 | 279 | 61 | 7 |
1,638 | C | 1638C | C. Inversion Graph | 1,300 | data structures; dsu; graphs; math | You are given a permutation \(p_1, p_2, \dots, p_n\). Then, an undirected graph is constructed in the following way: add an edge between vertices \(i\), \(j\) such that \(i < j\) if and only if \(p_i > p_j\). Your task is to count the number of connected components in this graph.Two vertices \(u\) and \(v\) belong to t... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^5\)) — the number of test cases. Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)) — the length of the permutation.The second line of each ... | For each test case, print one integer \(k\) — the number of connected components. | Each separate test case is depicted in the image below. The colored squares represent the elements of the permutation. For one permutation, each color represents some connected component. The number of distinct colors is the answer. | Input: 631 2 352 1 4 3 566 1 4 2 5 31163 2 1 6 5 453 1 5 2 4 | Output: 3 3 1 1 2 1 | Easy | 4 | 736 | 527 | 81 | 16 |
912 | D | 912D | D. Fishes | 2,100 | data structures; graphs; greedy; probabilities; shortest paths | While Grisha was celebrating New Year with Ded Moroz, Misha gifted Sasha a small rectangular pond of size n × m, divided into cells of size 1 × 1, inhabited by tiny evil fishes (no more than one fish per cell, otherwise they'll strife!).The gift bundle also includes a square scoop of size r × r, designed for fishing. I... | The only line contains four integers n, m, r, k (1 ≤ n, m ≤ 105, 1 ≤ r ≤ min(n, m), 1 ≤ k ≤ min(n·m, 105)). | Print a single number — the maximum possible expected number of caught fishes.You answer is considered correct, is its absolute or relative error does not exceed 10 - 9. Namely, let your answer be a, and the jury's answer be b. Your answer is considered correct, if . | In the first example you can put the fishes in cells (2, 1), (2, 2), (2, 3). In this case, for any of four possible positions of the scoop-net (highlighted with light green), the number of fishes inside is equal to two, and so is the expected value. | Input: 3 3 2 3 | Output: 2.0000000000 | Hard | 5 | 1,055 | 107 | 267 | 9 |
1,873 | H | 1873H | H. Mad City | 1,700 | dfs and similar; dsu; games; graphs; shortest paths; trees | Marcel and Valeriu are in the mad city, which is represented by \(n\) buildings with \(n\) two-way roads between them. Marcel and Valeriu start at buildings \(a\) and \(b\) respectively. Marcel wants to catch Valeriu, in other words, be in the same building as him or meet on the same road. During each move, they choose... | The first line contains a single integer \(t\) (\(1 \leq t \leq 1000\)) — the number of test cases.The first line of each test case contains three space-separated integers \(n\), \(a\), \(b\) (\(3 \leq n \leq 2 \cdot 10^5\); \(1 \leq a, b \leq n\)) — the number of buildings (which equals the number of roads) and the st... | For each test case output ""YES"" if Valeriu can escape Marcel forever 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 the graph looks as follows: Marcel starts at building \(2\), while Valeriu starts at building \(1\). Valeriu knows which way Marcel will move around the triangle, and he can simply always move in the same way to avoid Marcel forever.In the second test case the graph looks as follows: Marcel start... | Input: 63 2 12 13 21 34 1 41 41 21 32 34 1 21 22 32 43 47 1 14 12 15 34 64 27 53 48 5 38 35 12 66 81 24 85 76 710 6 11 24 35 87 810 41 92 48 16 23 1 | Output: YES NO YES NO NO YES | Medium | 6 | 826 | 775 | 235 | 18 |
1,617 | A | 1617A | A. Forbidden Subsequence | 800 | constructive algorithms; greedy; sortings; strings | You are given strings \(S\) and \(T\), consisting of lowercase English letters. It is guaranteed that \(T\) is a permutation of the string abc. Find string \(S'\), the lexicographically smallest permutation of \(S\) such that \(T\) is not a subsequence of \(S'\).String \(a\) is a permutation of string \(b\) if the numb... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) — the number of test cases. Description of the test cases follows.The first line of each test case contains a string \(S\) (\(1 \le |S| \le 100\)), consisting of lowercase English letters.The second line of eac... | For each test case, output a single string \(S'\), the lexicographically smallest permutation of \(S\) such that \(T\) is not a subsequence of \(S'\). | In the first test case, both aaaabbc and aaaabcb are lexicographically smaller than aaaacbb, but they contain abc as a subsequence.In the second test case, abccc is the smallest permutation of cccba and does not contain acb as a subsequence.In the third test case, bcdis is the smallest permutation of dbsic and does not... | Input: 7 abacaba abc cccba acb dbsic bac abracadabra abc dddddddddddd cba bbc abc ac abc | Output: aaaacbb abccc bcdis aaaaacbbdrr dddddddddddd bbc ac | Beginner | 4 | 845 | 490 | 150 | 16 |
1,989 | D | 1989D | D. Smithing Skill | 1,900 | brute force; data structures; dp; greedy; math; sortings; two pointers | You are playing a famous computer game (that just works) where you have various skills you can level up. Today, you focused on the ""Smithing"" skill. Your tactic is obvious: forging weapons from ingots and then melting them back to return the materials partially. For simplicity, every time you create an item, you get ... | The first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 10^6\)) — the number of weapon classes and metal types.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^6\)), where \(a_i\) is the number of ingots you need to forge one weapon of the \(i\)-th class.The third line... | Print one integer — the maximum total experience points you can gain by repeatedly forging and melting weapons. | In the first example, you can do the following: craft one weapon of the \(1\)-st class from the \(1\)-st type of metal, spending \(9\) ingots; melt that weapon, returning \(8\) ingots of the \(1\)-st metal type; again, craft and melt one weapon of the \(1\)-st class from the \(1\)-st metal type; craft and melt one weap... | Input: 5 39 6 7 5 58 4 5 1 210 4 7 | Output: 12 | Hard | 7 | 1,027 | 659 | 111 | 19 |
1,440 | B | 1440B | B. Sum of Medians | 900 | greedy; math | A median of an array of integers of length \(n\) is the number standing on the \(\lceil {\frac{n}{2}} \rceil\) (rounding up) position in the non-decreasing ordering of its elements. Positions are numbered starting with \(1\). For example, a median of the array \([2, 6, 4, 1, 3, 5]\) is equal to \(3\). There exist some ... | The first line contains a single integer \(t\) (\(1 \leq t \leq 100\)) — the number of test cases. The next \(2t\) lines contain descriptions of test cases.The first line of the description of each test case contains two integers \(n\), \(k\) (\(1 \leq n, k \leq 1000\)).The second line of the description of each test c... | For each test case print a single integer — the maximum possible sum of medians of all \(k\) arrays. | The examples of possible divisions into arrays for all test cases of the first test:Test case \(1\): \([0, 24], [34, 58], [62, 64], [69, 78]\). The medians are \(0, 34, 62, 69\). Their sum is \(165\).Test case \(2\): \([27, 61], [81, 91]\). The medians are \(27, 81\). Their sum is \(108\).Test case \(3\): \([2, 91, 92,... | Input: 6 2 4 0 24 34 58 62 64 69 78 2 2 27 61 81 91 4 3 2 4 16 18 21 27 36 53 82 91 92 95 3 4 3 11 12 22 33 35 38 67 69 71 94 99 2 1 11 41 3 3 1 1 1 1 1 1 1 1 1 | Output: 165 108 145 234 11 3 | Beginner | 2 | 694 | 604 | 100 | 14 |
559 | E | 559E | E. Gerald and Path | 3,000 | dp; sortings | The main walking trail in Geraldion is absolutely straight, and it passes strictly from the north to the south, it is so long that no one has ever reached its ends in either of the two directions. The Geraldionians love to walk on this path at any time, so the mayor of the city asked the Herald to illuminate this path ... | The first line contains integer n (1 ≤ n ≤ 100) — the number of spotlights. Each of the n lines contains two space-separated integers, ai and li (0 ≤ ai ≤ 108, 1 ≤ li ≤ 108). Number ai shows how much further the i-th spotlight to the north, and number li shows the length of the segment it illuminates.It is guaranteed t... | Print a single integer — the maximum total length of the illuminated part of the path. | Input: 31 12 23 3 | Output: 5 | Master | 2 | 1,024 | 350 | 86 | 5 | |
1,982 | D | 1982D | D. Beauty of the mountains | 1,700 | brute force; data structures; implementation; math; number theory | Nikita loves mountains and has finally decided to visit the Berlyand mountain range! The range was so beautiful that Nikita decided to capture it on a map. The map is a table of \(n\) rows and \(m\) columns, with each cell containing a non-negative integer representing the height of the mountain.He also noticed that mo... | Each test consists of several test cases. The first line contains an integer \(t\) (\(1 \le t \le 10^{4}\)) — the number of test cases. This is followed by a description of test cases.The first line of each test case contains three integers \(n, m, k\) (\(1 \le n, m \le 500, 1 \le k \le min(n, m)\)).The next \(n\) line... | For each test case, output ""YES"" without quotes if it is possible to equalize the sums of the mountain heights, otherwise output ""NO"" without quotes. You can output each letter in any case (for example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as a positive answer). | The mountain array from the first test case looks like this: Initially, the sum of the heights of the mountains with snowy caps is \(11 + 3 + 4 + 3 + 0 + 1 + 15 = 37\), and without them is \(7 + 2 = 9\).To equalize these sums, we can perform two transformations:First transformation: Note that the constant \(c\) can be ... | Input: 83 3 27 11 34 2 30 1 151000100004 4 3123 413 24 233123 42 0 21622 1 1 53427 763 22 601011111101001013 3 22 1 11 1 21 5 40101010103 3 22 1 11 1 21 5 30101010103 4 346 49 50 119 30 23 1230 25 1 461000010000105 4 439 30 0 1722 42 30 1310 44 46 3512 19 9 3921 0 45 40100011110011011111002 2 23 46 700002 2 20 02 00100... | Medium | 5 | 1,364 | 687 | 299 | 19 |
37 | C | 37C | C. Old Berland Language | 1,900 | data structures; greedy; trees | Berland scientists know that the Old Berland language had exactly n words. Those words had lengths of l1, l2, ..., ln letters. Every word consisted of two letters, 0 and 1. Ancient Berland people spoke quickly and didn’t make pauses between the words, but at the same time they could always understand each other perfect... | The first line contains one integer N (1 ≤ N ≤ 1000) — the number of words in Old Berland language. The second line contains N space-separated integers — the lengths of these words. All the lengths are natural numbers not exceeding 1000. | If there’s no such set of words, in the single line output NO. Otherwise, in the first line output YES, and in the next N lines output the words themselves in the order their lengths were given in the input file. If the answer is not unique, output any. | Input: 31 2 3 | Output: YES010110 | Hard | 3 | 632 | 237 | 253 | 0 | |
775 | A | 775A | A. University Schedule | 2,900 | *special | In this problem your task is to come up with a week schedule of classes in university for professors and student groups. Consider that there are 6 educational days in week and maximum number of classes per educational day is 7 (classes numerated from 1 to 7 for each educational day).It is known that in university n stu... | The first line contains three integers n, m and a (1 ≤ n, m, a ≤ 60) — the number of groups, the number of professors and the number of classrooms.Each of the following n lines contains m integers from 0 to 24 — j-th number in i-th line equals to the number of classes with the professor j must conduct with the i-th stu... | In the first line print the minimized value of function f.After that print blank line.After that print the schedule for each student group in increasing order of group number. For each student group print 7 lines. Each line must contains 6 numbers. Let the number at i-th line and j-th column equals to x. If in j-th day... | During the main part of the competition (one week) you solution will be judged on 100 preliminary tests. The first 10 preliminary tests are available for download by a link http://assets.codeforces.com/files/vk/vkcup-2017-wr2-materials-v1.tar.gz.After the end of the contest (i.e., a week after its start) the last solut... | Input: 3 3 11 0 00 1 00 0 1 | Output: 541 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 | Master | 1 | 2,645 | 698 | 693 | 7 |
535 | A | 535A | A. Tavas and Nafas | 1,000 | brute force; implementation | Today Tavas got his test result as an integer score and he wants to share it with his girlfriend, Nafas.His phone operating system is Tavdroid, and its keyboard doesn't have any digits! He wants to share his score with Nafas via text, so he has no choice but to send this number using words. He ate coffee mix without wa... | The first and only line of input contains an integer s (0 ≤ s ≤ 99), Tavas's score. | In the first and only line of output, print a single string consisting only from English lowercase letters and hyphens ('-'). Do not use spaces. | You can find all you need to know about English numerals in http://en.wikipedia.org/wiki/English_numerals . | Input: 6 | Output: six | Beginner | 2 | 435 | 83 | 144 | 5 |
482 | E | 482E | E. ELCA | 3,200 | data structures; trees | You have a root tree containing n vertexes. Let's number the tree vertexes with integers from 1 to n. The tree root is in the vertex 1.Each vertex (except fot the tree root) v has a direct ancestor pv. Also each vertex v has its integer value sv. Your task is to perform following queries: P v u (u ≠ v). If u isn't in s... | The first line of the input contains integer n (2 ≤ n ≤ 5·104) — the number of the tree vertexes. The second line contains n - 1 integer p2, p3, ..., pn (1 ≤ pi ≤ n) — the description of the tree edges. It is guaranteed that those numbers form a tree.The third line contains n integers — s1, s2, ... sn (0 ≤ si ≤ 106) — ... | Print q + 1 number — the corresponding expected values. Your answer will be considered correct if its absolute or relative error doesn't exceed 10 - 9. | Note that in the query P v u if u lies in subtree of v you must perform assignment pu = v. An example of such case is the last query in the sample. | Input: 51 2 2 11 2 3 4 55P 3 4P 4 5V 2 3P 5 2P 1 4 | Output: 1.6400000001.8000000002.2800000002.3200000002.8000000001.840000000 | Master | 2 | 1,020 | 700 | 151 | 4 |
920 | B | 920B | B. Tea Queue | 1,200 | implementation | Recently n students from city S moved to city P to attend a programming camp.They moved there by train. In the evening, all students in the train decided that they want to drink some tea. Of course, no two people can use the same teapot simultaneously, so the students had to form a queue to get their tea.i-th student c... | The first line contains one integer t — the number of test cases to solve (1 ≤ t ≤ 1000).Then t test cases follow. The first line of each test case contains one integer n (1 ≤ n ≤ 1000) — the number of students.Then n lines follow. Each line contains two integer li, ri (1 ≤ li ≤ ri ≤ 5000) — the second i-th student com... | For each test case print n integers. i-th of them must be equal to the second when i-th student gets his tea, or 0 if he leaves without tea. | The example contains 2 tests: During 1-st second, students 1 and 2 come to the queue, and student 1 gets his tea. Student 2 gets his tea during 2-nd second. During 1-st second, students 1 and 2 come to the queue, student 1 gets his tea, and student 2 leaves without tea. During 2-nd second, student 3 comes and gets his ... | Input: 221 31 431 51 12 3 | Output: 1 2 1 0 2 | Easy | 1 | 1,047 | 568 | 140 | 9 |
323 | C | 323C | C. Two permutations | 2,400 | data structures | You are given two permutations p and q, consisting of n elements, and m queries of the form: l1, r1, l2, r2 (l1 ≤ r1; l2 ≤ r2). The response for the query is the number of such integers from 1 to n, that their position in the first permutation is in segment [l1, r1] (borders included), and position in the second permut... | The first line contains one integer n (1 ≤ n ≤ 106), the number of elements in both permutations. The following line contains n integers, separated with spaces: p1, p2, ..., pn (1 ≤ pi ≤ n). These are elements of the first permutation. The next line contains the second permutation q1, q2, ..., qn in same format.The fol... | Print a response for each query in a separate line. | Input: 33 1 23 2 111 2 3 3 | Output: 1 | Expert | 1 | 578 | 951 | 51 | 3 | |
1,344 | D | 1344D | D. Résumé Review | 2,700 | binary search; greedy; math | Uh oh! Applications to tech companies are due soon, and you've been procrastinating by doing contests instead! (Let's pretend for now that it is actually possible to get a job in these uncertain times.)You have completed many programming projects. In fact, there are exactly \(n\) types of programming projects, and you ... | The first line contains two integers \(n\) and \(k\) (\(1\le n\le 10^5\), \(1\le k\le \sum\limits_{i=1}^n a_i\)) — the number of types of programming projects and the résumé size, respectively.The next line contains \(n\) integers \(a_1,\ldots,a_n\) (\(1\le a_i\le 10^9\)) — \(a_i\) is equal to the number of completed p... | In a single line, output \(n\) integers \(b_1,\ldots, b_n\) that achieve the maximum value of \(f(b_1,\ldots,b_n)\), while satisfying the requirements \(0\le b_i\le a_i\) and \(\sum\limits_{i=1}^n b_i=k\). If there are multiple solutions, output any.Note that you do not have to output the value \(f(b_1,\ldots,b_n)\). | For the first test, the optimal answer is \(f=-269\). Note that a larger \(f\) value is possible if we ignored the constraint \(\sum\limits_{i=1}^n b_i=k\).For the second test, the optimal answer is \(f=9\). | Input: 10 32 1 2 3 4 5 5 5 5 5 5 | Output: 1 2 3 3 3 4 4 4 4 4 | Master | 3 | 1,343 | 342 | 318 | 13 |
731 | E | 731E | E. Funny Game | 2,200 | dp; games | Once upon a time Petya and Gena gathered after another programming competition and decided to play some game. As they consider most modern games to be boring, they always try to invent their own games. They have only stickers and markers, but that won't stop them.The game they came up with has the following rules. Init... | The first line of input contains a single integer n (2 ≤ n ≤ 200 000) — the number of stickers, initially located on the wall.The second line contains n integers a1, a2, ..., an ( - 10 000 ≤ ai ≤ 10 000) — the numbers on stickers in order from left to right. | Print one integer — the difference between the Petya's score and Gena's score at the end of the game if both players play optimally. | In the first sample, the optimal move for Petya is to take all the stickers. As a result, his score will be equal to 14 and Gena's score will be equal to 0.In the second sample, the optimal sequence of moves is the following. On the first move Petya will take first three sticker and will put the new sticker with value ... | Input: 32 4 8 | Output: 14 | Hard | 2 | 1,297 | 258 | 132 | 7 |
2,034 | C | 2034C | C. Trapped in the Witch's Labyrinth | 1,400 | constructive algorithms; dfs and similar; graphs; implementation | In the fourth labor of Rostam, the legendary hero from the Shahnameh, an old witch has created a magical maze to trap him. The maze is a rectangular grid consisting of \(n\) rows and \(m\) columns. Each cell in the maze points in a specific direction: up, down, left, or right. The witch has enchanted Rostam so that whe... | The first line of the input contains an integer \(t\) (\(1 \leq t \leq 10^4\)), the number of test cases.For each test case: The first line contains two integers \(n\) and \(m\) (\(1 \leq n, m \leq 1000\)), representing the number of rows and columns in the maze. Each of the next \(n\) lines contains a string of \(m\) ... | For each test case, print a single integer, the maximum number of starting cells from which Rostam will be trapped forever after assigning directions to the unspecified cells optimally. | In the first test case, all of the cells will be good no matter what you do.In the second test case, if you assign the ?s like the picture below, all of the cells will be bad: In the third test case, if you assign the ?s like the picture below, you will have \(5\) bad cells (red-shaded cells): | Input: 33 3UUUL?RDDD2 3??????3 3?U?R?LRDL | Output: 0 6 5 | Easy | 4 | 906 | 559 | 185 | 20 |
378 | A | 378A | A. Playing with Dice | 800 | brute force | Two players are playing a game. First each of them writes an integer from 1 to 6, and then a dice is thrown. The player whose written number got closer to the number on the dice wins. If both payers have the same difference, it's a draw.The first player wrote number a, the second player wrote number b. How many ways to... | The single line contains two integers a and b (1 ≤ a, b ≤ 6) — the numbers written on the paper by the first and second player, correspondingly. | Print three integers: the number of ways to throw the dice at which the first player wins, the game ends with a draw or the second player wins, correspondingly. | The dice is a standard cube-shaped six-sided object with each side containing a number from 1 to 6, and where all numbers on all sides are distinct.You can assume that number a is closer to number x than number b, if |a - x| < |b - x|. | Input: 2 5 | Output: 3 0 3 | Beginner | 1 | 423 | 144 | 160 | 3 |
585 | C | 585C | C. Alice, Bob, Oranges and Apples | 2,400 | number theory | Alice and Bob decided to eat some fruit. In the kitchen they found a large bag of oranges and apples. Alice immediately took an orange for herself, Bob took an apple. To make the process of sharing the remaining fruit more fun, the friends decided to play a game. They put multiple cards and on each one they wrote a let... | The first line of the input contains two integers, x, y (1 ≤ x, y ≤ 1018, xy > 1) — the number of oranges and apples that were initially in the bag. | Print any sequence of cards that would meet the problem conditions as a compressed string of characters 'A' and 'B. That means that you need to replace the segments of identical consecutive characters by the number of repetitions of the characters and the actual character. For example, string AAABAABBB should be replac... | In the first sample, if the row contained three cards with letter 'B', then Bob should give one apple to Alice three times. So, in the end of the game Alice has one orange and three apples, and Bob has one apple, in total it is one orange and four apples.In second sample, there is no answer since one card is not enough... | Input: 1 4 | Output: 3B | Expert | 1 | 1,040 | 148 | 824 | 5 |
1,368 | G | 1368G | G. Shifting Dominoes | 3,200 | data structures; geometry; graphs; trees | Bill likes to play with dominoes. He took an \(n \times m\) board divided into equal square cells, and covered it with dominoes. Each domino covers two adjacent cells of the board either horizontally or vertically, and each cell is covered exactly once with a half of one domino (that is, there are no uncovered cells, a... | The first line contains two positive integers \(n\) and \(m\) (\(nm \leq 2 \cdot 10^5\)) — height and width of the board respectively.The next \(n\) lines describe the tiling of the board, row by row from top to bottom. Each of these lines contains \(m\) characters, describing the cells in the corresponding row left to... | Print a single integer — the number of distinct photos Bill can take. | In the first sample case, no moves are possible after removing any domino, thus there are four distinct photos.In the second sample case, four photos are possible after removing the leftmost domino by independently moving/not moving the remaining two dominoes. Two more different photos are obtained by removing one of t... | Input: 2 4 UUUU DDDD | Output: 4 | Master | 4 | 1,481 | 671 | 69 | 13 |
1,267 | D | 1267D | D. DevOps Best Practices | 2,800 | constructive algorithms | Daisy is a senior software engineer at RainyDay, LLC. She has just implemented three new features in their product: the first feature makes their product work, the second one makes their product fast, and the third one makes their product correct. The company encourages at least some testing of new features, so Daisy a... | The first line contains integer \(n\) (\(2 \le n \le 256\)) — the number of servers in your company.Next \(n\) lines contain three integers each. The \(j\)-th integer in the \(i\)-th line is \(1\) if Daisy wants the \(j\)-th feature to be deployed to the \(i\)-th server, or \(0\) otherwise.Next \(n\) lines contain thre... | If it is impossible to configure CD/CT system with CD being set up between at most \(264\) pairs of servers, then output the single line ""Impossible"".Otherwise, the first line of the output must contain the line ""Possible"".Next line must contain \(n\) space-separated integers — the configuration of CT. The \(i\)-th... | CD/CT system for the first sample test is shown below. | Input: 3 1 1 1 1 0 1 1 1 1 1 1 1 0 0 0 1 0 1 | Output: Possible 1 1 1 2 3 2 1 3 | Master | 1 | 2,235 | 669 | 762 | 12 |
831 | C | 831C | C. Jury Marks | 1,700 | brute force; constructive algorithms | Polycarp watched TV-show where k jury members one by one rated a participant by adding him a certain number of points (may be negative, i. e. points were subtracted). Initially the participant had some score, and each the marks were one by one added to his score. It is known that the i-th jury member gave ai points.Pol... | The first line contains two integers k and n (1 ≤ n ≤ k ≤ 2 000) — the number of jury members and the number of scores Polycarp remembers.The second line contains k integers a1, a2, ..., ak ( - 2 000 ≤ ai ≤ 2 000) — jury's marks in chronological order.The third line contains n distinct integers b1, b2, ..., bn ( - 4 00... | Print the number of options for the score the participant could have before the judges rated the participant. If Polycarp messes something up and there is no options, print ""0"" (without quotes). | The answer for the first example is 3 because initially the participant could have - 10, 10 or 15 points.In the second example there is only one correct initial score equaling to 4 002 000. | Input: 4 1-5 5 0 2010 | Output: 3 | Medium | 2 | 865 | 459 | 196 | 8 |
1,527 | A | 1527A | A. And Then There Were K | 800 | bitmasks | Given an integer \(n\), find the maximum value of integer \(k\) such that the following condition holds: \(n\) & (\(n-1\)) & (\(n-2\)) & (\(n-3\)) & ... (\(k\)) = \(0\) where & denotes the bitwise AND operation. | The first line contains a single integer \(t\) (\(1 \le t \le 3 \cdot 10^4\)). Then \(t\) test cases follow.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^9\)). | For each test case, output a single integer — the required integer \(k\). | In the first testcase, the maximum value for which the continuous & operation gives 0 value, is 1.In the second testcase, the maximum value for which the continuous & operation gives 0 value, is 3. No value greater then 3, say for example 4, will give the & sum 0. \(5 \, \& \, 4 \neq 0\), \(5 \, \& \, 4 \, \& \, 3 = 0\... | Input: 3 2 5 17 | Output: 1 3 15 | Beginner | 1 | 211 | 196 | 73 | 15 |
178 | A1 | 178A1 | A1. Educational Game | 1,000 | The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1, a2, ..., ak (i.e. some prefix of the sequence)... | The first input line contains a single integer n. The second line contains n integers ai (0 ≤ ai ≤ 104), separated by single spaces.The input limitations for getting 20 points are: 1 ≤ n ≤ 300 The input limitations for getting 50 points are: 1 ≤ n ≤ 2000 The input limitations for getting 100 points are: 1 ≤ n ≤ 105 | Print exactly n - 1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero.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. | Input: 41 0 1 2 | Output: 113 | Beginner | 0 | 1,085 | 316 | 314 | 1 | ||
1,406 | C | 1406C | C. Link Cut Centroids | 1,700 | constructive algorithms; dfs and similar; graphs; trees | Fishing Prince loves trees, and he especially loves trees with only one centroid. The tree is a connected graph without cycles.A vertex is a centroid of a tree only when you cut this vertex (remove it and remove all edges from this vertex), the size of the largest connected component of the remaining graph is the small... | The input consists of multiple test cases. The first line contains an 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 an integer \(n\) (\(3\leq n\leq 10^5\)) — the number of vertices.Each of the next \(n-1\) lines conta... | For each test case, print two lines.In the first line print two integers \(x_1, y_1\) (\(1 \leq x_1, y_1 \leq n\)), which means you cut the edge between vertices \(x_1\) and \(y_1\). There should exist edge connecting vertices \(x_1\) and \(y_1\).In the second line print two integers \(x_2, y_2\) (\(1 \leq x_2, y_2 \le... | Note that you can add the same edge that you cut.In the first test case, after cutting and adding the same edge, the vertex \(2\) is still the only centroid.In the second test case, the vertex \(2\) becomes the only centroid after cutting the edge between vertices \(1\) and \(3\) and adding the edge between vertices \(... | Input: 2 5 1 2 1 3 2 4 2 5 6 1 2 1 3 1 4 2 5 2 6 | Output: 1 2 1 2 1 3 2 3 | Medium | 4 | 1,136 | 570 | 498 | 14 |
1,174 | E | 1174E | E. Ehab and the Expected GCD Problem | 2,500 | combinatorics; dp; math; number theory | Let's define a function \(f(p)\) on a permutation \(p\) as follows. Let \(g_i\) be the greatest common divisor (GCD) of elements \(p_1\), \(p_2\), ..., \(p_i\) (in other words, it is the GCD of the prefix of length \(i\)). Then \(f(p)\) is the number of distinct elements among \(g_1\), \(g_2\), ..., \(g_n\).Let \(f_{ma... | The only line contains the integer \(n\) (\(2 \le n \le 10^6\)) — the length of the permutations. | The only line should contain your answer modulo \(10^9+7\). | Consider the second example: these are the permutations of length \(3\): \([1,2,3]\), \(f(p)=1\). \([1,3,2]\), \(f(p)=1\). \([2,1,3]\), \(f(p)=2\). \([2,3,1]\), \(f(p)=2\). \([3,1,2]\), \(f(p)=2\). \([3,2,1]\), \(f(p)=2\). The maximum value \(f_{max}(3) = 2\), and there are \(4\) permutations \(p\) such that \(f(p)=2\)... | Input: 2 | Output: 1 | Expert | 4 | 676 | 97 | 59 | 11 |
724 | G | 724G | G. Xor-matic Number of the Graph | 2,600 | bitmasks; graphs; math; number theory; trees | You are given an undirected graph, constisting of n vertices and m edges. Each edge of the graph has some non-negative integer written on it.Let's call a triple (u, v, s) interesting, if 1 ≤ u < v ≤ n and there is a path (possibly non-simple, i.e. it can visit the same vertices and edges multiple times) between vertice... | The first line of the input contains two integers n and m (1 ≤ n ≤ 100 000, 0 ≤ m ≤ 200 000) — numbers of vertices and edges in the given graph.The follow m lines contain three integers ui, vi and ti (1 ≤ ui, vi ≤ n, 0 ≤ ti ≤ 1018, ui ≠ vi) — vertices connected by the edge and integer written on it. It is guaranteed th... | Print the single integer, equal to the described sum over modulo 109 + 7. | In the first example the are 6 interesting triples: (1, 2, 1) (1, 3, 2) (1, 4, 3) (2, 3, 3) (2, 4, 2) (3, 4, 1) The sum is equal to 1 + 2 + 3 + 3 + 2 + 1 = 12.In the second example the are 12 interesting triples: (1, 2, 1) (2, 3, 2) (1, 3, 3) (3, 4, 4) (2, 4, 6) (1, 4, 7) (1, 4, 8) (2, 4, 9) (3, 4, 11) (1, 3, 12) (2, 3... | Input: 4 41 2 11 3 22 3 33 4 1 | Output: 12 | Expert | 5 | 675 | 375 | 73 | 7 |
1,115 | G3 | 1115G3 | G3. Palindrome checker oracle | 1,600 | *special | Implement a quantum oracle on \(N\) qubits which checks whether the vector \(\vec{x}\) is a palindrome (i.e., implements the function \(f(\vec{x}) = 1\) if \(\vec{x}\) is a palindrome, and 0 otherwise).You have to implement an operation which takes the following inputs: an array of \(N\) (\(1 \le N \le 8\)) qubits \(x\... | Medium | 1 | 836 | 0 | 0 | 11 | ||||
611 | A | 611A | A. New Year and Days | 900 | implementation | Today is Wednesday, the third day of the week. What's more interesting is that tomorrow is the last day of the year 2015.Limak is a little polar bear. He enjoyed this year a lot. Now, he is so eager to the coming year 2016.Limak wants to prove how responsible a bear he is. He is going to regularly save candies for the ... | The only line of the input is in one of the following two formats: ""x of week"" where x (1 ≤ x ≤ 7) denotes the day of the week. The 1-st day is Monday and the 7-th one is Sunday. ""x of month"" where x (1 ≤ x ≤ 31) denotes the day of the month. | Print one integer — the number of candies Limak will save in the year 2016. | Polar bears use the Gregorian calendar. It is the most common calendar and you likely use it too. You can read about it on Wikipedia if you want to – https://en.wikipedia.org/wiki/Gregorian_calendar. The week starts with Monday.In the first sample Limak wants to save one candy on each Thursday (the 4-th day of the week... | Input: 4 of week | Output: 52 | Beginner | 1 | 604 | 246 | 75 | 6 |
1,195 | D1 | 1195D1 | D1. Submarine in the Rybinsk Sea (easy edition) | 1,500 | combinatorics; math; number theory | This problem differs from the next one only in the presence of the constraint on the equal length of all numbers \(a_1, a_2, \dots, a_n\). Actually, this problem is a subtask of the problem D2 from the same contest and the solution of D2 solves this subtask too.A team of SIS students is going to make a trip on a submar... | The first line of the input contains a single integer \(n\) (\(1 \le n \le 100\,000\)) — the number of elements in the array. The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) — the elements of the array. All numbers \(a_1, a_2, \dots, a_n\) are of equal length (that... | Print the answer modulo \(998\,244\,353\). | Input: 3 12 33 45 | Output: 26730 | Medium | 3 | 2,099 | 368 | 42 | 11 | |
1,016 | C | 1016C | C. Vasya And The Mushrooms | 1,800 | dp; implementation | Vasya's house is situated in a forest, and there is a mushroom glade near it. The glade consists of two rows, each of which can be divided into n consecutive cells. For each cell Vasya knows how fast the mushrooms grow in this cell (more formally, how many grams of mushrooms grow in this cell each minute). Vasya spends... | The first line contains the number n (1 ≤ n ≤ 3·105) — the length of the glade.The second line contains n numbers a1, a2, ..., an (1 ≤ ai ≤ 106) — the growth rate of mushrooms in the first row of the glade.The third line contains n numbers b1, b2, ..., bn (1 ≤ bi ≤ 106) is the growth rate of mushrooms in the second row... | Output one number — the maximum total weight of mushrooms that Vasya can collect by choosing the optimal route. Pay attention that Vasya must visit every cell of the glade exactly once. | In the first test case, the optimal route is as follows: Thus, the collected weight of mushrooms will be 0·1 + 1·2 + 2·3 + 3·4 + 4·5 + 5·6 = 70.In the second test case, the optimal route is as follows: Thus, the collected weight of mushrooms will be 0·1 + 1·10 + 2·100 + 3·1000 + 4·10000 + 5·100000 = 543210. | Input: 31 2 36 5 4 | Output: 70 | Medium | 2 | 973 | 334 | 185 | 10 |
2,081 | A | 2081A | A. Math Division | 1,800 | bitmasks; dp; math; probabilities | Ecrade has an integer \(x\). He will show you this number in the form of a binary number of length \(n\).There are two kinds of operations. Replace \(x\) with \(\left\lfloor \dfrac{x}{2}\right\rfloor\), where \(\left\lfloor \dfrac{x}{2}\right\rfloor\) is the greatest integer \(\le \dfrac{x}{2}\). Replace \(x\) with \(\... | The first line contains a single integer \(t\) (\(1 \le t \le 10^5\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)) — the length of \(x\) in binary representation.The second line of each test case contains a ... | For each test case, print a single integer representing the expected number of operations Ecrade will perform to make \(x\) equal to \(1\), modulo \(10^9+7\).Formally, let \(M = 10^9+7\). It can be shown that the exact answer can be expressed as an irreducible fraction \(\frac{p}{q}\), where \(p\) and \(q\) are integer... | For simplicity, we call the first operation \(\text{OPER 1}\) and the second operation \(\text{OPER 2}\).In the first test case, \(x=6\), and there are six possible series of operations: \(6 \xrightarrow{\text{OPER 1}} 3 \xrightarrow{\text{OPER 1}} 1\), the probability is \(\dfrac{1}{4}\). \(6 \xrightarrow{\text{OPER 1... | Input: 331103100101101001011 | Output: 500000006 2 193359386 | Medium | 4 | 832 | 619 | 514 | 20 |
1,968 | A | 1968A | A. Maximize? | 800 | brute force; math; number theory | You are given an integer \(x\). Your task is to find any integer \(y\) \((1\le y<x)\) such that \(\gcd(x,y)+y\) is maximum possible.Note that if there is more than one \(y\) which satisfies the statement, you are allowed to find any.\(\gcd(a,b)\) is the Greatest Common Divisor of \(a\) and \(b\). For example, \(\gcd(6,... | The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) — the number of test cases.Each of the following \(t\) lines contains a single integer \(x\) (\(2 \le x \le 1000\)). | For each test case, output any \(y\) (\(1 \le y < x\)), which satisfies the statement. | Input: 710721100210006 | Output: 5 6 18 98 1 750 3 | Beginner | 3 | 327 | 186 | 86 | 19 | |
26 | E | 26E | E. Multithreading | 2,400 | constructive algorithms | You are given the following concurrent program. There are N processes and the i-th process has the following pseudocode: repeat ni times yi := y y := yi + 1end repeatHere y is a shared variable. Everything else is local for the process. All actions on a given row are atomic, i.e. when the process starts executing a row... | In the first line of the input you will be given two space separated integers N (1 ≤ N ≤ 100) and W ( - 109 ≤ W ≤ 109). In the second line there are N space separated integers ni (1 ≤ ni ≤ 1000). | On the first line of the output write Yes if it is possible that at the end y = W, or No otherwise. If the answer is No then there is no second line, but if the answer is Yes, then on the second line output a space separated list of integers representing some schedule that leads to the desired result. For more informat... | For simplicity, assume that there is no repeat statement in the code of the processes, but the code from the loop is written the correct amount of times. The processes are numbered starting from 1. The list of integers represent which process works on its next instruction at a given step. For example, consider the sche... | Input: 1 1011 | Output: No | Expert | 1 | 724 | 195 | 333 | 0 |
1,616 | B | 1616B | B. Mirror in the String | 1,100 | greedy; strings | You have a string \(s_1 s_2 \ldots s_n\) and you stand on the left of the string looking right. You want to choose an index \(k\) (\(1 \le k \le n\)) and place a mirror after the \(k\)-th letter, so that what you see is \(s_1 s_2 \ldots s_k s_k s_{k - 1} \ldots s_1\). What is the lexicographically smallest string you c... | The first line of input contains one integer \(t\) (\(1 \leq t \leq 10\,000\)): the number of test cases.The next \(t\) lines contain the description of the test cases, two lines per a test case.In the first line you are given one integer \(n\) (\(1 \leq n \leq 10^5\)): the length of the string.The second line contains... | For each test case print the lexicographically smallest string you can see. | In the first test case choose \(k = 1\) to obtain ""cc"".In the second test case choose \(k = 3\) to obtain ""cbaabc"".In the third test case choose \(k = 1\) to obtain ""aa"".In the fourth test case choose \(k = 1\) to obtain ""bb"". | Input: 4 10 codeforces 9 cbacbacba 3 aaa 4 bbaa | Output: cc cbaabc aa bb | Easy | 2 | 637 | 471 | 75 | 16 |
1,331 | A | 1331A | A. Is it rated? | 0 | *special | Beginner | 1 | 0 | 0 | 0 | 13 | |||||
995 | B | 995B | B. Suit and Tie | 1,400 | greedy; implementation; math | Allen is hosting a formal dinner party. \(2n\) people come to the event in \(n\) pairs (couples). After a night of fun, Allen wants to line everyone up for a final picture. The \(2n\) people line up, but Allen doesn't like the ordering. Allen prefers if each pair occupies adjacent positions in the line, as this makes t... | The first line contains a single integer \(n\) (\(1 \le n \le 100\)), the number of pairs of people.The second line contains \(2n\) integers \(a_1, a_2, \dots, a_{2n}\). For each \(i\) with \(1 \le i \le n\), \(i\) appears exactly twice. If \(a_j = a_k = i\), that means that the \(j\)-th and \(k\)-th people in the line... | Output a single integer, representing the minimum number of adjacent swaps needed to line the people up so that each pair occupies adjacent positions. | In the first sample case, we can transform \(1 1 2 3 3 2 4 4 \rightarrow 1 1 2 3 2 3 4 4 \rightarrow 1 1 2 2 3 3 4 4\) in two steps. Note that the sequence \(1 1 2 3 3 2 4 4 \rightarrow 1 1 3 2 3 2 4 4 \rightarrow 1 1 3 3 2 2 4 4\) also works in the same number of steps.The second sample case already satisfies the cons... | Input: 41 1 2 3 3 2 4 4 | Output: 2 | Easy | 3 | 499 | 335 | 150 | 9 |
1,348 | C | 1348C | C. Phoenix and Distribution | 1,600 | constructive algorithms; greedy; sortings; strings | Phoenix has a string \(s\) consisting of lowercase Latin letters. He wants to distribute all the letters of his string into \(k\) non-empty strings \(a_1, a_2, \dots, a_k\) such that every letter of \(s\) goes to exactly one of the strings \(a_i\). The strings \(a_i\) do not need to be substrings of \(s\). Phoenix can ... | The input consists of multiple test cases. The first line contains an integer \(t\) (\(1 \le t \le 1000\)) — the number of test cases. Each test case consists of two lines.The first line of each test case consists of two integers \(n\) and \(k\) (\(1 \le k \le n \le 10^5\)) — the length of string \(s\) and the number o... | Print \(t\) answers — one per test case. The \(i\)-th answer should be the minimal possible value of \(max(a_1, a_2, \dots, a_k)\) in the \(i\)-th test case. | In the first test case, one optimal solution is to distribute baba into ab and ab. In the second test case, one optimal solution is to distribute baacb into abbc and a.In the third test case, one optimal solution is to distribute baacb into ac, ab, and b.In the fourth test case, one optimal solution is to distribute aa... | Input: 6 4 2 baba 5 2 baacb 5 3 baacb 5 3 aaaaa 6 4 aaxxzz 7 1 phoenix | Output: ab abbc b aa x ehinopx | Medium | 4 | 1,300 | 603 | 157 | 13 |
1,938 | E | 1938E | 2,200 | Hard | 0 | 0 | 0 | 0 | 19 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.