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,352 |
B
|
1352B
|
B. Same Parity Summands
| 1,200 |
constructive algorithms; math
|
You are given two positive integers \(n\) (\(1 \le n \le 10^9\)) and \(k\) (\(1 \le k \le 100\)). Represent the number \(n\) as the sum of \(k\) positive integers of the same parity (have the same remainder when divided by \(2\)).In other words, find \(a_1, a_2, \ldots, a_k\) such that all \(a_i>0\), \(n = a_1 + a_2 + \ldots + a_k\) and either all \(a_i\) are even or all \(a_i\) are odd at the same time.If such a representation does not exist, then report it.
|
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\) (\(1 \le n \le 10^9\)) and \(k\) (\(1 \le k \le 100\)).
|
For each test case print: YES and the required values \(a_i\), if the answer exists (if there are several answers, print any of them); NO if the answer does not exist. The letters in the words YES and NO can be printed in any case.
|
Input: 8 10 3 100 4 8 7 97 2 8 8 3 10 5 3 1000000000 9 | Output: YES 4 2 4 YES 55 5 5 35 NO NO YES 1 1 1 1 1 1 1 1 NO YES 3 1 1 YES 111111110 111111110 111111110 111111110 111111110 111111110 111111110 111111110 111111120
|
Easy
| 2 | 463 | 253 | 231 | 13 |
|
797 |
C
|
797C
|
C. Minimal string
| 1,700 |
data structures; greedy; strings
|
Petya recieved a gift of a string s with length up to 105 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal.You should write a program that will help Petya win the game.
|
First line contains non-empty string s (1 β€ |s| β€ 105), consisting of lowercase English letters.
|
Print resulting string u.
|
Input: cab | Output: abc
|
Medium
| 3 | 462 | 96 | 25 | 7 |
|
114 |
A
|
114A
|
A. Cifera
| 1,000 |
math
|
When Petya went to school, he got interested in large numbers and what they were called in ancient times. For instance, he learned that the Russian word ""tma"" (which now means ""too much to be counted"") used to stand for a thousand and ""tma tmyschaya"" (which literally means ""the tma of tmas"") used to stand for a million.Petya wanted to modernize the words we use for numbers and invented a word petricium that represents number k. Moreover, petricium la petricium stands for number k2, petricium la petricium la petricium stands for k3 and so on. All numbers of this form are called petriciumus cifera, and the number's importance is the number of articles la in its title.Petya's invention brought on a challenge that needed to be solved quickly: does some number l belong to the set petriciumus cifera? As Petya is a very busy schoolboy he needs to automate the process, he asked you to solve it.
|
The first input line contains integer number k, the second line contains integer number l (2 β€ k, l β€ 231 - 1).
|
You should print in the first line of the output ""YES"", if the number belongs to the set petriciumus cifera and otherwise print ""NO"". If the number belongs to the set, then print on the seconds line the only number β the importance of number l.
|
Input: 525 | Output: YES1
|
Beginner
| 1 | 907 | 111 | 248 | 1 |
|
1,792 |
C
|
1792C
|
C. Min Max Sort
| 1,500 |
binary search; brute force; greedy; math; two pointers
|
You are given a permutation \(p\) of length \(n\) (a permutation of length \(n\) is an array of length \(n\) in which each integer from \(1\) to \(n\) occurs exactly once).You can perform the following operation any number of times (possibly zero): choose two different elements \(x\) and \(y\) and erase them from the permutation; insert the minimum of \(x\) and \(y\) into the permutation in such a way that it becomes the first element; insert the maximum of \(x\) and \(y\) into the permutation in such a way that it becomes the last element. For example, if \(p = [1, 5, 4, 2, 3]\) and we want to apply the operation to the elements \(3\) and \(5\), then after the first step of the operation, the permutation becomes \(p = [1, 4, 2]\); and after we insert the elements, it becomes \(p = [3, 1, 4, 2, 5]\).Your task is to calculate the minimum number of operations described above to sort the permutation \(p\) in ascending order (i. e. transform \(p\) so that \(p_1 < p_2 < \dots < p_n\)).
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of the test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of elements in the permutation.The second line of the test case contains \(n\) distinct integers from \(1\) to \(n\) β the given permutation \(p\).The sum of \(n\) over all test cases doesn't exceed \(2 \cdot 10^5\).
|
For each test case, output a single integer β the minimum number of operations described above to sort the array \(p\) in ascending order.
|
In the first example, you can proceed as follows: in the permutation \(p = [1, 5, 4, 2, 3]\), let's choose the elements \(4\) and \(2\), then, after applying the operation, the permutation becomes \(p = [2, 1, 5, 3, 4]\); in the permutation \(p = [2, 1, 5, 3, 4]\), let's choose the elements \(1\) and \(5\), then, after applying operation, the permutation becomes \(p = [1, 2, 3, 4, 5]\).
|
Input: 451 5 4 2 331 2 342 1 4 365 2 4 1 6 3 | Output: 2 0 1 3
|
Medium
| 5 | 995 | 421 | 138 | 17 |
802 |
D
|
802D
|
D. Marmots (easy)
| 2,100 |
math
|
Heidi is a statistician to the core, and she likes to study the evolution of marmot populations in each of V (1 β€ V β€ 100) villages! So it comes that every spring, when Heidi sees the first snowdrops sprout in the meadows around her barn, she impatiently dons her snowshoes and sets out to the Alps, to welcome her friends the marmots to a new season of thrilling adventures.Arriving in a village, Heidi asks each and every marmot she comes across for the number of inhabitants of that village. This year, the marmots decide to play an April Fools' joke on Heidi. Instead of consistently providing the exact number of inhabitants P (10 β€ P β€ 1000) of the village, they respond with a random non-negative integer k, drawn from one of two types of probability distributions: Poisson (d'avril) distribution: the probability of getting an answer k is for k = 0, 1, 2, 3, ..., Uniform distribution: the probability of getting an answer k is for k = 0, 1, 2, ..., 2P. Heidi collects exactly 250 answers per village. Every village follows either the Poisson or the uniform distribution. Heidi cannot tell marmots apart, so she may query some marmots several times, and each time the marmot will answer with a new number drawn from the village's distribution.Can you help Heidi to find out whether a village follows a Poisson or a uniform distribution?
|
The first line of input will contain the number of villages V (1 β€ V β€ 100). The following V lines each describe one village. The description of each village consists of 250 space-separated integers k, drawn from one of the above distributions.
|
Output one line per village, in the same order as provided in the input. The village's line shall state poisson if the village's distribution is of the Poisson type, and uniform if the answer came from a uniform distribution.
|
The full example input is visually represented below, along with the probability distribution function it was drawn from (the y-axis is labeled by its values multiplied by 250).
|
Input: 292 100 99 109 93 105 103 106 101 99 ... (input is truncated)28 180 147 53 84 80 180 85 8 16 ... (input is truncated) | Output: poissonuniform
|
Hard
| 1 | 1,344 | 244 | 225 | 8 |
2,086 |
D
|
2086D
|
D. Even String
| 1,700 |
brute force; combinatorics; dp; math; strings
|
You would like to construct a string \(s\), consisting of lowercase Latin letters, such that the following condition holds: For every pair of indices \(i\) and \(j\) such that \(s_{i} = s_{j}\), the difference of these indices is even, that is, \(|i - j| \bmod 2 = 0\). Constructing any string is too easy, so you will be given an array \(c\) of \(26\) numbers β the required number of occurrences of each individual letter in the string \(s\). So, for every \(i \in [1, 26]\), the \(i\)-th letter of the Latin alphabet should occur exactly \(c_i\) times.Your task is to count the number of distinct strings \(s\) that satisfy all these conditions. Since the answer can be huge, output it modulo \(998\,244\,353\).
|
Each test consists of several test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^{4}\))β the number of test cases. The description of test cases follows.Each test case contains \(26\) integers \(c_{i}\) (\(0 \le c_{i} \le 5 \cdot 10^{5}\))β the elements of the array \(c\).Additional constraints on the input data: The sum of \(c_{i}\) for every test case is positive; The sum of \(c_{i}\) over all test cases does not exceed \(5 \cdot 10^{5}\).
|
For each test case, print one integer β the number of suitable strings \(s\), taken modulo \(998\,244\,353\).
|
In the first test case, there are \(4\) suitable strings: ""abak"", ""akab"", ""baka"" and ""kaba"".
|
Input: 52 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 03 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 00 0 0 0 0 0 0 0 0 0 0 0 1 0 3 0 0 0 0 0 0 0 0 0 0 01 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 01 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 233527 233827 | Output: 4 960 0 1 789493841
|
Medium
| 5 | 714 | 471 | 109 | 20 |
2,065 |
D
|
2065D
|
D. Skibidus and Sigma
| 1,200 |
greedy; sortings
|
Let's denote the score of an array \(b\) with \(k\) elements as \(\sum_{i=1}^{k}\left(\sum_{j=1}^ib_j\right)\). In other words, let \(S_i\) denote the sum of the first \(i\) elements of \(b\). Then, the score can be denoted as \(S_1+S_2+\ldots+S_k\).Skibidus is given \(n\) arrays \(a_1,a_2,\ldots,a_n\), each of which contains \(m\) elements. Being the sigma that he is, he would like to concatenate them in any order to form a single array containing \(n\cdot m\) elements. Please find the maximum possible score Skibidus can achieve with his concatenated array! Formally, among all possible permutations\(^{\text{β}}\) \(p\) of length \(n\), output the maximum score of \(a_{p_1} + a_{p_2} + \dots + a_{p_n}\), where \(+\) represents concatenation\(^{\text{β }}\).\(^{\text{β}}\)A permutation of length \(n\) contains all integers from \(1\) to \(n\) exactly once.\(^{\text{β }}\)The concatenation of two arrays \(c\) and \(d\) with lengths \(e\) and \(f\) respectively (i.e. \(c + d\)) is \(c_1, c_2, \ldots, c_e, d_1, d_2, \ldots d_f\).
|
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 two integers \(n\) and \(m\) (\(1 \leq n \cdot m \leq 2 \cdot 10^5\)) β the number of arrays and the length of each array.The \(i\)'th of the next \(n\) lines contains \(m\) integers \(a_{i,1}, a_{i,2}, \ldots, a_{i,m}\) (\(1 \leq a_{i,j} \leq 10^6\)) β the elements of the \(i\)'th array.It is guaranteed that the sum of \(n \cdot m\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output the maximum score among all possible permutations \(p\) on a new line.
|
For the first test case, there are two possibilities for \(p\): \(p = [1, 2]\). Then, \(a_{p_1} + a_{p_2} = [4, 4, 6, 1]\). Its score is \(4+(4+4)+(4+4+6)+(4+4+6+1)=41\). \(p = [2, 1]\). Then, \(a_{p_1} + a_{p_2} = [6, 1, 4, 4]\). Its score is \(6+(6+1)+(6+1+4)+(6+1+4+4)=39\). The maximum possible score is \(41\).In the second test case, one optimal arrangement of the final concatenated array is \([4,1,2,1,2,2,2,2,3,2,1,2]\). We can calculate that the score is \(162\).
|
Input: 32 24 46 13 42 2 2 23 2 1 24 1 2 12 33 4 51 1 9 | Output: 41 162 72
|
Easy
| 2 | 1,039 | 524 | 97 | 20 |
2,074 |
D
|
2074D
|
D. Counting Points
| 1,400 |
brute force; data structures; geometry; implementation; two pointers
|
The pink soldiers drew \(n\) circles with their center on the \(x\)-axis of the plane. Also, they have told that the sum of radii is exactly \(m\)\(^{\text{β}}\).Please find the number of integer points inside or on the border of at least one circle. Formally, the problem is defined as follows.You are given an integer sequence \(x_1,x_2,\ldots,x_n\) and a positive integer sequence \(r_1,r_2,\ldots,r_n\), where it is known that \(\sum_{i=1}^n r_i = m\).You must count the number of integer pairs \((x,y)\) that satisfy the following condition. There exists an index \(i\) such that \((x-x_i)^2 + y^2 \le r_i^2\) (\(1 \le i \le n\)). \(^{\text{β}}\)Is this information really useful? Don't ask me; I don't really know.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n \le m \le 2\cdot 10^5\)).The second line of each test case contains \(x_1,x_2,\ldots,x_n\) β the centers of the circles (\(-10^9 \le x_i \le 10^9\)).The third line of each test case contains \(r_1,r_2,\ldots,r_n\) β the radii of the circles (\(1 \le r_i\), \(\sum_{i=1}^n r_i = m\)).It is guaranteed that the sum of \(m\) over all test cases does not exceed \(2\cdot 10^5\).
|
For each test case, output the number of integer points satisfying the condition on a separate line.
|
On the first test case, the circle with \(r_1=1\) is completely inside the circle with \(r_2=2\). Therefore, you only have to count the number of integer points inside the latter. There are \(13\) integer points such that \(x^2+y^2 \le 2^2\), so the answer is \(13\).On the second test case, the circle with \(r_1=1\) is not completely inside the circle with \(r_2=2\). There are \(3\) additional points that are inside the first circle but not inside the second circle, so the answer is \(3+13=16\).
|
Input: 42 30 01 22 30 21 23 30 2 51 1 14 80 5 10 152 2 2 2 | Output: 13 16 14 52
|
Easy
| 5 | 720 | 618 | 100 | 20 |
1,184 |
C3
|
1184C3
|
C3. Heidi and the Turing Test (Hard)
| 3,200 |
The Cybermen have again outwitted the Daleks! Unfortunately, this time the Daleks decided to abandon these tasks altogether, which means the Doctor has to deal with them.The Doctor can handle the Daleks on his own, but Heidi now has to make sure that the Cybermen are kept busy with this next task.There are \(k\) rings on a plane. For each ring, \(n\) points are uniformly sampled with a small random noise. The task is to recover the rings given only the noisy samples.The rings and the samples are generated as follows. The center of a ring is uniformly sampled from a disk of radius \(1\,000\,000\) centered at the origin, and the radius of the ring is uniformly sampled from \([250\,000, 750\,000]\). Let \(R\) be a ring with center \((x, y)\) and radius \(r\). To sample a point from \(R\), an angle \(\theta\) is uniformly sampled from \([0, 2\pi]\) and a distance \(d\) is uniformly sampled from \([0.9r, 1.1r]\). The coordinates of the sampled point are then \((x+d\cos(\theta), y+d\sin(\theta))\) rounded to the closest integers.The distance between rings is measured by their Hausdorff distance. In our case, the distance between two rings \(R_1, R_2\) can be written as follow. Let \(d\) be the distance between the two centers and \(r_1, r_2\) be the radii. Then the distance is $$$\(dist(R_1, R_2)=\max(\min(d_{--}, d_{-+}),\min(d_{+-}, d_{++}), \min(d_{--}, d_{+-}), \min(d_{-+}, d_{++}))\)\(, where \)d_{++}=|d+r_1+r_2|\(, \)d_{+-}=|d+r_1-r_2|\(, \)d_{-+}=|d-r_1+r_2|\(, \)d_{--}=|d-r_1-r_2|\(.We say that a ring \)R_0\( is recovered if one of the rings \)R\( in the output has Hausdorff distance less than \)100\,000\( from \)R_0\(. An output is accepted if all the rings are recovered. It is guaranteed that the distances between any two rings is greater than \)600\,000$$$.Remember that a human can very easily solve this task, so make sure that no human traitors are helping the Cybermen complete this task.
|
The first line contains an integer \(k\) (\(1 \leq k \leq 4\)), the number of rings.The second line contains an integer \(n\) (\(100 \leq n \leq 1\,000\)), the number of samples per ring.The following \(n \times k\) lines contain the samples, one sample per line.Each line contains a pair of integers \(x_i, y_i\), where \((x_i, y_i)\) are the coordinates of the \(i\)-th sample.
|
Print \(k\) lines, each describing a single ring.For each line, print three real numbers \(x_i, y_i, r_i\), where \((x_i, y_i)\) and \(r_i\) are the coordinates and the radius of the \(i\)-th ring.The order of the rings does not matter.
|
Here is how one of tests with \(k=4\) and \(n=100\) looks like. You can download the sample input and output here.
|
Master
| 0 | 1,927 | 379 | 236 | 11 |
||
1,542 |
A
|
1542A
|
A. Odd Set
| 800 |
math
|
You are given a multiset (i. e. a set that can contain multiple equal integers) containing \(2n\) integers. Determine if you can split it into exactly \(n\) pairs (i. e. each element should be in exactly one pair) so that the sum of the two elements in each pair is odd (i. e. when divided by \(2\), the remainder is \(1\)).
|
The input consists of multiple test cases. The first line contains an integer \(t\) (\(1\leq t\leq 100\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains an integer \(n\) (\(1\leq n\leq 100\)).The second line of each test case contains \(2n\) integers \(a_1,a_2,\dots, a_{2n}\) (\(0\leq a_i\leq 100\)) β the numbers in the set.
|
For each test case, print ""Yes"" if it can be split into exactly \(n\) pairs so that the sum of the two elements in each pair is odd, and ""No"" otherwise. You can print each letter in any case.
|
In the first test case, a possible way of splitting the set is \((2,3)\), \((4,5)\).In the second, third and fifth test case, we can prove that there isn't any possible way.In the fourth test case, a possible way of splitting the set is \((2,3)\).
|
Input: 5 2 2 3 4 5 3 2 3 4 5 5 5 1 2 4 1 2 3 4 1 5 3 2 6 7 3 4 | Output: Yes No No Yes No
|
Beginner
| 1 | 324 | 391 | 195 | 15 |
1,593 |
F
|
1593F
|
F. Red-Black Number
| 2,100 |
dfs and similar; dp; implementation; math; meet-in-the-middle
|
It is given a non-negative integer \(x\), the decimal representation of which contains \(n\) digits. You need to color each its digit in red or black, so that the number formed by the red digits is divisible by \(A\), and the number formed by the black digits is divisible by \(B\).At least one digit must be colored in each of two colors. Consider, the count of digits colored in red is \(r\) and the count of digits colored in black is \(b\). Among all possible colorings of the given number \(x\), you need to output any such that the value of \(|r - b|\) is the minimum possible.Note that the number \(x\) and the numbers formed by digits of each color, may contain leading zeros. Example of painting a number for \(A = 3\) and \(B = 13\) The figure above shows an example of painting the number \(x = 02165\) of \(n = 5\) digits for \(A = 3\) and \(B = 13\). The red digits form the number \(015\), which is divisible by \(3\), and the black ones β \(26\), which is divisible by \(13\). Note that the absolute value of the difference between the counts of red and black digits is \(1\), it is impossible to achieve a smaller value.
|
The first line contains one integer \(t\) (\(1 \le t \le 10\)) β the number of test cases. Then \(t\) test cases follow.Each test case consists of two lines. The first line contains three integers \(n\), \(A\), \(B\) (\(2 \le n \le 40\), \(1 \le A, B \le 40\)). The second line contains a non-negative integer \(x\) containing exactly \(n\) digits and probably containing leading zeroes.
|
For each test case, output in a separate line: -1 if the desired coloring does not exist; a string \(s\) of \(n\) characters, each of them is a letter 'R' or 'B'. If the \(i\)-th digit of the number \(x\) is colored in red, then the \(i\)-th character of the string \(s\) must be the letter 'R', otherwise the letter 'B'. The number formed by digits colored red should divisible by \(A\). The number formed by digits colored black should divisible by \(B\). The value \(|r-b|\) should be minimal, where \(r\) is the count of red digits, \(b\) is the count of black digits. If there are many possible answers, print any of them.
|
The first test case is considered in the statement.In the second test case, there are no even digits, so it is impossible to form a number from its digits that is divisible by \(2\).In the third test case, each coloring containing at least one red and one black digit is possible, so you can color \(4\) digits in red and \(4\) in black (\(|4 - 4| = 0\), it is impossible to improve the result).In the fourth test case, there is a single desired coloring.
|
Input: 4 5 3 13 02165 4 2 1 1357 8 1 1 12345678 2 7 9 90 | Output: RBRBR -1 BBRRRRBB BR
|
Hard
| 5 | 1,136 | 387 | 627 | 15 |
1,146 |
A
|
1146A
|
A. Love ""A""
| 800 |
implementation; strings
|
Alice has a string \(s\). She really likes the letter ""a"". She calls a string good if strictly more than half of the characters in that string are ""a""s. For example ""aaabb"", ""axaa"" are good strings, and ""baca"", ""awwwa"", """" (empty string) are not.Alice can erase some characters from her string \(s\). She would like to know what is the longest string remaining after erasing some characters (possibly zero) to get a good string. It is guaranteed that the string has at least one ""a"" in it, so the answer always exists.
|
The first line contains a string \(s\) (\(1 \leq |s| \leq 50\)) consisting of lowercase English letters. It is guaranteed that there is at least one ""a"" in \(s\).
|
Print a single integer, the length of the longest good string that Alice can get after erasing some characters from \(s\).
|
In the first example, it's enough to erase any four of the ""x""s. The answer is \(3\) since that is the maximum number of characters that can remain.In the second example, we don't need to erase any characters.
|
Input: xaxxxxa | Output: 3
|
Beginner
| 2 | 534 | 164 | 122 | 11 |
1,552 |
I
|
1552I
|
I. Organizing a Music Festival
| 3,400 |
dfs and similar; math
|
You are the organizer of the famous ""Zurich Music Festival"". There will be \(n\) singers who will perform at the festival, identified by the integers \(1\), \(2\), \(\dots\), \(n\). You must choose in which order they are going to perform on stage. You have \(m\) friends and each of them has a set of favourite singers. More precisely, for each \(1\le i\le m\), the \(i\)-th friend likes singers \(s_{i,1}, \, s_{i, 2}, \, \dots, \,s_{i, q_i}\).A friend of yours is happy if the singers he likes perform consecutively (in an arbitrary order). An ordering of the singers is valid if it makes all your friends happy.Compute the number of valid orderings modulo \(998\,244\,353\).
|
The first line contains two integers \(n\) and \(m\) (\(1\le n,\,m\le 100\)) β the number of singers and the number of friends correspondingly.The \(i\)-th of the next \(m\) lines contains the integer \(q_i\) (\(1\le q_i\le n\)) β the number of favorite singers of the \(i\)-th friend β followed by the \(q_i\) integers \(s_{i,1}, \, s_{i, 2}, \, \dots, \,s_{i, q_i}\) (\(1\le s_{i,1}<s_{i,2}<\cdots<s_{i,q_i}\le n\)) β the indexes of his favorite singers.
|
Print the number of valid orderings of the singers modulo \(998\,244\,353\).
|
Explanation of the first sample: There are \(3\) singers and only \(1\) friend. The friend likes the two singers \(1\) and \(3\). Thus, the \(4\) valid orderings are: 1 3 2 2 1 3 2 3 1 3 1 2 Explanation of the second sample: There are \(5\) singers and \(5\) friends. One can show that no ordering is valid.Explanation of the third sample: There are \(100\) singers and only \(1\) friend. The friend likes only singer \(50\), hence all the \(100!\) possible orderings are valid.Explanation of the fourth sample: There are \(5\) singers and only \(1\) friend. The friend likes all the singers, hence all the \(5!=120\) possible orderings are valid.
|
Input: 3 1 2 1 3 | Output: 4
|
Master
| 2 | 680 | 456 | 76 | 15 |
750 |
D
|
750D
|
D. New Year and Fireworks
| 1,900 |
brute force; data structures; dfs and similar; dp; implementation
|
One tradition of welcoming the New Year is launching fireworks into the sky. Usually a launched firework flies vertically upward for some period of time, then explodes, splitting into several parts flying in different directions. Sometimes those parts also explode after some period of time, splitting into even more parts, and so on.Limak, who lives in an infinite grid, has a single firework. The behaviour of the firework is described with a recursion depth n and a duration for each level of recursion t1, t2, ..., tn. Once Limak launches the firework in some cell, the firework starts moving upward. After covering t1 cells (including the starting cell), it explodes and splits into two parts, each moving in the direction changed by 45 degrees (see the pictures below for clarification). So, one part moves in the top-left direction, while the other one moves in the top-right direction. Each part explodes again after covering t2 cells, splitting into two parts moving in directions again changed by 45 degrees. The process continues till the n-th level of recursion, when all 2n - 1 existing parts explode and disappear without creating new parts. After a few levels of recursion, it's possible that some parts will be at the same place and at the same time β it is allowed and such parts do not crash.Before launching the firework, Limak must make sure that nobody stands in cells which will be visited at least once by the firework. Can you count the number of those cells?
|
The first line of the input contains a single integer n (1 β€ n β€ 30) β the total depth of the recursion.The second line contains n integers t1, t2, ..., tn (1 β€ ti β€ 5). On the i-th level each of 2i - 1 parts will cover ti cells before exploding.
|
Print one integer, denoting the number of cells which will be visited at least once by any part of the firework.
|
For the first sample, the drawings below show the situation after each level of recursion. Limak launched the firework from the bottom-most red cell. It covered t1 = 4 cells (marked red), exploded and divided into two parts (their further movement is marked green). All explosions are marked with an 'X' character. On the last drawing, there are 4 red, 4 green, 8 orange and 23 pink cells. So, the total number of visited cells is 4 + 4 + 8 + 23 = 39. For the second sample, the drawings below show the situation after levels 4, 5 and 6. The middle drawing shows directions of all parts that will move in the next level.
|
Input: 44 2 2 3 | Output: 39
|
Hard
| 5 | 1,483 | 246 | 112 | 7 |
230 |
A
|
230A
|
A. Dragons
| 1,000 |
greedy; sortings
|
Kirito is stuck on a level of the MMORPG he is playing now. To move on in the game, he's got to defeat all n dragons that live on this level. Kirito and the dragons have strength, which is represented by an integer. In the duel between two opponents the duel's outcome is determined by their strength. Initially, Kirito's strength equals s.If Kirito starts duelling with the i-th (1 β€ i β€ n) dragon and Kirito's strength is not greater than the dragon's strength xi, then Kirito loses the duel and dies. But if Kirito's strength is greater than the dragon's strength, then he defeats the dragon and gets a bonus strength increase by yi.Kirito can fight the dragons in any order. Determine whether he can move on to the next level of the game, that is, defeat all dragons without a single loss.
|
The first line contains two space-separated integers s and n (1 β€ s β€ 104, 1 β€ n β€ 103). Then n lines follow: the i-th line contains space-separated integers xi and yi (1 β€ xi β€ 104, 0 β€ yi β€ 104) β the i-th dragon's strength and the bonus for defeating it.
|
On a single line print ""YES"" (without the quotes), if Kirito can move on to the next level and print ""NO"" (without the quotes), if he can't.
|
In the first sample Kirito's strength initially equals 2. As the first dragon's strength is less than 2, Kirito can fight it and defeat it. After that he gets the bonus and his strength increases to 2 + 99 = 101. Now he can defeat the second dragon and move on to the next level.In the second sample Kirito's strength is too small to defeat the only dragon and win.
|
Input: 2 21 99100 0 | Output: YES
|
Beginner
| 2 | 793 | 257 | 144 | 2 |
733 |
C
|
733C
|
C. Epidemic in Monstropolis
| 1,800 |
constructive algorithms; dp; greedy; two pointers
|
There was an epidemic in Monstropolis and all monsters became sick. To recover, all monsters lined up in queue for an appointment to the only doctor in the city.Soon, monsters became hungry and began to eat each other. One monster can eat other monster if its weight is strictly greater than the weight of the monster being eaten, and they stand in the queue next to each other. Monsters eat each other instantly. There are no monsters which are being eaten at the same moment. After the monster A eats the monster B, the weight of the monster A increases by the weight of the eaten monster B. In result of such eating the length of the queue decreases by one, all monsters after the eaten one step forward so that there is no empty places in the queue again. A monster can eat several monsters one after another. Initially there were n monsters in the queue, the i-th of which had weight ai.For example, if weights are [1, 2, 2, 2, 1, 2] (in order of queue, monsters are numbered from 1 to 6 from left to right) then some of the options are: the first monster can't eat the second monster because a1 = 1 is not greater than a2 = 2; the second monster can't eat the third monster because a2 = 2 is not greater than a3 = 2; the second monster can't eat the fifth monster because they are not neighbors; the second monster can eat the first monster, the queue will be transformed to [3, 2, 2, 1, 2]. After some time, someone said a good joke and all monsters recovered. At that moment there were k (k β€ n) monsters in the queue, the j-th of which had weight bj. Both sequences (a and b) contain the weights of the monsters in the order from the first to the last.You are required to provide one of the possible orders of eating monsters which led to the current queue, or to determine that this could not happen. Assume that the doctor didn't make any appointments while monsters were eating each other.
|
The first line contains single integer n (1 β€ n β€ 500) β the number of monsters in the initial queue.The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 106) β the initial weights of the monsters.The third line contains single integer k (1 β€ k β€ n) β the number of monsters in the queue after the joke. The fourth line contains k integers b1, b2, ..., bk (1 β€ bj β€ 5Β·108) β the weights of the monsters after the joke. Monsters are listed in the order from the beginning of the queue to the end.
|
In case if no actions could lead to the final queue, print ""NO"" (without quotes) in the only line. Otherwise print ""YES"" (without quotes) in the first line. In the next n - k lines print actions in the chronological order. In each line print x β the index number of the monster in the current queue which eats and, separated by space, the symbol 'L' if the monster which stays the x-th in the queue eats the monster in front of him, or 'R' if the monster which stays the x-th in the queue eats the monster behind him. After each eating the queue is enumerated again. When one monster eats another the queue decreases. If there are several answers, print any of them.
|
In the first example, initially there were n = 6 monsters, their weights are [1, 2, 2, 2, 1, 2] (in order of queue from the first monster to the last monster). The final queue should be [5, 5]. The following sequence of eatings leads to the final queue: the second monster eats the monster to the left (i.e. the first monster), queue becomes [3, 2, 2, 1, 2]; the first monster (note, it was the second on the previous step) eats the monster to the right (i.e. the second monster), queue becomes [5, 2, 1, 2]; the fourth monster eats the mosnter to the left (i.e. the third monster), queue becomes [5, 2, 3]; the finally, the third monster eats the monster to the left (i.e. the second monster), queue becomes [5, 5]. Note that for each step the output contains numbers of the monsters in their current order in the queue.
|
Input: 61 2 2 2 1 225 5 | Output: YES2 L1 R4 L3 L
|
Medium
| 4 | 1,901 | 504 | 670 | 7 |
576 |
A
|
576A
|
A. Vasya and Petya's Game
| 1,500 |
math; number theory
|
Vasya and Petya are playing a simple game. Vasya thought of number x between 1 and n, and Petya tries to guess the number.Petya can ask questions like: ""Is the unknown number divisible by number y?"".The game is played by the following rules: first Petya asks all the questions that interest him (also, he can ask no questions), and then Vasya responds to each question with a 'yes' or a 'no'. After receiving all the answers Petya should determine the number that Vasya thought of.Unfortunately, Petya is not familiar with the number theory. Help him find the minimum number of questions he should ask to make a guaranteed guess of Vasya's number, and the numbers yi, he should ask the questions about.
|
A single line contains number n (1 β€ n β€ 103).
|
Print the length of the sequence of questions k (0 β€ k β€ n), followed by k numbers β the questions yi (1 β€ yi β€ n).If there are several correct sequences of questions of the minimum length, you are allowed to print any of them.
|
The sequence from the answer to the first sample test is actually correct.If the unknown number is not divisible by one of the sequence numbers, it is equal to 1.If the unknown number is divisible by 4, it is 4.If the unknown number is divisible by 3, then the unknown number is 3.Otherwise, it is equal to 2. Therefore, the sequence of questions allows you to guess the unknown number. It can be shown that there is no correct sequence of questions of length 2 or shorter.
|
Input: 4 | Output: 32 4 3
|
Medium
| 2 | 704 | 46 | 227 | 5 |
1,600 |
F
|
1600F
|
F. Party Organization
| 2,300 |
brute force; math; probabilities
|
On the great island of Baltia, there live \(N\) people, numbered from \(1\) to \(N\). There are exactly \(M\) pairs of people that are friends with each other. The people of Baltia want to organize a successful party, but they have very strict rules on what a party is and when the party is successful. On the island of Baltia, a party is a gathering of exactly \(5\) people. The party is considered to be successful if either all the people at the party are friends with each other (so that they can all talk to each other without having to worry about talking to someone they are not friends with) or no two people at the party are friends with each other (so that everyone can just be on their phones without anyone else bothering them). Please help the people of Baltia organize a successful party or tell them that it's impossible to do so.
|
The first line contains two integer numbers, \(N\) (\(5 \leq N \leq 2*10^5\)) and \(M\) (\(0 \leq M \leq 2*10^5\)) β the number of people that live in Baltia, and the number of friendships. The next \(M\) lines each contains two integers \(U_i\) and \(V_i\) (\(1 \leq U_i,V_i \leq N\)) β meaning that person \(U_i\) is friends with person \(V_i\). Two friends can not be in the list of friends twice (no pairs are repeated) and a person can be friends with themselves (\(U_i \ne V_i\)).
|
If it's possible to organize a successful party, print \(5\) numbers indicating which \(5\) people should be invited to the party. If it's not possible to organize a successful party, print \(-1\) instead. If there are multiple successful parties possible, print any.
|
Input: 6 3 1 4 4 2 5 4 | Output: 1 2 3 5 6
|
Expert
| 3 | 845 | 486 | 267 | 16 |
|
1,374 |
C
|
1374C
|
C. Move Brackets
| 1,000 |
greedy; strings
|
You are given a bracket sequence \(s\) of length \(n\), where \(n\) is even (divisible by two). The string \(s\) consists of \(\frac{n}{2}\) opening brackets '(' and \(\frac{n}{2}\) closing brackets ')'.In one move, you can choose exactly one bracket and move it to the beginning of the string or to the end of the string (i.e. you choose some index \(i\), remove the \(i\)-th character of \(s\) and insert it before or after all remaining characters of \(s\)).Your task is to find the minimum number of moves required to obtain regular bracket sequence from \(s\). It can be proved that the answer always exists under the given constraints.Recall what the regular bracket sequence is: ""()"" is regular bracket sequence; if \(s\) is regular bracket sequence then ""("" + \(s\) + "")"" is regular bracket sequence; if \(s\) and \(t\) are regular bracket sequences then \(s\) + \(t\) is regular bracket sequence. For example, ""()()"", ""(())()"", ""(())"" and ""()"" are regular bracket sequences, but "")("", ""()("" and "")))"" are not.You have to answer \(t\) independent test cases.
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 2000\)) β the number of test cases. Then \(t\) test cases follow.The first line of the test case contains one integer \(n\) (\(2 \le n \le 50\)) β the length of \(s\). It is guaranteed that \(n\) is even. The second line of the test case containg the string \(s\) consisting of \(\frac{n}{2}\) opening and \(\frac{n}{2}\) closing brackets.
|
For each test case, print the answer β the minimum number of moves required to obtain regular bracket sequence from \(s\). It can be proved that the answer always exists under the given constraints.
|
In the first test case of the example, it is sufficient to move the first bracket to the end of the string.In the third test case of the example, it is sufficient to move the last bracket to the beginning of the string.In the fourth test case of the example, we can choose last three openning brackets, move them to the beginning of the string and obtain ""((()))(())"".
|
Input: 4 2 )( 4 ()() 8 ())()()( 10 )))((((()) | Output: 1 0 1 3
|
Beginner
| 2 | 1,086 | 409 | 198 | 13 |
1,608 |
B
|
1608B
|
B. Build the Permutation
| 1,200 |
constructive algorithms; greedy
|
You are given three integers \(n, a, b\). Determine if there exists a permutation \(p_1, p_2, \ldots, p_n\) of integers from \(1\) to \(n\), such that:There are exactly \(a\) integers \(i\) with \(2 \le i \le n-1\) such that \(p_{i-1} < p_i > p_{i+1}\) (in other words, there are exactly \(a\) local maximums).There are exactly \(b\) integers \(i\) with \(2 \le i \le n-1\) such that \(p_{i-1} > p_i < p_{i+1}\) (in other words, there are exactly \(b\) local minimums).If such permutations exist, find any such permutation.
|
The first line of the input contains a single integer \(t\) (\(1 \le t \le 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\) (\(2 \leq n \leq 10^5\), \(0 \leq a,b \leq n\)).The sum of \(n\) over all test cases doesn't exceed \(10^5\).
|
For each test case, if there is no permutation with the requested properties, output \(-1\).Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them.
|
In the first test case, one example of such permutations is \([1, 3, 2, 4]\). In it \(p_1 < p_2 > p_3\), and \(2\) is the only such index, and \(p_2> p_3 < p_4\), and \(3\) the only such index.One can show that there is no such permutation for the third test case.
|
Input: 3 4 1 1 6 1 2 6 4 0 | Output: 1 3 2 4 4 2 3 1 5 6 -1
|
Easy
| 2 | 523 | 337 | 211 | 16 |
1,105 |
D
|
1105D
|
D. Kilani and the Game
| 1,900 |
dfs and similar; graphs; implementation; shortest paths
|
Kilani is playing a game with his friends. This game can be represented as a grid of size \(n \times m\), where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player \(i\) can expand from a cell with his castle to the empty cell if it's possible to reach it in at most \(s_i\) (where \(s_i\) is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
|
The first line contains three integers \(n\), \(m\) and \(p\) (\(1 \le n, m \le 1000\), \(1 \le p \le 9\)) β the size of the grid and the number of players.The second line contains \(p\) integers \(s_i\) (\(1 \le s \le 10^9\)) β the speed of the expansion for every player.The following \(n\) lines describe the game grid. Each of them consists of \(m\) symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit \(x\) (\(1 \le x \le p\)) denotes the castle owned by player \(x\).It is guaranteed, that each player has at least one castle on the grid.
|
Print \(p\) integers β the number of cells controlled by each player after the game ends.
|
The picture below show the game before it started, the game after the first round and game after the second round in the first example: In the second example, the first player is ""blocked"" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
|
Input: 3 3 2 1 1 1.. ... ..2 | Output: 6 3
|
Hard
| 4 | 1,181 | 571 | 89 | 11 |
599 |
B
|
599B
|
B. Spongebob and Joke
| 1,500 |
implementation
|
While Patrick was gone shopping, Spongebob decided to play a little trick on his friend. The naughty Sponge browsed through Patrick's personal stuff and found a sequence a1, a2, ..., am of length m, consisting of integers from 1 to n, not necessarily distinct. Then he picked some sequence f1, f2, ..., fn of length n and for each number ai got number bi = fai. To finish the prank he erased the initial sequence ai.It's hard to express how sad Patrick was when he returned home from shopping! We will just say that Spongebob immediately got really sorry about what he has done and he is now trying to restore the original sequence. Help him do this or determine that this is impossible.
|
The first line of the input contains two integers n and m (1 β€ n, m β€ 100 000) β the lengths of sequences fi and bi respectively.The second line contains n integers, determining sequence f1, f2, ..., fn (1 β€ fi β€ n).The last line contains m integers, determining sequence b1, b2, ..., bm (1 β€ bi β€ n).
|
Print ""Possible"" if there is exactly one sequence ai, such that bi = fai for all i from 1 to m. Then print m integers a1, a2, ..., am.If there are multiple suitable sequences ai, print ""Ambiguity"".If Spongebob has made a mistake in his calculations and no suitable sequence ai exists, print ""Impossible"".
|
In the first sample 3 is replaced by 1 and vice versa, while 2 never changes. The answer exists and is unique.In the second sample all numbers are replaced by 1, so it is impossible to unambiguously restore the original sequence.In the third sample fi β 3 for all i, so no sequence ai transforms into such bi and we can say for sure that Spongebob has made a mistake.
|
Input: 3 33 2 11 2 3 | Output: Possible3 2 1
|
Medium
| 1 | 687 | 301 | 310 | 5 |
1,989 |
B
|
1989B
|
B. Substring and Subsequence
| 1,200 |
brute force; greedy; strings
|
You are given two strings \(a\) and \(b\), both consisting of lowercase Latin letters. A subsequence of a string is a string which can be obtained by removing several (possibly zero) characters from the original string. A substring of a string is a contiguous subsequence of that string.For example, consider the string abac: a, b, c, ab, aa, ac, ba, bc, aba, abc, aac, bac and abac are its subsequences; a, b, c, ab, ba, ac, aba, bac and abac are its substrings. Your task is to calculate the minimum possible length of the string that contains \(a\) as a substring and \(b\) as a subsequence.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^3\)) β the number of test cases.The first line of each test case contains a string \(a\) (\(1 \le |a| \le 100\)), consisting of lowercase Latin letters.The second line of each test case contains a string \(b\) (\(1 \le |b| \le 100\)), consisting of lowercase Latin letters.
|
For each test case, print a single integer β the minimum possible length of the string that contains \(a\) as a substring and \(b\) as a subsequence.
|
In the examples below, the characters that correspond to the subsequence equal to \(b\) are bolded.In the first example, one of the possible answers is caba.In the second example, one of the possible answers is ercf.In the third example, one of the possible answers is mmm.In the fourth example, one of the possible answers is contest.In the fifth example, one of the possible answers is abcdefg.
|
Input: 5abacbercfmmmmmmcontesttestcdeabcefg | Output: 4 4 3 7 7
|
Easy
| 3 | 594 | 338 | 149 | 19 |
1,392 |
D
|
1392D
|
D. Omkar and Bed Wars
| 1,700 |
dp; greedy
|
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are \(n\) players arranged in a circle, so that for all \(j\) such that \(2 \leq j \leq n\), player \(j - 1\) is to the left of the player \(j\), and player \(j\) is to the right of player \(j - 1\). Additionally, player \(n\) is to the left of player \(1\), and player \(1\) is to the right of player \(n\).Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either \(0\), \(1\), or \(2\) other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly \(1\) other player, then they should logically attack that player in response. If instead a player is being attacked by \(0\) or \(2\) other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players.Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the \(n\) players in the game to make them instead attack another player β i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The descriptions of the test cases follows.The first line of each test case contains one integer \(n\) (\(3 \leq n \leq 2 \cdot 10^5\)) β the amount of players (and therefore beds) in this game of Bed Wars.The second line of each test case contains a string \(s\) of length \(n\). The \(j\)-th character of \(s\) is equal to L if the \(j\)-th player is attacking the player to their left, and R if the \(j\)-th player is attacking the player to their right.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy.It can be proven that it is always possible for Omkar to achieve this under the given constraints.
|
In the first test case, players \(1\) and \(2\) are attacking each other, and players \(3\) and \(4\) are attacking each other. Each player is being attacked by exactly \(1\) other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer \(0\).In the second test case, not every player acts logically: for example, player \(3\) is attacked only by player \(2\), but doesn't attack him in response. Omkar can talk to player \(3\) to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer \(1\).
|
Input: 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR | Output: 0 1 1 3 2
|
Medium
| 2 | 1,732 | 668 | 274 | 13 |
1,646 |
C
|
1646C
|
C. Factorials and Powers of Two
| 1,500 |
bitmasks; brute force; constructive algorithms; dp; math
|
A number is called powerful if it is a power of two or a factorial. In other words, the number \(m\) is powerful if there exists a non-negative integer \(d\) such that \(m=2^d\) or \(m=d!\), where \(d!=1\cdot 2\cdot \ldots \cdot d\) (in particular, \(0! = 1\)). For example \(1\), \(4\), and \(6\) are powerful numbers, because \(1=1!\), \(4=2^2\), and \(6=3!\) but \(7\), \(10\), or \(18\) are not.You are given a positive integer \(n\). Find the minimum number \(k\) such that \(n\) can be represented as the sum of \(k\) distinct powerful numbers, or say that there is no such \(k\).
|
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.A test case consists of only one line, containing one integer \(n\) (\(1\le n\le 10^{12}\)).
|
For each test case print the answer on a separate line.If \(n\) can not be represented as the sum of distinct powerful numbers, print \(-1\).Otherwise, print a single positive integer β the minimum possible value of \(k\).
|
In the first test case, \(7\) can be represented as \(7=1+6\), where \(1\) and \(6\) are powerful numbers. Because \(7\) is not a powerful number, we know that the minimum possible value of \(k\) in this case is \(k=2\).In the second test case, a possible way to represent \(11\) as the sum of three powerful numbers is \(11=1+4+6\). We can show that there is no way to represent \(11\) as the sum of two or less powerful numbers. In the third test case, \(240\) can be represented as \(240=24+32+64+120\). Observe that \(240=120+120\) is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, \(17179869184=2^{34}\), so \(17179869184\) is a powerful number and the minimum \(k\) in this case is \(k=1\).
|
Input: 471124017179869184 | Output: 2 3 4 1
|
Medium
| 5 | 586 | 248 | 222 | 16 |
641 |
G
|
641G
|
G. Little Artem and Graph
| 2,300 |
Little Artem is given a graph, constructed as follows: start with some k-clique, then add new vertices one by one, connecting them to k already existing vertices that form a k-clique.Artem wants to count the number of spanning trees in this graph modulo 109 + 7.
|
First line of the input contains two integers n and k (1 β€ n β€ 10 000, 1 β€ k β€ min(n, 5)) β the total size of the graph and the size of the initial clique, respectively.Next n - k lines describe k + 1-th, k + 2-th, ..., i-th, ..., n-th vertices by listing k distinct vertex indices 1 β€ aij < i it is connected to. It is guaranteed that those vertices form a k-clique.
|
Output a single integer β the number of spanning trees in the given graph modulo 109 + 7.
|
Input: 3 21 2 | Output: 3
|
Expert
| 0 | 262 | 367 | 89 | 6 |
||
1,411 |
B
|
1411B
|
B. Fair Numbers
| 1,000 |
brute force; number theory
|
We call a positive integer number fair if it is divisible by each of its nonzero digits. For example, \(102\) is fair (because it is divisible by \(1\) and \(2\)), but \(282\) is not, because it isn't divisible by \(8\). Given a positive integer \(n\). Find the minimum integer \(x\), such that \(n \leq x\) and \(x\) is fair.
|
The first line contains number of test cases \(t\) (\(1 \leq t \leq 10^3\)). Each of the next \(t\) lines contains an integer \(n\) (\(1 \leq n \leq 10^{18}\)).
|
For each of \(t\) test cases print a single integer β the least fair number, which is not less than \(n\).
|
Explanations for some test cases: In the first test case number \(1\) is fair itself. In the second test case number \(288\) is fair (it's divisible by both \(2\) and \(8\)). None of the numbers from \([282, 287]\) is fair, because, for example, none of them is divisible by \(8\).
|
Input: 4 1 282 1234567890 1000000000000000000 | Output: 1 288 1234568040 1000000000000000000
|
Beginner
| 2 | 326 | 160 | 106 | 14 |
1,863 |
F
|
1863F
|
F. Divide, XOR, and Conquer
| 2,600 |
bitmasks; dp; math
|
You are given an array of \(n\) integers \(a_1, a_2, \ldots, a_n\).In one operation you split the array into two parts: a non-empty prefix and a non-empty suffix. The value of each part is the bitwise XOR of all elements in it. Next, discard the part with the smaller value. If both parts have equal values, you can choose which one to discard. Replace the array with the remaining part.The operations are being performed until the length of the array becomes \(1\). For each \(i\) (\(1 \le i \le n\)), determine whether it is possible to achieve the state when only the \(i\)-th element (with respect to the original numbering) remains.Formally, you have two numbers \(l\) and \(r\), initially \(l = 1\) and \(r = n\). The current state of the array is \([a_l, a_{l+1}, \ldots, a_r]\).As long as \(l < r\), you apply the following operation: Choose an arbitrary \(k\) from the set \(\{l, l + 1, \ldots, r - 1\}\). Denote \(x = a_l \oplus a_{l + 1} \oplus \ldots \oplus a_k\) and \(y = a_{k + 1} \oplus a_{k + 2} \oplus \ldots \oplus a_{r}\), where \(\oplus\) denotes the bitwise XOR operation. If \(x < y\), set \(l = k + 1\). If \(x > y\), set \(r = k\). If \(x = y\), either set \(l = k + 1\), or set \(r = k\). For each \(i\) (\(1 \le i \le n\)), determine whether it is possible to achieve \(l = r = i\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10\,000\)). The description of the test cases follows.The first line of each test case contains one integer \(n\) (\(1 \le n \le 10\,000\)).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i < 2^{60}\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10\,000\).
|
For each test case, output a single string of length \(n\) where the \(i\)-th element is equal to 1 if it is possible to achieve \(l = r = i\) and is equal to 0 otherwise.
|
In the first test case, it is possible to achieve \(l = r = i\) for any \(i\) from \(1\) to \(n\): for \(i=1\): \([1; 6] \rightarrow [1; 4] \rightarrow [1; 1]\); for \(i=2\): \([1; 6] \rightarrow [1; 3] \rightarrow [2; 3] \rightarrow [2; 2]\); for \(i=3\): \([1; 6] \rightarrow [1; 3] \rightarrow [3; 3]\); for \(i=4\): \([1; 6] \rightarrow [1; 4] \rightarrow [4; 4]\); for \(i=5\): \([1; 6] \rightarrow [5; 6] \rightarrow [5; 5]\); for \(i=6\): \([1; 6] \rightarrow [6; 6]\). Let's take a closer look at \(i = 2\). Initially \(l = 1\), \(r = 6\). We can choose \(k = 3\) and set \(r = k = 3\) since \((3 \oplus 2 \oplus 1) = 0 \ge 0 = (3 \oplus 7 \oplus 4)\); Next, we can choose \(k = 1\) and set \(l = k + 1 = 2\) since \(3 \le 3 = (2 \oplus 1)\); Finally, we can choose \(k = 2\) and set \(r = k = 2\) since \(2 \ge 1\).
|
Input: 663 2 1 3 7 451 1 1 1 1101 2 4 8 4 1 2 3 4 550 0 0 0 051 2 3 0 11100500 | Output: 111111 10101 0001000000 11111 11001 1
|
Expert
| 3 | 1,309 | 446 | 171 | 18 |
2,112 |
A
|
2112A
|
A. Race
| 800 |
implementation; math
|
Alice and Bob participate in a game TV show. When the game starts, the prize will be dropped to a certain point, and whoever gets to it first will get the prize.Alice decided that she would start running from point \(a\). Bob, however, has not yet chosen his starting position.Bob knows that the prize could drop either at point \(x\) or at point \(y\). He also knows that he can reach the prize faster than Alice if the distance from his starting position to the prize is strictly less than the distance from Alice's starting position to the prize. The distance between any two points \(c\) and \(d\) is calculated as \(|c-d|\).Your task is to determine whether Bob can choose an integer point that is guarantee to get to the prize faster, regardless of where it appears (at point \(x\) or \(y\)). Bob can choose any integer point, except for \(a\) (in particular, he can choose to start in point \(x\), point \(y\), or any other point, but not \(a\)).
|
The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.The only line of each test case contains three integers \(a\), \(x\), \(y\) (\(1 \le a, x, y \le 100\)). Points \(a\), \(x\), and \(y\) are pairwise distinct.
|
For each test case, print ""YES"" (case insensitive) if Bob can choose an integer point that is guarantee to get to the prize faster, regardless of where it appears. Otherwise, print ""NO"" (case insensitive).
|
In the first example, Bob can choose point \(4\). If the prize will be at point \(x\), then Bob's distance is \(|4-3|=1\) and Alice's distance is \(|1-3|=2\). If the prize will be at point \(y\), then Bob's distance is \(|4-4|=0\) and Alice's distance is \(|1-4|=3\).In the second example, Bob can choose point \(2\). If the prize will be at point \(x\), then Bob's distance is \(|2-3|=1\) and Alice's distance is \(|5-3|=2\). If the prize will be at point \(y\), then Bob's distance is \(|2-1|=1\) and Alice's distance is \(|5-1|=4\). In the third example, Bob cannot choose a point to guarantee his victory.
|
Input: 31 3 45 3 13 1 5 | Output: YES YES NO
|
Beginner
| 2 | 953 | 255 | 209 | 21 |
1,517 |
A
|
1517A
|
A. Sum of 2050
| 800 |
greedy; math
|
A number is called 2050-number if it is \(2050\), \(20500\), ..., (\(2050 \cdot 10^k\) for integer \(k \ge 0\)).Given a number \(n\), you are asked to represent \(n\) as the sum of some (not necessarily distinct) 2050-numbers. Compute the minimum number of 2050-numbers required for that.
|
The first line contains a single integer \(T\) (\(1\le T\leq 1\,000\)) denoting the number of test cases.The only line of each test case contains a single integer \(n\) (\(1\le n\le 10^{18}\)) denoting the number to be represented.
|
For each test case, output the minimum number of 2050-numbers in one line. If \(n\) cannot be represented as the sum of 2050-numbers, output \(-1\) instead.
|
In the third case, \(4100 = 2050 + 2050\).In the fifth case, \(22550 = 20500 + 2050\).
|
Input: 6 205 2050 4100 20500 22550 25308639900 | Output: -1 1 2 1 2 36
|
Beginner
| 2 | 288 | 231 | 156 | 15 |
1,367 |
F1
|
1367F1
|
F1. Flying Sort (Easy Version)
| 2,100 |
dp; greedy; two pointers
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on \(n\) are less than in the hard version of the problem.You are given an array \(a\) of \(n\) integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index \(i\) (\(1 \le i \le n\)) and move the element \(a[i]\) to the begin of the array; choose any index \(i\) (\(1 \le i \le n\)) and move the element \(a[i]\) to the end of the array. For example, if \(n = 5\), \(a = [4, 7, 2, 3, 9]\), then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array \(a\) will become \([7, 4, 2, 3, 9]\); after performing the operation of the second type to the second element, the array \(a\) will become \([7, 2, 3, 9, 4]\). You can perform operations of any type any number of times in any order.Find the minimum total number of operations of the first and second type that will make the \(a\) array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities \(a[1] \le a[2] \le \ldots \le a[n]\).
|
The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases in the test. Then \(t\) test cases follow.Each test case starts with a line containing an integer \(n\) (\(1 \le n \le 3000\)) β length of the array \(a\).Then follow \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 10^9\)) β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.The sum of \(n\) for all test cases in one test does not exceed \(3000\).
|
For each test case output one integer β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
|
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: \([4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]\).In the second test case, you need to move the 1 to the beginning of the array, and the 8 β to the end. Therefore, the desired sequence of operations: \([3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]\).In the third test case, the array is already sorted.
|
Input: 4 5 4 7 2 3 9 5 3 5 8 1 7 5 1 4 5 7 12 4 0 2 1 3 | Output: 2 2 0 2
|
Hard
| 3 | 1,250 | 506 | 166 | 13 |
1,503 |
E
|
1503E
|
E. 2-Coloring
| 3,100 |
combinatorics; dp; math
|
There is a grid with \(n\) rows and \(m\) columns. Every cell of the grid should be colored either blue or yellow.A coloring of the grid is called stupid if every row has exactly one segment of blue cells and every column has exactly one segment of yellow cells.In other words, every row must have at least one blue cell, and all blue cells in a row must be consecutive. Similarly, every column must have at least one yellow cell, and all yellow cells in a column must be consecutive. An example of a stupid coloring. Examples of clever colorings. The first coloring is missing a blue cell in the second row, and the second coloring has two yellow segments in the second column. How many stupid colorings of the grid are there? Two colorings are considered different if there is some cell that is colored differently.
|
The only line contains two integers \(n\), \(m\) (\(1\le n, m\le 2021\)).
|
Output a single integer β the number of stupid colorings modulo \(998244353\).
|
In the first test case, these are the only two stupid \(2\times 2\) colorings.
|
Input: 2 2 | Output: 2
|
Master
| 3 | 817 | 73 | 78 | 15 |
1,310 |
D
|
1310D
|
D. Tourism
| 2,300 |
dp; graphs; probabilities
|
Masha lives in a country with \(n\) cities numbered from \(1\) to \(n\). She lives in the city number \(1\). There is a direct train route between each pair of distinct cities \(i\) and \(j\), where \(i \neq j\). In total there are \(n(n-1)\) distinct routes. Every route has a cost, cost for route from \(i\) to \(j\) may be different from the cost of route from \(j\) to \(i\).Masha wants to start her journey in city \(1\), take exactly \(k\) routes from one city to another and as a result return to the city \(1\). Masha is really careful with money, so she wants the journey to be as cheap as possible. To do so Masha doesn't mind visiting a city multiple times or even taking the same route multiple times.Masha doesn't want her journey to have odd cycles. Formally, if you can select visited by Masha city \(v\), take odd number of routes used by Masha in her journey and return to the city \(v\), such journey is considered unsuccessful.Help Masha to find the cheapest (with minimal total cost of all taken routes) successful journey.
|
First line of input had two integer numbers \(n,k\) (\(2 \leq n \leq 80; 2 \leq k \leq 10\)): number of cities in the country and number of routes in Masha's journey. It is guaranteed that \(k\) is even.Next \(n\) lines hold route descriptions: \(j\)-th number in \(i\)-th line represents the cost of route from \(i\) to \(j\) if \(i \neq j\), and is 0 otherwise (there are no routes \(i \to i\)). All route costs are integers from \(0\) to \(10^8\).
|
Output a single integer β total cost of the cheapest Masha's successful journey.
|
Input: 5 8 0 1 2 2 0 0 0 1 1 2 0 1 0 0 0 2 1 1 0 0 2 0 1 2 0 | Output: 2
|
Expert
| 3 | 1,043 | 450 | 80 | 13 |
|
364 |
B
|
364B
|
B. Free Market
| 2,200 |
dp; greedy
|
John Doe has recently found a ""Free Market"" in his city β that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
|
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
|
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
|
In the first sample John can act like this: Take the first item (1 - 0 β€ 2). Exchange the first item for the second one (3 - 1 β€ 2). Take the first item (1 - 0 β€ 2).
|
Input: 3 21 3 10 | Output: 4 3
|
Hard
| 2 | 969 | 242 | 150 | 3 |
1,084 |
B
|
1084B
|
B. Kvass and the Fair Nut
| 1,200 |
greedy; implementation
|
The Fair Nut likes kvass very much. On his birthday parents presented him \(n\) kegs of kvass. There are \(v_i\) liters of kvass in the \(i\)-th keg. Each keg has a lever. You can pour your glass by exactly \(1\) liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by \(s\) liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by \(s\) liters of kvass.
|
The first line contains two integers \(n\) and \(s\) (\(1 \le n \le 10^3\), \(1 \le s \le 10^{12}\)) β the number of kegs and glass volume.The second line contains \(n\) integers \(v_1, v_2, \ldots, v_n\) (\(1 \le v_i \le 10^9\)) β the volume of \(i\)-th keg.
|
If the Fair Nut cannot pour his glass by \(s\) liters of kvass, print \(-1\). Otherwise, print a single integer β how much kvass in the least keg can be.
|
In the first example, the answer is \(3\), the Fair Nut can take \(1\) liter from the first keg and \(2\) liters from the third keg. There are \(3\) liters of kvass in each keg.In the second example, the answer is \(2\), the Fair Nut can take \(3\) liters from the first keg and \(1\) liter from the second keg.In the third example, the Fair Nut can't pour his cup by \(7\) liters, so the answer is \(-1\).
|
Input: 3 34 3 5 | Output: 3
|
Easy
| 2 | 542 | 259 | 153 | 10 |
1,906 |
H
|
1906H
|
H. Twin Friends
| 2,200 |
combinatorics; dp
|
You meet two new friends who are twins. The name of the elder twin is \(A\), which consists of \(N\) characters. While the name of the younger twin is \(B\), which consists of \(M\) characters. It is known that \(N \leq M\).You want to call each of them with a nickname. For the elder twin, you want to pick any permutation of \(A\) as the nickname. For the younger twin, you want to remove exactly \(M - N\) characters from any permutation of \(B\). Denote the nicknames of the elder twin and the younger twin as \(A'\) and \(B'\), respectively.You want the nicknames to satisfy the following requirement. For each \(i\) that satisfies \(1 \leq i \leq N\), \(B'_i\) must be equal to either \(A'_i\) or the next letter that follows alphabetically after \(A'_i\) (if such a next letter exists).Determine the number of different pairs of nicknames \((A', B')\) that satisfy the requirement. Two pairs of nicknames are considered different if at least one of the nicknames are different. As the result might be large, find the answer modulo \(998\,244\,353\).
|
The first line consists of two integers \(N\) \(M\) (\(1 \leq N \leq M \leq 200\,000\)).The second line consists of a string \(A\) of length \(N\).The third line consists of a string \(B\) of length \(M\).All strings consist of only upper-case letters.
|
Output a single integer representing number of different pairs \((A', B')\) that satisfy the requirement, modulo \(998\,244\,353\).
|
Explanation for the sample input/output #1The \(9\) pairs are: (AAM, AAN), (AAM, ABN), (AAM, BAN), (AMA, ANA), (AMA, ANB), (AMA, BNA), (MAA, NAA), (MAA, NAB), and (MAA, NBA). Explanation for the sample input/output #2The \(120\) pairs are the pairs where \(A'\) is a permutation of BINUS and \(B' = A'\).
|
Input: 3 4 AMA ANAB | Output: 9
|
Hard
| 2 | 1,056 | 252 | 131 | 19 |
617 |
C
|
617C
|
C. Watering Flowers
| 1,600 |
implementation
|
A flowerbed has many flowers and two fountains.You can adjust the water pressure and set any values r1(r1 β₯ 0) and r2(r2 β₯ 0), giving the distances at which the water is spread from the first and second fountain respectively. You have to set such r1 and r2 that all the flowers are watered, that is, for each flower, the distance between the flower and the first fountain doesn't exceed r1, or the distance to the second fountain doesn't exceed r2. It's OK if some flowers are watered by both fountains.You need to decrease the amount of water you need, that is set such r1 and r2 that all the flowers are watered and the r12 + r22 is minimum possible. Find this minimum value.
|
The first line of the input contains integers n, x1, y1, x2, y2 (1 β€ n β€ 2000, - 107 β€ x1, y1, x2, y2 β€ 107) β the number of flowers, the coordinates of the first and the second fountain.Next follow n lines. The i-th of these lines contains integers xi and yi ( - 107 β€ xi, yi β€ 107) β the coordinates of the i-th flower.It is guaranteed that all n + 2 points in the input are distinct.
|
Print the minimum possible value r12 + r22. Note, that in this problem optimal answer is always integer.
|
The first sample is (r12 = 5, r22 = 1): The second sample is (r12 = 1, r22 = 32):
|
Input: 2 -1 0 5 30 25 2 | Output: 6
|
Medium
| 1 | 677 | 386 | 104 | 6 |
2,020 |
C
|
2020C
|
C. Bitwise Balancing
| 1,400 |
bitmasks; hashing; implementation; math; schedules; ternary search
|
You are given three non-negative integers \(b\), \(c\), and \(d\).Please find a non-negative integer \(a \in [0, 2^{61}]\) such that \((a\, |\, b)-(a\, \&\, c)=d\), where \(|\) and \(\&\) denote the bitwise OR operation and the bitwise AND operation, respectively.If such an \(a\) exists, print its value. If there is no solution, print a single integer \(-1\). If there are multiple solutions, print any of them.
|
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 only line of each test case contains three positive integers \(b\), \(c\), and \(d\) (\(0 \le b, c, d \le 10^{18}\)).
|
For each test case, output the value of \(a\), or \(-1\) if there is no solution. Please note that \(a\) must be non-negative and cannot exceed \(2^{61}\).
|
In the first test case, \((0\,|\,2)-(0\,\&\,2)=2-0=2\). So, \(a = 0\) is a correct answer.In the second test case, no value of \(a\) satisfies the equation.In the third test case, \((12\,|\,10)-(12\,\&\,2)=14-0=14\). So, \(a = 12\) is a correct answer.
|
Input: 32 2 24 2 610 2 14 | Output: 0 -1 12
|
Easy
| 6 | 413 | 282 | 155 | 20 |
1,659 |
F
|
1659F
|
F. Tree and Permutation Game
| 3,000 |
dfs and similar; games; graphs; trees
|
There is a tree of \(n\) vertices and a permutation \(p\) of size \(n\). A token is present on vertex \(x\) of the tree.Alice and Bob are playing a game. Alice is in control of the permutation \(p\), and Bob is in control of the token on the tree. In Alice's turn, she must pick two distinct numbers \(u\) and \(v\) (not positions; \(u \neq v\)), such that the token is neither at vertex \(u\) nor vertex \(v\) on the tree, and swap their positions in the permutation \(p\). In Bob's turn, he must move the token to an adjacent vertex from the one it is currently on.Alice wants to sort the permutation in increasing order. Bob wants to prevent that. Alice wins if the permutation is sorted in increasing order at the beginning or end of her turn. Bob wins if he can make the game go on for an infinite number of moves (which means that Alice is never able to get a sorted permutation). Both players play optimally. Alice makes the first move.Given the tree, the permutation \(p\), and the vertex \(x\) on which the token initially is, find the winner of the game.
|
The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. The description of each test case follows.The first line of each test case has two integers \(n\) and \(x\) (\(3 \leq n \leq 2 \cdot 10^5\); \(1 \leq x \leq n\)).Each of the next \(n-1\) lines contains two integers \(a\) and \(b\) (\(1 \le a, b \le n\), \(a \neq b\)) indicating an undirected edge between vertex \(a\) and vertex \(b\). It is guaranteed that the given edges form a tree.The next line contains \(n\) integers \(p_1, p_2, \ldots, p_n\) (\(1 \le p_i \le n\)) β the permutation \(p\).The sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output one line containing Alice or Bob β the winner of the game. The output is case-sensitive.
|
Here is the explanation for the first example:In the first test case, there is a way for Alice to win. Here is a possible sequence of moves: Alice swaps \(5\) and \(6\), resulting in \([2,1,3,5,4,6]\). Bob moves the token to vertex \(5\). Alice swaps \(1\) and \(2\), resulting in \([1,2,3,5,4,6]\). Bob moves the token to vertex \(3\). Alice swaps \(4\) and \(5\), resulting in \([1,2,3,4,5,6]\), and wins. In the second test case, Alice cannot win since Bob can make the game go on forever. Here is the sequence of moves: Alice can only swap \(1\) and \(3\), resulting in \([3,1,2]\). Bob moves the token to vertex \(1\). Alice can only swap \(2\) and \(3\), resulting in \([2,1,3]\). Bob moves the token to vertex \(2\). Alice can only swap \(1\) and \(3\), resulting in \([2,3,1]\). Bob moves the token to vertex \(3\). Alice can only swap \(1\) and \(2\), resulting in \([1,3,2]\). Bob moves the token to vertex \(2\). And then the sequence repeats forever.In the third test case, Alice wins immediately since the permutation is already sorted.
|
Input: 36 31 33 24 33 65 32 1 3 6 4 53 21 23 21 3 23 21 23 21 2 3 | Output: Alice Bob Alice
|
Master
| 4 | 1,064 | 665 | 115 | 16 |
123 |
D
|
123D
|
D. String
| 2,300 |
string suffix structures
|
You are given a string s. Each pair of numbers l and r that fulfill the condition 1 β€ l β€ r β€ |s|, correspond to a substring of the string s, starting in the position l and ending in the position r (inclusive).Let's define the function of two strings F(x, y) like this. We'll find a list of such pairs of numbers for which the corresponding substrings of string x are equal to string y. Let's sort this list of pairs according to the pair's first number's increasing. The value of function F(x, y) equals the number of non-empty continuous sequences in the list.For example: F(babbabbababbab, babb) = 6. The list of pairs is as follows:(1, 4), (4, 7), (9, 12)Its continuous sequences are: (1, 4) (4, 7) (9, 12) (1, 4), (4, 7) (4, 7), (9, 12) (1, 4), (4, 7), (9, 12) Your task is to calculate for the given string s the sum F(s, x) for all x, that x belongs to the set of all substrings of a string s.
|
The only line contains the given string s, consisting only of small Latin letters (1 β€ |s| β€ 105).
|
Print the single number β the sought sum.Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator.
|
In the first sample the function values at x equal to ""a"", ""aa"", ""aaa"" and ""aaaa"" equal 10, 6, 3 and 1 correspondingly.In the second sample for any satisfying x the function value is 1.
|
Input: aaaa | Output: 20
|
Expert
| 1 | 900 | 98 | 193 | 1 |
340 |
E
|
340E
|
E. Iahub and Permutations
| 2,000 |
combinatorics; math
|
Iahub is so happy about inventing bubble sort graphs that he's staying all day long at the office and writing permutations. Iahubina is angry that she is no more important for Iahub. When Iahub goes away, Iahubina comes to his office and sabotage his research work.The girl finds an important permutation for the research. The permutation contains n distinct integers a1, a2, ..., an (1 β€ ai β€ n). She replaces some of permutation elements with -1 value as a revenge. When Iahub finds out his important permutation is broken, he tries to recover it. The only thing he remembers about the permutation is it didn't have any fixed point. A fixed point for a permutation is an element ak which has value equal to k (ak = k). Your job is to proof to Iahub that trying to recover it is not a good idea. Output the number of permutations which could be originally Iahub's important permutation, modulo 1000000007 (109 + 7).
|
The first line contains integer n (2 β€ n β€ 2000). On the second line, there are n integers, representing Iahub's important permutation after Iahubina replaces some values with -1. It's guaranteed that there are no fixed points in the given permutation. Also, the given sequence contains at least two numbers -1 and each positive number occurs in the sequence at most once. It's guaranteed that there is at least one suitable permutation.
|
Output a single integer, the number of ways Iahub could recover his permutation, modulo 1000000007 (109 + 7).
|
For the first test example there are two permutations with no fixed points are [2, 5, 4, 3, 1] and [5, 1, 4, 3, 2]. Any other permutation would have at least one fixed point.
|
Input: 5-1 -1 4 3 -1 | Output: 2
|
Hard
| 2 | 916 | 437 | 109 | 3 |
2,093 |
C
|
2093C
|
C. Simple Repetition
| 1,000 |
math; number theory
|
Pasha loves prime numbers\(^{\text{β}}\)! Once again, in his attempts to find a new way to generate prime numbers, he became interested in an algorithm he found on the internet: To obtain a new number \(y\), repeat \(k\) times the decimal representation of the number \(x\) (without leading zeros). For example, for \(x = 52\) and \(k = 3\), we get \(y = 525252\), and for \(x = 6\) and \(k = 7\), we get \(y = 6666666\).Pasha really wants the resulting number \(y\) to be prime, but he doesn't yet know how to check the primality of numbers generated by this algorithm. Help Pasha and tell him whether \(y\) is prime!\(^{\text{β}}\)An integer \(x\) is considered prime if it has exactly \(2\) distinct divisors: \(1\) and \(x\). For example, \(13\) is prime because it has only \(2\) divisors: \(1\) and \(13\). Note that the number \(1\) is not prime, as it has only one divisor.
|
Each test consists of several sets of input data. The first line contains a single integer \(t\) (\(1 \leq t \leq 100\)) β the number of sets of input data. The following lines describe the sets of input data.The first and only line of each data set contains two integers: \(x\) and \(k\) (\(1 \leq x \leq 10^9\), \(1 \leq k \leq 7\)).
|
For each set of input data, output Β«YESΒ» (without quotes) if the resulting number \(y\) will be prime, and Β«NOΒ» otherwise.You may output Β«YesΒ» and Β«NoΒ» in any case (for example, the strings Β«yESΒ», Β«yesΒ», and Β«YesΒ» will be recognized as positive answers).
|
Input: 452 36 77 11 7 | Output: NO NO YES NO
|
Beginner
| 2 | 881 | 335 | 254 | 20 |
|
1,749 |
B
|
1749B
|
B. Death's Blessing
| 900 |
greedy
|
You are playing a computer game. To pass the current level, you have to kill a big horde of monsters. In this horde, there are \(n\) monsters standing in the row, numbered from \(1\) to \(n\). The \(i\)-th monster has \(a_i\) health and a special ""Death's Blessing"" spell of strength \(b_i\) attached to it.You are going to kill all of them one by one. It takes exactly \(h\) seconds to kill a monster with health \(h\).When the \(i\)-th monster dies, it casts its spell that increases the health of its neighbors by \(b_i\) (the neighbors of the \(j\)-th monster in the row are the monsters on places \(j - 1\) and \(j + 1\). The first and the last monsters have only one neighbor each).After each monster is killed, the row shrinks, so its former neighbors become adjacent to each other (so if one of them dies, the other one is affected by its spell). For example, imagine a situation with \(4\) monsters with health \(a = [2, 6, 7, 3]\) and spells \(b = [3, 6, 0, 5]\). One of the ways to get rid of the monsters is shown below: \(2\)\(6\)\(7\)\(3\)\(\xrightarrow{6\ s}\)\(8\)\(13\)\(3\)\(\xrightarrow{13\ s}\)\(8\)\(3\)\(\xrightarrow{8\ s}\)\(6\)\(\xrightarrow{6\ s}\)\(\{\}\)\(3\)\(6\)\(0\)\(5\)\(3\)\(0\)\(5\)\(3\)\(5\)\(5\) The first row represents the health of each monster, the second one β the power of the spells. As a result, we can kill all monsters in \(6 + 13 + 8 + 6\) \(=\) \(33\) seconds. Note that it's only an example and may not be the fastest way to get rid of the monsters.What is the minimum time required to kill all monsters in the row?
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of monsters in the row.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) β the initial health of corresponding monsters.The third line contains \(n\) integers \(b_1, b_2, \dots, b_n\) (\(0 \le b_i \le 10^9\)), where \(b_i\) is the strength of the spell for the \(i\)-th monster.It's guaranteed that the sum of \(n\) doesn't exceed \(2 \cdot 10^5\).
|
For each test case, print one integer β the minimum possible total time to kill all monsters.
|
In the first test case, there is only one monster that will be killed in \(10\) seconds.In the second test case, it's optimal to kill the first monster, the last monster and then the middle one. It will take \(100 + 100 + (1 + 1 + 1)\) \(=\) \(203\) seconds.In the third test case, it's optimal to kill the first monster, then the third one, then the fourth one and finally the second one. It will take \(2 + 7 + (3 + 0) + (3 + 6 + 5)\) \(=\) \(26\) seconds.
|
Input: 411003100 1 1001 100 142 6 7 33 6 0 521000000000 10000000001000000000 1000000000 | Output: 10 203 26 3000000000
|
Beginner
| 1 | 1,566 | 594 | 93 | 17 |
2,104 |
G
|
2104G
|
G. Modulo 3
| 2,700 |
data structures; divide and conquer; dsu; graphs; trees
|
Surely, you have seen problems which require you to output the answer modulo \(10^9+7\), \(10^9+9\), or \(998244353\). But have you ever seen a problem where you have to print the answer modulo \(3\)?You are given a functional graph consisting of \(n\) vertices, numbered from \(1\) to \(n\). It is a directed graph, in which each vertex has exactly one outgoing arc. The graph is given as the array \(g_1, g_2, \dots, g_n\), where \(g_i\) means that there is an arc that goes from \(i\) to \(g_i\). For some vertices, the outgoing arcs might be self-loops.Initially, all vertices of the graph are colored in color \(1\). You can perform the following operation: select a vertex and a color from \(1\) to \(k\), and then color this vertex and all vertices that are reachable from it. You can perform this operation any number of times (even zero).You should process \(q\) queries. The query is described by three integers \(x\), \(y\) and \(k\). For each query, you should: assign \(g_x := y\); then calculate the number of different graph colorings for the given value of \(k\) (two colorings are different if there exists at least one vertex that is colored in different colors in these two colorings); since the answer can be very large, print it modulo \(3\). Note that in every query, the initial coloring of the graph is reset (all vertices initially have color \(1\) in each query).
|
The first line contains two integers \(n\) and \(q\) (\(1 \le n, q \le 2 \cdot 10^5\)).The second line contains \(n\) integers \(g_1, g_2, \dots, g_n\) (\(1 \le g_i \le n\)).The \(q\) lines follow. The \(i\)-th line contains three integers \(x_i\), \(y_i\) and \(k_i\) (\(1 \le x_i, y_i \le n\); \(1 \le k_i \le 10^9\)).
|
For each query, print a single integer β the number of different graph colorings for the given value of \(k\), taken modulo \(3\).
|
Input: 4 52 3 1 44 3 12 1 23 4 34 1 52 4 4 | Output: 1 2 0 2 1
|
Master
| 5 | 1,389 | 320 | 130 | 21 |
|
780 |
F
|
780F
|
F. Axel and Marston in Bitland
| 2,400 |
bitmasks; dp; graphs; matrices
|
A couple of friends, Axel and Marston are travelling across the country of Bitland. There are n towns in Bitland, with some pairs of towns connected by one-directional roads. Each road in Bitland is either a pedestrian road or a bike road. There can be multiple roads between any pair of towns, and may even be a road from a town to itself. However, no pair of roads shares the starting and the destination towns along with their types simultaneously.The friends are now located in the town 1 and are planning the travel route. Axel enjoys walking, while Marston prefers biking. In order to choose a route diverse and equally interesting for both friends, they have agreed upon the following procedure for choosing the road types during the travel: The route starts with a pedestrian route. Suppose that a beginning of the route is written in a string s of letters P (pedestrain road) and B (biking road). Then, the string is appended to s, where stands for the string s with each character changed to opposite (that is, all pedestrian roads changed to bike roads, and vice versa).In the first few steps the route will look as follows: P, PB, PBBP, PBBPBPPB, PBBPBPPBBPPBPBBP, and so on.After that the friends start travelling from the town 1 via Bitlandian roads, choosing the next road according to the next character of their route type each time. If it is impossible to choose the next road, the friends terminate their travel and fly home instead.Help the friends to find the longest possible route that can be travelled along roads of Bitland according to the road types choosing procedure described above. If there is such a route with more than 1018 roads in it, print -1 instead.
|
The first line contains two integers n and m (1 β€ n β€ 500, 0 β€ m β€ 2n2) β the number of towns and roads in Bitland respectively.Next m lines describe the roads. i-th of these lines contains three integers vi, ui and ti (1 β€ vi, ui β€ n, 0 β€ ti β€ 1), where vi and ui denote start and destination towns indices of the i-th road, and ti decribes the type of i-th road (0 for a pedestrian road, 1 for a bike road). It is guaranteed that for each pair of distinct indices i, j such that 1 β€ i, j β€ m, either vi β vj, or ui β uj, or ti β tj holds.
|
If it is possible to find a route with length strictly greater than 1018, print -1. Otherwise, print the maximum length of a suitable path.
|
In the first sample we can obtain a route of length 3 by travelling along the road 1 from town 1 to town 2, and then following the road 2 twice from town 2 to itself.In the second sample we can obtain an arbitrarily long route by travelling the road 1 first, and then choosing road 2 or 3 depending on the necessary type.
|
Input: 2 21 2 02 2 1 | Output: 3
|
Expert
| 4 | 1,688 | 540 | 139 | 7 |
219 |
A
|
219A
|
A. k-String
| 1,000 |
implementation; strings
|
A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string ""aabaabaabaab"" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string.
|
The first input line contains integer k (1 β€ k β€ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 β€ |s| β€ 1000, where |s| is the length of string s.
|
Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them.If the solution doesn't exist, print ""-1"" (without quotes).
|
Input: 2aazz | Output: azaz
|
Beginner
| 2 | 492 | 232 | 233 | 2 |
|
1,975 |
I
|
1975I
|
I. Mind Bloom
| 3,500 |
dp
|
This is the way it always was. This is the way it always will be. All will be forgotten again soon...Jellyfish is playing a one-player card game called ""Slay the Spire"". There are \(n\) cards in total numbered from \(1\) to \(n\). The \(i\)-th card has power \(c_i\).There is a binary string \(s\) of length \(n\). If \(s_i = \texttt{0}\), the \(i\)-th card is initially in the draw pile. If \(s_i = \texttt{1}\), the \(i\)-th card is initially in Jellyfish's hand.Jellyfish will repeat the following process until either her hand or the draw pile is empty. Let \(x\) be the power of the card with the largest power in her hand. Place a single card with power \(x\) back into the draw pile. Randomly draw \(x\) cards from the draw pile. All subsets of \(x\) cards from the draw pile have an equal chance of being drawn. If there are fewer than \(x\) cards in the draw pile, Jellyfish will draw all cards. At the end of this process, find the probability that Jellyfish can empty the draw pile, modulo \(1\,000\,000\,007\).Formally, let \(M=1\,000\,000\,007\). 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}\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1\leq t\leq 100\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 120\)) β the number of cards.The second line of each test case contains \(n\) integers \(c_1,c_2,\ldots,c_n\) (\(0 \leq c_i \leq n\)) β the powers of the cards. It is guaranteed that \(c_1 \leq c_2 \leq \ldots \leq c_n\).The third line of each test case contains a binary string \(s\) of length \(n\). If \(s_i = \texttt{0}\), the \(i\)-th card is initially in the draw pile. If \(s_i = \texttt{1}\), the \(i\)-th card is initially in Jellyfish's hand.It is guaranteed that the sum of \(n^2\) over all test cases does not exceed \(120^2\).
|
For each test case, output the probability that Jellyfish can empty the draw pile modulo \(1\,000\,000\,007\).
|
In the first test case, Jellyfish will keep playing cards with power \(1\) until Jellyfish draws a card with power \(0\) or power \(2\). If Jellyfish draws a card with power \(0\), she will eventually empty her hand. If Jellyfish draws a card with power \(2\), she will eventually empty the draw pile. Since there is an equal chance of drawing \(0\) or \(2\), the answer is \(\frac{1}{2}\), and \(2 \cdot 500\,000\,004 \equiv 1 \pmod {10^9+7}\)
|
Input: 450 1 1 1 20010032 3 3000100 0 0 0 0 0 0 1 1 11111011111200 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 2 3 3 400000000001000101010 | Output: 500000004 0 0 675898154
|
Master
| 1 | 1,382 | 781 | 110 | 19 |
1,342 |
E
|
1342E
|
E. Placing Rooks
| 2,300 |
combinatorics; fft; math
|
Calculate the number of ways to place \(n\) rooks on \(n \times n\) chessboard so that both following conditions are met: each empty cell is under attack; exactly \(k\) pairs of rooks attack each other. An empty cell is under attack if there is at least one rook in the same row or at least one rook in the same column. Two rooks attack each other if they share the same row or column, and there are no other rooks between them. For example, there are only two pairs of rooks that attack each other in the following picture: One of the ways to place the rooks for \(n = 3\) and \(k = 2\) Two ways to place the rooks are considered different if there exists at least one cell which is empty in one of the ways but contains a rook in another way.The answer might be large, so print it modulo \(998244353\).
|
The only line of the input contains two integers \(n\) and \(k\) (\(1 \le n \le 200000\); \(0 \le k \le \frac{n(n - 1)}{2}\)).
|
Print one integer β the number of ways to place the rooks, taken modulo \(998244353\).
|
Input: 3 2 | Output: 6
|
Expert
| 3 | 804 | 126 | 86 | 13 |
|
630 |
O
|
630O
|
O. Arrow
| 2,000 |
geometry
|
Petya has recently started working as a programmer in the IT city company that develops computer games.Besides game mechanics implementation to create a game it is necessary to create tool programs that can be used by game designers to create game levels. Petya's first assignment is to create a tool that allows to paint different arrows on the screen.A user of this tool will choose a point on the screen, specify a vector (the arrow direction) and vary several parameters to get the required graphical effect. In the first version of the program Petya decided to limit parameters of the arrow by the following: a point with coordinates (px, py), a nonzero vector with coordinates (vx, vy), positive scalars a, b, c, d, a > c.The produced arrow should have the following properties. The arrow consists of a triangle and a rectangle. The triangle is isosceles with base of length a and altitude of length b perpendicular to the base. The rectangle sides lengths are c and d. Point (px, py) is situated in the middle of the triangle base and in the middle of side of rectangle that has length c. Area of intersection of the triangle and the rectangle is zero. The direction from (px, py) point to the triangle vertex opposite to base containing the point coincides with direction of (vx, vy) vector.Enumerate the arrow points coordinates in counter-clockwise order starting from the tip.
|
The only line of the input contains eight integers px, py, vx, vy ( - 1000 β€ px, py, vx, vy β€ 1000, vx2 + vy2 > 0), a, b, c, d (1 β€ a, b, c, d β€ 1000, a > c).
|
Output coordinates of the arrow points in counter-clockwise order. Each line should contain two coordinates, first x, then y. Relative or absolute error should not be greater than 10 - 9.
|
Input: 8 8 0 2 8 3 4 5 | Output: 8.000000000000 11.0000000000004.000000000000 8.0000000000006.000000000000 8.0000000000006.000000000000 3.00000000000010.000000000000 3.00000000000010.000000000000 8.00000000000012.000000000000 8.000000000000
|
Hard
| 1 | 1,387 | 158 | 187 | 6 |
|
1,356 |
A2
|
1356A2
|
A2. Distinguish I from Z
| 0 |
*special
|
You are given an operation that implements a single-qubit unitary transformation: either the identity (I gate) or the Z gate. The operation will have Adjoint and Controlled variants defined.Your task is to perform necessary operations and measurements to figure out which unitary it was and to return 0 if it was the I gate or 1 if it was the Z gate. You are allowed to apply the given operation and its adjoint/controlled variants exactly once.You have to implement an operation which takes a single-qubit operation as an input and returns an integer. Your code should have the following signature:namespace Solution { open Microsoft.Quantum.Intrinsic; operation Solve (unitary : (Qubit => Unit is Adj+Ctl)) : Int { // your code here }}
|
Beginner
| 1 | 737 | 0 | 0 | 13 |
||||
1,514 |
B
|
1514B
|
B. AND 0, Sum Big
| 1,200 |
bitmasks; combinatorics; math
|
Baby Badawy's first words were ""AND 0 SUM BIG"", so he decided to solve the following problem. Given two integers \(n\) and \(k\), count the number of arrays of length \(n\) such that: all its elements are integers between \(0\) and \(2^k-1\) (inclusive); the bitwise AND of all its elements is \(0\); the sum of its elements is as large as possible. Since the answer can be very large, print its remainder when divided by \(10^9+7\).
|
The first line contains an integer \(t\) (\(1 \le t \le 10\)) β the number of test cases you need to solve.Each test case consists of a line containing two integers \(n\) and \(k\) (\(1 \le n \le 10^{5}\), \(1 \le k \le 20\)).
|
For each test case, print the number of arrays satisfying the conditions. Since the answer can be very large, print its remainder when divided by \(10^9+7\).
|
In the first example, the \(4\) arrays are: \([3,0]\), \([0,3]\), \([1,2]\), \([2,1]\).
|
Input: 2 2 2 100000 20 | Output: 4 226732710
|
Easy
| 3 | 435 | 226 | 157 | 15 |
157 |
B
|
157B
|
B. Trace
| 1,000 |
geometry; sortings
|
One day, as Sherlock Holmes was tracking down one very important criminal, he found a wonderful painting on the wall. This wall could be represented as a plane. The painting had several concentric circles that divided the wall into several parts. Some parts were painted red and all the other were painted blue. Besides, any two neighboring parts were painted different colors, that is, the red and the blue color were alternating, i. e. followed one after the other. The outer area of the wall (the area that lied outside all circles) was painted blue. Help Sherlock Holmes determine the total area of red parts of the wall.Let us remind you that two circles are called concentric if their centers coincide. Several circles are called concentric if any two of them are concentric.
|
The first line contains the single integer n (1 β€ n β€ 100). The second line contains n space-separated integers ri (1 β€ ri β€ 1000) β the circles' radii. It is guaranteed that all circles are different.
|
Print the single real number β total area of the part of the wall that is painted red. The answer is accepted if absolute or relative error doesn't exceed 10 - 4.
|
In the first sample the picture is just one circle of radius 1. Inner part of the circle is painted red. The area of the red part equals Ο Γ 12 = Ο.In the second sample there are three circles of radii 1, 4 and 2. Outside part of the second circle is painted blue. Part between the second and the third circles is painted red. Part between the first and the third is painted blue. And, finally, the inner part of the first circle is painted red. Overall there are two red parts: the ring between the second and the third circles and the inner part of the first circle. Total area of the red parts is equal (Ο Γ 42 - Ο Γ 22) + Ο Γ 12 = Ο Γ 12 + Ο = 13Ο
|
Input: 11 | Output: 3.1415926536
|
Beginner
| 2 | 781 | 201 | 162 | 1 |
285 |
B
|
285B
|
B. Find Marble
| 1,200 |
implementation
|
Petya and Vasya are playing a game. Petya's got n non-transparent glasses, standing in a row. The glasses' positions are indexed with integers from 1 to n from left to right. Note that the positions are indexed but the glasses are not.First Petya puts a marble under the glass in position s. Then he performs some (possibly zero) shuffling operations. One shuffling operation means moving the glass from the first position to position p1, the glass from the second position to position p2 and so on. That is, a glass goes from position i to position pi. Consider all glasses are moving simultaneously during one shuffling operation. When the glasses are shuffled, the marble doesn't travel from one glass to another: it moves together with the glass it was initially been put in.After all shuffling operations Petya shows Vasya that the ball has moved to position t. Vasya's task is to say what minimum number of shuffling operations Petya has performed or determine that Petya has made a mistake and the marble could not have got from position s to position t.
|
The first line contains three integers: n, s, t (1 β€ n β€ 105; 1 β€ s, t β€ n) β the number of glasses, the ball's initial and final position. The second line contains n space-separated integers: p1, p2, ..., pn (1 β€ pi β€ n) β the shuffling operation parameters. It is guaranteed that all pi's are distinct.Note that s can equal t.
|
If the marble can move from position s to position t, then print on a single line a non-negative integer β the minimum number of shuffling operations, needed to get the marble to position t. If it is impossible, print number -1.
|
Input: 4 2 12 3 4 1 | Output: 3
|
Easy
| 1 | 1,061 | 328 | 228 | 2 |
|
432 |
D
|
432D
|
D. Prefixes and Suffixes
| 2,000 |
dp; string suffix structures; strings; two pointers
|
You have a string s = s1s2...s|s|, where |s| is the length of string s, and si its i-th character. Let's introduce several definitions: A substring s[i..j] (1 β€ i β€ j β€ |s|) of string s is string sisi + 1...sj. The prefix of string s of length l (1 β€ l β€ |s|) is string s[1..l]. The suffix of string s of length l (1 β€ l β€ |s|) is string s[|s| - l + 1..|s|]. Your task is, for any prefix of string s which matches a suffix of string s, print the number of times it occurs in string s as a substring.
|
The single line contains a sequence of characters s1s2...s|s| (1 β€ |s| β€ 105) β string s. The string only consists of uppercase English letters.
|
In the first line, print integer k (0 β€ k β€ |s|) β the number of prefixes that match a suffix of string s. Next print k lines, in each line print two integers li ci. Numbers li ci mean that the prefix of the length li matches the suffix of length li and occurs in string s as a substring ci times. Print pairs li ci in the order of increasing li.
|
Input: ABACABA | Output: 31 43 27 1
|
Hard
| 4 | 499 | 144 | 346 | 4 |
|
1,251 |
E2
|
1251E2
|
E2. Voting (Hard Version)
| 2,400 |
binary search; data structures; greedy
|
The only difference between easy and hard versions is constraints.Now elections are held in Berland and you want to win them. More precisely, you want everyone to vote for you.There are \(n\) voters, and two ways to convince each of them to vote for you. The first way to convince the \(i\)-th voter is to pay him \(p_i\) coins. The second way is to make \(m_i\) other voters vote for you, and the \(i\)-th voter will vote for free.Moreover, the process of such voting takes place in several steps. For example, if there are five voters with \(m_1 = 1\), \(m_2 = 2\), \(m_3 = 2\), \(m_4 = 4\), \(m_5 = 5\), then you can buy the vote of the fifth voter, and eventually everyone will vote for you. Set of people voting for you will change as follows: \({5} \rightarrow {1, 5} \rightarrow {1, 2, 3, 5} \rightarrow {1, 2, 3, 4, 5}\).Calculate the minimum number of coins you have to spend so that everyone votes for you.
|
The first line contains one integer \(t\) (\(1 \le t \le 2 \cdot 10^5\)) β the number of test cases.The first line of each test case contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of voters.The next \(n\) lines contains the description of voters. \(i\)-th line contains two integers \(m_i\) and \(p_i\) (\(1 \le p_i \le 10^9, 0 \le m_i < n\)).It is guaranteed that the sum of all \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case print one integer β the minimum number of coins you have to spend so that everyone votes for you.
|
In the first test case you have to buy vote of the third voter. Then the set of people voting for you will change as follows: \({3} \rightarrow {1, 3} \rightarrow {1, 2, 3}\).In the second example you don't need to buy votes. The set of people voting for you will change as follows: \({1} \rightarrow {1, 3, 5} \rightarrow {1, 2, 3, 5} \rightarrow {1, 2, 3, 5, 6, 7} \rightarrow {1, 2, 3, 4, 5, 6, 7}\).In the third test case you have to buy votes of the second and the fifth voters. Then the set of people voting for you will change as follows: \({2, 5} \rightarrow {1, 2, 3, 4, 5} \rightarrow {1, 2, 3, 4, 5, 6}\).
|
Input: 3 3 1 5 2 10 2 8 7 0 1 3 1 1 1 6 1 1 1 4 1 4 1 6 2 6 2 3 2 8 2 7 4 4 5 5 | Output: 8 0 7
|
Expert
| 3 | 916 | 463 | 116 | 12 |
707 |
D
|
707D
|
D. Persistent Bookcase
| 2,200 |
bitmasks; data structures; dfs and similar; implementation
|
Recently in school Alina has learned what are the persistent data structures: they are data structures that always preserves the previous version of itself and access to it when it is modified.After reaching home Alina decided to invent her own persistent data structure. Inventing didn't take long: there is a bookcase right behind her bed. Alina thinks that the bookcase is a good choice for a persistent data structure. Initially the bookcase is empty, thus there is no book at any position at any shelf.The bookcase consists of n shelves, and each shelf has exactly m positions for books at it. Alina enumerates shelves by integers from 1 to n and positions at shelves β from 1 to m. Initially the bookcase is empty, thus there is no book at any position at any shelf in it.Alina wrote down q operations, which will be consecutively applied to the bookcase. Each of the operations has one of four types: 1 i j β Place a book at position j at shelf i if there is no book at it. 2 i j β Remove the book from position j at shelf i if there is a book at it. 3 i β Invert book placing at shelf i. This means that from every position at shelf i which has a book at it, the book should be removed, and at every position at shelf i which has not book at it, a book should be placed. 4 k β Return the books in the bookcase in a state they were after applying k-th operation. In particular, k = 0 means that the bookcase should be in initial state, thus every book in the bookcase should be removed from its position.After applying each of operation Alina is interested in the number of books in the bookcase. Alina got 'A' in the school and had no problem finding this values. Will you do so?
|
The first line of the input contains three integers n, m and q (1 β€ n, m β€ 103, 1 β€ q β€ 105) β the bookcase dimensions and the number of operations respectively.The next q lines describes operations in chronological order β i-th of them describes i-th operation in one of the four formats described in the statement.It is guaranteed that shelf indices and position indices are correct, and in each of fourth-type operation the number k corresponds to some operation before it or equals to 0.
|
For each operation, print the number of books in the bookcase after applying it in a separate line. The answers should be printed in chronological order.
|
This image illustrates the second sample case.
|
Input: 2 3 31 1 13 24 0 | Output: 140
|
Hard
| 4 | 1,687 | 491 | 153 | 7 |
1,639 |
I
|
1639I
|
I. Treasure Hunt
| 0 |
graphs; interactive
|
All problems in this contest share the same statement, the only difference is the test your solution runs on. For further information on scoring please refer to ""Scoring"" section of the statement.This is an interactive problem.Imagine you are a treasure hunter, a very skillful one. One day you came across an ancient map which could help you to become rich. The map shows multiple forestry roads, and at each junction there is a treasure. So, you start your journey hoping to retrieve all the hidden treasures, but you don't know yet that there is a wicked wizard standing against you and craving to tangle up these roads and impede your achievements.The treasure map is represented as an undirected graph in which vertices correspond to junctions and edges correspond to roads. Your path begins at a certain fixed vertex with a label known to you. Every time you come to a vertex that you have not been to before, you dig up a treasure chest and put a flag in this vertex. At the initial vertex you'll find a treasure chest immediately and, consequently, you'll put a flag there immediately as well.When you are standing at the junction you can see for each of the adjacent vertices its degree and if there is a flag there. There are no other things you can see from there. Besides, the power of the wicked wizard is so great that he is able to change the location of the roads and junctions on the map without changing the graph structure. Therefore, the sequence of the roads coming from the junction \(v\) might be different each time you come in the junction \(v\). However, keep in mind that the set of adjacent crossroads does not change, and you are well aware of previously dug treasures at each adjacent to \(v\) vertex.Your goal is to collect treasures from all vertices of the graph as fast as you can. Good luck in hunting!
|
Beginner
| 2 | 1,839 | 0 | 0 | 16 |
||||
472 |
G
|
472G
|
G. Design Tutorial: Increase the Constraints
| 2,800 |
bitmasks; data structures; fft
|
There is a simple way to create hard tasks: take one simple problem as the query, and try to find an algorithm that can solve it faster than bruteforce. This kind of tasks usually appears in OI contest, and usually involves data structures.Let's try to create a task, for example, we take the ""Hamming distance problem"": for two binary strings s and t with the same length, the Hamming distance between them is the number of positions at which the corresponding symbols are different. For example, the Hamming distance between ""00111"" and ""10101"" is 2 (the different symbols are marked with bold).We use the Hamming distance problem as a query in the following way: you are given two strings a and b and several queries. Each query will be: what is the Hamming distance between two strings ap1ap1 + 1...ap1 + len - 1 and bp2bp2 + 1...bp2 + len - 1?Note, that in this problem the strings are zero-based, that is s = s0s1... s|s| - 1.
|
The first line contains a string a (1 β€ |a| β€ 200000). The second line contains a string b (1 β€ |b| β€ 200000). Each character of both strings is either ""0"" or ""1"".The third line contains an integer q (1 β€ q β€ 400000) β the number of queries. Each of the following q lines contains three integers: p1, p2 and len (0 β€ p1 β€ |a| - len; 0 β€ p2 β€ |b| - len), these numbers denote the parameters of the current query.
|
Output q integers β the answers for the queries.
|
Input: 1010101111000030 0 32 3 45 7 1 | Output: 110
|
Master
| 3 | 938 | 415 | 48 | 4 |
|
1,427 |
B
|
1427B
|
B. Chess Cheater
| 1,400 |
greedy; implementation; sortings
|
You like playing chess tournaments online.In your last tournament you played \(n\) games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get \(0\) points. When you win you get \(1\) or \(2\) points: if you have won also the previous game you get \(2\) points, otherwise you get \(1\) point. If you win the very first game of the tournament you get \(1\) point (since there is not a ""previous game"").The outcomes of the \(n\) games are represented by a string \(s\) of length \(n\): the \(i\)-th character of \(s\) is W if you have won the \(i\)-th game, while it is L if you have lost the \(i\)-th game.After the tournament, you notice a bug on the website that allows you to change the outcome of at most \(k\) of your games (meaning that at most \(k\) times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.Compute the maximum score you can get by cheating in the optimal way.
|
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1\le t \le 20,000\)) β the number of test cases. The description of the test cases follows.The first line of each testcase contains two integers \(n, k\) (\(1\le n\le 100,000\), \(0\le k\le n\)) β the number of games played and the number of outcomes that you can change.The second line contains a string \(s\) of length \(n\) containing only the characters W and L. If you have won the \(i\)-th game then \(s_i=\,\)W, if you have lost the \(i\)-th game then \(s_i=\,\)L.It is guaranteed that the sum of \(n\) over all testcases does not exceed \(200,000\).
|
For each testcase, print a single integer β the maximum score you can get by cheating in the optimal way.
|
Explanation of the first testcase. Before changing any outcome, the score is \(2\). Indeed, you won the first game, so you got \(1\) point, and you won also the third, so you got another \(1\) point (and not \(2\) because you lost the second game).An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is \(7=1+2+2+2\): \(1\) point for the first game and \(2\) points for the second, third and fourth game.Explanation of the second testcase. Before changing any outcome, the score is \(3\). Indeed, you won the fourth game, so you got \(1\) point, and you won also the fifth game, so you got \(2\) more points (since you won also the previous game).An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is \(11 = 1+2+2+2+2+2\): \(1\) point for the first game and \(2\) points for all the other games.
|
Input: 8 5 2 WLWLL 6 5 LLLWWL 7 1 LWLWLWL 15 5 WWWLLLWWWLLLWWW 40 7 LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL 1 0 L 1 1 L 6 1 WLLWLW | Output: 7 11 6 26 46 0 1 6
|
Easy
| 3 | 1,026 | 641 | 105 | 14 |
466 |
D
|
466D
|
D. Increase Sequence
| 2,100 |
combinatorics; dp
|
Peter has a sequence of integers a1, a2, ..., an. Peter wants all numbers in the sequence to equal h. He can perform the operation of ""adding one on the segment [l, r]"": add one to all elements of the sequence with indices from l to r (inclusive). At that, Peter never chooses any element as the beginning of the segment twice. Similarly, Peter never chooses any element as the end of the segment twice. In other words, for any two segments [l1, r1] and [l2, r2], where Peter added one, the following inequalities hold: l1 β l2 and r1 β r2.How many distinct ways are there to make all numbers in the sequence equal h? Print this number of ways modulo 1000000007 (109 + 7). Two ways are considered distinct if one of them has a segment that isn't in the other way.
|
The first line contains two integers n, h (1 β€ n, h β€ 2000). The next line contains n integers a1, a2, ..., an (0 β€ ai β€ 2000).
|
Print a single integer β the answer to the problem modulo 1000000007 (109 + 7).
|
Input: 3 21 1 1 | Output: 4
|
Hard
| 2 | 765 | 127 | 79 | 4 |
|
1,923 |
A
|
1923A
|
A. Moving Chips
| 800 |
greedy; implementation
|
There is a ribbon divided into \(n\) cells, numbered from \(1\) to \(n\) from left to right. Each cell either contains a chip or is free.You can perform the following operation any number of times (possibly zero): choose a chip and move it to the closest free cell to the left. You can choose any chip that you want, provided that there is at least one free cell to the left of it. When you move the chip, the cell where it was before the operation becomes free.Your goal is to move the chips in such a way that they form a single block, without any free cells between them. What is the minimum number of operations you have to perform?
|
The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.Each test case consists of two lines: the first line contains one integer \(n\) (\(2 \le n \le 50\)) β the number of cells; the second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i \le 1\)); \(a_i = 0\) means that the \(i\)-th cell is free; \(a_i = 1\) means that the \(i\)-th cell contains a chip. Additional constraint on the input: in each test case, at least one cell contains a chip.
|
For each test case, print one integer β the minimum number of operations you have to perform so that all chips form a single block without any free cells between them.
|
In the first example, you can perform the operation on the chip in the \(7\)-th cell. The closest free cell to the left is the \(5\)-th cell, so it moves there. After that, all chips form a single block.In the second example, all chips are already in a single block. Same for the third example.
|
Input: 580 1 1 1 0 1 1 060 1 0 0 0 061 1 1 1 1 151 0 1 0 190 1 1 0 0 0 1 1 0 | Output: 1 0 0 2 3
|
Beginner
| 2 | 636 | 501 | 167 | 19 |
300 |
E
|
300E
|
E. Empire Strikes Back
| 2,300 |
binary search; math; number theory
|
In a far away galaxy there is war again. The treacherous Republic made k precision strikes of power ai on the Empire possessions. To cope with the republican threat, the Supreme Council decided to deal a decisive blow to the enemy forces. To successfully complete the conflict, the confrontation balance after the blow should be a positive integer. The balance of confrontation is a number that looks like , where p = n! (n is the power of the Imperial strike), . After many years of war the Empire's resources are low. So to reduce the costs, n should be a minimum positive integer that is approved by the commanders.Help the Empire, find the minimum positive integer n, where the described fraction is a positive integer.
|
The first line contains integer k (1 β€ k β€ 106). The second line contains k integers a1, a2, ..., ak (1 β€ ai β€ 107).
|
Print the minimum positive integer n, needed for the Empire to win.Please, do not use the %lld to read or write 64-but integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator.
|
Input: 21000 1000 | Output: 2000
|
Expert
| 3 | 723 | 116 | 207 | 3 |
|
1,970 |
G1
|
1970G1
|
G1. Min-Fund Prison (Easy)
| 1,900 |
dfs and similar; trees
|
In the easy version, \(m = n-1\) and there exists a path between \(u\) and \(v\) for all \(u, v\) (\(1 \leq u, v \leq n\)).After a worker's strike organized by the Dementors asking for equal rights, the prison of Azkaban has suffered some damage. After settling the spirits, the Ministry of Magic is looking to renovate the prison to ensure that the Dementors are kept in check. The prison consists of \(n\) prison cells and \(m\) bi-directional corridors. The \(i^{th}\) corridor is from cells \(u_i\) to \(v_i\). A subset of these cells \(S\) is called a complex if any cell in \(S\) is reachable from any other cell in \(S\). Formally, a subset of cells \(S\) is a complex if \(x\) and \(y\) are reachable from each other for all \(x, y \in S\), using only cells from \(S\) on the way. The funding required for a complex \(S\) consisting of \(k\) cells is defined as \(k^2\).As part of your Intro to Magical Interior Design course at Hogwarts, you have been tasked with designing the prison. The Ministry of Magic has asked that you divide the prison into \(2\) complexes with \(\textbf{exactly one corridor}\) connecting them, so that the Dementors can't organize union meetings. For this purpose, you are allowed to build bi-directional corridors. The funding required to build a corridor between any \(2\) cells is \(c\). Due to budget cuts and the ongoing fight against the Death Eaters, you must find the \(\textbf{minimum total funding}\) required to divide the prison as per the Ministry's requirements or \(-1\) if no division is possible.Note: The total funding is the sum of the funding required for the \(2\) complexes and the corridors built. If after the division, the two complexes have \(x\) and \(y\) cells respectively and you have built a total of \(a\) corridors, the total funding will be \(x^2 + y^2 + c \times a\). Note that \(x+y=n\).
|
The first line contains one integer \(t\) (\(1 \leq t \leq 10^5\)) β the number of test cases. Then \(t\) test cases follow.The first line of each test case consists of three integers \(n, m\) and \(c\) (\(2 \leq n \leq 10^5\), \(m = n - 1\), \(1 \leq c \leq 10^9\))\(m\) lines follow, each consisting of 2 integers β \(u_i, v_i\) indicating a corridor is present between cells \(u_i\) and \(v_i\) (\(1 \leq u_i, v_i \leq n\), \(u_i \neq v_i\))It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).It is guaranteed that the sum of \(m\) over all test cases does not exceed \(5 \times 10^5\).It is guaranteed that there exists at most one corridor between any two cells.
|
Print the \(\textbf{minimum funding}\) required to divide the prison as per the Ministry's requirements or \(-1\) if no division is possible.
|
Input: 2 2 1 3 1 2 8 7 76 3 1 3 2 2 4 2 5 4 6 4 7 7 8 | Output: 2 32
|
Hard
| 2 | 1,860 | 699 | 141 | 19 |
|
15 |
C
|
15C
|
C. Industrial Nim
| 2,000 |
games
|
There are n stone quarries in Petrograd.Each quarry owns mi dumpers (1 β€ i β€ n). It is known that the first dumper of the i-th quarry has xi stones in it, the second dumper has xi + 1 stones in it, the third has xi + 2, and the mi-th dumper (the last for the i-th quarry) has xi + mi - 1 stones in it.Two oligarchs play a well-known game Nim. Players take turns removing stones from dumpers. On each turn, a player can select any dumper and remove any non-zero amount of stones from it. The player who cannot take a stone loses.Your task is to find out which oligarch will win, provided that both of them play optimally. The oligarchs asked you not to reveal their names. So, let's call the one who takes the first stone Β«tolikΒ» and the other one Β«bolikΒ».
|
The first line of the input contains one integer number n (1 β€ n β€ 105) β the amount of quarries. Then there follow n lines, each of them contains two space-separated integers xi and mi (1 β€ xi, mi β€ 1016) β the amount of stones in the first dumper of the i-th quarry and the number of dumpers at the i-th quarry.
|
Output Β«tolikΒ» if the oligarch who takes a stone first wins, and Β«bolikΒ» otherwise.
|
Input: 22 13 2 | Output: tolik
|
Hard
| 1 | 755 | 313 | 83 | 0 |
|
391 |
E1
|
391E1
|
E1. Three Trees
| 0 |
This problem consists of two subproblems: for solving subproblem E1 you will receive 11 points, and for solving subproblem E2 you will receive 13 points.A tree is an undirected connected graph containing no cycles. The distance between two nodes in an unweighted tree is the minimum number of edges that have to be traversed to get from one node to another.You are given 3 trees that have to be united into a single tree by adding two edges between these trees. Each of these edges can connect any pair of nodes from two trees. After the trees are connected, the distances between all unordered pairs of nodes in the united tree should be computed. What is the maximum possible value of the sum of these distances?
|
The first line contains three space-separated integers n1, n2, n3 β the number of vertices in the first, second, and third trees, respectively. The following n1 - 1 lines describe the first tree. Each of these lines describes an edge in the first tree and contains a pair of integers separated by a single space β the numeric labels of vertices connected by the edge. The following n2 - 1 lines describe the second tree in the same fashion, and the n3 - 1 lines after that similarly describe the third tree. The vertices in each tree are numbered with consecutive integers starting with 1.The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. In subproblem E1 (11 points), the number of vertices in each tree will be between 1 and 1000, inclusive. In subproblem E2 (13 points), the number of vertices in each tree will be between 1 and 100000, inclusive.
|
Print a single integer number β the maximum possible sum of distances between all pairs of nodes in the united tree.
|
Consider the first test case. There are two trees composed of two nodes, and one tree with three nodes. The maximum possible answer is obtained if the trees are connected in a single chain of 7 vertices.In the second test case, a possible choice of new edges to obtain the maximum answer is the following: Connect node 3 from the first tree to node 1 from the second tree; Connect node 2 from the third tree to node 1 from the second tree.
|
Input: 2 2 31 21 21 22 3 | Output: 56
|
Beginner
| 0 | 714 | 1,012 | 116 | 3 |
|
327 |
D
|
327D
|
D. Block Tower
| 1,900 |
constructive algorithms; dfs and similar; graphs
|
After too much playing on paper, Iahub has switched to computer games. The game he plays is called ""Block Towers"". It is played in a rectangular grid with n rows and m columns (it contains n Γ m cells). The goal of the game is to build your own city. Some cells in the grid are big holes, where Iahub can't build any building. The rest of cells are empty. In some empty cell Iahub can build exactly one tower of two following types: Blue towers. Each has population limit equal to 100. Red towers. Each has population limit equal to 200. However, it can be built in some cell only if in that moment at least one of the neighbouring cells has a Blue Tower. Two cells are neighbours is they share a side. Iahub is also allowed to destroy a building from any cell. He can do this operation as much as he wants. After destroying a building, the other buildings are not influenced, and the destroyed cell becomes empty (so Iahub can build a tower in this cell if needed, see the second example for such a case).Iahub can convince as many population as he wants to come into his city. So he needs to configure his city to allow maximum population possible. Therefore he should find a sequence of operations that builds the city in an optimal way, so that total population limit is as large as possible.He says he's the best at this game, but he doesn't have the optimal solution. Write a program that calculates the optimal one, to show him that he's not as good as he thinks.
|
The first line of the input contains two integers n and m (1 β€ n, m β€ 500). Each of the next n lines contains m characters, describing the grid. The j-th character in the i-th line is '.' if you're allowed to build at the cell with coordinates (i, j) a tower (empty cell) or '#' if there is a big hole there.
|
Print an integer k in the first line (0 β€ k β€ 106) β the number of operations Iahub should perform to obtain optimal result.Each of the following k lines must contain a single operation in the following format: Β«B x yΒ» (1 β€ x β€ n, 1 β€ y β€ m) β building a blue tower at the cell (x, y); Β«R x yΒ» (1 β€ x β€ n, 1 β€ y β€ m) β building a red tower at the cell (x, y); Β«D x yΒ» (1 β€ x β€ n, 1 β€ y β€ m) β destroying a tower at the cell (x, y). If there are multiple solutions you can output any of them. Note, that you shouldn't minimize the number of operations.
|
Input: 2 3..#.#. | Output: 4B 1 1R 1 2R 2 1B 2 3
|
Hard
| 3 | 1,472 | 308 | 551 | 3 |
|
1,476 |
A
|
1476A
|
A. K-divisible Sum
| 1,000 |
binary search; constructive algorithms; greedy; math
|
You are given two integers \(n\) and \(k\).You should create an array of \(n\) positive integers \(a_1, a_2, \dots, a_n\) such that the sum \((a_1 + a_2 + \dots + a_n)\) is divisible by \(k\) and maximum element in \(a\) is minimum possible.What is the minimum possible maximum element in \(a\)?
|
The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.The first and only line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 10^9\); \(1 \le k \le 10^9\)).
|
For each test case, print one integer β the minimum possible maximum element in array \(a\) such that the sum \((a_1 + \dots + a_n)\) is divisible by \(k\).
|
In the first test case \(n = 1\), so the array consists of one element \(a_1\) and if we make \(a_1 = 5\) it will be divisible by \(k = 5\) and the minimum possible.In the second test case, we can create array \(a = [1, 2, 1, 2]\). The sum is divisible by \(k = 3\) and the maximum is equal to \(2\).In the third test case, we can create array \(a = [1, 1, 1, 1, 1, 1, 1, 1]\). The sum is divisible by \(k = 8\) and the maximum is equal to \(1\).
|
Input: 4 1 5 4 3 8 8 8 17 | Output: 5 2 1 3
|
Beginner
| 4 | 295 | 222 | 156 | 14 |
1,778 |
C
|
1778C
|
C. Flexible String
| 1,600 |
bitmasks; brute force; strings
|
You have a string \(a\) and a string \(b\). Both of the strings have length \(n\). There are at most \(10\) different characters in the string \(a\). You also have a set \(Q\). Initially, the set \(Q\) is empty. You can apply the following operation on the string \(a\) any number of times: Choose an index \(i\) (\(1\leq i \leq n\)) and a lowercase English letter \(c\). Add \(a_i\) to the set \(Q\) and then replace \(a_i\) with \(c\). For example, Let the string \(a\) be ""\(\tt{abecca}\)"". We can do the following operations: In the first operation, if you choose \(i = 3\) and \(c = \tt{x}\), the character \(a_3 = \tt{e}\) will be added to the set \(Q\). So, the set \(Q\) will be \(\{\tt{e}\}\), and the string \(a\) will be ""\(\tt{ab\underline{x}cca}\)"". In the second operation, if you choose \(i = 6\) and \(c = \tt{s}\), the character \(a_6 = \tt{a}\) will be added to the set \(Q\). So, the set \(Q\) will be \(\{\tt{e}, \tt{a}\}\), and the string \(a\) will be ""\(\tt{abxcc\underline{s}}\)"". You can apply any number of operations on \(a\), but in the end, the set \(Q\) should contain at most \(k\) different characters. Under this constraint, you have to maximize the number of integer pairs \((l, r)\) (\(1\leq l\leq r \leq n\)) such that \(a[l,r] = b[l,r]\). Here, \(s[l,r]\) means the substring of string \(s\) starting at index \(l\) (inclusively) and ending at index \(r\) (inclusively).
|
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 two integers \(n\) and \(k\) (\(1\leq n \leq 10^5\), \(0\leq k\leq 10\)) β the length of the two strings and the limit on different characters in the set \(Q\). The second line contains the string \(a\) of length \(n\). There is at most \(10\) different characters in the string \(a\).The last line contains the string \(b\) of length \(n\). Both of the strings \(a\) and \(b\) contain only lowercase English letters. The sum of \(n\) over all test cases doesn't exceed \(10^5\).
|
For each test case, print a single integer in a line, the maximum number of pairs \((l, r)\) satisfying the constraints.
|
In the first case, we can select index \(i = 3\) and replace it with character \(c = \tt{d}\). All possible pairs \((l,r)\) will be valid.In the second case, we can't perform any operation. The \(3\) valid pairs \((l,r)\) are: \(a[1,1] = b[1,1] =\) ""\(\tt{a}\)"", \(a[1,2] = b[1,2] =\) ""\(\tt{ab}\)"", \(a[2,2] = b[2,2] =\) ""\(\tt{b}\)"". In the third case, we can choose index \(2\) and index \(3\) and replace them with the characters \(\tt{c}\) and \(\tt{d}\) respectively. The final set \(Q\) will be \(\{\tt{b}\}\) having size \(1\) that satisfies the value of \(k\). All possible pairs \((l,r)\) will be valid.
|
Input: 63 1abcabd3 0abcabd3 1xbbxcd4 1abcdaxcb3 10abcabd10 3lkwhbahuqaqoiujoncjb | Output: 6 3 6 6 6 11
|
Medium
| 3 | 1,413 | 664 | 120 | 17 |
1,811 |
E
|
1811E
|
E. Living Sequence
| 1,500 |
binary search; dp; math; number theory
|
In Japan, the number \(4\) reads like death, so Bob decided to build a live sequence. Living sequence \(a\) contains all natural numbers that do not contain the digit \(4\). \(a = [1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16, \ldots]\).For example, the number \(1235\) is part of the sequence \(a\), but the numbers \(4321\), \(443\) are not part of the sequence \(a\).Bob realized that he does not know how to quickly search for a particular number by the position \(k\) in the sequence, so he asks for your help.For example, if Bob wants to find the number at position \(k = 4\) (indexing from \(1\)), you need to answer \(a_k = 5\).
|
The first line of input data contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.In the only line of each test case, there is a single integer \(k\) (\(1 \le k \le 10^{12}\)) β the position Bob is interested in.
|
For each test case, print on a separate line the number \(a_k\) in indexing with \(1\).
|
Input: 735221010012345827264634912 | Output: 3 6 25 11 121 18937 2932285320890
|
Medium
| 4 | 638 | 241 | 87 | 18 |
|
2,026 |
E
|
2026E
|
E. Best Subsequence
| 2,500 |
bitmasks; dfs and similar; flows; graph matchings; graphs
|
Given an integer array \(a\) of size \(n\).Let's define the value of the array as its size minus the number of set bits in the bitwise OR of all elements of the array.For example, for the array \([1, 0, 1, 2]\), the bitwise OR is \(3\) (which contains \(2\) set bits), and the value of the array is \(4-2=2\).Your task is to calculate the maximum possible value of some subsequence of the given array.
|
The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 100\)).The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i < 2^{60}\)).
|
For each test case, print the maximum possible value of some subsequence of the given array.
|
Input: 430 0 041 0 1 21587 1 48 14 13 8 7 6 | Output: 3 2 0 3
|
Expert
| 5 | 401 | 291 | 92 | 20 |
|
409 |
G
|
409G
|
G. On a plane
| 2,200 |
*special; geometry
|
The first line contains a single integer n (1 β€ n β€ 1000) β the number of points on a plane.Each of the next n lines contains two real coordinates xi and yi of the point, specified with exactly 2 fractional digits. All coordinates are between - 1000 and 1000, inclusive.
|
Output a single real number ΞΈ β the answer to the problem statement. The absolute or relative error of your answer should be at most 10 - 2.
|
Input: 8-2.14 2.06-1.14 2.04-2.16 1.46-2.14 0.70-1.42 0.40-0.94 -0.48-1.42 -1.28-2.16 -1.62 | Output: 5.410
|
Hard
| 2 | 0 | 270 | 140 | 4 |
||
1,534 |
G
|
1534G
|
G. A New Beginning
| 3,300 |
data structures; dp; geometry; sortings
|
Annie has gotten bored of winning every coding contest and farming unlimited rating. Today, she is going to farm potatoes instead.Annie's garden is an infinite 2D plane. She has \(n\) potatoes to plant, and the \(i\)-th potato must be planted at \((x_i,y_i)\). Starting at the point \((0, 0)\), Annie begins walking, in one step she can travel one unit right or up (increasing her \(x\) or \(y\) coordinate by \(1\) respectively). At any point \((X,Y)\) during her walk she can plant some potatoes at arbitrary points using her potato gun, consuming \(\max(|X-x|,|Y-y|)\) units of energy in order to plant a potato at \((x,y)\). Find the minimum total energy required to plant every potato.Note that Annie may plant any number of potatoes from any point.
|
The first line contains the integer \(n\) (\(1 \le n \le 800\,000\)).The next \(n\) lines contain two integers \(x_i\) and \(y_i\) (\(0 \le x_i,y_i \le 10^9\)), representing the location of the \(i\)-th potato. It is possible that some potatoes should be planted in the same location.
|
Print the minimum total energy to plant all potatoes.
|
In example \(1\), Annie can travel to each spot directly and plant a potato with no energy required.In example \(2\), moving to \((1,0)\), Annie plants the second potato using \(1\) energy. Next, she travels to \((1,1)\) and plants the first potato with \(0\) energy.
|
Input: 2 1 1 2 2 | Output: 0
|
Master
| 4 | 754 | 284 | 53 | 15 |
1,017 |
D
|
1017D
|
D. The Wu
| 1,900 |
bitmasks; brute force; data structures
|
Childan is making up a legendary story and trying to sell his forgery β a necklace with a strong sense of ""Wu"" to the Kasouras. But Mr. Kasoura is challenging the truth of Childan's story. So he is going to ask a few questions about Childan's so-called ""personal treasure"" necklace.This ""personal treasure"" is a multiset \(S\) of \(m\) ""01-strings"".A ""01-string"" is a string that contains \(n\) characters ""0"" and ""1"". For example, if \(n=4\), strings ""0110"", ""0000"", and ""1110"" are ""01-strings"", but ""00110"" (there are \(5\) characters, not \(4\)) and ""zero"" (unallowed characters) are not.Note that the multiset \(S\) can contain equal elements.Frequently, Mr. Kasoura will provide a ""01-string"" \(t\) and ask Childan how many strings \(s\) are in the multiset \(S\) such that the ""Wu"" value of the pair \((s, t)\) is not greater than \(k\). Mrs. Kasoura and Mr. Kasoura think that if \(s_i = t_i\) (\(1\leq i\leq n\)) then the ""Wu"" value of the character pair equals to \(w_i\), otherwise \(0\). The ""Wu"" value of the ""01-string"" pair is the sum of the ""Wu"" values of every character pair. Note that the length of every ""01-string"" is equal to \(n\).For example, if \(w=[4, 5, 3, 6]\), ""Wu"" of (""1001"", ""1100"") is \(7\) because these strings have equal characters only on the first and third positions, so \(w_1+w_3=4+3=7\).You need to help Childan to answer Mr. Kasoura's queries. That is to find the number of strings in the multiset \(S\) such that the ""Wu"" value of the pair is not greater than \(k\).
|
The first line contains three integers \(n\), \(m\), and \(q\) (\(1\leq n\leq 12\), \(1\leq q, m\leq 5\cdot 10^5\)) β the length of the ""01-strings"", the size of the multiset \(S\), and the number of queries.The second line contains \(n\) integers \(w_1, w_2, \ldots, w_n\) (\(0 \le w_i \le 100\)) β the value of the \(i\)-th caracter.Each of the next \(m\) lines contains the ""01-string"" \(s\) of length \(n\) β the string in the multiset \(S\).Each of the next \(q\) lines contains the ""01-string"" \(t\) of length \(n\) and integer \(k\) (\(0\leq k\leq 100\)) β the query.
|
For each query, print the answer for this query.
|
In the first example, we can get:""Wu"" of (""01"", ""00"") is \(40\).""Wu"" of (""10"", ""00"") is \(20\).""Wu"" of (""11"", ""00"") is \(0\).""Wu"" of (""01"", ""11"") is \(20\).""Wu"" of (""10"", ""11"") is \(40\).""Wu"" of (""11"", ""11"") is \(60\).In the first query, pairs (""11"", ""00"") and (""10"", ""00"") satisfy the condition since their ""Wu"" is not greater than \(20\).In the second query, all strings satisfy the condition.In the third query, pairs (""01"", ""11"") and (""01"", ""11"") satisfy the condition. Note that since there are two ""01"" strings in the multiset, the answer is \(2\), not \(1\).In the fourth query, since \(k\) was increased, pair (""10"", ""11"") satisfies the condition too.In the fifth query, since \(k\) was increased, pair (""11"", ""11"") satisfies the condition too.
|
Input: 2 4 540 200101101100 2000 4011 2011 4011 60 | Output: 24234
|
Hard
| 3 | 1,556 | 580 | 48 | 10 |
1,867 |
C
|
1867C
|
C. Salyg1n and the MEX Game
| 1,300 |
constructive algorithms; data structures; games; greedy; interactive
|
This is an interactive problem!salyg1n gave Alice a set \(S\) of \(n\) distinct integers \(s_1, s_2, \ldots, s_n\) (\(0 \leq s_i \leq 10^9\)). Alice decided to play a game with this set against Bob. The rules of the game are as follows: Players take turns, with Alice going first. In one move, Alice adds one number \(x\) (\(0 \leq x \leq 10^9\)) to the set \(S\). The set \(S\) must not contain the number \(x\) at the time of the move. In one move, Bob removes one number \(y\) from the set \(S\). The set \(S\) must contain the number \(y\) at the time of the move. Additionally, the number \(y\) must be strictly smaller than the last number added by Alice. The game ends when Bob cannot make a move or after \(2 \cdot n + 1\) moves (in which case Alice's move will be the last one). The result of the game is \(\operatorname{MEX}\dagger(S)\) (\(S\) at the end of the game). Alice aims to maximize the result, while Bob aims to minimize it. Let \(R\) be the result when both players play optimally. In this problem, you play as Alice against the jury program playing as Bob. Your task is to implement a strategy for Alice such that the result of the game is always at least \(R\).\(\dagger\) \(\operatorname{MEX}\) of a set of integers \(c_1, c_2, \ldots, c_k\) is defined as the smallest non-negative integer \(x\) which does not occur in the set \(c\). For example, \(\operatorname{MEX}(\{0, 1, 2, 4\})\) \(=\) \(3\).
|
The first line contains an integer \(t\) (\(1 \leq t \leq 10^5\)) - the number of test cases.
|
In the first test case, the set \(S\) changed as follows:{\(1, 2, 3, 5, 7\)} \(\to\) {\(1, 2, 3, 5, 7, 8\)} \(\to\) {\(1, 2, 3, 5, 8\)} \(\to\) {\(1, 2, 3, 5, 8, 57\)} \(\to\) {\(1, 2, 3, 8, 57\)} \(\to\) {\(0, 1, 2, 3, 8, 57\)}. In the end of the game, \(\operatorname{MEX}(S) = 4\), \(R = 4\).In the second test case, the set \(S\) changed as follows:{\(0, 1, 2\)} \(\to\) {\(0, 1, 2, 3\)} \(\to\) {\(1, 2, 3\)} \(\to\) {\(0, 1, 2, 3\)}. In the end of the game, \(\operatorname{MEX}(S) = 4\), \(R = 4\).In the third test case, the set \(S\) changed as follows:{\(5, 7, 57\)} \(\to\) {\(0, 5, 7, 57\)}. In the end of the game, \(\operatorname{MEX}(S) = 1\), \(R = 1\).
|
Input: 3 5 1 2 3 5 7 7 5 -1 3 0 1 2 0 -1 3 5 7 57 -1 | Output: 8 57 0 3 0 0
|
Easy
| 5 | 1,423 | 93 | 0 | 18 |
|
1,656 |
H
|
1656H
|
H. Equal LCM Subsets
| 3,200 |
data structures; math; number theory
|
You are given two sets of positive integers \(A\) and \(B\). You have to find two non-empty subsets \(S_A \subseteq A\), \(S_B \subseteq B\) so that the least common multiple (LCM) of the elements of \(S_A\) is equal to the least common multiple (LCM) of the elements of \(S_B\).
|
The input consists of multiple test cases. The first line of the input contains one integer \(t\) (\(1 \leq t \leq 200\)), the number of test cases.For each test case, there is one line containing two integers \(n, m\) (\(1 \leq n, m \leq 1000\)), the sizes of the sets \(A\) and \(B\), respectively.The next line contains \(n\) distinct integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 4 \cdot 10^{36}\)), the elements of \(A\).The next line contains \(m\) distinct integers \(b_1, b_2, \ldots, b_m\) (\(1 \leq b_i \leq 4 \cdot 10^{36}\)), the elements of \(B\).The sum of \(n\) for all test cases and the sum of \(m\) for all test cases is at most \(1000\).
|
For each test case, if there do not exist two subsets with equal least common multiple, output one line with NO.Otherwise, output one line with YES, followed by a line with two integers \(|S_A|, |S_B|\) (\(1 \leq |S_A| \leq n\), \(1 \leq |S_B| \leq m\)), the sizes of the subsets \(S_A\) and \(S_B\)The next line should contain \(|S_A|\) integers \(x_1, x_2, \ldots, x_{|S_A|}\), the elements of \(S_A\), followed by a line with \(|S_B|\) integers \(y_1, y_2, \ldots, y_{|S_B|}\), the elements of \(S_B\). If there are multiple possible pairs of subsets, you can print any.
|
Input: 4 3 4 5 6 7 2 8 9 10 4 4 5 6 7 8 2 3 4 9 1 3 1 1 2 3 5 6 3 4 9 7 8 2 15 11 14 20 12 | Output: NO YES 1 2 6 2 3 YES 1 1 1 1 YES 3 2 3 7 4 12 14
|
Master
| 3 | 279 | 664 | 573 | 16 |
|
801 |
A
|
801A
|
A. Vicious Keyboard
| 1,100 |
brute force
|
Tonio has a keyboard with only two letters, ""V"" and ""K"".One day, he has typed out a string s with only these two letters. He really likes it when the string ""VK"" appears, so he wishes to change at most one letter in the string (or do no changes) to maximize the number of occurrences of that string. Compute the maximum number of times ""VK"" can appear as a substring (i. e. a letter ""K"" right after a letter ""V"") in the resulting string.
|
The first line will contain a string s consisting only of uppercase English letters ""V"" and ""K"" with length not less than 1 and not greater than 100.
|
Output a single integer, the maximum number of times ""VK"" can appear as a substring of the given string after changing at most one character.
|
For the first case, we do not change any letters. ""VK"" appears once, which is the maximum number of times it could appear.For the second case, we can change the second character from a ""V"" to a ""K"". This will give us the string ""VK"". This has one occurrence of the string ""VK"" as a substring.For the fourth case, we can change the fourth character from a ""K"" to a ""V"". This will give us the string ""VKKVKKKKKKVVVVVVVVVK"". This has three occurrences of the string ""VK"" as a substring. We can check no other moves can give us strictly more occurrences.
|
Input: VK | Output: 1
|
Easy
| 1 | 449 | 153 | 143 | 8 |
1,976 |
C
|
1976C
|
C. Job Interview
| 1,600 |
binary search; dp; greedy; implementation; two pointers
|
Monocarp is opening his own IT company. He wants to hire \(n\) programmers and \(m\) testers. There are \(n+m+1\) candidates, numbered from \(1\) to \(n+m+1\) in chronological order of their arriving time. The \(i\)-th candidate has programming skill \(a_i\) and testing skill \(b_i\) (a person's programming skill is different from their testing skill). The skill of the team is the sum of the programming skills of all candidates hired as programmers, and the sum of the testing skills of all candidates hired as testers.When a candidate arrives to interview, Monocarp tries to assign them to the most suitable position for them (if their programming skill is higher, then he hires them as a programmer, otherwise as a tester). If all slots for that position are filled, Monocarp assigns them to the other position.Your task is, for each candidate, calculate the skill of the team if everyone except them comes to interview. Note that it means that exactly \(n+m\) candidates will arrive, so all \(n+m\) positions in the company will be filled.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Each test case consists of three lines: the first line contains two integers \(n\) and \(m\) (\(0 \le n, m \le 2 \cdot 10^5\); \(2 \le n + m + 1 \le 2 \cdot 10^5\)) β the number of programmers and the number of testers Monocarp wants to hire, respectively; the second line contains \(n + m + 1\) integers \(a_1, a_2, \dots, a_{n+m+1}\) (\(1 \le a_i \le 10^9\)), where \(a_i\) is the programming skill of the \(i\)-th candidate; the third line contains \(n + m + 1\) integers \(b_1, b_2, \dots, b_{n+m+1}\) (\(1 \le b_i \le 10^9\); \(b_i \ne a_i\)), where \(b_i\) is the testing skill of the \(i\)-th candidate. Additional constraint on the input: the sum of \((n + m + 1)\) over all test cases doesn't exceed \(2 \cdot 10^5\).
|
For each test case, print \(n + m + 1\) integers, where the \(i\)-th integer should be equal to the skill of the team if everyone except the \(i\)-th candidate comes to interview.
|
Let's consider the third test case of the example: if the \(1\)-st candidate does not arrive, the \(2\)-nd candidate gets hired as a tester, the \(3\)-rd candidate gets hired as a programmer, the \(4\)-th candidate gets hired as a tester. The total skill of the team will be \(2 + 5 + 1 = 8\); if the \(2\)-nd candidate does not arrive, the \(1\)-st candidate gets hired as a tester, the \(3\)-rd candidate gets hired as a programmer, the \(4\)-th candidate gets hired as a tester. The total skill of the team will be \(5 + 5 + 1 = 11\); if the \(3\)-rd candidate does not arrive, the \(1\)-st candidate gets hired as a tester, the \(2\)-nd candidate gets hired as a tester, the \(4\)-th candidate gets hired as a programmer. The total skill of the team will be \(5 + 2 + 4 = 11\); if the \(4\)-th candidate does not arrive, the \(1\)-st candidate gets hired as a tester, the \(2\)-nd candidate gets hired as a tester, the \(3\)-rd candidate gets hired as a programmer. The total skill of the team will be \(5 + 2 + 5 = 12\).
|
Input: 41 02 11 20 24 5 55 4 11 22 1 5 45 2 3 13 14 3 3 4 15 5 4 5 2 | Output: 1 2 5 6 9 8 11 11 12 13 13 13 12 15
|
Medium
| 5 | 1,046 | 823 | 179 | 19 |
1,220 |
F
|
1220F
|
F. Gardener Alex
| 2,700 |
binary search; data structures
|
Gardener Alex loves to grow trees. We remind that tree is a connected acyclic graph on \(n\) vertices. Today he decided to grow a rooted binary tree. A binary tree is a tree where any vertex has no more than two sons. Luckily, Alex has a permutation of numbers from \(1\) to \(n\) which he was presented at his last birthday, so he decided to grow a tree according to this permutation. To do so he does the following process: he finds a minimum element and makes it a root of the tree. After that permutation is divided into two parts: everything that is to the left of the minimum element, and everything that is to the right. The minimum element on the left part becomes the left son of the root, and the minimum element on the right part becomes the right son of the root. After that, this process is repeated recursively on both parts.Now Alex wants to grow a forest of trees: one tree for each cyclic shift of the permutation. He is interested in what cyclic shift gives the tree of minimum depth. Unfortunately, growing a forest is a hard and long process, but Alex wants the answer right now. Will you help him?We remind that cyclic shift of permutation \(a_1, a_2, \ldots, a_k, \ldots, a_n\) for \(k\) elements to the left is the permutation \(a_{k + 1}, a_{k + 2}, \ldots, a_n, a_1, a_2, \ldots, a_k\).
|
First line contains an integer number \(n ~ (1 \leqslant n \leqslant 200\,000)\) β length of the permutation.Second line contains \(n\) integer numbers \(a_1, a_2, \ldots, a_n ~ (1 \leqslant a_i \leqslant n)\), and it is guaranteed that all numbers occur exactly one time.
|
Print two numbers separated with space: minimum possible depth of a tree and how many elements we need to shift left to achieve this depth. The number of elements should be a number from \(0\) to \(n - 1\). If there are several possible answers, print any of them.
|
The following picture depicts all possible trees for sample test and cyclic shifts on which they are achieved.
|
Input: 4 1 2 3 4 | Output: 3 3
|
Master
| 2 | 1,311 | 272 | 264 | 12 |
1,846 |
G
|
1846G
|
G. Rudolf and CodeVid-23
| 1,900 |
bitmasks; dp; graphs; greedy; shortest paths
|
A new virus called ""CodeVid-23"" has spread among programmers. Rudolf, being a programmer, was not able to avoid it.There are \(n\) symptoms numbered from \(1\) to \(n\) that can appear when infected. Initially, Rudolf has some of them. He went to the pharmacy and bought \(m\) medicines.For each medicine, the number of days it needs to be taken is known, and the set of symptoms it removes. Unfortunately, medicines often have side effects. Therefore, for each medicine, the set of symptoms that appear when taking it is also known.After reading the instructions, Rudolf realized that taking more than one medicine at a time is very unhealthy.Rudolph wants to be healed as soon as possible. Therefore, he asks you to calculate the minimum number of days to remove all symptoms, or to say that it is impossible.
|
The first line contains a single integer \(t\) \((1 \le t \le 100)\) β the number of test cases.Then follow the descriptions of the test cases.The first line of each test case contains two integers \(n, m\) \((1 \le n \le 10, 1 \le m \le 10^3)\) β the number of symptoms and medicines, respectively.The second line of each test case contains a string of length \(n\) consisting of the characters \(0\) and \(1\) β the description of Rudolf's symptoms. If the \(i\)-th character of the string is \(1\), Rudolf has the \(i\)-th symptom, otherwise he does not.Then follow \(3 \cdot m\) lines β the description of the medicines.The first line of each medicine description contains an integer \(d\) \((1 \le d \le 10^3)\) β the number of days the medicine needs to be taken.The next two lines of the medicine description contain two strings of length \(n\), consisting of the characters \(0\) and \(1\) β the description of the symptoms it removes and the description of the side effects.In the first of the two lines, \(1\) at position \(i\) means that the medicine removes the \(i\)-th symptom, and \(0\) otherwise.In the second of the two lines, \(1\) at position \(i\) means that the \(i\)-th symptom appears after taking the medicine, and \(0\) otherwise.Different medicines can have the same sets of symptoms and side effects. If a medicine relieves a certain symptom, it will not be among the side effects.The sum of \(m\) over all test cases does not exceed \(10^3\).
|
For each test case, output a single integer on a separate line β the minimum number of days it will take Rudolf to remove all symptoms. If this never happens, output \(-1\).
|
In the first example, we can first apply medicine number \(4\), after which the symptoms will look like ""00101"". After that, medicine number \(2\), then all symptoms will disappear, and the number of days will be \(5 + 3 = 8\). Another option is to apply the medicines in the order \(1, 3, 2\). In this case, all symptoms will also disappear, but the number of days will be \(3 + 3 + 3 = 9\).In the second example, there are no symptoms initially, so the treatment will take \(0\) days.In the third example, there are no options to remove all symptoms.
|
Input: 45 410011310000001103001010000030101000100511010001004 1000010101101002 21121001301102 311301103100041001 | Output: 8 0 -1 6
|
Hard
| 5 | 813 | 1,470 | 173 | 18 |
1,234 |
D
|
1234D
|
D. Distinct Characters Queries
| 1,600 |
data structures
|
You are given a string \(s\) consisting of lowercase Latin letters and \(q\) queries for this string.Recall that the substring \(s[l; r]\) of the string \(s\) is the string \(s_l s_{l + 1} \dots s_r\). For example, the substrings of ""codeforces"" are ""code"", ""force"", ""f"", ""for"", but not ""coder"" and ""top"".There are two types of queries: \(1~ pos~ c\) (\(1 \le pos \le |s|\), \(c\) is lowercase Latin letter): replace \(s_{pos}\) with \(c\) (set \(s_{pos} := c\)); \(2~ l~ r\) (\(1 \le l \le r \le |s|\)): calculate the number of distinct characters in the substring \(s[l; r]\).
|
The first line of the input contains one string \(s\) consisting of no more than \(10^5\) lowercase Latin letters.The second line of the input contains one integer \(q\) (\(1 \le q \le 10^5\)) β the number of queries.The next \(q\) lines contain queries, one per line. Each query is given in the format described in the problem statement. It is guaranteed that there is at least one query of the second type.
|
For each query of the second type print the answer for it β the number of distinct characters in the required substring in this query.
|
Input: abacaba 5 2 1 4 1 4 b 1 5 b 2 4 6 2 1 7 | Output: 3 1 2
|
Medium
| 1 | 592 | 408 | 134 | 12 |
|
887 |
C
|
887C
|
C. Solution for Cube
| 1,500 |
brute force; implementation
|
During the breaks between competitions, top-model Izabella tries to develop herself and not to be bored. For example, now she tries to solve Rubik's cube 2x2x2.It's too hard to learn to solve Rubik's cube instantly, so she learns to understand if it's possible to solve the cube in some state using 90-degrees rotation of one face of the cube in any direction.To check her answers she wants to use a program which will for some state of cube tell if it's possible to solve it using one rotation, described above.Cube is called solved if for each face of cube all squares on it has the same color.https://en.wikipedia.org/wiki/Rubik's_Cube
|
In first line given a sequence of 24 integers ai (1 β€ ai β€ 6), where ai denotes color of i-th square. There are exactly 4 occurrences of all colors in this sequence.
|
Print Β«YESΒ» (without quotes) if it's possible to solve cube using one rotation and Β«NOΒ» (without quotes) otherwise.
|
In first test case cube looks like this: In second test case cube looks like this: It's possible to solve cube by rotating face with squares with numbers 13, 14, 15, 16.
|
Input: 2 5 4 6 1 3 6 2 5 5 1 2 3 5 3 1 1 2 4 6 6 4 3 4 | Output: NO
|
Medium
| 2 | 638 | 165 | 115 | 8 |
1,244 |
A
|
1244A
|
A. Pens and Pencils
| 800 |
math
|
Tomorrow is a difficult day for Polycarp: he has to attend \(a\) lectures and \(b\) practical classes at the university! Since Polycarp is a diligent student, he is going to attend all of them.While preparing for the university, Polycarp wonders whether he can take enough writing implements to write all of the lectures and draw everything he has to during all of the practical classes. Polycarp writes lectures using a pen (he can't use a pencil to write lectures!); he can write down \(c\) lectures using one pen, and after that it runs out of ink. During practical classes Polycarp draws blueprints with a pencil (he can't use a pen to draw blueprints!); one pencil is enough to draw all blueprints during \(d\) practical classes, after which it is unusable.Polycarp's pencilcase can hold no more than \(k\) writing implements, so if Polycarp wants to take \(x\) pens and \(y\) pencils, they will fit in the pencilcase if and only if \(x + y \le k\).Now Polycarp wants to know how many pens and pencils should he take. Help him to determine it, or tell that his pencilcase doesn't have enough room for all the implements he needs tomorrow!Note that you don't have to minimize the number of writing implements (though their total number must not exceed \(k\)).
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 100\)) β the number of test cases in the input. Then the test cases follow.Each test case is described by one line containing five integers \(a\), \(b\), \(c\), \(d\) and \(k\), separated by spaces (\(1 \le a, b, c, d, k \le 100\)) β the number of lectures Polycarp has to attend, the number of practical classes Polycarp has to attend, the number of lectures which can be written down using one pen, the number of practical classes for which one pencil is enough, and the number of writing implements that can fit into Polycarp's pencilcase, respectively.In hacks it is allowed to use only one test case in the input, so \(t = 1\) should be satisfied.
|
For each test case, print the answer as follows:If the pencilcase can't hold enough writing implements to use them during all lectures and practical classes, print one integer \(-1\). Otherwise, print two non-negative integers \(x\) and \(y\) β the number of pens and pencils Polycarp should put in his pencilcase. If there are multiple answers, print any of them. Note that you don't have to minimize the number of writing implements (though their total number must not exceed \(k\)).
|
There are many different answers for the first test case; \(x = 7\), \(y = 1\) is only one of them. For example, \(x = 3\), \(y = 1\) is also correct.\(x = 1\), \(y = 3\) is the only correct answer for the third test case.
|
Input: 3 7 5 4 5 8 7 5 4 5 2 20 53 45 26 4 | Output: 7 1 -1 1 3
|
Beginner
| 1 | 1,263 | 722 | 485 | 12 |
96 |
A
|
96A
|
A. Football
| 900 |
implementation; strings
|
Petya loves football very much. One day, as he was watching a football match, he was writing the players' current positions on a piece of paper. To simplify the situation he depicted it as a string consisting of zeroes and ones. A zero corresponds to players of one team; a one corresponds to players of another team. If there are at least 7 players of some team standing one after another, then the situation is considered dangerous. For example, the situation 00100110111111101 is dangerous and 11110111011101 is not. You are given the current situation. Determine whether it is dangerous or not.
|
The first input line contains a non-empty string consisting of characters ""0"" and ""1"", which represents players. The length of the string does not exceed 100 characters. There's at least one player from each team present on the field.
|
Print ""YES"" if the situation is dangerous. Otherwise, print ""NO"".
|
Input: 001001 | Output: NO
|
Beginner
| 2 | 598 | 238 | 69 | 0 |
|
470 |
B
|
470B
|
B. Hexakosioihexekontahexaphobia
| 1,800 |
*special
|
This (unpronounceable) word means simply fear of number 666. You are given a string of digits. Check whether it is scary for a person suffering from this phobia, i.e., whether it contains number 666 as a substring.
|
The input will consist of a single string p. The string contains between 1 and 100 digits ('0'-'9'), inclusive. The string doesn't contain any other characters except digits.
|
Output ""YES"" if given string contains number 666, and ""NO"" otherwise (quotes for clarity only).
|
Note that 666 must be a contiguous substring of p, not a subsequence (see sample 3).
|
Input: 123098 | Output: NO
|
Medium
| 1 | 214 | 174 | 99 | 4 |
877 |
D
|
877D
|
D. Olya and Energy Drinks
| 2,100 |
data structures; dfs and similar; graphs; shortest paths
|
Olya loves energy drinks. She loves them so much that her room is full of empty cans from energy drinks.Formally, her room can be represented as a field of n Γ m cells, each cell of which is empty or littered with cans.Olya drank a lot of energy drink, so now she can run k meters per second. Each second she chooses one of the four directions (up, down, left or right) and runs from 1 to k meters in this direction. Of course, she can only run through empty cells.Now Olya needs to get from cell (x1, y1) to cell (x2, y2). How many seconds will it take her if she moves optimally?It's guaranteed that cells (x1, y1) and (x2, y2) are empty. These cells can coincide.
|
The first line contains three integers n, m and k (1 β€ n, m, k β€ 1000) β the sizes of the room and Olya's speed.Then n lines follow containing m characters each, the i-th of them contains on j-th position ""#"", if the cell (i, j) is littered with cans, and ""."" otherwise.The last line contains four integers x1, y1, x2, y2 (1 β€ x1, x2 β€ n, 1 β€ y1, y2 β€ m) β the coordinates of the first and the last cells.
|
Print a single integer β the minimum time it will take Olya to get from (x1, y1) to (x2, y2).If it's impossible to get from (x1, y1) to (x2, y2), print -1.
|
In the first sample Olya should run 3 meters to the right in the first second, 2 meters down in the second second and 3 meters to the left in the third second.In second sample Olya should run to the right for 3 seconds, then down for 2 seconds and then to the left for 3 seconds.Olya does not recommend drinking energy drinks and generally believes that this is bad.
|
Input: 3 4 4....###.....1 1 3 1 | Output: 3
|
Hard
| 4 | 666 | 409 | 155 | 8 |
2,044 |
F
|
2044F
|
F. Easy Demon Problem
| 1,900 |
binary search; brute force; data structures; math; number theory
|
For an arbitrary grid, Robot defines its beauty to be the sum of elements in the grid. Robot gives you an array \(a\) of length \(n\) and an array \(b\) of length \(m\). You construct a \(n\) by \(m\) grid \(M\) such that \(M_{i,j}=a_i\cdot b_j\) for all \(1 \leq i \leq n\) and \(1 \leq j \leq m\).Then, Robot gives you \(q\) queries, each consisting of a single integer \(x\). For each query, determine whether or not it is possible to perform the following operation exactly once so that \(M\) has a beauty of \(x\): Choose integers \(r\) and \(c\) such that \(1 \leq r \leq n\) and \(1 \leq c \leq m\) Set \(M_{i,j}\) to be \(0\) for all ordered pairs \((i,j)\) such that \(i=r\), \(j=c\), or both. Note that queries are not persistent, meaning that you do not actually set any elements to \(0\) in the process β you are only required to output if it is possible to find \(r\) and \(c\) such that if the above operation is performed, the beauty of the grid will be \(x\). Also, note that you must perform the operation for each query, even if the beauty of the original grid is already \(x\).
|
The first line contains three integers \(n\), \(m\), and \(q\) (\(1 \leq n,m \leq 2\cdot 10^5, 1 \leq q \leq 5\cdot 10^4\)) β the length of \(a\), the length of \(b\), and the number of queries respectively.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq |a_i| \leq n\)).The third line contains \(m\) integers \(b_1, b_2, \ldots, b_m\) (\(0 \leq |b_i| \leq m\)).The following \(q\) lines each contain a single integer \(x\) (\(1 \leq |x| \leq 2\cdot 10^5\)), the beauty of the grid you wish to achieve by setting all elements in a row and a column to \(0\).
|
For each testcase, output ""YES"" (without quotes) if there is a way to perform the aforementioned operation such that the beauty is \(x\), and ""NO"" (without quotes) otherwise.You can output ""YES"" and ""NO"" in any case (for example, strings ""yES"", ""yes"" and ""Yes"" will be recognized as a positive response).
|
In the second example, the grid is 0 -2 5 0 -30 4 -10 0 60 -6 15 0 -90 0 0 0 00 0 0 0 0By performing the operation with \(r=4\) and \(c=2\), we create the following grid:0 0 5 0 -30 0 -10 0 60 0 15 0 -90 0 0 0 00 0 0 0 0which has beauty \(4\). Thus, we output YES.In the second query, selecting \(r=3\) and \(c=5\) creates a grid with beauty \(-3\).In the third query, selecting \(r=3\) and \(c=3\) creates a grid with beauty \(5\).
|
Input: 3 3 6-2 3 -3-2 2 -1-11-22-33 | Output: NO YES NO NO YES NO
|
Hard
| 5 | 1,096 | 585 | 318 | 20 |
339 |
E
|
339E
|
E. Three Swaps
| 2,700 |
constructive algorithms; dfs and similar; greedy
|
Xenia the horse breeder has n (n > 1) horses that stand in a row. Each horse has its own unique number. Initially, the i-th left horse has number i. That is, the sequence of numbers of horses in a row looks as follows (from left to right): 1, 2, 3, ..., n.Xenia trains horses before the performance. During the practice sessions, she consistently gives them commands. Each command is a pair of numbers l, r (1 β€ l < r β€ n). The command l, r means that the horses that are on the l-th, (l + 1)-th, (l + 2)-th, ..., r-th places from the left must be rearranged. The horses that initially stand on the l-th and r-th places will swap. The horses on the (l + 1)-th and (r - 1)-th places will swap. The horses on the (l + 2)-th and (r - 2)-th places will swap and so on. In other words, the horses that were on the segment [l, r] change their order to the reverse one.For example, if Xenia commanded l = 2, r = 5, and the sequence of numbers of horses before the command looked as (2, 1, 3, 4, 5, 6), then after the command the sequence will be (2, 5, 4, 3, 1, 6).We know that during the practice Xenia gave at most three commands of the described form. You have got the final sequence of numbers of horses by the end of the practice. Find what commands Xenia gave during the practice. Note that you do not need to minimize the number of commands in the solution, find any valid sequence of at most three commands.
|
The first line contains an integer n (2 β€ n β€ 1000) β the number of horses in the row. The second line contains n distinct integers a1, a2, ..., an (1 β€ ai β€ n), where ai is the number of the i-th left horse in the row after the practice.
|
The first line should contain integer k (0 β€ k β€ 3) β the number of commads Xenia gave during the practice. In each of the next k lines print two integers. In the i-th line print numbers li, ri (1 β€ li < ri β€ n) β Xenia's i-th command during the practice.It is guaranteed that a solution exists. If there are several solutions, you are allowed to print any of them.
|
Input: 51 4 3 2 5 | Output: 12 4
|
Master
| 3 | 1,408 | 238 | 365 | 3 |
|
1,157 |
B
|
1157B
|
B. Long Number
| 1,300 |
greedy
|
You are given a long decimal number \(a\) consisting of \(n\) digits from \(1\) to \(9\). You also have a function \(f\) that maps every digit from \(1\) to \(9\) to some (possibly the same) digit from \(1\) to \(9\).You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in \(a\), and replace each digit \(x\) from this segment with \(f(x)\). For example, if \(a = 1337\), \(f(1) = 1\), \(f(3) = 5\), \(f(7) = 3\), and you choose the segment consisting of three rightmost digits, you get \(1553\) as the result.What is the maximum possible number you can obtain applying this operation no more than once?
|
The first line contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of digits in \(a\).The second line contains a string of \(n\) characters, denoting the number \(a\). Each character is a decimal digit from \(1\) to \(9\).The third line contains exactly \(9\) integers \(f(1)\), \(f(2)\), ..., \(f(9)\) (\(1 \le f(i) \le 9\)).
|
Print the maximum number you can get after applying the operation described in the statement no more than once.
|
Input: 4 1337 1 2 5 4 6 6 3 1 9 | Output: 1557
|
Easy
| 1 | 660 | 345 | 111 | 11 |
|
1,005 |
A
|
1005A
|
A. Tanya and Stairways
| 800 |
implementation
|
Little girl Tanya climbs the stairs inside a multi-storey building. Every time Tanya climbs a stairway, she starts counting steps from \(1\) to the number of steps in this stairway. She speaks every number aloud. For example, if she climbs two stairways, the first of which contains \(3\) steps, and the second contains \(4\) steps, she will pronounce the numbers \(1, 2, 3, 1, 2, 3, 4\).You are given all the numbers pronounced by Tanya. How many stairways did she climb? Also, output the number of steps in each stairway.The given sequence will be a valid sequence that Tanya could have pronounced when climbing one or more stairways.
|
The first line contains \(n\) (\(1 \le n \le 1000\)) β the total number of numbers pronounced by Tanya.The second line contains integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 1000\)) β all the numbers Tanya pronounced while climbing the stairs, in order from the first to the last pronounced number. Passing a stairway with \(x\) steps, she will pronounce the numbers \(1, 2, \dots, x\) in that order.The given sequence will be a valid sequence that Tanya could have pronounced when climbing one or more stairways.
|
In the first line, output \(t\) β the number of stairways that Tanya climbed. In the second line, output \(t\) numbers β the number of steps in each stairway she climbed. Write the numbers in the correct order of passage of the stairways.
|
Input: 71 2 3 1 2 3 4 | Output: 23 4
|
Beginner
| 1 | 636 | 518 | 238 | 10 |
|
48 |
D
|
48D
|
D. Permutations
| 1,500 |
greedy
|
A permutation is a sequence of integers from 1 to n of length n containing each number exactly once. For example, (1), (4, 3, 5, 1, 2), (3, 2, 1) are permutations, and (1, 1), (4, 3, 1), (2, 3, 4) are not. There are many tasks on permutations. Today you are going to solve one of them. Letβs imagine that somebody took several permutations (perhaps, with a different number of elements), wrote them down consecutively as one array and then shuffled the resulting array. The task is to restore the initial permutations if it is possible.
|
The first line contains an integer n (1 β€ n β€ 105). The next line contains the mixed array of n integers, divided with a single space. The numbers in the array are from 1 to 105.
|
If this array can be split into several permutations so that every element of the array belongs to exactly one permutation, print in the first line the number of permutations. The second line should contain n numbers, corresponding to the elements of the given array. If the i-th element belongs to the first permutation, the i-th number should be 1, if it belongs to the second one, then its number should be 2 and so on. The order of the permutationsβ numbering is free.If several solutions are possible, print any one of them. If thereβs no solution, print in the first line - 1.
|
In the first sample test the array is split into three permutations: (2, 1), (3, 2, 1, 4, 5), (1, 2). The first permutation is formed by the second and the fourth elements of the array, the second one β by the third, the fifth, the sixth, the seventh and the ninth elements, the third one β by the first and the eigth elements. Clearly, there are other splitting variants possible.
|
Input: 91 2 3 1 2 1 4 2 5 | Output: 33 1 2 1 2 2 2 3 2
|
Medium
| 1 | 536 | 178 | 582 | 0 |
152 |
A
|
152A
|
A. Marks
| 900 |
implementation
|
Vasya, or Mr. Vasily Petrov is a dean of a department in a local university. After the winter exams he got his hands on a group's gradebook.Overall the group has n students. They received marks for m subjects. Each student got a mark from 1 to 9 (inclusive) for each subject.Let's consider a student the best at some subject, if there is no student who got a higher mark for this subject. Let's consider a student successful, if there exists a subject he is the best at.Your task is to find the number of successful students in the group.
|
The first input line contains two integers n and m (1 β€ n, m β€ 100) β the number of students and the number of subjects, correspondingly. Next n lines each containing m characters describe the gradebook. Each character in the gradebook is a number from 1 to 9. Note that the marks in a rows are not sepatated by spaces.
|
Print the single number β the number of successful students in the given group.
|
In the first sample test the student number 1 is the best at subjects 1 and 3, student 2 is the best at subjects 1 and 2, but student 3 isn't the best at any subject.In the second sample test each student is the best at at least one subject.
|
Input: 3 3223232112 | Output: 2
|
Beginner
| 1 | 538 | 319 | 79 | 1 |
88 |
A
|
88A
|
A. Chord
| 1,200 |
brute force; implementation
|
Vasya studies music. He has learned lots of interesting stuff. For example, he knows that there are 12 notes: C, C#, D, D#, E, F, F#, G, G#, A, B, H. He also knows that the notes are repeated cyclically: after H goes C again, and before C stands H. We will consider the C note in the row's beginning and the C note after the H similar and we will identify them with each other. The distance between the notes along the musical scale is measured in tones: between two consecutive notes there's exactly one semitone, that is, 0.5 tone. The distance is taken from the lowest tone to the uppest one, that is, the distance between C and E is 4 semitones and between E and C is 8 semitonesVasya also knows what a chord is. A chord is an unordered set of no less than three notes. However, for now Vasya only works with triads, that is with the chords that consist of exactly three notes. He can already distinguish between two types of triads β major and minor.Let's define a major triad. Let the triad consist of notes X, Y and Z. If we can order the notes so as the distance along the musical scale between X and Y equals 4 semitones and the distance between Y and Z is 3 semitones, then the triad is major. The distance between X and Z, accordingly, equals 7 semitones.A minor triad is different in that the distance between X and Y should be 3 semitones and between Y and Z β 4 semitones.For example, the triad ""C E G"" is major: between C and E are 4 semitones, and between E and G are 3 semitones. And the triplet ""C# B F"" is minor, because if we order the notes as ""B C# F"", than between B and C# will be 3 semitones, and between C# and F β 4 semitones.Help Vasya classify the triad the teacher has given to him.
|
The only line contains 3 space-separated notes in the above-given notation.
|
Print ""major"" if the chord is major, ""minor"" if it is minor, and ""strange"" if the teacher gave Vasya some weird chord which is neither major nor minor. Vasya promises you that the answer will always be unambiguous. That is, there are no chords that are both major and minor simultaneously.
|
Input: C E G | Output: major
|
Easy
| 2 | 1,718 | 75 | 295 | 0 |
|
735 |
B
|
735B
|
B. Urbanization
| 1,100 |
greedy; number theory; sortings
|
Local authorities have heard a lot about combinatorial abilities of Ostap Bender so they decided to ask his help in the question of urbanization. There are n people who plan to move to the cities. The wealth of the i of them is equal to ai. Authorities plan to build two cities, first for n1 people and second for n2 people. Of course, each of n candidates can settle in only one of the cities. Thus, first some subset of candidates of size n1 settle in the first city and then some subset of size n2 is chosen among the remaining candidates and the move to the second city. All other candidates receive an official refuse and go back home.To make the statistic of local region look better in the eyes of their bosses, local authorities decided to pick subsets of candidates in such a way that the sum of arithmetic mean of wealth of people in each of the cities is as large as possible. Arithmetic mean of wealth in one city is the sum of wealth ai among all its residents divided by the number of them (n1 or n2 depending on the city). The division should be done in real numbers without any rounding.Please, help authorities find the optimal way to pick residents for two cities.
|
The first line of the input contains three integers n, n1 and n2 (1 β€ n, n1, n2 β€ 100 000, n1 + n2 β€ n) β the number of candidates who want to move to the cities, the planned number of residents of the first city and the planned number of residents of the second city.The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 100 000), the i-th of them is equal to the wealth of the i-th candidate.
|
Print one real value β the maximum possible sum of arithmetic means of wealth of cities' residents. You answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .
|
In the first sample, one of the optimal solutions is to move candidate 1 to the first city and candidate 2 to the second.In the second sample, the optimal solution is to pick candidates 3 and 4 for the first city, and candidate 2 for the second one. Thus we obtain (a3 + a4) / 2 + a2 = (3 + 2) / 2 + 4 = 6.5
|
Input: 2 1 11 5 | Output: 6.00000000
|
Easy
| 3 | 1,182 | 402 | 332 | 7 |
822 |
F
|
822F
|
F. Madness
| 2,500 |
constructive algorithms; dfs and similar; trees
|
The second semester starts at the University of Pavlopolis. After vacation in ViΔkopolis Noora needs to return to Pavlopolis and continue her study.Sometimes (or quite often) there are teachers who do not like you. Incidentally Noora also has one such teacher. His name is Yury Dmitrievich and he teaches graph theory. Yury Dmitrievich doesn't like Noora, so he always gives the girl the most difficult tasks. So it happened this time.The teacher gives Noora a tree with n vertices. Vertices are numbered with integers from 1 to n. The length of all the edges of this tree is 1. Noora chooses a set of simple paths that pairwise don't intersect in edges. However each vertex should belong to at least one of the selected path.For each of the selected paths, the following is done: We choose exactly one edge (u, v) that belongs to the path. On the selected edge (u, v) there is a point at some selected distance x from the vertex u and at distance 1 - x from vertex v. But the distance x chosen by Noora arbitrarily, i. e. it can be different for different edges. One of the vertices u or v is selected. The point will start moving to the selected vertex. Let us explain how the point moves by example. Suppose that the path consists of two edges (v1, v2) and (v2, v3), the point initially stands on the edge (v1, v2) and begins its movement to the vertex v1. Then the point will reach v1, then ""turn around"", because the end of the path was reached, further it will move in another direction to vertex v2, then to vertex v3, then ""turn around"" again, then move to v2 and so on. The speed of the points is 1 edge per second. For example, for 0.5 second the point moves to the length of the half of an edge.A stopwatch is placed at each vertex of the tree. The time that the stopwatches indicate at start time is 0 seconds. Then at the starting moment of time, all points simultaneously start moving from the selected positions to selected directions along the selected paths, and stopwatches are simultaneously started. When one of the points reaches the vertex v, the stopwatch at the vertex v is automatically reset, i.e. it starts counting the time from zero.Denote by resv the maximal time that the stopwatch at the vertex v will show if the point movement continues infinitely. Noora is asked to select paths and points on them so that res1 is as minimal as possible. If there are several solutions to do this, it is necessary to minimize res2, then res3, res4, ..., resn.Help Noora complete the teacher's task.For the better understanding of the statement, see the explanation for the example.
|
The first line contains single integer n (2 β€ n β€ 100) β number of vertices in the given tree.Each of next n - 1 lines contains two integers u and v (1 β€ u, v β€ n, u β v) β vertices connected by an edge.Guaranteed that input defines a valid tree.
|
In the first line print single integer paths β number of paths you want to choose.In the next paths lines print path's descriptions: Single integer len β number of edges in the current path. len integers β indices of the edges in the path. The edges are numbered from 1 to n - 1 in order they are given in input. Two integers u and v β means that you put point on the edge between vertices u and v (obviously the edge should belong to the path) and a point will start moving to the vertex v. Note that order of printing of the edge's ends is important. For example if you print ""1 2"" (without quotes), then point will start moving to vertex 2; but if you print ""2 1"" (without quotes), then point will start moving to vertex 1. Single real number x (0 β€ x β€ 1) β distance between point and vertex u (the same vertex that you print first in the third paragraph).
|
Consider an example.In starting moment of time points are located as following:The first path is highlighted in red, the second in blue, green circles represent chosen points, and brown numbers inside vertices β current time at stopwatch. Purple arrows represent direction in which points will move.In 0.(3) seconds points will be located in following way (before stopwatch reset):After stopwatch reset:In 1.0 second after the start of moving:In 1.(3) seconds after the start of moving (after stopwatch reset):Finally, in 2 seconds after the start of moving points return to their initial positions.This process will continue infinitely.
|
Input: 31 22 3 | Output: 21 1 1 2 0.66666666661 2 2 3 0.6666666666
|
Expert
| 3 | 2,603 | 246 | 864 | 8 |
2,127 |
B
|
2127B
|
B. Hamiiid, Haaamid... Hamid?
| 1,300 |
games; greedy
|
Mani has locked Hamid in a \(1 \times n\) grid. Initially, some cells of the grid contain walls and the rest are empty, and Hamid is in an empty cell.In each day, the following events happen in order: Mani selects an empty cell and builds a wall in that cell. Note that he can not build a wall in the cell which Hamid currently is at; Hamid selects a direction (left or right), then If there are no walls in that direction, he will escape the grid; Otherwise, he will move to the nearest wall in that direction and destroy that wall. Hamid is at the position of the destroyed wall after this day.Here is an example of a possible sequence of actions when \(n=6\): Hamid is always aware of where the walls are. He wants to minimize the number of days that he needs to escape the grid, while Mani wants to maximize it.You have to determine the number of days Hamid needs to escape the grid if they both act optimally.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains two integers \(n\) and \(x\) (\(2 \leq n \leq 2 \cdot 10^5\), \(1 \leq x \leq n\)) β the size of the grid and the initial position of Hamid. He is at the \(x\)-th cell from left to right initially.The second line contains a string \(s\) of length \(n\) (\(s_i=\texttt{""#""}\) or \(\texttt{"".""}\)) β the initial state of the grid. The \(i\)-th cell of the grid contains a wall if \(s_i= \texttt{""#""}\), and it is empty if \(s_i=\texttt{"".""}\).It is guaranteed that the \(x\)-th cell is empty, and there are at least two empty cells in the grid.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output a single integer β the number of days Hamid needs to escape the grid if they both act optimally.
|
In the first test case, Mani must build a wall in cell \(2\), so Hamid can escape from the left side of the grid on the first day.In the second test case, if Mani places the wall to the left of Hamid, Hamid can escape from the right. And if the wall is to Hamid's right, he can escape from the left. Thus, the answer is \(1\).In the third test case: It can be shown that both players acted optimally in the above illustration.In the fourth test case, we have shown an example of actions in the statements. Note that the players did not act optimally in this example.
|
Input: 43 1..#4 2....5 3##..#6 4#...#. | Output: 1 1 3 3
|
Easy
| 2 | 914 | 846 | 123 | 21 |
2,109 |
D
|
2109D
|
D. D/D/D
| 1,900 |
dfs and similar; graphs; greedy; shortest paths
|
Of course, a problem with the letter D is sponsored by Declan Akaba.You are given a simple, connected, undirected graph with \(n\) vertices and \(m\) edges. The graph contains no self-loops or multiple edges. You are also given a multiset \(A\) consisting of \(\ell\) elements: $$$\( A = \{A_1, A_2, \ldots, A_\ell\} \)\(Starting from vertex \)1\(, you may perform the following move any number of times, as long as the multiset \)A\( is not empty: Select an element \)k \in A\( and remove it from the multiset . You must remove exactly one occurrence of \)k\( from \)A\(. Traverse any walk\)^{\text{β}}\( of exactly \)k\( edges to reach some vertex (possibly the same one you started from). For each \)i\( (\)1 \le i \le n\(), determine whether there exists a sequence of such moves that starts at vertex \)1\( and ends at vertex \)i\(, using the original multiset \)A\(. Note that the check for each vertex \)i\( is independent β you restart from vertex \)1\( and use the original multiset \)A\( for each case.\)^{\text{β}}\(A walk of length \)k\( is a sequence of vertices \)v_0, v_1, \ldots, v_{k - 1}, v_k\( such that each consecutive pair of vertices \)(v_i, v_{i + 1})$$$ is connected by an edge in the graph. The sequence may include repeated vertices.
|
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 three integers \(n\), \(m\), and \(\ell\) (\(2 \leq n \leq 2 \cdot 10^5\), \(n-1 \leq m \leq 4 \cdot 10^5\), \(1 \leq \ell \leq 2 \cdot 10^5\)) β the number of vertices, the number of edges, and the size of the multiset, respectively.The second line of each test case contains \(\ell\) integers \(A_1, A_2, \ldots, A_{\ell}\) (\(1 \leq A_i \leq 10^4\)) β the elements of the multiset.Each of the following \(m\) lines contains two integers \(u\) and \(v\) (\(1 \le u < v \le n\)) β the endpoints of an edge in the graph. It is guaranteed that the edges form a simple, connected graph without self-loops or multiple edges.It is guaranteed that the sum of \(n\), the sum of \(m\), and the sum of \(\ell\) over all test cases does not exceed \(2 \cdot 10^5\), \(4 \cdot 10^5\), and \(2 \cdot 10^5\), respectively.
|
For each test case, output a binary string of length \(n\), where the \(i\)-th character is \(\mathtt{1}\) if there exists a sequence of moves ending at vertex \(i\), and \(\mathtt{0}\) otherwise.
|
In the first test case: Vertex \(1\) is reachable without making any moves. Vertex \(2\) is reachable by selecting element \(3 \in A\); one possible walk is [\(1 \rightarrow 2 \rightarrow 1 \rightarrow 2\)]. Vertex \(3\) can be reached by selecting element \(2 \in A\) and taking the walk [\(1 \rightarrow 2 \rightarrow 3\)]. Vertex \(4\) is reachable by selecting element \(3 \in A\) and following the walk [\(1 \rightarrow 2 \rightarrow 3 \rightarrow 4\)]. Vertex \(5\) is not reachable by any valid sequence of moves. Vertex \(6\) is reachable by first selecting element \(2 \in A\) and taking the walk [\(1 \rightarrow 2 \rightarrow 3\)], followed by selecting element \(3 \in A\) and taking the walk [\(3 \rightarrow 4 \rightarrow 5 \rightarrow 6\)].
|
Input: 36 5 22 31 22 33 44 55 65 5 151 22 33 44 53 55 4 3100 200 3001 21 31 42 5 | Output: 111101 11111 10001
|
Hard
| 4 | 1,260 | 1,014 | 196 | 21 |
1,861 |
E
|
1861E
|
E. Non-Intersecting Subpermutations
| 2,300 |
combinatorics; dp; implementation; math
|
You are given two integers \(n\) and \(k\).For an array of length \(n\), let's define its cost as the maximum number of contiguous subarrays of this array that can be chosen so that: each element belongs to at most one subarray; the length of each subarray is exactly \(k\); each subarray contains each integer from \(1\) to \(k\) exactly once. For example, if \(n = 10\), \(k = 3\) and the array is \([1, 2, 1, 3, 2, 3, 2, 3, 1, 3]\), its cost is \(2\) because, for example, we can choose the subarrays from the \(2\)-nd element to the \(4\)-th element and from the \(7\)-th element to the \(9\)-th element, and we can show that it's impossible to choose more than \(2\) subarrays.Calculate the sum of costs over all arrays of length \(n\) consisting of integers from \(1\) to \(k\), and print it modulo \(998244353\).
|
The only line of the input contains two integers \(n\) and \(k\) (\(2 \le k \le n \le 4000\)).
|
Print one integer β the sum of costs of all arrays of length \(n\) consisting of integers from \(1\) to \(k\) taken modulo \(998244353\).
|
Input: 10 3 | Output: 71712
|
Expert
| 4 | 819 | 94 | 137 | 18 |
|
1,422 |
C
|
1422C
|
C. Bargain
| 1,700 |
combinatorics; dp; math
|
Sometimes it is not easy to come to an agreement in a bargain. Right now Sasha and Vova can't come to an agreement: Sasha names a price as high as possible, then Vova wants to remove as many digits from the price as possible. In more details, Sasha names some integer price \(n\), Vova removes a non-empty substring of (consecutive) digits from the price, the remaining digits close the gap, and the resulting integer is the price.For example, is Sasha names \(1213121\), Vova can remove the substring \(1312\), and the result is \(121\).It is allowed for result to contain leading zeros. If Vova removes all digits, the price is considered to be \(0\).Sasha wants to come up with some constraints so that Vova can't just remove all digits, but he needs some arguments supporting the constraints. To start with, he wants to compute the sum of all possible resulting prices after Vova's move.Help Sasha to compute this sum. Since the answer can be very large, print it modulo \(10^9 + 7\).
|
The first and only line contains a single integer \(n\) (\(1 \le n < 10^{10^5}\)).
|
In the only line print the required sum modulo \(10^9 + 7\).
|
Consider the first example.Vova can choose to remove \(1\), \(0\), \(7\), \(10\), \(07\), or \(107\). The results are \(07\), \(17\), \(10\), \(7\), \(1\), \(0\). Their sum is \(42\).
|
Input: 107 | Output: 42
|
Medium
| 3 | 988 | 82 | 60 | 14 |
1,859 |
A
|
1859A
|
A. United We Stand
| 800 |
constructive algorithms; math; number theory
|
Given an array \(a\) of length \(n\), containing integers. And there are two initially empty arrays \(b\) and \(c\). You need to add each element of array \(a\) to exactly one of the arrays \(b\) or \(c\), in order to satisfy the following conditions: Both arrays \(b\) and \(c\) are non-empty. More formally, let \(l_b\) be the length of array \(b\), and \(l_c\) be the length of array \(c\). Then \(l_b, l_c \ge 1\). For any two indices \(i\) and \(j\) (\(1 \le i \le l_b, 1 \le j \le l_c\)), \(c_j\) is not a divisor of \(b_i\). Output the arrays \(b\) and \(c\) that can be obtained, or output \(-1\) if they do not exist.
|
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\) (\(2 \le n \le 100\)) β the length of array \(a\).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 elements of array \(a\).
|
For each test case, output a single integer \(-1\) if a solution does not exist.Otherwise, in the first line, output two integers \(l_b\) and \(l_c\) β the lengths of arrays \(b\) and \(c\) respectively.In the second line, output \(l_b\) integers \(b_1, b_2, \ldots, b_{l_b}\) β the elements of array \(b\).In the third line, output \(l_c\) integers \(c_1, c_2, \ldots, c_{l_c}\) β the elements of array \(c\).If there are multiple solutions, output any of them. You can output the elements of the arrays in any order.
|
In the first test case, a solution does not exist.In the second test case, we can obtain \(b = [1, 3, 5]\) and \(c = [2, 4]\). Then elements \(2\) and \(4\) do not divide elements \(1, 3\) and \(5\).In the fifth test case, we can obtain \(b = [4, 8, 4]\) and \(c = [12, 12]\).
|
Input: 532 2 251 2 3 4 531 3 571 7 7 2 9 1 454 8 12 12 4 | Output: -1 3 2 1 3 5 2 4 1 2 1 3 5 2 5 1 1 2 4 7 7 9 3 2 4 8 4 12 12
|
Beginner
| 3 | 626 | 436 | 518 | 18 |
1,427 |
F
|
1427F
|
F. Boring Card Game
| 3,200 |
data structures; greedy; trees
|
When they are bored, Federico and Giada often play the following card game with a deck containing \(6n\) cards.Each card contains one number between \(1\) and \(6n\) and each number appears on exactly one card. Initially the deck is sorted, so the first card contains the number \(1\), the second card contains the number \(2\), \(\dots\), and the last one contains the number \(6n\).Federico and Giada take turns, alternating; Federico starts.In his turn, the player takes \(3\) contiguous cards from the deck and puts them in his pocket. The order of the cards remaining in the deck is not changed. They play until the deck is empty (after exactly \(2n\) turns). At the end of the game both Federico and Giada have \(3n\) cards in their pockets.You are given the cards in Federico's pocket at the end of the game. Describe a sequence of moves that produces that set of cards in Federico's pocket.
|
The first line of the input contains one integer \(n\) (\(1\le n \le 200\)).The second line of the input contains \(3n\) numbers \(x_1, x_2,\ldots, x_{3n}\) (\(1 \le x_1 < x_2 <\ldots < x_{3n} \le 6n\)) β the cards in Federico's pocket at the end of the game. It is guaranteed that for each test there is at least one sequence of moves that produces such set of cards in Federico's pocket.
|
Print \(2n\) lines, each containing \(3\) integers.The \(i\)-th line should contain, in increasing order, the integers \(a_i<b_i<c_i\) written on the three cards taken by the player during the \(i\)-th turn (so taken by Federico if \(i\) is odd and by Giada if \(i\) is even).If there is more than one possible sequence of moves, you can print any.
|
Explanation of the first testcase: Initially the deck has \(12 = 2\cdot 6\) sorted cards, so the deck is \([1\ 2\ 3\ 4\ 5\ 6\ 7\ 8\ 9\ 10\ 11\ 12]\). During turn \(1\), Federico takes the three cards \([9\ 10\ 11]\). After his move, the deck is \([1\ 2\ 3\ 4\ 5\ 6\ 7\ 8\ 12]\). During turn \(2\), Giada takes the three cards \([6\ 7\ 8]\). After her move, the deck is \([1\ 2\ 3\ 4\ 5\ 12]\). During turn \(3\), Federico takes the three cards \([2\ 3\ 4]\). After his move, the deck is \([1\ 5\ 12]\). During turn \(4\), Giada takes the three cards \([1\ 5\ 12]\). After her move, the deck is empty. At the end of the game, the cards in Federico's pocket are \([2\ 3\ 4\ 9\ 10\ 11]\) and the cards in Giada's pocket are \([1\ 5\ 6\ 7\ 8\ 12]\).
|
Input: 2 2 3 4 9 10 11 | Output: 9 10 11 6 7 8 2 3 4 1 5 12
|
Master
| 3 | 898 | 389 | 348 | 14 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.