contest_id
int32
1
2.13k
index
stringclasses
62 values
problem_id
stringlengths
2
6
title
stringlengths
0
67
rating
int32
0
3.5k
tags
stringlengths
0
139
statement
stringlengths
0
6.96k
input_spec
stringlengths
0
2.32k
output_spec
stringlengths
0
1.52k
note
stringlengths
0
5.06k
sample_tests
stringlengths
0
1.02k
difficulty_category
stringclasses
6 values
tag_count
int8
0
11
statement_length
int32
0
6.96k
input_spec_length
int16
0
2.32k
output_spec_length
int16
0
1.52k
contest_year
int16
0
21
1,878
D
1878D
D. Reverse Madness
1,600
data structures; greedy
You are given a string \(s\) of length \(n\), containing lowercase Latin letters. Next you will be given a positive integer \(k\) and two arrays, \(l\) and \(r\) of length \(k\).It is guaranteed that the following conditions hold for these 2 arrays: \(l_1 = 1\); \(r_k = n\); \(l_i \le r_i\), for each positive integer \...
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. Description of the test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 2\cdot 10^5\)) — the length of the string \(s\), and...
For each test case, in a new line, output the string \(s\) after the last modification is done.
In the first test case:The initial string is ""abcd"". In the first modification, we have \(x=1\). Since \(l_1=1\leq x \leq r_1=2\), we find the index \(i = 1\). We reverse the substring from index \(x=1\) to \(l_1+r_1-x=1+2-1=2\). After this modification, our string is ""bacd"". In the second modification (and the las...
Input: 54 2abcd1 32 421 35 3abcde1 2 31 2 531 2 33 1gaf1322 210 1aghcdegdij11051 2 3 4 21 1a1111 | Output: badc abedc gaf jihgedcdga a
Medium
2
1,068
1,327
95
18
1,784
E
1784E
E. Infinite Game
3,100
brute force; combinatorics; dp; games; probabilities
Alice and Bob are playing an infinite game consisting of sets. Each set consists of rounds. In each round, one of the players wins. The first player to win two rounds in a set wins this set. Thus, a set always ends with the score of \(2:0\) or \(2:1\) in favor of one of the players.Let's call a game scenario a finite s...
The only line contains a single string \(s\) (\(1 \le |s| \le 200\)), consisting of characters 'a', 'b', and '?'.
Print three integers: how many ways result in a scenario winning for Alice, how many result in a tied scenario, and how many result in a scenario winning for Bob, modulo \(998\,244\,353\).
In the first example, there are four ways to replace the question marks: \(s = \mathtt{aa}\): Alice wins every set \(2:0\) — the scenario is winning for Alice; \(s = \mathtt{ab}\): Alice and Bob win sets in turns, with the score of \(2:1\) each — the scenario is tied; \(s = \mathtt{ba}\): Bob and Alice win sets in turn...
Input: ?? | Output: 1 2 1
Master
5
1,609
113
188
17
1,732
D2
1732D2
D2. Balance (Hard version)
2,400
brute force; data structures; number theory
This is the hard version of the problem. The only difference is that in this version there are remove queries.Initially you have a set containing one element — \(0\). You need to handle \(q\) queries of the following types:+ \(x\) — add the integer \(x\) to the set. It is guaranteed that this integer is not contained i...
The first line contains an integer \(q\) (\(1 \leq q \leq 2 \cdot 10^5\)) — the number of queries.The following \(q\) lines describe the queries.An addition query of integer \(x\) is given in the format + \(x\) (\(1 \leq x \leq 10^{18}\)). It is guaranteed that \(x\) is not contained in the set.A remove query of intege...
For each query of type ? output a single integer — the \(k\text{-mex}\) of the set.
In the first example:After the first and second queries, the set will contain elements \(\{0, 1, 2\}\). The smallest non-negative number that is divisible by \(1\) and is not in the set is \(3\).After the fourth query, the set will contain the elements \(\{0, 1, 2, 4\}\). The smallest non-negative number that is divisi...
Input: 18+ 1+ 2? 1+ 4? 2+ 6? 3+ 7+ 8? 1? 2+ 5? 1+ 1000000000000000000? 1000000000000000000- 4? 1? 2 | Output: 3 6 3 3 10 3 2000000000000000000 3 4
Expert
3
664
594
83
17
1,352
C
1352C
C. K-th Not Divisible by n
1,200
binary search; math
You are given two positive integers \(n\) and \(k\). Print the \(k\)-th positive integer that is not divisible by \(n\).For example, if \(n=3\), and \(k=7\), then all numbers that are not divisible by \(3\) are: \(1, 2, 4, 5, 7, 8, 10, 11, 13 \dots\). The \(7\)-th number among them is \(10\).
The first line contains an integer \(t\) (\(1 \le t \le 1000\)) — the number of test cases in the input. Next, \(t\) test cases are given, one per line.Each test case is two positive integers \(n\) (\(2 \le n \le 10^9\)) and \(k\) (\(1 \le k \le 10^9\)).
For each test case print the \(k\)-th positive integer that is not divisible by \(n\).
Input: 6 3 7 4 12 2 1000000000 7 97 1000000000 1000000000 2 1 | Output: 10 15 1999999999 113 1000000001 1
Easy
2
293
254
86
13
614
A
614A
A. Link/Cut Tree
1,500
brute force; implementation
Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, which is based on Splay trees. Specifically, he is now studying the expose procedure.Unfortunately, Rostislav is unable to understand the definition of this procedure, so he decided to ask programmer Serezha to help him. Serezha agreed t...
The first line of the input contains three space-separated integers l, r and k (1 ≤ l ≤ r ≤ 1018, 2 ≤ k ≤ 109).
Print all powers of number k, that lie within range from l to r in the increasing order. If there are no such numbers, print ""-1"" (without the quotes).
Note to the first sample: numbers 20 = 1, 21 = 2, 22 = 4, 23 = 8 lie within the specified range. The number 24 = 16 is greater then 10, thus it shouldn't be printed.
Input: 1 10 2 | Output: 1 2 4 8
Medium
2
665
111
153
6
1,143
C
1143C
C. Queen
1,400
dfs and similar; trees
You are given a rooted tree with vertices numerated from \(1\) to \(n\). A tree is a connected graph without cycles. A rooted tree has a special vertex named root.Ancestors of the vertex \(i\) are all vertices on the path from the root to the vertex \(i\), except the vertex \(i\) itself. The parent of the vertex \(i\) ...
The first line contains a single integer \(n\) (\(1 \le n \le 10^5\)) — the number of vertices in the tree.The next \(n\) lines describe the tree: the \(i\)-th line contains two integers \(p_i\) and \(c_i\) (\(1 \le p_i \le n\), \(0 \le c_i \le 1\)), where \(p_i\) is the parent of the vertex \(i\), and \(c_i = 0\), if ...
In case there is at least one vertex to delete, print the only line containing the indices of the vertices you will delete in the order you delete them. Otherwise print a single integer \(-1\).
The deletion process in the first example is as follows (see the picture below, the vertices with \(c_i=1\) are in yellow): first you will delete the vertex \(1\), because it does not respect ancestors and all its children (the vertex \(2\)) do not respect it, and \(1\) is the smallest index among such vertices; the ve...
Input: 5 3 1 1 1 -1 0 2 1 3 0 | Output: 1 2 4
Easy
2
1,582
606
193
11
1,628
A
1628A
A. Meximum Array
1,400
binary search; constructive algorithms; greedy; implementation; math; two pointers
Mihai has just learned about the MEX concept and since he liked it so much, he decided to use it right away.Given an array \(a\) of \(n\) non-negative integers, Mihai wants to create a new array \(b\) that is formed in the following way:While \(a\) is not empty: Choose an integer \(k\) (\(1 \leq k \leq |a|\)). Append t...
The first line of the input contains a single integer \(t\) (\(1 \le t \le 100\)) — the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) — the number of elements in the array \(a\).The second line of each test c...
For each test case print \(m\) — the length of the maximum array \(b\) Mihai can create, followed by \(m\) integers denoting the elements of the array \(b\).
In the first test case, the lexicographically maximum array \(b\) is obtained by selecting \(k=5\), resulting in the \(MEX\) of the whole array \(a\). It is lexicographically maximum because an array starting with a smaller number than \(4\) is lexicographically smaller, and choosing a \(k<5\) would result in an array ...
Input: 651 0 2 0 382 2 3 4 0 1 2 01150 1 2 3 440 1 1 0100 0 2 1 1 1 0 0 1 1 | Output: 1 4 2 5 1 1 0 1 5 2 2 2 4 3 2 2 0
Easy
6
1,167
558
157
16
1,938
F
1938F
2,400
Expert
0
0
0
0
19
144
C
144C
C. Anagram Search
1,500
implementation; strings
A string t is called an anagram of the string s, if it is possible to rearrange letters in t so that it is identical to the string s. For example, the string ""aab"" is an anagram of the string ""aba"" and the string ""aaa"" is not.The string t is called a substring of the string s if it can be read starting from some ...
The first line is non-empty string s, consisting of no more than 105 lowercase Latin letters and characters ""?"". The second line is non-empty string p, consisting of no more than 105 lowercase Latin letters. Please note that the length of the string p can exceed the length of the string s.
Print the single number representing the number of good substrings of string s.Two substrings are considered different in their positions of occurrence are different. Thus, if some string occurs several times, then it should be counted the same number of times.
Consider the first sample test. Here the string s has two good substrings: ""b??"" (after we replace the question marks we get ""baa""), ""???"" (after we replace the question marks we get ""baa"").Let's consider the second sample test. Here the string s has two good substrings: ""ab?"" (""?"" can be replaced by ""c"")...
Input: bb??x???aab | Output: 2
Medium
2
1,051
292
261
1
776
C
776C
C. Molly's Chemicals
1,800
binary search; brute force; data structures; implementation; math
Molly Hooper has n different kinds of chemicals arranged in a line. Each of the chemicals has an affection value, The i-th of them has affection value ai.Molly wants Sherlock to fall in love with her. She intends to do this by mixing a contiguous segment of chemicals together to make a love potion with total affection ...
The first line of input contains two integers, n and k, the number of chemicals and the number, such that the total affection value is a non-negative power of this number k. (1 ≤ n ≤ 105, 1 ≤ |k| ≤ 10).Next line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — affection values of chemicals.
Output a single integer — the number of valid segments.
Do keep in mind that k0 = 1.In the first sample, Molly can get following different affection values: 2: segments [1, 1], [2, 2], [3, 3], [4, 4]; 4: segments [1, 2], [2, 3], [3, 4]; 6: segments [1, 3], [2, 4]; 8: segments [1, 4]. Out of these, 2, 4 and 8 are powers of k = 2. Therefore, the answer is 8.In the second samp...
Input: 4 22 2 2 2 | Output: 8
Medium
5
550
300
55
7
1,842
F
1842F
F. Tenzing and Tree
2,500
dfs and similar; greedy; shortest paths; sortings; trees
Tenzing has an undirected tree of \(n\) vertices.Define the value of a tree with black and white vertices in the following way. The value of an edge is the absolute difference between the number of black nodes in the two components of the tree after deleting the edge. The value of the tree is the sum of values over all...
The first line of the input contains a single integer \(n\) (\(1\leq n\leq 5000\)) — the number of vertices.The following \(n-1\) lines of the input contains \(2\) integers \(u_i\) and \(v_i\) (\(1 \leq u_i, v_i \leq n, u_i \neq v_i\)) — indicating an edge between vertices \(u_i\) and \(v_i\). It is guaranteed that the...
Output \(n+1\) numbers. The \(i\)-th number is the answer of \(k=i-1\).
Consider the first example. When \(k=2\), Tenzing can paint vertices \(1\) and \(2\) black then the value of edge \((1,2)\) is 0, and the values of other edges are all equal to \(2\). So the value of that tree is \(4\).
Input: 4 1 2 3 2 2 4 | Output: 0 3 4 5 6
Expert
5
501
345
71
18
59
D
59D
D. Team Arrangement
2,000
constructive algorithms; greedy; implementation
Recently personal training sessions have finished in the Berland State University Olympiad Programmer Training Centre. By the results of these training sessions teams are composed for the oncoming team contest season. Each team consists of three people. All the students of the Centre possess numbers from 1 to 3n, and a...
The first line contains an integer n (1 ≤ n ≤ 105) which is the number of resulting teams. The second line contains 3n space-separated integers from 1 to 3n which are the results of personal training sessions. It is guaranteed that every student appears in the results exactly once.Then follow n lines each containing th...
Print 3n - 1 numbers — the lexicographically smallest list of priorities for the student number k. The lexicographical comparison is performed by the standard < operator in modern programming languages. The list a is lexicographically less that the list b if exists such an i (1 ≤ i ≤ 3n), that ai < bi, and for any j (1...
Input: 35 4 1 2 6 3 7 8 95 6 29 3 41 7 84 | Output: 2 3 5 6 9 1 7 8
Hard
3
1,293
856
489
0
1,366
G
1366G
G. Construct the String
2,700
data structures; dp; strings
Let's denote the function \(f(s)\) that takes a string \(s\) consisting of lowercase Latin letters and dots, and returns a string consisting of lowercase Latin letters as follows: let \(r\) be an empty string; process the characters of \(s\) from left to right. For each character \(c\), do the following: if \(c\) is a ...
The input consists of two lines: the first one contains \(s\) — a string consisting of lowercase Latin letters and dots, the second one contains \(t\) — a string consisting of lowercase Latin letters (\(1 \le |t| \le |s| \le 10000\)).Additional constraint on the input: it is possible to remove some number of characters...
Print one integer — the minimum possible number of characters you have to delete from \(s\) so \(f(s)\) does not crash and returns \(t\) as the result of the function.
Input: a.ba.b. abb | Output: 2
Master
3
825
353
167
13
1,762
E
1762E
E. Tree Sum
2,600
combinatorics; math; trees
Let us call an edge-weighted tree with \(n\) vertices numbered from \(1\) to \(n\) good if the weight of each edge is either \(1\) or \(-1\) and for each vertex \(i\), the product of the edge weights of all edges having \(i\) as one endpoint is \(-1\).You are given a positive integer \(n\). There are \(n^{n-2} \cdot 2^...
The first and only line of input contains a single integer \(n\) (\(1 \leq n \leq 5 \cdot 10^5\)).
The only line of output should contain a single integer, the required answer, modulo \(998\,244\,353\).
In the first test case, there is only \(1\) distinct good tree. The value of \(d(1,2)\) for that tree is \(-1\), which is \(998\,244\,352\) under modulo \(998\,244\,353\).In the second test case, the value of \(d(1,1)\) for any tree is \(0\), so the answer is \(0\).In the third test case, there are \(16\) distinct good...
Input: 2 | Output: 998244352
Expert
3
1,400
98
103
17
567
A
567A
A. Lineland Mail
900
greedy; implementation
All cities of Lineland are located on the Ox coordinate axis. Thus, each city is associated with its position xi — a coordinate on the Ox axis. No two cities are located at a single point.Lineland residents love to send letters to each other. A person may send a letter only if the recipient lives in another city (becau...
The first line of the input contains integer n (2 ≤ n ≤ 105) — the number of cities in Lineland. The second line contains the sequence of n distinct integers x1, x2, ..., xn ( - 109 ≤ xi ≤ 109), where xi is the x-coordinate of the i-th city. All the xi's are distinct and follow in ascending order.
Print n lines, the i-th line must contain two integers mini, maxi, separated by a space, where mini is the minimum cost of sending a letter from the i-th city, and maxi is the maximum cost of sending a letter from the i-th city.
Input: 4-5 -2 2 7 | Output: 3 123 94 75 12
Beginner
2
749
298
228
5
494
B
494B
B. Obsessive String
2,000
dp; strings
Hamed has recently found a string t and suddenly became quite fond of it. He spent several days trying to find all occurrences of t in other strings he had. Finally he became tired and started thinking about the following problem. Given a string s how many ways are there to extract k ≥ 1 non-overlapping substrings from...
Input consists of two lines containing strings s and t (1 ≤ |s|, |t| ≤ 105). Each string consists of lowercase Latin letters.
Print the answer in a single line.
Input: ababaaba | Output: 5
Hard
2
692
125
34
4
363
B
363B
B. Fence
1,100
brute force; dp
There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights. Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1] Polycarpus has bought a posh piano and is th...
The first line of the input contains integers n and k (1 ≤ n ≤ 1.5·105, 1 ≤ k ≤ n) — the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 ≤ hi ≤ 100), where hi is the height of the i-th plank of the fence.
Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them.
In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8.
Input: 7 31 2 6 1 1 7 1 | Output: 3
Easy
2
823
296
159
3
1,302
H
1302H
H. Who needs suffix structures?
0
This is an unusual problem in an unusual contest, here is the announcement: http://codeforces.com/blog/entry/73543You run a string shop. During a day your customers want to buy strings of certain lengths and sometimes satisfying other properties.You have a string of length \(n\) and can cut a string of any length start...
The first line contains two integers \(n\) and \(q\) (\(1\leq q, n\leq 200\,000\)). The second line contains \(n\) space-separated integers \(a_1\), ..., \(a_n\) (\(0\leq a_i < 987\,898\,789\)) which denote the letters of string \(s\), and each of the following \(q\) lines contains three integers \(len\), \(pos_1\) and...
Print \(q\) lines, \(i\)-th of them containing Yes if the two substrings in the \(i\)-th query (of length \(len\) starting from \(pos_1\) and \(pos_2\)) are equal and No otherwise.
Input: 5 2 1 2 3 1 2 2 1 4 3 1 3 | Output: Yes No
Beginner
0
686
426
180
13
164
D
164D
D. Minimum Diameter
3,100
binary search; brute force
You are given n points on the plane. You need to delete exactly k of them (k < n) so that the diameter of the set of the remaining n - k points were as small as possible. The diameter of a set of points is the maximum pairwise distance between the points of the set. The diameter of a one point set equals zero.
The first input line contains a pair of integers n, k (2 ≤ n ≤ 1000, 1 ≤ k ≤ 30, k < n) — the numbers of points on the plane and the number of points to delete, correspondingly.Next n lines describe the points, one per line. Each description consists of a pair of integers xi, yi (0 ≤ xi, yi ≤ 32000) — the coordinates o...
Print k different space-separated integers from 1 to n — the numbers of points to delete. The points are numbered in the order, in which they are given in the input from 1 to n. You can print the numbers in any order. If there are multiple solutions, print any of them.
Input: 5 21 20 02 21 13 3 | Output: 5 2
Master
2
311
368
269
1
1,804
C
1804C
C. Pull Your Luck
1,500
brute force; greedy; math; number theory
While James is gone on business, Vesper takes her time and explores what the legendary Casino Royale has to offer to people who are fond of competitive programming.Her attention was grabbed by the very new ""Pull Your Luck"" roulette which functions in a pretty peculiar way. The roulette's wheel consists of \(n\) secto...
The first line of the input contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) — the number of test cases. Then follow \(t\) lines containing one test description each.Each test description consists of three integers \(n\), \(x\) and \(p\) (\(3 \leq n \leq 10^5\), \(0 \leq x < n\), \(1 \leq p \leq 10^9\)). They a...
Print \(t\) lines, the \(i\)-th line should contain the answer for the \(i\)-th test case. If it is possible to pull the handle with the integer force from \(1\) to \(p\) in order to make sector \(0\) win, print ""Yes"". Otherwise, print ""No"".You can output the answer in any case (upper or lower). For example, the st...
In the first example, the only possible way to pull the handle is with force \(1\). That is not enough to make the arrow point at sector \(0\), at least force \(2\) is required to do so.In the second example, Vesper can pull the handle with the force \(2\) so the wheel will spin \(2 + 1 = 3\) sectors ahead and the arro...
Input: 75 2 15 2 210 0 10011 7 1003 1 100031 0 10100 49 7 | Output: No Yes Yes Yes No No No
Medium
4
1,629
563
406
18
1,951
C
1951C
C. Ticket Hoarding
1,400
greedy; math; sortings
Maître Gims - Est-ce que tu m'aimes ?ඞAs the CEO of a startup company, you want to reward each of your \(k\) employees with a ticket to the upcoming concert. The tickets will be on sale for \(n\) days, and by some time travelling, you have predicted that the price per ticket at day \(i\) will be \(a_i\). However, to pr...
Each test contains multiple test cases. The first line contains an 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 three integers \(n\), \(m\), and \(k\) (\(1 \le n \le 3 \cdot 10^5, 1 \le m \le 10^9, 1 \le k \le \min(n...
For each test case, print one integer: the minimum amount of money needed to purchase exactly \(k\) tickets.
In the first test case, one optimal way to buy \(3\) tickets is as follows: Buy \(0\) tickets on the first day. The prices per ticket for the remaining days are \([6, 4, 2]\). Buy \(0\) tickets on the second day. The prices per ticket for the remaining days are \([4, 2]\). Buy \(1\) ticket on the third day with cost \(...
Input: 44 2 38 6 4 24 2 88 6 4 25 100 110000 1 100 10 10006 3 95 5 5 5 5 5 | Output: 10 64 1 72
Easy
3
1,024
720
108
19
794
B
794B
B. Cutting Carrot
1,200
geometry; math
Igor the analyst has adopted n little bunnies. As we all know, bunnies love carrots. Thus, Igor has bought a carrot to be shared between his bunnies. Igor wants to treat all the bunnies equally, and thus he wants to cut the carrot into n pieces of equal area. Formally, the carrot can be viewed as an isosceles triangle ...
The first and only line of input contains two space-separated integers, n and h (2 ≤ n ≤ 1000, 1 ≤ h ≤ 105).
The output should contain n - 1 real numbers x1, x2, ..., xn - 1. The number xi denotes that the i-th cut must be made xi units away from the apex of the carrot. In addition, 0 < x1 < x2 < ... < xn - 1 < h must hold. Your output will be considered correct if absolute or relative error of every number in your output doe...
Definition of isosceles triangle: https://en.wikipedia.org/wiki/Isosceles_triangle.
Input: 3 2 | Output: 1.154700538379 1.632993161855
Easy
2
637
108
437
7
1,391
A
1391A
A. Suborrays
800
constructive algorithms; math
A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array) and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the arra...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). Description of the test cases follows.The first and only line of every test case contains a single integer \(n\) (\(1 \le n \le 100\)).
For every test, output any good permutation of length \(n\) on a separate line.
For \(n = 3\), \([3,1,2]\) is a good permutation. Some of the subarrays are listed below. \(3\text{ OR }1 = 3 \geq 2\) \((i = 1,j = 2)\) \(3\text{ OR }1\text{ OR }2 = 3 \geq 3\) \((i = 1,j = 3)\) \(1\text{ OR }2 = 3 \geq 2\) \((i = 2,j = 3)\) \(1 \geq 1\) \((i = 2,j = 2)\) Similarly, you can verify that \([4,3,5,2,7,1,...
Input: 3 1 3 7 | Output: 1 3 1 2 4 3 5 2 7 1 6
Beginner
2
973
253
79
13
689
C
689C
C. Mike and Chocolate Thieves
1,700
binary search; combinatorics; math
Bad news came to Mike's village, some thieves stole a bunch of chocolates from the local factory! Horrible! Aside from loving sweet things, thieves from this area are known to be very greedy. So after a thief takes his number of chocolates for himself, the next thief will take exactly k times more than the previous one...
The single line of input contains the integer m (1 ≤ m ≤ 1015) — the number of ways the thieves might steal the chocolates, as rumours say.
Print the only integer n — the maximum amount of chocolates that thieves' bags can carry. If there are more than one n satisfying the rumors, print the smallest one.If there is no such n for a false-rumoured m, print - 1.
In the first sample case the smallest n that leads to exactly one way of stealing chocolates is n = 8, whereas the amounts of stealed chocolates are (1, 2, 4, 8) (the number of chocolates stolen by each of the thieves).In the second sample case the smallest n that leads to exactly 8 ways is n = 54 with the possibilitie...
Input: 1 | Output: 8
Medium
3
1,102
139
221
6
869
C
869C
C. The Intriguing Obsession
1,800
combinatorics; dp; math
— This is not playing but duty as allies of justice, Nii-chan!— Not allies but justice itself, Onii-chan!With hands joined, go everywhere at a speed faster than our thoughts! This time, the Fire Sisters — Karen and Tsukihi — is heading for somewhere they've never reached — water-surrounded islands!There are three clust...
The first and only line of input contains three space-separated integers a, b and c (1 ≤ a, b, c ≤ 5 000) — the number of islands in the red, blue and purple clusters, respectively.
Output one line containing an integer — the number of different ways to build bridges, modulo 998 244 353.
In the first example, there are 3 bridges that can possibly be built, and no setup of bridges violates the restrictions. Thus the answer is 23 = 8.In the second example, the upper two structures in the figure below are instances of valid ones, while the lower two are invalid due to the blue and purple clusters, respect...
Input: 1 1 1 | Output: 8
Medium
3
1,203
181
106
8
1,416
F
1416F
F. Showing Off
3,300
flows; graph matchings; greedy; implementation
Another dull quarantine day was going by when BThero decided to start researching matrices of size \(n \times m\). The rows are numerated \(1\) through \(n\) from top to bottom, and the columns are numerated \(1\) through \(m\) from left to right. The cell in the \(i\)-th row and \(j\)-th column is denoted as \((i, j)\...
The first line of input file contains a single integer \(T\) (\(1 \le T \le 100\)) denoting the number of test cases. The description of \(T\) test cases follows.First line of a test case contains two integers \(n\) and \(m\) (\(1 \le n \cdot m \le 10^5\)).Each of the following \(n\) lines contain exactly \(m\) integer...
For each test case, if an answer does not exist, print a single word NO. Otherwise, print YES and both matrices in the same format as in the input. The first matrix should be the cost matrix and the second matrix should be the direction matrix. All integers in the cost matrix should be positive. All characters in the d...
Input: 2 3 4 7 6 7 8 5 5 4 4 5 7 4 4 1 1 5 | Output: YES 1 1 1 1 2 1 1 1 3 2 1 1 R D L L D R D L U L R U NO
Master
4
1,421
500
401
14
917
E
917E
E. Upside Down
3,400
data structures; string suffix structures; strings; trees
As we all know, Eleven has special abilities. Thus, Hopper convinced her to close the gate to the Upside Down World with her mind. Upside down monsters like to move between the worlds, so they are going to attack Hopper and Eleven in order to make them stop. The monsters live in the vines. The vines form a tree with n ...
The first line of input contains three integers, n, m and q (2 ≤ n ≤ 105, 1 ≤ m, q ≤ 105).The next n - 1 lines contain the tunnels (edges). Each line contains two integers v and u (1 ≤ v, u ≤ n, v ≠ u) and a lowercase English letter c, meaning there's a tunnel connecting junctions v and u written c in it. It is guarant...
Print q lines. i-th line should contain a single integer, the power the i-th monster gains after moving.
Input: 6 4 51 6 b2 3 a1 2 b5 3 b4 5 babbbaa1 2 16 2 31 6 24 5 41 6 2 | Output: 01101
Master
4
1,230
757
104
9
1,753
C
1753C
C. Wish I Knew How to Sort
2,000
dp; math; probabilities
You are given a binary array \(a\) (all elements of the array are \(0\) or \(1\)) of length \(n\). You wish to sort this array, but unfortunately, your algorithms teacher forgot to teach you sorting algorithms. You perform the following operations until \(a\) is sorted: Choose two random indices \(i\) and \(j\) such th...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^5\)). Description of the test cases follows.The first line of each test case contains an integer \(n\) (\(1 \le n \le 200\,000\)) — the number of elements in the binary array.The second line of each test cas...
For each test case print one integer — the value \(p \cdot q^{-1} \bmod 998\,244\,353\).
Consider the first test case. If the pair of indices \((2, 3)\) will be chosen, these elements will be swapped and array will become sorted. Otherwise, if one of pairs \((1, 2)\) or \((1, 3)\) will be selected, nothing will happen. So, the probability that the array will become sorted after one operation is \(\frac{1}{...
Input: 330 1 050 0 1 1 161 1 1 0 0 1 | Output: 3 0 249561107
Hard
3
963
502
88
17
1,299
B
1299B
B. Aerodynamic
1,800
geometry
Guy-Manuel and Thomas are going to build a polygon spaceship. You're given a strictly convex (i. e. no three points are collinear) polygon \(P\) which is defined by coordinates of its vertices. Define \(P(x,y)\) as a polygon obtained by translating \(P\) by vector \(\overrightarrow {(x,y)}\). The picture below depicts ...
The first line of input will contain a single integer \(n\) (\(3 \le n \le 10^5\)) — the number of points.The \(i\)-th of the next \(n\) lines contains two integers \(x_i, y_i\) (\(|x_i|, |y_i| \le 10^9\)), denoting the coordinates of the \(i\)-th vertex.It is guaranteed that these points are listed in counterclockwise...
Output ""YES"" in a separate line, if \(P\) and \(T\) are similar. Otherwise, output ""NO"" in a separate line. You can print each letter in any case (upper or lower).
The following image shows the first sample: both \(P\) and \(T\) are squares. The second sample was shown in the statements.
Input: 4 1 0 4 1 3 4 0 3 | Output: YES
Medium
1
1,074
375
167
12
4
A
4A
A. Watermelon
800
brute force; math
One hot summer day Pete and his friend Billy decided to buy a watermelon. They chose the biggest and the ripest one, in their opinion. After that the watermelon was weighed, and the scales showed w kilos. They rushed home, dying of thirst, and decided to divide the berry, however they faced a hard problem.Pete and Bill...
The first (and the only) input line contains integer number w (1 ≤ w ≤ 100) — the weight of the watermelon bought by the boys.
Print YES, if the boys can divide the watermelon into two parts, each of them weighing even number of kilos; and NO in the opposite case.
For example, the boys can divide the watermelon into two parts of 2 and 6 kilos respectively (another variant — two parts of 4 and 4 kilos).
Input: 8 | Output: YES
Beginner
2
775
126
137
0
497
E
497E
E. Subsequences Return
2,900
dp; matrices
Assume that sk(n) equals the sum of digits of number n in the k-based notation. For example, s2(5) = s2(1012) = 1 + 0 + 1 = 2, s3(14) = s3(1123) = 1 + 1 + 2 = 4.The sequence of integers a0, ..., an - 1 is defined as . Your task is to calculate the number of distinct subsequences of sequence a0, ..., an - 1. Calculate t...
The first line contains two space-separated numbers n and k (1 ≤ n ≤ 1018, 2 ≤ k ≤ 30).
In a single line print the answer to the problem modulo 109 + 7.
In the first sample the sequence ai looks as follows: (0, 1, 1, 0). All the possible subsequences are: (), (0), (0, 0), (0, 1), (0, 1, 0), (0, 1, 1), (0, 1, 1, 0), (1), (1, 0), (1, 1), (1, 1, 0).In the second sample the sequence ai looks as follows: (0, 1, 2, 3, 4, 5, 6). The subsequences of this sequence are exactly a...
Input: 4 2 | Output: 11
Master
2
627
87
64
4
1,253
E
1253E
E. Antenna Coverage
2,200
data structures; dp; greedy; sortings
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the \((Ox)\) axis.On this street, there are \(n\) antennas, numbered from \(1\) to \(n\). The \(i\)-th antenna lies on the position \(x_i\) and has an initial scope of \(s_i\): it covers all integer positions inside the inte...
The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 80\) and \(n \le m \le 100\ 000\)).The \(i\)-th of the next \(n\) lines contains two integers \(x_i\) and \(s_i\) (\(1 \le x_i \le m\) and \(0 \le s_i \le m\)).On each position, there is at most one antenna (values \(x_i\) are pairwise distinct).
You have to output a single integer: the minimum amount of coins required to make all integer positions from \(1\) to \(m\) inclusive covered by at least one antenna.
In the first example, here is a possible strategy: Increase the scope of the first antenna by \(40\), so that it becomes \(2 + 40 = 42\). This antenna will cover interval \([43 - 42; 43 + 42]\) which is \([1; 85]\) Increase the scope of the second antenna by \(210\), so that it becomes \(4 + 210 = 214\). This antenna w...
Input: 3 595 43 2 300 4 554 10 | Output: 281
Hard
4
831
314
166
12
188
D
188D
D. Asterisks
1,100
*special; implementation
You are given a number n. Print n lines, i-th line should consist of i characters ""*"". Lines' indices are 1-based.
The only line of input contains an integer n (1 ≤ n ≤ 50).
Output the described pattern.
Input: 3 | Output: ******
Easy
2
116
58
29
1
2,065
H
2065H
H. Bro Thinks He's Him
2,200
combinatorics; data structures; divide and conquer; dp; math; matrices
Skibidus thinks he's Him! He proved it by solving this difficult task. Can you also prove yourself?Given a binary string\(^{\text{∗}}\) \(t\), \(f(t)\) is defined as the minimum number of contiguous substrings, each consisting of identical characters, into which \(t\) can be partitioned. For example, \(f(\texttt{001100...
The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) — the number of test cases.The first line of each test case contains a binary string \(s\) (\(1 \leq |s| \leq 2 \cdot 10^5\)).The following line of each test case contains an integer \(q\) (\(1 \leq q \leq 2 \cdot 10^5\)) — the number of queries.The foll...
For each test case, output \(q\) integers on a single line — the answer after each query modulo \(998\,244\,353\).
In the first test case, \(s\) becomes \(\texttt{001}\) after the first query. Let's calculate the answer for each subsequence: \(f(s_1) = f(\texttt{0}) = 1\) \(f(s_2) = f(\texttt{0}) = 1\) \(f(s_3) = f(\texttt{1}) = 1\) \(f(s_1 s_2) = f(\texttt{00}) = 1\) \(f(s_1 s_3) = f(\texttt{01}) = 2\) \(f(s_2 s_3) = f(\texttt{01}...
Input: 310121 31011031 2 310111010157 2 4 4 1 | Output: 10 7 61 59 67 1495 1169 1417 1169 1396
Hard
6
1,100
578
114
20
955
F
955F
F. Heaps
2,600
dp; trees
You're given a tree with n vertices rooted at 1.We say that there's a k-ary heap of depth m located at u if the following holds: For m = 1 u itself is a k-ary heap of depth 1. For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1. Denote dpk(u) as maximum de...
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105). The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.It's guaranteed that the given configuration forms a tree.
Output the answer to the task.
Consider sample case one.For k ≥ 3 all dpk will be equal to 1.For k = 2 dpk is 2 if and 1 otherwise.For k = 1 dpk values are (3, 1, 2, 1) respectively.To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
Input: 41 32 34 3 | Output: 21
Expert
2
398
238
30
9
888
B
888B
B. Buggy Robot
1,000
greedy
Ivan has a robot which is situated on an infinite grid. Initially the robot is standing in the starting cell (0, 0). The robot can process commands. There are four types of commands it can perform: U — move from the cell (x, y) to (x, y + 1); D — move from (x, y) to (x, y - 1); L — move from (x, y) to (x - 1, y); R — m...
The first line contains one number n — the length of sequence of commands entered by Ivan (1 ≤ n ≤ 100).The second line contains the sequence itself — a string consisting of n characters. Each character can be U, D, L or R.
Print the maximum possible number of commands from the sequence the robot could perform to end up in the starting cell.
Input: 4LDUR | Output: 4
Beginner
1
830
223
119
8
1,184
E2
1184E2
E2. Daleks' Invasion (medium)
2,100
dfs and similar; graphs; shortest paths; trees
After a successful field test, Heidi is considering deploying a trap along some Corridor, possibly not the first one. She wants to avoid meeting the Daleks inside the Time Vortex, so for abundance of caution she considers placing the traps only along those Corridors that are not going to be used according to the curren...
The first line: number \(n\) of destinations, number \(m\) of Time Corridors (\(2 \leq n \leq 10^5\), \(n - 1 \leq m \leq 10^6\)). The next \(m\) lines: destinations \(a\), \(b\) and energy \(e\) (\(1 \leq a, b \leq n\), \(a \neq b\), \(0 \leq e \leq 10^9\)).No pair \(\{a, b\}\) will repeat. The graph is guaranteed to ...
Output \(m-(n-1)\) lines, each containing one integer: \(E_{max}(c_i)\) for the \(i\)-th Corridor \(c_i\) from the input that is not part of the current Daleks' plan (minimum spanning tree).
If \(m = n-1\), then you need not output anything.
Input: 3 31 2 82 3 33 1 4 | Output: 4
Hard
4
834
377
190
11
1,154
C
1154C
C. Gourmet Cat
1,400
implementation; math
Polycarp has a cat and his cat is a real gourmet! Dependent on a day of the week he eats certain type of food: on Mondays, Thursdays and Sundays he eats fish food; on Tuesdays and Saturdays he eats rabbit stew; on other days of week he eats chicken stake. Polycarp plans to go on a trip and already packed his backpack. ...
The first line of the input contains three positive integers \(a\), \(b\) and \(c\) (\(1 \le a, b, c \le 7\cdot10^8\)) — the number of daily rations of fish food, rabbit stew and chicken stakes in Polycarps backpack correspondingly.
Print the maximum number of days the cat can eat in a trip without additional food purchases, if Polycarp chooses the day of the week to start his trip optimally.
In the first example the best day for start of the trip is Sunday. In this case, during Sunday and Monday the cat will eat fish food, during Tuesday — rabbit stew and during Wednesday — chicken stake. So, after four days of the trip all food will be eaten.In the second example Polycarp can start his trip in any day of ...
Input: 2 1 1 | Output: 4
Easy
2
752
232
162
11
1,610
G
1610G
G. AmShZ Wins a Bet
3,300
data structures; greedy; hashing
Right before the UEFA Euro 2020, AmShZ and Safar placed bets on who'd be the champion, AmShZ betting on Italy, and Safar betting on France.Of course, AmShZ won. Hence, Safar gave him a bracket sequence \(S\). Note that a bracket sequence is a string made of '(' and ')' characters.AmShZ can perform the following operati...
The only line of input contains a single string \(S\) — the string after the operations \((1\le |S|\le 3 \cdot 10^5)\).It is guaranteed that the first character of \(S\) is ')'.
Print the lexicographically smallest possible initial string before operations.
In the first sample, you can transform "")((())))"" into "")(()(())))"" by splitting it into "")("", empty string, and ""(())))"". It can be shown that this is the lexicographically smallest possible initial string
Input: )(()(()))) | Output: )((())))
Master
3
1,391
177
79
16
597
C
597C
C. Subsequences
1,900
data structures; dp
For the given sequence with n different elements find the number of increasing subsequences with k + 1 elements. It is guaranteed that the answer is not greater than 8·1018.
First line contain two integer values n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 10) — the length of sequence and the number of elements in increasing subsequences.Next n lines contains one integer ai (1 ≤ ai ≤ n) each — elements of sequence. All values ai are different.
Print one integer — the answer to the problem.
Input: 5 212354 | Output: 7
Hard
2
173
258
46
5
2,029
C
2029C
C. New Rating
1,700
binary search; data structures; dp; greedy
Hello, Codeforces Forcescode! Kevin used to be a participant of Codeforces. Recently, the KDOI Team has developed a new Online Judge called Forcescode. Kevin has participated in \(n\) contests on Forcescode. In the \(i\)-th contest, his performance rating is \(a_i\).Now he has hacked into the backend of Forcescode and ...
Each test contains multiple test cases. The first line of the input contains a single integer \(t\) (\(1\le t\le 5\cdot 10^4\)) — the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(1\le n\le 3\cdot 10^5\)) — the number of contests.The seco...
For each test case, output a single integer — the maximum possible rating after the recalculation if Kevin chooses the interval optimally.
In the first test case, Kevin must skip at least one contest. If he chooses any interval of length \(1\), his rating after the recalculation will be equal to \(5\).In the second test case, Kevin's optimal choice is to select the interval \([3,5]\). During the recalculation, his rating changes as follows:$$$\( 0 \xright...
Input: 561 2 3 4 5 671 2 1 1 1 3 41199 9 8 2 4 4 3 5 3101 2 3 4 1 3 2 1 1 10 | Output: 5 4 0 4 5
Medium
4
1,084
529
138
20
1,157
C1
1157C1
C1. Increasing Subsequence (easy version)
1,300
greedy
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).You are given a sequence \(a\) consisting of \(n\) integers. All these integers are distinct, each value from \(1\) to \(n\) appears in the sequence exactly once.You are mak...
The first line of the input contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) — the number of elements in \(a\).The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le n\)), where \(a_i\) is the \(i\)-th element of \(a\). All these integers are pairwise distinct.
In the first line of the output print \(k\) — the maximum number of elements in a strictly increasing sequence you can obtain.In the second line print a string \(s\) of length \(k\), where the \(j\)-th character of this string \(s_j\) should be 'L' if you take the leftmost element during the \(j\)-th move and 'R' other...
The first example is described in the problem statement.
Input: 5 2 1 5 4 3 | Output: 4 LRRR
Easy
1
1,065
310
375
11
958
E3
958E3
E3. Guard Duty (hard)
2,700
geometry
Now that Heidi knows that she can assign Rebel spaceships to bases (recall the easy subtask), she is asking you: how exactly to do this? Now, given positions of N spaceships and N bases on a plane, your task is to connect spaceships and bases with line segments so that: The segments do not intersect. Such a connection ...
The first line contains an integer N (1 ≤ n ≤ 10000). For 1 ≤ i ≤ N, the i + 1-th line contains two integers xi and yi (|xi|, |yi| ≤ 10000) denoting the coordinates of the i-th spaceship. The following N lines have the same format, denoting the position of bases. It is guaranteed that no two points coincide and no thre...
The output should have N lines. The i-th line should contain an integer pi, the index of the base to which the i-th spaceship is connected. The sequence p1, ..., pN should form a permutation of 1, ..., N.It is guaranteed that a solution exists. If there are multiple solutions, you can output any one of them.
Input: 46 65 12 44 05 41 22 13 5 | Output: 4123
Master
1
345
350
309
9
2,041
L
2041L
L. Building Castle
2,900
A-Ju has a gorgeous castle in which she often enjoys living. However, she gets tired of living in the castle for a long time. Thus, she decides to rebuild her castle into some specific shape to make her it more beautiful.Let's say we represent A-Ju's castle as a 2D convex polygon\(^{\text{∗}}\) on the 2D plane. A-Ju ai...
The first line contains an integer \(n\), representing the number of vertices of the polygon that forms A-Ju's castle.Then, \(n\) lines follow, the \(i\)-th of which contains two integers \(x_i, y_i\), representing the coordinates of the \(i\)-th vertex. The vertices are given in a counterclockwise order. \(3\leq n\leq...
Print a single real number in one line, representing the minimum area of the symmetric difference between the original castle and the new castle. Your answer will be accepted if the absolute or relative error does not exceed \(10^{-4}\). Formally, let your answer be \(a\), and the jury's answer be \(b\). Your answer is...
Input: 3 2 1 6 3 3 7 | Output: 3.666666666667
Master
0
2,090
479
384
20
188
F
188F
F. Binary Notation
1,400
*special; implementation
You are given a positive integer n. Output its binary notation.
The only line of input data contains an integer n (1 ≤ n ≤ 106).
Output the binary notation of n (without any leading zeros).
In the first example 5 = 1 * 22 + 0 * 21 + 1 * 20.
Input: 5 | Output: 101
Easy
2
63
64
60
1
297
E
297E
E. Mystic Carvings
3,000
data structures
The polar bears have discovered a gigantic circular piece of floating ice with some mystic carvings on it. There are n lines carved on the ice. Each line connects two points on the boundary of the ice (we call these points endpoints). The endpoints are numbered 1, 2, ..., 2n counter-clockwise along the circumference. N...
The first line contains integer n(3 ≤ n ≤ 105) — the number of lines.Each of the following n lines contains two integers ai, bi (1 ≤ ai, bi ≤ 2n), which means that there is a line carved on the ice connecting the ai–th and bi–th endpoint. It's guaranteed that each endpoints touches exactly one line.
Print the number of ways to build the caves.Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
The second sample corresponds to the figure in the problem statement.
Input: 45 41 26 78 3 | Output: 2
Master
1
2,128
300
193
2
1,834
F
1834F
F. Typewriter
2,500
brute force; math
Recently, Polycarp was given an unusual typewriter as a gift! Unfortunately, the typewriter was defective and had a rather strange design.The typewriter consists of \(n\) cells numbered from left to right from \(1\) to \(n\), and a carriage that moves over them. The typewriter cells contain \(n\) distinct integers from...
The first line contains a single integer \(n\) (\(1 \le n \le 4 \cdot 10^5\)) — the number of cells.The second line contains \(n\) distinct integers \(p_1, p_2, \ldots, p_n\) (\(1 \le p_i \le n\)) — the initial arrangement of integers in the cells.The third line contains a single integer \(q\) (\(0 \le q \le 4 \cdot 10...
Output \(q + 1\) numbers — the minimum number of carriage resets required before and after each of Polycarp's queries.
In the first example, the answer is \(1\). You can understand how the carriage works using this example. In the second example, the sequences for which the answer needs to be calculated look like this: Before all queries: \(1\ 2\ 3\) — the answer is \(0\). After shifting to the right by \(1\): \(3\ 1\ 2\) — the answer ...
Input: 3 2 3 1 0 | Output: 1
Expert
2
1,983
663
118
18
2,126
G1
2126G1
G1. Big Wins! (easy version)
2,200
binary search; data structures; dp; dsu; two pointers
This is the easy version of the problem. The difference between the versions is that in this version \(a_i \leq \min(n,100)\).You are given an array of \(n\) integers \(a_1, a_2, \dots, a_n\).Your task is to find a subarray \(a[l, r]\) (a continuous sequence of elements \(a_l, a_{l + 1}, \dots, a_r\)) for which the val...
The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.The first line of each test case contains one integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) — the length of the array.The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \leq a_i \leq \min(n, ...
For each test case, output a single integer — the maximum possible value of \(\text{med} - \min\) among all subarrays of the array.
In the first example, consider the array: \(a=[3,\ 2,\ 5,\ 3,\ 1]\) you can choose the subarray \(a[2,\ 3]\), which consists of the elements \([2,\ 5]\). The length of the subarray is \(2\). The median is the element at position \(\left\lceil \dfrac{3}{2} \right\rceil = 2\) in the sorted subarray. After sorting, we get...
Input: 553 2 5 3 144 1 1 376 1 3 4 6 2 744 2 3 151 2 3 4 5 | Output: 3 3 5 2 2
Hard
5
1,038
450
131
21
132
D
132D
D. Constants in the language of Shakespeare
2,100
constructive algorithms; dp; greedy
Shakespeare is a widely known esoteric programming language in which programs look like plays by Shakespeare, and numbers are given by combinations of ornate epithets. In this problem we will have a closer look at the way the numbers are described in Shakespeare.Each constant in Shakespeare is created from non-negative...
The only line of input contains a positive integer n, written as its binary notation. The length of the notation is at most 106. The first digit of the notation is guaranteed to be 1.
Output the required minimal m. After it output m lines. Each line has to be formatted as ""+2^x"" or ""-2^x"", where x is the power coefficient of the corresponding term. The order of the lines doesn't matter.
Input: 1111 | Output: 2+2^4-2^0
Hard
3
727
183
209
1
663
A
663A
A. Rebus
1,800
constructive algorithms; expression parsing; greedy; math
You are given a rebus of form ? + ? - ? + ? = n, consisting of only question marks, separated by arithmetic operation '+' and '-', equality and positive integer n. The goal is to replace each question mark with some positive integer from 1 to n, such that equality holds.
The only line of the input contains a rebus. It's guaranteed that it contains no more than 100 question marks, integer n is positive and doesn't exceed 1 000 000, all letters and integers are separated by spaces, arithmetic operations are located only between question marks.
The first line of the output should contain ""Possible"" (without quotes) if rebus has a solution and ""Impossible"" (without quotes) otherwise.If the answer exists, the second line should contain any valid rebus with question marks replaced by integers from 1 to n. Follow the format given in the samples.
Input: ? + ? - ? + ? + ? = 42 | Output: Possible9 + 13 - 39 + 28 + 31 = 42
Medium
4
271
275
306
6
908
G
908G
G. New Year and Original Order
2,800
dp; math
Let S(n) denote the number that represents the digits of n in sorted order. For example, S(1) = 1, S(5) = 5, S(50394) = 3459, S(353535) = 333555.Given a number X, compute modulo 109 + 7.
The first line of input will contain the integer X (1 ≤ X ≤ 10700).
Print a single integer, the answer to the question.
The first few values of S are 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 12. The sum of these values is 195.
Input: 21 | Output: 195
Master
2
186
67
51
9
1,129
D
1129D
D. Isolation
2,900
data structures; dp
Find the number of ways to divide an array \(a\) of \(n\) integers into any number of disjoint non-empty segments so that, in each segment, there exist at most \(k\) distinct integers that appear exactly once.Since the answer can be large, find it modulo \(998\,244\,353\).
The first line contains two space-separated integers \(n\) and \(k\) (\(1 \leq k \leq n \leq 10^5\)) — the number of elements in the array \(a\) and the restriction from the statement.The following line contains \(n\) space-separated integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq n\)) — elements of the array \(a...
The first and only line contains the number of ways to divide an array \(a\) modulo \(998\,244\,353\).
In the first sample, the three possible divisions are as follows. \([[1], [1], [2]]\) \([[1, 1], [2]]\) \([[1, 1, 2]]\) Division \([[1], [1, 2]]\) is not possible because two distinct integers appear exactly once in the second segment \([1, 2]\).
Input: 3 1 1 1 2 | Output: 3
Master
2
273
323
102
11
648
A
648A
A. Наибольший подъем
900
constructive algorithms; implementation
Профиль горного хребта схематично задан в виде прямоугольной таблицы из символов «.» (пустое пространство) и «*» (часть горы). Каждый столбец таблицы содержит хотя бы одну «звёздочку». Гарантируется, что любой из символов «*» либо находится в нижней строке матрицы, либо непосредственно под ним находится другой символ «...
В первой строке входных данных записаны два целых числа n и m (1 ≤ n, m ≤ 100) — количество строк и столбцов в схематичном изображении соответственно.Далее следуют n строк по m символов в каждой — схематичное изображение горного хребта. Каждый символ схематичного изображения — это либо «.», либо «*». Каждый столбец мат...
Выведите через пробел два целых числа: величину наибольшего подъёма за день (или 0, если в профиле горного хребта нет ни одного подъёма), величину наибольшего спуска за день (или 0, если в профиле горного хребта нет ни одного спуска).
В первом тестовом примере высоты гор равны: 3, 4, 1, 1, 2, 1, 1, 1, 2, 5, 1. Наибольший подъем равен 3 и находится между горой номер 9 (её высота равна 2) и горой номер 10 (её высота равна 5). Наибольший спуск равен 4 и находится между горой номер 10 (её высота равна 5) и горой номер 11 (её высота равна 1).Во втором те...
Input: 6 11....................*..*.......*.**.......*.**..*...**.*********** | Output: 3 4
Beginner
2
1,048
497
234
6
2,000
A
2000A
A. Primary Task
800
implementation; math; strings
Dmitry wrote down \(t\) integers on the board, and that is good. He is sure that he lost an important integer \(n\) among them, and that is bad.The integer \(n\) had the form \(\text{10^x}\) (\(x \ge 2\)), where the symbol '\(\text{^}\)' denotes exponentiation.. Something went wrong, and Dmitry missed the symbol '\(\te...
The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) — the number of integers on the board.The next \(t\) lines each contain an integer \(a\) (\(1 \le a \le 10000\)) — the next integer from the board.
For each integer on the board, output ""YES"" if it could have been the important integer and ""NO"" otherwise.You may output each letter in any case (lowercase or uppercase). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be accepted as a positive answer.
Input: 71001010101105203310191002 | Output: NO YES NO YES NO YES NO
Beginner
3
621
225
278
20
1,425
C
1425C
C. Captain of Knights
3,100
math
Mr. Chanek just won the national chess tournament and got a huge chessboard of size \(N \times M\). Bored with playing conventional chess, Mr. Chanek now defines a function \(F(X, Y)\), which denotes the minimum number of moves to move a knight from square \((1, 1)\) to square \((X, Y)\). It turns out finding \(F(X, Y)...
The first line contains an integer \(T\) \((1 \le T \le 100)\), the number of test cases.Each test case contains a line with four integers \(X\) \(Y\) \(N\) \(M\) \((3 \leq X \leq N \leq 10^9, 3 \leq Y \leq M \leq 10^9)\).
For each test case, print a line with the value of \(G(X, Y)\) modulo \(10^9 + 7\).
Input: 2 3 4 5 6 5 5 8 8 | Output: 27 70
Master
1
659
222
83
14
1,704
A
1704A
A. Two 0-1 Sequences
800
constructive algorithms; greedy
AquaMoon has two binary sequences \(a\) and \(b\), which contain only \(0\) and \(1\). AquaMoon can perform the following two operations any number of times (\(a_1\) is the first element of \(a\), \(a_2\) is the second element of \(a\), and so on): Operation 1: if \(a\) contains at least two elements, change \(a_2\) to...
The first line contains a single integer \(t\) (\(1 \leq t \leq 2\,000\)) — 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 50\), \(m \leq n\)) — the lengths of \(a\) and \(b\) respectively.The second line of each test ca...
For each test case, output ""YES"" if AquaMoon can change \(a\) to \(b\) by using these options; otherwise, output ""NO"".You may print each letter in any case (for example, ""YES"", ""Yes"", ""yes"", ""yEs"" will all be recognized as a positive answer).
In the first test case, you can use Operation 2 four times to make \(a\) equals to \(b\).In the second test case, you can use Operation 1 four times to make \(a\) equals to \(b\).In the third test case, it can be proved that no matter how we use the operations, it is impossible to make \(a\) equal to \(b\).In the fourt...
Input: 106 2001001116 2110111016 2000001116 2111111018 510000101110107 4101000110018 6010100100100108 40101010110018 41010101001107 5101110011100 | Output: YES YES NO NO NO YES YES NO NO YES
Beginner
2
831
502
254
17
1,644
B
1644B
B. Anti-Fibonacci Permutation
800
brute force; constructive algorithms; implementation
Let's call a permutation \(p\) of length \(n\) anti-Fibonacci if the condition \(p_{i-2} + p_{i-1} \ne p_i\) holds for all \(i\) (\(3 \le i \le n\)). Recall that the permutation is the array of length \(n\) which contains each integer from \(1\) to \(n\) exactly once.Your task is for a given number \(n\) print \(n\) di...
The first line contains a single integer \(t\) (\(1 \le t \le 48\)) — the number of test cases. The single line of each test case contains a single integer \(n\) (\(3 \le n \le 50\)).
For each test case, print \(n\) lines. Each line should contain an anti-Fibonacci permutation of length \(n\). In each test case, you cannot print any permutation more than once.If there are multiple answers, print any of them. It can be shown that it is always possible to find \(n\) different anti-Fibonacci permutatio...
Input: 243 | Output: 4 1 3 2 1 2 4 3 3 4 1 2 2 4 1 3 3 2 1 1 3 2 3 1 2
Beginner
3
371
183
374
16
954
G
954G
G. Castle Defense
2,000
binary search; data structures; greedy; two pointers
Today you are going to lead a group of elven archers to defend the castle that is attacked by an army of angry orcs. Three sides of the castle are protected by impassable mountains and the remaining side is occupied by a long wall that is split into n sections. At this moment there are exactly ai archers located at the...
The first line of the input contains three integers n, r and k (1 ≤ n ≤ 500 000, 0 ≤ r ≤ n, 0 ≤ k ≤ 1018) — the number of sections of the wall, the maximum distance to other section archers can still shoot and the number of archers yet to be distributed along the wall. The second line contains n integers a1, a2, ..., a...
Print one integer — the maximum possible value of defense plan reliability, i.e. the maximum possible value of minimum defense level if we distribute k additional archers optimally.
Input: 5 0 65 4 3 4 9 | Output: 5
Hard
4
1,115
385
181
9
712
D
712D
D. Memory and Scores
2,200
combinatorics; dp; math
Memory and his friend Lexa are competing to get higher score in one popular computer game. Memory starts with score a and Lexa starts with score b. In a single turn, both Memory and Lexa get some integer in the range [ - k;k] (i.e. one integer among - k, - k + 1, - k + 2, ..., - 2, - 1, 0, 1, 2, ..., k - 1, k) and add ...
The first and only line of input contains the four integers a, b, k, and t (1 ≤ a, b ≤ 100, 1 ≤ k ≤ 1000, 1 ≤ t ≤ 100) — the amount Memory and Lexa start with, the number k, and the number of turns respectively.
Print the number of possible games satisfying the conditions modulo 1 000 000 007 (109 + 7) in one line.
In the first sample test, Memory starts with 1 and Lexa starts with 2. If Lexa picks - 2, Memory can pick 0, 1, or 2 to win. If Lexa picks - 1, Memory can pick 1 or 2 to win. If Lexa picks 0, Memory can pick 2 to win. If Lexa picks 1 or 2, Memory cannot win. Thus, there are 3 + 2 + 1 = 6 possible games in which Memory ...
Input: 1 2 2 1 | Output: 6
Hard
3
844
211
104
7
924
A
924A
A. Mystical Mosaic
1,300
greedy; implementation
There is a rectangular grid of n rows of m initially-white cells each.Arkady performed a certain number (possibly zero) of operations on it. In the i-th operation, a non-empty subset of rows Ri and a non-empty subset of columns Ci are chosen. For each row r in Ri and each column c in Ci, the intersection of row r and c...
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 50) — the number of rows and columns of the grid, respectively.Each of the following n lines contains a string of m characters, each being either '.' (denoting a white cell) or '#' (denoting a black cell), representing the desired setup.
If the given grid can be achieved by any valid sequence of operations, output ""Yes""; otherwise output ""No"" (both without quotes).You can print each character in any case (upper or lower).
For the first example, the desired setup can be produced by 3 operations, as is shown below. For the second example, the desired setup cannot be produced, since in order to colour the center row, the third row and all columns must be selected in one operation, but after that no column can be selected again, hence it wo...
Input: 5 8.#.#..#......#...#.#..#.#.#....#.....#.. | Output: Yes
Easy
2
686
310
191
9
895
B
895B
B. XK Segments
1,700
binary search; math; sortings; two pointers
While Vasya finished eating his piece of pizza, the lesson has already started. For being late for the lesson, the teacher suggested Vasya to solve one interesting problem. Vasya has an array a and integer x. He should find the number of different ordered pairs of indexes (i, j) such that ai ≤ aj and there are exactly ...
The first line contains 3 integers n, x, k (1 ≤ n ≤ 105, 1 ≤ x ≤ 109, 0 ≤ k ≤ 109), where n is the size of the array a and x and k are numbers from the statement.The second line contains n integers ai (1 ≤ ai ≤ 109) — the elements of the array a.
Print one integer — the answer to the problem.
In first sample there are only three suitable pairs of indexes — (1, 2), (2, 3), (3, 4).In second sample there are four suitable pairs of indexes(1, 1), (2, 2), (3, 3), (4, 4).In third sample every pair (i, j) is suitable, so the answer is 5 * 5 = 25.
Input: 4 2 11 3 5 7 | Output: 3
Medium
4
518
246
46
8
1,333
F
1333F
F. Kate and imperfection
2,200
greedy; implementation; math; number theory; sortings; two pointers
Kate has a set \(S\) of \(n\) integers \(\{1, \dots, n\} \). She thinks that imperfection of a subset \(M \subseteq S\) is equal to the maximum of \(gcd(a, b)\) over all pairs \((a, b)\) such that both \(a\) and \(b\) are in \(M\) and \(a \neq b\). Kate is a very neat girl and for each \(k \in \{2, \dots, n\}\) she wan...
The first and only line in the input consists of only one integer \(n\) (\(2\le n \le 5 \cdot 10^5\)) — the size of the given set \(S\).
Output contains only one line that includes \(n - 1\) integers: \(I_2\), \(I_3\), ..., \(I_n\).
First sample: answer is 1, because \(gcd(1, 2) = 1\).Second sample: there are subsets of \(S\) with sizes \(2, 3\) with imperfection equal to 1. For example, \(\{2,3\}\) and \(\{1, 2, 3\}\).
Input: 2 | Output: 1
Hard
6
750
136
95
13
1,545
D
1545D
D. AquaMoon and Wrong Coordinate
3,000
constructive algorithms; interactive; math
Cirno gives AquaMoon a problem. There are \(m\) people numbered from \(0\) to \(m - 1\). They are standing on a coordinate axis in points with positive integer coordinates. They are facing right (i.e. in the direction of the coordinate increase). At this moment everyone will start running with the constant speed in the...
This problem is made as interactive. It means, that your solution will read the input, given by the interactor. But the interactor will give you the full input at the beginning and after that, you should print the answer. So you should solve the problem, like as you solve the usual, non-interactive problem because you ...
Print a single line with two integers \(y\), \(p\) — the moment that contains the modified coordinate and the original coordinate.
In the first test the initial coordinates of people are \(9\), \(6\), \(6\), \(9\), \(9\) and their speeds are \(1\), \(2\), \(1\), \(1\), \(1\). So, it's easy to see, that at the moment \(4\) one coordinate was modified from \(13\) to \(12\).This is the first test in the hack format:5 79 6 6 9 91 2 1 1 12 3 4 1 00 2 3...
Input: 5 7 6 9 9 6 9 10 7 10 8 10 11 11 11 10 8 12 12 12 12 9 14 13 12 10 13 11 14 16 14 14 12 15 18 15 15 | Output: 4 13
Master
3
1,071
2,317
130
15
246
C
246C
C. Beauty Pageant
1,600
brute force; constructive algorithms; greedy
General Payne has a battalion of n soldiers. The soldiers' beauty contest is coming up, it will last for k days. Payne decided that his battalion will participate in the pageant. Now he has choose the participants.All soldiers in the battalion have different beauty that is represented by a positive integer. The value a...
The first line contains two integers n, k (1 ≤ n ≤ 50; 1 ≤ k ≤ ) — the number of soldiers and the number of days in the pageant, correspondingly. The second line contains space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 107) — the beauties of the battalion soldiers.It is guaranteed that Payne's battalion doesn't have...
Print k lines: in the i-th line print the description of the detachment that will participate in the pageant on the i-th day. The description consists of integer ci (1 ≤ ci ≤ n) — the number of soldiers in the detachment on the i-th day of the pageant and ci distinct integers p1, i, p2, i, ..., pci, i — the beauties of...
Input: 3 31 2 3 | Output: 1 11 22 3 2
Medium
3
980
355
618
2
1,761
A
1761A
A. Two Permutations
800
brute force; constructive algorithms
You are given three integers \(n\), \(a\), and \(b\). Determine if there exist two permutations \(p\) and \(q\) of length \(n\), for which the following conditions hold: The length of the longest common prefix of \(p\) and \(q\) is \(a\). The length of the longest common suffix of \(p\) and \(q\) is \(b\). A permutatio...
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 test cases follows.The only line of each test case contains three integers \(n\), \(a\), and \(b\) (\(1\leq a,b\leq n\leq 100\)).
For each test case, if such a pair of permutations exists, output ""Yes""; otherwise, output ""No"". You can output each letter in any case (upper or lower).
In the first test case, \([1]\) and \([1]\) form a valid pair.In the second test case and the third case, we can show that such a pair of permutations doesn't exist.In the fourth test case, \([1,2,3,4]\) and \([1,3,2,4]\) form a valid pair.
Input: 41 1 12 1 23 1 14 1 1 | Output: Yes No No Yes
Beginner
2
612
286
157
17
177
B2
177B2
B2. Rectangular Game
1,200
number theory
The Smart Beaver from ABBYY decided to have a day off. But doing nothing the whole day turned out to be too boring, and he decided to play a game with pebbles. Initially, the Beaver has n pebbles. He arranges them in a equal rows, each row has b pebbles (a > 1). Note that the Beaver must use all the pebbles he has, i. ...
The single line of the input contains a single integer n — the initial number of pebbles the Smart Beaver has.The input limitations for getting 30 points are: 2 ≤ n ≤ 50 The input limitations for getting 100 points are: 2 ≤ n ≤ 109
Print a single number — the maximum possible result of the game.
Consider the first example (c1 = 10). The possible options for the game development are: Arrange the pebbles in 10 rows, one pebble per row. Then c2 = 1, and the game ends after the first move with the result of 11. Arrange the pebbles in 5 rows, two pebbles per row. Then c2 = 2, and the game continues. During the seco...
Input: 10 | Output: 16
Easy
1
1,143
231
64
1
1,396
A
1396A
A. Multiples of Length
1,600
constructive algorithms; greedy; number theory
You are given an array \(a\) of \(n\) integers.You want to make all elements of \(a\) equal to zero by doing the following operation exactly three times: Select a segment, for each number in this segment we can add a multiple of \(len\) to it, where \(len\) is the length of this segment (added integers can be different...
The first line contains one integer \(n\) (\(1 \le n \le 100\,000\)): the number of elements of the array.The second line contains \(n\) elements of an array \(a\) separated by spaces: \(a_1, a_2, \dots, a_n\) (\(-10^9 \le a_i \le 10^9\)).
The output should contain six lines representing three operations.For each operation, print two lines: The first line contains two integers \(l\), \(r\) (\(1 \le l \le r \le n\)): the bounds of the selected segment. The second line contains \(r-l+1\) integers \(b_l, b_{l+1}, \dots, b_r\) (\(-10^{18} \le b_i \le 10^{18}...
Input: 4 1 3 2 4 | Output: 1 1 -1 3 4 4 2 2 4 -3 -6 -6
Medium
3
411
239
437
13
1,166
E
1166E
E. The LCMs Must be Large
2,100
bitmasks; brute force; constructive algorithms; math; number theory
Dora the explorer has decided to use her money after several years of juicy royalties to go shopping. What better place to shop than Nlogonia?There are \(n\) stores numbered from \(1\) to \(n\) in Nlogonia. The \(i\)-th of these stores offers a positive integer \(a_i\).Each day among the last \(m\) days Dora bought a s...
The first line contains integers \(m\) and \(n\) (\(1\leq m \leq 50\), \(1\leq n \leq 10^4\)) — the number of days and the number of stores.After this \(m\) lines follow, the \(i\)-th line starts with an integer \(s_i\) (\(1\leq s_i \leq n-1\)), the number of integers Dora bought on day \(i\), followed by \(s_i\) disti...
Output must consist of a single line containing ""possible"" if there exist positive integers \(a_i\) such that for each day the least common multiple of the integers bought by Dora is strictly greater than the least common multiple of the integers bought by Swiper on that day. Otherwise, print ""impossible"".Note that...
In the first sample, a possible choice for the values of the \(a_i\) is \(3, 4, 3, 5, 2\). On the first day, Dora buys the integers \(3, 4\) and \(3\), whose LCM is \(12\), while Swiper buys integers \(5\) and \(2\), whose LCM is \(10\). On the second day, Dora buys \(3, 5\) and \(2\), whose LCM is \(30\), and Swiper b...
Input: 2 5 3 1 2 3 3 3 4 5 | Output: possible
Hard
5
1,199
450
371
11
1,834
A
1834A
A. Unit Array
800
greedy; math
Given an array \(a\) of length \(n\), which elements are equal to \(-1\) and \(1\). Let's call the array \(a\) good if the following conditions are held at the same time: \(a_1 + a_2 + \ldots + a_n \ge 0\); \(a_1 \cdot a_2 \cdot \ldots \cdot a_n = 1\). In one operation, you can select an arbitrary element of the array ...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 500\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 100\)) — the length of the array \(a\).The second line of ...
For each test case, output a single integer — the minimum number of operations that need to be done to make the \(a\) array good.
In the first test case, we can assign the value \(a_1 := 1\). Then \(a_1 + a_2 + a_3 + a_4 = 1 + (-1) + 1 + (-1) = 0 \ge 0\) and \(a_1 \cdot a_2 \cdot a_3 \cdot a_4 = 1 \cdot (-1) \cdot 1 \cdot (-1) = 1\). Thus, we performed \(1\) operation.In the second test case, we can assign \(a_1 := 1\). Then \(a_1 + a_2 + a_3 + a...
Input: 74-1 -1 1 -15-1 -1 -1 1 14-1 1 -1 13-1 -1 -151 1 1 1 11-12-1 -1 | Output: 1 1 0 3 0 1 2
Beginner
2
638
437
129
18
2,035
A
2035A
A. Sliding
800
implementation; math
Red was ejected. They were not the imposter.There are \(n\) rows of \(m\) people. Let the position in the \(r\)-th row and the \(c\)-th column be denoted by \((r, c)\). Number each person starting from \(1\) in row-major order, i.e., the person numbered \((r-1)\cdot m+c\) is initially at \((r,c)\).The person at \((r, c...
The first line contains a single integer \(t\) (\(1\le t\le 10^4\)) — the number of test cases.The only line of each testcase contains \(4\) integers \(n\), \(m\), \(r\), and \(c\) (\(1\le r\le n\le 10^6\), \(1 \le c \le m \le 10^6\)), where \(n\) is the number of rows, \(m\) is the number of columns, and \((r,c)\) is ...
For each test case, output a single integer denoting the sum of the Manhattan distances.
For the first test case, the person numbered \(2\) leaves, and the distances of the movements of the person numbered \(3\), \(4\), \(5\), and \(6\) are \(1\), \(3\), \(1\), and \(1\), respectively. So the answer is \(1+3+1+1=6\).For the second test case, the person numbered \(3\) leaves, and the person numbered \(4\) m...
Input: 42 3 1 22 2 2 11 1 1 11000000 1000000 1 1 | Output: 6 1 0 1999998000000
Beginner
2
797
375
88
20
1,530
F
1530F
F. Bingo
2,600
bitmasks; combinatorics; dp; math; probabilities
Getting ready for VK Fest 2021, you prepared a table with \(n\) rows and \(n\) columns, and filled each cell of this table with some event related with the festival that could either happen or not: for example, whether you will win a prize on the festival, or whether it will rain.Forecasting algorithms used in VK have ...
The first line contains a single integer \(n\) (\(2 \le n \le 21\)) — the dimensions of the table.The \(i\)-th of the next \(n\) lines contains \(n\) integers \(a_{i, 1}, a_{i, 2}, \ldots, a_{i, n}\) (\(0 < a_{i, j} < 10^4\)). The probability of event in cell \((i, j)\) to happen is \(a_{i, j} \cdot 10^{-4}\).
Print the probability that your table will be winning, modulo \(31\,607\).Formally, let \(M = 31\,607\). It can be shown that the answer can be expressed as an irreducible fraction \(\frac{p}{q}\), where \(p\) and \(q\) are integers and \(q \not \equiv 0 \pmod{M}\). Output the integer equal to \(p \cdot q^{-1} \bmod M\...
In the first example, any two events form a line, and the table will be winning if any two events happen. The probability of this is \(\frac{11}{16}\), and \(5927 \cdot 16 \equiv 11 \pmod{31\,607}\).
Input: 2 5000 5000 5000 5000 | Output: 5927
Expert
5
1,016
311
425
15
2,089
C2
2089C2
C2. Key of Like (Hard Version)
3,100
dp; math; probabilities
This is the hard version of the problem. The difference between the versions is that in this version, \(k\) can be non-zero. You can hack only if you solved all versions of this problem. A toy box is a refrigerator filled with childhood delight. Like weakness, struggle, hope ... When such a sleeper is reawakened, what ...
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 only line of the input contains three integers, \(n\), \(l\), \(k\) (\(1 \leq n \leq 100, 1 \leq l \leq 5000, 0 \leq k \leq 25\)) — the number of members p...
For each test case, output a single line with \(n\) integers \(e_1, \ldots, e_n\), where \(e_i\) represents the expected number of successful matches, modulo \(10^9 + 7\).Formally, let \(M = 10^9 + 7\). It can be shown that the exact answer can be expressed as an irreducible fraction \(\frac{p}{q}\), where \(p\) and \(...
For the first test case, there is only \(1\) lock, so the strategy will always be choosing any key that no one has ever tried. Since there are \(1 + 4 = 5\) keys in total, the probability that each member successfully opens the lock will be \(2/5, 2/5, 1/5\) respectively, which are also the expected numbers of successf...
Input: 43 1 43 2 025 2 54 102 9 | Output: 800000006 800000006 400000003 500000004 1 500000004 142857144 166666668 615646263 639455787 234126986 257936510 195918369 502040820 478316330 81264173 190523433 471438023 23809524 0 0 0 0 0 0 0 0 0 0 0 0 568832210 85779764 969938175 375449967
Master
3
2,632
488
533
20
452
E
452E
E. Three strings
2,400
data structures; dsu; string suffix structures; strings
You are given three strings (s1, s2, s3). For each integer l (1 ≤ l ≤ min(|s1|, |s2|, |s3|) you need to find how many triples (i1, i2, i3) exist such that three strings sk[ik... ik + l - 1] (k = 1, 2, 3) are pairwise equal. Print all found numbers modulo 1000000007 (109 + 7).See notes if you are not sure about some of ...
First three lines contain three non-empty input strings. The sum of lengths of all strings is no more than 3·105. All strings consist only of lowercase English letters.
You need to output min(|s1|, |s2|, |s3|) numbers separated by spaces — answers for the problem modulo 1000000007 (109 + 7).
Consider a string t = t1t2... t|t|, where ti denotes the i-th character of the string, and |t| denotes the length of the string.Then t[i... j] (1 ≤ i ≤ j ≤ |t|) represents the string titi + 1... tj (substring of t from position i to position j inclusive).
Input: abcbccbc | Output: 3 1
Expert
4
356
168
123
4
756
B
756B
B. Travel Card
1,600
binary search; dp
A new innovative ticketing systems for public transport is introduced in Bytesburg. Now there is a single travel card for all transport. To make a trip a passenger scan his card and then he is charged according to the fare.The fare is constructed in the following manner. There are three types of tickets: a ticket for o...
The first line of input contains integer number n (1 ≤ n ≤ 105) — the number of trips made by passenger.Each of the following n lines contains the time of trip ti (0 ≤ ti ≤ 109), measured in minutes from the time of starting the system. All ti are different, given in ascending order, i. e. ti + 1 > ti holds for all 1 ≤...
Output n integers. For each trip, print the sum the passenger is charged after it.
In the first example, the system works as follows: for the first and second trips it is cheaper to pay for two one-trip tickets, so each time 20 rubles is charged, after the third trip the system understands that it would be cheaper to buy a ticket for 90 minutes. This ticket costs 50 rubles, and the passenger had alre...
Input: 3102030 | Output: 202010
Medium
2
1,213
327
82
7
1,592
E
1592E
E. Bored Bakry
2,400
bitmasks; greedy; math; two pointers
Bakry got bored of solving problems related to xor, so he asked you to solve this problem for him.You are given an array \(a\) of \(n\) integers \([a_1, a_2, \ldots, a_n]\).Let's call a subarray \(a_{l}, a_{l+1}, a_{l+2}, \ldots, a_r\) good if \(a_l \, \& \, a_{l+1} \, \& \, a_{l+2} \, \ldots \, \& \, a_r > a_l \oplus ...
The first line contains a single integer \(n\) (\(1 \le n \le 10^6\)) — the length of the array.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^6\)) — elements of the array.
Print a single integer — the length of the longest good subarray. If there are no good subarrays, print \(0\).
In the first case, the answer is \(2\), as the whole array is good: \(5 \& 6 = 4 > 5 \oplus 6 = 3\).In the third case, the answer is \(4\), and one of the longest good subarrays is \([a_2, a_3, a_4, a_5]\): \(1\& 3 \& 3 \&1 = 1 > 1\oplus 3 \oplus 3\oplus 1 = 0\).
Input: 2 5 6 | Output: 2
Expert
4
557
211
110
15
1,599
I
1599I
I. Desert
2,700
data structures; graphs
You are given an undirected graph of \(N\) nodes and \(M\) edges, \(E_1, E_2, \dots E_M\).A connected graph is a cactus if each of it's edges belogs to at most one simple cycle. A graph is a desert if each of it's connected components is a cactus. Find the number of pairs \((L, R)\), (\(1 \leq L \leq R \leq M\)) such t...
The first line contains two integers \(N\) and \(M\) (\(2 \leq N \leq 2.5 \times 10^5\), \(1 \leq M \leq 5 \times 10^5\)). Each of the next \(M\) lines contains two integers. The \(i\)-th line describes the \(i\)-th edge. It contains integers \(U_i\) and \(V_i\), the nodes connected by the \(i\)-th edge (\(E_i=(U_i, V_...
The output contains one integer number – the answer.
In the second example: Graphs for pairs \((1, 1)\), \((2, 2)\) and \((3, 3)\) are deserts because they don't have any cycles. Graphs for pairs \((1, 2)\) and \((2, 3)\) have one cycle of length 2 so they are deserts.
Input: 5 6 1 2 2 3 3 4 4 5 5 1 2 4 | Output: 20
Master
2
414
397
52
15
1,938
D
1938D
3,300
Master
0
0
0
0
19
2,052
J
2052J
J. Judicious Watching
2,000
binary search; greedy; sortings
Jill loves having good grades in university, so she never misses deadlines for her homework assignments. But even more, she loves watching the series and discussing it with her best friend Johnny. And unfortunately, today she needs to choose between these two activities!Jill needs to complete \(n\) homework tasks. The ...
There are several test cases in the input. The input begins with the number of test cases \(T\) (\(1 \le T \le 1\,000\)). Each test case starts with a line with three integers \(n\) (\(1 \le n \le 200\,000\)) — the number of homework tasks, \(m\) (\(1 \le m \le 200\,000\)) — the number of episodes, and \(q\) (\(1 \le q...
For each test case output a single line with \(q\) integers — for each possible time \(t_k\) the maximum number of episodes Jill can watch.
Input: 21 2 310155 55 15 203 4 58 100 810 150 202 32 1 19 200 51 50 10 | Output: 1 1 2 1 4 2 2 1
Hard
3
1,317
1,065
139
20
1,580
D
1580D
D. Subsequence
2,900
brute force; divide and conquer; dp; greedy; trees
Alice has an integer sequence \(a\) of length \(n\) and all elements are different. She will choose a subsequence of \(a\) of length \(m\), and defines the value of a subsequence \(a_{b_1},a_{b_2},\ldots,a_{b_m}\) as $$$\(\sum_{i = 1}^m (m \cdot a_{b_i}) - \sum_{i = 1}^m \sum_{j = 1}^m f(\min(b_i, b_j), \max(b_i, b_j))...
The first line contains two integers \(n\) and \(m\) (\(1 \le m \le n \le 4000\)).The second line contains \(n\) distinct integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i < 2^{31}\)).
Print the maximal value Alice can get.
In the first example, Alice can choose the subsequence \([15, 2, 18, 13]\), which has the value \(4 \cdot (15 + 2 + 18 + 13) - (15 + 2 + 2 + 2) - (2 + 2 + 2 + 2) - (2 + 2 + 18 + 12) - (2 + 2 + 12 + 13) = 100\). In the second example, there are a variety of subsequences with value \(176\), and one of them is \([9, 7, 12...
Input: 6 4 15 2 18 12 13 4 | Output: 100
Master
5
617
182
38
15
1,907
B
1907B
B. YetnotherrokenKeoard
1,000
data structures; implementation; strings
Polycarp has a problem — his laptop keyboard is broken.Now, when he presses the 'b' key, it acts like an unusual backspace: it deletes the last (rightmost) lowercase letter in the typed string. If there are no lowercase letters in the typed string, then the press is completely ignored.Similarly, when he presses the 'B'...
The first line of the input data contains an integer \(t\) (\(1 \le t \le 1000\)), the number of test cases in the test.The following contains \(t\) non-empty lines, which consist of lowercase and uppercase letters of the Latin alphabet.It is guaranteed that each line contains at least one letter and the sum of the len...
For each test case, output the result of processing the key presses on a separate line. If the typed string is empty, then output an empty line.
Input: 12ARaBbbitBabyYetAnotherBrokenKeyboardBubbleImprobableabbreviableBbBBBusyasaBeeinaBedofBloomingBlossomsCoDEBARbIEScodeforcesbobebobbesbTheBBlackbboard | Output: ity YetnotherrokenKeoard le Imprle revile usyasaeeinaedofloominglossoms CDARIES codeforces es helaoard
Beginner
3
1,249
363
144
19
1,542
C
1542C
C. Strange Function
1,600
math; number theory
Let \(f(i)\) denote the minimum positive integer \(x\) such that \(x\) is not a divisor of \(i\).Compute \(\sum_{i=1}^n f(i)\) modulo \(10^9+7\). In other words, compute \(f(1)+f(2)+\dots+f(n)\) modulo \(10^9+7\).
The first line contains a single integer \(t\) (\(1\leq t\leq 10^4\)), the number of test cases. Then \(t\) cases follow.The only line of each test case contains a single integer \(n\) (\(1\leq n\leq 10^{16}\)).
For each test case, output a single integer \(ans\), where \(ans=\sum_{i=1}^n f(i)\) modulo \(10^9+7\).
In the fourth test case \(n=4\), so \(ans=f(1)+f(2)+f(3)+f(4)\). \(1\) is a divisor of \(1\) but \(2\) isn't, so \(2\) is the minimum positive integer that isn't a divisor of \(1\). Thus, \(f(1)=2\). \(1\) and \(2\) are divisors of \(2\) but \(3\) isn't, so \(3\) is the minimum positive integer that isn't a divisor of ...
Input: 6 1 2 3 4 10 10000000000000000 | Output: 2 5 7 10 26 366580019
Medium
2
213
211
103
15
1,397
B
1397B
B. Power Sequence
1,500
brute force; math; number theory; sortings
Let's call a list of positive integers \(a_0, a_1, ..., a_{n-1}\) a power sequence if there is a positive integer \(c\), so that for every \(0 \le i \le n-1\) then \(a_i = c^i\).Given a list of \(n\) positive integers \(a_0, a_1, ..., a_{n-1}\), you are allowed to: Reorder the list (i.e. pick a permutation \(p\) of \(\...
The first line contains an integer \(n\) (\(3 \le n \le 10^5\)).The second line contains \(n\) integers \(a_0, a_1, ..., a_{n-1}\) (\(1 \le a_i \le 10^9\)).
Print the minimum cost to transform \(a_0, a_1, ..., a_{n-1}\) into a power sequence.
In the first example, we first reorder \(\{1, 3, 2\}\) into \(\{1, 2, 3\}\), then increment \(a_2\) to \(4\) with cost \(1\) to get a power sequence \(\{1, 2, 4\}\).
Input: 3 1 3 2 | Output: 1
Medium
4
650
156
85
13
474
F
474F
F. Ant colony
2,100
data structures; math; number theory
Mole is hungry again. He found one ant colony, consisting of n ants, ordered in a row. Each ant i (1 ≤ i ≤ n) has a strength si.In order to make his dinner more interesting, Mole organizes a version of «Hunger Games» for the ants. He chooses two numbers l and r (1 ≤ l ≤ r ≤ n) and each pair of ants with indices between...
The first line contains one integer n (1 ≤ n ≤ 105), the size of the ant colony. The second line contains n integers s1, s2, ..., sn (1 ≤ si ≤ 109), the strengths of the ants. The third line contains one integer t (1 ≤ t ≤ 105), the number of test cases. Each of the next t lines contains two integers li and ri (1 ≤ li ...
Print to the standard output t lines. The i-th line contains number of ants that Mole eats from the segment [li, ri].
In the first test battle points for each ant are v = [4, 0, 2, 0, 2], so ant number 1 is freed. Mole eats the ants 2, 3, 4, 5.In the second test case battle points are v = [0, 2, 0, 2], so no ant is freed and all of them are eaten by Mole.In the third test case battle points are v = [2, 0, 2], so ants number 3 and 5 ar...
Input: 51 3 2 4 241 52 53 54 5 | Output: 4411
Hard
3
959
352
117
4
468
C
468C
C. Hack it!
2,500
binary search; constructive algorithms; math
Little X has met the following problem recently. Let's define f(x) as the sum of digits in decimal representation of number x (for example, f(1234) = 1 + 2 + 3 + 4). You are to calculate Of course Little X has solved this problem quickly, has locked it, and then has tried to hack others. He has seen the following C++ c...
The first line contains a single integer a (1 ≤ a ≤ 1018).
Print two integers: l, r (1 ≤ l ≤ r < 10200) — the required test data. Leading zeros aren't allowed. It's guaranteed that the solution exists.
Input: 46 | Output: 1 10
Expert
3
485
58
142
4
2,079
A
2079A
3,300
*special; data structures; dp; games
Master
4
0
0
0
20
1,174
B
1174B
B. Ehab Is an Odd Person
1,200
sortings
You're given an array \(a\) of length \(n\). You can perform the following operation on it as many times as you want: Pick two integers \(i\) and \(j\) \((1 \le i,j \le n)\) such that \(a_i+a_j\) is odd, then swap \(a_i\) and \(a_j\). What is lexicographically the smallest array you can obtain?An array \(x\) is lexicog...
The first line contains an integer \(n\) (\(1 \le n \le 10^5\)) — the number of elements in the array \(a\).The second line contains \(n\) space-separated integers \(a_1\), \(a_2\), \(\ldots\), \(a_{n}\) (\(1 \le a_i \le 10^9\)) — the elements of the array \(a\).
The only line contains \(n\) space-separated integers, the lexicographically smallest array you can obtain.
In the first example, we can swap \(1\) and \(4\) since \(1+4=5\), which is odd.
Input: 3 4 1 7 | Output: 1 4 7
Easy
1
527
263
107
11
1,773
D
1773D
D. Dominoes
2,600
combinatorics; flows; graph matchings; greedy
Dora likes to play with dominoes. She takes \(n \times m\) table, marks some cells as occupied, and then tries to fill all unoccupied cells with \(2 \times 1\) dominoes. Her little brother Dani loves to play pranks on his older sister. So when she is away, he marks two more unoccupied cells as occupied. He wants to do ...
The first line contains integers \(n\) and \(m\) (\(1\le n, m\le 1000\)). Next \(n\) lines contain \(m\) characters each — the initial state of the table. Character ""#"" corresponds to an occupied cell, and character ""."" corresponds to an unoccupied cell. It is guaranteed that there are at least two unoccupied cells...
Let \(x\) be the number of ways Dani can mark two cells in such a way that it will be impossible to fill all unoccupied cells with dominoes. Print one integer \(\min(x, 10^6)\).
Input: 3 6 ...#.. ...... #...## | Output: 52
Expert
4
576
389
177
17
720
F
720F
F. Array Covering
3,100
data structures
Misha has an array of integers of length n. He wants to choose k different continuous subarrays, so that each element of the array belongs to at least one of the chosen subarrays.Misha wants to choose the subarrays in such a way that if he calculated the sum of elements for each subarray, and then add up all these sums...
The first line of input contains two integers: n, k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ n·(n + 1) / 2) — the number of elements in the array and the number of different subarrays that must be chosen.The second line contains n integers ai ( - 50 000 ≤ ai ≤ 50 000) — the elements of the array.
Output one integer — the maximum possible value Misha can get by choosing k different subarrays.
Input: 5 46 -4 -10 -4 7 | Output: 11
Master
1
363
282
96
7
474
D
474D
D. Flowers
1,700
dp
We saw the little game Marmot made for Mole's lunch. Now it's Marmot's dinner time and, as we all know, Marmot eats flowers. At every dinner he eats some red and white flowers. Therefore a dinner can be represented as a sequence of several flowers, some of them white and some of them red.But, for a dinner to be tasty, ...
Input contains several test cases.The first line contains two integers t and k (1 ≤ t, k ≤ 105), where t represents the number of test cases.The next t lines contain two integers ai and bi (1 ≤ ai ≤ bi ≤ 105), describing the i-th test.
Print t lines to the standard output. The i-th line should contain the number of ways in which Marmot can eat between ai and bi flowers at dinner modulo 1000000007 (109 + 7).
For K = 2 and length 1 Marmot can eat (R). For K = 2 and length 2 Marmot can eat (RR) and (WW). For K = 2 and length 3 Marmot can eat (RRR), (RWW) and (WWR). For K = 2 and length 4 Marmot can eat, for example, (WWWW) or (RWWR), but for example he can't eat (WWWR).
Input: 3 21 32 34 4 | Output: 655
Medium
1
548
235
174
4
797
A
797A
A. k-Factorization
1,100
implementation; math; number theory
Given a positive integer n, find k integers (not necessary distinct) such that all these integers are strictly greater than 1, and their product is equal to n.
The first line contains two integers n and k (2 ≤ n ≤ 100000, 1 ≤ k ≤ 20).
If it's impossible to find the representation of n as a product of k numbers, print -1.Otherwise, print k integers in any order. Their product must be equal to n. If there are multiple answers, print any of them.
Input: 100000 2 | Output: 2 50000
Easy
3
159
74
212
7
2,108
F
2108F
F. Fallen Towers
2,900
binary search; greedy
Pizano built an array \(a\) of \(n\) towers, each consisting of \(a_i \ge 0\) blocks.Pizano can knock down a tower so that the next \(a_i\) towers grow by \(1\). In other words, he can take the element \(a_i\), increase the next \(a_i\) elements by one, and then set \(a_i\) to \(0\). The blocks that fall outside the ar...
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 10^5\)) — the number of towers.The second line of each test case contains \(n\) in...
For each test case, output a single integer — the maximum \(\text{MEX}\) of the final array.
Explanation for the first test case. Explanation for the second test case. Note that all towers were knocked down exactly once, and the final array of heights is non-decreasing.
Input: 821 242 1 0 0105 9 3 7 1 5 1 5 4 3101 1 1 1 1 1 1 1 1 1103 2 1 0 3 2 1 0 3 255 2 0 5 51100000000074 0 1 0 2 7 7 | Output: 2 3 7 4 5 4 1 3
Master
2
1,012
495
92
21
1,852
B
1852B
B. Imbalanced Arrays
1,800
constructive algorithms; graphs; greedy; math; sortings; two pointers
Ntarsis has come up with an array \(a\) of \(n\) non-negative integers.Call an array \(b\) of \(n\) integers imbalanced if it satisfies the following: \(-n\le b_i\le n\), \(b_i \ne 0\), there are no two indices \((i, j)\) (\(1 \le i, j \le n\)) such that \(b_i + b_j = 0\), for each \(1 \leq i \leq n\), there are exactl...
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 has a single integer \(n\) (\(1 \leq n \leq 10^5\)).The next line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq ...
For each test case, output ""NO"" if there exists no imbalanced array. Otherwise, output ""YES"". Then, on the next line, output \(n\) integers \(b_1, b_2, \ldots, b_n\) where \(b_i \neq 0\) for all \(1 \leq i \leq n\) — an imbalanced array.
For the first test case, \(b = [1]\) is an imbalanced array. This is because for \(i = 1\), there is exactly one \(j\) (\(j = 1\)) where \(b_1 + b_j > 0\).For the second test case, it can be shown that there exists no imbalanced array.For the third test case, \(a = [0, 1, 0]\). The array \(b = [-3, 1, -2]\) is an imbal...
Input: 51141 4 3 430 1 044 3 2 131 3 1 | Output: YES 1 NO YES -3 1 -2 YES 4 2 -1 -3 YES -1 3 -1
Medium
6
574
420
241
18
1,858
E1
1858E1
E1. Rollbacks (Easy Version)
2,500
brute force; data structures; dfs and similar; trees
This is an easy version of this problem. The only difference between the versions is that you have to solve the hard version in online mode. You can make hacks only if both versions of the problem are solved.You have an array \(a\), which is initially empty. You need to process queries of the following types: + \(x\) —...
The first line contains an integer \(q\) (\(1 \leq q \leq 10^6\)) — the number of queries.The next \(q\) lines contain the queries as described above.It is guaranteed that in the queries of the first type, \(1 \le x \le 10^6\); in the queries of the second type, \(k \ge 1\) and \(k\) does not exceed the current length ...
For each query of the fourth type output one integer — the number of distinct elements in array \(a\) at the moment of query.
In the first example array \(a\) changes as follows: After the first query, \(a=[1]\). After the second query, \(a=[1,2]\). After the third query, \(a=[1,2,2]\). At the moment of the fourth query, there are \(2\) distinct intergers in the array \(a\): \(1\) and \(2\). After the fifth query, \(a=[1,2]\) (rolled back the...
Input: 10 + 1 + 2 + 2 ? ! + 3 - 2 ? + 1 ? | Output: 2 1 1
Expert
4
685
573
125
18
261
E
261E
E. Maxim and Calculator
2,800
brute force; dp; two pointers
Maxim has got a calculator. The calculator has two integer cells. Initially, the first cell contains number 1, and the second cell contains number 0. In one move you can perform one of the following operations: Let's assume that at the current time the first cell contains number a, and the second cell contains number b...
The first line contains three integers: l, r, p (2 ≤ l ≤ r ≤ 109, 1 ≤ p ≤ 100). The numbers in the line are separated by single spaces.
In a single line print a single integer — the answer to the problem.
Input: 2 10 3 | Output: 1
Master
3
677
135
68
2
1,720
D1
1720D1
D1. Xor-Subsequence (easy version)
1,800
bitmasks; brute force; dp; strings; trees; two pointers
It is the easy version of the problem. The only difference is that in this version \(a_i \le 200\).You are given an array of \(n\) integers \(a_0, a_1, a_2, \ldots a_{n - 1}\). Bryap wants to find the longest beautiful subsequence in the array.An array \(b = [b_0, b_1, \ldots, b_{m-1}]\), where \(0 \le b_0 < b_1 < \ldo...
The first line contains a single integer \(t\) (\(1 \leq t \leq 10^5\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \leq n \leq 3 \cdot 10^5\)) — the length of the array.The second line of each test case contains \(n\) integ...
For each test case print a single integer — the length of the longest beautiful subsequence.
In the first test case, we can pick the whole array as a beautiful subsequence because \(1 \oplus 1 < 2 \oplus 0\).In the second test case, we can pick elements with indexes \(1\), \(2\) and \(4\) (in \(0\)-indexation). For this elements holds: \(2 \oplus 2 < 4 \oplus 1\) and \(4 \oplus 4 < 1 \oplus 2\).
Input: 321 255 2 4 3 1103 8 8 2 9 1 6 2 8 3 | Output: 2 3 6
Medium
6
867
494
92
17
1,709
D
1709D
D. Rorororobot
1,700
binary search; data structures; greedy; math
There is a grid, consisting of \(n\) rows and \(m\) columns. The rows are numbered from \(1\) to \(n\) from bottom to top. The columns are numbered from \(1\) to \(m\) from left to right. The \(i\)-th column has the bottom \(a_i\) cells blocked (the cells in rows \(1, 2, \dots, a_i\)), the remaining \(n - a_i\) cells a...
The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 10^9\); \(1 \le m \le 2 \cdot 10^5\)) — the number of rows and columns of the grid.The second line contains \(m\) integers \(a_1, a_2, \dots, a_m\) (\(0 \le a_i \le n\)) — the number of blocked cells on the bottom of the \(i\)-th column.The third line ...
For each query, print ""YES"" if you can send the robot an arbitrary number of commands (possibly, zero) so that it reaches the finish cell from the start cell, given that it executes each command \(k\) times. Otherwise, print ""NO"".
Input: 11 10 9 0 0 10 3 4 8 11 10 8 6 1 2 1 3 1 1 2 1 3 2 4 3 4 5 2 5 3 11 5 3 5 3 11 5 2 11 9 9 10 1 | Output: YES NO NO NO YES YES
Medium
4
1,144
803
234
17
1,332
G
1332G
G. No Monotone Triples
3,100
data structures
Given a sequence of integers \(a\) of length \(n\), a tuple \((i,j,k)\) is called monotone triples if \(1 \le i<j<k\le n\); \(a_i \le a_j \le a_k\) or \(a_i \ge a_j \ge a_k\) is satisfied. For example, \(a=[5,3,4,5]\), then \((2,3,4)\) is monotone triples for sequence \(a\) while \((1,3,4)\) is not.Bob is given a seque...
The first line contains two integers \(n\), \(q\) (\(3 \le n \le 2 \cdot 10^5\), \(1 \le q \le 2 \cdot 10^5\)) — the length of sequence \(a\) and the number of queries.The second line contains \(n\) integers \(a_1,a_2,\ldots, a_n\) (\(1 \le a_i \le 10^{9}\)), representing the sequence \(a\).Then each of the following \...
For each query, output \(0\) if there is no subsequence \(b\) satisfying the constraints mentioned in the legend. You can print the empty line after but that's not mandatory.Otherwise, output one integer \(k\) (\(k > 2\)) denoting the length of sequence \(b\), then output \(k\) integers \(i_1, i_2, \ldots, i_k\) (\(L \...
For the first query, the given sequence itself is monotone triples free.For the second query, it can be shown that there is no subsequence \(b\) with length greater than \(2\) such that \(b\) is monotone triples free.
Input: 6 2 3 1 4 1 5 9 1 3 4 6 | Output: 3 1 2 3 0
Master
1
1,023
402
481
13
229
B
229B
B. Planets
1,700
binary search; data structures; graphs; shortest paths
Goa'uld Apophis captured Jack O'Neill's team again! Jack himself was able to escape, but by that time Apophis's ship had already jumped to hyperspace. But Jack knows on what planet will Apophis land. In order to save his friends, Jack must repeatedly go through stargates to get to this planet.Overall the galaxy has n p...
The first line contains two space-separated integers: n (2 ≤ n ≤ 105), the number of planets in the galaxy, and m (0 ≤ m ≤ 105) — the number of pairs of planets between which Jack can travel using stargates. Then m lines follow, containing three integers each: the i-th line contains numbers of planets ai and bi (1 ≤ ai...
Print a single number — the least amount of time Jack needs to get from planet 1 to planet n. If Jack can't get to planet n in any amount of time, print number -1.
In the first sample Jack has three ways to go from planet 1. If he moves to planet 4 at once, he spends 8 seconds. If he transfers to planet 3, he spends 3 seconds, but as other travellers arrive to planet 3 at time 3 and 4, he can travel to planet 4 only at time 5, thus spending 8 seconds in total. But if Jack moves t...
Input: 4 61 2 21 3 31 4 82 3 42 4 53 4 301 32 3 40 | Output: 7
Medium
4
1,273
978
163
2
195
E
195E
E. Building Forest
2,000
data structures; dsu; graphs
An oriented weighted forest is an acyclic weighted digraph in which from each vertex at most one edge goes.The root of vertex v of an oriented weighted forest is a vertex from which no edge goes and which can be reached from vertex v moving along the edges of the weighted oriented forest. We denote the root of vertex v...
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of operations of adding a vertex.Next n lines contain descriptions of the operations, the i-th line contains the description of the operation of adding the i-th vertex in the following format: the first number of a line is an integer k (0 ≤ k ≤ i - 1...
Print a single number — the sum of weights of all edges of the resulting graph modulo 1000000007 (109 + 7).
Conside the first sample: Vertex 1 is added. k = 0, thus no edges are added. Vertex 2 is added. k = 0, thus no edges are added. Vertex 3 is added. k = 1. v1 = 2, x1 = 1. Edge from vertex root(2) = 2 to vertex 3 with weight depth(2) + x1 = 0 + 1 = 1 is added. Vertex 4 is added. k = 2. v1 = 1, x1 = 5. Edge from vertex ro...
Input: 6001 2 12 1 5 2 21 1 21 3 4 | Output: 30
Hard
3
1,302
668
107
1
2,046
B
2046B
B. Move Back at a Cost
1,600
binary search; data structures; greedy; sortings
You are given an array of integers \(a\) of length \(n\). You can perform the following operation zero or more times: In one operation choose an index \(i\) (\(1 \le i \le n\)), assign \(a_i := a_i + 1\), and then move \(a_i\) to the back of the array (to the rightmost position). For example, if \(a = [3, 5, 1, 9]\), a...
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 contains a single integer \(n\) (\(1 \le n \le 10^5\)), the length of the array.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n...
For each test case, print the lexicographically smallest array you can get.
Input: 332 1 351 2 2 1 461 2 3 6 5 4 | Output: 1 3 3 1 1 3 3 5 1 2 3 4 6 7
Medium
4
781
459
75
20