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
383
E
383E
E. Vowels
2,700
combinatorics; divide and conquer; dp
Iahubina is tired of so many complicated languages, so she decided to invent a new, simple language. She already made a dictionary consisting of n 3-words. A 3-word is a sequence of exactly 3 lowercase letters of the first 24 letters of the English alphabet (a to x). She decided that some of the letters are vowels, and...
The first line contains one integer, n (1 ≀ n ≀ 104). Each of the next n lines contains a 3-word consisting of 3 lowercase letters. There will be no two identical 3-words.
Print one number, the xor of the squared answers to the queries.
Input: 5abcaaaadabcddef | Output: 0
Master
3
1,019
171
64
3
153
A
153A
A. A + B
1,600
*special
You are given two integers A and B. Calculate their sum and output it without leading zeros.
Two lines of input data contain integers A and B (1 ≀ A, B ≀ 105).
Output A + B without leading zeros.
The code provided in the post about the round doesn't solve the task.
Input: 123 | Output: 15
Medium
1
92
66
35
1
1,357
A4
1357A4
A4. Distinguish Rz from R1
0
*special
You are given an operation that implements a single-qubit unitary transformation: either the Rz gate or the R1 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 Rz gate or 1...
Beginner
1
1,009
0
0
13
1,000
F
1000F
F. One Occurrence
2,400
data structures; divide and conquer
You are given an array \(a\) consisting of \(n\) integers, and \(q\) queries to it. \(i\)-th query is denoted by two integers \(l_i\) and \(r_i\). For each query, you have to find any integer that occurs exactly once in the subarray of \(a\) from index \(l_i\) to index \(r_i\) (a subarray is a contiguous subsegment of ...
The first line contains one integer \(n\) (\(1 \le n \le 5 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 5 \cdot 10^5\)).The third line contains one integer \(q\) (\(1 \le q \le 5 \cdot 10^5\)).Then \(q\) lines follow, \(i\)-th line containing two integers \(l_i\) and \...
Answer the queries as follows:If there is no integer such that it occurs in the subarray from index \(l_i\) to index \(r_i\) exactly once, print \(0\). Otherwise print any such integer.
Input: 61 1 2 3 2 422 61 2 | Output: 40
Expert
2
690
385
185
10
1,375
G
1375G
G. Tree Modification
2,800
brute force; constructive algorithms; dfs and similar; graph matchings; graphs; trees
You are given a tree with \(n\) vertices. You are allowed to modify the structure of the tree through the following multi-step operation: Choose three vertices \(a\), \(b\), and \(c\) such that \(b\) is adjacent to both \(a\) and \(c\). For every vertex \(d\) other than \(b\) that is adjacent to \(a\), remove the edge ...
The first line contains an integer \(n\) (\(3 \le n \le 2 \cdot 10^5\)) β€” the number of vertices in the tree.The \(i\)-th of the following \(n - 1\) lines contains two integers \(u_i\) and \(v_i\) (\(1 \le u_i, v_i \le n\), \(u_i \neq v_i\)) denoting that there exists an edge connecting vertices \(u_i\) and \(v_i\). It...
Print a single integer β€” the minimum number of operations needed to transform the tree into a star.It can be proven that under the given constraints, it is always possible to transform the tree into a star using at most \(10^{18}\) operations.
The first test case corresponds to the tree shown in the statement. As we have seen before, we can transform the tree into a star with center at vertex \(5\) by applying a single operation to vertices \(2\), \(4\), and \(5\).In the second test case, the given tree is already a star with the center at vertex \(4\), so n...
Input: 6 4 5 2 6 3 2 1 2 2 4 | Output: 1
Master
6
944
368
243
13
1,355
C
1355C
C. Count Triangles
1,800
binary search; implementation; math; two pointers
Like any unknown mathematician, Yuri has favourite numbers: \(A\), \(B\), \(C\), and \(D\), where \(A \leq B \leq C \leq D\). Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides \(x\), \(y\), and \(z\) exist, such that \(A \leq x \leq B \leq y \leq C \leq z \leq D\) holds...
The first line contains four integers: \(A\), \(B\), \(C\) and \(D\) (\(1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5\)) β€” Yuri's favourite numbers.
Print the number of non-degenerate triangles with integer sides \(x\), \(y\), and \(z\) such that the inequality \(A \leq x \leq B \leq y \leq C \leq z \leq D\) holds.
In the first example Yuri can make up triangles with sides \((1, 3, 3)\), \((2, 2, 3)\), \((2, 3, 3)\) and \((2, 3, 4)\).In the second example Yuri can make up triangles with sides \((1, 2, 2)\), \((2, 2, 2)\) and \((2, 2, 3)\).In the third example Yuri can make up only one equilateral triangle with sides equal to \(5 ...
Input: 1 2 3 4 | Output: 4
Medium
4
560
150
167
13
1,864
A
1864A
A. Increasing and Decreasing
800
constructive algorithms; greedy; implementation; math
You are given three integers \(x\), \(y\), and \(n\).Your task is to construct an array \(a\) consisting of \(n\) integers which satisfies the following conditions: \(a_1=x\), \(a_n=y\); \(a\) is strictly increasing (i.e. \(a_1 < a_2 < \ldots < a_n\)); if we denote \(b_i=a_{i+1}-a_{i}\) for \(1 \leq i \leq n-1\), then ...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)). The description of the test cases follows.The only line of each test case contains three integers \(x\), \(y\), \(n\) (\(1 \le x < y \le 1000,3 \le n \le 1000\)).
For each test case, output \(n\) integers \(a_1,a_2,\ldots,a_n\). If there are multiple solutions, print any of them.If there is no solution, print a single integer \(-1\).
In the first test case, \(a=[1,3,4]\), which is strictly increasing. Next, \(b_1=a_2-a_1=3-1=2\), \(b_2=a_3-a_2=4-3=1\), thus \(b=[2,1]\), which is strictly decreasing.In the second test case, there is no array \(a\) that satisfies all the conditions above.
Input: 31 4 31 3 3100 200 4 | Output: 1 3 4 -1 100 150 180 200
Beginner
4
453
281
172
18
1,837
E
1837E
E. Playoff Fixing
2,200
combinatorics; trees
\(2^k\) teams participate in a playoff tournament. The teams are numbered from \(1\) to \(2^k\), in order of decreasing strength. So, team \(1\) is the strongest one, team \(2^k\) is the weakest one. A team with a smaller number always defeats a team with a larger number.First of all, the teams are arranged in some ord...
The first line contains a single integer \(k\) (\(0 \le k \le 19\)) β€” there are \(2^k\) teams.The second line contains \(2^k\) integers \(a_1, a_2, \dots, a_{2^k}\) (\(a_i = -1\) or \(1 \le a_i \le 2^k\)). If \(a_i \ne -1\), then team \(a_i\) has seed \(i\). Otherwise, the seed \(i\) is not reserved for any team.All va...
Print a single integer β€” the number of ways to fill the non-reserved seeds so that the tournament goes as planned, modulo \(998\,244\,353\).
Input: 2 1 2 3 4 | Output: 0
Hard
2
2,444
360
140
18
165
C
165C
C. Another Problem on Strings
1,600
binary search; brute force; dp; math; strings; two pointers
A string is binary, if it consists only of characters ""0"" and ""1"".String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string ""010"" has six substrings: ""0"", ""1"", ""0"", ""01"", ""10"", ""010"". Two substrings are considered diffe...
The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters.
Print the single number β€” the number of substrings of the given string, containing exactly k characters ""1"".Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
In the first sample the sought substrings are: ""1"", ""1"", ""10"", ""01"", ""10"", ""010"".In the second sample the sought substrings are: ""101"", ""0101"", ""1010"", ""01010"".
Input: 11010 | Output: 6
Medium
6
589
161
256
1
232
B
232B
B. Table
1,900
bitmasks; combinatorics; dp; math
John Doe has an n Γ— m table. John Doe can paint points in some table cells, not more than one point in one table cell. John Doe wants to use such operations to make each square subtable of size n Γ— n have exactly k points.John Doe wondered, how many distinct ways to fill the table with points are there, provided that t...
A single line contains space-separated integers n, m, k (1 ≀ n ≀ 100; n ≀ m ≀ 1018; 0 ≀ k ≀ n2) β€” the number of rows of the table, the number of columns of the table and the number of points each square must contain.Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use th...
In a single line print a single integer β€” the remainder from dividing the described number of ways by 1000000007 (109 + 7).
Let's consider the first test case: The gray area belongs to both 5 Γ— 5 squares. So, if it has one point, then there shouldn't be points in any other place. If one of the white areas has a point, then the other one also must have a point. Thus, there are about 20 variants, where the point lies in the gray area and 25 v...
Input: 5 6 1 | Output: 45
Hard
4
682
363
123
2
1,926
D
1926D
D. Vlad and Division
1,300
bitmasks; greedy
Vladislav has \(n\) non-negative integers, and he wants to divide all of them into several groups so that in any group, any pair of numbers does not have matching bit values among bits from \(1\)-st to \(31\)-st bit (i.e., considering the \(31\) least significant bits of the binary representation).For an integer \(k\),...
The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β€” the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β€” the total number of integers.The second line of each test case contains \(n\) given integers \(a_1, \ldots, a_n\) (\(0 \l...
For each test case, output a single integer β€” the minimum number of groups required to satisfy the condition.
In the first test case, any two numbers have the same last \(31\) bits, so we need to place each number in its own group.In the second test case, \(a_1=0000000000000000000000000000000_2\), \(a_2=1111111111111111111111111111111_2\) so they can be placed in the same group because \(a_1(i) \ne a_2(i)\) for each \(i\) betw...
Input: 941 4 3 420 21474836475476319172 261956880 2136179468 1671164475 188552676731335890506 811593141 11282233624688873446 627404104 1520079543 1458610201461545621 2085938026 1269342732 143025857540 0 2147483647 214748364730 0 214748364781858058912 289424735 1858058912 2024818580 1858058912 289424735 122665067 289424...
Easy
2
872
419
109
19
1,738
C
1738C
C. Even Number Addicts
1,500
dp; games; greedy; math
Alice and Bob are playing a game on a sequence \(a_1, a_2, \dots, a_n\) of length \(n\). They move in turns and Alice moves first.In the turn of each player, he or she should select an integer and remove it from the sequence. The game ends when there is no integer left in the sequence. Alice wins if the sum of her sele...
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 100\)) β€” the number of test cases. The following lines contain the description of each test case.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 100\)), indicating the length of the sequence.The...
For each test case, output ""Alice"" (without quotes) if Alice wins and ""Bob"" (without quotes) otherwise.
In the first and second test cases, Alice always selects two odd numbers, so the sum of her selected numbers is always even. Therefore, Alice always wins.In the third test case, Bob has a winning strategy that he always selects a number with the same parity as Alice selects in her last turn. Therefore, Bob always wins....
Input: 431 3 541 3 5 741 2 3 4410 20 30 40 | Output: Alice Alice Bob Alice
Medium
4
444
472
107
17
2
C
2C
C. Commentator problem
2,600
geometry
The Olympic Games in Bercouver are in full swing now. Here everyone has their own objectives: sportsmen compete for medals, and sport commentators compete for more convenient positions to give a running commentary. Today the main sport events take place at three round stadiums, and the commentator's objective is to cho...
The input data consists of three lines, each of them describes the position of one stadium. The lines have the format x, y, r, where (x, y) are the coordinates of the stadium's center ( - 103 ≀ x, y ≀ 103), and r (1 ≀ r ≀ 103) is its radius. All the numbers in the input data are integer, stadiums do not have common poi...
Print the coordinates of the required point with five digits after the decimal point. If there is no answer meeting the conditions, the program shouldn't print anything. The output data should be left blank.
Input: 0 0 1060 0 1030 30 10 | Output: 30.00000 0.00000
Expert
1
894
368
207
0
1,244
B
1244B
B. Rooms and Staircases
1,000
brute force; implementation
Nikolay lives in a two-storied house. There are \(n\) rooms on each floor, arranged in a row and numbered from one from left to right. So each room can be represented by the number of the floor and the number of the room on this floor (room number is an integer between \(1\) and \(n\)). If Nikolay is currently in some ...
The first line of the input contains one integer \(t\) (\(1 \le t \le 100\)) β€” the number of test cases in the input. Then test cases follow. Each test case consists of two lines.The first line contains one integer \(n\) \((1 \le n \le 1\,000)\) β€” the number of rooms on each floor.The second line contains one string co...
For each test case print one integer β€” the maximum number of rooms Nikolay can visit during his tour, if he can start in any room on any floor, and he won't visit the same room twice.
In the first test case Nikolay may start in the first room of the first floor. Then he moves to the second room on the first floor, and then β€” to the third room on the first floor. Then he uses a staircase to get to the third room on the second floor. Then he goes to the fourth room on the second floor, and then β€” to t...
Input: 4 5 00100 8 00000000 5 11111 3 110 | Output: 6 8 10 6
Beginner
2
1,582
775
183
12
257
D
257D
D. Sum
1,900
greedy; math
Vasya has found a piece of paper with an array written on it. The array consists of n integers a1, a2, ..., an. Vasya noticed that the following condition holds for the array ai ≀ ai + 1 ≀ 2Β·ai for any positive integer i (i < n).Vasya wants to add either a ""+"" or a ""-"" before each number of array. Thus, Vasya will ...
The first line contains integer n (1 ≀ n ≀ 105) β€” the size of the array. The second line contains space-separated integers a1, a2, ..., an (0 ≀ ai ≀ 109) β€” the original array. It is guaranteed that the condition ai ≀ ai + 1 ≀ 2Β·ai fulfills for any positive integer i (i < n).
In a single line print the sequence of n characters ""+"" and ""-"", where the i-th character is the sign that is placed in front of number ai. The value of the resulting expression s must fit into the limits 0 ≀ s ≀ a1. If there are multiple solutions, you are allowed to print any of them.
Input: 41 2 3 5 | Output: +++-
Hard
2
687
275
291
2
238
A
238A
A. Not Wool Sequences
1,300
constructive algorithms; math
A sequence of non-negative integers a1, a2, ..., an of length n is called a wool sequence if and only if there exists two integers l and r (1 ≀ l ≀ r ≀ n) such that . In other words each wool sequence contains a subsequence of consecutive elements with xor equal to 0.The expression means applying the operation of a bit...
The only line of input contains two space-separated integers n and m (1 ≀ n, m ≀ 105).
Print the required number of sequences modulo 1000000009 (109 + 9) on the only line of output.
Sequences of length 3 made of integers 0, 1, 2 and 3 that are not a wool sequence are (1, 3, 1), (1, 2, 1), (2, 1, 2), (2, 3, 2), (3, 1, 3) and (3, 2, 3).
Input: 3 2 | Output: 6
Easy
2
686
86
94
2
798
C
798C
C. Mike and gcd problem
1,700
dp; greedy; number theory
Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. . Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≀ i < n), delete numbers ai, ai + 1 and put nu...
The first line contains a single integer n (2 ≀ n ≀ 100 000) β€” length of sequence A.The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A.
Output on the first line ""YES"" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and ""NO"" (without quotes) otherwise.If the answer was ""YES"", output the minimal number of moves needed to make sequence A beautiful.
In the first example you can simply make one move to obtain sequence [0, 2] with .In the second example the gcd of the sequence is already greater than 1.
Input: 21 1 | Output: YES1
Medium
3
651
192
275
7
955
A
955A
A. Feed the cat
1,100
greedy; math
After waking up at hh:mm, Andrew realised that he had forgotten to feed his only cat for yet another time (guess why there's only one cat). The cat's current hunger level is H points, moreover each minute without food increases his hunger by D points.At any time Andrew can visit the store where tasty buns are sold (you...
The first line contains two integers hh and mm (00 ≀ hh ≀ 23, 00 ≀ mm ≀ 59) β€” the time of Andrew's awakening.The second line contains four integers H, D, C and N (1 ≀ H ≀ 105, 1 ≀ D, C, N ≀ 102).
Output the minimum amount of money to within three decimal digits. You answer is considered correct, if its absolute or relative error does not exceed 10 - 4.Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .
In the first sample Andrew can visit the store at exactly 20:00. The cat's hunger will be equal to 315, hence it will be necessary to purchase 315 buns. The discount makes the final answer 25200 roubles.In the second sample it's optimal to visit the store right after he wakes up. Then he'll have to buy 91 bins per 15 r...
Input: 19 00255 1 100 1 | Output: 25200.0000
Easy
2
815
195
256
9
2,096
B
2096B
B. Wonderful Gloves
1,100
greedy; math; sortings
You are the proud owner of many colorful gloves, and you keep them in a drawer. Each glove is in one of \(n\) colors numbered \(1\) to \(n\). Specifically, for each \(i\) from \(1\) to \(n\), you have \(l_i\) left gloves and \(r_i\) right gloves with color \(i\).Unfortunately, it's late at night, so you can't see any o...
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\), \(k\) (\(1 \leq k \leq n \leq 2 \cdot 10^5\)) β€” the number of different colors, and the minimum ...
For each test case, output a single integer β€” the minimum number of gloves you need to take out of the drawer.
In the first test case, you must take out all of the gloves, so the answer is \(6\).In the second test case, the answer is \(101\). If you take out \(100\) gloves or fewer, then it is possible that all of them are left gloves, which means you won't have a matching pair of gloves.In the third test case, the answer is \(...
Input: 53 31 1 11 1 11 110013 2100 1 1200 1 15 297 59 50 87 3695 77 33 13 7410 697 59 50 87 36 95 77 33 13 7491 14 84 33 54 89 68 34 14 15 | Output: 6 101 303 481 1010
Easy
3
931
838
110
20
96
B
96B
B. Lucky Numbers (easy)
1,300
binary search; bitmasks; brute force
Petya loves lucky numbers. Everybody knows that positive integers are lucky if their decimal representation doesn't contain digits other than 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.Lucky number is super lucky if it's decimal representation contains equal amount of digits 4 and 7. For ...
The only line contains a positive integer n (1 ≀ n ≀ 109). This number doesn't have leading zeroes.
Output the least super lucky number that is more than or equal to n.Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specificator.
Input: 4500 | Output: 4747
Easy
3
513
99
221
0
302
A
302A
A. Eugeny and Array
800
implementation
Eugeny has array a = a1, a2, ..., an, consisting of n integers. Each integer ai equals to -1, or to 1. Also, he has m queries: Query number i is given as a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). The response to the query will be integer 1, if the elements of array a can be rearranged so as the sum ali + ali + 1 + ....
The first line contains integers n and m (1 ≀ n, m ≀ 2Β·105). The second line contains n integers a1, a2, ..., an (ai = -1, 1). Next m lines contain Eugene's queries. The i-th line contains integers li, ri (1 ≀ li ≀ ri ≀ n).
Print m integers β€” the responses to Eugene's queries in the order they occur in the input.
Input: 2 31 -11 11 22 2 | Output: 010
Beginner
1
425
223
90
3
2,047
B
2047B
B. Replace Character
900
brute force; combinatorics; greedy; strings
You're given a string \(s\) of length \(n\), consisting of only lowercase English letters. You must do the following operation exactly once: Choose any two indices \(i\) and \(j\) (\(1 \le i, j \le n\)). You can choose \(i = j\). Set \(s_i := s_j\). You need to minimize the number of distinct permutations\(^\dagger\) o...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 500\)). The description of the test cases follows.The first line of each test case contains \(n\) (\(1 \le n \le 10\)) β€” the length of string \(s\).The second line of each test case contains \(s\) of length \(n...
For each test case, output the required \(s\) after applying exactly one operation. If there are multiple solutions, print any of them.
In the first test case, we can obtain the following strings in one operation: ""abc"", ""bbc"", ""cbc"", ""aac"", ""acc"", ""aba"", and ""abb"".The string ""abc"" has \(6\) distinct permutations: ""abc"", ""acb"", ""bac"", ""bca"", ""cab"", and ""cba"".The string ""cbc"" has \(3\) distinct permutations: ""bcc"", ""cbc"...
Input: 63abc4xyyx8alphabet1k10aabbccddee6ttbddq | Output: cbc yyyx alphaaet k eabbccddee tttddq
Beginner
4
597
375
135
20
2,085
E
2085E
E. Serval and Modulo
2,200
constructive algorithms; math; number theory
There is an array \(a\) consisting of \(n\) non-negative integers and a magic number \(k\) (\(k\ge 1\), \(k\) is an integer). Serval has constructed another array \(b\) of length \(n\), where \(b_i = a_i \bmod k\) holds\(^{\text{βˆ—}}\) for all \(1\leq i\leq n\). Then, he shuffled \(b\).You are given the two arrays \(a\)...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains an integer \(n\) (\(1\leq n\leq 10^4\)) β€” the length of the array \(a\).The second line contains \(n\) integers \(a_...
For each test case, output a single integer \(k\) (\(1\leq k\leq 10^9\)) β€” the magic number you found. Print \(-1\) if it is impossible to find such an integer.If there are multiple answers, you may print any of them.
In the first test case, if \(k\ge 3\), then \(2=a_3\bmod k\) should be in the array \(b\), which leads to a contradiction. For \(k = 1\), \([a_1\bmod k, a_2\bmod k, a_3\bmod k, a_4 \bmod k] = [0,0,0,0]\), which cannot be shuffled to \(b\). For \(k = 2\), \([a_1\bmod k, a_2\bmod k, a_3\bmod k, a_4 \bmod k] = [1,1,0,1]\)...
Input: 543 5 2 70 1 1 153 1 5 2 41 2 3 4 562 3 4 7 8 91 2 3 6 7 8521 22 25 28 200 1 2 1 061 1 2 3 5 80 0 1 1 0 0 | Output: 2 31415926 -1 4 -1
Hard
3
787
609
217
20
25
D
25D
D. Roads not only in Berland
1,900
dsu; graphs; trees
Berland Government decided to improve relations with neighboring countries. First of all, it was decided to build new roads so that from each city of Berland and neighboring countries it became possible to reach all the others. There are n cities in Berland and neighboring countries in total and exactly n - 1 two-way r...
The first line contains integer n (2 ≀ n ≀ 1000) β€” amount of cities in Berland and neighboring countries. Next n - 1 lines contain the description of roads. Each road is described by two space-separated integers ai, bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” pair of cities, which the road connects. It can't be more than one road b...
Output the answer, number t β€” what is the least amount of days needed to rebuild roads so that from each city it became possible to reach all the others. Then output t lines β€” the plan of closure of old roads and building of new ones. Each line should describe one day in the format i j u v β€” it means that road between ...
Input: 21 2 | Output: 0
Hard
3
779
383
465
0
732
B
732B
B. Cormen β€” The Best Friend Of a Man
1,000
dp; greedy
Recently a dog was bought for Polycarp. The dog's name is Cormen. Now Polycarp has a lot of troubles. For example, Cormen likes going for a walk. Empirically Polycarp learned that the dog needs at least k walks for any two consecutive days in order to feel good. For example, if k = 5 and yesterday Polycarp went for a w...
The first line contains two integers n and k (1 ≀ n, k ≀ 500) β€” the number of days and the minimum number of walks with Cormen for any two consecutive days. The second line contains integers a1, a2, ..., an (0 ≀ ai ≀ 500) β€” the number of walks with Cormen on the i-th day which Polycarp has already planned.
In the first line print the smallest number of additional walks that Polycarp should do during the next n days so that Cormen will feel good during all days. In the second line print n integers b1, b2, ..., bn, where bi β€” the total number of walks on the i-th day according to the found solutions (ai ≀ bi for all i from...
Input: 3 52 0 1 | Output: 42 3 2
Beginner
2
1,189
307
381
7
271
B
271B
B. Prime Matrix
1,300
binary search; brute force; math; number theory
You've got an n Γ— m matrix. The matrix consists of integers. In one move, you can apply a single transformation to the matrix: choose an arbitrary element of the matrix and increase it by 1. Each element can be increased an arbitrary number of times.You are really curious about prime numbers. Let us remind you that a p...
The first line contains two integers n, m (1 ≀ n, m ≀ 500) β€” the number of rows and columns in the matrix, correspondingly.Each of the following n lines contains m integers β€” the initial matrix. All matrix elements are positive integers. All numbers in the initial matrix do not exceed 105.The numbers in the lines are s...
Print a single integer β€” the minimum number of moves needed to get a prime matrix from the one you've got. If you've got a prime matrix, print 0.
In the first sample you need to increase number 1 in cell (1, 1). Thus, the first row will consist of prime numbers: 2, 2, 3.In the second sample you need to increase number 8 in cell (1, 2) three times. Thus, the second column will consist of prime numbers: 11, 2.In the third sample you don't have to do anything as th...
Input: 3 31 2 35 6 14 4 1 | Output: 1
Easy
4
776
346
145
2
1,009
G
1009G
G. Allowed Letters
2,400
bitmasks; flows; graph matchings; graphs; greedy
Polycarp has just launched his new startup idea. The niche is pretty free and the key vector of development sounds really promising, so he easily found himself some investors ready to sponsor the company. However, he is yet to name the startup!Actually, Polycarp has already came up with the name but some improvement to...
The first line is the string \(s\) (\(1 \le |s| \le 10^5\)) β€” the name Polycarp has came up with. The string consists only of lowercase Latin letters from ""a"" to ""f"".The second line contains a single integer \(m\) (\(0 \le m \le |s|\)) β€” the number of investors.The \(i\)-th of the next \(m\) lines contain an intege...
If Polycarp can't produce any valid name then print ""Impossible"".Otherwise print the smallest lexicographically name Polycarp can obtain by swapping letters in string \(s\) such that the letter at every position is among the allowed ones.
Input: bedefead52 e1 dc5 b7 ef6 ef | Output: deadbeef
Expert
5
1,247
657
240
10
1,787
C
1787C
C. Remove the Bracket
1,600
dp; greedy; math
RSJ has a sequence \(a\) of \(n\) integers \(a_1,a_2, \ldots, a_n\) and an integer \(s\). For each of \(a_2,a_3, \ldots, a_{n-1}\), he chose a pair of non-negative integers \(x_i\) and \(y_i\) such that \(x_i+y_i=a_i\) and \((x_i-s) \cdot (y_i-s) \geq 0\).Now he is interested in the value $$$\(F = a_1 \cdot x_2+y_2 \cd...
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The first line of each test case contains two integers \(n\), \(s\) (\(3 \le n \le 2 \cdot 10^5\); \(0 \le s \le 2 \cdot 10^5\)).The second line contains \(n\) integers \(a_1,a_2,\ldots,a_n...
For each test case, print the minimum possible value of \(F\).
In the first test case, \(2\cdot 0+0\cdot 1+0\cdot 3+0\cdot 4 = 0\).In the second test case, \(5\cdot 1+2\cdot 2+2\cdot 2+1\cdot 5 = 18\).
Input: 105 02 0 1 3 45 15 3 4 3 57 27 6 5 4 3 2 15 11 2 3 4 55 21 2 3 4 54 00 1 1 15 54 3 5 6 44 10 2 1 03 99999200000 200000 2000006 81397976 129785 12984 78561 173685 15480 | Output: 0 18 32 11 14 0 16 0 40000000000 2700826806
Medium
3
582
428
62
17
542
E
542E
E. Playing on Graph
2,600
graphs; shortest paths
Vova and Marina love offering puzzles to each other. Today Marina offered Vova to cope with the following task.Vova has a non-directed graph consisting of n vertices and m edges without loops and multiple edges. Let's define the operation of contraction two vertices a and b that are not connected by an edge. As a resul...
The first line contains two integers n, m (1 ≀ n ≀ 1000, 0 ≀ m ≀ 100 000) β€” the number of vertices and the number of edges in the original graph.Next m lines contain the descriptions of edges in the format ai, bi (1 ≀ ai, bi ≀ n, ai β‰  bi), which means that there is an edge between vertices ai and bi. It is guaranteed t...
If it is impossible to obtain a chain from the given graph, print - 1. Otherwise, print the maximum possible number of edges in the resulting chain.
In the first sample test you can contract vertices 4 and 5 and obtain a chain of length 3.In the second sample test it is initially impossible to contract any pair of vertexes, so it is impossible to achieve the desired result.In the third sample test you can contract vertices 1 and 2 and obtain a chain of length 2.
Input: 5 41 22 33 43 5 | Output: 3
Expert
2
1,570
380
148
5
1,749
D
1749D
D. Counting Arrays
1,900
combinatorics; dp; math; number theory
Consider an array \(a\) of length \(n\) with elements numbered from \(1\) to \(n\). It is possible to remove the \(i\)-th element of \(a\) if \(gcd(a_i, i) = 1\), where \(gcd\) denotes the greatest common divisor. After an element is removed, the elements to the right are shifted to the left by one position.An array \(...
The only line of the input contains two integers \(n\) and \(m\) (\(2 \le n \le 3 \cdot 10^5\); \(1 \le m \le 10^{12}\)).
Print one integer β€” the number of ambiguous arrays \(a\) such that the length of \(a\) is from \(1\) to \(n\) and each \(a_i\) is an integer from \(1\) to \(m\). Since the answer can be very large, print it modulo \(998244353\).
Input: 2 3 | Output: 6
Hard
4
1,472
121
228
17
1,202
E
1202E
E. You Are Given Some Strings...
2,400
brute force; string suffix structures; strings
You are given a string \(t\) and \(n\) strings \(s_1, s_2, \dots, s_n\). All strings consist of lowercase Latin letters.Let \(f(t, s)\) be the number of occurences of string \(s\) in string \(t\). For example, \(f('\text{aaabacaa}', '\text{aa}') = 3\), and \(f('\text{ababa}', '\text{aba}') = 2\).Calculate the value of ...
The first line contains string \(t\) (\(1 \le |t| \le 2 \cdot 10^5\)).The second line contains integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)).Each of next \(n\) lines contains string \(s_i\) (\(1 \le |s_i| \le 2 \cdot 10^5\)).It is guaranteed that \(\sum\limits_{i=1}^{n} |s_i| \le 2 \cdot 10^5\). All strings consist of l...
Print one integer β€” the value of \(\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{n} f(t, s_i + s_j)\).
Input: aaabacaa 2 a aa | Output: 5
Expert
3
668
345
97
12
2,062
F
2062F
F. Traveling Salescat
2,900
constructive algorithms; dp; geometry; graphs; greedy; math; sortings
You are a cat selling fun algorithm problems. Today, you want to recommend your fun algorithm problems to \(k\) cities.There are a total of \(n\) cities, each with two parameters \(a_i\) and \(b_i\). Between any two cities \(i,j\) (\(i\ne j\)), there is a bidirectional road with a length of \(\max(a_i + b_j , b_i + a_j...
The first line of input contains a single integer \(t\) (\(1 \leq t \leq 1500\)) β€” the number of test cases.For each test case, the first line contains a single integer \(n\) (\(2 \leq n \leq 3\cdot 10^3\)) β€” the number of cities.Then \(n\) lines follow, the \(i\)-th line contains two integers \(a_i,b_i\) (\(0 \leq a_i...
For each test case, print \(n-1\) integers in one line. The \(i\)-th integer represents the minimum cost when \(k=i+1\).
In the first test case: For \(k=2\), the optimal path is \(1\to 2\) with a cost of \(\max(0+1,2+2)=4\). For \(k=3\), the optimal path is \(2\to 1\to 3\) with a cost of \(\max(0+1,2+2)+\max(0+3,3+2)=4+5=9\). In the second test case: For \(k=2\), the optimal path is \(1\to 4\). For \(k=3\), the optimal path is \(2\to 3\t...
Input: 330 22 13 352 77 56 31 87 58899167687 609615846851467150 45726720931502759 23784096918190644 196992738142090421 475722765409556751 726971942513558832 998277529294328304 434714258 | Output: 4 9 10 22 34 46 770051069 1655330585 2931719265 3918741472 5033924854 6425541981 7934325514
Master
7
543
462
120
20
1,750
A
1750A
A. Indirect Sort
800
constructive algorithms; implementation; math
You are given a permutation \(a_1, a_2, \ldots, a_n\) of size \(n\), where each integer from \(1\) to \(n\) appears exactly once.You can do the following operation any number of times (possibly, zero): Choose any three indices \(i, j, k\) (\(1 \le i < j < k \le n\)). If \(a_i > a_k\), replace \(a_i\) with \(a_i + a_j\)...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 5000\)) β€” the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(3 \le n \le 10\)) β€” the length of the array \(a\).The second line contain...
For each test case, output ""Yes"" (without quotes) if the array can be sorted in non-descending order, 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 first test case, \([1,2,3]\) is already sorted in non-descending order.In the second test case, we can choose \(i = 1,j = 2,k = 3\). Since \(a_1 \le a_3\), swap \(a_2\) and \(a_3\), the array then becomes \([1,2,3]\), which is sorted in non-descending order.In the seventh test case, we can do the following opera...
Input: 731 2 331 3 275 3 4 7 6 2 177 6 5 4 3 2 152 1 4 5 352 1 3 4 571 2 6 7 4 3 5 | Output: Yes Yes No No No No Yes
Beginner
3
437
449
282
17
1,663
F
1663F
F. In Every Generation...
0
*special; strings
In every generation there is a Chosen One. She alone will stand against the vampires, the demons, and the forces of darkness. She is the Slayer. β€” Joss Whedon
A string \(s\) (\(3 \le |s| \le 7\)) consisting of lowercase English letters.
A single string. If there is no answer, print ""none"" (without the quotes).
Input: tourist | Output: ooggqjx
Beginner
2
158
77
76
16
2,029
I
2029I
I. Variance Challenge
3,400
flows; graphs; greedy
Kevin has recently learned the definition of variance. For an array \(a\) of length \(n\), the variance of \(a\) is defined as follows: Let \(x=\dfrac{1}{n}\displaystyle\sum_{i=1}^n a_i\), i.e., \(x\) is the mean of the array \(a\); Then, the variance of \(a\) is $$$\( V(a)=\frac{1}{n}\sum_{i=1}^n(a_i-x)^2. \)\( Now, K...
Each test contains multiple test cases. The first line of the input contains a single integer \(t\) (\(1\le t\le 100\)) β€” the number of test cases. The description of test cases follows.The first line of each test case contains three integers \(n\), \(m\), and \(k\) (\(1\le n,m\le 5000\), \(\color{red}{n\cdot m\le 2\cd...
For each test case, output \(m\) integers in a single line, the \(p\)-th integer denoting the minimum possible variance of \(a\) when exactly \(p\) operations are performed, multiplied by \(n^2\).
In the first test case: For \(p = 1\), you can perform the operation on \([1, 1]\), changing \(a\) from \([1, 2, 2]\) to \([2, 2, 2]\). Since all of the elements are equal, the variance is equal to \(0\). For \(p = 2\), you can perform the operation on \([1, 3]\) and then \([1, 1]\), changing \(a\) from \([1, 2, 2]\) t...
Input: 93 2 11 2 23 2 21 2 210 2 110 1 1 1 1 10 1 1 1 16 8 21 1 4 5 1 38 8 720 43 24 2 4 3 20 438 8 320 43 24 2 4 3 20 4310 12 15 3 3 5 4 1 8 1 1 113 10 1000001 2 3 4 5 6 7 8 9 10 11 5 410 5 100002308 9982 4435 3310 100000 9 7 8100 1919 100000 | Output: 0 0 2 2 1161 1024 53 21 21 5 5 5 5 5 10608 6912 4448 3104 1991 131...
Master
3
857
691
196
20
530
C
530C
C. Diophantine equation
1,500
*special
You are given an equation A * X + B * Y = C, A, B, C are positive integer coefficients, X and Y are variables which can have positive integer values only. Output the number of solutions of this equation and the solutions themselves.
The only line of input contains integers A, B and C (1 ≀ A, B, C ≀ 1000), separated with spaces.
In the first line of the output print the number of the solutions N. In the next N lines print the solutions, formatted as ""XY"", sorted in ascending order of X, one solution per line.
Input: 3 5 35 | Output: 25 410 1
Medium
1
232
96
185
5
411
A
411A
A. Password Check
800
*special; implementation
You have probably registered on Internet sites many times. And each time you should enter your invented password. Usually the registration form automatically checks the password's crypt resistance. If the user's password isn't complex enough, a message is displayed. Today your task is to implement such an automatic che...
The first line contains a non-empty sequence of characters (at most 100 characters). Each character is either a large English letter, or a small English letter, or a digit, or one of characters: ""!"", ""?"", ""."", "","", ""_"".
If the password is complex enough, print message ""Correct"" (without the quotes), otherwise print message ""Too weak"" (without the quotes).
Input: abacaba | Output: Too weak
Beginner
2
739
229
141
4
493
C
493C
C. Vasya and Basketball
1,600
binary search; brute force; data structures; implementation; sortings; two pointers
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger tha...
The first line contains integer n (1 ≀ n ≀ 2Β·105) β€” the number of throws of the first team. Then follow n integer numbers β€” the distances of throws ai (1 ≀ ai ≀ 2Β·109). Then follows number m (1 ≀ m ≀ 2Β·105) β€” the number of the throws of the second team. Then follow m integer numbers β€” the distances of throws of bi (1 ≀...
Print two numbers in the format a:b β€” the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
Input: 31 2 325 6 | Output: 9:6
Medium
6
589
333
226
4
1,152
E
1152E
E. Neko and Flashback
2,400
constructive algorithms; dfs and similar; graphs
A permutation of length \(k\) is a sequence of \(k\) integers from \(1\) to \(k\) containing each integer exactly once. For example, the sequence \([3, 1, 2]\) is a permutation of length \(3\).When Neko was five, he thought of an array \(a\) of \(n\) positive integers and a permutation \(p\) of length \(n - 1\). Then, ...
The first line contains an integer \(n\) (\(2 \leq n \leq 10^5\)) β€” the number of elements in array \(a\).The second line contains \(n-1\) integers \(b'_1, b'_2, \ldots, b'_{n-1}\) (\(1 \leq b'_i \leq 10^9\)).The third line contains \(n-1\) integers \(c'_1, c'_2, \ldots, c'_{n-1}\) (\(1 \leq c'_i \leq 10^9\)).
If Neko made a mistake and there is no array \(a\) and a permutation \(p\) leading to the \(b'\) and \(c'\), print -1. Otherwise, print \(n\) positive integers \(a_i\) (\(1 \le a_i \le 10^9\)), denoting the elements of the array \(a\).If there are multiple possible solutions, print any of them.
The first example is explained is the problem statement.In the third example, for \(a = [3, 4, 5, 2, 1, 4, 3, 2]\), a possible permutation \(p\) is \([7, 1, 5, 4, 3, 2, 6]\). In that case, Neko would have constructed the following arrays: \(b = [3, 4, 2, 1, 1, 3, 2]\) \(c = [4, 5, 5, 2, 4, 4, 3]\) \(b' = [2, 3, 1, 1, 2...
Input: 5 4 5 3 5 6 7 4 6 | Output: 3 4 6 5 7
Expert
3
1,371
311
295
11
1,978
E
1978E
E. Computing Machine
2,000
brute force; data structures; dp; greedy; implementation
Sasha has two binary strings \(s\) and \(t\) of the same length \(n\), consisting of the characters 0 and 1.There is also a computing machine that can perform two types of operations on binary strings \(a\) and \(b\) of the same length \(k\): If \(a_{i} = a_{i + 2} =\) 0, then you can assign \(b_{i + 1} :=\) 1 (\(1 \le...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^{4}\)) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the length of the strings \(s\) and \...
For each test case, output \(q\) integers β€” the answers to all queries.
In the first test case: In the first query, \(a =\) 11, so the maximum number of 1 characters is \(2\). In the second query, \(a =\) 111, so the maximum number of 1 characters is \(3\). In the second test case: In the first query, \(a =\) 101 and \(b =\) 110. No operations can be performed, so the maximum number of 1 c...
Input: 341111000021 22 441010110121 31 4601010101101052 31 62 54 43 6 | Output: 2 3 2 3 1 4 3 1 2
Hard
5
811
1,037
71
19
908
B
908B
B. New Year and Buggy Bot
1,200
brute force; implementation
Bob programmed a robot to navigate through a 2d maze.The maze has some obstacles. Empty cells are denoted by the character '.', where obstacles are denoted by '#'.There is a single robot in the maze. Its start position is denoted with the character 'S'. This position has no obstacle in it. There is also a single exit i...
The first line of input will contain two integers n and m (2 ≀ n, m ≀ 50), denoting the dimensions of the maze.The next n lines will contain exactly m characters each, denoting the maze.Each character of the maze will be '.', '#', 'S', or 'E'.There will be exactly one 'S' and exactly one 'E' in the maze.The last line w...
Print a single integer, the number of mappings of digits to directions that will lead the robot to the exit.
For the first sample, the only valid mapping is , where D is down, L is left, U is up, R is right.
Input: 5 6.....#S....#.#.....#.......E..333300012 | Output: 1
Easy
2
1,405
448
108
9
1,176
E
1176E
E. Cover it!
1,700
dfs and similar; dsu; graphs; shortest paths; trees
You are given an undirected unweighted connected graph consisting of \(n\) vertices and \(m\) edges. It is guaranteed that there are no self-loops or multiple edges in the given graph.Your task is to choose at most \(\lfloor\frac{n}{2}\rfloor\) vertices in this graph so each unchosen vertex is adjacent (in other words,...
The first line contains a single integer \(t\) (\(1 \le t \le 2 \cdot 10^5\)) β€” the number of queries.Then \(t\) queries follow.The first line of each query contains two integers \(n\) and \(m\) (\(2 \le n \le 2 \cdot 10^5\), \(n - 1 \le m \le min(2 \cdot 10^5, \frac{n(n-1)}{2})\)) β€” the number of vertices and the numb...
For each query print two lines.In the first line print \(k\) (\(1 \le \lfloor\frac{n}{2}\rfloor\)) β€” the number of chosen vertices.In the second line print \(k\) distinct integers \(c_1, c_2, \dots, c_k\) in any order, where \(c_i\) is the index of the \(i\)-th chosen vertex.It is guaranteed that the answer exists. If ...
In the first query any vertex or any pair of vertices will suffice. Note that you don't have to minimize the number of chosen vertices. In the second query two vertices can be enough (vertices \(2\) and \(4\)) but three is also ok.
Input: 2 4 6 1 2 1 3 1 4 2 3 2 4 3 4 6 8 2 5 5 4 4 3 4 1 1 3 2 3 2 6 5 6 | Output: 2 1 3 3 4 3 6
Medium
5
525
924
366
11
274
C
274C
C. The Last Hole!
2,600
brute force; geometry
Luyi has n circles on the plane. The i-th circle is centered at (xi, yi). At the time zero circles start to grow simultaneously. In other words, the radius of each circle at time t (t > 0) is equal to t. The circles are drawn as black discs on an infinite white plane. So at each moment the plane consists of several bla...
The first line of the input contains integer n (1 ≀ n ≀ 100). Each of the next n lines contains two integers xi and yi ( - 104 ≀ xi, yi ≀ 104), indicating the location of i-th circle.It's guaranteed that no two circles are centered at the same point.
Print the moment where the last hole disappears. If there exists no moment in which we can find holes print -1.The answer will be considered correct if the absolute or relative error does not exceed 10 - 4.
Input: 30 01 12 2 | Output: -1
Expert
2
786
250
206
2
690
F2
690F2
F2. Tree of Life (medium)
2,700
constructive algorithms; hashing; trees
Heidi got tired of deciphering the prophecy hidden in the Tree of Life and decided to go back to her headquarters, rest a little and try there. Of course, she cannot uproot the Tree and take it with her, so she made a drawing of the Tree on a piece of paper. On second thought, she made more identical drawings so as to ...
The first line of the input contains Z ≀ 20 – the number of test cases. Z descriptions of single test cases follow.In each test case, the first line of input contains numbers n (2 ≀ n ≀ 100) and k (where k is the number of drawings; we have k = n). In the following lines, the descriptions of the k drawings are given. T...
If Heidi's drawings cannot possibly come from a single tree, you should output the word NO. Otherwise, output one line containing the word YES and n - 1 lines describing any tree that Heidi's drawings could have come from. For every edge you should output the numbers of the vertices that it connects, separated with a s...
Input: 15 524 12 113 134 14 32 133 13 24 132 13 24 2 | Output: YES2 54 23 25 1
Master
3
1,034
566
380
6
1,357
A6
1357A6
A6. Distinguish four Pauli gates
0
*special
You are given an operation that implements a single-qubit unitary transformation: either the identity (I gate) or one of the Pauli gates (X, Y or 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 ...
Beginner
1
812
0
0
13
1,610
D
1610D
D. Not Quite Lee
2,000
combinatorics; dp; math; number theory
Lee couldn't sleep lately, because he had nightmares. In one of his nightmares (which was about an unbalanced global round), he decided to fight back and propose a problem below (which you should solve) to balance the round, hopefully setting him free from the nightmares.A non-empty array \(b_1, b_2, \ldots, b_m\) is c...
The first line contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β€” the size of array \(a\).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) β€” elements of the array.
Print a single integer β€” the number of nonempty good subsequences of \(a\), modulo \(10^9 + 7\).
For the first test, two examples of good subsequences are \([2, 7]\) and \([2, 2, 4, 7]\):For \(b = [2, 7]\) we can use \((-3, -4)\) as the first sequence and \((-2, -1, \ldots, 4)\) as the second. Note that subsequence \([2, 7]\) appears twice in \([2, 2, 4, 7]\), so we have to count it twice. Green circles denote \((...
Input: 4 2 2 4 7 | Output: 10
Hard
4
1,078
219
96
16
545
D
545D
D. Queue
1,300
greedy; implementation; sortings
Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when...
The first line contains integer n (1 ≀ n ≀ 105).The next line contains n integers ti (1 ≀ ti ≀ 109), separated by spaces.
Print a single number β€” the maximum number of not disappointed people in the queue.
Value 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5.
Input: 515 2 1 5 3 | Output: 4
Easy
3
629
121
83
5
39
C
39C
C. Moon Craters
2,100
dp; sortings
There are lots of theories concerning the origin of moon craters. Most scientists stick to the meteorite theory, which says that the craters were formed as a result of celestial bodies colliding with the Moon. The other version is that the craters were parts of volcanoes.An extraterrestrial intelligence research specia...
The first line has an integer n (1 ≀ n ≀ 2000) β€” the number of discovered craters. The next n lines contain crater descriptions in the ""ci ri"" format, where ci is the coordinate of the center of the crater on the moon robot’s path, ri is the radius of the crater. All the numbers ci and ri are positive integers not ex...
In the first line output the number of craters in the required largest set. In the next line output space-separated numbers of craters that this set consists of. The craters are numbered from 1 to n in the order in which they were given in the input data. The numbers may be output in any order. If the result is not uni...
Input: 41 12 24 15 1 | Output: 31 2 4
Hard
2
1,722
357
336
0
1,392
A
1392A
A. Omkar and Password
800
greedy; math
Lord Omkar has permitted you to enter the Holy Church of Omkar! To test your worthiness, Omkar gives you a password which you must interpret!A password is an array \(a\) of \(n\) positive integers. You apply the following operation to the array: pick any two adjacent numbers that are not equal to each other and replace...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). Description of the test cases follows.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β€” the length of the password.The second line of each test case contains ...
For each password, print one integer: the shortest possible length of the password after some number of operations.
In the first test case, you can do the following to achieve a length of \(1\):Pick \(i=2\) to get \([2, 4, 1]\)Pick \(i=1\) to get \([6, 1]\)Pick \(i=1\) to get \([7]\)In the second test case, you can't perform any operations because there is no valid \(i\) that satisfies the requirements mentioned above.
Input: 2 4 2 1 3 1 2 420 420 | Output: 1 2
Beginner
2
882
502
115
13
1,157
F
1157F
F. Maximum Balanced Circle
2,000
constructive algorithms; dp; greedy; two pointers
There are \(n\) people in a row. The height of the \(i\)-th person is \(a_i\). You can choose any subset of these people and try to arrange them into a balanced circle.A balanced circle is such an order of people that the difference between heights of any adjacent people is no more than \(1\). For example, let heights ...
The first line of the input contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the number of people.The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 2 \cdot 10^5\)), where \(a_i\) is the height of the \(i\)-th person.
In the first line of the output print \(k\) β€” the number of people in the maximum balanced circle.In the second line print \(k\) integers \(res_1, res_2, \dots, res_k\), where \(res_j\) is the height of the \(j\)-th person in the maximum balanced circle. The condition \(|res_{j} - res_{j + 1}| \le 1\) should be satisfi...
Input: 7 4 3 5 1 2 2 1 | Output: 5 2 1 1 2 3
Hard
4
935
272
432
11
2,077
G
2077G
G. RGB Walking
3,500
bitmasks; chinese remainder theorem; dfs and similar; graphs; number theory
Red and Blue and Green - fn and Silentroomβ €You are given a connected graph with \(n\) vertices and \(m\) bidirectional edges with weight not exceeding \(x\). The \(i\)-th edge connects vertices \(u_i\) and \(v_i\), has weight \(w_i\), and is assigned a color \(c_i\) (\(1 \leq i \leq m\), \(1 \leq u_i, v_i \leq n\)). Th...
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 \(x\) (\(4 \leq n \leq 2 \cdot 10^5\), \(n-1 \leq m \leq 2 \cdot 10^5\), \(1 \leq x...
For each test case, output a single integer β€” the minimum value of \(\max(s_r, s_g, s_b) - \min(s_r, s_g, s_b)\) over all walks from vertex \(1\) to vertex \(n\).
In the first test case, the optimal path is \(1 \to 2 \to 3 \to 4\). The edges used are: \(1 \to 2\) (red, weight \(2\)) \(2 \to 3\) (green, weight \(3\)) \(3 \to 4\) (blue, weight \(2\)) We have \(s_r = 2\), \(s_g = 3\), and \(s_b = 2\). Thus, the answer is \(1\).In the second test case, one of the optimal paths is \(...
Input: 34 3 31 2 2 r2 3 3 g3 4 2 b4 5 41 2 1 r1 1 1 r2 1 1 r2 3 4 g3 4 4 b4 6 41 2 2 r1 2 2 r2 3 3 b1 3 4 r1 4 1 g3 4 4 g | Output: 1 0 0
Master
5
820
1,171
162
20
1,601
D
1601D
D. Difficult Mountain
2,700
data structures; dp; greedy; sortings
A group of \(n\) alpinists has just reached the foot of the mountain. The initial difficulty of climbing this mountain can be described as an integer \(d\).Each alpinist can be described by two integers \(s\) and \(a\), where \(s\) is his skill of climbing mountains and \(a\) is his neatness.An alpinist of skill level ...
The first line contains two integers \(n\) and \(d\) (\(1 \leq n \leq 500\,000\); \(0 \leq d \leq 10^9\)) β€” the number of alpinists and the initial difficulty of the mountain.Each of the next \(n\) lines contains two integers \(s_i\) and \(a_i\) (\(0 \leq s_i, a_i \leq 10^9\)) that define the skill of climbing and the ...
Print one integer equal to the maximum number of alpinists who can climb the mountain if they choose the right order to do so.
In the first example, alpinists \(2\) and \(3\) can climb the mountain if they go in this order. There is no other way to achieve the answer of \(2\).In the second example, alpinist \(1\) is not able to climb because of the initial difficulty of the mountain, while alpinists \(2\) and \(3\) can go up in any order.In th...
Input: 3 2 2 6 3 5 5 7 | Output: 2
Master
4
1,034
354
126
16
1,949
B
1949B
B. Charming Meals
1,500
binary search; brute force; greedy; sortings
The Czech cuisine features \(n\) appetizers and \(n\) main dishes. The \(i\)-th appetizer has spiciness \(a_i\), and the \(i\)-th main dish has spiciness \(b_i\).A typical Czech meal consists of exactly one appetizer and one main dish. You want to pair up the \(n\) appetizers and \(n\) main dishes into \(n\) meals with...
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1\le t\le 1\,000\)) β€” the number of test cases. The descriptions of the \(t\) test cases follow.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 5\,000\)) β€”the number of appetizers and main dishes.The se...
For each test case, print the largest possible value of the minimum charm you can achieve.
In the first test case, no matter how you pair up the appetizers with the main dishes, each meal will have an appetizer with spiciness \(0\) and a main dish with spiciness \(1000000000\), so the charm of each meal will be \(1000000000\).In the second test case, one optimal way to pair up appetizers and main dishes is: ...
Input: 430 0 01000000000 1000000000 100000000051 2 3 4 51 2 3 4 560 0 0 100 100 100100 100 100 0 0 0714 25 62 74 86 95 1251 62 71 72 92 20 84 | Output: 1000000000 2 100 30
Medium
4
931
718
90
19
1,856
E2
1856E2
E2. PermuTree (hard version)
2,700
bitmasks; dfs and similar; dp; fft; greedy; implementation; math; trees
This is the hard version of the problem. The differences between the two versions are the constraint on \(n\) and the time limit. You can make hacks only if both versions of the problem are solved.You are given a tree with \(n\) vertices rooted at vertex \(1\).For some permutation\(^\dagger\) \(a\) of length \(n\), let...
The first line contains a single integer \(n\) (\(2 \le n \le 10^6\)).The second line contains \(n - 1\) integers \(p_2,p_3,\ldots,p_n\) (\(1 \le p_i < i\)) indicating that there is an edge between vertices \(i\) and \(p_i\).
Output the maximum value of \(f(a)\).
The tree in the first test: One possible optimal permutation \(a\) is \([2, 1, 4, 5, 3]\) with \(4\) suitable pairs of vertices: \((2, 3)\), since \(\operatorname{lca}(2, 3) = 1\) and \(1 < 2 < 4\), \((2, 4)\), since \(\operatorname{lca}(2, 4) = 1\) and \(1 < 2 < 5\), \((2, 5)\), since \(\operatorname{lca}(2, 5) = 1\) ...
Input: 5 1 1 3 3 | Output: 4
Master
8
954
225
37
18
667
A
667A
A. Pouring Rain
1,100
geometry; math
A lot of people in Berland hates rain, but you do not. Rain pacifies, puts your thoughts in order. By these years you have developed a good tradition β€” when it rains, you go on the street and stay silent for a moment, contemplate all around you, enjoy freshness, think about big deeds you have to do. Today everything ha...
The only line of the input contains four integer numbers d, h, v, e (1 ≀ d, h, v, e ≀ 104), where: d β€” the diameter of your cylindrical cup, h β€” the initial level of water in the cup, v β€” the speed of drinking process from the cup in milliliters per second, e β€” the growth of water because of rain if you do not drink fr...
If it is impossible to make the cup empty, print ""NO"" (without quotes).Otherwise print ""YES"" (without quotes) in the first line. In the second line print a real number β€” time in seconds needed the cup will be empty. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 4. It is...
In the first example the water fills the cup faster than you can drink from it.In the second example area of the cup's bottom equals to , thus we can conclude that you decrease the level of water by centimeters per second. At the same time water level increases by 1 centimeter per second due to rain. Thus, cup will be ...
Input: 1 2 3 100 | Output: NO
Easy
2
1,353
331
381
6
342
C
342C
C. Cupboard and Balloons
1,900
geometry
A girl named Xenia has a cupboard that looks like an arc from ahead. The arc is made of a semicircle with radius r (the cupboard's top) and two walls of height h (the cupboard's sides). The cupboard's depth is r, that is, it looks like a rectangle with base r and height h + r from the sides. The figure below shows what...
The single line contains two integers r, h (1 ≀ r, h ≀ 107).
Print a single integer β€” the maximum number of balloons Xenia can put in the cupboard.
Input: 1 1 | Output: 3
Hard
1
955
60
86
3
1,814
C
1814C
C. Search in Parallel
1,500
constructive algorithms; greedy; sortings
Suppose you have \(n\) boxes. The \(i\)-th box contains infinitely many balls of color \(i\). Sometimes you need to get a ball with some specific color; but you're too lazy to do it yourself.You have bought two robots to retrieve the balls for you. Now you have to program them. In order to program the robots, you have ...
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.Each test case consists of two lines: the first line contains three integers \(n\), \(s_1\), \(s_2\) (\(2 \le n \le 2 \cdot 10^5\); \(1 \le s_1, s_2 \le 10\)); the second line contains \(n\) integers \(r_1, r_2, \dots, r_n\) (\(...
For each test case, print two lines. The first line should contain the list \(a\), the second line β€” the list \(b\). Each list has to be printed as follows: first, print the number of elements in it, and then the elements themselves.If there are multiple answers, you may print any of them.
Input: 37 3 18 6 4 4 4 1 75 1 101 1 1 1 18 1 14 5 6 8 1 7 3 2 | Output: 2 5 6 5 1 7 2 4 3 5 4 3 5 2 1 0 4 4 2 7 5 4 6 3 1 8
Medium
3
2,770
449
290
18
1,066
B
1066B
B. Heaters
1,500
greedy; two pointers
Vova's house is an array consisting of \(n\) elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The \(i\)-th element of the array is \(1\) if there is a heater in the position \(i\), otherwise the \(i\)-th element of the array is \(0\...
The first line of the input contains two integers \(n\) and \(r\) (\(1 \le n, r \le 1000\)) β€” the number of elements in the array and the value of heaters.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i \le 1\)) β€” the Vova's house description.
Print one integer β€” the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
In the first example the heater at the position \(2\) warms up elements \([1; 3]\), the heater at the position \(3\) warms up elements \([2, 4]\) and the heater at the position \(6\) warms up elements \([5; 6]\) so the answer is \(3\).In the second example the heater at the position \(1\) warms up elements \([1; 3]\) a...
Input: 6 20 1 1 0 0 1 | Output: 3
Medium
2
1,462
273
119
10
750
E
750E
E. New Year and Old Subsequence
2,600
data structures; divide and conquer; dp; matrices
A string t is called nice if a string ""2017"" occurs in t as a subsequence but a string ""2016"" doesn't occur in t as a subsequence. For example, strings ""203434107"" and ""9220617"" are nice, while strings ""20016"", ""1234"" and ""20167"" aren't nice.The ugliness of a string is the minimum possible number of chara...
The first line of the input contains two integers n and q (4 ≀ n ≀ 200 000, 1 ≀ q ≀ 200 000) β€” the length of the string s and the number of queries respectively.The second line contains a string s of length n. Every character is one of digits '0'–'9'.The i-th of next q lines contains two integers ai and bi (1 ≀ ai ≀ bi...
For each query print the ugliness of the given substring.
In the first sample: In the first query, ugliness(""20166766"") = 4 because all four sixes must be removed. In the second query, ugliness(""2016676"") = 3 because all three sixes must be removed. In the third query, ugliness(""0166766"") = - 1 because it's impossible to remove some digits to get a nice string. In the s...
Input: 8 3201667661 81 72 8 | Output: 43-1
Expert
4
719
368
57
7
1,286
C2
1286C2
C2. Madhouse (Hard version)
2,800
brute force; constructive algorithms; hashing; interactive; math
This problem is different with easy version only by constraints on total answers lengthIt is an interactive problemVenya joined a tour to the madhouse, in which orderlies play with patients the following game. Orderlies pick a string \(s\) of length \(n\), consisting only of lowercase English letters. The player can as...
First line contains number \(n\) (\(1 \le n \le 100\)) β€” the length of the picked string.
Input: 4 a aa a cb b c c | Output: ? 1 2 ? 3 4 ? 4 4 ! aabc
Master
5
1,513
89
0
12
1,420
E
1420E
E. Battle Lemmings
2,500
dp; greedy
A lighthouse keeper Peter commands an army of \(n\) battle lemmings. He ordered his army to stand in a line and numbered the lemmings from \(1\) to \(n\) from left to right. Some of the lemmings hold shields. Each lemming cannot hold more than one shield.The more protected Peter's army is, the better. To calculate the ...
First line contains a single integer \(n\) (\(1 \le n \le 80\)), the number of lemmings in Peter's army.Second line contains \(n\) integers \(a_i\) (\(0 \le a_i \le 1\)). If \(a_i = 1\), then the \(i\)-th lemming has a shield, otherwise \(a_i = 0\).
Print \(\frac{n(n-1)}2 + 1\) numbers, the greatest possible protection after no more than \(0, 1, \dots, \frac{n(n-1)}2\) orders.
Consider the first example.The protection is initially equal to zero, because for each pair of lemmings without shields there is no lemmings with shield.In one second Peter can order the first lemming give his shield to the right neighbor. In this case, the protection is two, as there are two protected pairs of lemming...
Input: 5 1 0 0 0 1 | Output: 0 2 3 3 3 3 3 3 3 3 3
Expert
2
1,142
249
129
14
1,905
C
1905C
C. Largest Subsequence
1,400
greedy; strings
Given is a string \(s\) of length \(n\). In one operation you can select the lexicographically largest\(^\dagger\) subsequence of string \(s\) and cyclic shift it to the right\(^\ddagger\). Your task is to calculate the minimum number of operations it would take for \(s\) to become sorted, or report that it never reach...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the length of the string \(s\).The seco...
For each test case, output a single integer β€” the minimum number of operations required to make \(s\) sorted, or \(-1\) if it's impossible.
In the first test case, the string \(s\) is already sorted, so we need no operations.In the second test case, doing one operation, we will select cb and cyclic shift it. The string \(s\) is now abc which is sorted.In the third test case, \(s\) cannot be sorted.In the fourth test case we will perform the following opera...
Input: 65aaabc3acb3bac4zbca15czddeneeeemigec13cdefmopqsvxzz | Output: 0 1 -1 2 6 0
Easy
2
781
522
139
19
1,334
A
1334A
A. Level Statistics
1,200
implementation; math
Polycarp has recently created a new level in this cool new game Berlio Maker 85 and uploaded it online. Now players from all over the world can try his level.All levels in this game have two stats to them: the number of plays and the number of clears. So when a player attempts the level, the number of plays increases b...
The first line contains a single integer \(T\) \((1 \le T \le 500)\) β€” the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 100\)) β€” the number of moments of time Polycarp peeked at the stats.Each of the next \(n\) lines contains two integers \(p_i\) and \(c_i\) (\(0 ...
For each test case print a single line.If there could exist such a sequence of plays (and clears, respectively) that the stats were exactly as Polycarp has written down, then print ""YES"".Otherwise, print ""NO"".You can print each letter in any case (upper or lower).
In the first test case at the third moment of time the number of clears increased but the number of plays did not, that couldn't have happened.The second test case is a nice example of a Super Expert level.In the third test case the number of plays decreased, which is impossible.The fourth test case is probably an auto...
Input: 6 3 0 0 1 1 1 2 2 1 0 1000 3 4 10 1 15 2 10 2 15 2 1 765 432 2 4 4 4 3 5 0 0 1 0 1 0 1 0 1 0 | Output: NO YES NO YES NO YES
Easy
2
1,626
489
268
13
1,965
A
1965A
A. Everything Nim
1,400
games; greedy; math; sortings
Alice and Bob are playing a game on \(n\) piles of stones. On each player's turn, they select a positive integer \(k\) that is at most the size of the smallest nonempty pile and remove \(k\) stones from each nonempty pile at once. The first player who is unable to make a move (because all piles are empty) loses.Given t...
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 the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2\cdot 10^5\)) β€” the number of piles in the game.The next line of each test case contai...
For each test case, print a single line with the name of the winner, assuming both players play optimally. If Alice wins, print ""Alice"", otherwise print ""Bob"" (without quotes).
In the first test case, Alice can win by choosing \(k=3\) on her first turn, which will empty all of the piles at once.In the second test case, Alice must choose \(k=1\) on her first turn since there is a pile of size \(1\), so Bob can win on the next turn by choosing \(k=6\).
Input: 753 3 3 3 321 771 3 9 7 4 2 10031 2 362 1 3 4 2 485 7 2 9 6 3 3 211000000000 | Output: Alice Bob Alice Alice Bob Alice Alice
Easy
4
395
547
180
19
464
E
464E
E. The Classic Problem
3,000
data structures; graphs; shortest paths
You are given a weighted undirected graph on n vertices and m edges. Find the shortest path from vertex s to vertex t or else state that such path doesn't exist.
The first line of the input contains two space-separated integers β€” n and m (1 ≀ n ≀ 105; 0 ≀ m ≀ 105).Next m lines contain the description of the graph edges. The i-th line contains three space-separated integers β€” ui, vi, xi (1 ≀ ui, vi ≀ n; 0 ≀ xi ≀ 105). That means that vertices with numbers ui and vi are connected...
In the first line print the remainder after dividing the length of the shortest path by 1000000007 (109 + 7) if the path exists, and -1 if the path doesn't exist.If the path exists print in the second line integer k β€” the number of vertices in the shortest path from vertex s to vertex t; in the third line print k space...
A path from vertex s to vertex t is a sequence v0, ..., vk, such that v0 = s, vk = t, and for any i from 0 to k - 1 vertices vi and vi + 1 are connected by an edge. The length of the path is the sum of weights of edges between vi and vi + 1 for all i from 0 to k - 1. The shortest path from s to t is the path which leng...
Input: 4 41 4 21 2 02 3 03 4 01 4 | Output: 341 2 3 4
Master
3
161
543
528
4
901
C
901C
C. Bipartite Segments
2,300
binary search; data structures; dfs and similar; dsu; graphs; two pointers
You are given an undirected graph with n vertices. There are no edge-simple cycles with the even length in it. In other words, there are no cycles of even length that pass each edge at most once. Let's enumerate vertices from 1 to n. You have to answer q queries. Each query is described by a segment of vertices [l; r],...
The first line contains two integers n and m (1 ≀ n ≀ 3Β·105, 1 ≀ m ≀ 3Β·105) β€” the number of vertices and the number of edges in the graph.The next m lines describe edges in the graph. The i-th of these lines contains two integers ai and bi (1 ≀ ai, bi ≀ n; ai β‰  bi), denoting an edge between vertices ai and bi. It is gu...
Print q numbers, each in new line: the i-th of them should be the number of subsegments [x; y] (li ≀ x ≀ y ≀ ri), such that the graph that only includes vertices from segment [x; y] and edges between them is bipartite.
The first example is shown on the picture below:For the first query, all subsegments of [1; 3], except this segment itself, are suitable.For the first query, all subsegments of [4; 6], except this segment itself, are suitable.For the third query, all subsegments of [1; 6] are suitable, except [1; 3], [1; 4], [1; 5], [1...
Input: 6 61 22 33 14 55 66 431 34 61 6 | Output: 5514
Expert
6
548
609
218
9
1,725
L
1725L
L. Lemper Cooking Competition
2,400
data structures
Pak Chanek is participating in a lemper cooking competition. In the competition, Pak Chanek has to cook lempers with \(N\) stoves that are arranged sequentially from stove \(1\) to stove \(N\). Initially, stove \(i\) has a temperature of \(A_i\) degrees. A stove can have a negative temperature.Pak Chanek realises that,...
The first line contains a single integer \(N\) (\(1 \le N \le 10^5\)) β€” the number of stoves.The second line contains \(N\) integers \(A_1, A_2, \ldots, A_N\) (\(-10^9 \leq A_i \leq 10^9\)) β€” the initial temperatures of the stoves.
Output an integer representing the minimum number of operations needed to make the temperatures of all stoves at non-negative values or output \(-1\) if it is not possible.
For the first example, a sequence of operations that can be done is as follows: Pak Chanek does an operation to stove \(3\), \(A = [2, -2, 1, 4, 2, -2, 9]\). Pak Chanek does an operation to stove \(2\), \(A = [0, 2, -1, 4, 2, -2, 9]\). Pak Chanek does an operation to stove \(3\), \(A = [0, 1, 1, 3, 2, -2, 9]\). Pak Cha...
Input: 7 2 -1 -1 5 2 -2 9 | Output: 4
Expert
1
1,059
231
172
17
187
D
187D
D. BRT Contract
2,800
data structures
In the last war of PMP, he defeated all his opponents and advanced to the final round. But after the end of semi-final round evil attacked him from behind and killed him! God bless him. Before his death, PMP signed a contract with the bus rapid transit (BRT) that improves public transportations by optimizing time of tr...
The first line of input contains three space-separated positive integers n, g, r (1 ≀ n ≀ 105, 2 ≀ g + r ≀ 109) β€” the number of intersections, duration of green phase and duration of red phase. Next line contains n + 1 integers li (1 ≀ li ≀ 109) β€” the time to pass the i-th road segment in the path from source to destin...
In the i-th line of output you should print a single integer β€” the time that i-th bus gets to destination.Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
In the first sample, buses #1, #2 and #5 will reach the destination without waiting behind the red light. But buses #3 and #4 should wait.In the second sample, first bus should wait at third, fourth and fifth intersections. Second and third buses should wait only at the fifth intersection.
Input: 1 3 25 2512345 | Output: 89121212
Master
1
2,075
531
248
1
1,575
K
1575K
K. Knitting Batik
2,200
implementation; math
Mr. Chanek wants to knit a batik, a traditional cloth from Indonesia. The cloth forms a grid \(a\) with size \(n \times m\). There are \(k\) colors, and each cell in the grid can be one of the \(k\) colors.Define a sub-rectangle as an ordered pair of two cells \(((x_1, y_1), (x_2, y_2))\), denoting the top-left cell an...
The first line contains five integers \(n\), \(m\), \(k\), \(r\), and \(c\) (\(1 \leq n, m \leq 10^9\), \(1 \leq k \leq 10^9\), \(1 \leq r \leq \min(10^6, n)\), \(1 \leq c \leq \min(10^6, m)\)) β€” the size of the batik, the number of colors, and size of the sub-rectangle.The second line contains four integers \(a_x\), \...
Output an integer denoting the number of possible batik color combinations modulo \(10^9 + 7\).
The following are all \(32\) possible color combinations in the first example.
Input: 3 3 2 2 2 1 1 2 2 | Output: 32
Hard
2
1,039
618
95
15
1,879
F
1879F
F. Last Man Standing
2,800
brute force; data structures; number theory
There are \(n\) heroes in a videogame. Each hero has some health value \(h\) and initial armor value \(a\). Let the current value of armor be \(a_{\mathit{cur}}\), initially equal to \(a\).When \(x\) points of damage are inflicted on a hero, the following happens: if \(x < a_{\mathit{cur}}\), then \(x\) gets subtracted...
The first line contains a single integer \(t\) (\(1 \le t \le 10\)) β€” the number of testcases.The first line of each testcase contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the number of heroes.The second line contains \(n\) integers \(h_1, h_2, \dots, h_n\) (\(1 \le h_i \le 2 \cdot 10^5\)) β€” the heal...
For each testcase, print \(n\) integers β€” the maximum number of points each hero has had over the games played for every possible \(x\).
In the first testcase, the game for \(x = 1\) is played as follows: before all rounds: the heroes have \(h = [3, 1, 2]\), \(a_{\mathit{cur}} = [3, 11, 5]\); round \(1\): \(1\) damage is inflicted on every hero: \(h\) remains \([3, 1, 2]\), \(a_{\mathit{cur}}\) becomes \([2, 10, 4]\); round \(2\): \(h = [3, 1, 2]\), \(a...
Input: 333 1 23 11 5110020045 9 5 19 2 9 10 | Output: 1 1 1 20000 0 4 0 0
Master
3
1,163
478
136
18
2,026
A
2026A
A. Perpendicular Segments
900
constructive algorithms; geometry; greedy; math
You are given a coordinate plane and three integers \(X\), \(Y\), and \(K\). Find two line segments \(AB\) and \(CD\) such that the coordinates of points \(A\), \(B\), \(C\), and \(D\) are integers; \(0 \le A_x, B_x, C_x, D_x \le X\) and \(0 \le A_y, B_y, C_y, D_y \le Y\); the length of segment \(AB\) is at least \(K\)...
The first line contains a single integer \(t\) (\(1 \le t \le 5000\)) β€” the number of test cases. Next, \(t\) cases follow.The first and only line of each test case contains three integers \(X\), \(Y\), and \(K\) (\(1 \le X, Y \le 1000\); \(1 \le K \le 1414\)).Additional constraint on the input: the values of \(X\), \(...
For each test case, print two lines. The first line should contain \(4\) integers \(A_x\), \(A_y\), \(B_x\), and \(B_y\) β€” the coordinates of the first segment.The second line should also contain \(4\) integers \(C_x\), \(C_y\), \(D_x\), and \(D_y\) β€” the coordinates of the second segment.If there are multiple answers,...
The answer for the first test case is shown below: The answer for the second test case: The answer for the third test case: The answer for the fourth test case:
Input: 41 1 13 4 14 3 33 4 4 | Output: 0 0 1 0 0 0 0 1 2 4 2 2 0 1 1 1 0 0 1 3 1 2 4 1 0 1 3 4 0 3 3 0
Beginner
4
634
383
339
20
207
D9
207D9
D9. The Beaver's Problem - 3
1,800
The Smart Beaver from ABBYY came up with another splendid problem for the ABBYY Cup participants! This time the Beaver invites the contest participants to check out a problem on sorting documents by their subjects. Let's describe the problem:You've got some training set of documents. For each document you know its subj...
The first line contains integer id (0 ≀ id ≀ 106) β€” the document identifier. The second line contains the name of the document. The third and the subsequent lines contain the text of the document. It is guaranteed that the size of any given document will not exceed 10 kilobytes.The tests for this problem are divided in...
Print an integer from 1 to 3, inclusive β€” the number of the subject the given document corresponds to.
Medium
0
1,472
653
102
2
599
D
599D
D. Spongebob and Squares
1,900
brute force; math
Spongebob is already tired trying to reason his weird actions and calculations, so he simply asked you to find all pairs of n and m, such that there are exactly x distinct squares in the table consisting of n rows and m columns. For example, in a 3 Γ— 5 table there are 15 squares with side one, 8 squares with side two a...
The first line of the input contains a single integer x (1 ≀ x ≀ 1018) β€” the number of squares inside the tables Spongebob is interested in.
First print a single integer k β€” the number of tables with exactly x distinct squares inside.Then print k pairs of integers describing the tables. Print the pairs in the order of increasing n, and in case of equality β€” in the order of increasing m.
In a 1 Γ— 2 table there are 2 1 Γ— 1 squares. So, 2 distinct squares in total. In a 2 Γ— 3 table there are 6 1 Γ— 1 squares and 2 2 Γ— 2 squares. That is equal to 8 squares in total.
Input: 26 | Output: 61 262 93 55 39 226 1
Hard
2
423
140
248
5
171
C
171C
C. A Piece of Cake
2,000
*special; implementation
How to make a cake you'll never eat.Ingredients. 2 carrots 0 calories 100 g chocolate spread 1 pack of flour 1 egg Method. Put calories into the mixing bowl. Take carrots from refrigerator. Chop carrots. Take chocolate spread from refrigerator. Put chocolate spread into the mixing bowl. Combine pack of flour into the m...
The only line of input contains a sequence of integers a0, a1, ... (1 ≀ a0 ≀ 100, 0 ≀ ai ≀ 1000 for i β‰₯ 1).
Output a single integer.
Input: 4 1 2 3 4 | Output: 30
Hard
2
621
107
24
1
125
E
125E
E. MST Company
2,400
binary search; graphs
The MST (Meaningless State Team) company won another tender for an important state reform in Berland.There are n cities in Berland, some pairs of the cities are connected by roads. Each road has its price. One can move along any road in any direction. The MST team should carry out the repair works on some set of roads ...
The first input line contains three integers n, m, k (1 ≀ n ≀ 5000;0 ≀ m ≀ 105;0 ≀ k < 5000), where n is the number of cities in the country, m is the number of roads in the country, k is the number of capital roads in the required set. Then m lines enumerate the roads in question. Each road is specified by three numbe...
In the first line print the number of roads in the required set. The second line should contain the numbers of roads included in the sought set. If the sought set does not exist, print -1.
Input: 4 5 21 2 12 3 13 4 11 3 31 4 2 | Output: 31 5 2
Expert
2
676
581
188
1
286
B
286B
B. Shifting
2,200
implementation
John Doe has found the beautiful permutation formula.Let's take permutation p = p1, p2, ..., pn. Let's define transformation f of this permutation: where k (k > 1) is an integer, the transformation parameter, r is such maximum integer that rk ≀ n. If rk = n, then elements prk + 1, prk + 2 and so on are omitted. In othe...
A single line contains integer n (2 ≀ n ≀ 106).
Print n distinct space-separated integers from 1 to n β€” a beautiful permutation of size n.
A note to the third test sample: f([1, 2, 3, 4], 2) = [2, 1, 4, 3] f([2, 1, 4, 3], 3) = [1, 4, 2, 3] f([1, 4, 2, 3], 4) = [4, 2, 3, 1]
Input: 2 | Output: 2 1
Hard
1
855
47
90
2
1,490
D
1490D
D. Permutation Transformation
1,200
dfs and similar; divide and conquer; implementation
A permutation β€” is a sequence of length \(n\) integers from \(1\) to \(n\), in which all the numbers occur exactly once. For example, \([1]\), \([3, 5, 2, 1, 4]\), \([1, 3, 2]\) β€” permutations, and \([2, 3, 2]\), \([4, 3, 1]\), \([0]\) β€” no.Polycarp was recently gifted a permutation \(a[1 \dots n]\) of length \(n\). Po...
The first line contains one integer \(t\) (\(1 \le t \le 100\)) β€” the number of test cases. Then \(t\) test cases follow.The first line of each test case contains an integer \(n\) (\(1 \le n \le 100\)) β€” the length of the permutation.This is followed by \(n\) numbers \(a_1, a_2, \ldots, a_n\) β€” permutation \(a\).
For each test case, output \(n\) values β€” \(d_1, d_2, \ldots, d_n\).
Input: 3 5 3 5 2 1 4 1 1 4 4 3 1 2 | Output: 1 0 2 3 1 0 0 1 3 2
Easy
3
1,874
314
68
14
2,096
C
2096C
C. Wonderful City
1,700
dp; implementation
You are the proud leader of a city in Ancient Berland. There are \(n^2\) buildings arranged in a grid of \(n\) rows and \(n\) columns. The height of the building in row \(i\) and column \(j\) is \(h_{i, j}\).The city is beautiful if no two adjacent by side buildings have the same height. In other words, it must satisfy...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(2 \le n \le 1000\)) β€” the size of the grid.The \(i\)-th of the next \(n\) lines of each tes...
For each test case, output a single integer β€” the minimum number of coins needed, or \(-1\) if it is impossible.
For the first test case, we can see that the city is already beautiful. Thus, the answer is \(0\).For the second test case, we can hire worker \(2\) from company A, worker \(4\) from company A, and worker \(4\) from company B:\(1\)\(2\)\(1\)\(\color{red}2\)\(\implies\)\(1\)\(2\)\(1\)\(\color{red}3\)\(\color{red}3\)\(\c...
Input: 421 22 1100 100100 10041 2 1 23 2 1 21 2 1 11 3 1 21 2 3 45 6 7 831 2 22 2 12 1 1100 100 100100 100 10068 7 2 8 4 87 7 9 7 1 18 3 1 1 8 56 8 3 1 1 41 4 5 1 9 67 1 1 6 8 211 23 20 79 30 1515 83 73 57 34 63 | Output: 0 14 -1 183
Medium
2
1,261
858
112
20
1,535
D
1535D
D. Playoff Tournament
1,800
data structures; dfs and similar; dp; implementation; trees
\(2^k\) teams participate in a playoff tournament. The tournament consists of \(2^k - 1\) games. They are held as follows: first of all, the teams are split into pairs: team \(1\) plays against team \(2\), team \(3\) plays against team \(4\) (exactly in this order), and so on (so, \(2^{k-1}\) games are played in that p...
The first line contains one integer \(k\) (\(1 \le k \le 18\)).The second line contains a string consisting of \(2^k - 1\) characters β€” the initial state of the string \(s\). Each character is either ?, 0, or 1.The third line contains one integer \(q\) (\(1 \le q \le 2 \cdot 10^5\)) β€” the number of queries.Then \(q\) l...
For each query, print one integer β€” \(f(s)\).
Input: 3 0110?11 6 5 1 6 ? 7 ? 1 ? 5 ? 1 1 | Output: 1 2 3 3 5 4
Medium
5
1,782
484
45
15
1,182
D
1182D
D. Complete Mirror
2,400
constructive algorithms; dfs and similar; dp; hashing; implementation; trees
You have given tree consist of \(n\) vertices. Select a vertex as root vertex that satisfies the condition below. For all vertices \(v_{1}\) and \(v_{2}\), if \(distance\)(\(root\), \(v_{1}\)) \(= distance\)(\(root\), \(v_{2})\) then \(degree\)(\(v_{1}\)) \(= degree\)(\(v_{2}\)), where \(degree\) means the number of ve...
The first line contains a single integer \(n\) (\(1 \le n \le 10^{5}\)) β€” the number of vertices.Each of the next \(n-1\) lines contains two integers \(v_{i}\) and \(u_{i}\) (\(1 \le v_{i} \lt u_{i} \le n\)) β€” it means there is an edge exist between \(v_{i}\) and \(u_{i}\). It is guaranteed that the graph forms tree.
If there is such root vertex exists, print any of them. Otherwise, print \(-1\).
This is the picture for the first example. \(1\), \(5\), \(7\) also can be a valid answer. This is the picture for the second example. You can see that it's impossible to find such root vertex.
Input: 7 1 2 2 3 3 4 4 5 3 6 6 7 | Output: 3
Expert
6
527
318
80
11
1,625
E2
1625E2
E2. Cats on the Upgrade (hard version)
2,800
binary search; data structures; dfs and similar; graphs; trees
This is the hard version of the problem. The only difference between the easy and the hard versions are removal queries, they are present only in the hard version.""Interplanetary Software, Inc."" together with ""Robots of Cydonia, Ltd."" has developed and released robot cats. These electronic pets can meow, catch mice...
The first line contains two integers \(n\) and \(q\) (\(2 \le n \le 3\cdot10^5\), \(1 \le q \le 3\cdot10^5\)), the length of the string, and the number of queries.The second line contains the string \(s\), consisting of \(n\) characters ""("" and "")"".Each of the following \(q\) lines contains three integers \(t\), \(...
For each query, print a single integer in a separate line, the number of substrings that are simple RBS. The answers must be printed in the same order as the queries are specified in the input.
Consider the example test case.The answer to the first query is \(3\), as there are three suitable substrings: \(s[3\dots6]\), \(s[3\dots4]\) and \(s[5\dots6]\).The answer to the second query is \(4\). The substrings are \(s[3\dots6]\), \(s[3\dots4]\), \(s[5\dots6]\) and \(s[2\dots7]\).After the third query, the string...
Input: 9 8 )(()())() 2 3 6 2 2 7 1 3 4 2 2 7 2 2 9 1 5 6 1 2 7 2 8 9 | Output: 3 4 2 4 1
Master
5
2,570
500
193
16
93
E
93E
E. Lostborn
2,600
dp; math; number theory
Igor K. very much likes a multiplayer role playing game WineAge II. Who knows, perhaps, that might be the reason for his poor performance at the university. As any person who plays the game, he is interested in equipping his hero with as good weapon and outfit as possible. One day, as he was reading the game's forum ye...
The first line contains two integers: n and k (1 ≀ n ≀ 1013, 1 ≀ k ≀ 100). They are the indicator of Igor K's hero's strength and the number of hit indicators.The next line contains space-separated k integers ai (1 ≀ ai ≀ 1000). They are Lostborn sword's hit indicators. The given k numbers are pairwise coprime.
Print the single number β€” the damage that will be inflicted by Igor K.'s hero when he uses his new weapon. Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specificator.
Input: 20 32 3 5 | Output: 6
Expert
3
1,031
312
260
0
1,443
B
1443B
B. Saving the City
1,300
dp; greedy; math; sortings
Bertown is a city with \(n\) buildings in a straight line.The city's security service discovered that some buildings were mined. A map was compiled, which is a string of length \(n\), where the \(i\)-th character is ""1"" if there is a mine under the building number \(i\) and ""0"" otherwise.Bertown's best sapper knows...
The first line contains one positive integer \(t\) (\(1 \le t \le 10^5\)) β€” the number of test cases. Then \(t\) test cases follow.Each test case begins with a line containing two integers \(a\) and \(b\) (\(1 \le a, b \le 1000\)) β€” the cost of activating and placing one mine, respectively.The next line contains a map ...
For each test case, output one integer β€” the minimum number of coins that the sapper will have to pay.
In the second test case, if we place a mine under the fourth building and then activate it, then all mines on the field are activated. The cost of such operations is six, \(b=1\) coin for placing a mine and \(a=5\) coins for activating.
Input: 2 1 1 01000010 5 1 01101110 | Output: 2 6
Easy
4
1,268
455
102
14
183
D
183D
D. T-shirt
2,700
dp; greedy; probabilities
You are going to work in Codeforces as an intern in a team of n engineers, numbered 1 through n. You want to give each engineer a souvenir: a T-shirt from your country (T-shirts are highly desirable in Codeforces). Unfortunately you don't know the size of the T-shirt each engineer fits in. There are m different sizes, ...
The first line contains two space-separated integers n and m (1 ≀ n ≀ 3000, 1 ≀ m ≀ 300), denoting the number of engineers and the number of T-shirt sizes, respectively.Then n lines follow, each line contains m space-separated integers. The j-th integer in the i-th line represents the probability that the i-th engineer...
Print a single real number denoting the maximum possible expected number of engineers that will receive a T-shirt.For the answer the absolute or relative error of 10 - 9 is acceptable.
For the first example, bring one T-shirt of each size. With 0.5 chance, either both engineers fit inside T-shirts of size 1 or both fit inside T-shirts of size 2. With the other 0.5 chance, one engineer fits inside a T-shirt of size 1 and the other inside a T-shirt of size 2. If the first is true, the number of enginee...
Input: 2 2500 500500 500 | Output: 1.500000000000
Master
3
1,544
611
184
1
1,875
A
1875A
A. Jellyfish and Undertale
900
brute force; greedy
Flowey has planted a bomb in Snowdin!The bomb has a timer that is initially set to \(b\). Every second, the timer will decrease by \(1\). When the timer reaches \(0\), the bomb will explode! To give the residents of Snowdin enough time to evacuate, you will need to delay the bomb from exploding for as long as possible....
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 2000\)). The description of the test cases follows.The first line of each test case contains three integers \(a\), \(b\) and \(n\) (\(1 \leq b \leq a \leq 10^9\), \(1 \leq n \leq 100\)) β€” the maximum value of...
For each test case, output a single integer β€” the maximum time in seconds until the bomb explodes.
Let \(c\) denote the value of the bomb's timer. In the first test case: Second \(1\): choose tool \(1\) and \(2\) at this second, then \(c=5\); the timer decreases by \(1\), then \(c=4\). Second \(2\): the timer decreases by \(1\), then \(c=3\). Second \(3\): the timer decreases by \(1\), then \(c=2\). Second \(4\): th...
Input: 25 3 31 1 77 1 51 2 5 6 8 | Output: 9 21
Beginner
2
1,052
636
98
18
438
E
438E
E. The Child and Binary Tree
3,100
combinatorics; divide and conquer; fft; number theory
Our child likes computer science very much, especially he likes binary trees.Consider the sequence of n distinct positive integers: c1, c2, ..., cn. The child calls a vertex-weighted rooted binary tree good if and only if for every vertex v, the weight of v is in the set {c1, c2, ..., cn}. Also our child thinks that th...
The first line contains two integers n, m (1 ≀ n ≀ 105; 1 ≀ m ≀ 105). The second line contains n space-separated pairwise distinct integers c1, c2, ..., cn. (1 ≀ ci ≀ 105).
Print m lines, each line containing a single integer. The i-th line must contain the number of good vertex-weighted rooted binary trees whose weight exactly equal to i. Print the answers modulo 998244353 (7 Γ— 17 Γ— 223 + 1, a prime number).
In the first example, there are 9 good vertex-weighted rooted binary trees whose weight exactly equal to 3:
Input: 2 31 2 | Output: 139
Master
4
692
172
239
4
925
B
925B
B. Resource Distribution
1,700
binary search; implementation; sortings
One department of some software company has \(n\) servers of different specifications. Servers are indexed with consecutive integers from \(1\) to \(n\). Suppose that the specifications of the \(j\)-th server may be expressed with a single integer number \(c_j\) of artificial resource units.In order for production to w...
The first line contains three integers \(n\), \(x_1\), \(x_2\) (\(2 \leq n \leq 300\,000\), \(1 \leq x_1, x_2 \leq 10^9\)) β€” the number of servers that the department may use, and resource units requirements for each of the services.The second line contains \(n\) space-separated integers \(c_1, c_2, \ldots, c_n\) (\(1 ...
If it is impossible to deploy both services using the given servers, print the only word ""No"" (without the quotes).Otherwise print the word ""Yes"" (without the quotes). In the second line print two integers \(k_1\) and \(k_2\) (\(1 \leq k_1, k_2 \leq n\)) β€” the number of servers used for each of the services.In the ...
In the first sample test each of the servers 1, 2 and 6 will will provide \(8 / 3 = 2.(6)\) resource units and each of the servers 5, 4 will provide \(16 / 2 = 8\) resource units.In the second sample test the first server will provide \(20\) resource units and each of the remaining servers will provide \(32 / 3 = 10.(6...
Input: 6 8 163 5 2 9 8 7 | Output: Yes3 21 2 65 4
Medium
3
1,253
405
684
9
2,041
M
2041M
M. Selection Sort
2,000
binary search; data structures; greedy; two pointers
Every student enrolled in the algorithms course is required to submit an assignment this week. The task is to implement an \(O(n^2)\)-time algorithm to sort \(n\) given integers in non-decreasing order. Alice has already completed her assignment, and her implementation is shown below. int alice_sort(int *s, int n){ for...
The first line contains exactly one integer \(n\) which indicates the number of integers in the array \(s\). The second line contains the \(n\) integers in \(s=[s_0, s_1, \ldots, s_{n-1}]\). \(1 \le n \le 10^6\) For all \(i\) (\(0\le i < n\)), \(0\le s_i < 2^{31}-1\).
Output an integer on a line, indicating the minimum cost to sort the input array \(s\) of \(n\) integers in non-decreasing order using Alice's function, following the rules mentioned above.
Input: 6 3 2 5 5 4 1 | Output: 25
Hard
4
1,897
268
189
20
169
A
169A
A. Chores
800
sortings
Petya and Vasya are brothers. Today is a special day for them as their parents left them home alone and commissioned them to do n chores. Each chore is characterized by a single parameter β€” its complexity. The complexity of the i-th chore equals hi.As Petya is older, he wants to take the chores with complexity larger t...
The first input line contains three integers n, a and b (2 ≀ n ≀ 2000; a, b β‰₯ 1; a + b = n) β€” the total number of chores, the number of Petya's chores and the number of Vasya's chores.The next line contains a sequence of integers h1, h2, ..., hn (1 ≀ hi ≀ 109), hi is the complexity of the i-th chore. The numbers in the...
Print the required number of ways to choose an integer value of x. If there are no such ways, print 0.
In the first sample the possible values of x are 3, 4 or 5.In the second sample it is impossible to find such x, that Petya got 3 chores and Vasya got 4.
Input: 5 2 36 2 3 100 1 | Output: 3
Beginner
1
654
422
102
1
433
A
433A
A. Kitahara Haruki's Gift
1,100
brute force; implementation
Kitahara Haruki has bought n apples for Touma Kazusa and Ogiso Setsuna. Now he wants to divide all the apples between the friends.Each apple weights 100 grams or 200 grams. Of course Kitahara Haruki doesn't want to offend any of his friend. Therefore the total weight of the apples given to Touma Kazusa must be equal to...
The first line contains an integer n (1 ≀ n ≀ 100) β€” the number of apples. The second line contains n integers w1, w2, ..., wn (wi = 100 or wi = 200), where wi is the weight of the i-th apple.
In a single line print ""YES"" (without the quotes) if it is possible to divide all the apples between his friends. Otherwise print ""NO"" (without the quotes).
In the first test sample Kitahara Haruki can give the first and the last apple to Ogiso Setsuna and the middle apple to Touma Kazusa.
Input: 3100 200 100 | Output: YES
Easy
2
579
192
160
4
683
G
683G
G. The Fraction
1,900
*special
Periodic decimal fraction is usually written as: [entire_part.non-periodic_part (period)]. Any simple fraction can be represented as a periodic decimal fraction and vice versa. For example, the decimal fraction 0.2(45) corresponds to a fraction 27 / 110. Your task is to convert the periodic fraction to a simple periodi...
The first line contains the periodic decimal fraction x (0 < x < 1) in the format described in the statement. The total number of digits in the period and non-periodic part of the fraction does not exceed 8. Non-periodic part may be absent, the periodic part can't be absent (but it can be equal to any non-negative numb...
Print the representation of the fraction x as a simple fraction p / q, where p and q are mutually prime integers.
Input: 0.2(45) | Output: 27/110
Hard
1
331
324
113
6
95
C
95C
C. Volleyball
1,900
shortest paths
Petya loves volleyball very much. One day he was running late for a volleyball match. Petya hasn't bought his own car yet, that's why he had to take a taxi. The city has n junctions, some of which are connected by two-way roads. The length of each road is defined by some positive integer number of meters; the roads can...
The first line contains two integers n and m (1 ≀ n ≀ 1000, 0 ≀ m ≀ 1000). They are the number of junctions and roads in the city correspondingly. The junctions are numbered from 1 to n, inclusive. The next line contains two integers x and y (1 ≀ x, y ≀ n). They are the numbers of the initial and final junctions corres...
If taxis can't drive Petya to the destination point, print ""-1"" (without the quotes). Otherwise, print the drive's minimum cost.Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specificator.
An optimal way β€” ride from the junction 1 to 2 (via junction 4), then from 2 to 3. It costs 7+2=9 bourles.
Input: 4 41 31 2 31 4 12 4 12 3 52 77 21 27 7 | Output: 9
Hard
1
1,003
969
278
0
346
A
346A
A. Alice and Bob
1,600
games; math; number theory
It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of n distinct integers. And then they take turns to make the following moves. During each move, either Alice or Bob (the player whose turn is the current) can choose two di...
The first line contains an integer n (2 ≀ n ≀ 100) β€” the initial number of elements in the set. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the elements of the set.
Print a single line with the winner's name. If Alice wins print ""Alice"", otherwise print ""Bob"" (without quotes).
Consider the first test sample. Alice moves first, and the only move she can do is to choose 2 and 3, then to add 1 to the set. Next Bob moves, there is no valid move anymore, so the winner is Alice.
Input: 22 3 | Output: Alice
Medium
3
705
214
116
3
60
C
60C
C. Mushroom Strife
2,100
brute force; dfs and similar
Pasha and Akim were making a forest map β€” the lawns were the graph's vertexes and the roads joining the lawns were its edges. They decided to encode the number of laughy mushrooms on every lawn in the following way: on every edge between two lawns they wrote two numbers, the greatest common divisor (GCD) and the least ...
The first line contains two numbers n and m () which are the numbers of lawns and roads we know about. Each of the following m lines contains four numbers which are the numbers of lawns the road connects, the GCD and the LCM of the numbers of mushrooms on these lawns (1 ≀ GCD, LCM ≀ 106).It is guaranteed, that no road ...
The answer should contain ""YES"" or ""NO"" on the first line, saying whether it is possible or not to perform the arrangement. If the answer is ""YES"", print on the following line n numbers which are the numbers of mushrooms on the corresponding lawns.
Input: 1 0 | Output: YES1
Hard
2
879
398
254
0
1,574
A
1574A
A. Regular Bracket Sequences
800
constructive algorithms
A bracket sequence is a string containing only characters ""("" and "")"". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters ""1"" and ""+"" between the original characters of the sequence. For example, bracket sequences ""()()"" and ""...
The first line contains one integer \(t\) (\(1 \le t \le 50\)) β€” the number of test cases.Each test case consists of one line containing one integer \(n\) (\(1 \le n \le 50\)).
For each test case, print \(n\) lines, each containing a regular bracket sequence of length exactly \(2n\). All bracket sequences you output for a testcase should be different (though they may repeat in different test cases). If there are multiple answers, print any of them. It can be shown that it's always possible.
Input: 3 3 1 3 | Output: ()()() ((())) (()()) () ((())) (())() ()(())
Beginner
1
574
176
318
15
76
D
76D
D. Plus and xor
1,700
dp; greedy; math
Bitwise exclusive OR (or bitwise addition modulo two) is a binary operation which is equivalent to applying logical exclusive OR to every pair of bits located on the same positions in binary notation of operands. In other words, a binary digit of the result is equal to 1 if and only if bits on the respective positions ...
The first line contains integer number A and the second line contains integer number B (0 ≀ A, B ≀ 264 - 1).
The only output line should contain two integer non-negative numbers X and Y. Print the only number -1 if there is no answer.
Input: 14276 | Output: 33 109
Medium
3
746
108
125
0
2,038
D
2038D
D. Divide OR Conquer
2,400
binary search; bitmasks; data structures; dp; implementation
You are given an array \([a_1, a_2, \ldots a_n]\) consisting of integers between \(0\) and \(10^9\). You have to split this array into several segments (possibly one) in such a way that each element belongs to exactly one segment.Let the first segment be the array \([a_{l_1}, a_{l_1 + 1}, \ldots, a_{r_1}]\), the second...
The first line contains an integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the length of the array \(a\).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 10 ^9\)) β€” the elements of the given array.
Print one integer β€” the number of ways to split the array, taken modulo \(998\,244\,353\).
In the first two examples, every way to split the array is valid.In the third example, there are three valid ways to split the array: \(k = 3\); \(l_1 = 1, r_1 = 1, l_2 = 2, r_2 = 2, l_3 = 3, r_3 = 3\); the resulting arrays are \([3]\), \([4]\), \([6]\), and \(3 \le 4 \le 6\); \(k = 2\); \(l_1 = 1, r_1 = 1, l_2 = 2, r_...
Input: 31 2 3 | Output: 4
Expert
5
1,071
230
90
20
472
C
472C
C. Design Tutorial: Make It Nondeterministic
1,400
greedy
A way to make a new task is to make it nondeterministic or probabilistic. For example, the hard task of Topcoder SRM 595, Constellation, is the probabilistic version of a convex hull.Let's try to make a new task. Firstly we will use the following task. There are n people, sort them by their name. It is just an ordinary...
The first line contains an integer n (1 ≀ n ≀ 105) β€” the number of people.The next n lines each contains two strings. The i-th line contains strings fi and si (1 ≀ |fi|, |si| ≀ 50) β€” the first name and last name of the i-th person. Each string consists only of lowercase English letters. All of the given 2n strings will...
If it is possible, output ""YES"", otherwise output ""NO"".
In example 1 and 2, we have 3 people: tourist, Petr and me (cgy4ever). You can see that whatever handle is chosen, I must be the first, then tourist and Petr must be the last.In example 3, if Copernicus uses ""copernicus"" as his handle, everything will be alright.
Input: 3gennady korotkevichpetr mitrichevgaoyuan chen1 2 3 | Output: NO
Easy
1
698
406
59
4
1,182
B
1182B
B. Plus from Picture
1,300
dfs and similar; implementation; strings
You have a given picture with size \(w \times h\). Determine if the given picture has a single ""+"" shape or not. A ""+"" shape is described below: A ""+"" shape has one center nonempty cell. There should be some (at least one) consecutive non-empty cells in each direction (left, right, up, down) from the center. In o...
The first line contains two integers \(h\) and \(w\) (\(1 \le h\), \(w \le 500\)) β€” the height and width of the picture.The \(i\)-th of the next \(h\) lines contains string \(s_{i}\) of length \(w\) consisting ""."" and ""*"" where ""."" denotes the empty space and ""*"" denotes the non-empty space.
If the given picture satisfies all conditions, print ""YES"". Otherwise, print ""NO"".You can output each letter in any case (upper or lower).
In the first example, the given picture contains one ""+"".In the second example, two vertical branches are located in a different column.In the third example, there is a dot outside of the shape.In the fourth example, the width of the two vertical branches is \(2\).In the fifth example, there are two shapes.In the six...
Input: 5 6 ...... ..*... .****. ..*... ..*... | Output: YES
Easy
3
453
300
142
11
1,674
C
1674C
C. Infinite Replacement
1,000
combinatorics; implementation; strings
You are given a string \(s\), consisting only of Latin letters 'a', and a string \(t\), consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string \(s\) with a string \(t\). Note that after the replacement string \(s\) might contain letters other than 'a'.You can perform an arbitrar...
The first line contains a single integer \(q\) (\(1 \le q \le 10^4\)) β€” the number of testcases.The first line of each testcase contains a non-empty string \(s\), consisting only of Latin letters 'a'. The length of \(s\) doesn't exceed \(50\).The second line contains a non-empty string \(t\), consisting of lowercase La...
For each testcase, print the number of different strings \(s\) that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
In the first example, you can replace any letter 'a' with the string ""a"", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with ""abc"". String \(s\) becomes equal to ""aabc"". Then...
Input: 3aaaaaaaabcab | Output: 1 -1 2
Beginner
3
552
375
210
16