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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
151 | A | 151A | A. Soft Drinking | 800 | implementation; math | This winter is so cold in Nvodsk! A group of n friends decided to buy k bottles of a soft drink called ""Take-It-Light"" to warm up a bit. Each bottle has l milliliters of the drink. Also they bought c limes and cut each of them into d slices. After that they found p grams of salt.To make a toast, each friend needs nl ... | The first and only line contains positive integers n, k, l, c, d, p, nl, np, not exceeding 1000 and no less than 1. The numbers are separated by exactly one space. | Print a single integer — the number of toasts each friend can make. | A comment to the first sample: Overall the friends have 4 * 5 = 20 milliliters of the drink, it is enough to make 20 / 3 = 6 toasts. The limes are enough for 10 * 8 = 80 toasts and the salt is enough for 100 / 1 = 100 toasts. However, there are 3 friends in the group, so the answer is min(6, 80, 100) / 3 = 2. | Input: 3 4 5 10 8 100 3 1 | Output: 2 | Beginner | 2 | 515 | 163 | 67 | 1 |
671 | C | 671C | C. Ultimate Weirdness of an Array | 2,800 | data structures; number theory | Yasin has an array a containing n integers. Yasin is a 5 year old, so he loves ultimate weird things.Yasin denotes weirdness of an array as maximum gcd(ai, aj) value among all 1 ≤ i < j ≤ n. For n ≤ 1 weirdness is equal to 0, gcd(x, y) is the greatest common divisor of integers x and y.He also defines the ultimate weir... | The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of elements in a.The next line contains n integers ai (1 ≤ ai ≤ 200 000), where the i-th number is equal to the i-th element of the array a. It is guaranteed that all ai are distinct. | Print a single line containing the value of ultimate weirdness of the array a. | Consider the first sample. f(1, 1) is equal to 3. f(2, 2) is equal to 1. f(3, 3) is equal to 2. f(1, 2), f(1, 3) and f(2, 3) are equal to 0. Thus the answer is 3 + 0 + 0 + 1 + 0 + 2 = 6. | Input: 32 6 3 | Output: 6 | Master | 2 | 633 | 270 | 78 | 6 |
1,468 | A | 1468A | A. LaIS | 2,200 | data structures; dp; greedy | Let's call a sequence \(b_1, b_2, b_3 \dots, b_{k - 1}, b_k\) almost increasing if $$$\(\min(b_1, b_2) \le \min(b_2, b_3) \le \dots \le \min(b_{k - 1}, b_k).\)\( In particular, any sequence with no more than two elements is almost increasing.You are given a sequence of integers \)a_1, a_2, \dots, a_n\(. Calculate the l... | The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) — the number of independent test cases.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 5 \cdot 10^5\)) — the length of the sequence \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n... | For each test case, print one integer — the length of the longest almost increasing subsequence. | In the first test case, one of the optimal answers is subsequence \(1, 2, 7, 2, 2, 3\).In the second and third test cases, the whole sequence \(a\) is already almost increasing. | Input: 381 2 7 3 2 1 2 322 174 1 5 2 6 3 7 | Output: 6 2 7 | Hard | 3 | 600 | 463 | 96 | 14 |
2,022 | C | 2022C | C. Gerrymandering | 1,800 | dp; implementation | We all steal a little bit. But I have only one hand, while my adversaries have two.Álvaro ObregónÁlvaro and José are the only candidates running for the presidency of Tepito, a rectangular grid of \(2\) rows and \(n\) columns, where each cell represents a house. It is guaranteed that \(n\) is a multiple of \(3\).Under ... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The first line of each test case contains one integer \(n\) (\(3 \le n \le 10^5\); \(n\) is a multiple of \(3\)) — the number of columns of Tepito.The followin... | For each test case, output a single integer — the maximum number of districts Álvaro can win by optimally dividing the houses into districts. | The image below showcases the optimal arrangement of districts Álvaro can use for each test case in the example. | Input: 43AAAAJJ6JAJAJJJJAJAJ6AJJJAJAJJAAA9AJJJJAJAJJAAJJJJJA | Output: 2 2 3 2 | Medium | 2 | 1,070 | 727 | 141 | 20 |
100 | J | 100J | J. Interval Coloring | 2,400 | *special; greedy; math | Aryo has got a lot of intervals for his 2418th birthday. He is really excited and decided to color all these intervals with some colors. He has a simple rule for himself. He calls a coloring nice if there exists no three intervals a, b and c such that the following conditions are satisfied simultaneously: a, b and c ar... | The first line contains a single integer n (1 ≤ n ≤ 103), number of intervals.The following n lines contain a interval description each. Each interval is described by two numbers si, ei which are the start and end points of it ( - 105 < si, ei < 105, si ≤ ei). See samples for clarity. A square bracket stands for includ... | Write a single integer k — the minimum number of colors needed for a nice coloring. | Input: 2[1,2)(3,4] | Output: 1 | Expert | 3 | 585 | 398 | 83 | 1 | |
1,923 | C | 1923C | C. Find B | 1,400 | constructive algorithms; greedy | An array \(a\) of length \(m\) is considered good if there exists an integer array \(b\) of length \(m\) such that the following conditions hold: \(\sum\limits_{i=1}^{m} a_i = \sum\limits_{i=1}^{m} b_i\); \(a_i \neq b_i\) for every index \(i\) from \(1\) to \(m\); \(b_i > 0\) for every index \(i\) from \(1\) to \(m\). ... | The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.The first line of each test case contains two integers \(n\) and \(q\) (\(1 \le n, q \le 3 \cdot 10^5\)) — the length of the array \(c\) and the number of queries.The second line of each test case contains \(n\) integers \(c_1, ... | For each query, print YES if the subarray is good. Otherwise, print NO.You can output each letter of the answer in any case (upper or lower). For example, the strings yEs, yes, Yes, and YES will all be recognized as positive responses. | Input: 15 41 2 1 4 51 54 43 41 3 | Output: YES NO YES NO | Easy | 2 | 572 | 698 | 235 | 19 | |
204 | C | 204C | C. Little Elephant and Furik and Rubik | 2,000 | math; probabilities | Little Elephant loves Furik and Rubik, who he met in a small city Kremenchug.The Little Elephant has two strings of equal length a and b, consisting only of uppercase English letters. The Little Elephant selects a pair of substrings of equal length — the first one from string a, the second one from string b. The choice... | The first line contains a single integer n (1 ≤ n ≤ 2·105) — the length of strings a and b. The second line contains string a, the third line contains string b. The strings consist of uppercase English letters only. The length of both strings equals n. | On a single line print a real number — the answer to the problem. The answer will be considered correct if its relative or absolute error does not exceed 10 - 6. | Let's assume that we are given string a = a1a2... a|a|, then let's denote the string's length as |a|, and its i-th character — as ai.A substring a[l... r] (1 ≤ l ≤ r ≤ |a|) of string a is string alal + 1... ar.String a is a substring of string b, if there exists such pair of integers l and r (1 ≤ l ≤ r ≤ |b|), that b[l... | Input: 2ABBA | Output: 0.400000000 | Hard | 2 | 763 | 252 | 161 | 2 |
1,863 | E | 1863E | E. Speedrun | 2,100 | brute force; dfs and similar; dp; graphs; greedy; math; sortings; two pointers | You are playing a video game. The game has \(n\) quests that need to be completed. However, the \(j\)-th quest can only be completed at the beginning of hour \(h_j\) of a game day. The game day is \(k\) hours long. The hours of each game day are numbered \(0, 1, \ldots, k - 1\). After the first day ends, a new one star... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1\le t\le 100\,000\)). The description of the test cases follows.The first line of each test case contains three integers \(n\), \(m\), and \(k\) (\(1\le n\le 200\,000\), \(0\le m\le 200\,000\), \(1\le k\le 10^9\)) — the n... | For each test case, output a single integer — the minimum completion time. | In the first test case, quests \(1\) and \(4\) must be completed at the beginning of the \(12\)-th hour of the day, but they cannot be completed during the same hour, because you also need to complete quests \(2\) and \(3\) between them. You can do all this in \(24\) hours, though. To do so, you start at \(12\) hours o... | Input: 64 4 2412 16 18 121 21 32 43 44 3 102 6 5 91 42 43 42 1 105 51 25 0 10008 800 555 35 355 0 103 2 5 4 73 2 54 3 21 22 3 | Output: 24 7 0 480 5 8 | Hard | 8 | 1,154 | 964 | 74 | 18 |
612 | B | 612B | B. HDD is Outdated Technology | 1,200 | implementation; math | HDD hard drives group data by sectors. All files are split to fragments and each of them are written in some sector of hard drive. Note the fragments can be written in sectors in arbitrary order.One of the problems of HDD hard drives is the following: the magnetic head should move from one sector to another to read som... | The first line contains a positive integer n (1 ≤ n ≤ 2·105) — the number of fragments.The second line contains n different integers fi (1 ≤ fi ≤ n) — the number of the fragment written in the i-th sector. | Print the only integer — the number of time units needed to read the file. | In the second example the head moves in the following way: 1->2 means movement from the sector 1 to the sector 5, i.e. it takes 4 time units 2->3 means movement from the sector 5 to the sector 2, i.e. it takes 3 time units 3->4 means movement from the sector 2 to the sector 4, i.e. it takes 2 time units 4->5 means move... | Input: 33 1 2 | Output: 3 | Easy | 2 | 1,014 | 205 | 74 | 6 |
827 | C | 827C | C. DNA Evolution | 2,100 | data structures; strings | Everyone knows that DNA strands consist of nucleotides. There are four types of nucleotides: ""A"", ""T"", ""G"", ""C"". A DNA strand is a sequence of nucleotides. Scientists decided to track evolution of a rare species, which DNA strand was string s initially. Evolution of the species is described as a sequence of cha... | The first line contains the string s (1 ≤ |s| ≤ 105) that describes the initial DNA strand. It consists only of capital English letters ""A"", ""T"", ""G"" and ""C"".The next line contains single integer q (1 ≤ q ≤ 105) — the number of events.After that, q lines follow, each describes one event. Each of the lines has o... | For each scientists' query (second type query) print a single integer in a new line — the value of impact of the infection on the DNA. | Consider the first example. In the first query of second type all characters coincide, so the answer is 8. In the second query we compare string ""TTTTT..."" and the substring ""TGCAT"". There are two matches. In the third query, after the DNA change, we compare string ""TATAT...""' with substring ""TGTAT"". There are ... | Input: ATGCATGC42 1 8 ATGC2 2 6 TTT1 4 T2 2 6 TA | Output: 824 | Hard | 2 | 1,569 | 795 | 134 | 8 |
1,116 | D6 | 1116D6 | D6. Hessenberg matrix | 0 | *special | Implement a unitary operation on \(N\) qubits which is represented by an upper Hessenberg matrix (a square matrix of size \(2^N\) which has all zero elements below the first subdiagonal and all non-zero elements on the first subdiagonal and above it).For example, for \(N = 2\) the matrix of the operation should have th... | Beginner | 1 | 1,867 | 0 | 0 | 11 | ||||
39 | E | 39E | E. What Has Dirichlet Got to Do with That? | 2,000 | dp; games | You all know the Dirichlet principle, the point of which is that if n boxes have no less than n + 1 items, that leads to the existence of a box in which there are at least two items.Having heard of that principle, but having not mastered the technique of logical thinking, 8 year olds Stas and Masha invented a game. The... | The only input line has three integers a, b, n (1 ≤ a ≤ 10000, 1 ≤ b ≤ 30, 2 ≤ n ≤ 109) — the initial number of the boxes, the number of the items and the number which constrains the number of ways, respectively. Guaranteed that the initial number of ways is strictly less than n. | Output ""Stas"" if Masha wins. Output ""Masha"" if Stas wins. In case of a draw, output ""Missing"". | In the second example the initial number of ways is equal to 3125. If Stas increases the number of boxes, he will lose, as Masha may increase the number of boxes once more during her turn. After that any Stas's move will lead to defeat. But if Stas increases the number of items, then any Masha's move will be losing. | Input: 2 2 10 | Output: Masha | Hard | 2 | 710 | 280 | 100 | 0 |
158 | C | 158C | C. Cd and pwd commands | 1,400 | *special; data structures; implementation | Vasya is writing an operating system shell, and it should have commands for working with directories. To begin with, he decided to go with just two commands: cd (change the current directory) and pwd (display the current directory).Directories in Vasya's operating system form a traditional hierarchical tree structure. ... | The first line of the input data contains the single integer n (1 ≤ n ≤ 50) — the number of commands.Then follow n lines, each contains one command. Each of these lines contains either command pwd, or command cd, followed by a space-separated non-empty parameter.The command parameter cd only contains lower case Latin l... | For each command pwd you should print the full absolute path of the given directory, ending with a slash. It should start with a slash and contain the list of slash-separated directories in the order of being nested from the root to the current folder. It should contain no dots. | Input: 7pwdcd /home/vasyapwdcd ..pwdcd vasya/../petyapwd | Output: //home/vasya//home//home/petya/ | Easy | 3 | 1,694 | 688 | 279 | 1 | |
1,632 | B | 1632B | B. Roof Construction | 1,000 | bitmasks; constructive algorithms | It has finally been decided to build a roof over the football field in School 179. Its construction will require placing \(n\) consecutive vertical pillars. Furthermore, the headmaster wants the heights of all the pillars to form a permutation \(p\) of integers from \(0\) to \(n - 1\), where \(p_i\) is the height of th... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). Description of the test cases follows.The only line for each test case contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) — the number of pillars for the construction of the roof.It is guar... | For each test case print \(n\) integers \(p_1\), \(p_2\), \(\ldots\), \(p_n\) — the sequence of pillar heights with the smallest construction cost.If there are multiple answers, print any of them. | For \(n = 2\) there are \(2\) sequences of pillar heights: \([0, 1]\) — cost of construction is \(0 \oplus 1 = 1\). \([1, 0]\) — cost of construction is \(1 \oplus 0 = 1\). For \(n = 3\) there are \(6\) sequences of pillar heights: \([0, 1, 2]\) — cost of construction is \(\max(0 \oplus 1, 1 \oplus 2) = \max(1, 3) = 3\... | Input: 423510 | Output: 0 1 2 0 1 3 2 1 0 4 4 6 3 2 0 8 9 1 7 5 | Beginner | 2 | 1,122 | 402 | 196 | 16 |
1,437 | A | 1437A | A. Marketing Scheme | 800 | brute force; constructive algorithms; greedy; math | You got a job as a marketer in a pet shop, and your current task is to boost sales of cat food. One of the strategies is to sell cans of food in packs with discounts. Suppose you decided to sell packs with \(a\) cans in a pack with a discount and some customer wants to buy \(x\) cans of cat food. Then he follows a gree... | The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) — the number of test cases.The first and only line of each test case contains two integers \(l\) and \(r\) (\(1 \le l \le r \le 10^9\)) — the range of the number of cans customers can buy. | For each test case, print YES if you can choose such size of pack \(a\) that each customer buys more cans than they wanted initially. Otherwise, print NO.You can print each character in any case. | In the first test case, you can take, for example, \(a = 5\) as the size of the pack. Then if a customer wants to buy \(3\) cans, he'll buy \(5\) instead (\(3 \bmod 5 = 3\), \(\frac{5}{2} = 2.5\)). The one who wants \(4\) cans will also buy \(5\) cans.In the second test case, there is no way to choose \(a\).In the thir... | Input: 3 3 4 1 2 120 150 | Output: YES NO YES | Beginner | 4 | 1,164 | 258 | 195 | 14 |
1,658 | E | 1658E | E. Gojou and Matrix Game | 2,500 | data structures; dp; games; hashing; implementation; math; number theory; sortings | Marin feels exhausted after a long day of cosplay, so Gojou invites her to play a game!Marin and Gojou take turns to place one of their tokens on an \(n \times n\) grid with Marin starting first. There are some restrictions and allowances on where to place tokens: Apart from the first move, the token placed by a player... | The first line contains two integers \(n\), \(k\) (\(3 \le n \le 2000\), \(1 \leq k \leq n - 2\)). Note that under these constraints it is always possible to make a move.The following \(n\) lines contains \(n\) integers each. The \(j\)-th integer in the \(i\)-th line is \(v_{i,j}\) (\(1 \le v_{i,j} \le n^2\)). All elem... | You should print \(n\) lines. In the \(i\)-th line, print \(n\) characters, where the \(j\)-th character is the result of the game in which Marin places her first token in the cell \((i, j)\). Print 'M' if Marin wins, 'G' if Gojou wins, and 'D' if the game ends in a draw. Do not print spaces between the characters in o... | Input: 3 1 1 2 4 6 8 3 9 5 7 | Output: GGG MGG MGG | Expert | 8 | 1,411 | 347 | 328 | 16 | |
878 | E | 878E | E. Numbers on the blackboard | 3,300 | combinatorics; dp | A sequence of n integers is written on a blackboard. Soon Sasha will come to the blackboard and start the following actions: let x and y be two adjacent numbers (x before y), then he can remove them and write x + 2y instead of them. He will perform these operations until one number is left. Sasha likes big numbers and ... | The first line contains two integers n and q (1 ≤ n, q ≤ 105) — the number of integers on the blackboard and the number of Nikita's options.The next line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the sequence on the blackboard.Each of the next q lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n), d... | For each option output Sasha's result modulo 109 + 7. | In the second sample Nikita doesn't erase anything. Sasha first erases the numbers 1 and 2 and writes 5. Then he erases 5 and -3 and gets -1. -1 modulo 109 + 7 is 109 + 6. | Input: 3 31 2 31 31 22 3 | Output: 1758 | Master | 2 | 804 | 347 | 53 | 8 |
1,948 | G | 1948G | G. MST with Matching | 3,100 | bitmasks; brute force; dsu; graph matchings; trees | You are given an undirected connected graph on \(n\) vertices. Each edge of this graph has a weight; the weight of the edge connecting vertices \(i\) and \(j\) is \(w_{i,j}\) (or \(w_{i,j} = 0\) if there is no edge between \(i\) and \(j\)). All weights are positive integers.You are also given a positive integer \(c\).Y... | The first line contains two integers \(n\) and \(c\) (\(2 \le n \le 20\); \(1 \le c \le 10^6\)).Then \(n\) lines follow. The \(i\)-th of them contains \(n\) integers \(w_{i,1}, w_{i,2}, \dots, w_{i,n}\) (\(0 \le w_{i,j} \le 10^6\)), where \(w_{i,j}\) denotes the weight of the edge between \(i\) and \(j\) (or \(w_{i,j} ... | Print one integer — the minimum cost of a spanning tree of the given graph. | In the first example, the minimum cost spanning tree consists of edges \((1, 3)\), \((2, 3)\) and \((3, 4)\). The maximum matching for it is \(1\).In the second example, the minimum cost spanning tree consists of edges \((1, 2)\), \((2, 3)\) and \((3, 4)\). The maximum matching for it is \(2\). | Input: 4 100 1 8 01 0 1 08 1 0 20 0 2 0 | Output: 21 | Master | 5 | 980 | 573 | 75 | 19 |
995 | F | 995F | F. Cowmpany Cowmpensation | 2,700 | combinatorics; dp; math; trees | Allen, having graduated from the MOO Institute of Techcowlogy (MIT), has started a startup! Allen is the president of his startup. He also hires \(n-1\) other employees, each of which is assigned a direct superior. If \(u\) is a superior of \(v\) and \(v\) is a superior of \(w\) then also \(u\) is a superior of \(w\). ... | The first line of the input contains two integers \(n\) and \(D\) (\(1 \le n \le 3000\), \(1 \le D \le 10^9\)).The remaining \(n-1\) lines each contain a single positive integer, where the \(i\)-th line contains the integer \(p_i\) (\(1 \le p_i \le i\)). \(p_i\) denotes the direct superior of employee \(i+1\). | Output a single integer: the number of ways to assign salaries modulo \(10^9 + 7\). | In the first sample case, employee 2 and 3 report directly to Allen. The three salaries, in order, can be \((1,1,1)\), \((2,1,1)\), \((2,1,2)\), \((2,2,1)\) or \((2,2,2)\).In the second sample case, employee 2 reports to Allen and employee 3 reports to employee 2. In order, the possible salaries are \((1,1,1)\), \((2,1... | Input: 3 211 | Output: 5 | Master | 4 | 916 | 311 | 83 | 9 |
618 | G | 618G | G. Combining Slimes | 3,300 | dp; math; matrices; probabilities | Your friend recently gave you some slimes for your birthday. You have a very large amount of slimes with value 1 and 2, and you decide to invent a game using these slimes.You initialize a row with n empty spaces. You also choose a number p to be used in the game. Then, you will perform the following steps while the las... | The first line of the input will contain two integers n, p (1 ≤ n ≤ 109, 1 ≤ p < 109). | Print the expected sum of all slimes on the board after the game finishes. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 4.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, we have a board with two squares, and there is a 0.5 probability of a 1 appearing and a 0.5 probability of a 2 appearing.Our final board states can be 1 2 with probability 0.25, 2 1 with probability 0.375, 3 2 with probability 0.1875, 3 1 with probability 0.1875. The expected value is thus (1 + 2)·... | Input: 2 500000000 | Output: 3.562500000000000 | Master | 4 | 1,017 | 86 | 306 | 6 |
1,675 | E | 1675E | E. Replace With the Previous, Minimize | 1,500 | dsu; greedy; strings | You are given a string \(s\) of lowercase Latin letters. The following operation can be used: select one character (from 'a' to 'z') that occurs at least once in the string. And replace all such characters in the string with the previous one in alphabetical order on the loop. For example, replace all 'c' with 'b' or re... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) —the number of test cases in the test.This is followed by descriptions of the test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 2 \cdot 10^5\), \(1 \le k \le 10^9\)) — the size of the string \(s\) and th... | For each test case, output the lexicographically minimal string that can be obtained from the string \(s\) by performing no more than \(k\) operations. | Input: 43 2cba4 5fgde7 5gndcafb4 19ekyv | Output: aaa agaa bnbbabb aapp | Medium | 3 | 789 | 595 | 151 | 16 | |
1,304 | A | 1304A | A. Two Rabbits | 800 | math | Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a park. He sat down on a bench, and soon he found two rabbits hopping around. One of the rabbits was taller than the other.He noticed that the two rabbits were hopping towards each other. The positions of the two rabbits ca... | Each test contains one or more test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)).Each test case contains exactly one line. The line consists of four integers \(x\), \(y\), \(a\), \(b\) (\(0 \le x \lt y \le 10^9\), \(1 \le a,b \le 10^9\)) — the current position of the taller rabbi... | For each test case, print the single integer: number of seconds the two rabbits will take to be at the same position.If the two rabbits will never be at the same position simultaneously, print \(-1\). | The first case is explained in the description.In the second case, each rabbit will be at position \(3\) and \(7\) respectively at the \(1\)-st second. But in the \(2\)-nd second they will be at \(6\) and \(4\) respectively, and we can see that they will never be at the same position since the distance between the two ... | Input: 5 0 10 2 3 0 10 3 3 900000000 1000000000 1 9999999 1 2 1 1 1 3 1 1 | Output: 2 -1 10 -1 1 | Beginner | 1 | 1,088 | 471 | 200 | 13 |
1,129 | E | 1129E | E. Legendary Tree | 3,100 | binary search; interactive; trees | This is an interactive problem.A legendary tree rests deep in the forest. Legend has it that individuals who realize this tree would eternally become a Legendary Grandmaster.To help you determine the tree, Mikaela the Goddess has revealed to you that the tree contains \(n\) vertices, enumerated from \(1\) through \(n\)... | The first line contains an integer \(n\) (\(2 \leq n \leq 500\)) — the number of vertices in the tree. | When program has realized the tree and is ready to report the edges, print ""ANSWER"" in a separate line. Make sure that all letters are capitalized.Then, print \(n-1\) lines, each containing two space-separated integers, denoting the vertices that are the endpoints of a certain edge. Each edge should be reported exact... | In the sample, the tree is as follows. \(n = 5\) is given to the program. The program then asks Mikaela a question where \(S = \{1, 2, 3\}\), \(T = \{4, 5\}\), and \(v = 2\), to which she replies with \(5\) (the pairs \((s, t)\) are \((1, 4)\), \((1, 5)\), \((2, 4)\), \((2, 5)\), and \((3, 5)\)). | Input: 55 | Output: 31 2 324 52ANSWER1 22 33 42 5 | Master | 3 | 876 | 102 | 376 | 11 |
316 | F3 | 316F3 | F3. Suns and Rays | 2,200 | constructive algorithms; dfs and similar; implementation | Smart Beaver became interested in drawing. He draws suns. However, at some point, Smart Beaver realized that simply drawing suns is boring. So he decided to design a program that will process his drawings. You are given a picture drawn by the beaver. It will have two colors: one for the background and one for the suns ... | The first line contains two integers h and w — the height and width of the image (1 ≤ h, w ≤ 1600). Next h lines will contain w space-separated integers each. They describe Smart Beaver’s picture. Each number equals either a 0 (the image background), or a 1 (the sun color).The input limits for scoring 30 points are (su... | The first line must contain a single number k — the number of suns on the beaver’s image. The second line must contain exactly k space-separated integers, corresponding to the number of rays on each sun. The numbers of the second line must be sorted in the increasing order. | For each complexity level you are suggested a sample in the initial data. You can download the samples at http://www.abbyy.ru/sun.zip. | Hard | 3 | 995 | 647 | 274 | 3 | |
1,379 | E | 1379E | E. Inverse Genealogy | 2,800 | constructive algorithms; divide and conquer; dp; math; trees | Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in... | The input contains two integers \(n\) and \(k\) (\(1 \leq n \leq 100\,000\), \(0 \leq k \leq n\)), the total number of people and the number of imbalanced people. | If there are no constructions with \(n\) people and \(k\) imbalanced people, output NO.Otherwise output YES on the first line, and then \(n\) integers \(s_1, s_2, \ldots, s_n\) (\(0 \leq s_i \leq n\)), which describes the construction and specify the child of each node (or 0, if the person does not have any children). | In the first example case one can have a construction with 3 people, where 1 person has 2 parents.In the second example case one can use the following construction: Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors. | Input: 3 0 | Output: YES 0 1 1 | Master | 5 | 1,215 | 162 | 319 | 13 |
2,039 | F2 | 2039F2 | F2. Shohag Loves Counting (Hard Version) | 3,200 | dp; number theory | This is the hard version of the problem. The only differences between the two versions of this problem are the constraints on \(t\), \(m\), and the sum of \(m\). You can only make hacks if both versions of the problem are solved.For an integer array \(a\) of length \(n\), define \(f(k)\) as the greatest common divisor ... | The first line contains a single integer \(t\) (\(1 \le t \le 3 \cdot 10^5\)) — the number of test cases.The first and only line of each test case contains an integer \(m\) (\(1 \le m \le 10^6\)).Note that there is no limit on the sum of \(m\) over all test cases. | For each test case, output an integer — the number of valid arrays modulo \(998\,244\,353\). | In the first test case, the valid arrays are \([1]\), \([1, 2]\), \([2]\), and \([2, 1]\).In the second test case, there are a total of \(29\) valid arrays. In particular, the array \([2, 1, 4]\) with length \(n = 3\) is valid because all elements are from \(1\) to \(m = 5\) and \(f(1)\), \(f(2)\) and \(f(n = 3)\) all ... | Input: 3259 | Output: 4 29 165 | Master | 2 | 1,128 | 264 | 92 | 20 |
660 | E | 660E | E. Different Subsets For All Tuples | 2,300 | combinatorics; math | For a sequence a of n integers between 1 and m, inclusive, denote f(a) as the number of distinct subsequences of a (including the empty subsequence).You are given two positive integers n and m. Let S be the set of all sequences of length n consisting of numbers from 1 to m. Compute the sum f(a) over all a in S modulo 1... | The only line contains two integers n and m (1 ≤ n, m ≤ 106) — the number of elements in arrays and the upper bound for elements. | Print the only integer c — the desired sum modulo 109 + 7. | Input: 1 3 | Output: 6 | Expert | 2 | 327 | 129 | 58 | 6 | |
1,357 | D3 | 1357D3 | D3. Quantum Classification - Dataset 5 | 0 | *special | This problem is identical to the problem D1 in every aspect except the training dataset. Please refer to that problem for the full problem statement. | Beginner | 1 | 149 | 0 | 0 | 13 | ||||
1,728 | D | 1728D | D. Letter Picking | 1,800 | constructive algorithms; dp; games; two pointers | Alice and Bob are playing a game. Initially, they are given a non-empty string \(s\), consisting of lowercase Latin letters. The length of the string is even. Each player also has a string of their own, initially empty.Alice starts, then they alternate moves. In one move, a player takes either the first or the last let... | The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) — the number of testcases.Each testcase consists of a single line — a non-empty string \(s\), consisting of lowercase Latin letters. The length of the string \(s\) is even.The total length of the strings over all testcases doesn't exceed \(2000\). | For each testcase, print the result of the game if both players play optimally. If Alice wins, print ""Alice"". If Bob wins, print ""Bob"". If it's a draw, print ""Draw"". | One of the possible games Alice and Bob can play in the first testcase: Alice picks the first letter in \(s\): \(s=\)""orces"", \(a=\)""f"", \(b=\)""""; Bob picks the last letter in \(s\): \(s=\)""orce"", \(a=\)""f"", \(b=\)""s""; Alice picks the last letter in \(s\): \(s=\)""orc"", \(a=\)""ef"", \(b=\)""s""; Bob picks... | Input: 2forcesabba | Output: Alice Draw | Medium | 4 | 878 | 317 | 171 | 17 |
1,357 | D4 | 1357D4 | D4. Quantum Classification - Dataset 6 | 0 | *special | This problem is identical to the problem D1 in every aspect except the training dataset. Please refer to that problem for the full problem statement. | Beginner | 1 | 149 | 0 | 0 | 13 | ||||
1,807 | B | 1807B | B. Grab the Candies | 800 | greedy | Mihai and Bianca are playing with bags of candies. They have a row \(a\) of \(n\) bags of candies. The \(i\)-th bag has \(a_i\) candies. The bags are given to the players in the order from the first bag to the \(n\)-th bag. If a bag has an even number of candies, Mihai grabs the bag. Otherwise, Bianca grabs the bag. On... | The first line of the input contains an integer \(t\) (\(1 \leq t \leq 1000\)) — the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 100\)) — the number of bags in the array.The second line of each test case contains \(n\) space-separated integers \(a_i\) (\(1 \leq... | For each test case, output ""YES"" (without quotes) if such a reordering exists, and ""NO"" (without quotes) otherwise.You can output the answer in any case (for example, the strings ""yEs"", ""yes"", ""Yes"" and ""YES"" will be recognized as a positive answer). | In the first test case, Mihai can reorder the array as follows: \([4, 1, 2, 3]\). Then the process proceeds as follows: the first bag has \(4\) candies, which is even, so Mihai takes it — Mihai has \(4\) candies, and Bianca has \(0\). the second bag has \(1\) candies, which is odd, so Bianca takes it — Mihai has \(4\) ... | Input: 341 2 3 441 1 1 231 4 3 | Output: YES NO NO | Beginner | 1 | 672 | 373 | 262 | 18 |
498 | A | 498A | A. Crazy Town | 1,700 | geometry | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the po... | The first line contains two space-separated integers x1, y1 ( - 106 ≤ x1, y1 ≤ 106) — the coordinates of your home.The second line contains two integers separated by a space x2, y2 ( - 106 ≤ x2, y2 ≤ 106) — the coordinates of the university you are studying at.The third line contains an integer n (1 ≤ n ≤ 300) — the nu... | Output the answer to the problem. | Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): | Input: 1 1-1 -120 1 01 0 0 | Output: 2 | Medium | 1 | 970 | 689 | 33 | 4 |
1,182 | A | 1182A | A. Filling Shapes | 1,000 | dp; math | You have a given integer \(n\). Find the number of ways to fill all \(3 \times n\) tiles with the shape described in the picture below. Upon filling, no empty spaces are allowed. Shapes cannot overlap. This picture describes when \(n = 4\). The left one is the shape and the right one is \(3 \times n\) tiles. | The only line contains one integer \(n\) (\(1 \le n \le 60\)) — the length. | Print the number of ways to fill. | In the first example, there are \(4\) possible cases of filling.In the second example, you cannot fill the shapes in \(3 \times 1\) tiles. | Input: 4 | Output: 4 | Beginner | 2 | 309 | 75 | 33 | 11 |
207 | D8 | 207D8 | D8. The Beaver's Problem - 3 | 2,300 | The Smart Beaver from ABBYY came up with another splendid problem for the ABBYY Cup participants! This time the Beaver invites the contest participants to check out a problem on sorting documents by their subjects. Let's describe the problem:You've got some training set of documents. For each document you know its subj... | The first line contains integer id (0 ≤ id ≤ 106) — the document identifier. The second line contains the name of the document. The third and the subsequent lines contain the text of the document. It is guaranteed that the size of any given document will not exceed 10 kilobytes.The tests for this problem are divided in... | Print an integer from 1 to 3, inclusive — the number of the subject the given document corresponds to. | Expert | 0 | 1,472 | 653 | 102 | 2 | |||
1,466 | I | 1466I | I. The Riddle of the Sphinx | 3,400 | binary search; data structures; data structures; interactive | What walks on four feet in the morning, two in the afternoon, and three at night?This is an interactive problem. This problem doesn't support hacks.Sphinx's duty is to guard the city of Thebes by making sure that no unworthy traveler crosses its gates. Only the ones who answer her riddle timely and correctly (or get an... | The first line contains two integers \(n\) and \(b\) (\(1 \leq n, b \leq 200\)). The remaining parts of the input will be given throughout the interaction process. | In all examples, the sequence is fixed beforehand.In the first example, the sequence is \(2, 1, 4, 0, 6\).In the second example, the sequence is \(0, 0, 0, 0\).In the third example, the sequence is \(0\).Note that if the interactor was adaptive, then the interaction in the first and the third example would not be suffi... | Input: 5 3 yes no no no no yes | Output: 5 101 5 110 4 100 3 101 2 001 1 000 0 110 | Master | 4 | 1,111 | 163 | 0 | 14 | |
114 | B | 114B | B. PFAST Inc. | 1,500 | bitmasks; brute force; graphs | When little Petya grew up and entered the university, he started to take part in АСМ contests. Later he realized that he doesn't like how the АСМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members eff... | The first line contains two integer numbers n (1 ≤ n ≤ 16) — the number of volunteers, and m () — the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names — ... | The first output line should contain the single number k — the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. | Input: 3 1PetyaVasyaMashaPetya Vasya | Output: 2MashaPetya | Medium | 3 | 975 | 540 | 320 | 1 | |
1,088 | B | 1088B | B. Ehab and subtraction | 1,000 | implementation; sortings | You're given an array \(a\). You should repeat the following operation \(k\) times: find the minimum non-zero element in the array, print it, and then subtract it from all the non-zero elements of the array. If all the elements are 0s, just print 0. | The first line contains integers \(n\) and \(k\) \((1 \le n,k \le 10^5)\), the length of the array and the number of operations you should perform.The second line contains \(n\) space-separated integers \(a_1, a_2, \ldots, a_n\) \((1 \le a_i \le 10^9)\), the elements of the array. | Print the minimum non-zero element before each operation in a new line. | In the first sample:In the first step: the array is \([1,2,3]\), so the minimum non-zero element is 1.In the second step: the array is \([0,1,2]\), so the minimum non-zero element is 1.In the third step: the array is \([0,0,1]\), so the minimum non-zero element is 1.In the fourth and fifth step: the array is \([0,0,0]\... | Input: 3 51 2 3 | Output: 11100 | Beginner | 2 | 249 | 281 | 71 | 10 |
1,633 | C | 1633C | C. Kill the Monster | 1,100 | brute force; math | Monocarp is playing a computer game. In this game, his character fights different monsters.A fight between a character and a monster goes as follows. Suppose the character initially has health \(h_C\) and attack \(d_C\); the monster initially has health \(h_M\) and attack \(d_M\). The fight consists of several steps: t... | The first line contains one integer \(t\) (\(1 \le t \le 5 \cdot 10^4\)) — the number of test cases. Each test case consists of three lines:The first line contains two integers \(h_C\) and \(d_C\) (\(1 \le h_C \le 10^{15}\); \(1 \le d_C \le 10^9\)) — the character's health and attack;The second line contains two intege... | For each test case, print YES if it is possible to slay the monster by optimally choosing the upgrades. Otherwise, print NO. | In the first example, Monocarp can spend one coin to upgrade weapon (damage will be equal to \(5\)), then health during battle will change as follows: \((h_C, h_M) = (25, 9) \rightarrow (25, 4) \rightarrow (5, 4) \rightarrow (5, -1)\). The battle ended with Monocarp's victory.In the second example, Monocarp has no way ... | Input: 425 49 201 1 1025 412 201 1 10100 145 20 4 109 269 24 2 7 | Output: YES NO YES YES | Easy | 2 | 1,382 | 848 | 124 | 16 |
2,118 | E | 2118E | E. Grid Coloring | 2,400 | constructive algorithms; geometry; greedy; math | There is a \(n\times m\) grid with each cell initially white. You have to color all the cells one-by-one. After you color a cell, all the colored cells furthest from it receive a penalty. Find a coloring order, where no cell has more than \(3\) penalties.Note that \(n\) and \(m\) are both odd.The distance metric used i... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). The description of the test cases follows. The first line of each test case contains two odd integers \(n\) and \(m\) (\(1 \le n, m \le 4999\)) — the number of rows and columns. It is guaranteed that th... | For each test case, output \(n \cdot m\) lines where the \(i\)-th line should contain the coordinates of the \(i\)-th cell in your coloring order. If there are multiple solutions, print any of them.The empty lines in the example output are just for increased readability. You're not required to print them. | In the first test case, the grid can be colored as follows: The numbers indicate the penalty of the cells. | Input: 33 31 11 5 | Output: 2 1 2 3 2 2 1 1 3 2 3 3 3 1 1 3 1 2 1 1 1 2 1 4 1 5 1 1 1 3 | Expert | 4 | 1,089 | 388 | 306 | 21 |
1,365 | C | 1365C | C. Rotation Matching | 1,400 | constructive algorithms; data structures; greedy; implementation | After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size \(n\). Let's call them \(a\) and \(b\).Note that a permutation of \(n\) elements is a sequence of numbers \(a_1, ... | The first line of the input contains a single integer \(n\) \((1 \le n \le 2 \cdot 10^5)\) — the size of the arrays.The second line contains \(n\) integers \(a_1\), \(a_2\), ..., \(a_n\) \((1 \le a_i \le n)\) — the elements of the first permutation.The third line contains \(n\) integers \(b_1\), \(b_2\), ..., \(b_n\) \... | Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times. | For the first case: \(b\) can be shifted to the right by \(k = 1\). The resulting permutations will be \(\{1, 2, 3, 4, 5\}\) and \(\{1, 2, 3, 4, 5\}\).For the second case: The operation is not required. For all possible rotations of \(a\) and \(b\), the number of matching pairs won't exceed \(1\).For the third case: \(... | Input: 5 1 2 3 4 5 2 3 4 5 1 | Output: 5 | Easy | 4 | 1,307 | 382 | 120 | 13 |
1,921 | F | 1921F | F. Sum of Progression | 1,900 | brute force; data structures; dp; implementation; math | You are given an array \(a\) of \(n\) numbers. There are also \(q\) queries of the form \(s, d, k\).For each query \(q\), find the sum of elements \(a_s + a_{s+d} \cdot 2 + \dots + a_{s + d \cdot (k - 1)} \cdot k\). In other words, for each query, it is necessary to find the sum of \(k\) elements of the array with indi... | Each test consists of several testcases. The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) — the number of testcases. Next lines contain descriptions of testcases.The first line of each testcase contains two numbers \(n, q\) (\(1 \le n \le 10^5, 1 \le q \le 2 \cdot 10^5\)) — the number of elements in the... | For each testcase, print \(q\) numbers in a separate line — the desired sums, separated with space. | Input: 53 31 1 21 2 22 2 11 1 23 1-100000000 -100000000 -1000000001 1 35 31 2 3 4 51 2 32 3 21 1 53 1100000000 100000000 1000000001 1 37 734 87 5 42 -44 66 -322 2 24 3 11 3 26 2 15 2 22 5 26 1 2 | Output: 5 1 3 -600000000 22 12 55 600000000 171 42 118 66 -108 23 2 | Hard | 5 | 457 | 778 | 99 | 19 | |
1,421 | D | 1421D | D. Hexagons | 1,900 | brute force; constructive algorithms; greedy; implementation; math; shortest paths | Lindsey Buckingham told Stevie Nicks ""Go your own way"". Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world.Consider a hexagonal tiling of the plane as on the picture below. Nicks wishes to go from the cell marked \((0, 0)\) to a certain cell given by the coordinates. S... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^{4}\)). Description of the test cases follows.The first line of each test case contains two integers \(x\) and \(y\) (\(-10^{9} \le x, y \le 10^{9}\)) representing the coordinates of the target hexagon.The s... | For each testcase output the smallest cost of a path from the origin to the given cell. | The picture below shows the solution for the first sample. The cost \(18\) is reached by taking \(c_3\) 3 times and \(c_2\) once, amounting to \(5+5+5+3=18\). | Input: 2 -3 1 1 3 5 7 9 11 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 | Output: 18 1000000000000000000 | Hard | 6 | 827 | 617 | 87 | 14 |
355 | A | 355A | A. Vasya and Digital Root | 1,100 | constructive algorithms; implementation | Vasya has recently found out what a digital root of a number is and he decided to share his knowledge with you.Let's assume that S(n) is the sum of digits of number n, for example, S(4098) = 4 + 0 + 9 + 8 = 21. Then the digital root of number n equals to: dr(n) = S(n), if S(n) < 10; dr(n) = dr( S(n) ), if S(n) ≥ 10. Fo... | The first line contains two integers k and d (1 ≤ k ≤ 1000; 0 ≤ d ≤ 9). | In a single line print either any number that meets the requirements (without the leading zeroes) or ""No solution"" (without the quotes), if the corresponding number does not exist.The chosen number must consist of exactly k digits. We assume that number 0 doesn't contain any leading zeroes. | For the first test sample dr(5881) = dr(22) = 4.For the second test sample dr(36172) = dr(19) = dr(10) = 1. | Input: 4 4 | Output: 5881 | Easy | 2 | 863 | 71 | 293 | 3 |
690 | B1 | 690B1 | B1. Recover Polygon (easy) | 1,700 | The zombies are gathering in their secret lair! Heidi will strike hard to destroy them once and for all. But there is a little problem... Before she can strike, she needs to know where the lair is. And the intel she has is not very good.Heidi knows that the lair can be represented as a rectangle on a lattice, with side... | The first line of each test case contains one integer N, the size of the lattice grid (5 ≤ N ≤ 50). The next N lines each contain N characters, describing the level of Zombie Contamination of each cell in the lattice. Every character of every line is a digit between 0 and 4.Cells are given in the same order as they are... | The first line of the output should contain Yes if there exists a single non-zero area rectangular lair with corners on the grid for which checking the levels of Zombie Contamination gives the results given in the input, and No otherwise. | The lair, if it exists, has to be rectangular (that is, have corners at some grid points with coordinates (x1, y1), (x1, y2), (x2, y1), (x2, y2)), has a non-zero area and be contained inside of the grid (that is, 0 ≤ x1 < x2 ≤ N, 0 ≤ y1 < y2 ≤ N), and result in the levels of Zombie Contamination as reported in the inpu... | Input: 6000000000000012100024200012100000000 | Output: Yes | Medium | 0 | 878 | 626 | 238 | 6 | |
718 | C | 718C | C. Sasha and Array | 2,300 | data structures; math; matrices | Sasha has an array of integers a1, a2, ..., an. You have to perform m queries. There might be queries of two types: 1 l r x — increase all integers on the segment from l to r by values x; 2 l r — find , where f(x) is the x-th Fibonacci number. As this number may be large, you only have to find it modulo 109 + 7. In thi... | The first line of the input contains two integers n and m (1 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of elements in the array and the number of queries respectively.The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).Then follow m lines with queries descriptions. Each of them contains integers tpi, li... | For each query of the second type print the answer modulo 109 + 7. | Initially, array a is equal to 1, 1, 2, 1, 1.The answer for the first query of the second type is f(1) + f(1) + f(2) + f(1) + f(1) = 1 + 1 + 1 + 1 + 1 = 5. After the query 1 2 4 2 array a is equal to 1, 3, 4, 3, 1.The answer for the second query of the second type is f(3) + f(4) + f(3) = 2 + 3 + 2 = 7.The answer for th... | Input: 5 41 1 2 1 12 1 51 2 4 22 2 42 1 5 | Output: 579 | Expert | 3 | 584 | 580 | 66 | 7 |
1,571 | E | 1571E | E. Fix the String | 1,700 | *special; bitmasks; dp; greedy | A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters ""1"" and ""+"" between the original characters of the sequence. For example: bracket sequences ""()()"" and ""(())"" are regular (the resulting expressions are: ""(1)+(1)"" and ""((1+1)... | The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.Each test case consists of three lines. The first line contains one integer \(n\) (\(4 \le n \le 2 \cdot 10^5\)). The second line contains the string \(s\), consisting of exactly \(n\) characters; each character of \(s\) is eith... | For each test case, print one integer: the minimum number of characters that need to be changed in \(s\), or \(-1\) if it is impossible. | Input: 6 4 ))(( 1 4 ))(( 0 4 ()() 0 6 ))(()( 101 6 ))(()( 001 5 ((((( 11 | Output: 2 0 0 4 1 -1 | Medium | 4 | 1,417 | 572 | 136 | 15 | |
1,794 | C | 1794C | C. Scoring Subsequences | 1,300 | binary search; greedy; math; two pointers | The score of a sequence \([s_1, s_2, \ldots, s_d]\) is defined as \(\displaystyle \frac{s_1\cdot s_2\cdot \ldots \cdot s_d}{d!}\), where \(d!=1\cdot 2\cdot \ldots \cdot d\). In particular, the score of an empty sequence is \(1\).For a sequence \([s_1, s_2, \ldots, s_d]\), let \(m\) be the maximum score among all its su... | 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 an integer \(n\) (\(1\le n\le 10^5\)) — the length of the given sequence. The second line of each test case contains ... | For each test case, output \(n\) integers — the costs of sequences \([a_1, a_2, \ldots , a_k]\) in ascending order of \(k\). | In the first test case: The maximum score among the subsequences of \([1]\) is \(1\). The subsequences \([1]\) and \([]\) (the empty sequence) are the only ones with this score. Thus, the cost of \([1]\) is \(1\). The maximum score among the subsequences of \([1, 2]\) is \(2\). The only subsequence with this score is \... | Input: 331 2 321 155 5 5 5 5 | Output: 1 1 2 1 1 1 2 3 4 5 | Easy | 4 | 831 | 555 | 124 | 17 |
1,221 | F | 1221F | F. Choose a Square | 2,400 | binary search; data structures; sortings | Petya recently found a game ""Choose a Square"". In this game, there are \(n\) points numbered from \(1\) to \(n\) on an infinite field. The \(i\)-th point has coordinates \((x_i, y_i)\) and cost \(c_i\).You have to choose a square such that its sides are parallel to coordinate axes, the lower left and upper right corn... | The first line of the input contains one integer \(n\) (\(1 \le n \le 5 \cdot 10^5\)) — the number of points on the field.Each of the following \(n\) lines contains three integers \(x_i, y_i, c_i\) (\(0 \le x_i, y_i \le 10^9, -10^6 \le c_i \le 10^6\)) — coordinates of the \(i\)-th point and its cost, respectively. | In the first line print the maximum score Petya can achieve.In the second line print four integers \(x_1, y_1, x_2, y_2\) (\(0 \le x_1, y_1, x_2, y_2 \le 2 \cdot 10^9, x_1 = y_1, x_2 = y_2, x_1 \le x_2\)) separated by spaces — the coordinates of the lower left and upper right corners of the square which Petya has to se... | The field corresponding to the first example: | Input: 6 0 0 2 1 0 -5 1 1 3 2 3 4 1 4 -4 3 1 -1 | Output: 4 1 1 3 3 | Expert | 3 | 685 | 315 | 363 | 12 |
2,111 | E | 2111E | E. Changing the String | 1,900 | binary search; data structures; greedy; implementation; sortings; strings | Given a string \(s\) that consists only of the first three letters of the Latin alphabet, meaning each character of the string is either a, b, or c.Also given are \(q\) operations that need to be performed on the string. In each operation, two letters \(x\) and \(y\) from the set of the first three letters of the Latin... | Each test consists of several test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^{3}\)) — the number of test cases. The description of the test cases follows.In the first line of each test case, there are two integers \(n\) and \(q\) (\(1 \le n, q \le 2 \cdot 10^{5}\)) — the length of the stri... | For each test case, output the lexicographically minimal string that can be obtained from \(s\) using the given operations. | In the first test case, both operations need to be applied to the first letter: after the first operation, \(s = \) ""bb"" after the second operation, \(s = \) ""ab"" In the second test case, the string could change as follows: ""bbbbabbbbb"" (changed the \(5\)-th letter) ""cbbbabbbbb"" (changed the \(1\)-st letter) ""... | Input: 32 2cbc bb a10 10bbbbbbbbbbb ab cc bb ac ab cb cb aa bc a30 20abcaababcbbcabcbbcabcbabbbbabcb cb cc ab cb cb ab cb cb ab ab ab ac ab cc ab cc ac ab cc b | Output: ab aaaaabbbbb aaaaaaaaaaaaaaabbbabcbabbbbabc | Hard | 6 | 1,029 | 837 | 123 | 21 |
327 | E | 327E | E. Axis Walking | 2,300 | bitmasks; combinatorics; constructive algorithms; dp; meet-in-the-middle | Iahub wants to meet his girlfriend Iahubina. They both live in Ox axis (the horizontal axis). Iahub lives at point 0 and Iahubina at point d.Iahub has n positive integers a1, a2, ..., an. The sum of those numbers is d. Suppose p1, p2, ..., pn is a permutation of {1, 2, ..., n}. Then, let b1 = ap1, b2 = ap2 and so on. T... | The first line contains an integer n (1 ≤ n ≤ 24). The following line contains n integers: a1, a2, ..., an (1 ≤ ai ≤ 109). The third line contains integer k (0 ≤ k ≤ 2). The fourth line contains k positive integers, representing the numbers that give Iahub bad luck. Each of these numbers does not exceed 109. | Output a single integer — the answer of Iahub's dilemma modulo 1000000007 (109 + 7). | In the first case consider six possible orderings: [2, 3, 5]. Iahub will stop at position 2, 5 and 10. Among them, 5 is bad luck for him. [2, 5, 3]. Iahub will stop at position 2, 7 and 10. Among them, 7 is bad luck for him. [3, 2, 5]. He will stop at the unlucky 5. [3, 5, 2]. This is a valid ordering. [5, 2, 3]. He go... | Input: 32 3 525 7 | Output: 1 | Expert | 5 | 965 | 309 | 84 | 3 |
1,599 | B | 1599B | B. Restaurant Game | 3,100 | Alice and Bob always had hard time choosing restaurant for the dinner. Previously they performed Eenie Meenie Miney Mo game, but eventually as their restaurant list grew, they had to create a new game. This new game starts as they write restaurant names on \(N\) cards and align the cards in one line. Before the game be... | The first line of the input is one integer \(T\) (\(1\) \(\leq\) \(T\) \(\leq\) \(10^{4}\)) representing number of test cases. Each test case contains 3 lines: The first line contains an integer \(N\) representing initial number of cards. Next line contains two integer values \(A,B\) (\(0\) \(\leq\) \(A, B\) < \(N\), \... | The output contains \(T\) integer number – the 0-based index of the last card that remains for every test case in order. | Note that since Alice is starting at the beginning of the line even though her initial direction is left, on her next move she will go right. | Input: 1 4 0 1 left right | Output: 0 | Master | 0 | 978 | 548 | 120 | 15 | |
845 | B | 845B | B. Luba And The Ticket | 1,600 | brute force; greedy; implementation | Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky.The ticket is considered lucky if the sum of first three digits equals to the sum of last ... | You are given a string consisting of 6 characters (all characters are digits from 0 to 9) — this string denotes Luba's ticket. The ticket can start with the digit 0. | Print one number — the minimum possible number of digits Luba needs to replace to make the ticket lucky. | In the first example the ticket is already lucky, so the answer is 0.In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required.In the third example Luba can replace any zero with 3. It's easy to see that at least one replac... | Input: 000000 | Output: 0 | Medium | 3 | 333 | 165 | 104 | 8 |
582 | C | 582C | C. Superior Periodic Subarrays | 2,400 | number theory | You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, . A periodic subarray (l, s) (0 ≤ l < n, 1 ≤ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l.A periodic subarray (l, s) is superior... | The first line contains number n (1 ≤ n ≤ 2·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≤ ai ≤ 106), separated by a space. | Print a single integer — the sought number of pairs. | In the first sample the superior subarrays are (0, 1) and (3, 2).Subarray (0, 1) is superior, as a0 ≥ a0, a0 ≥ a1, a0 ≥ a2, a0 ≥ a3, a0 ≥ a0, ...Subarray (3, 2) is superior a3 ≥ a3, a0 ≥ a0, a3 ≥ a1, a0 ≥ a2, a3 ≥ a3, ...In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an... | Input: 47 1 2 3 | Output: 2 | Expert | 1 | 675 | 142 | 52 | 5 |
105 | B | 105B | B. Dark Assembly | 1,800 | brute force; probabilities | Dark Assembly is a governing body in the Netherworld. Here sit the senators who take the most important decisions for the player. For example, to expand the range of the shop or to improve certain characteristics of the character the Dark Assembly's approval is needed.The Dark Assembly consists of n senators. Each of t... | The first line contains three integers n, k and A (1 ≤ n, k ≤ 8, 1 ≤ A ≤ 9999).Then n lines follow. The i-th of them contains two numbers — bi and li — the i-th senator's level and his loyalty.The levels of all senators are integers in range from 1 to 9999 (inclusive). The loyalties of all senators are integers in rang... | Print one real number with precision 10 - 6 — the maximal possible probability that the Dark Assembly approves the player's proposal for the best possible distribution of candies among the senators. | In the first sample the best way of candies' distribution is giving them to first three of the senators. It ensures most of votes.It the second sample player should give all three candies to the fifth senator. | Input: 5 6 10011 8014 9023 7080 30153 70 | Output: 1.0000000000 | Medium | 2 | 1,887 | 384 | 198 | 1 |
1,266 | E | 1266E | E. Spaceship Solitaire | 2,100 | data structures; greedy; implementation | Bob is playing a game of Spaceship Solitaire. The goal of this game is to build a spaceship. In order to do this, he first needs to accumulate enough resources for the construction. There are \(n\) types of resources, numbered \(1\) through \(n\). Bob needs at least \(a_i\) pieces of the \(i\)-th resource to build the ... | The first line contains a single integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) — the number of types of resources.The second line contains \(n\) space separated integers \(a_1, a_2, \dots, a_n\) (\(1 \leq a_i \leq 10^9\)), the \(i\)-th of which is the goal for the \(i\)-th resource.The third line contains a single int... | Output \(q\) lines, each consisting of a single integer, the \(i\)-th represents the answer after the \(i\)-th update. | After the first update, the optimal strategy is as follows. First produce \(2\) once, which gives a free resource \(1\). Then, produce \(2\) twice and \(1\) once, for a total of four turns.After the second update, the optimal strategy is to produce \(2\) three times — the first two times a single unit of resource \(1\)... | Input: 2 2 3 5 2 1 1 2 2 1 1 1 1 2 1 2 2 2 0 | Output: 4 3 3 2 3 | Hard | 3 | 1,703 | 974 | 118 | 12 |
1,809 | B | 1809B | B. Points on Plane | 1,000 | binary search; greedy; math | You are given a two-dimensional plane, and you need to place \(n\) chips on it. You can place a chip only at a point with integer coordinates. The cost of placing a chip at the point \((x, y)\) is equal to \(|x| + |y|\) (where \(|a|\) is the absolute value of \(a\)).The cost of placing \(n\) chips is equal to the maxim... | The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. Next \(t\) cases follow.The first and only line of each test case contains one integer \(n\) (\(1 \le n \le 10^{18}\)) — the number of chips you need to place. | For each test case, print a single integer — the minimum cost to place \(n\) chips if the distance between each pair of chips must be strictly greater than \(1\). | In the first test case, you can place the only chip at point \((0, 0)\) with total cost equal to \(0 + 0 = 0\).In the second test case, you can, for example, place chips at points \((-1, 0)\), \((0, 1)\) and \((1, 0)\) with costs \(|-1| + |0| = 1\), \(|0| + |1| = 1\) and \(|0| + |1| = 1\). Distance between each pair of... | Input: 4135975461057789971042 | Output: 0 1 2 987654321 | Beginner | 3 | 533 | 252 | 162 | 18 |
228 | B | 228B | B. Two Tables | 1,400 | brute force; implementation | You've got two rectangular tables with sizes na × ma and nb × mb cells. The tables consist of zeroes and ones. We will consider the rows and columns of both tables indexed starting from 1. Then we will define the element of the first table, located at the intersection of the i-th row and the j-th column, as ai, j; we w... | The first line contains two space-separated integers na, ma (1 ≤ na, ma ≤ 50) — the number of rows and columns in the first table. Then na lines contain ma characters each — the elements of the first table. Each character is either a ""0"", or a ""1"".The next line contains two space-separated integers nb, mb (1 ≤ nb, ... | Print two space-separated integers x, y (|x|, |y| ≤ 109) — a shift with maximum overlap factor. If there are multiple solutions, print any of them. | Input: 3 20110002 3001111 | Output: 0 1 | Easy | 2 | 1,005 | 608 | 147 | 2 | |
500 | A | 500A | A. New Year Transportation | 1,000 | dfs and similar; graphs; implementation | New Year is coming in Line World! In this world, there are n cells numbered by integers from 1 to n, as a 1 × n board. People live in cells. However, it was hard to move between distinct cells, because of the difficulty of escaping the cell. People wanted to meet people who live in other cells.So, user tncks0121 has ma... | The first line contains two space-separated integers n (3 ≤ n ≤ 3 × 104) and t (2 ≤ t ≤ n) — the number of cells, and the index of the cell which I want to go to.The second line contains n - 1 space-separated integers a1, a2, ..., an - 1 (1 ≤ ai ≤ n - i). It is guaranteed, that using the given transportation system, on... | If I can go to cell t using the transportation system, print ""YES"". Otherwise, print ""NO"". | In the first sample, the visited cells are: 1, 2, 4; so we can successfully visit the cell 4.In the second sample, the possible cells to visit are: 1, 2, 4, 6, 7, 8; so we can't visit the cell 5, which we want to visit. | Input: 8 41 2 1 2 1 2 1 | Output: YES | Beginner | 3 | 1,203 | 350 | 94 | 5 |
930 | B | 930B | B. Game with String | 1,600 | implementation; probabilities; strings | Vasya and Kolya play a game with a string, using the following rules. Initially, Kolya creates a string s, consisting of small English letters, and uniformly at random chooses an integer k from a segment [0, len(s) - 1]. He tells Vasya this string s, and then shifts it k letters to the left, i. e. creates a new string ... | The only string contains the string s of length l (3 ≤ l ≤ 5000), consisting of small English letters only. | Print the only number — the answer for the problem. You 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 considered correct if | In the first example Vasya can always open the second letter after opening the first letter, and the cyclic shift is always determined uniquely.In the second example if the first opened letter of t is ""t"" or ""c"", then Vasya can't guess the shift by opening only one other letter. On the other hand, if the first lett... | Input: technocup | Output: 1.000000000000000 | Medium | 3 | 1,037 | 107 | 239 | 9 |
793 | E | 793E | E. Problem of offices | 2,900 | constructive algorithms; dfs and similar; dp; trees | Earlier, when there was no Internet, each bank had a lot of offices all around Bankopolis, and it caused a lot of problems. Namely, each day the bank had to collect cash from all the offices.Once Oleg the bank client heard a dialogue of two cash collectors. Each day they traveled through all the departments and offices... | The first line contains single integer n (5 ≤ n ≤ 5000) — the total number of offices and departments. The departments and offices are numbered from 1 to n, the central office has index 1.The second line contains four integers a, b, c and d (2 ≤ a, b, c, d ≤ n) — the indexes of the departments mentioned in collector's ... | If the situation described by the cash collectors was possible, print ""YES"". Otherwise, print ""NO"". | In the first example the following collector's route was possible: . We can note that between their visits to offices a and b the collectors visited the same number of offices as between visits to offices b and a; the same holds for c and d (the collectors' route is infinite as they follow it each day).In the second ex... | Input: 52 3 4 51 1 1 1 | Output: YES | Master | 4 | 1,736 | 894 | 103 | 7 |
2,057 | A | 2057A | A. MEX Table | 800 | constructive algorithms; math | One day, the schoolboy Mark misbehaved, so the teacher Sasha called him to the whiteboard.Sasha gave Mark a table with \(n\) rows and \(m\) columns. His task is to arrange the numbers \(0, 1, \ldots, n \cdot m - 1\) in the table (each number must be used exactly once) in such a way as to maximize the sum of MEX\(^{\tex... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n, m \le 10^9\)) — the number of rows and columns in the tab... | For each test case, output the maximum possible sum of \(\operatorname{mex}\) across all rows and columns. | In the first test case, the only element is \(0\), and the sum of the \(\operatorname{mex}\) of the numbers in the first row and the \(\operatorname{mex}\) of the numbers in the first column is \(\operatorname{mex}(\{0\}) + \operatorname{mex}(\{0\}) = 1 + 1 = 2\).In the second test case, the optimal table may look as f... | Input: 31 12 23 5 | Output: 2 3 6 | Beginner | 2 | 1,319 | 337 | 106 | 20 |
522 | C | 522C | C. Chicken or Fish? | 2,100 | greedy | Polycarp is flying in the airplane. Finally, it is his favorite time — the lunchtime. The BerAvia company stewardess is giving food consecutively to all the passengers from the 1-th one to the last one. Polycarp is sitting on seat m, that means, he will be the m-th person to get food.The flight menu has k dishes in tot... | Each test in this problem consists of one or more input sets. First goes a string that contains a single integer t (1 ≤ t ≤ 100 000) — the number of input data sets in the test. Then the sets follow, each set is preceded by an empty line.The first line of each set of the input contains integers m, k (2 ≤ m ≤ 100 000, 1... | For each input set print the answer as a single line. Print a string of k letters ""Y"" or ""N"". Letter ""Y"" in position i should be printed if they could have run out of the i-th dish by the time the stewardess started serving Polycarp. | In the first input set depending on the choice of the second passenger the situation could develop in different ways: If he chose the first dish, then by the moment the stewardess reaches Polycarp, they will have run out of the first dish; If he chose the fourth dish, then by the moment the stewardess reaches Polycarp,... | Input: 23 42 3 2 11 00 05 51 2 1 3 13 00 02 14 0 | Output: YNNYYYYNY | Hard | 1 | 1,436 | 1,288 | 239 | 5 |
1,438 | C | 1438C | C. Engineer Artem | 2,000 | 2-sat; chinese remainder theorem; constructive algorithms; fft; flows | Artem is building a new robot. He has a matrix \(a\) consisting of \(n\) rows and \(m\) columns. The cell located on the \(i\)-th row from the top and the \(j\)-th column from the left has a value \(a_{i,j}\) written in it. If two adjacent cells contain the same value, the robot will break. A matrix is called good if n... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10\)). Description of the test cases follows.The first line of each test case contains two integers \(n, m\) (\(1 \le n \le 100\), \(1 \le m \le 100\)) — the number of rows and columns, respectively.The followi... | For each case, output \(n\) lines each containing \(m\) integers. The \(j\)-th integer in the \(i\)-th line is \(b_{i,j}\). | In all the cases, you can verify that no two adjacent cells have the same value and that \(b\) is the same as \(a\) with some values incremented by one. | Input: 3 3 2 1 2 4 5 7 8 2 2 1 1 3 3 2 2 1 3 2 2 | Output: 1 2 5 6 7 8 2 1 4 3 2 4 3 2 | Hard | 5 | 887 | 452 | 123 | 14 |
1,666 | H | 1666H | 3,500 | math | Master | 1 | 0 | 0 | 0 | 16 | ||||||
1,678 | B2 | 1678B2 | B2. Tokitsukaze and Good 01-String (hard version) | 1,800 | dp; greedy; implementation | This is the hard version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string \(s\) of length \(n\), consisting only of zeros and ones, \(n\) is even.Now Tokitsukaze divides \(s\) into the minimum nu... | The first contains a single positive integer \(t\) (\(1 \leq t \leq 10\,000\)) — the number of test cases.For each test case, the first line contains a single integer \(n\) (\(2 \leq n \leq 2 \cdot 10^5\)) — the length of \(s\), it is guaranteed that \(n\) is even.The second line contains a binary string \(s\) of lengt... | For each test case, print a single line with two integers — the minimum number of operations to make \(s\) good, and the minimum number of subsegments that \(s\) can be divided into among all solutions with the minimum number of operations. | In the first test case, one of the ways to make \(s\) good is the following.Change \(s_3\), \(s_6\) and \(s_7\) to '0', after that \(s\) becomes ""1100000000"", it can be divided into ""11"" and ""00000000"", which lengths are \(2\) and \(8\) respectively, the number of subsegments of it is \(2\). There are other ways ... | Input: 51011100110008110011112002116100110 | Output: 3 2 0 3 0 1 0 1 3 1 | Medium | 3 | 1,335 | 455 | 240 | 16 |
1,682 | F | 1682F | F. MCMF? | 2,700 | data structures; flows; graphs; greedy; sortings; two pointers | You are given two integer arrays \(a\) and \(b\) (\(b_i \neq 0\) and \(|b_i| \leq 10^9\)). Array \(a\) is sorted in non-decreasing order.The cost of a subarray \(a[l:r]\) is defined as follows:If \( \sum\limits_{j = l}^{r} b_j \neq 0\), then the cost is not defined.Otherwise: Construct a bipartite flow graph with \(r-l... | The first line of input contains two integers \(n\) and \(q\) \((2 \leq n \leq 2\cdot 10^5, 1 \leq q \leq 2\cdot10^5)\) — length of arrays \(a\), \(b\) and the number of queries.The next line contains \(n\) integers \(a_1,a_2 \ldots a_n\) (\(0 \leq a_1 \le a_2 \ldots \le a_n \leq 10^9)\) — the array \(a\). It is guaran... | For each query \(l_i\), \(r_i\) — print the cost of subarray \(a[l_i:r_i]\) modulo \(10^9 + 7\). | In the first query, the maximum possible flow is \(1\) i.e one unit from source to \(2\), then one unit from \(2\) to \(3\), then one unit from \(3\) to sink. The cost of the flow is \(0 \cdot 1 + |2 - 4| \cdot 1 + 0 \cdot 1 = 2\).In the second query, the maximum possible flow is again \(1\) i.e from source to \(7\), \... | Input: 8 4 1 2 4 5 9 10 10 13 6 -1 1 -3 2 1 -1 1 2 3 6 7 3 5 2 6 | Output: 2 0 9 15 | Master | 6 | 1,268 | 658 | 96 | 16 |
2,131 | B | 2131B | B. Alternating Series | 0 | constructive algorithms; greedy; math | You are given an integer \(n\). Call an array \(a\) of length \(n\) good if: For all \(1 \le i < n\), \(a_i \cdot a_{i+1} < 0\) (i.e., the product of adjacent elements is negative). For all subarrays\(^{\text{∗}}\) with length at least \(2\), the sum of all elements in the subarray is positive\(^{\text{†}}\). Additiona... | The first line contains an integer \(t\) (\(1 \leq t \leq 500\)) — the number of test cases.The single line of each test case contains one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) — the length of your array.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\). | For each test case, output \(n\) integers \(a_1, a_2, \dots, a_n\) (\(-10^9 \leq a_i \leq 10^9\)), the elements of your array on a new line. | In the first test case, because \(a_1 \cdot a_2 = -2 < 0\) and \(a_1 + a_2 = 1 > 0\), it satisfies the two constraints. In addition, it can be shown that the corresponding \(b = [1, 2]\) is better than any other good array of length \(2\). | Input: 223 | Output: -1 2 -1 3 -1 | Beginner | 3 | 1,309 | 303 | 140 | 21 |
245 | D | 245D | D. Restoring Table | 1,500 | constructive algorithms; greedy | Recently Polycarpus has learned the ""bitwise AND"" operation (which is also called ""AND"") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation.For that Polycarpus came to school a little earlier and wrote on the board a sequence of non-negativ... | The first line contains a single integer n (1 ≤ n ≤ 100) — the size of square matrix b. Next n lines contain matrix b. The i-th of these lines contains n space-separated integers: the j-th number represents the element of matrix bij. It is guaranteed, that for all i (1 ≤ i ≤ n) the following condition fulfills: bii = -... | Print n non-negative integers a1, a2, ..., an (0 ≤ ai ≤ 109) — the sequence that Polycarpus wiped off the board. Separate the numbers by whitespaces. It is guaranteed that there is sequence a that satisfies the problem conditions. If there are multiple such sequences, you are allowed to print any of them. | If you do not know what is the ""bitwise AND"" operation please read: http://en.wikipedia.org/wiki/Bitwise_operation. | Input: 1-1 | Output: 0 | Medium | 2 | 1,121 | 439 | 306 | 2 |
616 | F | 616F | F. Expensive Strings | 2,700 | data structures; sortings; string suffix structures; strings | You are given n strings ti. Each string has cost ci.Let's define the function of string , where ps, i is the number of occurrences of s in ti, |s| is the length of the string s. Find the maximal value of function f(s) over all strings.Note that the string s is not necessarily some string from t. | The first line contains the only integer n (1 ≤ n ≤ 105) — the number of strings in t.Each of the next n lines contains contains a non-empty string ti. ti contains only lowercase English letters.It is guaranteed that the sum of lengths of all strings in t is not greater than 5·105.The last line contains n integers ci (... | Print the only integer a — the maximal value of the function f(s) over all strings s. Note one more time that the string s is not necessarily from t. | Input: 2aabb2 1 | Output: 4 | Master | 4 | 296 | 369 | 149 | 6 | |
1,938 | C | 1938C | 1,900 | Hard | 0 | 0 | 0 | 0 | 19 | |||||||
1,213 | B | 1213B | B. Bad Prices | 1,100 | data structures; implementation | Polycarp analyzes the prices of the new berPhone. At his disposal are the prices for \(n\) last days: \(a_1, a_2, \dots, a_n\), where \(a_i\) is the price of berPhone on the day \(i\).Polycarp considers the price on the day \(i\) to be bad if later (that is, a day with a greater number) berPhone was sold at a lower pri... | The first line contains an integer \(t\) (\(1 \le t \le 10000\)) — the number of sets of input data in the test. Input data sets must be processed independently, one after another.Each input data set consists of two lines. The first line contains an integer \(n\) (\(1 \le n \le 150000\)) — the number of days. The secon... | Print \(t\) integers, the \(j\)-th of which should be equal to the number of days with a bad price in the \(j\)-th input data set. | Input: 5 6 3 9 4 6 7 5 1 1000000 2 2 1 10 31 41 59 26 53 58 97 93 23 84 7 3 2 1 2 3 4 5 | Output: 3 0 1 8 2 | Easy | 2 | 593 | 546 | 130 | 12 | |
1,231 | E | 1231E | E. Middle-Out | 2,200 | constructive algorithms; greedy; strings | The problem was inspired by Pied Piper story. After a challenge from Hooli's compression competitor Nucleus, Richard pulled an all-nighter to invent a new approach to compression: middle-out.You are given two strings \(s\) and \(t\) of the same length \(n\). Their characters are numbered from \(1\) to \(n\) from left t... | The first line contains integer \(q\) (\(1 \le q \le 100\)) — the number of independent test cases in the input.Each test case is given in three lines. The first line of a test case contains \(n\) (\(1 \le n \le 100\)) — the length of the strings \(s\) and \(t\). The second line contains \(s\), the third line contains ... | For every test print minimum possible number of moves, which are needed to transform \(s\) into \(t\), or -1, if it is impossible to do. | In the first example, the moves in one of the optimal answers are: for the first test case \(s=\)""iredppipe"", \(t=\)""piedpiper"": ""iredppipe"" \(\rightarrow\) ""iedppiper"" \(\rightarrow\) ""piedpiper""; for the second test case \(s=\)""estt"", \(t=\)""test"": ""estt"" \(\rightarrow\) ""test""; for the third test c... | Input: 3 9 iredppipe piedpiper 4 estt test 4 tste test | Output: 2 1 2 | Hard | 3 | 1,584 | 533 | 136 | 12 |
796 | D | 796D | D. Police Stations | 2,100 | constructive algorithms; dfs and similar; dp; graphs; shortest paths; trees | Inzane finally found Zane with a lot of money to spare, so they together decided to establish a country of their own.Ruling a country is not an easy job. Thieves and terrorists are always ready to ruin the country's peace. To fight back, Zane and Inzane have enacted a very effective law: from each city it must be possi... | The first line contains three integers n, k, and d (2 ≤ n ≤ 3·105, 1 ≤ k ≤ 3·105, 0 ≤ d ≤ n - 1) — the number of cities, the number of police stations, and the distance limitation in kilometers, respectively.The second line contains k integers p1, p2, ..., pk (1 ≤ pi ≤ n) — each denoting the city each police station is... | In the first line, print one integer s that denotes the maximum number of roads that can be shut down.In the second line, print s distinct integers, the indices of such roads, in any order.If there are multiple answers, print any of them. | In the first sample, if you shut down road 5, all cities can still reach a police station within k = 4 kilometers.In the second sample, although this is the only largest valid set of roads that can be shut down, you can print either 4 5 or 5 4 in the second line. | Input: 6 2 41 61 22 33 44 55 6 | Output: 15 | Hard | 6 | 1,176 | 667 | 238 | 7 |
409 | I | 409I | I. Feed the Golorp | 2,400 | *special | Golorps are mysterious creatures who feed on variables. Golorp's name is a program in some programming language. Some scientists believe that this language is Befunge; golorps are tantalizingly silent.Variables consumed by golorps can take values from 0 to 9, inclusive. For each golorp its daily diet is defined by its ... | The input is a single string (between 13 and 1024 characters long) — the name of the visiting golorp. All names are similar and will resemble the ones given in the samples. The name is guaranteed to be valid. | Output lexicographically smallest sequence of variable values fit for feeding this golorp. Values should be listed in the order in which they get into the jaws. If the golorp is impossible to feed, output ""false"". | Input: ?(_-_/___*__):-___>__. | Output: 0010 | Expert | 1 | 913 | 208 | 215 | 4 | |
2,044 | G1 | 2044G1 | G1. Medium Demon Problem (easy version) | 1,700 | dfs and similar; graph matchings; graphs; implementation; trees | This is the easy version of the problem. The key difference between the two versions is highlighted in bold.A group of \(n\) spiders has come together to exchange plushies. Initially, each spider has \(1\) plushie. Every year, if spider \(i\) has at least one plushie, he will give exactly one plushie to spider \(r_i\).... | The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) — the number of test cases.The first line of each test case contains an integer \(n\) (\(2 \leq n \leq 2 \cdot 10^5\)) — the number of spiders.The following line contains \(n\) integers \(r_1, r_2, \ldots, r_n\) (\(1 \leq r_i \leq n, r_i \neq i\)) — the ... | For each test case, output an integer on a new line, the first year in which the process becomes stable. | For the second test case: At year \(1\), the following array shows the number of plushies each spider has: \([1, 1, 1, 1, 1]\). Then, year \(1\)'s exchange happens. At year \(2\), the following array shows the number of plushies each spider has: \([1, 1, 1, 1, 1]\). Since this array is the same as the previous year, th... | Input: 522 152 3 4 5 152 1 4 2 354 1 1 5 4104 3 9 1 6 7 9 10 10 3 | Output: 2 2 5 4 5 | Medium | 5 | 815 | 452 | 104 | 20 |
1,987 | F1 | 1987F1 | F1. Interesting Problem (Easy Version) | 2,500 | dp | 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.You are given an array of integers \(a\) of length \(n\). In one operation, you will perform the following two-step process: Choose an inde... | Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 100\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 100\)) — the length of the array \(a\).The second li... | For each test case, output a single integer — the maximum number of times that you can perform the operation. | In the first test case, one possible optimal sequence of operations is \([ 1, 5, \color{red}{3}, \color{red}{2}, 4 ] \rightarrow [\color{red}{1}, \color{red}{5}, 4] \rightarrow [4]\).In the third test case, one possible optimal sequence of operations is \([1, \color{red}{2}, \color{red}{3}] \rightarrow [1]\). | Input: 651 5 3 2 482 1 3 4 5 6 7 831 2 341 2 4 454 4 1 3 511 | Output: 2 3 1 2 0 0 | Expert | 1 | 530 | 530 | 109 | 19 |
507 | A | 507A | A. Amr and Music | 1,000 | greedy; implementation; sortings | Amr is a young coder who likes music a lot. He always wanted to learn how to play music but he was busy coding so he got an idea.Amr has n instruments, it takes ai days to learn i-th instrument. Being busy, Amr dedicated k days to learn how to play the maximum possible number of instruments.Amr asked for your help to d... | The first line contains two numbers n, k (1 ≤ n ≤ 100, 0 ≤ k ≤ 10 000), the number of instruments and number of days respectively.The second line contains n integers ai (1 ≤ ai ≤ 100), representing number of days required to learn the i-th instrument. | In the first line output one integer m representing the maximum number of instruments Amr can learn.In the second line output m space-separated integers: the indices of instruments to be learnt. You may output indices in any order.if there are multiple optimal solutions output any. It is not necessary to use all days f... | In the first test Amr can learn all 4 instruments.In the second test other possible solutions are: {2, 3, 5} or {3, 4, 5}.In the third test Amr doesn't have enough time to learn the only presented instrument. | Input: 4 104 3 1 2 | Output: 41 2 3 4 | Beginner | 3 | 396 | 251 | 332 | 5 |
679 | A | 679A | A. Bear and Prime 100 | 1,400 | constructive algorithms; interactive; math | This is an interactive problem. In the output section below you will see the information about flushing the output.Bear Limak thinks of some hidden number — an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite.Integer x > 1 is called prime if it has exactly two distinct divi... | After each query you should read one string from the input. It will be ""yes"" if the printed integer is a divisor of the hidden number, and ""no"" otherwise. | Up to 20 times you can ask a query — print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input.In any moment you can print the answer ""prime"" or ""composite"" (without the quotes). After that, fl... | The hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process.The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden numb... | Input: yesnoyes | Output: 2805composite | Easy | 3 | 1,200 | 158 | 777 | 6 |
1,204 | E | 1204E | E. Natasha, Sasha and the Prefix Sums | 2,300 | combinatorics; dp; math; number theory | Natasha's favourite numbers are \(n\) and \(1\), and Sasha's favourite numbers are \(m\) and \(-1\). One day Natasha and Sasha met and wrote down every possible array of length \(n+m\) such that some \(n\) of its elements are equal to \(1\) and another \(m\) elements are equal to \(-1\). For each such array they counte... | The only line contains two integers \(n\) and \(m\) (\(0 \le n,m \le 2\,000\)). | Output the answer to the problem modulo \(998\: 244\: 853\). | In the first example the only possible array is [-1,-1], its maximal prefix sum is equal to \(0\). In the second example the only possible array is [1,1], its maximal prefix sum is equal to \(2\). There are \(6\) possible arrays in the third example:[1,1,-1,-1], f([1,1,-1,-1]) = 2[1,-1,1,-1], f([1,-1,1,-1]) = 1[1,-1,-1... | Input: 0 2 | Output: 0 | Expert | 4 | 878 | 79 | 60 | 12 |
1,765 | C | 1765C | C. Card Guessing | 2,600 | combinatorics; dp; probabilities | Consider a deck of cards. Each card has one of \(4\) suits, and there are exactly \(n\) cards for each suit — so, the total number of cards in the deck is \(4n\). The deck is shuffled randomly so that each of \((4n)!\) possible orders of cards in the deck has the same probability of being the result of shuffling. Let \... | The first (and only) line contains two integers \(n\) (\(1 \le n \le 500\)) and \(k\) (\(1 \le k \le 4 \cdot n\)). | Let the expected value you have to calculate be an irreducible fraction \(\dfrac{x}{y}\). Print one integer — the value of \(x \cdot y^{-1} \bmod 998244353\), where \(y^{-1}\) is the inverse to \(y\) (i. e. an integer such that \(y \cdot y^{-1} \bmod 998244353 = 1\)). | Input: 1 1 | Output: 748683266 | Expert | 3 | 1,471 | 114 | 268 | 17 | |
2,045 | K | 2045K | K. GCDDCG | 2,900 | You are playing the Greatest Common Divisor Deck-Building Card Game (GCDDCG). There are \(N\) cards (numbered from \(1\) to \(N\)). Card \(i\) has the value of \(A_i\), which is an integer between \(1\) and \(N\) (inclusive).The game consists of \(N\) rounds (numbered from \(1\) to \(N\)). Within each round, you need t... | The first line consists of an integer \(N\) (\(2 \leq N \leq 200\,000)\).The second line consists of \(N\) integers \(A_i\) (\(1 \leq A_i \leq N\)). | Output a single integer representing the sum of creativity points across all \(N\) rounds modulo \(998\,244\,353\). | Explanation for the sample input/output #1The creativity point during each of rounds \(1\) and \(2\) is \(0\).During round \(3\), there are \(12\) ways to build both decks. Denote \(B\) and \(C\) as the set of card numbers within deck \(1\) and deck \(2\), respectively. The \(12\) ways to build both decks are: \(B = \{... | Input: 3 3 3 3 | Output: 36 | Master | 0 | 885 | 148 | 115 | 20 | |
1,720 | B | 1720B | B. Interesting Sum | 800 | brute force; data structures; greedy; math; sortings | You are given an array \(a\) that contains \(n\) integers. You can choose any proper subsegment \(a_l, a_{l + 1}, \ldots, a_r\) of this array, meaning you can choose any two integers \(1 \le l \le r \le n\), where \(r - l + 1 < n\). We define the beauty of a given subsegment as the value of the following expression:$$$... | The first line contains one integer \(t\) (\(1 \leq t \leq 1000\)) — the number of test cases. Then follow the descriptions of each test case.The first line of each test case contains a single integer \(n\) \((4 \leq n \leq 10^5)\) — the length of the array.The second line of each test case contains \(n\) integers \(a_... | For each testcase print a single integer — the maximum beauty of a proper subsegment. | In the first test case, the optimal segment is \(l = 7\), \(r = 8\). The beauty of this segment equals to \((6 - 1) + (5 - 1) = 9\).In the second test case, the optimal segment is \(l = 2\), \(r = 4\). The beauty of this segment equals \((100 - 2) + (200 - 1) = 297\). | Input: 4 8 1 2 2 3 1 5 6 1 5 1 2 3 100 200 4 3 3 3 3 6 7 8 3 1 1 8 | Output: 9 297 0 14 | Beginner | 5 | 585 | 489 | 85 | 17 |
648 | E | 648E | E. Собери число | 2,300 | graphs; shortest paths | Дано целое неотрицательное число k и n неотрицательных целых чисел a1, a2, ..., an. Записывая некоторые из этих чисел друг за другом в произвольном порядке и, возможно, используя какие-то из них несколько раз (а какие-то вообще не используя), требуется составить кратчайшее (наименьшее по количеству цифр) число, делящее... | В первой строке содержится два целых числа n (1 ≤ n ≤ 1 000 000) и k (1 ≤ k ≤ 1000) — количество чисел и требуемый делитель соответственно.Во второй строке содержится n целых чисел a1, a2, ..., an (0 ≤ ai ≤ 109). | Если ответ существует, в первой строке выведите «YES» (без кавычек), а во второй строке — искомое кратчайшее число без ведущих нулей. В случае если ответа не существует, в единственной строке выходных данных выведите «NO» (без кавычек). | Input: 2 3123 1 | Output: YES123 | Expert | 2 | 364 | 212 | 236 | 6 | |
238 | D | 238D | D. Tape Programming | 2,900 | data structures; implementation | There is a programming language in which every program is a non-empty sequence of ""<"" and "">"" signs and digits. Let's explain how the interpreter of this programming language works. A program is interpreted using movement of instruction pointer (IP) which consists of two parts. Current character pointer (CP); Direc... | The first line of input contains two integers n and q (1 ≤ n, q ≤ 105) — represents the length of the sequence s and the number of queries. The second line contains s, a sequence of ""<"", "">"" and digits (0..9) written from left to right. Note, that the characters of s are not separated with spaces. The next q lines ... | For each query print 10 space separated integers: x0, x1, ..., x9 where xi equals the number of times the interpreter prints i while running the corresponding program. Print answers to the queries in the order they are given in input. | Input: 7 41>3>22<1 34 77 71 7 | Output: 0 1 0 1 0 0 0 0 0 02 2 2 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 02 3 2 1 0 0 0 0 0 0 | Master | 2 | 1,588 | 392 | 234 | 2 | |
1,358 | C | 1358C | C. Celex Update | 1,600 | math | During the quarantine, Sicromoft has more free time to create the new functions in ""Celex-2021"". The developers made a new function GAZ-GIZ, which infinitely fills an infinite table to the right and down from the upper left corner as follows: The cell with coordinates \((x, y)\) is at the intersection of \(x\)-th row... | The first line contains one integer \(t\) (\(1 \le t \le 57179\)) — the number of test cases.Each of the following \(t\) lines contains four natural numbers \(x_1\), \(y_1\), \(x_2\), \(y_2\) (\(1 \le x_1 \le x_2 \le 10^9\), \(1 \le y_1 \le y_2 \le 10^9\)) — coordinates of the start and the end cells. | For each test case, in a separate line, print the number of possible different sums on the way from the start cell to the end cell. | In the first test case there are two possible sums: \(1+2+5=8\) and \(1+3+5=9\). | Input: 4 1 1 2 2 1 2 2 4 179 1 179 100000 5 7 5 7 | Output: 2 3 1 1 | Medium | 1 | 1,366 | 302 | 131 | 13 |
926 | B | 926B | B. Add Points | 1,800 | math; number theory | There are n points on a straight line, and the i-th point among them is located at xi. All these coordinates are distinct.Determine the number m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. | The first line contains a single integer n (3 ≤ n ≤ 100 000) — the number of points.The second line contains a sequence of integers x1, x2, ..., xn ( - 109 ≤ xi ≤ 109) — the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order. | Print a single integer m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. | In the first example you can add one point with coordinate 0.In the second example the distances between all neighboring points are already equal, so you shouldn't add anything. | Input: 3-5 10 5 | Output: 1 | Medium | 2 | 263 | 283 | 143 | 9 |
329 | C | 329C | C. Graph Reconstruction | 2,400 | constructive algorithms | I have an undirected graph consisting of n nodes, numbered 1 through n. Each node has at most two incident edges. For each pair of nodes, there is at most an edge connecting them. No edge connects a node to itself.I would like to create a new graph in such a way that: The new graph consists of the same number of nodes ... | The first line consists of two space-separated integers: n and m (1 ≤ m ≤ n ≤ 105), denoting the number of nodes and edges, respectively. Then m lines follow. Each of the m lines consists of two space-separated integers u and v (1 ≤ u, v ≤ n; u ≠ v), denoting an edge between nodes u and v. | If it is not possible to construct a new graph with the mentioned properties, output a single line consisting of -1. Otherwise, output exactly m lines. Each line should contain a description of edge in the same way as used in the input format. | The old graph of the first example:A possible new graph for the first example:In the second example, we cannot create any new graph.The old graph of the third example:A possible new graph for the third example: | Input: 8 71 22 34 55 66 88 77 4 | Output: 1 44 61 62 77 58 52 8 | Expert | 1 | 595 | 290 | 243 | 3 |
1,621 | D | 1621D | D. The Winter Hike | 2,100 | constructive algorithms; greedy; math | Circular land is an \(2n \times 2n\) grid. Rows of this grid are numbered by integers from \(1\) to \(2n\) from top to bottom and columns of this grid are numbered by integers from \(1\) to \(2n\) from left to right. The cell \((x, y)\) is the cell on the intersection of row \(x\) and column \(y\) for \(1 \leq x \leq 2... | The first line contains a single integer \(t\) (\(1 \leq t \leq 100\)) — the number of test cases.The first line of each test case contains the single integer \(n\) (\(1 \leq n \leq 250\)).Each of the next \(2n\) lines contains \(2n\) integers \(c_{i, 1}, c_{i, 2}, \ldots, c_{i, 2n}\) (\(0 \leq c_{i, j} \leq 10^9\)) — ... | For each test case output one integer — the minimal number of coins you should spend. | In the first test case you can remove snow from the cells \((2, 1)\) and \((2, 2)\) for \(100\) coins. Then you can give instructions All friends in the first collum should move to the previous cell. After this, your friend will be in the cell \((2, 1)\). All friends in the second row should move to the next cell. Afte... | Input: 410 81 9920 0 0 00 0 0 09 9 2 29 9 9 920 0 4 20 0 2 44 2 4 22 4 2 440 0 0 0 0 0 0 20 0 0 0 0 0 2 00 0 0 0 0 2 0 00 0 0 0 2 0 0 00 0 0 2 2 0 2 20 0 2 0 1 6 2 10 2 0 0 2 4 7 42 0 0 0 2 0 1 6 | Output: 100 22 14 42 | Hard | 3 | 2,904 | 631 | 85 | 16 |
607 | A | 607A | A. Chain Reaction | 1,600 | binary search; dp | There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will acti... | The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons.The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj i... | Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added. | For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42. | Input: 41 93 16 17 4 | Output: 1 | Medium | 2 | 771 | 328 | 110 | 6 |
514 | C | 514C | C. Watto and Mechanism | 2,000 | binary search; data structures; hashing; string suffix structures; strings | Watto, the owner of a spare parts store, has recently got an order for the mechanism that can process strings in a certain way. Initially the memory of the mechanism is filled with n strings. Then the mechanism should be able to process queries of the following type: ""Given string s, determine if the memory of the mec... | The first line contains two non-negative numbers n and m (0 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105) — the number of the initial strings and the number of queries, respectively.Next follow n non-empty strings that are uploaded to the memory of the mechanism.Next follow m non-empty strings that are the queries to the mechanism.The t... | For each query print on a single line ""YES"" (without the quotes), if the memory of the mechanism contains the required string, otherwise print ""NO"" (without the quotes). | Input: 2 3aaaaaacacacaaabaaccacacccaaac | Output: YESNONO | Hard | 5 | 637 | 425 | 173 | 5 | |
1,189 | A | 1189A | A. Keanu Reeves | 800 | strings | After playing Neo in the legendary ""Matrix"" trilogy, Keanu Reeves started doubting himself: maybe we really live in virtual reality? To find if this is true, he needs to solve the following problem.Let's call a string consisting of only zeroes and ones good if it contains different numbers of zeroes and ones. For exa... | The first line of the input contains a single integer \(n\) (\(1\le n \le 100\)) — the length of the string \(s\).The second line contains the string \(s\) of length \(n\) consisting only from zeros and ones. | In the first line, output a single integer \(k\) (\(1\le k\)) — a minimal number of strings you have cut \(s\) into.In the second line, output \(k\) strings \(s_1, s_2, \ldots, s_k\) separated with spaces. The length of each string has to be positive. Their concatenation has to be equal to \(s\) and all of them have to... | In the first example, the string 1 wasn't cut at all. As it is good, the condition is satisfied.In the second example, 1 and 0 both are good. As 10 isn't good, the answer is indeed minimal.In the third example, 100 and 011 both are good. As 100011 isn't good, the answer is indeed minimal. | Input: 1 1 | Output: 1 1 | Beginner | 1 | 1,316 | 208 | 370 | 11 |
980 | F | 980F | F. Cactus to Tree | 2,900 | dp; graphs; trees | You are given a special connected undirected graph where each vertex belongs to at most one simple cycle.Your task is to remove as many edges as needed to convert this graph into a tree (connected graph with no cycles). For each node, independently, output the maximum distance between it and a leaf in the resulting tre... | The first line of input contains two integers \(n\) and \(m\) (\(1 \leq n \leq 5\cdot 10^5\)), the number of nodes and the number of edges, respectively.Each of the following \(m\) lines contains two integers \(u\) and \(v\) (\(1 \leq u,v \leq n\), \(u \ne v\)), and represents an edge connecting the two nodes \(u\) and... | Print \(n\) space-separated integers, the \(i\)-th integer represents the maximum distance between node \(i\) and a leaf if the removed edges were chosen in a way that minimizes this distance. | In the first sample, a possible way to minimize the maximum distance from vertex \(1\) is by removing the marked edges in the following image: Note that to minimize the answer for different nodes, you can remove different edges. | Input: 9 107 29 21 63 14 34 77 69 85 85 9 | Output: 5 3 5 4 5 4 3 5 4 | Master | 3 | 399 | 483 | 192 | 9 |
986 | C | 986C | C. AND Graph | 2,500 | bitmasks; dfs and similar; dsu; graphs | You are given a set of size \(m\) with integer elements between \(0\) and \(2^{n}-1\) inclusive. Let's build an undirected graph on these integers in the following way: connect two integers \(x\) and \(y\) with an edge if and only if \(x \& y = 0\). Here \(\&\) is the bitwise AND operation. Count the number of connecte... | In the first line of input there are two integers \(n\) and \(m\) (\(0 \le n \le 22\), \(1 \le m \le 2^{n}\)).In the second line there are \(m\) integers \(a_1, a_2, \ldots, a_m\) (\(0 \le a_{i} < 2^{n}\)) — the elements of the set. All \(a_{i}\) are distinct. | Print the number of connected components. | Graph from first sample:Graph from second sample: | Input: 2 31 2 3 | Output: 2 | Expert | 4 | 347 | 260 | 41 | 9 |
1,267 | H | 1267H | H. Help BerLine | 3,200 | constructive algorithms | Very soon, the new cell phone services provider ""BerLine"" will begin its work in Berland!The start of customer service is planned along the main street of the capital. There are \(n\) base stations that are already installed. They are located one after another along the main street in the order from the \(1\)-st to t... | The first line of the input contains an integer \(t\) (\(1 \le t \le 50\)) — the number of test cases in the input. Then \(t\) test case descriptions follow.The first line of a test case contains an integer \(n\) (\( 1 \le n \le 8\,500\)) — the number of ""BerLine"" base stations.The following line contains \(n\) disti... | Print exactly \(t\) lines, where the \(j\)-th line contains the answer for the \(j\)-th test case in the input. Print the required frequencies \(f_1, f_2, \dots, f_n\) (\(1 \le f_i \le 24\)). If there are several possible answers, print any of them. | In the first test case \(n = 3\) and \(p = [1, 3, 2]\). The base stations can be assigned frequencies \([1, 3, 2]\). Day 1: only the base station \(1\) is turned on, its frequency is \(1\). Day 2: the base stations \(1\) and \(3\) are turned on, their frequencies are \([1, 2]\). Day 3: all base stations are turned on, ... | Input: 5 3 1 3 2 3 1 2 3 1 1 10 6 10 4 2 7 9 5 8 3 1 10 2 4 6 9 1 8 10 5 3 7 | Output: 1 3 2 10 20 10 1 2 3 4 5 3 1 3 5 4 2 1 2 3 4 5 6 7 8 9 10 | Master | 1 | 2,105 | 591 | 249 | 12 |
774 | K | 774K | K. Stepan and Vowels | 1,600 | *special; implementation; strings | Stepan likes to repeat vowel letters when he writes words. For example, instead of the word ""pobeda"" he can write ""pobeeeedaaaaa"".Sergey does not like such behavior, so he wants to write a program to format the words written by Stepan. This program must combine all consecutive equal vowels to a single vowel. The vo... | The first line contains the integer n (1 ≤ n ≤ 100 000) — the number of letters in the word written by Stepan.The second line contains the string s which has length that equals to n and contains only lowercase English letters — the word written by Stepan. | Print the single string — the word written by Stepan converted according to the rules described in the statement. | Input: 13pobeeeedaaaaa | Output: pobeda | Medium | 3 | 736 | 255 | 113 | 7 | |
933 | D | 933D | D. A Creative Cutout | 2,900 | brute force; combinatorics; math | Everything red frightens Nian the monster. So do red paper and... you, red on Codeforces, potential or real.Big Banban has got a piece of paper with endless lattice points, where lattice points form squares with the same area. His most favorite closed shape is the circle because of its beauty and simplicity. Once he ha... | The first line contains one integer m (1 ≤ m ≤ 1012). | In the first line print one integer representing . | A piece of paper with 5 circles is shown in the following. There are 5 types of lattice points where the degree of beauty of each red point is 1 + 2 + 3 + 4 + 5 = 15, the degree of beauty of each orange point is 2 + 3 + 4 + 5 = 14, the degree of beauty of each green point is 4 + 5 = 9, the degree of beauty of each blue... | Input: 5 | Output: 387 | Master | 3 | 984 | 53 | 50 | 9 |
1,141 | A | 1141A | A. Game 23 | 1,000 | implementation; math | Polycarp plays ""Game 23"". Initially he has a number \(n\) and his goal is to transform it to \(m\). In one move, he can multiply \(n\) by \(2\) or multiply \(n\) by \(3\). He can perform any number of moves.Print the number of moves needed to transform \(n\) to \(m\). Print -1 if it is impossible to do so.It is easy ... | The only line of the input contains two integers \(n\) and \(m\) (\(1 \le n \le m \le 5\cdot10^8\)). | Print the number of moves to transform \(n\) to \(m\), or -1 if there is no solution. | In the first example, the possible sequence of moves is: \(120 \rightarrow 240 \rightarrow 720 \rightarrow 1440 \rightarrow 4320 \rightarrow 12960 \rightarrow 25920 \rightarrow 51840.\) The are \(7\) steps in total.In the second example, no moves are needed. Thus, the answer is \(0\).In the third example, it is impossi... | Input: 120 51840 | Output: 7 | Beginner | 2 | 471 | 100 | 85 | 11 |
1,605 | E | 1605E | E. Array Equalizer | 2,400 | binary search; greedy; implementation; math; number theory; sortings; two pointers | Jeevan has two arrays \(a\) and \(b\) of size \(n\). He is fond of performing weird operations on arrays. This time, he comes up with two types of operations: Choose any \(i\) (\(1 \le i \le n\)) and increment \(a_j\) by \(1\) for every \(j\) which is a multiple of \(i\) and \(1 \le j \le n\). Choose any \(i\) (\(1 \le... | The first line contains a single integer \(n\) \((1 \le n \le 2 \cdot 10^{5})\) — the size of arrays \(a\) and \(b\).The second line contains \(n\) integers \(a_1, a_2, ..., a_n\) \((1 \le a_i \le 10^6)\).The third line contains \(n\) integers \(b_1, b_2, ..., b_n\) \((1 \le b_i \le 10^6\) for \(i \neq 1\); \(b_1 = -1\... | Output \(q\) integers — the answers to each of his \(q\) questions. | Consider the first test case. \(b_1 = 1\): We need to convert \([3, 7] \rightarrow [1, 5]\). We can perform the following operations:\([3, 7]\) \(\xrightarrow[\text{decrease}]{\text{i = 1}}\) \([2, 6]\) \(\xrightarrow[\text{decrease}]{\text{i = 1}}\) \([1, 5]\)Hence the answer is \(2\). \(b_1 = 4\): We need to convert ... | Input: 2 3 7 -1 5 3 1 4 3 | Output: 2 4 2 | Expert | 7 | 854 | 615 | 67 | 16 |
1,367 | A | 1367A | A. Short Substrings | 800 | implementation; strings | Alice guesses the strings that Bob made for her.At first, Bob came up with the secret string \(a\) consisting of lowercase English letters. The string \(a\) has a length of \(2\) or more characters. Then, from string \(a\) he builds a new string \(b\) and offers Alice the string \(b\) so that she can guess the string \... | The first line contains a single positive integer \(t\) (\(1 \le t \le 1000\)) — the number of test cases in the test. Then \(t\) test cases follow.Each test case consists of one line in which the string \(b\) is written, consisting of lowercase English letters (\(2 \le |b| \le 100\)) — the string Bob came up with, whe... | Output \(t\) answers to test cases. Each answer is the secret string \(a\), consisting of lowercase English letters, that Bob came up with. | The first test case is explained in the statement.In the second test case, Bob came up with the string \(a\)=""ac"", the string \(a\) has a length \(2\), so the string \(b\) is equal to the string \(a\).In the third test case, Bob came up with the string \(a\)=""bcdaf"", substrings of length \(2\) of string \(a\) are: ... | Input: 4 abbaac ac bccddaaf zzzzzzzzzz | Output: abac ac bcdaf zzzzzz | Beginner | 2 | 939 | 443 | 139 | 13 |
1,223 | A | 1223A | A. CME | 800 | math | Let's denote correct match equation (we will denote it as CME) an equation \(a + b = c\) there all integers \(a\), \(b\) and \(c\) are greater than zero.For example, equations \(2 + 2 = 4\) (||+||=||||) and \(1 + 2 = 3\) (|+||=|||) are CME but equations \(1 + 2 = 4\) (|+||=||||), \(2 + 2 = 3\) (||+||=|||), and \(0 + 1 ... | The first line contains one integer \(q\) (\(1 \le q \le 100\)) — the number of queries.The only line of each query contains one integer \(n\) (\(2 \le n \le 10^9\)) — the number of matches. | For each test case print one integer in single line — the minimum number of matches which you have to buy for assembling CME. | The first and second queries are explained in the statement.In the third query, you can assemble \(1 + 3 = 4\) (|+|||=||||) without buying matches.In the fourth query, buy one match and assemble \(2 + 4 = 6\) (||+||||=||||||). | Input: 4 2 5 8 11 | Output: 2 1 0 1 | Beginner | 1 | 830 | 190 | 125 | 12 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.