contest_id
int32
1
2.13k
index
stringclasses
62 values
problem_id
stringlengths
2
6
title
stringlengths
0
67
rating
int32
0
3.5k
tags
stringlengths
0
139
statement
stringlengths
0
6.96k
input_spec
stringlengths
0
2.32k
output_spec
stringlengths
0
1.52k
note
stringlengths
0
5.06k
sample_tests
stringlengths
0
1.02k
difficulty_category
stringclasses
6 values
tag_count
int8
0
11
statement_length
int32
0
6.96k
input_spec_length
int16
0
2.32k
output_spec_length
int16
0
1.52k
contest_year
int16
0
21
1,170
G
1170G
G. Graph Decomposition
0
*special; graphs
You are given an undirected graph consisting of \(n\) vertices and \(m\) edges.Recall that a cycle is a path that starts and ends in the same vertex. A cycle in a graph is called simple if it contains each vertex (except the starting and ending one) no more than once (the starting and the ending one is contained always...
The first line of the input contains two integers \(n\) and \(m\) (\(1 \le n, m \le 2 \cdot 10^5\)) β€” the number of vertices and the number of edges in the graph.The next \(m\) lines contain edges of the graph. The \(i\)-th line contains the \(i\)-th edge \(x_i, y_i\) (\(1 \le x_i, y_i \le n\)), where \(x_i\) and \(y_i...
If it is impossible to decompose the given graph into simple cycles, print ""NO"" in the first line.Otherwise print ""YES"" in the first line. In the second line print \(k\) β€” the number of simple cycles in the graph decomposition.In the next \(k\) lines print cycles themselves. The \(j\)-th line should contain the \(j...
The picture corresponding to the first example:
Input: 6 9 1 2 2 3 1 3 2 4 2 5 4 5 3 5 3 6 5 6 | Output: YES 3 4 2 5 4 2 4 3 6 5 3 4 1 3 2 1
Beginner
2
792
414
575
11
285
A
285A
A. Slightly Decreasing Permutations
1,100
greedy; implementation
Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1, p2, ..., pn.The decreasing coefficient of permutation p1, p2, ..., p...
The single line contains two space-separated integers: n, k (1 ≀ n ≀ 105, 0 ≀ k < n) β€” the permutation length and the decreasing coefficient.
In a single line print n space-separated integers: p1, p2, ..., pn β€” the permutation of length n with decreasing coefficient k. If there are several permutations that meet this condition, print any of them. It is guaranteed that the permutation with the sought parameters exists.
Input: 5 2 | Output: 1 5 2 4 3
Easy
2
482
141
279
2
1,296
E1
1296E1
E1. String Coloring (easy version)
1,800
constructive algorithms; dp; graphs; greedy; sortings
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different.You are given a string \(s\) consisting of \(n\) lowercase Latin letters.You have to color all its characters one of the ...
The first line of the input contains one integer \(n\) (\(1 \le n \le 200\)) β€” the length of \(s\).The second line of the input contains the string \(s\) consisting of exactly \(n\) lowercase Latin letters.
If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print ""NO"" (without quotes) in the first line.Otherwise, print ""YES"" in the first line and any correct coloring in the second line (the coloring is the string consisting of \(n\) characters, the \(i\...
Input: 9 abacbecfd | Output: YES 001010101
Medium
5
977
206
421
12
2,130
B
2130B
B. Pathless
1,100
constructive algorithms
There is an array \(a_1, a_2, \ldots, a_n\) consisting of values \(0\), \(1\), and \(2\), and an integer \(s\). It is guaranteed that \(a_1, a_2, \ldots, a_n\) contains at least one \(0\), one \(1\), and one \(2\).Alice wants to start from index \(1\) and perform steps of length \(1\) to the right or to the left, and r...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^3\)). The description of the test cases follows. The first line of each test case contains two integers \(n\) and \(s\) (\(3 \le n \le 50\), \(0 \le s \le 1000\)).The second line of each test case contains \...
For each test case, if it is possible to rearrange \(a\) such that Alice cannot find her target sequence, output \(n\) integers β€” such rearrangement of \(a\). Otherwise, output \(-1\) instead.
In the first test case, any rearrangement of \(a\) can prevent Alice from achieving her target.In the second test case, regardless of rearranging \(a\), Alice can always find the sequence \([1,2,3]\) as her target sequence.In the third test case,there is no rearrangement of \(a\) that can prevent Alice from achieving h...
Input: 63 20 1 23 30 1 23 60 1 23 40 1 23 100 1 25 10002 0 1 1 2 | Output: 0 1 2 -1 -1 0 2 1 -1 -1
Easy
1
1,074
464
192
21
27
E
27E
E. Number With The Given Amount Of Divisors
2,000
brute force; dp; number theory
Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018.
The first line of the input contains integer n (1 ≀ n ≀ 1000).
Output the smallest positive integer with exactly n divisors.
Input: 4 | Output: 6
Hard
3
155
62
61
0
2,068
K
2068K
K. Amusement Park Rides
3,000
flows; graphs
Ivan, Dmitrii, and Pjotr are celebrating Ivan's birthday at an amusement park with \(n\) attractions. The \(i\)-th attraction operates at minutes \(a_i, 2a_i, 3a_i, \dots\) (i.e., every \(a_i\) minutes).Each minute, the friends can either ride exactly one available attraction together or wait. Since the rides are very ...
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1\le t\le 2000\)) β€” the number of test cases. The descriptions of the \(t\) test cases follow.The first line contains an integer \(n\) (\(1\le n \le 2000\)) β€” the number of attractions. The second line contains \(n\) integers \(a_1,\, a...
For each test case, print the earliest time the three friends can finish all \(n\) attractions.
In the first test case, the three friends can ride the \(i\)-th attraction at the \(i\)-th minute. Since there are \(4\) attractions, they need \(4\) minutes to ride them all.In the third test case, the three friends can ride the attractions in order at minutes \(1,\, 2,\, 3,\, 4,\, 6,\, 8\) respectively. Therefore, th...
Input: 341 2 3 441 1 1 161 2 1 2 2 2 | Output: 4 4 8
Master
2
588
512
95
20
1,147
C
1147C
C. Thanos Nim
2,000
games
Alice and Bob are playing a game with \(n\) piles of stones. It is guaranteed that \(n\) is an even number. The \(i\)-th pile has \(a_i\) stones.Alice and Bob will play a game alternating turns with Alice going first.On a player's turn, they must choose exactly \(\frac{n}{2}\) nonempty piles and independently remove a ...
The first line contains one integer \(n\) (\(2 \leq n \leq 50\)) β€” the number of piles. It is guaranteed that \(n\) is an even number.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 50\)) β€” the number of stones in the piles.
Print a single string ""Alice"" if Alice wins; otherwise, print ""Bob"" (without double quotes).
In the first example, each player can only remove stones from one pile (\(\frac{2}{2}=1\)). Alice loses, since Bob can copy whatever Alice does on the other pile, so Alice will run out of moves first.In the second example, Alice can remove \(2\) stones from the first pile and \(3\) stones from the third pile on her fir...
Input: 2 8 8 | Output: Bob
Hard
1
624
261
96
11
1,984
H
1984H
H. Tower Capturing
3,300
combinatorics; dp; geometry
There are \(n\) towers at \(n\) distinct points \((x_1, y_1), (x_2, y_2), \ldots, (x_n, y_n)\), such that no three are collinear and no four are concyclic. Initially, you own towers \((x_1, y_1)\) and \((x_2, y_2)\), and you want to capture all of them. To do this, you can do the following operation any number of times...
The first line contains a single integer \(t\) (\(1 \leq t \leq 250\)) β€” the number of test cases.The first line of each test case contains a single integer \(n\) (\(4 \leq n \leq 100\)) β€” the number of towers.The \(i\)-th of the next \(n\) lines contains two integers \(x_i\) and \(y_i\) (\(-10^4 \leq x_i, y_i \leq 10^...
For each test case, output a single integer β€” the number of attack plans of minimal length after which you capture all towers, modulo \(998\,244\,353\).
In the first test case, there is only one possible attack plan of shortest length, shown below. Use the operation with \(P =\) tower \(1\), \(Q =\) tower \(2\), and \(R =\) tower \(5\). The circle through these three towers contains all towers inside of it, and as a result towers \(3\) and \(5\) are captured. Use the o...
Input: 351 12 53 34 25 461 13 31 22 13 1000019 8472 7-4 -3-3 63 1-5 21 -4-1 7 | Output: 1 0 10
Master
3
1,176
587
152
19
1,537
E2
1537E2
E2. Erase and Extend (Hard Version)
2,200
binary search; data structures; greedy; hashing; string suffix structures; strings; two pointers
This is the hard version of the problem. The only difference is the constraints on \(n\) and \(k\). You can make hacks only if all versions of the problem are solved.You have a string \(s\), and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: \(s:=s+s\), where \(...
The first line contains two integers \(n\), \(k\) (\(1 \leq n, k \leq 5\cdot 10^5\)) β€” the length of the original string \(s\) and the length of the desired string.The second line contains the string \(s\), consisting of \(n\) lowercase English letters.
Print the lexicographically smallest string of length \(k\) that can be obtained by doing the operations on string \(s\).
In the first test, it is optimal to make one duplication: ""dbcadabc"" \(\to\) ""dbcadabcdbcadabc"".In the second test it is optimal to delete the last \(3\) characters, then duplicate the string \(3\) times, then delete the last \(3\) characters to make the string have length \(k\).""abcd"" \(\to\) ""abc"" \(\to\) ""a...
Input: 8 16 dbcadabc | Output: dbcadabcdbcadabc
Hard
7
865
253
121
15
429
C
429C
C. Guess the Tree
2,300
bitmasks; constructive algorithms; dp; greedy; trees
Iahub and Iahubina went to a picnic in a forest full of trees. Less than 5 minutes passed before Iahub remembered of trees from programming. Moreover, he invented a new problem and Iahubina has to solve it, otherwise Iahub won't give her the food. Iahub asks Iahubina: can you build a rooted tree, such that each interna...
The first line of the input contains integer n (1 ≀ n ≀ 24). Next line contains n positive integers: the i-th number represents ci (1 ≀ ci ≀ n).
Output on the first line ""YES"" (without quotes) if there exist at least one tree following Iahub's restrictions, otherwise output ""NO"" (without quotes).
Input: 41 1 1 4 | Output: YES
Expert
5
721
144
156
4
407
B
407B
B. Long Path
1,600
dp; implementation
One day, little Vasya found himself in a maze consisting of (n + 1) rooms, numbered from 1 to (n + 1). Initially, Vasya is at the first room and to get out of the maze, he needs to get to the (n + 1)-th one.The maze is organized as follows. Each room of the maze has two one-way portals. Let's consider room number i (1 ...
The first line contains integer n (1 ≀ n ≀ 103) β€” the number of rooms. The second line contains n integers pi (1 ≀ pi ≀ i). Each pi denotes the number of the room, that someone can reach, if he will use the second portal in the i-th room.
Print a single number β€” the number of portal moves the boy needs to go out of the maze. As the number can be rather large, print it modulo 1000000007 (109 + 7).
Input: 21 2 | Output: 4
Medium
2
1,014
238
160
4
444
D
444D
D. DZY Loves Strings
2,500
binary search; hashing; strings; two pointers
DZY loves strings, and he enjoys collecting them.In China, many people like to use strings containing their names' initials, for example: xyz, jcvb, dzy, dyh.Once DZY found a lucky string s. A lot of pairs of good friends came to DZY when they heard about the news. The first member of the i-th pair has name ai, the sec...
The first line contains a string s (1 ≀ |s| ≀ 50000).The second line contains a non-negative integer q (0 ≀ q ≀ 100000) β€” the number of pairs. Each of the next q lines describes a pair, the line contains two space-separated strings ai and bi (1 ≀ |ai|, |bi| ≀ 4).It is guaranteed that all the strings only consist of low...
For each pair, print a line containing a single integer β€” the minimum length of the required substring. If there is no such substring, output -1.
The shortest substrings in the first sample are: xyz, dyhduxyz.The shortest substrings in the second sample are: ca, abc and abd.The shortest substrings in the third sample are: baabca and abaa.
Input: xudyhduxyz3xyz xyzdyh xyzdzy xyz | Output: 38-1
Expert
4
928
343
145
4
53
B
53B
B. Blog Photo
1,700
binary search; implementation
One popular blog site edits the uploaded photos like this. It cuts a rectangular area out of them so that the ratio of height to width (i.e. the height / width quotient) can vary from 0.8 to 1.25 inclusively. Besides, at least one side of the cut area should have a size, equal to some power of number 2 (2x for some int...
The first line contains a pair of integers h and w (1 ≀ h, w ≀ 109) which are the height and width of the uploaded photo in pixels.
Print two integers which are the height and width of the cut area.
Input: 2 1 | Output: 1 1
Medium
2
623
131
66
0
1,042
D
1042D
D. Petya and Array
1,800
data structures; divide and conquer; two pointers
Petya has an array \(a\) consisting of \(n\) integers. He has learned partial sums recently, and now he can calculate the sum of elements on any segment of the array really fast. The segment is a non-empty sequence of elements standing one next to another in the array.Now he wonders what is the number of segments in hi...
The first line contains two integers \(n\) and \(t\) (\(1 \le n \le 200\,000, |t| \le 2\cdot10^{14}\)).The second line contains a sequence of integers \(a_1, a_2, \dots, a_n\) (\(|a_{i}| \le 10^{9}\)) β€” the description of Petya's array. Note that there might be negative, zero and positive elements.
Print the number of segments in Petya's array with the sum of elements less than \(t\).
In the first example the following segments have sum less than \(4\): \([2, 2]\), sum of elements is \(-1\) \([2, 3]\), sum of elements is \(2\) \([3, 3]\), sum of elements is \(3\) \([4, 5]\), sum of elements is \(3\) \([5, 5]\), sum of elements is \(-1\)
Input: 5 45 -1 3 4 -1 | Output: 5
Medium
3
538
299
87
10
1,584
C
1584C
C. Two Arrays
900
greedy; math; sortings
You are given two arrays of integers \(a_1, a_2, \ldots, a_n\) and \(b_1, b_2, \ldots, b_n\).Let's define a transformation of the array \(a\): Choose any non-negative integer \(k\) such that \(0 \le k \le n\). Choose \(k\) distinct array indices \(1 \le i_1 < i_2 < \ldots < i_k \le n\). Add \(1\) to each of \(a_{i_1}, ...
The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β€” the number of test cases. Descriptions of test cases follow.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 100\)) β€” the size of arrays \(a\) and \(b\).The second line of each test case contains \(n\) integers \(a_1, ...
For each test case, print ""YES"" (without quotes) if it is possible to perform a transformation of the array \(a\), so that the resulting array is equal to \(b\). Print ""NO"" (without quotes) otherwise.You can print each letter in any case (upper or lower).
In the first test case, we can make the following transformation: Choose \(k = 2\). Choose \(i_1 = 1\), \(i_2 = 2\). Add \(1\) to \(a_1\) and \(a_2\). The resulting array is \([0, 2, 0]\). Swap the elements on the second and third positions. In the second test case there is no suitable transformation.In the third test ...
Input: 3 3 -1 1 0 0 0 2 1 0 2 5 1 2 3 4 5 1 2 3 4 5 | Output: YES NO YES
Beginner
3
575
476
259
15
1,037
C
1037C
C. Equalize
1,300
dp; greedy; strings
You are given two binary strings \(a\) and \(b\) of the same length. You can perform the following two operations on the string \(a\): Swap any two bits at indices \(i\) and \(j\) respectively (\(1 \le i, j \le n\)), the cost of this operation is \(|i - j|\), that is, the absolute difference between \(i\) and \(j\). Se...
The first line contains a single integer \(n\) (\(1 \le n \le 10^6\)) β€” the length of the strings \(a\) and \(b\).The second and third lines contain strings \(a\) and \(b\) respectively.Both strings \(a\) and \(b\) have length \(n\) and contain only '0' and '1'.
Output the minimum cost to make the string \(a\) equal to \(b\).
In the first example, one of the optimal solutions is to flip index \(1\) and index \(3\), the string \(a\) changes in the following way: ""100"" \(\to\) ""000"" \(\to\) ""001"". The cost is \(1 + 1 = 2\).The other optimal solution is to swap bits and indices \(1\) and \(3\), the string \(a\) changes then ""100"" \(\to...
Input: 3100001 | Output: 2
Easy
3
586
262
64
10
1,809
E
1809E
E. Two Tanks
2,400
binary search; dp; implementation; math
There are two water tanks, the first one fits \(a\) liters of water, the second one fits \(b\) liters of water. The first tank has \(c\) (\(0 \le c \le a\)) liters of water initially, the second tank has \(d\) (\(0 \le d \le b\)) liters of water initially.You want to perform \(n\) operations on them. The \(i\)-th opera...
The first line contains three integers \(n, a\) and \(b\) (\(1 \le n \le 10^4\); \(1 \le a, b \le 1000\)) β€” the number of operations and the capacities of the tanks, respectively.The second line contains \(n\) integers \(v_1, v_2, \dots, v_n\) (\(-1000 \le v_i \le 1000\); \(v_i \neq 0\)) β€” the volume of water you try t...
For all pairs of the initial volumes of water \((c, d)\) such that \(0 \le c \le a\) and \(0 \le d \le b\), calculate the volume of water in the first tank after all operations are performed.Print \(a + 1\) lines, each line should contain \(b + 1\) integers. The \(j\)-th value in the \(i\)-th line should be equal to th...
Consider \(c = 3\) and \(d = 2\) from the first example: The first operation tries to move \(2\) liters of water from the second tank to the first one, the second tank has \(2\) liters available, the first tank can fit \(1\) more liter. Thus, \(\min(2, 2, 1) = 1\) liter is moved, the first tank now contains \(4\) liter...
Input: 3 4 4 -2 1 2 | Output: 0 0 0 0 0 0 0 0 0 1 0 0 1 1 2 0 1 1 2 3 1 1 2 3 4
Expert
4
975
345
365
18
498
D
498D
D. Traffic Jams in the Land
2,400
data structures; dp; number theory
Some country consists of (n + 1) cities, located along a straight highway. Let's number the cities with consecutive integers from 1 to n + 1 in the order they occur along the highway. Thus, the cities are connected by n segments of the highway, the i-th segment connects cities number i and i + 1. Every segment of the h...
The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of highway segments that connect the n + 1 cities.The second line contains n integers a1, a2, ..., an (2 ≀ ai ≀ 6) β€” the periods of traffic jams appearance on segments of the highway.The next line contains a single integer q (1 ≀ q ≀ 105) β€” the numbe...
For each query of the first type output a single integer β€” the final value of time t after driving from city x to city y. Process the queries in the order in which they are given in the input.
Input: 102 5 3 2 3 5 3 4 2 410C 10 6A 2 6A 1 3C 3 4A 3 11A 4 9A 5 6C 7 3A 8 10A 2 5 | Output: 53146244
Expert
3
1,553
748
192
4
1,829
A
1829A
A. Love Story
800
implementation; strings
Timur loves codeforces. That's why he has a string \(s\) having length \(10\) made containing only lowercase Latin letters. Timur wants to know how many indices string \(s\) differs from the string ""codeforces"".For example string \(s =\) ""coolforsez"" differs from ""codeforces"" in \(4\) indices, shown in bold.Help ...
The first line contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β€” the number of test cases.Each test case is one line and contains the string \(s\), consisting of exactly \(10\) lowercase Latin characters.
For each test case, output a single integer β€” the number of indices where string \(s\) differs.
Input: 5coolforsezcadafurciecodeforcespaiuforcesforcescode | Output: 4 5 0 4 9
Beginner
2
469
213
95
18
551
E
551E
E. GukiZ and GukiZiana
2,500
binary search; data structures; implementation
Professor GukiZ was playing with arrays again and accidentally discovered new function, which he called GukiZiana. For given array a, indexed with integers from 1 to n, and number y, GukiZiana(a, y) represents maximum value of j - i, such that aj = ai = y. If there is no y as an element in a, then GukiZiana(a, y) is eq...
The first line contains two integers n, q (1 ≀ n ≀ 5 * 105, 1 ≀ q ≀ 5 * 104), size of array a, and the number of queries. The second line contains n integers a1, a2, ... an (1 ≀ ai ≀ 109), forming an array a. Each of next q lines contain either four or two numbers, as described in statement:If line starts with 1, then ...
For each query of type 2, print the value of GukiZiana(a, y), for y value for that query.
Input: 4 31 2 3 41 1 2 11 1 1 12 3 | Output: 2
Expert
3
670
481
89
5
1,742
E
1742E
E. Scuza
1,200
binary search; greedy; math
Timur has a stairway with \(n\) steps. The \(i\)-th step is \(a_i\) meters higher than its predecessor. The first step is \(a_1\) meters higher than the ground, and the ground starts at \(0\) meters. The stairs for the first test case. Timur has \(q\) questions, each denoted by an integer \(k_1, \dots, k_q\). For each ...
The first line contains a single integer \(t\) (\(1 \leq t \leq 100\)) β€” the number of test cases.The first line of each test case contains two integers \(n, q\) (\(1 \leq n, q \leq 2\cdot10^5\)) β€” the number of steps and the number of questions, respectively.The second line of each test case contains \(n\) integers (\...
For each test case, output a single line containing \(q\) integers, the answer for each question.Please note, that the answer for some questions won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
Consider the first test case, pictured in the statement. If Timur's legs have length \(1\), then he can only climb stair \(1\), so the highest he can reach is \(1\) meter. If Timur's legs have length \(2\) or \(4\), then he can only climb stairs \(1\), \(2\), and \(3\), so the highest he can reach is \(1+2+1=4\) meters...
Input: 34 51 2 1 51 2 4 9 102 21 10 13 11000000000 1000000000 10000000001000000000 | Output: 1 4 4 9 9 0 2 3000000000
Easy
3
659
610
282
17
313
B
313B
B. Ilya and Queries
1,100
dp; implementation
Ilya the Lion wants to help all his friends with passing exams. They need to solve the following problem to pass the IT exam.You've got string s = s1s2... sn (n is the length of the string), consisting only of characters ""."" and ""#"" and m queries. Each query is described by a pair of integers li, ri (1 ≀ li < ri ≀ ...
The first line contains string s of length n (2 ≀ n ≀ 105). It is guaranteed that the given string only consists of characters ""."" and ""#"".The next line contains integer m (1 ≀ m ≀ 105) β€” the number of queries. Each of the next m lines contains the description of the corresponding query. The i-th line contains inte...
Print m integers β€” the answers to the queries in the order in which they are given in the input.
Input: ......43 42 31 62 6 | Output: 1154
Easy
2
522
350
96
3
1,539
E
1539E
E. Game with Cards
2,500
binary search; constructive algorithms; data structures; dp; greedy; implementation
The Alice's computer is broken, so she can't play her favorite card game now. To help Alice, Bob wants to answer \(n\) her questions. Initially, Bob holds one card with number \(0\) in the left hand and one in the right hand. In the \(i\)-th question, Alice asks Bob to replace a card in the left or right hand with a ca...
The first line contains two integers \(n\) and \(m\) (\(2 \le n \le 100\,000\), \(2 \le m \le 10^9\)) β€” the number of questions and the maximum possible value on the card.Then \(n\) queries are described. Every description contains 3 lines.The first line of the description of the \(i\)-th query contains a single intege...
At the first line, print ""Yes"", if Bob can answer all queries, and ""No"" otherwise.If Bob can answer all \(n\) queries, then at the second line print \(n\) numbers: a way to satisfy all requirements. If in \(i\)-th query Bob needs to replace the card in the left hand, print \(0\), otherwise print \(1\). If there are...
Input: 2 10 3 0 3 0 2 2 0 4 0 2 | Output: Yes 0 1
Expert
6
888
843
349
15
1,663
C
1663C
C. Pōja Verdon
0
*special; implementation; math
Aenar has an array \(a\) consisting of \(n\) integers.
The first line is the integer \(n\) (\(1 \le n \le 100\)).The second line contains \(n\) integers \(a_i\) (\(0 \le a_i \le 3979\)) β€” the contents of the array.
Output a single number.
Beginner
3
54
159
23
16
1,930
D2
1930D2
D2. Sum over all Substrings (Hard Version)
2,100
bitmasks; divide and conquer; dp; dsu; greedy; implementation; strings
This is the hard version of the problem. The only difference between the two versions is the constraint on \(t\) and \(n\). You can make hacks only if both versions of the problem are solved.For a binary\(^\dagger\) pattern \(p\) and a binary string \(q\), both of length \(m\), \(q\) is called \(p\)-good if for every \...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^5\)) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^6\)) β€” the length of the binary string \(s\).The ...
For each test case, output the sum of values of \(f\) over all substrings of \(s\).
In the first test case, the only \(\mathtt{1}\)-good string is \(\mathtt{1}\). Thus, \(f(\mathtt{1})=1\).In the second test case, \(f(\mathtt{10})=1\) because \(\mathtt{01}\) is \(\mathtt{10}\)-good, and \(\mathtt{00}\) is not \(\mathtt{10}\)-good. Thus, the answer is \(f(\mathtt{1})+f(\mathtt{10})+f(\mathtt{0}) = 1 + ...
Input: 4112105000002011110110000000111111 | Output: 1 2 0 346
Hard
7
1,340
545
83
19
2,110
E
2110E
E. Melody
2,300
dfs and similar; graphs; implementation
In 2077, the robots that took over the world realized that human music wasn't that great, so they started composing their own.To write music, the robots have a special musical instrument capable of producing \(n\) different sounds. Each sound is characterized by its volume and pitch. A sequence of sounds is called musi...
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. In the first line of each test case, there is a number \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the number of sounds that the musical instrument can produce.Nex...
For each test case, if it is possible to compose such music, output ""YES"", and on the next line, output \(n\) numbers β€” the indices of the sounds in the order that forms beautiful non-boring music. Otherwise, output ""NO"".You may output each letter in any case (lowercase or uppercase). For example, the strings ""yEs...
In the first test case, the music \((239,239)-(239,179)-(179,179)-(179,239)\) is suitable, contains all sounds, and all consecutive sounds differ either only in volume or only in pitch.In the second test case, it can be shown that there is no suitable music with the given sounds.
Input: 54179 239179 179239 179239 23931 12 13 115 751 11 22 12 299 9971 11 32 12 23 13 23 3 | Output: YES 4 3 2 1 NO YES 1 NO YES 3 4 6 7 2 1 5
Expert
3
646
753
392
21
758
D
758D
D. Ability To Convert
2,000
constructive algorithms; dp; greedy; math; strings
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets...
The first line contains the integer n (2 ≀ n ≀ 109). The second line contains the integer k (0 ≀ k < 1060), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n.Alexander guarantees that the answer exists and does not exceed 1018.The number k doesn'...
Print the number x (0 ≀ x ≀ 1018) β€” the answer to the problem.
In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12Β·130 or 15 = 1Β·131 + 2Β·130.
Input: 1312 | Output: 12
Hard
5
635
344
62
7
489
D
489D
D. Unbearable Controversy of Being
1,700
brute force; combinatorics; dfs and similar; graphs
Tomash keeps wandering off and getting lost while he is walking along the streets of Berland. It's no surprise! In his home town, for any pair of intersections there is exactly one way to walk from one intersection to the other one. The capital of Berland is very different!Tomash has noticed that even simple cases of a...
The first line of the input contains a pair of integers n, m (1 ≀ n ≀ 3000, 0 ≀ m ≀ 30000) β€” the number of intersections and roads, respectively. Next m lines list the roads, one per line. Each of the roads is given by a pair of integers ai, bi (1 ≀ ai, bi ≀ n;ai β‰  bi) β€” the number of the intersection it goes out from ...
Print the required number of ""damn rhombi"".
Input: 5 41 22 31 44 3 | Output: 1
Medium
4
1,095
533
45
4
1,038
C
1038C
C. Gambling
1,300
greedy; sortings
Two players A and B have a list of \(n\) integers each. They both want to maximize the subtraction between their score and their opponent's score. In one turn, a player can either add to his score any element from his list (assuming his list is not empty), the element is removed from the list afterward. Or remove an el...
The first line of input contains an integer \(n\) (\(1 \le n \le 100\,000\)) β€” the sizes of the list.The second line contains \(n\) integers \(a_i\) (\(1 \le a_i \le 10^6\)), describing the list of the player A, who starts the game.The third line contains \(n\) integers \(b_i\) (\(1 \le b_i \le 10^6\)), describing the ...
Output the difference between A's score and B's score (\(A-B\)) if both of them are playing optimally.
In the first example, the game could have gone as follows: A removes \(5\) from B's list. B removes \(4\) from A's list. A takes his \(1\). B takes his \(1\). Hence, A's score is \(1\), B's score is \(1\) and difference is \(0\).There is also another optimal way of playing: A removes \(5\) from B's list. B removes \(4\...
Input: 21 45 1 | Output: 0
Easy
2
1,246
341
102
10
2,070
C
2070C
C. Limited Repainting
1,500
binary search; greedy
You are given a strip, consisting of \(n\) cells, all cells are initially colored red.In one operation, you can choose a segment of consecutive cells and paint them blue. Before painting, the chosen cells can be either red or blue. Note that it is not possible to paint them red. You are allowed to perform at most \(k\)...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 3 \cdot 10^5\); \(0 \le k \le n\)) β€” the length of the strip and the maximum number of operations.The second line contains a string \(s\)...
For each test case, print a single integer β€” the minimum penalty of the final painting.
In the first test case, you can paint the cells from \(1\) to \(3\). The painting will be BBBR. So, only cell \(2\) is painted the wrong color. Therefore, the penalty for it is the final penalty and equals \(3\).In the second test case, the painting BBBR will now result in a penalty of \(5\). However, if you paint the ...
Input: 54 1BRBR9 3 5 44 1BRBR9 5 3 44 2BRBR9 3 5 410 2BRBRBBRRBR5 1 2 4 5 3 6 1 5 45 5RRRRR5 3 1 2 4 | Output: 3 3 0 4 0
Medium
2
968
650
87
20
1,310
F
1310F
F. Bad Cryptography
3,400
math; number theory
In modern cryptography much is tied to the algorithmic complexity of solving several problems. One of such problems is a discrete logarithm problem. It is formulated as follows: Let's fix a finite field and two it's elements \(a\) and \(b\). One need to fun such \(x\) that \(a^x = b\) or detect there is no such x. It i...
In the first line of input there is single integer \(t\) (\(1 \le t \le 100\)) β€” number of pairs, for which you need to find the discrete logarithm.In each of next \(t\) line there is a pair of integers \(a\) \(b\) (\(1 \le a, b < 2^{64}\)).
For each pair you should print one integer \(x\) (\(0 \le x < 2^{64}\)), such that \(a^{\odot x} = b\), or -1 if no such x exists. It can be shown, that if any such \(x\) exists, there is one inside given bounds. If there are several good values, you can output any of them.
Input: 7 2 2 1 1 2 3 8 10 8 2 321321321321 2 123214213213 4356903202345442785 | Output: 1 1 2 4 -1 6148914691236517205 68943624821423112
Master
2
3,854
241
274
13
1,571
I
1571I
I. Physical Examination
3,200
*special; binary search; data structures
Polycarp plans to undergo a full physical examination at his local clinic. There are \(n\) doctors, numbered from \(1\) to \(n\). The \(i\)-th doctor takes patients from minute \(L_i\) to minute \(R_i\), so Polycarp can visit him at any minute in this range. It takes each doctor exactly one minute to examine Polycarp's...
The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β€” the number of testcases.Then the descriptions of \(t\) testcases follow.The first line of the testcase contains a single integer \(n\) (\(1 \le n \le 10^5\)) β€” the number of doctors.The second line of the testcase contains \(n\) integers \(L_1, L_2,...
For each testcase print an answer.If there exists such a minute \(x\) and a permutation \(p\) that Polycarp is able to visit all \(n\) doctors without waiting or visiting any doctor several times, then print \(x\) in the first line and a permutation \(p\) in the second line. If there are multiple answers, print any of ...
In the third testcase it's impossible to visit all doctors, because Polycarp has to visit doctor \(2\) at minute \(2\) and doctor \(1\) at minute \(4\). However, that would require him to wait a minute between the visits, which is not allowed.In the fourth testcase all doctors take patients in the span of \(2\) minutes...
Input: 5 3 2 3 1 3 3 2 8 6 6 5 4 9 4 3 6 7 6 10 6 9 6 6 8 2 4 2 4 2 3 2 2 2 3 3 3 1 5 10 | Output: 1 3 1 2 3 7 4 6 2 1 8 5 3 -1 -1 7 1
Master
3
1,182
524
366
15
1,915
G
1915G
G. Bicycles
1,800
graphs; greedy; implementation; shortest paths; sortings
All of Slavic's friends are planning to travel from the place where they live to a party using their bikes. And they all have a bike except Slavic. There are \(n\) cities through which they can travel. They all live in the city \(1\) and want to go to the party located in the city \(n\). The map of cities can be seen a...
The first line contains a single integer \(t\) (\(1 \leq t \leq 100\)) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains two space-separated integers \(n\) and \(m\) (\(2 \leq n \leq 1000\); \(n - 1 \leq m \leq 1000\)) β€” the number of cities and the number o...
For each test case, output a single integer denoting the shortest amount of time Slavic can reach city \(n\) starting from city \(1\).
Input: 35 51 2 23 2 12 4 52 5 74 5 15 2 1 3 35 101 2 51 3 51 4 41 5 82 3 62 4 32 5 23 4 13 5 84 5 27 2 8 4 17 103 2 82 1 42 5 72 6 47 1 24 3 56 4 26 7 16 7 44 5 97 6 5 4 3 2 1 | Output: 19 36 14
Medium
5
1,324
1,002
134
19
1,089
F
1089F
F. Fractions
1,900
math
You are given a positive integer \(n\).Find a sequence of fractions \(\frac{a_i}{b_i}\), \(i = 1 \ldots k\) (where \(a_i\) and \(b_i\) are positive integers) for some \(k\) such that:$$$$$$ \begin{cases} \text{$b_i$ divides $n$, $1 < b_i < n$ for $i = 1 \ldots k$} \\ \text{$1 \le a_i < b_i$ for $i = 1 \ldots k$} \\ \te...
The input consists of a single integer \(n\) (\(2 \le n \le 10^9\)).
In the first line print ""YES"" if there exists such a sequence of fractions or ""NO"" otherwise.If there exists such a sequence, next lines should contain a description of the sequence in the following format.The second line should contain integer \(k\) (\(1 \le k \le 100\,000\)) β€” the number of elements in the sequen...
In the second example there is a sequence \(\frac{1}{2}, \frac{1}{3}\) such that \(\frac{1}{2} + \frac{1}{3} = 1 - \frac{1}{6}\).
Input: 2 | Output: NO
Hard
1
398
68
542
10
62
D
62D
D. Wormhouse
2,300
dfs and similar; graphs
Arnie the Worm has finished eating an apple house yet again and decided to move. He made up his mind on the plan, the way the rooms are located and how they are joined by corridors. He numbered all the rooms from 1 to n. All the corridors are bidirectional.Arnie wants the new house to look just like the previous one. T...
The first line contains two integers n and m (3 ≀ n ≀ 100, 3 ≀ m ≀ 2000). It is the number of rooms and corridors in Arnie's house correspondingly. The next line contains m + 1 positive integers that do not exceed n. They are the description of Arnie's old path represented as a list of rooms he visited during the gnawi...
Print m + 1 positive integers that do not exceed n. Those numbers are the description of the new path, according to which Arnie should gnaw out his new house. If it is impossible to find new path you should print out No solution. The first number in your answer should be equal to the last one. Also it should be equal t...
Input: 3 31 2 3 1 | Output: 1 3 2 1
Expert
2
1,359
722
340
0
1,070
G
1070G
G. Monsters and Potions
2,300
brute force; dp; greedy; implementation
Polycarp is an introvert person. In fact he is so much of an introvert that he plays ""Monsters and Potions"" board game alone. The board of the game is a row of \(n\) cells. The cells are numbered from \(1\) to \(n\) from left to right. There are three types of cells: a cell containing a single monster, a cell contain...
The first line of the input contains two integers \(n\) and \(m\) (\(1 \le n \le 100\); \(1 \le m \le n\)) β€” length of the game board and the number of heroes on it.The following \(m\) lines describe heroes. Each line contains two integers \(s_i\) and \(h_i\) (\(1 \le s_i \le n\); \(1 \le h_i \le 10^6\)), where \(s_i\)...
On the first line of the output print the index of the rally point cell.On the second line print \(m\) integers β€” the order in which heroes should move to the rally point. Heroes are numbered from \(1\) to \(m\) in the order they are given in the input.If there are multiple solutions, print any of them.If it is impossi...
The picture illustrates the first example:
Input: 8 38 21 34 90 3 -5 0 -5 -4 -1 0 | Output: 63 1 2
Expert
4
2,742
866
422
10
1,582
F1
1582F1
F1. Korney Korneevich and XOR (easy version)
1,800
bitmasks; dp; greedy
This is an easier version of the problem with smaller constraints.Korney Korneevich dag up an array \(a\) of length \(n\). Korney Korneevich has recently read about the operation bitwise XOR, so he wished to experiment with it. For this purpose, he decided to find all integers \(x \ge 0\) such that there exists an incr...
The first line contains a single integer \(n\) (\(1 \le n \le 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 500\)) β€” the elements of the array \(a\).
In the first line print a single integer \(k\) β€” the number of found \(x\) values.In the second line print \(k\) integers in increasing order \(x_1, x_2, \ldots x_k\) (\(0 \le x_1 < \ldots < x_k\)) β€” found \(x\) values.
In the first test case: To get value \(x = 0\) it is possible to choose and empty subsequence To get value \(x = 2\) it is possible to choose a subsequence \([2]\) To get value \(x = 4\) it is possible to choose a subsequence \([4]\) To get value \(x = 6\) it is possible to choose a subsequence \([2, 4]\)
Input: 4 4 2 2 4 | Output: 4 0 2 4 6
Medium
3
803
194
219
15
1,495
A
1495A
A. Diamond Miner
1,200
geometry; greedy; math; sortings
Diamond Miner is a game that is similar to Gold Miner, but there are \(n\) miners instead of \(1\) in this game.The mining area can be described as a plane. The \(n\) miners can be regarded as \(n\) points on the y-axis. There are \(n\) diamond mines in the mining area. We can regard them as \(n\) points on the x-axis....
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1\le t\le 10\)) β€” 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 10^5\)) β€” the number of miners and mines.Each of the next \(2...
For each test case, print a single real number β€” the minimal sum of energy that should be spent.Your answer is considered correct if its absolute or relative error does not exceed \(10^{-9}\).Formally, let your answer be \(a\), and the jury's answer be \(b\). Your answer is accepted if and only if \(\frac{|a - b|}{\max...
In the first test case, the miners are at \((0,1)\) and \((0,-1)\), while the diamond mines are at \((1,0)\) and \((-2,0)\). If you arrange the miners to get the diamond mines in the way, shown in the picture, you can get the sum of the energy \(\sqrt2 + \sqrt5\).
Input: 3 2 0 1 1 0 0 -1 -2 0 4 1 0 3 0 -5 0 6 0 0 3 0 1 0 2 0 4 5 3 0 0 4 0 -3 4 0 2 0 1 0 -3 0 0 -10 0 -2 0 -10 | Output: 3.650281539872885 18.061819283610362 32.052255376143336
Easy
4
860
993
346
14
1,862
F
1862F
F. Magic Will Save the World
1,800
binary search; bitmasks; brute force; dp
A portal of dark forces has opened at the border of worlds, and now the whole world is under a terrible threat. To close the portal and save the world, you need to defeat \(n\) monsters that emerge from the portal one after another.Only the sorceress Vika can handle this. She possesses two magical powers β€” water magic ...
Each test consists of several test cases. The first line of each test contains a single integer \(t\) (\(1 \le t \le 100\)) β€” the number of test cases. This is followed by a description of the test cases.The first line of each test case contains two integers \(w\), \(f\) (\(1 \le w, f \le 10^9\)) β€” the amount of water ...
For each test case, output a single integer β€” the minimum time in seconds that Vika will need to defeat all the monsters.
In the first sample, after the first second, Vika can spend \(2\) units of fire mana to kill the first monster. Then she has \(2\) units of water mana and \(1\) unit of fire mana. After the third second, she will have \(6\) units of water mana and \(7\) units of fire mana at her disposal. This will be enough to immedia...
Input: 42 332 6 737 58193190 90223 9713 4410 10 2 45 | Output: 3 2 1 5
Medium
4
1,159
708
121
18
1,488
J
1488J
J. Flower Shop
3,100
*special; data structures; fft; math
Your friend is running a flower shop. In order to be prepared for the next holidays (when, as usual, sales skyrocket) she asked you to write her a special program that will help to analyze the stocks she has.There are \(n\) different types of flowers she can order and each flower of the type \(i\) costs \(w_i\). The la...
The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 1000\); \(1 \le m \le 1000\)) β€” the number of flower types and the number of queries.The second line contains \(n\) integers \(w_1, w_2, \dots, w_n\) (\(1 \le w_i \le 1000\)) β€” the cost of one flower of each type.The next \(m\) lines contains queries β€”...
For each query of the third type, print how many variants of bouquets she can make using only flowers of types \(l, l + 1, \dots, r\) with the total cost no more than \(k\). Since the answer may be too large, print it modulo \(998\,244\,353\).
Input: 5 12 1 2 3 2 1 1 1 5 1 2 3 1 3 1 3 1 5 10 3 4 5 100 1 4 4 1 5 1 3 2 5 7 3 1 1 3 3 1 5 100 2 1 5 3 1 5 100 | Output: 40 0 28 3 479 79
Master
4
1,177
749
243
14
1,863
C
1863C
C. MEX Repetition
1,100
implementation; math
You are given an array \(a_1,a_2,\ldots, a_n\) of pairwise distinct integers from \(0\) to \(n\). Consider the following operation: consecutively for each \(i\) from \(1\) to \(n\) in this order, replace \(a_i\) with \(\operatorname{MEX}(a_1, a_2, \ldots, a_n)\). Here \(\operatorname{MEX}\) of a collection of integers ...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^5\)). The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(1\le n\le 10^5\), \(1\le k\le 10^9\)).The second line contains \(n\) pairwise distinc...
For each test case, print all \(n\) elements of the array after applying \(k\) operations.
In the first test case, here is the entire process: On the first operation, the array changes from \([1]\) to \([0]\), since \(\operatorname{MEX}(1) = 0\). On the second operation, the array changes from \([0]\) to \([1]\), since \(\operatorname{MEX}(0) = 1\). Thus, the array becomes \([1]\) after two operations.In the...
Input: 51 213 10 1 32 20 25 51 2 3 4 510 1005 3 0 4 2 1 6 9 10 8 | Output: 1 2 0 1 2 1 2 3 4 5 0 7 5 3 0 4 2 1 6 9 10
Easy
2
591
529
90
18
995
D
995D
D. Game
2,500
math
Allen and Bessie are playing a simple number game. They both know a function \(f: \{0, 1\}^n \to \mathbb{R}\), i. e. the function takes \(n\) binary arguments and returns a real value. At the start of the game, the variables \(x_1, x_2, \dots, x_n\) are all set to \(-1\). Each round, with equal probability, one of Alle...
The first line contains two integers \(n\) and \(r\) (\(1 \le n \le 18\), \(0 \le r \le 2^{18}\)).The next line contains \(2^n\) integers \(c_0, c_1, \dots, c_{2^n-1}\) (\(0 \le c_i \le 10^9\)), denoting the initial values of \(f\). More specifically, \(f(x_0, x_1, \dots, x_{n-1}) = c_x\), if \(x = \overline{x_{n-1} \l...
Print \(r+1\) lines, the \(i\)-th of which denotes the value of the game \(f\) during the \(i\)-th round. Your answer must have absolute or relative error within \(10^{-6}\).Formally, let your answer be \(a\), and the jury's answer be \(b\). Your answer is considered correct if \(\frac{|a - b|}{\max{(1, |b|)}} \le 10^{...
Consider the second test case. If Allen goes first, he will set \(x_1 \to 1\), so the final value will be \(3\). If Bessie goes first, then she will set \(x_1 \to 0\) so the final value will be \(2\). Thus the answer is \(2.5\).In the third test case, the game value will always be \(1\) regardless of Allen and Bessie's...
Input: 2 20 1 2 32 50 4 | Output: 1.5000002.2500003.250000
Expert
1
1,028
565
326
9
1,601
E
1601E
E. Phys Ed Online
2,900
data structures; dp; greedy
Students of one unknown college don't have PE courses. That's why \(q\) of them decided to visit a gym nearby by themselves. The gym is open for \(n\) days and has a ticket system. At the \(i\)-th day, the cost of one ticket is equal to \(a_i\). You are free to buy more than one ticket per day.You can activate a ticket...
The first line contains three integers \(n\), \(q\) and \(k\) (\(1 \le n, q \le 300\,000\); \(1 \le k \le n\)) β€” the number of days, the number of students and the number of days each ticket is still valid. The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) β€” the cost of one ticke...
For each student, print the minimum possible amount of money they have to spend in order to go to the gym at desired days.
Let's see how each student have to spend their money: The first student should buy one ticket at day \(1\). The second student should buy one ticket at day \(3\) and two tickets at day \(4\). Note that student can keep purchased tickets for the next days. The third student should buy one ticket at day \(5\). The fourth...
Input: 7 5 2 2 15 6 3 7 5 6 1 2 3 7 5 5 7 7 3 5 | Output: 2 12 7 6 9
Master
3
1,283
518
122
16
232
E
232E
E. Quick Tortoise
3,000
bitmasks; divide and conquer; dp
John Doe has a field, which is a rectangular table of size n Γ— m. We assume that the field rows are numbered from 1 to n from top to bottom, and the field columns are numbered from 1 to m from left to right. Then the cell of the field at the intersection of the x-th row and the y-th column has coordinates (x; y).We kno...
The first line contains two space-separated integers n and m (1 ≀ n, m ≀ 500) β€” the field sizes.Each of the next n lines contains m characters ""#"" and ""."": the j-th character of the i-th line equals ""#"", if the cell (i; j) is painted black and ""."", if it is painted white.The next line contains integer q (1 ≀ q ...
For each of q queries print on a single line ""Yes"", if there is a way from cell (x1; y1) to cell (x2; y2), that meets the requirements, and ""No"" otherwise. Print the answers to the queries in the order, in which the queries are given in the input.
Input: 3 3....##.#.51 1 3 31 1 1 31 1 3 11 1 1 21 1 2 1 | Output: NoYesYesYesYes
Master
3
1,090
577
251
2
1,331
H
1331H
H. It's showtime
0
*special
You are given a mysterious language (codenamed ""UnknownX"") available in ""Custom Test"" tab. Find out what this language is, and use it to solve the following problem.You are given an integer \(input = 1000 * n + mod\) (\(1 \le n, mod \le 999\)). Calculate double factorial of \(n\) modulo \(mod\).
The input contains a single integer \(input\) (\(1001 \le input \le 999999\)). You are guaranteed that \(input \mod 1000 \neq 0\).
Output a single number.
In the first test case you need to calculate \(6!! \mod 100\); \(6!! = 6 * 4 * 2 = 48\).In the second test case you need to calculate \(9!! \mod 900\); \(9!! = 9 * 7 * 5 * 3 = 945\).In the third test case you need to calculate \(100!! \mod 2\); you can notice that \(100!!\) is a multiple of 100 and thus is divisible by...
Input: 6100 | Output: 48
Beginner
1
300
130
23
13
1,415
D
1415D
D. XOR-gun
2,000
bitmasks; brute force; constructive algorithms
Arkady owns a non-decreasing array \(a_1, a_2, \ldots, a_n\). You are jealous of its beauty and want to destroy this property. You have a so-called XOR-gun that you can use one or more times.In one step you can select two consecutive elements of the array, let's say \(x\) and \(y\), remove them from the array and inser...
The first line contains a single integer \(n\) (\(2 \le n \le 10^5\)) β€” the initial length of the array.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) β€” the elements of the array. It is guaranteed that \(a_i \le a_{i + 1}\) for all \(1 \le i < n\).
Print a single integer β€” the minimum number of steps needed. If there is no solution, print \(-1\).
In the first example you can select \(2\) and \(5\) and the array becomes \([7, 6, 8]\).In the second example you can only obtain arrays \([1, 1]\), \([3, 3]\) and \([0]\) which are all non-decreasing.In the third example you can select \(1\) and \(2\) and the array becomes \([3, 4, 6, 20]\). Then you can, for example,...
Input: 4 2 5 6 8 | Output: 1
Hard
3
877
292
99
14
1,463
E
1463E
E. Plan of Lectures
2,400
constructive algorithms; dfs and similar; dsu; graphs; implementation; sortings; trees
Ivan is a programming teacher. During the academic year, he plans to give \(n\) lectures on \(n\) different topics. Each topic should be used in exactly one lecture. Ivan wants to choose which topic will he explain during the \(1\)-st, \(2\)-nd, ..., \(n\)-th lecture β€” formally, he wants to choose some permutation of i...
The first line contains two integers \(n\) and \(k\) (\(2 \le n \le 3 \cdot 10^5\), \(1 \le k \le n - 1\)) β€” the number of topics and the number of special pairs of topics, respectively.The second line contains \(n\) integers \(p_1\), \(p_2\), ..., \(p_n\) (\(0 \le p_i \le n\)), where \(p_i\) is the prerequisite topic ...
If there is no ordering of topics meeting all the constraints, print \(0\).Otherwise, print \(n\) pairwise distinct integers \(q_1\), \(q_2\), ..., \(q_n\) (\(1 \le q_i \le n\)) β€” the ordering of topics meeting all of the constraints. If there are multiple answers, print any of them.
Input: 5 2 2 3 0 5 3 1 5 5 4 | Output: 3 2 1 5 4
Expert
7
1,408
842
284
14
98
A
98A
A. Help Victoria the Wise
1,700
brute force; implementation
Vasilisa the Wise from a far away kingdom got a present from her friend Helga the Wise from a farther away kingdom. The present is a surprise box, yet Vasilisa the Wise doesn't know yet what the surprise actually is because she cannot open the box. She hopes that you can help her in that.The box's lock is constructed l...
The first line contains exactly 6 characters without spaces from the set {R, O, Y, G, B, V} β€” they are the colors of gems with which the box should be decorated.
Print the required number of different ways to decorate the box.
Input: YYYYYY | Output: 1
Medium
2
1,267
161
64
0
656
B
656B
B. Scrambled
1,700
*special; implementation
Btoh yuo adn yuor roomatme lhoate wianshg disehs, btu stlil sdmoeboy msut peorrfm tihs cohre dialy. Oen dya yuo decdie to idourtcne smoe syestm. Yuor rmmotaoe sstgegus teh fooniwllg dael. Yuo argee on tow arayrs of ientgres M adn R, nmebur upmicnog dyas (induiclng teh cunrret oen) wtih sicsescuve irnegets (teh ceurrnt ...
The first line of input contains a single integer N (1 ≀ N ≀ 16).The second and third lines of input contain N integers each, all between 0 and 16, inclusive, and represent arrays M and R, respectively. All M[i] are positive, for each i R[i] < M[i].
Output a single real number. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 4.
Input: 120 | Output: 0.500000
Medium
2
764
249
125
6
495
B
495B
B. Modular Equations
1,600
math; number theory
Last week, Hamed learned about a new type of equations in his math class called Modular Equations. Lets define i modulo j as the remainder of division of i by j and denote it by . A Modular Equation, as Hamed's teacher described, is an equation of the form in which a and b are two non-negative integers and x is a varia...
In the only line of the input two space-separated integers a and b (0 ≀ a, b ≀ 109) are given.
If there is an infinite number of answers to our equation, print ""infinity"" (without the quotes). Otherwise print the number of solutions of the Modular Equation .
In the first sample the answers of the Modular Equation are 8 and 16 since
Input: 21 5 | Output: 2
Medium
2
808
94
165
4
1,833
B
1833B
B. Restore the Weather
900
greedy; sortings
You are given an array \(a\) containing the weather forecast for Berlandia for the last \(n\) days. That is, \(a_i\) β€” is the estimated air temperature on day \(i\) (\(1 \le i \le n\)).You are also given an array \(b\) β€” the air temperature that was actually present on each of the days. However, all the values in array...
The first line of input data 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 two integers \(n\) (\(1 \le n \le 10^5\)) and \(k\) (\(0 \le k \le10^9\)) β€” the number of days and the maximum difference bet...
On a separate line for each test case, output exactly \(n\) numbers β€” the values of air temperature on each of the days in the correct order. If there is more than one answer β€” output any of them.
Input: 35 21 3 5 3 92 5 11 2 46 1-1 3 -2 0 -5 -1-4 0 -1 4 0 03 37 7 79 4 8 | Output: 2 2 5 4 11 0 4 -1 0 -4 0 8 4 9
Beginner
2
1,305
830
196
18
1,162
B
1162B
B. Double Matrix
1,400
brute force; greedy
You are given two \(n \times m\) matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are...
The first line contains two integers \(n\) and \(m\) (\(1 \leq n,m \leq 50\)) β€” the dimensions of each matrix.Each of the next \(n\) lines contains \(m\) integers \(a_{i1}, a_{i2}, \ldots, a_{im}\) (\(1 \leq a_{ij} \leq 10^9\)) β€” the number located in position \((i, j)\) in the first matrix.Each of the next \(n\) lines...
Print a string ""Impossible"" or ""Possible"".
The first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be \(\begin{bmatrix} 9&10\\ 11&12\\ \end{bmatrix}\) and \(\begin{bmatrix} 2&4\\ 3&5\\ \end{bmatrix}\).In the second example, we don't need to do any operations.In the third example, no matter wh...
Input: 2 2 2 10 11 5 9 4 3 12 | Output: Possible
Easy
2
1,357
475
46
11
1,030
G
1030G
G. Linear Congruential Generator
2,900
number theory
You are given a tuple generator \(f^{(k)} = (f_1^{(k)}, f_2^{(k)}, \dots, f_n^{(k)})\), where \(f_i^{(k)} = (a_i \cdot f_i^{(k - 1)} + b_i) \bmod p_i\) and \(f^{(0)} = (x_1, x_2, \dots, x_n)\). Here \(x \bmod y\) denotes the remainder of \(x\) when divided by \(y\). All \(p_i\) are primes.One can see that with fixed se...
The first line contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the number of elements in the tuple.The second line contains \(n\) space separated prime numbers β€” the modules \(p_1, p_2, \ldots, p_n\) (\(2 \le p_i \le 2 \cdot 10^6\)).
Print one integer β€” the maximum number of different tuples modulo \(10^9 + 7\).
In the first example we can choose next parameters: \(a = [1, 1, 1, 1]\), \(b = [1, 1, 1, 1]\), \(x = [0, 0, 0, 0]\), then \(f_i^{(k)} = k \bmod p_i\).In the second example we can choose next parameters: \(a = [1, 1, 2]\), \(b = [1, 1, 0]\), \(x = [0, 0, 1]\).
Input: 42 3 5 7 | Output: 210
Master
1
757
245
79
10
331
D3
331D3
D3. Escaping on Beaveractor
3,000
data structures; implementation; trees
Don't put up with what you're sick of! The Smart Beaver decided to escape from the campus of Beaver Science Academy (BSA). BSA is a b Γ— b square on a plane. Each point x, y (0 ≀ x, y ≀ b) belongs to BSA. To make the path quick and funny, the Beaver constructed a Beaveractor, an effective and comfortable types of transp...
The first line contains two integers: the number of traffic rules n and the size of the campus b, 0 ≀ n, 1 ≀ b. Next n lines contain the rules. Each line of the rules contains four space-separated integers x0, y0, x1, y1 β€” the beginning and the end of the arrow. It is guaranteed that all arrows are parallel to the coor...
Print q lines. Each line should contain two integers β€” the Beaveractor's coordinates at the final moment of time for each plan. If the Smart Beaver manages to leave the campus in time ti, print the coordinates of the last point in the campus he visited.
Input: 3 30 0 0 10 2 2 23 3 2 3120 0 L 00 0 L 10 0 L 20 0 L 30 0 L 40 0 L 50 0 L 62 0 U 22 0 U 33 0 U 51 3 D 21 3 R 2 | Output: 0 00 10 21 22 23 23 22 23 21 32 21 3
Master
3
1,205
1,162
253
3
1,850
F
1850F
F. We Were Both Children
1,300
brute force; implementation; math; number theory
Mihai and Slavic were looking at a group of \(n\) frogs, numbered from \(1\) to \(n\), all initially located at point \(0\). Frog \(i\) has a hop length of \(a_i\). Each second, frog \(i\) hops \(a_i\) units forward. Before any frogs start hopping, Slavic and Mihai can place exactly one trap in a coordinate in order to...
The first line of the input contains a single integer \(t\) (\(1 \le t \le 100\)) β€” the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β€” the number of frogs, which equals the distance Slavic and Mihai can trav...
For each test case output a single integer β€” the maximum number of frogs Slavic and Mihai can catch using a trap.
In the first test case, the frogs will hop as follows: Frog 1: \(0 \to 1 \to 2 \to 3 \to \mathbf{\color{red}{4}} \to \cdots\) Frog 2: \(0 \to 2 \to \mathbf{\color{red}{4}} \to 6 \to 8 \to \cdots\) Frog 3: \(0 \to 3 \to 6 \to 9 \to 12 \to \cdots\) Frog 4: \(0 \to \mathbf{\color{red}{4}} \to 8 \to 12 \to 16 \to \cdots\) ...
Input: 751 2 3 4 532 2 263 1 3 4 9 1091 3 2 4 2 3 7 8 511087 11 6 8 12 4 4 8109 11 9 12 1 7 2 5 8 10 | Output: 3 3 3 5 0 4 4
Easy
4
755
590
113
18
747
D
747D
D. Winter Is Coming
1,800
dp; greedy; sortings
The winter in Berland lasts n days. For each day we know the forecast for the average air temperature that day. Vasya has a new set of winter tires which allows him to drive safely no more than k days at any average air temperature. After k days of using it (regardless of the temperature of these days) the set of winte...
The first line contains two positive integers n and k (1 ≀ n ≀ 2Β·105, 0 ≀ k ≀ n) β€” the number of winter days and the number of days winter tires can be used. It is allowed to drive on winter tires at any temperature, but no more than k days in total.The second line contains a sequence of n integers t1, t2, ..., tn ( - ...
Print the minimum number of times Vasya has to change summer tires to winter tires and vice versa to drive safely during all winter. If it is impossible, print -1.
In the first example before the first winter day Vasya should change summer tires to winter tires, use it for three days, and then change winter tires to summer tires because he can drive safely with the winter tires for just three days. Thus, the total number of tires' changes equals two. In the second example before ...
Input: 4 3-5 20 -3 0 | Output: 2
Medium
3
992
387
163
7
1,758
A
1758A
A. SSeeeeiinngg DDoouubbllee
800
constructive algorithms; strings
A palindrome is a string that reads the same backward as forward. For example, the strings \(\texttt{z}\), \(\texttt{aaa}\), \(\texttt{aba}\), and \(\texttt{abccba}\) are palindromes, but \(\texttt{codeforces}\) and \(\texttt{ab}\) are not.The double of a string \(s\) is obtained by writing each character twice. For ex...
The first line of input contains \(t\) (\(1 \leq t \leq 1000\)) β€” the number of test cases.The only line of each test case contains a single string \(s\) (\(1 \leq |s| \leq 100\)) consisting only of lowercase English letters.Note that the sum of \(|s|\) over all test cases is not bounded.
For each test case, output a palindromic string of length \(2 \cdot |s|\) that is a rearrangement of the double of \(s\).
In the first test case, the double of \(\texttt{a}\) is \(\texttt{aa}\), which is already a palindrome.In the second test case, the double of \(\texttt{sururu}\) is \(\texttt{ssuurruurruu}\). If we move the first \(\texttt{s}\) to the end, we get \(\texttt{suurruurruus}\), which is a palindrome.In the third test case, ...
Input: 4asururuerrorgornanutforajaroftuna | Output: aa suurruurruus rgnororerrerorongr aannuuttffoorraajjaarrooffttuunnaa
Beginner
2
542
289
121
17
1,203
C
1203C
C. Common Divisors
1,300
implementation; math
You are given an array \(a\) consisting of \(n\) integers.Your task is to say the number of such positive integers \(x\) such that \(x\) divides each number from the array. In other words, you have to find the number of common divisors of all elements in the array.For example, if the array \(a\) will be \([2, 4, 6, 2, ...
The first line of the input contains one integer \(n\) (\(1 \le n \le 4 \cdot 10^5\)) β€” the number of elements in \(a\).The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^{12}\)), where \(a_i\) is the \(i\)-th element of \(a\).
Print one integer β€” the number of such positive integers \(x\) such that \(x\) divides each number from the given array (in other words, the answer is the number of common divisors of all elements in the array).
Input: 5 1 2 3 4 5 | Output: 1
Easy
2
421
274
211
12
417
E
417E
E. Square Table
2,400
constructive algorithms; math; probabilities
While resting on the ship after the ""Russian Code Cup"" a boy named Misha invented an interesting game. He promised to give his quadrocopter to whoever will be the first one to make a rectangular table of size n Γ— m, consisting of positive integers such that the sum of the squares of numbers for each row and each colu...
The first line contains two integers n and m (1 ≀ n, m ≀ 100) β€” the size of the table.
Print the table that meets the condition: n lines containing m integers, separated by spaces. If there are multiple possible answers, you are allowed to print anyone. It is guaranteed that there exists at least one correct answer.
Input: 1 1 | Output: 1
Expert
3
474
86
230
4
1
B
1B
B. Spreadsheets
1,600
implementation; math
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second β€” number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 β€” AB, column 52 is marked by AZ. After ZZ there follow th...
The first line of the input contains integer number n (1 ≀ n ≀ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .
Write n lines, each line should contain a cell coordinates in the other numeration system.
Input: 2R23C55BC23 | Output: BC23R23C55
Medium
2
905
278
90
0
2,043
E
2043E
E. Matrix Transformation
2,300
bitmasks; brute force; data structures; dfs and similar; graphs; greedy; implementation
You are given two matrices \(A\) and \(B\) of size \(n \times m\), filled with integers between \(0\) and \(10^9\). You can perform the following operations on matrix \(A\) in any order and any number of times: &=: choose two integers \(i\) and \(x\) (\(1 \le i \le n\), \(x \ge 0\)) and replace each element in row \(i\...
The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β€” the number of test cases. Then, \(t\) test cases follow.Each test case is given as follows: the first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 10^3\); \(n \cdot m \le 10^3\)) β€” the dimensions of the matrices \(A\) and \(B\); the ...
For each test case, output Yes if it is possible to transform the matrix \(A\) into the matrix \(B\); otherwise, output No. Each letter can be output in any case, upper or lower.
Let's consider the second set of input data and show a sequence of operations that transforms matrix \(A\) into matrix \(B\):Initially, the matrix looks like this:\(\begin{bmatrix} 10&10\\ 42&42\\ \end{bmatrix}\)Apply an operation of the first type with parameters \(i = 1\) and \(x = 0\). As a result, we get the matrix...
Input: 41 112132 210 1042 4221 2121 212 274 1042 10621 8585 212 41 2 3 45 6 7 83 2 3 41 0 1 0 | Output: Yes Yes No Yes
Expert
7
1,010
651
178
20
1,899
E
1899E
E. Queue Sort
1,300
greedy; implementation; sortings
Vlad found an array \(a\) of \(n\) integers and decided to sort it in non-decreasing order.To do this, Vlad can apply the following operation any number of times: Extract the first element of the array and insert it at the end; Swap that element with the previous one until it becomes the first or until it becomes stric...
The first line of the input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. Descriptions of the test cases follow.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the length of the array.The second line of each test case contains \(n\) ...
For each test case, output a single integer β€” the minimum number of operations needed to sort the array. If it is impossible to do so, output \(-1\) as the answer.
Input: 556 4 1 2 574 5 3 7 8 6 264 3 1 2 6 445 2 4 232 2 3 | Output: 2 6 -1 -1 0
Easy
3
881
503
163
18
78
B
78B
B. Easter Eggs
1,200
constructive algorithms; implementation
The Easter Rabbit laid n eggs in a circle and is about to paint them. Each egg should be painted one color out of 7: red, orange, yellow, green, blue, indigo or violet. Also, the following conditions should be satisfied: Each of the seven colors should be used to paint at least one egg. Any four eggs lying sequentially...
The only line contains an integer n β€” the amount of eggs (7 ≀ n ≀ 100).
Print one line consisting of n characters. The i-th character should describe the color of the i-th egg in the order they lie in the circle. The colors should be represented as follows: ""R"" stands for red, ""O"" stands for orange, ""Y"" stands for yellow, ""G"" stands for green, ""B"" stands for blue, ""I"" stands fo...
The way the eggs will be painted in the first sample is shown on the picture:
Input: 8 | Output: ROYGRBIV
Easy
2
454
71
402
0
1,628
F
1628F
F. Spaceship Crisis Management
3,500
binary search; data structures; geometry; sortings
NASA (Norwegian Astronaut Stuff Association) is developing a new steering system for spaceships. But in its current state, it wouldn't be very safe if the spaceship would end up in a bunch of space junk. To make the steering system safe, they need to answer the following:Given the target position \(t = (0, 0)\), a set ...
The first line contains the the integer \(n\) (\(1 \le n \le 1500\)).Then follows \(n\) lines, the \(i\)-th of which containing the \(4\) integers \(a_{ix}\), \(a_{iy}\), \(b_{ix}\), and \(b_{iy}\) (\(|a_{ix}|, |a_{iy}|, |b_{ix}|, |b_{iy}| \le 1000\)).Then follows a line containing the integer \(q\) (\(1 \le q \le 1000...
For each query \(s_j\) print an answer. If there exists a direction such that floating from \(s_j\) in that direction, possibly sliding along some pieces of space junk, leads to \(t\), print ""YES"". Otherwise, print ""NO"" (case insensitive).
The blue cross represents the target location, and the other blue line segments represent the space junk.Green dots represent starting locations where the answer is yes, and red dots represent starting locations where the answer is no.The yellow lines are possible paths to the target location for the \(3\)rd and \(14\)...
Input: 3 0 1 2 4 1 3 -1 6 0 -1 1 -1 14 -2 10 -1 10 0 10 1 10 2 10 3 10 4 10 5 10 6 10 -1 -2 0 -2 1 -2 2 -2 3 -2 | Output: YES YES YES YES YES NO NO NO YES YES NO NO NO YES
Master
4
1,386
640
243
16
57
A
57A
A. Square Earth?
1,300
dfs and similar; greedy; implementation
Meg the Rabbit decided to do something nice, specifically β€” to determine the shortest distance between two points on the surface of our planet. But Meg... what can you say, she wants everything simple. So, she already regards our planet as a two-dimensional circle. No, wait, it's even worse β€” as a square of side n. Thu...
The single line contains 5 space-separated integers: n, x1, y1, x2, y2 (1 ≀ n ≀ 1000, 0 ≀ x1, y1, x2, y2 ≀ n) which correspondingly represent a side of the square, the coordinates of the first point and the coordinates of the second point. It is guaranteed that the points lie on the sides of the square.
You must print on a single line the shortest distance between the points.
Input: 2 0 0 1 0 | Output: 1
Easy
3
592
304
73
0
958
C1
958C1
C1. Encryption (easy)
1,200
brute force
Rebel spy Heidi has just obtained the plans for the Death Star from the Empire and, now on her way to safety, she is trying to break the encryption of the plans (of course they are encrypted – the Empire may be evil, but it is not stupid!). The encryption has several levels of security, and here is how the first one lo...
The first line of the input contains two space-separated integer N and p (2 ≀ N ≀ 100 000, 2 ≀ p ≀ 10 000) – the number of elements in A, and the modulo for computing scores, respectively.The second line contains N space-separated integers which are the elements of A. Each integer is from the interval [1, 1 000 000].
Output the number S as described in the problem statement.
In the first example, the score is maximized if the input sequence is split into two parts as (3, 4), (7, 2). It gives the total score of .In the second example, the score is maximized if the first part consists of the first three elements, and the second part consists of the rest. Then, the score is .
Input: 4 103 4 7 2 | Output: 16
Easy
1
970
318
58
9
2,125
B
2125B
B. Left and Down
900
math; number theory
There is a robot located in the cell \((a,b)\) of an infinite grid. Misha wants to move it to the cell \((0,0)\). To do this, he has fixed some integer \(k\).Misha can perform the following operation: choose two integers \(dx\) and \(dy\) (both from \(0\) to \(k\) inclusive) and move the robot \(dx\) cells to the left ...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The only line of each test case contains three integers \(a, b\), and \(k\) (\(1 \le a, b, k \le 10^{18}\)).
For each test case, output a single integer β€” the minimum total cost of operations required to move the robot to the cell \((0,0)\).
In the first test case, the operation \((3,5)\) can be applied once. The robot will immediately go to \((0,0)\), and the cost of the operation will be \(1\).In the second test case, the operations: \((1,1)\), \((0,1)\), and \((1,1)\) can be applied. After the first operation, the robot will be at cell \((1,2)\), after ...
Input: 43 5 152 3 112 18 89 7 5 | Output: 1 2 1 2
Beginner
2
893
205
132
21
1,875
D
1875D
D. Jellyfish and Mex
1,600
dp
You are given an array of \(n\) nonnegative integers \(a_1, a_2, \dots, a_n\). Let \(m\) be a variable that is initialized to \(0\), Jellyfish will perform the following operation \(n\) times: select an index \(i\) (\(1 \leq i \leq |a|\)) and delete \(a_i\) from \(a\). add \(\operatorname{MEX}(a)^{\dagger}\) to \(m\). ...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 5000\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 5000\)) β€” the size of the array.The second line of each test case contains ...
For each test case, output a single integer β€” the minimum value of \(m\) if the operations are performed optimally.
In the first test case, we delete elements from \(a\) in the following order: \([5,2,\color{red}{1},0,3,0,4,0] \to [5,2,0,3,\color{red}{0},4,0] \to [5,2,\color{red}{0},3,4,0] \to [5,2,3,4,\color{red}{0}] \to [5,2,\color{red}{3},4] \to [\color{red}{5},2,4] \to [\color{red}{2},4] \to [\color{red}{4}] \to [~]\). The value...
Input: 485 2 1 0 3 0 4 021 251 0 2 114514 080 1 2 0 1 2 0 3 | Output: 3 0 2 7
Medium
1
864
499
115
18
1,209
H
1209H
H. Moving Walkways
3,300
data structures; greedy; math
Airports often use moving walkways to help you walking big distances faster. Each such walkway has some speed that effectively increases your speed. You can stand on such a walkway and let it move you, or you could also walk and then your effective speed is your walking speed plus walkway's speed.Limak wants to get fro...
The first line contains integers \(n\) and \(L\) (\(1 \le n \le 200\,000\), \(1 \le L \le 10^9\)), the number of walkways and the distance to walk.Each of the next \(n\) lines contains integers \(x_i\), \(y_i\) and real value \(s_i\) (\(0 \le x_i < y_i \le L\), \(0.1 \le s_i \le 10.0\)). The value \(s_i\) is given with...
Print one real value, the fastest possible time to reach \(L\). Your answer will be considered correct if its absolute or relative error won't exceed \(10^{-9}\).
The drawings show the first two examples. In the first one, there is a walkway from \(0\) to \(2\) with speed \(2.0\) and Limak wants to get to point \(5\). The second example has a walkway from \(2\) to \(4\) with speed \(0.91\). In the first example, one of optimal strategies is as follows. Get from \(0\) to \(2\) by...
Input: 1 5 0 2 2.0 | Output: 3.000000000000
Master
3
1,732
531
162
12
906
A
906A
A. Shockers
1,600
implementation; strings
Valentin participates in a show called ""Shockers"". The rules are quite easy: jury selects one letter which Valentin doesn't know. He should make a small speech, but every time he pronounces a word that contains the selected letter, he receives an electric shock. He can make guesses which letter is selected, but for e...
The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of actions Valentin did.The next n lines contain descriptions of his actions, each line contains description of one action. Each action can be of one of three types: Valentin pronounced some word and didn't get an electric shock. This action is descr...
Output a single integer β€” the number of electric shocks that Valentin could have avoided if he had told the selected letter just after it became uniquely determined.
In the first test case after the first action it becomes clear that the selected letter is one of the following: a, b, c. After the second action we can note that the selected letter is not a. Valentin tells word ""b"" and doesn't get a shock. After that it is clear that the selected letter is c, but Valentin pronounce...
Input: 5! abc. ad. b! cd? c | Output: 1
Medium
2
780
1,419
165
9
1,566
F
1566F
F. Points Movement
2,600
data structures; dp; greedy; implementation; sortings
There are \(n\) points and \(m\) segments on the coordinate line. The initial coordinate of the \(i\)-th point is \(a_i\). The endpoints of the \(j\)-th segment are \(l_j\) and \(r_j\) β€” left and right endpoints, respectively.You can move the points. In one move you can move any point from its current coordinate \(x\) ...
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. Description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n, m \le 2 \cdot 10^5\)) β€” the number of points and segments ...
For each test case print a single integer β€” the minimal total cost of all moves such that all segments are visited.
In the first test case the points can be moved as follows: Move the second point from the coordinate \(6\) to the coordinate \(5\). Move the third point from the coordinate \(14\) to the coordinate \(13\). Move the fourth point from the coordinate \(18\) to the coordinate \(17\). Move the third point from the coordinat...
Input: 2 4 11 2 6 14 18 0 3 4 5 11 15 3 5 10 13 16 16 1 4 8 12 17 19 7 13 14 19 4 12 -9 -16 12 3 -20 -18 -14 -13 -10 -7 -3 -1 0 4 6 11 7 9 8 10 13 15 14 18 16 17 18 19 | Output: 5 22
Expert
5
728
751
115
15
427
A
427A
A. Police Recruits
800
implementation
The police department of your city has just started its journey. Initially, they don’t have any manpower. So, they started hiring new recruits in groups.Meanwhile, crimes keeps occurring within the city. One member of the police force can investigate only one crime during his/her lifetime.If there is no police officer ...
The first line of input will contain an integer n (1 ≀ n ≀ 105), the number of events. The next line will contain n space-separated integers.If the integer is -1 then it means a crime has occurred. Otherwise, the integer will be positive, the number of officers recruited together at that time. No more than 10 officers ...
Print a single integer, the number of crimes which will go untreated.
Lets consider the second example: Firstly one person is hired. Then crime appears, the last hired person will investigate this crime. One more person is hired. One more crime appears, the last hired person will investigate this crime. Crime appears. There is no free policeman at the time, so this crime will go untreate...
Input: 3-1 -1 1 | Output: 2
Beginner
1
526
348
69
4
2,048
H
2048H
H. Kevin and Strange Operation
3,100
data structures; dp
Kevin is exploring problems related to binary strings in Chinatown. When he was at a loss, a stranger approached him and introduced a peculiar operation: Suppose the current binary string is \( t \), with a length of \( \vert t \vert \). Choose an integer \( 1 \leq p \leq \vert t \vert \). For all \( 1 \leq i < p \), s...
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1\le t \le 10^4\)) β€” the number of test cases.For each test case, the only line contains a binary string \( s \) (\( 1 \le \lvert s \rvert \le 10^6 \)).It is guaranteed that the sum of \(\lvert s \rvert\) over all test cases does ...
For each test case, print a single integer in the only line of the output β€” the number of distinct non-empty binary strings you can obtain, modulo \(998\,244\,353\).
In the first test case, all the binary strings you can obtain are: 11001, 1001, 1101, 001, 101, 111, 01, 11, and 1. There are \( 9 \) in total.
Input: 211001000110111001100 | Output: 9 73
Master
2
968
340
165
20
1,822
C
1822C
C. Bun Lover
800
math
Tema loves cinnabon rolls β€” buns with cinnabon and chocolate in the shape of a ""snail"".Cinnabon rolls come in different sizes and are square when viewed from above. The most delicious part of a roll is the chocolate, which is poured in a thin layer over the cinnabon roll in the form of a spiral and around the bun, as...
The first line of the input contains a single integer \(t\) (\(1 \le t \le 10^5\)) β€” the number of test cases.The following \(t\) lines describe the test cases.Each test case is described by a single integer \(n\) (\(4 \le n \le 10^9\)) β€” the size of the cinnabon roll.
Output \(t\) integers. The \(i\)-th of them should be equal to the total length of the chocolate layer in the \(i\)-th test case.
Input: 4456179179179 | Output: 26 37 50 32105178545472401
Beginner
1
1,123
269
129
18
1,184
E3
1184E3
E3. Daleks' Invasion (hard)
2,400
data structures; dsu; graphs; trees
With your help, Heidi has prepared a plan of trap placement and defence. Yet suddenly, the Doctor popped out of the TARDIS and told her that he had spied on the Daleks' preparations, and there is more of them than ever. Desperate times require desperate measures, so Heidi is going to risk meeting with the Daleks and sh...
First line: number \(n\) of destinations, number \(m\) of corridors (\(2 \leq n \leq 10^5\), \(n - 1 \leq m \leq 10^6\)). The next \(m\) lines: destinations \(a\), \(b\) and energy \(e\) (\(1 \leq a, b \leq n\), \(a \neq b\), \(0 \leq e \leq 10^9\)).No pair \(\{a, b\}\) will repeat. The graph is guaranteed to be connec...
Output \(m\) lines, each containing one integer: \(E_{max}(c_i)\) for the \(i\)-th Corridor \(c_i\) from the input.
Input: 3 31 2 82 3 33 1 4 | Output: 488
Expert
4
621
439
115
11
330
B
330B
B. Road Construction
1,300
constructive algorithms; graphs
A country has n cities. Initially, there is no road in the country. One day, the king decides to construct some roads connecting pairs of cities. Roads can be traversed either way. He wants those roads to be constructed in such a way that it is possible to go from each city to any other city by traversing at most two r...
The first line consists of two integers n and m .Then m lines follow, each consisting of two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi), which means that it is not possible to construct a road connecting cities ai and bi. Consider the cities are numbered from 1 to n.It is guaranteed that every pair of cities will app...
You should print an integer s: the minimum number of roads that should be constructed, in the first line. Then s lines should follow, each consisting of two integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi), which means that a road should be constructed between cities ai and bi.If there are several solutions, you may print ...
This is one possible solution of the example: These are examples of wrong solutions: The above solution is wrong because it doesn't use the minimum number of edges (4 vs 3). In addition, it also tries to construct a road between cities 1 and 3, while the input specifies that it is not allowed to construct a road betwee...
Input: 4 11 3 | Output: 31 24 22 3
Easy
2
578
350
332
3
1,485
B
1485B
B. Replace and Keep Sorted
1,200
dp; implementation; math
Given a positive integer \(k\), two arrays are called \(k\)-similar if: they are strictly increasing; they have the same length; all their elements are positive integers between \(1\) and \(k\) (inclusive); they differ in exactly one position. You are given an integer \(k\), a strictly increasing array \(a\) and \(q\) ...
The first line contains three integers \(n\), \(q\) and \(k\) (\(1\leq n, q \leq 10^5\), \(n\leq k \leq 10^9\)) β€” the length of array \(a\), the number of queries and number \(k\).The second line contains \(n\) integers \(a_1, a_2, \ldots,a_n\) (\(1 \leq a_i \leq k\)). This array is strictly increasing β€” \(a_1 < a_2 < ...
Print \(q\) lines. The \(i\)-th of them should contain the answer to the \(i\)-th query.
In the first example:In the first query there are \(4\) arrays that are \(5\)-similar to \([2,4]\): \([1,4],[3,4],[2,3],[2,5]\).In the second query there are \(3\) arrays that are \(5\)-similar to \([4,5]\): \([1,5],[2,5],[3,5]\).
Input: 4 2 5 1 2 4 5 2 3 3 4 | Output: 4 3
Easy
3
520
441
88
14
1,851
G
1851G
G. Vlad and the Mountains
2,000
binary search; data structures; dsu; graphs; implementation; sortings; trees; two pointers
Vlad decided to go on a trip to the mountains. He plans to move between \(n\) mountains, some of which are connected by roads. The \(i\)-th mountain has a height of \(h_i\).If there is a road between mountains \(i\) and \(j\), Vlad can move from mountain \(i\) to mountain \(j\) by spending \(h_j - h_i\) units of energy...
The first line of the input contains an integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The descriptions of the test cases follow.The first line of each test case contains two numbers \(n\) and \(m\) (\(2 \le n \le 2 \cdot 10^5\), \(1 \le m \le \min(\frac{n\cdot(n - 1)}{2}, 2 \cdot 10^5)\)) β€” the number...
For each query, output ""YES"" if Vlad can construct a route from mountain \(a\) to mountain \(b\), and ""NO"" otherwise.You can output the answer in any case (for example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as a positive answer).In the examples below, the answers for different test c...
Input: 27 71 5 3 4 2 4 11 44 33 63 22 55 65 751 1 36 2 04 7 01 7 41 7 26 54 7 6 2 5 11 35 31 52 46 251 5 11 3 11 2 10006 2 66 2 5 | Output: YES NO YES YES NO YES NO NO YES NO
Hard
8
762
1,139
389
18
1,195
C
1195C
C. Basketball Exercise
1,400
dp
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. \(2 \cdot n\) students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly \(n\) people in each row). Students are numbered from \(1\) to \(n\) in eac...
The first line of the input contains a single integer \(n\) (\(1 \le n \le 10^5\)) β€” the number of students in each row.The second line of the input contains \(n\) integers \(h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}\) (\(1 \le h_{1, i} \le 10^9\)), where \(h_{1, i}\) is the height of the \(i\)-th student in the first row.T...
Print a single integer β€” the maximum possible total height of players in a team Demid can choose.
In the first example Demid can choose the following team as follows: In the second example Demid can choose the following team as follows:
Input: 5 9 3 5 7 3 5 8 1 4 5 | Output: 29
Easy
1
1,133
518
97
11
1,303
F
1303F
F. Number of Components
2,800
dsu; implementation
You are given a matrix \(n \times m\), initially filled with zeroes. We define \(a_{i, j}\) as the element in the \(i\)-th row and the \(j\)-th column of the matrix.Two cells of the matrix are connected if they share a side, and the elements in these cells are equal. Two cells of the matrix belong to the same connected...
The first line contains three integers \(n\), \(m\) and \(q\) (\(1 \le n, m \le 300\), \(1 \le q \le 2 \cdot 10^6\)) β€” the number of rows, the number of columns and the number of queries, respectively.Then \(q\) lines follow, each representing a query. The \(i\)-th line contains three integers \(x_i\), \(y_i\) and \(c_...
Print \(q\) integers, the \(i\)-th of them should be equal to the number of components in the matrix after the first \(i\) queries are performed.
Input: 3 2 10 2 1 1 1 2 1 2 2 1 1 1 2 3 1 2 1 2 2 2 2 2 2 1 2 3 2 4 2 1 5 | Output: 2 4 3 3 4 4 4 2 2 4
Master
2
857
491
145
13
573
B
573B
B. Bear and Blocks
1,600
binary search; data structures; dp; math
Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of hi identical blocks. For clarification see picture for the first sample.Limak will repeat the following operation till everything is destroyed.Block is called internal if it ha...
The first line contains single integer n (1 ≀ n ≀ 105).The second line contains n space-separated integers h1, h2, ..., hn (1 ≀ hi ≀ 109) β€” sizes of towers.
Print the number of operations needed to destroy all towers.
The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color. After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation.
Input: 62 1 4 6 2 2 | Output: 3
Medium
4
694
156
60
5
1,209
D
1209D
D. Cow and Snacks
1,700
dfs and similar; dsu; graphs
The legendary Farmer John is throwing a huge party, and animals from all over the world are hanging out at his house. His guests are hungry, so he instructs his cow Bessie to bring out the snacks! Moo!There are \(n\) snacks flavors, numbered with integers \(1, 2, \ldots, n\). Bessie has \(n\) snacks, one snack of each ...
The first line contains integers \(n\) and \(k\) (\(2 \le n \le 10^5\), \(1 \le k \le 10^5\)), the number of snacks and the number of guests. The \(i\)-th of the following \(k\) lines contains two integers \(x_i\) and \(y_i\) (\(1 \le x_i, y_i \le n\), \(x_i \ne y_i\)), favorite snack flavors of the \(i\)-th guest.
Output one integer, the smallest possible number of sad guests.
In the first example, Bessie can order the guests like this: \(3, 1, 2, 4\). Guest \(3\) goes first and eats snacks \(1\) and \(4\). Then the guest \(1\) goes and eats the snack \(2\) only, because the snack \(1\) has already been eaten. Similarly, the guest \(2\) goes up and eats the snack \(3\) only. All the snacks a...
Input: 5 4 1 2 4 3 1 4 3 4 | Output: 1
Medium
3
794
316
63
12
839
E
839E
E. Mother of Dragons
2,700
brute force; graphs; math; meet-in-the-middle
There are n castles in the Lannister's Kingdom and some walls connect two castles, no two castles are connected by more than one wall, no wall connects a castle to itself. Sir Jaime Lannister has discovered that Daenerys Targaryen is going to attack his kingdom soon. Therefore he wants to defend his kingdom. He has k l...
The first line of the input contains two integers n and k (1 ≀ n ≀ 40, 1 ≀ k ≀ 1000).Then n lines follows. The i-th of these lines contains n integers ai, 1, ai, 2, ..., ai, n (). If castles i and j are connected by a wall, then ai, j = 1. Otherwise it is equal to 0.It is guaranteed that ai, j = aj, i and ai, i = 0 for...
Print the maximum possible sum of stabilities of the walls that Sir Jaime Lannister can achieve.Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer corre...
In the first sample, we can assign 0.5, 0.5, 0 liters of liquid to castles 1, 2, 3, respectively, to get the maximum sum (0.25).In the second sample, we can assign 1.0, 1.0, 1.0, 1.0 liters of liquid to castles 1, 2, 3, 4, respectively, to get the maximum sum (4.0)
Input: 3 10 1 01 0 00 0 0 | Output: 0.250000000000
Master
4
801
338
328
8
747
E
747E
E. Comments
1,700
dfs and similar; expression parsing; implementation; strings
A rare article in the Internet is posted without a possibility to comment it. On a Polycarp's website each article has comments feed.Each comment on Polycarp's website is a non-empty string consisting of uppercase and lowercase letters of English alphabet. Comments have tree-like structure, that means each comment exce...
The first line contains non-empty comments feed in the described format. It consists of uppercase and lowercase letters of English alphabet, digits and commas. It is guaranteed that each comment is a non-empty string consisting of uppercase and lowercase English characters. Each of the number of comments is integer (co...
Print comments in a format that is given in the statement. For each level of nesting, comments should be printed in the order they are given in the input.
The first example is explained in the statements.
Input: hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0 | Output: 3hello test one ok bye two a b
Medium
4
1,644
518
154
7
711
E
711E
E. ZS and The Birthday Paradox
2,300
math; number theory; probabilities
ZS the Coder has recently found an interesting concept called the Birthday Paradox. It states that given a random set of 23 people, there is around 50% chance that some two of them share the same birthday. ZS the Coder finds this very interesting, and decides to test this with the inhabitants of Udayland.In Udayland, t...
The first and only line of the input contains two integers n and k (1 ≀ n ≀ 1018, 2 ≀ k ≀ 1018), meaning that there are 2n days in a year and that ZS the Coder wants to interview exactly k people.
If the probability of at least two k people having the same birthday in 2n days long year equals (A β‰₯ 0, B β‰₯ 1, ), print the A and B in a single line.Since these numbers may be too large, print them modulo 106 + 3. Note that A and B must be coprime before their remainders modulo 106 + 3 are taken.
In the first sample case, there are 23 = 8 days in Udayland. The probability that 2 people have the same birthday among 2 people is clearly , so A = 1, B = 8.In the second sample case, there are only 21 = 2 days in Udayland, but there are 3 people, so it is guaranteed that two of them have the same birthday. Thus, the ...
Input: 3 2 | Output: 1 8
Expert
3
769
196
298
7
346
E
346E
E. Doodle Jump
3,000
math; number theory
In Doodle Jump the aim is to guide a four-legged creature called ""The Doodler"" up a never-ending series of platforms without falling. β€” Wikipedia. It is a very popular game and xiaodao likes it very much. One day when playing the game she wondered whether there exists a platform that the doodler couldn't reach due to...
The first line contains an integer t (1 ≀ t ≀ 104) β€” the number of problem instances. Each of the next t lines contains four integers a, n, p and h (1 ≀ a ≀ 109, 1 ≀ n < p ≀ 109, 0 ≀ h ≀ 109). It's guaranteed that a and p are co-prime.
For each problem instance, if the Doodler can reach the highest platform, output ""YES"", otherwise output ""NO"".
Input: 37 4 12 27 1 9 47 4 12 3 | Output: NONOYES
Master
2
1,320
235
114
3
1,357
D5
1357D5
D5. Quantum Classification - Dataset 7
0
*special
This problem is identical to the problem D1 in every aspect except the training dataset. Please refer to that problem for the full problem statement.
Beginner
1
149
0
0
13
1,257
E
1257E
E. The Contest
2,000
data structures; dp; greedy
A team of three programmers is going to play a contest. The contest consists of \(n\) problems, numbered from \(1\) to \(n\). Each problem is printed on a separate sheet of paper. The participants have decided to divide the problem statements into three parts: the first programmer took some prefix of the statements (so...
The first line contains three integers \(k_1, k_2\) and \(k_3\) (\(1 \le k_1, k_2, k_3 \le 2 \cdot 10^5, k_1 + k_2 + k_3 \le 2 \cdot 10^5\)) β€” the number of problems initially taken by the first, the second and the third participant, respectively.The second line contains \(k_1\) integers \(a_{1, 1}, a_{1, 2}, \dots, a_...
Print one integer β€” the minimum number of moves required to redistribute the problems so that the first participant gets the prefix of the problemset, the third participant gets the suffix of the problemset, and the second participant gets all of the remaining problems.
In the first example the third contestant should give the problem \(2\) to the first contestant, so the first contestant has \(3\) first problems, the third contestant has \(1\) last problem, and the second contestant has \(1\) remaining problem.In the second example the distribution of problems is already valid: the f...
Input: 2 1 2 3 1 4 2 5 | Output: 1
Hard
3
1,480
853
270
12
1,404
B
1404B
B. Tree Tag
1,900
dfs and similar; dp; games; trees
Alice and Bob are playing a fun game of tree tag.The game is played on a tree of \(n\) vertices numbered from \(1\) to \(n\). Recall that a tree on \(n\) vertices is an undirected, connected graph with \(n-1\) edges.Initially, Alice is located at vertex \(a\), and Bob at vertex \(b\). They take turns alternately, and A...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). Description of the test cases follows.The first line of each test case contains five integers \(n,a,b,da,db\) (\(2\le n\le 10^5\), \(1\le a,b\le n\), \(a\ne b\), \(1\le da,db\le n-1\)) β€” the number of ...
For each test case, output a single line containing the winner of the game: ""Alice"" or ""Bob"".
In the first test case, Alice can win by moving to vertex \(1\). Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices \(1\) or \(6\) is far...
Input: 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 | Output: Alice Bob Alice Alice
Hard
4
1,036
789
97
14
1,366
E
1366E
E. Two Arrays
2,100
binary search; brute force; combinatorics; constructive algorithms; dp; two pointers
You are given two arrays \(a_1, a_2, \dots , a_n\) and \(b_1, b_2, \dots , b_m\). Array \(b\) is sorted in ascending order (\(b_i < b_{i + 1}\) for each \(i\) from \(1\) to \(m - 1\)).You have to divide the array \(a\) into \(m\) consecutive subarrays so that, for each \(i\) from \(1\) to \(m\), the minimum on the \(i\...
The first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 2 \cdot 10^5\)) β€” the length of arrays \(a\) and \(b\) respectively.The second line contains \(n\) integers \(a_1, a_2, \dots , a_n\) (\(1 \le a_i \le 10^9\)) β€” the array \(a\).The third line contains \(m\) integers \(b_1, b_2, \dots , b_m\) (\(1 \l...
In only line print one integer β€” the number of ways to divide the array \(a\) modulo 998244353.
Input: 6 3 12 10 20 20 25 30 10 20 30 | Output: 2
Hard
6
908
371
95
13
1,059
C
1059C
C. Sequence Transformation
1,600
constructive algorithms; math
Let's call the following process a transformation of a sequence of length \(n\).If the sequence is empty, the process ends. Otherwise, append the greatest common divisor (GCD) of all the elements of the sequence to the result and remove one arbitrary element from the sequence. Thus, when the process ends, we have a seq...
The first and only line of input contains one integer \(n\) (\(1\le n\le 10^6\)).
Output \(n\) integers β€” the lexicographically maximum result of the transformation.
In the first sample the answer may be achieved this way: Append GCD\((1, 2, 3) = 1\), remove \(2\). Append GCD\((1, 3) = 1\), remove \(1\). Append GCD\((3) = 3\), remove \(3\). We get the sequence \([1, 1, 3]\) as the result.
Input: 3 | Output: 1 1 3
Medium
2
745
81
83
10
2,000
C
2000C
C. Numeric String Template
1,000
data structures; strings
Kristina has an array \(a\), called a template, consisting of \(n\) integers. She also has \(m\) strings, each consisting only of lowercase Latin letters. The strings are numbered from \(1\) to \(m\). She wants to check which strings match the template.A string \(s\) is considered to match the template if all of the fo...
The first line of input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The following descriptions are for the test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the number of elements in the array \(a\).The second line of each ...
For each test case, output \(m\) lines. On the \(i\)-th line (\(1 \le i \le m\)) output: ""YES"", if the string with index \(i\) matches the template; ""NO"" otherwise. You may output the answer in any case (for example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as a positive answer).
The first test case is explained in the problem statement.
Input: 353 5 2 1 32abfdaafbfa21 23ababcaa45 -3 5 -34aaaabcbcabacbcb | Output: YES NO YES NO NO NO YES NO YES
Beginner
2
1,060
905
313
20
1,765
D
1765D
D. Watch the Videos
1,700
binary search; constructive algorithms; two pointers
Monocarp wants to watch \(n\) videos. Each video is only one minute long, but its size may be arbitrary. The \(i\)-th video has the size \(a_i\) megabytes. All videos are published on the Internet. A video should be downloaded before it can be watched. Monocarp has poor Internet connection β€” it takes exactly \(1\) minu...
The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 2 \cdot 10^5\); \(1 \le m \le 10^9\)) β€” the number of videos Monocarp wants to watch and the size of the hard disk, respectively. The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le m\)) β€” the sizes of the videos.
Print one integer β€” the minimum time required to watch all \(n\) videos.
Input: 5 6 1 2 3 4 5 | Output: 16
Medium
3
1,631
311
72
17
2,036
E
2036E
E. Reverse the Rivers
1,600
binary search; constructive algorithms; data structures; greedy
A conspiracy of ancient sages, who decided to redirect rivers for their own convenience, has put the world on the brink. But before implementing their grand plan, they decided to carefully think through their strategy β€” that's what sages do.There are \(n\) countries, each with exactly \(k\) regions. For the \(j\)-th re...
The first line contains three integers \(n\), \(k\), and \(q\) (\(1 \leq n, k, q \leq 10^5\)) β€” the number of countries, regions, and queries, respectively.Next, there are \(n\) lines, where the \(i\)-th line contains \(k\) integers \(a_{i,1}, a_{i,2}, \dots, a_{i,k}\) (\(1 \leq a_{i,j} \leq 10^9\)), where \(a_{i,j}\) ...
For each query, output a single integer on a new line β€” the smallest number of the suitable country, or \(-1\) if no such country exists.
In the example, the initial values of the regions are as follows: \(1\)\(3\)\(5\)\(9\)\(4\)\(6\)\(5\)\(3\)\(2\)\(1\)\(2\)\(7\) After creating the channels, the new values will look like this: \(1\)\(3\)\(5\)\(9\)\(1 | 4\)\(3 | 6\)\(5 | 5\)\(9 | 3\)\(1 | 4 | 2\)\(3 | 6 | 1\)\(5 | 5 | 2\)\(9 | 3 | 7\) \(\downarrow\) \(1\...
Input: 3 4 41 3 5 94 6 5 32 1 2 731 > 42 < 81 < 621 < 82 > 813 > 524 > 81 < 8 | Output: 2 -1 3 1
Medium
4
1,925
932
137
20
785
B
785B
B. Anton and Classes
1,100
greedy; sortings
Anton likes to play chess. Also he likes to do programming. No wonder that he decided to attend chess classes and programming classes.Anton has n variants when he will attend chess classes, i-th variant is given by a period of time (l1, i, r1, i). Also he has m variants when he will attend programming classes, i-th var...
The first line of the input contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of time periods when Anton can attend chess classes.Each of the following n lines of the input contains two integers l1, i and r1, i (1 ≀ l1, i ≀ r1, i ≀ 109) β€” the i-th variant of a period of time when Anton can attend chess classes...
Output one integer β€” the maximal possible distance between time periods.
In the first sample Anton can attend chess classes in the period (2, 3) and attend programming classes in the period (6, 8). It's not hard to see that in this case the distance between the periods will be equal to 3.In the second sample if he chooses any pair of periods, they will intersect. So the answer is 0.
Input: 31 52 62 322 46 8 | Output: 3
Easy
2
1,142
658
72
7
1,666
J
1666J
J. Job Lookup
2,100
constructive algorithms; dp; shortest paths; trees
Julia's \(n\) friends want to organize a startup in a new country they moved to. They assigned each other numbers from 1 to \(n\) according to the jobs they have, from the most front-end tasks to the most back-end ones. They also estimated a matrix \(c\), where \(c_{ij} = c_{ji}\) is the average number of messages per ...
The first line contains an integer \(n\) (\(1 \le n \le 200\)) – the number of team members organizing a startup.The next \(n\) lines contain \(n\) integers each, \(j\)-th number in \(i\)-th line is \(c_{ij}\) β€” the estimated number of messages per month between team members \(i\) and \(j\) (\(0 \le c_{ij} \le 10^9; c_...
Output a description of a hierarchy tree that minimizes the total cost of communication. To do so, for each team member from 1 to \(n\) output the number of the member in its parent node, or 0 for the leader. If there are many optimal trees, output a description of any one of them.
The minimal possible total cost is \(566 \cdot 1+239 \cdot 1+30 \cdot 1+1 \cdot 2+1 \cdot 2=839\):
Input: 4 0 566 1 0 566 0 239 30 1 239 0 1 0 30 1 0 | Output: 2 4 2 0
Hard
4
1,263
349
282
16
2,073
C
2073C
2,700
Master
0
0
0
0
20
1,255
C
1255C
C. League of Leesins
1,600
constructive algorithms; implementation
Bob is an avid fan of the video game ""League of Leesins"", and today he celebrates as the League of Leesins World Championship comes to an end! The tournament consisted of \(n\) (\(n \ge 5\)) teams around the world. Before the tournament starts, Bob has made a prediction of the rankings of each team, from \(1\)-st to ...
The first line contains a single integer \(n\) (\(5 \le n \le 10^5\)) β€” the size of permutation \(p\).The \(i\)-th of the next \(n-2\) lines contains \(3\) integers \(q_{i, 1}\), \(q_{i, 2}\), \(q_{i, 3}\) (\(1 \le q_{i, j} \le n\)) β€” the elements of the \(i\)-th triple of the rearranged (shuffled) array \(q_i\), in ra...
Print \(n\) distinct integers \(p_1, p_2, \ldots, p_n\) (\(1 \le p_i \le n\)) such that \(p\) is consistent with array \(q\). If there are multiple answers, print any.
Input: 5 4 3 2 2 3 5 4 1 2 | Output: 1 4 2 3 5
Medium
2
1,839
548
167
12
994
B
994B
B. Knights of a Polygonal Table
1,400
greedy; implementation; sortings
Unlike Knights of a Round Table, Knights of a Polygonal Table deprived of nobility and happy to kill each other. But each knight has some power and a knight can kill another knight if and only if his power is greater than the power of victim. However, even such a knight will torment his conscience, so he can kill no mo...
The first line contains two integers \(n\) and \(k\) \((1 \le n \le 10^5, 0 \le k \le \min(n-1,10))\) β€” the number of knights and the number \(k\) from the statement.The second line contains \(n\) integers \(p_1, p_2 ,\ldots,p_n\) \((1 \le p_i \le 10^9)\) β€” powers of the knights. All \(p_i\) are distinct.The third line...
Print \(n\) integers β€” the maximum number of coins each knight can have it only he kills other knights.
Consider the first example. The first knight is the weakest, so he can't kill anyone. That leaves him with the only coin he initially has. The second knight can kill the first knight and add his coin to his own two. The third knight is the strongest, but he can't kill more than \(k = 2\) other knights. It is optimal to...
Input: 4 24 5 9 71 2 11 33 | Output: 1 3 46 36
Easy
3
578
433
103
9
2,000
D
2000D
D. Right Left Wrong
1,200
greedy; implementation; two pointers
Vlad found a strip of \(n\) cells, numbered from left to right from \(1\) to \(n\). In the \(i\)-th cell, there is a positive integer \(a_i\) and a letter \(s_i\), where all \(s_i\) are either 'L' or 'R'.Vlad invites you to try to score the maximum possible points by performing any (possibly zero) number of operations....
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The first line of each test case contains one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β€” the length of the strip.The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^5\)) β€” t...
For each test case, output one integer β€” the maximum possible number of points that can be scored.
Input: 463 5 1 4 3 2LRLLLR22 8LR23 9RL51 2 3 4 5LRLRR | Output: 18 10 0 22
Easy
3
1,097
549
98
20