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,136
B
1136B
B. Nastya Is Playing Computer Games
1,000
constructive algorithms; math
Finished her homework, Nastya decided to play computer games. Passing levels one by one, Nastya eventually faced a problem. Her mission is to leave a room, where a lot of monsters live, as quickly as possible.There are \(n\) manholes in the room which are situated on one line, but, unfortunately, all the manholes are c...
The first and only line contains two integers \(n\) and \(k\), separated by space (\(2 \leq n \leq 5000\), \(1 \leq k \leq n\)) β€” the number of manholes and the index of manhole from the left, near which Nastya stays initially. Initially there is exactly one stone near each of the \(n\) manholes.
Print a single integer β€” minimum number of moves which lead Nastya to pick all the coins.
Let's consider the example where \(n = 2\), \(k = 2\). Nastya should play as follows: At first she throws the stone from the second manhole to the first. Now there are two stones on the first manhole. Then she opens the second manhole and pick the coin from it. Then she goes to the first manhole, throws two stones by t...
Input: 2 2 | Output: 6
Beginner
2
1,501
297
89
11
1,207
G
1207G
G. Indie Album
2,700
data structures; dfs and similar; hashing; string suffix structures; strings; trees
Mishka's favourite experimental indie band has recently dropped a new album! Songs of that album share one gimmick. Each name \(s_i\) is one of the following types: \(1~c\) β€” a single lowercase Latin letter; \(2~j~c\) β€” name \(s_j\) (\(1 \le j < i\)) with a single lowercase Latin letter appended to its end. Songs are n...
The first line contains a single integer \(n\) (\(1 \le n \le 4 \cdot 10^5\)) β€” the number of songs in the album.Each of the next \(n\) lines contains the desciption of the \(i\)-th song of the album in the following format: \(1~c\) β€” \(s_i\) is a single lowercase Latin letter; \(2~j~c\) β€” \(s_i\) is the name \(s_j\) (...
For each question print a single integer β€” the number of occurrences of the question string \(t\) in the name of the \(i\)-th song of the album as a continuous substring.
Song names of the first example: d da dad dada dadad dadada dadadad dadadada d do dok doki dokid dokido dokidok dokidoki do dok doki dokidoki Thus the occurrences for each question string are: string ""da"" starts in positions \([1, 3, 5, 7]\) in the name ""dadadada""; string ""dada"" starts in positions \([1, 3, 5]\) ...
Input: 20 1 d 2 1 a 2 2 d 2 3 a 2 4 d 2 5 a 2 6 d 2 7 a 1 d 2 9 o 2 10 k 2 11 i 2 12 d 2 13 o 2 14 k 2 15 i 2 1 o 2 17 k 2 18 i 2 15 i 12 8 da 8 dada 8 ada 6 dada 3 dada 19 doki 19 ok 16 doki 15 doki 9 d 1 a 20 doki | Output: 4 3 3 2 0 1 1 2 1 1 0 2
Master
6
977
952
170
12
353
E
353E
E. Antichain
2,200
dp; graph matchings; greedy
You have a directed acyclic graph G, consisting of n vertexes, numbered from 0 to n - 1. The graph contains n edges numbered from 0 to n - 1. An edge with number i connects vertexes i and (i + 1) mod n, and it can be directed in either direction (from i to (i + 1) mod n, or vise versa).Operation x mod y means taking th...
The first line contains the sequence of characters s0s1... sn - 1 (2 ≀ n ≀ 106), consisting of numbers zero and one. The length of the line (number n) corresponds to the number of vertexes and edges in graph G. If character si (i β‰₯ 0) equals 0, then the edge between vertexes i and (i + 1) mod n is directed from the i-t...
Print a single integer β€” the size of the maximum antichain of graph G.
Consider the first test sample. The graph's G edges are: 0 β†’ 1, 1 β†’ 2, 0 β†’ 2. We can choose the set of vertexes [0] as the maximum antichain. We cannot choose an antichain of larger size.
Input: 001 | Output: 1
Hard
3
823
441
70
3
341
D
341D
D. Iahub and Xors
2,500
data structures
Iahub does not like background stories, so he'll tell you exactly what this problem asks you for.You are given a matrix a with n rows and n columns. Initially, all values of the matrix are zeros. Both rows and columns are 1-based, that is rows are numbered 1, 2, ..., n and columns are numbered 1, 2, ..., n. Let's denot...
The first line contains two integers: n (1 ≀ n ≀ 1000) and m (1 ≀ m ≀ 105). The number m represents the number of operations you need to perform. Each of the next m lines contains five or six integers, depending on operation type. If the i-th operation from the input is a query, the first number from i-th line will be ...
For each query operation, output on a new line the result.
After the first 3 operations, the matrix will look like this: 1 1 21 1 23 3 3The fourth operation asks us to compute 1 xor 2 xor 3 xor 3 = 3.The fifth operation asks us to compute 1 xor 3 = 2.
Input: 3 52 1 1 2 2 12 1 3 2 3 22 3 1 3 3 31 2 2 3 31 2 2 3 2 | Output: 32
Expert
1
734
717
58
3
1,452
E
1452E
E. Two Editorials
2,500
brute force; dp; greedy; sortings; two pointers
Berland regional ICPC contest has just ended. There were \(m\) participants numbered from \(1\) to \(m\), who competed on a problemset of \(n\) problems numbered from \(1\) to \(n\).Now the editorial is about to take place. There are two problem authors, each of them is going to tell the tutorial to exactly \(k\) conse...
The first line contains three integers \(n, m\) and \(k\) (\(1 \le n, m \le 2000\), \(1 \le k \le n\)) β€” the number of problems, the number of participants and the length of the segment of tasks each of the problem authors plans to tell the tutorial to.The \(i\)-th of the next \(m\) lines contains two integers \(l_i\) ...
Print a single integer β€” the maximum sum of \(a_i\) over all participants.
In the first example the first author can tell the tutorial to problems from \(1\) to \(3\) and the second one β€” from \(6\) to \(8\). That way the sequence of \(a_i\) will be \([3, 2, 3, 3, 3]\). Notice that the last participant can't listen to both author, he only chooses the one that tells the maximum number of probl...
Input: 10 5 3 1 3 2 4 6 9 6 9 1 8 | Output: 14
Expert
5
1,059
456
74
14
1,553
D
1553D
D. Backspace
1,500
dp; greedy; strings; two pointers
You are given two strings \(s\) and \(t\), both consisting of lowercase English letters. You are going to type the string \(s\) character by character, from the first character to the last one.When typing a character, instead of pressing the button corresponding to it, you can press the ""Backspace"" button. It deletes...
The first line contains a single integer \(q\) (\(1 \le q \le 10^5\)) β€” the number of test cases.The first line of each test case contains the string \(s\) (\(1 \le |s| \le 10^5\)). Each character of \(s\) is a lowercase English letter.The second line of each test case contains the string \(t\) (\(1 \le |t| \le 10^5\))...
For each test case, print ""YES"" if you can obtain the string \(t\) by typing the string \(s\) and replacing some characters with presses of ""Backspace"" button, or ""NO"" if you cannot.You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized...
Consider the example test from the statement.In order to obtain ""ba"" from ""ababa"", you may press Backspace instead of typing the first and the fourth characters.There's no way to obtain ""bb"" while typing ""ababa"".There's no way to obtain ""aaaa"" while typing ""aaa"".In order to obtain ""ababa"" while typing ""a...
Input: 4 ababa ba ababa bb aaa aaaa aababa ababa | Output: YES NO NO YES
Medium
4
1,013
497
341
15
207
A3
207A3
A3. Beaver's Calculator 1.0
2,000
greedy
The Smart Beaver from ABBYY has once again surprised us! He has developed a new calculating device, which he called the ""Beaver's Calculator 1.0"". It is very peculiar and it is planned to be used in a variety of scientific problems.To test it, the Smart Beaver invited n scientists, numbered from 1 to n. The i-th scie...
The first line contains integer n β€” the number of scientists. To lessen the size of the input, each of the next n lines contains five integers ki, ai, 1, xi, yi, mi (0 ≀ ai, 1 < mi ≀ 109, 1 ≀ xi, yi ≀ 109) β€” the number of problems of the i-th scientist, the resources the first problem requires and three parameters that...
On the first line print a single number β€” the number of ""bad"" pairs in the optimal order.If the total number of problems does not exceed 200000, also print lines β€” the optimal order of the problems. On each of these lines print two integers separated by a single space β€” the required number of resources for the proble...
In the first sample n = 2, k1 = 2, a1, 1 = 1, a1, 2 = 2, k2 = 2, a2, 1 = 3, a2, 2 = 4. We've got two scientists, each of them has two calculating problems. The problems of the first scientist require 1 and 2 resource units, the problems of the second one require 3 and 4 resource units. Let's list all possible variants ...
Input: 22 1 1 1 102 3 1 1 10 | Output: 01 12 13 24 2
Hard
1
1,884
928
456
2
701
A
701A
A. Cards
800
greedy; implementation
There are n cards (n is even) in the deck. Each card has a positive integer written on it. n / 2 people will play new card game. At the beginning of the game each player gets two cards, each card is given to exactly one player. Find the way to distribute cards such that the sum of values written of the cards will be eq...
The first line of the input contains integer n (2 ≀ n ≀ 100) β€” the number of cards in the deck. It is guaranteed that n is even.The second line contains the sequence of n positive integers a1, a2, ..., an (1 ≀ ai ≀ 100), where ai is equal to the number written on the i-th card.
Print n / 2 pairs of integers, the i-th pair denote the cards that should be given to the i-th player. Each card should be given to exactly one player. Cards are numbered in the order they appear in the input.It is guaranteed that solution exists. If there are several correct answers, you are allowed to print any of th...
In the first sample, cards are distributed in such a way that each player has the sum of numbers written on his cards equal to 8. In the second sample, all values ai are equal. Thus, any distribution is acceptable.
Input: 61 5 7 4 4 3 | Output: 1 36 24 5
Beginner
2
385
278
323
7
73
E
73E
E. Morrowindows
2,400
math; number theory
Vasya plays The Elder Trolls III: Morrowindows. He has a huge list of items in the inventory, however, there is no limits on the size of things. Vasya does not know the total amount of items but he is sure that are not more than x and not less than 2 items in his inventory. A new patch for the game appeared to view inv...
The first line contains two integers n and x (0 ≀ n ≀ 105, 2 ≀ x ≀ 109). The second line contains integers ai (1 ≀ ai ≀ 109). Some numbers among all ai may be equal.
Output the fewest amount of modes required to uniquely determine amount of items in the inventory. If there is no solution output - 1.
In the second example Vasya is not able to determine items count uniquely because 3 items, as well as 4 items, can be displayed on two pages.
Input: 2 42 3 | Output: 2
Expert
2
1,283
165
134
0
269
C
269C
C. Flawed Flow
2,100
constructive algorithms; flows; graphs; greedy
Emuskald considers himself a master of flow algorithms. Now he has completed his most ingenious program yet β€” it calculates the maximum flow in an undirected graph. The graph consists of n vertices and m edges. Vertices are numbered from 1 to n. Vertices 1 and n being the source and the sink respectively.However, his m...
The first line of input contains two space-separated integers n and m (2 ≀ n ≀ 2Β·105, n - 1 ≀ m ≀ 2Β·105), the number of vertices and edges in the graph. The following m lines contain three space-separated integers ai, bi and ci (1 ≀ ai, bi ≀ n, ai β‰  bi, 1 ≀ ci ≀ 104), which means that there is an undirected edge from a...
Output m lines, each containing one integer di, which should be 0 if the direction of the i-th edge is ai β†’ bi (the flow goes from vertex ai to vertex bi) and should be 1 otherwise. The edges are numbered from 1 to m in the order they are given in the input.If there are several solutions you can print any of them.
In the first test case, 10 flow units pass through path , and 5 flow units pass directly from source to sink: .
Input: 3 33 2 101 2 103 1 5 | Output: 101
Hard
4
968
478
315
2
248
A
248A
A. Cupboards
800
implementation
One foggy Stockholm morning, Karlsson decided to snack on some jam in his friend Lillebror Svantenson's house. Fortunately for Karlsson, there wasn't anybody in his friend's house. Karlsson was not going to be hungry any longer, so he decided to get some food in the house.Karlsson's gaze immediately fell on n wooden cu...
The first input line contains a single integer n β€” the number of cupboards in the kitchen (2 ≀ n ≀ 104). Then follow n lines, each containing two integers li and ri (0 ≀ li, ri ≀ 1). Number li equals one, if the left door of the i-th cupboard is opened, otherwise number li equals zero. Similarly, number ri equals one, ...
In the only output line print a single integer t β€” the minimum number of seconds Karlsson needs to change the doors of all cupboards to the position he needs.
Input: 50 11 00 11 10 1 | Output: 3
Beginner
1
1,839
458
158
2
959
E
959E
E. Mahmoud and Ehab and the xor-MST
1,900
bitmasks; dp; graphs; implementation; math
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≀ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight (where is the bitwise-xor o...
The only line contains an integer n (2 ≀ n ≀ 1012), the number of vertices in the graph.
The only line contains an integer x, the weight of the graph's minimum spanning tree.
In the first sample: The weight of the minimum spanning tree is 1+2+1=4.
Input: 4 | Output: 4
Hard
5
673
88
85
9
230
B
230B
B. T-primes
1,300
binary search; implementation; math; number theory
We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Π’-prime, if t has exactly three distinct positive divisors.You are given an array of n positive integers. For each of them determine whether it is Π’-prime or not.
The first line contains a single positive integer, n (1 ≀ n ≀ 105), showing how many numbers are in the array. The next line contains n space-separated integers xi (1 ≀ xi ≀ 1012).Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is advised to use the cin, cout streams or the %I64d speci...
Print n lines: the i-th line should contain ""YES"" (without the quotes), if number xi is Π’-prime, and ""NO"" (without the quotes), if it isn't.
The given test has three numbers. The first number 4 has exactly three divisors β€” 1, 2 and 4, thus the answer for this number is ""YES"". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is ""NO"".
Input: 34 5 6 | Output: YESNONO
Easy
4
304
325
144
2
1,943
A
1943A
A. MEX Game 1
1,300
games; greedy
Alice and Bob play yet another game on an array \(a\) of size \(n\). Alice starts with an empty array \(c\). Both players take turns playing, with Alice starting first.On Alice's turn, she picks one element from \(a\), appends that element to \(c\), and then deletes it from \(a\).On Bob's turn, he picks one element fro...
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 2 \cdot 10^4\)) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)).The second line of each test case ...
For each test case, find game's score if both players play optimally.
In the first test case, a possible game with a score of \(2\) is as follows: Alice chooses the element \(1\). After this move, \(a=[0,0,1]\) and \(c=[1]\). Bob chooses the element \(0\). After this move, \(a=[0,1]\) and \(c=[1]\). Alice chooses the element \(0\). After this move, \(a=[1]\) and \(c=[1,0]\). Bob chooses ...
Input: 340 0 1 140 1 2 321 1 | Output: 2 1 0
Easy
2
1,057
482
69
19
103
A
103A
A. Testing Pants for Sadness
1,100
greedy; implementation; math
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called ""Testing Pants for Sadness"".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to ...
The first line contains a positive integer n (1 ≀ n ≀ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≀ ai ≀ 109), the number of answer variants to question i.
Print a single number β€” the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator.
Note to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click...
Input: 21 1 | Output: 2
Easy
3
1,058
227
257
1
1,607
E
1607E
E. Robot on the Board 1
1,600
implementation
The robot is located on a checkered rectangular board of size \(n \times m\) (\(n\) rows, \(m\) columns). The rows in the board are numbered from \(1\) to \(n\) from top to bottom, and the columns β€” from \(1\) to \(m\) from left to right.The robot is able to move from the current cell to one of the four cells adjacent ...
The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β€” the number of test cases.The next \(2t\) lines contain descriptions of the test cases.In the description of each test case, the first line contains two integers \(n\) and \(m\) (\(1 \leq n, m \leq 10^6\)) β€” the height and width of the field that the ro...
Print \(t\) lines, each of which contains the answer to the corresponding test case. The answer to the test case are two integers \(r\) (\(1 \leq r \leq n\)) and \(c\) (\(1 \leq c \leq m\)), separated by a space β€” the coordinates of the cell (row number and column number) from which the robot should start moving to per...
Input: 4 1 1 L 1 2 L 3 3 RRDLUU 4 3 LUURRDDLLLUU | Output: 1 1 1 2 2 1 3 2
Medium
1
1,705
642
414
16
2,043
G
2043G
G. Problem with Queries
3,000
brute force; data structures; implementation
You are given an array \(a\), consisting of \(n\) integers. Your task is to process \(q\) queries of two types: \(1~p~x\) β€” set the value of the element at index \(p\) equal to \(x\); \(2~l~r\) β€” count the number of pairs of indices \((i, j)\) such that \(l \le i < j \le r\) and \(a_i \ne a_j\). Note that the queries i...
The first line contains a single integer \(n\) (\(1 \le n \le 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le n\)).The third line contains a single integer \(q\) (\(1 \le q \le 3 \cdot 10^5\)) β€” the number of queries.The next \(q\) lines describe the queries in one of the follo...
For each query of the second type, print the answer β€” the number of pairs of indices \((i, j)\) such that \(l \le i < j \le r\) and \(a_i \ne a_j\).
In the first example, the actual queries (after decoding) are: 2 1 3 1 1 3 2 1 3 1 2 3 2 1 3
Input: 31 2 352 0 21 0 22 0 21 2 02 1 0 | Output: 3 2 0
Master
3
458
1,038
148
20
229
A
229A
A. Shifts
1,500
brute force; two pointers
You are given a table consisting of n rows and m columns. Each cell of the table contains a number, 0 or 1. In one move we can choose some row of the table and cyclically shift its values either one cell to the left, or one cell to the right.To cyclically shift a table row one cell to the right means to move the value ...
The first line contains two space-separated integers: n (1 ≀ n ≀ 100) β€” the number of rows in the table and m (1 ≀ m ≀ 104) β€” the number of columns in the table. Then n lines follow, each of them contains m characters ""0"" or ""1"": the j-th character of the i-th line describes the contents of the cell in the i-th row...
Print a single number: the minimum number of moves needed to get only numbers 1 in some column of the table. If this is impossible, print -1.
In the first sample one way to achieve the goal with the least number of moves is as follows: cyclically shift the second row to the right once, then shift the third row to the left twice. Then the table column before the last one will contain only 1s.In the second sample one can't shift the rows to get a column contai...
Input: 3 6101010000100100000 | Output: 3
Medium
2
811
461
141
2
1,301
A
1301A
A. Three Strings
800
implementation; strings
You are given three strings \(a\), \(b\) and \(c\) of the same length \(n\). The strings consist of lowercase English letters only. The \(i\)-th letter of \(a\) is \(a_i\), the \(i\)-th letter of \(b\) is \(b_i\), the \(i\)-th letter of \(c\) is \(c_i\).For every \(i\) (\(1 \leq i \leq n\)) you must swap (i.e. exchange...
The input consists of multiple test cases. 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 a string of lowercase English letters \(a\).The second line of each test case contains a strin...
Print \(t\) lines with answers for all test cases. For each test case:If it is possible to make string \(a\) equal to string \(b\) print ""YES"" (without quotes), otherwise print ""NO"" (without quotes).You can print either lowercase or uppercase letters in the answers.
In the first test case, it is impossible to do the swaps so that string \(a\) becomes exactly the same as string \(b\).In the second test case, you should swap \(c_i\) with \(a_i\) for all possible \(i\). After the swaps \(a\) becomes ""bca"", \(b\) becomes ""bca"" and \(c\) becomes ""abc"". Here the strings \(a\) and ...
Input: 4 aaa bbb ccc abc bca bca aabb bbaa baba imi mii iim | Output: NO YES YES NO
Beginner
2
928
574
270
13
1,290
B
1290B
B. Irreducible Anagrams
1,800
binary search; constructive algorithms; data structures; strings; two pointers
Let's call two strings \(s\) and \(t\) anagrams of each other if it is possible to rearrange symbols in the string \(s\) to get a string, equal to \(t\).Let's consider two strings \(s\) and \(t\) which are anagrams of each other. We say that \(t\) is a reducible anagram of \(s\) if there exists an integer \(k \ge 2\) a...
The first line contains a string \(s\), consisting of lowercase English characters (\(1 \le |s| \le 2 \cdot 10^5\)).The second line contains a single integer \(q\) (\(1 \le q \le 10^5\)) β€” the number of queries.Each of the following \(q\) lines contain two integers \(l\) and \(r\) (\(1 \le l \le r \le |s|\)), represent...
For each query, print a single line containing ""Yes"" (without quotes) if the corresponding substring has at least one irreducible anagram, and a single line containing ""No"" (without quotes) otherwise.
In the first sample, in the first and third queries, the substring is ""a"", which has itself as an irreducible anagram since two or more non-empty strings cannot be put together to obtain ""a"". On the other hand, in the second query, the substring is ""aaa"", which has no irreducible anagrams: its only anagram is its...
Input: aaaaa 3 1 1 2 4 5 5 | Output: Yes No Yes
Medium
5
1,601
414
204
12
1,242
C
1242C
C. Sum Balance
2,400
bitmasks; dfs and similar; dp; graphs
Ujan has a lot of numbers in his boxes. He likes order and balance, so he decided to reorder the numbers.There are \(k\) boxes numbered from \(1\) to \(k\). The \(i\)-th box contains \(n_i\) integer numbers. The integers can be negative. All of the integers are distinct. Ujan is lazy, so he will do the following reorde...
The first line contains a single integer \(k\) (\(1 \leq k \leq 15\)), the number of boxes. The \(i\)-th of the next \(k\) lines first contains a single integer \(n_i\) (\(1 \leq n_i \leq 5\,000\)), the number of integers in box \(i\). Then the same line contains \(n_i\) integers \(a_{i,1}, \ldots, a_{i,n_i}\) (\(|a_{i...
If Ujan cannot achieve his goal, output ""No"" in a single line. Otherwise in the first line output ""Yes"", and then output \(k\) lines. The \(i\)-th of these lines should contain two integers \(c_i\) and \(p_i\). This means that Ujan should pick the integer \(c_i\) from the \(i\)-th box and place it in the \(p_i\)-th...
In the first sample, Ujan can put the number \(7\) in the \(2\)nd box, the number \(2\) in the \(3\)rd box, the number \(5\) in the \(1\)st box and keep the number \(10\) in the same \(4\)th box. Then the boxes will contain numbers \(\{1,5,4\}\), \(\{3, 7\}\), \(\{8,2\}\) and \(\{10\}\). The sum in each box then is equ...
Input: 4 3 1 7 4 2 3 2 2 8 5 1 10 | Output: Yes 7 2 2 3 5 1 10 4
Expert
4
824
424
444
12
1,594
A
1594A
A. Consecutive Sum Riddle
800
math
Theofanis has a riddle for you and if you manage to solve it, he will give you a Cypriot snack halloumi for free (Cypriot cheese).You are given an integer \(n\). You need to find two integers \(l\) and \(r\) such that \(-10^{18} \le l < r \le 10^{18}\) and \(l + (l + 1) + \ldots + (r - 1) + r = n\).
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The first and only line of each test case contains a single integer \(n\) (\(1 \le n \le 10^{18}\)).
For each test case, print the two integers \(l\) and \(r\) such that \(-10^{18} \le l < r \le 10^{18}\) and \(l + (l + 1) + \ldots + (r - 1) + r = n\). It can be proven that an answer always exists. If there are multiple answers, print any.
In the first test case, \(0 + 1 = 1\).In the second test case, \((-1) + 0 + 1 + 2 = 2\).In the fourth test case, \(1 + 2 + 3 = 6\).In the fifth test case, \(18 + 19 + 20 + 21 + 22 = 100\).In the sixth test case, \((-2) + (-1) + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 = 25\).
Input: 7 1 2 3 6 100 25 3000000000000 | Output: 0 1 -1 2 1 2 1 3 18 22 -2 7 999999999999 1000000000001
Beginner
1
300
197
240
15
864
B
864B
B. Polycarp and Letters
1,000
brute force; implementation; strings
Polycarp loves lowercase letters and dislikes uppercase ones. Once he got a string s consisting only of lowercase and uppercase Latin letters.Let A be a set of positions in the string. Let's call it pretty if following conditions are met: letters on positions from A in the string are all distinct and lowercase; there a...
The first line contains a single integer n (1 ≀ n ≀ 200) β€” length of string s.The second line contains a string s consisting of lowercase and uppercase Latin letters.
Print maximum number of elements in pretty set of positions for string s.
In the first example the desired positions might be 6 and 8 or 7 and 8. Positions 6 and 7 contain letters 'a', position 8 contains letter 'b'. The pair of positions 1 and 8 is not suitable because there is an uppercase letter 'B' between these position.In the second example desired positions can be 7, 8 and 11. There a...
Input: 11aaaaBaabAbA | Output: 2
Beginner
3
601
166
73
8
44
C
44C
C. Holidays
1,300
implementation
School holidays come in Berland. The holidays are going to continue for n days. The students of school β„–N are having the time of their lives and the IT teacher Marina Sergeyevna, who has spent all the summer busy checking the BSE (Berland State Examination) results, has finally taken a vacation break! Some people are i...
The first input line contains two numbers n and m (1 ≀ n, m ≀ 100) β€” the number of days in Berland holidays and the number of people in charge of the watering respectively. The next m lines contain the description of the duty schedule. Each line contains two integers ai and bi (1 ≀ ai ≀ bi ≀ n), meaning that the i-th p...
Print ""OK"" (without quotes), if the schedule does not contain mistakes. Otherwise you have to find the minimal number of a day when the flowers will not be watered or will be watered multiple times, and output two integers β€” the day number and the number of times the flowers will be watered that day.
Keep in mind that in the second sample the mistake occurs not only on the second day, but also on the sixth day, when nobody waters the flowers. However, you have to print the second day, i.e. the day with the minimal number.
Input: 10 51 23 34 67 78 10 | Output: OK
Easy
1
766
517
303
0
35
C
35C
C. Fire Again
1,500
brute force; dfs and similar; shortest paths
After a terrifying forest fire in Berland a forest rebirth program was carried out. Due to it N rows with M trees each were planted and the rows were so neat that one could map it on a system of coordinates so that the j-th tree in the i-th row would have the coordinates of (i, j). However a terrible thing happened and...
The first input line contains two integers N, M (1 ≀ N, M ≀ 2000) β€” the size of the forest. The trees were planted in all points of the (x, y) (1 ≀ x ≀ N, 1 ≀ y ≀ M) type, x and y are integers.The second line contains an integer K (1 ≀ K ≀ 10) β€” amount of trees, burning in the beginning. The third line contains K pairs...
Output a line with two space-separated integers x and y β€” coordinates of the tree that will be the last one to start burning. If there are several such trees, output any.
Input: 3 312 2 | Output: 1 1
Medium
3
791
489
170
0
702
C
702C
C. Cellular Network
1,500
binary search; implementation; two pointers
You are given n points on the straight line β€” the positions (x-coordinates) of the cities and m points on the same line β€” the positions (x-coordinates) of the cellular towers. All towers work in the same way β€” they provide cellular network for all cities, which are located at the distance which is no more than r from t...
The first line contains two positive integers n and m (1 ≀ n, m ≀ 105) β€” the number of cities and the number of cellular towers.The second line contains a sequence of n integers a1, a2, ..., an ( - 109 ≀ ai ≀ 109) β€” the coordinates of cities. It is allowed that there are any number of cities in the same point. All coor...
Print minimal r so that each city will be covered by cellular network.
Input: 3 2-2 2 4-3 0 | Output: 4
Medium
3
749
610
70
7
2,074
C
2074C
C. XOR and Triangle
1,100
bitmasks; brute force; geometry; greedy; probabilities
This time, the pink soldiers have given you an integer \(x\) (\(x \ge 2\)).Please determine if there exists a positive integer \(y\) that satisfies the following conditions. \(y\) is strictly less than \(x\). There exists a non-degenerate triangle\(^{\text{βˆ—}}\) with side lengths \(x\), \(y\), \(x \oplus y\). Here, \(\...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 2000\)). The description of the test cases follows. The only line of each test case contains a single integer \(x\) (\(2 \le x \le 10^9\)).
For each test case, print one integer on a separate line. The integer you must output is as follows: If there exists an integer \(y\) satisfying the conditions, output the value of \(y\) (\(1 \le y < x\)); Otherwise, output \(-1\). If there exist multiple integers that satisfy the conditions, you may output any.
In the first test case, there exists a non-degenerate triangle with side lengths \(3\), \(5\), and \(3 \oplus 5 = 6\). Therefore, \(y=3\) is a valid answer.In the second test case, \(1\) is the only possible candidate for \(y\), but it cannot make a non-degenerate triangle. Therefore, the answer is \(-1\).
Input: 75263694420 | Output: 3 -1 5 -1 66 -1 320
Easy
5
552
249
313
20
1,044
D
1044D
D. Deduction Queries
2,400
data structures; dsu
There is an array \(a\) of \(2^{30}\) integers, indexed from \(0\) to \(2^{30}-1\). Initially, you know that \(0 \leq a_i < 2^{30}\) (\(0 \leq i < 2^{30}\)), but you do not know any of the values. Your task is to process queries of two types: 1 l r x: You are informed that the bitwise xor of the subarray \([l, r]\) (en...
The first line contains a single integer \(q\) (\(1 \leq q \leq 2 \cdot 10^5\)) β€” the number of queries.Each of the next \(q\) lines describes a query. It contains one integer \(t\) (\(1 \leq t \leq 2\)) β€” the type of query.The given queries will be encoded in the following way: let \(last\) be the answer to the last q...
After every query of the second type, output the bitwise xor of the given subarray or \(-1\) if it is still impossible to know.
In the first example, the real queries (without being encoded) are: 12 2 1 2 2 0 1073741823 1 1 2 5 2 1 1 2 2 2 2 1 2 1 2 3 6 2 1 1 1 1 3 0 2 1 1 2 2 2 2 3 3 The answers for the first two queries are \(-1\) because we don't have any such information on the array initially. The first update tells us \(a_1 \oplus a_2 = 5...
Input: 122 1 22 1 10737418221 0 3 42 0 02 3 32 0 31 6 7 32 4 41 0 2 12 0 02 4 42 0 0 | Output: -1-1-1-15-1635
Expert
2
884
1,344
127
10
1,398
B
1398B
B. Substring Removal Game
800
games; greedy; sortings
Alice and Bob play a game. They have a binary string \(s\) (a string such that each character in it is either \(0\) or \(1\)). Alice moves first, then Bob, then Alice again, and so on.During their move, the player can choose any number (not less than one) of consecutive equal characters in \(s\) and delete them.For exa...
The first line contains one integer \(T\) (\(1 \le T \le 500\)) β€” the number of test cases.Each test case contains exactly one line containing a binary string \(s\) (\(1 \le |s| \le 100\)).
For each test case, print one integer β€” the resulting score of Alice (the number of \(1\)-characters deleted by her).
Questions about the optimal strategy will be ignored.
Input: 5 01111001 0000 111111 101010101 011011110111 | Output: 4 0 6 3 6
Beginner
3
1,001
189
117
13
934
A
934A
A. A Compatible Pair
1,400
brute force; games
Nian is a monster which lives deep in the oceans. Once a year, it shows up on the land, devouring livestock and even people. In order to keep the monster away, people fill their villages with red colour, light, and cracking noise, all of which frighten the monster out of coming.Little Tommy has n lanterns and Big Banba...
The first line contains two space-separated integers n and m (2 ≀ n, m ≀ 50).The second line contains n space-separated integers a1, a2, ..., an.The third line contains m space-separated integers b1, b2, ..., bm.All the integers range from - 109 to 109.
Print a single integer β€” the brightness of the chosen pair.
In the first example, Tommy will hide 20 and Banban will choose 18 from Tommy and 14 from himself.In the second example, Tommy will hide 3 and Banban will choose 2 from Tommy and 1 from himself.
Input: 2 220 182 14 | Output: 252
Easy
2
856
253
59
9
567
C
567C
C. Geometric Progression
1,700
binary search; data structures; dp
Polycarp loves geometric progressions very much. Since he was only three years old, he loves only the progressions of length three. He also has a favorite integer k and a sequence a, consisting of n integers.He wants to know how many subsequences of length three can be selected from a, so that they form a geometric pro...
The first line of the input contains two integers, n and k (1 ≀ n, k ≀ 2Β·105), showing how many numbers Polycarp's sequence has and his favorite number.The second line contains n integers a1, a2, ..., an ( - 109 ≀ ai ≀ 109) β€” elements of the sequence.
Output a single number β€” the number of ways to choose a subsequence of length three, such that it forms a geometric progression with a common ratio k.
In the first sample test the answer is four, as any of the two 1s can be chosen as the first element, the second element can be any of the 2s, and the third element of the subsequence must be equal to 4.
Input: 5 21 1 2 2 4 | Output: 4
Medium
3
830
251
150
5
1,505
H
1505H
H. L BREAK into program
2,500
*special
Hack the program and get the password hidden in it.
This program has only one test, and it's empty (it doesn't give your program anything to read).
Output the password recovered from the program. The password is case sensitive.
Expert
1
51
95
79
15
962
B
962B
B. Students in Railway Carriage
1,300
constructive algorithms; greedy; implementation
There are \(n\) consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger.The university team for the Olympiad consists of \(a\) student-programmers and \(b\) student-athletes. Determine the largest number of students from all \(a+b\) students, which you can put in the railway...
The first line contain three integers \(n\), \(a\) and \(b\) (\(1 \le n \le 2\cdot10^{5}\), \(0 \le a, b \le 2\cdot10^{5}\), \(a + b > 0\)) β€” total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes.The second line contains a string with length \(n\), con...
Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete.
In the first example you can put all student, for example, in the following way: *.AB*In the second example you can put four students, for example, in the following way: *BAB*BIn the third example you can put seven students, for example, in the following way: B*ABAB**A*BThe letter A means a student-programmer, and the ...
Input: 5 1 1*...* | Output: 2
Easy
3
721
491
210
9
1,868
A
1868A
A. Fill in the Matrix
1,300
constructive algorithms; implementation
There is an empty matrix \(M\) of size \(n\times m\).Zhongkao examination is over, and Daniel would like to do some puzzle games. He is going to fill in the matrix \(M\) using permutations of length \(m\). That is, each row of \(M\) must be a permutation of length \(m^\dagger\).Define the value of the \(i\)-th column i...
The first line of input contains a single integer \(t\) (\(1\le t\le 1000\)) β€” the number of test cases. The description of test cases follows.The only line of each test case contains two integers \(n\) and \(m\) (\(1\le n,m\le 2\cdot 10^5\)) β€” the size of the matrix.It is guaranteed that the sum of \(n\cdot m\) over a...
For each test case, in the first line output a single integer β€” the maximum beauty of \(M\).Then output the matrix \(M\) of size \(n\times m\) β€” the matrix you find.If there are multiple solutions, you may output any of them.
In the first test case: \(v_1=\operatorname{MEX}(1,0,1,0)=2\); \(v_2=\operatorname{MEX}(0,2,0,2)=1\); \(v_3=\operatorname{MEX}(2,1,2,1)=0\). Therefore, \(s=\operatorname{MEX}(2,1,0)=3\). It can be shown that \(3\) is the maximum possible beauty of \(M\).In the second test case, any permutation will make \(s=2\).In the ...
Input: 44 31 166 62 1 | Output: 3 1 0 2 0 2 1 1 0 2 0 2 1 2 14 7 15 4 10 0 8 6 1 2 3 5 9 11 12 13 6 3 0 1 4 2 5 5 2 1 0 4 3 1 3 2 4 5 0 4 1 3 2 5 0 4 2 5 3 0 1 2 4 0 5 1 3 0 0 0
Easy
2
1,242
366
225
18
2,128
B
2128B
B. Deque Process
1,100
constructive algorithms; greedy; sortings; two pointers
We say that an array \(a\) of size \(n\) is bad if and only if there exists \(1 \leq i \leq n - 4\) such that one of the following conditions holds: \(a_i < a_{i+1} < a_{i+2} < a_{i+3} < a_{i+4}\) \(a_i > a_{i+1} > a_{i+2} > a_{i+3} > a_{i+4}\) An array is good if and only if it's not bad. For example: \(a = [3, \color...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10\,000\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(5 \leq n \leq 100\,000\)) β€” the length of the array.The second line of each test case c...
For each test case, you must output a string \(s\) of length \(n\). For every \(1 \leq i \leq n\), at the \(i\)-th turn: \(s_i = \texttt{L}\) means that you removed the leftmost element of \(p\) \(s_i = \texttt{R}\) means that you removed the rightmost element of \(p\) We can show that an answer always exists. If there...
In the first test case, the sequence \(\color{blue}{\texttt{RRR}}\color{red}{\texttt{LLLL}}\) results in \(q = [\color{blue}{7}, \color{blue}{6}, \color{blue}{5}, \color{red}{1}, \color{red}{2}, \color{red}{3}, \color{red}{4}]\).In the second test case, the sequence \(\color{red}{\texttt{LL}}\color{blue}{\texttt{RR}}\c...
Input: 671 2 3 4 5 6 791 3 6 8 9 7 5 4 2121 2 11 3 6 4 7 8 12 5 10 964 1 2 5 6 351 2 3 5 495 1 8 6 2 7 9 4 3 | Output: RRRLLLL LLRRLLRRL LLLLLLLLLLLL LLLLLL LLLLL LLLLLLLLL
Easy
4
1,264
541
363
21
30
D
30D
D. King's Problem?
2,600
geometry; greedy
Every true king during his life must conquer the world, hold the Codeforces world finals, win pink panda in the shooting gallery and travel all over his kingdom.King Copa has already done the first three things. Now he just needs to travel all over the kingdom. The kingdom is an infinite plane with Cartesian coordinate...
The first line contains two integers n and k (1 ≀ n ≀ 105, 1 ≀ k ≀ n + 1) β€” amount of cities and index of the starting city. The second line contains n + 1 numbers xi. The third line contains yn + 1. All coordinates are integers and do not exceed 106 by absolute value. No two cities coincide.
Output the minimum possible length of the journey. Your answer must have relative or absolute error less than 10 - 6.
Input: 3 10 1 2 11 | Output: 3.41421356237309490000
Expert
2
922
293
117
0
1,388
C
1388C
C. Uncle Bogdan and Country Happiness
1,800
dfs and similar; greedy; math; trees
Uncle Bogdan is in captain Flint's crew for a long time and sometimes gets nostalgic for his homeland. Today he told you how his country introduced a happiness index.There are \(n\) cities and \(nβˆ’1\) undirected roads connecting pairs of cities. Citizens of any city can reach any other city traveling by these roads. Ci...
The first line contains a single integer \(t\) (\(1 \le t \le 10000\)) β€” the number of test cases.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n \le 10^5\); \(0 \le m \le 10^9\)) β€” the number of cities and citizens.The second line of each test case contains \(n\) integers \(p_1, p_2, ...
For each test case, print YES, if the collected data is correct, or NO β€” otherwise. You can print characters in YES or NO in any case.
Let's look at the first test case of the first sample: At first, all citizens are in the capital. Let's describe one of possible scenarios: a person from city \(1\): he lives in the capital and is in good mood; a person from city \(4\): he visited cities \(1\) and \(4\), his mood was ruined between cities \(1\) and \(4...
Input: 2 7 4 1 0 1 1 0 1 0 4 0 0 -1 0 -1 0 1 2 1 3 1 4 3 5 3 6 3 7 5 11 1 2 5 2 1 -11 -2 -6 -2 -1 1 2 1 3 1 4 3 5 | Output: YES YES
Medium
4
1,753
952
134
13
1,245
F
1245F
F. Daniel and Spring Cleaning
2,300
bitmasks; brute force; combinatorics; dp
While doing some spring cleaning, Daniel found an old calculator that he loves so much. However, it seems like it is broken. When he tries to compute \(1 + 3\) using the calculator, he gets \(2\) instead of \(4\). But when he tries computing \(1 + 4\), he gets the correct answer, \(5\). Puzzled by this mystery, he open...
The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β€” the number of testcases.Then, \(t\) lines follow, each containing two space-separated integers \(l\) and \(r\) (\(0 \le l \le r \le 10^9\)).
Print \(t\) integers, the \(i\)-th integer should be the answer to the \(i\)-th testcase.
\(a \oplus b\) denotes the bitwise XOR of \(a\) and \(b\).For the first testcase, the pairs are: \((1, 2)\), \((1, 4)\), \((2, 1)\), \((2, 4)\), \((3, 4)\), \((4, 1)\), \((4, 2)\), and \((4, 3)\).
Input: 3 1 4 323 323 1 1000000 | Output: 8 0 3439863766
Expert
4
1,047
211
89
12
1,710
B
1710B
B. Rain
2,100
binary search; brute force; data structures; geometry; greedy; implementation; math
You are the owner of a harvesting field which can be modeled as an infinite line, whose positions are identified by integers.It will rain for the next \(n\) days. On the \(i\)-th day, the rain will be centered at position \(x_i\) and it will have intensity \(p_i\). Due to these rains, some rainfall will accumulate; let...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 10^4\)). The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \leq n \leq 2 \cdot 10^5\), \(1 \leq m \leq 10^9\)) β€” the number of rainy days an...
For each test case, output a binary string \(s\) length of \(n\). The \(i\)-th character of \(s\) is 1 if after erasing the \(i\)-th day's rain there is no flood, while it is 0, if after erasing the \(i\)-th day's rain the flood still happens.
In the first test case, if we do not use the spell, the accumulated rainfall distribution will be like this: If we erase the third day's rain, the flood is avoided and the accumulated rainfall distribution looks like this: In the second test case, since initially the flood will not happen, we can erase any day's rain.I...
Input: 43 61 55 53 42 31 35 22 51 610 66 124 51 612 55 59 78 3 | Output: 001 11 00 100110
Hard
7
806
631
243
17
2,106
F
2106F
F. Goblin
1,900
dfs and similar; dp; dsu; greedy; math
Dr. TC has a new patient called Goblin. He wants to test Goblin's intelligence, but he has gotten bored of his standard test. So, he decided to make it a bit harder.First, he creates a binary string\(^{\text{βˆ—}}\) \(s\) having \(n\) characters. Then, he creates \(n\) binary strings \(a_1, a_2, \ldots, a_n\). It is know...
The first line of the input consists of a single integer \(t\) \((1 \le t \le 10^3)\) β€” the number of test cases.The first line of each test contains a single integer \(n\) \((1 \le n \le 2 \cdot 10^5)\) β€” the length of the binary string \(s\).The second line of each test contains a single binary string \(s\) of length...
For each test case, output a single number, the maximum possible size of a good set of cells from the grid.
In the first example, the following grid has been written on the board:$$$\( 1 0 0 \)\( \)\( 0 1 0 \)\( \)\( 0 0 1 \)\(The set that consists of cells \)(1, 2)\( and \)(1, 3)\( is good. The set that consists of cells \)(1, 1)\( and \)(1, 2)\( is not good, since the value of cell \)(1, 1)\( is not \)0\(. The set of cells...
Input: 6300040010710110014000121110 | Output: 3 9 10 7 1 0
Hard
5
1,426
419
107
21
1,575
J
1575J
J. Jeopardy of Dropped Balls
1,500
binary search; brute force; dsu; implementation
Mr. Chanek has a new game called Dropping Balls. Initially, Mr. Chanek has a grid \(a\) of size \(n \times m\)Each cell \((x,y)\) contains an integer \(a_{x,y}\) denoting the direction of how the ball will move. \(a_{x,y}=1\) β€” the ball will move to the right (the next cell is \((x, y + 1)\)); \(a_{x,y}=2\) β€” the ball ...
The first line contains three integers \(n\), \(m\), and \(k\) (\(1 \leq n, m \leq 1000\), \(1 \leq k \leq 10^5\)) β€” the size of the grid and the number of balls dropped by Mr. Chanek.The \(i\)-th of the next \(n\) lines contains \(m\) integers \(a_{i,1},a_{i,2},\ldots,a_{i,m}\) (\(1 \leq a_{i,j} \leq 3\)). It will sat...
Output \(k\) integers β€” the \(i\)-th integer denoting the column where the \(i\)-th ball will end.
In the first example, the first ball will drop as follows. Note that the cell \((1, 1)\) will change direction to the bottom direction. The second and third balls will drop as follows. All balls will be dropped from the first row and on the \(c_1, c_2, \dots, c_k\)-th columns respectively. A ball will stop dropping onc...
Input: 5 5 3 1 2 3 3 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 1 | Output: 2 2 1
Medium
4
799
520
98
15
676
C
676C
C. Vasya and String
1,500
binary search; dp; strings; two pointers
High school student Vasya got a string of length n as a birthday present. This string consists of letters 'a' and 'b' only. Vasya denotes beauty of the string as the maximum length of a substring (consecutive subsequence) consisting of equal letters.Vasya can change no more than k characters of the original string. Wha...
The first line of the input contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ n) β€” the length of the string and the maximum number of characters to change.The second line contains the string, consisting of letters 'a' and 'b' only.
Print the only integer β€” the maximum beauty of the string Vasya can achieve by changing no more than k characters.
In the first sample, Vasya can obtain both strings ""aaaa"" and ""bbbb"".In the second sample, the optimal answer is obtained with the string ""aaaaabaa"" or with the string ""aabaaaaa"".
Input: 4 2abba | Output: 4
Medium
4
373
237
114
6
1,983
E
1983E
E. I Love Balls
2,300
combinatorics; math; probabilities
Alice and Bob are playing a game. There are \(n\) balls, out of which \(k\) are special. Each ball has a value associated with it. The players play turn by turn. In each turn, the player randomly picks a ball and adds the value of the ball to their score, which is \(0\) at the beginning of the game. The selected ball i...
There are multiple test cases. The first line of the input contains an integer \(t\), the number of test cases (\(1 \le t \le 2 \cdot 10^5\)).Each test case description is on a new line. The first line of the test case contains two integers \(n\) and \(k\) in the respective order separated by a space (\(1 \le k \le n \...
Output two integers per test case in a new line, the expected score of Alice and the expected score of Bob modulo \(10^9+7\).
In the first test case, Alice's expected score is \(45\), and Bob's is \(30\) at the end of the game.
Input: 15 210 20 5 15 25 | Output: 45 30
Expert
3
1,033
598
125
19
1,695
B
1695B
B. Circle Game
1,000
games; greedy
Mike and Joe are playing a game with some stones. Specifically, they have \(n\) piles of stones of sizes \(a_1, a_2, \ldots, a_n\). These piles are arranged in a circle.The game goes as follows. Players take turns removing some positive number of stones from a pile in clockwise order starting from pile \(1\). Formally,...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)). Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 50\)) β€” the number of piles.The second line contains \(n\) integers \(a_1, a_2, \ld...
For each test case print the winner of the game, either ""Mike"" or ""Joe"" on its own line (without quotes).
In the first test case, Mike just takes all \(37\) stones on his first turn.In the second test case, Joe can just copy Mike's moves every time. Since Mike went first, he will hit \(0\) on the first pile one move before Joe does so on the second pile.
Input: 21372100 100 | Output: Mike Joe
Beginner
2
607
380
109
16
2,043
B
2043B
B. Digits
1,100
math; number theory
Artem wrote the digit \(d\) on the board exactly \(n!\) times in a row. So, he got the number \(dddddd \dots ddd\) (exactly \(n!\) digits).Now he is curious about which odd digits from \(1\) to \(9\) divide the number written on the board.
The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β€” the number of test cases. The next \(t\) test cases follow.Each test case consists of a single line containing two integers \(n\) and \(d\) (\(2 \le n \le 10^9\), \(1 \le d \le 9\)).
For each test case, output the odd digits in ascending order that divide the number written on the board.
The factorial of a positive integer \(n\) (\(n!\)) is the product of all integers from \(1\) to \(n\). For example, the factorial of \(5\) is \(1 \cdot 2 \cdot 3 \cdot 4 \cdot 5 = 120\).
Input: 32 67 18 5 | Output: 1 3 1 3 7 9 1 3 5 7 9
Easy
2
239
253
105
20
1,031
F
1031F
F. Familiar Operations
2,800
brute force; graphs; math
You are given two positive integers \(a\) and \(b\). There are two possible operations: multiply one of the numbers by some prime \(p\); divide one of the numbers on its prime factor \(p\). What is the minimum number of operations required to obtain two integers having the same number of divisors? You are given several...
The first line contains a single integer \(t\) (\(1 \le t \le 10^5\)) β€” the number of pairs of integers for which you are to find the answer.Each of the next \(t\) lines contain two integers \(a_i\) and \(b_i\) (\(1 \le a_i, b_i \le 10^6\)).
Output \(t\) lines β€” the \(i\)-th of them should contain the answer for the pair \(a_i\), \(b_i\).
These are the numbers with equal number of divisors, which are optimal to obtain in the sample test case: \((27, 10)\), 4 divisors \((100, 1156)\), 9 divisors \((220, 140)\), 12 divisors \((17, 19)\), 2 divisors \((12, 18)\), 6 divisors \((50, 32)\), 6 divisors \((224, 1925)\), 12 divisors Note that there can be severa...
Input: 89 10100 17220 7017 194 1832 20100 32224 385 | Output: 13101011
Master
3
378
241
98
10
1,760
F
1760F
F. Quests
1,500
binary search; greedy; sortings
There are \(n\) quests. If you complete the \(i\)-th quest, you will gain \(a_i\) coins. You can only complete at most one quest per day. However, once you complete a quest, you cannot do the same quest again for \(k\) days. (For example, if \(k=2\) and you do quest \(1\) on day \(1\), then you cannot do it on day \(2\...
The input consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains three integers \(n,c,d\) (\(2 \leq n \leq 2\cdot10^5\); \(1 \leq c \leq 10^{16}\); \(1 \leq d \leq ...
For each test case, output one of the following. If no such \(k\) exists, output Impossible. If \(k\) can be arbitrarily large, output Infinity. Otherwise, output a single integer β€” the maximum value of \(k\) such that you can gain at least \(c\) coins over \(d\) days. Please note, the checker is case-sensitive, and yo...
In the first test case, one way to earn \(5\) coins over \(4\) days with \(k=2\) is as follows: Day 1: do quest 2, and earn \(2\) coins. Day 2: do quest 1, and earn \(1\) coin. Day 3: do nothing. Day 4: do quest 2, and earn \(2\) coins. In total, we earned \(2+1+2=5\) coins.In the second test case, we can make over \(2...
Input: 62 5 41 22 20 10100 103 100 37 2 64 20 34 5 6 74 100000000000 20228217734 927368 26389746 6278969742 20 45 1 | Output: 2 Infinity Impossible 1 12 0
Medium
3
602
691
370
17
1,789
D
1789D
D. Serval and Shift-Shift-Shift
2,200
bitmasks; brute force; constructive algorithms; implementation
Serval has two \(n\)-bit binary integer numbers \(a\) and \(b\). He wants to share those numbers with Toxel.Since Toxel likes the number \(b\) more, Serval decides to change \(a\) into \(b\) by some (possibly zero) operations. In an operation, Serval can choose any positive integer \(k\) between \(1\) and \(n\), and ch...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1\le t\le2\cdot10^{3}\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1\le n\le2\cdot10^{3}\)) β€” the number of bits in numbers \(a\) and \(b\).The second an...
For each test case, if it is impossible to change \(a\) into \(b\) in at most \(n\) operations, print a single integer \(-1\).Otherwise, in the first line, print the number of operations \(m\) (\(0\le m\le n\)).If \(m>0\), in the second line, print \(m\) integers \(k_{1},k_{2},\dots,k_{m}\) representing the operations....
In the first test case:The first operation changes \(a\) into \(\require{cancel}00111\oplus\cancel{001}11\underline{000}=11111\).The second operation changes \(a\) into \(\require{cancel}11111\oplus\underline{00}111\cancel{11}=11000\).The bits with strikethroughs are overflowed bits that are removed. The bits with unde...
Input: 3500111110001113001000 | Output: 2 3 -2 0 -1
Hard
4
1,133
576
538
17
258
E
258E
E. Little Elephant and Tree
2,400
data structures; dfs and similar; trees
The Little Elephant loves trees very much, he especially loves root trees.He's got a tree consisting of n nodes (the nodes are numbered from 1 to n), with root at node number 1. Each node of the tree contains some list of numbers which initially is empty. The Little Elephant wants to apply m operations. On the i-th ope...
The first line contains two integers n and m (1 ≀ n, m ≀ 105) β€” the number of the tree nodes and the number of operations. Each of the following n - 1 lines contains two space-separated integers, ui and vi (1 ≀ ui, vi ≀ n, ui β‰  vi), that mean that there is an edge between nodes number ui and vi. Each of the following m...
In a single line print n space-separated integers β€” c1, c2, ..., cn.
Input: 5 11 21 33 53 42 3 | Output: 0 3 3 3 3
Expert
3
789
525
68
2
598
B
598B
B. Queries on a String
1,300
implementation; strings
You are given a string s and should process m queries. Each query is described by two 1-based indices li, ri and integer ki. It means that you should cyclically shift the substring s[li... ri] ki times. The queries should be processed one after another in the order they are given.One operation of a cyclic shift (rotati...
The first line of the input contains the string s (1 ≀ |s| ≀ 10 000) in its initial state, where |s| stands for the length of s. It contains only lowercase English letters.Second line contains a single integer m (1 ≀ m ≀ 300) β€” the number of queries.The i-th of the next m lines contains three integers li, ri and ki (1 ...
Print the resulting string s after processing all m queries.
The sample is described in problem statement.
Input: abacaba23 6 11 4 2 | Output: baabcaa
Easy
2
675
393
60
5
173
C
173C
C. Spiral Maximum
1,900
brute force; dp
Let's consider a k Γ— k square, divided into unit squares. Please note that k β‰₯ 3 and is odd. We'll paint squares starting from the upper left square in the following order: first we move to the right, then down, then to the left, then up, then to the right again and so on. We finish moving in some direction in one of t...
The first line contains two integers n and m (3 ≀ n, m ≀ 500) β€” the sizes of the table.Each of the next n lines contains m space-separated integers: the j-th number in the i-th line aij ( - 1000 ≀ aij ≀ 1000) is the number recorded in the j-th cell of the i-th row of the table.
Print a single number β€” the maximum sum of numbers among all spirals.
In the first sample the spiral with maximum sum will cover all 1's of the table.In the second sample the spiral may cover only six 1's.
Input: 6 50 0 0 0 01 1 1 1 10 0 0 0 11 1 1 0 11 0 0 0 11 1 1 1 1 | Output: 17
Hard
2
1,000
278
69
1
1,025
D
1025D
D. Recovering BST
2,100
brute force; dp; math; number theory; trees
Dima the hamster enjoys nibbling different things: cages, sticks, bad problemsetters and even trees!Recently he found a binary search tree and instinctively nibbled all of its edges, hence messing up the vertices. Dima knows that if Andrew, who has been thoroughly assembling the tree for a long time, comes home and see...
The first line contains the number of vertices \(n\) (\(2 \le n \le 700\)).The second line features \(n\) distinct integers \(a_i\) (\(2 \le a_i \le 10^9\)) β€” the values of vertices in ascending order.
If it is possible to reassemble the binary search tree, such that the greatest common divisor of any two vertices connected by the edge is greater than \(1\), print ""Yes"" (quotes for clarity).Otherwise, print ""No"" (quotes for clarity).
The picture below illustrates one of the possible trees for the first example. The picture below illustrates one of the possible trees for the third example.
Input: 63 6 9 18 36 108 | Output: Yes
Hard
5
717
201
239
10
455
D
455D
D. Serega and Fun
2,700
data structures
Serega loves fun. However, everyone has fun in the unique manner. Serega has fun by solving query problems. One day Fedor came up with such a problem.You are given an array a consisting of n positive integers and queries to it. The queries can be of two types: Make a unit cyclic shift to the right on the segment from l...
The first line contains integer n (1 ≀ n ≀ 105) β€” the number of elements of the array. The second line contains n integers a[1], a[2], ..., a[n] (1 ≀ a[i] ≀ n).The third line contains a single integer q (1 ≀ q ≀ 105) β€” the number of queries. The next q lines contain the queries.As you need to respond to the queries onl...
For each query of the 2-nd type print the answer on a single line.
Input: 76 6 2 7 4 2 571 3 62 2 4 22 2 4 72 2 2 51 2 61 1 42 1 7 3 | Output: 2100
Master
1
693
949
66
4
958
A3
958A3
A3. Death Stars (hard)
3,100
The stardate is 2015, and Death Stars are bigger than ever! This time, two rebel spies have yet again given Heidi two maps with the possible locations of the Death Stars.Heidi has now received two maps with possible locations of N Death Stars. She knows that each of the maps is possibly corrupted, and may contain some ...
The first line of the input contains an integer N (1000 ≀ N ≀ 50000) – the number of Death Stars. The second line of the input contains an integer N1 (N ≀ N1 ≀ 1.5Β·N) – the number of stars in the first map. The next N1 lines specify the coordinates of the stars in the first map. The i-th line contains two space-separat...
You should output exactly N lines, each containing a space-separated pair of integers i1 and i2. Each such line should indicate that the star numbered i1 in the first map corresponds to the star numbered i2 in the second map. Your answer will be considered correct if over 90% of the distinct pairs listed in your output...
The tests are generated in the following way: The number of Death Stars N is pre-selected in some way. The numbers of stars on the first and on the second map, N1 and N2, are selected uniformly at random between 1.0 Γ— N and 1.5 Γ— N. N Death Stars are generated at random, with coordinates between - 10000 and 10000. Addi...
Master
0
686
684
340
9
1,954
B
1954B
B. Make It Ugly
1,200
implementation; math
Let's call an array \(a\) beautiful if you can make all its elements the same by using the following operation an arbitrary number of times (possibly, zero): choose an index \(i\) (\(2 \le i \le |a| - 1\)) such that \(a_{i - 1} = a_{i + 1}\), and replace \(a_i\) with \(a_{i - 1}\). You are given a beautiful array \(a_1...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 3 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le n\)).Additional constraints on the input: in ...
For each test case, output a single integer β€” the minimum number of elements you have to remove from the array \(a\) in order for it to stop being beautiful. If it is impossible, then output -1.
In the first testcase, it is impossible to modify the array in such a way that it stops being beautiful. An array consisting of identical numbers will remain beautiful no matter how many numbers we remove from it.In the second testcase, you can remove the number at the index \(5\), for example.The resulting array will ...
Input: 432 2 251 2 1 2 11173 3 3 5 3 3 3 | Output: -1 1 -1 3
Easy
2
526
443
194
19
1,468
B
1468B
B. Bakery
2,900
data structures; dsu
Monocarp would like to open a bakery in his local area. But, at first, he should figure out whether he can compete with other shops.Monocarp plans that the bakery will work for \(n\) days. On the \(i\)-th day, \(a_i\) loaves of bread will be baked in the morning before the opening. At the end of the \(n\)-th day, Monoc...
The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 2 \cdot 10^5\); \(1 \le m \le 2 \cdot 10^5\)) β€” the number of days the bakery is open and the number of possible values of consumer demand.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) β€” the number of bread l...
Print \(m\) integers: for each consumer demand, print the unattractiveness of the bakery.
In the first example, let's describe what happens for couple consumer demands:If consumer demand is equal to \(1\): at day \(1\): \(5\) loaves are baked and only \(1\) is sold with spoilage equal to \(1 - 1 = 0\); at day \(2\): \(4\) loaves are left and \(2\) more are baked. Only \(1\) loaf was sold and it was the loaf...
Input: 5 4 5 2 1 3 7 1 3 4 10 | Output: 4 2 1 0
Master
2
1,711
524
89
14
248
C
248C
C. Robo-Footballer
2,000
binary search; geometry
It's a beautiful April day and Wallace is playing football with his friends. But his friends do not know that Wallace actually stayed home with Gromit and sent them his robotic self instead. Robo-Wallace has several advantages over the other guys. For example, he can hit the ball directly to the specified point. And ye...
The first and the single line contains integers y1, y2, yw, xb, yb, r (1 ≀ y1, y2, yw, xb, yb ≀ 106; y1 < y2 < yw; yb + r < yw; 2Β·r < y2 - y1).It is guaranteed that the ball is positioned correctly in the field, doesn't cross any wall, doesn't touch the wall that Robo-Wallace is aiming at. The goal posts can't be locat...
If Robo-Wallace can't score a goal in the described manner, print ""-1"" (without the quotes). Otherwise, print a single number xw β€” the abscissa of his point of aiming. If there are multiple points of aiming, print the abscissa of any of them. When checking the correctness of the answer, all comparisons are made with ...
Note that in the first and third samples other correct values of abscissa xw are also possible.
Input: 4 10 13 10 3 1 | Output: 4.3750000000
Hard
2
2,917
344
451
2
1,535
E
1535E
E. Gold Transfer
2,200
binary search; data structures; dp; greedy; interactive; trees
You are given a rooted tree. Each vertex contains \(a_i\) tons of gold, which costs \(c_i\) per one ton. Initially, the tree consists only a root numbered \(0\) with \(a_0\) tons of gold and price \(c_0\) per ton.There are \(q\) queries. Each query has one of two types: Add vertex \(i\) (where \(i\) is an index of quer...
The first line contains three integers \(q\), \(a_0\) and \(c_0\) (\(1 \le q \le 3 \cdot 10^5\); \(1 \le a_0, c_0 < 10^6\)) β€” the number of queries, the amount of gold in the root and its price.Next \(q\) lines contain descriptions of queries; The \(i\)-th query has one of two types: ""\(1\) \(p_i\) \(a_i\) \(c_i\)"" (...
For each query of the second type, print the resulting amount of gold we bought and the minimum amount of money we should spend.
Explanation of the sample:At the first query, the tree consist of root, so we purchase \(2\) tons of gold and pay \(2 \cdot 2 = 4\). \(3\) tons remain in the root.At the second query, we add vertex \(2\) as a son of vertex \(0\). Vertex \(2\) now has \(3\) tons of gold with price \(4\) per one ton.At the third query, a...
Input: 5 5 2 2 0 2 1 0 3 4 2 2 4 1 0 1 3 2 4 2 | Output: 2 4 4 10 1 3
Hard
6
1,516
892
128
15
1,391
C
1391C
C. Cyclic Permutations
1,500
combinatorics; dp; graphs; math
A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array) and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the arra...
The first and only line contains a single integer \(n\) (\(3 \le n \le 10^6\)).
Output a single integer \(0 \leq x < 10^9+7\), the number of cyclic permutations of length \(n\) modulo \(10^9+7\).
There are \(16\) cyclic permutations for \(n = 4\). \([4,2,1,3]\) is one such permutation, having a cycle of length four: \(4 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 4\).Nodes \(v_1\), \(v_2\), \(\ldots\), \(v_k\) form a simple cycle if the following conditions hold: \(k \geq 3\). \(v_i \neq v_j\) for any...
Input: 4 | Output: 16
Medium
4
1,329
79
115
13
1,535
B
1535B
B. Array Reodering
900
brute force; greedy; math; number theory; sortings
You are given an array \(a\) consisting of \(n\) integers.Let's call a pair of indices \(i\), \(j\) good if \(1 \le i < j \le n\) and \(\gcd(a_i, 2a_j) > 1\) (where \(\gcd(x, y)\) is the greatest common divisor of \(x\) and \(y\)).Find the maximum number of good index pairs if you can reorder the array \(a\) in an arbi...
The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases.The first line of the test case contains a single integer \(n\) (\(2 \le n \le 2000\)) β€” the number of elements in the array.The second line of the test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le...
For each test case, output a single integer β€” the maximum number of good index pairs if you can reorder the array \(a\) in an arbitrary way.
In the first example, the array elements can be rearranged as follows: \([6, 3, 5, 3]\).In the third example, the array elements can be rearranged as follows: \([4, 4, 2, 1, 1]\).
Input: 3 4 3 6 5 3 2 1 7 5 1 4 2 4 1 | Output: 4 0 9
Beginner
5
330
413
140
15
2,040
A
2040A
A. Game of Division
800
games; math
You are given an array of integers \(a_1, a_2, \ldots, a_n\) of length \(n\) and an integer \(k\).Two players are playing a game. The first player chooses an index \(1 \le i \le n\). Then the second player chooses a different index \(1 \le j \le n, i \neq j\). The first player wins if \(|a_i - a_j|\) is not divisible b...
Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 100\)) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 100\); \(1 \le k \le 100\)) β€” the length of th...
For each test case, if it is impossible for the first player to win, print ""NO"" (without quotes).Otherwise, print ""YES"" (without quotes) and on the next line the appropriate index \(1 \le i \le n\). If there are multiple solutions, print any of them.You can output each letter in any case (lowercase or uppercase). F...
In the first test case, the first player can choose \(a_2 = 2\). Then: If the second player chooses \(a_1 = 1\), the resulting difference is \(|2 - 1| = 1\) which is not divisible by \(k = 2\). If the second player chooses \(a_3 = 3\), the resulting difference is \(|2 - 3| = 1\) which is not divisible by \(k = 2\). In ...
Input: 73 21 2 34 21 2 4 55 310 7 3 4 55 31 31 15 55 362 117 172 217 181 36 | Output: YES 2 NO YES 3 NO NO YES 2 YES 1
Beginner
2
597
491
422
20
1,003
B
1003B
B. Binary String Constructing
1,300
constructive algorithms
You are given three integers \(a\), \(b\) and \(x\). Your task is to construct a binary string \(s\) of length \(n = a + b\) such that there are exactly \(a\) zeroes, exactly \(b\) ones and exactly \(x\) indices \(i\) (where \(1 \le i < n\)) such that \(s_i \ne s_{i + 1}\). It is guaranteed that the answer always exist...
The first line of the input contains three integers \(a\), \(b\) and \(x\) (\(1 \le a, b \le 100, 1 \le x < a + b)\).
Print only one string \(s\), where \(s\) is any binary string satisfying conditions described above. It is guaranteed that the answer always exists.
All possible answers for the first example: 1100; 0011. All possible answers for the second example: 110100; 101100; 110010; 100110; 011001; 001101; 010011; 001011.
Input: 2 2 1 | Output: 1100
Easy
1
639
117
148
10
1,799
G
1799G
G. Count Voting
2,600
combinatorics; dp; math
There are \(n\) people that will participate in voting. Each person has exactly one vote.\(i\)-th person has a team \(t_i\) (\(1 \leq t_i \leq n\)) where \(t_i = t_j\) means \(i\), \(j\) are in the same team. By the rules each person should vote for the person from the different team. Note that it automatically means t...
The first line contains a single integer \(n\) (\(1 \leq n \leq 200\)) β€” the number of people.The second line contains \(n\) integers \(c_1, c_2, \ldots, c_n\) (\(0 \leq c_i \leq n\)) β€” desired number of votes. It is guaranteed, that \(\sum\limits_{i=1}^{n} c_i = n\).The third line contains \(n\) integers \(t_1, t_2, \...
Print a single integer β€” the number of possible votings by modulo \(998\,244\,353\).
In the first test there are two possible votings: \((2, 3, 1)\), \((3, 1, 2)\).In the third test there are five possible votings: \((3, 3, 2, 2, 1)\), \((2, 3, 2, 3, 1)\), \((3, 3, 1, 2, 2)\), \((3, 1, 2, 3, 2)\), \((2, 3, 1, 3, 2)\).
Input: 3 1 1 1 1 2 3 | Output: 2
Expert
3
583
372
84
17
461
A
461A
A. Appleman and Toastman
1,200
greedy; sortings
Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks: Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman. Each time Appleman gets a g...
The first line contains a single integer n (1 ≀ n ≀ 3Β·105). The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 106) β€” the initial group that is given to Toastman.
Print a single integer β€” the largest possible score.
Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and ...
Input: 33 1 5 | Output: 26
Easy
2
679
173
52
4
2,073
K
2073K
2,900
Master
0
0
0
0
20
215
E
215E
E. Periodical Numbers
2,100
combinatorics; dp; number theory
A non-empty string s is called binary, if it consists only of characters ""0"" and ""1"". Let's number the characters of binary string s from 1 to the string's length and let's denote the i-th character in string s as si.Binary string s with length n is periodical, if there is an integer 1 ≀ k < n such that: k is a div...
The single input line contains two integers l and r (1 ≀ l ≀ r ≀ 1018). The numbers are separated by a space.Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
Print a single integer, showing how many periodic numbers are in the interval from l to r (both ends are included).
In the first sample periodic numbers are 3, 7 and 10.In the second sample periodic numbers are 31 and 36.
Input: 1 10 | Output: 3
Hard
3
725
256
115
2
2,003
D2
2003D2
D2. Turtle and a MEX Problem (Hard Version)
2,100
dfs and similar; dp; graphs; greedy; implementation; math
The two versions are different problems. In this version of the problem, you can't choose the same integer twice or more. You can make hacks only if both versions are solved.One day, Turtle was playing with \(n\) sequences. Let the length of the \(i\)-th sequence be \(l_i\). Then the \(i\)-th sequence was \(a_{i, 1}, a...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The first line of each test case contains two integers \(n, m\) (\(1 \le n \le 2 \cdot 10^5, 0 \le m \le 10^9\)).Each of the following \(n\) lines contains sev...
For each test case, output a single integer β€” the value of \(\sum\limits_{i = 0}^m f(i)\).
In the first test case, when \(x\) is initially \(2\), Turtle can choose \(i = 3\) and set \(x\) to \(\text{mex}(x, a_{3, 1}, a_{3, 2}, a_{3, 3}, a_{3, 4}) = \text{mex}(2, 7, 0, 1, 5) = 3\). It can be proved that Turtle can't make the value of \(x\) greater than \(3\), so \(f(2) = 3\).It can be seen that \(f(0) = 3\), ...
Input: 63 42 0 23 2 3 34 7 0 1 53 45 0 2 0 4 111 15 1 3 0 3 32 502 1 22 1 21 17 1 2 4 1 4 9 54 1145142 2 25 7 3 6 0 33 0 1 15 0 9 2 1 55 19198101 22 324003 03 1416324 2 14607284 1312631 2 0 14151955 1223554 192248 2 1492515 725556 | Output: 16 18 1281 4 6556785365 1842836177961
Hard
6
1,523
771
90
20
1,030
A
1030A
A. In Search of an Easy Problem
800
implementation
When preparing a tournament, Codeforces coordinators try treir best to make the first problem as easy as possible. This time the coordinator had chosen some problem and asked \(n\) people about their opinions. Each person answered whether this problem is easy or hard.If at least one of these \(n\) people has answered t...
The first line contains a single integer \(n\) (\(1 \le n \le 100\)) β€” the number of people who were asked to give their opinions.The second line contains \(n\) integers, each integer is either \(0\) or \(1\). If \(i\)-th integer is \(0\), then \(i\)-th person thinks that the problem is easy; if it is \(1\), then \(i\)...
Print one word: ""EASY"" if the problem is easy according to all responses, or ""HARD"" if there is at least one person who thinks the problem is hard. You may print every letter in any register: ""EASY"", ""easy"", ""EaSY"" and ""eAsY"" all will be processed correctly.
In the first example the third person says it's a hard problem, so it should be replaced.In the second example the problem easy for the only person, so it doesn't have to be replaced.
Input: 30 0 1 | Output: HARD
Beginner
1
453
363
270
10
401
E
401E
E. Olympic Games
2,500
math
This problem was deleted from the contest, because it was used previously at another competition.
Input: 1 11 2 100 | Output: 6
Expert
1
97
0
0
4
1,427
C
1427C
C. The Hard Work of Paparazzi
2,000
dp
You are a paparazzi working in Manhattan.Manhattan has \(r\) south-to-north streets, denoted by numbers \(1, 2,\ldots, r\) in order from west to east, and \(r\) west-to-east streets, denoted by numbers \(1,2,\ldots,r\) in order from south to north. Each of the \(r\) south-to-north streets intersects each of the \(r\) w...
The first line of the input contains two positive integers \(r, n\) (\(1\le r\le 500\), \(1\le n\le 100,000\)) – the number of south-to-north/west-to-east streets and the number of celebrities.Then \(n\) lines follow, each describing the appearance of a celebrity. The \(i\)-th of these lines contains \(3\) positive int...
Print a single integer, the maximum number of celebrities you can take a photo of.
Explanation of the first testcase: There is only one celebrity in the city, and he will be at intersection \((6,8)\) exactly \(11\) minutes after the beginning of the working day. Since you are initially at \((1,1)\) and you need \(|1-6|+|1-8|=5+7=12\) minutes to reach \((6,8)\) you cannot take a photo of the celebrity...
Input: 10 1 11 6 8 | Output: 0
Hard
1
1,356
578
82
14
1,607
H
1607H
H. Banquet Preparations 2
2,200
greedy; sortings; two pointers
The chef has cooked \(n\) dishes yet again: the \(i\)-th dish consists of \(a_i\) grams of fish and \(b_i\) grams of meat. Banquet organizers consider two dishes \(i\) and \(j\) equal if \(a_i=a_j\) and \(b_i=b_j\) at the same time.The banquet organizers estimate the variety of \(n\) dishes as follows. The variety of a...
The first line of input data contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β€” the number of test cases.Each test case's description is preceded by a blank line. Next comes a line that contains an integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β€” the number of dishes. Then follows \(n\) lines, \(i\)-th of which conta...
For each test case, print on the first line the minimum value of variety that can be achieved by eating exactly \(m_i\) grams of food (for all \(i\) from \(1\) to \(n\)) from a dish \(i\).Then print \(n\) lines that describe a way to do this: the \(i\)-th line should contain two integers \(x_i\) and \(y_i\) (\(0 \leq x...
Input: 5 3 10 10 2 9 9 0 10 9 1 2 3 4 1 5 1 2 3 7 2 5 6 5 4 5 5 6 1 13 42 50 5 5 7 12 3 1 4 7 3 7 0 0 0 4 1 5 | Output: 1 1 1 0 0 1 0 2 0 1 1 1 2 3 2 0 4 1 5 1 8 42 2 5 7 3 1 4 3 0 0 4 1
Hard
3
909
661
572
16
1,188
C
1188C
C. Array Beauty
2,500
dp
Let's call beauty of an array \(b_1, b_2, \ldots, b_n\) (\(n > 1\)) β€” \(\min\limits_{1 \leq i < j \leq n} |b_i - b_j|\).You're given an array \(a_1, a_2, \ldots a_n\) and a number \(k\). Calculate the sum of beauty over all subsequences of the array of length exactly \(k\). As this number can be very large, output it m...
The first line contains integers \(n, k\) (\(2 \le k \le n \le 1000\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 10^5\)).
Output one integer β€” the sum of beauty over all subsequences of the array of length exactly \(k\). As this number can be very large, output it modulo \(998244353\).
In the first example, there are \(4\) subsequences of length \(3\) β€” \([1, 7, 3]\), \([1, 3, 5]\), \([7, 3, 5]\), \([1, 7, 5]\), each of which has beauty \(2\), so answer is \(8\).In the second example, there is only one subsequence of length \(5\) β€” the whole array, which has the beauty equal to \(|10-1| = 9\).
Input: 4 3 1 7 3 5 | Output: 8
Expert
1
484
162
164
11
1,305
G
1305G
G. Kuroni and Antihype
3,500
bitmasks; brute force; dp; dsu; graphs
Kuroni isn't good at economics. So he decided to found a new financial pyramid called Antihype. It has the following rules: You can join the pyramid for free and get \(0\) coins. If you are already a member of Antihype, you can invite your friend who is currently not a member of Antihype, and get a number of coins equa...
The first line contains a single integer \(n\) (\(1\le n \le 2\cdot 10^5\)) β€” the number of people.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0\le a_i \le 2\cdot 10^5\)) β€” the ages of the people.
Output exactly one integer β€” the maximum possible combined gainings of all \(n\) people.
Only the first and second persons are friends. The second can join Antihype and invite the first one, getting \(2\) for it.
Input: 3 1 2 3 | Output: 2
Master
5
872
220
88
13
86
E
86E
E. Long sequence
2,700
brute force; math; matrices
A sequence a0, a1, ... is called a recurrent binary sequence, if each term ai (i = 0, 1, ...) is equal to 0 or 1 and there exist coefficients such that an = c1Β·an - 1 + c2Β·an - 2 + ... + ckΒ·an - k (mod 2), for all n β‰₯ k. Assume that not all of ci are zeros.Note that such a sequence can be uniquely recovered from any k-...
Input contains a single integer k (2 ≀ k ≀ 50).
If there is no long sequence for a given k, output ""-1"" (without quotes). Otherwise the first line of the output should contain k integer numbers: c1, c2, ..., ck (coefficients). The second line should contain first k elements of the sequence: a0, a1, ..., ak - 1. All of them (elements and coefficients) should be equ...
1. In the first sample: c1 = 1, c2 = 1, so an = an - 1 + an - 2 (mod 2). Thus the sequence will be:so its period equals 3 = 22 - 1.2. In the second sample: c1 = 0, c2 = 1, c3 = 1, so an = an - 2 + an - 3 (mod 2). Thus our sequence is:and its period equals 7 = 23 - 1.Periods are colored.
Input: 2 | Output: 1 11 0
Master
3
786
47
418
0
1,208
A
1208A
A. XORinacci
900
math
Cengiz recently learned Fibonacci numbers and now he is studying different algorithms to find them. After getting bored of reading them, he came with his own new type of numbers that he named XORinacci numbers. He defined them as follows: \(f(0) = a\); \(f(1) = b\); \(f(n) = f(n-1) \oplus f(n-2)\) when \(n > 1\), where...
The input contains one or more independent test cases.The first line of input contains a single integer \(T\) (\(1 \le T \le 10^3\)), the number of test cases.Each of the \(T\) following lines contains three space-separated integers \(a\), \(b\), and \(n\) (\(0 \le a, b, n \le 10^9\)) respectively.
For each test case, output \(f(n)\).
In the first example, \(f(2) = f(0) \oplus f(1) = 3 \oplus 4 = 7\).
Input: 3 3 4 2 4 5 0 325 265 1231232 | Output: 7 4 76
Beginner
1
492
299
36
12
2,070
B
2070B
B. Robot Program
1,100
brute force; implementation; math
There is a robot on the coordinate line. Initially, the robot is located at the point \(x\) (\(x \ne 0\)). The robot has a sequence of commands of length \(n\) consisting of characters, where L represents a move to the left by one unit (from point \(p\) to point \((p-1)\)) and R represents a move to the right by one un...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The first line of a test case contains three integers \(n\), \(x\) and \(k\) (\(1 \le n \le 2 \cdot 10^5\); \(-n \le x \le n\); \(n \le k \le 10^{18}\)).The second line of a test case contains a string \(s\) consisting of \...
For each test case, print a single integer β€” the number of times the robot will enter the point \(0\) during the next \(k\) seconds.
In the first example, the robot moves as follows: \(2 \rightarrow 1 \rightarrow \underline{0} \rightarrow -1 \rightarrow -2 \rightarrow -1\). The robot has completed all instructions from the sequence and is not at \(0\). So it stops after \(5\) seconds and the point \(0\) is entered once.In the second example, the rob...
Input: 63 2 6LLR2 -1 8RL4 -2 5LRRR5 3 7LRRLL1 1 1L3 -1 4846549234412827RLR | Output: 1 4 1 0 1 2423274617206414
Easy
3
831
452
132
20
755
E
755E
E. PolandBall and White-Red graph
2,400
constructive algorithms; graphs; shortest paths
PolandBall has an undirected simple graph consisting of n vertices. Unfortunately, it has no edges. The graph is very sad because of that. PolandBall wanted to make it happier, adding some red edges. Then, he will add white edges in every remaining place. Therefore, the final graph will be a clique in two colors: white...
The only one input line contains two integers n and k (2 ≀ n ≀ 1000, 1 ≀ k ≀ 1000), representing graph's size and sought colorfulness.
If it's impossible to find a suitable graph, print -1.Otherwise, you can output any graph which fulfills PolandBall's requirements. First, output m β€” the number of red edges in your graph. Then, you should output m lines, each containing two integers ai and bi, (1 ≀ ai, bi ≀ n, ai β‰  bi) which means that there is an und...
In the first sample case, no graph can fulfill PolandBall's requirements.In the second sample case, red graph is a path from 1 to 5. Its diameter is 4. However, white graph has diameter 2, because it consists of edges 1-3, 1-4, 1-5, 2-4, 2-5, 3-5.
Input: 4 1 | Output: -1
Expert
3
836
134
583
7
1,251
F
1251F
F. Red-White Fence
2,500
combinatorics; fft
Polycarp wants to build a fence near his house. He has \(n\) white boards and \(k\) red boards he can use to build it. Each board is characterised by its length, which is an integer.A good fence should consist of exactly one red board and several (possibly zero) white boards. The red board should be the longest one in ...
The first line contains two integers \(n\) and \(k\) (\(1 \le n \le 3 \cdot 10^5\), \(1 \le k \le 5\)) β€” the number of white and red boards Polycarp has.The second line contains \(n\) integers \(a_1\), \(a_2\), ..., \(a_n\) (\(1 \le a_i \le 3 \cdot 10^5\)) β€” the lengths of white boards Polycarp has.The third line conta...
For each \(Q_i\), print one integer β€” the number of good fences with perimeter \(Q_i\) Polycarp can build, taken modulo \(998244353\).
Possible fences in the first example denoted by their arrays of lengths (the length of the red board is highlighted): with perimeter \(6\): \([\textbf{2}]\); with perimeter \(8\): \([1, \textbf{2}]\), \([\textbf{2}, 1]\); with perimeter \(10\): \([1, \textbf{2}, 1]\), \([\textbf{4}]\); with perimeter \(12\): \([1, \tex...
Input: 5 2 3 3 1 1 1 2 4 7 6 8 10 12 14 16 18 | Output: 1 2 2 4 6 4 1
Expert
2
1,637
742
134
12
1,191
A
1191A
A. Tokitsukaze and Enhancement
800
brute force
Tokitsukaze is one of the characters in the game ""Kantai Collection"". In this game, every character has a common attribute β€” health points, shortened to HP.In general, different values of HP are grouped into \(4\) categories: Category \(A\) if HP is in the form of \((4 n + 1)\), that is, when divided by \(4\), the re...
The only line contains a single integer \(x\) (\(30 \leq x \leq 100\)) β€” the value Tokitsukaze's HP currently.
Print an integer \(a\) (\(0 \leq a \leq 2\)) and an uppercase letter \(b\) (\(b \in \lbrace A, B, C, D \rbrace\)), representing that the best way is to increase her HP by \(a\), and then the category becomes \(b\).Note that the output characters are case-sensitive.
For the first example, the category of Tokitsukaze's HP is already \(A\), so you don't need to enhance her ability.For the second example: If you don't increase her HP, its value is still \(98\), which equals to \((4 \times 24 + 2)\), and its category is \(C\). If you increase her HP by \(1\), its value becomes \(99\),...
Input: 33 | Output: 0 A
Beginner
1
1,116
110
265
11
549
A
549A
A. Face Detection
900
implementation; strings
The developers of Looksery have to write an efficient algorithm that detects faces on a picture. Unfortunately, they are currently busy preparing a contest for you, so you will have to do it for them. In this problem an image is a rectangular table that consists of lowercase Latin letters. A face on the image is a 2 Γ— ...
The first line contains two space-separated integers, n and m (1 ≀ n, m ≀ 50) β€” the height and the width of the image, respectively.Next n lines define the image. Each line contains m lowercase Latin letters.
In the single line print the number of faces on the image.
In the first sample the image contains a single face, located in a square with the upper left corner at the second line and the second column: In the second sample the image also contains exactly one face, its upper left corner is at the second row and the first column.In the third sample two faces are shown: In the fo...
Input: 4 4xxxxxfaxxcexxxxx | Output: 1
Beginner
2
536
208
58
5
546
D
546D
D. Soldier and Number Game
1,700
constructive algorithms; dp; math; number theory
Two soldiers are playing a game. At the beginning first of them chooses a positive integer n and gives it to the second soldier. Then the second one tries to make maximum possible number of rounds. Each round consists of choosing a positive integer x > 1, such that n is divisible by x and replacing n with n / x. When n...
First line of input consists of single integer t (1 ≀ t ≀ 1 000 000) denoting number of games soldiers play.Then follow t lines, each contains pair of integers a and b (1 ≀ b ≀ a ≀ 5 000 000) defining the value of n for a game.
For each game output a maximum score that the second soldier can get.
Input: 23 16 3 | Output: 25
Medium
4
767
227
69
5
164
C
164C
C. Machine Programming
2,400
flows; graphs
One remarkable day company ""X"" received k machines. And they were not simple machines, they were mechanical programmers! This was the last unsuccessful step before switching to android programmers, but that's another story.The company has now n tasks, for each of them we know the start time of its execution si, the d...
The first line contains two integer numbers n and k (1 ≀ n ≀ 1000, 1 ≀ k ≀ 50) β€” the numbers of tasks and machines, correspondingly.The next n lines contain space-separated groups of three integers si, ti, ci (1 ≀ si, ti ≀ 109, 1 ≀ ci ≀ 106), si is the time where they start executing the i-th task, ti is the duration o...
Print n integers x1, x2, ..., xn. Number xi should equal 1, if task i should be completed and otherwise it should equal 0.If there are several optimal solutions, print any of them.
In the first sample the tasks need to be executed at moments of time 2 ... 8, 1 ... 3 and 4 ... 4, correspondingly. The first task overlaps with the second and the third ones, so we can execute either task one (profit 5) or tasks two and three (profit 6).
Input: 3 12 7 51 3 34 1 3 | Output: 0 1 1
Expert
2
734
374
180
1
1,725
D
1725D
D. Deducing Sortability
2,900
binary search; bitmasks; math
Let's say Pak Chanek has an array \(A\) consisting of \(N\) positive integers. Pak Chanek will do a number of operations. In each operation, Pak Chanek will do the following: Choose an index \(p\) (\(1 \leq p \leq N\)). Let \(c\) be the number of operations that have been done on index \(p\) before this operation. Decr...
The first line contains two integers \(N\) and \(Q\) (\(1 \leq N \leq 10^9\), \(0 \leq Q \leq \min(N, 10^5)\)) β€” the required length of array \(A\) and the number of questions.The \(i\)-th of the next \(Q\) lines contains a single integer \(P_i\) (\(1 \leq P_1 < P_2 < \ldots < P_Q \leq N\)) β€” the index asked in the \(i...
Print \(Q+1\) lines. The \(1\)-st line contains an integer representing \(A_1 + A_2 + A_3 + A_4 + \ldots + A_N\). For each \(1 \leq i \leq Q\), the \((i+1)\)-th line contains an integer representing \(A_{P_i}\).
In the first example, the array \(A\) obtained is \([1, 2, 3, 3, 4, 4]\). We can see that the array is sortable by doing the following operations: Choose index \(5\), then \(A = [1, 2, 3, 3, 6, 4]\). Choose index \(6\), then \(A = [1, 2, 3, 3, 6, 6]\). Choose index \(4\), then \(A = [1, 2, 3, 4, 6, 6]\). Choose index \...
Input: 6 3 1 4 5 | Output: 17 1 3 4
Master
3
1,411
335
211
17
2,002
H
2002H
H. Counting 101
3,500
greedy
It's been a long summer's day, with the constant chirping of cicadas and the heat which never seemed to end. Finally, it has drawn to a close. The showdown has passed, the gates are open, and only a gentle breeze is left behind.Your predecessors had taken their final bow; it's your turn to take the stage.Sorting throug...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1\le t\le10^3\)). The description of the test cases follows. The only line of each test case contains two integers \(n\), \(m\) (\(1\le n\le 130\), \(1\le m\le 30\)).
For each test case, output \(\left\lfloor\frac{n+1}{2}\right\rfloor\) numbers. The \(i\)-th number is the number of valid sequences such that when used as input for Problem 101, the answer is \(i-1\), modulo \(10^9+7\).
In the first test case, there are \(2^3=8\) candidate sequences. Among them, you can operate on \([1,2,1]\) and \([1,1,1]\) once; you cannot operate on the other \(6\) sequences.
Input: 23 210 10 | Output: 6 2 1590121 23399118 382293180 213020758 379696760
Master
1
1,167
264
219
20
1,639
J
1639J
J. Treasure Hunt
0
graphs; interactive
All problems in this contest share the same statement, the only difference is the test your solution runs on. For further information on scoring please refer to ""Scoring"" section of the statement.This is an interactive problem.Imagine you are a treasure hunter, a very skillful one. One day you came across an ancient ...
Beginner
2
1,839
0
0
16
386
C
386C
C. Diverse Substrings
2,000
dp; strings; two pointers
String diversity is the number of symbols that occur in the string at least once. Diversity of s will be denoted by d(s). For example , d(""aaa"")=1, d(""abacaba"")=3.Given a string s, consisting of lowercase Latin letters. Consider all its substrings. Obviously, any substring diversity is a number from 1 to d(s). Find...
The input consists of a single line containing s. It contains only lowercase Latin letters, the length of s is from 1 to 3Β·105.
Print to the first line the value d(s). Print sequence t1, t2, ..., td(s) to the following lines, where ti is the number of substrings of s having diversity of exactly i.
Consider the first example.We denote by s(i, j) a substring of ""abca"" with the indices in the segment [i, j]. s(1, 1) = ""a"", d(""a"") = 1 s(2, 2) = ""b"", d(""b"") = 1 s(3, 3) = ""c"", d(""c"") = 1 s(4, 4) = ""a"", d(""a"") = 1 s(1, 2) = ""ab"", d(""ab"") = 2 s(2, 3) = ""bc"", d(""bc"") = 2 s(3, 4) = ""ca"", d(""ca...
Input: abca | Output: 3433
Hard
3
446
127
170
3
377
E
377E
E. Cookie Clicker
2,800
dp; geometry
Kostya is playing the computer game Cookie Clicker. The goal of this game is to gather cookies. You can get cookies using different buildings: you can just click a special field on the screen and get the cookies for the clicks, you can buy a cookie factory, an alchemy lab, a time machine and it all will bring lots and ...
The first line contains two integers n and s (1 ≀ n ≀ 2Β·105, 1 ≀ s ≀ 1016) β€” the number of buildings in the game and the number of cookies Kostya wants to earn.Each of the next n lines contains two integers vi and ci (1 ≀ vi ≀ 108, 0 ≀ ci ≀ 108) β€” the number of cookies the i-th building brings per second and the buildi...
Output the only integer β€” the minimum number of seconds Kostya needs to earn at least s cookies. It is guaranteed that he can do it.
Input: 3 91 02 35 4 | Output: 6
Master
2
1,243
331
132
3
1,545
E1
1545E1
E1. AquaMoon and Time Stop (easy version)
3,500
data structures; dp
Note that the differences between easy and hard versions are the constraints on \(n\) and the time limit. You can make hacks only if both versions are solved.AquaMoon knew through foresight that some ghosts wanted to curse tourists on a pedestrian street. But unfortunately, this time, these ghosts were hiding in a barr...
The first line contains a single integer \(n\) (\(1\le n\le 2000\)) β€” the number of curses.The next line contains a single integer \(x\) (\(1\le x\le 10^6\)) β€” the initial coordinate of the person.The following \(n\) lines contain four integers \(tl_i\), \(tr_i\), \(l_i\), \(r_i\) each (\(1\le tl_i\le tr_i\le 10^6\), \...
Print a single integer β€” the minimum energy which AquaMoon needs to spent, rounded up to the nearest integer (in case there are two nearest integers you should round the answer to the highest of them).
Input: 2 1 1 2 1 2 2 3 2 3 | Output: 2
Master
2
1,823
348
201
15
2,126
F
2126F
F. 1-1-1, Free Tree!
2,000
brute force; data structures; dfs and similar; graphs; implementation; trees
Given a tree\(^{\text{βˆ—}}\) with \(n\) vertices numbered from \(1\) to \(n\). Each vertex has an initial color \(a_i\).Each edge of the tree is defined by three numbers: \(u_i\), \(v_i\), and \(c_i\), where \(u_i\) and \(v_i\) are the endpoints of the edge, and \(c_i\) is the edge parameter. The cost of the edge is def...
The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The first line of each test case contains two integers \(n\) and \(q\) (\(1 \le n, q \le 2\cdot10^5\)) β€” the number of vertices and the number of queries, respectively.The second line contains \(n\) integers \(a_1, a_2, \dots, a_...
For each query, output a single integer on a separate line β€” the sum of the costs of all edges in the tree after applying the corresponding query.
First test: \(n =1\), one vertex β€” no edges. Query: repaint \(a_1\) to \(1\), the sum of costs is \(0\).Second test: \(n=2\), edge \(1 - 2\) (\(c=10\)). Queries: \(a_1 = 2\): colors [\(2, 1\)], cost is \(10\); \(a_2 = 2\): colors [\(2, 2\)], cost \(0\); \(a_1 = 1\): colors [\(1, 2\)], cost \(10\). Third test: \(n=5\), ...
Input: 41 111 12 31 11 2 101 22 21 15 41 2 1 2 31 2 52 3 32 4 44 5 73 25 21 22 34 31 1 2 21 2 22 3 62 4 83 14 12 2 | Output: 0 10 0 10 12 5 0 12 8 0 16
Hard
6
768
914
146
21
987
C
987C
C. Three displays
1,400
brute force; dp; implementation
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.There are \(n\) displays placed along a road, and the \(i\)-th of them can display a text with font size \(s_i\) only. Maria Stepanovna wants to r...
The first line contains a single integer \(n\) (\(3 \le n \le 3\,000\)) β€” the number of displays.The second line contains \(n\) integers \(s_1, s_2, \ldots, s_n\) (\(1 \le s_i \le 10^9\)) β€” the font sizes on the displays in the order they stand along the road.The third line contains \(n\) integers \(c_1, c_2, \ldots, c...
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer β€” the minimum total rent cost of three displays with indices \(i < j < k\) such that \(s_i < s_j < s_k\).
In the first example you can, for example, choose displays \(1\), \(4\) and \(5\), because \(s_1 < s_4 < s_5\) (\(2 < 4 < 10\)), and the rent cost is \(40 + 10 + 40 = 90\).In the second example you can't select a valid triple of indices, so the answer is -1.
Input: 52 4 5 4 1040 30 20 10 40 | Output: 90
Easy
3
628
384
206
9
1,992
A
1992A
A. Only Pluses
800
brute force; constructive algorithms; greedy; math; sortings
Kmes has written three integers \(a\), \(b\) and \(c\) in order to remember that he has to give Noobish_Monk \(a \times b \times c\) bananas.Noobish_Monk has found these integers and decided to do the following at most \(5\) times: pick one of these integers; increase it by \(1\). For example, if \(a = 2\), \(b = 3\) a...
Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases. The description of the test cases follows.The first and only line of each test case contains three integers \(a\), \(b\) and \(c\) (\(1 \le a, b, c \le 10\)) β€” Kmes's intege...
For each test case, output a single integer β€” the maximum amount of bananas Noobish_Monk can get.
Input: 22 3 410 1 10 | Output: 100 600
Beginner
5
626
323
97
19
1,325
F
1325F
F. Ehab's Last Theorem
2,500
constructive algorithms; dfs and similar; graphs; greedy
It's the year 5555. You have a graph, and you want to find a long cycle and a huge independent set, just because you can. But for now, let's just stick with finding either.Given a connected graph with \(n\) vertices, you can choose to either: find an independent set that has exactly \(\lceil\sqrt{n}\rceil\) vertices. f...
The first line contains two integers \(n\) and \(m\) (\(5 \le n \le 10^5\), \(n-1 \le m \le 2 \cdot 10^5\)) β€” the number of vertices and edges in the graph.Each of the next \(m\) lines contains two space-separated integers \(u\) and \(v\) (\(1 \le u,v \le n\)) that mean there's an edge between vertices \(u\) and \(v\)....
If you choose to solve the first problem, then on the first line print ""1"", followed by a line containing \(\lceil\sqrt{n}\rceil\) distinct integers not exceeding \(n\), the vertices in the desired independent set.If you, however, choose to solve the second problem, then on the first line print ""2"", followed by a l...
In the first sample:Notice that you can solve either problem, so printing the cycle \(2-4-3-1-5-6\) is also acceptable.In the second sample:Notice that if there are multiple answers you can print any, so printing the cycle \(2-5-6\), for example, is acceptable.In the third sample:
Input: 6 6 1 3 3 4 4 2 2 6 5 6 5 1 | Output: 1 1 6 4
Expert
4
636
418
556
13
1,216
E1
1216E1
E1. Numerical Sequence (easy version)
1,900
binary search; brute force; math
The only difference between the easy and the hard versions is the maximum value of \(k\).You are given an infinite sequence of form ""112123123412345\(\dots\)"" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from \(1\) to \(1\), the second...
The first line of the input contains one integer \(q\) (\(1 \le q \le 500\)) β€” the number of queries.The \(i\)-th of the following \(q\) lines contains one integer \(k_i\) \((1 \le k_i \le 10^9)\) β€” the description of the corresponding query.
Print \(q\) lines. In the \(i\)-th line print one digit \(x_i\) \((0 \le x_i \le 9)\) β€” the answer to the query \(i\), i.e. \(x_i\) should be equal to the element at the position \(k_i\) of the sequence.
Answers on queries from the first example are described in the problem statement.
Input: 5 1 3 20 38 56 | Output: 1 2 5 2 0
Hard
3
1,020
242
203
12
448
B
448B
B. Suffix Structures
1,400
implementation; strings
Bizon the Champion isn't just a bison. He also is a favorite of the ""Bizons"" team.At a competition the ""Bizons"" got the following problem: ""You are given two distinct words (strings of English letters), s and t. You need to transform word s into word t"". The task looked simple to the guys because they know the su...
The first line contains a non-empty word s. The second line contains a non-empty word t. Words s and t are different. Each word consists only of lowercase English letters. Each word contains at most 100 letters.
In the single line print the answer to the problem. Print ""need tree"" (without the quotes) if word s cannot be transformed into word t even with use of both suffix array and suffix automaton. Print ""automaton"" (without the quotes) if you need only the suffix automaton to solve the problem. Print ""array"" (without ...
In the third sample you can act like that: first transform ""both"" into ""oth"" by removing the first character using the suffix automaton and then make two swaps of the string using the suffix array and get ""hot"".
Input: automatontomat | Output: automaton
Easy
2
1,120
211
668
4
207
C1
207C1
C1. Game with Two Trees
2,100
The Smart Beaver from ABBYY has come up with a new developing game for children. The Beaver thinks that this game will help children to understand programming better.The main object of the game is finite rooted trees, each of their edges contains some lowercase English letter. Vertices on any tree are always numbered s...
The first line contains integer n β€” the number of operations on the trees. Next n lines specify the operations in the order of their execution. Each line has form ""t v c"", where t is the number of the tree, v is the vertex index in this tree, and c is a lowercase English letter.To get the full points for the first gr...
Print exactly n lines, each containing one integer β€” the number of existing good combinations after the corresponding operation from the input.Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
After the first operation the only good combination was (1, 1, 1). After the second operation new good combinations appeared, (2, 1, 2) and (1, 2, 2). The third operation didn't bring any good combinations. The fourth operation added good combination (1, 3, 3). Finally, the fifth operation resulted in as much as three ...
Input: 51 1 a2 1 a1 2 b2 1 b2 3 a | Output: 13347
Hard
0
2,655
607
290
2
226
D
226D
D. The table
2,100
constructive algorithms; greedy
Harry Potter has a difficult homework. Given a rectangular table, consisting of n Γ— m cells. Each cell of the table contains the integer. Harry knows how to use two spells: the first spell change the sign of the integers in the selected row, the second β€” in the selected column. Harry's task is to make non-negative the ...
The first line contains two integers n and m (1 ≀ n, m ≀ 100) β€” the number of rows and the number of columns. Next n lines follow, each contains m integers: j-th integer in the i-th line is ai, j (|ai, j| ≀ 100), the number in the i-th row and j-th column of the table.The rows of the table numbered from 1 to n. The col...
In the first line print the number a β€” the number of required applications of the first spell. Next print a space-separated integers β€” the row numbers, you want to apply a spell. These row numbers must be distinct!In the second line print the number b β€” the number of required applications of the second spell. Next prin...
Input: 4 1-1-1-1-1 | Output: 4 1 2 3 4 0
Hard
2
439
359
500
2
575
H
575H
H. Bots
1,800
combinatorics; dp; math; number theory
Sasha and Ira are two best friends. But they aren’t just friends, they are software engineers and experts in artificial intelligence. They are developing an algorithm for two bots playing a two-player game. The game is cooperative and turn based. In each turn, one of the players makes a move (it doesn’t matter which pl...
The first and only line contains integer N. 1 ≀ N ≀ 106
Output should contain a single integer – number of possible states modulo 109 + 7.
Start: Game is in state A. Turn 1: Either bot can make a move (first bot is red and second bot is blue), so there are two possible states after the first turn – B and C. Turn 2: In both states B and C, either bot can again make a turn, so the list of possible states is expanded to include D, E, F and G. Turn 3: Red bot...
Input: 2 | Output: 19
Medium
4
1,184
55
82
5
2,042
E
2042E
E. Vertex Pairs
2,900
binary search; brute force; data structures; dfs and similar; divide and conquer; greedy; implementation; trees
You are given a tree consisting of \(2n\) vertices. Recall that a tree is a connected undirected graph with no cycles. Each vertex has an integer from \(1\) to \(n\) written on it. Each value from \(1\) to \(n\) is written on exactly two different vertices. Each vertex also has a cost β€”vertex \(i\) costs \(2^i\).You ne...
The first line contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)).The second line contains \(2n\) integers \(a_1, a_2, \dots, a_{2n}\) (\(1 \le a_i \le n\)). Each value from \(1\) to \(n\) appears exactly twice.Each of the next \(2n-1\) lines contains two integers \(v\) and \(u\) (\(1 \le v, u \le 2n\)) β€” t...
In the first line, print a single integer \(k\) β€” the number of vertices in the subset.In the second line, print \(k\) distinct integers from \(1\) to \(2n\) β€” the indices of the vertices in the chosen subset. The vertices can be printed in an arbitrary order.
The images show the answers to the first two examples. The numbers in parentheses are the values written on the vertices.In the first example, there are valid subsets such as: \([2, 4, 5]\) (with a cost of \(2^2 + 2^4 + 2^5 = 52\)), \([2, 4, 5, 6]\) (with a cost of \(116\)), \([1, 6, 3]\) (with a cost of \(74\)), \([2,...
Input: 31 1 3 2 3 24 21 66 26 32 5 | Output: 3 2 4 5
Master
8
803
372
260
20
147
A
147A
A. Punctuation
1,300
implementation; strings
You are given a text that consists of lowercase Latin letters, spaces and punctuation marks (dot, comma, exclamation mark and question mark). A word is defined as a sequence of consecutive Latin letters.Your task is to add spaces to the text by the following rules: if there is no punctuation mark between two words, the...
The input data contains of a single non-empty line β€” the text whose length is no more than 10000 characters.
Print the text, edited according to the rules. In this problem you should follow the output format very strictly. For example, extra space at the end of the output line is considered as wrong answer. Note that a newline character at the end of the line doesn't matter.
Input: galileo galilei was an italian physicist ,mathematician,astronomer | Output: galileo galilei was an italian physicist, mathematician, astronomer
Easy
2
614
108
268
1
1,371
A
1371A
A. Magical Sticks
800
math
A penguin Rocher has \(n\) sticks. He has exactly one stick with length \(i\) for all \(1 \le i \le n\).He can connect some sticks. If he connects two sticks that have lengths \(a\) and \(b\), he gets one stick with length \(a + b\). Two sticks, that were used in the operation disappear from his set and the new connect...
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases. Next \(t\) lines contain descriptions of test cases.For each test case, the only line contains a single integer \(n\) (\(1 \le n \le 10^{9}\)).
For each test case, print a single integer β€” the answer to the problem.
In the third case, he can connect two sticks with lengths \(1\) and \(2\) and he will get one stick with length \(3\). So, he will have two sticks with lengths \(3\).In the fourth case, he can connect two sticks with lengths \(1\) and \(3\) and he will get one stick with length \(4\). After that, he will have three sti...
Input: 4 1 2 3 4 | Output: 1 1 2 2
Beginner
1
616
284
71
13