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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
513 | C | 513C | C. Second price auction | 2,000 | bitmasks; probabilities | Nowadays, most of the internet advertisements are not statically linked to a web page. Instead, what will be shown to the person opening a web page is determined within 100 milliseconds after the web page is opened. Usually, multiple companies compete for each ad slot on the web page in an auction. Each of them receive... | The first line of input contains an integer number n (2 β€ n β€ 5). n lines follow, the i-th of them containing two numbers Li and Ri (1 β€ Li β€ Ri β€ 10000) describing the i-th company's bid preferences.This problem doesn't have subproblems. You will get 8 points for the correct submission. | Output the answer with absolute or relative error no more than 1e - 9. | Consider the first example. The first company bids a random integer number of microdollars in range [4, 7]; the second company bids between 8 and 10, and the third company bids 5 microdollars. The second company will win regardless of the exact value it bids, however the price it will pay depends on the value of first ... | Input: 34 78 105 5 | Output: 5.7500000000 | Hard | 2 | 1,399 | 288 | 70 | 5 |
1,935 | F | 1935F | F. Andrey's Tree | 2,800 | binary search; constructive algorithms; data structures; dfs and similar; dsu; greedy; implementation; trees | Master Andrey loves trees\(^{\dagger}\) very much, so he has a tree consisting of \(n\) vertices.But it's not that simple. Master Timofey decided to steal one vertex from the tree. If Timofey stole vertex \(v\) from the tree, then vertex \(v\) and all edges with one end at vertex \(v\) are removed from the tree, while ... | Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(5 \le n \le 2\cdot10^5\)) β the number of vertices in Andrey's tree.T... | For each test case, output the answer in the following format:For each vertex \(v\) (in the order from \(1\) to \(n\)), in the first line output two integers \(w\) and \(m\) β the minimum number of coins that need to be spent to make the graph a tree again after removing vertex \(v\), and the number of added edges.Then... | In the first test case:Consider the removal of vertex \(4\): The optimal solution would be to add an edge from vertex \(5\) to vertex \(3\). Then we will spend \(|5 - 3| = 2\) coins.In the third test case:Consider the removal of vertex \(1\): The optimal solution would be: Add an edge from vertex \(2\) to vertex \(3\),... | Input: 351 31 44 53 254 24 33 55 152 11 51 41 3 | Output: 1 1 3 4 0 0 1 1 1 2 2 1 3 5 0 0 0 0 0 0 1 1 1 2 1 1 1 2 1 1 1 2 3 3 2 3 4 5 3 4 0 0 0 0 0 0 0 0 | Master | 8 | 1,034 | 681 | 560 | 19 |
1,023 | G | 1023G | G. Pisces | 3,400 | data structures; flows; trees | A group of researchers are studying fish population in a natural system of lakes and rivers. The system contains \(n\) lakes connected by \(n - 1\) rivers. Each river has integer length (in kilometers) and can be traversed in both directions. It is possible to travel between any pair of lakes by traversing the rivers (... | The first line contains a single integer \(n\) (\(1 \leq n \leq 10^5\)) β the number of lakes in the system.The next \(n - 1\) lines describe the rivers. The \(i\)-th of these lines contains three integers \(u_i\), \(v_i\), \(l_i\) (\(1 \leq u_i, v_i \leq n\), \(u_i \neq v_i\), \(1 \leq l_i \leq 10^3\)) β \(1\)-based i... | Print one integer β the smallest total number of fish not contradicting the observations. | In the first example, there could be one fish swimming through lakes \(2\), \(1\), and \(4\), and the second fish swimming through lakes \(3\), \(1\), and \(2\).In the second example a single fish can not possibly be part of all observations simultaneously, but two fish swimming \(2 \to 1 \to 4\) and \(3 \to 1 \to 5\) ... | Input: 41 2 11 3 11 4 151 1 21 1 32 2 13 1 43 1 2 | Output: 2 | Master | 3 | 1,130 | 854 | 89 | 10 |
40 | E | 40E | E. Number Table | 2,500 | combinatorics | As it has been found out recently, all the Berland's current economical state can be described using a simple table n Γ m in size. n β the number of days in each Berland month, m β the number of months. Thus, a table cell corresponds to a day and a month of the Berland's year. Each cell will contain either 1, or -1, wh... | The first line contains integers n and m (1 β€ n, m β€ 1000). The second line contains the integer k (0 β€ k < max(n, m)) β the number of cells in which the data had been preserved. The next k lines contain the data on the state of the table in the preserved cells. Each line is of the form ""a b c"", where a (1 β€ a β€ n) β... | Print the number of different tables that could conform to the preserved data modulo p. | Input: 2 20100 | Output: 2 | Expert | 1 | 1,161 | 596 | 87 | 0 | |
989 | D | 989D | D. A Shade of Moonlight | 2,500 | binary search; geometry; math; sortings; two pointers | Gathering darkness shrouds the woods and the world. The moon sheds its light on the boat and the river.""To curtain off the moonlight should be hardly possible; the shades present its mellow beauty and restful nature."" Intonates Mino.""See? The clouds are coming."" Kanno gazes into the distance.""That can't be better,... | The first line contains three space-separated integers \(n\), \(l\), and \(w_\mathrm{max}\) (\(1 \leq n \leq 10^5\), \(1 \leq l, w_\mathrm{max} \leq 10^8\)) β the number of clouds, the length of each cloud and the maximum wind speed, respectively.The \(i\)-th of the following \(n\) lines contains two space-separated in... | Output one integer β the number of unordered pairs of clouds such that it's possible that clouds from each pair cover the moon at the same future moment with a proper choice of wind velocity \(w\). | In the first example, the initial positions and velocities of clouds are illustrated below. The pairs are: \((1, 3)\), covering the moon at time \(2.5\) with \(w = -0.4\); \((1, 4)\), covering the moon at time \(3.5\) with \(w = -0.6\); \((1, 5)\), covering the moon at time \(4.5\) with \(w = -0.7\); \((2, 5)\), coveri... | Input: 5 1 2-2 12 13 -15 -17 -1 | Output: 4 | Expert | 5 | 1,412 | 573 | 197 | 9 |
864 | A | 864A | A. Fair Game | 1,000 | implementation; sortings | Petya and Vasya decided to play a game. They have n cards (n is an even number). A single integer is written on each card.Before the game Petya will choose an integer and after that Vasya will choose another integer (different from the number that Petya chose). During the game each player takes all the cards with numbe... | The first line contains a single integer n (2 β€ n β€ 100) β number of cards. It is guaranteed that n is an even number.The following n lines contain a sequence of integers a1, a2, ..., an (one integer per line, 1 β€ ai β€ 100) β numbers written on the n cards. | If it is impossible for Petya and Vasya to choose numbers in such a way that the game will be fair, print ""NO"" (without quotes) in the first line. In this case you should not print anything more.In the other case print ""YES"" (without quotes) in the first line. In the second line print two distinct integers β number... | In the first example the game will be fair if, for example, Petya chooses number 11, and Vasya chooses number 27. Then the will take all cards β Petya will take cards 1 and 4, and Vasya will take cards 2 and 3. Thus, each of them will take exactly two cards.In the second example fair game is impossible because the numb... | Input: 411272711 | Output: YES11 27 | Beginner | 2 | 745 | 257 | 459 | 8 |
477 | E | 477E | E. Dreamoon and Notepad | 3,100 | data structures | Dreamoon has just created a document of hard problems using notepad.exe. The document consists of n lines of text, ai denotes the length of the i-th line. He now wants to know what is the fastest way to move the cursor around because the document is really long.Let (r, c) be a current cursor position, where r is row nu... | The first line contains an integer n(1 β€ n β€ 400, 000) β the number of lines of text. The second line contains n integers a1, a2, ..., an(1 β€ ai β€ 108).The third line contains an integer q(1 β€ q β€ 400, 000). Each of the next q lines contains four integers r1, c1, r2, c2 representing a query (1 β€ r1, r2 β€ n, 0 β€ c1 β€ ar... | For each query print the result of the query. | In the first sample, the first query can be solved with keys: HOME, right.The second query can be solved with keys: down, down, down, END, down.The third query can be solved with keys: down, END, down.The fourth query can be solved with keys: END, down. | Input: 91 3 5 3 1 3 5 3 143 5 3 13 3 7 31 0 3 36 0 7 3 | Output: 2532 | Master | 1 | 1,093 | 337 | 45 | 4 |
1,703 | A | 1703A | A. YES or YES? | 800 | brute force; implementation; strings | There is a string \(s\) of length \(3\), consisting of uppercase and lowercase English letters. Check if it is equal to ""YES"" (without quotes), where each letter can be in any case. For example, ""yES"", ""Yes"", ""yes"" are all allowable. | The first line of the input contains an integer \(t\) (\(1 \leq t \leq 10^3\)) β the number of testcases.The description of each test consists of one line containing one string \(s\) consisting of three characters. Each character of \(s\) is either an uppercase or lowercase English letter. | For each test case, output ""YES"" (without quotes) if \(s\) satisfies the condition, 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 contain the strings ""YES"", ""yES"", ""yes"", ""Yes"", ""YeS"". All of these are equal to ""YES"", where each character is either uppercase or lowercase. | Input: 10YESyESyesYesYeSNooorZyEzYasXES | Output: YES YES YES YES YES NO NO NO NO NO | Beginner | 3 | 241 | 290 | 264 | 17 |
659 | D | 659D | D. Bicycle Race | 1,500 | geometry; implementation; math | Maria participates in a bicycle race.The speedway takes place on the shores of Lake Lucerne, just repeating its contour. As you know, the lake shore consists only of straight sections, directed to the north, south, east or west.Let's introduce a system of coordinates, directing the Ox axis from west to east, and the Oy... | The first line of the input contains an integer n (4 β€ n β€ 1000) β the number of straight sections of the track.The following (n + 1)-th line contains pairs of integers (xi, yi) ( - 10 000 β€ xi, yi β€ 10 000). The first of these points is the starting position. The i-th straight section of the track begins at the point ... | Print a single integer β the number of dangerous turns on the track. | The first sample corresponds to the picture: The picture shows that you can get in the water under unfortunate circumstances only at turn at the point (1, 1). Thus, the answer is 1. | Input: 60 00 11 11 22 22 00 0 | Output: 1 | Medium | 3 | 1,272 | 927 | 68 | 6 |
542 | D | 542D | D. Superhero's Job | 2,600 | dfs and similar; dp; hashing; math; number theory | It's tough to be a superhero. And it's twice as tough to resist the supervillain who is cool at math. Suppose that you're an ordinary Batman in an ordinary city of Gotham. Your enemy Joker mined the building of the city administration and you only have several minutes to neutralize the charge. To do that you should ent... | The single line of the input contains a single integer A (1 β€ A β€ 1012). | Print the number of solutions of the equation J(x) = A. | Record x|n means that number n divides number x. is defined as the largest positive integer that divides both a and b.In the first sample test the only suitable value of x is 2. Then J(2) = 1 + 2.In the second sample test the following values of x match: x = 14, J(14) = 1 + 2 + 7 + 14 = 24 x = 15, J(15) = 1 + 3 + 5 + 1... | Input: 3 | Output: 1 | Expert | 5 | 835 | 72 | 55 | 5 |
776 | G | 776G | G. Sherlock and the Encrypted Data | 2,900 | bitmasks; combinatorics; dp | Sherlock found a piece of encrypted data which he thinks will be useful to catch Moriarty. The encrypted data consists of two integer l and r. He noticed that these integers were in hexadecimal form.He takes each of the integers from l to r, and performs the following operations: He lists the distinct digits present in... | First line contains the integer q (1 β€ q β€ 10000).Each of the next q lines contain two hexadecimal integers l and r (0 β€ l β€ r < 1615).The hexadecimal integers are written using digits from 0 to 9 and/or lowercase English letters a, b, c, d, e, f.The hexadecimal integers do not contain extra leading zeros. | Output q lines, i-th line contains answer to the i-th query (in decimal notation). | For the second input,1416 = 2010sum = 21 + 24 = 18 Thus, it reduces. And, we can verify that it is the only number in range 1 to 1e that reduces. | Input: 11014 1014 | Output: 1 | Master | 3 | 1,010 | 307 | 82 | 7 |
109 | B | 109B | B. Lucky Probability | 1,900 | brute force; probabilities | 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 and his friend Vasya play an interesting game. Petya randomly chooses an integer p from the inte... | The single line contains five integers pl, pr, vl, vr and k (1 β€ pl β€ pr β€ 109, 1 β€ vl β€ vr β€ 109, 1 β€ k β€ 1000). | On the single line print the result with an absolute error of no more than 10 - 9. | Consider that [a, b] denotes an interval of integers; this interval includes the boundaries. That is, In first case there are 32 suitable pairs: (1, 7), (1, 8), (1, 9), (1, 10), (2, 7), (2, 8), (2, 9), (2, 10), (3, 7), (3, 8), (3, 9), (3, 10), (4, 7), (4, 8), (4, 9), (4, 10), (7, 1), (7, 2), (7, 3), (7, 4), (8, 1), (8,... | Input: 1 10 1 10 2 | Output: 0.320000000000 | Hard | 2 | 553 | 113 | 82 | 1 |
32 | D | 32D | D. Constellation | 1,600 | implementation | A star map in Berland is a checked field n Γ m squares. In each square there is or there is not a star. The favourite constellation of all Berland's astronomers is the constellation of the Cross. This constellation can be formed by any 5 stars so, that for some integer x (radius of the constellation) the following is t... | The first line contains three integers n, m and k (1 β€ n, m β€ 300, 1 β€ k β€ 3Β·107) β height and width of the map and index of the required constellation respectively. The upper-left corner has coordinates (1, 1), and the lower-right β (n, m). Then there follow n lines, m characters each β description of the map. j-th ch... | If the number of the constellations is less than k, output -1. Otherwise output 5 lines, two integers each β coordinates of the required constellation. Output the stars in the following order: central, upper, lower, left, right. | Input: 5 6 1....*....***....*...*....***.. | Output: 2 51 53 52 42 6 | Medium | 1 | 1,126 | 429 | 228 | 0 | |
954 | I | 954I | I. Yet Another String Matching Problem | 2,200 | fft; math | Suppose you have two strings s and t, and their length is equal. You may perform the following operation any number of times: choose two different characters c1 and c2, and replace every occurence of c1 in both strings with c2. Let's denote the distance between strings s and t as the minimum number of operations requir... | The first line contains the string S, and the second β the string T (1 β€ |T| β€ |S| β€ 125000). Both strings consist of lowercase Latin letters from a to f. | Print |S| - |T| + 1 integers. The i-th of these integers must be equal to the distance between the substring of S beginning at i-th index with length |T| and the string T. | Input: abcdefaddcb | Output: 2 3 3 3 | Hard | 2 | 723 | 154 | 171 | 9 | |
1,534 | A | 1534A | A. Colour the Flag | 800 | brute force; implementation | Today we will be playing a red and white colouring game (no, this is not the Russian Civil War; these are just the colours of the Canadian flag).You are given an \(n \times m\) grid of ""R"", ""W"", and ""."" characters. ""R"" is red, ""W"" is white and ""."" is blank. The neighbours of a cell are those that share an e... | The first line contains \(t\) (\(1 \le t \le 100\)), the number of test cases.In each test case, the first line will contain \(n\) (\(1 \le n \le 50\)) and \(m\) (\(1 \le m \le 50\)), the height and width of the grid respectively.The next \(n\) lines will contain the grid. Each character of the grid is either 'R', 'W',... | For each test case, output ""YES"" if there is a valid grid or ""NO"" if there is not.If there is, output the grid on the next \(n\) lines. If there are multiple answers, print any.In the output, the ""YES""s and ""NO""s are case-insensitive, meaning that outputs such as ""yEs"" and ""nO"" are valid. However, the grid ... | The answer for the first example case is given in the example output, and it can be proven that no grid exists that satisfies the requirements of the second example case. In the third example all cells are initially coloured, and the colouring is valid. | Input: 3 4 6 .R.... ...... ...... .W.... 4 4 .R.W .... .... .... 5 1 R W R W R | Output: YES WRWRWR RWRWRW WRWRWR RWRWRW NO YES R W R W R | Beginner | 2 | 617 | 328 | 338 | 15 |
1,389 | A | 1389A | A. LCM Problem | 800 | constructive algorithms; greedy; math; number theory | Let \(LCM(x, y)\) be the minimum positive integer that is divisible by both \(x\) and \(y\). For example, \(LCM(13, 37) = 481\), \(LCM(9, 6) = 18\).You are given two integers \(l\) and \(r\). Find two integers \(x\) and \(y\) such that \(l \le x < y \le r\) and \(l \le LCM(x, y) \le r\). | The first line contains one integer \(t\) (\(1 \le t \le 10000\)) β the number of test cases.Each test case is represented by one line containing two integers \(l\) and \(r\) (\(1 \le l < r \le 10^9\)). | For each test case, print two integers: if it is impossible to find integers \(x\) and \(y\) meeting the constraints in the statement, print two integers equal to \(-1\); otherwise, print the values of \(x\) and \(y\) (if there are multiple valid answers, you may print any of them). | Input: 4 1 1337 13 69 2 4 88 89 | Output: 6 7 14 21 2 4 -1 -1 | Beginner | 4 | 288 | 202 | 283 | 13 | |
1,028 | E | 1028E | E. Restore Array | 2,400 | constructive algorithms | While discussing a proper problem A for a Codeforces Round, Kostya created a cyclic array of positive integers \(a_1, a_2, \ldots, a_n\). Since the talk was long and not promising, Kostya created a new cyclic array \(b_1, b_2, \ldots, b_{n}\) so that \(b_i = (a_i \mod a_{i + 1})\), where we take \(a_{n+1} = a_1\). Here... | The first line contains a single integer \(n\) (\(2 \le n \le 140582\)) β the length of the array \(a\).The second line contains \(n\) integers \(b_1, b_2, \ldots, b_{n}\) (\(0 \le b_i \le 187126\)). | If it is possible to restore some array \(a\) of length \(n\) so that \(b_i = a_i \mod a_{(i \mod n) + 1}\) holds for all \(i = 1, 2, \ldots, n\), print Β«YESΒ» in the first line and the integers \(a_1, a_2, \ldots, a_n\) in the second line. All \(a_i\) should satisfy \(1 \le a_i \le 10^{18}\). We can show that if an ans... | In the first example: \(1 \mod 3 = 1\) \(3 \mod 5 = 3\) \(5 \mod 2 = 1\) \(2 \mod 1 = 0\) | Input: 41 3 1 0 | Output: YES1 3 5 2 | Expert | 1 | 566 | 199 | 506 | 10 |
482 | B | 482B | B. Interesting Array | 1,800 | constructive algorithms; data structures; trees | We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 β€ li β€ ri β€ n) meaning that value should be equal to qi. Your task is to find any interesting array of n elements or state that such array d... | The first line contains two integers n, m (1 β€ n β€ 105, 1 β€ m β€ 105) β the number of elements in the array and the number of limits.Each of the next m lines contains three integers li, ri, qi (1 β€ li β€ ri β€ n, 0 β€ qi < 230) describing the i-th limit. | If the interesting array exists, in the first line print ""YES"" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 β€ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them.If the interesting array doesn't exist, print ""NO"" (without the quotes)... | Input: 3 11 3 3 | Output: YES3 3 3 | Medium | 3 | 499 | 250 | 340 | 4 | |
1,043 | F | 1043F | F. Make It One | 2,500 | bitmasks; combinatorics; dp; math; number theory; shortest paths | Janusz is a businessman. He owns a company ""Januszex"", which produces games for teenagers. Last hit of Januszex was a cool one-person game ""Make it one"". The player is given a sequence of \(n\) integers \(a_i\).It is allowed to select any subset of them, and the score is equal to the greatest common divisor of sele... | The first line contains an only integer \(n\) (\(1 \le n \le 300\,000\)) β the number of integers in the sequence.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 300\,000\)). | If there is no subset of the given sequence with gcd equal to \(1\), output -1.Otherwise, output exactly one integer β the size of the smallest subset with gcd equal to \(1\). | In the first example, selecting a subset of all numbers gives a gcd of \(1\) and for all smaller subsets the gcd is greater than \(1\).In the second example, for all subsets of numbers the gcd is at least \(2\). | Input: 310 6 15 | Output: 3 | Expert | 6 | 501 | 209 | 175 | 10 |
519 | B | 519B | B. A and B and Compilation Errors | 1,100 | data structures; implementation; sortings | A and B are preparing themselves for programming contests.B loves to debug his code. But before he runs the solution and starts debugging, he has to first compile the code.Initially, the compiler displayed n compilation errors, each of them is represented as a positive integer. After some effort, B managed to fix some ... | The first line of the input contains integer n (3 β€ n β€ 105) β the initial number of compilation errors.The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ 109) β the errors the compiler displayed for the first time. The third line contains n - 1 space-separated integers b1, b2, ..., bn - 1 β ... | Print two numbers on a single line: the numbers of the compilation errors that disappeared after B made the first and the second correction, respectively. | In the first test sample B first corrects the error number 8, then the error number 123.In the second test sample B first corrects the error number 1, then the error number 3. Note that if there are multiple errors with the same number, B can correct only one of them in one step. | Input: 51 5 8 123 7123 7 5 15 1 7 | Output: 8123 | Easy | 3 | 852 | 728 | 154 | 5 |
873 | C | 873C | C. Strange Game On Matrix | 1,600 | greedy; two pointers | Ivan is playing a strange game.He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows: Initially Ivan's score is 0; ... | The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1. | Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score. | In the first example Ivan will replace the element a1, 2. | Input: 4 3 20 1 01 0 10 1 01 1 1 | Output: 4 1 | Medium | 2 | 1,027 | 218 | 125 | 8 |
773 | B | 773B | B. Dynamic Problem Scoring | 2,000 | brute force; greedy | Vasya and Petya take part in a Codeforces round. The round lasts for two hours and contains five problems.For this round the dynamic problem scoring is used. If you were lucky not to participate in any Codeforces round with dynamic problem scoring, here is what it means. The maximum point value of the problem depends o... | The first line contains a single integer n (2 β€ n β€ 120) β the number of round participants, including Vasya and Petya.Each of the next n lines contains five integers ai, 1, ai, 2..., ai, 5 ( - 1 β€ ai, j β€ 119) β the number of minutes passed between the beginning of the round and the submission of problem j by particip... | Output a single integer β the number of new accounts Vasya needs to beat Petya, or -1 if Vasya can't achieve his goal. | In the first example, Vasya's optimal strategy is to submit the solutions to the last three problems from two new accounts. In this case the first two problems will have the maximum point value of 1000, while the last three problems will have the maximum point value of 500. Vasya's score will be equal to 980 + 940 + 42... | Input: 25 15 40 70 11550 45 40 30 15 | Output: 2 | Hard | 2 | 2,742 | 600 | 118 | 7 |
1,070 | L | 1070L | L. Odd Federalization | 2,600 | constructive algorithms | Berland has \(n\) cities, some of which are connected by roads. Each road is bidirectional, connects two distinct cities and for each two cities there's at most one road connecting them.The president of Berland decided to split country into \(r\) states in such a way that each city will belong to exactly one of these \... | The input contains one or several test cases. The first input line contains a single integer number \(t\) β number of test cases. Then, \(t\) test cases follow. Solve test cases separately, test cases are completely independent and do not affect each other.Then \(t\) blocks of input data follow. Each block starts from ... | For each test case first print a line containing a single integer \(r\) β smallest possible number of states for which required split is possible. In the next line print \(n\) space-separated integers in range from \(1\) to \(r\), inclusive, where the \(j\)-th number denotes number of state for the \(j\)-th city. If th... | Input: 2 5 31 22 51 5 6 51 22 33 44 24 1 | Output: 11 1 1 1 1 22 1 1 1 1 1 | Expert | 1 | 786 | 961 | 358 | 10 | |
558 | A | 558A | A. Lala Land and Apple Trees | 1,100 | brute force; implementation; sortings | Amr lives in Lala Land. Lala Land is a very beautiful country that is located on a coordinate line. Lala Land is famous with its apple trees growing everywhere.Lala Land has exactly n apple trees. Tree number i is located in a position xi and has ai apples growing on it. Amr wants to collect apples from the apple trees... | The first line contains one number n (1 β€ n β€ 100), the number of apple trees in Lala Land.The following n lines contains two integers each xi, ai ( - 105 β€ xi β€ 105, xi β 0, 1 β€ ai β€ 105), representing the position of the i-th tree and number of apples on it.It's guaranteed that there is at most one apple tree at each... | Output the maximum number of apples Amr can collect. | In the first sample test it doesn't matter if Amr chose at first to go left or right. In both cases he'll get all the apples.In the second sample test the optimal solution is to go left to x = - 1, collect apples from there, then the direction will be reversed, Amr has to go to x = 1, collect apples from there, then th... | Input: 2-1 51 5 | Output: 10 | Easy | 3 | 913 | 379 | 52 | 5 |
1,943 | D1 | 1943D1 | D1. Counting Is Fun (Easy Version) | 2,400 | brute force; combinatorics; dp; math | This is the easy version of the problem. The only difference between the two versions is the constraint on \(n\). You can make hacks only if both versions of the problem are solved.An array \(b\) of \(m\) non-negative integers is said to be good if all the elements of \(b\) can be made equal to \(0\) using the followin... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^3\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains three positive integers \(n\), \(k\) and \(p\) (\(3 \leq n \leq 400\), \(1 \leq k \leq n\), \(10... | For each test case, on a new line, output the number of good arrays modulo \(p\). | In the first test case, the \(4\) good arrays \(a\) are: \([0,0,0]\); \([0,1,1]\); \([1,1,0]\); \([1,1,1]\). | Input: 43 1 9982448534 1 9982443533 2 998244353343 343 998244353 | Output: 4 7 10 456615865 | Expert | 4 | 805 | 543 | 81 | 19 |
818 | A | 818A | A. Diplomas and Certificates | 800 | implementation; math | There are n students who have taken part in an olympiad. Now it's time to award the students.Some of them will receive diplomas, some wiil get certificates, and others won't receive anything. Students with diplomas and certificates are called winners. But there are some rules of counting the number of diplomas and cert... | The first (and the only) line of input contains two integers n and k (1 β€ n, k β€ 1012), where n is the number of students and k is the ratio between the number of certificates and the number of diplomas. | Output three numbers: the number of students with diplomas, the number of students with certificates and the number of students who are not winners in case when the number of winners is maximum possible.It's possible that there are no winners. | Input: 18 2 | Output: 3 6 9 | Beginner | 2 | 832 | 203 | 243 | 8 | |
1,970 | B1 | 1970B1 | B1. Exact Neighbours (Easy) | 1,900 | constructive algorithms | The only difference between this and the hard version is that all \(a_{i}\) are even.After some recent attacks on Hogwarts Castle by the Death Eaters, the Order of the Phoenix has decided to station \(n\) members in Hogsmead Village. The houses will be situated on a picturesque \(n\times n\) square field. Each wizard w... | The first line contains \(n\) (\(2 \leq n \leq 2\cdot 10^{5}\)), the number of houses to be built.The second line contains \(n\) integers \(a_{1}, \ldots, a_{n}\) \((0 \leq a_{i} \leq n)\). All \(a_{i}\) are even. | If there exists such a placement, output YES on the first line; otherwise, output NO.If the answer is YES, output \(n + 1\) more lines describing the placement.The next \(n\) lines should contain the positions of the houses \(1 \leq x_{i}, y_{i} \leq n\) for each wizard.The \(i\)-th element of the last line should cont... | For the sample, the house of the 1st wizard is located at \((4, 4)\), of the 2nd at \((1, 3)\), of the 3rd at \((2, 4)\), of the 4th at \((3, 1)\).The distance from the house of the 1st wizard to the house of the 1st wizard is \(|4 - 4| + |4 - 4| = 0\).The distance from the house of the 2nd wizard to the house of the 1... | Input: 4 0 4 2 4 | Output: YES 4 4 1 3 2 4 3 1 1 1 1 3 | Hard | 1 | 2,086 | 213 | 561 | 19 |
1,438 | B | 1438B | B. Valerii Against Everyone | 1,000 | constructive algorithms; data structures; greedy; sortings | You're given an array \(b\) of length \(n\). Let's define another array \(a\), also of length \(n\), for which \(a_i = 2^{b_i}\) (\(1 \leq i \leq n\)). Valerii says that every two non-intersecting subarrays of \(a\) have different sums of elements. You want to determine if he is wrong. More formally, you need to determ... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). Description of the test cases follows.The first line of every test case contains a single integer \(n\) (\(2 \le n \le 1000\)).The second line of every test case contains \(n\) integers \(b_1,b_2,\ldots... | For every test case, if there exist two non-intersecting subarrays in \(a\) that have the same sum, output YES on a separate line. Otherwise, output NO on a separate line. Also, note that each letter can be in any case. | In the first case, \(a = [16,8,1,2,4,1]\). Choosing \(l_1 = 1\), \(r_1 = 1\), \(l_2 = 2\) and \(r_2 = 6\) works because \(16 = (8+1+2+4+1)\).In the second case, you can verify that there is no way to select to such subarrays. | Input: 2 6 4 3 0 1 2 0 2 2 5 | Output: YES NO | Beginner | 4 | 843 | 352 | 219 | 14 |
630 | D | 630D | D. Hexagons! | 1,100 | math | After a probationary period in the game development company of IT City Petya was included in a group of the programmers that develops a new turn-based strategy game resembling the well known ""Heroes of Might & Magic"". A part of the game is turn-based fights of big squadrons of enemies on infinite fields where every c... | The only line of the input contains one integer n (0 β€ n β€ 109). | Output one integer β the number of hexagons situated not farther than n cells away from a given cell. | Input: 2 | Output: 19 | Easy | 1 | 1,107 | 64 | 101 | 6 | |
1,795 | A | 1795A | A. Two Towers | 800 | brute force; implementation; strings | There are two towers consisting of blocks of two colors: red and blue. Both towers are represented by strings of characters B and/or R denoting the order of blocks in them from the bottom to the top, where B corresponds to a blue block, and R corresponds to a red block. These two towers are represented by strings BRBB ... | The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.Each test case consists of three lines: the first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 20\)) β the number of blocks in the first tower and the number of blocks in the second tower, respectively; the secon... | For each test case, print YES if it is possible to perform several (possibly zero) operations in such a way that the pair of towers becomes beautiful; otherwise print NO.You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answe... | In the first test case, you can move the top block from the first tower to the second tower (see the third picture).In the second test case, you can move the top block from the second tower to the first tower \(6\) times.In the third test case, the pair of towers is already beautiful. | Input: 44 3BRBBRBR4 7BRBRRRBRBRB3 4RBRBRBR5 4BRBRRBRBR | Output: YES YES YES NO | Beginner | 3 | 801 | 526 | 323 | 17 |
459 | A | 459A | A. Pashmak and Garden | 1,200 | implementation | Pashmak has fallen in love with an attractive girl called Parmida since one year ago...Today, Pashmak set up a meeting with his partner in a romantic garden. Unfortunately, Pashmak has forgotten where the garden is. But he remembers that the garden looks like a square with sides parallel to the coordinate axes. He also... | The first line contains four space-separated x1, y1, x2, y2 ( - 100 β€ x1, y1, x2, y2 β€ 100) integers, where x1 and y1 are coordinates of the first tree and x2 and y2 are coordinates of the second tree. It's guaranteed that the given points are distinct. | If there is no solution to the problem, print -1. Otherwise print four space-separated integers x3, y3, x4, y4 that correspond to the coordinates of the two other trees. If there are several solutions you can output any of them. Note that x3, y3, x4, y4 must be in the range ( - 1000 β€ x3, y3, x4, y4 β€ 1000). | Input: 0 0 0 1 | Output: 1 0 1 1 | Easy | 1 | 502 | 253 | 309 | 4 | |
960 | A | 960A | A. Check the string | 1,200 | implementation | A has a string consisting of some number of lowercase English letters 'a'. He gives it to his friend B who appends some number of letters 'b' to the end of this string. Since both A and B like the characters 'a' and 'b', they have made sure that at this point, at least one 'a' and one 'b' exist in the string.B now give... | The first and only line consists of a string \(S\) (\( 1 \le |S| \le 5\,000 \)). It is guaranteed that the string will only consist of the lowercase English letters 'a', 'b', 'c'. | Print ""YES"" or ""NO"", according to the condition. | Consider first example: the number of 'c' is equal to the number of 'a'. Consider second example: although the number of 'c' is equal to the number of the 'b', the order is not correct.Consider third example: the number of 'c' is equal to the number of 'b'. | Input: aaabccc | Output: YES | Easy | 1 | 914 | 179 | 52 | 9 |
1,735 | F | 1735F | F. Pebbles and Beads | 2,900 | data structures; geometry | There are two currencies: pebbles and beads. Initially you have \(a\) pebbles, \(b\) beads.There are \(n\) days, each day you can exchange one currency for another at some exchange rate.On day \(i\), you can exchange \(-p_i \leq x \leq p_i\) pebbles for \(-q_i \leq y \leq q_i\) beads or vice versa. It's allowed not to ... | The first line of the input contains a single integer \(t\) (\(1 \le t \le 10^3\)) β the number of test cases. The description of test cases follows.The first line of each test case contains three integers \(n\), \(a\) and \(b\) (\(1 \le n \le 300\,000\), \(0 \le a, b \le 10^9\)) β the number of days and the initial nu... | Output \(n\) numbers β the maximum number of pebbles at the end of each day.Your answer is considered correct if its absolute or relative error does not exceed \(10^{-6}\).Formally, let your answer be \(a\), and the jury's answer be \(b\). Your answer is accepted if and only if \(\frac{\left|a - b\right|}{\max(1, \left... | In the image below you can see the solutions for the first two test cases. In each line there is an optimal sequence of actions for each day.In the first test case, the optimal strategy for the first day is to do no action at all, as we can only decrease the number of pebbles. The optimal strategy for the second day is... | Input: 32 6 02 34 23 0 64 2 102 3 101 10 10331000 | Output: 6 8 4 6 9.000000 10.33 | Master | 2 | 809 | 665 | 346 | 17 |
1,201 | E1 | 1201E1 | E1. Knightmare (easy) | 2,900 | graphs; interactive; shortest paths | This problem only differs from the next problem in constraints.This is an interactive problem.Alice and Bob are playing a game on the chessboard of size \(n \times m\) where \(n\) and \(m\) are even. The rows are numbered from \(1\) to \(n\) and the columns are numbered from \(1\) to \(m\). There are two knights on the... | In the first example, the white knight can reach it's target square in one move.In the second example black knight wins, no matter what white knight moves. | Input: 8 8 2 3 1 8 | Output: WHITE 4 4 | Master | 3 | 2,080 | 0 | 0 | 12 | ||
1,876 | B | 1876B | B. Effects of Anti Pimples | 1,500 | combinatorics; number theory; sortings | Chaneka has an array \([a_1,a_2,\ldots,a_n]\). Initially, all elements are white. Chaneka will choose one or more different indices and colour the elements at those chosen indices black. Then, she will choose all white elements whose indices are multiples of the index of at least one black element and colour those elem... | The first line contains a single integer \(n\) (\(1 \leq n \leq 10^5\)) β the size of array \(a\).The second line contains \(n\) integers \(a_1,a_2,a_3,\ldots,a_n\) (\(0\leq a_i\leq10^5\)). | An integer representing the sum of scores for all possible ways Chaneka can choose the black indices, modulo \(998\,244\,353\). | In the first example, below are the \(15\) possible ways to choose the black indices: Index \(1\) is black. Indices \(2\), \(3\), and \(4\) are green. Maximum value among them is \(19\). Index \(2\) is black. Index \(4\) is green. Maximum value among them is \(14\). Index \(3\) is black. Maximum value among them is \(1... | Input: 4 19 14 19 9 | Output: 265 | Medium | 3 | 647 | 189 | 127 | 18 |
1,290 | D | 1290D | D. Coffee Varieties (hard version) | 3,000 | constructive algorithms; graphs; interactive | This is the hard version of the problem. You can find the easy version in the Div. 2 contest. Both versions only differ in the number of times you can ask your friend to taste coffee.This is an interactive problem.You're considering moving to another city, where one of your friends already lives. There are \(n\) cafΓ©s ... | The first line contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 1024\), \(k\) and \(n\) are powers of two).It is guaranteed that \(\dfrac{3n^2}{2k} \le 15\ 000\). | In the first example, the array is \(a = [1, 4, 1, 3]\). The city produces \(3\) different varieties of coffee (\(1\), \(3\) and \(4\)).The successive varieties of coffee tasted by your friend are \(1, 4, \textbf{1}, 3, 3, 1, 4\) (bold answers correspond to Y answers). Note that between the two ? 4 asks, there is a res... | Input: 4 2 N N Y N N N N | Output: ? 1 ? 2 ? 3 ? 4 R ? 4 ? 1 ? 2 ! 3 | Master | 3 | 1,902 | 172 | 0 | 12 | |
240 | F | 240F | F. TorCoder | 2,600 | data structures | A boy named Leo doesn't miss a single TorCoder contest round. On the last TorCoder round number 100666 Leo stumbled over the following problem. He was given a string s, consisting of n lowercase English letters, and m queries. Each query is characterised by a pair of integers li, ri (1 β€ li β€ ri β€ n). We'll consider th... | The first input line contains two integers n and m (1 β€ n, m β€ 105) β the string length and the number of the queries.The second line contains string s, consisting of n lowercase Latin letters.Each of the next m lines contains a pair of integers li, ri (1 β€ li β€ ri β€ n) β a query to apply to the string. | In a single line print the result of applying m queries to string s. Print the queries in the order in which they are given in the input. | A substring (li, ri) 1 β€ li β€ ri β€ n) of string s = s1s2... sn of length n is a sequence of characters slisli + 1...sri.A string is a palindrome, if it reads the same from left to right and from right to left.String x1x2... xp is lexicographically smaller than string y1y2... yq, if either p < q and x1 = y1, x2 = y2, ..... | Input: 7 2aabcbaa1 35 7 | Output: abacaba | Expert | 1 | 1,056 | 304 | 137 | 2 |
1,612 | E | 1612E | E. Messages | 2,000 | brute force; dp; greedy; probabilities; sortings | Monocarp is a tutor of a group of \(n\) students. He communicates with them using a conference in a popular messenger.Today was a busy day for Monocarp β he was asked to forward a lot of posts and announcements to his group, that's why he had to write a very large number of messages in the conference. Monocarp knows th... | The first line contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of students in the conference.Then \(n\) lines follow. The \(i\)-th line contains two integers \(m_i\) and \(k_i\) (\(1 \le m_i \le 2 \cdot 10^5\); \(1 \le k_i \le 20\)) β the index of the message which Monocarp wants the \(i\)-th stu... | In the first line, print one integer \(t\) (\(1 \le t \le 2 \cdot 10^5\)) β the number of messages Monocarp should pin. In the second line, print \(t\) distinct integers \(c_1\), \(c_2\), ..., \(c_t\) (\(1 \le c_i \le 2 \cdot 10^5\)) β the indices of the messages Monocarp should pin. The messages can be listed in any o... | Let's consider the examples from the statement. In the first example, Monocarp pins the messages \(5\) and \(10\). if the first student reads the message \(5\), the second student reads the messages \(5\) and \(10\), and the third student reads the messages \(5\) and \(10\), the number of students which have read their... | Input: 3 10 1 10 2 5 2 | Output: 2 5 10 | Hard | 5 | 1,491 | 413 | 374 | 16 |
566 | C | 566C | C. Logistical Questions | 3,000 | dfs and similar; divide and conquer; trees | Some country consists of n cities, connected by a railroad network. The transport communication of the country is so advanced that the network consists of a minimum required number of (n - 1) bidirectional roads (in the other words, the graph of roads is a tree). The i-th road that directly connects cities ai and bi, h... | The first line of the input contains number n (1 β€ n β€ 200 000) β the number of cities in the country.The next line contains n integers w1, w2, ..., wn (0 β€ wi β€ 108) β the number of finalists living in each city of the country.Next (n - 1) lines contain the descriptions of the railroad, the i-th line contains three in... | Print two numbers β an integer f that is the number of the optimal city to conduct the competition, and the real number c, equal to the minimum total cost of transporting all the finalists to the competition. Your answer will be considered correct if two conditions are fulfilled at the same time: The absolute or relati... | In the sample test an optimal variant of choosing a city to conduct the finals of the competition is 3. At such choice the cost of conducting is burles.In the second sample test, whatever city you would choose, you will need to pay for the transport for five participants, so you will need to pay burles for each one of ... | Input: 53 1 2 6 51 2 32 3 14 3 95 3 1 | Output: 3 192.0 | Master | 3 | 1,460 | 380 | 618 | 5 |
2,089 | A | 2089A | A. Simple Permutation | 1,700 | constructive algorithms; number theory | Given an integer \(n\). Construct a permutation \(p_1, p_2, \ldots, p_n\) of length \(n\) that satisfies the following property:For \(1 \le i \le n\), define \(c_i = \lceil \frac{p_1+p_2+\ldots +p_i}{i} \rceil\), then among \(c_1,c_2,\ldots,c_n\) there must be at least \(\lfloor \frac{n}{3} \rfloor - 1\) prime numbers. | The first line contains an integer \(t\) (\(1 \le t \le 10\)) β the number of test cases. The description of the test cases follows.In a single line of each test case, there is a single integer \(n\) (\(2 \le n \le 10^5)\) β the size of the permutation. | For each test case, output the permutation \(p_1,p_2,\ldots,p_n\) of length \(n\) that satisfies the condition. It is guaranteed that such a permutation always exists. | In the first test case, \(c_1 = \lceil \frac{2}{1} \rceil = 2\), \(c_2 = \lceil \frac{2+1}{2} \rceil = 2\). Both are prime numbers.In the third test case, \(c_1 = \lceil \frac{2}{1} \rceil = 2\), \(c_2 = \lceil \frac{3}{2} \rceil = 2\), \(c_3 = \lceil \frac{6}{3} \rceil = 2\), \(c_4 = \lceil \frac{10}{4} \rceil = 3\), ... | Input: 3235 | Output: 2 1 2 1 3 2 1 3 4 5 | Medium | 2 | 320 | 253 | 167 | 20 |
1,157 | E | 1157E | E. Minimum Array | 1,700 | binary search; data structures; greedy | You are given two arrays \(a\) and \(b\), both of length \(n\). All elements of both arrays are from \(0\) to \(n-1\).You can reorder elements of the array \(b\) (if you want, you may leave the order of elements as it is). After that, let array \(c\) be the array of length \(n\), the \(i\)-th element of this array is \... | The first line of the input contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of elements in \(a\), \(b\) and \(c\).The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i < n\)), where \(a_i\) is the \(i\)-th element of \(a\).The third line of the input contains ... | Print the lexicographically minimum possible array \(c\). Recall that your task is to reorder elements of the array \(b\) and obtain the lexicographically minimum possible array \(c\), where the \(i\)-th element of \(c\) is \(c_i = (a_i + b_i) \% n\). | Input: 4 0 1 2 1 3 2 1 1 | Output: 1 0 0 2 | Medium | 3 | 705 | 428 | 251 | 11 | |
162 | C | 162C | C. Prime factorization | 1,800 | *special | You are given a positive integer n. Output its prime factorization.If n = a1b1 a2b2 ... akbk (bi > 0), where ak are prime numbers, the output of your program should look as follows: a1*...*a1*a2*...*a2*...*ak*...*ak, where the factors are ordered in non-decreasing order, and each factor ai is printed bi times. | The only line of input contains an integer n (2 β€ n β€ 10000). | Output the prime factorization of n, as described above. | Input: 245 | Output: 5*7*7 | Medium | 1 | 311 | 61 | 56 | 1 | |
2,010 | B | 2010B | B. Three Brothers | 800 | brute force; implementation; math | Three brothers agreed to meet. Let's number the brothers as follows: the oldest brother is number 1, the middle brother is number 2, and the youngest brother is number 3.When it was time for the meeting, one of the brothers was late. Given the numbers of the two brothers who arrived on time, you need to determine the n... | The first line of input contains two different integers a and b (1 β€ a, b β€ 3, a β b) β the numbers of the brothers who arrived on time. The numbers are given in arbitrary order. | Output a single integer β the number of the brother who was late to the meeting. | Input: 3 1 | Output: 2 | Beginner | 3 | 354 | 178 | 80 | 20 | |
1,765 | G | 1765G | G. Guess the String | 2,600 | constructive algorithms; interactive; probabilities | This is an interactive problem. You have to use flush operation right after printing each line. For example, in C++ you should use the function fflush(stdout), in Java or Kotlin β System.out.flush(), and in Python β sys.stdout.flush().The jury has a string \(s\) consisting of characters 0 and/or 1. The first character ... | The example contains one possible way of interaction in a test where \(t = 2\), and the strings guessed by the jury are 011001 and 00111. Note that everything after the // sign is a comment that explains which line means what in the interaction. The jury program won't print these comments in the actual problem, and you... | Input: 2 // 2 test cases 6 // n = 6 0 // p[3] = 0 1 // q[2] = 1 4 // q[6] = 4 1 // p[4] = 1 1 // answer is correct 5 // n = 5 1 // p[2] = 1 2 // q[4] = 2 2 // q[5] = 2 1 // answer is correct | Output: 1 3 // what is p[3]? 2 2 // what is q[2]? 2 6 // what is q[6]? 1 4 // what is p[4]? 0 011001 // the guess is 011001 1 2... | Expert | 3 | 1,429 | 0 | 0 | 17 | ||
710 | A | 710A | A. King Moves | 800 | implementation | The only king stands on the standard chess board. You are given his position in format ""cd"", where c is the column from 'a' to 'h' and d is the row from '1' to '8'. Find the number of moves permitted for the king.Check the king's moves here https://en.wikipedia.org/wiki/King_(chess). King moves from the position e4 | The only line contains the king's position in the format ""cd"", where 'c' is the column from 'a' to 'h' and 'd' is the row from '1' to '8'. | Print the only integer x β the number of moves permitted for the king. | Input: e4 | Output: 8 | Beginner | 1 | 318 | 140 | 70 | 7 | |
976 | C | 976C | C. Nested Segments | 1,500 | greedy; implementation; sortings | You are given a sequence a1, a2, ..., an of one-dimensional segments numbered 1 through n. Your task is to find two distinct indices i and j such that segment ai lies within segment aj.Segment [l1, r1] lies within segment [l2, r2] iff l1 β₯ l2 and r1 β€ r2.Print indices i and j. If there are multiple answers, print any o... | The first line contains one integer n (1 β€ n β€ 3Β·105) β the number of segments.Each of the next n lines contains two integers li and ri (1 β€ li β€ ri β€ 109) β the i-th segment. | Print two distinct indices i and j such that segment ai lies within segment aj. If there are multiple answers, print any of them. If no answer exists, print -1 -1. | In the first example the following pairs are considered correct: (2, 1), (3, 1), (4, 1), (5, 1) β not even touching borders; (3, 2), (4, 2), (3, 5), (4, 5) β touch one border; (5, 2), (2, 5) β match exactly. | Input: 51 102 93 92 32 9 | Output: 2 1 | Medium | 3 | 361 | 175 | 163 | 9 |
409 | H | 409H | H. A + B Strikes Back | 1,500 | *special; brute force; constructive algorithms; dsu; implementation | A + B is often used as an example of the easiest problem possible to show some contest platform. However, some scientists have observed that sometimes this problem is not so easy to get accepted. Want to try? | The input contains two integers a and b (0 β€ a, b β€ 103), separated by a single space. | Output the sum of the given integers. | Input: 5 14 | Output: 19 | Medium | 5 | 208 | 86 | 37 | 4 | |
830 | B | 830B | B. Cards Sorting | 1,600 | data structures; implementation; sortings | Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this integer is between 1 and 100 000, inclusive. It is possible that some cards have the same integers on them.Vasily decided to sort the cards. To do this, he repeatedly takes the top card from the deck, and if the number on i... | The first line contains single integer n (1 β€ n β€ 100 000) β the number of cards in the deck.The second line contains a sequence of n integers a1, a2, ..., an (1 β€ ai β€ 100 000), where ai is the number written on the i-th from top card in the deck. | Print the total number of times Vasily takes the top card from the deck. | In the first example Vasily at first looks at the card with number 6 on it, puts it under the deck, then on the card with number 3, puts it under the deck, and then on the card with number 1. He places away the card with 1, because the number written on it is the minimum among the remaining cards. After that the cards ... | Input: 46 3 1 2 | Output: 7 | Medium | 3 | 801 | 248 | 72 | 8 |
546 | B | 546B | B. Soldier and Badges | 1,200 | brute force; greedy; implementation; sortings | Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge has a coolness factor, which shows how much it's owner reached. Coolness factor can be increased by one for the cost of one coin. For every pair of soldiers one of them should get a badge with strictly higher factor than the second ... | First line of input consists of one integer n (1 β€ n β€ 3000).Next line consists of n integers ai (1 β€ ai β€ n), which stand for coolness factor of each badge. | Output single integer β minimum amount of coins the colonel has to pay. | In first sample test we can increase factor of first badge by 1.In second sample test we can increase factors of the second and the third badge by 1. | Input: 41 3 1 4 | Output: 1 | Easy | 4 | 672 | 157 | 71 | 5 |
1,662 | O | 1662O | O. Circular Maze | 0 | brute force; dfs and similar; graphs; implementation | You are given a circular maze such as the ones shown in the figures. Determine if it can be solved, i.e., if there is a path which goes from the center to the outside of the maze which does not touch any wall. The maze is described by \(n\) walls. Each wall can be either circular or straight. Circular walls are describ... | Each test contains multiple test cases. The first line contains an integer \(t\) (\(1\le t\le 20\)) β the number of test cases. The descriptions of the \(t\) test cases follow.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 5000\)) β the number of walls. Each of the following \(n\) lines eac... | For each test case, print YES if the maze can be solved and NO otherwise. | The two sample test cases correspond to the two mazes in the picture. | Input: 2 5 C 1 180 90 C 5 250 230 C 10 150 140 C 20 185 180 S 1 20 180 6 C 1 180 90 C 5 250 230 C 10 150 140 C 20 185 180 S 1 20 180 S 5 10 0 | Output: YES NO | Beginner | 4 | 882 | 939 | 73 | 16 |
733 | B | 733B | B. Parade | 1,100 | math | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits beg... | The first line contains single integer n (1 β€ n β€ 105) β the number of columns. The next n lines contain the pairs of integers li and ri (1 β€ li, ri β€ 500) β the number of soldiers in the i-th column which start to march from the left or the right leg respectively. | Print single integer k β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.Consider that columns are numbered from 1 to n in the order they are given in the input data.If there are several answers, print any of them. | In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.If you give the order to change the leg to the third colu... | Input: 35 68 910 3 | Output: 3 | Easy | 1 | 1,344 | 265 | 310 | 7 |
1,787 | B | 1787B | B. Number Factorization | 1,100 | greedy; math; number theory | Given an integer \(n\).Consider all pairs of integer arrays \(a\) and \(p\) of the same length such that \(n = \prod a_i^{p_i}\) (i.e. \(a_1^{p_1}\cdot a_2^{p_2}\cdot\ldots\)) (\(a_i>1;p_i>0\)) and \(a_i\) is the product of some (possibly one) distinct prime numbers.For example, for \(n = 28 = 2^2\cdot 7^1 = 4^1 \cdot ... | Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. Each test case contains only one integer \(n\) (\(2 \le n \le 10^9\)). | For each test case, print the maximum value of \(\sum a_i \cdot p_i\). | In the first test case, \(100 = 10^2\) so that \(a = [10]\), \(p = [2]\) when \(\sum a_i \cdot p_i\) hits the maximum value \(10\cdot 2 = 20\). Also, \(a = [100]\), \(p = [1]\) does not work since \(100\) is not made of distinct prime factors.In the second test case, we can consider \(10\) as \(10^1\), so \(a = [10]\),... | Input: 71001086413005619210000000002999999018 | Output: 20 10 22 118 90 2 333333009 | Easy | 3 | 746 | 202 | 70 | 17 |
1,919 | C | 1919C | C. Grouping Increases | 1,400 | data structures; dp; greedy | You are given an array \(a\) of size \(n\). You will do the following process to calculate your penalty: Split array \(a\) into two (possibly empty) subsequences\(^\dagger\) \(s\) and \(t\) such that every element of \(a\) is either in \(s\) or \(t^\ddagger\). For an array \(b\) of size \(m\), define the penalty \(p(b)... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1\le n\le 2\cdot 10^5\)) β the size of the array \(a\).The second line... | For each test case, output a single integer representing the minimum possible penalty you will receive. | In the first test case, a possible way to split \(a\) is \(s=[2,4,5]\) and \(t=[1,3]\). The penalty is \(p(s)+p(t)=2 + 1 =3\).In the second test case, a possible way to split \(a\) is \(s=[8,3,1]\) and \(t=[2,1,7,4,3]\). The penalty is \(p(s)+p(t)=0 + 1 =1\).In the third test case, a possible way to split \(a\) is \(s=... | Input: 551 2 3 4 588 2 3 1 1 7 4 353 3 3 3 31122 1 | Output: 3 1 0 0 0 | Easy | 3 | 998 | 518 | 103 | 19 |
1,237 | D | 1237D | D. Balanced Playlist | 2,000 | binary search; data structures; implementation | Your favorite music streaming platform has formed a perfectly balanced playlist exclusively for you. The playlist consists of \(n\) tracks numbered from \(1\) to \(n\). The playlist is automatic and cyclic: whenever track \(i\) finishes playing, track \(i+1\) starts playing automatically; after track \(n\) goes track \... | The first line contains a single integer \(n\) (\(2 \le n \le 10^5\)), denoting the number of tracks in the playlist.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)), denoting coolnesses of the tracks. | Output \(n\) integers \(c_1, c_2, \ldots, c_n\), where \(c_i\) is either the number of tracks you will listen to if you start listening from track \(i\) or \(-1\) if you will be listening to music indefinitely. | In the first example, here is what will happen if you start with... track \(1\): listen to track \(1\), stop as \(a_2 < \frac{a_1}{2}\). track \(2\): listen to track \(2\), stop as \(a_3 < \frac{a_2}{2}\). track \(3\): listen to track \(3\), listen to track \(4\), listen to track \(1\), stop as \(a_2 < \frac{\max(a_3, ... | Input: 4 11 5 2 7 | Output: 1 1 3 2 | Hard | 3 | 1,080 | 243 | 210 | 12 |
258 | B | 258B | B. Little Elephant and Elections | 1,900 | brute force; combinatorics; dp | There have recently been elections in the zoo. Overall there were 7 main political parties: one of them is the Little Elephant Political Party, 6 other parties have less catchy names.Political parties find their number in the ballot highly important. Overall there are m possible numbers: 1, 2, ..., m. Each of these 7 p... | A single line contains a single positive integer m (7 β€ m β€ 109) β the number of possible numbers in the ballot. | In a single line print a single integer β the answer to the problem modulo 1000000007 (109 + 7). | Input: 7 | Output: 0 | Hard | 3 | 1,005 | 112 | 96 | 2 | |
294 | C | 294C | C. Shaass and Lights | 1,900 | combinatorics; number theory | There are n lights aligned in a row. These lights are numbered 1 to n from left to right. Initially some of the lights are switched on. Shaass wants to switch all the lights on. At each step he can switch a light on (this light should be switched off at that moment) if there's at least one adjacent light which is alrea... | The first line of the input contains two integers n and m where n is the number of lights in the sequence and m is the number of lights which are initially switched on, (1 β€ n β€ 1000, 1 β€ m β€ n). The second line contains m distinct integers, each between 1 to n inclusive, denoting the indices of lights which are initia... | In the only line of the output print the number of different possible ways to switch on all the lights modulo 1000000007 (109 + 7). | Input: 3 11 | Output: 1 | Hard | 2 | 525 | 336 | 131 | 2 | |
1,266 | F | 1266F | F. Almost Same Distance | 2,900 | dfs and similar; graphs | Let \(G\) be a simple graph. Let \(W\) be a non-empty subset of vertices. Then \(W\) is almost-\(k\)-uniform if for each pair of distinct vertices \(u,v \in W\) the distance between \(u\) and \(v\) is either \(k\) or \(k+1\).You are given a tree on \(n\) vertices. For each \(i\) between \(1\) and \(n\), find the maximu... | The first line contains a single integer \(n\) (\(2 \leq n \leq 5 \cdot 10^5\)) β the number of vertices of the tree.Then \(n-1\) lines follows, the \(i\)-th of which consisting of two space separated integers \(u_i\), \(v_i\) (\(1 \leq u_i, v_i \leq n\)) meaning that there is an edge between vertices \(u_i\) and \(v_i... | Output a single line containing \(n\) space separated integers \(a_i\), where \(a_i\) is the maximum size of an almost-\(i\)-uniform set. | Consider the first example. The only maximum almost-\(1\)-uniform set is \(\{1, 2, 3, 4\}\). One of the maximum almost-\(2\)-uniform sets is or \(\{2, 3, 5\}\), another one is \(\{2, 3, 4\}\). A maximum almost-\(3\)-uniform set is any pair of vertices on distance \(3\). Any single vertex is an almost-\(k\)-uniform set ... | Input: 5 1 2 1 3 1 4 4 5 | Output: 4 3 2 1 1 | Master | 2 | 358 | 370 | 137 | 12 |
1,703 | G | 1703G | G. Good Key, Bad Key | 1,600 | bitmasks; brute force; dp; greedy; math | There are \(n\) chests. The \(i\)-th chest contains \(a_i\) coins. You need to open all \(n\) chests in order from chest \(1\) to chest \(n\).There are two types of keys you can use to open a chest: a good key, which costs \(k\) coins to use; a bad key, which does not cost any coins, but will halve all the coins in eac... | The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \leq n \leq 10^5\); \(0 \leq k \leq 10^9\)) β the number of chests and the cost of a good key respectively.The second line of each test case cont... | For each test case output a single integer β the maximum number of coins you can obtain after opening the chests in order from chest \(1\) to chest \(n\).Please note, that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like... | In the first test case, one possible strategy is as follows: Buy a good key for \(5\) coins, and open chest \(1\), receiving \(10\) coins. Your current balance is \(0 + 10 - 5 = 5\) coins. Buy a good key for \(5\) coins, and open chest \(2\), receiving \(10\) coins. Your current balance is \(5 + 10 - 5 = 10\) coins. Us... | Input: 54 510 10 3 11 213 1210 10 2912 515 74 89 45 18 69 67 67 11 96 23 592 5785 60 | Output: 11 0 13 60 58 | Medium | 5 | 1,193 | 473 | 340 | 17 |
1,583 | A | 1583A | A. Windblume Ode | 800 | math; number theory | A bow adorned with nameless flowers that bears the earnest hopes of an equally nameless person.You have obtained the elegant bow known as the Windblume Ode. Inscribed in the weapon is an array of \(n\) (\(n \ge 3\)) positive distinct integers (i.e. different, no duplicates are allowed).Find the largest subset (i.e. hav... | Each test consists of multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). Description of the test cases follows.The first line of each test case contains an integer \(n\) (\(3 \leq n \leq 100\)) β the length of the array.The second line of each test case contains \(n\) dis... | Each test case should have two lines of output.The first line should contain a single integer \(x\): the size of the largest subset with composite sum. The next line should contain \(x\) space separated integers representing the indices of the subset of the initial array. | In the first test case, the subset \(\{a_2, a_1\}\) has a sum of \(9\), which is a composite number. The only subset of size \(3\) has a prime sum equal to \(11\). Note that you could also have selected the subset \(\{a_1, a_3\}\) with sum \(8 + 2 = 10\), which is composite as it's divisible by \(2\).In the second test... | Input: 4 3 8 1 2 4 6 9 4 2 9 1 2 3 4 5 6 7 8 9 3 200 199 198 | Output: 2 2 1 4 2 1 4 3 9 6 9 1 2 3 4 5 7 8 3 1 2 3 | Beginner | 2 | 755 | 419 | 272 | 15 |
2,001 | A | 2001A | A. Make All Equal | 800 | greedy; implementation | You are given a cyclic array \(a_1, a_2, \ldots, a_n\).You can perform the following operation on \(a\) at most \(n - 1\) times: Let \(m\) be the current size of \(a\), you can choose any two adjacent elements where the previous one is no greater than the latter one (In particular, \(a_m\) and \(a_1\) are adjacent and ... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 500\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 100\)) β the length of the array \(a\).The second line of each test case contain... | For each test case, output a single line containing an integer: the minimum number of operations needed to make all elements in \(a\) equal. | In the first test case, there is only one element in \(a\), so we can't do any operation.In the second test case, we can perform the following operations to make all elements in \(a\) equal: choose \(i = 2\), delete \(a_3\), then \(a\) would become \([1, 2]\). choose \(i = 1\), delete \(a_1\), then \(a\) would become \... | Input: 71131 2 331 2 255 4 3 2 161 1 2 2 3 388 7 6 3 8 7 6 361 1 4 5 1 4 | Output: 0 2 1 4 4 6 3 | Beginner | 2 | 660 | 415 | 140 | 20 |
1,786 | A2 | 1786A2 | A2. Alternating Deck (hard version) | 800 | implementation | This is a hard version of the problem. In this version, there are two colors of the cards.Alice has \(n\) cards, each card is either black or white. The cards are stacked in a deck in such a way that the card colors alternate, starting from a white card. Alice deals the cards to herself and to Bob, dealing at once seve... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 200\)). The description of the test cases followsThe only line of each test case contains a single integer \(n\) (\(1 \le n \le 10^6\)) β the number of cards. | For each test case print four integers β the number of cards in the end for each player β in this order: white cards Alice has, black cards Alice has, white cards Bob has, black cards Bob has. | Input: 51061781000000 | Output: 3 2 2 3 1 0 2 3 6 4 3 4 2 1 2 3 250278 249924 249722 250076 | Beginner | 1 | 995 | 268 | 192 | 17 | |
2,041 | D | 2041D | D. Drunken Maze | 1,700 | brute force; dfs and similar; graphs; shortest paths | Image generated by ChatGPT 4o. You are given a two-dimensional maze with a start and end position. Your task is to find the fastest way to get from the start to the end position. The fastest way is to make the minimum number of steps where one step is going left, right, up, or down. Of course, you cannot walk through w... | The first line contains two numbers \(n\) and \(m\), which are the height and width of the maze. This is followed by an ASCII-representation of the maze where \(\tt{\#}\) is a wall, \(\tt{.}\) is an empty space, and \(\tt S\) and \(\tt T\) are the start and end positions. \(12 \leq n\times m \leq 200000\). \(3\leq n,m ... | The minimum number of steps to reach the end position from the start position or -1 if that is impossible. | Input: 7 12#############S........T##.########.##..........##..........##..#..#....############# | Output: 15 | Medium | 4 | 721 | 472 | 106 | 20 | |
1,196 | F | 1196F | F. K-th Path | 2,200 | brute force; constructive algorithms; shortest paths; sortings | You are given a connected undirected weighted graph consisting of \(n\) vertices and \(m\) edges.You need to print the \(k\)-th smallest shortest path in this graph (paths from the vertex to itself are not counted, paths from \(i\) to \(j\) and from \(j\) to \(i\) are counted as one).More formally, if \(d\) is the matr... | The first line of the input contains three integers \(n, m\) and \(k\) (\(2 \le n \le 2 \cdot 10^5\), \(n - 1 \le m \le \min\Big(\frac{n(n-1)}{2}, 2 \cdot 10^5\Big)\), \(1 \le k \le \min\Big(\frac{n(n-1)}{2}, 400\Big)\) β the number of vertices in the graph, the number of edges in the graph and the value of \(k\), corr... | Print one integer β the length of the \(k\)-th smallest shortest path in the given graph (paths from the vertex to itself are not counted, paths from \(i\) to \(j\) and from \(j\) to \(i\) are counted as one). | Input: 6 10 5 2 5 1 5 3 9 6 2 2 1 3 1 5 1 8 6 5 10 1 6 5 6 4 6 3 6 2 3 4 5 | Output: 3 | Hard | 4 | 578 | 833 | 209 | 11 | |
1,250 | C | 1250C | C. Trip to Saint Petersburg | 2,100 | data structures | You are planning your trip to Saint Petersburg. After doing some calculations, you estimated that you will have to spend \(k\) rubles each day you stay in Saint Petersburg β you have to rent a flat, to eat at some local cafe, et cetera. So, if the day of your arrival is \(L\), and the day of your departure is \(R\), yo... | The first line contains two integers \(n\) and \(k\) (\(1 \le n \le 2\cdot10^5\), \(1 \le k \le 10^{12}\)) β the number of projects and the amount of money you have to spend during each day in Saint Petersburg, respectively.Then \(n\) lines follow, each containing three integers \(l_i\), \(r_i\), \(p_i\) (\(1 \le l_i \... | If it is impossible to plan a trip with strictly positive profit, print the only integer \(0\).Otherwise, print two lines. The first line should contain four integers \(p\), \(L\), \(R\) and \(m\) β the maximum profit you can get, the starting day of your trip, the ending day of your trip and the number of projects you... | Input: 4 5 1 1 3 3 3 11 5 5 17 7 7 4 | Output: 13 3 5 2 3 2 | Hard | 1 | 1,594 | 542 | 578 | 12 | |
1,934 | D1 | 1934D1 | D1. XOR Break β Solo Version | 2,100 | bitmasks; constructive algorithms; greedy | This is the solo version of the problem. Note that the solution of this problem may or may not share ideas with the solution of the game version. You can solve and get points for both versions independently.You can make hacks only if both versions of the problem are solved.Given an integer variable \(x\) with the initi... | The first line contains one positive integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Each test case consists of a single line containing two integers \(n\) and \(m\) (\(1 \leq m \lt n \leq 10^{18}\)) β the initial value of \(x\) and the target value of \(x\). | For each test case, output your answer in the following format.If it is not possible to achieve \(m\) in \(63\) operations, print \(-1\).Otherwise, The first line should contain \(k\) (\(1 \leq k \leq 63\)) β where \(k\) is the number of operations required.The next line should contain \(k+1\) integers β the sequence w... | In the first test case \(n = 7\), for the first operation \(x = 7\) if we choose \(y = 3\) then \((7 \oplus 3) \lt 7\), hence we can update \(x\) with \(3\) which is equal to \(m\).In the second test case \(n = 4\), for the first operation \(x = 4\).If we choose: \(y = 1\) then \((4 \oplus 1) \gt 4\) \(y = 2\) then \((... | Input: 37 34 2481885160128643072 45035996273704960 | Output: 1 7 3 -1 3 481885160128643072 337769972052787200 49539595901075456 45035996273704960 | Hard | 3 | 836 | 275 | 453 | 19 |
2,121 | F | 2121F | F. Yamakasi | 1,800 | binary search; brute force; data structures; greedy; two pointers | You are given an array of integers \(a_1, a_2, \ldots, a_n\) and two integers \(s\) and \(x\). Count the number of subsegments of the array whose sum of elements equals \(s\) and whose maximum value equals \(x\).More formally, count the number of pairs \(1 \leq l \leq r \leq n\) such that: \(a_l + a_{l + 1} + \ldots + ... | Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains three integers \(n\), \(s\), and \(x\) (\(1 \leq n \leq 2 \cdot 10^5\), \(-2 \cdot 10^{14} \l... | For each test case, output the number of subsegments of the array whose sum of elements equals \(s\) and whose maximum value equals \(x\). | In the first test case, the suitable subsegment is \(l = 1\), \(r = 1\).In the third test case, the suitable subsegments are \(l = 1\), \(r = 1\) and \(l = 3\), \(r = 3\). In the fifth test case, the suitable subsegments are \(l = 1\), \(r = 3\) and \(l = 6\), \(r = 8\).In the sixth test case, the suitable subsegments ... | Input: 91 0 001 -2 -1-23 -1 -1-1 1 -16 -3 -2-1 -1 -1 -2 -1 -18 3 22 2 -1 -2 3 -1 2 29 6 31 2 3 1 2 3 1 2 313 7 30 -1 3 3 3 -2 1 2 2 3 -1 0 32 -2 -1-2 -12 -2 -1-1 -2 | Output: 1 0 2 0 2 7 8 0 0 | Medium | 5 | 373 | 587 | 138 | 21 |
1,204 | A | 1204A | A. BowWow and the Timetable | 1,000 | math | In the city of Saint Petersburg, a day lasts for \(2^{100}\) minutes. From the main station of Saint Petersburg, a train departs after \(1\) minute, \(4\) minutes, \(16\) minutes, and so on; in other words, the train departs at time \(4^k\) for each integer \(k \geq 0\). Team BowWow has arrived at the station at the ti... | The first line contains a single binary number \(s\) (\(0 \leq s < 2^{100}\)) without leading zeroes. | Output a single number β the number of trains which have departed strictly before the time \(s\). | In the first example \(100000000_2 = 256_{10}\), missed trains have departed at \(1\), \(4\), \(16\) and \(64\).In the second example \(101_2 = 5_{10}\), trains have departed at \(1\) and \(4\).The third example is explained in the statements. | Input: 100000000 | Output: 4 | Beginner | 1 | 721 | 101 | 97 | 12 |
258 | A | 258A | A. Little Elephant and Bits | 1,100 | greedy; math | The Little Elephant has an integer a, written in the binary notation. He wants to write this number on a piece of paper.To make sure that the number a fits on the piece of paper, the Little Elephant ought to delete exactly one any digit from number a in the binary record. At that a new number appears. It consists of th... | The single line contains integer a, written in the binary notation without leading zeroes. This number contains more than 1 and at most 105 digits. | In the single line print the number that is written without leading zeroes in the binary notation β the answer to the problem. | In the first sample the best strategy is to delete the second digit. That results in number 112 = 310.In the second sample the best strategy is to delete the third or fourth digits β that results in number 110102 = 2610. | Input: 101 | Output: 11 | Easy | 2 | 641 | 147 | 126 | 2 |
1,374 | A | 1374A | A. Required Remainder | 800 | math | You are given three integers \(x, y\) and \(n\). Your task is to find the maximum integer \(k\) such that \(0 \le k \le n\) that \(k \bmod x = y\), where \(\bmod\) is modulo operation. Many programming languages use percent operator % to implement it.In other words, with given \(x, y\) and \(n\) you need to find the ma... | The first line of the input contains one integer \(t\) (\(1 \le t \le 5 \cdot 10^4\)) β the number of test cases. The next \(t\) lines contain test cases.The only line of the test case contains three integers \(x, y\) and \(n\) (\(2 \le x \le 10^9;~ 0 \le y < x;~ y \le n \le 10^9\)).It can be shown that such \(k\) alwa... | For each test case, print the answer β maximum non-negative integer \(k\) such that \(0 \le k \le n\) and \(k \bmod x = y\). It is guaranteed that the answer always exists. | In the first test case of the example, the answer is \(12339 = 7 \cdot 1762 + 5\) (thus, \(12339 \bmod 7 = 5\)). It is obvious that there is no greater integer not exceeding \(12345\) which has the remainder \(5\) modulo \(7\). | Input: 7 7 5 12345 5 0 4 10 5 15 17 8 54321 499999993 9 1000000000 10 5 187 2 0 999999999 | Output: 12339 0 15 54306 999999995 185 999999998 | Beginner | 1 | 513 | 358 | 172 | 13 |
1,197 | D | 1197D | D. Yet Another Subarray Problem | 1,900 | dp; greedy; math | You are given an array \(a_1, a_2, \dots , a_n\) and two integers \(m\) and \(k\).You can choose some subarray \(a_l, a_{l+1}, \dots, a_{r-1}, a_r\). The cost of subarray \(a_l, a_{l+1}, \dots, a_{r-1}, a_r\) is equal to \(\sum\limits_{i=l}^{r} a_i - k \lceil \frac{r - l + 1}{m} \rceil\), where \(\lceil x \rceil\) is t... | The first line contains three integers \(n\), \(m\), and \(k\) (\(1 \le n \le 3 \cdot 10^5, 1 \le m \le 10, 1 \le k \le 10^9\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(-10^9 \le a_i \le 10^9\)). | Print the maximum cost of some subarray of array \(a\). | Input: 7 3 10 2 -4 15 -3 4 8 3 | Output: 7 | Hard | 3 | 996 | 222 | 55 | 11 | |
1,472 | F | 1472F | F. New Year's Puzzle | 2,100 | brute force; dp; graph matchings; greedy; sortings | Every year Santa Claus gives gifts to all children. However, each country has its own traditions, and this process takes place in different ways. For example, in Berland you need to solve the New Year's puzzle.Polycarp got the following problem: given a grid strip of size \(2 \times n\), some cells of it are blocked. Y... | The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. Then \(t\) test cases follow.Each test case is preceded by an empty line.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n \le 10^9\), \(1 \le m \le 2 \cdot 10^5\)) β the length of the strip and... | For each test case, print on a separate line: ""YES"", if it is possible to tile all unblocked squares with the \(2 \times 1\) and \(1 \times 2\) tiles; ""NO"" otherwise. You can output ""YES"" and ""NO"" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive). | The first two test cases are explained in the statement.In the third test case the strip looks like this: It is easy to check that the unblocked squares on it can not be tiled. | Input: 3 5 2 2 2 1 4 3 2 2 1 2 3 6 4 2 1 2 3 2 4 2 6 | Output: YES NO NO | Hard | 5 | 844 | 697 | 297 | 14 |
2,009 | E | 2009E | E. Klee's SUPER DUPER LARGE Array!!! | 1,400 | binary search; math; ternary search | Klee has an array \(a\) of length \(n\) containing integers \([k, k+1, ..., k+n-1]\) in that order. Klee wants to choose an index \(i\) (\(1 \leq i \leq n\)) such that \(x = |a_1 + a_2 + \dots + a_i - a_{i+1} - \dots - a_n|\) is minimized. Note that for an arbitrary integer \(z\), \(|z|\) represents the absolute value ... | The first line contains \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.Each test case contains two integers \(n\) and \(k\) (\(2 \leq n, k \leq 10^9\)) β the length of the array and the starting element of the array. | For each test case, output the minimum value of \(x\) on a new line. | In the first sample, \(a = [2, 3]\). When \(i = 1\) is chosen, \(x = |2-3| = 1\). It can be shown this is the minimum possible value of \(x\).In the third sample, \(a = [3, 4, 5, 6, 7]\). When \(i = 3\) is chosen, \(x = |3 + 4 + 5 - 6 - 7| = 1\). It can be shown this is the minimum possible value of \(x\). | Input: 42 27 25 31000000000 1000000000 | Output: 1 5 1 347369930 | Easy | 3 | 373 | 227 | 68 | 20 |
840 | A | 840A | A. Leha and Function | 1,300 | combinatorics; greedy; math; number theory; sortings | Leha like all kinds of strange things. Recently he liked the function F(n, k). Consider all possible k-element subsets of the set [1, 2, ..., n]. For subset find minimal element in it. F(n, k) β mathematical expectation of the minimal element among all k-element subsets.But only function does not interest him. He wants... | First line of input data contains single integer m (1 β€ m β€ 2Β·105) β length of arrays A and B.Next line contains m integers a1, a2, ..., am (1 β€ ai β€ 109) β array A.Next line contains m integers b1, b2, ..., bm (1 β€ bi β€ 109) β array B. | Output m integers a'1, a'2, ..., a'm β array A' which is permutation of the array A. | Input: 57 3 5 3 42 1 3 2 3 | Output: 4 7 3 5 3 | Easy | 5 | 608 | 236 | 84 | 8 | |
120 | E | 120E | E. Put Knight! | 1,400 | games; math | Petya and Gena play a very interesting game ""Put a Knight!"" on a chessboard n Γ n in size. In this game they take turns to put chess pieces called ""knights"" on the board so that no two knights could threat each other. A knight located in square (r, c) can threat squares (r - 1, c + 2), (r - 1, c - 2), (r + 1, c + 2... | The first line contains integer T (1 β€ T β€ 100) β the number of boards, for which you should determine the winning player. Next T lines contain T integers ni (1 β€ ni β€ 10000) β the sizes of the chessboards. | For each ni Γ ni board print on a single line ""0"" if Petya wins considering both players play optimally well. Otherwise, print ""1"". | Input: 221 | Output: 10 | Easy | 2 | 622 | 206 | 135 | 1 | |
1,567 | B | 1567B | B. MEXor Mixup | 1,000 | bitmasks; greedy | Alice gave Bob two integers \(a\) and \(b\) (\(a > 0\) and \(b \ge 0\)). Being a curious boy, Bob wrote down an array of non-negative integers with \(\operatorname{MEX}\) value of all elements equal to \(a\) and \(\operatorname{XOR}\) value of all elements equal to \(b\).What is the shortest possible length of the arra... | The input consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 5 \cdot 10^4\)) β the number of test cases. The description of the test cases follows.The only line of each test case contains two integers \(a\) and \(b\) (\(1 \leq a \leq 3 \cdot 10^5\); \(0 \leq b \leq 3 \cdot 10^5\)... | For each test case, output one (positive) integer β the length of the shortest array with \(\operatorname{MEX}\) \(a\) and \(\operatorname{XOR}\) \(b\). We can show that such an array always exists. | In the first test case, one of the shortest arrays with \(\operatorname{MEX}\) \(1\) and \(\operatorname{XOR}\) \(1\) is \([0, 2020, 2021]\).In the second test case, one of the shortest arrays with \(\operatorname{MEX}\) \(2\) and \(\operatorname{XOR}\) \(1\) is \([0, 1]\).It can be shown that these arrays are the shor... | Input: 5 1 1 2 1 2 0 1 10000 2 10000 | Output: 3 2 3 2 3 | Beginner | 2 | 567 | 405 | 198 | 15 |
130 | D | 130D | D. Exponentiation | 1,500 | *special | You are given integers a, b and c. Calculate ab modulo c. | Input data contains numbers a, b and c, one number per line. Each number is an integer between 1 and 100, inclusive. | Output ab mod c. | Input: 2540 | Output: 32 | Medium | 1 | 57 | 116 | 16 | 1 | |
630 | R | 630R | R. Game | 1,200 | games; math | There is a legend in the IT City college. A student that failed to answer all questions on the game theory exam is given one more chance by his professor. The student has to play a game with the professor.The game is played on a square field consisting of n Γ n cells. Initially all cells are empty. On each turn a playe... | The only line of the input contains one integer n (1 β€ n β€ 1018) β the size of the field. | Output number 1, if the player making the first turn wins when both players play optimally, otherwise print number 2. | Input: 1 | Output: 1 | Easy | 2 | 787 | 89 | 117 | 6 | |
289 | B | 289B | B. Polo the Penguin and Matrix | 1,400 | brute force; dp; implementation; sortings; ternary search | Little penguin Polo has an n Γ m matrix, consisting of integers. Let's index the matrix rows from 1 to n from top to bottom and let's index the columns from 1 to m from left to right. Let's represent the matrix element on the intersection of row i and column j as aij.In one move the penguin can add or subtract number d... | The first line contains three integers n, m and d (1 β€ n, m β€ 100, 1 β€ d β€ 104) β the matrix sizes and the d parameter. Next n lines contain the matrix: the j-th integer in the i-th row is the matrix element aij (1 β€ aij β€ 104). | In a single line print a single integer β the minimum number of moves the penguin needs to make all matrix elements equal. If that is impossible, print ""-1"" (without the quotes). | Input: 2 2 22 46 8 | Output: 4 | Easy | 5 | 479 | 228 | 180 | 2 | |
1,422 | E | 1422E | E. Minlexes | 2,700 | dp; greedy; implementation; strings | Some time ago Lesha found an entertaining string \(s\) consisting of lowercase English letters. Lesha immediately developed an unique algorithm for this string and shared it with you. The algorithm is as follows.Lesha chooses an arbitrary (possibly zero) number of pairs on positions \((i, i + 1)\) in such a way that th... | The only line contains the string \(s\) (\(1 \le |s| \le 10^5\)) β the initial string consisting of lowercase English letters only. | In \(|s|\) lines print the lengths of the answers and the answers themselves, starting with the answer for the longest suffix. The output can be large, so, when some answer is longer than \(10\) characters, instead print the first \(5\) characters, then ""..."", then the last \(2\) characters of the answer. | Consider the first example. The longest suffix is the whole string ""abcdd"". Choosing one pair \((4, 5)\), Lesha obtains ""abc"". The next longest suffix is ""bcdd"". Choosing one pair \((3, 4)\), we obtain ""bc"". The next longest suffix is ""cdd"". Choosing one pair \((2, 3)\), we obtain ""c"". The next longest suff... | Input: abcdd | Output: 3 abc 2 bc 1 c 0 1 d | Master | 4 | 800 | 131 | 308 | 14 |
1,282 | C | 1282C | C. Petya and Exam | 1,800 | greedy; sortings; two pointers | Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes.The exam consists of \(n\) problems that can be solved in \(T\) minutes. Thus, the exam begins at time \(0\) and ends at time \(T\). Petya can leave the exam at any integ... | The first line contains the integer \(m\) (\(1 \le m \le 10^4\)) β the number of test cases in the test.The next lines contain a description of \(m\) test cases. The first line of each test case contains four integers \(n, T, a, b\) (\(2 \le n \le 2\cdot10^5\), \(1 \le T \le 10^9\), \(1 \le a < b \le 10^9\)) β the numb... | Print the answers to \(m\) test cases. For each set, print a single integer β maximal number of points that he can receive, before leaving the exam. | Input: 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 | Output: 3 2 1 0 1 4 0 1 2 1 | Medium | 3 | 2,449 | 949 | 148 | 12 | |
1,464 | F | 1464F | F. My Beautiful Madness | 3,500 | data structures; trees | You are given a tree. We will consider simple paths on it. Let's denote path between vertices \(a\) and \(b\) as \((a, b)\). Let \(d\)-neighborhood of a path be a set of vertices of the tree located at a distance \(\leq d\) from at least one vertex of the path (for example, \(0\)-neighborhood of a path is a path itself... | The first line contains two integers \(n\) and \(q\) β the number of vertices in the tree and the number of queries, accordingly (\(1 \leq n \leq 2 \cdot 10^5\), \(2 \leq q \leq 2 \cdot 10^5\)).Each of the following \(n - 1\) lines contains two integers \(x_i\) and \(y_i\) β indices of vertices connected by \(i\)-th ed... | For each query of the third type output answer on a new line. | Input: 1 4 1 1 1 1 1 1 2 1 1 3 0 | Output: Yes | Master | 2 | 897 | 634 | 61 | 14 | |
369 | D | 369D | D. Valera and Fools | 2,200 | dfs and similar; dp; graphs; shortest paths | One fine morning, n fools lined up in a row. After that, they numbered each other with numbers from 1 to n, inclusive. Each fool got a unique number. The fools decided not to change their numbers before the end of the fun.Every fool has exactly k bullets and a pistol. In addition, the fool number i has probability of p... | The first line contains two integers n, k (1 β€ n, k β€ 3000) β the initial number of fools and the number of bullets for each fool.The second line contains n integers p1, p2, ..., pn (0 β€ pi β€ 100) β the given probabilities (in percent). | Print a single number β the answer to the problem. | In the first sample, any situation is possible, except for situation {1, 2}.In the second sample there is exactly one fool, so he does not make shots.In the third sample the possible situations are {1, 2} (after zero rounds) and the ""empty"" situation {} (after one round).In the fourth sample, the only possible situat... | Input: 3 350 50 50 | Output: 7 | Hard | 4 | 1,080 | 236 | 50 | 3 |
1,793 | B | 1793B | B. Fedya and Array | 1,100 | constructive algorithms; math | For his birthday recently Fedya was given an array \(a\) of \(n\) integers arranged in a circle, For each pair of neighboring numbers (\(a_1\) and \(a_2\), \(a_2\) and \(a_3\), \(\ldots\), \(a_{n - 1}\) and \(a_n\), \(a_n\) and \(a_1\)) the absolute difference between them is equal to \(1\).Let's call a local maximum a... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)). Description of the test cases follows.Each line of each test case contain two integers \(x\) and \(y\) (\(-10^{9} \le y < x \le 10^{9}\)) β the sum of local maximums and the sum of local minimums, resp... | For each test case, in the first line print one integer \(n\) β the minimum length of matching arrays.In the second line print \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(-10^{9} \leqslant a_i \leqslant 10^{9}\)) β the array elements such that the the absolute difference between each pair of neighboring is equal to \(1... | In the first test case, the local maximums are the numbers at \(3, 7\) and \(10\) positions, and the local minimums are the numbers at \(1, 6\) and \(8\) positions. \(x = a_3 + a_7 + a_{10} = 2 + 0 + 1 = 3\), \(y = a_1 + a_6 + a_8 = 0 + (-1) + (-1) = -2\).In the second test case, the local maximums are the numbers at \... | Input: 43 -24 -42 -15 -3 | Output: 10 0 1 2 1 0 -1 0 -1 0 1 16 -2 -1 -2 -1 0 1 2 3 4 5 4 3 2 1 0 -1 6 1 0 -1 0 1 0 16 2 3 2 1 0 -1 0 -1 0 -1 0 1 2 1 0 1 | Easy | 2 | 747 | 329 | 468 | 17 |
2,045 | G | 2045G | G. X Aura | 2,200 | graphs; math; shortest paths | Mount ICPC can be represented as a grid of \(R\) rows (numbered from \(1\) to \(R\)) and \(C\) columns (numbered from \(1\) to \(C\)). The cell located at row \(r\) and column \(c\) is denoted as \((r, c)\) and has a height of \(H_{r, c}\). Two cells are adjacent to each other if they share a side. Formally, \((r, c)\)... | The first line consists of three integers \(R\) \(C\) \(X\) (\(1 \leq R, C \leq 1000; 1 \leq X \leq 9; X\) is an odd integer).Each of the next \(R\) lines consists of a string \(H_r\) of length \(C\). Each character in \(H_r\) is a number from 0 to 9. The \(c\)-th character of \(H_r\) represents the height of cell \((r... | For each scenario, output the following in a single line. If the scenario is invalid, output INVALID. Otherwise, output a single integer representing the minimum total penalty to move from the starting cell to the destination cell. | Explanation for the sample input/output #1For the first scenario, one of the solutions is to move as follows: \((1, 1) \rightarrow (2, 1) \rightarrow (3, 1) \rightarrow (3, 2) \rightarrow (3, 3) \rightarrow (3, 4)\). The total penalty of this solution is \((3 - 4)^1 + (4 - 3)^1 + (3 - 6)^1 + (6 - 8)^1 + (8 - 1)^1 = 2\)... | Input: 3 4 1 3359 4294 3681 5 1 1 3 4 3 3 2 1 2 2 1 4 1 3 3 2 1 1 1 1 | Output: 2 4 -7 -1 0 | Hard | 3 | 1,113 | 556 | 231 | 20 |
505 | E | 505E | E. Mr. Kitayuta vs. Bamboos | 2,900 | binary search; greedy | Mr. Kitayuta's garden is planted with n bamboos. (Bamboos are tall, fast-growing tropical plants with hollow stems.) At the moment, the height of the i-th bamboo is hi meters, and it grows ai meters at the end of each day. Actually, Mr. Kitayuta hates these bamboos. He once attempted to cut them down, but failed becaus... | The first line of the input contains four space-separated integers n, m, k and p (1 β€ n β€ 105, 1 β€ m β€ 5000, 1 β€ k β€ 10, 1 β€ p β€ 109). They represent the number of the bamboos in Mr. Kitayuta's garden, the duration of Mr. Kitayuta's fight in days, the maximum number of times that Mr. Kitayuta beat the bamboos during ea... | Print the lowest possible height of the tallest bamboo after m days. | Input: 3 1 2 510 1010 1015 2 | Output: 17 | Master | 2 | 1,231 | 622 | 68 | 5 | |
386 | D | 386D | D. Game with Points | 2,100 | dp; graphs; implementation; shortest paths | You are playing the following game. There are n points on a plane. They are the vertices of a regular n-polygon. Points are labeled with integer numbers from 1 to n. Each pair of distinct points is connected by a diagonal, which is colored in one of 26 colors. Points are denoted by lowercase English letters. There are ... | In the first line there is one integer n (3 β€ n β€ 70) β the number of points. In the second line there are three space-separated integer from 1 to n β numbers of vertices, where stones are initially located.Each of the following n lines contains n symbols β the matrix denoting the colors of the diagonals. Colors are de... | If there is no way to put stones on vertices 1, 2 and 3, print -1 on a single line. Otherwise, on the first line print minimal required number of moves and in the next lines print the description of each move, one move per line. To describe a move print two integers. The point from which to remove the stone, and the po... | In the first example we can move stone from point 4 to point 1 because this points are connected by the diagonal of color 'a' and the diagonal connection point 2 and 3, where the other stones are located, are connected by the diagonal of the same color. After that stones will be on the points 1, 2 and 3. | Input: 42 3 4*abaa*abba*babb* | Output: 14 1 | Hard | 4 | 799 | 618 | 407 | 3 |
746 | C | 746C | C. Tram | 1,600 | constructive algorithms; implementation; math | The tram in Berland goes along a straight line from the point 0 to the point s and back, passing 1 meter per t1 seconds in both directions. It means that the tram is always in the state of uniform rectilinear motion, instantly turning around at points x = 0 and x = s.Igor is at the point x1. He should reach the point x... | The first line contains three integers s, x1 and x2 (2 β€ s β€ 1000, 0 β€ x1, x2 β€ s, x1 β x2) β the maximum coordinate of the point to which the tram goes, the point Igor is at, and the point he should come to.The second line contains two integers t1 and t2 (1 β€ t1, t2 β€ 1000) β the time in seconds in which the tram pass... | Print the minimum time in seconds which Igor needs to get from the point x1 to the point x2. | In the first example it is profitable for Igor to go by foot and not to wait the tram. Thus, he has to pass 2 meters and it takes 8 seconds in total, because he passes 1 meter per 4 seconds. In the second example Igor can, for example, go towards the point x2 and get to the point 1 in 6 seconds (because he has to pass ... | Input: 4 2 43 41 1 | Output: 8 | Medium | 3 | 942 | 719 | 92 | 7 |
2,001 | E1 | 2001E1 | E1. Deterministic Heap (Easy Version) | 2,400 | combinatorics; dp; math; trees | This is the easy version of the problem. The difference between the two versions is the definition of deterministic max-heap, time limit, and constraints on \(n\) and \(t\). You can make hacks only if both versions of the problem are solved.Consider a perfect binary tree with size \(2^n - 1\), with nodes numbered from ... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 500\)). The description of the test cases follows.The first line of each test case contains three integers \(n, k, p\) (\(1 \le n, k \le 500\), \(10^8 \le p \le 10^9\), \(p\) is a prime).It is guaranteed that t... | For each test case, output a single line containing an integer: the number of different deterministic max-heaps produced by applying the aforementioned operation \(\mathrm{add}\) exactly \(k\) times, modulo \(p\). | For the first testcase, there is only one way to generate \(a\), and such sequence is a deterministic max-heap, so the answer is \(1\).For the second testcase, if we choose \(v = 1\) and do the operation, we would have \(a = [1, 0, 0]\), and since \(a_2 = a_3\), we can choose either of them when doing the first \(\math... | Input: 71 13 9982443532 1 9982443533 2 9982448533 3 9982443533 4 1000000374 2 1000000394 3 100000037 | Output: 1 2 12 52 124 32 304 | Expert | 4 | 2,053 | 401 | 213 | 20 |
1,857 | D | 1857D | D. Strong Vertices | 1,300 | math; sortings; trees | Given two arrays \(a\) and \(b\), both of length \(n\). Elements of both arrays indexed from \(1\) to \(n\). You are constructing a directed graph, where edge from \(u\) to \(v\) (\(u\neq v\)) exists if \(a_u-a_v \ge b_u-b_v\).A vertex \(V\) is called strong if there exists a path from \(V\) to all other vertices.A pat... | The first line contains an integer \(t\) (\(1\le t\le 10^4\)) β the number of test cases.The first line of each test case contains an integer \(n\) (\(2 \le n \le 2\cdot 10^5\)) β the length of \(a\) and \(b\).The second line of each test case contains \(n\) integers \(a_1,a_2 \dots a_n\) (\(-10^9 \le a_i \le 10^9\)) β... | For each test case, output two lines: in the first line, output the number of strong vertices, and in the second line, output all strong vertices in ascending order. | The first sample is covered in the problem statement.For the second sample, the graph looks like this: The graph has two strong vertices with numbers \(3\) and \(5\). Note that there is a bidirectional edge between vertices \(3\) and \(5\). In the third sample, the vertices are connected by a single directed edge from ... | Input: 543 1 2 44 3 2 151 2 4 1 25 2 3 3 121 22 130 2 11 3 235 7 4-2 -3 -6 | Output: 1 4 2 3 5 1 2 3 1 2 3 2 2 3 | Easy | 3 | 680 | 553 | 165 | 18 |
44 | E | 44E | E. Anfisa the Monkey | 1,400 | dp | Anfisa the monkey learns to type. She is yet unfamiliar with the ""space"" key and can only type in lower-case Latin letters. Having typed for a fairly long line, Anfisa understood that it would be great to divide what she has written into k lines not shorter than a and not longer than b, for the text to resemble human... | The first line contains three integers k, a and b (1 β€ k β€ 200, 1 β€ a β€ b β€ 200). The second line contains a sequence of lowercase Latin letters β the text typed by Anfisa. It is guaranteed that the given line is not empty and its length does not exceed 200 symbols. | Print k lines, each of which contains no less than a and no more than b symbols β Anfisa's text divided into lines. It is not allowed to perform any changes in the text, such as: deleting or adding symbols, changing their order, etc. If the solution is not unique, print any of them. If there is no solution, print ""No ... | Input: 3 2 5abrakadabra | Output: abrakadabra | Easy | 1 | 346 | 266 | 348 | 0 | |
2,071 | E | 2071E | E. LeaFall | 2,600 | combinatorics; dp; probabilities; trees | You are given a tree\(^{\text{β}}\) with \(n\) vertices. Over time, each vertex \(i\) (\(1 \le i \le n\)) has a probability of \(\frac{p_i}{q_i}\) of falling. Determine the expected value of the number of unordered pairs\(^{\text{β }}\) of distinct vertices that become leaves\(^{\text{β‘}}\) in the resulting forest\(^{\t... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)).The \(i\)-th line of the following \(n\) lines contains two integers \... | For each test case, output a single integer β the expected value of the number of unordered pairs of distinct vertices that become leaves in the resulting forest modulo \(998\,244\,353\).Formally, let \(M = 998\,244\,353\). It can be shown that the exact answer can be expressed as an irreducible fraction \(\frac{p}{q}\... | In the first test case, only one vertex is in the tree, which is not a leaf, so the answer is \(0\).In the second test case, the tree is shown below. Vertices that have not fallen are denoted in bold. Let us examine the following three cases: We arrive at this configuration with a probability of \(\left( \frac{1}{2} \r... | Input: 511 231 21 21 21 22 331 31 51 31 22 31998244351 998244352610 177 136 112 1010 195 134 33 61 43 53 2 | Output: 0 623902721 244015287 0 799215919 | Expert | 4 | 896 | 675 | 550 | 20 |
1,700 | F | 1700F | F. Puzzle | 2,600 | constructive algorithms; dp; greedy | Pupils Alice and Ibragim are best friends. It's Ibragim's birthday soon, so Alice decided to gift him a new puzzle. The puzzle can be represented as a matrix with \(2\) rows and \(n\) columns, every element of which is either \(0\) or \(1\). In one move you can swap two values in neighboring cells.More formally, let's ... | The first line contains an integer \(n\) (\(1 \leq n \leq 200\,000\)) β the number of columns in the puzzle.Following two lines describe the current arrangement on the puzzle. Each line contains \(n\) integers, every one of which is either \(0\) or \(1\).The last two lines describe Alice's desired arrangement in the sa... | If it is possible to get the desired arrangement, print the minimal possible number of steps, otherwise print \(-1\). | In the first example the following sequence of swaps will suffice: \((2, 1), (1, 1)\), \((1, 2), (1, 3)\), \((2, 2), (2, 3)\), \((1, 4), (1, 5)\), \((2, 5), (2, 4)\). It can be shown that \(5\) is the minimal possible answer in this case.In the second example no matter what swaps you do, you won't get the desired arran... | Input: 5 0 1 0 1 0 1 1 0 0 1 1 0 1 0 1 0 0 1 1 0 | Output: 5 | Expert | 3 | 984 | 330 | 117 | 17 |
2,120 | C | 2120C | C. Divine Tree | 1,400 | constructive algorithms; greedy; math; sortings; trees | Harshith attained enlightenment in Competitive Programming by training under a Divine Tree. A divine tree is a rooted tree\(^{\text{β}}\) with \(n\) nodes, labelled from \(1\) to \(n\). The divineness of a node \(v\), denoted \(d(v)\), is defined as the smallest node label on the unique simple path from the root to nod... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n \le 10^6\), \(1 \le m \le 10^{12}\)).It is guaranteed that the sum of \(n\) ... | For each test case, output a single integer \(k\) in one line β the root of the tree.Then \(n-1\) lines follow, each containing a description of an edge of the tree β a pair of positive integers \(u_i,v_i\) (\(1\le u_i,v_i\le n\), \(u_i\ne v_i\)), denoting the \(i\)-th edge connects vertices \(u_i\) and \(v_i\).The edg... | In the first test case, there is a single node with a value of \(1\), so getting a sum of \(2\) is impossible.In the second test case, getting a sum of \(6\) is possible with the given tree rooted at \(3\). | Input: 21 24 6 | Output: -1 3 3 1 1 2 2 4 | Easy | 5 | 995 | 365 | 475 | 21 |
624 | A | 624A | A. Save Luke | 800 | math | Luke Skywalker got locked up in a rubbish shredder between two presses. R2D2 is already working on his rescue, but Luke needs to stay alive as long as possible. For simplicity we will assume that everything happens on a straight line, the presses are initially at coordinates 0 and L, and they move towards each other wi... | The first line of the input contains four integers d, L, v1, v2 (1 β€ d, L, v1, v2 β€ 10 000, d < L) β Luke's width, the initial position of the second press and the speed of the first and second presses, respectively. | Print a single real value β the maximum period of time Luke can stay alive for. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if . | In the first sample Luke should stay exactly in the middle of the segment, that is at coordinates [2;4], as the presses move with the same speed.In the second sample he needs to occupy the position . In this case both presses move to his edges at the same time. | Input: 2 6 2 2 | Output: 1.00000000000000000000 | Beginner | 1 | 564 | 216 | 313 | 6 |
1,805 | C | 1805C | C. Place for a Selfie | 1,400 | binary search; data structures; geometry; math | The universe is a coordinate plane. There are \(n\) space highways, each of which is a straight line \(y=kx\) passing through the origin \((0, 0)\). Also, there are \(m\) asteroid belts on the plane, which we represent as open upwards parabolas, i. e. graphs of functions \(y=ax^2+bx+c\), where \(a > 0\).You want to pho... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The first line of each test case contains \(2\) integers \(n\) and \(m\) (\(1 \le n, m \le 10^5\)) βthe number of lines and parabolas, respectively.Each of the... | For each test case, output the answers for each parabola in the given order. If there is a line that does not intersect the given parabola and doesn't touch it, print on a separate line the word ""YES"", and then on a separate line the number \(k\) β the coefficient of this line. If there are several answers, print any... | In the first test case, both parabolas do not intersect the only given line \(y=1\cdot x\), so the answer is two numbers \(1\). In the second test case, the line \(y=x\) and the parabola \(2x^2+5x+1\) intersect, and also the line \(y=4x\) and the parabola \(x^2+2x+1\) touch, so these pairs do not satisfy the condition.... | Input: 51 211 -1 21 -1 32 2141 2 12 5 11 101 0 01 1100000000100000000 100000000 1000000002 3022 2 11 -2 11 -2 -1 | Output: YES 1 YES 1 YES 1 YES 4 NO YES 100000000 YES 0 NO NO | Easy | 4 | 597 | 883 | 664 | 18 |
130 | C | 130C | C. Decimal sum | 1,500 | *special | You are given an array of integer numbers. Calculate the sum of its elements. | The first line of the input contains an integer n (1 β€ n β€ 100) β the size of the array. Next n lines contain the elements of the array, one per line. Each element is an integer between 1 and 100, inclusive. | Output the sum of the elements of the array. | Input: 512345 | Output: 15 | Medium | 1 | 77 | 207 | 44 | 1 | |
389 | A | 389A | A. Fox and Number Game | 1,000 | greedy; math | Fox Ciel is playing a game with numbers now. Ciel has n positive integers: x1, x2, ..., xn. She can do the following operation as many times as needed: select two different indexes i and j such that xi > xj hold, and then apply assignment xi = xi - xj. The goal is to make the sum of all numbers as small as possible.Ple... | The first line contains an integer n (2 β€ n β€ 100). Then the second line contains n integers: x1, x2, ..., xn (1 β€ xi β€ 100). | Output a single integer β the required minimal sum. | In the first example the optimal way is to do the assignment: x2 = x2 - x1.In the second example the optimal sequence of operations is: x3 = x3 - x2, x2 = x2 - x1. | Input: 21 2 | Output: 2 | Beginner | 2 | 359 | 125 | 51 | 3 |
1,512 | F | 1512F | F. Education | 1,900 | brute force; dp; greedy; implementation | Polycarp is wondering about buying a new computer, which costs \(c\) tugriks. To do this, he wants to get a job as a programmer in a big company.There are \(n\) positions in Polycarp's company, numbered starting from one. An employee in position \(i\) earns \(a[i]\) tugriks every day. The higher the position number, th... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)). Then \(t\) test cases follow.The first line of each test case contains two integers \(n\) and \(c\) (\(2 \le n \le 2 \cdot 10^5\), \(1 \le c \le 10^9\)) β the number of positions in the company and the cost of a new computer.The second line of each ... | For each test case, output the minimum number of days after which Polycarp will be able to buy a new computer. | Input: 3 4 15 1 3 10 11 1 2 7 4 100 1 5 10 50 3 14 12 2 1000000000 1 1 1 | Output: 6 13 1000000000 | Hard | 4 | 1,664 | 621 | 110 | 15 | |
1,914 | E2 | 1914E2 | E2. Game with Marbles (Hard Version) | 1,400 | games; greedy; sortings | The easy and hard versions of this problem differ only in the constraints on the number of test cases and \(n\). In the hard version, the number of test cases does not exceed \(10^4\), and the sum of values of \(n\) over all test cases does not exceed \(2 \cdot 10^5\). Furthermore, there are no additional constraints o... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Each test case consists of three lines: the first line contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the number of colors; the second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le... | For each test case, output a single integer β the score at the end of the game if both Alice and Bob act optimally. | In the first example, one way to achieve a score of \(1\) is as follows: Alice chooses color \(1\), discards \(1\) marble. Bob also discards \(1\) marble; Bob chooses color \(3\), discards \(1\) marble. Alice also discards \(1\) marble; Alice chooses color \(2\), discards \(1\) marble, and Bob discards \(2\) marble. As... | Input: 534 2 11 2 441 20 1 20100 15 10 2051000000000 1000000000 1000000000 1000000000 10000000001 1 1 1 135 6 52 1 763 2 4 2 5 59 4 7 9 2 5 | Output: 1 -9 2999999997 8 -6 | Easy | 3 | 1,645 | 677 | 115 | 19 |
1,603 | F | 1603F | F. October 18, 2017 | 2,700 | combinatorics; dp; implementation; math | It was October 18, 2017. Shohag, a melancholic soul, made a strong determination that he will pursue Competitive Programming seriously, by heart, because he found it fascinating. Fast forward to 4 years, he is happy that he took this road. He is now creating a contest on Codeforces. He found an astounding problem but h... | The first line contains a single integer \(t\) (\(1 \le t \le 10^5\)) β the number of test cases.The first and only line of each test case contains three space-separated integers \(n\), \(k\), and \(x\) (\(1 \le n \le 10^9\), \(0 \le k \le 10^7\), \(0 \le x \lt 2^{\operatorname{min}(20, k)}\)).It is guaranteed that the... | For each test case, print a single integer β the answer to the problem. | In the first test case, the valid sequences are \([1, 2]\), \([1, 3]\), \([2, 1]\), \([2, 3]\), \([3, 1]\) and \([3, 2]\).In the second test case, the only valid sequence is \([0, 0]\). | Input: 6 2 2 0 2 1 1 3 2 3 69 69 69 2017 10 18 5 7 0 | Output: 6 1 15 699496932 892852568 713939942 | Master | 4 | 918 | 387 | 71 | 16 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.