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 \(i\) such that \(1 \le i \le k\); \(l_i = r_{i-1}+1\), for each positive integer \(i\) such that \(2 \le i \le k\); Now you will be given a positive integer \(q\) which represents the number of modifications you need to do on \(s\).Each modification is defined with one positive integer \(x\): Find an index \(i\) such that \(l_i \le x \le r_i\) (notice that such \(i\) is unique). Let \(a=\min(x, r_i+l_i-x)\) and let \(b=\max(x, r_i+l_i-x)\). Reverse the substring of \(s\) from index \(a\) to index \(b\). Reversing the substring \([a, b]\) of a string \(s\) means to make \(s\) equal to \(s_1, s_2, \dots, s_{a-1},\ s_b, s_{b-1}, \dots, s_{a+1}, s_a,\ s_{b+1}, s_{b+2}, \dots, s_{n-1}, s_n\).Print \(s\) after the last modification is finished.
|
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 the length of arrays \(l\) and \(r\).The second line of each test case contains the string \(s\) (\( |s| = n\)) containing lowercase Latin letters — the initial string.The third line of each test case contains \(k\) positive integers \(l_1, l_2, \dots, l_k\) (\(1 \le l_i \le n\)) — the array \(l\).The fourth line of each test case contains \(k\) positive integers \(r_1, r_2, \dots, r_k\) (\(1 \le r_i \le n\)) — the array \(r\).The fifth line of each test case contains a positive integer \(q\) (\(1 \le q \le 2 \cdot 10^5 \)) — the number of modifications you need to do to \(s\).The sixth line of each test case contains \(q\) positive integers \(x_1, x_2, \dots, x_q\) (\(1\le x_i \le n\)) — the description of the modifications.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2\cdot10^5\). It is guaranteed that the sum of \(q\) over all test cases does not exceed \(2\cdot10^5\).It is guaranteed that the conditions in the statement hold for the arrays \(l\) and \(r\).
|
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 last modification), we have \(x=3\). Since \(l_2=3\leq x \leq r_2=4\), we find the index \(i = 2\). We reverse the substring from index \(x=3\) to \(l_2+r_2-x=3+4-3=4\). After this modification, our string is ""badc"".In the second test case:The initial string is ""abcde"". In the first modification, we have \(x=1\). Since \(l_1=1\leq x \leq r_1=1\), we find the index \(i = 1\). We reverse the substring from index \(x=1\) to \(l_1+r_1-x=1+1-1=1\). After this modification, our string hasn't changed (""abcde""). In the second modification, we have \(x=2\). Since \(l_2=2\leq x \leq r_2=2\), we find the index \(i = 2\). We reverse the substring from index \(x=2\) to \(l_2+r_2-x=2+2-2=2\). After this modification, our string hasn't changed (""abcde""). In the third modification (and the last modification), we have \(x=3\). Since \(l_3=3\leq x \leq r_3=5\), we find the index \(i = 3\). We reverse the substring from index \(x=3\) to \(l_3+r_3-x=3+5-3=5\). After this modification, our string is ""abedc"".
|
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 string \(s\) consisting of characters 'a' and 'b'. Consider an infinite string formed with repetitions of string \(s\): \(sss \ldots\) Suppose that Alice and Bob play rounds according to this infinite string, left to right. If a character of the string \(sss \ldots\) is 'a', then Alice wins the round; if it's 'b', Bob wins the round. As soon as one of the players wins two rounds, the set ends in their favor, and a new set starts from the next round.Let's define \(a_i\) as the number of sets won by Alice among the first \(i\) sets while playing according to the given scenario. Let's also define \(r\) as the limit of ratio \(\frac{a_i}{i}\) as \(i \rightarrow \infty\). If \(r > \frac{1}{2}\), we'll say that scenario \(s\) is winning for Alice. If \(r = \frac{1}{2}\), we'll say that scenario \(s\) is tied. If \(r < \frac{1}{2}\), we'll say that scenario \(s\) is winning for Bob.You are given a string \(s\) consisting of characters 'a', 'b', and '?'. Consider all possible ways of replacing every '?' with 'a' or 'b' to obtain a string consisting only of characters 'a' and 'b'. Count how many of them result in a scenario winning for Alice, how many result in a tied scenario, and how many result in a scenario winning for Bob. Print these three numbers modulo \(998\,244\,353\).
|
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 turns, with the score of \(2:1\) each — the scenario is tied; \(s = \mathtt{bb}\): Bob wins every set \(2:0\) — the scenario is winning for Bob.
|
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 in the set; - \(x\) — remove the integer \(x\) from the set. It is guaranteed that this integer is contained in the set; ? \(k\) — find the \(k\text{-mex}\) of the set. In our problem, we define the \(k\text{-mex}\) of a set of integers as the smallest non-negative integer \(x\) that is divisible by \(k\) and which is not contained in the set.
|
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 integer \(x\) is given in the format - \(x\) (\(1 \leq x \leq 10^{18}\)). It is guaranteed that \(x\) is contained in the set.A search query of \(k\text{-mex}\) is given in the format ? \(k\) (\(1 \leq k \leq 10^{18}\)).It is guaranteed that there is at least one query of type ?.
|
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 divisible by \(2\) and is not in the set is \(6\).In the second example: Initially, the set contains only the element \(\{0\}\). After adding an integer \(100\) the set contains elements \(\{0, 100\}\). \(100\text{-mex}\) of the set is \(200\). After adding an integer \(200\) the set contains elements \(\{0, 100, 200\}\). \(100\text{-mex}\) of the set \(300\). After removing an integer \(100\) the set contains elements \(\{0, 200\}\). \(100\text{-mex}\) of the set is \(100\). After adding an integer \(50\) the set contains elements \(\{0, 50, 200\}\). \(50\text{-mex}\) of the set is \(100\). After removing an integer \(50\) the set contains elements \(\{0, 200\}\). \(100\text{-mex}\) of the set is \(50\).
|
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 to help if Rostislav solves a simple task (and if he doesn't, then why would he need Splay trees anyway?)Given integers l, r and k, you need to print all powers of number k within range from l to r inclusive. However, Rostislav doesn't want to spent time doing this, as he got interested in playing a network game called Agar with Gleb. Help him!
|
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\) is the nearest to the vertex \(i\) ancestor of \(i\). Each vertex is a child of its parent. In the given tree the parent of the vertex \(i\) is the vertex \(p_i\). For the root, the value \(p_i\) is \(-1\). An example of a tree with \(n=8\), the root is vertex \(5\). The parent of the vertex \(2\) is vertex \(3\), the parent of the vertex \(1\) is vertex \(5\). The ancestors of the vertex \(6\) are vertices \(4\) and \(5\), the ancestors of the vertex \(7\) are vertices \(8\), \(3\) and \(5\) You noticed that some vertices do not respect others. In particular, if \(c_i = 1\), then the vertex \(i\) does not respect any of its ancestors, and if \(c_i = 0\), it respects all of them.You decided to delete vertices from the tree one by one. On each step you select such a non-root vertex that it does not respect its parent and none of its children respects it. If there are several such vertices, you select the one with the smallest number. When you delete this vertex \(v\), all children of \(v\) become connected with the parent of \(v\). An example of deletion of the vertex \(7\). Once there are no vertices matching the criteria for deletion, you stop the process. Print the order in which you will delete the vertices. Note that this order is unique.
|
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 the vertex \(i\) respects its parents, and \(c_i = 1\), if the vertex \(i\) does not respect any of its parents. The root of the tree has \(-1\) instead of the parent index, also, \(c_i=0\) for the root. It is guaranteed that the values \(p_i\) define a rooted tree with \(n\) vertices.
|
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 vertex \(2\) will be connected with the vertex \(3\) after deletion; then you will delete the vertex \(2\), because it does not respect ancestors and all its children (the only vertex \(4\)) do not respect it; the vertex \(4\) will be connected with the vertex \(3\); then you will delete the vertex \(4\), because it does not respect ancestors and all its children (there are none) do not respect it (vacuous truth); you will just delete the vertex \(4\); there are no more vertices to delete. In the second example you don't need to delete any vertex: vertices \(2\) and \(3\) have children that respect them; vertices \(4\) and \(5\) respect ancestors. In the third example the tree will change this way:
|
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 the MEX of the first \(k\) numbers of the array \(a\) to the end of array \(b\) and erase them from the array \(a\), shifting the positions of the remaining numbers in \(a\). But, since Mihai loves big arrays as much as the MEX concept, he wants the new array \(b\) to be the lexicographically maximum. So, Mihai asks you to tell him what the maximum array \(b\) that can be created by constructing the array optimally is.An array \(x\) is lexicographically greater than an array \(y\) if in the first position where \(x\) and \(y\) differ \(x_i > y_i\) or if \(|x| > |y|\) and \(y\) is a prefix of \(x\) (where \(|x|\) denotes the size of the array \(x\)).The MEX of a set of non-negative integers is the minimal non-negative integer such that it is not in the set. For example, MEX({\({1, 2, 3}\)}) \(= 0\) and MEX({\({0, 1, 2, 4, 5}\)}) \(= 3\).
|
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 case contains \(n\) non-negative integers \(a_1, \ldots, a_n\) (\(0 \leq a_i \leq n\)), where \(a_i\) is the \(i\)-th integer from the array \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
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 starting with a number smaller than \(4\).In the second test case, there are two ways to obtain the maximum array: first selecting \(k=6\), then \(k=2\), or first selecting \(k=7\) and then \(k=1\).
|
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 position in the string s. For example, the string ""aba"" has six substrings: ""a"", ""b"", ""a"", ""ab"", ""ba"", ""aba"".You are given a string s, consisting of lowercase Latin letters and characters ""?"". You are also given a string p, consisting of lowercase Latin letters only. Let's assume that a string is good if you can obtain an anagram of the string p from it, replacing the ""?"" characters by Latin letters. Each ""?"" can be replaced by exactly one character of the Latin alphabet. For example, if the string p = «aba», then the string ""a??"" is good, and the string «?bc» is not. Your task is to find the number of good substrings of the string s (identical substrings must be counted in the answer several times).
|
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""), ""b?c"" (""?"" can be replaced by ""a"").
|
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 value as a non-negative integer power of k. Total affection value of a continuous segment of chemicals is the sum of affection values of each chemical in that segment.Help her to do so in finding the total number of such segments.
|
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 sample, Molly can choose segments [1, 2], [3, 3], [3, 4].
|
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 edges.For all \(k\) such that \(0 \leq k \leq n\), Tenzing wants to know the maximum value of the tree when \(k\) vertices are painted black and \(n-k\) vertices are painted white.
|
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 given edges form a tree.
|
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 all the teams possess numbers from 1 to n. The splitting of students into teams is performed in the following manner: while there are people who are not part of a team, a person with the best total score is chosen among them (the captain of a new team), this person chooses for himself two teammates from those who is left according to his list of priorities. The list of every person's priorities is represented as a permutation from the rest of 3n - 1 students who attend the centre, besides himself.You are given the results of personal training sessions which are a permutation of numbers from 1 to 3n, where the i-th number is the number of student who has won the i-th place. No two students share a place. You are also given the arrangement of the already formed teams in the order in which they has been created. Your task is to determine the list of priorities for the student number k. If there are several priority lists, choose the lexicographically minimal one.
|
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 three integers from 1 to 3n — each line describes the members of a given team. The members of one team can be listed in any order, but the teams themselves are listed in the order in which they were created. It is guaranteed that the arrangement is correct, that is that every student is a member of exactly one team and those teams could really be created from the given results using the method described above.The last line contains number k (1 ≤ k ≤ 3n) which is the number of a student for who the list of priorities should be found.
|
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 ≤ j < i) aj = bj. Note, that the list 1 9 10 is lexicographically less than the list 1 10 9. That is, the comparison of lists is different from the comparison of lines.
|
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 lowercase Latin letter, append \(c\) at the end of the string \(r\); otherwise, delete the last character from \(r\) (if \(r\) is empty before deleting the last character — the function crashes); return \(r\) as the result of the function. You are given two strings \(s\) and \(t\). You have to delete the minimum possible number of characters from \(s\) so that \(f(s) = t\) (and the function does not crash). Note that you aren't allowed to insert new characters into \(s\) or reorder the existing ones.
|
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 from \(s\) so that \(f(s) = t\).
|
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^{n-1}\) distinct\(^\dagger\) edge-weighted trees with \(n\) vertices numbered from \(1\) to \(n\) such that each edge is either \(1\) or \(-1\). Your task is to find the sum of \(d(1,n)^\ddagger\) of all such trees that are good. Since the answer can be quite large, you only need to find it modulo \(998\,244\,353\).\(^\dagger\) Two trees are considered to be distinct if either: there exists two vertices such that there is an edge between them in one of the trees, and not in the other. there exists two vertices such that there is an edge between them in both trees but the weight of the edge between them in one tree is different from the one in the other tree. Note that by Cayley's formula, the number of trees on \(n\) labeled vertices is \(n^{n-2}\). Since we have \(n-1\) edges, there are \(2^{n-1}\) possible assignment of weights(weight can either be \(1\) or \(-1\)). That is why total number of distinct edge-weighted tree is \(n^{n-2} \cdot 2^{n-1}\).\(^\ddagger\) \(d(u,v)\) denotes the sum of the weight of all edges on the unique simple path from \(u\) to \(v\).
|
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 trees. The value of \(d(1,4)\) is: \(-2\) for \(2\) trees; \(-1\) for \(8\) trees; \(0\) for \(4\) trees; \(1\) for \(2\) trees. The sum of \(d(1,4)\) over all trees is \(2 \cdot (-2) + 8 \cdot (-1) + 4 \cdot (0) + 2 \cdot (1) = -10\), which is \(998\,244\,343\) under modulo \(998\,244\,353\).
|
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 (because if they live in the same city, then it is easier to drop in).Strange but true, the cost of sending the letter is exactly equal to the distance between the sender's city and the recipient's city.For each city calculate two values mini and maxi, where mini is the minimum cost of sending a letter from the i-th city to some other city, and maxi is the the maximum cost of sending a letter from the i-th city to some other city
|
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 it such that each of them contains string t as a substring? More formally, you need to calculate the number of ways to choose two sequences a1, a2, ..., ak and b1, b2, ..., bk satisfying the following requirements: k ≥ 1 t is a substring of string saisai + 1... sbi (string s is considered as 1-indexed). As the number of ways can be rather large print it modulo 109 + 7.
|
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 thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible.Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic).
|
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 starting at any position out of it. Today your first customer asks you some questions of type ""Is there any difference between substrings of length \(k\) if one of them starts at position \(i\) and another one starts from the \(j\)-th position?"" You are to answer all these questions.Please note that in your shop strings are over an alphabet of size \(987\,898\,789\).
|
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 \(pos_2\) (\(1\leq len\leq n\), \(1\leq pos_1, pos_2\leq n - len + 1\)) denoting the corresponding query.
|
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 of the i-th point. The given points can coincide.
|
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\) sectors number from \(0\) to \(n - 1\). There is no ball and the winning sector is determined by a static arrow pointing to one of the sectors. Sectors' indexes go in the natural order and the wheel always spins in the direction of indexes increment. That means that sector \(i + 1\) goes right after sector \(i\) for all \(i\) from \(0\) to \(n - 2\), and sector \(0\) goes right after sector \(n - 1\).After a bet is made, the player is allowed to pull the triggering handle herself and cause the wheel to spin. If the player's initial pull is made with the force equal to positive integer \(f\), the wheel will spin for \(f\) seconds. During the first second it will advance \(f\) sectors, the next second it will advance \(f - 1\) sectors, then \(f - 2\) sectors, and so on until it comes to a complete stop. After the wheel comes to a complete stop, the sector which the arrow is pointing to is the winning one.The roulette's arrow currently points at sector \(x\). Vesper knows that she can pull the handle with any integer force from \(1\) to \(p\) inclusive. Note that it is not allowed to pull the handle with force \(0\), i. e. not pull it all. The biggest prize is awarded if the winning sector is \(0\). Now Vesper wonders if she can make sector \(0\) win by pulling the triggering handle exactly once?
|
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 are the number of sectors on the wheel, the current sector the arrow points at, and the maximum force that Vesper can pull the handle with, respectively.It is guaranteed that the sum of \(n\) over all test cases doesn't exceed \(2 \cdot 10^5\).
|
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 strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses.
|
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 arrow will point at sector \(0\).In the third example, Vesper can pull the handle with the force \(4\) so the wheel will spin \(4 + 3 + 2 + 1 = 10\) sectors and will point at sector \(0\) again.In the fourth example, Vesper can pull the handle with the force \(5\) so the wheel will spin \(5 + 4 + 3 + 2 + 1 = 15\) sectors. That will make the wheel make one full turn plus \(4\) more sectors.In the fifth example, whatever force Vesper chooses to pull the handle with, she can only make sectors \(1\) and \(2\) win.
|
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 prevent ticket hoarding, the concert organizers have implemented the following measures: A person may purchase no more than \(m\) tickets per day. If a person purchases \(x\) tickets on day \(i\), all subsequent days (i.e. from day \(i+1\) onwards) will have their prices per ticket increased by \(x\). For example, if \(a = [1, 3, 8, 4, 5]\) and you purchase \(2\) tickets on day \(1\), they will cost \(2\) in total, and the prices from day \(2\) onwards will become \([5, 10, 6, 7]\). If you then purchase \(3\) more tickets on day \(2\), they will cost in total an additional \(15\), and the prices from day \(3\) onwards will become \([13, 9, 10]\).Find the minimum spending to purchase \(k\) tickets.
|
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(nm, 10^9)\)) — the number of sale days, the maximum amount of ticket purchasable each day, and the number of tickets to be bought at the end.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) — the price per ticket for each of the upcoming \(n\) days.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(3 \cdot 10^5\).
|
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 \(4\). The price per ticket for the remaining day is \([3]\). Buy \(2\) tickets on the fourth day with cost \(6\). In the second test case, there is only one way to buy \(8\) tickets: Buy \(2\) tickets on the first day with cost \(16\). The prices per ticket for the remaining days are \([8, 6, 4]\). Buy \(2\) tickets on the second day with cost \(16\). The prices per ticket for the remaining days are \([8, 6]\). Buy \(2\) tickets on the third day with cost \(16\). The price per ticket for the remaining day is \([8]\). Buy \(2\) tickets on the fourth day with cost \(16\).
|
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 with base length equal to 1 and height equal to h. Igor wants to make n - 1 cuts parallel to the base to cut the carrot into n pieces. He wants to make sure that all n pieces have the same area. Can you help Igor determine where to cut the carrot so that each piece have equal area? Illustration to the first example.
|
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 doesn't exceed 10 - 6.Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .
|
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 array).For a positive integer \(n\), we call a permutation \(p\) of length \(n\) good if the following condition holds for every pair \(i\) and \(j\) (\(1 \le i \le j \le n\)) — \((p_i \text{ OR } p_{i+1} \text{ OR } \ldots \text{ OR } p_{j-1} \text{ OR } p_{j}) \ge j-i+1\), where \(\text{OR}\) denotes the bitwise OR operation. In other words, a permutation \(p\) is good if for every subarray of \(p\), the \(\text{OR}\) of all elements in it is not less than the number of elements in that subarray. Given a positive integer \(n\), output any good permutation of length \(n\). We can show that for the given constraints such a permutation always exists.
|
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,6]\) is also good.
|
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 value of k (k > 1) is a secret integer known only to them. It is also known that each thief's bag can carry at most n chocolates (if they intend to take more, the deal is cancelled) and that there were exactly four thieves involved. Sadly, only the thieves know the value of n, but rumours say that the numbers of ways they could have taken the chocolates (for a fixed n, but not fixed k) is m. Two ways are considered different if one of the thieves (they should be numbered in the order they take chocolates) took different number of chocolates in them.Mike want to track the thieves down, so he wants to know what their bags are and value of n will help him in that. Please find the smallest possible value of n or tell him that the rumors are false and there is no such n.
|
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 possibilities: (1, 2, 4, 8), (1, 3, 9, 27), (2, 4, 8, 16), (2, 6, 18, 54), (3, 6, 12, 24), (4, 8, 16, 32), (5, 10, 20, 40), (6, 12, 24, 48).There is no n leading to exactly 10 ways of stealing chocolates in the third sample case.
|
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 clusters of islands, conveniently coloured red, blue and purple. The clusters consist of a, b and c distinct islands respectively.Bridges have been built between some (possibly all or none) of the islands. A bridge bidirectionally connects two different islands and has length 1. For any two islands of the same colour, either they shouldn't be reached from each other through bridges, or the shortest distance between them is at least 3, apparently in order to prevent oddities from spreading quickly inside a cluster.The Fire Sisters are ready for the unknown, but they'd also like to test your courage. And you're here to figure out the number of different ways to build all bridges under the constraints, and give the answer modulo 998 244 353. Two ways are considered different if a pair of islands exist, such that there's a bridge between them in one of them, but not in the other.
|
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, respectively.
|
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)\).For each cell \((i, j)\) BThero had two values: The cost of the cell, which is a single positive integer. The direction of the cell, which is one of characters L, R, D, U. Those characters correspond to transitions to adjacent cells \((i, j - 1)\), \((i, j + 1)\), \((i + 1, j)\) or \((i - 1, j)\), respectively. No transition pointed outside of the matrix. Let us call a cell \((i_2, j_2)\) reachable from \((i_1, j_1)\), if, starting from \((i_1, j_1)\) and repeatedly moving to the adjacent cell according to our current direction, we will, sooner or later, visit \((i_2, j_2)\). BThero decided to create another matrix from the existing two. For a cell \((i, j)\), let us denote \(S_{i, j}\) as a set of all reachable cells from it (including \((i, j)\) itself). Then, the value at the cell \((i, j)\) in the new matrix will be equal to the sum of costs of all cells in \(S_{i, j}\). After quickly computing the new matrix, BThero immediately sent it to his friends. However, he did not save any of the initial matrices! Help him to restore any two valid matrices, which produce the current one.
|
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\) integers — the elements of the produced matrix. Each element belongs to the segment \([2, 10^9]\).It is guaranteed that \(\sum{(n \cdot m)}\) over all test cases does not exceed \(10^5\).
|
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 direction matrix should be valid. No direction should point outside of the matrix.
|
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 vertices, numbered from 1 through n. There's a lowercase English letter written in each tunnel (edge). Upside down is a magical world. There are m types of monsters in upside down, numbered from 1 through m. Each type of monster has a special word that gives them powers. The special word of type i is si. There are q monsters in upside down. Each one is at a junction (vertex) and is going to some other junction. If monster of type k goes from junction i to junction j, the power it gains is the number of times it sees its special world (sk) consecutively in the tunnels. More formally: If f(i, j) is the string we get when we concatenate the letters written in the tunnels on the shortest path from i to j, then the power the monster gains is the number of occurrences of sk in f(i, j).Hopper and Eleven want to get prepared, so for each monster, they want to know the power the monster gains after moving.
|
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 guaranteed that the given graph is a tree.The next m lines contain the special words. i-th line of them contains a single string si (1 ≤ |si| ≤ 105), consisting of lowercase English letters. It is guaranteed that |s1| + |s2| + ... + |sm| ≤ 105).The next q lines contain the monsters. Each line contains three integers i, j and k (1 ≤ i, j ≤ n, i ≠ j, 1 ≤ k ≤ m), meaning a monster of type k is going from junction number i to junction number j.
|
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 that \(i < j\). Indices are chosen equally probable among all pairs of indices \((i, j)\) such that \(1 \le i < j \le n\). If \(a_i > a_j\), then swap elements \(a_i\) and \(a_j\). What is the expected number of such operations you will perform before the array becomes sorted?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{998\,244\,353}\). Output the integer equal to \(p \cdot q^{-1} \bmod 998\,244\,353\). In other words, output such an integer \(x\) that \(0 \le x < 998\,244\,353\) and \(x \cdot q \equiv p \pmod{998\,244\,353}\).
|
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 case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(a_i \in \{0, 1\}\)) — elements of the array.It's guaranteed that sum of \(n\) over all test cases does not exceed \(200\,000\).
|
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}{3}\), the probability that the array will become sorted after two operations is \(\frac{2}{3} \cdot \frac{1}{3}\), the probability that the array will become sorted after three operations is \(\frac{2}{3} \cdot \frac{2}{3} \cdot \frac{1}{3}\) and so on. The expected number of operations is \(\sum \limits_{i=1}^{\infty} \left(\frac{2}{3} \right)^{i - 1} \cdot \frac{1}{3} \cdot i = 3\).In the second test case the array is already sorted so the expected number of operations is zero.In the third test case the expected number of operations equals to \(\frac{75}{4}\) so the answer is \(75 \cdot 4^{-1} \equiv 249\,561\,107 \pmod {998\,244\,353}\).
|
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 an example of the translation:Define \(T\) as a set of points which is the union of all \(P(x,y)\) such that the origin \((0,0)\) lies in \(P(x,y)\) (both strictly inside and on the boundary). There is also an equivalent definition: a point \((x,y)\) lies in \(T\) only if there are two points \(A,B\) in \(P\) such that \(\overrightarrow {AB} = \overrightarrow {(x,y)}\). One can prove \(T\) is a polygon too. For example, if \(P\) is a regular triangle then \(T\) is a regular hexagon. At the picture below \(P\) is drawn in black and some \(P(x,y)\) which contain the origin are drawn in colored: The spaceship has the best aerodynamic performance if \(P\) and \(T\) are similar. Your task is to check whether the polygons \(P\) and \(T\) are similar.
|
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 order and these points form a strictly convex polygon.
|
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 Billy are great fans of even numbers, that's why they want to divide the watermelon in such a way that each of the two parts weighs even number of kilos, at the same time it is not obligatory that the parts are equal. The boys are extremely tired and want to start their meal as soon as possible, that's why you should help them and find out, if they can divide the watermelon in the way they want. For sure, each of them should get a part of positive weight.
|
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 the answer modulo 109 + 7.Sequence a1, ..., ak is called to be a subsequence of sequence b1, ..., bl, if there is a sequence of indices 1 ≤ i1 < ... < ik ≤ l, such that a1 = bi1, ..., ak = bik. In particular, an empty sequence (i.e. the sequence consisting of zero elements) is a subsequence of any sequence.
|
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 all increasing sequences formed from numbers from 0 to 6. It is easy to see that there are 27 = 128 such sequences.
|
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 interval \([x_i - s_i; x_i + s_i]\).It is possible to increment the scope of any antenna by \(1\), this operation costs \(1\) coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from \(1\) to \(m\) inclusive covered by at least one antenna. Note that it is authorized to cover positions outside \([1; m]\), even if it's not required.What is the minimum amount of coins needed to achieve this modernization?
|
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 will cover interval \([300 - 214; 300 + 214]\), which is \([86; 514]\) Increase the scope of the third antenna by \(31\), so that it becomes \(10 + 31 = 41\). This antenna will cover interval \([554 - 41; 554 + 41]\), which is \([513; 595]\) Total cost is \(40 + 210 + 31 = 281\). We can prove that it's the minimum cost required to make all positions from \(1\) to \(595\) covered by at least one antenna.Note that positions \(513\) and \(514\) are in this solution covered by two different antennas, but it's not important.—In the second example, the first antenna already covers an interval \([0; 2]\) so we have nothing to do.Note that the only position that we needed to cover was position \(1\); positions \(0\) and \(2\) are covered, but it's not important.
|
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{00110001}) = 4\) because \(t\) can be partitioned as \(\texttt{[00][11][000][1]}\) where each bracketed segment consists of identical characters.Skibidus gives you a binary string \(s\) and \(q\) queries. In each query, a single character of the string is flipped (i.e. \(\texttt{0}\) changes to \(\texttt{1}\) and \(\texttt{1}\) changes to \(\texttt{0}\)); changes are saved after the query is processed. After each query, output the sum over all \(f(b)\) where \(b\) is a non-empty subsequence\(^{\text{†}}\) of \(s\), modulo \(998\,244\,353\).\(^{\text{∗}}\)A binary string consists of only characters \(\texttt{0}\) and \(\texttt{1}\).\(^{\text{†}}\)A subsequence of a string is a string which can be obtained by removing several (possibly zero) characters from the original string.
|
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 following line contains \(q\) integers \(v_1, v_2, \ldots, v_q\) (\(1 \leq v_i \leq |s|\)), denoting \(s_{v_i}\) is flipped for the \(i\)'th query.It is guaranteed that the sum of \(|s|\) and the sum of \(q\) over all test cases does not exceed \(2 \cdot 10^5\).
|
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}) = 2\) \(f(s_1 s_2 s_3) = f(\texttt{001}) = 2\) The sum of these values is \(10\), modulo \(998\,244\,353\).
|
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 depth of k-ary heap in the subtree of u (including u). Your goal is to compute .
|
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 — move from (x, y) to (x + 1, y). Ivan entered a sequence of n commands, and the robot processed it. After this sequence the robot ended up in the starting cell (0, 0), but Ivan doubts that the sequence is such that after performing it correctly the robot ends up in the same cell. He thinks that some commands were ignored by robot. To acknowledge whether the robot is severely bugged, he needs to calculate the maximum possible number of commands that were performed correctly. Help Ivan to do the calculations!
|
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 current Daleks' plan – which is to use a minimum spanning tree of Corridors. Heidi knows that all energy requirements for different Corridors are now different, and that the Daleks have a single unique plan which they are intending to use.Your task is to calculate the number \(E_{max}(c)\), which is defined in the same way as in the easy version – i.e., the largest \(e \le 10^9\) such that if we changed the energy of corridor \(c\) to \(e\), the Daleks might use it – but now for every corridor that Heidi considers.
|
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 be connected. All energy requirements \(e\) are distinct.
|
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. His backpack contains: \(a\) daily rations of fish food; \(b\) daily rations of rabbit stew; \(c\) daily rations of chicken stakes. Polycarp has to choose such day of the week to start his trip that his cat can eat without additional food purchases as long as possible. 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.
|
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 the week. In any case there are food supplies only for one week in Polycarps backpack.In the third example Polycarp can start his trip in any day, excluding Wednesday, Saturday and Sunday. In this case, the cat will eat three different dishes in three days. Nevertheless that after three days of a trip there will be \(99\) portions of rabbit stew in a backpack, can cannot eat anything in fourth day of a trip.
|
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 operation any number of times: First, he cuts his string \(S\) into three (possibly empty) contiguous substrings \(A, B\) and \(C\). Then, he glues them back by using a '(' and a ')' characters, resulting in a new string \(S\) = \(A\) + ""("" + \(B\) + "")"" + \(C\).For example, if \(S\) = ""))(("" and AmShZ cuts it into \(A\) = """", \(B\) = ""))"", and \(C\) = ""(("", He will obtain \(S\) = ""()))(("" as a new string. After performing some (possibly none) operations, AmShZ gives his string to Keshi and asks him to find the initial string. Of course, Keshi might be able to come up with more than one possible initial string. Keshi is interested in finding the lexicographically smallest possible initial string.Your task is to help Keshi in achieving his goal.A string \(a\) is lexicographically smaller than a string \(b\) if and only if one of the following holds: \(a\) is a prefix of \(b\), but \(a \ne b\); in the first position where \(a\) and \(b\) differ, the string \(a\) has a letter that appears earlier in the alphabet than the corresponding letter in \(b\).
|
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 will select an interval \([l,r]\) (\(1\le l\le r\le n\)), then skip all of the contests in this interval. After that, his rating will be recalculated in the following way: Initially, his rating is \(x=0\); For each \(1\le i\le n\), after the \(i\)-th contest, If \(l\le i\le r\), this contest will be skipped, and the rating will remain unchanged; Otherwise, his rating will be updated according to the following rules: If \(a_i>x\), his rating \(x\) will increase by \(1\); If \(a_i=x\), his rating \(x\) will remain unchanged; If \(a_i<x\), his rating \(x\) will decrease by \(1\). You have to help Kevin to find his maximum possible rating after the recalculation if he chooses the interval \([l,r]\) optimally. Note that Kevin has to skip at least one contest.
|
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 second line contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(1\le a_i\le n\)) — the performance ratings in the contests.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(3 \cdot 10^5\).
|
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 \xrightarrow{a_1=1} 1 \xrightarrow{a_2=2} 2 \xrightarrow{\mathtt{skip}} 2 \xrightarrow{\mathtt{skip}} 2 \xrightarrow{\mathtt{skip}} 2 \xrightarrow{a_6=3} 3 \xrightarrow{a_7=4} 4 \)\(In the third test case, Kevin must skip the only contest, so his rating will remain at the initial value of \)0\(.In the fourth test case, Kevin's optimal choice is to select the interval \)[7,9]\(. During the recalculation, his rating changes as follows:\)\( 0 \xrightarrow{a_1=9} 1 \xrightarrow{a_2=9} 2 \xrightarrow{a_3=8} 3 \xrightarrow{a_4=2} 2 \xrightarrow{a_5=4} 3 \xrightarrow{a_6=4} 4 \xrightarrow{\mathtt{skip}} 4 \xrightarrow{\mathtt{skip}} 4 \xrightarrow{\mathtt{skip}} 4 \)\(In the fifth test case, Kevin's optimal choice is to select the interval \)[5,9]$$$.
|
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 making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).For example, for the sequence \([2, 1, 5, 4, 3]\) the answer is \(4\) (you take \(2\) and the sequence becomes \([1, 5, 4, 3]\), then you take the rightmost element \(3\) and the sequence becomes \([1, 5, 4]\), then you take \(4\) and the sequence becomes \([1, 5]\) and then you take \(5\) and the sequence becomes \([1]\), the obtained increasing sequence is \([2, 3, 4, 5]\)).
|
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' otherwise. If there are multiple answers, you can print any.
|
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 forms a perfect matching.
|
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 three points are on the same line.
|
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 aims to rebuild her castle into a point symmetric convex polygon. Here, a polygon is point symmetric if there exists a center \(c\) such that for every point \(p\) in the polygon, the reflection \(p^\prime\) of \(p\) across \(c\) is also in the polygon.While designing an arbitrary point symmetric convex polygon shape is easy, the cost of rebuilding is very high. After some estimation, A-Ju found that the cost of rebuilding is proportional to the area of the symmetric difference\(^{\text{†}}\) between the original castle and the new castle. See the following figure for an example: In the example above, A-Ju's castle is a shape of the convex polygon formed by points \((3, 7) - (2, 1) - (6, 3)\). After rebuilding her castle into the shape of the polygon formed by \((3, 7) - (\frac{7}{3}, 3) - (\frac{13}{3}, \frac{1}{3}) - (5, \frac{13}{3})\), the area of the symmetric difference between these two polygons will be \(\frac{11}{3}\). The difference can be calculated as the sum of the additional area (represented by the green grid region) and the shaved area (represented by the red line region).Please write a program that helps A-Ju design a blueprint of the new castle, such that the area of the symmetric difference between the original one and the new one is minimized. You only need to output the minimum value since A-Ju wants to estimate her cost first.\(^{\text{∗}}\)A polygon \(P\) is convex if for every two points \(p, q \in P\), the line segment connecting them is also contained in \(P\), i.e., \(tp + (1-t)q \in P\) for all \(t \in [0, 1]\). Equivalently, it is a polygon whose interior angles are all less than \(180^{\circ}\).\(^{\text{†}}\)The symmetric difference of two polygons is the part of the 2D plane that belongs to exactly one polygon.
|
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 500\) \(|x_i|, |y_i|\leq 10^4\) The vertices are given in a counterclockwise order and are guaranteed to form a convex polygon without three collinear points.
|
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 considered correct if \(\frac{|a-b|}{\max(1,|b|)}\le 10^{-4}\).
|
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. No two lines share an endpoint.Now a group of 6 polar bears (Alice, Bob, Carol, Dave, Eve, Frank) are going to build caves on the endpoints. Each polar bear would build a cave and live in it. No two polar bears can build a cave on the same endpoints. Alice and Bob is a pair of superstitious lovers. They believe the lines are carved by aliens (or humans, which are pretty much the same thing to polar bears), and have certain spiritual power. Therefore they want to build their caves on two endpoints which are connected by a line. The same for Carol and Dave, Eve and Frank.The distance between two caves X and Y is defined as one plus minimum number of other caves one need to pass through in order to travel from X to Y along the boundary of the ice (endpoints without caves are not counted).To ensure fairness, the distances between the three pairs of lovers have to be the same (that is, the distance between Alice and Bob, the distance between Carol and Dave, and the distance between Eve and Frank are the same).The figures below show two different configurations, where the dots on the circle are the endpoints. The configuration on the left is not valid. Although each pair of lovers (A and B, C and D, E and F) is connected a line, the distance requirement is not satisfied. The distance between A and B is 2 (one can go from A to B in the clockwise direction passing through F). The distance between E and F is also 2. However, the distance between C and D is 1 (one can go from C to D in the counter-clockwise direction without passing through any other caves). The configuration on the right is valid. All three pairs have the same distance 1. Count the number of ways to build the caves under the requirements. Two configurations are considered the same if the same set of 6 endpoints are used.
|
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 \(1\) to \(n\), and each cell \(i\) initially contains the integer \(p_i\). Before all actions, the carriage is at cell number \(1\) and there is nothing in its buffer storage. The cell on which the carriage is located is called the current cell. The carriage can perform five types of operations: Take the integer from the current cell, if it is not empty, and put it in the carriage buffer, if it is empty (this buffer can contain no more than one integer). Put the integer from the carriage buffer, if it is not empty, into the current cell, if it is empty. Swap the number in the carriage buffer with the number in the current cell, if both the buffer and the cell contain integers. Move the carriage from the current cell \(i\) to cell \(i + 1\) (if \(i < n\)), while the integer in the buffer is preserved. Reset the carriage, i.e. move it to cell number \(1\), while the integer in the buffer is preserved. Polycarp was very interested in this typewriter, so he asks you to help him understand it and will ask you \(q\) queries of three types: Perform a cyclic shift of the sequence \(p\) to the left by \(k_j\). Perform a cyclic shift of the sequence \(p\) to the right by \(k_j\). Reverse the sequence \(p\). Before and after each query, Polycarp wants to know what minimum number of carriage resets is needed for the current sequence in order to distribute the numbers to their cells (so that the number \(i\) ends up in cell number \(i\)).Note that Polycarp only wants to know the minimum number of carriage resets required to arrange the numbers in their places, but he does not actually distribute them.Help Polycarp find the answers to his queries!
|
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^5\)) — the number of queries.Each of the next \(q\) lines describes a query from Polycarp:The \(j\)-th line, at first, contains the integer \(t_j\) (\(1 \le t_j \le 3\)) — the type of query.If the query is of type \(t_j = 1\) or \(t_j = 2\), then the integer \(k_j\) (\(1 \le k_j \le n\)) — the length of the shift — follows in the same line.
|
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 is \(2\). After reversing the sequence: \(2\ 1\ 3\) — the answer is \(1\). In the third example, the sequences before and after each query look like this: \(3\ 1\ 2\ 5\ 4\) — the answer is \(3\). \(5\ 4\ 3\ 1\ 2\) — the answer is \(2\). \(2\ 1\ 3\ 4\ 5\) — the answer is \(1\). \(3\ 4\ 5\ 2\ 1\) — the answer is \(2\). \(1\ 3\ 4\ 5\ 2\) — the answer is \(1\). \(2\ 5\ 4\ 3\ 1\) — the answer is \(2\).
|
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 value of the expression \(\text{med}(a[l, r]) - \min(a[l, r])\) is maximized.Here: \(\text{med}\) — the median of the subarray, which is the element at position \(\left\lceil \frac{k + 1}{2} \right\rceil\) after sorting the subarray, where \(k\) is its length; \(\min\) — the minimum element of this subarray. For example, consider the array \(a=[1, 4, 1, 5, 3, 3]\) and choose the subarray \(a[2, 5] = [4, 1, 5, 3]\). In sorted form, it looks like \([1, 3, 4, 5]\). \(\text{med}(a[2, 5]) = 4\), since \(\left\lceil \frac{4 + 1}{2} \right\rceil = \) the third element in the sorted subarray is \(4\); \(\min(a[2, 5]) = 1\), since the minimum element is \(1\). In this example, the value \(\text{med} - \min = 4 - 1 = 3\).
|
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, 100)\)) — the elements of the array.It is guaranteed that the sum of \(n\) across all test cases does not exceed \(2 \cdot 10^5\).
|
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 \([2,\ 5]\), \(\text{med} = 5\). The minimum element of the subarray: \(\min = 2\). Therefore, \(\text{med} - \min = 5 - 2 = 3\), which is the maximum answer.In the second test, the array: \(a=[4,\ 1,\ 1,\ 3]\) you can choose the subarray \(a[1,\ 2]\), which consists of the elements \([4,\ 1]\). 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 \([1,\ 4]\), \(\text{med} = 4\). The minimum element of the subarray: \(\min = 1\). Therefore, \(\text{med} - \min = 4 - 1 = 3\).It can be proven that both of these subarrays are optimal and yield the maximum value of the expression \(\text{med} - \min\).
|
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 powers of 2 using arithmetic operations. For simplicity we'll allow only addition and subtraction and will look for a representation of the given number which requires a minimal number of operations.You are given an integer n. You have to represent it as n = a1 + a2 + ... + am, where each of ai is a non-negative power of 2, possibly multiplied by -1. Find a representation which minimizes the value of m.
|
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
|
Профиль горного хребта схематично задан в виде прямоугольной таблицы из символов «.» (пустое пространство) и «*» (часть горы). Каждый столбец таблицы содержит хотя бы одну «звёздочку». Гарантируется, что любой из символов «*» либо находится в нижней строке матрицы, либо непосредственно под ним находится другой символ «*». ....................*..*.......*.**.......*.**..*...**.*********** Пример изображения горного хребта. Маршрут туриста проходит через весь горный хребет слева направо. Каждый день турист перемещается вправо — в соседний столбец в схематичном изображении. Конечно, каждый раз он поднимается (или опускается) в самую верхнюю точку горы, которая находится в соответствующем столбце.Считая, что изначально турист находится в самой верхней точке в первом столбце, а закончит свой маршрут в самой верхней точке в последнем столбце, найдите две величины: наибольший подъём за день (равен 0, если в профиле горного хребта нет ни одного подъёма), наибольший спуск за день (равен 0, если в профиле горного хребта нет ни одного спуска).
|
В первой строке входных данных записаны два целых числа 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).Во втором тестовом примере высоты гор равны: 1, 2, 3, 4, 5. Наибольший подъём равен 1 и находится, например, между горой номер 2 (ее высота равна 2) и горой номер 3 (её высота равна 3). Так как в данном горном хребте нет спусков, то величина наибольшего спуска равна 0.В третьем тестовом примере высоты гор равны: 1, 7, 5, 3, 4, 2, 3. Наибольший подъём равен 6 и находится между горой номер 1 (её высота равна 1) и горой номер 2 (её высота равна 7). Наибольший спуск равен 2 и находится между горой номер 2 (её высота равна 7) и горой номер 3 (её высота равна 5). Такой же спуск находится между горой номер 5 (её высота равна 4) и горой номер 6 (её высота равна 2).
|
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 '\(\text{^}\)' when writing the important integer. For example, instead of the integer \(10^5\), he would have written \(105\), and instead of \(10^{19}\), he would have written \(1019\).Dmitry wants to understand which of the integers on the board could have been the important integer and which could not.
|
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)\) is too simple, so Mr. Chanek defines:\(G(X, Y) = \sum_{i=X}^{N} \sum_{j=Y}^{M} F(i, j)\)Given X and Y, you are tasked to find \(G(X, Y)\).A knight can move from square \((a, b)\) to square \((a', b')\) if and only if \(|a - a'| > 0\), \(|b - b'| > 0\), and \(|a - a'| + |b - b'| = 3\). Of course, the knight cannot leave the chessboard.
|
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 \(\operatorname{min}(a_1,a_2)\), and remove the first element of \(a\). Operation 2: if \(a\) contains at least two elements, change \(a_2\) to \(\operatorname{max}(a_1,a_2)\), and remove the first element of \(a\).Note that after a removal of the first element of \(a\), the former \(a_2\) becomes the first element of \(a\), the former \(a_3\) becomes the second element of \(a\) and so on, and the length of \(a\) reduces by one.Determine if AquaMoon can make \(a\) equal to \(b\) by using these operations.
|
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 case contains a string \(a\) of length \(n\), consisting only \(0\) and \(1\).The third line of each test case contains a string \(b\) of length \(m\), consisting only \(0\) and \(1\).
|
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 fourth 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 fifth test case, you can use Operation 2 three times to make \(a\) become \(10101\), so the first element of \(a\) equals to the first element of \(b\), but it can be proved that no matter how to operate, the second to the fifth elements of \(a\) can't be the same as \(b\).
|
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\) distinct anti-Fibonacci permutations of length \(n\).
|
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 permutations of size \(n\) under the constraints of the problem.
|
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 i-th section of this wall. You know that archer who stands at section i can shoot orcs that attack section located at distance not exceeding r, that is all such sections j that |i - j| ≤ r. In particular, r = 0 means that archers are only capable of shooting at orcs who attack section i.Denote as defense level of section i the total number of archers who can shoot at the orcs attacking this section. Reliability of the defense plan is the minimum value of defense level of individual wall section.There is a little time left till the attack so you can't redistribute archers that are already located at the wall. However, there is a reserve of k archers that you can distribute among wall sections in arbitrary way. You would like to achieve maximum possible reliability of the defence plan.
|
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, ..., an (0 ≤ ai ≤ 109) — the current number of archers at each section.
|
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 them to their current scores. The game has exactly t turns. Memory and Lexa, however, are not good at this game, so they both always get a random integer at their turn.Memory wonders how many possible games exist such that he ends with a strictly higher score than Lexa. Two games are considered to be different if in at least one turn at least one player gets different score. There are (2k + 1)2t games in total. Since the answer can be very large, you should print it modulo 109 + 7. Please solve this problem for Memory.
|
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 wins.
|
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 column c is coloured black.There's another constraint: a row or a column can only be chosen at most once among all operations. In other words, it means that no pair of (i, j) (i < j) exists such that or , where denotes intersection of sets, and denotes the empty set.You are to determine whether a valid sequence of operations exists that produces a given final grid.
|
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 won't be possible to colour the other cells in the center column.
|
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 k integers y such that ai ≤ y ≤ aj and y is divisible by x.In this problem it is meant that pair (i, j) is equal to (j, i) only if i is equal to j. For example pair (1, 2) is not the same as (2, 1).
|
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 wants to find a subset that has the smallest imperfection among all subsets in \(S\) of size \(k\). There can be more than one subset with the smallest imperfection and the same size, but you don't need to worry about it. Kate wants to find all the subsets herself, but she needs your help to find the smallest possible imperfection for each size \(k\), will name it \(I_k\). Please, help Kate to find \(I_2\), \(I_3\), ..., \(I_n\).
|
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 direction of coordinate increasing. The initial coordinate of the \(i\)-th person on the line is \(x_i\), and the speed of the \(i\)-th person is \(v_i\). So the coordinate of the \(i\)-th person at the moment \(t\) will be \(x_i + t \cdot v_i\).Cirno captured the coordinates of \(m\) people in \(k\) consecutive integer moments from \(0\) to \(k - 1\). In every moment, the coordinates of \(m\) people were recorded in arbitrary order.To make the problem more funny, Cirno modified one coordinate at the moment \(y\) (\(0 < y < k-1\)) to a different integer.AquaMoon wants to find the moment \(y\) and the original coordinate \(p\) before the modification. Actually, she is not a programmer at all. So she wasn't able to solve it. Can you help her?
|
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 won't have any interaction process. The only thing you should not forget is to flush the output buffer, after printing the answer. Otherwise, you can get an ""Idleness limit exceeded"" verdict. Refer to the interactive problems guide for the detailed information about flushing the output buffer.The first line contains two integers \(m\) and \(k\) (\(5 \leq m \leq 1000\), \(7 \leq k \leq 1000\)) — the number of people and the number of recorded moments. The next \(k\) lines contain captured positions. \(i\)-th of these lines contains \(m\) integers between \(1\) and \(10^6\) (inclusive), representing positions captured by Cirno at the moment \(i-1\).The input is guaranteed to be valid (i.e. only one integer was modified to a different value according to the problem statement). Also, it is guaranteed, that \(1 \le v_i \le 1000\) for all \(1 \leq i \leq m\).Hack format:The first line should contain two integers \(m\) and \(k\) (\(5 \leq m \leq 1000\), \(7 \leq k \leq 1000\)) — the number of people and the number of moments. In the second line, there should be \(m\) integers \(x_0, x_1, \dots,x_{m - 1}\) (\(1 \le x_i \le 10^6\)), where \(x_i\) is the initial coordinate of the \(i\)-th person.In the third line, there should be \(m\) integers \(v_0, v_1, \dots,v_{m - 1}\) (\(1 \le v_i \le 1000\)), where \(v_i\) is the speed of the \(i\)-th person. It should be true that \(x_i + (k-1) v_i \leq 10^6\) for each \(0 \leq i < m\).In the next \(k\) lines, each line should contain \(m\) integers. \(i\)-th line should contain \(m\) distinct integers \(p_0, p_1, \ldots, p_{m-1}\) (\(0 \leq p_j < m\)). The meaning of these numbers: \(j\)-th integer in the input in the \(i\)-th moment is the coordinate of the \(p_{j}\)-th person.In the last line, there should be three integers \(y\), \(i\), \(c\). Cirno modified the coordinate of the \(i\)-th person at the moment \(y\) to \(c\) (\(1 \leq y \leq k-2\), \(0 \leq i \leq m - 1\), \(1 \leq c \leq 10^6\), \(c \neq x_i + y \cdot v_i\)).
|
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 1 44 3 0 1 21 3 4 0 21 4 0 2 32 4 1 3 02 4 1 3 04 0 12
|
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 ai represents the beauty of the i-th soldier.On each of k days Generals has to send a detachment of soldiers to the pageant. The beauty of the detachment is the sum of the beauties of the soldiers, who are part of this detachment. Payne wants to surprise the jury of the beauty pageant, so each of k days the beauty of the sent detachment should be unique. In other words, all k beauties of the sent detachments must be distinct numbers.Help Payne choose k detachments of different beauties for the pageant. Please note that Payne cannot just forget to send soldiers on one day, that is, the detachment of soldiers he sends to the pageant should never be empty.
|
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 two soldiers with the same beauty.
|
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 the soldiers in the detachment on the i-th day of the pageant. The beauties of the soldiers are allowed to print in any order.Separate numbers on the lines by spaces. It is guaranteed that there is the solution that meets the problem conditions. If there are multiple solutions, print any of them.
|
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 permutation of length \(n\) is an array containing each integer from \(1\) to \(n\) exactly once. 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 array).
|
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. e. n = a·b. 10 pebbles are arranged in two rows, each row has 5 pebbles Once the Smart Beaver has arranged the pebbles, he takes back any of the resulting rows (that is, b pebbles) and discards all other pebbles. Then he arranges all his pebbles again (possibly choosing other values of a and b) and takes back one row, and so on. The game continues until at some point the Beaver ends up with exactly one pebble. The game process can be represented as a finite sequence of integers c1, ..., ck, where: c1 = n ci + 1 is the number of pebbles that the Beaver ends up with after the i-th move, that is, the number of pebbles in a row after some arrangement of ci pebbles (1 ≤ i < k). Note that ci > ci + 1. ck = 1 The result of the game is the sum of numbers ci. You are given n. Find the maximum possible result of the game.
|
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 second move we have two pebbles which can be arranged in a unique way (remember that you are not allowed to put all the pebbles in the same row!) — 2 rows, one pebble per row. c3 = 1, and the game ends with the result of 13. Finally, arrange the pebbles in two rows, five pebbles per row. The same logic leads us to c2 = 5, c3 = 1, and the game ends with the result of 16 — the maximum possible result.
|
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). It can be proven that it is always possible to make all elements of \(a\) equal to zero.
|
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}\)): the numbers to add to \(a_l, a_{l+1}, \ldots, a_r\), respectively; \(b_i\) should be divisible by \(r - l + 1\).
|
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 single integer from some of the stores. The same day, Swiper the fox bought a single integer from all the stores that Dora did not buy an integer from on that day.Dora considers Swiper to be her rival, and she considers that she beat Swiper on day \(i\) if and only if the least common multiple of the numbers she bought on day \(i\) is strictly greater than the least common multiple of the numbers that Swiper bought on day \(i\).The least common multiple (LCM) of a collection of integers is the smallest positive integer that is divisible by all the integers in the collection.However, Dora forgot the values of \(a_i\). Help Dora find out if there are positive integer values of \(a_i\) such that she beat Swiper on every day. You don't need to find what are the possible values of \(a_i\) though.Note that it is possible for some values of \(a_i\) to coincide in a solution.
|
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\) distinct integers, the indices of the stores where Dora bought an integer on the \(i\)-th day. The indices are between \(1\) and \(n\).
|
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 you don't have to restore the integers themselves.
|
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 buys integers \(3\) and \(4\), whose LCM is \(12\).
|
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 \(a_i\) and change its value to the opposite. In other words, if \(a_i = -1\), you can assign the value to \(a_i := 1\), and if \(a_i = 1\), then assign the value to \(a_i := -1\).Determine the minimum number of operations you need to perform to make the array \(a\) good. It can be shown that this is always possible.
|
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 each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(a_i = \pm 1\)) — the elements of the array \(a\).
|
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_4 + a_5 = 1 + (-1) + (-1) + 1 + 1 = 1 \ge 0\) and \(a_1 \cdot a_2 \cdot a_3 \cdot a_4 \cdot a_5 = 1 \cdot (-1) \cdot (-1) \cdot 1 \cdot 1 = 1\). Thus, we performed \(1\) operation.In the third test case, \(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, all conditions are already satisfied and no operations are needed.In the fourth test case, we can assign the values \(a_1 := 1, a_2 := 1, a_3 := 1\). Then \(a_1 + a_2 + a_3 = 1 + 1 + 1 = 3 \ge 0\) and \(a_1 \cdot a_2 \cdot a_3 = 1 \cdot 1 \cdot 1 = 1\). Thus, we performed \(3\) operations.
|
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)\) decides to leave. To fill the gap, let the person who left be numbered \(i\). Each person numbered \(j>i\) will move to the position where the person numbered \(j-1\) is initially at. The following diagram illustrates the case where \(n=2\), \(m=3\), \(r=1\), and \(c=2\). Calculate the sum of the Manhattan distances of each person's movement. If a person was initially at \((r_0, c_0)\) and then moved to \((r_1, c_1)\), the Manhattan distance is \(|r_0-r_1|+|c_0-c_1|\).
|
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 the position where the person who left is initially at.
|
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\) moves. The answer is \(1\).
|
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 already estimated the probability for each event to happen. Event in row \(i\) and column \(j\) will happen with probability \(a_{i, j} \cdot 10^{-4}\). All of the events are mutually independent.Let's call the table winning if there exists a line such that all \(n\) events on it happen. The line could be any horizontal line (cells \((i, 1), (i, 2), \ldots, (i, n)\) for some \(i\)), any vertical line (cells \((1, j), (2, j), \ldots, (n, j)\) for some \(j\)), the main diagonal (cells \((1, 1), (2, 2), \ldots, (n, n)\)), or the antidiagonal (cells \((1, n), (2, n - 1), \ldots, (n, 1)\)).Find the probability of your table to be winning, and output it modulo \(31\,607\) (see Output section).
|
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 other words, output such an integer \(x\) that \(0 \le x < M\) and \(x \cdot q \equiv p \pmod{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 kind of surprises will be waiting?M received her toy box as a birthday present from her mother. A jewellery designer would definitely spare no effort in decorating yet another priceless masterpiece as a starry firmament with exquisitely shaped gemstones. In addition, \(l\) distinct locks secure the tiny universe of her lovely daughter: a hair clip featuring a flower design, a weathered feather pen, a balloon shaped like the letter M ... each piece obscures a precious moment.A few days ago, M rediscovered her toy box when she was reorganizing her bedroom, along with a ring of keys uniquely designed for the toy box. Attached to the key ring are \((l + k)\) keys, of which \(l\) keys are able to open one of the \(l\) locks correspondingly, while the other \(k\) keys are nothing but counterfeits to discourage brute-force attack. To remind the correspondence, M's mother adorned each key with a gemstone of a different type. However, passing days have faded M's memory away.""... So I have to turn to you all,"" M said while laying that ring of keys on the table.K picked up the keys and examined them carefully. ""The appearance of these keys unveils nothing fruitful. Thus, I am afraid that we shall inspect them sequentially.""Although everyone is willing to help M, nobody has a plan. Observing others' reactions, T suggested, ""Let's play a game. Everyone tries a key in turn, and who opens the most locks is amazing.""\(n\) members, including M herself, take turns to unlock the toy box recursively in the same order until all the \(l\) locks are unlocked. At each turn, the current member only selects a single key and tests it on exactly one of the locks. To open the toy box as soon as possible, every member chooses the key and the lock that maximize the probability of being a successful match. If there are multiple such pairs, a member will randomly choose one of such pairs with equal probability. Apparently, if a lock has been matched with a key, then neither the lock nor the key will be chosen again in following attempts.Assume that at the very beginning, the probability that a lock can be opened by any key is equal. If everyone always tries the optimal pairs of keys and locks based on all the historical trials, what will the expected number of successful matches be for each member?
|
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 participating in the game, the number of locks, and the number of counterfeit keys.It is guaranteed that the sum of \(l\) across all test cases does not exceed \(5000\).
|
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 \(q\) are integers and \(q \not \equiv 0 \pmod{M}\). Output the integer equal to \(p \cdot q^{-1} \bmod M\). In other words, output such an integer \(e_i\) that \(0 \le x < M\) and \(e_i \cdot q \equiv p \pmod{M}\).
|
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 successful matches.For the second test case, there are exactly \(2\) locks and \(2\) keys, with each key corresponding to one of the locks. Without extra information, the first member randomly chooses a key and a lock with equal probabilities, for which the probability of success is \(1/2\). If the first member succeeds, the second member will open the other lock with the other key. If the first member fails, then the key she selected can open the other lock, and the other key must correspond to the lock she chose. This information allows both the second and the third member to open a lock. In conclusion, the expected numbers of successful matches will be:$$$\( \begin{split} e_1 &= \frac{1}{2}\times 1 + \frac{1}{2}\times 0 = \frac{1}{2} \equiv 500,000,004 \pmod {10^9+7},\\ e_2 &= \frac{1}{2}\times 1 + \frac{1}{2} \times 1 = 1,\\ e_3 &= \frac{1}{2}\times 0 + \frac{1}{2} \times 1 = \frac{1}{2} \equiv 500,000,004\pmod {10^9+7}. \end{split} \)$$$
|
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 the denotions used in the statement.
|
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 one trip costs 20 byteland rubles, a ticket for 90 minutes costs 50 byteland rubles, a ticket for one day (1440 minutes) costs 120 byteland rubles. Note that a ticket for x minutes activated at time t can be used for trips started in time range from t to t + x - 1, inclusive. Assume that all trips take exactly one minute.To simplify the choice for the passenger, the system automatically chooses the optimal tickets. After each trip starts, the system analyses all the previous trips and the current trip and chooses a set of tickets for these trips with a minimum total cost. Let the minimum total cost of tickets to cover all trips from the first to the current is a, and the total sum charged before is b. Then the system charges the passenger the sum a - b.You have to write a program that, for given trips made by a passenger, calculates the sum the passenger is charged after each trip.
|
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 ≤ i < n.
|
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 already paid 40 rubles, so it is necessary to charge 10 rubles only.
|
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 a_{l+1} \oplus a_{l+2} \ldots \oplus a_r\), where \(\oplus\) denotes the bitwise XOR operation and \(\&\) denotes the bitwise AND operation.Find the length of the longest good subarray of \(a\), or determine that no such subarray exists.
|
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 that, if we delete all the edges except for \(E_L, E_{L+1}, \dots E_R\), the graph is a desert.
|
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_i)\)). It is guaranteed that \(1 \leq U_i, V_i \leq N\) and \(U_i \neq V_i\).
|
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 \(i\)-th task would require \(a_i\) minutes to complete and needs to be submitted to the teacher at most \(d_i\) minutes from now. Also, there are \(m\) new episodes of the series that Johnny and Jill want to discuss. The \(j\)-th episode lasts \(l_j\) minutes. Jill can complete tasks in any order, but she needs to watch the episodes in the order they come. Neither completing a homework task nor watching an episode can be interrupted after starting. Johnny and Jill need to agree on a time \(t_k\) when they would have a call to discuss the series. They are not sure yet which time to choose. For each possible time, compute the maximum number of episodes Jill could watch before that time while still being able to complete all \(n\) homework tasks in time.Note that for the purpose of this problem we assume that discussing the series with Johnny at time \(t_k\) does not consume significant time from Jill and can happen even if she is in the middle of completing any of her homework tasks.
|
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 \le 200\,000\)) — the number of possible times for the call with Jill.The second line contains \(n\) integers \(a_i\) (\(1 \le a_i \le 10^9\)) — the number of minutes it takes to complete the task. The next line contains \(n\) integers \(d_i\) (\(1 \le d_i \le 10^{15}\)) — the deadline before which this task must be completed. The next line contains \(m\) integers \(l_j\) (\(1 \le l_j \le 10^9\)) — the length of episodes in the order they need to be watched. The next line contains \(q\) integers \(t_k\) (\(1 \le t_k \le 10^{15}\)) — the possible times of call with Jill. It is possible to complete all tasks within their respective deadlines.The sum of each of \(n\), \(m\), \(q\) over all test cases in input doesn't exceed \(200\,000\).
|
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)),\)\( where \)f(i, j)\( denotes \)\min(a_i, a_{i + 1}, \ldots, a_j)\(.Alice wants you to help her to maximize the value of the subsequence she choose.A sequence \)s\( is a subsequence of a sequence \)t\( if \)s\( can be obtained from \)t$$$ by deletion of several (possibly, zero or all) elements.
|
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, 20, 18]\).
|
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' key, it deletes the last (rightmost) uppercase letter in the typed string. If there are no uppercase letters in the typed string, then the press is completely ignored.In both cases, the letters 'b' and 'B' are not added to the typed string when these keys are pressed.Consider an example where the sequence of key presses was ""ARaBbbitBaby"". In this case, the typed string will change as follows: """" \(\xrightarrow{\texttt{A}}\) ""A"" \(\xrightarrow{\texttt{R}}\) ""AR"" \(\xrightarrow{\texttt{a}}\) ""ARa"" \(\xrightarrow{\texttt{B}}\) ""Aa"" \(\xrightarrow{\texttt{b}}\) ""A"" \(\xrightarrow{\texttt{b}}\) ""A"" \(\xrightarrow{\texttt{i}}\) ""Ai"" \(\xrightarrow{\texttt{t}}\) ""Ait"" \(\xrightarrow{\texttt{B}}\) ""it"" \(\xrightarrow{\texttt{a}}\) ""ita"" \(\xrightarrow{\texttt{b}}\) ""it"" \(\xrightarrow{\texttt{y}}\) ""ity"".Given a sequence of pressed keys, output the typed string after processing all key presses.
|
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 lengths of the lines does not exceed \(10^6\).
|
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 \(2\). Thus, \(f(2)=3\). \(1\) is a divisor of \(3\) but \(2\) isn't, so \(2\) is the minimum positive integer that isn't a divisor of \(3\). Thus, \(f(3)=2\). \(1\) and \(2\) are divisors of \(4\) but \(3\) isn't, so \(3\) is the minimum positive integer that isn't a divisor of \(4\). Thus, \(f(4)=3\). Therefore, \(ans=f(1)+f(2)+f(3)+f(4)=2+3+2+3=10\).
|
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 \(\{0,1,...,n - 1\}\) and change \(a_i\) to \(a_{p_i}\)), then Do the following operation any number of times: pick an index \(i\) and change \(a_i\) to \(a_i - 1\) or \(a_i + 1\) (i.e. increment or decrement \(a_i\) by \(1\)) with a cost of \(1\). Find the minimum cost to transform \(a_0, a_1, ..., a_{n-1}\) into a power sequence.
|
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 l and r (inclusively) will fight. When two ants i and j fight, ant i gets one battle point only if si divides sj (also, ant j gets one battle point only if sj divides si). After all fights have been finished, Mole makes the ranking. An ant i, with vi battle points obtained, is going to be freed only if vi = r - l, or in other words only if it took a point in every fight it participated. After that, Mole eats the rest of the ants. Note that there can be many ants freed or even none.In order to choose the best sequence, Mole gives you t segments [li, ri] and asks for each of them how many ants is he going to eat if those ants fight.
|
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 ≤ ri ≤ n), describing one query.
|
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 are freed. Mole eats only the ant 4.In the fourth test case battle points are v = [0, 1], so ant number 5 is freed. Mole eats the ant 4.
|
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++ code: ans = solve(l, r) % a; if (ans <= 0) ans += a; This code will fail only on the test with . You are given number a, help Little X to find a proper test for hack.
|
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 lexicographically smaller than an array \(y\) if there exists an index \(i\) such that \(x_i<y_i\), and \(x_j=y_j\) for all \(1 \le j < i\). Less formally, at the first index \(i\) in which they differ, \(x_i<y_i\)
|
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 it in such a way that it will be impossible to fill all unoccupied cells with dominoes.Help Dani to count the number of ways he can select these two cells. Since Dani can only count to one million, if this number of ways is \(x\), output \(\min(x, 10^6)\).
|
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, and that it is possible to fill all unoccupied cells with dominoes.
|
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 resulting value was maximum possible.
|
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, there is a rule: Marmot wants to eat white flowers only in groups of size k.Now Marmot wonders in how many ways he can eat between a and b flowers. As the number of ways could be very large, print it modulo 1000000007 (109 + 7).
|
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 array of towers disappear. If Pizano knocks down a tower with \(0\) blocks, nothing happens.Pizano wants to knock down all \(n\) towers in any order, each exactly once. That is, for each \(i\) from \(1\) to \(n\), he will knock down the tower at position \(i\) exactly once.Moreover, the resulting array of tower heights must be non-decreasing. This means that after he knocks down all \(n\) towers, for any \(i < j\), the tower at position \(i\) must not be taller than the tower at position \(j\).You are required to output the maximum \(\text{MEX}\) of the resulting array of tower heights.The \(\text{MEX}\) of an array is the smallest non-negative integer that is not present in the array.
|
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\) integers — the initial heights of the towers \(a_1, \ldots, a_n\) (\(0 \leq a_i \leq 10^9\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
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 exactly \(a_i\) indices \(j\) (\(1 \le j \le n\)) such that \(b_i+b_j>0\), where \(i\) and \(j\) are not necessarily distinct. Given the array \(a\), Ntarsis wants you to construct some imbalanced array. Help him solve this task, or determine it is impossible.
|
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 a_i \leq n\)).It is guaranteed that the sum of \(n\) across all test cases does not exceed \(10^5\).
|
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 imbalanced array. For \(i = 1\) and \(i = 3\), there exists no index \(j\) such that \(b_i + b_j > 0\). For \(i = 2\), there is only one index \(j = 2\) such that \(b_i + b_j > 0\) (\(b_2 + b_2 = 1 + 1 = 2\)). Another possible output for the third test case could be \(b = [-2, 1, -3]\).
|
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\) — add the integer \(x\) to the end of the array \(a\). - \(k\) — remove the last \(k\) numbers from the array \(a\). ! — roll back the last active change (i.e., make the array \(a\) the way it was before the change). In this problem, only queries of the first two types (+ and -) are considered as changes. ? — find the number of distinct numbers in the array \(a\).
|
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 of the array \(a\); at the moment of the queries of the third type, there is at least one query of the first or of the second type that can be rolled back. It is also guaranteed that the number of queries of the fourth type (?) does not exceed \(10^5\).
|
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 change + 2). After the sixth query, \(a=[1,2,3]\). After the seventh query, \(a=[1]\). At the moment of the eigth query, there is only one \(1\) in the array \(a\). After the ninth query, \(a=[1,1]\). At the moment of the tenth query, there are only two \(1\) in the array \(a\). In the second example array \(a\) changes as follows: After the first query, \(a=[1]\). After the second query, \(a=[1,1\,000\,000]\). At the moment of the third query, there are \(2\) distinct intergers in the array \(a\): \(1\) and \(1\,000\,000\). After the fourth query, \(a=[1]\) (rolled back the change + 1000000). After the fifth query, \(a=[]\) (rolled back the change + 1). At the moment of the sixth query, there are no integers in the array \(a\), so the answer to this query is \(0\).
|
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. Write to the second cell number b + 1; Let's assume that at the current time the first cell contains number a, and the second cell contains number b. Write to the first cell number a·b. Maxim is wondering, how many integers x (l ≤ x ≤ r) are there, such that we can write the number x to the first cell of the calculator, having performed at most p moves.
|
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 < \ldots < b_{m - 1} < n\), is a subsequence of length \(m\) of the array \(a\).Subsequence \(b = [b_0, b_1, \ldots, b_{m-1}]\) of length \(m\) is called beautiful, if the following condition holds: For any \(p\) (\(0 \le p < m - 1\)) holds: \(a_{b_p} \oplus b_{p+1} < a_{b_{p+1}} \oplus b_p\). Here \(a \oplus b\) denotes the bitwise XOR of \(a\) and \(b\). For example, \(2 \oplus 4 = 6\) and \(3 \oplus 1=2\).Bryap is a simple person so he only wants to know the length of the longest such subsequence. Help Bryap and find the answer to his question.
|
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\) integers \(a_0,a_1,...,a_{n-1}\) (\(0 \leq a_i \leq 200\)) — the elements of the array.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(3 \cdot 10^5\).
|
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 are unblocked.A robot is travelling across this grid. You can send it commands — move up, right, down or left. If a robot attempts to move into a blocked cell or outside the grid, it explodes.However, the robot is broken — it executes each received command \(k\) times. So if you tell it to move up, for example, it will move up \(k\) times (\(k\) cells). You can't send it commands while the robot executes the current one.You are asked \(q\) queries about the robot. Each query has a start cell, a finish cell and a value \(k\). Can you 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?The robot must stop in the finish cell. If it visits the finish cell while still executing commands, it doesn't count.
|
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 contains a single integer \(q\) (\(1 \le q \le 2 \cdot 10^5\)) — the number of queries.Each of the next \(q\) lines contain five integers \(x_s, y_s, x_f, y_f\) and \(k\) (\(a[y_s] < x_s \le n\); \(1 \le y_s \le m\); \(a[y_f] < x_f \le n\); \(1 \le y_f \le m\); \(1 \le k \le 10^9\)) — the row and the column of the start cell, the row and the column of the finish cell and the number of times each your command is executed. The start and the finish cell of each query are unblocked.
|
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 sequence of integers \(a\) of length \(n\) in a math exam. The exams itself contains questions of form \(L, R\), for each of them he is asked to find any subsequence \(b\) with size greater than \(2\) (i.e. \(|b| \ge 3\)) of sequence \(a_L, a_{L+1},\ldots, a_{R}\).Recall that an sequence \(b\) is a subsequence of sequence \(a\) if \(b\) can be obtained by deletion of several (possibly zero, or all) elements.However, he hates monotone stuff, and he wants to find a subsequence free from monotone triples. Besides, he wants to find one subsequence with the largest length among all subsequences free from monotone triples for every query.Please help Bob find out subsequences meeting the above constraints.
|
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 \(q\) lines contains two integers \(L\), \(R\) (\(1 \le L,R \le n\), \(R-L\ge 2\)).
|
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 \le i_1 < i_2<\ldots<i_k\le R\)) satisfying that \(b_j = a_{i_j}\) for \(1 \le j \le k\).If there are multiple answers with the maximum length, print any of them.
|
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 planets, indexed with numbers from 1 to n. Jack is on the planet with index 1, and Apophis will land on the planet with index n. Jack can move between some pairs of planets through stargates (he can move in both directions); the transfer takes a positive, and, perhaps, for different pairs of planets unequal number of seconds. Jack begins his journey at time 0.It can be that other travellers are arriving to the planet where Jack is currently located. In this case, Jack has to wait for exactly 1 second before he can use the stargate. That is, if at time t another traveller arrives to the planet, Jack can only pass through the stargate at time t + 1, unless there are more travellers arriving at time t + 1 to the same planet.Knowing the information about travel times between the planets, and the times when Jack would not be able to use the stargate on particular planets, determine the minimum time in which he can get to the planet with index n.
|
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, bi ≤ n, ai ≠ bi), which are connected through stargates, and the integer transfer time (in seconds) ci (1 ≤ ci ≤ 104) between these planets. It is guaranteed that between any pair of planets there is at most one stargate connection.Then n lines follow: the i-th line contains an integer ki (0 ≤ ki ≤ 105) that denotes the number of moments of time when other travellers arrive to the planet with index i. Then ki distinct space-separated integers tij (0 ≤ tij < 109) follow, sorted in ascending order. An integer tij means that at time tij (in seconds) another traveller arrives to the planet i. It is guaranteed that the sum of all ki does not exceed 105.
|
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 to planet 2, and then — to planet 4, then he spends a total of only 2 + 5 = 7 seconds.In the second sample one can't get from planet 1 to planet 3 by moving through stargates.
|
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 as root(v).The depth of vertex v is the sum of weights of paths passing from the vertex v to its root. Let's denote the depth of the vertex v as depth(v).Let's consider the process of constructing a weighted directed forest. Initially, the forest does not contain vertices. Vertices are added sequentially one by one. Overall, there are n performed operations of adding. The i-th (i > 0) adding operation is described by a set of numbers (k, v1, x1, v2, x2, ... , vk, xk) and means that we should add vertex number i and k edges to the graph: an edge from vertex root(v1) to vertex i with weight depth(v1) + x1, an edge from vertex root(v2) to vertex i with weight depth(v2) + x2 and so on. If k = 0, then only vertex i is added to the graph, there are no added edges.Your task is like this: given the operations of adding vertices, calculate the sum of the weights of all edges of the forest, resulting after the application of all defined operations, modulo 1000000007 (109 + 7).
|
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), then follow 2k space-separated integers: v1, x1, v2, x2, ... , vk, xk (1 ≤ vj ≤ i - 1, |xj| ≤ 109). The operations are given in the order, in which they should be applied to the graph. It is guaranteed that sum k of all operations does not exceed 105, also that applying operations of adding vertexes does not result in loops and multiple edges.
|
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 root(1) = 1 to vertex 4 with weight depth(1) + x1 = 0 + 5 = 5 is added. v2 = 2, x2 = 2. Edge from vertex root(2) = 3 to vertex 4 with weight depth(2) + x1 = 1 + 2 = 3 is added. Vertex 5 is added. k = 1. v1 = 1, x1 = 2. Edge from vertex root(1) = 4 to vertex 5 with weight depth(1) + x1 = 5 + 2 = 7 is added. Vertex 6 is added. k = 1. v1 = 3, x1 = 4. Edge from vertex root(3) = 5 to vertex 6 with weight depth(3) + x1 = 10 + 4 = 14 is added.The resulting graph is shown on the pictore below:
|
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]\), and you choose \(i = 2\), the array becomes \([3, 1, 9, 6]\). Find the lexicographically smallest\(^{\text{∗}}\) array you can get by performing these operations.\(^{\text{∗}}\)An array \(c\) is lexicographically smaller than an array \(d\) if and only if one of the following holds: \(c\) is a prefix of \(d\), but \(c \ne d\); or in the first position where \(c\) and \(d\) differ, the array \(c\) has a smaller element than the corresponding element in \(d\).
|
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\) (\(1 \le a_i \le 10^9\)), the elements of the array.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
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 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.