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 + ...
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....
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...
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 p...
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\...
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...
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 frien...
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 b...
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 constraint...
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 c...
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 \(...
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 th...
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 ...
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_...
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...
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 ...
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...
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_...
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 ...
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\) cont...
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 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 an...
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 w...
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 singer...
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 ...
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 o...
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 par...
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 th...
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'...
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...
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 be...
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 fou...
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, s...
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 mo...
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 qu...
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,...
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 ...
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 f...
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 strin...
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...
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 ...
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 ...
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...
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: fir...
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 gri...
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 thir...
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 an...
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 ...
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 ...
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 lowerc...
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 answ...
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 le...
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 ...
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 stra...
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, ...
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 ...
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 ...
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...
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 choo...
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, ...
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\): ...
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 ...
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...
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...
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...
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. Th...
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...
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\) m...
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...
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 eac...
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\) l...
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 th...
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 permutat...
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...
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...
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...
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...
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\) a...
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 ...
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 whi...
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 researc...
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 ...
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 ...
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 \l...
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 goi...
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\)) β€”...
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...
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 ...
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 fr...
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 ...
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, ...
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 ...
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\) ...
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 ...
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. ...
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 pain...
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 g...
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 el...
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,...
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 painte...
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 (possi...
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 ...
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...
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 t...
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\...
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 \...
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} \rightarr...
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 ...
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 i...
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 ...
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"...
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 ≀...
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, ot...
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 numb...
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 four...
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 segme...
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, pro...
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\) mean...
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 s...
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 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 β€” \(...
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 ...
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 trave...
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 nu...
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...
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 b...
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) β€” b...
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 ca...
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\le...
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 cha...
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...
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 \...
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 i...
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 ...
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 mu...
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 \...
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"") sa...
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\...
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, ...
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 contai...
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 ...
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 ma...
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 cas...
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 di...
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 Mono...
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 ...
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 birt...
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 o...
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 ea...
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 sympt...
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"".T...
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 ...
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 o...
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...
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 le...
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 th...
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...
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...
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, x...
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 d...
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...
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 contai...
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\) creat...
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 ...
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 sol...
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 ...
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)\...
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 ...
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 stairw...
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 s...
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...
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 el...
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 sub...
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 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 peo...
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 ...
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 c...
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. Y...
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 int...
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 poin...
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 cu...
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 in...
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 ...
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\} \)\...
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 \le...
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 ...
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\...
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 (...
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\...
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...
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 ...
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 numbe...
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 se...
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 ...
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 ...
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