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
571
C
571C
C. CNF 2
2,500
constructive algorithms; dfs and similar; graphs; greedy
'In Boolean logic, a formula is in conjunctive normal form (CNF) or clausal normal form if it is a conjunction of clauses, where a clause is a disjunction of literals' (cited from https://en.wikipedia.org/wiki/Conjunctive_normal_form)In the other words, CNF is a formula of type , where & represents a logical ""AND"" (c...
The first line contains integers n and m (1 ≤ n, m ≤ 2·105) — the number of clauses and the number variables, correspondingly.Next n lines contain the descriptions of each clause. The i-th line first contains first number ki (ki ≥ 1) — the number of literals in the i-th clauses. Then follow space-separated literals vij...
If CNF is not satisfiable, print a single line ""NO"" (without the quotes), otherwise print two strings: string ""YES"" (without the quotes), and then a string of m numbers zero or one — the values of variables in satisfying assignment in the order from x1 to xm.
In the first sample test formula is . One of possible answer is x1 = TRUE, x2 = TRUE.
Input: 2 22 1 -22 2 -1 | Output: YES11
Expert
4
1,004
454
263
5
420
D
420D
D. Cup Trick
2,200
data structures
The employees of the F company have lots of ways to entertain themselves. Today they invited a famous magician who shows a trick with plastic cups and a marble.The point is to trick the spectator's attention. Initially, the spectator stands in front of a line of n plastic cups. Then the magician places a small marble u...
The first line contains integers n and m (1 ≤ n, m ≤ 106). Each of the next m lines contains a couple of integers. The i-th line contains integers xi, yi (1 ≤ xi, yi ≤ n) — the description of the i-th operation of the magician. Note that the operations are given in the order in which the magician made them and the code...
If the described permutation doesn't exist (the programmer remembered wrong operations), print -1. Otherwise, print n distinct integers, each from 1 to n: the i-th number should represent the mark on the cup that initially is in the row in position i.If there are multiple correct answers, you should print the lexicogra...
Input: 2 12 1 | Output: 2 1
Hard
1
1,352
359
341
4
811
A
811A
A. Vladik and Courtesy
800
brute force; implementation
At regular competition Vladik and Valera won a and b candies respectively. Vladik offered 1 his candy to Valera. After that Valera gave Vladik 2 his candies, so that no one thought that he was less generous. Vladik for same reason gave 3 candies to Valera in next turn.More formally, the guys take turns giving each othe...
Single line of input data contains two space-separated integers a, b (1 ≤ a, b ≤ 109) — number of Vladik and Valera candies respectively.
Pring a single line ""Vladik’’ in case, if Vladik first who can’t give right amount of candy, or ""Valera’’ otherwise.
Illustration for first test case:Illustration for second test case:
Input: 1 1 | Output: Valera
Beginner
2
618
137
118
8
725
C
725C
C. Hidden Word
1,600
brute force; constructive algorithms; implementation; strings
Let’s define a grid to be a set of tiles with 2 rows and 13 columns. Each tile has an English letter written in it. The letters don't have to be unique: there might be two or more tiles with the same letter written on them. Here is an example of a grid: ABCDEFGHIJKLMNOPQRSTUVWXYZ We say that two tiles are adjacent if t...
The only line of the input contains the string s, consisting of 27 upper-case English letters. Each English letter occurs at least once in s.
Output two lines, each consisting of 13 upper-case English characters, representing the rows of the grid. If there are multiple solutions, print any of them. If there is no solution print ""Impossible"".
Input: ABCDEFGHIJKLMNOPQRSGTUVWXYZ | Output: YXWVUTGHIJKLMZABCDEFSRQPON
Medium
4
1,258
141
203
7
1,985
E
1985E
E. Secret Box
1,200
brute force; combinatorics; math
Ntarsis has a box \(B\) with side lengths \(x\), \(y\), and \(z\). It lies in the 3D coordinate plane, extending from \((0,0,0)\) to \((x,y,z)\). Ntarsis has a secret box \(S\). He wants to choose its dimensions such that all side lengths are positive integers, and the volume of \(S\) is \(k\). He can place \(S\) somew...
The first line consists of an integer \(t\), the number of test cases (\(1 \leq t \leq 2000\)). The description of the test cases follows.The first and only line of each test case contains four integers \(x, y, z\) and \(k\) (\(1 \leq x, y, z \leq 2000\), \(1 \leq k \leq x \cdot y \cdot z\)).It is guaranteed the sum of...
For each test case, output the answer as an integer on a new line. If there is no way to select the dimensions of \(S\) so it fits in \(B\), output \(0\).
For the first test case, it is optimal to choose \(S\) with side lengths \(2\), \(2\), and \(2\), which has a volume of \(2 \cdot 2 \cdot 2 = 8\). It can be shown there are \(8\) ways to put \(S\) inside \(B\).The coordinate with the least \(x\), \(y\), and \(z\) values for each possible arrangement of \(S\) are: \((0,...
Input: 73 3 3 83 3 3 185 1 1 12 2 2 73 4 2 124 3 1 61800 1800 1800 4913000000 | Output: 8 2 5 0 4 4 1030301
Easy
3
768
481
154
19
1,188
A2
1188A2
A2. Add on a Tree: Revolution
2,500
constructive algorithms; dfs and similar; implementation; trees
Note that this is the second problem of the two similar problems. You can hack this problem if you solve it. But you can hack the previous problem only if you solve both problems.You are given a tree with \(n\) nodes. In the beginning, \(0\) is written on all edges. In one operation, you can choose any \(2\) distinct l...
The first line contains a single integer \(n\) (\(2 \le n \le 1000\)) — the number of nodes in a tree.Each of the next \(n-1\) lines contains three integers \(u\), \(v\), \(val\) (\(1 \le u, v \le n\), \(u \neq v\), \(0 \le val \le 10\,000\)), meaning that there is an edge between nodes \(u\) and \(v\) with \(val\) wri...
If there aren't any sequences of operations which lead to the given configuration, output ""NO"".If it exists, output ""YES"" in the first line. In the second line output \(m\) — number of operations you are going to apply (\(0 \le m \le 10^5\)). Note that you don't have to minimize the number of the operations!In the ...
The configuration from the first sample is drawn below, and it is impossible to achieve. The sequence of operations from the second sample is illustrated below.
Input: 5 1 2 2 2 3 4 3 4 10 3 5 18 | Output: NO
Expert
4
1,217
453
737
11
609
E
609E
E. Minimum spanning tree for each edge
2,100
data structures; dfs and similar; dsu; graphs; trees
Connected undirected weighted graph without self-loops and multiple edges is given. Graph contains n vertices and m edges.For each edge (u, v) find the minimal possible weight of the spanning tree that contains the edge (u, v).The weight of the spanning tree is the sum of weights of all edges included in spanning tree.
First line contains two integers n and m (1 ≤ n ≤ 2·105, n - 1 ≤ m ≤ 2·105) — the number of vertices and edges in graph.Each of the next m lines contains three integers ui, vi, wi (1 ≤ ui, vi ≤ n, ui ≠ vi, 1 ≤ wi ≤ 109) — the endpoints of the i-th edge and its weight.
Print m lines. i-th line should contain the minimal possible weight of the spanning tree that contains i-th edge.The edges are numbered from 1 to m in order of their appearing in input.
Input: 5 71 2 31 3 11 4 52 3 22 5 33 4 24 5 4 | Output: 98118889
Hard
5
320
268
185
6
1,741
E
1741E
E. Sending a Sequence Over the Network
1,600
dp
The sequence \(a\) is sent over the network as follows: sequence \(a\) is split into segments (each element of the sequence belongs to exactly one segment, each segment is a group of consecutive elements of sequence); for each segment, its length is written next to it, either to the left of it or to the right of it; th...
The first line of input data contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.Each test case consists of two lines.The first line of the test case contains an integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) — the size of the sequence \(b\).The second line of test case contains \(n\) intege...
For each test case print on a separate line: YES if sequence \(b\) could be sent over the network, that is, if sequence \(b\) could be obtained from some sequence \(a\) to send \(a\) over the network. NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as p...
In the first case, the sequence \(b\) could be obtained from the sequence \(a = [1, 2, 3, 1, 2, 3]\) with the following partition: \([\color{red}{1}] + [\color{blue}{2, 3, 1}] + [\color{green}{2, 3}]\). The sequence \(b\): \([\color{red}{1}, 1, \color{blue}{2, 3, 1}, 3, 2, \color{green}{2, 3}]\).In the second case, the...
Input: 791 1 2 3 1 3 2 2 3512 1 2 7 565 7 8 9 10 344 8 6 223 1104 6 2 1 9 4 9 3 4 211 | Output: YES YES YES NO YES YES NO
Medium
1
1,220
493
338
17
2,018
E1
2018E1
E1. Complex Segments (Easy Version)
3,300
binary search; data structures; divide and conquer; dsu; greedy; math; sortings
Ken Arai - COMPLEX⠀This is the easy version of the problem. In this version, the constraints on \(n\) and the time limit are lower. You can make hacks only if both versions of the problem are solved.A set of (closed) segments is complex if it can be partitioned into some subsets such that all the subsets have the same ...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^3\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^4\)) — the number of segments.The second line of each test case cont...
For each test case, output a single integer: the maximum size of a complex subset of the given segments.
In the first test case, all pairs of segments intersect, therefore it is optimal to form a single group containing all of the three segments.In the second test case, there is no valid partition for all of the five segments. A valid partition with four segments is the following: \(\{\{ [1, 5], [2, 4] \}, \{ [6, 9], [8, ...
Input: 331 2 35 4 651 2 3 6 85 4 7 9 1053 1 4 1 57 2 6 5 10 | Output: 3 4 4
Master
7
553
665
104
20
342
E
342E
E. Xenia and Tree
2,400
data structures; divide and conquer; trees
Xenia the programmer has a tree consisting of n nodes. We will consider the tree nodes indexed from 1 to n. We will also consider the first node to be initially painted red, and the other nodes — to be painted blue.The distance between two tree nodes v and u is the number of edges in the shortest path between v and u.X...
The first line contains two integers n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of nodes in the tree and the number of queries. Next n - 1 lines contain the tree edges, the i-th line contains a pair of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — an edge of the tree.Next m lines contain queries. Each query is speci...
For each second type query print the reply in a single line.
Input: 5 41 22 32 44 52 12 51 22 5 | Output: 032
Expert
3
608
650
60
3
2,107
E
2107E
E. Ain and Apple Tree
2,600
binary search; constructive algorithms; greedy; math; trees
If I was also hit by an apple falling from an apple tree, could I become as good at physics as Newton?To be better at physics, Ain wants to build an apple tree so that she can get hit by apples on it. Her apple tree has \(n\) nodes and is rooted at \(1\). She defines the weight of an apple tree as \(\sum \limits_{i=1}^...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains two numbers \(n,k\) (\(2 \le n \le 10^5,0 \le k \le 10^{15}\)).It is guaranteed that the sum of \(n\) over all test ...
For each test case, first output \(\texttt{Yes}\) if a solution exists or \(\texttt{No}\) if no solution exists. You may print each character in either case, for example \(\texttt{YES}\) and \(\texttt{yEs}\) will also be accepted.If there's at least one solution, print \(n-1\) lines and each line contains two numbers \...
In the first test case, we can check that the weight is \(0\). This satisfies the condition because \(k = 1\) and so the absolute difference is only \(1\).In the second test case, there exists no solution because there are no trees of \(2\) nodes with weights of either \(1, 2\) or \(3\).
Input: 52 12 24 05 75 5 | Output: Yes 1 2 No Yes 1 2 1 3 1 4 Yes 1 3 3 5 4 5 3 2 Yes 1 2 2 3 2 4 2 5
Expert
5
1,076
359
375
21
1,935
E
1935E
E. Distance Learning Courses in MAC
2,400
bitmasks; brute force; data structures; greedy; math
The New Year has arrived in the Master's Assistance Center, which means it's time to introduce a new feature!Now students are given distance learning courses, with a total of \(n\) courses available. For the \(i\)-th distance learning course, a student can receive a grade ranging from \(x_i\) to \(y_i\).However, not al...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 2 \cdot 10^4\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) — the number of distance learni...
For each test case, output \(q\) integers, where the \(j\)-th integer is the maximum final grade that the \(j\)-th student can achieve.
In the first test case: The maximum grade for the first student is \(1\): On the first distance learning course, he will receive a grade of \(1\). Therefore, the final grade is \(1\). The maximum grade for the second student is \(5\): On the first distance learning course, he will receive a grade of \(1\). On the secon...
Input: 320 13 431 11 22 241 71 73 102 251 33 42 31 41 261 22 20 11 13 30 043 45 52 51 2 | Output: 1 5 4 15 11 15 15 7 1 3 3 3
Expert
5
1,109
928
135
19
727
F
727F
F. Polycarp's problems
2,300
binary search; dp; greedy
Polycarp is an experienced participant in Codehorses programming contests. Now he wants to become a problemsetter.He sent to the coordinator a set of n problems. Each problem has it's quality, the quality of the i-th problem is ai (ai can be positive, negative or equal to zero). The problems are ordered by expected dif...
The first line of input contains two integers n and m (1 ≤ n ≤ 750, 1 ≤ m ≤ 200 000) — the number of problems in the problemset and the number of guesses about the current coordinator's mood.The second line of input contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the qualities of the problems in order of incr...
Print m lines, in i-th line print single integer — the answer to the problem with q = bi.
Input: 6 38 -5 -4 1 -7 40 7 3 | Output: 201
Expert
3
1,418
464
89
7
733
D
733D
D. Kostya the Sculptor
1,600
data structures; hashing
Kostya is a genial sculptor, he has an idea: to carve a marble sculpture in the shape of a sphere. Kostya has a friend Zahar who works at a career. Zahar knows about Kostya's idea and wants to present him a rectangular parallelepiped of marble from which he can carve the sphere. Zahar has n stones which are rectangular...
The first line contains the integer n (1 ≤ n ≤ 105).n lines follow, in the i-th of which there are three integers ai, bi and ci (1 ≤ ai, bi, ci ≤ 109) — the lengths of edges of the i-th stone. Note, that two stones may have exactly the same sizes, but they still will be considered two different stones.
In the first line print k (1 ≤ k ≤ 2) the number of stones which Zahar has chosen. In the second line print k distinct integers from 1 to n — the numbers of stones which Zahar needs to choose. Consider that stones are numbered from 1 to n in the order as they are given in the input data.You can print the stones in arbi...
In the first example we can connect the pairs of stones: 2 and 4, the size of the parallelepiped: 3 × 2 × 5, the radius of the inscribed sphere 1 2 and 5, the size of the parallelepiped: 3 × 2 × 8 or 6 × 2 × 4 or 3 × 4 × 4, the radius of the inscribed sphere 1, or 1, or 1.5 respectively. 2 and 6, the size of the parall...
Input: 65 5 53 2 41 4 12 1 33 2 43 3 4 | Output: 11
Medium
2
929
303
380
7
1,627
E
1627E
E. Not Escaping
2,200
data structures; dp; implementation; shortest paths; two pointers
Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.The building consists of \(n\) floors, each with \(m\) rooms each...
The first line of input contains \(t\) (\(1 \leq t \leq 5 \cdot 10^4\)) — the number of test cases.The first line of each test case consists of \(3\) integers \(n, m, k\) (\(2 \leq n, m \leq 10^5\); \(1 \leq k \leq 10^5\)) — the number of floors, the number of rooms on each floor and the number of ladders respectively....
Output the minimum health Ram loses on the optimal path from \((1, 1)\) to \((n, m)\). If Ram cannot escape the clutches of Raghav regardless of the path he takes, output ""NO ESCAPE"" (all uppercase, without quotes).
The figure for the first test case is in the statement. There are only \(2\) possible paths to \((n, m)\): Ram travels to \((1, 3)\), takes the ladder to \((3, 3)\), travels to \((3, 2)\), takes the ladder to \((5, 1)\), travels to \((5, 3)\) where he finally escapes via helicopter. The health lost would be $$$\( \begi...
Input: 45 3 35 17 8 1 41 3 3 3 43 1 5 2 53 2 5 1 66 3 35 17 8 1 4 21 3 3 3 43 1 5 2 53 2 5 1 65 3 15 17 8 1 41 3 5 3 1005 5 53 2 3 7 53 5 4 2 12 2 5 4 54 4 5 2 31 2 4 2 23 3 5 2 4 | Output: 16 NO ESCAPE -90 27
Hard
5
1,197
894
217
16
2,026
F
2026F
F. Bermart Ice Cream
2,700
data structures; dfs and similar; divide and conquer; dp; implementation; trees
In the Bermart chain of stores, a variety of ice cream is sold. Each type of ice cream has two parameters: price and tastiness.Initially, there is one store numbered \(1\), which sells nothing. You have to process \(q\) queries of the following types: \(1~x\) — a new store opens, that sells the same types of ice cream ...
The first line contains a single integer \(q\) (\(1 \le q \le 3 \cdot 10^4\)) — the number of queries.Each of the following \(q\) lines contains a query in the format described in the statement: \(1~x\); \(2~x~p~t\) (\(1 \le p, t \le 2000\)); \(3~x\); \(4~x~p\) (\(1 \le p \le 2000\)). Additional constraints on the inpu...
For each query of type \(4\), output a single integer — for store \(x\), find the maximum total tastiness of a subset of types of ice cream that are sold there, such that the total price does not exceed \(p\) (each type can be used in the subset no more than once).
Input: 122 1 5 72 1 3 44 1 44 1 84 1 21 12 2 4 104 1 94 2 93 14 1 94 2 9 | Output: 4 11 0 11 17 4 17
Master
6
908
566
265
20
1,598
E
1598E
E. Staircases
2,100
brute force; combinatorics; data structures; dfs and similar; dp; implementation; math
You are given a matrix, consisting of \(n\) rows and \(m\) columns. The rows are numbered top to bottom, the columns are numbered left to right.Each cell of the matrix can be either free or locked.Let's call a path in the matrix a staircase if it: starts and ends in the free cell; visits only free cells; has one of the...
The first line contains three integers \(n\), \(m\) and \(q\) (\(1 \le n, m \le 1000\); \(1 \le q \le 10^4\)) — the sizes of the matrix and the number of queries.Each of the next \(q\) lines contains two integers \(x\) and \(y\) (\(1 \le x \le n\); \(1 \le y \le m\)) — the description of each query.
Print \(q\) integers — the \(i\)-th value should be equal to the number of different staircases after \(i\) queries. Two staircases are considered different if there exists such a cell that appears in one path and doesn't appear in the other path.
Input: 2 2 8 1 1 1 1 1 1 2 2 1 1 1 2 2 1 1 1 | Output: 5 10 5 2 5 3 1 0
Hard
7
1,265
300
247
15
1,801
G
1801G
G. A task for substrings
3,400
data structures; string suffix structures; strings
Philip is very fond of tasks on the lines. He had already solved all the problems known to him, but this was not enough for him. Therefore, Philip decided to come up with his own task.To do this, he took the string \(t\) and a set of \(n\) strings \(s_1\), \(s_2\), \(s_3\), ..., \(s_n\). Philip has \(m\) queries, in th...
The first line contains two positive integers \(n\) and \(m\) (\(1 \le n, m \le 500\,000\)) — the number of rows in the set and the number of queries.The second line contains a single string \(t\) consisting of lowercase letters of the English alphabet (\(1 \le |t| \le 5 \cdot 10^6\)).The following \(n\) lines describe...
In a single line, print \(m\) integers, \(i\)th of them should be equal to the answers to the \(i\)th query.
In the first example, the first query requires the entire string to count the number of substrings that are included in the set. The substrings \([1, 3]\) and \([4, 6]\) coincide with the string ""aba"". The substrings match with the string ""a"" \([1, 1]\), \([3, 3]\), \([5, 5]\), \([7, 7]\). The substring \([3, 4]\) ...
Input: 3 5 abacaba aba a ac 1 7 1 3 2 7 2 5 4 5 | Output: 7 3 5 3 1
Master
3
1,049
838
108
18
1,548
D2
1548D2
D2. Gregor and the Odd Cows (Hard)
3,300
brute force; geometry; math; number theory
This is the hard version of the problem. The only difference from the easy version is that in this version the coordinates can be both odd and even.There are \(n\) fence-posts at distinct coordinates on a plane. It is guaranteed that no three fence posts lie on the same line.There are an infinite number of cows on the ...
The first line contains the integer \(n\) (\(3 \le n \le 6000\)), the number of fence posts which Gregor can choose to form the vertices of a fence.Each of the next \(n\) line contains two integers \(x\) and \(y\) (\(0 \le x,y \le 10^7\), where \((x,y)\) is the coordinate of a fence post. All fence posts lie at distinc...
Print a single integer, the number of interesting fences. Two fences are considered different if they are constructed with a different set of three fence posts.
In the first example, there is only \(1\) fence. That fence is interesting since its area is \(4\) and there is \(1\) enclosed cow, marked in red. In the second example, there are \(4\) possible fences. Only one of them is interesting however. That fence has an area of \(8\) and \(5\) enclosed cows.
Input: 3 0 0 2 0 0 4 | Output: 1
Master
4
711
377
160
15
1,032
E
1032E
E. The Unbearable Lightness of Weights
2,100
dp; math
You have a set of \(n\) weights. You know that their masses are \(a_1\), \(a_2\), ..., \(a_n\) grams, but you don't know which of them has which mass. You can't distinguish the weights.However, your friend does know the mass of each weight. You can ask your friend to give you exactly \(k\) weights with the total mass \...
The first line contains a single integer \(n\) (\(1 \le n \le 100\)) — the number of weights.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 100\)) — the masses of the weights.
Print the maximum number of weights you can learn the masses for after making a single query.
In the first example we can ask for a subset of two weights with total mass being equal to \(4\), and the only option is to get \(\{2, 2\}\).Another way to obtain the same result is to ask for a subset of two weights with the total mass of \(5\) and get \(\{1, 4\}\). It is easy to see that the two remaining weights hav...
Input: 4 1 4 2 2 | Output: 2
Hard
2
577
211
93
10
1,611
B
1611B
B. Team Composition: Programmers and Mathematicians
800
binary search; constructive algorithms; math
The All-Berland Team Programming Contest will take place very soon. This year, teams of four are allowed to participate.There are \(a\) programmers and \(b\) mathematicians at Berland State University. How many maximum teams can be made if: each team must consist of exactly \(4\) students, teams of \(4\) mathematicians...
The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) —the number of test cases.This is followed by descriptions of \(t\) sets, one per line. Each set is given by two integers \(a\) and \(b\) (\(0 \le a,b \le 10^9\)).
Print \(t\) lines. Each line must contain the answer to the corresponding set of input data — the required maximum number of teams.
In the first test case of the example, two teams can be composed. One way to compose two teams is to compose two teams of \(2\) programmers and \(2\) mathematicians.In the second test case of the example, only one team can be composed: \(3\) programmers and \(1\) mathematician in the team.
Input: 6 5 5 10 1 2 3 0 0 17 2 1000000000 1000000000 | Output: 2 1 1 0 2 500000000
Beginner
3
602
227
131
16
548
B
548B
B. Mike and Fun
1,400
brute force; dp; greedy; implementation
Mike and some bears are playing a game just for fun. Mike is the judge. All bears except Mike are standing in an n × m grid, there's exactly one bear in each cell. We denote the bear standing in column number j of row number i by (i, j). Mike's hands are on his ears (since he's the judge) and each bear standing in the ...
The first line of input contains three integers n, m and q (1 ≤ n, m ≤ 500 and 1 ≤ q ≤ 5000).The next n lines contain the grid description. There are m integers separated by spaces in each line. Each of these numbers is either 0 (for mouth) or 1 (for eyes).The next q lines contain the information about the rounds. Each...
After each round, print the current score of the bears.
Input: 5 4 50 1 1 01 0 0 10 1 1 01 0 0 10 0 0 01 11 41 14 24 3 | Output: 34334
Easy
4
914
454
55
5
543
B
543B
B. Destroying Roads
2,100
constructive algorithms; graphs; shortest paths
In some country there are exactly n cities and m bidirectional roads connecting the cities. Cities are numbered with integers from 1 to n. If cities a and b are connected by a road, then in an hour you can go along this road either from city a to city b, or from city b to city a. The road network is such that from any ...
The first line contains two integers n, m (1 ≤ n ≤ 3000, ) — the number of cities and roads in the country, respectively. Next m lines contain the descriptions of the roads as pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi). It is guaranteed that the roads that are given in the description can transport you from any...
Print a single number — the answer to the problem. If the it is impossible to meet the conditions, print -1.
Input: 5 41 22 33 44 51 3 23 5 2 | Output: 0
Hard
3
761
538
108
5
2,023
A
2023A
A. Concatenation of Arrays
1,300
constructive algorithms; greedy; math; sortings
You are given \(n\) arrays \(a_1\), \(\ldots\), \(a_n\). The length of each array is two. Thus, \(a_i = [a_{i, 1}, a_{i, 2}]\). You need to concatenate the arrays into a single array of length \(2n\) such that the number of inversions\(^{\dagger}\) in the resulting array is minimized. Note that you do not need to count...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)) — the number of arrays.Each of the following \(n\...
For each test case, output \(2n\) integers — the elements of the array you obtained. If there are multiple solutions, output any of them.
In the first test case, we concatenated the arrays in the order \(2, 1\). Let's consider the inversions in the resulting array \(b = [2, 3, 1, 4]\): \(i = 1\), \(j = 3\), since \(b_1 = 2 > 1 = b_3\); \(i = 2\), \(j = 3\), since \(b_2 = 3 > 1 = b_3\). Thus, the number of inversions is \(2\). It can be proven that this i...
Input: 421 42 333 24 32 155 102 39 64 18 7110 20 | Output: 2 3 1 4 2 1 3 2 4 3 4 1 2 3 5 10 8 7 9 6 10 20
Easy
4
1,065
528
137
20
1,356
A5
1356A5
A5. Distinguish Z from -Z
0
*special
You are given an operation that implements a single-qubit unitary transformation: either the Z gate or the -Z gate (i.e., the \(-|0\rangle\langle0| + |1\rangle\langle1|\) gate: \((-Z)(\alpha|0\rangle + \beta|1\rangle) = -\alpha|0\rangle + \beta|1\rangle\)). The operation will have Adjoint and Controlled variants define...
Beginner
1
870
0
0
13
1,637
B
1637B
B. MEX and Array
1,100
brute force; dp; greedy; math
Let there be an array \(b_1, b_2, \ldots, b_k\). Let there be a partition of this array into segments \([l_1; r_1], [l_2; r_2], \ldots, [l_c; r_c]\), where \(l_1 = 1\), \(r_c = k\), and for any \(2 \leq i \leq c\) holds that \(r_{i-1} + 1 = l_i\). In other words, each element of the array belongs to exactly one segment...
The input contains several test cases. The first line contains one integer \(t\) (\(1 \leq t \leq 30\)) — the number of test cases.The first line for each test case contains one integer \(n\) (\(1 \leq n \leq 100\)) — the length of the array.The second line contains a sequence of integers \(a_1, a_2, \ldots, a_n\) (\(0...
For each test case print a single integer — the answer to the problem.
In the second test case: The best partition for the subsegment \([2, 0, 1]\): \([2], [0, 1]\). The cost of this partition equals to \(2 + \operatorname{mex}(\{2\}) + \operatorname{mex}(\{0, 1\}) = 2 + 0 + 2 = 4\). The best partition for the subsegment \([2, 0]\): \([2], [0]\). The cost of this partition equals to \(2 +...
Input: 421 232 0 142 0 5 150 1 1 0 1 | Output: 4 14 26 48
Easy
4
1,122
458
70
16
309
B
309B
B. Context Advertising
2,100
dp; two pointers
Advertising has become part of our routine. And now, in the era of progressive technologies, we need your ideas to make advertising better!In this problem we'll look at a simplified version of context advertising. You've got a text, consisting of exactly n words. A standard advertising banner has exactly r lines, each ...
The first input line contains three integers n, r, c (1 ≤ n, r, c ≤ 106; r × c ≤ 106). The next line contains a text, consisting of n words. The words consist only of lowercase English letters and are not empty. The words in the lines are separated by single spaces. The total number of characters in all words doesn't e...
Print at most r lines, in each line print at most c characters — the optimal advertisement banner. If there are multiple advertisement banners, print any of them. Note that some lines of the banner can be empty. You are allowed not to print such lines.
Input: 9 4 12this is a sample text for croc final round | Output: this is asample textfor crocfinal round
Hard
2
1,400
332
252
3
1,017
C
1017C
C. The Phone Number
1,600
constructive algorithms; greedy
Mrs. Smith is trying to contact her husband, John Smith, but she forgot the secret phone number!The only thing Mrs. Smith remembered was that any permutation of \(n\) can be a secret phone number. Only those permutations that minimize secret value might be the phone of her husband.The sequence of \(n\) integers is call...
The only line contains one integer \(n\) (\(1 \le n \le 10^5\)) — the length of permutation that you need to build.
Print a permutation that gives a minimum sum of lengths of LIS and LDS. If there are multiple answers, print any.
In the first sample, you can build a permutation \([3, 4, 1, 2]\). LIS is \([3, 4]\) (or \([1, 2]\)), so the length of LIS is equal to \(2\). LDS can be ony of \([3, 1]\), \([4, 2]\), \([3, 2]\), or \([4, 1]\). The length of LDS is also equal to \(2\). The sum is equal to \(4\). Note that \([3, 4, 1, 2]\) is not the on...
Input: 4 | Output: 3 4 1 2
Medium
2
1,354
115
113
10
1,496
A
1496A
A. Split it!
900
brute force; constructive algorithms; greedy; strings
Kawashiro Nitori is a girl who loves competitive programming.One day she found a string and an integer. As an advanced problem setter, she quickly thought of a problem.Given a string \(s\) and a parameter \(k\), you need to check if there exist \(k+1\) non-empty strings \(a_1,a_2...,a_{k+1}\), such that $$$\(s=a_1+a_2+...
The input consists of multiple test cases. The first line 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 description contains two integers \(n\), \(k\) (\(1\le n\le 100\), \(0\le k\le \lfloor \frac{n}{2} \rfloor\...
For each test case, print ""YES"" (without quotes), if it is possible to find \(a_1,a_2,\ldots,a_{k+1}\), and ""NO"" (without quotes) otherwise.You can print letters in any case (upper or lower).
In the first test case, one possible solution is \(a_1=qw\) and \(a_2=q\).In the third test case, one possible solution is \(a_1=i\) and \(a_2=o\).In the fifth test case, one possible solution is \(a_1=dokidokiliteratureclub\).
Input: 7 5 1 qwqwq 2 1 ab 3 1 ioi 4 2 icpc 22 0 dokidokiliteratureclub 19 8 imteamshanghaialice 6 3 aaaaaa | Output: YES NO YES NO YES NO NO
Beginner
4
576
514
195
14
1,275
E3
1275E3
E3. Контрольная сумма
0
*special
Данные пользователей ВКонтакте хранятся на десятках тысяч серверов. Для того, чтобы можно было определять ошибки при записи данных на диск, на диск регулярно записываются текущие контрольные суммы CRC32 (Wiki, IEEE 802-3). Благодаря этому, при чтении данных можно заново вычислить контрольную сумму и проверить, что данн...
В первой строке дано два целых числа \(n\) и \(q\) — количество байт в файле и количество запросов, для которых нужно решить задачу (\(8 \le n \le 2 \cdot 10^5\); \(1 \le q \le 10^5\)).Во второй строке дано \(n\) чисел \(a_0, a_1, \ldots, a_{n-1}\) — содержимое файла в байтах (\(0 \le a_i \le 255\)).В следующих \(q\) с...
Для каждого запроса выведите четыре целых числа \(z_0, z_1, z_2, z_3\), на которые нужно заменить четыре байта с номерами \(j, j+1, j+2, j+3\), чтобы crc32 не изменился. Обратите внимание, что все запросы независимы, и на самом деле последовательность не изменяется.Если существует несколько решений, выведите любое, а е...
CRC32 байтовой последовательности из первого примера (1 2 3 4 5 6 7 8) равен 3fca88c5, CRC32 измененной последовательности (0 0 0 0 212 34 127 159) так же равен 3fca88c5. Стандартная утилита crc32 из большинства дистрибутивов линукса должна посчитать от них данную контрольную сумму.CRC32 последовательности из второго п...
Input: 8 1 1 2 3 4 5 6 7 8 0 4 0 0 0 0 | Output: 212 34 127 159
Beginner
1
1,287
673
388
12
361
B
361B
B. Levko and Permutation
1,200
constructive algorithms; math; number theory
Levko loves permutations very much. A permutation of length n is a sequence of distinct positive integers, each is at most n.Let’s assume that value gcd(a, b) shows the greatest common divisor of numbers a and b. Levko assumes that element pi of permutation p1, p2, ... , pn is good if gcd(i, pi) > 1. Levko considers a ...
The single line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n).
In a single line print either any beautiful permutation or -1, if such permutation doesn’t exist.If there are multiple suitable permutations, you are allowed to print any of them.
In the first sample elements 4 and 3 are good because gcd(2, 4) = 2 > 1 and gcd(3, 3) = 3 > 1. Elements 2 and 1 are not good because gcd(1, 2) = 1 and gcd(4, 1) = 1. As there are exactly 2 good elements, the permutation is beautiful.The second sample has no beautiful permutations.
Input: 4 2 | Output: 2 4 3 1
Easy
3
490
71
179
3
892
A
892A
A. Greed
900
greedy; implementation
Jafar has n cans of cola. Each can is described by two integers: remaining volume of cola ai and can's capacity bi (ai ≤ bi).Jafar has decided to pour all remaining cola into just 2 cans, determine if he can do this or not!
The first line of the input contains one integer n (2 ≤ n ≤ 100 000) — number of cola cans.The second line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 109) — volume of remaining cola in cans.The third line contains n space-separated integers that b1, b2, ..., bn (ai ≤ bi ≤ 109) — capacities of the can...
Print ""YES"" (without quotes) if it is possible to pour all remaining cola in 2 cans. Otherwise print ""NO"" (without quotes).You can print each letter in any case (upper or lower).
In the first sample, there are already 2 cans, so the answer is ""YES"".
Input: 23 53 6 | Output: YES
Beginner
2
223
322
182
8
1,677
B
1677B
B. Tokitsukaze and Meeting
1,700
data structures; implementation; math
Tokitsukaze is arranging a meeting. There are \(n\) rows and \(m\) columns of seats in the meeting hall.There are exactly \(n \cdot m\) students attending the meeting, including several naughty students and several serious students. The students are numerated from \(1\) to \(n\cdot m\). The students will enter the meet...
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 two integers \(n\), \(m\) (\(1 \leq n,m \leq 10^6\); \(1 \leq n \cdot m \leq 10^6\)), denoting there are \(n\) rows and \(m\) columns of seats in the meeting hall.The sec...
For each test case, print a single line with \(n \cdot m\) integers — the number of good rows and columns just after the \(i\)-th student enters the meeting hall.
The first test case is shown in the statement.After the \(1\)-st student enters the meeting hall, there are \(2\) good rows and columns: the \(1\)-st row and the \(1\)-st column.After the \(2\)-nd student enters the meeting hall, there are \(3\) good rows and columns: the \(1\)-st row, the \(1\)-st column and the \(2\)...
Input: 32 211004 2110011012 411001101 | Output: 2 3 4 3 2 3 4 3 5 4 6 5 2 3 3 3 4 4 4 5
Medium
3
1,354
668
162
16
1,901
E
1901E
E. Compressed Tree
2,200
dfs and similar; dp; graphs; greedy; sortings; trees
You are given a tree consisting of \(n\) vertices. A number is written on each vertex; the number on vertex \(i\) is equal to \(a_i\).You can perform the following operation any number of times (possibly zero): choose a vertex which has at most \(1\) incident edge and remove this vertex from the tree. Note that you can...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 5 \cdot 10^5\)) — the number of vertices.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(-10^9 \le a_i \le 10^9\)).Each of ...
For each test case, print a single integer — the maximum possible sum of numbers written on vertices after applying the aforementioned operation any number of times, and then compressing the tree.
Input: 341 -2 2 11 23 22 42-2 -52 17-2 4 -2 3 3 2 -11 22 33 43 54 64 7 | Output: 3 0 9
Hard
6
930
645
196
19
855
D
855D
D. Rowena Ravenclaw's Diadem
2,500
trees
Harry, upon inquiring Helena Ravenclaw's ghost, came to know that she told Tom Riddle or You-know-who about Rowena Ravenclaw's diadem and that he stole it from her. Harry thought that Riddle would have assumed that he was the only one to discover the Room of Requirement and thus, would have hidden it there. So Harry is...
First line of input contains the number n (1 ≤ n ≤ 105), the number of objects. Next n lines contain two integer parenti and typei ( - 1 ≤ parenti < i parenti ≠ 0, - 1 ≤ typei ≤ 1), implying that the i-th object has the parent parenti. (If typei = 0, this implies that the object i is a special case of object parenti. I...
Output will contain q lines, each containing the answer for the corresponding query as ""YES"" (affirmative) or ""NO"" (without quotes).You can output each letter in any case (upper or lower).
In test case 1, as object 2 is a special case of object 1 and object 3 is a special case of object 2, this makes object 3 a special case of object 1.In test case 2, as object 2 is a special case of object 1 and object 1 has object 3, this will mean that object 2 will also have object 3. This is because when a general c...
Input: 3-1 -11 02 021 1 32 1 3 | Output: YESNO
Expert
1
1,607
651
192
8
137
A
137A
A. Postcards and photos
900
implementation
Polycarpus has postcards and photos hung in a row on the wall. He decided to put them away to the closet and hang on the wall a famous painter's picture. Polycarpus does it like that: he goes from the left to the right and removes the objects consecutively. As Polycarpus doesn't want any mix-ups to happen, he will not ...
The only line of the input data contains a non-empty string consisting of letters ""С"" and ""P"" whose length does not exceed 100 characters. If the i-th character in the string is the letter ""С"", that means that the i-th object (the numbering goes from the left to the right) on Polycarpus' wall is a postcard. And i...
Print the only number — the minimum number of times Polycarpus has to visit the closet.
In the first sample Polycarpus needs to take one item to the closet 7 times.In the second sample Polycarpus can first take 3 postcards to the closet; then 3 more. He can take the 6 photos that are left in the similar way, going to the closet twice.In the third sample Polycarpus can visit the closet twice, both times ca...
Input: CPCPCPC | Output: 7
Beginner
1
722
406
87
1
1,866
G
1866G
G. Grouped Carriages
2,100
binary search; data structures; dp; flows; greedy
Pak Chanek observes that the carriages of a train is always full on morning departure hours and afternoon departure hours. Therefore, the balance between carriages is needed so that it is not too crowded in only a few carriages.A train contains \(N\) carriages that are numbered from \(1\) to \(N\) from left to right. C...
The first line contains a single integer \(N\) (\(1 \leq N \leq 2\cdot10^5\)) — the number of carriages.The second line contains \(N\) integers \(A_1, A_2, A_3, \ldots, A_N\) (\(0 \leq A_i \leq 10^9\)) — the initial number of passengers in each carriage.The third line contains \(N\) integers \(D_1, D_2, D_3, \ldots, D_...
An integer representing the minimum possible value of \(Z\).
One strategy that is optimal is as follows: \(5\) people in carriage \(1\) move to carriage \(4\) (going through \(3\) doors). \(3\) people in carriage \(5\) move to carriage \(3\) (going through \(2\) doors). \(2\) people in carriage \(6\) move to carriage \(5\) (going through \(1\) door). \(1\) person in carriage \(6...
Input: 7 7 4 2 0 5 8 3 4 0 0 1 3 1 3 | Output: 5
Hard
5
856
420
60
18
1,909
H
1909H
H. Parallel Swaps Sort
3,500
constructive algorithms; data structures
Dubmood - Keygen 8⠀You are given a permutation \(p_1, p_2, \dots, p_n\) of \([1, 2, \dots, n]\). You can perform the following operation some (possibly \(0\)) times: choose a subarray \([l, r]\) of even length; swap \(a_l\), \(a_{l+1}\); swap \(a_{l+2}\), \(a_{l+3}\) (if \(l+3 \leq r\)); \(\dots\) swap \(a_{r-1}\), \(a...
The first line contains a single integer \(n\) (\(2 \le n \le 3 \cdot 10^5\)) — the length of the permutation.The second line contains \(n\) integers \(p_1, p_2, \ldots, p_n\) (\(1 \le p_i \le n\), the \(p_i\) are distinct) — the permutation before performing the operations.
Output your operations in the following format.The first line should contain an integer \(k\) (\(0 \le k \le 10^6\)) — the number of operations.The next \(k\) lines represent the \(k\) operations in order. Each of these \(k\) lines should contain two integers \(l\) and \(r\) (\(1 \leq l < r \leq n\), \(r-l+1\) must be ...
In the first test: At the beginning, \(p = [2, 5, 4, 1, 3]\). In the first operation, you can choose \([l, r] = [1, 4]\). Then, \((a_1, a_2)\) are swapped and \((a_3, a_4)\) are swapped. The new permutation is \(p = [5, 2, 1, 4, 3]\). In the second operation, you can choose \([l, r] = [1, 2]\). Then, \((a_1, a_2)\) are...
Input: 52 5 4 1 3 | Output: 5 1 4 1 2 2 5 1 4 4 5
Master
2
432
275
550
19
804
C
804C
C. Ice cream coloring
2,200
constructive algorithms; dfs and similar; greedy
Isart and Modsart were trying to solve an interesting problem when suddenly Kasra arrived. Breathless, he asked: ""Can you solve a problem I'm stuck at all day?""We have a tree T with n vertices and m types of ice cream numerated from 1 to m. Each vertex i has a set of si types of ice cream. Vertices which have the i-t...
The first line contains two integer n and m (1 ≤ n, m ≤ 3·105) — the number of vertices in T and the number of ice cream types.n lines follow, the i-th of these lines contain single integer si (0 ≤ si ≤ 3·105) and then si distinct integers, each between 1 and m — the types of ice cream in the i-th vertex. The sum of si...
Print single integer c in the first line — the minimum number of colors to paint the vertices in graph G.In the second line print m integers, the i-th of which should be the color of the i-th vertex. The colors should be between 1 and c. If there are some answers, print any of them.
In the first example the first type of ice cream is present in the first vertex only, so we can color it in any color. The second and the third ice cream are both presented in the second vertex, so we should paint them in different colors.In the second example the colors of the second, the fourth and the fifth ice crea...
Input: 3 31 12 2 31 21 22 3 | Output: 21 1 2
Hard
3
949
502
283
8
1,983
C
1983C
C. Have Your Cake and Eat It Too
1,400
binary search; brute force; greedy; implementation
Alice, Bob and Charlie want to share a rectangular cake cut into \(n\) pieces. Each person considers every piece to be worth a different value. The \(i\)-th piece is considered to be of value \(a_i\) by Alice, \(b_i\) by Bob and \(c_i\) by Charlie.The sum over all \(a_i\), all \(b_i\) and all \(c_i\) individually is th...
The first line contains an integer \(t\), the number of testcases, (\(1 \le t \le 10^4\))For each testcase:The first line contains the integer \(n\) (\(3 \le n \le 2 \cdot 10^5\)).The following three lines contain \(n\) integers each:One line with \(n\) integers \(a_1, a_2, \ldots, a_n\) represents the values for Alice...
For each testcase, output \(-1\) if the required condition is impossible. Otherwise, output six numbers – \(l_a, r_a, l_b, r_b, l_c, r_c\), the respective starting and ending indices (\(1\)-indexed) of the subarrays for Alice, Bob and Charlie, respectively.
In the first testcase, the sum of either of the three arrays is \(9\). Each person needs a cake slice corresponding to a subarray with a total value of at least \(\lceil \frac{9}{3} \rceil = 3\). If we assign the subarray (\(1\),\(1\)) to Alice, its total value to her is \(5\), which is \(\ge 3\); the subarray (\(2\),\...
Input: 1055 1 1 1 11 1 5 1 11 1 1 1 561 2 3 4 5 65 6 1 2 3 43 4 5 6 1 244 4 4 44 4 4 44 4 4 455 10 5 2 109 6 9 7 110 7 10 2 334 5 26 1 41 8 2310 4 108 7 910 4 10757113 65383 19795 53580 74452 3879 2325512917 16782 89147 93107 27365 15044 4309533518 63581 33565 34112 46774 44151 4175666 3 1 8 7 110 2 6 2 2 410 9 2 1 2 2...
Easy
4
1,353
743
257
19
1,994
D
1994D
D. Funny Game
1,900
constructive algorithms; dsu; graphs; greedy; math; number theory; trees
Vanya has a graph with \(n\) vertices (numbered from \(1\) to \(n\)) and an array \(a\) of \(n\) integers; initially, there are no edges in the graph. Vanya got bored, and to have fun, he decided to perform \(n - 1\) operations.Operation number \(x\) (operations are numbered in order starting from \(1\)) is as follows:...
Each test consists of multiple test cases. The first line contains an integer \(t\) (\(1 \le t \le 10^{3}\)) — the number of test cases. Then follows the description of the test cases.The first line of each test case contains the number \(n\) (\(1 \leq n \leq 2000\)) — the number of vertices in the graph.The second lin...
For each test case, if there is no solution, then output ""No"" (without quotes). Otherwise, output ""Yes"" (without quotes), and then output \(n - 1\) lines, where in the \(i\)-th line, output the numbers \(u\) and \(v\) that need to be chosen for operation \(i\).You can output each letter in any case (for example, th...
Let's consider the second test case. First operation \((x = 1)\): we can connect vertices \(4\) and \(1\), since \(|a_4 - a_1| = |13 - 99| = |-86| = 86\), and \(86\) is divisible by \(1\). Second operation \((x = 2)\): we can connect vertices \(2\) and \(1\), since \(|a_2 - a_1| = |7 - 99| = |-92| = 92\), and \(92\) is...
Input: 821 4499 7 1 13510 2 31 44 73587 6 81 44 32562 35 33 79 1656 51 31 69 42552 63 25 21 51233 40 3 11 31 43 37 8 50 5 12 22 | Output: YES 2 1 YES 4 1 2 1 3 2 YES 5 1 4 1 3 1 2 1 YES 4 1 3 1 2 1 5 4 YES 3 1 5 1 2 1 4 2 YES 4 1 5 1 2 1 3 2 YES 2 1 5 2 3 1 4 3 YES 9 1 12 9 11 1 10 1 6 1 7 6 2 1 8 2 5 2 3 1 4 1
Hard
7
731
499
410
19
121
E
121E
E. Lucky Array
2,400
data structures
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.Petya has an array consisting of n numbers. He wants to perform m operations of two types: add l r d —...
The first line contains two integers n and m (1 ≤ n, m ≤ 105) — the number of numbers in the array and the number of operations correspondingly. The second line contains n positive integers, none of which exceeds 104 — those are the array numbers. Next m lines contain operations, one per line. They correspond to the de...
For each operation of the second type print the single number on the single line — the number of lucky numbers in the corresponding interval.
In the first sample after the first addition the array will look in the following manner:4 5 6After the second addition:4 8 9The second sample after the first addition:7 7 7 7After the second addition:7 47 47 7
Input: 3 62 3 4count 1 3count 1 2add 1 3 2count 1 3add 2 3 3count 1 3 | Output: 1011
Expert
1
894
455
141
1
1,170
I
1170I
I. Good Subsets
0
*special; dp
You are given \(n\) segments on the \(Ox\) axis. The \(i\)-th segment is given as a pair \(l_i, r_i\), where \(l_i\) is the position of the left end of the \(i\)-th segment and \(r_i\) is the position of the right end of the \(i\)-th segment. Segments may intersect, overlap, or even coincide. A segment is a set of numb...
The first line of the input contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) — the number of segments.The next \(n\) lines contain segments. The \(i\)-th segment is given as a pair \(l_i, r_i\) (\(1 \le l_i \le r_i \le 10^9\)), where \(l_i\) is the left border of the segment and \(r_i\) is the right border of ...
Print the number of good subsets of the given set of segments. Since the answer may be very large, print it modulo \(998244353\).
Input: 3 1 1 2 6 1 6 | Output: 4
Beginner
2
820
383
129
11
1,672
F1
1672F1
F1. Array Shuffling
2,000
constructive algorithms; graphs; greedy
oolimry has an array \(a\) of length \(n\) which he really likes. Today, you have changed his array to \(b\), a permutation of \(a\), to make him sad.Because oolimry is only a duck, he can only perform the following operation to restore his array: Choose two integers \(i,j\) such that \(1 \leq i,j \leq n\). Swap \(b_i\...
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) — the length of the array.The second lin...
For each test case, print \(n\) integers \(b_1, b_2, \ldots, b_n\) — describing the array \(b\). If there are multiple answers, you may print any.
In the first test case, the array \([1,2]\) has sadness \(1\). We can transform \([1,2]\) into \([2,1]\) using one operation with \((i,j)=(1,2)\).In the second test case, the array \([3,3,2,1]\) has sadness \(2\). We can transform \([3,3,2,1]\) into \([1,2,3,3]\) with two operations with \((i,j)=(1,4)\) and \((i,j)=(2,...
Input: 222 141 2 3 3 | Output: 1 2 3 3 2 1
Hard
3
585
536
146
16
1,392
B
1392B
B. Omkar and Infinity Clock
800
implementation; math
Being stuck at home, Ray became extremely bored. To pass time, he asks Lord Omkar to use his time bending power: Infinity Clock! However, Lord Omkar will only listen to mortals who can solve the following problem:You are given an array \(a\) of \(n\) integers. You are also given an integer \(k\). Lord Omkar wants you t...
Each test contains multiple test cases. The first line contains the number of cases \(t\) (\(1 \le t \le 100\)). Description of the test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \leq n \leq 2 \cdot 10^5, 1 \leq k \leq 10^{18}\)) – the length of your array and the number ...
For each case, print the final version of array \(a\) after \(k\) operations described above.
In the first test case the array changes as follows:Initially, the array is \([-199, 192]\). \(d = 192\).After the operation, the array becomes \([d-(-199), d-192] = [391, 0]\).
Input: 3 2 1 -199 192 5 19 5 -1 4 2 0 1 2 69 | Output: 391 0 0 6 1 3 5 0
Beginner
2
657
591
93
13
22
A
22A
A. Second Order Statistics
800
brute force
Once Bob needed to find the second order statistics of a sequence of integer numbers. Lets choose each number from the sequence exactly once and sort them. The value on the second position is the second order statistics of the given sequence. In other words it is the smallest element strictly greater than the minimum. ...
The first input line contains integer n (1 ≤ n ≤ 100) — amount of numbers in the sequence. The second line contains n space-separated integer numbers — elements of the sequence. These numbers don't exceed 100 in absolute value.
If the given sequence has the second order statistics, output this order statistics, otherwise output NO.
Input: 41 2 2 -4 | Output: 1
Beginner
1
348
227
105
0
1,211
G
1211G
G. King's Path
2,500
*special; math; trees
There are \(n\) cities and \(n-1\) two-way roads in Treeland. Each road connects a pair of different cities. From any city you can drive to any other, moving only along the roads. Cities are numbered from \(1\) to \(n\). Yes, of course, you recognized an undirected tree in this description.There is exactly one flag in ...
The first line contains an integer \(t\) (\(1 \le t \le 10^5\)) — the number of test cases to solve. The following are the cases.Each case begins with a line containing an integer \(n\) (\(2 \le n \le 2\cdot10^5\)) — the number of cities in Treeland.The following is a line of \(n\) integers \(c_1, c_2, \dots, c_n\) (\(...
Print the answers to all cases in the order of their appearance in the input data.Each answer must begin with a line containing ""Yes"" (in the case of a positive answer) or ""No"" (in the case that the required route does not exist). In the case of a positive answer, the following line must contain an integer \(k\) — ...
Input: 1 7 2 3 2 7 1 1 3 7 1 2 3 1 2 3 1 7 4 1 2 6 2 3 2 4 5 4 | Output: Yes 4 1 4 2 6
Expert
3
2,006
1,089
514
12
2,097
B
2097B
B. Baggage Claim
2,300
combinatorics; dfs and similar; dp; dsu; graphs; implementation; math; trees
Every airport has a baggage claim area, and Balbesovo Airport is no exception. At some point, one of the administrators at Sheremetyevo came up with an unusual idea: to change the traditional shape of the baggage claim conveyor from a carousel to a more complex form.Suppose that the baggage claim area is represented as...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 3 \cdot 10^4\)). The description of the test cases follows. The first line of each test case contains three integers \(n\), \(m\), and \(k\) (\(1 \le n, m \le 1000\), \(n \cdot m \ge 3\), \(1 \le k \le \left\lf...
For each test case, output a single integer — the number of ways to restore the original complete path modulo \(10^9+7\).
In the first test case, there are two possible paths: \((1,1) \to (2,1) \to (2, 2) \to (2, 3) \to (2, 4)\) \((1,1) \to (1,2) \to (2, 2) \to (2, 3) \to (2, 4)\) In the second test case, there is no suitable path, as the cells \((1,1)\) and \((1,4)\) do not have a common neighboring cell.
Input: 52 4 21 12 22 41 4 11 11 45 5 112 53 44 55 44 35 24 13 22 11 22 31 43 4 41 22 13 22 33 43 3 22 21 11 3 | Output: 2 0 2 5 1
Expert
8
1,144
819
121
20
1,613
F
1613F
F. Tree Coloring
2,600
combinatorics; divide and conquer; fft
You are given a rooted tree consisting of \(n\) vertices numbered from \(1\) to \(n\). The root of the tree is the vertex \(1\).You have to color all vertices of the tree into \(n\) colors (also numbered from \(1\) to \(n\)) so that there is exactly one vertex for each color. Let \(c_i\) be the color of vertex \(i\), a...
The first line contains one integer \(n\) (\(2 \le n \le 250000\)) — the number of vertices in the tree.Then \(n-1\) lines follow, the \(i\)-th line contains two integers \(x_i\) and \(y_i\) (\(1 \le x_i, y_i \le n\); \(x_i \ne y_i\)) denoting an edge between the vertex \(x_i\) and the vertex \(y_i\). These edges form ...
Print one integer — the number of beautiful colorings, taken modulo \(998244353\).
Input: 5 1 2 3 2 4 2 2 5 | Output: 42
Expert
3
660
327
82
16
1,077
F1
1077F1
F1. Pictures with Kittens (easy version)
1,900
dp
The only difference between easy and hard versions is the constraints.Vova likes pictures with kittens. The news feed in the social network he uses can be represented as an array of \(n\) consecutive pictures (with kittens, of course). Vova likes all these pictures, but some are more beautiful than the others: the \(i\...
The first line of the input contains three integers \(n, k\) and \(x\) (\(1 \le k, x \le n \le 200\)) — the number of pictures in the news feed, the minimum length of segment with at least one repost in it and the number of pictures Vova is ready to repost.The second line of the input contains \(n\) integers \(a_1, a_2...
Print -1 if there is no way to repost some pictures to satisfy all the conditions in the problem statement.Otherwise print one integer — the maximum sum of values of reposted pictures if Vova follows conditions described in the problem statement.
Input: 5 2 3 5 1 3 10 1 | Output: 18
Hard
1
990
413
246
10
156
C
156C
C. Cipher
2,000
combinatorics; dp
Sherlock Holmes found a mysterious correspondence of two VIPs and made up his mind to read it. But there is a problem! The correspondence turned out to be encrypted. The detective tried really hard to decipher the correspondence, but he couldn't understand anything. At last, after some thought, he thought of something....
The input data contains several tests. The first line contains the only integer t (1 ≤ t ≤ 104) — the number of tests.Next t lines contain the words, one per line. Each word consists of lowercase Latin letters and has length from 1 to 100, inclusive. Lengths of words can differ.
For each word you should print the number of different other words that coincide with it in their meaning — not from the words listed in the input data, but from all possible words. As the sought number can be very large, print its value modulo 1000000007 (109 + 7).
Some explanations about the operation: Note that for each letter, we can clearly define the letter that follows it. Letter ""b"" alphabetically follows letter ""a"", letter ""c"" follows letter ""b"", ..., ""z"" follows letter ""y"". Preceding letters are defined in the similar manner: letter ""y"" precedes letter ""z"...
Input: 1ab | Output: 1
Hard
2
1,482
279
266
1
1,903
C
1903C
C. Theofanis' Nightmare
1,400
constructive algorithms; greedy
Theofanis easily gets obsessed with problems before going to sleep and often has nightmares about them. To deal with his obsession he visited his doctor, Dr. Emix.In his latest nightmare, he has an array \(a\) of size \(n\) and wants to divide it into non-empty subarrays\(^{\dagger}\) such that every element is in exac...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.Each test case consists of two lines. The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^{5}\)) — the size of the array. The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(-...
For each test case, print one integer — the maximum Cypriot value of the array \(a\).
In the first test case, to get the maximum Cypriot value we divide the array into \([1][-3][7][-6][2][5]\) which gives us: \(\Sigma_{i=1}^{k} i \cdot \mathrm{sum}_i = 1 \cdot 1 + 2 \cdot (-3) + 3 \cdot 7 + 4 \cdot (-6) + 5 \cdot 2 + 6 \cdot 5 = 32\)Similarly, in the second test case we divide the array into \([2][9,-5,...
Input: 461 -3 7 -6 2 542 9 -5 -38-3 -4 2 -5 1 10 17 231830 | Output: 32 4 343 830
Easy
2
1,161
469
85
19
610
A
610A
A. Pasha and Stick
1,000
combinatorics; math
Pasha has a wooden stick of some positive integer length n. He wants to perform exactly three cuts to get four parts of the stick. Each part must have some positive integer length and the sum of these lengths will obviously be n. Pasha likes rectangles but hates squares, so he wonders, how many ways are there to split ...
The first line of the input contains a positive integer n (1 ≤ n ≤ 2·109) — the length of Pasha's stick.
The output should contain a single integer — the number of ways to split Pasha's stick into four parts of positive integer length so that it's possible to make a rectangle by connecting the ends of these parts, but is impossible to form a square.
There is only one way to divide the stick in the first sample {1, 1, 2, 2}.Four ways to divide the stick in the second sample are {1, 1, 9, 9}, {2, 2, 8, 8}, {3, 3, 7, 7} and {4, 4, 6, 6}. Note that {5, 5, 5, 5} doesn't work.
Input: 6 | Output: 1
Beginner
2
705
104
246
6
1,285
B
1285B
B. Just Eat It!
1,300
dp; greedy; implementation
Today, Yasser and Adel are at the shop buying cupcakes. There are \(n\) cupcake types, arranged from \(1\) to \(n\) on the shelf, and there are infinitely many of each type. The tastiness of a cupcake of type \(i\) is an integer \(a_i\). There are both tasty and nasty cupcakes, so the tastiness can be positive, zero or...
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 \(n\) (\(2 \le n \le 10^5\)).The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(-1...
For each test case, print ""YES"", if the total tastiness of cupcakes Yasser buys will always be strictly greater than the total tastiness of cupcakes Adel buys regardless of Adel's choice. Otherwise, print ""NO"".
In the first example, the total tastiness of any segment Adel can choose is less than the total tastiness of all cupcakes.In the second example, Adel will choose the segment \([1, 2]\) with total tastiness \(11\), which is not less than the total tastiness of all cupcakes, which is \(10\).In the third example, Adel can...
Input: 3 4 1 2 3 4 3 7 4 -1 3 5 -5 5 | Output: YES NO NO
Easy
3
1,362
499
214
12
1,431
A
1431A
A. Selling Hamburgers
800
*special
There are \(n\) customers in the cafeteria. Each of them wants to buy a hamburger. The \(i\)-th customer has \(a_i\) coins, and they will buy a hamburger if it costs at most \(a_i\) coins.Suppose the cost of the hamburger is \(m\). Then the number of coins the cafeteria earns is \(m\) multiplied by the number of people...
The first line contains one integer \(t\) (\(1 \le t \le 100\)) — the number of test cases.Each test case consists of two lines. The first line contains one integer \(n\) (\(1 \le n \le 100\)) — the number of customers.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^{12}\)), where ...
For each test case, print one integer — the maximum number of coins the cafeteria can earn.
Explanations for the test cases of the example: the best price for the hamburger is \(m = 1\), so \(3\) hamburgers are bought, and the cafeteria earns \(3\) coins; the best price for the hamburger is \(m = 4\), so \(1\) hamburger is bought, and the cafeteria earns \(4\) coins; the best price for the hamburger is \(m = ...
Input: 6 3 1 1 1 3 4 1 1 3 2 4 2 8 1 2 3 4 5 6 7 8 1 1000000000000 3 1000000000000 999999999999 1 | Output: 3 4 6 20 1000000000000 1999999999998
Beginner
1
437
377
91
14
1,119
F
1119F
F. Niyaz and Small Degrees
3,400
data structures; dp; trees
Niyaz has a tree with \(n\) vertices numerated from \(1\) to \(n\). A tree is a connected graph without cycles.Each edge in this tree has strictly positive integer weight. A degree of a vertex is the number of edges adjacent to this vertex.Niyaz does not like when vertices in the tree have too large degrees. For each \...
The first line contains a single integer \(n\) (\(2 \le n \le 250\,000\)) — the number of vertices in Niyaz's tree.Each of the next \((n - 1)\) lines contains three integers \(a\), \(b\), \(c\) (\(1 \le a, b \le n\), \(1 \leq c \leq 10^6\)) — the indices of the vertices connected by this edge and its weight, respective...
Print \(n\) integers: for each \(x = 0, 1, \ldots, (n-1)\) print the smallest total weight of such a set of edges that after one deletes the edges from the set, the degrees of all vertices become less than or equal to \(x\).
In the first example, the vertex \(1\) is connected with all other vertices. So for each \(x\) you should delete the \((4-x)\) lightest edges outgoing from vertex \(1\), so the answers are \(1+2+3+4\), \(1+2+3\), \(1+2\), \(1\) and \(0\).In the second example, for \(x=0\) you need to delete all the edges, for \(x=1\) y...
Input: 5 1 2 1 1 3 2 1 4 3 1 5 4 | Output: 10 6 3 1 0
Master
3
478
374
224
11
58
E
58E
E. Expression
2,400
dp
One day Vasya was solving arithmetical problems. He wrote down an expression a + b = c in his notebook. When the teacher checked Vasya's work it turned out that Vasya had solved the problem incorrectly. Now Vasya tries to find excuses. He says that he simply forgot to write down several digits in numbers a, b and c, bu...
The first and only input line contains the expression a + b = c (1 ≤ a, b, c ≤ 106, a, b and c don't contain leading zeroes) which is the expression Vasya wrote down.
Print the correct expression x + y = z (x, y and z are non-negative numbers without leading zeroes). The expression a + b = c must be met in x + y = z as a subsequence. The printed solution should have the minimal possible number of characters. If there are several such solutions, you can print any of them.
Input: 2+4=5 | Output: 21+4=25
Expert
1
635
166
308
0
1,704
E
1704E
E. Count Seconds
2,200
brute force; constructive algorithms; dp; graphs; implementation; math
Cirno has a DAG (Directed Acyclic Graph) with \(n\) nodes and \(m\) edges. The graph has exactly one node that has no out edges. The \(i\)-th node has an integer \(a_i\) on it.Every second the following happens: Let \(S\) be the set of nodes \(x\) that have \(a_x > 0\). For all \(x \in S\), \(1\) is subtracted from \(a...
The first line contains a single integer \(t\) (\(1 \leq t \leq 1000\)) — the number of test cases. Description of test cases follows.The first line of each test case contains two integers \(n, m\) (\(1 \leq n, m \leq 1000\)) — the number of vertices and edges in the graph.The second line of each test case contains \(n...
For each test case, print an integer in a separate line — the first moment of time when all \(a_i\) become \(0\), modulo \(998\,244\,353\).
In the first test case: At time \(0\), the values of the nodes are \([1, 1, 1]\). At time \(1\), the values of the nodes are \([0, 1, 1]\). At time \(2\), the values of the nodes are \([0, 0, 1]\). At time \(3\), the values of the nodes are \([0, 0, 0]\).So the answer is \(3\). In the second test case: At time \(0\), t...
Input: 53 21 1 11 22 35 51 0 0 0 01 22 33 44 51 510 11998244353 0 0 0 998244353 0 0 0 0 01 22 33 44 55 66 77 88 99 101 37 95 61293 1145 9961 9961 19191 22 33 45 41 42 46 910 10 10 10 10 101 21 32 34 36 33 56 56 16 2 | Output: 3 5 4 28010 110
Hard
6
562
783
139
17
976
B
976B
B. Lara Croft and the New Game
1,300
implementation; math
You might have heard about the next game in Lara Croft series coming out this year. You also might have watched its trailer. Though you definitely missed the main idea about its plot, so let me lift the veil of secrecy.Lara is going to explore yet another dangerous dungeon. Game designers decided to use good old 2D env...
The only line contains three integers n, m and k (2 ≤ n, m ≤ 109, n is always even, 0 ≤ k < n·m). Note that k doesn't fit into 32-bit integer type!
Print the cell (the row and the column where the cell is situated) where Lara ends up after she moves k times.
Here is her path on matrix 4 by 3:
Input: 4 3 0 | Output: 1 1
Easy
2
1,118
147
110
9
530
F
530F
F. Jumping frogs
2,100
*special
A rectangular swamp is inhabited by 10 species of frogs. Frogs of species i can jump from hillocks to hillock exactly i units along X-axis or Y-axis. Initially frogs of all types sit at the hillock at coordinates (0, 0). You are given coordinates of all other hillocks in the swamp. Find the largest Manhattan distance f...
The first line of the input contains an integer N (1 ≤ N ≤ 100) - the number of hillocks. The following N lines contain the coordinates of the hillocks, formatted as ""X Y"" ( - 20 ≤ X, Y ≤ 20). All hillocks are distinct, and none of them is located at (0, 0).
Output a single integer — the largest Manhattan distance to any hillock reachable by one of the frogs by jumping between hillocks.
In the first example frogs of species 1, 2 and 3 can jump as far as the first, the second and the third hillocks, respectively.In the second example frog of species 2 can jump to the second hillock, then to the fourth and finally to the fifth.In the third example no frog can reach the hillock.
Input: 30 10 -20 3 | Output: 3
Hard
1
485
260
130
5
2,040
F
2040F
F. Number of Cubes
2,700
combinatorics; dp; math; number theory
Consider a rectangular parallelepiped with sides \(a\), \(b\), and \(c\), that consists of unit cubes of \(k\) different colors. We can apply cyclic shifts to the parallelepiped in any of the three directions any number of times\(^{\text{∗}}\).There are \(d_i\) cubes of the \(i\)-th color (\(1 \le i \le k\)). How many ...
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 four integers: \(a\), \(b\), \(c\), and \(k\) (\(1 \le a, b, c \le 3 \cdot 10^6\); \(a \cdot b \cdot c \le 3 \cdot 10^...
For each test case, print one integer — the number of different parallelepipeds modulo \(998\,244\,353\).
In the first test case, there is only one parallelepiped, which consists of one unit cube. Possible parallelepipeds in the second test case
Input: 61 1 1 116 1 1 31 2 312 1 1 32 4 63 3 1 23 62 3 3 26 1272 60 96 417280 86400 120960 190080 | Output: 1 10 1160 12 1044 231490207
Master
4
873
841
105
20
411
B
411B
B. Multi-core Processor
1,600
implementation
The research center Q has developed a new multi-core processor. The processor consists of n cores and has k cells of cache memory. Consider the work of this processor.At each cycle each core of the processor gets one instruction: either do nothing, or the number of the memory cell (the core will write an information to...
The first line contains three integers n, m, k (1 ≤ n, m, k ≤ 100). Then follow n lines describing instructions. The i-th line contains m integers: xi1, xi2, ..., xim (0 ≤ xij ≤ k), where xij is the instruction that must be executed by the i-th core at the j-th cycle. If xij equals 0, then the corresponding instruction...
Print n lines. In the i-th line print integer ti. This number should be equal to 0 if the i-th core won't be locked, or it should be equal to the number of the cycle when this core will be locked.
Input: 4 3 51 0 01 0 22 3 13 2 0 | Output: 1130
Medium
1
1,401
600
196
4
65
E
65E
E. Harry Potter and Moving Staircases
2,900
dfs and similar; implementation
Harry Potter lost his Invisibility Cloak, running from the school caretaker Filch. Finding an invisible object is not an easy task. Fortunately, Harry has friends who are willing to help. Hermione Granger had read ""The Invisibility Cloaks, and Everything about Them"", as well as six volumes of ""The Encyclopedia of Qu...
The first line contains integers n and m (1 ≤ n ≤ 100000, 0 ≤ m ≤ 200000), which are the number of floors and staircases in Hogwarts, respectively. The following m lines contain pairs of floors connected by staircases at the initial moment of time.
In the first line print ""YES"" (without the quotes) if Harry is able to search all the floors, and ""NO"" otherwise. If the answer is positive, then print on the second line the number of staircases that Ron and Hermione will have to shift. Further output should look like this:Harry's moves a staircase's move Harry's ...
Input: 6 41 21 32 34 5 | Output: YES23 1 2 32 3 53 5 4 54 5 63 6 5 3
Master
2
2,085
248
1,480
0
1,800
F
1800F
F. Dasha and Nightmares
1,900
bitmasks; hashing; meet-in-the-middle; strings
Dasha, an excellent student, is studying at the best mathematical lyceum in the country. Recently, a mysterious stranger brought \(n\) words consisting of small latin letters \(s_1, s_2, \ldots, s_n\) to the lyceum. Since that day, Dasha has been tormented by nightmares.Consider some pair of integers \(\langle i, j \ra...
The first line contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) — the number of words.The following \(n\) lines contain the words \(s_1, s_2, \ldots, s_n\), consisting of small latin letters.It is guaranteed that the total length of words does not exceed \(5 \cdot 10^6\).
Print a single integer — the number of different nightmares.
In the first test, nightmares are created by pairs \(\langle 1, 3 \rangle\), \(\langle 2, 5 \rangle\), \(\langle 3, 4 \rangle\), \(\langle 6, 7 \rangle\), \(\langle 9, 10 \rangle\).
Input: 10ftlabcdefghijklmnopqrstuvwxyabcdeffghijkllmnopqrsttuvwxyffftlaabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyythedevidbcdefghhiiiijklmnopqrsuwxyzgorillasilverbackabcdefgijklmnopqrstuvwxyz | Output: 5
Hard
4
1,113
286
60
18
1,369
D
1369D
D. TediousLee
1,900
dp; graphs; greedy; math; trees
Lee tried so hard to make a good div.2 D problem to balance his recent contest, but it still doesn't feel good at all. Lee invented it so tediously slow that he managed to develop a phobia about div.2 D problem setting instead. And now he is hiding behind the bushes...Let's define a Rooted Dead Bush (RDB) of level \(n\...
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.Next \(t\) lines contain test cases — one per line.The first line of each test case contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^6\)) — the level of Lee's RDB.
For each test case, print a single integer — the maximum number of yellow vertices Lee can make modulo \(10^9 + 7\).
It's easy to see that the answer for RDB of level \(1\) or \(2\) is \(0\).The answer for RDB of level \(3\) is \(4\) since there is only one claw we can choose: \(\{1, 2, 3, 4\}\).The answer for RDB of level \(4\) is \(4\) since we can choose either single claw \(\{1, 3, 2, 4\}\) or single claw \(\{2, 7, 5, 6\}\). Ther...
Input: 7 1 2 3 4 5 100 2000000 | Output: 0 0 4 4 12 990998587 804665184
Hard
5
1,374
259
116
13
670
A
670A
A. Holidays
900
brute force; constructive algorithms; greedy; math
On the planet Mars a year lasts exactly n days (there are no leap years on Mars). But Martians have the same weeks as earthlings — 5 work days and then 2 days off. Your task is to determine the minimum possible and the maximum possible number of days off per year on Mars.
The first line of the input contains a positive integer n (1 ≤ n ≤ 1 000 000) — the number of days in a year on Mars.
Print two integers — the minimum possible and the maximum possible number of days off per year on Mars.
In the first sample there are 14 days in a year on Mars, and therefore independently of the day a year starts with there will be exactly 4 days off .In the second sample there are only 2 days in a year on Mars, and they can both be either work days or days off.
Input: 14 | Output: 4 4
Beginner
4
272
117
103
6
1,985
G
1985G
G. D-Function
1,600
combinatorics; math; number theory
Let \(D(n)\) represent the sum of digits of \(n\). For how many integers \(n\) where \(10^{l} \leq n < 10^{r}\) satisfy \(D(k \cdot n) = k \cdot D(n)\)? Output the answer modulo \(10^9+7\).
The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) – the number of test cases. Each test case contains three integers \(l\), \(r\), and \(k\) (\(0 \leq l < r \leq 10^9\), \(1 \leq k \leq 10^9\)).
For each test case, output an integer, the answer, modulo \(10^9 + 7\).
For the first test case, the only values of \(n\) that satisfy the condition are \(1\) and \(2\).For the second test case, the only values of \(n\) that satisfy the condition are \(1\), \(10\), and \(11\).For the third test case, all values of \(n\) between \(10\) inclusive and \(100\) exclusive satisfy the condition.
Input: 60 1 40 2 71 2 11 2 3582 74663 30 3 1 | Output: 2 3 90 12 974995667 999
Medium
3
189
210
71
19
1,144
F
1144F
F. Graph Without Long Directed Paths
1,700
dfs and similar; graphs
You are given a connected undirected graph consisting of \(n\) vertices and \(m\) edges. There are no self-loops or multiple edges in the given graph.You have to direct its edges in such a way that the obtained directed graph does not contain any paths of length two or greater (where the length of path is denoted as th...
The first line contains two integer numbers \(n\) and \(m\) (\(2 \le n \le 2 \cdot 10^5\), \(n - 1 \le m \le 2 \cdot 10^5\)) — the number of vertices and edges, respectively.The following \(m\) lines contain edges: edge \(i\) is given as a pair of vertices \(u_i\), \(v_i\) (\(1 \le u_i, v_i \le n\), \(u_i \ne v_i\)). T...
If it is impossible to direct edges of the given graph in such a way that the obtained directed graph does not contain paths of length at least two, print ""NO"" in the first line.Otherwise print ""YES"" in the first line, and then print any suitable orientation of edges: a binary string (the string consisting only of ...
The picture corresponding to the first example: And one of possible answers:
Input: 6 5 1 5 2 1 1 4 3 1 6 1 | Output: YES 10100
Medium
2
349
603
557
11
708
E
708E
E. Student's Camp
3,100
dp; math
Alex studied well and won the trip to student camp Alushta, located on the seashore. Unfortunately, it's the period of the strong winds now and there is a chance the camp will be destroyed! Camp building can be represented as the rectangle of n + 2 concrete blocks height and m blocks width.Every day there is a breeze b...
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 1500) that define the size of the destructible part of building.The second line of the input contains two integers a and b (1 ≤ a ≤ b ≤ 109) that define the probability p. It's guaranteed that integers a and b are coprime. The third line contains a s...
Consider the answer as an irreducible fraction is equal to . Print one integer equal to . It's guaranteed that within the given constraints .
In the first sample, each of the four blocks is destroyed with the probability . There are 7 scenarios that result in building not collapsing, and the probability we are looking for is equal to , so you should print
Input: 2 21 21 | Output: 937500007
Master
2
1,189
412
141
7
1,633
F
1633F
F. Perfect Matching
2,800
data structures; divide and conquer; interactive; trees
You are given a tree consisting of \(n\) vertices (numbered from \(1\) to \(n\)) and \(n-1\) edges (numbered from \(1\) to \(n-1\)). Initially, all vertices except vertex \(1\) are inactive.You have to process queries of three types: \(1\) \(v\) — activate the vertex \(v\). It is guaranteed that the vertex \(v\) is ina...
The first line contains one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) — the number of vertices of the tree.Then \(n-1\) lines follow. The \(i\)-th line contains two integers \(u_i\) and \(v_i\) (\(1 \le u_i, v_i \le n\); \(u_i \ne v_i\)) — the endpoints of the \(i\)-th edge. These edges form a tree.Then the queries ...
For each query of type \(1\) or \(2\), print the answer on a separate line as described in the statement. Don't forget to flush the output.
Input: 6 1 4 6 1 3 2 1 2 5 1 1 4 2 1 2 2 1 3 2 1 5 1 6 2 3 | Output: 1 1 1 0 0 4 2 1 3 0 0 0
Master
4
1,569
1,256
139
16
1,426
A
1426A
A. Floor Number
800
implementation; math
Vasya goes to visit his classmate Petya. Vasya knows that Petya's apartment number is \(n\). There is only one entrance in Petya's house and the distribution of apartments is the following: the first floor contains \(2\) apartments, every other floor contains \(x\) apartments each. Apartments are numbered starting from...
The first line of the input contains one integer \(t\) (\(1 \le t \le 1000\)) — the number of test cases. Then \(t\) test cases follow.The only line of the test case contains two integers \(n\) and \(x\) (\(1 \le n, x \le 1000\)) — the number of Petya's apartment and the number of apartments on each floor of the house ...
For each test case, print the answer: the number of floor on which Petya lives.
Consider the first test case of the example: the first floor contains apartments with numbers \(1\) and \(2\), the second one contains apartments with numbers \(3\), \(4\) and \(5\), the third one contains apartments with numbers \(6\), \(7\) and \(8\). Therefore, Petya lives on the third floor.In the second test case ...
Input: 4 7 3 1 5 22 5 987 13 | Output: 3 1 5 77
Beginner
2
766
387
79
14
1,492
D
1492D
D. Genius's Gambit
1,900
bitmasks; constructive algorithms; greedy; math
You are given three integers \(a\), \(b\), \(k\).Find two binary integers \(x\) and \(y\) (\(x \ge y\)) such that both \(x\) and \(y\) consist of \(a\) zeroes and \(b\) ones; \(x - y\) (also written in binary form) has exactly \(k\) ones. You are not allowed to use leading zeros for \(x\) and \(y\).
The only line contains three integers \(a\), \(b\), and \(k\) (\(0 \leq a\); \(1 \leq b\); \(0 \leq k \leq a + b \leq 2 \cdot 10^5\)) — the number of zeroes, ones, and the number of ones in the result.
If it's possible to find two suitable integers, print ""Yes"" followed by \(x\) and \(y\) in base-2.Otherwise print ""No"".If there are multiple possible answers, print any of them.
In the first example, \(x = 101000_2 = 2^5 + 2^3 = 40_{10}\), \(y = 100001_2 = 2^5 + 2^0 = 33_{10}\), \(40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}\). Hence \(x-y\) has \(3\) ones in base-2.In the second example, \(x = 10100_2 = 2^4 + 2^2 = 20_{10}\), \(y = 10010_2 = 2^4 + 2^1 = 18\), \(x - y = 20 - 18 = 2_{...
Input: 4 2 3 | Output: Yes 101000 100001
Hard
4
300
201
181
14
1,364
B
1364B
B. Most socially-distanced subsequence
1,300
greedy; two pointers
Given a permutation \(p\) of length \(n\), find its subsequence \(s_1\), \(s_2\), \(\ldots\), \(s_k\) of length at least \(2\) such that: \(|s_1-s_2|+|s_2-s_3|+\ldots+|s_{k-1}-s_k|\) is as big as possible over all subsequences of \(p\) with length at least \(2\). Among all such subsequences, choose the one whose length...
The first line contains an integer \(t\) (\(1 \le t \le 2 \cdot 10^4\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains an integer \(n\) (\(2 \le n \le 10^5\)) — the length of the permutation \(p\).The second line of each test case contains \(n\) integers ...
For each test case, the first line should contain the length of the found subsequence, \(k\). The second line should contain \(s_1\), \(s_2\), \(\ldots\), \(s_k\) — its elements.If multiple subsequences satisfy these conditions, you are allowed to find any of them.
In the first test case, there are \(4\) subsequences of length at least \(2\): \([3,2]\) which gives us \(|3-2|=1\). \([3,1]\) which gives us \(|3-1|=2\). \([2,1]\) which gives us \(|2-1|=1\). \([3,2,1]\) which gives us \(|3-2|+|2-1|=2\). So the answer is either \([3,1]\) or \([3,2,1]\). Since we want the subsequence t...
Input: 2 3 3 2 1 4 1 3 4 2 | Output: 2 3 1 3 1 4 2
Easy
2
700
507
265
13
219
D
219D
D. Choosing Capital for Treeland
1,700
dfs and similar; dp; graphs; trees
The country Treeland consists of n cities, some pairs of them are connected with unidirectional roads. Overall there are n - 1 roads in the country. We know that if we don't take the direction of the roads into consideration, we can get from any city to any other one.The council of the elders has recently decided to ch...
The first input line contains integer n (2 ≤ n ≤ 2·105) — the number of cities in Treeland. Next n - 1 lines contain the descriptions of the roads, one road per line. A road is described by a pair of integers si, ti (1 ≤ si, ti ≤ n; si ≠ ti) — the numbers of cities, connected by that road. The i-th road is oriented fro...
In the first line print the minimum number of roads to be inversed if the capital is chosen optimally. In the second line print all possible ways to choose the capital — a sequence of indexes of cities in the increasing order.
Input: 32 12 3 | Output: 02
Medium
4
890
398
226
2
1,470
A
1470A
A. Strange Birthday Party
1,300
binary search; dp; greedy; sortings; two pointers
Petya organized a strange birthday party. He invited \(n\) friends and assigned an integer \(k_i\) to the \(i\)-th of them. Now Petya would like to give a present to each of them. In the nearby shop there are \(m\) unique presents available, the \(j\)-th present costs \(c_j\) dollars (\(1 \le c_1 \le c_2 \le \ldots \le...
The first input line contains a single integer \(t\) (\(1 \leq t \leq 10^3\)) — the number of test cases.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \leq n, m \leq 3 \cdot 10^5\)) — the number of friends, and the number of unique presents available.The following line contains \(n\) integ...
For each test case output a single integer — the minimum cost of the party.
In the first example, there are two test cases. In the first one, Petya has \(5\) friends and \(4\) available presents. Petya can spend only \(30\) dollars if he gives \(5\) dollars to the first friend. A present that costs \(12\) dollars to the second friend. A present that costs \(5\) dollars to the third friend. A p...
Input: 2 5 4 2 3 4 3 2 3 5 12 20 5 5 5 4 3 2 1 10 40 90 160 250 | Output: 30 190
Easy
5
601
731
75
14
625
C
625C
C. K-special Tables
1,300
constructive algorithms; implementation
People do many crazy things to stand out in a crowd. Some of them dance, some learn by heart rules of Russian language, some try to become an outstanding competitive programmers, while others collect funny math objects.Alis is among these collectors. Right now she wants to get one of k-special tables. In case you forge...
The first line of the input contains two integers n and k (1 ≤ n ≤ 500, 1 ≤ k ≤ n) — the size of the table Alice is looking for and the column that should have maximum possible sum.
First print the sum of the integers in the k-th column of the required table.Next n lines should contain the description of the table itself: first line should contains n elements of the first row, second line should contain n elements of the second row and so on.If there are multiple suitable table, you are allowed to...
Input: 4 1 | Output: 281 2 3 45 6 7 89 10 11 1213 14 15 16
Easy
2
789
181
331
6
1,941
F
1941F
F. Rudolf and Imbalance
1,800
binary search; greedy; sortings; two pointers
Rudolf has prepared a set of \(n\) problems with complexities \(a_1 < a_2 < a_3 < \dots < a_n\). He is not entirely satisfied with the balance, so he wants to add at most one problem to fix it.For this, Rudolf came up with \(m\) models of problems and \(k\) functions. The complexity of the \(i\)-th model is \(d_i\), an...
The first line of the input contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of testcases.The first line of each test case contains three integers \(n\), \(m\), and \(k\) (\(2 \le n \le 10^5\), \(1 \le m, k \le 2 \cdot 10^5\)) — the number of prepared problems, the number of models, and the number of...
For each testcase, output a single number — the minimum imbalance that Rudolf can achieve.
Input: 75 5 55 10 15 20 2611 14 16 13 816 4 5 3 17 6 51 4 7 10 18 21 222 3 5 7 4 26 8 9 3 27 6 51 4 7 10 18 21 222 3 5 7 4 26 8 13 3 25 6 32 10 13 20 2511 6 10 16 14 56 17 154 2 211 12 14 1519 1410 68 4 23 10 16 18 21 22 29 309 13 16 154 22 4 74 214 15 14 520 1 15 1 12 5 11 | Output: 5 4 5 8 2 7 11
Medium
4
938
1,085
90
19
1,325
C
1325C
C. Ehab and Path-etic MEXs
1,500
constructive algorithms; dfs and similar; greedy; trees
You are given a tree consisting of \(n\) nodes. You want to write some labels on the tree's edges such that the following conditions hold: Every label is an integer between \(0\) and \(n-2\) inclusive. All the written labels are distinct. The largest value among \(MEX(u,v)\) over all pairs of nodes \((u,v)\) is as smal...
The first line contains the integer \(n\) (\(2 \le n \le 10^5\)) — the number of nodes in the tree.Each of the next \(n-1\) lines contains two space-separated integers \(u\) and \(v\) (\(1 \le u,v \le n\)) that mean there's an edge between nodes \(u\) and \(v\). It's guaranteed that the given graph is a tree.
Output \(n-1\) integers. The \(i^{th}\) of them will be the number written on the \(i^{th}\) edge (in the input order).
The tree from the second sample:
Input: 3 1 2 1 3 | Output: 0 1
Medium
4
483
310
119
13
2,013
D
2013D
D. Minimize the Difference
1,900
binary search; greedy
Zhan, tired after the contest, gave the only task that he did not solve during the contest to his friend, Sungat. However, he could not solve it either, so we ask you to try to solve this problem.You are given an array \(a_1, a_2, \ldots, a_n\) of length \(n\). We can perform any number (possibly, zero) of operations o...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^5\)). The description of the test cases follows.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)).The second line of each test case contains \(n\) integers \(a_1, a_...
For each test case, output a single integer: the minimum possible value of \(\max(a_1, a_2, \ldots, a_n) - \min(a_1, a_2, \ldots, a_n)\).
In the third testcase, you can perform the operation twice with \(i = 1\).After that, the array is \(a = [2, 3, 2, 3]\), and \(\max(2, 3, 2, 3) - \min(2, 3, 2, 3) = 3 - 2 = 1\).
Input: 51131 2 344 1 2 344 2 3 155 14 4 10 2 | Output: 0 2 1 1 3
Hard
2
586
437
137
20
2,056
B
2056B
B. Find the Permutation
1,300
brute force; dfs and similar; graphs; implementation; sortings
You are given an undirected graph with \(n\) vertices, labeled from \(1\) to \(n\). This graph encodes a hidden permutation\(^{\text{∗}}\) \(p\) of size \(n\). The graph is constructed as follows: For every pair of integers \(1 \le i < j \le n\), an undirected edge is added between vertex \(p_i\) and vertex \(p_j\) if ...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 500\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(1 \le n \le 1000\)).The \(i\)-th of the next \(n\) lines contains a string of \(n\) charact...
For each test case, output \(n\) integers \(p_1, p_2, \ldots, p_n\) representing the reconstructed permutation.
In the first case \(p = [1]\). Since there are no pairs \(1 \le i < j \le n\), there are no edges in the graph.The graph in the second case is shown below. For example, when we choose \(i = 3\) and \(j = 4\), we add an edge between vertices \(p_i = 1\) and \(p_j = 3\), because \(p_i < p_j\). However, when we choose \(i...
Input: 310500101001011100100001111106000000000000000000000000000000000000 | Output: 1 4 2 1 3 5 6 5 4 3 2 1
Easy
5
989
845
111
20
301
E
301E
E. Yaroslav and Arrangements
2,800
dp
Yaroslav calls an array of r integers a1, a2, ..., ar good, if it meets the following conditions: |a1 - a2| = 1, |a2 - a3| = 1, ..., |ar - 1 - ar| = 1, |ar - a1| = 1, at that . An array of integers b1, b2, ..., br is called great, if it meets the following conditions: The elements in it do not decrease (bi ≤ bi + 1). I...
The single line contains three integers n, m, k (1 ≤ n, m, k ≤ 100).
In a single line print the remainder after dividing the answer to the problem by number 1000000007 (109 + 7).
Input: 1 1 1 | Output: 0
Master
1
758
68
109
3
1,119
H
1119H
H. Triple
3,200
fft; math
You have received your birthday gifts — \(n\) triples of integers! The \(i\)-th of them is \(\lbrace a_{i}, b_{i}, c_{i} \rbrace\). All numbers are greater than or equal to \(0\), and strictly smaller than \(2^{k}\), where \(k\) is a fixed integer.One day, you felt tired playing with triples. So you came up with three ...
The first line contains two integers \(n\) and \(k\) (\(1 \leq n \leq 10^{5}\), \(1 \leq k \leq 17\)) — the number of arrays and the binary length of all numbers.The second line contains three integers \(x\), \(y\), \(z\) (\(0 \leq x,y,z \leq 10^{9}\)) — the integers you chose.Then \(n\) lines follow. The \(i\)-th of t...
Print a single line containing \(2^{k}\) integers. The \(i\)-th of them should be the number of ways to choose exactly one integer from each array so that their XOR is equal to \(t = i-1\) modulo \(998244353\).
In the first example, the array we formed is \((1, 0, 0, 1, 1, 1)\), we have two choices to get \(0\) as the XOR and four choices to get \(1\).In the second example, two arrays are \((0, 1, 1, 2)\) and \((1, 2, 2, 3)\). There are sixteen \((4 \cdot 4)\) choices in total, \(4\) of them (\(1 \oplus 1\) and \(2 \oplus 2\)...
Input: 1 1 1 2 3 1 0 1 | Output: 2 4
Master
2
803
475
210
11
1,394
C
1394C
C. Boboniu and String
2,600
binary search; geometry; ternary search
Boboniu defines BN-string as a string \(s\) of characters 'B' and 'N'.You can perform the following operations on the BN-string \(s\): Remove a character of \(s\). Remove a substring ""BN"" or ""NB"" of \(s\). Add a character 'B' or 'N' to the end of \(s\). Add a string ""BN"" or ""NB"" to the end of \(s\). Note that a...
The first line contains a single integer \(n\) (\(1\le n\le 3\cdot 10^5\)).Each of the next \(n\) lines contains a string \(s_i\) (\(1\le |s_i| \le 5\cdot 10^5\)). It is guaranteed that \(s_i\) only contains 'B' and 'N'. The sum of \(|s_i|\) does not exceed \(5\cdot 10^5\).
In the first line, print the minimum \(\max_{i=1}^n \text{dist}(s_i,t)\).In the second line, print the suitable \(t\).If there are several possible \(t\)'s, you can print any.
In the first example \(\text{dist(B,BN)}=\text{dist(N,BN)}=1\), \(\text{dist(BN,BN)}=0\). So the maximum distance is \(1\).
Input: 3 B N BN | Output: 1 BN
Expert
3
1,143
274
175
13
1,209
B
1209B
B. Koala and Lights
1,300
implementation; math; number theory
It is a holiday season, and Koala is decorating his house with cool lights! He owns \(n\) lights, all of which flash periodically.After taking a quick glance at them, Koala realizes that each of his lights can be described with two parameters \(a_i\) and \(b_i\). Light with parameters \(a_i\) and \(b_i\) will toggle (o...
The first line contains a single integer \(n\) (\(1 \le n \le 100\)), the number of lights.The next line contains a string \(s\) of \(n\) characters. The \(i\)-th character is ""1"", if the \(i\)-th lamp is initially on. Otherwise, \(i\)-th character is ""0"".The \(i\)-th of the following \(n\) lines contains two integ...
Print a single integer — the maximum number of lights that will ever be on at the same time.
For first example, the lamps' states are shown in the picture above. The largest number of simultaneously on lamps is \(2\) (e.g. at the moment \(2\)).In the second example, all lights are initially on. So the answer is \(4\).
Input: 3 101 3 3 3 2 3 1 | Output: 2
Easy
3
781
410
92
12
1,248
A
1248A
A. Integer Points
1,000
geometry; math
DLS and JLS are bored with a Math lesson. In order to entertain themselves, DLS took a sheet of paper and drew \(n\) distinct lines, given by equations \(y = x + p_i\) for some distinct \(p_1, p_2, \ldots, p_n\).Then JLS drew on the same paper sheet \(m\) distinct lines given by equations \(y = -x + q_i\) for some dist...
The first line contains one integer \(t\) (\(1 \le t \le 1000\)), the number of test cases in the input. Then follow the test case descriptions.The first line of a test case contains an integer \(n\) (\(1 \le n \le 10^5\)), the number of lines drawn by DLS.The second line of a test case contains \(n\) distinct integers...
For each test case in the input print a single integer — the number of line pairs with integer intersection points.
The picture shows the lines from the first test case of the example. Black circles denote intersection points with integer coordinates.
Input: 3 3 1 3 2 2 0 3 1 1 1 1 1 2 1 1 | Output: 3 1 0
Beginner
2
581
1,069
115
12
1,436
E
1436E
E. Complicated Computations
2,400
binary search; data structures; two pointers
In this problem MEX of a certain array is the smallest positive integer not contained in this array.Everyone knows this definition, including Lesha. But Lesha loves MEX, so he comes up with a new problem involving MEX every day, including today.You are given an array \(a\) of length \(n\). Lesha considers all the non-e...
The first line contains a single integer \(n\) (\(1 \le n \le 10^5\)) — the length of the array. The next line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le n\)) — the elements of the array.
Print a single integer — the MEX of MEXes of all subarrays.
Input: 3 1 3 2 | Output: 3
Expert
3
862
211
59
14
2,066
B
2066B
B. White Magic
1,900
constructive algorithms; data structures; dp; greedy; implementation
We call a sequence \(a_1, a_2, \ldots, a_n\) magical if for all \(1 \leq i \leq n-1\) it holds that: \(\operatorname{min}(a_1, \ldots, a_i) \geq \operatorname{mex}(a_{i+1}, \ldots, a_n)\). In particular, any sequence of length \(1\) is considered magical.The minimum excluded (MEX) of a collection of integers \(a_1, a_2...
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 \leq n \leq 2 \cdot 10^5\)) — the length of the sequence \(a\).The second line of each test ca...
For each test case, output a single number — the maximum possible length of a magical subsequence of the sequence \(a\).
In the first test case, the sequence \([4, 3, 2, 1, 0]\) is magical, since: \(\operatorname{min}(4) = 4, \operatorname{mex}(3, 2, 1, 0) = 4\). \(4 \geq 4\) \(\operatorname{min}(4, 3) = 3, \operatorname{mex}(2, 1, 0) = 3\). \(3 \geq 3\) \(\operatorname{min}(4, 3, 2) = 2, \operatorname{mex}(1, 0) = 2\). \(2 \geq 2\) \(\o...
Input: 854 3 2 1 064 3 3 2 1 042 0 1 2177741000000000 1 7 920 121 240 1 0 1 | Output: 5 5 3 1 4 2 2 3
Hard
5
783
531
120
20
1,641
E
1641E
E. Special Positions
3,300
combinatorics; divide and conquer; fft; math
You are given an array \(a\) of length \(n\). Also you are given \(m\) distinct positions \(p_1, p_2, \ldots, p_m\) (\(1 \leq p_i \leq n\)).A non-empty subset of these positions \(T\) is randomly selected with equal probability and the following value is calculated: $$$\(\sum_{i=1}^{n} (a_i \cdot \min_{j \in T} \left|i...
The first line contains two integers \(n\) and \(m\) (\(1 \leq m \leq n \leq 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_i < 998\,244\,353\)).The third line contains \(m\) distinct integers \(p_1, p_2, \ldots, p_m\) (\(1 \leq p_i \le n\)).For every \(1 \leq i < m\) it is guaran...
Print a single integer — the answer to the problem.
In the first test: If only \(1\) is choosen, than the value equals to \(1 \cdot 0 + 2 \cdot 1 + 3 \cdot 2 + 4 \cdot 3 = 20\). If only \(4\) is choosen, than the value equals to \(1 \cdot 3 + 2 \cdot 2 + 3 \cdot 1 + 4 \cdot 0 = 10\). If both positions are chosen, than the value equals to \(1 \cdot 0 + 2 \cdot 1 + 3 \cdo...
Input: 4 2 1 2 3 4 1 4 | Output: 665496247
Master
4
938
348
51
16
278
A
278A
A. Circle Line
800
implementation
The circle line of the Berland subway has n stations. We know the distances between all pairs of neighboring stations: d1 is the distance between the 1-st and the 2-nd station; d2 is the distance between the 2-nd and the 3-rd station;... dn - 1 is the distance between the n - 1-th and the n-th station; dn is the distan...
The first line contains integer n (3 ≤ n ≤ 100) — the number of stations on the circle line. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 100) — the distances between pairs of neighboring stations. The third line contains two integers s and t (1 ≤ s, t ≤ n) — the numbers of stations, between which you ...
Print a single number — the length of the shortest path between stations number s and t.
In the first sample the length of path 1 → 2 → 3 equals 5, the length of path 1 → 4 → 3 equals 13.In the second sample the length of path 4 → 1 is 100, the length of path 4 → 3 → 2 → 1 is 15.In the third sample the length of path 3 → 1 is 1, the length of path 3 → 2 → 1 is 2.In the fourth sample the numbers of stations...
Input: 42 3 4 91 3 | Output: 5
Beginner
1
482
442
88
2
1,982
B
1982B
B. Collatz Conjecture
1,200
brute force; implementation; math; number theory
Recently, the first-year student Maxim learned about the Collatz conjecture, but he didn't pay much attention during the lecture, so he believes that the following process is mentioned in the conjecture:There is a variable \(x\) and a constant \(y\). The following operation is performed \(k\) times: increase \(x\) by \...
Each test consists of multiple test cases. The first line contains an integer \(t\) (\(1 \le t \le 10^{4}\)) — the number of test cases. Then follows the description of the test cases.The only line of each test case contains three integers \(x\), \(y\), and \(k\) (\(1 \le x, k \le 10^{9}\), \(2 \le y \le 10^{9}\)) — th...
For each test case, output a single integer — the number obtained after applying \(k\) operations.
In the first test case, there is only one operation applied to \(x = 1\), resulting in \(x\) becoming \(2\).In the second test case, for \(x = 2\), within one operation, one is added to \(x\) and it's divided by \(y = 3\), resulting in \(x\) becoming \(1\).In the third test case, \(x\) changes as follows: After the fir...
Input: 131 3 12 3 124 5 516 3 22 2 11337 18 11 2 14413312345678 3 10998244353 2 998244353998244353 123456789 998244352998244354 998241111 998244352998244355 2 99824431000000000 1000000000 1000000000 | Output: 2 1 1 2 3 1338 1 16936 1 21180097 6486 1 2
Easy
4
809
378
98
19
1,365
F
1365F
F. Swaps Again
2,100
constructive algorithms; implementation; sortings
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid.Each test case consists of an integer \(n\) and two arrays \(a\) and \(b\), of size \(n\). If after some (possibly zero) operations described below, array \(a\) can be transformed ...
The first line contains one integer \(t\) \((1 \le t \le 500)\) — the number of test cases. The description of each test case is as follows.The first line of each test case contains a single integer \(n\) \((1 \le n \le 500)\) — the size of the arrays.The second line of each test case contains \(n\) integers \(a_1\), \...
For each test case, print ""Yes"" if the given input is valid. Otherwise print ""No"".You may print the answer in any case.
For the first test case, we can swap prefix \(a[1:1]\) with suffix \(a[2:2]\) to get \(a=[2, 1]\).For the second test case, \(a\) is already equal to \(b\).For the third test case, it is impossible since we cannot obtain \(3\) in \(a\).For the fourth test case, we can first swap prefix \(a[1:1]\) with suffix \(a[4:4]\)...
Input: 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 | Output: yes yes No yes No
Hard
3
800
531
123
13
1,066
F
1066F
F. Yet another 2D Walking
2,100
dp
Maksim walks on a Cartesian plane. Initially, he stands at the point \((0, 0)\) and in one move he can go to any of four adjacent points (left, right, up, down). For example, if Maksim is currently at the point \((0, 0)\), he can go to any of the following points in one move: \((1, 0)\); \((0, 1)\); \((-1, 0)\); \((0, ...
The first line of the input contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) — the number of key points.Each of the next \(n\) lines contains two integers \(x_i\), \(y_i\) (\(0 \le x_i, y_i \le 10^9\)) — \(x\)-coordinate of the key point \(p_i\) and \(y\)-coordinate of the key point \(p_i\). It is guaranteed t...
Print one integer — the minimum possible total distance Maksim has to travel if he needs to visit all key points in a way described above.
The picture corresponding to the first example: There is one of the possible answers of length \(15\).The picture corresponding to the second example: There is one of the possible answers of length \(9\).
Input: 82 21 42 33 13 41 14 31 2 | Output: 15
Hard
1
1,276
396
138
10
2,082
A
2082A
A. Binary Matrix
800
constructive algorithms; greedy
A matrix is called binary if all its elements are either \(0\) or \(1\).Ecrade calls a binary matrix \(A\) good if the following two properties hold: The bitwise XOR of all numbers in each row of matrix \(A\) is equal to \(0\). The bitwise XOR of all numbers in each column of matrix \(A\) is equal to \(0\). Ecrade has ...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 400\)). The description of the test cases follows. The first line of each test case contains two integers \(n, m\) (\(1 \le n, m \le 100\)).This is followed by \(n\) lines, each containing exactly \(m\) charact...
For each test case, output a single integer, the minimum number of elements that need to be changed.
In the first test case, he needs to change 2 elements to obtain the following matrix \(\begin{pmatrix}1&1&0\\1&0&1\\0&1&1\end{pmatrix}\).In the second test case, he can make no changes to obtain the following matrix \(\begin{pmatrix}0&0&0\\0&0&0\\0&0&0\end{pmatrix}\).In the third test case, he needs to change 3 element...
Input: 73 30101010103 30000000003 31000100013 31010100003 30000100001 401014 10101 | Output: 2 0 3 3 1 2 2
Beginner
2
521
505
100
20
2,086
C
2086C
C. Disappearing Permutation
1,300
dfs and similar; dp; dsu; graphs; greedy; implementation
A permutation of integers from \(1\) to \(n\) is an array of size \(n\) where each integer from \(1\) to \(n\) appears exactly once.You are given a permutation \(p\) of integers from \(1\) to \(n\). You have to process \(n\) queries. During the \(i\)-th query, you replace \(p_{d_i}\) with \(0\). Each element is replace...
Each test consists of several test cases. The first line contains one integer \(t\) (\(1 \le t \le 10^{4}\)) — the number of test cases. Then the test cases follow.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^{5}\)).The second line of each test case contains \(n\) integers \(p_1, p...
For each test case, output a line containing \(n\) integers, where the \(i\)-th integer should be equal to the minimum number of operations required to fix the array which was obtained after the \(i\)-th query (i.e., the permutation \(p\) where all integers \(p_{d_1}, p_{d_2}, \dots, p_{d_i}\) are replaced by zeroes).
In the first test case, after each query, every integer which was replaced by \(0\) can be restored by one operation.In the second test case, you can act as follows: Query \(1\): \(p = [4, 5, 3, 0, 2]\), it can be transformed into \([{\color{red}1}, 5, 3, {\color{red}4}, 2]\). Query \(2\): \(p = [4, 5, 3, 0, 0]\), it c...
Input: 331 2 33 2 154 5 3 1 24 5 1 3 274 3 1 2 7 5 61 2 3 4 5 6 7 | Output: 1 2 3 2 4 4 5 5 4 4 4 4 7 7 7
Easy
6
1,098
658
319
20
1,737
C
1737C
C. Ela and Crickets
1,500
constructive algorithms; games; implementation; math
Ela likes Chess a lot. During breaks, she usually challenges her co-worker in DTL to some chess games. She's not an expert at classic chess, but she's very interested in Chess variants, where she has to adapt to new rules and test her tactical mindset to win the game.The problem, which involves a non-standard chess pie...
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 \(n\) (\(4 \le n \le 10^5\)) — denotes the size of the chessboard.The second line of each test case contains 6 number...
For each test case, print ""YES"" or ""NO"" to denotes whether Ela can put a cricket on the target square.
Here's the solution for the first test case. The red square denotes where the crickets need to reach. Note that in chess horizontals are counted from bottom to top, as well as on this picture.
Input: 687 2 8 2 7 15 182 2 1 2 2 15 582 2 1 2 2 16 681 1 1 2 2 15 582 2 1 2 2 18 888 8 8 7 7 84 8 | Output: YES NO YES NO YES YES
Medium
4
1,252
674
106
17
1,623
D
1623D
D. Robot Cleaner Revisit
2,300
implementation; math; probabilities
The statement of this problem shares a lot with problem A. The differences are that in this problem, the probability is introduced, and the constraint is different.A robot cleaner is placed on the floor of a rectangle room, surrounded by walls. The floor consists of \(n\) rows and \(m\) columns. The rows of the floor a...
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.A test case consists of only one line, containing \(n\), \(m\), \(r_b\), \(c_b\), \(r_d\), \(c_d\), and \(p\) (\(4 \le n \cdot m \le 10^5\), \(n, m \ge 2\), \(1 \le ...
For each test case, print a single integer — the expected time for the robot to clean the dirty cell, modulo \(10^9 + 7\).
In the first test case, the robot has the opportunity to clean the dirty cell every second. Using the geometric distribution, we can find out that with the success rate of \(25\%\), the expected number of tries to clear the dirty cell is \(\frac 1 {0.25} = 4\). But because the first moment the robot has the opportunity...
Input: 6 2 2 1 1 2 1 25 3 3 1 2 2 2 25 10 10 1 1 10 10 75 10 10 10 10 1 1 75 5 5 1 3 2 2 10 97 98 3 5 41 43 50 | Output: 3 3 15 15 332103349 99224487
Expert
3
2,202
520
122
16
48
E
48E
E. Ivan the Fool VS Gorynych the Dragon
2,100
dp; games; graphs
Once upon a time in a kingdom far, far away… Okay, let’s start at the point where Ivan the Fool met Gorynych the Dragon. Ivan took out his magic sword and the battle began. First Gorynych had h heads and t tails. With each strike of the sword Ivan can either cut off several heads (from 1 to n, but not more than Gorynyc...
The first line contains three integers h, t and R (0 ≤ h, t, R ≤ 200, 0 < h + t ≤ R) which represent the initial numbers of Gorynych’s heads and tails and the largest total number of heads and tails with which Gorynych the Dragon does not yet attack. The next line contains integer n (1 ≤ n ≤ 200). The next n contain pa...
Print ""Ivan"" (without quotes) in the first line if Ivan wins, or ""Zmey"" (that means a dragon in Russian) if Gorynych the Dragon wins. In the second line print a single integer which represents the number of blows Ivan makes. If the battle will continue forever, print in the first line ""Draw"".
Input: 2 2 421 00 130 10 10 0 | Output: Ivan2
Hard
3
1,720
722
299
0
1,690
G
1690G
G. Count the Trains
2,000
binary search; data structures; greedy; sortings
There are \(n\) of independent carriages on the rails. The carriages are numbered from left to right from \(1\) to \(n\). The carriages are not connected to each other. The carriages move to the left, so that the carriage with number \(1\) moves ahead of all of them.The \(i\)-th carriage has its own engine, which can a...
The first line of input data contains a single integer \(t\) (\(1 \le t \le 10^4\)) —the number of input test cases.This is followed by descriptions of the test cases.The first line of each test case is empty.The second line of the test case contains two integers \(n\) and \(m\) (\(1 \le n,m \le 10^5\)) —the number of ...
Print \(t\) lines. On each line print the answer for the corresponding test case.For each test case print \(m\) numbers: the number of trains formed after each message.
For the first test case: Initially array \(a = [6, 2, 3, 7]\). After the first message, the array \(a = [6, 2, 1, 7]\). Accordingly, the speeds of the carriages are \([6, 2, 1, 1]\) and will form \(3\) of the train. After the second message the array \(a = [6, 2, 1, 0]\). Accordingly, the speeds of the carriages are \(...
Input: 34 26 2 3 73 24 75 410 13 5 2 62 45 21 53 213 4769 514 336 173 181 373 519 338 985 709 729 702 16812 5816 2227 2335 117 | Output: 3 4 4 4 2 3 5 6 6 5
Hard
4
1,236
1,220
168
16
439
B
439B
B. Devu, the Dumb Guy
1,200
implementation; sortings
Devu is a dumb guy, his learning curve is very slow. You are supposed to teach him n subjects, the ith subject has ci chapters. When you teach him, you are supposed to teach all the chapters of a subject continuously.Let us say that his initial per chapter learning power of a subject is x hours. In other words he can l...
The first line will contain two space separated integers n, x (1 ≤ n, x ≤ 105). The next line will contain n space separated integers: c1, c2, ..., cn (1 ≤ ci ≤ 105).
Output a single integer representing the answer to the problem.
Look at the first example. Consider the order of subjects: 1, 2. When you teach Devu the first subject, it will take him 3 hours per chapter, so it will take 12 hours to teach first subject. After teaching first subject, his per chapter learning time will be 2 hours. Now teaching him second subject will take 2 × 1 = 2 ...
Input: 2 34 1 | Output: 11
Easy
2
991
166
63
4
2,057
E2
2057E2
E2. Another Exercise on Graphs (hard version)
2,500
binary search; dfs and similar; dp; dsu; graphs; shortest paths; sortings
This is the hard version of the problem. The difference between the versions is that in this version, there is no additional constraint on \(m\). You can hack only if you solved all versions of this problem. Recently, the instructors of ""T-generation"" needed to create a training contest. They were missing one problem...
Each test contains multiple test cases. The first line 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 set of test case contains three integers \(n, m\) and \(q\) (\(2 \le n \le 400\), \(n - 1 \le m \le \frac{n \cdot (n -...
For each set of test case, output the answers to all questions.
In the first set of test cases, one of the optimal paths in the first query is the path \(1 \rightarrow 3 \rightarrow 4\); the \(2\)-nd maximum weight of the edges on this path is \(1\). In the second query, one of the optimal paths is \(2 \rightarrow 4 \rightarrow 3\); \(1\)-st maximum weight of the edges is \(2\).In ...
Input: 34 4 21 2 22 4 21 3 43 4 11 4 22 3 16 7 31 2 102 3 33 4 94 5 25 6 12 4 104 6 101 6 31 6 22 4 111 17 101 4 51 3 191 2 103 2 134 5 14 6 113 5 93 6 182 7 175 8 155 10 86 9 47 10 207 8 168 11 39 11 610 11 143 11 13 11 31 11 11 11 41 11 38 2 210 4 13 9 23 9 16 7 3 | Output: 1 2 2 9 9 11 3 11 1 3 10 8 4 11 4
Expert
7
1,151
1,224
63
20